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