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