1 /* $FreeBSD$ */ 2 3 /*- 4 * Copyright (c) 2004, 2005 5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /*- 34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver 35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm 36 */ 37 38 #include <sys/param.h> 39 #include <sys/sysctl.h> 40 #include <sys/sockio.h> 41 #include <sys/mbuf.h> 42 #include <sys/kernel.h> 43 #include <sys/socket.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 #include <sys/module.h> 47 #include <sys/bus.h> 48 #include <sys/endian.h> 49 50 #include <machine/bus.h> 51 #include <machine/resource.h> 52 #include <machine/clock.h> 53 #include <sys/rman.h> 54 55 #include <dev/pci/pcireg.h> 56 #include <dev/pci/pcivar.h> 57 58 #include <net/bpf.h> 59 #include <net/if.h> 60 #include <net/if_arp.h> 61 #include <net/ethernet.h> 62 #include <net/if_dl.h> 63 #include <net/if_media.h> 64 #include <net/if_types.h> 65 66 #include <net80211/ieee80211_var.h> 67 #include <net80211/ieee80211_radiotap.h> 68 69 #include <netinet/in.h> 70 #include <netinet/in_systm.h> 71 #include <netinet/in_var.h> 72 #include <netinet/ip.h> 73 #include <netinet/if_ether.h> 74 75 #include <dev/iwi/if_iwireg.h> 76 #include <dev/iwi/if_iwivar.h> 77 78 #ifdef IWI_DEBUG 79 #define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0) 80 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0) 81 int iwi_debug = 0; 82 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level"); 83 #else 84 #define DPRINTF(x) 85 #define DPRINTFN(n, x) 86 #endif 87 88 MODULE_DEPEND(iwi, pci, 1, 1, 1); 89 MODULE_DEPEND(iwi, wlan, 1, 1, 1); 90 91 struct iwi_ident { 92 uint16_t vendor; 93 uint16_t device; 94 const char *name; 95 }; 96 97 static const struct iwi_ident iwi_ident_table[] = { 98 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" }, 99 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" }, 100 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" }, 101 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" }, 102 103 { 0, 0, NULL } 104 }; 105 106 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int); 107 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *, 108 int); 109 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 110 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 111 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *, 112 int); 113 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 114 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 115 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *, 116 int); 117 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 118 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 119 static int iwi_key_alloc(struct ieee80211com *, 120 const struct ieee80211_key *); 121 static int iwi_media_change(struct ifnet *); 122 static void iwi_media_status(struct ifnet *, struct ifmediareq *); 123 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int); 124 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t); 125 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *); 126 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int, 127 struct iwi_frame *); 128 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *); 129 static void iwi_rx_intr(struct iwi_softc *); 130 static void iwi_tx_intr(struct iwi_softc *); 131 static void iwi_intr(void *); 132 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int); 133 static int iwi_tx_start(struct ifnet *, struct mbuf *, 134 struct ieee80211_node *); 135 static void iwi_start(struct ifnet *); 136 static void iwi_watchdog(struct ifnet *); 137 static int iwi_ioctl(struct ifnet *, u_long, caddr_t); 138 static void iwi_stop_master(struct iwi_softc *); 139 static int iwi_reset(struct iwi_softc *); 140 static int iwi_load_ucode(struct iwi_softc *, void *, int); 141 static int iwi_load_firmware(struct iwi_softc *, void *, int); 142 static int iwi_cache_firmware(struct iwi_softc *, void *); 143 static void iwi_free_firmware(struct iwi_softc *); 144 static int iwi_config(struct iwi_softc *); 145 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *); 146 static int iwi_scan(struct iwi_softc *); 147 static int iwi_auth_and_assoc(struct iwi_softc *); 148 static void iwi_init(void *); 149 static void iwi_stop(void *); 150 #ifdef IWI_DEBUG 151 static int iwi_sysctl_stats(SYSCTL_HANDLER_ARGS); 152 #endif 153 static int iwi_sysctl_radio(SYSCTL_HANDLER_ARGS); 154 155 static int iwi_probe(device_t); 156 static int iwi_attach(device_t); 157 static int iwi_detach(device_t); 158 static int iwi_shutdown(device_t); 159 static int iwi_suspend(device_t); 160 static int iwi_resume(device_t); 161 162 static device_method_t iwi_methods[] = { 163 /* Device interface */ 164 DEVMETHOD(device_probe, iwi_probe), 165 DEVMETHOD(device_attach, iwi_attach), 166 DEVMETHOD(device_detach, iwi_detach), 167 DEVMETHOD(device_shutdown, iwi_shutdown), 168 DEVMETHOD(device_suspend, iwi_suspend), 169 DEVMETHOD(device_resume, iwi_resume), 170 171 { 0, 0 } 172 }; 173 174 static driver_t iwi_driver = { 175 "iwi", 176 iwi_methods, 177 sizeof (struct iwi_softc) 178 }; 179 180 static devclass_t iwi_devclass; 181 182 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0); 183 184 /* 185 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 186 */ 187 static const struct ieee80211_rateset iwi_rateset_11a = 188 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 189 190 static const struct ieee80211_rateset iwi_rateset_11b = 191 { 4, { 2, 4, 11, 22 } }; 192 193 static const struct ieee80211_rateset iwi_rateset_11g = 194 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 195 196 static __inline uint8_t 197 MEM_READ_1(struct iwi_softc *sc, uint32_t addr) 198 { 199 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 200 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA); 201 } 202 203 static __inline uint32_t 204 MEM_READ_4(struct iwi_softc *sc, uint32_t addr) 205 { 206 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 207 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA); 208 } 209 210 static int 211 iwi_probe(device_t dev) 212 { 213 const struct iwi_ident *ident; 214 215 for (ident = iwi_ident_table; ident->name != NULL; ident++) { 216 if (pci_get_vendor(dev) == ident->vendor && 217 pci_get_device(dev) == ident->device) { 218 device_set_desc(dev, ident->name); 219 return 0; 220 } 221 } 222 return ENXIO; 223 } 224 225 /* Base Address Register */ 226 #define IWI_PCI_BAR0 0x10 227 228 static int 229 iwi_attach(device_t dev) 230 { 231 struct iwi_softc *sc = device_get_softc(dev); 232 struct ifnet *ifp; 233 struct ieee80211com *ic = &sc->sc_ic; 234 uint16_t val; 235 int error, i; 236 237 sc->sc_dev = dev; 238 239 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 240 MTX_DEF | MTX_RECURSE); 241 242 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 243 device_printf(dev, "chip is in D%d power mode " 244 "-- setting to D0\n", pci_get_powerstate(dev)); 245 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 246 } 247 248 pci_write_config(dev, 0x41, 0, 1); 249 250 /* enable bus-mastering */ 251 pci_enable_busmaster(dev); 252 253 sc->mem_rid = IWI_PCI_BAR0; 254 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 255 RF_ACTIVE); 256 if (sc->mem == NULL) { 257 device_printf(dev, "could not allocate memory resource\n"); 258 goto fail; 259 } 260 261 sc->sc_st = rman_get_bustag(sc->mem); 262 sc->sc_sh = rman_get_bushandle(sc->mem); 263 264 sc->irq_rid = 0; 265 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 266 RF_ACTIVE | RF_SHAREABLE); 267 if (sc->irq == NULL) { 268 device_printf(dev, "could not allocate interrupt resource\n"); 269 goto fail; 270 } 271 272 if (iwi_reset(sc) != 0) { 273 device_printf(dev, "could not reset adapter\n"); 274 goto fail; 275 } 276 277 /* 278 * Allocate rings. 279 */ 280 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) { 281 device_printf(dev, "could not allocate Cmd ring\n"); 282 goto fail; 283 } 284 285 if (iwi_alloc_tx_ring(sc, &sc->txq, IWI_TX_RING_COUNT) != 0) { 286 device_printf(dev, "could not allocate Tx ring\n"); 287 goto fail; 288 } 289 290 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) { 291 device_printf(dev, "could not allocate Rx ring\n"); 292 goto fail; 293 } 294 295 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 296 if (ifp == NULL) { 297 device_printf(dev, "can not if_alloc()\n"); 298 goto fail; 299 return (ENOSPC); 300 } 301 ifp->if_softc = sc; 302 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 303 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 304 ifp->if_init = iwi_init; 305 ifp->if_ioctl = iwi_ioctl; 306 ifp->if_start = iwi_start; 307 ifp->if_watchdog = iwi_watchdog; 308 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 309 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 310 IFQ_SET_READY(&ifp->if_snd); 311 312 ic->ic_ifp = ifp; 313 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 314 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 315 ic->ic_state = IEEE80211_S_INIT; 316 317 /* set device capabilities */ 318 ic->ic_caps = IEEE80211_C_WPA | IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | 319 IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR; 320 321 /* read MAC address from EEPROM */ 322 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0); 323 ic->ic_myaddr[0] = val >> 8; 324 ic->ic_myaddr[1] = val & 0xff; 325 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1); 326 ic->ic_myaddr[2] = val >> 8; 327 ic->ic_myaddr[3] = val & 0xff; 328 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2); 329 ic->ic_myaddr[4] = val >> 8; 330 ic->ic_myaddr[5] = val & 0xff; 331 332 #if 0 333 if (pci_get_device(dev) >= 0x4223) { 334 /* set supported .11a rates (2915ABG only) */ 335 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a; 336 337 /* set supported .11a channels */ 338 for (i = 36; i <= 64; i += 4) { 339 ic->ic_channels[i].ic_freq = 340 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 341 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 342 } 343 for (i = 149; i <= 165; i += 4) { 344 ic->ic_channels[i].ic_freq = 345 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 346 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 347 } 348 } 349 #endif 350 351 /* set supported .11b and .11g rates */ 352 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b; 353 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g; 354 355 /* set supported .11b and .11g channels (1 through 14) */ 356 for (i = 1; i <= 14; i++) { 357 ic->ic_channels[i].ic_freq = 358 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 359 ic->ic_channels[i].ic_flags = 360 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 361 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 362 } 363 364 ieee80211_ifattach(ic); 365 /* override state transition machine */ 366 sc->sc_newstate = ic->ic_newstate; 367 ic->ic_newstate = iwi_newstate; 368 ic->ic_crypto.cs_key_alloc = iwi_key_alloc; 369 ieee80211_media_init(ic, iwi_media_change, iwi_media_status); 370 371 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 372 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf); 373 374 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 375 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 376 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT); 377 378 sc->sc_txtap_len = sizeof sc->sc_txtapu; 379 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 380 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT); 381 382 /* 383 * Add a few sysctl knobs. 384 */ 385 sc->dwelltime = 100; 386 sc->bluetooth = 1; 387 sc->antenna = 0; 388 389 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 390 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio", 391 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I", 392 "radio transmitter switch state (0=off, 1=on)"); 393 394 #ifdef IWI_DEBUG 395 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 396 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats", 397 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S", 398 "statistics"); 399 #endif 400 401 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 402 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell", 403 CTLFLAG_RW, &sc->dwelltime, 0, 404 "channel dwell time (ms) for AP/station scanning"); 405 406 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 407 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "bluetooth", 408 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence"); 409 410 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 411 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "antenna", 412 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)"); 413 414 /* 415 * Hook our interrupt after all initialization is complete. 416 */ 417 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 418 iwi_intr, sc, &sc->sc_ih); 419 if (error != 0) { 420 device_printf(dev, "could not set up interrupt\n"); 421 goto fail; 422 } 423 424 if (bootverbose) 425 ieee80211_announce(ic); 426 427 return 0; 428 429 fail: iwi_detach(dev); 430 return ENXIO; 431 } 432 433 static int 434 iwi_detach(device_t dev) 435 { 436 struct iwi_softc *sc = device_get_softc(dev); 437 struct ieee80211com *ic = &sc->sc_ic; 438 struct ifnet *ifp = ic->ic_ifp; 439 440 iwi_stop(sc); 441 442 iwi_free_firmware(sc); 443 444 if (ifp != NULL) 445 bpfdetach(ifp); 446 ieee80211_ifdetach(ic); 447 if (ifp != NULL) 448 if_free(ifp); 449 450 iwi_free_cmd_ring(sc, &sc->cmdq); 451 iwi_free_tx_ring(sc, &sc->txq); 452 iwi_free_rx_ring(sc, &sc->rxq); 453 454 if (sc->irq != NULL) { 455 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 456 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 457 } 458 459 if (sc->mem != NULL) 460 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 461 462 mtx_destroy(&sc->sc_mtx); 463 464 return 0; 465 } 466 467 static void 468 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 469 { 470 if (error != 0) 471 return; 472 473 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 474 475 *(bus_addr_t *)arg = segs[0].ds_addr; 476 } 477 478 static int 479 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count) 480 { 481 int error; 482 483 ring->count = count; 484 ring->queued = 0; 485 ring->cur = ring->next = 0; 486 487 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 488 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_CMD_DESC_SIZE, 1, 489 count * IWI_CMD_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat); 490 if (error != 0) { 491 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 492 goto fail; 493 } 494 495 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 496 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 497 if (error != 0) { 498 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 499 goto fail; 500 } 501 502 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 503 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 504 if (error != 0) { 505 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 506 goto fail; 507 } 508 509 return 0; 510 511 fail: iwi_free_cmd_ring(sc, ring); 512 return error; 513 } 514 515 static void 516 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 517 { 518 ring->queued = 0; 519 ring->cur = ring->next = 0; 520 } 521 522 static void 523 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 524 { 525 if (ring->desc != NULL) { 526 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 527 BUS_DMASYNC_POSTWRITE); 528 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 529 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 530 } 531 532 if (ring->desc_dmat != NULL) 533 bus_dma_tag_destroy(ring->desc_dmat); 534 } 535 536 static int 537 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count) 538 { 539 int i, error; 540 541 ring->count = count; 542 ring->queued = 0; 543 ring->cur = ring->next = 0; 544 545 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 546 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_TX_DESC_SIZE, 1, 547 count * IWI_TX_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat); 548 if (error != 0) { 549 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 550 goto fail; 551 } 552 553 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 554 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 555 if (error != 0) { 556 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 557 goto fail; 558 } 559 560 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 561 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 562 if (error != 0) { 563 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 564 goto fail; 565 } 566 567 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF, 568 M_NOWAIT | M_ZERO); 569 if (ring->data == NULL) { 570 device_printf(sc->sc_dev, "could not allocate soft data\n"); 571 error = ENOMEM; 572 goto fail; 573 } 574 575 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 576 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 577 NULL, &ring->data_dmat); 578 if (error != 0) { 579 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 580 goto fail; 581 } 582 583 for (i = 0; i < count; i++) { 584 error = bus_dmamap_create(ring->data_dmat, 0, 585 &ring->data[i].map); 586 if (error != 0) { 587 device_printf(sc->sc_dev, "could not create DMA map\n"); 588 goto fail; 589 } 590 } 591 592 return 0; 593 594 fail: iwi_free_tx_ring(sc, ring); 595 return error; 596 } 597 598 static void 599 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 600 { 601 struct iwi_tx_data *data; 602 int i; 603 604 for (i = 0; i < ring->count; i++) { 605 data = &ring->data[i]; 606 607 if (data->m != NULL) { 608 bus_dmamap_sync(ring->data_dmat, data->map, 609 BUS_DMASYNC_POSTWRITE); 610 bus_dmamap_unload(ring->data_dmat, data->map); 611 m_freem(data->m); 612 data->m = NULL; 613 } 614 615 if (data->ni != NULL) { 616 ieee80211_free_node(data->ni); 617 data->ni = NULL; 618 } 619 } 620 621 ring->queued = 0; 622 ring->cur = ring->next = 0; 623 } 624 625 static void 626 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 627 { 628 struct iwi_tx_data *data; 629 int i; 630 631 if (ring->desc != NULL) { 632 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 633 BUS_DMASYNC_POSTWRITE); 634 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 635 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 636 } 637 638 if (ring->desc_dmat != NULL) 639 bus_dma_tag_destroy(ring->desc_dmat); 640 641 if (ring->data != NULL) { 642 for (i = 0; i < ring->count; i++) { 643 data = &ring->data[i]; 644 645 if (data->m != NULL) { 646 bus_dmamap_sync(ring->data_dmat, data->map, 647 BUS_DMASYNC_POSTWRITE); 648 bus_dmamap_unload(ring->data_dmat, data->map); 649 m_freem(data->m); 650 } 651 652 if (data->ni != NULL) 653 ieee80211_free_node(data->ni); 654 655 if (data->map != NULL) 656 bus_dmamap_destroy(ring->data_dmat, data->map); 657 } 658 659 free(ring->data, M_DEVBUF); 660 } 661 662 if (ring->data_dmat != NULL) 663 bus_dma_tag_destroy(ring->data_dmat); 664 } 665 666 static int 667 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count) 668 { 669 struct iwi_rx_data *data; 670 int i, error; 671 672 ring->count = count; 673 ring->cur = 0; 674 675 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF, 676 M_NOWAIT | M_ZERO); 677 if (ring->data == NULL) { 678 device_printf(sc->sc_dev, "could not allocate soft data\n"); 679 error = ENOMEM; 680 goto fail; 681 } 682 683 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 684 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 685 NULL, &ring->data_dmat); 686 if (error != 0) { 687 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 688 goto fail; 689 } 690 691 for (i = 0; i < count; i++) { 692 data = &ring->data[i]; 693 694 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 695 if (error != 0) { 696 device_printf(sc->sc_dev, "could not create DMA map\n"); 697 goto fail; 698 } 699 700 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 701 if (data->m == NULL) { 702 device_printf(sc->sc_dev, 703 "could not allocate rx mbuf\n"); 704 error = ENOMEM; 705 goto fail; 706 } 707 708 error = bus_dmamap_load(ring->data_dmat, data->map, 709 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, 710 &data->physaddr, 0); 711 if (error != 0) { 712 device_printf(sc->sc_dev, 713 "could not load rx buf DMA map"); 714 goto fail; 715 } 716 717 data->reg = IWI_CSR_RX_BASE + i * 4; 718 } 719 720 return 0; 721 722 fail: iwi_free_rx_ring(sc, ring); 723 return error; 724 } 725 726 static void 727 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 728 { 729 ring->cur = 0; 730 } 731 732 static void 733 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 734 { 735 struct iwi_rx_data *data; 736 int i; 737 738 if (ring->data != NULL) { 739 for (i = 0; i < ring->count; i++) { 740 data = &ring->data[i]; 741 742 if (data->m != NULL) { 743 bus_dmamap_sync(ring->data_dmat, data->map, 744 BUS_DMASYNC_POSTREAD); 745 bus_dmamap_unload(ring->data_dmat, data->map); 746 m_freem(data->m); 747 } 748 749 if (data->map != NULL) 750 bus_dmamap_destroy(ring->data_dmat, data->map); 751 } 752 753 free(ring->data, M_DEVBUF); 754 } 755 756 if (ring->data_dmat != NULL) 757 bus_dma_tag_destroy(ring->data_dmat); 758 } 759 760 static int 761 iwi_shutdown(device_t dev) 762 { 763 struct iwi_softc *sc = device_get_softc(dev); 764 765 iwi_stop(sc); 766 767 return 0; 768 } 769 770 static int 771 iwi_suspend(device_t dev) 772 { 773 struct iwi_softc *sc = device_get_softc(dev); 774 775 iwi_stop(sc); 776 777 return 0; 778 } 779 780 static int 781 iwi_resume(device_t dev) 782 { 783 struct iwi_softc *sc = device_get_softc(dev); 784 struct ifnet *ifp = sc->sc_ic.ic_ifp; 785 786 IWI_LOCK(sc); 787 788 pci_write_config(dev, 0x41, 0, 1); 789 790 if (ifp->if_flags & IFF_UP) { 791 ifp->if_init(ifp->if_softc); 792 if (ifp->if_flags & IFF_RUNNING) 793 ifp->if_start(ifp); 794 } 795 796 IWI_UNLOCK(sc); 797 798 return 0; 799 } 800 801 static int 802 iwi_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k) 803 { 804 if (k >= ic->ic_nw_keys && k < &ic->ic_nw_keys[IEEE80211_WEP_NKID]) 805 return k - ic->ic_nw_keys; 806 807 return IEEE80211_KEYIX_NONE; 808 } 809 810 static int 811 iwi_media_change(struct ifnet *ifp) 812 { 813 struct iwi_softc *sc = ifp->if_softc; 814 int error; 815 816 IWI_LOCK(sc); 817 818 error = ieee80211_media_change(ifp); 819 if (error != ENETRESET) { 820 IWI_UNLOCK(sc); 821 return error; 822 } 823 824 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 825 iwi_init(sc); 826 827 IWI_UNLOCK(sc); 828 829 return 0; 830 } 831 832 /* 833 * The firmware automaticly adapt the transmit speed. We report the current 834 * transmit speed here. 835 */ 836 static void 837 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 838 { 839 struct iwi_softc *sc = ifp->if_softc; 840 struct ieee80211com *ic = &sc->sc_ic; 841 #define N(a) (sizeof (a) / sizeof (a[0])) 842 static const struct { 843 uint32_t val; 844 int rate; 845 } rates[] = { 846 { IWI_RATE_DS1, 2 }, 847 { IWI_RATE_DS2, 4 }, 848 { IWI_RATE_DS5, 11 }, 849 { IWI_RATE_DS11, 22 }, 850 { IWI_RATE_OFDM6, 12 }, 851 { IWI_RATE_OFDM9, 18 }, 852 { IWI_RATE_OFDM12, 24 }, 853 { IWI_RATE_OFDM18, 36 }, 854 { IWI_RATE_OFDM24, 48 }, 855 { IWI_RATE_OFDM36, 72 }, 856 { IWI_RATE_OFDM48, 96 }, 857 { IWI_RATE_OFDM54, 108 }, 858 }; 859 uint32_t val; 860 int rate, i; 861 862 imr->ifm_status = IFM_AVALID; 863 imr->ifm_active = IFM_IEEE80211; 864 if (ic->ic_state == IEEE80211_S_RUN) 865 imr->ifm_status |= IFM_ACTIVE; 866 867 /* read current transmission rate from adapter */ 868 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE); 869 870 /* convert rate to 802.11 rate */ 871 for (i = 0; i < N(rates) && rates[i].val != val; i++); 872 rate = (i < N(rates)) ? rates[i].rate : 0; 873 874 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode); 875 switch (ic->ic_opmode) { 876 case IEEE80211_M_STA: 877 break; 878 879 case IEEE80211_M_IBSS: 880 imr->ifm_active |= IFM_IEEE80211_ADHOC; 881 break; 882 883 case IEEE80211_M_MONITOR: 884 imr->ifm_active |= IFM_IEEE80211_MONITOR; 885 break; 886 887 case IEEE80211_M_AHDEMO: 888 case IEEE80211_M_HOSTAP: 889 /* should not get there */ 890 break; 891 } 892 #undef N 893 } 894 895 static int 896 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 897 { 898 struct ifnet *ifp = ic->ic_ifp; 899 struct iwi_softc *sc = ifp->if_softc; 900 901 switch (nstate) { 902 case IEEE80211_S_SCAN: 903 if (sc->flags & IWI_FLAG_SCANNING) 904 break; 905 906 ieee80211_node_table_reset(&ic->ic_scan); 907 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 908 sc->flags |= IWI_FLAG_SCANNING; 909 iwi_scan(sc); 910 break; 911 912 case IEEE80211_S_AUTH: 913 iwi_auth_and_assoc(sc); 914 break; 915 916 case IEEE80211_S_RUN: 917 if (ic->ic_opmode == IEEE80211_M_IBSS) 918 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 919 else if (ic->ic_opmode == IEEE80211_M_MONITOR) 920 iwi_set_chan(sc, ic->ic_ibss_chan); 921 922 return sc->sc_newstate(ic, nstate, 923 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 924 925 case IEEE80211_S_ASSOC: 926 break; 927 928 case IEEE80211_S_INIT: 929 sc->flags &= ~IWI_FLAG_SCANNING; 930 break; 931 } 932 933 ic->ic_state = nstate; 934 return 0; 935 } 936 937 /* 938 * Read 16 bits at address 'addr' from the serial EEPROM. 939 */ 940 static uint16_t 941 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr) 942 { 943 uint32_t tmp; 944 uint16_t val; 945 int n; 946 947 /* clock C once before the first command */ 948 IWI_EEPROM_CTL(sc, 0); 949 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 950 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 951 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 952 953 /* write start bit (1) */ 954 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 955 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 956 957 /* write READ opcode (10) */ 958 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 959 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 960 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 961 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 962 963 /* write address A7-A0 */ 964 for (n = 7; n >= 0; n--) { 965 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 966 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D)); 967 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 968 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C); 969 } 970 971 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 972 973 /* read data Q15-Q0 */ 974 val = 0; 975 for (n = 15; n >= 0; n--) { 976 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 977 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 978 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL); 979 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n; 980 } 981 982 IWI_EEPROM_CTL(sc, 0); 983 984 /* clear Chip Select and clock C */ 985 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 986 IWI_EEPROM_CTL(sc, 0); 987 IWI_EEPROM_CTL(sc, IWI_EEPROM_C); 988 989 return be16toh(val); 990 } 991 992 /* 993 * XXX: Hack to set the current channel to the value advertised in beacons or 994 * probe responses. Only used during AP detection. 995 */ 996 static void 997 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m) 998 { 999 struct ieee80211_frame *wh; 1000 uint8_t subtype; 1001 uint8_t *frm, *efrm; 1002 1003 wh = mtod(m, struct ieee80211_frame *); 1004 1005 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 1006 return; 1007 1008 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1009 1010 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 1011 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1012 return; 1013 1014 frm = (uint8_t *)(wh + 1); 1015 efrm = mtod(m, uint8_t *) + m->m_len; 1016 1017 frm += 12; /* skip tstamp, bintval and capinfo fields */ 1018 while (frm < efrm) { 1019 if (*frm == IEEE80211_ELEMID_DSPARMS) 1020 #if IEEE80211_CHAN_MAX < 255 1021 if (frm[2] <= IEEE80211_CHAN_MAX) 1022 #endif 1023 ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]]; 1024 1025 frm += frm[1] + 2; 1026 } 1027 } 1028 1029 static void 1030 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i, 1031 struct iwi_frame *frame) 1032 { 1033 struct ieee80211com *ic = &sc->sc_ic; 1034 struct ifnet *ifp = ic->ic_ifp; 1035 struct mbuf *m; 1036 struct ieee80211_frame *wh; 1037 struct ieee80211_node *ni; 1038 int error; 1039 1040 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n", 1041 le16toh(frame->len), frame->chan, frame->rssi_dbm)); 1042 1043 if (le16toh(frame->len) < sizeof (struct ieee80211_frame)) 1044 return; 1045 1046 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1047 1048 /* finalize mbuf */ 1049 m = data->m; 1050 m->m_pkthdr.rcvif = ifp; 1051 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) + 1052 sizeof (struct iwi_frame) + le16toh(frame->len); 1053 1054 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame)); 1055 1056 if (ic->ic_state == IEEE80211_S_SCAN) 1057 iwi_fix_channel(ic, m); 1058 1059 if (sc->sc_drvbpf != NULL) { 1060 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap; 1061 1062 tap->wr_flags = 0; 1063 tap->wr_rate = frame->rate; 1064 tap->wr_chan_freq = 1065 htole16(ic->ic_channels[frame->chan].ic_freq); 1066 tap->wr_chan_flags = 1067 htole16(ic->ic_channels[frame->chan].ic_flags); 1068 tap->wr_antsignal = frame->signal; 1069 tap->wr_antenna = frame->antenna; 1070 1071 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1072 } 1073 1074 wh = mtod(m, struct ieee80211_frame *); 1075 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1076 1077 /* send the frame to the 802.11 layer */ 1078 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0); 1079 1080 /* node is no longer needed */ 1081 ieee80211_free_node(ni); 1082 1083 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1084 if (data->m == NULL) { 1085 device_printf(sc->sc_dev, "could not allocate rx mbuf\n"); 1086 return; 1087 } 1088 1089 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1090 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr, 1091 0); 1092 if (error != 0) { 1093 device_printf(sc->sc_dev, "could not load rx buf DMA map\n"); 1094 m_freem(data->m); 1095 data->m = NULL; 1096 return; 1097 } 1098 1099 CSR_WRITE_4(sc, data->reg, data->physaddr); 1100 } 1101 1102 static void 1103 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif) 1104 { 1105 struct ieee80211com *ic = &sc->sc_ic; 1106 struct iwi_notif_scan_channel *chan; 1107 struct iwi_notif_scan_complete *scan; 1108 struct iwi_notif_authentication *auth; 1109 struct iwi_notif_association *assoc; 1110 1111 switch (notif->type) { 1112 case IWI_NOTIF_TYPE_SCAN_CHANNEL: 1113 chan = (struct iwi_notif_scan_channel *)(notif + 1); 1114 1115 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan)); 1116 break; 1117 1118 case IWI_NOTIF_TYPE_SCAN_COMPLETE: 1119 scan = (struct iwi_notif_scan_complete *)(notif + 1); 1120 1121 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan, 1122 scan->status)); 1123 1124 /* monitor mode uses scan to set the channel ... */ 1125 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1126 sc->flags &= ~IWI_FLAG_SCANNING; 1127 ieee80211_end_scan(ic); 1128 } else 1129 iwi_set_chan(sc, ic->ic_ibss_chan); 1130 break; 1131 1132 case IWI_NOTIF_TYPE_AUTHENTICATION: 1133 auth = (struct iwi_notif_authentication *)(notif + 1); 1134 1135 DPRINTFN(2, ("Authentication (%u)\n", auth->state)); 1136 1137 switch (auth->state) { 1138 case IWI_AUTHENTICATED: 1139 ieee80211_node_authorize(ic, ic->ic_bss); 1140 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 1141 break; 1142 1143 case IWI_DEAUTHENTICATED: 1144 break; 1145 1146 default: 1147 device_printf(sc->sc_dev, 1148 "unknown authentication state %u\n", auth->state); 1149 } 1150 break; 1151 1152 case IWI_NOTIF_TYPE_ASSOCIATION: 1153 assoc = (struct iwi_notif_association *)(notif + 1); 1154 1155 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state, 1156 assoc->status)); 1157 1158 switch (assoc->state) { 1159 case IWI_AUTHENTICATED: 1160 /* re-association, do nothing */ 1161 break; 1162 1163 case IWI_ASSOCIATED: 1164 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1165 break; 1166 1167 case IWI_DEASSOCIATED: 1168 ieee80211_begin_scan(ic, 1); 1169 break; 1170 1171 default: 1172 device_printf(sc->sc_dev, 1173 "unknown association state %u\n", assoc->state); 1174 } 1175 break; 1176 1177 case IWI_NOTIF_TYPE_CALIBRATION: 1178 case IWI_NOTIF_TYPE_BEACON: 1179 case IWI_NOTIF_TYPE_NOISE: 1180 DPRINTFN(5, ("Notification (%u)\n", notif->type)); 1181 break; 1182 1183 default: 1184 device_printf(sc->sc_dev, "unknown notification type %u\n", 1185 notif->type); 1186 } 1187 } 1188 1189 static void 1190 iwi_rx_intr(struct iwi_softc *sc) 1191 { 1192 struct iwi_rx_data *data; 1193 struct iwi_hdr *hdr; 1194 uint32_t hw; 1195 1196 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX); 1197 1198 for (; sc->rxq.cur != hw;) { 1199 data = &sc->rxq.data[sc->rxq.cur]; 1200 1201 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1202 BUS_DMASYNC_POSTREAD); 1203 1204 hdr = mtod(data->m, struct iwi_hdr *); 1205 1206 switch (hdr->type) { 1207 case IWI_HDR_TYPE_FRAME: 1208 iwi_frame_intr(sc, data, sc->rxq.cur, 1209 (struct iwi_frame *)(hdr + 1)); 1210 break; 1211 1212 case IWI_HDR_TYPE_NOTIF: 1213 iwi_notification_intr(sc, 1214 (struct iwi_notif *)(hdr + 1)); 1215 break; 1216 1217 default: 1218 device_printf(sc->sc_dev, "unknown hdr type %u\n", 1219 hdr->type); 1220 } 1221 1222 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1223 1224 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT; 1225 } 1226 1227 /* tell the firmware what we have processed */ 1228 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1; 1229 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw); 1230 } 1231 1232 static void 1233 iwi_tx_intr(struct iwi_softc *sc) 1234 { 1235 struct ieee80211com *ic = &sc->sc_ic; 1236 struct ifnet *ifp = ic->ic_ifp; 1237 struct iwi_tx_data *data; 1238 uint32_t hw; 1239 1240 hw = CSR_READ_4(sc, IWI_CSR_TX1_RIDX); 1241 1242 for (; sc->txq.next != hw;) { 1243 data = &sc->txq.data[sc->txq.next]; 1244 1245 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1246 BUS_DMASYNC_POSTWRITE); 1247 bus_dmamap_unload(sc->txq.data_dmat, data->map); 1248 m_freem(data->m); 1249 data->m = NULL; 1250 ieee80211_free_node(data->ni); 1251 data->ni = NULL; 1252 1253 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next)); 1254 1255 ifp->if_opackets++; 1256 1257 sc->txq.queued--; 1258 sc->txq.next = (sc->txq.next + 1) % IWI_TX_RING_COUNT; 1259 } 1260 1261 sc->sc_tx_timer = 0; 1262 ifp->if_flags &= ~IFF_OACTIVE; 1263 iwi_start(ifp); 1264 } 1265 1266 static void 1267 iwi_intr(void *arg) 1268 { 1269 struct iwi_softc *sc = arg; 1270 uint32_t r; 1271 1272 IWI_LOCK(sc); 1273 1274 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) { 1275 IWI_UNLOCK(sc); 1276 return; 1277 } 1278 1279 /* disable interrupts */ 1280 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1281 1282 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) { 1283 device_printf(sc->sc_dev, "fatal error\n"); 1284 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1285 iwi_stop(sc); 1286 } 1287 1288 if (r & IWI_INTR_FW_INITED) { 1289 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR))) 1290 wakeup(sc); 1291 } 1292 1293 if (r & IWI_INTR_RADIO_OFF) { 1294 DPRINTF(("radio transmitter turned off\n")); 1295 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1296 iwi_stop(sc); 1297 } 1298 1299 if (r & IWI_INTR_RX_DONE) 1300 iwi_rx_intr(sc); 1301 1302 if (r & IWI_INTR_CMD_DONE) 1303 wakeup(sc); 1304 1305 if (r & IWI_INTR_TX1_DONE) 1306 iwi_tx_intr(sc); 1307 1308 /* acknowledge interrupts */ 1309 CSR_WRITE_4(sc, IWI_CSR_INTR, r); 1310 1311 /* re-enable interrupts */ 1312 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 1313 1314 IWI_UNLOCK(sc); 1315 } 1316 1317 static int 1318 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async) 1319 { 1320 struct iwi_cmd_desc *desc; 1321 1322 desc = &sc->cmdq.desc[sc->cmdq.cur]; 1323 1324 desc->hdr.type = IWI_HDR_TYPE_COMMAND; 1325 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1326 desc->type = type; 1327 desc->len = len; 1328 memcpy(desc->data, data, len); 1329 1330 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map, 1331 BUS_DMASYNC_PREWRITE); 1332 1333 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur, 1334 type, len)); 1335 1336 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT; 1337 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 1338 1339 return async ? 0 : msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz); 1340 } 1341 1342 static int 1343 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni) 1344 { 1345 struct iwi_softc *sc = ifp->if_softc; 1346 struct ieee80211com *ic = &sc->sc_ic; 1347 struct ieee80211_frame *wh; 1348 struct ieee80211_key *k; 1349 struct iwi_tx_data *data; 1350 struct iwi_tx_desc *desc; 1351 struct mbuf *mnew; 1352 bus_dma_segment_t segs[IWI_MAX_NSEG]; 1353 int nsegs, error, i; 1354 1355 wh = mtod(m0, struct ieee80211_frame *); 1356 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1357 k = ieee80211_crypto_encap(ic, ni, m0); 1358 if (k == NULL) 1359 return ENOBUFS; 1360 1361 /* packet header may have moved, reset our local pointer */ 1362 wh = mtod(m0, struct ieee80211_frame *); 1363 } 1364 1365 if (sc->sc_drvbpf != NULL) { 1366 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap; 1367 1368 tap->wt_flags = 0; 1369 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1370 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1371 1372 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1373 } 1374 1375 data = &sc->txq.data[sc->txq.cur]; 1376 desc = &sc->txq.desc[sc->txq.cur]; 1377 1378 /* trim IEEE802.11 header */ 1379 m_adj(m0, sizeof (struct ieee80211_frame)); 1380 1381 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, segs, 1382 &nsegs, 0); 1383 if (error != 0 && error != EFBIG) { 1384 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1385 error); 1386 m_freem(m0); 1387 return error; 1388 } 1389 if (error != 0) { 1390 mnew = m_defrag(m0, M_DONTWAIT); 1391 if (mnew == NULL) { 1392 device_printf(sc->sc_dev, 1393 "could not defragment mbuf\n"); 1394 m_freem(m0); 1395 return ENOBUFS; 1396 } 1397 m0 = mnew; 1398 1399 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1400 m0, segs, &nsegs, 0); 1401 if (error != 0) { 1402 device_printf(sc->sc_dev, 1403 "could not map mbuf (error %d)\n", error); 1404 m_freem(m0); 1405 return error; 1406 } 1407 } 1408 1409 data->m = m0; 1410 data->ni = ni; 1411 1412 desc->hdr.type = IWI_HDR_TYPE_DATA; 1413 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1414 desc->cmd = IWI_DATA_CMD_TX; 1415 desc->len = htole16(m0->m_pkthdr.len); 1416 memcpy(&desc->wh, wh, sizeof (struct ieee80211_frame)); 1417 desc->flags = 0; 1418 1419 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) 1420 desc->flags |= IWI_DATA_FLAG_NEED_ACK; 1421 1422 #if 0 1423 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 1424 wh->i_fc[1] |= IEEE80211_FC1_WEP; 1425 desc->wep_txkey = ic->ic_crypto.cs_def_txkey; 1426 } else 1427 #endif 1428 desc->flags |= IWI_DATA_FLAG_NO_WEP; 1429 1430 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1431 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE; 1432 1433 desc->nseg = htole32(nsegs); 1434 for (i = 0; i < nsegs; i++) { 1435 desc->seg_addr[i] = htole32(segs[i].ds_addr); 1436 desc->seg_len[i] = htole32(segs[i].ds_len); 1437 } 1438 1439 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1440 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1441 BUS_DMASYNC_PREWRITE); 1442 1443 DPRINTFN(5, ("sending data frame idx=%u len=%u nseg=%u\n", sc->txq.cur, 1444 desc->len, desc->nseg)); 1445 1446 sc->txq.queued++; 1447 sc->txq.cur = (sc->txq.cur + 1) % IWI_TX_RING_COUNT; 1448 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur); 1449 1450 return 0; 1451 } 1452 1453 static void 1454 iwi_start(struct ifnet *ifp) 1455 { 1456 struct iwi_softc *sc = ifp->if_softc; 1457 struct ieee80211com *ic = &sc->sc_ic; 1458 struct mbuf *m0; 1459 struct ether_header *eh; 1460 struct ieee80211_node *ni; 1461 1462 IWI_LOCK(sc); 1463 1464 if (ic->ic_state != IEEE80211_S_RUN) { 1465 IWI_UNLOCK(sc); 1466 return; 1467 } 1468 1469 for (;;) { 1470 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 1471 if (m0 == NULL) 1472 break; 1473 1474 if (sc->txq.queued >= IWI_TX_RING_COUNT - 4) { 1475 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 1476 ifp->if_flags |= IFF_OACTIVE; 1477 break; 1478 } 1479 1480 if (m0->m_len < sizeof (struct ether_header) && 1481 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) 1482 continue; 1483 1484 eh = mtod(m0, struct ether_header *); 1485 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1486 if (ni == NULL) { 1487 m_freem(m0); 1488 continue; 1489 } 1490 BPF_MTAP(ifp, m0); 1491 1492 m0 = ieee80211_encap(ic, m0, ni); 1493 if (m0 == NULL) 1494 continue; 1495 1496 if (ic->ic_rawbpf != NULL) 1497 bpf_mtap(ic->ic_rawbpf, m0); 1498 1499 if (iwi_tx_start(ifp, m0, ni) != 0) { 1500 ieee80211_free_node(ni); 1501 ifp->if_oerrors++; 1502 break; 1503 } 1504 1505 sc->sc_tx_timer = 5; 1506 ifp->if_timer = 1; 1507 } 1508 1509 IWI_UNLOCK(sc); 1510 } 1511 1512 static void 1513 iwi_watchdog(struct ifnet *ifp) 1514 { 1515 struct iwi_softc *sc = ifp->if_softc; 1516 struct ieee80211com *ic = &sc->sc_ic; 1517 1518 IWI_LOCK(sc); 1519 1520 ifp->if_timer = 0; 1521 1522 if (sc->sc_tx_timer > 0) { 1523 if (--sc->sc_tx_timer == 0) { 1524 if_printf(ifp, "device timeout\n"); 1525 ifp->if_oerrors++; 1526 ifp->if_flags &= ~IFF_UP; 1527 iwi_stop(sc); 1528 IWI_UNLOCK(sc); 1529 return; 1530 } 1531 ifp->if_timer = 1; 1532 } 1533 1534 ieee80211_watchdog(ic); 1535 1536 IWI_UNLOCK(sc); 1537 } 1538 1539 static int 1540 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1541 { 1542 struct iwi_softc *sc = ifp->if_softc; 1543 struct ieee80211com *ic = &sc->sc_ic; 1544 struct ifreq *ifr; 1545 int error = 0; 1546 1547 IWI_LOCK(sc); 1548 1549 switch (cmd) { 1550 case SIOCSIFFLAGS: 1551 if (ifp->if_flags & IFF_UP) { 1552 if (!(ifp->if_flags & IFF_RUNNING)) 1553 iwi_init(sc); 1554 } else { 1555 if (ifp->if_flags & IFF_RUNNING) 1556 iwi_stop(sc); 1557 } 1558 break; 1559 1560 case SIOCSLOADFW: 1561 /* only super-user can do that! */ 1562 if ((error = suser(curthread)) != 0) 1563 break; 1564 1565 ifr = (struct ifreq *)data; 1566 error = iwi_cache_firmware(sc, ifr->ifr_data); 1567 break; 1568 1569 case SIOCSKILLFW: 1570 /* only super-user can do that! */ 1571 if ((error = suser(curthread)) != 0) 1572 break; 1573 1574 ifp->if_flags &= ~IFF_UP; 1575 iwi_stop(sc); 1576 iwi_free_firmware(sc); 1577 break; 1578 1579 default: 1580 error = ieee80211_ioctl(ic, cmd, data); 1581 } 1582 1583 if (error == ENETRESET) { 1584 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1585 (IFF_UP | IFF_RUNNING)) 1586 iwi_init(sc); 1587 error = 0; 1588 } 1589 1590 IWI_UNLOCK(sc); 1591 1592 return error; 1593 } 1594 1595 static void 1596 iwi_stop_master(struct iwi_softc *sc) 1597 { 1598 uint32_t tmp; 1599 int ntries; 1600 1601 /* disable interrupts */ 1602 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1603 1604 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER); 1605 for (ntries = 0; ntries < 5; ntries++) { 1606 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1607 break; 1608 DELAY(10); 1609 } 1610 if (ntries == 5) 1611 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1612 1613 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1614 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET); 1615 1616 sc->flags &= ~IWI_FLAG_FW_INITED; 1617 } 1618 1619 static int 1620 iwi_reset(struct iwi_softc *sc) 1621 { 1622 uint32_t tmp; 1623 int i, ntries; 1624 1625 iwi_stop_master(sc); 1626 1627 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 1628 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 1629 1630 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST); 1631 1632 /* wait for clock stabilization */ 1633 for (ntries = 0; ntries < 1000; ntries++) { 1634 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY) 1635 break; 1636 DELAY(200); 1637 } 1638 if (ntries == 1000) { 1639 device_printf(sc->sc_dev, 1640 "timeout waiting for clock stabilization\n"); 1641 return EIO; 1642 } 1643 1644 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1645 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET); 1646 1647 DELAY(10); 1648 1649 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 1650 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 1651 1652 /* clear NIC memory */ 1653 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0); 1654 for (i = 0; i < 0xc000; i++) 1655 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 1656 1657 return 0; 1658 } 1659 1660 static int 1661 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size) 1662 { 1663 uint32_t tmp; 1664 uint16_t *w; 1665 int ntries, i; 1666 1667 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1668 IWI_RST_STOP_MASTER); 1669 for (ntries = 0; ntries < 5; ntries++) { 1670 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1671 break; 1672 DELAY(10); 1673 } 1674 if (ntries == 5) { 1675 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1676 return EIO; 1677 } 1678 1679 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 1680 DELAY(5000); 1681 1682 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1683 tmp &= ~IWI_RST_PRINCETON_RESET; 1684 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 1685 1686 DELAY(5000); 1687 MEM_WRITE_4(sc, 0x3000e0, 0); 1688 DELAY(1000); 1689 MEM_WRITE_4(sc, 0x300004, 1); 1690 DELAY(1000); 1691 MEM_WRITE_4(sc, 0x300004, 0); 1692 DELAY(1000); 1693 MEM_WRITE_1(sc, 0x200000, 0x00); 1694 MEM_WRITE_1(sc, 0x200000, 0x40); 1695 DELAY(1000); 1696 1697 /* write microcode into adapter memory */ 1698 for (w = uc; size > 0; w++, size -= 2) 1699 MEM_WRITE_2(sc, 0x200010, *w); 1700 1701 MEM_WRITE_1(sc, 0x200000, 0x00); 1702 MEM_WRITE_1(sc, 0x200000, 0x80); 1703 1704 /* wait until we get an answer */ 1705 for (ntries = 0; ntries < 100; ntries++) { 1706 if (MEM_READ_1(sc, 0x200000) & 1) 1707 break; 1708 DELAY(100); 1709 } 1710 if (ntries == 100) { 1711 device_printf(sc->sc_dev, 1712 "timeout waiting for ucode to initialize\n"); 1713 return EIO; 1714 } 1715 1716 /* read the answer or the firmware will not initialize properly */ 1717 for (i = 0; i < 7; i++) 1718 MEM_READ_4(sc, 0x200004); 1719 1720 MEM_WRITE_1(sc, 0x200000, 0x00); 1721 1722 return 0; 1723 } 1724 1725 /* macro to handle unaligned little endian data in firmware image */ 1726 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 1727 1728 static int 1729 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size) 1730 { 1731 bus_dma_tag_t dmat; 1732 bus_dmamap_t map; 1733 bus_addr_t physaddr; 1734 void *virtaddr; 1735 u_char *p, *end; 1736 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp; 1737 int ntries, error = 0; 1738 1739 /* allocate DMA memory for mapping firmware image */ 1740 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 1741 BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, NULL, NULL, &dmat); 1742 if (error != 0) { 1743 device_printf(sc->sc_dev, 1744 "could not create firmware DMA tag\n"); 1745 goto fail1; 1746 } 1747 1748 error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_NOWAIT, &map); 1749 if (error != 0) { 1750 device_printf(sc->sc_dev, 1751 "could not allocate firmware DMA memory\n"); 1752 goto fail2; 1753 } 1754 1755 error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr, 1756 &physaddr, 0); 1757 if (error != 0) { 1758 device_printf(sc->sc_dev, "could not load firmware DMA map\n"); 1759 goto fail3; 1760 } 1761 1762 /* copy firmware image to DMA memory */ 1763 memcpy(virtaddr, fw, size); 1764 1765 /* make sure the adapter will get up-to-date values */ 1766 bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE); 1767 1768 /* tell the adapter where the command blocks are stored */ 1769 MEM_WRITE_4(sc, 0x3000a0, 0x27000); 1770 1771 /* 1772 * Store command blocks into adapter's internal memory using register 1773 * indirections. The adapter will read the firmware image through DMA 1774 * using information stored in command blocks. 1775 */ 1776 src = physaddr; 1777 p = virtaddr; 1778 end = p + size; 1779 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000); 1780 1781 while (p < end) { 1782 dst = GETLE32(p); p += 4; src += 4; 1783 len = GETLE32(p); p += 4; src += 4; 1784 p += len; 1785 1786 while (len > 0) { 1787 mlen = min(len, IWI_CB_MAXDATALEN); 1788 1789 ctl = IWI_CB_DEFAULT_CTL | mlen; 1790 sum = ctl ^ src ^ dst; 1791 1792 /* write a command block */ 1793 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl); 1794 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src); 1795 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst); 1796 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum); 1797 1798 src += mlen; 1799 dst += mlen; 1800 len -= mlen; 1801 } 1802 } 1803 1804 /* write a fictive final command block (sentinel) */ 1805 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR); 1806 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 1807 1808 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1809 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER); 1810 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 1811 1812 /* tell the adapter to start processing command blocks */ 1813 MEM_WRITE_4(sc, 0x3000a4, 0x540100); 1814 1815 /* wait until the adapter reach the sentinel */ 1816 for (ntries = 0; ntries < 400; ntries++) { 1817 if (MEM_READ_4(sc, 0x3000d0) >= sentinel) 1818 break; 1819 DELAY(100); 1820 } 1821 if (ntries == 400) { 1822 device_printf(sc->sc_dev, 1823 "timeout processing command blocks\n"); 1824 error = EIO; 1825 goto fail4; 1826 } 1827 1828 /* we're done with command blocks processing */ 1829 MEM_WRITE_4(sc, 0x3000a4, 0x540c00); 1830 1831 /* allow interrupts so we know when the firmware is inited */ 1832 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 1833 1834 /* tell the adapter to initialize the firmware */ 1835 CSR_WRITE_4(sc, IWI_CSR_RST, 0); 1836 1837 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 1838 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY); 1839 1840 /* wait at most one second for firmware initialization to complete */ 1841 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) { 1842 device_printf(sc->sc_dev, "timeout waiting for firmware " 1843 "initialization to complete\n"); 1844 goto fail4; 1845 } 1846 1847 fail4: bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE); 1848 bus_dmamap_unload(dmat, map); 1849 fail3: bus_dmamem_free(dmat, virtaddr, map); 1850 fail2: bus_dma_tag_destroy(dmat); 1851 fail1: 1852 return error; 1853 } 1854 1855 /* 1856 * Store firmware into kernel memory so we can download it when we need to, 1857 * e.g when the adapter wakes up from suspend mode. 1858 */ 1859 static int 1860 iwi_cache_firmware(struct iwi_softc *sc, void *data) 1861 { 1862 struct iwi_firmware *kfw = &sc->fw; 1863 struct iwi_firmware ufw; 1864 int error; 1865 1866 iwi_free_firmware(sc); 1867 1868 IWI_UNLOCK(sc); 1869 1870 if ((error = copyin(data, &ufw, sizeof ufw)) != 0) 1871 goto fail1; 1872 1873 kfw->boot_size = ufw.boot_size; 1874 kfw->ucode_size = ufw.ucode_size; 1875 kfw->main_size = ufw.main_size; 1876 1877 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT); 1878 if (kfw->boot == NULL) { 1879 error = ENOMEM; 1880 goto fail1; 1881 } 1882 1883 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT); 1884 if (kfw->ucode == NULL) { 1885 error = ENOMEM; 1886 goto fail2; 1887 } 1888 1889 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT); 1890 if (kfw->main == NULL) { 1891 error = ENOMEM; 1892 goto fail3; 1893 } 1894 1895 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0) 1896 goto fail4; 1897 1898 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0) 1899 goto fail4; 1900 1901 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0) 1902 goto fail4; 1903 1904 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n", 1905 kfw->boot_size, kfw->ucode_size, kfw->main_size)); 1906 1907 IWI_LOCK(sc); 1908 1909 sc->flags |= IWI_FLAG_FW_CACHED; 1910 1911 return 0; 1912 1913 fail4: free(kfw->boot, M_DEVBUF); 1914 fail3: free(kfw->ucode, M_DEVBUF); 1915 fail2: free(kfw->main, M_DEVBUF); 1916 fail1: IWI_LOCK(sc); 1917 1918 return error; 1919 } 1920 1921 static void 1922 iwi_free_firmware(struct iwi_softc *sc) 1923 { 1924 if (!(sc->flags & IWI_FLAG_FW_CACHED)) 1925 return; 1926 1927 free(sc->fw.boot, M_DEVBUF); 1928 free(sc->fw.ucode, M_DEVBUF); 1929 free(sc->fw.main, M_DEVBUF); 1930 1931 sc->flags &= ~IWI_FLAG_FW_CACHED; 1932 } 1933 1934 static int 1935 iwi_config(struct iwi_softc *sc) 1936 { 1937 struct ieee80211com *ic = &sc->sc_ic; 1938 struct ifnet *ifp = ic->ic_ifp; 1939 struct iwi_configuration config; 1940 struct iwi_rateset rs; 1941 struct iwi_txpower power; 1942 struct ieee80211_key *wk; 1943 struct iwi_wep_key wepkey; 1944 uint32_t data; 1945 int error, i; 1946 1947 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 1948 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":")); 1949 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr, 1950 IEEE80211_ADDR_LEN, 0); 1951 if (error != 0) 1952 return error; 1953 1954 memset(&config, 0, sizeof config); 1955 config.bluetooth_coexistence = sc->bluetooth; 1956 config.antenna = sc->antenna; 1957 config.multicast_enabled = 1; 1958 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 1959 config.disable_unicast_decryption = 1; 1960 config.disable_multicast_decryption = 1; 1961 DPRINTF(("Configuring adapter\n")); 1962 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0); 1963 if (error != 0) 1964 return error; 1965 1966 data = htole32(IWI_POWER_MODE_CAM); 1967 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 1968 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0); 1969 if (error != 0) 1970 return error; 1971 1972 data = htole32(ic->ic_rtsthreshold); 1973 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 1974 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0); 1975 if (error != 0) 1976 return error; 1977 1978 data = htole32(ic->ic_fragthreshold); 1979 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data))); 1980 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0); 1981 if (error != 0) 1982 return error; 1983 1984 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1985 power.mode = IWI_MODE_11B; 1986 power.nchan = 11; 1987 for (i = 0; i < 11; i++) { 1988 power.chan[i].chan = i + 1; 1989 power.chan[i].power = IWI_TXPOWER_MAX; 1990 } 1991 DPRINTF(("Setting .11b channels tx power\n")); 1992 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 1993 0); 1994 if (error != 0) 1995 return error; 1996 1997 power.mode = IWI_MODE_11G; 1998 DPRINTF(("Setting .11g channels tx power\n")); 1999 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2000 0); 2001 if (error != 0) 2002 return error; 2003 } 2004 2005 rs.mode = IWI_MODE_11G; 2006 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2007 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates; 2008 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, 2009 rs.nrates); 2010 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates)); 2011 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2012 if (error != 0) 2013 return error; 2014 2015 rs.mode = IWI_MODE_11A; 2016 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2017 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates; 2018 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, 2019 rs.nrates); 2020 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates)); 2021 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2022 if (error != 0) 2023 return error; 2024 2025 data = htole32(arc4random()); 2026 DPRINTF(("Setting initialization vector to %u\n", le32toh(data))); 2027 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0); 2028 if (error != 0) 2029 return error; 2030 2031 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2032 wk = &ic->ic_crypto.cs_nw_keys[i]; 2033 2034 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY; 2035 wepkey.idx = i; 2036 wepkey.len = wk->wk_keylen; 2037 memset(wepkey.key, 0, sizeof wepkey.key); 2038 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2039 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx, 2040 wepkey.len)); 2041 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey, 2042 sizeof wepkey, 0); 2043 if (error != 0) 2044 return error; 2045 } 2046 2047 /* enable adapter */ 2048 DPRINTF(("Enabling adapter\n")); 2049 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0); 2050 } 2051 2052 static int 2053 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan) 2054 { 2055 struct ieee80211com *ic = &sc->sc_ic; 2056 struct iwi_scan scan; 2057 2058 memset(&scan, 0, sizeof scan); 2059 scan.type = IWI_SCAN_TYPE_PASSIVE; 2060 scan.dwelltime = htole16(2000); 2061 scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : 2062 IWI_CHAN_2GHZ); 2063 scan.channels[1] = ieee80211_chan2ieee(ic, chan); 2064 2065 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan))); 2066 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2067 } 2068 2069 static int 2070 iwi_scan(struct iwi_softc *sc) 2071 { 2072 struct ieee80211com *ic = &sc->sc_ic; 2073 struct iwi_scan scan; 2074 uint8_t *p; 2075 int i, count; 2076 2077 memset(&scan, 0, sizeof scan); 2078 scan.type = IWI_SCAN_TYPE_BROADCAST; 2079 scan.dwelltime = htole16(sc->dwelltime); 2080 2081 p = scan.channels; 2082 count = 0; 2083 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2084 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) && 2085 isset(ic->ic_chan_active, i)) { 2086 *++p = i; 2087 count++; 2088 } 2089 } 2090 *(p - count) = IWI_CHAN_5GHZ | count; 2091 2092 count = 0; 2093 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2094 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) && 2095 isset(ic->ic_chan_active, i)) { 2096 *++p = i; 2097 count++; 2098 } 2099 } 2100 *(p - count) = IWI_CHAN_2GHZ | count; 2101 2102 DPRINTF(("Start scanning\n")); 2103 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2104 } 2105 2106 static int 2107 iwi_auth_and_assoc(struct iwi_softc *sc) 2108 { 2109 struct ieee80211com *ic = &sc->sc_ic; 2110 struct ifnet *ifp = ic->ic_ifp; 2111 struct ieee80211_node *ni = ic->ic_bss; 2112 struct iwi_configuration config; 2113 struct iwi_associate assoc; 2114 struct iwi_rateset rs; 2115 uint16_t capinfo; 2116 uint32_t data; 2117 int error; 2118 2119 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2120 memset(&config, 0, sizeof config); 2121 config.bluetooth_coexistence = sc->bluetooth; 2122 config.antenna = sc->antenna; 2123 config.multicast_enabled = 1; 2124 config.use_protection = 1; 2125 config.answer_pbreq = 2126 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2127 config.disable_unicast_decryption = 1; 2128 config.disable_multicast_decryption = 1; 2129 DPRINTF(("Configuring adapter\n")); 2130 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 2131 1); 2132 if (error != 0) 2133 return error; 2134 } 2135 2136 #ifdef IWI_DEBUG 2137 if (iwi_debug > 0) { 2138 printf("Setting ESSID to "); 2139 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 2140 printf("\n"); 2141 } 2142 #endif 2143 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1); 2144 if (error != 0) 2145 return error; 2146 2147 /* the rate set has already been "negociated" */ 2148 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2149 IWI_MODE_11G; 2150 rs.type = IWI_RATESET_TYPE_NEGOCIATED; 2151 rs.nrates = ni->ni_rates.rs_nrates; 2152 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates); 2153 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates)); 2154 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1); 2155 if (error != 0) 2156 return error; 2157 2158 if (ic->ic_opt_ie != NULL) { 2159 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len)); 2160 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie, 2161 ic->ic_opt_ie_len, 1); 2162 if (error != 0) 2163 return error; 2164 } 2165 2166 data = htole32(ni->ni_rssi); 2167 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi)); 2168 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1); 2169 if (error != 0) 2170 return error; 2171 2172 memset(&assoc, 0, sizeof assoc); 2173 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2174 IWI_MODE_11G; 2175 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2176 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) 2177 assoc.auth = ic->ic_crypto.cs_def_txkey << 4 | IWI_AUTH_SHARED; 2178 if (ic->ic_opt_ie != NULL) 2179 assoc.policy |= htole16(IWI_POLICY_OPTIE); 2180 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8); 2181 2182 if (ic->ic_opmode == IEEE80211_M_IBSS) 2183 capinfo = IEEE80211_CAPINFO_IBSS; 2184 else 2185 capinfo = IEEE80211_CAPINFO_ESS; 2186 if (ic->ic_flags & IEEE80211_F_PRIVACY) 2187 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2188 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2189 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2190 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2191 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2192 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2193 assoc.capinfo = htole16(capinfo); 2194 2195 assoc.lintval = htole16(ic->ic_lintval); 2196 assoc.intval = htole16(ni->ni_intval); 2197 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid); 2198 if (ic->ic_opmode == IEEE80211_M_IBSS) 2199 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr); 2200 else 2201 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid); 2202 2203 DPRINTF(("Trying to associate to %6D channel %u auth %u\n", 2204 assoc.bssid, ":", assoc.chan, assoc.auth)); 2205 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1); 2206 } 2207 2208 static void 2209 iwi_init(void *priv) 2210 { 2211 struct iwi_softc *sc = priv; 2212 struct ieee80211com *ic = &sc->sc_ic; 2213 struct ifnet *ifp = ic->ic_ifp; 2214 struct iwi_firmware *fw = &sc->fw; 2215 struct iwi_rx_data *data; 2216 int i; 2217 2218 /* exit immediately if firmware has not been ioctl'd */ 2219 if (!(sc->flags & IWI_FLAG_FW_CACHED)) { 2220 if (!(sc->flags & IWI_FLAG_FW_WARNED)) 2221 device_printf(sc->sc_dev, "Please load firmware\n"); 2222 sc->flags |= IWI_FLAG_FW_WARNED; 2223 ifp->if_flags &= ~IFF_UP; 2224 return; 2225 } 2226 2227 iwi_stop(sc); 2228 2229 if (iwi_reset(sc) != 0) { 2230 device_printf(sc->sc_dev, "could not reset adapter\n"); 2231 goto fail; 2232 } 2233 2234 if (iwi_load_firmware(sc, fw->boot, fw->boot_size) != 0) { 2235 device_printf(sc->sc_dev, "could not load boot firmware\n"); 2236 goto fail; 2237 } 2238 2239 if (iwi_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) { 2240 device_printf(sc->sc_dev, "could not load microcode\n"); 2241 goto fail; 2242 } 2243 2244 iwi_stop_master(sc); 2245 2246 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr); 2247 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count); 2248 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 2249 2250 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.physaddr); 2251 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count); 2252 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur); 2253 2254 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.physaddr); 2255 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count); 2256 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur); 2257 2258 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.physaddr); 2259 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count); 2260 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur); 2261 2262 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.physaddr); 2263 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count); 2264 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur); 2265 2266 for (i = 0; i < sc->rxq.count; i++) { 2267 data = &sc->rxq.data[i]; 2268 CSR_WRITE_4(sc, data->reg, data->physaddr); 2269 } 2270 2271 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1); 2272 2273 if (iwi_load_firmware(sc, fw->main, fw->main_size) != 0) { 2274 device_printf(sc->sc_dev, "could not load main firmware\n"); 2275 goto fail; 2276 } 2277 2278 sc->flags |= IWI_FLAG_FW_INITED; 2279 2280 if (iwi_config(sc) != 0) { 2281 device_printf(sc->sc_dev, "device configuration failed\n"); 2282 goto fail; 2283 } 2284 2285 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2286 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2287 else 2288 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2289 2290 ifp->if_flags &= ~IFF_OACTIVE; 2291 ifp->if_flags |= IFF_RUNNING; 2292 2293 return; 2294 2295 fail: ifp->if_flags &= ~IFF_UP; 2296 iwi_stop(sc); 2297 } 2298 2299 static void 2300 iwi_stop(void *priv) 2301 { 2302 struct iwi_softc *sc = priv; 2303 struct ieee80211com *ic = &sc->sc_ic; 2304 struct ifnet *ifp = ic->ic_ifp; 2305 2306 iwi_stop_master(sc); 2307 2308 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET); 2309 2310 /* reset rings */ 2311 iwi_reset_cmd_ring(sc, &sc->cmdq); 2312 iwi_reset_tx_ring(sc, &sc->txq); 2313 iwi_reset_rx_ring(sc, &sc->rxq); 2314 2315 sc->sc_tx_timer = 0; 2316 ifp->if_timer = 0; 2317 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2318 2319 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2320 } 2321 2322 #ifdef IWI_DEBUG 2323 static int 2324 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS) 2325 { 2326 struct iwi_softc *sc = arg1; 2327 uint32_t size, buf[128]; 2328 2329 if (!(sc->flags & IWI_FLAG_FW_INITED)) { 2330 memset(buf, 0, sizeof buf); 2331 return SYSCTL_OUT(req, buf, sizeof buf); 2332 } 2333 2334 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1); 2335 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size); 2336 2337 return SYSCTL_OUT(req, buf, sizeof buf); 2338 } 2339 #endif 2340 2341 static int 2342 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS) 2343 { 2344 struct iwi_softc *sc = arg1; 2345 int val; 2346 2347 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0; 2348 2349 return SYSCTL_OUT(req, &val, sizeof val); 2350 } 2351