Home | History | Annotate | Line # | Download | only in pci
if_kse.c revision 1.46
      1 /*	$NetBSD: if_kse.c,v 1.46 2019/12/14 04:12:49 nisimura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Micrel 8841/8842 10/100 PCI ethernet driver
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: if_kse.c,v 1.46 2019/12/14 04:12:49 nisimura Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/callout.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/errno.h>
     47 #include <sys/device.h>
     48 #include <sys/queue.h>
     49 
     50 #include <machine/endian.h>
     51 #include <sys/bus.h>
     52 #include <sys/intr.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_media.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_ether.h>
     58 #include <dev/mii/mii.h>
     59 #include <dev/mii/miivar.h>
     60 #include <net/bpf.h>
     61 
     62 #include <dev/pci/pcivar.h>
     63 #include <dev/pci/pcireg.h>
     64 #include <dev/pci/pcidevs.h>
     65 
     66 #define KSE_LINKDEBUG 1
     67 
     68 #define CSR_READ_4(sc, off) \
     69 	    bus_space_read_4(sc->sc_st, sc->sc_sh, off)
     70 #define CSR_WRITE_4(sc, off, val) \
     71 	    bus_space_write_4(sc->sc_st, sc->sc_sh, off, val)
     72 #define CSR_READ_2(sc, off) \
     73 	    bus_space_read_2((sc)->sc_st, (sc)->sc_sh, (off))
     74 #define CSR_WRITE_2(sc, off, val) \
     75 	    bus_space_write_2((sc)->sc_st, (sc)->sc_sh, (off), (val))
     76 
     77 #define MDTXC	0x000	/* DMA transmit control */
     78 #define MDRXC	0x004	/* DMA receive control */
     79 #define MDTSC	0x008	/* DMA transmit start */
     80 #define MDRSC	0x00c	/* DMA receive start */
     81 #define TDLB	0x010	/* transmit descriptor list base */
     82 #define RDLB	0x014	/* receive descriptor list base */
     83 #define MTR0	0x020	/* multicast table 31:0 */
     84 #define MTR1	0x024	/* multicast table 63:32 */
     85 #define INTEN	0x028	/* interrupt enable */
     86 #define INTST	0x02c	/* interrupt status */
     87 #define MAAL0	0x080	/* additional MAC address 0 low */
     88 #define MAAH0	0x084	/* additional MAC address 0 high */
     89 #define MARL	0x200	/* MAC address low */
     90 #define MARM	0x202	/* MAC address middle */
     91 #define MARH	0x204	/* MAC address high */
     92 #define GRR	0x216	/* global reset */
     93 #define SIDER	0x400	/* switch ID and function enable */
     94 #define SGCR3	0x406	/* switch function control 3 */
     95 #define  CR3_USEHDX	(1U<<6)	/* use half-duplex 8842 host port */
     96 #define  CR3_USEFC	(1U<<5) /* use flowcontrol 8842 host port */
     97 #define IACR	0x4a0	/* indirect access control */
     98 #define IADR1	0x4a2	/* indirect access data 66:63 */
     99 #define IADR2	0x4a4	/* indirect access data 47:32 */
    100 #define IADR3	0x4a6	/* indirect access data 63:48 */
    101 #define IADR4	0x4a8	/* indirect access data 15:0 */
    102 #define IADR5	0x4aa	/* indirect access data 31:16 */
    103 #define P1CR4	0x512	/* port 1 control 4 */
    104 #define P1SR	0x514	/* port 1 status */
    105 #define P2CR4	0x532	/* port 2 control 4 */
    106 #define P2SR	0x534	/* port 2 status */
    107 #define  PxCR_STARTNEG	(1U<<9)		/* restart auto negotiation */
    108 #define  PxCR_AUTOEN	(1U<<7)		/* auto negotiation enable */
    109 #define  PxCR_SPD100	(1U<<6)		/* force speed 100 */
    110 #define  PxCR_USEFDX	(1U<<5)		/* force full duplex */
    111 #define  PxCR_USEFC	(1U<<4)		/* advertise pause flow control */
    112 #define  PxSR_ACOMP	(1U<<6)		/* auto negotiation completed */
    113 #define  PxSR_SPD100	(1U<<10)	/* speed is 100Mbps */
    114 #define  PxSR_FDX	(1U<<9)		/* full duplex */
    115 #define  PxSR_LINKUP	(1U<<5)		/* link is good */
    116 #define  PxSR_RXFLOW	(1U<<12)	/* receive flow control active */
    117 #define  PxSR_TXFLOW	(1U<<11)	/* transmit flow control active */
    118 #define P1VIDCR	0x504	/* port 1 vtag */
    119 #define P2VIDCR	0x524	/* port 2 vtag */
    120 #define P3VIDCR	0x544	/* 8842 host vtag */
    121 
    122 #define TXC_BS_MSK	0x3f000000	/* burst size */
    123 #define TXC_BS_SFT	(24)		/* 1,2,4,8,16,32 or 0 for unlimited */
    124 #define TXC_UCG		(1U<<18)	/* generate UDP checksum */
    125 #define TXC_TCG		(1U<<17)	/* generate TCP checksum */
    126 #define TXC_ICG		(1U<<16)	/* generate IP checksum */
    127 #define TXC_FCE		(1U<<9)		/* generate PAUSE to moderate Rx lvl */
    128 #define TXC_EP		(1U<<2)		/* enable automatic padding */
    129 #define TXC_AC		(1U<<1)		/* add CRC to frame */
    130 #define TXC_TEN		(1)		/* enable DMA to run */
    131 
    132 #define RXC_BS_MSK	0x3f000000	/* burst size */
    133 #define RXC_BS_SFT	(24)		/* 1,2,4,8,16,32 or 0 for unlimited */
    134 #define RXC_IHAE	(1U<<19)	/* IP header alignment enable */
    135 #define RXC_UCC		(1U<<18)	/* run UDP checksum */
    136 #define RXC_TCC		(1U<<17)	/* run TDP checksum */
    137 #define RXC_ICC		(1U<<16)	/* run IP checksum */
    138 #define RXC_FCE		(1U<<9)		/* accept PAUSE to throttle Tx */
    139 #define RXC_RB		(1U<<6)		/* receive broadcast frame */
    140 #define RXC_RM		(1U<<5)		/* receive all multicast (inc. RB) */
    141 #define RXC_RU		(1U<<4)		/* receive 16 additional unicasts */
    142 #define RXC_RE		(1U<<3)		/* accept error frame */
    143 #define RXC_RA		(1U<<2)		/* receive all frame */
    144 #define RXC_MHTE	(1U<<1)		/* use multicast hash table */
    145 #define RXC_REN		(1)		/* enable DMA to run */
    146 
    147 #define INT_DMLCS	(1U<<31)	/* link status change */
    148 #define INT_DMTS	(1U<<30)	/* sending desc. has posted Tx done */
    149 #define INT_DMRS	(1U<<29)	/* frame was received */
    150 #define INT_DMRBUS	(1U<<27)	/* Rx descriptor pool is full */
    151 #define INT_DMxPSS	(3U<<25)	/* 26:25 DMA Tx/Rx have stopped */
    152 
    153 #define T0_OWN		(1U<<31)	/* desc is ready to Tx */
    154 
    155 #define R0_OWN		(1U<<31)	/* desc is empty */
    156 #define R0_FS		(1U<<30)	/* first segment of frame */
    157 #define R0_LS		(1U<<29)	/* last segment of frame */
    158 #define R0_IPE		(1U<<28)	/* IP checksum error */
    159 #define R0_TCPE		(1U<<27)	/* TCP checksum error */
    160 #define R0_UDPE		(1U<<26)	/* UDP checksum error */
    161 #define R0_ES		(1U<<25)	/* error summary */
    162 #define R0_MF		(1U<<24)	/* multicast frame */
    163 #define R0_SPN		0x00300000	/* 21:20 switch port 1/2 */
    164 #define R0_ALIGN	0x00300000	/* 21:20 (KSZ8692P) Rx align amount */
    165 #define R0_RE		(1U<<19)	/* MII reported error */
    166 #define R0_TL		(1U<<18)	/* frame too long, beyond 1518 */
    167 #define R0_RF		(1U<<17)	/* damaged runt frame */
    168 #define R0_CE		(1U<<16)	/* CRC error */
    169 #define R0_FT		(1U<<15)	/* frame type */
    170 #define R0_FL_MASK	0x7ff		/* frame length 10:0 */
    171 
    172 #define T1_IC		(1U<<31)	/* post interrupt on complete */
    173 #define T1_FS		(1U<<30)	/* first segment of frame */
    174 #define T1_LS		(1U<<29)	/* last segment of frame */
    175 #define T1_IPCKG	(1U<<28)	/* generate IP checksum */
    176 #define T1_TCPCKG	(1U<<27)	/* generate TCP checksum */
    177 #define T1_UDPCKG	(1U<<26)	/* generate UDP checksum */
    178 #define T1_TER		(1U<<25)	/* end of ring */
    179 #define T1_SPN		0x00300000	/* 21:20 switch port 1/2 */
    180 #define T1_TBS_MASK	0x7ff		/* segment size 10:0 */
    181 
    182 #define R1_RER		(1U<<25)	/* end of ring */
    183 #define R1_RBS_MASK	0x7fc		/* segment size 10:0 */
    184 
    185 #define KSE_NTXSEGS		16
    186 #define KSE_TXQUEUELEN		64
    187 #define KSE_TXQUEUELEN_MASK	(KSE_TXQUEUELEN - 1)
    188 #define KSE_TXQUEUE_GC		(KSE_TXQUEUELEN / 4)
    189 #define KSE_NTXDESC		256
    190 #define KSE_NTXDESC_MASK	(KSE_NTXDESC - 1)
    191 #define KSE_NEXTTX(x)		(((x) + 1) & KSE_NTXDESC_MASK)
    192 #define KSE_NEXTTXS(x)		(((x) + 1) & KSE_TXQUEUELEN_MASK)
    193 
    194 #define KSE_NRXDESC		64
    195 #define KSE_NRXDESC_MASK	(KSE_NRXDESC - 1)
    196 #define KSE_NEXTRX(x)		(((x) + 1) & KSE_NRXDESC_MASK)
    197 
    198 struct tdes {
    199 	uint32_t t0, t1, t2, t3;
    200 };
    201 
    202 struct rdes {
    203 	uint32_t r0, r1, r2, r3;
    204 };
    205 
    206 struct kse_control_data {
    207 	struct tdes kcd_txdescs[KSE_NTXDESC];
    208 	struct rdes kcd_rxdescs[KSE_NRXDESC];
    209 };
    210 #define KSE_CDOFF(x)		offsetof(struct kse_control_data, x)
    211 #define KSE_CDTXOFF(x)		KSE_CDOFF(kcd_txdescs[(x)])
    212 #define KSE_CDRXOFF(x)		KSE_CDOFF(kcd_rxdescs[(x)])
    213 
    214 struct kse_txsoft {
    215 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    216 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    217 	int txs_firstdesc;		/* first descriptor in packet */
    218 	int txs_lastdesc;		/* last descriptor in packet */
    219 	int txs_ndesc;			/* # of descriptors used */
    220 };
    221 
    222 struct kse_rxsoft {
    223 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    224 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    225 };
    226 
    227 struct kse_softc {
    228 	device_t sc_dev;		/* generic device information */
    229 	bus_space_tag_t sc_st;		/* bus space tag */
    230 	bus_space_handle_t sc_sh;	/* bus space handle */
    231 	bus_size_t sc_memsize;		/* csr map size */
    232 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    233 	pci_chipset_tag_t sc_pc;	/* PCI chipset tag */
    234 	struct ethercom sc_ethercom;	/* Ethernet common data */
    235 	void *sc_ih;			/* interrupt cookie */
    236 
    237 	struct mii_data sc_mii;		/* mii 8841 */
    238 	struct ifmedia sc_media;	/* ifmedia 8842 */
    239 	int sc_flowflags;		/* 802.3x PAUSE flow control */
    240 
    241 	callout_t  sc_tick_ch;		/* MII tick callout */
    242 	callout_t  sc_stat_ch;		/* statistics counter callout */
    243 
    244 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    245 #define sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    246 
    247 	struct kse_control_data *sc_control_data;
    248 #define sc_txdescs	sc_control_data->kcd_txdescs
    249 #define sc_rxdescs	sc_control_data->kcd_rxdescs
    250 
    251 	struct kse_txsoft sc_txsoft[KSE_TXQUEUELEN];
    252 	struct kse_rxsoft sc_rxsoft[KSE_NRXDESC];
    253 	int sc_txfree;			/* number of free Tx descriptors */
    254 	int sc_txnext;			/* next ready Tx descriptor */
    255 	int sc_txsfree;			/* number of free Tx jobs */
    256 	int sc_txsnext;			/* next ready Tx job */
    257 	int sc_txsdirty;		/* dirty Tx jobs */
    258 	int sc_rxptr;			/* next ready Rx descriptor/descsoft */
    259 
    260 	uint32_t sc_txc, sc_rxc;
    261 	uint32_t sc_t1csum;
    262 	int sc_mcsum;
    263 	uint32_t sc_inten;
    264 
    265 	uint32_t sc_chip;
    266 	uint8_t sc_altmac[16][ETHER_ADDR_LEN];
    267 	uint16_t sc_vlan[16];
    268 
    269 #ifdef KSE_EVENT_COUNTERS
    270 	struct ksext {
    271 		char evcntname[3][8];
    272 		struct evcnt pev[3][34];
    273 	} sc_ext;			/* switch statistics */
    274 #endif
    275 };
    276 
    277 #define KSE_CDTXADDR(sc, x)	((sc)->sc_cddma + KSE_CDTXOFF((x)))
    278 #define KSE_CDRXADDR(sc, x)	((sc)->sc_cddma + KSE_CDRXOFF((x)))
    279 
    280 #define KSE_CDTXSYNC(sc, x, n, ops)					\
    281 do {									\
    282 	int __x, __n;							\
    283 									\
    284 	__x = (x);							\
    285 	__n = (n);							\
    286 									\
    287 	/* If it will wrap around, sync to the end of the ring. */	\
    288 	if ((__x + __n) > KSE_NTXDESC) {				\
    289 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
    290 		    KSE_CDTXOFF(__x), sizeof(struct tdes) *		\
    291 		    (KSE_NTXDESC - __x), (ops));			\
    292 		__n -= (KSE_NTXDESC - __x);				\
    293 		__x = 0;						\
    294 	}								\
    295 									\
    296 	/* Now sync whatever is left. */				\
    297 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    298 	    KSE_CDTXOFF(__x), sizeof(struct tdes) * __n, (ops));	\
    299 } while (/*CONSTCOND*/0)
    300 
    301 #define KSE_CDRXSYNC(sc, x, ops)					\
    302 do {									\
    303 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    304 	    KSE_CDRXOFF((x)), sizeof(struct rdes), (ops));		\
    305 } while (/*CONSTCOND*/0)
    306 
    307 #define KSE_INIT_RXDESC(sc, x)						\
    308 do {									\
    309 	struct kse_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
    310 	struct rdes *__rxd = &(sc)->sc_rxdescs[(x)];			\
    311 	struct mbuf *__m = __rxs->rxs_mbuf;				\
    312 									\
    313 	__m->m_data = __m->m_ext.ext_buf;				\
    314 	__rxd->r2 = __rxs->rxs_dmamap->dm_segs[0].ds_addr;		\
    315 	__rxd->r1 = R1_RBS_MASK /* __m->m_ext.ext_size */;		\
    316 	__rxd->r0 = R0_OWN;						\
    317 	KSE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \
    318 } while (/*CONSTCOND*/0)
    319 
    320 u_int kse_burstsize = 8;	/* DMA burst length tuning knob */
    321 
    322 #ifdef KSEDIAGNOSTIC
    323 u_int kse_monitor_rxintr;	/* fragmented UDP csum HW bug hook */
    324 #endif
    325 
    326 static int kse_match(device_t, cfdata_t, void *);
    327 static void kse_attach(device_t, device_t, void *);
    328 
    329 CFATTACH_DECL_NEW(kse, sizeof(struct kse_softc),
    330     kse_match, kse_attach, NULL, NULL);
    331 
    332 static int kse_ioctl(struct ifnet *, u_long, void *);
    333 static void kse_start(struct ifnet *);
    334 static void kse_watchdog(struct ifnet *);
    335 static int kse_init(struct ifnet *);
    336 static void kse_stop(struct ifnet *, int);
    337 static void kse_reset(struct kse_softc *);
    338 static void kse_set_filter(struct kse_softc *);
    339 static int add_rxbuf(struct kse_softc *, int);
    340 static void rxdrain(struct kse_softc *);
    341 static int kse_intr(void *);
    342 static void rxintr(struct kse_softc *);
    343 static void txreap(struct kse_softc *);
    344 static void lnkchg(struct kse_softc *);
    345 static int kse_ifmedia_upd(struct ifnet *);
    346 static void kse_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    347 static void nopifmedia_sts(struct ifnet *, struct ifmediareq *);
    348 static void phy_tick(void *);
    349 int kse_mii_readreg(device_t, int, int, uint16_t *);
    350 int kse_mii_writereg(device_t, int, int, uint16_t);
    351 void kse_mii_statchg(struct ifnet *);
    352 #ifdef KSE_EVENT_COUNTERS
    353 static void stat_tick(void *);
    354 static void zerostats(struct kse_softc *);
    355 #endif
    356 
    357 static int
    358 kse_match(device_t parent, cfdata_t match, void *aux)
    359 {
    360 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    361 
    362 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MICREL &&
    363 	     (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MICREL_KSZ8842 ||
    364 	      PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MICREL_KSZ8841) &&
    365 	    PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK)
    366 		return 1;
    367 
    368 	return 0;
    369 }
    370 
    371 static void
    372 kse_attach(device_t parent, device_t self, void *aux)
    373 {
    374 	struct kse_softc *sc = device_private(self);
    375 	struct pci_attach_args *pa = aux;
    376 	pci_chipset_tag_t pc = pa->pa_pc;
    377 	pci_intr_handle_t ih;
    378 	const char *intrstr;
    379 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    380 	struct mii_data * const mii = &sc->sc_mii;
    381 	struct ifmedia *ifm;
    382 	uint8_t enaddr[ETHER_ADDR_LEN];
    383 	bus_dma_segment_t seg;
    384 	int i, error, nseg;
    385 	char intrbuf[PCI_INTRSTR_LEN];
    386 
    387 	aprint_normal(": Micrel KSZ%04x Ethernet (rev. 0x%02x)\n",
    388 	    PCI_PRODUCT(pa->pa_id), PCI_REVISION(pa->pa_class));
    389 
    390 	if (pci_mapreg_map(pa, 0x10,
    391 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    392 	    0, &sc->sc_st, &sc->sc_sh, NULL, &sc->sc_memsize) != 0) {
    393 		aprint_error_dev(self, "unable to map device registers\n");
    394 		return;
    395 	}
    396 
    397 	/* Make sure bus mastering is enabled. */
    398 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    399 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    400 	    PCI_COMMAND_MASTER_ENABLE);
    401 
    402 	/* Power up chip if necessary. */
    403 	if ((error = pci_activate(pc, pa->pa_tag, self, NULL))
    404 	    && error != EOPNOTSUPP) {
    405 		aprint_error_dev(self, "cannot activate %d\n", error);
    406 		return;
    407 	}
    408 
    409 	/* Map and establish our interrupt. */
    410 	if (pci_intr_map(pa, &ih)) {
    411 		aprint_error_dev(self, "unable to map interrupt\n");
    412 		return;
    413 	}
    414 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    415 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, kse_intr, sc,
    416 	    device_xname(self));
    417 	if (sc->sc_ih == NULL) {
    418 		aprint_error_dev(self, "unable to establish interrupt");
    419 		if (intrstr != NULL)
    420 			aprint_error(" at %s", intrstr);
    421 		aprint_error("\n");
    422 		return;
    423 	}
    424 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    425 
    426 	sc->sc_dev = self;
    427 	sc->sc_dmat = pa->pa_dmat;
    428 	sc->sc_pc = pa->pa_pc;
    429 	sc->sc_chip = PCI_PRODUCT(pa->pa_id);
    430 
    431 	/*
    432 	 * Read the Ethernet address from the EEPROM.
    433 	 */
    434 	i = CSR_READ_2(sc, MARL);
    435 	enaddr[5] = i;
    436 	enaddr[4] = i >> 8;
    437 	i = CSR_READ_2(sc, MARM);
    438 	enaddr[3] = i;
    439 	enaddr[2] = i >> 8;
    440 	i = CSR_READ_2(sc, MARH);
    441 	enaddr[1] = i;
    442 	enaddr[0] = i >> 8;
    443 	aprint_normal_dev(self,
    444 	    "Ethernet address %s\n", ether_sprintf(enaddr));
    445 
    446 	/*
    447 	 * Enable chip function.
    448 	 */
    449 	CSR_WRITE_2(sc, SIDER, 1);
    450 
    451 	/*
    452 	 * Allocate the control data structures, and create and load the
    453 	 * DMA map for it.
    454 	 */
    455 	error = bus_dmamem_alloc(sc->sc_dmat,
    456 	    sizeof(struct kse_control_data), PAGE_SIZE, 0, &seg, 1, &nseg, 0);
    457 	if (error != 0) {
    458 		aprint_error_dev(self,
    459 		    "unable to allocate control data, error = %d\n", error);
    460 		goto fail_0;
    461 	}
    462 	error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    463 	    sizeof(struct kse_control_data), (void **)&sc->sc_control_data,
    464 	    BUS_DMA_COHERENT);
    465 	if (error != 0) {
    466 		aprint_error_dev(self,
    467 		    "unable to map control data, error = %d\n", error);
    468 		goto fail_1;
    469 	}
    470 	error = bus_dmamap_create(sc->sc_dmat,
    471 	    sizeof(struct kse_control_data), 1,
    472 	    sizeof(struct kse_control_data), 0, 0, &sc->sc_cddmamap);
    473 	if (error != 0) {
    474 		aprint_error_dev(self,
    475 		    "unable to create control data DMA map, "
    476 		    "error = %d\n", error);
    477 		goto fail_2;
    478 	}
    479 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    480 	    sc->sc_control_data, sizeof(struct kse_control_data), NULL, 0);
    481 	if (error != 0) {
    482 		aprint_error_dev(self,
    483 		    "unable to load control data DMA map, error = %d\n",
    484 		    error);
    485 		goto fail_3;
    486 	}
    487 	for (i = 0; i < KSE_TXQUEUELEN; i++) {
    488 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    489 		    KSE_NTXSEGS, MCLBYTES, 0, 0,
    490 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    491 			aprint_error_dev(self,
    492 			    "unable to create tx DMA map %d, error = %d\n",
    493 			    i, error);
    494 			goto fail_4;
    495 		}
    496 	}
    497 	for (i = 0; i < KSE_NRXDESC; i++) {
    498 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    499 		    1, MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    500 			aprint_error_dev(self,
    501 			    "unable to create rx DMA map %d, error = %d\n",
    502 			    i, error);
    503 			goto fail_5;
    504 		}
    505 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    506 	}
    507 
    508 	callout_init(&sc->sc_tick_ch, 0);
    509 	callout_init(&sc->sc_stat_ch, 0);
    510 	callout_setfunc(&sc->sc_tick_ch, phy_tick, sc);
    511 #ifdef KSE_EVENT_COUNTERS
    512 	callout_setfunc(&sc->sc_stat_ch, stat_tick, sc);
    513 #endif
    514 
    515 	mii->mii_ifp = ifp;
    516 	mii->mii_readreg = kse_mii_readreg;
    517 	mii->mii_writereg = kse_mii_writereg;
    518 	mii->mii_statchg = kse_mii_statchg;
    519 
    520 	/* Initialize ifmedia structures. */
    521 	sc->sc_flowflags = 0;
    522 	if (sc->sc_chip == 0x8841) {
    523 		/* use port 1 builtin PHY as index 1 device */
    524 		sc->sc_ethercom.ec_mii = mii;
    525 		ifm = &mii->mii_media;
    526 		ifmedia_init(ifm, 0, kse_ifmedia_upd, kse_ifmedia_sts);
    527 		mii_attach(sc->sc_dev, mii, 0xffffffff, 1 /* PHY1 */,
    528 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
    529 		if (LIST_FIRST(&mii->mii_phys) == NULL) {
    530 			ifmedia_add(ifm, IFM_ETHER | IFM_NONE, 0, NULL);
    531 			ifmedia_set(ifm, IFM_ETHER | IFM_NONE);
    532 		} else
    533 			ifmedia_set(ifm, IFM_ETHER | IFM_AUTO);
    534 	} else {
    535 		/*
    536 		 * pretend 100FDX w/ no alternative media selection.
    537 		 * 8842 MAC is tied with a builtin 3 port switch. It can do
    538 		 * 4 degree priotised rate control over either of tx/rx
    539 		 * direction for any of ports, respectively. Tough, this
    540 		 * driver leaves the rate unlimited intending 100Mbps maximum.
    541 		 * 2 external ports behave in AN mode and this driver provides
    542 		 * no mean to manipulate and see their operational details.
    543 		 */
    544 		sc->sc_ethercom.ec_ifmedia = ifm = &sc->sc_media;
    545 		ifmedia_init(ifm, 0, NULL, nopifmedia_sts);
    546 		ifmedia_add(ifm, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
    547 		ifmedia_set(ifm, IFM_ETHER | IFM_100_TX | IFM_FDX);
    548 
    549 		aprint_normal_dev(self,
    550 		    "10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto\n");
    551 	}
    552 	ifm->ifm_media = ifm->ifm_cur->ifm_media; /* as if user has requested */
    553 
    554 
    555 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    556 	ifp->if_softc = sc;
    557 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    558 	ifp->if_ioctl = kse_ioctl;
    559 	ifp->if_start = kse_start;
    560 	ifp->if_watchdog = kse_watchdog;
    561 	ifp->if_init = kse_init;
    562 	ifp->if_stop = kse_stop;
    563 	IFQ_SET_READY(&ifp->if_snd);
    564 
    565 	/*
    566 	 * capable of 802.1Q VLAN-sized frames and hw assisted tagging.
    567 	 * can do IPv4, TCPv4, and UDPv4 checksums in hardware.
    568 	 */
    569 	sc->sc_ethercom.ec_capabilities = ETHERCAP_VLAN_MTU;
    570 	ifp->if_capabilities =
    571 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    572 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    573 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    574 
    575 	if_attach(ifp);
    576 	if_deferred_start_init(ifp, NULL);
    577 	ether_ifattach(ifp, enaddr);
    578 
    579 #ifdef KSE_EVENT_COUNTERS
    580 	int p = (sc->sc_chip == 0x8842) ? 3 : 1;
    581 	for (i = 0; i < p; i++) {
    582 		struct ksext *ee = &sc->sc_ext;
    583 		snprintf(ee->evcntname[i], sizeof(ee->evcntname[i]),
    584 		    "%s.%d", device_xname(sc->sc_dev), i+1);
    585 		evcnt_attach_dynamic(&ee->pev[i][0], EVCNT_TYPE_MISC,
    586 		    NULL, ee->evcntname[i], "RxLoPriotyByte");
    587 		evcnt_attach_dynamic(&ee->pev[i][1], EVCNT_TYPE_MISC,
    588 		    NULL, ee->evcntname[i], "RxHiPriotyByte");
    589 		evcnt_attach_dynamic(&ee->pev[i][2], EVCNT_TYPE_MISC,
    590 		    NULL, ee->evcntname[i], "RxUndersizePkt");
    591 		evcnt_attach_dynamic(&ee->pev[i][3], EVCNT_TYPE_MISC,
    592 		    NULL, ee->evcntname[i], "RxFragments");
    593 		evcnt_attach_dynamic(&ee->pev[i][4], EVCNT_TYPE_MISC,
    594 		    NULL, ee->evcntname[i], "RxOversize");
    595 		evcnt_attach_dynamic(&ee->pev[i][5], EVCNT_TYPE_MISC,
    596 		    NULL, ee->evcntname[i], "RxJabbers");
    597 		evcnt_attach_dynamic(&ee->pev[i][6], EVCNT_TYPE_MISC,
    598 		    NULL, ee->evcntname[i], "RxSymbolError");
    599 		evcnt_attach_dynamic(&ee->pev[i][7], EVCNT_TYPE_MISC,
    600 		    NULL, ee->evcntname[i], "RxCRCError");
    601 		evcnt_attach_dynamic(&ee->pev[i][8], EVCNT_TYPE_MISC,
    602 		    NULL, ee->evcntname[i], "RxAlignmentError");
    603 		evcnt_attach_dynamic(&ee->pev[i][9], EVCNT_TYPE_MISC,
    604 		    NULL, ee->evcntname[i], "RxControl8808Pkts");
    605 		evcnt_attach_dynamic(&ee->pev[i][10], EVCNT_TYPE_MISC,
    606 		    NULL, ee->evcntname[i], "RxPausePkts");
    607 		evcnt_attach_dynamic(&ee->pev[i][11], EVCNT_TYPE_MISC,
    608 		    NULL, ee->evcntname[i], "RxBroadcast");
    609 		evcnt_attach_dynamic(&ee->pev[i][12], EVCNT_TYPE_MISC,
    610 		    NULL, ee->evcntname[i], "RxMulticast");
    611 		evcnt_attach_dynamic(&ee->pev[i][13], EVCNT_TYPE_MISC,
    612 		    NULL, ee->evcntname[i], "RxUnicast");
    613 		evcnt_attach_dynamic(&ee->pev[i][14], EVCNT_TYPE_MISC,
    614 		    NULL, ee->evcntname[i], "Rx64Octets");
    615 		evcnt_attach_dynamic(&ee->pev[i][15], EVCNT_TYPE_MISC,
    616 		    NULL, ee->evcntname[i], "Rx65To127Octets");
    617 		evcnt_attach_dynamic(&ee->pev[i][16], EVCNT_TYPE_MISC,
    618 		    NULL, ee->evcntname[i], "Rx128To255Octets");
    619 		evcnt_attach_dynamic(&ee->pev[i][17], EVCNT_TYPE_MISC,
    620 		    NULL, ee->evcntname[i], "Rx255To511Octets");
    621 		evcnt_attach_dynamic(&ee->pev[i][18], EVCNT_TYPE_MISC,
    622 		    NULL, ee->evcntname[i], "Rx512To1023Octets");
    623 		evcnt_attach_dynamic(&ee->pev[i][19], EVCNT_TYPE_MISC,
    624 		    NULL, ee->evcntname[i], "Rx1024To1522Octets");
    625 		evcnt_attach_dynamic(&ee->pev[i][20], EVCNT_TYPE_MISC,
    626 		    NULL, ee->evcntname[i], "TxLoPriotyByte");
    627 		evcnt_attach_dynamic(&ee->pev[i][21], EVCNT_TYPE_MISC,
    628 		    NULL, ee->evcntname[i], "TxHiPriotyByte");
    629 		evcnt_attach_dynamic(&ee->pev[i][22], EVCNT_TYPE_MISC,
    630 		    NULL, ee->evcntname[i], "TxLateCollision");
    631 		evcnt_attach_dynamic(&ee->pev[i][23], EVCNT_TYPE_MISC,
    632 		    NULL, ee->evcntname[i], "TxPausePkts");
    633 		evcnt_attach_dynamic(&ee->pev[i][24], EVCNT_TYPE_MISC,
    634 		    NULL, ee->evcntname[i], "TxBroadcastPkts");
    635 		evcnt_attach_dynamic(&ee->pev[i][25], EVCNT_TYPE_MISC,
    636 		    NULL, ee->evcntname[i], "TxMulticastPkts");
    637 		evcnt_attach_dynamic(&ee->pev[i][26], EVCNT_TYPE_MISC,
    638 		    NULL, ee->evcntname[i], "TxUnicastPkts");
    639 		evcnt_attach_dynamic(&ee->pev[i][27], EVCNT_TYPE_MISC,
    640 		    NULL, ee->evcntname[i], "TxDeferred");
    641 		evcnt_attach_dynamic(&ee->pev[i][28], EVCNT_TYPE_MISC,
    642 		    NULL, ee->evcntname[i], "TxTotalCollision");
    643 		evcnt_attach_dynamic(&ee->pev[i][29], EVCNT_TYPE_MISC,
    644 		    NULL, ee->evcntname[i], "TxExcessiveCollision");
    645 		evcnt_attach_dynamic(&ee->pev[i][30], EVCNT_TYPE_MISC,
    646 		    NULL, ee->evcntname[i], "TxSingleCollision");
    647 		evcnt_attach_dynamic(&ee->pev[i][31], EVCNT_TYPE_MISC,
    648 		    NULL, ee->evcntname[i], "TxMultipleCollision");
    649 		evcnt_attach_dynamic(&ee->pev[i][32], EVCNT_TYPE_MISC,
    650 		    NULL, ee->evcntname[i], "TxDropPkts");
    651 		evcnt_attach_dynamic(&ee->pev[i][33], EVCNT_TYPE_MISC,
    652 		    NULL, ee->evcntname[i], "RxDropPkts");
    653 	}
    654 #endif
    655 	return;
    656 
    657  fail_5:
    658 	for (i = 0; i < KSE_NRXDESC; i++) {
    659 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    660 			bus_dmamap_destroy(sc->sc_dmat,
    661 			    sc->sc_rxsoft[i].rxs_dmamap);
    662 	}
    663  fail_4:
    664 	for (i = 0; i < KSE_TXQUEUELEN; i++) {
    665 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    666 			bus_dmamap_destroy(sc->sc_dmat,
    667 			    sc->sc_txsoft[i].txs_dmamap);
    668 	}
    669 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    670  fail_3:
    671 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    672  fail_2:
    673 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    674 	    sizeof(struct kse_control_data));
    675  fail_1:
    676 	bus_dmamem_free(sc->sc_dmat, &seg, nseg);
    677  fail_0:
    678 	if (sc->sc_ih != NULL) {
    679 		pci_intr_disestablish(pc, sc->sc_ih);
    680 		sc->sc_ih = NULL;
    681 	}
    682 	if (sc->sc_memsize) {
    683 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_memsize);
    684 		sc->sc_memsize = 0;
    685 	}
    686 	return;
    687 }
    688 
    689 static int
    690 kse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    691 {
    692 	struct kse_softc *sc = ifp->if_softc;
    693 	struct ifreq *ifr = (struct ifreq *)data;
    694 	struct ifmedia *ifm;
    695 	int s, error;
    696 
    697 	s = splnet();
    698 
    699 	switch (cmd) {
    700 	case SIOCSIFMEDIA:
    701 		/* Flow control requires full-duplex mode. */
    702 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
    703 		    (ifr->ifr_media & IFM_FDX) == 0)
    704 			ifr->ifr_media &= ~IFM_ETH_FMASK;
    705 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
    706 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
    707 				/* We can do both TXPAUSE and RXPAUSE. */
    708 				ifr->ifr_media |=
    709 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
    710 			}
    711 			sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
    712 		}
    713 		ifm = (sc->sc_chip == 0x8841)
    714 		    ? &sc->sc_mii.mii_media : &sc->sc_media;
    715 		error = ifmedia_ioctl(ifp, ifr, ifm, cmd);
    716 		break;
    717 	default:
    718 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
    719 			break;
    720 
    721 		error = 0;
    722 
    723 		if (cmd == SIOCSIFCAP)
    724 			error = (*ifp->if_init)(ifp);
    725 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    726 			;
    727 		else if (ifp->if_flags & IFF_RUNNING) {
    728 			/*
    729 			 * Multicast list has changed; set the hardware filter
    730 			 * accordingly.
    731 			 */
    732 			kse_set_filter(sc);
    733 		}
    734 		break;
    735 	}
    736 
    737 	splx(s);
    738 	return error;
    739 }
    740 
    741 static int
    742 kse_init(struct ifnet *ifp)
    743 {
    744 	struct kse_softc *sc = ifp->if_softc;
    745 	uint32_t paddr;
    746 	int i, error = 0;
    747 
    748 	/* cancel pending I/O */
    749 	kse_stop(ifp, 0);
    750 
    751 	/* reset all registers but PCI configuration */
    752 	kse_reset(sc);
    753 
    754 	/* craft Tx descriptor ring */
    755 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    756 	for (i = 0, paddr = KSE_CDTXADDR(sc, 1); i < KSE_NTXDESC - 1; i++) {
    757 		sc->sc_txdescs[i].t3 = paddr;
    758 		paddr += sizeof(struct tdes);
    759 	}
    760 	sc->sc_txdescs[KSE_NTXDESC - 1].t3 = KSE_CDTXADDR(sc, 0);
    761 	KSE_CDTXSYNC(sc, 0, KSE_NTXDESC,
    762 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    763 	sc->sc_txfree = KSE_NTXDESC;
    764 	sc->sc_txnext = 0;
    765 
    766 	for (i = 0; i < KSE_TXQUEUELEN; i++)
    767 		sc->sc_txsoft[i].txs_mbuf = NULL;
    768 	sc->sc_txsfree = KSE_TXQUEUELEN;
    769 	sc->sc_txsnext = 0;
    770 	sc->sc_txsdirty = 0;
    771 
    772 	/* craft Rx descriptor ring */
    773 	memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
    774 	for (i = 0, paddr = KSE_CDRXADDR(sc, 1); i < KSE_NRXDESC - 1; i++) {
    775 		sc->sc_rxdescs[i].r3 = paddr;
    776 		paddr += sizeof(struct rdes);
    777 	}
    778 	sc->sc_rxdescs[KSE_NRXDESC - 1].r3 = KSE_CDRXADDR(sc, 0);
    779 	for (i = 0; i < KSE_NRXDESC; i++) {
    780 		if (sc->sc_rxsoft[i].rxs_mbuf == NULL) {
    781 			if ((error = add_rxbuf(sc, i)) != 0) {
    782 				aprint_error_dev(sc->sc_dev,
    783 				    "unable to allocate or map rx "
    784 				    "buffer %d, error = %d\n",
    785 				    i, error);
    786 				rxdrain(sc);
    787 				goto out;
    788 			}
    789 		}
    790 		else
    791 			KSE_INIT_RXDESC(sc, i);
    792 	}
    793 	sc->sc_rxptr = 0;
    794 
    795 	/* hand Tx/Rx rings to HW */
    796 	CSR_WRITE_4(sc, TDLB, KSE_CDTXADDR(sc, 0));
    797 	CSR_WRITE_4(sc, RDLB, KSE_CDRXADDR(sc, 0));
    798 
    799 	sc->sc_txc = TXC_TEN | TXC_EP | TXC_AC;
    800 	sc->sc_rxc = RXC_REN | RXC_RU | RXC_RB;
    801 	sc->sc_t1csum = sc->sc_mcsum = 0;
    802 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
    803 		sc->sc_rxc |= RXC_ICC;
    804 		sc->sc_mcsum |= M_CSUM_IPv4;
    805 	}
    806 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
    807 		sc->sc_txc |= TXC_ICG;
    808 		sc->sc_t1csum |= T1_IPCKG;
    809 	}
    810 	if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
    811 		sc->sc_rxc |= RXC_TCC;
    812 		sc->sc_mcsum |= M_CSUM_TCPv4;
    813 	}
    814 	if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
    815 		sc->sc_txc |= TXC_TCG;
    816 		sc->sc_t1csum |= T1_TCPCKG;
    817 	}
    818 	if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
    819 		sc->sc_rxc |= RXC_UCC;
    820 		sc->sc_mcsum |= M_CSUM_UDPv4;
    821 	}
    822 	if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
    823 		sc->sc_txc |= TXC_UCG;
    824 		sc->sc_t1csum |= T1_UDPCKG;
    825 	}
    826 	sc->sc_txc |= (kse_burstsize << TXC_BS_SFT);
    827 	sc->sc_rxc |= (kse_burstsize << RXC_BS_SFT);
    828 
    829 	if (sc->sc_chip == 0x8842) {
    830 		sc->sc_txc |= TXC_FCE;
    831 		sc->sc_rxc |= RXC_FCE;
    832 		CSR_WRITE_2(sc, SGCR3,
    833 		    CSR_READ_2(sc, SGCR3) | CR3_USEFC);
    834 	}
    835 
    836 	/* build multicast hash filter if necessary */
    837 	kse_set_filter(sc);
    838 
    839 	/* set current media */
    840 	if (sc->sc_chip == 0x8841)
    841 		(void)kse_ifmedia_upd(ifp);
    842 
    843 	/* enable transmitter and receiver */
    844 	CSR_WRITE_4(sc, MDTXC, sc->sc_txc);
    845 	CSR_WRITE_4(sc, MDRXC, sc->sc_rxc);
    846 	CSR_WRITE_4(sc, MDRSC, 1);
    847 
    848 	/* enable interrupts */
    849 	sc->sc_inten = INT_DMTS | INT_DMRS | INT_DMRBUS;
    850 	if (sc->sc_chip == 0x8841)
    851 		sc->sc_inten |= INT_DMLCS;
    852 	CSR_WRITE_4(sc, INTST, ~0);
    853 	CSR_WRITE_4(sc, INTEN, sc->sc_inten);
    854 
    855 	ifp->if_flags |= IFF_RUNNING;
    856 	ifp->if_flags &= ~IFF_OACTIVE;
    857 
    858 	if (sc->sc_chip == 0x8841) {
    859 		/* start one second timer */
    860 		callout_schedule(&sc->sc_tick_ch, hz);
    861 	}
    862 #ifdef KSE_EVENT_COUNTERS
    863 	/* start statistics gather 1 minute timer. should be tunable */
    864 	zerostats(sc);
    865 	callout_schedule(&sc->sc_stat_ch, hz * 60);
    866 #endif
    867 
    868  out:
    869 	if (error) {
    870 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    871 		ifp->if_timer = 0;
    872 		aprint_error_dev(sc->sc_dev, "interface not running\n");
    873 	}
    874 	return error;
    875 }
    876 
    877 static void
    878 kse_stop(struct ifnet *ifp, int disable)
    879 {
    880 	struct kse_softc *sc = ifp->if_softc;
    881 	struct kse_txsoft *txs;
    882 	int i;
    883 
    884 	if (sc->sc_chip == 0x8841)
    885 		callout_stop(&sc->sc_tick_ch);
    886 	callout_stop(&sc->sc_stat_ch);
    887 
    888 	sc->sc_txc &= ~TXC_TEN;
    889 	sc->sc_rxc &= ~RXC_REN;
    890 	CSR_WRITE_4(sc, MDTXC, sc->sc_txc);
    891 	CSR_WRITE_4(sc, MDRXC, sc->sc_rxc);
    892 
    893 	for (i = 0; i < KSE_TXQUEUELEN; i++) {
    894 		txs = &sc->sc_txsoft[i];
    895 		if (txs->txs_mbuf != NULL) {
    896 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
    897 			m_freem(txs->txs_mbuf);
    898 			txs->txs_mbuf = NULL;
    899 		}
    900 	}
    901 
    902 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    903 	ifp->if_timer = 0;
    904 
    905 	if (disable)
    906 		rxdrain(sc);
    907 }
    908 
    909 static void
    910 kse_reset(struct kse_softc *sc)
    911 {
    912 
    913 	/* software reset */
    914 	CSR_WRITE_2(sc, GRR, 1);
    915 	delay(1000); /* PDF does not mention the delay amount */
    916 	CSR_WRITE_2(sc, GRR, 0);
    917 
    918 	/* enable switch function */
    919 	CSR_WRITE_2(sc, SIDER, 1);
    920 }
    921 
    922 static void
    923 kse_watchdog(struct ifnet *ifp)
    924 {
    925 	struct kse_softc *sc = ifp->if_softc;
    926 
    927 	/*
    928 	 * Since we're not interrupting every packet, sweep
    929 	 * up before we report an error.
    930 	 */
    931 	txreap(sc);
    932 
    933 	if (sc->sc_txfree != KSE_NTXDESC) {
    934 		aprint_error_dev(sc->sc_dev,
    935 		    "device timeout (txfree %d txsfree %d txnext %d)\n",
    936 		    sc->sc_txfree, sc->sc_txsfree, sc->sc_txnext);
    937 		ifp->if_oerrors++;
    938 
    939 		/* Reset the interface. */
    940 		kse_init(ifp);
    941 	}
    942 	else if (ifp->if_flags & IFF_DEBUG)
    943 		aprint_error_dev(sc->sc_dev, "recovered from device timeout\n");
    944 
    945 	/* Try to get more packets going. */
    946 	kse_start(ifp);
    947 }
    948 
    949 static void
    950 kse_start(struct ifnet *ifp)
    951 {
    952 	struct kse_softc *sc = ifp->if_softc;
    953 	struct mbuf *m0, *m;
    954 	struct kse_txsoft *txs;
    955 	bus_dmamap_t dmamap;
    956 	int error, nexttx, lasttx, ofree, seg;
    957 	uint32_t tdes0;
    958 
    959 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    960 		return;
    961 
    962 	/* Remember the previous number of free descriptors. */
    963 	ofree = sc->sc_txfree;
    964 
    965 	/*
    966 	 * Loop through the send queue, setting up transmit descriptors
    967 	 * until we drain the queue, or use up all available transmit
    968 	 * descriptors.
    969 	 */
    970 	for (;;) {
    971 		IFQ_POLL(&ifp->if_snd, m0);
    972 		if (m0 == NULL)
    973 			break;
    974 
    975 		if (sc->sc_txsfree < KSE_TXQUEUE_GC) {
    976 			txreap(sc);
    977 			if (sc->sc_txsfree == 0)
    978 				break;
    979 		}
    980 		txs = &sc->sc_txsoft[sc->sc_txsnext];
    981 		dmamap = txs->txs_dmamap;
    982 
    983 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    984 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    985 		if (error) {
    986 			if (error == EFBIG) {
    987 				aprint_error_dev(sc->sc_dev,
    988 				    "Tx packet consumes too many "
    989 				    "DMA segments, dropping...\n");
    990 				    IFQ_DEQUEUE(&ifp->if_snd, m0);
    991 				    m_freem(m0);
    992 				    continue;
    993 			}
    994 			/* Short on resources, just stop for now. */
    995 			break;
    996 		}
    997 
    998 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    999 			/*
   1000 			 * Not enough free descriptors to transmit this
   1001 			 * packet.  We haven't committed anything yet,
   1002 			 * so just unload the DMA map, put the packet
   1003 			 * back on the queue, and punt.	 Notify the upper
   1004 			 * layer that there are not more slots left.
   1005 			 */
   1006 			ifp->if_flags |= IFF_OACTIVE;
   1007 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   1008 			break;
   1009 		}
   1010 
   1011 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1012 
   1013 		/*
   1014 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1015 		 */
   1016 
   1017 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1018 		    BUS_DMASYNC_PREWRITE);
   1019 
   1020 		tdes0 = 0; /* to postpone 1st segment T0_OWN write */
   1021 		lasttx = -1;
   1022 		for (nexttx = sc->sc_txnext, seg = 0;
   1023 		     seg < dmamap->dm_nsegs;
   1024 		     seg++, nexttx = KSE_NEXTTX(nexttx)) {
   1025 			struct tdes *tdes = &sc->sc_txdescs[nexttx];
   1026 			/*
   1027 			 * If this is the first descriptor we're
   1028 			 * enqueueing, don't set the OWN bit just
   1029 			 * yet.	 That could cause a race condition.
   1030 			 * We'll do it below.
   1031 			 */
   1032 			tdes->t2 = dmamap->dm_segs[seg].ds_addr;
   1033 			tdes->t1 = sc->sc_t1csum
   1034 			     | (dmamap->dm_segs[seg].ds_len & T1_TBS_MASK);
   1035 			tdes->t0 = tdes0;
   1036 			tdes0 = T0_OWN; /* 2nd and other segments */
   1037 			lasttx = nexttx;
   1038 		}
   1039 		/*
   1040 		 * Outgoing NFS mbuf must be unloaded when Tx completed.
   1041 		 * Without T1_IC NFS mbuf is left unack'ed for excessive
   1042 		 * time and NFS stops to proceed until kse_watchdog()
   1043 		 * calls txreap() to reclaim the unack'ed mbuf.
   1044 		 * It's painful to traverse every mbuf chain to determine
   1045 		 * whether someone is waiting for Tx completion.
   1046 		 */
   1047 		m = m0;
   1048 		do {
   1049 			if ((m->m_flags & M_EXT) && m->m_ext.ext_free) {
   1050 				sc->sc_txdescs[lasttx].t1 |= T1_IC;
   1051 				break;
   1052 			}
   1053 		} while ((m = m->m_next) != NULL);
   1054 
   1055 		/* Write deferred 1st segment T0_OWN at the final stage */
   1056 		sc->sc_txdescs[lasttx].t1 |= T1_LS;
   1057 		sc->sc_txdescs[sc->sc_txnext].t1 |= T1_FS;
   1058 		sc->sc_txdescs[sc->sc_txnext].t0 = T0_OWN;
   1059 		KSE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   1060 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1061 
   1062 		/* Tell DMA start transmit */
   1063 		CSR_WRITE_4(sc, MDTSC, 1);
   1064 
   1065 		txs->txs_mbuf = m0;
   1066 		txs->txs_firstdesc = sc->sc_txnext;
   1067 		txs->txs_lastdesc = lasttx;
   1068 		txs->txs_ndesc = dmamap->dm_nsegs;
   1069 
   1070 		sc->sc_txfree -= txs->txs_ndesc;
   1071 		sc->sc_txnext = nexttx;
   1072 		sc->sc_txsfree--;
   1073 		sc->sc_txsnext = KSE_NEXTTXS(sc->sc_txsnext);
   1074 		/*
   1075 		 * Pass the packet to any BPF listeners.
   1076 		 */
   1077 		bpf_mtap(ifp, m0, BPF_D_OUT);
   1078 	}
   1079 
   1080 	if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
   1081 		/* No more slots left; notify upper layer. */
   1082 		ifp->if_flags |= IFF_OACTIVE;
   1083 	}
   1084 	if (sc->sc_txfree != ofree) {
   1085 		/* Set a watchdog timer in case the chip flakes out. */
   1086 		ifp->if_timer = 5;
   1087 	}
   1088 }
   1089 
   1090 static void
   1091 kse_set_filter(struct kse_softc *sc)
   1092 {
   1093 	struct ether_multistep step;
   1094 	struct ether_multi *enm;
   1095 	struct ethercom *ec = &sc->sc_ethercom;
   1096 	struct ifnet *ifp = &ec->ec_if;
   1097 	uint32_t crc, mchash[2];
   1098 	int i;
   1099 
   1100 	sc->sc_rxc &= ~(RXC_MHTE | RXC_RM | RXC_RA);
   1101 	ifp->if_flags &= ~IFF_ALLMULTI;
   1102 
   1103 	if ((ifp->if_flags & IFF_PROMISC) || ec->ec_multicnt > 0) {
   1104 		ifp->if_flags |= IFF_ALLMULTI;
   1105 		goto update;
   1106 	}
   1107 
   1108 	for (i = 0; i < 16; i++)
   1109 		 CSR_WRITE_4(sc, MAAH0 + i*8, 0);
   1110 	crc = mchash[0] = mchash[1] = 0;
   1111 	ETHER_LOCK(ec);
   1112 	ETHER_FIRST_MULTI(step, ec, enm);
   1113 	i = 0;
   1114 	while (enm != NULL) {
   1115 #if KSE_MCASTDEBUG == 1
   1116 		printf("%s: addrs %s %s\n", __func__,
   1117 		   ether_sprintf(enm->enm_addrlo),
   1118 		   ether_sprintf(enm->enm_addrhi));
   1119 #endif
   1120 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1121 			/*
   1122 			 * We must listen to a range of multicast addresses.
   1123 			 * For now, just accept all multicasts, rather than
   1124 			 * trying to set only those filter bits needed to match
   1125 			 * the range.  (At this time, the only use of address
   1126 			 * ranges is for IP multicast routing, for which the
   1127 			 * range is big enough to require all bits set.)
   1128 			 */
   1129 			ETHER_UNLOCK(ec);
   1130 			ifp->if_flags |= IFF_ALLMULTI;
   1131 			goto update;
   1132 		}
   1133 		if (i < 16) {
   1134 			/* use 16 additional MAC addr to accept mcast */
   1135 			uint32_t addr;
   1136 			uint8_t *ep = enm->enm_addrlo;
   1137 			addr = (ep[3] << 24) | (ep[2] << 16)
   1138 			     | (ep[1] << 8)  |  ep[0];
   1139 			CSR_WRITE_4(sc, MAAL0 + i*8, addr);
   1140 			addr = (ep[5] << 8) | ep[4] | (1U<<31);
   1141 			CSR_WRITE_4(sc, MAAH0 + i*8, addr);
   1142 		} else {
   1143 			/* use hash table when too many */
   1144 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   1145 			mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
   1146 		}
   1147 		ETHER_NEXT_MULTI(step, enm);
   1148 		i++;
   1149 	}
   1150 	ETHER_UNLOCK(ec);
   1151 
   1152 	if (crc) {
   1153 		CSR_WRITE_4(sc, MTR0, mchash[0]);
   1154 		CSR_WRITE_4(sc, MTR1, mchash[1]);
   1155 		sc->sc_rxc |= RXC_MHTE;
   1156 	}
   1157 	return;
   1158 
   1159  update:
   1160 	/* With RA or RM, MHTE/MTR0/MTR1 are never consulted. */
   1161 	if (ifp->if_flags & IFF_PROMISC)
   1162 		sc->sc_rxc |= RXC_RA;
   1163 	else
   1164 		sc->sc_rxc |= RXC_RM;
   1165 	return;
   1166 }
   1167 
   1168 static int
   1169 add_rxbuf(struct kse_softc *sc, int idx)
   1170 {
   1171 	struct kse_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1172 	struct mbuf *m;
   1173 	int error;
   1174 
   1175 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1176 	if (m == NULL)
   1177 		return ENOBUFS;
   1178 
   1179 	MCLGET(m, M_DONTWAIT);
   1180 	if ((m->m_flags & M_EXT) == 0) {
   1181 		m_freem(m);
   1182 		return ENOBUFS;
   1183 	}
   1184 
   1185 	if (rxs->rxs_mbuf != NULL)
   1186 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1187 
   1188 	rxs->rxs_mbuf = m;
   1189 
   1190 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1191 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1192 	if (error) {
   1193 		aprint_error_dev(sc->sc_dev,
   1194 		    "can't load rx DMA map %d, error = %d\n", idx, error);
   1195 		panic("kse_add_rxbuf");
   1196 	}
   1197 
   1198 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1199 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1200 
   1201 	KSE_INIT_RXDESC(sc, idx);
   1202 
   1203 	return 0;
   1204 }
   1205 
   1206 static void
   1207 rxdrain(struct kse_softc *sc)
   1208 {
   1209 	struct kse_rxsoft *rxs;
   1210 	int i;
   1211 
   1212 	for (i = 0; i < KSE_NRXDESC; i++) {
   1213 		rxs = &sc->sc_rxsoft[i];
   1214 		if (rxs->rxs_mbuf != NULL) {
   1215 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1216 			m_freem(rxs->rxs_mbuf);
   1217 			rxs->rxs_mbuf = NULL;
   1218 		}
   1219 	}
   1220 }
   1221 
   1222 static int
   1223 kse_intr(void *arg)
   1224 {
   1225 	struct kse_softc *sc = arg;
   1226 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1227 	uint32_t isr;
   1228 
   1229 	if ((isr = CSR_READ_4(sc, INTST)) == 0)
   1230 		return 0;
   1231 
   1232 	if (isr & INT_DMRS)
   1233 		rxintr(sc);
   1234 	if (isr & INT_DMTS)
   1235 		txreap(sc);
   1236 	if (isr & INT_DMLCS)
   1237 		lnkchg(sc);
   1238 	if (isr & INT_DMRBUS)
   1239 		aprint_error_dev(sc->sc_dev, "Rx descriptor full\n");
   1240 
   1241 	CSR_WRITE_4(sc, INTST, isr);
   1242 
   1243 	if (ifp->if_flags & IFF_RUNNING)
   1244 		if_schedule_deferred_start(ifp);
   1245 
   1246 	return 1;
   1247 }
   1248 
   1249 static void
   1250 rxintr(struct kse_softc *sc)
   1251 {
   1252 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1253 	struct kse_rxsoft *rxs;
   1254 	struct mbuf *m;
   1255 	uint32_t rxstat;
   1256 	int i, len;
   1257 
   1258 	for (i = sc->sc_rxptr; /*CONSTCOND*/ 1; i = KSE_NEXTRX(i)) {
   1259 		rxs = &sc->sc_rxsoft[i];
   1260 
   1261 		KSE_CDRXSYNC(sc, i,
   1262 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1263 
   1264 		rxstat = sc->sc_rxdescs[i].r0;
   1265 
   1266 		if (rxstat & R0_OWN) /* desc is left empty */
   1267 			break;
   1268 
   1269 		/* R0_FS | R0_LS must have been marked for this desc */
   1270 
   1271 		if (rxstat & R0_ES) {
   1272 			ifp->if_ierrors++;
   1273 #define PRINTERR(bit, str)						\
   1274 			if (rxstat & (bit))				\
   1275 				aprint_error_dev(sc->sc_dev,		\
   1276 				    "%s\n", str)
   1277 			PRINTERR(R0_TL, "frame too long");
   1278 			PRINTERR(R0_RF, "runt frame");
   1279 			PRINTERR(R0_CE, "bad FCS");
   1280 #undef PRINTERR
   1281 			KSE_INIT_RXDESC(sc, i);
   1282 			continue;
   1283 		}
   1284 
   1285 		/* HW errata; frame might be too small or too large */
   1286 
   1287 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1288 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1289 
   1290 		len = rxstat & R0_FL_MASK;
   1291 		len -= ETHER_CRC_LEN;	/* Trim CRC off */
   1292 		m = rxs->rxs_mbuf;
   1293 
   1294 		if (add_rxbuf(sc, i) != 0) {
   1295 			ifp->if_ierrors++;
   1296 			KSE_INIT_RXDESC(sc, i);
   1297 			bus_dmamap_sync(sc->sc_dmat,
   1298 			    rxs->rxs_dmamap, 0,
   1299 			    rxs->rxs_dmamap->dm_mapsize,
   1300 			    BUS_DMASYNC_PREREAD);
   1301 			continue;
   1302 		}
   1303 
   1304 		m_set_rcvif(m, ifp);
   1305 		m->m_pkthdr.len = m->m_len = len;
   1306 
   1307 		if (sc->sc_mcsum) {
   1308 			m->m_pkthdr.csum_flags |= sc->sc_mcsum;
   1309 			if (rxstat & R0_IPE)
   1310 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1311 			if (rxstat & (R0_TCPE | R0_UDPE))
   1312 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1313 		}
   1314 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1315 #ifdef KSEDIAGNOSTIC
   1316 		if (kse_monitor_rxintr > 0) {
   1317 			aprint_error_dev(sc->sc_dev,
   1318 			    "m stat %x data %p len %d\n",
   1319 			    rxstat, m->m_data, m->m_len);
   1320 		}
   1321 #endif
   1322 	}
   1323 	sc->sc_rxptr = i;
   1324 }
   1325 
   1326 static void
   1327 txreap(struct kse_softc *sc)
   1328 {
   1329 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1330 	struct kse_txsoft *txs;
   1331 	uint32_t txstat;
   1332 	int i;
   1333 
   1334 	ifp->if_flags &= ~IFF_OACTIVE;
   1335 
   1336 	for (i = sc->sc_txsdirty; sc->sc_txsfree != KSE_TXQUEUELEN;
   1337 	     i = KSE_NEXTTXS(i), sc->sc_txsfree++) {
   1338 		txs = &sc->sc_txsoft[i];
   1339 
   1340 		KSE_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndesc,
   1341 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1342 
   1343 		txstat = sc->sc_txdescs[txs->txs_lastdesc].t0;
   1344 
   1345 		if (txstat & T0_OWN) /* desc is still in use */
   1346 			break;
   1347 
   1348 		/* There is no way to tell transmission status per frame */
   1349 
   1350 		ifp->if_opackets++;
   1351 
   1352 		sc->sc_txfree += txs->txs_ndesc;
   1353 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1354 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1355 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1356 		m_freem(txs->txs_mbuf);
   1357 		txs->txs_mbuf = NULL;
   1358 	}
   1359 	sc->sc_txsdirty = i;
   1360 	if (sc->sc_txsfree == KSE_TXQUEUELEN)
   1361 		ifp->if_timer = 0;
   1362 }
   1363 
   1364 static void
   1365 lnkchg(struct kse_softc *sc)
   1366 {
   1367 	struct ifmediareq ifmr;
   1368 
   1369 #if KSE_LINKDEBUG == 1
   1370 	uint16_t p1sr = CSR_READ_2(sc, P1SR);
   1371 printf("link %s detected\n", (p1sr & PxSR_LINKUP) ? "up" : "down");
   1372 #endif
   1373 	kse_ifmedia_sts(&sc->sc_ethercom.ec_if, &ifmr);
   1374 }
   1375 
   1376 static int
   1377 kse_ifmedia_upd(struct ifnet *ifp)
   1378 {
   1379 	struct kse_softc *sc = ifp->if_softc;
   1380 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
   1381 	uint16_t p1cr4;
   1382 
   1383 	p1cr4 = 0;
   1384 	if (IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_AUTO) {
   1385 		p1cr4 |= PxCR_STARTNEG;	/* restart AN */
   1386 		p1cr4 |= PxCR_AUTOEN;	/* enable AN */
   1387 		p1cr4 |= PxCR_USEFC;	/* advertise flow control pause */
   1388 		p1cr4 |= 0xf;		/* adv. 100FDX,100HDX,10FDX,10HDX */
   1389 	} else {
   1390 		if (IFM_SUBTYPE(ifm->ifm_cur->ifm_media) == IFM_100_TX)
   1391 			p1cr4 |= PxCR_SPD100;
   1392 		if (ifm->ifm_media & IFM_FDX)
   1393 			p1cr4 |= PxCR_USEFDX;
   1394 	}
   1395 	CSR_WRITE_2(sc, P1CR4, p1cr4);
   1396 #if KSE_LINKDEBUG == 1
   1397 printf("P1CR4: %04x\n", p1cr4);
   1398 #endif
   1399 	return 0;
   1400 }
   1401 
   1402 static void
   1403 kse_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1404 {
   1405 	struct kse_softc *sc = ifp->if_softc;
   1406 	struct mii_data *mii = &sc->sc_mii;
   1407 
   1408 	mii_pollstat(mii);
   1409 	ifmr->ifm_status = mii->mii_media_status;
   1410 	ifmr->ifm_active = (mii->mii_media_active & ~IFM_ETH_FMASK) |
   1411 	    sc->sc_flowflags;
   1412 }
   1413 
   1414 static void
   1415 nopifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1416 {
   1417 	struct kse_softc *sc = ifp->if_softc;
   1418 	struct ifmedia *ifm = &sc->sc_media;
   1419 
   1420 #if KSE_LINKDEBUG == 2
   1421 printf("p1sr: %04x, p2sr: %04x\n", CSR_READ_2(sc, P1SR), CSR_READ_2(sc, P2SR));
   1422 #endif
   1423 
   1424 	/* 8842 MAC pretends 100FDX all the time */
   1425 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
   1426 	ifmr->ifm_active = ifm->ifm_cur->ifm_media |
   1427 	    IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE;
   1428 }
   1429 
   1430 static void
   1431 phy_tick(void *arg)
   1432 {
   1433 	struct kse_softc *sc = arg;
   1434 	struct mii_data *mii = &sc->sc_mii;
   1435 	int s;
   1436 
   1437 	s = splnet();
   1438 	mii_tick(mii);
   1439 	splx(s);
   1440 
   1441 	callout_schedule(&sc->sc_tick_ch, hz);
   1442 }
   1443 
   1444 static const uint16_t phy1csr[] = {
   1445 	/* 0 BMCR */	0x4d0,
   1446 	/* 1 BMSR */	0x4d2,
   1447 	/* 2 PHYID1 */	0x4d6,	/* 0x0022 - PHY1HR */
   1448 	/* 3 PHYID2 */	0x4d4,	/* 0x1430 - PHY1LR */
   1449 	/* 4 ANAR */	0x4d8,
   1450 	/* 5 ANLPAR */	0x4da,
   1451 };
   1452 
   1453 int
   1454 kse_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   1455 {
   1456 	struct kse_softc *sc = device_private(self);
   1457 
   1458 	if (phy != 1 || reg >= __arraycount(phy1csr) || reg < 0)
   1459 		return EINVAL;
   1460 	*val = CSR_READ_2(sc, phy1csr[reg]);
   1461 	return 0;
   1462 }
   1463 
   1464 int
   1465 kse_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   1466 {
   1467 	struct kse_softc *sc = device_private(self);
   1468 
   1469 	if (phy != 1 || reg >= __arraycount(phy1csr) || reg < 0)
   1470 		return EINVAL;
   1471 	CSR_WRITE_2(sc, phy1csr[reg], val);
   1472 	return 0;
   1473 }
   1474 
   1475 void
   1476 kse_mii_statchg(struct ifnet *ifp)
   1477 {
   1478 	struct kse_softc *sc = ifp->if_softc;
   1479 	struct mii_data *mii = &sc->sc_mii;
   1480 
   1481 #if KSE_LINKDEBUG == 1
   1482 	/* decode P1SR register value */
   1483 	uint16_t p1sr = CSR_READ_2(sc, P1SR);
   1484 	printf("P1SR %04x, spd%d", p1sr, (p1sr & PxSR_SPD100) ? 100 : 10);
   1485 	if (p1sr & PxSR_FDX)
   1486 		printf(",full-duplex");
   1487 	if (p1sr & PxSR_RXFLOW)
   1488 		printf(",rxpause");
   1489 	if (p1sr & PxSR_TXFLOW)
   1490 		printf(",txpause");
   1491 	printf("\n");
   1492 	/* show resolved mii(4) parameters to compare against above */
   1493 	printf("MII spd%d",
   1494 	    (int)(sc->sc_ethercom.ec_if.if_baudrate / IF_Mbps(1)));
   1495 	if (mii->mii_media_active & IFM_FDX)
   1496 		printf(",full-duplex");
   1497 	if (mii->mii_media_active & IFM_FLOW) {
   1498 		printf(",flowcontrol");
   1499 		if (mii->mii_media_active & IFM_ETH_RXPAUSE)
   1500 			printf(",rxpause");
   1501 		if (mii->mii_media_active & IFM_ETH_TXPAUSE)
   1502 			printf(",txpause");
   1503 	}
   1504 	printf("\n");
   1505 #endif
   1506 	/* Get flow control negotiation result. */
   1507 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
   1508 	    (mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags)
   1509 		sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
   1510 
   1511 	/* Adjust MAC PAUSE flow control. */
   1512 	if ((mii->mii_media_active & IFM_FDX)
   1513 	    && (sc->sc_flowflags & IFM_ETH_TXPAUSE))
   1514 		sc->sc_txc |= TXC_FCE;
   1515 	else
   1516 		sc->sc_txc &= ~TXC_FCE;
   1517 	if ((mii->mii_media_active & IFM_FDX)
   1518 	    && (sc->sc_flowflags & IFM_ETH_RXPAUSE))
   1519 		sc->sc_rxc |= RXC_FCE;
   1520 	else
   1521 		sc->sc_rxc &= ~RXC_FCE;
   1522 	CSR_WRITE_4(sc, MDTXC, sc->sc_txc);
   1523 	CSR_WRITE_4(sc, MDRXC, sc->sc_rxc);
   1524 #if KSE_LINKDEBUG == 1
   1525 	printf("%ctxfce, %crxfce\n",
   1526 	    (sc->sc_txc & TXC_FCE) ? '+' : '-',
   1527 	    (sc->sc_rxc & RXC_FCE) ? '+' : '-');
   1528 #endif
   1529 }
   1530 
   1531 #ifdef KSE_EVENT_COUNTERS
   1532 static void
   1533 stat_tick(void *arg)
   1534 {
   1535 	struct kse_softc *sc = arg;
   1536 	struct ksext *ee = &sc->sc_ext;
   1537 	int nport, p, i, val;
   1538 
   1539 	nport = (sc->sc_chip == 0x8842) ? 3 : 1;
   1540 	for (p = 0; p < nport; p++) {
   1541 		for (i = 0; i < 32; i++) {
   1542 			val = 0x1c00 | (p * 0x20 + i);
   1543 			CSR_WRITE_2(sc, IACR, val);
   1544 			do {
   1545 				val = CSR_READ_2(sc, IADR5) << 16;
   1546 			} while ((val & (1U << 30)) == 0);
   1547 			if (val & (1U << 31)) {
   1548 				(void)CSR_READ_2(sc, IADR4);
   1549 				val = 0x3fffffff; /* has made overflow */
   1550 			}
   1551 			else {
   1552 				val &= 0x3fff0000;		/* 29:16 */
   1553 				val |= CSR_READ_2(sc, IADR4);	/* 15:0 */
   1554 			}
   1555 			ee->pev[p][i].ev_count += val; /* i (0-31) */
   1556 		}
   1557 		CSR_WRITE_2(sc, IACR, 0x1c00 + 0x100 + p);
   1558 		ee->pev[p][32].ev_count = CSR_READ_2(sc, IADR4); /* 32 */
   1559 		CSR_WRITE_2(sc, IACR, 0x1c00 + 0x100 + p * 3 + 1);
   1560 		ee->pev[p][33].ev_count = CSR_READ_2(sc, IADR4); /* 33 */
   1561 	}
   1562 	callout_schedule(&sc->sc_stat_ch, hz * 60);
   1563 }
   1564 
   1565 static void
   1566 zerostats(struct kse_softc *sc)
   1567 {
   1568 	struct ksext *ee = &sc->sc_ext;
   1569 	int nport, p, i, val;
   1570 
   1571 	/* Make sure all the HW counters get zero */
   1572 	nport = (sc->sc_chip == 0x8842) ? 3 : 1;
   1573 	for (p = 0; p < nport; p++) {
   1574 		for (i = 0; i < 31; i++) {
   1575 			val = 0x1c00 | (p * 0x20 + i);
   1576 			CSR_WRITE_2(sc, IACR, val);
   1577 			do {
   1578 				val = CSR_READ_2(sc, IADR5) << 16;
   1579 			} while ((val & (1U << 30)) == 0);
   1580 			(void)CSR_READ_2(sc, IADR4);
   1581 			ee->pev[p][i].ev_count = 0;
   1582 		}
   1583 	}
   1584 }
   1585 #endif
   1586