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