BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / vax / if / if_dmc.c
index 85f677e..0e252ce 100644 (file)
@@ -1,9 +1,36 @@
 /*
 /*
- * Copyright (c) 1982 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1982, 1986 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)if_dmc.c    6.8 (Berkeley) %G%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)if_dmc.c    7.10 (Berkeley) 12/16/90
  */
 
 #include "dmc.h"
  */
 
 #include "dmc.h"
 
 /* #define DEBUG       /* for base table dump on fatal error */
 
 
 /* #define DEBUG       /* for base table dump on fatal error */
 
-#include "../machine/pte.h"
-
-#include "param.h"
-#include "systm.h"
-#include "mbuf.h"
-#include "buf.h"
-#include "ioctl.h"             /* must precede tty.h */
-#include "tty.h"
-#include "protosw.h"
-#include "socket.h"
-#include "vmmac.h"
-#include "errno.h"
-
-#include "../net/if.h"
-#include "../net/netisr.h"
-#include "../net/route.h"
-
-#ifdef BBNNET
-#define        INET
-#endif
+#include "../include/pte.h"
+
+#include "sys/param.h"
+#include "sys/systm.h"
+#include "sys/mbuf.h"
+#include "sys/buf.h"
+#include "sys/ioctl.h"         /* must precede tty.h */
+#include "sys/tty.h"
+#include "sys/protosw.h"
+#include "sys/socket.h"
+#include "sys/syslog.h"
+#include "sys/vmmac.h"
+#include "sys/errno.h"
+#include "sys/time.h"
+#include "sys/kernel.h"
+
+#include "net/if.h"
+#include "net/netisr.h"
+#include "net/route.h"
+
 #ifdef INET
 #ifdef INET
-#include "../netinet/in.h"
-#include "../netinet/in_systm.h"
-#include "../netinet/ip.h"
+#include "netinet/in.h"
+#include "netinet/in_systm.h"
+#include "netinet/in_var.h"
+#include "netinet/ip.h"
 #endif
 
 #endif
 
-#include "../vax/cpu.h"
-#include "../vax/mtpr.h"
+#include "../include/cpu.h"
+#include "../include/mtpr.h"
 #include "if_uba.h"
 #include "if_dmc.h"
 #include "if_uba.h"
 #include "if_dmc.h"
-#include "../vaxuba/ubareg.h"
-#include "../vaxuba/ubavar.h"
+#include "../uba/ubareg.h"
+#include "../uba/ubavar.h"
 
 
-#include "../h/time.h"
-#include "../h/kernel.h"
 
 
-int    dmctimer;                       /* timer started? */
-int    dmc_timeout = 8;                /* timeout value */
-int    dmcwatch();
+/*
+ * output timeout value, sec.; should depend on line speed.
+ */
+int    dmc_timeout = 20;
 
 /*
  * Driver information for auto-configuration stuff.
  */
 int    dmcprobe(), dmcattach(), dmcinit(), dmcioctl();
 
 /*
  * Driver information for auto-configuration stuff.
  */
 int    dmcprobe(), dmcattach(), dmcinit(), dmcioctl();
-int    dmcoutput(), dmcreset();
+int    dmcoutput(), dmcreset(), dmctimeout();
 struct uba_device *dmcinfo[NDMC];
 u_short        dmcstd[] = { 0 };
 struct uba_driver dmcdriver =
 struct uba_device *dmcinfo[NDMC];
 u_short        dmcstd[] = { 0 };
 struct uba_driver dmcdriver =
