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