conservative fix for increasing FIN
[unix-history] / usr / src / sys / vax / stand / hp.c
index 360761a..59fba1f 100644 (file)
@@ -1,4 +1,10 @@
-/*     hp.c    4.3     83/01/18        */
+/*
+ * Copyright (c) 1982 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)hp.c        6.8 (Berkeley) %G%
+ */
 
 /*
  * RP??/RM?? disk driver
 
 /*
  * RP??/RM?? disk driver
@@ -6,7 +12,6 @@
  * Also supports header io operations and
  * commands to write check header and data.
  */
  * Also supports header io operations and
  * commands to write check header and data.
  */
-
 #include "../h/param.h"
 #include "../h/inode.h"
 #include "../h/fs.h"
 #include "../h/param.h"
 #include "../h/inode.h"
 #include "../h/fs.h"
 #include "saio.h"
 #include "savax.h"
 
 #include "saio.h"
 #include "savax.h"
 
+#define        RETRIES         27
+
 #define        MASKREG(reg)    ((reg)&0xffff)
 
 #define        MASKREG(reg)    ((reg)&0xffff)
 
-#define MAXBADDESC     126
-#define SECTSIZ        512     /* sector size in bytes */
-#define HDRSIZ         4       /* number of bytes in sector header */
-#define MAXECC         5       /* the maximum number of bad bits accepted in
-                                * an ecc error when F_ECCLM is set */
-
-char   hp_type[MAXNMBA*8] = { 0 };
-
-/* THIS SHOULD BE READ IN OFF THE PACK, PER DRIVE */
-short  hp6_off[8] =    { 0, 38, 0, -1, -1, -1, 118, -1 };
-short  rm3_off[8] =    { 0, 100, 0, -1, -1, -1, 310, -1 };
-short  rm5_off[8] =    { 0, 27, 0, 562, 589, 681, 562, 82 };
-short  rm80_off[8] =   { 0, 37, 0, -1, -1, -1, 115, 305 };
-short  hp7_off[8] =    { 0, 10, 0, 330, 340, 500, 330, 50 };
-short  ml_off[8] =     { 0, -1, -1, -1, -1, -1, -1, -1 };
-short  si9775_off[8] = { 0, 13, 0, -1, -1, -1, 40, 441 };
-short  si9730_off[8] = { 0, 50, 0, -1, -1, -1, -1, 155 };
-short  hpam_off[8] =   { 0, 32, 0, 668, 723, 778, 668, 98 };
-/* END SHOULD BE READ IN */
-
-short  hptypes[] =
-    { MBDT_RM03, MBDT_RM05, MBDT_RP06, MBDT_RM80, MBDT_RP05, MBDT_RP07,
-      MBDT_ML11A, MBDT_ML11B, -1/*9755*/, -1/*9730*/, -1/*Capr*/, MBDT_RM02, 0};
-
-#define RP06 (hptypes[UNITTODRIVE(unit)] <= MBDT_RP06)
-#define ML11 (hptypes[UNITTODRIVE(unit)] <= MBDT_ML11A)
-#define RM80 (hptypes[UNITTODRIVE(unit)] <= MBDT_RM80)
+#define        MAXBADDESC      126
+#define        SECTSIZ         512     /* sector size in bytes */
+#define        HDRSIZ          4       /* number of bytes in sector header */
+
+extern struct st hpst[];
+extern short hptypes[];
+
+#define        RP06 (hptypes[sc->type] == MBDT_RP06 || hptypes[sc->type] == MBDT_RP05 \
+       || hptypes[sc->type] == MBDT_RP04)
+#define        ML11 (hptypes[sc->type] == MBDT_ML11A)
+#define        RM80 (hptypes[sc->type] == MBDT_RM80)
 
 u_char hp_offset[16] = {
     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
 
 u_char hp_offset[16] = {
     HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400,
@@ -56,22 +47,28 @@ u_char      hp_offset[16] = {
     0, 0, 0, 0,
 };
 
     0, 0, 0, 0,
 };
 
-struct st hpst[] = {
-       32,     5,      32*5,   823,    rm3_off,        /* RM03 */
-       32,     19,     32*19,  823,    rm5_off,        /* RM05 */
-       22,     19,     22*19,  815,    hp6_off,        /* RP06 */
-       31,     14,     31*14,  559,    rm80_off,       /* RM80 */
-       22,     19,     22*19,  411,    hp6_off,        /* RP06 */
-       50,     32,     50*32,  630,    hp7_off,        /* RP07 */
-       1,      1,      1,      1,      ml_off,         /* ML11A */
-       1,      1,      1,      1,      ml_off,         /* ML11B */
-       32,     40,     32*40,  843,    si9775_off,     /* 9775 */
-       32,     10,     32*10,  823,    si9730_off,     /* 9730 */
-       32,     16,     32*16,  1024,   hpam_off,       /* AMPEX capricorn */
-       1,      1,      1,      1,      0,              /* rm02 - not used */
-};
-struct dkbad hpbad[MAXNMBA*8];
-int sectsiz;
+struct dkbad hpbad[MAXNMBA*8];
+
+struct hp_softc {
+       char    type;
+       char    gottype;
+       char    ssect;          /* 1 when on track w/skip sector */
+       char    debug;
+#      define  HPF_BSEDEBUG    01      /* debugging bad sector forwarding */
+#      define  HPF_ECCDEBUG    02      /* debugging ecc correction */
+       int     ecclim;
+       int     retries;
+} hp_softc[MAXNMBA * 8];
+
+int    sectsiz;
+
+/*
+ * When awaiting command completion, don't
+ * hang on to the status register since
+ * this ties up some controllers.
+ */
+#define        HPWAIT(addr) \
+       while ((((addr)->hpds)&HPDS_DRY)==0) DELAY(500);
 
 hpopen(io)
        register struct iob *io;
 
 hpopen(io)
        register struct iob *io;
@@ -79,11 +76,11 @@ hpopen(io)
        register unit = io->i_unit;
        struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
        register struct st *st;
        register unit = io->i_unit;
        struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
        register struct st *st;
+       register struct hp_softc *sc = &hp_softc[unit];
 
        mbainit(UNITTOMBA(unit));
 
        mbainit(UNITTOMBA(unit));
-       if (hp_type[unit] == 0) {
-               register type = hpaddr->hpdt & MBDT_TYPE;
-               register int i;
+       if (sc->gottype == 0) {
+               register i, type = hpaddr->hpdt & MBDT_TYPE;
                struct iob tio;
 
                for (i = 0; hptypes[i]; i++)
                struct iob tio;
 
                for (i = 0; hptypes[i]; i++)
@@ -91,59 +88,25 @@ hpopen(io)
                                goto found;
                _stop("unknown drive type");
 found:
                                goto found;
                _stop("unknown drive type");
 found:
-               switch (i) {
-               case 0: case 1: {       /* rm03 or rm05 */
-                       register hpsn = hpaddr->hpsn;
-
-                       if ((hpsn & SIMB_LU) != unit)
-                               break;
-                       switch ((hpsn & SIMB_MB) &~ (SIMB_S6|SIRM03|SIRM05)) {
-                       case SI9775D:
-                               i = 8;  /* si9775 */
-                               break;
-                       case SI9730D:
-                               i = 9;  /* si9730 */
-                               break;
-                       case SI9766:
-                               i = 1;  /* rm05 */
-                               hpaddr->hpcs1 = HP_RECAL|HP_GO;
-                               DELAY(100000);
-                               break;
-                       case SI9762:
-                               i = 0;  /* rm03 */
-                               break;
-                       }
-                       break;
-               }
-
-               case 11:                /* rm02 */
-                       hpaddr->hpcs1 = HP_NOP;
-                       hpaddr->hphr = HPHR_MAXTRAK;
-                       if (MASKREG(hpaddr->hphr) == 15)
-                               i = 10;         /* ampex capricorn */
-                       else
-                               i = 0;          /* rm03 */
-                       break;
-               
-               case 6: case 7:         /* ml11a ml11b */
-                       i = 6;                  /* ml11a */
-                       break;
-               }
-               hp_type[unit] = i;
+               sc->retries = RETRIES;
+               sc->ecclim = 11;
+               sc->debug = 0;
+               hpaddr->hpcs1 = HP_DCLR|HP_GO;          /* init drive */
+               hpaddr->hpcs1 = HP_PRESET|HP_GO;
+               if (!ML11)
+                       hpaddr->hpof = HPOF_FMT22;
+               sc->type = hpmaptype(hpaddr, i, UNITTODRIVE(unit));
                /*
                /*
-                * Read in the bad sector table:
-                *      copy the contents of the io structure
-                *      to tio for use during the bb pointer
-                *      read operation.
+                * Read in the bad sector table.
                 */
                 */
-               st = &hpst[hp_type[unit]];
+               st = &hpst[sc->type];
                tio = *io;
                tio.i_bn = st->nspc * st->ncyl - st->nsect;
                tio = *io;
                tio.i_bn = st->nspc * st->ncyl - st->nsect;
-               tio.i_ma = (char *)&hpbad[tio.i_unit];
-               tio.i_cc = sizeof (hpbad);
+               tio.i_ma = (char *)&hpbad[unit];
+               tio.i_cc = sizeof (struct dkbad);
                tio.i_flgs |= F_RDDATA;
                for (i = 0; i < 5; i++) {
                tio.i_flgs |= F_RDDATA;
                for (i = 0; i < 5; i++) {
-                       if (hpstrategy(&tio, READ) == sizeof (hpbad))
+                       if (hpstrategy(&tio, READ) == sizeof (struct dkbad))
                                break;
                        tio.i_bn += 2;
                }
                                break;
                        tio.i_bn += 2;
                }
@@ -153,8 +116,10 @@ found:
                                hpbad[unit].bt_bad[i].bt_cyl = -1;
                                hpbad[unit].bt_bad[i].bt_trksec = -1;
                        }
                                hpbad[unit].bt_bad[i].bt_cyl = -1;
                                hpbad[unit].bt_bad[i].bt_trksec = -1;
                        }
-               }       
+               }
+               sc->gottype = 1;
        }
        }
