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