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