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