304 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status); 305 306 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 307 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap), 308 &sc->sc_drvbpf); 309 310 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 311 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 312 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT); 313 314 sc->sc_txtap_len = sizeof sc->sc_txtap; 315 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 316 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT); 317 318 /* 319 * Add a few sysctl knobs. 320 */ 321 sc->dwelltime = 200; 322 323 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 324 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 325 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)"); 326 327 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 328 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 329 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)"); 330 331 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 332 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell", 333 CTLFLAG_RW, &sc->dwelltime, 0, 334 "channel dwell time (ms) for AP/station scanning"); 335 336 if (bootverbose) 337 ieee80211_announce(ic); 338 339 return 0; 340 341fail6: rt2560_free_rx_ring(sc, &sc->rxq); 342fail5: rt2560_free_tx_ring(sc, &sc->bcnq); 343fail4: rt2560_free_tx_ring(sc, &sc->prioq); 344fail3: rt2560_free_tx_ring(sc, &sc->atimq); 345fail2: rt2560_free_tx_ring(sc, &sc->txq); 346fail1: mtx_destroy(&sc->sc_mtx); 347 348 return ENXIO; 349} 350 351int 352rt2560_detach(void *xsc) 353{ 354 struct rt2560_softc *sc = xsc; 355 struct ieee80211com *ic = &sc->sc_ic; 356 struct ifnet *ifp = ic->ic_ifp; 357 358 rt2560_stop(sc); 359 callout_stop(&sc->watchdog_ch); 360 callout_stop(&sc->rssadapt_ch); 361 362 bpfdetach(ifp); 363 ieee80211_ifdetach(ic); 364 365 rt2560_free_tx_ring(sc, &sc->txq); 366 rt2560_free_tx_ring(sc, &sc->atimq); 367 rt2560_free_tx_ring(sc, &sc->prioq); 368 rt2560_free_tx_ring(sc, &sc->bcnq); 369 rt2560_free_rx_ring(sc, &sc->rxq); 370 371 if_free(ifp); 372 373 mtx_destroy(&sc->sc_mtx); 374 375 return 0; 376} 377 378void 379rt2560_resume(void *xsc) 380{ 381 struct rt2560_softc *sc = xsc; 382 struct ifnet *ifp = sc->sc_ic.ic_ifp; 383 384 if (ifp->if_flags & IFF_UP) { 385 ifp->if_init(ifp->if_softc); 386 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 387 ifp->if_start(ifp); 388 } 389} 390 391static void 392rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 393{ 394 if (error != 0) 395 return; 396 397 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 398 399 *(bus_addr_t *)arg = segs[0].ds_addr; 400} 401 402static int 403rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring, 404 int count) 405{ 406 int i, error; 407 408 ring->count = count; 409 ring->queued = 0; 410 ring->cur = ring->next = 0; 411 ring->cur_encrypt = ring->next_encrypt = 0; 412 413 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 414 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 415 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE, 416 0, NULL, NULL, &ring->desc_dmat); 417 if (error != 0) { 418 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 419 goto fail; 420 } 421 422 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 423 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 424 if (error != 0) { 425 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 426 goto fail; 427 } 428 429 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 430 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 431 0); 432 if (error != 0) { 433 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 434 goto fail; 435 } 436 437 ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF, 438 M_NOWAIT | M_ZERO); 439 if (ring->data == NULL) { 440 device_printf(sc->sc_dev, "could not allocate soft data\n"); 441 error = ENOMEM; 442 goto fail; 443 } 444 445 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 446 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 447 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL, 448 &ring->data_dmat); 449 if (error != 0) { 450 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 451 goto fail; 452 } 453 454 for (i = 0; i < count; i++) { 455 error = bus_dmamap_create(ring->data_dmat, 0, 456 &ring->data[i].map); 457 if (error != 0) { 458 device_printf(sc->sc_dev, "could not create DMA map\n"); 459 goto fail; 460 } 461 } 462 463 return 0; 464 465fail: rt2560_free_tx_ring(sc, ring); 466 return error; 467} 468 469static void 470rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 471{ 472 struct rt2560_tx_desc *desc; 473 struct rt2560_tx_data *data; 474 int i; 475 476 for (i = 0; i < ring->count; i++) { 477 desc = &ring->desc[i]; 478 data = &ring->data[i]; 479 480 if (data->m != NULL) { 481 bus_dmamap_sync(ring->data_dmat, data->map, 482 BUS_DMASYNC_POSTWRITE); 483 bus_dmamap_unload(ring->data_dmat, data->map); 484 m_freem(data->m); 485 data->m = NULL; 486 } 487 488 if (data->ni != NULL) { 489 ieee80211_free_node(data->ni); 490 data->ni = NULL; 491 } 492 493 desc->flags = 0; 494 } 495 496 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 497 498 ring->queued = 0; 499 ring->cur = ring->next = 0; 500 ring->cur_encrypt = ring->next_encrypt = 0; 501} 502 503static void 504rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 505{ 506 struct rt2560_tx_data *data; 507 int i; 508 509 if (ring->desc != NULL) { 510 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 511 BUS_DMASYNC_POSTWRITE); 512 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 513 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 514 } 515 516 if (ring->desc_dmat != NULL) 517 bus_dma_tag_destroy(ring->desc_dmat); 518 519 if (ring->data != NULL) { 520 for (i = 0; i < ring->count; i++) { 521 data = &ring->data[i]; 522 523 if (data->m != NULL) { 524 bus_dmamap_sync(ring->data_dmat, data->map, 525 BUS_DMASYNC_POSTWRITE); 526 bus_dmamap_unload(ring->data_dmat, data->map); 527 m_freem(data->m); 528 } 529 530 if (data->ni != NULL) 531 ieee80211_free_node(data->ni); 532 533 if (data->map != NULL) 534 bus_dmamap_destroy(ring->data_dmat, data->map); 535 } 536 537 free(ring->data, M_DEVBUF); 538 } 539 540 if (ring->data_dmat != NULL) 541 bus_dma_tag_destroy(ring->data_dmat); 542} 543 544static int 545rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring, 546 int count) 547{ 548 struct rt2560_rx_desc *desc; 549 struct rt2560_rx_data *data; 550 bus_addr_t physaddr; 551 int i, error; 552 553 ring->count = count; 554 ring->cur = ring->next = 0; 555 ring->cur_decrypt = 0; 556 557 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 558 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 559 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE, 560 0, NULL, NULL, &ring->desc_dmat); 561 if (error != 0) { 562 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 563 goto fail; 564 } 565 566 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 567 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 568 if (error != 0) { 569 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 570 goto fail; 571 } 572 573 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 574 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 575 0); 576 if (error != 0) { 577 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 578 goto fail; 579 } 580 581 ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF, 582 M_NOWAIT | M_ZERO); 583 if (ring->data == NULL) { 584 device_printf(sc->sc_dev, "could not allocate soft data\n"); 585 error = ENOMEM; 586 goto fail; 587 } 588 589 /* 590 * Pre-allocate Rx buffers and populate Rx ring. 591 */ 592 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 593 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 594 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 595 if (error != 0) { 596 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 597 goto fail; 598 } 599 600 for (i = 0; i < count; i++) { 601 desc = &sc->rxq.desc[i]; 602 data = &sc->rxq.data[i]; 603 604 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 605 if (error != 0) { 606 device_printf(sc->sc_dev, "could not create DMA map\n"); 607 goto fail; 608 } 609 610 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 611 if (data->m == NULL) { 612 device_printf(sc->sc_dev, 613 "could not allocate rx mbuf\n"); 614 error = ENOMEM; 615 goto fail; 616 } 617 618 error = bus_dmamap_load(ring->data_dmat, data->map, 619 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr, 620 &physaddr, 0); 621 if (error != 0) { 622 device_printf(sc->sc_dev, 623 "could not load rx buf DMA map"); 624 goto fail; 625 } 626 627 desc->flags = htole32(RT2560_RX_BUSY); 628 desc->physaddr = htole32(physaddr); 629 } 630 631 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 632 633 return 0; 634 635fail: rt2560_free_rx_ring(sc, ring); 636 return error; 637} 638 639static void 640rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 641{ 642 int i; 643 644 for (i = 0; i < ring->count; i++) { 645 ring->desc[i].flags = htole32(RT2560_RX_BUSY); 646 ring->data[i].drop = 0; 647 } 648 649 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 650 651 ring->cur = ring->next = 0; 652 ring->cur_decrypt = 0; 653} 654 655static void 656rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 657{ 658 struct rt2560_rx_data *data; 659 int i; 660 661 if (ring->desc != NULL) { 662 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 663 BUS_DMASYNC_POSTWRITE); 664 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 665 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 666 } 667 668 if (ring->desc_dmat != NULL) 669 bus_dma_tag_destroy(ring->desc_dmat); 670 671 if (ring->data != NULL) { 672 for (i = 0; i < ring->count; i++) { 673 data = &ring->data[i]; 674 675 if (data->m != NULL) { 676 bus_dmamap_sync(ring->data_dmat, data->map, 677 BUS_DMASYNC_POSTREAD); 678 bus_dmamap_unload(ring->data_dmat, data->map); 679 m_freem(data->m); 680 } 681 682 if (data->map != NULL) 683 bus_dmamap_destroy(ring->data_dmat, data->map); 684 } 685 686 free(ring->data, M_DEVBUF); 687 } 688 689 if (ring->data_dmat != NULL) 690 bus_dma_tag_destroy(ring->data_dmat); 691} 692 693static struct ieee80211_node * 694rt2560_node_alloc(struct ieee80211_node_table *nt) 695{ 696 struct rt2560_node *rn; 697 698 rn = malloc(sizeof (struct rt2560_node), M_80211_NODE, 699 M_NOWAIT | M_ZERO); 700 701 return (rn != NULL) ? &rn->ni : NULL; 702} 703 704static int 705rt2560_media_change(struct ifnet *ifp) 706{ 707 struct rt2560_softc *sc = ifp->if_softc; 708 int error; 709 710 error = ieee80211_media_change(ifp); 711 712 if (error == ENETRESET) { 713 if ((ifp->if_flags & IFF_UP) && 714 (ifp->if_drv_flags & IFF_DRV_RUNNING)) 715 rt2560_init(sc); 716 } 717 return error; 718} 719 720/* 721 * This function is called for each node present in the node station table. 722 */ 723static void 724rt2560_iter_func(void *arg, struct ieee80211_node *ni) 725{ 726 struct rt2560_node *rn = (struct rt2560_node *)ni; 727 728 ral_rssadapt_updatestats(&rn->rssadapt); 729} 730 731/* 732 * This function is called periodically (every 100ms) in RUN state to update 733 * the rate adaptation statistics. 734 */ 735static void 736rt2560_update_rssadapt(void *arg) 737{ 738 struct rt2560_softc *sc = arg; 739 struct ieee80211com *ic = &sc->sc_ic; 740 741 RAL_LOCK(sc); 742 743 ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg); 744 callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc); 745 746 RAL_UNLOCK(sc); 747} 748 749static int 750rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 751{ 752 struct rt2560_softc *sc = ic->ic_ifp->if_softc; 753 enum ieee80211_state ostate; 754 struct ieee80211_node *ni; 755 struct mbuf *m; 756 int error = 0; 757 758 ostate = ic->ic_state; 759 760 switch (nstate) { 761 case IEEE80211_S_INIT: 762 callout_stop(&sc->rssadapt_ch); 763 764 if (ostate == IEEE80211_S_RUN) { 765 /* abort TSF synchronization */ 766 RAL_WRITE(sc, RT2560_CSR14, 0); 767 768 /* turn association led off */ 769 rt2560_update_led(sc, 0, 0); 770 } 771 break; 772 case IEEE80211_S_RUN: 773 ni = ic->ic_bss; 774 775 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 776 rt2560_update_plcp(sc); 777 rt2560_set_basicrates(sc); 778 rt2560_set_bssid(sc, ni->ni_bssid); 779 } 780 781 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 782 ic->ic_opmode == IEEE80211_M_IBSS) {
| 306 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status); 307 308 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 309 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap), 310 &sc->sc_drvbpf); 311 312 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 313 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 314 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT); 315 316 sc->sc_txtap_len = sizeof sc->sc_txtap; 317 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 318 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT); 319 320 /* 321 * Add a few sysctl knobs. 322 */ 323 sc->dwelltime = 200; 324 325 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 326 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 327 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)"); 328 329 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 330 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 331 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)"); 332 333 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 334 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell", 335 CTLFLAG_RW, &sc->dwelltime, 0, 336 "channel dwell time (ms) for AP/station scanning"); 337 338 if (bootverbose) 339 ieee80211_announce(ic); 340 341 return 0; 342 343fail6: rt2560_free_rx_ring(sc, &sc->rxq); 344fail5: rt2560_free_tx_ring(sc, &sc->bcnq); 345fail4: rt2560_free_tx_ring(sc, &sc->prioq); 346fail3: rt2560_free_tx_ring(sc, &sc->atimq); 347fail2: rt2560_free_tx_ring(sc, &sc->txq); 348fail1: mtx_destroy(&sc->sc_mtx); 349 350 return ENXIO; 351} 352 353int 354rt2560_detach(void *xsc) 355{ 356 struct rt2560_softc *sc = xsc; 357 struct ieee80211com *ic = &sc->sc_ic; 358 struct ifnet *ifp = ic->ic_ifp; 359 360 rt2560_stop(sc); 361 callout_stop(&sc->watchdog_ch); 362 callout_stop(&sc->rssadapt_ch); 363 364 bpfdetach(ifp); 365 ieee80211_ifdetach(ic); 366 367 rt2560_free_tx_ring(sc, &sc->txq); 368 rt2560_free_tx_ring(sc, &sc->atimq); 369 rt2560_free_tx_ring(sc, &sc->prioq); 370 rt2560_free_tx_ring(sc, &sc->bcnq); 371 rt2560_free_rx_ring(sc, &sc->rxq); 372 373 if_free(ifp); 374 375 mtx_destroy(&sc->sc_mtx); 376 377 return 0; 378} 379 380void 381rt2560_resume(void *xsc) 382{ 383 struct rt2560_softc *sc = xsc; 384 struct ifnet *ifp = sc->sc_ic.ic_ifp; 385 386 if (ifp->if_flags & IFF_UP) { 387 ifp->if_init(ifp->if_softc); 388 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 389 ifp->if_start(ifp); 390 } 391} 392 393static void 394rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 395{ 396 if (error != 0) 397 return; 398 399 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 400 401 *(bus_addr_t *)arg = segs[0].ds_addr; 402} 403 404static int 405rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring, 406 int count) 407{ 408 int i, error; 409 410 ring->count = count; 411 ring->queued = 0; 412 ring->cur = ring->next = 0; 413 ring->cur_encrypt = ring->next_encrypt = 0; 414 415 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 416 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 417 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE, 418 0, NULL, NULL, &ring->desc_dmat); 419 if (error != 0) { 420 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 421 goto fail; 422 } 423 424 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 425 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 426 if (error != 0) { 427 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 428 goto fail; 429 } 430 431 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 432 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 433 0); 434 if (error != 0) { 435 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 436 goto fail; 437 } 438 439 ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF, 440 M_NOWAIT | M_ZERO); 441 if (ring->data == NULL) { 442 device_printf(sc->sc_dev, "could not allocate soft data\n"); 443 error = ENOMEM; 444 goto fail; 445 } 446 447 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 448 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 449 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL, 450 &ring->data_dmat); 451 if (error != 0) { 452 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 453 goto fail; 454 } 455 456 for (i = 0; i < count; i++) { 457 error = bus_dmamap_create(ring->data_dmat, 0, 458 &ring->data[i].map); 459 if (error != 0) { 460 device_printf(sc->sc_dev, "could not create DMA map\n"); 461 goto fail; 462 } 463 } 464 465 return 0; 466 467fail: rt2560_free_tx_ring(sc, ring); 468 return error; 469} 470 471static void 472rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 473{ 474 struct rt2560_tx_desc *desc; 475 struct rt2560_tx_data *data; 476 int i; 477 478 for (i = 0; i < ring->count; i++) { 479 desc = &ring->desc[i]; 480 data = &ring->data[i]; 481 482 if (data->m != NULL) { 483 bus_dmamap_sync(ring->data_dmat, data->map, 484 BUS_DMASYNC_POSTWRITE); 485 bus_dmamap_unload(ring->data_dmat, data->map); 486 m_freem(data->m); 487 data->m = NULL; 488 } 489 490 if (data->ni != NULL) { 491 ieee80211_free_node(data->ni); 492 data->ni = NULL; 493 } 494 495 desc->flags = 0; 496 } 497 498 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 499 500 ring->queued = 0; 501 ring->cur = ring->next = 0; 502 ring->cur_encrypt = ring->next_encrypt = 0; 503} 504 505static void 506rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 507{ 508 struct rt2560_tx_data *data; 509 int i; 510 511 if (ring->desc != NULL) { 512 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 513 BUS_DMASYNC_POSTWRITE); 514 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 515 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 516 } 517 518 if (ring->desc_dmat != NULL) 519 bus_dma_tag_destroy(ring->desc_dmat); 520 521 if (ring->data != NULL) { 522 for (i = 0; i < ring->count; i++) { 523 data = &ring->data[i]; 524 525 if (data->m != NULL) { 526 bus_dmamap_sync(ring->data_dmat, data->map, 527 BUS_DMASYNC_POSTWRITE); 528 bus_dmamap_unload(ring->data_dmat, data->map); 529 m_freem(data->m); 530 } 531 532 if (data->ni != NULL) 533 ieee80211_free_node(data->ni); 534 535 if (data->map != NULL) 536 bus_dmamap_destroy(ring->data_dmat, data->map); 537 } 538 539 free(ring->data, M_DEVBUF); 540 } 541 542 if (ring->data_dmat != NULL) 543 bus_dma_tag_destroy(ring->data_dmat); 544} 545 546static int 547rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring, 548 int count) 549{ 550 struct rt2560_rx_desc *desc; 551 struct rt2560_rx_data *data; 552 bus_addr_t physaddr; 553 int i, error; 554 555 ring->count = count; 556 ring->cur = ring->next = 0; 557 ring->cur_decrypt = 0; 558 559 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 560 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 561 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE, 562 0, NULL, NULL, &ring->desc_dmat); 563 if (error != 0) { 564 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 565 goto fail; 566 } 567 568 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 569 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 570 if (error != 0) { 571 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 572 goto fail; 573 } 574 575 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 576 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 577 0); 578 if (error != 0) { 579 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 580 goto fail; 581 } 582 583 ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF, 584 M_NOWAIT | M_ZERO); 585 if (ring->data == NULL) { 586 device_printf(sc->sc_dev, "could not allocate soft data\n"); 587 error = ENOMEM; 588 goto fail; 589 } 590 591 /* 592 * Pre-allocate Rx buffers and populate Rx ring. 593 */ 594 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 595 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 596 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 597 if (error != 0) { 598 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 599 goto fail; 600 } 601 602 for (i = 0; i < count; i++) { 603 desc = &sc->rxq.desc[i]; 604 data = &sc->rxq.data[i]; 605 606 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 607 if (error != 0) { 608 device_printf(sc->sc_dev, "could not create DMA map\n"); 609 goto fail; 610 } 611 612 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 613 if (data->m == NULL) { 614 device_printf(sc->sc_dev, 615 "could not allocate rx mbuf\n"); 616 error = ENOMEM; 617 goto fail; 618 } 619 620 error = bus_dmamap_load(ring->data_dmat, data->map, 621 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr, 622 &physaddr, 0); 623 if (error != 0) { 624 device_printf(sc->sc_dev, 625 "could not load rx buf DMA map"); 626 goto fail; 627 } 628 629 desc->flags = htole32(RT2560_RX_BUSY); 630 desc->physaddr = htole32(physaddr); 631 } 632 633 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 634 635 return 0; 636 637fail: rt2560_free_rx_ring(sc, ring); 638 return error; 639} 640 641static void 642rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 643{ 644 int i; 645 646 for (i = 0; i < ring->count; i++) { 647 ring->desc[i].flags = htole32(RT2560_RX_BUSY); 648 ring->data[i].drop = 0; 649 } 650 651 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 652 653 ring->cur = ring->next = 0; 654 ring->cur_decrypt = 0; 655} 656 657static void 658rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 659{ 660 struct rt2560_rx_data *data; 661 int i; 662 663 if (ring->desc != NULL) { 664 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 665 BUS_DMASYNC_POSTWRITE); 666 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 667 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 668 } 669 670 if (ring->desc_dmat != NULL) 671 bus_dma_tag_destroy(ring->desc_dmat); 672 673 if (ring->data != NULL) { 674 for (i = 0; i < ring->count; i++) { 675 data = &ring->data[i]; 676 677 if (data->m != NULL) { 678 bus_dmamap_sync(ring->data_dmat, data->map, 679 BUS_DMASYNC_POSTREAD); 680 bus_dmamap_unload(ring->data_dmat, data->map); 681 m_freem(data->m); 682 } 683 684 if (data->map != NULL) 685 bus_dmamap_destroy(ring->data_dmat, data->map); 686 } 687 688 free(ring->data, M_DEVBUF); 689 } 690 691 if (ring->data_dmat != NULL) 692 bus_dma_tag_destroy(ring->data_dmat); 693} 694 695static struct ieee80211_node * 696rt2560_node_alloc(struct ieee80211_node_table *nt) 697{ 698 struct rt2560_node *rn; 699 700 rn = malloc(sizeof (struct rt2560_node), M_80211_NODE, 701 M_NOWAIT | M_ZERO); 702 703 return (rn != NULL) ? &rn->ni : NULL; 704} 705 706static int 707rt2560_media_change(struct ifnet *ifp) 708{ 709 struct rt2560_softc *sc = ifp->if_softc; 710 int error; 711 712 error = ieee80211_media_change(ifp); 713 714 if (error == ENETRESET) { 715 if ((ifp->if_flags & IFF_UP) && 716 (ifp->if_drv_flags & IFF_DRV_RUNNING)) 717 rt2560_init(sc); 718 } 719 return error; 720} 721 722/* 723 * This function is called for each node present in the node station table. 724 */ 725static void 726rt2560_iter_func(void *arg, struct ieee80211_node *ni) 727{ 728 struct rt2560_node *rn = (struct rt2560_node *)ni; 729 730 ral_rssadapt_updatestats(&rn->rssadapt); 731} 732 733/* 734 * This function is called periodically (every 100ms) in RUN state to update 735 * the rate adaptation statistics. 736 */ 737static void 738rt2560_update_rssadapt(void *arg) 739{ 740 struct rt2560_softc *sc = arg; 741 struct ieee80211com *ic = &sc->sc_ic; 742 743 RAL_LOCK(sc); 744 745 ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg); 746 callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc); 747 748 RAL_UNLOCK(sc); 749} 750 751static int 752rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 753{ 754 struct rt2560_softc *sc = ic->ic_ifp->if_softc; 755 enum ieee80211_state ostate; 756 struct ieee80211_node *ni; 757 struct mbuf *m; 758 int error = 0; 759 760 ostate = ic->ic_state; 761 762 switch (nstate) { 763 case IEEE80211_S_INIT: 764 callout_stop(&sc->rssadapt_ch); 765 766 if (ostate == IEEE80211_S_RUN) { 767 /* abort TSF synchronization */ 768 RAL_WRITE(sc, RT2560_CSR14, 0); 769 770 /* turn association led off */ 771 rt2560_update_led(sc, 0, 0); 772 } 773 break; 774 case IEEE80211_S_RUN: 775 ni = ic->ic_bss; 776 777 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 778 rt2560_update_plcp(sc); 779 rt2560_set_basicrates(sc); 780 rt2560_set_bssid(sc, ni->ni_bssid); 781 } 782 783 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 784 ic->ic_opmode == IEEE80211_M_IBSS) {
|
784 if (m == NULL) { 785 device_printf(sc->sc_dev, 786 "could not allocate beacon\n"); 787 error = ENOBUFS; 788 break; 789 } 790 791 ieee80211_ref_node(ni); 792 error = rt2560_tx_bcn(sc, m, ni); 793 if (error != 0) 794 break; 795 } 796 797 /* turn assocation led on */ 798 rt2560_update_led(sc, 1, 0); 799 800 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 801 callout_reset(&sc->rssadapt_ch, hz / 10, 802 rt2560_update_rssadapt, sc); 803 804 rt2560_enable_tsf_sync(sc); 805 } 806 break; 807 case IEEE80211_S_SCAN: 808 case IEEE80211_S_AUTH: 809 case IEEE80211_S_ASSOC: 810 default: 811 break; 812 } 813 814 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg); 815} 816 817/* 818 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or 819 * 93C66). 820 */ 821static uint16_t 822rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr) 823{ 824 uint32_t tmp; 825 uint16_t val; 826 int n; 827 828 /* clock C once before the first command */ 829 RT2560_EEPROM_CTL(sc, 0); 830 831 RT2560_EEPROM_CTL(sc, RT2560_S); 832 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 833 RT2560_EEPROM_CTL(sc, RT2560_S); 834 835 /* write start bit (1) */ 836 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 837 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 838 839 /* write READ opcode (10) */ 840 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 841 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 842 RT2560_EEPROM_CTL(sc, RT2560_S); 843 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 844 845 /* write address (A5-A0 or A7-A0) */ 846 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7; 847 for (; n >= 0; n--) { 848 RT2560_EEPROM_CTL(sc, RT2560_S | 849 (((addr >> n) & 1) << RT2560_SHIFT_D)); 850 RT2560_EEPROM_CTL(sc, RT2560_S | 851 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C); 852 } 853 854 RT2560_EEPROM_CTL(sc, RT2560_S); 855 856 /* read data Q15-Q0 */ 857 val = 0; 858 for (n = 15; n >= 0; n--) { 859 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 860 tmp = RAL_READ(sc, RT2560_CSR21); 861 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n; 862 RT2560_EEPROM_CTL(sc, RT2560_S); 863 } 864 865 RT2560_EEPROM_CTL(sc, 0); 866 867 /* clear Chip Select and clock C */ 868 RT2560_EEPROM_CTL(sc, RT2560_S); 869 RT2560_EEPROM_CTL(sc, 0); 870 RT2560_EEPROM_CTL(sc, RT2560_C); 871 872 return val; 873} 874 875/* 876 * Some frames were processed by the hardware cipher engine and are ready for 877 * transmission. 878 */ 879static void 880rt2560_encryption_intr(struct rt2560_softc *sc) 881{ 882 struct rt2560_tx_desc *desc; 883 int hw; 884 885 /* retrieve last descriptor index processed by cipher engine */ 886 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr; 887 hw /= RT2560_TX_DESC_SIZE; 888 889 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 890 BUS_DMASYNC_POSTREAD); 891 892 for (; sc->txq.next_encrypt != hw;) { 893 desc = &sc->txq.desc[sc->txq.next_encrypt]; 894 895 if ((le32toh(desc->flags) & RT2560_TX_BUSY) || 896 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY)) 897 break; 898 899 /* for TKIP, swap eiv field to fix a bug in ASIC */ 900 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) == 901 RT2560_TX_CIPHER_TKIP) 902 desc->eiv = bswap32(desc->eiv); 903 904 /* mark the frame ready for transmission */ 905 desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID); 906 907 DPRINTFN(15, ("encryption done idx=%u\n", 908 sc->txq.next_encrypt)); 909 910 sc->txq.next_encrypt = 911 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT; 912 } 913 914 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 915 BUS_DMASYNC_PREWRITE); 916 917 /* kick Tx */ 918 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX); 919} 920 921static void 922rt2560_tx_intr(struct rt2560_softc *sc) 923{ 924 struct ieee80211com *ic = &sc->sc_ic; 925 struct ifnet *ifp = ic->ic_ifp; 926 struct rt2560_tx_desc *desc; 927 struct rt2560_tx_data *data; 928 struct rt2560_node *rn; 929 930 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 931 BUS_DMASYNC_POSTREAD); 932 933 for (;;) { 934 desc = &sc->txq.desc[sc->txq.next]; 935 data = &sc->txq.data[sc->txq.next]; 936 937 if ((le32toh(desc->flags) & RT2560_TX_BUSY) || 938 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) || 939 !(le32toh(desc->flags) & RT2560_TX_VALID)) 940 break; 941 942 rn = (struct rt2560_node *)data->ni; 943 944 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) { 945 case RT2560_TX_SUCCESS: 946 DPRINTFN(10, ("data frame sent successfully\n")); 947 if (data->id.id_node != NULL) { 948 ral_rssadapt_raise_rate(ic, &rn->rssadapt, 949 &data->id); 950 } 951 ifp->if_opackets++; 952 break; 953 954 case RT2560_TX_SUCCESS_RETRY: 955 DPRINTFN(9, ("data frame sent after %u retries\n", 956 (le32toh(desc->flags) >> 5) & 0x7)); 957 ifp->if_opackets++; 958 break; 959 960 case RT2560_TX_FAIL_RETRY: 961 DPRINTFN(9, ("sending data frame failed (too much " 962 "retries)\n")); 963 if (data->id.id_node != NULL) { 964 ral_rssadapt_lower_rate(ic, data->ni, 965 &rn->rssadapt, &data->id); 966 } 967 ifp->if_oerrors++; 968 break; 969 970 case RT2560_TX_FAIL_INVALID: 971 case RT2560_TX_FAIL_OTHER: 972 default: 973 device_printf(sc->sc_dev, "sending data frame failed " 974 "0x%08x\n", le32toh(desc->flags)); 975 ifp->if_oerrors++; 976 } 977 978 bus_dmamap_sync(sc->txq.data_dmat, data->map, 979 BUS_DMASYNC_POSTWRITE); 980 bus_dmamap_unload(sc->txq.data_dmat, data->map); 981 m_freem(data->m); 982 data->m = NULL; 983 ieee80211_free_node(data->ni); 984 data->ni = NULL; 985 986 /* descriptor is no longer valid */ 987 desc->flags &= ~htole32(RT2560_TX_VALID); 988 989 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next)); 990 991 sc->txq.queued--; 992 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT; 993 } 994 995 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 996 BUS_DMASYNC_PREWRITE); 997 998 sc->sc_tx_timer = 0; 999 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1000 rt2560_start(ifp); 1001} 1002 1003static void 1004rt2560_prio_intr(struct rt2560_softc *sc) 1005{ 1006 struct ieee80211com *ic = &sc->sc_ic; 1007 struct ifnet *ifp = ic->ic_ifp; 1008 struct rt2560_tx_desc *desc; 1009 struct rt2560_tx_data *data; 1010 struct ieee80211_node *ni; 1011 struct mbuf *m; 1012 int flags; 1013 1014 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1015 BUS_DMASYNC_POSTREAD); 1016 1017 for (;;) { 1018 desc = &sc->prioq.desc[sc->prioq.next]; 1019 data = &sc->prioq.data[sc->prioq.next]; 1020 1021 flags = le32toh(desc->flags); 1022 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0) 1023 break; 1024 1025 switch (flags & RT2560_TX_RESULT_MASK) { 1026 case RT2560_TX_SUCCESS: 1027 DPRINTFN(10, ("mgt frame sent successfully\n")); 1028 break; 1029 1030 case RT2560_TX_SUCCESS_RETRY: 1031 DPRINTFN(9, ("mgt frame sent after %u retries\n", 1032 (flags >> 5) & 0x7)); 1033 break; 1034 1035 case RT2560_TX_FAIL_RETRY: 1036 DPRINTFN(9, ("sending mgt frame failed (too much " 1037 "retries)\n")); 1038 break; 1039 1040 case RT2560_TX_FAIL_INVALID: 1041 case RT2560_TX_FAIL_OTHER: 1042 default: 1043 device_printf(sc->sc_dev, "sending mgt frame failed " 1044 "0x%08x\n", flags); 1045 break; 1046 } 1047 1048 bus_dmamap_sync(sc->prioq.data_dmat, data->map, 1049 BUS_DMASYNC_POSTWRITE); 1050 bus_dmamap_unload(sc->prioq.data_dmat, data->map); 1051 1052 m = data->m; 1053 data->m = NULL; 1054 ni = data->ni; 1055 data->ni = NULL; 1056 1057 /* descriptor is no longer valid */ 1058 desc->flags &= ~htole32(RT2560_TX_VALID); 1059 1060 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next)); 1061 1062 sc->prioq.queued--; 1063 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT; 1064 1065 if (m->m_flags & M_TXCB) 1066 ieee80211_process_callback(ni, m, 1067 (flags & RT2560_TX_RESULT_MASK) &~ 1068 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY)); 1069 m_freem(m); 1070 ieee80211_free_node(ni); 1071 } 1072 1073 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1074 BUS_DMASYNC_PREWRITE); 1075 1076 sc->sc_tx_timer = 0; 1077 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1078 rt2560_start(ifp); 1079} 1080 1081/* 1082 * Some frames were processed by the hardware cipher engine and are ready for 1083 * transmission to the IEEE802.11 layer. 1084 */ 1085static void 1086rt2560_decryption_intr(struct rt2560_softc *sc) 1087{ 1088 struct ieee80211com *ic = &sc->sc_ic; 1089 struct ifnet *ifp = ic->ic_ifp; 1090 struct rt2560_rx_desc *desc; 1091 struct rt2560_rx_data *data; 1092 bus_addr_t physaddr; 1093 struct ieee80211_frame *wh; 1094 struct ieee80211_node *ni; 1095 struct rt2560_node *rn; 1096 struct mbuf *mnew, *m; 1097 int hw, error; 1098 1099 /* retrieve last decriptor index processed by cipher engine */ 1100 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr; 1101 hw /= RT2560_RX_DESC_SIZE; 1102 1103 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1104 BUS_DMASYNC_POSTREAD); 1105 1106 for (; sc->rxq.cur_decrypt != hw;) { 1107 desc = &sc->rxq.desc[sc->rxq.cur_decrypt]; 1108 data = &sc->rxq.data[sc->rxq.cur_decrypt]; 1109 1110 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1111 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1112 break; 1113 1114 if (data->drop) { 1115 ifp->if_ierrors++; 1116 goto skip; 1117 } 1118 1119 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 && 1120 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) { 1121 ifp->if_ierrors++; 1122 goto skip; 1123 } 1124 1125 /* 1126 * Try to allocate a new mbuf for this ring element and load it 1127 * before processing the current mbuf. If the ring element 1128 * cannot be loaded, drop the received packet and reuse the old 1129 * mbuf. In the unlikely case that the old mbuf can't be 1130 * reloaded either, explicitly panic. 1131 */ 1132 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1133 if (mnew == NULL) { 1134 ifp->if_ierrors++; 1135 goto skip; 1136 } 1137 1138 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1139 BUS_DMASYNC_POSTREAD); 1140 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1141 1142 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1143 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr, 1144 &physaddr, 0); 1145 if (error != 0) { 1146 m_freem(mnew); 1147 1148 /* try to reload the old mbuf */ 1149 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1150 mtod(data->m, void *), MCLBYTES, 1151 rt2560_dma_map_addr, &physaddr, 0); 1152 if (error != 0) { 1153 /* very unlikely that it will fail... */ 1154 panic("%s: could not load old rx mbuf", 1155 device_get_name(sc->sc_dev)); 1156 } 1157 ifp->if_ierrors++; 1158 goto skip; 1159 } 1160 1161 /* 1162 * New mbuf successfully loaded, update Rx ring and continue 1163 * processing. 1164 */ 1165 m = data->m; 1166 data->m = mnew; 1167 desc->physaddr = htole32(physaddr); 1168 1169 /* finalize mbuf */ 1170 m->m_pkthdr.rcvif = ifp; 1171 m->m_pkthdr.len = m->m_len = 1172 (le32toh(desc->flags) >> 16) & 0xfff; 1173 1174 if (bpf_peers_present(sc->sc_drvbpf)) { 1175 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap; 1176 uint32_t tsf_lo, tsf_hi; 1177 1178 /* get timestamp (low and high 32 bits) */ 1179 tsf_hi = RAL_READ(sc, RT2560_CSR17); 1180 tsf_lo = RAL_READ(sc, RT2560_CSR16); 1181 1182 tap->wr_tsf = 1183 htole64(((uint64_t)tsf_hi << 32) | tsf_lo); 1184 tap->wr_flags = 0; 1185 tap->wr_rate = rt2560_rxrate(desc); 1186 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1187 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1188 tap->wr_antenna = sc->rx_ant; 1189 tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi); 1190 1191 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1192 } 1193 1194 sc->sc_flags |= RAL_INPUT_RUNNING; 1195 RAL_UNLOCK(sc); 1196 wh = mtod(m, struct ieee80211_frame *); 1197 ni = ieee80211_find_rxnode(ic, 1198 (struct ieee80211_frame_min *)wh); 1199 1200 /* send the frame to the 802.11 layer */ 1201 ieee80211_input(ic, m, ni, RT2560_RSSI(sc, desc->rssi), 1202 RT2560_NOISE_FLOOR, 0); 1203 1204 /* give rssi to the rate adatation algorithm */ 1205 rn = (struct rt2560_node *)ni; 1206 ral_rssadapt_input(ic, ni, &rn->rssadapt, 1207 RT2560_RSSI(sc, desc->rssi)); 1208 1209 /* node is no longer needed */ 1210 ieee80211_free_node(ni); 1211 1212 RAL_LOCK(sc); 1213 sc->sc_flags &= ~RAL_INPUT_RUNNING; 1214skip: desc->flags = htole32(RT2560_RX_BUSY); 1215 1216 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt)); 1217 1218 sc->rxq.cur_decrypt = 1219 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT; 1220 } 1221 1222 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1223 BUS_DMASYNC_PREWRITE); 1224} 1225 1226/* 1227 * Some frames were received. Pass them to the hardware cipher engine before 1228 * sending them to the 802.11 layer. 1229 */ 1230static void 1231rt2560_rx_intr(struct rt2560_softc *sc) 1232{ 1233 struct rt2560_rx_desc *desc; 1234 struct rt2560_rx_data *data; 1235 1236 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1237 BUS_DMASYNC_POSTREAD); 1238 1239 for (;;) { 1240 desc = &sc->rxq.desc[sc->rxq.cur]; 1241 data = &sc->rxq.data[sc->rxq.cur]; 1242 1243 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1244 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1245 break; 1246 1247 data->drop = 0; 1248 1249 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) || 1250 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) { 1251 /* 1252 * This should not happen since we did not request 1253 * to receive those frames when we filled RXCSR0. 1254 */ 1255 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n", 1256 le32toh(desc->flags))); 1257 data->drop = 1; 1258 } 1259 1260 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) { 1261 DPRINTFN(5, ("bad length\n")); 1262 data->drop = 1; 1263 } 1264 1265 /* mark the frame for decryption */ 1266 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY); 1267 1268 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1269 1270 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT; 1271 } 1272 1273 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1274 BUS_DMASYNC_PREWRITE); 1275 1276 /* kick decrypt */ 1277 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT); 1278} 1279
| 786 if (m == NULL) { 787 device_printf(sc->sc_dev, 788 "could not allocate beacon\n"); 789 error = ENOBUFS; 790 break; 791 } 792 793 ieee80211_ref_node(ni); 794 error = rt2560_tx_bcn(sc, m, ni); 795 if (error != 0) 796 break; 797 } 798 799 /* turn assocation led on */ 800 rt2560_update_led(sc, 1, 0); 801 802 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 803 callout_reset(&sc->rssadapt_ch, hz / 10, 804 rt2560_update_rssadapt, sc); 805 806 rt2560_enable_tsf_sync(sc); 807 } 808 break; 809 case IEEE80211_S_SCAN: 810 case IEEE80211_S_AUTH: 811 case IEEE80211_S_ASSOC: 812 default: 813 break; 814 } 815 816 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg); 817} 818 819/* 820 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or 821 * 93C66). 822 */ 823static uint16_t 824rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr) 825{ 826 uint32_t tmp; 827 uint16_t val; 828 int n; 829 830 /* clock C once before the first command */ 831 RT2560_EEPROM_CTL(sc, 0); 832 833 RT2560_EEPROM_CTL(sc, RT2560_S); 834 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 835 RT2560_EEPROM_CTL(sc, RT2560_S); 836 837 /* write start bit (1) */ 838 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 839 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 840 841 /* write READ opcode (10) */ 842 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 843 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 844 RT2560_EEPROM_CTL(sc, RT2560_S); 845 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 846 847 /* write address (A5-A0 or A7-A0) */ 848 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7; 849 for (; n >= 0; n--) { 850 RT2560_EEPROM_CTL(sc, RT2560_S | 851 (((addr >> n) & 1) << RT2560_SHIFT_D)); 852 RT2560_EEPROM_CTL(sc, RT2560_S | 853 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C); 854 } 855 856 RT2560_EEPROM_CTL(sc, RT2560_S); 857 858 /* read data Q15-Q0 */ 859 val = 0; 860 for (n = 15; n >= 0; n--) { 861 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 862 tmp = RAL_READ(sc, RT2560_CSR21); 863 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n; 864 RT2560_EEPROM_CTL(sc, RT2560_S); 865 } 866 867 RT2560_EEPROM_CTL(sc, 0); 868 869 /* clear Chip Select and clock C */ 870 RT2560_EEPROM_CTL(sc, RT2560_S); 871 RT2560_EEPROM_CTL(sc, 0); 872 RT2560_EEPROM_CTL(sc, RT2560_C); 873 874 return val; 875} 876 877/* 878 * Some frames were processed by the hardware cipher engine and are ready for 879 * transmission. 880 */ 881static void 882rt2560_encryption_intr(struct rt2560_softc *sc) 883{ 884 struct rt2560_tx_desc *desc; 885 int hw; 886 887 /* retrieve last descriptor index processed by cipher engine */ 888 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr; 889 hw /= RT2560_TX_DESC_SIZE; 890 891 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 892 BUS_DMASYNC_POSTREAD); 893 894 for (; sc->txq.next_encrypt != hw;) { 895 desc = &sc->txq.desc[sc->txq.next_encrypt]; 896 897 if ((le32toh(desc->flags) & RT2560_TX_BUSY) || 898 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY)) 899 break; 900 901 /* for TKIP, swap eiv field to fix a bug in ASIC */ 902 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) == 903 RT2560_TX_CIPHER_TKIP) 904 desc->eiv = bswap32(desc->eiv); 905 906 /* mark the frame ready for transmission */ 907 desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID); 908 909 DPRINTFN(15, ("encryption done idx=%u\n", 910 sc->txq.next_encrypt)); 911 912 sc->txq.next_encrypt = 913 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT; 914 } 915 916 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 917 BUS_DMASYNC_PREWRITE); 918 919 /* kick Tx */ 920 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX); 921} 922 923static void 924rt2560_tx_intr(struct rt2560_softc *sc) 925{ 926 struct ieee80211com *ic = &sc->sc_ic; 927 struct ifnet *ifp = ic->ic_ifp; 928 struct rt2560_tx_desc *desc; 929 struct rt2560_tx_data *data; 930 struct rt2560_node *rn; 931 932 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 933 BUS_DMASYNC_POSTREAD); 934 935 for (;;) { 936 desc = &sc->txq.desc[sc->txq.next]; 937 data = &sc->txq.data[sc->txq.next]; 938 939 if ((le32toh(desc->flags) & RT2560_TX_BUSY) || 940 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) || 941 !(le32toh(desc->flags) & RT2560_TX_VALID)) 942 break; 943 944 rn = (struct rt2560_node *)data->ni; 945 946 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) { 947 case RT2560_TX_SUCCESS: 948 DPRINTFN(10, ("data frame sent successfully\n")); 949 if (data->id.id_node != NULL) { 950 ral_rssadapt_raise_rate(ic, &rn->rssadapt, 951 &data->id); 952 } 953 ifp->if_opackets++; 954 break; 955 956 case RT2560_TX_SUCCESS_RETRY: 957 DPRINTFN(9, ("data frame sent after %u retries\n", 958 (le32toh(desc->flags) >> 5) & 0x7)); 959 ifp->if_opackets++; 960 break; 961 962 case RT2560_TX_FAIL_RETRY: 963 DPRINTFN(9, ("sending data frame failed (too much " 964 "retries)\n")); 965 if (data->id.id_node != NULL) { 966 ral_rssadapt_lower_rate(ic, data->ni, 967 &rn->rssadapt, &data->id); 968 } 969 ifp->if_oerrors++; 970 break; 971 972 case RT2560_TX_FAIL_INVALID: 973 case RT2560_TX_FAIL_OTHER: 974 default: 975 device_printf(sc->sc_dev, "sending data frame failed " 976 "0x%08x\n", le32toh(desc->flags)); 977 ifp->if_oerrors++; 978 } 979 980 bus_dmamap_sync(sc->txq.data_dmat, data->map, 981 BUS_DMASYNC_POSTWRITE); 982 bus_dmamap_unload(sc->txq.data_dmat, data->map); 983 m_freem(data->m); 984 data->m = NULL; 985 ieee80211_free_node(data->ni); 986 data->ni = NULL; 987 988 /* descriptor is no longer valid */ 989 desc->flags &= ~htole32(RT2560_TX_VALID); 990 991 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next)); 992 993 sc->txq.queued--; 994 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT; 995 } 996 997 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 998 BUS_DMASYNC_PREWRITE); 999 1000 sc->sc_tx_timer = 0; 1001 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1002 rt2560_start(ifp); 1003} 1004 1005static void 1006rt2560_prio_intr(struct rt2560_softc *sc) 1007{ 1008 struct ieee80211com *ic = &sc->sc_ic; 1009 struct ifnet *ifp = ic->ic_ifp; 1010 struct rt2560_tx_desc *desc; 1011 struct rt2560_tx_data *data; 1012 struct ieee80211_node *ni; 1013 struct mbuf *m; 1014 int flags; 1015 1016 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1017 BUS_DMASYNC_POSTREAD); 1018 1019 for (;;) { 1020 desc = &sc->prioq.desc[sc->prioq.next]; 1021 data = &sc->prioq.data[sc->prioq.next]; 1022 1023 flags = le32toh(desc->flags); 1024 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0) 1025 break; 1026 1027 switch (flags & RT2560_TX_RESULT_MASK) { 1028 case RT2560_TX_SUCCESS: 1029 DPRINTFN(10, ("mgt frame sent successfully\n")); 1030 break; 1031 1032 case RT2560_TX_SUCCESS_RETRY: 1033 DPRINTFN(9, ("mgt frame sent after %u retries\n", 1034 (flags >> 5) & 0x7)); 1035 break; 1036 1037 case RT2560_TX_FAIL_RETRY: 1038 DPRINTFN(9, ("sending mgt frame failed (too much " 1039 "retries)\n")); 1040 break; 1041 1042 case RT2560_TX_FAIL_INVALID: 1043 case RT2560_TX_FAIL_OTHER: 1044 default: 1045 device_printf(sc->sc_dev, "sending mgt frame failed " 1046 "0x%08x\n", flags); 1047 break; 1048 } 1049 1050 bus_dmamap_sync(sc->prioq.data_dmat, data->map, 1051 BUS_DMASYNC_POSTWRITE); 1052 bus_dmamap_unload(sc->prioq.data_dmat, data->map); 1053 1054 m = data->m; 1055 data->m = NULL; 1056 ni = data->ni; 1057 data->ni = NULL; 1058 1059 /* descriptor is no longer valid */ 1060 desc->flags &= ~htole32(RT2560_TX_VALID); 1061 1062 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next)); 1063 1064 sc->prioq.queued--; 1065 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT; 1066 1067 if (m->m_flags & M_TXCB) 1068 ieee80211_process_callback(ni, m, 1069 (flags & RT2560_TX_RESULT_MASK) &~ 1070 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY)); 1071 m_freem(m); 1072 ieee80211_free_node(ni); 1073 } 1074 1075 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1076 BUS_DMASYNC_PREWRITE); 1077 1078 sc->sc_tx_timer = 0; 1079 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1080 rt2560_start(ifp); 1081} 1082 1083/* 1084 * Some frames were processed by the hardware cipher engine and are ready for 1085 * transmission to the IEEE802.11 layer. 1086 */ 1087static void 1088rt2560_decryption_intr(struct rt2560_softc *sc) 1089{ 1090 struct ieee80211com *ic = &sc->sc_ic; 1091 struct ifnet *ifp = ic->ic_ifp; 1092 struct rt2560_rx_desc *desc; 1093 struct rt2560_rx_data *data; 1094 bus_addr_t physaddr; 1095 struct ieee80211_frame *wh; 1096 struct ieee80211_node *ni; 1097 struct rt2560_node *rn; 1098 struct mbuf *mnew, *m; 1099 int hw, error; 1100 1101 /* retrieve last decriptor index processed by cipher engine */ 1102 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr; 1103 hw /= RT2560_RX_DESC_SIZE; 1104 1105 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1106 BUS_DMASYNC_POSTREAD); 1107 1108 for (; sc->rxq.cur_decrypt != hw;) { 1109 desc = &sc->rxq.desc[sc->rxq.cur_decrypt]; 1110 data = &sc->rxq.data[sc->rxq.cur_decrypt]; 1111 1112 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1113 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1114 break; 1115 1116 if (data->drop) { 1117 ifp->if_ierrors++; 1118 goto skip; 1119 } 1120 1121 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 && 1122 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) { 1123 ifp->if_ierrors++; 1124 goto skip; 1125 } 1126 1127 /* 1128 * Try to allocate a new mbuf for this ring element and load it 1129 * before processing the current mbuf. If the ring element 1130 * cannot be loaded, drop the received packet and reuse the old 1131 * mbuf. In the unlikely case that the old mbuf can't be 1132 * reloaded either, explicitly panic. 1133 */ 1134 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1135 if (mnew == NULL) { 1136 ifp->if_ierrors++; 1137 goto skip; 1138 } 1139 1140 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1141 BUS_DMASYNC_POSTREAD); 1142 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1143 1144 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1145 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr, 1146 &physaddr, 0); 1147 if (error != 0) { 1148 m_freem(mnew); 1149 1150 /* try to reload the old mbuf */ 1151 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1152 mtod(data->m, void *), MCLBYTES, 1153 rt2560_dma_map_addr, &physaddr, 0); 1154 if (error != 0) { 1155 /* very unlikely that it will fail... */ 1156 panic("%s: could not load old rx mbuf", 1157 device_get_name(sc->sc_dev)); 1158 } 1159 ifp->if_ierrors++; 1160 goto skip; 1161 } 1162 1163 /* 1164 * New mbuf successfully loaded, update Rx ring and continue 1165 * processing. 1166 */ 1167 m = data->m; 1168 data->m = mnew; 1169 desc->physaddr = htole32(physaddr); 1170 1171 /* finalize mbuf */ 1172 m->m_pkthdr.rcvif = ifp; 1173 m->m_pkthdr.len = m->m_len = 1174 (le32toh(desc->flags) >> 16) & 0xfff; 1175 1176 if (bpf_peers_present(sc->sc_drvbpf)) { 1177 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap; 1178 uint32_t tsf_lo, tsf_hi; 1179 1180 /* get timestamp (low and high 32 bits) */ 1181 tsf_hi = RAL_READ(sc, RT2560_CSR17); 1182 tsf_lo = RAL_READ(sc, RT2560_CSR16); 1183 1184 tap->wr_tsf = 1185 htole64(((uint64_t)tsf_hi << 32) | tsf_lo); 1186 tap->wr_flags = 0; 1187 tap->wr_rate = rt2560_rxrate(desc); 1188 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1189 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1190 tap->wr_antenna = sc->rx_ant; 1191 tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi); 1192 1193 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1194 } 1195 1196 sc->sc_flags |= RAL_INPUT_RUNNING; 1197 RAL_UNLOCK(sc); 1198 wh = mtod(m, struct ieee80211_frame *); 1199 ni = ieee80211_find_rxnode(ic, 1200 (struct ieee80211_frame_min *)wh); 1201 1202 /* send the frame to the 802.11 layer */ 1203 ieee80211_input(ic, m, ni, RT2560_RSSI(sc, desc->rssi), 1204 RT2560_NOISE_FLOOR, 0); 1205 1206 /* give rssi to the rate adatation algorithm */ 1207 rn = (struct rt2560_node *)ni; 1208 ral_rssadapt_input(ic, ni, &rn->rssadapt, 1209 RT2560_RSSI(sc, desc->rssi)); 1210 1211 /* node is no longer needed */ 1212 ieee80211_free_node(ni); 1213 1214 RAL_LOCK(sc); 1215 sc->sc_flags &= ~RAL_INPUT_RUNNING; 1216skip: desc->flags = htole32(RT2560_RX_BUSY); 1217 1218 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt)); 1219 1220 sc->rxq.cur_decrypt = 1221 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT; 1222 } 1223 1224 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1225 BUS_DMASYNC_PREWRITE); 1226} 1227 1228/* 1229 * Some frames were received. Pass them to the hardware cipher engine before 1230 * sending them to the 802.11 layer. 1231 */ 1232static void 1233rt2560_rx_intr(struct rt2560_softc *sc) 1234{ 1235 struct rt2560_rx_desc *desc; 1236 struct rt2560_rx_data *data; 1237 1238 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1239 BUS_DMASYNC_POSTREAD); 1240 1241 for (;;) { 1242 desc = &sc->rxq.desc[sc->rxq.cur]; 1243 data = &sc->rxq.data[sc->rxq.cur]; 1244 1245 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1246 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1247 break; 1248 1249 data->drop = 0; 1250 1251 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) || 1252 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) { 1253 /* 1254 * This should not happen since we did not request 1255 * to receive those frames when we filled RXCSR0. 1256 */ 1257 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n", 1258 le32toh(desc->flags))); 1259 data->drop = 1; 1260 } 1261 1262 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) { 1263 DPRINTFN(5, ("bad length\n")); 1264 data->drop = 1; 1265 } 1266 1267 /* mark the frame for decryption */ 1268 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY); 1269 1270 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1271 1272 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT; 1273 } 1274 1275 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1276 BUS_DMASYNC_PREWRITE); 1277 1278 /* kick decrypt */ 1279 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT); 1280} 1281
|
1305 1306 if (bpf_peers_present(ic->ic_rawbpf)) 1307 bpf_mtap(ic->ic_rawbpf, data->m); 1308 1309 rt2560_tx_bcn(sc, data->m, data->ni); 1310 1311 DPRINTFN(15, ("beacon expired\n")); 1312 1313 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT; 1314} 1315 1316/* ARGSUSED */ 1317static void 1318rt2560_wakeup_expire(struct rt2560_softc *sc) 1319{ 1320 DPRINTFN(2, ("wakeup expired\n")); 1321} 1322 1323void 1324rt2560_intr(void *arg) 1325{ 1326 struct rt2560_softc *sc = arg; 1327 struct ifnet *ifp = sc->sc_ifp; 1328 uint32_t r; 1329 1330 RAL_LOCK(sc); 1331 1332 /* disable interrupts */ 1333 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 1334 1335 /* don't re-enable interrupts if we're shutting down */ 1336 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1337 RAL_UNLOCK(sc); 1338 return; 1339 } 1340 1341 r = RAL_READ(sc, RT2560_CSR7); 1342 RAL_WRITE(sc, RT2560_CSR7, r); 1343 1344 if (r & RT2560_BEACON_EXPIRE) 1345 rt2560_beacon_expire(sc); 1346 1347 if (r & RT2560_WAKEUP_EXPIRE) 1348 rt2560_wakeup_expire(sc); 1349 1350 if (r & RT2560_ENCRYPTION_DONE) 1351 rt2560_encryption_intr(sc); 1352 1353 if (r & RT2560_TX_DONE) 1354 rt2560_tx_intr(sc); 1355 1356 if (r & RT2560_PRIO_DONE) 1357 rt2560_prio_intr(sc); 1358 1359 if (r & RT2560_DECRYPTION_DONE) 1360 rt2560_decryption_intr(sc); 1361 1362 if (r & RT2560_RX_DONE) 1363 rt2560_rx_intr(sc); 1364 1365 /* re-enable interrupts */ 1366 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 1367 1368 RAL_UNLOCK(sc); 1369} 1370 1371/* quickly determine if a given rate is CCK or OFDM */ 1372#define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1373 1374#define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */ 1375#define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */ 1376 1377#define RAL_SIFS 10 /* us */ 1378 1379#define RT2560_TXRX_TURNAROUND 10 /* us */ 1380 1381/* 1382 * This function is only used by the Rx radiotap code. 1383 */ 1384static uint8_t 1385rt2560_rxrate(struct rt2560_rx_desc *desc) 1386{ 1387 if (le32toh(desc->flags) & RT2560_RX_OFDM) { 1388 /* reverse function of rt2560_plcp_signal */ 1389 switch (desc->rate) { 1390 case 0xb: return 12; 1391 case 0xf: return 18; 1392 case 0xa: return 24; 1393 case 0xe: return 36; 1394 case 0x9: return 48; 1395 case 0xd: return 72; 1396 case 0x8: return 96; 1397 case 0xc: return 108; 1398 } 1399 } else { 1400 if (desc->rate == 10) 1401 return 2; 1402 if (desc->rate == 20) 1403 return 4; 1404 if (desc->rate == 55) 1405 return 11; 1406 if (desc->rate == 110) 1407 return 22; 1408 } 1409 return 2; /* should not get there */ 1410} 1411 1412/* 1413 * Return the expected ack rate for a frame transmitted at rate `rate'. 1414 * XXX: this should depend on the destination node basic rate set. 1415 */ 1416static int 1417rt2560_ack_rate(struct ieee80211com *ic, int rate) 1418{ 1419 switch (rate) { 1420 /* CCK rates */ 1421 case 2: 1422 return 2; 1423 case 4: 1424 case 11: 1425 case 22: 1426 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate; 1427 1428 /* OFDM rates */ 1429 case 12: 1430 case 18: 1431 return 12; 1432 case 24: 1433 case 36: 1434 return 24; 1435 case 48: 1436 case 72: 1437 case 96: 1438 case 108: 1439 return 48; 1440 } 1441 1442 /* default to 1Mbps */ 1443 return 2; 1444} 1445 1446/* 1447 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'. 1448 * The function automatically determines the operating mode depending on the 1449 * given rate. `flags' indicates whether short preamble is in use or not. 1450 */ 1451static uint16_t 1452rt2560_txtime(int len, int rate, uint32_t flags) 1453{ 1454 uint16_t txtime; 1455 1456 if (RAL_RATE_IS_OFDM(rate)) { 1457 /* IEEE Std 802.11a-1999, pp. 37 */ 1458 txtime = (8 + 4 * len + 3 + rate - 1) / rate; 1459 txtime = 16 + 4 + 4 * txtime + 6; 1460 } else { 1461 /* IEEE Std 802.11b-1999, pp. 28 */ 1462 txtime = (16 * len + rate - 1) / rate; 1463 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) 1464 txtime += 72 + 24; 1465 else 1466 txtime += 144 + 48; 1467 } 1468 1469 return txtime; 1470} 1471 1472static uint8_t 1473rt2560_plcp_signal(int rate) 1474{ 1475 switch (rate) { 1476 /* CCK rates (returned values are device-dependent) */ 1477 case 2: return 0x0; 1478 case 4: return 0x1; 1479 case 11: return 0x2; 1480 case 22: return 0x3; 1481 1482 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1483 case 12: return 0xb; 1484 case 18: return 0xf; 1485 case 24: return 0xa; 1486 case 36: return 0xe; 1487 case 48: return 0x9; 1488 case 72: return 0xd; 1489 case 96: return 0x8; 1490 case 108: return 0xc; 1491 1492 /* unsupported rates (should not get there) */ 1493 default: return 0xff; 1494 } 1495} 1496 1497static void 1498rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc, 1499 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr) 1500{ 1501 struct ieee80211com *ic = &sc->sc_ic; 1502 uint16_t plcp_length; 1503 int remainder; 1504 1505 desc->flags = htole32(flags); 1506 desc->flags |= htole32(len << 16); 1507 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) : 1508 htole32(RT2560_TX_BUSY | RT2560_TX_VALID); 1509 1510 desc->physaddr = htole32(physaddr); 1511 desc->wme = htole16( 1512 RT2560_AIFSN(2) | 1513 RT2560_LOGCWMIN(3) | 1514 RT2560_LOGCWMAX(8)); 1515 1516 /* setup PLCP fields */ 1517 desc->plcp_signal = rt2560_plcp_signal(rate); 1518 desc->plcp_service = 4; 1519 1520 len += IEEE80211_CRC_LEN; 1521 if (RAL_RATE_IS_OFDM(rate)) { 1522 desc->flags |= htole32(RT2560_TX_OFDM); 1523 1524 plcp_length = len & 0xfff; 1525 desc->plcp_length_hi = plcp_length >> 6; 1526 desc->plcp_length_lo = plcp_length & 0x3f; 1527 } else { 1528 plcp_length = (16 * len + rate - 1) / rate; 1529 if (rate == 22) { 1530 remainder = (16 * len) % 22; 1531 if (remainder != 0 && remainder < 7) 1532 desc->plcp_service |= RT2560_PLCP_LENGEXT; 1533 } 1534 desc->plcp_length_hi = plcp_length >> 8; 1535 desc->plcp_length_lo = plcp_length & 0xff; 1536 1537 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1538 desc->plcp_signal |= 0x08; 1539 } 1540} 1541 1542static int 1543rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0, 1544 struct ieee80211_node *ni) 1545{ 1546 struct ieee80211com *ic = &sc->sc_ic; 1547 struct rt2560_tx_desc *desc; 1548 struct rt2560_tx_data *data; 1549 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1550 int nsegs, rate, error; 1551 1552 desc = &sc->bcnq.desc[sc->bcnq.cur]; 1553 data = &sc->bcnq.data[sc->bcnq.cur]; 1554 1555 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2; 1556 1557 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0, 1558 segs, &nsegs, BUS_DMA_NOWAIT); 1559 if (error != 0) { 1560 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1561 error); 1562 m_freem(m0); 1563 return error; 1564 } 1565 1566 if (bpf_peers_present(sc->sc_drvbpf)) { 1567 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1568 1569 tap->wt_flags = 0; 1570 tap->wt_rate = rate; 1571 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1572 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1573 tap->wt_antenna = sc->tx_ant; 1574 1575 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1576 } 1577 1578 data->m = m0; 1579 data->ni = ni; 1580 1581 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF | 1582 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr); 1583 1584 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n", 1585 m0->m_pkthdr.len, sc->bcnq.cur, rate)); 1586 1587 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1588 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map, 1589 BUS_DMASYNC_PREWRITE); 1590 1591 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT; 1592 1593 return 0; 1594} 1595 1596static int 1597rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0, 1598 struct ieee80211_node *ni) 1599{ 1600 struct ieee80211com *ic = &sc->sc_ic; 1601 struct rt2560_tx_desc *desc; 1602 struct rt2560_tx_data *data; 1603 struct ieee80211_frame *wh; 1604 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1605 uint16_t dur; 1606 uint32_t flags = 0; 1607 int nsegs, rate, error; 1608 1609 desc = &sc->prioq.desc[sc->prioq.cur]; 1610 data = &sc->prioq.data[sc->prioq.cur]; 1611 1612 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 1613 1614 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1615 segs, &nsegs, 0); 1616 if (error != 0) { 1617 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1618 error); 1619 m_freem(m0); 1620 return error; 1621 } 1622 1623 if (bpf_peers_present(sc->sc_drvbpf)) { 1624 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1625 1626 tap->wt_flags = 0; 1627 tap->wt_rate = rate; 1628 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1629 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1630 tap->wt_antenna = sc->tx_ant; 1631 1632 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1633 } 1634 1635 data->m = m0; 1636 data->ni = ni; 1637 1638 wh = mtod(m0, struct ieee80211_frame *); 1639 1640 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1641 flags |= RT2560_TX_ACK; 1642 1643 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + 1644 RAL_SIFS; 1645 *(uint16_t *)wh->i_dur = htole16(dur); 1646 1647 /* tell hardware to add timestamp for probe responses */ 1648 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1649 IEEE80211_FC0_TYPE_MGT && 1650 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1651 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1652 flags |= RT2560_TX_TIMESTAMP; 1653 } 1654 1655 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, 1656 segs->ds_addr); 1657 1658 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1659 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1660 BUS_DMASYNC_PREWRITE); 1661 1662 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n", 1663 m0->m_pkthdr.len, sc->prioq.cur, rate)); 1664 1665 /* kick prio */ 1666 sc->prioq.queued++; 1667 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1668 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1669 1670 return 0; 1671} 1672 1673static int 1674rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0, 1675 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 1676{ 1677 struct ieee80211com *ic = &sc->sc_ic; 1678 struct rt2560_tx_desc *desc; 1679 struct rt2560_tx_data *data; 1680 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1681 uint32_t flags; 1682 int nsegs, rate, error; 1683 1684 desc = &sc->prioq.desc[sc->prioq.cur]; 1685 data = &sc->prioq.data[sc->prioq.cur]; 1686 1687 rate = params->ibp_rate0 & IEEE80211_RATE_VAL; 1688 /* XXX validate */ 1689 if (rate == 0) { 1690 m_freem(m0); 1691 return EINVAL; 1692 } 1693 1694 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1695 segs, &nsegs, 0); 1696 if (error != 0) { 1697 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1698 error); 1699 m_freem(m0); 1700 return error; 1701 } 1702 1703 if (bpf_peers_present(sc->sc_drvbpf)) { 1704 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1705 1706 tap->wt_flags = 0; 1707 tap->wt_rate = rate; 1708 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1709 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1710 tap->wt_antenna = sc->tx_ant; 1711 1712 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1713 } 1714 1715 data->m = m0; 1716 data->ni = ni; 1717 1718 flags = 0; 1719 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1720 flags |= RT2560_TX_ACK; 1721 1722 /* XXX need to setup descriptor ourself */ 1723 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1724 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1725 segs->ds_addr); 1726 1727 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1728 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1729 BUS_DMASYNC_PREWRITE); 1730 1731 DPRINTFN(10, ("sending raw frame len=%u idx=%u rate=%u\n", 1732 m0->m_pkthdr.len, sc->prioq.cur, rate)); 1733 1734 /* kick prio */ 1735 sc->prioq.queued++; 1736 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1737 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1738 1739 return 0; 1740} 1741 1742/* 1743 * Build a RTS control frame. 1744 */ 1745static struct mbuf * 1746rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh, 1747 uint16_t dur) 1748{ 1749 struct ieee80211_frame_rts *rts; 1750 struct mbuf *m; 1751 1752 MGETHDR(m, M_DONTWAIT, MT_DATA); 1753 if (m == NULL) { 1754 sc->sc_ic.ic_stats.is_tx_nobuf++; 1755 device_printf(sc->sc_dev, "could not allocate RTS frame\n"); 1756 return NULL; 1757 } 1758 1759 rts = mtod(m, struct ieee80211_frame_rts *); 1760 1761 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL | 1762 IEEE80211_FC0_SUBTYPE_RTS; 1763 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1764 *(uint16_t *)rts->i_dur = htole16(dur); 1765 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1); 1766 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2); 1767 1768 m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts); 1769 1770 return m; 1771} 1772 1773static int 1774rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1775 struct ieee80211_node *ni) 1776{ 1777 struct ieee80211com *ic = &sc->sc_ic; 1778 struct rt2560_tx_desc *desc; 1779 struct rt2560_tx_data *data; 1780 struct rt2560_node *rn; 1781 struct ieee80211_frame *wh; 1782 struct ieee80211_key *k; 1783 struct mbuf *mnew; 1784 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1785 uint16_t dur; 1786 uint32_t flags = 0; 1787 int nsegs, rate, error; 1788 1789 wh = mtod(m0, struct ieee80211_frame *); 1790 1791 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 1792 rate = ic->ic_fixed_rate; 1793 } else { 1794 struct ieee80211_rateset *rs; 1795 1796 rs = &ni->ni_rates; 1797 rn = (struct rt2560_node *)ni; 1798 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh, 1799 m0->m_pkthdr.len, NULL, 0); 1800 rate = rs->rs_rates[ni->ni_txrate]; 1801 } 1802 rate &= IEEE80211_RATE_VAL; 1803 1804 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1805 k = ieee80211_crypto_encap(ic, ni, m0); 1806 if (k == NULL) { 1807 m_freem(m0); 1808 return ENOBUFS; 1809 } 1810 1811 /* packet header may have moved, reset our local pointer */ 1812 wh = mtod(m0, struct ieee80211_frame *); 1813 } 1814 1815 /* 1816 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange 1817 * for directed frames only when the length of the MPDU is greater 1818 * than the length threshold indicated by [...]" ic_rtsthreshold. 1819 */ 1820 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 1821 m0->m_pkthdr.len > ic->ic_rtsthreshold) { 1822 struct mbuf *m; 1823 uint16_t dur; 1824 int rtsrate, ackrate; 1825 1826 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 1827 ackrate = rt2560_ack_rate(ic, rate); 1828 1829 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) + 1830 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) + 1831 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) + 1832 3 * RAL_SIFS; 1833 1834 m = rt2560_get_rts(sc, wh, dur); 1835 1836 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1837 data = &sc->txq.data[sc->txq.cur_encrypt]; 1838 1839 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1840 m, segs, &nsegs, 0); 1841 if (error != 0) { 1842 device_printf(sc->sc_dev, 1843 "could not map mbuf (error %d)\n", error); 1844 m_freem(m); 1845 m_freem(m0); 1846 return error; 1847 } 1848 1849 /* avoid multiple free() of the same node for each fragment */ 1850 ieee80211_ref_node(ni); 1851 1852 data->m = m; 1853 data->ni = ni; 1854 1855 /* RTS frames are not taken into account for rssadapt */ 1856 data->id.id_node = NULL; 1857 1858 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK | 1859 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, 1860 segs->ds_addr); 1861 1862 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1863 BUS_DMASYNC_PREWRITE); 1864 1865 sc->txq.queued++; 1866 sc->txq.cur_encrypt = 1867 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1868 1869 /* 1870 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the 1871 * asynchronous data frame shall be transmitted after the CTS 1872 * frame and a SIFS period. 1873 */ 1874 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1875 } 1876 1877 data = &sc->txq.data[sc->txq.cur_encrypt]; 1878 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1879 1880 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, 1881 segs, &nsegs, 0); 1882 if (error != 0 && error != EFBIG) { 1883 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1884 error); 1885 m_freem(m0); 1886 return error; 1887 } 1888 if (error != 0) { 1889 mnew = m_defrag(m0, M_DONTWAIT); 1890 if (mnew == NULL) { 1891 device_printf(sc->sc_dev, 1892 "could not defragment mbuf\n"); 1893 m_freem(m0); 1894 return ENOBUFS; 1895 } 1896 m0 = mnew; 1897 1898 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1899 m0, segs, &nsegs, 0); 1900 if (error != 0) { 1901 device_printf(sc->sc_dev, 1902 "could not map mbuf (error %d)\n", error); 1903 m_freem(m0); 1904 return error; 1905 } 1906 1907 /* packet header may have moved, reset our local pointer */ 1908 wh = mtod(m0, struct ieee80211_frame *); 1909 } 1910 1911 if (bpf_peers_present(sc->sc_drvbpf)) { 1912 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1913 1914 tap->wt_flags = 0; 1915 tap->wt_rate = rate; 1916 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1917 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1918 tap->wt_antenna = sc->tx_ant; 1919 1920 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1921 } 1922 1923 data->m = m0; 1924 data->ni = ni; 1925 1926 /* remember link conditions for rate adaptation algorithm */ 1927 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 1928 data->id.id_len = m0->m_pkthdr.len; 1929 data->id.id_rateidx = ni->ni_txrate; 1930 data->id.id_node = ni; 1931 data->id.id_rssi = ni->ni_rssi; 1932 } else 1933 data->id.id_node = NULL; 1934 1935 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1936 flags |= RT2560_TX_ACK; 1937 1938 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate), 1939 ic->ic_flags) + RAL_SIFS; 1940 *(uint16_t *)wh->i_dur = htole16(dur); 1941 } 1942 1943 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1944 segs->ds_addr); 1945 1946 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1947 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1948 BUS_DMASYNC_PREWRITE); 1949 1950 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n", 1951 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate)); 1952 1953 /* kick encrypt */ 1954 sc->txq.queued++; 1955 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1956 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1957 1958 return 0; 1959} 1960 1961static void 1962rt2560_start(struct ifnet *ifp) 1963{ 1964 struct rt2560_softc *sc = ifp->if_softc; 1965 struct ieee80211com *ic = &sc->sc_ic; 1966 struct mbuf *m0; 1967 struct ether_header *eh; 1968 struct ieee80211_node *ni; 1969 1970 RAL_LOCK(sc); 1971 1972 /* prevent management frames from being sent if we're not ready */ 1973 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1974 RAL_UNLOCK(sc); 1975 return; 1976 } 1977 1978 for (;;) { 1979 IF_POLL(&ic->ic_mgtq, m0); 1980 if (m0 != NULL) { 1981 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 1982 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1983 break; 1984 } 1985 IF_DEQUEUE(&ic->ic_mgtq, m0); 1986 1987 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1988 m0->m_pkthdr.rcvif = NULL; 1989 1990 if (bpf_peers_present(ic->ic_rawbpf)) 1991 bpf_mtap(ic->ic_rawbpf, m0); 1992 1993 if (rt2560_tx_mgt(sc, m0, ni) != 0) { 1994 ieee80211_free_node(ni); 1995 break; 1996 } 1997 } else { 1998 if (ic->ic_state != IEEE80211_S_RUN) 1999 break; 2000 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2001 if (m0 == NULL) 2002 break; 2003 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) { 2004 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2005 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2006 break; 2007 } 2008 /* 2009 * Cancel any background scan. 2010 */ 2011 if (ic->ic_flags & IEEE80211_F_SCAN) 2012 ieee80211_cancel_scan(ic); 2013 2014 if (m0->m_len < sizeof (struct ether_header) && 2015 !(m0 = m_pullup(m0, sizeof (struct ether_header)))) 2016 continue; 2017 2018 eh = mtod(m0, struct ether_header *); 2019 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2020 if (ni == NULL) { 2021 m_freem(m0); 2022 continue; 2023 } 2024 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 2025 (m0->m_flags & M_PWR_SAV) == 0) { 2026 /* 2027 * Station in power save mode; pass the frame 2028 * to the 802.11 layer and continue. We'll get 2029 * the frame back when the time is right. 2030 */ 2031 ieee80211_pwrsave(ni, m0); 2032 /* 2033 * If we're in power save mode 'cuz of a bg 2034 * scan cancel it so the traffic can flow. 2035 * The packet we just queued will automatically 2036 * get sent when we drop out of power save. 2037 * XXX locking 2038 */ 2039 if (ic->ic_flags & IEEE80211_F_SCAN) 2040 ieee80211_cancel_scan(ic); 2041 ieee80211_free_node(ni); 2042 continue; 2043 2044 } 2045 2046 BPF_MTAP(ifp, m0); 2047 2048 m0 = ieee80211_encap(ic, m0, ni); 2049 if (m0 == NULL) { 2050 ieee80211_free_node(ni); 2051 continue; 2052 } 2053 2054 if (bpf_peers_present(ic->ic_rawbpf)) 2055 bpf_mtap(ic->ic_rawbpf, m0); 2056 2057 if (rt2560_tx_data(sc, m0, ni) != 0) { 2058 ieee80211_free_node(ni); 2059 ifp->if_oerrors++; 2060 break; 2061 } 2062 } 2063 2064 sc->sc_tx_timer = 5; 2065 ic->ic_lastdata = ticks; 2066 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2067 } 2068 2069 RAL_UNLOCK(sc); 2070} 2071 2072static void 2073rt2560_watchdog(void *arg) 2074{ 2075 struct rt2560_softc *sc = arg; 2076 2077 if (sc->sc_tx_timer > 0) { 2078 if (--sc->sc_tx_timer == 0) { 2079 device_printf(sc->sc_dev, "device timeout\n"); 2080 rt2560_init(sc); 2081 sc->sc_ifp->if_oerrors++; 2082 return; 2083 } 2084 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2085 } 2086} 2087 2088/* 2089 * This function allows for fast channel switching in monitor mode (used by 2090 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to 2091 * generate a new beacon frame. 2092 */ 2093static int 2094rt2560_reset(struct ifnet *ifp) 2095{ 2096 struct rt2560_softc *sc = ifp->if_softc; 2097 struct ieee80211com *ic = &sc->sc_ic; 2098 2099 if (ic->ic_opmode != IEEE80211_M_MONITOR) 2100 return ENETRESET; 2101 2102 rt2560_set_chan(sc, ic->ic_curchan); 2103 2104 return 0; 2105} 2106 2107static int 2108rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2109{ 2110 struct rt2560_softc *sc = ifp->if_softc; 2111 struct ieee80211com *ic = &sc->sc_ic; 2112 int error = 0; 2113 2114 2115 2116 switch (cmd) { 2117 case SIOCSIFFLAGS: 2118 if (ifp->if_flags & IFF_UP) { 2119 RAL_LOCK(sc); 2120 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2121 rt2560_update_promisc(sc); 2122 else 2123 rt2560_init(sc); 2124 RAL_UNLOCK(sc); 2125 } else { 2126 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2127 rt2560_stop(sc); 2128 } 2129 2130 break; 2131 2132 default: 2133 error = ieee80211_ioctl(ic, cmd, data); 2134 } 2135 2136 if (error == ENETRESET) { 2137 if ((ifp->if_flags & IFF_UP) && 2138 (ifp->if_drv_flags & IFF_DRV_RUNNING) && 2139 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 2140 rt2560_init(sc); 2141 error = 0; 2142 } 2143 2144 2145 return error; 2146} 2147 2148static void 2149rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 2150{ 2151 uint32_t tmp; 2152 int ntries; 2153 2154 for (ntries = 0; ntries < 100; ntries++) { 2155 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2156 break; 2157 DELAY(1); 2158 } 2159 if (ntries == 100) { 2160 device_printf(sc->sc_dev, "could not write to BBP\n"); 2161 return; 2162 } 2163 2164 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 2165 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 2166 2167 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val)); 2168} 2169 2170static uint8_t 2171rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 2172{ 2173 uint32_t val; 2174 int ntries; 2175 2176 val = RT2560_BBP_BUSY | reg << 8; 2177 RAL_WRITE(sc, RT2560_BBPCSR, val); 2178 2179 for (ntries = 0; ntries < 100; ntries++) { 2180 val = RAL_READ(sc, RT2560_BBPCSR); 2181 if (!(val & RT2560_BBP_BUSY)) 2182 return val & 0xff; 2183 DELAY(1); 2184 } 2185 2186 device_printf(sc->sc_dev, "could not read from BBP\n"); 2187 return 0; 2188} 2189 2190static void 2191rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2192{ 2193 uint32_t tmp; 2194 int ntries; 2195 2196 for (ntries = 0; ntries < 100; ntries++) { 2197 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2198 break; 2199 DELAY(1); 2200 } 2201 if (ntries == 100) { 2202 device_printf(sc->sc_dev, "could not write to RF\n"); 2203 return; 2204 } 2205 2206 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2207 (reg & 0x3); 2208 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2209 2210 /* remember last written value in sc */ 2211 sc->rf_regs[reg] = val; 2212 2213 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff)); 2214} 2215 2216static void 2217rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2218{ 2219 struct ieee80211com *ic = &sc->sc_ic; 2220 uint8_t power, tmp; 2221 u_int i, chan; 2222 2223 chan = ieee80211_chan2ieee(ic, c); 2224 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 2225 return; 2226 2227 if (IEEE80211_IS_CHAN_2GHZ(c)) 2228 power = min(sc->txpow[chan - 1], 31); 2229 else 2230 power = 31; 2231 2232 /* adjust txpower using ifconfig settings */ 2233 power -= (100 - ic->ic_txpowlimit) / 8; 2234 2235 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power)); 2236 2237 switch (sc->rf_rev) { 2238 case RT2560_RF_2522: 2239 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2240 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2241 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2242 break; 2243 2244 case RT2560_RF_2523: 2245 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2246 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2247 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2248 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2249 break; 2250 2251 case RT2560_RF_2524: 2252 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2253 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2254 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2255 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2256 break; 2257 2258 case RT2560_RF_2525: 2259 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2260 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2261 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2262 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2263 2264 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2265 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2266 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2267 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2268 break; 2269 2270 case RT2560_RF_2525E: 2271 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2272 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2273 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2274 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2275 break; 2276 2277 case RT2560_RF_2526: 2278 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2279 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2280 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2281 2282 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2283 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2284 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2285 break; 2286 2287 /* dual-band RF */ 2288 case RT2560_RF_5222: 2289 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2290 2291 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2292 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2293 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2294 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2295 break; 2296 default: 2297 printf("unknown ral rev=%d\n", sc->rf_rev); 2298 } 2299 2300 if (ic->ic_state != IEEE80211_S_SCAN) { 2301 /* set Japan filter bit for channel 14 */ 2302 tmp = rt2560_bbp_read(sc, 70); 2303 2304 tmp &= ~RT2560_JAPAN_FILTER; 2305 if (chan == 14) 2306 tmp |= RT2560_JAPAN_FILTER; 2307 2308 rt2560_bbp_write(sc, 70, tmp); 2309 2310 /* clear CRC errors */ 2311 RAL_READ(sc, RT2560_CNT0); 2312 } 2313} 2314 2315static void 2316rt2560_set_channel(struct ieee80211com *ic) 2317{ 2318 struct ifnet *ifp = ic->ic_ifp; 2319 struct rt2560_softc *sc = ifp->if_softc; 2320 2321 RAL_LOCK(sc); 2322 rt2560_set_chan(sc, ic->ic_curchan); 2323 RAL_UNLOCK(sc); 2324 2325} 2326 2327#if 0 2328/* 2329 * Disable RF auto-tuning. 2330 */ 2331static void 2332rt2560_disable_rf_tune(struct rt2560_softc *sc) 2333{ 2334 uint32_t tmp; 2335 2336 if (sc->rf_rev != RT2560_RF_2523) { 2337 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2338 rt2560_rf_write(sc, RAL_RF1, tmp); 2339 } 2340 2341 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2342 rt2560_rf_write(sc, RAL_RF3, tmp); 2343 2344 DPRINTFN(2, ("disabling RF autotune\n")); 2345} 2346#endif 2347 2348/* 2349 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2350 * synchronization. 2351 */ 2352static void 2353rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2354{ 2355 struct ieee80211com *ic = &sc->sc_ic; 2356 uint16_t logcwmin, preload; 2357 uint32_t tmp; 2358 2359 /* first, disable TSF synchronization */ 2360 RAL_WRITE(sc, RT2560_CSR14, 0); 2361 2362 tmp = 16 * ic->ic_bss->ni_intval; 2363 RAL_WRITE(sc, RT2560_CSR12, tmp); 2364 2365 RAL_WRITE(sc, RT2560_CSR13, 0); 2366 2367 logcwmin = 5; 2368 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024; 2369 tmp = logcwmin << 16 | preload; 2370 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2371 2372 /* finally, enable TSF synchronization */ 2373 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2374 if (ic->ic_opmode == IEEE80211_M_STA) 2375 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2376 else 2377 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2378 RT2560_ENABLE_BEACON_GENERATOR; 2379 RAL_WRITE(sc, RT2560_CSR14, tmp); 2380 2381 DPRINTF(("enabling TSF synchronization\n")); 2382} 2383 2384static void 2385rt2560_update_plcp(struct rt2560_softc *sc) 2386{ 2387 struct ieee80211com *ic = &sc->sc_ic; 2388 2389 /* no short preamble for 1Mbps */ 2390 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2391 2392 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2393 /* values taken from the reference driver */ 2394 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2395 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2396 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2397 } else { 2398 /* same values as above or'ed 0x8 */ 2399 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2400 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2401 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2402 } 2403 2404 DPRINTF(("updating PLCP for %s preamble\n", 2405 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long")); 2406} 2407 2408/* 2409 * This function can be called by ieee80211_set_shortslottime(). Refer to 2410 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2411 */ 2412static void 2413rt2560_update_slot(struct ifnet *ifp) 2414{ 2415 struct rt2560_softc *sc = ifp->if_softc; 2416 struct ieee80211com *ic = &sc->sc_ic; 2417 uint8_t slottime; 2418 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2419 uint32_t tmp; 2420 2421 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 2422 2423 /* update the MAC slot boundaries */ 2424 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2425 tx_pifs = tx_sifs + slottime; 2426 tx_difs = tx_sifs + 2 * slottime; 2427 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2428 2429 tmp = RAL_READ(sc, RT2560_CSR11); 2430 tmp = (tmp & ~0x1f00) | slottime << 8; 2431 RAL_WRITE(sc, RT2560_CSR11, tmp); 2432 2433 tmp = tx_pifs << 16 | tx_sifs; 2434 RAL_WRITE(sc, RT2560_CSR18, tmp); 2435 2436 tmp = eifs << 16 | tx_difs; 2437 RAL_WRITE(sc, RT2560_CSR19, tmp); 2438 2439 DPRINTF(("setting slottime to %uus\n", slottime)); 2440} 2441 2442static void 2443rt2560_set_basicrates(struct rt2560_softc *sc) 2444{ 2445 struct ieee80211com *ic = &sc->sc_ic; 2446 2447 /* update basic rate set */ 2448 if (ic->ic_curmode == IEEE80211_MODE_11B) { 2449 /* 11b basic rates: 1, 2Mbps */ 2450 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3); 2451 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) { 2452 /* 11a basic rates: 6, 12, 24Mbps */ 2453 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150); 2454 } else { 2455 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 2456 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f); 2457 } 2458} 2459 2460static void 2461rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2462{ 2463 uint32_t tmp; 2464 2465 /* set ON period to 70ms and OFF period to 30ms */ 2466 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2467 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2468} 2469 2470static void 2471rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2472{ 2473 uint32_t tmp; 2474 2475 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2476 RAL_WRITE(sc, RT2560_CSR5, tmp); 2477 2478 tmp = bssid[4] | bssid[5] << 8; 2479 RAL_WRITE(sc, RT2560_CSR6, tmp); 2480 2481 DPRINTF(("setting BSSID to %6D\n", bssid, ":")); 2482} 2483 2484static void 2485rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2486{ 2487 uint32_t tmp; 2488 2489 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2490 RAL_WRITE(sc, RT2560_CSR3, tmp); 2491 2492 tmp = addr[4] | addr[5] << 8; 2493 RAL_WRITE(sc, RT2560_CSR4, tmp); 2494 2495 DPRINTF(("setting MAC address to %6D\n", addr, ":")); 2496} 2497 2498static void 2499rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2500{ 2501 uint32_t tmp; 2502 2503 tmp = RAL_READ(sc, RT2560_CSR3); 2504 addr[0] = tmp & 0xff; 2505 addr[1] = (tmp >> 8) & 0xff; 2506 addr[2] = (tmp >> 16) & 0xff; 2507 addr[3] = (tmp >> 24); 2508 2509 tmp = RAL_READ(sc, RT2560_CSR4); 2510 addr[4] = tmp & 0xff; 2511 addr[5] = (tmp >> 8) & 0xff; 2512} 2513 2514static void 2515rt2560_update_promisc(struct rt2560_softc *sc) 2516{ 2517 struct ifnet *ifp = sc->sc_ic.ic_ifp; 2518 uint32_t tmp; 2519 2520 tmp = RAL_READ(sc, RT2560_RXCSR0); 2521 2522 tmp &= ~RT2560_DROP_NOT_TO_ME; 2523 if (!(ifp->if_flags & IFF_PROMISC)) 2524 tmp |= RT2560_DROP_NOT_TO_ME; 2525 2526 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2527 2528 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 2529 "entering" : "leaving")); 2530} 2531 2532static const char * 2533rt2560_get_rf(int rev) 2534{ 2535 switch (rev) { 2536 case RT2560_RF_2522: return "RT2522"; 2537 case RT2560_RF_2523: return "RT2523"; 2538 case RT2560_RF_2524: return "RT2524"; 2539 case RT2560_RF_2525: return "RT2525"; 2540 case RT2560_RF_2525E: return "RT2525e"; 2541 case RT2560_RF_2526: return "RT2526"; 2542 case RT2560_RF_5222: return "RT5222"; 2543 default: return "unknown"; 2544 } 2545} 2546 2547static void 2548rt2560_read_eeprom(struct rt2560_softc *sc) 2549{ 2550 uint16_t val; 2551 int i; 2552 2553 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2554 sc->rf_rev = (val >> 11) & 0x7; 2555 sc->hw_radio = (val >> 10) & 0x1; 2556 sc->led_mode = (val >> 6) & 0x7; 2557 sc->rx_ant = (val >> 4) & 0x3; 2558 sc->tx_ant = (val >> 2) & 0x3; 2559 sc->nb_ant = val & 0x3; 2560 2561 /* read default values for BBP registers */ 2562 for (i = 0; i < 16; i++) { 2563 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2564 sc->bbp_prom[i].reg = val >> 8; 2565 sc->bbp_prom[i].val = val & 0xff; 2566 } 2567 2568 /* read Tx power for all b/g channels */ 2569 for (i = 0; i < 14 / 2; i++) { 2570 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2571 sc->txpow[i * 2] = val >> 8; 2572 sc->txpow[i * 2 + 1] = val & 0xff; 2573 } 2574 2575 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2576 if ((val & 0xff) == 0xff) 2577 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2578 else 2579 sc->rssi_corr = val & 0xff; 2580 DPRINTF(("rssi correction %d, calibrate 0x%02x\n", 2581 sc->rssi_corr, val)); 2582} 2583 2584 2585static void 2586rt2560_scan_start(struct ieee80211com *ic) 2587{ 2588 struct ifnet *ifp = ic->ic_ifp; 2589 struct rt2560_softc *sc = ifp->if_softc; 2590 2591 /* abort TSF synchronization */ 2592 RAL_WRITE(sc, RT2560_CSR14, 0); 2593 rt2560_set_bssid(sc, ifp->if_broadcastaddr); 2594} 2595 2596static void 2597rt2560_scan_end(struct ieee80211com *ic) 2598{ 2599 struct ifnet *ifp = ic->ic_ifp; 2600 struct rt2560_softc *sc = ifp->if_softc; 2601 2602 rt2560_enable_tsf_sync(sc); 2603 /* XXX keep local copy */ 2604 rt2560_set_bssid(sc, ic->ic_bss->ni_bssid); 2605} 2606 2607static int 2608rt2560_bbp_init(struct rt2560_softc *sc) 2609{ 2610#define N(a) (sizeof (a) / sizeof ((a)[0])) 2611 int i, ntries; 2612 2613 /* wait for BBP to be ready */ 2614 for (ntries = 0; ntries < 100; ntries++) { 2615 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2616 break; 2617 DELAY(1); 2618 } 2619 if (ntries == 100) { 2620 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2621 return EIO; 2622 } 2623 2624 /* initialize BBP registers to default values */ 2625 for (i = 0; i < N(rt2560_def_bbp); i++) { 2626 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2627 rt2560_def_bbp[i].val); 2628 } 2629#if 0 2630 /* initialize BBP registers to values stored in EEPROM */ 2631 for (i = 0; i < 16; i++) { 2632 if (sc->bbp_prom[i].reg == 0xff) 2633 continue; 2634 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2635 } 2636#endif 2637 2638 return 0; 2639#undef N 2640} 2641 2642static void 2643rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2644{ 2645 uint32_t tmp; 2646 uint8_t tx; 2647 2648 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2649 if (antenna == 1) 2650 tx |= RT2560_BBP_ANTA; 2651 else if (antenna == 2) 2652 tx |= RT2560_BBP_ANTB; 2653 else 2654 tx |= RT2560_BBP_DIVERSITY; 2655 2656 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2657 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2658 sc->rf_rev == RT2560_RF_5222) 2659 tx |= RT2560_BBP_FLIPIQ; 2660 2661 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2662 2663 /* update values for CCK and OFDM in BBPCSR1 */ 2664 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2665 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2666 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2667} 2668 2669static void 2670rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2671{ 2672 uint8_t rx; 2673 2674 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2675 if (antenna == 1) 2676 rx |= RT2560_BBP_ANTA; 2677 else if (antenna == 2) 2678 rx |= RT2560_BBP_ANTB; 2679 else 2680 rx |= RT2560_BBP_DIVERSITY; 2681 2682 /* need to force no I/Q flip for RF 2525e and 2526 */ 2683 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2684 rx &= ~RT2560_BBP_FLIPIQ; 2685 2686 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2687} 2688 2689static void 2690rt2560_init(void *priv) 2691{ 2692#define N(a) (sizeof (a) / sizeof ((a)[0])) 2693 struct rt2560_softc *sc = priv; 2694 struct ieee80211com *ic = &sc->sc_ic; 2695 struct ifnet *ifp = ic->ic_ifp; 2696 uint32_t tmp; 2697 int i; 2698 2699 2700 2701 rt2560_stop(sc); 2702 2703 RAL_LOCK(sc); 2704 /* setup tx rings */ 2705 tmp = RT2560_PRIO_RING_COUNT << 24 | 2706 RT2560_ATIM_RING_COUNT << 16 | 2707 RT2560_TX_RING_COUNT << 8 | 2708 RT2560_TX_DESC_SIZE; 2709 2710 /* rings must be initialized in this exact order */ 2711 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2712 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2713 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2714 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2715 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2716 2717 /* setup rx ring */ 2718 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2719 2720 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2721 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2722 2723 /* initialize MAC registers to default values */ 2724 for (i = 0; i < N(rt2560_def_mac); i++) 2725 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2726 2727 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2728 rt2560_set_macaddr(sc, ic->ic_myaddr); 2729 2730 /* set basic rate set (will be updated later) */ 2731 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2732 2733 rt2560_set_txantenna(sc, sc->tx_ant); 2734 rt2560_set_rxantenna(sc, sc->rx_ant); 2735 rt2560_update_slot(ifp); 2736 rt2560_update_plcp(sc); 2737 rt2560_update_led(sc, 0, 0); 2738 2739 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2740 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2741 2742 if (rt2560_bbp_init(sc) != 0) { 2743 rt2560_stop(sc); 2744 RAL_UNLOCK(sc); 2745 return; 2746 } 2747 2748 /* set default BSS channel */ 2749 rt2560_set_chan(sc, ic->ic_curchan); 2750 2751 /* kick Rx */ 2752 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2753 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2754 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2755 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2756 tmp |= RT2560_DROP_TODS; 2757 if (!(ifp->if_flags & IFF_PROMISC)) 2758 tmp |= RT2560_DROP_NOT_TO_ME; 2759 } 2760 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2761 2762 /* clear old FCS and Rx FIFO errors */ 2763 RAL_READ(sc, RT2560_CNT0); 2764 RAL_READ(sc, RT2560_CNT4); 2765 2766 /* clear any pending interrupts */ 2767 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2768 2769 /* enable interrupts */ 2770 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2771 2772 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2773 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2774 2775 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2776 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2777 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2778 } else 2779 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2780 2781 RAL_UNLOCK(sc); 2782#undef N 2783} 2784 2785void 2786rt2560_stop(void *arg) 2787{ 2788 struct rt2560_softc *sc = arg; 2789 struct ieee80211com *ic = &sc->sc_ic; 2790 struct ifnet *ifp = ic->ic_ifp; 2791 volatile int *flags = &sc->sc_flags; 2792 2793 while (*flags & RAL_INPUT_RUNNING) { 2794 tsleep(sc, 0, "ralrunning", hz/10); 2795 } 2796 2797 RAL_LOCK(sc); 2798 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2799 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2800 sc->sc_tx_timer = 0; 2801 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2802 2803 /* abort Tx */ 2804 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2805 2806 /* disable Rx */ 2807 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2808 2809 /* reset ASIC (imply reset BBP) */ 2810 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2811 RAL_WRITE(sc, RT2560_CSR1, 0); 2812 2813 /* disable interrupts */ 2814 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2815 2816 /* reset Tx and Rx rings */ 2817 rt2560_reset_tx_ring(sc, &sc->txq); 2818 rt2560_reset_tx_ring(sc, &sc->atimq); 2819 rt2560_reset_tx_ring(sc, &sc->prioq); 2820 rt2560_reset_tx_ring(sc, &sc->bcnq); 2821 rt2560_reset_rx_ring(sc, &sc->rxq); 2822 } 2823 RAL_UNLOCK(sc); 2824} 2825 2826static int 2827rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2828 const struct ieee80211_bpf_params *params) 2829{ 2830 struct ieee80211com *ic = ni->ni_ic; 2831 struct ifnet *ifp = ic->ic_ifp; 2832 struct rt2560_softc *sc = ifp->if_softc; 2833 2834 RAL_LOCK(sc); 2835 2836 /* prevent management frames from being sent if we're not ready */ 2837 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2838 RAL_UNLOCK(sc); 2839 m_freem(m); 2840 ieee80211_free_node(ni); 2841 return ENETDOWN; 2842 } 2843 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2844 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2845 RAL_UNLOCK(sc); 2846 m_freem(m); 2847 ieee80211_free_node(ni); 2848 return ENOBUFS; /* XXX */ 2849 } 2850 2851 if (bpf_peers_present(ic->ic_rawbpf)) 2852 bpf_mtap(ic->ic_rawbpf, m); 2853 2854 ifp->if_opackets++; 2855 2856 if (params == NULL) { 2857 /* 2858 * Legacy path; interpret frame contents to decide 2859 * precisely how to send the frame. 2860 */ 2861 if (rt2560_tx_mgt(sc, m, ni) != 0) 2862 goto bad; 2863 } else { 2864 /* 2865 * Caller supplied explicit parameters to use in 2866 * sending the frame. 2867 */ 2868 if (rt2560_tx_raw(sc, m, ni, params)) 2869 goto bad; 2870 } 2871 sc->sc_tx_timer = 5; 2872 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2873 2874 RAL_UNLOCK(sc); 2875 2876 return 0; 2877bad: 2878 ifp->if_oerrors++; 2879 ieee80211_free_node(ni); 2880 RAL_UNLOCK(sc); 2881 return EIO; /* XXX */ 2882} 2883
| 1316 1317 if (bpf_peers_present(ic->ic_rawbpf)) 1318 bpf_mtap(ic->ic_rawbpf, data->m); 1319 1320 rt2560_tx_bcn(sc, data->m, data->ni); 1321 1322 DPRINTFN(15, ("beacon expired\n")); 1323 1324 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT; 1325} 1326 1327/* ARGSUSED */ 1328static void 1329rt2560_wakeup_expire(struct rt2560_softc *sc) 1330{ 1331 DPRINTFN(2, ("wakeup expired\n")); 1332} 1333 1334void 1335rt2560_intr(void *arg) 1336{ 1337 struct rt2560_softc *sc = arg; 1338 struct ifnet *ifp = sc->sc_ifp; 1339 uint32_t r; 1340 1341 RAL_LOCK(sc); 1342 1343 /* disable interrupts */ 1344 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 1345 1346 /* don't re-enable interrupts if we're shutting down */ 1347 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1348 RAL_UNLOCK(sc); 1349 return; 1350 } 1351 1352 r = RAL_READ(sc, RT2560_CSR7); 1353 RAL_WRITE(sc, RT2560_CSR7, r); 1354 1355 if (r & RT2560_BEACON_EXPIRE) 1356 rt2560_beacon_expire(sc); 1357 1358 if (r & RT2560_WAKEUP_EXPIRE) 1359 rt2560_wakeup_expire(sc); 1360 1361 if (r & RT2560_ENCRYPTION_DONE) 1362 rt2560_encryption_intr(sc); 1363 1364 if (r & RT2560_TX_DONE) 1365 rt2560_tx_intr(sc); 1366 1367 if (r & RT2560_PRIO_DONE) 1368 rt2560_prio_intr(sc); 1369 1370 if (r & RT2560_DECRYPTION_DONE) 1371 rt2560_decryption_intr(sc); 1372 1373 if (r & RT2560_RX_DONE) 1374 rt2560_rx_intr(sc); 1375 1376 /* re-enable interrupts */ 1377 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 1378 1379 RAL_UNLOCK(sc); 1380} 1381 1382/* quickly determine if a given rate is CCK or OFDM */ 1383#define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1384 1385#define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */ 1386#define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */ 1387 1388#define RAL_SIFS 10 /* us */ 1389 1390#define RT2560_TXRX_TURNAROUND 10 /* us */ 1391 1392/* 1393 * This function is only used by the Rx radiotap code. 1394 */ 1395static uint8_t 1396rt2560_rxrate(struct rt2560_rx_desc *desc) 1397{ 1398 if (le32toh(desc->flags) & RT2560_RX_OFDM) { 1399 /* reverse function of rt2560_plcp_signal */ 1400 switch (desc->rate) { 1401 case 0xb: return 12; 1402 case 0xf: return 18; 1403 case 0xa: return 24; 1404 case 0xe: return 36; 1405 case 0x9: return 48; 1406 case 0xd: return 72; 1407 case 0x8: return 96; 1408 case 0xc: return 108; 1409 } 1410 } else { 1411 if (desc->rate == 10) 1412 return 2; 1413 if (desc->rate == 20) 1414 return 4; 1415 if (desc->rate == 55) 1416 return 11; 1417 if (desc->rate == 110) 1418 return 22; 1419 } 1420 return 2; /* should not get there */ 1421} 1422 1423/* 1424 * Return the expected ack rate for a frame transmitted at rate `rate'. 1425 * XXX: this should depend on the destination node basic rate set. 1426 */ 1427static int 1428rt2560_ack_rate(struct ieee80211com *ic, int rate) 1429{ 1430 switch (rate) { 1431 /* CCK rates */ 1432 case 2: 1433 return 2; 1434 case 4: 1435 case 11: 1436 case 22: 1437 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate; 1438 1439 /* OFDM rates */ 1440 case 12: 1441 case 18: 1442 return 12; 1443 case 24: 1444 case 36: 1445 return 24; 1446 case 48: 1447 case 72: 1448 case 96: 1449 case 108: 1450 return 48; 1451 } 1452 1453 /* default to 1Mbps */ 1454 return 2; 1455} 1456 1457/* 1458 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'. 1459 * The function automatically determines the operating mode depending on the 1460 * given rate. `flags' indicates whether short preamble is in use or not. 1461 */ 1462static uint16_t 1463rt2560_txtime(int len, int rate, uint32_t flags) 1464{ 1465 uint16_t txtime; 1466 1467 if (RAL_RATE_IS_OFDM(rate)) { 1468 /* IEEE Std 802.11a-1999, pp. 37 */ 1469 txtime = (8 + 4 * len + 3 + rate - 1) / rate; 1470 txtime = 16 + 4 + 4 * txtime + 6; 1471 } else { 1472 /* IEEE Std 802.11b-1999, pp. 28 */ 1473 txtime = (16 * len + rate - 1) / rate; 1474 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE)) 1475 txtime += 72 + 24; 1476 else 1477 txtime += 144 + 48; 1478 } 1479 1480 return txtime; 1481} 1482 1483static uint8_t 1484rt2560_plcp_signal(int rate) 1485{ 1486 switch (rate) { 1487 /* CCK rates (returned values are device-dependent) */ 1488 case 2: return 0x0; 1489 case 4: return 0x1; 1490 case 11: return 0x2; 1491 case 22: return 0x3; 1492 1493 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1494 case 12: return 0xb; 1495 case 18: return 0xf; 1496 case 24: return 0xa; 1497 case 36: return 0xe; 1498 case 48: return 0x9; 1499 case 72: return 0xd; 1500 case 96: return 0x8; 1501 case 108: return 0xc; 1502 1503 /* unsupported rates (should not get there) */ 1504 default: return 0xff; 1505 } 1506} 1507 1508static void 1509rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc, 1510 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr) 1511{ 1512 struct ieee80211com *ic = &sc->sc_ic; 1513 uint16_t plcp_length; 1514 int remainder; 1515 1516 desc->flags = htole32(flags); 1517 desc->flags |= htole32(len << 16); 1518 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) : 1519 htole32(RT2560_TX_BUSY | RT2560_TX_VALID); 1520 1521 desc->physaddr = htole32(physaddr); 1522 desc->wme = htole16( 1523 RT2560_AIFSN(2) | 1524 RT2560_LOGCWMIN(3) | 1525 RT2560_LOGCWMAX(8)); 1526 1527 /* setup PLCP fields */ 1528 desc->plcp_signal = rt2560_plcp_signal(rate); 1529 desc->plcp_service = 4; 1530 1531 len += IEEE80211_CRC_LEN; 1532 if (RAL_RATE_IS_OFDM(rate)) { 1533 desc->flags |= htole32(RT2560_TX_OFDM); 1534 1535 plcp_length = len & 0xfff; 1536 desc->plcp_length_hi = plcp_length >> 6; 1537 desc->plcp_length_lo = plcp_length & 0x3f; 1538 } else { 1539 plcp_length = (16 * len + rate - 1) / rate; 1540 if (rate == 22) { 1541 remainder = (16 * len) % 22; 1542 if (remainder != 0 && remainder < 7) 1543 desc->plcp_service |= RT2560_PLCP_LENGEXT; 1544 } 1545 desc->plcp_length_hi = plcp_length >> 8; 1546 desc->plcp_length_lo = plcp_length & 0xff; 1547 1548 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1549 desc->plcp_signal |= 0x08; 1550 } 1551} 1552 1553static int 1554rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0, 1555 struct ieee80211_node *ni) 1556{ 1557 struct ieee80211com *ic = &sc->sc_ic; 1558 struct rt2560_tx_desc *desc; 1559 struct rt2560_tx_data *data; 1560 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1561 int nsegs, rate, error; 1562 1563 desc = &sc->bcnq.desc[sc->bcnq.cur]; 1564 data = &sc->bcnq.data[sc->bcnq.cur]; 1565 1566 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2; 1567 1568 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0, 1569 segs, &nsegs, BUS_DMA_NOWAIT); 1570 if (error != 0) { 1571 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1572 error); 1573 m_freem(m0); 1574 return error; 1575 } 1576 1577 if (bpf_peers_present(sc->sc_drvbpf)) { 1578 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1579 1580 tap->wt_flags = 0; 1581 tap->wt_rate = rate; 1582 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1583 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1584 tap->wt_antenna = sc->tx_ant; 1585 1586 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1587 } 1588 1589 data->m = m0; 1590 data->ni = ni; 1591 1592 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF | 1593 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr); 1594 1595 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n", 1596 m0->m_pkthdr.len, sc->bcnq.cur, rate)); 1597 1598 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1599 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map, 1600 BUS_DMASYNC_PREWRITE); 1601 1602 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT; 1603 1604 return 0; 1605} 1606 1607static int 1608rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0, 1609 struct ieee80211_node *ni) 1610{ 1611 struct ieee80211com *ic = &sc->sc_ic; 1612 struct rt2560_tx_desc *desc; 1613 struct rt2560_tx_data *data; 1614 struct ieee80211_frame *wh; 1615 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1616 uint16_t dur; 1617 uint32_t flags = 0; 1618 int nsegs, rate, error; 1619 1620 desc = &sc->prioq.desc[sc->prioq.cur]; 1621 data = &sc->prioq.data[sc->prioq.cur]; 1622 1623 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 1624 1625 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1626 segs, &nsegs, 0); 1627 if (error != 0) { 1628 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1629 error); 1630 m_freem(m0); 1631 return error; 1632 } 1633 1634 if (bpf_peers_present(sc->sc_drvbpf)) { 1635 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1636 1637 tap->wt_flags = 0; 1638 tap->wt_rate = rate; 1639 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1640 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1641 tap->wt_antenna = sc->tx_ant; 1642 1643 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1644 } 1645 1646 data->m = m0; 1647 data->ni = ni; 1648 1649 wh = mtod(m0, struct ieee80211_frame *); 1650 1651 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1652 flags |= RT2560_TX_ACK; 1653 1654 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + 1655 RAL_SIFS; 1656 *(uint16_t *)wh->i_dur = htole16(dur); 1657 1658 /* tell hardware to add timestamp for probe responses */ 1659 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1660 IEEE80211_FC0_TYPE_MGT && 1661 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1662 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1663 flags |= RT2560_TX_TIMESTAMP; 1664 } 1665 1666 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, 1667 segs->ds_addr); 1668 1669 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1670 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1671 BUS_DMASYNC_PREWRITE); 1672 1673 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n", 1674 m0->m_pkthdr.len, sc->prioq.cur, rate)); 1675 1676 /* kick prio */ 1677 sc->prioq.queued++; 1678 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1679 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1680 1681 return 0; 1682} 1683 1684static int 1685rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0, 1686 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 1687{ 1688 struct ieee80211com *ic = &sc->sc_ic; 1689 struct rt2560_tx_desc *desc; 1690 struct rt2560_tx_data *data; 1691 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1692 uint32_t flags; 1693 int nsegs, rate, error; 1694 1695 desc = &sc->prioq.desc[sc->prioq.cur]; 1696 data = &sc->prioq.data[sc->prioq.cur]; 1697 1698 rate = params->ibp_rate0 & IEEE80211_RATE_VAL; 1699 /* XXX validate */ 1700 if (rate == 0) { 1701 m_freem(m0); 1702 return EINVAL; 1703 } 1704 1705 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1706 segs, &nsegs, 0); 1707 if (error != 0) { 1708 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1709 error); 1710 m_freem(m0); 1711 return error; 1712 } 1713 1714 if (bpf_peers_present(sc->sc_drvbpf)) { 1715 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1716 1717 tap->wt_flags = 0; 1718 tap->wt_rate = rate; 1719 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1720 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1721 tap->wt_antenna = sc->tx_ant; 1722 1723 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1724 } 1725 1726 data->m = m0; 1727 data->ni = ni; 1728 1729 flags = 0; 1730 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1731 flags |= RT2560_TX_ACK; 1732 1733 /* XXX need to setup descriptor ourself */ 1734 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1735 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1736 segs->ds_addr); 1737 1738 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1739 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1740 BUS_DMASYNC_PREWRITE); 1741 1742 DPRINTFN(10, ("sending raw frame len=%u idx=%u rate=%u\n", 1743 m0->m_pkthdr.len, sc->prioq.cur, rate)); 1744 1745 /* kick prio */ 1746 sc->prioq.queued++; 1747 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1748 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1749 1750 return 0; 1751} 1752 1753/* 1754 * Build a RTS control frame. 1755 */ 1756static struct mbuf * 1757rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh, 1758 uint16_t dur) 1759{ 1760 struct ieee80211_frame_rts *rts; 1761 struct mbuf *m; 1762 1763 MGETHDR(m, M_DONTWAIT, MT_DATA); 1764 if (m == NULL) { 1765 sc->sc_ic.ic_stats.is_tx_nobuf++; 1766 device_printf(sc->sc_dev, "could not allocate RTS frame\n"); 1767 return NULL; 1768 } 1769 1770 rts = mtod(m, struct ieee80211_frame_rts *); 1771 1772 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL | 1773 IEEE80211_FC0_SUBTYPE_RTS; 1774 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS; 1775 *(uint16_t *)rts->i_dur = htole16(dur); 1776 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1); 1777 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2); 1778 1779 m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts); 1780 1781 return m; 1782} 1783 1784static int 1785rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1786 struct ieee80211_node *ni) 1787{ 1788 struct ieee80211com *ic = &sc->sc_ic; 1789 struct rt2560_tx_desc *desc; 1790 struct rt2560_tx_data *data; 1791 struct rt2560_node *rn; 1792 struct ieee80211_frame *wh; 1793 struct ieee80211_key *k; 1794 struct mbuf *mnew; 1795 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1796 uint16_t dur; 1797 uint32_t flags = 0; 1798 int nsegs, rate, error; 1799 1800 wh = mtod(m0, struct ieee80211_frame *); 1801 1802 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 1803 rate = ic->ic_fixed_rate; 1804 } else { 1805 struct ieee80211_rateset *rs; 1806 1807 rs = &ni->ni_rates; 1808 rn = (struct rt2560_node *)ni; 1809 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh, 1810 m0->m_pkthdr.len, NULL, 0); 1811 rate = rs->rs_rates[ni->ni_txrate]; 1812 } 1813 rate &= IEEE80211_RATE_VAL; 1814 1815 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1816 k = ieee80211_crypto_encap(ic, ni, m0); 1817 if (k == NULL) { 1818 m_freem(m0); 1819 return ENOBUFS; 1820 } 1821 1822 /* packet header may have moved, reset our local pointer */ 1823 wh = mtod(m0, struct ieee80211_frame *); 1824 } 1825 1826 /* 1827 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange 1828 * for directed frames only when the length of the MPDU is greater 1829 * than the length threshold indicated by [...]" ic_rtsthreshold. 1830 */ 1831 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 1832 m0->m_pkthdr.len > ic->ic_rtsthreshold) { 1833 struct mbuf *m; 1834 uint16_t dur; 1835 int rtsrate, ackrate; 1836 1837 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2; 1838 ackrate = rt2560_ack_rate(ic, rate); 1839 1840 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) + 1841 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) + 1842 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) + 1843 3 * RAL_SIFS; 1844 1845 m = rt2560_get_rts(sc, wh, dur); 1846 1847 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1848 data = &sc->txq.data[sc->txq.cur_encrypt]; 1849 1850 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1851 m, segs, &nsegs, 0); 1852 if (error != 0) { 1853 device_printf(sc->sc_dev, 1854 "could not map mbuf (error %d)\n", error); 1855 m_freem(m); 1856 m_freem(m0); 1857 return error; 1858 } 1859 1860 /* avoid multiple free() of the same node for each fragment */ 1861 ieee80211_ref_node(ni); 1862 1863 data->m = m; 1864 data->ni = ni; 1865 1866 /* RTS frames are not taken into account for rssadapt */ 1867 data->id.id_node = NULL; 1868 1869 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK | 1870 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, 1871 segs->ds_addr); 1872 1873 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1874 BUS_DMASYNC_PREWRITE); 1875 1876 sc->txq.queued++; 1877 sc->txq.cur_encrypt = 1878 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1879 1880 /* 1881 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the 1882 * asynchronous data frame shall be transmitted after the CTS 1883 * frame and a SIFS period. 1884 */ 1885 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1886 } 1887 1888 data = &sc->txq.data[sc->txq.cur_encrypt]; 1889 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1890 1891 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, 1892 segs, &nsegs, 0); 1893 if (error != 0 && error != EFBIG) { 1894 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1895 error); 1896 m_freem(m0); 1897 return error; 1898 } 1899 if (error != 0) { 1900 mnew = m_defrag(m0, M_DONTWAIT); 1901 if (mnew == NULL) { 1902 device_printf(sc->sc_dev, 1903 "could not defragment mbuf\n"); 1904 m_freem(m0); 1905 return ENOBUFS; 1906 } 1907 m0 = mnew; 1908 1909 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1910 m0, segs, &nsegs, 0); 1911 if (error != 0) { 1912 device_printf(sc->sc_dev, 1913 "could not map mbuf (error %d)\n", error); 1914 m_freem(m0); 1915 return error; 1916 } 1917 1918 /* packet header may have moved, reset our local pointer */ 1919 wh = mtod(m0, struct ieee80211_frame *); 1920 } 1921 1922 if (bpf_peers_present(sc->sc_drvbpf)) { 1923 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1924 1925 tap->wt_flags = 0; 1926 tap->wt_rate = rate; 1927 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1928 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1929 tap->wt_antenna = sc->tx_ant; 1930 1931 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1932 } 1933 1934 data->m = m0; 1935 data->ni = ni; 1936 1937 /* remember link conditions for rate adaptation algorithm */ 1938 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 1939 data->id.id_len = m0->m_pkthdr.len; 1940 data->id.id_rateidx = ni->ni_txrate; 1941 data->id.id_node = ni; 1942 data->id.id_rssi = ni->ni_rssi; 1943 } else 1944 data->id.id_node = NULL; 1945 1946 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1947 flags |= RT2560_TX_ACK; 1948 1949 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate), 1950 ic->ic_flags) + RAL_SIFS; 1951 *(uint16_t *)wh->i_dur = htole16(dur); 1952 } 1953 1954 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1955 segs->ds_addr); 1956 1957 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1958 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1959 BUS_DMASYNC_PREWRITE); 1960 1961 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n", 1962 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate)); 1963 1964 /* kick encrypt */ 1965 sc->txq.queued++; 1966 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1967 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1968 1969 return 0; 1970} 1971 1972static void 1973rt2560_start(struct ifnet *ifp) 1974{ 1975 struct rt2560_softc *sc = ifp->if_softc; 1976 struct ieee80211com *ic = &sc->sc_ic; 1977 struct mbuf *m0; 1978 struct ether_header *eh; 1979 struct ieee80211_node *ni; 1980 1981 RAL_LOCK(sc); 1982 1983 /* prevent management frames from being sent if we're not ready */ 1984 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1985 RAL_UNLOCK(sc); 1986 return; 1987 } 1988 1989 for (;;) { 1990 IF_POLL(&ic->ic_mgtq, m0); 1991 if (m0 != NULL) { 1992 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 1993 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1994 break; 1995 } 1996 IF_DEQUEUE(&ic->ic_mgtq, m0); 1997 1998 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 1999 m0->m_pkthdr.rcvif = NULL; 2000 2001 if (bpf_peers_present(ic->ic_rawbpf)) 2002 bpf_mtap(ic->ic_rawbpf, m0); 2003 2004 if (rt2560_tx_mgt(sc, m0, ni) != 0) { 2005 ieee80211_free_node(ni); 2006 break; 2007 } 2008 } else { 2009 if (ic->ic_state != IEEE80211_S_RUN) 2010 break; 2011 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2012 if (m0 == NULL) 2013 break; 2014 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) { 2015 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2016 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2017 break; 2018 } 2019 /* 2020 * Cancel any background scan. 2021 */ 2022 if (ic->ic_flags & IEEE80211_F_SCAN) 2023 ieee80211_cancel_scan(ic); 2024 2025 if (m0->m_len < sizeof (struct ether_header) && 2026 !(m0 = m_pullup(m0, sizeof (struct ether_header)))) 2027 continue; 2028 2029 eh = mtod(m0, struct ether_header *); 2030 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2031 if (ni == NULL) { 2032 m_freem(m0); 2033 continue; 2034 } 2035 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 2036 (m0->m_flags & M_PWR_SAV) == 0) { 2037 /* 2038 * Station in power save mode; pass the frame 2039 * to the 802.11 layer and continue. We'll get 2040 * the frame back when the time is right. 2041 */ 2042 ieee80211_pwrsave(ni, m0); 2043 /* 2044 * If we're in power save mode 'cuz of a bg 2045 * scan cancel it so the traffic can flow. 2046 * The packet we just queued will automatically 2047 * get sent when we drop out of power save. 2048 * XXX locking 2049 */ 2050 if (ic->ic_flags & IEEE80211_F_SCAN) 2051 ieee80211_cancel_scan(ic); 2052 ieee80211_free_node(ni); 2053 continue; 2054 2055 } 2056 2057 BPF_MTAP(ifp, m0); 2058 2059 m0 = ieee80211_encap(ic, m0, ni); 2060 if (m0 == NULL) { 2061 ieee80211_free_node(ni); 2062 continue; 2063 } 2064 2065 if (bpf_peers_present(ic->ic_rawbpf)) 2066 bpf_mtap(ic->ic_rawbpf, m0); 2067 2068 if (rt2560_tx_data(sc, m0, ni) != 0) { 2069 ieee80211_free_node(ni); 2070 ifp->if_oerrors++; 2071 break; 2072 } 2073 } 2074 2075 sc->sc_tx_timer = 5; 2076 ic->ic_lastdata = ticks; 2077 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2078 } 2079 2080 RAL_UNLOCK(sc); 2081} 2082 2083static void 2084rt2560_watchdog(void *arg) 2085{ 2086 struct rt2560_softc *sc = arg; 2087 2088 if (sc->sc_tx_timer > 0) { 2089 if (--sc->sc_tx_timer == 0) { 2090 device_printf(sc->sc_dev, "device timeout\n"); 2091 rt2560_init(sc); 2092 sc->sc_ifp->if_oerrors++; 2093 return; 2094 } 2095 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2096 } 2097} 2098 2099/* 2100 * This function allows for fast channel switching in monitor mode (used by 2101 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to 2102 * generate a new beacon frame. 2103 */ 2104static int 2105rt2560_reset(struct ifnet *ifp) 2106{ 2107 struct rt2560_softc *sc = ifp->if_softc; 2108 struct ieee80211com *ic = &sc->sc_ic; 2109 2110 if (ic->ic_opmode != IEEE80211_M_MONITOR) 2111 return ENETRESET; 2112 2113 rt2560_set_chan(sc, ic->ic_curchan); 2114 2115 return 0; 2116} 2117 2118static int 2119rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2120{ 2121 struct rt2560_softc *sc = ifp->if_softc; 2122 struct ieee80211com *ic = &sc->sc_ic; 2123 int error = 0; 2124 2125 2126 2127 switch (cmd) { 2128 case SIOCSIFFLAGS: 2129 if (ifp->if_flags & IFF_UP) { 2130 RAL_LOCK(sc); 2131 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2132 rt2560_update_promisc(sc); 2133 else 2134 rt2560_init(sc); 2135 RAL_UNLOCK(sc); 2136 } else { 2137 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2138 rt2560_stop(sc); 2139 } 2140 2141 break; 2142 2143 default: 2144 error = ieee80211_ioctl(ic, cmd, data); 2145 } 2146 2147 if (error == ENETRESET) { 2148 if ((ifp->if_flags & IFF_UP) && 2149 (ifp->if_drv_flags & IFF_DRV_RUNNING) && 2150 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 2151 rt2560_init(sc); 2152 error = 0; 2153 } 2154 2155 2156 return error; 2157} 2158 2159static void 2160rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 2161{ 2162 uint32_t tmp; 2163 int ntries; 2164 2165 for (ntries = 0; ntries < 100; ntries++) { 2166 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2167 break; 2168 DELAY(1); 2169 } 2170 if (ntries == 100) { 2171 device_printf(sc->sc_dev, "could not write to BBP\n"); 2172 return; 2173 } 2174 2175 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 2176 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 2177 2178 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val)); 2179} 2180 2181static uint8_t 2182rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 2183{ 2184 uint32_t val; 2185 int ntries; 2186 2187 val = RT2560_BBP_BUSY | reg << 8; 2188 RAL_WRITE(sc, RT2560_BBPCSR, val); 2189 2190 for (ntries = 0; ntries < 100; ntries++) { 2191 val = RAL_READ(sc, RT2560_BBPCSR); 2192 if (!(val & RT2560_BBP_BUSY)) 2193 return val & 0xff; 2194 DELAY(1); 2195 } 2196 2197 device_printf(sc->sc_dev, "could not read from BBP\n"); 2198 return 0; 2199} 2200 2201static void 2202rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2203{ 2204 uint32_t tmp; 2205 int ntries; 2206 2207 for (ntries = 0; ntries < 100; ntries++) { 2208 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2209 break; 2210 DELAY(1); 2211 } 2212 if (ntries == 100) { 2213 device_printf(sc->sc_dev, "could not write to RF\n"); 2214 return; 2215 } 2216 2217 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2218 (reg & 0x3); 2219 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2220 2221 /* remember last written value in sc */ 2222 sc->rf_regs[reg] = val; 2223 2224 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff)); 2225} 2226 2227static void 2228rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2229{ 2230 struct ieee80211com *ic = &sc->sc_ic; 2231 uint8_t power, tmp; 2232 u_int i, chan; 2233 2234 chan = ieee80211_chan2ieee(ic, c); 2235 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 2236 return; 2237 2238 if (IEEE80211_IS_CHAN_2GHZ(c)) 2239 power = min(sc->txpow[chan - 1], 31); 2240 else 2241 power = 31; 2242 2243 /* adjust txpower using ifconfig settings */ 2244 power -= (100 - ic->ic_txpowlimit) / 8; 2245 2246 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power)); 2247 2248 switch (sc->rf_rev) { 2249 case RT2560_RF_2522: 2250 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2251 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2252 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2253 break; 2254 2255 case RT2560_RF_2523: 2256 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2257 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2258 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2259 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2260 break; 2261 2262 case RT2560_RF_2524: 2263 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2264 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2265 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2266 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2267 break; 2268 2269 case RT2560_RF_2525: 2270 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2271 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2272 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2273 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2274 2275 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2276 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2277 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2278 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2279 break; 2280 2281 case RT2560_RF_2525E: 2282 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2283 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2284 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2285 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2286 break; 2287 2288 case RT2560_RF_2526: 2289 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2290 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2291 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2292 2293 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2294 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2295 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2296 break; 2297 2298 /* dual-band RF */ 2299 case RT2560_RF_5222: 2300 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2301 2302 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2303 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2304 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2305 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2306 break; 2307 default: 2308 printf("unknown ral rev=%d\n", sc->rf_rev); 2309 } 2310 2311 if (ic->ic_state != IEEE80211_S_SCAN) { 2312 /* set Japan filter bit for channel 14 */ 2313 tmp = rt2560_bbp_read(sc, 70); 2314 2315 tmp &= ~RT2560_JAPAN_FILTER; 2316 if (chan == 14) 2317 tmp |= RT2560_JAPAN_FILTER; 2318 2319 rt2560_bbp_write(sc, 70, tmp); 2320 2321 /* clear CRC errors */ 2322 RAL_READ(sc, RT2560_CNT0); 2323 } 2324} 2325 2326static void 2327rt2560_set_channel(struct ieee80211com *ic) 2328{ 2329 struct ifnet *ifp = ic->ic_ifp; 2330 struct rt2560_softc *sc = ifp->if_softc; 2331 2332 RAL_LOCK(sc); 2333 rt2560_set_chan(sc, ic->ic_curchan); 2334 RAL_UNLOCK(sc); 2335 2336} 2337 2338#if 0 2339/* 2340 * Disable RF auto-tuning. 2341 */ 2342static void 2343rt2560_disable_rf_tune(struct rt2560_softc *sc) 2344{ 2345 uint32_t tmp; 2346 2347 if (sc->rf_rev != RT2560_RF_2523) { 2348 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2349 rt2560_rf_write(sc, RAL_RF1, tmp); 2350 } 2351 2352 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2353 rt2560_rf_write(sc, RAL_RF3, tmp); 2354 2355 DPRINTFN(2, ("disabling RF autotune\n")); 2356} 2357#endif 2358 2359/* 2360 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2361 * synchronization. 2362 */ 2363static void 2364rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2365{ 2366 struct ieee80211com *ic = &sc->sc_ic; 2367 uint16_t logcwmin, preload; 2368 uint32_t tmp; 2369 2370 /* first, disable TSF synchronization */ 2371 RAL_WRITE(sc, RT2560_CSR14, 0); 2372 2373 tmp = 16 * ic->ic_bss->ni_intval; 2374 RAL_WRITE(sc, RT2560_CSR12, tmp); 2375 2376 RAL_WRITE(sc, RT2560_CSR13, 0); 2377 2378 logcwmin = 5; 2379 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024; 2380 tmp = logcwmin << 16 | preload; 2381 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2382 2383 /* finally, enable TSF synchronization */ 2384 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2385 if (ic->ic_opmode == IEEE80211_M_STA) 2386 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2387 else 2388 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2389 RT2560_ENABLE_BEACON_GENERATOR; 2390 RAL_WRITE(sc, RT2560_CSR14, tmp); 2391 2392 DPRINTF(("enabling TSF synchronization\n")); 2393} 2394 2395static void 2396rt2560_update_plcp(struct rt2560_softc *sc) 2397{ 2398 struct ieee80211com *ic = &sc->sc_ic; 2399 2400 /* no short preamble for 1Mbps */ 2401 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2402 2403 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2404 /* values taken from the reference driver */ 2405 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2406 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2407 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2408 } else { 2409 /* same values as above or'ed 0x8 */ 2410 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2411 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2412 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2413 } 2414 2415 DPRINTF(("updating PLCP for %s preamble\n", 2416 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long")); 2417} 2418 2419/* 2420 * This function can be called by ieee80211_set_shortslottime(). Refer to 2421 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2422 */ 2423static void 2424rt2560_update_slot(struct ifnet *ifp) 2425{ 2426 struct rt2560_softc *sc = ifp->if_softc; 2427 struct ieee80211com *ic = &sc->sc_ic; 2428 uint8_t slottime; 2429 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2430 uint32_t tmp; 2431 2432 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 2433 2434 /* update the MAC slot boundaries */ 2435 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2436 tx_pifs = tx_sifs + slottime; 2437 tx_difs = tx_sifs + 2 * slottime; 2438 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2439 2440 tmp = RAL_READ(sc, RT2560_CSR11); 2441 tmp = (tmp & ~0x1f00) | slottime << 8; 2442 RAL_WRITE(sc, RT2560_CSR11, tmp); 2443 2444 tmp = tx_pifs << 16 | tx_sifs; 2445 RAL_WRITE(sc, RT2560_CSR18, tmp); 2446 2447 tmp = eifs << 16 | tx_difs; 2448 RAL_WRITE(sc, RT2560_CSR19, tmp); 2449 2450 DPRINTF(("setting slottime to %uus\n", slottime)); 2451} 2452 2453static void 2454rt2560_set_basicrates(struct rt2560_softc *sc) 2455{ 2456 struct ieee80211com *ic = &sc->sc_ic; 2457 2458 /* update basic rate set */ 2459 if (ic->ic_curmode == IEEE80211_MODE_11B) { 2460 /* 11b basic rates: 1, 2Mbps */ 2461 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3); 2462 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) { 2463 /* 11a basic rates: 6, 12, 24Mbps */ 2464 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150); 2465 } else { 2466 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 2467 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f); 2468 } 2469} 2470 2471static void 2472rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2473{ 2474 uint32_t tmp; 2475 2476 /* set ON period to 70ms and OFF period to 30ms */ 2477 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2478 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2479} 2480 2481static void 2482rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2483{ 2484 uint32_t tmp; 2485 2486 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2487 RAL_WRITE(sc, RT2560_CSR5, tmp); 2488 2489 tmp = bssid[4] | bssid[5] << 8; 2490 RAL_WRITE(sc, RT2560_CSR6, tmp); 2491 2492 DPRINTF(("setting BSSID to %6D\n", bssid, ":")); 2493} 2494 2495static void 2496rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2497{ 2498 uint32_t tmp; 2499 2500 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2501 RAL_WRITE(sc, RT2560_CSR3, tmp); 2502 2503 tmp = addr[4] | addr[5] << 8; 2504 RAL_WRITE(sc, RT2560_CSR4, tmp); 2505 2506 DPRINTF(("setting MAC address to %6D\n", addr, ":")); 2507} 2508 2509static void 2510rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2511{ 2512 uint32_t tmp; 2513 2514 tmp = RAL_READ(sc, RT2560_CSR3); 2515 addr[0] = tmp & 0xff; 2516 addr[1] = (tmp >> 8) & 0xff; 2517 addr[2] = (tmp >> 16) & 0xff; 2518 addr[3] = (tmp >> 24); 2519 2520 tmp = RAL_READ(sc, RT2560_CSR4); 2521 addr[4] = tmp & 0xff; 2522 addr[5] = (tmp >> 8) & 0xff; 2523} 2524 2525static void 2526rt2560_update_promisc(struct rt2560_softc *sc) 2527{ 2528 struct ifnet *ifp = sc->sc_ic.ic_ifp; 2529 uint32_t tmp; 2530 2531 tmp = RAL_READ(sc, RT2560_RXCSR0); 2532 2533 tmp &= ~RT2560_DROP_NOT_TO_ME; 2534 if (!(ifp->if_flags & IFF_PROMISC)) 2535 tmp |= RT2560_DROP_NOT_TO_ME; 2536 2537 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2538 2539 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 2540 "entering" : "leaving")); 2541} 2542 2543static const char * 2544rt2560_get_rf(int rev) 2545{ 2546 switch (rev) { 2547 case RT2560_RF_2522: return "RT2522"; 2548 case RT2560_RF_2523: return "RT2523"; 2549 case RT2560_RF_2524: return "RT2524"; 2550 case RT2560_RF_2525: return "RT2525"; 2551 case RT2560_RF_2525E: return "RT2525e"; 2552 case RT2560_RF_2526: return "RT2526"; 2553 case RT2560_RF_5222: return "RT5222"; 2554 default: return "unknown"; 2555 } 2556} 2557 2558static void 2559rt2560_read_eeprom(struct rt2560_softc *sc) 2560{ 2561 uint16_t val; 2562 int i; 2563 2564 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2565 sc->rf_rev = (val >> 11) & 0x7; 2566 sc->hw_radio = (val >> 10) & 0x1; 2567 sc->led_mode = (val >> 6) & 0x7; 2568 sc->rx_ant = (val >> 4) & 0x3; 2569 sc->tx_ant = (val >> 2) & 0x3; 2570 sc->nb_ant = val & 0x3; 2571 2572 /* read default values for BBP registers */ 2573 for (i = 0; i < 16; i++) { 2574 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2575 sc->bbp_prom[i].reg = val >> 8; 2576 sc->bbp_prom[i].val = val & 0xff; 2577 } 2578 2579 /* read Tx power for all b/g channels */ 2580 for (i = 0; i < 14 / 2; i++) { 2581 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2582 sc->txpow[i * 2] = val >> 8; 2583 sc->txpow[i * 2 + 1] = val & 0xff; 2584 } 2585 2586 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2587 if ((val & 0xff) == 0xff) 2588 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2589 else 2590 sc->rssi_corr = val & 0xff; 2591 DPRINTF(("rssi correction %d, calibrate 0x%02x\n", 2592 sc->rssi_corr, val)); 2593} 2594 2595 2596static void 2597rt2560_scan_start(struct ieee80211com *ic) 2598{ 2599 struct ifnet *ifp = ic->ic_ifp; 2600 struct rt2560_softc *sc = ifp->if_softc; 2601 2602 /* abort TSF synchronization */ 2603 RAL_WRITE(sc, RT2560_CSR14, 0); 2604 rt2560_set_bssid(sc, ifp->if_broadcastaddr); 2605} 2606 2607static void 2608rt2560_scan_end(struct ieee80211com *ic) 2609{ 2610 struct ifnet *ifp = ic->ic_ifp; 2611 struct rt2560_softc *sc = ifp->if_softc; 2612 2613 rt2560_enable_tsf_sync(sc); 2614 /* XXX keep local copy */ 2615 rt2560_set_bssid(sc, ic->ic_bss->ni_bssid); 2616} 2617 2618static int 2619rt2560_bbp_init(struct rt2560_softc *sc) 2620{ 2621#define N(a) (sizeof (a) / sizeof ((a)[0])) 2622 int i, ntries; 2623 2624 /* wait for BBP to be ready */ 2625 for (ntries = 0; ntries < 100; ntries++) { 2626 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2627 break; 2628 DELAY(1); 2629 } 2630 if (ntries == 100) { 2631 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2632 return EIO; 2633 } 2634 2635 /* initialize BBP registers to default values */ 2636 for (i = 0; i < N(rt2560_def_bbp); i++) { 2637 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2638 rt2560_def_bbp[i].val); 2639 } 2640#if 0 2641 /* initialize BBP registers to values stored in EEPROM */ 2642 for (i = 0; i < 16; i++) { 2643 if (sc->bbp_prom[i].reg == 0xff) 2644 continue; 2645 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2646 } 2647#endif 2648 2649 return 0; 2650#undef N 2651} 2652 2653static void 2654rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2655{ 2656 uint32_t tmp; 2657 uint8_t tx; 2658 2659 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2660 if (antenna == 1) 2661 tx |= RT2560_BBP_ANTA; 2662 else if (antenna == 2) 2663 tx |= RT2560_BBP_ANTB; 2664 else 2665 tx |= RT2560_BBP_DIVERSITY; 2666 2667 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2668 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2669 sc->rf_rev == RT2560_RF_5222) 2670 tx |= RT2560_BBP_FLIPIQ; 2671 2672 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2673 2674 /* update values for CCK and OFDM in BBPCSR1 */ 2675 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2676 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2677 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2678} 2679 2680static void 2681rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2682{ 2683 uint8_t rx; 2684 2685 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2686 if (antenna == 1) 2687 rx |= RT2560_BBP_ANTA; 2688 else if (antenna == 2) 2689 rx |= RT2560_BBP_ANTB; 2690 else 2691 rx |= RT2560_BBP_DIVERSITY; 2692 2693 /* need to force no I/Q flip for RF 2525e and 2526 */ 2694 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2695 rx &= ~RT2560_BBP_FLIPIQ; 2696 2697 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2698} 2699 2700static void 2701rt2560_init(void *priv) 2702{ 2703#define N(a) (sizeof (a) / sizeof ((a)[0])) 2704 struct rt2560_softc *sc = priv; 2705 struct ieee80211com *ic = &sc->sc_ic; 2706 struct ifnet *ifp = ic->ic_ifp; 2707 uint32_t tmp; 2708 int i; 2709 2710 2711 2712 rt2560_stop(sc); 2713 2714 RAL_LOCK(sc); 2715 /* setup tx rings */ 2716 tmp = RT2560_PRIO_RING_COUNT << 24 | 2717 RT2560_ATIM_RING_COUNT << 16 | 2718 RT2560_TX_RING_COUNT << 8 | 2719 RT2560_TX_DESC_SIZE; 2720 2721 /* rings must be initialized in this exact order */ 2722 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2723 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2724 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2725 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2726 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2727 2728 /* setup rx ring */ 2729 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2730 2731 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2732 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2733 2734 /* initialize MAC registers to default values */ 2735 for (i = 0; i < N(rt2560_def_mac); i++) 2736 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2737 2738 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2739 rt2560_set_macaddr(sc, ic->ic_myaddr); 2740 2741 /* set basic rate set (will be updated later) */ 2742 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2743 2744 rt2560_set_txantenna(sc, sc->tx_ant); 2745 rt2560_set_rxantenna(sc, sc->rx_ant); 2746 rt2560_update_slot(ifp); 2747 rt2560_update_plcp(sc); 2748 rt2560_update_led(sc, 0, 0); 2749 2750 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2751 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2752 2753 if (rt2560_bbp_init(sc) != 0) { 2754 rt2560_stop(sc); 2755 RAL_UNLOCK(sc); 2756 return; 2757 } 2758 2759 /* set default BSS channel */ 2760 rt2560_set_chan(sc, ic->ic_curchan); 2761 2762 /* kick Rx */ 2763 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2764 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2765 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2766 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2767 tmp |= RT2560_DROP_TODS; 2768 if (!(ifp->if_flags & IFF_PROMISC)) 2769 tmp |= RT2560_DROP_NOT_TO_ME; 2770 } 2771 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2772 2773 /* clear old FCS and Rx FIFO errors */ 2774 RAL_READ(sc, RT2560_CNT0); 2775 RAL_READ(sc, RT2560_CNT4); 2776 2777 /* clear any pending interrupts */ 2778 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2779 2780 /* enable interrupts */ 2781 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2782 2783 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2784 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2785 2786 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2787 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2788 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2789 } else 2790 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2791 2792 RAL_UNLOCK(sc); 2793#undef N 2794} 2795 2796void 2797rt2560_stop(void *arg) 2798{ 2799 struct rt2560_softc *sc = arg; 2800 struct ieee80211com *ic = &sc->sc_ic; 2801 struct ifnet *ifp = ic->ic_ifp; 2802 volatile int *flags = &sc->sc_flags; 2803 2804 while (*flags & RAL_INPUT_RUNNING) { 2805 tsleep(sc, 0, "ralrunning", hz/10); 2806 } 2807 2808 RAL_LOCK(sc); 2809 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2810 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2811 sc->sc_tx_timer = 0; 2812 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2813 2814 /* abort Tx */ 2815 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2816 2817 /* disable Rx */ 2818 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2819 2820 /* reset ASIC (imply reset BBP) */ 2821 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2822 RAL_WRITE(sc, RT2560_CSR1, 0); 2823 2824 /* disable interrupts */ 2825 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2826 2827 /* reset Tx and Rx rings */ 2828 rt2560_reset_tx_ring(sc, &sc->txq); 2829 rt2560_reset_tx_ring(sc, &sc->atimq); 2830 rt2560_reset_tx_ring(sc, &sc->prioq); 2831 rt2560_reset_tx_ring(sc, &sc->bcnq); 2832 rt2560_reset_rx_ring(sc, &sc->rxq); 2833 } 2834 RAL_UNLOCK(sc); 2835} 2836 2837static int 2838rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2839 const struct ieee80211_bpf_params *params) 2840{ 2841 struct ieee80211com *ic = ni->ni_ic; 2842 struct ifnet *ifp = ic->ic_ifp; 2843 struct rt2560_softc *sc = ifp->if_softc; 2844 2845 RAL_LOCK(sc); 2846 2847 /* prevent management frames from being sent if we're not ready */ 2848 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2849 RAL_UNLOCK(sc); 2850 m_freem(m); 2851 ieee80211_free_node(ni); 2852 return ENETDOWN; 2853 } 2854 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2855 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2856 RAL_UNLOCK(sc); 2857 m_freem(m); 2858 ieee80211_free_node(ni); 2859 return ENOBUFS; /* XXX */ 2860 } 2861 2862 if (bpf_peers_present(ic->ic_rawbpf)) 2863 bpf_mtap(ic->ic_rawbpf, m); 2864 2865 ifp->if_opackets++; 2866 2867 if (params == NULL) { 2868 /* 2869 * Legacy path; interpret frame contents to decide 2870 * precisely how to send the frame. 2871 */ 2872 if (rt2560_tx_mgt(sc, m, ni) != 0) 2873 goto bad; 2874 } else { 2875 /* 2876 * Caller supplied explicit parameters to use in 2877 * sending the frame. 2878 */ 2879 if (rt2560_tx_raw(sc, m, ni, params)) 2880 goto bad; 2881 } 2882 sc->sc_tx_timer = 5; 2883 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2884 2885 RAL_UNLOCK(sc); 2886 2887 return 0; 2888bad: 2889 ifp->if_oerrors++; 2890 ieee80211_free_node(ni); 2891 RAL_UNLOCK(sc); 2892 return EIO; /* XXX */ 2893} 2894
|