+       st = &hpst[sc->type];
        if (io->i_boff < 0 || io->i_boff > 7 ||
            st->off[io->i_boff]== -1)
                _stop("hp bad minor");
        if (io->i_boff < 0 || io->i_boff > 7 ||
            st->off[io->i_boff]== -1)
                _stop("hp bad minor");
@@ -166,11 +131,11 @@ hpstrategy(io, func)
 {
        register unit = io->i_unit;
        struct mba_regs *mba = mbamba(unit);
 {
        register unit = io->i_unit;
        struct mba_regs *mba = mbamba(unit);
-       daddr_t bn;
+       daddr_t bn, startblock;
        struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
        struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit);
-       struct st *st = &hpst[hp_type[unit]];
-       int cn, tn, sn, bytecnt, bytesleft; 
-       daddr_t startblock;
+       register struct hp_softc *sc = &hp_softc[unit];
+       struct st *st = &hpst[sc->type];
+       int cn, tn, sn, bytecnt, bytesleft, rv; 
        char *membase;
        int er1, er2, hprecal;
 
        char *membase;
        int er1, er2, hprecal;
 
@@ -180,168 +145,190 @@ hpstrategy(io, func)
        if ((hpaddr->hpds & HPDS_VV) == 0) {
                hpaddr->hpcs1 = HP_DCLR|HP_GO;
                hpaddr->hpcs1 = HP_PRESET|HP_GO;
        if ((hpaddr->hpds & HPDS_VV) == 0) {
                hpaddr->hpcs1 = HP_DCLR|HP_GO;
                hpaddr->hpcs1 = HP_PRESET|HP_GO;
-               if (hp_type[unit] != 6)         /* any but ml11 */
+               if (!ML11)
                        hpaddr->hpof = HPOF_FMT22;
        }
        io->i_errcnt = 0;
                        hpaddr->hpof = HPOF_FMT22;
        }
        io->i_errcnt = 0;
-       bytecnt = io->i_cc;
+       sc->ssect = 0;
+       rv = bytecnt = io->i_cc;
        membase = io->i_ma;
        startblock = io->i_bn;
        membase = io->i_ma;
        startblock = io->i_bn;
-       hprecal = 1;
-readmore:
+       hprecal = 0;
+
+restart:
        bn = io->i_bn;
        cn = bn/st->nspc;
        sn = bn%st->nspc;
        tn = sn/st->nsect;
        bn = io->i_bn;
        cn = bn/st->nspc;
        sn = bn%st->nspc;
        tn = sn/st->nsect;
-       sn = sn%st->nsect;
+       sn = sn%st->nsect + sc->ssect;
 
 
-       while ((hpaddr->hpds & HPDS_DRY) == 0)
-               ;
-       if (hp_type[unit] == 6)         /* ml11 */
+       HPWAIT(hpaddr);
+       mba->mba_sr = -1;
+       if (ML11)
                hpaddr->hpda = bn;
        else {
                hpaddr->hpdc = cn;
                hpaddr->hpda = (tn << 8) + sn;
        }
                hpaddr->hpda = bn;
        else {
                hpaddr->hpdc = cn;
                hpaddr->hpda = (tn << 8) + sn;
        }
-       if (mbastart(io, func) != 0)            /* start transfer */
-               return (-1);
-
-       while ((hpaddr->hpds & HPDS_DRY) == 0)
-               ;
-       if (((hpaddr->hpds&HPDS_ERR) | (mba->mba_sr&MBSR_EBITS)) == 0 )
-               return(bytecnt);
-
-       /* ------- error handling ------- */
-
-       if (bytesleft = MASKREG(mba->mba_bcr>>16)) 
-               bytesleft |= 0xffff0000;        /* sign ext */
-       bn = io->i_bn + (io->i_cc + bytesleft)/sectsiz;
+       if (mbastart(io, func) != 0) {          /* start transfer */
+               rv = -1;
+               goto done;
+       }
+       HPWAIT(hpaddr);
+       /*
+        * Successful data transfer, return.
+        */
+       if ((hpaddr->hpds&HPDS_ERR) == 0 && (mba->mba_sr&MBSR_EBITS) == 0)
+               goto done;
+
+       /*
+        * Error handling.  Calculate location of error.
+        */
+       bytesleft = MASKREG(mba->mba_bcr);
+       if (bytesleft) 
+               bytesleft |= 0xffff0000;        /* sxt */
+       bn = io->i_bn + (io->i_cc + bytesleft) / sectsiz;
+       er1 = MASKREG(hpaddr->hper1);
+       er2 = MASKREG(hpaddr->hper2);
+       if (er1 & (HPER1_DCK|HPER1_ECH))
+               bn--;   /* Error is in Prev block */
        cn = bn/st->nspc;
        sn = bn%st->nspc;
        tn = sn/st->nsect;
        sn = sn%st->nsect;
        cn = bn/st->nspc;
        sn = bn%st->nspc;
        tn = sn/st->nsect;
        sn = sn%st->nsect;
-       er1 = MASKREG(hpaddr->hper1);
-       er2 = MASKREG(hpaddr->hper2);
-#ifdef HPDEBUG
-       printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
-               cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
-       printf("er1=%b er2=%b",
-               er1, HPER1_BITS,
-               er2, HPER2_BITS);
-       printf("\n");
-#endif
+       if (sc->debug & (HPF_ECCDEBUG|HPF_BSEDEBUG)) {
+               printf("hp error: sn%d (cyl,trk,sec)=(%d,%d,%d) ds=%b\n",
+                       bn, cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
+               printf("er1=%b er2=%b\n", er1, HPER1_BITS, er2, HPER2_BITS);
+               printf("bytes left: %d, of 0x%x, da 0x%x\n",-bytesleft,
+                       hpaddr->hpof, hpaddr->hpda);
+       }
        if (er1 & HPER1_HCRC) {
                er1 &= ~(HPER1_HCE|HPER1_FER);
                er2 &= ~HPER2_BSE;
        if (er1 & HPER1_HCRC) {
                er1 &= ~(HPER1_HCE|HPER1_FER);
                er2 &= ~HPER2_BSE;
+               if ((io->i_flgs&F_NBSF) == 0 && hpecc(io, BSE) == 0)
+                       goto success;
        }
        }
+       /*
+        * Give up early if drive write locked.
+        */
        if (er1&HPER1_WLE) {
                printf("hp%d: write locked\n", unit);
        if (er1&HPER1_WLE) {
                printf("hp%d: write locked\n", unit);
-               return(-1);
-       } else if ((er1&0xffff) == HPER1_FER && RP06) {
-               goto badsect;
-
-       } else if (++io->i_errcnt > 27 ||
-                  er1 & HPER1_HARD ||
-                  (!ML11 && (er2 & HPER2_HARD))) {
-               io->i_error = EHER;
-               if ((mba->mba_sr & (MBSR_WCKUP | MBSR_WCKLWR)) != 0)
-                       io->i_error = EWCK;
-hard:
-               io->i_errblk = bn;
-               printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
-                          cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
-               printf("er1=%b er2=%b",
-                          er1, HPER1_BITS,
-                          er2, HPER2_BITS);
-               if (hpaddr->hpmr)
-                       printf(" mr=%o", hpaddr->hpmr&0xffff);
-               if (hpaddr->hpmr2)
-                       printf(" mr2=%o", hpaddr->hpmr2&0xffff);
-               printf("\n");
-               return(-1);
-
-       } else if ((er2 & HPER2_BSE) && !ML11) {
-badsect:
-               if ((io->i_flgs & F_NBSF) != 0) {
+               rv = -1;
+               goto done;
+       }
+       /*
+        * Skip sector handling.
+        */
+       if (RM80 && (er2 & HPER2_SSE)) {
+               (void) hpecc(io, SSE);
+               sc->ssect = 1;
+               goto restart;
+       }
+       /*
+        * Attempt to forward bad sectors on anything but an ML11.
+        * Interpret format error bit as a bad block on RP06's.
+        */
+       if (((er2 & HPER2_BSE) && !ML11) ||
+           (MASKREG(er1) == HPER1_FER && RP06)) {
+               if (io->i_flgs & F_NBSF) {
                        io->i_error = EBSE;     
                        goto hard;
                }
                if (hpecc(io, BSE) == 0)
                        goto success;
                        io->i_error = EBSE;     
                        goto hard;
                }
                if (hpecc(io, BSE) == 0)
                        goto success;
-               else {
-                       io->i_error = EBSE;
-                       goto hard;
-               }
-       } else if (RM80 && er2&HPER2_SSE) {
-       /* skip sector error */
-               (void) hpecc(io, SSE);
-               goto success;
-       } else if ((er1&(HPER1_DCK|HPER1_ECH))==HPER1_DCK) {
-               if ( hpecc(io, ECC) == 0)
+               io->i_error = EBSE;
+               goto hard;
+       }
+       /*
+        * ECC correction?
+        */
+       if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) {
+               if (hpecc(io, ECC) == 0)
                        goto success;
                        goto success;
+               io->i_error = EECC;
+               goto hard;
+       } 
+
+       /*
+        * If a hard error, or maximum retry count
+        * exceeded, clear controller state and
+        * pass back error to caller.
+        */
+       if (++io->i_errcnt > sc->retries || (er1 & HPER1_HARD) ||
+           (!ML11 && (er2 & HPER2_HARD)) || (ML11 && (io->i_errcnt >= 16))) {
+               io->i_error = EHER;
+               if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR))
+                       io->i_error = EWCK;
+hard:
+               io->i_errblk = bn + sc->ssect;
+               if (sc->debug & (HPF_BSEDEBUG|HPF_ECCDEBUG))
+                   printf(" dc=%d, da=0x%x",MASKREG(hpaddr->hpdc),
+                         MASKREG(hpaddr->hpda));
                else {
                else {
-                       io->i_error = EECC;
-                       return(1);      
+                   printf("hp error: sn%d (cyl,trk,sec)=(%d,%d,%d) ds=%b \n",
+                          bn, cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS);
+                   printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS);
                }
                }
