Home | History | Annotate | Line # | Download | only in isa
if_eg.c revision 1.37
      1  1.37  explorer /*	$NetBSD: if_eg.c,v 1.37 1997/10/15 05:59:16 explorer Exp $	*/
      2   1.4       cgd 
      3   1.1   deraadt /*
      4   1.1   deraadt  * Copyright (c) 1993 Dean Huxley <dean (at) fsa.ca>
      5   1.1   deraadt  * All rights reserved.
      6   1.1   deraadt  *
      7   1.1   deraadt  * Redistribution and use in source and binary forms, with or without
      8   1.1   deraadt  * modification, are permitted provided that the following conditions
      9   1.1   deraadt  * are met:
     10   1.1   deraadt  * 1. Redistributions of source code must retain the above copyright
     11   1.1   deraadt  *    notice, this list of conditions and the following disclaimer.
     12   1.1   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   deraadt  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   deraadt  *    documentation and/or other materials provided with the distribution.
     15   1.1   deraadt  * 3. All advertising materials mentioning features or use of this software
     16   1.1   deraadt  *    must display the following acknowledgement:
     17   1.1   deraadt  *      This product includes software developed by Dean Huxley.
     18   1.1   deraadt  * 4. The name of Dean Huxley may not be used to endorse or promote products
     19   1.1   deraadt  *    derived from this software without specific prior written permission.
     20   1.1   deraadt  *
     21   1.1   deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1   deraadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1   deraadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1   deraadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1   deraadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1   deraadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1   deraadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1   deraadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1   deraadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1   deraadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.22   hpeyerl  */
     32  1.22   hpeyerl /*
     33  1.22   hpeyerl  * Support for 3Com 3c505 Etherlink+ card.
     34   1.1   deraadt  */
     35   1.1   deraadt 
     36   1.1   deraadt /* To do:
     37   1.1   deraadt  * - multicast
     38   1.1   deraadt  * - promiscuous
     39  1.27   thorpej  * - get rid of isa indirect stuff
     40   1.1   deraadt  */
     41   1.1   deraadt #include "bpfilter.h"
     42  1.37  explorer #include "rnd.h"
     43   1.1   deraadt 
     44   1.1   deraadt #include <sys/types.h>
     45   1.1   deraadt #include <sys/param.h>
     46  1.31   thorpej #include <sys/systm.h>
     47   1.1   deraadt #include <sys/mbuf.h>
     48   1.1   deraadt #include <sys/socket.h>
     49   1.1   deraadt #include <sys/ioctl.h>
     50   1.1   deraadt #include <sys/errno.h>
     51   1.1   deraadt #include <sys/syslog.h>
     52   1.1   deraadt #include <sys/select.h>
     53   1.1   deraadt #include <sys/device.h>
     54  1.37  explorer #if NRND > 0
     55  1.37  explorer #include <sys/rnd.h>
     56  1.37  explorer #endif
     57   1.1   deraadt 
     58   1.1   deraadt #include <net/if.h>
     59   1.1   deraadt #include <net/if_dl.h>
     60   1.1   deraadt #include <net/if_types.h>
     61  1.33        is 
     62  1.33        is #include <net/if_ether.h>
     63   1.1   deraadt 
     64   1.1   deraadt #ifdef INET
     65   1.1   deraadt #include <netinet/in.h>
     66   1.1   deraadt #include <netinet/in_systm.h>
     67   1.1   deraadt #include <netinet/in_var.h>
     68   1.1   deraadt #include <netinet/ip.h>
     69  1.33        is #include <netinet/if_inarp.h>
     70   1.1   deraadt #endif
     71   1.1   deraadt 
     72   1.1   deraadt #ifdef NS
     73   1.1   deraadt #include <netns/ns.h>
     74   1.1   deraadt #include <netns/ns_if.h>
     75   1.1   deraadt #endif
     76   1.1   deraadt 
     77   1.1   deraadt #if NBPFILTER > 0
     78   1.1   deraadt #include <net/bpf.h>
     79   1.1   deraadt #include <net/bpfdesc.h>
     80   1.1   deraadt #endif
     81   1.1   deraadt 
     82   1.1   deraadt #include <machine/cpu.h>
     83  1.26   mycroft #include <machine/intr.h>
     84  1.27   thorpej #include <machine/bus.h>
     85   1.1   deraadt 
     86  1.12       cgd #include <dev/isa/isavar.h>
     87  1.10       cgd #include <dev/isa/if_egreg.h>
     88  1.10       cgd #include <dev/isa/elink.h>
     89   1.1   deraadt 
     90   1.1   deraadt /* for debugging convenience */
     91   1.1   deraadt #ifdef EGDEBUG
     92  1.30  christos #define DPRINTF(x) printf x
     93   1.1   deraadt #else
     94  1.29  christos #define DPRINTF(x)
     95   1.1   deraadt #endif
     96   1.1   deraadt 
     97   1.1   deraadt #define ETHER_MIN_LEN	64
     98   1.1   deraadt #define ETHER_MAX_LEN	1518
     99   1.1   deraadt #define ETHER_ADDR_LEN	6
    100   1.1   deraadt 
    101   1.1   deraadt #define EG_INLEN  	10
    102   1.1   deraadt #define EG_BUFLEN	0x0670
    103   1.1   deraadt 
    104   1.1   deraadt /*
    105   1.1   deraadt  * Ethernet software status per interface.
    106   1.1   deraadt  */
    107   1.1   deraadt struct eg_softc {
    108   1.1   deraadt 	struct device sc_dev;
    109  1.12       cgd 	void *sc_ih;
    110  1.33        is 	struct ethercom sc_ethercom;	/* Ethernet common part */
    111  1.32   thorpej 	bus_space_tag_t sc_iot;		/* bus space identifier */
    112  1.32   thorpej 	bus_space_handle_t sc_ioh;	/* i/o handle */
    113  1.27   thorpej 	u_int8_t eg_rom_major;		/* Cards ROM version (major number) */
    114  1.27   thorpej 	u_int8_t eg_rom_minor;		/* Cards ROM version (minor number) */
    115  1.27   thorpej 	short	 eg_ram;		/* Amount of RAM on the card */
    116  1.27   thorpej 	u_int8_t eg_pcb[64];		/* Primary Command Block buffer */
    117  1.27   thorpej 	u_int8_t eg_incount;		/* Number of buffers currently used */
    118  1.27   thorpej 	caddr_t	eg_inbuf;		/* Incoming packet buffer */
    119  1.27   thorpej 	caddr_t	eg_outbuf;		/* Outgoing packet buffer */
    120  1.37  explorer 
    121  1.37  explorer #if NRND > 0
    122  1.37  explorer 	rndsource_element_t rnd_source;
    123  1.37  explorer #endif
    124   1.1   deraadt };
    125   1.1   deraadt 
    126   1.6   mycroft int egprobe __P((struct device *, void *, void *));
    127   1.6   mycroft void egattach __P((struct device *, struct device *, void *));
    128   1.1   deraadt 
    129  1.23   thorpej struct cfattach eg_ca = {
    130  1.23   thorpej 	sizeof(struct eg_softc), egprobe, egattach
    131  1.23   thorpej };
    132  1.23   thorpej 
    133  1.23   thorpej struct cfdriver eg_cd = {
    134  1.23   thorpej 	NULL, "eg", DV_IFNET
    135   1.1   deraadt };
    136   1.1   deraadt 
    137  1.12       cgd int egintr __P((void *));
    138  1.18   mycroft void eginit __P((struct eg_softc *));
    139  1.18   mycroft int egioctl __P((struct ifnet *, u_long, caddr_t));
    140  1.19   mycroft void egrecv __P((struct eg_softc *));
    141  1.18   mycroft void egstart __P((struct ifnet *));
    142  1.25   thorpej void egwatchdog __P((struct ifnet *));
    143  1.18   mycroft void egreset __P((struct eg_softc *));
    144  1.18   mycroft void egread __P((struct eg_softc *, caddr_t, int));
    145  1.18   mycroft struct mbuf *egget __P((struct eg_softc *, caddr_t, int));
    146  1.18   mycroft void egstop __P((struct eg_softc *));
    147   1.1   deraadt 
    148  1.31   thorpej static inline void egprintpcb __P((struct eg_softc *));
    149  1.31   thorpej static inline void egprintstat __P((u_char));
    150  1.31   thorpej static int egoutPCB __P((struct eg_softc *, u_int8_t));
    151  1.31   thorpej static int egreadPCBstat __P((struct eg_softc *, u_int8_t));
    152  1.31   thorpej static int egreadPCBready __P((struct eg_softc *));
    153  1.31   thorpej static int egwritePCB __P((struct eg_softc *));
    154  1.31   thorpej static int egreadPCB __P((struct eg_softc *));
    155  1.31   thorpej 
    156   1.1   deraadt /*
    157   1.1   deraadt  * Support stuff
    158   1.1   deraadt  */
    159   1.1   deraadt 
    160   1.1   deraadt static inline void
    161   1.1   deraadt egprintpcb(sc)
    162   1.1   deraadt 	struct eg_softc *sc;
    163   1.1   deraadt {
    164   1.1   deraadt 	int i;
    165   1.1   deraadt 
    166   1.1   deraadt 	for (i = 0; i < sc->eg_pcb[1] + 2; i++)
    167  1.29  christos 		DPRINTF(("pcb[%2d] = %x\n", i, sc->eg_pcb[i]));
    168   1.1   deraadt }
    169   1.1   deraadt 
    170   1.1   deraadt 
    171   1.1   deraadt static inline void
    172   1.1   deraadt egprintstat(b)
    173   1.1   deraadt 	u_char b;
    174   1.1   deraadt {
    175  1.29  christos 	DPRINTF(("%s %s %s %s %s %s %s\n",
    176   1.1   deraadt 		 (b & EG_STAT_HCRE)?"HCRE":"",
    177   1.1   deraadt 		 (b & EG_STAT_ACRF)?"ACRF":"",
    178   1.1   deraadt 		 (b & EG_STAT_DIR )?"DIR ":"",
    179   1.1   deraadt 		 (b & EG_STAT_DONE)?"DONE":"",
    180   1.1   deraadt 		 (b & EG_STAT_ASF3)?"ASF3":"",
    181   1.1   deraadt 		 (b & EG_STAT_ASF2)?"ASF2":"",
    182   1.1   deraadt 		 (b & EG_STAT_ASF1)?"ASF1":""));
    183   1.1   deraadt }
    184   1.1   deraadt 
    185   1.1   deraadt static int
    186   1.1   deraadt egoutPCB(sc, b)
    187   1.1   deraadt 	struct eg_softc *sc;
    188  1.27   thorpej 	u_int8_t b;
    189   1.1   deraadt {
    190  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    191  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    192   1.1   deraadt 	int i;
    193   1.1   deraadt 
    194   1.1   deraadt 	for (i=0; i < 4000; i++) {
    195  1.32   thorpej 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE) {
    196  1.32   thorpej 			bus_space_write_1(iot, ioh, EG_COMMAND, b);
    197   1.1   deraadt 			return 0;
    198   1.1   deraadt 		}
    199   1.1   deraadt 		delay(10);
    200   1.1   deraadt 	}
    201  1.29  christos 	DPRINTF(("egoutPCB failed\n"));
    202   1.1   deraadt 	return 1;
    203   1.1   deraadt }
    204   1.1   deraadt 
    205   1.1   deraadt static int
    206   1.1   deraadt egreadPCBstat(sc, statb)
    207   1.1   deraadt 	struct eg_softc *sc;
    208  1.27   thorpej 	u_int8_t statb;
    209   1.1   deraadt {
    210  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    211  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    212   1.1   deraadt 	int i;
    213   1.1   deraadt 
    214   1.1   deraadt 	for (i=0; i < 5000; i++) {
    215  1.32   thorpej 		if ((bus_space_read_1(iot, ioh, EG_STATUS) &
    216  1.27   thorpej 		    EG_PCB_STAT) != EG_PCB_NULL)
    217   1.1   deraadt 			break;
    218   1.1   deraadt 		delay(10);
    219   1.1   deraadt 	}
    220  1.32   thorpej 	if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) == statb)
    221   1.1   deraadt 		return 0;
    222   1.1   deraadt 	return 1;
    223   1.1   deraadt }
    224   1.1   deraadt 
    225   1.1   deraadt static int
    226   1.1   deraadt egreadPCBready(sc)
    227   1.1   deraadt 	struct eg_softc *sc;
    228   1.1   deraadt {
    229  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    230  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    231   1.1   deraadt 	int i;
    232   1.1   deraadt 
    233   1.1   deraadt 	for (i=0; i < 10000; i++) {
    234  1.32   thorpej 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF)
    235   1.1   deraadt 			return 0;
    236   1.1   deraadt 		delay(5);
    237   1.1   deraadt 	}
    238  1.29  christos 	DPRINTF(("PCB read not ready\n"));
    239   1.1   deraadt 	return 1;
    240   1.1   deraadt }
    241   1.1   deraadt 
    242   1.1   deraadt static int
    243   1.1   deraadt egwritePCB(sc)
    244   1.1   deraadt 	struct eg_softc *sc;
    245   1.1   deraadt {
    246  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    247  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    248   1.1   deraadt 	int i;
    249  1.27   thorpej 	u_int8_t len;
    250   1.1   deraadt 
    251  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL,
    252  1.32   thorpej 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL);
    253   1.1   deraadt 
    254   1.1   deraadt 	len = sc->eg_pcb[1] + 2;
    255   1.1   deraadt 	for (i = 0; i < len; i++)
    256   1.1   deraadt 		egoutPCB(sc, sc->eg_pcb[i]);
    257  1.27   thorpej 
    258   1.1   deraadt 	for (i=0; i < 4000; i++) {
    259  1.32   thorpej 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE)
    260   1.1   deraadt 			break;
    261   1.1   deraadt 		delay(10);
    262   1.1   deraadt 	}
    263  1.17   mycroft 
    264  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL,
    265  1.32   thorpej 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_DONE);
    266  1.17   mycroft 
    267   1.1   deraadt 	egoutPCB(sc, len);
    268   1.1   deraadt 
    269   1.1   deraadt 	if (egreadPCBstat(sc, EG_PCB_ACCEPT))
    270   1.1   deraadt 		return 1;
    271   1.1   deraadt 	return 0;
    272   1.1   deraadt }
    273   1.1   deraadt 
    274   1.1   deraadt static int
    275   1.1   deraadt egreadPCB(sc)
    276   1.1   deraadt 	struct eg_softc *sc;
    277   1.1   deraadt {
    278  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    279  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    280   1.1   deraadt 	int i;
    281  1.27   thorpej 	u_int8_t b;
    282  1.27   thorpej 
    283  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL,
    284  1.32   thorpej 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL);
    285   1.1   deraadt 
    286   1.1   deraadt 	bzero(sc->eg_pcb, sizeof(sc->eg_pcb));
    287   1.1   deraadt 
    288   1.1   deraadt 	if (egreadPCBready(sc))
    289   1.1   deraadt 		return 1;
    290   1.1   deraadt 
    291  1.32   thorpej 	sc->eg_pcb[0] = bus_space_read_1(iot, ioh, EG_COMMAND);
    292  1.27   thorpej 
    293   1.1   deraadt 	if (egreadPCBready(sc))
    294   1.1   deraadt 		return 1;
    295   1.1   deraadt 
    296  1.32   thorpej 	sc->eg_pcb[1] = bus_space_read_1(iot, ioh, EG_COMMAND);
    297   1.1   deraadt 
    298   1.1   deraadt 	if (sc->eg_pcb[1] > 62) {
    299  1.29  christos 		DPRINTF(("len %d too large\n", sc->eg_pcb[1]));
    300   1.1   deraadt 		return 1;
    301   1.1   deraadt 	}
    302  1.27   thorpej 
    303   1.1   deraadt 	for (i = 0; i < sc->eg_pcb[1]; i++) {
    304   1.1   deraadt 		if (egreadPCBready(sc))
    305   1.1   deraadt 			return 1;
    306  1.32   thorpej 		sc->eg_pcb[2+i] = bus_space_read_1(iot, ioh, EG_COMMAND);
    307   1.1   deraadt 	}
    308   1.1   deraadt 	if (egreadPCBready(sc))
    309   1.1   deraadt 		return 1;
    310   1.1   deraadt 	if (egreadPCBstat(sc, EG_PCB_DONE))
    311   1.1   deraadt 		return 1;
    312  1.32   thorpej 	if ((b = bus_space_read_1(iot, ioh, EG_COMMAND)) != sc->eg_pcb[1] + 2) {
    313  1.29  christos 		DPRINTF(("%d != %d\n", b, sc->eg_pcb[1] + 2));
    314   1.1   deraadt 		return 1;
    315   1.1   deraadt 	}
    316  1.17   mycroft 
    317  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL,
    318  1.32   thorpej 	    (bus_space_read_1(iot, ioh, EG_CONTROL) &
    319  1.27   thorpej 	    ~EG_PCB_STAT) | EG_PCB_ACCEPT);
    320  1.17   mycroft 
    321   1.1   deraadt 	return 0;
    322   1.1   deraadt }
    323   1.1   deraadt 
    324   1.1   deraadt /*
    325   1.1   deraadt  * Real stuff
    326   1.1   deraadt  */
    327   1.1   deraadt 
    328   1.1   deraadt int
    329   1.6   mycroft egprobe(parent, match, aux)
    330   1.6   mycroft 	struct device *parent;
    331   1.6   mycroft 	void *match, *aux;
    332   1.1   deraadt {
    333   1.6   mycroft 	struct eg_softc *sc = match;
    334   1.1   deraadt 	struct isa_attach_args *ia = aux;
    335  1.32   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    336  1.32   thorpej 	bus_space_handle_t ioh;
    337  1.27   thorpej 	int i, rval;
    338  1.27   thorpej 
    339  1.27   thorpej 	rval = 0;
    340   1.1   deraadt 
    341  1.31   thorpej 	if ((ia->ia_iobase & ~0x07f0) != 0) {
    342  1.29  christos 		DPRINTF(("Weird iobase %x\n", ia->ia_iobase));
    343   1.1   deraadt 		return 0;
    344   1.1   deraadt 	}
    345  1.27   thorpej 
    346  1.27   thorpej 	/* Map i/o space. */
    347  1.32   thorpej 	if (bus_space_map(iot, ia->ia_iobase, 0x08, 0, &ioh)) {
    348  1.29  christos 		DPRINTF(("egprobe: can't map i/o space in probe\n"));
    349  1.27   thorpej 		return 0;
    350  1.27   thorpej 	}
    351  1.27   thorpej 
    352  1.27   thorpej 	/*
    353  1.27   thorpej 	 * XXX Indirect brokenness.
    354  1.27   thorpej 	 */
    355  1.32   thorpej 	sc->sc_iot = iot;			/* XXX */
    356  1.27   thorpej 	sc->sc_ioh = ioh;		/* XXX */
    357   1.1   deraadt 
    358   1.1   deraadt 	/* hard reset card */
    359  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_RESET);
    360  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    361   1.1   deraadt 	for (i = 0; i < 5000; i++) {
    362   1.1   deraadt 		delay(1000);
    363  1.32   thorpej 		if ((bus_space_read_1(iot, ioh, EG_STATUS) &
    364  1.27   thorpej 		    EG_PCB_STAT) == EG_PCB_NULL)
    365   1.1   deraadt 			break;
    366   1.1   deraadt 	}
    367  1.32   thorpej 	if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) != EG_PCB_NULL) {
    368  1.29  christos 		DPRINTF(("egprobe: Reset failed\n"));
    369  1.27   thorpej 		goto out;
    370   1.1   deraadt 	}
    371   1.1   deraadt 	sc->eg_pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */
    372   1.1   deraadt 	sc->eg_pcb[1] = 0;
    373   1.1   deraadt 	if (egwritePCB(sc) != 0)
    374  1.27   thorpej 		goto out;
    375  1.27   thorpej 
    376   1.1   deraadt 	if (egreadPCB(sc) != 0) {
    377   1.1   deraadt 		egprintpcb(sc);
    378  1.27   thorpej 		goto out;
    379   1.1   deraadt 	}
    380   1.1   deraadt 
    381   1.1   deraadt 	if (sc->eg_pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */
    382   1.1   deraadt 	    sc->eg_pcb[1] != 0x0a) {
    383   1.1   deraadt 		egprintpcb(sc);
    384  1.27   thorpej 		goto out;
    385   1.1   deraadt 	}
    386   1.1   deraadt 	sc->eg_rom_major = sc->eg_pcb[3];
    387   1.1   deraadt 	sc->eg_rom_minor = sc->eg_pcb[2];
    388   1.1   deraadt 	sc->eg_ram = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    389  1.27   thorpej 
    390   1.1   deraadt 	ia->ia_iosize = 0x08;
    391   1.1   deraadt 	ia->ia_msize = 0;
    392  1.27   thorpej 	rval = 1;
    393  1.27   thorpej 
    394  1.27   thorpej  out:
    395  1.32   thorpej 	bus_space_unmap(iot, ioh, 0x08);
    396  1.27   thorpej 	return rval;
    397   1.1   deraadt }
    398   1.1   deraadt 
    399  1.19   mycroft void
    400   1.1   deraadt egattach(parent, self, aux)
    401   1.1   deraadt 	struct device *parent, *self;
    402   1.1   deraadt 	void *aux;
    403   1.1   deraadt {
    404   1.2   mycroft 	struct eg_softc *sc = (void *)self;
    405   1.2   mycroft 	struct isa_attach_args *ia = aux;
    406  1.32   thorpej 	bus_space_tag_t iot = ia->ia_iot;
    407  1.32   thorpej 	bus_space_handle_t ioh;
    408  1.33        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    409  1.33        is 	u_int8_t myaddr[ETHER_ADDR_LEN];
    410  1.27   thorpej 
    411  1.30  christos 	printf("\n");
    412  1.27   thorpej 
    413  1.27   thorpej 	/* Map i/o space. */
    414  1.32   thorpej 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    415  1.30  christos 		printf("%s: can't map i/o space\n", self->dv_xname);
    416  1.27   thorpej 		return;
    417  1.27   thorpej 	}
    418  1.27   thorpej 
    419  1.32   thorpej 	sc->sc_iot = iot;
    420  1.27   thorpej 	sc->sc_ioh = ioh;
    421  1.27   thorpej 
    422   1.2   mycroft 	egstop(sc);
    423   1.1   deraadt 
    424   1.1   deraadt 	sc->eg_pcb[0] = EG_CMD_GETEADDR; /* Get Station address */
    425   1.1   deraadt 	sc->eg_pcb[1] = 0;
    426   1.1   deraadt 	if (egwritePCB(sc) != 0) {
    427  1.30  christos 		printf("%s: can't send Get Station Address\n", self->dv_xname);
    428   1.1   deraadt 		return;
    429   1.1   deraadt 	}
    430   1.1   deraadt 	if (egreadPCB(sc) != 0) {
    431  1.30  christos 		printf("%s: can't read station address\n", self->dv_xname);
    432   1.1   deraadt 		egprintpcb(sc);
    433   1.1   deraadt 		return;
    434   1.1   deraadt 	}
    435   1.1   deraadt 
    436   1.1   deraadt 	/* check Get station address response */
    437   1.1   deraadt 	if (sc->eg_pcb[0] != EG_RSP_GETEADDR || sc->eg_pcb[1] != 0x06) {
    438  1.30  christos 		printf("%s: card responded with garbage (1)\n",
    439  1.27   thorpej 		    self->dv_xname);
    440   1.1   deraadt 		egprintpcb(sc);
    441   1.1   deraadt 		return;
    442   1.1   deraadt 	}
    443  1.33        is 	bcopy(&sc->eg_pcb[2], myaddr, ETHER_ADDR_LEN);
    444   1.1   deraadt 
    445  1.30  christos 	printf("%s: ROM v%d.%02d %dk address %s\n", self->dv_xname,
    446   1.2   mycroft 	    sc->eg_rom_major, sc->eg_rom_minor, sc->eg_ram,
    447  1.33        is 	    ether_sprintf(myaddr));
    448   1.1   deraadt 
    449   1.1   deraadt 	sc->eg_pcb[0] = EG_CMD_SETEADDR; /* Set station address */
    450   1.1   deraadt 	if (egwritePCB(sc) != 0) {
    451  1.30  christos 		printf("%s: can't send Set Station Address\n", self->dv_xname);
    452   1.1   deraadt 		return;
    453   1.1   deraadt 	}
    454   1.1   deraadt 	if (egreadPCB(sc) != 0) {
    455  1.30  christos 		printf("%s: can't read Set Station Address status\n",
    456  1.27   thorpej 		    self->dv_xname);
    457   1.1   deraadt 		egprintpcb(sc);
    458   1.1   deraadt 		return;
    459   1.1   deraadt 	}
    460   1.2   mycroft 	if (sc->eg_pcb[0] != EG_RSP_SETEADDR || sc->eg_pcb[1] != 0x02 ||
    461   1.7   mycroft 	    sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) {
    462  1.30  christos 		printf("%s: card responded with garbage (2)\n",
    463  1.27   thorpej 		    self->dv_xname);
    464   1.1   deraadt 		egprintpcb(sc);
    465   1.1   deraadt 		return;
    466   1.1   deraadt 	}
    467   1.1   deraadt 
    468   1.2   mycroft 	/* Initialize ifnet structure. */
    469  1.25   thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    470  1.25   thorpej 	ifp->if_softc = sc;
    471   1.1   deraadt 	ifp->if_start = egstart;
    472   1.1   deraadt 	ifp->if_ioctl = egioctl;
    473   1.1   deraadt 	ifp->if_watchdog = egwatchdog;
    474   1.1   deraadt 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    475   1.1   deraadt 
    476   1.1   deraadt 	/* Now we can attach the interface. */
    477   1.1   deraadt 	if_attach(ifp);
    478  1.33        is 	ether_ifattach(ifp, myaddr);
    479   1.1   deraadt 
    480   1.1   deraadt #if NBPFILTER > 0
    481   1.2   mycroft 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    482   1.1   deraadt #endif
    483  1.12       cgd 
    484  1.24       cgd 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    485  1.24       cgd 	    IPL_NET, egintr, sc);
    486  1.37  explorer 
    487  1.37  explorer #if NRND > 0
    488  1.37  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
    489  1.37  explorer #endif
    490   1.1   deraadt }
    491   1.1   deraadt 
    492  1.18   mycroft void
    493   1.1   deraadt eginit(sc)
    494   1.1   deraadt 	register struct eg_softc *sc;
    495   1.1   deraadt {
    496  1.33        is 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    497  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    498  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    499   1.1   deraadt 
    500   1.1   deraadt 	/* soft reset the board */
    501  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_FLSH);
    502   1.1   deraadt 	delay(100);
    503  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_ATTN);
    504   1.1   deraadt 	delay(100);
    505  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    506   1.1   deraadt 	delay(200);
    507   1.1   deraadt 
    508   1.1   deraadt 	sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */
    509   1.1   deraadt 	sc->eg_pcb[1] = 2;
    510   1.1   deraadt 	sc->eg_pcb[2] = 3; /* receive broadcast & multicast */
    511   1.1   deraadt 	sc->eg_pcb[3] = 0;
    512   1.1   deraadt 	if (egwritePCB(sc) != 0)
    513  1.30  christos 		printf("%s: can't send Configure 82586\n",
    514  1.27   thorpej 		    sc->sc_dev.dv_xname);
    515   1.1   deraadt 
    516   1.1   deraadt 	if (egreadPCB(sc) != 0) {
    517  1.30  christos 		printf("%s: can't read Configure 82586 status\n",
    518  1.27   thorpej 		    sc->sc_dev.dv_xname);
    519   1.1   deraadt 		egprintpcb(sc);
    520   1.1   deraadt 	} else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0)
    521  1.30  christos 		printf("%s: configure card command failed\n",
    522   1.7   mycroft 		    sc->sc_dev.dv_xname);
    523   1.1   deraadt 
    524  1.27   thorpej 	if (sc->eg_inbuf == NULL) {
    525   1.1   deraadt 		sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    526  1.27   thorpej 		if (sc->eg_inbuf == NULL) {
    527  1.30  christos 			printf("%s: can't allocate inbuf\n",
    528  1.27   thorpej 			    sc->sc_dev.dv_xname);
    529  1.27   thorpej 			panic("eginit");
    530  1.27   thorpej 		}
    531  1.27   thorpej 	}
    532   1.1   deraadt 	sc->eg_incount = 0;
    533   1.1   deraadt 
    534  1.27   thorpej 	if (sc->eg_outbuf == NULL) {
    535   1.1   deraadt 		sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    536  1.27   thorpej 		if (sc->eg_outbuf == NULL) {
    537  1.30  christos 			printf("%s: can't allocate outbuf\n",
    538  1.27   thorpej 			    sc->sc_dev.dv_xname);
    539  1.27   thorpej 			panic("eginit");
    540  1.27   thorpej 		}
    541  1.27   thorpej 	}
    542  1.15   mycroft 
    543  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_CMDE);
    544  1.16   mycroft 
    545  1.16   mycroft 	sc->eg_incount = 0;
    546  1.16   mycroft 	egrecv(sc);
    547  1.16   mycroft 
    548  1.16   mycroft 	/* Interface is now `running', with no output active. */
    549   1.1   deraadt 	ifp->if_flags |= IFF_RUNNING;
    550   1.1   deraadt 	ifp->if_flags &= ~IFF_OACTIVE;
    551   1.1   deraadt 
    552  1.16   mycroft 	/* Attempt to start output, if any. */
    553   1.1   deraadt 	egstart(ifp);
    554   1.1   deraadt }
    555   1.1   deraadt 
    556  1.18   mycroft void
    557   1.1   deraadt egrecv(sc)
    558   1.1   deraadt 	struct eg_softc *sc;
    559   1.1   deraadt {
    560   1.2   mycroft 
    561   1.1   deraadt 	while (sc->eg_incount < EG_INLEN) {
    562   1.1   deraadt 		sc->eg_pcb[0] = EG_CMD_RECVPACKET;
    563   1.1   deraadt 		sc->eg_pcb[1] = 0x08;
    564   1.1   deraadt 		sc->eg_pcb[2] = 0; /* address not used.. we send zero */
    565   1.1   deraadt 		sc->eg_pcb[3] = 0;
    566   1.1   deraadt 		sc->eg_pcb[4] = 0;
    567   1.1   deraadt 		sc->eg_pcb[5] = 0;
    568  1.25   thorpej 		sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */
    569  1.25   thorpej 		sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff;
    570   1.1   deraadt 		sc->eg_pcb[8] = 0; /* timeout, 0 == none */
    571   1.1   deraadt 		sc->eg_pcb[9] = 0;
    572  1.16   mycroft 		if (egwritePCB(sc) != 0)
    573   1.1   deraadt 			break;
    574  1.16   mycroft 		sc->eg_incount++;
    575   1.1   deraadt 	}
    576   1.1   deraadt }
    577   1.1   deraadt 
    578  1.18   mycroft void
    579   1.1   deraadt egstart(ifp)
    580   1.1   deraadt 	struct ifnet *ifp;
    581   1.1   deraadt {
    582  1.25   thorpej 	register struct eg_softc *sc = ifp->if_softc;
    583  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    584  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    585   1.1   deraadt 	struct mbuf *m0, *m;
    586  1.15   mycroft 	caddr_t buffer;
    587   1.1   deraadt 	int len;
    588  1.27   thorpej 	u_int16_t *ptr;
    589   1.1   deraadt 
    590   1.1   deraadt 	/* Don't transmit if interface is busy or not running */
    591  1.15   mycroft 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    592  1.14   mycroft 		return;
    593   1.1   deraadt 
    594  1.15   mycroft loop:
    595   1.1   deraadt 	/* Dequeue the next datagram. */
    596  1.15   mycroft 	IF_DEQUEUE(&ifp->if_snd, m0);
    597  1.15   mycroft 	if (m0 == 0)
    598  1.14   mycroft 		return;
    599   1.1   deraadt 
    600  1.15   mycroft 	ifp->if_flags |= IFF_OACTIVE;
    601  1.15   mycroft 
    602  1.15   mycroft 	/* We need to use m->m_pkthdr.len, so require the header */
    603  1.27   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0) {
    604  1.30  christos 		printf("%s: no header mbuf\n", sc->sc_dev.dv_xname);
    605  1.27   thorpej 		panic("egstart");
    606  1.27   thorpej 	}
    607  1.15   mycroft 	len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    608   1.1   deraadt 
    609   1.1   deraadt #if NBPFILTER > 0
    610  1.15   mycroft 	if (ifp->if_bpf)
    611  1.15   mycroft 		bpf_mtap(ifp->if_bpf, m0);
    612   1.1   deraadt #endif
    613   1.1   deraadt 
    614   1.1   deraadt 	sc->eg_pcb[0] = EG_CMD_SENDPACKET;
    615   1.1   deraadt 	sc->eg_pcb[1] = 0x06;
    616   1.1   deraadt 	sc->eg_pcb[2] = 0; /* address not used, we send zero */
    617   1.1   deraadt 	sc->eg_pcb[3] = 0;
    618   1.1   deraadt 	sc->eg_pcb[4] = 0;
    619   1.1   deraadt 	sc->eg_pcb[5] = 0;
    620  1.15   mycroft 	sc->eg_pcb[6] = len; /* length of packet */
    621  1.15   mycroft 	sc->eg_pcb[7] = len >> 8;
    622  1.15   mycroft 	if (egwritePCB(sc) != 0) {
    623  1.30  christos 		printf("%s: can't send Send Packet command\n",
    624  1.27   thorpej 		    sc->sc_dev.dv_xname);
    625  1.15   mycroft 		ifp->if_oerrors++;
    626  1.15   mycroft 		ifp->if_flags &= ~IFF_OACTIVE;
    627  1.27   thorpej 		m_freem(m0);
    628  1.15   mycroft 		goto loop;
    629  1.15   mycroft 	}
    630  1.15   mycroft 
    631  1.15   mycroft 	buffer = sc->eg_outbuf;
    632  1.15   mycroft 	for (m = m0; m != 0; m = m->m_next) {
    633  1.15   mycroft 		bcopy(mtod(m, caddr_t), buffer, m->m_len);
    634  1.15   mycroft 		buffer += m->m_len;
    635   1.1   deraadt 	}
    636  1.15   mycroft 
    637  1.16   mycroft 	/* set direction bit: host -> adapter */
    638  1.32   thorpej 	bus_space_write_1(iot, ioh, EG_CONTROL,
    639  1.32   thorpej 	    bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_CTL_DIR);
    640  1.16   mycroft 
    641  1.27   thorpej 	for (ptr = (u_int16_t *) sc->eg_outbuf; len > 0; len -= 2) {
    642  1.32   thorpej 		bus_space_write_2(iot, ioh, EG_DATA, *ptr++);
    643  1.32   thorpej 		while (!(bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HRDY))
    644  1.15   mycroft 			; /* XXX need timeout here */
    645  1.15   mycroft 	}
    646  1.15   mycroft 
    647  1.15   mycroft 	m_freem(m0);
    648   1.1   deraadt }
    649   1.1   deraadt 
    650   1.1   deraadt int
    651  1.12       cgd egintr(arg)
    652  1.12       cgd 	void *arg;
    653   1.1   deraadt {
    654  1.12       cgd 	register struct eg_softc *sc = arg;
    655  1.32   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    656  1.32   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    657  1.27   thorpej 	int i, len, serviced;
    658  1.27   thorpej 	u_int16_t *ptr;
    659   1.1   deraadt 
    660  1.27   thorpej 	serviced = 0;
    661  1.27   thorpej 
    662  1.32   thorpej 	while (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) {
    663   1.1   deraadt 		egreadPCB(sc);
    664   1.2   mycroft 		switch (sc->eg_pcb[0]) {
    665   1.1   deraadt 		case EG_RSP_RECVPACKET:
    666   1.7   mycroft 			len = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    667  1.16   mycroft 
    668  1.16   mycroft 			/* Set direction bit : Adapter -> host */
    669  1.32   thorpej 			bus_space_write_1(iot, ioh, EG_CONTROL,
    670  1.32   thorpej 			    bus_space_read_1(iot, ioh, EG_CONTROL) | EG_CTL_DIR);
    671  1.16   mycroft 
    672  1.27   thorpej 			for (ptr = (u_int16_t *) sc->eg_inbuf;
    673  1.27   thorpej 			    len > 0; len -= 2) {
    674  1.32   thorpej 				while (!(bus_space_read_1(iot, ioh, EG_STATUS) &
    675  1.27   thorpej 				    EG_STAT_HRDY))
    676   1.1   deraadt 					;
    677  1.32   thorpej 				*ptr++ = bus_space_read_2(iot, ioh, EG_DATA);
    678   1.1   deraadt 			}
    679  1.16   mycroft 
    680   1.7   mycroft 			len = sc->eg_pcb[8] | (sc->eg_pcb[9] << 8);
    681   1.1   deraadt 			egread(sc, sc->eg_inbuf, len);
    682  1.16   mycroft 
    683   1.1   deraadt 			sc->eg_incount--;
    684  1.16   mycroft 			egrecv(sc);
    685  1.27   thorpej 			serviced = 1;
    686   1.1   deraadt 			break;
    687   1.1   deraadt 
    688   1.1   deraadt 		case EG_RSP_SENDPACKET:
    689   1.1   deraadt 			if (sc->eg_pcb[6] || sc->eg_pcb[7]) {
    690  1.29  christos 				DPRINTF(("%s: packet dropped\n",
    691  1.27   thorpej 				    sc->sc_dev.dv_xname));
    692  1.33        is 				sc->sc_ethercom.ec_if.if_oerrors++;
    693   1.2   mycroft 			} else
    694  1.33        is 				sc->sc_ethercom.ec_if.if_opackets++;
    695  1.33        is 			sc->sc_ethercom.ec_if.if_collisions +=
    696  1.27   thorpej 			    sc->eg_pcb[8] & 0xf;
    697  1.33        is 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    698  1.33        is 			egstart(&sc->sc_ethercom.ec_if);
    699  1.27   thorpej 			serviced = 1;
    700   1.1   deraadt 			break;
    701   1.1   deraadt 
    702  1.27   thorpej 		/* XXX byte-order and type-size bugs here... */
    703   1.1   deraadt 		case EG_RSP_GETSTATS:
    704  1.29  christos 			DPRINTF(("%s: Card Statistics\n",
    705  1.27   thorpej 			    sc->sc_dev.dv_xname));
    706   1.3   deraadt 			bcopy(&sc->eg_pcb[2], &i, sizeof(i));
    707  1.29  christos 			DPRINTF(("Receive Packets %d\n", i));
    708   1.3   deraadt 			bcopy(&sc->eg_pcb[6], &i, sizeof(i));
    709  1.29  christos 			DPRINTF(("Transmit Packets %d\n", i));
    710  1.29  christos 			DPRINTF(("CRC errors %d\n",
    711  1.27   thorpej 			    *(short *) &sc->eg_pcb[10]));
    712  1.29  christos 			DPRINTF(("alignment errors %d\n",
    713  1.27   thorpej 			    *(short *) &sc->eg_pcb[12]));
    714  1.29  christos 			DPRINTF(("no resources errors %d\n",
    715  1.27   thorpej 			    *(short *) &sc->eg_pcb[14]));
    716  1.29  christos 			DPRINTF(("overrun errors %d\n",
    717  1.27   thorpej 			    *(short *) &sc->eg_pcb[16]));
    718  1.27   thorpej 			serviced = 1;
    719   1.1   deraadt 			break;
    720   1.1   deraadt 
    721   1.1   deraadt 		default:
    722  1.30  christos 			printf("%s: egintr: Unknown response %x??\n",
    723  1.27   thorpej 			    sc->sc_dev.dv_xname, sc->eg_pcb[0]);
    724   1.3   deraadt 			egprintpcb(sc);
    725   1.1   deraadt 			break;
    726   1.1   deraadt 		}
    727  1.37  explorer 
    728  1.37  explorer #if NRND > 0
    729  1.37  explorer 		rnd_add_uint32(&sc->rnd_source, sc->eg_pcb[0]);
    730  1.37  explorer #endif
    731   1.1   deraadt 	}
    732   1.1   deraadt 
    733  1.27   thorpej 	return serviced;
    734   1.1   deraadt }
    735   1.1   deraadt 
    736   1.1   deraadt /*
    737   1.1   deraadt  * Pass a packet up to the higher levels.
    738   1.1   deraadt  */
    739  1.18   mycroft void
    740   1.1   deraadt egread(sc, buf, len)
    741   1.1   deraadt 	struct eg_softc *sc;
    742   1.1   deraadt 	caddr_t buf;
    743   1.1   deraadt 	int len;
    744   1.1   deraadt {
    745  1.33        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    746   1.1   deraadt 	struct mbuf *m;
    747   1.2   mycroft 	struct ether_header *eh;
    748   1.1   deraadt 
    749   1.1   deraadt 	if (len <= sizeof(struct ether_header) ||
    750   1.1   deraadt 	    len > ETHER_MAX_LEN) {
    751  1.30  christos 		printf("%s: invalid packet size %d; dropping\n",
    752  1.20   mycroft 		    sc->sc_dev.dv_xname, len);
    753  1.18   mycroft 		ifp->if_ierrors++;
    754   1.1   deraadt 		return;
    755   1.1   deraadt 	}
    756   1.1   deraadt 
    757   1.2   mycroft 	/* Pull packet off interface. */
    758  1.18   mycroft 	m = egget(sc, buf, len);
    759   1.1   deraadt 	if (m == 0) {
    760  1.18   mycroft 		ifp->if_ierrors++;
    761   1.1   deraadt 		return;
    762   1.1   deraadt 	}
    763   1.1   deraadt 
    764  1.18   mycroft 	ifp->if_ipackets++;
    765  1.18   mycroft 
    766   1.2   mycroft 	/* We assume the header fit entirely in one mbuf. */
    767   1.2   mycroft 	eh = mtod(m, struct ether_header *);
    768   1.2   mycroft 
    769   1.1   deraadt #if NBPFILTER > 0
    770   1.1   deraadt 	/*
    771   1.1   deraadt 	 * Check if there's a BPF listener on this interface.
    772   1.2   mycroft 	 * If so, hand off the raw packet to BPF.
    773   1.1   deraadt 	 */
    774   1.2   mycroft 	if (ifp->if_bpf) {
    775   1.2   mycroft 		bpf_mtap(ifp->if_bpf, m);
    776   1.1   deraadt 
    777   1.1   deraadt 		/*
    778   1.1   deraadt 		 * Note that the interface cannot be in promiscuous mode if
    779   1.1   deraadt 		 * there are no BPF listeners.  And if we are in promiscuous
    780   1.1   deraadt 		 * mode, we have to check if this packet is really ours.
    781   1.1   deraadt 		 */
    782   1.2   mycroft 		if ((ifp->if_flags & IFF_PROMISC) &&
    783   1.1   deraadt 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    784  1.33        is 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    785   1.1   deraadt 			    sizeof(eh->ether_dhost)) != 0) {
    786   1.1   deraadt 			m_freem(m);
    787   1.1   deraadt 			return;
    788   1.1   deraadt 		}
    789   1.1   deraadt 	}
    790   1.1   deraadt #endif
    791   1.1   deraadt 
    792   1.2   mycroft 	/* We assume the header fit entirely in one mbuf. */
    793  1.18   mycroft 	m_adj(m, sizeof(struct ether_header));
    794   1.2   mycroft 	ether_input(ifp, eh, m);
    795   1.1   deraadt }
    796   1.1   deraadt 
    797   1.1   deraadt /*
    798   1.1   deraadt  * convert buf into mbufs
    799   1.1   deraadt  */
    800  1.18   mycroft struct mbuf *
    801  1.18   mycroft egget(sc, buf, totlen)
    802  1.18   mycroft 	struct eg_softc *sc;
    803   1.2   mycroft 	caddr_t buf;
    804   1.2   mycroft 	int totlen;
    805   1.2   mycroft {
    806  1.33        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    807   1.2   mycroft 	struct mbuf *top, **mp, *m;
    808   1.2   mycroft 	int len;
    809   1.1   deraadt 
    810   1.2   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    811  1.18   mycroft 	if (m == 0)
    812   1.2   mycroft 		return 0;
    813   1.2   mycroft 	m->m_pkthdr.rcvif = ifp;
    814   1.2   mycroft 	m->m_pkthdr.len = totlen;
    815   1.2   mycroft 	len = MHLEN;
    816   1.2   mycroft 	top = 0;
    817   1.2   mycroft 	mp = &top;
    818   1.2   mycroft 
    819   1.2   mycroft 	while (totlen > 0) {
    820   1.2   mycroft 		if (top) {
    821   1.2   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    822   1.2   mycroft 			if (m == 0) {
    823   1.2   mycroft 				m_freem(top);
    824   1.2   mycroft 				return 0;
    825   1.2   mycroft 			}
    826   1.2   mycroft 			len = MLEN;
    827   1.2   mycroft 		}
    828   1.2   mycroft 		if (totlen >= MINCLSIZE) {
    829   1.2   mycroft 			MCLGET(m, M_DONTWAIT);
    830  1.35   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    831  1.36   mycroft 				m_free(m);
    832  1.34   mycroft 				m_freem(top);
    833  1.34   mycroft 				return 0;
    834  1.34   mycroft 			}
    835  1.34   mycroft 			len = MCLBYTES;
    836   1.2   mycroft 		}
    837   1.2   mycroft 		m->m_len = len = min(totlen, len);
    838   1.2   mycroft 		bcopy((caddr_t)buf, mtod(m, caddr_t), len);
    839   1.2   mycroft 		buf += len;
    840   1.2   mycroft 		totlen -= len;
    841   1.2   mycroft 		*mp = m;
    842   1.2   mycroft 		mp = &m->m_next;
    843   1.2   mycroft 	}
    844   1.1   deraadt 
    845   1.2   mycroft 	return top;
    846   1.1   deraadt }
    847   1.1   deraadt 
    848  1.18   mycroft int
    849  1.18   mycroft egioctl(ifp, cmd, data)
    850   1.1   deraadt 	register struct ifnet *ifp;
    851  1.18   mycroft 	u_long cmd;
    852   1.1   deraadt 	caddr_t data;
    853   1.1   deraadt {
    854  1.25   thorpej 	struct eg_softc *sc = ifp->if_softc;
    855  1.18   mycroft 	struct ifaddr *ifa = (struct ifaddr *)data;
    856   1.2   mycroft 	int s, error = 0;
    857   1.2   mycroft 
    858  1.21   mycroft 	s = splnet();
    859   1.2   mycroft 
    860  1.18   mycroft 	switch (cmd) {
    861   1.1   deraadt 
    862   1.1   deraadt 	case SIOCSIFADDR:
    863   1.1   deraadt 		ifp->if_flags |= IFF_UP;
    864   1.2   mycroft 
    865   1.1   deraadt 		switch (ifa->ifa_addr->sa_family) {
    866   1.1   deraadt #ifdef INET
    867   1.1   deraadt 		case AF_INET:
    868  1.11   mycroft 			eginit(sc);
    869  1.33        is 			arp_ifinit(ifp, ifa);
    870   1.1   deraadt 			break;
    871   1.1   deraadt #endif
    872   1.1   deraadt #ifdef NS
    873   1.1   deraadt 		case AF_NS:
    874   1.2   mycroft 		    {
    875   1.2   mycroft 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    876   1.1   deraadt 
    877   1.2   mycroft 			if (ns_nullhost(*ina))
    878   1.2   mycroft 				ina->x_host =
    879  1.33        is 				   *(union ns_host *)LLADDR(ifp->if_sadl);
    880   1.2   mycroft 			else
    881  1.33        is 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    882  1.33        is 				    ETHER_ADDR_LEN);
    883   1.2   mycroft 			/* Set new address. */
    884   1.2   mycroft 			eginit(sc);
    885   1.2   mycroft 			break;
    886   1.2   mycroft 		    }
    887   1.1   deraadt #endif
    888   1.1   deraadt 		default:
    889   1.1   deraadt 			eginit(sc);
    890   1.1   deraadt 			break;
    891   1.1   deraadt 		}
    892   1.1   deraadt 		break;
    893   1.2   mycroft 
    894   1.1   deraadt 	case SIOCSIFFLAGS:
    895   1.1   deraadt 		if ((ifp->if_flags & IFF_UP) == 0 &&
    896   1.1   deraadt 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    897   1.2   mycroft 			/*
    898   1.2   mycroft 			 * If interface is marked down and it is running, then
    899   1.2   mycroft 			 * stop it.
    900   1.2   mycroft 			 */
    901   1.2   mycroft 			egstop(sc);
    902   1.1   deraadt 			ifp->if_flags &= ~IFF_RUNNING;
    903   1.1   deraadt 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    904   1.1   deraadt 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    905   1.2   mycroft 			/*
    906   1.2   mycroft 			 * If interface is marked up and it is stopped, then
    907   1.2   mycroft 			 * start it.
    908   1.2   mycroft 			 */
    909   1.1   deraadt 			eginit(sc);
    910   1.1   deraadt 		} else {
    911   1.3   deraadt 			sc->eg_pcb[0] = EG_CMD_GETSTATS;
    912   1.3   deraadt 			sc->eg_pcb[1] = 0;
    913   1.3   deraadt 			if (egwritePCB(sc) != 0)
    914  1.29  christos 				DPRINTF(("write error\n"));
    915   1.1   deraadt 			/*
    916   1.1   deraadt 			 * XXX deal with flags changes:
    917   1.1   deraadt 			 * IFF_MULTICAST, IFF_PROMISC,
    918   1.1   deraadt 			 * IFF_LINK0, IFF_LINK1,
    919   1.1   deraadt 			 */
    920   1.1   deraadt 		}
    921   1.1   deraadt 		break;
    922   1.2   mycroft 
    923   1.1   deraadt 	default:
    924   1.1   deraadt 		error = EINVAL;
    925  1.18   mycroft 		break;
    926   1.1   deraadt 	}
    927   1.2   mycroft 
    928   1.2   mycroft 	splx(s);
    929   1.2   mycroft 	return error;
    930   1.1   deraadt }
    931   1.1   deraadt 
    932  1.18   mycroft void
    933   1.1   deraadt egreset(sc)
    934   1.1   deraadt 	struct eg_softc *sc;
    935   1.1   deraadt {
    936   1.2   mycroft 	int s;
    937   1.1   deraadt 
    938  1.29  christos 	DPRINTF(("%s: egreset()\n", sc->sc_dev.dv_xname));
    939  1.21   mycroft 	s = splnet();
    940   1.2   mycroft 	egstop(sc);
    941   1.2   mycroft 	eginit(sc);
    942   1.2   mycroft 	splx(s);
    943   1.1   deraadt }
    944   1.1   deraadt 
    945  1.18   mycroft void
    946  1.25   thorpej egwatchdog(ifp)
    947  1.25   thorpej 	struct ifnet *ifp;
    948   1.1   deraadt {
    949  1.25   thorpej 	struct eg_softc *sc = ifp->if_softc;
    950   1.1   deraadt 
    951   1.1   deraadt 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    952  1.33        is 	sc->sc_ethercom.ec_if.if_oerrors++;
    953   1.1   deraadt 
    954   1.1   deraadt 	egreset(sc);
    955   1.1   deraadt }
    956   1.1   deraadt 
    957  1.18   mycroft void
    958   1.1   deraadt egstop(sc)
    959   1.1   deraadt 	register struct eg_softc *sc;
    960   1.1   deraadt {
    961   1.1   deraadt 
    962  1.32   thorpej 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EG_CONTROL, 0);
    963   1.1   deraadt }
    964