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