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