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