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