@@ -116,22 +143,21 @@ struct dmcbufs {
  * efficiently.
  */
 struct dmc_softc {
  * efficiently.
  */
 struct dmc_softc {
+       struct  ifnet sc_if;            /* network-visible interface */
        short   sc_oused;               /* output buffers currently in use */
        short   sc_iused;               /* input buffers given to DMC */
        short   sc_flag;                /* flags */
        short   sc_oused;               /* output buffers currently in use */
        short   sc_iused;               /* input buffers given to DMC */
        short   sc_flag;                /* flags */
-       int     sc_nticks;              /* seconds since last interrupt */
-       struct  ifnet sc_if;            /* network-visible interface */
-       struct  dmcbufs sc_rbufs[NRCV]; /* receive buffer info */
-       struct  dmcbufs sc_xbufs[NXMT]; /* transmit buffer info */
-       struct  ifubinfo sc_ifuba;      /* UNIBUS resources */
-       struct  ifrw sc_ifr[NRCV];      /* UNIBUS receive buffer maps */
-       struct  ifxmt sc_ifw[NXMT];     /* UNIBUS receive buffer maps */
        int     sc_ubinfo;              /* UBA mapping info for base table */
        int     sc_errors[4];           /* non-fatal error counters */
 #define sc_datck sc_errors[0]
 #define sc_timeo sc_errors[1]
 #define sc_nobuf sc_errors[2]
 #define sc_disc  sc_errors[3]
        int     sc_ubinfo;              /* UBA mapping info for base table */
        int     sc_errors[4];           /* non-fatal error counters */
 #define sc_datck sc_errors[0]
 #define sc_timeo sc_errors[1]
 #define sc_nobuf sc_errors[2]
 #define sc_disc  sc_errors[3]
+       struct  dmcbufs sc_rbufs[NRCV]; /* receive buffer info */
+       struct  dmcbufs sc_xbufs[NXMT]; /* transmit buffer info */
+       struct  ifubinfo sc_ifuba;      /* UNIBUS resources */
+       struct  ifrw sc_ifr[NRCV];      /* UNIBUS receive buffer maps */
+       struct  ifxmt sc_ifw[NXMT];     /* UNIBUS receive buffer maps */
        /* command queue stuff */
        struct  dmc_command sc_cmdbuf[NCMDS];
        struct  dmc_command *sc_qhead;  /* head of command queue */
        /* command queue stuff */
        struct  dmc_command sc_cmdbuf[NCMDS];
        struct  dmc_command *sc_qhead;  /* head of command queue */
@@ -143,10 +169,10 @@ struct dmc_softc {
 } dmc_softc[NDMC];
 
 /* flags */
 } dmc_softc[NDMC];
 
 /* flags */
-#define DMC_ALLOC      01              /* unibus resources allocated */
-#define DMC_BMAPPED    02              /* base table mapped */
-#define DMC_RESTART    04              /* software restart in progress */
-#define DMC_ACTIVE     08              /* device active */
+#define DMC_RUNNING    0x01            /* device initialized */
+#define DMC_BMAPPED    0x02            /* base table mapped */
+#define DMC_RESTART    0x04            /* software restart in progress */
+#define DMC_ONLINE     0x08            /* device running (had a RDYO) */
 
 struct dmc_base {
        short   d_base[128];            /* DMC base table */
 
 struct dmc_base {
        short   d_base[128];            /* DMC base table */
@@ -217,13 +243,10 @@ dmcattach(ui)
        sc->sc_if.if_output = dmcoutput;
        sc->sc_if.if_ioctl = dmcioctl;
        sc->sc_if.if_reset = dmcreset;
        sc->sc_if.if_output = dmcoutput;
        sc->sc_if.if_ioctl = dmcioctl;
        sc->sc_if.if_reset = dmcreset;
+       sc->sc_if.if_watchdog = dmctimeout;
        sc->sc_if.if_flags = IFF_POINTOPOINT;
        sc->sc_ifuba.iff_flags = UBA_CANTWAIT;
 
        sc->sc_if.if_flags = IFF_POINTOPOINT;
        sc->sc_ifuba.iff_flags = UBA_CANTWAIT;
 
-       if (dmctimer == 0) {
-               dmctimer = 1;
-               timeout(dmcwatch, (caddr_t) 0, hz);
-       }
        if_attach(&sc->sc_if);
 }
 
        if_attach(&sc->sc_if);
 }
 
@@ -271,7 +294,7 @@ dmcinit(unit)
         * (both local and destination for an address family).
         */
        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
         * (both local and destination for an address family).
         */
        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
-               if (ifa->ifa_addr.sa_family && ifa->ifa_dstaddr.sa_family)
+               if (ifa->ifa_addr->sa_family && ifa->ifa_dstaddr->sa_family)
                        break;
        if (ifa == (struct ifaddr *) 0)
                return;
                        break;
        if (ifa == (struct ifaddr *) 0)
                return;
@@ -299,12 +322,18 @@ dmcinit(unit)
                }
                ifp->if_flags |= IFF_RUNNING;
        }
                }
                ifp->if_flags |= IFF_RUNNING;
        }