-       } else
-               io->i_active = 0;               /* force retry */
+               hpaddr->hpcs1 = HP_DCLR|HP_GO;
+               printf("\n");
+               rv = -1;
+               goto done;
 
 
-       hpaddr->hpcs1 = HP_DCLR|HP_GO;
-       while ((hpaddr->hpds & HPDS_DRY) == 0)
-               ;
-       if (ML11) {
-               if (io->i_errcnt >= 16)
-                       goto hard;
-       } else if (((io->i_errcnt&07) == 4) && (io->i_active == 0)) {
-               hpaddr->hpcs1 = HP_RECAL|HP_GO;
-               hprecal = 0;
-               goto nextrecal;
        }
        }
-       switch (hprecal) {
+       /* fall thru to retry */
+       hpaddr->hpcs1 = HP_DCLR|HP_GO;
+       HPWAIT(hpaddr);
 
 
-       case 1:
+       /* 
+        * Every fourth retry recalibrate.
+        */
+       if (((io->i_errcnt & 07) == 4) ) {
+               hpaddr->hpcs1 = HP_RECAL|HP_GO;
+               HPWAIT(hpaddr);
                hpaddr->hpdc = cn;
                hpaddr->hpcs1 = HP_SEEK|HP_GO;
                hpaddr->hpdc = cn;
                hpaddr->hpcs1 = HP_SEEK|HP_GO;
-               goto nextrecal;
-       case 2:
-               if (io->i_errcnt < 16 || (func & READ) == 0)
-                       goto donerecal;
+               HPWAIT(hpaddr);
+       }
+
+       if (io->i_errcnt >= 16 && (io->i_flgs & F_READ)) {
                hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
                hpaddr->hpcs1 = HP_OFFSET|HP_GO;
                hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22;
                hpaddr->hpcs1 = HP_OFFSET|HP_GO;
-nextrecal:
-               hprecal++;
-               io->i_active = 1;
-               goto try_again;
-       donerecal:
-       case 3:
-               hprecal = 0;
-               io->i_active = 0;
-               goto try_again;
-       }
-       if (io->i_active) {
-               if (io->i_errcnt >= 16) {
-                       hpaddr->hpcs1 = HP_RTC|HP_GO;
-                       while (hpaddr->hpds & HPDS_PIP)
-                               ;
-               }
+               HPWAIT(hpaddr);
        }
        }
