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