+       sc->sc_flag &= ~DMC_ONLINE;
+       sc->sc_flag |= DMC_RUNNING;
+       /*
+        * Limit packets enqueued until we see if we're on the air.
+        */
+       ifp->if_snd.ifq_maxlen = 3;
 
        /* initialize buffer pool */
        /* receives */
        ifrw = &sc->sc_ifr[0];
        for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
 
        /* initialize buffer pool */
        /* receives */
        ifrw = &sc->sc_ifr[0];
        for (rp = &sc->sc_rbufs[0]; rp < &sc->sc_rbufs[NRCV]; rp++) {
-               rp->ubinfo = ifrw->ifrw_info & 0x3ffff;
+               rp->ubinfo = UBAI_ADDR(ifrw->ifrw_info);
                rp->cc = DMCMTU + sizeof (struct dmc_header);
                rp->flags = DBUF_OURS|DBUF_RCV;
                ifrw++; 
                rp->cc = DMCMTU + sizeof (struct dmc_header);
                rp->flags = DBUF_OURS|DBUF_RCV;
                ifrw++; 
@@ -312,7 +341,7 @@ dmcinit(unit)
        /* transmits */
        ifxp = &sc->sc_ifw[0];
        for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
        /* transmits */
        ifxp = &sc->sc_ifw[0];
        for (rp = &sc->sc_xbufs[0]; rp < &sc->sc_xbufs[NXMT]; rp++) {
-               rp->ubinfo = ifxp->ifw_info & 0x3ffff;
+               rp->ubinfo = UBAI_ADDR(ifxp->ifw_info);
                rp->cc = 0;
                rp->flags = DBUF_OURS|DBUF_XMIT;
                ifxp++; 
                rp->cc = 0;
                rp->flags = DBUF_OURS|DBUF_XMIT;
                ifxp++; 
@@ -328,12 +357,12 @@ dmcinit(unit)
        }
 
        /* base in */
        }
 
        /* base in */
-       base = sc->sc_ubinfo & 0x3ffff;
-       dmcload(sc, DMC_BASEI, base, (base>>2) & DMC_XMEM);
+       base = UBAI_ADDR(sc->sc_ubinfo);
+       dmcload(sc, DMC_BASEI, (u_short)base, (base>>2) & DMC_XMEM);
        /* specify half duplex operation, flags tell if primary */
        /* or secondary station */
        if (ui->ui_flags == 0)
        /* specify half duplex operation, flags tell if primary */
        /* or secondary station */
        if (ui->ui_flags == 0)
-               /* use DDMCP mode in full duplex */
+               /* use DDCMP mode in full duplex */
                dmcload(sc, DMC_CNTLI, 0, 0);
        else if (ui->ui_flags == 1)
                /* use MAINTENENCE mode */
                dmcload(sc, DMC_CNTLI, 0, 0);
        else if (ui->ui_flags == 1)
                /* use MAINTENENCE mode */
@@ -346,7 +375,6 @@ dmcinit(unit)
                dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX | DMC_SEC);
 
        /* enable operation done interrupts */
                dmcload(sc, DMC_CNTLI, 0, DMC_HDPLX | DMC_SEC);
 
        /* enable operation done interrupts */
-       sc->sc_flag &= ~DMC_ACTIVE;
        while ((addr->bsel2 & DMC_IEO) == 0)
                addr->bsel2 |= DMC_IEO;
        s = spl5();
        while ((addr->bsel2 & DMC_IEO) == 0)
                addr->bsel2 |= DMC_IEO;
        s = spl5();
