Home | History | Annotate | Line # | Download | only in isa
if_iy.c revision 1.20
      1 /*	$NetBSD: if_iy.c,v 1.20 1998/02/03 16:22:01 is 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 #ifdef IYDEBUG
    643 			printf("%s: len = %d, avail = %d, setting OACTIVE\n",
    644 			    sc->sc_dev.dv_xname, len, avail);
    645 #endif
    646 			ifp->if_flags |= IFF_OACTIVE;
    647 			return;
    648 		}
    649 
    650 		/* we know it fits in the hardware now, so dequeue it */
    651 		IF_DEQUEUE(&ifp->if_snd, m0);
    652 
    653 		last = sc->tx_end;
    654 		end = last + pad + len + I595_XMT_HDRLEN;
    655 
    656 		if (end >= sc->sram) {
    657 			if ((sc->sram - last) <= I595_XMT_HDRLEN) {
    658 				/* keep header in one piece */
    659 				last = sc->rx_size;
    660 				end = last + pad + len + I595_XMT_HDRLEN;
    661 			} else
    662 				end -= sc->tx_size;
    663 		}
    664 
    665 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, last);
    666 		bus_space_write_2(iot, ioh, MEM_PORT_REG, XMT_CMD);
    667 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    668 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    669 		bus_space_write_2(iot, ioh, MEM_PORT_REG, len + pad);
    670 
    671 		residual = resval = 0;
    672 
    673 		while ((m = m0)!=0) {
    674 			data = mtod(m, caddr_t);
    675 			llen = m->m_len;
    676 			if (residual) {
    677 #ifdef IYDEBUG
    678 				printf("%s: merging residual with next mbuf.\n",
    679 				    sc->sc_dev.dv_xname);
    680 #endif
    681 				resval |= *data << 8;
    682 				bus_space_write_2(iot, ioh, MEM_PORT_REG, resval);
    683 				--llen;
    684 				++data;
    685 			}
    686 			if (llen > 1)
    687 				bus_space_write_multi_2(iot, ioh, MEM_PORT_REG,
    688 				    data, llen>>1);
    689 			residual = llen & 1;
    690 			if (residual) {
    691 				resval = *(data + llen - 1);
    692 #ifdef IYDEBUG
    693 				printf("%s: got odd mbuf to send.\n",
    694 				    sc->sc_dev.dv_xname);
    695 #endif
    696 			}
    697 
    698 			MFREE(m, m0);
    699 		}
    700 
    701 		if (residual)
    702 			bus_space_write_2(iot, ioh, MEM_PORT_REG, resval);
    703 
    704 		pad >>= 1;
    705 		while (pad-- > 0)
    706 			bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
    707 
    708 #ifdef IYDEBUG
    709 		printf("%s: new last = 0x%x, end = 0x%x.\n",
    710 		    sc->sc_dev.dv_xname, last, end);
    711 		printf("%s: old start = 0x%x, end = 0x%x, last = 0x%x\n",
    712 		    sc->sc_dev.dv_xname, sc->tx_start, sc->tx_end, sc->tx_last);
    713 #endif
    714 
    715 		if (sc->tx_start != sc->tx_end) {
    716 			bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_last + XMT_COUNT);
    717 			stat = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    718 
    719 			bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_last + XMT_CHAIN);
    720 			bus_space_write_2(iot, ioh, MEM_PORT_REG, last);
    721 			bus_space_write_2(iot, ioh, MEM_PORT_REG, stat | CHAIN);
    722 #ifdef IYDEBUG
    723 			printf("%s: setting 0x%x to 0x%x\n",
    724 			    sc->sc_dev.dv_xname, sc->tx_last + XMT_COUNT,
    725 			    stat | CHAIN);
    726 #endif
    727 		}
    728 		stat = bus_space_read_2(iot, ioh, MEM_PORT_REG); /* dummy read */
    729 
    730 		/* XXX todo: enable ints here if disabled */
    731 
    732 		++ifp->if_opackets;
    733 
    734 		if (sc->tx_start == sc->tx_end) {
    735 			bus_space_write_2(iot, ioh, XMT_ADDR_REG, last);
    736 			bus_space_write_1(iot, ioh, 0, XMT_CMD);
    737 			sc->tx_start = last;
    738 #ifdef IYDEBUG
    739 			printf("%s: writing 0x%x to XAR and giving XCMD\n",
    740 			    sc->sc_dev.dv_xname, last);
    741 #endif
    742 		} else {
    743 			bus_space_write_1(iot, ioh, 0, RESUME_XMT_CMD);
    744 #ifdef IYDEBUG
    745 			printf("%s: giving RESUME_XCMD\n",
    746 			    sc->sc_dev.dv_xname);
    747 #endif
    748 		}
    749 		sc->tx_last = last;
    750 		sc->tx_end = end;
    751 	}
    752 }
    753 
    754 
    755 static __inline void
    756 eepromwritebit(iot, ioh, what)
    757 	bus_space_tag_t iot;
    758 	bus_space_handle_t ioh;
    759 	int what;
    760 {
    761 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    762 	delay(1);
    763 	bus_space_write_1(iot, ioh, EEPROM_REG, what|EESK);
    764 	delay(1);
    765 	bus_space_write_1(iot, ioh, EEPROM_REG, what);
    766 	delay(1);
    767 }
    768 
    769 static __inline int
    770 eepromreadbit(iot, ioh)
    771 	bus_space_tag_t iot;
    772 	bus_space_handle_t ioh;
    773 {
    774 	int b;
    775 
    776 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS|EESK);
    777 	delay(1);
    778 	b = bus_space_read_1(iot, ioh, EEPROM_REG);
    779 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS);
    780 	delay(1);
    781 
    782 	return ((b & EEDO) != 0);
    783 }
    784 
    785 static u_int16_t
    786 eepromread(iot, ioh, offset)
    787 	bus_space_tag_t iot;
    788 	bus_space_handle_t ioh;
    789 	int offset;
    790 {
    791 	volatile int i;
    792 	volatile int j;
    793 	volatile u_int16_t readval;
    794 
    795 	bus_space_write_1(iot, ioh, 0, BANK_SEL(2));
    796 	delay(1);
    797 	bus_space_write_1(iot, ioh, EEPROM_REG, EECS); /* XXXX??? */
    798 	delay(1);
    799 
    800 	eepromwritebit(iot, ioh, EECS|EEDI);
    801 	eepromwritebit(iot, ioh, EECS|EEDI);
    802 	eepromwritebit(iot, ioh, EECS);
    803 
    804 	for (j=5; j>=0; --j) {
    805 		if ((offset>>j) & 1)
    806 			eepromwritebit(iot, ioh, EECS|EEDI);
    807 		else
    808 			eepromwritebit(iot, ioh, EECS);
    809 	}
    810 
    811 	for (readval=0, i=0; i<16; ++i) {
    812 		readval<<=1;
    813 		readval |= eepromreadbit(iot, ioh);
    814 	}
    815 
    816 	bus_space_write_1(iot, ioh, EEPROM_REG, 0|EESK);
    817 	delay(1);
    818 	bus_space_write_1(iot, ioh, EEPROM_REG, 0);
    819 
    820 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
    821 
    822 	return readval;
    823 }
    824 
    825 /*
    826  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    827  * an interrupt after a transmit has been started on it.
    828  */
    829 void
    830 iywatchdog(ifp)
    831 	struct ifnet *ifp;
    832 {
    833 	struct iy_softc *sc = ifp->if_softc;
    834 
    835 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    836 	++sc->sc_ethercom.ec_if.if_oerrors;
    837 	iyreset(sc);
    838 }
    839 
    840 /*
    841  * What to do upon receipt of an interrupt.
    842  */
    843 int
    844 iyintr(arg)
    845 	void *arg;
    846 {
    847 	struct iy_softc *sc = arg;
    848 	bus_space_tag_t iot;
    849 	bus_space_handle_t ioh;
    850 
    851 	register u_short status;
    852 
    853 	iot = sc->sc_iot;
    854 	ioh = sc->sc_ioh;
    855 
    856 	status = bus_space_read_1(iot, ioh, STATUS_REG);
    857 #ifdef IYDEBUG
    858 	if (status & ALL_INTS) {
    859 		printf("%s: got interupt %b", sc->sc_dev.dv_xname, status,
    860 		    "\020\1RX_STP\2RX\3TX\4EXEC");
    861 		if (status & EXEC_INT)
    862 			printf(" event %b\n", bus_space_read_1(iot, ioh, 0),
    863 			    "\020\6ABORT");
    864 		else
    865 			printf("\n");
    866 	}
    867 #endif
    868 	if (((status & (RX_INT | TX_INT)) == 0))
    869 		return 0;
    870 
    871 	if (status & RX_INT) {
    872 		iy_intr_rx(sc);
    873 		bus_space_write_1(iot, ioh, STATUS_REG, RX_INT);
    874 	} else if (status & TX_INT) {
    875 		iy_intr_tx(sc);
    876 		bus_space_write_1(iot, ioh, STATUS_REG, TX_INT);
    877 	}
    878 
    879 #if NRND > 0
    880 	rnd_add_uint32(&sc->rnd_source, status);
    881 #endif
    882 
    883 	return 1;
    884 }
    885 
    886 void
    887 iyget(sc, iot, ioh, rxlen)
    888 	struct iy_softc *sc;
    889 	bus_space_tag_t iot;
    890 	bus_space_handle_t ioh;
    891 	int rxlen;
    892 {
    893 	struct mbuf *m, *top, **mp;
    894 	struct ether_header *eh;
    895 	struct ifnet *ifp;
    896 	int len;
    897 
    898 	ifp = &sc->sc_ethercom.ec_if;
    899 
    900 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    901 	if (m == 0)
    902 		goto dropped;
    903 	m->m_pkthdr.rcvif = ifp;
    904 	m->m_pkthdr.len = rxlen;
    905 	len = MHLEN;
    906 	top = 0;
    907 	mp = &top;
    908 
    909 	while (rxlen > 0) {
    910 		if (top) {
    911 			MGET(m, M_DONTWAIT, MT_DATA);
    912 			if (m == 0) {
    913 				m_freem(top);
    914 				goto dropped;
    915 			}
    916 			len = MLEN;
    917 		}
    918 		if (rxlen >= MINCLSIZE) {
    919 			MCLGET(m, M_DONTWAIT);
    920 			if ((m->m_flags & M_EXT) == 0) {
    921 				m_free(m);
    922 				m_freem(top);
    923 				goto dropped;
    924 			}
    925 			len = MCLBYTES;
    926 		}
    927 		len = min(rxlen, len);
    928 		if (len > 1) {
    929 			len &= ~1;
    930 
    931 			bus_space_read_multi_2(iot, ioh, MEM_PORT_REG,
    932 			    mtod(m, caddr_t), len/2);
    933 		} else {
    934 #ifdef IYDEBUG
    935 			printf("%s: received odd mbuf\n", sc->sc_dev.dv_xname);
    936 #endif
    937 			*(mtod(m, caddr_t)) = bus_space_read_2(iot, ioh,
    938 			    MEM_PORT_REG);
    939 		}
    940 		m->m_len = len;
    941 		rxlen -= len;
    942 		*mp = m;
    943 		mp = &m->m_next;
    944 	}
    945 	/* XXX receive the top here */
    946 	++ifp->if_ipackets;
    947 
    948 	eh = mtod(top, struct ether_header *);
    949 
    950 #if NBPFILTER > 0
    951 	if (ifp->if_bpf) {
    952 		bpf_mtap(ifp->if_bpf, top);
    953 		if ((ifp->if_flags & IFF_PROMISC) &&
    954 		    (eh->ether_dhost[0] & 1) == 0 &&
    955 		    bcmp(eh->ether_dhost,
    956 		    	LLADDR(sc->sc_ethercom.ec_if.if_sadl),
    957 			sizeof(eh->ether_dhost)) != 0) {
    958 
    959 			m_freem(top);
    960 			return;
    961 		}
    962 	}
    963 #endif
    964 	m_adj(top, sizeof(struct ether_header));
    965 	ether_input(ifp, eh, top);
    966 	return;
    967 
    968 dropped:
    969 	++ifp->if_ierrors;
    970 	return;
    971 }
    972 void
    973 iy_intr_rx(sc)
    974 struct iy_softc *sc;
    975 {
    976 	struct ifnet *ifp;
    977 	bus_space_tag_t iot;
    978 	bus_space_handle_t ioh;
    979 
    980 	u_int rxadrs, rxevnt, rxstatus, rxnext, rxlen;
    981 
    982 	iot = sc->sc_iot;
    983 	ioh = sc->sc_ioh;
    984 	ifp = &sc->sc_ethercom.ec_if;
    985 
    986 	rxadrs = sc->rx_start;
    987 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxadrs);
    988 	rxevnt = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    989 	rxnext = 0;
    990 
    991 	while (rxevnt == RCV_DONE) {
    992 		rxstatus = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    993 		rxnext = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    994 		rxlen = bus_space_read_2(iot, ioh, MEM_PORT_REG);
    995 #ifdef IYDEBUG
    996 		printf("%s: pck at 0x%04x stat %b next 0x%x len 0x%x\n",
    997 		    sc->sc_dev.dv_xname, rxadrs, rxstatus,
    998 		    "\020\1RCLD\2IA_MCH\010SHORT\011OVRN\013ALGERR"
    999 		    "\014CRCERR\015LENERR\016RCVOK\020TYP",
   1000 		    rxnext, rxlen);
   1001 #endif
   1002 		iyget(sc, iot, ioh, rxlen);
   1003 
   1004 		/* move stop address */
   1005 		bus_space_write_2(iot, ioh, RCV_STOP_LOW,
   1006 			    rxnext == 0 ? sc->rx_size - 2 : rxnext - 2);
   1007 
   1008 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, rxnext);
   1009 		rxadrs = rxnext;
   1010 		rxevnt = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1011 	}
   1012 	sc->rx_start = rxnext;
   1013 }
   1014 
   1015 void
   1016 iy_intr_tx(sc)
   1017 struct iy_softc *sc;
   1018 {
   1019 	bus_space_tag_t iot;
   1020 	bus_space_handle_t ioh;
   1021 	struct ifnet *ifp;
   1022 	u_int txstatus, txstat2, txlen, txnext;
   1023 
   1024 	ifp = &sc->sc_ethercom.ec_if;
   1025 	iot = sc->sc_iot;
   1026 	ioh = sc->sc_ioh;
   1027 
   1028 	while (sc->tx_start != sc->tx_end) {
   1029 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, sc->tx_start);
   1030 		txstatus = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1031 		if ((txstatus & (TX_DONE|CMD_MASK)) != (TX_DONE|XMT_CMD))
   1032 			break;
   1033 
   1034 		txstat2 = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1035 		txnext = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1036 		txlen = bus_space_read_2(iot, ioh, MEM_PORT_REG);
   1037 #ifdef IYDEBUG
   1038 		printf("txstat 0x%x stat2 0x%b next 0x%x len 0x%x\n",
   1039 		    txstatus, txstat2, "\020\6MAX_COL\7HRT_BEAT\010TX_DEF"
   1040 		    "\011UND_RUN\012JERR\013LST_CRS\014LTCOL\016TX_OK\020COLL",
   1041 			txnext, txlen);
   1042 #endif
   1043 		if (txlen & CHAIN)
   1044 			sc->tx_start = txnext;
   1045 		else
   1046 			sc->tx_start = sc->tx_end;
   1047 		ifp->if_flags &= ~IFF_OACTIVE;
   1048 
   1049 		if ((txstat2 & 0x2000) == 0)
   1050 			++ifp->if_oerrors;
   1051 		if (txstat2 & 0x000f)
   1052 			ifp->if_oerrors += txstat2 & 0x000f;
   1053 	}
   1054 	ifp->if_flags &= ~IFF_OACTIVE;
   1055 }
   1056 
   1057 #if 0
   1058 /*
   1059  * Compare two Ether/802 addresses for equality, inlined and unrolled for
   1060  * speed.  I'd love to have an inline assembler version of this...
   1061  */
   1062 static inline int
   1063 ether_equal(one, two)
   1064 	u_char *one, *two;
   1065 {
   1066 
   1067 	if (one[0] != two[0] || one[1] != two[1] || one[2] != two[2] ||
   1068 	    one[3] != two[3] || one[4] != two[4] || one[5] != two[5])
   1069 		return 0;
   1070 	return 1;
   1071 }
   1072 
   1073 /*
   1074  * Check for a valid address.  to_bpf is filled in with one of the following:
   1075  *   0 -> BPF doesn't get this packet
   1076  *   1 -> BPF does get this packet
   1077  *   2 -> BPF does get this packet, but we don't
   1078  * Return value is true if the packet is for us, and false otherwise.
   1079  *
   1080  * This routine is a mess, but it's also critical that it be as fast
   1081  * as possible.  It could be made cleaner if we can assume that the
   1082  * only client which will fiddle with IFF_PROMISC is BPF.  This is
   1083  * probably a good assumption, but we do not make it here.  (Yet.)
   1084  */
   1085 static inline int
   1086 check_eh(sc, eh, to_bpf)
   1087 	struct iy_softc *sc;
   1088 	struct ether_header *eh;
   1089 	int *to_bpf;
   1090 {
   1091 	int i;
   1092 
   1093 	switch (sc->promisc) {
   1094 	case IFF_ALLMULTI:
   1095 		/*
   1096 		 * Receiving all multicasts, but no unicasts except those
   1097 		 * destined for us.
   1098 		 */
   1099 #if NBPFILTER > 0
   1100 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0); /* BPF gets this packet if anybody cares */
   1101 #endif
   1102 		if (eh->ether_dhost[0] & 1)
   1103 			return 1;
   1104 		if (ether_equal(eh->ether_dhost,
   1105 		    LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1106 			return 1;
   1107 		return 0;
   1108 
   1109 	case IFF_PROMISC:
   1110 		/*
   1111 		 * Receiving all packets.  These need to be passed on to BPF.
   1112 		 */
   1113 #if NBPFILTER > 0
   1114 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1115 #endif
   1116 		/* If for us, accept and hand up to BPF */
   1117 		if (ether_equal(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1118 			return 1;
   1119 
   1120 #if NBPFILTER > 0
   1121 		if (*to_bpf)
   1122 			*to_bpf = 2; /* we don't need to see it */
   1123 #endif
   1124 
   1125 		/*
   1126 		 * Not a multicast, so BPF wants to see it but we don't.
   1127 		 */
   1128 		if (!(eh->ether_dhost[0] & 1))
   1129 			return 1;
   1130 
   1131 		/*
   1132 		 * If it's one of our multicast groups, accept it and pass it
   1133 		 * up.
   1134 		 */
   1135 		for (i = 0; i < sc->mcast_count; i++) {
   1136 			if (ether_equal(eh->ether_dhost, (u_char *)&sc->mcast_addrs[i])) {
   1137 #if NBPFILTER > 0
   1138 				if (*to_bpf)
   1139 					*to_bpf = 1;
   1140 #endif
   1141 				return 1;
   1142 			}
   1143 		}
   1144 		return 1;
   1145 
   1146 	case IFF_ALLMULTI | IFF_PROMISC:
   1147 		/*
   1148 		 * Acting as a multicast router, and BPF running at the same
   1149 		 * time.  Whew!  (Hope this is a fast machine...)
   1150 		 */
   1151 #if NBPFILTER > 0
   1152 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1153 #endif
   1154 		/* We want to see multicasts. */
   1155 		if (eh->ether_dhost[0] & 1)
   1156 			return 1;
   1157 
   1158 		/* We want to see our own packets */
   1159 		if (ether_equal(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl)))
   1160 			return 1;
   1161 
   1162 		/* Anything else goes to BPF but nothing else. */
   1163 #if NBPFILTER > 0
   1164 		if (*to_bpf)
   1165 			*to_bpf = 2;
   1166 #endif
   1167 		return 1;
   1168 
   1169 	case 0:
   1170 		/*
   1171 		 * Only accept unicast packets destined for us, or multicasts
   1172 		 * for groups that we belong to.  For now, we assume that the
   1173 		 * '586 will only return packets that we asked it for.  This
   1174 		 * isn't strictly true (it uses hashing for the multicast
   1175 		 * filter), but it will do in this case, and we want to get out
   1176 		 * of here as quickly as possible.
   1177 		 */
   1178 #if NBPFILTER > 0
   1179 		*to_bpf = (sc->sc_ethercom.ec_if.iy_bpf != 0);
   1180 #endif
   1181 		return 1;
   1182 	}
   1183 
   1184 #ifdef DIAGNOSTIC
   1185 	panic("check_eh: impossible");
   1186 #endif
   1187 }
   1188 #endif
   1189 
   1190 int
   1191 iyioctl(ifp, cmd, data)
   1192 	register struct ifnet *ifp;
   1193 	u_long cmd;
   1194 	caddr_t data;
   1195 {
   1196 	struct iy_softc *sc;
   1197 	struct ifaddr *ifa;
   1198 	struct ifreq *ifr;
   1199 	int s, error = 0;
   1200 
   1201 	sc = ifp->if_softc;
   1202 	ifa = (struct ifaddr *)data;
   1203 	ifr = (struct ifreq *)data;
   1204 
   1205 #ifdef IYDEBUG
   1206 	printf("iyioctl called with ifp 0x%p (%s) cmd 0x%x data 0x%p\n",
   1207 	    ifp, ifp->if_xname, cmd, data);
   1208 #endif
   1209 
   1210 	s = splimp();
   1211 
   1212 	switch (cmd) {
   1213 
   1214 	case SIOCSIFADDR:
   1215 		ifp->if_flags |= IFF_UP;
   1216 
   1217 		switch (ifa->ifa_addr->sa_family) {
   1218 #ifdef INET
   1219 		case AF_INET:
   1220 			iyinit(sc);
   1221 			arp_ifinit(ifp, ifa);
   1222 			break;
   1223 #endif
   1224 #ifdef NS
   1225 		/* XXX - This code is probably wrong. */
   1226 		case AF_NS:
   1227 		    {
   1228 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1229 
   1230 			if (ns_nullhost(*ina))
   1231 				ina->x_host = *(union ns_host *)
   1232 				    LLADDR(sc->sc_ethercom.ec_if.if_sadl);
   1233 			else
   1234 				bcopy(ina->x_host.c_host,
   1235 				    LLADDR(sc->sc_ethercom.ec_if.if_sadl),
   1236 				    ETHER_ADDR_LEN);
   1237 			/* Set new address. */
   1238 			iyinit(sc);
   1239 			break;
   1240 		    }
   1241 #endif /* NS */
   1242 		default:
   1243 			iyinit(sc);
   1244 			break;
   1245 		}
   1246 		break;
   1247 
   1248 	case SIOCSIFFLAGS:
   1249 		sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
   1250 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1251 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1252 			/*
   1253 			 * If interface is marked down and it is running, then
   1254 			 * stop it.
   1255 			 */
   1256 			iystop(sc);
   1257 			ifp->if_flags &= ~IFF_RUNNING;
   1258 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1259 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1260 			/*
   1261 			 * If interface is marked up and it is stopped, then
   1262 			 * start it.
   1263 			 */
   1264 			iyinit(sc);
   1265 		} else {
   1266 			/*
   1267 			 * Reset the interface to pick up changes in any other
   1268 			 * flags that affect hardware registers.
   1269 			 */
   1270 			iystop(sc);
   1271 			iyinit(sc);
   1272 		}
   1273 #ifdef IYDEBUGX
   1274 		if (ifp->if_flags & IFF_DEBUG)
   1275 			sc->sc_debug = IFY_ALL;
   1276 		else
   1277 			sc->sc_debug = 0;
   1278 #endif
   1279 		break;
   1280 
   1281 #if 0 /* XXX */
   1282 	case SIOCADDMULTI:
   1283 	case SIOCDELMULTI:
   1284 		error = (cmd == SIOCADDMULTI) ?
   1285 		    ether_addmulti(ifr, &sc->sc_ethercom):
   1286 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1287 
   1288 		if (error == ENETRESET) {
   1289 			/*
   1290 			 * Multicast list has changed; set the hardware filter
   1291 			 * accordingly.
   1292 			 */
   1293 			iy_mc_reset(sc); /* XXX */
   1294 			error = 0;
   1295 		}
   1296 		break;
   1297 #endif
   1298 	case SIOCSIFMEDIA:
   1299 	case SIOCGIFMEDIA:
   1300 		error = ifmedia_ioctl(ifp, ifr, &sc->iy_ifmedia, cmd);
   1301 		break;
   1302 	default:
   1303 		error = EINVAL;
   1304 	}
   1305 	splx(s);
   1306 	return error;
   1307 }
   1308 
   1309 int
   1310 iy_mediachange(ifp)
   1311 	struct ifnet *ifp;
   1312 {
   1313 	struct iy_softc *sc = ifp->if_softc;
   1314 
   1315 	if (IFM_TYPE(sc->iy_ifmedia.ifm_media) != IFM_ETHER)
   1316 	    return EINVAL;
   1317 	switch(IFM_SUBTYPE(sc->iy_ifmedia.ifm_media)) {
   1318 	case IFM_10_5:
   1319 	case IFM_10_2:
   1320 	case IFM_10_T:
   1321 	case IFM_AUTO:
   1322 	    iystop(sc);
   1323 	    iyinit(sc);
   1324 	    return 0;
   1325 	default:
   1326 	    return EINVAL;
   1327 	}
   1328 }
   1329 
   1330 void
   1331 iy_mediastatus(ifp, ifmr)
   1332 	struct ifnet *ifp;
   1333 	struct ifmediareq *ifmr;
   1334 {
   1335 	struct iy_softc *sc = ifp->if_softc;
   1336 
   1337 	ifmr->ifm_active = sc->iy_media;
   1338 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
   1339 }
   1340 
   1341 #if 0
   1342 static void
   1343 iy_mc_reset(sc)
   1344 	struct iy_softc *sc;
   1345 {
   1346 	struct ether_multi *enm;
   1347 	struct ether_multistep step;
   1348 
   1349 	/*
   1350 	 * Step through the list of addresses.
   1351 	 */
   1352 	sc->mcast_count = 0;
   1353 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1354 	while (enm) {
   1355 		if (sc->mcast_count >= MAXMCAST ||
   1356 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1357 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1358 			iyioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS,
   1359 			    (void *)0);
   1360 			goto setflag;
   1361 		}
   1362 
   1363 		bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
   1364 		sc->mcast_count++;
   1365 		ETHER_NEXT_MULTI(step, enm);
   1366 	}
   1367 	setflag:
   1368 	sc->want_mcsetup = 1;
   1369 }
   1370 
   1371 #ifdef IYDEBUG
   1372 void
   1373 print_rbd(rbd)
   1374 	volatile struct ie_recv_buf_desc *rbd;
   1375 {
   1376 
   1377 	printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
   1378 	    "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
   1379 	    rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
   1380 	    rbd->mbz);
   1381 }
   1382 #endif
   1383 #endif
   1384 
   1385 void
   1386 iyprobemem(sc)
   1387 	struct iy_softc *sc;
   1388 {
   1389 	bus_space_tag_t iot;
   1390 	bus_space_handle_t ioh;
   1391 	int testing;
   1392 
   1393 	iot = sc->sc_iot;
   1394 	ioh = sc->sc_ioh;
   1395 
   1396 	bus_space_write_1(iot, ioh, COMMAND_REG, BANK_SEL(0));
   1397 	delay(1);
   1398 	bus_space_write_2(iot, ioh, HOST_ADDR_REG, 4096-2);
   1399 	bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1400 
   1401 	for (testing=65536; testing >= 4096; testing >>= 1) {
   1402 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1403 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xdead);
   1404 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1405 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xdead) {
   1406 #ifdef IYMEMDEBUG
   1407 			printf("%s: Didn't keep 0xdead at 0x%x\n",
   1408 			    sc->sc_dev.dv_xname, testing-2);
   1409 #endif
   1410 			continue;
   1411 		}
   1412 
   1413 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1414 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0xbeef);
   1415 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing-2);
   1416 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) != 0xbeef) {
   1417 #ifdef IYMEMDEBUG
   1418 			printf("%s: Didn't keep 0xbeef at 0x%x\n",
   1419 			    sc->sc_dev.dv_xname, testing-2);
   1420 #endif
   1421 			continue;
   1422 		}
   1423 
   1424 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1425 		bus_space_write_2(iot, ioh, MEM_PORT_REG, 0);
   1426 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, testing >> 1);
   1427 		bus_space_write_2(iot, ioh, MEM_PORT_REG, testing >> 1);
   1428 		bus_space_write_2(iot, ioh, HOST_ADDR_REG, 0);
   1429 		if (bus_space_read_2(iot, ioh, MEM_PORT_REG) == (testing >> 1)) {
   1430 #ifdef IYMEMDEBUG
   1431 			printf("%s: 0x%x alias of 0x0\n",
   1432 			    sc->sc_dev.dv_xname, testing >> 1);
   1433 #endif
   1434 			continue;
   1435 		}
   1436 
   1437 		break;
   1438 	}
   1439 
   1440 	sc->sram = testing;
   1441 
   1442 	switch(testing) {
   1443 		case 65536:
   1444 			/* 4 NFS packets + overhead RX, 2 NFS + overhead TX  */
   1445 			sc->rx_size = 44*1024;
   1446 			break;
   1447 
   1448 		case 32768:
   1449 			/* 2 NFS packets + overhead RX, 1 NFS + overhead TX  */
   1450 			sc->rx_size = 22*1024;
   1451 			break;
   1452 
   1453 		case 16384:
   1454 			/* 1 NFS packet + overhead RX, 4 big packets TX */
   1455 			sc->rx_size = 10*1024;
   1456 			break;
   1457 		default:
   1458 			sc->rx_size = testing/2;
   1459 			break;
   1460 	}
   1461 	sc->tx_size = testing - sc->rx_size;
   1462 }
   1463 
   1464 static int
   1465 eepromreadall(iot, ioh, wordp, maxi)
   1466 	bus_space_tag_t iot;
   1467 	bus_space_handle_t ioh;
   1468 	u_int16_t *wordp;
   1469 	int maxi;
   1470 {
   1471 	int i;
   1472 	u_int16_t checksum, tmp;
   1473 
   1474 	checksum = 0;
   1475 
   1476 	for (i=0; i<EEPP_LENGTH; ++i) {
   1477 		tmp = eepromread(iot, ioh, i);
   1478 		checksum += tmp;
   1479 		if (i<maxi)
   1480 			wordp[i] = tmp;
   1481 	}
   1482 
   1483 	if (checksum != EEPP_CHKSUM) {
   1484 #ifdef IYDEBUG
   1485 		printf("wrong EEPROM checksum 0x%x should be 0x%x\n",
   1486 		    checksum, EEPP_CHKSUM);
   1487 #endif
   1488 		return 1;
   1489 	}
   1490 	return 0;
   1491 }
   1492