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/pci/if_rl.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 /*-
    2  * Copyright (c) 1997, 1998
    3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by Bill Paul.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/7.4/sys/pci/if_rl.c 215368 2010-11-16 04:40:03Z sobomax $");
   35 
   36 /*
   37  * RealTek 8129/8139 PCI NIC driver
   38  *
   39  * Supports several extremely cheap PCI 10/100 adapters based on
   40  * the RealTek chipset. Datasheets can be obtained from
   41  * www.realtek.com.tw.
   42  *
   43  * Written by Bill Paul <wpaul@ctr.columbia.edu>
   44  * Electrical Engineering Department
   45  * Columbia University, New York City
   46  */
   47 /*
   48  * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
   49  * probably the worst PCI ethernet controller ever made, with the possible
   50  * exception of the FEAST chip made by SMC. The 8139 supports bus-master
   51  * DMA, but it has a terrible interface that nullifies any performance
   52  * gains that bus-master DMA usually offers.
   53  *
   54  * For transmission, the chip offers a series of four TX descriptor
   55  * registers. Each transmit frame must be in a contiguous buffer, aligned
   56  * on a longword (32-bit) boundary. This means we almost always have to
   57  * do mbuf copies in order to transmit a frame, except in the unlikely
   58  * case where a) the packet fits into a single mbuf, and b) the packet
   59  * is 32-bit aligned within the mbuf's data area. The presence of only
   60  * four descriptor registers means that we can never have more than four
   61  * packets queued for transmission at any one time.
   62  *
   63  * Reception is not much better. The driver has to allocate a single large
   64  * buffer area (up to 64K in size) into which the chip will DMA received
   65  * frames. Because we don't know where within this region received packets
   66  * will begin or end, we have no choice but to copy data from the buffer
   67  * area into mbufs in order to pass the packets up to the higher protocol
   68  * levels.
   69  *
   70  * It's impossible given this rotten design to really achieve decent
   71  * performance at 100Mbps, unless you happen to have a 400Mhz PII or
   72  * some equally overmuscled CPU to drive it.
   73  *
   74  * On the bright side, the 8139 does have a built-in PHY, although
   75  * rather than using an MDIO serial interface like most other NICs, the
   76  * PHY registers are directly accessible through the 8139's register
   77  * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
   78  * filter.
   79  *
   80  * The 8129 chip is an older version of the 8139 that uses an external PHY
   81  * chip. The 8129 has a serial MDIO interface for accessing the MII where
   82  * the 8139 lets you directly access the on-board PHY registers. We need
   83  * to select which interface to use depending on the chip type.
   84  */
   85 
   86 #ifdef HAVE_KERNEL_OPTION_HEADERS
   87 #include "opt_device_polling.h"
   88 #endif
   89 
   90 #include <sys/param.h>
   91 #include <sys/endian.h>
   92 #include <sys/systm.h>
   93 #include <sys/sockio.h>
   94 #include <sys/mbuf.h>
   95 #include <sys/malloc.h>
   96 #include <sys/kernel.h>
   97 #include <sys/module.h>
   98 #include <sys/socket.h>
   99 #include <sys/sysctl.h>
  100 
  101 #include <net/if.h>
  102 #include <net/if_arp.h>
  103 #include <net/ethernet.h>
  104 #include <net/if_dl.h>
  105 #include <net/if_media.h>
  106 #include <net/if_types.h>
  107 
  108 #include <net/bpf.h>
  109 
  110 #include <machine/bus.h>
  111 #include <machine/resource.h>
  112 #include <sys/bus.h>
  113 #include <sys/rman.h>
  114 
  115 #include <dev/mii/mii.h>
  116 #include <dev/mii/miivar.h>
  117 
  118 #include <dev/pci/pcireg.h>
  119 #include <dev/pci/pcivar.h>
  120 
  121 MODULE_DEPEND(rl, pci, 1, 1, 1);
  122 MODULE_DEPEND(rl, ether, 1, 1, 1);
  123 MODULE_DEPEND(rl, miibus, 1, 1, 1);
  124 
  125 /* "device miibus" required.  See GENERIC if you get errors here. */
  126 #include "miibus_if.h"
  127 
  128 #include <pci/if_rlreg.h>
  129 
  130 /*
  131  * Various supported device vendors/types and their names.
  132  */
  133 static struct rl_type rl_devs[] = {
  134         { RT_VENDORID, RT_DEVICEID_8129, RL_8129,
  135                 "RealTek 8129 10/100BaseTX" },
  136         { RT_VENDORID, RT_DEVICEID_8139, RL_8139,
  137                 "RealTek 8139 10/100BaseTX" },
  138         { RT_VENDORID, RT_DEVICEID_8139D, RL_8139,
  139                 "RealTek 8139 10/100BaseTX" },
  140         { RT_VENDORID, RT_DEVICEID_8138, RL_8139,
  141                 "RealTek 8139 10/100BaseTX CardBus" },
  142         { RT_VENDORID, RT_DEVICEID_8100, RL_8139,
  143                 "RealTek 8100 10/100BaseTX" },
  144         { ACCTON_VENDORID, ACCTON_DEVICEID_5030, RL_8139,
  145                 "Accton MPX 5030/5038 10/100BaseTX" },
  146         { DELTA_VENDORID, DELTA_DEVICEID_8139, RL_8139,
  147                 "Delta Electronics 8139 10/100BaseTX" },
  148         { ADDTRON_VENDORID, ADDTRON_DEVICEID_8139, RL_8139,
  149                 "Addtron Technology 8139 10/100BaseTX" },
  150         { DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS, RL_8139,
  151                 "D-Link DFE-530TX+ 10/100BaseTX" },
  152         { DLINK_VENDORID, DLINK_DEVICEID_690TXD, RL_8139,
  153                 "D-Link DFE-690TXD 10/100BaseTX" },
  154         { NORTEL_VENDORID, ACCTON_DEVICEID_5030, RL_8139,
  155                 "Nortel Networks 10/100BaseTX" },
  156         { COREGA_VENDORID, COREGA_DEVICEID_FETHERCBTXD, RL_8139,
  157                 "Corega FEther CB-TXD" },
  158         { COREGA_VENDORID, COREGA_DEVICEID_FETHERIICBTXD, RL_8139,
  159                 "Corega FEtherII CB-TXD" },
  160         { PEPPERCON_VENDORID, PEPPERCON_DEVICEID_ROLF, RL_8139,
  161                 "Peppercon AG ROL-F" },
  162         { PLANEX_VENDORID, PLANEX_DEVICEID_FNW3603TX, RL_8139,
  163                 "Planex FNW-3603-TX" },
  164         { PLANEX_VENDORID, PLANEX_DEVICEID_FNW3800TX, RL_8139,
  165                 "Planex FNW-3800-TX" },
  166         { CP_VENDORID, RT_DEVICEID_8139, RL_8139,
  167                 "Compaq HNE-300" },
  168         { LEVEL1_VENDORID, LEVEL1_DEVICEID_FPC0106TX, RL_8139,
  169                 "LevelOne FPC-0106TX" },
  170         { EDIMAX_VENDORID, EDIMAX_DEVICEID_EP4103DL, RL_8139,
  171                 "Edimax EP-4103DL CardBus" }
  172 };
  173 
  174 static int rl_attach(device_t);
  175 static int rl_detach(device_t);
  176 static void rl_dmamap_cb(void *, bus_dma_segment_t *, int, int);
  177 static int rl_dma_alloc(struct rl_softc *);
  178 static void rl_dma_free(struct rl_softc *);
  179 static void rl_eeprom_putbyte(struct rl_softc *, int);
  180 static void rl_eeprom_getword(struct rl_softc *, int, uint16_t *);
  181 static int rl_encap(struct rl_softc *, struct mbuf **);
  182 static int rl_list_tx_init(struct rl_softc *);
  183 static int rl_list_rx_init(struct rl_softc *);
  184 static int rl_ifmedia_upd(struct ifnet *);
  185 static void rl_ifmedia_sts(struct ifnet *, struct ifmediareq *);
  186 static int rl_ioctl(struct ifnet *, u_long, caddr_t);
  187 static void rl_intr(void *);
  188 static void rl_init(void *);
  189 static void rl_init_locked(struct rl_softc *sc);
  190 static void rl_mii_send(struct rl_softc *, uint32_t, int);
  191 static void rl_mii_sync(struct rl_softc *);
  192 static int rl_mii_readreg(struct rl_softc *, struct rl_mii_frame *);
  193 static int rl_mii_writereg(struct rl_softc *, struct rl_mii_frame *);
  194 static int rl_miibus_readreg(device_t, int, int);
  195 static void rl_miibus_statchg(device_t);
  196 static int rl_miibus_writereg(device_t, int, int, int);
  197 #ifdef DEVICE_POLLING
  198 static void rl_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
  199 static void rl_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count);
  200 #endif
  201 static int rl_probe(device_t);
  202 static void rl_read_eeprom(struct rl_softc *, uint8_t *, int, int, int);
  203 static void rl_reset(struct rl_softc *);
  204 static int rl_resume(device_t);
  205 static void rl_rxeof(struct rl_softc *);
  206 static void rl_rxfilter(struct rl_softc *);
  207 static int rl_shutdown(device_t);
  208 static void rl_start(struct ifnet *);
  209 static void rl_start_locked(struct ifnet *);
  210 static void rl_stop(struct rl_softc *);
  211 static int rl_suspend(device_t);
  212 static void rl_tick(void *);
  213 static void rl_txeof(struct rl_softc *);
  214 static void rl_watchdog(struct rl_softc *);
  215 static void rl_setwol(struct rl_softc *);
  216 static void rl_clrwol(struct rl_softc *);
  217 
  218 static device_method_t rl_methods[] = {
  219         /* Device interface */
  220         DEVMETHOD(device_probe,         rl_probe),
  221         DEVMETHOD(device_attach,        rl_attach),
  222         DEVMETHOD(device_detach,        rl_detach),
  223         DEVMETHOD(device_suspend,       rl_suspend),
  224         DEVMETHOD(device_resume,        rl_resume),
  225         DEVMETHOD(device_shutdown,      rl_shutdown),
  226 
  227         /* bus interface */
  228         DEVMETHOD(bus_print_child,      bus_generic_print_child),
  229         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
  230 
  231         /* MII interface */
  232         DEVMETHOD(miibus_readreg,       rl_miibus_readreg),
  233         DEVMETHOD(miibus_writereg,      rl_miibus_writereg),
  234         DEVMETHOD(miibus_statchg,       rl_miibus_statchg),
  235 
  236         { 0, 0 }
  237 };
  238 
  239 static driver_t rl_driver = {
  240         "rl",
  241         rl_methods,
  242         sizeof(struct rl_softc)
  243 };
  244 
  245 static devclass_t rl_devclass;
  246 
  247 DRIVER_MODULE(rl, pci, rl_driver, rl_devclass, 0, 0);
  248 DRIVER_MODULE(rl, cardbus, rl_driver, rl_devclass, 0, 0);
  249 DRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0);
  250 
  251 #define EE_SET(x)                                       \
  252         CSR_WRITE_1(sc, RL_EECMD,                       \
  253                 CSR_READ_1(sc, RL_EECMD) | x)
  254 
  255 #define EE_CLR(x)                                       \
  256         CSR_WRITE_1(sc, RL_EECMD,                       \
  257                 CSR_READ_1(sc, RL_EECMD) & ~x)
  258 
  259 /*
  260  * Send a read command and address to the EEPROM, check for ACK.
  261  */
  262 static void
  263 rl_eeprom_putbyte(struct rl_softc *sc, int addr)
  264 {
  265         register int            d, i;
  266 
  267         d = addr | sc->rl_eecmd_read;
  268 
  269         /*
  270          * Feed in each bit and strobe the clock.
  271          */
  272         for (i = 0x400; i; i >>= 1) {
  273                 if (d & i) {
  274                         EE_SET(RL_EE_DATAIN);
  275                 } else {
  276                         EE_CLR(RL_EE_DATAIN);
  277                 }
  278                 DELAY(100);
  279                 EE_SET(RL_EE_CLK);
  280                 DELAY(150);
  281                 EE_CLR(RL_EE_CLK);
  282                 DELAY(100);
  283         }
  284 }
  285 
  286 /*
  287  * Read a word of data stored in the EEPROM at address 'addr.'
  288  */
  289 static void
  290 rl_eeprom_getword(struct rl_softc *sc, int addr, uint16_t *dest)
  291 {
  292         register int            i;
  293         uint16_t                word = 0;
  294 
  295         /* Enter EEPROM access mode. */
  296         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
  297 
  298         /*
  299          * Send address of word we want to read.
  300          */
  301         rl_eeprom_putbyte(sc, addr);
  302 
  303         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
  304 
  305         /*
  306          * Start reading bits from EEPROM.
  307          */
  308         for (i = 0x8000; i; i >>= 1) {
  309                 EE_SET(RL_EE_CLK);
  310                 DELAY(100);
  311                 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
  312                         word |= i;
  313                 EE_CLR(RL_EE_CLK);
  314                 DELAY(100);
  315         }
  316 
  317         /* Turn off EEPROM access mode. */
  318         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
  319 
  320         *dest = word;
  321 }
  322 
  323 /*
  324  * Read a sequence of words from the EEPROM.
  325  */
  326 static void
  327 rl_read_eeprom(struct rl_softc *sc, uint8_t *dest, int off, int cnt, int swap)
  328 {
  329         int                     i;
  330         uint16_t                word = 0, *ptr;
  331 
  332         for (i = 0; i < cnt; i++) {
  333                 rl_eeprom_getword(sc, off + i, &word);
  334                 ptr = (uint16_t *)(dest + (i * 2));
  335                 if (swap)
  336                         *ptr = ntohs(word);
  337                 else
  338                         *ptr = word;
  339         }
  340 }
  341 
  342 /*
  343  * MII access routines are provided for the 8129, which
  344  * doesn't have a built-in PHY. For the 8139, we fake things
  345  * up by diverting rl_phy_readreg()/rl_phy_writereg() to the
  346  * direct access PHY registers.
  347  */
  348 #define MII_SET(x)                                      \
  349         CSR_WRITE_1(sc, RL_MII,                         \
  350                 CSR_READ_1(sc, RL_MII) | (x))
  351 
  352 #define MII_CLR(x)                                      \
  353         CSR_WRITE_1(sc, RL_MII,                         \
  354                 CSR_READ_1(sc, RL_MII) & ~(x))
  355 
  356 /*
  357  * Sync the PHYs by setting data bit and strobing the clock 32 times.
  358  */
  359 static void
  360 rl_mii_sync(struct rl_softc *sc)
  361 {
  362         register int            i;
  363 
  364         MII_SET(RL_MII_DIR|RL_MII_DATAOUT);
  365 
  366         for (i = 0; i < 32; i++) {
  367                 MII_SET(RL_MII_CLK);
  368                 DELAY(1);
  369                 MII_CLR(RL_MII_CLK);
  370                 DELAY(1);
  371         }
  372 }
  373 
  374 /*
  375  * Clock a series of bits through the MII.
  376  */
  377 static void
  378 rl_mii_send(struct rl_softc *sc, uint32_t bits, int cnt)
  379 {
  380         int                     i;
  381 
  382         MII_CLR(RL_MII_CLK);
  383 
  384         for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
  385                 if (bits & i) {
  386                         MII_SET(RL_MII_DATAOUT);
  387                 } else {
  388                         MII_CLR(RL_MII_DATAOUT);
  389                 }
  390                 DELAY(1);
  391                 MII_CLR(RL_MII_CLK);
  392                 DELAY(1);
  393                 MII_SET(RL_MII_CLK);
  394         }
  395 }
  396 
  397 /*
  398  * Read an PHY register through the MII.
  399  */
  400 static int
  401 rl_mii_readreg(struct rl_softc *sc, struct rl_mii_frame *frame)
  402 {
  403         int                     i, ack;
  404 
  405         /* Set up frame for RX. */
  406         frame->mii_stdelim = RL_MII_STARTDELIM;
  407         frame->mii_opcode = RL_MII_READOP;
  408         frame->mii_turnaround = 0;
  409         frame->mii_data = 0;
  410 
  411         CSR_WRITE_2(sc, RL_MII, 0);
  412 
  413         /* Turn on data xmit. */
  414         MII_SET(RL_MII_DIR);
  415 
  416         rl_mii_sync(sc);
  417 
  418         /* Send command/address info. */
  419         rl_mii_send(sc, frame->mii_stdelim, 2);
  420         rl_mii_send(sc, frame->mii_opcode, 2);
  421         rl_mii_send(sc, frame->mii_phyaddr, 5);
  422         rl_mii_send(sc, frame->mii_regaddr, 5);
  423 
  424         /* Idle bit */
  425         MII_CLR((RL_MII_CLK|RL_MII_DATAOUT));
  426         DELAY(1);
  427         MII_SET(RL_MII_CLK);
  428         DELAY(1);
  429 
  430         /* Turn off xmit. */
  431         MII_CLR(RL_MII_DIR);
  432 
  433         /* Check for ack */
  434         MII_CLR(RL_MII_CLK);
  435         DELAY(1);
  436         ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN;
  437         MII_SET(RL_MII_CLK);
  438         DELAY(1);
  439 
  440         /*
  441          * Now try reading data bits. If the ack failed, we still
  442          * need to clock through 16 cycles to keep the PHY(s) in sync.
  443          */
  444         if (ack) {
  445                 for(i = 0; i < 16; i++) {
  446                         MII_CLR(RL_MII_CLK);
  447                         DELAY(1);
  448                         MII_SET(RL_MII_CLK);
  449                         DELAY(1);
  450                 }
  451                 goto fail;
  452         }
  453 
  454         for (i = 0x8000; i; i >>= 1) {
  455                 MII_CLR(RL_MII_CLK);
  456                 DELAY(1);
  457                 if (!ack) {
  458                         if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN)
  459                                 frame->mii_data |= i;
  460                         DELAY(1);
  461                 }
  462                 MII_SET(RL_MII_CLK);
  463                 DELAY(1);
  464         }
  465 
  466 fail:
  467         MII_CLR(RL_MII_CLK);
  468         DELAY(1);
  469         MII_SET(RL_MII_CLK);
  470         DELAY(1);
  471 
  472         return (ack ? 1 : 0);
  473 }
  474 
  475 /*
  476  * Write to a PHY register through the MII.
  477  */
  478 static int
  479 rl_mii_writereg(struct rl_softc *sc, struct rl_mii_frame *frame)
  480 {
  481 
  482         /* Set up frame for TX. */
  483         frame->mii_stdelim = RL_MII_STARTDELIM;
  484         frame->mii_opcode = RL_MII_WRITEOP;
  485         frame->mii_turnaround = RL_MII_TURNAROUND;
  486 
  487         /* Turn on data output. */
  488         MII_SET(RL_MII_DIR);
  489 
  490         rl_mii_sync(sc);
  491 
  492         rl_mii_send(sc, frame->mii_stdelim, 2);
  493         rl_mii_send(sc, frame->mii_opcode, 2);
  494         rl_mii_send(sc, frame->mii_phyaddr, 5);
  495         rl_mii_send(sc, frame->mii_regaddr, 5);
  496         rl_mii_send(sc, frame->mii_turnaround, 2);
  497         rl_mii_send(sc, frame->mii_data, 16);
  498 
  499         /* Idle bit. */
  500         MII_SET(RL_MII_CLK);
  501         DELAY(1);
  502         MII_CLR(RL_MII_CLK);
  503         DELAY(1);
  504 
  505         /* Turn off xmit. */
  506         MII_CLR(RL_MII_DIR);
  507 
  508         return (0);
  509 }
  510 
  511 static int
  512 rl_miibus_readreg(device_t dev, int phy, int reg)
  513 {
  514         struct rl_softc         *sc;
  515         struct rl_mii_frame     frame;
  516         uint16_t                rval = 0;
  517         uint16_t                rl8139_reg = 0;
  518 
  519         sc = device_get_softc(dev);
  520 
  521         if (sc->rl_type == RL_8139) {
  522                 switch (reg) {
  523                 case MII_BMCR:
  524                         rl8139_reg = RL_BMCR;
  525                         break;
  526                 case MII_BMSR:
  527                         rl8139_reg = RL_BMSR;
  528                         break;
  529                 case MII_ANAR:
  530                         rl8139_reg = RL_ANAR;
  531                         break;
  532                 case MII_ANER:
  533                         rl8139_reg = RL_ANER;
  534                         break;
  535                 case MII_ANLPAR:
  536                         rl8139_reg = RL_LPAR;
  537                         break;
  538                 case MII_PHYIDR1:
  539                 case MII_PHYIDR2:
  540                         return (0);
  541                 /*
  542                  * Allow the rlphy driver to read the media status
  543                  * register. If we have a link partner which does not
  544                  * support NWAY, this is the register which will tell
  545                  * us the results of parallel detection.
  546                  */
  547                 case RL_MEDIASTAT:
  548                         rval = CSR_READ_1(sc, RL_MEDIASTAT);
  549                         return (rval);
  550                 default:
  551                         device_printf(sc->rl_dev, "bad phy register\n");
  552                         return (0);
  553                 }
  554                 rval = CSR_READ_2(sc, rl8139_reg);
  555                 return (rval);
  556         }
  557 
  558         bzero((char *)&frame, sizeof(frame));
  559         frame.mii_phyaddr = phy;
  560         frame.mii_regaddr = reg;
  561         rl_mii_readreg(sc, &frame);
  562 
  563         return (frame.mii_data);
  564 }
  565 
  566 static int
  567 rl_miibus_writereg(device_t dev, int phy, int reg, int data)
  568 {
  569         struct rl_softc         *sc;
  570         struct rl_mii_frame     frame;
  571         uint16_t                rl8139_reg = 0;
  572 
  573         sc = device_get_softc(dev);
  574 
  575         if (sc->rl_type == RL_8139) {
  576                 switch (reg) {
  577                 case MII_BMCR:
  578                         rl8139_reg = RL_BMCR;
  579                         break;
  580                 case MII_BMSR:
  581                         rl8139_reg = RL_BMSR;
  582                         break;
  583                 case MII_ANAR:
  584                         rl8139_reg = RL_ANAR;
  585                         break;
  586                 case MII_ANER:
  587                         rl8139_reg = RL_ANER;
  588                         break;
  589                 case MII_ANLPAR:
  590                         rl8139_reg = RL_LPAR;
  591                         break;
  592                 case MII_PHYIDR1:
  593                 case MII_PHYIDR2:
  594                         return (0);
  595                         break;
  596                 default:
  597                         device_printf(sc->rl_dev, "bad phy register\n");
  598                         return (0);
  599                 }
  600                 CSR_WRITE_2(sc, rl8139_reg, data);
  601                 return (0);
  602         }
  603 
  604         bzero((char *)&frame, sizeof(frame));
  605         frame.mii_phyaddr = phy;
  606         frame.mii_regaddr = reg;
  607         frame.mii_data = data;
  608         rl_mii_writereg(sc, &frame);
  609 
  610         return (0);
  611 }
  612 
  613 static void
  614 rl_miibus_statchg(device_t dev)
  615 {
  616         struct rl_softc         *sc;
  617         struct ifnet            *ifp;
  618         struct mii_data         *mii;
  619 
  620         sc = device_get_softc(dev);
  621         mii = device_get_softc(sc->rl_miibus);
  622         ifp = sc->rl_ifp;
  623         if (mii == NULL || ifp == NULL ||
  624             (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
  625                 return;
  626 
  627         sc->rl_flags &= ~RL_FLAG_LINK;
  628         if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
  629             (IFM_ACTIVE | IFM_AVALID)) {
  630                 switch (IFM_SUBTYPE(mii->mii_media_active)) {
  631                 case IFM_10_T:
  632                 case IFM_100_TX:
  633                         sc->rl_flags |= RL_FLAG_LINK;
  634                         break;
  635                 default:
  636                         break;
  637                 }
  638         }
  639         /*
  640          * RealTek controllers do not provide any interface to
  641          * Tx/Rx MACs for resolved speed, duplex and flow-control
  642          * parameters.
  643          */
  644 }
  645 
  646 /*
  647  * Program the 64-bit multicast hash filter.
  648  */
  649 static void
  650 rl_rxfilter(struct rl_softc *sc)
  651 {
  652         struct ifnet            *ifp = sc->rl_ifp;
  653         int                     h = 0;
  654         uint32_t                hashes[2] = { 0, 0 };
  655         struct ifmultiaddr      *ifma;
  656         uint32_t                rxfilt;
  657 
  658         RL_LOCK_ASSERT(sc);
  659 
  660         rxfilt = CSR_READ_4(sc, RL_RXCFG);
  661         rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_BROAD |
  662             RL_RXCFG_RX_MULTI);
  663         /* Always accept frames destined for this host. */
  664         rxfilt |= RL_RXCFG_RX_INDIV;
  665         /* Set capture broadcast bit to capture broadcast frames. */
  666         if (ifp->if_flags & IFF_BROADCAST)
  667                 rxfilt |= RL_RXCFG_RX_BROAD;
  668         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
  669                 rxfilt |= RL_RXCFG_RX_MULTI;
  670                 if (ifp->if_flags & IFF_PROMISC)
  671                         rxfilt |= RL_RXCFG_RX_ALLPHYS;
  672                 hashes[0] = 0xFFFFFFFF;
  673                 hashes[1] = 0xFFFFFFFF;
  674         } else {
  675                 /* Now program new ones. */
  676                 IF_ADDR_LOCK(ifp);
  677                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
  678                         if (ifma->ifma_addr->sa_family != AF_LINK)
  679                                 continue;
  680                         h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
  681                             ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
  682                         if (h < 32)
  683                                 hashes[0] |= (1 << h);
  684                         else
  685                                 hashes[1] |= (1 << (h - 32));
  686                 }
  687                 IF_ADDR_UNLOCK(ifp);
  688                 if (hashes[0] != 0 || hashes[1] != 0)
  689                         rxfilt |= RL_RXCFG_RX_MULTI;
  690         }
  691 
  692         CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
  693         CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
  694         CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
  695 }
  696 
  697 static void
  698 rl_reset(struct rl_softc *sc)
  699 {
  700         register int            i;
  701 
  702         RL_LOCK_ASSERT(sc);
  703 
  704         CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
  705 
  706         for (i = 0; i < RL_TIMEOUT; i++) {
  707                 DELAY(10);
  708                 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
  709                         break;
  710         }
  711         if (i == RL_TIMEOUT)
  712                 device_printf(sc->rl_dev, "reset never completed!\n");
  713 }
  714 
  715 /*
  716  * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device
  717  * IDs against our list and return a device name if we find a match.
  718  */
  719 static int
  720 rl_probe(device_t dev)
  721 {
  722         struct rl_type          *t;
  723         uint16_t                devid, revid, vendor;
  724         int                     i;
  725         
  726         vendor = pci_get_vendor(dev);
  727         devid = pci_get_device(dev);
  728         revid = pci_get_revid(dev);
  729 
  730         if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
  731                 if (revid == 0x20) {
  732                         /* 8139C+, let re(4) take care of this device. */
  733                         return (ENXIO);
  734                 }
  735         }
  736         t = rl_devs;
  737         for (i = 0; i < sizeof(rl_devs) / sizeof(rl_devs[0]); i++, t++) {
  738                 if (vendor == t->rl_vid && devid == t->rl_did) {
  739                         device_set_desc(dev, t->rl_name);
  740                         return (BUS_PROBE_DEFAULT);
  741                 }
  742         }
  743 
  744         return (ENXIO);
  745 }
  746 
  747 struct rl_dmamap_arg {
  748         bus_addr_t      rl_busaddr;
  749 };
  750 
  751 static void
  752 rl_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
  753 {
  754         struct rl_dmamap_arg    *ctx;
  755 
  756         if (error != 0)
  757                 return;
  758 
  759         KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
  760 
  761         ctx = (struct rl_dmamap_arg *)arg;
  762         ctx->rl_busaddr = segs[0].ds_addr;
  763 }
  764 
  765 /*
  766  * Attach the interface. Allocate softc structures, do ifmedia
  767  * setup and ethernet/BPF attach.
  768  */
  769 static int
  770 rl_attach(device_t dev)
  771 {
  772         uint8_t                 eaddr[ETHER_ADDR_LEN];
  773         uint16_t                as[3];
  774         struct ifnet            *ifp;
  775         struct rl_softc         *sc;
  776         struct rl_type          *t;
  777         struct sysctl_ctx_list  *ctx;
  778         struct sysctl_oid_list  *children;
  779         int                     error = 0, hwrev, i, phy, pmc, rid;
  780         int                     prefer_iomap, unit;
  781         uint16_t                rl_did = 0;
  782         char                    tn[32];
  783 
  784         sc = device_get_softc(dev);
  785         unit = device_get_unit(dev);
  786         sc->rl_dev = dev;
  787 
  788         sc->rl_twister_enable = 0;
  789         snprintf(tn, sizeof(tn), "dev.rl.%d.twister_enable", unit);
  790         TUNABLE_INT_FETCH(tn, &sc->rl_twister_enable);
  791         ctx = device_get_sysctl_ctx(sc->rl_dev);
  792         children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
  793         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "twister_enable", CTLFLAG_RD,
  794            &sc->rl_twister_enable, 0, "");
  795 
  796         mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
  797             MTX_DEF);
  798         callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
  799 
  800         pci_enable_busmaster(dev);
  801 
  802 
  803         /*
  804          * Map control/status registers.
  805          * Default to using PIO access for this driver. On SMP systems,
  806          * there appear to be problems with memory mapped mode: it looks
  807          * like doing too many memory mapped access back to back in rapid
  808          * succession can hang the bus. I'm inclined to blame this on
  809          * crummy design/construction on the part of RealTek. Memory
  810          * mapped mode does appear to work on uniprocessor systems though.
  811          */
  812         prefer_iomap = 1;
  813         snprintf(tn, sizeof(tn), "dev.rl.%d.prefer_iomap", unit);
  814         TUNABLE_INT_FETCH(tn, &prefer_iomap);
  815         if (prefer_iomap) {
  816                 sc->rl_res_id = PCIR_BAR(0);
  817                 sc->rl_res_type = SYS_RES_IOPORT;
  818                 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
  819                     &sc->rl_res_id, RF_ACTIVE);
  820         }
  821         if (prefer_iomap == 0 || sc->rl_res == NULL) {
  822                 sc->rl_res_id = PCIR_BAR(1);
  823                 sc->rl_res_type = SYS_RES_MEMORY;
  824                 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
  825                     &sc->rl_res_id, RF_ACTIVE);
  826         }
  827         if (sc->rl_res == NULL) {
  828                 device_printf(dev, "couldn't map ports/memory\n");
  829                 error = ENXIO;
  830                 goto fail;
  831         }
  832 
  833 #ifdef notdef
  834         /*
  835          * Detect the Realtek 8139B. For some reason, this chip is very
  836          * unstable when left to autoselect the media
  837          * The best workaround is to set the device to the required
  838          * media type or to set it to the 10 Meg speed.
  839          */
  840         if ((rman_get_end(sc->rl_res) - rman_get_start(sc->rl_res)) == 0xFF)
  841                 device_printf(dev,
  842 "Realtek 8139B detected. Warning, this may be unstable in autoselect mode\n");
  843 #endif
  844 
  845         sc->rl_btag = rman_get_bustag(sc->rl_res);
  846         sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
  847 
  848         /* Allocate interrupt */
  849         rid = 0;
  850         sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  851             RF_SHAREABLE | RF_ACTIVE);
  852 
  853         if (sc->rl_irq[0] == NULL) {
  854                 device_printf(dev, "couldn't map interrupt\n");
  855                 error = ENXIO;
  856                 goto fail;
  857         }
  858 
  859         /*
  860          * Reset the adapter. Only take the lock here as it's needed in
  861          * order to call rl_reset().
  862          */
  863         RL_LOCK(sc);
  864         rl_reset(sc);
  865         RL_UNLOCK(sc);
  866 
  867         sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
  868         rl_read_eeprom(sc, (uint8_t *)&rl_did, 0, 1, 0);
  869         if (rl_did != 0x8129)
  870                 sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
  871 
  872         /*
  873          * Get station address from the EEPROM.
  874          */
  875         rl_read_eeprom(sc, (uint8_t *)as, RL_EE_EADDR, 3, 0);
  876         for (i = 0; i < 3; i++) {
  877                 eaddr[(i * 2) + 0] = as[i] & 0xff;
  878                 eaddr[(i * 2) + 1] = as[i] >> 8;
  879         }
  880 
  881         /*
  882          * Now read the exact device type from the EEPROM to find
  883          * out if it's an 8129 or 8139.
  884          */
  885         rl_read_eeprom(sc, (uint8_t *)&rl_did, RL_EE_PCI_DID, 1, 0);
  886 
  887         t = rl_devs;
  888         sc->rl_type = 0;
  889         while(t->rl_name != NULL) {
  890                 if (rl_did == t->rl_did) {
  891                         sc->rl_type = t->rl_basetype;
  892                         break;
  893                 }
  894                 t++;
  895         }
  896 
  897         if (sc->rl_type == 0) {
  898                 device_printf(dev, "unknown device ID: %x assuming 8139\n",
  899                     rl_did);
  900                 sc->rl_type = RL_8139;
  901                 /*
  902                  * Read RL_IDR register to get ethernet address as accessing
  903                  * EEPROM may not extract correct address.
  904                  */
  905                 for (i = 0; i < ETHER_ADDR_LEN; i++)
  906                         eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
  907         }
  908 
  909         if ((error = rl_dma_alloc(sc)) != 0)
  910                 goto fail;
  911 
  912         ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
  913         if (ifp == NULL) {
  914                 device_printf(dev, "can not if_alloc()\n");
  915                 error = ENOSPC;
  916                 goto fail;
  917         }
  918 
  919 #define RL_PHYAD_INTERNAL       0
  920 
  921         /* Do MII setup */
  922         phy = MII_PHY_ANY;
  923         if (sc->rl_type == RL_8139)
  924                 phy = RL_PHYAD_INTERNAL;
  925         error = mii_attach(dev, &sc->rl_miibus, ifp, rl_ifmedia_upd,
  926             rl_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
  927         if (error != 0) {
  928                 device_printf(dev, "attaching PHYs failed\n");
  929                 goto fail;
  930         }
  931 
  932         ifp->if_softc = sc;
  933         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
  934         ifp->if_mtu = ETHERMTU;
  935         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  936         ifp->if_ioctl = rl_ioctl;
  937         ifp->if_start = rl_start;
  938         ifp->if_init = rl_init;
  939         ifp->if_capabilities = IFCAP_VLAN_MTU;
  940         /* Check WOL for RTL8139B or newer controllers. */
  941         if (sc->rl_type == RL_8139 &&
  942             pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) == 0) {
  943                 hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
  944                 switch (hwrev) {
  945                 case RL_HWREV_8139B:
  946                 case RL_HWREV_8130:
  947                 case RL_HWREV_8139C:
  948                 case RL_HWREV_8139D:
  949                 case RL_HWREV_8101:
  950                 case RL_HWREV_8100:
  951                         ifp->if_capabilities |= IFCAP_WOL;
  952                         /* Disable WOL. */
  953                         rl_clrwol(sc);
  954                         break;
  955                 default:
  956                         break;
  957                 }
  958         }
  959         ifp->if_capenable = ifp->if_capabilities;
  960 #ifdef DEVICE_POLLING
  961         ifp->if_capabilities |= IFCAP_POLLING;
  962 #endif
  963         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  964         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  965         IFQ_SET_READY(&ifp->if_snd);
  966 
  967         /*
  968          * Call MI attach routine.
  969          */
  970         ether_ifattach(ifp, eaddr);
  971 
  972         /* Hook interrupt last to avoid having to lock softc */
  973         error = bus_setup_intr(dev, sc->rl_irq[0], INTR_TYPE_NET | INTR_MPSAFE,
  974             NULL, rl_intr, sc, &sc->rl_intrhand[0]);
  975         if (error) {
  976                 device_printf(sc->rl_dev, "couldn't set up irq\n");
  977                 ether_ifdetach(ifp);
  978         }
  979 
  980 fail:
  981         if (error)
  982                 rl_detach(dev);
  983 
  984         return (error);
  985 }
  986 
  987 /*
  988  * Shutdown hardware and free up resources. This can be called any
  989  * time after the mutex has been initialized. It is called in both
  990  * the error case in attach and the normal detach case so it needs
  991  * to be careful about only freeing resources that have actually been
  992  * allocated.
  993  */
  994 static int
  995 rl_detach(device_t dev)
  996 {
  997         struct rl_softc         *sc;
  998         struct ifnet            *ifp;
  999 
 1000         sc = device_get_softc(dev);
 1001         ifp = sc->rl_ifp;
 1002 
 1003         KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized"));
 1004 
 1005 #ifdef DEVICE_POLLING
 1006         if (ifp->if_capenable & IFCAP_POLLING)
 1007                 ether_poll_deregister(ifp);
 1008 #endif
 1009         /* These should only be active if attach succeeded */
 1010         if (device_is_attached(dev)) {
 1011                 RL_LOCK(sc);
 1012                 rl_stop(sc);
 1013                 RL_UNLOCK(sc);
 1014                 callout_drain(&sc->rl_stat_callout);
 1015                 ether_ifdetach(ifp);
 1016         }
 1017 #if 0
 1018         sc->suspended = 1;
 1019 #endif
 1020         if (sc->rl_miibus)
 1021                 device_delete_child(dev, sc->rl_miibus);
 1022         bus_generic_detach(dev);
 1023 
 1024         if (sc->rl_intrhand[0])
 1025                 bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
 1026         if (sc->rl_irq[0])
 1027                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq[0]);
 1028         if (sc->rl_res)
 1029                 bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
 1030                     sc->rl_res);
 1031 
 1032         if (ifp)
 1033                 if_free(ifp);
 1034 
 1035         rl_dma_free(sc);
 1036 
 1037         mtx_destroy(&sc->rl_mtx);
 1038 
 1039         return (0);
 1040 }
 1041 
 1042 static int
 1043 rl_dma_alloc(struct rl_softc *sc)
 1044 {
 1045         struct rl_dmamap_arg    ctx;
 1046         int                     error, i;
 1047 
 1048         /*
 1049          * Allocate the parent bus DMA tag appropriate for PCI.
 1050          */
 1051         error = bus_dma_tag_create(bus_get_dma_tag(sc->rl_dev), /* parent */
 1052             1, 0,                       /* alignment, boundary */
 1053             BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
 1054             BUS_SPACE_MAXADDR,          /* highaddr */
 1055             NULL, NULL,                 /* filter, filterarg */
 1056             BUS_SPACE_MAXSIZE_32BIT, 0, /* maxsize, nsegments */
 1057             BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
 1058             0,                          /* flags */
 1059             NULL, NULL,                 /* lockfunc, lockarg */
 1060             &sc->rl_parent_tag);
 1061         if (error) {
 1062                 device_printf(sc->rl_dev,
 1063                     "failed to create parent DMA tag.\n");
 1064                 goto fail;
 1065         }
 1066         /* Create DMA tag for Rx memory block. */
 1067         error = bus_dma_tag_create(sc->rl_parent_tag,   /* parent */
 1068             RL_RX_8139_BUF_ALIGN, 0,    /* alignment, boundary */
 1069             BUS_SPACE_MAXADDR,          /* lowaddr */
 1070             BUS_SPACE_MAXADDR,          /* highaddr */
 1071             NULL, NULL,                 /* filter, filterarg */
 1072             RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ, 1,   /* maxsize,nsegments */
 1073             RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ,      /* maxsegsize */
 1074             0,                          /* flags */
 1075             NULL, NULL,                 /* lockfunc, lockarg */
 1076             &sc->rl_cdata.rl_rx_tag);
 1077         if (error) {
 1078                 device_printf(sc->rl_dev,
 1079                     "failed to create Rx memory block DMA tag.\n");
 1080                 goto fail;
 1081         }
 1082         /* Create DMA tag for Tx buffer. */
 1083         error = bus_dma_tag_create(sc->rl_parent_tag,   /* parent */
 1084             RL_TX_8139_BUF_ALIGN, 0,    /* alignment, boundary */
 1085             BUS_SPACE_MAXADDR,          /* lowaddr */
 1086             BUS_SPACE_MAXADDR,          /* highaddr */
 1087             NULL, NULL,                 /* filter, filterarg */
 1088             MCLBYTES, 1,                /* maxsize, nsegments */
 1089             MCLBYTES,                   /* maxsegsize */
 1090             0,                          /* flags */
 1091             NULL, NULL,                 /* lockfunc, lockarg */
 1092             &sc->rl_cdata.rl_tx_tag);
 1093         if (error) {
 1094                 device_printf(sc->rl_dev, "failed to create Tx DMA tag.\n");
 1095                 goto fail;
 1096         }
 1097 
 1098         /*
 1099          * Allocate DMA'able memory and load DMA map for Rx memory block.
 1100          */
 1101         error = bus_dmamem_alloc(sc->rl_cdata.rl_rx_tag,
 1102             (void **)&sc->rl_cdata.rl_rx_buf, BUS_DMA_WAITOK |
 1103             BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->rl_cdata.rl_rx_dmamap);
 1104         if (error != 0) {
 1105                 device_printf(sc->rl_dev,
 1106                     "failed to allocate Rx DMA memory block.\n");
 1107                 goto fail;
 1108         }
 1109         ctx.rl_busaddr = 0;
 1110         error = bus_dmamap_load(sc->rl_cdata.rl_rx_tag,
 1111             sc->rl_cdata.rl_rx_dmamap, sc->rl_cdata.rl_rx_buf,
 1112             RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ, rl_dmamap_cb, &ctx,
 1113             BUS_DMA_NOWAIT);
 1114         if (error != 0 || ctx.rl_busaddr == 0) {
 1115                 device_printf(sc->rl_dev,
 1116                     "could not load Rx DMA memory block.\n");
 1117                 goto fail;
 1118         }
 1119         sc->rl_cdata.rl_rx_buf_paddr = ctx.rl_busaddr;
 1120 
 1121         /* Create DMA maps for Tx buffers. */
 1122         for (i = 0; i < RL_TX_LIST_CNT; i++) {
 1123                 sc->rl_cdata.rl_tx_chain[i] = NULL;
 1124                 sc->rl_cdata.rl_tx_dmamap[i] = NULL;
 1125                 error = bus_dmamap_create(sc->rl_cdata.rl_tx_tag, 0,
 1126                     &sc->rl_cdata.rl_tx_dmamap[i]);
 1127                 if (error != 0) {
 1128                         device_printf(sc->rl_dev,
 1129                             "could not create Tx dmamap.\n");
 1130                         goto fail;
 1131                 }
 1132         }
 1133 
 1134         /* Leave a few bytes before the start of the RX ring buffer. */
 1135         sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
 1136         sc->rl_cdata.rl_rx_buf += RL_RX_8139_BUF_RESERVE;
 1137 
 1138 fail:
 1139         return (error);
 1140 }
 1141 
 1142 static void
 1143 rl_dma_free(struct rl_softc *sc)
 1144 {
 1145         int                     i;
 1146 
 1147         /* Rx memory block. */
 1148         if (sc->rl_cdata.rl_rx_tag != NULL) {
 1149                 if (sc->rl_cdata.rl_rx_dmamap != NULL)
 1150                         bus_dmamap_unload(sc->rl_cdata.rl_rx_tag,
 1151                             sc->rl_cdata.rl_rx_dmamap);
 1152                 if (sc->rl_cdata.rl_rx_dmamap != NULL &&
 1153                     sc->rl_cdata.rl_rx_buf_ptr != NULL)
 1154                         bus_dmamem_free(sc->rl_cdata.rl_rx_tag,
 1155                             sc->rl_cdata.rl_rx_buf_ptr,
 1156                             sc->rl_cdata.rl_rx_dmamap);
 1157                 sc->rl_cdata.rl_rx_buf_ptr = NULL;
 1158                 sc->rl_cdata.rl_rx_buf = NULL;
 1159                 sc->rl_cdata.rl_rx_dmamap = NULL;
 1160                 bus_dma_tag_destroy(sc->rl_cdata.rl_rx_tag);
 1161                 sc->rl_cdata.rl_tx_tag = NULL;
 1162         }
 1163 
 1164         /* Tx buffers. */
 1165         if (sc->rl_cdata.rl_tx_tag != NULL) {
 1166                 for (i = 0; i < RL_TX_LIST_CNT; i++) {
 1167                         if (sc->rl_cdata.rl_tx_dmamap[i] != NULL) {
 1168                                 bus_dmamap_destroy(
 1169                                     sc->rl_cdata.rl_tx_tag,
 1170                                     sc->rl_cdata.rl_tx_dmamap[i]);
 1171                                 sc->rl_cdata.rl_tx_dmamap[i] = NULL;
 1172                         }
 1173                 }
 1174                 bus_dma_tag_destroy(sc->rl_cdata.rl_tx_tag);
 1175                 sc->rl_cdata.rl_tx_tag = NULL;
 1176         }
 1177 
 1178         if (sc->rl_parent_tag != NULL) {
 1179                 bus_dma_tag_destroy(sc->rl_parent_tag);
 1180                 sc->rl_parent_tag = NULL;
 1181         }
 1182 }
 1183 
 1184 /*
 1185  * Initialize the transmit descriptors.
 1186  */
 1187 static int
 1188 rl_list_tx_init(struct rl_softc *sc)
 1189 {
 1190         struct rl_chain_data    *cd;
 1191         int                     i;
 1192 
 1193         RL_LOCK_ASSERT(sc);
 1194 
 1195         cd = &sc->rl_cdata;
 1196         for (i = 0; i < RL_TX_LIST_CNT; i++) {
 1197                 cd->rl_tx_chain[i] = NULL;
 1198                 CSR_WRITE_4(sc,
 1199                     RL_TXADDR0 + (i * sizeof(uint32_t)), 0x0000000);
 1200         }
 1201 
 1202         sc->rl_cdata.cur_tx = 0;
 1203         sc->rl_cdata.last_tx = 0;
 1204 
 1205         return (0);
 1206 }
 1207 
 1208 static int
 1209 rl_list_rx_init(struct rl_softc *sc)
 1210 {
 1211 
 1212         RL_LOCK_ASSERT(sc);
 1213 
 1214         bzero(sc->rl_cdata.rl_rx_buf_ptr,
 1215             RL_RXBUFLEN + RL_RX_8139_BUF_GUARD_SZ);
 1216         bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, sc->rl_cdata.rl_rx_dmamap,
 1217             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
 1218 
 1219         return (0);
 1220 }
 1221 
 1222 /*
 1223  * A frame has been uploaded: pass the resulting mbuf chain up to
 1224  * the higher level protocols.
 1225  *
 1226  * You know there's something wrong with a PCI bus-master chip design
 1227  * when you have to use m_devget().
 1228  *
 1229  * The receive operation is badly documented in the datasheet, so I'll
 1230  * attempt to document it here. The driver provides a buffer area and
 1231  * places its base address in the RX buffer start address register.
 1232  * The chip then begins copying frames into the RX buffer. Each frame
 1233  * is preceded by a 32-bit RX status word which specifies the length
 1234  * of the frame and certain other status bits. Each frame (starting with
 1235  * the status word) is also 32-bit aligned. The frame length is in the
 1236  * first 16 bits of the status word; the lower 15 bits correspond with
 1237  * the 'rx status register' mentioned in the datasheet.
 1238  *
 1239  * Note: to make the Alpha happy, the frame payload needs to be aligned
 1240  * on a 32-bit boundary. To achieve this, we pass RL_ETHER_ALIGN (2 bytes)
 1241  * as the offset argument to m_devget().
 1242  */
 1243 static void
 1244 rl_rxeof(struct rl_softc *sc)
 1245 {
 1246         struct mbuf             *m;
 1247         struct ifnet            *ifp = sc->rl_ifp;
 1248         uint8_t                 *rxbufpos;
 1249         int                     total_len = 0;
 1250         int                     wrap = 0;
 1251         uint32_t                rxstat;
 1252         uint16_t                cur_rx;
 1253         uint16_t                limit;
 1254         uint16_t                max_bytes, rx_bytes = 0;
 1255 
 1256         RL_LOCK_ASSERT(sc);
 1257 
 1258         bus_dmamap_sync(sc->rl_cdata.rl_rx_tag, sc->rl_cdata.rl_rx_dmamap,
 1259             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
 1260 
 1261         cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
 1262 
 1263         /* Do not try to read past this point. */
 1264         limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
 1265 
 1266         if (limit < cur_rx)
 1267                 max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
 1268         else
 1269                 max_bytes = limit - cur_rx;
 1270 
 1271         while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
 1272 #ifdef DEVICE_POLLING
 1273                 if (ifp->if_capenable & IFCAP_POLLING) {
 1274                         if (sc->rxcycles <= 0)
 1275                                 break;
 1276                         sc->rxcycles--;
 1277                 }
 1278 #endif
 1279                 rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
 1280                 rxstat = le32toh(*(uint32_t *)rxbufpos);
 1281 
 1282                 /*
 1283                  * Here's a totally undocumented fact for you. When the
 1284                  * RealTek chip is in the process of copying a packet into
 1285                  * RAM for you, the length will be 0xfff0. If you spot a
 1286                  * packet header with this value, you need to stop. The
 1287                  * datasheet makes absolutely no mention of this and
 1288                  * RealTek should be shot for this.
 1289                  */
 1290                 total_len = rxstat >> 16;
 1291                 if (total_len == RL_RXSTAT_UNFINISHED)
 1292                         break;
 1293 
 1294                 if (!(rxstat & RL_RXSTAT_RXOK) ||
 1295                     total_len < ETHER_MIN_LEN ||
 1296                     total_len > ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) {
 1297                         ifp->if_ierrors++;
 1298                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1299                         rl_init_locked(sc);
 1300                         return;
 1301                 }
 1302 
 1303                 /* No errors; receive the packet. */
 1304                 rx_bytes += total_len + 4;
 1305 
 1306                 /*
 1307                  * XXX The RealTek chip includes the CRC with every
 1308                  * received frame, and there's no way to turn this
 1309                  * behavior off (at least, I can't find anything in
 1310                  * the manual that explains how to do it) so we have
 1311                  * to trim off the CRC manually.
 1312                  */
 1313                 total_len -= ETHER_CRC_LEN;
 1314 
 1315                 /*
 1316                  * Avoid trying to read more bytes than we know
 1317                  * the chip has prepared for us.
 1318                  */
 1319                 if (rx_bytes > max_bytes)
 1320                         break;
 1321 
 1322                 rxbufpos = sc->rl_cdata.rl_rx_buf +
 1323                         ((cur_rx + sizeof(uint32_t)) % RL_RXBUFLEN);
 1324                 if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
 1325                         rxbufpos = sc->rl_cdata.rl_rx_buf;
 1326 
 1327                 wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
 1328                 if (total_len > wrap) {
 1329                         m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp,
 1330                             NULL);
 1331                         if (m != NULL)
 1332                                 m_copyback(m, wrap, total_len - wrap,
 1333                                         sc->rl_cdata.rl_rx_buf);
 1334                         cur_rx = (total_len - wrap + ETHER_CRC_LEN);
 1335                 } else {
 1336                         m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp,
 1337                             NULL);
 1338                         cur_rx += total_len + 4 + ETHER_CRC_LEN;
 1339                 }
 1340 
 1341                 /* Round up to 32-bit boundary. */
 1342                 cur_rx = (cur_rx + 3) & ~3;
 1343                 CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
 1344 
 1345                 if (m == NULL) {
 1346                         ifp->if_iqdrops++;
 1347                         continue;
 1348                 }
 1349 
 1350                 ifp->if_ipackets++;
 1351                 RL_UNLOCK(sc);
 1352                 (*ifp->if_input)(ifp, m);
 1353                 RL_LOCK(sc);
 1354         }
 1355 
 1356         /* No need to sync Rx memory block as we didn't modify it. */
 1357 }
 1358 
 1359 /*
 1360  * A frame was downloaded to the chip. It's safe for us to clean up
 1361  * the list buffers.
 1362  */
 1363 static void
 1364 rl_txeof(struct rl_softc *sc)
 1365 {
 1366         struct ifnet            *ifp = sc->rl_ifp;
 1367         uint32_t                txstat;
 1368 
 1369         RL_LOCK_ASSERT(sc);
 1370 
 1371         /*
 1372          * Go through our tx list and free mbufs for those
 1373          * frames that have been uploaded.
 1374          */
 1375         do {
 1376                 if (RL_LAST_TXMBUF(sc) == NULL)
 1377                         break;
 1378                 txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
 1379                 if (!(txstat & (RL_TXSTAT_TX_OK|
 1380                     RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
 1381                         break;
 1382 
 1383                 ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
 1384 
 1385                 bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, RL_LAST_DMAMAP(sc),
 1386                     BUS_DMASYNC_POSTWRITE);
 1387                 bus_dmamap_unload(sc->rl_cdata.rl_tx_tag, RL_LAST_DMAMAP(sc));
 1388                 m_freem(RL_LAST_TXMBUF(sc));
 1389                 RL_LAST_TXMBUF(sc) = NULL;
 1390                 /*
 1391                  * If there was a transmit underrun, bump the TX threshold.
 1392                  * Make sure not to overflow the 63 * 32byte we can address
 1393                  * with the 6 available bit.
 1394                  */
 1395                 if ((txstat & RL_TXSTAT_TX_UNDERRUN) &&
 1396                     (sc->rl_txthresh < 2016))
 1397                         sc->rl_txthresh += 32;
 1398                 if (txstat & RL_TXSTAT_TX_OK)
 1399                         ifp->if_opackets++;
 1400                 else {
 1401                         int                     oldthresh;
 1402                         ifp->if_oerrors++;
 1403                         if ((txstat & RL_TXSTAT_TXABRT) ||
 1404                             (txstat & RL_TXSTAT_OUTOFWIN))
 1405                                 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
 1406                         oldthresh = sc->rl_txthresh;
 1407                         /* error recovery */
 1408                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1409                         rl_init_locked(sc);
 1410                         /* restore original threshold */
 1411                         sc->rl_txthresh = oldthresh;
 1412                         return;
 1413                 }
 1414                 RL_INC(sc->rl_cdata.last_tx);
 1415                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1416         } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
 1417 
 1418         if (RL_LAST_TXMBUF(sc) == NULL)
 1419                 sc->rl_watchdog_timer = 0;
 1420 }
 1421 
 1422 static void
 1423 rl_twister_update(struct rl_softc *sc)
 1424 {
 1425         uint16_t linktest;
 1426         /*
 1427          * Table provided by RealTek (Kinston <shangh@realtek.com.tw>) for
 1428          * Linux driver.  Values undocumented otherwise.
 1429          */
 1430         static const uint32_t param[4][4] = {
 1431                 {0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
 1432                 {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
 1433                 {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
 1434                 {0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
 1435         };
 1436 
 1437         /*
 1438          * Tune the so-called twister registers of the RTL8139.  These
 1439          * are used to compensate for impedance mismatches.  The
 1440          * method for tuning these registers is undocumented and the
 1441          * following procedure is collected from public sources.
 1442          */
 1443         switch (sc->rl_twister)
 1444         {
 1445         case CHK_LINK:
 1446                 /*
 1447                  * If we have a sufficient link, then we can proceed in
 1448                  * the state machine to the next stage.  If not, then
 1449                  * disable further tuning after writing sane defaults.
 1450                  */
 1451                 if (CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_LINK_OK) {
 1452                         CSR_WRITE_2(sc, RL_CSCFG, RL_CSCFG_LINK_DOWN_OFF_CMD);
 1453                         sc->rl_twister = FIND_ROW;
 1454                 } else {
 1455                         CSR_WRITE_2(sc, RL_CSCFG, RL_CSCFG_LINK_DOWN_CMD);
 1456                         CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_CBL_TEST);
 1457                         CSR_WRITE_4(sc, RL_PARA78, RL_PARA78_DEF);
 1458                         CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_DEF);
 1459                         sc->rl_twister = DONE;
 1460                 }
 1461                 break;
 1462         case FIND_ROW:
 1463                 /*
 1464                  * Read how long it took to see the echo to find the tuning
 1465                  * row to use.
 1466                  */
 1467                 linktest = CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_STATUS;
 1468                 if (linktest == RL_CSCFG_ROW3)
 1469                         sc->rl_twist_row = 3;
 1470                 else if (linktest == RL_CSCFG_ROW2)
 1471                         sc->rl_twist_row = 2;
 1472                 else if (linktest == RL_CSCFG_ROW1)
 1473                         sc->rl_twist_row = 1;
 1474                 else
 1475                         sc->rl_twist_row = 0;
 1476                 sc->rl_twist_col = 0;
 1477                 sc->rl_twister = SET_PARAM;
 1478                 break;
 1479         case SET_PARAM:
 1480                 if (sc->rl_twist_col == 0)
 1481                         CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_RESET);
 1482                 CSR_WRITE_4(sc, RL_PARA7C,
 1483                     param[sc->rl_twist_row][sc->rl_twist_col]);
 1484                 if (++sc->rl_twist_col == 4) {
 1485                         if (sc->rl_twist_row == 3)
 1486                                 sc->rl_twister = RECHK_LONG;
 1487                         else
 1488                                 sc->rl_twister = DONE;
 1489                 }
 1490                 break;
 1491         case RECHK_LONG:
 1492                 /*
 1493                  * For long cables, we have to double check to make sure we
 1494                  * don't mistune.
 1495                  */
 1496                 linktest = CSR_READ_2(sc, RL_CSCFG) & RL_CSCFG_STATUS;
 1497                 if (linktest == RL_CSCFG_ROW3)
 1498                         sc->rl_twister = DONE;
 1499                 else {
 1500                         CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_RETUNE);
 1501                         sc->rl_twister = RETUNE;
 1502                 }
 1503                 break;
 1504         case RETUNE:
 1505                 /* Retune for a shorter cable (try column 2) */
 1506                 CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_CBL_TEST);
 1507                 CSR_WRITE_4(sc, RL_PARA78, RL_PARA78_DEF);
 1508                 CSR_WRITE_4(sc, RL_PARA7C, RL_PARA7C_DEF);
 1509                 CSR_WRITE_4(sc, RL_NWAYTST, RL_NWAYTST_RESET);
 1510                 sc->rl_twist_row--;
 1511                 sc->rl_twist_col = 0;
 1512                 sc->rl_twister = SET_PARAM;
 1513                 break;
 1514 
 1515         case DONE:
 1516                 break;
 1517         }
 1518         
 1519 }
 1520 
 1521 static void
 1522 rl_tick(void *xsc)
 1523 {
 1524         struct rl_softc         *sc = xsc;
 1525         struct mii_data         *mii;
 1526         int ticks;
 1527 
 1528         RL_LOCK_ASSERT(sc);
 1529         /*
 1530          * If we're doing the twister cable calibration, then we need to defer
 1531          * watchdog timeouts.  This is a no-op in normal operations, but
 1532          * can falsely trigger when the cable calibration takes a while and
 1533          * there was traffic ready to go when rl was started.
 1534          *
 1535          * We don't defer mii_tick since that updates the mii status, which
 1536          * helps the twister process, at least according to similar patches
 1537          * for the Linux driver I found online while doing the fixes.  Worst
 1538          * case is a few extra mii reads during calibration.
 1539          */
 1540         mii = device_get_softc(sc->rl_miibus);
 1541         mii_tick(mii);
 1542         if ((sc->rl_flags & RL_FLAG_LINK) == 0)
 1543                 rl_miibus_statchg(sc->rl_dev);
 1544         if (sc->rl_twister_enable) {
 1545                 if (sc->rl_twister == DONE)
 1546                         rl_watchdog(sc);
 1547                 else
 1548                         rl_twister_update(sc);
 1549                 if (sc->rl_twister == DONE)
 1550                         ticks = hz;
 1551                 else
 1552                         ticks = hz / 10;
 1553         } else {
 1554                 rl_watchdog(sc);
 1555                 ticks = hz;
 1556         }
 1557 
 1558         callout_reset(&sc->rl_stat_callout, ticks, rl_tick, sc);
 1559 }
 1560 
 1561 #ifdef DEVICE_POLLING
 1562 static void
 1563 rl_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
 1564 {
 1565         struct rl_softc *sc = ifp->if_softc;
 1566 
 1567         RL_LOCK(sc);
 1568         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1569                 rl_poll_locked(ifp, cmd, count);
 1570         RL_UNLOCK(sc);
 1571 }
 1572 
 1573 static void
 1574 rl_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
 1575 {
 1576         struct rl_softc *sc = ifp->if_softc;
 1577 
 1578         RL_LOCK_ASSERT(sc);
 1579 
 1580         sc->rxcycles = count;
 1581         rl_rxeof(sc);
 1582         rl_txeof(sc);
 1583 
 1584         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
 1585                 rl_start_locked(ifp);
 1586 
 1587         if (cmd == POLL_AND_CHECK_STATUS) {
 1588                 uint16_t        status;
 1589 
 1590                 /* We should also check the status register. */
 1591                 status = CSR_READ_2(sc, RL_ISR);
 1592                 if (status == 0xffff)
 1593                         return;
 1594                 if (status != 0)
 1595                         CSR_WRITE_2(sc, RL_ISR, status);
 1596 
 1597                 /* XXX We should check behaviour on receiver stalls. */
 1598 
 1599                 if (status & RL_ISR_SYSTEM_ERR) {
 1600                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1601                         rl_init_locked(sc);
 1602                 }
 1603         }
 1604 }
 1605 #endif /* DEVICE_POLLING */
 1606 
 1607 static void
 1608 rl_intr(void *arg)
 1609 {
 1610         struct rl_softc         *sc = arg;
 1611         struct ifnet            *ifp = sc->rl_ifp;
 1612         uint16_t                status;
 1613         int                     count;
 1614 
 1615         RL_LOCK(sc);
 1616 
 1617         if (sc->suspended)
 1618                 goto done_locked;
 1619 
 1620 #ifdef DEVICE_POLLING
 1621         if  (ifp->if_capenable & IFCAP_POLLING)
 1622                 goto done_locked;
 1623 #endif
 1624 
 1625         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
 1626                 goto done_locked2;
 1627         status = CSR_READ_2(sc, RL_ISR);
 1628         if (status == 0xffff || (status & RL_INTRS) == 0)
 1629                 goto done_locked;
 1630         /*
 1631          * Ours, disable further interrupts.
 1632          */
 1633         CSR_WRITE_2(sc, RL_IMR, 0);
 1634         for (count = 16; count > 0; count--) {
 1635                 CSR_WRITE_2(sc, RL_ISR, status);
 1636                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
 1637                         if (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR))
 1638                                 rl_rxeof(sc);
 1639                         if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR))
 1640                                 rl_txeof(sc);
 1641                         if (status & RL_ISR_SYSTEM_ERR) {
 1642                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1643                                 rl_init_locked(sc);
 1644                                 RL_UNLOCK(sc);
 1645                                 return;
 1646                         }
 1647                 }
 1648                 status = CSR_READ_2(sc, RL_ISR);
 1649                 /* If the card has gone away, the read returns 0xffff. */
 1650                 if (status == 0xffff || (status & RL_INTRS) == 0)
 1651                         break;
 1652         }
 1653 
 1654         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
 1655                 rl_start_locked(ifp);
 1656 
 1657 done_locked2:
 1658         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1659                 CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
 1660 done_locked:
 1661         RL_UNLOCK(sc);
 1662 }
 1663 
 1664 /*
 1665  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
 1666  * pointers to the fragment pointers.
 1667  */
 1668 static int
 1669 rl_encap(struct rl_softc *sc, struct mbuf **m_head)
 1670 {
 1671         struct mbuf             *m;
 1672         bus_dma_segment_t       txsegs[1];
 1673         int                     error, nsegs, padlen;
 1674 
 1675         RL_LOCK_ASSERT(sc);
 1676 
 1677         m = *m_head;
 1678         padlen = 0;
 1679         /*
 1680          * Hardware doesn't auto-pad, so we have to make sure
 1681          * pad short frames out to the minimum frame length.
 1682          */
 1683         if (m->m_pkthdr.len < RL_MIN_FRAMELEN)
 1684                 padlen = RL_MIN_FRAMELEN - m->m_pkthdr.len;
 1685         /*
 1686          * The RealTek is brain damaged and wants longword-aligned
 1687          * TX buffers, plus we can only have one fragment buffer
 1688          * per packet. We have to copy pretty much all the time.
 1689          */
 1690         if (m->m_next != NULL || (mtod(m, uintptr_t) & 3) != 0 ||
 1691             (padlen > 0 && M_TRAILINGSPACE(m) < padlen)) {
 1692                 m = m_defrag(*m_head, M_DONTWAIT);
 1693                 if (m == NULL) {
 1694                         m_freem(*m_head);
 1695                         *m_head = NULL;
 1696                         return (ENOMEM);
 1697                 }
 1698         }
 1699         *m_head = m;
 1700 
 1701         if (padlen > 0) {
 1702                 /*
 1703                  * Make security-conscious people happy: zero out the
 1704                  * bytes in the pad area, since we don't know what
 1705                  * this mbuf cluster buffer's previous user might
 1706                  * have left in it.
 1707                  */
 1708                 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
 1709                 m->m_pkthdr.len += padlen;
 1710                 m->m_len = m->m_pkthdr.len;
 1711         }
 1712 
 1713         error = bus_dmamap_load_mbuf_sg(sc->rl_cdata.rl_tx_tag,
 1714             RL_CUR_DMAMAP(sc), m, txsegs, &nsegs, 0);
 1715         if (error != 0)
 1716                 return (error);
 1717         if (nsegs == 0) {
 1718                 m_freem(*m_head);
 1719                 *m_head = NULL;
 1720                 return (EIO);
 1721         }
 1722 
 1723         RL_CUR_TXMBUF(sc) = m;
 1724         bus_dmamap_sync(sc->rl_cdata.rl_tx_tag, RL_CUR_DMAMAP(sc),
 1725             BUS_DMASYNC_PREWRITE);
 1726         CSR_WRITE_4(sc, RL_CUR_TXADDR(sc), RL_ADDR_LO(txsegs[0].ds_addr));
 1727 
 1728         return (0);
 1729 }
 1730 
 1731 /*
 1732  * Main transmit routine.
 1733  */
 1734 static void
 1735 rl_start(struct ifnet *ifp)
 1736 {
 1737         struct rl_softc         *sc = ifp->if_softc;
 1738 
 1739         RL_LOCK(sc);
 1740         rl_start_locked(ifp);
 1741         RL_UNLOCK(sc);
 1742 }
 1743 
 1744 static void
 1745 rl_start_locked(struct ifnet *ifp)
 1746 {
 1747         struct rl_softc         *sc = ifp->if_softc;
 1748         struct mbuf             *m_head = NULL;
 1749 
 1750         RL_LOCK_ASSERT(sc);
 1751 
 1752         if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
 1753             IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
 1754                 return;
 1755 
 1756         while (RL_CUR_TXMBUF(sc) == NULL) {
 1757 
 1758                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
 1759 
 1760                 if (m_head == NULL)
 1761                         break;
 1762 
 1763                 if (rl_encap(sc, &m_head)) {
 1764                         if (m_head == NULL)
 1765                                 break;
 1766                         IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
 1767                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1768                         break;
 1769                 }
 1770 
 1771                 /* Pass a copy of this mbuf chain to the bpf subsystem. */
 1772                 BPF_MTAP(ifp, RL_CUR_TXMBUF(sc));
 1773 
 1774                 /* Transmit the frame. */
 1775                 CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
 1776                     RL_TXTHRESH(sc->rl_txthresh) |
 1777                     RL_CUR_TXMBUF(sc)->m_pkthdr.len);
 1778 
 1779                 RL_INC(sc->rl_cdata.cur_tx);
 1780 
 1781                 /* Set a timeout in case the chip goes out to lunch. */
 1782                 sc->rl_watchdog_timer = 5;
 1783         }
 1784 
 1785         /*
 1786          * We broke out of the loop because all our TX slots are
 1787          * full. Mark the NIC as busy until it drains some of the
 1788          * packets from the queue.
 1789          */
 1790         if (RL_CUR_TXMBUF(sc) != NULL)
 1791                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1792 }
 1793 
 1794 static void
 1795 rl_init(void *xsc)
 1796 {
 1797         struct rl_softc         *sc = xsc;
 1798 
 1799         RL_LOCK(sc);
 1800         rl_init_locked(sc);
 1801         RL_UNLOCK(sc);
 1802 }
 1803 
 1804 static void
 1805 rl_init_locked(struct rl_softc *sc)
 1806 {
 1807         struct ifnet            *ifp = sc->rl_ifp;
 1808         struct mii_data         *mii;
 1809         uint32_t                eaddr[2];
 1810 
 1811         RL_LOCK_ASSERT(sc);
 1812 
 1813         mii = device_get_softc(sc->rl_miibus);
 1814 
 1815         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
 1816                 return;
 1817 
 1818         /*
 1819          * Cancel pending I/O and free all RX/TX buffers.
 1820          */
 1821         rl_stop(sc);
 1822 
 1823         rl_reset(sc);
 1824         if (sc->rl_twister_enable) {
 1825                 /*
 1826                  * Reset twister register tuning state.  The twister
 1827                  * registers and their tuning are undocumented, but
 1828                  * are necessary to cope with bad links.  rl_twister =
 1829                  * DONE here will disable this entirely.
 1830                  */
 1831                 sc->rl_twister = CHK_LINK;
 1832         }
 1833 
 1834         /*
 1835          * Init our MAC address.  Even though the chipset
 1836          * documentation doesn't mention it, we need to enter "Config
 1837          * register write enable" mode to modify the ID registers.
 1838          */
 1839         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
 1840         bzero(eaddr, sizeof(eaddr));
 1841         bcopy(IF_LLADDR(sc->rl_ifp), eaddr, ETHER_ADDR_LEN);
 1842         CSR_WRITE_STREAM_4(sc, RL_IDR0, eaddr[0]);
 1843         CSR_WRITE_STREAM_4(sc, RL_IDR4, eaddr[1]);
 1844         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
 1845 
 1846         /* Init the RX memory block pointer register. */
 1847         CSR_WRITE_4(sc, RL_RXADDR, sc->rl_cdata.rl_rx_buf_paddr +
 1848             RL_RX_8139_BUF_RESERVE);
 1849         /* Init TX descriptors. */
 1850         rl_list_tx_init(sc);
 1851         /* Init Rx memory block. */
 1852         rl_list_rx_init(sc);
 1853 
 1854         /*
 1855          * Enable transmit and receive.
 1856          */
 1857         CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
 1858 
 1859         /*
 1860          * Set the initial TX and RX configuration.
 1861          */
 1862         CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
 1863         CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
 1864 
 1865         /* Set RX filter. */
 1866         rl_rxfilter(sc);
 1867 
 1868 #ifdef DEVICE_POLLING
 1869         /* Disable interrupts if we are polling. */
 1870         if (ifp->if_capenable & IFCAP_POLLING)
 1871                 CSR_WRITE_2(sc, RL_IMR, 0);
 1872         else
 1873 #endif
 1874         /* Enable interrupts. */
 1875         CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
 1876 
 1877         /* Set initial TX threshold */
 1878         sc->rl_txthresh = RL_TX_THRESH_INIT;
 1879 
 1880         /* Start RX/TX process. */
 1881         CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
 1882 
 1883         /* Enable receiver and transmitter. */
 1884         CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
 1885 
 1886         sc->rl_flags &= ~RL_FLAG_LINK;
 1887         mii_mediachg(mii);
 1888 
 1889         CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
 1890 
 1891         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 1892         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1893 
 1894         callout_reset(&sc->rl_stat_callout, hz, rl_tick, sc);
 1895 }
 1896 
 1897 /*
 1898  * Set media options.
 1899  */
 1900 static int
 1901 rl_ifmedia_upd(struct ifnet *ifp)
 1902 {
 1903         struct rl_softc         *sc = ifp->if_softc;
 1904         struct mii_data         *mii;
 1905 
 1906         mii = device_get_softc(sc->rl_miibus);
 1907 
 1908         RL_LOCK(sc);
 1909         mii_mediachg(mii);
 1910         RL_UNLOCK(sc);
 1911 
 1912         return (0);
 1913 }
 1914 
 1915 /*
 1916  * Report current media status.
 1917  */
 1918 static void
 1919 rl_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
 1920 {
 1921         struct rl_softc         *sc = ifp->if_softc;
 1922         struct mii_data         *mii;
 1923 
 1924         mii = device_get_softc(sc->rl_miibus);
 1925 
 1926         RL_LOCK(sc);
 1927         mii_pollstat(mii);
 1928         RL_UNLOCK(sc);
 1929         ifmr->ifm_active = mii->mii_media_active;
 1930         ifmr->ifm_status = mii->mii_media_status;
 1931 }
 1932 
 1933 static int
 1934 rl_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
 1935 {
 1936         struct ifreq            *ifr = (struct ifreq *)data;
 1937         struct mii_data         *mii;
 1938         struct rl_softc         *sc = ifp->if_softc;
 1939         int                     error = 0, mask;
 1940 
 1941         switch (command) {
 1942         case SIOCSIFFLAGS:
 1943                 RL_LOCK(sc);
 1944                 if (ifp->if_flags & IFF_UP) {
 1945                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
 1946                             ((ifp->if_flags ^ sc->rl_if_flags) &
 1947                             (IFF_PROMISC | IFF_ALLMULTI)))
 1948                                 rl_rxfilter(sc);
 1949                         else
 1950                                 rl_init_locked(sc);
 1951                 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1952                         rl_stop(sc);
 1953                 sc->rl_if_flags = ifp->if_flags;
 1954                 RL_UNLOCK(sc);
 1955                 break;
 1956         case SIOCADDMULTI:
 1957         case SIOCDELMULTI:
 1958                 RL_LOCK(sc);
 1959                 rl_rxfilter(sc);
 1960                 RL_UNLOCK(sc);
 1961                 break;
 1962         case SIOCGIFMEDIA:
 1963         case SIOCSIFMEDIA:
 1964                 mii = device_get_softc(sc->rl_miibus);
 1965                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
 1966                 break;
 1967         case SIOCSIFCAP:
 1968                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
 1969 #ifdef DEVICE_POLLING
 1970                 if (ifr->ifr_reqcap & IFCAP_POLLING &&
 1971                     !(ifp->if_capenable & IFCAP_POLLING)) {
 1972                         error = ether_poll_register(rl_poll, ifp);
 1973                         if (error)
 1974                                 return(error);
 1975                         RL_LOCK(sc);
 1976                         /* Disable interrupts */
 1977                         CSR_WRITE_2(sc, RL_IMR, 0x0000);
 1978                         ifp->if_capenable |= IFCAP_POLLING;
 1979                         RL_UNLOCK(sc);
 1980                         return (error);
 1981                         
 1982                 }
 1983                 if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
 1984                     ifp->if_capenable & IFCAP_POLLING) {
 1985                         error = ether_poll_deregister(ifp);
 1986                         /* Enable interrupts. */
 1987                         RL_LOCK(sc);
 1988                         CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
 1989                         ifp->if_capenable &= ~IFCAP_POLLING;
 1990                         RL_UNLOCK(sc);
 1991                         return (error);
 1992                 }
 1993 #endif /* DEVICE_POLLING */
 1994                 if ((mask & IFCAP_WOL) != 0 &&
 1995                     (ifp->if_capabilities & IFCAP_WOL) != 0) {
 1996                         if ((mask & IFCAP_WOL_UCAST) != 0)
 1997                                 ifp->if_capenable ^= IFCAP_WOL_UCAST;
 1998                         if ((mask & IFCAP_WOL_MCAST) != 0)
 1999                                 ifp->if_capenable ^= IFCAP_WOL_MCAST;
 2000                         if ((mask & IFCAP_WOL_MAGIC) != 0)
 2001                                 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
 2002                 }
 2003                 break;
 2004         default:
 2005                 error = ether_ioctl(ifp, command, data);
 2006                 break;
 2007         }
 2008 
 2009         return (error);
 2010 }
 2011 
 2012 static void
 2013 rl_watchdog(struct rl_softc *sc)
 2014 {
 2015 
 2016         RL_LOCK_ASSERT(sc);
 2017 
 2018         if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer >0)
 2019                 return;
 2020 
 2021         device_printf(sc->rl_dev, "watchdog timeout\n");
 2022         sc->rl_ifp->if_oerrors++;
 2023 
 2024         rl_txeof(sc);
 2025         rl_rxeof(sc);
 2026         sc->rl_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 2027         rl_init_locked(sc);
 2028 }
 2029 
 2030 /*
 2031  * Stop the adapter and free any mbufs allocated to the
 2032  * RX and TX lists.
 2033  */
 2034 static void
 2035 rl_stop(struct rl_softc *sc)
 2036 {
 2037         register int            i;
 2038         struct ifnet            *ifp = sc->rl_ifp;
 2039 
 2040         RL_LOCK_ASSERT(sc);
 2041 
 2042         sc->rl_watchdog_timer = 0;
 2043         callout_stop(&sc->rl_stat_callout);
 2044         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
 2045         sc->rl_flags &= ~RL_FLAG_LINK;
 2046 
 2047         CSR_WRITE_1(sc, RL_COMMAND, 0x00);
 2048         CSR_WRITE_2(sc, RL_IMR, 0x0000);
 2049         for (i = 0; i < RL_TIMEOUT; i++) {
 2050                 DELAY(10);
 2051                 if ((CSR_READ_1(sc, RL_COMMAND) &
 2052                     (RL_CMD_RX_ENB | RL_CMD_TX_ENB)) == 0)
 2053                         break;
 2054         }
 2055         if (i == RL_TIMEOUT)
 2056                 device_printf(sc->rl_dev, "Unable to stop Tx/Rx MAC\n");
 2057 
 2058         /*
 2059          * Free the TX list buffers.
 2060          */
 2061         for (i = 0; i < RL_TX_LIST_CNT; i++) {
 2062                 if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
 2063                         if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
 2064                                 bus_dmamap_sync(sc->rl_cdata.rl_tx_tag,
 2065                                     sc->rl_cdata.rl_tx_dmamap[i],
 2066                                     BUS_DMASYNC_POSTWRITE);
 2067                                 bus_dmamap_unload(sc->rl_cdata.rl_tx_tag,
 2068                                     sc->rl_cdata.rl_tx_dmamap[i]);
 2069                                 m_freem(sc->rl_cdata.rl_tx_chain[i]);
 2070                                 sc->rl_cdata.rl_tx_chain[i] = NULL;
 2071                         }
 2072                         CSR_WRITE_4(sc, RL_TXADDR0 + (i * sizeof(uint32_t)),
 2073                             0x0000000);
 2074                 }
 2075         }
 2076 }
 2077 
 2078 /*
 2079  * Device suspend routine.  Stop the interface and save some PCI
 2080  * settings in case the BIOS doesn't restore them properly on
 2081  * resume.
 2082  */
 2083 static int
 2084 rl_suspend(device_t dev)
 2085 {
 2086         struct rl_softc         *sc;
 2087 
 2088         sc = device_get_softc(dev);
 2089 
 2090         RL_LOCK(sc);
 2091         rl_stop(sc);
 2092         rl_setwol(sc);
 2093         sc->suspended = 1;
 2094         RL_UNLOCK(sc);
 2095 
 2096         return (0);
 2097 }
 2098 
 2099 /*
 2100  * Device resume routine.  Restore some PCI settings in case the BIOS
 2101  * doesn't, re-enable busmastering, and restart the interface if
 2102  * appropriate.
 2103  */
 2104 static int
 2105 rl_resume(device_t dev)
 2106 {
 2107         struct rl_softc         *sc;
 2108         struct ifnet            *ifp;
 2109         int                     pmc;
 2110         uint16_t                pmstat;
 2111 
 2112         sc = device_get_softc(dev);
 2113         ifp = sc->rl_ifp;
 2114 
 2115         RL_LOCK(sc);
 2116 
 2117         if ((ifp->if_capabilities & IFCAP_WOL) != 0 &&
 2118             pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) == 0) {
 2119                 /* Disable PME and clear PME status. */
 2120                 pmstat = pci_read_config(sc->rl_dev,
 2121                     pmc + PCIR_POWER_STATUS, 2);
 2122                 if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
 2123                         pmstat &= ~PCIM_PSTAT_PMEENABLE;
 2124                         pci_write_config(sc->rl_dev,
 2125                             pmc + PCIR_POWER_STATUS, pmstat, 2);
 2126                 }
 2127                 /*
 2128                  * Clear WOL matching such that normal Rx filtering
 2129                  * wouldn't interfere with WOL patterns.
 2130                  */
 2131                 rl_clrwol(sc);
 2132         }
 2133 
 2134         /* reinitialize interface if necessary */
 2135         if (ifp->if_flags & IFF_UP)
 2136                 rl_init_locked(sc);
 2137 
 2138         sc->suspended = 0;
 2139 
 2140         RL_UNLOCK(sc);
 2141 
 2142         return (0);
 2143 }
 2144 
 2145 /*
 2146  * Stop all chip I/O so that the kernel's probe routines don't
 2147  * get confused by errant DMAs when rebooting.
 2148  */
 2149 static int
 2150 rl_shutdown(device_t dev)
 2151 {
 2152         struct rl_softc         *sc;
 2153 
 2154         sc = device_get_softc(dev);
 2155 
 2156         RL_LOCK(sc);
 2157         rl_stop(sc);
 2158         /*
 2159          * Mark interface as down since otherwise we will panic if
 2160          * interrupt comes in later on, which can happen in some
 2161          * cases.
 2162          */
 2163         sc->rl_ifp->if_flags &= ~IFF_UP;
 2164         rl_setwol(sc);
 2165         RL_UNLOCK(sc);
 2166 
 2167         return (0);
 2168 }
 2169 
 2170 static void
 2171 rl_setwol(struct rl_softc *sc)
 2172 {
 2173         struct ifnet            *ifp;
 2174         int                     pmc;
 2175         uint16_t                pmstat;
 2176         uint8_t                 v;
 2177 
 2178         RL_LOCK_ASSERT(sc);
 2179 
 2180         ifp = sc->rl_ifp;
 2181         if ((ifp->if_capabilities & IFCAP_WOL) == 0)
 2182                 return;
 2183         if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
 2184                 return;
 2185 
 2186         /* Enable config register write. */
 2187         CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
 2188 
 2189         /* Enable PME. */
 2190         v = CSR_READ_1(sc, RL_CFG1);
 2191         v &= ~RL_CFG1_PME;
 2192         if ((ifp->if_capenable & IFCAP_WOL) != 0)
 2193                 v |= RL_CFG1_PME;
 2194         CSR_WRITE_1(sc, RL_CFG1, v);
 2195 
 2196         v = CSR_READ_1(sc, RL_CFG3);
 2197         v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
 2198         if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
 2199                 v |= RL_CFG3_WOL_MAGIC;
 2200         CSR_WRITE_1(sc, RL_CFG3, v);
 2201 
 2202         /* Config register write done. */
 2203         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
 2204 
 2205         v = CSR_READ_1(sc, RL_CFG5);
 2206         v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
 2207         v &= ~RL_CFG5_WOL_LANWAKE;
 2208         if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
 2209                 v |= RL_CFG5_WOL_UCAST;
 2210         if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
 2211                 v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
 2212         if ((ifp->if_capenable & IFCAP_WOL) != 0)
 2213                 v |= RL_CFG5_WOL_LANWAKE;
 2214         CSR_WRITE_1(sc, RL_CFG5, v);
 2215         /* Request PME if WOL is requested. */
 2216         pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
 2217         pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
 2218         if ((ifp->if_capenable & IFCAP_WOL) != 0)
 2219                 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
 2220         pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
 2221 }
 2222 
 2223 static void
 2224 rl_clrwol(struct rl_softc *sc)
 2225 {
 2226         struct ifnet            *ifp;
 2227         uint8_t                 v;
 2228 
 2229         ifp = sc->rl_ifp;
 2230         if ((ifp->if_capabilities & IFCAP_WOL) == 0)
 2231                 return;
 2232 
 2233         /* Enable config register write. */
 2234         CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
 2235 
 2236         v = CSR_READ_1(sc, RL_CFG3);
 2237         v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
 2238         CSR_WRITE_1(sc, RL_CFG3, v);
 2239 
 2240         /* Config register write done. */
 2241         CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
 2242 
 2243         v = CSR_READ_1(sc, RL_CFG5);
 2244         v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
 2245         v &= ~RL_CFG5_WOL_LANWAKE;
 2246         CSR_WRITE_1(sc, RL_CFG5, v);
 2247 }

Cache object: 8476508d57e96c1ab1a65fc6d10e0146


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