LCOV - code coverage report
Current view: top level - dev/ic - rt2560.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 1325 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 59 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: rt2560.c,v 1.84 2017/10/26 15:00:28 mpi Exp $  */
       2             : 
       3             : /*-
       4             :  * Copyright (c) 2005, 2006
       5             :  *      Damien Bergamini <damien.bergamini@free.fr>
       6             :  *
       7             :  * Permission to use, copy, modify, and distribute this software for any
       8             :  * purpose with or without fee is hereby granted, provided that the above
       9             :  * copyright notice and this permission notice appear in all copies.
      10             :  *
      11             :  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      12             :  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      13             :  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
      14             :  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      15             :  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      16             :  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      17             :  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      18             :  */
      19             : 
      20             : /*-
      21             :  * Ralink Technology RT2560 chipset driver
      22             :  * http://www.ralinktech.com/
      23             :  */
      24             : 
      25             : #include "bpfilter.h"
      26             : 
      27             : #include <sys/param.h>
      28             : #include <sys/sockio.h>
      29             : #include <sys/mbuf.h>
      30             : #include <sys/kernel.h>
      31             : #include <sys/socket.h>
      32             : #include <sys/systm.h>
      33             : #include <sys/malloc.h>
      34             : #include <sys/timeout.h>
      35             : #include <sys/conf.h>
      36             : #include <sys/device.h>
      37             : #include <sys/endian.h>
      38             : 
      39             : #include <machine/bus.h>
      40             : #include <machine/intr.h>
      41             : 
      42             : #if NBPFILTER > 0
      43             : #include <net/bpf.h>
      44             : #endif
      45             : #include <net/if.h>
      46             : #include <net/if_dl.h>
      47             : #include <net/if_media.h>
      48             : 
      49             : #include <netinet/in.h>
      50             : #include <netinet/if_ether.h>
      51             : 
      52             : #include <net80211/ieee80211_var.h>
      53             : #include <net80211/ieee80211_amrr.h>
      54             : #include <net80211/ieee80211_radiotap.h>
      55             : 
      56             : #include <dev/ic/rt2560reg.h>
      57             : #include <dev/ic/rt2560var.h>
      58             : 
      59             : #ifdef RAL_DEBUG
      60             : #define DPRINTF(x)      do { if (rt2560_debug > 0) printf x; } while (0)
      61             : #define DPRINTFN(n, x)  do { if (rt2560_debug >= (n)) printf x; } while (0)
      62             : int rt2560_debug = 1;
      63             : #else
      64             : #define DPRINTF(x)
      65             : #define DPRINTFN(n, x)
      66             : #endif
      67             : 
      68             : int             rt2560_alloc_tx_ring(struct rt2560_softc *,
      69             :                     struct rt2560_tx_ring *, int);
      70             : void            rt2560_reset_tx_ring(struct rt2560_softc *,
      71             :                     struct rt2560_tx_ring *);
      72             : void            rt2560_free_tx_ring(struct rt2560_softc *,
      73             :                     struct rt2560_tx_ring *);
      74             : int             rt2560_alloc_rx_ring(struct rt2560_softc *,
      75             :                     struct rt2560_rx_ring *, int);
      76             : void            rt2560_reset_rx_ring(struct rt2560_softc *,
      77             :                     struct rt2560_rx_ring *);
      78             : void            rt2560_free_rx_ring(struct rt2560_softc *,
      79             :                     struct rt2560_rx_ring *);
      80             : struct          ieee80211_node *rt2560_node_alloc(struct ieee80211com *);
      81             : int             rt2560_media_change(struct ifnet *);
      82             : void            rt2560_next_scan(void *);
      83             : void            rt2560_iter_func(void *, struct ieee80211_node *);
      84             : void            rt2560_amrr_timeout(void *);
      85             : void            rt2560_newassoc(struct ieee80211com *, struct ieee80211_node *,
      86             :                     int);
      87             : int             rt2560_newstate(struct ieee80211com *, enum ieee80211_state,
      88             :                     int);
      89             : uint16_t        rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
      90             : void            rt2560_encryption_intr(struct rt2560_softc *);
      91             : void            rt2560_tx_intr(struct rt2560_softc *);
      92             : void            rt2560_prio_intr(struct rt2560_softc *);
      93             : void            rt2560_decryption_intr(struct rt2560_softc *);
      94             : void            rt2560_rx_intr(struct rt2560_softc *);
      95             : #ifndef IEEE80211_STA_ONLY
      96             : void            rt2560_beacon_expire(struct rt2560_softc *);
      97             : #endif
      98             : void            rt2560_wakeup_expire(struct rt2560_softc *);
      99             : #if NBPFILTER > 0
     100             : uint8_t         rt2560_rxrate(const struct rt2560_rx_desc *);
     101             : #endif
     102             : int             rt2560_ack_rate(struct ieee80211com *, int);
     103             : uint16_t        rt2560_txtime(int, int, uint32_t);
     104             : uint8_t         rt2560_plcp_signal(int);
     105             : void            rt2560_setup_tx_desc(struct rt2560_softc *,
     106             :                     struct rt2560_tx_desc *, uint32_t, int, int, int,
     107             :                     bus_addr_t);
     108             : #ifndef IEEE80211_STA_ONLY
     109             : int             rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
     110             :                     struct ieee80211_node *);
     111             : #endif
     112             : int             rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
     113             :                     struct ieee80211_node *);
     114             : int             rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
     115             :                     struct ieee80211_node *);
     116             : void            rt2560_start(struct ifnet *);
     117             : void            rt2560_watchdog(struct ifnet *);
     118             : int             rt2560_ioctl(struct ifnet *, u_long, caddr_t);
     119             : void            rt2560_bbp_write(struct rt2560_softc *, uint8_t, uint8_t);
     120             : uint8_t         rt2560_bbp_read(struct rt2560_softc *, uint8_t);
     121             : void            rt2560_rf_write(struct rt2560_softc *, uint8_t, uint32_t);
     122             : void            rt2560_set_chan(struct rt2560_softc *,
     123             :                     struct ieee80211_channel *);
     124             : void            rt2560_disable_rf_tune(struct rt2560_softc *);
     125             : void            rt2560_enable_tsf_sync(struct rt2560_softc *);
     126             : void            rt2560_update_plcp(struct rt2560_softc *);
     127             : void            rt2560_updateslot(struct ieee80211com *);
     128             : void            rt2560_set_slottime(struct rt2560_softc *);
     129             : void            rt2560_set_basicrates(struct rt2560_softc *);
     130             : void            rt2560_update_led(struct rt2560_softc *, int, int);
     131             : void            rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
     132             : void            rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
     133             : void            rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
     134             : void            rt2560_update_promisc(struct rt2560_softc *);
     135             : void            rt2560_set_txantenna(struct rt2560_softc *, int);
     136             : void            rt2560_set_rxantenna(struct rt2560_softc *, int);
     137             : const char      *rt2560_get_rf(int);
     138             : void            rt2560_read_eeprom(struct rt2560_softc *);
     139             : int             rt2560_bbp_init(struct rt2560_softc *);
     140             : int             rt2560_init(struct ifnet *);
     141             : void            rt2560_stop(struct ifnet *, int);
     142             : 
     143             : static const struct {
     144             :         uint32_t        reg;
     145             :         uint32_t        val;
     146             : } rt2560_def_mac[] = {
     147             :         RT2560_DEF_MAC
     148             : };
     149             : 
     150             : static const struct {
     151             :         uint8_t reg;
     152             :         uint8_t val;
     153             : } rt2560_def_bbp[] = {
     154             :         RT2560_DEF_BBP
     155             : };
     156             : 
     157             : static const uint32_t rt2560_rf2522_r2[]    = RT2560_RF2522_R2;
     158             : static const uint32_t rt2560_rf2523_r2[]    = RT2560_RF2523_R2;
     159             : static const uint32_t rt2560_rf2524_r2[]    = RT2560_RF2524_R2;
     160             : static const uint32_t rt2560_rf2525_r2[]    = RT2560_RF2525_R2;
     161             : static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
     162             : static const uint32_t rt2560_rf2525e_r2[]   = RT2560_RF2525E_R2;
     163             : static const uint32_t rt2560_rf2526_r2[]    = RT2560_RF2526_R2;
     164             : static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
     165             : 
     166             : int
     167           0 : rt2560_attach(void *xsc, int id)
     168             : {
     169           0 :         struct rt2560_softc *sc = xsc;
     170           0 :         struct ieee80211com *ic = &sc->sc_ic;
     171           0 :         struct ifnet *ifp = &ic->ic_if;
     172             :         int error, i;
     173             : 
     174           0 :         sc->amrr.amrr_min_success_threshold =  1;
     175           0 :         sc->amrr.amrr_max_success_threshold = 15;
     176           0 :         timeout_set(&sc->amrr_to, rt2560_amrr_timeout, sc);
     177           0 :         timeout_set(&sc->scan_to, rt2560_next_scan, sc);
     178             : 
     179             :         /* retrieve RT2560 rev. no */
     180           0 :         sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
     181             : 
     182             :         /* retrieve MAC address */
     183           0 :         rt2560_get_macaddr(sc, ic->ic_myaddr);
     184           0 :         printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
     185             : 
     186             :         /* retrieve RF rev. no and various other things from EEPROM */
     187           0 :         rt2560_read_eeprom(sc);
     188             : 
     189           0 :         printf("%s: MAC/BBP RT2560 (rev 0x%02x), RF %s\n", sc->sc_dev.dv_xname,
     190           0 :             sc->asic_rev, rt2560_get_rf(sc->rf_rev));
     191             : 
     192             :         /*
     193             :          * Allocate Tx and Rx rings.
     194             :          */
     195           0 :         error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
     196           0 :         if (error != 0) {
     197           0 :                 printf("%s: could not allocate Tx ring\n",
     198             :                     sc->sc_dev.dv_xname);
     199           0 :                 goto fail1;
     200             :         }
     201           0 :         error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
     202           0 :         if (error != 0) {
     203           0 :                 printf("%s: could not allocate ATIM ring\n",
     204             :                     sc->sc_dev.dv_xname);
     205           0 :                 goto fail2;
     206             :         }
     207           0 :         error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
     208           0 :         if (error != 0) {
     209           0 :                 printf("%s: could not allocate Prio ring\n",
     210             :                     sc->sc_dev.dv_xname);
     211           0 :                 goto fail3;
     212             :         }
     213           0 :         error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
     214           0 :         if (error != 0) {
     215           0 :                 printf("%s: could not allocate Beacon ring\n",
     216             :                     sc->sc_dev.dv_xname);
     217           0 :                 goto fail4;
     218             :         }
     219           0 :         error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
     220           0 :         if (error != 0) {
     221           0 :                 printf("%s: could not allocate Rx ring\n",
     222             :                     sc->sc_dev.dv_xname);
     223             :                 goto fail5;
     224             :         }
     225             : 
     226           0 :         ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
     227           0 :         ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
     228           0 :         ic->ic_state = IEEE80211_S_INIT;
     229             : 
     230             :         /* set device capabilities */
     231           0 :         ic->ic_caps =
     232             :             IEEE80211_C_MONITOR |       /* monitor mode supported */
     233             : #ifndef IEEE80211_STA_ONLY
     234             :             IEEE80211_C_IBSS |          /* IBSS mode supported */
     235             :             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
     236             : #endif
     237             :             IEEE80211_C_TXPMGT |        /* tx power management */
     238             :             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
     239             :             IEEE80211_C_SHSLOT |        /* short slot time supported */
     240             :             IEEE80211_C_WEP |           /* s/w WEP */
     241             :             IEEE80211_C_RSN;            /* WPA/RSN */
     242             : 
     243             :         /* set supported .11b and .11g rates */
     244           0 :         ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
     245           0 :         ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
     246             : 
     247             :         /* set supported .11b and .11g channels (1 through 14) */
     248           0 :         for (i = 1; i <= 14; i++) {
     249           0 :                 ic->ic_channels[i].ic_freq =
     250           0 :                     ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
     251           0 :                 ic->ic_channels[i].ic_flags =
     252             :                     IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
     253             :                     IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
     254             :         }
     255             : 
     256           0 :         ifp->if_softc = sc;
     257           0 :         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
     258           0 :         ifp->if_ioctl = rt2560_ioctl;
     259           0 :         ifp->if_start = rt2560_start;
     260           0 :         ifp->if_watchdog = rt2560_watchdog;
     261           0 :         memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
     262             : 
     263           0 :         if_attach(ifp);
     264           0 :         ieee80211_ifattach(ifp);
     265           0 :         ic->ic_node_alloc = rt2560_node_alloc;
     266           0 :         ic->ic_newassoc = rt2560_newassoc;
     267           0 :         ic->ic_updateslot = rt2560_updateslot;
     268             : 
     269             :         /* XXX RTS causes throughput problems -- where is the bug? */
     270           0 :         ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
     271             : 
     272             :         /* override state transition machine */
     273           0 :         sc->sc_newstate = ic->ic_newstate;
     274           0 :         ic->ic_newstate = rt2560_newstate;
     275           0 :         ieee80211_media_init(ifp, rt2560_media_change, ieee80211_media_status);
     276             : 
     277             : #if NBPFILTER > 0
     278           0 :         bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
     279             :             sizeof (struct ieee80211_frame) + 64);
     280             : 
     281           0 :         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
     282           0 :         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
     283           0 :         sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
     284             : 
     285           0 :         sc->sc_txtap_len = sizeof sc->sc_txtapu;
     286           0 :         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
     287           0 :         sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
     288             : #endif
     289           0 :         return 0;
     290             : 
     291           0 : fail5:  rt2560_free_tx_ring(sc, &sc->bcnq);
     292           0 : fail4:  rt2560_free_tx_ring(sc, &sc->prioq);
     293           0 : fail3:  rt2560_free_tx_ring(sc, &sc->atimq);
     294           0 : fail2:  rt2560_free_tx_ring(sc, &sc->txq);
     295           0 : fail1:  return ENXIO;
     296           0 : }
     297             : 
     298             : int
     299           0 : rt2560_detach(void *xsc)
     300             : {
     301           0 :         struct rt2560_softc *sc = xsc;
     302           0 :         struct ifnet *ifp = &sc->sc_ic.ic_if;
     303             : 
     304           0 :         timeout_del(&sc->scan_to);
     305           0 :         timeout_del(&sc->amrr_to);
     306             : 
     307           0 :         ieee80211_ifdetach(ifp);        /* free all nodes */
     308           0 :         if_detach(ifp);
     309             : 
     310           0 :         rt2560_free_tx_ring(sc, &sc->txq);
     311           0 :         rt2560_free_tx_ring(sc, &sc->atimq);
     312           0 :         rt2560_free_tx_ring(sc, &sc->prioq);
     313           0 :         rt2560_free_tx_ring(sc, &sc->bcnq);
     314           0 :         rt2560_free_rx_ring(sc, &sc->rxq);
     315             : 
     316           0 :         return 0;
     317             : }
     318             : 
     319             : void
     320           0 : rt2560_suspend(void *xsc)
     321             : {
     322           0 :         struct rt2560_softc *sc = xsc;
     323           0 :         struct ifnet *ifp = &sc->sc_ic.ic_if;
     324             : 
     325           0 :         if (ifp->if_flags & IFF_RUNNING)
     326           0 :                 rt2560_stop(ifp, 1);
     327           0 : }
     328             : 
     329             : void
     330           0 : rt2560_wakeup(void *xsc)
     331             : {
     332           0 :         struct rt2560_softc *sc = xsc;
     333           0 :         struct ifnet *ifp = &sc->sc_ic.ic_if;
     334             : 
     335           0 :         if (ifp->if_flags & IFF_UP)
     336           0 :                 rt2560_init(ifp);
     337           0 : }
     338             : 
     339             : int
     340           0 : rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
     341             :     int count)
     342             : {
     343           0 :         int i, nsegs, error;
     344             : 
     345           0 :         ring->count = count;
     346           0 :         ring->queued = 0;
     347           0 :         ring->cur = ring->next = 0;
     348           0 :         ring->cur_encrypt = ring->next_encrypt = 0;
     349             : 
     350           0 :         error = bus_dmamap_create(sc->sc_dmat, count * RT2560_TX_DESC_SIZE, 1,
     351             :             count * RT2560_TX_DESC_SIZE, 0, BUS_DMA_NOWAIT, &ring->map);
     352           0 :         if (error != 0) {
     353           0 :                 printf("%s: could not create desc DMA map\n",
     354           0 :                     sc->sc_dev.dv_xname);
     355           0 :                 goto fail;
     356             :         }
     357             : 
     358           0 :         error = bus_dmamem_alloc(sc->sc_dmat, count * RT2560_TX_DESC_SIZE,
     359             :             PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
     360           0 :         if (error != 0) {
     361           0 :                 printf("%s: could not allocate DMA memory\n",
     362           0 :                     sc->sc_dev.dv_xname);
     363           0 :                 goto fail;
     364             :         }
     365             : 
     366           0 :         error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
     367             :             count * RT2560_TX_DESC_SIZE, (caddr_t *)&ring->desc,
     368             :             BUS_DMA_NOWAIT);
     369           0 :         if (error != 0) {
     370           0 :                 printf("%s: can't map desc DMA memory\n",
     371           0 :                     sc->sc_dev.dv_xname);
     372           0 :                 goto fail;
     373             :         }
     374             : 
     375           0 :         error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
     376             :             count * RT2560_TX_DESC_SIZE, NULL, BUS_DMA_NOWAIT);
     377           0 :         if (error != 0) {
     378           0 :                 printf("%s: could not load desc DMA map\n",
     379           0 :                     sc->sc_dev.dv_xname);
     380           0 :                 goto fail;
     381             :         }
     382             : 
     383           0 :         ring->physaddr = ring->map->dm_segs->ds_addr;
     384             : 
     385           0 :         ring->data = mallocarray(count, sizeof (struct rt2560_tx_data),
     386             :             M_DEVBUF, M_NOWAIT | M_ZERO);
     387           0 :         if (ring->data == NULL) {
     388           0 :                 printf("%s: could not allocate soft data\n",
     389           0 :                     sc->sc_dev.dv_xname);
     390             :                 error = ENOMEM;
     391           0 :                 goto fail;
     392             :         }
     393             : 
     394           0 :         for (i = 0; i < count; i++) {
     395           0 :                 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
     396             :                     RT2560_MAX_SCATTER, MCLBYTES, 0, BUS_DMA_NOWAIT,
     397             :                     &ring->data[i].map);
     398           0 :                 if (error != 0) {
     399           0 :                         printf("%s: could not create DMA map\n",
     400           0 :                             sc->sc_dev.dv_xname);
     401           0 :                         goto fail;
     402             :                 }
     403             :         }
     404             : 
     405           0 :         return 0;
     406             : 
     407           0 : fail:   rt2560_free_tx_ring(sc, ring);
     408           0 :         return error;
     409           0 : }
     410             : 
     411             : void
     412           0 : rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
     413             : {
     414             :         int i;
     415             : 
     416           0 :         for (i = 0; i < ring->count; i++) {
     417           0 :                 struct rt2560_tx_desc *desc = &ring->desc[i];
     418           0 :                 struct rt2560_tx_data *data = &ring->data[i];
     419             : 
     420           0 :                 if (data->m != NULL) {
     421           0 :                         bus_dmamap_sync(sc->sc_dmat, data->map, 0,
     422             :                             data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
     423           0 :                         bus_dmamap_unload(sc->sc_dmat, data->map);
     424           0 :                         m_freem(data->m);
     425           0 :                         data->m = NULL;
     426           0 :                 }
     427             : 
     428             :                 /*
     429             :                  * The node has already been freed at that point so don't call
     430             :                  * ieee80211_release_node() here.
     431             :                  */
     432           0 :                 data->ni = NULL;
     433             : 
     434           0 :                 desc->flags = 0;
     435             :         }
     436             : 
     437           0 :         bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
     438             :             BUS_DMASYNC_PREWRITE);
     439             : 
     440           0 :         ring->queued = 0;
     441           0 :         ring->cur = ring->next = 0;
     442           0 :         ring->cur_encrypt = ring->next_encrypt = 0;
     443           0 : }
     444             : 
     445             : void
     446           0 : rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
     447             : {
     448             :         int i;
     449             : 
     450           0 :         if (ring->desc != NULL) {
     451           0 :                 bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
     452             :                     ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
     453           0 :                 bus_dmamap_unload(sc->sc_dmat, ring->map);
     454           0 :                 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
     455             :                     ring->count * RT2560_TX_DESC_SIZE);
     456           0 :                 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
     457           0 :         }
     458             : 
     459           0 :         if (ring->data != NULL) {
     460           0 :                 for (i = 0; i < ring->count; i++) {
     461           0 :                         struct rt2560_tx_data *data = &ring->data[i];
     462             : 
     463           0 :                         if (data->m != NULL) {
     464           0 :                                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
     465             :                                     data->map->dm_mapsize,
     466             :                                     BUS_DMASYNC_POSTWRITE);
     467           0 :                                 bus_dmamap_unload(sc->sc_dmat, data->map);
     468           0 :                                 m_freem(data->m);
     469           0 :                         }
     470             : 
     471             :                         /*
     472             :                          * The node has already been freed at that point so
     473             :                          * don't call ieee80211_release_node() here.
     474             :                          */
     475           0 :                         data->ni = NULL;
     476             : 
     477           0 :                         if (data->map != NULL)
     478           0 :                                 bus_dmamap_destroy(sc->sc_dmat, data->map);
     479             :                 }
     480           0 :                 free(ring->data, M_DEVBUF, ring->count * sizeof *ring->data);
     481           0 :         }
     482           0 : }
     483             : 
     484             : int
     485           0 : rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
     486             :     int count)
     487             : {
     488           0 :         int i, nsegs, error;
     489             : 
     490           0 :         ring->count = count;
     491           0 :         ring->cur = ring->next = 0;
     492           0 :         ring->cur_decrypt = 0;
     493             : 
     494           0 :         error = bus_dmamap_create(sc->sc_dmat, count * RT2560_RX_DESC_SIZE, 1,
     495             :             count * RT2560_RX_DESC_SIZE, 0, BUS_DMA_NOWAIT, &ring->map);
     496           0 :         if (error != 0) {
     497           0 :                 printf("%s: could not create desc DMA map\n",
     498           0 :                     sc->sc_dev.dv_xname);
     499           0 :                 goto fail;
     500             :         }
     501             : 
     502           0 :         error = bus_dmamem_alloc(sc->sc_dmat, count * RT2560_RX_DESC_SIZE,
     503             :             PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
     504           0 :         if (error != 0) {
     505           0 :                 printf("%s: could not allocate DMA memory\n",
     506           0 :                     sc->sc_dev.dv_xname);
     507           0 :                 goto fail;
     508             :         }
     509             : 
     510           0 :         error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
     511             :             count * RT2560_RX_DESC_SIZE, (caddr_t *)&ring->desc,
     512             :             BUS_DMA_NOWAIT);
     513           0 :         if (error != 0) {
     514           0 :                 printf("%s: can't map desc DMA memory\n",
     515           0 :                     sc->sc_dev.dv_xname);
     516           0 :                 goto fail;
     517             :         }
     518             : 
     519           0 :         error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
     520             :             count * RT2560_RX_DESC_SIZE, NULL, BUS_DMA_NOWAIT);
     521           0 :         if (error != 0) {
     522           0 :                 printf("%s: could not load desc DMA map\n",
     523           0 :                     sc->sc_dev.dv_xname);
     524           0 :                 goto fail;
     525             :         }
     526             : 
     527           0 :         ring->physaddr = ring->map->dm_segs->ds_addr;
     528             : 
     529           0 :         ring->data = mallocarray(count, sizeof (struct rt2560_rx_data),
     530             :             M_DEVBUF, M_NOWAIT | M_ZERO);
     531           0 :         if (ring->data == NULL) {
     532           0 :                 printf("%s: could not allocate soft data\n",
     533           0 :                     sc->sc_dev.dv_xname);
     534             :                 error = ENOMEM;
     535           0 :                 goto fail;
     536             :         }
     537             : 
     538             :         /*
     539             :          * Pre-allocate Rx buffers and populate Rx ring.
     540             :          */
     541           0 :         for (i = 0; i < count; i++) {
     542           0 :                 struct rt2560_rx_desc *desc = &sc->rxq.desc[i];
     543           0 :                 struct rt2560_rx_data *data = &sc->rxq.data[i];
     544             : 
     545           0 :                 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
     546             :                     0, BUS_DMA_NOWAIT, &data->map);
     547           0 :                 if (error != 0) {
     548           0 :                         printf("%s: could not create DMA map\n",
     549           0 :                             sc->sc_dev.dv_xname);
     550           0 :                         goto fail;
     551             :                 }
     552             : 
     553           0 :                 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
     554           0 :                 if (data->m == NULL) {
     555           0 :                         printf("%s: could not allocate rx mbuf\n",
     556           0 :                             sc->sc_dev.dv_xname);
     557             :                         error = ENOMEM;
     558           0 :                         goto fail;
     559             :                 }
     560           0 :                 MCLGET(data->m, M_DONTWAIT);
     561           0 :                 if (!(data->m->m_flags & M_EXT)) {
     562           0 :                         printf("%s: could not allocate rx mbuf cluster\n",
     563           0 :                             sc->sc_dev.dv_xname);
     564             :                         error = ENOMEM;
     565           0 :                         goto fail;
     566             :                 }
     567             : 
     568           0 :                 error = bus_dmamap_load(sc->sc_dmat, data->map,
     569             :                     mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
     570           0 :                 if (error != 0) {
     571           0 :                         printf("%s: could not load rx buf DMA map",
     572           0 :                             sc->sc_dev.dv_xname);
     573           0 :                         goto fail;
     574             :                 }
     575             : 
     576           0 :                 desc->flags = htole32(RT2560_RX_BUSY);
     577           0 :                 desc->physaddr = htole32(data->map->dm_segs->ds_addr);
     578           0 :         }
     579             : 
     580           0 :         bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
     581             :             BUS_DMASYNC_PREWRITE);
     582             : 
     583           0 :         return 0;
     584             : 
     585           0 : fail:   rt2560_free_rx_ring(sc, ring);
     586           0 :         return error;
     587           0 : }
     588             : 
     589             : void
     590           0 : rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
     591             : {
     592             :         int i;
     593             : 
     594           0 :         for (i = 0; i < ring->count; i++) {
     595           0 :                 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
     596           0 :                 ring->data[i].drop = 0;
     597             :         }
     598             : 
     599           0 :         bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
     600             :             BUS_DMASYNC_PREWRITE);
     601             : 
     602           0 :         ring->cur = ring->next = 0;
     603           0 :         ring->cur_decrypt = 0;
     604           0 : }
     605             : 
     606             : void
     607           0 : rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
     608             : {
     609             :         int i;
     610             : 
     611           0 :         if (ring->desc != NULL) {
     612           0 :                 bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
     613             :                     ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
     614           0 :                 bus_dmamap_unload(sc->sc_dmat, ring->map);
     615           0 :                 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
     616             :                     ring->count * RT2560_RX_DESC_SIZE);
     617           0 :                 bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
     618           0 :         }
     619             : 
     620           0 :         if (ring->data != NULL) {
     621           0 :                 for (i = 0; i < ring->count; i++) {
     622           0 :                         struct rt2560_rx_data *data = &ring->data[i];
     623             : 
     624           0 :                         if (data->m != NULL) {
     625           0 :                                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
     626             :                                     data->map->dm_mapsize,
     627             :                                     BUS_DMASYNC_POSTREAD);
     628           0 :                                 bus_dmamap_unload(sc->sc_dmat, data->map);
     629           0 :                                 m_freem(data->m);
     630           0 :                         }
     631             : 
     632           0 :                         if (data->map != NULL)
     633           0 :                                 bus_dmamap_destroy(sc->sc_dmat, data->map);
     634             :                 }
     635           0 :                 free(ring->data, M_DEVBUF, ring->count * sizeof *ring->data);
     636           0 :         }
     637           0 : }
     638             : 
     639             : struct ieee80211_node *
     640           0 : rt2560_node_alloc(struct ieee80211com *ic)
     641             : {
     642           0 :         return malloc(sizeof (struct rt2560_node), M_DEVBUF,
     643             :             M_NOWAIT | M_ZERO);
     644             : }
     645             : 
     646             : int
     647           0 : rt2560_media_change(struct ifnet *ifp)
     648             : {
     649             :         int error;
     650             : 
     651           0 :         error = ieee80211_media_change(ifp);
     652           0 :         if (error != ENETRESET)
     653           0 :                 return error;
     654             : 
     655           0 :         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
     656           0 :                 rt2560_init(ifp);
     657             : 
     658           0 :         return 0;
     659           0 : }
     660             : 
     661             : /*
     662             :  * This function is called periodically (every 200ms) during scanning to
     663             :  * switch from one channel to another.
     664             :  */
     665             : void
     666           0 : rt2560_next_scan(void *arg)
     667             : {
     668           0 :         struct rt2560_softc *sc = arg;
     669           0 :         struct ieee80211com *ic = &sc->sc_ic;
     670           0 :         struct ifnet *ifp = &ic->ic_if;
     671             :         int s;
     672             : 
     673           0 :         s = splnet();
     674           0 :         if (ic->ic_state == IEEE80211_S_SCAN)
     675           0 :                 ieee80211_next_scan(ifp);
     676           0 :         splx(s);
     677           0 : }
     678             : 
     679             : /*
     680             :  * This function is called for each neighbor node.
     681             :  */
     682             : void
     683           0 : rt2560_iter_func(void *arg, struct ieee80211_node *ni)
     684             : {
     685           0 :         struct rt2560_softc *sc = arg;
     686           0 :         struct rt2560_node *rn = (struct rt2560_node *)ni;
     687             : 
     688           0 :         ieee80211_amrr_choose(&sc->amrr, ni, &rn->amn);
     689           0 : }
     690             : 
     691             : void
     692           0 : rt2560_amrr_timeout(void *arg)
     693             : {
     694           0 :         struct rt2560_softc *sc = arg;
     695           0 :         struct ieee80211com *ic = &sc->sc_ic;
     696             :         int s;
     697             : 
     698           0 :         s = splnet();
     699           0 :         if (ic->ic_opmode == IEEE80211_M_STA)
     700           0 :                 rt2560_iter_func(sc, ic->ic_bss);
     701             : #ifndef IEEE80211_STA_ONLY
     702             :         else
     703           0 :                 ieee80211_iterate_nodes(ic, rt2560_iter_func, sc);
     704             : #endif
     705           0 :         splx(s);
     706             : 
     707           0 :         timeout_add_msec(&sc->amrr_to, 500);
     708           0 : }
     709             : 
     710             : void
     711           0 : rt2560_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
     712             : {
     713           0 :         struct rt2560_softc *sc = ic->ic_softc;
     714             :         int i;
     715             : 
     716           0 :         ieee80211_amrr_node_init(&sc->amrr, &((struct rt2560_node *)ni)->amn);
     717             : 
     718             :         /* set rate to some reasonable initial value */
     719           0 :         for (i = ni->ni_rates.rs_nrates - 1;
     720           0 :              i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
     721           0 :              i--);
     722           0 :         ni->ni_txrate = i;
     723           0 : }
     724             : 
     725             : int
     726           0 : rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
     727             : {
     728           0 :         struct rt2560_softc *sc = ic->ic_if.if_softc;
     729             :         enum ieee80211_state ostate;
     730             :         struct ieee80211_node *ni;
     731             :         int error = 0;
     732             : 
     733           0 :         ostate = ic->ic_state;
     734           0 :         timeout_del(&sc->scan_to);
     735           0 :         timeout_del(&sc->amrr_to);
     736             : 
     737           0 :         switch (nstate) {
     738             :         case IEEE80211_S_INIT:
     739           0 :                 if (ostate == IEEE80211_S_RUN) {
     740             :                         /* abort TSF synchronization */
     741           0 :                         RAL_WRITE(sc, RT2560_CSR14, 0);
     742             : 
     743             :                         /* turn association led off */
     744           0 :                         rt2560_update_led(sc, 0, 0);
     745           0 :                 }
     746             :                 break;
     747             : 
     748             :         case IEEE80211_S_SCAN:
     749           0 :                 rt2560_set_chan(sc, ic->ic_bss->ni_chan);
     750           0 :                 timeout_add_msec(&sc->scan_to, 200);
     751           0 :                 break;
     752             : 
     753             :         case IEEE80211_S_AUTH:
     754           0 :                 rt2560_set_chan(sc, ic->ic_bss->ni_chan);
     755           0 :                 break;
     756             : 
     757             :         case IEEE80211_S_ASSOC:
     758           0 :                 rt2560_set_chan(sc, ic->ic_bss->ni_chan);
     759           0 :                 break;
     760             : 
     761             :         case IEEE80211_S_RUN:
     762           0 :                 rt2560_set_chan(sc, ic->ic_bss->ni_chan);
     763             : 
     764           0 :                 ni = ic->ic_bss;
     765             : 
     766           0 :                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
     767           0 :                         rt2560_update_plcp(sc);
     768           0 :                         rt2560_set_slottime(sc);
     769           0 :                         rt2560_set_basicrates(sc);
     770           0 :                         rt2560_set_bssid(sc, ni->ni_bssid);
     771           0 :                 }
     772             : 
     773             : #ifndef IEEE80211_STA_ONLY
     774           0 :                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
     775           0 :                     ic->ic_opmode == IEEE80211_M_IBSS) {
     776           0 :                         struct mbuf *m = ieee80211_beacon_alloc(ic, ni);
     777           0 :                         if (m == NULL) {
     778           0 :                                 printf("%s: could not allocate beacon\n",
     779           0 :                                     sc->sc_dev.dv_xname);
     780             :                                 error = ENOBUFS;
     781           0 :                                 break;
     782             :                         }
     783             : 
     784           0 :                         error = rt2560_tx_bcn(sc, m, ni);
     785           0 :                         if (error != 0)
     786           0 :                                 break;
     787           0 :                 }
     788             : #endif
     789             : 
     790             :                 /* turn assocation led on */
     791           0 :                 rt2560_update_led(sc, 1, 0);
     792             : 
     793           0 :                 if (ic->ic_opmode == IEEE80211_M_STA) {
     794             :                         /* fake a join to init the tx rate */
     795           0 :                         rt2560_newassoc(ic, ni, 1);
     796           0 :                 }
     797             : 
     798           0 :                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
     799             :                         /* start automatic rate control timer */
     800           0 :                         if (ic->ic_fixed_rate == -1)
     801           0 :                                 timeout_add_msec(&sc->amrr_to, 500);
     802             : 
     803           0 :                         rt2560_enable_tsf_sync(sc);
     804           0 :                 }
     805             :                 break;
     806             :         }
     807             : 
     808           0 :         return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
     809             : }
     810             : 
     811             : /*
     812             :  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
     813             :  * 93C66).
     814             :  */
     815             : uint16_t
     816           0 : rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
     817             : {
     818             :         uint32_t tmp;
     819             :         uint16_t val;
     820             :         int n;
     821             : 
     822             :         /* clock C once before the first command */
     823           0 :         RT2560_EEPROM_CTL(sc, 0);
     824             : 
     825           0 :         RT2560_EEPROM_CTL(sc, RT2560_S);
     826           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
     827           0 :         RT2560_EEPROM_CTL(sc, RT2560_S);
     828             : 
     829             :         /* write start bit (1) */
     830           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
     831           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
     832             : 
     833             :         /* write READ opcode (10) */
     834           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
     835           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
     836           0 :         RT2560_EEPROM_CTL(sc, RT2560_S);
     837           0 :         RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
     838             : 
     839             :         /* write address (A5-A0 or A7-A0) */
     840           0 :         n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
     841           0 :         for (; n >= 0; n--) {
     842           0 :                 RT2560_EEPROM_CTL(sc, RT2560_S |
     843             :                     (((addr >> n) & 1) << RT2560_SHIFT_D));
     844           0 :                 RT2560_EEPROM_CTL(sc, RT2560_S |
     845             :                     (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
     846             :         }
     847             : 
     848           0 :         RT2560_EEPROM_CTL(sc, RT2560_S);
     849             : 
     850             :         /* read data Q15-Q0 */
     851             :         val = 0;
     852           0 :         for (n = 15; n >= 0; n--) {
     853           0 :                 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
     854           0 :                 tmp = RAL_READ(sc, RT2560_CSR21);
     855           0 :                 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
     856           0 :                 RT2560_EEPROM_CTL(sc, RT2560_S);
     857             :         }
     858             : 
     859           0 :         RT2560_EEPROM_CTL(sc, 0);
     860             : 
     861             :         /* clear Chip Select and clock C */
     862           0 :         RT2560_EEPROM_CTL(sc, RT2560_S);
     863           0 :         RT2560_EEPROM_CTL(sc, 0);
     864           0 :         RT2560_EEPROM_CTL(sc, RT2560_C);
     865             : 
     866           0 :         return val;
     867             : }
     868             : 
     869             : /*
     870             :  * Some frames were processed by the hardware cipher engine and are ready for
     871             :  * transmission.
     872             :  */
     873             : void
     874           0 : rt2560_encryption_intr(struct rt2560_softc *sc)
     875             : {
     876             :         int hw;
     877             : 
     878             :         /* retrieve last descriptor index processed by cipher engine */
     879           0 :         hw = (RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr) /
     880             :             RT2560_TX_DESC_SIZE;
     881             : 
     882           0 :         for (; sc->txq.next_encrypt != hw;) {
     883             :                 struct rt2560_tx_desc *desc =
     884           0 :                     &sc->txq.desc[sc->txq.next_encrypt];
     885             : 
     886           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
     887             :                     sc->txq.next_encrypt * RT2560_TX_DESC_SIZE,
     888             :                     RT2560_TX_DESC_SIZE, BUS_DMASYNC_POSTREAD);
     889             : 
     890           0 :                 if (letoh32(desc->flags) &
     891             :                     (RT2560_TX_BUSY | RT2560_TX_CIPHER_BUSY))
     892           0 :                         break;
     893             : 
     894             :                 /* for TKIP, swap eiv field to fix a bug in ASIC */
     895           0 :                 if ((letoh32(desc->flags) & RT2560_TX_CIPHER_MASK) ==
     896             :                     RT2560_TX_CIPHER_TKIP)
     897           0 :                         desc->eiv = swap32(desc->eiv);
     898             : 
     899             :                 /* mark the frame ready for transmission */
     900           0 :                 desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
     901             : 
     902           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
     903             :                     sc->txq.next_encrypt * RT2560_TX_DESC_SIZE,
     904             :                     RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
     905             : 
     906             :                 DPRINTFN(15, ("encryption done idx=%u\n",
     907             :                     sc->txq.next_encrypt));
     908             : 
     909           0 :                 sc->txq.next_encrypt =
     910           0 :                     (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
     911           0 :         }
     912             : 
     913             :         /* kick Tx */
     914           0 :         RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
     915           0 : }
     916             : 
     917             : void
     918           0 : rt2560_tx_intr(struct rt2560_softc *sc)
     919             : {
     920           0 :         struct ieee80211com *ic = &sc->sc_ic;
     921           0 :         struct ifnet *ifp = &ic->ic_if;
     922             : 
     923           0 :         for (;;) {
     924           0 :                 struct rt2560_tx_desc *desc = &sc->txq.desc[sc->txq.next];
     925           0 :                 struct rt2560_tx_data *data = &sc->txq.data[sc->txq.next];
     926             :                 struct rt2560_node *rn;
     927             : 
     928           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
     929             :                     sc->txq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
     930             :                     BUS_DMASYNC_POSTREAD);
     931             : 
     932           0 :                 if ((letoh32(desc->flags) & RT2560_TX_BUSY) ||
     933           0 :                     (letoh32(desc->flags) & RT2560_TX_CIPHER_BUSY) ||
     934           0 :                     !(letoh32(desc->flags) & RT2560_TX_VALID))
     935           0 :                         break;
     936             : 
     937           0 :                 rn = (struct rt2560_node *)data->ni;
     938             : 
     939           0 :                 switch (letoh32(desc->flags) & RT2560_TX_RESULT_MASK) {
     940             :                 case RT2560_TX_SUCCESS:
     941             :                         DPRINTFN(10, ("data frame sent successfully\n"));
     942           0 :                         rn->amn.amn_txcnt++;
     943           0 :                         break;
     944             : 
     945             :                 case RT2560_TX_SUCCESS_RETRY:
     946             :                         DPRINTFN(9, ("data frame sent after %u retries\n",
     947             :                             (letoh32(desc->flags) >> 5) & 0x7));
     948           0 :                         rn->amn.amn_txcnt++;
     949           0 :                         rn->amn.amn_retrycnt++;
     950           0 :                         break;
     951             : 
     952             :                 case RT2560_TX_FAIL_RETRY:
     953             :                         DPRINTFN(9, ("sending data frame failed (too much "
     954             :                             "retries)\n"));
     955           0 :                         rn->amn.amn_txcnt++;
     956           0 :                         rn->amn.amn_retrycnt++;
     957           0 :                         ifp->if_oerrors++;
     958           0 :                         break;
     959             : 
     960             :                 case RT2560_TX_FAIL_INVALID:
     961             :                 case RT2560_TX_FAIL_OTHER:
     962             :                 default:
     963             :                         DPRINTF(("%s: sending data frame failed 0x%08x\n",
     964             :                             sc->sc_dev.dv_xname, letoh32(desc->flags)));
     965           0 :                         ifp->if_oerrors++;
     966           0 :                         break;
     967             :                 }
     968             : 
     969             :                 /* descriptor is no longer valid */
     970           0 :                 desc->flags &= ~htole32(RT2560_TX_VALID);
     971             : 
     972           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
     973             :                     sc->txq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
     974             :                     BUS_DMASYNC_PREWRITE);
     975             : 
     976           0 :                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
     977             :                     data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
     978           0 :                 bus_dmamap_unload(sc->sc_dmat, data->map);
     979           0 :                 m_freem(data->m);
     980           0 :                 data->m = NULL;
     981           0 :                 ieee80211_release_node(ic, data->ni);
     982           0 :                 data->ni = NULL;
     983             : 
     984             :                 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
     985             : 
     986           0 :                 sc->txq.queued--;
     987           0 :                 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
     988           0 :         }
     989             : 
     990           0 :         if (sc->txq.queued == 0 && sc->prioq.queued == 0)
     991           0 :                 sc->sc_tx_timer = 0;
     992           0 :         if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
     993           0 :                 sc->sc_flags &= ~RT2560_DATA_OACTIVE;
     994           0 :                 if (!(sc->sc_flags & (RT2560_DATA_OACTIVE|RT2560_PRIO_OACTIVE)))
     995           0 :                         ifq_clr_oactive(&ifp->if_snd);
     996           0 :                 rt2560_start(ifp);
     997           0 :         }
     998           0 : }
     999             : 
    1000             : void
    1001           0 : rt2560_prio_intr(struct rt2560_softc *sc)
    1002             : {
    1003           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1004           0 :         struct ifnet *ifp = &ic->ic_if;
    1005             : 
    1006           0 :         for (;;) {
    1007           0 :                 struct rt2560_tx_desc *desc = &sc->prioq.desc[sc->prioq.next];
    1008           0 :                 struct rt2560_tx_data *data = &sc->prioq.data[sc->prioq.next];
    1009             : 
    1010           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
    1011             :                     sc->prioq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
    1012             :                     BUS_DMASYNC_POSTREAD);
    1013             : 
    1014           0 :                 if ((letoh32(desc->flags) & RT2560_TX_BUSY) ||
    1015           0 :                     !(letoh32(desc->flags) & RT2560_TX_VALID))
    1016           0 :                         break;
    1017             : 
    1018           0 :                 switch (letoh32(desc->flags) & RT2560_TX_RESULT_MASK) {
    1019             :                 case RT2560_TX_SUCCESS:
    1020             :                         DPRINTFN(10, ("mgt frame sent successfully\n"));
    1021             :                         break;
    1022             : 
    1023             :                 case RT2560_TX_SUCCESS_RETRY:
    1024             :                         DPRINTFN(9, ("mgt frame sent after %u retries\n",
    1025             :                             (letoh32(desc->flags) >> 5) & 0x7));
    1026             :                         break;
    1027             : 
    1028             :                 case RT2560_TX_FAIL_RETRY:
    1029             :                         DPRINTFN(9, ("sending mgt frame failed (too much "
    1030             :                             "retries)\n"));
    1031             :                         break;
    1032             : 
    1033             :                 case RT2560_TX_FAIL_INVALID:
    1034             :                 case RT2560_TX_FAIL_OTHER:
    1035             :                 default:
    1036             :                         DPRINTF(("%s: sending mgt frame failed 0x%08x\n",
    1037             :                             sc->sc_dev.dv_xname, letoh32(desc->flags)));
    1038             :                         break;
    1039             :                 }
    1040             : 
    1041             :                 /* descriptor is no longer valid */
    1042           0 :                 desc->flags &= ~htole32(RT2560_TX_VALID);
    1043             : 
    1044           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
    1045             :                     sc->prioq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
    1046             :                     BUS_DMASYNC_PREWRITE);
    1047             : 
    1048           0 :                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    1049             :                     data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    1050           0 :                 bus_dmamap_unload(sc->sc_dmat, data->map);
    1051           0 :                 m_freem(data->m);
    1052           0 :                 data->m = NULL;
    1053           0 :                 ieee80211_release_node(ic, data->ni);
    1054           0 :                 data->ni = NULL;
    1055             : 
    1056             :                 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
    1057             : 
    1058           0 :                 sc->prioq.queued--;
    1059           0 :                 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
    1060           0 :         }
    1061             : 
    1062           0 :         if (sc->txq.queued == 0 && sc->prioq.queued == 0)
    1063           0 :                 sc->sc_tx_timer = 0;
    1064           0 :         if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
    1065           0 :                 sc->sc_flags &= ~RT2560_PRIO_OACTIVE;
    1066           0 :                 if (!(sc->sc_flags & (RT2560_DATA_OACTIVE|RT2560_PRIO_OACTIVE)))
    1067           0 :                         ifq_clr_oactive(&ifp->if_snd);
    1068           0 :                 rt2560_start(ifp);
    1069           0 :         }
    1070           0 : }
    1071             : 
    1072             : /*
    1073             :  * Some frames were processed by the hardware cipher engine and are ready for
    1074             :  * transmission to the IEEE802.11 layer.
    1075             :  */
    1076             : void
    1077           0 : rt2560_decryption_intr(struct rt2560_softc *sc)
    1078             : {
    1079           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1080           0 :         struct ifnet *ifp = &ic->ic_if;
    1081             :         struct ieee80211_frame *wh;
    1082           0 :         struct ieee80211_rxinfo rxi;
    1083             :         struct ieee80211_node *ni;
    1084             :         struct mbuf *mnew, *m;
    1085             :         int hw, error;
    1086             : 
    1087             :         /* retrieve last decriptor index processed by cipher engine */
    1088           0 :         hw = (RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr) /
    1089             :             RT2560_RX_DESC_SIZE;
    1090             : 
    1091           0 :         for (; sc->rxq.cur_decrypt != hw;) {
    1092             :                 struct rt2560_rx_desc *desc =
    1093           0 :                     &sc->rxq.desc[sc->rxq.cur_decrypt];
    1094             :                 struct rt2560_rx_data *data =
    1095           0 :                     &sc->rxq.data[sc->rxq.cur_decrypt];
    1096             : 
    1097           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
    1098             :                     sc->rxq.cur_decrypt * RT2560_TX_DESC_SIZE,
    1099             :                     RT2560_TX_DESC_SIZE, BUS_DMASYNC_POSTREAD);
    1100             : 
    1101           0 :                 if (letoh32(desc->flags) &
    1102             :                     (RT2560_RX_BUSY | RT2560_RX_CIPHER_BUSY))
    1103           0 :                         break;
    1104             : 
    1105           0 :                 if (data->drop) {
    1106           0 :                         ifp->if_ierrors++;
    1107           0 :                         goto skip;
    1108             :                 }
    1109             : 
    1110           0 :                 if ((letoh32(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
    1111           0 :                     (letoh32(desc->flags) & RT2560_RX_ICV_ERROR)) {
    1112           0 :                         ifp->if_ierrors++;
    1113           0 :                         goto skip;
    1114             :                 }
    1115             : 
    1116             :                 /*
    1117             :                  * Try to allocate a new mbuf for this ring element and load it
    1118             :                  * before processing the current mbuf.  If the ring element
    1119             :                  * cannot be loaded, drop the received packet and reuse the old
    1120             :                  * mbuf.  In the unlikely case that the old mbuf can't be
    1121             :                  * reloaded either, explicitly panic.
    1122             :                  */
    1123           0 :                 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
    1124           0 :                 if (mnew == NULL) {
    1125           0 :                         ifp->if_ierrors++;
    1126           0 :                         goto skip;
    1127             :                 }
    1128           0 :                 MCLGET(mnew, M_DONTWAIT);
    1129           0 :                 if (!(mnew->m_flags & M_EXT)) {
    1130           0 :                         m_freem(mnew);
    1131           0 :                         ifp->if_ierrors++;
    1132           0 :                         goto skip;
    1133             :                 }
    1134             : 
    1135           0 :                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    1136             :                     data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
    1137           0 :                 bus_dmamap_unload(sc->sc_dmat, data->map);
    1138             : 
    1139           0 :                 error = bus_dmamap_load(sc->sc_dmat, data->map,
    1140             :                     mtod(mnew, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
    1141           0 :                 if (error != 0) {
    1142           0 :                         m_freem(mnew);
    1143             : 
    1144             :                         /* try to reload the old mbuf */
    1145           0 :                         error = bus_dmamap_load(sc->sc_dmat, data->map,
    1146             :                             mtod(data->m, void *), MCLBYTES, NULL,
    1147             :                             BUS_DMA_NOWAIT);
    1148           0 :                         if (error != 0) {
    1149             :                                 /* very unlikely that it will fail... */
    1150           0 :                                 panic("%s: could not load old rx mbuf",
    1151           0 :                                     sc->sc_dev.dv_xname);
    1152             :                         }
    1153             :                         /* physical address may have changed */
    1154           0 :                         desc->physaddr = htole32(data->map->dm_segs->ds_addr);
    1155           0 :                         ifp->if_ierrors++;
    1156           0 :                         goto skip;
    1157             :                 }
    1158             : 
    1159             :                 /*
    1160             :                  * New mbuf successfully loaded, update Rx ring and continue
    1161             :                  * processing.
    1162             :                  */
    1163           0 :                 m = data->m;
    1164           0 :                 data->m = mnew;
    1165           0 :                 desc->physaddr = htole32(data->map->dm_segs->ds_addr);
    1166             : 
    1167             :                 /* finalize mbuf */
    1168           0 :                 m->m_pkthdr.len = m->m_len =
    1169           0 :                     (letoh32(desc->flags) >> 16) & 0xfff;
    1170             : 
    1171             : #if NBPFILTER > 0
    1172           0 :                 if (sc->sc_drvbpf != NULL) {
    1173           0 :                         struct mbuf mb;
    1174           0 :                         struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
    1175             :                         uint32_t tsf_lo, tsf_hi;
    1176             : 
    1177             :                         /* get timestamp (low and high 32 bits) */
    1178           0 :                         tsf_hi = RAL_READ(sc, RT2560_CSR17);
    1179           0 :                         tsf_lo = RAL_READ(sc, RT2560_CSR16);
    1180             : 
    1181           0 :                         tap->wr_tsf =
    1182           0 :                             htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
    1183           0 :                         tap->wr_flags = 0;
    1184           0 :                         tap->wr_rate = rt2560_rxrate(desc);
    1185           0 :                         tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
    1186           0 :                         tap->wr_chan_flags =
    1187           0 :                             htole16(ic->ic_ibss_chan->ic_flags);
    1188           0 :                         tap->wr_antenna = sc->rx_ant;
    1189           0 :                         tap->wr_antsignal = desc->rssi;
    1190             : 
    1191           0 :                         mb.m_data = (caddr_t)tap;
    1192           0 :                         mb.m_len = sc->sc_txtap_len;
    1193           0 :                         mb.m_next = m;
    1194           0 :                         mb.m_nextpkt = NULL;
    1195           0 :                         mb.m_type = 0;
    1196           0 :                         mb.m_flags = 0;
    1197           0 :                         bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
    1198           0 :                 }
    1199             : #endif
    1200           0 :                 wh = mtod(m, struct ieee80211_frame *);
    1201           0 :                 ni = ieee80211_find_rxnode(ic, wh);
    1202             : 
    1203             :                 /* send the frame to the 802.11 layer */
    1204           0 :                 rxi.rxi_flags = 0;
    1205           0 :                 rxi.rxi_rssi = desc->rssi;
    1206           0 :                 rxi.rxi_tstamp = 0;     /* unused */
    1207           0 :                 ieee80211_input(ifp, m, ni, &rxi);
    1208             : 
    1209             :                 /* node is no longer needed */
    1210           0 :                 ieee80211_release_node(ic, ni);
    1211             : 
    1212           0 : skip:           desc->flags = htole32(RT2560_RX_BUSY);
    1213             : 
    1214           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
    1215             :                     sc->rxq.cur_decrypt * RT2560_TX_DESC_SIZE,
    1216             :                     RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
    1217             : 
    1218             :                 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
    1219             : 
    1220           0 :                 sc->rxq.cur_decrypt =
    1221           0 :                     (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
    1222           0 :         }
    1223           0 : }
    1224             : 
    1225             : /*
    1226             :  * Some frames were received. Pass them to the hardware cipher engine before
    1227             :  * sending them to the 802.11 layer.
    1228             :  */
    1229             : void
    1230           0 : rt2560_rx_intr(struct rt2560_softc *sc)
    1231             : {
    1232           0 :         for (;;) {
    1233           0 :                 struct rt2560_rx_desc *desc = &sc->rxq.desc[sc->rxq.cur];
    1234           0 :                 struct rt2560_rx_data *data = &sc->rxq.data[sc->rxq.cur];
    1235             : 
    1236           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
    1237             :                     sc->rxq.cur * RT2560_RX_DESC_SIZE, RT2560_RX_DESC_SIZE,
    1238             :                     BUS_DMASYNC_POSTREAD);
    1239             : 
    1240           0 :                 if (letoh32(desc->flags) &
    1241             :                     (RT2560_RX_BUSY | RT2560_RX_CIPHER_BUSY))
    1242           0 :                         break;
    1243             : 
    1244           0 :                 data->drop = 0;
    1245             : 
    1246           0 :                 if (letoh32(desc->flags) &
    1247             :                     (RT2560_RX_PHY_ERROR | RT2560_RX_CRC_ERROR)) {
    1248             :                         /*
    1249             :                          * This should not happen since we did not request
    1250             :                          * to receive those frames when we filled RXCSR0.
    1251             :                          */
    1252             :                         DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
    1253             :                             letoh32(desc->flags)));
    1254           0 :                         data->drop = 1;
    1255           0 :                 }
    1256             : 
    1257           0 :                 if (((letoh32(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
    1258             :                         DPRINTFN(5, ("bad length\n"));
    1259           0 :                         data->drop = 1;
    1260           0 :                 }
    1261             : 
    1262             :                 /* mark the frame for decryption */
    1263           0 :                 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
    1264             : 
    1265           0 :                 bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
    1266             :                     sc->rxq.cur * RT2560_RX_DESC_SIZE, RT2560_RX_DESC_SIZE,
    1267             :                     BUS_DMASYNC_PREWRITE);
    1268             : 
    1269             :                 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
    1270             : 
    1271           0 :                 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
    1272           0 :         }
    1273             : 
    1274             :         /* kick decrypt */
    1275           0 :         RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
    1276           0 : }
    1277             : 
    1278             : #ifndef IEEE80211_STA_ONLY
    1279             : /*
    1280             :  * This function is called in HostAP or IBSS modes when it's time to send a
    1281             :  * new beacon (every ni_intval milliseconds).
    1282             :  */
    1283             : void
    1284           0 : rt2560_beacon_expire(struct rt2560_softc *sc)
    1285             : {
    1286           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1287             :         struct rt2560_tx_data *data;
    1288             : 
    1289           0 :         if (ic->ic_opmode != IEEE80211_M_IBSS &&
    1290           0 :             ic->ic_opmode != IEEE80211_M_HOSTAP)
    1291           0 :                 return;
    1292             : 
    1293           0 :         data = &sc->bcnq.data[sc->bcnq.next];
    1294             : 
    1295           0 :         if (sc->sc_flags & RT2560_UPDATE_SLOT) {
    1296           0 :                 sc->sc_flags &= ~RT2560_UPDATE_SLOT;
    1297           0 :                 sc->sc_flags |= RT2560_SET_SLOTTIME;
    1298           0 :         } else if (sc->sc_flags & RT2560_SET_SLOTTIME) {
    1299           0 :                 sc->sc_flags &= ~RT2560_SET_SLOTTIME;
    1300           0 :                 rt2560_set_slottime(sc);
    1301           0 :         }
    1302             : 
    1303           0 :         if (ic->ic_curmode == IEEE80211_MODE_11G) {
    1304             :                 /* update ERP Information Element */
    1305           0 :                 *sc->erp = ic->ic_bss->ni_erp;
    1306           0 :                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    1307             :                     data->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    1308           0 :         }
    1309             : 
    1310             : #if defined(RT2560_DEBUG) && NBPFILTER > 0
    1311             :         if (ic->ic_rawbpf != NULL)
    1312             :                 bpf_mtap(ic->ic_rawbpf, data->m, BPF_DIRECTION_OUT);
    1313             : #endif
    1314             : 
    1315             :         DPRINTFN(15, ("beacon expired\n"));
    1316           0 : }
    1317             : #endif
    1318             : 
    1319             : void
    1320           0 : rt2560_wakeup_expire(struct rt2560_softc *sc)
    1321             : {
    1322             :         DPRINTFN(15, ("wakeup expired\n"));
    1323           0 : }
    1324             : 
    1325             : int
    1326           0 : rt2560_intr(void *arg)
    1327             : {
    1328           0 :         struct rt2560_softc *sc = arg;
    1329           0 :         struct ifnet *ifp = &sc->sc_ic.ic_if;
    1330             :         uint32_t r;
    1331             : 
    1332           0 :         r = RAL_READ(sc, RT2560_CSR7);
    1333           0 :         if (__predict_false(r == 0xffffffff))
    1334           0 :                 return 0;       /* device likely went away */
    1335           0 :         if (r == 0)
    1336           0 :                 return 0;       /* not for us */
    1337             : 
    1338             :         /* disable interrupts */
    1339           0 :         RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
    1340             : 
    1341             :         /* acknowledge interrupts */
    1342           0 :         RAL_WRITE(sc, RT2560_CSR7, r);
    1343             : 
    1344             :         /* don't re-enable interrupts if we're shutting down */
    1345           0 :         if (!(ifp->if_flags & IFF_RUNNING))
    1346           0 :                 return 0;
    1347             : 
    1348             : #ifndef IEEE80211_STA_ONLY
    1349           0 :         if (r & RT2560_BEACON_EXPIRE)
    1350           0 :                 rt2560_beacon_expire(sc);
    1351             : #endif
    1352             : 
    1353           0 :         if (r & RT2560_WAKEUP_EXPIRE)
    1354           0 :                 rt2560_wakeup_expire(sc);
    1355             : 
    1356           0 :         if (r & RT2560_ENCRYPTION_DONE)
    1357           0 :                 rt2560_encryption_intr(sc);
    1358             : 
    1359           0 :         if (r & RT2560_TX_DONE)
    1360           0 :                 rt2560_tx_intr(sc);
    1361             : 
    1362           0 :         if (r & RT2560_PRIO_DONE)
    1363           0 :                 rt2560_prio_intr(sc);
    1364             : 
    1365           0 :         if (r & RT2560_DECRYPTION_DONE)
    1366           0 :                 rt2560_decryption_intr(sc);
    1367             : 
    1368           0 :         if (r & RT2560_RX_DONE)
    1369           0 :                 rt2560_rx_intr(sc);
    1370             : 
    1371             :         /* re-enable interrupts */
    1372           0 :         RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
    1373             : 
    1374           0 :         return 1;
    1375           0 : }
    1376             : 
    1377             : /* quickly determine if a given rate is CCK or OFDM */
    1378             : #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
    1379             : 
    1380             : #define RAL_ACK_SIZE    14      /* 10 + 4(FCS) */
    1381             : #define RAL_CTS_SIZE    14      /* 10 + 4(FCS) */
    1382             : 
    1383             : #define RAL_SIFS                10      /* us */
    1384             : 
    1385             : #define RT2560_RXTX_TURNAROUND  10      /* us */
    1386             : 
    1387             : /*
    1388             :  * This function is only used by the Rx radiotap code. It returns the rate at
    1389             :  * which a given frame was received.
    1390             :  */
    1391             : #if NBPFILTER > 0
    1392             : uint8_t
    1393           0 : rt2560_rxrate(const struct rt2560_rx_desc *desc)
    1394             : {
    1395           0 :         if (letoh32(desc->flags) & RT2560_RX_OFDM) {
    1396             :                 /* reverse function of rt2560_plcp_signal */
    1397           0 :                 switch (desc->rate) {
    1398           0 :                 case 0xb:       return 12;
    1399           0 :                 case 0xf:       return 18;
    1400           0 :                 case 0xa:       return 24;
    1401           0 :                 case 0xe:       return 36;
    1402           0 :                 case 0x9:       return 48;
    1403           0 :                 case 0xd:       return 72;
    1404           0 :                 case 0x8:       return 96;
    1405           0 :                 case 0xc:       return 108;
    1406             :                 }
    1407             :         } else {
    1408           0 :                 if (desc->rate == 10)
    1409           0 :                         return 2;
    1410           0 :                 if (desc->rate == 20)
    1411           0 :                         return 4;
    1412           0 :                 if (desc->rate == 55)
    1413           0 :                         return 11;
    1414           0 :                 if (desc->rate == 110)
    1415           0 :                         return 22;
    1416             :         }
    1417           0 :         return 2;       /* should not get there */
    1418           0 : }
    1419             : #endif
    1420             : 
    1421             : /*
    1422             :  * Return the expected ack rate for a frame transmitted at rate `rate'.
    1423             :  */
    1424             : int
    1425           0 : rt2560_ack_rate(struct ieee80211com *ic, int rate)
    1426             : {
    1427           0 :         switch (rate) {
    1428             :         /* CCK rates */
    1429             :         case 2:
    1430           0 :                 return 2;
    1431             :         case 4:
    1432             :         case 11:
    1433             :         case 22:
    1434           0 :                 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
    1435             : 
    1436             :         /* OFDM rates */
    1437             :         case 12:
    1438             :         case 18:
    1439           0 :                 return 12;
    1440             :         case 24:
    1441             :         case 36:
    1442           0 :                 return 24;
    1443             :         case 48:
    1444             :         case 72:
    1445             :         case 96:
    1446             :         case 108:
    1447           0 :                 return 48;
    1448             :         }
    1449             : 
    1450             :         /* default to 1Mbps */
    1451           0 :         return 2;
    1452           0 : }
    1453             : 
    1454             : /*
    1455             :  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
    1456             :  * The function automatically determines the operating mode depending on the
    1457             :  * given rate. `flags' indicates whether short preamble is in use or not.
    1458             :  */
    1459             : uint16_t
    1460           0 : rt2560_txtime(int len, int rate, uint32_t flags)
    1461             : {
    1462             :         uint16_t txtime;
    1463             : 
    1464           0 :         if (RAL_RATE_IS_OFDM(rate)) {
    1465             :                 /* IEEE Std 802.11g-2003, pp. 44 */
    1466           0 :                 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
    1467           0 :                 txtime = 16 + 4 + 4 * txtime + 6;
    1468           0 :         } else {
    1469             :                 /* IEEE Std 802.11b-1999, pp. 28 */
    1470           0 :                 txtime = (16 * len + rate - 1) / rate;
    1471           0 :                 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
    1472           0 :                         txtime +=  72 + 24;
    1473             :                 else
    1474           0 :                         txtime += 144 + 48;
    1475             :         }
    1476           0 :         return txtime;
    1477             : }
    1478             : 
    1479             : uint8_t
    1480           0 : rt2560_plcp_signal(int rate)
    1481             : {
    1482           0 :         switch (rate) {
    1483             :         /* CCK rates (returned values are device-dependent) */
    1484           0 :         case 2:         return 0x0;
    1485           0 :         case 4:         return 0x1;
    1486           0 :         case 11:        return 0x2;
    1487           0 :         case 22:        return 0x3;
    1488             : 
    1489             :         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
    1490           0 :         case 12:        return 0xb;
    1491           0 :         case 18:        return 0xf;
    1492           0 :         case 24:        return 0xa;
    1493           0 :         case 36:        return 0xe;
    1494           0 :         case 48:        return 0x9;
    1495           0 :         case 72:        return 0xd;
    1496           0 :         case 96:        return 0x8;
    1497           0 :         case 108:       return 0xc;
    1498             : 
    1499             :         /* unsupported rates (should not get there) */
    1500           0 :         default:        return 0xff;
    1501             :         }
    1502           0 : }
    1503             : 
    1504             : void
    1505           0 : rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
    1506             :     uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
    1507             : {
    1508           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1509             :         uint16_t plcp_length;
    1510             :         int remainder;
    1511             : 
    1512           0 :         desc->flags = htole32(flags);
    1513           0 :         desc->flags |= htole32(len << 16);
    1514           0 :         desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) :
    1515             :             htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
    1516             : 
    1517           0 :         desc->physaddr = htole32(physaddr);
    1518           0 :         desc->wme = htole16(
    1519             :             RT2560_AIFSN(2) |
    1520             :             RT2560_LOGCWMIN(3) |
    1521             :             RT2560_LOGCWMAX(8));
    1522             : 
    1523             :         /* setup PLCP fields */
    1524           0 :         desc->plcp_signal  = rt2560_plcp_signal(rate);
    1525           0 :         desc->plcp_service = 4;
    1526             : 
    1527           0 :         len += IEEE80211_CRC_LEN;
    1528           0 :         if (RAL_RATE_IS_OFDM(rate)) {
    1529           0 :                 desc->flags |= htole32(RT2560_TX_OFDM);
    1530             : 
    1531           0 :                 plcp_length = len & 0xfff;
    1532           0 :                 desc->plcp_length_hi = plcp_length >> 6;
    1533           0 :                 desc->plcp_length_lo = plcp_length & 0x3f;
    1534           0 :         } else {
    1535           0 :                 plcp_length = (16 * len + rate - 1) / rate;
    1536           0 :                 if (rate == 22) {
    1537           0 :                         remainder = (16 * len) % 22;
    1538           0 :                         if (remainder != 0 && remainder < 7)
    1539           0 :                                 desc->plcp_service |= RT2560_PLCP_LENGEXT;
    1540             :                 }
    1541           0 :                 desc->plcp_length_hi = plcp_length >> 8;
    1542           0 :                 desc->plcp_length_lo = plcp_length & 0xff;
    1543             : 
    1544           0 :                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
    1545           0 :                         desc->plcp_signal |= 0x08;
    1546             :         }
    1547           0 : }
    1548             : 
    1549             : #ifndef IEEE80211_STA_ONLY
    1550             : int
    1551           0 : rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
    1552             :     struct ieee80211_node *ni)
    1553             : {
    1554           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1555             :         struct rt2560_tx_desc *desc;
    1556             :         struct rt2560_tx_data *data;
    1557             :         int rate = 2, error;
    1558             : 
    1559           0 :         desc = &sc->bcnq.desc[sc->bcnq.cur];
    1560           0 :         data = &sc->bcnq.data[sc->bcnq.cur];
    1561             : 
    1562           0 :         error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
    1563             :             BUS_DMA_NOWAIT);
    1564           0 :         if (error != 0) {
    1565           0 :                 printf("%s: can't map mbuf (error %d)\n",
    1566           0 :                     sc->sc_dev.dv_xname, error);
    1567           0 :                 m_freem(m0);
    1568           0 :                 return error;
    1569             :         }
    1570             : 
    1571           0 :         data->m = m0;
    1572           0 :         data->ni = ni;
    1573             : 
    1574           0 :         rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
    1575           0 :             RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0,
    1576           0 :             data->map->dm_segs->ds_addr);
    1577             : 
    1578           0 :         bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
    1579             :             BUS_DMASYNC_PREWRITE);
    1580           0 :         bus_dmamap_sync(sc->sc_dmat, sc->bcnq.map,
    1581             :             sc->bcnq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
    1582             :             BUS_DMASYNC_PREWRITE);
    1583             : 
    1584             :         /*
    1585             :          * Store pointer to ERP Information Element so that we can update it
    1586             :          * dynamically when the slot time changes.
    1587             :          * XXX: this is ugly since it depends on how net80211 builds beacon
    1588             :          * frames but ieee80211_beacon_alloc() don't store offsets for us.
    1589             :          */
    1590           0 :         if (ic->ic_curmode == IEEE80211_MODE_11G) {
    1591           0 :                 sc->erp =
    1592           0 :                     mtod(m0, uint8_t *) +
    1593           0 :                     sizeof (struct ieee80211_frame) +
    1594           0 :                     8 + 2 + 2 +
    1595           0 :                     ((ic->ic_flags & IEEE80211_F_HIDENWID) ?
    1596           0 :                         1 : 2 + ni->ni_esslen) +
    1597           0 :                     2 + min(ni->ni_rates.rs_nrates, IEEE80211_RATE_SIZE) +
    1598           0 :                     2 + 1 +
    1599           0 :                     ((ic->ic_opmode == IEEE80211_M_IBSS) ? 4 : 6) +
    1600             :                     2;
    1601           0 :         }
    1602             : 
    1603           0 :         return 0;
    1604           0 : }
    1605             : #endif
    1606             : 
    1607             : int
    1608           0 : rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
    1609             :     struct ieee80211_node *ni)
    1610             : {
    1611           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1612             :         struct rt2560_tx_desc *desc;
    1613             :         struct rt2560_tx_data *data;
    1614             :         struct ieee80211_frame *wh;
    1615             :         uint16_t dur;
    1616             :         uint32_t flags = 0;
    1617             :         int rate = 2, error;
    1618             : 
    1619           0 :         desc = &sc->prioq.desc[sc->prioq.cur];
    1620           0 :         data = &sc->prioq.data[sc->prioq.cur];
    1621             : 
    1622           0 :         error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
    1623             :             BUS_DMA_NOWAIT);
    1624           0 :         if (error != 0) {
    1625           0 :                 printf("%s: can't map mbuf (error %d)\n",
    1626           0 :                     sc->sc_dev.dv_xname, error);
    1627           0 :                 m_freem(m0);
    1628           0 :                 return error;
    1629             :         }
    1630             : 
    1631             : #if NBPFILTER > 0
    1632           0 :         if (sc->sc_drvbpf != NULL) {
    1633           0 :                 struct mbuf mb;
    1634           0 :                 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
    1635             : 
    1636           0 :                 tap->wt_flags = 0;
    1637           0 :                 tap->wt_rate = rate;
    1638           0 :                 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
    1639           0 :                 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
    1640           0 :                 tap->wt_antenna = sc->tx_ant;
    1641             : 
    1642           0 :                 mb.m_data = (caddr_t)tap;
    1643           0 :                 mb.m_len = sc->sc_txtap_len;
    1644           0 :                 mb.m_next = m0;
    1645           0 :                 mb.m_nextpkt = NULL;
    1646           0 :                 mb.m_type = 0;
    1647           0 :                 mb.m_flags = 0;
    1648           0 :                 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
    1649           0 :         }
    1650             : #endif
    1651             : 
    1652           0 :         data->m = m0;
    1653           0 :         data->ni = ni;
    1654             : 
    1655           0 :         wh = mtod(m0, struct ieee80211_frame *);
    1656             : 
    1657           0 :         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
    1658             :                 flags |= RT2560_TX_NEED_ACK;
    1659             : 
    1660           0 :                 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
    1661             :                     RAL_SIFS;
    1662           0 :                 *(uint16_t *)wh->i_dur = htole16(dur);
    1663             : 
    1664             : #ifndef IEEE80211_STA_ONLY
    1665             :                 /* tell hardware to set timestamp for probe responses */
    1666           0 :                 if ((wh->i_fc[0] &
    1667           0 :                     (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
    1668             :                     (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
    1669           0 :                         flags |= RT2560_TX_TIMESTAMP;
    1670             : #endif
    1671             :         }
    1672             : 
    1673           0 :         rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
    1674           0 :             data->map->dm_segs->ds_addr);
    1675             : 
    1676           0 :         bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
    1677             :             BUS_DMASYNC_PREWRITE);
    1678           0 :         bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
    1679             :             sc->prioq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
    1680             :             BUS_DMASYNC_PREWRITE);
    1681             : 
    1682             :         DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
    1683             :             m0->m_pkthdr.len, sc->prioq.cur, rate));
    1684             : 
    1685             :         /* kick prio */
    1686           0 :         sc->prioq.queued++;
    1687           0 :         sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
    1688           0 :         RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
    1689             : 
    1690           0 :         return 0;
    1691           0 : }
    1692             : 
    1693             : int
    1694           0 : rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
    1695             :     struct ieee80211_node *ni)
    1696             : {
    1697           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1698           0 :         struct rt2560_tx_ring *txq = &sc->txq;
    1699             :         struct rt2560_tx_desc *desc;
    1700             :         struct rt2560_tx_data *data;
    1701             :         struct ieee80211_frame *wh;
    1702             :         struct ieee80211_key *k;
    1703             :         struct mbuf *m1;
    1704             :         uint16_t dur;
    1705             :         uint32_t flags = 0;
    1706             :         int pktlen, rate, needcts = 0, needrts = 0, error;
    1707             : 
    1708           0 :         wh = mtod(m0, struct ieee80211_frame *);
    1709             : 
    1710           0 :         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
    1711           0 :                 k = ieee80211_get_txkey(ic, wh, ni);
    1712             : 
    1713           0 :                 if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
    1714           0 :                         return ENOBUFS;
    1715             : 
    1716             :                 /* packet header may have moved, reset our local pointer */
    1717           0 :                 wh = mtod(m0, struct ieee80211_frame *);
    1718           0 :         }
    1719             : 
    1720             :         /* compute actual packet length (including CRC and crypto overhead) */
    1721           0 :         pktlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
    1722             : 
    1723             :         /* pickup a rate */
    1724           0 :         if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
    1725           0 :             ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    1726             :              IEEE80211_FC0_TYPE_MGT)) {
    1727             :                 /* mgmt/multicast frames are sent at the lowest avail. rate */
    1728           0 :                 rate = ni->ni_rates.rs_rates[0];
    1729           0 :         } else if (ic->ic_fixed_rate != -1) {
    1730           0 :                 rate = ic->ic_sup_rates[ic->ic_curmode].
    1731           0 :                     rs_rates[ic->ic_fixed_rate];
    1732           0 :         } else
    1733           0 :                 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
    1734           0 :         if (rate == 0)
    1735           0 :                 rate = 2;       /* XXX should not happen */
    1736           0 :         rate &= IEEE80211_RATE_VAL;
    1737             : 
    1738             :         /*
    1739             :          * Packet Bursting: backoff after ppb=8 frames to give other STAs a
    1740             :          * chance to contend for the wireless medium.
    1741             :          */
    1742           0 :         if (ic->ic_opmode == IEEE80211_M_STA && (ni->ni_txseq & 7))
    1743           0 :                 flags |= RT2560_TX_IFS_SIFS;
    1744             : 
    1745             :         /* check if RTS/CTS or CTS-to-self protection must be used */
    1746           0 :         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
    1747             :                 /* multicast frames are not sent at OFDM rates in 802.11b/g */
    1748           0 :                 if (pktlen > ic->ic_rtsthreshold) {
    1749             :                         needrts = 1;    /* RTS/CTS based on frame length */
    1750           0 :                 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
    1751           0 :                     RAL_RATE_IS_OFDM(rate)) {
    1752           0 :                         if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
    1753           0 :                                 needcts = 1;    /* CTS-to-self */
    1754           0 :                         else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
    1755           0 :                                 needrts = 1;    /* RTS/CTS */
    1756             :                 }
    1757             :         }
    1758           0 :         if (needrts || needcts) {
    1759             :                 struct mbuf *mprot;
    1760             :                 int protrate, ackrate;
    1761             : 
    1762             :                 protrate = 2;   /* XXX */
    1763           0 :                 ackrate  = rt2560_ack_rate(ic, rate);
    1764             : 
    1765           0 :                 dur = rt2560_txtime(pktlen, rate, ic->ic_flags) +
    1766           0 :                       rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
    1767             :                       2 * RAL_SIFS;
    1768           0 :                 if (needrts) {
    1769           0 :                         dur += rt2560_txtime(RAL_CTS_SIZE, rt2560_ack_rate(ic,
    1770           0 :                             protrate), ic->ic_flags) + RAL_SIFS;
    1771           0 :                         mprot = ieee80211_get_rts(ic, wh, dur);
    1772           0 :                 } else {
    1773           0 :                         mprot = ieee80211_get_cts_to_self(ic, dur);
    1774             :                 }
    1775           0 :                 if (mprot == NULL) {
    1776           0 :                         printf("%s: could not allocate protection frame\n",
    1777           0 :                             sc->sc_dev.dv_xname);
    1778           0 :                         m_freem(m0);
    1779           0 :                         return ENOBUFS;
    1780             :                 }
    1781             : 
    1782           0 :                 desc = &txq->desc[txq->cur_encrypt];
    1783           0 :                 data = &txq->data[txq->cur_encrypt];
    1784             : 
    1785           0 :                 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, mprot,
    1786             :                     BUS_DMA_NOWAIT);
    1787           0 :                 if (error != 0) {
    1788           0 :                         printf("%s: can't map mbuf (error %d)\n",
    1789           0 :                             sc->sc_dev.dv_xname, error);
    1790           0 :                         m_freem(mprot);
    1791           0 :                         m_freem(m0);
    1792           0 :                         return error;
    1793             :                 }
    1794             : 
    1795           0 :                 data->m = mprot;
    1796             :                 /* avoid multiple free() of the same node for each fragment */
    1797           0 :                 data->ni = ieee80211_ref_node(ni);
    1798             : 
    1799             :                 /* XXX may want to pass the protection frame to BPF */
    1800             : 
    1801           0 :                 rt2560_setup_tx_desc(sc, desc,
    1802           0 :                     (needrts ? RT2560_TX_NEED_ACK : 0) | RT2560_TX_MORE_FRAG,
    1803           0 :                     mprot->m_pkthdr.len, protrate, 1,
    1804           0 :                     data->map->dm_segs->ds_addr);
    1805             : 
    1806           0 :                 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    1807             :                     data->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    1808           0 :                 bus_dmamap_sync(sc->sc_dmat, txq->map,
    1809             :                     txq->cur_encrypt * RT2560_TX_DESC_SIZE,
    1810             :                     RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
    1811             : 
    1812           0 :                 txq->queued++;
    1813           0 :                 if (++txq->cur_encrypt >= txq->count)
    1814           0 :                         txq->cur_encrypt = 0;
    1815             : 
    1816             :                 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
    1817           0 :         }
    1818             : 
    1819           0 :         data = &txq->data[txq->cur_encrypt];
    1820           0 :         desc = &txq->desc[txq->cur_encrypt];
    1821             : 
    1822           0 :         error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
    1823             :             BUS_DMA_NOWAIT);
    1824           0 :         if (error != 0 && error != EFBIG) {
    1825           0 :                 printf("%s: can't map mbuf (error %d)\n",
    1826           0 :                     sc->sc_dev.dv_xname, error);
    1827           0 :                 m_freem(m0);
    1828           0 :                 return error;
    1829             :         }
    1830           0 :         if (error != 0) {
    1831             :                 /* too many fragments, linearize */
    1832           0 :                 MGETHDR(m1, M_DONTWAIT, MT_DATA);
    1833           0 :                 if (m1 == NULL) {
    1834           0 :                         m_freem(m0);
    1835           0 :                         return ENOBUFS;
    1836             :                 }
    1837           0 :                 if (m0->m_pkthdr.len > MHLEN) {
    1838           0 :                         MCLGET(m1, M_DONTWAIT);
    1839           0 :                         if (!(m1->m_flags & M_EXT)) {
    1840           0 :                                 m_freem(m0);
    1841           0 :                                 m_freem(m1);
    1842           0 :                                 return ENOBUFS;
    1843             :                         }
    1844             :                 }
    1845           0 :                 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m1, caddr_t));
    1846           0 :                 m1->m_pkthdr.len = m1->m_len = m0->m_pkthdr.len;
    1847           0 :                 m_freem(m0);
    1848             :                 m0 = m1;
    1849             : 
    1850           0 :                 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
    1851             :                     BUS_DMA_NOWAIT);
    1852           0 :                 if (error != 0) {
    1853           0 :                         printf("%s: can't map mbuf (error %d)\n",
    1854           0 :                             sc->sc_dev.dv_xname, error);
    1855           0 :                         m_freem(m0);
    1856           0 :                         return error;
    1857             :                 }
    1858             : 
    1859             :                 /* packet header have moved, reset our local pointer */
    1860           0 :                 wh = mtod(m0, struct ieee80211_frame *);
    1861           0 :         }
    1862             : 
    1863             : #if NBPFILTER > 0
    1864           0 :         if (sc->sc_drvbpf != NULL) {
    1865           0 :                 struct mbuf mb;
    1866           0 :                 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
    1867             : 
    1868           0 :                 tap->wt_flags = 0;
    1869           0 :                 tap->wt_rate = rate;
    1870           0 :                 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
    1871           0 :                 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
    1872           0 :                 tap->wt_antenna = sc->tx_ant;
    1873             : 
    1874           0 :                 mb.m_data = (caddr_t)tap;
    1875           0 :                 mb.m_len = sc->sc_txtap_len;
    1876           0 :                 mb.m_next = m0;
    1877           0 :                 mb.m_nextpkt = NULL;
    1878           0 :                 mb.m_type = 0;
    1879           0 :                 mb.m_flags = 0;
    1880           0 :                 bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
    1881           0 :         }
    1882             : #endif
    1883             : 
    1884           0 :         data->m = m0;
    1885           0 :         data->ni = ni;
    1886             : 
    1887           0 :         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
    1888           0 :                 flags |= RT2560_TX_NEED_ACK;
    1889             : 
    1890           0 :                 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
    1891           0 :                     ic->ic_flags) + RAL_SIFS;
    1892           0 :                 *(uint16_t *)wh->i_dur = htole16(dur);
    1893           0 :         }
    1894             : 
    1895           0 :         rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
    1896           0 :             data->map->dm_segs->ds_addr);
    1897             : 
    1898           0 :         bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
    1899             :             BUS_DMASYNC_PREWRITE);
    1900           0 :         bus_dmamap_sync(sc->sc_dmat, txq->map,
    1901             :             txq->cur_encrypt * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
    1902             :             BUS_DMASYNC_PREWRITE);
    1903             : 
    1904             :         DPRINTFN(10, ("sending frame len=%u idx=%u rate=%u\n",
    1905             :             m0->m_pkthdr.len, txq->cur_encrypt, rate));
    1906             : 
    1907             :         /* kick encrypt */
    1908           0 :         txq->queued++;
    1909           0 :         if (++txq->cur_encrypt >= txq->count)
    1910           0 :                 txq->cur_encrypt = 0;
    1911           0 :         RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
    1912             : 
    1913           0 :         return 0;
    1914           0 : }
    1915             : 
    1916             : void
    1917           0 : rt2560_start(struct ifnet *ifp)
    1918             : {
    1919           0 :         struct rt2560_softc *sc = ifp->if_softc;
    1920           0 :         struct ieee80211com *ic = &sc->sc_ic;
    1921             :         struct mbuf *m0;
    1922           0 :         struct ieee80211_node *ni;
    1923             : 
    1924             :         /*
    1925             :          * net80211 may still try to send management frames even if the
    1926             :          * IFF_RUNNING flag is not set...
    1927             :          */
    1928           0 :         if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd))
    1929           0 :                 return;
    1930             : 
    1931           0 :         for (;;) {
    1932           0 :                 if (mq_len(&ic->ic_mgtq) > 0) {
    1933           0 :                         if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
    1934           0 :                                 ifq_set_oactive(&ifp->if_snd);
    1935           0 :                                 sc->sc_flags |= RT2560_PRIO_OACTIVE;
    1936           0 :                                 break;
    1937             :                         }
    1938             : 
    1939           0 :                         m0 = mq_dequeue(&ic->ic_mgtq);
    1940           0 :                         if (m0 == NULL)
    1941           0 :                                 continue;
    1942           0 :                         ni = m0->m_pkthdr.ph_cookie;
    1943             : #if NBPFILTER > 0
    1944           0 :                         if (ic->ic_rawbpf != NULL)
    1945           0 :                                 bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
    1946             : #endif
    1947           0 :                         if (rt2560_tx_mgt(sc, m0, ni) != 0)
    1948             :                                 break;
    1949             : 
    1950             :                 } else {
    1951             :                         /* Because RTS/CTS requires an extra frame we need
    1952             :                          * space for 2 frames on the regular Tx queue. */
    1953           0 :                         if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
    1954           0 :                                 ifq_set_oactive(&ifp->if_snd);
    1955           0 :                                 sc->sc_flags |= RT2560_DATA_OACTIVE;
    1956           0 :                                 break;
    1957             :                         }
    1958             : 
    1959           0 :                         if (ic->ic_state != IEEE80211_S_RUN)
    1960             :                                 break;
    1961             : 
    1962           0 :                         IFQ_DEQUEUE(&ifp->if_snd, m0);
    1963           0 :                         if (m0 == NULL)
    1964             :                                 break;
    1965             : #if NBPFILTER > 0
    1966           0 :                         if (ifp->if_bpf != NULL)
    1967           0 :                                 bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
    1968             : #endif
    1969           0 :                         m0 = ieee80211_encap(ifp, m0, &ni);
    1970           0 :                         if (m0 == NULL)
    1971           0 :                                 continue;
    1972             : #if NBPFILTER > 0
    1973           0 :                         if (ic->ic_rawbpf != NULL)
    1974           0 :                                 bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
    1975             : #endif
    1976           0 :                         if (rt2560_tx_data(sc, m0, ni) != 0) {
    1977           0 :                                 if (ni != NULL)
    1978           0 :                                         ieee80211_release_node(ic, ni);
    1979           0 :                                 ifp->if_oerrors++;
    1980           0 :                                 break;
    1981             :                         }
    1982             :                 }
    1983             : 
    1984           0 :                 sc->sc_tx_timer = 5;
    1985           0 :                 ifp->if_timer = 1;
    1986             :         }
    1987           0 : }
    1988             : 
    1989             : void
    1990           0 : rt2560_watchdog(struct ifnet *ifp)
    1991             : {
    1992           0 :         struct rt2560_softc *sc = ifp->if_softc;
    1993             : 
    1994           0 :         ifp->if_timer = 0;
    1995             : 
    1996           0 :         if (sc->sc_tx_timer > 0) {
    1997           0 :                 if (--sc->sc_tx_timer == 0) {
    1998           0 :                         printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    1999           0 :                         rt2560_init(ifp);
    2000           0 :                         ifp->if_oerrors++;
    2001           0 :                         return;
    2002             :                 }
    2003           0 :                 ifp->if_timer = 1;
    2004           0 :         }
    2005             : 
    2006           0 :         ieee80211_watchdog(ifp);
    2007           0 : }
    2008             : 
    2009             : int
    2010           0 : rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    2011             : {
    2012           0 :         struct rt2560_softc *sc = ifp->if_softc;
    2013           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2014             :         int s, error = 0;
    2015             : 
    2016           0 :         s = splnet();
    2017             : 
    2018           0 :         switch (cmd) {
    2019             :         case SIOCSIFADDR:
    2020           0 :                 ifp->if_flags |= IFF_UP;
    2021             :                 /* FALLTHROUGH */
    2022             :         case SIOCSIFFLAGS:
    2023           0 :                 if (ifp->if_flags & IFF_UP) {
    2024           0 :                         if (ifp->if_flags & IFF_RUNNING)
    2025           0 :                                 rt2560_update_promisc(sc);
    2026             :                         else
    2027           0 :                                 rt2560_init(ifp);
    2028             :                 } else {
    2029           0 :                         if (ifp->if_flags & IFF_RUNNING)
    2030           0 :                                 rt2560_stop(ifp, 1);
    2031             :                 }
    2032             :                 break;
    2033             : 
    2034             :         case SIOCS80211CHANNEL:
    2035             :                 /*
    2036             :                  * This allows for fast channel switching in monitor mode
    2037             :                  * (used by kismet). In IBSS mode, we must explicitly reset
    2038             :                  * the interface to generate a new beacon frame.
    2039             :                  */
    2040           0 :                 error = ieee80211_ioctl(ifp, cmd, data);
    2041           0 :                 if (error == ENETRESET &&
    2042           0 :                     ic->ic_opmode == IEEE80211_M_MONITOR) {
    2043           0 :                         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
    2044             :                             (IFF_UP | IFF_RUNNING))
    2045           0 :                                 rt2560_set_chan(sc, ic->ic_ibss_chan);
    2046             :                         error = 0;
    2047           0 :                 }
    2048             :                 break;
    2049             : 
    2050             :         default:
    2051           0 :                 error = ieee80211_ioctl(ifp, cmd, data);
    2052           0 :         }
    2053             : 
    2054           0 :         if (error == ENETRESET) {
    2055           0 :                 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
    2056             :                     (IFF_UP | IFF_RUNNING))
    2057           0 :                         rt2560_init(ifp);
    2058             :                 error = 0;
    2059           0 :         }
    2060             : 
    2061           0 :         splx(s);
    2062             : 
    2063           0 :         return error;
    2064             : }
    2065             : 
    2066             : void
    2067           0 : rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
    2068             : {
    2069             :         uint32_t tmp;
    2070             :         int ntries;
    2071             : 
    2072           0 :         for (ntries = 0; ntries < 100; ntries++) {
    2073           0 :                 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
    2074             :                         break;
    2075           0 :                 DELAY(1);
    2076             :         }
    2077           0 :         if (ntries == 100) {
    2078           0 :                 printf("%s: could not write to BBP\n", sc->sc_dev.dv_xname);
    2079           0 :                 return;
    2080             :         }
    2081             : 
    2082           0 :         tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
    2083           0 :         RAL_WRITE(sc, RT2560_BBPCSR, tmp);
    2084             : 
    2085             :         DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
    2086           0 : }
    2087             : 
    2088             : uint8_t
    2089           0 : rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
    2090             : {
    2091             :         uint32_t val;
    2092             :         int ntries;
    2093             : 
    2094           0 :         for (ntries = 0; ntries < 100; ntries++) {
    2095           0 :                 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
    2096             :                         break;
    2097           0 :                 DELAY(1);
    2098             :         }
    2099           0 :         if (ntries == 100) {
    2100           0 :                 printf("%s: could not read from BBP\n", sc->sc_dev.dv_xname);
    2101           0 :                 return 0;
    2102             :         }
    2103             : 
    2104           0 :         val = RT2560_BBP_BUSY | reg << 8;
    2105           0 :         RAL_WRITE(sc, RT2560_BBPCSR, val);
    2106             : 
    2107           0 :         for (ntries = 0; ntries < 100; ntries++) {
    2108           0 :                 val = RAL_READ(sc, RT2560_BBPCSR);
    2109           0 :                 if (!(val & RT2560_BBP_BUSY))
    2110           0 :                         return val & 0xff;
    2111           0 :                 DELAY(1);
    2112             :         }
    2113             : 
    2114           0 :         printf("%s: could not read from BBP\n", sc->sc_dev.dv_xname);
    2115           0 :         return 0;
    2116           0 : }
    2117             : 
    2118             : void
    2119           0 : rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
    2120             : {
    2121             :         uint32_t tmp;
    2122             :         int ntries;
    2123             : 
    2124           0 :         for (ntries = 0; ntries < 100; ntries++) {
    2125           0 :                 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
    2126             :                         break;
    2127           0 :                 DELAY(1);
    2128             :         }
    2129           0 :         if (ntries == 100) {
    2130           0 :                 printf("%s: could not write to RF\n", sc->sc_dev.dv_xname);
    2131           0 :                 return;
    2132             :         }
    2133             : 
    2134           0 :         tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
    2135           0 :             (reg & 0x3);
    2136           0 :         RAL_WRITE(sc, RT2560_RFCSR, tmp);
    2137             : 
    2138             :         /* remember last written value in sc */
    2139           0 :         sc->rf_regs[reg] = val;
    2140             : 
    2141             :         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
    2142           0 : }
    2143             : 
    2144             : void
    2145           0 : rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
    2146             : {
    2147           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2148             :         uint8_t power, tmp;
    2149             :         u_int chan;
    2150             : 
    2151           0 :         chan = ieee80211_chan2ieee(ic, c);
    2152           0 :         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
    2153           0 :                 return;
    2154             : 
    2155           0 :         power = min(sc->txpow[chan - 1], 31);
    2156             : 
    2157             :         DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
    2158             : 
    2159           0 :         switch (sc->rf_rev) {
    2160             :         case RT2560_RF_2522:
    2161           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x00814);
    2162           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2522_r2[chan - 1]);
    2163           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
    2164           0 :                 break;
    2165             : 
    2166             :         case RT2560_RF_2523:
    2167           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x08804);
    2168           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2523_r2[chan - 1]);
    2169           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x38044);
    2170           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2171           0 :                     (chan == 14) ? 0x00280 : 0x00286);
    2172           0 :                 break;
    2173             : 
    2174             :         case RT2560_RF_2524:
    2175           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x0c808);
    2176           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2524_r2[chan - 1]);
    2177           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
    2178           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2179           0 :                     (chan == 14) ? 0x00280 : 0x00286);
    2180           0 :                 break;
    2181             : 
    2182             :         case RT2560_RF_2525:
    2183           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
    2184           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_hi_r2[chan - 1]);
    2185           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
    2186           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2187           0 :                     (chan == 14) ? 0x00280 : 0x00286);
    2188             : 
    2189           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
    2190           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_r2[chan - 1]);
    2191           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
    2192           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2193             :                     (chan == 14) ? 0x00280 : 0x00286);
    2194           0 :                 break;
    2195             : 
    2196             :         case RT2560_RF_2525E:
    2197           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x08808);
    2198           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525e_r2[chan - 1]);
    2199           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
    2200           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2201           0 :                     (chan == 14) ? 0x00286 : 0x00282);
    2202           0 :                 break;
    2203             : 
    2204             :         case RT2560_RF_2526:
    2205           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_hi_r2[chan - 1]);
    2206           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2207           0 :                    (chan & 1) ? 0x00386 : 0x00381);
    2208           0 :                 rt2560_rf_write(sc, RT2560_RF1, 0x08804);
    2209             : 
    2210           0 :                 rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_r2[chan - 1]);
    2211           0 :                 rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
    2212           0 :                 rt2560_rf_write(sc, RT2560_RF4,
    2213             :                     (chan & 1) ? 0x00386 : 0x00381);
    2214           0 :                 break;
    2215             :         }
    2216             : 
    2217           0 :         if (ic->ic_opmode != IEEE80211_M_MONITOR &&
    2218           0 :             ic->ic_state != IEEE80211_S_SCAN) {
    2219             :                 /* set Japan filter bit for channel 14 */
    2220           0 :                 tmp = rt2560_bbp_read(sc, 70);
    2221             : 
    2222           0 :                 tmp &= ~RT2560_JAPAN_FILTER;
    2223           0 :                 if (chan == 14)
    2224           0 :                         tmp |= RT2560_JAPAN_FILTER;
    2225             : 
    2226           0 :                 rt2560_bbp_write(sc, 70, tmp);
    2227             : 
    2228           0 :                 DELAY(1000); /* RF needs a 1ms delay here */
    2229           0 :                 rt2560_disable_rf_tune(sc);
    2230             : 
    2231             :                 /* clear CRC errors */
    2232           0 :                 RAL_READ(sc, RT2560_CNT0);
    2233           0 :         }
    2234           0 : }
    2235             : 
    2236             : /*
    2237             :  * Disable RF auto-tuning.
    2238             :  */
    2239             : void
    2240           0 : rt2560_disable_rf_tune(struct rt2560_softc *sc)
    2241             : {
    2242             :         uint32_t tmp;
    2243             : 
    2244           0 :         if (sc->rf_rev != RT2560_RF_2523) {
    2245           0 :                 tmp = sc->rf_regs[RT2560_RF1] & ~RT2560_RF1_AUTOTUNE;
    2246           0 :                 rt2560_rf_write(sc, RT2560_RF1, tmp);
    2247           0 :         }
    2248             : 
    2249           0 :         tmp = sc->rf_regs[RT2560_RF3] & ~RT2560_RF3_AUTOTUNE;
    2250           0 :         rt2560_rf_write(sc, RT2560_RF3, tmp);
    2251             : 
    2252             :         DPRINTFN(2, ("disabling RF autotune\n"));
    2253           0 : }
    2254             : 
    2255             : /*
    2256             :  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
    2257             :  * synchronization.
    2258             :  */
    2259             : void
    2260           0 : rt2560_enable_tsf_sync(struct rt2560_softc *sc)
    2261             : {
    2262           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2263             :         uint16_t logcwmin, preload;
    2264             :         uint32_t tmp;
    2265             : 
    2266             :         /* first, disable TSF synchronization */
    2267           0 :         RAL_WRITE(sc, RT2560_CSR14, 0);
    2268             : 
    2269           0 :         tmp = 16 * ic->ic_bss->ni_intval;
    2270           0 :         RAL_WRITE(sc, RT2560_CSR12, tmp);
    2271             : 
    2272           0 :         RAL_WRITE(sc, RT2560_CSR13, 0);
    2273             : 
    2274             :         logcwmin = 5;
    2275           0 :         preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
    2276           0 :         tmp = logcwmin << 16 | preload;
    2277           0 :         RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
    2278             : 
    2279             :         /* finally, enable TSF synchronization */
    2280             :         tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
    2281           0 :         if (ic->ic_opmode == IEEE80211_M_STA)
    2282           0 :                 tmp |= RT2560_ENABLE_TSF_SYNC(1);
    2283             : #ifndef IEEE80211_STA_ONLY
    2284             :         else
    2285             :                 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
    2286             :                        RT2560_ENABLE_BEACON_GENERATOR;
    2287             : #endif
    2288           0 :         RAL_WRITE(sc, RT2560_CSR14, tmp);
    2289             : 
    2290             :         DPRINTF(("enabling TSF synchronization\n"));
    2291           0 : }
    2292             : 
    2293             : void
    2294           0 : rt2560_update_plcp(struct rt2560_softc *sc)
    2295             : {
    2296           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2297             : 
    2298             :         /* no short preamble for 1Mbps */
    2299           0 :         RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
    2300             : 
    2301           0 :         if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
    2302             :                 /* values taken from the reference driver */
    2303           0 :                 RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
    2304           0 :                 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
    2305           0 :                 RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
    2306           0 :         } else {
    2307             :                 /* same values as above or'ed 0x8 */
    2308           0 :                 RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
    2309           0 :                 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
    2310           0 :                 RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
    2311             :         }
    2312             : 
    2313             :         DPRINTF(("updating PLCP for %s preamble\n",
    2314             :             (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
    2315           0 : }
    2316             : 
    2317             : void
    2318           0 : rt2560_updateslot(struct ieee80211com *ic)
    2319             : {
    2320           0 :         struct rt2560_softc *sc = ic->ic_if.if_softc;
    2321             : 
    2322             : #ifndef IEEE80211_STA_ONLY
    2323           0 :         if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    2324             :                 /*
    2325             :                  * In HostAP mode, we defer setting of new slot time until
    2326             :                  * updated ERP Information Element has propagated to all
    2327             :                  * associated STAs.
    2328             :                  */
    2329           0 :                 sc->sc_flags |= RT2560_UPDATE_SLOT;
    2330           0 :         } else
    2331             : #endif
    2332           0 :                 rt2560_set_slottime(sc);
    2333           0 : }
    2334             : 
    2335             : /*
    2336             :  * IEEE 802.11a (and possibly 802.11g) use short slot time. Refer to
    2337             :  * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
    2338             :  */
    2339             : void
    2340           0 : rt2560_set_slottime(struct rt2560_softc *sc)
    2341             : {
    2342           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2343             :         uint8_t slottime;
    2344             :         uint16_t sifs, pifs, difs, eifs;
    2345             :         uint32_t tmp;
    2346             : 
    2347           0 :         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ?
    2348             :             IEEE80211_DUR_DS_SHSLOT : IEEE80211_DUR_DS_SLOT;
    2349             : 
    2350             :         /* define the MAC slot boundaries */
    2351             :         sifs = RAL_SIFS - RT2560_RXTX_TURNAROUND;
    2352           0 :         pifs = sifs + slottime;
    2353           0 :         difs = sifs + 2 * slottime;
    2354           0 :         eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
    2355             : 
    2356           0 :         tmp = RAL_READ(sc, RT2560_CSR11);
    2357           0 :         tmp = (tmp & ~0x1f00) | slottime << 8;
    2358           0 :         RAL_WRITE(sc, RT2560_CSR11, tmp);
    2359             : 
    2360           0 :         tmp = pifs << 16 | sifs;
    2361           0 :         RAL_WRITE(sc, RT2560_CSR18, tmp);
    2362             : 
    2363           0 :         tmp = eifs << 16 | difs;
    2364           0 :         RAL_WRITE(sc, RT2560_CSR19, tmp);
    2365             : 
    2366             :         DPRINTF(("setting slottime to %uus\n", slottime));
    2367           0 : }
    2368             : 
    2369             : void
    2370           0 : rt2560_set_basicrates(struct rt2560_softc *sc)
    2371             : {
    2372           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2373             : 
    2374             :         /* update basic rate set */
    2375           0 :         if (ic->ic_curmode == IEEE80211_MODE_11B) {
    2376             :                 /* 11b basic rates: 1, 2Mbps */
    2377           0 :                 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
    2378           0 :         } else {
    2379             :                 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
    2380           0 :                 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0xf);
    2381             :         }
    2382           0 : }
    2383             : 
    2384             : void
    2385           0 : rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
    2386             : {
    2387             :         uint32_t tmp;
    2388             : 
    2389             :         /* set ON period to 70ms and OFF period to 30ms */
    2390           0 :         tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
    2391           0 :         RAL_WRITE(sc, RT2560_LEDCSR, tmp);
    2392           0 : }
    2393             : 
    2394             : void
    2395           0 : rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
    2396             : {
    2397             :         uint32_t tmp;
    2398             : 
    2399           0 :         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
    2400           0 :         RAL_WRITE(sc, RT2560_CSR5, tmp);
    2401             : 
    2402           0 :         tmp = bssid[4] | bssid[5] << 8;
    2403           0 :         RAL_WRITE(sc, RT2560_CSR6, tmp);
    2404             : 
    2405             :         DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
    2406           0 : }
    2407             : 
    2408             : void
    2409           0 : rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
    2410             : {
    2411             :         uint32_t tmp;
    2412             : 
    2413           0 :         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
    2414           0 :         RAL_WRITE(sc, RT2560_CSR3, tmp);
    2415             : 
    2416           0 :         tmp = addr[4] | addr[5] << 8;
    2417           0 :         RAL_WRITE(sc, RT2560_CSR4, tmp);
    2418             : 
    2419             :         DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
    2420           0 : }
    2421             : 
    2422             : void
    2423           0 : rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
    2424             : {
    2425             :         uint32_t tmp;
    2426             : 
    2427           0 :         tmp = RAL_READ(sc, RT2560_CSR3);
    2428           0 :         addr[0] = tmp & 0xff;
    2429           0 :         addr[1] = (tmp >>  8) & 0xff;
    2430           0 :         addr[2] = (tmp >> 16) & 0xff;
    2431           0 :         addr[3] = (tmp >> 24);
    2432             : 
    2433           0 :         tmp = RAL_READ(sc, RT2560_CSR4);
    2434           0 :         addr[4] = tmp & 0xff;
    2435           0 :         addr[5] = (tmp >> 8) & 0xff;
    2436           0 : }
    2437             : 
    2438             : void
    2439           0 : rt2560_update_promisc(struct rt2560_softc *sc)
    2440             : {
    2441           0 :         struct ifnet *ifp = &sc->sc_ic.ic_if;
    2442             :         uint32_t tmp;
    2443             : 
    2444           0 :         tmp = RAL_READ(sc, RT2560_RXCSR0);
    2445             : 
    2446           0 :         tmp &= ~RT2560_DROP_NOT_TO_ME;
    2447           0 :         if (!(ifp->if_flags & IFF_PROMISC))
    2448           0 :                 tmp |= RT2560_DROP_NOT_TO_ME;
    2449             : 
    2450           0 :         RAL_WRITE(sc, RT2560_RXCSR0, tmp);
    2451             : 
    2452             :         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
    2453             :             "entering" : "leaving"));
    2454           0 : }
    2455             : 
    2456             : void
    2457           0 : rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
    2458             : {
    2459             :         uint32_t tmp;
    2460             :         uint8_t tx;
    2461             : 
    2462           0 :         tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
    2463           0 :         if (antenna == 1)
    2464           0 :                 tx |= RT2560_BBP_ANTA;
    2465           0 :         else if (antenna == 2)
    2466           0 :                 tx |= RT2560_BBP_ANTB;
    2467             :         else
    2468           0 :                 tx |= RT2560_BBP_DIVERSITY;
    2469             : 
    2470             :         /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
    2471           0 :         if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
    2472           0 :             sc->rf_rev == RT2560_RF_5222)
    2473           0 :                 tx |= RT2560_BBP_FLIPIQ;
    2474             : 
    2475           0 :         rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
    2476             : 
    2477             :         /* update values for CCK and OFDM in BBPCSR1 */
    2478           0 :         tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
    2479           0 :         tmp |= (tx & 0x7) << 16 | (tx & 0x7);
    2480           0 :         RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
    2481           0 : }
    2482             : 
    2483             : void
    2484           0 : rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
    2485             : {
    2486             :         uint8_t rx;
    2487             : 
    2488           0 :         rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
    2489           0 :         if (antenna == 1)
    2490           0 :                 rx |= RT2560_BBP_ANTA;
    2491           0 :         else if (antenna == 2)
    2492           0 :                 rx |= RT2560_BBP_ANTB;
    2493             :         else
    2494           0 :                 rx |= RT2560_BBP_DIVERSITY;
    2495             : 
    2496             :         /* need to force no I/Q flip for RF 2525e and 2526 */
    2497           0 :         if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
    2498           0 :                 rx &= ~RT2560_BBP_FLIPIQ;
    2499             : 
    2500           0 :         rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
    2501           0 : }
    2502             : 
    2503             : const char *
    2504           0 : rt2560_get_rf(int rev)
    2505             : {
    2506           0 :         switch (rev) {
    2507           0 :         case RT2560_RF_2522:    return "RT2522";
    2508           0 :         case RT2560_RF_2523:    return "RT2523";
    2509           0 :         case RT2560_RF_2524:    return "RT2524";
    2510           0 :         case RT2560_RF_2525:    return "RT2525";
    2511           0 :         case RT2560_RF_2525E:   return "RT2525e";
    2512           0 :         case RT2560_RF_2526:    return "RT2526";
    2513           0 :         case RT2560_RF_5222:    return "RT5222";
    2514           0 :         default:                return "unknown";
    2515             :         }
    2516           0 : }
    2517             : 
    2518             : void
    2519           0 : rt2560_read_eeprom(struct rt2560_softc *sc)
    2520             : {
    2521             :         uint16_t val;
    2522             :         int i;
    2523             : 
    2524           0 :         val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
    2525           0 :         sc->rf_rev =   (val >> 11) & 0x1f;
    2526           0 :         sc->hw_radio = (val >> 10) & 0x1;
    2527           0 :         sc->led_mode = (val >> 6)  & 0x7;
    2528           0 :         sc->rx_ant =   (val >> 4)  & 0x3;
    2529           0 :         sc->tx_ant =   (val >> 2)  & 0x3;
    2530           0 :         sc->nb_ant =   val & 0x3;
    2531             : 
    2532             :         /* read default values for BBP registers */
    2533           0 :         for (i = 0; i < 16; i++) {
    2534           0 :                 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
    2535           0 :                 sc->bbp_prom[i].reg = val >> 8;
    2536           0 :                 sc->bbp_prom[i].val = val & 0xff;
    2537             :         }
    2538             : 
    2539             :         /* read Tx power for all b/g channels */
    2540           0 :         for (i = 0; i < 14 / 2; i++) {
    2541           0 :                 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
    2542           0 :                 sc->txpow[i * 2] = val >> 8;
    2543           0 :                 sc->txpow[i * 2 + 1] = val & 0xff;
    2544             :         }
    2545           0 : }
    2546             : 
    2547             : int
    2548           0 : rt2560_bbp_init(struct rt2560_softc *sc)
    2549             : {
    2550             :         int i, ntries;
    2551             : 
    2552             :         /* wait for BBP to be ready */
    2553           0 :         for (ntries = 0; ntries < 100; ntries++) {
    2554           0 :                 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
    2555             :                         break;
    2556           0 :                 DELAY(1);
    2557             :         }
    2558           0 :         if (ntries == 100) {
    2559           0 :                 printf("%s: timeout waiting for BBP\n", sc->sc_dev.dv_xname);
    2560           0 :                 return EIO;
    2561             :         }
    2562             : 
    2563             :         /* initialize BBP registers to default values */
    2564           0 :         for (i = 0; i < nitems(rt2560_def_bbp); i++) {
    2565           0 :                 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
    2566           0 :                     rt2560_def_bbp[i].val);
    2567             :         }
    2568             : #if 0
    2569             :         /* initialize BBP registers to values stored in EEPROM */
    2570             :         for (i = 0; i < 16; i++) {
    2571             :                 if (sc->bbp_prom[i].reg == 0xff)
    2572             :                         continue;
    2573             :                 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
    2574             :         }
    2575             : #endif
    2576             : 
    2577           0 :         return 0;
    2578           0 : }
    2579             : 
    2580             : int
    2581           0 : rt2560_init(struct ifnet *ifp)
    2582             : {
    2583           0 :         struct rt2560_softc *sc = ifp->if_softc;
    2584           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2585             :         uint32_t tmp;
    2586             :         int i;
    2587             : 
    2588             :         /* for CardBus, power on the socket */
    2589           0 :         if (!(sc->sc_flags & RT2560_ENABLED)) {
    2590           0 :                 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
    2591           0 :                         printf("%s: could not enable device\n",
    2592           0 :                             sc->sc_dev.dv_xname);
    2593           0 :                         return EIO;
    2594             :                 }
    2595           0 :                 sc->sc_flags |= RT2560_ENABLED;
    2596           0 :         }
    2597             : 
    2598           0 :         rt2560_stop(ifp, 0);
    2599             : 
    2600             :         /* setup tx rings */
    2601             :         tmp = RT2560_PRIO_RING_COUNT << 24 |
    2602             :               RT2560_ATIM_RING_COUNT << 16 |
    2603             :               RT2560_TX_RING_COUNT   <<  8 |
    2604             :               RT2560_TX_DESC_SIZE;
    2605             : 
    2606             :         /* rings _must_ be initialized in this _exact_ order! */
    2607           0 :         RAL_WRITE(sc, RT2560_TXCSR2, tmp);
    2608           0 :         RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
    2609           0 :         RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
    2610           0 :         RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
    2611           0 :         RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
    2612             : 
    2613             :         /* setup rx ring */
    2614             :         tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
    2615             : 
    2616           0 :         RAL_WRITE(sc, RT2560_RXCSR1, tmp);
    2617           0 :         RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
    2618             : 
    2619             :         /* initialize MAC registers to default values */
    2620           0 :         for (i = 0; i < nitems(rt2560_def_mac); i++)
    2621           0 :                 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
    2622             : 
    2623           0 :         IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
    2624           0 :         rt2560_set_macaddr(sc, ic->ic_myaddr);
    2625             : 
    2626             :         /* set basic rate set (will be updated later) */
    2627           0 :         RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
    2628             : 
    2629           0 :         rt2560_set_slottime(sc);
    2630           0 :         rt2560_update_plcp(sc);
    2631           0 :         rt2560_update_led(sc, 0, 0);
    2632             : 
    2633           0 :         RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
    2634           0 :         RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
    2635             : 
    2636           0 :         if (rt2560_bbp_init(sc) != 0) {
    2637           0 :                 rt2560_stop(ifp, 1);
    2638           0 :                 return EIO;
    2639             :         }
    2640             : 
    2641           0 :         rt2560_set_txantenna(sc, 1);
    2642           0 :         rt2560_set_rxantenna(sc, 1);
    2643             : 
    2644             :         /* set default BSS channel */
    2645           0 :         ic->ic_bss->ni_chan = ic->ic_ibss_chan;
    2646           0 :         rt2560_set_chan(sc, ic->ic_bss->ni_chan);
    2647             : 
    2648             :         /* kick Rx */
    2649             :         tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
    2650           0 :         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
    2651             :                 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
    2652             : #ifndef IEEE80211_STA_ONLY
    2653           0 :                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
    2654             : #endif
    2655           0 :                         tmp |= RT2560_DROP_TODS;
    2656           0 :                 if (!(ifp->if_flags & IFF_PROMISC))
    2657           0 :                         tmp |= RT2560_DROP_NOT_TO_ME;
    2658             :         }
    2659           0 :         RAL_WRITE(sc, RT2560_RXCSR0, tmp);
    2660             : 
    2661             :         /* clear old FCS and Rx FIFO errors */
    2662           0 :         RAL_READ(sc, RT2560_CNT0);
    2663           0 :         RAL_READ(sc, RT2560_CNT4);
    2664             : 
    2665             :         /* clear any pending interrupts */
    2666           0 :         RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
    2667             : 
    2668             :         /* enable interrupts */
    2669           0 :         RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
    2670             : 
    2671           0 :         ifp->if_flags |= IFF_RUNNING;
    2672           0 :         ifq_clr_oactive(&ifp->if_snd);
    2673             : 
    2674           0 :         if (ic->ic_opmode == IEEE80211_M_MONITOR)
    2675           0 :                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    2676             :         else
    2677           0 :                 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    2678             : 
    2679           0 :         return 0;
    2680           0 : }
    2681             : 
    2682             : void
    2683           0 : rt2560_stop(struct ifnet *ifp, int disable)
    2684             : {
    2685           0 :         struct rt2560_softc *sc = ifp->if_softc;
    2686           0 :         struct ieee80211com *ic = &sc->sc_ic;
    2687             : 
    2688           0 :         sc->sc_tx_timer = 0;
    2689           0 :         sc->sc_flags &= ~(RT2560_PRIO_OACTIVE|RT2560_DATA_OACTIVE);
    2690           0 :         ifp->if_timer = 0;
    2691           0 :         ifp->if_flags &= ~IFF_RUNNING;
    2692           0 :         ifq_clr_oactive(&ifp->if_snd);
    2693             : 
    2694           0 :         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);  /* free all nodes */
    2695             : 
    2696             :         /* abort Tx */
    2697           0 :         RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
    2698             : 
    2699             :         /* disable Rx */
    2700           0 :         RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
    2701             : 
    2702             :         /* reset ASIC (and thus, BBP) */
    2703           0 :         RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
    2704           0 :         RAL_WRITE(sc, RT2560_CSR1, 0);
    2705             : 
    2706             :         /* disable interrupts */
    2707           0 :         RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
    2708             : 
    2709             :         /* clear any pending interrupt */
    2710           0 :         RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
    2711             : 
    2712             :         /* reset Tx and Rx rings */
    2713           0 :         rt2560_reset_tx_ring(sc, &sc->txq);
    2714           0 :         rt2560_reset_tx_ring(sc, &sc->atimq);
    2715           0 :         rt2560_reset_tx_ring(sc, &sc->prioq);
    2716           0 :         rt2560_reset_tx_ring(sc, &sc->bcnq);
    2717           0 :         rt2560_reset_rx_ring(sc, &sc->rxq);
    2718             : 
    2719             :         /* for CardBus, power down the socket */
    2720           0 :         if (disable && sc->sc_disable != NULL) {
    2721           0 :                 if (sc->sc_flags & RT2560_ENABLED) {
    2722           0 :                         (*sc->sc_disable)(sc);
    2723           0 :                         sc->sc_flags &= ~RT2560_ENABLED;
    2724           0 :                 }
    2725             :         }
    2726           0 : }
    2727             : 
    2728             : struct cfdriver ral_cd = {
    2729             :         NULL, "ral", DV_IFNET
    2730             : };

Generated by: LCOV version 1.13