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