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.11 2006/05/18 13:51:45 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/socket.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/endian.h>
53 #include <sys/ucred.h>
54 #include <sys/serialize.h>
56 #include <machine/bus.h>
57 #include <machine/resource.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) printf x; } while (0)
86 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
88 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
91 #define DPRINTFN(n, x)
100 static const struct iwi_ident iwi_ident_table[] = {
101 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
102 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
103 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
104 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
109 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
110 static void iwi_dma_map_mbuf(void *, bus_dma_segment_t *, int, bus_size_t,
112 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
114 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
115 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
116 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
117 int, bus_addr_t, bus_addr_t);
118 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
119 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
120 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
122 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
123 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
124 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
125 static void iwi_node_free(struct ieee80211_node *);
126 static int iwi_media_change(struct ifnet *);
127 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
128 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
129 static int iwi_wme_update(struct ieee80211com *);
130 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
131 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
132 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
134 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
135 static void iwi_rx_intr(struct iwi_softc *);
136 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
137 static void iwi_intr(void *);
138 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
139 static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
140 static int iwi_tx_start(struct ifnet *, struct mbuf *,
141 struct ieee80211_node *, int);
142 static void iwi_start(struct ifnet *);
143 static void iwi_watchdog(struct ifnet *);
144 static int iwi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
145 static void iwi_stop_master(struct iwi_softc *);
146 static int iwi_reset(struct iwi_softc *);
147 static int iwi_load_ucode(struct iwi_softc *, void *, int);
148 static int iwi_load_firmware(struct iwi_softc *, void *, int);
149 static int iwi_cache_firmware(struct iwi_softc *, void *);
150 static void iwi_free_firmware(struct iwi_softc *);
151 static int iwi_config(struct iwi_softc *);
152 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
153 static int iwi_scan(struct iwi_softc *);
154 static int iwi_auth_and_assoc(struct iwi_softc *);
155 static void iwi_init(void *);
156 static void iwi_stop(void *);
157 static int iwi_sysctl_stats(SYSCTL_HANDLER_ARGS);
158 static int iwi_sysctl_radio(SYSCTL_HANDLER_ARGS);
160 static int iwi_probe(device_t);
161 static int iwi_attach(device_t);
162 static int iwi_detach(device_t);
163 static int iwi_shutdown(device_t);
164 static int iwi_suspend(device_t);
165 static int iwi_resume(device_t);
167 static int iwi_alloc_ibss_node(struct iwi_softc *);
168 static void iwi_free_ibss_node(struct iwi_softc *, int);
170 static device_method_t iwi_methods[] = {
171 /* Device interface */
172 DEVMETHOD(device_probe, iwi_probe),
173 DEVMETHOD(device_attach, iwi_attach),
174 DEVMETHOD(device_detach, iwi_detach),
175 DEVMETHOD(device_shutdown, iwi_shutdown),
176 DEVMETHOD(device_suspend, iwi_suspend),
177 DEVMETHOD(device_resume, iwi_resume),
182 static driver_t iwi_driver = {
185 sizeof (struct iwi_softc)
188 static devclass_t iwi_devclass;
190 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
192 MODULE_DEPEND(iwi, pci, 1, 1, 1);
193 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
196 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
198 static const struct ieee80211_rateset iwi_rateset_11a =
199 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
201 static const struct ieee80211_rateset iwi_rateset_11b =
202 { 4, { 2, 4, 11, 22 } };
204 static const struct ieee80211_rateset iwi_rateset_11g =
205 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
208 iwi_probe(device_t dev)
210 const struct iwi_ident *ident;
213 vid = pci_get_vendor(dev);
214 did = pci_get_device(dev);
215 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
216 if (vid == ident->vendor && did == ident->device) {
217 device_set_desc(dev, ident->name);
224 /* Base Address Register */
225 #define IWI_PCI_BAR0 0x10
228 iwi_attach(device_t dev)
230 struct iwi_softc *sc = device_get_softc(dev);
231 struct ieee80211com *ic = &sc->sc_ic;
232 struct ifnet *ifp = &ic->ic_if;
237 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
239 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
240 device_printf(dev, "chip is in D%d power mode "
241 "-- setting to D0\n", pci_get_powerstate(dev));
242 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
245 pci_write_config(dev, 0x41, 0, 1);
247 /* enable bus-mastering */
248 pci_enable_busmaster(dev);
250 sc->mem_rid = IWI_PCI_BAR0;
251 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
253 if (sc->mem == NULL) {
254 device_printf(dev, "could not allocate memory resource\n");
257 sc->sc_st = rman_get_bustag(sc->mem);
258 sc->sc_sh = rman_get_bushandle(sc->mem);
261 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
262 RF_ACTIVE | RF_SHAREABLE);
263 if (sc->irq == NULL) {
264 device_printf(dev, "could not allocate interrupt resource\n");
268 if (iwi_reset(sc) != 0) {
269 device_printf(dev, "could not reset adapter\n");
276 error = iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT);
278 device_printf(dev, "could not allocate Cmd ring\n");
282 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
283 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
285 device_printf(dev, "could not allocate Tx ring 1\n");
289 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
290 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
292 device_printf(dev, "could not allocate Tx ring 2\n");
296 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
297 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
299 device_printf(dev, "could not allocate Tx ring 3\n");
303 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
304 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
306 device_printf(dev, "could not allocate Tx ring 4\n");
310 error = iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT);
312 device_printf(dev, "could not allocate Rx ring\n");
316 sysctl_ctx_init(&sc->sysctl_ctx);
317 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
318 SYSCTL_STATIC_CHILDREN(_hw),
320 device_get_nameunit(dev),
323 if (sc->sysctl_tree == NULL) {
324 device_printf(dev, "sysctl add node failed\n");
330 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
331 ifp->if_init = iwi_init;
332 ifp->if_ioctl = iwi_ioctl;
333 ifp->if_start = iwi_start;
334 ifp->if_watchdog = iwi_watchdog;
335 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
336 ifq_set_ready(&ifp->if_snd);
338 ic->ic_wme.wme_update = iwi_wme_update;
339 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
340 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
341 ic->ic_state = IEEE80211_S_INIT;
343 /* set device capabilities */
345 IEEE80211_C_IBSS | /* IBSS mode supported */
346 IEEE80211_C_MONITOR | /* monitor mode supported */
347 IEEE80211_C_TXPMGT | /* tx power management */
348 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
349 IEEE80211_C_WEP | /* WEP */
350 IEEE80211_C_WPA | /* 802.11i */
351 IEEE80211_C_WME; /* 802.11e */
353 /* read MAC address from EEPROM */
354 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
355 ic->ic_myaddr[0] = val & 0xff;
356 ic->ic_myaddr[1] = val >> 8;
357 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
358 ic->ic_myaddr[2] = val & 0xff;
359 ic->ic_myaddr[3] = val >> 8;
360 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
361 ic->ic_myaddr[4] = val & 0xff;
362 ic->ic_myaddr[5] = val >> 8;
364 if (pci_get_device(dev) >= 0x4223) {
365 /* set supported .11a rates (2915ABG only) */
366 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
368 /* set supported .11a channels */
369 for (i = 36; i <= 64; i += 4) {
370 ic->ic_channels[i].ic_freq =
371 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
372 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
374 for (i = 149; i <= 165; i += 4) {
375 ic->ic_channels[i].ic_freq =
376 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
377 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
381 /* set supported .11b and .11g rates */
382 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
383 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
385 /* set supported .11b and .11g channels (1 through 14) */
386 for (i = 1; i <= 14; i++) {
387 ic->ic_channels[i].ic_freq =
388 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
389 ic->ic_channels[i].ic_flags =
390 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
391 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
394 ieee80211_ifattach(ic);
395 /* override default methods */
396 ic->ic_node_alloc = iwi_node_alloc;
397 sc->sc_node_free = ic->ic_node_free;
398 ic->ic_node_free = iwi_node_free;
399 /* override state transition machine */
400 sc->sc_newstate = ic->ic_newstate;
401 ic->ic_newstate = iwi_newstate;
402 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
404 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
405 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
407 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
408 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
409 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
411 sc->sc_txtap_len = sizeof sc->sc_txtapu;
412 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
413 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
416 * Add a few sysctl knobs.
422 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
423 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "radio",
424 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
425 "radio transmitter switch state (0=off, 1=on)");
427 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
428 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "stats",
429 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
432 SYSCTL_ADD_INT(&sc->sysctl_ctx,
433 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
434 CTLFLAG_RW, &sc->dwelltime, 0,
435 "channel dwell time (ms) for AP/station scanning");
437 SYSCTL_ADD_INT(&sc->sysctl_ctx,
438 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "bluetooth",
439 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
441 SYSCTL_ADD_INT(&sc->sysctl_ctx,
442 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "antenna",
443 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
446 * Hook our interrupt after all initialization is complete.
448 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE, iwi_intr, sc,
449 &sc->sc_ih, ifp->if_serializer);
451 device_printf(dev, "could not set up interrupt\n");
454 ieee80211_ifdetach(ic);
459 ieee80211_announce(ic);
468 iwi_detach(device_t dev)
470 struct iwi_softc *sc = device_get_softc(dev);
471 struct ieee80211com *ic = &sc->sc_ic;
472 struct ifnet *ifp = ic->ic_ifp;
474 if (device_is_attached(dev)) {
475 lwkt_serialize_enter(ifp->if_serializer);
478 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
479 iwi_free_firmware(sc);
481 lwkt_serialize_exit(ifp->if_serializer);
484 ieee80211_ifdetach(ic);
487 iwi_free_cmd_ring(sc, &sc->cmdq);
488 iwi_free_tx_ring(sc, &sc->txq[0]);
489 iwi_free_tx_ring(sc, &sc->txq[1]);
490 iwi_free_tx_ring(sc, &sc->txq[2]);
491 iwi_free_tx_ring(sc, &sc->txq[3]);
492 iwi_free_rx_ring(sc, &sc->rxq);
495 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
498 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
500 if (sc->sysctl_tree != NULL)
501 sysctl_ctx_free(&sc->sysctl_ctx);
507 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
512 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
514 *(bus_addr_t *)arg = segs[0].ds_addr;
518 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
524 ring->cur = ring->next = 0;
526 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
527 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_CMD_DESC_SIZE, 1,
528 count * IWI_CMD_DESC_SIZE, 0, &ring->desc_dmat);
530 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
534 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
535 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
537 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
541 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
542 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
544 device_printf(sc->sc_dev, "could not load desc DMA map\n");
545 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
552 fail: iwi_free_cmd_ring(sc, ring);
557 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
560 ring->cur = ring->next = 0;
564 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
566 if (ring->desc != NULL) {
567 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
568 BUS_DMASYNC_POSTWRITE);
569 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
570 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);
576 ring->desc_dmat = NULL;
581 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
582 bus_addr_t csr_ridx, bus_addr_t csr_widx)
588 ring->cur = ring->next = 0;
589 ring->csr_ridx = csr_ridx;
590 ring->csr_widx = csr_widx;
592 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
593 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_TX_DESC_SIZE, 1,
594 count * IWI_TX_DESC_SIZE, 0, &ring->desc_dmat);
596 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
600 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
601 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
603 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
607 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
608 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
610 device_printf(sc->sc_dev, "could not load desc DMA map\n");
612 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
617 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
620 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
621 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWI_MAX_NSEG - 2,
622 MCLBYTES, 0, &ring->data_dmat);
624 device_printf(sc->sc_dev, "could not create data DMA tag\n");
628 for (i = 0; i < count; i++) {
629 error = bus_dmamap_create(ring->data_dmat, 0,
632 device_printf(sc->sc_dev, "could not create DMA map\n");
639 fail: iwi_free_tx_ring(sc, ring);
644 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
646 struct iwi_tx_data *data;
649 for (i = 0; i < ring->count; i++) {
650 data = &ring->data[i];
652 if (data->m != NULL) {
653 bus_dmamap_sync(ring->data_dmat, data->map,
654 BUS_DMASYNC_POSTWRITE);
655 bus_dmamap_unload(ring->data_dmat, data->map);
660 if (data->ni != NULL) {
661 ieee80211_free_node(data->ni);
667 ring->cur = ring->next = 0;
671 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
673 struct iwi_tx_data *data;
676 if (ring->desc != NULL) {
677 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
678 BUS_DMASYNC_POSTWRITE);
679 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
680 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);
686 ring->desc_dmat = NULL;
689 if (ring->data != NULL) {
690 for (i = 0; i < ring->count; i++) {
691 data = &ring->data[i];
693 if (data->m != NULL) {
694 bus_dmamap_sync(ring->data_dmat, data->map,
695 BUS_DMASYNC_POSTWRITE);
696 bus_dmamap_unload(ring->data_dmat, data->map);
701 if (data->ni != NULL) {
702 ieee80211_free_node(data->ni);
706 if (data->map != NULL) {
707 bus_dmamap_destroy(ring->data_dmat, data->map);
712 free(ring->data, M_DEVBUF);
716 if (ring->data_dmat != NULL) {
717 bus_dma_tag_destroy(ring->data_dmat);
718 ring->data_dmat = NULL;
723 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
725 struct iwi_rx_data *data;
731 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
734 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
735 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
738 device_printf(sc->sc_dev, "could not create data DMA tag\n");
742 for (i = 0; i < count; i++) {
743 data = &ring->data[i];
745 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
747 device_printf(sc->sc_dev, "could not create DMA map\n");
751 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
752 if (data->m == NULL) {
753 device_printf(sc->sc_dev,
754 "could not allocate rx mbuf\n");
759 error = bus_dmamap_load(ring->data_dmat, data->map,
760 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
763 device_printf(sc->sc_dev,
764 "could not load rx buf DMA map");
771 data->reg = IWI_CSR_RX_BASE + i * 4;
776 fail: iwi_free_rx_ring(sc, ring);
781 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
787 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
789 struct iwi_rx_data *data;
792 if (ring->data != NULL) {
793 for (i = 0; i < ring->count; i++) {
794 data = &ring->data[i];
796 if (data->m != NULL) {
797 bus_dmamap_sync(ring->data_dmat, data->map,
798 BUS_DMASYNC_POSTREAD);
799 bus_dmamap_unload(ring->data_dmat, data->map);
804 if (data->map != NULL) {
805 bus_dmamap_destroy(ring->data_dmat, data->map);
810 free(ring->data, M_DEVBUF);
814 if (ring->data_dmat != NULL) {
815 bus_dma_tag_destroy(ring->data_dmat);
816 ring->data_dmat = NULL;
821 iwi_shutdown(device_t dev)
823 struct iwi_softc *sc = device_get_softc(dev);
824 struct ifnet *ifp = &sc->sc_ic.ic_if;
826 lwkt_serialize_enter(ifp->if_serializer);
828 lwkt_serialize_exit(ifp->if_serializer);
834 iwi_suspend(device_t dev)
836 struct iwi_softc *sc = device_get_softc(dev);
837 struct ifnet *ifp = &sc->sc_ic.ic_if;
839 lwkt_serialize_enter(ifp->if_serializer);
841 lwkt_serialize_exit(ifp->if_serializer);
847 iwi_resume(device_t dev)
849 struct iwi_softc *sc = device_get_softc(dev);
850 struct ifnet *ifp = sc->sc_ic.ic_ifp;
852 lwkt_serialize_enter(ifp->if_serializer);
854 pci_write_config(dev, 0x41, 0, 1);
856 if (ifp->if_flags & IFF_UP) {
857 ifp->if_init(ifp->if_softc);
858 if (ifp->if_flags & IFF_RUNNING)
862 lwkt_serialize_exit(ifp->if_serializer);
867 static struct ieee80211_node *
868 iwi_node_alloc(struct ieee80211_node_table *nt)
872 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
882 iwi_node_free(struct ieee80211_node *ni)
884 struct ieee80211com *ic = ni->ni_ic;
885 struct iwi_softc *sc = ic->ic_ifp->if_softc;
886 struct iwi_node *in = (struct iwi_node *)ni;
888 if (in->in_station != -1)
889 iwi_free_ibss_node(sc, in->in_station);
891 sc->sc_node_free(ni);
895 iwi_media_change(struct ifnet *ifp)
897 struct iwi_softc *sc = ifp->if_softc;
900 ASSERT_SERIALIZED(ifp->if_serializer);
902 error = ieee80211_media_change(ifp);
903 if (error != ENETRESET)
906 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
912 * The firmware automatically adapts the transmit speed. We report its current
916 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
918 struct iwi_softc *sc = ifp->if_softc;
919 struct ieee80211com *ic = &sc->sc_ic;
920 #define N(a) (sizeof (a) / sizeof (a[0]))
921 static const struct {
927 { IWI_RATE_DS5, 11 },
928 { IWI_RATE_DS11, 22 },
929 { IWI_RATE_OFDM6, 12 },
930 { IWI_RATE_OFDM9, 18 },
931 { IWI_RATE_OFDM12, 24 },
932 { IWI_RATE_OFDM18, 36 },
933 { IWI_RATE_OFDM24, 48 },
934 { IWI_RATE_OFDM36, 72 },
935 { IWI_RATE_OFDM48, 96 },
936 { IWI_RATE_OFDM54, 108 },
941 imr->ifm_status = IFM_AVALID;
942 imr->ifm_active = IFM_IEEE80211;
943 if (ic->ic_state == IEEE80211_S_RUN)
944 imr->ifm_status |= IFM_ACTIVE;
946 /* read current transmission rate from adapter */
947 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
949 /* convert rate to 802.11 rate */
950 for (i = 0; i < N(rates) && rates[i].val != val; i++);
951 rate = (i < N(rates)) ? rates[i].rate : 0;
953 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
954 switch (ic->ic_opmode) {
955 case IEEE80211_M_STA:
958 case IEEE80211_M_IBSS:
959 imr->ifm_active |= IFM_IEEE80211_ADHOC;
962 case IEEE80211_M_MONITOR:
963 imr->ifm_active |= IFM_IEEE80211_MONITOR;
966 case IEEE80211_M_AHDEMO:
967 case IEEE80211_M_HOSTAP:
968 /* should not get there */
975 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
977 struct ifnet *ifp = ic->ic_ifp;
978 struct iwi_softc *sc = ifp->if_softc;
979 enum ieee80211_state ostate;
982 ostate = ic->ic_state;
985 case IEEE80211_S_SCAN:
986 if (sc->flags & IWI_FLAG_SCANNING)
989 ieee80211_node_table_reset(&ic->ic_scan);
990 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
991 sc->flags |= IWI_FLAG_SCANNING;
995 case IEEE80211_S_AUTH:
996 iwi_auth_and_assoc(sc);
999 case IEEE80211_S_RUN:
1000 if (ic->ic_opmode == IEEE80211_M_IBSS)
1001 iwi_auth_and_assoc(sc);
1002 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
1003 iwi_set_chan(sc, ic->ic_ibss_chan);
1006 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
1007 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp | IWI_LED_ASSOC);
1009 return sc->sc_newstate(ic, nstate,
1010 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1012 case IEEE80211_S_ASSOC:
1015 case IEEE80211_S_INIT:
1016 sc->flags &= ~IWI_FLAG_SCANNING;
1018 if (ostate != IEEE80211_S_RUN)
1022 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
1023 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp & ~IWI_LED_ASSOC);
1027 ic->ic_state = nstate;
1033 * WME parameters coming from IEEE 802.11e specification. These values are
1034 * already declared in ieee80211_proto.c, but they are static so they can't
1037 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1038 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1039 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1040 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1041 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1044 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1045 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1046 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1047 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1048 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1052 iwi_wme_update(struct ieee80211com *ic)
1054 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1055 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1056 struct iwi_softc *sc = ic->ic_ifp->if_softc;
1057 struct iwi_wme_params wme[3];
1058 const struct wmeParams *wmep;
1062 * We shall not override firmware default WME values if WME is not
1065 if (!(ic->ic_flags & IEEE80211_F_WME))
1068 for (ac = 0; ac < WME_NUM_AC; ac++) {
1069 /* set WME values for current operating mode */
1070 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1071 wme[0].aifsn[ac] = wmep->wmep_aifsn;
1072 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1073 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1074 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1075 wme[0].acm[ac] = wmep->wmep_acm;
1077 /* set WME values for CCK modulation */
1078 wmep = &iwi_wme_cck_params[ac];
1079 wme[1].aifsn[ac] = wmep->wmep_aifsn;
1080 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1081 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1082 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1083 wme[1].acm[ac] = wmep->wmep_acm;
1085 /* set WME values for OFDM modulation */
1086 wmep = &iwi_wme_ofdm_params[ac];
1087 wme[2].aifsn[ac] = wmep->wmep_aifsn;
1088 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1089 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1090 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1091 wme[2].acm[ac] = wmep->wmep_acm;
1094 DPRINTF(("Setting WME parameters\n"));
1095 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
1101 * Read 16 bits at address 'addr' from the serial EEPROM.
1104 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1110 /* Clock C once before the first command */
1111 IWI_EEPROM_CTL(sc, 0);
1112 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1113 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1114 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1116 /* Write start bit (1) */
1117 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1118 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1120 /* Write READ opcode (10) */
1121 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1122 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1123 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1124 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1126 /* Write address A7-A0 */
1127 for (n = 7; n >= 0; n--) {
1128 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1129 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1130 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1131 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1134 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1136 /* Read data Q15-Q0 */
1138 for (n = 15; n >= 0; n--) {
1139 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1140 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1141 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1142 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1145 IWI_EEPROM_CTL(sc, 0);
1147 /* Clear Chip Select and clock C */
1148 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1149 IWI_EEPROM_CTL(sc, 0);
1150 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1156 * XXX: Hack to set the current channel to the value advertised in beacons or
1157 * probe responses. Only used during AP detection.
1160 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1162 struct ieee80211_frame *wh;
1164 uint8_t *frm, *efrm;
1166 wh = mtod(m, struct ieee80211_frame *);
1168 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1171 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1173 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1174 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1177 frm = (uint8_t *)(wh + 1);
1178 efrm = mtod(m, uint8_t *) + m->m_len;
1180 frm += 12; /* skip tstamp, bintval and capinfo fields */
1181 while (frm < efrm) {
1182 if (*frm == IEEE80211_ELEMID_DSPARMS)
1183 #if IEEE80211_CHAN_MAX < 255
1184 if (frm[2] <= IEEE80211_CHAN_MAX)
1186 ic->ic_curchan = &ic->ic_channels[frm[2]];
1193 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1194 struct iwi_frame *frame)
1196 struct ieee80211com *ic = &sc->sc_ic;
1197 struct ifnet *ifp = ic->ic_ifp;
1198 struct mbuf *mnew, *m;
1199 struct ieee80211_frame *wh;
1200 struct ieee80211_node *ni;
1203 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1204 le16toh(frame->len), frame->chan, frame->rssi_dbm));
1206 if (le16toh(frame->len) < sizeof (struct ieee80211_frame))
1210 * Try to allocate a new mbuf for this ring element and load it before
1211 * processing the current mbuf. If the ring element cannot be loaded,
1212 * drop the received packet and reuse the old mbuf. In the unlikely
1213 * case that the old mbuf can't be reloaded either, explicitly panic.
1215 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1221 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1223 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1224 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1229 /* try to reload the old mbuf */
1230 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1231 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1232 &data->physaddr, 0);
1234 /* very unlikely that it will fail... */
1235 panic("%s: could not load old rx mbuf",
1236 device_get_name(sc->sc_dev));
1243 * New mbuf successfully loaded, update Rx ring and continue
1248 CSR_WRITE_4(sc, data->reg, data->physaddr);
1251 m->m_pkthdr.rcvif = ifp;
1252 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1253 sizeof (struct iwi_frame) + le16toh(frame->len);
1255 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1257 if (ic->ic_state == IEEE80211_S_SCAN)
1258 iwi_fix_channel(ic, m);
1260 if (sc->sc_drvbpf != NULL) {
1261 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1264 tap->wr_rate = frame->rate;
1266 htole16(ic->ic_channels[frame->chan].ic_freq);
1267 tap->wr_chan_flags =
1268 htole16(ic->ic_channels[frame->chan].ic_flags);
1269 tap->wr_antsignal = frame->signal;
1270 tap->wr_antenna = frame->antenna;
1272 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1275 wh = mtod(m, struct ieee80211_frame *);
1276 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1278 /* send the frame to the 802.11 layer */
1279 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1281 /* node is no longer needed */
1282 ieee80211_free_node(ni);
1286 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1288 struct ieee80211com *ic = &sc->sc_ic;
1289 struct iwi_notif_scan_channel *chan;
1290 struct iwi_notif_scan_complete *scan;
1291 struct iwi_notif_authentication *auth;
1292 struct iwi_notif_association *assoc;
1294 switch (notif->type) {
1295 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1296 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1298 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
1301 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1302 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1304 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1307 /* monitor mode uses scan to set the channel ... */
1308 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1309 sc->flags &= ~IWI_FLAG_SCANNING;
1310 ieee80211_end_scan(ic);
1312 iwi_set_chan(sc, ic->ic_ibss_chan);
1315 case IWI_NOTIF_TYPE_AUTHENTICATION:
1316 auth = (struct iwi_notif_authentication *)(notif + 1);
1318 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1320 switch (auth->state) {
1321 case IWI_AUTHENTICATED:
1322 ieee80211_node_authorize(ic->ic_bss);
1323 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1326 case IWI_DEAUTHENTICATED:
1330 device_printf(sc->sc_dev,
1331 "unknown authentication state %u\n", auth->state);
1335 case IWI_NOTIF_TYPE_ASSOCIATION:
1336 assoc = (struct iwi_notif_association *)(notif + 1);
1338 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1341 switch (assoc->state) {
1342 case IWI_AUTHENTICATED:
1343 /* re-association, do nothing */
1346 case IWI_ASSOCIATED:
1347 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1350 case IWI_DEASSOCIATED:
1351 ieee80211_begin_scan(ic, 1);
1355 device_printf(sc->sc_dev,
1356 "unknown association state %u\n", assoc->state);
1361 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1366 iwi_rx_intr(struct iwi_softc *sc)
1368 struct iwi_rx_data *data;
1369 struct iwi_hdr *hdr;
1372 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1374 for (; sc->rxq.cur != hw;) {
1375 data = &sc->rxq.data[sc->rxq.cur];
1377 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1378 BUS_DMASYNC_POSTREAD);
1380 hdr = mtod(data->m, struct iwi_hdr *);
1382 switch (hdr->type) {
1383 case IWI_HDR_TYPE_FRAME:
1384 iwi_frame_intr(sc, data, sc->rxq.cur,
1385 (struct iwi_frame *)(hdr + 1));
1388 case IWI_HDR_TYPE_NOTIF:
1389 iwi_notification_intr(sc,
1390 (struct iwi_notif *)(hdr + 1));
1394 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1398 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1400 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1403 /* Tell the firmware what we have processed */
1404 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1405 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1409 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1411 struct ieee80211com *ic = &sc->sc_ic;
1412 struct ifnet *ifp = ic->ic_ifp;
1413 struct iwi_tx_data *data;
1416 hw = CSR_READ_4(sc, txq->csr_ridx);
1418 for (; txq->next != hw;) {
1419 data = &txq->data[txq->next];
1421 bus_dmamap_sync(txq->data_dmat, data->map,
1422 BUS_DMASYNC_POSTWRITE);
1423 bus_dmamap_unload(txq->data_dmat, data->map);
1426 ieee80211_free_node(data->ni);
1429 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1434 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1437 sc->sc_tx_timer = 0;
1438 ifp->if_flags &= ~IFF_OACTIVE;
1445 struct iwi_softc *sc = arg;
1448 r = CSR_READ_4(sc, IWI_CSR_INTR);
1449 if (r == 0 || r == 0xffffffff)
1452 /* disable interrupts */
1453 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1455 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
1456 device_printf(sc->sc_dev, "fatal error\n");
1457 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1461 if (r & IWI_INTR_FW_INITED) {
1462 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1463 wakeup(IWI_FW_INITIALIZED(sc));
1466 if (r & IWI_INTR_RADIO_OFF) {
1467 DPRINTF(("radio transmitter turned off\n"));
1468 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1472 if (r & IWI_INTR_CMD_DONE)
1473 wakeup(IWI_FW_CMD_ACKED(sc));
1475 if (r & IWI_INTR_TX1_DONE)
1476 iwi_tx_intr(sc, &sc->txq[0]);
1478 if (r & IWI_INTR_TX2_DONE)
1479 iwi_tx_intr(sc, &sc->txq[1]);
1481 if (r & IWI_INTR_TX3_DONE)
1482 iwi_tx_intr(sc, &sc->txq[2]);
1484 if (r & IWI_INTR_TX4_DONE)
1485 iwi_tx_intr(sc, &sc->txq[3]);
1487 if (r & IWI_INTR_RX_DONE)
1490 /* acknowledge interrupts */
1491 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1493 /* re-enable interrupts */
1494 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1498 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async)
1500 struct iwi_cmd_desc *desc;
1501 struct ifnet *ifp = &sc->sc_ic.ic_if;
1504 desc = &sc->cmdq.desc[sc->cmdq.cur];
1506 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1507 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1510 memcpy(desc->data, data, len);
1512 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1513 BUS_DMASYNC_PREWRITE);
1515 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1518 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1519 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1522 ASSERT_SERIALIZED(ifp->if_serializer);
1525 tsleep_interlock(IWI_FW_CMD_ACKED(sc));
1526 lwkt_serialize_exit(ifp->if_serializer);
1527 ret = tsleep(IWI_FW_CMD_ACKED(sc), 0, "iwicmd", hz);
1529 lwkt_serialize_enter(ifp->if_serializer);
1538 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
1540 struct iwi_ibssnode node;
1542 /* write node information into NIC memory */
1543 memset(&node, 0, sizeof node);
1544 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
1546 CSR_WRITE_REGION_1(sc,
1547 IWI_CSR_NODE_BASE + in->in_station * sizeof node,
1548 (uint8_t *)&node, sizeof node);
1551 struct iwi_dma_mapping {
1552 bus_dma_segment_t segs[IWI_MAX_NSEG];
1558 iwi_dma_map_mbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1559 bus_size_t mapsize, int error)
1561 struct iwi_dma_mapping *map = arg;
1566 KASSERT(nseg <= IWI_MAX_NSEG, ("too many DMA segments %d", nseg));
1568 bcopy(segs, map->segs, nseg * sizeof(bus_dma_segment_t));
1570 map->mapsize = mapsize;
1574 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1577 struct iwi_softc *sc = ifp->if_softc;
1578 struct ieee80211com *ic = &sc->sc_ic;
1579 struct iwi_node *in = (struct iwi_node *)ni;
1580 struct ieee80211_frame *wh;
1581 struct ieee80211_key *k;
1582 const struct chanAccParams *cap;
1583 struct iwi_tx_ring *txq = &sc->txq[ac];
1584 struct iwi_tx_data *data;
1585 struct iwi_tx_desc *desc;
1587 struct iwi_dma_mapping map;
1588 int error, hdrlen, i, noack = 0;
1590 wh = mtod(m0, struct ieee80211_frame *);
1592 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1593 hdrlen = sizeof (struct ieee80211_qosframe);
1594 cap = &ic->ic_wme.wme_chanParams;
1595 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1597 hdrlen = sizeof (struct ieee80211_frame);
1600 * This is only used in IBSS mode where the firmware expect an index
1601 * in a h/w table instead of a destination address.
1603 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
1604 in->in_station = iwi_alloc_ibss_node(sc);
1605 if (in->in_station == -1) { /* h/w table is full */
1607 ieee80211_free_node(ni);
1609 if_printf(ifp, "ibss table is full\n");
1612 iwi_write_ibssnode(sc, in);
1615 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1616 k = ieee80211_crypto_encap(ic, ni, m0);
1622 /* packet header may have moved, reset our local pointer */
1623 wh = mtod(m0, struct ieee80211_frame *);
1626 if (sc->sc_drvbpf != NULL) {
1627 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1630 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1631 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1633 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1636 data = &txq->data[txq->cur];
1637 desc = &txq->desc[txq->cur];
1639 /* save and trim IEEE802.11 header */
1640 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1643 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1644 iwi_dma_map_mbuf, &map, BUS_DMA_NOWAIT);
1645 if (error != 0 && error != EFBIG) {
1646 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1652 mnew = m_defrag(m0, MB_DONTWAIT);
1654 device_printf(sc->sc_dev,
1655 "could not defragment mbuf\n");
1661 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1662 iwi_dma_map_mbuf, &map,
1665 device_printf(sc->sc_dev,
1666 "could not map mbuf (error %d)\n", error);
1675 desc->hdr.type = IWI_HDR_TYPE_DATA;
1676 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1678 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
1679 desc->cmd = IWI_DATA_CMD_TX;
1680 desc->len = htole16(m0->m_pkthdr.len);
1684 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1685 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1688 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1689 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
1690 desc->weptxkey = ic->ic_crypto.cs_def_txkey;
1693 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1695 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1696 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1698 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
1699 desc->xflags |= IWI_DATA_XFLAG_QOS;
1701 desc->nseg = htole32(map.nseg);
1702 for (i = 0; i < map.nseg; i++) {
1703 desc->seg_addr[i] = htole32(map.segs[i].ds_addr);
1704 desc->seg_len[i] = htole16(map.segs[i].ds_len);
1707 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1708 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1710 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1711 ac, txq->cur, le16toh(desc->len), nsegs));
1714 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1715 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1721 iwi_start(struct ifnet *ifp)
1723 struct iwi_softc *sc = ifp->if_softc;
1724 struct ieee80211com *ic = &sc->sc_ic;
1726 struct ether_header *eh;
1727 struct ieee80211_node *ni;
1730 if (ic->ic_state != IEEE80211_S_RUN)
1734 m0 = ifq_dequeue(&ifp->if_snd, NULL);
1738 if (m0->m_len < sizeof (struct ether_header) &&
1739 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1743 eh = mtod(m0, struct ether_header *);
1744 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1751 /* classify mbuf so we can find which tx ring to use */
1752 if (ieee80211_classify(ic, m0, ni) != 0) {
1754 ieee80211_free_node(ni);
1759 /* no QoS encapsulation for EAPOL frames */
1760 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1761 M_WME_GETAC(m0) : WME_AC_BE;
1763 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1764 /* there is no place left in this ring */
1766 ieee80211_free_node(ni);
1767 ifp->if_flags |= IFF_OACTIVE;
1773 m0 = ieee80211_encap(ic, m0, ni);
1775 ieee80211_free_node(ni);
1780 if (ic->ic_rawbpf != NULL)
1781 bpf_mtap(ic->ic_rawbpf, m0);
1783 if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1784 ieee80211_free_node(ni);
1789 sc->sc_tx_timer = 5;
1795 iwi_watchdog(struct ifnet *ifp)
1797 struct iwi_softc *sc = ifp->if_softc;
1798 struct ieee80211com *ic = &sc->sc_ic;
1802 if (sc->sc_tx_timer > 0) {
1803 if (--sc->sc_tx_timer == 0) {
1804 if_printf(ifp, "device timeout\n");
1806 ifp->if_flags &= ~IFF_UP;
1813 ieee80211_watchdog(ic);
1817 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1819 struct iwi_softc *sc = ifp->if_softc;
1820 struct ieee80211com *ic = &sc->sc_ic;
1826 if (ifp->if_flags & IFF_UP) {
1827 if (!(ifp->if_flags & IFF_RUNNING))
1830 if (ifp->if_flags & IFF_RUNNING)
1836 /* only super-user can do that! */
1837 error = suser_cred(cr, NULL_CRED_OKAY);
1841 ifr = (struct ifreq *)data;
1842 error = iwi_cache_firmware(sc, ifr->ifr_data);
1846 /* only super-user can do that! */
1847 error = suser_cred(cr, NULL_CRED_OKAY);
1851 ifp->if_flags &= ~IFF_UP;
1853 iwi_free_firmware(sc);
1857 error = ieee80211_ioctl(ic, cmd, data, cr);
1860 if (error == ENETRESET) {
1861 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1862 (IFF_UP | IFF_RUNNING) &&
1863 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1872 iwi_stop_master(struct iwi_softc *sc)
1877 /* disable interrupts */
1878 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1880 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1881 for (ntries = 0; ntries < 5; ntries++) {
1882 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1887 if_printf(&sc->sc_ic.ic_if, "timeout waiting for master\n");
1889 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1890 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
1892 sc->flags &= ~IWI_FLAG_FW_INITED;
1896 iwi_reset(struct iwi_softc *sc)
1901 iwi_stop_master(sc);
1903 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1904 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1906 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1908 /* Wait for clock stabilization */
1909 for (ntries = 0; ntries < 1000; ntries++) {
1910 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1914 if (ntries == 1000) {
1915 if_printf(&sc->sc_ic.ic_if,
1916 "timeout waiting for clock stabilization\n");
1920 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1921 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
1925 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1926 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1928 /* Clear NIC memory */
1929 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1930 for (i = 0; i < 0xc000; i++)
1931 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1937 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1943 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1944 IWI_RST_STOP_MASTER);
1945 for (ntries = 0; ntries < 5; ntries++) {
1946 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1951 device_printf(sc->sc_dev, "timeout waiting for master\n");
1955 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1958 tmp = CSR_READ_4(sc, IWI_CSR_RST);
1959 tmp &= ~IWI_RST_PRINCETON_RESET;
1960 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
1963 MEM_WRITE_4(sc, 0x3000e0, 0);
1965 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 1);
1967 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 0);
1969 MEM_WRITE_1(sc, 0x200000, 0x00);
1970 MEM_WRITE_1(sc, 0x200000, 0x40);
1973 /* write microcode into adapter memory */
1974 for (w = uc; size > 0; w++, size -= 2)
1975 MEM_WRITE_2(sc, 0x200010, htole16(*w));
1977 MEM_WRITE_1(sc, 0x200000, 0x00);
1978 MEM_WRITE_1(sc, 0x200000, 0x80);
1980 /* wait until we get an answer */
1981 for (ntries = 0; ntries < 100; ntries++) {
1982 if (MEM_READ_1(sc, 0x200000) & 1)
1986 if (ntries == 100) {
1987 device_printf(sc->sc_dev,
1988 "timeout waiting for ucode to initialize\n");
1992 /* read the answer or the firmware will not initialize properly */
1993 for (i = 0; i < 7; i++)
1994 MEM_READ_4(sc, 0x200004);
1996 MEM_WRITE_1(sc, 0x200000, 0x00);
2001 /* macro to handle unaligned little endian data in firmware image */
2002 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2005 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2009 bus_addr_t physaddr;
2012 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2013 int ntries, error = 0;
2014 struct ifnet *ifp = &sc->sc_ic.ic_if;
2016 ASSERT_SERIALIZED(ifp->if_serializer);
2018 /* Allocate DMA memory for mapping firmware image */
2019 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
2020 BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, &dmat);
2022 device_printf(sc->sc_dev,
2023 "could not create firmware DMA tag\n");
2027 error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_WAITOK, &map);
2029 device_printf(sc->sc_dev,
2030 "could not allocate firmware DMA memory\n");
2034 error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr,
2037 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
2041 /* Copy firmware image to DMA memory */
2042 memcpy(virtaddr, fw, size);
2044 /* Make sure the adapter will get up-to-date values */
2045 bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE);
2047 /* Tell the adapter where the command blocks are stored */
2048 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2051 * Store command blocks into adapter's internal memory using register
2052 * indirections. The adapter will read the firmware image through DMA
2053 * using information stored in command blocks.
2058 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2061 dst = GETLE32(p); p += 4; src += 4;
2062 len = GETLE32(p); p += 4; src += 4;
2066 mlen = min(len, IWI_CB_MAXDATALEN);
2068 ctl = IWI_CB_DEFAULT_CTL | mlen;
2069 sum = ctl ^ src ^ dst;
2071 /* Write a command block */
2072 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2073 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2074 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2075 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2083 /* Write a fictive final command block (sentinel) */
2084 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2085 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2087 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2088 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2089 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2091 /* Tell the adapter to start processing command blocks */
2092 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2094 /* Wait until the adapter reaches the sentinel */
2095 for (ntries = 0; ntries < 400; ntries++) {
2096 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2100 if (ntries == 400) {
2101 device_printf(sc->sc_dev,
2102 "timeout processing command blocks\n");
2107 /* We're done with command blocks processing */
2108 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2110 /* Allow interrupts so we know when the firmware is ready */
2111 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2113 /* Tell the adapter to initialize the firmware */
2114 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2116 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2117 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2119 /* wait at most one second for firmware initialization to complete */
2121 tsleep_interlock(IWI_FW_INITIALIZED(sc));
2122 lwkt_serialize_exit(ifp->if_serializer);
2123 error = tsleep(IWI_FW_INITIALIZED(sc), 0, "iwiinit", hz);
2125 lwkt_serialize_enter(ifp->if_serializer);
2127 device_printf(sc->sc_dev, "timeout waiting for firmware "
2128 "initialization to complete\n");
2132 fail4: bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE);
2133 bus_dmamap_unload(dmat, map);
2134 fail3: bus_dmamem_free(dmat, virtaddr, map);
2135 fail2: bus_dma_tag_destroy(dmat);
2141 * Store firmware into kernel memory so we can download it when we need to,
2142 * e.g when the adapter wakes up from suspend mode.
2145 iwi_cache_firmware(struct iwi_softc *sc, void *data)
2147 struct iwi_firmware *kfw = &sc->fw;
2148 struct iwi_firmware ufw;
2151 iwi_free_firmware(sc);
2153 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
2156 kfw->boot_size = ufw.boot_size;
2157 kfw->ucode_size = ufw.ucode_size;
2158 kfw->main_size = ufw.main_size;
2160 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_WAITOK);
2161 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_WAITOK);
2162 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_WAITOK);
2164 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
2167 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
2170 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
2173 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2174 kfw->boot_size, kfw->ucode_size, kfw->main_size));
2176 sc->flags |= IWI_FLAG_FW_CACHED;
2181 free(kfw->boot, M_DEVBUF);
2182 free(kfw->ucode, M_DEVBUF);
2183 free(kfw->main, M_DEVBUF);
2189 iwi_free_firmware(struct iwi_softc *sc)
2191 if (!(sc->flags & IWI_FLAG_FW_CACHED))
2194 free(sc->fw.boot, M_DEVBUF);
2195 free(sc->fw.ucode, M_DEVBUF);
2196 free(sc->fw.main, M_DEVBUF);
2198 sc->flags &= ~IWI_FLAG_FW_CACHED;
2202 iwi_config(struct iwi_softc *sc)
2204 struct ieee80211com *ic = &sc->sc_ic;
2205 struct ifnet *ifp = ic->ic_ifp;
2206 struct iwi_configuration config;
2207 struct iwi_rateset rs;
2208 struct iwi_txpower power;
2209 struct ieee80211_key *wk;
2210 struct iwi_wep_key wepkey;
2214 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2215 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2216 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2217 IEEE80211_ADDR_LEN, 0);
2221 memset(&config, 0, sizeof config);
2222 config.bluetooth_coexistence = sc->bluetooth;
2223 config.antenna = sc->antenna;
2224 config.multicast_enabled = 1;
2225 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2226 config.disable_unicast_decryption = 1;
2227 config.disable_multicast_decryption = 1;
2228 DPRINTF(("Configuring adapter\n"));
2229 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0);
2233 data = htole32(IWI_POWER_MODE_CAM);
2234 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2235 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2239 data = htole32(ic->ic_rtsthreshold);
2240 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2241 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2245 data = htole32(ic->ic_fragthreshold);
2246 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2247 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2251 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2252 power.mode = IWI_MODE_11B;
2254 for (i = 0; i < 11; i++) {
2255 power.chan[i].chan = i + 1;
2256 power.chan[i].power = IWI_TXPOWER_MAX;
2258 DPRINTF(("Setting .11b channels tx power\n"));
2259 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2264 power.mode = IWI_MODE_11G;
2265 DPRINTF(("Setting .11g channels tx power\n"));
2266 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2272 rs.mode = IWI_MODE_11G;
2273 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2274 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2275 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2277 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2278 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2282 rs.mode = IWI_MODE_11A;
2283 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2284 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2285 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2287 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2288 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2292 /* if we have a desired ESSID, set it now */
2293 if (ic->ic_des_esslen != 0) {
2295 if (iwi_debug > 0) {
2296 printf("Setting desired ESSID to ");
2297 ieee80211_print_essid(ic->ic_des_essid,
2302 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
2303 ic->ic_des_esslen, 0);
2308 data = htole32(arc4random());
2309 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2310 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2314 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2315 wk = &ic->ic_crypto.cs_nw_keys[i];
2317 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2319 wepkey.len = wk->wk_keylen;
2320 memset(wepkey.key, 0, sizeof wepkey.key);
2321 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2322 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2324 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2330 /* Enable adapter */
2331 DPRINTF(("Enabling adapter\n"));
2332 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2336 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2338 struct ieee80211com *ic = &sc->sc_ic;
2339 struct iwi_scan scan;
2341 memset(&scan, 0, sizeof scan);
2342 memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type);
2343 scan.passive = htole16(2000);
2344 scan.channels[0] = 1 |
2345 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
2346 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2348 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2349 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2353 iwi_scan(struct iwi_softc *sc)
2355 struct ieee80211com *ic = &sc->sc_ic;
2356 struct iwi_scan scan;
2360 memset(&scan, 0, sizeof scan);
2362 if (ic->ic_des_esslen != 0) {
2363 scan.bdirected = htole16(sc->dwelltime);
2364 memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type);
2366 scan.broadcast = htole16(sc->dwelltime);
2367 memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type);
2372 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2373 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2374 isset(ic->ic_chan_active, i)) {
2379 *(p - count) = IWI_CHAN_5GHZ | count;
2381 p = (count > 0) ? p + 1 : scan.channels;
2383 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2384 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2385 isset(ic->ic_chan_active, i)) {
2390 *(p - count) = IWI_CHAN_2GHZ | count;
2392 DPRINTF(("Start scanning\n"));
2393 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2397 iwi_auth_and_assoc(struct iwi_softc *sc)
2399 struct ieee80211com *ic = &sc->sc_ic;
2400 struct ifnet *ifp = ic->ic_ifp;
2401 struct ieee80211_node *ni = ic->ic_bss;
2402 struct ieee80211_wme_info wme;
2403 struct iwi_configuration config;
2404 struct iwi_associate assoc;
2405 struct iwi_rateset rs;
2410 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2411 memset(&config, 0, sizeof config);
2412 config.bluetooth_coexistence = sc->bluetooth;
2413 config.antenna = sc->antenna;
2414 config.multicast_enabled = 1;
2415 config.use_protection = 1;
2416 config.answer_pbreq =
2417 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2418 config.disable_unicast_decryption = 1;
2419 config.disable_multicast_decryption = 1;
2420 DPRINTF(("Configuring adapter\n"));
2421 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config,
2428 if (iwi_debug > 0) {
2429 printf("Setting ESSID to ");
2430 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2434 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2438 /* the rate set has already been "negotiated" */
2439 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2441 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2442 rs.nrates = ni->ni_rates.rs_nrates;
2443 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2444 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
2445 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2449 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
2450 wme.wme_id = IEEE80211_ELEMID_VENDOR;
2451 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
2452 wme.wme_oui[0] = 0x00;
2453 wme.wme_oui[1] = 0x50;
2454 wme.wme_oui[2] = 0xf2;
2455 wme.wme_type = WME_OUI_TYPE;
2456 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
2457 wme.wme_version = WME_VERSION;
2460 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
2461 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
2466 if (ic->ic_opt_ie != NULL) {
2467 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2468 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2469 ic->ic_opt_ie_len, 1);
2474 data = htole32(ni->ni_rssi);
2475 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2476 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2480 memset(&assoc, 0, sizeof assoc);
2481 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2483 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2484 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2485 assoc.auth = ic->ic_crypto.cs_def_txkey << 4 | IWI_AUTH_SHARED;
2486 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
2487 assoc.policy |= htole16(IWI_POLICY_WME);
2488 if (ic->ic_flags & IEEE80211_F_WPA)
2489 assoc.policy |= htole16(IWI_POLICY_WPA);
2490 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2492 if (ic->ic_opmode == IEEE80211_M_IBSS)
2493 capinfo = IEEE80211_CAPINFO_IBSS;
2495 capinfo = IEEE80211_CAPINFO_ESS;
2496 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2497 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2498 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2499 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2500 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2501 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2502 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2503 assoc.capinfo = htole16(capinfo);
2505 assoc.lintval = htole16(ic->ic_lintval);
2506 assoc.intval = htole16(ni->ni_intval);
2507 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2508 if (ic->ic_opmode == IEEE80211_M_IBSS)
2509 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2511 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2513 DPRINTF(("Trying to associate to %6D channel %u auth %u\n",
2514 assoc.bssid, ":", assoc.chan, assoc.auth));
2515 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2519 iwi_init(void *priv)
2521 struct iwi_softc *sc = priv;
2522 struct ieee80211com *ic = &sc->sc_ic;
2523 struct ifnet *ifp = ic->ic_ifp;
2524 struct iwi_firmware *fw = &sc->fw;
2525 struct iwi_rx_data *data;
2528 /* exit immediately if firmware has not been ioctl'd */
2529 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2530 if (!(sc->flags & IWI_FLAG_FW_WARNED))
2531 device_printf(sc->sc_dev, "Please load firmware\n");
2532 sc->flags |= IWI_FLAG_FW_WARNED;
2533 ifp->if_flags &= ~IFF_UP;
2539 if (iwi_reset(sc) != 0) {
2540 device_printf(sc->sc_dev, "could not reset adapter\n");
2544 if (iwi_load_firmware(sc, fw->boot, fw->boot_size) != 0) {
2545 device_printf(sc->sc_dev, "could not load boot firmware\n");
2549 if (iwi_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2550 device_printf(sc->sc_dev, "could not load microcode\n");
2554 iwi_stop_master(sc);
2556 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
2557 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2558 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2560 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
2561 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
2562 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
2564 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
2565 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
2566 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
2568 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
2569 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
2570 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
2572 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
2573 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
2574 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
2576 for (i = 0; i < sc->rxq.count; i++) {
2577 data = &sc->rxq.data[i];
2578 CSR_WRITE_4(sc, data->reg, data->physaddr);
2581 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
2583 if (iwi_load_firmware(sc, fw->main, fw->main_size) != 0) {
2584 device_printf(sc->sc_dev, "could not load main firmware\n");
2588 sc->flags |= IWI_FLAG_FW_INITED;
2590 if (iwi_config(sc) != 0) {
2591 device_printf(sc->sc_dev, "device configuration failed\n");
2595 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2596 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2597 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2599 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2601 ifp->if_flags &= ~IFF_OACTIVE;
2602 ifp->if_flags |= IFF_RUNNING;
2606 fail: ifp->if_flags &= ~IFF_UP;
2611 iwi_stop(void *priv)
2613 struct iwi_softc *sc = priv;
2614 struct ieee80211com *ic = &sc->sc_ic;
2615 struct ifnet *ifp = ic->ic_ifp;
2617 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2619 iwi_stop_master(sc);
2621 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
2624 iwi_reset_cmd_ring(sc, &sc->cmdq);
2625 iwi_reset_tx_ring(sc, &sc->txq[0]);
2626 iwi_reset_tx_ring(sc, &sc->txq[1]);
2627 iwi_reset_tx_ring(sc, &sc->txq[2]);
2628 iwi_reset_tx_ring(sc, &sc->txq[3]);
2629 iwi_reset_rx_ring(sc, &sc->rxq);
2631 sc->sc_tx_timer = 0;
2633 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2637 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
2639 struct iwi_softc *sc = arg1;
2640 struct ifnet *ifp = &sc->sc_ic.ic_if;
2641 uint32_t size, buf[128];
2643 lwkt_serialize_enter(ifp->if_serializer);
2645 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
2646 memset(buf, 0, sizeof buf);
2650 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
2651 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
2653 lwkt_serialize_exit(ifp->if_serializer);
2654 return SYSCTL_OUT(req, buf, sizeof buf);
2658 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
2660 struct iwi_softc *sc = arg1;
2661 struct ifnet *ifp = &sc->sc_ic.ic_if;
2664 lwkt_serialize_enter(ifp->if_serializer);
2665 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
2666 lwkt_serialize_exit(ifp->if_serializer);
2668 return SYSCTL_OUT(req, &val, sizeof val);
2671 static const int8_t iwi_bitmap[256] = {
2672 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2673 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2674 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2675 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
2676 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2677 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2678 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2679 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7,
2680 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2681 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2682 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2683 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
2684 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2685 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2686 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2687 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, -1
2691 iwi_alloc_ibss_node(struct iwi_softc *sc)
2695 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
2697 for (i = 0; i < IWI_MAX_IBSSNODE_NBYTE; ++i) {
2700 ret = iwi_bitmap[sc->sc_ibss_node[i]];
2702 sc->sc_ibss_node[i] |= (1 << ret);
2711 iwi_free_ibss_node(struct iwi_softc *sc, int ibss_node)
2715 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
2716 KASSERT(ibss_node < IWI_MAX_IBSSNODE, ("free ibss node, out of range"));
2718 i = ibss_node / NBBY;
2719 b = ibss_node % NBBY;
2721 sc->sc_ibss_node[i] &= ~(1 << b);