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


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

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

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

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

Cache object: 50bd58b361bf99c186354efa30446fa1


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