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