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