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