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