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