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