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