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