The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ral/rt2560.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: f6e29e2dfd662d44b4032c75b15f00b0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.