-success:                /* continue with the next block */
+       if (sc->debug & (HPF_ECCDEBUG|HPF_BSEDEBUG))
+               printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
+                 io->i_bn, io->i_cc, io->i_ma, hprecal);
+       goto restart;
+
+success:
+       /*
+        * On successful error recovery, bump
+        * block number to advance to next portion
+        * of i/o transfer.
+        */
        bn++;
        if ((bn-startblock) * sectsiz < bytecnt) {
        bn++;
        if ((bn-startblock) * sectsiz < bytecnt) {
-
-try_again:             /* re-read same block */
                io->i_bn = bn;
                io->i_bn = bn;
-               mba->mba_sr = -1;
                io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
                io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
                io->i_ma = membase + (io->i_bn - startblock)*sectsiz;
                io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz;
-#ifdef HPDEBUG
-               printf("restart: bl %d, byte %d, mem 0x%x %d\n",
-                       io->i_bn, io->i_cc, io->i_ma, io->i_ma);
-#endif
-               goto readmore;
+               if (sc->debug & (HPF_ECCDEBUG|HPF_BSEDEBUG))
+                       printf("restart: bn=%d, cc=%d, ma=0x%x hprecal=%d\n",
+                         io->i_bn, io->i_cc, io->i_ma, hprecal);
+               goto restart;
+       }
+done:
+       if (io->i_errcnt >= 16) {
+               hpaddr->hpcs1 = HP_RTC|HP_GO;
+               while (hpaddr->hpds & HPDS_PIP)
+                       ;
        }
        }
