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