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