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