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