@@ -367,10 +395,9 @@ dmcinit(unit)
  *
  * Must be called at spl 5
  */
  *
  * Must be called at spl 5
  */
-dmcstart(dev)
-       dev_t dev;
+dmcstart(unit)
+       int unit;
 {
 {
-       int unit = minor(dev);
        register struct dmc_softc *sc = &dmc_softc[unit];
        struct mbuf *m;
        register struct dmcbufs *rp;
        register struct dmc_softc *sc = &dmc_softc[unit];
        struct mbuf *m;
        register struct dmcbufs *rp;
@@ -395,7 +422,8 @@ dmcstart(dev)
                         */
                        rp->cc = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[n], m);
                        rp->cc &= DMC_CCOUNT;
                         */
                        rp->cc = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[n], m);
                        rp->cc &= DMC_CCOUNT;
-                       sc->sc_oused++;
+                       if (++sc->sc_oused == 1)
+                               sc->sc_if.if_timer = dmc_timeout;
                        dmcload(sc, DMC_WRITE, rp->ubinfo, 
                                rp->cc | ((rp->ubinfo>>2)&DMC_XMEM));
                }
                        dmcload(sc, DMC_WRITE, rp->ubinfo, 
                                rp->cc | ((rp->ubinfo>>2)&DMC_XMEM));
                }
@@ -408,7 +436,8 @@ dmcstart(dev)
  */
 dmcload(sc, type, w0, w1)
        register struct dmc_softc *sc;
  */
 dmcload(sc, type, w0, w1)
        register struct dmc_softc *sc;
-       int type, w0, w1;
+       int type;
+       u_short w0, w1;
 {
        register struct dmcdevice *addr;
        register int unit, sps;
 {
        register struct dmcdevice *addr;
        register int unit, sps;
@@ -509,7 +538,7 @@ dmcxint(unit)
        struct dmcdevice *addr;
        struct mbuf *m;
        struct ifqueue *inq;
        struct dmcdevice *addr;
        struct mbuf *m;
        struct ifqueue *inq;
-       int arg, pkaddr, cmd, len;
+       int arg, pkaddr, cmd, len, s;
        register struct ifrw *ifrw;
        register struct dmcbufs *rp;
        register struct ifxmt *ifxp;
        register struct ifrw *ifrw;
        register struct dmcbufs *rp;
        register struct ifxmt *ifxp;
@@ -589,14 +618,6 @@ dmcxint(unit)
                        m = if_ubaget(&sc->sc_ifuba, ifrw, len, off, ifp);
                        if (m == 0)
                                goto setup;
                        m = if_ubaget(&sc->sc_ifuba, ifrw, len, off, ifp);
                        if (m == 0)
                                goto setup;
-                       if (off) {
-                               struct ifnet *ifp;
-
-                               ifp = *(mtod(m, struct ifnet **));
-                               m->m_off += 2 * sizeof (u_short);
-                               m->m_len -= 2 * sizeof (u_short);
-                               *(mtod(m, struct ifnet **)) = ifp;
-                       }
                        switch (dh->dmc_type) {
 
 #ifdef INET
                        switch (dh->dmc_type) {
 
 #ifdef INET
@@ -610,15 +631,17 @@ dmcxint(unit)
                                goto setup;
                        }
 
                                goto setup;
                        }
 
+                       s = splimp();
                        if (IF_QFULL(inq)) {
                                IF_DROP(inq);
                                m_freem(m);
                        } else
                                IF_ENQUEUE(inq, m);
                        if (IF_QFULL(inq)) {
                                IF_DROP(inq);
                                m_freem(m);
                        } else
                                IF_ENQUEUE(inq, m);
+                       splx(s);
 
        setup:
                        /* is this needed? */
 
        setup:
                        /* is this needed? */
-                       rp->ubinfo = ifrw->ifrw_info & 0x3ffff;
+                       rp->ubinfo = UBAI_ADDR(ifrw->ifrw_info);
 
                        dmcload(sc, DMC_READ, rp->ubinfo, 
                            ((rp->ubinfo >> 2) & DMC_XMEM) | rp->cc);
 
                        dmcload(sc, DMC_READ, rp->ubinfo, 
                            ((rp->ubinfo >> 2) & DMC_XMEM) | rp->cc);
@@ -651,16 +674,29 @@ dmcxint(unit)
                                ifxp->ifw_xtofree = 0;
                        }
                        rp->flags &= ~DBUF_DMCS;
                                ifxp->ifw_xtofree = 0;
                        }
                        rp->flags &= ~DBUF_DMCS;
