Home | History | Annotate | Line # | Download | only in pci
if_msk.c revision 1.67.2.4
      1  1.67.2.2    martin /* $NetBSD: if_msk.c,v 1.67.2.4 2020/04/21 18:42:16 martin Exp $ */
      2  1.67.2.1  christos /*	$OpenBSD: if_msk.c,v 1.79 2009/10/15 17:54:56 deraadt 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.10       dsl #include <sys/cdefs.h>
     55  1.67.2.2    martin __KERNEL_RCSID(0, "$NetBSD: if_msk.c,v 1.67.2.4 2020/04/21 18:42:16 martin Exp $");
     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.21      cube #include <sys/mutex.h>
     63       1.1       riz #include <sys/kernel.h>
     64       1.1       riz #include <sys/socket.h>
     65       1.1       riz #include <sys/device.h>
     66       1.1       riz #include <sys/queue.h>
     67       1.1       riz #include <sys/callout.h>
     68       1.1       riz #include <sys/sysctl.h>
     69       1.1       riz #include <sys/endian.h>
     70       1.1       riz #ifdef __NetBSD__
     71       1.1       riz  #define letoh16 htole16
     72       1.1       riz  #define letoh32 htole32
     73       1.1       riz #endif
     74       1.1       riz 
     75       1.1       riz #include <net/if.h>
     76       1.1       riz #include <net/if_dl.h>
     77       1.1       riz #include <net/if_types.h>
     78       1.1       riz 
     79       1.1       riz #include <net/if_media.h>
     80       1.1       riz 
     81       1.1       riz #include <net/bpf.h>
     82      1.48  riastrad #include <sys/rndsource.h>
     83       1.1       riz 
     84       1.1       riz #include <dev/mii/mii.h>
     85       1.1       riz #include <dev/mii/miivar.h>
     86       1.1       riz #include <dev/mii/brgphyreg.h>
     87       1.1       riz 
     88       1.1       riz #include <dev/pci/pcireg.h>
     89       1.1       riz #include <dev/pci/pcivar.h>
     90       1.1       riz #include <dev/pci/pcidevs.h>
     91       1.1       riz 
     92       1.1       riz #include <dev/pci/if_skreg.h>
     93       1.1       riz #include <dev/pci/if_mskvar.h>
     94       1.1       riz 
     95  1.67.2.2    martin static int mskc_probe(device_t, cfdata_t, void *);
     96  1.67.2.2    martin static void mskc_attach(device_t, device_t, void *);
     97  1.67.2.2    martin static int mskc_detach(device_t, int);
     98  1.67.2.2    martin static void mskc_reset(struct sk_softc *);
     99      1.33    dyoung static bool mskc_suspend(device_t, const pmf_qual_t *);
    100      1.33    dyoung static bool mskc_resume(device_t, const pmf_qual_t *);
    101  1.67.2.2    martin static int msk_probe(device_t, cfdata_t, void *);
    102  1.67.2.2    martin static void msk_attach(device_t, device_t, void *);
    103  1.67.2.2    martin static int msk_detach(device_t, int);
    104  1.67.2.2    martin static void msk_reset(struct sk_if_softc *);
    105  1.67.2.2    martin static int mskcprint(void *, const char *);
    106  1.67.2.2    martin static int msk_intr(void *);
    107  1.67.2.2    martin static void msk_intr_yukon(struct sk_if_softc *);
    108  1.67.2.2    martin static void msk_rxeof(struct sk_if_softc *, uint16_t, uint32_t);
    109  1.67.2.2    martin static void msk_txeof(struct sk_if_softc *);
    110  1.67.2.2    martin static int msk_encap(struct sk_if_softc *, struct mbuf *, uint32_t *);
    111  1.67.2.2    martin static void msk_start(struct ifnet *);
    112  1.67.2.2    martin static int msk_ioctl(struct ifnet *, u_long, void *);
    113  1.67.2.2    martin static int msk_init(struct ifnet *);
    114  1.67.2.2    martin static void msk_init_yukon(struct sk_if_softc *);
    115  1.67.2.2    martin static void msk_stop(struct ifnet *, int);
    116  1.67.2.2    martin static void msk_watchdog(struct ifnet *);
    117  1.67.2.2    martin static int msk_newbuf(struct sk_if_softc *, bus_dmamap_t);
    118  1.67.2.2    martin static int msk_alloc_jumbo_mem(struct sk_if_softc *);
    119  1.67.2.2    martin static void *msk_jalloc(struct sk_if_softc *);
    120  1.67.2.2    martin static void msk_jfree(struct mbuf *, void *, size_t, void *);
    121  1.67.2.2    martin static int msk_init_rx_ring(struct sk_if_softc *);
    122  1.67.2.2    martin static int msk_init_tx_ring(struct sk_if_softc *);
    123  1.67.2.2    martin static void msk_fill_rx_ring(struct sk_if_softc *);
    124  1.67.2.2    martin 
    125  1.67.2.2    martin static void msk_update_int_mod(struct sk_softc *, int);
    126  1.67.2.2    martin 
    127  1.67.2.2    martin static int msk_miibus_readreg(device_t, int, int, uint16_t *);
    128  1.67.2.2    martin static int msk_miibus_writereg(device_t, int, int, uint16_t);
    129  1.67.2.2    martin static void msk_miibus_statchg(struct ifnet *);
    130  1.67.2.2    martin 
    131  1.67.2.2    martin static void msk_setmulti(struct sk_if_softc *);
    132  1.67.2.2    martin static void msk_setpromisc(struct sk_if_softc *);
    133  1.67.2.2    martin static void msk_tick(void *);
    134  1.67.2.1  christos static void msk_fill_rx_tick(void *);
    135       1.1       riz 
    136       1.1       riz /* #define MSK_DEBUG 1 */
    137       1.1       riz #ifdef MSK_DEBUG
    138       1.1       riz #define DPRINTF(x)	if (mskdebug) printf x
    139  1.67.2.1  christos #define DPRINTFN(n, x)	if (mskdebug >= (n)) printf x
    140       1.1       riz int	mskdebug = MSK_DEBUG;
    141       1.1       riz 
    142  1.67.2.2    martin static void msk_dump_txdesc(struct msk_tx_desc *, int);
    143  1.67.2.2    martin static void msk_dump_mbuf(struct mbuf *);
    144  1.67.2.2    martin static void msk_dump_bytes(const char *, int);
    145       1.1       riz #else
    146       1.1       riz #define DPRINTF(x)
    147  1.67.2.1  christos #define DPRINTFN(n, x)
    148       1.1       riz #endif
    149       1.1       riz 
    150       1.1       riz static int msk_sysctl_handler(SYSCTLFN_PROTO);
    151       1.1       riz static int msk_root_num;
    152       1.1       riz 
    153  1.67.2.1  christos #define MSK_ADDR_LO(x)	((uint64_t) (x) & 0xffffffffUL)
    154  1.67.2.1  christos #define MSK_ADDR_HI(x)	((uint64_t) (x) >> 32)
    155  1.67.2.1  christos 
    156       1.1       riz /* supported device vendors */
    157       1.1       riz static const struct msk_product {
    158  1.67.2.1  christos 	pci_vendor_id_t		msk_vendor;
    159  1.67.2.1  christos 	pci_product_id_t	msk_product;
    160       1.1       riz } msk_products[] = {
    161       1.5   msaitoh 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE550SX },
    162      1.60  jdolecek 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE550T_B1 },
    163       1.5   msaitoh 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE560SX },
    164       1.5   msaitoh 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DGE560T },
    165      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8021CU },
    166      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8021X },
    167      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8022CU },
    168      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8022X },
    169       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8035 },
    170       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8036 },
    171       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8038 },
    172       1.5   msaitoh 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8039 },
    173      1.47  christos 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8040 },
    174      1.60  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8040T },
    175      1.60  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8042 },
    176      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8048 },
    177       1.5   msaitoh 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8050 },
    178       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8052 },
    179       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8053 },
    180       1.5   msaitoh 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8055 },
    181      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8055_2 },
    182       1.5   msaitoh 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8056 },
    183      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8057 },
    184      1.55  christos 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8058 },
    185      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8059 },
    186       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8061CU },
    187       1.5   msaitoh 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8061X },
    188       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8062CU },
    189       1.1       riz 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKONII_8062X },
    190      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8070 },
    191      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8071 },
    192      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8072 },
    193      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8075 },
    194      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_8079 },
    195      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C032 },
    196      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C033 },
    197      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C034 },
    198      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C036 },
    199      1.56  jdolecek 	{ PCI_VENDOR_MARVELL,		PCI_PRODUCT_MARVELL_YUKON_C042 },
    200       1.1       riz 	{ PCI_VENDOR_SCHNEIDERKOCH,	PCI_PRODUCT_SCHNEIDERKOCH_SK_9SXX },
    201  1.67.2.1  christos 	{ PCI_VENDOR_SCHNEIDERKOCH,	PCI_PRODUCT_SCHNEIDERKOCH_SK_9E21 },
    202  1.67.2.1  christos 	{ 0,				0 }
    203       1.1       riz };
    204       1.1       riz 
    205  1.67.2.1  christos static inline uint32_t
    206  1.67.2.1  christos sk_win_read_4(struct sk_softc *sc, uint32_t reg)
    207       1.1       riz {
    208       1.1       riz 	return CSR_READ_4(sc, reg);
    209       1.1       riz }
    210       1.1       riz 
    211  1.67.2.1  christos static inline uint16_t
    212  1.67.2.1  christos sk_win_read_2(struct sk_softc *sc, uint32_t reg)
    213       1.1       riz {
    214       1.1       riz 	return CSR_READ_2(sc, reg);
    215       1.1       riz }
    216       1.1       riz 
    217  1.67.2.1  christos static inline uint8_t
    218  1.67.2.1  christos sk_win_read_1(struct sk_softc *sc, uint32_t reg)
    219       1.1       riz {
    220       1.1       riz 	return CSR_READ_1(sc, reg);
    221       1.1       riz }
    222       1.1       riz 
    223       1.1       riz static inline void
    224  1.67.2.1  christos sk_win_write_4(struct sk_softc *sc, uint32_t reg, uint32_t x)
    225       1.1       riz {
    226       1.1       riz 	CSR_WRITE_4(sc, reg, x);
    227       1.1       riz }
    228       1.1       riz 
    229       1.1       riz static inline void
    230  1.67.2.1  christos sk_win_write_2(struct sk_softc *sc, uint32_t reg, uint16_t x)
    231       1.1       riz {
    232       1.1       riz 	CSR_WRITE_2(sc, reg, x);
    233       1.1       riz }
    234       1.1       riz 
    235       1.1       riz static inline void
    236  1.67.2.1  christos sk_win_write_1(struct sk_softc *sc, uint32_t reg, uint8_t x)
    237       1.1       riz {
    238       1.1       riz 	CSR_WRITE_1(sc, reg, x);
    239       1.1       riz }
    240       1.1       riz 
    241  1.67.2.2    martin static int
    242  1.67.2.1  christos msk_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    243       1.1       riz {
    244      1.27    cegger 	struct sk_if_softc *sc_if = device_private(dev);
    245  1.67.2.1  christos 	uint16_t data;
    246       1.1       riz 	int i;
    247       1.1       riz 
    248      1.59  jdolecek 	SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
    249       1.1       riz 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ);
    250      1.65   msaitoh 
    251       1.1       riz 	for (i = 0; i < SK_TIMEOUT; i++) {
    252       1.1       riz 		DELAY(1);
    253  1.67.2.1  christos 		data = SK_YU_READ_2(sc_if, YUKON_SMICR);
    254  1.67.2.1  christos 		if (data & YU_SMICR_READ_VALID)
    255       1.1       riz 			break;
    256       1.1       riz 	}
    257       1.1       riz 
    258       1.1       riz 	if (i == SK_TIMEOUT) {
    259      1.30  christos 		aprint_error_dev(sc_if->sk_dev, "phy failed to come ready\n");
    260  1.67.2.1  christos 		return ETIMEDOUT;
    261       1.1       riz 	}
    262      1.65   msaitoh 
    263  1.67.2.1  christos 	DPRINTFN(9, ("msk_miibus_readreg: i=%d, timeout=%d\n", i, SK_TIMEOUT));
    264       1.1       riz 
    265  1.67.2.1  christos 	*val = SK_YU_READ_2(sc_if, YUKON_SMIDR);
    266       1.1       riz 
    267  1.67.2.1  christos 	DPRINTFN(9, ("msk_miibus_readreg phy=%d, reg=%#x, val=%#hx\n",
    268  1.67.2.1  christos 		phy, reg, *val));
    269       1.1       riz 
    270  1.67.2.1  christos 	return 0;
    271       1.1       riz }
    272       1.1       riz 
    273  1.67.2.2    martin static int
    274  1.67.2.1  christos msk_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    275       1.1       riz {
    276      1.27    cegger 	struct sk_if_softc *sc_if = device_private(dev);
    277       1.1       riz 	int i;
    278       1.1       riz 
    279  1.67.2.1  christos 	DPRINTFN(9, ("msk_miibus_writereg phy=%d reg=%#x val=%#hx\n",
    280       1.1       riz 		     phy, reg, val));
    281       1.1       riz 
    282       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val);
    283       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) |
    284       1.1       riz 		      YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE);
    285       1.1       riz 
    286       1.1       riz 	for (i = 0; i < SK_TIMEOUT; i++) {
    287       1.1       riz 		DELAY(1);
    288       1.4   msaitoh 		if (!(SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY))
    289       1.1       riz 			break;
    290       1.1       riz 	}
    291       1.1       riz 
    292  1.67.2.1  christos 	if (i == SK_TIMEOUT) {
    293      1.30  christos 		aprint_error_dev(sc_if->sk_dev, "phy write timed out\n");
    294  1.67.2.1  christos 		return ETIMEDOUT;
    295  1.67.2.1  christos 	}
    296  1.67.2.1  christos 
    297  1.67.2.1  christos 	return 0;
    298       1.1       riz }
    299       1.1       riz 
    300  1.67.2.2    martin static void
    301      1.41      matt msk_miibus_statchg(struct ifnet *ifp)
    302       1.1       riz {
    303      1.41      matt 	struct sk_if_softc *sc_if = ifp->if_softc;
    304       1.5   msaitoh 	struct mii_data *mii = &sc_if->sk_mii;
    305       1.5   msaitoh 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    306       1.5   msaitoh 	int gpcr;
    307       1.5   msaitoh 
    308       1.5   msaitoh 	gpcr = SK_YU_READ_2(sc_if, YUKON_GPCR);
    309       1.5   msaitoh 	gpcr &= (YU_GPCR_TXEN | YU_GPCR_RXEN);
    310       1.5   msaitoh 
    311      1.60  jdolecek 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO ||
    312      1.60  jdolecek 	    sc_if->sk_softc->sk_type == SK_YUKON_FE_P) {
    313       1.5   msaitoh 		/* Set speed. */
    314       1.5   msaitoh 		gpcr |= YU_GPCR_SPEED_DIS;
    315       1.5   msaitoh 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    316       1.5   msaitoh 		case IFM_1000_SX:
    317       1.5   msaitoh 		case IFM_1000_LX:
    318       1.5   msaitoh 		case IFM_1000_CX:
    319       1.5   msaitoh 		case IFM_1000_T:
    320       1.5   msaitoh 			gpcr |= (YU_GPCR_GIG | YU_GPCR_SPEED);
    321       1.5   msaitoh 			break;
    322       1.5   msaitoh 		case IFM_100_TX:
    323       1.5   msaitoh 			gpcr |= YU_GPCR_SPEED;
    324       1.5   msaitoh 			break;
    325       1.5   msaitoh 		}
    326       1.5   msaitoh 
    327       1.5   msaitoh 		/* Set duplex. */
    328       1.5   msaitoh 		gpcr |= YU_GPCR_DPLX_DIS;
    329  1.67.2.1  christos 		if ((mii->mii_media_active & IFM_FDX) != 0)
    330       1.5   msaitoh 			gpcr |= YU_GPCR_DUPLEX;
    331       1.5   msaitoh 
    332       1.5   msaitoh 		/* Disable flow control. */
    333       1.5   msaitoh 		gpcr |= YU_GPCR_FCTL_DIS;
    334       1.5   msaitoh 		gpcr |= (YU_GPCR_FCTL_TX_DIS | YU_GPCR_FCTL_RX_DIS);
    335       1.5   msaitoh 	}
    336       1.5   msaitoh 
    337       1.5   msaitoh 	SK_YU_WRITE_2(sc_if, YUKON_GPCR, gpcr);
    338       1.5   msaitoh 
    339       1.5   msaitoh 	DPRINTFN(9, ("msk_miibus_statchg: gpcr=%x\n",
    340      1.41      matt 		     SK_YU_READ_2(sc_if, YUKON_GPCR)));
    341       1.1       riz }
    342       1.1       riz 
    343  1.67.2.2    martin static void
    344       1.1       riz msk_setmulti(struct sk_if_softc *sc_if)
    345       1.1       riz {
    346       1.1       riz 	struct ifnet *ifp= &sc_if->sk_ethercom.ec_if;
    347  1.67.2.1  christos 	uint32_t hashes[2] = { 0, 0 };
    348       1.1       riz 	int h;
    349       1.1       riz 	struct ethercom *ec = &sc_if->sk_ethercom;
    350       1.1       riz 	struct ether_multi *enm;
    351       1.1       riz 	struct ether_multistep step;
    352  1.67.2.1  christos 	uint16_t reg;
    353       1.1       riz 
    354       1.1       riz 	/* First, zot all the existing filters. */
    355       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0);
    356       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0);
    357       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0);
    358       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0);
    359       1.1       riz 
    360       1.1       riz 
    361       1.1       riz 	/* Now program new ones. */
    362       1.6   msaitoh 	reg = SK_YU_READ_2(sc_if, YUKON_RCR);
    363       1.6   msaitoh 	reg |= YU_RCR_UFLEN;
    364       1.1       riz allmulti:
    365       1.1       riz 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    366       1.6   msaitoh 		if ((ifp->if_flags & IFF_PROMISC) != 0)
    367       1.6   msaitoh 			reg &= ~(YU_RCR_UFLEN | YU_RCR_MUFLEN);
    368       1.6   msaitoh 		else if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
    369       1.6   msaitoh 			hashes[0] = 0xFFFFFFFF;
    370       1.6   msaitoh 			hashes[1] = 0xFFFFFFFF;
    371       1.6   msaitoh 		}
    372       1.1       riz 	} else {
    373       1.1       riz 		/* First find the tail of the list. */
    374  1.67.2.1  christos 		ETHER_LOCK(ec);
    375       1.1       riz 		ETHER_FIRST_MULTI(step, ec, enm);
    376       1.1       riz 		while (enm != NULL) {
    377      1.23    cegger 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    378       1.1       riz 				 ETHER_ADDR_LEN)) {
    379       1.1       riz 				ifp->if_flags |= IFF_ALLMULTI;
    380  1.67.2.1  christos 				ETHER_UNLOCK(ec);
    381       1.1       riz 				goto allmulti;
    382       1.1       riz 			}
    383       1.5   msaitoh 			h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) &
    384      1.61  jdolecek 			    ((1 << SK_HASH_BITS) - 1);
    385       1.1       riz 			if (h < 32)
    386       1.1       riz 				hashes[0] |= (1 << h);
    387       1.1       riz 			else
    388       1.1       riz 				hashes[1] |= (1 << (h - 32));
    389       1.1       riz 
    390       1.1       riz 			ETHER_NEXT_MULTI(step, enm);
    391       1.1       riz 		}
    392  1.67.2.1  christos 		ETHER_UNLOCK(ec);
    393       1.6   msaitoh 		reg |= YU_RCR_MUFLEN;
    394       1.1       riz 	}
    395       1.1       riz 
    396       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff);
    397       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff);
    398       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff);
    399       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff);
    400       1.6   msaitoh 	SK_YU_WRITE_2(sc_if, YUKON_RCR, reg);
    401       1.1       riz }
    402       1.1       riz 
    403  1.67.2.2    martin static void
    404       1.1       riz msk_setpromisc(struct sk_if_softc *sc_if)
    405       1.1       riz {
    406       1.1       riz 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
    407       1.1       riz 
    408       1.1       riz 	if (ifp->if_flags & IFF_PROMISC)
    409       1.1       riz 		SK_YU_CLRBIT_2(sc_if, YUKON_RCR,
    410       1.1       riz 		    YU_RCR_UFLEN | YU_RCR_MUFLEN);
    411       1.1       riz 	else
    412       1.1       riz 		SK_YU_SETBIT_2(sc_if, YUKON_RCR,
    413       1.1       riz 		    YU_RCR_UFLEN | YU_RCR_MUFLEN);
    414       1.1       riz }
    415       1.1       riz 
    416  1.67.2.2    martin static int
    417       1.1       riz msk_init_rx_ring(struct sk_if_softc *sc_if)
    418       1.1       riz {
    419       1.1       riz 	struct msk_chain_data	*cd = &sc_if->sk_cdata;
    420       1.1       riz 	struct msk_ring_data	*rd = sc_if->sk_rdata;
    421  1.67.2.1  christos 	struct msk_rx_desc	*r;
    422       1.1       riz 	int			i, nexti;
    423       1.1       riz 
    424      1.30  christos 	memset(rd->sk_rx_ring, 0, sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT);
    425       1.1       riz 
    426       1.1       riz 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
    427       1.1       riz 		cd->sk_rx_chain[i].sk_le = &rd->sk_rx_ring[i];
    428       1.1       riz 		if (i == (MSK_RX_RING_CNT - 1))
    429       1.1       riz 			nexti = 0;
    430       1.1       riz 		else
    431       1.1       riz 			nexti = i + 1;
    432       1.1       riz 		cd->sk_rx_chain[i].sk_next = &cd->sk_rx_chain[nexti];
    433       1.1       riz 	}
    434       1.1       riz 
    435  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_prod = 0;
    436       1.1       riz 	sc_if->sk_cdata.sk_rx_cons = 0;
    437  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cnt = 0;
    438  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_hiaddr = 0;
    439       1.1       riz 
    440  1.67.2.1  christos 	/* Mark the first ring element to initialize the high address. */
    441  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_hiaddr = 0;
    442  1.67.2.1  christos 	r = &rd->sk_rx_ring[cd->sk_rx_prod];
    443  1.67.2.1  christos 	r->sk_addr = htole32(cd->sk_rx_hiaddr);
    444  1.67.2.1  christos 	r->sk_len = 0;
    445  1.67.2.1  christos 	r->sk_ctl = 0;
    446  1.67.2.1  christos 	r->sk_opcode = SK_Y2_BMUOPC_ADDR64 | SK_Y2_RXOPC_OWN;
    447  1.67.2.1  christos 	MSK_CDRXSYNC(sc_if, cd->sk_rx_prod,
    448  1.67.2.1  christos 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    449  1.67.2.1  christos 	SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
    450  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cnt++;
    451  1.67.2.1  christos 
    452  1.67.2.1  christos 	msk_fill_rx_ring(sc_if);
    453  1.67.2.1  christos 	return 0;
    454       1.1       riz }
    455       1.1       riz 
    456  1.67.2.2    martin static int
    457       1.1       riz msk_init_tx_ring(struct sk_if_softc *sc_if)
    458       1.1       riz {
    459       1.1       riz 	struct msk_chain_data	*cd = &sc_if->sk_cdata;
    460       1.1       riz 	struct msk_ring_data	*rd = sc_if->sk_rdata;
    461  1.67.2.1  christos 	struct msk_tx_desc	*t;
    462       1.1       riz 	int			i, nexti;
    463       1.1       riz 
    464      1.66   msaitoh 	memset(rd->sk_tx_ring, 0, sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT);
    465       1.1       riz 
    466       1.1       riz 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
    467       1.1       riz 		cd->sk_tx_chain[i].sk_le = &rd->sk_tx_ring[i];
    468       1.1       riz 		if (i == (MSK_TX_RING_CNT - 1))
    469       1.1       riz 			nexti = 0;
    470       1.1       riz 		else
    471       1.1       riz 			nexti = i + 1;
    472       1.1       riz 		cd->sk_tx_chain[i].sk_next = &cd->sk_tx_chain[nexti];
    473       1.1       riz 	}
    474       1.1       riz 
    475       1.1       riz 	sc_if->sk_cdata.sk_tx_prod = 0;
    476       1.1       riz 	sc_if->sk_cdata.sk_tx_cons = 0;
    477       1.1       riz 	sc_if->sk_cdata.sk_tx_cnt = 0;
    478  1.67.2.1  christos 	sc_if->sk_cdata.sk_tx_hiaddr = 0;
    479       1.1       riz 
    480  1.67.2.1  christos 	/* Mark the first ring element to initialize the high address. */
    481  1.67.2.1  christos 	sc_if->sk_cdata.sk_tx_hiaddr = 0;
    482  1.67.2.1  christos 	t = &rd->sk_tx_ring[cd->sk_tx_prod];
    483  1.67.2.1  christos 	t->sk_addr = htole32(cd->sk_tx_hiaddr);
    484  1.67.2.1  christos 	t->sk_len = 0;
    485  1.67.2.1  christos 	t->sk_ctl = 0;
    486  1.67.2.1  christos 	t->sk_opcode = SK_Y2_BMUOPC_ADDR64 | SK_Y2_TXOPC_OWN;
    487       1.1       riz 	MSK_CDTXSYNC(sc_if, 0, MSK_TX_RING_CNT,
    488  1.67.2.1  christos 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    489  1.67.2.1  christos 	SK_INC(sc_if->sk_cdata.sk_tx_prod, MSK_TX_RING_CNT);
    490  1.67.2.1  christos 	sc_if->sk_cdata.sk_tx_cnt++;
    491       1.1       riz 
    492  1.67.2.1  christos 	return 0;
    493       1.1       riz }
    494       1.1       riz 
    495  1.67.2.2    martin static int
    496  1.67.2.1  christos msk_newbuf(struct sk_if_softc *sc_if, bus_dmamap_t dmamap)
    497       1.1       riz {
    498       1.1       riz 	struct mbuf		*m_new = NULL;
    499       1.1       riz 	struct sk_chain		*c;
    500       1.1       riz 	struct msk_rx_desc	*r;
    501  1.67.2.1  christos 	void			*buf = NULL;
    502  1.67.2.1  christos 	bus_addr_t		addr;
    503       1.1       riz 
    504  1.67.2.1  christos 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    505  1.67.2.1  christos 	if (m_new == NULL)
    506  1.67.2.1  christos 		return ENOBUFS;
    507       1.1       riz 
    508  1.67.2.1  christos 	/* Allocate the jumbo buffer */
    509  1.67.2.1  christos 	buf = msk_jalloc(sc_if);
    510  1.67.2.1  christos 	if (buf == NULL) {
    511  1.67.2.1  christos 		m_freem(m_new);
    512  1.67.2.1  christos 		DPRINTFN(1, ("%s jumbo allocation failed -- packet "
    513  1.67.2.1  christos 		    "dropped!\n", sc_if->sk_ethercom.ec_if.if_xname));
    514  1.67.2.1  christos 		return ENOBUFS;
    515       1.1       riz 	}
    516  1.67.2.1  christos 
    517  1.67.2.1  christos 	/* Attach the buffer to the mbuf */
    518  1.67.2.1  christos 	m_new->m_len = m_new->m_pkthdr.len = SK_JLEN;
    519  1.67.2.1  christos 	MEXTADD(m_new, buf, SK_JLEN, 0, msk_jfree, sc_if);
    520  1.67.2.1  christos 
    521       1.1       riz 	m_adj(m_new, ETHER_ALIGN);
    522       1.1       riz 
    523  1.67.2.1  christos 	addr = dmamap->dm_segs[0].ds_addr +
    524  1.67.2.1  christos 		  ((vaddr_t)m_new->m_data -
    525  1.67.2.1  christos 		   (vaddr_t)sc_if->sk_cdata.sk_jumbo_buf);
    526  1.67.2.1  christos 
    527  1.67.2.1  christos 	if (sc_if->sk_cdata.sk_rx_hiaddr != MSK_ADDR_HI(addr)) {
    528  1.67.2.1  christos 		c = &sc_if->sk_cdata.sk_rx_chain[sc_if->sk_cdata.sk_rx_prod];
    529  1.67.2.1  christos 		r = c->sk_le;
    530  1.67.2.1  christos 		c->sk_mbuf = NULL;
    531  1.67.2.1  christos 		r->sk_addr = htole32(MSK_ADDR_HI(addr));
    532  1.67.2.1  christos 		r->sk_len = 0;
    533  1.67.2.1  christos 		r->sk_ctl = 0;
    534  1.67.2.1  christos 		r->sk_opcode = SK_Y2_BMUOPC_ADDR64 | SK_Y2_RXOPC_OWN;
    535  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_hiaddr = MSK_ADDR_HI(addr);
    536  1.67.2.1  christos 
    537  1.67.2.1  christos 		MSK_CDRXSYNC(sc_if, sc_if->sk_cdata.sk_rx_prod,
    538  1.67.2.1  christos 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    539  1.67.2.1  christos 
    540  1.67.2.1  christos 		SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
    541  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_cnt++;
    542  1.67.2.1  christos 
    543  1.67.2.1  christos 		DPRINTFN(10, ("%s: rx ADDR64: %#x\n",
    544  1.67.2.1  christos 		    sc_if->sk_ethercom.ec_if.if_xname,
    545  1.67.2.1  christos 			(unsigned)MSK_ADDR_HI(addr)));
    546  1.67.2.1  christos 	}
    547  1.67.2.1  christos 
    548  1.67.2.1  christos 	c = &sc_if->sk_cdata.sk_rx_chain[sc_if->sk_cdata.sk_rx_prod];
    549       1.1       riz 	r = c->sk_le;
    550       1.1       riz 	c->sk_mbuf = m_new;
    551  1.67.2.1  christos 	r->sk_addr = htole32(MSK_ADDR_LO(addr));
    552       1.1       riz 	r->sk_len = htole16(SK_JLEN);
    553       1.1       riz 	r->sk_ctl = 0;
    554       1.1       riz 	r->sk_opcode = SK_Y2_RXOPC_PACKET | SK_Y2_RXOPC_OWN;
    555       1.1       riz 
    556  1.67.2.1  christos 	MSK_CDRXSYNC(sc_if, sc_if->sk_cdata.sk_rx_prod,
    557  1.67.2.1  christos 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    558  1.67.2.1  christos 
    559  1.67.2.1  christos 	SK_INC(sc_if->sk_cdata.sk_rx_prod, MSK_RX_RING_CNT);
    560  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cnt++;
    561       1.1       riz 
    562  1.67.2.1  christos 	return 0;
    563       1.1       riz }
    564       1.1       riz 
    565       1.1       riz /*
    566       1.1       riz  * Memory management for jumbo frames.
    567       1.1       riz  */
    568       1.1       riz 
    569  1.67.2.2    martin static int
    570       1.1       riz msk_alloc_jumbo_mem(struct sk_if_softc *sc_if)
    571       1.1       riz {
    572       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
    573       1.8  christos 	char *ptr, *kva;
    574  1.67.2.1  christos 	int		i, state, error;
    575  1.67.2.1  christos 	struct sk_jpool_entry	*entry;
    576       1.1       riz 
    577       1.1       riz 	state = error = 0;
    578       1.1       riz 
    579       1.1       riz 	/* Grab a big chunk o' storage. */
    580       1.1       riz 	if (bus_dmamem_alloc(sc->sc_dmatag, MSK_JMEM, PAGE_SIZE, 0,
    581  1.67.2.1  christos 	     &sc_if->sk_cdata.sk_jumbo_seg, 1, &sc_if->sk_cdata.sk_jumbo_nseg,
    582  1.67.2.1  christos 	     BUS_DMA_NOWAIT)) {
    583       1.1       riz 		aprint_error(": can't alloc rx buffers");
    584  1.67.2.1  christos 		return ENOBUFS;
    585       1.1       riz 	}
    586       1.1       riz 
    587       1.1       riz 	state = 1;
    588  1.67.2.1  christos 	if (bus_dmamem_map(sc->sc_dmatag, &sc_if->sk_cdata.sk_jumbo_seg,
    589  1.67.2.1  christos 	    sc_if->sk_cdata.sk_jumbo_nseg, MSK_JMEM, (void **)&kva,
    590  1.67.2.1  christos 	    BUS_DMA_NOWAIT)) {
    591       1.1       riz 		aprint_error(": can't map dma buffers (%d bytes)", MSK_JMEM);
    592       1.1       riz 		error = ENOBUFS;
    593       1.1       riz 		goto out;
    594       1.1       riz 	}
    595       1.1       riz 
    596       1.1       riz 	state = 2;
    597       1.1       riz 	if (bus_dmamap_create(sc->sc_dmatag, MSK_JMEM, 1, MSK_JMEM, 0,
    598       1.1       riz 	    BUS_DMA_NOWAIT, &sc_if->sk_cdata.sk_rx_jumbo_map)) {
    599       1.1       riz 		aprint_error(": can't create dma map");
    600       1.1       riz 		error = ENOBUFS;
    601       1.1       riz 		goto out;
    602       1.1       riz 	}
    603       1.1       riz 
    604       1.1       riz 	state = 3;
    605       1.1       riz 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map,
    606       1.1       riz 			    kva, MSK_JMEM, NULL, BUS_DMA_NOWAIT)) {
    607       1.1       riz 		aprint_error(": can't load dma map");
    608       1.1       riz 		error = ENOBUFS;
    609       1.1       riz 		goto out;
    610       1.1       riz 	}
    611       1.1       riz 
    612       1.1       riz 	state = 4;
    613       1.8  christos 	sc_if->sk_cdata.sk_jumbo_buf = (void *)kva;
    614  1.67.2.1  christos 	DPRINTFN(1,("msk_jumbo_buf = %p\n",
    615  1.67.2.1  christos 		(void *)sc_if->sk_cdata.sk_jumbo_buf));
    616       1.1       riz 
    617       1.1       riz 	LIST_INIT(&sc_if->sk_jfree_listhead);
    618       1.1       riz 	LIST_INIT(&sc_if->sk_jinuse_listhead);
    619      1.21      cube 	mutex_init(&sc_if->sk_jpool_mtx, MUTEX_DEFAULT, IPL_NET);
    620       1.1       riz 
    621       1.1       riz 	/*
    622       1.1       riz 	 * Now divide it up into 9K pieces and save the addresses
    623       1.1       riz 	 * in an array.
    624       1.1       riz 	 */
    625       1.1       riz 	ptr = sc_if->sk_cdata.sk_jumbo_buf;
    626       1.1       riz 	for (i = 0; i < MSK_JSLOTS; i++) {
    627       1.1       riz 		sc_if->sk_cdata.sk_jslots[i] = ptr;
    628       1.1       riz 		ptr += SK_JLEN;
    629       1.1       riz 		entry = malloc(sizeof(struct sk_jpool_entry),
    630  1.67.2.3    martin 		    M_DEVBUF, M_WAITOK);
    631       1.1       riz 		entry->slot = i;
    632       1.5   msaitoh 		LIST_INSERT_HEAD(&sc_if->sk_jfree_listhead,
    633       1.1       riz 				 entry, jpool_entries);
    634       1.1       riz 	}
    635       1.1       riz out:
    636       1.1       riz 	if (error != 0) {
    637       1.1       riz 		switch (state) {
    638       1.1       riz 		case 4:
    639       1.1       riz 			bus_dmamap_unload(sc->sc_dmatag,
    640       1.1       riz 			    sc_if->sk_cdata.sk_rx_jumbo_map);
    641  1.67.2.1  christos 			/* FALLTHROUGH */
    642       1.1       riz 		case 3:
    643       1.1       riz 			bus_dmamap_destroy(sc->sc_dmatag,
    644       1.1       riz 			    sc_if->sk_cdata.sk_rx_jumbo_map);
    645  1.67.2.1  christos 			/* FALLTHROUGH */
    646       1.1       riz 		case 2:
    647       1.1       riz 			bus_dmamem_unmap(sc->sc_dmatag, kva, MSK_JMEM);
    648  1.67.2.1  christos 			/* FALLTHROUGH */
    649       1.1       riz 		case 1:
    650  1.67.2.1  christos 			bus_dmamem_free(sc->sc_dmatag,
    651  1.67.2.1  christos 			    &sc_if->sk_cdata.sk_jumbo_seg,
    652  1.67.2.1  christos 			    sc_if->sk_cdata.sk_jumbo_nseg);
    653       1.1       riz 			break;
    654       1.1       riz 		default:
    655       1.1       riz 			break;
    656       1.1       riz 		}
    657       1.1       riz 	}
    658       1.1       riz 
    659      1.52  christos 	return error;
    660       1.1       riz }
    661       1.1       riz 
    662  1.67.2.1  christos static void
    663  1.67.2.1  christos msk_free_jumbo_mem(struct sk_if_softc *sc_if)
    664  1.67.2.1  christos {
    665  1.67.2.1  christos 	struct sk_softc		*sc = sc_if->sk_softc;
    666  1.67.2.1  christos 
    667  1.67.2.1  christos 	bus_dmamap_unload(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map);
    668  1.67.2.1  christos 	bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_cdata.sk_rx_jumbo_map);
    669  1.67.2.1  christos 	bus_dmamem_unmap(sc->sc_dmatag, sc_if->sk_cdata.sk_jumbo_buf, MSK_JMEM);
    670  1.67.2.1  christos 	bus_dmamem_free(sc->sc_dmatag, &sc_if->sk_cdata.sk_jumbo_seg,
    671  1.67.2.1  christos 	    sc_if->sk_cdata.sk_jumbo_nseg);
    672  1.67.2.1  christos }
    673  1.67.2.1  christos 
    674       1.1       riz /*
    675       1.1       riz  * Allocate a jumbo buffer.
    676       1.1       riz  */
    677  1.67.2.2    martin static void *
    678       1.1       riz msk_jalloc(struct sk_if_softc *sc_if)
    679       1.1       riz {
    680  1.67.2.1  christos 	struct sk_jpool_entry	*entry;
    681       1.1       riz 
    682      1.21      cube 	mutex_enter(&sc_if->sk_jpool_mtx);
    683       1.1       riz 	entry = LIST_FIRST(&sc_if->sk_jfree_listhead);
    684       1.1       riz 
    685      1.21      cube 	if (entry == NULL) {
    686      1.21      cube 		mutex_exit(&sc_if->sk_jpool_mtx);
    687      1.21      cube 		return NULL;
    688      1.21      cube 	}
    689       1.1       riz 
    690       1.1       riz 	LIST_REMOVE(entry, jpool_entries);
    691       1.1       riz 	LIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries);
    692      1.21      cube 	mutex_exit(&sc_if->sk_jpool_mtx);
    693  1.67.2.1  christos 	return sc_if->sk_cdata.sk_jslots[entry->slot];
    694       1.1       riz }
    695       1.1       riz 
    696       1.1       riz /*
    697       1.1       riz  * Release a jumbo buffer.
    698       1.1       riz  */
    699  1.67.2.2    martin static void
    700       1.8  christos msk_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
    701       1.1       riz {
    702       1.1       riz 	struct sk_jpool_entry *entry;
    703       1.1       riz 	struct sk_if_softc *sc;
    704      1.21      cube 	int i;
    705       1.1       riz 
    706       1.1       riz 	/* Extract the softc struct pointer. */
    707       1.1       riz 	sc = (struct sk_if_softc *)arg;
    708       1.1       riz 
    709       1.1       riz 	if (sc == NULL)
    710       1.1       riz 		panic("msk_jfree: can't find softc pointer!");
    711       1.1       riz 
    712       1.1       riz 	/* calculate the slot this buffer belongs to */
    713       1.1       riz 	i = ((vaddr_t)buf
    714       1.1       riz 	     - (vaddr_t)sc->sk_cdata.sk_jumbo_buf) / SK_JLEN;
    715       1.1       riz 
    716       1.1       riz 	if ((i < 0) || (i >= MSK_JSLOTS))
    717       1.6   msaitoh 		panic("msk_jfree: asked to free buffer that we don't manage!");
    718       1.1       riz 
    719      1.21      cube 	mutex_enter(&sc->sk_jpool_mtx);
    720       1.1       riz 	entry = LIST_FIRST(&sc->sk_jinuse_listhead);
    721       1.1       riz 	if (entry == NULL)
    722       1.1       riz 		panic("msk_jfree: buffer not in use!");
    723       1.1       riz 	entry->slot = i;
    724       1.1       riz 	LIST_REMOVE(entry, jpool_entries);
    725       1.1       riz 	LIST_INSERT_HEAD(&sc->sk_jfree_listhead, entry, jpool_entries);
    726      1.21      cube 	mutex_exit(&sc->sk_jpool_mtx);
    727       1.1       riz 
    728       1.1       riz 	if (__predict_true(m != NULL))
    729      1.12        ad 		pool_cache_put(mb_cache, m);
    730  1.67.2.1  christos 
    731  1.67.2.1  christos 	/* Now that we know we have a free RX buffer, refill if running out */
    732  1.67.2.1  christos 	if ((sc->sk_ethercom.ec_if.if_flags & IFF_RUNNING) != 0
    733  1.67.2.1  christos 	    && sc->sk_cdata.sk_rx_cnt < (MSK_RX_RING_CNT/3))
    734  1.67.2.1  christos 		callout_schedule(&sc->sk_tick_rx, 0);
    735       1.1       riz }
    736       1.1       riz 
    737  1.67.2.2    martin static int
    738      1.19    dyoung msk_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    739       1.1       riz {
    740      1.52  christos 	struct sk_if_softc *sc = ifp->if_softc;
    741      1.52  christos 	int s, error;
    742       1.1       riz 
    743       1.1       riz 	s = splnet();
    744       1.1       riz 
    745  1.67.2.1  christos 	DPRINTFN(2, ("msk_ioctl ETHER cmd %lx\n", cmd));
    746      1.52  christos 	switch (cmd) {
    747      1.52  christos 	case SIOCSIFFLAGS:
    748      1.52  christos 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    749      1.52  christos 			break;
    750       1.1       riz 
    751      1.52  christos 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
    752      1.52  christos 		case IFF_RUNNING:
    753      1.52  christos 			msk_stop(ifp, 1);
    754      1.52  christos 			break;
    755      1.52  christos 		case IFF_UP:
    756      1.52  christos 			msk_init(ifp);
    757      1.52  christos 			break;
    758      1.52  christos 		case IFF_UP | IFF_RUNNING:
    759      1.52  christos 			if ((ifp->if_flags ^ sc->sk_if_flags) == IFF_PROMISC) {
    760      1.52  christos 				msk_setpromisc(sc);
    761      1.52  christos 				msk_setmulti(sc);
    762      1.52  christos 			} else
    763      1.52  christos 				msk_init(ifp);
    764      1.52  christos 			break;
    765       1.1       riz 		}
    766      1.52  christos 		sc->sk_if_flags = ifp->if_flags;
    767      1.52  christos 		break;
    768      1.52  christos 	default:
    769      1.52  christos 		error = ether_ioctl(ifp, cmd, data);
    770      1.52  christos 		if (error == ENETRESET) {
    771      1.52  christos 			error = 0;
    772      1.52  christos 			if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    773      1.52  christos 				;
    774      1.52  christos 			else if (ifp->if_flags & IFF_RUNNING) {
    775      1.52  christos 				/*
    776      1.52  christos 				 * Multicast list has changed; set the hardware
    777      1.52  christos 				 * filter accordingly.
    778      1.52  christos 				 */
    779      1.52  christos 				msk_setmulti(sc);
    780      1.52  christos 			}
    781      1.52  christos 		}
    782      1.52  christos 		break;
    783       1.1       riz 	}
    784       1.1       riz 
    785       1.1       riz 	splx(s);
    786      1.52  christos 	return error;
    787       1.1       riz }
    788       1.1       riz 
    789  1.67.2.2    martin static void
    790      1.30  christos msk_update_int_mod(struct sk_softc *sc, int verbose)
    791       1.1       riz {
    792  1.67.2.1  christos 	uint32_t imtimer_ticks;
    793       1.1       riz 
    794       1.1       riz 	/*
    795       1.1       riz  	 * Configure interrupt moderation. The moderation timer
    796       1.1       riz 	 * defers interrupts specified in the interrupt moderation
    797       1.1       riz 	 * timer mask based on the timeout specified in the interrupt
    798       1.1       riz 	 * moderation timer init register. Each bit in the timer
    799       1.1       riz 	 * register represents one tick, so to specify a timeout in
    800       1.1       riz 	 * microseconds, we have to multiply by the correct number of
    801       1.1       riz 	 * ticks-per-microsecond.
    802       1.1       riz 	 */
    803       1.1       riz 	switch (sc->sk_type) {
    804       1.1       riz 	case SK_YUKON_EC:
    805       1.6   msaitoh 	case SK_YUKON_EC_U:
    806      1.56  jdolecek 	case SK_YUKON_EX:
    807      1.56  jdolecek 	case SK_YUKON_SUPR:
    808      1.56  jdolecek 	case SK_YUKON_ULTRA2:
    809      1.56  jdolecek 	case SK_YUKON_OPTIMA:
    810      1.56  jdolecek 	case SK_YUKON_PRM:
    811      1.56  jdolecek 	case SK_YUKON_OPTIMA2:
    812       1.5   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
    813       1.1       riz 		break;
    814       1.6   msaitoh 	case SK_YUKON_FE:
    815       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
    816       1.6   msaitoh 		break;
    817  1.67.2.1  christos 	case SK_YUKON_FE_P:
    818  1.67.2.1  christos 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE_P;
    819  1.67.2.1  christos 		break;
    820       1.6   msaitoh 	case SK_YUKON_XL:
    821       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
    822       1.6   msaitoh 		break;
    823       1.1       riz 	default:
    824       1.5   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
    825       1.1       riz 	}
    826      1.30  christos 	if (verbose)
    827      1.30  christos 		aprint_verbose_dev(sc->sk_dev,
    828      1.30  christos 		    "interrupt moderation is %d us\n", sc->sk_int_mod);
    829      1.59  jdolecek 	sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod));
    830  1.67.2.1  christos 	sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF | SK_ISR_TX2_S_EOF |
    831  1.67.2.1  christos 	    SK_ISR_RX1_EOF | SK_ISR_RX2_EOF);
    832      1.59  jdolecek 	sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START);
    833       1.1       riz 	sc->sk_int_mod_pending = 0;
    834       1.1       riz }
    835       1.1       riz 
    836       1.1       riz static int
    837       1.1       riz msk_lookup(const struct pci_attach_args *pa)
    838       1.1       riz {
    839       1.1       riz 	const struct msk_product *pmsk;
    840       1.1       riz 
    841       1.1       riz 	for ( pmsk = &msk_products[0]; pmsk->msk_vendor != 0; pmsk++) {
    842       1.1       riz 		if (PCI_VENDOR(pa->pa_id) == pmsk->msk_vendor &&
    843       1.1       riz 		    PCI_PRODUCT(pa->pa_id) == pmsk->msk_product)
    844       1.1       riz 			return 1;
    845       1.1       riz 	}
    846       1.1       riz 	return 0;
    847       1.1       riz }
    848       1.1       riz 
    849       1.1       riz /*
    850       1.1       riz  * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device
    851       1.1       riz  * IDs against our list and return a device name if we find a match.
    852       1.1       riz  */
    853  1.67.2.2    martin static int
    854      1.26    cegger mskc_probe(device_t parent, cfdata_t match, void *aux)
    855       1.1       riz {
    856       1.1       riz 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    857       1.1       riz 
    858       1.1       riz 	return msk_lookup(pa);
    859       1.1       riz }
    860       1.1       riz 
    861       1.1       riz /*
    862       1.1       riz  * Force the GEnesis into reset, then bring it out of reset.
    863       1.1       riz  */
    864  1.67.2.2    martin static void
    865      1.63  jdolecek mskc_reset(struct sk_softc *sc)
    866       1.1       riz {
    867  1.67.2.1  christos 	uint32_t imtimer_ticks, reg1;
    868  1.67.2.3    martin 	uint16_t status;
    869       1.1       riz 	int reg;
    870       1.1       riz 
    871      1.63  jdolecek 	DPRINTFN(2, ("mskc_reset\n"));
    872       1.1       riz 
    873  1.67.2.3    martin 	/* Disable ASF */
    874  1.67.2.3    martin 	if ((sc->sk_type == SK_YUKON_EX) || (sc->sk_type == SK_YUKON_SUPR)) {
    875  1.67.2.3    martin 		CSR_WRITE_4(sc, SK_Y2_CPU_WDOG, 0);
    876  1.67.2.3    martin 		status = CSR_READ_2(sc, SK_Y2_ASF_HCU_CCSR);
    877  1.67.2.3    martin 		/* Clear AHB bridge & microcontroller reset. */
    878  1.67.2.3    martin 		status &= ~(SK_Y2_ASF_HCU_CSSR_ARB_RST |
    879  1.67.2.3    martin 		    SK_Y2_ASF_HCU_CSSR_CPU_RST_MODE);
    880  1.67.2.3    martin 		/* Clear ASF microcontroller state. */
    881  1.67.2.3    martin 		status &= ~SK_Y2_ASF_HCU_CSSR_UC_STATE_MSK;
    882  1.67.2.3    martin 		status &= ~SK_Y2_ASF_HCU_CSSR_CPU_CLK_DIVIDE_MSK;
    883  1.67.2.3    martin 		CSR_WRITE_2(sc, SK_Y2_ASF_HCU_CCSR, status);
    884  1.67.2.3    martin 		CSR_WRITE_4(sc, SK_Y2_CPU_WDOG, 0);
    885  1.67.2.3    martin 	} else
    886  1.67.2.3    martin 		CSR_WRITE_1(sc, SK_Y2_ASF_CSR, SK_Y2_ASF_RESET);
    887  1.67.2.3    martin 	CSR_WRITE_2(sc, SK_CSR, SK_CSR_ASF_OFF);
    888  1.67.2.3    martin 
    889       1.1       riz 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_RESET);
    890       1.1       riz 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_RESET);
    891       1.1       riz 
    892       1.1       riz 	DELAY(1000);
    893       1.1       riz 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_SW_UNRESET);
    894       1.1       riz 	DELAY(2);
    895       1.1       riz 	CSR_WRITE_1(sc, SK_CSR, SK_CSR_MASTER_UNRESET);
    896       1.5   msaitoh 	sk_win_write_1(sc, SK_TESTCTL1, 2);
    897       1.5   msaitoh 
    898      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_EC_U || sc->sk_type == SK_YUKON_EX ||
    899      1.56  jdolecek 	    sc->sk_type >= SK_YUKON_FE_P) {
    900      1.22     chris 		uint32_t our;
    901      1.22     chris 
    902      1.22     chris 		CSR_WRITE_2(sc, SK_CSR, SK_CSR_WOL_ON);
    903      1.65   msaitoh 
    904      1.22     chris 		/* enable all clocks. */
    905      1.22     chris 		sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG3), 0);
    906      1.22     chris 		our = sk_win_read_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG4));
    907  1.67.2.1  christos 		our &= (SK_Y2_REG4_FORCE_ASPM_REQUEST |
    908  1.67.2.1  christos 			SK_Y2_REG4_ASPM_GPHY_LINK_DOWN |
    909  1.67.2.1  christos 			SK_Y2_REG4_ASPM_INT_FIFO_EMPTY |
    910      1.22     chris 			SK_Y2_REG4_ASPM_CLKRUN_REQUEST);
    911      1.43  christos 		/* Set all bits to 0 except bits 15..12 */
    912      1.22     chris 		sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG4), our);
    913      1.22     chris 		/* Set to default value */
    914      1.22     chris 		sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG5), 0);
    915  1.67.2.1  christos 
    916  1.67.2.1  christos 		/*
    917  1.67.2.1  christos 		 * Disable status race, workaround for Yukon EC Ultra &
    918  1.67.2.1  christos 		 * Yukon EX.
    919  1.67.2.1  christos 		 */
    920  1.67.2.1  christos 		reg1 = sk_win_read_4(sc, SK_GPIO);
    921  1.67.2.1  christos 		reg1 |= SK_Y2_GPIO_STAT_RACE_DIS;
    922  1.67.2.1  christos 		sk_win_write_4(sc, SK_GPIO, reg1);
    923  1.67.2.1  christos 		sk_win_read_4(sc, SK_GPIO);
    924      1.22     chris 	}
    925      1.22     chris 
    926      1.22     chris 	/* release PHY from PowerDown/Coma mode. */
    927      1.60  jdolecek 	reg1 = sk_win_read_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1));
    928      1.60  jdolecek 	if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
    929      1.60  jdolecek 		reg1 |= (SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
    930      1.60  jdolecek 	else
    931      1.60  jdolecek 		reg1 &= ~(SK_Y2_REG1_PHY1_COMA | SK_Y2_REG1_PHY2_COMA);
    932       1.5   msaitoh 	sk_win_write_4(sc, SK_Y2_PCI_REG(SK_PCI_OURREG1), reg1);
    933      1.43  christos 
    934       1.5   msaitoh 	if (sc->sk_type == SK_YUKON_XL && sc->sk_rev > SK_YUKON_XL_REV_A1)
    935       1.5   msaitoh 		sk_win_write_1(sc, SK_Y2_CLKGATE,
    936       1.5   msaitoh 		    SK_Y2_CLKGATE_LINK1_GATE_DIS |
    937       1.5   msaitoh 		    SK_Y2_CLKGATE_LINK2_GATE_DIS |
    938       1.5   msaitoh 		    SK_Y2_CLKGATE_LINK1_CORE_DIS |
    939       1.5   msaitoh 		    SK_Y2_CLKGATE_LINK2_CORE_DIS |
    940       1.5   msaitoh 		    SK_Y2_CLKGATE_LINK1_PCI_DIS | SK_Y2_CLKGATE_LINK2_PCI_DIS);
    941       1.5   msaitoh 	else
    942       1.5   msaitoh 		sk_win_write_1(sc, SK_Y2_CLKGATE, 0);
    943      1.43  christos 
    944       1.5   msaitoh 	CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET);
    945       1.5   msaitoh 	CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_SET);
    946       1.5   msaitoh 	DELAY(1000);
    947       1.1       riz 	CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR);
    948       1.5   msaitoh 	CSR_WRITE_2(sc, SK_LINK_CTRL + SK_WIN_LEN, SK_LINK_RESET_CLEAR);
    949       1.5   msaitoh 
    950      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_EX || sc->sk_type == SK_YUKON_SUPR) {
    951      1.56  jdolecek 		CSR_WRITE_2(sc, SK_GMAC_CTRL, SK_GMAC_BYP_MACSECRX |
    952      1.56  jdolecek 		    SK_GMAC_BYP_MACSECTX | SK_GMAC_BYP_RETR_FIFO);
    953      1.59  jdolecek 	}
    954      1.56  jdolecek 
    955       1.5   msaitoh 	sk_win_write_1(sc, SK_TESTCTL1, 1);
    956       1.1       riz 
    957      1.63  jdolecek 	DPRINTFN(2, ("mskc_reset: sk_csr=%x\n", CSR_READ_1(sc, SK_CSR)));
    958      1.63  jdolecek 	DPRINTFN(2, ("mskc_reset: sk_link_ctrl=%x\n",
    959       1.1       riz 		     CSR_READ_2(sc, SK_LINK_CTRL)));
    960       1.1       riz 
    961       1.1       riz 	/* Clear I2C IRQ noise */
    962       1.1       riz 	CSR_WRITE_4(sc, SK_I2CHWIRQ, 1);
    963       1.1       riz 
    964       1.1       riz 	/* Disable hardware timer */
    965       1.1       riz 	CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_STOP);
    966       1.1       riz 	CSR_WRITE_1(sc, SK_TIMERCTL, SK_IMCTL_IRQ_CLEAR);
    967       1.1       riz 
    968       1.1       riz 	/* Disable descriptor polling */
    969       1.1       riz 	CSR_WRITE_4(sc, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_STOP);
    970       1.1       riz 
    971       1.1       riz 	/* Disable time stamps */
    972       1.1       riz 	CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_STOP);
    973       1.1       riz 	CSR_WRITE_1(sc, SK_TSTAMP_CTL, SK_TSTAMP_IRQ_CLEAR);
    974       1.1       riz 
    975       1.1       riz 	/* Enable RAM interface */
    976       1.1       riz 	sk_win_write_1(sc, SK_RAMCTL, SK_RAMCTL_UNRESET);
    977       1.1       riz 	for (reg = SK_TO0;reg <= SK_TO11; reg++)
    978       1.1       riz 		sk_win_write_1(sc, reg, 36);
    979       1.5   msaitoh 	sk_win_write_1(sc, SK_RAMCTL + (SK_WIN_LEN / 2), SK_RAMCTL_UNRESET);
    980       1.5   msaitoh 	for (reg = SK_TO0;reg <= SK_TO11; reg++)
    981       1.5   msaitoh 		sk_win_write_1(sc, reg + (SK_WIN_LEN / 2), 36);
    982       1.1       riz 
    983       1.1       riz 	/*
    984       1.1       riz 	 * Configure interrupt moderation. The moderation timer
    985       1.1       riz 	 * defers interrupts specified in the interrupt moderation
    986       1.1       riz 	 * timer mask based on the timeout specified in the interrupt
    987       1.1       riz 	 * moderation timer init register. Each bit in the timer
    988       1.1       riz 	 * register represents one tick, so to specify a timeout in
    989       1.1       riz 	 * microseconds, we have to multiply by the correct number of
    990       1.1       riz 	 * ticks-per-microsecond.
    991       1.1       riz 	 */
    992       1.1       riz 	switch (sc->sk_type) {
    993       1.1       riz 	case SK_YUKON_EC:
    994       1.6   msaitoh 	case SK_YUKON_EC_U:
    995      1.60  jdolecek 	case SK_YUKON_EX:
    996      1.60  jdolecek 	case SK_YUKON_SUPR:
    997      1.60  jdolecek 	case SK_YUKON_ULTRA2:
    998      1.60  jdolecek 	case SK_YUKON_OPTIMA:
    999      1.60  jdolecek 	case SK_YUKON_PRM:
   1000      1.60  jdolecek 	case SK_YUKON_OPTIMA2:
   1001       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
   1002       1.6   msaitoh 		break;
   1003       1.6   msaitoh 	case SK_YUKON_FE:
   1004       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
   1005       1.6   msaitoh 		break;
   1006      1.60  jdolecek 	case SK_YUKON_FE_P:
   1007      1.60  jdolecek 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE_P;
   1008      1.60  jdolecek 		break;
   1009       1.1       riz 	case SK_YUKON_XL:
   1010       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
   1011       1.1       riz 		break;
   1012       1.1       riz 	default:
   1013       1.5   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
   1014      1.60  jdolecek 		break;
   1015       1.1       riz 	}
   1016       1.1       riz 
   1017       1.1       riz 	/* Reset status ring. */
   1018      1.30  christos 	memset(sc->sk_status_ring, 0,
   1019       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1020      1.17  kiyohara 	bus_dmamap_sync(sc->sc_dmatag, sc->sk_status_map, 0,
   1021      1.17  kiyohara 	    sc->sk_status_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1022       1.1       riz 	sc->sk_status_idx = 0;
   1023       1.1       riz 
   1024       1.1       riz 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_RESET);
   1025       1.1       riz 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_UNRESET);
   1026       1.1       riz 
   1027       1.1       riz 	sk_win_write_2(sc, SK_STAT_BMU_LIDX, MSK_STATUS_RING_CNT - 1);
   1028       1.1       riz 	sk_win_write_4(sc, SK_STAT_BMU_ADDRLO,
   1029  1.67.2.1  christos 	    MSK_ADDR_LO(sc->sk_status_map->dm_segs[0].ds_addr));
   1030       1.1       riz 	sk_win_write_4(sc, SK_STAT_BMU_ADDRHI,
   1031  1.67.2.1  christos 	    MSK_ADDR_HI(sc->sk_status_map->dm_segs[0].ds_addr));
   1032  1.67.2.1  christos 	if (sc->sk_type == SK_YUKON_EC &&
   1033  1.67.2.1  christos 	    sc->sk_rev == SK_YUKON_EC_REV_A1) {
   1034  1.67.2.1  christos 		/* WA for dev. #4.3 */
   1035  1.67.2.1  christos 		sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH,
   1036  1.67.2.1  christos 		    SK_STAT_BMU_TXTHIDX_MSK);
   1037  1.67.2.1  christos 		/* WA for dev. #4.18 */
   1038       1.6   msaitoh 		sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x21);
   1039       1.6   msaitoh 		sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 0x07);
   1040       1.6   msaitoh 	} else {
   1041       1.6   msaitoh 		sk_win_write_2(sc, SK_STAT_BMU_TX_THRESH, 0x000a);
   1042       1.6   msaitoh 		sk_win_write_1(sc, SK_STAT_BMU_FIFOWM, 0x10);
   1043  1.67.2.1  christos 		if (sc->sk_type == SK_YUKON_XL)
   1044  1.67.2.1  christos 			sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 0x04);
   1045  1.67.2.1  christos 		else
   1046  1.67.2.1  christos 			sk_win_write_1(sc, SK_STAT_BMU_FIFOIWM, 0x10);
   1047       1.6   msaitoh 		sk_win_write_4(sc, SK_Y2_ISR_ITIMERINIT, 0x0190); /* 3.2us on Yukon-EC */
   1048       1.6   msaitoh 	}
   1049       1.1       riz 
   1050       1.1       riz #if 0
   1051       1.1       riz 	sk_win_write_4(sc, SK_Y2_LEV_ITIMERINIT, SK_IM_USECS(100));
   1052       1.6   msaitoh #endif
   1053       1.1       riz 	sk_win_write_4(sc, SK_Y2_TX_ITIMERINIT, SK_IM_USECS(1000));
   1054       1.1       riz 
   1055  1.67.2.1  christos 	/* Enable status unit. */
   1056       1.1       riz 	sk_win_write_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_ON);
   1057       1.1       riz 
   1058       1.1       riz 	sk_win_write_1(sc, SK_Y2_LEV_ITIMERCTL, SK_IMCTL_START);
   1059       1.1       riz 	sk_win_write_1(sc, SK_Y2_TX_ITIMERCTL, SK_IMCTL_START);
   1060       1.1       riz 	sk_win_write_1(sc, SK_Y2_ISR_ITIMERCTL, SK_IMCTL_START);
   1061       1.1       riz 
   1062      1.30  christos 	msk_update_int_mod(sc, 0);
   1063       1.1       riz }
   1064       1.1       riz 
   1065  1.67.2.2    martin static int
   1066      1.26    cegger msk_probe(device_t parent, cfdata_t match, void *aux)
   1067       1.1       riz {
   1068       1.1       riz 	struct skc_attach_args *sa = aux;
   1069       1.1       riz 
   1070       1.1       riz 	if (sa->skc_port != SK_PORT_A && sa->skc_port != SK_PORT_B)
   1071  1.67.2.1  christos 		return 0;
   1072       1.1       riz 
   1073       1.1       riz 	switch (sa->skc_type) {
   1074       1.1       riz 	case SK_YUKON_XL:
   1075       1.1       riz 	case SK_YUKON_EC_U:
   1076      1.56  jdolecek 	case SK_YUKON_EX:
   1077       1.1       riz 	case SK_YUKON_EC:
   1078       1.1       riz 	case SK_YUKON_FE:
   1079      1.47  christos 	case SK_YUKON_FE_P:
   1080      1.56  jdolecek 	case SK_YUKON_SUPR:
   1081      1.56  jdolecek 	case SK_YUKON_ULTRA2:
   1082      1.56  jdolecek 	case SK_YUKON_OPTIMA:
   1083      1.56  jdolecek 	case SK_YUKON_PRM:
   1084      1.56  jdolecek 	case SK_YUKON_OPTIMA2:
   1085  1.67.2.1  christos 		return 1;
   1086       1.1       riz 	}
   1087       1.1       riz 
   1088  1.67.2.1  christos 	return 0;
   1089       1.1       riz }
   1090       1.1       riz 
   1091  1.67.2.2    martin static void
   1092      1.63  jdolecek msk_reset(struct sk_if_softc *sc_if)
   1093      1.63  jdolecek {
   1094      1.63  jdolecek 	/* GMAC and GPHY Reset */
   1095      1.63  jdolecek 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET);
   1096  1.67.2.3    martin 	SK_IF_WRITE_1(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET);
   1097      1.63  jdolecek 	DELAY(1000);
   1098  1.67.2.3    martin 	SK_IF_WRITE_1(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_CLEAR);
   1099      1.63  jdolecek 	SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF |
   1100      1.63  jdolecek 		      SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR);
   1101      1.63  jdolecek }
   1102      1.63  jdolecek 
   1103      1.20     joerg static bool
   1104      1.33    dyoung msk_resume(device_t dv, const pmf_qual_t *qual)
   1105      1.20     joerg {
   1106      1.20     joerg 	struct sk_if_softc *sc_if = device_private(dv);
   1107      1.43  christos 
   1108      1.20     joerg 	msk_init_yukon(sc_if);
   1109      1.20     joerg 	return true;
   1110      1.20     joerg }
   1111      1.20     joerg 
   1112       1.1       riz /*
   1113       1.1       riz  * Each XMAC chip is attached as a separate logical IP interface.
   1114       1.1       riz  * Single port cards will have only one logical interface of course.
   1115       1.1       riz  */
   1116  1.67.2.2    martin static void
   1117      1.26    cegger msk_attach(device_t parent, device_t self, void *aux)
   1118       1.1       riz {
   1119      1.27    cegger 	struct sk_if_softc *sc_if = device_private(self);
   1120      1.27    cegger 	struct sk_softc *sc = device_private(parent);
   1121       1.1       riz 	struct skc_attach_args *sa = aux;
   1122  1.67.2.4    martin 	bus_dmamap_t dmamap;
   1123  1.67.2.4    martin 	struct sk_txmap_entry *entry;
   1124       1.1       riz 	struct ifnet *ifp;
   1125  1.67.2.1  christos 	struct mii_data * const mii = &sc_if->sk_mii;
   1126       1.8  christos 	void *kva;
   1127      1.63  jdolecek 	int i;
   1128  1.67.2.1  christos 	uint32_t chunk;
   1129      1.63  jdolecek 	int mii_flags;
   1130       1.1       riz 
   1131      1.30  christos 	sc_if->sk_dev = self;
   1132       1.1       riz 	sc_if->sk_port = sa->skc_port;
   1133       1.1       riz 	sc_if->sk_softc = sc;
   1134       1.1       riz 	sc->sk_if[sa->skc_port] = sc_if;
   1135       1.1       riz 
   1136       1.1       riz 	DPRINTFN(2, ("begin msk_attach: port=%d\n", sc_if->sk_port));
   1137       1.1       riz 
   1138       1.1       riz 	/*
   1139       1.1       riz 	 * Get station address for this interface. Note that
   1140       1.1       riz 	 * dual port cards actually come with three station
   1141       1.1       riz 	 * addresses: one for each port, plus an extra. The
   1142       1.1       riz 	 * extra one is used by the SysKonnect driver software
   1143       1.1       riz 	 * as a 'virtual' station address for when both ports
   1144       1.1       riz 	 * are operating in failover mode. Currently we don't
   1145       1.1       riz 	 * use this extra address.
   1146       1.1       riz 	 */
   1147       1.1       riz 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1148       1.1       riz 		sc_if->sk_enaddr[i] =
   1149       1.1       riz 		    sk_win_read_1(sc, SK_MAC0_0 + (sa->skc_port * 8) + i);
   1150       1.1       riz 
   1151       1.1       riz 	aprint_normal(": Ethernet address %s\n",
   1152       1.1       riz 	    ether_sprintf(sc_if->sk_enaddr));
   1153       1.1       riz 
   1154       1.1       riz 	/*
   1155      1.57  jdolecek 	 * Set up RAM buffer addresses. The Yukon2 has a small amount
   1156      1.57  jdolecek 	 * of SRAM on it, somewhere between 4K and 48K.  We need to
   1157      1.57  jdolecek 	 * divide this up between the transmitter and receiver.  We
   1158      1.57  jdolecek 	 * give the receiver 2/3 of the memory (rounded down), and the
   1159      1.57  jdolecek 	 * transmitter whatever remains.
   1160       1.1       riz 	 */
   1161  1.67.2.1  christos 	if (sc->sk_ramsize) {
   1162  1.67.2.1  christos 		chunk = (2 * (sc->sk_ramsize / sizeof(uint64_t)) / 3) & ~0xff;
   1163  1.67.2.1  christos 		sc_if->sk_rx_ramstart = 0;
   1164  1.67.2.1  christos 		sc_if->sk_rx_ramend = sc_if->sk_rx_ramstart + chunk - 1;
   1165  1.67.2.1  christos 		chunk = (sc->sk_ramsize / sizeof(uint64_t)) - chunk;
   1166  1.67.2.1  christos 		sc_if->sk_tx_ramstart = sc_if->sk_rx_ramend + 1;
   1167  1.67.2.1  christos 		sc_if->sk_tx_ramend = sc_if->sk_tx_ramstart + chunk - 1;
   1168  1.67.2.1  christos 
   1169  1.67.2.1  christos 		DPRINTFN(2, ("msk_attach: rx_ramstart=%#x rx_ramend=%#x\n"
   1170  1.67.2.1  christos 			     "           tx_ramstart=%#x tx_ramend=%#x\n",
   1171  1.67.2.1  christos 			     sc_if->sk_rx_ramstart, sc_if->sk_rx_ramend,
   1172  1.67.2.1  christos 			     sc_if->sk_tx_ramstart, sc_if->sk_tx_ramend));
   1173  1.67.2.1  christos 	}
   1174       1.1       riz 
   1175       1.1       riz 	/* Allocate the descriptor queues. */
   1176       1.1       riz 	if (bus_dmamem_alloc(sc->sc_dmatag, sizeof(struct msk_ring_data),
   1177      1.63  jdolecek 	    PAGE_SIZE, 0, &sc_if->sk_ring_seg, 1, &sc_if->sk_ring_nseg,
   1178      1.63  jdolecek 	    BUS_DMA_NOWAIT)) {
   1179       1.1       riz 		aprint_error(": can't alloc rx buffers\n");
   1180       1.1       riz 		goto fail;
   1181       1.1       riz 	}
   1182      1.63  jdolecek 	if (bus_dmamem_map(sc->sc_dmatag, &sc_if->sk_ring_seg,
   1183      1.63  jdolecek 	    sc_if->sk_ring_nseg,
   1184       1.1       riz 	    sizeof(struct msk_ring_data), &kva, BUS_DMA_NOWAIT)) {
   1185       1.1       riz 		aprint_error(": can't map dma buffers (%zu bytes)\n",
   1186       1.1       riz 		       sizeof(struct msk_ring_data));
   1187       1.1       riz 		goto fail_1;
   1188       1.1       riz 	}
   1189       1.1       riz 	if (bus_dmamap_create(sc->sc_dmatag, sizeof(struct msk_ring_data), 1,
   1190       1.1       riz 	    sizeof(struct msk_ring_data), 0, BUS_DMA_NOWAIT,
   1191      1.59  jdolecek 	    &sc_if->sk_ring_map)) {
   1192       1.1       riz 		aprint_error(": can't create dma map\n");
   1193       1.1       riz 		goto fail_2;
   1194       1.1       riz 	}
   1195       1.1       riz 	if (bus_dmamap_load(sc->sc_dmatag, sc_if->sk_ring_map, kva,
   1196       1.1       riz 	    sizeof(struct msk_ring_data), NULL, BUS_DMA_NOWAIT)) {
   1197       1.1       riz 		aprint_error(": can't load dma map\n");
   1198       1.1       riz 		goto fail_3;
   1199       1.1       riz 	}
   1200  1.67.2.4    martin 
   1201  1.67.2.4    martin 	SIMPLEQ_INIT(&sc_if->sk_txmap_head);
   1202  1.67.2.4    martin 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
   1203  1.67.2.4    martin 		sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
   1204  1.67.2.4    martin 
   1205  1.67.2.4    martin 		if (bus_dmamap_create(sc->sc_dmatag, SK_JLEN, SK_NTXSEG,
   1206  1.67.2.4    martin 		    SK_JLEN, 0, BUS_DMA_NOWAIT, &dmamap)) {
   1207  1.67.2.4    martin 			aprint_error_dev(sc_if->sk_dev,
   1208  1.67.2.4    martin 			    "Can't create TX dmamap\n");
   1209  1.67.2.4    martin 			goto fail_3;
   1210  1.67.2.4    martin 		}
   1211  1.67.2.4    martin 
   1212  1.67.2.4    martin 		entry = malloc(sizeof(*entry), M_DEVBUF, M_WAITOK);
   1213  1.67.2.4    martin 		entry->dmamap = dmamap;
   1214  1.67.2.4    martin 		SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head, entry, link);
   1215  1.67.2.4    martin 	}
   1216  1.67.2.4    martin 
   1217      1.59  jdolecek 	sc_if->sk_rdata = (struct msk_ring_data *)kva;
   1218      1.24    cegger 	memset(sc_if->sk_rdata, 0, sizeof(struct msk_ring_data));
   1219       1.1       riz 
   1220  1.67.2.1  christos 	if (sc->sk_type != SK_YUKON_FE &&
   1221  1.67.2.1  christos 	    sc->sk_type != SK_YUKON_FE_P)
   1222  1.67.2.1  christos 		sc_if->sk_pktlen = SK_JLEN;
   1223  1.67.2.1  christos 	else
   1224  1.67.2.1  christos 		sc_if->sk_pktlen = MCLBYTES;
   1225  1.67.2.1  christos 
   1226       1.1       riz 	/* Try to allocate memory for jumbo buffers. */
   1227       1.1       riz 	if (msk_alloc_jumbo_mem(sc_if)) {
   1228       1.1       riz 		aprint_error(": jumbo buffer allocation failed\n");
   1229       1.1       riz 		goto fail_3;
   1230       1.1       riz 	}
   1231  1.67.2.1  christos 
   1232      1.19    dyoung 	sc_if->sk_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU;
   1233  1.67.2.1  christos 	if (sc->sk_type != SK_YUKON_FE &&
   1234  1.67.2.1  christos 	    sc->sk_type != SK_YUKON_FE_P)
   1235      1.19    dyoung 		sc_if->sk_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1236       1.1       riz 
   1237  1.67.2.1  christos 	ifp = &sc_if->sk_ethercom.ec_if;
   1238       1.1       riz 	ifp->if_softc = sc_if;
   1239       1.1       riz 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1240       1.1       riz 	ifp->if_ioctl = msk_ioctl;
   1241       1.1       riz 	ifp->if_start = msk_start;
   1242       1.1       riz 	ifp->if_stop = msk_stop;
   1243       1.1       riz 	ifp->if_init = msk_init;
   1244       1.1       riz 	ifp->if_watchdog = msk_watchdog;
   1245       1.1       riz 	ifp->if_baudrate = 1000000000;
   1246       1.1       riz 	IFQ_SET_MAXLEN(&ifp->if_snd, MSK_TX_RING_CNT - 1);
   1247       1.1       riz 	IFQ_SET_READY(&ifp->if_snd);
   1248      1.30  christos 	strlcpy(ifp->if_xname, device_xname(sc_if->sk_dev), IFNAMSIZ);
   1249       1.1       riz 
   1250      1.63  jdolecek 	msk_reset(sc_if);
   1251      1.63  jdolecek 
   1252       1.1       riz 	/*
   1253       1.1       riz 	 * Do miibus setup.
   1254       1.1       riz 	 */
   1255  1.67.2.1  christos 	DPRINTFN(2, ("msk_attach: 1\n"));
   1256       1.1       riz 
   1257  1.67.2.1  christos 	mii->mii_ifp = ifp;
   1258  1.67.2.1  christos 	mii->mii_readreg = msk_miibus_readreg;
   1259  1.67.2.1  christos 	mii->mii_writereg = msk_miibus_writereg;
   1260  1.67.2.1  christos 	mii->mii_statchg = msk_miibus_statchg;
   1261       1.1       riz 
   1262  1.67.2.1  christos 	sc_if->sk_ethercom.ec_mii = mii;
   1263  1.67.2.1  christos 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1264      1.63  jdolecek 	mii_flags = MIIF_DOPAUSE;
   1265      1.63  jdolecek 	if (sc->sk_fibertype)
   1266      1.63  jdolecek 		mii_flags |= MIIF_HAVEFIBER;
   1267  1.67.2.1  christos 	mii_attach(self, mii, 0xffffffff, 0, MII_OFFSET_ANY, mii_flags);
   1268  1.67.2.1  christos 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1269      1.30  christos 		aprint_error_dev(sc_if->sk_dev, "no PHY found!\n");
   1270  1.67.2.1  christos 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL,
   1271       1.1       riz 			    0, NULL);
   1272  1.67.2.1  christos 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
   1273       1.1       riz 	} else
   1274  1.67.2.1  christos 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1275       1.1       riz 
   1276       1.9        ad 	callout_init(&sc_if->sk_tick_ch, 0);
   1277       1.5   msaitoh 	callout_setfunc(&sc_if->sk_tick_ch, msk_tick, sc_if);
   1278       1.1       riz 	callout_schedule(&sc_if->sk_tick_ch, hz);
   1279       1.1       riz 
   1280  1.67.2.1  christos 	callout_init(&sc_if->sk_tick_rx, 0);
   1281  1.67.2.1  christos 	callout_setfunc(&sc_if->sk_tick_rx, msk_fill_rx_tick, sc_if);
   1282  1.67.2.1  christos 
   1283       1.1       riz 	/*
   1284       1.1       riz 	 * Call MI attach routines.
   1285       1.1       riz 	 */
   1286       1.1       riz 	if_attach(ifp);
   1287      1.53     ozaki 	if_deferred_start_init(ifp, NULL);
   1288       1.1       riz 	ether_ifattach(ifp, sc_if->sk_enaddr);
   1289       1.1       riz 
   1290      1.28   tsutsui 	if (pmf_device_register(self, NULL, msk_resume))
   1291      1.28   tsutsui 		pmf_class_network_register(self, ifp);
   1292      1.28   tsutsui 	else
   1293      1.20     joerg 		aprint_error_dev(self, "couldn't establish power handler\n");
   1294       1.1       riz 
   1295  1.67.2.1  christos 	if (sc->rnd_attached++ == 0) {
   1296  1.67.2.1  christos 		rnd_attach_source(&sc->rnd_source, device_xname(sc->sk_dev),
   1297  1.67.2.1  christos 			RND_TYPE_NET, RND_FLAG_DEFAULT);
   1298  1.67.2.1  christos 	}
   1299       1.1       riz 
   1300       1.1       riz 	DPRINTFN(2, ("msk_attach: end\n"));
   1301       1.1       riz 	return;
   1302       1.1       riz 
   1303       1.1       riz fail_3:
   1304       1.1       riz 	bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
   1305       1.1       riz fail_2:
   1306       1.1       riz 	bus_dmamem_unmap(sc->sc_dmatag, kva, sizeof(struct msk_ring_data));
   1307       1.1       riz fail_1:
   1308      1.63  jdolecek 	bus_dmamem_free(sc->sc_dmatag, &sc_if->sk_ring_seg, sc_if->sk_ring_nseg);
   1309       1.1       riz fail:
   1310       1.1       riz 	sc->sk_if[sa->skc_port] = NULL;
   1311       1.1       riz }
   1312       1.1       riz 
   1313  1.67.2.2    martin static int
   1314      1.63  jdolecek msk_detach(device_t self, int flags)
   1315      1.63  jdolecek {
   1316  1.67.2.1  christos 	struct sk_if_softc *sc_if = device_private(self);
   1317      1.63  jdolecek 	struct sk_softc *sc = sc_if->sk_softc;
   1318  1.67.2.4    martin 	struct sk_txmap_entry *entry;
   1319      1.63  jdolecek 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
   1320      1.63  jdolecek 
   1321      1.63  jdolecek 	if (sc->sk_if[sc_if->sk_port] == NULL)
   1322  1.67.2.1  christos 		return 0;
   1323  1.67.2.1  christos 
   1324  1.67.2.1  christos 	msk_stop(ifp, 1);
   1325      1.63  jdolecek 
   1326  1.67.2.4    martin 	while ((entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head))) {
   1327  1.67.2.4    martin 		SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
   1328  1.67.2.4    martin 		bus_dmamap_destroy(sc->sc_dmatag, entry->dmamap);
   1329  1.67.2.4    martin 		free(entry, M_DEVBUF);
   1330  1.67.2.4    martin 	}
   1331  1.67.2.4    martin 
   1332  1.67.2.1  christos 	if (--sc->rnd_attached == 0)
   1333  1.67.2.1  christos 		rnd_detach_source(&sc->rnd_source);
   1334      1.63  jdolecek 
   1335      1.63  jdolecek 	callout_halt(&sc_if->sk_tick_ch, NULL);
   1336      1.63  jdolecek 	callout_destroy(&sc_if->sk_tick_ch);
   1337      1.63  jdolecek 
   1338  1.67.2.1  christos 	callout_halt(&sc_if->sk_tick_rx, NULL);
   1339  1.67.2.1  christos 	callout_destroy(&sc_if->sk_tick_rx);
   1340  1.67.2.1  christos 
   1341      1.63  jdolecek 	/* Detach any PHYs we might have. */
   1342      1.63  jdolecek 	if (LIST_FIRST(&sc_if->sk_mii.mii_phys) != NULL)
   1343      1.63  jdolecek 		mii_detach(&sc_if->sk_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1344      1.63  jdolecek 
   1345      1.63  jdolecek 	pmf_device_deregister(self);
   1346      1.63  jdolecek 
   1347      1.63  jdolecek 	ether_ifdetach(ifp);
   1348      1.63  jdolecek 	if_detach(ifp);
   1349      1.63  jdolecek 
   1350  1.67.2.2    martin 	/* Delete any remaining media. */
   1351  1.67.2.2    martin 	ifmedia_fini(&sc_if->sk_mii.mii_media);
   1352  1.67.2.2    martin 
   1353  1.67.2.1  christos 	msk_free_jumbo_mem(sc_if);
   1354  1.67.2.1  christos 
   1355      1.63  jdolecek 	bus_dmamem_unmap(sc->sc_dmatag, sc_if->sk_rdata,
   1356      1.63  jdolecek 	    sizeof(struct msk_ring_data));
   1357      1.63  jdolecek 	bus_dmamem_free(sc->sc_dmatag,
   1358      1.63  jdolecek 	    &sc_if->sk_ring_seg, sc_if->sk_ring_nseg);
   1359  1.67.2.1  christos 	bus_dmamap_destroy(sc->sc_dmatag, sc_if->sk_ring_map);
   1360      1.63  jdolecek 	sc->sk_if[sc_if->sk_port] = NULL;
   1361      1.63  jdolecek 
   1362  1.67.2.1  christos 	return 0;
   1363      1.63  jdolecek }
   1364      1.63  jdolecek 
   1365  1.67.2.2    martin static int
   1366       1.1       riz mskcprint(void *aux, const char *pnp)
   1367       1.1       riz {
   1368       1.1       riz 	struct skc_attach_args *sa = aux;
   1369       1.1       riz 
   1370       1.1       riz 	if (pnp)
   1371      1.64  jdolecek 		aprint_normal("msk port %c at %s",
   1372       1.1       riz 		    (sa->skc_port == SK_PORT_A) ? 'A' : 'B', pnp);
   1373       1.1       riz 	else
   1374  1.67.2.1  christos 		aprint_normal(" port %c",
   1375  1.67.2.1  christos 		    (sa->skc_port == SK_PORT_A) ? 'A' : 'B');
   1376  1.67.2.1  christos 	return UNCONF;
   1377       1.1       riz }
   1378       1.1       riz 
   1379       1.1       riz /*
   1380       1.1       riz  * Attach the interface. Allocate softc structures, do ifmedia
   1381       1.1       riz  * setup and ethernet/BPF attach.
   1382       1.1       riz  */
   1383  1.67.2.2    martin static void
   1384      1.26    cegger mskc_attach(device_t parent, device_t self, void *aux)
   1385       1.1       riz {
   1386      1.27    cegger 	struct sk_softc *sc = device_private(self);
   1387       1.1       riz 	struct pci_attach_args *pa = aux;
   1388       1.1       riz 	struct skc_attach_args skca;
   1389       1.1       riz 	pci_chipset_tag_t pc = pa->pa_pc;
   1390       1.1       riz 	pcireg_t command, memtype;
   1391       1.1       riz 	const char *intrstr = NULL;
   1392       1.1       riz 	int rc, sk_nodenum;
   1393  1.67.2.1  christos 	uint8_t hw, pmd;
   1394       1.1       riz 	const char *revstr = NULL;
   1395       1.1       riz 	const struct sysctlnode *node;
   1396       1.8  christos 	void *kva;
   1397      1.45  christos 	char intrbuf[PCI_INTRSTR_LEN];
   1398       1.1       riz 
   1399       1.1       riz 	DPRINTFN(2, ("begin mskc_attach\n"));
   1400       1.1       riz 
   1401      1.30  christos 	sc->sk_dev = self;
   1402       1.1       riz 	/*
   1403       1.1       riz 	 * Handle power management nonsense.
   1404       1.1       riz 	 */
   1405       1.1       riz 	command = pci_conf_read(pc, pa->pa_tag, SK_PCI_CAPID) & 0x000000FF;
   1406       1.1       riz 
   1407       1.1       riz 	if (command == 0x01) {
   1408       1.1       riz 		command = pci_conf_read(pc, pa->pa_tag, SK_PCI_PWRMGMTCTRL);
   1409       1.1       riz 		if (command & SK_PSTATE_MASK) {
   1410  1.67.2.1  christos 			uint32_t		iobase, membase, irq;
   1411       1.1       riz 
   1412       1.1       riz 			/* Save important PCI config data. */
   1413       1.1       riz 			iobase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOIO);
   1414       1.1       riz 			membase = pci_conf_read(pc, pa->pa_tag, SK_PCI_LOMEM);
   1415       1.1       riz 			irq = pci_conf_read(pc, pa->pa_tag, SK_PCI_INTLINE);
   1416       1.1       riz 
   1417       1.1       riz 			/* Reset the power state. */
   1418      1.30  christos 			aprint_normal_dev(sc->sk_dev, "chip is in D%d power "
   1419      1.30  christos 			    "mode -- setting to D0\n",
   1420       1.1       riz 			    command & SK_PSTATE_MASK);
   1421       1.1       riz 			command &= 0xFFFFFFFC;
   1422       1.1       riz 			pci_conf_write(pc, pa->pa_tag,
   1423       1.1       riz 			    SK_PCI_PWRMGMTCTRL, command);
   1424       1.1       riz 
   1425       1.1       riz 			/* Restore PCI config data. */
   1426       1.1       riz 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOIO, iobase);
   1427       1.1       riz 			pci_conf_write(pc, pa->pa_tag, SK_PCI_LOMEM, membase);
   1428       1.1       riz 			pci_conf_write(pc, pa->pa_tag, SK_PCI_INTLINE, irq);
   1429       1.1       riz 		}
   1430       1.1       riz 	}
   1431       1.1       riz 
   1432       1.1       riz 	/*
   1433       1.1       riz 	 * Map control/status registers.
   1434       1.1       riz 	 */
   1435       1.1       riz 	memtype = pci_mapreg_type(pc, pa->pa_tag, SK_PCI_LOMEM);
   1436      1.63  jdolecek 	if (pci_mapreg_map(pa, SK_PCI_LOMEM, memtype, 0, &sc->sk_btag,
   1437  1.67.2.1  christos 	    &sc->sk_bhandle, NULL, &sc->sk_bsize)) {
   1438       1.1       riz 		aprint_error(": can't map mem space\n");
   1439       1.1       riz 		return;
   1440       1.1       riz 	}
   1441       1.1       riz 
   1442  1.67.2.1  christos 	if (pci_dma64_available(pa))
   1443  1.67.2.1  christos 		sc->sc_dmatag = pa->pa_dmat64;
   1444  1.67.2.1  christos 	else
   1445  1.67.2.1  christos 		sc->sc_dmatag = pa->pa_dmat;
   1446       1.1       riz 
   1447      1.36  jakllsch 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1448      1.37  jakllsch 	command |= PCI_COMMAND_MASTER_ENABLE;
   1449      1.36  jakllsch 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
   1450      1.36  jakllsch 
   1451       1.1       riz 	sc->sk_type = sk_win_read_1(sc, SK_CHIPVER);
   1452       1.1       riz 	sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4);
   1453       1.1       riz 
   1454       1.1       riz 	/* bail out here if chip is not recognized */
   1455       1.5   msaitoh 	if (!(SK_IS_YUKON2(sc))) {
   1456       1.1       riz 		aprint_error(": unknown chip type: %d\n", sc->sk_type);
   1457       1.1       riz 		goto fail_1;
   1458       1.1       riz 	}
   1459       1.1       riz 	DPRINTFN(2, ("mskc_attach: allocate interrupt\n"));
   1460       1.1       riz 
   1461       1.1       riz 	/* Allocate interrupt */
   1462  1.67.2.1  christos 	if (pci_intr_alloc(pa, &sc->sk_pihp, NULL, 0)) {
   1463       1.1       riz 		aprint_error(": couldn't map interrupt\n");
   1464       1.1       riz 		goto fail_1;
   1465       1.1       riz 	}
   1466       1.1       riz 
   1467  1.67.2.1  christos 	intrstr = pci_intr_string(pc, sc->sk_pihp[0], intrbuf, sizeof(intrbuf));
   1468  1.67.2.1  christos 	sc->sk_intrhand = pci_intr_establish_xname(pc, sc->sk_pihp[0], IPL_NET,
   1469  1.67.2.1  christos 	    msk_intr, sc, device_xname(sc->sk_dev));
   1470       1.1       riz 	if (sc->sk_intrhand == NULL) {
   1471       1.1       riz 		aprint_error(": couldn't establish interrupt");
   1472       1.1       riz 		if (intrstr != NULL)
   1473       1.1       riz 			aprint_error(" at %s", intrstr);
   1474       1.1       riz 		aprint_error("\n");
   1475       1.1       riz 		goto fail_1;
   1476       1.1       riz 	}
   1477      1.63  jdolecek 	sc->sk_pc = pc;
   1478       1.1       riz 
   1479       1.1       riz 	if (bus_dmamem_alloc(sc->sc_dmatag,
   1480  1.67.2.1  christos 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1481  1.67.2.1  christos 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1482      1.63  jdolecek 	    0, &sc->sk_status_seg, 1, &sc->sk_status_nseg, BUS_DMA_NOWAIT)) {
   1483       1.1       riz 		aprint_error(": can't alloc status buffers\n");
   1484       1.1       riz 		goto fail_2;
   1485       1.1       riz 	}
   1486       1.1       riz 
   1487      1.63  jdolecek 	if (bus_dmamem_map(sc->sc_dmatag,
   1488      1.63  jdolecek 	    &sc->sk_status_seg, sc->sk_status_nseg,
   1489       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1490       1.1       riz 	    &kva, BUS_DMA_NOWAIT)) {
   1491       1.1       riz 		aprint_error(": can't map dma buffers (%zu bytes)\n",
   1492       1.1       riz 		    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1493       1.1       riz 		goto fail_3;
   1494       1.1       riz 	}
   1495       1.1       riz 	if (bus_dmamap_create(sc->sc_dmatag,
   1496       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 1,
   1497       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc), 0,
   1498       1.1       riz 	    BUS_DMA_NOWAIT, &sc->sk_status_map)) {
   1499       1.1       riz 		aprint_error(": can't create dma map\n");
   1500       1.1       riz 		goto fail_4;
   1501       1.1       riz 	}
   1502       1.1       riz 	if (bus_dmamap_load(sc->sc_dmatag, sc->sk_status_map, kva,
   1503       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc),
   1504       1.1       riz 	    NULL, BUS_DMA_NOWAIT)) {
   1505       1.1       riz 		aprint_error(": can't load dma map\n");
   1506       1.1       riz 		goto fail_5;
   1507       1.1       riz 	}
   1508       1.1       riz 	sc->sk_status_ring = (struct msk_status_desc *)kva;
   1509       1.1       riz 
   1510      1.30  christos 	sc->sk_int_mod = SK_IM_DEFAULT;
   1511      1.30  christos 	sc->sk_int_mod_pending = 0;
   1512      1.30  christos 
   1513       1.1       riz 	/* Reset the adapter. */
   1514      1.63  jdolecek 	mskc_reset(sc);
   1515       1.1       riz 
   1516      1.57  jdolecek 	sc->sk_ramsize = sk_win_read_1(sc, SK_EPROM0) * 4096;
   1517      1.57  jdolecek 	DPRINTFN(2, ("mskc_attach: ramsize=%dK\n", sc->sk_ramsize / 1024));
   1518       1.1       riz 
   1519      1.63  jdolecek 	pmd = sk_win_read_1(sc, SK_PMDTYPE);
   1520      1.63  jdolecek 	if (pmd == 'L' || pmd == 'S' || pmd == 'P')
   1521      1.63  jdolecek 		sc->sk_fibertype = 1;
   1522      1.63  jdolecek 
   1523       1.1       riz 	switch (sc->sk_type) {
   1524       1.1       riz 	case SK_YUKON_XL:
   1525       1.5   msaitoh 		sc->sk_name = "Yukon-2 XL";
   1526       1.1       riz 		break;
   1527       1.1       riz 	case SK_YUKON_EC_U:
   1528       1.5   msaitoh 		sc->sk_name = "Yukon-2 EC Ultra";
   1529       1.1       riz 		break;
   1530      1.56  jdolecek 	case SK_YUKON_EX:
   1531      1.56  jdolecek 		sc->sk_name = "Yukon-2 Extreme";
   1532      1.56  jdolecek 		break;
   1533       1.1       riz 	case SK_YUKON_EC:
   1534       1.5   msaitoh 		sc->sk_name = "Yukon-2 EC";
   1535       1.1       riz 		break;
   1536       1.1       riz 	case SK_YUKON_FE:
   1537       1.5   msaitoh 		sc->sk_name = "Yukon-2 FE";
   1538       1.1       riz 		break;
   1539      1.56  jdolecek 	case SK_YUKON_FE_P:
   1540      1.56  jdolecek 		sc->sk_name = "Yukon-2 FE+";
   1541      1.56  jdolecek 		break;
   1542      1.56  jdolecek 	case SK_YUKON_SUPR:
   1543      1.56  jdolecek 		sc->sk_name = "Yukon-2 Supreme";
   1544      1.56  jdolecek 		break;
   1545      1.56  jdolecek 	case SK_YUKON_ULTRA2:
   1546      1.56  jdolecek 		sc->sk_name = "Yukon-2 Ultra 2";
   1547      1.56  jdolecek 		break;
   1548      1.56  jdolecek 	case SK_YUKON_OPTIMA:
   1549      1.56  jdolecek 		sc->sk_name = "Yukon-2 Optima";
   1550      1.56  jdolecek 		break;
   1551      1.56  jdolecek 	case SK_YUKON_PRM:
   1552      1.56  jdolecek 		sc->sk_name = "Yukon-2 Optima Prime";
   1553      1.56  jdolecek 		break;
   1554      1.56  jdolecek 	case SK_YUKON_OPTIMA2:
   1555      1.56  jdolecek 		sc->sk_name = "Yukon-2 Optima 2";
   1556      1.56  jdolecek 		break;
   1557       1.1       riz 	default:
   1558       1.5   msaitoh 		sc->sk_name = "Yukon (Unknown)";
   1559       1.1       riz 	}
   1560       1.1       riz 
   1561       1.1       riz 	if (sc->sk_type == SK_YUKON_XL) {
   1562       1.1       riz 		switch (sc->sk_rev) {
   1563       1.1       riz 		case SK_YUKON_XL_REV_A0:
   1564       1.1       riz 			revstr = "A0";
   1565       1.1       riz 			break;
   1566       1.1       riz 		case SK_YUKON_XL_REV_A1:
   1567       1.1       riz 			revstr = "A1";
   1568       1.1       riz 			break;
   1569       1.1       riz 		case SK_YUKON_XL_REV_A2:
   1570       1.1       riz 			revstr = "A2";
   1571       1.1       riz 			break;
   1572       1.1       riz 		case SK_YUKON_XL_REV_A3:
   1573       1.1       riz 			revstr = "A3";
   1574       1.1       riz 			break;
   1575       1.1       riz 		default:
   1576       1.6   msaitoh 			break;
   1577       1.1       riz 		}
   1578       1.1       riz 	}
   1579       1.1       riz 
   1580       1.1       riz 	if (sc->sk_type == SK_YUKON_EC) {
   1581       1.1       riz 		switch (sc->sk_rev) {
   1582       1.1       riz 		case SK_YUKON_EC_REV_A1:
   1583       1.1       riz 			revstr = "A1";
   1584       1.1       riz 			break;
   1585       1.1       riz 		case SK_YUKON_EC_REV_A2:
   1586       1.1       riz 			revstr = "A2";
   1587       1.1       riz 			break;
   1588       1.1       riz 		case SK_YUKON_EC_REV_A3:
   1589       1.1       riz 			revstr = "A3";
   1590       1.1       riz 			break;
   1591       1.1       riz 		default:
   1592       1.6   msaitoh 			break;
   1593       1.6   msaitoh 		}
   1594       1.6   msaitoh 	}
   1595       1.6   msaitoh 
   1596       1.6   msaitoh 	if (sc->sk_type == SK_YUKON_FE) {
   1597       1.6   msaitoh 		switch (sc->sk_rev) {
   1598       1.6   msaitoh 		case SK_YUKON_FE_REV_A1:
   1599       1.6   msaitoh 			revstr = "A1";
   1600       1.6   msaitoh 			break;
   1601       1.6   msaitoh 		case SK_YUKON_FE_REV_A2:
   1602       1.6   msaitoh 			revstr = "A2";
   1603       1.6   msaitoh 			break;
   1604       1.6   msaitoh 		default:
   1605       1.6   msaitoh 			break;
   1606       1.1       riz 		}
   1607       1.1       riz 	}
   1608       1.1       riz 
   1609       1.1       riz 	if (sc->sk_type == SK_YUKON_EC_U) {
   1610       1.1       riz 		switch (sc->sk_rev) {
   1611       1.1       riz 		case SK_YUKON_EC_U_REV_A0:
   1612       1.1       riz 			revstr = "A0";
   1613       1.1       riz 			break;
   1614       1.1       riz 		case SK_YUKON_EC_U_REV_A1:
   1615       1.1       riz 			revstr = "A1";
   1616       1.1       riz 			break;
   1617       1.6   msaitoh 		case SK_YUKON_EC_U_REV_B0:
   1618       1.6   msaitoh 			revstr = "B0";
   1619       1.6   msaitoh 			break;
   1620      1.56  jdolecek 		case SK_YUKON_EC_U_REV_B1:
   1621      1.56  jdolecek 			revstr = "B1";
   1622      1.56  jdolecek 			break;
   1623       1.1       riz 		default:
   1624       1.6   msaitoh 			break;
   1625       1.1       riz 		}
   1626       1.1       riz 	}
   1627       1.1       riz 
   1628      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_FE) {
   1629      1.56  jdolecek 		switch (sc->sk_rev) {
   1630      1.56  jdolecek 		case SK_YUKON_FE_REV_A1:
   1631      1.56  jdolecek 			revstr = "A1";
   1632      1.56  jdolecek 			break;
   1633      1.56  jdolecek 		case SK_YUKON_FE_REV_A2:
   1634      1.56  jdolecek 			revstr = "A2";
   1635      1.56  jdolecek 			break;
   1636      1.56  jdolecek 		default:
   1637      1.56  jdolecek 			;
   1638      1.56  jdolecek 		}
   1639      1.56  jdolecek 	}
   1640      1.56  jdolecek 
   1641      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_FE_P && sc->sk_rev == SK_YUKON_FE_P_REV_A0)
   1642      1.56  jdolecek 		revstr = "A0";
   1643      1.56  jdolecek 
   1644      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_EX) {
   1645      1.56  jdolecek 		switch (sc->sk_rev) {
   1646      1.56  jdolecek 		case SK_YUKON_EX_REV_A0:
   1647      1.56  jdolecek 			revstr = "A0";
   1648      1.56  jdolecek 			break;
   1649      1.56  jdolecek 		case SK_YUKON_EX_REV_B0:
   1650      1.56  jdolecek 			revstr = "B0";
   1651      1.56  jdolecek 			break;
   1652      1.56  jdolecek 		default:
   1653      1.56  jdolecek 			;
   1654      1.56  jdolecek 		}
   1655      1.56  jdolecek 	}
   1656      1.56  jdolecek 
   1657      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_SUPR) {
   1658      1.56  jdolecek 		switch (sc->sk_rev) {
   1659      1.56  jdolecek 		case SK_YUKON_SUPR_REV_A0:
   1660      1.56  jdolecek 			revstr = "A0";
   1661      1.56  jdolecek 			break;
   1662      1.56  jdolecek 		case SK_YUKON_SUPR_REV_B0:
   1663      1.56  jdolecek 			revstr = "B0";
   1664      1.56  jdolecek 			break;
   1665      1.56  jdolecek 		case SK_YUKON_SUPR_REV_B1:
   1666      1.56  jdolecek 			revstr = "B1";
   1667      1.56  jdolecek 			break;
   1668      1.56  jdolecek 		default:
   1669      1.56  jdolecek 			;
   1670      1.56  jdolecek 		}
   1671      1.56  jdolecek 	}
   1672      1.56  jdolecek 
   1673      1.56  jdolecek 	if (sc->sk_type == SK_YUKON_PRM) {
   1674      1.56  jdolecek 		switch (sc->sk_rev) {
   1675      1.56  jdolecek 		case SK_YUKON_PRM_REV_Z1:
   1676      1.56  jdolecek 			revstr = "Z1";
   1677      1.56  jdolecek 			break;
   1678      1.56  jdolecek 		case SK_YUKON_PRM_REV_A0:
   1679      1.56  jdolecek 			revstr = "A0";
   1680      1.56  jdolecek 			break;
   1681      1.56  jdolecek 		default:
   1682      1.56  jdolecek 			;
   1683      1.56  jdolecek 		}
   1684      1.56  jdolecek 	}
   1685      1.56  jdolecek 
   1686       1.1       riz 	/* Announce the product name. */
   1687       1.1       riz 	aprint_normal(", %s", sc->sk_name);
   1688       1.1       riz 	if (revstr != NULL)
   1689       1.1       riz 		aprint_normal(" rev. %s", revstr);
   1690       1.1       riz 	aprint_normal(" (0x%x): %s\n", sc->sk_rev, intrstr);
   1691       1.1       riz 
   1692       1.1       riz 	sc->sk_macs = 1;
   1693       1.1       riz 
   1694       1.1       riz 	hw = sk_win_read_1(sc, SK_Y2_HWRES);
   1695       1.1       riz 	if ((hw & SK_Y2_HWRES_LINK_MASK) == SK_Y2_HWRES_LINK_DUAL) {
   1696       1.1       riz 		if ((sk_win_read_1(sc, SK_Y2_CLKGATE) &
   1697       1.1       riz 		    SK_Y2_CLKGATE_LINK2_INACTIVE) == 0)
   1698       1.1       riz 			sc->sk_macs++;
   1699       1.1       riz 	}
   1700       1.1       riz 
   1701       1.1       riz 	skca.skc_port = SK_PORT_A;
   1702       1.1       riz 	skca.skc_type = sc->sk_type;
   1703       1.1       riz 	skca.skc_rev = sc->sk_rev;
   1704      1.30  christos 	(void)config_found(sc->sk_dev, &skca, mskcprint);
   1705       1.1       riz 
   1706       1.1       riz 	if (sc->sk_macs > 1) {
   1707       1.1       riz 		skca.skc_port = SK_PORT_B;
   1708       1.1       riz 		skca.skc_type = sc->sk_type;
   1709       1.1       riz 		skca.skc_rev = sc->sk_rev;
   1710      1.30  christos 		(void)config_found(sc->sk_dev, &skca, mskcprint);
   1711       1.1       riz 	}
   1712       1.1       riz 
   1713       1.1       riz 	/* Turn on the 'driver is loaded' LED. */
   1714       1.1       riz 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
   1715       1.1       riz 
   1716       1.1       riz 	/* skc sysctl setup */
   1717       1.1       riz 
   1718       1.1       riz 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
   1719      1.30  christos 	    0, CTLTYPE_NODE, device_xname(sc->sk_dev),
   1720       1.1       riz 	    SYSCTL_DESCR("mskc per-controller controls"),
   1721       1.1       riz 	    NULL, 0, NULL, 0, CTL_HW, msk_root_num, CTL_CREATE,
   1722       1.1       riz 	    CTL_EOL)) != 0) {
   1723      1.30  christos 		aprint_normal_dev(sc->sk_dev, "couldn't create sysctl node\n");
   1724       1.1       riz 		goto fail_6;
   1725       1.1       riz 	}
   1726       1.1       riz 
   1727       1.1       riz 	sk_nodenum = node->sysctl_num;
   1728       1.1       riz 
   1729       1.1       riz 	/* interrupt moderation time in usecs */
   1730       1.1       riz 	if ((rc = sysctl_createv(&sc->sk_clog, 0, NULL, &node,
   1731       1.1       riz 	    CTLFLAG_READWRITE,
   1732       1.1       riz 	    CTLTYPE_INT, "int_mod",
   1733       1.1       riz 	    SYSCTL_DESCR("msk interrupt moderation timer"),
   1734      1.40       dsl 	    msk_sysctl_handler, 0, (void *)sc,
   1735       1.1       riz 	    0, CTL_HW, msk_root_num, sk_nodenum, CTL_CREATE,
   1736       1.1       riz 	    CTL_EOL)) != 0) {
   1737  1.67.2.1  christos 		aprint_normal_dev(sc->sk_dev,
   1738  1.67.2.1  christos 		    "couldn't create int_mod sysctl node\n");
   1739       1.1       riz 		goto fail_6;
   1740       1.1       riz 	}
   1741       1.1       riz 
   1742      1.20     joerg 	if (!pmf_device_register(self, mskc_suspend, mskc_resume))
   1743      1.20     joerg 		aprint_error_dev(self, "couldn't establish power handler\n");
   1744      1.20     joerg 
   1745       1.1       riz 	return;
   1746       1.1       riz 
   1747  1.67.2.1  christos fail_6:
   1748       1.1       riz 	bus_dmamap_unload(sc->sc_dmatag, sc->sk_status_map);
   1749       1.1       riz fail_4:
   1750      1.43  christos 	bus_dmamem_unmap(sc->sc_dmatag, kva,
   1751       1.1       riz 	    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1752       1.1       riz fail_3:
   1753      1.63  jdolecek 	bus_dmamem_free(sc->sc_dmatag,
   1754      1.63  jdolecek 	    &sc->sk_status_seg, sc->sk_status_nseg);
   1755      1.63  jdolecek 	sc->sk_status_nseg = 0;
   1756  1.67.2.1  christos fail_5:
   1757  1.67.2.1  christos 	bus_dmamap_destroy(sc->sc_dmatag, sc->sk_status_map);
   1758       1.1       riz fail_2:
   1759       1.1       riz 	pci_intr_disestablish(pc, sc->sk_intrhand);
   1760      1.63  jdolecek 	sc->sk_intrhand = NULL;
   1761       1.1       riz fail_1:
   1762  1.67.2.1  christos 	bus_space_unmap(sc->sk_btag, sc->sk_bhandle, sc->sk_bsize);
   1763      1.63  jdolecek 	sc->sk_bsize = 0;
   1764      1.63  jdolecek }
   1765      1.63  jdolecek 
   1766  1.67.2.2    martin static int
   1767      1.63  jdolecek mskc_detach(device_t self, int flags)
   1768      1.63  jdolecek {
   1769  1.67.2.1  christos 	struct sk_softc *sc = device_private(self);
   1770      1.63  jdolecek 	int rv;
   1771      1.63  jdolecek 
   1772  1.67.2.1  christos 	if (sc->sk_intrhand) {
   1773  1.67.2.1  christos 		pci_intr_disestablish(sc->sk_pc, sc->sk_intrhand);
   1774  1.67.2.1  christos 		sc->sk_intrhand = NULL;
   1775  1.67.2.1  christos 	}
   1776  1.67.2.1  christos 
   1777  1.67.2.1  christos 	if (sc->sk_pihp != NULL) {
   1778  1.67.2.1  christos 		pci_intr_release(sc->sk_pc, sc->sk_pihp, 1);
   1779  1.67.2.1  christos 		sc->sk_pihp = NULL;
   1780  1.67.2.1  christos 	}
   1781  1.67.2.1  christos 
   1782      1.63  jdolecek 	rv = config_detach_children(self, flags);
   1783      1.63  jdolecek 	if (rv != 0)
   1784  1.67.2.1  christos 		return rv;
   1785      1.63  jdolecek 
   1786      1.63  jdolecek 	if (sc->sk_status_nseg > 0) {
   1787      1.63  jdolecek 		bus_dmamap_destroy(sc->sc_dmatag, sc->sk_status_map);
   1788      1.63  jdolecek 		bus_dmamem_unmap(sc->sc_dmatag, sc->sk_status_ring,
   1789      1.63  jdolecek 		    MSK_STATUS_RING_CNT * sizeof(struct msk_status_desc));
   1790      1.63  jdolecek 		bus_dmamem_free(sc->sc_dmatag,
   1791      1.63  jdolecek 		    &sc->sk_status_seg, sc->sk_status_nseg);
   1792      1.63  jdolecek 	}
   1793      1.63  jdolecek 
   1794      1.63  jdolecek 	if (sc->sk_bsize > 0)
   1795      1.63  jdolecek 		bus_space_unmap(sc->sk_btag, sc->sk_bhandle, sc->sk_bsize);
   1796      1.63  jdolecek 
   1797  1.67.2.1  christos 	return 0;
   1798       1.1       riz }
   1799       1.1       riz 
   1800  1.67.2.2    martin static int
   1801  1.67.2.1  christos msk_encap(struct sk_if_softc *sc_if, struct mbuf *m_head, uint32_t *txidx)
   1802       1.1       riz {
   1803       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
   1804       1.1       riz 	struct msk_tx_desc		*f = NULL;
   1805  1.67.2.1  christos 	uint32_t		frag, cur, hiaddr, old_hiaddr, total;
   1806  1.67.2.1  christos 	uint32_t		entries = 0;
   1807  1.67.2.1  christos 	size_t			i;
   1808       1.1       riz 	struct sk_txmap_entry	*entry;
   1809       1.1       riz 	bus_dmamap_t		txmap;
   1810  1.67.2.1  christos 	bus_addr_t		addr;
   1811       1.1       riz 
   1812       1.1       riz 	DPRINTFN(2, ("msk_encap\n"));
   1813       1.1       riz 
   1814       1.1       riz 	entry = SIMPLEQ_FIRST(&sc_if->sk_txmap_head);
   1815       1.1       riz 	if (entry == NULL) {
   1816       1.1       riz 		DPRINTFN(2, ("msk_encap: no txmap available\n"));
   1817  1.67.2.1  christos 		return ENOBUFS;
   1818       1.1       riz 	}
   1819       1.1       riz 	txmap = entry->dmamap;
   1820       1.1       riz 
   1821       1.1       riz 	cur = frag = *txidx;
   1822       1.1       riz 
   1823       1.1       riz #ifdef MSK_DEBUG
   1824       1.1       riz 	if (mskdebug >= 2)
   1825       1.1       riz 		msk_dump_mbuf(m_head);
   1826       1.1       riz #endif
   1827       1.1       riz 
   1828       1.1       riz 	/*
   1829       1.1       riz 	 * Start packing the mbufs in this chain into
   1830       1.1       riz 	 * the fragment pointers. Stop when we run out
   1831       1.1       riz 	 * of fragments or hit the end of the mbuf chain.
   1832       1.1       riz 	 */
   1833       1.1       riz 	if (bus_dmamap_load_mbuf(sc->sc_dmatag, txmap, m_head,
   1834       1.1       riz 	    BUS_DMA_NOWAIT)) {
   1835       1.1       riz 		DPRINTFN(2, ("msk_encap: dmamap failed\n"));
   1836  1.67.2.1  christos 		return ENOBUFS;
   1837       1.1       riz 	}
   1838       1.1       riz 
   1839  1.67.2.1  christos 	/* Count how many tx descriptors needed. */
   1840  1.67.2.1  christos 	hiaddr = sc_if->sk_cdata.sk_tx_hiaddr;
   1841  1.67.2.1  christos 	for (total = i = 0; i < txmap->dm_nsegs; i++) {
   1842  1.67.2.1  christos 		if (hiaddr != MSK_ADDR_HI(txmap->dm_segs[i].ds_addr)) {
   1843  1.67.2.1  christos 			hiaddr = MSK_ADDR_HI(txmap->dm_segs[i].ds_addr);
   1844  1.67.2.1  christos 			total++;
   1845  1.67.2.1  christos 		}
   1846  1.67.2.1  christos 		total++;
   1847  1.67.2.1  christos 	}
   1848  1.67.2.1  christos 
   1849  1.67.2.1  christos 	if (total > MSK_TX_RING_CNT - sc_if->sk_cdata.sk_tx_cnt - 2) {
   1850       1.5   msaitoh 		DPRINTFN(2, ("msk_encap: too few descriptors free\n"));
   1851       1.5   msaitoh 		bus_dmamap_unload(sc->sc_dmatag, txmap);
   1852  1.67.2.1  christos 		return ENOBUFS;
   1853       1.5   msaitoh 	}
   1854       1.5   msaitoh 
   1855  1.67.2.1  christos 	DPRINTFN(2, ("msk_encap: dm_nsegs=%d total desc=%u\n",
   1856  1.67.2.1  christos 	    txmap->dm_nsegs, total));
   1857       1.1       riz 
   1858       1.1       riz 	/* Sync the DMA map. */
   1859       1.1       riz 	bus_dmamap_sync(sc->sc_dmatag, txmap, 0, txmap->dm_mapsize,
   1860       1.1       riz 	    BUS_DMASYNC_PREWRITE);
   1861       1.1       riz 
   1862  1.67.2.1  christos 	old_hiaddr = sc_if->sk_cdata.sk_tx_hiaddr;
   1863       1.1       riz 	for (i = 0; i < txmap->dm_nsegs; i++) {
   1864  1.67.2.1  christos 		addr = txmap->dm_segs[i].ds_addr;
   1865  1.67.2.1  christos 		DPRINTFN(2, ("msk_encap: addr %llx\n",
   1866  1.67.2.1  christos 		    (unsigned long long)addr));
   1867  1.67.2.1  christos 		hiaddr = MSK_ADDR_HI(addr);
   1868  1.67.2.1  christos 
   1869  1.67.2.1  christos 		if (sc_if->sk_cdata.sk_tx_hiaddr != hiaddr) {
   1870  1.67.2.1  christos 			f = &sc_if->sk_rdata->sk_tx_ring[frag];
   1871  1.67.2.1  christos 			f->sk_addr = htole32(hiaddr);
   1872  1.67.2.1  christos 			f->sk_len = 0;
   1873  1.67.2.1  christos 			f->sk_ctl = 0;
   1874  1.67.2.1  christos 			if (i == 0)
   1875  1.67.2.1  christos 				f->sk_opcode = SK_Y2_BMUOPC_ADDR64;
   1876  1.67.2.1  christos 			else
   1877  1.67.2.1  christos 				f->sk_opcode = SK_Y2_BMUOPC_ADDR64 | SK_Y2_TXOPC_OWN;
   1878  1.67.2.1  christos 			sc_if->sk_cdata.sk_tx_hiaddr = hiaddr;
   1879  1.67.2.1  christos 			SK_INC(frag, MSK_TX_RING_CNT);
   1880  1.67.2.1  christos 			entries++;
   1881  1.67.2.1  christos 			DPRINTFN(10, ("%s: tx ADDR64: %#x\n",
   1882  1.67.2.1  christos 			    sc_if->sk_ethercom.ec_if.if_xname, hiaddr));
   1883  1.67.2.1  christos 		}
   1884  1.67.2.1  christos 
   1885       1.1       riz 		f = &sc_if->sk_rdata->sk_tx_ring[frag];
   1886  1.67.2.1  christos 		f->sk_addr = htole32(MSK_ADDR_LO(addr));
   1887       1.1       riz 		f->sk_len = htole16(txmap->dm_segs[i].ds_len);
   1888       1.1       riz 		f->sk_ctl = 0;
   1889  1.67.2.1  christos 		if (i == 0) {
   1890  1.67.2.1  christos 			if (hiaddr != old_hiaddr)
   1891  1.67.2.1  christos 				f->sk_opcode = SK_Y2_TXOPC_PACKET | SK_Y2_TXOPC_OWN;
   1892  1.67.2.1  christos 			else
   1893  1.67.2.1  christos 				f->sk_opcode = SK_Y2_TXOPC_PACKET;
   1894  1.67.2.1  christos 		} else
   1895       1.1       riz 			f->sk_opcode = SK_Y2_TXOPC_BUFFER | SK_Y2_TXOPC_OWN;
   1896       1.1       riz 		cur = frag;
   1897       1.1       riz 		SK_INC(frag, MSK_TX_RING_CNT);
   1898  1.67.2.1  christos 		entries++;
   1899       1.1       riz 	}
   1900  1.67.2.1  christos 	KASSERTMSG(entries == total, "entries %u total %u", entries, total);
   1901       1.1       riz 
   1902       1.1       riz 	sc_if->sk_cdata.sk_tx_chain[cur].sk_mbuf = m_head;
   1903       1.1       riz 	SIMPLEQ_REMOVE_HEAD(&sc_if->sk_txmap_head, link);
   1904       1.1       riz 
   1905       1.1       riz 	sc_if->sk_cdata.sk_tx_map[cur] = entry;
   1906       1.1       riz 	sc_if->sk_rdata->sk_tx_ring[cur].sk_ctl |= SK_Y2_TXCTL_LASTFRAG;
   1907       1.1       riz 
   1908       1.1       riz 	/* Sync descriptors before handing to chip */
   1909  1.67.2.1  christos 	MSK_CDTXSYNC(sc_if, *txidx, entries,
   1910  1.67.2.1  christos 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1911       1.1       riz 
   1912       1.1       riz 	sc_if->sk_rdata->sk_tx_ring[*txidx].sk_opcode |= SK_Y2_TXOPC_OWN;
   1913       1.1       riz 
   1914       1.1       riz 	/* Sync first descriptor to hand it off */
   1915       1.1       riz 	MSK_CDTXSYNC(sc_if, *txidx, 1,
   1916  1.67.2.1  christos 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1917       1.1       riz 
   1918  1.67.2.1  christos 	sc_if->sk_cdata.sk_tx_cnt += entries;
   1919       1.1       riz 
   1920       1.1       riz #ifdef MSK_DEBUG
   1921       1.1       riz 	if (mskdebug >= 2) {
   1922       1.1       riz 		struct msk_tx_desc *le;
   1923  1.67.2.1  christos 		uint32_t idx;
   1924       1.1       riz 		for (idx = *txidx; idx != frag; SK_INC(idx, MSK_TX_RING_CNT)) {
   1925       1.1       riz 			le = &sc_if->sk_rdata->sk_tx_ring[idx];
   1926       1.1       riz 			msk_dump_txdesc(le, idx);
   1927       1.1       riz 		}
   1928       1.1       riz 	}
   1929       1.1       riz #endif
   1930       1.1       riz 
   1931       1.1       riz 	*txidx = frag;
   1932       1.1       riz 
   1933  1.67.2.1  christos 	DPRINTFN(2, ("msk_encap: successful: %u entries\n", entries));
   1934       1.1       riz 
   1935  1.67.2.1  christos 	return 0;
   1936       1.1       riz }
   1937       1.1       riz 
   1938  1.67.2.2    martin static void
   1939       1.1       riz msk_start(struct ifnet *ifp)
   1940       1.1       riz {
   1941      1.59  jdolecek 	struct sk_if_softc	*sc_if = ifp->if_softc;
   1942      1.59  jdolecek 	struct mbuf		*m_head = NULL;
   1943  1.67.2.1  christos 	uint32_t		idx = sc_if->sk_cdata.sk_tx_prod;
   1944       1.1       riz 	int			pkts = 0;
   1945       1.1       riz 
   1946       1.1       riz 	DPRINTFN(2, ("msk_start\n"));
   1947       1.1       riz 
   1948       1.1       riz 	while (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf == NULL) {
   1949       1.1       riz 		IFQ_POLL(&ifp->if_snd, m_head);
   1950       1.1       riz 		if (m_head == NULL)
   1951       1.1       riz 			break;
   1952       1.1       riz 
   1953       1.1       riz 		/*
   1954       1.1       riz 		 * Pack the data into the transmit ring. If we
   1955       1.1       riz 		 * don't have room, set the OACTIVE flag and wait
   1956       1.1       riz 		 * for the NIC to drain the ring.
   1957       1.1       riz 		 */
   1958       1.1       riz 		if (msk_encap(sc_if, m_head, &idx)) {
   1959       1.1       riz 			ifp->if_flags |= IFF_OACTIVE;
   1960       1.1       riz 			break;
   1961       1.1       riz 		}
   1962       1.1       riz 
   1963       1.1       riz 		/* now we are committed to transmit the packet */
   1964       1.1       riz 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1965       1.1       riz 		pkts++;
   1966       1.1       riz 
   1967       1.1       riz 		/*
   1968       1.1       riz 		 * If there's a BPF listener, bounce a copy of this frame
   1969       1.1       riz 		 * to him.
   1970       1.1       riz 		 */
   1971      1.67   msaitoh 		bpf_mtap(ifp, m_head, BPF_D_OUT);
   1972       1.1       riz 	}
   1973       1.1       riz 	if (pkts == 0)
   1974       1.1       riz 		return;
   1975       1.1       riz 
   1976       1.1       riz 	/* Transmit */
   1977       1.1       riz 	if (idx != sc_if->sk_cdata.sk_tx_prod) {
   1978       1.1       riz 		sc_if->sk_cdata.sk_tx_prod = idx;
   1979       1.1       riz 		SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_PUTIDX, idx);
   1980       1.1       riz 
   1981       1.1       riz 		/* Set a timeout in case the chip goes out to lunch. */
   1982       1.1       riz 		ifp->if_timer = 5;
   1983       1.1       riz 	}
   1984       1.1       riz }
   1985       1.1       riz 
   1986  1.67.2.2    martin static void
   1987       1.1       riz msk_watchdog(struct ifnet *ifp)
   1988       1.1       riz {
   1989       1.1       riz 	struct sk_if_softc *sc_if = ifp->if_softc;
   1990       1.1       riz 
   1991       1.1       riz 	/*
   1992       1.1       riz 	 * Reclaim first as there is a possibility of losing Tx completion
   1993       1.1       riz 	 * interrupts.
   1994       1.1       riz 	 */
   1995  1.67.2.1  christos 	msk_txeof(sc_if);
   1996  1.67.2.1  christos 	if (sc_if->sk_cdata.sk_tx_cnt != 0) {
   1997  1.67.2.1  christos 		aprint_error_dev(sc_if->sk_dev, "watchdog timeout\n");
   1998  1.67.2.1  christos 
   1999  1.67.2.2    martin 		if_statinc(ifp, if_oerrors);
   2000  1.67.2.1  christos 
   2001  1.67.2.1  christos 		/* XXX Resets both ports; we shouldn't do that. */
   2002  1.67.2.1  christos 		mskc_reset(sc_if->sk_softc);
   2003  1.67.2.1  christos 		msk_reset(sc_if);
   2004  1.67.2.1  christos 		msk_init(ifp);
   2005       1.1       riz 	}
   2006       1.1       riz }
   2007       1.1       riz 
   2008      1.20     joerg static bool
   2009      1.33    dyoung mskc_suspend(device_t dv, const pmf_qual_t *qual)
   2010       1.1       riz {
   2011      1.20     joerg 	struct sk_softc *sc = device_private(dv);
   2012       1.1       riz 
   2013      1.20     joerg 	DPRINTFN(2, ("mskc_suspend\n"));
   2014       1.1       riz 
   2015       1.1       riz 	/* Turn off the 'driver is loaded' LED. */
   2016       1.1       riz 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF);
   2017       1.1       riz 
   2018      1.20     joerg 	return true;
   2019      1.20     joerg }
   2020      1.20     joerg 
   2021      1.20     joerg static bool
   2022      1.33    dyoung mskc_resume(device_t dv, const pmf_qual_t *qual)
   2023      1.20     joerg {
   2024      1.20     joerg 	struct sk_softc *sc = device_private(dv);
   2025      1.20     joerg 
   2026      1.20     joerg 	DPRINTFN(2, ("mskc_resume\n"));
   2027      1.20     joerg 
   2028      1.63  jdolecek 	mskc_reset(sc);
   2029      1.20     joerg 	CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON);
   2030      1.20     joerg 
   2031      1.20     joerg 	return true;
   2032       1.1       riz }
   2033       1.1       riz 
   2034      1.38    plunky static __inline int
   2035  1.67.2.1  christos msk_rxvalid(struct sk_softc *sc, uint32_t stat, uint32_t len)
   2036       1.1       riz {
   2037       1.1       riz 	if ((stat & (YU_RXSTAT_CRCERR | YU_RXSTAT_LONGERR |
   2038       1.1       riz 	    YU_RXSTAT_MIIERR | YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC |
   2039       1.1       riz 	    YU_RXSTAT_JABBER)) != 0 ||
   2040       1.1       riz 	    (stat & YU_RXSTAT_RXOK) != YU_RXSTAT_RXOK ||
   2041       1.1       riz 	    YU_RXSTAT_BYTES(stat) != len)
   2042  1.67.2.1  christos 		return 0;
   2043       1.1       riz 
   2044  1.67.2.1  christos 	return 1;
   2045       1.1       riz }
   2046       1.1       riz 
   2047  1.67.2.2    martin static void
   2048  1.67.2.1  christos msk_rxeof(struct sk_if_softc *sc_if, uint16_t len, uint32_t rxstat)
   2049       1.1       riz {
   2050       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
   2051       1.1       riz 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
   2052       1.1       riz 	struct mbuf		*m;
   2053  1.67.2.1  christos 	unsigned		cur, prod, tail, total_len = len;
   2054       1.1       riz 	bus_dmamap_t		dmamap;
   2055       1.1       riz 
   2056       1.1       riz 	cur = sc_if->sk_cdata.sk_rx_cons;
   2057  1.67.2.1  christos 	prod = sc_if->sk_cdata.sk_rx_prod;
   2058       1.1       riz 
   2059       1.1       riz 	/* Sync the descriptor */
   2060  1.67.2.1  christos 	MSK_CDRXSYNC(sc_if, cur, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2061  1.67.2.1  christos 
   2062  1.67.2.1  christos 	DPRINTFN(2, ("msk_rxeof: cur %u prod %u rx_cnt %u\n", cur, prod,
   2063  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_cnt));
   2064       1.1       riz 
   2065  1.67.2.1  christos 	while (prod != cur) {
   2066  1.67.2.1  christos 		tail = cur;
   2067  1.67.2.1  christos 		SK_INC(cur, MSK_RX_RING_CNT);
   2068  1.67.2.1  christos 
   2069  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_cnt--;
   2070  1.67.2.1  christos 		m = sc_if->sk_cdata.sk_rx_chain[tail].sk_mbuf;
   2071  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_chain[tail].sk_mbuf = NULL;
   2072  1.67.2.1  christos 		if (m != NULL)
   2073  1.67.2.1  christos 			break;	/* found it */
   2074  1.67.2.1  christos 	}
   2075  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cons = cur;
   2076  1.67.2.1  christos 	DPRINTFN(2, ("msk_rxeof: cur %u rx_cnt %u m %p\n", cur,
   2077  1.67.2.1  christos 		sc_if->sk_cdata.sk_rx_cnt, m));
   2078  1.67.2.1  christos 
   2079  1.67.2.1  christos 	if (m == NULL)
   2080      1.42  riastrad 		return;
   2081      1.42  riastrad 
   2082       1.1       riz 	dmamap = sc_if->sk_cdata.sk_rx_jumbo_map;
   2083  1.67.2.1  christos 
   2084       1.1       riz 	bus_dmamap_sync(sc_if->sk_softc->sc_dmatag, dmamap, 0,
   2085       1.1       riz 	    dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   2086       1.1       riz 
   2087       1.1       riz 	if (total_len < SK_MIN_FRAMELEN ||
   2088      1.19    dyoung 	    total_len > ETHER_MAX_LEN_JUMBO ||
   2089       1.1       riz 	    msk_rxvalid(sc, rxstat, total_len) == 0) {
   2090  1.67.2.2    martin 		if_statinc(ifp, if_ierrors);
   2091  1.67.2.1  christos 		m_freem(m);
   2092       1.1       riz 		return;
   2093       1.1       riz 	}
   2094       1.1       riz 
   2095  1.67.2.1  christos 	m_set_rcvif(m, ifp);
   2096  1.67.2.1  christos 	m->m_pkthdr.len = m->m_len = total_len;
   2097       1.1       riz 
   2098       1.1       riz 	/* pass it on. */
   2099      1.49     ozaki 	if_percpuq_enqueue(ifp->if_percpuq, m);
   2100       1.1       riz }
   2101       1.1       riz 
   2102  1.67.2.2    martin static void
   2103  1.67.2.1  christos msk_txeof(struct sk_if_softc *sc_if)
   2104       1.1       riz {
   2105       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
   2106       1.1       riz 	struct msk_tx_desc	*cur_tx;
   2107       1.1       riz 	struct ifnet		*ifp = &sc_if->sk_ethercom.ec_if;
   2108  1.67.2.1  christos 	uint32_t		idx, reg, sk_ctl;
   2109       1.1       riz 	struct sk_txmap_entry	*entry;
   2110       1.1       riz 
   2111       1.1       riz 	DPRINTFN(2, ("msk_txeof\n"));
   2112       1.1       riz 
   2113  1.67.2.1  christos 	if (sc_if->sk_port == SK_PORT_A)
   2114  1.67.2.1  christos 		reg = SK_STAT_BMU_TXA1_RIDX;
   2115  1.67.2.1  christos 	else
   2116  1.67.2.1  christos 		reg = SK_STAT_BMU_TXA2_RIDX;
   2117  1.67.2.1  christos 
   2118       1.1       riz 	/*
   2119       1.1       riz 	 * Go through our tx ring and free mbufs for those
   2120       1.1       riz 	 * frames that have been sent.
   2121       1.1       riz 	 */
   2122  1.67.2.1  christos 	idx = sc_if->sk_cdata.sk_tx_cons;
   2123  1.67.2.1  christos 	while (idx != sk_win_read_2(sc, reg)) {
   2124  1.67.2.1  christos 		MSK_CDTXSYNC(sc_if, idx, 1,
   2125  1.67.2.1  christos 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2126      1.17  kiyohara 
   2127  1.67.2.1  christos 		cur_tx = &sc_if->sk_rdata->sk_tx_ring[idx];
   2128       1.5   msaitoh 		sk_ctl = cur_tx->sk_ctl;
   2129       1.1       riz #ifdef MSK_DEBUG
   2130       1.1       riz 		if (mskdebug >= 2)
   2131  1.67.2.1  christos 			msk_dump_txdesc(cur_tx, idx);
   2132       1.1       riz #endif
   2133       1.5   msaitoh 		if (sk_ctl & SK_Y2_TXCTL_LASTFRAG)
   2134  1.67.2.2    martin 			if_statinc(ifp, if_opackets);
   2135  1.67.2.1  christos 		if (sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf != NULL) {
   2136  1.67.2.1  christos 			entry = sc_if->sk_cdata.sk_tx_map[idx];
   2137  1.67.2.1  christos 
   2138       1.1       riz 			bus_dmamap_sync(sc->sc_dmatag, entry->dmamap, 0,
   2139       1.1       riz 			    entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2140       1.1       riz 
   2141       1.1       riz 			bus_dmamap_unload(sc->sc_dmatag, entry->dmamap);
   2142       1.1       riz 			SIMPLEQ_INSERT_TAIL(&sc_if->sk_txmap_head, entry,
   2143       1.1       riz 					  link);
   2144  1.67.2.1  christos 			sc_if->sk_cdata.sk_tx_map[idx] = NULL;
   2145  1.67.2.3    martin 			m_freem(sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf);
   2146  1.67.2.3    martin 			sc_if->sk_cdata.sk_tx_chain[idx].sk_mbuf = NULL;
   2147       1.1       riz 		}
   2148       1.1       riz 		sc_if->sk_cdata.sk_tx_cnt--;
   2149  1.67.2.1  christos 		SK_INC(idx, MSK_TX_RING_CNT);
   2150       1.1       riz 	}
   2151  1.67.2.1  christos 	if (idx == sc_if->sk_cdata.sk_tx_cons)
   2152  1.67.2.1  christos 		return;
   2153  1.67.2.1  christos 
   2154       1.1       riz 	ifp->if_timer = sc_if->sk_cdata.sk_tx_cnt > 0 ? 5 : 0;
   2155       1.1       riz 
   2156       1.1       riz 	if (sc_if->sk_cdata.sk_tx_cnt < MSK_TX_RING_CNT - 2)
   2157       1.1       riz 		ifp->if_flags &= ~IFF_OACTIVE;
   2158       1.1       riz 
   2159  1.67.2.1  christos 	sc_if->sk_cdata.sk_tx_cons = idx;
   2160  1.67.2.1  christos }
   2161  1.67.2.1  christos 
   2162  1.67.2.2    martin static void
   2163  1.67.2.1  christos msk_fill_rx_ring(struct sk_if_softc *sc_if)
   2164  1.67.2.1  christos {
   2165  1.67.2.1  christos 	/* Make sure to not completely wrap around */
   2166  1.67.2.1  christos 	while (sc_if->sk_cdata.sk_rx_cnt < (MSK_RX_RING_CNT - 1)) {
   2167  1.67.2.1  christos 		if (msk_newbuf(sc_if,
   2168  1.67.2.1  christos 		    sc_if->sk_cdata.sk_rx_jumbo_map) == ENOBUFS) {
   2169  1.67.2.1  christos 			goto schedretry;
   2170  1.67.2.1  christos 		}
   2171  1.67.2.1  christos 	}
   2172  1.67.2.1  christos 
   2173  1.67.2.1  christos 	return;
   2174  1.67.2.1  christos 
   2175  1.67.2.1  christos schedretry:
   2176  1.67.2.1  christos 	/* Try later */
   2177  1.67.2.1  christos 	callout_schedule(&sc_if->sk_tick_rx, hz/2);
   2178  1.67.2.1  christos }
   2179  1.67.2.1  christos 
   2180  1.67.2.1  christos static void
   2181  1.67.2.1  christos msk_fill_rx_tick(void *xsc_if)
   2182  1.67.2.1  christos {
   2183  1.67.2.1  christos 	struct sk_if_softc *sc_if = xsc_if;
   2184  1.67.2.1  christos 	int s, rx_prod;
   2185  1.67.2.1  christos 
   2186  1.67.2.1  christos 	KASSERT(KERNEL_LOCKED_P());	/* XXXSMP */
   2187  1.67.2.1  christos 
   2188  1.67.2.1  christos 	s = splnet();
   2189  1.67.2.1  christos 	rx_prod = sc_if->sk_cdata.sk_rx_prod;
   2190  1.67.2.1  christos 	msk_fill_rx_ring(sc_if);
   2191  1.67.2.1  christos 	if (rx_prod != sc_if->sk_cdata.sk_rx_prod) {
   2192  1.67.2.1  christos 		SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_PUTIDX,
   2193  1.67.2.1  christos 		    sc_if->sk_cdata.sk_rx_prod);
   2194  1.67.2.1  christos 	}
   2195  1.67.2.1  christos 	splx(s);
   2196       1.1       riz }
   2197       1.1       riz 
   2198  1.67.2.2    martin static void
   2199       1.5   msaitoh msk_tick(void *xsc_if)
   2200       1.1       riz {
   2201      1.59  jdolecek 	struct sk_if_softc *sc_if = xsc_if;
   2202       1.1       riz 	struct mii_data *mii = &sc_if->sk_mii;
   2203      1.22     chris 	int s;
   2204       1.1       riz 
   2205      1.22     chris 	s = splnet();
   2206      1.62  jdolecek 	mii_tick(mii);
   2207      1.22     chris 	splx(s);
   2208      1.22     chris 
   2209       1.1       riz 	callout_schedule(&sc_if->sk_tick_ch, hz);
   2210       1.1       riz }
   2211       1.1       riz 
   2212  1.67.2.2    martin static void
   2213       1.1       riz msk_intr_yukon(struct sk_if_softc *sc_if)
   2214       1.1       riz {
   2215  1.67.2.1  christos 	uint8_t status;
   2216       1.1       riz 
   2217       1.1       riz 	status = SK_IF_READ_1(sc_if, 0, SK_GMAC_ISR);
   2218       1.1       riz 	/* RX overrun */
   2219       1.1       riz 	if ((status & SK_GMAC_INT_RX_OVER) != 0) {
   2220       1.1       riz 		SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST,
   2221       1.1       riz 		    SK_RFCTL_RX_FIFO_OVER);
   2222       1.1       riz 	}
   2223       1.1       riz 	/* TX underrun */
   2224       1.1       riz 	if ((status & SK_GMAC_INT_TX_UNDER) != 0) {
   2225       1.6   msaitoh 		SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST,
   2226       1.1       riz 		    SK_TFCTL_TX_FIFO_UNDER);
   2227       1.1       riz 	}
   2228       1.1       riz 
   2229       1.1       riz 	DPRINTFN(2, ("msk_intr_yukon status=%#x\n", status));
   2230       1.1       riz }
   2231       1.1       riz 
   2232  1.67.2.2    martin static int
   2233       1.1       riz msk_intr(void *xsc)
   2234       1.1       riz {
   2235       1.1       riz 	struct sk_softc		*sc = xsc;
   2236  1.67.2.1  christos 	struct sk_if_softc	*sc_if;
   2237       1.1       riz 	struct sk_if_softc	*sc_if0 = sc->sk_if[SK_PORT_A];
   2238       1.1       riz 	struct sk_if_softc	*sc_if1 = sc->sk_if[SK_PORT_B];
   2239       1.1       riz 	struct ifnet		*ifp0 = NULL, *ifp1 = NULL;
   2240       1.1       riz 	int			claimed = 0;
   2241  1.67.2.1  christos 	uint32_t		status;
   2242       1.1       riz 	struct msk_status_desc	*cur_st;
   2243       1.1       riz 
   2244       1.1       riz 	status = CSR_READ_4(sc, SK_Y2_ISSR2);
   2245  1.67.2.1  christos 	if (status == 0xffffffff)
   2246  1.67.2.1  christos 		return 0;
   2247       1.1       riz 	if (status == 0) {
   2248       1.1       riz 		CSR_WRITE_4(sc, SK_Y2_ICR, 2);
   2249  1.67.2.1  christos 		return 0;
   2250       1.1       riz 	}
   2251       1.1       riz 
   2252       1.1       riz 	status = CSR_READ_4(sc, SK_ISR);
   2253       1.1       riz 
   2254       1.1       riz 	if (sc_if0 != NULL)
   2255       1.1       riz 		ifp0 = &sc_if0->sk_ethercom.ec_if;
   2256       1.1       riz 	if (sc_if1 != NULL)
   2257       1.1       riz 		ifp1 = &sc_if1->sk_ethercom.ec_if;
   2258       1.1       riz 
   2259       1.1       riz 	if (sc_if0 && (status & SK_Y2_IMR_MAC1) &&
   2260       1.1       riz 	    (ifp0->if_flags & IFF_RUNNING)) {
   2261       1.1       riz 		msk_intr_yukon(sc_if0);
   2262       1.1       riz 	}
   2263       1.1       riz 
   2264       1.1       riz 	if (sc_if1 && (status & SK_Y2_IMR_MAC2) &&
   2265       1.1       riz 	    (ifp1->if_flags & IFF_RUNNING)) {
   2266       1.1       riz 		msk_intr_yukon(sc_if1);
   2267       1.1       riz 	}
   2268       1.1       riz 
   2269  1.67.2.1  christos 	MSK_CDSTSYNC(sc, sc->sk_status_idx,
   2270  1.67.2.1  christos 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2271  1.67.2.1  christos 	cur_st = &sc->sk_status_ring[sc->sk_status_idx];
   2272       1.5   msaitoh 
   2273  1.67.2.1  christos 	while (cur_st->sk_opcode & SK_Y2_STOPC_OWN) {
   2274  1.67.2.1  christos 		cur_st->sk_opcode &= ~SK_Y2_STOPC_OWN;
   2275  1.67.2.1  christos 		switch (cur_st->sk_opcode) {
   2276       1.1       riz 		case SK_Y2_STOPC_RXSTAT:
   2277  1.67.2.1  christos 			sc_if = sc->sk_if[cur_st->sk_link & 0x01];
   2278  1.67.2.1  christos 			if (sc_if) {
   2279  1.67.2.1  christos 				msk_rxeof(sc_if, letoh16(cur_st->sk_len),
   2280  1.67.2.1  christos 				    letoh32(cur_st->sk_status));
   2281  1.67.2.1  christos 				if (sc_if->sk_cdata.sk_rx_cnt < (MSK_RX_RING_CNT/3))
   2282  1.67.2.1  christos 					msk_fill_rx_tick(sc_if);
   2283  1.67.2.1  christos 			}
   2284       1.1       riz 			break;
   2285       1.1       riz 		case SK_Y2_STOPC_TXSTAT:
   2286       1.5   msaitoh 			if (sc_if0)
   2287  1.67.2.1  christos 				msk_txeof(sc_if0);
   2288       1.5   msaitoh 			if (sc_if1)
   2289  1.67.2.1  christos 				msk_txeof(sc_if1);
   2290       1.1       riz 			break;
   2291       1.1       riz 		default:
   2292  1.67.2.1  christos 			aprint_error("opcode=0x%x\n", cur_st->sk_opcode);
   2293       1.1       riz 			break;
   2294       1.1       riz 		}
   2295       1.1       riz 		SK_INC(sc->sk_status_idx, MSK_STATUS_RING_CNT);
   2296      1.17  kiyohara 
   2297  1.67.2.1  christos 		MSK_CDSTSYNC(sc, sc->sk_status_idx,
   2298  1.67.2.1  christos 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2299  1.67.2.1  christos 		cur_st = &sc->sk_status_ring[sc->sk_status_idx];
   2300       1.1       riz 	}
   2301       1.1       riz 
   2302       1.1       riz 	if (status & SK_Y2_IMR_BMU) {
   2303       1.1       riz 		CSR_WRITE_4(sc, SK_STAT_BMU_CSR, SK_STAT_BMU_IRQ_CLEAR);
   2304       1.1       riz 		claimed = 1;
   2305       1.1       riz 	}
   2306       1.1       riz 
   2307       1.1       riz 	CSR_WRITE_4(sc, SK_Y2_ICR, 2);
   2308       1.1       riz 
   2309  1.67.2.1  christos 	if (ifp0 != NULL && !IFQ_IS_EMPTY(&ifp0->if_snd))
   2310      1.53     ozaki 		if_schedule_deferred_start(ifp0);
   2311  1.67.2.1  christos 	if (ifp1 != NULL && !IFQ_IS_EMPTY(&ifp1->if_snd))
   2312      1.53     ozaki 		if_schedule_deferred_start(ifp1);
   2313       1.1       riz 
   2314  1.67.2.1  christos 	KASSERT(sc->rnd_attached > 0);
   2315      1.39       tls 	rnd_add_uint32(&sc->rnd_source, status);
   2316       1.1       riz 
   2317       1.1       riz 	if (sc->sk_int_mod_pending)
   2318      1.30  christos 		msk_update_int_mod(sc, 1);
   2319       1.1       riz 
   2320       1.1       riz 	return claimed;
   2321       1.1       riz }
   2322       1.1       riz 
   2323  1.67.2.2    martin static void
   2324       1.1       riz msk_init_yukon(struct sk_if_softc *sc_if)
   2325       1.1       riz {
   2326  1.67.2.1  christos 	uint32_t		v;
   2327  1.67.2.1  christos 	uint16_t		reg;
   2328       1.1       riz 	struct sk_softc		*sc;
   2329       1.1       riz 	int			i;
   2330       1.1       riz 
   2331       1.1       riz 	sc = sc_if->sk_softc;
   2332       1.1       riz 
   2333       1.1       riz 	DPRINTFN(2, ("msk_init_yukon: start: sk_csr=%#x\n",
   2334       1.1       riz 		     CSR_READ_4(sc_if->sk_softc, SK_CSR)));
   2335       1.1       riz 
   2336       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 1\n"));
   2337       1.1       riz 
   2338       1.1       riz 	DPRINTFN(3, ("msk_init_yukon: gmac_ctrl=%#x\n",
   2339       1.1       riz 		     SK_IF_READ_4(sc_if, 0, SK_GMAC_CTRL)));
   2340       1.1       riz 
   2341       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 3\n"));
   2342       1.1       riz 
   2343       1.1       riz 	/* unused read of the interrupt source register */
   2344       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 4\n"));
   2345       1.1       riz 	SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR);
   2346       1.1       riz 
   2347       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 4a\n"));
   2348       1.1       riz 	reg = SK_YU_READ_2(sc_if, YUKON_PAR);
   2349       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
   2350       1.1       riz 
   2351       1.1       riz 	/* MIB Counter Clear Mode set */
   2352      1.59  jdolecek 	reg |= YU_PAR_MIB_CLR;
   2353       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: YUKON_PAR=%#x\n", reg));
   2354       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 4b\n"));
   2355       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
   2356       1.1       riz 
   2357       1.1       riz 	/* MIB Counter Clear Mode clear */
   2358       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 5\n"));
   2359      1.59  jdolecek 	reg &= ~YU_PAR_MIB_CLR;
   2360       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_PAR, reg);
   2361       1.1       riz 
   2362       1.1       riz 	/* receive control reg */
   2363       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 7\n"));
   2364       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR);
   2365       1.1       riz 
   2366       1.6   msaitoh 	/* transmit control register */
   2367       1.6   msaitoh 	SK_YU_WRITE_2(sc_if, YUKON_TCR, (0x04 << 10));
   2368       1.6   msaitoh 
   2369       1.6   msaitoh 	/* transmit flow control register */
   2370       1.6   msaitoh 	SK_YU_WRITE_2(sc_if, YUKON_TFCR, 0xffff);
   2371       1.6   msaitoh 
   2372       1.1       riz 	/* transmit parameter register */
   2373       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 8\n"));
   2374       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) |
   2375       1.6   msaitoh 		      YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1c) | 0x04);
   2376       1.1       riz 
   2377       1.1       riz 	/* serial mode register */
   2378       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 9\n"));
   2379       1.5   msaitoh 	reg = YU_SMR_DATA_BLIND(0x1c) |
   2380       1.5   msaitoh 	      YU_SMR_MFL_VLAN |
   2381       1.5   msaitoh 	      YU_SMR_IPG_DATA(0x1e);
   2382       1.5   msaitoh 
   2383      1.56  jdolecek 	if (sc->sk_type != SK_YUKON_FE &&
   2384      1.60  jdolecek 	    sc->sk_type != SK_YUKON_FE_P)
   2385       1.5   msaitoh 		reg |= YU_SMR_MFL_JUMBO;
   2386       1.5   msaitoh 
   2387       1.5   msaitoh 	SK_YU_WRITE_2(sc_if, YUKON_SMR, reg);
   2388       1.1       riz 
   2389       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 10\n"));
   2390      1.50  pgoyette 	struct ifnet *ifp = &sc_if->sk_ethercom.ec_if;
   2391      1.50  pgoyette 	/* msk_attach calls me before ether_ifattach so check null */
   2392      1.50  pgoyette 	if (ifp != NULL && ifp->if_sadl != NULL)
   2393      1.50  pgoyette 		memcpy(sc_if->sk_enaddr, CLLADDR(ifp->if_sadl),
   2394      1.50  pgoyette 		    sizeof(sc_if->sk_enaddr));
   2395       1.1       riz 	/* Setup Yukon's address */
   2396       1.1       riz 	for (i = 0; i < 3; i++) {
   2397       1.1       riz 		/* Write Source Address 1 (unicast filter) */
   2398      1.43  christos 		SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4,
   2399       1.1       riz 			      sc_if->sk_enaddr[i * 2] |
   2400       1.1       riz 			      sc_if->sk_enaddr[i * 2 + 1] << 8);
   2401       1.1       riz 	}
   2402       1.1       riz 
   2403       1.1       riz 	for (i = 0; i < 3; i++) {
   2404       1.1       riz 		reg = sk_win_read_2(sc_if->sk_softc,
   2405       1.1       riz 				    SK_MAC1_0 + i * 2 + sc_if->sk_port * 8);
   2406       1.1       riz 		SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg);
   2407       1.1       riz 	}
   2408       1.1       riz 
   2409       1.1       riz 	/* Set promiscuous mode */
   2410       1.1       riz 	msk_setpromisc(sc_if);
   2411       1.1       riz 
   2412       1.1       riz 	/* Set multicast filter */
   2413       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 11\n"));
   2414       1.1       riz 	msk_setmulti(sc_if);
   2415       1.1       riz 
   2416       1.1       riz 	/* enable interrupt mask for counter overflows */
   2417       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: 12\n"));
   2418       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0);
   2419       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0);
   2420       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0);
   2421       1.1       riz 
   2422       1.1       riz 	/* Configure RX MAC FIFO Flush Mask */
   2423       1.1       riz 	v = YU_RXSTAT_FOFL | YU_RXSTAT_CRCERR | YU_RXSTAT_MIIERR |
   2424       1.1       riz 	    YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | YU_RXSTAT_RUNT |
   2425       1.1       riz 	    YU_RXSTAT_JABBER;
   2426       1.1       riz 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_MASK, v);
   2427       1.1       riz 
   2428       1.1       riz 	/* Configure RX MAC FIFO */
   2429       1.1       riz 	SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR);
   2430  1.67.2.3    martin 	v =  SK_RFCTL_OPERATION_ON | SK_RFCTL_FIFO_FLUSH_ON;
   2431  1.67.2.3    martin 	if ((sc->sk_type == SK_YUKON_EX) || (sc->sk_type == SK_YUKON_FE_P))
   2432  1.67.2.3    martin 		v |= SK_RFCTL_RX_OVER_ON;
   2433  1.67.2.3    martin 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_CTRL_TEST, v);
   2434  1.67.2.3    martin 
   2435  1.67.2.3    martin 	if ((sc->sk_type == SK_YUKON_FE_P) &&
   2436  1.67.2.3    martin 	    (sc->sk_rev == SK_YUKON_FE_P_REV_A0))
   2437  1.67.2.3    martin 		v = 0x178; /* Magic value */
   2438  1.67.2.3    martin 	else {
   2439  1.67.2.3    martin 		/* Increase flush threshold to 64 bytes */
   2440  1.67.2.3    martin 		v = SK_RFCTL_FIFO_THRESHOLD + 1;
   2441  1.67.2.3    martin 	}
   2442  1.67.2.3    martin 	SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_THRESHOLD, v);
   2443       1.1       riz 
   2444       1.1       riz 	/* Configure TX MAC FIFO */
   2445       1.1       riz 	SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR);
   2446       1.1       riz 	SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON);
   2447       1.1       riz 
   2448  1.67.2.3    martin 	if ((sc->sk_type == SK_YUKON_FE_P) &&
   2449  1.67.2.3    martin 	    (sc->sk_rev == SK_YUKON_FE_P_REV_A0)) {
   2450  1.67.2.3    martin 		v = SK_IF_READ_2(sc_if, 0, SK_TXMF1_END);
   2451  1.67.2.3    martin 		v &= ~SK_TXEND_WM_ON;
   2452  1.67.2.3    martin 		SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_END, v);
   2453  1.67.2.3    martin 	}
   2454  1.67.2.3    martin 
   2455       1.1       riz #if 1
   2456       1.1       riz 	SK_YU_WRITE_2(sc_if, YUKON_GPCR, YU_GPCR_TXEN | YU_GPCR_RXEN);
   2457       1.1       riz #endif
   2458       1.1       riz 	DPRINTFN(6, ("msk_init_yukon: end\n"));
   2459       1.1       riz }
   2460       1.1       riz 
   2461       1.1       riz /*
   2462       1.1       riz  * Note that to properly initialize any part of the GEnesis chip,
   2463       1.1       riz  * you first have to take it out of reset mode.
   2464       1.1       riz  */
   2465  1.67.2.2    martin static int
   2466       1.1       riz msk_init(struct ifnet *ifp)
   2467       1.1       riz {
   2468       1.1       riz 	struct sk_if_softc	*sc_if = ifp->if_softc;
   2469       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
   2470      1.15    dyoung 	int			rc = 0, s;
   2471       1.5   msaitoh 	uint32_t		imr, imtimer_ticks;
   2472       1.1       riz 
   2473       1.1       riz 
   2474       1.1       riz 	DPRINTFN(2, ("msk_init\n"));
   2475       1.1       riz 
   2476       1.1       riz 	s = splnet();
   2477       1.1       riz 
   2478       1.1       riz 	/* Cancel pending I/O and free all RX/TX buffers. */
   2479  1.67.2.1  christos 	msk_stop(ifp, 1);
   2480       1.1       riz 
   2481       1.1       riz 	/* Configure I2C registers */
   2482       1.1       riz 
   2483       1.1       riz 	/* Configure XMAC(s) */
   2484       1.1       riz 	msk_init_yukon(sc_if);
   2485      1.15    dyoung 	if ((rc = ether_mediachange(ifp)) != 0)
   2486      1.15    dyoung 		goto out;
   2487       1.1       riz 
   2488       1.1       riz 	/* Configure transmit arbiter(s) */
   2489       1.1       riz 	SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_ON);
   2490       1.1       riz #if 0
   2491  1.67.2.3    martin /*	    SK_TXARCTL_ON | SK_TXARCTL_FSYNC_ON); */
   2492       1.1       riz #endif
   2493       1.1       riz 
   2494  1.67.2.1  christos 	if (sc->sk_ramsize) {
   2495  1.67.2.1  christos 		/* Configure RAMbuffers */
   2496  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET);
   2497  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart);
   2498  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart);
   2499  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart);
   2500  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend);
   2501  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON);
   2502  1.67.2.1  christos 
   2503  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_UNRESET);
   2504  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_STORENFWD_ON);
   2505  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_START, sc_if->sk_tx_ramstart);
   2506  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_WR_PTR, sc_if->sk_tx_ramstart);
   2507  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_RD_PTR, sc_if->sk_tx_ramstart);
   2508  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_END, sc_if->sk_tx_ramend);
   2509  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_ON);
   2510  1.67.2.1  christos 	}
   2511       1.1       riz 
   2512       1.1       riz 	/* Configure BMUs */
   2513       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000016);
   2514       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000d28);
   2515       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, 0x00000080);
   2516       1.6   msaitoh 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_WM, 0x0600);	/* XXX ??? */
   2517       1.1       riz 
   2518       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000016);
   2519       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000d28);
   2520       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, 0x00000080);
   2521       1.6   msaitoh 	SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_WM, 0x0600);	/* XXX ??? */
   2522       1.1       riz 
   2523       1.1       riz 	/* Make sure the sync transmit queue is disabled. */
   2524       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET);
   2525       1.1       riz 
   2526       1.1       riz 	/* Init descriptors */
   2527       1.1       riz 	if (msk_init_rx_ring(sc_if) == ENOBUFS) {
   2528      1.30  christos 		aprint_error_dev(sc_if->sk_dev, "initialization failed: no "
   2529      1.18    cegger 		    "memory for rx buffers\n");
   2530  1.67.2.1  christos 		msk_stop(ifp, 1);
   2531       1.1       riz 		splx(s);
   2532       1.1       riz 		return ENOBUFS;
   2533       1.1       riz 	}
   2534       1.1       riz 
   2535       1.1       riz 	if (msk_init_tx_ring(sc_if) == ENOBUFS) {
   2536      1.30  christos 		aprint_error_dev(sc_if->sk_dev, "initialization failed: no "
   2537      1.18    cegger 		    "memory for tx buffers\n");
   2538  1.67.2.1  christos 		msk_stop(ifp, 1);
   2539       1.1       riz 		splx(s);
   2540       1.1       riz 		return ENOBUFS;
   2541       1.1       riz 	}
   2542       1.1       riz 
   2543       1.1       riz 	/* Set interrupt moderation if changed via sysctl. */
   2544       1.1       riz 	switch (sc->sk_type) {
   2545       1.1       riz 	case SK_YUKON_EC:
   2546       1.6   msaitoh 	case SK_YUKON_EC_U:
   2547      1.56  jdolecek 	case SK_YUKON_EX:
   2548      1.56  jdolecek 	case SK_YUKON_SUPR:
   2549      1.56  jdolecek 	case SK_YUKON_ULTRA2:
   2550      1.56  jdolecek 	case SK_YUKON_OPTIMA:
   2551      1.56  jdolecek 	case SK_YUKON_PRM:
   2552      1.56  jdolecek 	case SK_YUKON_OPTIMA2:
   2553       1.5   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_EC;
   2554       1.1       riz 		break;
   2555       1.6   msaitoh 	case SK_YUKON_FE:
   2556       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE;
   2557       1.6   msaitoh 		break;
   2558      1.60  jdolecek 	case SK_YUKON_FE_P:
   2559      1.60  jdolecek 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_FE_P;
   2560      1.60  jdolecek 		break;
   2561       1.6   msaitoh 	case SK_YUKON_XL:
   2562       1.6   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON_XL;
   2563       1.6   msaitoh 		break;
   2564       1.1       riz 	default:
   2565       1.5   msaitoh 		imtimer_ticks = SK_IMTIMER_TICKS_YUKON;
   2566       1.1       riz 	}
   2567       1.1       riz 	imr = sk_win_read_4(sc, SK_IMTIMERINIT);
   2568       1.1       riz 	if (imr != SK_IM_USECS(sc->sk_int_mod)) {
   2569       1.1       riz 		sk_win_write_4(sc, SK_IMTIMERINIT,
   2570       1.1       riz 		    SK_IM_USECS(sc->sk_int_mod));
   2571      1.30  christos 		aprint_verbose_dev(sc->sk_dev,
   2572      1.34       tnn 		    "interrupt moderation is %d us\n", sc->sk_int_mod);
   2573       1.1       riz 	}
   2574       1.1       riz 
   2575       1.1       riz 	/* Initialize prefetch engine. */
   2576       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
   2577       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000002);
   2578       1.1       riz 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_LIDX, MSK_RX_RING_CNT - 1);
   2579       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRLO,
   2580       1.1       riz 	    MSK_RX_RING_ADDR(sc_if, 0));
   2581       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_ADDRHI,
   2582  1.67.2.1  christos 	    (uint64_t)MSK_RX_RING_ADDR(sc_if, 0) >> 32);
   2583       1.1       riz 	SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000008);
   2584       1.1       riz 	SK_IF_READ_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR);
   2585       1.1       riz 
   2586       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
   2587       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000002);
   2588       1.1       riz 	SK_IF_WRITE_2(sc_if, 1, SK_TXQA1_Y2_PREF_LIDX, MSK_TX_RING_CNT - 1);
   2589       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRLO,
   2590       1.1       riz 	    MSK_TX_RING_ADDR(sc_if, 0));
   2591       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_ADDRHI,
   2592  1.67.2.1  christos 	    (uint64_t)MSK_TX_RING_ADDR(sc_if, 0) >> 32);
   2593       1.1       riz 	SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000008);
   2594       1.1       riz 	SK_IF_READ_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR);
   2595       1.1       riz 
   2596       1.1       riz 	SK_IF_WRITE_2(sc_if, 0, SK_RXQ1_Y2_PREF_PUTIDX,
   2597       1.1       riz 	    sc_if->sk_cdata.sk_rx_prod);
   2598       1.1       riz 
   2599  1.67.2.3    martin 
   2600  1.67.2.3    martin 	if ((sc->sk_type == SK_YUKON_EX) || (sc->sk_type == SK_YUKON_SUPR)) {
   2601  1.67.2.3    martin 		/* Disable flushing of non-ASF packets. */
   2602  1.67.2.3    martin 		SK_IF_WRITE_4(sc_if, 0, SK_RXMF1_CTRL_TEST,
   2603  1.67.2.3    martin 		    SK_RFCTL_RX_MACSEC_FLUSH_OFF);
   2604  1.67.2.3    martin 	}
   2605  1.67.2.3    martin 
   2606       1.1       riz 	/* Configure interrupt handling */
   2607       1.1       riz 	if (sc_if->sk_port == SK_PORT_A)
   2608       1.1       riz 		sc->sk_intrmask |= SK_Y2_INTRS1;
   2609       1.1       riz 	else
   2610       1.1       riz 		sc->sk_intrmask |= SK_Y2_INTRS2;
   2611       1.1       riz 	sc->sk_intrmask |= SK_Y2_IMR_BMU;
   2612       1.1       riz 	CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
   2613       1.1       riz 
   2614       1.1       riz 	ifp->if_flags |= IFF_RUNNING;
   2615       1.1       riz 	ifp->if_flags &= ~IFF_OACTIVE;
   2616       1.1       riz 
   2617       1.1       riz 	callout_schedule(&sc_if->sk_tick_ch, hz);
   2618       1.1       riz 
   2619      1.15    dyoung out:
   2620       1.1       riz 	splx(s);
   2621      1.15    dyoung 	return rc;
   2622       1.1       riz }
   2623       1.1       riz 
   2624  1.67.2.1  christos /*
   2625  1.67.2.1  christos  * Note: the logic of second parameter is inverted compared to OpenBSD
   2626  1.67.2.1  christos  * code, since this code uses the function as if_stop hook too.
   2627  1.67.2.1  christos  */
   2628  1.67.2.2    martin static void
   2629       1.3  christos msk_stop(struct ifnet *ifp, int disable)
   2630       1.1       riz {
   2631       1.1       riz 	struct sk_if_softc	*sc_if = ifp->if_softc;
   2632       1.1       riz 	struct sk_softc		*sc = sc_if->sk_softc;
   2633       1.1       riz 	struct sk_txmap_entry	*dma;
   2634       1.1       riz 	int			i;
   2635       1.1       riz 
   2636       1.1       riz 	DPRINTFN(2, ("msk_stop\n"));
   2637       1.1       riz 
   2638       1.1       riz 	callout_stop(&sc_if->sk_tick_ch);
   2639  1.67.2.1  christos 	callout_stop(&sc_if->sk_tick_rx);
   2640       1.1       riz 
   2641  1.67.2.1  christos 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2642       1.1       riz 
   2643       1.1       riz 	/* Stop transfer of Tx descriptors */
   2644       1.1       riz 
   2645       1.1       riz 	/* Stop transfer of Rx descriptors */
   2646       1.1       riz 
   2647  1.67.2.1  christos 	if (disable) {
   2648  1.67.2.1  christos 		/* Turn off various components of this interface. */
   2649  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET);
   2650  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET);
   2651  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE);
   2652  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET | SK_RBCTL_OFF);
   2653  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_BMU_CSR, SK_TXBMU_OFFLINE);
   2654  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXRBA1_CTLTST, SK_RBCTL_RESET | SK_RBCTL_OFF);
   2655  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF);
   2656  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP);
   2657  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_TXLEDCTL_COUNTER_STOP);
   2658  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF);
   2659  1.67.2.1  christos 		SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF);
   2660  1.67.2.1  christos 
   2661  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_Y2_PREF_CSR, 0x00000001);
   2662  1.67.2.1  christos 		SK_IF_WRITE_4(sc_if, 1, SK_TXQA1_Y2_PREF_CSR, 0x00000001);
   2663  1.67.2.1  christos 
   2664  1.67.2.1  christos 		/* Disable interrupts */
   2665  1.67.2.1  christos 		if (sc_if->sk_port == SK_PORT_A)
   2666  1.67.2.1  christos 			sc->sk_intrmask &= ~SK_Y2_INTRS1;
   2667  1.67.2.1  christos 		else
   2668  1.67.2.1  christos 			sc->sk_intrmask &= ~SK_Y2_INTRS2;
   2669  1.67.2.1  christos 		CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask);
   2670  1.67.2.1  christos 	}
   2671       1.1       riz 
   2672       1.1       riz 	/* Free RX and TX mbufs still in the queues. */
   2673       1.1       riz 	for (i = 0; i < MSK_RX_RING_CNT; i++) {
   2674       1.1       riz 		if (sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf != NULL) {
   2675       1.1       riz 			m_freem(sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf);
   2676       1.1       riz 			sc_if->sk_cdata.sk_rx_chain[i].sk_mbuf = NULL;
   2677       1.1       riz 		}
   2678       1.1       riz 	}
   2679       1.1       riz 
   2680  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_prod = 0;
   2681  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cons = 0;
   2682  1.67.2.1  christos 	sc_if->sk_cdata.sk_rx_cnt = 0;
   2683  1.67.2.1  christos 
   2684       1.1       riz 	for (i = 0; i < MSK_TX_RING_CNT; i++) {
   2685       1.1       riz 		if (sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf != NULL) {
   2686  1.67.2.3    martin 			dma = sc_if->sk_cdata.sk_tx_map[i];
   2687  1.67.2.3    martin 
   2688  1.67.2.3    martin 			bus_dmamap_sync(sc->sc_dmatag, dma->dmamap, 0,
   2689  1.67.2.3    martin 			    dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2690  1.67.2.3    martin 
   2691  1.67.2.3    martin 			bus_dmamap_unload(sc->sc_dmatag, dma->dmamap);
   2692  1.67.2.4    martin 
   2693       1.1       riz 			SIMPLEQ_INSERT_HEAD(&sc_if->sk_txmap_head,
   2694       1.1       riz 			    sc_if->sk_cdata.sk_tx_map[i], link);
   2695       1.1       riz 			sc_if->sk_cdata.sk_tx_map[i] = 0;
   2696  1.67.2.4    martin 
   2697  1.67.2.3    martin 			m_freem(sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf);
   2698  1.67.2.3    martin 			sc_if->sk_cdata.sk_tx_chain[i].sk_mbuf = NULL;
   2699       1.1       riz 		}
   2700       1.1       riz 	}
   2701       1.1       riz }
   2702       1.1       riz 
   2703  1.67.2.1  christos CFATTACH_DECL3_NEW(mskc, sizeof(struct sk_softc), mskc_probe, mskc_attach,
   2704  1.67.2.1  christos 	mskc_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
   2705       1.1       riz 
   2706  1.67.2.1  christos CFATTACH_DECL3_NEW(msk, sizeof(struct sk_if_softc), msk_probe, msk_attach,
   2707  1.67.2.1  christos 	msk_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
   2708       1.1       riz 
   2709       1.1       riz #ifdef MSK_DEBUG
   2710  1.67.2.2    martin static void
   2711       1.1       riz msk_dump_txdesc(struct msk_tx_desc *le, int idx)
   2712       1.1       riz {
   2713       1.1       riz #define DESC_PRINT(X)					\
   2714  1.67.2.1  christos 	if (X)						\
   2715       1.1       riz 		printf("txdesc[%d]." #X "=%#x\n",	\
   2716       1.1       riz 		       idx, X);
   2717       1.1       riz 
   2718       1.1       riz 	DESC_PRINT(letoh32(le->sk_addr));
   2719       1.1       riz 	DESC_PRINT(letoh16(le->sk_len));
   2720       1.1       riz 	DESC_PRINT(le->sk_ctl);
   2721       1.1       riz 	DESC_PRINT(le->sk_opcode);
   2722       1.1       riz #undef DESC_PRINT
   2723       1.1       riz }
   2724       1.1       riz 
   2725  1.67.2.2    martin static void
   2726       1.1       riz msk_dump_bytes(const char *data, int len)
   2727       1.1       riz {
   2728       1.1       riz 	int c, i, j;
   2729       1.1       riz 
   2730       1.1       riz 	for (i = 0; i < len; i += 16) {
   2731       1.1       riz 		printf("%08x  ", i);
   2732       1.1       riz 		c = len - i;
   2733       1.1       riz 		if (c > 16) c = 16;
   2734       1.1       riz 
   2735       1.1       riz 		for (j = 0; j < c; j++) {
   2736       1.1       riz 			printf("%02x ", data[i + j] & 0xff);
   2737       1.1       riz 			if ((j & 0xf) == 7 && j > 0)
   2738       1.1       riz 				printf(" ");
   2739       1.1       riz 		}
   2740      1.59  jdolecek 
   2741       1.1       riz 		for (; j < 16; j++)
   2742       1.1       riz 			printf("   ");
   2743       1.1       riz 		printf("  ");
   2744       1.1       riz 
   2745       1.1       riz 		for (j = 0; j < c; j++) {
   2746       1.1       riz 			int ch = data[i + j] & 0xff;
   2747       1.1       riz 			printf("%c", ' ' <= ch && ch <= '~' ? ch : ' ');
   2748       1.1       riz 		}
   2749      1.59  jdolecek 
   2750       1.1       riz 		printf("\n");
   2751      1.59  jdolecek 
   2752       1.1       riz 		if (c < 16)
   2753       1.1       riz 			break;
   2754       1.1       riz 	}
   2755       1.1       riz }
   2756       1.1       riz 
   2757  1.67.2.2    martin static void
   2758       1.1       riz msk_dump_mbuf(struct mbuf *m)
   2759       1.1       riz {
   2760       1.1       riz 	int count = m->m_pkthdr.len;
   2761       1.1       riz 
   2762       1.1       riz 	printf("m=%p, m->m_pkthdr.len=%d\n", m, m->m_pkthdr.len);
   2763       1.1       riz 
   2764       1.1       riz 	while (count > 0 && m) {
   2765       1.1       riz 		printf("m=%p, m->m_data=%p, m->m_len=%d\n",
   2766       1.1       riz 		       m, m->m_data, m->m_len);
   2767  1.67.2.1  christos 		if (mskdebug >= 4)
   2768  1.67.2.1  christos 			msk_dump_bytes(mtod(m, char *), m->m_len);
   2769       1.1       riz 
   2770       1.1       riz 		count -= m->m_len;
   2771       1.1       riz 		m = m->m_next;
   2772       1.1       riz 	}
   2773       1.1       riz }
   2774       1.1       riz #endif
   2775       1.1       riz 
   2776       1.1       riz static int
   2777       1.1       riz msk_sysctl_handler(SYSCTLFN_ARGS)
   2778       1.1       riz {
   2779       1.1       riz 	int error, t;
   2780       1.1       riz 	struct sysctlnode node;
   2781       1.1       riz 	struct sk_softc *sc;
   2782       1.1       riz 
   2783       1.1       riz 	node = *rnode;
   2784       1.1       riz 	sc = node.sysctl_data;
   2785       1.1       riz 	t = sc->sk_int_mod;
   2786       1.1       riz 	node.sysctl_data = &t;
   2787       1.1       riz 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2788       1.1       riz 	if (error || newp == NULL)
   2789       1.1       riz 		return error;
   2790       1.1       riz 
   2791       1.1       riz 	if (t < SK_IM_MIN || t > SK_IM_MAX)
   2792       1.1       riz 		return EINVAL;
   2793       1.1       riz 
   2794       1.1       riz 	/* update the softc with sysctl-changed value, and mark
   2795       1.1       riz 	   for hardware update */
   2796       1.1       riz 	sc->sk_int_mod = t;
   2797       1.1       riz 	sc->sk_int_mod_pending = 1;
   2798       1.1       riz 	return 0;
   2799       1.1       riz }
   2800       1.1       riz 
   2801       1.1       riz /*
   2802  1.67.2.1  christos  * Set up sysctl(3) MIB, hw.msk.* - Individual controllers will be
   2803  1.67.2.1  christos  * set up in mskc_attach()
   2804       1.1       riz  */
   2805       1.1       riz SYSCTL_SETUP(sysctl_msk, "sysctl msk subtree setup")
   2806       1.1       riz {
   2807       1.1       riz 	int rc;
   2808       1.1       riz 	const struct sysctlnode *node;
   2809       1.1       riz 
   2810       1.1       riz 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   2811       1.1       riz 	    0, CTLTYPE_NODE, "msk",
   2812       1.1       riz 	    SYSCTL_DESCR("msk interface controls"),
   2813       1.1       riz 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   2814       1.1       riz 		goto err;
   2815       1.1       riz 	}
   2816       1.1       riz 
   2817       1.1       riz 	msk_root_num = node->sysctl_num;
   2818       1.1       riz 	return;
   2819       1.1       riz 
   2820       1.1       riz err:
   2821       1.1       riz 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
   2822       1.1       riz }
   2823