-       return (bytecnt);
+       io->i_bn = startblock;          /*reset i_bn to original */
+       io->i_cc = bytecnt;             /*reset i_cc to total count xfered*/
+       io->i_ma = membase;             /*reset i_ma to original */
+       return (rv);
 }
 }
+
 hpecc(io, flag)
        register struct iob *io;
        int flag;
 hpecc(io, flag)
        register struct iob *io;
        int flag;
@@ -349,143 +336,172 @@ hpecc(io, flag)
        register unit = io->i_unit;
        register struct mba_regs *mbp = mbamba(unit);
        register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
        register unit = io->i_unit;
        register struct mba_regs *mbp = mbamba(unit);
        register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit);
-       register struct st *st = &hpst[hp_type[unit]];
-       int npf;
-       int bn, cn, tn, sn;
-       int bcr, tad;
+       register struct hp_softc *sc = &hp_softc[unit];
+       register struct st *st = &hpst[sc->type];
+       int npf, bn, cn, tn, sn, bcr;
 
 
-       if (bcr = MASKREG(mbp->mba_bcr>>16))
+       bcr = MASKREG(mbp->mba_bcr);
+       if (bcr)
                bcr |= 0xffff0000;              /* sxt */
                bcr |= 0xffff0000;              /* sxt */
