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