1 /*- 2 * Copyright (c) 2004, 2005 3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 4 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting 5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org> 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/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/module.h> 49 #include <sys/bus.h> 50 #include <sys/endian.h> 51 #include <sys/proc.h> 52 #include <sys/mount.h> 53 #include <sys/namei.h> 54 #include <sys/linker.h> 55 #include <sys/firmware.h> 56 #include <sys/taskqueue.h> 57 58 #include <machine/bus.h> 59 #include <machine/resource.h> 60 #include <sys/rman.h> 61 62 #include <dev/pci/pcireg.h> 63 #include <dev/pci/pcivar.h> 64 65 #include <net/bpf.h> 66 #include <net/if.h> 67 #include <net/if_var.h> 68 #include <net/if_arp.h> 69 #include <net/ethernet.h> 70 #include <net/if_dl.h> 71 #include <net/if_media.h> 72 #include <net/if_types.h> 73 74 #include <net80211/ieee80211_var.h> 75 #include <net80211/ieee80211_radiotap.h> 76 #include <net80211/ieee80211_input.h> 77 #include <net80211/ieee80211_regdomain.h> 78 79 #include <netinet/in.h> 80 #include <netinet/in_systm.h> 81 #include <netinet/in_var.h> 82 #include <netinet/ip.h> 83 #include <netinet/if_ether.h> 84 85 #include <dev/iwi/if_iwireg.h> 86 #include <dev/iwi/if_iwivar.h> 87 88 #define IWI_DEBUG 89 #ifdef IWI_DEBUG 90 #define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0) 91 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0) 92 int iwi_debug = 0; 93 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level"); 94 95 static const char *iwi_fw_states[] = { 96 "IDLE", /* IWI_FW_IDLE */ 97 "LOADING", /* IWI_FW_LOADING */ 98 "ASSOCIATING", /* IWI_FW_ASSOCIATING */ 99 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */ 100 "SCANNING", /* IWI_FW_SCANNING */ 101 }; 102 #else 103 #define DPRINTF(x) 104 #define DPRINTFN(n, x) 105 #endif 106 107 MODULE_DEPEND(iwi, pci, 1, 1, 1); 108 MODULE_DEPEND(iwi, wlan, 1, 1, 1); 109 MODULE_DEPEND(iwi, firmware, 1, 1, 1); 110 111 enum { 112 IWI_LED_TX, 113 IWI_LED_RX, 114 IWI_LED_POLL, 115 }; 116 117 struct iwi_ident { 118 uint16_t vendor; 119 uint16_t device; 120 const char *name; 121 }; 122 123 static const struct iwi_ident iwi_ident_table[] = { 124 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" }, 125 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" }, 126 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" }, 127 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" }, 128 129 { 0, 0, NULL } 130 }; 131 132 static struct ieee80211vap *iwi_vap_create(struct ieee80211com *, 133 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 134 const uint8_t [IEEE80211_ADDR_LEN], 135 const uint8_t [IEEE80211_ADDR_LEN]); 136 static void iwi_vap_delete(struct ieee80211vap *); 137 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int); 138 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *, 139 int); 140 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 141 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 142 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *, 143 int, bus_addr_t, bus_addr_t); 144 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 145 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 146 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *, 147 int); 148 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 149 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 150 static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *, 151 const uint8_t [IEEE80211_ADDR_LEN]); 152 static void iwi_node_free(struct ieee80211_node *); 153 static void iwi_media_status(struct ifnet *, struct ifmediareq *); 154 static int iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int); 155 static void iwi_wme_init(struct iwi_softc *); 156 static int iwi_wme_setparams(struct iwi_softc *, struct ieee80211com *); 157 static void iwi_update_wme(void *, int); 158 static int iwi_wme_update(struct ieee80211com *); 159 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t); 160 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int, 161 struct iwi_frame *); 162 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *); 163 static void iwi_rx_intr(struct iwi_softc *); 164 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *); 165 static void iwi_intr(void *); 166 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t); 167 static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int); 168 static int iwi_tx_start(struct ifnet *, struct mbuf *, 169 struct ieee80211_node *, int); 170 static int iwi_raw_xmit(struct ieee80211_node *, struct mbuf *, 171 const struct ieee80211_bpf_params *); 172 static void iwi_start_locked(struct ifnet *); 173 static void iwi_start(struct ifnet *); 174 static void iwi_watchdog(void *); 175 static int iwi_ioctl(struct ifnet *, u_long, caddr_t); 176 static void iwi_stop_master(struct iwi_softc *); 177 static int iwi_reset(struct iwi_softc *); 178 static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *); 179 static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *); 180 static void iwi_release_fw_dma(struct iwi_softc *sc); 181 static int iwi_config(struct iwi_softc *); 182 static int iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode); 183 static void iwi_put_firmware(struct iwi_softc *); 184 static void iwi_monitor_scan(void *, int); 185 static int iwi_scanchan(struct iwi_softc *, unsigned long, int); 186 static void iwi_scan_start(struct ieee80211com *); 187 static void iwi_scan_end(struct ieee80211com *); 188 static void iwi_set_channel(struct ieee80211com *); 189 static void iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell); 190 static void iwi_scan_mindwell(struct ieee80211_scan_state *); 191 static int iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *); 192 static void iwi_disassoc(void *, int); 193 static int iwi_disassociate(struct iwi_softc *, int quiet); 194 static void iwi_init_locked(struct iwi_softc *); 195 static void iwi_init(void *); 196 static int iwi_init_fw_dma(struct iwi_softc *, int); 197 static void iwi_stop_locked(void *); 198 static void iwi_stop(struct iwi_softc *); 199 static void iwi_restart(void *, int); 200 static int iwi_getrfkill(struct iwi_softc *); 201 static void iwi_radio_on(void *, int); 202 static void iwi_radio_off(void *, int); 203 static void iwi_sysctlattach(struct iwi_softc *); 204 static void iwi_led_event(struct iwi_softc *, int); 205 static void iwi_ledattach(struct iwi_softc *); 206 207 static int iwi_probe(device_t); 208 static int iwi_attach(device_t); 209 static int iwi_detach(device_t); 210 static int iwi_shutdown(device_t); 211 static int iwi_suspend(device_t); 212 static int iwi_resume(device_t); 213 214 static device_method_t iwi_methods[] = { 215 /* Device interface */ 216 DEVMETHOD(device_probe, iwi_probe), 217 DEVMETHOD(device_attach, iwi_attach), 218 DEVMETHOD(device_detach, iwi_detach), 219 DEVMETHOD(device_shutdown, iwi_shutdown), 220 DEVMETHOD(device_suspend, iwi_suspend), 221 DEVMETHOD(device_resume, iwi_resume), 222 223 DEVMETHOD_END 224 }; 225 226 static driver_t iwi_driver = { 227 "iwi", 228 iwi_methods, 229 sizeof (struct iwi_softc) 230 }; 231 232 static devclass_t iwi_devclass; 233 234 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, NULL, NULL); 235 236 MODULE_VERSION(iwi, 1); 237 238 static __inline uint8_t 239 MEM_READ_1(struct iwi_softc *sc, uint32_t addr) 240 { 241 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 242 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA); 243 } 244 245 static __inline uint32_t 246 MEM_READ_4(struct iwi_softc *sc, uint32_t addr) 247 { 248 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 249 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA); 250 } 251 252 static int 253 iwi_probe(device_t dev) 254 { 255 const struct iwi_ident *ident; 256 257 for (ident = iwi_ident_table; ident->name != NULL; ident++) { 258 if (pci_get_vendor(dev) == ident->vendor && 259 pci_get_device(dev) == ident->device) { 260 device_set_desc(dev, ident->name); 261 return (BUS_PROBE_DEFAULT); 262 } 263 } 264 return ENXIO; 265 } 266 267 static int 268 iwi_attach(device_t dev) 269 { 270 struct iwi_softc *sc = device_get_softc(dev); 271 struct ifnet *ifp; 272 struct ieee80211com *ic; 273 uint16_t val; 274 int i, error; 275 uint8_t bands; 276 uint8_t macaddr[IEEE80211_ADDR_LEN]; 277 278 sc->sc_dev = dev; 279 280 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 281 if (ifp == NULL) { 282 device_printf(dev, "can not if_alloc()\n"); 283 return ENXIO; 284 } 285 ic = ifp->if_l2com; 286 287 IWI_LOCK_INIT(sc); 288 289 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx); 290 291 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc); 292 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc); 293 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc); 294 TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc); 295 TASK_INIT(&sc->sc_wmetask, 0, iwi_update_wme, sc); 296 TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc); 297 298 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0); 299 callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0); 300 301 pci_write_config(dev, 0x41, 0, 1); 302 303 /* enable bus-mastering */ 304 pci_enable_busmaster(dev); 305 306 i = PCIR_BAR(0); 307 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE); 308 if (sc->mem == NULL) { 309 device_printf(dev, "could not allocate memory resource\n"); 310 goto fail; 311 } 312 313 sc->sc_st = rman_get_bustag(sc->mem); 314 sc->sc_sh = rman_get_bushandle(sc->mem); 315 316 i = 0; 317 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, 318 RF_ACTIVE | RF_SHAREABLE); 319 if (sc->irq == NULL) { 320 device_printf(dev, "could not allocate interrupt resource\n"); 321 goto fail; 322 } 323 324 if (iwi_reset(sc) != 0) { 325 device_printf(dev, "could not reset adapter\n"); 326 goto fail; 327 } 328 329 /* 330 * Allocate rings. 331 */ 332 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) { 333 device_printf(dev, "could not allocate Cmd ring\n"); 334 goto fail; 335 } 336 337 for (i = 0; i < 4; i++) { 338 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT, 339 IWI_CSR_TX1_RIDX + i * 4, 340 IWI_CSR_TX1_WIDX + i * 4); 341 if (error != 0) { 342 device_printf(dev, "could not allocate Tx ring %d\n", 343 i+i); 344 goto fail; 345 } 346 } 347 348 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) { 349 device_printf(dev, "could not allocate Rx ring\n"); 350 goto fail; 351 } 352 353 iwi_wme_init(sc); 354 355 ifp->if_softc = sc; 356 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 357 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 358 ifp->if_init = iwi_init; 359 ifp->if_ioctl = iwi_ioctl; 360 ifp->if_start = iwi_start; 361 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 362 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 363 IFQ_SET_READY(&ifp->if_snd); 364 365 ic->ic_ifp = ifp; 366 ic->ic_opmode = IEEE80211_M_STA; 367 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 368 369 /* set device capabilities */ 370 ic->ic_caps = 371 IEEE80211_C_STA /* station mode supported */ 372 | IEEE80211_C_IBSS /* IBSS mode supported */ 373 | IEEE80211_C_MONITOR /* monitor mode supported */ 374 | IEEE80211_C_PMGT /* power save supported */ 375 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 376 | IEEE80211_C_WPA /* 802.11i */ 377 | IEEE80211_C_WME /* 802.11e */ 378 #if 0 379 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 380 #endif 381 ; 382 383 /* read MAC address from EEPROM */ 384 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0); 385 macaddr[0] = val & 0xff; 386 macaddr[1] = val >> 8; 387 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1); 388 macaddr[2] = val & 0xff; 389 macaddr[3] = val >> 8; 390 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2); 391 macaddr[4] = val & 0xff; 392 macaddr[5] = val >> 8; 393 394 bands = 0; 395 setbit(&bands, IEEE80211_MODE_11B); 396 setbit(&bands, IEEE80211_MODE_11G); 397 if (pci_get_device(dev) >= 0x4223) 398 setbit(&bands, IEEE80211_MODE_11A); 399 ieee80211_init_channels(ic, NULL, &bands); 400 401 ieee80211_ifattach(ic, macaddr); 402 /* override default methods */ 403 ic->ic_node_alloc = iwi_node_alloc; 404 sc->sc_node_free = ic->ic_node_free; 405 ic->ic_node_free = iwi_node_free; 406 ic->ic_raw_xmit = iwi_raw_xmit; 407 ic->ic_scan_start = iwi_scan_start; 408 ic->ic_scan_end = iwi_scan_end; 409 ic->ic_set_channel = iwi_set_channel; 410 ic->ic_scan_curchan = iwi_scan_curchan; 411 ic->ic_scan_mindwell = iwi_scan_mindwell; 412 ic->ic_wme.wme_update = iwi_wme_update; 413 414 ic->ic_vap_create = iwi_vap_create; 415 ic->ic_vap_delete = iwi_vap_delete; 416 417 ieee80211_radiotap_attach(ic, 418 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 419 IWI_TX_RADIOTAP_PRESENT, 420 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 421 IWI_RX_RADIOTAP_PRESENT); 422 423 iwi_sysctlattach(sc); 424 iwi_ledattach(sc); 425 426 /* 427 * Hook our interrupt after all initialization is complete. 428 */ 429 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 430 NULL, iwi_intr, sc, &sc->sc_ih); 431 if (error != 0) { 432 device_printf(dev, "could not set up interrupt\n"); 433 goto fail; 434 } 435 436 if (bootverbose) 437 ieee80211_announce(ic); 438 439 return 0; 440 fail: 441 /* XXX fix */ 442 iwi_detach(dev); 443 return ENXIO; 444 } 445 446 static int 447 iwi_detach(device_t dev) 448 { 449 struct iwi_softc *sc = device_get_softc(dev); 450 struct ifnet *ifp = sc->sc_ifp; 451 struct ieee80211com *ic = ifp->if_l2com; 452 453 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 454 455 /* NB: do early to drain any pending tasks */ 456 ieee80211_draintask(ic, &sc->sc_radiontask); 457 ieee80211_draintask(ic, &sc->sc_radiofftask); 458 ieee80211_draintask(ic, &sc->sc_restarttask); 459 ieee80211_draintask(ic, &sc->sc_disassoctask); 460 ieee80211_draintask(ic, &sc->sc_monitortask); 461 462 iwi_stop(sc); 463 464 ieee80211_ifdetach(ic); 465 466 iwi_put_firmware(sc); 467 iwi_release_fw_dma(sc); 468 469 iwi_free_cmd_ring(sc, &sc->cmdq); 470 iwi_free_tx_ring(sc, &sc->txq[0]); 471 iwi_free_tx_ring(sc, &sc->txq[1]); 472 iwi_free_tx_ring(sc, &sc->txq[2]); 473 iwi_free_tx_ring(sc, &sc->txq[3]); 474 iwi_free_rx_ring(sc, &sc->rxq); 475 476 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq); 477 478 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem), 479 sc->mem); 480 481 delete_unrhdr(sc->sc_unr); 482 483 IWI_LOCK_DESTROY(sc); 484 485 if_free(ifp); 486 487 return 0; 488 } 489 490 static struct ieee80211vap * 491 iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 492 enum ieee80211_opmode opmode, int flags, 493 const uint8_t bssid[IEEE80211_ADDR_LEN], 494 const uint8_t mac[IEEE80211_ADDR_LEN]) 495 { 496 struct ifnet *ifp = ic->ic_ifp; 497 struct iwi_softc *sc = ifp->if_softc; 498 struct iwi_vap *ivp; 499 struct ieee80211vap *vap; 500 int i; 501 502 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 503 return NULL; 504 /* 505 * Get firmware image (and possibly dma memory) on mode change. 506 */ 507 if (iwi_get_firmware(sc, opmode)) 508 return NULL; 509 /* allocate DMA memory for mapping firmware image */ 510 i = sc->fw_fw.size; 511 if (sc->fw_boot.size > i) 512 i = sc->fw_boot.size; 513 /* XXX do we dma the ucode as well ? */ 514 if (sc->fw_uc.size > i) 515 i = sc->fw_uc.size; 516 if (iwi_init_fw_dma(sc, i)) 517 return NULL; 518 519 ivp = (struct iwi_vap *) malloc(sizeof(struct iwi_vap), 520 M_80211_VAP, M_NOWAIT | M_ZERO); 521 if (ivp == NULL) 522 return NULL; 523 vap = &ivp->iwi_vap; 524 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 525 /* override the default, the setting comes from the linux driver */ 526 vap->iv_bmissthreshold = 24; 527 /* override with driver methods */ 528 ivp->iwi_newstate = vap->iv_newstate; 529 vap->iv_newstate = iwi_newstate; 530 531 /* complete setup */ 532 ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status); 533 ic->ic_opmode = opmode; 534 return vap; 535 } 536 537 static void 538 iwi_vap_delete(struct ieee80211vap *vap) 539 { 540 struct iwi_vap *ivp = IWI_VAP(vap); 541 542 ieee80211_vap_detach(vap); 543 free(ivp, M_80211_VAP); 544 } 545 546 static void 547 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 548 { 549 if (error != 0) 550 return; 551 552 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 553 554 *(bus_addr_t *)arg = segs[0].ds_addr; 555 } 556 557 static int 558 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count) 559 { 560 int error; 561 562 ring->count = count; 563 ring->queued = 0; 564 ring->cur = ring->next = 0; 565 566 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 567 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 568 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0, 569 NULL, NULL, &ring->desc_dmat); 570 if (error != 0) { 571 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 572 goto fail; 573 } 574 575 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 576 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 577 if (error != 0) { 578 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 579 goto fail; 580 } 581 582 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 583 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 584 if (error != 0) { 585 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 586 goto fail; 587 } 588 589 return 0; 590 591 fail: iwi_free_cmd_ring(sc, ring); 592 return error; 593 } 594 595 static void 596 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 597 { 598 ring->queued = 0; 599 ring->cur = ring->next = 0; 600 } 601 602 static void 603 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 604 { 605 if (ring->desc != NULL) { 606 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 607 BUS_DMASYNC_POSTWRITE); 608 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 609 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 610 } 611 612 if (ring->desc_dmat != NULL) 613 bus_dma_tag_destroy(ring->desc_dmat); 614 } 615 616 static int 617 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count, 618 bus_addr_t csr_ridx, bus_addr_t csr_widx) 619 { 620 int i, error; 621 622 ring->count = count; 623 ring->queued = 0; 624 ring->cur = ring->next = 0; 625 ring->csr_ridx = csr_ridx; 626 ring->csr_widx = csr_widx; 627 628 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 629 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 630 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL, 631 NULL, &ring->desc_dmat); 632 if (error != 0) { 633 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 634 goto fail; 635 } 636 637 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 638 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 639 if (error != 0) { 640 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 641 goto fail; 642 } 643 644 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 645 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 646 if (error != 0) { 647 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 648 goto fail; 649 } 650 651 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF, 652 M_NOWAIT | M_ZERO); 653 if (ring->data == NULL) { 654 device_printf(sc->sc_dev, "could not allocate soft data\n"); 655 error = ENOMEM; 656 goto fail; 657 } 658 659 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 660 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 661 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 662 if (error != 0) { 663 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 664 goto fail; 665 } 666 667 for (i = 0; i < count; i++) { 668 error = bus_dmamap_create(ring->data_dmat, 0, 669 &ring->data[i].map); 670 if (error != 0) { 671 device_printf(sc->sc_dev, "could not create DMA map\n"); 672 goto fail; 673 } 674 } 675 676 return 0; 677 678 fail: iwi_free_tx_ring(sc, ring); 679 return error; 680 } 681 682 static void 683 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 684 { 685 struct iwi_tx_data *data; 686 int i; 687 688 for (i = 0; i < ring->count; i++) { 689 data = &ring->data[i]; 690 691 if (data->m != NULL) { 692 bus_dmamap_sync(ring->data_dmat, data->map, 693 BUS_DMASYNC_POSTWRITE); 694 bus_dmamap_unload(ring->data_dmat, data->map); 695 m_freem(data->m); 696 data->m = NULL; 697 } 698 699 if (data->ni != NULL) { 700 ieee80211_free_node(data->ni); 701 data->ni = NULL; 702 } 703 } 704 705 ring->queued = 0; 706 ring->cur = ring->next = 0; 707 } 708 709 static void 710 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 711 { 712 struct iwi_tx_data *data; 713 int i; 714 715 if (ring->desc != NULL) { 716 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 717 BUS_DMASYNC_POSTWRITE); 718 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 719 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 720 } 721 722 if (ring->desc_dmat != NULL) 723 bus_dma_tag_destroy(ring->desc_dmat); 724 725 if (ring->data != NULL) { 726 for (i = 0; i < ring->count; i++) { 727 data = &ring->data[i]; 728 729 if (data->m != NULL) { 730 bus_dmamap_sync(ring->data_dmat, data->map, 731 BUS_DMASYNC_POSTWRITE); 732 bus_dmamap_unload(ring->data_dmat, data->map); 733 m_freem(data->m); 734 } 735 736 if (data->ni != NULL) 737 ieee80211_free_node(data->ni); 738 739 if (data->map != NULL) 740 bus_dmamap_destroy(ring->data_dmat, data->map); 741 } 742 743 free(ring->data, M_DEVBUF); 744 } 745 746 if (ring->data_dmat != NULL) 747 bus_dma_tag_destroy(ring->data_dmat); 748 } 749 750 static int 751 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count) 752 { 753 struct iwi_rx_data *data; 754 int i, error; 755 756 ring->count = count; 757 ring->cur = 0; 758 759 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF, 760 M_NOWAIT | M_ZERO); 761 if (ring->data == NULL) { 762 device_printf(sc->sc_dev, "could not allocate soft data\n"); 763 error = ENOMEM; 764 goto fail; 765 } 766 767 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 768 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 769 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 770 if (error != 0) { 771 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 772 goto fail; 773 } 774 775 for (i = 0; i < count; i++) { 776 data = &ring->data[i]; 777 778 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 779 if (error != 0) { 780 device_printf(sc->sc_dev, "could not create DMA map\n"); 781 goto fail; 782 } 783 784 data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 785 if (data->m == NULL) { 786 device_printf(sc->sc_dev, 787 "could not allocate rx mbuf\n"); 788 error = ENOMEM; 789 goto fail; 790 } 791 792 error = bus_dmamap_load(ring->data_dmat, data->map, 793 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, 794 &data->physaddr, 0); 795 if (error != 0) { 796 device_printf(sc->sc_dev, 797 "could not load rx buf DMA map"); 798 goto fail; 799 } 800 801 data->reg = IWI_CSR_RX_BASE + i * 4; 802 } 803 804 return 0; 805 806 fail: iwi_free_rx_ring(sc, ring); 807 return error; 808 } 809 810 static void 811 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 812 { 813 ring->cur = 0; 814 } 815 816 static void 817 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 818 { 819 struct iwi_rx_data *data; 820 int i; 821 822 if (ring->data != NULL) { 823 for (i = 0; i < ring->count; i++) { 824 data = &ring->data[i]; 825 826 if (data->m != NULL) { 827 bus_dmamap_sync(ring->data_dmat, data->map, 828 BUS_DMASYNC_POSTREAD); 829 bus_dmamap_unload(ring->data_dmat, data->map); 830 m_freem(data->m); 831 } 832 833 if (data->map != NULL) 834 bus_dmamap_destroy(ring->data_dmat, data->map); 835 } 836 837 free(ring->data, M_DEVBUF); 838 } 839 840 if (ring->data_dmat != NULL) 841 bus_dma_tag_destroy(ring->data_dmat); 842 } 843 844 static int 845 iwi_shutdown(device_t dev) 846 { 847 struct iwi_softc *sc = device_get_softc(dev); 848 849 iwi_stop(sc); 850 iwi_put_firmware(sc); /* ??? XXX */ 851 852 return 0; 853 } 854 855 static int 856 iwi_suspend(device_t dev) 857 { 858 struct iwi_softc *sc = device_get_softc(dev); 859 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 860 861 ieee80211_suspend_all(ic); 862 return 0; 863 } 864 865 static int 866 iwi_resume(device_t dev) 867 { 868 struct iwi_softc *sc = device_get_softc(dev); 869 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 870 871 pci_write_config(dev, 0x41, 0, 1); 872 873 ieee80211_resume_all(ic); 874 return 0; 875 } 876 877 static struct ieee80211_node * 878 iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 879 { 880 struct iwi_node *in; 881 882 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO); 883 if (in == NULL) 884 return NULL; 885 /* XXX assign sta table entry for adhoc */ 886 in->in_station = -1; 887 888 return &in->in_node; 889 } 890 891 static void 892 iwi_node_free(struct ieee80211_node *ni) 893 { 894 struct ieee80211com *ic = ni->ni_ic; 895 struct iwi_softc *sc = ic->ic_ifp->if_softc; 896 struct iwi_node *in = (struct iwi_node *)ni; 897 898 if (in->in_station != -1) { 899 DPRINTF(("%s mac %6D station %u\n", __func__, 900 ni->ni_macaddr, ":", in->in_station)); 901 free_unr(sc->sc_unr, in->in_station); 902 } 903 904 sc->sc_node_free(ni); 905 } 906 907 /* 908 * Convert h/w rate code to IEEE rate code. 909 */ 910 static int 911 iwi_cvtrate(int iwirate) 912 { 913 switch (iwirate) { 914 case IWI_RATE_DS1: return 2; 915 case IWI_RATE_DS2: return 4; 916 case IWI_RATE_DS5: return 11; 917 case IWI_RATE_DS11: return 22; 918 case IWI_RATE_OFDM6: return 12; 919 case IWI_RATE_OFDM9: return 18; 920 case IWI_RATE_OFDM12: return 24; 921 case IWI_RATE_OFDM18: return 36; 922 case IWI_RATE_OFDM24: return 48; 923 case IWI_RATE_OFDM36: return 72; 924 case IWI_RATE_OFDM48: return 96; 925 case IWI_RATE_OFDM54: return 108; 926 } 927 return 0; 928 } 929 930 /* 931 * The firmware automatically adapts the transmit speed. We report its current 932 * value here. 933 */ 934 static void 935 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 936 { 937 struct ieee80211vap *vap = ifp->if_softc; 938 struct ieee80211com *ic = vap->iv_ic; 939 struct iwi_softc *sc = ic->ic_ifp->if_softc; 940 struct ieee80211_node *ni; 941 942 /* read current transmission rate from adapter */ 943 ni = ieee80211_ref_node(vap->iv_bss); 944 ni->ni_txrate = 945 iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE)); 946 ieee80211_free_node(ni); 947 ieee80211_media_status(ifp, imr); 948 } 949 950 static int 951 iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 952 { 953 struct iwi_vap *ivp = IWI_VAP(vap); 954 struct ieee80211com *ic = vap->iv_ic; 955 struct ifnet *ifp = ic->ic_ifp; 956 struct iwi_softc *sc = ifp->if_softc; 957 IWI_LOCK_DECL; 958 959 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__, 960 ieee80211_state_name[vap->iv_state], 961 ieee80211_state_name[nstate], sc->flags)); 962 963 IEEE80211_UNLOCK(ic); 964 IWI_LOCK(sc); 965 switch (nstate) { 966 case IEEE80211_S_INIT: 967 /* 968 * NB: don't try to do this if iwi_stop_master has 969 * shutdown the firmware and disabled interrupts. 970 */ 971 if (vap->iv_state == IEEE80211_S_RUN && 972 (sc->flags & IWI_FLAG_FW_INITED)) 973 iwi_disassociate(sc, 0); 974 break; 975 case IEEE80211_S_AUTH: 976 iwi_auth_and_assoc(sc, vap); 977 break; 978 case IEEE80211_S_RUN: 979 if (vap->iv_opmode == IEEE80211_M_IBSS && 980 vap->iv_state == IEEE80211_S_SCAN) { 981 /* 982 * XXX when joining an ibss network we are called 983 * with a SCAN -> RUN transition on scan complete. 984 * Use that to call iwi_auth_and_assoc. On completing 985 * the join we are then called again with an 986 * AUTH -> RUN transition and we want to do nothing. 987 * This is all totally bogus and needs to be redone. 988 */ 989 iwi_auth_and_assoc(sc, vap); 990 } else if (vap->iv_opmode == IEEE80211_M_MONITOR) 991 ieee80211_runtask(ic, &sc->sc_monitortask); 992 break; 993 case IEEE80211_S_ASSOC: 994 /* 995 * If we are transitioning from AUTH then just wait 996 * for the ASSOC status to come back from the firmware. 997 * Otherwise we need to issue the association request. 998 */ 999 if (vap->iv_state == IEEE80211_S_AUTH) 1000 break; 1001 iwi_auth_and_assoc(sc, vap); 1002 break; 1003 default: 1004 break; 1005 } 1006 IWI_UNLOCK(sc); 1007 IEEE80211_LOCK(ic); 1008 return ivp->iwi_newstate(vap, nstate, arg); 1009 } 1010 1011 /* 1012 * WME parameters coming from IEEE 802.11e specification. These values are 1013 * already declared in ieee80211_proto.c, but they are static so they can't 1014 * be reused here. 1015 */ 1016 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = { 1017 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */ 1018 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */ 1019 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */ 1020 { 0, 2, 3, 4, 102 } /* WME_AC_VO */ 1021 }; 1022 1023 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = { 1024 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */ 1025 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */ 1026 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */ 1027 { 0, 2, 2, 3, 47 } /* WME_AC_VO */ 1028 }; 1029 #define IWI_EXP2(v) htole16((1 << (v)) - 1) 1030 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 1031 1032 static void 1033 iwi_wme_init(struct iwi_softc *sc) 1034 { 1035 const struct wmeParams *wmep; 1036 int ac; 1037 1038 memset(sc->wme, 0, sizeof sc->wme); 1039 for (ac = 0; ac < WME_NUM_AC; ac++) { 1040 /* set WME values for CCK modulation */ 1041 wmep = &iwi_wme_cck_params[ac]; 1042 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn; 1043 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1044 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1045 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1046 sc->wme[1].acm[ac] = wmep->wmep_acm; 1047 1048 /* set WME values for OFDM modulation */ 1049 wmep = &iwi_wme_ofdm_params[ac]; 1050 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn; 1051 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1052 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1053 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1054 sc->wme[2].acm[ac] = wmep->wmep_acm; 1055 } 1056 } 1057 1058 static int 1059 iwi_wme_setparams(struct iwi_softc *sc, struct ieee80211com *ic) 1060 { 1061 const struct wmeParams *wmep; 1062 int ac; 1063 1064 for (ac = 0; ac < WME_NUM_AC; ac++) { 1065 /* set WME values for current operating mode */ 1066 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 1067 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn; 1068 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1069 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1070 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1071 sc->wme[0].acm[ac] = wmep->wmep_acm; 1072 } 1073 1074 DPRINTF(("Setting WME parameters\n")); 1075 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme); 1076 } 1077 #undef IWI_USEC 1078 #undef IWI_EXP2 1079 1080 static void 1081 iwi_update_wme(void *arg, int npending) 1082 { 1083 struct ieee80211com *ic = arg; 1084 struct iwi_softc *sc = ic->ic_ifp->if_softc; 1085 IWI_LOCK_DECL; 1086 1087 IWI_LOCK(sc); 1088 (void) iwi_wme_setparams(sc, ic); 1089 IWI_UNLOCK(sc); 1090 } 1091 1092 static int 1093 iwi_wme_update(struct ieee80211com *ic) 1094 { 1095 struct iwi_softc *sc = ic->ic_ifp->if_softc; 1096 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1097 1098 /* 1099 * We may be called to update the WME parameters in 1100 * the adapter at various places. If we're already 1101 * associated then initiate the request immediately; 1102 * otherwise we assume the params will get sent down 1103 * to the adapter as part of the work iwi_auth_and_assoc 1104 * does. 1105 */ 1106 if (vap->iv_state == IEEE80211_S_RUN) 1107 ieee80211_runtask(ic, &sc->sc_wmetask); 1108 return (0); 1109 } 1110 1111 static int 1112 iwi_wme_setie(struct iwi_softc *sc) 1113 { 1114 struct ieee80211_wme_info wme; 1115 1116 memset(&wme, 0, sizeof wme); 1117 wme.wme_id = IEEE80211_ELEMID_VENDOR; 1118 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2; 1119 wme.wme_oui[0] = 0x00; 1120 wme.wme_oui[1] = 0x50; 1121 wme.wme_oui[2] = 0xf2; 1122 wme.wme_type = WME_OUI_TYPE; 1123 wme.wme_subtype = WME_INFO_OUI_SUBTYPE; 1124 wme.wme_version = WME_VERSION; 1125 wme.wme_info = 0; 1126 1127 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len)); 1128 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme); 1129 } 1130 1131 /* 1132 * Read 16 bits at address 'addr' from the serial EEPROM. 1133 */ 1134 static uint16_t 1135 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr) 1136 { 1137 uint32_t tmp; 1138 uint16_t val; 1139 int n; 1140 1141 /* clock C once before the first command */ 1142 IWI_EEPROM_CTL(sc, 0); 1143 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1144 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1145 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1146 1147 /* write start bit (1) */ 1148 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1149 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1150 1151 /* write READ opcode (10) */ 1152 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1153 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1154 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1155 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1156 1157 /* write address A7-A0 */ 1158 for (n = 7; n >= 0; n--) { 1159 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1160 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D)); 1161 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1162 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C); 1163 } 1164 1165 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1166 1167 /* read data Q15-Q0 */ 1168 val = 0; 1169 for (n = 15; n >= 0; n--) { 1170 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1171 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1172 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL); 1173 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n; 1174 } 1175 1176 IWI_EEPROM_CTL(sc, 0); 1177 1178 /* clear Chip Select and clock C */ 1179 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1180 IWI_EEPROM_CTL(sc, 0); 1181 IWI_EEPROM_CTL(sc, IWI_EEPROM_C); 1182 1183 return val; 1184 } 1185 1186 static void 1187 iwi_setcurchan(struct iwi_softc *sc, int chan) 1188 { 1189 struct ifnet *ifp = sc->sc_ifp; 1190 struct ieee80211com *ic = ifp->if_l2com; 1191 1192 sc->curchan = chan; 1193 ieee80211_radiotap_chan_change(ic); 1194 } 1195 1196 static void 1197 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i, 1198 struct iwi_frame *frame) 1199 { 1200 struct ifnet *ifp = sc->sc_ifp; 1201 struct ieee80211com *ic = ifp->if_l2com; 1202 struct mbuf *mnew, *m; 1203 struct ieee80211_node *ni; 1204 int type, error, framelen; 1205 int8_t rssi, nf; 1206 IWI_LOCK_DECL; 1207 1208 framelen = le16toh(frame->len); 1209 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) { 1210 /* 1211 * XXX >MCLBYTES is bogus as it means the h/w dma'd 1212 * out of bounds; need to figure out how to limit 1213 * frame size in the firmware 1214 */ 1215 /* XXX stat */ 1216 DPRINTFN(1, 1217 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n", 1218 le16toh(frame->len), frame->chan, frame->rssi, 1219 frame->rssi_dbm)); 1220 return; 1221 } 1222 1223 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n", 1224 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm)); 1225 1226 if (frame->chan != sc->curchan) 1227 iwi_setcurchan(sc, frame->chan); 1228 1229 /* 1230 * Try to allocate a new mbuf for this ring element and load it before 1231 * processing the current mbuf. If the ring element cannot be loaded, 1232 * drop the received packet and reuse the old mbuf. In the unlikely 1233 * case that the old mbuf can't be reloaded either, explicitly panic. 1234 */ 1235 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1236 if (mnew == NULL) { 1237 ifp->if_ierrors++; 1238 return; 1239 } 1240 1241 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1242 1243 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1244 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr, 1245 0); 1246 if (error != 0) { 1247 m_freem(mnew); 1248 1249 /* try to reload the old mbuf */ 1250 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1251 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, 1252 &data->physaddr, 0); 1253 if (error != 0) { 1254 /* very unlikely that it will fail... */ 1255 panic("%s: could not load old rx mbuf", 1256 device_get_name(sc->sc_dev)); 1257 } 1258 ifp->if_ierrors++; 1259 return; 1260 } 1261 1262 /* 1263 * New mbuf successfully loaded, update Rx ring and continue 1264 * processing. 1265 */ 1266 m = data->m; 1267 data->m = mnew; 1268 CSR_WRITE_4(sc, data->reg, data->physaddr); 1269 1270 /* finalize mbuf */ 1271 m->m_pkthdr.rcvif = ifp; 1272 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) + 1273 sizeof (struct iwi_frame) + framelen; 1274 1275 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame)); 1276 1277 rssi = frame->rssi_dbm; 1278 nf = -95; 1279 if (ieee80211_radiotap_active(ic)) { 1280 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap; 1281 1282 tap->wr_flags = 0; 1283 tap->wr_antsignal = rssi; 1284 tap->wr_antnoise = nf; 1285 tap->wr_rate = iwi_cvtrate(frame->rate); 1286 tap->wr_antenna = frame->antenna; 1287 } 1288 IWI_UNLOCK(sc); 1289 1290 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1291 if (ni != NULL) { 1292 type = ieee80211_input(ni, m, rssi, nf); 1293 ieee80211_free_node(ni); 1294 } else 1295 type = ieee80211_input_all(ic, m, rssi, nf); 1296 1297 IWI_LOCK(sc); 1298 if (sc->sc_softled) { 1299 /* 1300 * Blink for any data frame. Otherwise do a 1301 * heartbeat-style blink when idle. The latter 1302 * is mainly for station mode where we depend on 1303 * periodic beacon frames to trigger the poll event. 1304 */ 1305 if (type == IEEE80211_FC0_TYPE_DATA) { 1306 sc->sc_rxrate = frame->rate; 1307 iwi_led_event(sc, IWI_LED_RX); 1308 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle) 1309 iwi_led_event(sc, IWI_LED_POLL); 1310 } 1311 } 1312 1313 /* 1314 * Check for an association response frame to see if QoS 1315 * has been negotiated. We parse just enough to figure 1316 * out if we're supposed to use QoS. The proper solution 1317 * is to pass the frame up so ieee80211_input can do the 1318 * work but that's made hard by how things currently are 1319 * done in the driver. 1320 */ 1321 static void 1322 iwi_checkforqos(struct ieee80211vap *vap, 1323 const struct ieee80211_frame *wh, int len) 1324 { 1325 #define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) 1326 const uint8_t *frm, *efrm, *wme; 1327 struct ieee80211_node *ni; 1328 uint16_t capinfo, status, associd; 1329 1330 /* NB: +8 for capinfo, status, associd, and first ie */ 1331 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) || 1332 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP) 1333 return; 1334 /* 1335 * asresp frame format 1336 * [2] capability information 1337 * [2] status 1338 * [2] association ID 1339 * [tlv] supported rates 1340 * [tlv] extended supported rates 1341 * [tlv] WME 1342 */ 1343 frm = (const uint8_t *)&wh[1]; 1344 efrm = ((const uint8_t *) wh) + len; 1345 1346 capinfo = le16toh(*(const uint16_t *)frm); 1347 frm += 2; 1348 status = le16toh(*(const uint16_t *)frm); 1349 frm += 2; 1350 associd = le16toh(*(const uint16_t *)frm); 1351 frm += 2; 1352 1353 wme = NULL; 1354 while (efrm - frm > 1) { 1355 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return); 1356 switch (*frm) { 1357 case IEEE80211_ELEMID_VENDOR: 1358 if (iswmeoui(frm)) 1359 wme = frm; 1360 break; 1361 } 1362 frm += frm[1] + 2; 1363 } 1364 1365 ni = ieee80211_ref_node(vap->iv_bss); 1366 ni->ni_capinfo = capinfo; 1367 ni->ni_associd = associd & 0x3fff; 1368 if (wme != NULL) 1369 ni->ni_flags |= IEEE80211_NODE_QOS; 1370 else 1371 ni->ni_flags &= ~IEEE80211_NODE_QOS; 1372 ieee80211_free_node(ni); 1373 #undef SUBTYPE 1374 } 1375 1376 /* 1377 * Task queue callbacks for iwi_notification_intr used to avoid LOR's. 1378 */ 1379 1380 static void 1381 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif) 1382 { 1383 struct ifnet *ifp = sc->sc_ifp; 1384 struct ieee80211com *ic = ifp->if_l2com; 1385 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1386 struct iwi_notif_scan_channel *chan; 1387 struct iwi_notif_scan_complete *scan; 1388 struct iwi_notif_authentication *auth; 1389 struct iwi_notif_association *assoc; 1390 struct iwi_notif_beacon_state *beacon; 1391 1392 switch (notif->type) { 1393 case IWI_NOTIF_TYPE_SCAN_CHANNEL: 1394 chan = (struct iwi_notif_scan_channel *)(notif + 1); 1395 1396 DPRINTFN(3, ("Scan of channel %u complete (%u)\n", 1397 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan)); 1398 1399 /* Reset the timer, the scan is still going */ 1400 sc->sc_state_timer = 3; 1401 break; 1402 1403 case IWI_NOTIF_TYPE_SCAN_COMPLETE: 1404 scan = (struct iwi_notif_scan_complete *)(notif + 1); 1405 1406 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan, 1407 scan->status)); 1408 1409 IWI_STATE_END(sc, IWI_FW_SCANNING); 1410 1411 /* 1412 * Monitor mode works by doing a passive scan to set 1413 * the channel and enable rx. Because we don't want 1414 * to abort a scan lest the firmware crash we scan 1415 * for a short period of time and automatically restart 1416 * the scan when notified the sweep has completed. 1417 */ 1418 if (vap->iv_opmode == IEEE80211_M_MONITOR) { 1419 ieee80211_runtask(ic, &sc->sc_monitortask); 1420 break; 1421 } 1422 1423 if (scan->status == IWI_SCAN_COMPLETED) { 1424 /* NB: don't need to defer, net80211 does it for us */ 1425 ieee80211_scan_next(vap); 1426 } 1427 break; 1428 1429 case IWI_NOTIF_TYPE_AUTHENTICATION: 1430 auth = (struct iwi_notif_authentication *)(notif + 1); 1431 switch (auth->state) { 1432 case IWI_AUTH_SUCCESS: 1433 DPRINTFN(2, ("Authentication succeeeded\n")); 1434 ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1); 1435 break; 1436 case IWI_AUTH_FAIL: 1437 /* 1438 * These are delivered as an unsolicited deauth 1439 * (e.g. due to inactivity) or in response to an 1440 * associate request. 1441 */ 1442 sc->flags &= ~IWI_FLAG_ASSOCIATED; 1443 if (vap->iv_state != IEEE80211_S_RUN) { 1444 DPRINTFN(2, ("Authentication failed\n")); 1445 vap->iv_stats.is_rx_auth_fail++; 1446 IWI_STATE_END(sc, IWI_FW_ASSOCIATING); 1447 } else { 1448 DPRINTFN(2, ("Deauthenticated\n")); 1449 vap->iv_stats.is_rx_deauth++; 1450 } 1451 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1452 break; 1453 case IWI_AUTH_SENT_1: 1454 case IWI_AUTH_RECV_2: 1455 case IWI_AUTH_SEQ1_PASS: 1456 break; 1457 case IWI_AUTH_SEQ1_FAIL: 1458 DPRINTFN(2, ("Initial authentication handshake failed; " 1459 "you probably need shared key\n")); 1460 vap->iv_stats.is_rx_auth_fail++; 1461 IWI_STATE_END(sc, IWI_FW_ASSOCIATING); 1462 /* XXX retry shared key when in auto */ 1463 break; 1464 default: 1465 device_printf(sc->sc_dev, 1466 "unknown authentication state %u\n", auth->state); 1467 break; 1468 } 1469 break; 1470 1471 case IWI_NOTIF_TYPE_ASSOCIATION: 1472 assoc = (struct iwi_notif_association *)(notif + 1); 1473 switch (assoc->state) { 1474 case IWI_AUTH_SUCCESS: 1475 /* re-association, do nothing */ 1476 break; 1477 case IWI_ASSOC_SUCCESS: 1478 DPRINTFN(2, ("Association succeeded\n")); 1479 sc->flags |= IWI_FLAG_ASSOCIATED; 1480 IWI_STATE_END(sc, IWI_FW_ASSOCIATING); 1481 iwi_checkforqos(vap, 1482 (const struct ieee80211_frame *)(assoc+1), 1483 le16toh(notif->len) - sizeof(*assoc) - 1); 1484 ieee80211_new_state(vap, IEEE80211_S_RUN, -1); 1485 break; 1486 case IWI_ASSOC_INIT: 1487 sc->flags &= ~IWI_FLAG_ASSOCIATED; 1488 switch (sc->fw_state) { 1489 case IWI_FW_ASSOCIATING: 1490 DPRINTFN(2, ("Association failed\n")); 1491 IWI_STATE_END(sc, IWI_FW_ASSOCIATING); 1492 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1493 break; 1494 1495 case IWI_FW_DISASSOCIATING: 1496 DPRINTFN(2, ("Dissassociated\n")); 1497 IWI_STATE_END(sc, IWI_FW_DISASSOCIATING); 1498 vap->iv_stats.is_rx_disassoc++; 1499 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1500 break; 1501 } 1502 break; 1503 default: 1504 device_printf(sc->sc_dev, 1505 "unknown association state %u\n", assoc->state); 1506 break; 1507 } 1508 break; 1509 1510 case IWI_NOTIF_TYPE_BEACON: 1511 /* XXX check struct length */ 1512 beacon = (struct iwi_notif_beacon_state *)(notif + 1); 1513 1514 DPRINTFN(5, ("Beacon state (%u, %u)\n", 1515 beacon->state, le32toh(beacon->number))); 1516 1517 if (beacon->state == IWI_BEACON_MISS) { 1518 /* 1519 * The firmware notifies us of every beacon miss 1520 * so we need to track the count against the 1521 * configured threshold before notifying the 1522 * 802.11 layer. 1523 * XXX try to roam, drop assoc only on much higher count 1524 */ 1525 if (le32toh(beacon->number) >= vap->iv_bmissthreshold) { 1526 DPRINTF(("Beacon miss: %u >= %u\n", 1527 le32toh(beacon->number), 1528 vap->iv_bmissthreshold)); 1529 vap->iv_stats.is_beacon_miss++; 1530 /* 1531 * It's pointless to notify the 802.11 layer 1532 * as it'll try to send a probe request (which 1533 * we'll discard) and then timeout and drop us 1534 * into scan state. Instead tell the firmware 1535 * to disassociate and then on completion we'll 1536 * kick the state machine to scan. 1537 */ 1538 ieee80211_runtask(ic, &sc->sc_disassoctask); 1539 } 1540 } 1541 break; 1542 1543 case IWI_NOTIF_TYPE_CALIBRATION: 1544 case IWI_NOTIF_TYPE_NOISE: 1545 case IWI_NOTIF_TYPE_LINK_QUALITY: 1546 DPRINTFN(5, ("Notification (%u)\n", notif->type)); 1547 break; 1548 1549 default: 1550 DPRINTF(("unknown notification type %u flags 0x%x len %u\n", 1551 notif->type, notif->flags, le16toh(notif->len))); 1552 break; 1553 } 1554 } 1555 1556 static void 1557 iwi_rx_intr(struct iwi_softc *sc) 1558 { 1559 struct iwi_rx_data *data; 1560 struct iwi_hdr *hdr; 1561 uint32_t hw; 1562 1563 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX); 1564 1565 for (; sc->rxq.cur != hw;) { 1566 data = &sc->rxq.data[sc->rxq.cur]; 1567 1568 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1569 BUS_DMASYNC_POSTREAD); 1570 1571 hdr = mtod(data->m, struct iwi_hdr *); 1572 1573 switch (hdr->type) { 1574 case IWI_HDR_TYPE_FRAME: 1575 iwi_frame_intr(sc, data, sc->rxq.cur, 1576 (struct iwi_frame *)(hdr + 1)); 1577 break; 1578 1579 case IWI_HDR_TYPE_NOTIF: 1580 iwi_notification_intr(sc, 1581 (struct iwi_notif *)(hdr + 1)); 1582 break; 1583 1584 default: 1585 device_printf(sc->sc_dev, "unknown hdr type %u\n", 1586 hdr->type); 1587 } 1588 1589 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1590 1591 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT; 1592 } 1593 1594 /* tell the firmware what we have processed */ 1595 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1; 1596 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw); 1597 } 1598 1599 static void 1600 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq) 1601 { 1602 struct ifnet *ifp = sc->sc_ifp; 1603 struct iwi_tx_data *data; 1604 uint32_t hw; 1605 1606 hw = CSR_READ_4(sc, txq->csr_ridx); 1607 1608 for (; txq->next != hw;) { 1609 data = &txq->data[txq->next]; 1610 1611 bus_dmamap_sync(txq->data_dmat, data->map, 1612 BUS_DMASYNC_POSTWRITE); 1613 bus_dmamap_unload(txq->data_dmat, data->map); 1614 if (data->m->m_flags & M_TXCB) 1615 ieee80211_process_callback(data->ni, data->m, 0/*XXX*/); 1616 m_freem(data->m); 1617 data->m = NULL; 1618 ieee80211_free_node(data->ni); 1619 data->ni = NULL; 1620 1621 DPRINTFN(15, ("tx done idx=%u\n", txq->next)); 1622 1623 ifp->if_opackets++; 1624 1625 txq->queued--; 1626 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT; 1627 } 1628 1629 sc->sc_tx_timer = 0; 1630 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1631 1632 if (sc->sc_softled) 1633 iwi_led_event(sc, IWI_LED_TX); 1634 1635 iwi_start_locked(ifp); 1636 } 1637 1638 static void 1639 iwi_fatal_error_intr(struct iwi_softc *sc) 1640 { 1641 struct ifnet *ifp = sc->sc_ifp; 1642 struct ieee80211com *ic = ifp->if_l2com; 1643 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1644 1645 device_printf(sc->sc_dev, "firmware error\n"); 1646 if (vap != NULL) 1647 ieee80211_cancel_scan(vap); 1648 ieee80211_runtask(ic, &sc->sc_restarttask); 1649 1650 sc->flags &= ~IWI_FLAG_BUSY; 1651 sc->sc_busy_timer = 0; 1652 wakeup(sc); 1653 } 1654 1655 static void 1656 iwi_radio_off_intr(struct iwi_softc *sc) 1657 { 1658 struct ifnet *ifp = sc->sc_ifp; 1659 struct ieee80211com *ic = ifp->if_l2com; 1660 1661 ieee80211_runtask(ic, &sc->sc_radiofftask); 1662 } 1663 1664 static void 1665 iwi_intr(void *arg) 1666 { 1667 struct iwi_softc *sc = arg; 1668 uint32_t r; 1669 IWI_LOCK_DECL; 1670 1671 IWI_LOCK(sc); 1672 1673 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) { 1674 IWI_UNLOCK(sc); 1675 return; 1676 } 1677 1678 /* acknowledge interrupts */ 1679 CSR_WRITE_4(sc, IWI_CSR_INTR, r); 1680 1681 if (r & IWI_INTR_FATAL_ERROR) { 1682 iwi_fatal_error_intr(sc); 1683 goto done; 1684 } 1685 1686 if (r & IWI_INTR_FW_INITED) { 1687 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR))) 1688 wakeup(sc); 1689 } 1690 1691 if (r & IWI_INTR_RADIO_OFF) 1692 iwi_radio_off_intr(sc); 1693 1694 if (r & IWI_INTR_CMD_DONE) { 1695 sc->flags &= ~IWI_FLAG_BUSY; 1696 sc->sc_busy_timer = 0; 1697 wakeup(sc); 1698 } 1699 1700 if (r & IWI_INTR_TX1_DONE) 1701 iwi_tx_intr(sc, &sc->txq[0]); 1702 1703 if (r & IWI_INTR_TX2_DONE) 1704 iwi_tx_intr(sc, &sc->txq[1]); 1705 1706 if (r & IWI_INTR_TX3_DONE) 1707 iwi_tx_intr(sc, &sc->txq[2]); 1708 1709 if (r & IWI_INTR_TX4_DONE) 1710 iwi_tx_intr(sc, &sc->txq[3]); 1711 1712 if (r & IWI_INTR_RX_DONE) 1713 iwi_rx_intr(sc); 1714 1715 if (r & IWI_INTR_PARITY_ERROR) { 1716 /* XXX rate-limit */ 1717 device_printf(sc->sc_dev, "parity error\n"); 1718 } 1719 done: 1720 IWI_UNLOCK(sc); 1721 } 1722 1723 static int 1724 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len) 1725 { 1726 struct iwi_cmd_desc *desc; 1727 1728 IWI_LOCK_ASSERT(sc); 1729 1730 if (sc->flags & IWI_FLAG_BUSY) { 1731 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n", 1732 __func__, type); 1733 return EAGAIN; 1734 } 1735 sc->flags |= IWI_FLAG_BUSY; 1736 sc->sc_busy_timer = 2; 1737 1738 desc = &sc->cmdq.desc[sc->cmdq.cur]; 1739 1740 desc->hdr.type = IWI_HDR_TYPE_COMMAND; 1741 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1742 desc->type = type; 1743 desc->len = len; 1744 memcpy(desc->data, data, len); 1745 1746 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map, 1747 BUS_DMASYNC_PREWRITE); 1748 1749 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur, 1750 type, len)); 1751 1752 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT; 1753 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 1754 1755 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz); 1756 } 1757 1758 static void 1759 iwi_write_ibssnode(struct iwi_softc *sc, 1760 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry) 1761 { 1762 struct iwi_ibssnode node; 1763 1764 /* write node information into NIC memory */ 1765 memset(&node, 0, sizeof node); 1766 IEEE80211_ADDR_COPY(node.bssid, addr); 1767 1768 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry)); 1769 1770 CSR_WRITE_REGION_1(sc, 1771 IWI_CSR_NODE_BASE + entry * sizeof node, 1772 (uint8_t *)&node, sizeof node); 1773 } 1774 1775 static int 1776 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni, 1777 int ac) 1778 { 1779 struct iwi_softc *sc = ifp->if_softc; 1780 struct ieee80211vap *vap = ni->ni_vap; 1781 struct ieee80211com *ic = ni->ni_ic; 1782 struct iwi_node *in = (struct iwi_node *)ni; 1783 const struct ieee80211_frame *wh; 1784 struct ieee80211_key *k; 1785 const struct chanAccParams *cap; 1786 struct iwi_tx_ring *txq = &sc->txq[ac]; 1787 struct iwi_tx_data *data; 1788 struct iwi_tx_desc *desc; 1789 struct mbuf *mnew; 1790 bus_dma_segment_t segs[IWI_MAX_NSEG]; 1791 int error, nsegs, hdrlen, i; 1792 int ismcast, flags, xflags, staid; 1793 1794 IWI_LOCK_ASSERT(sc); 1795 wh = mtod(m0, const struct ieee80211_frame *); 1796 /* NB: only data frames use this path */ 1797 hdrlen = ieee80211_hdrsize(wh); 1798 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 1799 flags = xflags = 0; 1800 1801 if (!ismcast) 1802 flags |= IWI_DATA_FLAG_NEED_ACK; 1803 if (vap->iv_flags & IEEE80211_F_SHPREAMBLE) 1804 flags |= IWI_DATA_FLAG_SHPREAMBLE; 1805 if (IEEE80211_QOS_HAS_SEQ(wh)) { 1806 xflags |= IWI_DATA_XFLAG_QOS; 1807 cap = &ic->ic_wme.wme_chanParams; 1808 if (!cap->cap_wmeParams[ac].wmep_noackPolicy) 1809 flags &= ~IWI_DATA_FLAG_NEED_ACK; 1810 } 1811 1812 /* 1813 * This is only used in IBSS mode where the firmware expect an index 1814 * in a h/w table instead of a destination address. 1815 */ 1816 if (vap->iv_opmode == IEEE80211_M_IBSS) { 1817 if (!ismcast) { 1818 if (in->in_station == -1) { 1819 in->in_station = alloc_unr(sc->sc_unr); 1820 if (in->in_station == -1) { 1821 /* h/w table is full */ 1822 m_freem(m0); 1823 ieee80211_free_node(ni); 1824 ifp->if_oerrors++; 1825 return 0; 1826 } 1827 iwi_write_ibssnode(sc, 1828 ni->ni_macaddr, in->in_station); 1829 } 1830 staid = in->in_station; 1831 } else { 1832 /* 1833 * Multicast addresses have no associated node 1834 * so there will be no station entry. We reserve 1835 * entry 0 for one mcast address and use that. 1836 * If there are many being used this will be 1837 * expensive and we'll need to do a better job 1838 * but for now this handles the broadcast case. 1839 */ 1840 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) { 1841 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1); 1842 iwi_write_ibssnode(sc, sc->sc_mcast, 0); 1843 } 1844 staid = 0; 1845 } 1846 } else 1847 staid = 0; 1848 1849 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1850 k = ieee80211_crypto_encap(ni, m0); 1851 if (k == NULL) { 1852 m_freem(m0); 1853 return ENOBUFS; 1854 } 1855 1856 /* packet header may have moved, reset our local pointer */ 1857 wh = mtod(m0, struct ieee80211_frame *); 1858 } 1859 1860 if (ieee80211_radiotap_active_vap(vap)) { 1861 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap; 1862 1863 tap->wt_flags = 0; 1864 1865 ieee80211_radiotap_tx(vap, m0); 1866 } 1867 1868 data = &txq->data[txq->cur]; 1869 desc = &txq->desc[txq->cur]; 1870 1871 /* save and trim IEEE802.11 header */ 1872 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh); 1873 m_adj(m0, hdrlen); 1874 1875 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs, 1876 &nsegs, 0); 1877 if (error != 0 && error != EFBIG) { 1878 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1879 error); 1880 m_freem(m0); 1881 return error; 1882 } 1883 if (error != 0) { 1884 mnew = m_defrag(m0, M_NOWAIT); 1885 if (mnew == NULL) { 1886 device_printf(sc->sc_dev, 1887 "could not defragment mbuf\n"); 1888 m_freem(m0); 1889 return ENOBUFS; 1890 } 1891 m0 = mnew; 1892 1893 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, 1894 m0, segs, &nsegs, 0); 1895 if (error != 0) { 1896 device_printf(sc->sc_dev, 1897 "could not map mbuf (error %d)\n", error); 1898 m_freem(m0); 1899 return error; 1900 } 1901 } 1902 1903 data->m = m0; 1904 data->ni = ni; 1905 1906 desc->hdr.type = IWI_HDR_TYPE_DATA; 1907 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1908 desc->station = staid; 1909 desc->cmd = IWI_DATA_CMD_TX; 1910 desc->len = htole16(m0->m_pkthdr.len); 1911 desc->flags = flags; 1912 desc->xflags = xflags; 1913 1914 #if 0 1915 if (vap->iv_flags & IEEE80211_F_PRIVACY) 1916 desc->wep_txkey = vap->iv_def_txkey; 1917 else 1918 #endif 1919 desc->flags |= IWI_DATA_FLAG_NO_WEP; 1920 1921 desc->nseg = htole32(nsegs); 1922 for (i = 0; i < nsegs; i++) { 1923 desc->seg_addr[i] = htole32(segs[i].ds_addr); 1924 desc->seg_len[i] = htole16(segs[i].ds_len); 1925 } 1926 1927 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1928 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE); 1929 1930 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n", 1931 ac, txq->cur, le16toh(desc->len), nsegs)); 1932 1933 txq->queued++; 1934 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT; 1935 CSR_WRITE_4(sc, txq->csr_widx, txq->cur); 1936 1937 return 0; 1938 } 1939 1940 static int 1941 iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 1942 const struct ieee80211_bpf_params *params) 1943 { 1944 /* no support; just discard */ 1945 m_freem(m); 1946 ieee80211_free_node(ni); 1947 return 0; 1948 } 1949 1950 static void 1951 iwi_start_locked(struct ifnet *ifp) 1952 { 1953 struct iwi_softc *sc = ifp->if_softc; 1954 struct mbuf *m; 1955 struct ieee80211_node *ni; 1956 int ac; 1957 1958 IWI_LOCK_ASSERT(sc); 1959 1960 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1961 return; 1962 1963 for (;;) { 1964 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1965 if (m == NULL) 1966 break; 1967 ac = M_WME_GETAC(m); 1968 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) { 1969 /* there is no place left in this ring; tail drop */ 1970 /* XXX tail drop */ 1971 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1972 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1973 break; 1974 } 1975 1976 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1977 if (iwi_tx_start(ifp, m, ni, ac) != 0) { 1978 ieee80211_free_node(ni); 1979 ifp->if_oerrors++; 1980 break; 1981 } 1982 1983 sc->sc_tx_timer = 5; 1984 } 1985 } 1986 1987 static void 1988 iwi_start(struct ifnet *ifp) 1989 { 1990 struct iwi_softc *sc = ifp->if_softc; 1991 IWI_LOCK_DECL; 1992 1993 IWI_LOCK(sc); 1994 iwi_start_locked(ifp); 1995 IWI_UNLOCK(sc); 1996 } 1997 1998 static void 1999 iwi_watchdog(void *arg) 2000 { 2001 struct iwi_softc *sc = arg; 2002 struct ifnet *ifp = sc->sc_ifp; 2003 struct ieee80211com *ic = ifp->if_l2com; 2004 2005 IWI_LOCK_ASSERT(sc); 2006 2007 if (sc->sc_tx_timer > 0) { 2008 if (--sc->sc_tx_timer == 0) { 2009 if_printf(ifp, "device timeout\n"); 2010 ifp->if_oerrors++; 2011 ieee80211_runtask(ic, &sc->sc_restarttask); 2012 } 2013 } 2014 if (sc->sc_state_timer > 0) { 2015 if (--sc->sc_state_timer == 0) { 2016 if_printf(ifp, "firmware stuck in state %d, resetting\n", 2017 sc->fw_state); 2018 if (sc->fw_state == IWI_FW_SCANNING) { 2019 struct ieee80211com *ic = ifp->if_l2com; 2020 ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps)); 2021 } 2022 ieee80211_runtask(ic, &sc->sc_restarttask); 2023 sc->sc_state_timer = 3; 2024 } 2025 } 2026 if (sc->sc_busy_timer > 0) { 2027 if (--sc->sc_busy_timer == 0) { 2028 if_printf(ifp, "firmware command timeout, resetting\n"); 2029 ieee80211_runtask(ic, &sc->sc_restarttask); 2030 } 2031 } 2032 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc); 2033 } 2034 2035 static int 2036 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2037 { 2038 struct iwi_softc *sc = ifp->if_softc; 2039 struct ieee80211com *ic = ifp->if_l2com; 2040 struct ifreq *ifr = (struct ifreq *) data; 2041 int error = 0, startall = 0; 2042 IWI_LOCK_DECL; 2043 2044 switch (cmd) { 2045 case SIOCSIFFLAGS: 2046 IWI_LOCK(sc); 2047 if (ifp->if_flags & IFF_UP) { 2048 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2049 iwi_init_locked(sc); 2050 startall = 1; 2051 } 2052 } else { 2053 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2054 iwi_stop_locked(sc); 2055 } 2056 IWI_UNLOCK(sc); 2057 if (startall) 2058 ieee80211_start_all(ic); 2059 break; 2060 case SIOCGIFMEDIA: 2061 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 2062 break; 2063 case SIOCGIFADDR: 2064 error = ether_ioctl(ifp, cmd, data); 2065 break; 2066 default: 2067 error = EINVAL; 2068 break; 2069 } 2070 return error; 2071 } 2072 2073 static void 2074 iwi_stop_master(struct iwi_softc *sc) 2075 { 2076 uint32_t tmp; 2077 int ntries; 2078 2079 /* disable interrupts */ 2080 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 2081 2082 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER); 2083 for (ntries = 0; ntries < 5; ntries++) { 2084 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 2085 break; 2086 DELAY(10); 2087 } 2088 if (ntries == 5) 2089 device_printf(sc->sc_dev, "timeout waiting for master\n"); 2090 2091 tmp = CSR_READ_4(sc, IWI_CSR_RST); 2092 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET); 2093 2094 sc->flags &= ~IWI_FLAG_FW_INITED; 2095 } 2096 2097 static int 2098 iwi_reset(struct iwi_softc *sc) 2099 { 2100 uint32_t tmp; 2101 int i, ntries; 2102 2103 iwi_stop_master(sc); 2104 2105 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 2106 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 2107 2108 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST); 2109 2110 /* wait for clock stabilization */ 2111 for (ntries = 0; ntries < 1000; ntries++) { 2112 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY) 2113 break; 2114 DELAY(200); 2115 } 2116 if (ntries == 1000) { 2117 device_printf(sc->sc_dev, 2118 "timeout waiting for clock stabilization\n"); 2119 return EIO; 2120 } 2121 2122 tmp = CSR_READ_4(sc, IWI_CSR_RST); 2123 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET); 2124 2125 DELAY(10); 2126 2127 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 2128 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 2129 2130 /* clear NIC memory */ 2131 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0); 2132 for (i = 0; i < 0xc000; i++) 2133 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 2134 2135 return 0; 2136 } 2137 2138 static const struct iwi_firmware_ohdr * 2139 iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw) 2140 { 2141 const struct firmware *fp = fw->fp; 2142 const struct iwi_firmware_ohdr *hdr; 2143 2144 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) { 2145 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name); 2146 return NULL; 2147 } 2148 hdr = (const struct iwi_firmware_ohdr *)fp->data; 2149 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) || 2150 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) { 2151 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n", 2152 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)), 2153 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR, 2154 IWI_FW_REQ_MINOR); 2155 return NULL; 2156 } 2157 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr); 2158 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr); 2159 fw->name = fp->name; 2160 return hdr; 2161 } 2162 2163 static const struct iwi_firmware_ohdr * 2164 iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw) 2165 { 2166 const struct iwi_firmware_ohdr *hdr; 2167 2168 hdr = iwi_setup_ofw(sc, fw); 2169 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) { 2170 device_printf(sc->sc_dev, "%s is not a ucode image\n", 2171 fw->name); 2172 hdr = NULL; 2173 } 2174 return hdr; 2175 } 2176 2177 static void 2178 iwi_getfw(struct iwi_fw *fw, const char *fwname, 2179 struct iwi_fw *uc, const char *ucname) 2180 { 2181 if (fw->fp == NULL) 2182 fw->fp = firmware_get(fwname); 2183 /* NB: pre-3.0 ucode is packaged separately */ 2184 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300) 2185 uc->fp = firmware_get(ucname); 2186 } 2187 2188 /* 2189 * Get the required firmware images if not already loaded. 2190 * Note that we hold firmware images so long as the device 2191 * is marked up in case we need to reload them on device init. 2192 * This is necessary because we re-init the device sometimes 2193 * from a context where we cannot read from the filesystem 2194 * (e.g. from the taskqueue thread when rfkill is re-enabled). 2195 * XXX return 0 on success, 1 on error. 2196 * 2197 * NB: the order of get'ing and put'ing images here is 2198 * intentional to support handling firmware images bundled 2199 * by operating mode and/or all together in one file with 2200 * the boot firmware as "master". 2201 */ 2202 static int 2203 iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode) 2204 { 2205 const struct iwi_firmware_hdr *hdr; 2206 const struct firmware *fp; 2207 2208 /* invalidate cached firmware on mode change */ 2209 if (sc->fw_mode != opmode) 2210 iwi_put_firmware(sc); 2211 2212 switch (opmode) { 2213 case IEEE80211_M_STA: 2214 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss"); 2215 break; 2216 case IEEE80211_M_IBSS: 2217 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss"); 2218 break; 2219 case IEEE80211_M_MONITOR: 2220 iwi_getfw(&sc->fw_fw, "iwi_monitor", 2221 &sc->fw_uc, "iwi_ucode_monitor"); 2222 break; 2223 default: 2224 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode); 2225 return EINVAL; 2226 } 2227 fp = sc->fw_fw.fp; 2228 if (fp == NULL) { 2229 device_printf(sc->sc_dev, "could not load firmware\n"); 2230 goto bad; 2231 } 2232 if (fp->version < 300) { 2233 /* 2234 * Firmware prior to 3.0 was packaged as separate 2235 * boot, firmware, and ucode images. Verify the 2236 * ucode image was read in, retrieve the boot image 2237 * if needed, and check version stamps for consistency. 2238 * The version stamps in the data are also checked 2239 * above; this is a bit paranoid but is a cheap 2240 * safeguard against mis-packaging. 2241 */ 2242 if (sc->fw_uc.fp == NULL) { 2243 device_printf(sc->sc_dev, "could not load ucode\n"); 2244 goto bad; 2245 } 2246 if (sc->fw_boot.fp == NULL) { 2247 sc->fw_boot.fp = firmware_get("iwi_boot"); 2248 if (sc->fw_boot.fp == NULL) { 2249 device_printf(sc->sc_dev, 2250 "could not load boot firmware\n"); 2251 goto bad; 2252 } 2253 } 2254 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version || 2255 sc->fw_boot.fp->version != sc->fw_uc.fp->version) { 2256 device_printf(sc->sc_dev, 2257 "firmware version mismatch: " 2258 "'%s' is %d, '%s' is %d, '%s' is %d\n", 2259 sc->fw_boot.fp->name, sc->fw_boot.fp->version, 2260 sc->fw_uc.fp->name, sc->fw_uc.fp->version, 2261 sc->fw_fw.fp->name, sc->fw_fw.fp->version 2262 ); 2263 goto bad; 2264 } 2265 /* 2266 * Check and setup each image. 2267 */ 2268 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL || 2269 iwi_setup_ofw(sc, &sc->fw_boot) == NULL || 2270 iwi_setup_ofw(sc, &sc->fw_fw) == NULL) 2271 goto bad; 2272 } else { 2273 /* 2274 * Check and setup combined image. 2275 */ 2276 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) { 2277 device_printf(sc->sc_dev, "image '%s' too small\n", 2278 fp->name); 2279 goto bad; 2280 } 2281 hdr = (const struct iwi_firmware_hdr *)fp->data; 2282 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize) 2283 + le32toh(hdr->fsize)) { 2284 device_printf(sc->sc_dev, "image '%s' too small (2)\n", 2285 fp->name); 2286 goto bad; 2287 } 2288 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr); 2289 sc->fw_boot.size = le32toh(hdr->bsize); 2290 sc->fw_boot.name = fp->name; 2291 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size; 2292 sc->fw_uc.size = le32toh(hdr->usize); 2293 sc->fw_uc.name = fp->name; 2294 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size; 2295 sc->fw_fw.size = le32toh(hdr->fsize); 2296 sc->fw_fw.name = fp->name; 2297 } 2298 #if 0 2299 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n", 2300 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size); 2301 #endif 2302 2303 sc->fw_mode = opmode; 2304 return 0; 2305 bad: 2306 iwi_put_firmware(sc); 2307 return 1; 2308 } 2309 2310 static void 2311 iwi_put_fw(struct iwi_fw *fw) 2312 { 2313 if (fw->fp != NULL) { 2314 firmware_put(fw->fp, FIRMWARE_UNLOAD); 2315 fw->fp = NULL; 2316 } 2317 fw->data = NULL; 2318 fw->size = 0; 2319 fw->name = NULL; 2320 } 2321 2322 /* 2323 * Release any cached firmware images. 2324 */ 2325 static void 2326 iwi_put_firmware(struct iwi_softc *sc) 2327 { 2328 iwi_put_fw(&sc->fw_uc); 2329 iwi_put_fw(&sc->fw_fw); 2330 iwi_put_fw(&sc->fw_boot); 2331 } 2332 2333 static int 2334 iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw) 2335 { 2336 uint32_t tmp; 2337 const uint16_t *w; 2338 const char *uc = fw->data; 2339 size_t size = fw->size; 2340 int i, ntries, error; 2341 2342 IWI_LOCK_ASSERT(sc); 2343 error = 0; 2344 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 2345 IWI_RST_STOP_MASTER); 2346 for (ntries = 0; ntries < 5; ntries++) { 2347 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 2348 break; 2349 DELAY(10); 2350 } 2351 if (ntries == 5) { 2352 device_printf(sc->sc_dev, "timeout waiting for master\n"); 2353 error = EIO; 2354 goto fail; 2355 } 2356 2357 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 2358 DELAY(5000); 2359 2360 tmp = CSR_READ_4(sc, IWI_CSR_RST); 2361 tmp &= ~IWI_RST_PRINCETON_RESET; 2362 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 2363 2364 DELAY(5000); 2365 MEM_WRITE_4(sc, 0x3000e0, 0); 2366 DELAY(1000); 2367 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1); 2368 DELAY(1000); 2369 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0); 2370 DELAY(1000); 2371 MEM_WRITE_1(sc, 0x200000, 0x00); 2372 MEM_WRITE_1(sc, 0x200000, 0x40); 2373 DELAY(1000); 2374 2375 /* write microcode into adapter memory */ 2376 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2) 2377 MEM_WRITE_2(sc, 0x200010, htole16(*w)); 2378 2379 MEM_WRITE_1(sc, 0x200000, 0x00); 2380 MEM_WRITE_1(sc, 0x200000, 0x80); 2381 2382 /* wait until we get an answer */ 2383 for (ntries = 0; ntries < 100; ntries++) { 2384 if (MEM_READ_1(sc, 0x200000) & 1) 2385 break; 2386 DELAY(100); 2387 } 2388 if (ntries == 100) { 2389 device_printf(sc->sc_dev, 2390 "timeout waiting for ucode to initialize\n"); 2391 error = EIO; 2392 goto fail; 2393 } 2394 2395 /* read the answer or the firmware will not initialize properly */ 2396 for (i = 0; i < 7; i++) 2397 MEM_READ_4(sc, 0x200004); 2398 2399 MEM_WRITE_1(sc, 0x200000, 0x00); 2400 2401 fail: 2402 return error; 2403 } 2404 2405 /* macro to handle unaligned little endian data in firmware image */ 2406 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 2407 2408 static int 2409 iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw) 2410 { 2411 u_char *p, *end; 2412 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp; 2413 int ntries, error; 2414 2415 IWI_LOCK_ASSERT(sc); 2416 2417 /* copy firmware image to DMA memory */ 2418 memcpy(sc->fw_virtaddr, fw->data, fw->size); 2419 2420 /* make sure the adapter will get up-to-date values */ 2421 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE); 2422 2423 /* tell the adapter where the command blocks are stored */ 2424 MEM_WRITE_4(sc, 0x3000a0, 0x27000); 2425 2426 /* 2427 * Store command blocks into adapter's internal memory using register 2428 * indirections. The adapter will read the firmware image through DMA 2429 * using information stored in command blocks. 2430 */ 2431 src = sc->fw_physaddr; 2432 p = sc->fw_virtaddr; 2433 end = p + fw->size; 2434 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000); 2435 2436 while (p < end) { 2437 dst = GETLE32(p); p += 4; src += 4; 2438 len = GETLE32(p); p += 4; src += 4; 2439 p += len; 2440 2441 while (len > 0) { 2442 mlen = min(len, IWI_CB_MAXDATALEN); 2443 2444 ctl = IWI_CB_DEFAULT_CTL | mlen; 2445 sum = ctl ^ src ^ dst; 2446 2447 /* write a command block */ 2448 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl); 2449 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src); 2450 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst); 2451 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum); 2452 2453 src += mlen; 2454 dst += mlen; 2455 len -= mlen; 2456 } 2457 } 2458 2459 /* write a fictive final command block (sentinel) */ 2460 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR); 2461 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 2462 2463 tmp = CSR_READ_4(sc, IWI_CSR_RST); 2464 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER); 2465 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 2466 2467 /* tell the adapter to start processing command blocks */ 2468 MEM_WRITE_4(sc, 0x3000a4, 0x540100); 2469 2470 /* wait until the adapter reaches the sentinel */ 2471 for (ntries = 0; ntries < 400; ntries++) { 2472 if (MEM_READ_4(sc, 0x3000d0) >= sentinel) 2473 break; 2474 DELAY(100); 2475 } 2476 /* sync dma, just in case */ 2477 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE); 2478 if (ntries == 400) { 2479 device_printf(sc->sc_dev, 2480 "timeout processing command blocks for %s firmware\n", 2481 fw->name); 2482 return EIO; 2483 } 2484 2485 /* we're done with command blocks processing */ 2486 MEM_WRITE_4(sc, 0x3000a4, 0x540c00); 2487 2488 /* allow interrupts so we know when the firmware is ready */ 2489 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 2490 2491 /* tell the adapter to initialize the firmware */ 2492 CSR_WRITE_4(sc, IWI_CSR_RST, 0); 2493 2494 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 2495 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY); 2496 2497 /* wait at most one second for firmware initialization to complete */ 2498 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) { 2499 device_printf(sc->sc_dev, "timeout waiting for %s firmware " 2500 "initialization to complete\n", fw->name); 2501 } 2502 2503 return error; 2504 } 2505 2506 static int 2507 iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap) 2508 { 2509 uint32_t data; 2510 2511 if (vap->iv_flags & IEEE80211_F_PMGTON) { 2512 /* XXX set more fine-grained operation */ 2513 data = htole32(IWI_POWER_MODE_MAX); 2514 } else 2515 data = htole32(IWI_POWER_MODE_CAM); 2516 2517 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 2518 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data); 2519 } 2520 2521 static int 2522 iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap) 2523 { 2524 struct iwi_wep_key wepkey; 2525 struct ieee80211_key *wk; 2526 int error, i; 2527 2528 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2529 wk = &vap->iv_nw_keys[i]; 2530 2531 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY; 2532 wepkey.idx = i; 2533 wepkey.len = wk->wk_keylen; 2534 memset(wepkey.key, 0, sizeof wepkey.key); 2535 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2536 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx, 2537 wepkey.len)); 2538 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey, 2539 sizeof wepkey); 2540 if (error != 0) 2541 return error; 2542 } 2543 return 0; 2544 } 2545 2546 static int 2547 iwi_config(struct iwi_softc *sc) 2548 { 2549 struct ifnet *ifp = sc->sc_ifp; 2550 struct ieee80211com *ic = ifp->if_l2com; 2551 struct iwi_configuration config; 2552 struct iwi_rateset rs; 2553 struct iwi_txpower power; 2554 uint32_t data; 2555 int error, i; 2556 2557 IWI_LOCK_ASSERT(sc); 2558 2559 DPRINTF(("Setting MAC address to %6D\n", IF_LLADDR(ifp), ":")); 2560 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, IF_LLADDR(ifp), 2561 IEEE80211_ADDR_LEN); 2562 if (error != 0) 2563 return error; 2564 2565 memset(&config, 0, sizeof config); 2566 config.bluetooth_coexistence = sc->bluetooth; 2567 config.silence_threshold = 0x1e; 2568 config.antenna = sc->antenna; 2569 config.multicast_enabled = 1; 2570 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2571 config.disable_unicast_decryption = 1; 2572 config.disable_multicast_decryption = 1; 2573 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 2574 config.allow_invalid_frames = 1; 2575 config.allow_beacon_and_probe_resp = 1; 2576 config.allow_mgt = 1; 2577 } 2578 DPRINTF(("Configuring adapter\n")); 2579 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config); 2580 if (error != 0) 2581 return error; 2582 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2583 power.mode = IWI_MODE_11B; 2584 power.nchan = 11; 2585 for (i = 0; i < 11; i++) { 2586 power.chan[i].chan = i + 1; 2587 power.chan[i].power = IWI_TXPOWER_MAX; 2588 } 2589 DPRINTF(("Setting .11b channels tx power\n")); 2590 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power); 2591 if (error != 0) 2592 return error; 2593 2594 power.mode = IWI_MODE_11G; 2595 DPRINTF(("Setting .11g channels tx power\n")); 2596 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power); 2597 if (error != 0) 2598 return error; 2599 } 2600 2601 memset(&rs, 0, sizeof rs); 2602 rs.mode = IWI_MODE_11G; 2603 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2604 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates; 2605 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, 2606 rs.nrates); 2607 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates)); 2608 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs); 2609 if (error != 0) 2610 return error; 2611 2612 memset(&rs, 0, sizeof rs); 2613 rs.mode = IWI_MODE_11A; 2614 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2615 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates; 2616 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, 2617 rs.nrates); 2618 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates)); 2619 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs); 2620 if (error != 0) 2621 return error; 2622 2623 data = htole32(arc4random()); 2624 DPRINTF(("Setting initialization vector to %u\n", le32toh(data))); 2625 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data); 2626 if (error != 0) 2627 return error; 2628 2629 /* enable adapter */ 2630 DPRINTF(("Enabling adapter\n")); 2631 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0); 2632 } 2633 2634 static __inline void 2635 set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type) 2636 { 2637 uint8_t *st = &scan->scan_type[ix / 2]; 2638 if (ix % 2) 2639 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0); 2640 else 2641 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4); 2642 } 2643 2644 static int 2645 scan_type(const struct ieee80211_scan_state *ss, 2646 const struct ieee80211_channel *chan) 2647 { 2648 /* We can only set one essid for a directed scan */ 2649 if (ss->ss_nssid != 0) 2650 return IWI_SCAN_TYPE_BDIRECTED; 2651 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) && 2652 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) 2653 return IWI_SCAN_TYPE_BROADCAST; 2654 return IWI_SCAN_TYPE_PASSIVE; 2655 } 2656 2657 static __inline int 2658 scan_band(const struct ieee80211_channel *c) 2659 { 2660 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ; 2661 } 2662 2663 static void 2664 iwi_monitor_scan(void *arg, int npending) 2665 { 2666 struct iwi_softc *sc = arg; 2667 IWI_LOCK_DECL; 2668 2669 IWI_LOCK(sc); 2670 (void) iwi_scanchan(sc, 2000, 0); 2671 IWI_UNLOCK(sc); 2672 } 2673 2674 /* 2675 * Start a scan on the current channel or all channels. 2676 */ 2677 static int 2678 iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan) 2679 { 2680 struct ieee80211com *ic; 2681 struct ieee80211_channel *chan; 2682 struct ieee80211_scan_state *ss; 2683 struct iwi_scan_ext scan; 2684 int error = 0; 2685 2686 IWI_LOCK_ASSERT(sc); 2687 if (sc->fw_state == IWI_FW_SCANNING) { 2688 /* 2689 * This should not happen as we only trigger scan_next after 2690 * completion 2691 */ 2692 DPRINTF(("%s: called too early - still scanning\n", __func__)); 2693 return (EBUSY); 2694 } 2695 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING); 2696 2697 ic = sc->sc_ifp->if_l2com; 2698 ss = ic->ic_scan; 2699 2700 memset(&scan, 0, sizeof scan); 2701 scan.full_scan_index = htole32(++sc->sc_scangen); 2702 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell); 2703 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) { 2704 /* 2705 * Use very short dwell times for when we send probe request 2706 * frames. Without this bg scans hang. Ideally this should 2707 * be handled with early-termination as done by net80211 but 2708 * that's not feasible (aborting a scan is problematic). 2709 */ 2710 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30); 2711 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30); 2712 } else { 2713 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell); 2714 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell); 2715 } 2716 2717 /* We can only set one essid for a directed scan */ 2718 if (ss->ss_nssid != 0) { 2719 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid, 2720 ss->ss_ssid[0].len); 2721 if (error) 2722 return (error); 2723 } 2724 2725 if (allchan) { 2726 int i, next, band, b, bstart; 2727 /* 2728 * Convert scan list to run-length encoded channel list 2729 * the firmware requires (preserving the order setup by 2730 * net80211). The first entry in each run specifies the 2731 * band and the count of items in the run. 2732 */ 2733 next = 0; /* next open slot */ 2734 bstart = 0; /* NB: not needed, silence compiler */ 2735 band = -1; /* NB: impossible value */ 2736 KASSERT(ss->ss_last > 0, ("no channels")); 2737 for (i = 0; i < ss->ss_last; i++) { 2738 chan = ss->ss_chans[i]; 2739 b = scan_band(chan); 2740 if (b != band) { 2741 if (band != -1) 2742 scan.channels[bstart] = 2743 (next - bstart) | band; 2744 /* NB: this allocates a slot for the run-len */ 2745 band = b, bstart = next++; 2746 } 2747 if (next >= IWI_SCAN_CHANNELS) { 2748 DPRINTF(("truncating scan list\n")); 2749 break; 2750 } 2751 scan.channels[next] = ieee80211_chan2ieee(ic, chan); 2752 set_scan_type(&scan, next, scan_type(ss, chan)); 2753 next++; 2754 } 2755 scan.channels[bstart] = (next - bstart) | band; 2756 } else { 2757 /* Scan the current channel only */ 2758 chan = ic->ic_curchan; 2759 scan.channels[0] = 1 | scan_band(chan); 2760 scan.channels[1] = ieee80211_chan2ieee(ic, chan); 2761 set_scan_type(&scan, 1, scan_type(ss, chan)); 2762 } 2763 #ifdef IWI_DEBUG 2764 if (iwi_debug > 0) { 2765 static const char *scantype[8] = 2766 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" }; 2767 int i; 2768 printf("Scan request: index %u dwell %d/%d/%d\n" 2769 , le32toh(scan.full_scan_index) 2770 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE]) 2771 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST]) 2772 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED]) 2773 ); 2774 i = 0; 2775 do { 2776 int run = scan.channels[i]; 2777 if (run == 0) 2778 break; 2779 printf("Scan %d %s channels:", run & 0x3f, 2780 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz"); 2781 for (run &= 0x3f, i++; run > 0; run--, i++) { 2782 uint8_t type = scan.scan_type[i/2]; 2783 printf(" %u/%s", scan.channels[i], 2784 scantype[(i & 1 ? type : type>>4) & 7]); 2785 } 2786 printf("\n"); 2787 } while (i < IWI_SCAN_CHANNELS); 2788 } 2789 #endif 2790 2791 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan)); 2792 } 2793 2794 static int 2795 iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm) 2796 { 2797 struct iwi_sensitivity sens; 2798 2799 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm)); 2800 2801 memset(&sens, 0, sizeof sens); 2802 sens.rssi = htole16(rssi_dbm); 2803 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens); 2804 } 2805 2806 static int 2807 iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap) 2808 { 2809 struct ieee80211com *ic = vap->iv_ic; 2810 struct ifnet *ifp = vap->iv_ifp; 2811 struct ieee80211_node *ni; 2812 struct iwi_configuration config; 2813 struct iwi_associate *assoc = &sc->assoc; 2814 struct iwi_rateset rs; 2815 uint16_t capinfo; 2816 uint32_t data; 2817 int error, mode; 2818 2819 IWI_LOCK_ASSERT(sc); 2820 2821 ni = ieee80211_ref_node(vap->iv_bss); 2822 2823 if (sc->flags & IWI_FLAG_ASSOCIATED) { 2824 DPRINTF(("Already associated\n")); 2825 return (-1); 2826 } 2827 2828 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING); 2829 error = 0; 2830 mode = 0; 2831 2832 if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) 2833 mode = IWI_MODE_11A; 2834 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan)) 2835 mode = IWI_MODE_11G; 2836 if (IEEE80211_IS_CHAN_B(ic->ic_curchan)) 2837 mode = IWI_MODE_11B; 2838 2839 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2840 memset(&config, 0, sizeof config); 2841 config.bluetooth_coexistence = sc->bluetooth; 2842 config.antenna = sc->antenna; 2843 config.multicast_enabled = 1; 2844 if (mode == IWI_MODE_11G) 2845 config.use_protection = 1; 2846 config.answer_pbreq = 2847 (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2848 config.disable_unicast_decryption = 1; 2849 config.disable_multicast_decryption = 1; 2850 DPRINTF(("Configuring adapter\n")); 2851 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config); 2852 if (error != 0) 2853 goto done; 2854 } 2855 2856 #ifdef IWI_DEBUG 2857 if (iwi_debug > 0) { 2858 printf("Setting ESSID to "); 2859 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 2860 printf("\n"); 2861 } 2862 #endif 2863 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen); 2864 if (error != 0) 2865 goto done; 2866 2867 error = iwi_setpowermode(sc, vap); 2868 if (error != 0) 2869 goto done; 2870 2871 data = htole32(vap->iv_rtsthreshold); 2872 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 2873 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data); 2874 if (error != 0) 2875 goto done; 2876 2877 data = htole32(vap->iv_fragthreshold); 2878 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data))); 2879 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data); 2880 if (error != 0) 2881 goto done; 2882 2883 /* the rate set has already been "negotiated" */ 2884 memset(&rs, 0, sizeof rs); 2885 rs.mode = mode; 2886 rs.type = IWI_RATESET_TYPE_NEGOTIATED; 2887 rs.nrates = ni->ni_rates.rs_nrates; 2888 if (rs.nrates > IWI_RATESET_SIZE) { 2889 DPRINTF(("Truncating negotiated rate set from %u\n", 2890 rs.nrates)); 2891 rs.nrates = IWI_RATESET_SIZE; 2892 } 2893 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates); 2894 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates)); 2895 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs); 2896 if (error != 0) 2897 goto done; 2898 2899 memset(assoc, 0, sizeof *assoc); 2900 2901 if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) { 2902 /* NB: don't treat WME setup as failure */ 2903 if (iwi_wme_setparams(sc, ic) == 0 && iwi_wme_setie(sc) == 0) 2904 assoc->policy |= htole16(IWI_POLICY_WME); 2905 /* XXX complain on failure? */ 2906 } 2907 2908 if (vap->iv_appie_wpa != NULL) { 2909 struct ieee80211_appie *ie = vap->iv_appie_wpa; 2910 2911 DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len)); 2912 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len); 2913 if (error != 0) 2914 goto done; 2915 } 2916 2917 error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni)); 2918 if (error != 0) 2919 goto done; 2920 2921 assoc->mode = mode; 2922 assoc->chan = ic->ic_curchan->ic_ieee; 2923 /* 2924 * NB: do not arrange for shared key auth w/o privacy 2925 * (i.e. a wep key); it causes a firmware error. 2926 */ 2927 if ((vap->iv_flags & IEEE80211_F_PRIVACY) && 2928 ni->ni_authmode == IEEE80211_AUTH_SHARED) { 2929 assoc->auth = IWI_AUTH_SHARED; 2930 /* 2931 * It's possible to have privacy marked but no default 2932 * key setup. This typically is due to a user app bug 2933 * but if we blindly grab the key the firmware will 2934 * barf so avoid it for now. 2935 */ 2936 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) 2937 assoc->auth |= vap->iv_def_txkey << 4; 2938 2939 error = iwi_setwepkeys(sc, vap); 2940 if (error != 0) 2941 goto done; 2942 } 2943 if (vap->iv_flags & IEEE80211_F_WPA) 2944 assoc->policy |= htole16(IWI_POLICY_WPA); 2945 if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0) 2946 assoc->type = IWI_HC_IBSS_START; 2947 else 2948 assoc->type = IWI_HC_ASSOC; 2949 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8); 2950 2951 if (vap->iv_opmode == IEEE80211_M_IBSS) 2952 capinfo = IEEE80211_CAPINFO_IBSS; 2953 else 2954 capinfo = IEEE80211_CAPINFO_ESS; 2955 if (vap->iv_flags & IEEE80211_F_PRIVACY) 2956 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2957 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2958 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 2959 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2960 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME) 2961 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2962 assoc->capinfo = htole16(capinfo); 2963 2964 assoc->lintval = htole16(ic->ic_lintval); 2965 assoc->intval = htole16(ni->ni_intval); 2966 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid); 2967 if (vap->iv_opmode == IEEE80211_M_IBSS) 2968 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr); 2969 else 2970 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid); 2971 2972 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x " 2973 "auth %u capinfo 0x%x lintval %u bintval %u\n", 2974 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join", 2975 assoc->bssid, ":", assoc->dst, ":", 2976 assoc->chan, le16toh(assoc->policy), assoc->auth, 2977 le16toh(assoc->capinfo), le16toh(assoc->lintval), 2978 le16toh(assoc->intval))); 2979 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc); 2980 done: 2981 ieee80211_free_node(ni); 2982 if (error) 2983 IWI_STATE_END(sc, IWI_FW_ASSOCIATING); 2984 2985 return (error); 2986 } 2987 2988 static void 2989 iwi_disassoc(void *arg, int pending) 2990 { 2991 struct iwi_softc *sc = arg; 2992 IWI_LOCK_DECL; 2993 2994 IWI_LOCK(sc); 2995 iwi_disassociate(sc, 0); 2996 IWI_UNLOCK(sc); 2997 } 2998 2999 static int 3000 iwi_disassociate(struct iwi_softc *sc, int quiet) 3001 { 3002 struct iwi_associate *assoc = &sc->assoc; 3003 3004 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) { 3005 DPRINTF(("Not associated\n")); 3006 return (-1); 3007 } 3008 3009 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING); 3010 3011 if (quiet) 3012 assoc->type = IWI_HC_DISASSOC_QUIET; 3013 else 3014 assoc->type = IWI_HC_DISASSOC; 3015 3016 DPRINTF(("Trying to disassociate from %6D channel %u\n", 3017 assoc->bssid, ":", assoc->chan)); 3018 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc); 3019 } 3020 3021 /* 3022 * release dma resources for the firmware 3023 */ 3024 static void 3025 iwi_release_fw_dma(struct iwi_softc *sc) 3026 { 3027 if (sc->fw_flags & IWI_FW_HAVE_PHY) 3028 bus_dmamap_unload(sc->fw_dmat, sc->fw_map); 3029 if (sc->fw_flags & IWI_FW_HAVE_MAP) 3030 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map); 3031 if (sc->fw_flags & IWI_FW_HAVE_DMAT) 3032 bus_dma_tag_destroy(sc->fw_dmat); 3033 3034 sc->fw_flags = 0; 3035 sc->fw_dma_size = 0; 3036 sc->fw_dmat = NULL; 3037 sc->fw_map = NULL; 3038 sc->fw_physaddr = 0; 3039 sc->fw_virtaddr = NULL; 3040 } 3041 3042 /* 3043 * allocate the dma descriptor for the firmware. 3044 * Return 0 on success, 1 on error. 3045 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING. 3046 */ 3047 static int 3048 iwi_init_fw_dma(struct iwi_softc *sc, int size) 3049 { 3050 if (sc->fw_dma_size >= size) 3051 return 0; 3052 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 3053 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 3054 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) { 3055 device_printf(sc->sc_dev, 3056 "could not create firmware DMA tag\n"); 3057 goto error; 3058 } 3059 sc->fw_flags |= IWI_FW_HAVE_DMAT; 3060 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0, 3061 &sc->fw_map) != 0) { 3062 device_printf(sc->sc_dev, 3063 "could not allocate firmware DMA memory\n"); 3064 goto error; 3065 } 3066 sc->fw_flags |= IWI_FW_HAVE_MAP; 3067 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr, 3068 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) { 3069 device_printf(sc->sc_dev, "could not load firmware DMA map\n"); 3070 goto error; 3071 } 3072 sc->fw_flags |= IWI_FW_HAVE_PHY; 3073 sc->fw_dma_size = size; 3074 return 0; 3075 3076 error: 3077 iwi_release_fw_dma(sc); 3078 return 1; 3079 } 3080 3081 static void 3082 iwi_init_locked(struct iwi_softc *sc) 3083 { 3084 struct ifnet *ifp = sc->sc_ifp; 3085 struct iwi_rx_data *data; 3086 int i; 3087 3088 IWI_LOCK_ASSERT(sc); 3089 3090 if (sc->fw_state == IWI_FW_LOADING) { 3091 device_printf(sc->sc_dev, "%s: already loading\n", __func__); 3092 return; /* XXX: condvar? */ 3093 } 3094 3095 iwi_stop_locked(sc); 3096 3097 IWI_STATE_BEGIN(sc, IWI_FW_LOADING); 3098 3099 if (iwi_reset(sc) != 0) { 3100 device_printf(sc->sc_dev, "could not reset adapter\n"); 3101 goto fail; 3102 } 3103 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) { 3104 device_printf(sc->sc_dev, 3105 "could not load boot firmware %s\n", sc->fw_boot.name); 3106 goto fail; 3107 } 3108 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) { 3109 device_printf(sc->sc_dev, 3110 "could not load microcode %s\n", sc->fw_uc.name); 3111 goto fail; 3112 } 3113 3114 iwi_stop_master(sc); 3115 3116 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr); 3117 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count); 3118 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 3119 3120 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr); 3121 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count); 3122 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur); 3123 3124 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr); 3125 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count); 3126 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur); 3127 3128 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr); 3129 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count); 3130 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur); 3131 3132 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr); 3133 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count); 3134 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur); 3135 3136 for (i = 0; i < sc->rxq.count; i++) { 3137 data = &sc->rxq.data[i]; 3138 CSR_WRITE_4(sc, data->reg, data->physaddr); 3139 } 3140 3141 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1); 3142 3143 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) { 3144 device_printf(sc->sc_dev, 3145 "could not load main firmware %s\n", sc->fw_fw.name); 3146 goto fail; 3147 } 3148 sc->flags |= IWI_FLAG_FW_INITED; 3149 3150 IWI_STATE_END(sc, IWI_FW_LOADING); 3151 3152 if (iwi_config(sc) != 0) { 3153 device_printf(sc->sc_dev, "unable to enable adapter\n"); 3154 goto fail2; 3155 } 3156 3157 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc); 3158 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3159 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3160 return; 3161 fail: 3162 IWI_STATE_END(sc, IWI_FW_LOADING); 3163 fail2: 3164 iwi_stop_locked(sc); 3165 } 3166 3167 static void 3168 iwi_init(void *priv) 3169 { 3170 struct iwi_softc *sc = priv; 3171 struct ifnet *ifp = sc->sc_ifp; 3172 struct ieee80211com *ic = ifp->if_l2com; 3173 IWI_LOCK_DECL; 3174 3175 IWI_LOCK(sc); 3176 iwi_init_locked(sc); 3177 IWI_UNLOCK(sc); 3178 3179 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 3180 ieee80211_start_all(ic); 3181 } 3182 3183 static void 3184 iwi_stop_locked(void *priv) 3185 { 3186 struct iwi_softc *sc = priv; 3187 struct ifnet *ifp = sc->sc_ifp; 3188 3189 IWI_LOCK_ASSERT(sc); 3190 3191 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3192 3193 if (sc->sc_softled) { 3194 callout_stop(&sc->sc_ledtimer); 3195 sc->sc_blinking = 0; 3196 } 3197 callout_stop(&sc->sc_wdtimer); 3198 callout_stop(&sc->sc_rftimer); 3199 3200 iwi_stop_master(sc); 3201 3202 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET); 3203 3204 /* reset rings */ 3205 iwi_reset_cmd_ring(sc, &sc->cmdq); 3206 iwi_reset_tx_ring(sc, &sc->txq[0]); 3207 iwi_reset_tx_ring(sc, &sc->txq[1]); 3208 iwi_reset_tx_ring(sc, &sc->txq[2]); 3209 iwi_reset_tx_ring(sc, &sc->txq[3]); 3210 iwi_reset_rx_ring(sc, &sc->rxq); 3211 3212 sc->sc_tx_timer = 0; 3213 sc->sc_state_timer = 0; 3214 sc->sc_busy_timer = 0; 3215 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED); 3216 sc->fw_state = IWI_FW_IDLE; 3217 wakeup(sc); 3218 } 3219 3220 static void 3221 iwi_stop(struct iwi_softc *sc) 3222 { 3223 IWI_LOCK_DECL; 3224 3225 IWI_LOCK(sc); 3226 iwi_stop_locked(sc); 3227 IWI_UNLOCK(sc); 3228 } 3229 3230 static void 3231 iwi_restart(void *arg, int npending) 3232 { 3233 struct iwi_softc *sc = arg; 3234 3235 iwi_init(sc); 3236 } 3237 3238 /* 3239 * Return whether or not the radio is enabled in hardware 3240 * (i.e. the rfkill switch is "off"). 3241 */ 3242 static int 3243 iwi_getrfkill(struct iwi_softc *sc) 3244 { 3245 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0; 3246 } 3247 3248 static void 3249 iwi_radio_on(void *arg, int pending) 3250 { 3251 struct iwi_softc *sc = arg; 3252 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3253 3254 device_printf(sc->sc_dev, "radio turned on\n"); 3255 3256 iwi_init(sc); 3257 ieee80211_notify_radio(ic, 1); 3258 } 3259 3260 static void 3261 iwi_rfkill_poll(void *arg) 3262 { 3263 struct iwi_softc *sc = arg; 3264 3265 IWI_LOCK_ASSERT(sc); 3266 3267 /* 3268 * Check for a change in rfkill state. We get an 3269 * interrupt when a radio is disabled but not when 3270 * it is enabled so we must poll for the latter. 3271 */ 3272 if (!iwi_getrfkill(sc)) { 3273 struct ifnet *ifp = sc->sc_ifp; 3274 struct ieee80211com *ic = ifp->if_l2com; 3275 3276 ieee80211_runtask(ic, &sc->sc_radiontask); 3277 return; 3278 } 3279 callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc); 3280 } 3281 3282 static void 3283 iwi_radio_off(void *arg, int pending) 3284 { 3285 struct iwi_softc *sc = arg; 3286 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3287 IWI_LOCK_DECL; 3288 3289 device_printf(sc->sc_dev, "radio turned off\n"); 3290 3291 ieee80211_notify_radio(ic, 0); 3292 3293 IWI_LOCK(sc); 3294 iwi_stop_locked(sc); 3295 iwi_rfkill_poll(sc); 3296 IWI_UNLOCK(sc); 3297 } 3298 3299 static int 3300 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS) 3301 { 3302 struct iwi_softc *sc = arg1; 3303 uint32_t size, buf[128]; 3304 3305 memset(buf, 0, sizeof buf); 3306 3307 if (!(sc->flags & IWI_FLAG_FW_INITED)) 3308 return SYSCTL_OUT(req, buf, sizeof buf); 3309 3310 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1); 3311 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size); 3312 3313 return SYSCTL_OUT(req, buf, size); 3314 } 3315 3316 static int 3317 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS) 3318 { 3319 struct iwi_softc *sc = arg1; 3320 int val = !iwi_getrfkill(sc); 3321 3322 return SYSCTL_OUT(req, &val, sizeof val); 3323 } 3324 3325 /* 3326 * Add sysctl knobs. 3327 */ 3328 static void 3329 iwi_sysctlattach(struct iwi_softc *sc) 3330 { 3331 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 3332 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 3333 3334 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio", 3335 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I", 3336 "radio transmitter switch state (0=off, 1=on)"); 3337 3338 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats", 3339 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S", 3340 "statistics"); 3341 3342 sc->bluetooth = 0; 3343 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth", 3344 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence"); 3345 3346 sc->antenna = IWI_ANTENNA_AUTO; 3347 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna", 3348 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)"); 3349 } 3350 3351 /* 3352 * LED support. 3353 * 3354 * Different cards have different capabilities. Some have three 3355 * led's while others have only one. The linux ipw driver defines 3356 * led's for link state (associated or not), band (11a, 11g, 11b), 3357 * and for link activity. We use one led and vary the blink rate 3358 * according to the tx/rx traffic a la the ath driver. 3359 */ 3360 3361 static __inline uint32_t 3362 iwi_toggle_event(uint32_t r) 3363 { 3364 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA | 3365 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA); 3366 } 3367 3368 static uint32_t 3369 iwi_read_event(struct iwi_softc *sc) 3370 { 3371 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT); 3372 } 3373 3374 static void 3375 iwi_write_event(struct iwi_softc *sc, uint32_t v) 3376 { 3377 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v); 3378 } 3379 3380 static void 3381 iwi_led_done(void *arg) 3382 { 3383 struct iwi_softc *sc = arg; 3384 3385 sc->sc_blinking = 0; 3386 } 3387 3388 /* 3389 * Turn the activity LED off: flip the pin and then set a timer so no 3390 * update will happen for the specified duration. 3391 */ 3392 static void 3393 iwi_led_off(void *arg) 3394 { 3395 struct iwi_softc *sc = arg; 3396 uint32_t v; 3397 3398 v = iwi_read_event(sc); 3399 v &= ~sc->sc_ledpin; 3400 iwi_write_event(sc, iwi_toggle_event(v)); 3401 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc); 3402 } 3403 3404 /* 3405 * Blink the LED according to the specified on/off times. 3406 */ 3407 static void 3408 iwi_led_blink(struct iwi_softc *sc, int on, int off) 3409 { 3410 uint32_t v; 3411 3412 v = iwi_read_event(sc); 3413 v |= sc->sc_ledpin; 3414 iwi_write_event(sc, iwi_toggle_event(v)); 3415 sc->sc_blinking = 1; 3416 sc->sc_ledoff = off; 3417 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc); 3418 } 3419 3420 static void 3421 iwi_led_event(struct iwi_softc *sc, int event) 3422 { 3423 #define N(a) (sizeof(a)/sizeof(a[0])) 3424 /* NB: on/off times from the Atheros NDIS driver, w/ permission */ 3425 static const struct { 3426 u_int rate; /* tx/rx iwi rate */ 3427 u_int16_t timeOn; /* LED on time (ms) */ 3428 u_int16_t timeOff; /* LED off time (ms) */ 3429 } blinkrates[] = { 3430 { IWI_RATE_OFDM54, 40, 10 }, 3431 { IWI_RATE_OFDM48, 44, 11 }, 3432 { IWI_RATE_OFDM36, 50, 13 }, 3433 { IWI_RATE_OFDM24, 57, 14 }, 3434 { IWI_RATE_OFDM18, 67, 16 }, 3435 { IWI_RATE_OFDM12, 80, 20 }, 3436 { IWI_RATE_DS11, 100, 25 }, 3437 { IWI_RATE_OFDM9, 133, 34 }, 3438 { IWI_RATE_OFDM6, 160, 40 }, 3439 { IWI_RATE_DS5, 200, 50 }, 3440 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */ 3441 { IWI_RATE_DS2, 267, 66 }, 3442 { IWI_RATE_DS1, 400, 100 }, 3443 { 0, 500, 130 }, /* unknown rate/polling */ 3444 }; 3445 uint32_t txrate; 3446 int j = 0; /* XXX silence compiler */ 3447 3448 sc->sc_ledevent = ticks; /* time of last event */ 3449 if (sc->sc_blinking) /* don't interrupt active blink */ 3450 return; 3451 switch (event) { 3452 case IWI_LED_POLL: 3453 j = N(blinkrates)-1; 3454 break; 3455 case IWI_LED_TX: 3456 /* read current transmission rate from adapter */ 3457 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE); 3458 if (blinkrates[sc->sc_txrix].rate != txrate) { 3459 for (j = 0; j < N(blinkrates)-1; j++) 3460 if (blinkrates[j].rate == txrate) 3461 break; 3462 sc->sc_txrix = j; 3463 } else 3464 j = sc->sc_txrix; 3465 break; 3466 case IWI_LED_RX: 3467 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) { 3468 for (j = 0; j < N(blinkrates)-1; j++) 3469 if (blinkrates[j].rate == sc->sc_rxrate) 3470 break; 3471 sc->sc_rxrix = j; 3472 } else 3473 j = sc->sc_rxrix; 3474 break; 3475 } 3476 /* XXX beware of overflow */ 3477 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000, 3478 (blinkrates[j].timeOff * hz) / 1000); 3479 #undef N 3480 } 3481 3482 static int 3483 iwi_sysctl_softled(SYSCTL_HANDLER_ARGS) 3484 { 3485 struct iwi_softc *sc = arg1; 3486 int softled = sc->sc_softled; 3487 int error; 3488 3489 error = sysctl_handle_int(oidp, &softled, 0, req); 3490 if (error || !req->newptr) 3491 return error; 3492 softled = (softled != 0); 3493 if (softled != sc->sc_softled) { 3494 if (softled) { 3495 uint32_t v = iwi_read_event(sc); 3496 v &= ~sc->sc_ledpin; 3497 iwi_write_event(sc, iwi_toggle_event(v)); 3498 } 3499 sc->sc_softled = softled; 3500 } 3501 return 0; 3502 } 3503 3504 static void 3505 iwi_ledattach(struct iwi_softc *sc) 3506 { 3507 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 3508 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 3509 3510 sc->sc_blinking = 0; 3511 sc->sc_ledstate = 1; 3512 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */ 3513 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0); 3514 3515 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 3516 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 3517 iwi_sysctl_softled, "I", "enable/disable software LED support"); 3518 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 3519 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0, 3520 "pin setting to turn activity LED on"); 3521 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 3522 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0, 3523 "idle time for inactivity LED (ticks)"); 3524 /* XXX for debugging */ 3525 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 3526 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0, 3527 "NIC type from EEPROM"); 3528 3529 sc->sc_ledpin = IWI_RST_LED_ACTIVITY; 3530 sc->sc_softled = 1; 3531 3532 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff; 3533 if (sc->sc_nictype == 1) { 3534 /* 3535 * NB: led's are reversed. 3536 */ 3537 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED; 3538 } 3539 } 3540 3541 static void 3542 iwi_scan_start(struct ieee80211com *ic) 3543 { 3544 /* ignore */ 3545 } 3546 3547 static void 3548 iwi_set_channel(struct ieee80211com *ic) 3549 { 3550 struct ifnet *ifp = ic->ic_ifp; 3551 struct iwi_softc *sc = ifp->if_softc; 3552 if (sc->fw_state == IWI_FW_IDLE) 3553 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee); 3554 } 3555 3556 static void 3557 iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 3558 { 3559 struct ieee80211vap *vap = ss->ss_vap; 3560 struct ifnet *ifp = vap->iv_ic->ic_ifp; 3561 struct iwi_softc *sc = ifp->if_softc; 3562 IWI_LOCK_DECL; 3563 3564 IWI_LOCK(sc); 3565 if (iwi_scanchan(sc, maxdwell, 0)) 3566 ieee80211_cancel_scan(vap); 3567 IWI_UNLOCK(sc); 3568 } 3569 3570 static void 3571 iwi_scan_mindwell(struct ieee80211_scan_state *ss) 3572 { 3573 /* NB: don't try to abort scan; wait for firmware to finish */ 3574 } 3575 3576 static void 3577 iwi_scan_end(struct ieee80211com *ic) 3578 { 3579 struct ifnet *ifp = ic->ic_ifp; 3580 struct iwi_softc *sc = ifp->if_softc; 3581 IWI_LOCK_DECL; 3582 3583 IWI_LOCK(sc); 3584 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN; 3585 /* NB: make sure we're still scanning */ 3586 if (sc->fw_state == IWI_FW_SCANNING) 3587 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0); 3588 IWI_UNLOCK(sc); 3589 } 3590