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