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