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