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