Home | History | Annotate | Line # | Download | only in ebus
if_le_ebus.c revision 1.3.2.1
      1 /*	$NetBSD: if_le_ebus.c,v 1.3.2.1 2012/02/18 07:31:44 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was written by Alessandro Forin and Neil Pittman
      8  * at Microsoft Research and contributed to The NetBSD Foundation
      9  * by Microsoft Corporation.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.3.2.1 2012/02/18 07:31:44 mrg Exp $");
     35 
     36 #include "opt_inet.h"
     37 
     38 
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/syslog.h>
     44 #include <sys/socket.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/errno.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_dl.h>
     52 #include <net/if_ether.h>
     53 #include <net/if_media.h>
     54 
     55 #ifdef INET
     56 #include <netinet/in.h>
     57 #include <netinet/if_inarp.h>
     58 #endif
     59 
     60 #include <net/bpf.h>
     61 #include <net/bpfdesc.h>
     62 
     63 #include <sys/rnd.h>
     64 
     65 #include <emips/ebus/ebusvar.h>
     66 #include <emips/emips/machdep.h>
     67 #include <machine/emipsreg.h>
     68 
     69 extern paddr_t kvtophys(vaddr_t);
     70 
     71 struct bufmap {
     72 	struct mbuf *mbuf;
     73 	paddr_t phys;
     74 };
     75 
     76 struct enic_softc {
     77 	device_t sc_dev;		/* base device glue */
     78 	struct	ethercom sc_ethercom;	/* Ethernet common part */
     79 	struct	ifmedia sc_media;	/* our supported media */
     80 
     81 	struct _Enic *sc_regs;		/* hw registers */
     82 
     83 	int	sc_havecarrier;		/* carrier status */
     84 	void	*sc_sh;			/* shutdownhook cookie */
     85 	int inited;
     86 
     87 	int sc_no_rd;
     88 	int sc_n_recv;
     89 	int sc_recv_h;
     90 	/* BUGBUG really should be malloc-ed */
     91 #define SC_MAX_N_RECV 64
     92 	struct bufmap sc_recv[SC_MAX_N_RECV];
     93 
     94 	int sc_no_td;
     95 	int sc_n_xmit;
     96 	int sc_xmit_h;
     97 	/* BUGBUG really should be malloc-ed */
     98 #define SC_MAX_N_XMIT 16
     99 	struct bufmap sc_xmit[SC_MAX_N_XMIT];
    100 
    101 #if DEBUG
    102 	int xhit;
    103 	int xmiss;
    104 	int tfull;
    105 	int tfull2;
    106 	int brh;
    107 	int rf;
    108 	int bxh;
    109 
    110 	int it;
    111 #endif
    112 
    113 	uint8_t sc_enaddr[ETHER_ADDR_LEN];
    114 	uint8_t sc_pad[2];
    115 	krndsource_t	rnd_source;
    116 };
    117 
    118 void enic_reset(struct ifnet *);
    119 int enic_init(struct ifnet *);
    120 void enic_stop(struct ifnet *, int);
    121 void enic_start(struct ifnet *);
    122 void enic_shutdown(void *);
    123 void enic_watchdog(struct ifnet *);
    124 int enic_mediachange(struct ifnet *);
    125 void enic_mediastatus(struct ifnet *, struct ifmediareq *);
    126 int enic_ioctl(struct ifnet *, u_long, void *);
    127 int enic_intr(void *, void *);
    128 void enic_rint(struct enic_softc *, uint32_t, paddr_t);
    129 void enic_tint(struct enic_softc *, uint32_t, paddr_t);
    130 void enic_kill_xmit(struct enic_softc *);
    131 void enic_post_recv(struct enic_softc *, struct mbuf *);
    132 void enic_refill(struct enic_softc *);
    133 static int enic_gethwinfo(struct enic_softc *);
    134 int enic_put(struct enic_softc *, struct mbuf **);
    135 
    136 static int enic_match(device_t, cfdata_t, void *);
    137 static void enic_attach(device_t, device_t, void *);
    138 
    139 CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
    140     enic_match, enic_attach, NULL, NULL);
    141 
    142 int
    143 enic_match(device_t parent, cfdata_t cf, void *aux)
    144 {
    145 	struct ebus_attach_args *d = aux;
    146 	/* donno yet */
    147 	struct _Enic *et = (struct _Enic *)d->ia_vaddr;
    148 
    149 	if (strcmp("enic", d->ia_name) != 0)
    150 		return 0;
    151 	if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
    152 		return 0;
    153 	return 1;
    154 }
    155 
    156 void
    157 enic_attach(device_t parent, device_t self, void *aux)
    158 {
    159 	struct enic_softc *sc = device_private(self);
    160 	struct ebus_attach_args *ia = aux;
    161 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    162 
    163 	sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
    164 #if DEBUG
    165 	printf(" virt=%p ", (void *)sc->sc_regs);
    166 #endif
    167 
    168 	/* Get the MAC and the depth of the FIFOs */
    169 	if (!enic_gethwinfo(sc)) {
    170 		printf(" <cannot get hw info> DISABLED.\n");
    171 		/*
    172 		 * NB: caveat maintainer: make sure what we
    173 		 * did NOT do below does not hurt the system
    174 		 */
    175 		return;
    176 	}
    177 
    178 	sc->sc_dev = self;
    179 	sc->sc_no_td = 0;
    180 	sc->sc_havecarrier = 1; /* BUGBUG */
    181 	sc->sc_recv_h = 0;
    182 	sc->sc_xmit_h = 0;
    183 	/* uhmm do I need to do this? */
    184 	memset(sc->sc_recv, 0, sizeof sc->sc_recv);
    185 	memset(sc->sc_xmit, 0, sizeof sc->sc_xmit);
    186 
    187 	/* Initialize ifnet structure. */
    188 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    189 	ifp->if_softc = sc;
    190 	ifp->if_start = enic_start;
    191 	ifp->if_ioctl = enic_ioctl;
    192 	ifp->if_watchdog = enic_watchdog;
    193 	ifp->if_init = enic_init;
    194 	ifp->if_stop = enic_stop;
    195 	ifp->if_flags =
    196 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    197 	IFQ_SET_READY(&ifp->if_snd);
    198 
    199 	/* Initialize ifmedia structures. */
    200 	ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
    201 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    202 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    203 
    204 	/* Make sure the chip is stopped. */
    205 	enic_stop(ifp, 0);
    206 	sc->inited = 0;
    207 
    208 	/* Get the mac address and print it */
    209 	printf(": eNIC [%d %d], address %s\n",
    210 	    sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
    211 
    212 	/* claim 802.1q capability */
    213 #if 0
    214 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    215 #endif
    216 
    217 	/* Attach the interface. */
    218 	if_attach(ifp);
    219 	ether_ifattach(ifp, sc->sc_enaddr);
    220 
    221 	sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
    222 	if (sc->sc_sh == NULL)
    223 		panic("enic_attach: cannot establish shutdown hook");
    224 
    225 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    226 			  RND_TYPE_NET, 0);
    227 
    228 	ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
    229 	    enic_intr, sc);
    230 }
    231 
    232 /*
    233  * Beware: does not work while the nic is running
    234  */
    235 static int enic_gethwinfo(struct enic_softc *sc)
    236 {
    237 	uint8_t buffer[8];	/* 64bits max */
    238 	PENIC_INFO hw = (PENIC_INFO)buffer;
    239 	paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
    240 	int i;
    241 
    242 	/*
    243 	 * First thing first, get the MAC address
    244 	 */
    245 	memset(buffer,0,sizeof buffer);
    246 	buffer[0] = ENIC_CMD_GET_ADDRESS;
    247 	buffer[3] = ENIC_CMD_GET_ADDRESS;	/* bswap bug */
    248 	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
    249 	sc->sc_regs->BufferAddressHi32 = 0;
    250 	sc->sc_regs->BufferAddressLo32 = phys; /* go! */
    251 
    252 	for (i = 0; i < 100; i++) {
    253 		DELAY(100);
    254 		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
    255 			break;
    256 	}
    257 	if (i == 100)
    258 		return 0;
    259 
    260 	phys2 = sc->sc_regs->BufferAddressLo32;
    261 	if (phys2 != phys) {
    262 		printf("enic uhu? %llx != %llx?\n",
    263 		    (long long)phys, (long long)phys2);
    264 		return 0;
    265 	}
    266 	memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);
    267 
    268 	/*
    269 	 * Next get the HW parameters
    270 	 */
    271 	memset(buffer,0,sizeof buffer);
    272 	buffer[0] = ENIC_CMD_GET_INFO;
    273 	buffer[3] = ENIC_CMD_GET_INFO;	/* bswap bug */
    274 	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
    275 	sc->sc_regs->BufferAddressHi32 = 0;
    276 	sc->sc_regs->BufferAddressLo32 = phys; /* go! */
    277 
    278 	for (i = 0; i < 100; i++) {
    279 		DELAY(100);
    280 		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
    281 			break;
    282 	}
    283 	if (i == 100)
    284 		return 0;
    285 
    286 	phys2 = sc->sc_regs->BufferAddressLo32;
    287 	if (phys2 != phys) {
    288 		printf("enic uhu2? %llx != %llx?\n",
    289 		    (long long)phys, (long long)phys2);
    290 		return 0;
    291 	}
    292 #if 0
    293 	printf("enic: hwinfo: %x %x %x %x %x %x \n",
    294 	    hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
    295 	    hw->ErrorCount, hw->FramesDropped, hw->Reserved);
    296 #endif
    297 
    298 	/*
    299 	 * Get FIFO depths and cap them
    300 	 */
    301 	sc->sc_n_recv = hw->InputFifoSize;
    302 	if (sc->sc_n_recv > SC_MAX_N_RECV)
    303 		sc->sc_n_recv = SC_MAX_N_RECV;
    304 	if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
    305 		sc->sc_n_recv = 8;
    306 		sc->sc_n_xmit = 4;
    307 	} else {
    308 		sc->sc_n_xmit = hw->OutputFifoSize;
    309 		if (sc->sc_n_xmit > SC_MAX_N_XMIT)
    310 			sc->sc_n_xmit = SC_MAX_N_XMIT;
    311 	}
    312 
    313 	return 1;
    314 }
    315 
    316 void
    317 enic_reset(struct ifnet *ifp)
    318 {
    319 	int s;
    320 
    321 	s = splnet();
    322 	enic_stop(ifp,0);
    323 	enic_init(ifp);
    324 	splx(s);
    325 }
    326 
    327 void
    328 enic_stop(struct ifnet *ifp, int suspend)
    329 {
    330 	struct enic_softc *sc = ifp->if_softc;
    331 
    332 #if 0
    333 	printf("enic_stop %x\n", sc->sc_regs->Control);
    334 #endif
    335 
    336 	/*
    337 	 * NB: only "ifconfig down" says suspend=1 (then "up" calls init)
    338 	 * Could simply set RXDIS in this case
    339 	 */
    340 	sc->inited = 2;
    341 	sc->sc_regs->Control = EC_RESET;
    342 	sc->sc_no_rd = 0; /* they are gone */
    343 	sc->sc_no_td = 0; /* they are gone */
    344 }
    345 
    346 void
    347 enic_shutdown(void *arg)
    348 {
    349 	struct ifnet *ifp = arg;
    350 
    351 	enic_stop(ifp, 0);
    352 }
    353 
    354 void
    355 enic_kill_xmit(struct enic_softc *sc)
    356 {
    357 	int i;
    358 	struct mbuf *m;
    359 
    360 	for (i = 0; i < sc->sc_n_xmit; i++) {
    361 		if ((m = sc->sc_xmit[i].mbuf) != NULL) {
    362 			sc->sc_xmit[i].mbuf = NULL;
    363 			sc->sc_xmit[i].phys = ~0;
    364 			m_freem(m);
    365 		}
    366 	}
    367 	sc->sc_no_td = 0;
    368 	sc->sc_xmit_h = 0;
    369 }
    370 
    371 void
    372 enic_post_recv(struct enic_softc *sc, struct mbuf *m)
    373 {
    374 	int i, waitmode = M_DONTWAIT;
    375 	paddr_t phys;
    376 
    377 #define rpostone(_p_) \
    378     sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
    379     sc->sc_regs->BufferAddressHi32 = 0; \
    380     sc->sc_regs->BufferAddressLo32 = _p_;
    381 #define tpostone(_p_,_s_) \
    382     sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
    383     sc->sc_regs->BufferAddressHi32 = 0; \
    384     sc->sc_regs->BufferAddressLo32 = _p_;
    385 
    386 	/* Operational reload? */
    387 	if (m != NULL) {
    388 		/* But is the hw ready for it */
    389 		if (sc->sc_regs->Control & EC_IF_FULL)
    390 			goto no_room;
    391 		/* Yes, find a spot. Include empty q case. */
    392 		for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
    393 			if (sc->sc_recv[i].mbuf == NULL)
    394 				goto found;
    395 		for (i = 0; i < sc->sc_recv_h; i++)
    396 			if (sc->sc_recv[i].mbuf == NULL)
    397 				goto found;
    398 		/* no spot, drop it (sigh) */
    399  no_room:
    400 #if DEBUG
    401 		sc->rf++;
    402 #endif
    403 		m_freem(m);
    404 		return;
    405  found:
    406 		phys = kvtophys((vaddr_t)m->m_data);
    407 		sc->sc_recv[i].mbuf = m;
    408 		sc->sc_recv[i].phys = phys;
    409 		rpostone(phys);
    410 		sc->sc_no_rd++;
    411 		return;
    412 	}
    413 
    414 	/* Repost after reset? */
    415 	if (sc->inited) {
    416 		/* order doesnt matter, might as well keep it clean */
    417 		int j = 0;
    418 		sc->sc_recv_h = 0;
    419 		for (i = 0; i < sc->sc_n_recv; i++)
    420 			if ((m = sc->sc_recv[i].mbuf) != NULL) {
    421 				phys = sc->sc_recv[i].phys;
    422 				sc->sc_recv[i].mbuf = NULL;
    423 				sc->sc_recv[i].phys = ~0;
    424 				sc->sc_recv[j].mbuf = m;
    425 				sc->sc_recv[j].phys = phys;
    426 #if DEBUG
    427 				if (sc->sc_regs->Control & EC_IF_FULL)
    428 					printf("?uhu? postrecv full? %d\n",
    429 					    sc->sc_no_rd);
    430 #endif
    431 				sc->sc_no_rd++;
    432 				rpostone(phys);
    433 				j++;
    434 			}
    435 		/* Any holes left? */
    436 		sc->inited = 1;
    437 		if (j >= sc->sc_n_recv)
    438 			return;	/* no, we are done */
    439 		/* continue on with the loop below */
    440 		i = j; m = NULL;
    441 		goto fillem;
    442 	}
    443 
    444 	/* Initial refill, we can wait */
    445 	waitmode = M_WAIT;
    446 	sc->sc_recv_h = 0;
    447 	memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
    448 	i = 0;
    449  fillem:
    450 	for (; i < sc->sc_n_recv; i++) {
    451 		MGETHDR(m, waitmode, MT_DATA);
    452 		if (m == 0)
    453 			break;
    454 		m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    455 		m->m_pkthdr.len = 0;
    456 
    457 		MCLGET(m, waitmode);
    458 		if ((m->m_flags & M_EXT) == 0)
    459 			break;
    460 
    461 		/*
    462 		 * This offset aligns IP/TCP headers and helps performance
    463 		 */
    464 #if 1
    465 #define ADJUST_MBUF_OFFSET(_m_) { \
    466 	(_m_)->m_data += 2; \
    467 	(_m_)->m_len -= 2; \
    468 }
    469 #else
    470 #define ADJUST_MBUF_OFFSET(_m_)
    471 #endif
    472 
    473 		m->m_len = MCLBYTES;
    474 
    475 		ADJUST_MBUF_OFFSET(m);
    476 		phys = kvtophys((vaddr_t)m->m_data);
    477 		sc->sc_recv[i].mbuf = m;
    478 		sc->sc_recv[i].phys = phys;
    479 #if DEBUG
    480 		if (sc->sc_regs->Control & EC_IF_FULL)
    481 			printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
    482 #endif
    483 		sc->sc_no_rd++;
    484 		rpostone(phys);
    485 		m = NULL;
    486 	}
    487 
    488 	if (m)
    489 		m_freem(m);
    490 	sc->inited = 1;
    491 }
    492 
    493 void enic_refill(struct enic_softc *sc)
    494 {
    495 	struct mbuf *m;
    496 	int waitmode = M_DONTWAIT;
    497 
    498 	MGETHDR(m, waitmode, MT_DATA);
    499 	if (m == NULL)
    500 		return;
    501 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    502 	m->m_pkthdr.len = 0;
    503 
    504 	MCLGET(m, waitmode);
    505 	if ((m->m_flags & M_EXT) == 0) {
    506 		m_freem(m);
    507 		return;
    508 	}
    509 
    510 	m->m_len = MCLBYTES;
    511 	ADJUST_MBUF_OFFSET(m);
    512 
    513 	enic_post_recv(sc,m);
    514 }
    515 
    516 int
    517 enic_init(struct ifnet *ifp)
    518 {
    519 	struct enic_softc *sc = ifp->if_softc;
    520 	uint32_t ctl;
    521 
    522 	/* no need to init many times unless we are in reset */
    523 	if (sc->inited != 1) {
    524 
    525 		/* Cancel all xmit buffers */
    526 		enic_kill_xmit(sc);
    527 
    528 		/* Re-post all recv buffers */
    529 		enic_post_recv(sc,NULL);
    530 	}
    531 
    532 	/* Start the eNIC */
    533 	ifp->if_flags |= IFF_RUNNING;
    534 	ifp->if_flags &= ~IFF_OACTIVE;
    535 	ifp->if_timer = 0;
    536 	ctl = sc->sc_regs->Control | EC_INTEN;
    537 	ctl &= ~EC_RXDIS;
    538 	sc->sc_regs->Control = ctl;
    539 #if 0
    540 	printf("enic_init <- %x\n",ctl);
    541 #endif
    542 
    543 	enic_start(ifp);
    544 
    545 	return 0;
    546 }
    547 
    548 
    549 void
    550 enic_watchdog(struct ifnet *ifp)
    551 {
    552 	struct enic_softc *sc = ifp->if_softc;
    553 
    554 #if 0
    555 	printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
    556 #endif
    557 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    558 	++ifp->if_oerrors;
    559 
    560 	enic_reset(ifp);
    561 }
    562 
    563 int
    564 enic_mediachange(struct ifnet *ifp)
    565 {
    566 #if 0
    567 	struct enic_softc *sc = ifp->if_softc;
    568 #endif
    569 	/* more code here.. */
    570 
    571 	return 0;
    572 }
    573 
    574 void
    575 enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    576 {
    577 	struct enic_softc *sc = ifp->if_softc;
    578 
    579 	if ((ifp->if_flags & IFF_UP) == 0)
    580 		return;
    581 
    582 	ifmr->ifm_status = IFM_AVALID;
    583 	if (sc->sc_havecarrier)
    584 		ifmr->ifm_status |= IFM_ACTIVE;
    585 
    586 	/* more code here someday.. */
    587 }
    588 
    589 /*
    590  * Process an ioctl request.
    591  */
    592 int
    593 enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    594 {
    595 	struct enic_softc *sc = ifp->if_softc;
    596 	struct ifreq *ifr = (struct ifreq *)data;
    597 	int s, error = 0;
    598 
    599 	s = splnet();
    600 
    601 	switch (cmd) {
    602 	case SIOCGIFMEDIA:
    603 	case SIOCSIFMEDIA:
    604 #if 0 /*DEBUG*/
    605 	    {
    606 		extern int ei_drops[];
    607 		static int flip = 0;
    608 		if (flip++ == 2) {
    609 			int i;
    610 			flip = 0;
    611 			printf("enic_ioctl(%x) %qd/%qd %qd/%qd %d/%d %d:%d "
    612 			    "%d+%d %d/%d/%d\n", ifp->if_flags,
    613 			    ifp->if_ierrors, ifp->if_oerrors,
    614 			    ifp->if_ipackets, ifp->if_opackets,
    615 			    sc->sc_no_rd, sc->sc_no_td,
    616 			    sc->xhit, sc->xmiss,
    617 			    sc->tfull, sc->tfull2,
    618 			    sc->brh, sc->rf, sc->bxh);
    619 			printf(" Ctl %x lt %x tim %d\n",
    620 			    sc->sc_regs->Control, sc->it, ifp->if_timer);
    621 
    622 			for (i = 0; i < 64; i++)
    623 				if (ei_drops[i])
    624 					printf(" %d.%d",i,ei_drops[i]);
    625 			printf("\n");
    626 		}
    627 	    }
    628 #endif
    629 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    630 		break;
    631 
    632 	default:
    633 		error = ether_ioctl(ifp, cmd, data);
    634 		if (cmd == SIOCSIFADDR) {
    635 			/*
    636 			 * hackattack: NFS does not turn us back
    637 			 * on after a stop. So.
    638 			 */
    639 #if 0
    640 			printf("enic_ioctl(%lx)\n",cmd);
    641 #endif
    642 			enic_init(ifp);
    643 		}
    644 		if (error != ENETRESET)
    645 			break;
    646 		error = 0;
    647 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    648 			break;
    649 		if (ifp->if_flags & IFF_RUNNING) {
    650 			enic_reset(ifp);
    651 		}
    652 		break;
    653 	}
    654 	splx(s);
    655 
    656 	return error;
    657 }
    658 
    659 int
    660 enic_intr(void *cookie, void *f)
    661 {
    662 	struct enic_softc *sc = cookie;
    663 	uint32_t isr, saf, hi, lo, fl;
    664 
    665 	isr = sc->sc_regs->Control;
    666 
    667 	/* Make sure there is one and that we should take it */
    668 	if ((isr & (EC_INTEN|EC_DONE)) != (EC_INTEN|EC_DONE))
    669 		return 0;
    670 
    671 	if (isr & EC_ERROR) {
    672 		printf("%s: internal error\n", device_xname(sc->sc_dev));
    673 		enic_reset(&sc->sc_ethercom.ec_if);
    674 		return 1;
    675 	}
    676 
    677 	/*
    678 	 * pull out all completed buffers
    679 	 */
    680 	while ((isr & EC_OF_EMPTY) == 0) {
    681 
    682 		/* beware, order matters */
    683 		saf = sc->sc_regs->SizeAndFlags;
    684 		hi  = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
    685 		lo  = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
    686 
    687 		fl = saf & (ES_F_MASK &~ ES_F_DONE);
    688 		if (fl == ES_F_RECV)
    689 			enic_rint(sc,saf,lo);
    690 		else if (fl == ES_F_XMIT)
    691 			enic_tint(sc,saf,lo);
    692 		else {
    693 			/*
    694 			 * we do not currently expect or care for
    695 			 * command completions?
    696 			 */
    697 			if (fl != ES_F_CMD)
    698 				printf("%s: invalid saf=x%x (lo=%x)\n",
    699 				    device_xname(sc->sc_dev), saf, lo);
    700 		}
    701 
    702 		isr = sc->sc_regs->Control;
    703 	}
    704 
    705 	rnd_add_uint32(&sc->rnd_source, isr);
    706 
    707 	return 1;
    708 }
    709 
    710 void
    711 enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
    712 {
    713 	struct mbuf *m;
    714 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    715 	int len = saf & ES_S_MASK, i;
    716 
    717 	/* Find what buffer it is. Should be the first. */
    718 	for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
    719 		if (sc->sc_recv[i].phys == phys)
    720 			goto found;
    721 	for (i = 0; i < sc->sc_recv_h; i++)
    722 		if (sc->sc_recv[i].phys == phys)
    723 			goto found;
    724 
    725 	/* uhu?? */
    726 	printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
    727 	    (long long)phys);
    728 	ifp->if_ierrors++;
    729 	return;
    730 
    731 	/* got it, pop it */
    732  found:
    733 	sc->sc_no_rd--;
    734 	m = sc->sc_recv[i].mbuf;
    735 	sc->sc_recv[i].mbuf = NULL;
    736 	sc->sc_recv[i].phys = ~0;
    737 	if (i == sc->sc_recv_h) { /* should be */
    738 		sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
    739 	}
    740 #if DEBUG
    741 	else
    742 		sc->brh++;
    743 #endif
    744 
    745 	if (len <= sizeof(struct ether_header) ||
    746 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    747 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    748 		ETHERMTU + sizeof(struct ether_header))) {
    749 		ifp->if_ierrors++;
    750 
    751 		/* reuse it */
    752 		enic_post_recv(sc,m);
    753 		return;
    754 	}
    755 
    756 	/* Adjust size */
    757 	m->m_pkthdr.len = len;
    758 	m->m_len = len; /* recheck */
    759 
    760 	ifp->if_ipackets++;
    761 
    762 	/*
    763 	 * Check if there's a BPF listener on this interface.
    764 	 * If so, hand off the raw packet to BPF.
    765 	 */
    766 	if (ifp->if_bpf)
    767 		bpf_mtap(ifp, m);
    768 
    769 	/* Pass the packet up. */
    770 	(*ifp->if_input)(ifp, m);
    771 
    772 	/* Need to refill now */
    773 	enic_refill(sc);
    774 }
    775 
    776 void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
    777 {
    778 	struct mbuf *m;
    779 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    780 	int i;
    781 
    782 #if DEBUG
    783 	sc->it = 1;
    784 #endif
    785 
    786 	/* BUGBUG should there be a per-buffer error bit in SAF? */
    787 
    788 	/* Find what buffer it is. Should be the first. */
    789 	for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
    790 		if (sc->sc_xmit[i].phys == phys)
    791 			goto found;
    792 	for (i = 0; i < sc->sc_xmit_h; i++)
    793 		if (sc->sc_xmit[i].phys == phys)
    794 			goto found;
    795 
    796 	/* uhu?? */
    797 	printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
    798 	    (long long)phys);
    799 	ifp->if_oerrors++;
    800 	return;
    801 
    802 	/* got it, pop it */
    803  found:
    804 	m = sc->sc_xmit[i].mbuf;
    805 	sc->sc_xmit[i].mbuf = NULL;
    806 	sc->sc_xmit[i].phys = ~0;
    807 	if (i == sc->sc_xmit_h) { /* should be */
    808 		sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
    809 	}
    810 #if DEBUG
    811 	else
    812 		sc->bxh++;
    813 #endif
    814 	m_freem(m);
    815 	ifp->if_opackets++;
    816 
    817 	if (--sc->sc_no_td == 0)
    818 		ifp->if_timer = 0;
    819 
    820 	ifp->if_flags &= ~IFF_OACTIVE;
    821 	enic_start(ifp);
    822 #if DEBUG
    823 	sc->it = 1;
    824 #endif
    825 }
    826 
    827 /*
    828  * Setup output on interface.
    829  * Get another datagram to send off of the interface queue, and map it to the
    830  * interface before starting the output.
    831  * Called only at splnet or interrupt level.
    832  */
    833 void
    834 enic_start(struct ifnet *ifp)
    835 {
    836 	struct enic_softc *sc = ifp->if_softc;
    837 	struct mbuf *m;
    838 	int len, ix, s;
    839 	paddr_t phys;
    840 
    841 #if DEBUG
    842 	sc->it = 0;
    843 #endif
    844 
    845 #if 0
    846 	printf("enic_start(%x)\n", ifp->if_flags);
    847 #endif
    848 
    849 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    850 		return;
    851 
    852 	s = splnet();	/* I know, I dont trust people.. */
    853 
    854 	ix = sc->sc_xmit_h;
    855 	for (;;) {
    856 
    857 		/* Anything to do? */
    858 		IFQ_POLL(&ifp->if_snd, m);
    859 		if (m == NULL)
    860 			break;
    861 
    862 		/* find a spot, if any */
    863 		for (; ix < sc->sc_n_xmit; ix++)
    864 			if (sc->sc_xmit[ix].mbuf == NULL)
    865 				goto found;
    866 		for (ix = 0; ix < sc->sc_xmit_h; ix++)
    867 			if (sc->sc_xmit[ix].mbuf == NULL)
    868 				goto found;
    869 		/* oh well */
    870 		ifp->if_flags |= IFF_OACTIVE;
    871 #if DEBUG
    872 		sc->tfull++;
    873 #endif
    874 		break;
    875 
    876  found:
    877 		IFQ_DEQUEUE(&ifp->if_snd, m);
    878 		if (m == NULL)
    879 			break;
    880 
    881 		/*
    882 		 * If BPF is listening on this interface, let it see the packet
    883 		 * before we commit it to the wire.
    884 		 */
    885 		if (ifp->if_bpf)
    886 			bpf_mtap(ifp, m);
    887 
    888 		/*
    889 		 * Copy the mbuf chain into a contiguous transmit buffer.
    890 		 */
    891 		len = enic_put(sc, &m);
    892 		if (len == 0)
    893 			break;	/* sanity */
    894 		if (len > (ETHERMTU + sizeof(struct ether_header))) {
    895 			printf("enic? tlen %d > %d\n",
    896 			    len, ETHERMTU + sizeof(struct ether_header));
    897 			len = ETHERMTU + sizeof(struct ether_header);
    898 		}
    899 
    900 		ifp->if_timer = 5;
    901 
    902 		/*
    903 		 * Remember and post the buffer
    904 		 */
    905 		phys = kvtophys((vaddr_t)m->m_data);
    906 		sc->sc_xmit[ix].mbuf = m;
    907 		sc->sc_xmit[ix].phys = phys;
    908 
    909 		sc->sc_no_td++;
    910 
    911 		tpostone(phys,len);
    912 
    913 		if (sc->sc_regs->Control & EC_IF_FULL) {
    914 			ifp->if_flags |= IFF_OACTIVE;
    915 #if DEBUG
    916 			sc->tfull2++;
    917 #endif
    918 			break;
    919 		}
    920 
    921 		ix++;
    922 	}
    923 
    924 	splx(s);
    925 }
    926 
    927 int enic_put(struct enic_softc *sc, struct mbuf **pm)
    928 {
    929 	struct mbuf *n, *m = *pm, *mm;
    930 	int len, tlen = 0, xlen = m->m_pkthdr.len;
    931 	uint8_t *cp;
    932 
    933 #if 0
    934 	/* drop garbage */
    935 	tlen = xlen;
    936 	for (; m; m = n) {
    937 		len = m->m_len;
    938 		if (len == 0) {
    939 			MFREE(m, n);
    940 		if (m == *pm)
    941 			*pm = n;
    942 			continue;
    943 		}
    944 		tlen -= len;
    945 		KASSERT(m != m->m_next);
    946 		n = m->m_next;
    947 		if (tlen <= 0)
    948 			break;
    949 	}
    950 
    951 	/*
    952 	 * We might be done:
    953 	 * (a) empty chain (b) only one segment (c) bad chain
    954 	 */
    955 	if (((m = *pm) == NULL) || (tlen > 0))
    956 		xlen = 0;
    957 #endif
    958 
    959 	if ((xlen == 0) || (xlen <= m->m_len)) {
    960 #if DEBUG
    961 		sc->xhit++;
    962 #endif
    963 		return xlen;
    964 	}
    965 
    966 	/* Nope, true chain. Copy to contig :-(( */
    967 	tlen = xlen;
    968 	MGETHDR(n, M_NOWAIT, MT_DATA);
    969 	if (n == NULL)
    970 		goto Bad;
    971 	n->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    972 	n->m_pkthdr.len = tlen;
    973 
    974 	MCLGET(n, M_NOWAIT);
    975 	if ((n->m_flags & M_EXT) == 0) {
    976 		m_freem(n);
    977 		goto Bad;
    978 	}
    979 
    980 	n->m_len = tlen;
    981 	cp = mtod(n, uint8_t *);
    982 	for (; m && tlen; m = mm) {
    983 
    984 		len = m->m_len;
    985 		if (len > tlen)
    986 			len = tlen;
    987 		if (len)
    988 			memcpy(cp, mtod(m, void *), len);
    989 
    990 		cp += len;
    991 		tlen -= len;
    992 		MFREE(m, mm);
    993 
    994 	}
    995 
    996 	*pm = n;
    997 #if DEBUG
    998 	sc->xmiss++;
    999 #endif
   1000 	return (xlen);
   1001 
   1002  Bad:
   1003 	printf("enic_put: no mem?\n");
   1004 	m_freem(m);
   1005 	return 0;
   1006 }
   1007