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