No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / sun3 / dev / if_ie.c
blobcec04b986f6e50053fc6f956cac8e0b400aeed5d
1 /* $NetBSD: if_ie.c,v 1.52 2008/11/07 00:20:02 dyoung Exp $ */
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.
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
14 * All rights reserved.
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.
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.
48 * Intel 82586 Ethernet chip
49 * Register, bit, and structure definitions.
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.
54 * BPF support code taken from hpdev/if_le.c, supplied with tcpdump.
56 * 3C507 support is loosely based on code donated to NetBSD by Rafal Boni.
58 * Majorly cleaned up and 3C507 code merged by Charles Hannum.
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 ]
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.
76 Mode of operation:
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.
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.
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.
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: if_ie.c,v 1.52 2008/11/07 00:20:02 dyoung Exp $");
103 #include "opt_inet.h"
104 #include "opt_ns.h"
105 #include "bpfilter.h"
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>
118 #include <net/if.h>
119 #include <net/if_types.h>
120 #include <net/if_dl.h>
121 #include <net/if_ether.h>
123 #if NBPFILTER > 0
124 #include <net/bpf.h>
125 #include <net/bpfdesc.h>
126 #endif
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
136 #ifdef NS
137 #include <netns/ns.h>
138 #include <netns/ns_if.h>
139 #endif
141 #include <uvm/uvm_extern.h>
143 #include <machine/autoconf.h>
144 #include <machine/cpu.h>
145 #include <machine/pmap.h>
148 * ugly byte-order hack for SUNs
151 #define XSWAP(y) ( (((y) & 0xff00) >> 8) | (((y) & 0xff) << 8) )
152 #define SWAP(x) ((u_short)(XSWAP((u_short)(x))))
154 #include "i82586.h"
155 #include "if_iereg.h"
156 #include "if_ievar.h"
158 /* #define IEDEBUG XXX */
161 * IED: ie debug flags
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
173 #ifdef IEDEBUG
174 #define inline /* not */
175 void print_rbd(volatile struct ie_recv_buf_desc *);
176 int in_ierint = 0;
177 int in_ietint = 0;
178 int ie_debug_flags = 0;
179 #endif
181 /* XXX - Skip TDR for now - it always complains... */
182 int ie_run_tdr = 0;
184 static void iewatchdog(struct ifnet *);
185 static int ieinit(struct ie_softc *);
186 static int ieioctl(struct ifnet *, u_long, void *);
187 static void iestart(struct ifnet *);
188 static void iereset(struct ie_softc *);
189 static int ie_setupram(struct ie_softc *);
191 static int cmd_and_wait(struct ie_softc *, int, void *, int);
193 static void ie_drop_packet_buffer(struct ie_softc *);
194 static void ie_readframe(struct ie_softc *, int);
195 static inline void ie_setup_config(struct ie_config_cmd *, int, int);
197 static void ierint(struct ie_softc *);
198 static void iestop(struct ie_softc *);
199 static void ietint(struct ie_softc *);
200 static void iexmit(struct ie_softc *);
202 static int mc_setup(struct ie_softc *, void *);
203 static void mc_reset(struct ie_softc *);
204 static void run_tdr(struct ie_softc *, struct ie_tdr_cmd *);
205 static void iememinit(struct ie_softc *);
207 static inline uint8_t *Align(char *);
208 static inline u_int Swap32(u_int);
209 static inline u_int vtop24(struct ie_softc *, void *);
210 static inline uint16_t vtop16sw(struct ie_softc *, void *);
212 static inline void ie_ack(struct ie_softc *, u_int);
213 static inline u_short ether_cmp(u_char *, uint8_t *);
214 static inline int check_eh(struct ie_softc *, struct ether_header *, int *);
215 static inline int ie_buflen(struct ie_softc *, int);
216 static inline int ie_packet_len(struct ie_softc *);
217 static inline struct mbuf * ieget(struct ie_softc *, int *);
221 * Here are a few useful functions. We could have done these as macros,
222 * but since we have the inline facility, it makes sense to use that
223 * instead.
226 /* KVA to 24 bit device address */
227 static inline u_int
228 vtop24(struct ie_softc *sc, void *ptr)
230 u_int pa;
232 pa = (vaddr_t)ptr - (vaddr_t)sc->sc_iobase;
233 #ifdef IEDEBUG
234 if (pa & ~0xffFFff)
235 panic("ie:vtop24");
236 #endif
237 return pa;
240 /* KVA to 16 bit offset, swapped */
241 static inline u_short
242 vtop16sw(struct ie_softc *sc, void *ptr)
244 u_int pa;
246 pa = (vaddr_t)ptr - (vaddr_t)sc->sc_maddr;
247 #ifdef IEDEBUG
248 if (pa & ~0xFFff)
249 panic("ie:vtop16");
250 #endif
252 return SWAP(pa);
255 static inline u_int
256 Swap32(u_int x)
258 u_int y;
260 y = x & 0xFF;
261 y <<= 8; x >>= 8;
262 y |= x & 0xFF;
263 y <<= 8; x >>= 8;
264 y |= x & 0xFF;
265 y <<= 8; x >>= 8;
266 y |= x & 0xFF;
268 return y;
271 static inline uint8_t *
272 Align(char *ptr)
274 u_long l = (u_long)ptr;
276 l = (l + 3) & ~3L;
277 return (uint8_t *)l;
281 static inline void
282 ie_ack(struct ie_softc *sc, u_int mask)
284 volatile struct ie_sys_ctl_block *scb = sc->scb;
286 cmd_and_wait(sc, scb->ie_status & mask, 0, 0);
291 * Taken almost exactly from Bill's if_is.c,
292 * then modified beyond recognition...
294 void
295 ie_attach(struct ie_softc *sc)
297 struct ifnet *ifp = &sc->sc_if;
299 /* MD code has done its part before calling this. */
300 printf(": macaddr %s\n", ether_sprintf(sc->sc_addr));
303 * Compute number of transmit and receive buffers.
304 * Tx buffers take 1536 bytes, and fixed in number.
305 * Rx buffers are 512 bytes each, variable number.
306 * Need at least 1 frame for each 3 rx buffers.
307 * The ratio 3bufs:2frames is a compromise.
309 sc->ntxbuf = NTXBUF; /* XXX - Fix me... */
310 switch (sc->sc_msize) {
311 case 16384:
312 sc->nframes = 8 * 4;
313 sc->nrxbuf = 8 * 6;
314 break;
315 case 32768:
316 sc->nframes = 16 * 4;
317 sc->nrxbuf = 16 * 6;
318 break;
319 case 65536:
320 sc->nframes = 32 * 4;
321 sc->nrxbuf = 32 * 6;
322 break;
323 default:
324 sc->nframes = 0;
326 if (sc->nframes > MXFRAMES)
327 sc->nframes = MXFRAMES;
328 if (sc->nrxbuf > MXRXBUF)
329 sc->nrxbuf = MXRXBUF;
331 #ifdef IEDEBUG
332 aprint_debug_dev(sc->sc_dev,
333 "%dK memory, %d tx frames, %d rx frames, %d rx bufs\n",
334 (sc->sc_msize >> 10), sc->ntxbuf, sc->nframes, sc->nrxbuf);
335 #endif
337 if ((sc->nframes <= 0) || (sc->nrxbuf <= 0))
338 panic("%s: weird memory size", __func__);
341 * Setup RAM for transmit/receive
343 if (ie_setupram(sc) == 0) {
344 aprint_error(": RAM CONFIG FAILED!\n");
345 /* XXX should reclaim resources? */
346 return;
350 * Initialize and attach S/W interface
352 strcpy(ifp->if_xname, device_xname(sc->sc_dev));
353 ifp->if_softc = sc;
354 ifp->if_start = iestart;
355 ifp->if_ioctl = ieioctl;
356 ifp->if_watchdog = iewatchdog;
357 ifp->if_flags =
358 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
360 /* Attach the interface. */
361 if_attach(ifp);
362 ether_ifattach(ifp, sc->sc_addr);
366 * Setup IE's ram space.
368 static int
369 ie_setupram(struct ie_softc *sc)
371 volatile struct ie_sys_conf_ptr *scp;
372 volatile struct ie_int_sys_conf_ptr *iscp;
373 volatile struct ie_sys_ctl_block *scb;
374 int off;
377 * Allocate from end of buffer space for
378 * ISCP, SCB, and other small stuff.
380 off = sc->buf_area_sz;
381 off &= ~3;
383 /* SCP (address already chosen). */
384 scp = sc->scp;
385 (sc->sc_memset)(__UNVOLATILE(scp), 0, sizeof(*scp));
387 /* ISCP */
388 off -= sizeof(*iscp);
389 iscp = (volatile void *)(sc->buf_area + off);
390 (sc->sc_memset)(__UNVOLATILE(iscp), 0, sizeof(*iscp));
391 sc->iscp = iscp;
393 /* SCB */
394 off -= sizeof(*scb);
395 scb = (volatile void *)(sc->buf_area + off);
396 (sc->sc_memset)(__UNVOLATILE(scb), 0, sizeof(*scb));
397 sc->scb = scb;
399 /* Remainder is for buffers, etc. */
400 sc->buf_area_sz = off;
403 * Now fill in the structures we just allocated.
406 /* SCP: main thing is 24-bit ptr to ISCP */
407 scp->ie_bus_use = 0; /* 16-bit */
408 scp->ie_iscp_ptr = Swap32(vtop24(sc, __UNVOLATILE(iscp)));
410 /* ISCP */
411 iscp->ie_busy = 1; /* ie_busy == char */
412 iscp->ie_scb_offset = vtop16sw(sc, __UNVOLATILE(scb));
413 iscp->ie_base = Swap32(vtop24(sc, sc->sc_maddr));
415 /* SCB */
416 scb->ie_command_list = SWAP(0xffff);
417 scb->ie_recv_list = SWAP(0xffff);
419 /* Other stuff is done in ieinit() */
420 (sc->reset_586)(sc);
421 (sc->chan_attn)(sc);
423 delay(100); /* wait a while... */
425 if (iscp->ie_busy) {
426 return 0;
429 * Acknowledge any interrupts we may have caused...
431 ie_ack(sc, IE_ST_WHENCE);
433 return 1;
437 * Device timeout/watchdog routine. Entered if the device neglects to
438 * generate an interrupt after a transmit has been started on it.
440 static void
441 iewatchdog(struct ifnet *ifp)
443 struct ie_softc *sc = ifp->if_softc;
445 log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
446 ++ifp->if_oerrors;
447 iereset(sc);
451 * What to do upon receipt of an interrupt.
453 int
454 ie_intr(void *arg)
456 struct ie_softc *sc = arg;
457 uint16_t status;
458 int loopcnt;
461 * check for parity error
463 if (sc->hard_type == IE_VME) {
464 volatile struct ievme *iev =
465 (volatile struct ievme *)sc->sc_reg;
467 if (iev->status & IEVME_PERR) {
468 printf("%s: parity error (ctrl 0x%x @ 0x%02x%04x)\n",
469 device_xname(sc->sc_dev), iev->pectrl,
470 iev->pectrl & IEVME_HADDR, iev->peaddr);
471 iev->pectrl = iev->pectrl | IEVME_PARACK;
475 status = sc->scb->ie_status;
476 if ((status & IE_ST_WHENCE) == 0)
477 return 0;
479 loopcnt = sc->nframes;
480 loop:
481 /* Ack interrupts FIRST in case we receive more during the ISR. */
482 ie_ack(sc, IE_ST_WHENCE & status);
484 if (status & (IE_ST_RECV | IE_ST_RNR)) {
485 #ifdef IEDEBUG
486 in_ierint++;
487 if (sc->sc_debug & IED_RINT)
488 printf("%s: rint\n", device_xname(sc->sc_dev));
489 #endif
490 ierint(sc);
491 #ifdef IEDEBUG
492 in_ierint--;
493 #endif
496 if (status & IE_ST_DONE) {
497 #ifdef IEDEBUG
498 in_ietint++;
499 if (sc->sc_debug & IED_TINT)
500 printf("%s: tint\n", device_xname(sc->sc_dev));
501 #endif
502 ietint(sc);
503 #ifdef IEDEBUG
504 in_ietint--;
505 #endif
509 * Receiver not ready (RNR) just means it has
510 * run out of resources (buffers or frames).
511 * One can easily cause this with (i.e.) spray.
512 * This is not a serious error, so be silent.
514 if (status & IE_ST_RNR) {
515 #ifdef IEDEBUG
516 printf("%s: receiver not ready\n", device_xname(sc->sc_dev));
517 #endif
518 sc->sc_if.if_ierrors++;
519 iereset(sc);
522 #ifdef IEDEBUG
523 if ((status & IE_ST_ALLDONE) && (sc->sc_debug & IED_CNA))
524 printf("%s: cna\n", device_xname(sc->sc_dev));
525 #endif
527 status = sc->scb->ie_status;
528 if (status & IE_ST_WHENCE) {
529 /* It still wants service... */
530 if (--loopcnt > 0)
531 goto loop;
532 /* ... but we've been here long enough. */
533 log(LOG_ERR, "%s: interrupt stuck?\n",
534 device_xname(sc->sc_dev));
535 iereset(sc);
537 return 1;
541 * Process a received-frame interrupt.
543 void
544 ierint(struct ie_softc *sc)
546 volatile struct ie_sys_ctl_block *scb = sc->scb;
547 int i, status;
548 static int timesthru = 1024;
550 i = sc->rfhead;
551 for (;;) {
552 status = sc->rframes[i]->ie_fd_status;
554 if ((status & IE_FD_COMPLETE) && (status & IE_FD_OK)) {
555 if (!--timesthru) {
556 sc->sc_if.if_ierrors +=
557 SWAP(scb->ie_err_crc) +
558 SWAP(scb->ie_err_align) +
559 SWAP(scb->ie_err_resource) +
560 SWAP(scb->ie_err_overrun);
561 scb->ie_err_crc = 0;
562 scb->ie_err_align = 0;
563 scb->ie_err_resource = 0;
564 scb->ie_err_overrun = 0;
565 timesthru = 1024;
567 ie_readframe(sc, i);
568 } else {
569 if ((status & IE_FD_RNR) != 0 &&
570 (scb->ie_status & IE_RU_READY) == 0) {
571 sc->rframes[0]->ie_fd_buf_desc = vtop16sw(sc,
572 __UNVOLATILE(sc->rbuffs[0]));
573 scb->ie_recv_list = vtop16sw(sc,
574 __UNVOLATILE(sc->rframes[0]));
575 cmd_and_wait(sc, IE_RU_START, 0, 0);
577 break;
579 i = (i + 1) % sc->nframes;
584 * Process a command-complete interrupt. These are only generated by the
585 * transmission of frames. This routine is deceptively simple, since most
586 * of the real work is done by iestart().
588 void
589 ietint(struct ie_softc *sc)
591 struct ifnet *ifp;
592 int status;
594 ifp = &sc->sc_if;
596 ifp->if_timer = 0;
597 ifp->if_flags &= ~IFF_OACTIVE;
599 status = sc->xmit_cmds[sc->xctail]->ie_xmit_status;
601 if (!(status & IE_STAT_COMPL) || (status & IE_STAT_BUSY))
602 printf("%s: command still busy!\n", __func__);
604 if (status & IE_STAT_OK) {
605 ifp->if_opackets++;
606 ifp->if_collisions +=
607 SWAP(status & IE_XS_MAXCOLL);
608 } else {
609 ifp->if_oerrors++;
611 * XXX
612 * Check SQE and DEFERRED?
613 * What if more than one bit is set?
615 if (status & IE_STAT_ABORT)
616 printf("%s: send aborted\n", device_xname(sc->sc_dev));
617 if (status & IE_XS_LATECOLL)
618 printf("%s: late collision\n",
619 device_xname(sc->sc_dev));
620 if (status & IE_XS_NOCARRIER)
621 printf("%s: no carrier\n", device_xname(sc->sc_dev));
622 if (status & IE_XS_LOSTCTS)
623 printf("%s: lost CTS\n", device_xname(sc->sc_dev));
624 if (status & IE_XS_UNDERRUN)
625 printf("%s: DMA underrun\n", device_xname(sc->sc_dev));
626 if (status & IE_XS_EXCMAX) {
627 /* Do not print this one (too noisy). */
628 ifp->if_collisions += 16;
633 * If multicast addresses were added or deleted while we
634 * were transmitting, mc_reset() set the want_mcsetup flag
635 * indicating that we should do it.
637 if (sc->want_mcsetup) {
638 mc_setup(sc, (void *)sc->xmit_cbuffs[sc->xctail]);
639 sc->want_mcsetup = 0;
642 /* Done with the buffer. */
643 sc->xmit_busy--;
644 sc->xctail = (sc->xctail + 1) % NTXBUF;
646 /* Start the next packet, if any, transmitting. */
647 if (sc->xmit_busy > 0)
648 iexmit(sc);
650 iestart(ifp);
654 * Compare two Ether/802 addresses for equality, inlined and
655 * unrolled for speed. I'd love to have an inline assembler
656 * version of this... XXX: Who wanted that? mycroft?
657 * I wrote one, but the following is just as efficient.
658 * This expands to 10 short m68k instructions! -gwr
659 * Note: use this like memcmp()
661 static inline uint16_t
662 ether_cmp(uint8_t *one, uint8_t *two)
664 uint16_t *a = (uint16_t *)one;
665 uint16_t *b = (uint16_t *)two;
666 uint16_t diff;
668 diff = *a++ - *b++;
669 diff |= *a++ - *b++;
670 diff |= *a++ - *b++;
672 return diff;
674 #define ether_equal !ether_cmp
677 * Check for a valid address. to_bpf is filled in with one of the following:
678 * 0 -> BPF doesn't get this packet
679 * 1 -> BPF does get this packet
680 * 2 -> BPF does get this packet, but we don't
681 * Return value is true if the packet is for us, and false otherwise.
683 * This routine is a mess, but it's also critical that it be as fast
684 * as possible. It could be made cleaner if we can assume that the
685 * only client which will fiddle with IFF_PROMISC is BPF. This is
686 * probably a good assumption, but we do not make it here. (Yet.)
688 static inline int
689 check_eh(struct ie_softc *sc, struct ether_header *eh, int *to_bpf)
691 #if NBPFILTER > 0
692 struct ifnet *ifp;
694 ifp = &sc->sc_if;
695 *to_bpf = (ifp->if_bpf != 0);
696 #endif
699 * This is all handled at a higher level now.
701 return 1;
705 * We want to isolate the bits that have meaning... This assumes that
706 * IE_RBUF_SIZE is an even power of two. If somehow the act_len exceeds
707 * the size of the buffer, then we are screwed anyway.
709 static inline int
710 ie_buflen(struct ie_softc *sc, int head)
712 int len;
714 len = SWAP(sc->rbuffs[head]->ie_rbd_actual);
715 len &= (IE_RBUF_SIZE | (IE_RBUF_SIZE - 1));
716 return len;
719 static inline int
720 ie_packet_len(struct ie_softc *sc)
722 int i;
723 int head = sc->rbhead;
724 int acc = 0;
726 do {
727 if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
728 == 0) {
729 #ifdef IEDEBUG
730 print_rbd(sc->rbuffs[sc->rbhead]);
731 #endif
732 log(LOG_ERR,
733 "%s: receive descriptors out of sync at %d\n",
734 device_xname(sc->sc_dev), sc->rbhead);
735 iereset(sc);
736 return -1;
739 i = sc->rbuffs[head]->ie_rbd_actual & IE_RBD_LAST;
741 acc += ie_buflen(sc, head);
742 head = (head + 1) % sc->nrxbuf;
743 } while (i == 0);
745 return acc;
749 * Setup all necessary artifacts for an XMIT command, and then pass the XMIT
750 * command to the chip to be executed. On the way, if we have a BPF listener
751 * also give him a copy.
753 static void
754 iexmit(struct ie_softc *sc)
756 struct ifnet *ifp;
758 ifp = &sc->sc_if;
760 #ifdef IEDEBUG
761 if (sc->sc_debug & IED_XMIT)
762 printf("%s: xmit buffer %d\n", device_xname(sc->sc_dev),
763 sc->xctail);
764 #endif
766 #if NBPFILTER > 0
768 * If BPF is listening on this interface, let it see the packet before
769 * we push it on the wire.
771 if (ifp->if_bpf)
772 bpf_tap(ifp->if_bpf,
773 sc->xmit_cbuffs[sc->xctail],
774 SWAP(sc->xmit_buffs[sc->xctail]->ie_xmit_flags));
775 #endif
777 sc->xmit_buffs[sc->xctail]->ie_xmit_flags |= IE_XMIT_LAST;
778 sc->xmit_buffs[sc->xctail]->ie_xmit_next = SWAP(0xffff);
779 sc->xmit_buffs[sc->xctail]->ie_xmit_buf =
780 Swap32(vtop24(sc, sc->xmit_cbuffs[sc->xctail]));
782 sc->xmit_cmds[sc->xctail]->com.ie_cmd_link = SWAP(0xffff);
783 sc->xmit_cmds[sc->xctail]->com.ie_cmd_cmd =
784 IE_CMD_XMIT | IE_CMD_INTR | IE_CMD_LAST;
786 sc->xmit_cmds[sc->xctail]->ie_xmit_status = SWAP(0);
787 sc->xmit_cmds[sc->xctail]->ie_xmit_desc =
788 vtop16sw(sc, __UNVOLATILE(sc->xmit_buffs[sc->xctail]));
790 sc->scb->ie_command_list =
791 vtop16sw(sc, __UNVOLATILE(sc->xmit_cmds[sc->xctail]));
792 cmd_and_wait(sc, IE_CU_START, 0, 0);
794 ifp->if_timer = 5;
798 * Read data off the interface, and turn it into an mbuf chain.
800 * This code is DRAMATICALLY different from the previous version; this
801 * version tries to allocate the entire mbuf chain up front, given the
802 * length of the data available. This enables us to allocate mbuf
803 * clusters in many situations where before we would have had a long
804 * chain of partially-full mbufs. This should help to speed up the
805 * operation considerably. (Provided that it works, of course.)
807 static inline struct mbuf *
808 ieget(struct ie_softc *sc, int *to_bpf)
810 struct mbuf *top, **mp, *m;
811 int len, totlen, resid;
812 int thisrboff, thismboff;
813 int head;
814 struct ether_header eh;
816 totlen = ie_packet_len(sc);
817 if (totlen <= 0)
818 return 0;
820 head = sc->rbhead;
823 * Snarf the Ethernet header.
825 (sc->sc_memcpy)((void *)&eh, (void *)sc->cbuffs[head],
826 sizeof(struct ether_header));
829 * As quickly as possible, check if this packet is for us.
830 * If not, don't waste a single cycle copying the rest of the
831 * packet in.
832 * This is only a consideration when FILTER is defined; i.e., when
833 * we are either running BPF or doing multicasting.
835 if (check_eh(sc, &eh, to_bpf) == 0) {
836 /* just this case, it's not an error */
837 sc->sc_if.if_ierrors--;
838 return 0;
841 resid = totlen;
843 MGETHDR(m, M_DONTWAIT, MT_DATA);
844 if (m == 0)
845 return 0;
847 m->m_pkthdr.rcvif = &sc->sc_if;
848 m->m_pkthdr.len = totlen;
849 len = MHLEN;
850 top = 0;
851 mp = &top;
854 * This loop goes through and allocates mbufs for all the data we will
855 * be copying in. It does not actually do the copying yet.
857 while (totlen > 0) {
858 if (top) {
859 MGET(m, M_DONTWAIT, MT_DATA);
860 if (m == 0) {
861 m_freem(top);
862 return 0;
864 len = MLEN;
866 if (totlen >= MINCLSIZE) {
867 MCLGET(m, M_DONTWAIT);
868 if (m->m_flags & M_EXT)
869 len = MCLBYTES;
872 if (mp == &top) {
873 char *newdata = (char *)
874 ALIGN(m->m_data + sizeof(struct ether_header)) -
875 sizeof(struct ether_header);
876 len -= newdata - m->m_data;
877 m->m_data = newdata;
880 m->m_len = len = min(totlen, len);
882 totlen -= len;
883 *mp = m;
884 mp = &m->m_next;
887 m = top;
888 thismboff = 0;
891 * Copy the Ethernet header into the mbuf chain.
893 memcpy(mtod(m, void *), &eh, sizeof(struct ether_header));
894 thismboff = sizeof(struct ether_header);
895 thisrboff = sizeof(struct ether_header);
896 resid -= sizeof(struct ether_header);
899 * Now we take the mbuf chain (hopefully only one mbuf most of the
900 * time) and stuff the data into it. There are no possible failures
901 * at or after this point.
903 while (resid > 0) {
904 int thisrblen = ie_buflen(sc, head) - thisrboff;
905 int thismblen = m->m_len - thismboff;
907 len = min(thisrblen, thismblen);
908 (sc->sc_memcpy)(mtod(m, char *) + thismboff,
909 (void *)(sc->cbuffs[head] + thisrboff),
910 (u_int)len);
911 resid -= len;
913 if (len == thismblen) {
914 m = m->m_next;
915 thismboff = 0;
916 } else
917 thismboff += len;
919 if (len == thisrblen) {
920 head = (head + 1) % sc->nrxbuf;
921 thisrboff = 0;
922 } else
923 thisrboff += len;
927 * Unless something changed strangely while we were doing the copy,
928 * we have now copied everything in from the shared memory.
929 * This means that we are done.
931 return top;
935 * Read frame NUM from unit UNIT (pre-cached as IE).
937 * This routine reads the RFD at NUM, and copies in the buffers from
938 * the list of RBD, then rotates the RBD and RFD lists so that the receiver
939 * doesn't start complaining. Trailers are DROPPED---there's no point
940 * in wasting time on confusing code to deal with them. Hopefully,
941 * this machine will never ARP for trailers anyway.
943 static void
944 ie_readframe(struct ie_softc *sc, int num)
946 int status;
947 struct mbuf *m = 0;
948 #if NBPFILTER > 0
949 int bpf_gets_it = 0;
950 #endif
952 status = sc->rframes[num]->ie_fd_status;
954 /* Advance the RFD list, since we're done with this descriptor. */
955 sc->rframes[num]->ie_fd_status = SWAP(0);
956 sc->rframes[num]->ie_fd_last |= IE_FD_LAST;
957 sc->rframes[sc->rftail]->ie_fd_last &= ~IE_FD_LAST;
958 sc->rftail = (sc->rftail + 1) % sc->nframes;
959 sc->rfhead = (sc->rfhead + 1) % sc->nframes;
961 if (status & IE_FD_OK) {
962 #if NBPFILTER > 0
963 m = ieget(sc, &bpf_gets_it);
964 #else
965 m = ieget(sc, NULL);
966 #endif
967 ie_drop_packet_buffer(sc);
969 if (m == 0) {
970 sc->sc_if.if_ierrors++;
971 return;
974 #ifdef IEDEBUG
975 if (sc->sc_debug & IED_READFRAME) {
976 struct ether_header *eh = mtod(m, struct ether_header *);
978 printf("%s: frame from ether %s type 0x%x\n",
979 device_xname(sc->sc_dev),
980 ether_sprintf(eh->ether_shost), (u_int)eh->ether_type);
982 #endif
984 #if NBPFILTER > 0
986 * Check for a BPF filter; if so, hand it up.
987 * Note that we have to stick an extra mbuf up front, because
988 * bpf_mtap expects to have the ether header at the front.
989 * It doesn't matter that this results in an ill-formatted mbuf chain,
990 * since BPF just looks at the data. (It doesn't try to free the mbuf,
991 * tho' it will make a copy for tcpdump.)
993 if (bpf_gets_it) {
994 /* Pass it up. */
995 bpf_mtap(sc->sc_if.if_bpf, m);
998 * A signal passed up from the filtering code indicating that
999 * the packet is intended for BPF but not for the protocol
1000 * machinery. We can save a few cycles by not handing it off
1001 * to them.
1003 if (bpf_gets_it == 2) {
1004 m_freem(m);
1005 return;
1008 #endif /* NBPFILTER > 0 */
1011 * In here there used to be code to check destination addresses upon
1012 * receipt of a packet. We have deleted that code, and replaced it
1013 * with code to check the address much earlier in the cycle, before
1014 * copying the data in; this saves us valuable cycles when operating
1015 * as a multicast router or when using BPF.
1019 * Finally pass this packet up to higher layers.
1021 (*sc->sc_if.if_input)(&sc->sc_if, m);
1022 sc->sc_if.if_ipackets++;
1025 static void
1026 ie_drop_packet_buffer(struct ie_softc *sc)
1028 int i;
1030 do {
1032 * This means we are somehow out of sync. So, we reset the
1033 * adapter.
1035 if ((sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_USED)
1036 == 0) {
1037 #ifdef IEDEBUG
1038 print_rbd(sc->rbuffs[sc->rbhead]);
1039 #endif
1040 log(LOG_ERR,
1041 "%s: receive descriptors out of sync at %d\n",
1042 device_xname(sc->sc_dev), sc->rbhead);
1043 iereset(sc);
1044 return;
1047 i = sc->rbuffs[sc->rbhead]->ie_rbd_actual & IE_RBD_LAST;
1049 sc->rbuffs[sc->rbhead]->ie_rbd_length |= IE_RBD_LAST;
1050 sc->rbuffs[sc->rbhead]->ie_rbd_actual = SWAP(0);
1051 sc->rbhead = (sc->rbhead + 1) % sc->nrxbuf;
1052 sc->rbuffs[sc->rbtail]->ie_rbd_length &= ~IE_RBD_LAST;
1053 sc->rbtail = (sc->rbtail + 1) % sc->nrxbuf;
1054 } while (i == 0);
1058 * Start transmission on an interface.
1060 static void
1061 iestart(struct ifnet *ifp)
1063 struct ie_softc *sc = ifp->if_softc;
1064 struct mbuf *m0, *m;
1065 uint8_t *buffer;
1066 uint16_t len;
1068 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1069 return;
1071 for (;;) {
1072 if (sc->xmit_busy == sc->ntxbuf) {
1073 ifp->if_flags |= IFF_OACTIVE;
1074 break;
1077 IF_DEQUEUE(&ifp->if_snd, m0);
1078 if (m0 == 0)
1079 break;
1081 /* We need to use m->m_pkthdr.len, so require the header */
1082 if ((m0->m_flags & M_PKTHDR) == 0)
1083 panic("%s: no header mbuf", __func__);
1085 #if NBPFILTER > 0
1086 /* Tap off here if there is a BPF listener. */
1087 if (ifp->if_bpf)
1088 bpf_mtap(ifp->if_bpf, m0);
1089 #endif
1091 #ifdef IEDEBUG
1092 if (sc->sc_debug & IED_ENQ)
1093 printf("%s: fill buffer %d\n", device_xname(sc->sc_dev),
1094 sc->xchead);
1095 #endif
1097 buffer = sc->xmit_cbuffs[sc->xchead];
1098 for (m = m0; m != 0; m = m->m_next) {
1099 (sc->sc_memcpy)(buffer, mtod(m, void *), m->m_len);
1100 buffer += m->m_len;
1102 if (m0->m_pkthdr.len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1103 sc->sc_memset(buffer, 0,
1104 ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len);
1105 len = ETHER_MIN_LEN - ETHER_CRC_LEN;
1106 } else
1107 len = m0->m_pkthdr.len;
1109 m_freem(m0);
1110 sc->xmit_buffs[sc->xchead]->ie_xmit_flags = SWAP(len);
1112 /* Start the first packet transmitting. */
1113 if (sc->xmit_busy == 0)
1114 iexmit(sc);
1116 sc->xchead = (sc->xchead + 1) % sc->ntxbuf;
1117 sc->xmit_busy++;
1121 static void
1122 iereset(struct ie_softc *sc)
1124 int s;
1126 s = splnet();
1128 /* No message here. The caller does that. */
1129 iestop(sc);
1132 * Stop i82586 dead in its tracks.
1134 if (cmd_and_wait(sc, IE_RU_ABORT | IE_CU_ABORT, 0, 0))
1135 printf("%s: abort commands timed out\n",
1136 device_xname(sc->sc_dev));
1138 if (cmd_and_wait(sc, IE_RU_DISABLE | IE_CU_STOP, 0, 0))
1139 printf("%s: disable commands timed out\n",
1140 device_xname(sc->sc_dev));
1142 ieinit(sc);
1144 splx(s);
1148 * Send a command to the controller and wait for it to either
1149 * complete or be accepted, depending on the command. If the
1150 * command pointer is null, then pretend that the command is
1151 * not an action command. If the command pointer is not null,
1152 * and the command is an action command, wait for
1153 * ((volatile struct ie_cmd_common *)pcmd)->ie_cmd_status & MASK
1154 * to become true.
1156 static int
1157 cmd_and_wait(struct ie_softc *sc, int cmd, void *pcmd, int mask)
1159 volatile struct ie_cmd_common *cc = pcmd;
1160 volatile struct ie_sys_ctl_block *scb = sc->scb;
1161 int tmo;
1163 scb->ie_command = (uint16_t)cmd;
1164 (sc->chan_attn)(sc);
1166 /* Wait for the command to be accepted by the CU. */
1167 tmo = 10;
1168 while (scb->ie_command && --tmo)
1169 delay(10);
1170 if (scb->ie_command) {
1171 #ifdef IEDEBUG
1172 printf("%s: cmd_and_wait, CU stuck (1)\n",
1173 device_xname(sc->sc_dev));
1174 #endif
1175 return -1; /* timed out */
1179 * If asked, also wait for it to finish.
1181 if (IE_ACTION_COMMAND(cmd) && pcmd) {
1184 * According to the packet driver, the minimum timeout should
1185 * be .369 seconds, which we round up to .4.
1187 tmo = 36900;
1190 * Now spin-lock waiting for status. This is not a very nice
1191 * thing to do, but I haven't figured out how, or indeed if, we
1192 * can put the process waiting for action to sleep. (We may
1193 * be getting called through some other timeout running in the
1194 * kernel.)
1196 while (((cc->ie_cmd_status & mask) == 0) && --tmo)
1197 delay(10);
1199 if ((cc->ie_cmd_status & mask) == 0) {
1200 #ifdef IEDEBUG
1201 printf("%s: cmd_and_wait, CU stuck (2)\n",
1202 device_xname(sc->sc_dev));
1203 #endif
1204 return -1; /* timed out */
1207 return 0;
1211 * Run the time-domain reflectometer.
1213 static void
1214 run_tdr(struct ie_softc *sc, struct ie_tdr_cmd *cmd)
1216 int result;
1218 cmd->com.ie_cmd_status = SWAP(0);
1219 cmd->com.ie_cmd_cmd = IE_CMD_TDR | IE_CMD_LAST;
1220 cmd->com.ie_cmd_link = SWAP(0xffff);
1222 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1223 cmd->ie_tdr_time = SWAP(0);
1225 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1226 (cmd->com.ie_cmd_status & IE_STAT_OK) == 0)
1227 result = 0x10000; /* impossible value */
1228 else
1229 result = cmd->ie_tdr_time;
1231 ie_ack(sc, IE_ST_WHENCE);
1233 if (result & IE_TDR_SUCCESS)
1234 return;
1236 if (result & 0x10000) {
1237 printf("%s: TDR command failed\n", device_xname(sc->sc_dev));
1238 } else if (result & IE_TDR_XCVR) {
1239 printf("%s: transceiver problem\n", device_xname(sc->sc_dev));
1240 } else if (result & IE_TDR_OPEN) {
1241 printf("%s: TDR detected an open %d clocks away\n",
1242 device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
1243 } else if (result & IE_TDR_SHORT) {
1244 printf("%s: TDR detected a short %d clocks away\n",
1245 device_xname(sc->sc_dev), SWAP(result & IE_TDR_TIME));
1246 } else {
1247 printf("%s: TDR returned unknown status 0x%x\n",
1248 device_xname(sc->sc_dev), result);
1253 * iememinit: set up the buffers
1255 * we have a block of KVA at sc->buf_area which is of size sc->buf_area_sz.
1256 * this is to be used for the buffers. the chip indexs its control data
1257 * structures with 16 bit offsets, and it indexes actual buffers with
1258 * 24 bit addresses. so we should allocate control buffers first so that
1259 * we don't overflow the 16 bit offset field. The number of transmit
1260 * buffers is fixed at compile time.
1262 * note: this function was written to be easy to understand, rather than
1263 * highly efficient (it isn't in the critical path).
1265 * The memory layout is: tbufs, rbufs, (gap), control blocks
1266 * [tbuf0, tbuf1] [rbuf0,...rbufN] gap [rframes] [tframes]
1267 * XXX - This needs review...
1269 static void
1270 iememinit(struct ie_softc *sc)
1272 uint8_t *ptr;
1273 int i;
1274 uint16_t nxt;
1276 /* First, zero all the memory. */
1277 ptr = sc->buf_area;
1278 (sc->sc_memset)(ptr, 0, sc->buf_area_sz);
1280 /* Allocate tx/rx buffers. */
1281 for (i = 0; i < NTXBUF; i++) {
1282 sc->xmit_cbuffs[i] = ptr;
1283 ptr += IE_TBUF_SIZE;
1285 for (i = 0; i < sc->nrxbuf; i++) {
1286 sc->cbuffs[i] = ptr;
1287 ptr += IE_RBUF_SIZE;
1290 /* Small pad (Don't trust the chip...) */
1291 ptr += 16;
1293 /* Allocate and fill in xmit buffer descriptors. */
1294 for (i = 0; i < NTXBUF; i++) {
1295 sc->xmit_buffs[i] = (volatile void *)ptr;
1296 ptr = Align(ptr + sizeof(*sc->xmit_buffs[i]));
1297 sc->xmit_buffs[i]->ie_xmit_buf =
1298 Swap32(vtop24(sc, sc->xmit_cbuffs[i]));
1299 sc->xmit_buffs[i]->ie_xmit_next = SWAP(0xffff);
1302 /* Allocate and fill in recv buffer descriptors. */
1303 for (i = 0; i < sc->nrxbuf; i++) {
1304 sc->rbuffs[i] = (volatile void *)ptr;
1305 ptr = Align(ptr + sizeof(*sc->rbuffs[i]));
1306 sc->rbuffs[i]->ie_rbd_buffer =
1307 Swap32(vtop24(sc, sc->cbuffs[i]));
1308 sc->rbuffs[i]->ie_rbd_length = SWAP(IE_RBUF_SIZE);
1311 /* link together recv bufs and set EOL on last */
1312 i = sc->nrxbuf - 1;
1313 sc->rbuffs[i]->ie_rbd_length |= IE_RBD_LAST;
1314 nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
1315 do {
1316 sc->rbuffs[i]->ie_rbd_next = nxt;
1317 nxt = vtop16sw(sc, __UNVOLATILE(sc->rbuffs[i]));
1318 } while (--i >= 0);
1320 /* Allocate transmit commands. */
1321 for (i = 0; i < NTXBUF; i++) {
1322 sc->xmit_cmds[i] = (volatile void *)ptr;
1323 ptr = Align(ptr + sizeof(*sc->xmit_cmds[i]));
1324 sc->xmit_cmds[i]->com.ie_cmd_link = SWAP(0xffff);
1327 /* Allocate receive frames. */
1328 for (i = 0; i < sc->nframes; i++) {
1329 sc->rframes[i] = (volatile void *)ptr;
1330 ptr = Align(ptr + sizeof(*sc->rframes[i]));
1333 /* Link together recv frames and set EOL on last */
1334 i = sc->nframes - 1;
1335 sc->rframes[i]->ie_fd_last |= IE_FD_LAST;
1336 nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1337 do {
1338 sc->rframes[i]->ie_fd_next = nxt;
1339 nxt = vtop16sw(sc, __UNVOLATILE(sc->rframes[i]));
1340 } while (--i >= 0);
1343 /* Pointers to last packet sent and next available transmit buffer. */
1344 sc->xchead = sc->xctail = 0;
1346 /* Clear transmit-busy flag. */
1347 sc->xmit_busy = 0;
1350 * Set the head and tail pointers on receive to keep track of
1351 * the order in which RFDs and RBDs are used. link the
1352 * recv frames and buffer into the scb.
1354 sc->rfhead = 0;
1355 sc->rftail = sc->nframes - 1;
1356 sc->rbhead = 0;
1357 sc->rbtail = sc->nrxbuf - 1;
1359 sc->scb->ie_recv_list =
1360 vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1361 sc->rframes[0]->ie_fd_buf_desc =
1362 vtop16sw(sc, __UNVOLATILE(sc->rbuffs[0]));
1364 i = (ptr - sc->buf_area);
1365 #ifdef IEDEBUG
1366 printf("IE_DEBUG: used %d of %d bytes\n", i, sc->buf_area_sz);
1367 #endif
1368 if (i > sc->buf_area_sz)
1369 panic("ie: iememinit, out of space");
1373 * Run the multicast setup command.
1374 * Called at splnet().
1376 static int
1377 mc_setup(struct ie_softc *sc, void *ptr)
1379 struct ie_mcast_cmd *cmd = ptr; /* XXX - Was volatile */
1381 cmd->com.ie_cmd_status = SWAP(0);
1382 cmd->com.ie_cmd_cmd = IE_CMD_MCAST | IE_CMD_LAST;
1383 cmd->com.ie_cmd_link = SWAP(0xffff);
1385 (sc->sc_memcpy)((void *)cmd->ie_mcast_addrs,
1386 (void *)sc->mcast_addrs,
1387 sc->mcast_count * sizeof *sc->mcast_addrs);
1389 cmd->ie_mcast_bytes =
1390 SWAP(sc->mcast_count * ETHER_ADDR_LEN); /* grrr... */
1392 sc->scb->ie_command_list = vtop16sw(sc, cmd);
1393 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1394 (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1395 printf("%s: multicast address setup command failed\n",
1396 device_xname(sc->sc_dev));
1397 return 0;
1399 return 1;
1402 static inline void
1403 ie_setup_config(struct ie_config_cmd *cmd, int promiscuous, int manchester)
1407 * these are all char's so no need to byte-swap
1409 cmd->ie_config_count = 0x0c;
1410 cmd->ie_fifo = 8;
1411 cmd->ie_save_bad = 0x40;
1412 cmd->ie_addr_len = 0x2e;
1413 cmd->ie_priority = 0;
1414 cmd->ie_ifs = 0x60;
1415 cmd->ie_slot_low = 0;
1416 cmd->ie_slot_high = 0xf2;
1417 cmd->ie_promisc = promiscuous | manchester << 2;
1418 cmd->ie_crs_cdt = 0;
1419 cmd->ie_min_len = 64;
1420 cmd->ie_junk = 0xff;
1424 * This routine inits the ie.
1425 * This includes executing the CONFIGURE, IA-SETUP, and MC-SETUP commands,
1426 * starting the receiver unit, and clearing interrupts.
1428 * THIS ROUTINE MUST BE CALLED AT splnet() OR HIGHER.
1430 static int
1431 ieinit(struct ie_softc *sc)
1433 volatile struct ie_sys_ctl_block *scb = sc->scb;
1434 void *ptr;
1435 struct ifnet *ifp;
1437 ifp = &sc->sc_if;
1438 ptr = sc->buf_area; /* XXX - Use scb instead? */
1441 * Send the configure command first.
1444 struct ie_config_cmd *cmd = ptr; /* XXX - Was volatile */
1446 scb->ie_command_list = vtop16sw(sc, cmd);
1447 cmd->com.ie_cmd_status = SWAP(0);
1448 cmd->com.ie_cmd_cmd = IE_CMD_CONFIG | IE_CMD_LAST;
1449 cmd->com.ie_cmd_link = SWAP(0xffff);
1451 ie_setup_config(cmd, (sc->promisc != 0), 0);
1453 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1454 (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1455 printf("%s: configure command failed\n",
1456 device_xname(sc->sc_dev));
1457 return 0;
1462 * Now send the Individual Address Setup command.
1465 struct ie_iasetup_cmd *cmd = ptr; /* XXX - Was volatile */
1467 scb->ie_command_list = vtop16sw(sc, cmd);
1468 cmd->com.ie_cmd_status = SWAP(0);
1469 cmd->com.ie_cmd_cmd = IE_CMD_IASETUP | IE_CMD_LAST;
1470 cmd->com.ie_cmd_link = SWAP(0xffff);
1472 (sc->sc_memcpy)((void *)&cmd->ie_address,
1473 CLLADDR(ifp->if_sadl), sizeof(cmd->ie_address));
1475 if (cmd_and_wait(sc, IE_CU_START, cmd, IE_STAT_COMPL) ||
1476 (cmd->com.ie_cmd_status & IE_STAT_OK) == 0) {
1477 printf("%s: individual address setup command failed\n",
1478 device_xname(sc->sc_dev));
1479 return 0;
1484 * Now run the time-domain reflectometer.
1486 if (ie_run_tdr)
1487 run_tdr(sc, ptr);
1490 * Acknowledge any interrupts we have generated thus far.
1492 ie_ack(sc, IE_ST_WHENCE);
1495 * Set up the transmit and recv buffers.
1497 iememinit(sc);
1499 /* tell higher levels that we are here */
1500 ifp->if_flags |= IFF_RUNNING;
1501 ifp->if_flags &= ~IFF_OACTIVE;
1503 sc->scb->ie_recv_list =
1504 vtop16sw(sc, __UNVOLATILE(sc->rframes[0]));
1505 cmd_and_wait(sc, IE_RU_START, 0, 0);
1507 ie_ack(sc, IE_ST_WHENCE);
1509 if (sc->run_586)
1510 (sc->run_586)(sc);
1512 return 0;
1515 static void
1516 iestop(struct ie_softc *sc)
1519 cmd_and_wait(sc, IE_RU_DISABLE, 0, 0);
1522 static int
1523 ieioctl(struct ifnet *ifp, u_long cmd, void *data)
1525 struct ie_softc *sc = ifp->if_softc;
1526 struct ifaddr *ifa = (struct ifaddr *)data;
1527 int s, error = 0;
1529 s = splnet();
1531 switch (cmd) {
1533 case SIOCINITIFADDR:
1534 ifp->if_flags |= IFF_UP;
1536 switch (ifa->ifa_addr->sa_family) {
1537 #ifdef INET
1538 case AF_INET:
1539 ieinit(sc);
1540 arp_ifinit(ifp, ifa);
1541 break;
1542 #endif
1543 #ifdef NS
1544 /* XXX - This code is probably wrong. */
1545 case AF_NS:
1547 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1549 if (ns_nullhost(*ina))
1550 ina->x_host =
1551 *(union ns_host *)LLADDR(ifp->if_sadl);
1552 else
1553 memcpy(LLADDR(ifp->if_sadl),
1554 ina->x_host.c_host, ETHER_ADDR_LEN);
1555 /* Set new address. */
1556 ieinit(sc);
1557 break;
1559 #endif /* NS */
1560 default:
1561 ieinit(sc);
1562 break;
1564 break;
1566 case SIOCSIFFLAGS:
1567 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1568 break;
1569 sc->promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
1571 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1572 case IFF_RUNNING:
1574 * If interface is marked down and it is running, then
1575 * stop it.
1577 iestop(sc);
1578 ifp->if_flags &= ~IFF_RUNNING;
1579 break;
1580 case IFF_UP:
1582 * If interface is marked up and it is stopped, then
1583 * start it.
1585 ieinit(sc);
1586 break;
1587 default:
1589 * Reset the interface to pick up changes in any other
1590 * flags that affect hardware registers.
1592 iestop(sc);
1593 ieinit(sc);
1594 break;
1596 #ifdef IEDEBUG
1597 if (ifp->if_flags & IFF_DEBUG)
1598 sc->sc_debug = IED_ALL;
1599 else
1600 sc->sc_debug = ie_debug_flags;
1601 #endif
1602 break;
1604 case SIOCADDMULTI:
1605 case SIOCDELMULTI:
1606 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1608 * Multicast list has changed; set the hardware filter
1609 * accordingly.
1611 if (ifp->if_flags & IFF_RUNNING)
1612 mc_reset(sc);
1613 error = 0;
1615 break;
1617 default:
1618 error = ether_ioctl(ifp, cmd, data);
1619 break;
1621 splx(s);
1622 return error;
1625 static void
1626 mc_reset(struct ie_softc *sc)
1628 struct ether_multi *enm;
1629 struct ether_multistep step;
1630 struct ifnet *ifp;
1632 ifp = &sc->sc_if;
1635 * Step through the list of addresses.
1637 sc->mcast_count = 0;
1638 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1639 while (enm) {
1640 if (sc->mcast_count >= MAXMCAST ||
1641 ether_cmp(enm->enm_addrlo, enm->enm_addrhi) != 0) {
1642 ifp->if_flags |= IFF_ALLMULTI;
1643 ieioctl(ifp, SIOCSIFFLAGS, NULL);
1644 goto setflag;
1646 memcpy(&sc->mcast_addrs[sc->mcast_count], enm->enm_addrlo,
1647 ETHER_ADDR_LEN);
1648 sc->mcast_count++;
1649 ETHER_NEXT_MULTI(step, enm);
1651 setflag:
1652 sc->want_mcsetup = 1;
1655 #ifdef IEDEBUG
1656 void
1657 print_rbd(volatile struct ie_recv_buf_desc *rbd)
1660 printf("RBD at %08lx:\nactual %04x, next %04x, buffer %08x\n"
1661 "length %04x, mbz %04x\n", (u_long)rbd, rbd->ie_rbd_actual,
1662 rbd->ie_rbd_next, rbd->ie_rbd_buffer, rbd->ie_rbd_length,
1663 rbd->mbz);
1665 #endif