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