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