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