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