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