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