Home | History | Annotate | Line # | Download | only in isa
if_iy.c revision 1.19
      1 /*	$NetBSD: if_iy.c,v 1.19 1998/01/12 09:43:39 thorpej Exp $	*/
      2 /* #define IYDEBUG */
      3 /* #define IYMEMDEBUG */
      4 /*-
      5  * Copyright (c) 1996 Ignatios Souvatzis.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product contains software developed by Ignatios Souvatzis for
     19  *	the NetBSD project.
     20  * 4. The names of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include "bpfilter.h"
     37 #include "rnd.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/buf.h>
     43 #include <sys/protosw.h>
     44 #include <sys/socket.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/errno.h>
     47 #include <sys/syslog.h>
     48 #include <sys/device.h>
     49 #if NRND > 0
     50 #include <sys/rnd.h>
     51 #endif
     52 
     53 #include <net/if.h>
     54 #include <net/if_types.h>
     55 #include <net/if_dl.h>
     56 
     57 #include <net/if_ether.h>
     58 
     59 #if NBPFILTER > 0
     60 #include <net/bpf.h>
     61 #include <net/bpfdesc.h>
     62 #endif
     63 
     64 #ifdef INET
     65 #include <netinet/in.h>
     66 #include <netinet/in_systm.h>
     67 #include <netinet/in_var.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/if_inarp.h>
     70 #endif
     71 
     72 #ifdef NS
     73 #include <netns/ns.h>
     74 #include <netns/ns_if.h>
     75 #endif
     76 
     77 #if defined(SIOCSIFMEDIA)
     78 #include <net/if_media.h>
     79 #endif
     80 
     81 #include <vm/vm.h>
     82 
     83 #include <machine/cpu.h>
     84 #include <machine/bus.h>
     85 #include <machine/intr.h>
     86 
     87 #include <dev/isa/isareg.h>
     88 #include <dev/isa/isavar.h>
     89 #include <dev/ic/i82595reg.h>
     90 
     91 #define	ETHER_MIN_LEN	(ETHERMIN + sizeof(struct ether_header) + 4)
     92 #define	ETHER_MAX_LEN	(ETHERMTU + sizeof(struct ether_header) + 4)
     93 
     94 /*
     95  * Ethernet status, per interface.
     96  */
     97 struct iy_softc {
     98 	struct device sc_dev;
     99 	void *sc_ih;
    100 
    101 	bus_space_tag_t sc_iot;
    102 	bus_space_handle_t sc_ioh;
    103 
    104 	struct ethercom sc_ethercom;
    105 
    106 	struct ifmedia iy_ifmedia;
    107 	int iy_media;
    108 
    109 	int mappedirq;
    110 
    111 	int hard_vers;
    112 
    113 	int promisc;
    114 
    115 	int sram, tx_size, rx_size;
    116 
    117 	int tx_start, tx_end, tx_last;
    118 	int rx_start;
    119 
    120 #ifdef IYDEBUG
    121 	int sc_debug;
    122 #endif
    123 
    124 #if NRND > 0
    125 	rndsource_element_t rnd_source;
    126 #endif
    127 };
    128 
    129 void iywatchdog __P((struct ifnet *));
    130 int iyioctl __P((struct ifnet *, u_long, caddr_t));
    131 int iyintr __P((void *));
    132 void iyinit __P((struct iy_softc *));
    133 void iystop __P((struct iy_softc *));
    134 void iystart __P((struct ifnet *));
    135 
    136 void iy_intr_rx __P((struct iy_softc *));
    137 void iy_intr_tx __P((struct iy_softc *));
    138 
    139 void iyreset __P((struct iy_softc *));
    140 void iy_readframe __P((struct iy_softc *, int));
    141 void iy_drop_packet_buffer __P((struct iy_softc *));
    142 void iy_find_mem_size __P((struct iy_softc *));
    143 void iyrint __P((struct iy_softc *));
    144 void iytint __P((struct iy_softc *));
    145 void iyxmit __P((struct iy_softc *));
    146 void iyget __P((struct iy_softc *, bus_space_tag_t, bus_space_handle_t, int));
    147 void iyprobemem __P((struct iy_softc *));
    148 static __inline void eepromwritebit __P((bus_space_tag_t, bus_space_handle_t,
    149     int));
    150 static __inline int eepromreadbit __P((bus_space_tag_t, bus_space_handle_t));
    151 /*
    152  * void iymeminit __P((void *, struct iy_softc *));
    153  * static int iy_mc_setup __P((struct iy_softc *, void *));
    154  * static void iy_mc_reset __P((struct iy_softc *));
    155  */
    156 #ifdef IYDEBUGX
    157 void print_rbd __P((volatile struct iy_recv_buf_desc *));
    158 
    159 int in_ifrint = 0;
    160 int in_iftint = 0;
    161 #endif
    162 
    163 int iy_mediachange __P((struct ifnet *));
    164 void iy_mediastatus __P((struct ifnet *, struct ifmediareq *));
    165 
    166 #ifdef __BROKEN_INDIRECT_CONFIG
    167 int iyprobe __P((struct device *, void *, void *));
    168 #else
    169 int iyprobe __P((struct device *, struct cfdata *, void *));
    170 #endif
    171 void iyattach __P((struct device *, struct device *, void *));
    172 
    173 static u_int16_t eepromread __P((bus_space_tag_t, bus_space_handle_t, int));
    174 
    175 static int eepromreadall __P((bus_space_tag_t, bus_space_handle_t, u_int16_t *,
    176     int));
    177 
    178 struct cfattach iy_ca = {
    179 	sizeof(struct iy_softc), iyprobe, iyattach
    180 };
    181 
    182 static u_int8_t eepro_irqmap[] = EEPP_INTMAP;
    183 static u_int8_t eepro_revirqmap[] = EEPP_RINTMAP;
    184 
    185 int
    186 iyprobe(parent, match, aux)
    187 	struct device *parent;
    188 #ifdef __BROKEN_INDIRECT_CONFIG
    189 	void *match;
    190 #else
    191 	struct cfdata *match;
    192 #endif
    193 	void *aux;
    194 {
    195 	struct isa_attach_args *ia = aux;
    196 	u_int16_t eaddr[8];
    197 
    198 	bus_space_tag_t iot;
    199 	bus_space_handle_t ioh;
    200 
    201 	u_int8_t c, d;
    202 
    203 	iot = ia->ia_iot;
    204 
    205 	if (ia->ia_iobase == IOBASEUNK)
    206 		return 0;
    207 
    208 	if (bus_space_map(iot, ia->ia_iobase, 16, 0, &ioh))
    209 		return 0;
    210 
    211 	/* try to find the round robin sig: */
    212 
    213 	c = bus_space_read_1(iot, ioh, ID_REG);
    214 	if ((c & ID_REG_MASK) != ID_REG_SIG)
    215 		goto out;
    216 
    217 	d = bus_space_read_1(iot, ioh, ID_REG);
    218 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    219 		goto out;
    220 
    221 	if (((d-c) & R_ROBIN_BITS) != 0x40)
    222 		goto out;
    223 
    224 	d = bus_space_read_1(iot, ioh, ID_REG);
    225 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    226 		goto out;
    227 
    228 	if (((d-c) & R_ROBIN_BITS) != 0x80)
    229 		goto out;
    230 
    231 	d = bus_space_read_1(iot, ioh, ID_REG);
    232 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    233 		goto out;
    234 
    235 	if (((d-c) & R_ROBIN_BITS) != 0xC0)
    236 		goto out;
    237 
    238 	d = bus_space_read_1(iot, ioh, ID_REG);
    239 	if ((d & ID_REG_MASK) != ID_REG_SIG)
    240 		goto out;
    241 
    242 	if (((d-c) & R_ROBIN_BITS) != 0x00)
    243 		goto out;
    244 
    245 #ifdef IYDEBUG
    246 		printf("iyprobe verified working ID reg.\n");
    247 #endif
    248 
    249 	if (eepromreadall(iot, ioh, eaddr, 8))
    250 		goto out;
    251 
    252 	if (ia->ia_irq == IRQUNK)
    253 		ia->ia_irq = eepro_irqmap[eaddr[EEPPW1] & EEPP_Int];
    254 
    255 	if (ia->ia_irq >= sizeof(eepro_revirqmap))
    256 		goto out;
    257 
    258 	if (eepro_revirqmap[ia->ia_irq] == 0xff)
    259 		goto out;
    260 
    261 	/* now lets reset the chip */
    262 
    263 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    264 	delay(200);
    265 
    266 	ia->ia_iosize = 16;
    267 
    268 	bus_space_unmap(iot, ioh, 16);
    269 	return 1;		/* found */
    270 out:
    271 	bus_space_unmap(iot, ioh, 16);
    272 	return 0;
    273 }
    274 
    275 void
    276 iyattach(parent, self, aux)
    277 	struct device *parent, *self;
    278 	void *aux;
    279 {
    280 	struct iy_softc *sc = (void *)self;
    281 	struct isa_attach_args *ia = aux;
    282 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    283 	bus_space_tag_t iot;
    284 	bus_space_handle_t ioh;
    285 	unsigned temp;
    286 	u_int16_t eaddr[8];
    287 	u_int8_t myaddr[ETHER_ADDR_LEN];
    288 	int eirq;
    289 
    290 	iot = ia->ia_iot;
    291 
    292 	if (bus_space_map(iot, ia->ia_iobase, 16, 0, &ioh)) {
    293 		printf(": can't map i/o space\n");
    294 		return;
    295 	}
    296 
    297 	sc->sc_iot = iot;
    298 	sc->sc_ioh = ioh;
    299 
    300 	sc->mappedirq = eepro_revirqmap[ia->ia_irq];
    301 
    302 	/* now let's reset the chip */
    303 
    304 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    305 	delay(200);
    306 
    307 	iyprobemem(sc);
    308 
    309 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    310 	ifp->if_softc = sc;
    311 	ifp->if_start = iystart;
    312 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    313 					/* XXX todo: | IFF_MULTICAST */
    314 
    315 	ifp->if_ioctl = iyioctl;
    316 	ifp->if_watchdog = iywatchdog;
    317 
    318 	(void)eepromreadall(iot, ioh, eaddr, 8);
    319 	sc->hard_vers = eaddr[EEPW6] & EEPP_BoardRev;
    320 
    321 #ifdef DIAGNOSTICS
    322 	if ((eaddr[EEPPEther0] !=
    323 	     eepromread(iot, ioh, EEPPEther0a)) &&
    324 	    (eaddr[EEPPEther1] !=
    325 	     eepromread(iot, ioh, EEPPEther1a)) &&
    326 	    (eaddr[EEPPEther2] !=
    327 	     eepromread(iot, ioh, EEPPEther2a)))
    328 
    329 		printf("EEPROM Ethernet address differs from copy\n");
    330 #endif
    331 
    332         myaddr[1] = eaddr[EEPPEther0] & 0xFF;
    333         myaddr[0] = eaddr[EEPPEther0] >> 8;
    334         myaddr[3] = eaddr[EEPPEther1] & 0xFF;
    335         myaddr[2] = eaddr[EEPPEther1] >> 8;
    336         myaddr[5] = eaddr[EEPPEther2] & 0xFF;
    337         myaddr[4] = eaddr[EEPPEther2] >> 8;
    338 
    339 	ifmedia_init(&sc->iy_ifmedia, 0, iy_mediachange, iy_mediastatus);
    340 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_2, 0, NULL);
    341 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_5, 0, NULL);
    342 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
    343 	ifmedia_add(&sc->iy_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
    344 	ifmedia_set(&sc->iy_ifmedia, IFM_ETHER | IFM_AUTO);
    345 	/* Attach the interface. */
    346 	if_attach(ifp);
    347 	ether_ifattach(ifp, myaddr);
    348 	printf(": address %s, rev. %d, %d kB\n",
    349 	    ether_sprintf(myaddr),
    350 	    sc->hard_vers, sc->sram/1024);
    351 
    352 	eirq = eepro_irqmap[eaddr[EEPPW1] & EEPP_Int];
    353 	if (eirq != ia->ia_irq)
    354 		printf("%s: EEPROM irq setting %d ignored\n",
    355 		    sc->sc_dev.dv_xname, eirq);
    356 
    357 #if NBPFILTER > 0
    358 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    359 #endif
    360 
    361 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    362 	    IPL_NET, iyintr, sc);
    363 
    364 #if NRND > 0
    365 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
    366 #endif
    367 
    368 	temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    369 	bus_space_write_1(iot, ioh, INT_NO_REG, (temp & 0xf8) | sc->mappedirq);
    370 }
    371 
    372 void
    373 iystop(sc)
    374 struct iy_softc *sc;
    375 {
    376 	bus_space_tag_t iot;
    377 	bus_space_handle_t ioh;
    378 #ifdef IYDEBUG
    379 	u_int p, v;
    380 #endif
    381 
    382 	iot = sc->sc_iot;
    383 	ioh = sc->sc_ioh;
    384 
    385 	bus_space_write_1(iot, ioh, COMMAND_REG, RCV_DISABLE_CMD);
    386 
    387 	bus_space_write_1(iot, ioh, INT_MASK_REG, ALL_INTS);
    388 	bus_space_write_1(iot, ioh, STATUS_REG, ALL_INTS);
    389 
    390 	bus_space_write_1(iot, ioh, COMMAND_REG, RESET_CMD);
    391 	delay(200);
    392 #ifdef IYDEBUG
    393 	printf("%s: dumping tx chain (st 0x%x end 0x%x last 0x%x)\n",
    394 		    sc->sc_dev.dv_xname, sc->tx_start, sc->tx_end, sc->tx_last);
    395 	p = sc->tx_last;
    396 	if (!p)
    397 		p = sc->tx_start;
    398 	do {
    399 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, p);
    400 		v = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    401 		printf("0x%04x: %b ", p, v, "\020\006Ab\010Dn");
    402 		v = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    403 		printf("0x%b", v, "\020\6MAX_COL\7HRT_BEAT\010TX_DEF\011UND_RUN\012JERR\013LST_CRS\014LTCOL\016TX_OK\020COLL");
    404 		p = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    405 		printf(" 0x%04x", p);
    406 		v = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    407 		printf(" 0x%b\n", v, "\020\020Ch");
    408 
    409 	} while (v & 0x8000);
    410 #endif
    411 	sc->tx_start = sc->tx_end = sc->rx_size;
    412 	sc->tx_last = 0;
    413 	sc->sc_ethercom.ec_if.if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
    414 }
    415 
    416 void
    417 iyreset(sc)
    418 struct iy_softc *sc;
    419 {
    420 	int s;
    421 	s = splimp();
    422 	iystop(sc);
    423 	iyinit(sc);
    424 	splx(s);
    425 }
    426 
    427 void
    428 iyinit(sc)
    429 struct iy_softc *sc;
    430 {
    431 	int i;
    432 	unsigned temp;
    433 	struct ifnet *ifp;
    434 	bus_space_tag_t iot;
    435 	bus_space_handle_t ioh;
    436 
    437 	iot = sc->sc_iot;
    438 	ioh = sc->sc_ioh;
    439 
    440 	ifp = &sc->sc_ethercom.ec_if;
    441 #ifdef IYDEBUG
    442 	printf("ifp is %p\n", ifp);
    443 #endif
    444 
    445 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    446 
    447 	temp = bus_space_read_1(iot, ioh, EEPROM_REG);
    448 	if (temp & 0x10)
    449 		bus_space_write_1(iot, ioh, EEPROM_REG, temp & ~0x10);
    450 
    451 	for (i=0; i<6; ++i) {
    452 		bus_space_write_1(iot, ioh, I_ADD(i), LLADDR(ifp->if_sadl)[i]);
    453 	}
    454 
    455 	temp = bus_space_read_1(iot, ioh, REG1);
    456 	bus_space_write_1(iot, ioh, REG1,
    457 	    temp | XMT_CHAIN_INT | XMT_CHAIN_ERRSTOP | RCV_DISCARD_BAD);
    458 
    459 	temp = bus_space_read_1(iot, ioh, RECV_MODES_REG);
    460 	bus_space_write_1(iot, ioh, RECV_MODES_REG, temp | MATCH_BRDCST);
    461 #ifdef IYDEBUG
    462 	printf("%s: RECV_MODES were %b set to %b\n",
    463 	    sc->sc_dev.dv_xname,
    464 	    temp, "\020\1PRMSC\2NOBRDST\3SEECRC\4LENGTH\5NOSaIns\6MultiIA",
    465 	    temp|MATCH_BRDCST,
    466 	    "\020\1PRMSC\2NOBRDST\3SEECRC\4LENGTH\5NOSaIns\6MultiIA");
    467 #endif
    468 
    469 
    470 	delay(500000); /* for the hardware to test for the connector */
    471 
    472 	temp = bus_space_read_1(iot, ioh, MEDIA_SELECT);
    473 #ifdef IYDEBUG
    474 	printf("%s: media select was 0x%b ", sc->sc_dev.dv_xname,
    475 	    temp, "\020\1LnkInDis\2PolCor\3TPE\4JabberDis\5NoAport\6BNC");
    476 #endif
    477 	temp = (temp & TEST_MODE_MASK);
    478 
    479 	switch(IFM_SUBTYPE(sc->iy_ifmedia.ifm_media)) {
    480 	case IFM_10_5:
    481 		temp &= ~ (BNC_BIT | TPE_BIT);
    482 		break;
    483 
    484 	case IFM_10_2:
    485 		temp = (temp & ~TPE_BIT) | BNC_BIT;
    486 		break;
    487 
    488 	case IFM_10_T:
    489 		temp = (temp & ~BNC_BIT) | TPE_BIT;
    490 		break;
    491 	default:
    492 		/* nothing; leave as it is */
    493 	}
    494 	switch (temp & (BNC_BIT | TPE_BIT)) {
    495 	case BNC_BIT:
    496 		sc->iy_media = IFM_ETHER | IFM_10_2;
    497 		break;
    498 	case TPE_BIT:
    499 		sc->iy_media = IFM_ETHER | IFM_10_T;
    500 		break;
    501 	default:
    502 		sc->iy_media = IFM_ETHER | IFM_10_5;
    503 	}
    504 
    505 	bus_space_write_1(iot, ioh, MEDIA_SELECT, temp);
    506 #ifdef IYDEBUG
    507 	printf("changed to 0x%b\n",
    508 	    temp, "\020\1LnkInDis\2PolCor\3TPE\4JabberDis\5NoAport\6BNC");
    509 #endif
    510 
    511 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    512 	bus_space_write_1(iot, ioh, INT_MASK_REG, ALL_INTS);
    513 	bus_space_write_1(iot, ioh, 0, BANK_SEL(1));
    514 
    515 	temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    516 	bus_space_write_1(iot, ioh, INT_NO_REG, (temp & 0xf8) | sc->mappedirq);
    517 
    518 #ifdef IYDEBUG
    519 	printf("%s: int no was %b\n", sc->sc_dev.dv_xname,
    520 	    temp, "\020\4bad_irq\010flash/boot present");
    521 	temp = bus_space_read_1(iot, ioh, INT_NO_REG);
    522 	printf("%s: int no now 0x%02x\n", sc->sc_dev.dv_xname,
    523 	    temp, "\020\4BAD IRQ\010flash/boot present");
    524 #endif
    525 
    526 
    527 	bus_space_write_1(iot, ioh, RCV_LOWER_LIMIT_REG, 0);
    528 	bus_space_write_1(iot, ioh, RCV_UPPER_LIMIT_REG, (sc->rx_size - 2) >> 8);
    529 	bus_space_write_1(iot, ioh, XMT_LOWER_LIMIT_REG, sc->rx_size >> 8);
    530 	bus_space_write_1(iot, ioh, XMT_UPPER_LIMIT_REG, sc->sram >> 8);
    531 
    532 	temp = bus_space_read_1(iot, ioh, REG1);
    533 #ifdef IYDEBUG
    534 	printf("%s: HW access is %b\n", sc->sc_dev.dv_xname,
    535 	    temp, "\020\2WORD_WIDTH\010INT_ENABLE");
    536 #endif
    537 	bus_space_write_1(iot, ioh, REG1, temp | INT_ENABLE); /* XXX what about WORD_WIDTH? */
    538 
    539 #ifdef IYDEBUG
    540 	temp = bus_space_read_1(iot, ioh, REG1);
    541 	printf("%s: HW access is %b\n", sc->sc_dev.dv_xname,
    542 	    temp, "\020\2WORD_WIDTH\010INT_ENABLE");
    543 #endif
    544 
    545 	bus_space_write_1(iot, ioh, 0, BANK_SEL(0));
    546 
    547 	bus_space_write_1(iot, ioh, INT_MASK_REG, ALL_INTS & ~(RX_BIT|TX_BIT));
    548 	bus_space_write_1(iot, ioh, STATUS_REG, ALL_INTS); /* clear ints */
    549 
    550 	bus_space_write_2(iot, ioh, RCV_START_LOW, 0);
    551 	bus_space_write_2(iot, ioh, RCV_STOP_LOW,  sc->rx_size - 2);
    552 	sc->rx_start = 0;
    553 
    554 	bus_space_write_1(iot, ioh, 0, SEL_RESET_CMD);
    555 	delay(200);
    556 
    557 	bus_space_write_2(iot, ioh, XMT_ADDR_REG, sc->rx_size);
    558 
    559 	sc->tx_start = sc->tx_end = sc->rx_size;
    560 	sc->tx_last = 0;
    561 
    562 	bus_space_write_1(iot, ioh, 0, RCV_ENABLE_CMD);
    563 
    564 	ifp->if_flags |= IFF_RUNNING;
    565 	ifp->if_flags &= ~IFF_OACTIVE;
    566 }
    567 
    568 void
    569 iystart(ifp)
    570 struct ifnet *ifp;
    571 {
    572 	struct iy_softc *sc;
    573 
    574 
    575 	struct mbuf *m0, *m;
    576 	u_int len, pad, last, end;
    577 	u_int llen, residual;
    578 	int avail;
    579 	caddr_t data;
    580 	u_int16_t resval, stat;
    581 	bus_space_tag_t iot;
    582 	bus_space_handle_t ioh;
    583 
    584 #ifdef IYDEBUG
    585 	printf("iystart called\n");
    586 #endif
    587 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    588                 return;
    589 
    590 	sc = ifp->if_softc;
    591 	iot = sc->sc_iot;
    592 	ioh = sc->sc_ioh;
    593 
    594 	while ((m0 = ifp->if_snd.ifq_head) != NULL) {
    595 #ifdef IYDEBUG
    596 		printf("%s: trying to write another packet to the hardware\n",
    597 		    sc->sc_dev.dv_xname);
    598 #endif
    599 
    600 		/* We need to use m->m_pkthdr.len, so require the header */
    601 		if ((m0->m_flags & M_PKTHDR) == 0)
    602 			panic("iystart: no header mbuf");
    603 
    604 		len = m0->m_pkthdr.len;
    605 		pad = len & 1;
    606 
    607 #ifdef IYDEBUG
    608 		printf("%s: length is %d.\n", sc->sc_dev.dv_xname, len);
    609 #endif
    610 		if (len < ETHER_MIN_LEN) {
    611 			pad = ETHER_MIN_LEN - len;
    612 		}
    613 
    614         	if (len + pad > ETHER_MAX_LEN) {
    615         	        /* packet is obviously too large: toss it */
    616         	        ++ifp->if_oerrors;
    617         	        IF_DEQUEUE(&ifp->if_snd, m0);
    618         	        m_freem(m0);
    619 			continue;
    620         	}
    621 
    622 #if NBPFILTER > 0
    623 		if (ifp->if_bpf)
    624 			bpf_mtap(ifp->if_bpf, m0);
    625 #endif
    626 
    627 		avail = sc->tx_start - sc->tx_end;
    628 		if (avail <= 0)
    629 			avail += sc->tx_size;
    630 
    631 #ifdef IYDEBUG
    632 		printf("%s: avail is %d.\n", sc->sc_dev.dv_xname, avail);
    633 #endif
    634 		/*
    635 		 * we MUST RUN at splnet here  ---
    636 		 * XXX todo: or even turn off the boards ints ??? hm...
    637 		 */
    638 
    639        		/* See if there is room to put another packet in the buffer. */
    640 
    641 		if ((len+pad+2*I595_XMT_HDRLEN) > avail) {
    642 			printf("%s: len = %d, avail = %d, setting OACTIVE\n",
    643 			    sc->sc_dev.dv_xname, len, avail);
    644 			ifp->if_flags |= IFF_OACTIVE;
    645 			return;
    646 		}
    647 
    648 		/* we know it fits in the hardware now, so dequeue it */
    649 		IF_DEQUEUE(&ifp->if_snd, m0);
    650 
    651 		last = sc->tx_end;
    652 		end = last + pad + len + I595_XMT_HDRLEN;
    653 
    654 		if (end >= sc->sram) {
    655 			if ((sc->sram - last) <= I595_XMT_HDRLEN) {
    656 				/* keep header in one piece */
    657 				last = sc->rx_size;
    658 				end = last + pad + len + I595_XMT_HDRLEN;
    659 			} else
    660 				end -= sc->tx_size;
    661 		}
    662 
    663 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, last);
    664 		bus_space_write_2(iot, ioh, MEM_PORT_REG, XMT_CMD);
    665 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    666 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    667 		bus_space_write_2(iot, ioh, MEM_PORT_REG, len + pad);
    668 
    669 		residual = resval = 0;
    670 
    671 		while ((m = m0)!=0) {
    672 			data = mtod(m, caddr_t);
    673 			llen = m->m_len;
    674 			if (residual) {
    675 #ifdef IYDEBUG
    676 				printf("%s: merging residual with next mbuf.\n",
    677 				    sc->sc_dev.dv_xname);
    678 #endif
    679 				resval |= *data << 8;
    680 				bus_space_write_2(iot, ioh, MEM_PORT_REG, resval);
    681 				--llen;
    682 				++data;
    683 			}
    684 			if (llen > 1)
    685 				bus_space_write_multi_2(iot, ioh, MEM_PORT_REG,
    686 				    data, llen>>1);
    687 			residual = llen & 1;
    688 			if (residual) {
    689 				resval = *(data + llen - 1);
    690 #ifdef IYDEBUG
    691 				printf("%s: got odd mbuf to send.\n",
    692 				    sc->sc_dev.dv_xname);
    693 #endif
    694 			}
    695 
    696 			MFREE(m, m0);
    697 		}
    698 
    699 		if (residual)
    700 			bus_space_write_2(iot, ioh, MEM_PORT_REG, resval);
    701 
    702 		pad >>= 1;
    703 		while (pad-- > 0)
    704 			bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    705 
    706 #ifdef IYDEBUG
    707 		printf("%s: new last = 0x%x, end = 0x%x.\n",
    708 		    sc->sc_dev.dv_xname, last, end);
    709 		printf("%s: old start = 0x%x, end = 0x%x, last = 0x%x\n",
    710 		    sc->sc_dev.dv_xname, sc->tx_start, sc->tx_end, sc->tx_last);
    711 #endif
    712 
    713 		if (sc->tx_start != sc->tx_end) {
    714 			bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_last + XMT_COUNT);
    715 			stat = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    716 
    717 			bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_last + XMT_CHAIN);
    718 			bus_space_write_2(iot, ioh, MEM_PORT_REG, last);
    719 			bus_space_write_2(iot, ioh, MEM_PORT_REG, stat | CHAIN);
    720 #ifdef IYDEBUG
    721 			printf("%s: setting 0x%x to 0x%x\n",
    722 			    sc->sc_dev.dv_xname, sc->tx_last + XMT_COUNT,
    723 			    stat | CHAIN);
    724 #endif
    725 		}
    726 		stat = bus_space_read_2(iot, ioh, MEM_PORT_REG); /* dummy read */
    727 
    728 		/* XXX todo: enable ints here if disabled */
    729 
    730 		++ifp->if_opackets;
    731 
    732 		if (sc->tx_start == sc->tx_end) {
    733 			bus_space_write_2(iot, ioh, XMT_ADDR_REG, last);
    734 			bus_space_write_1(iot, ioh, 0, XMT_CMD);
    735 			sc->tx_start = last;
    736 #ifdef IYDEBUG
    737 			printf("%s: writing 0x%x to XAR and giving XCMD\n",
    738 			    sc->sc_dev.dv_xname, last);
    739 #endif
    740 		} else {
    741 			bus_space_write_1(iot, ioh, 0, RESUME_XMT_CMD);
    742 #ifdef IYDEBUG
    743 			printf("%s: giving RESUME_XCMD\n",
    744 			    sc->sc_dev.dv_xname);
    745 #endif
    746 		}
    747 		sc->tx_last = last;
    748 		sc->tx_end = end;
    749 	}
    750 }
    751 
    752 
    753 static __inline void
    754 eepromwritebit(iot, ioh, what)
    755 	bus_space_tag_t iot;
    756 	bus_space_handle_t ioh;
    757 	int what;
    758 {
    759 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    760 	delay(1);
    761 	bus_space_write_1(iot, ioh, EEPROM_REG, what|EESK);
    762 	delay(1);
    763 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    764 	delay(1);
    765 }
    766 
    767 static __inline int
    768 eepromreadbit(iot, ioh)
    769 	bus_space_tag_t iot;
    770 	bus_space_handle_t ioh;
    771 {
    772 	int b;
    773 
    774 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS|EESK);
    775 	delay(1);
    776 	b = bus_space_read_1(iot, ioh, EEPROM_REG);
    777 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS);
    778 	delay(1);
    779 
    780 	return ((b & EEDO) != 0);
    781 }
    782 
    783 static u_int16_t
    784 eepromread(iot, ioh, offset)
    785 	bus_space_tag_t iot;
    786 	bus_space_handle_t ioh;
    787 	int offset;
    788 {
    789 	volatile int i;
    790 	volatile int j;
    791 	volatile u_int16_t readval;
    792 
    793 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    794 	delay(1);
    795 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS); /* XXXX??? */
    796 	delay(1);
    797 
    798 	eepromwritebit(iot, ioh, EECS|EEDI);
    799 	eepromwritebit(iot, ioh, EECS|EEDI);
    800 	eepromwritebit(iot, ioh, EECS);
    801 
    802 	for (j=5; j>=0; --j) {
    803 		if ((offset>>j) & 1)
    804 			eepromwritebit(iot, ioh, EECS|EEDI);
    805 		else
    806 			eepromwritebit(iot, ioh, EECS);
    807 	}
    808 
    809 	for (readval=0, i=0; i<16; ++i) {
    810 		readval<<=1;
    811 		readval |= eepromreadbit(iot, ioh);
    812 	}
    813 
    814 	bus_space_write_1(iot, ioh, EEPROM_REG, 0|EESK);
    815 	delay(1);
    816 	bus_space_write_1(iot, ioh, EEPROM_REG, 0);
    817 
    818 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
    819 
    820 	return readval;
    821 }
    822 
    823 /*
    824  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    825  * an interrupt after a transmit has been started on it.
    826  */
    827 void
    828 iywatchdog(ifp)
    829 	struct ifnet *ifp;
    830 {
    831 	struct iy_softc *sc = ifp->if_softc;
    832 
    833 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    834 	++sc->sc_ethercom.ec_if.if_oerrors;
    835 	iyreset(sc);
    836 }
    837 
    838 /*
    839  * What to do upon receipt of an interrupt.
    840  */
    841 int
    842 iyintr(arg)
    843 	void *arg;
    844 {
    845 	struct iy_softc *sc = arg;
    846 	bus_space_tag_t iot;
    847 	bus_space_handle_t ioh;
    848 
    849 	register u_short status;
    850 
    851 	iot = sc->sc_iot;
    852 	ioh = sc->sc_ioh;
    853 
    854 	status = bus_space_read_1(iot, ioh, STATUS_REG);
    855 #ifdef IYDEBUG
    856 	if (status & ALL_INTS) {
    857 		printf("%s: got interupt %b", sc->sc_dev.dv_xname, status,
    858 		    "\020\1RX_STP\2RX\3TX\4EXEC");
    859 		if (status & EXEC_INT)
    860 			printf(" event %b\n", bus_space_read_1(iot, ioh, 0),
    861 			    "\020\6ABORT");
    862 		else
    863 			printf("\n");
    864 	}
    865 #endif
    866 	if (((status & (RX_INT | TX_INT)) == 0))
    867 		return 0;
    868 
    869 	if (status & RX_INT) {
    870 		iy_intr_rx(sc);
    871 		bus_space_write_1(iot, ioh, STATUS_REG, RX_INT);
    872 	} else if (status & TX_INT) {
    873 		iy_intr_tx(sc);
    874 		bus_space_write_1(iot, ioh, STATUS_REG, TX_INT);
    875 	}
    876 
    877 #if NRND > 0
    878 	rnd_add_uint32(&sc->rnd_source, status);
    879 #endif
    880 
    881 	return 1;
    882 }
    883 
    884 void
    885 iyget(sc, iot, ioh, rxlen)
    886 	struct iy_softc *sc;
    887 	bus_space_tag_t iot;
    888 	bus_space_handle_t ioh;
    889 	int rxlen;
    890 {
    891 	struct mbuf *m, *top, **mp;
    892 	struct ether_header *eh;
    893 	struct ifnet *ifp;
    894 	int len;
    895 
    896 	ifp = &sc->sc_ethercom.ec_if;
    897 
    898 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    899 	if (m == 0)
    900 		goto dropped;
    901 	m->m_pkthdr.rcvif = ifp;
    902 	m->m_pkthdr.len = rxlen;
    903 	len = MHLEN;
    904 	top = 0;
    905 	mp = &top;
    906 
    907 	while (rxlen > 0) {
    908 		if (top) {
    909 			MGET(m, M_DONTWAIT, MT_DATA);
    910 			if (m == 0) {
    911 				m_freem(top);
    912 				goto dropped;
    913 			}
    914 			len = MLEN;
    915 		}
    916 		if (rxlen >= MINCLSIZE) {
    917 			MCLGET(m, M_DONTWAIT);
    918 			if ((m->m_flags & M_EXT) == 0) {
    919 				m_free(m);
    920 				m_freem(top);
    921 				goto dropped;
    922 			}
    923 			len = MCLBYTES;
    924 		}
    925 		len = min(rxlen, len);
    926 		if (len > 1) {
    927 			len &= ~1;
    928 
    929 			bus_space_read_multi_2(iot, ioh, MEM_PORT_REG,
    930 			    mtod(m, caddr_t), len/2);
    931 		} else {
    932 #ifdef IYDEBUG
    933 			printf("%s: received odd mbuf\n", sc->sc_dev.dv_xname);
    934 #endif
    935 			*(mtod(m, caddr_t)) = bus_space_read_2(iot, ioh,
    936 			    MEM_PORT_REG);
    937 		}
    938 		m->m_len = len;
    939 		rxlen -= len;
    940 		*mp = m;
    941 		mp = &m->m_next;
    942 	}
    943 	/* XXX receive the top here */
    944 	++ifp->if_ipackets;
    945 
    946 	eh = mtod(top, struct ether_header *);
    947 
    948 #if NBPFILTER > 0
    949 	if (ifp->if_bpf) {
    950 		bpf_mtap(ifp->if_bpf, top);
    951 		if ((ifp->if_flags & IFF_PROMISC) &&
    952 		    (eh->ether_dhost[0] & 1) == 0 &&
    953 		    bcmp(eh->ether_dhost,
    954 		    	LLADDR(sc->sc_ethercom.ec_if.if_sadl),
    955 			sizeof(eh->ether_dhost)) != 0) {
    956 
    957 			m_freem(top);
    958 			return;
    959 		}
    960 	}
    961 #endif
    962 	m_adj(top, sizeof(struct ether_header));
    963 	ether_input(ifp, eh, top);
    964 	return;
    965 
    966 dropped:
    967 	++ifp->if_ierrors;
    968 	return;
    969 }
    970 void
    971 iy_intr_rx(sc)
    972 struct iy_softc *sc;
    973 {
    974 	struct ifnet *ifp;
    975 	bus_space_tag_t iot;
    976 	bus_space_handle_t ioh;
    977 
    978 	u_int rxadrs, rxevnt, rxstatus, rxnext, rxlen;
    979 
    980 	iot = sc->sc_iot;
    981 	ioh = sc->sc_ioh;
    982 	ifp = &sc->sc_ethercom.ec_if;
    983 
    984 	rxadrs = sc->rx_start;
    985 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxadrs);
    986 	rxevnt = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    987 	rxnext = 0;
    988 
    989 	while (rxevnt == RCV_DONE) {
    990 		rxstatus = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    991 		rxnext = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    992 		rxlen = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    993 #ifdef IYDEBUG
    994 		printf("%s: pck at 0x%04x stat %b next 0x%x len 0x%x\n",
    995 		    sc->sc_dev.dv_xname, rxadrs, rxstatus,
    996 		    "\020\1RCLD\2IA_MCH\010SHORT\011OVRN\013ALGERR"
    997 		    "\014CRCERR\015LENERR\016RCVOK\020TYP",
    998 		    rxnext, rxlen);
    999 #endif
   1000 		iyget(sc, iot, ioh, rxlen);
   1001 
   1002 		/* move stop address */
   1003 		bus_space_write_2(iot, ioh, RCV_STOP_LOW,
   1004 			    rxnext == 0 ? sc->rx_size - 2 : rxnext - 2);
   1005 
   1006 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxnext);
   1007 		rxadrs = rxnext;
   1008 		rxevnt = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1009 	}
   1010 	sc->rx_start = rxnext;
   1011 }
   1012 
   1013 void
   1014 iy_intr_tx(sc)
   1015 struct iy_softc *sc;
   1016 {
   1017 	bus_space_tag_t iot;
   1018 	bus_space_handle_t ioh;
   1019 	struct ifnet *ifp;
   1020 	u_int txstatus, txstat2, txlen, txnext;
   1021 
   1022 	ifp = &sc->sc_ethercom.ec_if;
   1023 	iot = sc->sc_iot;
   1024 	ioh = sc->sc_ioh;
   1025 
   1026 	while (sc->tx_start != sc->tx_end) {
   1027 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_start);
   1028 		txstatus = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1029 		if ((txstatus & (TX_DONE|CMD_MASK)) != (TX_DONE|XMT_CMD))
   1030 			break;
   1031 
   1032 		txstat2 = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1033 		txnext = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1034 		txlen = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1035 #ifdef IYDEBUG
   1036 		printf("txstat 0x%x stat2 0x%b next 0x%x len 0x%x\n",
   1037 		    txstatus, txstat2, "\020\6MAX_COL\7HRT_BEAT\010TX_DEF"
   1038 		    "\011UND_RUN\012JERR\013LST_CRS\014LTCOL\016TX_OK\020COLL",
   1039 			txnext, txlen);
   1040 #endif
   1041 		if (txlen & CHAIN)
   1042 			sc->tx_start = txnext;
   1043 		else
   1044 			sc->tx_start = sc->tx_end;
   1045 		ifp->if_flags &= ~IFF_OACTIVE;
   1046 
   1047 		if ((txstat2 & 0x2000) == 0)
   1048 			++ifp->if_oerrors;
   1049 		if (txstat2 & 0x000f)
   1050 			ifp->if_oerrors += txstat2 & 0x000f;
   1051 	}
   1052 	ifp->if_flags &= ~IFF_OACTIVE;
   1053 }
   1054 
   1055 #if 0
   1056 /*
   1057  * Compare two Ether/802 addresses for equality, inlined and unrolled for
   1058  * speed.  I'd love to have an inline assembler version of this...
   1059  */
   1060 static inline int
   1061 ether_equal(one, two)
   1062 	u_char *one, *two;
   1063 {
   1064 
   1065 	if (one[0] != two[0] || one[1] != two[1] || one[2] != two[2] ||
   1066 	    one[3] != two[3] || one[4] != two[4] || one[5] != two[5])
   1067 		return 0;
   1068 	return 1;
   1069 }
   1070 
   1071 /*
   1072  * Check for a valid address.  to_bpf is filled in with one of the following:
   1073  *   0 -> BPF doesn't get this packet
   1074  *   1 -> BPF does get this packet
   1075  *   2 -> BPF does get this packet, but we don't
   1076  * Return value is true if the packet is for us, and false otherwise.
   1077  *
   1078  * This routine is a mess, but it's also critical that it be as fast
   1079  * as possible.  It could be made cleaner if we can assume that the
   1080  * only client which will fiddle with IFF_PROMISC is BPF.  This is
   1081  * probably a good assumption, but we do not make it here.  (Yet.)
   1082  */
   1083 static inline int
   1084 check_eh(sc, eh, to_bpf)
   1085 	struct iy_softc *sc;
   1086 	struct ether_header *eh;
   1087 	int *to_bpf;
   1088 {
   1089 	int i;
   1090 
   1091 	switch (sc->promisc) {
   1092 	case IFF_ALLMULTI:
   1093 		/*
   1094 		 * Receiving all multicasts, but no unicasts except those
   1095 		 * destined for us.
   1096 		 */
   1097 #if NBPFILTER > 0
   1098 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0); /* BPF gets this packet if anybody cares */
   1099 #endif
   1100 		if (eh->ether_dhost[0] & 1)
   1101 			return 1;
   1102 		if (ether_equal(eh->ether_dhost,
   1103 		    LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1104 			return 1;
   1105 		return 0;
   1106 
   1107 	case IFF_PROMISC:
   1108 		/*
   1109 		 * Receiving all packets.  These need to be passed on to BPF.
   1110 		 */
   1111 #if NBPFILTER > 0
   1112 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1113 #endif
   1114 		/* If for us, accept and hand up to BPF */
   1115 		if (ether_equal(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1116 			return 1;
   1117 
   1118 #if NBPFILTER > 0
   1119 		if (*to_bpf)
   1120 			*to_bpf = 2; /* we don't need to see it */
   1121 #endif
   1122 
   1123 		/*
   1124 		 * Not a multicast, so BPF wants to see it but we don't.
   1125 		 */
   1126 		if (!(eh->ether_dhost[0] & 1))
   1127 			return 1;
   1128 
   1129 		/*
   1130 		 * If it's one of our multicast groups, accept it and pass it
   1131 		 * up.
   1132 		 */
   1133 		for (i = 0; i < sc->mcast_count; i++) {
   1134 			if (ether_equal(eh->ether_dhost, (u_char *)&sc->mcast_addrs[i])) {
   1135 #if NBPFILTER > 0
   1136 				if (*to_bpf)
   1137 					*to_bpf = 1;
   1138 #endif
   1139 				return 1;
   1140 			}
   1141 		}
   1142 		return 1;
   1143 
   1144 	case IFF_ALLMULTI | IFF_PROMISC:
   1145 		/*
   1146 		 * Acting as a multicast router, and BPF running at the same
   1147 		 * time.  Whew!  (Hope this is a fast machine...)
   1148 		 */
   1149 #if NBPFILTER > 0
   1150 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1151 #endif
   1152 		/* We want to see multicasts. */
   1153 		if (eh->ether_dhost[0] & 1)
   1154 			return 1;
   1155 
   1156 		/* We want to see our own packets */
   1157 		if (ether_equal(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1158 			return 1;
   1159 
   1160 		/* Anything else goes to BPF but nothing else. */
   1161 #if NBPFILTER > 0
   1162 		if (*to_bpf)
   1163 			*to_bpf = 2;
   1164 #endif
   1165 		return 1;
   1166 
   1167 	case 0:
   1168 		/*
   1169 		 * Only accept unicast packets destined for us, or multicasts
   1170 		 * for groups that we belong to.  For now, we assume that the
   1171 		 * '586 will only return packets that we asked it for.  This
   1172 		 * isn't strictly true (it uses hashing for the multicast
   1173 		 * filter), but it will do in this case, and we want to get out
   1174 		 * of here as quickly as possible.
   1175 		 */
   1176 #if NBPFILTER > 0
   1177 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1178 #endif
   1179 		return 1;
   1180 	}
   1181 
   1182 #ifdef DIAGNOSTIC
   1183 	panic("check_eh: impossible");
   1184 #endif
   1185 }
   1186 #endif
   1187 
   1188 int
   1189 iyioctl(ifp, cmd, data)
   1190 	register struct ifnet *ifp;
   1191 	u_long cmd;
   1192 	caddr_t data;
   1193 {
   1194 	struct iy_softc *sc;
   1195 	struct ifaddr *ifa;
   1196 	struct ifreq *ifr;
   1197 	int s, error = 0;
   1198 
   1199 	sc = ifp->if_softc;
   1200 	ifa = (struct ifaddr *)data;
   1201 	ifr = (struct ifreq *)data;
   1202 
   1203 #ifdef IYDEBUG
   1204 	printf("iyioctl called with ifp 0x%p (%s) cmd 0x%x data 0x%p\n",
   1205 	    ifp, ifp->if_xname, cmd, data);
   1206 #endif
   1207 
   1208 	s = splimp();
   1209 
   1210 	switch (cmd) {
   1211 
   1212 	case SIOCSIFADDR:
   1213 		ifp->if_flags |= IFF_UP;
   1214 
   1215 		switch (ifa->ifa_addr->sa_family) {
   1216 #ifdef INET
   1217 		case AF_INET:
   1218 			iyinit(sc);
   1219 			arp_ifinit(ifp, ifa);
   1220 			break;
   1221 #endif
   1222 #ifdef NS
   1223 		/* XXX - This code is probably wrong. */
   1224 		case AF_NS:
   1225 		    {
   1226 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1227 
   1228 			if (ns_nullhost(*ina))
   1229 				ina->x_host = *(union ns_host *)
   1230 				    LLADDR(sc->sc_ethercom.ec_if.if_sadl);
   1231 			else
   1232 				bcopy(ina->x_host.c_host,
   1233 				    LLADDR(sc->sc_ethercom.ec_if.if_sadl),
   1234 				    ETHER_ADDR_LEN);
   1235 			/* Set new address. */
   1236 			iyinit(sc);
   1237 			break;
   1238 		    }
   1239 #endif /* NS */
   1240 		default:
   1241 			iyinit(sc);
   1242 			break;
   1243 		}
   1244 		break;
   1245 
   1246 	case SIOCSIFFLAGS:
   1247 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1248 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1249 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1250 			/*
   1251 			 * If interface is marked down and it is running, then
   1252 			 * stop it.
   1253 			 */
   1254 			iystop(sc);
   1255 			ifp->if_flags &= ~IFF_RUNNING;
   1256 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1257 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1258 			/*
   1259 			 * If interface is marked up and it is stopped, then
   1260 			 * start it.
   1261 			 */
   1262 			iyinit(sc);
   1263 		} else {
   1264 			/*
   1265 			 * Reset the interface to pick up changes in any other
   1266 			 * flags that affect hardware registers.
   1267 			 */
   1268 			iystop(sc);
   1269 			iyinit(sc);
   1270 		}
   1271 #ifdef IYDEBUGX
   1272 		if (ifp->if_flags & IFF_DEBUG)
   1273 			sc->sc_debug = IFY_ALL;
   1274 		else
   1275 			sc->sc_debug = 0;
   1276 #endif
   1277 		break;
   1278 
   1279 #if 0 /* XXX */
   1280 	case SIOCADDMULTI:
   1281 	case SIOCDELMULTI:
   1282 		error = (cmd == SIOCADDMULTI) ?
   1283 		    ether_addmulti(ifr, &sc->sc_ethercom):
   1284 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1285 
   1286 		if (error == ENETRESET) {
   1287 			/*
   1288 			 * Multicast list has changed; set the hardware filter
   1289 			 * accordingly.
   1290 			 */
   1291 			iy_mc_reset(sc); /* XXX */
   1292 			error = 0;
   1293 		}
   1294 		break;
   1295 #endif
   1296 	case SIOCSIFMEDIA:
   1297 	case SIOCGIFMEDIA:
   1298 		error = ifmedia_ioctl(ifp, ifr, &sc->iy_ifmedia, cmd);
   1299 		break;
   1300 	default:
   1301 		error = EINVAL;
   1302 	}
   1303 	splx(s);
   1304 	return error;
   1305 }
   1306 
   1307 int
   1308 iy_mediachange(ifp)
   1309 	struct ifnet *ifp;
   1310 {
   1311 	struct iy_softc *sc = ifp->if_softc;
   1312 
   1313 	if (IFM_TYPE(sc->iy_ifmedia.ifm_media) != IFM_ETHER)
   1314 	    return EINVAL;
   1315 	switch(IFM_SUBTYPE(sc->iy_ifmedia.ifm_media)) {
   1316 	case IFM_10_5:
   1317 	case IFM_10_2:
   1318 	case IFM_10_T:
   1319 	case IFM_AUTO:
   1320 	    iystop(sc);
   1321 	    iyinit(sc);
   1322 	    return 0;
   1323 	default:
   1324 	    return EINVAL;
   1325 	}
   1326 }
   1327 
   1328 void
   1329 iy_mediastatus(ifp, ifmr)
   1330 	struct ifnet *ifp;
   1331 	struct ifmediareq *ifmr;
   1332 {
   1333 	struct iy_softc *sc = ifp->if_softc;
   1334 
   1335 	ifmr->ifm_active = sc->iy_media;
   1336 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
   1337 }
   1338 
   1339 #if 0
   1340 static void
   1341 iy_mc_reset(sc)
   1342 	struct iy_softc *sc;
   1343 {
   1344 	struct ether_multi *enm;
   1345 	struct ether_multistep step;
   1346 
   1347 	/*
   1348 	 * Step through the list of addresses.
   1349 	 */
   1350 	sc->mcast_count = 0;
   1351 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1352 	while (enm) {
   1353 		if (sc->mcast_count >= MAXMCAST ||
   1354 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1355 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1356 			iyioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS,
   1357 			    (void *)0);
   1358 			goto setflag;
   1359 		}
   1360 
   1361 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   1362 		sc->mcast_count++;
   1363 		ETHER_NEXT_MULTI(step, enm);
   1364 	}
   1365 	setflag:
   1366 	sc->want_mcsetup = 1;
   1367 }
   1368 
   1369 #ifdef IYDEBUG
   1370 void
   1371 print_rbd(rbd)
   1372 	volatile struct ie_recv_buf_desc *rbd;
   1373 {
   1374 
   1375 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
   1376 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
   1377 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
   1378 	    rbd->mbz);
   1379 }
   1380 #endif
   1381 #endif
   1382 
   1383 void
   1384 iyprobemem(sc)
   1385 	struct iy_softc *sc;
   1386 {
   1387 	bus_space_tag_t iot;
   1388 	bus_space_handle_t ioh;
   1389 	int testing;
   1390 
   1391 	iot = sc->sc_iot;
   1392 	ioh = sc->sc_ioh;
   1393 
   1394 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
   1395 	delay(1);
   1396 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, 4096-2);
   1397 	bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1398 
   1399 	for (testing=65536; testing >= 4096; testing >>= 1) {
   1400 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1401 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xdead);
   1402 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1403 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xdead) {
   1404 #ifdef IYMEMDEBUG
   1405 			printf("%s: Didn't keep 0xdead at 0x%x\n",
   1406 			    sc->sc_dev.dv_xname, testing-2);
   1407 #endif
   1408 			continue;
   1409 		}
   1410 
   1411 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1412 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xbeef);
   1413 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1414 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xbeef) {
   1415 #ifdef IYMEMDEBUG
   1416 			printf("%s: Didn't keep 0xbeef at 0x%x\n",
   1417 			    sc->sc_dev.dv_xname, testing-2);
   1418 #endif
   1419 			continue;
   1420 		}
   1421 
   1422 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1423 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1424 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing >> 1);
   1425 		bus_space_write_2(iot, ioh, MEM_PORT_REG, testing >> 1);
   1426 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1427 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) == (testing >> 1)) {
   1428 #ifdef IYMEMDEBUG
   1429 			printf("%s: 0x%x alias of 0x0\n",
   1430 			    sc->sc_dev.dv_xname, testing >> 1);
   1431 #endif
   1432 			continue;
   1433 		}
   1434 
   1435 		break;
   1436 	}
   1437 
   1438 	sc->sram = testing;
   1439 
   1440 	switch(testing) {
   1441 		case 65536:
   1442 			/* 4 NFS packets + overhead RX, 2 NFS + overhead TX  */
   1443 			sc->rx_size = 44*1024;
   1444 			break;
   1445 
   1446 		case 32768:
   1447 			/* 2 NFS packets + overhead RX, 1 NFS + overhead TX  */
   1448 			sc->rx_size = 22*1024;
   1449 			break;
   1450 
   1451 		case 16384:
   1452 			/* 1 NFS packet + overhead RX, 4 big packets TX */
   1453 			sc->rx_size = 10*1024;
   1454 			break;
   1455 		default:
   1456 			sc->rx_size = testing/2;
   1457 			break;
   1458 	}
   1459 	sc->tx_size = testing - sc->rx_size;
   1460 }
   1461 
   1462 static int
   1463 eepromreadall(iot, ioh, wordp, maxi)
   1464 	bus_space_tag_t iot;
   1465 	bus_space_handle_t ioh;
   1466 	u_int16_t *wordp;
   1467 	int maxi;
   1468 {
   1469 	int i;
   1470 	u_int16_t checksum, tmp;
   1471 
   1472 	checksum = 0;
   1473 
   1474 	for (i=0; i<EEPP_LENGTH; ++i) {
   1475 		tmp = eepromread(iot, ioh, i);
   1476 		checksum += tmp;
   1477 		if (i<maxi)
   1478 			wordp[i] = tmp;
   1479 	}
   1480 
   1481 	if (checksum != EEPP_CHKSUM) {
   1482 #ifdef IYDEBUG
   1483 		printf("wrong EEPROM checksum 0x%x should be 0x%x\n",
   1484 		    checksum, EEPP_CHKSUM);
   1485 #endif
   1486 		return 1;
   1487 	}
   1488 	return 0;
   1489 }
   1490