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