1 /* $OpenBSD: if_wpi.c,v 1.149 2019/09/30 01:53:05 dlg Exp $ */ 2 3 /*- 4 * Copyright (c) 2006-2008 5 * Damien Bergamini <damien.bergamini@free.fr> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters. 22 */ 23 24 #include "bpfilter.h" 25 26 #include <sys/param.h> 27 #include <sys/sockio.h> 28 #include <sys/mbuf.h> 29 #include <sys/kernel.h> 30 #include <sys/rwlock.h> 31 #include <sys/socket.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/conf.h> 35 #include <sys/device.h> 36 #include <sys/task.h> 37 #include <sys/endian.h> 38 39 #include <machine/bus.h> 40 #include <machine/intr.h> 41 42 #include <dev/pci/pcireg.h> 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcidevs.h> 45 46 #if NBPFILTER > 0 47 #include <net/bpf.h> 48 #endif 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 53 #include <netinet/in.h> 54 #include <netinet/if_ether.h> 55 56 #include <net80211/ieee80211_var.h> 57 #include <net80211/ieee80211_amrr.h> 58 #include <net80211/ieee80211_radiotap.h> 59 60 #include <dev/pci/if_wpireg.h> 61 #include <dev/pci/if_wpivar.h> 62 63 static const struct pci_matchid wpi_devices[] = { 64 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 }, 65 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2 } 66 }; 67 68 int wpi_match(struct device *, void *, void *); 69 void wpi_attach(struct device *, struct device *, void *); 70 #if NBPFILTER > 0 71 void wpi_radiotap_attach(struct wpi_softc *); 72 #endif 73 int wpi_detach(struct device *, int); 74 int wpi_activate(struct device *, int); 75 void wpi_wakeup(struct wpi_softc *); 76 void wpi_init_task(void *); 77 int wpi_nic_lock(struct wpi_softc *); 78 int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int); 79 int wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *, 80 void **, bus_size_t, bus_size_t); 81 void wpi_dma_contig_free(struct wpi_dma_info *); 82 int wpi_alloc_shared(struct wpi_softc *); 83 void wpi_free_shared(struct wpi_softc *); 84 int wpi_alloc_fwmem(struct wpi_softc *); 85 void wpi_free_fwmem(struct wpi_softc *); 86 int wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 87 void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 88 void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 89 int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, 90 int); 91 void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 92 void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 93 int wpi_read_eeprom(struct wpi_softc *); 94 void wpi_read_eeprom_channels(struct wpi_softc *, int); 95 void wpi_read_eeprom_group(struct wpi_softc *, int); 96 struct ieee80211_node *wpi_node_alloc(struct ieee80211com *); 97 void wpi_newassoc(struct ieee80211com *, struct ieee80211_node *, 98 int); 99 int wpi_media_change(struct ifnet *); 100 int wpi_newstate(struct ieee80211com *, enum ieee80211_state, int); 101 void wpi_iter_func(void *, struct ieee80211_node *); 102 void wpi_calib_timeout(void *); 103 int wpi_ccmp_decap(struct wpi_softc *, struct mbuf *, 104 struct ieee80211_key *); 105 void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *, 106 struct wpi_rx_data *, struct mbuf_list *); 107 void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *); 108 void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *); 109 void wpi_notif_intr(struct wpi_softc *); 110 void wpi_fatal_intr(struct wpi_softc *); 111 int wpi_intr(void *); 112 int wpi_tx(struct wpi_softc *, struct mbuf *, 113 struct ieee80211_node *); 114 void wpi_start(struct ifnet *); 115 void wpi_watchdog(struct ifnet *); 116 int wpi_ioctl(struct ifnet *, u_long, caddr_t); 117 int wpi_cmd(struct wpi_softc *, int, const void *, int, int); 118 int wpi_mrr_setup(struct wpi_softc *); 119 void wpi_updateedca(struct ieee80211com *); 120 void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t); 121 int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *); 122 void wpi_power_calibration(struct wpi_softc *); 123 int wpi_set_txpower(struct wpi_softc *, int); 124 int wpi_get_power_index(struct wpi_softc *, 125 struct wpi_power_group *, struct ieee80211_channel *, int); 126 int wpi_set_pslevel(struct wpi_softc *, int, int, int); 127 int wpi_config(struct wpi_softc *); 128 int wpi_scan(struct wpi_softc *, uint16_t); 129 int wpi_auth(struct wpi_softc *); 130 int wpi_run(struct wpi_softc *); 131 int wpi_set_key(struct ieee80211com *, struct ieee80211_node *, 132 struct ieee80211_key *); 133 void wpi_delete_key(struct ieee80211com *, struct ieee80211_node *, 134 struct ieee80211_key *); 135 int wpi_post_alive(struct wpi_softc *); 136 int wpi_load_bootcode(struct wpi_softc *, const uint8_t *, int); 137 int wpi_load_firmware(struct wpi_softc *); 138 int wpi_read_firmware(struct wpi_softc *); 139 int wpi_clock_wait(struct wpi_softc *); 140 int wpi_apm_init(struct wpi_softc *); 141 void wpi_apm_stop_master(struct wpi_softc *); 142 void wpi_apm_stop(struct wpi_softc *); 143 void wpi_nic_config(struct wpi_softc *); 144 int wpi_hw_init(struct wpi_softc *); 145 void wpi_hw_stop(struct wpi_softc *); 146 int wpi_init(struct ifnet *); 147 void wpi_stop(struct ifnet *, int); 148 149 #ifdef WPI_DEBUG 150 #define DPRINTF(x) do { if (wpi_debug > 0) printf x; } while (0) 151 #define DPRINTFN(n, x) do { if (wpi_debug >= (n)) printf x; } while (0) 152 int wpi_debug = 0; 153 #else 154 #define DPRINTF(x) 155 #define DPRINTFN(n, x) 156 #endif 157 158 struct cfdriver wpi_cd = { 159 NULL, "wpi", DV_IFNET 160 }; 161 162 struct cfattach wpi_ca = { 163 sizeof (struct wpi_softc), wpi_match, wpi_attach, wpi_detach, 164 wpi_activate 165 }; 166 167 int 168 wpi_match(struct device *parent, void *match, void *aux) 169 { 170 return pci_matchbyid((struct pci_attach_args *)aux, wpi_devices, 171 nitems(wpi_devices)); 172 } 173 174 void 175 wpi_attach(struct device *parent, struct device *self, void *aux) 176 { 177 struct wpi_softc *sc = (struct wpi_softc *)self; 178 struct ieee80211com *ic = &sc->sc_ic; 179 struct ifnet *ifp = &ic->ic_if; 180 struct pci_attach_args *pa = aux; 181 const char *intrstr; 182 pci_intr_handle_t ih; 183 pcireg_t memtype, reg; 184 int i, error; 185 186 sc->sc_pct = pa->pa_pc; 187 sc->sc_pcitag = pa->pa_tag; 188 sc->sc_dmat = pa->pa_dmat; 189 190 /* 191 * Get the offset of the PCI Express Capability Structure in PCI 192 * Configuration Space (the vendor driver hard-codes it as E0h.) 193 */ 194 error = pci_get_capability(sc->sc_pct, sc->sc_pcitag, 195 PCI_CAP_PCIEXPRESS, &sc->sc_cap_off, NULL); 196 if (error == 0) { 197 printf(": PCIe capability structure not found!\n"); 198 return; 199 } 200 201 /* Clear device-specific "PCI retry timeout" register (41h). */ 202 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 203 reg &= ~0xff00; 204 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg); 205 206 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, WPI_PCI_BAR0); 207 error = pci_mapreg_map(pa, WPI_PCI_BAR0, memtype, 0, &sc->sc_st, 208 &sc->sc_sh, NULL, &sc->sc_sz, 0); 209 if (error != 0) { 210 printf(": can't map mem space\n"); 211 return; 212 } 213 214 /* Install interrupt handler. */ 215 if (pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) { 216 printf(": can't map interrupt\n"); 217 return; 218 } 219 intrstr = pci_intr_string(sc->sc_pct, ih); 220 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc, 221 sc->sc_dev.dv_xname); 222 if (sc->sc_ih == NULL) { 223 printf(": can't establish interrupt"); 224 if (intrstr != NULL) 225 printf(" at %s", intrstr); 226 printf("\n"); 227 return; 228 } 229 printf(": %s", intrstr); 230 231 /* Power ON adapter. */ 232 if ((error = wpi_apm_init(sc)) != 0) { 233 printf(": could not power ON adapter\n"); 234 return; 235 } 236 237 /* Read MAC address, channels, etc from EEPROM. */ 238 if ((error = wpi_read_eeprom(sc)) != 0) { 239 printf(": could not read EEPROM\n"); 240 return; 241 } 242 243 /* Allocate DMA memory for firmware transfers. */ 244 if ((error = wpi_alloc_fwmem(sc)) != 0) { 245 printf(": could not allocate memory for firmware\n"); 246 return; 247 } 248 249 /* Allocate shared area. */ 250 if ((error = wpi_alloc_shared(sc)) != 0) { 251 printf(": could not allocate shared area\n"); 252 goto fail1; 253 } 254 255 /* Allocate TX rings. */ 256 for (i = 0; i < WPI_NTXQUEUES; i++) { 257 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) { 258 printf(": could not allocate TX ring %d\n", i); 259 goto fail2; 260 } 261 } 262 263 /* Allocate RX ring. */ 264 if ((error = wpi_alloc_rx_ring(sc, &sc->rxq)) != 0) { 265 printf(": could not allocate Rx ring\n"); 266 goto fail2; 267 } 268 269 /* Power OFF adapter. */ 270 wpi_apm_stop(sc); 271 /* Clear pending interrupts. */ 272 WPI_WRITE(sc, WPI_INT, 0xffffffff); 273 274 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 275 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 276 ic->ic_state = IEEE80211_S_INIT; 277 278 /* Set device capabilities. */ 279 ic->ic_caps = 280 IEEE80211_C_WEP | /* WEP */ 281 IEEE80211_C_RSN | /* WPA/RSN */ 282 IEEE80211_C_SCANALL | /* device scans all channels at once */ 283 IEEE80211_C_SCANALLBAND | /* driver scans all bands at once */ 284 IEEE80211_C_MONITOR | /* monitor mode supported */ 285 IEEE80211_C_SHSLOT | /* short slot time supported */ 286 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 287 IEEE80211_C_PMGT; /* power saving supported */ 288 289 /* Set supported rates. */ 290 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b; 291 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g; 292 if (sc->sc_flags & WPI_FLAG_HAS_5GHZ) { 293 ic->ic_sup_rates[IEEE80211_MODE_11A] = 294 ieee80211_std_rateset_11a; 295 } 296 297 /* IBSS channel undefined for now. */ 298 ic->ic_ibss_chan = &ic->ic_channels[0]; 299 300 ifp->if_softc = sc; 301 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 302 ifp->if_ioctl = wpi_ioctl; 303 ifp->if_start = wpi_start; 304 ifp->if_watchdog = wpi_watchdog; 305 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 306 307 if_attach(ifp); 308 ieee80211_ifattach(ifp); 309 ic->ic_node_alloc = wpi_node_alloc; 310 ic->ic_newassoc = wpi_newassoc; 311 ic->ic_updateedca = wpi_updateedca; 312 ic->ic_set_key = wpi_set_key; 313 ic->ic_delete_key = wpi_delete_key; 314 315 /* Override 802.11 state transition machine. */ 316 sc->sc_newstate = ic->ic_newstate; 317 ic->ic_newstate = wpi_newstate; 318 ieee80211_media_init(ifp, wpi_media_change, ieee80211_media_status); 319 320 sc->amrr.amrr_min_success_threshold = 1; 321 sc->amrr.amrr_max_success_threshold = 15; 322 323 #if NBPFILTER > 0 324 wpi_radiotap_attach(sc); 325 #endif 326 timeout_set(&sc->calib_to, wpi_calib_timeout, sc); 327 rw_init(&sc->sc_rwlock, "wpilock"); 328 task_set(&sc->init_task, wpi_init_task, sc); 329 return; 330 331 /* Free allocated memory if something failed during attachment. */ 332 fail2: while (--i >= 0) 333 wpi_free_tx_ring(sc, &sc->txq[i]); 334 wpi_free_shared(sc); 335 fail1: wpi_free_fwmem(sc); 336 } 337 338 #if NBPFILTER > 0 339 /* 340 * Attach the interface to 802.11 radiotap. 341 */ 342 void 343 wpi_radiotap_attach(struct wpi_softc *sc) 344 { 345 bpfattach(&sc->sc_drvbpf, &sc->sc_ic.ic_if, DLT_IEEE802_11_RADIO, 346 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN); 347 348 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 349 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 350 sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT); 351 352 sc->sc_txtap_len = sizeof sc->sc_txtapu; 353 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 354 sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT); 355 } 356 #endif 357 358 int 359 wpi_detach(struct device *self, int flags) 360 { 361 struct wpi_softc *sc = (struct wpi_softc *)self; 362 struct ifnet *ifp = &sc->sc_ic.ic_if; 363 int qid; 364 365 timeout_del(&sc->calib_to); 366 task_del(systq, &sc->init_task); 367 368 /* Uninstall interrupt handler. */ 369 if (sc->sc_ih != NULL) 370 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 371 372 /* Free DMA resources. */ 373 wpi_free_rx_ring(sc, &sc->rxq); 374 for (qid = 0; qid < WPI_NTXQUEUES; qid++) 375 wpi_free_tx_ring(sc, &sc->txq[qid]); 376 wpi_free_shared(sc); 377 wpi_free_fwmem(sc); 378 379 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz); 380 381 ieee80211_ifdetach(ifp); 382 if_detach(ifp); 383 384 return 0; 385 } 386 387 int 388 wpi_activate(struct device *self, int act) 389 { 390 struct wpi_softc *sc = (struct wpi_softc *)self; 391 struct ifnet *ifp = &sc->sc_ic.ic_if; 392 393 switch (act) { 394 case DVACT_SUSPEND: 395 if (ifp->if_flags & IFF_RUNNING) 396 wpi_stop(ifp, 0); 397 break; 398 case DVACT_WAKEUP: 399 wpi_wakeup(sc); 400 break; 401 } 402 403 return 0; 404 } 405 406 void 407 wpi_wakeup(struct wpi_softc *sc) 408 { 409 pcireg_t reg; 410 411 /* Clear device-specific "PCI retry timeout" register (41h). */ 412 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 413 reg &= ~0xff00; 414 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg); 415 416 wpi_init_task(sc); 417 } 418 419 void 420 wpi_init_task(void *arg1) 421 { 422 struct wpi_softc *sc = arg1; 423 struct ifnet *ifp = &sc->sc_ic.ic_if; 424 int s; 425 426 rw_enter_write(&sc->sc_rwlock); 427 s = splnet(); 428 429 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP) 430 wpi_init(ifp); 431 432 splx(s); 433 rw_exit_write(&sc->sc_rwlock); 434 } 435 436 int 437 wpi_nic_lock(struct wpi_softc *sc) 438 { 439 int ntries; 440 441 /* Request exclusive access to NIC. */ 442 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 443 444 /* Spin until we actually get the lock. */ 445 for (ntries = 0; ntries < 1000; ntries++) { 446 if ((WPI_READ(sc, WPI_GP_CNTRL) & 447 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) == 448 WPI_GP_CNTRL_MAC_ACCESS_ENA) 449 return 0; 450 DELAY(10); 451 } 452 return ETIMEDOUT; 453 } 454 455 static __inline void 456 wpi_nic_unlock(struct wpi_softc *sc) 457 { 458 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ); 459 } 460 461 static __inline uint32_t 462 wpi_prph_read(struct wpi_softc *sc, uint32_t addr) 463 { 464 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr); 465 WPI_BARRIER_READ_WRITE(sc); 466 return WPI_READ(sc, WPI_PRPH_RDATA); 467 } 468 469 static __inline void 470 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data) 471 { 472 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr); 473 WPI_BARRIER_WRITE(sc); 474 WPI_WRITE(sc, WPI_PRPH_WDATA, data); 475 } 476 477 static __inline void 478 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask) 479 { 480 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask); 481 } 482 483 static __inline void 484 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask) 485 { 486 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask); 487 } 488 489 static __inline void 490 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr, 491 const uint32_t *data, int count) 492 { 493 for (; count > 0; count--, data++, addr += 4) 494 wpi_prph_write(sc, addr, *data); 495 } 496 497 #ifdef WPI_DEBUG 498 499 static __inline uint32_t 500 wpi_mem_read(struct wpi_softc *sc, uint32_t addr) 501 { 502 WPI_WRITE(sc, WPI_MEM_RADDR, addr); 503 WPI_BARRIER_READ_WRITE(sc); 504 return WPI_READ(sc, WPI_MEM_RDATA); 505 } 506 507 static __inline void 508 wpi_mem_write(struct wpi_softc *sc, uint32_t addr, uint32_t data) 509 { 510 WPI_WRITE(sc, WPI_MEM_WADDR, addr); 511 WPI_BARRIER_WRITE(sc); 512 WPI_WRITE(sc, WPI_MEM_WDATA, data); 513 } 514 515 static __inline void 516 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data, 517 int count) 518 { 519 for (; count > 0; count--, addr += 4) 520 *data++ = wpi_mem_read(sc, addr); 521 } 522 523 #endif 524 525 int 526 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count) 527 { 528 uint8_t *out = data; 529 uint32_t val; 530 int error, ntries; 531 532 if ((error = wpi_nic_lock(sc)) != 0) 533 return error; 534 535 for (; count > 0; count -= 2, addr++) { 536 WPI_WRITE(sc, WPI_EEPROM, addr << 2); 537 WPI_CLRBITS(sc, WPI_EEPROM, WPI_EEPROM_CMD); 538 539 for (ntries = 0; ntries < 10; ntries++) { 540 val = WPI_READ(sc, WPI_EEPROM); 541 if (val & WPI_EEPROM_READ_VALID) 542 break; 543 DELAY(5); 544 } 545 if (ntries == 10) { 546 printf("%s: could not read EEPROM\n", 547 sc->sc_dev.dv_xname); 548 return ETIMEDOUT; 549 } 550 *out++ = val >> 16; 551 if (count > 1) 552 *out++ = val >> 24; 553 } 554 555 wpi_nic_unlock(sc); 556 return 0; 557 } 558 559 int 560 wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma, void **kvap, 561 bus_size_t size, bus_size_t alignment) 562 { 563 int nsegs, error; 564 565 dma->tag = tag; 566 dma->size = size; 567 568 error = bus_dmamap_create(tag, size, 1, size, 0, BUS_DMA_NOWAIT, 569 &dma->map); 570 if (error != 0) 571 goto fail; 572 573 error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs, 574 BUS_DMA_NOWAIT | BUS_DMA_ZERO); 575 if (error != 0) 576 goto fail; 577 578 error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, 579 BUS_DMA_NOWAIT | BUS_DMA_COHERENT); 580 if (error != 0) 581 goto fail; 582 583 error = bus_dmamap_load_raw(tag, dma->map, &dma->seg, 1, size, 584 BUS_DMA_NOWAIT); 585 if (error != 0) 586 goto fail; 587 588 bus_dmamap_sync(tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE); 589 590 dma->paddr = dma->map->dm_segs[0].ds_addr; 591 if (kvap != NULL) 592 *kvap = dma->vaddr; 593 594 return 0; 595 596 fail: wpi_dma_contig_free(dma); 597 return error; 598 } 599 600 void 601 wpi_dma_contig_free(struct wpi_dma_info *dma) 602 { 603 if (dma->map != NULL) { 604 if (dma->vaddr != NULL) { 605 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, 606 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 607 bus_dmamap_unload(dma->tag, dma->map); 608 bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size); 609 bus_dmamem_free(dma->tag, &dma->seg, 1); 610 dma->vaddr = NULL; 611 } 612 bus_dmamap_destroy(dma->tag, dma->map); 613 dma->map = NULL; 614 } 615 } 616 617 int 618 wpi_alloc_shared(struct wpi_softc *sc) 619 { 620 /* Shared buffer must be aligned on a 4KB boundary. */ 621 return wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma, 622 (void **)&sc->shared, sizeof (struct wpi_shared), 4096); 623 } 624 625 void 626 wpi_free_shared(struct wpi_softc *sc) 627 { 628 wpi_dma_contig_free(&sc->shared_dma); 629 } 630 631 int 632 wpi_alloc_fwmem(struct wpi_softc *sc) 633 { 634 /* Allocate enough contiguous space to store text and data. */ 635 return wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL, 636 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16); 637 } 638 639 void 640 wpi_free_fwmem(struct wpi_softc *sc) 641 { 642 wpi_dma_contig_free(&sc->fw_dma); 643 } 644 645 int 646 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 647 { 648 bus_size_t size; 649 int i, error; 650 651 ring->cur = 0; 652 653 /* Allocate RX descriptors (16KB aligned.) */ 654 size = WPI_RX_RING_COUNT * sizeof (uint32_t); 655 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 656 (void **)&ring->desc, size, 16 * 1024); 657 if (error != 0) { 658 printf("%s: could not allocate RX ring DMA memory\n", 659 sc->sc_dev.dv_xname); 660 goto fail; 661 } 662 663 /* 664 * Allocate and map RX buffers. 665 */ 666 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 667 struct wpi_rx_data *data = &ring->data[i]; 668 669 error = bus_dmamap_create(sc->sc_dmat, WPI_RBUF_SIZE, 1, 670 WPI_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map); 671 if (error != 0) { 672 printf("%s: could not create RX buf DMA map\n", 673 sc->sc_dev.dv_xname); 674 goto fail; 675 } 676 677 data->m = MCLGETI(NULL, M_DONTWAIT, NULL, WPI_RBUF_SIZE); 678 if (data->m == NULL) { 679 printf("%s: could not allocate RX mbuf\n", 680 sc->sc_dev.dv_xname); 681 error = ENOBUFS; 682 goto fail; 683 } 684 685 error = bus_dmamap_load(sc->sc_dmat, data->map, 686 mtod(data->m, void *), WPI_RBUF_SIZE, NULL, 687 BUS_DMA_NOWAIT | BUS_DMA_READ); 688 if (error != 0) { 689 printf("%s: can't map mbuf (error %d)\n", 690 sc->sc_dev.dv_xname, error); 691 goto fail; 692 } 693 694 /* Set physical address of RX buffer. */ 695 ring->desc[i] = htole32(data->map->dm_segs[0].ds_addr); 696 } 697 698 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, size, 699 BUS_DMASYNC_PREWRITE); 700 701 return 0; 702 703 fail: wpi_free_rx_ring(sc, ring); 704 return error; 705 } 706 707 void 708 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 709 { 710 int ntries; 711 712 if (wpi_nic_lock(sc) == 0) { 713 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0); 714 for (ntries = 0; ntries < 100; ntries++) { 715 if (WPI_READ(sc, WPI_FH_RX_STATUS) & 716 WPI_FH_RX_STATUS_IDLE) 717 break; 718 DELAY(10); 719 } 720 wpi_nic_unlock(sc); 721 } 722 ring->cur = 0; 723 } 724 725 void 726 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 727 { 728 int i; 729 730 wpi_dma_contig_free(&ring->desc_dma); 731 732 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 733 struct wpi_rx_data *data = &ring->data[i]; 734 735 if (data->m != NULL) { 736 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 737 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD); 738 bus_dmamap_unload(sc->sc_dmat, data->map); 739 m_freem(data->m); 740 } 741 if (data->map != NULL) 742 bus_dmamap_destroy(sc->sc_dmat, data->map); 743 } 744 } 745 746 int 747 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid) 748 { 749 bus_addr_t paddr; 750 bus_size_t size; 751 int i, error; 752 753 ring->qid = qid; 754 ring->queued = 0; 755 ring->cur = 0; 756 757 /* Allocate TX descriptors (16KB aligned.) */ 758 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc); 759 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 760 (void **)&ring->desc, size, 16 * 1024); 761 if (error != 0) { 762 printf("%s: could not allocate TX ring DMA memory\n", 763 sc->sc_dev.dv_xname); 764 goto fail; 765 } 766 767 /* Update shared area with ring physical address. */ 768 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr); 769 bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0, 770 sizeof (struct wpi_shared), BUS_DMASYNC_PREWRITE); 771 772 /* 773 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need 774 * to allocate commands space for other rings. 775 * XXX Do we really need to allocate descriptors for other rings? 776 */ 777 if (qid > 4) 778 return 0; 779 780 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd); 781 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma, 782 (void **)&ring->cmd, size, 4); 783 if (error != 0) { 784 printf("%s: could not allocate TX cmd DMA memory\n", 785 sc->sc_dev.dv_xname); 786 goto fail; 787 } 788 789 paddr = ring->cmd_dma.paddr; 790 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 791 struct wpi_tx_data *data = &ring->data[i]; 792 793 data->cmd_paddr = paddr; 794 paddr += sizeof (struct wpi_tx_cmd); 795 796 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 797 WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT, 798 &data->map); 799 if (error != 0) { 800 printf("%s: could not create TX buf DMA map\n", 801 sc->sc_dev.dv_xname); 802 goto fail; 803 } 804 } 805 return 0; 806 807 fail: wpi_free_tx_ring(sc, ring); 808 return error; 809 } 810 811 void 812 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 813 { 814 int i; 815 816 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 817 struct wpi_tx_data *data = &ring->data[i]; 818 819 if (data->m != NULL) { 820 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 821 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 822 bus_dmamap_unload(sc->sc_dmat, data->map); 823 m_freem(data->m); 824 data->m = NULL; 825 } 826 } 827 /* Clear TX descriptors. */ 828 memset(ring->desc, 0, ring->desc_dma.size); 829 sc->qfullmsk &= ~(1 << ring->qid); 830 ring->queued = 0; 831 ring->cur = 0; 832 } 833 834 void 835 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 836 { 837 int i; 838 839 wpi_dma_contig_free(&ring->desc_dma); 840 wpi_dma_contig_free(&ring->cmd_dma); 841 842 for (i = 0; i < WPI_TX_RING_COUNT; i++) { 843 struct wpi_tx_data *data = &ring->data[i]; 844 845 if (data->m != NULL) { 846 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 847 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 848 bus_dmamap_unload(sc->sc_dmat, data->map); 849 m_freem(data->m); 850 } 851 if (data->map != NULL) 852 bus_dmamap_destroy(sc->sc_dmat, data->map); 853 } 854 } 855 856 int 857 wpi_read_eeprom(struct wpi_softc *sc) 858 { 859 struct ieee80211com *ic = &sc->sc_ic; 860 char domain[4]; 861 int i; 862 863 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) { 864 printf("%s: bad EEPROM signature\n", sc->sc_dev.dv_xname); 865 return EIO; 866 } 867 /* Clear HW ownership of EEPROM. */ 868 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER); 869 870 wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1); 871 wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2); 872 wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1); 873 874 DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, letoh16(sc->rev), 875 sc->type)); 876 877 /* Read and print regulatory domain (4 ASCII characters.) */ 878 wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4); 879 printf(", %.4s", domain); 880 881 /* Read and print MAC address. */ 882 wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6); 883 printf(", address %s\n", ether_sprintf(ic->ic_myaddr)); 884 885 /* Read the list of authorized channels. */ 886 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++) 887 wpi_read_eeprom_channels(sc, i); 888 889 /* Read the list of TX power groups. */ 890 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++) 891 wpi_read_eeprom_group(sc, i); 892 893 return 0; 894 } 895 896 void 897 wpi_read_eeprom_channels(struct wpi_softc *sc, int n) 898 { 899 struct ieee80211com *ic = &sc->sc_ic; 900 const struct wpi_chan_band *band = &wpi_bands[n]; 901 struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND]; 902 int chan, i; 903 904 wpi_read_prom_data(sc, band->addr, channels, 905 band->nchan * sizeof (struct wpi_eeprom_chan)); 906 907 for (i = 0; i < band->nchan; i++) { 908 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) 909 continue; 910 911 chan = band->chan[i]; 912 913 if (n == 0) { /* 2GHz band */ 914 ic->ic_channels[chan].ic_freq = 915 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 916 ic->ic_channels[chan].ic_flags = 917 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 918 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 919 920 } else { /* 5GHz band */ 921 /* 922 * Some adapters support channels 7, 8, 11 and 12 923 * both in the 2GHz and 4.9GHz bands. 924 * Because of limitations in our net80211 layer, 925 * we don't support them in the 4.9GHz band. 926 */ 927 if (chan <= 14) 928 continue; 929 930 ic->ic_channels[chan].ic_freq = 931 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 932 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A; 933 /* We have at least one valid 5GHz channel. */ 934 sc->sc_flags |= WPI_FLAG_HAS_5GHZ; 935 } 936 937 /* Is active scan allowed on this channel? */ 938 if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) { 939 ic->ic_channels[chan].ic_flags |= 940 IEEE80211_CHAN_PASSIVE; 941 } 942 943 /* Save maximum allowed TX power for this channel. */ 944 sc->maxpwr[chan] = channels[i].maxpwr; 945 946 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n", 947 chan, channels[i].flags, sc->maxpwr[chan])); 948 } 949 } 950 951 void 952 wpi_read_eeprom_group(struct wpi_softc *sc, int n) 953 { 954 struct wpi_power_group *group = &sc->groups[n]; 955 struct wpi_eeprom_group rgroup; 956 int i; 957 958 wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup, 959 sizeof rgroup); 960 961 /* Save TX power group information. */ 962 group->chan = rgroup.chan; 963 group->maxpwr = rgroup.maxpwr; 964 /* Retrieve temperature at which the samples were taken. */ 965 group->temp = (int16_t)letoh16(rgroup.temp); 966 967 DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n, 968 group->chan, group->maxpwr, group->temp)); 969 970 for (i = 0; i < WPI_SAMPLES_COUNT; i++) { 971 group->samples[i].index = rgroup.samples[i].index; 972 group->samples[i].power = rgroup.samples[i].power; 973 974 DPRINTF(("\tsample %d: index=%d power=%d\n", i, 975 group->samples[i].index, group->samples[i].power)); 976 } 977 } 978 979 struct ieee80211_node * 980 wpi_node_alloc(struct ieee80211com *ic) 981 { 982 return malloc(sizeof (struct wpi_node), M_DEVBUF, M_NOWAIT | M_ZERO); 983 } 984 985 void 986 wpi_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 987 { 988 struct wpi_softc *sc = ic->ic_if.if_softc; 989 struct wpi_node *wn = (void *)ni; 990 uint8_t rate; 991 int ridx, i; 992 993 ieee80211_amrr_node_init(&sc->amrr, &wn->amn); 994 /* Start at lowest available bit-rate, AMRR will raise. */ 995 ni->ni_txrate = 0; 996 997 for (i = 0; i < ni->ni_rates.rs_nrates; i++) { 998 rate = ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL; 999 /* Map 802.11 rate to HW rate index. */ 1000 for (ridx = 0; ridx <= WPI_RIDX_MAX; ridx++) 1001 if (wpi_rates[ridx].rate == rate) 1002 break; 1003 wn->ridx[i] = ridx; 1004 } 1005 } 1006 1007 int 1008 wpi_media_change(struct ifnet *ifp) 1009 { 1010 struct wpi_softc *sc = ifp->if_softc; 1011 struct ieee80211com *ic = &sc->sc_ic; 1012 uint8_t rate, ridx; 1013 int error; 1014 1015 error = ieee80211_media_change(ifp); 1016 if (error != ENETRESET) 1017 return error; 1018 1019 if (ic->ic_fixed_rate != -1) { 1020 rate = ic->ic_sup_rates[ic->ic_curmode]. 1021 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 1022 /* Map 802.11 rate to HW rate index. */ 1023 for (ridx = 0; ridx <= WPI_RIDX_MAX; ridx++) 1024 if (wpi_rates[ridx].rate == rate) 1025 break; 1026 sc->fixed_ridx = ridx; 1027 } 1028 1029 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1030 (IFF_UP | IFF_RUNNING)) { 1031 wpi_stop(ifp, 0); 1032 error = wpi_init(ifp); 1033 } 1034 return error; 1035 } 1036 1037 int 1038 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1039 { 1040 struct ifnet *ifp = &ic->ic_if; 1041 struct wpi_softc *sc = ifp->if_softc; 1042 int error; 1043 1044 timeout_del(&sc->calib_to); 1045 1046 switch (nstate) { 1047 case IEEE80211_S_SCAN: 1048 /* Make the link LED blink while we're scanning. */ 1049 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 1050 1051 if ((error = wpi_scan(sc, IEEE80211_CHAN_2GHZ)) != 0) { 1052 printf("%s: could not initiate scan\n", 1053 sc->sc_dev.dv_xname); 1054 return error; 1055 } 1056 if (ifp->if_flags & IFF_DEBUG) 1057 printf("%s: %s -> %s\n", ifp->if_xname, 1058 ieee80211_state_name[ic->ic_state], 1059 ieee80211_state_name[nstate]); 1060 ieee80211_set_link_state(ic, LINK_STATE_DOWN); 1061 ieee80211_node_cleanup(ic, ic->ic_bss); 1062 ic->ic_state = nstate; 1063 return 0; 1064 1065 case IEEE80211_S_ASSOC: 1066 if (ic->ic_state != IEEE80211_S_RUN) 1067 break; 1068 /* FALLTHROUGH */ 1069 case IEEE80211_S_AUTH: 1070 /* Reset state to handle reassociations correctly. */ 1071 sc->rxon.associd = 0; 1072 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS); 1073 1074 if ((error = wpi_auth(sc)) != 0) { 1075 printf("%s: could not move to auth state\n", 1076 sc->sc_dev.dv_xname); 1077 return error; 1078 } 1079 break; 1080 1081 case IEEE80211_S_RUN: 1082 if ((error = wpi_run(sc)) != 0) { 1083 printf("%s: could not move to run state\n", 1084 sc->sc_dev.dv_xname); 1085 return error; 1086 } 1087 break; 1088 1089 case IEEE80211_S_INIT: 1090 break; 1091 } 1092 1093 return sc->sc_newstate(ic, nstate, arg); 1094 } 1095 1096 void 1097 wpi_iter_func(void *arg, struct ieee80211_node *ni) 1098 { 1099 struct wpi_softc *sc = arg; 1100 struct wpi_node *wn = (struct wpi_node *)ni; 1101 1102 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn); 1103 } 1104 1105 void 1106 wpi_calib_timeout(void *arg) 1107 { 1108 struct wpi_softc *sc = arg; 1109 struct ieee80211com *ic = &sc->sc_ic; 1110 int s; 1111 1112 s = splnet(); 1113 /* Automatic rate control triggered every 500ms. */ 1114 if (ic->ic_fixed_rate == -1) { 1115 if (ic->ic_opmode == IEEE80211_M_STA) 1116 wpi_iter_func(sc, ic->ic_bss); 1117 else 1118 ieee80211_iterate_nodes(ic, wpi_iter_func, sc); 1119 } 1120 1121 /* Force automatic TX power calibration every 60 secs. */ 1122 if (++sc->calib_cnt >= 120) { 1123 wpi_power_calibration(sc); 1124 sc->calib_cnt = 0; 1125 } 1126 splx(s); 1127 1128 /* Automatic rate control triggered every 500ms. */ 1129 timeout_add_msec(&sc->calib_to, 500); 1130 } 1131 1132 int 1133 wpi_ccmp_decap(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_key *k) 1134 { 1135 struct ieee80211_frame *wh; 1136 uint64_t pn, *prsc; 1137 uint8_t *ivp; 1138 uint8_t tid; 1139 int hdrlen; 1140 1141 wh = mtod(m, struct ieee80211_frame *); 1142 hdrlen = ieee80211_get_hdrlen(wh); 1143 ivp = (uint8_t *)wh + hdrlen; 1144 1145 /* Check that ExtIV bit is be set. */ 1146 if (!(ivp[3] & IEEE80211_WEP_EXTIV)) { 1147 DPRINTF(("CCMP decap ExtIV not set\n")); 1148 return 1; 1149 } 1150 tid = ieee80211_has_qos(wh) ? 1151 ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0; 1152 prsc = &k->k_rsc[tid]; 1153 1154 /* Extract the 48-bit PN from the CCMP header. */ 1155 pn = (uint64_t)ivp[0] | 1156 (uint64_t)ivp[1] << 8 | 1157 (uint64_t)ivp[4] << 16 | 1158 (uint64_t)ivp[5] << 24 | 1159 (uint64_t)ivp[6] << 32 | 1160 (uint64_t)ivp[7] << 40; 1161 if (pn <= *prsc) { 1162 /* 1163 * Not necessarily a replayed frame since we did not check 1164 * the sequence number of the 802.11 header yet. 1165 */ 1166 DPRINTF(("CCMP replayed\n")); 1167 return 1; 1168 } 1169 /* Update last seen packet number. */ 1170 *prsc = pn; 1171 1172 /* Clear Protected bit and strip IV. */ 1173 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED; 1174 memmove(mtod(m, caddr_t) + IEEE80211_CCMP_HDRLEN, wh, hdrlen); 1175 m_adj(m, IEEE80211_CCMP_HDRLEN); 1176 /* Strip MIC. */ 1177 m_adj(m, -IEEE80211_CCMP_MICLEN); 1178 return 0; 1179 } 1180 1181 void 1182 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc, 1183 struct wpi_rx_data *data, struct mbuf_list *ml) 1184 { 1185 struct ieee80211com *ic = &sc->sc_ic; 1186 struct ifnet *ifp = &ic->ic_if; 1187 struct wpi_rx_ring *ring = &sc->rxq; 1188 struct wpi_rx_stat *stat; 1189 struct wpi_rx_head *head; 1190 struct wpi_rx_tail *tail; 1191 struct ieee80211_frame *wh; 1192 struct ieee80211_rxinfo rxi; 1193 struct ieee80211_node *ni; 1194 struct mbuf *m, *m1; 1195 uint32_t flags; 1196 int error; 1197 1198 bus_dmamap_sync(sc->sc_dmat, data->map, 0, WPI_RBUF_SIZE, 1199 BUS_DMASYNC_POSTREAD); 1200 stat = (struct wpi_rx_stat *)(desc + 1); 1201 1202 if (stat->len > WPI_STAT_MAXLEN) { 1203 printf("%s: invalid RX statistic header\n", 1204 sc->sc_dev.dv_xname); 1205 ifp->if_ierrors++; 1206 return; 1207 } 1208 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len); 1209 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + letoh16(head->len)); 1210 flags = letoh32(tail->flags); 1211 1212 /* Discard frames with a bad FCS early. */ 1213 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) { 1214 DPRINTFN(2, ("rx tail flags error %x\n", flags)); 1215 ifp->if_ierrors++; 1216 return; 1217 } 1218 /* Discard frames that are too short. */ 1219 if (letoh16(head->len) < sizeof (*wh)) { 1220 DPRINTF(("frame too short: %d\n", letoh16(head->len))); 1221 ic->ic_stats.is_rx_tooshort++; 1222 ifp->if_ierrors++; 1223 return; 1224 } 1225 1226 m1 = MCLGETI(NULL, M_DONTWAIT, NULL, WPI_RBUF_SIZE); 1227 if (m1 == NULL) { 1228 ic->ic_stats.is_rx_nombuf++; 1229 ifp->if_ierrors++; 1230 return; 1231 } 1232 bus_dmamap_unload(sc->sc_dmat, data->map); 1233 1234 error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(m1, void *), 1235 WPI_RBUF_SIZE, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ); 1236 if (error != 0) { 1237 m_freem(m1); 1238 1239 /* Try to reload the old mbuf. */ 1240 error = bus_dmamap_load(sc->sc_dmat, data->map, 1241 mtod(data->m, void *), WPI_RBUF_SIZE, NULL, 1242 BUS_DMA_NOWAIT | BUS_DMA_READ); 1243 if (error != 0) { 1244 panic("%s: could not load old RX mbuf", 1245 sc->sc_dev.dv_xname); 1246 } 1247 /* Physical address may have changed. */ 1248 ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr); 1249 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 1250 ring->cur * sizeof (uint32_t), sizeof (uint32_t), 1251 BUS_DMASYNC_PREWRITE); 1252 ifp->if_ierrors++; 1253 return; 1254 } 1255 1256 m = data->m; 1257 data->m = m1; 1258 /* Update RX descriptor. */ 1259 ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr); 1260 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 1261 ring->cur * sizeof (uint32_t), sizeof (uint32_t), 1262 BUS_DMASYNC_PREWRITE); 1263 1264 /* Finalize mbuf. */ 1265 m->m_data = (caddr_t)(head + 1); 1266 m->m_pkthdr.len = m->m_len = letoh16(head->len); 1267 1268 /* Grab a reference to the source node. */ 1269 wh = mtod(m, struct ieee80211_frame *); 1270 ni = ieee80211_find_rxnode(ic, wh); 1271 1272 rxi.rxi_flags = 0; 1273 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) && 1274 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 1275 (ni->ni_flags & IEEE80211_NODE_RXPROT) && 1276 ni->ni_pairwise_key.k_cipher == IEEE80211_CIPHER_CCMP) { 1277 if ((flags & WPI_RX_CIPHER_MASK) != WPI_RX_CIPHER_CCMP) { 1278 ic->ic_stats.is_ccmp_dec_errs++; 1279 ifp->if_ierrors++; 1280 m_freem(m); 1281 return; 1282 } 1283 /* Check whether decryption was successful or not. */ 1284 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) { 1285 DPRINTF(("CCMP decryption failed 0x%x\n", flags)); 1286 ic->ic_stats.is_ccmp_dec_errs++; 1287 ifp->if_ierrors++; 1288 m_freem(m); 1289 return; 1290 } 1291 if (wpi_ccmp_decap(sc, m, &ni->ni_pairwise_key) != 0) { 1292 ifp->if_ierrors++; 1293 m_freem(m); 1294 return; 1295 } 1296 rxi.rxi_flags |= IEEE80211_RXI_HWDEC; 1297 } 1298 1299 #if NBPFILTER > 0 1300 if (sc->sc_drvbpf != NULL) { 1301 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap; 1302 1303 tap->wr_flags = 0; 1304 if (letoh16(head->flags) & 0x4) 1305 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1306 tap->wr_chan_freq = 1307 htole16(ic->ic_channels[head->chan].ic_freq); 1308 tap->wr_chan_flags = 1309 htole16(ic->ic_channels[head->chan].ic_flags); 1310 tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET); 1311 tap->wr_dbm_antnoise = (int8_t)letoh16(stat->noise); 1312 tap->wr_tsft = tail->tstamp; 1313 tap->wr_antenna = (letoh16(head->flags) >> 4) & 0xf; 1314 switch (head->rate) { 1315 /* CCK rates. */ 1316 case 10: tap->wr_rate = 2; break; 1317 case 20: tap->wr_rate = 4; break; 1318 case 55: tap->wr_rate = 11; break; 1319 case 110: tap->wr_rate = 22; break; 1320 /* OFDM rates. */ 1321 case 0xd: tap->wr_rate = 12; break; 1322 case 0xf: tap->wr_rate = 18; break; 1323 case 0x5: tap->wr_rate = 24; break; 1324 case 0x7: tap->wr_rate = 36; break; 1325 case 0x9: tap->wr_rate = 48; break; 1326 case 0xb: tap->wr_rate = 72; break; 1327 case 0x1: tap->wr_rate = 96; break; 1328 case 0x3: tap->wr_rate = 108; break; 1329 /* Unknown rate: should not happen. */ 1330 default: tap->wr_rate = 0; 1331 } 1332 1333 bpf_mtap_hdr(sc->sc_drvbpf, tap, sc->sc_rxtap_len, 1334 m, BPF_DIRECTION_IN); 1335 } 1336 #endif 1337 1338 /* Send the frame to the 802.11 layer. */ 1339 rxi.rxi_rssi = stat->rssi; 1340 rxi.rxi_tstamp = 0; /* unused */ 1341 ieee80211_inputm(ifp, m, ni, &rxi, ml); 1342 1343 /* Node is no longer needed. */ 1344 ieee80211_release_node(ic, ni); 1345 } 1346 1347 void 1348 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1349 { 1350 struct ieee80211com *ic = &sc->sc_ic; 1351 struct ifnet *ifp = &ic->ic_if; 1352 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3]; 1353 struct wpi_tx_data *data = &ring->data[desc->idx]; 1354 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1); 1355 struct wpi_node *wn = (struct wpi_node *)data->ni; 1356 1357 /* Update rate control statistics. */ 1358 wn->amn.amn_txcnt++; 1359 if (stat->retrycnt > 0) 1360 wn->amn.amn_retrycnt++; 1361 1362 if ((letoh32(stat->status) & 0xff) != 1) 1363 ifp->if_oerrors++; 1364 1365 /* Unmap and free mbuf. */ 1366 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 1367 BUS_DMASYNC_POSTWRITE); 1368 bus_dmamap_unload(sc->sc_dmat, data->map); 1369 m_freem(data->m); 1370 data->m = NULL; 1371 ieee80211_release_node(ic, data->ni); 1372 data->ni = NULL; 1373 1374 sc->sc_tx_timer = 0; 1375 if (--ring->queued < WPI_TX_RING_LOMARK) { 1376 sc->qfullmsk &= ~(1 << ring->qid); 1377 if (sc->qfullmsk == 0 && ifq_is_oactive(&ifp->if_snd)) { 1378 ifq_clr_oactive(&ifp->if_snd); 1379 (*ifp->if_start)(ifp); 1380 } 1381 } 1382 } 1383 1384 void 1385 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1386 { 1387 struct wpi_tx_ring *ring = &sc->txq[4]; 1388 struct wpi_tx_data *data; 1389 1390 if ((desc->qid & 7) != 4) 1391 return; /* Not a command ack. */ 1392 1393 data = &ring->data[desc->idx]; 1394 1395 /* If the command was mapped in an mbuf, free it. */ 1396 if (data->m != NULL) { 1397 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1398 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1399 bus_dmamap_unload(sc->sc_dmat, data->map); 1400 m_freem(data->m); 1401 data->m = NULL; 1402 } 1403 wakeup(&ring->cmd[desc->idx]); 1404 } 1405 1406 void 1407 wpi_notif_intr(struct wpi_softc *sc) 1408 { 1409 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 1410 struct ieee80211com *ic = &sc->sc_ic; 1411 struct ifnet *ifp = &ic->ic_if; 1412 uint32_t hw; 1413 1414 bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0, 1415 sizeof (struct wpi_shared), BUS_DMASYNC_POSTREAD); 1416 1417 hw = letoh32(sc->shared->next); 1418 while (sc->rxq.cur != hw) { 1419 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur]; 1420 struct wpi_rx_desc *desc; 1421 1422 bus_dmamap_sync(sc->sc_dmat, data->map, 0, sizeof (*desc), 1423 BUS_DMASYNC_POSTREAD); 1424 desc = mtod(data->m, struct wpi_rx_desc *); 1425 1426 DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d " 1427 "len=%d\n", desc->qid, desc->idx, desc->flags, desc->type, 1428 letoh32(desc->len))); 1429 1430 if (!(desc->qid & 0x80)) /* Reply to a command. */ 1431 wpi_cmd_done(sc, desc); 1432 1433 switch (desc->type) { 1434 case WPI_RX_DONE: 1435 /* An 802.11 frame has been received. */ 1436 wpi_rx_done(sc, desc, data, &ml); 1437 break; 1438 1439 case WPI_TX_DONE: 1440 /* An 802.11 frame has been transmitted. */ 1441 wpi_tx_done(sc, desc); 1442 break; 1443 1444 case WPI_UC_READY: 1445 { 1446 struct wpi_ucode_info *uc = 1447 (struct wpi_ucode_info *)(desc + 1); 1448 1449 /* The microcontroller is ready. */ 1450 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), 1451 sizeof (*uc), BUS_DMASYNC_POSTREAD); 1452 DPRINTF(("microcode alive notification version %x " 1453 "alive %x\n", letoh32(uc->version), 1454 letoh32(uc->valid))); 1455 1456 if (letoh32(uc->valid) != 1) { 1457 printf("%s: microcontroller initialization " 1458 "failed\n", sc->sc_dev.dv_xname); 1459 } 1460 if (uc->subtype != WPI_UCODE_INIT) { 1461 /* Save the address of the error log. */ 1462 sc->errptr = letoh32(uc->errptr); 1463 } 1464 break; 1465 } 1466 case WPI_STATE_CHANGED: 1467 { 1468 uint32_t *status = (uint32_t *)(desc + 1); 1469 1470 /* Enabled/disabled notification. */ 1471 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), 1472 sizeof (*status), BUS_DMASYNC_POSTREAD); 1473 DPRINTF(("state changed to %x\n", letoh32(*status))); 1474 1475 if (letoh32(*status) & 1) { 1476 /* The radio button has to be pushed. */ 1477 printf("%s: Radio transmitter is off\n", 1478 sc->sc_dev.dv_xname); 1479 /* Turn the interface down. */ 1480 wpi_stop(ifp, 1); 1481 return; /* No further processing. */ 1482 } 1483 break; 1484 } 1485 case WPI_START_SCAN: 1486 { 1487 struct wpi_start_scan *scan = 1488 (struct wpi_start_scan *)(desc + 1); 1489 1490 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), 1491 sizeof (*scan), BUS_DMASYNC_POSTREAD); 1492 DPRINTFN(2, ("scanning channel %d status %x\n", 1493 scan->chan, letoh32(scan->status))); 1494 1495 /* Fix current channel. */ 1496 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan]; 1497 break; 1498 } 1499 case WPI_STOP_SCAN: 1500 { 1501 struct wpi_stop_scan *scan = 1502 (struct wpi_stop_scan *)(desc + 1); 1503 1504 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc), 1505 sizeof (*scan), BUS_DMASYNC_POSTREAD); 1506 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n", 1507 scan->nchan, scan->status, scan->chan)); 1508 1509 if (scan->status == 1 && scan->chan <= 14 && 1510 (sc->sc_flags & WPI_FLAG_HAS_5GHZ)) { 1511 /* 1512 * We just finished scanning 2GHz channels, 1513 * start scanning 5GHz ones. 1514 */ 1515 if (wpi_scan(sc, IEEE80211_CHAN_5GHZ) == 0) 1516 break; 1517 } 1518 ieee80211_end_scan(ifp); 1519 break; 1520 } 1521 } 1522 1523 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT; 1524 } 1525 if_input(&ic->ic_if, &ml); 1526 1527 /* Tell the firmware what we have processed. */ 1528 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 1529 WPI_WRITE(sc, WPI_FH_RX_WPTR, hw & ~7); 1530 } 1531 1532 #ifdef WPI_DEBUG 1533 /* 1534 * Dump the error log of the firmware when a firmware panic occurs. Although 1535 * we can't debug the firmware because it is neither open source nor free, it 1536 * can help us to identify certain classes of problems. 1537 */ 1538 void 1539 wpi_fatal_intr(struct wpi_softc *sc) 1540 { 1541 #define N(a) (sizeof (a) / sizeof ((a)[0])) 1542 struct wpi_fwdump dump; 1543 uint32_t i, offset, count; 1544 1545 /* Check that the error log address is valid. */ 1546 if (sc->errptr < WPI_FW_DATA_BASE || 1547 sc->errptr + sizeof (dump) > 1548 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) { 1549 printf("%s: bad firmware error log address 0x%08x\n", 1550 sc->sc_dev.dv_xname, sc->errptr); 1551 return; 1552 } 1553 1554 if (wpi_nic_lock(sc) != 0) { 1555 printf("%s: could not read firmware error log\n", 1556 sc->sc_dev.dv_xname); 1557 return; 1558 } 1559 /* Read number of entries in the log. */ 1560 count = wpi_mem_read(sc, sc->errptr); 1561 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) { 1562 printf("%s: invalid count field (count=%u)\n", 1563 sc->sc_dev.dv_xname, count); 1564 wpi_nic_unlock(sc); 1565 return; 1566 } 1567 /* Skip "count" field. */ 1568 offset = sc->errptr + sizeof (uint32_t); 1569 printf("firmware error log (count=%u):\n", count); 1570 for (i = 0; i < count; i++) { 1571 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump, 1572 sizeof (dump) / sizeof (uint32_t)); 1573 1574 printf(" error type = \"%s\" (0x%08X)\n", 1575 (dump.desc < N(wpi_fw_errmsg)) ? 1576 wpi_fw_errmsg[dump.desc] : "UNKNOWN", 1577 dump.desc); 1578 printf(" error data = 0x%08X\n", 1579 dump.data); 1580 printf(" branch link = 0x%08X%08X\n", 1581 dump.blink[0], dump.blink[1]); 1582 printf(" interrupt link = 0x%08X%08X\n", 1583 dump.ilink[0], dump.ilink[1]); 1584 printf(" time = %u\n", dump.time); 1585 1586 offset += sizeof (dump); 1587 } 1588 wpi_nic_unlock(sc); 1589 /* Dump driver status (TX and RX rings) while we're here. */ 1590 printf("driver status:\n"); 1591 for (i = 0; i < 6; i++) { 1592 struct wpi_tx_ring *ring = &sc->txq[i]; 1593 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n", 1594 i, ring->qid, ring->cur, ring->queued); 1595 } 1596 printf(" rx ring: cur=%d\n", sc->rxq.cur); 1597 printf(" 802.11 state %d\n", sc->sc_ic.ic_state); 1598 #undef N 1599 } 1600 #endif 1601 1602 int 1603 wpi_intr(void *arg) 1604 { 1605 struct wpi_softc *sc = arg; 1606 struct ifnet *ifp = &sc->sc_ic.ic_if; 1607 uint32_t r1, r2; 1608 1609 /* Disable interrupts. */ 1610 WPI_WRITE(sc, WPI_MASK, 0); 1611 1612 r1 = WPI_READ(sc, WPI_INT); 1613 r2 = WPI_READ(sc, WPI_FH_INT); 1614 1615 if (r1 == 0 && r2 == 0) { 1616 if (ifp->if_flags & IFF_UP) 1617 WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK); 1618 return 0; /* Interrupt not for us. */ 1619 } 1620 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0) 1621 return 0; /* Hardware gone! */ 1622 1623 /* Acknowledge interrupts. */ 1624 WPI_WRITE(sc, WPI_INT, r1); 1625 WPI_WRITE(sc, WPI_FH_INT, r2); 1626 1627 if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) { 1628 printf("%s: fatal firmware error\n", sc->sc_dev.dv_xname); 1629 /* Dump firmware error log and stop. */ 1630 #ifdef WPI_DEBUG 1631 wpi_fatal_intr(sc); 1632 #endif 1633 wpi_stop(ifp, 1); 1634 task_add(systq, &sc->init_task); 1635 return 1; 1636 } 1637 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) || 1638 (r2 & WPI_FH_INT_RX)) 1639 wpi_notif_intr(sc); 1640 1641 if (r1 & WPI_INT_ALIVE) 1642 wakeup(sc); /* Firmware is alive. */ 1643 1644 /* Re-enable interrupts. */ 1645 if (ifp->if_flags & IFF_UP) 1646 WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK); 1647 1648 return 1; 1649 } 1650 1651 int 1652 wpi_tx(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 1653 { 1654 struct ieee80211com *ic = &sc->sc_ic; 1655 struct wpi_node *wn = (void *)ni; 1656 struct wpi_tx_ring *ring; 1657 struct wpi_tx_desc *desc; 1658 struct wpi_tx_data *data; 1659 struct wpi_tx_cmd *cmd; 1660 struct wpi_cmd_data *tx; 1661 const struct wpi_rate *rinfo; 1662 struct ieee80211_frame *wh; 1663 struct ieee80211_key *k = NULL; 1664 enum ieee80211_edca_ac ac; 1665 uint32_t flags; 1666 uint16_t qos; 1667 u_int hdrlen; 1668 uint8_t *ivp, tid, ridx, type; 1669 int i, totlen, hasqos, error; 1670 1671 wh = mtod(m, struct ieee80211_frame *); 1672 hdrlen = ieee80211_get_hdrlen(wh); 1673 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1674 1675 /* Select EDCA Access Category and TX ring for this frame. */ 1676 if ((hasqos = ieee80211_has_qos(wh))) { 1677 qos = ieee80211_get_qos(wh); 1678 tid = qos & IEEE80211_QOS_TID; 1679 ac = ieee80211_up_to_ac(ic, tid); 1680 } else { 1681 tid = 0; 1682 ac = EDCA_AC_BE; 1683 } 1684 1685 ring = &sc->txq[ac]; 1686 desc = &ring->desc[ring->cur]; 1687 data = &ring->data[ring->cur]; 1688 1689 /* Choose a TX rate index. */ 1690 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 1691 type != IEEE80211_FC0_TYPE_DATA) { 1692 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ? 1693 WPI_RIDX_OFDM6 : WPI_RIDX_CCK1; 1694 } else if (ic->ic_fixed_rate != -1) { 1695 ridx = sc->fixed_ridx; 1696 } else 1697 ridx = wn->ridx[ni->ni_txrate]; 1698 rinfo = &wpi_rates[ridx]; 1699 1700 #if NBPFILTER > 0 1701 if (sc->sc_drvbpf != NULL) { 1702 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 1703 1704 tap->wt_flags = 0; 1705 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq); 1706 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags); 1707 tap->wt_rate = rinfo->rate; 1708 tap->wt_hwqueue = ac; 1709 if ((ic->ic_flags & IEEE80211_F_WEPON) && 1710 (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)) 1711 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 1712 1713 bpf_mtap_hdr(sc->sc_drvbpf, tap, sc->sc_txtap_len, 1714 m, BPF_DIRECTION_OUT); 1715 } 1716 #endif 1717 1718 totlen = m->m_pkthdr.len; 1719 1720 /* Encrypt the frame if need be. */ 1721 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1722 /* Retrieve key for TX. */ 1723 k = ieee80211_get_txkey(ic, wh, ni); 1724 if (k->k_cipher != IEEE80211_CIPHER_CCMP) { 1725 /* Do software encryption. */ 1726 if ((m = ieee80211_encrypt(ic, m, k)) == NULL) 1727 return ENOBUFS; 1728 /* 802.11 header may have moved. */ 1729 wh = mtod(m, struct ieee80211_frame *); 1730 totlen = m->m_pkthdr.len; 1731 1732 } else /* HW appends CCMP MIC. */ 1733 totlen += IEEE80211_CCMP_HDRLEN; 1734 } 1735 1736 /* Prepare TX firmware command. */ 1737 cmd = &ring->cmd[ring->cur]; 1738 cmd->code = WPI_CMD_TX_DATA; 1739 cmd->flags = 0; 1740 cmd->qid = ring->qid; 1741 cmd->idx = ring->cur; 1742 1743 tx = (struct wpi_cmd_data *)cmd->data; 1744 /* NB: No need to clear tx, all fields are reinitialized here. */ 1745 1746 flags = 0; 1747 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1748 /* Unicast frame, check if an ACK is expected. */ 1749 if (!hasqos || (qos & IEEE80211_QOS_ACK_POLICY_MASK) != 1750 IEEE80211_QOS_ACK_POLICY_NOACK) 1751 flags |= WPI_TX_NEED_ACK; 1752 } 1753 1754 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */ 1755 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1756 /* NB: Group frames are sent using CCK in 802.11b/g. */ 1757 if (totlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) { 1758 flags |= WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP; 1759 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1760 ridx <= WPI_RIDX_OFDM54) { 1761 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 1762 flags |= WPI_TX_NEED_CTS | WPI_TX_FULL_TXOP; 1763 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 1764 flags |= WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP; 1765 } 1766 } 1767 1768 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 1769 type != IEEE80211_FC0_TYPE_DATA) 1770 tx->id = WPI_ID_BROADCAST; 1771 else 1772 tx->id = wn->id; 1773 1774 if (type == IEEE80211_FC0_TYPE_MGT) { 1775 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1776 1777 #ifndef IEEE80211_STA_ONLY 1778 /* Tell HW to set timestamp in probe responses. */ 1779 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1780 flags |= WPI_TX_INSERT_TSTAMP; 1781 #endif 1782 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 1783 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 1784 tx->timeout = htole16(3); 1785 else 1786 tx->timeout = htole16(2); 1787 } else 1788 tx->timeout = htole16(0); 1789 1790 tx->len = htole16(totlen); 1791 tx->tid = tid; 1792 tx->rts_ntries = 7; 1793 tx->data_ntries = 15; 1794 tx->ofdm_mask = 0xff; 1795 tx->cck_mask = 0x0f; 1796 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 1797 tx->plcp = rinfo->plcp; 1798 1799 /* Copy 802.11 header in TX command. */ 1800 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 1801 1802 if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) { 1803 /* Trim 802.11 header and prepend CCMP IV. */ 1804 m_adj(m, hdrlen - IEEE80211_CCMP_HDRLEN); 1805 ivp = mtod(m, uint8_t *); 1806 k->k_tsc++; 1807 ivp[0] = k->k_tsc; 1808 ivp[1] = k->k_tsc >> 8; 1809 ivp[2] = 0; 1810 ivp[3] = k->k_id << 6 | IEEE80211_WEP_EXTIV; 1811 ivp[4] = k->k_tsc >> 16; 1812 ivp[5] = k->k_tsc >> 24; 1813 ivp[6] = k->k_tsc >> 32; 1814 ivp[7] = k->k_tsc >> 40; 1815 1816 tx->security = WPI_CIPHER_CCMP; 1817 memcpy(tx->key, k->k_key, k->k_len); 1818 } else { 1819 /* Trim 802.11 header. */ 1820 m_adj(m, hdrlen); 1821 tx->security = 0; 1822 } 1823 tx->flags = htole32(flags); 1824 1825 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m, 1826 BUS_DMA_NOWAIT | BUS_DMA_WRITE); 1827 if (error != 0 && error != EFBIG) { 1828 printf("%s: can't map mbuf (error %d)\n", 1829 sc->sc_dev.dv_xname, error); 1830 m_freem(m); 1831 return error; 1832 } 1833 if (error != 0) { 1834 /* Too many DMA segments, linearize mbuf. */ 1835 if (m_defrag(m, M_DONTWAIT)) { 1836 m_freem(m); 1837 return ENOBUFS; 1838 } 1839 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m, 1840 BUS_DMA_NOWAIT | BUS_DMA_WRITE); 1841 if (error != 0) { 1842 printf("%s: can't map mbuf (error %d)\n", 1843 sc->sc_dev.dv_xname, error); 1844 m_freem(m); 1845 return error; 1846 } 1847 } 1848 1849 data->m = m; 1850 data->ni = ni; 1851 1852 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n", 1853 ring->qid, ring->cur, m->m_pkthdr.len, data->map->dm_nsegs)); 1854 1855 /* Fill TX descriptor. */ 1856 desc->flags = htole32(WPI_PAD32(m->m_pkthdr.len) << 28 | 1857 (1 + data->map->dm_nsegs) << 24); 1858 /* First DMA segment is used by the TX command. */ 1859 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 1860 ring->cur * sizeof (struct wpi_tx_cmd)); 1861 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data) + 1862 ((hdrlen + 3) & ~3)); 1863 /* Other DMA segments are for data payload. */ 1864 for (i = 1; i <= data->map->dm_nsegs; i++) { 1865 desc->segs[i].addr = 1866 htole32(data->map->dm_segs[i - 1].ds_addr); 1867 desc->segs[i].len = 1868 htole32(data->map->dm_segs[i - 1].ds_len); 1869 } 1870 1871 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 1872 BUS_DMASYNC_PREWRITE); 1873 bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map, 1874 (caddr_t)cmd - ring->cmd_dma.vaddr, sizeof (*cmd), 1875 BUS_DMASYNC_PREWRITE); 1876 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 1877 (caddr_t)desc - ring->desc_dma.vaddr, sizeof (*desc), 1878 BUS_DMASYNC_PREWRITE); 1879 1880 /* Kick TX ring. */ 1881 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 1882 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 1883 1884 /* Mark TX ring as full if we reach a certain threshold. */ 1885 if (++ring->queued > WPI_TX_RING_HIMARK) 1886 sc->qfullmsk |= 1 << ring->qid; 1887 1888 return 0; 1889 } 1890 1891 void 1892 wpi_start(struct ifnet *ifp) 1893 { 1894 struct wpi_softc *sc = ifp->if_softc; 1895 struct ieee80211com *ic = &sc->sc_ic; 1896 struct ieee80211_node *ni; 1897 struct mbuf *m; 1898 1899 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd)) 1900 return; 1901 1902 for (;;) { 1903 if (sc->qfullmsk != 0) { 1904 ifq_set_oactive(&ifp->if_snd); 1905 break; 1906 } 1907 /* Send pending management frames first. */ 1908 m = mq_dequeue(&ic->ic_mgtq); 1909 if (m != NULL) { 1910 ni = m->m_pkthdr.ph_cookie; 1911 goto sendit; 1912 } 1913 if (ic->ic_state != IEEE80211_S_RUN) 1914 break; 1915 1916 /* Encapsulate and send data frames. */ 1917 IFQ_DEQUEUE(&ifp->if_snd, m); 1918 if (m == NULL) 1919 break; 1920 #if NBPFILTER > 0 1921 if (ifp->if_bpf != NULL) 1922 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 1923 #endif 1924 if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) 1925 continue; 1926 sendit: 1927 #if NBPFILTER > 0 1928 if (ic->ic_rawbpf != NULL) 1929 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 1930 #endif 1931 if (wpi_tx(sc, m, ni) != 0) { 1932 ieee80211_release_node(ic, ni); 1933 ifp->if_oerrors++; 1934 continue; 1935 } 1936 1937 sc->sc_tx_timer = 5; 1938 ifp->if_timer = 1; 1939 } 1940 } 1941 1942 void 1943 wpi_watchdog(struct ifnet *ifp) 1944 { 1945 struct wpi_softc *sc = ifp->if_softc; 1946 1947 ifp->if_timer = 0; 1948 1949 if (sc->sc_tx_timer > 0) { 1950 if (--sc->sc_tx_timer == 0) { 1951 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 1952 wpi_stop(ifp, 1); 1953 ifp->if_oerrors++; 1954 return; 1955 } 1956 ifp->if_timer = 1; 1957 } 1958 1959 ieee80211_watchdog(ifp); 1960 } 1961 1962 int 1963 wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1964 { 1965 struct wpi_softc *sc = ifp->if_softc; 1966 struct ieee80211com *ic = &sc->sc_ic; 1967 int s, error = 0; 1968 1969 error = rw_enter(&sc->sc_rwlock, RW_WRITE | RW_INTR); 1970 if (error) 1971 return error; 1972 s = splnet(); 1973 1974 switch (cmd) { 1975 case SIOCSIFADDR: 1976 ifp->if_flags |= IFF_UP; 1977 /* FALLTHROUGH */ 1978 case SIOCSIFFLAGS: 1979 if (ifp->if_flags & IFF_UP) { 1980 if (!(ifp->if_flags & IFF_RUNNING)) 1981 error = wpi_init(ifp); 1982 } else { 1983 if (ifp->if_flags & IFF_RUNNING) 1984 wpi_stop(ifp, 1); 1985 } 1986 break; 1987 1988 case SIOCS80211POWER: 1989 error = ieee80211_ioctl(ifp, cmd, data); 1990 if (error != ENETRESET) 1991 break; 1992 if (ic->ic_state == IEEE80211_S_RUN) { 1993 if (ic->ic_flags & IEEE80211_F_PMGTON) 1994 error = wpi_set_pslevel(sc, 0, 3, 0); 1995 else /* back to CAM */ 1996 error = wpi_set_pslevel(sc, 0, 0, 0); 1997 } else { 1998 /* Defer until transition to IEEE80211_S_RUN. */ 1999 error = 0; 2000 } 2001 break; 2002 2003 default: 2004 error = ieee80211_ioctl(ifp, cmd, data); 2005 } 2006 2007 if (error == ENETRESET) { 2008 error = 0; 2009 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 2010 (IFF_UP | IFF_RUNNING)) { 2011 wpi_stop(ifp, 0); 2012 error = wpi_init(ifp); 2013 } 2014 } 2015 2016 splx(s); 2017 rw_exit_write(&sc->sc_rwlock); 2018 return error; 2019 } 2020 2021 /* 2022 * Send a command to the firmware. 2023 */ 2024 int 2025 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async) 2026 { 2027 struct wpi_tx_ring *ring = &sc->txq[4]; 2028 struct wpi_tx_desc *desc; 2029 struct wpi_tx_data *data; 2030 struct wpi_tx_cmd *cmd; 2031 struct mbuf *m; 2032 bus_addr_t paddr; 2033 int totlen, error; 2034 2035 desc = &ring->desc[ring->cur]; 2036 data = &ring->data[ring->cur]; 2037 totlen = 4 + size; 2038 2039 if (size > sizeof cmd->data) { 2040 /* Command is too large to fit in a descriptor. */ 2041 if (totlen > MCLBYTES) 2042 return EINVAL; 2043 MGETHDR(m, M_DONTWAIT, MT_DATA); 2044 if (m == NULL) 2045 return ENOMEM; 2046 if (totlen > MHLEN) { 2047 MCLGET(m, M_DONTWAIT); 2048 if (!(m->m_flags & M_EXT)) { 2049 m_freem(m); 2050 return ENOMEM; 2051 } 2052 } 2053 cmd = mtod(m, struct wpi_tx_cmd *); 2054 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, totlen, 2055 NULL, BUS_DMA_NOWAIT | BUS_DMA_WRITE); 2056 if (error != 0) { 2057 m_freem(m); 2058 return error; 2059 } 2060 data->m = m; 2061 paddr = data->map->dm_segs[0].ds_addr; 2062 } else { 2063 cmd = &ring->cmd[ring->cur]; 2064 paddr = data->cmd_paddr; 2065 } 2066 2067 cmd->code = code; 2068 cmd->flags = 0; 2069 cmd->qid = ring->qid; 2070 cmd->idx = ring->cur; 2071 memcpy(cmd->data, buf, size); 2072 2073 desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24); 2074 desc->segs[0].addr = htole32(paddr); 2075 desc->segs[0].len = htole32(totlen); 2076 2077 if (size > sizeof cmd->data) { 2078 bus_dmamap_sync(sc->sc_dmat, data->map, 0, totlen, 2079 BUS_DMASYNC_PREWRITE); 2080 } else { 2081 bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map, 2082 (caddr_t)cmd - ring->cmd_dma.vaddr, totlen, 2083 BUS_DMASYNC_PREWRITE); 2084 } 2085 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 2086 (caddr_t)desc - ring->desc_dma.vaddr, sizeof (*desc), 2087 BUS_DMASYNC_PREWRITE); 2088 2089 /* Kick command ring. */ 2090 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2091 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 2092 2093 return async ? 0 : tsleep_nsec(cmd, PCATCH, "wpicmd", SEC_TO_NSEC(1)); 2094 } 2095 2096 /* 2097 * Configure HW multi-rate retries. 2098 */ 2099 int 2100 wpi_mrr_setup(struct wpi_softc *sc) 2101 { 2102 struct ieee80211com *ic = &sc->sc_ic; 2103 struct wpi_mrr_setup mrr; 2104 int i, error; 2105 2106 /* CCK rates (not used with 802.11a). */ 2107 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) { 2108 mrr.rates[i].flags = 0; 2109 mrr.rates[i].plcp = wpi_rates[i].plcp; 2110 /* Fallback to the immediate lower CCK rate (if any.) */ 2111 mrr.rates[i].next = 2112 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1; 2113 /* Try one time at this rate before falling back to "next". */ 2114 mrr.rates[i].ntries = 1; 2115 } 2116 /* OFDM rates (not used with 802.11b). */ 2117 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) { 2118 mrr.rates[i].flags = 0; 2119 mrr.rates[i].plcp = wpi_rates[i].plcp; 2120 /* Fallback to the immediate lower rate (if any.) */ 2121 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */ 2122 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ? 2123 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 2124 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) : 2125 i - 1; 2126 /* Try one time at this rate before falling back to "next". */ 2127 mrr.rates[i].ntries = 1; 2128 } 2129 /* Setup MRR for control frames. */ 2130 mrr.which = htole32(WPI_MRR_CTL); 2131 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2132 if (error != 0) { 2133 printf("%s: could not setup MRR for control frames\n", 2134 sc->sc_dev.dv_xname); 2135 return error; 2136 } 2137 /* Setup MRR for data frames. */ 2138 mrr.which = htole32(WPI_MRR_DATA); 2139 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2140 if (error != 0) { 2141 printf("%s: could not setup MRR for data frames\n", 2142 sc->sc_dev.dv_xname); 2143 return error; 2144 } 2145 return 0; 2146 } 2147 2148 void 2149 wpi_updateedca(struct ieee80211com *ic) 2150 { 2151 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 2152 struct wpi_softc *sc = ic->ic_softc; 2153 struct wpi_edca_params cmd; 2154 int aci; 2155 2156 memset(&cmd, 0, sizeof cmd); 2157 cmd.flags = htole32(WPI_EDCA_UPDATE); 2158 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 2159 const struct ieee80211_edca_ac_params *ac = 2160 &ic->ic_edca_ac[aci]; 2161 cmd.ac[aci].aifsn = ac->ac_aifsn; 2162 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->ac_ecwmin)); 2163 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->ac_ecwmax)); 2164 cmd.ac[aci].txoplimit = 2165 htole16(IEEE80211_TXOP_TO_US(ac->ac_txoplimit)); 2166 } 2167 (void)wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1); 2168 #undef WPI_EXP2 2169 } 2170 2171 void 2172 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2173 { 2174 struct wpi_cmd_led led; 2175 2176 led.which = which; 2177 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2178 led.off = off; 2179 led.on = on; 2180 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 2181 } 2182 2183 int 2184 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni) 2185 { 2186 struct wpi_cmd_timing cmd; 2187 uint64_t val, mod; 2188 2189 memset(&cmd, 0, sizeof cmd); 2190 memcpy(&cmd.tstamp, ni->ni_tstamp, sizeof (uint64_t)); 2191 cmd.bintval = htole16(ni->ni_intval); 2192 cmd.lintval = htole16(10); 2193 2194 /* Compute remaining time until next beacon. */ 2195 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */ 2196 mod = letoh64(cmd.tstamp) % val; 2197 cmd.binitval = htole32((uint32_t)(val - mod)); 2198 2199 DPRINTF(("timing bintval=%u, tstamp=%llu, init=%u\n", 2200 ni->ni_intval, letoh64(cmd.tstamp), (uint32_t)(val - mod))); 2201 2202 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1); 2203 } 2204 2205 /* 2206 * This function is called periodically (every minute) to adjust TX power 2207 * based on temperature variation. 2208 */ 2209 void 2210 wpi_power_calibration(struct wpi_softc *sc) 2211 { 2212 int temp; 2213 2214 temp = (int)WPI_READ(sc, WPI_UCODE_GP2); 2215 /* Sanity-check temperature. */ 2216 if (temp < -260 || temp > 25) { 2217 /* This can't be correct, ignore. */ 2218 DPRINTF(("out-of-range temperature reported: %d\n", temp)); 2219 return; 2220 } 2221 DPRINTF(("temperature %d->%d\n", sc->temp, temp)); 2222 /* Adjust TX power if need be (delta > 6). */ 2223 if (abs(temp - sc->temp) > 6) { 2224 /* Record temperature of last calibration. */ 2225 sc->temp = temp; 2226 (void)wpi_set_txpower(sc, 1); 2227 } 2228 } 2229 2230 /* 2231 * Set TX power for current channel (each rate has its own power settings). 2232 */ 2233 int 2234 wpi_set_txpower(struct wpi_softc *sc, int async) 2235 { 2236 struct ieee80211com *ic = &sc->sc_ic; 2237 struct ieee80211_channel *ch; 2238 struct wpi_power_group *group; 2239 struct wpi_cmd_txpower cmd; 2240 u_int chan; 2241 int idx, i; 2242 2243 /* Retrieve current channel from last RXON. */ 2244 chan = sc->rxon.chan; 2245 DPRINTF(("setting TX power for channel %d\n", chan)); 2246 ch = &ic->ic_channels[chan]; 2247 2248 /* Find the TX power group to which this channel belongs. */ 2249 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 2250 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 2251 if (chan <= group->chan) 2252 break; 2253 } else 2254 group = &sc->groups[0]; 2255 2256 memset(&cmd, 0, sizeof cmd); 2257 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1; 2258 cmd.chan = htole16(chan); 2259 2260 /* Set TX power for all OFDM and CCK rates. */ 2261 for (i = 0; i <= WPI_RIDX_MAX ; i++) { 2262 /* Retrieve TX power for this channel/rate. */ 2263 idx = wpi_get_power_index(sc, group, ch, i); 2264 2265 cmd.rates[i].plcp = wpi_rates[i].plcp; 2266 2267 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 2268 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx]; 2269 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx]; 2270 } else { 2271 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx]; 2272 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx]; 2273 } 2274 DPRINTF(("chan %d/rate %d: power index %d\n", chan, 2275 wpi_rates[i].rate, idx)); 2276 } 2277 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async); 2278 } 2279 2280 /* 2281 * Determine TX power index for a given channel/rate combination. 2282 * This takes into account the regulatory information from EEPROM and the 2283 * current temperature. 2284 */ 2285 int 2286 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 2287 struct ieee80211_channel *c, int ridx) 2288 { 2289 /* Fixed-point arithmetic division using a n-bit fractional part. */ 2290 #define fdivround(a, b, n) \ 2291 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 2292 2293 /* Linear interpolation. */ 2294 #define interpolate(x, x1, y1, x2, y2, n) \ 2295 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 2296 2297 struct ieee80211com *ic = &sc->sc_ic; 2298 struct wpi_power_sample *sample; 2299 int pwr, idx; 2300 u_int chan; 2301 2302 /* Get channel number. */ 2303 chan = ieee80211_chan2ieee(ic, c); 2304 2305 /* Default TX power is group maximum TX power minus 3dB. */ 2306 pwr = group->maxpwr / 2; 2307 2308 /* Decrease TX power for highest OFDM rates to reduce distortion. */ 2309 switch (ridx) { 2310 case WPI_RIDX_OFDM36: 2311 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5; 2312 break; 2313 case WPI_RIDX_OFDM48: 2314 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10; 2315 break; 2316 case WPI_RIDX_OFDM54: 2317 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12; 2318 break; 2319 } 2320 2321 /* Never exceed the channel maximum allowed TX power. */ 2322 pwr = MIN(pwr, sc->maxpwr[chan]); 2323 2324 /* Retrieve TX power index into gain tables from samples. */ 2325 for (sample = group->samples; sample < &group->samples[3]; sample++) 2326 if (pwr > sample[1].power) 2327 break; 2328 /* Fixed-point linear interpolation using a 19-bit fractional part. */ 2329 idx = interpolate(pwr, sample[0].power, sample[0].index, 2330 sample[1].power, sample[1].index, 19); 2331 2332 /*- 2333 * Adjust power index based on current temperature: 2334 * - if cooler than factory-calibrated: decrease output power 2335 * - if warmer than factory-calibrated: increase output power 2336 */ 2337 idx -= (sc->temp - group->temp) * 11 / 100; 2338 2339 /* Decrease TX power for CCK rates (-5dB). */ 2340 if (ridx >= WPI_RIDX_CCK1) 2341 idx += 10; 2342 2343 /* Make sure idx stays in a valid range. */ 2344 if (idx < 0) 2345 idx = 0; 2346 else if (idx > WPI_MAX_PWR_INDEX) 2347 idx = WPI_MAX_PWR_INDEX; 2348 return idx; 2349 2350 #undef interpolate 2351 #undef fdivround 2352 } 2353 2354 /* 2355 * Set STA mode power saving level (between 0 and 5). 2356 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving. 2357 */ 2358 int 2359 wpi_set_pslevel(struct wpi_softc *sc, int dtim, int level, int async) 2360 { 2361 struct wpi_pmgt_cmd cmd; 2362 const struct wpi_pmgt *pmgt; 2363 uint32_t max, skip_dtim; 2364 pcireg_t reg; 2365 int i; 2366 2367 /* Select which PS parameters to use. */ 2368 if (dtim <= 10) 2369 pmgt = &wpi_pmgt[0][level]; 2370 else 2371 pmgt = &wpi_pmgt[1][level]; 2372 2373 memset(&cmd, 0, sizeof cmd); 2374 if (level != 0) /* not CAM */ 2375 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP); 2376 /* Retrieve PCIe Active State Power Management (ASPM). */ 2377 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 2378 sc->sc_cap_off + PCI_PCIE_LCSR); 2379 if (!(reg & PCI_PCIE_LCSR_ASPM_L0S)) /* L0s Entry disabled. */ 2380 cmd.flags |= htole16(WPI_PS_PCI_PMGT); 2381 cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024); 2382 cmd.txtimeout = htole32(pmgt->txtimeout * 1024); 2383 2384 if (dtim == 0) { 2385 dtim = 1; 2386 skip_dtim = 0; 2387 } else 2388 skip_dtim = pmgt->skip_dtim; 2389 if (skip_dtim != 0) { 2390 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM); 2391 max = pmgt->intval[4]; 2392 if (max == (uint32_t)-1) 2393 max = dtim * (skip_dtim + 1); 2394 else if (max > dtim) 2395 max = (max / dtim) * dtim; 2396 } else 2397 max = dtim; 2398 for (i = 0; i < 5; i++) 2399 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i])); 2400 2401 DPRINTF(("setting power saving level to %d\n", level)); 2402 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async); 2403 } 2404 2405 int 2406 wpi_config(struct wpi_softc *sc) 2407 { 2408 struct ieee80211com *ic = &sc->sc_ic; 2409 struct ifnet *ifp = &ic->ic_if; 2410 struct wpi_bluetooth bluetooth; 2411 struct wpi_node_info node; 2412 int error; 2413 2414 /* Set power saving level to CAM during initialization. */ 2415 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) { 2416 printf("%s: could not set power saving level\n", 2417 sc->sc_dev.dv_xname); 2418 return error; 2419 } 2420 2421 /* Configure bluetooth coexistence. */ 2422 memset(&bluetooth, 0, sizeof bluetooth); 2423 bluetooth.flags = WPI_BT_COEX_MODE_4WIRE; 2424 bluetooth.lead_time = WPI_BT_LEAD_TIME_DEF; 2425 bluetooth.max_kill = WPI_BT_MAX_KILL_DEF; 2426 error = wpi_cmd(sc, WPI_CMD_BT_COEX, &bluetooth, sizeof bluetooth, 0); 2427 if (error != 0) { 2428 printf("%s: could not configure bluetooth coexistence\n", 2429 sc->sc_dev.dv_xname); 2430 return error; 2431 } 2432 2433 /* Configure adapter. */ 2434 memset(&sc->rxon, 0, sizeof (struct wpi_rxon)); 2435 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 2436 IEEE80211_ADDR_COPY(sc->rxon.myaddr, ic->ic_myaddr); 2437 /* Set default channel. */ 2438 sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan); 2439 sc->rxon.flags = htole32(WPI_RXON_TSF); 2440 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) 2441 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 2442 switch (ic->ic_opmode) { 2443 case IEEE80211_M_STA: 2444 sc->rxon.mode = WPI_MODE_STA; 2445 sc->rxon.filter = htole32(WPI_FILTER_MULTICAST); 2446 break; 2447 case IEEE80211_M_MONITOR: 2448 sc->rxon.mode = WPI_MODE_MONITOR; 2449 sc->rxon.filter = htole32(WPI_FILTER_MULTICAST | 2450 WPI_FILTER_CTL | WPI_FILTER_PROMISC); 2451 break; 2452 default: 2453 /* Should not get there. */ 2454 break; 2455 } 2456 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */ 2457 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */ 2458 DPRINTF(("setting configuration\n")); 2459 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon), 2460 0); 2461 if (error != 0) { 2462 printf("%s: RXON command failed\n", sc->sc_dev.dv_xname); 2463 return error; 2464 } 2465 2466 /* Configuration has changed, set TX power accordingly. */ 2467 if ((error = wpi_set_txpower(sc, 0)) != 0) { 2468 printf("%s: could not set TX power\n", sc->sc_dev.dv_xname); 2469 return error; 2470 } 2471 2472 /* Add broadcast node. */ 2473 memset(&node, 0, sizeof node); 2474 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr); 2475 node.id = WPI_ID_BROADCAST; 2476 node.plcp = wpi_rates[WPI_RIDX_CCK1].plcp; 2477 node.action = htole32(WPI_ACTION_SET_RATE); 2478 node.antenna = WPI_ANTENNA_BOTH; 2479 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0); 2480 if (error != 0) { 2481 printf("%s: could not add broadcast node\n", 2482 sc->sc_dev.dv_xname); 2483 return error; 2484 } 2485 2486 if ((error = wpi_mrr_setup(sc)) != 0) { 2487 printf("%s: could not setup MRR\n", sc->sc_dev.dv_xname); 2488 return error; 2489 } 2490 return 0; 2491 } 2492 2493 int 2494 wpi_scan(struct wpi_softc *sc, uint16_t flags) 2495 { 2496 struct ieee80211com *ic = &sc->sc_ic; 2497 struct wpi_scan_hdr *hdr; 2498 struct wpi_cmd_data *tx; 2499 struct wpi_scan_essid *essid; 2500 struct wpi_scan_chan *chan; 2501 struct ieee80211_frame *wh; 2502 struct ieee80211_rateset *rs; 2503 struct ieee80211_channel *c; 2504 uint8_t *buf, *frm; 2505 int buflen, error; 2506 2507 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO); 2508 if (buf == NULL) { 2509 printf("%s: could not allocate buffer for scan command\n", 2510 sc->sc_dev.dv_xname); 2511 return ENOMEM; 2512 } 2513 hdr = (struct wpi_scan_hdr *)buf; 2514 /* 2515 * Move to the next channel if no frames are received within 10ms 2516 * after sending the probe request. 2517 */ 2518 hdr->quiet_time = htole16(10); /* timeout in milliseconds */ 2519 hdr->quiet_threshold = htole16(1); /* min # of packets */ 2520 2521 tx = (struct wpi_cmd_data *)(hdr + 1); 2522 tx->flags = htole32(WPI_TX_AUTO_SEQ); 2523 tx->id = WPI_ID_BROADCAST; 2524 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 2525 2526 if (flags & IEEE80211_CHAN_5GHZ) { 2527 hdr->crc_threshold = htole16(1); 2528 /* Send probe requests at 6Mbps. */ 2529 tx->plcp = wpi_rates[WPI_RIDX_OFDM6].plcp; 2530 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A]; 2531 } else { 2532 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO); 2533 /* Send probe requests at 1Mbps. */ 2534 tx->plcp = wpi_rates[WPI_RIDX_CCK1].plcp; 2535 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 2536 } 2537 2538 essid = (struct wpi_scan_essid *)(tx + 1); 2539 if (ic->ic_des_esslen != 0) { 2540 essid[0].id = IEEE80211_ELEMID_SSID; 2541 essid[0].len = ic->ic_des_esslen; 2542 memcpy(essid[0].data, ic->ic_des_essid, ic->ic_des_esslen); 2543 } 2544 /* 2545 * Build a probe request frame. Most of the following code is a 2546 * copy & paste of what is done in net80211. 2547 */ 2548 wh = (struct ieee80211_frame *)(essid + 4); 2549 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2550 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 2551 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2552 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr); 2553 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 2554 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr); 2555 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */ 2556 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */ 2557 2558 frm = (uint8_t *)(wh + 1); 2559 frm = ieee80211_add_ssid(frm, NULL, 0); 2560 frm = ieee80211_add_rates(frm, rs); 2561 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 2562 frm = ieee80211_add_xrates(frm, rs); 2563 2564 /* Set length of probe request. */ 2565 tx->len = htole16(frm - (uint8_t *)wh); 2566 2567 chan = (struct wpi_scan_chan *)frm; 2568 for (c = &ic->ic_channels[1]; 2569 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) { 2570 if ((c->ic_flags & flags) != flags) 2571 continue; 2572 2573 chan->chan = ieee80211_chan2ieee(ic, c); 2574 DPRINTFN(2, ("adding channel %d\n", chan->chan)); 2575 chan->flags = 0; 2576 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) 2577 chan->flags |= WPI_CHAN_ACTIVE; 2578 if (ic->ic_des_esslen != 0) 2579 chan->flags |= WPI_CHAN_NPBREQS(1); 2580 chan->dsp_gain = 0x6e; 2581 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2582 chan->rf_gain = 0x3b; 2583 chan->active = htole16(24); 2584 chan->passive = htole16(110); 2585 } else { 2586 chan->rf_gain = 0x28; 2587 chan->active = htole16(36); 2588 chan->passive = htole16(120); 2589 } 2590 hdr->nchan++; 2591 chan++; 2592 } 2593 2594 buflen = (uint8_t *)chan - buf; 2595 hdr->len = htole16(buflen); 2596 2597 DPRINTF(("sending scan command nchan=%d\n", hdr->nchan)); 2598 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1); 2599 free(buf, M_DEVBUF, WPI_SCAN_MAXSZ); 2600 return error; 2601 } 2602 2603 int 2604 wpi_auth(struct wpi_softc *sc) 2605 { 2606 struct ieee80211com *ic = &sc->sc_ic; 2607 struct ieee80211_node *ni = ic->ic_bss; 2608 struct wpi_node_info node; 2609 int error; 2610 2611 /* Update adapter configuration. */ 2612 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 2613 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2614 sc->rxon.flags = htole32(WPI_RXON_TSF); 2615 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2616 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ); 2617 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2618 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 2619 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 2620 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 2621 switch (ic->ic_curmode) { 2622 case IEEE80211_MODE_11A: 2623 sc->rxon.cck_mask = 0; 2624 sc->rxon.ofdm_mask = 0x15; 2625 break; 2626 case IEEE80211_MODE_11B: 2627 sc->rxon.cck_mask = 0x03; 2628 sc->rxon.ofdm_mask = 0; 2629 break; 2630 default: /* Assume 802.11b/g. */ 2631 sc->rxon.cck_mask = 0x0f; 2632 sc->rxon.ofdm_mask = 0x15; 2633 } 2634 DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan, 2635 sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask)); 2636 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon), 2637 1); 2638 if (error != 0) { 2639 printf("%s: RXON command failed\n", sc->sc_dev.dv_xname); 2640 return error; 2641 } 2642 2643 /* Configuration has changed, set TX power accordingly. */ 2644 if ((error = wpi_set_txpower(sc, 1)) != 0) { 2645 printf("%s: could not set TX power\n", sc->sc_dev.dv_xname); 2646 return error; 2647 } 2648 /* 2649 * Reconfiguring RXON clears the firmware nodes table so we must 2650 * add the broadcast node again. 2651 */ 2652 memset(&node, 0, sizeof node); 2653 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr); 2654 node.id = WPI_ID_BROADCAST; 2655 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2656 wpi_rates[WPI_RIDX_OFDM6].plcp : wpi_rates[WPI_RIDX_CCK1].plcp; 2657 node.action = htole32(WPI_ACTION_SET_RATE); 2658 node.antenna = WPI_ANTENNA_BOTH; 2659 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2660 if (error != 0) { 2661 printf("%s: could not add broadcast node\n", 2662 sc->sc_dev.dv_xname); 2663 return error; 2664 } 2665 return 0; 2666 } 2667 2668 int 2669 wpi_run(struct wpi_softc *sc) 2670 { 2671 struct ieee80211com *ic = &sc->sc_ic; 2672 struct ieee80211_node *ni = ic->ic_bss; 2673 struct wpi_node_info node; 2674 int error; 2675 2676 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 2677 /* Link LED blinks while monitoring. */ 2678 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 2679 return 0; 2680 } 2681 if ((error = wpi_set_timing(sc, ni)) != 0) { 2682 printf("%s: could not set timing\n", sc->sc_dev.dv_xname); 2683 return error; 2684 } 2685 2686 /* Update adapter configuration. */ 2687 sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd)); 2688 /* Short preamble and slot time are negotiated when associating. */ 2689 sc->rxon.flags &= ~htole32(WPI_RXON_SHPREAMBLE | WPI_RXON_SHSLOT); 2690 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2691 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT); 2692 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 2693 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE); 2694 sc->rxon.filter |= htole32(WPI_FILTER_BSS); 2695 DPRINTF(("rxon chan %d flags %x\n", sc->rxon.chan, sc->rxon.flags)); 2696 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon), 2697 1); 2698 if (error != 0) { 2699 printf("%s: RXON command failed\n", sc->sc_dev.dv_xname); 2700 return error; 2701 } 2702 2703 /* Configuration has changed, set TX power accordingly. */ 2704 if ((error = wpi_set_txpower(sc, 1)) != 0) { 2705 printf("%s: could not set TX power\n", sc->sc_dev.dv_xname); 2706 return error; 2707 } 2708 2709 /* Fake a join to init the TX rate. */ 2710 ((struct wpi_node *)ni)->id = WPI_ID_BSS; 2711 wpi_newassoc(ic, ni, 1); 2712 2713 /* Add BSS node. */ 2714 memset(&node, 0, sizeof node); 2715 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_bssid); 2716 node.id = WPI_ID_BSS; 2717 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2718 wpi_rates[WPI_RIDX_OFDM6].plcp : wpi_rates[WPI_RIDX_CCK1].plcp; 2719 node.action = htole32(WPI_ACTION_SET_RATE); 2720 node.antenna = WPI_ANTENNA_BOTH; 2721 DPRINTF(("adding BSS node\n")); 2722 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2723 if (error != 0) { 2724 printf("%s: could not add BSS node\n", sc->sc_dev.dv_xname); 2725 return error; 2726 } 2727 2728 /* Start periodic calibration timer. */ 2729 sc->calib_cnt = 0; 2730 timeout_add_msec(&sc->calib_to, 500); 2731 2732 /* Link LED always on while associated. */ 2733 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 2734 2735 /* Enable power-saving mode if requested by user. */ 2736 if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON) 2737 (void)wpi_set_pslevel(sc, 0, 3, 1); 2738 2739 return 0; 2740 } 2741 2742 /* 2743 * We support CCMP hardware encryption/decryption of unicast frames only. 2744 * HW support for TKIP really sucks. We should let TKIP die anyway. 2745 */ 2746 int 2747 wpi_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 2748 struct ieee80211_key *k) 2749 { 2750 struct wpi_softc *sc = ic->ic_softc; 2751 struct wpi_node *wn = (void *)ni; 2752 struct wpi_node_info node; 2753 uint16_t kflags; 2754 2755 if ((k->k_flags & IEEE80211_KEY_GROUP) || 2756 k->k_cipher != IEEE80211_CIPHER_CCMP) 2757 return ieee80211_set_key(ic, ni, k); 2758 2759 kflags = WPI_KFLAG_CCMP | WPI_KFLAG_KID(k->k_id); 2760 memset(&node, 0, sizeof node); 2761 node.id = wn->id; 2762 node.control = WPI_NODE_UPDATE; 2763 node.flags = WPI_FLAG_SET_KEY; 2764 node.kflags = htole16(kflags); 2765 memcpy(node.key, k->k_key, k->k_len); 2766 DPRINTF(("set key id=%d for node %d\n", k->k_id, node.id)); 2767 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2768 } 2769 2770 void 2771 wpi_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni, 2772 struct ieee80211_key *k) 2773 { 2774 struct wpi_softc *sc = ic->ic_softc; 2775 struct wpi_node *wn = (void *)ni; 2776 struct wpi_node_info node; 2777 2778 if ((k->k_flags & IEEE80211_KEY_GROUP) || 2779 k->k_cipher != IEEE80211_CIPHER_CCMP) { 2780 /* See comment about other ciphers above. */ 2781 ieee80211_delete_key(ic, ni, k); 2782 return; 2783 } 2784 if (ic->ic_state != IEEE80211_S_RUN) 2785 return; /* Nothing to do. */ 2786 memset(&node, 0, sizeof node); 2787 node.id = wn->id; 2788 node.control = WPI_NODE_UPDATE; 2789 node.flags = WPI_FLAG_SET_KEY; 2790 node.kflags = 0; 2791 DPRINTF(("delete keys for node %d\n", node.id)); 2792 (void)wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2793 } 2794 2795 int 2796 wpi_post_alive(struct wpi_softc *sc) 2797 { 2798 int ntries, error; 2799 2800 /* Check (again) that the radio is not disabled. */ 2801 if ((error = wpi_nic_lock(sc)) != 0) 2802 return error; 2803 /* NB: Runtime firmware must be up and running. */ 2804 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) { 2805 printf("%s: radio is disabled by hardware switch\n", 2806 sc->sc_dev.dv_xname); 2807 wpi_nic_unlock(sc); 2808 return EPERM; /* :-) */ 2809 } 2810 wpi_nic_unlock(sc); 2811 2812 /* Wait for thermal sensor to calibrate. */ 2813 for (ntries = 0; ntries < 1000; ntries++) { 2814 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0) 2815 break; 2816 DELAY(10); 2817 } 2818 if (ntries == 1000) { 2819 printf("%s: timeout waiting for thermal sensor calibration\n", 2820 sc->sc_dev.dv_xname); 2821 return ETIMEDOUT; 2822 } 2823 DPRINTF(("temperature %d\n", sc->temp)); 2824 return 0; 2825 } 2826 2827 /* 2828 * The firmware boot code is small and is intended to be copied directly into 2829 * the NIC internal memory (no DMA transfer.) 2830 */ 2831 int 2832 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size) 2833 { 2834 int error, ntries; 2835 2836 size /= sizeof (uint32_t); 2837 2838 if ((error = wpi_nic_lock(sc)) != 0) 2839 return error; 2840 2841 /* Copy microcode image into NIC memory. */ 2842 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE, 2843 (const uint32_t *)ucode, size); 2844 2845 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0); 2846 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE); 2847 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size); 2848 2849 /* Start boot load now. */ 2850 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START); 2851 2852 /* Wait for transfer to complete. */ 2853 for (ntries = 0; ntries < 1000; ntries++) { 2854 if (!(wpi_prph_read(sc, WPI_BSM_WR_CTRL) & 2855 WPI_BSM_WR_CTRL_START)) 2856 break; 2857 DELAY(10); 2858 } 2859 if (ntries == 1000) { 2860 printf("%s: could not load boot firmware\n", 2861 sc->sc_dev.dv_xname); 2862 wpi_nic_unlock(sc); 2863 return ETIMEDOUT; 2864 } 2865 2866 /* Enable boot after power up. */ 2867 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN); 2868 2869 wpi_nic_unlock(sc); 2870 return 0; 2871 } 2872 2873 int 2874 wpi_load_firmware(struct wpi_softc *sc) 2875 { 2876 struct wpi_fw_info *fw = &sc->fw; 2877 struct wpi_dma_info *dma = &sc->fw_dma; 2878 int error; 2879 2880 /* Copy initialization sections into pre-allocated DMA-safe memory. */ 2881 memcpy(dma->vaddr, fw->init.data, fw->init.datasz); 2882 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->init.datasz, 2883 BUS_DMASYNC_PREWRITE); 2884 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, 2885 fw->init.text, fw->init.textsz); 2886 bus_dmamap_sync(sc->sc_dmat, dma->map, WPI_FW_DATA_MAXSZ, 2887 fw->init.textsz, BUS_DMASYNC_PREWRITE); 2888 2889 /* Tell adapter where to find initialization sections. */ 2890 if ((error = wpi_nic_lock(sc)) != 0) 2891 return error; 2892 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 2893 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz); 2894 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 2895 dma->paddr + WPI_FW_DATA_MAXSZ); 2896 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz); 2897 wpi_nic_unlock(sc); 2898 2899 /* Load firmware boot code. */ 2900 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz); 2901 if (error != 0) { 2902 printf("%s: could not load boot firmware\n", 2903 sc->sc_dev.dv_xname); 2904 return error; 2905 } 2906 /* Now press "execute". */ 2907 WPI_WRITE(sc, WPI_RESET, 0); 2908 2909 /* Wait at most one second for first alive notification. */ 2910 if ((error = tsleep_nsec(sc, PCATCH, "wpiinit", SEC_TO_NSEC(1))) != 0) { 2911 printf("%s: timeout waiting for adapter to initialize\n", 2912 sc->sc_dev.dv_xname); 2913 return error; 2914 } 2915 2916 /* Copy runtime sections into pre-allocated DMA-safe memory. */ 2917 memcpy(dma->vaddr, fw->main.data, fw->main.datasz); 2918 bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->main.datasz, 2919 BUS_DMASYNC_PREWRITE); 2920 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, 2921 fw->main.text, fw->main.textsz); 2922 bus_dmamap_sync(sc->sc_dmat, dma->map, WPI_FW_DATA_MAXSZ, 2923 fw->main.textsz, BUS_DMASYNC_PREWRITE); 2924 2925 /* Tell adapter where to find runtime sections. */ 2926 if ((error = wpi_nic_lock(sc)) != 0) 2927 return error; 2928 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr); 2929 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz); 2930 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR, 2931 dma->paddr + WPI_FW_DATA_MAXSZ); 2932 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, 2933 WPI_FW_UPDATED | fw->main.textsz); 2934 wpi_nic_unlock(sc); 2935 2936 return 0; 2937 } 2938 2939 int 2940 wpi_read_firmware(struct wpi_softc *sc) 2941 { 2942 struct wpi_fw_info *fw = &sc->fw; 2943 const struct wpi_firmware_hdr *hdr; 2944 int error; 2945 2946 /* Read firmware image from filesystem. */ 2947 if ((error = loadfirmware("wpi-3945abg", &fw->data, &fw->datalen)) != 0) { 2948 printf("%s: error, %d, could not read firmware %s\n", 2949 sc->sc_dev.dv_xname, error, "wpi-3945abg"); 2950 return error; 2951 } 2952 if (fw->datalen < sizeof (*hdr)) { 2953 printf("%s: truncated firmware header: %zu bytes\n", 2954 sc->sc_dev.dv_xname, fw->datalen); 2955 free(fw->data, M_DEVBUF, fw->datalen); 2956 return EINVAL; 2957 } 2958 /* Extract firmware header information. */ 2959 hdr = (struct wpi_firmware_hdr *)fw->data; 2960 fw->main.textsz = letoh32(hdr->main_textsz); 2961 fw->main.datasz = letoh32(hdr->main_datasz); 2962 fw->init.textsz = letoh32(hdr->init_textsz); 2963 fw->init.datasz = letoh32(hdr->init_datasz); 2964 fw->boot.textsz = letoh32(hdr->boot_textsz); 2965 fw->boot.datasz = 0; 2966 2967 /* Sanity-check firmware header. */ 2968 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ || 2969 fw->main.datasz > WPI_FW_DATA_MAXSZ || 2970 fw->init.textsz > WPI_FW_TEXT_MAXSZ || 2971 fw->init.datasz > WPI_FW_DATA_MAXSZ || 2972 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ || 2973 (fw->boot.textsz & 3) != 0) { 2974 printf("%s: invalid firmware header\n", sc->sc_dev.dv_xname); 2975 free(fw->data, M_DEVBUF, fw->datalen); 2976 return EINVAL; 2977 } 2978 2979 /* Check that all firmware sections fit. */ 2980 if (fw->datalen < sizeof (*hdr) + fw->main.textsz + fw->main.datasz + 2981 fw->init.textsz + fw->init.datasz + fw->boot.textsz) { 2982 printf("%s: firmware file too short: %zu bytes\n", 2983 sc->sc_dev.dv_xname, fw->datalen); 2984 free(fw->data, M_DEVBUF, fw->datalen); 2985 return EINVAL; 2986 } 2987 2988 /* Get pointers to firmware sections. */ 2989 fw->main.text = (const uint8_t *)(hdr + 1); 2990 fw->main.data = fw->main.text + fw->main.textsz; 2991 fw->init.text = fw->main.data + fw->main.datasz; 2992 fw->init.data = fw->init.text + fw->init.textsz; 2993 fw->boot.text = fw->init.data + fw->init.datasz; 2994 2995 return 0; 2996 } 2997 2998 int 2999 wpi_clock_wait(struct wpi_softc *sc) 3000 { 3001 int ntries; 3002 3003 /* Set "initialization complete" bit. */ 3004 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE); 3005 3006 /* Wait for clock stabilization. */ 3007 for (ntries = 0; ntries < 25000; ntries++) { 3008 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY) 3009 return 0; 3010 DELAY(100); 3011 } 3012 printf("%s: timeout waiting for clock stabilization\n", 3013 sc->sc_dev.dv_xname); 3014 return ETIMEDOUT; 3015 } 3016 3017 int 3018 wpi_apm_init(struct wpi_softc *sc) 3019 { 3020 int error; 3021 3022 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT); 3023 /* Disable L0s. */ 3024 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX); 3025 3026 if ((error = wpi_clock_wait(sc)) != 0) 3027 return error; 3028 3029 if ((error = wpi_nic_lock(sc)) != 0) 3030 return error; 3031 /* Enable DMA. */ 3032 wpi_prph_write(sc, WPI_APMG_CLK_ENA, 3033 WPI_APMG_CLK_DMA_CLK_RQT | WPI_APMG_CLK_BSM_CLK_RQT); 3034 DELAY(20); 3035 /* Disable L1. */ 3036 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS); 3037 wpi_nic_unlock(sc); 3038 3039 return 0; 3040 } 3041 3042 void 3043 wpi_apm_stop_master(struct wpi_softc *sc) 3044 { 3045 int ntries; 3046 3047 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER); 3048 3049 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) == 3050 WPI_GP_CNTRL_MAC_PS) 3051 return; /* Already asleep. */ 3052 3053 for (ntries = 0; ntries < 100; ntries++) { 3054 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED) 3055 return; 3056 DELAY(10); 3057 } 3058 printf("%s: timeout waiting for master\n", sc->sc_dev.dv_xname); 3059 } 3060 3061 void 3062 wpi_apm_stop(struct wpi_softc *sc) 3063 { 3064 wpi_apm_stop_master(sc); 3065 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW); 3066 } 3067 3068 void 3069 wpi_nic_config(struct wpi_softc *sc) 3070 { 3071 pcireg_t reg; 3072 uint8_t rev; 3073 3074 /* Voodoo from the reference driver. */ 3075 reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG); 3076 rev = PCI_REVISION(reg); 3077 if ((rev & 0xc0) == 0x40) 3078 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB); 3079 else if (!(rev & 0x80)) 3080 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM); 3081 3082 if (sc->cap == 0x80) 3083 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC); 3084 3085 if ((letoh16(sc->rev) & 0xf0) == 0xd0) 3086 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 3087 else 3088 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D); 3089 3090 if (sc->type > 1) 3091 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B); 3092 } 3093 3094 int 3095 wpi_hw_init(struct wpi_softc *sc) 3096 { 3097 int chnl, ntries, error; 3098 3099 /* Clear pending interrupts. */ 3100 WPI_WRITE(sc, WPI_INT, 0xffffffff); 3101 3102 if ((error = wpi_apm_init(sc)) != 0) { 3103 printf("%s: could not power ON adapter\n", 3104 sc->sc_dev.dv_xname); 3105 return error; 3106 } 3107 3108 /* Select VMAIN power source. */ 3109 if ((error = wpi_nic_lock(sc)) != 0) 3110 return error; 3111 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK); 3112 wpi_nic_unlock(sc); 3113 /* Spin until VMAIN gets selected. */ 3114 for (ntries = 0; ntries < 5000; ntries++) { 3115 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN) 3116 break; 3117 DELAY(10); 3118 } 3119 if (ntries == 5000) { 3120 printf("%s: timeout selecting power source\n", 3121 sc->sc_dev.dv_xname); 3122 return ETIMEDOUT; 3123 } 3124 3125 /* Perform adapter initialization. */ 3126 (void)wpi_nic_config(sc); 3127 3128 /* Initialize RX ring. */ 3129 if ((error = wpi_nic_lock(sc)) != 0) 3130 return error; 3131 /* Set physical address of RX ring. */ 3132 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr); 3133 /* Set physical address of RX read pointer. */ 3134 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr + 3135 offsetof(struct wpi_shared, next)); 3136 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0); 3137 /* Enable RX. */ 3138 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 3139 WPI_FH_RX_CONFIG_DMA_ENA | 3140 WPI_FH_RX_CONFIG_RDRBD_ENA | 3141 WPI_FH_RX_CONFIG_WRSTATUS_ENA | 3142 WPI_FH_RX_CONFIG_MAXFRAG | 3143 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) | 3144 WPI_FH_RX_CONFIG_IRQ_DST_HOST | 3145 WPI_FH_RX_CONFIG_IRQ_RBTH(1)); 3146 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */ 3147 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7); 3148 wpi_nic_unlock(sc); 3149 3150 /* Initialize TX rings. */ 3151 if ((error = wpi_nic_lock(sc)) != 0) 3152 return error; 3153 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */ 3154 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */ 3155 /* Enable all 6 TX rings. */ 3156 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f); 3157 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000); 3158 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002); 3159 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4); 3160 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5); 3161 /* Set physical address of TX rings. */ 3162 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr); 3163 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5); 3164 3165 /* Enable all DMA channels. */ 3166 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 3167 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0); 3168 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0); 3169 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008); 3170 } 3171 wpi_nic_unlock(sc); 3172 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */ 3173 3174 /* Clear "radio off" and "commands blocked" bits. */ 3175 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 3176 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED); 3177 3178 /* Clear pending interrupts. */ 3179 WPI_WRITE(sc, WPI_INT, 0xffffffff); 3180 /* Enable interrupts. */ 3181 WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK); 3182 3183 /* _Really_ make sure "radio off" bit is cleared! */ 3184 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 3185 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL); 3186 3187 if ((error = wpi_load_firmware(sc)) != 0) { 3188 printf("%s: could not load firmware\n", sc->sc_dev.dv_xname); 3189 return error; 3190 } 3191 /* Wait at most one second for firmware alive notification. */ 3192 if ((error = tsleep_nsec(sc, PCATCH, "wpiinit", SEC_TO_NSEC(1))) != 0) { 3193 printf("%s: timeout waiting for adapter to initialize\n", 3194 sc->sc_dev.dv_xname); 3195 return error; 3196 } 3197 /* Do post-firmware initialization. */ 3198 return wpi_post_alive(sc); 3199 } 3200 3201 void 3202 wpi_hw_stop(struct wpi_softc *sc) 3203 { 3204 int chnl, qid, ntries; 3205 uint32_t tmp; 3206 3207 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO); 3208 3209 /* Disable interrupts. */ 3210 WPI_WRITE(sc, WPI_MASK, 0); 3211 WPI_WRITE(sc, WPI_INT, 0xffffffff); 3212 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff); 3213 3214 /* Make sure we no longer hold the NIC lock. */ 3215 wpi_nic_unlock(sc); 3216 3217 if (wpi_nic_lock(sc) == 0) { 3218 /* Stop TX scheduler. */ 3219 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0); 3220 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0); 3221 3222 /* Stop all DMA channels. */ 3223 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) { 3224 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0); 3225 for (ntries = 0; ntries < 100; ntries++) { 3226 tmp = WPI_READ(sc, WPI_FH_TX_STATUS); 3227 if ((tmp & WPI_FH_TX_STATUS_IDLE(chnl)) == 3228 WPI_FH_TX_STATUS_IDLE(chnl)) 3229 break; 3230 DELAY(10); 3231 } 3232 } 3233 wpi_nic_unlock(sc); 3234 } 3235 3236 /* Stop RX ring. */ 3237 wpi_reset_rx_ring(sc, &sc->rxq); 3238 3239 /* Reset all TX rings. */ 3240 for (qid = 0; qid < WPI_NTXQUEUES; qid++) 3241 wpi_reset_tx_ring(sc, &sc->txq[qid]); 3242 3243 if (wpi_nic_lock(sc) == 0) { 3244 wpi_prph_write(sc, WPI_APMG_CLK_DIS, WPI_APMG_CLK_DMA_CLK_RQT); 3245 wpi_nic_unlock(sc); 3246 } 3247 DELAY(5); 3248 /* Power OFF adapter. */ 3249 wpi_apm_stop(sc); 3250 } 3251 3252 int 3253 wpi_init(struct ifnet *ifp) 3254 { 3255 struct wpi_softc *sc = ifp->if_softc; 3256 struct ieee80211com *ic = &sc->sc_ic; 3257 int error; 3258 3259 #ifdef notyet 3260 /* Check that the radio is not disabled by hardware switch. */ 3261 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) { 3262 printf("%s: radio is disabled by hardware switch\n", 3263 sc->sc_dev.dv_xname); 3264 error = EPERM; /* :-) */ 3265 goto fail; 3266 } 3267 #endif 3268 /* Read firmware images from the filesystem. */ 3269 if ((error = wpi_read_firmware(sc)) != 0) { 3270 printf("%s: could not read firmware\n", sc->sc_dev.dv_xname); 3271 goto fail; 3272 } 3273 3274 /* Initialize hardware and upload firmware. */ 3275 error = wpi_hw_init(sc); 3276 free(sc->fw.data, M_DEVBUF, sc->fw.datalen); 3277 if (error != 0) { 3278 printf("%s: could not initialize hardware\n", 3279 sc->sc_dev.dv_xname); 3280 goto fail; 3281 } 3282 3283 /* Configure adapter now that it is ready. */ 3284 if ((error = wpi_config(sc)) != 0) { 3285 printf("%s: could not configure device\n", 3286 sc->sc_dev.dv_xname); 3287 goto fail; 3288 } 3289 3290 ifq_clr_oactive(&ifp->if_snd); 3291 ifp->if_flags |= IFF_RUNNING; 3292 3293 if (ic->ic_opmode != IEEE80211_M_MONITOR) 3294 ieee80211_begin_scan(ifp); 3295 else 3296 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3297 3298 return 0; 3299 3300 fail: wpi_stop(ifp, 1); 3301 return error; 3302 } 3303 3304 void 3305 wpi_stop(struct ifnet *ifp, int disable) 3306 { 3307 struct wpi_softc *sc = ifp->if_softc; 3308 struct ieee80211com *ic = &sc->sc_ic; 3309 3310 ifp->if_timer = sc->sc_tx_timer = 0; 3311 ifp->if_flags &= ~IFF_RUNNING; 3312 ifq_clr_oactive(&ifp->if_snd); 3313 3314 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3315 3316 /* Power OFF hardware. */ 3317 wpi_hw_stop(sc); 3318 } 3319