-                       sc->sc_oused--;
-                       sc->sc_nticks = 0;
-                       sc->sc_flag |= DMC_ACTIVE;
+                       if (--sc->sc_oused == 0)
+                               sc->sc_if.if_timer = 0;
+                       else
+                               sc->sc_if.if_timer = dmc_timeout;
+                       if ((sc->sc_flag & DMC_ONLINE) == 0) {
+                               extern int ifqmaxlen;
+
+                               /*
+                                * We're on the air.
+                                * Open the queue to the usual value.
+                                */
+                               sc->sc_flag |= DMC_ONLINE;
+                               ifp->if_snd.ifq_maxlen = ifqmaxlen;
+                       }
                        break;
 
                case DMC_CNTLO:
                        arg &= DMC_CNTMASK;
                        if (arg & DMC_FATAL) {
                        break;
 
                case DMC_CNTLO:
                        arg &= DMC_CNTMASK;
                        if (arg & DMC_FATAL) {
-                               printd("dmc%d: fatal error, flags=%b\n",
-                                   unit, arg, CNTLO_BITS);
+                               if (arg != DMC_START)
+                                       log(LOG_ERR,
+                                           "dmc%d: fatal error, flags=%b\n",
+                                           unit, arg, CNTLO_BITS);
                                dmcrestart(unit);
                                break;
                        }
                                dmcrestart(unit);
                                break;
                        }
@@ -698,7 +734,7 @@ dmcxint(unit)
                                 * procedure error
                                 */
                                sc->sc_flag |= DMC_RESTART;
                                 * procedure error
                                 */
                                sc->sc_flag |= DMC_RESTART;
-                               arg = sc->sc_ubinfo & 0x3ffff;
+                               arg = UBAI_ADDR(sc->sc_ubinfo);
                                dmcload(sc, DMC_BASEI, arg, (arg>>2)&DMC_XMEM);
                        }
                        break;
                                dmcload(sc, DMC_BASEI, arg, (arg>>2)&DMC_XMEM);
                        }
                        break;
@@ -728,15 +764,21 @@ dmcoutput(ifp, m0, dst)
        register struct dmc_header *dh;
        register int off;
 
        register struct dmc_header *dh;
        register int off;
 
