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