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