BSD 4_4 release
[unix-history] / usr / src / sys / vax / uba / up.c
index 0fa50d8..a4a5f25 100644 (file)
@@ -1,29 +1,45 @@
-/*     up.c    4.25    81/02/26        */
+/*
+ * Copyright (c) 1982, 1986 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)up.c        7.10 (Berkeley) 12/16/90
+ */
 
 #include "up.h"
 #if NSC > 0
 /*
 
 #include "up.h"
 #if NSC > 0
 /*
- * UNIBUS disk driver with overlapped seeks and ECC recovery.
+ * UNIBUS disk driver with:
+ *     overlapped seeks,
+ *     ECC recovery, and
+ *     bad sector forwarding.
+ *
+ * TODO:
+ *     Check that offset recovery code works
  */
  */
-#define        DELAY(N)                { register int d; d = N; while (--d > 0); }
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/cpu.h"
-#include "../h/nexus.h"
-#include "../h/dk.h"
-#include "../h/buf.h"
-#include "../h/conf.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/map.h"
-#include "../h/pte.h"
-#include "../h/mtpr.h"
-#include "../h/vm.h"
-#include "../h/uba.h"
-#include "../h/cmap.h"
-
-#include "../h/upreg.h"
+#include "../include/pte.h"
+
+#include "sys/param.h"
+#include "sys/systm.h"
+#include "sys/dkstat.h"
+#include "sys/dkbad.h"
+#include "sys/ioctl.h"
+#include "sys/disklabel.h"
+#include "sys/buf.h"
+#include "sys/conf.h"
+#include "sys/user.h"
+#include "sys/map.h"
+#include "sys/vm.h"
+#include "sys/cmap.h"
+#include "sys/uio.h"
+#include "sys/kernel.h"
+#include "sys/syslog.h"
+
+#include "../include/cpu.h"
+#include "../vax/nexus.h"
+#include "ubavar.h"
+#include "ubareg.h"
+#include "upreg.h"
 
 struct up_softc {
        int     sc_softas;
 
 struct up_softc {
        int     sc_softas;
@@ -32,73 +48,111 @@ struct     up_softc {
        int     sc_recal;
 } up_softc[NSC];
 
        int     sc_recal;
 } up_softc[NSC];
 
+#define upunit(dev)    (minor(dev) >> 3)
+
 /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
 /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
-struct size
-{
+struct size {
        daddr_t nblocks;
        int     cyloff;
        daddr_t nblocks;
        int     cyloff;
-} up_sizes[8] = {
+} up9300_sizes[8] = {
        15884,  0,              /* A=cyl 0 thru 26 */
        33440,  27,             /* B=cyl 27 thru 81 */
        495520, 0,              /* C=cyl 0 thru 814 */
        15884,  562,            /* D=cyl 562 thru 588 */
        55936,  589,            /* E=cyl 589 thru 680 */
        15884,  0,              /* A=cyl 0 thru 26 */
        33440,  27,             /* B=cyl 27 thru 81 */
        495520, 0,              /* C=cyl 0 thru 814 */
        15884,  562,            /* D=cyl 562 thru 588 */
        55936,  589,            /* E=cyl 589 thru 680 */
-       81472,  681,            /* F=cyl 681 thru 814 */
-       153824, 562,            /* G=cyl 562 thru 814 */
+       81376,  681,            /* F=cyl 681 thru 814 */
+       153728, 562,            /* G=cyl 562 thru 814 */
+       291346, 82,             /* H=cyl 82 thru 561 */
+}, up9766_sizes[8] = {
+       15884,  0,              /* A=cyl 0 thru 26 */
+       33440,  27,             /* B=cyl 27 thru 81 */
+       500384, 0,              /* C=cyl 0 thru 822 */
+       15884,  562,            /* D=cyl 562 thru 588 */
+       55936,  589,            /* E=cyl 589 thru 680 */
+       86240,  681,            /* F=cyl 681 thru 822 */
+       158592, 562,            /* G=cyl 562 thru 822 */
        291346, 82,             /* H=cyl 82 thru 561 */
        291346, 82,             /* H=cyl 82 thru 561 */
-}, fj_sizes[8] = {
+}, up160_sizes[8] = {
        15884,  0,              /* A=cyl 0 thru 49 */
        33440,  50,             /* B=cyl 50 thru 154 */
        263360, 0,              /* C=cyl 0 thru 822 */
        15884,  0,              /* A=cyl 0 thru 49 */
        33440,  50,             /* B=cyl 50 thru 154 */
        263360, 0,              /* C=cyl 0 thru 822 */
+       15884,  155,            /* D=cyl 155 thru 204 */
+       55936,  205,            /* E=cyl 205 thru 379 */
+       141664, 380,            /* F=cyl 380 thru 822 */
+       213664, 155,            /* G=cyl 155 thru 822 */
        0,      0,
        0,      0,
+}, upam_sizes[8] = {
+       15884,  0,              /* A=cyl 0 thru 31 */
+       33440,  32,             /* B=cyl 32 thru 97 */
+       524288, 0,              /* C=cyl 0 thru 1023 */
+       15884,  668,            /* D=cyl 668 thru 699 */
+       55936,  700,            /* E=cyl 700 thru 809 */
+       109472, 810,            /* F=cyl 810 thru 1023 */
+       182176, 668,            /* G=cyl 668 thru 1023 */
+       291346, 98,             /* H=cyl 98 thru 667 */
+}, up980_sizes[8] = {
+       15884,  0,              /* A=cyl 0 thru 99 */
+       33440,  100,            /* B=cyl 100 thru 308 */
+       131680, 0,              /* C=cyl 0 thru 822 */
+       15884,  309,            /* D=cyl 309 thru 408 */
+       55936,  409,            /* E=cyl 409 thru 758 */
+       10080,  759,            /* F=cyl 759 thru 822 */
+       82080,  309,            /* G=cyl 309 thru 822 */
        0,      0,
        0,      0,
-       0,      0,
-       0,      0,
-       213760, 155,            /* H=cyl 155 thru 822 */
+}, upeagle_sizes[8] = {
+       15884,  0,              /* A=cyl 0 thru 16 */
+       66880,  17,             /* B=cyl 17 thru 86 */
+       808320, 0,              /* C=cyl 0 thru 841 */
+       15884,  391,            /* D=cyl 391 thru 407 */
+       307200, 408,            /* E=cyl 408 thru 727 */
+       109296, 728,            /* F=cyl 728 thru 841 */
+       432816, 391,            /* G=cyl 391 thru 841 */
+       291346, 87,             /* H=cyl 87 thru 390 */
 };
 /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
 
 };
 /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
 
-#define        _upSDIST        2               /* 1.0 msec */
-#define        _upRDIST        4               /* 2.0 msec */
-
-int    upSDIST = _upSDIST;
-int    upRDIST = _upRDIST;
-
 int    upprobe(), upslave(), upattach(), updgo(), upintr();
 int    upprobe(), upslave(), upattach(), updgo(), upintr();
-struct uba_minfo *upminfo[NSC];
-struct uba_dinfo *updinfo[NUP];
-struct uba_dinfo *upip[NSC][4];
+struct uba_ctlr *upminfo[NSC];
+struct uba_device *updinfo[NUP];
+#define        UPIPUNITS       8
+struct uba_device *upip[NSC][UPIPUNITS]; /* fuji w/fixed head gives n,n+4 */
 
 u_short        upstd[] = { 0776700, 0774400, 0776300, 0 };
 struct uba_driver scdriver =
     { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
 struct buf     uputab[NUP];
 
 u_short        upstd[] = { 0776700, 0774400, 0776300, 0 };
 struct uba_driver scdriver =
     { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
 struct buf     uputab[NUP];
+char upinit[NUP];
 
 struct upst {
 
 struct upst {
-       short   nsect;
-       short   ntrak;
-       short   nspc;
-       short   ncyl;
-       struct  size *sizes;
+       short   nsect;          /* # sectors/track */
+       short   ntrak;          /* # tracks/cylinder */
+       short   nspc;           /* # sectors/cylinder */
+       short   ncyl;           /* # cylinders */
+       struct  size *sizes;    /* partition tables */
+       short   sdist;          /* seek distance metric */
+       short   rdist;          /* rotational distance metric */
 } upst[] = {
 } upst[] = {
-       32,     19,     32*19,  823,    up_sizes,       /* 9300/cdc */
-/* 9300 actually has 815 cylinders... */
-       32,     10,     32*10,  823,    fj_sizes,       /* fujitsu 160m */
+       { 32,   19,     32*19,  815,    up9300_sizes,   3, 4 }, /* 9300 */
+       { 32,   19,     32*19,  823,    up9766_sizes,   3, 4 }, /* 9766 */
+       { 32,   10,     32*10,  823,    up160_sizes,    3, 4 }, /* fuji 160m */
+       { 32,   16,     32*16,  1024,   upam_sizes,     7, 8 }, /* Capricorn */
+       { 32,   5,      32*5,   823,    up980_sizes,    3, 4 }, /* DM980 */
+        { 48,  20,     48*20,  842,    upeagle_sizes, 15, 8 }, /* EAGLE */
+       { 0,    0,      0,      0,      0,              0, 0 }
 };
 
 u_char up_offset[16] = {
 };
 
 u_char up_offset[16] = {
-    UP_P400, UP_M400, UP_P400, UP_M400, UP_P800, UP_M800, UP_P800, UP_M800, 
-    UP_P1200, UP_M1200, UP_P1200, UP_M1200, 0, 0, 0, 0
+       UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400,
+       UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 
+       UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200,
+       0, 0, 0, 0
 };
 
 };
 
-struct buf     rupbuf[NUP];
+struct         buf     bupbuf[NUP];
+struct dkbad   upbad[NUP];
 
 #define        b_cylin b_resid
 
 
 #define        b_cylin b_resid
 
-#ifdef INTRLVE
-daddr_t dkblock();
-#endif
-
 int    upwstart, upwatch();            /* Have started guardian */
 int    upseek;
 int    upwaitdry;
 int    upwstart, upwatch();            /* Have started guardian */
 int    upseek;
 int    upwaitdry;
@@ -110,23 +164,24 @@ upprobe(reg)
        register int br, cvec;
 
 #ifdef lint    
        register int br, cvec;
 
 #ifdef lint    
-       br = 0; cvec = br; br = cvec;
+       br = 0; cvec = br; br = cvec; upintr(0);
 #endif
        ((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
        DELAY(10);
        ((struct updevice *)reg)->upcs1 = 0;
 #endif
        ((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
        DELAY(10);
        ((struct updevice *)reg)->upcs1 = 0;
-       return (1);
+       return (sizeof (struct updevice));
 }
 
 upslave(ui, reg)
 }
 
 upslave(ui, reg)
-       struct uba_dinfo *ui;
+       struct uba_device *ui;
        caddr_t reg;
 {
        register struct updevice *upaddr = (struct updevice *)reg;
 
        upaddr->upcs1 = 0;              /* conservative */
        upaddr->upcs2 = ui->ui_slave;
        caddr_t reg;
 {
        register struct updevice *upaddr = (struct updevice *)reg;
 
        upaddr->upcs1 = 0;              /* conservative */
        upaddr->upcs2 = ui->ui_slave;
-       if (upaddr->upcs2&UP_NED) {
+       upaddr->upcs1 = UP_NOP|UP_GO;
+       if (upaddr->upcs2&UPCS2_NED) {
                upaddr->upcs1 = UP_DCLR|UP_GO;
                return (0);
        }
                upaddr->upcs1 = UP_DCLR|UP_GO;
                return (0);
        }
@@ -134,54 +189,91 @@ upslave(ui, reg)
 }
 
 upattach(ui)
 }
 
 upattach(ui)
-       register struct uba_dinfo *ui;
+       register struct uba_device *ui;
 {
 {
-#ifdef notdef
-       register struct updevice *upaddr;
-#endif
 
        if (upwstart == 0) {
 
        if (upwstart == 0) {
-               timeout(upwatch, (caddr_t)0, HZ);
+               timeout(upwatch, (caddr_t)0, hz);
                upwstart++;
        }
        if (ui->ui_dk >= 0)
                upwstart++;
        }
        if (ui->ui_dk >= 0)
-               dk_mspw[ui->ui_dk] = .0000020345;
+               dk_wpms[ui->ui_dk] = 491521;
        upip[ui->ui_ctlr][ui->ui_slave] = ui;
        up_softc[ui->ui_ctlr].sc_ndrive++;
        upip[ui->ui_ctlr][ui->ui_slave] = ui;
        up_softc[ui->ui_ctlr].sc_ndrive++;
-#ifdef notdef
-       upaddr = (struct updevice *)ui->ui_addr;
+       ui->ui_type = upmaptype(ui);
+}
+
+upmaptype(ui)
+       register struct uba_device *ui;
+{
+       register struct updevice *upaddr = (struct updevice *)ui->ui_addr;
+       int type = ui->ui_type;
+       register struct upst *st;
+
        upaddr->upcs1 = 0;
        upaddr->upcs2 = ui->ui_slave;
        upaddr->upcs1 = 0;
        upaddr->upcs2 = ui->ui_slave;
-       upaddr->uphr = -1;
-       /* ... */
-       if (upaddr-> ... == 10)
-               ui->ui_type = 1;
-#endif
+       upaddr->uphr = UPHR_MAXTRAK;
+       for (st = upst; st->nsect != 0; st++)
+               if (upaddr->uphr == st->ntrak - 1) {
+                       type = st - upst;
+                       break;
+               }
+       if (st->nsect == 0)
+               printf(": uphr=%x", upaddr->uphr);
+       if (type == 0) {
+               upaddr->uphr = UPHR_MAXCYL;
+               if (upaddr->uphr == 822)
+                       type++;
+       }
+       upaddr->upcs2 = UPCS2_CLR;
+       return (type);
 }
  
 }
  
+upopen(dev)
+       dev_t dev;
+{
+       register int unit = upunit(dev);
+       register struct uba_device *ui;
+
+       if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
+               return (ENXIO);
+       return (0);
+}
+
 upstrategy(bp)
        register struct buf *bp;
 {
 upstrategy(bp)
        register struct buf *bp;
 {
-       register struct uba_dinfo *ui;
+       register struct uba_device *ui;
        register struct upst *st;
        register int unit;
        register struct buf *dp;
        int xunit = minor(bp->b_dev) & 07;
        long bn, sz;
        register struct upst *st;
        register int unit;
        register struct buf *dp;
        int xunit = minor(bp->b_dev) & 07;
        long bn, sz;
+       int s;
 
        sz = (bp->b_bcount+511) >> 9;
 
        sz = (bp->b_bcount+511) >> 9;
-       unit = dkunit(bp);
-       if (unit >= NUP)
+       unit = upunit(bp->b_dev);
+       if (unit >= NUP) {
+               bp->b_error = ENXIO;
                goto bad;
                goto bad;
+       }
        ui = updinfo[unit];
        ui = updinfo[unit];
-       if (ui == 0 || ui->ui_alive == 0)
+       if (ui == 0 || ui->ui_alive == 0) {
+               bp->b_error = ENXIO;
                goto bad;
                goto bad;
+       }
        st = &upst[ui->ui_type];
        if (bp->b_blkno < 0 ||
        st = &upst[ui->ui_type];
        if (bp->b_blkno < 0 ||
-           (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
+           (bn = bp->b_blkno)+sz > st->sizes[xunit].nblocks) {
+               if (bp->b_blkno == st->sizes[xunit].nblocks) {
+                       bp->b_resid = bp->b_bcount;
+                       goto done;
+               }
+               bp->b_error = EINVAL;
                goto bad;
                goto bad;
+       }
        bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
        bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
-       (void) spl5();
+       s = spl5();
        dp = &uputab[ui->ui_unit];
        disksort(dp, bp);
        if (dp->b_active == 0) {
        dp = &uputab[ui->ui_unit];
        disksort(dp, bp);
        if (dp->b_active == 0) {
@@ -190,11 +282,12 @@ upstrategy(bp)
                if (bp->b_actf && bp->b_active == 0)
                        (void) upstart(ui->ui_mi);
        }
                if (bp->b_actf && bp->b_active == 0)
                        (void) upstart(ui->ui_mi);
        }
-       (void) spl0();
+       splx(s);
        return;
 
 bad:
        bp->b_flags |= B_ERROR;
        return;
 
 bad:
        bp->b_flags |= B_ERROR;
+done:
        iodone(bp);
        return;
 }
        iodone(bp);
        return;
 }
@@ -212,10 +305,10 @@ bad:
  * positioning forever without transferrring.)
  */
 upustart(ui)
  * positioning forever without transferrring.)
  */
 upustart(ui)
-       register struct uba_dinfo *ui;
+       register struct uba_device *ui;
 {
        register struct buf *bp, *dp;
 {
        register struct buf *bp, *dp;
-       register struct uba_minfo *um = ui->ui_mi;
+       register struct uba_ctlr *um;
        register struct updevice *upaddr;
        register struct upst *st;
        daddr_t bn;
        register struct updevice *upaddr;
        register struct upst *st;
        daddr_t bn;
@@ -230,6 +323,7 @@ upustart(ui)
 
        if (ui == 0)
                return (0);
 
        if (ui == 0)
                return (0);
+       um = ui->ui_mi;
        dk_busy &= ~(1<<ui->ui_dk);
        dp = &uputab[ui->ui_unit];
        if ((bp = dp->b_actf) == NULL)
        dk_busy &= ~(1<<ui->ui_dk);
        dp = &uputab[ui->ui_unit];
        if ((bp = dp->b_actf) == NULL)
@@ -256,17 +350,30 @@ upustart(ui)
         * If drive has just come up,
         * setup the pack.
         */
         * If drive has just come up,
         * setup the pack.
         */
-       if ((upaddr->upds & UP_VV) == 0) {
+       if ((upaddr->upds & UPDS_VV) == 0 || upinit[ui->ui_unit] == 0) {
+               struct buf *bbp = &bupbuf[ui->ui_unit];
+
                /* SHOULD WARN SYSTEM THAT THIS HAPPENED */
                /* SHOULD WARN SYSTEM THAT THIS HAPPENED */
+               upinit[ui->ui_unit] = 1;
                upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
                upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
                upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
                upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
-               upaddr->upof = UP_FMT22;
+               upaddr->upof = UPOF_FMT22;
                didie = 1;
                didie = 1;
+               st = &upst[ui->ui_type];
+               bbp->b_flags = B_READ|B_BUSY;
+               bbp->b_dev = bp->b_dev;
+               bbp->b_bcount = 512;
+               bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit];
+               bbp->b_blkno = st->ncyl * st->nspc - st->nsect;
+               bbp->b_cylin = st->ncyl - 1;
+               dp->b_actf = bbp;
+               bbp->av_forw = bp;
+               bp = bbp;
        }
        /*
         * If drive is offline, forget about positioning.
         */
        }
        /*
         * If drive is offline, forget about positioning.
         */
-       if ((upaddr->upds & (UP_DPR|UP_MOL)) != (UP_DPR|UP_MOL))
+       if ((upaddr->upds & (UPDS_DPR|UPDS_MOL)) != (UPDS_DPR|UPDS_MOL))
                goto done;
        /*
         * If there is only one drive,
                goto done;
        /*
         * If there is only one drive,
@@ -279,9 +386,9 @@ upustart(ui)
         * and see if we are close enough to justify not searching.
         */
        st = &upst[ui->ui_type];
         * and see if we are close enough to justify not searching.
         */
        st = &upst[ui->ui_type];
-       bn = dkblock(bp);
+       bn = bp->b_blkno;
        sn = bn%st->nspc;
        sn = bn%st->nspc;
-       sn = (sn + st->nsect - upSDIST) % st->nsect;
+       sn = (sn + st->nsect - st->sdist) % st->nsect;
        if (bp->b_cylin - upaddr->updc)
                goto search;            /* Not on-cylinder */
        else if (upseek)
        if (bp->b_cylin - upaddr->updc)
                goto search;            /* Not on-cylinder */
        else if (upseek)
@@ -289,7 +396,7 @@ upustart(ui)
        csn = (upaddr->upla>>6) - sn - 1;
        if (csn < 0)
                csn += st->nsect;
        csn = (upaddr->upla>>6) - sn - 1;
        if (csn < 0)
                csn += st->nsect;
-       if (csn > st->nsect - upRDIST)
+       if (csn > st->nsect - st->rdist)
                goto done;
 search:
        upaddr->updc = bp->b_cylin;
                goto done;
 search:
        upaddr->updc = bp->b_cylin;
@@ -335,10 +442,10 @@ out:
  * Start up a transfer on a drive.
  */
 upstart(um)
  * Start up a transfer on a drive.
  */
 upstart(um)
-       register struct uba_minfo *um;
+       register struct uba_ctlr *um;
 {
        register struct buf *bp, *dp;
 {
        register struct buf *bp, *dp;
-       register struct uba_dinfo *ui;
+       register struct uba_device *ui;
        register struct updevice *upaddr;
        struct upst *st;
        daddr_t bn;
        register struct updevice *upaddr;
        struct upst *st;
        daddr_t bn;
@@ -359,8 +466,8 @@ loop:
         * determine destination of this request.
         */
        um->um_tab.b_active++;
         * determine destination of this request.
         */
        um->um_tab.b_active++;
-       ui = updinfo[dkunit(bp)];
-       bn = dkblock(bp);
+       ui = updinfo[upunit(bp->b_dev)];
+       bn = bp->b_blkno;
        dn = ui->ui_slave;
        st = &upst[ui->ui_type];
        sn = bn%st->nspc;
        dn = ui->ui_slave;
        st = &upst[ui->ui_type];
        sn = bn%st->nspc;
@@ -376,14 +483,15 @@ loop:
         * Check that it is ready and online
         */
        waitdry = 0;
         * Check that it is ready and online
         */
        waitdry = 0;
-       while ((upaddr->upds&UP_DRY) == 0) {
+       while ((upaddr->upds&UPDS_DRY) == 0) {
+               printf("up%d: ds wait ds=%o\n",upunit(bp->b_dev),upaddr->upds);
                if (++waitdry > 512)
                        break;
                upwaitdry++;
        }
                if (++waitdry > 512)
                        break;
                upwaitdry++;
        }
-       if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
-               printf("up%d not ready", dkunit(bp));
-               if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
+       if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
+               printf("up%d: not ready", upunit(bp->b_dev));
+               if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
                        printf("\n");
                        um->um_tab.b_active = 0;
                        um->um_tab.b_errcnt = 0;
                        printf("\n");
                        um->um_tab.b_active = 0;
                        um->um_tab.b_errcnt = 0;
@@ -399,15 +507,6 @@ loop:
                 */
                printf(" (flakey)\n");
        }
                 */
                printf(" (flakey)\n");
        }
-       /*
-        * After 16th retry, do offset positioning
-        */
-       if (um->um_tab.b_errcnt >= 16 && (bp->b_flags&B_READ) != 0) {
-               upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UP_FMT22;
-               upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
-               while (upaddr->upds & UP_PIP)
-                       DELAY(25);
-       }
        /*
         * Setup for the transfer, and get in the
         * UNIBUS adaptor queue.
        /*
         * Setup for the transfer, and get in the
         * UNIBUS adaptor queue.
@@ -420,7 +519,7 @@ loop:
        else
                cmd = UP_IE|UP_WCOM|UP_GO;
        um->um_cmd = cmd;
        else
                cmd = UP_IE|UP_WCOM|UP_GO;
        um->um_cmd = cmd;
-       ubago(ui);
+       (void) ubago(ui);
        return (1);
 }
 
        return (1);
 }
 
@@ -428,10 +527,11 @@ loop:
  * Now all ready to go, stuff the registers.
  */
 updgo(um)
  * Now all ready to go, stuff the registers.
  */
 updgo(um)
-       struct uba_minfo *um;
+       struct uba_ctlr *um;
 {
        register struct updevice *upaddr = (struct updevice *)um->um_addr;
 
 {
        register struct updevice *upaddr = (struct updevice *)um->um_addr;
 
+       um->um_tab.b_active = 2;        /* should now be 2 */
        upaddr->upba = um->um_ubinfo;
        upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
 }
        upaddr->upba = um->um_ubinfo;
        upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
 }
@@ -443,8 +543,8 @@ upintr(sc21)
        register sc21;
 {
        register struct buf *bp, *dp;
        register sc21;
 {
        register struct buf *bp, *dp;
-       register struct uba_minfo *um = upminfo[sc21];
-       register struct uba_dinfo *ui;
+       register struct uba_ctlr *um = upminfo[sc21];
+       register struct uba_device *ui;
        register struct updevice *upaddr = (struct updevice *)um->um_addr;
        register unit;
        struct up_softc *sc = &up_softc[um->um_ctlr];
        register struct updevice *upaddr = (struct updevice *)um->um_addr;
        register unit;
        struct up_softc *sc = &up_softc[um->um_ctlr];
@@ -458,59 +558,71 @@ upintr(sc21)
         * interrupt for attention status on seeking drives.
         * Just service them.
         */
         * interrupt for attention status on seeking drives.
         * Just service them.
         */
-       if (um->um_tab.b_active == 0) {
+       if (um->um_tab.b_active != 2 && !sc->sc_recal) {
                if (upaddr->upcs1 & UP_TRE)
                        upaddr->upcs1 = UP_TRE;
                goto doattn;
        }
                if (upaddr->upcs1 & UP_TRE)
                        upaddr->upcs1 = UP_TRE;
                goto doattn;
        }
-       if ((upaddr->upcs1 & UP_RDY) == 0)
-               printf("upintr !RDY\n");                /* shouldn't happen */
+       um->um_tab.b_active = 1;
        /*
         * Get device and block structures, and a pointer
        /*
         * Get device and block structures, and a pointer
-        * to the uba_dinfo for the drive.  Select the drive.
+        * to the uba_device for the drive.  Select the drive.
         */
        dp = um->um_tab.b_actf;
        bp = dp->b_actf;
         */
        dp = um->um_tab.b_actf;
        bp = dp->b_actf;
-       ui = updinfo[dkunit(bp)];
+       ui = updinfo[upunit(bp->b_dev)];
        dk_busy &= ~(1 << ui->ui_dk);
        if ((upaddr->upcs2&07) != ui->ui_slave)
                upaddr->upcs2 = ui->ui_slave;
        dk_busy &= ~(1 << ui->ui_dk);
        if ((upaddr->upcs2&07) != ui->ui_slave)
                upaddr->upcs2 = ui->ui_slave;
+       if (bp->b_flags&B_BAD) {
+               if (upecc(ui, CONT))
+                       return;
+       }
        /*
         * Check for and process errors on
         * either the drive or the controller.
         */
        /*
         * Check for and process errors on
         * either the drive or the controller.
         */
-       if ((upaddr->upds&UP_ERR) || (upaddr->upcs1&UP_TRE)) {
+       if ((upaddr->upds&UPDS_ERR) || (upaddr->upcs1&UP_TRE)) {
                waitdry = 0;
                waitdry = 0;
-               while ((upaddr->upds & UP_DRY) == 0) {
+               while ((upaddr->upds & UPDS_DRY) == 0) {
                        if (++waitdry > 512)
                                break;
                        upwaitdry++;
                }
                        if (++waitdry > 512)
                                break;
                        upwaitdry++;
                }
-               if ((upaddr->upds&UP_DREADY) != UP_DREADY) {
-                       printf("up%d not ready", dkunit(bp));
-                       bp->b_flags |= B_ERROR;
-               } else if (upaddr->uper1&UP_WLE) {
+               if (upaddr->uper1&UPER1_WLE) {
                        /*
                         * Give up on write locked devices
                         * immediately.
                         */
                        /*
                         * Give up on write locked devices
                         * immediately.
                         */
-                       printf("up%d is write locked\n", dkunit(bp));
+                       printf("up%d: write locked\n", upunit(bp->b_dev));
                        bp->b_flags |= B_ERROR;
                } else if (++um->um_tab.b_errcnt > 27) {
                        /*
                         * After 28 retries (16 without offset, and
                         * 12 with offset positioning) give up.
                        bp->b_flags |= B_ERROR;
                } else if (++um->um_tab.b_errcnt > 27) {
                        /*
                         * After 28 retries (16 without offset, and
                         * 12 with offset positioning) give up.
+                        * If the error was header CRC, the header is 
+                        * screwed up, and the sector may in fact exist
+                        * in the bad sector table, better check...
                         */
                         */
-                       if (upaddr->upcs2&(UP_NEM|UP_MXF)) {
-                               printf("FLAKEY UP ");
-                               ubareset(um->um_ubanum);
-                               return;
+                       if (upaddr->uper1&UPER1_HCRC) {
+                               if (upecc(ui, BSE))
+                                       return;
                        }
                        }
-                       harderr(bp);
-                       printf("up%d cs2 %b er1 %b er2 %b\n",
-                           dkunit(bp), upaddr->upcs2, UPCS2_BITS, upaddr->uper1,
-                           UPER1_BITS, upaddr->uper2, UPER2_BITS);
+       hard:
+                       diskerr(bp, "up", "hard error", LOG_PRINTF, -1,
+                           (struct disklabel *)0);
+                       printf(" cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n",
+                               upaddr->updc, ((upaddr->upda)>>8)&077,
+                               (upaddr->upda)&037,
+                               upaddr->upcs2, UPCS2_BITS,
+                               upaddr->uper1, UPER1_BITS,
+                               upaddr->uper2, UPER2_BITS);
                        bp->b_flags |= B_ERROR;
                        bp->b_flags |= B_ERROR;
+               } else if (upaddr->uper2 & UPER2_BSE) {
+                       if (upecc(ui, BSE))
+                               return;
+                       else
+                               goto hard;
                } else {
                        /*
                         * Retriable error.
                } else {
                        /*
                         * Retriable error.
@@ -518,10 +630,11 @@ upintr(sc21)
                         * by returning if necessary.
                         * Otherwise fall through and retry the transfer
                         */
                         * by returning if necessary.
                         * Otherwise fall through and retry the transfer
                         */
-                       um->um_tab.b_active = 0;         /* force retry */
-                       if ((upaddr->uper1&(UP_DCK|UP_ECH))==UP_DCK)
-                               if (upecc(ui))
+                       if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) {
+                               if (upecc(ui, ECC))
                                        return;
                                        return;
+                       } else
+                               um->um_tab.b_active = 0; /* force retry */
                }
                /*
                 * Clear drive error and, every eight attempts,
                }
                /*
                 * Clear drive error and, every eight attempts,
@@ -530,23 +643,41 @@ upintr(sc21)
                 */
                upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
                needie = 0;
                 */
                upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
                needie = 0;
-               if ((um->um_tab.b_errcnt&07) == 4) {
+               if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) {
                        upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
                        upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
-                       um->um_tab.b_active = 1;
-                       sc->sc_recal = 1;
-                       return;
+                       sc->sc_recal = 0;
+                       goto nextrecal;
                }
        }
        /*
                }
        }
        /*
-        * Done retrying transfer... release
-        * resources... if we were recalibrating,
-        * then retry the transfer.
-        * Mathematical note: 28%8 != 4.
+        * Advance recalibration finite state machine
+        * if recalibrate in progress, through
+        *      RECAL
+        *      SEEK
+        *      OFFSET (optional)
+        *      RETRY
         */
         */
-       ubadone(um);
-       if (sc->sc_recal) {
+       switch (sc->sc_recal) {
+
+       case 1:
+               upaddr->updc = bp->b_cylin;
+               upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO;
+               goto nextrecal;
+       case 2:
+               if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0)
+                       goto donerecal;
+               upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22;
+               upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
+               goto nextrecal;
+       nextrecal:
+               sc->sc_recal++;
+               um->um_tab.b_active = 1;
+               return;
+       donerecal:
+       case 3:
                sc->sc_recal = 0;
                sc->sc_recal = 0;
-               um->um_tab.b_active = 0;        /* force retry */
+               um->um_tab.b_active = 0;
+               break;
        }
        /*
         * If still ``active'', then don't need any more retries.
        }
        /*
         * If still ``active'', then don't need any more retries.
@@ -557,9 +688,9 @@ upintr(sc21)
                 * return to centerline.
                 */
                if (um->um_tab.b_errcnt >= 16) {
                 * return to centerline.
                 */
                if (um->um_tab.b_errcnt >= 16) {
-                       upaddr->upof = UP_FMT22;
+                       upaddr->upof = UPOF_FMT22;
                        upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
                        upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
-                       while (upaddr->upds & UP_PIP)
+                       while (upaddr->upds & UPDS_PIP)
                                DELAY(25);
                        needie = 0;
                }
                                DELAY(25);
                        needie = 0;
                }
@@ -580,6 +711,10 @@ upintr(sc21)
                                needie = 0;
        }
        as &= ~(1<<ui->ui_slave);
                                needie = 0;
        }
        as &= ~(1<<ui->ui_slave);
+       /*
+        * Release unibus resources and flush data paths.
+        */
+       ubadone(um);
 doattn:
        /*
         * Process other units which need attention.
 doattn:
        /*
         * Process other units which need attention.
@@ -587,12 +722,13 @@ doattn:
         * the unit start routine to place the slave
         * on the controller device queue.
         */
         * the unit start routine to place the slave
         * on the controller device queue.
         */
-       for (unit = 0; as; as >>= 1, unit++)
-               if (as & 1) {
-                       upaddr->upas = 1<<unit;
-                       if (upustart(upip[sc21][unit]))
-                               needie = 0;
-               }
+       while (unit = ffs((long)as)) {
+               unit--;         /* was 1 origin */
+               as &= ~(1<<unit);
+               upaddr->upas = 1<<unit;
+               if (unit < UPIPUNITS && upustart(upip[sc21][unit]))
+                       needie = 0;
+       }
        /*
         * If the controller is not transferring, but
         * there are devices ready to transfer, start
        /*
         * If the controller is not transferring, but
         * there are devices ready to transfer, start
@@ -605,40 +741,19 @@ doattn:
                upaddr->upcs1 = UP_IE;
 }
 
                upaddr->upcs1 = UP_IE;
 }
 
-upread(dev)
-       dev_t dev;
-{
-       register int unit = minor(dev) >> 3;
-
-       if (unit >= NUP)
-               u.u_error = ENXIO;
-       else
-               physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys);
-}
-
-upwrite(dev)
-       dev_t dev;
-{
-       register int unit = minor(dev) >> 3;
-
-       if (unit >= NUP)
-               u.u_error = ENXIO;
-       else
-               physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys);
-}
-
 /*
  * Correct an ECC error, and restart the i/o to complete
  * the transfer if necessary.  This is quite complicated because
  * the transfer may be going to an odd memory address base and/or
  * across a page boundary.
  */
 /*
  * Correct an ECC error, and restart the i/o to complete
  * the transfer if necessary.  This is quite complicated because
  * the transfer may be going to an odd memory address base and/or
  * across a page boundary.
  */
-upecc(ui)
-       register struct uba_dinfo *ui;
+upecc(ui, flag)
+       register struct uba_device *ui;
+       int flag;
 {
        register struct updevice *up = (struct updevice *)ui->ui_addr;
        register struct buf *bp = uputab[ui->ui_unit].b_actf;
 {
        register struct updevice *up = (struct updevice *)ui->ui_addr;
        register struct buf *bp = uputab[ui->ui_unit].b_actf;
-       register struct uba_minfo *um = ui->ui_mi;
+       register struct uba_ctlr *um = ui->ui_mi;
        register struct upst *st;
        struct uba_regs *ubp = ui->ui_hd->uh_uba;
        register int i;
        register struct upst *st;
        struct uba_regs *ubp = ui->ui_hd->uh_uba;
        register int i;
@@ -652,44 +767,115 @@ upecc(ui)
         * mapping (the first part of) the transfer.
         * O is offset within a memory page of the first byte transferred.
         */
         * mapping (the first part of) the transfer.
         * O is offset within a memory page of the first byte transferred.
         */
-       npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
-       reg = btop(um->um_ubinfo&0x3ffff) + npf;
+       if (flag == CONT)
+               npf = bp->b_error;
+       else
+               npf = btodb(bp->b_bcount + (up->upwc * sizeof(short)) + 511);
+       reg = btop(UBAI_ADDR(um->um_ubinfo)) + npf;
        o = (int)bp->b_un.b_addr & PGOFSET;
        o = (int)bp->b_un.b_addr & PGOFSET;
-       printf("%D ", bp->b_blkno+npf);
-       prdev("ECC", bp->b_dev);
        mask = up->upec2;
        mask = up->upec2;
-       if (mask == 0) {
-               up->upof = UP_FMT22;            /* == RTC ???? */
-               return (0);
-       }
-       /*
-        * Flush the buffered data path, and compute the
-        * byte and bit position of the error.  The variable i
-        * is the byte offset in the transfer, the variable byte
-        * is the offset from a page boundary in main memory.
-        */
+#ifdef UPECCDEBUG
+       printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask,
+           up->upec1);
+#endif
+       bn = bp->b_blkno;
+       st = &upst[ui->ui_type];
+       cn = bp->b_cylin;
+       sn = bn%st->nspc + npf;
+       tn = sn/st->nsect;
+       sn %= st->nsect;
+       cn += tn/st->ntrak;
+       tn %= st->ntrak;
        ubapurge(um);
        ubapurge(um);
-       i = up->upec1 - 1;              /* -1 makes 0 origin */
-       bit = i&07;
-       i = (i&~07)>>3;
-       byte = i + o;
+       um->um_tab.b_active=2;
        /*
        /*
-        * Correct while possible bits remain of mask.  Since mask
-        * contains 11 bits, we continue while the bit offset is > -11.
-        * Also watch out for end of this block and the end of the whole
-        * transfer.
+        * action taken depends on the flag
         */
         */
-       while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
-               addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
-                   (byte & PGOFSET);
-               putmemc(addr, getmemc(addr)^(mask<<bit));
-               byte++;
-               i++;
-               bit -= 8;
+       switch(flag){
+       case ECC:
+               npf--;
+               reg--;
+               mask = up->upec2;
+               diskerr(bp, "up", "soft ecc", LOG_WARNING, npf,
+                   (struct disklabel *)0);
+               addlog("\n");
+               /*
+                * Flush the buffered data path, and compute the
+                * byte and bit position of the error.  The variable i
+                * is the byte offset in the transfer, the variable byte
+                * is the offset from a page boundary in main memory.
+                */
+               i = up->upec1 - 1;              /* -1 makes 0 origin */
+               bit = i&07;
+               i = (i&~07)>>3;
+               byte = i + o;
+               /*
+                * Correct while possible bits remain of mask.  Since mask
+                * contains 11 bits, we continue while the bit offset is > -11.
+                * Also watch out for end of this block and the end of the whole
+                * transfer.
+                */
+               while (i < 512 && (int)dbtob(npf)+i < bp->b_bcount && bit > -11) {
+                       struct pte pte;
+
+                       pte = ubp->uba_map[reg + btop(byte)];
+                       addr = ptob(pte.pg_pfnum) + (byte & PGOFSET);
+#ifdef UPECCDEBUG
+                       printf("addr %x map reg %x\n",
+                               addr, *(int *)(&ubp->uba_map[reg+btop(byte)]));
+                       printf("old: %x, ", getmemc(addr));
+#endif
+                       putmemc(addr, getmemc(addr)^(mask<<bit));
+#ifdef UPECCDEBUG
+                       printf("new: %x\n", getmemc(addr));
+#endif
+                       byte++;
+                       i++;
+                       bit -= 8;
+               }
+               if (up->upwc == 0)
+                       return (0);
+               npf++;
+               reg++;
+               break;
+       case BSE:
+               /*
+                * if not in bad sector table, return 0
+                */
+               if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0)
+                       return(0);
+               /*
+                * flag this one as bad
+                */
+               bp->b_flags |= B_BAD;
+               bp->b_error = npf + 1;
+#ifdef UPECCDEBUG
+               printf("BSE: restart at %d\n",npf+1);
+#endif
+               bn = st->ncyl * st->nspc -st->nsect - 1 - bn;
+               cn = bn / st->nspc;
+               sn = bn % st->nspc;
+               tn = sn / st->nsect;
+               sn %= st->nsect;
+               up->upwc = -(512 / sizeof (short));
+#ifdef UPECCDEBUG
+               printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
+#endif
+               break;
+       case CONT:
+#ifdef UPECCDEBUG
+               printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
+#endif
+               bp->b_flags &= ~B_BAD;
+               if ((int)dbtob(npf) >= bp->b_bcount)
+                       return (0);
+               up->upwc = -((bp->b_bcount - (int)dbtob(npf)) / sizeof(short));
+               break;
        }
        }
-       um->um_tab.b_active++;  /* Either complete or continuing... */
-       if (up->upwc == 0)
+       if (up->upwc == 0) {
+               um->um_tab.b_active = 0;
                return (0);
                return (0);
+       }
        /*
         * Have to continue the transfer... clear the drive,
         * and compute the position where the transfer is to continue.
        /*
         * Have to continue the transfer... clear the drive,
         * and compute the position where the transfer is to continue.
@@ -701,20 +887,13 @@ upecc(ui)
        up->upcs1 |= UP_GO;
 #else
        up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
        up->upcs1 |= UP_GO;
 #else
        up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
-       bn = dkblock(bp);
-       st = &upst[ui->ui_type];
-       cn = bp->b_cylin;
-       sn = bn%st->nspc + npf + 1;
-       tn = sn/st->nsect;
-       sn %= st->nsect;
-       cn += tn/st->ntrak;
-       tn %= st->ntrak;
        up->updc = cn;
        up->upda = (tn << 8) | sn;
        up->updc = cn;
        up->upda = (tn << 8) | sn;
-       ubaddr = (int)ptob(reg+1) + o;
+       ubaddr = (int)ptob(reg) + o;
        up->upba = ubaddr;
        cmd = (ubaddr >> 8) & 0x300;
        up->upba = ubaddr;
        cmd = (ubaddr >> 8) & 0x300;
-       cmd |= UP_IE|UP_GO|UP_RCOM;
+       cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO;
+       um->um_tab.b_errcnt = 0;
        up->upcs1 = cmd;
 #endif
        return (1);
        up->upcs1 = cmd;
 #endif
        return (1);
@@ -726,32 +905,30 @@ upecc(ui)
  * and restart all units and the controller.
  */
 upreset(uban)
  * and restart all units and the controller.
  */
 upreset(uban)
+       int uban;
 {
 {
-       register struct uba_minfo *um;
-       register struct uba_dinfo *ui;
+       register struct uba_ctlr *um;
+       register struct uba_device *ui;
        register sc21, unit;
        register sc21, unit;
-       int any = 0;
 
        for (sc21 = 0; sc21 < NSC; sc21++) {
                if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
                    um->um_alive == 0)
                        continue;
 
        for (sc21 = 0; sc21 < NSC; sc21++) {
                if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
                    um->um_alive == 0)
                        continue;
-               if (any == 0) {
-                       printf(" up");
-                       DELAY(10000000);        /* give it time to self-test */
-                       any++;
-               }
+               printf(" sc%d", sc21);
                um->um_tab.b_active = 0;
                um->um_tab.b_actf = um->um_tab.b_actl = 0;
                um->um_tab.b_active = 0;
                um->um_tab.b_actf = um->um_tab.b_actl = 0;
+               up_softc[sc21].sc_recal = 0;
+               up_softc[sc21].sc_wticks = 0;
                if (um->um_ubinfo) {
                        printf("<%d>", (um->um_ubinfo>>28)&0xf);
                if (um->um_ubinfo) {
                        printf("<%d>", (um->um_ubinfo>>28)&0xf);
-                       ubadone(um);
+                       um->um_ubinfo = 0;
                }
                }
-               ((struct updevice *)(um->um_addr))->upcs2 = UP_CLR;
+               ((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR;
                for (unit = 0; unit < NUP; unit++) {
                        if ((ui = updinfo[unit]) == 0)
                                continue;
                for (unit = 0; unit < NUP; unit++) {
                        if ((ui = updinfo[unit]) == 0)
                                continue;
-                       if (ui->ui_alive == 0)
+                       if (ui->ui_alive == 0 || ui->ui_mi != um)
                                continue;
                        uputab[unit].b_active = 0;
                        (void) upustart(ui);
                                continue;
                        uputab[unit].b_active = 0;
                        (void) upustart(ui);
@@ -763,16 +940,16 @@ upreset(uban)
 /*
  * Wake up every second and if an interrupt is pending
  * but nothing has happened increment a counter.
 /*
  * Wake up every second and if an interrupt is pending
  * but nothing has happened increment a counter.
- * If nothing happens for 20 seconds, reset the controller
+ * If nothing happens for 20 seconds, reset the UNIBUS
  * and begin anew.
  */
 upwatch()
 {
  * and begin anew.
  */
 upwatch()
 {
-       register struct uba_minfo *um;
+       register struct uba_ctlr *um;
        register sc21, unit;
        register struct up_softc *sc;
 
        register sc21, unit;
        register struct up_softc *sc;
 
-       timeout(upwatch, (caddr_t)0, HZ);
+       timeout(upwatch, (caddr_t)0, hz);
        for (sc21 = 0; sc21 < NSC; sc21++) {
                um = upminfo[sc21];
                if (um == 0 || um->um_alive == 0)
        for (sc21 = 0; sc21 < NSC; sc21++) {
                um = upminfo[sc21];
                if (um == 0 || um->um_alive == 0)
@@ -786,11 +963,11 @@ upwatch()
                        sc->sc_wticks = 0;
                        continue;
                }
                        sc->sc_wticks = 0;
                        continue;
                }
-    active:
+active:
                sc->sc_wticks++;
                if (sc->sc_wticks >= 20) {
                        sc->sc_wticks = 0;
                sc->sc_wticks++;
                if (sc->sc_wticks >= 20) {
                        sc->sc_wticks = 0;
-                       printf("LOST upintr ");
+                       printf("sc%d: lost interrupt\n", sc21);
                        ubareset(um->um_ubanum);
                }
        }
                        ubareset(um->um_ubanum);
                }
        }
@@ -806,48 +983,43 @@ updump(dev)
        int num, blk, unit;
        struct size *sizes;
        register struct uba_regs *uba;
        int num, blk, unit;
        struct size *sizes;
        register struct uba_regs *uba;
-       register struct uba_dinfo *ui;
+       register struct uba_device *ui;
        register short *rp;
        struct upst *st;
        register short *rp;
        struct upst *st;
+       register int retry;
 
 
-       unit = minor(dev) >> 3;
-       if (unit >= NUP) {
-               printf("bad unit\n");
-               return (-1);
-       }
+       unit = upunit(dev);
+       if (unit >= NUP)
+               return (ENXIO);
 #define        phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
 #define        phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
-       ui = phys(struct uba_dinfo *, updinfo[unit]);
-       if (ui->ui_alive == 0) {
-               printf("dna\n");
-               return(-1);
-       }
+       ui = phys(struct uba_device *, updinfo[unit]);
+       if (ui->ui_alive == 0)
+               return (ENXIO);
        uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
        uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
-#if VAX780
-       if (cpu == VAX_780)
-               ubainit(uba);
-#endif
-       DELAY(1000000);
+       ubainit(uba);
        upaddr = (struct updevice *)ui->ui_physaddr;
        upaddr = (struct updevice *)ui->ui_physaddr;
-       while ((upaddr->upcs1&UP_DVA) == 0)
-               ;
+       DELAY(5000000);
        num = maxfree;
        num = maxfree;
-       start = 0;
        upaddr->upcs2 = unit;
        upaddr->upcs2 = unit;
-       if ((upaddr->upds & UP_VV) == 0) {
-               upaddr->upcs1 = UP_DCLR|UP_GO;
-               upaddr->upcs1 = UP_PRESET|UP_GO;
-               upaddr->upof = UP_FMT22;
-       }
-       if ((upaddr->upds & (UP_DPR|UP_MOL)) != (UP_DPR|UP_MOL)) {
-               printf("dna\n");
-               return (-1);
-       }
+       DELAY(100);
+       upaddr->upcs1 = UP_DCLR|UP_GO;
+       upaddr->upcs1 = UP_PRESET|UP_GO;
+       upaddr->upof = UPOF_FMT22;
+       retry = 0;
+       do {
+               DELAY(25);
+               if (++retry > 527)
+                       break;
+       } while ((upaddr->upds & UP_RDY) == 0);
+       if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY)
+               return (EFAULT);
+       start = 0;
        st = &upst[ui->ui_type];
        sizes = phys(struct size *, st->sizes);
        st = &upst[ui->ui_type];
        sizes = phys(struct size *, st->sizes);
-       if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks) {
-               printf("oor\n");
-               return (-1);
-       }
+       if (dumplo < 0)
+               return (EINVAL);
+       if (dumplo + num >= sizes[minor(dev)&07].nblocks)
+               num = sizes[minor(dev)&07].nblocks - dumplo;
        while (num > 0) {
                register struct pte *io;
                register int i;
        while (num > 0) {
                register struct pte *io;
                register int i;
@@ -857,7 +1029,7 @@ updump(dev)
                blk = num > DBSIZE ? DBSIZE : num;
                io = uba->uba_map;
                for (i = 0; i < blk; i++)
                blk = num > DBSIZE ? DBSIZE : num;
                io = uba->uba_map;
                for (i = 0; i < blk; i++)
-                       *(int *)io++ = (btop(start)+i) | (1<<21) | UBA_MRV;
+                       *(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV;
                *(int *)io = 0;
                bn = dumplo + btop(start);
                cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
                *(int *)io = 0;
                bn = dumplo + btop(start);
                cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
@@ -870,17 +1042,38 @@ updump(dev)
                *--rp = 0;
                *--rp = -blk*NBPG / sizeof (short);
                *--rp = UP_GO|UP_WCOM;
                *--rp = 0;
                *--rp = -blk*NBPG / sizeof (short);
                *--rp = UP_GO|UP_WCOM;
+               retry = 0;
                do {
                        DELAY(25);
                do {
                        DELAY(25);
+                       if (++retry > 527)
+                               break;
                } while ((upaddr->upcs1 & UP_RDY) == 0);
                } while ((upaddr->upcs1 & UP_RDY) == 0);
-               if (upaddr->upcs1&UP_ERR) {
-                       printf("up dump dsk err: (%d,%d,%d) cs1=%x, er1=%x\n",
-                           cn, tn, sn, upaddr->upcs1, upaddr->uper1);
-                       return (-1);
+               if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
+                       printf("up%d: not ready", unit);
+                       if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
+                               printf("\n");
+                               return (EIO);
+                       }
+                       printf(" (flakey)\n");
                }
                }
+               if (upaddr->upds&UPDS_ERR)
+                       return (EIO);
                start += blk*NBPG;
                num -= blk;
        }
        return (0);
 }
                start += blk*NBPG;
                num -= blk;
        }
        return (0);
 }
+
+upsize(dev)
+       dev_t dev;
+{
+       int unit = upunit(dev);
+       struct uba_device *ui;
+       struct upst *st;
+
+       if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
+               return (-1);
+       st = &upst[ui->ui_type];
+       return (st->sizes[minor(dev) & 07].nblocks);
+}
 #endif
 #endif