+       if ((ifp->if_flags & IFF_UP) == 0) {
+               error = ENETDOWN;
+               goto bad;
+       }
+
        switch (dst->sa_family) {
 #ifdef INET
        case AF_INET:
        switch (dst->sa_family) {
 #ifdef INET
        case AF_INET:
-               off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
+               off = m->m_pkthdr.len - m->m_len;
                if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
                if (off > 0 && (off & 0x1ff) == 0 &&
                if ((ifp->if_flags & IFF_NOTRAILERS) == 0)
                if (off > 0 && (off & 0x1ff) == 0 &&
-                   m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
+                   (m->m_flags & M_EXT) == 0 &&
+                   m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
                        type = DMC_TRAILER + (off>>9);
                        type = DMC_TRAILER + (off>>9);
-                       m->m_off -= 2 * sizeof (u_short);
+                       m->m_data -= 2 * sizeof (u_short);
                        m->m_len += 2 * sizeof (u_short);
                        *mtod(m, u_short *) = htons((u_short)DMC_IPTYPE);
                        *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
                        m->m_len += 2 * sizeof (u_short);
                        *mtod(m, u_short *) = htons((u_short)DMC_IPTYPE);
                        *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
@@ -776,19 +818,10 @@ gottype:
         * Add local network header
         * (there is space for a uba on a vax to step on)
         */
         * Add local network header
         * (there is space for a uba on a vax to step on)
         */
-       if (m->m_off > MMAXOFF ||
-           MMINOFF + sizeof(struct dmc_header) > m->m_off) {
-               m = m_get(M_DONTWAIT, MT_HEADER);
-               if (m == 0) {
-                       error = ENOBUFS;
-                       goto bad;
-               }
-               m->m_next = m0;
-               m->m_off = MMINOFF;
-               m->m_len = sizeof (struct dmc_header);
-       } else {
-               m->m_off -= sizeof (struct dmc_header);
-               m->m_len += sizeof (struct dmc_header);
+       M_PREPEND(m, sizeof(struct dmc_header), M_DONTWAIT);
+       if (m == 0) {
+               error = ENOBUFS;
+               goto bad;
        }
        dh = mtod(m, struct dmc_header *);
        dh->dmc_type = htons((u_short)type);
        }
        dh = mtod(m, struct dmc_header *);
        dh->dmc_type = htons((u_short)type);
@@ -825,6 +858,7 @@ dmcioctl(ifp, cmd, data)
        caddr_t data;
 {
        int s = splimp(), error = 0;
        caddr_t data;
 {
        int s = splimp(), error = 0;
+       register struct dmc_softc *sc = &dmc_softc[ifp->if_unit];
 
        switch (cmd) {
 
 
        switch (cmd) {
 
@@ -839,6 +873,15 @@ dmcioctl(ifp, cmd, data)
                        dmcinit(ifp->if_unit); 
                break;
                
                        dmcinit(ifp->if_unit); 
                break;
                
+       case SIOCSIFFLAGS:
+               if ((ifp->if_flags & IFF_UP) == 0 &&
+                   sc->sc_flag & DMC_RUNNING)
+                       dmcdown(ifp->if_unit);
+               else if (ifp->if_flags & IFF_UP &&
+                   (sc->sc_flag & DMC_RUNNING) == 0)
+                       dmcrestart(ifp->if_unit);
+               break;
+
        default:
                error = EINVAL;
        }
        default:
                error = EINVAL;
        }
@@ -854,75 +897,81 @@ dmcrestart(unit)
        int unit;
 {
        register struct dmc_softc *sc = &dmc_softc[unit];
        int unit;
 {
        register struct dmc_softc *sc = &dmc_softc[unit];
-       register struct uba_device *ui = dmcinfo[unit];
        register struct dmcdevice *addr;
        register struct dmcdevice *addr;
-       register struct ifxmt *ifxp;
        register int i;
        register int i;
-       struct ifubinfo *ifu;
+       int s;
        
        
-       addr = (struct dmcdevice *)ui->ui_addr;
-       ifu = &sc->sc_ifuba;
 #ifdef DEBUG
        /* dump base table */
        printf("dmc%d base table:\n", unit);
        for (i = 0; i < sizeof (struct dmc_base); i++)
                printf("%o\n" ,dmc_base[unit].d_base[i]);
 #endif
 #ifdef DEBUG
        /* dump base table */
        printf("dmc%d base table:\n", unit);
        for (i = 0; i < sizeof (struct dmc_base); i++)
                printf("%o\n" ,dmc_base[unit].d_base[i]);
 #endif
+
+       dmcdown(unit);
+
        /*
         * Let the DMR finish the MCLR.  At 1 Mbit, it should do so
         * in about a max of 6.4 milliseconds with diagnostics enabled.
         */
        /*
         * Let the DMR finish the MCLR.  At 1 Mbit, it should do so
         * in about a max of 6.4 milliseconds with diagnostics enabled.
         */
-       addr->bsel1 = DMC_MCLR;
+       addr = (struct dmcdevice *)(dmcinfo[unit]->ui_addr);
        for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--)
                ;
        /* Did the timer expire or did the DMR finish? */
        if ((addr->bsel1 & DMC_RUN) == 0) {
        for (i = 100000; i && (addr->bsel1 & DMC_RUN) == 0; i--)
                ;
        /* Did the timer expire or did the DMR finish? */
        if ((addr->bsel1 & DMC_RUN) == 0) {
-               printf("dmc%d: M820 Test Failed\n", unit);
+               log(LOG_ERR, "dmc%d: M820 Test Failed\n", unit);
                return;
        }
 
                return;
        }
 
+       /* restart DMC */
+       dmcinit(unit);
+       sc->sc_flag &= ~DMC_RESTART;
+       s = spl5();
+       dmcstart(unit);
+       splx(s);
+       sc->sc_if.if_collisions++;      /* why not? */
+}
+
+/*
+ * Reset a device and mark down.
+ * Flush output queue and drop queue limit.
+ */
+dmcdown(unit)
+       int unit;
+{
+       register struct dmc_softc *sc = &dmc_softc[unit];
+       register struct ifxmt *ifxp;
+
+       ((struct dmcdevice *)(dmcinfo[unit]->ui_addr))->bsel1 = DMC_MCLR;
+       sc->sc_flag &= ~(DMC_RUNNING | DMC_ONLINE);
+
        for (ifxp = sc->sc_ifw; ifxp < &sc->sc_ifw[NXMT]; ifxp++) {
                if (ifxp->ifw_xtofree) {
                        (void) m_freem(ifxp->ifw_xtofree);
                        ifxp->ifw_xtofree = 0;
                }
        }
        for (ifxp = sc->sc_ifw; ifxp < &sc->sc_ifw[NXMT]; ifxp++) {
                if (ifxp->ifw_xtofree) {
                        (void) m_freem(ifxp->ifw_xtofree);
                        ifxp->ifw_xtofree = 0;
                }
        }
-
-       /* restart DMC */
-       dmcinit(unit);
-       sc->sc_flag &= ~DMC_RESTART;
-       sc->sc_if.if_collisions++;      /* why not? */
+       if_qflush(&sc->sc_if.if_snd);
 }
 
 /*
 }
 
 /*
- * Check to see that transmitted packets don't
- * lose interrupts.  The device has to be active.
+ * Watchdog timeout to see that transmitted packets don't
+ * lose interrupts.  The device has to be online (the first
+ * transmission may block until the other side comes up).
  */
  */
-dmcwatch()
+dmctimeout(unit)
+       int unit;
 {
 {
-       register struct uba_device *ui;
        register struct dmc_softc *sc;
        struct dmcdevice *addr;
        register struct dmc_softc *sc;
        struct dmcdevice *addr;
-       register int i;
 
 
-       for (i = 0; i < NDMC; i++) {
-               sc = &dmc_softc[i];
-               if ((sc->sc_flag & DMC_ACTIVE) == 0)
-                       continue;
-               if ((ui = dmcinfo[i]) == 0 || ui->ui_alive == 0)
-                       continue;
-               if (sc->sc_oused) {
-                       sc->sc_nticks++;
-                       if (sc->sc_nticks > dmc_timeout) {
-                               sc->sc_nticks = 0;
-                               addr = (struct dmcdevice *)ui->ui_addr;
-                               printd("dmc%d hung: bsel0=%b bsel2=%b\n", i,
-                                   addr->bsel0 & 0xff, DMC0BITS,
-                                   addr->bsel2 & 0xff, DMC2BITS);
-                               dmcrestart(i);
-                       }
-               }
+       sc = &dmc_softc[unit];
+       if (sc->sc_flag & DMC_ONLINE) {
+               addr = (struct dmcdevice *)(dmcinfo[unit]->ui_addr);
+               log(LOG_ERR, "dmc%d: output timeout, bsel0=%b bsel2=%b\n",
+                   unit, addr->bsel0 & 0xff, DMC0BITS,
+                   addr->bsel2 & 0xff, DMC2BITS);
+               dmcrestart(unit);
        }
        }
-       timeout(dmcwatch, (caddr_t) 0, hz);
 }
 #endif
 }
 #endif