-       npf = (bcr + io->i_cc)/sectsiz;         /* number of sectors read */
-       switch (flag) {
-       case ECC:
-               {
+       npf = (bcr + io->i_cc) / sectsiz;       /* # sectors read */
+       if (flag == ECC)
+               npf--;          /* Error is in prev block --ghg */
+       bn = io->i_bn + npf + sc->ssect;        /* physical block #*/
+       if (sc->debug & HPF_ECCDEBUG)
+               printf("bcr=%d npf=%d ssect=%d sectsiz=%d i_cc=%d\n",
+                       bcr, npf, sc->ssect, sectsiz, io->i_cc);
+       /*
+        * ECC correction logic.
+        */
+       if (flag == ECC) {
                register int i;
                caddr_t addr;
                register int i;
                caddr_t addr;
-               int bit, byte, mask, ecccnt = 0;
+               int bit, o, mask;
 
 
-               printf("hp%d: soft ecc sn%d\n", unit, io->i_bn + npf);
+               printf("hp%d: soft ecc sn%d\n", unit, bn);
                mask = MASKREG(rp->hpec2);
                mask = MASKREG(rp->hpec2);
-               i = MASKREG(rp->hpec1) - 1;             /* -1 makes 0 origin */
+               for (i = mask, bit = 0; i; i >>= 1)
+                       if (i & 1)
+                               bit++;
+               if (bit > sc->ecclim) {
+                       printf("%d-bit error\n", bit);
+                       return (1);
+               }
+               i = MASKREG(rp->hpec1) - 1;     /* -1 makes 0 origin */
                bit = i&07;
                bit = i&07;
-               i = (i&~07)>>3;
-               byte = i;
+               o = (i & ~07) >> 3;
                rp->hpcs1 = HP_DCLR | HP_GO;
                rp->hpcs1 = HP_DCLR | HP_GO;
-               while (i <sectsiz && npf*sectsiz + i < io->i_cc && bit > -11) {
-                       addr = io->i_ma + (npf*sectsiz) + byte;
-#ifdef HPECCDEBUG
-                       printf("addr %x old:%x ",addr, (*addr&0xff));
-#endif
-                       if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0)
-                               *addr ^= (mask << bit); /* don't 'correct' mem-
-                                                        * ory during Wcheck */
-#ifdef HPECCDEBUG
-                       printf("new:%x\n",(*addr&0xff));
-#endif
-                       byte++;
-                       i++;
-                       bit -= 8;
-                       if ((ecccnt++>=MAXECC) && ((io->i_flgs&F_ECCLM) != 0))
-                               return(1);
-               }
-               return(0);
+               while (o <sectsiz && npf*sectsiz + o < io->i_cc && bit > -11) {
+                       addr = io->i_ma + (npf*sectsiz) + o;
+                       /*
+                        * No data transfer occurs with a write check,
+                        * so don't correct the resident copy of data.
+                        */
+                       if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) {
+                               if (sc->debug & HPF_ECCDEBUG)
+                                       printf("addr=%x old=%x ", addr,
+                                               (*addr & 0xff));
+                               *addr ^= (mask << bit);
+                               if (sc->debug & HPF_ECCDEBUG)
+                                       printf("new=%x\n",(*addr & 0xff));
+                       }
+                       o++, bit -= 8;
                }
                }
