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