if_ie.c revision 1.32 1 /* $NetBSD: if_ie.c,v 1.32 1999/08/20 03:52:38 chs Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
5 * Copyright (c) 1992, 1993, University of Vermont and State
6 * Agricultural College.
7 * Copyright (c) 1992, 1993, Garrett A. Wollman.
8 *
9 * Portions:
10 * Copyright (c) 1994, 1995, Rafal K. Boni
11 * Copyright (c) 1990, 1991, William F. Jolitz
12 * Copyright (c) 1990, The Regents of the University of California
13 *
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by Charles M. Hannum, by the
27 * University of Vermont and State Agricultural College and Garrett A.
28 * Wollman, by William F. Jolitz, and by the University of California,
29 * Berkeley, Lawrence Berkeley Laboratory, and its contributors.
30 * 4. Neither the names of the Universities nor the names of the authors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR AUTHORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 */
46
47 /*
48 * Intel 82586 Ethernet chip
49 * Register, bit, and structure definitions.
50 *
51 * Original StarLAN driver written by Garrett Wollman with reference to the
52 * Clarkson Packet Driver code for this chip written by Russ Nelson and others.
53 *
54 * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
55 *
56 * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
57 *
58 * Majorly cleaned up and 3C507 code merged by Charles Hannum.
59 *
60 * Converted to SUN ie driver by Charles D. Cranor,
61 * October 1994, January 1995.
62 * This sun version based on i386 version 1.30.
63 * [ see sys/dev/isa/if_ie.c ]
64 */
65
66 /*
67 * The i82586 is a very painful chip, found in sun3's, sun-4/100's
68 * sun-4/200's, and VME based suns. The byte order is all wrong for a
69 * SUN, making life difficult. Programming this chip is mostly the same,
70 * but certain details differ from system to system. This driver is
71 * written so that different "ie" interfaces can be controled by the same
72 * driver.
73 */
74
75 /*
76 Mode of operation:
77
78 We run the 82586 in a standard Ethernet mode. We keep NFRAMES
79 received frame descriptors around for the receiver to use, and
80 NRXBUF associated receive buffer descriptors, both in a circular
81 list. Whenever a frame is received, we rotate both lists as
82 necessary. (The 586 treats both lists as a simple queue.) We also
83 keep a transmit command around so that packets can be sent off
84 quickly.
85
86 We configure the adapter in AL-LOC = 1 mode, which means that the
87 Ethernet/802.3 MAC header is placed at the beginning of the receive
88 buffer rather than being split off into various fields in the RFD.
89 This also means that we must include this header in the transmit
90 buffer as well.
91
92 By convention, all transmit commands, and only transmit commands,
93 shall have the I (IE_CMD_INTR) bit set in the command. This way,
94 when an interrupt arrives at ieintr(), it is immediately possible
95 to tell what precisely caused it. ANY OTHER command-sending
96 routines should run at splnet(), and should post an acknowledgement
97 to every interrupt they generate.
98 */
99
100 #include "opt_inet.h"
101 #include "opt_ns.h"
102 #include "bpfilter.h"
103
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/mbuf.h>
107 #include <sys/buf.h>
108 #include <sys/protosw.h>
109 #include <sys/socket.h>
110 #include <sys/ioctl.h>
111 #include <sys/errno.h>
112 #include <sys/syslog.h>
113 #include <sys/device.h>
114
115 #include <net/if.h>
116 #include <net/if_types.h>
117 #include <net/if_dl.h>
118 #include <net/if_ether.h>
119
120 #if NBPFILTER > 0
121 #include <net/bpf.h>
122 #include <net/bpfdesc.h>
123 #endif
124
125 #ifdef INET
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/in_var.h>
129 #include <netinet/ip.h>
130 #include <netinet/if_inarp.h>
131 #endif
132
133 #ifdef NS
134 #include <netns/ns.h>
135 #include <netns/ns_if.h>
136 #endif
137
138 #include <vm/vm.h>
139
140 #include <machine/autoconf.h>
141 #include <machine/cpu.h>
142 #include <machine/pmap.h>
143
144 /*
145 * ugly byte-order hack for SUNs
146 */
147
148 #define XSWAP(y) ( (((y)&0xff00) >> 8) | (((y)&0xff) << 8) )
149 #define SWAP(x) ((u_short)(XSWAP((u_short)(x))))
150
151 #include "i82586.h"
152 #include "if_iereg.h"
153 #include "if_ievar.h"
154
155 /* #define IEDEBUG XXX */
156
157 /*
158 * IED: ie debug flags
159 */
160
161 #define IED_RINT 0x01
162 #define IED_TINT 0x02
163 #define IED_RNR 0x04
164 #define IED_CNA 0x08
165 #define IED_READFRAME 0x10
166 #define IED_ENQ 0x20
167 #define IED_XMIT 0x40
168 #define IED_ALL 0x7f
169
170 #ifdef IEDEBUG
171 #define inline /* not */
172 void print_rbd __P((volatile struct ie_recv_buf_desc *));
173 int in_ierint = 0;
174 int in_ietint = 0;
175 int ie_debug_flags = 0;
176 #endif
177
178 /* XXX - Skip TDR for now - it always complains... */
179 int ie_run_tdr = 0;
180
181 static void iewatchdog __P((struct ifnet *));
182 static int ieinit __P((struct ie_softc *));
183 static int ieioctl __P((struct ifnet *, u_long, caddr_t));
184 static void iestart __P((struct ifnet *));
185 static void iereset __P((struct ie_softc *));
186 static int ie_setupram __P((struct ie_softc *sc));
187
188 static int cmd_and_wait __P((struct ie_softc *, int, void *, int));
189
190 static void ie_drop_packet_buffer __P((struct ie_softc *));
191 static void ie_readframe __P((struct ie_softc *, int));
192 static inline void ie_setup_config __P((struct ie_config_cmd *, int, int));
193
194 static void ierint __P((struct ie_softc *));
195 static void iestop __P((struct ie_softc *));
196 static void ietint __P((struct ie_softc *));
197 static void iexmit __P((struct ie_softc *));
198
199 static int mc_setup __P((struct ie_softc *, void *));
200 static void mc_reset __P((struct ie_softc *));
201 static void run_tdr __P((struct ie_softc *, struct ie_tdr_cmd *));
202 static void iememinit __P((struct ie_softc *));
203
204 static inline char * Align __P((char *));
205 static inline u_int Swap32 __P((u_int x));
206 static inline u_int vtop24 __P((struct ie_softc *, void *));
207 static inline u_short vtop16sw __P((struct ie_softc *, void *));
208
209 static inline void ie_ack __P((struct ie_softc *, u_int));
210 static inline u_short ether_cmp __P((u_char *, u_char *));
211 static inline int check_eh __P((struct ie_softc *,
212 struct ether_header *eh, int *));
213 static inline int ie_buflen __P((struct ie_softc *, int));
214 static inline int ie_packet_len __P((struct ie_softc *));
215 static inline struct mbuf * ieget __P((struct ie_softc *sc, int *to_bpf));
216
217
218 /*
219 * Here are a few useful functions. We could have done these as macros,
220 * but since we have the inline facility, it makes sense to use that
221 * instead.
222 */
223
224 /* KVA to 24 bit device address */
225 static inline u_int
226 vtop24(sc, ptr)
227 struct ie_softc *sc;
228 void *ptr;
229 {
230 u_int pa;
231
232 pa = ((caddr_t)ptr) - sc->sc_iobase;
233 #ifdef IEDEBUG
234 if (pa & ~0xffFFff)
235 panic("ie:vtop24");
236 #endif
237 return (pa);
238 }
239
240 /* KVA to 16 bit offset, swapped */
241 static inline u_short
242 vtop16sw(sc, ptr)
243 struct ie_softc *sc;
244 void *ptr;
245 {
246 u_int pa;
247
248 pa = ((caddr_t)ptr) - sc->sc_maddr;
249 #ifdef IEDEBUG
250 if (pa & ~0xFFff)
251 panic("ie:vtop16");
252 #endif
253
254 return (SWAP(pa));
255 }
256
257 static inline u_int
258 Swap32(x)
259 u_int x;
260 {
261 u_int y;
262
263 y = x & 0xFF;
264 y <<= 8; x >>= 8;
265 y |= x & 0xFF;
266 y <<= 8; x >>= 8;
267 y |= x & 0xFF;
268 y <<= 8; x >>= 8;
269 y |= x & 0xFF;
270
271 return (y);
272 }
273
274 static inline char *
275 Align(ptr)
276 caddr_t ptr;
277 {
278 u_long l = (u_long)ptr;
279
280 l = (l + 3) & ~3L;
281 return ((char *)l);
282 }
283
284
285 static inline void
286 ie_ack(sc, mask)
287 struct ie_softc *sc;
288 u_int mask;
289 {
290 volatile struct ie_sys_ctl_block *scb = sc->scb;
291
292 cmd_and_wait(sc, scb->ie_status & mask, 0, 0);
293 }
294
295
296 /*
297 * Taken almost exactly from Bill's if_is.c,
298 * then modified beyond recognition...
299 */
300 void
301 ie_attach(sc)
302 struct ie_softc *sc;
303 {
304 struct ifnet *ifp = &sc->sc_if;
305
306 /* MD code has done its part before calling this. */
307 printf(": macaddr %s\n", ether_sprintf(sc->sc_addr));
308
309 /*
310 * Compute number of transmit and receive buffers.
311 * Tx buffers take 1536 bytes, and fixed in number.
312 * Rx buffers are 512 bytes each, variable number.
313 * Need at least 1 frame for each 3 rx buffers.
314 * The ratio 3bufs:2frames is a compromise.
315 */
316 sc->ntxbuf = NTXBUF; /* XXX - Fix me... */
317 switch (sc->sc_msize) {
318 case 16384:
319 sc->nframes = 8 * 4;
320 sc->nrxbuf = 8 * 6;
321 break;
322 case 32768:
323 sc->nframes = 16 * 4;
324 sc->nrxbuf = 16 * 6;
325 break;
326 case 65536:
327 sc->nframes = 32 * 4;
328 sc->nrxbuf = 32 * 6;
329 break;
330 default:
331 sc->nframes = 0;
332 }
333 if (sc->nframes > MXFRAMES)
334 sc->nframes = MXFRAMES;
335 if (sc->nrxbuf > MXRXBUF)
336 sc->nrxbuf = MXRXBUF;
337
338 #ifdef IEDEBUG
339 printf("%s: %dK memory, %d tx frames, %d rx frames, %d rx bufs\n",
340 sc->sc_dev.dv_xname, (sc->sc_msize >> 10),
341 sc->ntxbuf, sc->nframes, sc->nrxbuf);
342 #endif
343
344 if ((sc->nframes <= 0) || (sc->nrxbuf <= 0))
345 panic("ie_attach: weird memory size");
346
347 /*
348 * Setup RAM for transmit/receive
349 */
350 if (ie_setupram(sc) == 0) {
351 printf(": RAM CONFIG FAILED!\n");
352 /* XXX should reclaim resources? */
353 return;
354 }
355
356 /*
357 * Initialize and attach S/W interface
358 */
359 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
360 ifp->if_softc = sc;
361 ifp->if_start = iestart;
362 ifp->if_ioctl = ieioctl;
363 ifp->if_watchdog = iewatchdog;
364 ifp->if_flags =
365 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
366
367 /* Attach the interface. */
368 if_attach(ifp);
369 ether_ifattach(ifp, sc->sc_addr);
370 #if NBPFILTER > 0
371 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
372 #endif
373 }
374
375 /*
376 * Setup IE's ram space.
377 */
378 static int
379 ie_setupram(sc)
380 struct ie_softc *sc;
381 {
382 volatile struct ie_sys_conf_ptr *scp;
383 volatile struct ie_int_sys_conf_ptr *iscp;
384 volatile struct ie_sys_ctl_block *scb;
385 int off;
386
387 /*
388 * Allocate from end of buffer space for
389 * ISCP, SCB, and other small stuff.
390 */
391 off = sc->buf_area_sz;
392 off &= ~3;
393
394 /* SCP (address already chosen). */
395 scp = sc->scp;
396 (sc->sc_memset)((char *) scp, 0, sizeof(*scp));
397
398 /* ISCP */
399 off -= sizeof(*iscp);
400 iscp = (volatile void *) (sc->buf_area + off);
401 (sc->sc_memset)((char *) iscp, 0, sizeof(*iscp));
402 sc->iscp = iscp;
403
404 /* SCB */
405 off -= sizeof(*scb);
406 scb = (volatile void *) (sc->buf_area + off);
407 (sc->sc_memset)((char *) scb, 0, sizeof(*scb));
408 sc->scb = scb;
409
410 /* Remainder is for buffers, etc. */
411 sc->buf_area_sz = off;
412
413 /*
414 * Now fill in the structures we just allocated.
415 */
416
417 /* SCP: main thing is 24-bit ptr to ISCP */
418 scp->ie_bus_use = 0; /* 16-bit */
419 scp->ie_iscp_ptr = Swap32(vtop24(sc, (void*)iscp));
420
421 /* ISCP */
422 iscp->ie_busy = 1; /* ie_busy == char */
423 iscp->ie_scb_offset = vtop16sw(sc, (void*)scb);
424 iscp->ie_base = Swap32(vtop24(sc, sc->sc_maddr));
425
426 /* SCB */
427 scb->ie_command_list = SWAP(0xffff);
428 scb->ie_recv_list = SWAP(0xffff);
429
430 /* Other stuff is done in ieinit() */
431 (sc->reset_586) (sc);
432 (sc->chan_attn) (sc);
433
434 delay(100); /* wait a while... */
435
436 if (iscp->ie_busy) {
437 return 0;
438 }
439 /*
440 * Acknowledge any interrupts we may have caused...
441 */
442 ie_ack(sc, IE_ST_WHENCE);
443
444 return 1;
445 }
446
447 /*
448 * Device timeout/watchdog routine. Entered if the device neglects to
449 * generate an interrupt after a transmit has been started on it.
450 */
451 static void
452 iewatchdog(ifp)
453 struct ifnet *ifp;
454 {
455 struct ie_softc *sc = ifp->if_softc;
456
457 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
458 ++ifp->if_oerrors;
459 iereset(sc);
460 }
461
462 /*
463 * What to do upon receipt of an interrupt.
464 */
465 int
466 ie_intr(arg)
467 void *arg;
468 {
469 struct ie_softc *sc = arg;
470 register u_short status;
471 int loopcnt;
472
473 /*
474 * check for parity error
475 */
476 if (sc->hard_type == IE_VME) {
477 volatile struct ievme *iev = (volatile struct ievme *)sc->sc_reg;
478 if (iev->status & IEVME_PERR) {
479 printf("%s: parity error (ctrl 0x%x @ 0x%02x%04x)\n",
480 sc->sc_dev.dv_xname, iev->pectrl,
481 iev->pectrl & IEVME_HADDR, iev->peaddr);
482 iev->pectrl = iev->pectrl | IEVME_PARACK;
483 }
484 }
485
486 status = sc->scb->ie_status;
487 if ((status & IE_ST_WHENCE) == 0)
488 return 0;
489
490 loopcnt = sc->nframes;
491 loop:
492 /* Ack interrupts FIRST in case we receive more during the ISR. */
493 ie_ack(sc, IE_ST_WHENCE & status);
494
495 if (status & (IE_ST_RECV | IE_ST_RNR)) {
496 #ifdef IEDEBUG
497 in_ierint++;
498 if (sc->sc_debug & IED_RINT)
499 printf("%s: rint\n", sc->sc_dev.dv_xname);
500 #endif
501 ierint(sc);
502 #ifdef IEDEBUG
503 in_ierint--;
504 #endif
505 }
506
507 if (status & IE_ST_DONE) {
508 #ifdef IEDEBUG
509 in_ietint++;
510 if (sc->sc_debug & IED_TINT)
511 printf("%s: tint\n", sc->sc_dev.dv_xname);
512 #endif
513 ietint(sc);
514 #ifdef IEDEBUG
515 in_ietint--;
516 #endif
517 }
518
519 /*
520 * Receiver not ready (RNR) just means it has
521 * run out of resources (buffers or frames).
522 * One can easily cause this with (i.e.) spray.
523 * This is not a serious error, so be silent.
524 */
525 if (status & IE_ST_RNR) {
526 #ifdef IEDEBUG
527 printf("%s: receiver not ready\n", sc->sc_dev.dv_xname);
528 #endif
529 sc->sc_if.if_ierrors++;
530 iereset(sc);
531 }
532
533 #ifdef IEDEBUG
534 if ((status & IE_ST_ALLDONE) && (sc->sc_debug & IED_CNA))
535 printf("%s: cna\n", sc->sc_dev.dv_xname);
536 #endif
537
538 status = sc->scb->ie_status;
539 if (status & IE_ST_WHENCE) {
540 /* It still wants service... */
541 if (--loopcnt > 0)
542 goto loop;
543 /* ... but we've been here long enough. */
544 log(LOG_ERR, "%s: interrupt stuck?\n",
545 sc->sc_dev.dv_xname);
546 iereset(sc);
547 }
548 return 1;
549 }
550
551 /*
552 * Process a received-frame interrupt.
553 */
554 void
555 ierint(sc)
556 struct ie_softc *sc;
557 {
558 volatile struct ie_sys_ctl_block *scb = sc->scb;
559 int i, status;
560 static int timesthru = 1024;
561
562 i = sc->rfhead;
563 for (;;) {
564 status = sc->rframes[i]->ie_fd_status;
565
566 if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
567 if (!--timesthru) {
568 sc->sc_if.if_ierrors +=
569 SWAP(scb->ie_err_crc) +
570 SWAP(scb->ie_err_align) +
571 SWAP(scb->ie_err_resource) +
572 SWAP(scb->ie_err_overrun);
573 scb->ie_err_crc = 0;
574 scb->ie_err_align = 0;
575 scb->ie_err_resource = 0;
576 scb->ie_err_overrun = 0;
577 timesthru = 1024;
578 }
579 ie_readframe(sc, i);
580 } else {
581 if ((status & IE_FD_RNR) != 0 &&
582 (scb->ie_status & IE_RU_READY) == 0) {
583 sc->rframes[0]->ie_fd_buf_desc =
584 vtop16sw(sc, (void*) sc->rbuffs[0]);
585 scb->ie_recv_list =
586 vtop16sw(sc, (void*) sc->rframes[0]);
587 cmd_and_wait(sc, IE_RU_START, 0, 0);
588 }
589 break;
590 }
591 i = (i + 1) % sc->nframes;
592 }
593 }
594
595 /*
596 * Process a command-complete interrupt. These are only generated by the
597 * transmission of frames. This routine is deceptively simple, since most
598 * of the real work is done by iestart().
599 */
600 void
601 ietint(sc)
602 struct ie_softc *sc;
603 {
604 struct ifnet *ifp;
605 int status;
606
607 ifp = &sc->sc_if;
608
609 ifp->if_timer = 0;
610 ifp->if_flags &= ~IFF_OACTIVE;
611
612 status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
613
614 if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
615 printf("ietint: command still busy!\n");
616
617 if (status & IE_STAT_OK) {
618 ifp->if_opackets++;
619 ifp->if_collisions +=
620 SWAP(status & IE_XS_MAXCOLL);
621 } else {
622 ifp->if_oerrors++;
623 /*
624 * XXX
625 * Check SQE and DEFERRED?
626 * What if more than one bit is set?
627 */
628 if (status & IE_STAT_ABORT)
629 printf("%s: send aborted\n", sc->sc_dev.dv_xname);
630 if (status & IE_XS_LATECOLL)
631 printf("%s: late collision\n", sc->sc_dev.dv_xname);
632 if (status & IE_XS_NOCARRIER)
633 printf("%s: no carrier\n", sc->sc_dev.dv_xname);
634 if (status & IE_XS_LOSTCTS)
635 printf("%s: lost CTS\n", sc->sc_dev.dv_xname);
636 if (status & IE_XS_UNDERRUN)
637 printf("%s: DMA underrun\n", sc->sc_dev.dv_xname);
638 if (status & IE_XS_EXCMAX) {
639 /* Do not print this one (too noisy). */
640 ifp->if_collisions += 16;
641 }
642 }
643
644 /*
645 * If multicast addresses were added or deleted while we
646 * were transmitting, mc_reset() set the want_mcsetup flag
647 * indicating that we should do it.
648 */
649 if (sc->want_mcsetup) {
650 mc_setup(sc, (caddr_t)sc->xmit_cbuffs[sc->xctail]);
651 sc->want_mcsetup = 0;
652 }
653
654 /* Done with the buffer. */
655 sc->xmit_busy--;
656 sc->xctail = (sc->xctail + 1) % NTXBUF;
657
658 /* Start the next packet, if any, transmitting. */
659 if (sc->xmit_busy > 0)
660 iexmit(sc);
661
662 iestart(ifp);
663 }
664
665 /*
666 * Compare two Ether/802 addresses for equality, inlined and
667 * unrolled for speed. I'd love to have an inline assembler
668 * version of this... XXX: Who wanted that? mycroft?
669 * I wrote one, but the following is just as efficient.
670 * This expands to 10 short m68k instructions! -gwr
671 * Note: use this like bcmp()
672 */
673 static inline u_short
674 ether_cmp(one, two)
675 u_char *one, *two;
676 {
677 register u_short *a = (u_short *) one;
678 register u_short *b = (u_short *) two;
679 register u_short diff;
680
681 diff = *a++ - *b++;
682 diff |= *a++ - *b++;
683 diff |= *a++ - *b++;
684
685 return (diff);
686 }
687 #define ether_equal !ether_cmp
688
689 /*
690 * Check for a valid address. to_bpf is filled in with one of the following:
691 * 0 -> BPF doesn't get this packet
692 * 1 -> BPF does get this packet
693 * 2 -> BPF does get this packet, but we don't
694 * Return value is true if the packet is for us, and false otherwise.
695 *
696 * This routine is a mess, but it's also critical that it be as fast
697 * as possible. It could be made cleaner if we can assume that the
698 * only client which will fiddle with IFF_PROMISC is BPF. This is
699 * probably a good assumption, but we do not make it here. (Yet.)
700 */
701 static inline int
702 check_eh(sc, eh, to_bpf)
703 struct ie_softc *sc;
704 struct ether_header *eh;
705 int *to_bpf;
706 {
707 struct ifnet *ifp;
708 int i;
709
710 ifp = &sc->sc_if;
711
712 switch (sc->promisc) {
713 case IFF_ALLMULTI:
714 /*
715 * Receiving all multicasts, but no unicasts except those
716 * destined for us.
717 */
718 #if NBPFILTER > 0
719 /* BPF gets this packet if anybody cares */
720 *to_bpf = (ifp->if_bpf != 0);
721 #endif
722 if (eh->ether_dhost[0] & 1)
723 return 1;
724 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
725 return 1;
726 return 0;
727
728 case IFF_PROMISC:
729 /*
730 * Receiving all packets. These need to be passed on to BPF.
731 */
732 #if NBPFILTER > 0
733 *to_bpf = (ifp->if_bpf != 0);
734 #endif
735 /* If for us, accept and hand up to BPF */
736 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
737 return 1;
738
739 #if NBPFILTER > 0
740 if (*to_bpf)
741 *to_bpf = 2; /* we don't need to see it */
742 #endif
743
744 /*
745 * Not a multicast, so BPF wants to see it but we don't.
746 */
747 if (!(eh->ether_dhost[0] & 1))
748 return 1;
749
750 /*
751 * If it's one of our multicast groups, accept it and pass it
752 * up.
753 */
754 for (i = 0; i < sc->mcast_count; i++) {
755 if (ether_equal(eh->ether_dhost,
756 (u_char *)&sc->mcast_addrs[i])) {
757 #if NBPFILTER > 0
758 if (*to_bpf)
759 *to_bpf = 1;
760 #endif
761 return 1;
762 }
763 }
764 return 1;
765
766 case IFF_ALLMULTI | IFF_PROMISC:
767 /*
768 * Acting as a multicast router, and BPF running at the same
769 * time. Whew! (Hope this is a fast machine...)
770 */
771 #if NBPFILTER > 0
772 *to_bpf = (ifp->if_bpf != 0);
773 #endif
774 /* We want to see multicasts. */
775 if (eh->ether_dhost[0] & 1)
776 return 1;
777
778 /* We want to see our own packets */
779 if (ether_equal(eh->ether_dhost, LLADDR(ifp->if_sadl)))
780 return 1;
781
782 /* Anything else goes to BPF but nothing else. */
783 #if NBPFILTER > 0
784 if (*to_bpf)
785 *to_bpf = 2;
786 #endif
787 return 1;
788
789 case 0:
790 /*
791 * Only accept unicast packets destined for us, or multicasts
792 * for groups that we belong to. For now, we assume that the
793 * '586 will only return packets that we asked it for. This
794 * isn't strictly true (it uses hashing for the multicast filter),
795 * but it will do in this case, and we want to get out of here
796 * as quickly as possible.
797 */
798 #if NBPFILTER > 0
799 *to_bpf = (ifp->if_bpf != 0);
800 #endif
801 return 1;
802 }
803 #ifdef DIAGNOSTIC
804 panic("ie: check_eh, impossible");
805 #endif
806 return 0;
807 }
808
809 /*
810 * We want to isolate the bits that have meaning... This assumes that
811 * IE_RBUF_SIZE is an even power of two. If somehow the act_len exceeds
812 * the size of the buffer, then we are screwed anyway.
813 */
814 static inline int
815 ie_buflen(sc, head)
816 struct ie_softc *sc;
817 int head;
818 {
819 register int len;
820
821 len = SWAP(sc->rbuffs[head]->ie_rbd_actual);
822 len &= (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1));
823 return (len);
824 }
825
826 static inline int
827 ie_packet_len(sc)
828 struct ie_softc *sc;
829 {
830 int i;
831 int head = sc->rbhead;
832 int acc = 0;
833
834 do {
835 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
836 #ifdef IEDEBUG
837 print_rbd(sc->rbuffs[sc->rbhead]);
838 #endif
839 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
840 sc->sc_dev.dv_xname, sc->rbhead);
841 iereset(sc);
842 return -1;
843 }
844
845 i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
846
847 acc += ie_buflen(sc, head);
848 head = (head + 1) % sc->nrxbuf;
849 } while (!i);
850
851 return acc;
852 }
853
854 /*
855 * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
856 * command to the chip to be executed. On the way, if we have a BPF listener
857 * also give him a copy.
858 */
859 static void
860 iexmit(sc)
861 struct ie_softc *sc;
862 {
863 struct ifnet *ifp;
864
865 ifp = &sc->sc_if;
866
867 #ifdef IEDEBUG
868 if (sc->sc_debug & IED_XMIT)
869 printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname,
870 sc->xctail);
871 #endif
872
873 #if NBPFILTER > 0
874 /*
875 * If BPF is listening on this interface, let it see the packet before
876 * we push it on the wire.
877 */
878 if (ifp->if_bpf)
879 bpf_tap(ifp->if_bpf,
880 sc->xmit_cbuffs[sc->xctail],
881 SWAP(sc->xmit_buffs[sc->xctail]->ie_xmit_flags));
882 #endif
883
884 sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
885 sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
886 sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
887 Swap32(vtop24(sc, sc->xmit_cbuffs[sc->xctail]));
888
889 sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
890 sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
891 IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
892
893 sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
894 sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
895 vtop16sw(sc, (void*) sc->xmit_buffs[sc->xctail]);
896
897 sc->scb->ie_command_list =
898 vtop16sw(sc, (void*) sc->xmit_cmds[sc->xctail]);
899 cmd_and_wait(sc, IE_CU_START, 0, 0);
900
901 ifp->if_timer = 5;
902 }
903
904 /*
905 * Read data off the interface, and turn it into an mbuf chain.
906 *
907 * This code is DRAMATICALLY different from the previous version; this
908 * version tries to allocate the entire mbuf chain up front, given the
909 * length of the data available. This enables us to allocate mbuf
910 * clusters in many situations where before we would have had a long
911 * chain of partially-full mbufs. This should help to speed up the
912 * operation considerably. (Provided that it works, of course.)
913 */
914 static inline struct mbuf *
915 ieget(sc, to_bpf)
916 struct ie_softc *sc;
917 int *to_bpf;
918 {
919 struct mbuf *top, **mp, *m;
920 int len, totlen, resid;
921 int thisrboff, thismboff;
922 int head;
923 struct ether_header eh;
924
925 totlen = ie_packet_len(sc);
926 if (totlen <= 0)
927 return 0;
928
929 head = sc->rbhead;
930
931 /*
932 * Snarf the Ethernet header.
933 */
934 (sc->sc_memcpy)((caddr_t)&eh, (caddr_t)sc->cbuffs[head],
935 sizeof(struct ether_header));
936
937 /*
938 * As quickly as possible, check if this packet is for us.
939 * If not, don't waste a single cycle copying the rest of the
940 * packet in.
941 * This is only a consideration when FILTER is defined; i.e., when
942 * we are either running BPF or doing multicasting.
943 */
944 if (!check_eh(sc, &eh, to_bpf)) {
945 /* just this case, it's not an error */
946 sc->sc_if.if_ierrors--;
947 return 0;
948 }
949
950 resid = totlen;
951
952 MGETHDR(m, M_DONTWAIT, MT_DATA);
953 if (m == 0)
954 return 0;
955
956 m->m_pkthdr.rcvif = &sc->sc_if;
957 m->m_pkthdr.len = totlen;
958 len = MHLEN;
959 top = 0;
960 mp = ⊤
961
962 /*
963 * This loop goes through and allocates mbufs for all the data we will
964 * be copying in. It does not actually do the copying yet.
965 */
966 while (totlen > 0) {
967 if (top) {
968 MGET(m, M_DONTWAIT, MT_DATA);
969 if (m == 0) {
970 m_freem(top);
971 return 0;
972 }
973 len = MLEN;
974 }
975 if (totlen >= MINCLSIZE) {
976 MCLGET(m, M_DONTWAIT);
977 if (m->m_flags & M_EXT)
978 len = MCLBYTES;
979 }
980
981 if (mp == &top) {
982 caddr_t newdata = (caddr_t)
983 ALIGN(m->m_data + sizeof(struct ether_header)) -
984 sizeof(struct ether_header);
985 len -= newdata - m->m_data;
986 m->m_data = newdata;
987 }
988
989 m->m_len = len = min(totlen, len);
990
991 totlen -= len;
992 *mp = m;
993 mp = &m->m_next;
994 }
995
996 m = top;
997 thismboff = 0;
998
999 /*
1000 * Copy the Ethernet header into the mbuf chain.
1001 */
1002 memcpy(mtod(m, caddr_t), &eh, sizeof(struct ether_header));
1003 thismboff = sizeof(struct ether_header);
1004 thisrboff = sizeof(struct ether_header);
1005 resid -= sizeof(struct ether_header);
1006
1007 /*
1008 * Now we take the mbuf chain (hopefully only one mbuf most of the
1009 * time) and stuff the data into it. There are no possible failures
1010 * at or after this point.
1011 */
1012 while (resid > 0) {
1013 int thisrblen = ie_buflen(sc, head) - thisrboff;
1014 int thismblen = m->m_len - thismboff;
1015
1016 len = min(thisrblen, thismblen);
1017 (sc->sc_memcpy)(mtod(m, caddr_t) + thismboff,
1018 (caddr_t)(sc->cbuffs[head] + thisrboff),
1019 (u_int)len);
1020 resid -= len;
1021
1022 if (len == thismblen) {
1023 m = m->m_next;
1024 thismboff = 0;
1025 } else
1026 thismboff += len;
1027
1028 if (len == thisrblen) {
1029 head = (head + 1) % sc->nrxbuf;
1030 thisrboff = 0;
1031 } else
1032 thisrboff += len;
1033 }
1034
1035 /*
1036 * Unless something changed strangely while we were doing the copy,
1037 * we have now copied everything in from the shared memory.
1038 * This means that we are done.
1039 */
1040 return top;
1041 }
1042
1043 /*
1044 * Read frame NUM from unit UNIT (pre-cached as IE).
1045 *
1046 * This routine reads the RFD at NUM, and copies in the buffers from
1047 * the list of RBD, then rotates the RBD and RFD lists so that the receiver
1048 * doesn't start complaining. Trailers are DROPPED---there's no point
1049 * in wasting time on confusing code to deal with them. Hopefully,
1050 * this machine will never ARP for trailers anyway.
1051 */
1052 static void
1053 ie_readframe(sc, num)
1054 struct ie_softc *sc;
1055 int num; /* frame number to read */
1056 {
1057 int status;
1058 struct mbuf *m = 0;
1059 #if NBPFILTER > 0
1060 int bpf_gets_it = 0;
1061 #endif
1062
1063 status = sc->rframes[num]->ie_fd_status;
1064
1065 /* Advance the RFD list, since we're done with this descriptor. */
1066 sc->rframes[num]->ie_fd_status = SWAP(0);
1067 sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
1068 sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
1069 sc->rftail = (sc->rftail + 1) % sc->nframes;
1070 sc->rfhead = (sc->rfhead + 1) % sc->nframes;
1071
1072 if (status & IE_FD_OK) {
1073 #if NBPFILTER > 0
1074 m = ieget(sc, &bpf_gets_it);
1075 #else
1076 m = ieget(sc, 0);
1077 #endif
1078 ie_drop_packet_buffer(sc);
1079 }
1080 if (m == 0) {
1081 sc->sc_if.if_ierrors++;
1082 return;
1083 }
1084
1085 #ifdef IEDEBUG
1086 if (sc->sc_debug & IED_READFRAME) {
1087 struct ether_header *eh = mtod(m, struct ether_header *);
1088
1089 printf("%s: frame from ether %s type 0x%x\n",
1090 sc->sc_dev.dv_xname,
1091 ether_sprintf(eh->ether_shost), (u_int)eh->ether_type);
1092 }
1093 #endif
1094
1095 #if NBPFILTER > 0
1096 /*
1097 * Check for a BPF filter; if so, hand it up.
1098 * Note that we have to stick an extra mbuf up front, because
1099 * bpf_mtap expects to have the ether header at the front.
1100 * It doesn't matter that this results in an ill-formatted mbuf chain,
1101 * since BPF just looks at the data. (It doesn't try to free the mbuf,
1102 * tho' it will make a copy for tcpdump.)
1103 */
1104 if (bpf_gets_it) {
1105 /* Pass it up. */
1106 bpf_mtap(sc->sc_if.if_bpf, m);
1107
1108 /*
1109 * A signal passed up from the filtering code indicating that
1110 * the packet is intended for BPF but not for the protocol
1111 * machinery. We can save a few cycles by not handing it off
1112 * to them.
1113 */
1114 if (bpf_gets_it == 2) {
1115 m_freem(m);
1116 return;
1117 }
1118 }
1119 #endif /* NBPFILTER > 0 */
1120
1121 /*
1122 * In here there used to be code to check destination addresses upon
1123 * receipt of a packet. We have deleted that code, and replaced it
1124 * with code to check the address much earlier in the cycle, before
1125 * copying the data in; this saves us valuable cycles when operating
1126 * as a multicast router or when using BPF.
1127 */
1128
1129 /*
1130 * Finally pass this packet up to higher layers.
1131 */
1132 (*sc->sc_if.if_input)(&sc->sc_if, m);
1133 sc->sc_if.if_ipackets++;
1134 }
1135
1136 static void
1137 ie_drop_packet_buffer(sc)
1138 struct ie_softc *sc;
1139 {
1140 int i;
1141
1142 do {
1143 /*
1144 * This means we are somehow out of sync. So, we reset the
1145 * adapter.
1146 */
1147 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
1148 #ifdef IEDEBUG
1149 print_rbd(sc->rbuffs[sc->rbhead]);
1150 #endif
1151 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
1152 sc->sc_dev.dv_xname, sc->rbhead);
1153 iereset(sc);
1154 return;
1155 }
1156
1157 i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
1158
1159 sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
1160 sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
1161 sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
1162 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
1163 sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
1164 } while (!i);
1165 }
1166
1167 /*
1168 * Start transmission on an interface.
1169 */
1170 static void
1171 iestart(ifp)
1172 struct ifnet *ifp;
1173 {
1174 struct ie_softc *sc = ifp->if_softc;
1175 struct mbuf *m0, *m;
1176 u_char *buffer;
1177 u_short len;
1178
1179 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1180 return;
1181
1182 for (;;) {
1183 if (sc->xmit_busy == sc->ntxbuf) {
1184 ifp->if_flags |= IFF_OACTIVE;
1185 break;
1186 }
1187
1188 IF_DEQUEUE(&ifp->if_snd, m0);
1189 if (m0 == 0)
1190 break;
1191
1192 /* We need to use m->m_pkthdr.len, so require the header */
1193 if ((m0->m_flags & M_PKTHDR) == 0)
1194 panic("iestart: no header mbuf");
1195
1196 #if NBPFILTER > 0
1197 /* Tap off here if there is a BPF listener. */
1198 if (ifp->if_bpf)
1199 bpf_mtap(ifp->if_bpf, m0);
1200 #endif
1201
1202 #ifdef IEDEBUG
1203 if (sc->sc_debug & IED_ENQ)
1204 printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
1205 sc->xchead);
1206 #endif
1207
1208 buffer = sc->xmit_cbuffs[sc->xchead];
1209 for (m = m0; m != 0; m = m->m_next) {
1210 (sc->sc_memcpy)(buffer, mtod(m, caddr_t), m->m_len);
1211 buffer += m->m_len;
1212 }
1213 len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
1214
1215 m_freem(m0);
1216 sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
1217
1218 /* Start the first packet transmitting. */
1219 if (sc->xmit_busy == 0)
1220 iexmit(sc);
1221
1222 sc->xchead = (sc->xchead + 1) % sc->ntxbuf;
1223 sc->xmit_busy++;
1224 }
1225 }
1226
1227 static void
1228 iereset(sc)
1229 struct ie_softc *sc;
1230 {
1231 int s = splnet();
1232
1233 /* No message here. The caller does that. */
1234 iestop(sc);
1235
1236 /*
1237 * Stop i82586 dead in its tracks.
1238 */
1239 if (cmd_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1240 printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
1241
1242 if (cmd_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1243 printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
1244
1245 ieinit(sc);
1246
1247 splx(s);
1248 }
1249
1250 /*
1251 * Send a command to the controller and wait for it to either
1252 * complete or be accepted, depending on the command. If the
1253 * command pointer is null, then pretend that the command is
1254 * not an action command. If the command pointer is not null,
1255 * and the command is an action command, wait for
1256 * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1257 * to become true.
1258 */
1259 static int
1260 cmd_and_wait(sc, cmd, pcmd, mask)
1261 struct ie_softc *sc;
1262 int cmd;
1263 void *pcmd; /* XXX - Was volatile */
1264 int mask;
1265 {
1266 volatile struct ie_cmd_common *cc = pcmd;
1267 volatile struct ie_sys_ctl_block *scb = sc->scb;
1268 int tmo;
1269
1270 scb->ie_command = (u_short)cmd;
1271 (sc->chan_attn)(sc);
1272
1273 /* Wait for the command to be accepted by the CU. */
1274 tmo = 10;
1275 while (scb->ie_command && --tmo)
1276 delay(10);
1277 if (scb->ie_command) {
1278 #ifdef IEDEBUG
1279 printf("%s: cmd_and_wait, CU stuck (1)\n",
1280 sc->sc_dev.dv_xname);
1281 #endif
1282 return -1; /* timed out */
1283 }
1284
1285 /*
1286 * If asked, also wait for it to finish.
1287 */
1288 if (IE_ACTION_COMMAND(cmd) && pcmd) {
1289
1290 /*
1291 * According to the packet driver, the minimum timeout should
1292 * be .369 seconds, which we round up to .4.
1293 */
1294 tmo = 36900;
1295
1296 /*
1297 * Now spin-lock waiting for status. This is not a very nice
1298 * thing to do, but I haven't figured out how, or indeed if, we
1299 * can put the process waiting for action to sleep. (We may
1300 * be getting called through some other timeout running in the
1301 * kernel.)
1302 */
1303 while (((cc->ie_cmd_status & mask) == 0) && --tmo)
1304 delay(10);
1305
1306 if ((cc->ie_cmd_status & mask) == 0) {
1307 #ifdef IEDEBUG
1308 printf("%s: cmd_and_wait, CU stuck (2)\n",
1309 sc->sc_dev.dv_xname);
1310 #endif
1311 return -1; /* timed out */
1312 }
1313 }
1314 return 0;
1315 }
1316
1317 /*
1318 * Run the time-domain reflectometer.
1319 */
1320 static void
1321 run_tdr(sc, cmd)
1322 struct ie_softc *sc;
1323 struct ie_tdr_cmd *cmd;
1324 {
1325 int result;
1326
1327 cmd->com.ie_cmd_status = SWAP(0);
1328 cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1329 cmd->com.ie_cmd_link = SWAP(0xffff);
1330
1331 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1332 cmd->ie_tdr_time = SWAP(0);
1333
1334 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1335 !(cmd->com.ie_cmd_status & IE_STAT_OK))
1336 result = 0x10000; /* impossible value */
1337 else
1338 result = cmd->ie_tdr_time;
1339
1340 ie_ack(sc, IE_ST_WHENCE);
1341
1342 if (result & IE_TDR_SUCCESS)
1343 return;
1344
1345 if (result & 0x10000) {
1346 printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
1347 } else if (result & IE_TDR_XCVR) {
1348 printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
1349 } else if (result & IE_TDR_OPEN) {
1350 printf("%s: TDR detected an open %d clocks away\n",
1351 sc->sc_dev.dv_xname, SWAP(result & IE_TDR_TIME));
1352 } else if (result & IE_TDR_SHORT) {
1353 printf("%s: TDR detected a short %d clocks away\n",
1354 sc->sc_dev.dv_xname, SWAP(result & IE_TDR_TIME));
1355 } else {
1356 printf("%s: TDR returned unknown status 0x%x\n",
1357 sc->sc_dev.dv_xname, result);
1358 }
1359 }
1360
1361 /*
1362 * iememinit: set up the buffers
1363 *
1364 * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1365 * this is to be used for the buffers. the chip indexs its control data
1366 * structures with 16 bit offsets, and it indexes actual buffers with
1367 * 24 bit addresses. so we should allocate control buffers first so that
1368 * we don't overflow the 16 bit offset field. The number of transmit
1369 * buffers is fixed at compile time.
1370 *
1371 * note: this function was written to be easy to understand, rather than
1372 * highly efficient (it isn't in the critical path).
1373 *
1374 * The memory layout is: tbufs, rbufs, (gap), control blocks
1375 * [tbuf0, tbuf1] [rbuf0,...rbufN] gap [rframes] [tframes]
1376 * XXX - This needs review...
1377 */
1378 static void
1379 iememinit(sc)
1380 struct ie_softc *sc;
1381 {
1382 char *ptr;
1383 int i;
1384 u_short nxt;
1385
1386 /* First, zero all the memory. */
1387 ptr = sc->buf_area;
1388 (sc->sc_memset)(ptr, 0, sc->buf_area_sz);
1389
1390 /* Allocate tx/rx buffers. */
1391 for (i = 0; i < NTXBUF; i++) {
1392 sc->xmit_cbuffs[i] = ptr;
1393 ptr += IE_TBUF_SIZE;
1394 }
1395 for (i = 0; i < sc->nrxbuf; i++) {
1396 sc->cbuffs[i] = ptr;
1397 ptr += IE_RBUF_SIZE;
1398 }
1399
1400 /* Small pad (Don't trust the chip...) */
1401 ptr += 16;
1402
1403 /* Allocate and fill in xmit buffer descriptors. */
1404 for (i = 0; i < NTXBUF; i++) {
1405 sc->xmit_buffs[i] = (volatile void *) ptr;
1406 ptr = Align(ptr + sizeof(*sc->xmit_buffs[i]));
1407 sc->xmit_buffs[i]->ie_xmit_buf =
1408 Swap32(vtop24(sc, sc->xmit_cbuffs[i]));
1409 sc->xmit_buffs[i]->ie_xmit_next = SWAP(0xffff);
1410 }
1411
1412 /* Allocate and fill in recv buffer descriptors. */
1413 for (i = 0; i < sc->nrxbuf; i++) {
1414 sc->rbuffs[i] = (volatile void *) ptr;
1415 ptr = Align(ptr + sizeof(*sc->rbuffs[i]));
1416 sc->rbuffs[i]->ie_rbd_buffer =
1417 Swap32(vtop24(sc, sc->cbuffs[i]));
1418 sc->rbuffs[i]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
1419 }
1420
1421 /* link together recv bufs and set EOL on last */
1422 i = sc->nrxbuf - 1;
1423 sc->rbuffs[i]->ie_rbd_length |= IE_RBD_LAST;
1424 nxt = vtop16sw(sc, (void*) sc->rbuffs[0]);
1425 do {
1426 sc->rbuffs[i]->ie_rbd_next = nxt;
1427 nxt = vtop16sw(sc, (void*) sc->rbuffs[i]);
1428 } while (--i >= 0);
1429
1430 /* Allocate transmit commands. */
1431 for (i = 0; i < NTXBUF; i++) {
1432 sc->xmit_cmds[i] = (volatile void *) ptr;
1433 ptr = Align(ptr + sizeof(*sc->xmit_cmds[i]));
1434 sc->xmit_cmds[i]->com.ie_cmd_link = SWAP(0xffff);
1435 }
1436
1437 /* Allocate receive frames. */
1438 for (i = 0; i < sc->nframes; i++) {
1439 sc->rframes[i] = (volatile void *) ptr;
1440 ptr = Align(ptr + sizeof(*sc->rframes[i]));
1441 }
1442
1443 /* Link together recv frames and set EOL on last */
1444 i = sc->nframes - 1;
1445 sc->rframes[i]->ie_fd_last |= IE_FD_LAST;
1446 nxt = vtop16sw(sc, (void*) sc->rframes[0]);
1447 do {
1448 sc->rframes[i]->ie_fd_next = nxt;
1449 nxt = vtop16sw(sc, (void*) sc->rframes[i]);
1450 } while (--i >= 0);
1451
1452
1453 /* Pointers to last packet sent and next available transmit buffer. */
1454 sc->xchead = sc->xctail = 0;
1455
1456 /* Clear transmit-busy flag. */
1457 sc->xmit_busy = 0;
1458
1459 /*
1460 * Set the head and tail pointers on receive to keep track of
1461 * the order in which RFDs and RBDs are used. link the
1462 * recv frames and buffer into the scb.
1463 */
1464 sc->rfhead = 0;
1465 sc->rftail = sc->nframes - 1;
1466 sc->rbhead = 0;
1467 sc->rbtail = sc->nrxbuf - 1;
1468
1469 sc->scb->ie_recv_list =
1470 vtop16sw(sc, (void*) sc->rframes[0]);
1471 sc->rframes[0]->ie_fd_buf_desc =
1472 vtop16sw(sc, (void*) sc->rbuffs[0]);
1473
1474 i = (ptr - sc->buf_area);
1475 #ifdef IEDEBUG
1476 printf("IE_DEBUG: used %d of %d bytes\n", i, sc->buf_area_sz);
1477 #endif
1478 if (i > sc->buf_area_sz)
1479 panic("ie: iememinit, out of space");
1480 }
1481
1482 /*
1483 * Run the multicast setup command.
1484 * Called at splnet().
1485 */
1486 static int
1487 mc_setup(sc, ptr)
1488 struct ie_softc *sc;
1489 void *ptr;
1490 {
1491 struct ie_mcast_cmd *cmd = ptr; /* XXX - Was volatile */
1492
1493 cmd->com.ie_cmd_status = SWAP(0);
1494 cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1495 cmd->com.ie_cmd_link = SWAP(0xffff);
1496
1497 (sc->sc_memcpy)((caddr_t)cmd->ie_mcast_addrs,
1498 (caddr_t)sc->mcast_addrs,
1499 sc->mcast_count * sizeof *sc->mcast_addrs);
1500
1501 cmd->ie_mcast_bytes =
1502 SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
1503
1504 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1505 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1506 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1507 printf("%s: multicast address setup command failed\n",
1508 sc->sc_dev.dv_xname);
1509 return 0;
1510 }
1511 return 1;
1512 }
1513
1514 static inline void
1515 ie_setup_config(cmd, promiscuous, manchester)
1516 struct ie_config_cmd *cmd; /* XXX - was volatile */
1517 int promiscuous, manchester;
1518 {
1519
1520 /*
1521 * these are all char's so no need to byte-swap
1522 */
1523 cmd->ie_config_count = 0x0c;
1524 cmd->ie_fifo = 8;
1525 cmd->ie_save_bad = 0x40;
1526 cmd->ie_addr_len = 0x2e;
1527 cmd->ie_priority = 0;
1528 cmd->ie_ifs = 0x60;
1529 cmd->ie_slot_low = 0;
1530 cmd->ie_slot_high = 0xf2;
1531 cmd->ie_promisc = promiscuous | manchester << 2;
1532 cmd->ie_crs_cdt = 0;
1533 cmd->ie_min_len = 64;
1534 cmd->ie_junk = 0xff;
1535 }
1536
1537 /*
1538 * This routine inits the ie.
1539 * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1540 * starting the receiver unit, and clearing interrupts.
1541 *
1542 * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1543 */
1544 static int
1545 ieinit(sc)
1546 struct ie_softc *sc;
1547 {
1548 volatile struct ie_sys_ctl_block *scb = sc->scb;
1549 void *ptr;
1550 struct ifnet *ifp;
1551
1552 ifp = &sc->sc_if;
1553 ptr = sc->buf_area; /* XXX - Use scb instead? */
1554
1555 /*
1556 * Send the configure command first.
1557 */
1558 {
1559 struct ie_config_cmd *cmd = ptr; /* XXX - Was volatile */
1560
1561 scb->ie_command_list = vtop16sw(sc, cmd);
1562 cmd->com.ie_cmd_status = SWAP(0);
1563 cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1564 cmd->com.ie_cmd_link = SWAP(0xffff);
1565
1566 ie_setup_config(cmd, (sc->promisc != 0), 0);
1567
1568 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1569 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1570 printf("%s: configure command failed\n",
1571 sc->sc_dev.dv_xname);
1572 return 0;
1573 }
1574 }
1575
1576 /*
1577 * Now send the Individual Address Setup command.
1578 */
1579 {
1580 struct ie_iasetup_cmd *cmd = ptr; /* XXX - Was volatile */
1581
1582 scb->ie_command_list = vtop16sw(sc, cmd);
1583 cmd->com.ie_cmd_status = SWAP(0);
1584 cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1585 cmd->com.ie_cmd_link = SWAP(0xffff);
1586
1587 (sc->sc_memcpy)((caddr_t)&cmd->ie_address,
1588 LLADDR(ifp->if_sadl), sizeof(cmd->ie_address));
1589
1590 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1591 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1592 printf("%s: individual address setup command failed\n",
1593 sc->sc_dev.dv_xname);
1594 return 0;
1595 }
1596 }
1597
1598 /*
1599 * Now run the time-domain reflectometer.
1600 */
1601 if (ie_run_tdr)
1602 run_tdr(sc, ptr);
1603
1604 /*
1605 * Acknowledge any interrupts we have generated thus far.
1606 */
1607 ie_ack(sc, IE_ST_WHENCE);
1608
1609 /*
1610 * Set up the transmit and recv buffers.
1611 */
1612 iememinit(sc);
1613
1614 /* tell higher levels that we are here */
1615 ifp->if_flags |= IFF_RUNNING;
1616 ifp->if_flags &= ~IFF_OACTIVE;
1617
1618 sc->scb->ie_recv_list =
1619 vtop16sw(sc, (void*) sc->rframes[0]);
1620 cmd_and_wait(sc, IE_RU_START, 0, 0);
1621
1622 ie_ack(sc, IE_ST_WHENCE);
1623
1624 if (sc->run_586)
1625 (sc->run_586)(sc);
1626
1627 return 0;
1628 }
1629
1630 static void
1631 iestop(sc)
1632 struct ie_softc *sc;
1633 {
1634
1635 cmd_and_wait(sc, IE_RU_DISABLE, 0, 0);
1636 }
1637
1638 static int
1639 ieioctl(ifp, cmd, data)
1640 register struct ifnet *ifp;
1641 u_long cmd;
1642 caddr_t data;
1643 {
1644 struct ie_softc *sc = ifp->if_softc;
1645 struct ifaddr *ifa = (struct ifaddr *)data;
1646 struct ifreq *ifr = (struct ifreq *)data;
1647 int s, error = 0;
1648
1649 s = splnet();
1650
1651 switch (cmd) {
1652
1653 case SIOCSIFADDR:
1654 ifp->if_flags |= IFF_UP;
1655
1656 switch (ifa->ifa_addr->sa_family) {
1657 #ifdef INET
1658 case AF_INET:
1659 ieinit(sc);
1660 arp_ifinit(ifp, ifa);
1661 break;
1662 #endif
1663 #ifdef NS
1664 /* XXX - This code is probably wrong. */
1665 case AF_NS:
1666 {
1667 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1668
1669 if (ns_nullhost(*ina))
1670 ina->x_host =
1671 *(union ns_host *)LLADDR(ifp->if_sadl);
1672 else
1673 bcopy(ina->x_host.c_host,
1674 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
1675 /* Set new address. */
1676 ieinit(sc);
1677 break;
1678 }
1679 #endif /* NS */
1680 default:
1681 ieinit(sc);
1682 break;
1683 }
1684 break;
1685
1686 case SIOCSIFFLAGS:
1687 sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1688
1689 if ((ifp->if_flags & IFF_UP) == 0 &&
1690 (ifp->if_flags & IFF_RUNNING) != 0) {
1691 /*
1692 * If interface is marked down and it is running, then
1693 * stop it.
1694 */
1695 iestop(sc);
1696 ifp->if_flags &= ~IFF_RUNNING;
1697 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1698 (ifp->if_flags & IFF_RUNNING) == 0) {
1699 /*
1700 * If interface is marked up and it is stopped, then
1701 * start it.
1702 */
1703 ieinit(sc);
1704 } else {
1705 /*
1706 * Reset the interface to pick up changes in any other
1707 * flags that affect hardware registers.
1708 */
1709 iestop(sc);
1710 ieinit(sc);
1711 }
1712 #ifdef IEDEBUG
1713 if (ifp->if_flags & IFF_DEBUG)
1714 sc->sc_debug = IED_ALL;
1715 else
1716 sc->sc_debug = ie_debug_flags;
1717 #endif
1718 break;
1719
1720 case SIOCADDMULTI:
1721 case SIOCDELMULTI:
1722 error = (cmd == SIOCADDMULTI) ?
1723 ether_addmulti(ifr, &sc->sc_ethercom) :
1724 ether_delmulti(ifr, &sc->sc_ethercom);
1725
1726 if (error == ENETRESET) {
1727 /*
1728 * Multicast list has changed; set the hardware filter
1729 * accordingly.
1730 */
1731 mc_reset(sc);
1732 error = 0;
1733 }
1734 break;
1735
1736 default:
1737 error = EINVAL;
1738 }
1739 splx(s);
1740 return error;
1741 }
1742
1743 static void
1744 mc_reset(sc)
1745 struct ie_softc *sc;
1746 {
1747 struct ether_multi *enm;
1748 struct ether_multistep step;
1749 struct ifnet *ifp;
1750
1751 ifp = &sc->sc_if;
1752
1753 /*
1754 * Step through the list of addresses.
1755 */
1756 sc->mcast_count = 0;
1757 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1758 while (enm) {
1759 if (sc->mcast_count >= MAXMCAST ||
1760 bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
1761 ifp->if_flags |= IFF_ALLMULTI;
1762 ieioctl(ifp, SIOCSIFFLAGS, (void *)0);
1763 goto setflag;
1764 }
1765 bcopy(enm->enm_addrlo, &sc->mcast_addrs[sc->mcast_count], 6);
1766 sc->mcast_count++;
1767 ETHER_NEXT_MULTI(step, enm);
1768 }
1769 setflag:
1770 sc->want_mcsetup = 1;
1771 }
1772
1773 #ifdef IEDEBUG
1774 void
1775 print_rbd(rbd)
1776 volatile struct ie_recv_buf_desc *rbd;
1777 {
1778
1779 printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
1780 "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
1781 rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
1782 rbd->mbz);
1783 }
1784 #endif
1785