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