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