1 /* $NetBSD: if_wpi.c,v 1.75 2016/12/08 01:12:01 ozaki-r Exp $ */ 2 3 /*- 4 * Copyright (c) 2006, 2007 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 #include <sys/cdefs.h> 21 __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.75 2016/12/08 01:12:01 ozaki-r Exp $"); 22 23 /* 24 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters. 25 */ 26 27 28 #include <sys/param.h> 29 #include <sys/sockio.h> 30 #include <sys/sysctl.h> 31 #include <sys/mbuf.h> 32 #include <sys/kernel.h> 33 #include <sys/socket.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/mutex.h> 37 #include <sys/once.h> 38 #include <sys/conf.h> 39 #include <sys/kauth.h> 40 #include <sys/callout.h> 41 #include <sys/proc.h> 42 #include <sys/kthread.h> 43 44 #include <sys/bus.h> 45 #include <machine/endian.h> 46 #include <sys/intr.h> 47 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pcidevs.h> 51 52 #include <dev/sysmon/sysmonvar.h> 53 54 #include <net/bpf.h> 55 #include <net/if.h> 56 #include <net/if_arp.h> 57 #include <net/if_dl.h> 58 #include <net/if_ether.h> 59 #include <net/if_media.h> 60 #include <net/if_types.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/in_var.h> 65 #include <netinet/ip.h> 66 67 #include <net80211/ieee80211_var.h> 68 #include <net80211/ieee80211_amrr.h> 69 #include <net80211/ieee80211_radiotap.h> 70 71 #include <dev/firmload.h> 72 73 #include <dev/pci/if_wpireg.h> 74 #include <dev/pci/if_wpivar.h> 75 76 static const char wpi_firmware_name[] = "iwlwifi-3945.ucode"; 77 static once_t wpi_firmware_init; 78 static kmutex_t wpi_firmware_mutex; 79 static size_t wpi_firmware_users; 80 static uint8_t *wpi_firmware_image; 81 static size_t wpi_firmware_size; 82 83 static int wpi_match(device_t, cfdata_t, void *); 84 static void wpi_attach(device_t, device_t, void *); 85 static int wpi_detach(device_t , int); 86 static int wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *, 87 void **, bus_size_t, bus_size_t, int); 88 static void wpi_dma_contig_free(struct wpi_dma_info *); 89 static int wpi_alloc_shared(struct wpi_softc *); 90 static void wpi_free_shared(struct wpi_softc *); 91 static int wpi_alloc_fwmem(struct wpi_softc *); 92 static void wpi_free_fwmem(struct wpi_softc *); 93 static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *); 94 static void wpi_free_rbuf(struct mbuf *, void *, size_t, void *); 95 static int wpi_alloc_rpool(struct wpi_softc *); 96 static void wpi_free_rpool(struct wpi_softc *); 97 static int wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 98 static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 99 static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 100 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, 101 int, int); 102 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 103 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 104 static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *); 105 static void wpi_newassoc(struct ieee80211_node *, int); 106 static int wpi_media_change(struct ifnet *); 107 static int wpi_newstate(struct ieee80211com *, enum ieee80211_state, int); 108 static void wpi_mem_lock(struct wpi_softc *); 109 static void wpi_mem_unlock(struct wpi_softc *); 110 static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t); 111 static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t); 112 static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t, 113 const uint32_t *, int); 114 static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int); 115 static int wpi_load_microcode(struct wpi_softc *, const uint8_t *, int); 116 static int wpi_cache_firmware(struct wpi_softc *); 117 static void wpi_release_firmware(void); 118 static int wpi_load_firmware(struct wpi_softc *); 119 static void wpi_calib_timeout(void *); 120 static void wpi_iter_func(void *, struct ieee80211_node *); 121 static void wpi_power_calibration(struct wpi_softc *, int); 122 static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *, 123 struct wpi_rx_data *); 124 static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *); 125 static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *); 126 static void wpi_notif_intr(struct wpi_softc *); 127 static int wpi_intr(void *); 128 static void wpi_read_eeprom(struct wpi_softc *); 129 static void wpi_read_eeprom_channels(struct wpi_softc *, int); 130 static void wpi_read_eeprom_group(struct wpi_softc *, int); 131 static uint8_t wpi_plcp_signal(int); 132 static int wpi_tx_data(struct wpi_softc *, struct mbuf *, 133 struct ieee80211_node *, int); 134 static void wpi_start(struct ifnet *); 135 static void wpi_watchdog(struct ifnet *); 136 static int wpi_ioctl(struct ifnet *, u_long, void *); 137 static int wpi_cmd(struct wpi_softc *, int, const void *, int, int); 138 static int wpi_wme_update(struct ieee80211com *); 139 static int wpi_mrr_setup(struct wpi_softc *); 140 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t); 141 static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *); 142 static int wpi_set_txpower(struct wpi_softc *, 143 struct ieee80211_channel *, int); 144 static int wpi_get_power_index(struct wpi_softc *, 145 struct wpi_power_group *, struct ieee80211_channel *, int); 146 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *); 147 static int wpi_auth(struct wpi_softc *); 148 static int wpi_scan(struct wpi_softc *); 149 static int wpi_config(struct wpi_softc *); 150 static void wpi_stop_master(struct wpi_softc *); 151 static int wpi_power_up(struct wpi_softc *); 152 static int wpi_reset(struct wpi_softc *); 153 static void wpi_hw_config(struct wpi_softc *); 154 static int wpi_init(struct ifnet *); 155 static void wpi_stop(struct ifnet *, int); 156 static bool wpi_resume(device_t, const pmf_qual_t *); 157 static int wpi_getrfkill(struct wpi_softc *); 158 static void wpi_sysctlattach(struct wpi_softc *); 159 static void wpi_rsw_thread(void *); 160 161 #ifdef WPI_DEBUG 162 #define DPRINTF(x) do { if (wpi_debug > 0) printf x; } while (0) 163 #define DPRINTFN(n, x) do { if (wpi_debug >= (n)) printf x; } while (0) 164 int wpi_debug = 1; 165 #else 166 #define DPRINTF(x) 167 #define DPRINTFN(n, x) 168 #endif 169 170 CFATTACH_DECL_NEW(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach, 171 wpi_detach, NULL); 172 173 static int 174 wpi_match(device_t parent, cfdata_t match __unused, void *aux) 175 { 176 struct pci_attach_args *pa = aux; 177 178 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) 179 return 0; 180 181 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 || 182 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2) 183 return 1; 184 185 return 0; 186 } 187 188 /* Base Address Register */ 189 #define WPI_PCI_BAR0 0x10 190 191 static int 192 wpi_attach_once(void) 193 { 194 195 mutex_init(&wpi_firmware_mutex, MUTEX_DEFAULT, IPL_NONE); 196 return 0; 197 } 198 199 static void 200 wpi_attach(device_t parent __unused, device_t self, void *aux) 201 { 202 struct wpi_softc *sc = device_private(self); 203 struct ieee80211com *ic = &sc->sc_ic; 204 struct ifnet *ifp = &sc->sc_ec.ec_if; 205 struct pci_attach_args *pa = aux; 206 const char *intrstr; 207 bus_space_tag_t memt; 208 bus_space_handle_t memh; 209 pci_intr_handle_t ih; 210 pcireg_t data; 211 int ac, error; 212 char intrbuf[PCI_INTRSTR_LEN]; 213 214 RUN_ONCE(&wpi_firmware_init, wpi_attach_once); 215 sc->fw_used = false; 216 217 sc->sc_dev = self; 218 sc->sc_pct = pa->pa_pc; 219 sc->sc_pcitag = pa->pa_tag; 220 221 sc->sc_rsw_status = WPI_RSW_UNKNOWN; 222 sc->sc_rsw.smpsw_name = device_xname(self); 223 sc->sc_rsw.smpsw_type = PSWITCH_TYPE_RADIO; 224 error = sysmon_pswitch_register(&sc->sc_rsw); 225 if (error) { 226 aprint_error_dev(self, 227 "unable to register radio switch with sysmon\n"); 228 return; 229 } 230 mutex_init(&sc->sc_rsw_mtx, MUTEX_DEFAULT, IPL_NONE); 231 cv_init(&sc->sc_rsw_cv, "wpirsw"); 232 if (kthread_create(PRI_NONE, 0, NULL, 233 wpi_rsw_thread, sc, &sc->sc_rsw_lwp, "%s", device_xname(self))) { 234 aprint_error_dev(self, "couldn't create switch thread\n"); 235 } 236 237 callout_init(&sc->calib_to, 0); 238 callout_setfunc(&sc->calib_to, wpi_calib_timeout, sc); 239 240 pci_aprint_devinfo(pa, NULL); 241 242 /* enable bus-mastering */ 243 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 244 data |= PCI_COMMAND_MASTER_ENABLE; 245 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data); 246 247 /* map the register window */ 248 error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM | 249 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz); 250 if (error != 0) { 251 aprint_error_dev(self, "could not map memory space\n"); 252 return; 253 } 254 255 sc->sc_st = memt; 256 sc->sc_sh = memh; 257 sc->sc_dmat = pa->pa_dmat; 258 259 if (pci_intr_map(pa, &ih) != 0) { 260 aprint_error_dev(self, "could not map interrupt\n"); 261 return; 262 } 263 264 intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf)); 265 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc); 266 if (sc->sc_ih == NULL) { 267 aprint_error_dev(self, "could not establish interrupt"); 268 if (intrstr != NULL) 269 aprint_error(" at %s", intrstr); 270 aprint_error("\n"); 271 return; 272 } 273 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 274 275 /* 276 * Put adapter into a known state. 277 */ 278 if ((error = wpi_reset(sc)) != 0) { 279 aprint_error_dev(self, "could not reset adapter\n"); 280 return; 281 } 282 283 /* 284 * Allocate DMA memory for firmware transfers. 285 */ 286 if ((error = wpi_alloc_fwmem(sc)) != 0) { 287 aprint_error_dev(self, "could not allocate firmware memory\n"); 288 return; 289 } 290 291 /* 292 * Allocate shared page and Tx/Rx rings. 293 */ 294 if ((error = wpi_alloc_shared(sc)) != 0) { 295 aprint_error_dev(self, "could not allocate shared area\n"); 296 goto fail1; 297 } 298 299 if ((error = wpi_alloc_rpool(sc)) != 0) { 300 aprint_error_dev(self, "could not allocate Rx buffers\n"); 301 goto fail2; 302 } 303 304 for (ac = 0; ac < 4; ac++) { 305 error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, 306 ac); 307 if (error != 0) { 308 aprint_error_dev(self, 309 "could not allocate Tx ring %d\n", ac); 310 goto fail3; 311 } 312 } 313 314 error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4); 315 if (error != 0) { 316 aprint_error_dev(self, "could not allocate command ring\n"); 317 goto fail3; 318 } 319 320 error = wpi_alloc_rx_ring(sc, &sc->rxq); 321 if (error != 0) { 322 aprint_error_dev(self, "could not allocate Rx ring\n"); 323 goto fail4; 324 } 325 326 ic->ic_ifp = ifp; 327 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 328 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 329 ic->ic_state = IEEE80211_S_INIT; 330 331 /* set device capabilities */ 332 ic->ic_caps = 333 IEEE80211_C_WPA | /* 802.11i */ 334 IEEE80211_C_MONITOR | /* monitor mode supported */ 335 IEEE80211_C_TXPMGT | /* tx power management */ 336 IEEE80211_C_SHSLOT | /* short slot time supported */ 337 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 338 IEEE80211_C_WME; /* 802.11e */ 339 340 /* read supported channels and MAC address from EEPROM */ 341 wpi_read_eeprom(sc); 342 343 /* set supported .11a, .11b and .11g rates */ 344 ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a; 345 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b; 346 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g; 347 348 /* IBSS channel undefined for now */ 349 ic->ic_ibss_chan = &ic->ic_channels[0]; 350 351 ifp->if_softc = sc; 352 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 353 ifp->if_init = wpi_init; 354 ifp->if_stop = wpi_stop; 355 ifp->if_ioctl = wpi_ioctl; 356 ifp->if_start = wpi_start; 357 ifp->if_watchdog = wpi_watchdog; 358 IFQ_SET_READY(&ifp->if_snd); 359 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 360 361 if_attach(ifp); 362 if_deferred_start_init(ifp, NULL); 363 ieee80211_ifattach(ic); 364 /* override default methods */ 365 ic->ic_node_alloc = wpi_node_alloc; 366 ic->ic_newassoc = wpi_newassoc; 367 ic->ic_wme.wme_update = wpi_wme_update; 368 369 /* override state transition machine */ 370 sc->sc_newstate = ic->ic_newstate; 371 ic->ic_newstate = wpi_newstate; 372 ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status); 373 374 sc->amrr.amrr_min_success_threshold = 1; 375 sc->amrr.amrr_max_success_threshold = 15; 376 377 wpi_sysctlattach(sc); 378 379 if (pmf_device_register(self, NULL, wpi_resume)) 380 pmf_class_network_register(self, ifp); 381 else 382 aprint_error_dev(self, "couldn't establish power handler\n"); 383 384 bpf_attach2(ifp, DLT_IEEE802_11_RADIO, 385 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 386 &sc->sc_drvbpf); 387 388 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 389 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 390 sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT); 391 392 sc->sc_txtap_len = sizeof sc->sc_txtapu; 393 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 394 sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT); 395 396 ieee80211_announce(ic); 397 398 return; 399 400 /* free allocated memory if something failed during attachment */ 401 fail4: wpi_free_tx_ring(sc, &sc->cmdq); 402 fail3: while (--ac >= 0) 403 wpi_free_tx_ring(sc, &sc->txq[ac]); 404 wpi_free_rpool(sc); 405 fail2: wpi_free_shared(sc); 406 fail1: wpi_free_fwmem(sc); 407 } 408 409 static int 410 wpi_detach(device_t self, int flags __unused) 411 { 412 struct wpi_softc *sc = device_private(self); 413 struct ifnet *ifp = sc->sc_ic.ic_ifp; 414 int ac; 415 416 wpi_stop(ifp, 1); 417 418 if (ifp != NULL) 419 bpf_detach(ifp); 420 ieee80211_ifdetach(&sc->sc_ic); 421 if (ifp != NULL) 422 if_detach(ifp); 423 424 for (ac = 0; ac < 4; ac++) 425 wpi_free_tx_ring(sc, &sc->txq[ac]); 426 wpi_free_tx_ring(sc, &sc->cmdq); 427 wpi_free_rx_ring(sc, &sc->rxq); 428 wpi_free_rpool(sc); 429 wpi_free_shared(sc); 430 431 if (sc->sc_ih != NULL) { 432 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 433 sc->sc_ih = NULL; 434 } 435 mutex_enter(&sc->sc_rsw_mtx); 436 sc->sc_dying = 1; 437 cv_signal(&sc->sc_rsw_cv); 438 while (sc->sc_rsw_lwp != NULL) 439 cv_wait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx); 440 mutex_exit(&sc->sc_rsw_mtx); 441 sysmon_pswitch_unregister(&sc->sc_rsw); 442 443 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz); 444 445 if (sc->fw_used) { 446 sc->fw_used = false; 447 wpi_release_firmware(); 448 } 449 cv_destroy(&sc->sc_rsw_cv); 450 mutex_destroy(&sc->sc_rsw_mtx); 451 return 0; 452 } 453 454 static int 455 wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma, void **kvap, 456 bus_size_t size, bus_size_t alignment, int flags) 457 { 458 int nsegs, error; 459 460 dma->tag = tag; 461 dma->size = size; 462 463 error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map); 464 if (error != 0) 465 goto fail; 466 467 error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs, 468 flags); 469 if (error != 0) 470 goto fail; 471 472 error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags); 473 if (error != 0) 474 goto fail; 475 476 error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags); 477 if (error != 0) 478 goto fail; 479 480 memset(dma->vaddr, 0, size); 481 bus_dmamap_sync(dma->tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE); 482 483 dma->paddr = dma->map->dm_segs[0].ds_addr; 484 if (kvap != NULL) 485 *kvap = dma->vaddr; 486 487 return 0; 488 489 fail: wpi_dma_contig_free(dma); 490 return error; 491 } 492 493 static void 494 wpi_dma_contig_free(struct wpi_dma_info *dma) 495 { 496 if (dma->map != NULL) { 497 if (dma->vaddr != NULL) { 498 bus_dmamap_unload(dma->tag, dma->map); 499 bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size); 500 bus_dmamem_free(dma->tag, &dma->seg, 1); 501 dma->vaddr = NULL; 502 } 503 bus_dmamap_destroy(dma->tag, dma->map); 504 dma->map = NULL; 505 } 506 } 507 508 /* 509 * Allocate a shared page between host and NIC. 510 */ 511 static int 512 wpi_alloc_shared(struct wpi_softc *sc) 513 { 514 int error; 515 516 /* must be aligned on a 4K-page boundary */ 517 error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma, 518 (void **)&sc->shared, sizeof (struct wpi_shared), WPI_BUF_ALIGN, 519 BUS_DMA_NOWAIT); 520 if (error != 0) 521 aprint_error_dev(sc->sc_dev, 522 "could not allocate shared area DMA memory\n"); 523 524 return error; 525 } 526 527 static void 528 wpi_free_shared(struct wpi_softc *sc) 529 { 530 wpi_dma_contig_free(&sc->shared_dma); 531 } 532 533 /* 534 * Allocate DMA-safe memory for firmware transfer. 535 */ 536 static int 537 wpi_alloc_fwmem(struct wpi_softc *sc) 538 { 539 int error; 540 541 /* allocate enough contiguous space to store text and data */ 542 error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL, 543 WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 0, 544 BUS_DMA_NOWAIT); 545 546 if (error != 0) 547 aprint_error_dev(sc->sc_dev, 548 "could not allocate firmware transfer area DMA memory\n"); 549 return error; 550 } 551 552 static void 553 wpi_free_fwmem(struct wpi_softc *sc) 554 { 555 wpi_dma_contig_free(&sc->fw_dma); 556 } 557 558 static struct wpi_rbuf * 559 wpi_alloc_rbuf(struct wpi_softc *sc) 560 { 561 struct wpi_rbuf *rbuf; 562 563 mutex_enter(&sc->rxq.freelist_mtx); 564 rbuf = SLIST_FIRST(&sc->rxq.freelist); 565 if (rbuf != NULL) { 566 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next); 567 } 568 mutex_exit(&sc->rxq.freelist_mtx); 569 570 return rbuf; 571 } 572 573 /* 574 * This is called automatically by the network stack when the mbuf to which our 575 * Rx buffer is attached is freed. 576 */ 577 static void 578 wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg) 579 { 580 struct wpi_rbuf *rbuf = arg; 581 struct wpi_softc *sc = rbuf->sc; 582 583 /* put the buffer back in the free list */ 584 585 mutex_enter(&sc->rxq.freelist_mtx); 586 SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next); 587 mutex_exit(&sc->rxq.freelist_mtx); 588 589 if (__predict_true(m != NULL)) 590 pool_cache_put(mb_cache, m); 591 } 592 593 static int 594 wpi_alloc_rpool(struct wpi_softc *sc) 595 { 596 struct wpi_rx_ring *ring = &sc->rxq; 597 int i, error; 598 599 /* allocate a big chunk of DMA'able memory.. */ 600 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL, 601 WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT); 602 if (error != 0) { 603 aprint_normal_dev(sc->sc_dev, 604 "could not allocate Rx buffers DMA memory\n"); 605 return error; 606 } 607 608 /* ..and split it into 3KB chunks */ 609 mutex_init(&ring->freelist_mtx, MUTEX_DEFAULT, IPL_NET); 610 SLIST_INIT(&ring->freelist); 611 for (i = 0; i < WPI_RBUF_COUNT; i++) { 612 struct wpi_rbuf *rbuf = &ring->rbuf[i]; 613 614 rbuf->sc = sc; /* backpointer for callbacks */ 615 rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE; 616 rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE; 617 618 SLIST_INSERT_HEAD(&ring->freelist, rbuf, next); 619 } 620 621 return 0; 622 } 623 624 static void 625 wpi_free_rpool(struct wpi_softc *sc) 626 { 627 wpi_dma_contig_free(&sc->rxq.buf_dma); 628 } 629 630 static int 631 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 632 { 633 bus_size_t size; 634 int i, error; 635 636 ring->cur = 0; 637 638 size = WPI_RX_RING_COUNT * sizeof (uint32_t); 639 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 640 (void **)&ring->desc, size, 641 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 642 if (error != 0) { 643 aprint_error_dev(sc->sc_dev, 644 "could not allocate rx ring DMA memory\n"); 645 goto fail; 646 } 647 648 /* 649 * Setup Rx buffers. 650 */ 651 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 652 struct wpi_rx_data *data = &ring->data[i]; 653 struct wpi_rbuf *rbuf; 654 655 error = bus_dmamap_create(sc->sc_dmat, WPI_RBUF_SIZE, 1, 656 WPI_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map); 657 if (error) { 658 aprint_error_dev(sc->sc_dev, 659 "could not allocate rx dma map\n"); 660 goto fail; 661 } 662 663 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 664 if (data->m == NULL) { 665 aprint_error_dev(sc->sc_dev, 666 "could not allocate rx mbuf\n"); 667 error = ENOMEM; 668 goto fail; 669 } 670 if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) { 671 m_freem(data->m); 672 data->m = NULL; 673 aprint_error_dev(sc->sc_dev, 674 "could not allocate rx cluster\n"); 675 error = ENOMEM; 676 goto fail; 677 } 678 /* attach Rx buffer to mbuf */ 679 MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf, 680 rbuf); 681 data->m->m_flags |= M_EXT_RW; 682 683 error = bus_dmamap_load(sc->sc_dmat, data->map, 684 mtod(data->m, void *), WPI_RBUF_SIZE, NULL, 685 BUS_DMA_NOWAIT | BUS_DMA_READ); 686 if (error) { 687 aprint_error_dev(sc->sc_dev, 688 "could not load mbuf: %d\n", error); 689 goto fail; 690 } 691 692 ring->desc[i] = htole32(rbuf->paddr); 693 } 694 695 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, size, 696 BUS_DMASYNC_PREWRITE); 697 698 return 0; 699 700 fail: wpi_free_rx_ring(sc, ring); 701 return error; 702 } 703 704 static void 705 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 706 { 707 int ntries; 708 709 wpi_mem_lock(sc); 710 711 WPI_WRITE(sc, WPI_RX_CONFIG, 0); 712 for (ntries = 0; ntries < 100; ntries++) { 713 if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE) 714 break; 715 DELAY(10); 716 } 717 #ifdef WPI_DEBUG 718 if (ntries == 100 && wpi_debug > 0) 719 aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n"); 720 #endif 721 wpi_mem_unlock(sc); 722 723 ring->cur = 0; 724 } 725 726 static void 727 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 728 { 729 int i; 730 731 wpi_dma_contig_free(&ring->desc_dma); 732 733 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 734 if (ring->data[i].m != NULL) { 735 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map); 736 m_freem(ring->data[i].m); 737 } 738 if (ring->data[i].map != NULL) { 739 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map); 740 } 741 } 742 } 743 744 static int 745 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count, 746 int qid) 747 { 748 int i, error; 749 750 ring->qid = qid; 751 ring->count = count; 752 ring->queued = 0; 753 ring->cur = 0; 754 755 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 756 (void **)&ring->desc, count * sizeof (struct wpi_tx_desc), 757 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 758 if (error != 0) { 759 aprint_error_dev(sc->sc_dev, 760 "could not allocate tx ring DMA memory\n"); 761 goto fail; 762 } 763 764 /* update shared page with ring's base address */ 765 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr); 766 bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0, 767 sizeof(struct wpi_shared), BUS_DMASYNC_PREWRITE); 768 769 error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma, 770 (void **)&ring->cmd, count * sizeof (struct wpi_tx_cmd), 4, 771 BUS_DMA_NOWAIT); 772 if (error != 0) { 773 aprint_error_dev(sc->sc_dev, 774 "could not allocate tx cmd DMA memory\n"); 775 goto fail; 776 } 777 778 ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF, 779 M_NOWAIT | M_ZERO); 780 if (ring->data == NULL) { 781 aprint_error_dev(sc->sc_dev, 782 "could not allocate tx data slots\n"); 783 goto fail; 784 } 785 786 for (i = 0; i < count; i++) { 787 struct wpi_tx_data *data = &ring->data[i]; 788 789 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 790 WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT, 791 &data->map); 792 if (error != 0) { 793 aprint_error_dev(sc->sc_dev, 794 "could not create tx buf DMA map\n"); 795 goto fail; 796 } 797 } 798 799 return 0; 800 801 fail: wpi_free_tx_ring(sc, ring); 802 return error; 803 } 804 805 static void 806 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 807 { 808 int i, ntries; 809 810 wpi_mem_lock(sc); 811 812 WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0); 813 for (ntries = 0; ntries < 100; ntries++) { 814 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid)) 815 break; 816 DELAY(10); 817 } 818 #ifdef WPI_DEBUG 819 if (ntries == 100 && wpi_debug > 0) { 820 aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n", 821 ring->qid); 822 } 823 #endif 824 wpi_mem_unlock(sc); 825 826 for (i = 0; i < ring->count; i++) { 827 struct wpi_tx_data *data = &ring->data[i]; 828 829 if (data->m != NULL) { 830 bus_dmamap_unload(sc->sc_dmat, data->map); 831 m_freem(data->m); 832 data->m = NULL; 833 } 834 } 835 836 ring->queued = 0; 837 ring->cur = 0; 838 } 839 840 static void 841 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 842 { 843 int i; 844 845 wpi_dma_contig_free(&ring->desc_dma); 846 wpi_dma_contig_free(&ring->cmd_dma); 847 848 if (ring->data != NULL) { 849 for (i = 0; i < ring->count; i++) { 850 struct wpi_tx_data *data = &ring->data[i]; 851 852 if (data->m != NULL) { 853 bus_dmamap_unload(sc->sc_dmat, data->map); 854 m_freem(data->m); 855 } 856 } 857 free(ring->data, M_DEVBUF); 858 } 859 } 860 861 /*ARGUSED*/ 862 static struct ieee80211_node * 863 wpi_node_alloc(struct ieee80211_node_table *nt __unused) 864 { 865 struct wpi_node *wn; 866 867 wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO); 868 869 return (struct ieee80211_node *)wn; 870 } 871 872 static void 873 wpi_newassoc(struct ieee80211_node *ni, int isnew) 874 { 875 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc; 876 int i; 877 878 ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn); 879 880 /* set rate to some reasonable initial value */ 881 for (i = ni->ni_rates.rs_nrates - 1; 882 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 883 i--); 884 ni->ni_txrate = i; 885 } 886 887 static int 888 wpi_media_change(struct ifnet *ifp) 889 { 890 int error; 891 892 error = ieee80211_media_change(ifp); 893 if (error != ENETRESET) 894 return error; 895 896 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 897 wpi_init(ifp); 898 899 return 0; 900 } 901 902 static int 903 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 904 { 905 struct ifnet *ifp = ic->ic_ifp; 906 struct wpi_softc *sc = ifp->if_softc; 907 struct ieee80211_node *ni; 908 enum ieee80211_state ostate = ic->ic_state; 909 int error; 910 911 callout_stop(&sc->calib_to); 912 913 switch (nstate) { 914 case IEEE80211_S_SCAN: 915 916 if (sc->is_scanning) 917 break; 918 919 sc->is_scanning = true; 920 921 if (ostate != IEEE80211_S_SCAN) { 922 /* make the link LED blink while we're scanning */ 923 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 924 } 925 926 if ((error = wpi_scan(sc)) != 0) { 927 aprint_error_dev(sc->sc_dev, 928 "could not initiate scan\n"); 929 return error; 930 } 931 break; 932 933 case IEEE80211_S_ASSOC: 934 if (ic->ic_state != IEEE80211_S_RUN) 935 break; 936 /* FALLTHROUGH */ 937 case IEEE80211_S_AUTH: 938 /* reset state to handle reassociations correctly */ 939 sc->config.associd = 0; 940 sc->config.filter &= ~htole32(WPI_FILTER_BSS); 941 942 if ((error = wpi_auth(sc)) != 0) { 943 aprint_error_dev(sc->sc_dev, 944 "could not send authentication request\n"); 945 return error; 946 } 947 break; 948 949 case IEEE80211_S_RUN: 950 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 951 /* link LED blinks while monitoring */ 952 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 953 break; 954 } 955 ni = ic->ic_bss; 956 957 if (ic->ic_opmode != IEEE80211_M_STA) { 958 (void) wpi_auth(sc); /* XXX */ 959 wpi_setup_beacon(sc, ni); 960 } 961 962 wpi_enable_tsf(sc, ni); 963 964 /* update adapter's configuration */ 965 sc->config.associd = htole16(ni->ni_associd & ~0xc000); 966 /* short preamble/slot time are negotiated when associating */ 967 sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE | 968 WPI_CONFIG_SHSLOT); 969 if (ic->ic_flags & IEEE80211_F_SHSLOT) 970 sc->config.flags |= htole32(WPI_CONFIG_SHSLOT); 971 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 972 sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE); 973 sc->config.filter |= htole32(WPI_FILTER_BSS); 974 if (ic->ic_opmode != IEEE80211_M_STA) 975 sc->config.filter |= htole32(WPI_FILTER_BEACON); 976 977 /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */ 978 979 DPRINTF(("config chan %d flags %x\n", sc->config.chan, 980 sc->config.flags)); 981 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 982 sizeof (struct wpi_config), 1); 983 if (error != 0) { 984 aprint_error_dev(sc->sc_dev, 985 "could not update configuration\n"); 986 return error; 987 } 988 989 /* configuration has changed, set Tx power accordingly */ 990 if ((error = wpi_set_txpower(sc, ic->ic_curchan, 1)) != 0) { 991 aprint_error_dev(sc->sc_dev, 992 "could not set Tx power\n"); 993 return error; 994 } 995 996 if (ic->ic_opmode == IEEE80211_M_STA) { 997 /* fake a join to init the tx rate */ 998 wpi_newassoc(ni, 1); 999 } 1000 1001 /* start periodic calibration timer */ 1002 sc->calib_cnt = 0; 1003 callout_schedule(&sc->calib_to, hz/2); 1004 1005 /* link LED always on while associated */ 1006 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 1007 break; 1008 1009 case IEEE80211_S_INIT: 1010 sc->is_scanning = false; 1011 break; 1012 } 1013 1014 return sc->sc_newstate(ic, nstate, arg); 1015 } 1016 1017 /* 1018 * Grab exclusive access to NIC memory. 1019 */ 1020 static void 1021 wpi_mem_lock(struct wpi_softc *sc) 1022 { 1023 uint32_t tmp; 1024 int ntries; 1025 1026 tmp = WPI_READ(sc, WPI_GPIO_CTL); 1027 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC); 1028 1029 /* spin until we actually get the lock */ 1030 for (ntries = 0; ntries < 1000; ntries++) { 1031 if ((WPI_READ(sc, WPI_GPIO_CTL) & 1032 (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK) 1033 break; 1034 DELAY(10); 1035 } 1036 if (ntries == 1000) 1037 aprint_error_dev(sc->sc_dev, "could not lock memory\n"); 1038 } 1039 1040 /* 1041 * Release lock on NIC memory. 1042 */ 1043 static void 1044 wpi_mem_unlock(struct wpi_softc *sc) 1045 { 1046 uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL); 1047 WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC); 1048 } 1049 1050 static uint32_t 1051 wpi_mem_read(struct wpi_softc *sc, uint16_t addr) 1052 { 1053 WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr); 1054 return WPI_READ(sc, WPI_READ_MEM_DATA); 1055 } 1056 1057 static void 1058 wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data) 1059 { 1060 WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr); 1061 WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data); 1062 } 1063 1064 static void 1065 wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr, 1066 const uint32_t *data, int wlen) 1067 { 1068 for (; wlen > 0; wlen--, data++, addr += 4) 1069 wpi_mem_write(sc, addr, *data); 1070 } 1071 1072 /* 1073 * Read `len' bytes from the EEPROM. We access the EEPROM through the MAC 1074 * instead of using the traditional bit-bang method. 1075 */ 1076 static int 1077 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len) 1078 { 1079 uint8_t *out = data; 1080 uint32_t val; 1081 int ntries; 1082 1083 wpi_mem_lock(sc); 1084 for (; len > 0; len -= 2, addr++) { 1085 WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2); 1086 1087 for (ntries = 0; ntries < 10; ntries++) { 1088 if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & 1089 WPI_EEPROM_READY) 1090 break; 1091 DELAY(5); 1092 } 1093 if (ntries == 10) { 1094 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n"); 1095 return ETIMEDOUT; 1096 } 1097 *out++ = val >> 16; 1098 if (len > 1) 1099 *out++ = val >> 24; 1100 } 1101 wpi_mem_unlock(sc); 1102 1103 return 0; 1104 } 1105 1106 /* 1107 * The firmware boot code is small and is intended to be copied directly into 1108 * the NIC internal memory. 1109 */ 1110 int 1111 wpi_load_microcode(struct wpi_softc *sc, const uint8_t *ucode, int size) 1112 { 1113 int ntries; 1114 1115 size /= sizeof (uint32_t); 1116 1117 wpi_mem_lock(sc); 1118 1119 /* copy microcode image into NIC memory */ 1120 wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE, 1121 (const uint32_t *)ucode, size); 1122 1123 wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0); 1124 wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT); 1125 wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size); 1126 1127 /* run microcode */ 1128 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN); 1129 1130 /* wait for transfer to complete */ 1131 for (ntries = 0; ntries < 1000; ntries++) { 1132 if (!(wpi_mem_read(sc, WPI_MEM_UCODE_CTL) & WPI_UC_RUN)) 1133 break; 1134 DELAY(10); 1135 } 1136 if (ntries == 1000) { 1137 wpi_mem_unlock(sc); 1138 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n"); 1139 return ETIMEDOUT; 1140 } 1141 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE); 1142 1143 wpi_mem_unlock(sc); 1144 1145 return 0; 1146 } 1147 1148 static int 1149 wpi_cache_firmware(struct wpi_softc *sc) 1150 { 1151 const char *const fwname = wpi_firmware_name; 1152 firmware_handle_t fw; 1153 int error; 1154 1155 /* sc is used here only to report error messages. */ 1156 1157 mutex_enter(&wpi_firmware_mutex); 1158 1159 if (wpi_firmware_users == SIZE_MAX) { 1160 mutex_exit(&wpi_firmware_mutex); 1161 return ENFILE; /* Too many of something in the system... */ 1162 } 1163 if (wpi_firmware_users++) { 1164 KASSERT(wpi_firmware_image != NULL); 1165 KASSERT(wpi_firmware_size > 0); 1166 mutex_exit(&wpi_firmware_mutex); 1167 return 0; /* Already good to go. */ 1168 } 1169 1170 KASSERT(wpi_firmware_image == NULL); 1171 KASSERT(wpi_firmware_size == 0); 1172 1173 /* load firmware image from disk */ 1174 if ((error = firmware_open("if_wpi", fwname, &fw)) != 0) { 1175 aprint_error_dev(sc->sc_dev, 1176 "could not open firmware file %s: %d\n", fwname, error); 1177 goto fail0; 1178 } 1179 1180 wpi_firmware_size = firmware_get_size(fw); 1181 1182 if (wpi_firmware_size > sizeof (struct wpi_firmware_hdr) + 1183 WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ + 1184 WPI_FW_INIT_TEXT_MAXSZ + WPI_FW_INIT_DATA_MAXSZ + 1185 WPI_FW_BOOT_TEXT_MAXSZ) { 1186 aprint_error_dev(sc->sc_dev, 1187 "firmware file %s too large: %zu bytes\n", 1188 fwname, wpi_firmware_size); 1189 error = EFBIG; 1190 goto fail1; 1191 } 1192 1193 if (wpi_firmware_size < sizeof (struct wpi_firmware_hdr)) { 1194 aprint_error_dev(sc->sc_dev, 1195 "firmware file %s too small: %zu bytes\n", 1196 fwname, wpi_firmware_size); 1197 error = EINVAL; 1198 goto fail1; 1199 } 1200 1201 wpi_firmware_image = firmware_malloc(wpi_firmware_size); 1202 if (wpi_firmware_image == NULL) { 1203 aprint_error_dev(sc->sc_dev, 1204 "not enough memory for firmware file %s\n", fwname); 1205 error = ENOMEM; 1206 goto fail1; 1207 } 1208 1209 error = firmware_read(fw, 0, wpi_firmware_image, wpi_firmware_size); 1210 if (error != 0) { 1211 aprint_error_dev(sc->sc_dev, 1212 "error reading firmware file %s: %d\n", fwname, error); 1213 goto fail2; 1214 } 1215 1216 /* Success! */ 1217 firmware_close(fw); 1218 mutex_exit(&wpi_firmware_mutex); 1219 return 0; 1220 1221 fail2: 1222 firmware_free(wpi_firmware_image, wpi_firmware_size); 1223 wpi_firmware_image = NULL; 1224 fail1: 1225 wpi_firmware_size = 0; 1226 firmware_close(fw); 1227 fail0: 1228 KASSERT(wpi_firmware_users == 1); 1229 wpi_firmware_users = 0; 1230 KASSERT(wpi_firmware_image == NULL); 1231 KASSERT(wpi_firmware_size == 0); 1232 1233 mutex_exit(&wpi_firmware_mutex); 1234 return error; 1235 } 1236 1237 static void 1238 wpi_release_firmware(void) 1239 { 1240 1241 mutex_enter(&wpi_firmware_mutex); 1242 1243 KASSERT(wpi_firmware_users > 0); 1244 KASSERT(wpi_firmware_image != NULL); 1245 KASSERT(wpi_firmware_size != 0); 1246 1247 if (--wpi_firmware_users == 0) { 1248 firmware_free(wpi_firmware_image, wpi_firmware_size); 1249 wpi_firmware_image = NULL; 1250 wpi_firmware_size = 0; 1251 } 1252 1253 mutex_exit(&wpi_firmware_mutex); 1254 } 1255 1256 static int 1257 wpi_load_firmware(struct wpi_softc *sc) 1258 { 1259 struct wpi_dma_info *dma = &sc->fw_dma; 1260 struct wpi_firmware_hdr hdr; 1261 const uint8_t *init_text, *init_data, *main_text, *main_data; 1262 const uint8_t *boot_text; 1263 uint32_t init_textsz, init_datasz, main_textsz, main_datasz; 1264 uint32_t boot_textsz; 1265 size_t size; 1266 int error; 1267 1268 if (!sc->fw_used) { 1269 if ((error = wpi_cache_firmware(sc)) != 0) 1270 return error; 1271 sc->fw_used = true; 1272 } 1273 1274 KASSERT(sc->fw_used); 1275 KASSERT(wpi_firmware_image != NULL); 1276 KASSERT(wpi_firmware_size > sizeof(hdr)); 1277 1278 memcpy(&hdr, wpi_firmware_image, sizeof(hdr)); 1279 1280 main_textsz = le32toh(hdr.main_textsz); 1281 main_datasz = le32toh(hdr.main_datasz); 1282 init_textsz = le32toh(hdr.init_textsz); 1283 init_datasz = le32toh(hdr.init_datasz); 1284 boot_textsz = le32toh(hdr.boot_textsz); 1285 1286 /* sanity-check firmware segments sizes */ 1287 if (main_textsz > WPI_FW_MAIN_TEXT_MAXSZ || 1288 main_datasz > WPI_FW_MAIN_DATA_MAXSZ || 1289 init_textsz > WPI_FW_INIT_TEXT_MAXSZ || 1290 init_datasz > WPI_FW_INIT_DATA_MAXSZ || 1291 boot_textsz > WPI_FW_BOOT_TEXT_MAXSZ || 1292 (boot_textsz & 3) != 0) { 1293 aprint_error_dev(sc->sc_dev, "invalid firmware header\n"); 1294 error = EINVAL; 1295 goto free_firmware; 1296 } 1297 1298 /* check that all firmware segments are present */ 1299 size = sizeof (struct wpi_firmware_hdr) + main_textsz + 1300 main_datasz + init_textsz + init_datasz + boot_textsz; 1301 if (wpi_firmware_size < size) { 1302 aprint_error_dev(sc->sc_dev, 1303 "firmware file truncated: %zu bytes, expected %zu bytes\n", 1304 wpi_firmware_size, size); 1305 error = EINVAL; 1306 goto free_firmware; 1307 } 1308 1309 /* get pointers to firmware segments */ 1310 main_text = wpi_firmware_image + sizeof (struct wpi_firmware_hdr); 1311 main_data = main_text + main_textsz; 1312 init_text = main_data + main_datasz; 1313 init_data = init_text + init_textsz; 1314 boot_text = init_data + init_datasz; 1315 1316 /* copy initialization images into pre-allocated DMA-safe memory */ 1317 memcpy(dma->vaddr, init_data, init_datasz); 1318 memcpy((char *)dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, init_text, 1319 init_textsz); 1320 1321 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE); 1322 1323 /* tell adapter where to find initialization images */ 1324 wpi_mem_lock(sc); 1325 wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr); 1326 wpi_mem_write(sc, WPI_MEM_DATA_SIZE, init_datasz); 1327 wpi_mem_write(sc, WPI_MEM_TEXT_BASE, 1328 dma->paddr + WPI_FW_INIT_DATA_MAXSZ); 1329 wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, init_textsz); 1330 wpi_mem_unlock(sc); 1331 1332 /* load firmware boot code */ 1333 if ((error = wpi_load_microcode(sc, boot_text, boot_textsz)) != 0) { 1334 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n"); 1335 return error; 1336 } 1337 1338 /* now press "execute" ;-) */ 1339 WPI_WRITE(sc, WPI_RESET, 0); 1340 1341 /* wait at most one second for first alive notification */ 1342 if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) { 1343 /* this isn't what was supposed to happen.. */ 1344 aprint_error_dev(sc->sc_dev, 1345 "timeout waiting for adapter to initialize\n"); 1346 } 1347 1348 /* copy runtime images into pre-allocated DMA-safe memory */ 1349 memcpy(dma->vaddr, main_data, main_datasz); 1350 memcpy((char *)dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, main_text, 1351 main_textsz); 1352 1353 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE); 1354 1355 /* tell adapter where to find runtime images */ 1356 wpi_mem_lock(sc); 1357 wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr); 1358 wpi_mem_write(sc, WPI_MEM_DATA_SIZE, main_datasz); 1359 wpi_mem_write(sc, WPI_MEM_TEXT_BASE, 1360 dma->paddr + WPI_FW_MAIN_DATA_MAXSZ); 1361 wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | main_textsz); 1362 wpi_mem_unlock(sc); 1363 1364 /* wait at most one second for second alive notification */ 1365 if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) { 1366 /* this isn't what was supposed to happen.. */ 1367 aprint_error_dev(sc->sc_dev, 1368 "timeout waiting for adapter to initialize\n"); 1369 } 1370 1371 return error; 1372 1373 free_firmware: 1374 sc->fw_used = false; 1375 wpi_release_firmware(); 1376 return error; 1377 } 1378 1379 static void 1380 wpi_calib_timeout(void *arg) 1381 { 1382 struct wpi_softc *sc = arg; 1383 struct ieee80211com *ic = &sc->sc_ic; 1384 int temp, s; 1385 1386 /* automatic rate control triggered every 500ms */ 1387 if (ic->ic_fixed_rate == -1) { 1388 s = splnet(); 1389 if (ic->ic_opmode == IEEE80211_M_STA) 1390 wpi_iter_func(sc, ic->ic_bss); 1391 else 1392 ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc); 1393 splx(s); 1394 } 1395 1396 /* update sensor data */ 1397 temp = (int)WPI_READ(sc, WPI_TEMPERATURE); 1398 1399 /* automatic power calibration every 60s */ 1400 if (++sc->calib_cnt >= 120) { 1401 wpi_power_calibration(sc, temp); 1402 sc->calib_cnt = 0; 1403 } 1404 1405 callout_schedule(&sc->calib_to, hz/2); 1406 } 1407 1408 static void 1409 wpi_iter_func(void *arg, struct ieee80211_node *ni) 1410 { 1411 struct wpi_softc *sc = arg; 1412 struct wpi_node *wn = (struct wpi_node *)ni; 1413 1414 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn); 1415 } 1416 1417 /* 1418 * This function is called periodically (every 60 seconds) to adjust output 1419 * power to temperature changes. 1420 */ 1421 void 1422 wpi_power_calibration(struct wpi_softc *sc, int temp) 1423 { 1424 /* sanity-check read value */ 1425 if (temp < -260 || temp > 25) { 1426 /* this can't be correct, ignore */ 1427 DPRINTF(("out-of-range temperature reported: %d\n", temp)); 1428 return; 1429 } 1430 1431 DPRINTF(("temperature %d->%d\n", sc->temp, temp)); 1432 1433 /* adjust Tx power if need be */ 1434 if (abs(temp - sc->temp) <= 6) 1435 return; 1436 1437 sc->temp = temp; 1438 1439 if (wpi_set_txpower(sc, sc->sc_ic.ic_curchan, 1) != 0) { 1440 /* just warn, too bad for the automatic calibration... */ 1441 aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n"); 1442 } 1443 } 1444 1445 static void 1446 wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc, 1447 struct wpi_rx_data *data) 1448 { 1449 struct ieee80211com *ic = &sc->sc_ic; 1450 struct ifnet *ifp = ic->ic_ifp; 1451 struct wpi_rx_ring *ring = &sc->rxq; 1452 struct wpi_rx_stat *stat; 1453 struct wpi_rx_head *head; 1454 struct wpi_rx_tail *tail; 1455 struct wpi_rbuf *rbuf; 1456 struct ieee80211_frame *wh; 1457 struct ieee80211_node *ni; 1458 struct mbuf *m, *mnew; 1459 int data_off, error; 1460 1461 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 1462 BUS_DMASYNC_POSTREAD); 1463 stat = (struct wpi_rx_stat *)(desc + 1); 1464 1465 if (stat->len > WPI_STAT_MAXLEN) { 1466 aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n"); 1467 ifp->if_ierrors++; 1468 return; 1469 } 1470 1471 head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len); 1472 tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len)); 1473 1474 DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x " 1475 "chan=%d tstamp=%" PRIu64 "\n", ring->cur, le32toh(desc->len), 1476 le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan, 1477 le64toh(tail->tstamp))); 1478 1479 /* 1480 * Discard Rx frames with bad CRC early (XXX we may want to pass them 1481 * to radiotap in monitor mode). 1482 */ 1483 if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) { 1484 DPRINTF(("rx tail flags error %x\n", 1485 le32toh(tail->flags))); 1486 ifp->if_ierrors++; 1487 return; 1488 } 1489 1490 /* Compute where are the useful datas */ 1491 data_off = (char*)(head + 1) - mtod(data->m, char*); 1492 1493 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 1494 if (mnew == NULL) { 1495 ifp->if_ierrors++; 1496 return; 1497 } 1498 1499 rbuf = wpi_alloc_rbuf(sc); 1500 if (rbuf == NULL) { 1501 m_freem(mnew); 1502 ifp->if_ierrors++; 1503 return; 1504 } 1505 1506 /* attach Rx buffer to mbuf */ 1507 MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf, 1508 rbuf); 1509 mnew->m_flags |= M_EXT_RW; 1510 1511 bus_dmamap_unload(sc->sc_dmat, data->map); 1512 1513 error = bus_dmamap_load(sc->sc_dmat, data->map, 1514 mtod(mnew, void *), WPI_RBUF_SIZE, NULL, 1515 BUS_DMA_NOWAIT | BUS_DMA_READ); 1516 if (error) { 1517 device_printf(sc->sc_dev, 1518 "couldn't load rx mbuf: %d\n", error); 1519 m_freem(mnew); 1520 ifp->if_ierrors++; 1521 1522 error = bus_dmamap_load(sc->sc_dmat, data->map, 1523 mtod(data->m, void *), WPI_RBUF_SIZE, NULL, 1524 BUS_DMA_NOWAIT | BUS_DMA_READ); 1525 if (error) 1526 panic("%s: bus_dmamap_load failed: %d\n", 1527 device_xname(sc->sc_dev), error); 1528 return; 1529 } 1530 1531 /* new mbuf loaded successfully */ 1532 m = data->m; 1533 data->m = mnew; 1534 1535 /* update Rx descriptor */ 1536 ring->desc[ring->cur] = htole32(rbuf->paddr); 1537 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, 1538 ring->desc_dma.size, 1539 BUS_DMASYNC_PREWRITE); 1540 1541 m->m_data = (char*)m->m_data + data_off; 1542 m->m_pkthdr.len = m->m_len = le16toh(head->len); 1543 1544 /* finalize mbuf */ 1545 m_set_rcvif(m, ifp); 1546 1547 if (sc->sc_drvbpf != NULL) { 1548 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap; 1549 1550 tap->wr_flags = 0; 1551 tap->wr_chan_freq = 1552 htole16(ic->ic_channels[head->chan].ic_freq); 1553 tap->wr_chan_flags = 1554 htole16(ic->ic_channels[head->chan].ic_flags); 1555 tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET); 1556 tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise); 1557 tap->wr_tsft = tail->tstamp; 1558 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf; 1559 switch (head->rate) { 1560 /* CCK rates */ 1561 case 10: tap->wr_rate = 2; break; 1562 case 20: tap->wr_rate = 4; break; 1563 case 55: tap->wr_rate = 11; break; 1564 case 110: tap->wr_rate = 22; break; 1565 /* OFDM rates */ 1566 case 0xd: tap->wr_rate = 12; break; 1567 case 0xf: tap->wr_rate = 18; break; 1568 case 0x5: tap->wr_rate = 24; break; 1569 case 0x7: tap->wr_rate = 36; break; 1570 case 0x9: tap->wr_rate = 48; break; 1571 case 0xb: tap->wr_rate = 72; break; 1572 case 0x1: tap->wr_rate = 96; break; 1573 case 0x3: tap->wr_rate = 108; break; 1574 /* unknown rate: should not happen */ 1575 default: tap->wr_rate = 0; 1576 } 1577 if (le16toh(head->flags) & 0x4) 1578 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1579 1580 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1581 } 1582 1583 /* grab a reference to the source node */ 1584 wh = mtod(m, struct ieee80211_frame *); 1585 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1586 1587 /* send the frame to the 802.11 layer */ 1588 ieee80211_input(ic, m, ni, stat->rssi, 0); 1589 1590 /* release node reference */ 1591 ieee80211_free_node(ni); 1592 } 1593 1594 static void 1595 wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1596 { 1597 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1598 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3]; 1599 struct wpi_tx_data *data = &ring->data[desc->idx]; 1600 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1); 1601 struct wpi_node *wn = (struct wpi_node *)data->ni; 1602 1603 DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x " 1604 "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries, 1605 stat->nkill, stat->rate, le32toh(stat->duration), 1606 le32toh(stat->status))); 1607 1608 /* 1609 * Update rate control statistics for the node. 1610 * XXX we should not count mgmt frames since they're always sent at 1611 * the lowest available bit-rate. 1612 */ 1613 wn->amn.amn_txcnt++; 1614 if (stat->ntries > 0) { 1615 DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries)); 1616 wn->amn.amn_retrycnt++; 1617 } 1618 1619 if ((le32toh(stat->status) & 0xff) != 1) 1620 ifp->if_oerrors++; 1621 else 1622 ifp->if_opackets++; 1623 1624 bus_dmamap_unload(sc->sc_dmat, data->map); 1625 m_freem(data->m); 1626 data->m = NULL; 1627 ieee80211_free_node(data->ni); 1628 data->ni = NULL; 1629 1630 ring->queued--; 1631 1632 sc->sc_tx_timer = 0; 1633 ifp->if_flags &= ~IFF_OACTIVE; 1634 if_schedule_deferred_start(ifp); 1635 } 1636 1637 static void 1638 wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1639 { 1640 struct wpi_tx_ring *ring = &sc->cmdq; 1641 struct wpi_tx_data *data; 1642 1643 if ((desc->qid & 7) != 4) 1644 return; /* not a command ack */ 1645 1646 data = &ring->data[desc->idx]; 1647 1648 /* if the command was mapped in a mbuf, free it */ 1649 if (data->m != NULL) { 1650 bus_dmamap_unload(sc->sc_dmat, data->map); 1651 m_freem(data->m); 1652 data->m = NULL; 1653 } 1654 1655 wakeup(&ring->cmd[desc->idx]); 1656 } 1657 1658 static void 1659 wpi_notif_intr(struct wpi_softc *sc) 1660 { 1661 struct ieee80211com *ic = &sc->sc_ic; 1662 struct ifnet *ifp = ic->ic_ifp; 1663 uint32_t hw; 1664 1665 bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0, 1666 sizeof(struct wpi_shared), BUS_DMASYNC_POSTREAD); 1667 1668 hw = le32toh(sc->shared->next); 1669 while (sc->rxq.cur != hw) { 1670 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur]; 1671 struct wpi_rx_desc *desc; 1672 1673 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 1674 BUS_DMASYNC_POSTREAD); 1675 desc = mtod(data->m, struct wpi_rx_desc *); 1676 1677 DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d " 1678 "len=%d\n", desc->qid, desc->idx, desc->flags, 1679 desc->type, le32toh(desc->len))); 1680 1681 if (!(desc->qid & 0x80)) /* reply to a command */ 1682 wpi_cmd_intr(sc, desc); 1683 1684 switch (desc->type) { 1685 case WPI_RX_DONE: 1686 /* a 802.11 frame was received */ 1687 wpi_rx_intr(sc, desc, data); 1688 break; 1689 1690 case WPI_TX_DONE: 1691 /* a 802.11 frame has been transmitted */ 1692 wpi_tx_intr(sc, desc); 1693 break; 1694 1695 case WPI_UC_READY: 1696 { 1697 struct wpi_ucode_info *uc = 1698 (struct wpi_ucode_info *)(desc + 1); 1699 1700 /* the microcontroller is ready */ 1701 DPRINTF(("microcode alive notification version %x " 1702 "alive %x\n", le32toh(uc->version), 1703 le32toh(uc->valid))); 1704 1705 if (le32toh(uc->valid) != 1) { 1706 aprint_error_dev(sc->sc_dev, 1707 "microcontroller initialization failed\n"); 1708 } 1709 break; 1710 } 1711 case WPI_STATE_CHANGED: 1712 { 1713 uint32_t *status = (uint32_t *)(desc + 1); 1714 1715 /* enabled/disabled notification */ 1716 DPRINTF(("state changed to %x\n", le32toh(*status))); 1717 1718 if (le32toh(*status) & 1) { 1719 /* the radio button has to be pushed */ 1720 /* wake up thread to signal powerd */ 1721 cv_signal(&sc->sc_rsw_cv); 1722 aprint_error_dev(sc->sc_dev, 1723 "Radio transmitter is off\n"); 1724 /* turn the interface down */ 1725 ifp->if_flags &= ~IFF_UP; 1726 wpi_stop(ifp, 1); 1727 return; /* no further processing */ 1728 } 1729 break; 1730 } 1731 case WPI_START_SCAN: 1732 { 1733 #if 0 1734 struct wpi_start_scan *scan = 1735 (struct wpi_start_scan *)(desc + 1); 1736 1737 DPRINTFN(2, ("scanning channel %d status %x\n", 1738 scan->chan, le32toh(scan->status))); 1739 1740 /* fix current channel */ 1741 ic->ic_curchan = &ic->ic_channels[scan->chan]; 1742 #endif 1743 break; 1744 } 1745 case WPI_STOP_SCAN: 1746 { 1747 #ifdef WPI_DEBUG 1748 struct wpi_stop_scan *scan = 1749 (struct wpi_stop_scan *)(desc + 1); 1750 #endif 1751 1752 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n", 1753 scan->nchan, scan->status, scan->chan)); 1754 1755 sc->is_scanning = false; 1756 if (ic->ic_state == IEEE80211_S_SCAN) 1757 ieee80211_next_scan(ic); 1758 1759 break; 1760 } 1761 } 1762 1763 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT; 1764 } 1765 1766 /* tell the firmware what we have processed */ 1767 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 1768 WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7); 1769 } 1770 1771 static int 1772 wpi_intr(void *arg) 1773 { 1774 struct wpi_softc *sc = arg; 1775 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1776 uint32_t r; 1777 1778 r = WPI_READ(sc, WPI_INTR); 1779 if (r == 0 || r == 0xffffffff) 1780 return 0; /* not for us */ 1781 1782 DPRINTFN(6, ("interrupt reg %x\n", r)); 1783 1784 /* disable interrupts */ 1785 WPI_WRITE(sc, WPI_MASK, 0); 1786 /* ack interrupts */ 1787 WPI_WRITE(sc, WPI_INTR, r); 1788 1789 if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) { 1790 /* SYSTEM FAILURE, SYSTEM FAILURE */ 1791 aprint_error_dev(sc->sc_dev, "fatal firmware error\n"); 1792 ifp->if_flags &= ~IFF_UP; 1793 wpi_stop(ifp, 1); 1794 return 1; 1795 } 1796 1797 if (r & WPI_RX_INTR) 1798 wpi_notif_intr(sc); 1799 1800 if (r & WPI_ALIVE_INTR) /* firmware initialized */ 1801 wakeup(sc); 1802 1803 /* re-enable interrupts */ 1804 if (ifp->if_flags & IFF_UP) 1805 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 1806 1807 return 1; 1808 } 1809 1810 static uint8_t 1811 wpi_plcp_signal(int rate) 1812 { 1813 switch (rate) { 1814 /* CCK rates (returned values are device-dependent) */ 1815 case 2: return 10; 1816 case 4: return 20; 1817 case 11: return 55; 1818 case 22: return 110; 1819 1820 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1821 /* R1-R4, (u)ral is R4-R1 */ 1822 case 12: return 0xd; 1823 case 18: return 0xf; 1824 case 24: return 0x5; 1825 case 36: return 0x7; 1826 case 48: return 0x9; 1827 case 72: return 0xb; 1828 case 96: return 0x1; 1829 case 108: return 0x3; 1830 1831 /* unsupported rates (should not get there) */ 1832 default: return 0; 1833 } 1834 } 1835 1836 /* quickly determine if a given rate is CCK or OFDM */ 1837 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1838 1839 static int 1840 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1841 int ac) 1842 { 1843 struct ieee80211com *ic = &sc->sc_ic; 1844 struct wpi_tx_ring *ring = &sc->txq[ac]; 1845 struct wpi_tx_desc *desc; 1846 struct wpi_tx_data *data; 1847 struct wpi_tx_cmd *cmd; 1848 struct wpi_cmd_data *tx; 1849 struct ieee80211_frame *wh; 1850 struct ieee80211_key *k; 1851 const struct chanAccParams *cap; 1852 struct mbuf *mnew; 1853 int i, rate, error, hdrlen, noack = 0; 1854 1855 desc = &ring->desc[ring->cur]; 1856 data = &ring->data[ring->cur]; 1857 1858 wh = mtod(m0, struct ieee80211_frame *); 1859 1860 if (ieee80211_has_qos(wh)) { 1861 cap = &ic->ic_wme.wme_chanParams; 1862 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1863 } 1864 1865 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1866 k = ieee80211_crypto_encap(ic, ni, m0); 1867 if (k == NULL) { 1868 m_freem(m0); 1869 return ENOBUFS; 1870 } 1871 1872 /* packet header may have moved, reset our local pointer */ 1873 wh = mtod(m0, struct ieee80211_frame *); 1874 } 1875 1876 hdrlen = ieee80211_anyhdrsize(wh); 1877 1878 /* pickup a rate */ 1879 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1880 IEEE80211_FC0_TYPE_MGT) { 1881 /* mgmt frames are sent at the lowest available bit-rate */ 1882 rate = ni->ni_rates.rs_rates[0]; 1883 } else { 1884 if (ic->ic_fixed_rate != -1) { 1885 rate = ic->ic_sup_rates[ic->ic_curmode]. 1886 rs_rates[ic->ic_fixed_rate]; 1887 } else 1888 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 1889 } 1890 rate &= IEEE80211_RATE_VAL; 1891 1892 if (sc->sc_drvbpf != NULL) { 1893 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 1894 1895 tap->wt_flags = 0; 1896 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq); 1897 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags); 1898 tap->wt_rate = rate; 1899 tap->wt_hwqueue = ac; 1900 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 1901 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 1902 1903 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1904 } 1905 1906 cmd = &ring->cmd[ring->cur]; 1907 cmd->code = WPI_CMD_TX_DATA; 1908 cmd->flags = 0; 1909 cmd->qid = ring->qid; 1910 cmd->idx = ring->cur; 1911 1912 tx = (struct wpi_cmd_data *)cmd->data; 1913 /* no need to zero tx, all fields are reinitialized here */ 1914 tx->flags = 0; 1915 1916 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1917 tx->flags |= htole32(WPI_TX_NEED_ACK); 1918 } else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) 1919 tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP); 1920 1921 tx->flags |= htole32(WPI_TX_AUTO_SEQ); 1922 1923 /* retrieve destination node's id */ 1924 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST : 1925 WPI_ID_BSS; 1926 1927 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1928 IEEE80211_FC0_TYPE_MGT) { 1929 /* tell h/w to set timestamp in probe responses */ 1930 if ((wh->i_fc[0] & 1931 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 1932 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) 1933 tx->flags |= htole32(WPI_TX_INSERT_TSTAMP); 1934 1935 if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1936 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) || 1937 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1938 IEEE80211_FC0_SUBTYPE_REASSOC_REQ)) 1939 tx->timeout = htole16(3); 1940 else 1941 tx->timeout = htole16(2); 1942 } else 1943 tx->timeout = htole16(0); 1944 1945 tx->rate = wpi_plcp_signal(rate); 1946 1947 /* be very persistant at sending frames out */ 1948 tx->rts_ntries = 7; 1949 tx->data_ntries = 15; 1950 1951 tx->ofdm_mask = 0xff; 1952 tx->cck_mask = 0x0f; 1953 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 1954 1955 tx->len = htole16(m0->m_pkthdr.len); 1956 1957 /* save and trim IEEE802.11 header */ 1958 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 1959 m_adj(m0, hdrlen); 1960 1961 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 1962 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 1963 if (error != 0 && error != EFBIG) { 1964 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", 1965 error); 1966 m_freem(m0); 1967 return error; 1968 } 1969 if (error != 0) { 1970 /* too many fragments, linearize */ 1971 1972 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 1973 if (mnew == NULL) { 1974 m_freem(m0); 1975 return ENOMEM; 1976 } 1977 M_COPY_PKTHDR(mnew, m0); 1978 if (m0->m_pkthdr.len > MHLEN) { 1979 MCLGET(mnew, M_DONTWAIT); 1980 if (!(mnew->m_flags & M_EXT)) { 1981 m_freem(m0); 1982 m_freem(mnew); 1983 return ENOMEM; 1984 } 1985 } 1986 1987 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *)); 1988 m_freem(m0); 1989 mnew->m_len = mnew->m_pkthdr.len; 1990 m0 = mnew; 1991 1992 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 1993 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 1994 if (error != 0) { 1995 aprint_error_dev(sc->sc_dev, 1996 "could not map mbuf (error %d)\n", error); 1997 m_freem(m0); 1998 return error; 1999 } 2000 } 2001 2002 data->m = m0; 2003 data->ni = ni; 2004 2005 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n", 2006 ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs)); 2007 2008 /* first scatter/gather segment is used by the tx data command */ 2009 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2010 (1 + data->map->dm_nsegs) << 24); 2011 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2012 ring->cur * sizeof (struct wpi_tx_cmd)); 2013 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data) + 2014 ((hdrlen + 3) & ~3)); 2015 for (i = 1; i <= data->map->dm_nsegs; i++) { 2016 desc->segs[i].addr = 2017 htole32(data->map->dm_segs[i - 1].ds_addr); 2018 desc->segs[i].len = 2019 htole32(data->map->dm_segs[i - 1].ds_len); 2020 } 2021 2022 ring->queued++; 2023 2024 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 2025 data->map->dm_mapsize, 2026 BUS_DMASYNC_PREWRITE); 2027 bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map, 0, 2028 ring->cmd_dma.size, 2029 BUS_DMASYNC_PREWRITE); 2030 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, 2031 ring->desc_dma.size, 2032 BUS_DMASYNC_PREWRITE); 2033 2034 /* kick ring */ 2035 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2036 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2037 2038 return 0; 2039 } 2040 2041 static void 2042 wpi_start(struct ifnet *ifp) 2043 { 2044 struct wpi_softc *sc = ifp->if_softc; 2045 struct ieee80211com *ic = &sc->sc_ic; 2046 struct ieee80211_node *ni; 2047 struct ether_header *eh; 2048 struct mbuf *m0; 2049 int ac; 2050 2051 /* 2052 * net80211 may still try to send management frames even if the 2053 * IFF_RUNNING flag is not set... 2054 */ 2055 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 2056 return; 2057 2058 for (;;) { 2059 IF_DEQUEUE(&ic->ic_mgtq, m0); 2060 if (m0 != NULL) { 2061 2062 ni = M_GETCTX(m0, struct ieee80211_node *); 2063 M_CLEARCTX(m0); 2064 2065 /* management frames go into ring 0 */ 2066 if (sc->txq[0].queued > sc->txq[0].count - 8) { 2067 ifp->if_oerrors++; 2068 continue; 2069 } 2070 bpf_mtap3(ic->ic_rawbpf, m0); 2071 if (wpi_tx_data(sc, m0, ni, 0) != 0) { 2072 ifp->if_oerrors++; 2073 break; 2074 } 2075 } else { 2076 if (ic->ic_state != IEEE80211_S_RUN) 2077 break; 2078 IFQ_POLL(&ifp->if_snd, m0); 2079 if (m0 == NULL) 2080 break; 2081 2082 if (m0->m_len < sizeof (*eh) && 2083 (m0 = m_pullup(m0, sizeof (*eh))) == NULL) { 2084 ifp->if_oerrors++; 2085 continue; 2086 } 2087 eh = mtod(m0, struct ether_header *); 2088 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2089 if (ni == NULL) { 2090 m_freem(m0); 2091 ifp->if_oerrors++; 2092 continue; 2093 } 2094 2095 /* classify mbuf so we can find which tx ring to use */ 2096 if (ieee80211_classify(ic, m0, ni) != 0) { 2097 m_freem(m0); 2098 ieee80211_free_node(ni); 2099 ifp->if_oerrors++; 2100 continue; 2101 } 2102 2103 /* no QoS encapsulation for EAPOL frames */ 2104 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 2105 M_WME_GETAC(m0) : WME_AC_BE; 2106 2107 if (sc->txq[ac].queued > sc->txq[ac].count - 8) { 2108 /* there is no place left in this ring */ 2109 ifp->if_flags |= IFF_OACTIVE; 2110 break; 2111 } 2112 IFQ_DEQUEUE(&ifp->if_snd, m0); 2113 bpf_mtap(ifp, m0); 2114 m0 = ieee80211_encap(ic, m0, ni); 2115 if (m0 == NULL) { 2116 ieee80211_free_node(ni); 2117 ifp->if_oerrors++; 2118 continue; 2119 } 2120 bpf_mtap3(ic->ic_rawbpf, m0); 2121 if (wpi_tx_data(sc, m0, ni, ac) != 0) { 2122 ieee80211_free_node(ni); 2123 ifp->if_oerrors++; 2124 break; 2125 } 2126 } 2127 2128 sc->sc_tx_timer = 5; 2129 ifp->if_timer = 1; 2130 } 2131 } 2132 2133 static void 2134 wpi_watchdog(struct ifnet *ifp) 2135 { 2136 struct wpi_softc *sc = ifp->if_softc; 2137 2138 ifp->if_timer = 0; 2139 2140 if (sc->sc_tx_timer > 0) { 2141 if (--sc->sc_tx_timer == 0) { 2142 aprint_error_dev(sc->sc_dev, "device timeout\n"); 2143 ifp->if_flags &= ~IFF_UP; 2144 wpi_stop(ifp, 1); 2145 ifp->if_oerrors++; 2146 return; 2147 } 2148 ifp->if_timer = 1; 2149 } 2150 2151 ieee80211_watchdog(&sc->sc_ic); 2152 } 2153 2154 static int 2155 wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data) 2156 { 2157 #define IS_RUNNING(ifp) \ 2158 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING)) 2159 2160 struct wpi_softc *sc = ifp->if_softc; 2161 struct ieee80211com *ic = &sc->sc_ic; 2162 int s, error = 0; 2163 2164 s = splnet(); 2165 2166 switch (cmd) { 2167 case SIOCSIFFLAGS: 2168 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 2169 break; 2170 if (ifp->if_flags & IFF_UP) { 2171 if (!(ifp->if_flags & IFF_RUNNING)) 2172 wpi_init(ifp); 2173 } else { 2174 if (ifp->if_flags & IFF_RUNNING) 2175 wpi_stop(ifp, 1); 2176 } 2177 break; 2178 2179 case SIOCADDMULTI: 2180 case SIOCDELMULTI: 2181 /* XXX no h/w multicast filter? --dyoung */ 2182 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 2183 /* setup multicast filter, etc */ 2184 error = 0; 2185 } 2186 break; 2187 2188 default: 2189 error = ieee80211_ioctl(ic, cmd, data); 2190 } 2191 2192 if (error == ENETRESET) { 2193 if (IS_RUNNING(ifp) && 2194 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 2195 wpi_init(ifp); 2196 error = 0; 2197 } 2198 2199 splx(s); 2200 return error; 2201 2202 #undef IS_RUNNING 2203 } 2204 2205 /* 2206 * Extract various information from EEPROM. 2207 */ 2208 static void 2209 wpi_read_eeprom(struct wpi_softc *sc) 2210 { 2211 struct ieee80211com *ic = &sc->sc_ic; 2212 char domain[4]; 2213 int i; 2214 2215 wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1); 2216 wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2); 2217 wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1); 2218 2219 DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, le16toh(sc->rev), 2220 sc->type)); 2221 2222 /* read and print regulatory domain */ 2223 wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4); 2224 aprint_normal_dev(sc->sc_dev, "%.4s", domain); 2225 2226 /* read and print MAC address */ 2227 wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6); 2228 aprint_normal(", address %s\n", ether_sprintf(ic->ic_myaddr)); 2229 2230 /* read the list of authorized channels */ 2231 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++) 2232 wpi_read_eeprom_channels(sc, i); 2233 2234 /* read the list of power groups */ 2235 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++) 2236 wpi_read_eeprom_group(sc, i); 2237 } 2238 2239 static void 2240 wpi_read_eeprom_channels(struct wpi_softc *sc, int n) 2241 { 2242 struct ieee80211com *ic = &sc->sc_ic; 2243 const struct wpi_chan_band *band = &wpi_bands[n]; 2244 struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND]; 2245 int chan, i; 2246 2247 wpi_read_prom_data(sc, band->addr, channels, 2248 band->nchan * sizeof (struct wpi_eeprom_chan)); 2249 2250 for (i = 0; i < band->nchan; i++) { 2251 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) 2252 continue; 2253 2254 chan = band->chan[i]; 2255 2256 if (n == 0) { /* 2GHz band */ 2257 ic->ic_channels[chan].ic_freq = 2258 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 2259 ic->ic_channels[chan].ic_flags = 2260 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 2261 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 2262 2263 } else { /* 5GHz band */ 2264 /* 2265 * Some 3945ABG adapters support channels 7, 8, 11 2266 * and 12 in the 2GHz *and* 5GHz bands. 2267 * Because of limitations in our net80211(9) stack, 2268 * we can't support these channels in 5GHz band. 2269 */ 2270 if (chan <= 14) 2271 continue; 2272 2273 ic->ic_channels[chan].ic_freq = 2274 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 2275 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A; 2276 } 2277 2278 /* is active scan allowed on this channel? */ 2279 if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) { 2280 ic->ic_channels[chan].ic_flags |= 2281 IEEE80211_CHAN_PASSIVE; 2282 } 2283 2284 /* save maximum allowed power for this channel */ 2285 sc->maxpwr[chan] = channels[i].maxpwr; 2286 2287 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n", 2288 chan, channels[i].flags, sc->maxpwr[chan])); 2289 } 2290 } 2291 2292 static void 2293 wpi_read_eeprom_group(struct wpi_softc *sc, int n) 2294 { 2295 struct wpi_power_group *group = &sc->groups[n]; 2296 struct wpi_eeprom_group rgroup; 2297 int i; 2298 2299 wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup, 2300 sizeof rgroup); 2301 2302 /* save power group information */ 2303 group->chan = rgroup.chan; 2304 group->maxpwr = rgroup.maxpwr; 2305 /* temperature at which the samples were taken */ 2306 group->temp = (int16_t)le16toh(rgroup.temp); 2307 2308 DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n, 2309 group->chan, group->maxpwr, group->temp)); 2310 2311 for (i = 0; i < WPI_SAMPLES_COUNT; i++) { 2312 group->samples[i].index = rgroup.samples[i].index; 2313 group->samples[i].power = rgroup.samples[i].power; 2314 2315 DPRINTF(("\tsample %d: index=%d power=%d\n", i, 2316 group->samples[i].index, group->samples[i].power)); 2317 } 2318 } 2319 2320 /* 2321 * Send a command to the firmware. 2322 */ 2323 static int 2324 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async) 2325 { 2326 struct wpi_tx_ring *ring = &sc->cmdq; 2327 struct wpi_tx_desc *desc; 2328 struct wpi_tx_cmd *cmd; 2329 struct wpi_dma_info *dma; 2330 2331 KASSERT(size <= sizeof cmd->data); 2332 2333 desc = &ring->desc[ring->cur]; 2334 cmd = &ring->cmd[ring->cur]; 2335 2336 cmd->code = code; 2337 cmd->flags = 0; 2338 cmd->qid = ring->qid; 2339 cmd->idx = ring->cur; 2340 memcpy(cmd->data, buf, size); 2341 2342 dma = &ring->cmd_dma; 2343 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE); 2344 2345 desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24); 2346 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2347 ring->cur * sizeof (struct wpi_tx_cmd)); 2348 desc->segs[0].len = htole32(4 + size); 2349 2350 dma = &ring->desc_dma; 2351 bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE); 2352 2353 /* kick cmd ring */ 2354 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2355 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2356 2357 return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz); 2358 } 2359 2360 static int 2361 wpi_wme_update(struct ieee80211com *ic) 2362 { 2363 #define WPI_EXP2(v) htole16((1 << (v)) - 1) 2364 #define WPI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 2365 struct wpi_softc *sc = ic->ic_ifp->if_softc; 2366 const struct wmeParams *wmep; 2367 struct wpi_wme_setup wme; 2368 int ac; 2369 2370 /* don't override default WME values if WME is not actually enabled */ 2371 if (!(ic->ic_flags & IEEE80211_F_WME)) 2372 return 0; 2373 2374 wme.flags = 0; 2375 for (ac = 0; ac < WME_NUM_AC; ac++) { 2376 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 2377 wme.ac[ac].aifsn = wmep->wmep_aifsn; 2378 wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin); 2379 wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax); 2380 wme.ac[ac].txop = WPI_USEC(wmep->wmep_txopLimit); 2381 2382 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 2383 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin, 2384 wme.ac[ac].cwmax, wme.ac[ac].txop)); 2385 } 2386 2387 return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1); 2388 #undef WPI_USEC 2389 #undef WPI_EXP2 2390 } 2391 2392 /* 2393 * Configure h/w multi-rate retries. 2394 */ 2395 static int 2396 wpi_mrr_setup(struct wpi_softc *sc) 2397 { 2398 struct ieee80211com *ic = &sc->sc_ic; 2399 struct wpi_mrr_setup mrr; 2400 int i, error; 2401 2402 /* CCK rates (not used with 802.11a) */ 2403 for (i = WPI_CCK1; i <= WPI_CCK11; i++) { 2404 mrr.rates[i].flags = 0; 2405 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 2406 /* fallback to the immediate lower CCK rate (if any) */ 2407 mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1; 2408 /* try one time at this rate before falling back to "next" */ 2409 mrr.rates[i].ntries = 1; 2410 } 2411 2412 /* OFDM rates (not used with 802.11b) */ 2413 for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) { 2414 mrr.rates[i].flags = 0; 2415 mrr.rates[i].plcp = wpi_ridx_to_plcp[i]; 2416 /* fallback to the immediate lower rate (if any) */ 2417 /* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */ 2418 mrr.rates[i].next = (i == WPI_OFDM6) ? 2419 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 2420 WPI_OFDM6 : WPI_CCK2) : 2421 i - 1; 2422 /* try one time at this rate before falling back to "next" */ 2423 mrr.rates[i].ntries = 1; 2424 } 2425 2426 /* setup MRR for control frames */ 2427 mrr.which = htole32(WPI_MRR_CTL); 2428 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2429 if (error != 0) { 2430 aprint_error_dev(sc->sc_dev, 2431 "could not setup MRR for control frames\n"); 2432 return error; 2433 } 2434 2435 /* setup MRR for data frames */ 2436 mrr.which = htole32(WPI_MRR_DATA); 2437 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2438 if (error != 0) { 2439 aprint_error_dev(sc->sc_dev, 2440 "could not setup MRR for data frames\n"); 2441 return error; 2442 } 2443 2444 return 0; 2445 } 2446 2447 static void 2448 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2449 { 2450 struct wpi_cmd_led led; 2451 2452 led.which = which; 2453 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2454 led.off = off; 2455 led.on = on; 2456 2457 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 2458 } 2459 2460 static void 2461 wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni) 2462 { 2463 struct wpi_cmd_tsf tsf; 2464 uint64_t val, mod; 2465 2466 memset(&tsf, 0, sizeof tsf); 2467 memcpy(&tsf.tstamp, ni->ni_tstamp.data, sizeof (uint64_t)); 2468 tsf.bintval = htole16(ni->ni_intval); 2469 tsf.lintval = htole16(10); 2470 2471 /* compute remaining time until next beacon */ 2472 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */ 2473 mod = le64toh(tsf.tstamp) % val; 2474 tsf.binitval = htole32((uint32_t)(val - mod)); 2475 2476 DPRINTF(("TSF bintval=%u tstamp=%" PRIu64 ", init=%u\n", 2477 ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod))); 2478 2479 if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0) 2480 aprint_error_dev(sc->sc_dev, "could not enable TSF\n"); 2481 } 2482 2483 /* 2484 * Update Tx power to match what is defined for channel `c'. 2485 */ 2486 static int 2487 wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async) 2488 { 2489 struct ieee80211com *ic = &sc->sc_ic; 2490 struct wpi_power_group *group; 2491 struct wpi_cmd_txpower txpower; 2492 u_int chan; 2493 int i; 2494 2495 /* get channel number */ 2496 chan = ieee80211_chan2ieee(ic, c); 2497 2498 /* find the power group to which this channel belongs */ 2499 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2500 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 2501 if (chan <= group->chan) 2502 break; 2503 } else 2504 group = &sc->groups[0]; 2505 2506 memset(&txpower, 0, sizeof txpower); 2507 txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1; 2508 txpower.chan = htole16(chan); 2509 2510 /* set Tx power for all OFDM and CCK rates */ 2511 for (i = 0; i <= 11 ; i++) { 2512 /* retrieve Tx power for this channel/rate combination */ 2513 int idx = wpi_get_power_index(sc, group, c, 2514 wpi_ridx_to_rate[i]); 2515 2516 txpower.rates[i].plcp = wpi_ridx_to_plcp[i]; 2517 2518 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2519 txpower.rates[i].rf_gain = wpi_rf_gain_5ghz[idx]; 2520 txpower.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx]; 2521 } else { 2522 txpower.rates[i].rf_gain = wpi_rf_gain_2ghz[idx]; 2523 txpower.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx]; 2524 } 2525 DPRINTF(("chan %d/rate %d: power index %d\n", chan, 2526 wpi_ridx_to_rate[i], idx)); 2527 } 2528 2529 return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async); 2530 } 2531 2532 /* 2533 * Determine Tx power index for a given channel/rate combination. 2534 * This takes into account the regulatory information from EEPROM and the 2535 * current temperature. 2536 */ 2537 static int 2538 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 2539 struct ieee80211_channel *c, int rate) 2540 { 2541 /* fixed-point arithmetic division using a n-bit fractional part */ 2542 #define fdivround(a, b, n) \ 2543 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 2544 2545 /* linear interpolation */ 2546 #define interpolate(x, x1, y1, x2, y2, n) \ 2547 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 2548 2549 struct ieee80211com *ic = &sc->sc_ic; 2550 struct wpi_power_sample *sample; 2551 int pwr, idx; 2552 u_int chan; 2553 2554 /* get channel number */ 2555 chan = ieee80211_chan2ieee(ic, c); 2556 2557 /* default power is group's maximum power - 3dB */ 2558 pwr = group->maxpwr / 2; 2559 2560 /* decrease power for highest OFDM rates to reduce distortion */ 2561 switch (rate) { 2562 case 72: /* 36Mb/s */ 2563 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5; 2564 break; 2565 case 96: /* 48Mb/s */ 2566 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10; 2567 break; 2568 case 108: /* 54Mb/s */ 2569 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12; 2570 break; 2571 } 2572 2573 /* never exceed channel's maximum allowed Tx power */ 2574 pwr = min(pwr, sc->maxpwr[chan]); 2575 2576 /* retrieve power index into gain tables from samples */ 2577 for (sample = group->samples; sample < &group->samples[3]; sample++) 2578 if (pwr > sample[1].power) 2579 break; 2580 /* fixed-point linear interpolation using a 19-bit fractional part */ 2581 idx = interpolate(pwr, sample[0].power, sample[0].index, 2582 sample[1].power, sample[1].index, 19); 2583 2584 /*- 2585 * Adjust power index based on current temperature: 2586 * - if cooler than factory-calibrated: decrease output power 2587 * - if warmer than factory-calibrated: increase output power 2588 */ 2589 idx -= (sc->temp - group->temp) * 11 / 100; 2590 2591 /* decrease power for CCK rates (-5dB) */ 2592 if (!WPI_RATE_IS_OFDM(rate)) 2593 idx += 10; 2594 2595 /* keep power index in a valid range */ 2596 if (idx < 0) 2597 return 0; 2598 if (idx > WPI_MAX_PWR_INDEX) 2599 return WPI_MAX_PWR_INDEX; 2600 return idx; 2601 2602 #undef interpolate 2603 #undef fdivround 2604 } 2605 2606 /* 2607 * Build a beacon frame that the firmware will broadcast periodically in 2608 * IBSS or HostAP modes. 2609 */ 2610 static int 2611 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni) 2612 { 2613 struct ieee80211com *ic = &sc->sc_ic; 2614 struct wpi_tx_ring *ring = &sc->cmdq; 2615 struct wpi_tx_desc *desc; 2616 struct wpi_tx_data *data; 2617 struct wpi_tx_cmd *cmd; 2618 struct wpi_cmd_beacon *bcn; 2619 struct ieee80211_beacon_offsets bo; 2620 struct mbuf *m0; 2621 int error; 2622 2623 desc = &ring->desc[ring->cur]; 2624 data = &ring->data[ring->cur]; 2625 2626 m0 = ieee80211_beacon_alloc(ic, ni, &bo); 2627 if (m0 == NULL) { 2628 aprint_error_dev(sc->sc_dev, 2629 "could not allocate beacon frame\n"); 2630 return ENOMEM; 2631 } 2632 2633 cmd = &ring->cmd[ring->cur]; 2634 cmd->code = WPI_CMD_SET_BEACON; 2635 cmd->flags = 0; 2636 cmd->qid = ring->qid; 2637 cmd->idx = ring->cur; 2638 2639 bcn = (struct wpi_cmd_beacon *)cmd->data; 2640 memset(bcn, 0, sizeof (struct wpi_cmd_beacon)); 2641 bcn->id = WPI_ID_BROADCAST; 2642 bcn->ofdm_mask = 0xff; 2643 bcn->cck_mask = 0x0f; 2644 bcn->lifetime = htole32(WPI_LIFETIME_INFINITE); 2645 bcn->len = htole16(m0->m_pkthdr.len); 2646 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2647 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2648 bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP); 2649 2650 /* save and trim IEEE802.11 header */ 2651 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh); 2652 m_adj(m0, sizeof (struct ieee80211_frame)); 2653 2654 /* assume beacon frame is contiguous */ 2655 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 2656 BUS_DMA_READ | BUS_DMA_NOWAIT); 2657 if (error != 0) { 2658 aprint_error_dev(sc->sc_dev, "could not map beacon\n"); 2659 m_freem(m0); 2660 return error; 2661 } 2662 2663 data->m = m0; 2664 2665 /* first scatter/gather segment is used by the beacon command */ 2666 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24); 2667 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2668 ring->cur * sizeof (struct wpi_tx_cmd)); 2669 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_beacon)); 2670 desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr); 2671 desc->segs[1].len = htole32(data->map->dm_segs[0].ds_len); 2672 2673 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, 2674 ring->desc_dma.map->dm_mapsize, BUS_DMASYNC_PREWRITE); 2675 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 2676 BUS_DMASYNC_PREWRITE); 2677 2678 /* kick cmd ring */ 2679 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2680 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2681 2682 return 0; 2683 } 2684 2685 static int 2686 wpi_auth(struct wpi_softc *sc) 2687 { 2688 struct ieee80211com *ic = &sc->sc_ic; 2689 struct ieee80211_node *ni = ic->ic_bss; 2690 struct wpi_node_info node; 2691 int error; 2692 2693 /* update adapter's configuration */ 2694 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid); 2695 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2696 sc->config.flags = htole32(WPI_CONFIG_TSF); 2697 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2698 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2699 WPI_CONFIG_24GHZ); 2700 } 2701 switch (ic->ic_curmode) { 2702 case IEEE80211_MODE_11A: 2703 sc->config.cck_mask = 0; 2704 sc->config.ofdm_mask = 0x15; 2705 break; 2706 case IEEE80211_MODE_11B: 2707 sc->config.cck_mask = 0x03; 2708 sc->config.ofdm_mask = 0; 2709 break; 2710 default: /* assume 802.11b/g */ 2711 sc->config.cck_mask = 0x0f; 2712 sc->config.ofdm_mask = 0x15; 2713 } 2714 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan, 2715 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask)); 2716 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2717 sizeof (struct wpi_config), 1); 2718 if (error != 0) { 2719 aprint_error_dev(sc->sc_dev, "could not configure\n"); 2720 return error; 2721 } 2722 2723 /* configuration has changed, set Tx power accordingly */ 2724 if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) { 2725 aprint_error_dev(sc->sc_dev, "could not set Tx power\n"); 2726 return error; 2727 } 2728 2729 /* add default node */ 2730 memset(&node, 0, sizeof node); 2731 IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid); 2732 node.id = WPI_ID_BSS; 2733 node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2734 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2735 node.action = htole32(WPI_ACTION_SET_RATE); 2736 node.antenna = WPI_ANTENNA_BOTH; 2737 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2738 if (error != 0) { 2739 aprint_error_dev(sc->sc_dev, "could not add BSS node\n"); 2740 return error; 2741 } 2742 2743 return 0; 2744 } 2745 2746 /* 2747 * Send a scan request to the firmware. Since this command is huge, we map it 2748 * into a mbuf instead of using the pre-allocated set of commands. 2749 */ 2750 static int 2751 wpi_scan(struct wpi_softc *sc) 2752 { 2753 struct ieee80211com *ic = &sc->sc_ic; 2754 struct wpi_tx_ring *ring = &sc->cmdq; 2755 struct wpi_tx_desc *desc; 2756 struct wpi_tx_data *data; 2757 struct wpi_tx_cmd *cmd; 2758 struct wpi_scan_hdr *hdr; 2759 struct wpi_scan_chan *chan; 2760 struct ieee80211_frame *wh; 2761 struct ieee80211_rateset *rs; 2762 struct ieee80211_channel *c; 2763 uint8_t *frm; 2764 int pktlen, error, nrates; 2765 2766 if (ic->ic_curchan == NULL) 2767 return EIO; 2768 2769 desc = &ring->desc[ring->cur]; 2770 data = &ring->data[ring->cur]; 2771 2772 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 2773 if (data->m == NULL) { 2774 aprint_error_dev(sc->sc_dev, 2775 "could not allocate mbuf for scan command\n"); 2776 return ENOMEM; 2777 } 2778 MCLGET(data->m, M_DONTWAIT); 2779 if (!(data->m->m_flags & M_EXT)) { 2780 m_freem(data->m); 2781 data->m = NULL; 2782 aprint_error_dev(sc->sc_dev, 2783 "could not allocate mbuf for scan command\n"); 2784 return ENOMEM; 2785 } 2786 2787 cmd = mtod(data->m, struct wpi_tx_cmd *); 2788 cmd->code = WPI_CMD_SCAN; 2789 cmd->flags = 0; 2790 cmd->qid = ring->qid; 2791 cmd->idx = ring->cur; 2792 2793 hdr = (struct wpi_scan_hdr *)cmd->data; 2794 memset(hdr, 0, sizeof (struct wpi_scan_hdr)); 2795 hdr->cmd.flags = htole32(WPI_TX_AUTO_SEQ); 2796 hdr->cmd.id = WPI_ID_BROADCAST; 2797 hdr->cmd.lifetime = htole32(WPI_LIFETIME_INFINITE); 2798 /* 2799 * Move to the next channel if no packets are received within 5 msecs 2800 * after sending the probe request (this helps to reduce the duration 2801 * of active scans). 2802 */ 2803 hdr->quiet = htole16(5); /* timeout in milliseconds */ 2804 hdr->plcp_threshold = htole16(1); /* min # of packets */ 2805 2806 if (ic->ic_curchan->ic_flags & IEEE80211_CHAN_5GHZ) { 2807 hdr->crc_threshold = htole16(1); 2808 /* send probe requests at 6Mbps */ 2809 hdr->cmd.rate = wpi_plcp_signal(12); 2810 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A]; 2811 } else { 2812 hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO); 2813 /* send probe requests at 1Mbps */ 2814 hdr->cmd.rate = wpi_plcp_signal(2); 2815 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 2816 } 2817 2818 /* for directed scans, firmware inserts the essid IE itself */ 2819 if (ic->ic_des_esslen != 0) { 2820 hdr->essid[0].id = IEEE80211_ELEMID_SSID; 2821 hdr->essid[0].len = ic->ic_des_esslen; 2822 memcpy(hdr->essid[0].data, ic->ic_des_essid, ic->ic_des_esslen); 2823 } 2824 2825 /* 2826 * Build a probe request frame. Most of the following code is a 2827 * copy & paste of what is done in net80211. 2828 */ 2829 wh = (struct ieee80211_frame *)(hdr + 1); 2830 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2831 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 2832 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2833 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr); 2834 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 2835 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr); 2836 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 2837 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 2838 2839 frm = (uint8_t *)(wh + 1); 2840 2841 /* add empty essid IE (firmware generates it for directed scans) */ 2842 *frm++ = IEEE80211_ELEMID_SSID; 2843 *frm++ = 0; 2844 2845 /* add supported rates IE */ 2846 *frm++ = IEEE80211_ELEMID_RATES; 2847 nrates = rs->rs_nrates; 2848 if (nrates > IEEE80211_RATE_SIZE) 2849 nrates = IEEE80211_RATE_SIZE; 2850 *frm++ = nrates; 2851 memcpy(frm, rs->rs_rates, nrates); 2852 frm += nrates; 2853 2854 /* add supported xrates IE */ 2855 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 2856 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 2857 *frm++ = IEEE80211_ELEMID_XRATES; 2858 *frm++ = nrates; 2859 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 2860 frm += nrates; 2861 } 2862 2863 /* setup length of probe request */ 2864 hdr->cmd.len = htole16(frm - (uint8_t *)wh); 2865 2866 chan = (struct wpi_scan_chan *)frm; 2867 c = ic->ic_curchan; 2868 2869 chan->chan = ieee80211_chan2ieee(ic, c); 2870 chan->flags = 0; 2871 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 2872 chan->flags |= WPI_CHAN_ACTIVE; 2873 if (ic->ic_des_esslen != 0) 2874 chan->flags |= WPI_CHAN_DIRECT; 2875 } 2876 chan->dsp_gain = 0x6e; 2877 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2878 chan->rf_gain = 0x3b; 2879 chan->active = htole16(10); 2880 chan->passive = htole16(110); 2881 } else { 2882 chan->rf_gain = 0x28; 2883 chan->active = htole16(20); 2884 chan->passive = htole16(120); 2885 } 2886 hdr->nchan++; 2887 chan++; 2888 2889 frm += sizeof (struct wpi_scan_chan); 2890 2891 hdr->len = htole16(frm - (uint8_t *)hdr); 2892 pktlen = frm - (uint8_t *)cmd; 2893 2894 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen, NULL, 2895 BUS_DMA_NOWAIT); 2896 if (error != 0) { 2897 aprint_error_dev(sc->sc_dev, "could not map scan command\n"); 2898 m_freem(data->m); 2899 data->m = NULL; 2900 return error; 2901 } 2902 2903 desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24); 2904 desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr); 2905 desc->segs[0].len = htole32(data->map->dm_segs[0].ds_len); 2906 2907 bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, 2908 ring->desc_dma.map->dm_mapsize, BUS_DMASYNC_PREWRITE); 2909 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 2910 BUS_DMASYNC_PREWRITE); 2911 2912 /* kick cmd ring */ 2913 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2914 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2915 2916 return 0; /* will be notified async. of failure/success */ 2917 } 2918 2919 static int 2920 wpi_config(struct wpi_softc *sc) 2921 { 2922 struct ieee80211com *ic = &sc->sc_ic; 2923 struct ifnet *ifp = ic->ic_ifp; 2924 struct wpi_power power; 2925 struct wpi_bluetooth bluetooth; 2926 struct wpi_node_info node; 2927 int error; 2928 2929 memset(&power, 0, sizeof power); 2930 power.flags = htole32(WPI_POWER_CAM | 0x8); 2931 error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0); 2932 if (error != 0) { 2933 aprint_error_dev(sc->sc_dev, "could not set power mode\n"); 2934 return error; 2935 } 2936 2937 /* configure bluetooth coexistence */ 2938 memset(&bluetooth, 0, sizeof bluetooth); 2939 bluetooth.flags = 3; 2940 bluetooth.lead = 0xaa; 2941 bluetooth.kill = 1; 2942 error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth, 2943 0); 2944 if (error != 0) { 2945 aprint_error_dev(sc->sc_dev, 2946 "could not configure bluetooth coexistence\n"); 2947 return error; 2948 } 2949 2950 /* configure adapter */ 2951 memset(&sc->config, 0, sizeof (struct wpi_config)); 2952 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 2953 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr); 2954 /* set default channel */ 2955 sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_curchan); 2956 sc->config.flags = htole32(WPI_CONFIG_TSF); 2957 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2958 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2959 WPI_CONFIG_24GHZ); 2960 } 2961 sc->config.filter = 0; 2962 switch (ic->ic_opmode) { 2963 case IEEE80211_M_STA: 2964 sc->config.mode = WPI_MODE_STA; 2965 sc->config.filter |= htole32(WPI_FILTER_MULTICAST); 2966 break; 2967 case IEEE80211_M_IBSS: 2968 case IEEE80211_M_AHDEMO: 2969 sc->config.mode = WPI_MODE_IBSS; 2970 break; 2971 case IEEE80211_M_HOSTAP: 2972 sc->config.mode = WPI_MODE_HOSTAP; 2973 break; 2974 case IEEE80211_M_MONITOR: 2975 sc->config.mode = WPI_MODE_MONITOR; 2976 sc->config.filter |= htole32(WPI_FILTER_MULTICAST | 2977 WPI_FILTER_CTL | WPI_FILTER_PROMISC); 2978 break; 2979 } 2980 sc->config.cck_mask = 0x0f; /* not yet negotiated */ 2981 sc->config.ofdm_mask = 0xff; /* not yet negotiated */ 2982 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2983 sizeof (struct wpi_config), 0); 2984 if (error != 0) { 2985 aprint_error_dev(sc->sc_dev, "configure command failed\n"); 2986 return error; 2987 } 2988 2989 /* configuration has changed, set Tx power accordingly */ 2990 if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) { 2991 aprint_error_dev(sc->sc_dev, "could not set Tx power\n"); 2992 return error; 2993 } 2994 2995 /* add broadcast node */ 2996 memset(&node, 0, sizeof node); 2997 IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr); 2998 node.id = WPI_ID_BROADCAST; 2999 node.rate = wpi_plcp_signal(2); 3000 node.action = htole32(WPI_ACTION_SET_RATE); 3001 node.antenna = WPI_ANTENNA_BOTH; 3002 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0); 3003 if (error != 0) { 3004 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n"); 3005 return error; 3006 } 3007 3008 if ((error = wpi_mrr_setup(sc)) != 0) { 3009 aprint_error_dev(sc->sc_dev, "could not setup MRR\n"); 3010 return error; 3011 } 3012 3013 return 0; 3014 } 3015 3016 static void 3017 wpi_stop_master(struct wpi_softc *sc) 3018 { 3019 uint32_t tmp; 3020 int ntries; 3021 3022 tmp = WPI_READ(sc, WPI_RESET); 3023 WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER); 3024 3025 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3026 if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP) 3027 return; /* already asleep */ 3028 3029 for (ntries = 0; ntries < 100; ntries++) { 3030 if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED) 3031 break; 3032 DELAY(10); 3033 } 3034 if (ntries == 100) { 3035 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n"); 3036 } 3037 } 3038 3039 static int 3040 wpi_power_up(struct wpi_softc *sc) 3041 { 3042 uint32_t tmp; 3043 int ntries; 3044 3045 wpi_mem_lock(sc); 3046 tmp = wpi_mem_read(sc, WPI_MEM_POWER); 3047 wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000); 3048 wpi_mem_unlock(sc); 3049 3050 for (ntries = 0; ntries < 5000; ntries++) { 3051 if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED) 3052 break; 3053 DELAY(10); 3054 } 3055 if (ntries == 5000) { 3056 aprint_error_dev(sc->sc_dev, 3057 "timeout waiting for NIC to power up\n"); 3058 return ETIMEDOUT; 3059 } 3060 return 0; 3061 } 3062 3063 static int 3064 wpi_reset(struct wpi_softc *sc) 3065 { 3066 uint32_t tmp; 3067 int ntries; 3068 3069 /* clear any pending interrupts */ 3070 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3071 3072 tmp = WPI_READ(sc, WPI_PLL_CTL); 3073 WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT); 3074 3075 tmp = WPI_READ(sc, WPI_CHICKEN); 3076 WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS); 3077 3078 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3079 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT); 3080 3081 /* wait for clock stabilization */ 3082 for (ntries = 0; ntries < 1000; ntries++) { 3083 if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK) 3084 break; 3085 DELAY(10); 3086 } 3087 if (ntries == 1000) { 3088 aprint_error_dev(sc->sc_dev, 3089 "timeout waiting for clock stabilization\n"); 3090 return ETIMEDOUT; 3091 } 3092 3093 /* initialize EEPROM */ 3094 tmp = WPI_READ(sc, WPI_EEPROM_STATUS); 3095 if ((tmp & WPI_EEPROM_VERSION) == 0) { 3096 aprint_error_dev(sc->sc_dev, "EEPROM not found\n"); 3097 return EIO; 3098 } 3099 WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED); 3100 3101 return 0; 3102 } 3103 3104 static void 3105 wpi_hw_config(struct wpi_softc *sc) 3106 { 3107 uint32_t rev, hw; 3108 3109 /* voodoo from the reference driver */ 3110 hw = WPI_READ(sc, WPI_HWCONFIG); 3111 3112 rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG); 3113 rev = PCI_REVISION(rev); 3114 if ((rev & 0xc0) == 0x40) 3115 hw |= WPI_HW_ALM_MB; 3116 else if (!(rev & 0x80)) 3117 hw |= WPI_HW_ALM_MM; 3118 3119 if (sc->cap == 0x80) 3120 hw |= WPI_HW_SKU_MRC; 3121 3122 hw &= ~WPI_HW_REV_D; 3123 if ((le16toh(sc->rev) & 0xf0) == 0xd0) 3124 hw |= WPI_HW_REV_D; 3125 3126 if (sc->type > 1) 3127 hw |= WPI_HW_TYPE_B; 3128 3129 DPRINTF(("setting h/w config %x\n", hw)); 3130 WPI_WRITE(sc, WPI_HWCONFIG, hw); 3131 } 3132 3133 static int 3134 wpi_init(struct ifnet *ifp) 3135 { 3136 struct wpi_softc *sc = ifp->if_softc; 3137 struct ieee80211com *ic = &sc->sc_ic; 3138 uint32_t tmp; 3139 int qid, ntries, error; 3140 3141 wpi_stop(ifp,1); 3142 (void)wpi_reset(sc); 3143 3144 wpi_mem_lock(sc); 3145 wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00); 3146 DELAY(20); 3147 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV); 3148 wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800); 3149 wpi_mem_unlock(sc); 3150 3151 (void)wpi_power_up(sc); 3152 wpi_hw_config(sc); 3153 3154 /* init Rx ring */ 3155 wpi_mem_lock(sc); 3156 WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr); 3157 WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr + 3158 offsetof(struct wpi_shared, next)); 3159 WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7); 3160 WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010); 3161 wpi_mem_unlock(sc); 3162 3163 /* init Tx rings */ 3164 wpi_mem_lock(sc); 3165 wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */ 3166 wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */ 3167 wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */ 3168 wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000); 3169 wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002); 3170 wpi_mem_write(sc, WPI_MEM_MAGIC4, 4); 3171 wpi_mem_write(sc, WPI_MEM_MAGIC5, 5); 3172 3173 WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr); 3174 WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5); 3175 3176 for (qid = 0; qid < 6; qid++) { 3177 WPI_WRITE(sc, WPI_TX_CTL(qid), 0); 3178 WPI_WRITE(sc, WPI_TX_BASE(qid), 0); 3179 WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008); 3180 } 3181 wpi_mem_unlock(sc); 3182 3183 /* clear "radio off" and "disable command" bits (reversed logic) */ 3184 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3185 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD); 3186 3187 /* clear any pending interrupts */ 3188 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3189 /* enable interrupts */ 3190 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 3191 3192 /* not sure why/if this is necessary... */ 3193 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3194 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3195 3196 if ((error = wpi_load_firmware(sc)) != 0) 3197 /* wpi_load_firmware prints error messages for us. */ 3198 goto fail1; 3199 3200 /* Check the status of the radio switch */ 3201 mutex_enter(&sc->sc_rsw_mtx); 3202 if (wpi_getrfkill(sc)) { 3203 mutex_exit(&sc->sc_rsw_mtx); 3204 aprint_error_dev(sc->sc_dev, 3205 "radio is disabled by hardware switch\n"); 3206 ifp->if_flags &= ~IFF_UP; 3207 error = EBUSY; 3208 goto fail1; 3209 } 3210 mutex_exit(&sc->sc_rsw_mtx); 3211 3212 /* wait for thermal sensors to calibrate */ 3213 for (ntries = 0; ntries < 1000; ntries++) { 3214 if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0) 3215 break; 3216 DELAY(10); 3217 } 3218 if (ntries == 1000) { 3219 aprint_error_dev(sc->sc_dev, 3220 "timeout waiting for thermal sensors calibration\n"); 3221 error = ETIMEDOUT; 3222 goto fail1; 3223 } 3224 DPRINTF(("temperature %d\n", sc->temp)); 3225 3226 if ((error = wpi_config(sc)) != 0) { 3227 aprint_error_dev(sc->sc_dev, "could not configure device\n"); 3228 goto fail1; 3229 } 3230 3231 ifp->if_flags &= ~IFF_OACTIVE; 3232 ifp->if_flags |= IFF_RUNNING; 3233 3234 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 3235 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 3236 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3237 } 3238 else 3239 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3240 3241 return 0; 3242 3243 fail1: wpi_stop(ifp, 1); 3244 return error; 3245 } 3246 3247 static void 3248 wpi_stop(struct ifnet *ifp, int disable) 3249 { 3250 struct wpi_softc *sc = ifp->if_softc; 3251 struct ieee80211com *ic = &sc->sc_ic; 3252 uint32_t tmp; 3253 int ac; 3254 3255 ifp->if_timer = sc->sc_tx_timer = 0; 3256 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 3257 3258 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3259 3260 /* disable interrupts */ 3261 WPI_WRITE(sc, WPI_MASK, 0); 3262 WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK); 3263 WPI_WRITE(sc, WPI_INTR_STATUS, 0xff); 3264 WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000); 3265 3266 wpi_mem_lock(sc); 3267 wpi_mem_write(sc, WPI_MEM_MODE, 0); 3268 wpi_mem_unlock(sc); 3269 3270 /* reset all Tx rings */ 3271 for (ac = 0; ac < 4; ac++) 3272 wpi_reset_tx_ring(sc, &sc->txq[ac]); 3273 wpi_reset_tx_ring(sc, &sc->cmdq); 3274 3275 /* reset Rx ring */ 3276 wpi_reset_rx_ring(sc, &sc->rxq); 3277 3278 wpi_mem_lock(sc); 3279 wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200); 3280 wpi_mem_unlock(sc); 3281 3282 DELAY(5); 3283 3284 wpi_stop_master(sc); 3285 3286 tmp = WPI_READ(sc, WPI_RESET); 3287 WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET); 3288 } 3289 3290 static bool 3291 wpi_resume(device_t dv, const pmf_qual_t *qual) 3292 { 3293 struct wpi_softc *sc = device_private(dv); 3294 3295 (void)wpi_reset(sc); 3296 3297 return true; 3298 } 3299 3300 /* 3301 * Return whether or not the radio is enabled in hardware 3302 * (i.e. the rfkill switch is "off"). 3303 */ 3304 static int 3305 wpi_getrfkill(struct wpi_softc *sc) 3306 { 3307 uint32_t tmp; 3308 3309 wpi_mem_lock(sc); 3310 tmp = wpi_mem_read(sc, WPI_MEM_RFKILL); 3311 wpi_mem_unlock(sc); 3312 3313 KASSERT(mutex_owned(&sc->sc_rsw_mtx)); 3314 if (tmp & 0x01) { 3315 /* switch is on */ 3316 if (sc->sc_rsw_status != WPI_RSW_ON) { 3317 sc->sc_rsw_status = WPI_RSW_ON; 3318 sysmon_pswitch_event(&sc->sc_rsw, 3319 PSWITCH_EVENT_PRESSED); 3320 } 3321 } else { 3322 /* switch is off */ 3323 if (sc->sc_rsw_status != WPI_RSW_OFF) { 3324 sc->sc_rsw_status = WPI_RSW_OFF; 3325 sysmon_pswitch_event(&sc->sc_rsw, 3326 PSWITCH_EVENT_RELEASED); 3327 } 3328 } 3329 3330 return !(tmp & 0x01); 3331 } 3332 3333 static int 3334 wpi_sysctl_radio(SYSCTLFN_ARGS) 3335 { 3336 struct sysctlnode node; 3337 struct wpi_softc *sc; 3338 int val, error; 3339 3340 node = *rnode; 3341 sc = (struct wpi_softc *)node.sysctl_data; 3342 3343 mutex_enter(&sc->sc_rsw_mtx); 3344 val = !wpi_getrfkill(sc); 3345 mutex_exit(&sc->sc_rsw_mtx); 3346 3347 node.sysctl_data = &val; 3348 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 3349 3350 if (error || newp == NULL) 3351 return error; 3352 3353 return 0; 3354 } 3355 3356 static void 3357 wpi_sysctlattach(struct wpi_softc *sc) 3358 { 3359 int rc; 3360 const struct sysctlnode *rnode; 3361 const struct sysctlnode *cnode; 3362 3363 struct sysctllog **clog = &sc->sc_sysctllog; 3364 3365 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, 3366 CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev), 3367 SYSCTL_DESCR("wpi controls and statistics"), 3368 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) 3369 goto err; 3370 3371 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 3372 CTLFLAG_PERMANENT, CTLTYPE_INT, "radio", 3373 SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"), 3374 wpi_sysctl_radio, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL)) != 0) 3375 goto err; 3376 3377 #ifdef WPI_DEBUG 3378 /* control debugging printfs */ 3379 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 3380 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 3381 "debug", SYSCTL_DESCR("Enable debugging output"), 3382 NULL, 0, &wpi_debug, 0, CTL_CREATE, CTL_EOL)) != 0) 3383 goto err; 3384 #endif 3385 3386 return; 3387 err: 3388 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 3389 } 3390 3391 static void 3392 wpi_rsw_thread(void *arg) 3393 { 3394 struct wpi_softc *sc = (struct wpi_softc *)arg; 3395 3396 mutex_enter(&sc->sc_rsw_mtx); 3397 for (;;) { 3398 cv_timedwait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx, hz); 3399 if (sc->sc_dying) { 3400 sc->sc_rsw_lwp = NULL; 3401 cv_broadcast(&sc->sc_rsw_cv); 3402 mutex_exit(&sc->sc_rsw_mtx); 3403 kthread_exit(0); 3404 } 3405 wpi_getrfkill(sc); 3406 } 3407 } 3408 3409