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