+               return (0);
+       }
 
 
-       case SSE:               /* skip sector error */
-                               /* -----this section must be fixed------*/
+       /*
+        * Skip sector error.
+        * Set skip-sector-inhibit and
+        * read next sector
+        */
+       if (flag == SSE) {
                rp->hpcs1 = HP_DCLR | HP_GO;
                rp->hpcs1 = HP_DCLR | HP_GO;
+               HPWAIT(rp);
                rp->hpof |= HPOF_SSEI;
                rp->hpof |= HPOF_SSEI;
-               mbp->mba_bcr = -(io->i_cc - npf*sectsiz);
-               /* presumably the disk address has already
-                * been incremented to point to the next sector */
-               return(0);      
-
-#ifndef NOBADSECT
-       case BSE:
-#ifdef HPDEBUG
-               printf("hpecc: BSE @ bn %d\n", bn);
-#endif
+               return (0);     
+       }
+
+       /*
+        * Bad block forwarding.
+        */
+        if (flag == BSE) {
+               int bbn;
+
                rp->hpcs1 = HP_DCLR | HP_GO;
                rp->hpcs1 = HP_DCLR | HP_GO;
-               bcr += SECTSIZ;
-               tad = rp->hpda;
-               if ((bn = isbad(&hpbad[unit], bcr, tad>>8, tad&0x7f)) < 0)
-                       return(1);
-               bn = st->ncyl*st->nspc - st->nsect - 1 - bn;
+               if (sc->debug & HPF_BSEDEBUG)
+                       printf("hpecc: BSE @ bn %d\n", bn);
                cn = bn/st->nspc;
                sn = bn%st->nspc;
                tn = sn/st->nsect;
                cn = bn/st->nspc;
                sn = bn%st->nspc;
                tn = sn/st->nsect;
-               sn %= st->nsect;
-               io->i_cc = -SECTSIZ;
-               io->i_ma = (char *)((io->i_bn + npf -1)*SECTSIZ);
-#ifdef HPDEBUG
-               printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
-#endif
+               sn = sn%st->nsect;
+               bcr += sectsiz;
+               if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0)
+                       return (1);
+               bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn;
+               cn = bbn/st->nspc;
+               sn = bbn%st->nspc;
+               tn = sn/st->nsect;
+               sn = sn%st->nsect;
+               io->i_cc = sectsiz;
+               io->i_ma += npf*sectsiz;
+               if (sc->debug & HPF_BSEDEBUG)
+                       printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
+               rp->hpof &= ~HPOF_SSEI;
+               mbp->mba_sr = -1;
                rp->hpdc = cn;
                rp->hpda = (tn<<8) + sn;
                rp->hpdc = cn;
                rp->hpda = (tn<<8) + sn;
