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