if_ie.c revision 1.40 1 /* $NetBSD: if_ie.c,v 1.40 2004/10/30 18:08:35 thorpej 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 <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: if_ie.c,v 1.40 2004/10/30 18:08:35 thorpej Exp $");
102
103 #include "opt_inet.h"
104 #include "opt_ns.h"
105 #include "bpfilter.h"
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/mbuf.h>
110 #include <sys/buf.h>
111 #include <sys/protosw.h>
112 #include <sys/socket.h>
113 #include <sys/ioctl.h>
114 #include <sys/errno.h>
115 #include <sys/syslog.h>
116 #include <sys/device.h>
117
118 #include <net/if.h>
119 #include <net/if_types.h>
120 #include <net/if_dl.h>
121 #include <net/if_ether.h>
122
123 #if NBPFILTER > 0
124 #include <net/bpf.h>
125 #include <net/bpfdesc.h>
126 #endif
127
128 #ifdef INET
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
133 #include <netinet/if_inarp.h>
134 #endif
135
136 #ifdef NS
137 #include <netns/ns.h>
138 #include <netns/ns_if.h>
139 #endif
140
141 #include <uvm/uvm_extern.h>
142
143 #include <machine/autoconf.h>
144 #include <machine/cpu.h>
145 #include <machine/pmap.h>
146
147 /*
148 * ugly byte-order hack for SUNs
149 */
150
151 #define XSWAP(y) ( (((y)&0xff00) >> 8) | (((y)&0xff) << 8) )
152 #define SWAP(x) ((u_short)(XSWAP((u_short)(x))))
153
154 #include "i82586.h"
155 #include "if_iereg.h"
156 #include "if_ievar.h"
157
158 /* #define IEDEBUG XXX */
159
160 /*
161 * IED: ie debug flags
162 */
163
164 #define IED_RINT 0x01
165 #define IED_TINT 0x02
166 #define IED_RNR 0x04
167 #define IED_CNA 0x08
168 #define IED_READFRAME 0x10
169 #define IED_ENQ 0x20
170 #define IED_XMIT 0x40
171 #define IED_ALL 0x7f
172
173 #ifdef IEDEBUG
174 #define inline /* not */
175 void print_rbd __P((volatile struct ie_recv_buf_desc *));
176 int in_ierint = 0;
177 int in_ietint = 0;
178 int ie_debug_flags = 0;
179 #endif
180
181 /* XXX - Skip TDR for now - it always complains... */
182 int ie_run_tdr = 0;
183
184 static void iewatchdog __P((struct ifnet *));
185 static int ieinit __P((struct ie_softc *));
186 static int ieioctl __P((struct ifnet *, u_long, caddr_t));
187 static void iestart __P((struct ifnet *));
188 static void iereset __P((struct ie_softc *));
189 static int ie_setupram __P((struct ie_softc *sc));
190
191 static int cmd_and_wait __P((struct ie_softc *, int, void *, int));
192
193 static void ie_drop_packet_buffer __P((struct ie_softc *));
194 static void ie_readframe __P((struct ie_softc *, int));
195 static inline void ie_setup_config __P((struct ie_config_cmd *, int, int));
196
197 static void ierint __P((struct ie_softc *));
198 static void iestop __P((struct ie_softc *));
199 static void ietint __P((struct ie_softc *));
200 static void iexmit __P((struct ie_softc *));
201
202 static int mc_setup __P((struct ie_softc *, void *));
203 static void mc_reset __P((struct ie_softc *));
204 static void run_tdr __P((struct ie_softc *, struct ie_tdr_cmd *));
205 static void iememinit __P((struct ie_softc *));
206
207 static inline char * Align __P((char *));
208 static inline u_int Swap32 __P((u_int x));
209 static inline u_int vtop24 __P((struct ie_softc *, void *));
210 static inline u_short vtop16sw __P((struct ie_softc *, void *));
211
212 static inline void ie_ack __P((struct ie_softc *, u_int));
213 static inline u_short ether_cmp __P((u_char *, u_char *));
214 static inline int check_eh __P((struct ie_softc *,
215 struct ether_header *eh, int *));
216 static inline int ie_buflen __P((struct ie_softc *, int));
217 static inline int ie_packet_len __P((struct ie_softc *));
218 static inline struct mbuf * ieget __P((struct ie_softc *sc, int *to_bpf));
219
220
221 /*
222 * Here are a few useful functions. We could have done these as macros,
223 * but since we have the inline facility, it makes sense to use that
224 * instead.
225 */
226
227 /* KVA to 24 bit device address */
228 static inline u_int
229 vtop24(sc, ptr)
230 struct ie_softc *sc;
231 void *ptr;
232 {
233 u_int pa;
234
235 pa = ((caddr_t)ptr) - sc->sc_iobase;
236 #ifdef IEDEBUG
237 if (pa & ~0xffFFff)
238 panic("ie:vtop24");
239 #endif
240 return (pa);
241 }
242
243 /* KVA to 16 bit offset, swapped */
244 static inline u_short
245 vtop16sw(sc, ptr)
246 struct ie_softc *sc;
247 void *ptr;
248 {
249 u_int pa;
250
251 pa = ((caddr_t)ptr) - sc->sc_maddr;
252 #ifdef IEDEBUG
253 if (pa & ~0xFFff)
254 panic("ie:vtop16");
255 #endif
256
257 return (SWAP(pa));
258 }
259
260 static inline u_int
261 Swap32(x)
262 u_int x;
263 {
264 u_int y;
265
266 y = x & 0xFF;
267 y <<= 8; x >>= 8;
268 y |= x & 0xFF;
269 y <<= 8; x >>= 8;
270 y |= x & 0xFF;
271 y <<= 8; x >>= 8;
272 y |= x & 0xFF;
273
274 return (y);
275 }
276
277 static inline char *
278 Align(ptr)
279 caddr_t ptr;
280 {
281 u_long l = (u_long)ptr;
282
283 l = (l + 3) & ~3L;
284 return ((char *)l);
285 }
286
287
288 static inline void
289 ie_ack(sc, mask)
290 struct ie_softc *sc;
291 u_int mask;
292 {
293 volatile struct ie_sys_ctl_block *scb = sc->scb;
294
295 cmd_and_wait(sc, scb->ie_status & mask, 0, 0);
296 }
297
298
299 /*
300 * Taken almost exactly from Bill's if_is.c,
301 * then modified beyond recognition...
302 */
303 void
304 ie_attach(sc)
305 struct ie_softc *sc;
306 {
307 struct ifnet *ifp = &sc->sc_if;
308
309 /* MD code has done its part before calling this. */
310 printf(": macaddr %s\n", ether_sprintf(sc->sc_addr));
311
312 /*
313 * Compute number of transmit and receive buffers.
314 * Tx buffers take 1536 bytes, and fixed in number.
315 * Rx buffers are 512 bytes each, variable number.
316 * Need at least 1 frame for each 3 rx buffers.
317 * The ratio 3bufs:2frames is a compromise.
318 */
319 sc->ntxbuf = NTXBUF; /* XXX - Fix me... */
320 switch (sc->sc_msize) {
321 case 16384:
322 sc->nframes = 8 * 4;
323 sc->nrxbuf = 8 * 6;
324 break;
325 case 32768:
326 sc->nframes = 16 * 4;
327 sc->nrxbuf = 16 * 6;
328 break;
329 case 65536:
330 sc->nframes = 32 * 4;
331 sc->nrxbuf = 32 * 6;
332 break;
333 default:
334 sc->nframes = 0;
335 }
336 if (sc->nframes > MXFRAMES)
337 sc->nframes = MXFRAMES;
338 if (sc->nrxbuf > MXRXBUF)
339 sc->nrxbuf = MXRXBUF;
340
341 #ifdef IEDEBUG
342 printf("%s: %dK memory, %d tx frames, %d rx frames, %d rx bufs\n",
343 sc->sc_dev.dv_xname, (sc->sc_msize >> 10),
344 sc->ntxbuf, sc->nframes, sc->nrxbuf);
345 #endif
346
347 if ((sc->nframes <= 0) || (sc->nrxbuf <= 0))
348 panic("ie_attach: weird memory size");
349
350 /*
351 * Setup RAM for transmit/receive
352 */
353 if (ie_setupram(sc) == 0) {
354 printf(": RAM CONFIG FAILED!\n");
355 /* XXX should reclaim resources? */
356 return;
357 }
358
359 /*
360 * Initialize and attach S/W interface
361 */
362 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
363 ifp->if_softc = sc;
364 ifp->if_start = iestart;
365 ifp->if_ioctl = ieioctl;
366 ifp->if_watchdog = iewatchdog;
367 ifp->if_flags =
368 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
369
370 /* Attach the interface. */
371 if_attach(ifp);
372 ether_ifattach(ifp, sc->sc_addr);
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 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 u_short *a = (u_short *) one;
678 u_short *b = (u_short *) two;
679 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
709 ifp = &sc->sc_if;
710
711 #if NBPFILTER > 0
712 *to_bpf = (ifp->if_bpf != 0);
713 #else
714 *to_bpf = 0;
715 #endif
716
717 /*
718 * This is all handled at a higher level now.
719 */
720 return 1;
721 }
722
723 /*
724 * We want to isolate the bits that have meaning... This assumes that
725 * IE_RBUF_SIZE is an even power of two. If somehow the act_len exceeds
726 * the size of the buffer, then we are screwed anyway.
727 */
728 static inline int
729 ie_buflen(sc, head)
730 struct ie_softc *sc;
731 int head;
732 {
733 int len;
734
735 len = SWAP(sc->rbuffs[head]->ie_rbd_actual);
736 len &= (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1));
737 return (len);
738 }
739
740 static inline int
741 ie_packet_len(sc)
742 struct ie_softc *sc;
743 {
744 int i;
745 int head = sc->rbhead;
746 int acc = 0;
747
748 do {
749 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
750 #ifdef IEDEBUG
751 print_rbd(sc->rbuffs[sc->rbhead]);
752 #endif
753 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
754 sc->sc_dev.dv_xname, sc->rbhead);
755 iereset(sc);
756 return -1;
757 }
758
759 i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
760
761 acc += ie_buflen(sc, head);
762 head = (head + 1) % sc->nrxbuf;
763 } while (!i);
764
765 return acc;
766 }
767
768 /*
769 * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
770 * command to the chip to be executed. On the way, if we have a BPF listener
771 * also give him a copy.
772 */
773 static void
774 iexmit(sc)
775 struct ie_softc *sc;
776 {
777 struct ifnet *ifp;
778
779 ifp = &sc->sc_if;
780
781 #ifdef IEDEBUG
782 if (sc->sc_debug & IED_XMIT)
783 printf("%s: xmit buffer %d\n", sc->sc_dev.dv_xname,
784 sc->xctail);
785 #endif
786
787 #if NBPFILTER > 0
788 /*
789 * If BPF is listening on this interface, let it see the packet before
790 * we push it on the wire.
791 */
792 if (ifp->if_bpf)
793 bpf_tap(ifp->if_bpf,
794 sc->xmit_cbuffs[sc->xctail],
795 SWAP(sc->xmit_buffs[sc->xctail]->ie_xmit_flags));
796 #endif
797
798 sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
799 sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
800 sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
801 Swap32(vtop24(sc, sc->xmit_cbuffs[sc->xctail]));
802
803 sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
804 sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
805 IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
806
807 sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
808 sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
809 vtop16sw(sc, (void*) sc->xmit_buffs[sc->xctail]);
810
811 sc->scb->ie_command_list =
812 vtop16sw(sc, (void*) sc->xmit_cmds[sc->xctail]);
813 cmd_and_wait(sc, IE_CU_START, 0, 0);
814
815 ifp->if_timer = 5;
816 }
817
818 /*
819 * Read data off the interface, and turn it into an mbuf chain.
820 *
821 * This code is DRAMATICALLY different from the previous version; this
822 * version tries to allocate the entire mbuf chain up front, given the
823 * length of the data available. This enables us to allocate mbuf
824 * clusters in many situations where before we would have had a long
825 * chain of partially-full mbufs. This should help to speed up the
826 * operation considerably. (Provided that it works, of course.)
827 */
828 static inline struct mbuf *
829 ieget(sc, to_bpf)
830 struct ie_softc *sc;
831 int *to_bpf;
832 {
833 struct mbuf *top, **mp, *m;
834 int len, totlen, resid;
835 int thisrboff, thismboff;
836 int head;
837 struct ether_header eh;
838
839 totlen = ie_packet_len(sc);
840 if (totlen <= 0)
841 return 0;
842
843 head = sc->rbhead;
844
845 /*
846 * Snarf the Ethernet header.
847 */
848 (sc->sc_memcpy)((caddr_t)&eh, (caddr_t)sc->cbuffs[head],
849 sizeof(struct ether_header));
850
851 /*
852 * As quickly as possible, check if this packet is for us.
853 * If not, don't waste a single cycle copying the rest of the
854 * packet in.
855 * This is only a consideration when FILTER is defined; i.e., when
856 * we are either running BPF or doing multicasting.
857 */
858 if (!check_eh(sc, &eh, to_bpf)) {
859 /* just this case, it's not an error */
860 sc->sc_if.if_ierrors--;
861 return 0;
862 }
863
864 resid = totlen;
865
866 MGETHDR(m, M_DONTWAIT, MT_DATA);
867 if (m == 0)
868 return 0;
869
870 m->m_pkthdr.rcvif = &sc->sc_if;
871 m->m_pkthdr.len = totlen;
872 len = MHLEN;
873 top = 0;
874 mp = ⊤
875
876 /*
877 * This loop goes through and allocates mbufs for all the data we will
878 * be copying in. It does not actually do the copying yet.
879 */
880 while (totlen > 0) {
881 if (top) {
882 MGET(m, M_DONTWAIT, MT_DATA);
883 if (m == 0) {
884 m_freem(top);
885 return 0;
886 }
887 len = MLEN;
888 }
889 if (totlen >= MINCLSIZE) {
890 MCLGET(m, M_DONTWAIT);
891 if (m->m_flags & M_EXT)
892 len = MCLBYTES;
893 }
894
895 if (mp == &top) {
896 caddr_t newdata = (caddr_t)
897 ALIGN(m->m_data + sizeof(struct ether_header)) -
898 sizeof(struct ether_header);
899 len -= newdata - m->m_data;
900 m->m_data = newdata;
901 }
902
903 m->m_len = len = min(totlen, len);
904
905 totlen -= len;
906 *mp = m;
907 mp = &m->m_next;
908 }
909
910 m = top;
911 thismboff = 0;
912
913 /*
914 * Copy the Ethernet header into the mbuf chain.
915 */
916 memcpy(mtod(m, caddr_t), &eh, sizeof(struct ether_header));
917 thismboff = sizeof(struct ether_header);
918 thisrboff = sizeof(struct ether_header);
919 resid -= sizeof(struct ether_header);
920
921 /*
922 * Now we take the mbuf chain (hopefully only one mbuf most of the
923 * time) and stuff the data into it. There are no possible failures
924 * at or after this point.
925 */
926 while (resid > 0) {
927 int thisrblen = ie_buflen(sc, head) - thisrboff;
928 int thismblen = m->m_len - thismboff;
929
930 len = min(thisrblen, thismblen);
931 (sc->sc_memcpy)(mtod(m, caddr_t) + thismboff,
932 (caddr_t)(sc->cbuffs[head] + thisrboff),
933 (u_int)len);
934 resid -= len;
935
936 if (len == thismblen) {
937 m = m->m_next;
938 thismboff = 0;
939 } else
940 thismboff += len;
941
942 if (len == thisrblen) {
943 head = (head + 1) % sc->nrxbuf;
944 thisrboff = 0;
945 } else
946 thisrboff += len;
947 }
948
949 /*
950 * Unless something changed strangely while we were doing the copy,
951 * we have now copied everything in from the shared memory.
952 * This means that we are done.
953 */
954 return top;
955 }
956
957 /*
958 * Read frame NUM from unit UNIT (pre-cached as IE).
959 *
960 * This routine reads the RFD at NUM, and copies in the buffers from
961 * the list of RBD, then rotates the RBD and RFD lists so that the receiver
962 * doesn't start complaining. Trailers are DROPPED---there's no point
963 * in wasting time on confusing code to deal with them. Hopefully,
964 * this machine will never ARP for trailers anyway.
965 */
966 static void
967 ie_readframe(sc, num)
968 struct ie_softc *sc;
969 int num; /* frame number to read */
970 {
971 int status;
972 struct mbuf *m = 0;
973 #if NBPFILTER > 0
974 int bpf_gets_it = 0;
975 #endif
976
977 status = sc->rframes[num]->ie_fd_status;
978
979 /* Advance the RFD list, since we're done with this descriptor. */
980 sc->rframes[num]->ie_fd_status = SWAP(0);
981 sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
982 sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
983 sc->rftail = (sc->rftail + 1) % sc->nframes;
984 sc->rfhead = (sc->rfhead + 1) % sc->nframes;
985
986 if (status & IE_FD_OK) {
987 #if NBPFILTER > 0
988 m = ieget(sc, &bpf_gets_it);
989 #else
990 m = ieget(sc, 0);
991 #endif
992 ie_drop_packet_buffer(sc);
993 }
994 if (m == 0) {
995 sc->sc_if.if_ierrors++;
996 return;
997 }
998
999 #ifdef IEDEBUG
1000 if (sc->sc_debug & IED_READFRAME) {
1001 struct ether_header *eh = mtod(m, struct ether_header *);
1002
1003 printf("%s: frame from ether %s type 0x%x\n",
1004 sc->sc_dev.dv_xname,
1005 ether_sprintf(eh->ether_shost), (u_int)eh->ether_type);
1006 }
1007 #endif
1008
1009 #if NBPFILTER > 0
1010 /*
1011 * Check for a BPF filter; if so, hand it up.
1012 * Note that we have to stick an extra mbuf up front, because
1013 * bpf_mtap expects to have the ether header at the front.
1014 * It doesn't matter that this results in an ill-formatted mbuf chain,
1015 * since BPF just looks at the data. (It doesn't try to free the mbuf,
1016 * tho' it will make a copy for tcpdump.)
1017 */
1018 if (bpf_gets_it) {
1019 /* Pass it up. */
1020 bpf_mtap(sc->sc_if.if_bpf, m);
1021
1022 /*
1023 * A signal passed up from the filtering code indicating that
1024 * the packet is intended for BPF but not for the protocol
1025 * machinery. We can save a few cycles by not handing it off
1026 * to them.
1027 */
1028 if (bpf_gets_it == 2) {
1029 m_freem(m);
1030 return;
1031 }
1032 }
1033 #endif /* NBPFILTER > 0 */
1034
1035 /*
1036 * In here there used to be code to check destination addresses upon
1037 * receipt of a packet. We have deleted that code, and replaced it
1038 * with code to check the address much earlier in the cycle, before
1039 * copying the data in; this saves us valuable cycles when operating
1040 * as a multicast router or when using BPF.
1041 */
1042
1043 /*
1044 * Finally pass this packet up to higher layers.
1045 */
1046 (*sc->sc_if.if_input)(&sc->sc_if, m);
1047 sc->sc_if.if_ipackets++;
1048 }
1049
1050 static void
1051 ie_drop_packet_buffer(sc)
1052 struct ie_softc *sc;
1053 {
1054 int i;
1055
1056 do {
1057 /*
1058 * This means we are somehow out of sync. So, we reset the
1059 * adapter.
1060 */
1061 if (!(sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)) {
1062 #ifdef IEDEBUG
1063 print_rbd(sc->rbuffs[sc->rbhead]);
1064 #endif
1065 log(LOG_ERR, "%s: receive descriptors out of sync at %d\n",
1066 sc->sc_dev.dv_xname, sc->rbhead);
1067 iereset(sc);
1068 return;
1069 }
1070
1071 i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
1072
1073 sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
1074 sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
1075 sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
1076 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
1077 sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
1078 } while (!i);
1079 }
1080
1081 /*
1082 * Start transmission on an interface.
1083 */
1084 static void
1085 iestart(ifp)
1086 struct ifnet *ifp;
1087 {
1088 struct ie_softc *sc = ifp->if_softc;
1089 struct mbuf *m0, *m;
1090 u_char *buffer;
1091 u_short len;
1092
1093 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1094 return;
1095
1096 for (;;) {
1097 if (sc->xmit_busy == sc->ntxbuf) {
1098 ifp->if_flags |= IFF_OACTIVE;
1099 break;
1100 }
1101
1102 IF_DEQUEUE(&ifp->if_snd, m0);
1103 if (m0 == 0)
1104 break;
1105
1106 /* We need to use m->m_pkthdr.len, so require the header */
1107 if ((m0->m_flags & M_PKTHDR) == 0)
1108 panic("iestart: no header mbuf");
1109
1110 #if NBPFILTER > 0
1111 /* Tap off here if there is a BPF listener. */
1112 if (ifp->if_bpf)
1113 bpf_mtap(ifp->if_bpf, m0);
1114 #endif
1115
1116 #ifdef IEDEBUG
1117 if (sc->sc_debug & IED_ENQ)
1118 printf("%s: fill buffer %d\n", sc->sc_dev.dv_xname,
1119 sc->xchead);
1120 #endif
1121
1122 buffer = sc->xmit_cbuffs[sc->xchead];
1123 for (m = m0; m != 0; m = m->m_next) {
1124 (sc->sc_memcpy)(buffer, mtod(m, caddr_t), m->m_len);
1125 buffer += m->m_len;
1126 }
1127 if (m0->m_pkthdr.len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1128 sc->sc_memset(buffer, 0,
1129 ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len);
1130 len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1131 } else
1132 len = m0->m_pkthdr.len;
1133
1134 m_freem(m0);
1135 sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
1136
1137 /* Start the first packet transmitting. */
1138 if (sc->xmit_busy == 0)
1139 iexmit(sc);
1140
1141 sc->xchead = (sc->xchead + 1) % sc->ntxbuf;
1142 sc->xmit_busy++;
1143 }
1144 }
1145
1146 static void
1147 iereset(sc)
1148 struct ie_softc *sc;
1149 {
1150 int s = splnet();
1151
1152 /* No message here. The caller does that. */
1153 iestop(sc);
1154
1155 /*
1156 * Stop i82586 dead in its tracks.
1157 */
1158 if (cmd_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1159 printf("%s: abort commands timed out\n", sc->sc_dev.dv_xname);
1160
1161 if (cmd_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1162 printf("%s: disable commands timed out\n", sc->sc_dev.dv_xname);
1163
1164 ieinit(sc);
1165
1166 splx(s);
1167 }
1168
1169 /*
1170 * Send a command to the controller and wait for it to either
1171 * complete or be accepted, depending on the command. If the
1172 * command pointer is null, then pretend that the command is
1173 * not an action command. If the command pointer is not null,
1174 * and the command is an action command, wait for
1175 * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1176 * to become true.
1177 */
1178 static int
1179 cmd_and_wait(sc, cmd, pcmd, mask)
1180 struct ie_softc *sc;
1181 int cmd;
1182 void *pcmd; /* XXX - Was volatile */
1183 int mask;
1184 {
1185 volatile struct ie_cmd_common *cc = pcmd;
1186 volatile struct ie_sys_ctl_block *scb = sc->scb;
1187 int tmo;
1188
1189 scb->ie_command = (u_short)cmd;
1190 (sc->chan_attn)(sc);
1191
1192 /* Wait for the command to be accepted by the CU. */
1193 tmo = 10;
1194 while (scb->ie_command && --tmo)
1195 delay(10);
1196 if (scb->ie_command) {
1197 #ifdef IEDEBUG
1198 printf("%s: cmd_and_wait, CU stuck (1)\n",
1199 sc->sc_dev.dv_xname);
1200 #endif
1201 return -1; /* timed out */
1202 }
1203
1204 /*
1205 * If asked, also wait for it to finish.
1206 */
1207 if (IE_ACTION_COMMAND(cmd) && pcmd) {
1208
1209 /*
1210 * According to the packet driver, the minimum timeout should
1211 * be .369 seconds, which we round up to .4.
1212 */
1213 tmo = 36900;
1214
1215 /*
1216 * Now spin-lock waiting for status. This is not a very nice
1217 * thing to do, but I haven't figured out how, or indeed if, we
1218 * can put the process waiting for action to sleep. (We may
1219 * be getting called through some other timeout running in the
1220 * kernel.)
1221 */
1222 while (((cc->ie_cmd_status & mask) == 0) && --tmo)
1223 delay(10);
1224
1225 if ((cc->ie_cmd_status & mask) == 0) {
1226 #ifdef IEDEBUG
1227 printf("%s: cmd_and_wait, CU stuck (2)\n",
1228 sc->sc_dev.dv_xname);
1229 #endif
1230 return -1; /* timed out */
1231 }
1232 }
1233 return 0;
1234 }
1235
1236 /*
1237 * Run the time-domain reflectometer.
1238 */
1239 static void
1240 run_tdr(sc, cmd)
1241 struct ie_softc *sc;
1242 struct ie_tdr_cmd *cmd;
1243 {
1244 int result;
1245
1246 cmd->com.ie_cmd_status = SWAP(0);
1247 cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1248 cmd->com.ie_cmd_link = SWAP(0xffff);
1249
1250 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1251 cmd->ie_tdr_time = SWAP(0);
1252
1253 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1254 !(cmd->com.ie_cmd_status & IE_STAT_OK))
1255 result = 0x10000; /* impossible value */
1256 else
1257 result = cmd->ie_tdr_time;
1258
1259 ie_ack(sc, IE_ST_WHENCE);
1260
1261 if (result & IE_TDR_SUCCESS)
1262 return;
1263
1264 if (result & 0x10000) {
1265 printf("%s: TDR command failed\n", sc->sc_dev.dv_xname);
1266 } else if (result & IE_TDR_XCVR) {
1267 printf("%s: transceiver problem\n", sc->sc_dev.dv_xname);
1268 } else if (result & IE_TDR_OPEN) {
1269 printf("%s: TDR detected an open %d clocks away\n",
1270 sc->sc_dev.dv_xname, SWAP(result & IE_TDR_TIME));
1271 } else if (result & IE_TDR_SHORT) {
1272 printf("%s: TDR detected a short %d clocks away\n",
1273 sc->sc_dev.dv_xname, SWAP(result & IE_TDR_TIME));
1274 } else {
1275 printf("%s: TDR returned unknown status 0x%x\n",
1276 sc->sc_dev.dv_xname, result);
1277 }
1278 }
1279
1280 /*
1281 * iememinit: set up the buffers
1282 *
1283 * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1284 * this is to be used for the buffers. the chip indexs its control data
1285 * structures with 16 bit offsets, and it indexes actual buffers with
1286 * 24 bit addresses. so we should allocate control buffers first so that
1287 * we don't overflow the 16 bit offset field. The number of transmit
1288 * buffers is fixed at compile time.
1289 *
1290 * note: this function was written to be easy to understand, rather than
1291 * highly efficient (it isn't in the critical path).
1292 *
1293 * The memory layout is: tbufs, rbufs, (gap), control blocks
1294 * [tbuf0, tbuf1] [rbuf0,...rbufN] gap [rframes] [tframes]
1295 * XXX - This needs review...
1296 */
1297 static void
1298 iememinit(sc)
1299 struct ie_softc *sc;
1300 {
1301 char *ptr;
1302 int i;
1303 u_short nxt;
1304
1305 /* First, zero all the memory. */
1306 ptr = sc->buf_area;
1307 (sc->sc_memset)(ptr, 0, sc->buf_area_sz);
1308
1309 /* Allocate tx/rx buffers. */
1310 for (i = 0; i < NTXBUF; i++) {
1311 sc->xmit_cbuffs[i] = ptr;
1312 ptr += IE_TBUF_SIZE;
1313 }
1314 for (i = 0; i < sc->nrxbuf; i++) {
1315 sc->cbuffs[i] = ptr;
1316 ptr += IE_RBUF_SIZE;
1317 }
1318
1319 /* Small pad (Don't trust the chip...) */
1320 ptr += 16;
1321
1322 /* Allocate and fill in xmit buffer descriptors. */
1323 for (i = 0; i < NTXBUF; i++) {
1324 sc->xmit_buffs[i] = (volatile void *) ptr;
1325 ptr = Align(ptr + sizeof(*sc->xmit_buffs[i]));
1326 sc->xmit_buffs[i]->ie_xmit_buf =
1327 Swap32(vtop24(sc, sc->xmit_cbuffs[i]));
1328 sc->xmit_buffs[i]->ie_xmit_next = SWAP(0xffff);
1329 }
1330
1331 /* Allocate and fill in recv buffer descriptors. */
1332 for (i = 0; i < sc->nrxbuf; i++) {
1333 sc->rbuffs[i] = (volatile void *) ptr;
1334 ptr = Align(ptr + sizeof(*sc->rbuffs[i]));
1335 sc->rbuffs[i]->ie_rbd_buffer =
1336 Swap32(vtop24(sc, sc->cbuffs[i]));
1337 sc->rbuffs[i]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
1338 }
1339
1340 /* link together recv bufs and set EOL on last */
1341 i = sc->nrxbuf - 1;
1342 sc->rbuffs[i]->ie_rbd_length |= IE_RBD_LAST;
1343 nxt = vtop16sw(sc, (void*) sc->rbuffs[0]);
1344 do {
1345 sc->rbuffs[i]->ie_rbd_next = nxt;
1346 nxt = vtop16sw(sc, (void*) sc->rbuffs[i]);
1347 } while (--i >= 0);
1348
1349 /* Allocate transmit commands. */
1350 for (i = 0; i < NTXBUF; i++) {
1351 sc->xmit_cmds[i] = (volatile void *) ptr;
1352 ptr = Align(ptr + sizeof(*sc->xmit_cmds[i]));
1353 sc->xmit_cmds[i]->com.ie_cmd_link = SWAP(0xffff);
1354 }
1355
1356 /* Allocate receive frames. */
1357 for (i = 0; i < sc->nframes; i++) {
1358 sc->rframes[i] = (volatile void *) ptr;
1359 ptr = Align(ptr + sizeof(*sc->rframes[i]));
1360 }
1361
1362 /* Link together recv frames and set EOL on last */
1363 i = sc->nframes - 1;
1364 sc->rframes[i]->ie_fd_last |= IE_FD_LAST;
1365 nxt = vtop16sw(sc, (void*) sc->rframes[0]);
1366 do {
1367 sc->rframes[i]->ie_fd_next = nxt;
1368 nxt = vtop16sw(sc, (void*) sc->rframes[i]);
1369 } while (--i >= 0);
1370
1371
1372 /* Pointers to last packet sent and next available transmit buffer. */
1373 sc->xchead = sc->xctail = 0;
1374
1375 /* Clear transmit-busy flag. */
1376 sc->xmit_busy = 0;
1377
1378 /*
1379 * Set the head and tail pointers on receive to keep track of
1380 * the order in which RFDs and RBDs are used. link the
1381 * recv frames and buffer into the scb.
1382 */
1383 sc->rfhead = 0;
1384 sc->rftail = sc->nframes - 1;
1385 sc->rbhead = 0;
1386 sc->rbtail = sc->nrxbuf - 1;
1387
1388 sc->scb->ie_recv_list =
1389 vtop16sw(sc, (void*) sc->rframes[0]);
1390 sc->rframes[0]->ie_fd_buf_desc =
1391 vtop16sw(sc, (void*) sc->rbuffs[0]);
1392
1393 i = (ptr - sc->buf_area);
1394 #ifdef IEDEBUG
1395 printf("IE_DEBUG: used %d of %d bytes\n", i, sc->buf_area_sz);
1396 #endif
1397 if (i > sc->buf_area_sz)
1398 panic("ie: iememinit, out of space");
1399 }
1400
1401 /*
1402 * Run the multicast setup command.
1403 * Called at splnet().
1404 */
1405 static int
1406 mc_setup(sc, ptr)
1407 struct ie_softc *sc;
1408 void *ptr;
1409 {
1410 struct ie_mcast_cmd *cmd = ptr; /* XXX - Was volatile */
1411
1412 cmd->com.ie_cmd_status = SWAP(0);
1413 cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1414 cmd->com.ie_cmd_link = SWAP(0xffff);
1415
1416 (sc->sc_memcpy)((caddr_t)cmd->ie_mcast_addrs,
1417 (caddr_t)sc->mcast_addrs,
1418 sc->mcast_count * sizeof *sc->mcast_addrs);
1419
1420 cmd->ie_mcast_bytes =
1421 SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
1422
1423 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1424 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1425 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1426 printf("%s: multicast address setup command failed\n",
1427 sc->sc_dev.dv_xname);
1428 return 0;
1429 }
1430 return 1;
1431 }
1432
1433 static inline void
1434 ie_setup_config(cmd, promiscuous, manchester)
1435 struct ie_config_cmd *cmd; /* XXX - was volatile */
1436 int promiscuous, manchester;
1437 {
1438
1439 /*
1440 * these are all char's so no need to byte-swap
1441 */
1442 cmd->ie_config_count = 0x0c;
1443 cmd->ie_fifo = 8;
1444 cmd->ie_save_bad = 0x40;
1445 cmd->ie_addr_len = 0x2e;
1446 cmd->ie_priority = 0;
1447 cmd->ie_ifs = 0x60;
1448 cmd->ie_slot_low = 0;
1449 cmd->ie_slot_high = 0xf2;
1450 cmd->ie_promisc = promiscuous | manchester << 2;
1451 cmd->ie_crs_cdt = 0;
1452 cmd->ie_min_len = 64;
1453 cmd->ie_junk = 0xff;
1454 }
1455
1456 /*
1457 * This routine inits the ie.
1458 * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1459 * starting the receiver unit, and clearing interrupts.
1460 *
1461 * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1462 */
1463 static int
1464 ieinit(sc)
1465 struct ie_softc *sc;
1466 {
1467 volatile struct ie_sys_ctl_block *scb = sc->scb;
1468 void *ptr;
1469 struct ifnet *ifp;
1470
1471 ifp = &sc->sc_if;
1472 ptr = sc->buf_area; /* XXX - Use scb instead? */
1473
1474 /*
1475 * Send the configure command first.
1476 */
1477 {
1478 struct ie_config_cmd *cmd = ptr; /* XXX - Was volatile */
1479
1480 scb->ie_command_list = vtop16sw(sc, cmd);
1481 cmd->com.ie_cmd_status = SWAP(0);
1482 cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1483 cmd->com.ie_cmd_link = SWAP(0xffff);
1484
1485 ie_setup_config(cmd, (sc->promisc != 0), 0);
1486
1487 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1488 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1489 printf("%s: configure command failed\n",
1490 sc->sc_dev.dv_xname);
1491 return 0;
1492 }
1493 }
1494
1495 /*
1496 * Now send the Individual Address Setup command.
1497 */
1498 {
1499 struct ie_iasetup_cmd *cmd = ptr; /* XXX - Was volatile */
1500
1501 scb->ie_command_list = vtop16sw(sc, cmd);
1502 cmd->com.ie_cmd_status = SWAP(0);
1503 cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1504 cmd->com.ie_cmd_link = SWAP(0xffff);
1505
1506 (sc->sc_memcpy)((caddr_t)&cmd->ie_address,
1507 LLADDR(ifp->if_sadl), sizeof(cmd->ie_address));
1508
1509 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1510 !(cmd->com.ie_cmd_status & IE_STAT_OK)) {
1511 printf("%s: individual address setup command failed\n",
1512 sc->sc_dev.dv_xname);
1513 return 0;
1514 }
1515 }
1516
1517 /*
1518 * Now run the time-domain reflectometer.
1519 */
1520 if (ie_run_tdr)
1521 run_tdr(sc, ptr);
1522
1523 /*
1524 * Acknowledge any interrupts we have generated thus far.
1525 */
1526 ie_ack(sc, IE_ST_WHENCE);
1527
1528 /*
1529 * Set up the transmit and recv buffers.
1530 */
1531 iememinit(sc);
1532
1533 /* tell higher levels that we are here */
1534 ifp->if_flags |= IFF_RUNNING;
1535 ifp->if_flags &= ~IFF_OACTIVE;
1536
1537 sc->scb->ie_recv_list =
1538 vtop16sw(sc, (void*) sc->rframes[0]);
1539 cmd_and_wait(sc, IE_RU_START, 0, 0);
1540
1541 ie_ack(sc, IE_ST_WHENCE);
1542
1543 if (sc->run_586)
1544 (sc->run_586)(sc);
1545
1546 return 0;
1547 }
1548
1549 static void
1550 iestop(sc)
1551 struct ie_softc *sc;
1552 {
1553
1554 cmd_and_wait(sc, IE_RU_DISABLE, 0, 0);
1555 }
1556
1557 static int
1558 ieioctl(ifp, cmd, data)
1559 struct ifnet *ifp;
1560 u_long cmd;
1561 caddr_t data;
1562 {
1563 struct ie_softc *sc = ifp->if_softc;
1564 struct ifaddr *ifa = (struct ifaddr *)data;
1565 struct ifreq *ifr = (struct ifreq *)data;
1566 int s, error = 0;
1567
1568 s = splnet();
1569
1570 switch (cmd) {
1571
1572 case SIOCSIFADDR:
1573 ifp->if_flags |= IFF_UP;
1574
1575 switch (ifa->ifa_addr->sa_family) {
1576 #ifdef INET
1577 case AF_INET:
1578 ieinit(sc);
1579 arp_ifinit(ifp, ifa);
1580 break;
1581 #endif
1582 #ifdef NS
1583 /* XXX - This code is probably wrong. */
1584 case AF_NS:
1585 {
1586 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1587
1588 if (ns_nullhost(*ina))
1589 ina->x_host =
1590 *(union ns_host *)LLADDR(ifp->if_sadl);
1591 else
1592 memcpy(LLADDR(ifp->if_sadl),
1593 ina->x_host.c_host, ETHER_ADDR_LEN);
1594 /* Set new address. */
1595 ieinit(sc);
1596 break;
1597 }
1598 #endif /* NS */
1599 default:
1600 ieinit(sc);
1601 break;
1602 }
1603 break;
1604
1605 case SIOCSIFFLAGS:
1606 sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1607
1608 if ((ifp->if_flags & IFF_UP) == 0 &&
1609 (ifp->if_flags & IFF_RUNNING) != 0) {
1610 /*
1611 * If interface is marked down and it is running, then
1612 * stop it.
1613 */
1614 iestop(sc);
1615 ifp->if_flags &= ~IFF_RUNNING;
1616 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1617 (ifp->if_flags & IFF_RUNNING) == 0) {
1618 /*
1619 * If interface is marked up and it is stopped, then
1620 * start it.
1621 */
1622 ieinit(sc);
1623 } else {
1624 /*
1625 * Reset the interface to pick up changes in any other
1626 * flags that affect hardware registers.
1627 */
1628 iestop(sc);
1629 ieinit(sc);
1630 }
1631 #ifdef IEDEBUG
1632 if (ifp->if_flags & IFF_DEBUG)
1633 sc->sc_debug = IED_ALL;
1634 else
1635 sc->sc_debug = ie_debug_flags;
1636 #endif
1637 break;
1638
1639 case SIOCADDMULTI:
1640 case SIOCDELMULTI:
1641 error = (cmd == SIOCADDMULTI) ?
1642 ether_addmulti(ifr, &sc->sc_ethercom) :
1643 ether_delmulti(ifr, &sc->sc_ethercom);
1644
1645 if (error == ENETRESET) {
1646 /*
1647 * Multicast list has changed; set the hardware filter
1648 * accordingly.
1649 */
1650 if (ifp->if_flags & IFF_RUNNING)
1651 mc_reset(sc);
1652 error = 0;
1653 }
1654 break;
1655
1656 default:
1657 error = EINVAL;
1658 }
1659 splx(s);
1660 return error;
1661 }
1662
1663 static void
1664 mc_reset(sc)
1665 struct ie_softc *sc;
1666 {
1667 struct ether_multi *enm;
1668 struct ether_multistep step;
1669 struct ifnet *ifp;
1670
1671 ifp = &sc->sc_if;
1672
1673 /*
1674 * Step through the list of addresses.
1675 */
1676 sc->mcast_count = 0;
1677 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1678 while (enm) {
1679 if (sc->mcast_count >= MAXMCAST ||
1680 ether_cmp(enm->enm_addrlo, enm->enm_addrhi) != 0) {
1681 ifp->if_flags |= IFF_ALLMULTI;
1682 ieioctl(ifp, SIOCSIFFLAGS, (void *)0);
1683 goto setflag;
1684 }
1685 memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo,
1686 ETHER_ADDR_LEN);
1687 sc->mcast_count++;
1688 ETHER_NEXT_MULTI(step, enm);
1689 }
1690 setflag:
1691 sc->want_mcsetup = 1;
1692 }
1693
1694 #ifdef IEDEBUG
1695 void
1696 print_rbd(rbd)
1697 volatile struct ie_recv_buf_desc *rbd;
1698 {
1699
1700 printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
1701 "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
1702 rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
1703 rbd->mbz);
1704 }
1705 #endif
1706