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