[BACK]Return to if_ie.c CVS log [TXT][DIR] Up to [local] / sys / arch / mvme88k / dev

Annotation of sys/arch/mvme88k/dev/if_ie.c, Revision 1.1.1.1

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

CVSweb