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/pci/if_sk.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: if_sk.c,v 1.7.2.4 2005/01/07 12:13:50 jdc Exp $        */
    2 
    3 /*-
    4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by the NetBSD
   18  *      Foundation, Inc. and its contributors.
   19  * 4. Neither the name of The NetBSD Foundation nor the names of its
   20  *    contributors may be used to endorse or promote products derived
   21  *    from this software without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33  * POSSIBILITY OF SUCH DAMAGE.
   34  */
   35 
   36 /*      $OpenBSD: if_sk.c,v 1.33 2003/08/12 05:23:06 nate Exp $ */
   37 
   38 /*
   39  * Copyright (c) 1997, 1998, 1999, 2000
   40  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
   41  *
   42  * Redistribution and use in source and binary forms, with or without
   43  * modification, are permitted provided that the following conditions
   44  * are met:
   45  * 1. Redistributions of source code must retain the above copyright
   46  *    notice, this list of conditions and the following disclaimer.
   47  * 2. Redistributions in binary form must reproduce the above copyright
   48  *    notice, this list of conditions and the following disclaimer in the
   49  *    documentation and/or other materials provided with the distribution.
   50  * 3. All advertising materials mentioning features or use of this software
   51  *    must display the following acknowledgement:
   52  *      This product includes software developed by Bill Paul.
   53  * 4. Neither the name of the author nor the names of any co-contributors
   54  *    may be used to endorse or promote products derived from this software
   55  *    without specific prior written permission.
   56  *
   57  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   60  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   61  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   62  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   63  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   64  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   65  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   66  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   67  * THE POSSIBILITY OF SUCH DAMAGE.
   68  *
   69  * $FreeBSD: /c/ncvs/src/sys/pci/if_sk.c,v 1.20 2000/04/22 02:16:37 wpaul Exp $
   70  */
   71 
   72 /*
   73  * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu>
   74  *
   75  * Permission to use, copy, modify, and distribute this software for any
   76  * purpose with or without fee is hereby granted, provided that the above
   77  * copyright notice and this permission notice appear in all copies.
   78  *
   79  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   80  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   81  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   82  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   83  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   84  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   85  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   86  */
   87 
   88 /*
   89  * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports
   90  * the SK-984x series adapters, both single port and dual port.
   91  * References:
   92  *      The XaQti XMAC II datasheet,
   93  * http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
   94  *      The SysKonnect GEnesis manual, http://www.syskonnect.com
   95  *
   96  * Note: XaQti has been acquired by Vitesse, and Vitesse does not have the
   97  * XMAC II datasheet online. I have put my copy at people.freebsd.org as a
   98  * convenience to others until Vitesse corrects this problem:
   99  *
  100  * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf
  101  *
  102  * Written by Bill Paul <wpaul@ee.columbia.edu>
  103  * Department of Electrical Engineering
  104  * Columbia University, New York City
  105  */
  106 
  107 /*
  108  * The SysKonnect gigabit ethernet adapters consist of two main
  109  * components: the SysKonnect GEnesis controller chip and the XaQti Corp.
  110  * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC
  111  * components and a PHY while the GEnesis controller provides a PCI
  112  * interface with DMA support. Each card may have between 512K and
  113  * 2MB of SRAM on board depending on the configuration.
  114  *
  115  * The SysKonnect GEnesis controller can have either one or two XMAC
  116  * chips connected to it, allowing single or dual port NIC configurations.
  117  * SysKonnect has the distinction of being the only vendor on the market
  118  * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs,
  119  * dual DMA queues, packet/MAC/transmit arbiters and direct access to the
  120  * XMAC registers. This driver takes advantage of these features to allow
  121  * both XMACs to operate as independent interfaces.
  122  */
  123  
  124 #include "bpfilter.h"
  125 
  126 #include <sys/param.h>
  127 #include <sys/systm.h>
  128 #include <sys/sockio.h>
  129 #include <sys/mbuf.h>
  130 #include <sys/malloc.h>
  131 #include <sys/kernel.h>
  132 #include <sys/socket.h>
  133 #include <sys/device.h>
  134 #include <sys/queue.h>
  135 #include <sys/callout.h>
  136 
  137 #include <net/if.h>
  138 #include <net/if_dl.h>
  139 #include <net/if_types.h>
  140 
  141 #ifdef INET
  142 #include <netinet/in.h>
  143 #include <netinet/in_systm.h>
  144 #include <netinet/in_var.h>
  145 #include <netinet/ip.h>
  146 #include <netinet/if_ether.h>
  147 #endif
  148 
  149 #include <net/if_media.h>
  150 
  151 #if NBPFILTER > 0
  152 #include <net/bpf.h>
  153 #endif
  154 
  155 #include <dev/mii/mii.h>
  156 #include <dev/mii/miivar.h>
  157 #include <dev/mii/brgphyreg.h>
  158 
  159 #include <dev/pci/pcireg.h>
  160 #include <dev/pci/pcivar.h>
  161 #include <dev/pci/pcidevs.h>
  162 
  163 #define SK_VERBOSE
  164 /* #define SK_USEIOSPACE */
  165 
  166 #include <dev/pci/if_skreg.h>
  167 #include <dev/pci/if_skvar.h>
  168 
  169 int skc_probe(struct device *, struct cfdata *, void *);
  170 void skc_attach(struct device *, struct device *self, void *aux);
  171 int sk_probe(struct device *, struct cfdata *, void *);
  172 void sk_attach(struct device *, struct device *self, void *aux);
  173 int skcprint(void *, const char *);
  174 int sk_intr(void *);
  175 void sk_intr_bcom(struct sk_if_softc *);
  176 void sk_intr_xmac(struct sk_if_softc *);
  177 void sk_intr_yukon(struct sk_if_softc *);
  178 void sk_rxeof(struct sk_if_softc *);
  179 void sk_txeof(struct sk_if_softc *);
  180 int sk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
  181 void sk_start(struct ifnet *);
  182 int sk_ioctl(struct ifnet *, u_long, caddr_t);
  183 int sk_init(struct ifnet *);
  184 void sk_init_xmac(struct sk_if_softc *);
  185 void sk_init_yukon(struct sk_if_softc *);
  186 void sk_stop(struct ifnet *, int);
  187 void sk_watchdog(struct ifnet *);
  188 void sk_shutdown(void *);
  189 int sk_ifmedia_upd(struct ifnet *);
  190 void sk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
  191 void sk_reset(struct sk_softc *);
  192 int sk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
  193 int sk_init_rx_ring(struct sk_if_softc *);
  194 int sk_init_tx_ring(struct sk_if_softc *);
  195 u_int8_t sk_vpd_readbyte(struct sk_softc *, int);
  196 void sk_vpd_read_res(struct sk_softc *,
  197                                         struct vpd_res *, int);
  198 void sk_vpd_read(struct sk_softc *);
  199 
  200 int sk_xmac_miibus_readreg(struct device *, int, int);
  201 void sk_xmac_miibus_writereg(struct device *, int, int, int);
  202 void sk_xmac_miibus_statchg(struct device *);
  203 
  204 int sk_marv_miibus_readreg(struct device *, int, int);
  205 void sk_marv_miibus_writereg(struct device *, int, int, int);
  206 void sk_marv_miibus_statchg(struct device *);
  207 
  208 u_int32_t sk_xmac_hash(caddr_t);
  209 u_int32_t sk_yukon_hash(caddr_t);
  210 void sk_setfilt(struct sk_if_softc *, caddr_t, int);
  211 void sk_setmulti(struct sk_if_softc *);
  212 void sk_tick(void *);
  213 
  214 /* #define SK_DEBUG 2 */
  215 #ifdef SK_DEBUG
  216 #define DPRINTF(x)      if (skdebug) printf x
  217 #define DPRINTFN(n,x)   if (skdebug >= (n)) printf x
  218 int     skdebug = SK_DEBUG;
  219 
  220 void sk_dump_txdesc(struct sk_tx_desc *, int);
  221 void sk_dump_mbuf(struct mbuf *);
  222 void sk_dump_bytes(const char *, int);
  223 #else
  224 #define DPRINTF(x)
  225 #define DPRINTFN(n,x)
  226 #endif
  227 
  228 #define SK_SETBIT(sc, reg, x)           \
  229         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x)
  230 
  231 #define SK_CLRBIT(sc, reg, x)           \
  232         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x)
  233 
  234 #define SK_WIN_SETBIT_4(sc, reg, x)     \
  235         sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x)
  236 
  237 #define SK_WIN_CLRBIT_4(sc, reg, x)     \
  238         sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x)
  239 
  240 #define SK_WIN_SETBIT_2(sc, reg, x)     \
  241         sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x)
  242 
  243 #define SK_WIN_CLRBIT_2(sc, reg, x)     \
  244         sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x)
  245 
  246 /* supported device vendors */
  247 static const struct sk_product { 
  248         pci_vendor_id_t         sk_vendor;
  249         pci_product_id_t        sk_product;
  250 } sk_products[] = {
  251         { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3C940, },
  252         { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T, },
  253         { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032, },
  254         { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1064, },
  255         { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE, },
  256         { PCI_VENDOR_SCHNEIDERKOCH, PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2, },
  257         { PCI_VENDOR_GALILEO, PCI_PRODUCT_GALILEO_SKNET, },
  258         { 0, 0, }
  259 };
  260 
  261 static inline u_int32_t
  262 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
  263 {
  264 #ifdef SK_USEIOSPACE
  265         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  266         return CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg));
  267 #else
  268         return CSR_READ_4(sc, reg);
  269 #endif
  270 }
  271 
  272 static inline u_int16_t
  273 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
  274 {
  275 #ifdef SK_USEIOSPACE
  276         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  277         return CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg));
  278 #else
  279         return CSR_READ_2(sc, reg);
  280 #endif
  281 }
  282 
  283 static inline u_int8_t
  284 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
  285 {
  286 #ifdef SK_USEIOSPACE
  287         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  288         return CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg));
  289 #else
  290         return CSR_READ_1(sc, reg);
  291 #endif
  292 }
  293 
  294 static inline void
  295 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
  296 {
  297 #ifdef SK_USEIOSPACE
  298         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  299         CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), x);
  300 #else
  301         CSR_WRITE_4(sc, reg, x);
  302 #endif
  303 }
  304 
  305 static inline void
  306 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
  307 {
  308 #ifdef SK_USEIOSPACE
  309         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  310         CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), x);
  311 #else
  312         CSR_WRITE_2(sc, reg, x);
  313 #endif
  314 }
  315 
  316 static inline void
  317 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
  318 {
  319 #ifdef SK_USEIOSPACE
  320         CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg));
  321         CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), x);
  322 #else
  323         CSR_WRITE_1(sc, reg, x);
  324 #endif
  325 }
  326 
  327 /*
  328  * The VPD EEPROM contains Vital Product Data, as suggested in
  329  * the PCI 2.1 specification. The VPD data is separared into areas
  330  * denoted by resource IDs. The SysKonnect VPD contains an ID string
  331  * resource (the name of the adapter), a read-only area resource
  332  * containing various key/data fields and a read/write area which
  333  * can be used to store asset management information or log messages.
  334  * We read the ID string and read-only into buffers attached to
  335  * the controller softc structure for later use. At the moment,
  336  * we only use the ID string during sk_attach().
  337  */
  338 u_int8_t
  339 sk_vpd_readbyte(struct sk_softc *sc, int addr)
  340 {
  341         int                     i;
  342 
  343         sk_win_write_2(sc, SK_PCI_REG(SK_PCI_VPD_ADDR), addr);
  344         for (i = 0; i < SK_TIMEOUT; i++) {
  345                 DELAY(1);
  346                 if (sk_win_read_2(sc,
  347                     SK_PCI_REG(SK_PCI_VPD_ADDR)) & SK_VPD_FLAG)
  348                         break;
  349         }
  350 
  351         if (i == SK_TIMEOUT)
  352                 return(0);
  353 
  354         return(sk_win_read_1(sc, SK_PCI_REG(SK_PCI_VPD_DATA)));
  355 }
  356 
  357 void
  358 sk_vpd_read_res(struct sk_softc *sc, struct vpd_res *res, int addr)
  359 {
  360         int                     i;
  361         u_int8_t                *ptr;
  362 
  363         ptr = (u_int8_t *)res;
  364         for (i = 0; i < sizeof(struct vpd_res); i++)
  365                 ptr[i] = sk_vpd_readbyte(sc, i + addr);
  366 }
  367 
  368 void
  369 sk_vpd_read(struct sk_softc *sc)
  370 {
  371         int                     pos = 0, i;
  372         struct vpd_res          res;
  373 
  374         if (sc->sk_vpd_prodname != NULL)
  375                 free(sc->sk_vpd_prodname, M_DEVBUF);
  376         if (sc->sk_vpd_readonly != NULL)
  377                 free(sc->sk_vpd_readonly, M_DEVBUF);
  378         sc->sk_vpd_prodname = NULL;
  379         sc->sk_vpd_readonly = NULL;
  380 
  381         sk_vpd_read_res(sc, &res, pos);
  382 
  383         if (res.vr_id != VPD_RES_ID) {
  384                 printf("%s: bad VPD resource id: expected %x got %x\n",
  385                     sc->sk_dev.dv_xname, VPD_RES_ID, res.vr_id);
  386                 return;
  387         }
  388 
  389         pos += sizeof(res);
  390         sc->sk_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
  391         if (sc->sk_vpd_prodname == NULL)
  392                 panic("sk_vpd_read");
  393         for (i = 0; i < res.vr_len; i++)
  394                 sc->sk_vpd_prodname[i] = sk_vpd_readbyte(sc, i + pos);
  395         sc->sk_vpd_prodname[i] = '\0';
  396         pos += i;
  397 
  398         sk_vpd_read_res(sc, &res, pos);
  399 
  400         if (res.vr_id != VPD_RES_READ) {
  401                 printf("%s: bad VPD resource id: expected %x got %x\n",
  402                     sc->sk_dev.dv_xname, VPD_RES_READ, res.vr_id);
  403                 return;
  404         }
  405 
  406         pos += sizeof(res);
  407         sc->sk_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
  408         if (sc->sk_vpd_readonly == NULL)
  409                 panic("sk_vpd_read");
  410         for (i = 0; i < res.vr_len ; i++)
  411                 sc->sk_vpd_readonly[i] = sk_vpd_readbyte(sc, i + pos);
  412 }
  413 
  414 int
  415 sk_xmac_miibus_readreg(struct device *dev, int phy, int reg)
  416 {
  417         struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
  418         int i;
  419 
  420         DPRINTFN(9, ("sk_xmac_miibus_readreg\n"));
  421 
  422         if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0)
  423                 return(0);
  424 
  425         SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
  426         SK_XM_READ_2(sc_if, XM_PHY_DATA);
  427         if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
  428                 for (i = 0; i < SK_TIMEOUT; i++) {
  429                         DELAY(1);
  430                         if (SK_XM_READ_2(sc_if, XM_MMUCMD) &
  431                             XM_MMUCMD_PHYDATARDY)
  432                                 break;
  433                 }
  434 
  435                 if (i == SK_TIMEOUT) {
  436                         printf("%s: phy failed to come ready\n",
  437                             sc_if->sk_dev.dv_xname);
  438                         return(0);
  439                 }
  440         }
  441         DELAY(1);
  442         return(SK_XM_READ_2(sc_if, XM_PHY_DATA));
  443 }
  444 
  445 void
  446 sk_xmac_miibus_writereg(struct device *dev, int phy, int reg, int val)
  447 {
  448         struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
  449         int i;
  450 
  451         DPRINTFN(9, ("sk_xmac_miibus_writereg\n"));
  452 
  453         SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8));
  454         for (i = 0; i < SK_TIMEOUT; i++) {
  455                 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
  456                         break;
  457         }
  458 
  459         if (i == SK_TIMEOUT) {
  460                 printf("%s: phy failed to come ready\n",
  461                     sc_if->sk_dev.dv_xname);
  462                 return;
  463         }
  464 
  465         SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val);
  466         for (i = 0; i < SK_TIMEOUT; i++) {
  467                 DELAY(1);
  468                 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY))
  469                         break;
  470         }
  471 
  472         if (i == SK_TIMEOUT)
  473                 printf("%s: phy write timed out\n", sc_if->sk_dev.dv_xname);
  474 }
  475 
  476 void
  477 sk_xmac_miibus_statchg(struct device *dev)
  478 {
  479         struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
  480         struct mii_data *mii = &sc_if->sk_mii;
  481 
  482         DPRINTFN(9, ("sk_xmac_miibus_statchg\n"));
  483 
  484         /*
  485          * If this is a GMII PHY, manually set the XMAC's
  486          * duplex mode accordingly.
  487          */
  488         if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) {
  489                 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
  490                         SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
  491                 } else {
  492                         SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX);
  493                 }
  494         }
  495 }
  496 
  497 int
  498 sk_marv_miibus_readreg(dev, phy, reg)
  499         struct device *dev;
  500         int phy, reg;
  501 {
  502         struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
  503         u_int16_t val;
  504         int i;
  505 
  506         if (phy != 0 ||
  507             (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER &&
  508              sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) {
  509                 DPRINTFN(9, ("sk_marv_miibus_readreg (skip) phy=%d, reg=%#x\n",
  510                              phy, reg));
  511                 return(0);
  512         }
  513 
  514         SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
  515                       YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
  516         
  517         for (i = 0; i < SK_TIMEOUT; i++) {
  518                 DELAY(1);
  519                 val = SK_YU_READ_2(sc_if, YUKON_SMICR);
  520                 if (val & YU_SMICR_READ_VALID)
  521                         break;
  522         }
  523 
  524         if (i == SK_TIMEOUT) {
  525                 printf("%s: phy failed to come ready\n",
  526                        sc_if->sk_dev.dv_xname);
  527                 return 0;
  528         }
  529         
  530         DPRINTFN(9, ("sk_marv_miibus_readreg: i=%d, timeout=%d\n", i,
  531                      SK_TIMEOUT));
  532 
  533         val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
  534 
  535         DPRINTFN(9, ("sk_marv_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
  536                      phy, reg, val));
  537 
  538         return val;
  539 }
  540 
  541 void
  542 sk_marv_miibus_writereg(dev, phy, reg, val)
  543         struct device *dev;
  544         int phy, reg, val;
  545 {
  546         struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
  547         int i;
  548 
  549         DPRINTFN(9, ("sk_marv_miibus_writereg phy=%d reg=%#x val=%#x\n",
  550                      phy, reg, val));
  551 
  552         SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
  553         SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
  554                       YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
  555 
  556         for (i = 0; i < SK_TIMEOUT; i++) {
  557                 DELAY(1);
  558                 if (SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY)
  559                         break;
  560         }
  561 }
  562 
  563 void
  564 sk_marv_miibus_statchg(dev)
  565         struct device *dev;
  566 {
  567         DPRINTFN(9, ("sk_marv_miibus_statchg: gpcr=%x\n",
  568                      SK_YU_READ_2(((struct sk_if_softc *)dev), YUKON_GPCR)));
  569 }
  570 
  571 #define SK_HASH_BITS            6
  572 
  573 u_int32_t
  574 sk_xmac_hash(caddr_t addr)
  575 {
  576         u_int32_t               crc;
  577 
  578         crc = ether_crc32_le(addr,ETHER_ADDR_LEN);
  579         crc = ~crc & ((1<< SK_HASH_BITS) - 1);  
  580         DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
  581         return (crc);
  582 }
  583 
  584 u_int32_t
  585 sk_yukon_hash(caddr_t addr)
  586 {
  587         u_int32_t               crc;
  588 
  589         crc = ether_crc32_be(addr,ETHER_ADDR_LEN);
  590         crc &= ((1 << SK_HASH_BITS) - 1);
  591         DPRINTFN(2,("multicast hash for %s is %x\n",ether_sprintf(addr),crc));
  592         return (crc);
  593 }
  594 
  595 void
  596 sk_setfilt(struct sk_if_softc *sc_if, caddr_t addr, int slot)
  597 {
  598         int base = XM_RXFILT_ENTRY(slot);
  599 
  600         SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
  601         SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
  602         SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
  603 }
  604 
  605 void
  606 sk_setmulti(struct sk_if_softc *sc_if)
  607 {
  608         struct sk_softc *sc = sc_if->sk_softc;
  609         struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
  610         u_int32_t hashes[2] = { 0, 0 };
  611         int h = 0, i;
  612         struct ethercom *ec = &sc_if->sk_ethercom;
  613         struct ether_multi *enm;
  614         struct ether_multistep step;
  615         u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
  616 
  617         /* First, zot all the existing filters. */
  618         switch(sc->sk_type) {
  619         case SK_GENESIS:
  620                 for (i = 1; i < XM_RXFILT_MAX; i++)
  621                         sk_setfilt(sc_if, (caddr_t)&dummy, i);
  622 
  623                 SK_XM_WRITE_4(sc_if, XM_MAR0, 0);
  624                 SK_XM_WRITE_4(sc_if, XM_MAR2, 0);
  625                 break;
  626         case SK_YUKON:
  627         case SK_YUKON_LITE:
  628         case SK_YUKON_LP:
  629                 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
  630                 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
  631                 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
  632                 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
  633                 break;
  634         }
  635 
  636         /* Now program new ones. */
  637 allmulti:
  638         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
  639                 hashes[0] = 0xFFFFFFFF;
  640                 hashes[1] = 0xFFFFFFFF;
  641         } else {
  642                 i = 1;
  643                 /* First find the tail of the list. */
  644                 ETHER_FIRST_MULTI(step, ec, enm);
  645                 while (enm != NULL) {
  646                         if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
  647                                  ETHER_ADDR_LEN)) {
  648                                 ifp->if_flags |= IFF_ALLMULTI;
  649                                 goto allmulti;
  650                         }
  651                         DPRINTFN(2,("multicast address %s\n",
  652                                 ether_sprintf(enm->enm_addrlo)));
  653                         /*
  654                          * Program the first XM_RXFILT_MAX multicast groups
  655                          * into the perfect filter. For all others,
  656                          * use the hash table.
  657                          */
  658                         if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) {
  659                                 sk_setfilt(sc_if, enm->enm_addrlo, i);
  660                                 i++;
  661                         }
  662                         else {
  663                                 switch (sc->sk_type) {
  664                                 case SK_GENESIS:
  665                                         h = sk_xmac_hash(enm->enm_addrlo);
  666                                         break;
  667                                 case SK_YUKON:
  668                                 case SK_YUKON_LITE:
  669                                 case SK_YUKON_LP:
  670                                         h = sk_yukon_hash(enm->enm_addrlo);
  671                                         break;
  672                                 }
  673                                 if (h < 32)
  674                                         hashes[0] |= (1 << h);
  675                                 else
  676                                         hashes[1] |= (1 << (h - 32));
  677                         }
  678 
  679                         ETHER_NEXT_MULTI(step, enm);
  680                 }
  681         }
  682 
  683         switch(sc->sk_type) {
  684         case SK_GENESIS:
  685                 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH|
  686                                XM_MODE_RX_USE_PERFECT);
  687                 SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]);
  688                 SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]);
  689                 break;
  690         case SK_YUKON:
  691         case SK_YUKON_LITE:
  692         case SK_YUKON_LP:
  693                 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
  694                 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
  695                 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
  696                 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
  697                 break;
  698         }
  699 }
  700 
  701 int
  702 sk_init_rx_ring(struct sk_if_softc *sc_if)
  703 {
  704         struct sk_chain_data    *cd = &sc_if->sk_cdata;
  705         struct sk_ring_data     *rd = sc_if->sk_rdata;
  706         int                     i;
  707 
  708         bzero((char *)rd->sk_rx_ring,
  709             sizeof(struct sk_rx_desc) * SK_RX_RING_CNT);
  710 
  711         for (i = 0; i < SK_RX_RING_CNT; i++) {
  712                 cd->sk_rx_chain[i].sk_desc = &rd->sk_rx_ring[i];
  713                 if (i == (SK_RX_RING_CNT - 1)) {
  714                         cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[0];
  715                         rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if, 0);
  716                 } else {
  717                         cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[i + 1];
  718                         rd->sk_rx_ring[i].sk_next = SK_RX_RING_ADDR(sc_if,i+1);
  719                 }
  720         }
  721 
  722         for (i = 0; i < SK_RX_RING_CNT; i++) {
  723                 if (sk_newbuf(sc_if, i, NULL, NULL) == ENOBUFS) {
  724                         printf("%s: failed alloc of %dth mbuf\n",
  725                             sc_if->sk_dev.dv_xname, i);
  726                         return(ENOBUFS);
  727                 }
  728         }
  729         sc_if->sk_cdata.sk_rx_prod = 0;
  730         sc_if->sk_cdata.sk_rx_cons = 0;
  731 
  732         return(0);
  733 }
  734 
  735 int
  736 sk_init_tx_ring(struct sk_if_softc *sc_if)
  737 {
  738         struct sk_chain_data    *cd = &sc_if->sk_cdata;
  739         struct sk_ring_data     *rd = sc_if->sk_rdata;
  740         int                     i;
  741 
  742         bzero((char *)sc_if->sk_rdata->sk_tx_ring,
  743             sizeof(struct sk_tx_desc) * SK_TX_RING_CNT);
  744 
  745         for (i = 0; i < SK_TX_RING_CNT; i++) {
  746                 cd->sk_tx_chain[i].sk_desc = &rd->sk_tx_ring[i];
  747                 if (i == (SK_TX_RING_CNT - 1)) {
  748                         cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[0];
  749                         rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if, 0);
  750                 } else {
  751                         cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[i + 1];
  752                         rd->sk_tx_ring[i].sk_next = SK_TX_RING_ADDR(sc_if,i+1);
  753                 }
  754         }
  755 
  756         sc_if->sk_cdata.sk_tx_prod = 0;
  757         sc_if->sk_cdata.sk_tx_cons = 0;
  758         sc_if->sk_cdata.sk_tx_cnt = 0;
  759 
  760         SK_CDTXSYNC(sc_if, 0, SK_TX_RING_CNT,
  761             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  762 
  763         return (0);
  764 }
  765 
  766 int
  767 sk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
  768           bus_dmamap_t dmamap)
  769 {
  770         struct sk_softc         *sc = sc_if->sk_softc;
  771         struct mbuf             *m_new = NULL;
  772         struct sk_chain         *c;
  773         struct sk_rx_desc       *r;
  774 
  775         if (dmamap == NULL) {
  776                 /* if (m) panic() */
  777 
  778                 if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1, MCLBYTES,
  779                                       0, BUS_DMA_NOWAIT, &dmamap)) {
  780                         printf("%s: can't create recv map\n",
  781                                sc_if->sk_dev.dv_xname);
  782                         return(ENOMEM);
  783                 }
  784         } else if (m == NULL)
  785                 bus_dmamap_unload(sc->sc_dmatag, dmamap);
  786 
  787         sc_if->sk_cdata.sk_rx_map[i] = dmamap;
  788 
  789         if (m == NULL) {
  790                 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
  791                 if (m_new == NULL) {
  792                         printf("%s: no memory for rx list -- "
  793                             "packet dropped!\n", sc_if->sk_dev.dv_xname);
  794                         return(ENOBUFS);
  795                 }
  796 
  797                 /* Allocate the jumbo buffer */
  798                 MCLGET(m_new, M_DONTWAIT);
  799                 if (!(m_new->m_flags & M_EXT)) {
  800                         m_freem(m_new);
  801                         return (ENOBUFS);
  802                 }
  803 
  804                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
  805 
  806                 m_adj(m_new, ETHER_ALIGN);
  807 
  808                 if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m_new,
  809                                          BUS_DMA_NOWAIT))
  810                         return(ENOBUFS);
  811         } else {
  812                 /*
  813                  * We're re-using a previously allocated mbuf;
  814                  * be sure to re-init pointers and lengths to
  815                  * default values.
  816                  */
  817                 m_new = m;
  818                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
  819                 m_adj(m_new, ETHER_ALIGN);
  820                 m_new->m_data = m_new->m_ext.ext_buf;
  821         }
  822 
  823         c = &sc_if->sk_cdata.sk_rx_chain[i];
  824         r = c->sk_desc;
  825         c->sk_mbuf = m_new;
  826         r->sk_data_lo = dmamap->dm_segs[0].ds_addr;
  827         r->sk_ctl = dmamap->dm_segs[0].ds_len | SK_RXSTAT;
  828 
  829         SK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
  830 
  831         return(0);
  832 }
  833 
  834 /*
  835  * Set media options.
  836  */
  837 int
  838 sk_ifmedia_upd(struct ifnet *ifp)
  839 {
  840         struct sk_if_softc *sc_if = ifp->if_softc;
  841 
  842         (void) sk_init(ifp);
  843         mii_mediachg(&sc_if->sk_mii);
  844         return(0);
  845 }
  846 
  847 /*
  848  * Report current media status.
  849  */
  850 void
  851 sk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
  852 {
  853         struct sk_if_softc *sc_if = ifp->if_softc;
  854 
  855         mii_pollstat(&sc_if->sk_mii);
  856         ifmr->ifm_active = sc_if->sk_mii.mii_media_active;
  857         ifmr->ifm_status = sc_if->sk_mii.mii_media_status;
  858 }
  859 
  860 int
  861 sk_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
  862 {
  863         struct sk_if_softc *sc_if = ifp->if_softc;
  864         struct sk_softc *sc = sc_if->sk_softc;
  865         struct ifreq *ifr = (struct ifreq *) data;
  866         /* struct ifaddr *ifa = (struct ifaddr *) data; */
  867         struct mii_data *mii;
  868         int s, error = 0;
  869 
  870         /* DPRINTFN(2, ("sk_ioctl\n")); */
  871 
  872         s = splnet();
  873 
  874         switch(command) {
  875 
  876         case SIOCSIFFLAGS:
  877                 DPRINTFN(2, ("sk_ioctl IFFLAGS\n"));
  878                 if (ifp->if_flags & IFF_UP) {
  879                         if (ifp->if_flags & IFF_RUNNING &&
  880                             ifp->if_flags & IFF_PROMISC &&
  881                             !(sc_if->sk_if_flags & IFF_PROMISC)) {
  882                                 switch(sc->sk_type) {
  883                                 case SK_GENESIS:
  884                                         SK_XM_SETBIT_4(sc_if, XM_MODE,
  885                                             XM_MODE_RX_PROMISC);
  886                                         break;
  887                                 case SK_YUKON:
  888                                 case SK_YUKON_LITE:
  889                                 case SK_YUKON_LP:
  890                                         SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
  891                                             YU_RCR_UFLEN | YU_RCR_MUFLEN);
  892                                         break;
  893                                 }
  894                                 sk_setmulti(sc_if);
  895                         } else if (ifp->if_flags & IFF_RUNNING &&
  896                             !(ifp->if_flags & IFF_PROMISC) &&
  897                             sc_if->sk_if_flags & IFF_PROMISC) {
  898                                 switch(sc->sk_type) {
  899                                 case SK_GENESIS:
  900                                         SK_XM_CLRBIT_4(sc_if, XM_MODE,
  901                                             XM_MODE_RX_PROMISC);
  902                                         break;
  903                                 case SK_YUKON:
  904                                 case SK_YUKON_LITE:
  905                                 case SK_YUKON_LP:
  906                                         SK_YU_SETBIT_2(sc_if, YUKON_RCR,
  907                                             YU_RCR_UFLEN | YU_RCR_MUFLEN);
  908                                         break;
  909                                 }
  910 
  911                                 sk_setmulti(sc_if);
  912                         } else
  913                                 (void) sk_init(ifp);
  914                 } else {
  915                         if (ifp->if_flags & IFF_RUNNING)
  916                                 sk_stop(ifp,0);
  917                 }
  918                 sc_if->sk_if_flags = ifp->if_flags;
  919                 error = 0;
  920                 break;
  921 
  922         case SIOCGIFMEDIA:
  923         case SIOCSIFMEDIA:
  924                 DPRINTFN(2, ("sk_ioctl MEDIA\n"));
  925                 mii = &sc_if->sk_mii;
  926                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
  927                 break;
  928         default:
  929                 DPRINTFN(2, ("sk_ioctl ETHER\n"));
  930                 error = ether_ioctl(ifp, command, data);
  931 
  932                 if ( error == ENETRESET) {
  933                         sk_setmulti(sc_if);
  934                         DPRINTFN(2, ("sk_ioctl setmulti called\n"));
  935                         error = 0;
  936                 } else if ( error ) {
  937                         splx(s);
  938                         return error;
  939                 }
  940                 break;
  941         }
  942 
  943         splx(s);
  944         return(error);
  945 }
  946 
  947 /*
  948  * Lookup: Check the PCI vendor and device, and return a pointer to
  949  * The structure if the IDs match against our list.
  950  */
  951 
  952 static const struct sk_product *
  953 sk_lookup(const struct pci_attach_args *pa)
  954 {
  955         const struct sk_product *psk;
  956         
  957         for ( psk = &sk_products[0]; psk->sk_vendor != 0; psk++ ) {
  958                 if (PCI_VENDOR(pa->pa_id) == psk->sk_vendor &&
  959                     PCI_PRODUCT(pa->pa_id) == psk->sk_product)
  960                         return (psk);
  961         }
  962         return (NULL);
  963 }
  964 
  965 /*
  966  * Probe for a SysKonnect GEnesis chip.
  967  */
  968 
  969 int
  970 skc_probe(struct device *parent, struct cfdata *match, void *aux)
  971 {
  972         struct pci_attach_args *pa = (struct pci_attach_args *)aux;
  973         const struct sk_product *psk;
  974         
  975         if ((psk = sk_lookup(pa))) {
  976                 return(1);
  977         }
  978         return(0);
  979 }
  980 
  981 /*
  982  * Force the GEnesis into reset, then bring it out of reset.
  983  */
  984 void sk_reset(struct sk_softc *sc)
  985 {
  986         DPRINTFN(2, ("sk_reset\n"));
  987 
  988         CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET);
  989         CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET);
  990         if (SK_YUKON_FAMILY(sc->sk_type))
  991                 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
  992 
  993         DELAY(1000);
  994         CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET);
  995         DELAY(2);
  996         CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
  997         if (SK_YUKON_FAMILY(sc->sk_type))
  998                 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
  999 
 1000         DPRINTFN(2, ("sk_reset: sk_csr=%x\n", CSR_READ_2(sc, SK_CSR)));
 1001         DPRINTFN(2, ("sk_reset: sk_link_ctrl=%x\n",
 1002                      CSR_READ_2(sc, SK_LINK_CTRL)));
 1003 
 1004         if (sc->sk_type == SK_GENESIS) {
 1005                 /* Configure packet arbiter */
 1006                 sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET);
 1007                 sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT);
 1008                 sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT);
 1009                 sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT);
 1010                 sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT);
 1011         }
 1012 
 1013         /* Enable RAM interface */
 1014         sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
 1015 
 1016         /*
 1017          * Configure interrupt moderation. The moderation timer
 1018          * defers interrupts specified in the interrupt moderation
 1019          * timer mask based on the timeout specified in the interrupt
 1020          * moderation timer init register. Each bit in the timer
 1021          * register represents 18.825ns, so to specify a timeout in
 1022          * microseconds, we have to multiply by 54.
 1023          */
 1024         sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(100));
 1025         sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
 1026             SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
 1027         sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
 1028 }
 1029 
 1030 int
 1031 sk_probe(struct device *parent, struct cfdata *match, void *aux)
 1032 {
 1033         struct skc_attach_args *sa = aux;
 1034 
 1035         if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
 1036                 return(0);
 1037 
 1038         return (1);
 1039 }
 1040 
 1041 /*
 1042  * Each XMAC chip is attached as a separate logical IP interface.
 1043  * Single port cards will have only one logical interface of course.
 1044  */
 1045 void
 1046 sk_attach(struct device *parent, struct device *self, void *aux)
 1047 {
 1048         struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
 1049         struct sk_softc *sc = (struct sk_softc *)parent;
 1050         struct skc_attach_args *sa = aux;
 1051         struct sk_txmap_entry   *entry;
 1052         struct ifnet *ifp;
 1053         bus_dma_segment_t seg;
 1054         bus_dmamap_t dmamap;
 1055         caddr_t kva;
 1056         int i, rseg;
 1057 
 1058         sc_if->sk_port = sa->skc_port;
 1059         sc_if->sk_softc = sc;
 1060         sc->sk_if[sa->skc_port] = sc_if;
 1061 
 1062         if (sa->skc_port == SK_PORT_A)
 1063                 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0;
 1064         if (sa->skc_port == SK_PORT_B)
 1065                 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1;
 1066 
 1067         DPRINTFN(2, ("begin sk_attach: port=%d\n", sc_if->sk_port));
 1068 
 1069         /*
 1070          * Get station address for this interface. Note that
 1071          * dual port cards actually come with three station
 1072          * addresses: one for each port, plus an extra. The
 1073          * extra one is used by the SysKonnect driver software
 1074          * as a 'virtual' station address for when both ports
 1075          * are operating in failover mode. Currently we don't
 1076          * use this extra address.
 1077          */
 1078         for (i = 0; i < ETHER_ADDR_LEN; i++)
 1079                 sc_if->sk_enaddr[i] =
 1080                         sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
 1081 
 1082 
 1083         aprint_normal(": Ethernet address %s\n",
 1084             ether_sprintf(sc_if->sk_enaddr));
 1085 
 1086         /*
 1087          * Set up RAM buffer addresses. The NIC will have a certain
 1088          * amount of SRAM on it, somewhere between 512K and 2MB. We
 1089          * need to divide this up a) between the transmitter and
 1090          * receiver and b) between the two XMACs, if this is a
 1091          * dual port NIC. Our algotithm is to divide up the memory
 1092          * evenly so that everyone gets a fair share.
 1093          */
 1094         if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) {
 1095                 u_int32_t               chunk, val;
 1096 
 1097                 chunk = sc->sk_ramsize / 2;
 1098                 val = sc->sk_rboff / sizeof(u_int64_t);
 1099                 sc_if->sk_rx_ramstart = val;
 1100                 val += (chunk / sizeof(u_int64_t));
 1101                 sc_if->sk_rx_ramend = val - 1;
 1102                 sc_if->sk_tx_ramstart = val;
 1103                 val += (chunk / sizeof(u_int64_t));
 1104                 sc_if->sk_tx_ramend = val - 1;
 1105         } else {
 1106                 u_int32_t               chunk, val;
 1107 
 1108                 chunk = sc->sk_ramsize / 4;
 1109                 val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) /
 1110                     sizeof(u_int64_t);
 1111                 sc_if->sk_rx_ramstart = val;
 1112                 val += (chunk / sizeof(u_int64_t));
 1113                 sc_if->sk_rx_ramend = val - 1;
 1114                 sc_if->sk_tx_ramstart = val;
 1115                 val += (chunk / sizeof(u_int64_t));
 1116                 sc_if->sk_tx_ramend = val - 1;
 1117         }
 1118 
 1119         DPRINTFN(2, ("sk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
 1120                      "           tx_ramstart=%#x tx_ramend=%#x\n",
 1121                      sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
 1122                      sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
 1123 
 1124         /* Read and save PHY type and set PHY address */
 1125         sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF;
 1126         switch (sc_if->sk_phytype) {
 1127         case SK_PHYTYPE_XMAC:
 1128                 sc_if->sk_phyaddr = SK_PHYADDR_XMAC;
 1129                 break;
 1130         case SK_PHYTYPE_BCOM:
 1131                 sc_if->sk_phyaddr = SK_PHYADDR_BCOM;
 1132                 break;
 1133         case SK_PHYTYPE_MARV_COPPER:
 1134                 sc_if->sk_phyaddr = SK_PHYADDR_MARV;
 1135                 break;
 1136         default:
 1137                 aprint_error("%s: unsupported PHY type: %d\n",
 1138                     sc->sk_dev.dv_xname, sc_if->sk_phytype);
 1139                 return;
 1140         }
 1141 
 1142         /* Allocate the descriptor queues. */
 1143         if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct sk_ring_data),
 1144             PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
 1145                 aprint_error("%s: can't alloc rx buffers\n",
 1146                     sc->sk_dev.dv_xname);
 1147                 goto fail;
 1148         }
 1149         if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
 1150             sizeof(struct sk_ring_data), &kva, BUS_DMA_NOWAIT)) {
 1151                 aprint_error("%s: can't map dma buffers (%lu bytes)\n",
 1152                        sc_if->sk_dev.dv_xname,
 1153                        (u_long) sizeof(struct sk_ring_data));
 1154                 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
 1155                 goto fail;
 1156         }
 1157         if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct sk_ring_data), 1,
 1158             sizeof(struct sk_ring_data), 0, BUS_DMA_NOWAIT,
 1159             &sc_if->sk_ring_map)) {
 1160                 aprint_error("%s: can't create dma map\n",
 1161                     sc_if->sk_dev.dv_xname);
 1162                 bus_dmamem_unmap(sc->sc_dmatag, kva,
 1163                     sizeof(struct sk_ring_data));
 1164                 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
 1165                 goto fail;
 1166         }
 1167         if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
 1168             sizeof(struct sk_ring_data), NULL, BUS_DMA_NOWAIT)) {
 1169                 aprint_error("%s: can't load dma map\n",
 1170                     sc_if->sk_dev.dv_xname);
 1171                 bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
 1172                 bus_dmamem_unmap(sc->sc_dmatag, kva,
 1173                     sizeof(struct sk_ring_data));
 1174                 bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
 1175                 goto fail;
 1176         }
 1177 
 1178         for (i = 0; i < SK_RX_RING_CNT; i++)
 1179                 sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
 1180 
 1181         SIMPLEQ_INIT(&sc_if->sk_txmap_head);
 1182         for (i = 0; i < SK_TX_RING_CNT; i++) {
 1183                 sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
 1184 
 1185                 if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, SK_NTXSEG,
 1186                     MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap)) {
 1187                         aprint_error("%s: Can't create TX dmamap\n",
 1188                                 sc_if->sk_dev.dv_xname);
 1189                         bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
 1190                         bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
 1191                         bus_dmamem_unmap(sc->sc_dmatag, kva,
 1192                             sizeof(struct sk_ring_data));
 1193                         bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
 1194                         goto fail;
 1195                 }
 1196 
 1197                 entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
 1198                 if (!entry) {
 1199                         aprint_error("%s: Can't alloc txmap entry\n",
 1200                                 sc_if->sk_dev.dv_xname);
 1201                         bus_dmamap_destroy(sc->sc_dmatag, dmamap);
 1202                         bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_ring_map);
 1203                         bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
 1204                         bus_dmamem_unmap(sc->sc_dmatag, kva,
 1205                             sizeof(struct sk_ring_data));
 1206                         bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
 1207                         goto fail;
 1208                 }
 1209                 entry->dmamap = dmamap;
 1210                 SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
 1211         }
 1212 
 1213         sc_if->sk_rdata = (struct sk_ring_data *)kva;
 1214         bzero(sc_if->sk_rdata, sizeof(struct sk_ring_data));
 1215 
 1216         /* XXX TLS It's not clear what's wrong with the Jumbo MTU
 1217            XXX TLS support in this driver, so we don't enable it. */
 1218 
 1219         sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU;
 1220 
 1221         ifp = &sc_if->sk_ethercom.ec_if;
 1222         ifp->if_softc = sc_if;
 1223         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 1224         ifp->if_ioctl = sk_ioctl;
 1225         ifp->if_start = sk_start;
 1226         ifp->if_stop = sk_stop;
 1227         ifp->if_init = sk_init;
 1228         ifp->if_watchdog = sk_watchdog;
 1229         ifp->if_capabilities = 0;
 1230         IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1);
 1231         IFQ_SET_READY(&ifp->if_snd);
 1232         strcpy(ifp->if_xname, sc_if->sk_dev.dv_xname);
 1233 
 1234         /*
 1235          * Do miibus setup.
 1236          */
 1237         switch (sc->sk_type) {
 1238         case SK_GENESIS:
 1239                 sk_init_xmac(sc_if);
 1240                 break;
 1241         case SK_YUKON:
 1242         case SK_YUKON_LITE:
 1243         case SK_YUKON_LP:
 1244                 sk_init_yukon(sc_if);
 1245                 break;
 1246         default:
 1247                 panic("%s: unknown device type %d", sc->sk_dev.dv_xname,
 1248                       sc->sk_type);
 1249         }
 1250 
 1251         DPRINTFN(2, ("sk_attach: 1\n"));
 1252 
 1253         sc_if->sk_mii.mii_ifp = ifp;
 1254         switch (sc->sk_type) {
 1255         case SK_GENESIS:
 1256                 sc_if->sk_mii.mii_readreg = sk_xmac_miibus_readreg;
 1257                 sc_if->sk_mii.mii_writereg = sk_xmac_miibus_writereg;
 1258                 sc_if->sk_mii.mii_statchg = sk_xmac_miibus_statchg;
 1259                 break;
 1260         case SK_YUKON:
 1261         case SK_YUKON_LITE:
 1262         case SK_YUKON_LP:
 1263                 sc_if->sk_mii.mii_readreg = sk_marv_miibus_readreg;
 1264                 sc_if->sk_mii.mii_writereg = sk_marv_miibus_writereg;
 1265                 sc_if->sk_mii.mii_statchg = sk_marv_miibus_statchg;
 1266                 break;
 1267         }
 1268 
 1269         ifmedia_init(&sc_if->sk_mii.mii_media, 0,
 1270             sk_ifmedia_upd, sk_ifmedia_sts);
 1271         mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
 1272             MII_OFFSET_ANY, 0);
 1273         if (LIST_FIRST(&sc_if->sk_mii.mii_phys) == NULL) {
 1274                 printf("%s: no PHY found!\n", sc_if->sk_dev.dv_xname);
 1275                 ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
 1276                             0, NULL);
 1277                 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
 1278         }
 1279         else
 1280                 ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
 1281 
 1282         callout_init(&sc_if->sk_tick_ch);
 1283         callout_reset(&sc_if->sk_tick_ch,hz,sk_tick,sc_if);
 1284 
 1285         DPRINTFN(2, ("sk_attach: 1\n"));
 1286 
 1287         /*
 1288          * Call MI attach routines.
 1289          */
 1290         if_attach(ifp);
 1291 
 1292         ether_ifattach(ifp, sc_if->sk_enaddr);
 1293 
 1294 #if NRND > 0
 1295         rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
 1296             RND_TYPE_NET, 0);
 1297 #endif
 1298 
 1299         DPRINTFN(2, ("sk_attach: end\n"));
 1300 
 1301         return;
 1302 
 1303 fail:
 1304         sc->sk_if[sa->skc_port] = NULL;
 1305 }
 1306 
 1307 int
 1308 skcprint(void *aux, const char *pnp)
 1309 {
 1310         struct skc_attach_args *sa = aux;
 1311 
 1312         if (pnp)
 1313                 aprint_normal("sk port %c at %s",
 1314                     (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
 1315         else
 1316                 aprint_normal(" port %c",
 1317                     (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
 1318         return (UNCONF);
 1319 }
 1320 
 1321 /*
 1322  * Attach the interface. Allocate softc structures, do ifmedia
 1323  * setup and ethernet/BPF attach.
 1324  */
 1325 void
 1326 skc_attach(struct device *parent, struct device *self, void *aux)
 1327 {
 1328         struct sk_softc *sc = (struct sk_softc *)self;
 1329         struct pci_attach_args *pa = aux;
 1330         struct skc_attach_args skca;
 1331         pci_chipset_tag_t pc = pa->pa_pc;
 1332         pcireg_t memtype;
 1333         pci_intr_handle_t ih;
 1334         const char *intrstr = NULL;
 1335         bus_addr_t iobase;
 1336         bus_size_t iosize;
 1337         int s;
 1338         u_int32_t command;
 1339         char *revstr;
 1340 
 1341         DPRINTFN(2, ("begin skc_attach\n"));
 1342 
 1343         s = splnet();
 1344 
 1345         /*
 1346          * Handle power management nonsense.
 1347          */
 1348         command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
 1349 
 1350         if (command == 0x01) {
 1351                 command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
 1352                 if (command & SK_PSTATE_MASK) {
 1353                         u_int32_t               iobase, membase, irq;
 1354 
 1355                         /* Save important PCI config data. */
 1356                         iobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
 1357                         membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
 1358                         irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
 1359 
 1360                         /* Reset the power state. */
 1361                         aprint_normal("%s chip is in D%d power mode "
 1362                             "-- setting to D0\n", sc->sk_dev.dv_xname,
 1363                             command & SK_PSTATE_MASK);
 1364                         command &= 0xFFFFFFFC;
 1365                         pci_conf_write(pc, pa->pa_tag,
 1366                             SK_PCI_PWRMGMTCTRL, command);
 1367 
 1368                         /* Restore PCI config data. */
 1369                         pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, iobase);
 1370                         pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
 1371                         pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
 1372                 }
 1373         }
 1374 
 1375         /*
 1376          * Map control/status registers.
 1377          */
 1378         command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
 1379         command |= PCI_COMMAND_IO_ENABLE |
 1380             PCI_COMMAND_MEM_ENABLE |
 1381             PCI_COMMAND_MASTER_ENABLE;
 1382         pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
 1383         command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
 1384 
 1385 #ifdef SK_USEIOSPACE
 1386         if (!(command & PCI_COMMAND_IO_ENABLE)) {
 1387                 aprint_error(": failed to enable I/O ports!\n");
 1388                 goto fail;
 1389         }
 1390         /*
 1391          * Map control/status registers.
 1392          */
 1393         if (pci_mapreg_map(pa, SK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
 1394                            &iobase, &iosize)) {
 1395                 aprint_error(": can't find i/o space\n");
 1396                 goto fail;
 1397         }
 1398 #else
 1399         if (!(command & PCI_COMMAND_MEM_ENABLE)) {
 1400                 aprint_error(": failed to enable memory mapping!\n");
 1401                 goto fail;
 1402         }
 1403         memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
 1404         switch (memtype) {
 1405         case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
 1406         case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
 1407                 if (pci_mapreg_map(pa, SK_PCI_LOMEM,
 1408                                    memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
 1409                                    &iobase, &iosize) == 0)
 1410                         break;
 1411         default:
 1412                 aprint_error("%s: can't find mem space\n",
 1413                        sc->sk_dev.dv_xname);
 1414                 return;
 1415         }
 1416 
 1417         DPRINTFN(2, ("skc_attach: iobase=%lx, iosize=%lx\n", iobase, iosize));
 1418 #endif
 1419         sc->sc_dmatag = pa->pa_dmat;
 1420 
 1421         sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
 1422         sc->sk_rev  = (sk_win_read_1(sc, SK_CONFIG) >> 4);
 1423 
 1424         /* bail out here if chip is not recognized */
 1425         if ( sc->sk_type != SK_GENESIS && ! SK_YUKON_FAMILY(sc->sk_type)) {
 1426                 aprint_error("%s: unknown chip type\n",sc->sk_dev.dv_xname);
 1427                 goto fail;
 1428         }
 1429         DPRINTFN(2, ("skc_attach: allocate interrupt\n"));
 1430 
 1431         /* Allocate interrupt */
 1432         if (pci_intr_map(pa, &ih)) {
 1433                 aprint_error(": couldn't map interrupt\n");
 1434                 goto fail;
 1435         }
 1436 
 1437         intrstr = pci_intr_string(pc, ih);
 1438         sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, sk_intr, sc);
 1439         if (sc->sk_intrhand == NULL) {
 1440                 aprint_error(": couldn't establish interrupt");
 1441                 if (intrstr != NULL)
 1442                         aprint_normal(" at %s", intrstr);
 1443                 goto fail;
 1444         }
 1445         aprint_normal(": %s\n", intrstr);
 1446 
 1447         /* Reset the adapter. */
 1448         sk_reset(sc);
 1449 
 1450         /* Read and save vital product data from EEPROM. */
 1451         sk_vpd_read(sc);
 1452 
 1453         if (sc->sk_type == SK_GENESIS) {
 1454                 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
 1455                 /* Read and save RAM size and RAMbuffer offset */
 1456                 switch(val) {
 1457                 case SK_RAMSIZE_512K_64:
 1458                         sc->sk_ramsize = 0x80000;
 1459                         sc->sk_rboff = SK_RBOFF_0;
 1460                         break;
 1461                 case SK_RAMSIZE_1024K_64:
 1462                         sc->sk_ramsize = 0x100000;
 1463                         sc->sk_rboff = SK_RBOFF_80000;
 1464                         break;
 1465                 case SK_RAMSIZE_1024K_128:
 1466                         sc->sk_ramsize = 0x100000;
 1467                         sc->sk_rboff = SK_RBOFF_0;
 1468                         break;
 1469                 case SK_RAMSIZE_2048K_128:
 1470                         sc->sk_ramsize = 0x200000;
 1471                         sc->sk_rboff = SK_RBOFF_0;
 1472                         break;
 1473                 default:
 1474                         aprint_error("%s: unknown ram size: %d\n",
 1475                                sc->sk_dev.dv_xname, val);
 1476                         goto fail;
 1477                         break;
 1478                 }
 1479 
 1480                 DPRINTFN(2, ("skc_attach: ramsize=%d(%dk), rboff=%d\n",
 1481                              sc->sk_ramsize, sc->sk_ramsize / 1024,
 1482                              sc->sk_rboff));
 1483         } else {
 1484                 u_int8_t val = sk_win_read_1(sc, SK_EPROM0);
 1485                 sc->sk_ramsize =  ( val == 0 ) ?  0x20000 : (( val * 4 )*1024);
 1486                 sc->sk_rboff = SK_RBOFF_0;
 1487 
 1488                 DPRINTFN(2, ("skc_attach: ramsize=%dk (%d), rboff=%d\n",
 1489                              sc->sk_ramsize / 1024, sc->sk_ramsize,
 1490                              sc->sk_rboff));
 1491         }
 1492 
 1493         /* Read and save physical media type */
 1494         switch(sk_win_read_1(sc, SK_PMDTYPE)) {
 1495         case SK_PMD_1000BASESX:
 1496                 sc->sk_pmd = IFM_1000_SX;
 1497                 break;
 1498         case SK_PMD_1000BASELX:
 1499                 sc->sk_pmd = IFM_1000_LX;
 1500                 break;
 1501         case SK_PMD_1000BASECX:
 1502                 sc->sk_pmd = IFM_1000_CX;
 1503                 break;
 1504         case SK_PMD_1000BASETX:
 1505                 sc->sk_pmd = IFM_1000_T;
 1506                 break;
 1507         default:
 1508                 aprint_error("%s: unknown media type: 0x%x\n",
 1509                     sc->sk_dev.dv_xname, sk_win_read_1(sc, SK_PMDTYPE));
 1510                 goto fail;
 1511         }
 1512 
 1513         /* determine whether to name it with vpd or just make it up */
 1514         /* Marvell Yukon VPD's can freqently be bogus */
 1515 
 1516         switch (pa->pa_id) {
 1517         case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
 1518                          PCI_PRODUCT_SCHNEIDERKOCH_SKNET_GE):
 1519         case PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2:
 1520         case PCI_PRODUCT_3COM_3C940:
 1521         case PCI_PRODUCT_DLINK_DGE530T:
 1522         case PCI_PRODUCT_LINKSYS_EG1032:
 1523         case PCI_PRODUCT_LINKSYS_EG1064:
 1524         case PCI_ID_CODE(PCI_VENDOR_SCHNEIDERKOCH,
 1525                          PCI_PRODUCT_SCHNEIDERKOCH_SK9821v2):
 1526         case PCI_ID_CODE(PCI_VENDOR_3COM,PCI_PRODUCT_3COM_3C940):
 1527         case PCI_ID_CODE(PCI_VENDOR_DLINK,PCI_PRODUCT_DLINK_DGE530T):
 1528         case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1032):
 1529         case PCI_ID_CODE(PCI_VENDOR_LINKSYS,PCI_PRODUCT_LINKSYS_EG1064):
 1530                 sc->sk_name = sc->sk_vpd_prodname;
 1531                 break;
 1532         case PCI_ID_CODE(PCI_VENDOR_GALILEO,PCI_PRODUCT_GALILEO_SKNET):
 1533         /* whoops yukon vpd prodname bears no resemblance to reality */
 1534                 switch (sc->sk_type) {
 1535                 case SK_GENESIS:
 1536                         sc->sk_name = sc->sk_vpd_prodname;
 1537                         break;
 1538                 case SK_YUKON:
 1539                         sc->sk_name = "Marvell Yukon Gigabit Ethernet";
 1540                         break;
 1541                 case SK_YUKON_LITE:
 1542                         sc->sk_name = "Marvell Yukon Lite Gigabit Ethernet";
 1543                         break;
 1544                 case SK_YUKON_LP:
 1545                         sc->sk_name = "Marvell Yukon LP Gigabit Ethernet";
 1546                         break;
 1547                 default:
 1548                         sc->sk_name = "Marvell Yukon (Unknown) Gigabit Ethernet";
 1549                 }
 1550 
 1551         /* Yukon Lite Rev A0 needs special test, from sk98lin driver */
 1552 
 1553                 if ( sc->sk_type == SK_YUKON ) {
 1554                         uint32_t flashaddr;
 1555                         uint8_t testbyte;
 1556                         
 1557                         flashaddr = sk_win_read_4(sc,SK_EP_ADDR);
 1558                         
 1559                         /* test Flash-Address Register */
 1560                         sk_win_write_1(sc,SK_EP_ADDR+3, 0xff);
 1561                         testbyte = sk_win_read_1(sc, SK_EP_ADDR+3);
 1562                         
 1563                         if (testbyte != 0) {
 1564                                 /* this is yukon lite Rev. A0 */
 1565                                 sc->sk_type = SK_YUKON_LITE;
 1566                                 sc->sk_rev = SK_YUKON_LITE_REV_A0;
 1567                                 /* restore Flash-Address Register */
 1568                                 sk_win_write_4(sc,SK_EP_ADDR,flashaddr);
 1569                         }
 1570                 }
 1571                 break;
 1572         default:
 1573                 sc->sk_name = "Unkown Marvell";
 1574         }
 1575 
 1576                 
 1577         if ( sc->sk_type == SK_YUKON_LITE ) {
 1578                 switch (sc->sk_rev) {
 1579                 case SK_YUKON_LITE_REV_A0:
 1580                         revstr = "A0";
 1581                         break;
 1582                 case SK_YUKON_LITE_REV_A1:
 1583                         revstr = "A1";
 1584                         break;
 1585                 case SK_YUKON_LITE_REV_A3:
 1586                         revstr = "A3";
 1587                         break;
 1588                 default:
 1589                         revstr = "";
 1590                 }
 1591         } else {
 1592                 revstr = "";
 1593         }
 1594 
 1595         /* Announce the product name. */
 1596         aprint_normal("%s: %s rev. %s(0x%x)\n", sc->sk_dev.dv_xname,
 1597                               sc->sk_name, revstr, sc->sk_rev);
 1598 
 1599         skca.skc_port = SK_PORT_A;
 1600         (void)config_found(&sc->sk_dev, &skca, skcprint);
 1601 
 1602         if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) {
 1603                 skca.skc_port = SK_PORT_B;
 1604                 (void)config_found(&sc->sk_dev, &skca, skcprint);
 1605         }
 1606 
 1607         /* Turn on the 'driver is loaded' LED. */
 1608         CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
 1609 
 1610 fail:
 1611         splx(s);
 1612 }
 1613 
 1614 int
 1615 sk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
 1616 {
 1617         struct sk_softc         *sc = sc_if->sk_softc;
 1618         struct sk_tx_desc       *f = NULL;
 1619         u_int32_t               frag, cur, cnt = 0;
 1620         int                     i;
 1621         struct sk_txmap_entry   *entry;
 1622         bus_dmamap_t            txmap;
 1623 
 1624         DPRINTFN(3, ("sk_encap\n"));
 1625 
 1626         entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
 1627         if (entry == NULL) {
 1628                 DPRINTFN(3, ("sk_encap: no txmap available\n"));
 1629                 return ENOBUFS;
 1630         }
 1631         txmap = entry->dmamap;
 1632 
 1633         cur = frag = *txidx;
 1634 
 1635 #ifdef SK_DEBUG
 1636         if (skdebug >= 3)
 1637                 sk_dump_mbuf(m_head);
 1638 #endif
 1639 
 1640         /*
 1641          * Start packing the mbufs in this chain into
 1642          * the fragment pointers. Stop when we run out
 1643          * of fragments or hit the end of the mbuf chain.
 1644          */
 1645         if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
 1646             BUS_DMA_NOWAIT)) {
 1647                 DPRINTFN(1, ("sk_encap: dmamap failed\n"));
 1648                 return(ENOBUFS);
 1649         }
 1650 
 1651         DPRINTFN(3, ("sk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
 1652 
 1653         /* Sync the DMA map. */
 1654         bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
 1655             BUS_DMASYNC_PREWRITE);
 1656 
 1657         for (i = 0; i < txmap->dm_nsegs; i++) {
 1658                 if ((SK_TX_RING_CNT - (sc_if->sk_cdata.sk_tx_cnt + cnt)) < 2) {
 1659                         DPRINTFN(1, ("sk_encap: too few descriptors free\n"));
 1660                         return(ENOBUFS);
 1661                 }
 1662                 f = &sc_if->sk_rdata->sk_tx_ring[frag];
 1663                 f->sk_data_lo = txmap->dm_segs[i].ds_addr;
 1664                 f->sk_ctl = txmap->dm_segs[i].ds_len | SK_OPCODE_DEFAULT;
 1665                 if (cnt == 0)
 1666                         f->sk_ctl |= SK_TXCTL_FIRSTFRAG;
 1667                 else
 1668                         f->sk_ctl |= SK_TXCTL_OWN;
 1669 
 1670                 cur = frag;
 1671                 SK_INC(frag, SK_TX_RING_CNT);
 1672                 cnt++;
 1673         }
 1674 
 1675         sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
 1676         SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
 1677 
 1678         sc_if->sk_cdata.sk_tx_map[cur] = entry;
 1679         sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |=
 1680                 SK_TXCTL_LASTFRAG|SK_TXCTL_EOF_INTR;
 1681 
 1682         /* Sync descriptors before handing to chip */
 1683         SK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
 1684             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1685 
 1686         sc_if->sk_rdata->sk_tx_ring[*txidx].sk_ctl |= SK_TXCTL_OWN;
 1687 
 1688         /* Sync first descriptor to hand it off */
 1689         SK_CDTXSYNC(sc_if, *txidx, 1, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1690 
 1691         sc_if->sk_cdata.sk_tx_cnt += cnt;
 1692 
 1693 #ifdef SK_DEBUG
 1694         if (skdebug >= 3) {
 1695                 struct sk_tx_desc *desc;
 1696                 u_int32_t idx;
 1697                 for (idx = *txidx; idx != frag; SK_INC(idx, SK_TX_RING_CNT)) {
 1698                         desc = &sc_if->sk_rdata->sk_tx_ring[idx];
 1699                         sk_dump_txdesc(desc, idx);
 1700                 }
 1701         }
 1702 #endif
 1703 
 1704         *txidx = frag;
 1705 
 1706         DPRINTFN(3, ("sk_encap: completed successfully\n"));
 1707 
 1708         return(0);
 1709 }
 1710 
 1711 void
 1712 sk_start(struct ifnet *ifp)
 1713 {
 1714         struct sk_if_softc      *sc_if = ifp->if_softc;
 1715         struct sk_softc         *sc = sc_if->sk_softc;
 1716         struct mbuf             *m_head = NULL;
 1717         u_int32_t               idx = sc_if->sk_cdata.sk_tx_prod;
 1718         int                     pkts = 0;
 1719 
 1720         DPRINTFN(3, ("sk_start (idx %d, tx_chain[idx] %p)\n", idx,
 1721                 sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf));
 1722 
 1723         while(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
 1724 
 1725                 IFQ_POLL(&ifp->if_snd, m_head);
 1726                 if (m_head == NULL)
 1727                         break;
 1728 
 1729                 /*
 1730                  * Pack the data into the transmit ring. If we
 1731                  * don't have room, set the OACTIVE flag and wait
 1732                  * for the NIC to drain the ring.
 1733                  */
 1734                 if (sk_encap(sc_if, m_head, &idx)) {
 1735                         ifp->if_flags |= IFF_OACTIVE;
 1736                         break;
 1737                 }
 1738 
 1739                 /* now we are committed to transmit the packet */
 1740                 IFQ_DEQUEUE(&ifp->if_snd, m_head);
 1741                 pkts++;
 1742 
 1743                 /*
 1744                  * If there's a BPF listener, bounce a copy of this frame
 1745                  * to him.
 1746                  */
 1747 #if NBPFILTER > 0
 1748                 if (ifp->if_bpf)
 1749                         bpf_mtap(ifp->if_bpf, m_head);
 1750 #endif
 1751         }
 1752         if (pkts == 0)
 1753                 return;
 1754 
 1755         /* Transmit */
 1756         sc_if->sk_cdata.sk_tx_prod = idx;
 1757         CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
 1758 
 1759         /* Set a timeout in case the chip goes out to lunch. */
 1760         ifp->if_timer = 5;
 1761 }
 1762 
 1763 
 1764 void
 1765 sk_watchdog(struct ifnet *ifp)
 1766 {
 1767         struct sk_if_softc *sc_if = ifp->if_softc;
 1768 
 1769         printf("%s: watchdog timeout\n", sc_if->sk_dev.dv_xname);
 1770         (void) sk_init(ifp);
 1771 }
 1772 
 1773 void
 1774 sk_shutdown(void * v)
 1775 {
 1776         struct sk_if_softc      *sc_if = (struct sk_if_softc *)v;
 1777         struct sk_softc         *sc = sc_if->sk_softc;
 1778         struct ifnet            *ifp = &sc_if->sk_ethercom.ec_if;
 1779 
 1780         DPRINTFN(2, ("sk_shutdown\n"));
 1781         sk_stop(ifp,1);
 1782 
 1783         /* Turn off the 'driver is loaded' LED. */
 1784         CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
 1785 
 1786         /*
 1787          * Reset the GEnesis controller. Doing this should also
 1788          * assert the resets on the attached XMAC(s).
 1789          */
 1790         sk_reset(sc);
 1791 }
 1792 
 1793 void
 1794 sk_rxeof(struct sk_if_softc *sc_if)
 1795 {
 1796         struct ifnet            *ifp = &sc_if->sk_ethercom.ec_if;
 1797         struct mbuf             *m;
 1798         struct sk_chain         *cur_rx;
 1799         struct sk_rx_desc       *cur_desc;
 1800         int                     i, cur, total_len = 0;
 1801         u_int32_t               rxstat;
 1802         bus_dmamap_t            dmamap;
 1803 
 1804         i = sc_if->sk_cdata.sk_rx_prod;
 1805 
 1806         DPRINTFN(3, ("sk_rxeof %d\n", i));
 1807 
 1808         for (;;) {
 1809                 cur = i;
 1810 
 1811                 /* Sync the descriptor */
 1812                 SK_CDRXSYNC(sc_if, cur,
 1813                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1814 
 1815                 if (sc_if->sk_rdata->sk_rx_ring[cur].sk_ctl & SK_RXCTL_OWN) {
 1816                         /* Invalidate the descriptor -- it's not ready yet */
 1817                         SK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_PREREAD);
 1818                         sc_if->sk_cdata.sk_rx_prod = i;
 1819                         break;
 1820                 }
 1821 
 1822                 cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
 1823                 cur_desc = &sc_if->sk_rdata->sk_rx_ring[cur];
 1824                 dmamap = sc_if->sk_cdata.sk_rx_map[cur];
 1825 
 1826                 bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
 1827                     dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
 1828 
 1829                 rxstat = cur_desc->sk_xmac_rxstat;
 1830                 m = cur_rx->sk_mbuf;
 1831                 cur_rx->sk_mbuf = NULL;
 1832                 total_len = SK_RXBYTES(cur_desc->sk_ctl);
 1833 
 1834                 sc_if->sk_cdata.sk_rx_map[cur] = 0;
 1835 
 1836                 SK_INC(i, SK_RX_RING_CNT);
 1837 
 1838                 if (rxstat & XM_RXSTAT_ERRFRAME) {
 1839                         ifp->if_ierrors++;
 1840                         sk_newbuf(sc_if, cur, m, dmamap);
 1841                         continue;
 1842                 }
 1843 
 1844                 /*
 1845                  * Try to allocate a new jumbo buffer. If that
 1846                  * fails, copy the packet to mbufs and put the
 1847                  * jumbo buffer back in the ring so it can be
 1848                  * re-used. If allocating mbufs fails, then we
 1849                  * have to drop the packet.
 1850                  */
 1851                 if (sk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
 1852                         struct mbuf             *m0;
 1853                         m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
 1854                             total_len + ETHER_ALIGN, 0, ifp, NULL);
 1855                         sk_newbuf(sc_if, cur, m, dmamap);
 1856                         if (m0 == NULL) {
 1857                                 printf("%s: no receive buffers "
 1858                                     "available -- packet dropped!\n",
 1859                                     sc_if->sk_dev.dv_xname);
 1860                                 ifp->if_ierrors++;
 1861                                 continue;
 1862                         }
 1863                         m_adj(m0, ETHER_ALIGN);
 1864                         m = m0;
 1865                 } else {
 1866                         m->m_pkthdr.rcvif = ifp;
 1867                         m->m_pkthdr.len = m->m_len = total_len;
 1868                 }
 1869 
 1870                 ifp->if_ipackets++;
 1871 
 1872 #if NBPFILTER > 0
 1873                 if (ifp->if_bpf)
 1874                         bpf_mtap(ifp->if_bpf, m);
 1875 #endif
 1876                 /* pass it on. */
 1877                 (*ifp->if_input)(ifp, m);
 1878         }
 1879 }
 1880 
 1881 void
 1882 sk_txeof(struct sk_if_softc *sc_if)
 1883 {
 1884         struct sk_softc         *sc = sc_if->sk_softc;
 1885         struct sk_tx_desc       *cur_tx = NULL;
 1886         struct ifnet            *ifp = &sc_if->sk_ethercom.ec_if;
 1887         u_int32_t               idx;
 1888         struct sk_txmap_entry   *entry;
 1889 
 1890         DPRINTFN(3, ("sk_txeof\n"));
 1891 
 1892         /*
 1893          * Go through our tx ring and free mbufs for those
 1894          * frames that have been sent.
 1895          */
 1896         idx = sc_if->sk_cdata.sk_tx_cons;
 1897         while(idx != sc_if->sk_cdata.sk_tx_prod) {
 1898                 SK_CDTXSYNC(sc_if, idx, 1,
 1899                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1900 
 1901                 cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
 1902 #ifdef SK_DEBUG
 1903                 if (skdebug >= 3)
 1904                         sk_dump_txdesc(cur_tx, idx);
 1905 #endif
 1906                 if (cur_tx->sk_ctl & SK_TXCTL_OWN) {
 1907                         SK_CDTXSYNC(sc_if, idx, 1, BUS_DMASYNC_PREREAD);
 1908                         break;
 1909                 }
 1910                 if (cur_tx->sk_ctl & SK_TXCTL_LASTFRAG)
 1911                         ifp->if_opackets++;
 1912                 if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
 1913                         entry = sc_if->sk_cdata.sk_tx_map[idx];
 1914 
 1915                         m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
 1916                         sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
 1917 
 1918                         bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
 1919                             entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
 1920 
 1921                         bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
 1922                         SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
 1923                                           link);
 1924                         sc_if->sk_cdata.sk_tx_map[idx] = NULL;
 1925                 }
 1926                 sc_if->sk_cdata.sk_tx_cnt--;
 1927                 SK_INC(idx, SK_TX_RING_CNT);
 1928         }
 1929         if (sc_if->sk_cdata.sk_tx_cnt == 0)
 1930                 ifp->if_timer = 0;
 1931         else /* nudge chip to keep tx ring moving */
 1932                 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START);
 1933 
 1934         sc_if->sk_cdata.sk_tx_cons = idx;
 1935 
 1936         if (cur_tx != NULL)
 1937                 ifp->if_flags &= ~IFF_OACTIVE;
 1938 }
 1939 
 1940 void
 1941 sk_tick(void *xsc_if)
 1942 {
 1943         struct sk_if_softc *sc_if = xsc_if;
 1944         struct mii_data *mii = &sc_if->sk_mii;
 1945         struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
 1946         int i;
 1947 
 1948         DPRINTFN(3, ("sk_tick\n"));
 1949 
 1950         if (!(ifp->if_flags & IFF_UP))
 1951                 return;
 1952 
 1953         if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
 1954                 sk_intr_bcom(sc_if);
 1955                 return;
 1956         }
 1957 
 1958         /*
 1959          * According to SysKonnect, the correct way to verify that
 1960          * the link has come back up is to poll bit 0 of the GPIO
 1961          * register three times. This pin has the signal from the
 1962          * link sync pin connected to it; if we read the same link
 1963          * state 3 times in a row, we know the link is up.
 1964          */
 1965         for (i = 0; i < 3; i++) {
 1966                 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET)
 1967                         break;
 1968         }
 1969 
 1970         if (i != 3) {
 1971                 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
 1972                 return;
 1973         }
 1974 
 1975         /* Turn the GP0 interrupt back on. */
 1976         SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
 1977         SK_XM_READ_2(sc_if, XM_ISR);
 1978         mii_tick(mii);
 1979         mii_pollstat(mii);
 1980         callout_stop(&sc_if->sk_tick_ch);
 1981 }
 1982 
 1983 void
 1984 sk_intr_bcom(struct sk_if_softc *sc_if)
 1985 {
 1986         struct mii_data *mii = &sc_if->sk_mii;
 1987         struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
 1988         int status;
 1989 
 1990 
 1991         DPRINTFN(3, ("sk_intr_bcom\n"));
 1992 
 1993         SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
 1994 
 1995         /*
 1996          * Read the PHY interrupt register to make sure
 1997          * we clear any pending interrupts.
 1998          */
 1999         status = sk_xmac_miibus_readreg((struct device *)sc_if,
 2000             SK_PHYADDR_BCOM, BRGPHY_MII_ISR);
 2001 
 2002         if (!(ifp->if_flags & IFF_RUNNING)) {
 2003                 sk_init_xmac(sc_if);
 2004                 return;
 2005         }
 2006 
 2007         if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) {
 2008                 int lstat;
 2009                 lstat = sk_xmac_miibus_readreg((struct device *)sc_if,
 2010                     SK_PHYADDR_BCOM, BRGPHY_MII_AUXSTS);
 2011 
 2012                 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) {
 2013                         mii_mediachg(mii);
 2014                         /* Turn off the link LED. */
 2015                         SK_IF_WRITE_1(sc_if, 0,
 2016                             SK_LINKLED1_CTL, SK_LINKLED_OFF);
 2017                         sc_if->sk_link = 0;
 2018                 } else if (status & BRGPHY_ISR_LNK_CHG) {
 2019                         sk_xmac_miibus_writereg((struct device *)sc_if,
 2020                             SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFF00);
 2021                         mii_tick(mii);
 2022                         sc_if->sk_link = 1;
 2023                         /* Turn on the link LED. */
 2024                         SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
 2025                             SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF|
 2026                             SK_LINKLED_BLINK_OFF);
 2027                         mii_pollstat(mii);
 2028                 } else {
 2029                         mii_tick(mii);
 2030                         callout_reset(&sc_if->sk_tick_ch, hz, sk_tick,sc_if);
 2031                 }
 2032         }
 2033 
 2034         SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
 2035 }
 2036 
 2037 void
 2038 sk_intr_xmac(struct sk_if_softc *sc_if)
 2039 {
 2040         u_int16_t status = SK_XM_READ_2(sc_if, XM_ISR);
 2041 
 2042         DPRINTFN(3, ("sk_intr_xmac\n"));
 2043 
 2044         if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) {
 2045                 if (status & XM_ISR_GP0_SET) {
 2046                         SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET);
 2047                         callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
 2048                 }
 2049 
 2050                 if (status & XM_ISR_AUTONEG_DONE) {
 2051                         callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if);
 2052                 }
 2053         }
 2054 
 2055         if (status & XM_IMR_TX_UNDERRUN)
 2056                 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO);
 2057 
 2058         if (status & XM_IMR_RX_OVERRUN)
 2059                 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO);
 2060 }
 2061 
 2062 void
 2063 sk_intr_yukon(sc_if)
 2064         struct sk_if_softc *sc_if;
 2065 {
 2066         int status;
 2067 
 2068         status = SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
 2069 
 2070         DPRINTFN(3, ("sk_intr_yukon status=%#x\n", status));
 2071 }
 2072 
 2073 int
 2074 sk_intr(void *xsc)
 2075 {
 2076         struct sk_softc         *sc = xsc;
 2077         struct sk_if_softc      *sc_if0 = sc->sk_if[SK_PORT_A];
 2078         struct sk_if_softc      *sc_if1 = sc->sk_if[SK_PORT_B];
 2079         struct ifnet            *ifp0 = NULL, *ifp1 = NULL;
 2080         u_int32_t               status;
 2081         int                     claimed = 0;
 2082 
 2083         if (sc_if0 != NULL)
 2084                 ifp0 = &sc_if0->sk_ethercom.ec_if;
 2085         if (sc_if1 != NULL)
 2086                 ifp1 = &sc_if1->sk_ethercom.ec_if;
 2087 
 2088         for (;;) {
 2089                 status = CSR_READ_4(sc, SK_ISSR);
 2090                 DPRINTFN(3, ("sk_intr: status=%#x\n", status));
 2091 
 2092                 if (!(status & sc->sk_intrmask))
 2093                         break;
 2094 
 2095                 claimed = 1;
 2096 
 2097                 /* Handle receive interrupts first. */
 2098                 if (status & SK_ISR_RX1_EOF) {
 2099                         sk_rxeof(sc_if0);
 2100                         CSR_WRITE_4(sc, SK_BMU_RX_CSR0,
 2101                             SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
 2102                 }
 2103                 if (status & SK_ISR_RX2_EOF) {
 2104                         sk_rxeof(sc_if1);
 2105                         CSR_WRITE_4(sc, SK_BMU_RX_CSR1,
 2106                             SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START);
 2107                 }
 2108 
 2109                 /* Then transmit interrupts. */
 2110                 if (status & SK_ISR_TX1_S_EOF) {
 2111                         sk_txeof(sc_if0);
 2112                         CSR_WRITE_4(sc, SK_BMU_TXS_CSR0,
 2113                             SK_TXBMU_CLR_IRQ_EOF);
 2114                 }
 2115                 if (status & SK_ISR_TX2_S_EOF) {
 2116                         sk_txeof(sc_if1);
 2117                         CSR_WRITE_4(sc, SK_BMU_TXS_CSR1,
 2118                             SK_TXBMU_CLR_IRQ_EOF);
 2119                 }
 2120 
 2121                 /* Then MAC interrupts. */
 2122                 if (status & SK_ISR_MAC1 && (ifp0->if_flags & IFF_RUNNING)) {
 2123                         if (sc->sk_type == SK_GENESIS)
 2124                                 sk_intr_xmac(sc_if0);
 2125                         else
 2126                                 sk_intr_yukon(sc_if0);
 2127                 }
 2128 
 2129                 if (status & SK_ISR_MAC2 && (ifp1->if_flags & IFF_RUNNING)) {
 2130                         if (sc->sk_type == SK_GENESIS)
 2131                                 sk_intr_xmac(sc_if1);
 2132                         else
 2133                                 sk_intr_yukon(sc_if1);
 2134 
 2135                 }
 2136 
 2137                 if (status & SK_ISR_EXTERNAL_REG) {
 2138                         if (ifp0 != NULL &&
 2139                             sc_if0->sk_phytype == SK_PHYTYPE_BCOM)
 2140                                 sk_intr_bcom(sc_if0);
 2141 
 2142                         if (ifp1 != NULL &&
 2143                             sc_if1->sk_phytype == SK_PHYTYPE_BCOM)
 2144                                 sk_intr_bcom(sc_if1);
 2145                 }
 2146         }
 2147 
 2148         CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
 2149 
 2150         if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
 2151                 sk_start(ifp0);
 2152         if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
 2153                 sk_start(ifp1);
 2154 
 2155         return (claimed);
 2156 }
 2157 
 2158 void
 2159 sk_init_xmac(struct sk_if_softc *sc_if)
 2160 {
 2161         struct sk_softc         *sc = sc_if->sk_softc;
 2162         struct ifnet            *ifp = &sc_if->sk_ethercom.ec_if;
 2163         static const struct sk_bcom_hack     bhack[] = {
 2164         { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 },
 2165         { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 },
 2166         { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
 2167         { 0, 0 } };
 2168 
 2169         DPRINTFN(1, ("sk_init_xmac\n"));
 2170 
 2171         /* Unreset the XMAC. */
 2172         SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET);
 2173         DELAY(1000);
 2174 
 2175         /* Reset the XMAC's internal state. */
 2176         SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
 2177 
 2178         /* Save the XMAC II revision */
 2179         sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID));
 2180 
 2181         /*
 2182          * Perform additional initialization for external PHYs,
 2183          * namely for the 1000baseTX cards that use the XMAC's
 2184          * GMII mode.
 2185          */
 2186         if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
 2187                 int                     i = 0;
 2188                 u_int32_t               val;
 2189 
 2190                 /* Take PHY out of reset. */
 2191                 val = sk_win_read_4(sc, SK_GPIO);
 2192                 if (sc_if->sk_port == SK_PORT_A)
 2193                         val |= SK_GPIO_DIR0|SK_GPIO_DAT0;
 2194                 else
 2195                         val |= SK_GPIO_DIR2|SK_GPIO_DAT2;
 2196                 sk_win_write_4(sc, SK_GPIO, val);
 2197 
 2198                 /* Enable GMII mode on the XMAC. */
 2199                 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE);
 2200 
 2201                 sk_xmac_miibus_writereg((struct device *)sc_if,
 2202                     SK_PHYADDR_BCOM, MII_BMCR, BMCR_RESET);
 2203                 DELAY(10000);
 2204                 sk_xmac_miibus_writereg((struct device *)sc_if,
 2205                     SK_PHYADDR_BCOM, BRGPHY_MII_IMR, 0xFFF0);
 2206 
 2207                 /*
 2208                  * Early versions of the BCM5400 apparently have
 2209                  * a bug that requires them to have their reserved
 2210                  * registers initialized to some magic values. I don't
 2211                  * know what the numbers do, I'm just the messenger.
 2212                  */
 2213                 if (sk_xmac_miibus_readreg((struct device *)sc_if,
 2214                     SK_PHYADDR_BCOM, 0x03) == 0x6041) {
 2215                         while(bhack[i].reg) {
 2216                                 sk_xmac_miibus_writereg((struct device *)sc_if,
 2217                                     SK_PHYADDR_BCOM, bhack[i].reg,
 2218                                     bhack[i].val);
 2219                                 i++;
 2220                         }
 2221                 }
 2222         }
 2223 
 2224         /* Set station address */
 2225         SK_XM_WRITE_2(sc_if, XM_PAR0,
 2226                       *(u_int16_t *)(&sc_if->sk_enaddr[0]));
 2227         SK_XM_WRITE_2(sc_if, XM_PAR1,
 2228                       *(u_int16_t *)(&sc_if->sk_enaddr[2]));
 2229         SK_XM_WRITE_2(sc_if, XM_PAR2,
 2230                       *(u_int16_t *)(&sc_if->sk_enaddr[4]));
 2231         SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION);
 2232 
 2233         if (ifp->if_flags & IFF_PROMISC) {
 2234                 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
 2235         } else {
 2236                 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC);
 2237         }
 2238 
 2239         if (ifp->if_flags & IFF_BROADCAST) {
 2240                 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
 2241         } else {
 2242                 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD);
 2243         }
 2244 
 2245         /* We don't need the FCS appended to the packet. */
 2246         SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS);
 2247 
 2248         /* We want short frames padded to 60 bytes. */
 2249         SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD);
 2250 
 2251         /*
 2252          * Enable the reception of all error frames. This is is
 2253          * a necessary evil due to the design of the XMAC. The
 2254          * XMAC's receive FIFO is only 8K in size, however jumbo
 2255          * frames can be up to 9000 bytes in length. When bad
 2256          * frame filtering is enabled, the XMAC's RX FIFO operates
 2257          * in 'store and forward' mode. For this to work, the
 2258          * entire frame has to fit into the FIFO, but that means
 2259          * that jumbo frames larger than 8192 bytes will be
 2260          * truncated. Disabling all bad frame filtering causes
 2261          * the RX FIFO to operate in streaming mode, in which
 2262          * case the XMAC will start transfering frames out of the
 2263          * RX FIFO as soon as the FIFO threshold is reached.
 2264          */
 2265         SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES|
 2266             XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS|
 2267             XM_MODE_RX_INRANGELEN);
 2268 
 2269         if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
 2270                 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
 2271         else
 2272                 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK);
 2273 
 2274         /*
 2275          * Bump up the transmit threshold. This helps hold off transmit
 2276          * underruns when we're blasting traffic from both ports at once.
 2277          */
 2278         SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH);
 2279 
 2280         /* Set multicast filter */
 2281         sk_setmulti(sc_if);
 2282 
 2283         /* Clear and enable interrupts */
 2284         SK_XM_READ_2(sc_if, XM_ISR);
 2285         if (sc_if->sk_phytype == SK_PHYTYPE_XMAC)
 2286                 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS);
 2287         else
 2288                 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
 2289 
 2290         /* Configure MAC arbiter */
 2291         switch(sc_if->sk_xmac_rev) {
 2292         case XM_XMAC_REV_B2:
 2293                 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2);
 2294                 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2);
 2295                 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2);
 2296                 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2);
 2297                 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2);
 2298                 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2);
 2299                 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2);
 2300                 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2);
 2301                 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
 2302                 break;
 2303         case XM_XMAC_REV_C1:
 2304                 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1);
 2305                 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1);
 2306                 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1);
 2307                 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1);
 2308                 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1);
 2309                 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1);
 2310                 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1);
 2311                 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1);
 2312                 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2);
 2313                 break;
 2314         default:
 2315                 break;
 2316         }
 2317         sk_win_write_2(sc, SK_MACARB_CTL,
 2318             SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF);
 2319 
 2320         sc_if->sk_link = 1;
 2321 }
 2322 
 2323 void sk_init_yukon(sc_if)
 2324         struct sk_if_softc      *sc_if;
 2325 {
 2326         u_int32_t               /*mac, */phy;
 2327         u_int16_t               reg;
 2328         int                     i;
 2329 
 2330         DPRINTFN(1, ("sk_init_yukon: start: sk_csr=%#x\n",
 2331                      CSR_READ_4(sc_if->sk_softc, SK_CSR)));
 2332 
 2333         /* GMAC and GPHY Reset */
 2334         SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
 2335 
 2336         DPRINTFN(6, ("sk_init_yukon: 1\n"));
 2337 
 2338         SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
 2339         DELAY(1000);
 2340         SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_CLEAR);
 2341         SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
 2342         DELAY(1000);
 2343 
 2344 
 2345         DPRINTFN(6, ("sk_init_yukon: 2\n"));
 2346 
 2347         phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP |
 2348                 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE;
 2349 
 2350         switch(sc_if->sk_softc->sk_pmd) {
 2351         case IFM_1000_SX:
 2352         case IFM_1000_LX:
 2353                 phy |= SK_GPHY_FIBER;
 2354                 break;
 2355 
 2356         case IFM_1000_CX:
 2357         case IFM_1000_T:
 2358                 phy |= SK_GPHY_COPPER;
 2359                 break;
 2360         }
 2361 
 2362         DPRINTFN(3, ("sk_init_yukon: phy=%#x\n", phy));
 2363 
 2364         SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET);
 2365         DELAY(1000);
 2366         SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR);
 2367         SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
 2368                       SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
 2369 
 2370         DPRINTFN(3, ("sk_init_yukon: gmac_ctrl=%#x\n",
 2371                      SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
 2372 
 2373         DPRINTFN(6, ("sk_init_yukon: 3\n"));
 2374 
 2375         /* unused read of the interrupt source register */
 2376         DPRINTFN(6, ("sk_init_yukon: 4\n"));
 2377         SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
 2378 
 2379         DPRINTFN(6, ("sk_init_yukon: 4a\n"));
 2380         reg = SK_YU_READ_2(sc_if, YUKON_PAR);
 2381         DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
 2382 
 2383         /* MIB Counter Clear Mode set */
 2384         reg |= YU_PAR_MIB_CLR;
 2385         DPRINTFN(6, ("sk_init_yukon: YUKON_PAR=%#x\n", reg));
 2386         DPRINTFN(6, ("sk_init_yukon: 4b\n"));
 2387         SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
 2388         
 2389         /* MIB Counter Clear Mode clear */
 2390         DPRINTFN(6, ("sk_init_yukon: 5\n"));
 2391         reg &= ~YU_PAR_MIB_CLR;
 2392         SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
 2393 
 2394         /* receive control reg */
 2395         DPRINTFN(6, ("sk_init_yukon: 7\n"));
 2396         SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_UFLEN | YU_RCR_MUFLEN |
 2397                       YU_RCR_CRCR);
 2398 
 2399         /* transmit parameter register */
 2400         DPRINTFN(6, ("sk_init_yukon: 8\n"));
 2401         SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
 2402                       YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) );
 2403 
 2404         /* serial mode register */
 2405         DPRINTFN(6, ("sk_init_yukon: 9\n"));
 2406         SK_YU_WRITE_2(sc_if, YUKON_SMR, YU_SMR_DATA_BLIND(0x1c) |
 2407                       YU_SMR_MFL_VLAN | YU_SMR_IPG_DATA(0x1e));
 2408 
 2409         DPRINTFN(6, ("sk_init_yukon: 10\n"));
 2410         /* Setup Yukon's address */
 2411         for (i = 0; i < 3; i++) {
 2412                 /* Write Source Address 1 (unicast filter) */
 2413                 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4, 
 2414                               sc_if->sk_enaddr[i * 2] |
 2415                               sc_if->sk_enaddr[i * 2 + 1] << 8);
 2416         }
 2417 
 2418         for (i = 0; i < 3; i++) {
 2419                 reg = sk_win_read_2(sc_if->sk_softc,
 2420                                     SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
 2421                 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
 2422         }
 2423 
 2424         /* Set multicast filter */
 2425         DPRINTFN(6, ("sk_init_yukon: 11\n"));
 2426         sk_setmulti(sc_if);
 2427 
 2428         /* enable interrupt mask for counter overflows */
 2429         DPRINTFN(6, ("sk_init_yukon: 12\n"));
 2430         SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
 2431         SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
 2432         SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
 2433 
 2434         /* Configure RX MAC FIFO */
 2435         SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
 2436         SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON);
 2437         
 2438         /* Configure TX MAC FIFO */
 2439         SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
 2440         SK_IF_WRITE_4(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
 2441                 
 2442         DPRINTFN(6, ("sk_init_yukon: end\n"));
 2443 }
 2444 
 2445 /*
 2446  * Note that to properly initialize any part of the GEnesis chip,
 2447  * you first have to take it out of reset mode.
 2448  */
 2449 int
 2450 sk_init(struct ifnet *ifp)
 2451 {
 2452         struct sk_if_softc      *sc_if = ifp->if_softc;
 2453         struct sk_softc         *sc = sc_if->sk_softc;
 2454         struct mii_data         *mii = &sc_if->sk_mii;
 2455         int                     s;
 2456 
 2457         DPRINTFN(1, ("sk_init\n"));
 2458 
 2459         s = splnet();
 2460 
 2461         /* Cancel pending I/O and free all RX/TX buffers. */
 2462         sk_stop(ifp,0);
 2463 
 2464         if (sc->sk_type == SK_GENESIS) {
 2465                 /* Configure LINK_SYNC LED */
 2466                 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON);
 2467                 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL,
 2468                               SK_LINKLED_LINKSYNC_ON);
 2469 
 2470                 /* Configure RX LED */
 2471                 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL,
 2472                               SK_RXLEDCTL_COUNTER_START);
 2473                 
 2474                 /* Configure TX LED */
 2475                 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL,
 2476                               SK_TXLEDCTL_COUNTER_START);
 2477         }
 2478 
 2479         /* Configure I2C registers */
 2480 
 2481         /* Configure XMAC(s) */
 2482         switch (sc->sk_type) {
 2483         case SK_GENESIS:
 2484                 sk_init_xmac(sc_if);
 2485                 break;
 2486         case SK_YUKON:
 2487         case SK_YUKON_LITE:
 2488         case SK_YUKON_LP:
 2489                 sk_init_yukon(sc_if);
 2490                 break;
 2491         }
 2492         mii_mediachg(mii);
 2493 
 2494         if (sc->sk_type == SK_GENESIS) {
 2495                 /* Configure MAC FIFOs */
 2496                 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET);
 2497                 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END);
 2498                 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON);
 2499                 
 2500                 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET);
 2501                 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END);
 2502                 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON);
 2503         }
 2504 
 2505         /* Configure transmit arbiter(s) */
 2506         SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL,
 2507             SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
 2508 
 2509         /* Configure RAMbuffers */
 2510         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
 2511         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
 2512         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
 2513         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
 2514         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
 2515         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
 2516 
 2517         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET);
 2518         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON);
 2519         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart);
 2520         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart);
 2521         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart);
 2522         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend);
 2523         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON);
 2524 
 2525         /* Configure BMUs */
 2526         SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE);
 2527         SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO,
 2528             SK_RX_RING_ADDR(sc_if, 0));
 2529         SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 0);
 2530 
 2531         SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE);
 2532         SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO,
 2533             SK_TX_RING_ADDR(sc_if, 0));
 2534         SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 0);
 2535 
 2536         /* Init descriptors */
 2537         if (sk_init_rx_ring(sc_if) == ENOBUFS) {
 2538                 printf("%s: initialization failed: no "
 2539                     "memory for rx buffers\n", sc_if->sk_dev.dv_xname);
 2540                 sk_stop(ifp,0);
 2541                 splx(s);
 2542                 return(ENOBUFS);
 2543         }
 2544 
 2545         if (sk_init_tx_ring(sc_if) == ENOBUFS) {
 2546                 printf("%s: initialization failed: no "
 2547                     "memory for tx buffers\n", sc_if->sk_dev.dv_xname);
 2548                 sk_stop(ifp,0);
 2549                 splx(s);
 2550                 return(ENOBUFS);
 2551         }
 2552 
 2553         /* Configure interrupt handling */
 2554         CSR_READ_4(sc, SK_ISSR);
 2555         if (sc_if->sk_port == SK_PORT_A)
 2556                 sc->sk_intrmask |= SK_INTRS1;
 2557         else
 2558                 sc->sk_intrmask |= SK_INTRS2;
 2559 
 2560         sc->sk_intrmask |= SK_ISR_EXTERNAL_REG;
 2561 
 2562         CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
 2563 
 2564         /* Start BMUs. */
 2565         SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START);
 2566 
 2567         if (sc->sk_type == SK_GENESIS) {
 2568                 /* Enable XMACs TX and RX state machines */
 2569                 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE);
 2570                 SK_XM_SETBIT_2(sc_if, XM_MMUCMD,
 2571                                XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB);
 2572         }
 2573 
 2574         if (SK_YUKON_FAMILY(sc->sk_type)) {
 2575                 u_int16_t reg = SK_YU_READ_2(sc_if, YUKON_GPCR);
 2576                 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN;
 2577                 reg &= ~(YU_GPCR_SPEED_EN | YU_GPCR_DPLX_EN);
 2578                 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg);
 2579         }
 2580 
 2581 
 2582         ifp->if_flags |= IFF_RUNNING;
 2583         ifp->if_flags &= ~IFF_OACTIVE;
 2584 
 2585         splx(s);
 2586         return(0);
 2587 }
 2588 
 2589 void
 2590 sk_stop(struct ifnet *ifp, int disable)
 2591 {
 2592         struct sk_if_softc      *sc_if = ifp->if_softc;
 2593         struct sk_softc         *sc = sc_if->sk_softc;
 2594         int                     i;
 2595 
 2596         DPRINTFN(1, ("sk_stop\n"));
 2597 
 2598         callout_stop(&sc_if->sk_tick_ch);
 2599 
 2600         if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) {
 2601                 u_int32_t               val;
 2602 
 2603                 /* Put PHY back into reset. */
 2604                 val = sk_win_read_4(sc, SK_GPIO);
 2605                 if (sc_if->sk_port == SK_PORT_A) {
 2606                         val |= SK_GPIO_DIR0;
 2607                         val &= ~SK_GPIO_DAT0;
 2608                 } else {
 2609                         val |= SK_GPIO_DIR2;
 2610                         val &= ~SK_GPIO_DAT2;
 2611                 }
 2612                 sk_win_write_4(sc, SK_GPIO, val);
 2613         }
 2614 
 2615         /* Turn off various components of this interface. */
 2616         SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
 2617         switch (sc->sk_type) {
 2618         case SK_GENESIS:
 2619                 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL,
 2620                               SK_TXMACCTL_XMAC_RESET);
 2621                 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET);
 2622                 break;
 2623         case SK_YUKON:
 2624         case SK_YUKON_LITE:
 2625         case SK_YUKON_LP:
 2626                 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
 2627                 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
 2628                 break;
 2629         }
 2630         SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
 2631         SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
 2632         SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE);
 2633         SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
 2634         SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
 2635         SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
 2636         SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
 2637         SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
 2638         SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
 2639 
 2640         /* Disable interrupts */
 2641         if (sc_if->sk_port == SK_PORT_A)
 2642                 sc->sk_intrmask &= ~SK_INTRS1;
 2643         else
 2644                 sc->sk_intrmask &= ~SK_INTRS2;
 2645         CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
 2646 
 2647         SK_XM_READ_2(sc_if, XM_ISR);
 2648         SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
 2649 
 2650         /* Free RX and TX mbufs still in the queues. */
 2651         for (i = 0; i < SK_RX_RING_CNT; i++) {
 2652                 if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
 2653                         m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
 2654                         sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
 2655                 }
 2656         }
 2657 
 2658         for (i = 0; i < SK_TX_RING_CNT; i++) {
 2659                 if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
 2660                         m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
 2661                         sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
 2662                 }
 2663         }
 2664 
 2665         ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
 2666 }
 2667 
 2668 CFATTACH_DECL(skc,sizeof(struct sk_softc), skc_probe, skc_attach, NULL, NULL);
 2669 
 2670 /*
 2671 struct cfdriver skc_cd = {
 2672         0, "skc", DV_DULL
 2673 };
 2674 */
 2675 
 2676 CFATTACH_DECL(sk,sizeof(struct sk_if_softc), sk_probe, sk_attach, NULL, NULL);
 2677 
 2678 /*
 2679 struct cfdriver sk_cd = {
 2680         0, "sk", DV_IFNET
 2681 };
 2682 */
 2683 
 2684 #ifdef SK_DEBUG
 2685 void
 2686 sk_dump_txdesc(struct sk_tx_desc *desc, int idx)
 2687 {
 2688 #define DESC_PRINT(X)                                   \
 2689         if (desc->X)                                    \
 2690                 printf("txdesc[%d]." #X "=%#x\n",       \
 2691                        idx, desc->X);
 2692 
 2693         DESC_PRINT(sk_ctl);
 2694         DESC_PRINT(sk_next);
 2695         DESC_PRINT(sk_data_lo);
 2696         DESC_PRINT(sk_data_hi);
 2697         DESC_PRINT(sk_xmac_txstat);
 2698         DESC_PRINT(sk_rsvd0);
 2699         DESC_PRINT(sk_csum_startval);
 2700         DESC_PRINT(sk_csum_startpos);
 2701         DESC_PRINT(sk_csum_writepos);
 2702         DESC_PRINT(sk_rsvd1);
 2703 #undef PRINT
 2704 }
 2705 
 2706 void
 2707 sk_dump_bytes(const char *data, int len)
 2708 {
 2709         int c, i, j;
 2710 
 2711         for (i = 0; i < len; i += 16) {
 2712                 printf("%08x  ", i);
 2713                 c = len - i;
 2714                 if (c > 16) c = 16;
 2715 
 2716                 for (j = 0; j < c; j++) {
 2717                         printf("%02x ", data[i + j] & 0xff);
 2718                         if ((j & 0xf) == 7 && j > 0)
 2719                                 printf(" ");
 2720                 }
 2721                 
 2722                 for (; j < 16; j++)
 2723                         printf("   ");
 2724                 printf("  ");
 2725 
 2726                 for (j = 0; j < c; j++) {
 2727                         int ch = data[i + j] & 0xff;
 2728                         printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
 2729                 }
 2730                 
 2731                 printf("\n");
 2732                 
 2733                 if (c < 16)
 2734                         break;
 2735         }
 2736 }
 2737 
 2738 void
 2739 sk_dump_mbuf(struct mbuf *m)
 2740 {
 2741         int count = m->m_pkthdr.len;
 2742 
 2743         printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
 2744 
 2745         while (count > 0 && m) {
 2746                 printf("m=%p, m->m_data=%p, m->m_len=%d\n",
 2747                        m, m->m_data, m->m_len);
 2748                 sk_dump_bytes(mtod(m, char *), m->m_len);
 2749 
 2750                 count -= m->m_len;
 2751                 m = m->m_next;
 2752         }
 2753 }
 2754 #endif

Cache object: c44a679f6e41d36f80023352729a85ca


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