1/*- 2 * Copyright (c) 2006,2007 3 * Damien Bergamini <damien.bergamini@free.fr> 4 * Benjamin Close <Benjamin.Close@clearchain.com> 5 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#include <sys/cdefs.h> 21__FBSDID("$FreeBSD: releng/12.0/sys/dev/wpi/if_wpi.c 327479 2018-01-02 00:07:28Z adrian $"); 22 23/* 24 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters. 25 * 26 * The 3945ABG network adapter doesn't use traditional hardware as 27 * many other adaptors do. Instead at run time the eeprom is set into a known 28 * state and told to load boot firmware. The boot firmware loads an init and a 29 * main binary firmware image into SRAM on the card via DMA. 30 * Once the firmware is loaded, the driver/hw then 31 * communicate by way of circular dma rings via the SRAM to the firmware. 32 * 33 * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings. 34 * The 4 tx data rings allow for prioritization QoS. 35 * 36 * The rx data ring consists of 32 dma buffers. Two registers are used to 37 * indicate where in the ring the driver and the firmware are up to. The 38 * driver sets the initial read index (reg1) and the initial write index (reg2), 39 * the firmware updates the read index (reg1) on rx of a packet and fires an 40 * interrupt. The driver then processes the buffers starting at reg1 indicating 41 * to the firmware which buffers have been accessed by updating reg2. At the 42 * same time allocating new memory for the processed buffer. 43 * 44 * A similar thing happens with the tx rings. The difference is the firmware 45 * stop processing buffers once the queue is full and until confirmation 46 * of a successful transmition (tx_done) has occurred. 47 * 48 * The command ring operates in the same manner as the tx queues. 49 * 50 * All communication direct to the card (ie eeprom) is classed as Stage1 51 * communication 52 * 53 * All communication via the firmware to the card is classed as State2. 54 * The firmware consists of 2 parts. A bootstrap firmware and a runtime 55 * firmware. The bootstrap firmware and runtime firmware are loaded 56 * from host memory via dma to the card then told to execute. From this point 57 * on the majority of communications between the driver and the card goes 58 * via the firmware. 59 */ 60 61#include <sys/param.h> 62#include <sys/sysctl.h> 63#include <sys/sockio.h> 64#include <sys/mbuf.h> 65#include <sys/kernel.h> 66#include <sys/socket.h> 67#include <sys/systm.h> 68#include <sys/malloc.h> 69#include <sys/queue.h> 70#include <sys/taskqueue.h> 71#include <sys/module.h> 72#include <sys/bus.h> 73#include <sys/endian.h> 74#include <sys/linker.h> 75#include <sys/firmware.h> 76 77#include <machine/bus.h> 78#include <machine/resource.h> 79#include <sys/rman.h> 80 81#include <dev/pci/pcireg.h> 82#include <dev/pci/pcivar.h> 83 84#include <net/bpf.h> 85#include <net/if.h> 86#include <net/if_var.h> 87#include <net/if_arp.h> 88#include <net/ethernet.h> 89#include <net/if_dl.h> 90#include <net/if_media.h> 91#include <net/if_types.h> 92 93#include <netinet/in.h> 94#include <netinet/in_systm.h> 95#include <netinet/in_var.h> 96#include <netinet/if_ether.h> 97#include <netinet/ip.h> 98 99#include <net80211/ieee80211_var.h> 100#include <net80211/ieee80211_radiotap.h> 101#include <net80211/ieee80211_regdomain.h> 102#include <net80211/ieee80211_ratectl.h> 103 104#include <dev/wpi/if_wpireg.h> 105#include <dev/wpi/if_wpivar.h> 106#include <dev/wpi/if_wpi_debug.h> 107 108struct wpi_ident { 109 uint16_t vendor; 110 uint16_t device; 111 uint16_t subdevice; 112 const char *name; 113}; 114 115static const struct wpi_ident wpi_ident_table[] = { 116 /* The below entries support ABG regardless of the subid */ 117 { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" }, 118 { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" }, 119 /* The below entries only support BG */ 120 { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG" }, 121 { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG" }, 122 { 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG" }, 123 { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG" }, 124 { 0, 0, 0, NULL } 125}; 126 127static int wpi_probe(device_t); 128static int wpi_attach(device_t); 129static void wpi_radiotap_attach(struct wpi_softc *); 130static void wpi_sysctlattach(struct wpi_softc *); 131static void wpi_init_beacon(struct wpi_vap *); 132static struct ieee80211vap *wpi_vap_create(struct ieee80211com *, 133 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 134 const uint8_t [IEEE80211_ADDR_LEN], 135 const uint8_t [IEEE80211_ADDR_LEN]); 136static void wpi_vap_delete(struct ieee80211vap *); 137static int wpi_detach(device_t); 138static int wpi_shutdown(device_t); 139static int wpi_suspend(device_t); 140static int wpi_resume(device_t); 141static int wpi_nic_lock(struct wpi_softc *); 142static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int); 143static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int); 144static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *, 145 void **, bus_size_t, bus_size_t); 146static void wpi_dma_contig_free(struct wpi_dma_info *); 147static int wpi_alloc_shared(struct wpi_softc *); 148static void wpi_free_shared(struct wpi_softc *); 149static int wpi_alloc_fwmem(struct wpi_softc *); 150static void wpi_free_fwmem(struct wpi_softc *); 151static int wpi_alloc_rx_ring(struct wpi_softc *); 152static void wpi_update_rx_ring(struct wpi_softc *); 153static void wpi_update_rx_ring_ps(struct wpi_softc *); 154static void wpi_reset_rx_ring(struct wpi_softc *); 155static void wpi_free_rx_ring(struct wpi_softc *); 156static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, 157 uint8_t); 158static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 159static void wpi_update_tx_ring_ps(struct wpi_softc *, 160 struct wpi_tx_ring *); 161static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 162static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 163static int wpi_read_eeprom(struct wpi_softc *, 164 uint8_t macaddr[IEEE80211_ADDR_LEN]); 165static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *); 166static void wpi_read_eeprom_band(struct wpi_softc *, uint8_t, int, int *, 167 struct ieee80211_channel[]); 168static int wpi_read_eeprom_channels(struct wpi_softc *, uint8_t); 169static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *, 170 struct ieee80211_channel *); 171static void wpi_getradiocaps(struct ieee80211com *, int, int *, 172 struct ieee80211_channel[]); 173static int wpi_setregdomain(struct ieee80211com *, 174 struct ieee80211_regdomain *, int, 175 struct ieee80211_channel[]); 176static int wpi_read_eeprom_group(struct wpi_softc *, uint8_t); 177static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *, 178 const uint8_t mac[IEEE80211_ADDR_LEN]); 179static void wpi_node_free(struct ieee80211_node *); 180static void wpi_ibss_recv_mgmt(struct ieee80211_node *, struct mbuf *, int, 181 const struct ieee80211_rx_stats *, 182 int, int); 183static void wpi_restore_node(void *, struct ieee80211_node *); 184static void wpi_restore_node_table(struct wpi_softc *, struct wpi_vap *); 185static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int); 186static void wpi_calib_timeout(void *); 187static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *, 188 struct wpi_rx_data *); 189static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *, 190 struct wpi_rx_data *); 191static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *); 192static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *); 193static void wpi_notif_intr(struct wpi_softc *); 194static void wpi_wakeup_intr(struct wpi_softc *); 195#ifdef WPI_DEBUG 196static void wpi_debug_registers(struct wpi_softc *); 197#endif 198static void wpi_fatal_intr(struct wpi_softc *); 199static void wpi_intr(void *); 200static void wpi_free_txfrags(struct wpi_softc *, uint16_t); 201static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *); 202static int wpi_tx_data(struct wpi_softc *, struct mbuf *, 203 struct ieee80211_node *); 204static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *, 205 struct ieee80211_node *, 206 const struct ieee80211_bpf_params *); 207static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *, 208 const struct ieee80211_bpf_params *); 209static int wpi_transmit(struct ieee80211com *, struct mbuf *); 210static void wpi_watchdog_rfkill(void *); 211static void wpi_scan_timeout(void *); 212static void wpi_tx_timeout(void *); 213static void wpi_parent(struct ieee80211com *); 214static int wpi_cmd(struct wpi_softc *, uint8_t, const void *, uint16_t, 215 int); 216static int wpi_mrr_setup(struct wpi_softc *); 217static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *); 218static int wpi_add_broadcast_node(struct wpi_softc *, int); 219static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *); 220static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *); 221static int wpi_updateedca(struct ieee80211com *); 222static void wpi_set_promisc(struct wpi_softc *); 223static void wpi_update_promisc(struct ieee80211com *); 224static void wpi_update_mcast(struct ieee80211com *); 225static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t); 226static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *); 227static void wpi_power_calibration(struct wpi_softc *); 228static int wpi_set_txpower(struct wpi_softc *, int); 229static int wpi_get_power_index(struct wpi_softc *, 230 struct wpi_power_group *, uint8_t, int, int); 231static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int); 232static int wpi_send_btcoex(struct wpi_softc *); 233static int wpi_send_rxon(struct wpi_softc *, int, int); 234static int wpi_config(struct wpi_softc *); 235static uint16_t wpi_get_active_dwell_time(struct wpi_softc *, 236 struct ieee80211_channel *, uint8_t); 237static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t); 238static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *, 239 struct ieee80211_channel *); 240static uint32_t wpi_get_scan_pause_time(uint32_t, uint16_t); 241static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *); 242static int wpi_auth(struct wpi_softc *, struct ieee80211vap *); 243static int wpi_config_beacon(struct wpi_vap *); 244static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *); 245static void wpi_update_beacon(struct ieee80211vap *, int); 246static void wpi_newassoc(struct ieee80211_node *, int); 247static int wpi_run(struct wpi_softc *, struct ieee80211vap *); 248static int wpi_load_key(struct ieee80211_node *, 249 const struct ieee80211_key *); 250static void wpi_load_key_cb(void *, struct ieee80211_node *); 251static int wpi_set_global_keys(struct ieee80211_node *); 252static int wpi_del_key(struct ieee80211_node *, 253 const struct ieee80211_key *); 254static void wpi_del_key_cb(void *, struct ieee80211_node *); 255static int wpi_process_key(struct ieee80211vap *, 256 const struct ieee80211_key *, int); 257static int wpi_key_set(struct ieee80211vap *, 258 const struct ieee80211_key *); 259static int wpi_key_delete(struct ieee80211vap *, 260 const struct ieee80211_key *); 261static int wpi_post_alive(struct wpi_softc *); 262static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *, 263 uint32_t); 264static int wpi_load_firmware(struct wpi_softc *); 265static int wpi_read_firmware(struct wpi_softc *); 266static void wpi_unload_firmware(struct wpi_softc *); 267static int wpi_clock_wait(struct wpi_softc *); 268static int wpi_apm_init(struct wpi_softc *); 269static void wpi_apm_stop_master(struct wpi_softc *); 270static void wpi_apm_stop(struct wpi_softc *); 271static void wpi_nic_config(struct wpi_softc *); 272static int wpi_hw_init(struct wpi_softc *); 273static void wpi_hw_stop(struct wpi_softc *); 274static void wpi_radio_on(void *, int); 275static void wpi_radio_off(void *, int); 276static int wpi_init(struct wpi_softc *); 277static void wpi_stop_locked(struct wpi_softc *); 278static void wpi_stop(struct wpi_softc *); 279static void wpi_scan_start(struct ieee80211com *); 280static void wpi_scan_end(struct ieee80211com *); 281static void wpi_set_channel(struct ieee80211com *); 282static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long); 283static void wpi_scan_mindwell(struct ieee80211_scan_state *); 284 285static device_method_t wpi_methods[] = { 286 /* Device interface */ 287 DEVMETHOD(device_probe, wpi_probe), 288 DEVMETHOD(device_attach, wpi_attach), 289 DEVMETHOD(device_detach, wpi_detach), 290 DEVMETHOD(device_shutdown, wpi_shutdown), 291 DEVMETHOD(device_suspend, wpi_suspend), 292 DEVMETHOD(device_resume, wpi_resume), 293 294 DEVMETHOD_END 295}; 296 297static driver_t wpi_driver = { 298 "wpi", 299 wpi_methods, 300 sizeof (struct wpi_softc) 301}; 302static devclass_t wpi_devclass; 303 304DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL); 305 306MODULE_VERSION(wpi, 1); 307 308MODULE_DEPEND(wpi, pci, 1, 1, 1); 309MODULE_DEPEND(wpi, wlan, 1, 1, 1); 310MODULE_DEPEND(wpi, firmware, 1, 1, 1); 311 312static int 313wpi_probe(device_t dev) 314{ 315 const struct wpi_ident *ident; 316 317 for (ident = wpi_ident_table; ident->name != NULL; ident++) { 318 if (pci_get_vendor(dev) == ident->vendor && 319 pci_get_device(dev) == ident->device) { 320 device_set_desc(dev, ident->name); 321 return (BUS_PROBE_DEFAULT); 322 } 323 } 324 return ENXIO; 325} 326 327static int 328wpi_attach(device_t dev) 329{ 330 struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev); 331 struct ieee80211com *ic; 332 uint8_t i; 333 int error, rid; 334#ifdef WPI_DEBUG 335 int supportsa = 1; 336 const struct wpi_ident *ident; 337#endif 338 339 sc->sc_dev = dev; 340 341#ifdef WPI_DEBUG 342 error = resource_int_value(device_get_name(sc->sc_dev), 343 device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug)); 344 if (error != 0) 345 sc->sc_debug = 0; 346#else 347 sc->sc_debug = 0; 348#endif 349 350 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 351 352 /* 353 * Get the offset of the PCI Express Capability Structure in PCI 354 * Configuration Space. 355 */ 356 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off); 357 if (error != 0) { 358 device_printf(dev, "PCIe capability structure not found!\n"); 359 return error; 360 } 361 362 /* 363 * Some card's only support 802.11b/g not a, check to see if 364 * this is one such card. A 0x0 in the subdevice table indicates 365 * the entire subdevice range is to be ignored. 366 */ 367#ifdef WPI_DEBUG 368 for (ident = wpi_ident_table; ident->name != NULL; ident++) { 369 if (ident->subdevice && 370 pci_get_subdevice(dev) == ident->subdevice) { 371 supportsa = 0; 372 break; 373 } 374 } 375#endif 376 377 /* Clear device-specific "PCI retry timeout" register (41h). */ 378 pci_write_config(dev, 0x41, 0, 1); 379 380 /* Enable bus-mastering. */ 381 pci_enable_busmaster(dev); 382 383 rid = PCIR_BAR(0); 384 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 385 RF_ACTIVE); 386 if (sc->mem == NULL) { 387 device_printf(dev, "can't map mem space\n"); 388 return ENOMEM; 389 } 390 sc->sc_st = rman_get_bustag(sc->mem); 391 sc->sc_sh = rman_get_bushandle(sc->mem); 392 393 rid = 1; 394 if (pci_alloc_msi(dev, &rid) == 0) 395 rid = 1; 396 else 397 rid = 0; 398 /* Install interrupt handler. */ 399 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE | 400 (rid != 0 ? 0 : RF_SHAREABLE)); 401 if (sc->irq == NULL) { 402 device_printf(dev, "can't map interrupt\n"); 403 error = ENOMEM; 404 goto fail; 405 } 406 407 WPI_LOCK_INIT(sc); 408 WPI_TX_LOCK_INIT(sc); 409 WPI_RXON_LOCK_INIT(sc); 410 WPI_NT_LOCK_INIT(sc); 411 WPI_TXQ_LOCK_INIT(sc); 412 WPI_TXQ_STATE_LOCK_INIT(sc); 413 414 /* Allocate DMA memory for firmware transfers. */ 415 if ((error = wpi_alloc_fwmem(sc)) != 0) { 416 device_printf(dev, 417 "could not allocate memory for firmware, error %d\n", 418 error); 419 goto fail; 420 } 421 422 /* Allocate shared page. */ 423 if ((error = wpi_alloc_shared(sc)) != 0) { 424 device_printf(dev, "could not allocate shared page\n"); 425 goto fail; 426 } 427 428 /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */ 429 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) { 430 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) { 431 device_printf(dev, 432 "could not allocate TX ring %d, error %d\n", i, 433 error); 434 goto fail; 435 } 436 } 437 438 /* Allocate RX ring. */ 439 if ((error = wpi_alloc_rx_ring(sc)) != 0) { 440 device_printf(dev, "could not allocate RX ring, error %d\n", 441 error); 442 goto fail; 443 } 444 445 /* Clear pending interrupts. */ 446 WPI_WRITE(sc, WPI_INT, 0xffffffff); 447 448 ic = &sc->sc_ic; 449 ic->ic_softc = sc; 450 ic->ic_name = device_get_nameunit(dev); 451 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 452 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 453 454 /* Set device capabilities. */ 455 ic->ic_caps = 456 IEEE80211_C_STA /* station mode supported */ 457 | IEEE80211_C_IBSS /* IBSS mode supported */ 458 | IEEE80211_C_HOSTAP /* Host access point mode */ 459 | IEEE80211_C_MONITOR /* monitor mode supported */ 460 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 461 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 462 | IEEE80211_C_TXFRAG /* handle tx frags */ 463 | IEEE80211_C_TXPMGT /* tx power management */ 464 | IEEE80211_C_SHSLOT /* short slot time supported */ 465 | IEEE80211_C_WPA /* 802.11i */ 466 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 467 | IEEE80211_C_WME /* 802.11e */ 468 | IEEE80211_C_PMGT /* Station-side power mgmt */ 469 ; 470 471 ic->ic_cryptocaps = 472 IEEE80211_CRYPTO_AES_CCM; 473 474 /* 475 * Read in the eeprom and also setup the channels for 476 * net80211. We don't set the rates as net80211 does this for us 477 */ 478 if ((error = wpi_read_eeprom(sc, ic->ic_macaddr)) != 0) { 479 device_printf(dev, "could not read EEPROM, error %d\n", 480 error); 481 goto fail; 482 } 483 484#ifdef WPI_DEBUG 485 if (bootverbose) { 486 device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", 487 sc->domain); 488 device_printf(sc->sc_dev, "Hardware Type: %c\n", 489 sc->type > 1 ? 'B': '?'); 490 device_printf(sc->sc_dev, "Hardware Revision: %c\n", 491 ((sc->rev & 0xf0) == 0xd0) ? 'D': '?'); 492 device_printf(sc->sc_dev, "SKU %s support 802.11a\n", 493 supportsa ? "does" : "does not"); 494 495 /* XXX hw_config uses the PCIDEV for the Hardware rev. Must 496 check what sc->rev really represents - benjsc 20070615 */ 497 } 498#endif 499 500 ieee80211_ifattach(ic); 501 ic->ic_vap_create = wpi_vap_create; 502 ic->ic_vap_delete = wpi_vap_delete; 503 ic->ic_parent = wpi_parent; 504 ic->ic_raw_xmit = wpi_raw_xmit; 505 ic->ic_transmit = wpi_transmit; 506 ic->ic_node_alloc = wpi_node_alloc; 507 sc->sc_node_free = ic->ic_node_free; 508 ic->ic_node_free = wpi_node_free; 509 ic->ic_wme.wme_update = wpi_updateedca; 510 ic->ic_update_promisc = wpi_update_promisc; 511 ic->ic_update_mcast = wpi_update_mcast; 512 ic->ic_newassoc = wpi_newassoc; 513 ic->ic_scan_start = wpi_scan_start; 514 ic->ic_scan_end = wpi_scan_end; 515 ic->ic_set_channel = wpi_set_channel; 516 ic->ic_scan_curchan = wpi_scan_curchan; 517 ic->ic_scan_mindwell = wpi_scan_mindwell; 518 ic->ic_getradiocaps = wpi_getradiocaps; 519 ic->ic_setregdomain = wpi_setregdomain; 520 521 sc->sc_update_rx_ring = wpi_update_rx_ring; 522 sc->sc_update_tx_ring = wpi_update_tx_ring; 523 524 wpi_radiotap_attach(sc); 525 526 /* Setup Tx status flags (constant). */ 527 sc->sc_txs.flags = IEEE80211_RATECTL_STATUS_PKTLEN | 528 IEEE80211_RATECTL_STATUS_SHORT_RETRY | 529 IEEE80211_RATECTL_STATUS_LONG_RETRY; 530 531 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0); 532 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0); 533 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0); 534 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0); 535 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc); 536 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc); 537 538 wpi_sysctlattach(sc); 539 540 /* 541 * Hook our interrupt after all initialization is complete. 542 */ 543 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 544 NULL, wpi_intr, sc, &sc->sc_ih); 545 if (error != 0) { 546 device_printf(dev, "can't establish interrupt, error %d\n", 547 error); 548 goto fail; 549 } 550 551 if (bootverbose) 552 ieee80211_announce(ic); 553 554#ifdef WPI_DEBUG 555 if (sc->sc_debug & WPI_DEBUG_HW) 556 ieee80211_announce_channels(ic); 557#endif 558 559 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 560 return 0; 561 562fail: wpi_detach(dev); 563 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 564 return error; 565} 566 567/* 568 * Attach the interface to 802.11 radiotap. 569 */ 570static void 571wpi_radiotap_attach(struct wpi_softc *sc) 572{ 573 struct wpi_rx_radiotap_header *rxtap = &sc->sc_rxtap; 574 struct wpi_tx_radiotap_header *txtap = &sc->sc_txtap; 575 576 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 577 ieee80211_radiotap_attach(&sc->sc_ic, 578 &txtap->wt_ihdr, sizeof(*txtap), WPI_TX_RADIOTAP_PRESENT, 579 &rxtap->wr_ihdr, sizeof(*rxtap), WPI_RX_RADIOTAP_PRESENT); 580 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 581} 582 583static void 584wpi_sysctlattach(struct wpi_softc *sc) 585{ 586#ifdef WPI_DEBUG 587 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 588 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 589 590 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 591 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug, 592 "control debugging printfs"); 593#endif 594} 595 596static void 597wpi_init_beacon(struct wpi_vap *wvp) 598{ 599 struct wpi_buf *bcn = &wvp->wv_bcbuf; 600 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data; 601 602 cmd->id = WPI_ID_BROADCAST; 603 cmd->ofdm_mask = 0xff; 604 cmd->cck_mask = 0x0f; 605 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE); 606 607 /* 608 * XXX WPI_TX_AUTO_SEQ seems to be ignored - workaround this issue 609 * XXX by using WPI_TX_NEED_ACK instead (with some side effects). 610 */ 611 cmd->flags = htole32(WPI_TX_NEED_ACK | WPI_TX_INSERT_TSTAMP); 612 613 bcn->code = WPI_CMD_SET_BEACON; 614 bcn->ac = WPI_CMD_QUEUE_NUM; 615 bcn->size = sizeof(struct wpi_cmd_beacon); 616} 617 618static struct ieee80211vap * 619wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 620 enum ieee80211_opmode opmode, int flags, 621 const uint8_t bssid[IEEE80211_ADDR_LEN], 622 const uint8_t mac[IEEE80211_ADDR_LEN]) 623{ 624 struct wpi_vap *wvp; 625 struct ieee80211vap *vap; 626 627 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 628 return NULL; 629 630 wvp = malloc(sizeof(struct wpi_vap), M_80211_VAP, M_WAITOK | M_ZERO); 631 vap = &wvp->wv_vap; 632 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid); 633 634 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) { 635 WPI_VAP_LOCK_INIT(wvp); 636 wpi_init_beacon(wvp); 637 } 638 639 /* Override with driver methods. */ 640 vap->iv_key_set = wpi_key_set; 641 vap->iv_key_delete = wpi_key_delete; 642 if (opmode == IEEE80211_M_IBSS) { 643 wvp->wv_recv_mgmt = vap->iv_recv_mgmt; 644 vap->iv_recv_mgmt = wpi_ibss_recv_mgmt; 645 } 646 wvp->wv_newstate = vap->iv_newstate; 647 vap->iv_newstate = wpi_newstate; 648 vap->iv_update_beacon = wpi_update_beacon; 649 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1; 650 651 ieee80211_ratectl_init(vap); 652 /* Complete setup. */ 653 ieee80211_vap_attach(vap, ieee80211_media_change, 654 ieee80211_media_status, mac); 655 ic->ic_opmode = opmode; 656 return vap; 657} 658 659static void 660wpi_vap_delete(struct ieee80211vap *vap) 661{ 662 struct wpi_vap *wvp = WPI_VAP(vap); 663 struct wpi_buf *bcn = &wvp->wv_bcbuf; 664 enum ieee80211_opmode opmode = vap->iv_opmode; 665 666 ieee80211_ratectl_deinit(vap); 667 ieee80211_vap_detach(vap); 668 669 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) { 670 if (bcn->m != NULL) 671 m_freem(bcn->m); 672 673 WPI_VAP_LOCK_DESTROY(wvp); 674 } 675 676 free(wvp, M_80211_VAP); 677} 678 679static int 680wpi_detach(device_t dev) 681{ 682 struct wpi_softc *sc = device_get_softc(dev); 683 struct ieee80211com *ic = &sc->sc_ic; 684 uint8_t qid; 685 686 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 687 688 if (ic->ic_vap_create == wpi_vap_create) { 689 ieee80211_draintask(ic, &sc->sc_radioon_task); 690 ieee80211_draintask(ic, &sc->sc_radiooff_task); 691 692 wpi_stop(sc); 693 694 callout_drain(&sc->watchdog_rfkill); 695 callout_drain(&sc->tx_timeout); 696 callout_drain(&sc->scan_timeout); 697 callout_drain(&sc->calib_to); 698 ieee80211_ifdetach(ic); 699 } 700 701 /* Uninstall interrupt handler. */ 702 if (sc->irq != NULL) { 703 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 704 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), 705 sc->irq); 706 pci_release_msi(dev); 707 } 708 709 if (sc->txq[0].data_dmat) { 710 /* Free DMA resources. */ 711 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) 712 wpi_free_tx_ring(sc, &sc->txq[qid]); 713 714 wpi_free_rx_ring(sc); 715 wpi_free_shared(sc); 716 } 717 718 if (sc->fw_dma.tag) 719 wpi_free_fwmem(sc); 720 721 if (sc->mem != NULL) 722 bus_release_resource(dev, SYS_RES_MEMORY, 723 rman_get_rid(sc->mem), sc->mem); 724 725 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 726 WPI_TXQ_STATE_LOCK_DESTROY(sc); 727 WPI_TXQ_LOCK_DESTROY(sc); 728 WPI_NT_LOCK_DESTROY(sc); 729 WPI_RXON_LOCK_DESTROY(sc); 730 WPI_TX_LOCK_DESTROY(sc); 731 WPI_LOCK_DESTROY(sc); 732 return 0; 733} 734 735static int 736wpi_shutdown(device_t dev) 737{ 738 struct wpi_softc *sc = device_get_softc(dev); 739 740 wpi_stop(sc); 741 return 0; 742} 743 744static int 745wpi_suspend(device_t dev) 746{ 747 struct wpi_softc *sc = device_get_softc(dev); 748 struct ieee80211com *ic = &sc->sc_ic; 749 750 ieee80211_suspend_all(ic); 751 return 0; 752} 753 754static int 755wpi_resume(device_t dev) 756{ 757 struct wpi_softc *sc = device_get_softc(dev); 758 struct ieee80211com *ic = &sc->sc_ic; 759 760 /* Clear device-specific "PCI retry timeout" register (41h). */ 761 pci_write_config(dev, 0x41, 0, 1); 762 763 ieee80211_resume_all(ic); 764 return 0; 765} 766 767/* 768 * Grab exclusive access to NIC memory. 769 */ 770static int 771wpi_nic_lock(struct wpi_softc *sc) 772{ 773 int ntries; 774 775 /* Request exclusive access to NIC. */ 776 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 777 778 /* Spin until we actually get the lock. */ 779 for (ntries = 0; ntries < 1000; ntries++) { 780 if ((WPI_READ(sc, WPI_GP_CNTRL) & 781 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) == 782 WPI_GP_CNTRL_MAC_ACCESS_ENA) 783 return 0; 784 DELAY(10); 785 } 786 787 device_printf(sc->sc_dev, "could not lock memory\n"); 788 789 return ETIMEDOUT; 790} 791 792/* 793 * Release lock on NIC memory. 794 */ 795static __inline void 796wpi_nic_unlock(struct wpi_softc *sc) 797{ 798 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 799} 800 801static __inline uint32_t 802wpi_prph_read(struct wpi_softc *sc, uint32_t addr) 803{ 804 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr); 805 WPI_BARRIER_READ_WRITE(sc); 806 return WPI_READ(sc, WPI_PRPH_RDATA); 807} 808 809static __inline void 810wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data) 811{ 812 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr); 813 WPI_BARRIER_WRITE(sc); 814 WPI_WRITE(sc, WPI_PRPH_WDATA, data); 815} 816 817static __inline void 818wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask) 819{ 820 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask); 821} 822 823static __inline void 824wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask) 825{ 826 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask); 827} 828 829static __inline void 830wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr, 831 const uint32_t *data, uint32_t count) 832{ 833 for (; count != 0; count--, data++, addr += 4) 834 wpi_prph_write(sc, addr, *data); 835} 836 837static __inline uint32_t 838wpi_mem_read(struct wpi_softc *sc, uint32_t addr) 839{ 840 WPI_WRITE(sc, WPI_MEM_RADDR, addr); 841 WPI_BARRIER_READ_WRITE(sc); 842 return WPI_READ(sc, WPI_MEM_RDATA); 843} 844 845static __inline void 846wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data, 847 int count) 848{ 849 for (; count > 0; count--, addr += 4) 850 *data++ = wpi_mem_read(sc, addr); 851} 852 853static int 854wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count) 855{ 856 uint8_t *out = data; 857 uint32_t val; 858 int error, ntries; 859 860 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 861 862 if ((error = wpi_nic_lock(sc)) != 0) 863 return error; 864 865 for (; count > 0; count -= 2, addr++) { 866 WPI_WRITE(sc, WPI_EEPROM, addr << 2); 867 for (ntries = 0; ntries < 10; ntries++) { 868 val = WPI_READ(sc, WPI_EEPROM); 869 if (val & WPI_EEPROM_READ_VALID) 870 break; 871 DELAY(5); 872 } 873 if (ntries == 10) { 874 device_printf(sc->sc_dev, 875 "timeout reading ROM at 0x%x\n", addr); 876 return ETIMEDOUT; 877 } 878 *out++= val >> 16; 879 if (count > 1) 880 *out ++= val >> 24; 881 } 882 883 wpi_nic_unlock(sc); 884 885 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 886 887 return 0; 888} 889 890static void 891wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 892{ 893 if (error != 0) 894 return; 895 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 896 *(bus_addr_t *)arg = segs[0].ds_addr; 897} 898 899/* 900 * Allocates a contiguous block of dma memory of the requested size and 901 * alignment. 902 */ 903static int 904wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma, 905 void **kvap, bus_size_t size, bus_size_t alignment) 906{ 907 int error; 908 909 dma->tag = NULL; 910 dma->size = size; 911 912 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment, 913 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size, 914 1, size, 0, NULL, NULL, &dma->tag); 915 if (error != 0) 916 goto fail; 917 918 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 919 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map); 920 if (error != 0) 921 goto fail; 922 923 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size, 924 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT); 925 if (error != 0) 926 goto fail; 927 928 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 929 930 if (kvap != NULL) 931 *kvap = dma->vaddr; 932 933 return 0; 934 935fail: wpi_dma_contig_free(dma); 936 return error; 937} 938 939static void 940wpi_dma_contig_free(struct wpi_dma_info *dma) 941{ 942 if (dma->vaddr != NULL) { 943 bus_dmamap_sync(dma->tag, dma->map, 944 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 945 bus_dmamap_unload(dma->tag, dma->map); 946 bus_dmamem_free(dma->tag, dma->vaddr, dma->map); 947 dma->vaddr = NULL; 948 } 949 if (dma->tag != NULL) { 950 bus_dma_tag_destroy(dma->tag); 951 dma->tag = NULL; 952 } 953} 954 955/* 956 * Allocate a shared page between host and NIC. 957 */ 958static int 959wpi_alloc_shared(struct wpi_softc *sc) 960{ 961 /* Shared buffer must be aligned on a 4KB boundary. */ 962 return wpi_dma_contig_alloc(sc, &sc->shared_dma, 963 (void **)&sc->shared, sizeof (struct wpi_shared), 4096); 964} 965 966static void 967wpi_free_shared(struct wpi_softc *sc) 968{ 969 wpi_dma_contig_free(&sc->shared_dma); 970} 971 972/* 973 * Allocate DMA-safe memory for firmware transfer. 974 */ 975static int 976wpi_alloc_fwmem(struct wpi_softc *sc) 977{ 978 /* Must be aligned on a 16-byte boundary. */ 979 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL, 980 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16); 981} 982 983static void 984wpi_free_fwmem(struct wpi_softc *sc) 985{ 986 wpi_dma_contig_free(&sc->fw_dma); 987} 988 989static int 990wpi_alloc_rx_ring(struct wpi_softc *sc) 991{ 992 struct wpi_rx_ring *ring = &sc->rxq; 993 bus_size_t size; 994 int i, error; 995 996 ring->cur = 0; 997 ring->update = 0; 998 999 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1000 1001 /* Allocate RX descriptors (16KB aligned.) */ 1002 size = WPI_RX_RING_COUNT * sizeof (uint32_t); 1003 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, 1004 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN); 1005 if (error != 0) { 1006 device_printf(sc->sc_dev, 1007 "%s: could not allocate RX ring DMA memory, error %d\n", 1008 __func__, error); 1009 goto fail; 1010 } 1011 1012 /* Create RX buffer DMA tag. */ 1013 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 1014 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 1015 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL, &ring->data_dmat); 1016 if (error != 0) { 1017 device_printf(sc->sc_dev, 1018 "%s: could not create RX buf DMA tag, error %d\n", 1019 __func__, error); 1020 goto fail; 1021 } 1022 1023 /* 1024 * Allocate and map RX buffers. 1025 */ 1026 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 1027 struct wpi_rx_data *data = &ring->data[i]; 1028 bus_addr_t paddr; 1029 1030 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 1031 if (error != 0) { 1032 device_printf(sc->sc_dev, 1033 "%s: could not create RX buf DMA map, error %d\n", 1034 __func__, error); 1035 goto fail; 1036 } 1037 1038 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 1039 if (data->m == NULL) { 1040 device_printf(sc->sc_dev, 1041 "%s: could not allocate RX mbuf\n", __func__); 1042 error = ENOBUFS; 1043 goto fail; 1044 } 1045 1046 error = bus_dmamap_load(ring->data_dmat, data->map, 1047 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr, 1048 &paddr, BUS_DMA_NOWAIT); 1049 if (error != 0 && error != EFBIG) { 1050 device_printf(sc->sc_dev, 1051 "%s: can't map mbuf (error %d)\n", __func__, 1052 error); 1053 goto fail; 1054 } 1055 1056 /* Set physical address of RX buffer. */ 1057 ring->desc[i] = htole32(paddr); 1058 } 1059 1060 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1061 BUS_DMASYNC_PREWRITE); 1062 1063 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 1064 1065 return 0; 1066 1067fail: wpi_free_rx_ring(sc); 1068 1069 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1070 1071 return error; 1072} 1073 1074static void 1075wpi_update_rx_ring(struct wpi_softc *sc) 1076{ 1077 WPI_WRITE(sc, WPI_FH_RX_WPTR, sc->rxq.cur & ~7); 1078} 1079 1080static void 1081wpi_update_rx_ring_ps(struct wpi_softc *sc) 1082{ 1083 struct wpi_rx_ring *ring = &sc->rxq; 1084 1085 if (ring->update != 0) { 1086 /* Wait for INT_WAKEUP event. */ 1087 return; 1088 } 1089 1090 WPI_TXQ_LOCK(sc); 1091 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 1092 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) { 1093 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n", 1094 __func__); 1095 ring->update = 1; 1096 } else { 1097 wpi_update_rx_ring(sc); 1098 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 1099 } 1100 WPI_TXQ_UNLOCK(sc); 1101} 1102 1103static void 1104wpi_reset_rx_ring(struct wpi_softc *sc) 1105{ 1106 struct wpi_rx_ring *ring = &sc->rxq; 1107 int ntries; 1108 1109 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 1110 1111 if (wpi_nic_lock(sc) == 0) { 1112 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0); 1113 for (ntries = 0; ntries < 1000; ntries++) { 1114 if (WPI_READ(sc, WPI_FH_RX_STATUS) & 1115 WPI_FH_RX_STATUS_IDLE) 1116 break; 1117 DELAY(10); 1118 } 1119 wpi_nic_unlock(sc); 1120 } 1121 1122 ring->cur = 0; 1123 ring->update = 0; 1124} 1125 1126static void 1127wpi_free_rx_ring(struct wpi_softc *sc) 1128{ 1129 struct wpi_rx_ring *ring = &sc->rxq; 1130 int i; 1131 1132 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 1133 1134 wpi_dma_contig_free(&ring->desc_dma); 1135 1136 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 1137 struct wpi_rx_data *data = &ring->data[i]; 1138 1139 if (data->m != NULL) { 1140 bus_dmamap_sync(ring->data_dmat, data->map, 1141 BUS_DMASYNC_POSTREAD); 1142 bus_dmamap_unload(ring->data_dmat, data->map); 1143 m_freem(data->m); 1144 data->m = NULL; 1145 } 1146 if (data->map != NULL) 1147 bus_dmamap_destroy(ring->data_dmat, data->map); 1148 } 1149 if (ring->data_dmat != NULL) { 1150 bus_dma_tag_destroy(ring->data_dmat); 1151 ring->data_dmat = NULL; 1152 } 1153} 1154 1155static int 1156wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, uint8_t qid) 1157{ 1158 bus_addr_t paddr; 1159 bus_size_t size; 1160 int i, error; 1161 1162 ring->qid = qid; 1163 ring->queued = 0; 1164 ring->cur = 0; 1165 ring->pending = 0; 1166 ring->update = 0; 1167 1168 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1169 1170 /* Allocate TX descriptors (16KB aligned.) */ 1171 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc); 1172 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc, 1173 size, WPI_RING_DMA_ALIGN); 1174 if (error != 0) { 1175 device_printf(sc->sc_dev, 1176 "%s: could not allocate TX ring DMA memory, error %d\n", 1177 __func__, error); 1178 goto fail; 1179 } 1180 1181 /* Update shared area with ring physical address. */ 1182 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr); 1183 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map, 1184 BUS_DMASYNC_PREWRITE); 1185 1186 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd); 1187 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd, 1188 size, 4); 1189 if (error != 0) { 1190 device_printf(sc->sc_dev, 1191 "%s: could not allocate TX cmd DMA memory, error %d\n", 1192 __func__, error); 1193 goto fail; 1194 } 1195 1196 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 1197 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1198 WPI_MAX_SCATTER - 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 1199 if (error != 0) { 1200 device_printf(sc->sc_dev, 1201 "%s: could not create TX buf DMA tag, error %d\n", 1202 __func__, error); 1203 goto fail; 1204 } 1205 1206 paddr = ring->cmd_dma.paddr; 1207 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 1208 struct wpi_tx_data *data = &ring->data[i]; 1209 1210 data->cmd_paddr = paddr; 1211 paddr += sizeof (struct wpi_tx_cmd); 1212 1213 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 1214 if (error != 0) { 1215 device_printf(sc->sc_dev, 1216 "%s: could not create TX buf DMA map, error %d\n", 1217 __func__, error); 1218 goto fail; 1219 } 1220 } 1221 1222 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 1223 1224 return 0; 1225 1226fail: wpi_free_tx_ring(sc, ring); 1227 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1228 return error; 1229} 1230 1231static void 1232wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1233{ 1234 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 1235} 1236 1237static void 1238wpi_update_tx_ring_ps(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1239{ 1240 1241 if (ring->update != 0) { 1242 /* Wait for INT_WAKEUP event. */ 1243 return; 1244 } 1245 1246 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 1247 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) { 1248 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n", 1249 __func__, ring->qid); 1250 ring->update = 1; 1251 } else { 1252 wpi_update_tx_ring(sc, ring); 1253 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 1254 } 1255} 1256 1257static void 1258wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1259{ 1260 int i; 1261 1262 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 1263 1264 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 1265 struct wpi_tx_data *data = &ring->data[i]; 1266 1267 if (data->m != NULL) { 1268 bus_dmamap_sync(ring->data_dmat, data->map, 1269 BUS_DMASYNC_POSTWRITE); 1270 bus_dmamap_unload(ring->data_dmat, data->map); 1271 m_freem(data->m); 1272 data->m = NULL; 1273 } 1274 if (data->ni != NULL) { 1275 ieee80211_free_node(data->ni); 1276 data->ni = NULL; 1277 } 1278 } 1279 /* Clear TX descriptors. */ 1280 memset(ring->desc, 0, ring->desc_dma.size); 1281 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1282 BUS_DMASYNC_PREWRITE); 1283 ring->queued = 0; 1284 ring->cur = 0; 1285 ring->pending = 0; 1286 ring->update = 0; 1287} 1288 1289static void 1290wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1291{ 1292 int i; 1293 1294 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 1295 1296 wpi_dma_contig_free(&ring->desc_dma); 1297 wpi_dma_contig_free(&ring->cmd_dma); 1298 1299 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 1300 struct wpi_tx_data *data = &ring->data[i]; 1301 1302 if (data->m != NULL) { 1303 bus_dmamap_sync(ring->data_dmat, data->map, 1304 BUS_DMASYNC_POSTWRITE); 1305 bus_dmamap_unload(ring->data_dmat, data->map); 1306 m_freem(data->m); 1307 } 1308 if (data->map != NULL) 1309 bus_dmamap_destroy(ring->data_dmat, data->map); 1310 } 1311 if (ring->data_dmat != NULL) { 1312 bus_dma_tag_destroy(ring->data_dmat); 1313 ring->data_dmat = NULL; 1314 } 1315} 1316 1317/* 1318 * Extract various information from EEPROM. 1319 */ 1320static int 1321wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN]) 1322{ 1323#define WPI_CHK(res) do { \ 1324 if ((error = res) != 0) \ 1325 goto fail; \ 1326} while (0) 1327 uint8_t i; 1328 int error; 1329 1330 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1331 1332 /* Adapter has to be powered on for EEPROM access to work. */ 1333 if ((error = wpi_apm_init(sc)) != 0) { 1334 device_printf(sc->sc_dev, 1335 "%s: could not power ON adapter, error %d\n", __func__, 1336 error); 1337 return error; 1338 } 1339 1340 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) { 1341 device_printf(sc->sc_dev, "bad EEPROM signature\n"); 1342 error = EIO; 1343 goto fail; 1344 } 1345 /* Clear HW ownership of EEPROM. */ 1346 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER); 1347 1348 /* Read the hardware capabilities, revision and SKU type. */ 1349 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap, 1350 sizeof(sc->cap))); 1351 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 1352 sizeof(sc->rev))); 1353 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1354 sizeof(sc->type))); 1355 1356 sc->rev = le16toh(sc->rev); 1357 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap, 1358 sc->rev, sc->type); 1359 1360 /* Read the regulatory domain (4 ASCII characters.) */ 1361 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 1362 sizeof(sc->domain))); 1363 1364 /* Read MAC address. */ 1365 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr, 1366 IEEE80211_ADDR_LEN)); 1367 1368 /* Read the list of authorized channels. */ 1369 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++) 1370 WPI_CHK(wpi_read_eeprom_channels(sc, i)); 1371 1372 /* Read the list of TX power groups. */ 1373 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++) 1374 WPI_CHK(wpi_read_eeprom_group(sc, i)); 1375 1376fail: wpi_apm_stop(sc); /* Power OFF adapter. */ 1377 1378 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END, 1379 __func__); 1380 1381 return error; 1382#undef WPI_CHK 1383} 1384 1385/* 1386 * Translate EEPROM flags to net80211. 1387 */ 1388static uint32_t 1389wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel) 1390{ 1391 uint32_t nflags; 1392 1393 nflags = 0; 1394 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0) 1395 nflags |= IEEE80211_CHAN_PASSIVE; 1396 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0) 1397 nflags |= IEEE80211_CHAN_NOADHOC; 1398 if (channel->flags & WPI_EEPROM_CHAN_RADAR) { 1399 nflags |= IEEE80211_CHAN_DFS; 1400 /* XXX apparently IBSS may still be marked */ 1401 nflags |= IEEE80211_CHAN_NOADHOC; 1402 } 1403 1404 /* XXX HOSTAP uses WPI_MODE_IBSS */ 1405 if (nflags & IEEE80211_CHAN_NOADHOC) 1406 nflags |= IEEE80211_CHAN_NOHOSTAP; 1407 1408 return nflags; 1409} 1410 1411static void 1412wpi_read_eeprom_band(struct wpi_softc *sc, uint8_t n, int maxchans, 1413 int *nchans, struct ieee80211_channel chans[]) 1414{ 1415 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n]; 1416 const struct wpi_chan_band *band = &wpi_bands[n]; 1417 uint32_t nflags; 1418 uint8_t bands[IEEE80211_MODE_BYTES]; 1419 uint8_t chan, i; 1420 int error; 1421 1422 memset(bands, 0, sizeof(bands)); 1423 1424 if (n == 0) { 1425 setbit(bands, IEEE80211_MODE_11B); 1426 setbit(bands, IEEE80211_MODE_11G); 1427 } else 1428 setbit(bands, IEEE80211_MODE_11A); 1429 1430 for (i = 0; i < band->nchan; i++) { 1431 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) { 1432 DPRINTF(sc, WPI_DEBUG_EEPROM, 1433 "Channel Not Valid: %d, band %d\n", 1434 band->chan[i],n); 1435 continue; 1436 } 1437 1438 chan = band->chan[i]; 1439 nflags = wpi_eeprom_channel_flags(&channels[i]); 1440 error = ieee80211_add_channel(chans, maxchans, nchans, 1441 chan, 0, channels[i].maxpwr, nflags, bands); 1442 if (error != 0) 1443 break; 1444 1445 /* Save maximum allowed TX power for this channel. */ 1446 sc->maxpwr[chan] = channels[i].maxpwr; 1447 1448 DPRINTF(sc, WPI_DEBUG_EEPROM, 1449 "adding chan %d flags=0x%x maxpwr=%d, offset %d\n", 1450 chan, channels[i].flags, sc->maxpwr[chan], *nchans); 1451 } 1452} 1453 1454/** 1455 * Read the eeprom to find out what channels are valid for the given 1456 * band and update net80211 with what we find. 1457 */ 1458static int 1459wpi_read_eeprom_channels(struct wpi_softc *sc, uint8_t n) 1460{ 1461 struct ieee80211com *ic = &sc->sc_ic; 1462 const struct wpi_chan_band *band = &wpi_bands[n]; 1463 int error; 1464 1465 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1466 1467 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n], 1468 band->nchan * sizeof (struct wpi_eeprom_chan)); 1469 if (error != 0) { 1470 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1471 return error; 1472 } 1473 1474 wpi_read_eeprom_band(sc, n, IEEE80211_CHAN_MAX, &ic->ic_nchans, 1475 ic->ic_channels); 1476 1477 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 1478 1479 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 1480 1481 return 0; 1482} 1483 1484static struct wpi_eeprom_chan * 1485wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c) 1486{ 1487 int i, j; 1488 1489 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++) 1490 for (i = 0; i < wpi_bands[j].nchan; i++) 1491 if (wpi_bands[j].chan[i] == c->ic_ieee && 1492 ((j == 0) ^ IEEE80211_IS_CHAN_A(c)) == 1) 1493 return &sc->eeprom_channels[j][i]; 1494 1495 return NULL; 1496} 1497 1498static void 1499wpi_getradiocaps(struct ieee80211com *ic, 1500 int maxchans, int *nchans, struct ieee80211_channel chans[]) 1501{ 1502 struct wpi_softc *sc = ic->ic_softc; 1503 int i; 1504 1505 /* Parse the list of authorized channels. */ 1506 for (i = 0; i < WPI_CHAN_BANDS_COUNT && *nchans < maxchans; i++) 1507 wpi_read_eeprom_band(sc, i, maxchans, nchans, chans); 1508} 1509 1510/* 1511 * Enforce flags read from EEPROM. 1512 */ 1513static int 1514wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd, 1515 int nchan, struct ieee80211_channel chans[]) 1516{ 1517 struct wpi_softc *sc = ic->ic_softc; 1518 int i; 1519 1520 for (i = 0; i < nchan; i++) { 1521 struct ieee80211_channel *c = &chans[i]; 1522 struct wpi_eeprom_chan *channel; 1523 1524 channel = wpi_find_eeprom_channel(sc, c); 1525 if (channel == NULL) { 1526 ic_printf(ic, "%s: invalid channel %u freq %u/0x%x\n", 1527 __func__, c->ic_ieee, c->ic_freq, c->ic_flags); 1528 return EINVAL; 1529 } 1530 c->ic_flags |= wpi_eeprom_channel_flags(channel); 1531 } 1532 1533 return 0; 1534} 1535 1536static int 1537wpi_read_eeprom_group(struct wpi_softc *sc, uint8_t n) 1538{ 1539 struct wpi_power_group *group = &sc->groups[n]; 1540 struct wpi_eeprom_group rgroup; 1541 int i, error; 1542 1543 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1544 1545 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, 1546 &rgroup, sizeof rgroup)) != 0) { 1547 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1548 return error; 1549 } 1550 1551 /* Save TX power group information. */ 1552 group->chan = rgroup.chan; 1553 group->maxpwr = rgroup.maxpwr; 1554 /* Retrieve temperature at which the samples were taken. */ 1555 group->temp = (int16_t)le16toh(rgroup.temp); 1556 1557 DPRINTF(sc, WPI_DEBUG_EEPROM, 1558 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan, 1559 group->maxpwr, group->temp); 1560 1561 for (i = 0; i < WPI_SAMPLES_COUNT; i++) { 1562 group->samples[i].index = rgroup.samples[i].index; 1563 group->samples[i].power = rgroup.samples[i].power; 1564 1565 DPRINTF(sc, WPI_DEBUG_EEPROM, 1566 "\tsample %d: index=%d power=%d\n", i, 1567 group->samples[i].index, group->samples[i].power); 1568 } 1569 1570 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 1571 1572 return 0; 1573} 1574 1575static __inline uint8_t 1576wpi_add_node_entry_adhoc(struct wpi_softc *sc) 1577{ 1578 uint8_t newid = WPI_ID_IBSS_MIN; 1579 1580 for (; newid <= WPI_ID_IBSS_MAX; newid++) { 1581 if ((sc->nodesmsk & (1 << newid)) == 0) { 1582 sc->nodesmsk |= 1 << newid; 1583 return newid; 1584 } 1585 } 1586 1587 return WPI_ID_UNDEFINED; 1588} 1589 1590static __inline uint8_t 1591wpi_add_node_entry_sta(struct wpi_softc *sc) 1592{ 1593 sc->nodesmsk |= 1 << WPI_ID_BSS; 1594 1595 return WPI_ID_BSS; 1596} 1597 1598static __inline int 1599wpi_check_node_entry(struct wpi_softc *sc, uint8_t id) 1600{ 1601 if (id == WPI_ID_UNDEFINED) 1602 return 0; 1603 1604 return (sc->nodesmsk >> id) & 1; 1605} 1606 1607static __inline void 1608wpi_clear_node_table(struct wpi_softc *sc) 1609{ 1610 sc->nodesmsk = 0; 1611} 1612 1613static __inline void 1614wpi_del_node_entry(struct wpi_softc *sc, uint8_t id) 1615{ 1616 sc->nodesmsk &= ~(1 << id); 1617} 1618 1619static struct ieee80211_node * 1620wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 1621{ 1622 struct wpi_node *wn; 1623 1624 wn = malloc(sizeof (struct wpi_node), M_80211_NODE, 1625 M_NOWAIT | M_ZERO); 1626 1627 if (wn == NULL) 1628 return NULL; 1629 1630 wn->id = WPI_ID_UNDEFINED; 1631 1632 return &wn->ni; 1633} 1634 1635static void 1636wpi_node_free(struct ieee80211_node *ni) 1637{ 1638 struct wpi_softc *sc = ni->ni_ic->ic_softc; 1639 struct wpi_node *wn = WPI_NODE(ni); 1640 1641 if (wn->id != WPI_ID_UNDEFINED) { 1642 WPI_NT_LOCK(sc); 1643 if (wpi_check_node_entry(sc, wn->id)) { 1644 wpi_del_node_entry(sc, wn->id); 1645 wpi_del_node(sc, ni); 1646 } 1647 WPI_NT_UNLOCK(sc); 1648 } 1649 1650 sc->sc_node_free(ni); 1651} 1652 1653static __inline int 1654wpi_check_bss_filter(struct wpi_softc *sc) 1655{ 1656 return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0; 1657} 1658 1659static void 1660wpi_ibss_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype, 1661 const struct ieee80211_rx_stats *rxs, 1662 int rssi, int nf) 1663{ 1664 struct ieee80211vap *vap = ni->ni_vap; 1665 struct wpi_softc *sc = vap->iv_ic->ic_softc; 1666 struct wpi_vap *wvp = WPI_VAP(vap); 1667 uint64_t ni_tstamp, rx_tstamp; 1668 1669 wvp->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf); 1670 1671 if (vap->iv_state == IEEE80211_S_RUN && 1672 (subtype == IEEE80211_FC0_SUBTYPE_BEACON || 1673 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) { 1674 ni_tstamp = le64toh(ni->ni_tstamp.tsf); 1675 rx_tstamp = le64toh(sc->rx_tstamp); 1676 1677 if (ni_tstamp >= rx_tstamp) { 1678 DPRINTF(sc, WPI_DEBUG_STATE, 1679 "ibss merge, tsf %ju tstamp %ju\n", 1680 (uintmax_t)rx_tstamp, (uintmax_t)ni_tstamp); 1681 (void) ieee80211_ibss_merge(ni); 1682 } 1683 } 1684} 1685 1686static void 1687wpi_restore_node(void *arg, struct ieee80211_node *ni) 1688{ 1689 struct wpi_softc *sc = arg; 1690 struct wpi_node *wn = WPI_NODE(ni); 1691 int error; 1692 1693 WPI_NT_LOCK(sc); 1694 if (wn->id != WPI_ID_UNDEFINED) { 1695 wn->id = WPI_ID_UNDEFINED; 1696 if ((error = wpi_add_ibss_node(sc, ni)) != 0) { 1697 device_printf(sc->sc_dev, 1698 "%s: could not add IBSS node, error %d\n", 1699 __func__, error); 1700 } 1701 } 1702 WPI_NT_UNLOCK(sc); 1703} 1704 1705static void 1706wpi_restore_node_table(struct wpi_softc *sc, struct wpi_vap *wvp) 1707{ 1708 struct ieee80211com *ic = &sc->sc_ic; 1709 1710 /* Set group keys once. */ 1711 WPI_NT_LOCK(sc); 1712 wvp->wv_gtk = 0; 1713 WPI_NT_UNLOCK(sc); 1714 1715 ieee80211_iterate_nodes(&ic->ic_sta, wpi_restore_node, sc); 1716 ieee80211_crypto_reload_keys(ic); 1717} 1718 1719/** 1720 * Called by net80211 when ever there is a change to 80211 state machine 1721 */ 1722static int 1723wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1724{ 1725 struct wpi_vap *wvp = WPI_VAP(vap); 1726 struct ieee80211com *ic = vap->iv_ic; 1727 struct wpi_softc *sc = ic->ic_softc; 1728 int error = 0; 1729 1730 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 1731 1732 WPI_TXQ_LOCK(sc); 1733 if (nstate > IEEE80211_S_INIT && sc->sc_running == 0) { 1734 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1735 WPI_TXQ_UNLOCK(sc); 1736 1737 return ENXIO; 1738 } 1739 WPI_TXQ_UNLOCK(sc); 1740 1741 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__, 1742 ieee80211_state_name[vap->iv_state], 1743 ieee80211_state_name[nstate]); 1744 1745 if (vap->iv_state == IEEE80211_S_RUN && nstate < IEEE80211_S_RUN) { 1746 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) { 1747 device_printf(sc->sc_dev, 1748 "%s: could not set power saving level\n", 1749 __func__); 1750 return error; 1751 } 1752 1753 wpi_set_led(sc, WPI_LED_LINK, 1, 0); 1754 } 1755 1756 switch (nstate) { 1757 case IEEE80211_S_SCAN: 1758 WPI_RXON_LOCK(sc); 1759 if (wpi_check_bss_filter(sc) != 0) { 1760 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS); 1761 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 1762 device_printf(sc->sc_dev, 1763 "%s: could not send RXON\n", __func__); 1764 } 1765 } 1766 WPI_RXON_UNLOCK(sc); 1767 break; 1768 1769 case IEEE80211_S_ASSOC: 1770 if (vap->iv_state != IEEE80211_S_RUN) 1771 break; 1772 /* FALLTHROUGH */ 1773 case IEEE80211_S_AUTH: 1774 /* 1775 * NB: do not optimize AUTH -> AUTH state transmission - 1776 * this will break powersave with non-QoS AP! 1777 */ 1778 1779 /* 1780 * The node must be registered in the firmware before auth. 1781 * Also the associd must be cleared on RUN -> ASSOC 1782 * transitions. 1783 */ 1784 if ((error = wpi_auth(sc, vap)) != 0) { 1785 device_printf(sc->sc_dev, 1786 "%s: could not move to AUTH state, error %d\n", 1787 __func__, error); 1788 } 1789 break; 1790 1791 case IEEE80211_S_RUN: 1792 /* 1793 * RUN -> RUN transition: 1794 * STA mode: Just restart the timers. 1795 * IBSS mode: Process IBSS merge. 1796 */ 1797 if (vap->iv_state == IEEE80211_S_RUN) { 1798 if (vap->iv_opmode != IEEE80211_M_IBSS) { 1799 WPI_RXON_LOCK(sc); 1800 wpi_calib_timeout(sc); 1801 WPI_RXON_UNLOCK(sc); 1802 break; 1803 } else { 1804 /* 1805 * Drop the BSS_FILTER bit 1806 * (there is no another way to change bssid). 1807 */ 1808 WPI_RXON_LOCK(sc); 1809 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS); 1810 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 1811 device_printf(sc->sc_dev, 1812 "%s: could not send RXON\n", 1813 __func__); 1814 } 1815 WPI_RXON_UNLOCK(sc); 1816 1817 /* Restore all what was lost. */ 1818 wpi_restore_node_table(sc, wvp); 1819 1820 /* XXX set conditionally? */ 1821 wpi_updateedca(ic); 1822 } 1823 } 1824 1825 /* 1826 * !RUN -> RUN requires setting the association id 1827 * which is done with a firmware cmd. We also defer 1828 * starting the timers until that work is done. 1829 */ 1830 if ((error = wpi_run(sc, vap)) != 0) { 1831 device_printf(sc->sc_dev, 1832 "%s: could not move to RUN state\n", __func__); 1833 } 1834 break; 1835 1836 default: 1837 break; 1838 } 1839 if (error != 0) { 1840 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 1841 return error; 1842 } 1843 1844 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 1845 1846 return wvp->wv_newstate(vap, nstate, arg); 1847} 1848 1849static void 1850wpi_calib_timeout(void *arg) 1851{ 1852 struct wpi_softc *sc = arg; 1853 1854 if (wpi_check_bss_filter(sc) == 0) 1855 return; 1856 1857 wpi_power_calibration(sc); 1858 1859 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc); 1860} 1861 1862static __inline uint8_t 1863rate2plcp(const uint8_t rate) 1864{ 1865 switch (rate) { 1866 case 12: return 0xd; 1867 case 18: return 0xf; 1868 case 24: return 0x5; 1869 case 36: return 0x7; 1870 case 48: return 0x9; 1871 case 72: return 0xb; 1872 case 96: return 0x1; 1873 case 108: return 0x3; 1874 case 2: return 10; 1875 case 4: return 20; 1876 case 11: return 55; 1877 case 22: return 110; 1878 default: return 0; 1879 } 1880} 1881 1882static __inline uint8_t 1883plcp2rate(const uint8_t plcp) 1884{ 1885 switch (plcp) { 1886 case 0xd: return 12; 1887 case 0xf: return 18; 1888 case 0x5: return 24; 1889 case 0x7: return 36; 1890 case 0x9: return 48; 1891 case 0xb: return 72; 1892 case 0x1: return 96; 1893 case 0x3: return 108; 1894 case 10: return 2; 1895 case 20: return 4; 1896 case 55: return 11; 1897 case 110: return 22; 1898 default: return 0; 1899 } 1900} 1901 1902/* Quickly determine if a given rate is CCK or OFDM. */ 1903#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1904 1905static void 1906wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc, 1907 struct wpi_rx_data *data) 1908{ 1909 struct ieee80211com *ic = &sc->sc_ic; 1910 struct wpi_rx_ring *ring = &sc->rxq; 1911 struct wpi_rx_stat *stat; 1912 struct wpi_rx_head *head; 1913 struct wpi_rx_tail *tail; 1914 struct ieee80211_frame *wh; 1915 struct ieee80211_node *ni; 1916 struct mbuf *m, *m1; 1917 bus_addr_t paddr; 1918 uint32_t flags; 1919 uint16_t len; 1920 int error; 1921 1922 stat = (struct wpi_rx_stat *)(desc + 1); 1923 1924 if (__predict_false(stat->len > WPI_STAT_MAXLEN)) { 1925 device_printf(sc->sc_dev, "invalid RX statistic header\n"); 1926 goto fail1; 1927 } 1928 1929 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD); 1930 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len); 1931 len = le16toh(head->len); 1932 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len); 1933 flags = le32toh(tail->flags); 1934 1935 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d" 1936 " rate %x chan %d tstamp %ju\n", __func__, ring->cur, 1937 le32toh(desc->len), len, (int8_t)stat->rssi, 1938 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp)); 1939 1940 /* Discard frames with a bad FCS early. */ 1941 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) { 1942 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n", 1943 __func__, flags); 1944 goto fail1; 1945 } 1946 /* Discard frames that are too short. */ 1947 if (len < sizeof (struct ieee80211_frame_ack)) { 1948 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n", 1949 __func__, len); 1950 goto fail1; 1951 } 1952 1953 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 1954 if (__predict_false(m1 == NULL)) { 1955 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n", 1956 __func__); 1957 goto fail1; 1958 } 1959 bus_dmamap_unload(ring->data_dmat, data->map); 1960 1961 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *), 1962 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 1963 if (__predict_false(error != 0 && error != EFBIG)) { 1964 device_printf(sc->sc_dev, 1965 "%s: bus_dmamap_load failed, error %d\n", __func__, error); 1966 m_freem(m1); 1967 1968 /* Try to reload the old mbuf. */ 1969 error = bus_dmamap_load(ring->data_dmat, data->map, 1970 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr, 1971 &paddr, BUS_DMA_NOWAIT); 1972 if (error != 0 && error != EFBIG) { 1973 panic("%s: could not load old RX mbuf", __func__); 1974 } 1975 /* Physical address may have changed. */ 1976 ring->desc[ring->cur] = htole32(paddr); 1977 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map, 1978 BUS_DMASYNC_PREWRITE); 1979 goto fail1; 1980 } 1981 1982 m = data->m; 1983 data->m = m1; 1984 /* Update RX descriptor. */ 1985 ring->desc[ring->cur] = htole32(paddr); 1986 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1987 BUS_DMASYNC_PREWRITE); 1988 1989 /* Finalize mbuf. */ 1990 m->m_data = (caddr_t)(head + 1); 1991 m->m_pkthdr.len = m->m_len = len; 1992 1993 /* Grab a reference to the source node. */ 1994 wh = mtod(m, struct ieee80211_frame *); 1995 1996 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 1997 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) { 1998 /* Check whether decryption was successful or not. */ 1999 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) { 2000 DPRINTF(sc, WPI_DEBUG_RECV, 2001 "CCMP decryption failed 0x%x\n", flags); 2002 goto fail2; 2003 } 2004 m->m_flags |= M_WEP; 2005 } 2006 2007 if (len >= sizeof(struct ieee80211_frame_min)) 2008 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 2009 else 2010 ni = NULL; 2011 2012 sc->rx_tstamp = tail->tstamp; 2013 2014 if (ieee80211_radiotap_active(ic)) { 2015 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap; 2016 2017 tap->wr_flags = 0; 2018 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE)) 2019 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2020 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET); 2021 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET; 2022 tap->wr_tsft = tail->tstamp; 2023 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf; 2024 tap->wr_rate = plcp2rate(head->plcp); 2025 } 2026 2027 WPI_UNLOCK(sc); 2028 2029 /* Send the frame to the 802.11 layer. */ 2030 if (ni != NULL) { 2031 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET); 2032 /* Node is no longer needed. */ 2033 ieee80211_free_node(ni); 2034 } else 2035 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET); 2036 2037 WPI_LOCK(sc); 2038 2039 return; 2040 2041fail2: m_freem(m); 2042 2043fail1: counter_u64_add(ic->ic_ierrors, 1); 2044} 2045 2046static void 2047wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc, 2048 struct wpi_rx_data *data) 2049{ 2050 /* Ignore */ 2051} 2052 2053static void 2054wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc) 2055{ 2056 struct ieee80211_ratectl_tx_status *txs = &sc->sc_txs; 2057 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3]; 2058 struct wpi_tx_data *data = &ring->data[desc->idx]; 2059 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1); 2060 struct mbuf *m; 2061 struct ieee80211_node *ni; 2062 uint32_t status = le32toh(stat->status); 2063 2064 KASSERT(data->ni != NULL, ("no node")); 2065 KASSERT(data->m != NULL, ("no mbuf")); 2066 2067 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 2068 2069 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: " 2070 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d " 2071 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt, 2072 stat->btkillcnt, stat->rate, le32toh(stat->duration), status); 2073 2074 /* Unmap and free mbuf. */ 2075 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE); 2076 bus_dmamap_unload(ring->data_dmat, data->map); 2077 m = data->m, data->m = NULL; 2078 ni = data->ni, data->ni = NULL; 2079 2080 /* Restore frame header. */ 2081 KASSERT(M_LEADINGSPACE(m) >= data->hdrlen, ("no frame header!")); 2082 M_PREPEND(m, data->hdrlen, M_NOWAIT); 2083 KASSERT(m != NULL, ("%s: m is NULL\n", __func__)); 2084 2085 /* 2086 * Update rate control statistics for the node. 2087 */ 2088 txs->pktlen = m->m_pkthdr.len; 2089 txs->short_retries = stat->rtsfailcnt; 2090 txs->long_retries = stat->ackfailcnt / WPI_NTRIES_DEFAULT; 2091 if (!(status & WPI_TX_STATUS_FAIL)) 2092 txs->status = IEEE80211_RATECTL_TX_SUCCESS; 2093 else { 2094 switch (status & 0xff) { 2095 case WPI_TX_STATUS_FAIL_SHORT_LIMIT: 2096 txs->status = IEEE80211_RATECTL_TX_FAIL_SHORT; 2097 break; 2098 case WPI_TX_STATUS_FAIL_LONG_LIMIT: 2099 txs->status = IEEE80211_RATECTL_TX_FAIL_LONG; 2100 break; 2101 case WPI_TX_STATUS_FAIL_LIFE_EXPIRE: 2102 txs->status = IEEE80211_RATECTL_TX_FAIL_EXPIRED; 2103 break; 2104 default: 2105 txs->status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED; 2106 break; 2107 } 2108 } 2109 2110 ieee80211_ratectl_tx_complete(ni, txs); 2111 ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0); 2112 2113 WPI_TXQ_STATE_LOCK(sc); 2114 if (--ring->queued > 0) 2115 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc); 2116 else 2117 callout_stop(&sc->tx_timeout); 2118 WPI_TXQ_STATE_UNLOCK(sc); 2119 2120 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 2121} 2122 2123/* 2124 * Process a "command done" firmware notification. This is where we wakeup 2125 * processes waiting for a synchronous command completion. 2126 */ 2127static void 2128wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc) 2129{ 2130 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM]; 2131 struct wpi_tx_data *data; 2132 struct wpi_tx_cmd *cmd; 2133 2134 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x " 2135 "type %s len %d\n", desc->qid, desc->idx, 2136 desc->flags, wpi_cmd_str(desc->type), 2137 le32toh(desc->len)); 2138 2139 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM) 2140 return; /* Not a command ack. */ 2141 2142 KASSERT(ring->queued == 0, ("ring->queued must be 0")); 2143 2144 data = &ring->data[desc->idx]; 2145 cmd = &ring->cmd[desc->idx]; 2146 2147 /* If the command was mapped in an mbuf, free it. */ 2148 if (data->m != NULL) { 2149 bus_dmamap_sync(ring->data_dmat, data->map, 2150 BUS_DMASYNC_POSTWRITE); 2151 bus_dmamap_unload(ring->data_dmat, data->map); 2152 m_freem(data->m); 2153 data->m = NULL; 2154 } 2155 2156 wakeup(cmd); 2157 2158 if (desc->type == WPI_CMD_SET_POWER_MODE) { 2159 struct wpi_pmgt_cmd *pcmd = (struct wpi_pmgt_cmd *)cmd->data; 2160 2161 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 2162 BUS_DMASYNC_POSTREAD); 2163 2164 WPI_TXQ_LOCK(sc); 2165 if (le16toh(pcmd->flags) & WPI_PS_ALLOW_SLEEP) { 2166 sc->sc_update_rx_ring = wpi_update_rx_ring_ps; 2167 sc->sc_update_tx_ring = wpi_update_tx_ring_ps; 2168 } else { 2169 sc->sc_update_rx_ring = wpi_update_rx_ring; 2170 sc->sc_update_tx_ring = wpi_update_tx_ring; 2171 } 2172 WPI_TXQ_UNLOCK(sc); 2173 } 2174} 2175 2176static void 2177wpi_notif_intr(struct wpi_softc *sc) 2178{ 2179 struct ieee80211com *ic = &sc->sc_ic; 2180 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2181 uint32_t hw; 2182 2183 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map, 2184 BUS_DMASYNC_POSTREAD); 2185 2186 hw = le32toh(sc->shared->next) & 0xfff; 2187 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 2188 2189 while (sc->rxq.cur != hw) { 2190 struct wpi_rx_data *data; 2191 struct wpi_rx_desc *desc; 2192 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT; 2193 2194 data = &sc->rxq.data[sc->rxq.cur]; 2195 2196 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2197 BUS_DMASYNC_POSTREAD); 2198 desc = mtod(data->m, struct wpi_rx_desc *); 2199 2200 DPRINTF(sc, WPI_DEBUG_NOTIFY, 2201 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n", 2202 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags, 2203 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len)); 2204 2205 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) { 2206 /* Reply to a command. */ 2207 wpi_cmd_done(sc, desc); 2208 } 2209 2210 switch (desc->type) { 2211 case WPI_RX_DONE: 2212 /* An 802.11 frame has been received. */ 2213 wpi_rx_done(sc, desc, data); 2214 2215 if (__predict_false(sc->sc_running == 0)) { 2216 /* wpi_stop() was called. */ 2217 return; 2218 } 2219 2220 break; 2221 2222 case WPI_TX_DONE: 2223 /* An 802.11 frame has been transmitted. */ 2224 wpi_tx_done(sc, desc); 2225 break; 2226 2227 case WPI_RX_STATISTICS: 2228 case WPI_BEACON_STATISTICS: 2229 wpi_rx_statistics(sc, desc, data); 2230 break; 2231 2232 case WPI_BEACON_MISSED: 2233 { 2234 struct wpi_beacon_missed *miss = 2235 (struct wpi_beacon_missed *)(desc + 1); 2236 uint32_t expected, misses, received, threshold; 2237 2238 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2239 BUS_DMASYNC_POSTREAD); 2240 2241 misses = le32toh(miss->consecutive); 2242 expected = le32toh(miss->expected); 2243 received = le32toh(miss->received); 2244 threshold = MAX(2, vap->iv_bmissthreshold); 2245 2246 DPRINTF(sc, WPI_DEBUG_BMISS, 2247 "%s: beacons missed %u(%u) (received %u/%u)\n", 2248 __func__, misses, le32toh(miss->total), received, 2249 expected); 2250 2251 if (misses >= threshold || 2252 (received == 0 && expected >= threshold)) { 2253 WPI_RXON_LOCK(sc); 2254 if (callout_pending(&sc->scan_timeout)) { 2255 wpi_cmd(sc, WPI_CMD_SCAN_ABORT, NULL, 2256 0, 1); 2257 } 2258 WPI_RXON_UNLOCK(sc); 2259 if (vap->iv_state == IEEE80211_S_RUN && 2260 (ic->ic_flags & IEEE80211_F_SCAN) == 0) 2261 ieee80211_beacon_miss(ic); 2262 } 2263 2264 break; 2265 } 2266#ifdef WPI_DEBUG 2267 case WPI_BEACON_SENT: 2268 { 2269 struct wpi_tx_stat *stat = 2270 (struct wpi_tx_stat *)(desc + 1); 2271 uint64_t *tsf = (uint64_t *)(stat + 1); 2272 uint32_t *mode = (uint32_t *)(tsf + 1); 2273 2274 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2275 BUS_DMASYNC_POSTREAD); 2276 2277 DPRINTF(sc, WPI_DEBUG_BEACON, 2278 "beacon sent: rts %u, ack %u, btkill %u, rate %u, " 2279 "duration %u, status %x, tsf %ju, mode %x\n", 2280 stat->rtsfailcnt, stat->ackfailcnt, 2281 stat->btkillcnt, stat->rate, le32toh(stat->duration), 2282 le32toh(stat->status), le64toh(*tsf), 2283 le32toh(*mode)); 2284 2285 break; 2286 } 2287#endif 2288 case WPI_UC_READY: 2289 { 2290 struct wpi_ucode_info *uc = 2291 (struct wpi_ucode_info *)(desc + 1); 2292 2293 /* The microcontroller is ready. */ 2294 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2295 BUS_DMASYNC_POSTREAD); 2296 DPRINTF(sc, WPI_DEBUG_RESET, 2297 "microcode alive notification version=%d.%d " 2298 "subtype=%x alive=%x\n", uc->major, uc->minor, 2299 uc->subtype, le32toh(uc->valid)); 2300 2301 if (le32toh(uc->valid) != 1) { 2302 device_printf(sc->sc_dev, 2303 "microcontroller initialization failed\n"); 2304 wpi_stop_locked(sc); 2305 return; 2306 } 2307 /* Save the address of the error log in SRAM. */ 2308 sc->errptr = le32toh(uc->errptr); 2309 break; 2310 } 2311 case WPI_STATE_CHANGED: 2312 { 2313 uint32_t *status; 2314 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2315 BUS_DMASYNC_POSTREAD); 2316 2317 status = (uint32_t *)(desc + 1); 2318 2319 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n", 2320 le32toh(*status)); 2321 2322 if (le32toh(*status) & 1) { 2323 WPI_NT_LOCK(sc); 2324 wpi_clear_node_table(sc); 2325 WPI_NT_UNLOCK(sc); 2326 ieee80211_runtask(ic, 2327 &sc->sc_radiooff_task); 2328 return; 2329 } 2330 break; 2331 } 2332#ifdef WPI_DEBUG 2333 case WPI_START_SCAN: 2334 { 2335 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2336 BUS_DMASYNC_POSTREAD); 2337 2338 struct wpi_start_scan *scan = 2339 (struct wpi_start_scan *)(desc + 1); 2340 DPRINTF(sc, WPI_DEBUG_SCAN, 2341 "%s: scanning channel %d status %x\n", 2342 __func__, scan->chan, le32toh(scan->status)); 2343 2344 break; 2345 } 2346#endif 2347 case WPI_STOP_SCAN: 2348 { 2349 struct wpi_stop_scan *scan; 2350 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2351 BUS_DMASYNC_POSTREAD); 2352 2353 scan = 2354 (struct wpi_stop_scan *)(desc + 1); 2355 2356 DPRINTF(sc, WPI_DEBUG_SCAN, 2357 "scan finished nchan=%d status=%d chan=%d\n", 2358 scan->nchan, scan->status, scan->chan); 2359 2360 WPI_RXON_LOCK(sc); 2361 callout_stop(&sc->scan_timeout); 2362 WPI_RXON_UNLOCK(sc); 2363 if (scan->status == WPI_SCAN_ABORTED) 2364 ieee80211_cancel_scan(vap); 2365 else 2366 ieee80211_scan_next(vap); 2367 break; 2368 } 2369 } 2370 2371 if (sc->rxq.cur % 8 == 0) { 2372 /* Tell the firmware what we have processed. */ 2373 sc->sc_update_rx_ring(sc); 2374 } 2375 } 2376} 2377 2378/* 2379 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up 2380 * from power-down sleep mode. 2381 */ 2382static void 2383wpi_wakeup_intr(struct wpi_softc *sc) 2384{ 2385 int qid; 2386 2387 DPRINTF(sc, WPI_DEBUG_PWRSAVE, 2388 "%s: ucode wakeup from power-down sleep\n", __func__); 2389 2390 /* Wakeup RX and TX rings. */ 2391 if (sc->rxq.update) { 2392 sc->rxq.update = 0; 2393 wpi_update_rx_ring(sc); 2394 } 2395 WPI_TXQ_LOCK(sc); 2396 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) { 2397 struct wpi_tx_ring *ring = &sc->txq[qid]; 2398 2399 if (ring->update) { 2400 ring->update = 0; 2401 wpi_update_tx_ring(sc, ring); 2402 } 2403 } 2404 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 2405 WPI_TXQ_UNLOCK(sc); 2406} 2407 2408/* 2409 * This function prints firmware registers 2410 */ 2411#ifdef WPI_DEBUG 2412static void 2413wpi_debug_registers(struct wpi_softc *sc) 2414{ 2415 size_t i; 2416 static const uint32_t csr_tbl[] = { 2417 WPI_HW_IF_CONFIG, 2418 WPI_INT, 2419 WPI_INT_MASK, 2420 WPI_FH_INT, 2421 WPI_GPIO_IN, 2422 WPI_RESET, 2423 WPI_GP_CNTRL, 2424 WPI_EEPROM, 2425 WPI_EEPROM_GP, 2426 WPI_GIO, 2427 WPI_UCODE_GP1, 2428 WPI_UCODE_GP2, 2429 WPI_GIO_CHICKEN, 2430 WPI_ANA_PLL, 2431 WPI_DBG_HPET_MEM, 2432 }; 2433 static const uint32_t prph_tbl[] = { 2434 WPI_APMG_CLK_CTRL, 2435 WPI_APMG_PS, 2436 WPI_APMG_PCI_STT, 2437 WPI_APMG_RFKILL, 2438 }; 2439 2440 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n"); 2441 2442 for (i = 0; i < nitems(csr_tbl); i++) { 2443 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ", 2444 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i])); 2445 2446 if ((i + 1) % 2 == 0) 2447 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n"); 2448 } 2449 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n"); 2450 2451 if (wpi_nic_lock(sc) == 0) { 2452 for (i = 0; i < nitems(prph_tbl); i++) { 2453 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ", 2454 wpi_get_prph_string(prph_tbl[i]), 2455 wpi_prph_read(sc, prph_tbl[i])); 2456 2457 if ((i + 1) % 2 == 0) 2458 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n"); 2459 } 2460 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n"); 2461 wpi_nic_unlock(sc); 2462 } else { 2463 DPRINTF(sc, WPI_DEBUG_REGISTER, 2464 "Cannot access internal registers.\n"); 2465 } 2466} 2467#endif 2468 2469/* 2470 * Dump the error log of the firmware when a firmware panic occurs. Although 2471 * we can't debug the firmware because it is neither open source nor free, it 2472 * can help us to identify certain classes of problems. 2473 */ 2474static void 2475wpi_fatal_intr(struct wpi_softc *sc) 2476{ 2477 struct wpi_fw_dump dump; 2478 uint32_t i, offset, count; 2479 2480 /* Check that the error log address is valid. */ 2481 if (sc->errptr < WPI_FW_DATA_BASE || 2482 sc->errptr + sizeof (dump) > 2483 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) { 2484 printf("%s: bad firmware error log address 0x%08x\n", __func__, 2485 sc->errptr); 2486 return; 2487 } 2488 if (wpi_nic_lock(sc) != 0) { 2489 printf("%s: could not read firmware error log\n", __func__); 2490 return; 2491 } 2492 /* Read number of entries in the log. */ 2493 count = wpi_mem_read(sc, sc->errptr); 2494 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) { 2495 printf("%s: invalid count field (count = %u)\n", __func__, 2496 count); 2497 wpi_nic_unlock(sc); 2498 return; 2499 } 2500 /* Skip "count" field. */ 2501 offset = sc->errptr + sizeof (uint32_t); 2502 printf("firmware error log (count = %u):\n", count); 2503 for (i = 0; i < count; i++) { 2504 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump, 2505 sizeof (dump) / sizeof (uint32_t)); 2506 2507 printf(" error type = \"%s\" (0x%08X)\n", 2508 (dump.desc < nitems(wpi_fw_errmsg)) ? 2509 wpi_fw_errmsg[dump.desc] : "UNKNOWN", 2510 dump.desc); 2511 printf(" error data = 0x%08X\n", 2512 dump.data); 2513 printf(" branch link = 0x%08X%08X\n", 2514 dump.blink[0], dump.blink[1]); 2515 printf(" interrupt link = 0x%08X%08X\n", 2516 dump.ilink[0], dump.ilink[1]); 2517 printf(" time = %u\n", dump.time); 2518 2519 offset += sizeof (dump); 2520 } 2521 wpi_nic_unlock(sc); 2522 /* Dump driver status (TX and RX rings) while we're here. */ 2523 printf("driver status:\n"); 2524 WPI_TXQ_LOCK(sc); 2525 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) { 2526 struct wpi_tx_ring *ring = &sc->txq[i]; 2527 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n", 2528 i, ring->qid, ring->cur, ring->queued); 2529 } 2530 WPI_TXQ_UNLOCK(sc); 2531 printf(" rx ring: cur=%d\n", sc->rxq.cur); 2532} 2533 2534static void 2535wpi_intr(void *arg) 2536{ 2537 struct wpi_softc *sc = arg; 2538 uint32_t r1, r2; 2539 2540 WPI_LOCK(sc); 2541 2542#if !defined(__HAIKU__) 2543 /* Disable interrupts. */ 2544 WPI_WRITE(sc, WPI_INT_MASK, 0); 2545 2546 r1 = WPI_READ(sc, WPI_INT); 2547 2548 if (__predict_false(r1 == 0xffffffff || 2549 (r1 & 0xfffffff0) == 0xa5a5a5a0)) 2550 goto end; /* Hardware gone! */ 2551 2552 r2 = WPI_READ(sc, WPI_FH_INT); 2553 2554 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__, 2555 r1, r2); 2556 2557 if (r1 == 0 && r2 == 0) 2558 goto done; /* Interrupt not for us. */ 2559#else 2560 r1 = atomic_get((int32 *)&sc->sc_intr_status_1); 2561 r2 = atomic_get((int32 *)&sc->sc_intr_status_2); 2562#endif 2563 2564 /* Acknowledge interrupts. */ 2565 WPI_WRITE(sc, WPI_INT, r1); 2566 WPI_WRITE(sc, WPI_FH_INT, r2); 2567 2568 if (__predict_false(r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR))) { 2569 struct ieee80211com *ic = &sc->sc_ic; 2570 2571 device_printf(sc->sc_dev, "fatal firmware error\n"); 2572#ifdef WPI_DEBUG 2573 wpi_debug_registers(sc); 2574#endif 2575 wpi_fatal_intr(sc); 2576 DPRINTF(sc, WPI_DEBUG_HW, 2577 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" : 2578 "(Hardware Error)"); 2579 ieee80211_restart_all(ic); 2580 goto end; 2581 } 2582 2583 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) || 2584 (r2 & WPI_FH_INT_RX)) 2585 wpi_notif_intr(sc); 2586 2587 if (r1 & WPI_INT_ALIVE) 2588 wakeup(sc); /* Firmware is alive. */ 2589 2590 if (r1 & WPI_INT_WAKEUP) 2591 wpi_wakeup_intr(sc); 2592 2593done: 2594 /* Re-enable interrupts. */ 2595 if (__predict_true(sc->sc_running)) 2596 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF); 2597 2598end: WPI_UNLOCK(sc); 2599} 2600 2601static void 2602wpi_free_txfrags(struct wpi_softc *sc, uint16_t ac) 2603{ 2604 struct wpi_tx_ring *ring; 2605 struct wpi_tx_data *data; 2606 uint8_t cur; 2607 2608 WPI_TXQ_LOCK(sc); 2609 ring = &sc->txq[ac]; 2610 2611 while (ring->pending != 0) { 2612 ring->pending--; 2613 cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT; 2614 data = &ring->data[cur]; 2615 2616 bus_dmamap_sync(ring->data_dmat, data->map, 2617 BUS_DMASYNC_POSTWRITE); 2618 bus_dmamap_unload(ring->data_dmat, data->map); 2619 m_freem(data->m); 2620 data->m = NULL; 2621 2622 ieee80211_node_decref(data->ni); 2623 data->ni = NULL; 2624 } 2625 2626 WPI_TXQ_UNLOCK(sc); 2627} 2628 2629static int 2630wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf) 2631{ 2632 struct ieee80211_frame *wh; 2633 struct wpi_tx_cmd *cmd; 2634 struct wpi_tx_data *data; 2635 struct wpi_tx_desc *desc; 2636 struct wpi_tx_ring *ring; 2637 struct mbuf *m1; 2638 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER]; 2639 uint8_t cur, pad; 2640 uint16_t hdrlen; 2641 int error, i, nsegs, totlen, frag; 2642 2643 WPI_TXQ_LOCK(sc); 2644 2645 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow")); 2646 2647 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 2648 2649 if (__predict_false(sc->sc_running == 0)) { 2650 /* wpi_stop() was called */ 2651 error = ENETDOWN; 2652 goto end; 2653 } 2654 2655 wh = mtod(buf->m, struct ieee80211_frame *); 2656 hdrlen = ieee80211_anyhdrsize(wh); 2657 totlen = buf->m->m_pkthdr.len; 2658 frag = ((buf->m->m_flags & (M_FRAG | M_LASTFRAG)) == M_FRAG); 2659 2660 if (__predict_false(totlen < sizeof(struct ieee80211_frame_min))) { 2661 error = EINVAL; 2662 goto end; 2663 } 2664 2665 if (hdrlen & 3) { 2666 /* First segment length must be a multiple of 4. */ 2667 pad = 4 - (hdrlen & 3); 2668 } else 2669 pad = 0; 2670 2671 ring = &sc->txq[buf->ac]; 2672 cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT; 2673 desc = &ring->desc[cur]; 2674 data = &ring->data[cur]; 2675 2676 /* Prepare TX firmware command. */ 2677 cmd = &ring->cmd[cur]; 2678 cmd->code = buf->code; 2679 cmd->flags = 0; 2680 cmd->qid = ring->qid; 2681 cmd->idx = cur; 2682 2683 memcpy(cmd->data, buf->data, buf->size); 2684 2685 /* Save and trim IEEE802.11 header. */ 2686 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen); 2687 m_adj(buf->m, hdrlen); 2688 2689 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m, 2690 segs, &nsegs, BUS_DMA_NOWAIT); 2691 if (error != 0 && error != EFBIG) { 2692 device_printf(sc->sc_dev, 2693 "%s: can't map mbuf (error %d)\n", __func__, error); 2694 goto end; 2695 } 2696 if (error != 0) { 2697 /* Too many DMA segments, linearize mbuf. */ 2698 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1); 2699 if (m1 == NULL) { 2700 device_printf(sc->sc_dev, 2701 "%s: could not defrag mbuf\n", __func__); 2702 error = ENOBUFS; 2703 goto end; 2704 } 2705 buf->m = m1; 2706 2707 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, 2708 buf->m, segs, &nsegs, BUS_DMA_NOWAIT); 2709 if (__predict_false(error != 0)) { 2710 /* XXX fix this (applicable to the iwn(4) too) */ 2711 /* 2712 * NB: Do not return error; 2713 * original mbuf does not exist anymore. 2714 */ 2715 device_printf(sc->sc_dev, 2716 "%s: can't map mbuf (error %d)\n", __func__, 2717 error); 2718 if (ring->qid < WPI_CMD_QUEUE_NUM) { 2719 if_inc_counter(buf->ni->ni_vap->iv_ifp, 2720 IFCOUNTER_OERRORS, 1); 2721 if (!frag) 2722 ieee80211_free_node(buf->ni); 2723 } 2724 m_freem(buf->m); 2725 error = 0; 2726 goto end; 2727 } 2728 } 2729 2730 KASSERT(nsegs < WPI_MAX_SCATTER, 2731 ("too many DMA segments, nsegs (%d) should be less than %d", 2732 nsegs, WPI_MAX_SCATTER)); 2733 2734 data->m = buf->m; 2735 data->ni = buf->ni; 2736 data->hdrlen = hdrlen; 2737 2738 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 2739 __func__, ring->qid, cur, totlen, nsegs); 2740 2741 /* Fill TX descriptor. */ 2742 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs); 2743 /* First DMA segment is used by the TX command. */ 2744 desc->segs[0].addr = htole32(data->cmd_paddr); 2745 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad); 2746 /* Other DMA segments are for data payload. */ 2747 seg = &segs[0]; 2748 for (i = 1; i <= nsegs; i++) { 2749 desc->segs[i].addr = htole32(seg->ds_addr); 2750 desc->segs[i].len = htole32(seg->ds_len); 2751 seg++; 2752 } 2753 2754 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2755 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 2756 BUS_DMASYNC_PREWRITE); 2757 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2758 BUS_DMASYNC_PREWRITE); 2759 2760 ring->pending += 1; 2761 2762 if (!frag) { 2763 if (ring->qid < WPI_CMD_QUEUE_NUM) { 2764 WPI_TXQ_STATE_LOCK(sc); 2765 ring->queued += ring->pending; 2766 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, 2767 sc); 2768 WPI_TXQ_STATE_UNLOCK(sc); 2769 } 2770 2771 /* Kick TX ring. */ 2772 ring->cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT; 2773 ring->pending = 0; 2774 sc->sc_update_tx_ring(sc, ring); 2775 } else 2776 ieee80211_node_incref(data->ni); 2777 2778end: DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END, 2779 __func__); 2780 2781 WPI_TXQ_UNLOCK(sc); 2782 2783 return (error); 2784} 2785 2786/* 2787 * Construct the data packet for a transmit buffer. 2788 */ 2789static int 2790wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 2791{ 2792 const struct ieee80211_txparam *tp = ni->ni_txparms; 2793 struct ieee80211vap *vap = ni->ni_vap; 2794 struct ieee80211com *ic = ni->ni_ic; 2795 struct wpi_node *wn = WPI_NODE(ni); 2796 struct ieee80211_frame *wh; 2797 struct ieee80211_key *k = NULL; 2798 struct wpi_buf tx_data; 2799 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data; 2800 uint32_t flags; 2801 uint16_t ac, qos; 2802 uint8_t tid, type, rate; 2803 int swcrypt, ismcast, totlen; 2804 2805 wh = mtod(m, struct ieee80211_frame *); 2806 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2807 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 2808 swcrypt = 1; 2809 2810 /* Select EDCA Access Category and TX ring for this frame. */ 2811 if (IEEE80211_QOS_HAS_SEQ(wh)) { 2812 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 2813 tid = qos & IEEE80211_QOS_TID; 2814 } else { 2815 qos = 0; 2816 tid = 0; 2817 } 2818 ac = M_WME_GETAC(m); 2819 2820 /* Choose a TX rate index. */ 2821 if (type == IEEE80211_FC0_TYPE_MGT || 2822 type == IEEE80211_FC0_TYPE_CTL || 2823 (m->m_flags & M_EAPOL) != 0) 2824 rate = tp->mgmtrate; 2825 else if (ismcast) 2826 rate = tp->mcastrate; 2827 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 2828 rate = tp->ucastrate; 2829 else { 2830 /* XXX pass pktlen */ 2831 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2832 rate = ni->ni_txrate; 2833 } 2834 2835 /* Encrypt the frame if need be. */ 2836 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2837 /* Retrieve key for TX. */ 2838 k = ieee80211_crypto_encap(ni, m); 2839 if (k == NULL) 2840 return (ENOBUFS); 2841 2842 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT; 2843 2844 /* 802.11 header may have moved. */ 2845 wh = mtod(m, struct ieee80211_frame *); 2846 } 2847 totlen = m->m_pkthdr.len; 2848 2849 if (ieee80211_radiotap_active_vap(vap)) { 2850 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2851 2852 tap->wt_flags = 0; 2853 tap->wt_rate = rate; 2854 if (k != NULL) 2855 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 2856 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) 2857 tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG; 2858 2859 ieee80211_radiotap_tx(vap, m); 2860 } 2861 2862 flags = 0; 2863 if (!ismcast) { 2864 /* Unicast frame, check if an ACK is expected. */ 2865 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 2866 IEEE80211_QOS_ACKPOLICY_NOACK) 2867 flags |= WPI_TX_NEED_ACK; 2868 } 2869 2870 if (!IEEE80211_QOS_HAS_SEQ(wh)) 2871 flags |= WPI_TX_AUTO_SEQ; 2872 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) 2873 flags |= WPI_TX_MORE_FRAG; 2874 2875 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */ 2876 if (!ismcast) { 2877 /* NB: Group frames are sent using CCK in 802.11b/g. */ 2878 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) { 2879 flags |= WPI_TX_NEED_RTS; 2880 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 2881 WPI_RATE_IS_OFDM(rate)) { 2882 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2883 flags |= WPI_TX_NEED_CTS; 2884 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2885 flags |= WPI_TX_NEED_RTS; 2886 } 2887 2888 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS)) 2889 flags |= WPI_TX_FULL_TXOP; 2890 } 2891 2892 memset(tx, 0, sizeof (struct wpi_cmd_data)); 2893 if (type == IEEE80211_FC0_TYPE_MGT) { 2894 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2895 2896 /* Tell HW to set timestamp in probe responses. */ 2897 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2898 flags |= WPI_TX_INSERT_TSTAMP; 2899 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2900 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2901 tx->timeout = htole16(3); 2902 else 2903 tx->timeout = htole16(2); 2904 } 2905 2906 if (ismcast || type != IEEE80211_FC0_TYPE_DATA) 2907 tx->id = WPI_ID_BROADCAST; 2908 else { 2909 if (wn->id == WPI_ID_UNDEFINED) { 2910 device_printf(sc->sc_dev, 2911 "%s: undefined node id\n", __func__); 2912 return (EINVAL); 2913 } 2914 2915 tx->id = wn->id; 2916 } 2917 2918 if (!swcrypt) { 2919 switch (k->wk_cipher->ic_cipher) { 2920 case IEEE80211_CIPHER_AES_CCM: 2921 tx->security = WPI_CIPHER_CCMP; 2922 break; 2923 2924 default: 2925 break; 2926 } 2927 2928 memcpy(tx->key, k->wk_key, k->wk_keylen); 2929 } 2930 2931 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) { 2932 struct mbuf *next = m->m_nextpkt; 2933 2934 tx->lnext = htole16(next->m_pkthdr.len); 2935 tx->fnext = htole32(tx->security | 2936 (flags & WPI_TX_NEED_ACK) | 2937 WPI_NEXT_STA_ID(tx->id)); 2938 } 2939 2940 tx->len = htole16(totlen); 2941 tx->flags = htole32(flags); 2942 tx->plcp = rate2plcp(rate); 2943 tx->tid = tid; 2944 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 2945 tx->ofdm_mask = 0xff; 2946 tx->cck_mask = 0x0f; 2947 tx->rts_ntries = 7; 2948 tx->data_ntries = tp->maxretry; 2949 2950 tx_data.ni = ni; 2951 tx_data.m = m; 2952 tx_data.size = sizeof(struct wpi_cmd_data); 2953 tx_data.code = WPI_CMD_TX_DATA; 2954 tx_data.ac = ac; 2955 2956 return wpi_cmd2(sc, &tx_data); 2957} 2958 2959static int 2960wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m, 2961 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 2962{ 2963 struct ieee80211vap *vap = ni->ni_vap; 2964 struct ieee80211_key *k = NULL; 2965 struct ieee80211_frame *wh; 2966 struct wpi_buf tx_data; 2967 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data; 2968 uint32_t flags; 2969 uint8_t ac, type, rate; 2970 int swcrypt, totlen; 2971 2972 wh = mtod(m, struct ieee80211_frame *); 2973 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2974 swcrypt = 1; 2975 2976 ac = params->ibp_pri & 3; 2977 2978 /* Choose a TX rate index. */ 2979 rate = params->ibp_rate0; 2980 2981 flags = 0; 2982 if (!IEEE80211_QOS_HAS_SEQ(wh)) 2983 flags |= WPI_TX_AUTO_SEQ; 2984 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 2985 flags |= WPI_TX_NEED_ACK; 2986 if (params->ibp_flags & IEEE80211_BPF_RTS) 2987 flags |= WPI_TX_NEED_RTS; 2988 if (params->ibp_flags & IEEE80211_BPF_CTS) 2989 flags |= WPI_TX_NEED_CTS; 2990 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS)) 2991 flags |= WPI_TX_FULL_TXOP; 2992 2993 /* Encrypt the frame if need be. */ 2994 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 2995 /* Retrieve key for TX. */ 2996 k = ieee80211_crypto_encap(ni, m); 2997 if (k == NULL) 2998 return (ENOBUFS); 2999 3000 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT; 3001 3002 /* 802.11 header may have moved. */ 3003 wh = mtod(m, struct ieee80211_frame *); 3004 } 3005 totlen = m->m_pkthdr.len; 3006 3007 if (ieee80211_radiotap_active_vap(vap)) { 3008 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 3009 3010 tap->wt_flags = 0; 3011 tap->wt_rate = rate; 3012 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) 3013 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3014 3015 ieee80211_radiotap_tx(vap, m); 3016 } 3017 3018 memset(tx, 0, sizeof (struct wpi_cmd_data)); 3019 if (type == IEEE80211_FC0_TYPE_MGT) { 3020 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 3021 3022 /* Tell HW to set timestamp in probe responses. */ 3023 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 3024 flags |= WPI_TX_INSERT_TSTAMP; 3025 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 3026 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 3027 tx->timeout = htole16(3); 3028 else 3029 tx->timeout = htole16(2); 3030 } 3031 3032 if (!swcrypt) { 3033 switch (k->wk_cipher->ic_cipher) { 3034 case IEEE80211_CIPHER_AES_CCM: 3035 tx->security = WPI_CIPHER_CCMP; 3036 break; 3037 3038 default: 3039 break; 3040 } 3041 3042 memcpy(tx->key, k->wk_key, k->wk_keylen); 3043 } 3044 3045 tx->len = htole16(totlen); 3046 tx->flags = htole32(flags); 3047 tx->plcp = rate2plcp(rate); 3048 tx->id = WPI_ID_BROADCAST; 3049 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 3050 tx->rts_ntries = params->ibp_try1; 3051 tx->data_ntries = params->ibp_try0; 3052 3053 tx_data.ni = ni; 3054 tx_data.m = m; 3055 tx_data.size = sizeof(struct wpi_cmd_data); 3056 tx_data.code = WPI_CMD_TX_DATA; 3057 tx_data.ac = ac; 3058 3059 return wpi_cmd2(sc, &tx_data); 3060} 3061 3062static __inline int 3063wpi_tx_ring_free_space(struct wpi_softc *sc, uint16_t ac) 3064{ 3065 struct wpi_tx_ring *ring = &sc->txq[ac]; 3066 int retval; 3067 3068 WPI_TXQ_STATE_LOCK(sc); 3069 retval = WPI_TX_RING_HIMARK - ring->queued; 3070 WPI_TXQ_STATE_UNLOCK(sc); 3071 3072 return retval; 3073} 3074 3075static int 3076wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3077 const struct ieee80211_bpf_params *params) 3078{ 3079 struct ieee80211com *ic = ni->ni_ic; 3080 struct wpi_softc *sc = ic->ic_softc; 3081 uint16_t ac; 3082 int error = 0; 3083 3084 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3085 3086 ac = M_WME_GETAC(m); 3087 3088 WPI_TX_LOCK(sc); 3089 3090 /* NB: no fragments here */ 3091 if (sc->sc_running == 0 || wpi_tx_ring_free_space(sc, ac) < 1) { 3092 error = sc->sc_running ? ENOBUFS : ENETDOWN; 3093 goto unlock; 3094 } 3095 3096 if (params == NULL) { 3097 /* 3098 * Legacy path; interpret frame contents to decide 3099 * precisely how to send the frame. 3100 */ 3101 error = wpi_tx_data(sc, m, ni); 3102 } else { 3103 /* 3104 * Caller supplied explicit parameters to use in 3105 * sending the frame. 3106 */ 3107 error = wpi_tx_data_raw(sc, m, ni, params); 3108 } 3109 3110unlock: WPI_TX_UNLOCK(sc); 3111 3112 if (error != 0) { 3113 m_freem(m); 3114 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 3115 3116 return error; 3117 } 3118 3119 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3120 3121 return 0; 3122} 3123 3124static int 3125wpi_transmit(struct ieee80211com *ic, struct mbuf *m) 3126{ 3127 struct wpi_softc *sc = ic->ic_softc; 3128 struct ieee80211_node *ni; 3129 struct mbuf *mnext; 3130 uint16_t ac; 3131 int error, nmbufs; 3132 3133 WPI_TX_LOCK(sc); 3134 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__); 3135 3136 /* Check if interface is up & running. */ 3137 if (__predict_false(sc->sc_running == 0)) { 3138 error = ENXIO; 3139 goto unlock; 3140 } 3141 3142 nmbufs = 1; 3143 for (mnext = m->m_nextpkt; mnext != NULL; mnext = mnext->m_nextpkt) 3144 nmbufs++; 3145 3146 /* Check for available space. */ 3147 ac = M_WME_GETAC(m); 3148 if (wpi_tx_ring_free_space(sc, ac) < nmbufs) { 3149 error = ENOBUFS; 3150 goto unlock; 3151 } 3152 3153 error = 0; 3154 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 3155 do { 3156 mnext = m->m_nextpkt; 3157 if (wpi_tx_data(sc, m, ni) != 0) { 3158 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 3159 nmbufs); 3160 wpi_free_txfrags(sc, ac); 3161 ieee80211_free_mbuf(m); 3162 ieee80211_free_node(ni); 3163 break; 3164 } 3165 } while((m = mnext) != NULL); 3166 3167 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__); 3168 3169unlock: WPI_TX_UNLOCK(sc); 3170 3171 return (error); 3172} 3173 3174static void 3175wpi_watchdog_rfkill(void *arg) 3176{ 3177 struct wpi_softc *sc = arg; 3178 struct ieee80211com *ic = &sc->sc_ic; 3179 3180 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n"); 3181 3182 /* No need to lock firmware memory. */ 3183 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) { 3184 /* Radio kill switch is still off. */ 3185 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, 3186 sc); 3187 } else 3188 ieee80211_runtask(ic, &sc->sc_radioon_task); 3189} 3190 3191static void 3192wpi_scan_timeout(void *arg) 3193{ 3194 struct wpi_softc *sc = arg; 3195 struct ieee80211com *ic = &sc->sc_ic; 3196 3197 ic_printf(ic, "scan timeout\n"); 3198 ieee80211_restart_all(ic); 3199} 3200 3201static void 3202wpi_tx_timeout(void *arg) 3203{ 3204 struct wpi_softc *sc = arg; 3205 struct ieee80211com *ic = &sc->sc_ic; 3206 3207 ic_printf(ic, "device timeout\n"); 3208 ieee80211_restart_all(ic); 3209} 3210 3211static void 3212wpi_parent(struct ieee80211com *ic) 3213{ 3214 struct wpi_softc *sc = ic->ic_softc; 3215 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3216 3217 if (ic->ic_nrunning > 0) { 3218 if (wpi_init(sc) == 0) { 3219 ieee80211_notify_radio(ic, 1); 3220 ieee80211_start_all(ic); 3221 } else { 3222 ieee80211_notify_radio(ic, 0); 3223 ieee80211_stop(vap); 3224 } 3225 } else { 3226 ieee80211_notify_radio(ic, 0); 3227 wpi_stop(sc); 3228 } 3229} 3230 3231/* 3232 * Send a command to the firmware. 3233 */ 3234static int 3235wpi_cmd(struct wpi_softc *sc, uint8_t code, const void *buf, uint16_t size, 3236 int async) 3237{ 3238 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM]; 3239 struct wpi_tx_desc *desc; 3240 struct wpi_tx_data *data; 3241 struct wpi_tx_cmd *cmd; 3242 struct mbuf *m; 3243 bus_addr_t paddr; 3244 uint16_t totlen; 3245 int error; 3246 3247 WPI_TXQ_LOCK(sc); 3248 3249 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3250 3251 if (__predict_false(sc->sc_running == 0)) { 3252 /* wpi_stop() was called */ 3253 if (code == WPI_CMD_SCAN) 3254 error = ENETDOWN; 3255 else 3256 error = 0; 3257 3258 goto fail; 3259 } 3260 3261 if (async == 0) 3262 WPI_LOCK_ASSERT(sc); 3263 3264 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %u async %d\n", 3265 __func__, wpi_cmd_str(code), size, async); 3266 3267 desc = &ring->desc[ring->cur]; 3268 data = &ring->data[ring->cur]; 3269 totlen = 4 + size; 3270 3271 if (size > sizeof cmd->data) { 3272 /* Command is too large to fit in a descriptor. */ 3273 if (totlen > MCLBYTES) { 3274 error = EINVAL; 3275 goto fail; 3276 } 3277 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 3278 if (m == NULL) { 3279 error = ENOMEM; 3280 goto fail; 3281 } 3282 cmd = mtod(m, struct wpi_tx_cmd *); 3283 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, 3284 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 3285 if (error != 0) { 3286 m_freem(m); 3287 goto fail; 3288 } 3289 data->m = m; 3290 } else { 3291 cmd = &ring->cmd[ring->cur]; 3292 paddr = data->cmd_paddr; 3293 } 3294 3295 cmd->code = code; 3296 cmd->flags = 0; 3297 cmd->qid = ring->qid; 3298 cmd->idx = ring->cur; 3299 memcpy(cmd->data, buf, size); 3300 3301 desc->nsegs = 1 + (WPI_PAD32(size) << 4); 3302 desc->segs[0].addr = htole32(paddr); 3303 desc->segs[0].len = htole32(totlen); 3304 3305 if (size > sizeof cmd->data) { 3306 bus_dmamap_sync(ring->data_dmat, data->map, 3307 BUS_DMASYNC_PREWRITE); 3308 } else { 3309 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 3310 BUS_DMASYNC_PREWRITE); 3311 } 3312 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 3313 BUS_DMASYNC_PREWRITE); 3314 3315 /* Kick command ring. */ 3316 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 3317 sc->sc_update_tx_ring(sc, ring); 3318 3319 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3320 3321 WPI_TXQ_UNLOCK(sc); 3322 3323 return async ? 0 : mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz); 3324 3325fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 3326 3327 WPI_TXQ_UNLOCK(sc); 3328 3329 return error; 3330} 3331 3332/* 3333 * Configure HW multi-rate retries. 3334 */ 3335static int 3336wpi_mrr_setup(struct wpi_softc *sc) 3337{ 3338 struct ieee80211com *ic = &sc->sc_ic; 3339 struct wpi_mrr_setup mrr; 3340 uint8_t i; 3341 int error; 3342 3343 /* CCK rates (not used with 802.11a). */ 3344 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) { 3345 mrr.rates[i].flags = 0; 3346 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 3347 /* Fallback to the immediate lower CCK rate (if any.) */ 3348 mrr.rates[i].next = 3349 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1; 3350 /* Try twice at this rate before falling back to "next". */ 3351 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT; 3352 } 3353 /* OFDM rates (not used with 802.11b). */ 3354 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) { 3355 mrr.rates[i].flags = 0; 3356 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 3357 /* Fallback to the immediate lower rate (if any.) */ 3358 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */ 3359 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ? 3360 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 3361 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) : 3362 i - 1; 3363 /* Try twice at this rate before falling back to "next". */ 3364 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT; 3365 } 3366 /* Setup MRR for control frames. */ 3367 mrr.which = htole32(WPI_MRR_CTL); 3368 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 3369 if (error != 0) { 3370 device_printf(sc->sc_dev, 3371 "could not setup MRR for control frames\n"); 3372 return error; 3373 } 3374 /* Setup MRR for data frames. */ 3375 mrr.which = htole32(WPI_MRR_DATA); 3376 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 3377 if (error != 0) { 3378 device_printf(sc->sc_dev, 3379 "could not setup MRR for data frames\n"); 3380 return error; 3381 } 3382 return 0; 3383} 3384 3385static int 3386wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni) 3387{ 3388 struct ieee80211com *ic = ni->ni_ic; 3389 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap); 3390 struct wpi_node *wn = WPI_NODE(ni); 3391 struct wpi_node_info node; 3392 int error; 3393 3394 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3395 3396 if (wn->id == WPI_ID_UNDEFINED) 3397 return EINVAL; 3398 3399 memset(&node, 0, sizeof node); 3400 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 3401 node.id = wn->id; 3402 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3403 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 3404 node.action = htole32(WPI_ACTION_SET_RATE); 3405 node.antenna = WPI_ANTENNA_BOTH; 3406 3407 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__, 3408 wn->id, ether_sprintf(ni->ni_macaddr)); 3409 3410 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 3411 if (error != 0) { 3412 device_printf(sc->sc_dev, 3413 "%s: wpi_cmd() call failed with error code %d\n", __func__, 3414 error); 3415 return error; 3416 } 3417 3418 if (wvp->wv_gtk != 0) { 3419 error = wpi_set_global_keys(ni); 3420 if (error != 0) { 3421 device_printf(sc->sc_dev, 3422 "%s: error while setting global keys\n", __func__); 3423 return ENXIO; 3424 } 3425 } 3426 3427 return 0; 3428} 3429 3430/* 3431 * Broadcast node is used to send group-addressed and management frames. 3432 */ 3433static int 3434wpi_add_broadcast_node(struct wpi_softc *sc, int async) 3435{ 3436 struct ieee80211com *ic = &sc->sc_ic; 3437 struct wpi_node_info node; 3438 3439 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3440 3441 memset(&node, 0, sizeof node); 3442 IEEE80211_ADDR_COPY(node.macaddr, ieee80211broadcastaddr); 3443 node.id = WPI_ID_BROADCAST; 3444 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3445 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 3446 node.action = htole32(WPI_ACTION_SET_RATE); 3447 node.antenna = WPI_ANTENNA_BOTH; 3448 3449 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__); 3450 3451 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async); 3452} 3453 3454static int 3455wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni) 3456{ 3457 struct wpi_node *wn = WPI_NODE(ni); 3458 int error; 3459 3460 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3461 3462 wn->id = wpi_add_node_entry_sta(sc); 3463 3464 if ((error = wpi_add_node(sc, ni)) != 0) { 3465 wpi_del_node_entry(sc, wn->id); 3466 wn->id = WPI_ID_UNDEFINED; 3467 return error; 3468 } 3469 3470 return 0; 3471} 3472 3473static int 3474wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni) 3475{ 3476 struct wpi_node *wn = WPI_NODE(ni); 3477 int error; 3478 3479 KASSERT(wn->id == WPI_ID_UNDEFINED, 3480 ("the node %d was added before", wn->id)); 3481 3482 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3483 3484 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) { 3485 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__); 3486 return ENOMEM; 3487 } 3488 3489 if ((error = wpi_add_node(sc, ni)) != 0) { 3490 wpi_del_node_entry(sc, wn->id); 3491 wn->id = WPI_ID_UNDEFINED; 3492 return error; 3493 } 3494 3495 return 0; 3496} 3497 3498static void 3499wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni) 3500{ 3501 struct wpi_node *wn = WPI_NODE(ni); 3502 struct wpi_cmd_del_node node; 3503 int error; 3504 3505 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed")); 3506 3507 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3508 3509 memset(&node, 0, sizeof node); 3510 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 3511 node.count = 1; 3512 3513 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__, 3514 wn->id, ether_sprintf(ni->ni_macaddr)); 3515 3516 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1); 3517 if (error != 0) { 3518 device_printf(sc->sc_dev, 3519 "%s: could not delete node %u, error %d\n", __func__, 3520 wn->id, error); 3521 } 3522} 3523 3524static int 3525wpi_updateedca(struct ieee80211com *ic) 3526{ 3527#define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 3528 struct wpi_softc *sc = ic->ic_softc; 3529 struct chanAccParams chp; 3530 struct wpi_edca_params cmd; 3531 int aci, error; 3532 3533 ieee80211_wme_ic_getparams(ic, &chp); 3534 3535 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3536 3537 memset(&cmd, 0, sizeof cmd); 3538 cmd.flags = htole32(WPI_EDCA_UPDATE); 3539 for (aci = 0; aci < WME_NUM_AC; aci++) { 3540 const struct wmeParams *ac = &chp.cap_wmeParams[aci]; 3541 cmd.ac[aci].aifsn = ac->wmep_aifsn; 3542 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin)); 3543 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax)); 3544 cmd.ac[aci].txoplimit = 3545 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit)); 3546 3547 DPRINTF(sc, WPI_DEBUG_EDCA, 3548 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 3549 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn, 3550 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax, 3551 cmd.ac[aci].txoplimit); 3552 } 3553 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1); 3554 3555 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3556 3557 return error; 3558#undef WPI_EXP2 3559} 3560 3561static void 3562wpi_set_promisc(struct wpi_softc *sc) 3563{ 3564 struct ieee80211com *ic = &sc->sc_ic; 3565 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3566 uint32_t promisc_filter; 3567 3568 promisc_filter = WPI_FILTER_CTL; 3569 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP) 3570 promisc_filter |= WPI_FILTER_PROMISC; 3571 3572 if (ic->ic_promisc > 0) 3573 sc->rxon.filter |= htole32(promisc_filter); 3574 else 3575 sc->rxon.filter &= ~htole32(promisc_filter); 3576} 3577 3578static void 3579wpi_update_promisc(struct ieee80211com *ic) 3580{ 3581 struct wpi_softc *sc = ic->ic_softc; 3582 3583 WPI_LOCK(sc); 3584 if (sc->sc_running == 0) { 3585 WPI_UNLOCK(sc); 3586 return; 3587 } 3588 WPI_UNLOCK(sc); 3589 3590 WPI_RXON_LOCK(sc); 3591 wpi_set_promisc(sc); 3592 3593 if (wpi_send_rxon(sc, 1, 1) != 0) { 3594 device_printf(sc->sc_dev, "%s: could not send RXON\n", 3595 __func__); 3596 } 3597 WPI_RXON_UNLOCK(sc); 3598} 3599 3600static void 3601wpi_update_mcast(struct ieee80211com *ic) 3602{ 3603 /* Ignore */ 3604} 3605 3606static void 3607wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 3608{ 3609 struct wpi_cmd_led led; 3610 3611 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3612 3613 led.which = which; 3614 led.unit = htole32(100000); /* on/off in unit of 100ms */ 3615 led.off = off; 3616 led.on = on; 3617 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 3618} 3619 3620static int 3621wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni) 3622{ 3623 struct wpi_cmd_timing cmd; 3624 uint64_t val, mod; 3625 3626 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3627 3628 memset(&cmd, 0, sizeof cmd); 3629 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t)); 3630 cmd.bintval = htole16(ni->ni_intval); 3631 cmd.lintval = htole16(10); 3632 3633 /* Compute remaining time until next beacon. */ 3634 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU; 3635 mod = le64toh(cmd.tstamp) % val; 3636 cmd.binitval = htole32((uint32_t)(val - mod)); 3637 3638 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n", 3639 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod)); 3640 3641 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1); 3642} 3643 3644/* 3645 * This function is called periodically (every 60 seconds) to adjust output 3646 * power to temperature changes. 3647 */ 3648static void 3649wpi_power_calibration(struct wpi_softc *sc) 3650{ 3651 int temp; 3652 3653 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3654 3655 /* Update sensor data. */ 3656 temp = (int)WPI_READ(sc, WPI_UCODE_GP2); 3657 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp); 3658 3659 /* Sanity-check read value. */ 3660 if (temp < -260 || temp > 25) { 3661 /* This can't be correct, ignore. */ 3662 DPRINTF(sc, WPI_DEBUG_TEMP, 3663 "out-of-range temperature reported: %d\n", temp); 3664 return; 3665 } 3666 3667 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp); 3668 3669 /* Adjust Tx power if need be. */ 3670 if (abs(temp - sc->temp) <= 6) 3671 return; 3672 3673 sc->temp = temp; 3674 3675 if (wpi_set_txpower(sc, 1) != 0) { 3676 /* just warn, too bad for the automatic calibration... */ 3677 device_printf(sc->sc_dev,"could not adjust Tx power\n"); 3678 } 3679} 3680 3681/* 3682 * Set TX power for current channel. 3683 */ 3684static int 3685wpi_set_txpower(struct wpi_softc *sc, int async) 3686{ 3687 struct wpi_power_group *group; 3688 struct wpi_cmd_txpower cmd; 3689 uint8_t chan; 3690 int idx, is_chan_5ghz, i; 3691 3692 /* Retrieve current channel from last RXON. */ 3693 chan = sc->rxon.chan; 3694 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0; 3695 3696 /* Find the TX power group to which this channel belongs. */ 3697 if (is_chan_5ghz) { 3698 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 3699 if (chan <= group->chan) 3700 break; 3701 } else 3702 group = &sc->groups[0]; 3703 3704 memset(&cmd, 0, sizeof cmd); 3705 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ; 3706 cmd.chan = htole16(chan); 3707 3708 /* Set TX power for all OFDM and CCK rates. */ 3709 for (i = 0; i <= WPI_RIDX_MAX ; i++) { 3710 /* Retrieve TX power for this channel/rate. */ 3711 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i); 3712 3713 cmd.rates[i].plcp = wpi_ridx_to_plcp[i]; 3714 3715 if (is_chan_5ghz) { 3716 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx]; 3717 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx]; 3718 } else { 3719 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx]; 3720 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx]; 3721 } 3722 DPRINTF(sc, WPI_DEBUG_TEMP, 3723 "chan %d/ridx %d: power index %d\n", chan, i, idx); 3724 } 3725 3726 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async); 3727} 3728 3729/* 3730 * Determine Tx power index for a given channel/rate combination. 3731 * This takes into account the regulatory information from EEPROM and the 3732 * current temperature. 3733 */ 3734static int 3735wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 3736 uint8_t chan, int is_chan_5ghz, int ridx) 3737{ 3738/* Fixed-point arithmetic division using a n-bit fractional part. */ 3739#define fdivround(a, b, n) \ 3740 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 3741 3742/* Linear interpolation. */ 3743#define interpolate(x, x1, y1, x2, y2, n) \ 3744 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 3745 3746 struct wpi_power_sample *sample; 3747 int pwr, idx; 3748 3749 /* Default TX power is group maximum TX power minus 3dB. */ 3750 pwr = group->maxpwr / 2; 3751 3752 /* Decrease TX power for highest OFDM rates to reduce distortion. */ 3753 switch (ridx) { 3754 case WPI_RIDX_OFDM36: 3755 pwr -= is_chan_5ghz ? 5 : 0; 3756 break; 3757 case WPI_RIDX_OFDM48: 3758 pwr -= is_chan_5ghz ? 10 : 7; 3759 break; 3760 case WPI_RIDX_OFDM54: 3761 pwr -= is_chan_5ghz ? 12 : 9; 3762 break; 3763 } 3764 3765 /* Never exceed the channel maximum allowed TX power. */ 3766 pwr = min(pwr, sc->maxpwr[chan]); 3767 3768 /* Retrieve TX power index into gain tables from samples. */ 3769 for (sample = group->samples; sample < &group->samples[3]; sample++) 3770 if (pwr > sample[1].power) 3771 break; 3772 /* Fixed-point linear interpolation using a 19-bit fractional part. */ 3773 idx = interpolate(pwr, sample[0].power, sample[0].index, 3774 sample[1].power, sample[1].index, 19); 3775 3776 /*- 3777 * Adjust power index based on current temperature: 3778 * - if cooler than factory-calibrated: decrease output power 3779 * - if warmer than factory-calibrated: increase output power 3780 */ 3781 idx -= (sc->temp - group->temp) * 11 / 100; 3782 3783 /* Decrease TX power for CCK rates (-5dB). */ 3784 if (ridx >= WPI_RIDX_CCK1) 3785 idx += 10; 3786 3787 /* Make sure idx stays in a valid range. */ 3788 if (idx < 0) 3789 return 0; 3790 if (idx > WPI_MAX_PWR_INDEX) 3791 return WPI_MAX_PWR_INDEX; 3792 return idx; 3793 3794#undef interpolate 3795#undef fdivround 3796} 3797 3798/* 3799 * Set STA mode power saving level (between 0 and 5). 3800 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving. 3801 */ 3802static int 3803wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async) 3804{ 3805 struct wpi_pmgt_cmd cmd; 3806 const struct wpi_pmgt *pmgt; 3807 uint32_t max, reg; 3808 uint8_t skip_dtim; 3809 int i; 3810 3811 DPRINTF(sc, WPI_DEBUG_PWRSAVE, 3812 "%s: dtim=%d, level=%d, async=%d\n", 3813 __func__, dtim, level, async); 3814 3815 /* Select which PS parameters to use. */ 3816 if (dtim <= 10) 3817 pmgt = &wpi_pmgt[0][level]; 3818 else 3819 pmgt = &wpi_pmgt[1][level]; 3820 3821 memset(&cmd, 0, sizeof cmd); 3822 if (level != 0) /* not CAM */ 3823 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP); 3824 /* Retrieve PCIe Active State Power Management (ASPM). */ 3825 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1); 3826 if (!(reg & PCIEM_LINK_CTL_ASPMC_L0S)) /* L0s Entry disabled. */ 3827 cmd.flags |= htole16(WPI_PS_PCI_PMGT); 3828 3829 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU); 3830 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU); 3831 3832 if (dtim == 0) { 3833 dtim = 1; 3834 skip_dtim = 0; 3835 } else 3836 skip_dtim = pmgt->skip_dtim; 3837 3838 if (skip_dtim != 0) { 3839 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM); 3840 max = pmgt->intval[4]; 3841 if (max == (uint32_t)-1) 3842 max = dtim * (skip_dtim + 1); 3843 else if (max > dtim) 3844 max = rounddown(max, dtim); 3845 } else 3846 max = dtim; 3847 3848 for (i = 0; i < 5; i++) 3849 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i])); 3850 3851 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async); 3852} 3853 3854static int 3855wpi_send_btcoex(struct wpi_softc *sc) 3856{ 3857 struct wpi_bluetooth cmd; 3858 3859 memset(&cmd, 0, sizeof cmd); 3860 cmd.flags = WPI_BT_COEX_MODE_4WIRE; 3861 cmd.lead_time = WPI_BT_LEAD_TIME_DEF; 3862 cmd.max_kill = WPI_BT_MAX_KILL_DEF; 3863 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n", 3864 __func__); 3865 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0); 3866} 3867 3868static int 3869wpi_send_rxon(struct wpi_softc *sc, int assoc, int async) 3870{ 3871 int error; 3872 3873 if (async) 3874 WPI_RXON_LOCK_ASSERT(sc); 3875 3876 if (assoc && wpi_check_bss_filter(sc) != 0) { 3877 struct wpi_assoc rxon_assoc; 3878 3879 rxon_assoc.flags = sc->rxon.flags; 3880 rxon_assoc.filter = sc->rxon.filter; 3881 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask; 3882 rxon_assoc.cck_mask = sc->rxon.cck_mask; 3883 rxon_assoc.reserved = 0; 3884 3885 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc, 3886 sizeof (struct wpi_assoc), async); 3887 if (error != 0) { 3888 device_printf(sc->sc_dev, 3889 "RXON_ASSOC command failed, error %d\n", error); 3890 return error; 3891 } 3892 } else { 3893 if (async) { 3894 WPI_NT_LOCK(sc); 3895 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, 3896 sizeof (struct wpi_rxon), async); 3897 if (error == 0) 3898 wpi_clear_node_table(sc); 3899 WPI_NT_UNLOCK(sc); 3900 } else { 3901 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, 3902 sizeof (struct wpi_rxon), async); 3903 if (error == 0) 3904 wpi_clear_node_table(sc); 3905 } 3906 3907 if (error != 0) { 3908 device_printf(sc->sc_dev, 3909 "RXON command failed, error %d\n", error); 3910 return error; 3911 } 3912 3913 /* Add broadcast node. */ 3914 error = wpi_add_broadcast_node(sc, async); 3915 if (error != 0) { 3916 device_printf(sc->sc_dev, 3917 "could not add broadcast node, error %d\n", error); 3918 return error; 3919 } 3920 } 3921 3922 /* Configuration has changed, set Tx power accordingly. */ 3923 if ((error = wpi_set_txpower(sc, async)) != 0) { 3924 device_printf(sc->sc_dev, 3925 "%s: could not set TX power, error %d\n", __func__, error); 3926 return error; 3927 } 3928 3929 return 0; 3930} 3931 3932/** 3933 * Configure the card to listen to a particular channel, this transisions the 3934 * card in to being able to receive frames from remote devices. 3935 */ 3936static int 3937wpi_config(struct wpi_softc *sc) 3938{ 3939 struct ieee80211com *ic = &sc->sc_ic; 3940 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3941 struct ieee80211_channel *c = ic->ic_curchan; 3942 int error; 3943 3944 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3945 3946 /* Set power saving level to CAM during initialization. */ 3947 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) { 3948 device_printf(sc->sc_dev, 3949 "%s: could not set power saving level\n", __func__); 3950 return error; 3951 } 3952 3953 /* Configure bluetooth coexistence. */ 3954 if ((error = wpi_send_btcoex(sc)) != 0) { 3955 device_printf(sc->sc_dev, 3956 "could not configure bluetooth coexistence\n"); 3957 return error; 3958 } 3959 3960 /* Configure adapter. */ 3961 memset(&sc->rxon, 0, sizeof (struct wpi_rxon)); 3962 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr); 3963 3964 /* Set default channel. */ 3965 sc->rxon.chan = ieee80211_chan2ieee(ic, c); 3966 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 3967 if (IEEE80211_IS_CHAN_2GHZ(c)) 3968 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 3969 3970 sc->rxon.filter = WPI_FILTER_MULTICAST; 3971 switch (ic->ic_opmode) { 3972 case IEEE80211_M_STA: 3973 sc->rxon.mode = WPI_MODE_STA; 3974 break; 3975 case IEEE80211_M_IBSS: 3976 sc->rxon.mode = WPI_MODE_IBSS; 3977 sc->rxon.filter |= WPI_FILTER_BEACON; 3978 break; 3979 case IEEE80211_M_HOSTAP: 3980 /* XXX workaround for beaconing */ 3981 sc->rxon.mode = WPI_MODE_IBSS; 3982 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC; 3983 break; 3984 case IEEE80211_M_AHDEMO: 3985 sc->rxon.mode = WPI_MODE_HOSTAP; 3986 break; 3987 case IEEE80211_M_MONITOR: 3988 sc->rxon.mode = WPI_MODE_MONITOR; 3989 break; 3990 default: 3991 device_printf(sc->sc_dev, "unknown opmode %d\n", 3992 ic->ic_opmode); 3993 return EINVAL; 3994 } 3995 sc->rxon.filter = htole32(sc->rxon.filter); 3996 wpi_set_promisc(sc); 3997 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */ 3998 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */ 3999 4000 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) { 4001 device_printf(sc->sc_dev, "%s: could not send RXON\n", 4002 __func__); 4003 return error; 4004 } 4005 4006 /* Setup rate scalling. */ 4007 if ((error = wpi_mrr_setup(sc)) != 0) { 4008 device_printf(sc->sc_dev, "could not setup MRR, error %d\n", 4009 error); 4010 return error; 4011 } 4012 4013 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4014 4015 return 0; 4016} 4017 4018static uint16_t 4019wpi_get_active_dwell_time(struct wpi_softc *sc, 4020 struct ieee80211_channel *c, uint8_t n_probes) 4021{ 4022 /* No channel? Default to 2GHz settings. */ 4023 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) { 4024 return (WPI_ACTIVE_DWELL_TIME_2GHZ + 4025 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1)); 4026 } 4027 4028 /* 5GHz dwell time. */ 4029 return (WPI_ACTIVE_DWELL_TIME_5GHZ + 4030 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1)); 4031} 4032 4033/* 4034 * Limit the total dwell time. 4035 * 4036 * Returns the dwell time in milliseconds. 4037 */ 4038static uint16_t 4039wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time) 4040{ 4041 struct ieee80211com *ic = &sc->sc_ic; 4042 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4043 uint16_t bintval = 0; 4044 4045 /* bintval is in TU (1.024mS) */ 4046 if (vap != NULL) 4047 bintval = vap->iv_bss->ni_intval; 4048 4049 /* 4050 * If it's non-zero, we should calculate the minimum of 4051 * it and the DWELL_BASE. 4052 * 4053 * XXX Yes, the math should take into account that bintval 4054 * is 1.024mS, not 1mS.. 4055 */ 4056 if (bintval > 0) { 4057 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__, 4058 bintval); 4059 return (MIN(dwell_time, bintval - WPI_CHANNEL_TUNE_TIME * 2)); 4060 } 4061 4062 /* No association context? Default. */ 4063 return dwell_time; 4064} 4065 4066static uint16_t 4067wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c) 4068{ 4069 uint16_t passive; 4070 4071 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) 4072 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ; 4073 else 4074 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ; 4075 4076 /* Clamp to the beacon interval if we're associated. */ 4077 return (wpi_limit_dwell(sc, passive)); 4078} 4079 4080static uint32_t 4081wpi_get_scan_pause_time(uint32_t time, uint16_t bintval) 4082{ 4083 uint32_t mod = (time % bintval) * IEEE80211_DUR_TU; 4084 uint32_t nbeacons = time / bintval; 4085 4086 if (mod > WPI_PAUSE_MAX_TIME) 4087 mod = WPI_PAUSE_MAX_TIME; 4088 4089 return WPI_PAUSE_SCAN(nbeacons, mod); 4090} 4091 4092/* 4093 * Send a scan request to the firmware. 4094 */ 4095static int 4096wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c) 4097{ 4098 struct ieee80211com *ic = &sc->sc_ic; 4099 struct ieee80211_scan_state *ss = ic->ic_scan; 4100 struct ieee80211vap *vap = ss->ss_vap; 4101 struct wpi_scan_hdr *hdr; 4102 struct wpi_cmd_data *tx; 4103 struct wpi_scan_essid *essids; 4104 struct wpi_scan_chan *chan; 4105 struct ieee80211_frame *wh; 4106 struct ieee80211_rateset *rs; 4107 uint16_t bintval, buflen, dwell_active, dwell_passive; 4108 uint8_t *buf, *frm, i, nssid; 4109 int bgscan, error; 4110 4111 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4112 4113 /* 4114 * We are absolutely not allowed to send a scan command when another 4115 * scan command is pending. 4116 */ 4117 if (callout_pending(&sc->scan_timeout)) { 4118 device_printf(sc->sc_dev, "%s: called whilst scanning!\n", 4119 __func__); 4120 error = EAGAIN; 4121 goto fail; 4122 } 4123 4124 bgscan = wpi_check_bss_filter(sc); 4125 bintval = vap->iv_bss->ni_intval; 4126 if (bgscan != 0 && 4127 bintval < WPI_QUIET_TIME_DEFAULT + WPI_CHANNEL_TUNE_TIME * 2) { 4128 error = EOPNOTSUPP; 4129 goto fail; 4130 } 4131 4132 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO); 4133 if (buf == NULL) { 4134 device_printf(sc->sc_dev, 4135 "%s: could not allocate buffer for scan command\n", 4136 __func__); 4137 error = ENOMEM; 4138 goto fail; 4139 } 4140 hdr = (struct wpi_scan_hdr *)buf; 4141 4142 /* 4143 * Move to the next channel if no packets are received within 10 msecs 4144 * after sending the probe request. 4145 */ 4146 hdr->quiet_time = htole16(WPI_QUIET_TIME_DEFAULT); 4147 hdr->quiet_threshold = htole16(1); 4148 4149 if (bgscan != 0) { 4150 /* 4151 * Max needs to be greater than active and passive and quiet! 4152 * It's also in microseconds! 4153 */ 4154 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU); 4155 hdr->pause_svc = htole32(wpi_get_scan_pause_time(100, 4156 bintval)); 4157 } 4158 4159 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON); 4160 4161 tx = (struct wpi_cmd_data *)(hdr + 1); 4162 tx->flags = htole32(WPI_TX_AUTO_SEQ); 4163 tx->id = WPI_ID_BROADCAST; 4164 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 4165 4166 if (IEEE80211_IS_CHAN_5GHZ(c)) { 4167 /* Send probe requests at 6Mbps. */ 4168 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6]; 4169 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A]; 4170 } else { 4171 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO); 4172 /* Send probe requests at 1Mbps. */ 4173 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 4174 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 4175 } 4176 4177 essids = (struct wpi_scan_essid *)(tx + 1); 4178 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS); 4179 for (i = 0; i < nssid; i++) { 4180 essids[i].id = IEEE80211_ELEMID_SSID; 4181 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN); 4182 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len); 4183#ifdef WPI_DEBUG 4184 if (sc->sc_debug & WPI_DEBUG_SCAN) { 4185 printf("Scanning Essid: "); 4186 ieee80211_print_essid(essids[i].data, essids[i].len); 4187 printf("\n"); 4188 } 4189#endif 4190 } 4191 4192 /* 4193 * Build a probe request frame. Most of the following code is a 4194 * copy & paste of what is done in net80211. 4195 */ 4196 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS); 4197 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 4198 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 4199 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 4200 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr); 4201 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 4202 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr); 4203 4204 frm = (uint8_t *)(wh + 1); 4205 frm = ieee80211_add_ssid(frm, NULL, 0); 4206 frm = ieee80211_add_rates(frm, rs); 4207 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 4208 frm = ieee80211_add_xrates(frm, rs); 4209 4210 /* Set length of probe request. */ 4211 tx->len = htole16(frm - (uint8_t *)wh); 4212 4213 /* 4214 * Construct information about the channel that we 4215 * want to scan. The firmware expects this to be directly 4216 * after the scan probe request 4217 */ 4218 chan = (struct wpi_scan_chan *)frm; 4219 chan->chan = ieee80211_chan2ieee(ic, c); 4220 chan->flags = 0; 4221 if (nssid) { 4222 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT; 4223 chan->flags |= WPI_CHAN_NPBREQS(nssid); 4224 } else 4225 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER; 4226 4227 if (!IEEE80211_IS_CHAN_PASSIVE(c)) 4228 chan->flags |= WPI_CHAN_ACTIVE; 4229 4230 /* 4231 * Calculate the active/passive dwell times. 4232 */ 4233 dwell_active = wpi_get_active_dwell_time(sc, c, nssid); 4234 dwell_passive = wpi_get_passive_dwell_time(sc, c); 4235 4236 /* Make sure they're valid. */ 4237 if (dwell_active > dwell_passive) 4238 dwell_active = dwell_passive; 4239 4240 chan->active = htole16(dwell_active); 4241 chan->passive = htole16(dwell_passive); 4242 4243 chan->dsp_gain = 0x6e; /* Default level */ 4244 4245 if (IEEE80211_IS_CHAN_5GHZ(c)) 4246 chan->rf_gain = 0x3b; 4247 else 4248 chan->rf_gain = 0x28; 4249 4250 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n", 4251 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c)); 4252 4253 hdr->nchan++; 4254 4255 if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) { 4256 /* XXX Force probe request transmission. */ 4257 memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan)); 4258 4259 chan++; 4260 4261 /* Reduce unnecessary delay. */ 4262 chan->flags = 0; 4263 chan->passive = chan->active = hdr->quiet_time; 4264 4265 hdr->nchan++; 4266 } 4267 4268 chan++; 4269 4270 buflen = (uint8_t *)chan - buf; 4271 hdr->len = htole16(buflen); 4272 4273 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n", 4274 hdr->nchan); 4275 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1); 4276 free(buf, M_DEVBUF); 4277 4278 if (error != 0) 4279 goto fail; 4280 4281 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc); 4282 4283 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4284 4285 return 0; 4286 4287fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 4288 4289 return error; 4290} 4291 4292static int 4293wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap) 4294{ 4295 struct ieee80211com *ic = vap->iv_ic; 4296 struct ieee80211_node *ni = vap->iv_bss; 4297 struct ieee80211_channel *c = ni->ni_chan; 4298 int error; 4299 4300 WPI_RXON_LOCK(sc); 4301 4302 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4303 4304 /* Update adapter configuration. */ 4305 sc->rxon.associd = 0; 4306 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS); 4307 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 4308 sc->rxon.chan = ieee80211_chan2ieee(ic, c); 4309 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 4310 if (IEEE80211_IS_CHAN_2GHZ(c)) 4311 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 4312 if (ic->ic_flags & IEEE80211_F_SHSLOT) 4313 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 4314 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 4315 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 4316 if (IEEE80211_IS_CHAN_A(c)) { 4317 sc->rxon.cck_mask = 0; 4318 sc->rxon.ofdm_mask = 0x15; 4319 } else if (IEEE80211_IS_CHAN_B(c)) { 4320 sc->rxon.cck_mask = 0x03; 4321 sc->rxon.ofdm_mask = 0; 4322 } else { 4323 /* Assume 802.11b/g. */ 4324 sc->rxon.cck_mask = 0x0f; 4325 sc->rxon.ofdm_mask = 0x15; 4326 } 4327 4328 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n", 4329 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask, 4330 sc->rxon.ofdm_mask); 4331 4332 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 4333 device_printf(sc->sc_dev, "%s: could not send RXON\n", 4334 __func__); 4335 } 4336 4337 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4338 4339 WPI_RXON_UNLOCK(sc); 4340 4341 return error; 4342} 4343 4344static int 4345wpi_config_beacon(struct wpi_vap *wvp) 4346{ 4347 struct ieee80211vap *vap = &wvp->wv_vap; 4348 struct ieee80211com *ic = vap->iv_ic; 4349 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 4350 struct wpi_buf *bcn = &wvp->wv_bcbuf; 4351 struct wpi_softc *sc = ic->ic_softc; 4352 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data; 4353 struct ieee80211_tim_ie *tie; 4354 struct mbuf *m; 4355 uint8_t *ptr; 4356 int error; 4357 4358 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4359 4360 WPI_VAP_LOCK_ASSERT(wvp); 4361 4362 cmd->len = htole16(bcn->m->m_pkthdr.len); 4363 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 4364 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 4365 4366 /* XXX seems to be unused */ 4367 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) { 4368 tie = (struct ieee80211_tim_ie *) bo->bo_tim; 4369 ptr = mtod(bcn->m, uint8_t *); 4370 4371 cmd->tim = htole16(bo->bo_tim - ptr); 4372 cmd->timsz = tie->tim_len; 4373 } 4374 4375 /* Necessary for recursion in ieee80211_beacon_update(). */ 4376 m = bcn->m; 4377 bcn->m = m_dup(m, M_NOWAIT); 4378 if (bcn->m == NULL) { 4379 device_printf(sc->sc_dev, 4380 "%s: could not copy beacon frame\n", __func__); 4381 error = ENOMEM; 4382 goto end; 4383 } 4384 4385 if ((error = wpi_cmd2(sc, bcn)) != 0) { 4386 device_printf(sc->sc_dev, 4387 "%s: could not update beacon frame, error %d", __func__, 4388 error); 4389 m_freem(bcn->m); 4390 } 4391 4392 /* Restore mbuf. */ 4393end: bcn->m = m; 4394 4395 return error; 4396} 4397 4398static int 4399wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni) 4400{ 4401 struct ieee80211vap *vap = ni->ni_vap; 4402 struct wpi_vap *wvp = WPI_VAP(vap); 4403 struct wpi_buf *bcn = &wvp->wv_bcbuf; 4404 struct mbuf *m; 4405 int error; 4406 4407 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4408 4409 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 4410 return EINVAL; 4411 4412 m = ieee80211_beacon_alloc(ni); 4413 if (m == NULL) { 4414 device_printf(sc->sc_dev, 4415 "%s: could not allocate beacon frame\n", __func__); 4416 return ENOMEM; 4417 } 4418 4419 WPI_VAP_LOCK(wvp); 4420 if (bcn->m != NULL) 4421 m_freem(bcn->m); 4422 4423 bcn->m = m; 4424 4425 error = wpi_config_beacon(wvp); 4426 WPI_VAP_UNLOCK(wvp); 4427 4428 return error; 4429} 4430 4431static void 4432wpi_update_beacon(struct ieee80211vap *vap, int item) 4433{ 4434 struct wpi_softc *sc = vap->iv_ic->ic_softc; 4435 struct wpi_vap *wvp = WPI_VAP(vap); 4436 struct wpi_buf *bcn = &wvp->wv_bcbuf; 4437 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 4438 struct ieee80211_node *ni = vap->iv_bss; 4439 int mcast = 0; 4440 4441 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4442 4443 WPI_VAP_LOCK(wvp); 4444 if (bcn->m == NULL) { 4445 bcn->m = ieee80211_beacon_alloc(ni); 4446 if (bcn->m == NULL) { 4447 device_printf(sc->sc_dev, 4448 "%s: could not allocate beacon frame\n", __func__); 4449 4450 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, 4451 __func__); 4452 4453 WPI_VAP_UNLOCK(wvp); 4454 return; 4455 } 4456 } 4457 WPI_VAP_UNLOCK(wvp); 4458 4459 if (item == IEEE80211_BEACON_TIM) 4460 mcast = 1; /* TODO */ 4461 4462 setbit(bo->bo_flags, item); 4463 ieee80211_beacon_update(ni, bcn->m, mcast); 4464 4465 WPI_VAP_LOCK(wvp); 4466 wpi_config_beacon(wvp); 4467 WPI_VAP_UNLOCK(wvp); 4468 4469 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4470} 4471 4472static void 4473wpi_newassoc(struct ieee80211_node *ni, int isnew) 4474{ 4475 struct ieee80211vap *vap = ni->ni_vap; 4476 struct wpi_softc *sc = ni->ni_ic->ic_softc; 4477 struct wpi_node *wn = WPI_NODE(ni); 4478 int error; 4479 4480 WPI_NT_LOCK(sc); 4481 4482 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4483 4484 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) { 4485 if ((error = wpi_add_ibss_node(sc, ni)) != 0) { 4486 device_printf(sc->sc_dev, 4487 "%s: could not add IBSS node, error %d\n", 4488 __func__, error); 4489 } 4490 } 4491 WPI_NT_UNLOCK(sc); 4492} 4493 4494static int 4495wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap) 4496{ 4497 struct ieee80211com *ic = vap->iv_ic; 4498 struct ieee80211_node *ni = vap->iv_bss; 4499 struct ieee80211_channel *c = ni->ni_chan; 4500 int error; 4501 4502 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4503 4504 if (vap->iv_opmode == IEEE80211_M_MONITOR) { 4505 /* Link LED blinks while monitoring. */ 4506 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 4507 return 0; 4508 } 4509 4510 /* XXX kernel panic workaround */ 4511 if (c == IEEE80211_CHAN_ANYC) { 4512 device_printf(sc->sc_dev, "%s: incomplete configuration\n", 4513 __func__); 4514 return EINVAL; 4515 } 4516 4517 if ((error = wpi_set_timing(sc, ni)) != 0) { 4518 device_printf(sc->sc_dev, 4519 "%s: could not set timing, error %d\n", __func__, error); 4520 return error; 4521 } 4522 4523 /* Update adapter configuration. */ 4524 WPI_RXON_LOCK(sc); 4525 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 4526 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni)); 4527 sc->rxon.chan = ieee80211_chan2ieee(ic, c); 4528 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 4529 if (IEEE80211_IS_CHAN_2GHZ(c)) 4530 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 4531 if (ic->ic_flags & IEEE80211_F_SHSLOT) 4532 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 4533 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 4534 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 4535 if (IEEE80211_IS_CHAN_A(c)) { 4536 sc->rxon.cck_mask = 0; 4537 sc->rxon.ofdm_mask = 0x15; 4538 } else if (IEEE80211_IS_CHAN_B(c)) { 4539 sc->rxon.cck_mask = 0x03; 4540 sc->rxon.ofdm_mask = 0; 4541 } else { 4542 /* Assume 802.11b/g. */ 4543 sc->rxon.cck_mask = 0x0f; 4544 sc->rxon.ofdm_mask = 0x15; 4545 } 4546 sc->rxon.filter |= htole32(WPI_FILTER_BSS); 4547 4548 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n", 4549 sc->rxon.chan, sc->rxon.flags); 4550 4551 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 4552 device_printf(sc->sc_dev, "%s: could not send RXON\n", 4553 __func__); 4554 return error; 4555 } 4556 4557 /* Start periodic calibration timer. */ 4558 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc); 4559 4560 WPI_RXON_UNLOCK(sc); 4561 4562 if (vap->iv_opmode == IEEE80211_M_IBSS || 4563 vap->iv_opmode == IEEE80211_M_HOSTAP) { 4564 if ((error = wpi_setup_beacon(sc, ni)) != 0) { 4565 device_printf(sc->sc_dev, 4566 "%s: could not setup beacon, error %d\n", __func__, 4567 error); 4568 return error; 4569 } 4570 } 4571 4572 if (vap->iv_opmode == IEEE80211_M_STA) { 4573 /* Add BSS node. */ 4574 WPI_NT_LOCK(sc); 4575 error = wpi_add_sta_node(sc, ni); 4576 WPI_NT_UNLOCK(sc); 4577 if (error != 0) { 4578 device_printf(sc->sc_dev, 4579 "%s: could not add BSS node, error %d\n", __func__, 4580 error); 4581 return error; 4582 } 4583 } 4584 4585 /* Link LED always on while associated. */ 4586 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 4587 4588 /* Enable power-saving mode if requested by user. */ 4589 if ((vap->iv_flags & IEEE80211_F_PMGTON) && 4590 vap->iv_opmode != IEEE80211_M_IBSS) 4591 (void)wpi_set_pslevel(sc, 0, 3, 1); 4592 4593 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4594 4595 return 0; 4596} 4597 4598static int 4599wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k) 4600{ 4601 const struct ieee80211_cipher *cip = k->wk_cipher; 4602 struct ieee80211vap *vap = ni->ni_vap; 4603 struct wpi_softc *sc = ni->ni_ic->ic_softc; 4604 struct wpi_node *wn = WPI_NODE(ni); 4605 struct wpi_node_info node; 4606 uint16_t kflags; 4607 int error; 4608 4609 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4610 4611 if (wpi_check_node_entry(sc, wn->id) == 0) { 4612 device_printf(sc->sc_dev, "%s: node does not exist\n", 4613 __func__); 4614 return 0; 4615 } 4616 4617 switch (cip->ic_cipher) { 4618 case IEEE80211_CIPHER_AES_CCM: 4619 kflags = WPI_KFLAG_CCMP; 4620 break; 4621 4622 default: 4623 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__, 4624 cip->ic_cipher); 4625 return 0; 4626 } 4627 4628 kflags |= WPI_KFLAG_KID(k->wk_keyix); 4629 if (k->wk_flags & IEEE80211_KEY_GROUP) 4630 kflags |= WPI_KFLAG_MULTICAST; 4631 4632 memset(&node, 0, sizeof node); 4633 node.id = wn->id; 4634 node.control = WPI_NODE_UPDATE; 4635 node.flags = WPI_FLAG_KEY_SET; 4636 node.kflags = htole16(kflags); 4637 memcpy(node.key, k->wk_key, k->wk_keylen); 4638again: 4639 DPRINTF(sc, WPI_DEBUG_KEY, 4640 "%s: setting %s key id %d for node %d (%s)\n", __func__, 4641 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix, 4642 node.id, ether_sprintf(ni->ni_macaddr)); 4643 4644 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 4645 if (error != 0) { 4646 device_printf(sc->sc_dev, "can't update node info, error %d\n", 4647 error); 4648 return !error; 4649 } 4650 4651 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k && 4652 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) { 4653 kflags |= WPI_KFLAG_MULTICAST; 4654 node.kflags = htole16(kflags); 4655 4656 goto again; 4657 } 4658 4659 return 1; 4660} 4661 4662static void 4663wpi_load_key_cb(void *arg, struct ieee80211_node *ni) 4664{ 4665 const struct ieee80211_key *k = arg; 4666 struct ieee80211vap *vap = ni->ni_vap; 4667 struct wpi_softc *sc = ni->ni_ic->ic_softc; 4668 struct wpi_node *wn = WPI_NODE(ni); 4669 int error; 4670 4671 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED) 4672 return; 4673 4674 WPI_NT_LOCK(sc); 4675 error = wpi_load_key(ni, k); 4676 WPI_NT_UNLOCK(sc); 4677 4678 if (error == 0) { 4679 device_printf(sc->sc_dev, "%s: error while setting key\n", 4680 __func__); 4681 } 4682} 4683 4684static int 4685wpi_set_global_keys(struct ieee80211_node *ni) 4686{ 4687 struct ieee80211vap *vap = ni->ni_vap; 4688 struct ieee80211_key *wk = &vap->iv_nw_keys[0]; 4689 int error = 1; 4690 4691 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++) 4692 if (wk->wk_keyix != IEEE80211_KEYIX_NONE) 4693 error = wpi_load_key(ni, wk); 4694 4695 return !error; 4696} 4697 4698static int 4699wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k) 4700{ 4701 struct ieee80211vap *vap = ni->ni_vap; 4702 struct wpi_softc *sc = ni->ni_ic->ic_softc; 4703 struct wpi_node *wn = WPI_NODE(ni); 4704 struct wpi_node_info node; 4705 uint16_t kflags; 4706 int error; 4707 4708 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4709 4710 if (wpi_check_node_entry(sc, wn->id) == 0) { 4711 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__); 4712 return 1; /* Nothing to do. */ 4713 } 4714 4715 kflags = WPI_KFLAG_KID(k->wk_keyix); 4716 if (k->wk_flags & IEEE80211_KEY_GROUP) 4717 kflags |= WPI_KFLAG_MULTICAST; 4718 4719 memset(&node, 0, sizeof node); 4720 node.id = wn->id; 4721 node.control = WPI_NODE_UPDATE; 4722 node.flags = WPI_FLAG_KEY_SET; 4723 node.kflags = htole16(kflags); 4724again: 4725 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n", 4726 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", 4727 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr)); 4728 4729 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 4730 if (error != 0) { 4731 device_printf(sc->sc_dev, "can't update node info, error %d\n", 4732 error); 4733 return !error; 4734 } 4735 4736 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k && 4737 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) { 4738 kflags |= WPI_KFLAG_MULTICAST; 4739 node.kflags = htole16(kflags); 4740 4741 goto again; 4742 } 4743 4744 return 1; 4745} 4746 4747static void 4748wpi_del_key_cb(void *arg, struct ieee80211_node *ni) 4749{ 4750 const struct ieee80211_key *k = arg; 4751 struct ieee80211vap *vap = ni->ni_vap; 4752 struct wpi_softc *sc = ni->ni_ic->ic_softc; 4753 struct wpi_node *wn = WPI_NODE(ni); 4754 int error; 4755 4756 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED) 4757 return; 4758 4759 WPI_NT_LOCK(sc); 4760 error = wpi_del_key(ni, k); 4761 WPI_NT_UNLOCK(sc); 4762 4763 if (error == 0) { 4764 device_printf(sc->sc_dev, "%s: error while deleting key\n", 4765 __func__); 4766 } 4767} 4768 4769static int 4770wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k, 4771 int set) 4772{ 4773 struct ieee80211com *ic = vap->iv_ic; 4774 struct wpi_softc *sc = ic->ic_softc; 4775 struct wpi_vap *wvp = WPI_VAP(vap); 4776 struct ieee80211_node *ni; 4777 int error, ni_ref = 0; 4778 4779 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4780 4781 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 4782 /* Not for us. */ 4783 return 1; 4784 } 4785 4786 if (!(k->wk_flags & IEEE80211_KEY_RECV)) { 4787 /* XMIT keys are handled in wpi_tx_data(). */ 4788 return 1; 4789 } 4790 4791 /* Handle group keys. */ 4792 if (&vap->iv_nw_keys[0] <= k && 4793 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) { 4794 WPI_NT_LOCK(sc); 4795 if (set) 4796 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix); 4797 else 4798 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix); 4799 WPI_NT_UNLOCK(sc); 4800 4801 if (vap->iv_state == IEEE80211_S_RUN) { 4802 ieee80211_iterate_nodes(&ic->ic_sta, 4803 set ? wpi_load_key_cb : wpi_del_key_cb, 4804 __DECONST(void *, k)); 4805 } 4806 4807 return 1; 4808 } 4809 4810 switch (vap->iv_opmode) { 4811 case IEEE80211_M_STA: 4812 ni = vap->iv_bss; 4813 break; 4814 4815 case IEEE80211_M_IBSS: 4816 case IEEE80211_M_AHDEMO: 4817 case IEEE80211_M_HOSTAP: 4818 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr); 4819 if (ni == NULL) 4820 return 0; /* should not happen */ 4821 4822 ni_ref = 1; 4823 break; 4824 4825 default: 4826 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__, 4827 vap->iv_opmode); 4828 return 0; 4829 } 4830 4831 WPI_NT_LOCK(sc); 4832 if (set) 4833 error = wpi_load_key(ni, k); 4834 else 4835 error = wpi_del_key(ni, k); 4836 WPI_NT_UNLOCK(sc); 4837 4838 if (ni_ref) 4839 ieee80211_node_decref(ni); 4840 4841 return error; 4842} 4843 4844static int 4845wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 4846{ 4847 return wpi_process_key(vap, k, 1); 4848} 4849 4850static int 4851wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 4852{ 4853 return wpi_process_key(vap, k, 0); 4854} 4855 4856/* 4857 * This function is called after the runtime firmware notifies us of its 4858 * readiness (called in a process context). 4859 */ 4860static int 4861wpi_post_alive(struct wpi_softc *sc) 4862{ 4863 int ntries, error; 4864 4865 /* Check (again) that the radio is not disabled. */ 4866 if ((error = wpi_nic_lock(sc)) != 0) 4867 return error; 4868 4869 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4870 4871 /* NB: Runtime firmware must be up and running. */ 4872 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) { 4873 device_printf(sc->sc_dev, 4874 "RF switch: radio disabled (%s)\n", __func__); 4875 wpi_nic_unlock(sc); 4876 return EPERM; /* :-) */ 4877 } 4878 wpi_nic_unlock(sc); 4879 4880 /* Wait for thermal sensor to calibrate. */ 4881 for (ntries = 0; ntries < 1000; ntries++) { 4882 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0) 4883 break; 4884 DELAY(10); 4885 } 4886 4887 if (ntries == 1000) { 4888 device_printf(sc->sc_dev, 4889 "timeout waiting for thermal sensor calibration\n"); 4890 return ETIMEDOUT; 4891 } 4892 4893 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp); 4894 return 0; 4895} 4896 4897/* 4898 * The firmware boot code is small and is intended to be copied directly into 4899 * the NIC internal memory (no DMA transfer). 4900 */ 4901static int 4902wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, uint32_t size) 4903{ 4904 int error, ntries; 4905 4906 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size); 4907 4908 size /= sizeof (uint32_t); 4909 4910 if ((error = wpi_nic_lock(sc)) != 0) 4911 return error; 4912 4913 /* Copy microcode image into NIC memory. */ 4914 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE, 4915 (const uint32_t *)ucode, size); 4916 4917 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0); 4918 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE); 4919 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size); 4920 4921 /* Start boot load now. */ 4922 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START); 4923 4924 /* Wait for transfer to complete. */ 4925 for (ntries = 0; ntries < 1000; ntries++) { 4926 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS); 4927 DPRINTF(sc, WPI_DEBUG_HW, 4928 "firmware status=0x%x, val=0x%x, result=0x%x\n", status, 4929 WPI_FH_TX_STATUS_IDLE(6), 4930 status & WPI_FH_TX_STATUS_IDLE(6)); 4931 if (status & WPI_FH_TX_STATUS_IDLE(6)) { 4932 DPRINTF(sc, WPI_DEBUG_HW, 4933 "Status Match! - ntries = %d\n", ntries); 4934 break; 4935 } 4936 DELAY(10); 4937 } 4938 if (ntries == 1000) { 4939 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 4940 __func__); 4941 wpi_nic_unlock(sc); 4942 return ETIMEDOUT; 4943 } 4944 4945 /* Enable boot after power up. */ 4946 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN); 4947 4948 wpi_nic_unlock(sc); 4949 return 0; 4950} 4951 4952static int 4953wpi_load_firmware(struct wpi_softc *sc) 4954{ 4955 struct wpi_fw_info *fw = &sc->fw; 4956 struct wpi_dma_info *dma = &sc->fw_dma; 4957 int error; 4958 4959 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4960 4961 /* Copy initialization sections into pre-allocated DMA-safe memory. */ 4962 memcpy(dma->vaddr, fw->init.data, fw->init.datasz); 4963 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4964 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz); 4965 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4966 4967 /* Tell adapter where to find initialization sections. */ 4968 if ((error = wpi_nic_lock(sc)) != 0) 4969 return error; 4970 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 4971 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz); 4972 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 4973 dma->paddr + WPI_FW_DATA_MAXSZ); 4974 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz); 4975 wpi_nic_unlock(sc); 4976 4977 /* Load firmware boot code. */ 4978 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz); 4979 if (error != 0) { 4980 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 4981 __func__); 4982 return error; 4983 } 4984 4985 /* Now press "execute". */ 4986 WPI_WRITE(sc, WPI_RESET, 0); 4987 4988 /* Wait at most one second for first alive notification. */ 4989 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 4990 device_printf(sc->sc_dev, 4991 "%s: timeout waiting for adapter to initialize, error %d\n", 4992 __func__, error); 4993 return error; 4994 } 4995 4996 /* Copy runtime sections into pre-allocated DMA-safe memory. */ 4997 memcpy(dma->vaddr, fw->main.data, fw->main.datasz); 4998 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4999 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz); 5000 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 5001 5002 /* Tell adapter where to find runtime sections. */ 5003 if ((error = wpi_nic_lock(sc)) != 0) 5004 return error; 5005 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 5006 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz); 5007 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 5008 dma->paddr + WPI_FW_DATA_MAXSZ); 5009 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, 5010 WPI_FW_UPDATED | fw->main.textsz); 5011 wpi_nic_unlock(sc); 5012 5013 return 0; 5014} 5015 5016static int 5017wpi_read_firmware(struct wpi_softc *sc) 5018{ 5019 const struct firmware *fp; 5020 struct wpi_fw_info *fw = &sc->fw; 5021 const struct wpi_firmware_hdr *hdr; 5022 int error; 5023 5024 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 5025 5026 DPRINTF(sc, WPI_DEBUG_FIRMWARE, 5027 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME); 5028 5029 WPI_UNLOCK(sc); 5030 fp = firmware_get(WPI_FW_NAME); 5031 WPI_LOCK(sc); 5032 5033 if (fp == NULL) { 5034 device_printf(sc->sc_dev, 5035 "could not load firmware image '%s'\n", WPI_FW_NAME); 5036 return EINVAL; 5037 } 5038 5039 sc->fw_fp = fp; 5040 5041 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) { 5042 device_printf(sc->sc_dev, 5043 "firmware file too short: %zu bytes\n", fp->datasize); 5044 error = EINVAL; 5045 goto fail; 5046 } 5047 5048 fw->size = fp->datasize; 5049 fw->data = (const uint8_t *)fp->data; 5050 5051 /* Extract firmware header information. */ 5052 hdr = (const struct wpi_firmware_hdr *)fw->data; 5053 5054 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW | 5055 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */ 5056 5057 fw->main.textsz = le32toh(hdr->rtextsz); 5058 fw->main.datasz = le32toh(hdr->rdatasz); 5059 fw->init.textsz = le32toh(hdr->itextsz); 5060 fw->init.datasz = le32toh(hdr->idatasz); 5061 fw->boot.textsz = le32toh(hdr->btextsz); 5062 fw->boot.datasz = 0; 5063 5064 /* Sanity-check firmware header. */ 5065 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ || 5066 fw->main.datasz > WPI_FW_DATA_MAXSZ || 5067 fw->init.textsz > WPI_FW_TEXT_MAXSZ || 5068 fw->init.datasz > WPI_FW_DATA_MAXSZ || 5069 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ || 5070 (fw->boot.textsz & 3) != 0) { 5071 device_printf(sc->sc_dev, "invalid firmware header\n"); 5072 error = EINVAL; 5073 goto fail; 5074 } 5075 5076 /* Check that all firmware sections fit. */ 5077 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz + 5078 fw->init.textsz + fw->init.datasz + fw->boot.textsz) { 5079 device_printf(sc->sc_dev, 5080 "firmware file too short: %zu bytes\n", fw->size); 5081 error = EINVAL; 5082 goto fail; 5083 } 5084 5085 /* Get pointers to firmware sections. */ 5086 fw->main.text = (const uint8_t *)(hdr + 1); 5087 fw->main.data = fw->main.text + fw->main.textsz; 5088 fw->init.text = fw->main.data + fw->main.datasz; 5089 fw->init.data = fw->init.text + fw->init.textsz; 5090 fw->boot.text = fw->init.data + fw->init.datasz; 5091 5092 DPRINTF(sc, WPI_DEBUG_FIRMWARE, 5093 "Firmware Version: Major %d, Minor %d, Driver %d, \n" 5094 "runtime (text: %u, data: %u) init (text: %u, data %u) " 5095 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver), 5096 fw->main.textsz, fw->main.datasz, 5097 fw->init.textsz, fw->init.datasz, fw->boot.textsz); 5098 5099 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text); 5100 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data); 5101 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text); 5102 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data); 5103 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text); 5104 5105 return 0; 5106 5107fail: wpi_unload_firmware(sc); 5108 return error; 5109} 5110 5111/** 5112 * Free the referenced firmware image 5113 */ 5114static void 5115wpi_unload_firmware(struct wpi_softc *sc) 5116{ 5117 if (sc->fw_fp != NULL) { 5118 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 5119 sc->fw_fp = NULL; 5120 } 5121} 5122 5123static int 5124wpi_clock_wait(struct wpi_softc *sc) 5125{ 5126 int ntries; 5127 5128 /* Set "initialization complete" bit. */ 5129 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE); 5130 5131 /* Wait for clock stabilization. */ 5132 for (ntries = 0; ntries < 2500; ntries++) { 5133 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY) 5134 return 0; 5135 DELAY(100); 5136 } 5137 device_printf(sc->sc_dev, 5138 "%s: timeout waiting for clock stabilization\n", __func__); 5139 5140 return ETIMEDOUT; 5141} 5142 5143static int 5144wpi_apm_init(struct wpi_softc *sc) 5145{ 5146 uint32_t reg; 5147 int error; 5148 5149 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 5150 5151 /* Disable L0s exit timer (NMI bug workaround). */ 5152 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER); 5153 /* Don't wait for ICH L0s (ICH bug workaround). */ 5154 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX); 5155 5156 /* Set FH wait threshold to max (HW bug under stress workaround). */ 5157 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000); 5158 5159 /* Retrieve PCIe Active State Power Management (ASPM). */ 5160 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1); 5161 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */ 5162 if (reg & PCIEM_LINK_CTL_ASPMC_L1) /* L1 Entry enabled. */ 5163 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA); 5164 else 5165 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA); 5166 5167 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT); 5168 5169 /* Wait for clock stabilization before accessing prph. */ 5170 if ((error = wpi_clock_wait(sc)) != 0) 5171 return error; 5172 5173 if ((error = wpi_nic_lock(sc)) != 0) 5174 return error; 5175 /* Cleanup. */ 5176 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400); 5177 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200); 5178 5179 /* Enable DMA and BSM (Bootstrap State Machine). */ 5180 wpi_prph_write(sc, WPI_APMG_CLK_EN, 5181 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT); 5182 DELAY(20); 5183 /* Disable L1-Active. */ 5184 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS); 5185 wpi_nic_unlock(sc); 5186 5187 return 0; 5188} 5189 5190static void 5191wpi_apm_stop_master(struct wpi_softc *sc) 5192{ 5193 int ntries; 5194 5195 /* Stop busmaster DMA activity. */ 5196 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER); 5197 5198 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) == 5199 WPI_GP_CNTRL_MAC_PS) 5200 return; /* Already asleep. */ 5201 5202 for (ntries = 0; ntries < 100; ntries++) { 5203 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED) 5204 return; 5205 DELAY(10); 5206 } 5207 device_printf(sc->sc_dev, "%s: timeout waiting for master\n", 5208 __func__); 5209} 5210 5211static void 5212wpi_apm_stop(struct wpi_softc *sc) 5213{ 5214 wpi_apm_stop_master(sc); 5215 5216 /* Reset the entire device. */ 5217 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW); 5218 DELAY(10); 5219 /* Clear "initialization complete" bit. */ 5220 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE); 5221} 5222 5223static void 5224wpi_nic_config(struct wpi_softc *sc) 5225{ 5226 uint32_t rev; 5227 5228 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 5229 5230 /* voodoo from the Linux "driver".. */ 5231 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1); 5232 if ((rev & 0xc0) == 0x40) 5233 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB); 5234 else if (!(rev & 0x80)) 5235 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM); 5236 5237 if (sc->cap == 0x80) 5238 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC); 5239 5240 if ((sc->rev & 0xf0) == 0xd0) 5241 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 5242 else 5243 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 5244 5245 if (sc->type > 1) 5246 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B); 5247} 5248 5249static int 5250wpi_hw_init(struct wpi_softc *sc) 5251{ 5252 uint8_t chnl; 5253 int ntries, error; 5254 5255 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 5256 5257 /* Clear pending interrupts. */ 5258 WPI_WRITE(sc, WPI_INT, 0xffffffff); 5259 5260 if ((error = wpi_apm_init(sc)) != 0) { 5261 device_printf(sc->sc_dev, 5262 "%s: could not power ON adapter, error %d\n", __func__, 5263 error); 5264 return error; 5265 } 5266 5267 /* Select VMAIN power source. */ 5268 if ((error = wpi_nic_lock(sc)) != 0) 5269 return error; 5270 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK); 5271 wpi_nic_unlock(sc); 5272 /* Spin until VMAIN gets selected. */ 5273 for (ntries = 0; ntries < 5000; ntries++) { 5274 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN) 5275 break; 5276 DELAY(10); 5277 } 5278 if (ntries == 5000) { 5279 device_printf(sc->sc_dev, "timeout selecting power source\n"); 5280 return ETIMEDOUT; 5281 } 5282 5283 /* Perform adapter initialization. */ 5284 wpi_nic_config(sc); 5285 5286 /* Initialize RX ring. */ 5287 if ((error = wpi_nic_lock(sc)) != 0) 5288 return error; 5289 /* Set physical address of RX ring. */ 5290 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr); 5291 /* Set physical address of RX read pointer. */ 5292 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr + 5293 offsetof(struct wpi_shared, next)); 5294 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0); 5295 /* Enable RX. */ 5296 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 5297 WPI_FH_RX_CONFIG_DMA_ENA | 5298 WPI_FH_RX_CONFIG_RDRBD_ENA | 5299 WPI_FH_RX_CONFIG_WRSTATUS_ENA | 5300 WPI_FH_RX_CONFIG_MAXFRAG | 5301 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) | 5302 WPI_FH_RX_CONFIG_IRQ_DST_HOST | 5303 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1)); 5304 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */ 5305 wpi_nic_unlock(sc); 5306 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7); 5307 5308 /* Initialize TX rings. */ 5309 if ((error = wpi_nic_lock(sc)) != 0) 5310 return error; 5311 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */ 5312 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */ 5313 /* Enable all 6 TX rings. */ 5314 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f); 5315 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000); 5316 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002); 5317 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4); 5318 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5); 5319 /* Set physical address of TX rings. */ 5320 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr); 5321 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5); 5322 5323 /* Enable all DMA channels. */ 5324 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 5325 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0); 5326 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0); 5327 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008); 5328 } 5329 wpi_nic_unlock(sc); 5330 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */ 5331 5332 /* Clear "radio off" and "commands blocked" bits. */ 5333 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 5334 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED); 5335 5336 /* Clear pending interrupts. */ 5337 WPI_WRITE(sc, WPI_INT, 0xffffffff); 5338 /* Enable interrupts. */ 5339 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF); 5340 5341 /* _Really_ make sure "radio off" bit is cleared! */ 5342 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 5343 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 5344 5345 if ((error = wpi_load_firmware(sc)) != 0) { 5346 device_printf(sc->sc_dev, 5347 "%s: could not load firmware, error %d\n", __func__, 5348 error); 5349 return error; 5350 } 5351 /* Wait at most one second for firmware alive notification. */ 5352 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 5353 device_printf(sc->sc_dev, 5354 "%s: timeout waiting for adapter to initialize, error %d\n", 5355 __func__, error); 5356 return error; 5357 } 5358 5359 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 5360 5361 /* Do post-firmware initialization. */ 5362 return wpi_post_alive(sc); 5363} 5364 5365static void 5366wpi_hw_stop(struct wpi_softc *sc) 5367{ 5368 uint8_t chnl, qid; 5369 int ntries; 5370 5371 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 5372 5373 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) 5374 wpi_nic_lock(sc); 5375 5376 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO); 5377 5378 /* Disable interrupts. */ 5379 WPI_WRITE(sc, WPI_INT_MASK, 0); 5380 WPI_WRITE(sc, WPI_INT, 0xffffffff); 5381 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff); 5382 5383 /* Make sure we no longer hold the NIC lock. */ 5384 wpi_nic_unlock(sc); 5385 5386 if (wpi_nic_lock(sc) == 0) { 5387 /* Stop TX scheduler. */ 5388 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0); 5389 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0); 5390 5391 /* Stop all DMA channels. */ 5392 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 5393 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0); 5394 for (ntries = 0; ntries < 200; ntries++) { 5395 if (WPI_READ(sc, WPI_FH_TX_STATUS) & 5396 WPI_FH_TX_STATUS_IDLE(chnl)) 5397 break; 5398 DELAY(10); 5399 } 5400 } 5401 wpi_nic_unlock(sc); 5402 } 5403 5404 /* Stop RX ring. */ 5405 wpi_reset_rx_ring(sc); 5406 5407 /* Reset all TX rings. */ 5408 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) 5409 wpi_reset_tx_ring(sc, &sc->txq[qid]); 5410 5411 if (wpi_nic_lock(sc) == 0) { 5412 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 5413 WPI_APMG_CLK_CTRL_DMA_CLK_RQT); 5414 wpi_nic_unlock(sc); 5415 } 5416 DELAY(5); 5417 /* Power OFF adapter. */ 5418 wpi_apm_stop(sc); 5419} 5420 5421static void 5422wpi_radio_on(void *arg0, int pending) 5423{ 5424 struct wpi_softc *sc = arg0; 5425 struct ieee80211com *ic = &sc->sc_ic; 5426 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 5427 5428 device_printf(sc->sc_dev, "RF switch: radio enabled\n"); 5429 5430 WPI_LOCK(sc); 5431 callout_stop(&sc->watchdog_rfkill); 5432 WPI_UNLOCK(sc); 5433 5434 if (vap != NULL) 5435 ieee80211_init(vap); 5436} 5437 5438static void 5439wpi_radio_off(void *arg0, int pending) 5440{ 5441 struct wpi_softc *sc = arg0; 5442 struct ieee80211com *ic = &sc->sc_ic; 5443 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 5444 5445 device_printf(sc->sc_dev, "RF switch: radio disabled\n"); 5446 5447 ieee80211_notify_radio(ic, 0); 5448 wpi_stop(sc); 5449 if (vap != NULL) 5450 ieee80211_stop(vap); 5451 5452 WPI_LOCK(sc); 5453 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc); 5454 WPI_UNLOCK(sc); 5455} 5456 5457static int 5458wpi_init(struct wpi_softc *sc) 5459{ 5460 int error = 0; 5461 5462 WPI_LOCK(sc); 5463 5464 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 5465 5466 if (sc->sc_running != 0) 5467 goto end; 5468 5469 /* Check that the radio is not disabled by hardware switch. */ 5470 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) { 5471 device_printf(sc->sc_dev, 5472 "RF switch: radio disabled (%s)\n", __func__); 5473 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, 5474 sc); 5475 error = EINPROGRESS; 5476 goto end; 5477 } 5478 5479 /* Read firmware images from the filesystem. */ 5480 if ((error = wpi_read_firmware(sc)) != 0) { 5481 device_printf(sc->sc_dev, 5482 "%s: could not read firmware, error %d\n", __func__, 5483 error); 5484 goto end; 5485 } 5486 5487 sc->sc_running = 1; 5488 5489 /* Initialize hardware and upload firmware. */ 5490 error = wpi_hw_init(sc); 5491 wpi_unload_firmware(sc); 5492 if (error != 0) { 5493 device_printf(sc->sc_dev, 5494 "%s: could not initialize hardware, error %d\n", __func__, 5495 error); 5496 goto fail; 5497 } 5498 5499 /* Configure adapter now that it is ready. */ 5500 if ((error = wpi_config(sc)) != 0) { 5501 device_printf(sc->sc_dev, 5502 "%s: could not configure device, error %d\n", __func__, 5503 error); 5504 goto fail; 5505 } 5506 5507 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 5508 5509 WPI_UNLOCK(sc); 5510 5511 return 0; 5512 5513fail: wpi_stop_locked(sc); 5514 5515end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 5516 WPI_UNLOCK(sc); 5517 5518 return error; 5519} 5520 5521static void 5522wpi_stop_locked(struct wpi_softc *sc) 5523{ 5524 5525 WPI_LOCK_ASSERT(sc); 5526 5527 if (sc->sc_running == 0) 5528 return; 5529 5530 WPI_TX_LOCK(sc); 5531 WPI_TXQ_LOCK(sc); 5532 sc->sc_running = 0; 5533 WPI_TXQ_UNLOCK(sc); 5534 WPI_TX_UNLOCK(sc); 5535 5536 WPI_TXQ_STATE_LOCK(sc); 5537 callout_stop(&sc->tx_timeout); 5538 WPI_TXQ_STATE_UNLOCK(sc); 5539 5540 WPI_RXON_LOCK(sc); 5541 callout_stop(&sc->scan_timeout); 5542 callout_stop(&sc->calib_to); 5543 WPI_RXON_UNLOCK(sc); 5544 5545 /* Power OFF hardware. */ 5546 wpi_hw_stop(sc); 5547} 5548 5549static void 5550wpi_stop(struct wpi_softc *sc) 5551{ 5552 WPI_LOCK(sc); 5553 wpi_stop_locked(sc); 5554 WPI_UNLOCK(sc); 5555} 5556 5557/* 5558 * Callback from net80211 to start a scan. 5559 */ 5560static void 5561wpi_scan_start(struct ieee80211com *ic) 5562{ 5563 struct wpi_softc *sc = ic->ic_softc; 5564 5565 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 5566} 5567 5568/* 5569 * Callback from net80211 to terminate a scan. 5570 */ 5571static void 5572wpi_scan_end(struct ieee80211com *ic) 5573{ 5574 struct wpi_softc *sc = ic->ic_softc; 5575 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 5576 5577 if (vap->iv_state == IEEE80211_S_RUN) 5578 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 5579} 5580 5581/** 5582 * Called by the net80211 framework to indicate to the driver 5583 * that the channel should be changed 5584 */ 5585static void 5586wpi_set_channel(struct ieee80211com *ic) 5587{ 5588 const struct ieee80211_channel *c = ic->ic_curchan; 5589 struct wpi_softc *sc = ic->ic_softc; 5590 int error; 5591 5592 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 5593 5594 WPI_LOCK(sc); 5595 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq); 5596 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags); 5597 WPI_UNLOCK(sc); 5598 WPI_TX_LOCK(sc); 5599 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq); 5600 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags); 5601 WPI_TX_UNLOCK(sc); 5602 5603 /* 5604 * Only need to set the channel in Monitor mode. AP scanning and auth 5605 * are already taken care of by their respective firmware commands. 5606 */ 5607 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 5608 WPI_RXON_LOCK(sc); 5609 sc->rxon.chan = ieee80211_chan2ieee(ic, c); 5610 if (IEEE80211_IS_CHAN_2GHZ(c)) { 5611 sc->rxon.flags |= htole32(WPI_RXON_AUTO | 5612 WPI_RXON_24GHZ); 5613 } else { 5614 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO | 5615 WPI_RXON_24GHZ); 5616 } 5617 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) 5618 device_printf(sc->sc_dev, 5619 "%s: error %d setting channel\n", __func__, 5620 error); 5621 WPI_RXON_UNLOCK(sc); 5622 } 5623} 5624 5625/** 5626 * Called by net80211 to indicate that we need to scan the current 5627 * channel. The channel is previously be set via the wpi_set_channel 5628 * callback. 5629 */ 5630static void 5631wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 5632{ 5633 struct ieee80211vap *vap = ss->ss_vap; 5634 struct ieee80211com *ic = vap->iv_ic; 5635 struct wpi_softc *sc = ic->ic_softc; 5636 int error; 5637 5638 WPI_RXON_LOCK(sc); 5639 error = wpi_scan(sc, ic->ic_curchan); 5640 WPI_RXON_UNLOCK(sc); 5641 if (error != 0) 5642 ieee80211_cancel_scan(vap); 5643} 5644 5645/** 5646 * Called by the net80211 framework to indicate 5647 * the minimum dwell time has been met, terminate the scan. 5648 * We don't actually terminate the scan as the firmware will notify 5649 * us when it's finished and we have no way to interrupt it. 5650 */ 5651static void 5652wpi_scan_mindwell(struct ieee80211_scan_state *ss) 5653{ 5654 /* NB: don't try to abort scan; wait for firmware to finish */ 5655} 5656