-               mbp->mba_sr = -1;
                mbastart(io,io->i_flgs);
                mbastart(io,io->i_flgs);
-               io->i_errcnt = 0;       /* error has been corrected */
-               while(rp->hpds & HPDS_DRY == 0)
-                       ;
-               if (rp->hpds&HPDS_ERR)
-                       return(1);
-               else
-                       return(0);
+               io->i_errcnt = 0;
+               HPWAIT(rp);
+               return (rp->hpds&HPDS_ERR);
        }
        }
+       printf("hpecc: flag=%d\n", flag);
+       return (1);
 }
 }
+
 /*ARGSUSED*/
 hpioctl(io, cmd, arg)
        struct iob *io;
        int cmd;
        caddr_t arg;
 {
 /*ARGSUSED*/
 hpioctl(io, cmd, arg)
        struct iob *io;
        int cmd;
        caddr_t arg;
 {
-
-       struct st *st = &hpst[hp_type[io->i_unit]], *tmp;
-       struct mba_drv *drv = mbadrv(io->i_unit);
+       register unit = io->i_unit;
+       register struct hp_softc *sc = &hp_softc[unit];
+       struct st *st = &hpst[sc->type];
+       struct mba_drv *drv = mbadrv(unit);
 
        switch(cmd) {
 
 
        switch(cmd) {
 
+       case SAIODEBUG:
+               sc->debug = (int)arg;
+               break;
+
        case SAIODEVDATA:
        case SAIODEVDATA:
-               if ((drv->mbd_dt&MBDT_TAP) == 0) {
-                       tmp = (struct st *)arg;
-                       *tmp = *st;
-                       return(0);
+               if (drv->mbd_dt&MBDT_TAP)
+                       return (ECMD);
+               *(struct st *)arg = *st;
+               break;
+
+       case SAIOGBADINFO:
+               if (drv->mbd_dt&MBDT_TAP)
+                       return (ECMD);
+               *(struct dkbad *)arg = hpbad[unit];
+               break;
+
+       case SAIOECCLIM:
+               sc->ecclim = (int)arg;
+               break;
+
+       case SAIORETRIES:
+               sc->retries = (int)arg;
+               break;
+
+       case SAIOSSI:                   /* skip-sector-inhibit */
+               if (drv->mbd_dt&MBDT_TAP)
+                       return (ECMD);
+               if ((io->i_flgs&F_SSI) == 0) {
+                       /* make sure this is done once only */
+                       io->i_flgs |= F_SSI;
+                       st->nsect++;
+                       st->nspc += st->ntrak;
+               }
+               break;
+
+       case SAIONOSSI:                 /* remove skip-sector-inhibit */
+               if (io->i_flgs & F_SSI) {
+                       io->i_flgs &= ~F_SSI;
+                       drv->mbd_of &= ~HPOF_SSEI;
+                       st->nsect--;
+                       st->nspc -= st->ntrak;
                }
                }
-               else 
-                       return(ECMD);
+               break;
+
+       case SAIOSSDEV:                 /* drive have skip sector? */
+               return (RM80 ? 0 : ECMD);
 
        default:
                return (ECMD);
        }
 
        default:
                return (ECMD);
        }
-}
-
-/* this routine is common to up & hp, move to a separate file */
-
-/*
- * Search the bad sector table looking for
- * the specified sector.  Return index if found.
- * Return -1 if not found.
- */
-
-isbad(bt, st, blno)
-       register struct dkbad *bt;
-       register struct st *st;
-{
-       register int i;
-       register long blk, bblk;
-       int trk, sec;
-
-       sec = blno % st->nspc;
-       trk = sec / st->nsect;
-       sec %= st->nsect;
-       blk = ((long)(blno/st->nspc) << 16) + (trk << 8) + sec;
-       for (i = 0; i < MAXBADDESC; i++) {
-               bblk = ((long)bt->bt_bad[i].bt_cyl << 16) +
-                       bt->bt_bad[i].bt_trksec;
-               if (blk == bblk)
-                       return (i);
-               if (blk < bblk || bblk < 0)
-                       break;
-       }
-       return (-1);
+       return (0);
 }
 }