Home | History | Annotate | Line # | Download | only in pci
if_msk.c revision 1.19
      1 /* $NetBSD: if_msk.c,v 1.19 2008/05/27 20:14:28 dyoung Exp $ */
      2 /*	$OpenBSD: if_msk.c,v 1.42 2007/01/17 02:43:02 krw Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1997, 1998, 1999, 2000
      6  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Bill Paul.
     19  * 4. Neither the name of the author nor the names of any co-contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     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
     33  * THE POSSIBILITY OF SUCH DAMAGE.
     34  *
     35  * $FreeBSD: /c/ncvs/src/sys/pci/if_sk.c,v 1.20 2000/04/22 02:16:37 wpaul Exp $
     36  */
     37 
     38 /*
     39  * Copyright (c) 2003 Nathan L. Binkert <binkertn (at) umich.edu>
     40  *
     41  * Permission to use, copy, modify, and distribute this software for any
     42  * purpose with or without fee is hereby granted, provided that the above
     43  * copyright notice and this permission notice appear in all copies.
     44  *
     45  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     46  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     47  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     48  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     49  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     50  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     51  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: if_msk.c,v 1.19 2008/05/27 20:14:28 dyoung Exp $");
     56 
     57 #include "bpfilter.h"
     58 #include "rnd.h"
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/sockio.h>
     63 #include <sys/mbuf.h>
     64 #include <sys/malloc.h>
     65 #include <sys/kernel.h>
     66 #include <sys/socket.h>
     67 #include <sys/device.h>
     68 #include <sys/queue.h>
     69 #include <sys/callout.h>
     70 #include <sys/sysctl.h>
     71 #include <sys/endian.h>
     72 #ifdef __NetBSD__
     73  #define letoh16 htole16
     74  #define letoh32 htole32
     75 #endif
     76 
     77 #include <net/if.h>
     78 #include <net/if_dl.h>
     79 #include <net/if_types.h>
     80 
     81 #include <net/if_media.h>
     82 
     83 #if NBPFILTER > 0
     84 #include <net/bpf.h>
     85 #endif
     86 #if NRND > 0
     87 #include <sys/rnd.h>
     88 #endif
     89 
     90 #include <dev/mii/mii.h>
     91 #include <dev/mii/miivar.h>
     92 #include <dev/mii/brgphyreg.h>
     93 
     94 #include <dev/pci/pcireg.h>
     95 #include <dev/pci/pcivar.h>
     96 #include <dev/pci/pcidevs.h>
     97 
     98 #include <dev/pci/if_skreg.h>
     99 #include <dev/pci/if_mskvar.h>
    100 
    101 int mskc_probe(struct device *, struct cfdata *, void *);
    102 void mskc_attach(struct device *, struct device *self, void *aux);
    103 void mskc_shutdown(void *);
    104 int msk_probe(struct device *, struct cfdata *, void *);
    105 void msk_attach(struct device *, struct device *self, void *aux);
    106 int mskcprint(void *, const char *);
    107 int msk_intr(void *);
    108 void msk_intr_yukon(struct sk_if_softc *);
    109 __inline int msk_rxvalid(struct sk_softc *, u_int32_t, u_int32_t);
    110 void msk_rxeof(struct sk_if_softc *, u_int16_t, u_int32_t);
    111 void msk_txeof(struct sk_if_softc *, int);
    112 int msk_encap(struct sk_if_softc *, struct mbuf *, u_int32_t *);
    113 void msk_start(struct ifnet *);
    114 int msk_ioctl(struct ifnet *, u_long, void *);
    115 int msk_init(struct ifnet *);
    116 void msk_init_yukon(struct sk_if_softc *);
    117 void msk_stop(struct ifnet *, int);
    118 void msk_watchdog(struct ifnet *);
    119 void msk_reset(struct sk_softc *);
    120 int msk_newbuf(struct sk_if_softc *, int, struct mbuf *, bus_dmamap_t);
    121 int msk_alloc_jumbo_mem(struct sk_if_softc *);
    122 void *msk_jalloc(struct sk_if_softc *);
    123 void msk_jfree(struct mbuf *, void *, size_t, void *);
    124 int msk_init_rx_ring(struct sk_if_softc *);
    125 int msk_init_tx_ring(struct sk_if_softc *);
    126 
    127 void msk_update_int_mod(struct sk_softc *);
    128 
    129 int msk_miibus_readreg(struct device *, int, int);
    130 void msk_miibus_writereg(struct device *, int, int, int);
    131 void msk_miibus_statchg(struct device *);
    132 
    133 void msk_setfilt(struct sk_if_softc *, void *, int);
    134 void msk_setmulti(struct sk_if_softc *);
    135 void msk_setpromisc(struct sk_if_softc *);
    136 void msk_tick(void *);
    137 
    138 /* #define MSK_DEBUG 1 */
    139 #ifdef MSK_DEBUG
    140 #define DPRINTF(x)	if (mskdebug) printf x
    141 #define DPRINTFN(n,x)	if (mskdebug >= (n)) printf x
    142 int	mskdebug = MSK_DEBUG;
    143 
    144 void msk_dump_txdesc(struct msk_tx_desc *, int);
    145 void msk_dump_mbuf(struct mbuf *);
    146 void msk_dump_bytes(const char *, int);
    147 #else
    148 #define DPRINTF(x)
    149 #define DPRINTFN(n,x)
    150 #endif
    151 
    152 static int msk_sysctl_handler(SYSCTLFN_PROTO);
    153 static int msk_root_num;
    154 
    155 /* supported device vendors */
    156 static const struct msk_product {
    157         pci_vendor_id_t         msk_vendor;
    158         pci_product_id_t        msk_product;
    159 } msk_products[] = {
    160 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE550SX },
    161 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE560SX },
    162 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE560T },
    163 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_1 },
    164 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C032 },
    165 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C033 },
    166 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C034 },
    167 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C036 },
    168 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C042 },
    169 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C055 },
    170 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8035 },
    171 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8036 },
    172 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8038 },
    173 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8039 },
    174 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8050 },
    175 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8052 },
    176 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8053 },
    177 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8055 },
    178 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8056 },
    179 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8021CU },
    180 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8021X },
    181 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8022CU },
    182 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8022X },
    183 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8061CU },
    184 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8061X },
    185 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8062CU },
    186 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8062X },
    187 	{ PCI_VENDOR_SCHNEIDERKOCH,	PCI_PRODUCT_SCHNEIDERKOCH_SK_9SXX },
    188 	{ PCI_VENDOR_SCHNEIDERKOCH,	PCI_PRODUCT_SCHNEIDERKOCH_SK_9E21 }
    189 };
    190 
    191 static inline u_int32_t
    192 sk_win_read_4(struct sk_softc *sc, u_int32_t reg)
    193 {
    194 	return CSR_READ_4(sc, reg);
    195 }
    196 
    197 static inline u_int16_t
    198 sk_win_read_2(struct sk_softc *sc, u_int32_t reg)
    199 {
    200 	return CSR_READ_2(sc, reg);
    201 }
    202 
    203 static inline u_int8_t
    204 sk_win_read_1(struct sk_softc *sc, u_int32_t reg)
    205 {
    206 	return CSR_READ_1(sc, reg);
    207 }
    208 
    209 static inline void
    210 sk_win_write_4(struct sk_softc *sc, u_int32_t reg, u_int32_t x)
    211 {
    212 	CSR_WRITE_4(sc, reg, x);
    213 }
    214 
    215 static inline void
    216 sk_win_write_2(struct sk_softc *sc, u_int32_t reg, u_int16_t x)
    217 {
    218 	CSR_WRITE_2(sc, reg, x);
    219 }
    220 
    221 static inline void
    222 sk_win_write_1(struct sk_softc *sc, u_int32_t reg, u_int8_t x)
    223 {
    224 	CSR_WRITE_1(sc, reg, x);
    225 }
    226 
    227 int
    228 msk_miibus_readreg(struct device *dev, int phy, int reg)
    229 {
    230 	struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
    231 	u_int16_t val;
    232 	int i;
    233 
    234         SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
    235 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
    236 
    237 	for (i = 0; i < SK_TIMEOUT; i++) {
    238 		DELAY(1);
    239 		val = SK_YU_READ_2(sc_if, YUKON_SMICR);
    240 		if (val & YU_SMICR_READ_VALID)
    241 			break;
    242 	}
    243 
    244 	if (i == SK_TIMEOUT) {
    245 		aprint_error_dev(&sc_if->sk_dev, "phy failed to come ready\n");
    246 		return (0);
    247 	}
    248 
    249  	DPRINTFN(9, ("msk_miibus_readreg: i=%d, timeout=%d\n", i,
    250 		     SK_TIMEOUT));
    251 
    252         val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
    253 
    254 	DPRINTFN(9, ("msk_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
    255 		     phy, reg, val));
    256 
    257 	return (val);
    258 }
    259 
    260 void
    261 msk_miibus_writereg(struct device *dev, int phy, int reg, int val)
    262 {
    263 	struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
    264 	int i;
    265 
    266 	DPRINTFN(9, ("msk_miibus_writereg phy=%d reg=%#x val=%#x\n",
    267 		     phy, reg, val));
    268 
    269 	SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
    270 	SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
    271 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
    272 
    273 	for (i = 0; i < SK_TIMEOUT; i++) {
    274 		DELAY(1);
    275 		if (!(SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY))
    276 			break;
    277 	}
    278 
    279 	if (i == SK_TIMEOUT)
    280 		aprint_error_dev(&sc_if->sk_dev, "phy write timed out\n");
    281 }
    282 
    283 void
    284 msk_miibus_statchg(struct device *dev)
    285 {
    286 	struct sk_if_softc *sc_if = (struct sk_if_softc *)dev;
    287 	struct mii_data *mii = &sc_if->sk_mii;
    288 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    289 	int gpcr;
    290 
    291 	gpcr = SK_YU_READ_2(sc_if, YUKON_GPCR);
    292 	gpcr &= (YU_GPCR_TXEN | YU_GPCR_RXEN);
    293 
    294 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
    295 		/* Set speed. */
    296 		gpcr |= YU_GPCR_SPEED_DIS;
    297 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    298 		case IFM_1000_SX:
    299 		case IFM_1000_LX:
    300 		case IFM_1000_CX:
    301 		case IFM_1000_T:
    302 			gpcr |= (YU_GPCR_GIG | YU_GPCR_SPEED);
    303 			break;
    304 		case IFM_100_TX:
    305 			gpcr |= YU_GPCR_SPEED;
    306 			break;
    307 		}
    308 
    309 		/* Set duplex. */
    310 		gpcr |= YU_GPCR_DPLX_DIS;
    311 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    312 			gpcr |= YU_GPCR_DUPLEX;
    313 
    314 		/* Disable flow control. */
    315 		gpcr |= YU_GPCR_FCTL_DIS;
    316 		gpcr |= (YU_GPCR_FCTL_TX_DIS | YU_GPCR_FCTL_RX_DIS);
    317 	}
    318 
    319 	SK_YU_WRITE_2(sc_if, YUKON_GPCR, gpcr);
    320 
    321 	DPRINTFN(9, ("msk_miibus_statchg: gpcr=%x\n",
    322 		     SK_YU_READ_2(((struct sk_if_softc *)dev), YUKON_GPCR)));
    323 }
    324 
    325 #define HASH_BITS	6
    326 
    327 void
    328 msk_setfilt(struct sk_if_softc *sc_if, void *addrv, int slot)
    329 {
    330 	char *addr = addrv;
    331 	int base = XM_RXFILT_ENTRY(slot);
    332 
    333 	SK_XM_WRITE_2(sc_if, base, *(u_int16_t *)(&addr[0]));
    334 	SK_XM_WRITE_2(sc_if, base + 2, *(u_int16_t *)(&addr[2]));
    335 	SK_XM_WRITE_2(sc_if, base + 4, *(u_int16_t *)(&addr[4]));
    336 }
    337 
    338 void
    339 msk_setmulti(struct sk_if_softc *sc_if)
    340 {
    341 	struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
    342 	u_int32_t hashes[2] = { 0, 0 };
    343 	int h;
    344 	struct ethercom *ec = &sc_if->sk_ethercom;
    345 	struct ether_multi *enm;
    346 	struct ether_multistep step;
    347 	u_int16_t reg;
    348 
    349 	/* First, zot all the existing filters. */
    350 	SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
    351 	SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
    352 	SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
    353 	SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
    354 
    355 
    356 	/* Now program new ones. */
    357 	reg = SK_YU_READ_2(sc_if, YUKON_RCR);
    358 	reg |= YU_RCR_UFLEN;
    359 allmulti:
    360 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    361 		if ((ifp->if_flags & IFF_PROMISC) != 0)
    362 			reg &= ~(YU_RCR_UFLEN | YU_RCR_MUFLEN);
    363 		else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
    364 			hashes[0] = 0xFFFFFFFF;
    365 			hashes[1] = 0xFFFFFFFF;
    366 		}
    367 	} else {
    368 		/* First find the tail of the list. */
    369 		ETHER_FIRST_MULTI(step, ec, enm);
    370 		while (enm != NULL) {
    371 			if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
    372 				 ETHER_ADDR_LEN)) {
    373 				ifp->if_flags |= IFF_ALLMULTI;
    374 				goto allmulti;
    375 			}
    376 			h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) &
    377 			    ((1 << HASH_BITS) - 1);
    378 			if (h < 32)
    379 				hashes[0] |= (1 << h);
    380 			else
    381 				hashes[1] |= (1 << (h - 32));
    382 
    383 			ETHER_NEXT_MULTI(step, enm);
    384 		}
    385 		reg |= YU_RCR_MUFLEN;
    386 	}
    387 
    388 	SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
    389 	SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
    390 	SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
    391 	SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
    392 	SK_YU_WRITE_2(sc_if, YUKON_RCR, reg);
    393 }
    394 
    395 void
    396 msk_setpromisc(struct sk_if_softc *sc_if)
    397 {
    398 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
    399 
    400 	if (ifp->if_flags & IFF_PROMISC)
    401 		SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
    402 		    YU_RCR_UFLEN | YU_RCR_MUFLEN);
    403 	else
    404 		SK_YU_SETBIT_2(sc_if, YUKON_RCR,
    405 		    YU_RCR_UFLEN | YU_RCR_MUFLEN);
    406 }
    407 
    408 int
    409 msk_init_rx_ring(struct sk_if_softc *sc_if)
    410 {
    411 	struct msk_chain_data	*cd = &sc_if->sk_cdata;
    412 	struct msk_ring_data	*rd = sc_if->sk_rdata;
    413 	int			i, nexti;
    414 
    415 	bzero((char *)rd->sk_rx_ring,
    416 	    sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT);
    417 
    418 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
    419 		cd->sk_rx_chain[i].sk_le = &rd->sk_rx_ring[i];
    420 		if (i == (MSK_RX_RING_CNT - 1))
    421 			nexti = 0;
    422 		else
    423 			nexti = i + 1;
    424 		cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[nexti];
    425 	}
    426 
    427 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
    428 		if (msk_newbuf(sc_if, i, NULL,
    429 		    sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
    430 			aprint_error_dev(&sc_if->sk_dev, "failed alloc of %dth mbuf\n", i);
    431 			return (ENOBUFS);
    432 		}
    433 	}
    434 
    435 	sc_if->sk_cdata.sk_rx_prod = MSK_RX_RING_CNT - 1;
    436 	sc_if->sk_cdata.sk_rx_cons = 0;
    437 
    438 	return (0);
    439 }
    440 
    441 int
    442 msk_init_tx_ring(struct sk_if_softc *sc_if)
    443 {
    444 	struct sk_softc		*sc = sc_if->sk_softc;
    445 	struct msk_chain_data	*cd = &sc_if->sk_cdata;
    446 	struct msk_ring_data	*rd = sc_if->sk_rdata;
    447 	bus_dmamap_t		dmamap;
    448 	struct sk_txmap_entry	*entry;
    449 	int			i, nexti;
    450 
    451 	bzero((char *)sc_if->sk_rdata->sk_tx_ring,
    452 	    sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT);
    453 
    454 	SIMPLEQ_INIT(&sc_if->sk_txmap_head);
    455 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
    456 		cd->sk_tx_chain[i].sk_le = &rd->sk_tx_ring[i];
    457 		if (i == (MSK_TX_RING_CNT - 1))
    458 			nexti = 0;
    459 		else
    460 			nexti = i + 1;
    461 		cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[nexti];
    462 
    463 		if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
    464 		   SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap))
    465 			return (ENOBUFS);
    466 
    467 		entry = malloc(sizeof(*entry), M_DEVBUF, M_NOWAIT);
    468 		if (!entry) {
    469 			bus_dmamap_destroy(sc->sc_dmatag, dmamap);
    470 			return (ENOBUFS);
    471 		}
    472 		entry->dmamap = dmamap;
    473 		SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
    474 	}
    475 
    476 	sc_if->sk_cdata.sk_tx_prod = 0;
    477 	sc_if->sk_cdata.sk_tx_cons = 0;
    478 	sc_if->sk_cdata.sk_tx_cnt = 0;
    479 
    480 	MSK_CDTXSYNC(sc_if, 0, MSK_TX_RING_CNT,
    481 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    482 
    483 	return (0);
    484 }
    485 
    486 int
    487 msk_newbuf(struct sk_if_softc *sc_if, int i, struct mbuf *m,
    488 	  bus_dmamap_t dmamap)
    489 {
    490 	struct mbuf		*m_new = NULL;
    491 	struct sk_chain		*c;
    492 	struct msk_rx_desc	*r;
    493 
    494 	if (m == NULL) {
    495 		void *buf = NULL;
    496 
    497 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    498 		if (m_new == NULL)
    499 			return (ENOBUFS);
    500 
    501 		/* Allocate the jumbo buffer */
    502 		buf = msk_jalloc(sc_if);
    503 		if (buf == NULL) {
    504 			m_freem(m_new);
    505 			DPRINTFN(1, ("%s jumbo allocation failed -- packet "
    506 			    "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
    507 			return (ENOBUFS);
    508 		}
    509 
    510 		/* Attach the buffer to the mbuf */
    511 		m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
    512 		MEXTADD(m_new, buf, SK_JLEN, 0, msk_jfree, sc_if);
    513 	} else {
    514 		/*
    515 	 	 * We're re-using a previously allocated mbuf;
    516 		 * be sure to re-init pointers and lengths to
    517 		 * default values.
    518 		 */
    519 		m_new = m;
    520 		m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
    521 		m_new->m_data = m_new->m_ext.ext_buf;
    522 	}
    523 	m_adj(m_new, ETHER_ALIGN);
    524 
    525 	c = &sc_if->sk_cdata.sk_rx_chain[i];
    526 	r = c->sk_le;
    527 	c->sk_mbuf = m_new;
    528 	r->sk_addr = htole32(dmamap->dm_segs[0].ds_addr +
    529 	    (((vaddr_t)m_new->m_data
    530              - (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf)));
    531 	r->sk_len = htole16(SK_JLEN);
    532 	r->sk_ctl = 0;
    533 	r->sk_opcode = SK_Y2_RXOPC_PACKET | SK_Y2_RXOPC_OWN;
    534 
    535 	MSK_CDRXSYNC(sc_if, i, BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    536 
    537 	return (0);
    538 }
    539 
    540 /*
    541  * Memory management for jumbo frames.
    542  */
    543 
    544 int
    545 msk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
    546 {
    547 	struct sk_softc		*sc = sc_if->sk_softc;
    548 	char *ptr, *kva;
    549 	bus_dma_segment_t	seg;
    550 	int		i, rseg, state, error;
    551 	struct sk_jpool_entry   *entry;
    552 
    553 	state = error = 0;
    554 
    555 	/* Grab a big chunk o' storage. */
    556 	if (bus_dmamem_alloc(sc->sc_dmatag, MSK_JMEM, PAGE_SIZE, 0,
    557 			     &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    558 		aprint_error(": can't alloc rx buffers");
    559 		return (ENOBUFS);
    560 	}
    561 
    562 	state = 1;
    563 	if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg, MSK_JMEM, (void **)&kva,
    564 			   BUS_DMA_NOWAIT)) {
    565 		aprint_error(": can't map dma buffers (%d bytes)", MSK_JMEM);
    566 		error = ENOBUFS;
    567 		goto out;
    568 	}
    569 
    570 	state = 2;
    571 	if (bus_dmamap_create(sc->sc_dmatag, MSK_JMEM, 1, MSK_JMEM, 0,
    572 	    BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
    573 		aprint_error(": can't create dma map");
    574 		error = ENOBUFS;
    575 		goto out;
    576 	}
    577 
    578 	state = 3;
    579 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
    580 			    kva, MSK_JMEM, NULL, BUS_DMA_NOWAIT)) {
    581 		aprint_error(": can't load dma map");
    582 		error = ENOBUFS;
    583 		goto out;
    584 	}
    585 
    586 	state = 4;
    587 	sc_if->sk_cdata.sk_jumbo_buf = (void *)kva;
    588 	DPRINTFN(1,("msk_jumbo_buf = %p\n", (void *)sc_if->sk_cdata.sk_jumbo_buf));
    589 
    590 	LIST_INIT(&sc_if->sk_jfree_listhead);
    591 	LIST_INIT(&sc_if->sk_jinuse_listhead);
    592 
    593 	/*
    594 	 * Now divide it up into 9K pieces and save the addresses
    595 	 * in an array.
    596 	 */
    597 	ptr = sc_if->sk_cdata.sk_jumbo_buf;
    598 	for (i = 0; i < MSK_JSLOTS; i++) {
    599 		sc_if->sk_cdata.sk_jslots[i] = ptr;
    600 		ptr += SK_JLEN;
    601 		entry = malloc(sizeof(struct sk_jpool_entry),
    602 		    M_DEVBUF, M_NOWAIT);
    603 		if (entry == NULL) {
    604 			sc_if->sk_cdata.sk_jumbo_buf = NULL;
    605 			aprint_error(": no memory for jumbo buffer queue!");
    606 			error = ENOBUFS;
    607 			goto out;
    608 		}
    609 		entry->slot = i;
    610 		LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
    611 				 entry, jpool_entries);
    612 	}
    613 out:
    614 	if (error != 0) {
    615 		switch (state) {
    616 		case 4:
    617 			bus_dmamap_unload(sc->sc_dmatag,
    618 			    sc_if->sk_cdata.sk_rx_jumbo_map);
    619 		case 3:
    620 			bus_dmamap_destroy(sc->sc_dmatag,
    621 			    sc_if->sk_cdata.sk_rx_jumbo_map);
    622 		case 2:
    623 			bus_dmamem_unmap(sc->sc_dmatag, kva, MSK_JMEM);
    624 		case 1:
    625 			bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
    626 			break;
    627 		default:
    628 			break;
    629 		}
    630 	}
    631 
    632 	return (error);
    633 }
    634 
    635 /*
    636  * Allocate a jumbo buffer.
    637  */
    638 void *
    639 msk_jalloc(struct sk_if_softc *sc_if)
    640 {
    641 	struct sk_jpool_entry   *entry;
    642 
    643 	entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
    644 
    645 	if (entry == NULL)
    646 		return (NULL);
    647 
    648 	LIST_REMOVE(entry, jpool_entries);
    649 	LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
    650 	return (sc_if->sk_cdata.sk_jslots[entry->slot]);
    651 }
    652 
    653 /*
    654  * Release a jumbo buffer.
    655  */
    656 void
    657 msk_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
    658 {
    659 	struct sk_jpool_entry *entry;
    660 	struct sk_if_softc *sc;
    661 	int i, s;
    662 
    663 	/* Extract the softc struct pointer. */
    664 	sc = (struct sk_if_softc *)arg;
    665 
    666 	if (sc == NULL)
    667 		panic("msk_jfree: can't find softc pointer!");
    668 
    669 	/* calculate the slot this buffer belongs to */
    670 	i = ((vaddr_t)buf
    671 	     - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
    672 
    673 	if ((i < 0) || (i >= MSK_JSLOTS))
    674 		panic("msk_jfree: asked to free buffer that we don't manage!");
    675 
    676 	s = splvm();
    677 	entry = LIST_FIRST(&sc->sk_jinuse_listhead);
    678 	if (entry == NULL)
    679 		panic("msk_jfree: buffer not in use!");
    680 	entry->slot = i;
    681 	LIST_REMOVE(entry, jpool_entries);
    682 	LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
    683 
    684 	if (__predict_true(m != NULL))
    685 		pool_cache_put(mb_cache, m);
    686 	splx(s);
    687 }
    688 
    689 int
    690 msk_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    691 {
    692 	struct sk_if_softc *sc_if = ifp->if_softc;
    693 	int s, error = 0;
    694 
    695 	s = splnet();
    696 
    697 	DPRINTFN(2, ("msk_ioctl ETHER\n"));
    698 	error = ether_ioctl(ifp, cmd, data);
    699 
    700 	if (error == ENETRESET) {
    701 		error = 0;
    702 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    703 			;
    704 		else if (ifp->if_flags & IFF_RUNNING) {
    705 			/*
    706 			 * Multicast list has changed; set the hardware
    707 			 * filter accordingly.
    708 			 */
    709 			msk_setmulti(sc_if);
    710 		}
    711 	}
    712 
    713 	splx(s);
    714 	return (error);
    715 }
    716 
    717 void
    718 msk_update_int_mod(struct sk_softc *sc)
    719 {
    720 	u_int32_t imtimer_ticks;
    721 
    722 	/*
    723  	 * Configure interrupt moderation. The moderation timer
    724 	 * defers interrupts specified in the interrupt moderation
    725 	 * timer mask based on the timeout specified in the interrupt
    726 	 * moderation timer init register. Each bit in the timer
    727 	 * register represents one tick, so to specify a timeout in
    728 	 * microseconds, we have to multiply by the correct number of
    729 	 * ticks-per-microsecond.
    730 	 */
    731 	switch (sc->sk_type) {
    732 	case SK_YUKON_EC:
    733 	case SK_YUKON_EC_U:
    734 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
    735 		break;
    736 	case SK_YUKON_FE:
    737 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
    738 		break;
    739 	case SK_YUKON_XL:
    740 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
    741 		break;
    742 	default:
    743 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
    744 	}
    745 	aprint_verbose_dev(&sc->sk_dev, "interrupt moderation is %d us\n",
    746 	    sc->sk_int_mod);
    747         sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
    748         sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF|
    749 	    SK_ISR_RX1_EOF|SK_ISR_RX2_EOF);
    750         sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
    751 	sc->sk_int_mod_pending = 0;
    752 }
    753 
    754 static int
    755 msk_lookup(const struct pci_attach_args *pa)
    756 {
    757 	const struct msk_product *pmsk;
    758 
    759 	for ( pmsk = &msk_products[0]; pmsk->msk_vendor != 0; pmsk++) {
    760 		if (PCI_VENDOR(pa->pa_id) == pmsk->msk_vendor &&
    761 		    PCI_PRODUCT(pa->pa_id) == pmsk->msk_product)
    762 			return 1;
    763 	}
    764 	return 0;
    765 }
    766 
    767 /*
    768  * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
    769  * IDs against our list and return a device name if we find a match.
    770  */
    771 int
    772 mskc_probe(struct device *parent, struct cfdata *match,
    773     void *aux)
    774 {
    775 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    776 
    777 	return msk_lookup(pa);
    778 }
    779 
    780 /*
    781  * Force the GEnesis into reset, then bring it out of reset.
    782  */
    783 void msk_reset(struct sk_softc *sc)
    784 {
    785 	u_int32_t imtimer_ticks, reg1;
    786 	int reg;
    787 
    788 	DPRINTFN(2, ("msk_reset\n"));
    789 
    790 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_RESET);
    791 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_RESET);
    792 
    793 	DELAY(1000);
    794 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_UNRESET);
    795 	DELAY(2);
    796 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
    797 	sk_win_write_1(sc, SK_TESTCTL1, 2);
    798 
    799 	reg1 = sk_win_read_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1));
    800 	if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
    801 		reg1 |= (SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
    802 	else
    803 		reg1 &= ~(SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
    804 	sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1), reg1);
    805 
    806 	if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
    807 		sk_win_write_1(sc, SK_Y2_CLKGATE,
    808 		    SK_Y2_CLKGATE_LINK1_GATE_DIS |
    809 		    SK_Y2_CLKGATE_LINK2_GATE_DIS |
    810 		    SK_Y2_CLKGATE_LINK1_CORE_DIS |
    811 		    SK_Y2_CLKGATE_LINK2_CORE_DIS |
    812 		    SK_Y2_CLKGATE_LINK1_PCI_DIS | SK_Y2_CLKGATE_LINK2_PCI_DIS);
    813 	else
    814 		sk_win_write_1(sc, SK_Y2_CLKGATE, 0);
    815 
    816 	CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
    817 	CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_SET);
    818 	DELAY(1000);
    819 	CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
    820 	CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_CLEAR);
    821 
    822 	sk_win_write_1(sc, SK_TESTCTL1, 1);
    823 
    824 	DPRINTFN(2, ("msk_reset: sk_csr=%x\n", CSR_READ_1(sc, SK_CSR)));
    825 	DPRINTFN(2, ("msk_reset: sk_link_ctrl=%x\n",
    826 		     CSR_READ_2(sc, SK_LINK_CTRL)));
    827 
    828 	/* Disable ASF */
    829 	CSR_WRITE_1(sc, SK_Y2_ASF_CSR, SK_Y2_ASF_RESET);
    830 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_ASF_OFF);
    831 
    832 	/* Clear I2C IRQ noise */
    833 	CSR_WRITE_4(sc, SK_I2CHWIRQ, 1);
    834 
    835 	/* Disable hardware timer */
    836 	CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_STOP);
    837 	CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_IRQ_CLEAR);
    838 
    839 	/* Disable descriptor polling */
    840 	CSR_WRITE_4(sc, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_STOP);
    841 
    842 	/* Disable time stamps */
    843 	CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_STOP);
    844 	CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_IRQ_CLEAR);
    845 
    846 	/* Enable RAM interface */
    847 	sk_win_write_1(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
    848 	for (reg = SK_TO0;reg <= SK_TO11; reg++)
    849 		sk_win_write_1(sc, reg, 36);
    850 	sk_win_write_1(sc, SK_RAMCTL + (SK_WIN_LEN / 2), SK_RAMCTL_UNRESET);
    851 	for (reg = SK_TO0;reg <= SK_TO11; reg++)
    852 		sk_win_write_1(sc, reg + (SK_WIN_LEN / 2), 36);
    853 
    854 	/*
    855 	 * Configure interrupt moderation. The moderation timer
    856 	 * defers interrupts specified in the interrupt moderation
    857 	 * timer mask based on the timeout specified in the interrupt
    858 	 * moderation timer init register. Each bit in the timer
    859 	 * register represents one tick, so to specify a timeout in
    860 	 * microseconds, we have to multiply by the correct number of
    861 	 * ticks-per-microsecond.
    862 	 */
    863 	switch (sc->sk_type) {
    864 	case SK_YUKON_EC:
    865 	case SK_YUKON_EC_U:
    866 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
    867 		break;
    868 	case SK_YUKON_FE:
    869 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
    870 		break;
    871 	case SK_YUKON_XL:
    872 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
    873 		break;
    874 	default:
    875 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
    876 	}
    877 
    878 	/* Reset status ring. */
    879 	bzero((char *)sc->sk_status_ring,
    880 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
    881 	bus_dmamap_sync(sc->sc_dmatag, sc->sk_status_map, 0,
    882 	    sc->sk_status_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    883 	sc->sk_status_idx = 0;
    884 	sc->sk_status_own_idx = 0;
    885 
    886 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_RESET);
    887 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_UNRESET);
    888 
    889 	sk_win_write_2(sc, SK_STAT_BMU_LIDX, MSK_STATUS_RING_CNT - 1);
    890 	sk_win_write_4(sc, SK_STAT_BMU_ADDRLO,
    891 	    sc->sk_status_map->dm_segs[0].ds_addr);
    892 	sk_win_write_4(sc, SK_STAT_BMU_ADDRHI,
    893 	    (u_int64_t)sc->sk_status_map->dm_segs[0].ds_addr >> 32);
    894 	if ((sc->sk_workaround & SK_STAT_BMU_FIFOIWM) != 0) {
    895 		sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, SK_STAT_BMU_TXTHIDX_MSK);
    896 		sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x21);
    897 		sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 0x07);
    898 	} else {
    899 		sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, 0x000a);
    900 		sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x10);
    901 		sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM,
    902 		    ((sc->sk_workaround & SK_WA_4109) != 0) ? 0x10 : 0x04);
    903 		sk_win_write_4(sc, SK_Y2_ISR_ITIMERINIT, 0x0190); /* 3.2us on Yukon-EC */
    904 	}
    905 
    906 #if 0
    907 	sk_win_write_4(sc, SK_Y2_LEV_ITIMERINIT, SK_IM_USECS(100));
    908 #endif
    909 	sk_win_write_4(sc, SK_Y2_TX_ITIMERINIT, SK_IM_USECS(1000));
    910 
    911 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_ON);
    912 
    913 	sk_win_write_1(sc, SK_Y2_LEV_ITIMERCTL, SK_IMCTL_START);
    914 	sk_win_write_1(sc, SK_Y2_TX_ITIMERCTL, SK_IMCTL_START);
    915 	sk_win_write_1(sc, SK_Y2_ISR_ITIMERCTL, SK_IMCTL_START);
    916 
    917 	msk_update_int_mod(sc);
    918 }
    919 
    920 int
    921 msk_probe(struct device *parent, struct cfdata *match,
    922     void *aux)
    923 {
    924 	struct skc_attach_args *sa = aux;
    925 
    926 	if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
    927 		return (0);
    928 
    929 	switch (sa->skc_type) {
    930 	case SK_YUKON_XL:
    931 	case SK_YUKON_EC_U:
    932 	case SK_YUKON_EC:
    933 	case SK_YUKON_FE:
    934 		return (1);
    935 	}
    936 
    937 	return (0);
    938 }
    939 
    940 /*
    941  * Each XMAC chip is attached as a separate logical IP interface.
    942  * Single port cards will have only one logical interface of course.
    943  */
    944 void
    945 msk_attach(struct device *parent, struct device *self, void *aux)
    946 {
    947 	struct sk_if_softc *sc_if = (struct sk_if_softc *) self;
    948 	struct sk_softc *sc = (struct sk_softc *)parent;
    949 	struct skc_attach_args *sa = aux;
    950 	struct ifnet *ifp;
    951 	void *kva;
    952 	bus_dma_segment_t seg;
    953 	int i, rseg;
    954 	u_int32_t chunk, val;
    955 
    956 	sc_if->sk_port = sa->skc_port;
    957 	sc_if->sk_softc = sc;
    958 	sc->sk_if[sa->skc_port] = sc_if;
    959 
    960 	DPRINTFN(2, ("begin msk_attach: port=%d\n", sc_if->sk_port));
    961 
    962 	/*
    963 	 * Get station address for this interface. Note that
    964 	 * dual port cards actually come with three station
    965 	 * addresses: one for each port, plus an extra. The
    966 	 * extra one is used by the SysKonnect driver software
    967 	 * as a 'virtual' station address for when both ports
    968 	 * are operating in failover mode. Currently we don't
    969 	 * use this extra address.
    970 	 */
    971 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    972 		sc_if->sk_enaddr[i] =
    973 		    sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
    974 
    975 	aprint_normal(": Ethernet address %s\n",
    976 	    ether_sprintf(sc_if->sk_enaddr));
    977 
    978 	/*
    979 	 * Set up RAM buffer addresses. The NIC will have a certain
    980 	 * amount of SRAM on it, somewhere between 512K and 2MB. We
    981 	 * need to divide this up a) between the transmitter and
    982  	 * receiver and b) between the two XMACs, if this is a
    983 	 * dual port NIC. Our algorithm is to divide up the memory
    984 	 * evenly so that everyone gets a fair share.
    985 	 *
    986 	 * Just to be contrary, Yukon2 appears to have separate memory
    987 	 * for each MAC.
    988 	 */
    989 	chunk = sc->sk_ramsize  - (sc->sk_ramsize + 2) / 3;
    990 	val = sc->sk_rboff / sizeof(u_int64_t);
    991 	sc_if->sk_rx_ramstart = val;
    992 	val += (chunk / sizeof(u_int64_t));
    993 	sc_if->sk_rx_ramend = val - 1;
    994 	chunk = sc->sk_ramsize - chunk;
    995 	sc_if->sk_tx_ramstart = val;
    996 	val += (chunk / sizeof(u_int64_t));
    997 	sc_if->sk_tx_ramend = val - 1;
    998 
    999 	DPRINTFN(2, ("msk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
   1000 		     "           tx_ramstart=%#x tx_ramend=%#x\n",
   1001 		     sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
   1002 		     sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
   1003 
   1004 	/* Allocate the descriptor queues. */
   1005 	if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct msk_ring_data),
   1006 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
   1007 		aprint_error(": can't alloc rx buffers\n");
   1008 		goto fail;
   1009 	}
   1010 	if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
   1011 	    sizeof(struct msk_ring_data), &kva, BUS_DMA_NOWAIT)) {
   1012 		aprint_error(": can't map dma buffers (%zu bytes)\n",
   1013 		       sizeof(struct msk_ring_data));
   1014 		goto fail_1;
   1015 	}
   1016 	if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct msk_ring_data), 1,
   1017 	    sizeof(struct msk_ring_data), 0, BUS_DMA_NOWAIT,
   1018             &sc_if->sk_ring_map)) {
   1019 		aprint_error(": can't create dma map\n");
   1020 		goto fail_2;
   1021 	}
   1022 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
   1023 	    sizeof(struct msk_ring_data), NULL, BUS_DMA_NOWAIT)) {
   1024 		aprint_error(": can't load dma map\n");
   1025 		goto fail_3;
   1026 	}
   1027         sc_if->sk_rdata = (struct msk_ring_data *)kva;
   1028 	bzero(sc_if->sk_rdata, sizeof(struct msk_ring_data));
   1029 
   1030 	ifp = &sc_if->sk_ethercom.ec_if;
   1031 	/* Try to allocate memory for jumbo buffers. */
   1032 	if (msk_alloc_jumbo_mem(sc_if)) {
   1033 		aprint_error(": jumbo buffer allocation failed\n");
   1034 		goto fail_3;
   1035 	}
   1036 	sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU;
   1037 	if (sc->sk_type != SK_YUKON_FE)
   1038 		sc_if->sk_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1039 
   1040 	ifp->if_softc = sc_if;
   1041 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1042 	ifp->if_ioctl = msk_ioctl;
   1043 	ifp->if_start = msk_start;
   1044 	ifp->if_stop = msk_stop;
   1045 	ifp->if_init = msk_init;
   1046 	ifp->if_watchdog = msk_watchdog;
   1047 	ifp->if_baudrate = 1000000000;
   1048 	IFQ_SET_MAXLEN(&ifp->if_snd, MSK_TX_RING_CNT - 1);
   1049 	IFQ_SET_READY(&ifp->if_snd);
   1050 	strlcpy(ifp->if_xname, device_xname(&sc_if->sk_dev), IFNAMSIZ);
   1051 
   1052 	/*
   1053 	 * Do miibus setup.
   1054 	 */
   1055 	msk_init_yukon(sc_if);
   1056 
   1057  	DPRINTFN(2, ("msk_attach: 1\n"));
   1058 
   1059 	sc_if->sk_mii.mii_ifp = ifp;
   1060 	sc_if->sk_mii.mii_readreg = msk_miibus_readreg;
   1061 	sc_if->sk_mii.mii_writereg = msk_miibus_writereg;
   1062 	sc_if->sk_mii.mii_statchg = msk_miibus_statchg;
   1063 
   1064 	sc_if->sk_ethercom.ec_mii = &sc_if->sk_mii;
   1065 	ifmedia_init(&sc_if->sk_mii.mii_media, 0,
   1066 	    ether_mediachange, ether_mediastatus);
   1067 	mii_attach(self, &sc_if->sk_mii, 0xffffffff, MII_PHY_ANY,
   1068 	    MII_OFFSET_ANY, MIIF_DOPAUSE|MIIF_FORCEANEG);
   1069 	if (LIST_FIRST(&sc_if->sk_mii.mii_phys) == NULL) {
   1070 		aprint_error_dev(&sc_if->sk_dev, "no PHY found!\n");
   1071 		ifmedia_add(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL,
   1072 			    0, NULL);
   1073 		ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_MANUAL);
   1074 	} else
   1075 		ifmedia_set(&sc_if->sk_mii.mii_media, IFM_ETHER|IFM_AUTO);
   1076 
   1077 	callout_init(&sc_if->sk_tick_ch, 0);
   1078 	callout_setfunc(&sc_if->sk_tick_ch, msk_tick, sc_if);
   1079 	callout_schedule(&sc_if->sk_tick_ch, hz);
   1080 
   1081 	/*
   1082 	 * Call MI attach routines.
   1083 	 */
   1084 	if_attach(ifp);
   1085 	ether_ifattach(ifp, sc_if->sk_enaddr);
   1086 
   1087 	shutdownhook_establish(mskc_shutdown, sc);
   1088 
   1089 #if NRND > 0
   1090 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sk_dev),
   1091 		RND_TYPE_NET, 0);
   1092 #endif
   1093 
   1094 	DPRINTFN(2, ("msk_attach: end\n"));
   1095 	return;
   1096 
   1097 fail_3:
   1098 	bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
   1099 fail_2:
   1100 	bus_dmamem_unmap(sc->sc_dmatag, kva, sizeof(struct msk_ring_data));
   1101 fail_1:
   1102 	bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
   1103 fail:
   1104 	sc->sk_if[sa->skc_port] = NULL;
   1105 }
   1106 
   1107 int
   1108 mskcprint(void *aux, const char *pnp)
   1109 {
   1110 	struct skc_attach_args *sa = aux;
   1111 
   1112 	if (pnp)
   1113 		aprint_normal("sk port %c at %s",
   1114 		    (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
   1115 	else
   1116 		aprint_normal(" port %c", (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
   1117 	return (UNCONF);
   1118 }
   1119 
   1120 /*
   1121  * Attach the interface. Allocate softc structures, do ifmedia
   1122  * setup and ethernet/BPF attach.
   1123  */
   1124 void
   1125 mskc_attach(struct device *parent, struct device *self, void *aux)
   1126 {
   1127 	struct sk_softc *sc = (struct sk_softc *)self;
   1128 	struct pci_attach_args *pa = aux;
   1129 	struct skc_attach_args skca;
   1130 	pci_chipset_tag_t pc = pa->pa_pc;
   1131 	pcireg_t command, memtype;
   1132 	pci_intr_handle_t ih;
   1133 	const char *intrstr = NULL;
   1134 	bus_size_t size;
   1135 	int rc, sk_nodenum;
   1136 	u_int8_t hw, skrs;
   1137 	const char *revstr = NULL;
   1138 	const struct sysctlnode *node;
   1139 	void *kva;
   1140 	bus_dma_segment_t seg;
   1141 	int rseg;
   1142 
   1143 	DPRINTFN(2, ("begin mskc_attach\n"));
   1144 
   1145 	/*
   1146 	 * Handle power management nonsense.
   1147 	 */
   1148 	command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
   1149 
   1150 	if (command == 0x01) {
   1151 		command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
   1152 		if (command & SK_PSTATE_MASK) {
   1153 			u_int32_t		iobase, membase, irq;
   1154 
   1155 			/* Save important PCI config data. */
   1156 			iobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
   1157 			membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
   1158 			irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
   1159 
   1160 			/* Reset the power state. */
   1161 			aprint_normal_dev(&sc->sk_dev, "chip is in D%d power mode "
   1162 			    "-- setting to D0\n",
   1163 			    command & SK_PSTATE_MASK);
   1164 			command &= 0xFFFFFFFC;
   1165 			pci_conf_write(pc, pa->pa_tag,
   1166 			    SK_PCI_PWRMGMTCTRL, command);
   1167 
   1168 			/* Restore PCI config data. */
   1169 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, iobase);
   1170 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
   1171 			pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
   1172 		}
   1173 	}
   1174 
   1175 	/*
   1176 	 * Map control/status registers.
   1177 	 */
   1178 
   1179 	memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
   1180 	switch (memtype) {
   1181 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   1182 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   1183 		if (pci_mapreg_map(pa, SK_PCI_LOMEM,
   1184 				   memtype, 0, &sc->sk_btag, &sc->sk_bhandle,
   1185 				   NULL, &size) == 0)
   1186 			break;
   1187 	default:
   1188 		aprint_error(": can't map mem space\n");
   1189 		return;
   1190 	}
   1191 
   1192 	sc->sc_dmatag = pa->pa_dmat;
   1193 
   1194 	sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
   1195 	sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
   1196 
   1197 	/* bail out here if chip is not recognized */
   1198 	if (!(SK_IS_YUKON2(sc))) {
   1199 		aprint_error(": unknown chip type: %d\n", sc->sk_type);
   1200 		goto fail_1;
   1201 	}
   1202 	DPRINTFN(2, ("mskc_attach: allocate interrupt\n"));
   1203 
   1204 	/* Allocate interrupt */
   1205 	if (pci_intr_map(pa, &ih)) {
   1206 		aprint_error(": couldn't map interrupt\n");
   1207 		goto fail_1;
   1208 	}
   1209 
   1210 	intrstr = pci_intr_string(pc, ih);
   1211 	sc->sk_intrhand = pci_intr_establish(pc, ih, IPL_NET, msk_intr, sc);
   1212 	if (sc->sk_intrhand == NULL) {
   1213 		aprint_error(": couldn't establish interrupt");
   1214 		if (intrstr != NULL)
   1215 			aprint_error(" at %s", intrstr);
   1216 		aprint_error("\n");
   1217 		goto fail_1;
   1218 	}
   1219 
   1220 	if (bus_dmamem_alloc(sc->sc_dmatag,
   1221 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1222 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
   1223 		aprint_error(": can't alloc status buffers\n");
   1224 		goto fail_2;
   1225 	}
   1226 
   1227 	if (bus_dmamem_map(sc->sc_dmatag, &seg, rseg,
   1228 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1229 	    &kva, BUS_DMA_NOWAIT)) {
   1230 		aprint_error(": can't map dma buffers (%zu bytes)\n",
   1231 		    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1232 		goto fail_3;
   1233 	}
   1234 	if (bus_dmamap_create(sc->sc_dmatag,
   1235 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 1,
   1236 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 0,
   1237 	    BUS_DMA_NOWAIT, &sc->sk_status_map)) {
   1238 		aprint_error(": can't create dma map\n");
   1239 		goto fail_4;
   1240 	}
   1241 	if (bus_dmamap_load(sc->sc_dmatag, sc->sk_status_map, kva,
   1242 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1243 	    NULL, BUS_DMA_NOWAIT)) {
   1244 		aprint_error(": can't load dma map\n");
   1245 		goto fail_5;
   1246 	}
   1247 	sc->sk_status_ring = (struct msk_status_desc *)kva;
   1248 
   1249 	/* Reset the adapter. */
   1250 	msk_reset(sc);
   1251 
   1252 	skrs = sk_win_read_1(sc, SK_EPROM0);
   1253 	if (skrs == 0x00)
   1254 		sc->sk_ramsize = 0x20000;
   1255 	else
   1256 		sc->sk_ramsize = skrs * (1<<12);
   1257 	sc->sk_rboff = SK_RBOFF_0;
   1258 
   1259 	DPRINTFN(2, ("mskc_attach: ramsize=%d (%dk), rboff=%d\n",
   1260 		     sc->sk_ramsize, sc->sk_ramsize / 1024,
   1261 		     sc->sk_rboff));
   1262 
   1263 	switch (sc->sk_type) {
   1264 	case SK_YUKON_XL:
   1265 		sc->sk_name = "Yukon-2 XL";
   1266 		break;
   1267 	case SK_YUKON_EC_U:
   1268 		sc->sk_name = "Yukon-2 EC Ultra";
   1269 		break;
   1270 	case SK_YUKON_EC:
   1271 		sc->sk_name = "Yukon-2 EC";
   1272 		break;
   1273 	case SK_YUKON_FE:
   1274 		sc->sk_name = "Yukon-2 FE";
   1275 		break;
   1276 	default:
   1277 		sc->sk_name = "Yukon (Unknown)";
   1278 	}
   1279 
   1280 	if (sc->sk_type == SK_YUKON_XL) {
   1281 		switch (sc->sk_rev) {
   1282 		case SK_YUKON_XL_REV_A0:
   1283 			sc->sk_workaround = 0;
   1284 			revstr = "A0";
   1285 			break;
   1286 		case SK_YUKON_XL_REV_A1:
   1287 			sc->sk_workaround = SK_WA_4109;
   1288 			revstr = "A1";
   1289 			break;
   1290 		case SK_YUKON_XL_REV_A2:
   1291 			sc->sk_workaround = SK_WA_4109;
   1292 			revstr = "A2";
   1293 			break;
   1294 		case SK_YUKON_XL_REV_A3:
   1295 			sc->sk_workaround = SK_WA_4109;
   1296 			revstr = "A3";
   1297 			break;
   1298 		default:
   1299 			sc->sk_workaround = 0;
   1300 			break;
   1301 		}
   1302 	}
   1303 
   1304 	if (sc->sk_type == SK_YUKON_EC) {
   1305 		switch (sc->sk_rev) {
   1306 		case SK_YUKON_EC_REV_A1:
   1307 			sc->sk_workaround = SK_WA_43_418 | SK_WA_4109;
   1308 			revstr = "A1";
   1309 			break;
   1310 		case SK_YUKON_EC_REV_A2:
   1311 			sc->sk_workaround = SK_WA_4109;
   1312 			revstr = "A2";
   1313 			break;
   1314 		case SK_YUKON_EC_REV_A3:
   1315 			sc->sk_workaround = SK_WA_4109;
   1316 			revstr = "A3";
   1317 			break;
   1318 		default:
   1319 			sc->sk_workaround = 0;
   1320 			break;
   1321 		}
   1322 	}
   1323 
   1324 	if (sc->sk_type == SK_YUKON_FE) {
   1325 		sc->sk_workaround = SK_WA_4109;
   1326 		switch (sc->sk_rev) {
   1327 		case SK_YUKON_FE_REV_A1:
   1328 			revstr = "A1";
   1329 			break;
   1330 		case SK_YUKON_FE_REV_A2:
   1331 			revstr = "A2";
   1332 			break;
   1333 		default:
   1334 			sc->sk_workaround = 0;
   1335 			break;
   1336 		}
   1337 	}
   1338 
   1339 	if (sc->sk_type == SK_YUKON_EC_U) {
   1340 		sc->sk_workaround = SK_WA_4109;
   1341 		switch (sc->sk_rev) {
   1342 		case SK_YUKON_EC_U_REV_A0:
   1343 			revstr = "A0";
   1344 			break;
   1345 		case SK_YUKON_EC_U_REV_A1:
   1346 			revstr = "A1";
   1347 			break;
   1348 		case SK_YUKON_EC_U_REV_B0:
   1349 			revstr = "B0";
   1350 			break;
   1351 		default:
   1352 			sc->sk_workaround = 0;
   1353 			break;
   1354 		}
   1355 	}
   1356 
   1357 	/* Announce the product name. */
   1358 	aprint_normal(", %s", sc->sk_name);
   1359 	if (revstr != NULL)
   1360 		aprint_normal(" rev. %s", revstr);
   1361 	aprint_normal(" (0x%x): %s\n", sc->sk_rev, intrstr);
   1362 
   1363 	sc->sk_macs = 1;
   1364 
   1365 	hw = sk_win_read_1(sc, SK_Y2_HWRES);
   1366 	if ((hw & SK_Y2_HWRES_LINK_MASK) == SK_Y2_HWRES_LINK_DUAL) {
   1367 		if ((sk_win_read_1(sc, SK_Y2_CLKGATE) &
   1368 		    SK_Y2_CLKGATE_LINK2_INACTIVE) == 0)
   1369 			sc->sk_macs++;
   1370 	}
   1371 
   1372 	skca.skc_port = SK_PORT_A;
   1373 	skca.skc_type = sc->sk_type;
   1374 	skca.skc_rev = sc->sk_rev;
   1375 	(void)config_found(&sc->sk_dev, &skca, mskcprint);
   1376 
   1377 	if (sc->sk_macs > 1) {
   1378 		skca.skc_port = SK_PORT_B;
   1379 		skca.skc_type = sc->sk_type;
   1380 		skca.skc_rev = sc->sk_rev;
   1381 		(void)config_found(&sc->sk_dev, &skca, mskcprint);
   1382 	}
   1383 
   1384 	/* Turn on the 'driver is loaded' LED. */
   1385 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
   1386 
   1387 	/* skc sysctl setup */
   1388 
   1389 	sc->sk_int_mod = SK_IM_DEFAULT;
   1390 	sc->sk_int_mod_pending = 0;
   1391 
   1392 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
   1393 	    0, CTLTYPE_NODE, device_xname(&sc->sk_dev),
   1394 	    SYSCTL_DESCR("mskc per-controller controls"),
   1395 	    NULL, 0, NULL, 0, CTL_HW, msk_root_num, CTL_CREATE,
   1396 	    CTL_EOL)) != 0) {
   1397 		aprint_normal_dev(&sc->sk_dev, "couldn't create sysctl node\n");
   1398 		goto fail_6;
   1399 	}
   1400 
   1401 	sk_nodenum = node->sysctl_num;
   1402 
   1403 	/* interrupt moderation time in usecs */
   1404 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
   1405 	    CTLFLAG_READWRITE,
   1406 	    CTLTYPE_INT, "int_mod",
   1407 	    SYSCTL_DESCR("msk interrupt moderation timer"),
   1408 	    msk_sysctl_handler, 0, sc,
   1409 	    0, CTL_HW, msk_root_num, sk_nodenum, CTL_CREATE,
   1410 	    CTL_EOL)) != 0) {
   1411 		aprint_normal_dev(&sc->sk_dev, "couldn't create int_mod sysctl node\n");
   1412 		goto fail_6;
   1413 	}
   1414 
   1415 	return;
   1416 
   1417  fail_6:
   1418 	bus_dmamap_unload(sc->sc_dmatag, sc->sk_status_map);
   1419 fail_5:
   1420 	bus_dmamap_destroy(sc->sc_dmatag, sc->sk_status_map);
   1421 fail_4:
   1422 	bus_dmamem_unmap(sc->sc_dmatag, kva,
   1423 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1424 fail_3:
   1425 	bus_dmamem_free(sc->sc_dmatag, &seg, rseg);
   1426 fail_2:
   1427 	pci_intr_disestablish(pc, sc->sk_intrhand);
   1428 fail_1:
   1429 	bus_space_unmap(sc->sk_btag, sc->sk_bhandle, size);
   1430 }
   1431 
   1432 int
   1433 msk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, u_int32_t *txidx)
   1434 {
   1435 	struct sk_softc		*sc = sc_if->sk_softc;
   1436 	struct msk_tx_desc		*f = NULL;
   1437 	u_int32_t		frag, cur;
   1438 	int			i;
   1439 	struct sk_txmap_entry	*entry;
   1440 	bus_dmamap_t		txmap;
   1441 
   1442 	DPRINTFN(2, ("msk_encap\n"));
   1443 
   1444 	entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
   1445 	if (entry == NULL) {
   1446 		DPRINTFN(2, ("msk_encap: no txmap available\n"));
   1447 		return (ENOBUFS);
   1448 	}
   1449 	txmap = entry->dmamap;
   1450 
   1451 	cur = frag = *txidx;
   1452 
   1453 #ifdef MSK_DEBUG
   1454 	if (mskdebug >= 2)
   1455 		msk_dump_mbuf(m_head);
   1456 #endif
   1457 
   1458 	/*
   1459 	 * Start packing the mbufs in this chain into
   1460 	 * the fragment pointers. Stop when we run out
   1461 	 * of fragments or hit the end of the mbuf chain.
   1462 	 */
   1463 	if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
   1464 	    BUS_DMA_NOWAIT)) {
   1465 		DPRINTFN(2, ("msk_encap: dmamap failed\n"));
   1466 		return (ENOBUFS);
   1467 	}
   1468 
   1469 	if (txmap->dm_nsegs > (MSK_TX_RING_CNT - sc_if->sk_cdata.sk_tx_cnt - 2)) {
   1470 		DPRINTFN(2, ("msk_encap: too few descriptors free\n"));
   1471 		bus_dmamap_unload(sc->sc_dmatag, txmap);
   1472 		return (ENOBUFS);
   1473 	}
   1474 
   1475 	DPRINTFN(2, ("msk_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
   1476 
   1477 	/* Sync the DMA map. */
   1478 	bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
   1479 	    BUS_DMASYNC_PREWRITE);
   1480 
   1481 	for (i = 0; i < txmap->dm_nsegs; i++) {
   1482 		f = &sc_if->sk_rdata->sk_tx_ring[frag];
   1483 		f->sk_addr = htole32(txmap->dm_segs[i].ds_addr);
   1484 		f->sk_len = htole16(txmap->dm_segs[i].ds_len);
   1485 		f->sk_ctl = 0;
   1486 		if (i == 0)
   1487 			f->sk_opcode = SK_Y2_TXOPC_PACKET;
   1488 		else
   1489 			f->sk_opcode = SK_Y2_TXOPC_BUFFER | SK_Y2_TXOPC_OWN;
   1490 		cur = frag;
   1491 		SK_INC(frag, MSK_TX_RING_CNT);
   1492 	}
   1493 
   1494 	sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
   1495 	SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
   1496 
   1497 	sc_if->sk_cdata.sk_tx_map[cur] = entry;
   1498 	sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |= SK_Y2_TXCTL_LASTFRAG;
   1499 
   1500 	/* Sync descriptors before handing to chip */
   1501 	MSK_CDTXSYNC(sc_if, *txidx, txmap->dm_nsegs,
   1502             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1503 
   1504 	sc_if->sk_rdata->sk_tx_ring[*txidx].sk_opcode |= SK_Y2_TXOPC_OWN;
   1505 
   1506 	/* Sync first descriptor to hand it off */
   1507 	MSK_CDTXSYNC(sc_if, *txidx, 1,
   1508 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1509 
   1510 	sc_if->sk_cdata.sk_tx_cnt += txmap->dm_nsegs;
   1511 
   1512 #ifdef MSK_DEBUG
   1513 	if (mskdebug >= 2) {
   1514 		struct msk_tx_desc *le;
   1515 		u_int32_t idx;
   1516 		for (idx = *txidx; idx != frag; SK_INC(idx, MSK_TX_RING_CNT)) {
   1517 			le = &sc_if->sk_rdata->sk_tx_ring[idx];
   1518 			msk_dump_txdesc(le, idx);
   1519 		}
   1520 	}
   1521 #endif
   1522 
   1523 	*txidx = frag;
   1524 
   1525 	DPRINTFN(2, ("msk_encap: completed successfully\n"));
   1526 
   1527 	return (0);
   1528 }
   1529 
   1530 void
   1531 msk_start(struct ifnet *ifp)
   1532 {
   1533         struct sk_if_softc	*sc_if = ifp->if_softc;
   1534         struct mbuf		*m_head = NULL;
   1535         u_int32_t		idx = sc_if->sk_cdata.sk_tx_prod;
   1536 	int			pkts = 0;
   1537 
   1538 	DPRINTFN(2, ("msk_start\n"));
   1539 
   1540 	while (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
   1541 		IFQ_POLL(&ifp->if_snd, m_head);
   1542 		if (m_head == NULL)
   1543 			break;
   1544 
   1545 		/*
   1546 		 * Pack the data into the transmit ring. If we
   1547 		 * don't have room, set the OACTIVE flag and wait
   1548 		 * for the NIC to drain the ring.
   1549 		 */
   1550 		if (msk_encap(sc_if, m_head, &idx)) {
   1551 			ifp->if_flags |= IFF_OACTIVE;
   1552 			break;
   1553 		}
   1554 
   1555 		/* now we are committed to transmit the packet */
   1556 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1557 		pkts++;
   1558 
   1559 		/*
   1560 		 * If there's a BPF listener, bounce a copy of this frame
   1561 		 * to him.
   1562 		 */
   1563 #if NBPFILTER > 0
   1564 		if (ifp->if_bpf)
   1565 			bpf_mtap(ifp->if_bpf, m_head);
   1566 #endif
   1567 	}
   1568 	if (pkts == 0)
   1569 		return;
   1570 
   1571 	/* Transmit */
   1572 	if (idx != sc_if->sk_cdata.sk_tx_prod) {
   1573 		sc_if->sk_cdata.sk_tx_prod = idx;
   1574 		SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_PUTIDX, idx);
   1575 
   1576 		/* Set a timeout in case the chip goes out to lunch. */
   1577 		ifp->if_timer = 5;
   1578 	}
   1579 }
   1580 
   1581 void
   1582 msk_watchdog(struct ifnet *ifp)
   1583 {
   1584 	struct sk_if_softc *sc_if = ifp->if_softc;
   1585 	u_int32_t reg;
   1586 	int idx;
   1587 
   1588 	/*
   1589 	 * Reclaim first as there is a possibility of losing Tx completion
   1590 	 * interrupts.
   1591 	 */
   1592 	if (sc_if->sk_port == SK_PORT_A)
   1593 		reg = SK_STAT_BMU_TXA1_RIDX;
   1594 	else
   1595 		reg = SK_STAT_BMU_TXA2_RIDX;
   1596 
   1597 	idx = sk_win_read_2(sc_if->sk_softc, reg);
   1598 	if (sc_if->sk_cdata.sk_tx_cons != idx) {
   1599 		msk_txeof(sc_if, idx);
   1600 		if (sc_if->sk_cdata.sk_tx_cnt != 0) {
   1601 			aprint_error_dev(&sc_if->sk_dev, "watchdog timeout\n");
   1602 
   1603 			ifp->if_oerrors++;
   1604 
   1605 			/* XXX Resets both ports; we shouldn't do that. */
   1606 			msk_reset(sc_if->sk_softc);
   1607 			msk_init(ifp);
   1608 		}
   1609 	}
   1610 }
   1611 
   1612 void
   1613 mskc_shutdown(void *v)
   1614 {
   1615 	struct sk_softc		*sc = v;
   1616 
   1617 	DPRINTFN(2, ("msk_shutdown\n"));
   1618 
   1619 	/* Turn off the 'driver is loaded' LED. */
   1620 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
   1621 
   1622 	msk_reset(sc);
   1623 }
   1624 
   1625 __inline int
   1626 msk_rxvalid(struct sk_softc *sc, u_int32_t stat, u_int32_t len)
   1627 {
   1628 	if ((stat & (YU_RXSTAT_CRCERR | YU_RXSTAT_LONGERR |
   1629 	    YU_RXSTAT_MIIERR | YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC |
   1630 	    YU_RXSTAT_JABBER)) != 0 ||
   1631 	    (stat & YU_RXSTAT_RXOK) != YU_RXSTAT_RXOK ||
   1632 	    YU_RXSTAT_BYTES(stat) != len)
   1633 		return (0);
   1634 
   1635 	return (1);
   1636 }
   1637 
   1638 void
   1639 msk_rxeof(struct sk_if_softc *sc_if, u_int16_t len, u_int32_t rxstat)
   1640 {
   1641 	struct sk_softc		*sc = sc_if->sk_softc;
   1642 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
   1643 	struct mbuf		*m;
   1644 	struct sk_chain		*cur_rx;
   1645 	int			cur, total_len = len;
   1646 	bus_dmamap_t		dmamap;
   1647 
   1648 	DPRINTFN(2, ("msk_rxeof\n"));
   1649 
   1650 	cur = sc_if->sk_cdata.sk_rx_cons;
   1651 	SK_INC(sc_if->sk_cdata.sk_rx_cons, MSK_RX_RING_CNT);
   1652 	SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
   1653 
   1654 	/* Sync the descriptor */
   1655 	MSK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1656 
   1657 	cur_rx = &sc_if->sk_cdata.sk_rx_chain[cur];
   1658 	dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
   1659 
   1660 	bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
   1661 	    dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1662 
   1663 	m = cur_rx->sk_mbuf;
   1664 	cur_rx->sk_mbuf = NULL;
   1665 
   1666 	if (total_len < SK_MIN_FRAMELEN ||
   1667 	    total_len > ETHER_MAX_LEN_JUMBO ||
   1668 	    msk_rxvalid(sc, rxstat, total_len) == 0) {
   1669 		ifp->if_ierrors++;
   1670 		msk_newbuf(sc_if, cur, m, dmamap);
   1671 		return;
   1672 	}
   1673 
   1674 	/*
   1675 	 * Try to allocate a new jumbo buffer. If that fails, copy the
   1676 	 * packet to mbufs and put the jumbo buffer back in the ring
   1677 	 * so it can be re-used. If allocating mbufs fails, then we
   1678 	 * have to drop the packet.
   1679 	 */
   1680 	if (msk_newbuf(sc_if, cur, NULL, dmamap) == ENOBUFS) {
   1681 		struct mbuf		*m0;
   1682 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
   1683 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
   1684 		msk_newbuf(sc_if, cur, m, dmamap);
   1685 		if (m0 == NULL) {
   1686 			ifp->if_ierrors++;
   1687 			return;
   1688 		}
   1689 		m_adj(m0, ETHER_ALIGN);
   1690 		m = m0;
   1691 	} else {
   1692 		m->m_pkthdr.rcvif = ifp;
   1693 		m->m_pkthdr.len = m->m_len = total_len;
   1694 	}
   1695 
   1696 	ifp->if_ipackets++;
   1697 
   1698 #if NBPFILTER > 0
   1699 	if (ifp->if_bpf)
   1700 		bpf_mtap(ifp->if_bpf, m);
   1701 #endif
   1702 
   1703 	/* pass it on. */
   1704 	(*ifp->if_input)(ifp, m);
   1705 }
   1706 
   1707 void
   1708 msk_txeof(struct sk_if_softc *sc_if, int idx)
   1709 {
   1710 	struct sk_softc		*sc = sc_if->sk_softc;
   1711 	struct msk_tx_desc	*cur_tx;
   1712 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
   1713 	u_int32_t		sk_ctl;
   1714 	struct sk_txmap_entry	*entry;
   1715 	int			cons, prog;
   1716 
   1717 	DPRINTFN(2, ("msk_txeof\n"));
   1718 
   1719 	/*
   1720 	 * Go through our tx ring and free mbufs for those
   1721 	 * frames that have been sent.
   1722 	 */
   1723 	cons = sc_if->sk_cdata.sk_tx_cons;
   1724 	prog = 0;
   1725 	while (cons != idx) {
   1726 		if (sc_if->sk_cdata.sk_tx_cnt <= 0)
   1727 			break;
   1728 		prog++;
   1729 		cur_tx = &sc_if->sk_rdata->sk_tx_ring[cons];
   1730 
   1731 		MSK_CDTXSYNC(sc_if, cons, 1,
   1732 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1733 		sk_ctl = cur_tx->sk_ctl;
   1734 		MSK_CDTXSYNC(sc_if, cons, 1, BUS_DMASYNC_PREREAD);
   1735 #ifdef MSK_DEBUG
   1736 		if (mskdebug >= 2)
   1737 			msk_dump_txdesc(cur_tx, cons);
   1738 #endif
   1739 		if (sk_ctl & SK_Y2_TXCTL_LASTFRAG)
   1740 			ifp->if_opackets++;
   1741 		if (sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf != NULL) {
   1742 			entry = sc_if->sk_cdata.sk_tx_map[cons];
   1743 
   1744 			bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
   1745 			    entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1746 
   1747 			bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
   1748 			SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
   1749 					  link);
   1750 			sc_if->sk_cdata.sk_tx_map[cons] = NULL;
   1751 			m_freem(sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf);
   1752 			sc_if->sk_cdata.sk_tx_chain[cons].sk_mbuf = NULL;
   1753 		}
   1754 		sc_if->sk_cdata.sk_tx_cnt--;
   1755 		SK_INC(cons, MSK_TX_RING_CNT);
   1756 	}
   1757 	ifp->if_timer = sc_if->sk_cdata.sk_tx_cnt > 0 ? 5 : 0;
   1758 
   1759 	if (sc_if->sk_cdata.sk_tx_cnt < MSK_TX_RING_CNT - 2)
   1760 		ifp->if_flags &= ~IFF_OACTIVE;
   1761 
   1762 	if (prog > 0)
   1763 		sc_if->sk_cdata.sk_tx_cons = cons;
   1764 }
   1765 
   1766 void
   1767 msk_tick(void *xsc_if)
   1768 {
   1769 	struct sk_if_softc *sc_if = xsc_if;
   1770 	struct mii_data *mii = &sc_if->sk_mii;
   1771 
   1772 	mii_tick(mii);
   1773 	callout_schedule(&sc_if->sk_tick_ch, hz);
   1774 }
   1775 
   1776 void
   1777 msk_intr_yukon(struct sk_if_softc *sc_if)
   1778 {
   1779 	u_int8_t status;
   1780 
   1781 	status = SK_IF_READ_1(sc_if, 0, SK_GMAC_ISR);
   1782 	/* RX overrun */
   1783 	if ((status & SK_GMAC_INT_RX_OVER) != 0) {
   1784 		SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST,
   1785 		    SK_RFCTL_RX_FIFO_OVER);
   1786 	}
   1787 	/* TX underrun */
   1788 	if ((status & SK_GMAC_INT_TX_UNDER) != 0) {
   1789 		SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST,
   1790 		    SK_TFCTL_TX_FIFO_UNDER);
   1791 	}
   1792 
   1793 	DPRINTFN(2, ("msk_intr_yukon status=%#x\n", status));
   1794 }
   1795 
   1796 int
   1797 msk_intr(void *xsc)
   1798 {
   1799 	struct sk_softc		*sc = xsc;
   1800 	struct sk_if_softc	*sc_if0 = sc->sk_if[SK_PORT_A];
   1801 	struct sk_if_softc	*sc_if1 = sc->sk_if[SK_PORT_B];
   1802 	struct ifnet		*ifp0 = NULL, *ifp1 = NULL;
   1803 	int			claimed = 0;
   1804 	u_int32_t		status;
   1805 	uint32_t		st_status;
   1806 	uint16_t		st_len;
   1807 	uint8_t			st_opcode, st_link;
   1808 	struct msk_status_desc	*cur_st;
   1809 
   1810 	status = CSR_READ_4(sc, SK_Y2_ISSR2);
   1811 	if (status == 0) {
   1812 		CSR_WRITE_4(sc, SK_Y2_ICR, 2);
   1813 		return (0);
   1814 	}
   1815 
   1816 	status = CSR_READ_4(sc, SK_ISR);
   1817 
   1818 	if (sc_if0 != NULL)
   1819 		ifp0 = &sc_if0->sk_ethercom.ec_if;
   1820 	if (sc_if1 != NULL)
   1821 		ifp1 = &sc_if1->sk_ethercom.ec_if;
   1822 
   1823 	if (sc_if0 && (status & SK_Y2_IMR_MAC1) &&
   1824 	    (ifp0->if_flags & IFF_RUNNING)) {
   1825 		msk_intr_yukon(sc_if0);
   1826 	}
   1827 
   1828 	if (sc_if1 && (status & SK_Y2_IMR_MAC2) &&
   1829 	    (ifp1->if_flags & IFF_RUNNING)) {
   1830 		msk_intr_yukon(sc_if1);
   1831 	}
   1832 
   1833 	for (;;) {
   1834 		cur_st = &sc->sk_status_ring[sc->sk_status_idx];
   1835 		MSK_CDSTSYNC(sc, sc->sk_status_idx,
   1836 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1837 		st_opcode = cur_st->sk_opcode;
   1838 		if ((st_opcode & SK_Y2_STOPC_OWN) == 0) {
   1839 			MSK_CDSTSYNC(sc, sc->sk_status_idx,
   1840 			    BUS_DMASYNC_PREREAD);
   1841 			break;
   1842 		}
   1843 		st_status = le32toh(cur_st->sk_status);
   1844 		st_len = le16toh(cur_st->sk_len);
   1845 		st_link = cur_st->sk_link;
   1846 		st_opcode &= ~SK_Y2_STOPC_OWN;
   1847 
   1848 		switch (st_opcode) {
   1849 		case SK_Y2_STOPC_RXSTAT:
   1850 			msk_rxeof(sc->sk_if[st_link], st_len, st_status);
   1851 			SK_IF_WRITE_2(sc->sk_if[st_link], 0,
   1852 			    SK_RXQ1_Y2_PREF_PUTIDX,
   1853 			    sc->sk_if[st_link]->sk_cdata.sk_rx_prod);
   1854 			break;
   1855 		case SK_Y2_STOPC_TXSTAT:
   1856 			if (sc_if0)
   1857 				msk_txeof(sc_if0, st_status
   1858 				    & SK_Y2_ST_TXA1_MSKL);
   1859 			if (sc_if1)
   1860 				msk_txeof(sc_if1,
   1861 				    ((st_status & SK_Y2_ST_TXA2_MSKL)
   1862 					>> SK_Y2_ST_TXA2_SHIFTL)
   1863 				    | ((st_len & SK_Y2_ST_TXA2_MSKH) << SK_Y2_ST_TXA2_SHIFTH));
   1864 			break;
   1865 		default:
   1866 			aprint_error("opcode=0x%x\n", st_opcode);
   1867 			break;
   1868 		}
   1869 		SK_INC(sc->sk_status_idx, MSK_STATUS_RING_CNT);
   1870 	}
   1871 
   1872 #define MSK_STATUS_RING_OWN_CNT(sc)			\
   1873 	(((sc)->sk_status_idx + MSK_STATUS_RING_CNT -	\
   1874 	    (sc)->sk_status_own_idx) % MSK_STATUS_RING_CNT)
   1875 
   1876 	while (MSK_STATUS_RING_OWN_CNT(sc) > MSK_STATUS_RING_CNT / 2) {
   1877 		cur_st = &sc->sk_status_ring[sc->sk_status_own_idx];
   1878 		cur_st->sk_opcode &= ~SK_Y2_STOPC_OWN;
   1879 		MSK_CDSTSYNC(sc, sc->sk_status_own_idx,
   1880 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1881 
   1882 		SK_INC(sc->sk_status_own_idx, MSK_STATUS_RING_CNT);
   1883 	}
   1884 
   1885 	if (status & SK_Y2_IMR_BMU) {
   1886 		CSR_WRITE_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_IRQ_CLEAR);
   1887 		claimed = 1;
   1888 	}
   1889 
   1890 	CSR_WRITE_4(sc, SK_Y2_ICR, 2);
   1891 
   1892 	if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
   1893 		msk_start(ifp0);
   1894 	if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
   1895 		msk_start(ifp1);
   1896 
   1897 #if NRND > 0
   1898 	if (RND_ENABLED(&sc->rnd_source))
   1899 		rnd_add_uint32(&sc->rnd_source, status);
   1900 #endif
   1901 
   1902 	if (sc->sk_int_mod_pending)
   1903 		msk_update_int_mod(sc);
   1904 
   1905 	return claimed;
   1906 }
   1907 
   1908 void
   1909 msk_init_yukon(struct sk_if_softc *sc_if)
   1910 {
   1911 	u_int32_t		v;
   1912 	u_int16_t		reg;
   1913 	struct sk_softc		*sc;
   1914 	int			i;
   1915 
   1916 	sc = sc_if->sk_softc;
   1917 
   1918 	DPRINTFN(2, ("msk_init_yukon: start: sk_csr=%#x\n",
   1919 		     CSR_READ_4(sc_if->sk_softc, SK_CSR)));
   1920 
   1921 	DPRINTFN(6, ("msk_init_yukon: 1\n"));
   1922 
   1923 	/* GMAC and GPHY Reset */
   1924 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
   1925 	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
   1926 	DELAY(1000);
   1927 
   1928 	DPRINTFN(6, ("msk_init_yukon: 2\n"));
   1929 
   1930 	SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_CLEAR);
   1931 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
   1932 		      SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
   1933 
   1934 	DPRINTFN(3, ("msk_init_yukon: gmac_ctrl=%#x\n",
   1935 		     SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
   1936 
   1937 	DPRINTFN(6, ("msk_init_yukon: 3\n"));
   1938 
   1939 	/* unused read of the interrupt source register */
   1940 	DPRINTFN(6, ("msk_init_yukon: 4\n"));
   1941 	SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
   1942 
   1943 	DPRINTFN(6, ("msk_init_yukon: 4a\n"));
   1944 	reg = SK_YU_READ_2(sc_if, YUKON_PAR);
   1945 	DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
   1946 
   1947 	/* MIB Counter Clear Mode set */
   1948         reg |= YU_PAR_MIB_CLR;
   1949 	DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
   1950 	DPRINTFN(6, ("msk_init_yukon: 4b\n"));
   1951 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
   1952 
   1953 	/* MIB Counter Clear Mode clear */
   1954 	DPRINTFN(6, ("msk_init_yukon: 5\n"));
   1955         reg &= ~YU_PAR_MIB_CLR;
   1956 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
   1957 
   1958 	/* receive control reg */
   1959 	DPRINTFN(6, ("msk_init_yukon: 7\n"));
   1960 	SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
   1961 
   1962 	/* transmit control register */
   1963 	SK_YU_WRITE_2(sc_if, YUKON_TCR, (0x04 << 10));
   1964 
   1965 	/* transmit flow control register */
   1966 	SK_YU_WRITE_2(sc_if, YUKON_TFCR, 0xffff);
   1967 
   1968 	/* transmit parameter register */
   1969 	DPRINTFN(6, ("msk_init_yukon: 8\n"));
   1970 	SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
   1971 		      YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1c) | 0x04);
   1972 
   1973 	/* serial mode register */
   1974 	DPRINTFN(6, ("msk_init_yukon: 9\n"));
   1975 	reg = YU_SMR_DATA_BLIND(0x1c) |
   1976 	      YU_SMR_MFL_VLAN |
   1977 	      YU_SMR_IPG_DATA(0x1e);
   1978 
   1979 	if (sc->sk_type != SK_YUKON_FE)
   1980 		reg |= YU_SMR_MFL_JUMBO;
   1981 
   1982 	SK_YU_WRITE_2(sc_if, YUKON_SMR, reg);
   1983 
   1984 	DPRINTFN(6, ("msk_init_yukon: 10\n"));
   1985 	/* Setup Yukon's address */
   1986 	for (i = 0; i < 3; i++) {
   1987 		/* Write Source Address 1 (unicast filter) */
   1988 		SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
   1989 			      sc_if->sk_enaddr[i * 2] |
   1990 			      sc_if->sk_enaddr[i * 2 + 1] << 8);
   1991 	}
   1992 
   1993 	for (i = 0; i < 3; i++) {
   1994 		reg = sk_win_read_2(sc_if->sk_softc,
   1995 				    SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
   1996 		SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
   1997 	}
   1998 
   1999 	/* Set promiscuous mode */
   2000 	msk_setpromisc(sc_if);
   2001 
   2002 	/* Set multicast filter */
   2003 	DPRINTFN(6, ("msk_init_yukon: 11\n"));
   2004 	msk_setmulti(sc_if);
   2005 
   2006 	/* enable interrupt mask for counter overflows */
   2007 	DPRINTFN(6, ("msk_init_yukon: 12\n"));
   2008 	SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
   2009 	SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
   2010 	SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
   2011 
   2012 	/* Configure RX MAC FIFO Flush Mask */
   2013 	v = YU_RXSTAT_FOFL | YU_RXSTAT_CRCERR | YU_RXSTAT_MIIERR |
   2014 	    YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | YU_RXSTAT_RUNT |
   2015 	    YU_RXSTAT_JABBER;
   2016 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_MASK, v);
   2017 
   2018 	/* Configure RX MAC FIFO */
   2019 	SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
   2020 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_OPERATION_ON |
   2021 	    SK_RFCTL_FIFO_FLUSH_ON);
   2022 
   2023 	/* Increase flush threshould to 64 bytes */
   2024 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_THRESHOLD,
   2025 	    SK_RFCTL_FIFO_THRESHOLD + 1);
   2026 
   2027 	/* Configure TX MAC FIFO */
   2028 	SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
   2029 	SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
   2030 
   2031 #if 1
   2032 	SK_YU_WRITE_2(sc_if, YUKON_GPCR, YU_GPCR_TXEN | YU_GPCR_RXEN);
   2033 #endif
   2034 	DPRINTFN(6, ("msk_init_yukon: end\n"));
   2035 }
   2036 
   2037 /*
   2038  * Note that to properly initialize any part of the GEnesis chip,
   2039  * you first have to take it out of reset mode.
   2040  */
   2041 int
   2042 msk_init(struct ifnet *ifp)
   2043 {
   2044 	struct sk_if_softc	*sc_if = ifp->if_softc;
   2045 	struct sk_softc		*sc = sc_if->sk_softc;
   2046 	int			rc = 0, s;
   2047 	uint32_t		imr, imtimer_ticks;
   2048 
   2049 
   2050 	DPRINTFN(2, ("msk_init\n"));
   2051 
   2052 	s = splnet();
   2053 
   2054 	/* Cancel pending I/O and free all RX/TX buffers. */
   2055 	msk_stop(ifp,0);
   2056 
   2057 	/* Configure I2C registers */
   2058 
   2059 	/* Configure XMAC(s) */
   2060 	msk_init_yukon(sc_if);
   2061 	if ((rc = ether_mediachange(ifp)) != 0)
   2062 		goto out;
   2063 
   2064 	/* Configure transmit arbiter(s) */
   2065 	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_ON);
   2066 #if 0
   2067 	    SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON);
   2068 #endif
   2069 
   2070 	/* Configure RAMbuffers */
   2071 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
   2072 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
   2073 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
   2074 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
   2075 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
   2076 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
   2077 
   2078 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_UNRESET);
   2079 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_STORENFWD_ON);
   2080 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_START, sc_if->sk_tx_ramstart);
   2081 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_WR_PTR, sc_if->sk_tx_ramstart);
   2082 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_RD_PTR, sc_if->sk_tx_ramstart);
   2083 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_END, sc_if->sk_tx_ramend);
   2084 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_ON);
   2085 
   2086 	/* Configure BMUs */
   2087 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000016);
   2088 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000d28);
   2089 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000080);
   2090 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_WM, 0x0600);	/* XXX ??? */
   2091 
   2092 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000016);
   2093 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000d28);
   2094 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000080);
   2095 	SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_WM, 0x0600);	/* XXX ??? */
   2096 
   2097 	/* Make sure the sync transmit queue is disabled. */
   2098 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET);
   2099 
   2100 	/* Init descriptors */
   2101 	if (msk_init_rx_ring(sc_if) == ENOBUFS) {
   2102 		aprint_error_dev(&sc_if->sk_dev, "initialization failed: no "
   2103 		    "memory for rx buffers\n");
   2104 		msk_stop(ifp,0);
   2105 		splx(s);
   2106 		return ENOBUFS;
   2107 	}
   2108 
   2109 	if (msk_init_tx_ring(sc_if) == ENOBUFS) {
   2110 		aprint_error_dev(&sc_if->sk_dev, "initialization failed: no "
   2111 		    "memory for tx buffers\n");
   2112 		msk_stop(ifp,0);
   2113 		splx(s);
   2114 		return ENOBUFS;
   2115 	}
   2116 
   2117 	/* Set interrupt moderation if changed via sysctl. */
   2118 	switch (sc->sk_type) {
   2119 	case SK_YUKON_EC:
   2120 	case SK_YUKON_EC_U:
   2121 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
   2122 		break;
   2123 	case SK_YUKON_FE:
   2124 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
   2125 		break;
   2126 	case SK_YUKON_XL:
   2127 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
   2128 		break;
   2129 	default:
   2130 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
   2131 	}
   2132 	imr = sk_win_read_4(sc, SK_IMTIMERINIT);
   2133 	if (imr != SK_IM_USECS(sc->sk_int_mod)) {
   2134 		sk_win_write_4(sc, SK_IMTIMERINIT,
   2135 		    SK_IM_USECS(sc->sk_int_mod));
   2136 		aprint_verbose_dev(&sc->sk_dev, "interrupt moderation is %d us\n",
   2137 		    sc->sk_int_mod);
   2138 	}
   2139 
   2140 	/* Initialize prefetch engine. */
   2141 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
   2142 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000002);
   2143 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_LIDX, MSK_RX_RING_CNT - 1);
   2144 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRLO,
   2145 	    MSK_RX_RING_ADDR(sc_if, 0));
   2146 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRHI,
   2147 	    (u_int64_t)MSK_RX_RING_ADDR(sc_if, 0) >> 32);
   2148 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000008);
   2149 	SK_IF_READ_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR);
   2150 
   2151 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
   2152 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000002);
   2153 	SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_LIDX, MSK_TX_RING_CNT - 1);
   2154 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRLO,
   2155 	    MSK_TX_RING_ADDR(sc_if, 0));
   2156 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRHI,
   2157 	    (u_int64_t)MSK_TX_RING_ADDR(sc_if, 0) >> 32);
   2158 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000008);
   2159 	SK_IF_READ_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR);
   2160 
   2161 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_PUTIDX,
   2162 	    sc_if->sk_cdata.sk_rx_prod);
   2163 
   2164 	/* Configure interrupt handling */
   2165 	if (sc_if->sk_port == SK_PORT_A)
   2166 		sc->sk_intrmask |= SK_Y2_INTRS1;
   2167 	else
   2168 		sc->sk_intrmask |= SK_Y2_INTRS2;
   2169 	sc->sk_intrmask |= SK_Y2_IMR_BMU;
   2170 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
   2171 
   2172 	ifp->if_flags |= IFF_RUNNING;
   2173 	ifp->if_flags &= ~IFF_OACTIVE;
   2174 
   2175 	callout_schedule(&sc_if->sk_tick_ch, hz);
   2176 
   2177 out:
   2178 	splx(s);
   2179 	return rc;
   2180 }
   2181 
   2182 void
   2183 msk_stop(struct ifnet *ifp, int disable)
   2184 {
   2185 	struct sk_if_softc	*sc_if = ifp->if_softc;
   2186 	struct sk_softc		*sc = sc_if->sk_softc;
   2187 	struct sk_txmap_entry	*dma;
   2188 	int			i;
   2189 
   2190 	DPRINTFN(2, ("msk_stop\n"));
   2191 
   2192 	callout_stop(&sc_if->sk_tick_ch);
   2193 
   2194 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
   2195 
   2196 	/* Stop transfer of Tx descriptors */
   2197 
   2198 	/* Stop transfer of Rx descriptors */
   2199 
   2200 	/* Turn off various components of this interface. */
   2201 	SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC);
   2202 	SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
   2203 	SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
   2204 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
   2205 	SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
   2206 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, SK_TXBMU_OFFLINE);
   2207 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF);
   2208 	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
   2209 	SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
   2210 	SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_TXLEDCTL_COUNTER_STOP);
   2211 	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
   2212 	SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
   2213 
   2214 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
   2215 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
   2216 
   2217 	/* Disable interrupts */
   2218 	if (sc_if->sk_port == SK_PORT_A)
   2219 		sc->sk_intrmask &= ~SK_Y2_INTRS1;
   2220 	else
   2221 		sc->sk_intrmask &= ~SK_Y2_INTRS2;
   2222 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
   2223 
   2224 	SK_XM_READ_2(sc_if, XM_ISR);
   2225 	SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF);
   2226 
   2227 	/* Free RX and TX mbufs still in the queues. */
   2228 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
   2229 		if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
   2230 			m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
   2231 			sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
   2232 		}
   2233 	}
   2234 
   2235 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
   2236 		if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
   2237 			m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
   2238 			sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
   2239 #if 1
   2240 			SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head,
   2241 			    sc_if->sk_cdata.sk_tx_map[i], link);
   2242 			sc_if->sk_cdata.sk_tx_map[i] = 0;
   2243 #endif
   2244 		}
   2245 	}
   2246 
   2247 #if 1
   2248 	while ((dma = SIMPLEQ_FIRST(&sc_if->sk_txmap_head))) {
   2249 		SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
   2250 		bus_dmamap_destroy(sc->sc_dmatag, dma->dmamap);
   2251 		free(dma, M_DEVBUF);
   2252 	}
   2253 #endif
   2254 }
   2255 
   2256 CFATTACH_DECL(mskc, sizeof(struct sk_softc), mskc_probe, mskc_attach,
   2257 	NULL, NULL);
   2258 
   2259 CFATTACH_DECL(msk, sizeof(struct sk_if_softc), msk_probe, msk_attach,
   2260 	NULL, NULL);
   2261 
   2262 #ifdef MSK_DEBUG
   2263 void
   2264 msk_dump_txdesc(struct msk_tx_desc *le, int idx)
   2265 {
   2266 #define DESC_PRINT(X)					\
   2267 	if (X)					\
   2268 		printf("txdesc[%d]." #X "=%#x\n",	\
   2269 		       idx, X);
   2270 
   2271 	DESC_PRINT(letoh32(le->sk_addr));
   2272 	DESC_PRINT(letoh16(le->sk_len));
   2273 	DESC_PRINT(le->sk_ctl);
   2274 	DESC_PRINT(le->sk_opcode);
   2275 #undef DESC_PRINT
   2276 }
   2277 
   2278 void
   2279 msk_dump_bytes(const char *data, int len)
   2280 {
   2281 	int c, i, j;
   2282 
   2283 	for (i = 0; i < len; i += 16) {
   2284 		printf("%08x  ", i);
   2285 		c = len - i;
   2286 		if (c > 16) c = 16;
   2287 
   2288 		for (j = 0; j < c; j++) {
   2289 			printf("%02x ", data[i + j] & 0xff);
   2290 			if ((j & 0xf) == 7 && j > 0)
   2291 				printf(" ");
   2292 		}
   2293 
   2294 		for (; j < 16; j++)
   2295 			printf("   ");
   2296 		printf("  ");
   2297 
   2298 		for (j = 0; j < c; j++) {
   2299 			int ch = data[i + j] & 0xff;
   2300 			printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
   2301 		}
   2302 
   2303 		printf("\n");
   2304 
   2305 		if (c < 16)
   2306 			break;
   2307 	}
   2308 }
   2309 
   2310 void
   2311 msk_dump_mbuf(struct mbuf *m)
   2312 {
   2313 	int count = m->m_pkthdr.len;
   2314 
   2315 	printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
   2316 
   2317 	while (count > 0 && m) {
   2318 		printf("m=%p, m->m_data=%p, m->m_len=%d\n",
   2319 		       m, m->m_data, m->m_len);
   2320 		msk_dump_bytes(mtod(m, char *), m->m_len);
   2321 
   2322 		count -= m->m_len;
   2323 		m = m->m_next;
   2324 	}
   2325 }
   2326 #endif
   2327 
   2328 static int
   2329 msk_sysctl_handler(SYSCTLFN_ARGS)
   2330 {
   2331 	int error, t;
   2332 	struct sysctlnode node;
   2333 	struct sk_softc *sc;
   2334 
   2335 	node = *rnode;
   2336 	sc = node.sysctl_data;
   2337 	t = sc->sk_int_mod;
   2338 	node.sysctl_data = &t;
   2339 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2340 	if (error || newp == NULL)
   2341 		return error;
   2342 
   2343 	if (t < SK_IM_MIN || t > SK_IM_MAX)
   2344 		return EINVAL;
   2345 
   2346 	/* update the softc with sysctl-changed value, and mark
   2347 	   for hardware update */
   2348 	sc->sk_int_mod = t;
   2349 	sc->sk_int_mod_pending = 1;
   2350 	return 0;
   2351 }
   2352 
   2353 /*
   2354  * Set up sysctl(3) MIB, hw.sk.* - Individual controllers will be
   2355  * set up in skc_attach()
   2356  */
   2357 SYSCTL_SETUP(sysctl_msk, "sysctl msk subtree setup")
   2358 {
   2359 	int rc;
   2360 	const struct sysctlnode *node;
   2361 
   2362 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
   2363 	    0, CTLTYPE_NODE, "hw", NULL,
   2364 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
   2365 		goto err;
   2366 	}
   2367 
   2368 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   2369 	    0, CTLTYPE_NODE, "msk",
   2370 	    SYSCTL_DESCR("msk interface controls"),
   2371 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   2372 		goto err;
   2373 	}
   2374 
   2375 	msk_root_num = node->sysctl_num;
   2376 	return;
   2377 
   2378 err:
   2379 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
   2380 }
   2381