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

Cache object: 983d03dbbd2afb850ad627ee4ca5c371


[ 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.