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