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/ic/i82557.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 /*      $NetBSD: i82557.c,v 1.100.2.1 2008/02/02 23:16:03 riz Exp $     */
    2 
    3 /*-
    4  * Copyright (c) 1997, 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
    9  * NASA Ames Research Center.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Copyright (c) 1995, David Greenman
   42  * Copyright (c) 2001 Jonathan Lemon <jlemon@freebsd.org>
   43  * All rights reserved.
   44  *
   45  * Redistribution and use in source and binary forms, with or without
   46  * modification, are permitted provided that the following conditions
   47  * are met:
   48  * 1. Redistributions of source code must retain the above copyright
   49  *    notice unmodified, this list of conditions, and the following
   50  *    disclaimer.
   51  * 2. Redistributions in binary form must reproduce the above copyright
   52  *    notice, this list of conditions and the following disclaimer in the
   53  *    documentation and/or other materials provided with the distribution.
   54  *
   55  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   65  * SUCH DAMAGE.
   66  *
   67  *      Id: if_fxp.c,v 1.113 2001/05/17 23:50:24 jlemon
   68  */
   69 
   70 /*
   71  * Device driver for the Intel i82557 fast Ethernet controller,
   72  * and its successors, the i82558 and i82559.
   73  */
   74 
   75 #include <sys/cdefs.h>
   76 __KERNEL_RCSID(0, "$NetBSD: i82557.c,v 1.100.2.1 2008/02/02 23:16:03 riz Exp $");
   77 
   78 #include "bpfilter.h"
   79 #include "rnd.h"
   80 
   81 #include <sys/param.h>
   82 #include <sys/systm.h>
   83 #include <sys/callout.h>
   84 #include <sys/mbuf.h>
   85 #include <sys/malloc.h>
   86 #include <sys/kernel.h>
   87 #include <sys/socket.h>
   88 #include <sys/ioctl.h>
   89 #include <sys/errno.h>
   90 #include <sys/device.h>
   91 #include <sys/syslog.h>
   92 
   93 #include <machine/endian.h>
   94 
   95 #include <uvm/uvm_extern.h>
   96 
   97 #if NRND > 0
   98 #include <sys/rnd.h>
   99 #endif
  100 
  101 #include <net/if.h>
  102 #include <net/if_dl.h>
  103 #include <net/if_media.h>
  104 #include <net/if_ether.h>
  105 
  106 #if NBPFILTER > 0
  107 #include <net/bpf.h>
  108 #endif
  109 
  110 #include <machine/bus.h>
  111 #include <machine/intr.h>
  112 
  113 #include <dev/mii/miivar.h>
  114 
  115 #include <dev/ic/i82557reg.h>
  116 #include <dev/ic/i82557var.h>
  117 
  118 #include <dev/microcode/i8255x/rcvbundl.h>
  119 
  120 /*
  121  * NOTE!  On the Alpha, we have an alignment constraint.  The
  122  * card DMAs the packet immediately following the RFA.  However,
  123  * the first thing in the packet is a 14-byte Ethernet header.
  124  * This means that the packet is misaligned.  To compensate,
  125  * we actually offset the RFA 2 bytes into the cluster.  This
  126  * alignes the packet after the Ethernet header at a 32-bit
  127  * boundary.  HOWEVER!  This means that the RFA is misaligned!
  128  */
  129 #define RFA_ALIGNMENT_FUDGE     2
  130 
  131 /*
  132  * The configuration byte map has several undefined fields which
  133  * must be one or must be zero.  Set up a template for these bits
  134  * only (assuming an i82557 chip), leaving the actual configuration
  135  * for fxp_init().
  136  *
  137  * See the definition of struct fxp_cb_config for the bit definitions.
  138  */
  139 const u_int8_t fxp_cb_config_template[] = {
  140         0x0, 0x0,               /* cb_status */
  141         0x0, 0x0,               /* cb_command */
  142         0x0, 0x0, 0x0, 0x0,     /* link_addr */
  143         0x0,    /*  0 */
  144         0x0,    /*  1 */
  145         0x0,    /*  2 */
  146         0x0,    /*  3 */
  147         0x0,    /*  4 */
  148         0x0,    /*  5 */
  149         0x32,   /*  6 */
  150         0x0,    /*  7 */
  151         0x0,    /*  8 */
  152         0x0,    /*  9 */
  153         0x6,    /* 10 */
  154         0x0,    /* 11 */
  155         0x0,    /* 12 */
  156         0x0,    /* 13 */
  157         0xf2,   /* 14 */
  158         0x48,   /* 15 */
  159         0x0,    /* 16 */
  160         0x40,   /* 17 */
  161         0xf0,   /* 18 */
  162         0x0,    /* 19 */
  163         0x3f,   /* 20 */
  164         0x5,    /* 21 */
  165         0x0,    /* 22 */
  166         0x0,    /* 23 */
  167         0x0,    /* 24 */
  168         0x0,    /* 25 */
  169         0x0,    /* 26 */
  170         0x0,    /* 27 */
  171         0x0,    /* 28 */
  172         0x0,    /* 29 */
  173         0x0,    /* 30 */
  174         0x0,    /* 31 */
  175 };
  176 
  177 void    fxp_mii_initmedia(struct fxp_softc *);
  178 int     fxp_mii_mediachange(struct ifnet *);
  179 void    fxp_mii_mediastatus(struct ifnet *, struct ifmediareq *);
  180 
  181 void    fxp_80c24_initmedia(struct fxp_softc *);
  182 int     fxp_80c24_mediachange(struct ifnet *);
  183 void    fxp_80c24_mediastatus(struct ifnet *, struct ifmediareq *);
  184 
  185 void    fxp_start(struct ifnet *);
  186 int     fxp_ioctl(struct ifnet *, u_long, caddr_t);
  187 void    fxp_watchdog(struct ifnet *);
  188 int     fxp_init(struct ifnet *);
  189 void    fxp_stop(struct ifnet *, int);
  190 
  191 void    fxp_txintr(struct fxp_softc *);
  192 int     fxp_rxintr(struct fxp_softc *);
  193 
  194 int     fxp_rx_hwcksum(struct mbuf *, const struct fxp_rfa *);
  195 
  196 void    fxp_rxdrain(struct fxp_softc *);
  197 int     fxp_add_rfabuf(struct fxp_softc *, bus_dmamap_t, int);
  198 int     fxp_mdi_read(struct device *, int, int);
  199 void    fxp_statchg(struct device *);
  200 void    fxp_mdi_write(struct device *, int, int, int);
  201 void    fxp_autosize_eeprom(struct fxp_softc*);
  202 void    fxp_read_eeprom(struct fxp_softc *, u_int16_t *, int, int);
  203 void    fxp_write_eeprom(struct fxp_softc *, u_int16_t *, int, int);
  204 void    fxp_eeprom_update_cksum(struct fxp_softc *);
  205 void    fxp_get_info(struct fxp_softc *, u_int8_t *);
  206 void    fxp_tick(void *);
  207 void    fxp_mc_setup(struct fxp_softc *);
  208 void    fxp_load_ucode(struct fxp_softc *);
  209 
  210 void    fxp_shutdown(void *);
  211 void    fxp_power(int, void *);
  212 
  213 int     fxp_copy_small = 0;
  214 
  215 /*
  216  * Variables for interrupt mitigating microcode.
  217  */
  218 int     fxp_int_delay = 1000;           /* usec */
  219 int     fxp_bundle_max = 6;             /* packets */
  220 
  221 struct fxp_phytype {
  222         int     fp_phy;         /* type of PHY, -1 for MII at the end. */
  223         void    (*fp_init)(struct fxp_softc *);
  224 } fxp_phytype_table[] = {
  225         { FXP_PHY_80C24,                fxp_80c24_initmedia },
  226         { -1,                           fxp_mii_initmedia },
  227 };
  228 
  229 /*
  230  * Set initial transmit threshold at 64 (512 bytes). This is
  231  * increased by 64 (512 bytes) at a time, to maximum of 192
  232  * (1536 bytes), if an underrun occurs.
  233  */
  234 static int tx_threshold = 64;
  235 
  236 /*
  237  * Wait for the previous command to be accepted (but not necessarily
  238  * completed).
  239  */
  240 static inline void
  241 fxp_scb_wait(struct fxp_softc *sc)
  242 {
  243         int i = 10000;
  244 
  245         while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
  246                 delay(2);
  247         if (i == 0)
  248                 log(LOG_WARNING,
  249                     "%s: WARNING: SCB timed out!\n", sc->sc_dev.dv_xname);
  250 }
  251 
  252 /*
  253  * Submit a command to the i82557.
  254  */
  255 static inline void
  256 fxp_scb_cmd(struct fxp_softc *sc, u_int8_t cmd)
  257 {
  258 
  259         CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
  260 }
  261 
  262 /*
  263  * Finish attaching an i82557 interface.  Called by bus-specific front-end.
  264  */
  265 void
  266 fxp_attach(struct fxp_softc *sc)
  267 {
  268         u_int8_t enaddr[ETHER_ADDR_LEN];
  269         struct ifnet *ifp;
  270         bus_dma_segment_t seg;
  271         int rseg, i, error;
  272         struct fxp_phytype *fp;
  273 
  274         callout_init(&sc->sc_callout);
  275 
  276         /*
  277          * Enable some good stuff on i82558 and later.
  278          */
  279         if (sc->sc_rev >= FXP_REV_82558_A4) {
  280                 /* Enable the extended TxCB. */
  281                 sc->sc_flags |= FXPF_EXT_TXCB;
  282         }
  283 
  284         /*
  285          * Enable use of extended RFDs and TCBs for 82550
  286          * and later chips. Note: we need extended TXCB support
  287          * too, but that's already enabled by the code above.
  288          * Be careful to do this only on the right devices.
  289          */
  290         if (sc->sc_rev == FXP_REV_82550 || sc->sc_rev == FXP_REV_82550_C) {
  291                 sc->sc_flags |= FXPF_EXT_RFA | FXPF_IPCB;
  292                 sc->sc_txcmd = htole16(FXP_CB_COMMAND_IPCBXMIT);
  293         } else {
  294                 sc->sc_txcmd = htole16(FXP_CB_COMMAND_XMIT);
  295         }
  296 
  297         sc->sc_rfa_size =
  298             (sc->sc_flags & FXPF_EXT_RFA) ? RFA_EXT_SIZE : RFA_SIZE;
  299 
  300         /*
  301          * Allocate the control data structures, and create and load the
  302          * DMA map for it.
  303          */
  304         if ((error = bus_dmamem_alloc(sc->sc_dmat,
  305             sizeof(struct fxp_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
  306             0)) != 0) {
  307                 aprint_error(
  308                     "%s: unable to allocate control data, error = %d\n",
  309                     sc->sc_dev.dv_xname, error);
  310                 goto fail_0;
  311         }
  312 
  313         if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
  314             sizeof(struct fxp_control_data), (caddr_t *)&sc->sc_control_data,
  315             BUS_DMA_COHERENT)) != 0) {
  316                 aprint_error("%s: unable to map control data, error = %d\n",
  317                     sc->sc_dev.dv_xname, error);
  318                 goto fail_1;
  319         }
  320         sc->sc_cdseg = seg;
  321         sc->sc_cdnseg = rseg;
  322 
  323         memset(sc->sc_control_data, 0, sizeof(struct fxp_control_data));
  324 
  325         if ((error = bus_dmamap_create(sc->sc_dmat,
  326             sizeof(struct fxp_control_data), 1,
  327             sizeof(struct fxp_control_data), 0, 0, &sc->sc_dmamap)) != 0) {
  328                 aprint_error("%s: unable to create control data DMA map, "
  329                     "error = %d\n", sc->sc_dev.dv_xname, error);
  330                 goto fail_2;
  331         }
  332 
  333         if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
  334             sc->sc_control_data, sizeof(struct fxp_control_data), NULL,
  335             0)) != 0) {
  336                 aprint_error(
  337                     "%s: can't load control data DMA map, error = %d\n",
  338                     sc->sc_dev.dv_xname, error);
  339                 goto fail_3;
  340         }
  341 
  342         /*
  343          * Create the transmit buffer DMA maps.
  344          */
  345         for (i = 0; i < FXP_NTXCB; i++) {
  346                 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
  347                     (sc->sc_flags & FXPF_IPCB) ? FXP_IPCB_NTXSEG : FXP_NTXSEG,
  348                     MCLBYTES, 0, 0, &FXP_DSTX(sc, i)->txs_dmamap)) != 0) {
  349                         aprint_error("%s: unable to create tx DMA map %d, "
  350                             "error = %d\n", sc->sc_dev.dv_xname, i, error);
  351                         goto fail_4;
  352                 }
  353         }
  354 
  355         /*
  356          * Create the receive buffer DMA maps.
  357          */
  358         for (i = 0; i < FXP_NRFABUFS; i++) {
  359                 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
  360                     MCLBYTES, 0, 0, &sc->sc_rxmaps[i])) != 0) {
  361                         aprint_error("%s: unable to create rx DMA map %d, "
  362                             "error = %d\n", sc->sc_dev.dv_xname, i, error);
  363                         goto fail_5;
  364                 }
  365         }
  366 
  367         /* Initialize MAC address and media structures. */
  368         fxp_get_info(sc, enaddr);
  369 
  370         aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
  371             ether_sprintf(enaddr));
  372 
  373         ifp = &sc->sc_ethercom.ec_if;
  374 
  375         /*
  376          * Get info about our media interface, and initialize it.  Note
  377          * the table terminates itself with a phy of -1, indicating
  378          * that we're using MII.
  379          */
  380         for (fp = fxp_phytype_table; fp->fp_phy != -1; fp++)
  381                 if (fp->fp_phy == sc->phy_primary_device)
  382                         break;
  383         (*fp->fp_init)(sc);
  384 
  385         strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
  386         ifp->if_softc = sc;
  387         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  388         ifp->if_ioctl = fxp_ioctl;
  389         ifp->if_start = fxp_start;
  390         ifp->if_watchdog = fxp_watchdog;
  391         ifp->if_init = fxp_init;
  392         ifp->if_stop = fxp_stop;
  393         IFQ_SET_READY(&ifp->if_snd);
  394 
  395         if (sc->sc_flags & FXPF_IPCB) {
  396                 KASSERT(sc->sc_flags & FXPF_EXT_RFA); /* we have both or none */
  397                 /*
  398                  * IFCAP_CSUM_IPv4_Tx seems to have a problem,
  399                  * at least, on i82550 rev.12.
  400                  * specifically, it doesn't calculate ipv4 checksum correctly
  401                  * when sending 20 byte ipv4 header + 1 or 2 byte data.
  402                  * FreeBSD driver has related comments.
  403                  */
  404                 ifp->if_capabilities =
  405                     IFCAP_CSUM_IPv4_Rx |
  406                     IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
  407                     IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
  408                 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
  409         }
  410 
  411         /*
  412          * We can support 802.1Q VLAN-sized frames.
  413          */
  414         sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
  415 
  416         /*
  417          * Attach the interface.
  418          */
  419         if_attach(ifp);
  420         ether_ifattach(ifp, enaddr);
  421 #if NRND > 0
  422         rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
  423             RND_TYPE_NET, 0);
  424 #endif
  425 
  426 #ifdef FXP_EVENT_COUNTERS
  427         evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
  428             NULL, sc->sc_dev.dv_xname, "txstall");
  429         evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
  430             NULL, sc->sc_dev.dv_xname, "txintr");
  431         evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
  432             NULL, sc->sc_dev.dv_xname, "rxintr");
  433         if (sc->sc_rev >= FXP_REV_82558_A4) {
  434                 evcnt_attach_dynamic(&sc->sc_ev_txpause, EVCNT_TYPE_MISC,
  435                     NULL, sc->sc_dev.dv_xname, "txpause");
  436                 evcnt_attach_dynamic(&sc->sc_ev_rxpause, EVCNT_TYPE_MISC,
  437                     NULL, sc->sc_dev.dv_xname, "rxpause");
  438         }
  439 #endif /* FXP_EVENT_COUNTERS */
  440 
  441         /*
  442          * Add shutdown hook so that DMA is disabled prior to reboot. Not
  443          * doing do could allow DMA to corrupt kernel memory during the
  444          * reboot before the driver initializes.
  445          */
  446         sc->sc_sdhook = shutdownhook_establish(fxp_shutdown, sc);
  447         if (sc->sc_sdhook == NULL)
  448                 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
  449                     sc->sc_dev.dv_xname);
  450         /*
  451          * Add suspend hook, for similar reasons..
  452          */
  453         sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
  454             fxp_power, sc);
  455         if (sc->sc_powerhook == NULL)
  456                 aprint_error("%s: WARNING: unable to establish power hook\n",
  457                     sc->sc_dev.dv_xname);
  458 
  459         /* The attach is successful. */
  460         sc->sc_flags |= FXPF_ATTACHED;
  461 
  462         return;
  463 
  464         /*
  465          * Free any resources we've allocated during the failed attach
  466          * attempt.  Do this in reverse order and fall though.
  467          */
  468  fail_5:
  469         for (i = 0; i < FXP_NRFABUFS; i++) {
  470                 if (sc->sc_rxmaps[i] != NULL)
  471                         bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
  472         }
  473  fail_4:
  474         for (i = 0; i < FXP_NTXCB; i++) {
  475                 if (FXP_DSTX(sc, i)->txs_dmamap != NULL)
  476                         bus_dmamap_destroy(sc->sc_dmat,
  477                             FXP_DSTX(sc, i)->txs_dmamap);
  478         }
  479         bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
  480  fail_3:
  481         bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
  482  fail_2:
  483         bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
  484             sizeof(struct fxp_control_data));
  485  fail_1:
  486         bus_dmamem_free(sc->sc_dmat, &seg, rseg);
  487  fail_0:
  488         return;
  489 }
  490 
  491 void
  492 fxp_mii_initmedia(struct fxp_softc *sc)
  493 {
  494         int flags;
  495 
  496         sc->sc_flags |= FXPF_MII;
  497 
  498         sc->sc_mii.mii_ifp = &sc->sc_ethercom.ec_if;
  499         sc->sc_mii.mii_readreg = fxp_mdi_read;
  500         sc->sc_mii.mii_writereg = fxp_mdi_write;
  501         sc->sc_mii.mii_statchg = fxp_statchg;
  502         ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, fxp_mii_mediachange,
  503             fxp_mii_mediastatus);
  504 
  505         flags = MIIF_NOISOLATE;
  506         if (sc->sc_rev >= FXP_REV_82558_A4)
  507                 flags |= MIIF_DOPAUSE;
  508         /*
  509          * The i82557 wedges if all of its PHYs are isolated!
  510          */
  511         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
  512             MII_OFFSET_ANY, flags);
  513         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
  514                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
  515                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
  516         } else
  517                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
  518 }
  519 
  520 void
  521 fxp_80c24_initmedia(struct fxp_softc *sc)
  522 {
  523 
  524         /*
  525          * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
  526          * doesn't have a programming interface of any sort.  The
  527          * media is sensed automatically based on how the link partner
  528          * is configured.  This is, in essence, manual configuration.
  529          */
  530         aprint_normal("%s: Seeq 80c24 AutoDUPLEX media interface present\n",
  531             sc->sc_dev.dv_xname);
  532         ifmedia_init(&sc->sc_mii.mii_media, 0, fxp_80c24_mediachange,
  533             fxp_80c24_mediastatus);
  534         ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
  535         ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
  536 }
  537 
  538 /*
  539  * Device shutdown routine. Called at system shutdown after sync. The
  540  * main purpose of this routine is to shut off receiver DMA so that
  541  * kernel memory doesn't get clobbered during warmboot.
  542  */
  543 void
  544 fxp_shutdown(void *arg)
  545 {
  546         struct fxp_softc *sc = arg;
  547 
  548         /*
  549          * Since the system's going to halt shortly, don't bother
  550          * freeing mbufs.
  551          */
  552         fxp_stop(&sc->sc_ethercom.ec_if, 0);
  553 }
  554 /*
  555  * Power handler routine. Called when the system is transitioning
  556  * into/out of power save modes.  As with fxp_shutdown, the main
  557  * purpose of this routine is to shut off receiver DMA so it doesn't
  558  * clobber kernel memory at the wrong time.
  559  */
  560 void
  561 fxp_power(int why, void *arg)
  562 {
  563         struct fxp_softc *sc = arg;
  564         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
  565         int s;
  566 
  567         s = splnet();
  568         switch (why) {
  569         case PWR_SUSPEND:
  570         case PWR_STANDBY:
  571                 fxp_stop(ifp, 0);
  572                 break;
  573         case PWR_RESUME:
  574                 if (ifp->if_flags & IFF_UP)
  575                         fxp_init(ifp);
  576                 break;
  577         case PWR_SOFTSUSPEND:
  578         case PWR_SOFTSTANDBY:
  579         case PWR_SOFTRESUME:
  580                 break;
  581         }
  582         splx(s);
  583 }
  584 
  585 /*
  586  * Initialize the interface media.
  587  */
  588 void
  589 fxp_get_info(struct fxp_softc *sc, u_int8_t *enaddr)
  590 {
  591         u_int16_t data, myea[ETHER_ADDR_LEN / 2];
  592 
  593         /*
  594          * Reset to a stable state.
  595          */
  596         CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
  597         DELAY(100);
  598 
  599         sc->sc_eeprom_size = 0;
  600         fxp_autosize_eeprom(sc);
  601         if (sc->sc_eeprom_size == 0) {
  602                 aprint_error("%s: failed to detect EEPROM size\n",
  603                     sc->sc_dev.dv_xname);
  604                 sc->sc_eeprom_size = 6; /* XXX panic here? */
  605         }
  606 #ifdef DEBUG
  607         aprint_debug("%s: detected %d word EEPROM\n",
  608             sc->sc_dev.dv_xname, 1 << sc->sc_eeprom_size);
  609 #endif
  610 
  611         /*
  612          * Get info about the primary PHY
  613          */
  614         fxp_read_eeprom(sc, &data, 6, 1);
  615         sc->phy_primary_device =
  616             (data & FXP_PHY_DEVICE_MASK) >> FXP_PHY_DEVICE_SHIFT;
  617 
  618         /*
  619          * Read MAC address.
  620          */
  621         fxp_read_eeprom(sc, myea, 0, 3);
  622         enaddr[0] = myea[0] & 0xff;
  623         enaddr[1] = myea[0] >> 8;
  624         enaddr[2] = myea[1] & 0xff;
  625         enaddr[3] = myea[1] >> 8;
  626         enaddr[4] = myea[2] & 0xff;
  627         enaddr[5] = myea[2] >> 8;
  628 
  629         /*
  630          * Systems based on the ICH2/ICH2-M chip from Intel, as well
  631          * as some i82559 designs, have a defect where the chip can
  632          * cause a PCI protocol violation if it receives a CU_RESUME
  633          * command when it is entering the IDLE state.
  634          *
  635          * The work-around is to disable Dynamic Standby Mode, so that
  636          * the chip never deasserts #CLKRUN, and always remains in the
  637          * active state.
  638          *
  639          * Unfortunately, the only way to disable Dynamic Standby is
  640          * to frob an EEPROM setting and reboot (the EEPROM setting
  641          * is only consulted when the PCI bus comes out of reset).
  642          *
  643          * See Intel 82801BA/82801BAM Specification Update, Errata #30.
  644          */
  645         if (sc->sc_flags & FXPF_HAS_RESUME_BUG) {
  646                 fxp_read_eeprom(sc, &data, 10, 1);
  647                 if (data & 0x02) {              /* STB enable */
  648                         aprint_error("%s: WARNING: "
  649                             "Disabling dynamic standby mode in EEPROM "
  650                             "to work around a\n",
  651                             sc->sc_dev.dv_xname);
  652                         aprint_normal(
  653                             "%s: WARNING: hardware bug.  You must reset "
  654                             "the system before using this\n",
  655                             sc->sc_dev.dv_xname);
  656                         aprint_normal("%s: WARNING: interface.\n",
  657                             sc->sc_dev.dv_xname);
  658                         data &= ~0x02;
  659                         fxp_write_eeprom(sc, &data, 10, 1);
  660                         aprint_normal("%s: new EEPROM ID: 0x%04x\n",
  661                             sc->sc_dev.dv_xname, data);
  662                         fxp_eeprom_update_cksum(sc);
  663                 }
  664         }
  665 
  666         /* Receiver lock-up workaround detection. (FXPF_RECV_WORKAROUND) */
  667         /* Due to false positives we make it conditional on setting link1 */
  668         fxp_read_eeprom(sc, &data, 3, 1);
  669         if ((data & 0x03) != 0x03) {
  670                 aprint_verbose("%s: May need receiver lock-up workaround\n",
  671                     sc->sc_dev.dv_xname);
  672         }
  673 }
  674 
  675 static void
  676 fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int len)
  677 {
  678         uint16_t reg;
  679         int x;
  680 
  681         for (x = 1 << (len - 1); x != 0; x >>= 1) {
  682                 DELAY(40);
  683                 if (data & x)
  684                         reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
  685                 else
  686                         reg = FXP_EEPROM_EECS;
  687                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
  688                 DELAY(40);
  689                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
  690                     reg | FXP_EEPROM_EESK);
  691                 DELAY(40);
  692                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
  693         }
  694         DELAY(40);
  695 }
  696 
  697 /*
  698  * Figure out EEPROM size.
  699  *
  700  * 559's can have either 64-word or 256-word EEPROMs, the 558
  701  * datasheet only talks about 64-word EEPROMs, and the 557 datasheet
  702  * talks about the existence of 16 to 256 word EEPROMs.
  703  *
  704  * The only known sizes are 64 and 256, where the 256 version is used
  705  * by CardBus cards to store CIS information.
  706  *
  707  * The address is shifted in msb-to-lsb, and after the last
  708  * address-bit the EEPROM is supposed to output a `dummy zero' bit,
  709  * after which follows the actual data. We try to detect this zero, by
  710  * probing the data-out bit in the EEPROM control register just after
  711  * having shifted in a bit. If the bit is zero, we assume we've
  712  * shifted enough address bits. The data-out should be tri-state,
  713  * before this, which should translate to a logical one.
  714  *
  715  * Other ways to do this would be to try to read a register with known
  716  * contents with a varying number of address bits, but no such
  717  * register seem to be available. The high bits of register 10 are 01
  718  * on the 558 and 559, but apparently not on the 557.
  719  *
  720  * The Linux driver computes a checksum on the EEPROM data, but the
  721  * value of this checksum is not very well documented.
  722  */
  723 
  724 void
  725 fxp_autosize_eeprom(struct fxp_softc *sc)
  726 {
  727         int x;
  728 
  729         CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  730         DELAY(40);
  731 
  732         /* Shift in read opcode. */
  733         fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
  734 
  735         /*
  736          * Shift in address, wait for the dummy zero following a correct
  737          * address shift.
  738          */
  739         for (x = 1; x <= 8; x++) {
  740                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  741                 DELAY(40);
  742                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
  743                     FXP_EEPROM_EECS | FXP_EEPROM_EESK);
  744                 DELAY(40);
  745                 if ((CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
  746                     FXP_EEPROM_EEDO) == 0)
  747                         break;
  748                 DELAY(40);
  749                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  750                 DELAY(40);
  751         }
  752         CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  753         DELAY(40);
  754         if (x != 6 && x != 8) {
  755 #ifdef DEBUG
  756                 printf("%s: strange EEPROM size (%d)\n",
  757                     sc->sc_dev.dv_xname, 1 << x);
  758 #endif
  759         } else
  760                 sc->sc_eeprom_size = x;
  761 }
  762 
  763 /*
  764  * Read from the serial EEPROM. Basically, you manually shift in
  765  * the read opcode (one bit at a time) and then shift in the address,
  766  * and then you shift out the data (all of this one bit at a time).
  767  * The word size is 16 bits, so you have to provide the address for
  768  * every 16 bits of data.
  769  */
  770 void
  771 fxp_read_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
  772 {
  773         u_int16_t reg;
  774         int i, x;
  775 
  776         for (i = 0; i < words; i++) {
  777                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  778 
  779                 /* Shift in read opcode. */
  780                 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
  781 
  782                 /* Shift in address. */
  783                 fxp_eeprom_shiftin(sc, i + offset, sc->sc_eeprom_size);
  784 
  785                 reg = FXP_EEPROM_EECS;
  786                 data[i] = 0;
  787 
  788                 /* Shift out data. */
  789                 for (x = 16; x > 0; x--) {
  790                         CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL,
  791                             reg | FXP_EEPROM_EESK);
  792                         DELAY(40);
  793                         if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
  794                             FXP_EEPROM_EEDO)
  795                                 data[i] |= (1 << (x - 1));
  796                         CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
  797                         DELAY(40);
  798                 }
  799                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  800                 DELAY(40);
  801         }
  802 }
  803 
  804 /*
  805  * Write data to the serial EEPROM.
  806  */
  807 void
  808 fxp_write_eeprom(struct fxp_softc *sc, u_int16_t *data, int offset, int words)
  809 {
  810         int i, j;
  811 
  812         for (i = 0; i < words; i++) {
  813                 /* Erase/write enable. */
  814                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  815                 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
  816                 fxp_eeprom_shiftin(sc, 0x3 << (sc->sc_eeprom_size - 2),
  817                     sc->sc_eeprom_size);
  818                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  819                 DELAY(4);
  820 
  821                 /* Shift in write opcode, address, data. */
  822                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  823                 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
  824                 fxp_eeprom_shiftin(sc, offset, sc->sc_eeprom_size);
  825                 fxp_eeprom_shiftin(sc, data[i], 16);
  826                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  827                 DELAY(4);
  828 
  829                 /* Wait for the EEPROM to finish up. */
  830                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  831                 DELAY(4);
  832                 for (j = 0; j < 1000; j++) {
  833                         if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) &
  834                             FXP_EEPROM_EEDO)
  835                                 break;
  836                         DELAY(50);
  837                 }
  838                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  839                 DELAY(4);
  840 
  841                 /* Erase/write disable. */
  842                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
  843                 fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_ERASE, 3);
  844                 fxp_eeprom_shiftin(sc, 0, sc->sc_eeprom_size);
  845                 CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
  846                 DELAY(4);
  847         }
  848 }
  849 
  850 /*
  851  * Update the checksum of the EEPROM.
  852  */
  853 void
  854 fxp_eeprom_update_cksum(struct fxp_softc *sc)
  855 {
  856         int i;
  857         uint16_t data, cksum;
  858 
  859         cksum = 0;
  860         for (i = 0; i < (1 << sc->sc_eeprom_size) - 1; i++) {
  861                 fxp_read_eeprom(sc, &data, i, 1);
  862                 cksum += data;
  863         }
  864         i = (1 << sc->sc_eeprom_size) - 1;
  865         cksum = 0xbaba - cksum;
  866         fxp_read_eeprom(sc, &data, i, 1);
  867         fxp_write_eeprom(sc, &cksum, i, 1);
  868         log(LOG_INFO, "%s: EEPROM checksum @ 0x%x: 0x%04x -> 0x%04x\n",
  869             sc->sc_dev.dv_xname, i, data, cksum);
  870 }
  871 
  872 /*
  873  * Start packet transmission on the interface.
  874  */
  875 void
  876 fxp_start(struct ifnet *ifp)
  877 {
  878         struct fxp_softc *sc = ifp->if_softc;
  879         struct mbuf *m0, *m;
  880         struct fxp_txdesc *txd;
  881         struct fxp_txsoft *txs;
  882         bus_dmamap_t dmamap;
  883         int error, lasttx, nexttx, opending, seg;
  884 
  885         /*
  886          * If we want a re-init, bail out now.
  887          */
  888         if (sc->sc_flags & FXPF_WANTINIT) {
  889                 ifp->if_flags |= IFF_OACTIVE;
  890                 return;
  891         }
  892 
  893         if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
  894                 return;
  895 
  896         /*
  897          * Remember the previous txpending and the current lasttx.
  898          */
  899         opending = sc->sc_txpending;
  900         lasttx = sc->sc_txlast;
  901 
  902         /*
  903          * Loop through the send queue, setting up transmit descriptors
  904          * until we drain the queue, or use up all available transmit
  905          * descriptors.
  906          */
  907         for (;;) {
  908                 struct fxp_tbd *tbdp;
  909                 int csum_flags;
  910 
  911                 /*
  912                  * Grab a packet off the queue.
  913                  */
  914                 IFQ_POLL(&ifp->if_snd, m0);
  915                 if (m0 == NULL)
  916                         break;
  917                 m = NULL;
  918 
  919                 if (sc->sc_txpending == FXP_NTXCB - 1) {
  920                         FXP_EVCNT_INCR(&sc->sc_ev_txstall);
  921                         break;
  922                 }
  923 
  924                 /*
  925                  * Get the next available transmit descriptor.
  926                  */
  927                 nexttx = FXP_NEXTTX(sc->sc_txlast);
  928                 txd = FXP_CDTX(sc, nexttx);
  929                 txs = FXP_DSTX(sc, nexttx);
  930                 dmamap = txs->txs_dmamap;
  931 
  932                 /*
  933                  * Load the DMA map.  If this fails, the packet either
  934                  * didn't fit in the allotted number of frags, or we were
  935                  * short on resources.  In this case, we'll copy and try
  936                  * again.
  937                  */
  938                 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
  939                     BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
  940                         MGETHDR(m, M_DONTWAIT, MT_DATA);
  941                         if (m == NULL) {
  942                                 log(LOG_ERR, "%s: unable to allocate Tx mbuf\n",
  943                                     sc->sc_dev.dv_xname);
  944                                 break;
  945                         }
  946                         MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
  947                         if (m0->m_pkthdr.len > MHLEN) {
  948                                 MCLGET(m, M_DONTWAIT);
  949                                 if ((m->m_flags & M_EXT) == 0) {
  950                                         log(LOG_ERR,
  951                                             "%s: unable to allocate Tx "
  952                                             "cluster\n", sc->sc_dev.dv_xname);
  953                                         m_freem(m);
  954                                         break;
  955                                 }
  956                         }
  957                         m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
  958                         m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
  959                         error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
  960                             m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
  961                         if (error) {
  962                                 log(LOG_ERR, "%s: unable to load Tx buffer, "
  963                                     "error = %d\n", sc->sc_dev.dv_xname, error);
  964                                 break;
  965                         }
  966                 }
  967 
  968                 IFQ_DEQUEUE(&ifp->if_snd, m0);
  969                 csum_flags = m0->m_pkthdr.csum_flags;
  970                 if (m != NULL) {
  971                         m_freem(m0);
  972                         m0 = m;
  973                 }
  974 
  975                 /* Initialize the fraglist. */
  976                 tbdp = txd->txd_tbd;
  977                 if (sc->sc_flags & FXPF_IPCB)
  978                         tbdp++;
  979                 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
  980                         tbdp[seg].tb_addr =
  981                             htole32(dmamap->dm_segs[seg].ds_addr);
  982                         tbdp[seg].tb_size =
  983                             htole32(dmamap->dm_segs[seg].ds_len);
  984                 }
  985 
  986                 /* Sync the DMA map. */
  987                 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
  988                     BUS_DMASYNC_PREWRITE);
  989 
  990                 /*
  991                  * Store a pointer to the packet so we can free it later.
  992                  */
  993                 txs->txs_mbuf = m0;
  994 
  995                 /*
  996                  * Initialize the transmit descriptor.
  997                  */
  998                 /* BIG_ENDIAN: no need to swap to store 0 */
  999                 txd->txd_txcb.cb_status = 0;
 1000                 txd->txd_txcb.cb_command =
 1001                     sc->sc_txcmd | htole16(FXP_CB_COMMAND_SF);
 1002                 txd->txd_txcb.tx_threshold = tx_threshold;
 1003                 txd->txd_txcb.tbd_number = dmamap->dm_nsegs;
 1004 
 1005                 KASSERT((csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) == 0);
 1006                 if (sc->sc_flags & FXPF_IPCB) {
 1007                         struct m_tag *vtag;
 1008                         struct fxp_ipcb *ipcb;
 1009                         /*
 1010                          * Deal with TCP/IP checksum offload. Note that
 1011                          * in order for TCP checksum offload to work,
 1012                          * the pseudo header checksum must have already
 1013                          * been computed and stored in the checksum field
 1014                          * in the TCP header. The stack should have
 1015                          * already done this for us.
 1016                          */
 1017                         ipcb = &txd->txd_u.txdu_ipcb;
 1018                         memset(ipcb, 0, sizeof(*ipcb));
 1019                         /*
 1020                          * always do hardware parsing.
 1021                          */
 1022                         ipcb->ipcb_ip_activation_high =
 1023                             FXP_IPCB_HARDWAREPARSING_ENABLE;
 1024                         /*
 1025                          * ip checksum offloading.
 1026                          */
 1027                         if (csum_flags & M_CSUM_IPv4) {
 1028                                 ipcb->ipcb_ip_schedule |=
 1029                                     FXP_IPCB_IP_CHECKSUM_ENABLE;
 1030                         }
 1031                         /*
 1032                          * TCP/UDP checksum offloading.
 1033                          */
 1034                         if (csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
 1035                                 ipcb->ipcb_ip_schedule |=
 1036                                     FXP_IPCB_TCPUDP_CHECKSUM_ENABLE;
 1037                         }
 1038 
 1039                         /*
 1040                          * request VLAN tag insertion if needed.
 1041                          */
 1042                         vtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0);
 1043                         if (vtag) {
 1044                                 ipcb->ipcb_vlan_id =
 1045                                     htobe16(*(u_int *)(vtag + 1));
 1046                                 ipcb->ipcb_ip_activation_high |=
 1047                                     FXP_IPCB_INSERTVLAN_ENABLE;
 1048                         }
 1049                 } else {
 1050                         KASSERT((csum_flags &
 1051                             (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) == 0);
 1052                 }
 1053 
 1054                 FXP_CDTXSYNC(sc, nexttx,
 1055                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1056 
 1057                 /* Advance the tx pointer. */
 1058                 sc->sc_txpending++;
 1059                 sc->sc_txlast = nexttx;
 1060 
 1061 #if NBPFILTER > 0
 1062                 /*
 1063                  * Pass packet to bpf if there is a listener.
 1064                  */
 1065                 if (ifp->if_bpf)
 1066                         bpf_mtap(ifp->if_bpf, m0);
 1067 #endif
 1068         }
 1069 
 1070         if (sc->sc_txpending == FXP_NTXCB - 1) {
 1071                 /* No more slots; notify upper layer. */
 1072                 ifp->if_flags |= IFF_OACTIVE;
 1073         }
 1074 
 1075         if (sc->sc_txpending != opending) {
 1076                 /*
 1077                  * We enqueued packets.  If the transmitter was idle,
 1078                  * reset the txdirty pointer.
 1079                  */
 1080                 if (opending == 0)
 1081                         sc->sc_txdirty = FXP_NEXTTX(lasttx);
 1082 
 1083                 /*
 1084                  * Cause the chip to interrupt and suspend command
 1085                  * processing once the last packet we've enqueued
 1086                  * has been transmitted.
 1087                  *
 1088                  * To avoid a race between updating status bits
 1089                  * by the fxp chip and clearing command bits
 1090                  * by this function on machines which don't have
 1091                  * atomic methods to clear/set bits in memory
 1092                  * smaller than 32bits (both cb_status and cb_command
 1093                  * members are uint16_t and in the same 32bit word),
 1094                  * we have to prepare a dummy TX descriptor which has
 1095                  * NOP command and just causes a TX completion interrupt.
 1096                  */
 1097                 sc->sc_txpending++;
 1098                 sc->sc_txlast = FXP_NEXTTX(sc->sc_txlast);
 1099                 txd = FXP_CDTX(sc, sc->sc_txlast);
 1100                 /* BIG_ENDIAN: no need to swap to store 0 */
 1101                 txd->txd_txcb.cb_status = 0;
 1102                 txd->txd_txcb.cb_command = htole16(FXP_CB_COMMAND_NOP |
 1103                     FXP_CB_COMMAND_I | FXP_CB_COMMAND_S);
 1104                 FXP_CDTXSYNC(sc, sc->sc_txlast,
 1105                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1106 
 1107                 /*
 1108                  * The entire packet chain is set up.  Clear the suspend bit
 1109                  * on the command prior to the first packet we set up.
 1110                  */
 1111                 FXP_CDTXSYNC(sc, lasttx,
 1112                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1113                 FXP_CDTX(sc, lasttx)->txd_txcb.cb_command &=
 1114                     htole16(~FXP_CB_COMMAND_S);
 1115                 FXP_CDTXSYNC(sc, lasttx,
 1116                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1117 
 1118                 /*
 1119                  * Issue a Resume command in case the chip was suspended.
 1120                  */
 1121                 fxp_scb_wait(sc);
 1122                 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
 1123 
 1124                 /* Set a watchdog timer in case the chip flakes out. */
 1125                 ifp->if_timer = 5;
 1126         }
 1127 }
 1128 
 1129 /*
 1130  * Process interface interrupts.
 1131  */
 1132 int
 1133 fxp_intr(void *arg)
 1134 {
 1135         struct fxp_softc *sc = arg;
 1136         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1137         bus_dmamap_t rxmap;
 1138         int claimed = 0, rnr;
 1139         u_int8_t statack;
 1140 
 1141         if (!device_is_active(&sc->sc_dev) || sc->sc_enabled == 0)
 1142                 return (0);
 1143         /*
 1144          * If the interface isn't running, don't try to
 1145          * service the interrupt.. just ack it and bail.
 1146          */
 1147         if ((ifp->if_flags & IFF_RUNNING) == 0) {
 1148                 statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
 1149                 if (statack) {
 1150                         claimed = 1;
 1151                         CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
 1152                 }
 1153                 return (claimed);
 1154         }
 1155 
 1156         while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
 1157                 claimed = 1;
 1158 
 1159                 /*
 1160                  * First ACK all the interrupts in this pass.
 1161                  */
 1162                 CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
 1163 
 1164                 /*
 1165                  * Process receiver interrupts. If a no-resource (RNR)
 1166                  * condition exists, get whatever packets we can and
 1167                  * re-start the receiver.
 1168                  */
 1169                 rnr = (statack & (FXP_SCB_STATACK_RNR | FXP_SCB_STATACK_SWI)) ?
 1170                     1 : 0;
 1171                 if (statack & (FXP_SCB_STATACK_FR | FXP_SCB_STATACK_RNR |
 1172                     FXP_SCB_STATACK_SWI)) {
 1173                         FXP_EVCNT_INCR(&sc->sc_ev_rxintr);
 1174                         rnr |= fxp_rxintr(sc);
 1175                 }
 1176 
 1177                 /*
 1178                  * Free any finished transmit mbuf chains.
 1179                  */
 1180                 if (statack & (FXP_SCB_STATACK_CXTNO|FXP_SCB_STATACK_CNA)) {
 1181                         FXP_EVCNT_INCR(&sc->sc_ev_txintr);
 1182                         fxp_txintr(sc);
 1183 
 1184                         /*
 1185                          * Try to get more packets going.
 1186                          */
 1187                         fxp_start(ifp);
 1188 
 1189                         if (sc->sc_txpending == 0) {
 1190                                 /*
 1191                                  * If we want a re-init, do that now.
 1192                                  */
 1193                                 if (sc->sc_flags & FXPF_WANTINIT)
 1194                                         (void) fxp_init(ifp);
 1195                         }
 1196                 }
 1197 
 1198                 if (rnr) {
 1199                         fxp_scb_wait(sc);
 1200                         fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_ABORT);
 1201                         rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
 1202                         fxp_scb_wait(sc);
 1203                         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
 1204                             rxmap->dm_segs[0].ds_addr +
 1205                             RFA_ALIGNMENT_FUDGE);
 1206                         fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
 1207                 }
 1208         }
 1209 
 1210 #if NRND > 0
 1211         if (claimed)
 1212                 rnd_add_uint32(&sc->rnd_source, statack);
 1213 #endif
 1214         return (claimed);
 1215 }
 1216 
 1217 /*
 1218  * Handle transmit completion interrupts.
 1219  */
 1220 void
 1221 fxp_txintr(struct fxp_softc *sc)
 1222 {
 1223         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1224         struct fxp_txdesc *txd;
 1225         struct fxp_txsoft *txs;
 1226         int i;
 1227         u_int16_t txstat;
 1228 
 1229         ifp->if_flags &= ~IFF_OACTIVE;
 1230         for (i = sc->sc_txdirty; sc->sc_txpending != 0;
 1231             i = FXP_NEXTTX(i), sc->sc_txpending--) {
 1232                 txd = FXP_CDTX(sc, i);
 1233                 txs = FXP_DSTX(sc, i);
 1234 
 1235                 FXP_CDTXSYNC(sc, i,
 1236                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1237 
 1238                 /* skip dummy NOP TX descriptor */
 1239                 if ((le16toh(txd->txd_txcb.cb_command) & FXP_CB_COMMAND_CMD)
 1240                     == FXP_CB_COMMAND_NOP)
 1241                         continue;
 1242 
 1243                 txstat = le16toh(txd->txd_txcb.cb_status);
 1244 
 1245                 if ((txstat & FXP_CB_STATUS_C) == 0)
 1246                         break;
 1247 
 1248                 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
 1249                     0, txs->txs_dmamap->dm_mapsize,
 1250                     BUS_DMASYNC_POSTWRITE);
 1251                 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
 1252                 m_freem(txs->txs_mbuf);
 1253                 txs->txs_mbuf = NULL;
 1254         }
 1255 
 1256         /* Update the dirty transmit buffer pointer. */
 1257         sc->sc_txdirty = i;
 1258 
 1259         /*
 1260          * Cancel the watchdog timer if there are no pending
 1261          * transmissions.
 1262          */
 1263         if (sc->sc_txpending == 0)
 1264                 ifp->if_timer = 0;
 1265 }
 1266 
 1267 /*
 1268  * fxp_rx_hwcksum: check status of H/W offloading for received packets.
 1269  */
 1270 
 1271 int
 1272 fxp_rx_hwcksum(struct mbuf *m, const struct fxp_rfa *rfa)
 1273 {
 1274         u_int16_t rxparsestat;
 1275         u_int16_t csum_stat;
 1276         u_int32_t csum_data;
 1277         int csum_flags;
 1278 
 1279         /*
 1280          * check VLAN tag stripping.
 1281          */
 1282 
 1283         if (rfa->rfa_status & htole16(FXP_RFA_STATUS_VLAN)) {
 1284                 struct m_tag *vtag;
 1285 
 1286                 vtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int), M_NOWAIT);
 1287                 if (vtag == NULL)
 1288                         return ENOMEM;
 1289                 *(u_int *)(vtag + 1) = be16toh(rfa->vlan_id);
 1290                 m_tag_prepend(m, vtag);
 1291         }
 1292 
 1293         /*
 1294          * check H/W Checksumming.
 1295          */
 1296 
 1297         csum_stat = le16toh(rfa->cksum_stat);
 1298         rxparsestat = le16toh(rfa->rx_parse_stat);
 1299         if (!(rfa->rfa_status & htole16(FXP_RFA_STATUS_PARSE)))
 1300                 return 0;
 1301 
 1302         csum_flags = 0;
 1303         csum_data = 0;
 1304 
 1305         if (csum_stat & FXP_RFDX_CS_IP_CSUM_BIT_VALID) {
 1306                 csum_flags = M_CSUM_IPv4;
 1307                 if (!(csum_stat & FXP_RFDX_CS_IP_CSUM_VALID))
 1308                         csum_flags |= M_CSUM_IPv4_BAD;
 1309         }
 1310 
 1311         if (csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_BIT_VALID) {
 1312                 csum_flags |= (M_CSUM_TCPv4|M_CSUM_UDPv4); /* XXX */
 1313                 if (!(csum_stat & FXP_RFDX_CS_TCPUDP_CSUM_VALID))
 1314                         csum_flags |= M_CSUM_TCP_UDP_BAD;
 1315         }
 1316 
 1317         m->m_pkthdr.csum_flags = csum_flags;
 1318         m->m_pkthdr.csum_data = csum_data;
 1319 
 1320         return 0;
 1321 }
 1322 
 1323 /*
 1324  * Handle receive interrupts.
 1325  */
 1326 int
 1327 fxp_rxintr(struct fxp_softc *sc)
 1328 {
 1329         struct ethercom *ec = &sc->sc_ethercom;
 1330         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1331         struct mbuf *m, *m0;
 1332         bus_dmamap_t rxmap;
 1333         struct fxp_rfa *rfa;
 1334         int rnr;
 1335         u_int16_t len, rxstat;
 1336 
 1337         rnr = 0;
 1338 
 1339         for (;;) {
 1340                 m = sc->sc_rxq.ifq_head;
 1341                 rfa = FXP_MTORFA(m);
 1342                 rxmap = M_GETCTX(m, bus_dmamap_t);
 1343 
 1344                 FXP_RFASYNC(sc, m,
 1345                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1346 
 1347                 rxstat = le16toh(rfa->rfa_status);
 1348 
 1349                 if ((rxstat & FXP_RFA_STATUS_RNR) != 0)
 1350                         rnr = 1;
 1351 
 1352                 if ((rxstat & FXP_RFA_STATUS_C) == 0) {
 1353                         /*
 1354                          * We have processed all of the
 1355                          * receive buffers.
 1356                          */
 1357                         FXP_RFASYNC(sc, m, BUS_DMASYNC_PREREAD);
 1358                         return rnr;
 1359                 }
 1360 
 1361                 IF_DEQUEUE(&sc->sc_rxq, m);
 1362 
 1363                 FXP_RXBUFSYNC(sc, m, BUS_DMASYNC_POSTREAD);
 1364 
 1365                 len = le16toh(rfa->actual_size) &
 1366                     (m->m_ext.ext_size - 1);
 1367 
 1368                 if (len < sizeof(struct ether_header)) {
 1369                         /*
 1370                          * Runt packet; drop it now.
 1371                          */
 1372                         FXP_INIT_RFABUF(sc, m);
 1373                         continue;
 1374                 }
 1375 
 1376                 /*
 1377                  * If support for 802.1Q VLAN sized frames is
 1378                  * enabled, we need to do some additional error
 1379                  * checking (as we are saving bad frames, in
 1380                  * order to receive the larger ones).
 1381                  */
 1382                 if ((ec->ec_capenable & ETHERCAP_VLAN_MTU) != 0 &&
 1383                     (rxstat & (FXP_RFA_STATUS_OVERRUN|
 1384                                FXP_RFA_STATUS_RNR|
 1385                                FXP_RFA_STATUS_ALIGN|
 1386                                FXP_RFA_STATUS_CRC)) != 0) {
 1387                         FXP_INIT_RFABUF(sc, m);
 1388                         continue;
 1389                 }
 1390 
 1391                 /* Do checksum checking. */
 1392                 m->m_pkthdr.csum_flags = 0;
 1393                 if (sc->sc_flags & FXPF_EXT_RFA)
 1394                         if (fxp_rx_hwcksum(m, rfa))
 1395                                 goto dropit;
 1396 
 1397                 /*
 1398                  * If the packet is small enough to fit in a
 1399                  * single header mbuf, allocate one and copy
 1400                  * the data into it.  This greatly reduces
 1401                  * memory consumption when we receive lots
 1402                  * of small packets.
 1403                  *
 1404                  * Otherwise, we add a new buffer to the receive
 1405                  * chain.  If this fails, we drop the packet and
 1406                  * recycle the old buffer.
 1407                  */
 1408                 if (fxp_copy_small != 0 && len <= MHLEN) {
 1409                         MGETHDR(m0, M_DONTWAIT, MT_DATA);
 1410                         if (m0 == NULL)
 1411                                 goto dropit;
 1412                         MCLAIM(m0, &sc->sc_ethercom.ec_rx_mowner);
 1413                         memcpy(mtod(m0, caddr_t),
 1414                             mtod(m, caddr_t), len);
 1415                         m0->m_pkthdr.csum_flags = m->m_pkthdr.csum_flags;
 1416                         m0->m_pkthdr.csum_data = m->m_pkthdr.csum_data;
 1417                         FXP_INIT_RFABUF(sc, m);
 1418                         m = m0;
 1419                 } else {
 1420                         if (fxp_add_rfabuf(sc, rxmap, 1) != 0) {
 1421  dropit:
 1422                                 ifp->if_ierrors++;
 1423                                 FXP_INIT_RFABUF(sc, m);
 1424                                 continue;
 1425                         }
 1426                 }
 1427 
 1428                 m->m_pkthdr.rcvif = ifp;
 1429                 m->m_pkthdr.len = m->m_len = len;
 1430 
 1431 #if NBPFILTER > 0
 1432                 /*
 1433                  * Pass this up to any BPF listeners, but only
 1434                  * pass it up the stack it its for us.
 1435                  */
 1436                 if (ifp->if_bpf)
 1437                         bpf_mtap(ifp->if_bpf, m);
 1438 #endif
 1439 
 1440                 /* Pass it on. */
 1441                 (*ifp->if_input)(ifp, m);
 1442         }
 1443 }
 1444 
 1445 /*
 1446  * Update packet in/out/collision statistics. The i82557 doesn't
 1447  * allow you to access these counters without doing a fairly
 1448  * expensive DMA to get _all_ of the statistics it maintains, so
 1449  * we do this operation here only once per second. The statistics
 1450  * counters in the kernel are updated from the previous dump-stats
 1451  * DMA and then a new dump-stats DMA is started. The on-chip
 1452  * counters are zeroed when the DMA completes. If we can't start
 1453  * the DMA immediately, we don't wait - we just prepare to read
 1454  * them again next time.
 1455  */
 1456 void
 1457 fxp_tick(void *arg)
 1458 {
 1459         struct fxp_softc *sc = arg;
 1460         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1461         struct fxp_stats *sp = &sc->sc_control_data->fcd_stats;
 1462         int s;
 1463 
 1464         if (!device_is_active(&sc->sc_dev))
 1465                 return;
 1466 
 1467         s = splnet();
 1468 
 1469         FXP_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
 1470 
 1471         ifp->if_opackets += le32toh(sp->tx_good);
 1472         ifp->if_collisions += le32toh(sp->tx_total_collisions);
 1473         if (sp->rx_good) {
 1474                 ifp->if_ipackets += le32toh(sp->rx_good);
 1475                 sc->sc_rxidle = 0;
 1476         } else if (sc->sc_flags & FXPF_RECV_WORKAROUND) {
 1477                 sc->sc_rxidle++;
 1478         }
 1479         ifp->if_ierrors +=
 1480             le32toh(sp->rx_crc_errors) +
 1481             le32toh(sp->rx_alignment_errors) +
 1482             le32toh(sp->rx_rnr_errors) +
 1483             le32toh(sp->rx_overrun_errors);
 1484         /*
 1485          * If any transmit underruns occurred, bump up the transmit
 1486          * threshold by another 512 bytes (64 * 8).
 1487          */
 1488         if (sp->tx_underruns) {
 1489                 ifp->if_oerrors += le32toh(sp->tx_underruns);
 1490                 if (tx_threshold < 192)
 1491                         tx_threshold += 64;
 1492         }
 1493 #ifdef FXP_EVENT_COUNTERS
 1494         if (sc->sc_rev >= FXP_REV_82558_A4) {
 1495                 sc->sc_ev_txpause.ev_count += sp->tx_pauseframes;
 1496                 sc->sc_ev_rxpause.ev_count += sp->rx_pauseframes;
 1497         }
 1498 #endif
 1499 
 1500         /*
 1501          * If we haven't received any packets in FXP_MAX_RX_IDLE seconds,
 1502          * then assume the receiver has locked up and attempt to clear
 1503          * the condition by reprogramming the multicast filter (actually,
 1504          * resetting the interface). This is a work-around for a bug in
 1505          * the 82557 where the receiver locks up if it gets certain types
 1506          * of garbage in the synchronization bits prior to the packet header.
 1507          * This bug is supposed to only occur in 10Mbps mode, but has been
 1508          * seen to occur in 100Mbps mode as well (perhaps due to a 10/100
 1509          * speed transition).
 1510          */
 1511         if (sc->sc_rxidle > FXP_MAX_RX_IDLE) {
 1512                 (void) fxp_init(ifp);
 1513                 splx(s);
 1514                 return;
 1515         }
 1516         /*
 1517          * If there is no pending command, start another stats
 1518          * dump. Otherwise punt for now.
 1519          */
 1520         if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
 1521                 /*
 1522                  * Start another stats dump.
 1523                  */
 1524                 FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
 1525                 fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
 1526         } else {
 1527                 /*
 1528                  * A previous command is still waiting to be accepted.
 1529                  * Just zero our copy of the stats and wait for the
 1530                  * next timer event to update them.
 1531                  */
 1532                 /* BIG_ENDIAN: no swap required to store 0 */
 1533                 sp->tx_good = 0;
 1534                 sp->tx_underruns = 0;
 1535                 sp->tx_total_collisions = 0;
 1536 
 1537                 sp->rx_good = 0;
 1538                 sp->rx_crc_errors = 0;
 1539                 sp->rx_alignment_errors = 0;
 1540                 sp->rx_rnr_errors = 0;
 1541                 sp->rx_overrun_errors = 0;
 1542                 if (sc->sc_rev >= FXP_REV_82558_A4) {
 1543                         sp->tx_pauseframes = 0;
 1544                         sp->rx_pauseframes = 0;
 1545                 }
 1546         }
 1547 
 1548         if (sc->sc_flags & FXPF_MII) {
 1549                 /* Tick the MII clock. */
 1550                 mii_tick(&sc->sc_mii);
 1551         }
 1552 
 1553         splx(s);
 1554 
 1555         /*
 1556          * Schedule another timeout one second from now.
 1557          */
 1558         callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
 1559 }
 1560 
 1561 /*
 1562  * Drain the receive queue.
 1563  */
 1564 void
 1565 fxp_rxdrain(struct fxp_softc *sc)
 1566 {
 1567         bus_dmamap_t rxmap;
 1568         struct mbuf *m;
 1569 
 1570         for (;;) {
 1571                 IF_DEQUEUE(&sc->sc_rxq, m);
 1572                 if (m == NULL)
 1573                         break;
 1574                 rxmap = M_GETCTX(m, bus_dmamap_t);
 1575                 bus_dmamap_unload(sc->sc_dmat, rxmap);
 1576                 FXP_RXMAP_PUT(sc, rxmap);
 1577                 m_freem(m);
 1578         }
 1579 }
 1580 
 1581 /*
 1582  * Stop the interface. Cancels the statistics updater and resets
 1583  * the interface.
 1584  */
 1585 void
 1586 fxp_stop(struct ifnet *ifp, int disable)
 1587 {
 1588         struct fxp_softc *sc = ifp->if_softc;
 1589         struct fxp_txsoft *txs;
 1590         int i;
 1591 
 1592         /*
 1593          * Turn down interface (done early to avoid bad interactions
 1594          * between panics, shutdown hooks, and the watchdog timer)
 1595          */
 1596         ifp->if_timer = 0;
 1597         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
 1598 
 1599         /*
 1600          * Cancel stats updater.
 1601          */
 1602         callout_stop(&sc->sc_callout);
 1603         if (sc->sc_flags & FXPF_MII) {
 1604                 /* Down the MII. */
 1605                 mii_down(&sc->sc_mii);
 1606         }
 1607 
 1608         /*
 1609          * Issue software reset.  This unloads any microcode that
 1610          * might already be loaded.
 1611          */
 1612         sc->sc_flags &= ~FXPF_UCODE_LOADED;
 1613         CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
 1614         DELAY(50);
 1615 
 1616         /*
 1617          * Release any xmit buffers.
 1618          */
 1619         for (i = 0; i < FXP_NTXCB; i++) {
 1620                 txs = FXP_DSTX(sc, i);
 1621                 if (txs->txs_mbuf != NULL) {
 1622                         bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
 1623                         m_freem(txs->txs_mbuf);
 1624                         txs->txs_mbuf = NULL;
 1625                 }
 1626         }
 1627         sc->sc_txpending = 0;
 1628 
 1629         if (disable) {
 1630                 fxp_rxdrain(sc);
 1631                 fxp_disable(sc);
 1632         }
 1633 
 1634 }
 1635 
 1636 /*
 1637  * Watchdog/transmission transmit timeout handler. Called when a
 1638  * transmission is started on the interface, but no interrupt is
 1639  * received before the timeout. This usually indicates that the
 1640  * card has wedged for some reason.
 1641  */
 1642 void
 1643 fxp_watchdog(struct ifnet *ifp)
 1644 {
 1645         struct fxp_softc *sc = ifp->if_softc;
 1646 
 1647         log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
 1648         ifp->if_oerrors++;
 1649 
 1650         (void) fxp_init(ifp);
 1651 }
 1652 
 1653 /*
 1654  * Initialize the interface.  Must be called at splnet().
 1655  */
 1656 int
 1657 fxp_init(struct ifnet *ifp)
 1658 {
 1659         struct fxp_softc *sc = ifp->if_softc;
 1660         struct fxp_cb_config *cbp;
 1661         struct fxp_cb_ias *cb_ias;
 1662         struct fxp_txdesc *txd;
 1663         bus_dmamap_t rxmap;
 1664         int i, prm, save_bf, lrxen, vlan_drop, allm, error = 0;
 1665 
 1666         if ((error = fxp_enable(sc)) != 0)
 1667                 goto out;
 1668 
 1669         /*
 1670          * Cancel any pending I/O
 1671          */
 1672         fxp_stop(ifp, 0);
 1673 
 1674         /*
 1675          * XXX just setting sc_flags to 0 here clears any FXPF_MII
 1676          * flag, and this prevents the MII from detaching resulting in
 1677          * a panic. The flags field should perhaps be split in runtime
 1678          * flags and more static information. For now, just clear the
 1679          * only other flag set.
 1680          */
 1681 
 1682         sc->sc_flags &= ~FXPF_WANTINIT;
 1683 
 1684         /*
 1685          * Initialize base of CBL and RFA memory. Loading with zero
 1686          * sets it up for regular linear addressing.
 1687          */
 1688         fxp_scb_wait(sc);
 1689         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
 1690         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
 1691 
 1692         fxp_scb_wait(sc);
 1693         fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
 1694 
 1695         /*
 1696          * Initialize the multicast filter.  Do this now, since we might
 1697          * have to setup the config block differently.
 1698          */
 1699         fxp_mc_setup(sc);
 1700 
 1701         prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
 1702         allm = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
 1703 
 1704         /*
 1705          * In order to support receiving 802.1Q VLAN frames, we have to
 1706          * enable "save bad frames", since they are 4 bytes larger than
 1707          * the normal Ethernet maximum frame length.  On i82558 and later,
 1708          * we have a better mechanism for this.
 1709          */
 1710         save_bf = 0;
 1711         lrxen = 0;
 1712         vlan_drop = 0;
 1713         if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
 1714                 if (sc->sc_rev < FXP_REV_82558_A4)
 1715                         save_bf = 1;
 1716                 else
 1717                         lrxen = 1;
 1718                 if (sc->sc_rev >= FXP_REV_82550)
 1719                         vlan_drop = 1;
 1720         }
 1721 
 1722         /*
 1723          * Initialize base of dump-stats buffer.
 1724          */
 1725         fxp_scb_wait(sc);
 1726         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
 1727             sc->sc_cddma + FXP_CDSTATSOFF);
 1728         FXP_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
 1729         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
 1730 
 1731         cbp = &sc->sc_control_data->fcd_configcb;
 1732         memset(cbp, 0, sizeof(struct fxp_cb_config));
 1733 
 1734         /*
 1735          * Load microcode for this controller.
 1736          */
 1737         fxp_load_ucode(sc);
 1738 
 1739         if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK1))
 1740                 sc->sc_flags |= FXPF_RECV_WORKAROUND;
 1741         else
 1742                 sc->sc_flags &= ~FXPF_RECV_WORKAROUND;
 1743 
 1744         /*
 1745          * This copy is kind of disgusting, but there are a bunch of must be
 1746          * zero and must be one bits in this structure and this is the easiest
 1747          * way to initialize them all to proper values.
 1748          */
 1749         memcpy(cbp, fxp_cb_config_template, sizeof(fxp_cb_config_template));
 1750 
 1751         /* BIG_ENDIAN: no need to swap to store 0 */
 1752         cbp->cb_status =        0;
 1753         cbp->cb_command =       htole16(FXP_CB_COMMAND_CONFIG |
 1754                                     FXP_CB_COMMAND_EL);
 1755         /* BIG_ENDIAN: no need to swap to store 0xffffffff */
 1756         cbp->link_addr =        0xffffffff; /* (no) next command */
 1757                                         /* bytes in config block */
 1758         cbp->byte_count =       (sc->sc_flags & FXPF_EXT_RFA) ?
 1759                                 FXP_EXT_CONFIG_LEN : FXP_CONFIG_LEN;
 1760         cbp->rx_fifo_limit =    8;      /* rx fifo threshold (32 bytes) */
 1761         cbp->tx_fifo_limit =    0;      /* tx fifo threshold (0 bytes) */
 1762         cbp->adaptive_ifs =     0;      /* (no) adaptive interframe spacing */
 1763         cbp->mwi_enable =       (sc->sc_flags & FXPF_MWI) ? 1 : 0;
 1764         cbp->type_enable =      0;      /* actually reserved */
 1765         cbp->read_align_en =    (sc->sc_flags & FXPF_READ_ALIGN) ? 1 : 0;
 1766         cbp->end_wr_on_cl =     (sc->sc_flags & FXPF_WRITE_ALIGN) ? 1 : 0;
 1767         cbp->rx_dma_bytecount = 0;      /* (no) rx DMA max */
 1768         cbp->tx_dma_bytecount = 0;      /* (no) tx DMA max */
 1769         cbp->dma_mbce =         0;      /* (disable) dma max counters */
 1770         cbp->late_scb =         0;      /* (don't) defer SCB update */
 1771         cbp->tno_int_or_tco_en =0;      /* (disable) tx not okay interrupt */
 1772         cbp->ci_int =           1;      /* interrupt on CU idle */
 1773         cbp->ext_txcb_dis =     (sc->sc_flags & FXPF_EXT_TXCB) ? 0 : 1;
 1774         cbp->ext_stats_dis =    1;      /* disable extended counters */
 1775         cbp->keep_overrun_rx =  0;      /* don't pass overrun frames to host */
 1776         cbp->save_bf =          save_bf;/* save bad frames */
 1777         cbp->disc_short_rx =    !prm;   /* discard short packets */
 1778         cbp->underrun_retry =   1;      /* retry mode (1) on DMA underrun */
 1779         cbp->ext_rfa =          (sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
 1780         cbp->two_frames =       0;      /* do not limit FIFO to 2 frames */
 1781         cbp->dyn_tbd =          0;      /* (no) dynamic TBD mode */
 1782                                         /* interface mode */
 1783         cbp->mediatype =        (sc->sc_flags & FXPF_MII) ? 1 : 0;
 1784         cbp->csma_dis =         0;      /* (don't) disable link */
 1785         cbp->tcp_udp_cksum =    0;      /* (don't) enable checksum */
 1786         cbp->vlan_tco =         0;      /* (don't) enable vlan wakeup */
 1787         cbp->link_wake_en =     0;      /* (don't) assert PME# on link change */
 1788         cbp->arp_wake_en =      0;      /* (don't) assert PME# on arp */
 1789         cbp->mc_wake_en =       0;      /* (don't) assert PME# on mcmatch */
 1790         cbp->nsai =             1;      /* (don't) disable source addr insert */
 1791         cbp->preamble_length =  2;      /* (7 byte) preamble */
 1792         cbp->loopback =         0;      /* (don't) loopback */
 1793         cbp->linear_priority =  0;      /* (normal CSMA/CD operation) */
 1794         cbp->linear_pri_mode =  0;      /* (wait after xmit only) */
 1795         cbp->interfrm_spacing = 6;      /* (96 bits of) interframe spacing */
 1796         cbp->promiscuous =      prm;    /* promiscuous mode */
 1797         cbp->bcast_disable =    0;      /* (don't) disable broadcasts */
 1798         cbp->wait_after_win =   0;      /* (don't) enable modified backoff alg*/
 1799         cbp->ignore_ul =        0;      /* consider U/L bit in IA matching */
 1800         cbp->crc16_en =         0;      /* (don't) enable crc-16 algorithm */
 1801         cbp->crscdt =           (sc->sc_flags & FXPF_MII) ? 0 : 1;
 1802         cbp->stripping =        !prm;   /* truncate rx packet to byte count */
 1803         cbp->padding =          1;      /* (do) pad short tx packets */
 1804         cbp->rcv_crc_xfer =     0;      /* (don't) xfer CRC to host */
 1805         cbp->long_rx_en =       lrxen;  /* long packet receive enable */
 1806         cbp->ia_wake_en =       0;      /* (don't) wake up on address match */
 1807         cbp->magic_pkt_dis =    0;      /* (don't) disable magic packet */
 1808                                         /* must set wake_en in PMCSR also */
 1809         cbp->force_fdx =        0;      /* (don't) force full duplex */
 1810         cbp->fdx_pin_en =       1;      /* (enable) FDX# pin */
 1811         cbp->multi_ia =         0;      /* (don't) accept multiple IAs */
 1812         cbp->mc_all =           allm;   /* accept all multicasts */
 1813         cbp->ext_rx_mode =      (sc->sc_flags & FXPF_EXT_RFA) ? 1 : 0;
 1814         cbp->vlan_drop_en =     vlan_drop;
 1815 
 1816         if (sc->sc_rev < FXP_REV_82558_A4) {
 1817                 /*
 1818                  * The i82557 has no hardware flow control, the values
 1819                  * here are the defaults for the chip.
 1820                  */
 1821                 cbp->fc_delay_lsb =     0;
 1822                 cbp->fc_delay_msb =     0x40;
 1823                 cbp->pri_fc_thresh =    3;
 1824                 cbp->tx_fc_dis =        0;
 1825                 cbp->rx_fc_restop =     0;
 1826                 cbp->rx_fc_restart =    0;
 1827                 cbp->fc_filter =        0;
 1828                 cbp->pri_fc_loc =       1;
 1829         } else {
 1830                 cbp->fc_delay_lsb =     0x1f;
 1831                 cbp->fc_delay_msb =     0x01;
 1832                 cbp->pri_fc_thresh =    3;
 1833                 cbp->tx_fc_dis =        0;      /* enable transmit FC */
 1834                 cbp->rx_fc_restop =     1;      /* enable FC restop frames */
 1835                 cbp->rx_fc_restart =    1;      /* enable FC restart frames */
 1836                 cbp->fc_filter =        !prm;   /* drop FC frames to host */
 1837                 cbp->pri_fc_loc =       1;      /* FC pri location (byte31) */
 1838                 cbp->ext_stats_dis =    0;      /* enable extended stats */
 1839         }
 1840 
 1841         FXP_CDCONFIGSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1842 
 1843         /*
 1844          * Start the config command/DMA.
 1845          */
 1846         fxp_scb_wait(sc);
 1847         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDCONFIGOFF);
 1848         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
 1849         /* ...and wait for it to complete. */
 1850         i = 1000;
 1851         do {
 1852                 FXP_CDCONFIGSYNC(sc,
 1853                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1854                 DELAY(1);
 1855         } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
 1856         if (i == 0) {
 1857                 log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
 1858                     sc->sc_dev.dv_xname, __LINE__);
 1859                 return (ETIMEDOUT);
 1860         }
 1861 
 1862         /*
 1863          * Initialize the station address.
 1864          */
 1865         cb_ias = &sc->sc_control_data->fcd_iascb;
 1866         /* BIG_ENDIAN: no need to swap to store 0 */
 1867         cb_ias->cb_status = 0;
 1868         cb_ias->cb_command = htole16(FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL);
 1869         /* BIG_ENDIAN: no need to swap to store 0xffffffff */
 1870         cb_ias->link_addr = 0xffffffff;
 1871         memcpy(cb_ias->macaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
 1872 
 1873         FXP_CDIASSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1874 
 1875         /*
 1876          * Start the IAS (Individual Address Setup) command/DMA.
 1877          */
 1878         fxp_scb_wait(sc);
 1879         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDIASOFF);
 1880         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
 1881         /* ...and wait for it to complete. */
 1882         i = 1000;
 1883         do {
 1884                 FXP_CDIASSYNC(sc,
 1885                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1886                 DELAY(1);
 1887         } while ((le16toh(cb_ias->cb_status) & FXP_CB_STATUS_C) == 0 && --i);
 1888         if (i == 0) {
 1889                 log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
 1890                     sc->sc_dev.dv_xname, __LINE__);
 1891                 return (ETIMEDOUT);
 1892         }
 1893 
 1894         /*
 1895          * Initialize the transmit descriptor ring.  txlast is initialized
 1896          * to the end of the list so that it will wrap around to the first
 1897          * descriptor when the first packet is transmitted.
 1898          */
 1899         for (i = 0; i < FXP_NTXCB; i++) {
 1900                 txd = FXP_CDTX(sc, i);
 1901                 memset(txd, 0, sizeof(*txd));
 1902                 txd->txd_txcb.cb_command =
 1903                     htole16(FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S);
 1904                 txd->txd_txcb.link_addr =
 1905                     htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(i)));
 1906                 if (sc->sc_flags & FXPF_EXT_TXCB)
 1907                         txd->txd_txcb.tbd_array_addr =
 1908                             htole32(FXP_CDTBDADDR(sc, i) +
 1909                                     (2 * sizeof(struct fxp_tbd)));
 1910                 else
 1911                         txd->txd_txcb.tbd_array_addr =
 1912                             htole32(FXP_CDTBDADDR(sc, i));
 1913                 FXP_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1914         }
 1915         sc->sc_txpending = 0;
 1916         sc->sc_txdirty = 0;
 1917         sc->sc_txlast = FXP_NTXCB - 1;
 1918 
 1919         /*
 1920          * Initialize the receive buffer list.
 1921          */
 1922         sc->sc_rxq.ifq_maxlen = FXP_NRFABUFS;
 1923         while (sc->sc_rxq.ifq_len < FXP_NRFABUFS) {
 1924                 rxmap = FXP_RXMAP_GET(sc);
 1925                 if ((error = fxp_add_rfabuf(sc, rxmap, 0)) != 0) {
 1926                         log(LOG_ERR, "%s: unable to allocate or map rx "
 1927                             "buffer %d, error = %d\n",
 1928                             sc->sc_dev.dv_xname,
 1929                             sc->sc_rxq.ifq_len, error);
 1930                         /*
 1931                          * XXX Should attempt to run with fewer receive
 1932                          * XXX buffers instead of just failing.
 1933                          */
 1934                         FXP_RXMAP_PUT(sc, rxmap);
 1935                         fxp_rxdrain(sc);
 1936                         goto out;
 1937                 }
 1938         }
 1939         sc->sc_rxidle = 0;
 1940 
 1941         /*
 1942          * Give the transmit ring to the chip.  We do this by pointing
 1943          * the chip at the last descriptor (which is a NOP|SUSPEND), and
 1944          * issuing a start command.  It will execute the NOP and then
 1945          * suspend, pointing at the first descriptor.
 1946          */
 1947         fxp_scb_wait(sc);
 1948         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, FXP_CDTXADDR(sc, sc->sc_txlast));
 1949         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
 1950 
 1951         /*
 1952          * Initialize receiver buffer area - RFA.
 1953          */
 1954 #if 0   /* initialization will be done by FXP_SCB_INTRCNTL_REQUEST_SWI later */
 1955         rxmap = M_GETCTX(sc->sc_rxq.ifq_head, bus_dmamap_t);
 1956         fxp_scb_wait(sc);
 1957         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
 1958             rxmap->dm_segs[0].ds_addr + RFA_ALIGNMENT_FUDGE);
 1959         fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
 1960 #endif
 1961 
 1962         if (sc->sc_flags & FXPF_MII) {
 1963                 /*
 1964                  * Set current media.
 1965                  */
 1966                 mii_mediachg(&sc->sc_mii);
 1967         }
 1968 
 1969         /*
 1970          * ...all done!
 1971          */
 1972         ifp->if_flags |= IFF_RUNNING;
 1973         ifp->if_flags &= ~IFF_OACTIVE;
 1974 
 1975         /*
 1976          * Request a software generated interrupt that will be used to 
 1977          * (re)start the RU processing.  If we direct the chip to start
 1978          * receiving from the start of queue now, instead of letting the
 1979          * interrupt handler first process all received packets, we run
 1980          * the risk of having it overwrite mbuf clusters while they are
 1981          * being processed or after they have been returned to the pool.
 1982          */
 1983         CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTRCNTL_REQUEST_SWI);
 1984  
 1985         /*
 1986          * Start the one second timer.
 1987          */
 1988         callout_reset(&sc->sc_callout, hz, fxp_tick, sc);
 1989 
 1990         /*
 1991          * Attempt to start output on the interface.
 1992          */
 1993         fxp_start(ifp);
 1994 
 1995  out:
 1996         if (error) {
 1997                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
 1998                 ifp->if_timer = 0;
 1999                 log(LOG_ERR, "%s: interface not running\n",
 2000                     sc->sc_dev.dv_xname);
 2001         }
 2002         return (error);
 2003 }
 2004 
 2005 /*
 2006  * Change media according to request.
 2007  */
 2008 int
 2009 fxp_mii_mediachange(struct ifnet *ifp)
 2010 {
 2011         struct fxp_softc *sc = ifp->if_softc;
 2012 
 2013         if (ifp->if_flags & IFF_UP)
 2014                 mii_mediachg(&sc->sc_mii);
 2015         return (0);
 2016 }
 2017 
 2018 /*
 2019  * Notify the world which media we're using.
 2020  */
 2021 void
 2022 fxp_mii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
 2023 {
 2024         struct fxp_softc *sc = ifp->if_softc;
 2025 
 2026         if (sc->sc_enabled == 0) {
 2027                 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
 2028                 ifmr->ifm_status = 0;
 2029                 return;
 2030         }
 2031 
 2032         mii_pollstat(&sc->sc_mii);
 2033         ifmr->ifm_status = sc->sc_mii.mii_media_status;
 2034         ifmr->ifm_active = sc->sc_mii.mii_media_active;
 2035 
 2036         /*
 2037          * XXX Flow control is always turned on if the chip supports
 2038          * XXX it; we can't easily control it dynamically, since it
 2039          * XXX requires sending a setup packet.
 2040          */
 2041         if (sc->sc_rev >= FXP_REV_82558_A4)
 2042                 ifmr->ifm_active |= IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE;
 2043 }
 2044 
 2045 int
 2046 fxp_80c24_mediachange(struct ifnet *ifp)
 2047 {
 2048 
 2049         /* Nothing to do here. */
 2050         return (0);
 2051 }
 2052 
 2053 void
 2054 fxp_80c24_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
 2055 {
 2056         struct fxp_softc *sc = ifp->if_softc;
 2057 
 2058         /*
 2059          * Media is currently-selected media.  We cannot determine
 2060          * the link status.
 2061          */
 2062         ifmr->ifm_status = 0;
 2063         ifmr->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
 2064 }
 2065 
 2066 /*
 2067  * Add a buffer to the end of the RFA buffer list.
 2068  * Return 0 if successful, error code on failure.
 2069  *
 2070  * The RFA struct is stuck at the beginning of mbuf cluster and the
 2071  * data pointer is fixed up to point just past it.
 2072  */
 2073 int
 2074 fxp_add_rfabuf(struct fxp_softc *sc, bus_dmamap_t rxmap, int unload)
 2075 {
 2076         struct mbuf *m;
 2077         int error;
 2078 
 2079         MGETHDR(m, M_DONTWAIT, MT_DATA);
 2080         if (m == NULL)
 2081                 return (ENOBUFS);
 2082 
 2083         MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
 2084         MCLGET(m, M_DONTWAIT);
 2085         if ((m->m_flags & M_EXT) == 0) {
 2086                 m_freem(m);
 2087                 return (ENOBUFS);
 2088         }
 2089 
 2090         if (unload)
 2091                 bus_dmamap_unload(sc->sc_dmat, rxmap);
 2092 
 2093         M_SETCTX(m, rxmap);
 2094 
 2095         m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
 2096         error = bus_dmamap_load_mbuf(sc->sc_dmat, rxmap, m,
 2097             BUS_DMA_READ|BUS_DMA_NOWAIT);
 2098         if (error) {
 2099                 /* XXX XXX XXX */
 2100                 printf("%s: can't load rx DMA map %d, error = %d\n",
 2101                     sc->sc_dev.dv_xname, sc->sc_rxq.ifq_len, error);
 2102                 panic("fxp_add_rfabuf");
 2103         }
 2104 
 2105         FXP_INIT_RFABUF(sc, m);
 2106 
 2107         return (0);
 2108 }
 2109 
 2110 int
 2111 fxp_mdi_read(struct device *self, int phy, int reg)
 2112 {
 2113         struct fxp_softc *sc = (struct fxp_softc *)self;
 2114         int count = 10000;
 2115         int value;
 2116 
 2117         CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
 2118             (FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
 2119 
 2120         while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) &
 2121             0x10000000) == 0 && count--)
 2122                 DELAY(10);
 2123 
 2124         if (count <= 0)
 2125                 log(LOG_WARNING,
 2126                     "%s: fxp_mdi_read: timed out\n", sc->sc_dev.dv_xname);
 2127 
 2128         return (value & 0xffff);
 2129 }
 2130 
 2131 void
 2132 fxp_statchg(struct device *self)
 2133 {
 2134 
 2135         /* Nothing to do. */
 2136 }
 2137 
 2138 void
 2139 fxp_mdi_write(struct device *self, int phy, int reg, int value)
 2140 {
 2141         struct fxp_softc *sc = (struct fxp_softc *)self;
 2142         int count = 10000;
 2143 
 2144         CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
 2145             (FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
 2146             (value & 0xffff));
 2147 
 2148         while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
 2149             count--)
 2150                 DELAY(10);
 2151 
 2152         if (count <= 0)
 2153                 log(LOG_WARNING,
 2154                     "%s: fxp_mdi_write: timed out\n", sc->sc_dev.dv_xname);
 2155 }
 2156 
 2157 int
 2158 fxp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 2159 {
 2160         struct fxp_softc *sc = ifp->if_softc;
 2161         struct ifreq *ifr = (struct ifreq *)data;
 2162         int s, error;
 2163 
 2164         s = splnet();
 2165 
 2166         switch (cmd) {
 2167         case SIOCSIFMEDIA:
 2168         case SIOCGIFMEDIA:
 2169                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
 2170                 break;
 2171 
 2172         default:
 2173                 error = ether_ioctl(ifp, cmd, data);
 2174                 if (error == ENETRESET) {
 2175                         if (ifp->if_flags & IFF_RUNNING) {
 2176                                 /*
 2177                                  * Multicast list has changed; set the
 2178                                  * hardware filter accordingly.
 2179                                  */
 2180                                 if (sc->sc_txpending) {
 2181                                         sc->sc_flags |= FXPF_WANTINIT;
 2182                                         error = 0;
 2183                                 } else
 2184                                         error = fxp_init(ifp);
 2185                         } else
 2186                                 error = 0;
 2187                 }
 2188                 break;
 2189         }
 2190 
 2191         /* Try to get more packets going. */
 2192         if (sc->sc_enabled)
 2193                 fxp_start(ifp);
 2194 
 2195         splx(s);
 2196         return (error);
 2197 }
 2198 
 2199 /*
 2200  * Program the multicast filter.
 2201  *
 2202  * This function must be called at splnet().
 2203  */
 2204 void
 2205 fxp_mc_setup(struct fxp_softc *sc)
 2206 {
 2207         struct fxp_cb_mcs *mcsp = &sc->sc_control_data->fcd_mcscb;
 2208         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 2209         struct ethercom *ec = &sc->sc_ethercom;
 2210         struct ether_multi *enm;
 2211         struct ether_multistep step;
 2212         int count, nmcasts;
 2213 
 2214 #ifdef DIAGNOSTIC
 2215         if (sc->sc_txpending)
 2216                 panic("fxp_mc_setup: pending transmissions");
 2217 #endif
 2218 
 2219         ifp->if_flags &= ~IFF_ALLMULTI;
 2220 
 2221         /*
 2222          * Initialize multicast setup descriptor.
 2223          */
 2224         nmcasts = 0;
 2225         ETHER_FIRST_MULTI(step, ec, enm);
 2226         while (enm != NULL) {
 2227                 /*
 2228                  * Check for too many multicast addresses or if we're
 2229                  * listening to a range.  Either way, we simply have
 2230                  * to accept all multicasts.
 2231                  */
 2232                 if (nmcasts >= MAXMCADDR ||
 2233                     memcmp(enm->enm_addrlo, enm->enm_addrhi,
 2234                     ETHER_ADDR_LEN) != 0) {
 2235                         /*
 2236                          * Callers of this function must do the
 2237                          * right thing with this.  If we're called
 2238                          * from outside fxp_init(), the caller must
 2239                          * detect if the state if IFF_ALLMULTI changes.
 2240                          * If it does, the caller must then call
 2241                          * fxp_init(), since allmulti is handled by
 2242                          * the config block.
 2243                          */
 2244                         ifp->if_flags |= IFF_ALLMULTI;
 2245                         return;
 2246                 }
 2247                 memcpy(&mcsp->mc_addr[nmcasts][0], enm->enm_addrlo,
 2248                     ETHER_ADDR_LEN);
 2249                 nmcasts++;
 2250                 ETHER_NEXT_MULTI(step, enm);
 2251         }
 2252 
 2253         /* BIG_ENDIAN: no need to swap to store 0 */
 2254         mcsp->cb_status = 0;
 2255         mcsp->cb_command = htole16(FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL);
 2256         mcsp->link_addr = htole32(FXP_CDTXADDR(sc, FXP_NEXTTX(sc->sc_txlast)));
 2257         mcsp->mc_cnt = htole16(nmcasts * ETHER_ADDR_LEN);
 2258 
 2259         FXP_CDMCSSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 2260 
 2261         /*
 2262          * Wait until the command unit is not active.  This should never
 2263          * happen since nothing is queued, but make sure anyway.
 2264          */
 2265         count = 100;
 2266         while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
 2267             FXP_SCB_CUS_ACTIVE && --count)
 2268                 DELAY(1);
 2269         if (count == 0) {
 2270                 log(LOG_WARNING, "%s: line %d: command queue timeout\n",
 2271                     sc->sc_dev.dv_xname, __LINE__);
 2272                 return;
 2273         }
 2274 
 2275         /*
 2276          * Start the multicast setup command/DMA.
 2277          */
 2278         fxp_scb_wait(sc);
 2279         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDMCSOFF);
 2280         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
 2281 
 2282         /* ...and wait for it to complete. */
 2283         count = 1000;
 2284         do {
 2285                 FXP_CDMCSSYNC(sc,
 2286                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 2287                 DELAY(1);
 2288         } while ((le16toh(mcsp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
 2289         if (count == 0) {
 2290                 log(LOG_WARNING, "%s: line %d: dmasync timeout\n",
 2291                     sc->sc_dev.dv_xname, __LINE__);
 2292                 return;
 2293         }
 2294 }
 2295 
 2296 static const uint32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
 2297 static const uint32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
 2298 static const uint32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
 2299 static const uint32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
 2300 static const uint32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
 2301 static const uint32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
 2302 
 2303 #define UCODE(x)        x, sizeof(x)/sizeof(uint32_t)
 2304 
 2305 static const struct ucode {
 2306         int32_t         revision;
 2307         const uint32_t  *ucode;
 2308         size_t          length;
 2309         uint16_t        int_delay_offset;
 2310         uint16_t        bundle_max_offset;
 2311 } ucode_table[] = {
 2312         { FXP_REV_82558_A4, UCODE(fxp_ucode_d101a),
 2313           D101_CPUSAVER_DWORD, 0 },
 2314 
 2315         { FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0),
 2316           D101_CPUSAVER_DWORD, 0 },
 2317 
 2318         { FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
 2319           D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
 2320 
 2321         { FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
 2322           D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
 2323 
 2324         { FXP_REV_82550, UCODE(fxp_ucode_d102),
 2325           D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
 2326 
 2327         { FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
 2328           D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
 2329 
 2330         { 0, NULL, 0, 0, 0 }
 2331 };
 2332 
 2333 void
 2334 fxp_load_ucode(struct fxp_softc *sc)
 2335 {
 2336         const struct ucode *uc;
 2337         struct fxp_cb_ucode *cbp = &sc->sc_control_data->fcd_ucode;
 2338         int count, i;
 2339 
 2340         if (sc->sc_flags & FXPF_UCODE_LOADED)
 2341                 return;
 2342 
 2343         /*
 2344          * Only load the uCode if the user has requested that
 2345          * we do so.
 2346          */
 2347         if ((sc->sc_ethercom.ec_if.if_flags & IFF_LINK0) == 0) {
 2348                 sc->sc_int_delay = 0;
 2349                 sc->sc_bundle_max = 0;
 2350                 return;
 2351         }
 2352 
 2353         for (uc = ucode_table; uc->ucode != NULL; uc++) {
 2354                 if (sc->sc_rev == uc->revision)
 2355                         break;
 2356         }
 2357         if (uc->ucode == NULL)
 2358                 return;
 2359 
 2360         /* BIG ENDIAN: no need to swap to store 0 */
 2361         cbp->cb_status = 0;
 2362         cbp->cb_command = htole16(FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL);
 2363         cbp->link_addr = 0xffffffff;            /* (no) next command */
 2364         for (i = 0; i < uc->length; i++)
 2365                 cbp->ucode[i] = htole32(uc->ucode[i]);
 2366 
 2367         if (uc->int_delay_offset)
 2368                 *(volatile uint16_t *) &cbp->ucode[uc->int_delay_offset] =
 2369                     htole16(fxp_int_delay + (fxp_int_delay / 2));
 2370 
 2371         if (uc->bundle_max_offset)
 2372                 *(volatile uint16_t *) &cbp->ucode[uc->bundle_max_offset] =
 2373                     htole16(fxp_bundle_max);
 2374 
 2375         FXP_CDUCODESYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 2376 
 2377         /*
 2378          * Download the uCode to the chip.
 2379          */
 2380         fxp_scb_wait(sc);
 2381         CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, sc->sc_cddma + FXP_CDUCODEOFF);
 2382         fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
 2383 
 2384         /* ...and wait for it to complete. */
 2385         count = 10000;
 2386         do {
 2387                 FXP_CDUCODESYNC(sc,
 2388                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 2389                 DELAY(2);
 2390         } while ((le16toh(cbp->cb_status) & FXP_CB_STATUS_C) == 0 && --count);
 2391         if (count == 0) {
 2392                 sc->sc_int_delay = 0;
 2393                 sc->sc_bundle_max = 0;
 2394                 log(LOG_WARNING, "%s: timeout loading microcode\n",
 2395                     sc->sc_dev.dv_xname);
 2396                 return;
 2397         }
 2398 
 2399         if (sc->sc_int_delay != fxp_int_delay ||
 2400             sc->sc_bundle_max != fxp_bundle_max) {
 2401                 sc->sc_int_delay = fxp_int_delay;
 2402                 sc->sc_bundle_max = fxp_bundle_max;
 2403                 log(LOG_INFO, "%s: Microcode loaded: int delay: %d usec, "
 2404                     "max bundle: %d\n", sc->sc_dev.dv_xname,
 2405                     sc->sc_int_delay,
 2406                     uc->bundle_max_offset == 0 ? 0 : sc->sc_bundle_max);
 2407         }
 2408 
 2409         sc->sc_flags |= FXPF_UCODE_LOADED;
 2410 }
 2411 
 2412 int
 2413 fxp_enable(struct fxp_softc *sc)
 2414 {
 2415 
 2416         if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
 2417                 if ((*sc->sc_enable)(sc) != 0) {
 2418                         log(LOG_ERR, "%s: device enable failed\n",
 2419                             sc->sc_dev.dv_xname);
 2420                         return (EIO);
 2421                 }
 2422         }
 2423 
 2424         sc->sc_enabled = 1;
 2425         return (0);
 2426 }
 2427 
 2428 void
 2429 fxp_disable(struct fxp_softc *sc)
 2430 {
 2431 
 2432         if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
 2433                 (*sc->sc_disable)(sc);
 2434                 sc->sc_enabled = 0;
 2435         }
 2436 }
 2437 
 2438 /*
 2439  * fxp_activate:
 2440  *
 2441  *      Handle device activation/deactivation requests.
 2442  */
 2443 int
 2444 fxp_activate(struct device *self, enum devact act)
 2445 {
 2446         struct fxp_softc *sc = (void *) self;
 2447         int s, error = 0;
 2448 
 2449         s = splnet();
 2450         switch (act) {
 2451         case DVACT_ACTIVATE:
 2452                 error = EOPNOTSUPP;
 2453                 break;
 2454 
 2455         case DVACT_DEACTIVATE:
 2456                 if (sc->sc_flags & FXPF_MII)
 2457                         mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
 2458                             MII_OFFSET_ANY);
 2459                 if_deactivate(&sc->sc_ethercom.ec_if);
 2460                 break;
 2461         }
 2462         splx(s);
 2463 
 2464         return (error);
 2465 }
 2466 
 2467 /*
 2468  * fxp_detach:
 2469  *
 2470  *      Detach an i82557 interface.
 2471  */
 2472 int
 2473 fxp_detach(struct fxp_softc *sc)
 2474 {
 2475         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 2476         int i;
 2477 
 2478         /* Succeed now if there's no work to do. */
 2479         if ((sc->sc_flags & FXPF_ATTACHED) == 0)
 2480                 return (0);
 2481 
 2482         /* Unhook our tick handler. */
 2483         callout_stop(&sc->sc_callout);
 2484 
 2485         if (sc->sc_flags & FXPF_MII) {
 2486                 /* Detach all PHYs */
 2487                 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
 2488         }
 2489 
 2490         /* Delete all remaining media. */
 2491         ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
 2492 
 2493 #if NRND > 0
 2494         rnd_detach_source(&sc->rnd_source);
 2495 #endif
 2496         ether_ifdetach(ifp);
 2497         if_detach(ifp);
 2498 
 2499         for (i = 0; i < FXP_NRFABUFS; i++) {
 2500                 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmaps[i]);
 2501                 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmaps[i]);
 2502         }
 2503 
 2504         for (i = 0; i < FXP_NTXCB; i++) {
 2505                 bus_dmamap_unload(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
 2506                 bus_dmamap_destroy(sc->sc_dmat, FXP_DSTX(sc, i)->txs_dmamap);
 2507         }
 2508 
 2509         bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap);
 2510         bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap);
 2511         bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
 2512             sizeof(struct fxp_control_data));
 2513         bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
 2514 
 2515         shutdownhook_disestablish(sc->sc_sdhook);
 2516         powerhook_disestablish(sc->sc_powerhook);
 2517 
 2518         return (0);
 2519 }

Cache object: 67328b45160cb67e0ad37d583135ecc9


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