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