if_wpi.c revision 280065
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 280065 2015-03-15 20:30:03Z 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 > WPI_CMD_QUEUE_NUM) 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 = 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[WPI_CMD_QUEUE_NUM]; 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 & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM) 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 & WPI_UNSOLICITED_RX_NOTIF)) { 1965 /* Reply to a command. */ 1966 wpi_cmd_done(sc, desc); 1967 } 1968 1969 switch (desc->type) { 1970 case WPI_RX_DONE: 1971 /* An 802.11 frame has been received. */ 1972 wpi_rx_done(sc, desc, data); 1973 1974 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1975 /* wpi_stop() was called. */ 1976 return; 1977 } 1978 1979 break; 1980 1981 case WPI_TX_DONE: 1982 /* An 802.11 frame has been transmitted. */ 1983 wpi_tx_done(sc, desc); 1984 break; 1985 1986 case WPI_RX_STATISTICS: 1987 case WPI_BEACON_STATISTICS: 1988 wpi_rx_statistics(sc, desc, data); 1989 break; 1990 1991 case WPI_BEACON_MISSED: 1992 { 1993 struct wpi_beacon_missed *miss = 1994 (struct wpi_beacon_missed *)(desc + 1); 1995 int misses; 1996 1997 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1998 BUS_DMASYNC_POSTREAD); 1999 misses = le32toh(miss->consecutive); 2000 2001 DPRINTF(sc, WPI_DEBUG_STATE, 2002 "%s: beacons missed %d/%d\n", __func__, misses, 2003 le32toh(miss->total)); 2004 2005 if (vap->iv_state == IEEE80211_S_RUN && 2006 (ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2007 if (misses >= vap->iv_bmissthreshold) { 2008 WPI_UNLOCK(sc); 2009 ieee80211_beacon_miss(ic); 2010 WPI_LOCK(sc); 2011 } 2012 } 2013 break; 2014 } 2015 case WPI_UC_READY: 2016 { 2017 struct wpi_ucode_info *uc = 2018 (struct wpi_ucode_info *)(desc + 1); 2019 2020 /* The microcontroller is ready. */ 2021 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2022 BUS_DMASYNC_POSTREAD); 2023 DPRINTF(sc, WPI_DEBUG_RESET, 2024 "microcode alive notification version=%d.%d " 2025 "subtype=%x alive=%x\n", uc->major, uc->minor, 2026 uc->subtype, le32toh(uc->valid)); 2027 2028 if (le32toh(uc->valid) != 1) { 2029 device_printf(sc->sc_dev, 2030 "microcontroller initialization failed\n"); 2031 wpi_stop_locked(sc); 2032 } 2033 /* Save the address of the error log in SRAM. */ 2034 sc->errptr = le32toh(uc->errptr); 2035 break; 2036 } 2037 case WPI_STATE_CHANGED: 2038 { 2039 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2040 BUS_DMASYNC_POSTREAD); 2041 2042 uint32_t *status = (uint32_t *)(desc + 1); 2043 2044 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n", 2045 le32toh(*status)); 2046 2047 if (le32toh(*status) & 1) { 2048 ieee80211_runtask(ic, &sc->sc_radiooff_task); 2049 return; 2050 } 2051 break; 2052 } 2053 case WPI_START_SCAN: 2054 { 2055 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2056 BUS_DMASYNC_POSTREAD); 2057#ifdef WPI_DEBUG 2058 struct wpi_start_scan *scan = 2059 (struct wpi_start_scan *)(desc + 1); 2060 DPRINTF(sc, WPI_DEBUG_SCAN, 2061 "%s: scanning channel %d status %x\n", 2062 __func__, scan->chan, le32toh(scan->status)); 2063#endif 2064 break; 2065 } 2066 case WPI_STOP_SCAN: 2067 { 2068 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2069 BUS_DMASYNC_POSTREAD); 2070#ifdef WPI_DEBUG 2071 struct wpi_stop_scan *scan = 2072 (struct wpi_stop_scan *)(desc + 1); 2073 DPRINTF(sc, WPI_DEBUG_SCAN, 2074 "scan finished nchan=%d status=%d chan=%d\n", 2075 scan->nchan, scan->status, scan->chan); 2076#endif 2077 sc->sc_scan_timer = 0; 2078 WPI_UNLOCK(sc); 2079 ieee80211_scan_next(vap); 2080 WPI_LOCK(sc); 2081 break; 2082 } 2083 } 2084 } 2085 2086 /* Tell the firmware what we have processed. */ 2087 wpi_update_rx_ring(sc); 2088} 2089 2090/* 2091 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up 2092 * from power-down sleep mode. 2093 */ 2094static void 2095wpi_wakeup_intr(struct wpi_softc *sc) 2096{ 2097 int qid; 2098 2099 DPRINTF(sc, WPI_DEBUG_PWRSAVE, 2100 "%s: ucode wakeup from power-down sleep\n", __func__); 2101 2102 /* Wakeup RX and TX rings. */ 2103 if (sc->rxq.update) { 2104 sc->rxq.update = 0; 2105 wpi_update_rx_ring(sc); 2106 } 2107 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) { 2108 struct wpi_tx_ring *ring = &sc->txq[qid]; 2109 2110 if (ring->update) { 2111 ring->update = 0; 2112 wpi_update_tx_ring(sc, ring); 2113 } 2114 } 2115 2116 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 2117} 2118 2119/* 2120 * Dump the error log of the firmware when a firmware panic occurs. Although 2121 * we can't debug the firmware because it is neither open source nor free, it 2122 * can help us to identify certain classes of problems. 2123 */ 2124static void 2125wpi_fatal_intr(struct wpi_softc *sc) 2126{ 2127 struct wpi_fw_dump dump; 2128 uint32_t i, offset, count; 2129 const uint32_t size_errmsg = 2130 (sizeof (wpi_fw_errmsg) / sizeof ((wpi_fw_errmsg)[0])); 2131 2132 /* Check that the error log address is valid. */ 2133 if (sc->errptr < WPI_FW_DATA_BASE || 2134 sc->errptr + sizeof (dump) > 2135 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) { 2136 printf("%s: bad firmware error log address 0x%08x\n", __func__, 2137 sc->errptr); 2138 return; 2139 } 2140 if (wpi_nic_lock(sc) != 0) { 2141 printf("%s: could not read firmware error log\n", __func__); 2142 return; 2143 } 2144 /* Read number of entries in the log. */ 2145 count = wpi_mem_read(sc, sc->errptr); 2146 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) { 2147 printf("%s: invalid count field (count = %u)\n", __func__, 2148 count); 2149 wpi_nic_unlock(sc); 2150 return; 2151 } 2152 /* Skip "count" field. */ 2153 offset = sc->errptr + sizeof (uint32_t); 2154 printf("firmware error log (count = %u):\n", count); 2155 for (i = 0; i < count; i++) { 2156 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump, 2157 sizeof (dump) / sizeof (uint32_t)); 2158 2159 printf(" error type = \"%s\" (0x%08X)\n", 2160 (dump.desc < size_errmsg) ? 2161 wpi_fw_errmsg[dump.desc] : "UNKNOWN", 2162 dump.desc); 2163 printf(" error data = 0x%08X\n", 2164 dump.data); 2165 printf(" branch link = 0x%08X%08X\n", 2166 dump.blink[0], dump.blink[1]); 2167 printf(" interrupt link = 0x%08X%08X\n", 2168 dump.ilink[0], dump.ilink[1]); 2169 printf(" time = %u\n", dump.time); 2170 2171 offset += sizeof (dump); 2172 } 2173 wpi_nic_unlock(sc); 2174 /* Dump driver status (TX and RX rings) while we're here. */ 2175 printf("driver status:\n"); 2176 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) { 2177 struct wpi_tx_ring *ring = &sc->txq[i]; 2178 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n", 2179 i, ring->qid, ring->cur, ring->queued); 2180 } 2181 printf(" rx ring: cur=%d\n", sc->rxq.cur); 2182} 2183 2184static void 2185wpi_intr(void *arg) 2186{ 2187 struct wpi_softc *sc = arg; 2188 struct ifnet *ifp = sc->sc_ifp; 2189 uint32_t r1, r2; 2190 2191 WPI_LOCK(sc); 2192 2193 /* Disable interrupts. */ 2194 WPI_WRITE(sc, WPI_INT_MASK, 0); 2195 2196 r1 = WPI_READ(sc, WPI_INT); 2197 2198 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0) 2199 goto end; /* Hardware gone! */ 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 goto end; 2224 } 2225 2226 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) || 2227 (r2 & WPI_FH_INT_RX)) 2228 wpi_notif_intr(sc); 2229 2230 if (r1 & WPI_INT_ALIVE) 2231 wakeup(sc); /* Firmware is alive. */ 2232 2233 if (r1 & WPI_INT_WAKEUP) 2234 wpi_wakeup_intr(sc); 2235 2236done: 2237 /* Re-enable interrupts. */ 2238 if (ifp->if_flags & IFF_UP) 2239 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF); 2240 2241end: WPI_UNLOCK(sc); 2242} 2243 2244static int 2245wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf) 2246{ 2247 struct ieee80211_frame *wh; 2248 struct wpi_tx_cmd *cmd; 2249 struct wpi_tx_data *data; 2250 struct wpi_tx_desc *desc; 2251 struct wpi_tx_ring *ring; 2252 struct mbuf *m1; 2253 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER]; 2254 int error, i, hdrlen, nsegs, totlen, pad; 2255 2256 WPI_LOCK_ASSERT(sc); 2257 2258 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 2259 2260 wh = mtod(buf->m, struct ieee80211_frame *); 2261 hdrlen = ieee80211_anyhdrsize(wh); 2262 totlen = buf->m->m_pkthdr.len; 2263 2264 if (hdrlen & 3) { 2265 /* First segment length must be a multiple of 4. */ 2266 pad = 4 - (hdrlen & 3); 2267 } else 2268 pad = 0; 2269 2270 ring = &sc->txq[buf->ac]; 2271 desc = &ring->desc[ring->cur]; 2272 data = &ring->data[ring->cur]; 2273 2274 /* Prepare TX firmware command. */ 2275 cmd = &ring->cmd[ring->cur]; 2276 cmd->code = buf->code; 2277 cmd->flags = 0; 2278 cmd->qid = ring->qid; 2279 cmd->idx = ring->cur; 2280 2281 memcpy(cmd->data, buf->data, buf->size); 2282 2283 /* Save and trim IEEE802.11 header. */ 2284 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen); 2285 m_adj(buf->m, hdrlen); 2286 2287 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m, 2288 segs, &nsegs, BUS_DMA_NOWAIT); 2289 if (error != 0 && error != EFBIG) { 2290 device_printf(sc->sc_dev, 2291 "%s: can't map mbuf (error %d)\n", __func__, error); 2292 m_freem(buf->m); 2293 return error; 2294 } 2295 if (error != 0) { 2296 /* Too many DMA segments, linearize mbuf. */ 2297 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1); 2298 if (m1 == NULL) { 2299 device_printf(sc->sc_dev, 2300 "%s: could not defrag mbuf\n", __func__); 2301 m_freem(buf->m); 2302 return ENOBUFS; 2303 } 2304 buf->m = m1; 2305 2306 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, 2307 buf->m, segs, &nsegs, BUS_DMA_NOWAIT); 2308 if (error != 0) { 2309 device_printf(sc->sc_dev, 2310 "%s: can't map mbuf (error %d)\n", __func__, error); 2311 m_freem(buf->m); 2312 return error; 2313 } 2314 } 2315 2316 KASSERT(nsegs < WPI_MAX_SCATTER, 2317 ("too many DMA segments, nsegs (%d) should be less than %d", 2318 nsegs, WPI_MAX_SCATTER)); 2319 2320 data->m = buf->m; 2321 data->ni = buf->ni; 2322 2323 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 2324 __func__, ring->qid, ring->cur, totlen, nsegs); 2325 2326 /* Fill TX descriptor. */ 2327 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs); 2328 /* First DMA segment is used by the TX command. */ 2329 desc->segs[0].addr = htole32(data->cmd_paddr); 2330 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad); 2331 /* Other DMA segments are for data payload. */ 2332 seg = &segs[0]; 2333 for (i = 1; i <= nsegs; i++) { 2334 desc->segs[i].addr = htole32(seg->ds_addr); 2335 desc->segs[i].len = htole32(seg->ds_len); 2336 seg++; 2337 } 2338 2339 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2340 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 2341 BUS_DMASYNC_PREWRITE); 2342 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2343 BUS_DMASYNC_PREWRITE); 2344 2345 /* Kick TX ring. */ 2346 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2347 wpi_update_tx_ring(sc, ring); 2348 2349 /* Mark TX ring as full if we reach a certain threshold. */ 2350 if (++ring->queued > WPI_TX_RING_HIMARK) 2351 sc->qfullmsk |= 1 << ring->qid; 2352 2353 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 2354 2355 return 0; 2356} 2357 2358/* 2359 * Construct the data packet for a transmit buffer. 2360 */ 2361static int 2362wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 2363{ 2364 const struct ieee80211_txparam *tp; 2365 struct ieee80211vap *vap = ni->ni_vap; 2366 struct ieee80211com *ic = ni->ni_ic; 2367 struct wpi_node *wn = WPI_NODE(ni); 2368 struct ieee80211_channel *chan; 2369 struct ieee80211_frame *wh; 2370 struct ieee80211_key *k = NULL; 2371 struct wpi_cmd_data tx; 2372 struct wpi_buf tx_data; 2373 uint32_t flags; 2374 uint16_t qos; 2375 uint8_t tid, type; 2376 int ac, error, rate, ismcast, totlen; 2377 2378 wh = mtod(m, struct ieee80211_frame *); 2379 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2380 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 2381 2382 /* Select EDCA Access Category and TX ring for this frame. */ 2383 if (IEEE80211_QOS_HAS_SEQ(wh)) { 2384 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 2385 tid = qos & IEEE80211_QOS_TID; 2386 } else { 2387 qos = 0; 2388 tid = 0; 2389 } 2390 ac = M_WME_GETAC(m); 2391 2392 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ? 2393 ni->ni_chan : ic->ic_curchan; 2394 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)]; 2395 2396 /* Choose a TX rate index. */ 2397 if (type == IEEE80211_FC0_TYPE_MGT) 2398 rate = tp->mgmtrate; 2399 else if (ismcast) 2400 rate = tp->mcastrate; 2401 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 2402 rate = tp->ucastrate; 2403 else if (m->m_flags & M_EAPOL) 2404 rate = tp->mgmtrate; 2405 else { 2406 /* XXX pass pktlen */ 2407 (void) ieee80211_ratectl_rate(ni, NULL, 0); 2408 rate = ni->ni_txrate; 2409 } 2410 2411 /* Encrypt the frame if need be. */ 2412 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 2413 /* Retrieve key for TX. */ 2414 k = ieee80211_crypto_encap(ni, m); 2415 if (k == NULL) { 2416 error = ENOBUFS; 2417 goto fail; 2418 } 2419 /* 802.11 header may have moved. */ 2420 wh = mtod(m, struct ieee80211_frame *); 2421 } 2422 totlen = m->m_pkthdr.len; 2423 2424 if (ieee80211_radiotap_active_vap(vap)) { 2425 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2426 2427 tap->wt_flags = 0; 2428 tap->wt_rate = rate; 2429 if (k != NULL) 2430 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 2431 2432 ieee80211_radiotap_tx(vap, m); 2433 } 2434 2435 flags = 0; 2436 if (!ismcast) { 2437 /* Unicast frame, check if an ACK is expected. */ 2438 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 2439 IEEE80211_QOS_ACKPOLICY_NOACK) 2440 flags |= WPI_TX_NEED_ACK; 2441 } 2442 2443 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) 2444 flags |= WPI_TX_MORE_FRAG; /* Cannot happen yet. */ 2445 2446 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */ 2447 if (!ismcast) { 2448 /* NB: Group frames are sent using CCK in 802.11b/g. */ 2449 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) { 2450 flags |= WPI_TX_NEED_RTS; 2451 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 2452 WPI_RATE_IS_OFDM(rate)) { 2453 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 2454 flags |= WPI_TX_NEED_CTS; 2455 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 2456 flags |= WPI_TX_NEED_RTS; 2457 } 2458 2459 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS)) 2460 flags |= WPI_TX_FULL_TXOP; 2461 } 2462 2463 memset(&tx, 0, sizeof (struct wpi_cmd_data)); 2464 if (type == IEEE80211_FC0_TYPE_MGT) { 2465 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2466 2467 /* Tell HW to set timestamp in probe responses. */ 2468 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2469 flags |= WPI_TX_INSERT_TSTAMP; 2470 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2471 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2472 tx.timeout = htole16(3); 2473 else 2474 tx.timeout = htole16(2); 2475 } 2476 2477 if (ismcast || type != IEEE80211_FC0_TYPE_DATA) 2478 tx.id = WPI_ID_BROADCAST; 2479 else { 2480 if (wn->id == WPI_ID_UNDEFINED && 2481 (vap->iv_opmode == IEEE80211_M_IBSS || 2482 vap->iv_opmode == IEEE80211_M_AHDEMO)) { 2483 error = wpi_add_ibss_node(sc, ni); 2484 if (error != 0) { 2485 device_printf(sc->sc_dev, 2486 "%s: could not add IBSS node, error %d\n", 2487 __func__, error); 2488 goto fail; 2489 } 2490 } 2491 2492 if (wn->id == WPI_ID_UNDEFINED) { 2493 device_printf(sc->sc_dev, 2494 "%s: undefined node id\n", __func__); 2495 error = EINVAL; 2496 goto fail; 2497 } 2498 2499 tx.id = wn->id; 2500 } 2501 2502 if (type != IEEE80211_FC0_TYPE_MGT) 2503 tx.data_ntries = tp->maxretry; 2504 2505 tx.len = htole16(totlen); 2506 tx.flags = htole32(flags); 2507 tx.plcp = rate2plcp(rate); 2508 tx.tid = tid; 2509 tx.lifetime = htole32(WPI_LIFETIME_INFINITE); 2510 tx.ofdm_mask = 0xff; 2511 tx.cck_mask = 0x0f; 2512 tx.rts_ntries = 7; 2513 2514 if (k != NULL && k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM) { 2515 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) { 2516 tx.security = WPI_CIPHER_CCMP; 2517 memcpy(tx.key, k->wk_key, k->wk_keylen); 2518 } 2519 } 2520 2521 tx_data.data = &tx; 2522 tx_data.ni = ni; 2523 tx_data.m = m; 2524 tx_data.size = sizeof(tx); 2525 tx_data.code = WPI_CMD_TX_DATA; 2526 tx_data.ac = ac; 2527 2528 return wpi_cmd2(sc, &tx_data); 2529 2530fail: m_freem(m); 2531 return error; 2532} 2533 2534static int 2535wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni, 2536 const struct ieee80211_bpf_params *params) 2537{ 2538 struct ieee80211vap *vap = ni->ni_vap; 2539 struct ieee80211_frame *wh; 2540 struct wpi_cmd_data tx; 2541 struct wpi_buf tx_data; 2542 uint32_t flags; 2543 uint8_t type; 2544 int ac, rate, totlen; 2545 2546 wh = mtod(m, struct ieee80211_frame *); 2547 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2548 totlen = m->m_pkthdr.len; 2549 2550 ac = params->ibp_pri & 3; 2551 2552 /* Choose a TX rate index. */ 2553 rate = params->ibp_rate0; 2554 2555 flags = 0; 2556 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 2557 flags |= WPI_TX_NEED_ACK; 2558 if (params->ibp_flags & IEEE80211_BPF_RTS) 2559 flags |= WPI_TX_NEED_RTS; 2560 if (params->ibp_flags & IEEE80211_BPF_CTS) 2561 flags |= WPI_TX_NEED_CTS; 2562 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS)) 2563 flags |= WPI_TX_FULL_TXOP; 2564 2565 if (ieee80211_radiotap_active_vap(vap)) { 2566 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2567 2568 tap->wt_flags = 0; 2569 tap->wt_rate = rate; 2570 2571 ieee80211_radiotap_tx(vap, m); 2572 } 2573 2574 memset(&tx, 0, sizeof (struct wpi_cmd_data)); 2575 if (type == IEEE80211_FC0_TYPE_MGT) { 2576 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2577 2578 /* Tell HW to set timestamp in probe responses. */ 2579 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2580 flags |= WPI_TX_INSERT_TSTAMP; 2581 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2582 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2583 tx.timeout = htole16(3); 2584 else 2585 tx.timeout = htole16(2); 2586 } 2587 2588 tx.len = htole16(totlen); 2589 tx.flags = htole32(flags); 2590 tx.plcp = rate2plcp(rate); 2591 tx.id = WPI_ID_BROADCAST; 2592 tx.lifetime = htole32(WPI_LIFETIME_INFINITE); 2593 tx.rts_ntries = params->ibp_try1; 2594 tx.data_ntries = params->ibp_try0; 2595 2596 tx_data.data = &tx; 2597 tx_data.ni = ni; 2598 tx_data.m = m; 2599 tx_data.size = sizeof(tx); 2600 tx_data.code = WPI_CMD_TX_DATA; 2601 tx_data.ac = ac; 2602 2603 return wpi_cmd2(sc, &tx_data); 2604} 2605 2606static int 2607wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2608 const struct ieee80211_bpf_params *params) 2609{ 2610 struct ieee80211com *ic = ni->ni_ic; 2611 struct ifnet *ifp = ic->ic_ifp; 2612 struct wpi_softc *sc = ifp->if_softc; 2613 int error = 0; 2614 2615 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 2616 2617 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2618 ieee80211_free_node(ni); 2619 m_freem(m); 2620 return ENETDOWN; 2621 } 2622 2623 WPI_LOCK(sc); 2624 if (params == NULL) { 2625 /* 2626 * Legacy path; interpret frame contents to decide 2627 * precisely how to send the frame. 2628 */ 2629 error = wpi_tx_data(sc, m, ni); 2630 } else { 2631 /* 2632 * Caller supplied explicit parameters to use in 2633 * sending the frame. 2634 */ 2635 error = wpi_tx_data_raw(sc, m, ni, params); 2636 } 2637 WPI_UNLOCK(sc); 2638 2639 if (error != 0) { 2640 /* NB: m is reclaimed on tx failure */ 2641 ieee80211_free_node(ni); 2642 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2643 2644 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 2645 2646 return error; 2647 } 2648 2649 sc->sc_tx_timer = 5; 2650 2651 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 2652 2653 return 0; 2654} 2655 2656/** 2657 * Process data waiting to be sent on the IFNET output queue 2658 */ 2659static void 2660wpi_start(struct ifnet *ifp) 2661{ 2662 struct wpi_softc *sc = ifp->if_softc; 2663 2664 WPI_LOCK(sc); 2665 wpi_start_locked(ifp); 2666 WPI_UNLOCK(sc); 2667} 2668 2669static void 2670wpi_start_locked(struct ifnet *ifp) 2671{ 2672 struct wpi_softc *sc = ifp->if_softc; 2673 struct ieee80211_node *ni; 2674 struct mbuf *m; 2675 2676 WPI_LOCK_ASSERT(sc); 2677 2678 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__); 2679 2680 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2681 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) 2682 return; 2683 2684 for (;;) { 2685 if (sc->qfullmsk != 0) { 2686 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2687 break; 2688 } 2689 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 2690 if (m == NULL) 2691 break; 2692 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2693 if (wpi_tx_data(sc, m, ni) != 0) { 2694 WPI_UNLOCK(sc); 2695 ieee80211_free_node(ni); 2696 WPI_LOCK(sc); 2697 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2698 } else 2699 sc->sc_tx_timer = 5; 2700 } 2701 2702 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__); 2703} 2704 2705static void 2706wpi_watchdog_rfkill(void *arg) 2707{ 2708 struct wpi_softc *sc = arg; 2709 struct ifnet *ifp = sc->sc_ifp; 2710 struct ieee80211com *ic = ifp->if_l2com; 2711 2712 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n"); 2713 2714 /* No need to lock firmware memory. */ 2715 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) { 2716 /* Radio kill switch is still off. */ 2717 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, 2718 sc); 2719 } else 2720 ieee80211_runtask(ic, &sc->sc_radioon_task); 2721} 2722 2723/** 2724 * Called every second, wpi_watchdog used by the watch dog timer 2725 * to check that the card is still alive 2726 */ 2727static void 2728wpi_watchdog(void *arg) 2729{ 2730 struct wpi_softc *sc = arg; 2731 struct ifnet *ifp = sc->sc_ifp; 2732 struct ieee80211com *ic = ifp->if_l2com; 2733 2734 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "Watchdog: tick\n"); 2735 2736 if (sc->sc_tx_timer > 0) { 2737 if (--sc->sc_tx_timer == 0) { 2738 if_printf(ifp, "device timeout\n"); 2739 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2740 ieee80211_runtask(ic, &sc->sc_reinittask); 2741 } 2742 } 2743 2744 if (sc->sc_scan_timer > 0) { 2745 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2746 if (--sc->sc_scan_timer == 0 && vap != NULL) { 2747 if_printf(ifp, "scan timeout\n"); 2748 ieee80211_cancel_scan(vap); 2749 ieee80211_runtask(ic, &sc->sc_reinittask); 2750 } 2751 } 2752 2753 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2754 callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc); 2755} 2756 2757static int 2758wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2759{ 2760 struct wpi_softc *sc = ifp->if_softc; 2761 struct ieee80211com *ic = ifp->if_l2com; 2762 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2763 struct ifreq *ifr = (struct ifreq *) data; 2764 int error = 0, startall = 0, stop = 0; 2765 2766 switch (cmd) { 2767 case SIOCGIFADDR: 2768 error = ether_ioctl(ifp, cmd, data); 2769 break; 2770 case SIOCSIFFLAGS: 2771 WPI_LOCK(sc); 2772 if (ifp->if_flags & IFF_UP) { 2773 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2774 wpi_init_locked(sc); 2775 if (WPI_READ(sc, WPI_GP_CNTRL) & 2776 WPI_GP_CNTRL_RFKILL) 2777 startall = 1; 2778 else 2779 stop = 1; 2780 } 2781 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2782 wpi_stop_locked(sc); 2783 WPI_UNLOCK(sc); 2784 if (startall) 2785 ieee80211_start_all(ic); 2786 else if (vap != NULL && stop) 2787 ieee80211_stop(vap); 2788 break; 2789 case SIOCGIFMEDIA: 2790 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 2791 break; 2792 default: 2793 error = EINVAL; 2794 break; 2795 } 2796 return error; 2797} 2798 2799/* 2800 * Send a command to the firmware. 2801 */ 2802static int 2803wpi_cmd(struct wpi_softc *sc, int code, const void *buf, size_t size, 2804 int async) 2805{ 2806 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM]; 2807 struct wpi_tx_desc *desc; 2808 struct wpi_tx_data *data; 2809 struct wpi_tx_cmd *cmd; 2810 struct mbuf *m; 2811 bus_addr_t paddr; 2812 int totlen, error; 2813 2814 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 2815 2816 if (async == 0) 2817 WPI_LOCK_ASSERT(sc); 2818 2819 DPRINTF(sc, WPI_DEBUG_CMD, "wpi_cmd %s size %zu async %d\n", 2820 wpi_cmd_str(code), size, async); 2821 2822 if (sc->flags & WPI_FLAG_BUSY) { 2823 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n", 2824 __func__, code); 2825 return EAGAIN; 2826 } 2827 sc->flags |= WPI_FLAG_BUSY; 2828 2829 desc = &ring->desc[ring->cur]; 2830 data = &ring->data[ring->cur]; 2831 totlen = 4 + size; 2832 2833 if (size > sizeof cmd->data) { 2834 /* Command is too large to fit in a descriptor. */ 2835 if (totlen > MCLBYTES) 2836 return EINVAL; 2837 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 2838 if (m == NULL) 2839 return ENOMEM; 2840 cmd = mtod(m, struct wpi_tx_cmd *); 2841 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, 2842 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 2843 if (error != 0) { 2844 m_freem(m); 2845 return error; 2846 } 2847 data->m = m; 2848 } else { 2849 cmd = &ring->cmd[ring->cur]; 2850 paddr = data->cmd_paddr; 2851 } 2852 2853 cmd->code = code; 2854 cmd->flags = 0; 2855 cmd->qid = ring->qid; 2856 cmd->idx = ring->cur; 2857 memcpy(cmd->data, buf, size); 2858 2859 desc->nsegs = 1 + (WPI_PAD32(size) << 4); 2860 desc->segs[0].addr = htole32(paddr); 2861 desc->segs[0].len = htole32(totlen); 2862 2863 if (size > sizeof cmd->data) { 2864 bus_dmamap_sync(ring->data_dmat, data->map, 2865 BUS_DMASYNC_PREWRITE); 2866 } else { 2867 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 2868 BUS_DMASYNC_PREWRITE); 2869 } 2870 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2871 BUS_DMASYNC_PREWRITE); 2872 2873 /* Kick command ring. */ 2874 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2875 wpi_update_tx_ring(sc, ring); 2876 2877 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 2878 2879 if (async) { 2880 sc->flags &= ~WPI_FLAG_BUSY; 2881 return 0; 2882 } 2883 2884 return mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz); 2885} 2886 2887/* 2888 * Configure HW multi-rate retries. 2889 */ 2890static int 2891wpi_mrr_setup(struct wpi_softc *sc) 2892{ 2893 struct ifnet *ifp = sc->sc_ifp; 2894 struct ieee80211com *ic = ifp->if_l2com; 2895 struct wpi_mrr_setup mrr; 2896 int i, error; 2897 2898 /* CCK rates (not used with 802.11a). */ 2899 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) { 2900 mrr.rates[i].flags = 0; 2901 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 2902 /* Fallback to the immediate lower CCK rate (if any.) */ 2903 mrr.rates[i].next = 2904 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1; 2905 /* Try one time at this rate before falling back to "next". */ 2906 mrr.rates[i].ntries = 1; 2907 } 2908 /* OFDM rates (not used with 802.11b). */ 2909 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) { 2910 mrr.rates[i].flags = 0; 2911 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 2912 /* Fallback to the immediate lower rate (if any.) */ 2913 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */ 2914 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ? 2915 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 2916 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) : 2917 i - 1; 2918 /* Try one time at this rate before falling back to "next". */ 2919 mrr.rates[i].ntries = 1; 2920 } 2921 /* Setup MRR for control frames. */ 2922 mrr.which = htole32(WPI_MRR_CTL); 2923 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2924 if (error != 0) { 2925 device_printf(sc->sc_dev, 2926 "could not setup MRR for control frames\n"); 2927 return error; 2928 } 2929 /* Setup MRR for data frames. */ 2930 mrr.which = htole32(WPI_MRR_DATA); 2931 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2932 if (error != 0) { 2933 device_printf(sc->sc_dev, 2934 "could not setup MRR for data frames\n"); 2935 return error; 2936 } 2937 return 0; 2938} 2939 2940static int 2941wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni) 2942{ 2943 struct ieee80211com *ic = ni->ni_ic; 2944 struct wpi_node *wn = WPI_NODE(ni); 2945 struct wpi_node_info node; 2946 2947 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 2948 2949 if (wn->id == WPI_ID_UNDEFINED) 2950 return EINVAL; 2951 2952 memset(&node, 0, sizeof node); 2953 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 2954 node.id = wn->id; 2955 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2956 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 2957 node.action = htole32(WPI_ACTION_SET_RATE); 2958 node.antenna = WPI_ANTENNA_BOTH; 2959 2960 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2961} 2962 2963/* 2964 * Broadcast node is used to send group-addressed and management frames. 2965 */ 2966static int 2967wpi_add_broadcast_node(struct wpi_softc *sc, int async) 2968{ 2969 struct ifnet *ifp = sc->sc_ifp; 2970 struct ieee80211com *ic = ifp->if_l2com; 2971 struct wpi_node_info node; 2972 2973 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 2974 2975 memset(&node, 0, sizeof node); 2976 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr); 2977 node.id = WPI_ID_BROADCAST; 2978 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2979 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 2980 node.action = htole32(WPI_ACTION_SET_RATE); 2981 node.antenna = WPI_ANTENNA_BOTH; 2982 2983 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async); 2984} 2985 2986static int 2987wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni) 2988{ 2989 struct wpi_node *wn = WPI_NODE(ni); 2990 2991 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 2992 2993 if (wn->id != WPI_ID_UNDEFINED) 2994 return EINVAL; 2995 2996 wn->id = alloc_unrl(sc->sc_unr); 2997 2998 if (wn->id == (uint8_t)-1) 2999 return ENOBUFS; 3000 3001 return wpi_add_node(sc, ni); 3002} 3003 3004static void 3005wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni) 3006{ 3007 struct wpi_node *wn = WPI_NODE(ni); 3008 struct wpi_cmd_del_node node; 3009 int error; 3010 3011 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3012 3013 if (wn->id == WPI_ID_UNDEFINED) { 3014 device_printf(sc->sc_dev, "%s: undefined node id passed\n", 3015 __func__); 3016 return; 3017 } 3018 3019 memset(&node, 0, sizeof node); 3020 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 3021 node.count = 1; 3022 3023 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1); 3024 if (error != 0) { 3025 device_printf(sc->sc_dev, 3026 "%s: could not delete node %u, error %d\n", __func__, 3027 wn->id, error); 3028 } 3029} 3030 3031static int 3032wpi_updateedca(struct ieee80211com *ic) 3033{ 3034#define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 3035 struct wpi_softc *sc = ic->ic_ifp->if_softc; 3036 struct wpi_edca_params cmd; 3037 int aci, error; 3038 3039 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3040 3041 memset(&cmd, 0, sizeof cmd); 3042 cmd.flags = htole32(WPI_EDCA_UPDATE); 3043 for (aci = 0; aci < WME_NUM_AC; aci++) { 3044 const struct wmeParams *ac = 3045 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci]; 3046 cmd.ac[aci].aifsn = ac->wmep_aifsn; 3047 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin)); 3048 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax)); 3049 cmd.ac[aci].txoplimit = 3050 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit)); 3051 3052 DPRINTF(sc, WPI_DEBUG_EDCA, 3053 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 3054 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn, 3055 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax, 3056 cmd.ac[aci].txoplimit); 3057 } 3058 IEEE80211_UNLOCK(ic); 3059 WPI_LOCK(sc); 3060 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1); 3061 WPI_UNLOCK(sc); 3062 IEEE80211_LOCK(ic); 3063 3064 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3065 3066 return error; 3067#undef WPI_EXP2 3068} 3069 3070static void 3071wpi_set_promisc(struct wpi_softc *sc) 3072{ 3073 struct ifnet *ifp = sc->sc_ifp; 3074 uint32_t promisc_filter; 3075 3076 promisc_filter = WPI_FILTER_PROMISC | WPI_FILTER_CTL; 3077 3078 if (ifp->if_flags & IFF_PROMISC) 3079 sc->rxon.filter |= htole32(promisc_filter); 3080 else 3081 sc->rxon.filter &= ~htole32(promisc_filter); 3082} 3083 3084static void 3085wpi_update_promisc(struct ifnet *ifp) 3086{ 3087 struct wpi_softc *sc = ifp->if_softc; 3088 3089 wpi_set_promisc(sc); 3090 3091 WPI_LOCK(sc); 3092 if (wpi_send_rxon(sc, 1, 1) != 0) { 3093 device_printf(sc->sc_dev, "%s: could not send RXON\n", 3094 __func__); 3095 } 3096 WPI_UNLOCK(sc); 3097} 3098 3099static void 3100wpi_update_mcast(struct ifnet *ifp) 3101{ 3102 /* Ignore */ 3103} 3104 3105static void 3106wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 3107{ 3108 struct wpi_cmd_led led; 3109 3110 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3111 3112 led.which = which; 3113 led.unit = htole32(100000); /* on/off in unit of 100ms */ 3114 led.off = off; 3115 led.on = on; 3116 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 3117} 3118 3119static int 3120wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni) 3121{ 3122 struct wpi_cmd_timing cmd; 3123 uint64_t val, mod; 3124 3125 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3126 3127 memset(&cmd, 0, sizeof cmd); 3128 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t)); 3129 cmd.bintval = htole16(ni->ni_intval); 3130 cmd.lintval = htole16(10); 3131 3132 /* Compute remaining time until next beacon. */ 3133 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU; 3134 mod = le64toh(cmd.tstamp) % val; 3135 cmd.binitval = htole32((uint32_t)(val - mod)); 3136 3137 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n", 3138 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod)); 3139 3140 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1); 3141} 3142 3143/* 3144 * This function is called periodically (every 60 seconds) to adjust output 3145 * power to temperature changes. 3146 */ 3147static void 3148wpi_power_calibration(struct wpi_softc *sc) 3149{ 3150 int temp; 3151 3152 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3153 3154 /* Update sensor data. */ 3155 temp = (int)WPI_READ(sc, WPI_UCODE_GP2); 3156 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp); 3157 3158 /* Sanity-check read value. */ 3159 if (temp < -260 || temp > 25) { 3160 /* This can't be correct, ignore. */ 3161 DPRINTF(sc, WPI_DEBUG_TEMP, 3162 "out-of-range temperature reported: %d\n", temp); 3163 return; 3164 } 3165 3166 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp); 3167 3168 /* Adjust Tx power if need be. */ 3169 if (abs(temp - sc->temp) <= 6) 3170 return; 3171 3172 sc->temp = temp; 3173 3174 if (wpi_set_txpower(sc, 1) != 0) { 3175 /* just warn, too bad for the automatic calibration... */ 3176 device_printf(sc->sc_dev,"could not adjust Tx power\n"); 3177 } 3178} 3179 3180/* 3181 * Set TX power for current channel. 3182 */ 3183static int 3184wpi_set_txpower(struct wpi_softc *sc, int async) 3185{ 3186 struct ifnet *ifp = sc->sc_ifp; 3187 struct ieee80211com *ic = ifp->if_l2com; 3188 struct ieee80211_channel *ch; 3189 struct wpi_power_group *group; 3190 struct wpi_cmd_txpower cmd; 3191 uint8_t chan; 3192 int idx, i; 3193 3194 /* Retrieve current channel from last RXON. */ 3195 chan = sc->rxon.chan; 3196 ch = &ic->ic_channels[chan]; 3197 3198 /* Find the TX power group to which this channel belongs. */ 3199 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 3200 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 3201 if (chan <= group->chan) 3202 break; 3203 } else 3204 group = &sc->groups[0]; 3205 3206 memset(&cmd, 0, sizeof cmd); 3207 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1; 3208 cmd.chan = htole16(chan); 3209 3210 /* Set TX power for all OFDM and CCK rates. */ 3211 for (i = 0; i <= WPI_RIDX_MAX ; i++) { 3212 /* Retrieve TX power for this channel/rate. */ 3213 idx = wpi_get_power_index(sc, group, ch, i); 3214 3215 cmd.rates[i].plcp = wpi_ridx_to_plcp[i]; 3216 3217 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 3218 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx]; 3219 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx]; 3220 } else { 3221 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx]; 3222 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx]; 3223 } 3224 DPRINTF(sc, WPI_DEBUG_TEMP, 3225 "chan %d/ridx %d: power index %d\n", chan, i, idx); 3226 } 3227 3228 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async); 3229} 3230 3231/* 3232 * Determine Tx power index for a given channel/rate combination. 3233 * This takes into account the regulatory information from EEPROM and the 3234 * current temperature. 3235 */ 3236static int 3237wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 3238 struct ieee80211_channel *c, int ridx) 3239{ 3240/* Fixed-point arithmetic division using a n-bit fractional part. */ 3241#define fdivround(a, b, n) \ 3242 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 3243 3244/* Linear interpolation. */ 3245#define interpolate(x, x1, y1, x2, y2, n) \ 3246 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 3247 3248 struct ifnet *ifp = sc->sc_ifp; 3249 struct ieee80211com *ic = ifp->if_l2com; 3250 struct wpi_power_sample *sample; 3251 int pwr, idx; 3252 u_int chan; 3253 3254 /* Get channel number. */ 3255 chan = ieee80211_chan2ieee(ic, c); 3256 3257 /* Default TX power is group maximum TX power minus 3dB. */ 3258 pwr = group->maxpwr / 2; 3259 3260 /* Decrease TX power for highest OFDM rates to reduce distortion. */ 3261 switch (ridx) { 3262 case WPI_RIDX_OFDM36: 3263 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5; 3264 break; 3265 case WPI_RIDX_OFDM48: 3266 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10; 3267 break; 3268 case WPI_RIDX_OFDM54: 3269 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12; 3270 break; 3271 } 3272 3273 /* Never exceed the channel maximum allowed TX power. */ 3274 pwr = min(pwr, sc->maxpwr[chan]); 3275 3276 /* Retrieve TX power index into gain tables from samples. */ 3277 for (sample = group->samples; sample < &group->samples[3]; sample++) 3278 if (pwr > sample[1].power) 3279 break; 3280 /* Fixed-point linear interpolation using a 19-bit fractional part. */ 3281 idx = interpolate(pwr, sample[0].power, sample[0].index, 3282 sample[1].power, sample[1].index, 19); 3283 3284 /*- 3285 * Adjust power index based on current temperature: 3286 * - if cooler than factory-calibrated: decrease output power 3287 * - if warmer than factory-calibrated: increase output power 3288 */ 3289 idx -= (sc->temp - group->temp) * 11 / 100; 3290 3291 /* Decrease TX power for CCK rates (-5dB). */ 3292 if (ridx >= WPI_RIDX_CCK1) 3293 idx += 10; 3294 3295 /* Make sure idx stays in a valid range. */ 3296 if (idx < 0) 3297 return 0; 3298 if (idx > WPI_MAX_PWR_INDEX) 3299 return WPI_MAX_PWR_INDEX; 3300 return idx; 3301 3302#undef interpolate 3303#undef fdivround 3304} 3305 3306/* 3307 * Set STA mode power saving level (between 0 and 5). 3308 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving. 3309 */ 3310static int 3311wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async) 3312{ 3313 struct wpi_pmgt_cmd cmd; 3314 const struct wpi_pmgt *pmgt; 3315 uint32_t max, skip_dtim; 3316 uint32_t reg; 3317 int i; 3318 3319 DPRINTF(sc, WPI_DEBUG_PWRSAVE, 3320 "%s: dtim=%d, level=%d, async=%d\n", 3321 __func__, dtim, level, async); 3322 3323 /* Select which PS parameters to use. */ 3324 if (dtim <= 10) 3325 pmgt = &wpi_pmgt[0][level]; 3326 else 3327 pmgt = &wpi_pmgt[1][level]; 3328 3329 memset(&cmd, 0, sizeof cmd); 3330 if (level != 0) /* not CAM */ 3331 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP); 3332 /* Retrieve PCIe Active State Power Management (ASPM). */ 3333 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1); 3334 if (!(reg & 0x1)) /* L0s Entry disabled. */ 3335 cmd.flags |= htole16(WPI_PS_PCI_PMGT); 3336 3337 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU); 3338 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU); 3339 3340 if (dtim == 0) { 3341 dtim = 1; 3342 skip_dtim = 0; 3343 } else 3344 skip_dtim = pmgt->skip_dtim; 3345 3346 if (skip_dtim != 0) { 3347 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM); 3348 max = pmgt->intval[4]; 3349 if (max == (uint32_t)-1) 3350 max = dtim * (skip_dtim + 1); 3351 else if (max > dtim) 3352 max = (max / dtim) * dtim; 3353 } else 3354 max = dtim; 3355 3356 for (i = 0; i < 5; i++) 3357 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i])); 3358 3359 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async); 3360} 3361 3362static int 3363wpi_send_btcoex(struct wpi_softc *sc) 3364{ 3365 struct wpi_bluetooth cmd; 3366 3367 memset(&cmd, 0, sizeof cmd); 3368 cmd.flags = WPI_BT_COEX_MODE_4WIRE; 3369 cmd.lead_time = WPI_BT_LEAD_TIME_DEF; 3370 cmd.max_kill = WPI_BT_MAX_KILL_DEF; 3371 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n", 3372 __func__); 3373 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0); 3374} 3375 3376static int 3377wpi_send_rxon(struct wpi_softc *sc, int assoc, int async) 3378{ 3379 int error; 3380 3381 if (assoc && (sc->rxon.filter & htole32(WPI_FILTER_BSS))) { 3382 struct wpi_assoc rxon_assoc; 3383 3384 rxon_assoc.flags = sc->rxon.flags; 3385 rxon_assoc.filter = sc->rxon.filter; 3386 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask; 3387 rxon_assoc.cck_mask = sc->rxon.cck_mask; 3388 rxon_assoc.reserved = 0; 3389 3390 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc, 3391 sizeof (struct wpi_assoc), async); 3392 } else { 3393 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, 3394 sizeof (struct wpi_rxon), async); 3395 } 3396 if (error != 0) { 3397 device_printf(sc->sc_dev, "RXON command failed, error %d\n", 3398 error); 3399 return error; 3400 } 3401 3402 /* Configuration has changed, set Tx power accordingly. */ 3403 if ((error = wpi_set_txpower(sc, async)) != 0) { 3404 device_printf(sc->sc_dev, 3405 "%s: could not set TX power, error %d\n", __func__, error); 3406 return error; 3407 } 3408 3409 if (!(sc->rxon.filter & htole32(WPI_FILTER_BSS))) { 3410 /* Add broadcast node. */ 3411 error = wpi_add_broadcast_node(sc, async); 3412 if (error != 0) { 3413 device_printf(sc->sc_dev, 3414 "could not add broadcast node, error %d\n", error); 3415 return error; 3416 } 3417 } 3418 3419 return 0; 3420} 3421 3422/** 3423 * Configure the card to listen to a particular channel, this transisions the 3424 * card in to being able to receive frames from remote devices. 3425 */ 3426static int 3427wpi_config(struct wpi_softc *sc) 3428{ 3429 struct ifnet *ifp = sc->sc_ifp; 3430 struct ieee80211com *ic = ifp->if_l2com; 3431 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3432 uint32_t flags; 3433 int error; 3434 3435 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3436 3437 /* Set power saving level to CAM during initialization. */ 3438 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) { 3439 device_printf(sc->sc_dev, 3440 "%s: could not set power saving level\n", __func__); 3441 return error; 3442 } 3443 3444 /* Configure bluetooth coexistence. */ 3445 if ((error = wpi_send_btcoex(sc)) != 0) { 3446 device_printf(sc->sc_dev, 3447 "could not configure bluetooth coexistence\n"); 3448 return error; 3449 } 3450 3451 /* Configure adapter. */ 3452 memset(&sc->rxon, 0, sizeof (struct wpi_rxon)); 3453 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr); 3454 3455 /* Set default channel. */ 3456 sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_curchan); 3457 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 3458 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 3459 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 3460 3461 sc->rxon.filter = WPI_FILTER_MULTICAST; 3462 switch (ic->ic_opmode) { 3463 case IEEE80211_M_STA: 3464 sc->rxon.mode = WPI_MODE_STA; 3465 break; 3466 case IEEE80211_M_IBSS: 3467 sc->rxon.mode = WPI_MODE_IBSS; 3468 sc->rxon.filter |= WPI_FILTER_BEACON; 3469 break; 3470 /* XXX workaround for passive channels selection */ 3471 case IEEE80211_M_AHDEMO: 3472 case IEEE80211_M_HOSTAP: 3473 sc->rxon.mode = WPI_MODE_HOSTAP; 3474 break; 3475 case IEEE80211_M_MONITOR: 3476 sc->rxon.mode = WPI_MODE_MONITOR; 3477 break; 3478 default: 3479 device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode); 3480 return EINVAL; 3481 } 3482 sc->rxon.filter = htole32(sc->rxon.filter); 3483 wpi_set_promisc(sc); 3484 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */ 3485 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */ 3486 3487 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) { 3488 device_printf(sc->sc_dev, "%s: could not send RXON\n", 3489 __func__); 3490 return error; 3491 } 3492 3493 /* Setup rate scalling. */ 3494 if ((error = wpi_mrr_setup(sc)) != 0) { 3495 device_printf(sc->sc_dev, "could not setup MRR, error %d\n", 3496 error); 3497 return error; 3498 } 3499 3500 /* Disable beacon notifications (unused). */ 3501 flags = WPI_STATISTICS_BEACON_DISABLE; 3502 error = wpi_cmd(sc, WPI_CMD_GET_STATISTICS, &flags, sizeof flags, 1); 3503 if (error != 0) { 3504 device_printf(sc->sc_dev, 3505 "could not disable beacon statistics, error %d\n", error); 3506 return error; 3507 } 3508 3509 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3510 3511 return 0; 3512} 3513 3514static uint16_t 3515wpi_get_active_dwell_time(struct wpi_softc *sc, 3516 struct ieee80211_channel *c, uint8_t n_probes) 3517{ 3518 /* No channel? Default to 2GHz settings. */ 3519 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) { 3520 return (WPI_ACTIVE_DWELL_TIME_2GHZ + 3521 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1)); 3522 } 3523 3524 /* 5GHz dwell time. */ 3525 return (WPI_ACTIVE_DWELL_TIME_5GHZ + 3526 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1)); 3527} 3528 3529/* 3530 * Limit the total dwell time to 85% of the beacon interval. 3531 * 3532 * Returns the dwell time in milliseconds. 3533 */ 3534static uint16_t 3535wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time) 3536{ 3537 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3538 struct ieee80211vap *vap = NULL; 3539 int bintval = 0; 3540 3541 /* bintval is in TU (1.024mS) */ 3542 if (! TAILQ_EMPTY(&ic->ic_vaps)) { 3543 vap = TAILQ_FIRST(&ic->ic_vaps); 3544 bintval = vap->iv_bss->ni_intval; 3545 } 3546 3547 /* 3548 * If it's non-zero, we should calculate the minimum of 3549 * it and the DWELL_BASE. 3550 * 3551 * XXX Yes, the math should take into account that bintval 3552 * is 1.024mS, not 1mS.. 3553 */ 3554 if (bintval > 0) { 3555 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__, 3556 bintval); 3557 return (MIN(WPI_PASSIVE_DWELL_BASE, ((bintval * 85) / 100))); 3558 } 3559 3560 /* No association context? Default. */ 3561 return (WPI_PASSIVE_DWELL_BASE); 3562} 3563 3564static uint16_t 3565wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c) 3566{ 3567 uint16_t passive; 3568 3569 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) 3570 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ; 3571 else 3572 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ; 3573 3574 /* Clamp to the beacon interval if we're associated. */ 3575 return (wpi_limit_dwell(sc, passive)); 3576} 3577 3578/* 3579 * Send a scan request to the firmware. 3580 */ 3581static int 3582wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c) 3583{ 3584 struct ifnet *ifp = sc->sc_ifp; 3585 struct ieee80211com *ic = ifp->if_l2com; 3586 struct ieee80211_scan_state *ss = ic->ic_scan; 3587 struct ieee80211vap *vap = ss->ss_vap; 3588 struct wpi_scan_hdr *hdr; 3589 struct wpi_cmd_data *tx; 3590 struct wpi_scan_essid *essids; 3591 struct wpi_scan_chan *chan; 3592 struct ieee80211_frame *wh; 3593 struct ieee80211_rateset *rs; 3594 uint16_t dwell_active, dwell_passive; 3595 uint8_t *buf, *frm; 3596 int buflen, error, i, nssid; 3597 3598 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3599 3600 /* 3601 * We are absolutely not allowed to send a scan command when another 3602 * scan command is pending. 3603 */ 3604 if (sc->sc_scan_timer) { 3605 device_printf(sc->sc_dev, "%s: called whilst scanning!\n", 3606 __func__); 3607 3608 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 3609 3610 return (EAGAIN); 3611 } 3612 3613 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO); 3614 if (buf == NULL) { 3615 device_printf(sc->sc_dev, 3616 "%s: could not allocate buffer for scan command\n", 3617 __func__); 3618 error = ENOMEM; 3619 goto fail; 3620 } 3621 hdr = (struct wpi_scan_hdr *)buf; 3622 3623 /* 3624 * Move to the next channel if no packets are received within 10 msecs 3625 * after sending the probe request. 3626 */ 3627 hdr->quiet_time = htole16(10); /* timeout in milliseconds */ 3628 hdr->quiet_threshold = htole16(1); /* min # of packets */ 3629 /* 3630 * Max needs to be greater than active and passive and quiet! 3631 * It's also in microseconds! 3632 */ 3633 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU); 3634 hdr->pause_svc = htole32((4 << 24) | 3635 (100 * IEEE80211_DUR_TU)); /* Hardcode for now */ 3636 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON); 3637 3638 tx = (struct wpi_cmd_data *)(hdr + 1); 3639 tx->flags = htole32(WPI_TX_AUTO_SEQ); 3640 tx->id = WPI_ID_BROADCAST; 3641 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 3642 3643 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3644 /* Send probe requests at 6Mbps. */ 3645 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6]; 3646 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A]; 3647 } else { 3648 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO); 3649 /* Send probe requests at 1Mbps. */ 3650 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 3651 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 3652 } 3653 3654 essids = (struct wpi_scan_essid *)(tx + 1); 3655 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS); 3656 for (i = 0; i < nssid; i++) { 3657 essids[i].id = IEEE80211_ELEMID_SSID; 3658 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN); 3659 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len); 3660#ifdef WPI_DEBUG 3661 if (sc->sc_debug & WPI_DEBUG_SCAN) { 3662 printf("Scanning Essid: "); 3663 ieee80211_print_essid(essids[i].data, essids[i].len); 3664 printf("\n"); 3665 } 3666#endif 3667 } 3668 3669 /* 3670 * Build a probe request frame. Most of the following code is a 3671 * copy & paste of what is done in net80211. 3672 */ 3673 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS); 3674 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3675 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 3676 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3677 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 3678 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr); 3679 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 3680 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 3681 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 3682 3683 frm = (uint8_t *)(wh + 1); 3684 frm = ieee80211_add_ssid(frm, NULL, 0); 3685 frm = ieee80211_add_rates(frm, rs); 3686 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 3687 frm = ieee80211_add_xrates(frm, rs); 3688 3689 /* Set length of probe request. */ 3690 tx->len = htole16(frm - (uint8_t *)wh); 3691 3692 /* 3693 * Construct information about the channel that we 3694 * want to scan. The firmware expects this to be directly 3695 * after the scan probe request 3696 */ 3697 chan = (struct wpi_scan_chan *)frm; 3698 chan->chan = htole16(ieee80211_chan2ieee(ic, c)); 3699 chan->flags = 0; 3700 if (nssid) { 3701 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT; 3702 chan->flags |= WPI_CHAN_NPBREQS(nssid); 3703 } else 3704 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER; 3705 3706 if (!IEEE80211_IS_CHAN_PASSIVE(c)) 3707 chan->flags |= WPI_CHAN_ACTIVE; 3708 3709 /* 3710 * Calculate the active/passive dwell times. 3711 */ 3712 3713 dwell_active = wpi_get_active_dwell_time(sc, c, nssid); 3714 dwell_passive = wpi_get_passive_dwell_time(sc, c); 3715 3716 /* Make sure they're valid. */ 3717 if (dwell_passive <= dwell_active) 3718 dwell_passive = dwell_active + 1; 3719 3720 chan->active = htole16(dwell_active); 3721 chan->passive = htole16(dwell_passive); 3722 3723 chan->dsp_gain = 0x6e; /* Default level */ 3724 3725 if (IEEE80211_IS_CHAN_5GHZ(c)) 3726 chan->rf_gain = 0x3b; 3727 else 3728 chan->rf_gain = 0x28; 3729 3730 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n", 3731 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c)); 3732 3733 hdr->nchan++; 3734 chan++; 3735 3736 buflen = (uint8_t *)chan - buf; 3737 hdr->len = htole16(buflen); 3738 3739 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n", 3740 hdr->nchan); 3741 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1); 3742 free(buf, M_DEVBUF); 3743 3744 if (error != 0) 3745 goto fail; 3746 3747 sc->sc_scan_timer = 5; 3748 3749 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3750 3751 return 0; 3752 3753fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 3754 3755 return error; 3756} 3757 3758static int 3759wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap) 3760{ 3761 struct ieee80211com *ic = vap->iv_ic; 3762 struct ieee80211_node *ni = vap->iv_bss; 3763 int error; 3764 3765 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3766 3767 /* Update adapter configuration. */ 3768 sc->rxon.associd = 0; 3769 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS); 3770 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 3771 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 3772 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 3773 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 3774 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 3775 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3776 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 3777 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3778 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 3779 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) { 3780 sc->rxon.cck_mask = 0; 3781 sc->rxon.ofdm_mask = 0x15; 3782 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) { 3783 sc->rxon.cck_mask = 0x03; 3784 sc->rxon.ofdm_mask = 0; 3785 } else { 3786 /* Assume 802.11b/g. */ 3787 sc->rxon.cck_mask = 0x0f; 3788 sc->rxon.ofdm_mask = 0x15; 3789 } 3790 3791 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n", 3792 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask, 3793 sc->rxon.ofdm_mask); 3794 3795 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 3796 device_printf(sc->sc_dev, "%s: could not send RXON\n", 3797 __func__); 3798 } 3799 3800 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3801 3802 return error; 3803} 3804 3805static int 3806wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni) 3807{ 3808 struct ifnet *ifp = sc->sc_ifp; 3809 struct ieee80211com *ic = ifp->if_l2com; 3810 struct ieee80211vap *vap = ni->ni_vap; 3811 struct wpi_vap *wvp = WPI_VAP(vap); 3812 struct wpi_buf *bcn = &wvp->wv_bcbuf; 3813 struct ieee80211_beacon_offsets bo; 3814 struct wpi_cmd_beacon *cmd; 3815 struct mbuf *m; 3816 int totlen; 3817 3818 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 3819 3820 if (ni->ni_chan == IEEE80211_CHAN_ANYC) 3821 return EINVAL; 3822 3823 m = ieee80211_beacon_alloc(ni, &bo); 3824 if (m == NULL) { 3825 device_printf(sc->sc_dev, 3826 "%s: could not allocate beacon frame\n", __func__); 3827 return ENOMEM; 3828 } 3829 totlen = m->m_pkthdr.len; 3830 3831 if (bcn->data == NULL) { 3832 cmd = malloc(sizeof(struct wpi_cmd_beacon), M_DEVBUF, 3833 M_NOWAIT | M_ZERO); 3834 3835 if (cmd == NULL) { 3836 device_printf(sc->sc_dev, 3837 "could not allocate buffer for beacon command\n"); 3838 m_freem(m); 3839 return ENOMEM; 3840 } 3841 3842 cmd->id = WPI_ID_BROADCAST; 3843 cmd->ofdm_mask = 0xff; 3844 cmd->cck_mask = 0x0f; 3845 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE); 3846 cmd->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP); 3847 3848 bcn->data = cmd; 3849 bcn->ni = NULL; 3850 bcn->code = WPI_CMD_SET_BEACON; 3851 bcn->ac = 4; 3852 bcn->size = sizeof(struct wpi_cmd_beacon); 3853 } else 3854 cmd = bcn->data; 3855 3856 cmd->len = htole16(totlen); 3857 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 3858 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1]; 3859 3860 /* NB: m will be freed in wpi_cmd_done() */ 3861 bcn->m = m; 3862 3863 return wpi_cmd2(sc, bcn); 3864} 3865 3866static void 3867wpi_update_beacon(struct ieee80211vap *vap, int item) 3868{ 3869 struct wpi_softc *sc = vap->iv_ic->ic_ifp->if_softc; 3870 struct ieee80211_node *ni = vap->iv_bss; 3871 int error; 3872 3873 WPI_LOCK(sc); 3874 if ((error = wpi_setup_beacon(sc, ni)) != 0) { 3875 device_printf(sc->sc_dev, 3876 "%s: could not update beacon frame, error %d", __func__, 3877 error); 3878 } 3879 WPI_UNLOCK(sc); 3880} 3881 3882static int 3883wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap) 3884{ 3885 struct ieee80211com *ic = vap->iv_ic; 3886 struct ieee80211_node *ni = vap->iv_bss; 3887 int error; 3888 3889 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 3890 3891 if (vap->iv_opmode == IEEE80211_M_MONITOR) { 3892 /* Link LED blinks while monitoring. */ 3893 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 3894 return 0; 3895 } 3896 3897 /* XXX kernel panic workaround */ 3898 if (ni->ni_chan == IEEE80211_CHAN_ANYC) { 3899 device_printf(sc->sc_dev, "%s: incomplete configuration\n", 3900 __func__); 3901 return EINVAL; 3902 } 3903 3904 if ((error = wpi_set_timing(sc, ni)) != 0) { 3905 device_printf(sc->sc_dev, 3906 "%s: could not set timing, error %d\n", __func__, error); 3907 return error; 3908 } 3909 3910 /* Update adapter configuration. */ 3911 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 3912 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni)); 3913 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 3914 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF); 3915 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 3916 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 3917 /* Short preamble and slot time are negotiated when associating. */ 3918 sc->rxon.flags &= ~htole32(WPI_RXON_SHPREAMBLE | WPI_RXON_SHSLOT); 3919 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3920 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 3921 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3922 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 3923 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) { 3924 sc->rxon.cck_mask = 0; 3925 sc->rxon.ofdm_mask = 0x15; 3926 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) { 3927 sc->rxon.cck_mask = 0x03; 3928 sc->rxon.ofdm_mask = 0; 3929 } else { 3930 /* Assume 802.11b/g. */ 3931 sc->rxon.cck_mask = 0x0f; 3932 sc->rxon.ofdm_mask = 0x15; 3933 } 3934 sc->rxon.filter |= htole32(WPI_FILTER_BSS); 3935 3936 /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */ 3937 3938 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n", 3939 sc->rxon.chan, sc->rxon.flags); 3940 3941 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) { 3942 device_printf(sc->sc_dev, "%s: could not send RXON\n", 3943 __func__); 3944 return error; 3945 } 3946 3947 if (vap->iv_opmode == IEEE80211_M_IBSS) { 3948 if ((error = wpi_setup_beacon(sc, ni)) != 0) { 3949 device_printf(sc->sc_dev, 3950 "%s: could not setup beacon, error %d\n", __func__, 3951 error); 3952 return error; 3953 } 3954 } 3955 3956 if (vap->iv_opmode == IEEE80211_M_STA) { 3957 /* Add BSS node. */ 3958 ((struct wpi_node *)ni)->id = WPI_ID_BSS; 3959 if ((error = wpi_add_node(sc, ni)) != 0) { 3960 device_printf(sc->sc_dev, 3961 "%s: could not add BSS node, error %d\n", __func__, 3962 error); 3963 return error; 3964 } 3965 } 3966 3967 /* Link LED always on while associated. */ 3968 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 3969 3970 /* Start periodic calibration timer. */ 3971 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc); 3972 3973 /* Enable power-saving mode if requested by user. */ 3974 if (vap->iv_flags & IEEE80211_F_PMGTON) 3975 (void)wpi_set_pslevel(sc, 0, 3, 1); 3976 else 3977 (void)wpi_set_pslevel(sc, 0, 0, 1); 3978 3979 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 3980 3981 return 0; 3982} 3983 3984static int 3985wpi_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 3986 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 3987{ 3988 struct wpi_softc *sc = vap->iv_ic->ic_ifp->if_softc; 3989 3990 if (!(&vap->iv_nw_keys[0] <= k && 3991 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 3992 if (k->wk_flags & IEEE80211_KEY_GROUP) { 3993 /* should not happen */ 3994 DPRINTF(sc, WPI_DEBUG_KEY, "%s: bogus group key\n", 3995 __func__); 3996 return 0; 3997 } 3998 *keyix = 0; /* NB: use key index 0 for ucast key */ 3999 } else { 4000 *keyix = *rxkeyix = k - vap->iv_nw_keys; 4001 4002 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM) 4003 k->wk_flags |= IEEE80211_KEY_SWCRYPT; 4004 } 4005 return 1; 4006} 4007 4008static int 4009wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k, 4010 const uint8_t mac[IEEE80211_ADDR_LEN]) 4011{ 4012 const struct ieee80211_cipher *cip = k->wk_cipher; 4013 struct ieee80211com *ic = vap->iv_ic; 4014 struct ieee80211_node *ni = vap->iv_bss; 4015 struct wpi_softc *sc = ic->ic_ifp->if_softc; 4016 struct wpi_node *wn = WPI_NODE(ni); 4017 struct wpi_node_info node; 4018 uint16_t kflags; 4019 int error; 4020 4021 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4022 4023 switch (cip->ic_cipher) { 4024 case IEEE80211_CIPHER_AES_CCM: 4025 if (k->wk_flags & IEEE80211_KEY_GROUP) 4026 return 1; 4027 4028 kflags = WPI_KFLAG_CCMP; 4029 break; 4030 default: 4031 /* null_key_set() */ 4032 return 1; 4033 } 4034 4035 if (wn->id == WPI_ID_UNDEFINED) 4036 return 0; 4037 4038 kflags |= WPI_KFLAG_KID(k->wk_keyix); 4039 if (k->wk_flags & IEEE80211_KEY_GROUP) 4040 kflags |= WPI_KFLAG_MULTICAST; 4041 4042 memset(&node, 0, sizeof node); 4043 node.id = wn->id; 4044 node.control = WPI_NODE_UPDATE; 4045 node.flags = WPI_FLAG_KEY_SET; 4046 node.kflags = htole16(kflags); 4047 memcpy(node.key, k->wk_key, k->wk_keylen); 4048 4049 DPRINTF(sc, WPI_DEBUG_KEY, "set key id=%d for node %d\n", k->wk_keyix, 4050 node.id); 4051 4052 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 4053 if (error != 0) { 4054 device_printf(sc->sc_dev, "can't update node info, error %d\n", 4055 error); 4056 return 0; 4057 } 4058 4059 return 1; 4060} 4061 4062static int 4063wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 4064{ 4065 const struct ieee80211_cipher *cip = k->wk_cipher; 4066 struct ieee80211com *ic = vap->iv_ic; 4067 struct ieee80211_node *ni = vap->iv_bss; 4068 struct wpi_softc *sc = ic->ic_ifp->if_softc; 4069 struct wpi_node *wn = WPI_NODE(ni); 4070 struct wpi_node_info node; 4071 4072 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4073 4074 switch (cip->ic_cipher) { 4075 case IEEE80211_CIPHER_AES_CCM: 4076 break; 4077 default: 4078 /* null_key_delete() */ 4079 return 1; 4080 } 4081 4082 if (vap->iv_state != IEEE80211_S_RUN || 4083 (k->wk_flags & IEEE80211_KEY_GROUP)) 4084 return 1; /* Nothing to do. */ 4085 4086 memset(&node, 0, sizeof node); 4087 node.id = wn->id; 4088 node.control = WPI_NODE_UPDATE; 4089 node.flags = WPI_FLAG_KEY_SET; 4090 4091 DPRINTF(sc, WPI_DEBUG_KEY, "delete keys for node %d\n", node.id); 4092 (void)wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 4093 4094 return 1; 4095} 4096 4097/* 4098 * This function is called after the runtime firmware notifies us of its 4099 * readiness (called in a process context). 4100 */ 4101static int 4102wpi_post_alive(struct wpi_softc *sc) 4103{ 4104 int ntries, error; 4105 4106 /* Check (again) that the radio is not disabled. */ 4107 if ((error = wpi_nic_lock(sc)) != 0) 4108 return error; 4109 4110 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4111 4112 /* NB: Runtime firmware must be up and running. */ 4113 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) { 4114 device_printf(sc->sc_dev, 4115 "RF switch: radio disabled (%s)\n", __func__); 4116 wpi_nic_unlock(sc); 4117 return EPERM; /* :-) */ 4118 } 4119 wpi_nic_unlock(sc); 4120 4121 /* Wait for thermal sensor to calibrate. */ 4122 for (ntries = 0; ntries < 1000; ntries++) { 4123 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0) 4124 break; 4125 DELAY(10); 4126 } 4127 4128 if (ntries == 1000) { 4129 device_printf(sc->sc_dev, 4130 "timeout waiting for thermal sensor calibration\n"); 4131 return ETIMEDOUT; 4132 } 4133 4134 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp); 4135 return 0; 4136} 4137 4138/* 4139 * The firmware boot code is small and is intended to be copied directly into 4140 * the NIC internal memory (no DMA transfer). 4141 */ 4142static int 4143wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size) 4144{ 4145 int error, ntries; 4146 4147 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size); 4148 4149 size /= sizeof (uint32_t); 4150 4151 if ((error = wpi_nic_lock(sc)) != 0) 4152 return error; 4153 4154 /* Copy microcode image into NIC memory. */ 4155 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE, 4156 (const uint32_t *)ucode, size); 4157 4158 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0); 4159 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE); 4160 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size); 4161 4162 /* Start boot load now. */ 4163 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START); 4164 4165 /* Wait for transfer to complete. */ 4166 for (ntries = 0; ntries < 1000; ntries++) { 4167 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS); 4168 DPRINTF(sc, WPI_DEBUG_HW, 4169 "firmware status=0x%x, val=0x%x, result=0x%x\n", status, 4170 WPI_FH_TX_STATUS_IDLE(6), 4171 status & WPI_FH_TX_STATUS_IDLE(6)); 4172 if (status & WPI_FH_TX_STATUS_IDLE(6)) { 4173 DPRINTF(sc, WPI_DEBUG_HW, 4174 "Status Match! - ntries = %d\n", ntries); 4175 break; 4176 } 4177 DELAY(10); 4178 } 4179 if (ntries == 1000) { 4180 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 4181 __func__); 4182 wpi_nic_unlock(sc); 4183 return ETIMEDOUT; 4184 } 4185 4186 /* Enable boot after power up. */ 4187 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN); 4188 4189 wpi_nic_unlock(sc); 4190 return 0; 4191} 4192 4193static int 4194wpi_load_firmware(struct wpi_softc *sc) 4195{ 4196 struct wpi_fw_info *fw = &sc->fw; 4197 struct wpi_dma_info *dma = &sc->fw_dma; 4198 int error; 4199 4200 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4201 4202 /* Copy initialization sections into pre-allocated DMA-safe memory. */ 4203 memcpy(dma->vaddr, fw->init.data, fw->init.datasz); 4204 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4205 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz); 4206 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4207 4208 /* Tell adapter where to find initialization sections. */ 4209 if ((error = wpi_nic_lock(sc)) != 0) 4210 return error; 4211 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 4212 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz); 4213 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 4214 dma->paddr + WPI_FW_DATA_MAXSZ); 4215 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz); 4216 wpi_nic_unlock(sc); 4217 4218 /* Load firmware boot code. */ 4219 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz); 4220 if (error != 0) { 4221 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 4222 __func__); 4223 return error; 4224 } 4225 4226 /* Now press "execute". */ 4227 WPI_WRITE(sc, WPI_RESET, 0); 4228 4229 /* Wait at most one second for first alive notification. */ 4230 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 4231 device_printf(sc->sc_dev, 4232 "%s: timeout waiting for adapter to initialize, error %d\n", 4233 __func__, error); 4234 return error; 4235 } 4236 4237 /* Copy runtime sections into pre-allocated DMA-safe memory. */ 4238 memcpy(dma->vaddr, fw->main.data, fw->main.datasz); 4239 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4240 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz); 4241 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 4242 4243 /* Tell adapter where to find runtime sections. */ 4244 if ((error = wpi_nic_lock(sc)) != 0) 4245 return error; 4246 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 4247 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz); 4248 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 4249 dma->paddr + WPI_FW_DATA_MAXSZ); 4250 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, 4251 WPI_FW_UPDATED | fw->main.textsz); 4252 wpi_nic_unlock(sc); 4253 4254 return 0; 4255} 4256 4257static int 4258wpi_read_firmware(struct wpi_softc *sc) 4259{ 4260 const struct firmware *fp; 4261 struct wpi_fw_info *fw = &sc->fw; 4262 const struct wpi_firmware_hdr *hdr; 4263 int error; 4264 4265 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4266 4267 DPRINTF(sc, WPI_DEBUG_FIRMWARE, 4268 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME); 4269 4270 WPI_UNLOCK(sc); 4271 fp = firmware_get(WPI_FW_NAME); 4272 WPI_LOCK(sc); 4273 4274 if (fp == NULL) { 4275 device_printf(sc->sc_dev, 4276 "could not load firmware image '%s'\n", WPI_FW_NAME); 4277 return EINVAL; 4278 } 4279 4280 sc->fw_fp = fp; 4281 4282 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) { 4283 device_printf(sc->sc_dev, 4284 "firmware file too short: %zu bytes\n", fp->datasize); 4285 error = EINVAL; 4286 goto fail; 4287 } 4288 4289 fw->size = fp->datasize; 4290 fw->data = (const uint8_t *)fp->data; 4291 4292 /* Extract firmware header information. */ 4293 hdr = (const struct wpi_firmware_hdr *)fw->data; 4294 4295 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW | 4296 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */ 4297 4298 fw->main.textsz = le32toh(hdr->rtextsz); 4299 fw->main.datasz = le32toh(hdr->rdatasz); 4300 fw->init.textsz = le32toh(hdr->itextsz); 4301 fw->init.datasz = le32toh(hdr->idatasz); 4302 fw->boot.textsz = le32toh(hdr->btextsz); 4303 fw->boot.datasz = 0; 4304 4305 /* Sanity-check firmware header. */ 4306 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ || 4307 fw->main.datasz > WPI_FW_DATA_MAXSZ || 4308 fw->init.textsz > WPI_FW_TEXT_MAXSZ || 4309 fw->init.datasz > WPI_FW_DATA_MAXSZ || 4310 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ || 4311 (fw->boot.textsz & 3) != 0) { 4312 device_printf(sc->sc_dev, "invalid firmware header\n"); 4313 error = EINVAL; 4314 goto fail; 4315 } 4316 4317 /* Check that all firmware sections fit. */ 4318 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz + 4319 fw->init.textsz + fw->init.datasz + fw->boot.textsz) { 4320 device_printf(sc->sc_dev, 4321 "firmware file too short: %zu bytes\n", fw->size); 4322 error = EINVAL; 4323 goto fail; 4324 } 4325 4326 /* Get pointers to firmware sections. */ 4327 fw->main.text = (const uint8_t *)(hdr + 1); 4328 fw->main.data = fw->main.text + fw->main.textsz; 4329 fw->init.text = fw->main.data + fw->main.datasz; 4330 fw->init.data = fw->init.text + fw->init.textsz; 4331 fw->boot.text = fw->init.data + fw->init.datasz; 4332 4333 DPRINTF(sc, WPI_DEBUG_FIRMWARE, 4334 "Firmware Version: Major %d, Minor %d, Driver %d, \n" 4335 "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n", 4336 hdr->major, hdr->minor, le32toh(hdr->driver), 4337 fw->main.textsz, fw->main.datasz, 4338 fw->init.textsz, fw->init.datasz, fw->boot.textsz); 4339 4340 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text); 4341 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data); 4342 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text); 4343 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data); 4344 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text); 4345 4346 return 0; 4347 4348fail: wpi_unload_firmware(sc); 4349 return error; 4350} 4351 4352/** 4353 * Free the referenced firmware image 4354 */ 4355static void 4356wpi_unload_firmware(struct wpi_softc *sc) 4357{ 4358 if (sc->fw_fp != NULL) { 4359 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 4360 sc->fw_fp = NULL; 4361 } 4362} 4363 4364static int 4365wpi_clock_wait(struct wpi_softc *sc) 4366{ 4367 int ntries; 4368 4369 /* Set "initialization complete" bit. */ 4370 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE); 4371 4372 /* Wait for clock stabilization. */ 4373 for (ntries = 0; ntries < 2500; ntries++) { 4374 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY) 4375 return 0; 4376 DELAY(100); 4377 } 4378 device_printf(sc->sc_dev, 4379 "%s: timeout waiting for clock stabilization\n", __func__); 4380 4381 return ETIMEDOUT; 4382} 4383 4384static int 4385wpi_apm_init(struct wpi_softc *sc) 4386{ 4387 uint32_t reg; 4388 int error; 4389 4390 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4391 4392 /* Disable L0s exit timer (NMI bug workaround). */ 4393 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER); 4394 /* Don't wait for ICH L0s (ICH bug workaround). */ 4395 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX); 4396 4397 /* Set FH wait threshold to max (HW bug under stress workaround). */ 4398 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000); 4399 4400 /* Retrieve PCIe Active State Power Management (ASPM). */ 4401 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1); 4402 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */ 4403 if (reg & 0x02) /* L1 Entry enabled. */ 4404 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA); 4405 else 4406 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA); 4407 4408 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT); 4409 4410 /* Wait for clock stabilization before accessing prph. */ 4411 if ((error = wpi_clock_wait(sc)) != 0) 4412 return error; 4413 4414 if ((error = wpi_nic_lock(sc)) != 0) 4415 return error; 4416 /* Enable DMA and BSM (Bootstrap State Machine). */ 4417 wpi_prph_write(sc, WPI_APMG_CLK_EN, 4418 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT); 4419 DELAY(20); 4420 /* Disable L1-Active. */ 4421 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS); 4422 /* ??? */ 4423 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000E00); 4424 wpi_nic_unlock(sc); 4425 4426 return 0; 4427} 4428 4429static void 4430wpi_apm_stop_master(struct wpi_softc *sc) 4431{ 4432 int ntries; 4433 4434 /* Stop busmaster DMA activity. */ 4435 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER); 4436 4437 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) == 4438 WPI_GP_CNTRL_MAC_PS) 4439 return; /* Already asleep. */ 4440 4441 for (ntries = 0; ntries < 100; ntries++) { 4442 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED) 4443 return; 4444 DELAY(10); 4445 } 4446 device_printf(sc->sc_dev, "%s: timeout waiting for master\n", __func__); 4447} 4448 4449static void 4450wpi_apm_stop(struct wpi_softc *sc) 4451{ 4452 wpi_apm_stop_master(sc); 4453 4454 /* Reset the entire device. */ 4455 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW); 4456 DELAY(10); 4457 /* Clear "initialization complete" bit. */ 4458 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE); 4459} 4460 4461static void 4462wpi_nic_config(struct wpi_softc *sc) 4463{ 4464 uint32_t rev; 4465 4466 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4467 4468 /* voodoo from the Linux "driver".. */ 4469 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1); 4470 if ((rev & 0xc0) == 0x40) 4471 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB); 4472 else if (!(rev & 0x80)) 4473 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM); 4474 4475 if (sc->cap == 0x80) 4476 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC); 4477 4478 if ((le16toh(sc->rev) & 0xf0) == 0xd0) 4479 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 4480 else 4481 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 4482 4483 if (sc->type > 1) 4484 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B); 4485} 4486 4487static int 4488wpi_hw_init(struct wpi_softc *sc) 4489{ 4490 int chnl, ntries, error; 4491 4492 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4493 4494 /* Clear pending interrupts. */ 4495 WPI_WRITE(sc, WPI_INT, 0xffffffff); 4496 4497 if ((error = wpi_apm_init(sc)) != 0) { 4498 device_printf(sc->sc_dev, 4499 "%s: could not power ON adapter, error %d\n", __func__, 4500 error); 4501 return error; 4502 } 4503 4504 /* Select VMAIN power source. */ 4505 if ((error = wpi_nic_lock(sc)) != 0) 4506 return error; 4507 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK); 4508 wpi_nic_unlock(sc); 4509 /* Spin until VMAIN gets selected. */ 4510 for (ntries = 0; ntries < 5000; ntries++) { 4511 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN) 4512 break; 4513 DELAY(10); 4514 } 4515 if (ntries == 5000) { 4516 device_printf(sc->sc_dev, "timeout selecting power source\n"); 4517 return ETIMEDOUT; 4518 } 4519 4520 /* Perform adapter initialization. */ 4521 wpi_nic_config(sc); 4522 4523 /* Initialize RX ring. */ 4524 if ((error = wpi_nic_lock(sc)) != 0) 4525 return error; 4526 /* Set physical address of RX ring. */ 4527 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr); 4528 /* Set physical address of RX read pointer. */ 4529 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr + 4530 offsetof(struct wpi_shared, next)); 4531 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0); 4532 /* Enable RX. */ 4533 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 4534 WPI_FH_RX_CONFIG_DMA_ENA | 4535 WPI_FH_RX_CONFIG_RDRBD_ENA | 4536 WPI_FH_RX_CONFIG_WRSTATUS_ENA | 4537 WPI_FH_RX_CONFIG_MAXFRAG | 4538 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) | 4539 WPI_FH_RX_CONFIG_IRQ_DST_HOST | 4540 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1)); 4541 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */ 4542 wpi_nic_unlock(sc); 4543 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7); 4544 4545 /* Initialize TX rings. */ 4546 if ((error = wpi_nic_lock(sc)) != 0) 4547 return error; 4548 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */ 4549 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */ 4550 /* Enable all 6 TX rings. */ 4551 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f); 4552 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000); 4553 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002); 4554 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4); 4555 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5); 4556 /* Set physical address of TX rings. */ 4557 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr); 4558 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5); 4559 4560 /* Enable all DMA channels. */ 4561 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 4562 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0); 4563 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0); 4564 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008); 4565 } 4566 wpi_nic_unlock(sc); 4567 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */ 4568 4569 /* Clear "radio off" and "commands blocked" bits. */ 4570 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 4571 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED); 4572 4573 /* Clear pending interrupts. */ 4574 WPI_WRITE(sc, WPI_INT, 0xffffffff); 4575 /* Enable interrupts. */ 4576 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF); 4577 4578 /* _Really_ make sure "radio off" bit is cleared! */ 4579 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 4580 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 4581 4582 if ((error = wpi_load_firmware(sc)) != 0) { 4583 device_printf(sc->sc_dev, 4584 "%s: could not load firmware, error %d\n", __func__, 4585 error); 4586 return error; 4587 } 4588 /* Wait at most one second for firmware alive notification. */ 4589 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 4590 device_printf(sc->sc_dev, 4591 "%s: timeout waiting for adapter to initialize, error %d\n", 4592 __func__, error); 4593 return error; 4594 } 4595 4596 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4597 4598 /* Do post-firmware initialization. */ 4599 return wpi_post_alive(sc); 4600} 4601 4602static void 4603wpi_hw_stop(struct wpi_softc *sc) 4604{ 4605 int chnl, qid, ntries; 4606 4607 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4608 4609 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) 4610 wpi_nic_lock(sc); 4611 4612 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO); 4613 4614 /* Disable interrupts. */ 4615 WPI_WRITE(sc, WPI_INT_MASK, 0); 4616 WPI_WRITE(sc, WPI_INT, 0xffffffff); 4617 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff); 4618 4619 /* Make sure we no longer hold the NIC lock. */ 4620 wpi_nic_unlock(sc); 4621 4622 if (wpi_nic_lock(sc) == 0) { 4623 /* Stop TX scheduler. */ 4624 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0); 4625 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0); 4626 4627 /* Stop all DMA channels. */ 4628 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 4629 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0); 4630 for (ntries = 0; ntries < 200; ntries++) { 4631 if (WPI_READ(sc, WPI_FH_TX_STATUS) & 4632 WPI_FH_TX_STATUS_IDLE(chnl)) 4633 break; 4634 DELAY(10); 4635 } 4636 } 4637 wpi_nic_unlock(sc); 4638 } 4639 4640 /* Stop RX ring. */ 4641 wpi_reset_rx_ring(sc); 4642 4643 /* Reset all TX rings. */ 4644 for (qid = 0; qid < WPI_NTXQUEUES; qid++) 4645 wpi_reset_tx_ring(sc, &sc->txq[qid]); 4646 4647 if (wpi_nic_lock(sc) == 0) { 4648 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 4649 WPI_APMG_CLK_CTRL_DMA_CLK_RQT); 4650 wpi_nic_unlock(sc); 4651 } 4652 DELAY(5); 4653 /* Power OFF adapter. */ 4654 wpi_apm_stop(sc); 4655} 4656 4657static void 4658wpi_radio_on(void *arg0, int pending) 4659{ 4660 struct wpi_softc *sc = arg0; 4661 struct ifnet *ifp = sc->sc_ifp; 4662 struct ieee80211com *ic = ifp->if_l2com; 4663 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4664 4665 device_printf(sc->sc_dev, "RF switch: radio enabled\n"); 4666 4667 if (vap != NULL) { 4668 wpi_init(sc); 4669 ieee80211_init(vap); 4670 } 4671 4672 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL) { 4673 WPI_LOCK(sc); 4674 callout_stop(&sc->watchdog_rfkill); 4675 WPI_UNLOCK(sc); 4676 } 4677} 4678 4679static void 4680wpi_radio_off(void *arg0, int pending) 4681{ 4682 struct wpi_softc *sc = arg0; 4683 struct ifnet *ifp = sc->sc_ifp; 4684 struct ieee80211com *ic = ifp->if_l2com; 4685 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4686 4687 device_printf(sc->sc_dev, "RF switch: radio disabled\n"); 4688 4689 wpi_stop(sc); 4690 if (vap != NULL) 4691 ieee80211_stop(vap); 4692 4693 WPI_LOCK(sc); 4694 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc); 4695 WPI_UNLOCK(sc); 4696} 4697 4698static void 4699wpi_init_locked(struct wpi_softc *sc) 4700{ 4701 struct ifnet *ifp = sc->sc_ifp; 4702 int error; 4703 4704 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__); 4705 4706 WPI_LOCK_ASSERT(sc); 4707 4708 /* Check that the radio is not disabled by hardware switch. */ 4709 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) { 4710 device_printf(sc->sc_dev, 4711 "RF switch: radio disabled (%s)\n", __func__); 4712 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, 4713 sc); 4714 return; 4715 } 4716 4717 /* Read firmware images from the filesystem. */ 4718 if ((error = wpi_read_firmware(sc)) != 0) { 4719 device_printf(sc->sc_dev, 4720 "%s: could not read firmware, error %d\n", __func__, 4721 error); 4722 goto fail; 4723 } 4724 4725 /* Initialize hardware and upload firmware. */ 4726 error = wpi_hw_init(sc); 4727 wpi_unload_firmware(sc); 4728 if (error != 0) { 4729 device_printf(sc->sc_dev, 4730 "%s: could not initialize hardware, error %d\n", __func__, 4731 error); 4732 goto fail; 4733 } 4734 4735 /* Configure adapter now that it is ready. */ 4736 if ((error = wpi_config(sc)) != 0) { 4737 device_printf(sc->sc_dev, 4738 "%s: could not configure device, error %d\n", __func__, 4739 error); 4740 goto fail; 4741 } 4742 4743 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4744 ifp->if_drv_flags |= IFF_DRV_RUNNING; 4745 4746 callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc); 4747 4748 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__); 4749 4750 return; 4751 4752fail: wpi_stop_locked(sc); 4753 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__); 4754} 4755 4756static void 4757wpi_init(void *arg) 4758{ 4759 struct wpi_softc *sc = arg; 4760 struct ifnet *ifp = sc->sc_ifp; 4761 struct ieee80211com *ic = ifp->if_l2com; 4762 4763 WPI_LOCK(sc); 4764 wpi_init_locked(sc); 4765 WPI_UNLOCK(sc); 4766 4767 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4768 ieee80211_start_all(ic); 4769} 4770 4771static void 4772wpi_stop_locked(struct wpi_softc *sc) 4773{ 4774 struct ifnet *ifp = sc->sc_ifp; 4775 4776 WPI_LOCK_ASSERT(sc); 4777 4778 sc->sc_scan_timer = 0; 4779 sc->sc_tx_timer = 0; 4780 callout_stop(&sc->watchdog_to); 4781 callout_stop(&sc->calib_to); 4782 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 4783 4784 /* Power OFF hardware. */ 4785 wpi_hw_stop(sc); 4786} 4787 4788static void 4789wpi_stop(struct wpi_softc *sc) 4790{ 4791 WPI_LOCK(sc); 4792 wpi_stop_locked(sc); 4793 WPI_UNLOCK(sc); 4794} 4795 4796/* 4797 * Callback from net80211 to start a scan. 4798 */ 4799static void 4800wpi_scan_start(struct ieee80211com *ic) 4801{ 4802 struct ifnet *ifp = ic->ic_ifp; 4803 struct wpi_softc *sc = ifp->if_softc; 4804 4805 WPI_LOCK(sc); 4806 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 4807 WPI_UNLOCK(sc); 4808} 4809 4810/* 4811 * Callback from net80211 to terminate a scan. 4812 */ 4813static void 4814wpi_scan_end(struct ieee80211com *ic) 4815{ 4816 struct ifnet *ifp = ic->ic_ifp; 4817 struct wpi_softc *sc = ifp->if_softc; 4818 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4819 4820 if (vap->iv_state == IEEE80211_S_RUN) { 4821 WPI_LOCK(sc); 4822 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 4823 WPI_UNLOCK(sc); 4824 } 4825} 4826 4827/** 4828 * Called by the net80211 framework to indicate to the driver 4829 * that the channel should be changed 4830 */ 4831static void 4832wpi_set_channel(struct ieee80211com *ic) 4833{ 4834 const struct ieee80211_channel *c = ic->ic_curchan; 4835 struct ifnet *ifp = ic->ic_ifp; 4836 struct wpi_softc *sc = ifp->if_softc; 4837 int error; 4838 4839 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4840 4841 WPI_LOCK(sc); 4842 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq); 4843 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags); 4844 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq); 4845 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags); 4846 4847 /* 4848 * Only need to set the channel in Monitor mode. AP scanning and auth 4849 * are already taken care of by their respective firmware commands. 4850 */ 4851 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 4852 sc->rxon.chan = ieee80211_chan2ieee(ic, c); 4853 if (IEEE80211_IS_CHAN_2GHZ(c)) { 4854 sc->rxon.flags |= htole32(WPI_RXON_AUTO | 4855 WPI_RXON_24GHZ); 4856 } else { 4857 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO | 4858 WPI_RXON_24GHZ); 4859 } 4860 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) 4861 device_printf(sc->sc_dev, 4862 "%s: error %d settting channel\n", __func__, 4863 error); 4864 } 4865 WPI_UNLOCK(sc); 4866} 4867 4868/** 4869 * Called by net80211 to indicate that we need to scan the current 4870 * channel. The channel is previously be set via the wpi_set_channel 4871 * callback. 4872 */ 4873static void 4874wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 4875{ 4876 struct ieee80211vap *vap = ss->ss_vap; 4877 struct ieee80211com *ic = vap->iv_ic; 4878 struct ifnet *ifp = ic->ic_ifp; 4879 struct wpi_softc *sc = ifp->if_softc; 4880 int error; 4881 4882 if (sc->rxon.chan != ieee80211_chan2ieee(ic, ic->ic_curchan)) { 4883 WPI_LOCK(sc); 4884 error = wpi_scan(sc, ic->ic_curchan); 4885 WPI_UNLOCK(sc); 4886 if (error != 0) 4887 ieee80211_cancel_scan(vap); 4888 } else { 4889 /* Send probe request when associated. */ 4890 sc->sc_scan_curchan(ss, maxdwell); 4891 } 4892} 4893 4894/** 4895 * Called by the net80211 framework to indicate 4896 * the minimum dwell time has been met, terminate the scan. 4897 * We don't actually terminate the scan as the firmware will notify 4898 * us when it's finished and we have no way to interrupt it. 4899 */ 4900static void 4901wpi_scan_mindwell(struct ieee80211_scan_state *ss) 4902{ 4903 /* NB: don't try to abort scan; wait for firmware to finish */ 4904} 4905 4906static void 4907wpi_hw_reset(void *arg, int pending) 4908{ 4909 struct wpi_softc *sc = arg; 4910 struct ifnet *ifp = sc->sc_ifp; 4911 struct ieee80211com *ic = ifp->if_l2com; 4912 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 4913 4914 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__); 4915 4916 wpi_stop(sc); 4917 if (vap != NULL) 4918 ieee80211_stop(vap); 4919 wpi_init(sc); 4920 if (vap != NULL) 4921 ieee80211_init(vap); 4922} 4923