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