1*11140Ssam /* hp.c 4.15 83/02/18 */ 210334Shelge 310334Shelge /* 410334Shelge * RP??/RM?? disk driver 510334Shelge * with ECC handling and bad block forwarding. 610334Shelge * Also supports header io operations and 710334Shelge * commands to write check header and data. 810334Shelge */ 910334Shelge 1010334Shelge #include "../h/param.h" 1110334Shelge #include "../h/inode.h" 1210334Shelge #include "../h/fs.h" 1310334Shelge #include "../h/dkbad.h" 1410334Shelge 1510334Shelge #include "../vax/pte.h" 1610334Shelge #include "../vaxmba/hpreg.h" 1710334Shelge #include "../vaxmba/mbareg.h" 1810334Shelge 1910334Shelge #include "saio.h" 2010334Shelge #include "savax.h" 2110334Shelge 2210334Shelge #define MASKREG(reg) ((reg)&0xffff) 2310334Shelge 2410334Shelge #define MAXBADDESC 126 2510334Shelge #define SECTSIZ 512 /* sector size in bytes */ 2610334Shelge #define HDRSIZ 4 /* number of bytes in sector header */ 27*11140Ssam #define MAXECC 5 /* max # bits allow in ecc error w/ F_ECCLM */ 2810334Shelge 2910334Shelge char hp_type[MAXNMBA*8] = { 0 }; 30*11140Ssam extern struct st hpst[]; 3110334Shelge 3211115Ssam short hptypes[] = { 3311115Ssam MBDT_RM03, MBDT_RM05, MBDT_RP06, MBDT_RM80, 3411115Ssam MBDT_RP05, MBDT_RP07, MBDT_ML11A, MBDT_ML11B, 3511115Ssam -1 /*9755*/, -1 /*9730*/, -1 /*Cap*/, -1 /* Eagle */, 3611115Ssam -1 /* Eagle */, MBDT_RM02, 0 3711115Ssam }; 3810334Shelge 3910626Shelge #define RP06 (hptypes[hp_type[unit]] <= MBDT_RP06) 4010626Shelge #define ML11 (hptypes[hp_type[unit]] == MBDT_ML11A) 4110626Shelge #define RM80 (hptypes[hp_type[unit]] == MBDT_RM80) 4210334Shelge 4310334Shelge u_char hp_offset[16] = { 4410334Shelge HPOF_P400, HPOF_M400, HPOF_P400, HPOF_M400, 4510334Shelge HPOF_P800, HPOF_M800, HPOF_P800, HPOF_M800, 4610334Shelge HPOF_P1200, HPOF_M1200, HPOF_P1200, HPOF_M1200, 4710334Shelge 0, 0, 0, 0, 4810334Shelge }; 4910334Shelge 5010334Shelge struct dkbad hpbad[MAXNMBA*8]; 5110334Shelge int sectsiz; 5210334Shelge 5310864Ssam /* 5410864Ssam * When awaiting command completion, don't 5510864Ssam * hang on to the status register since 5610864Ssam * this ties up the controller. 5710864Ssam */ 5810864Ssam #define HPWAIT(addr) while ((((addr)->hpds)&HPDS_DRY)==0) DELAY(500); 5910864Ssam 6010334Shelge hpopen(io) 6110334Shelge register struct iob *io; 6210334Shelge { 6310334Shelge register unit = io->i_unit; 6410334Shelge struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit); 6510353Shelge register struct st *st; 6610334Shelge 6710334Shelge mbainit(UNITTOMBA(unit)); 6810334Shelge if (hp_type[unit] == 0) { 6910334Shelge register type = hpaddr->hpdt & MBDT_TYPE; 7010334Shelge register int i; 7110334Shelge struct iob tio; 7210334Shelge 7310334Shelge for (i = 0; hptypes[i]; i++) 7410334Shelge if (hptypes[i] == type) 7510334Shelge goto found; 7610334Shelge _stop("unknown drive type"); 7710334Shelge found: 7810647Shelge hpaddr->hpcs1 = HP_DCLR|HP_GO; /* init drive */ 7910647Shelge hpaddr->hpcs1 = HP_PRESET|HP_GO; 8010647Shelge if (!ML11) 8110647Shelge hpaddr->hpof = HPOF_FMT22; 8211113Shelge hp_type[unit] = hpmaptype(hpaddr, i, unit); 8310334Shelge /* 8410334Shelge * Read in the bad sector table: 8510334Shelge * copy the contents of the io structure 8610334Shelge * to tio for use during the bb pointer 8710334Shelge * read operation. 8810334Shelge */ 89*11140Ssam st = &hpst[hp_type[unit]]; 9010334Shelge tio = *io; 9110334Shelge tio.i_bn = st->nspc * st->ncyl - st->nsect; 9210626Shelge tio.i_ma = (char *)&hpbad[unit]; 9310628Shelge tio.i_cc = sizeof (struct dkbad); 9410334Shelge tio.i_flgs |= F_RDDATA; 9510334Shelge for (i = 0; i < 5; i++) { 9610628Shelge if (hpstrategy(&tio, READ) == sizeof (struct dkbad)) 9710334Shelge break; 9810334Shelge tio.i_bn += 2; 9910334Shelge } 10010334Shelge if (i == 5) { 10110334Shelge printf("Unable to read bad sector table\n"); 10210334Shelge for (i = 0; i < MAXBADDESC; i++) { 10310334Shelge hpbad[unit].bt_bad[i].bt_cyl = -1; 10410334Shelge hpbad[unit].bt_bad[i].bt_trksec = -1; 10510334Shelge } 10610334Shelge } 10710334Shelge } 10810334Shelge if (io->i_boff < 0 || io->i_boff > 7 || 10910334Shelge st->off[io->i_boff]== -1) 11010334Shelge _stop("hp bad minor"); 11110334Shelge io->i_boff = st->off[io->i_boff] * st->nspc; 11210334Shelge } 11310334Shelge 11410647Shelge int ssect; /* set to 1 if we are on a track with skip sectors */ 11510647Shelge 11610334Shelge hpstrategy(io, func) 11710334Shelge register struct iob *io; 11810334Shelge { 11910334Shelge register unit = io->i_unit; 12010334Shelge struct mba_regs *mba = mbamba(unit); 12110334Shelge daddr_t bn; 12210334Shelge struct hpdevice *hpaddr = (struct hpdevice *)mbadrv(unit); 12310353Shelge struct st *st = &hpst[hp_type[unit]]; 12410334Shelge int cn, tn, sn, bytecnt, bytesleft; 12510334Shelge daddr_t startblock; 12610334Shelge char *membase; 12710334Shelge int er1, er2, hprecal; 12810334Shelge 12910334Shelge sectsiz = SECTSIZ; 13010334Shelge if ((io->i_flgs & (F_HDR|F_HCHECK)) != 0) 13110334Shelge sectsiz += HDRSIZ; 13210334Shelge if ((hpaddr->hpds & HPDS_VV) == 0) { 13310334Shelge hpaddr->hpcs1 = HP_DCLR|HP_GO; 13410334Shelge hpaddr->hpcs1 = HP_PRESET|HP_GO; 13511084Ssam if (!ML11) 13610334Shelge hpaddr->hpof = HPOF_FMT22; 13710334Shelge } 13810334Shelge io->i_errcnt = 0; 13910647Shelge ssect = 0; 14010334Shelge bytecnt = io->i_cc; 14110334Shelge membase = io->i_ma; 14210334Shelge startblock = io->i_bn; 14310608Ssam hprecal = 0; 144*11140Ssam 14510626Shelge restart: 14610334Shelge bn = io->i_bn; 14710334Shelge cn = bn/st->nspc; 14810334Shelge sn = bn%st->nspc; 14910334Shelge tn = sn/st->nsect; 15010647Shelge sn = sn%st->nsect + ssect; 15110334Shelge 15210864Ssam HPWAIT(hpaddr); 15310626Shelge mba->mba_sr = -1; 15410647Shelge if (ML11) 15510334Shelge hpaddr->hpda = bn; 15610334Shelge else { 15710334Shelge hpaddr->hpdc = cn; 15810334Shelge hpaddr->hpda = (tn << 8) + sn; 15910334Shelge } 16010334Shelge if (mbastart(io, func) != 0) /* start transfer */ 16110334Shelge return (-1); 16210864Ssam HPWAIT(hpaddr); 16311084Ssam if ((hpaddr->hpds&HPDS_ERR) == 0 && (mba->mba_sr&MBSR_EBITS) == 0) 16411084Ssam return (bytecnt); 16510334Shelge 16610334Shelge /* ------- error handling ------- */ 16710334Shelge 16810334Shelge if (bytesleft = MASKREG(mba->mba_bcr>>16)) 16910334Shelge bytesleft |= 0xffff0000; /* sign ext */ 17010334Shelge bn = io->i_bn + (io->i_cc + bytesleft)/sectsiz; 17110334Shelge cn = bn/st->nspc; 17210334Shelge sn = bn%st->nspc; 17310334Shelge tn = sn/st->nsect; 17410334Shelge sn = sn%st->nsect; 17510334Shelge er1 = MASKREG(hpaddr->hper1); 17610334Shelge er2 = MASKREG(hpaddr->hper2); 17710334Shelge #ifdef HPDEBUG 17810334Shelge printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n", 17910334Shelge cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS); 18010626Shelge printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS); 18110626Shelge printf("\nbytes left: %d, of 0x%x, da 0x%x",-bytesleft, 18210626Shelge hpaddr->hpof, hpaddr->hpda); 18310334Shelge printf("\n"); 18410334Shelge #endif 18510334Shelge if (er1 & HPER1_HCRC) { 18610334Shelge er1 &= ~(HPER1_HCE|HPER1_FER); 18710334Shelge er2 &= ~HPER2_BSE; 18810334Shelge } 189*11140Ssam /* 190*11140Ssam * Give up early if drive write locked. 191*11140Ssam */ 19210334Shelge if (er1&HPER1_WLE) { 19310334Shelge printf("hp%d: write locked\n", unit); 19411084Ssam return (-1); 19511084Ssam } 196*11140Ssam /* 197*11140Ssam * No bad sector handling on RP06's yet. 198*11140Ssam */ 19911084Ssam if (MASKREG(er1) == HPER1_FER && RP06) 20010334Shelge goto badsect; 201*11140Ssam 202*11140Ssam /* 203*11140Ssam * If a hard error, or maximum retry count 204*11140Ssam * exceeded, clear controller state and 205*11140Ssam * pass back error to caller. 206*11140Ssam */ 20711084Ssam if (++io->i_errcnt > 27 || (er1 & HPER1_HARD) || 20811084Ssam (!ML11 && (er2 & HPER2_HARD))) { 20910608Ssam hard0: 21010334Shelge io->i_error = EHER; 21111084Ssam if (mba->mba_sr & (MBSR_WCKUP|MBSR_WCKLWR)) 21210334Shelge io->i_error = EWCK; 21310334Shelge hard: 21410647Shelge io->i_errblk = bn + ssect; 21510334Shelge printf("hp error: (cyl,trk,sec)=(%d,%d,%d) ds=%b \n", 21610334Shelge cn, tn, sn, MASKREG(hpaddr->hpds), HPDS_BITS); 21711084Ssam printf("er1=%b er2=%b", er1, HPER1_BITS, er2, HPER2_BITS); 21810334Shelge if (hpaddr->hpmr) 21910626Shelge printf(" mr1=%o", MASKREG(hpaddr->hpmr)); 22010334Shelge if (hpaddr->hpmr2) 22110626Shelge printf(" mr2=%o", MASKREG(hpaddr->hpmr2)); 22210626Shelge #ifdef HPDEBUG 22310626Shelge printf("dc: %d, da: 0x%x",MASKREG(hpaddr->hpdc), 22411084Ssam MASKREG(hpaddr->hpda)); 22510626Shelge #endif 22610626Shelge hpaddr->hpcs1 = HP_DCLR|HP_GO; 22710334Shelge printf("\n"); 22811084Ssam return (-1); 22910334Shelge 23011084Ssam } 231*11140Ssam /* 232*11140Ssam * Attempt to forward bad sectors on 233*11140Ssam * anything but an ML11. If drive 234*11140Ssam * supports skip sector handling, try to 235*11140Ssam * use it first; otherwise try the 236*11140Ssam * bad sector table. 237*11140Ssam */ 23811084Ssam if ((er2 & HPER2_BSE) && !ML11) { 23910334Shelge badsect: 24010647Shelge if (!ssect && (er2&HPER2_SSE)) 24110647Shelge goto skipsect; 24211084Ssam if (io->i_flgs & F_NBSF) { 24310334Shelge io->i_error = EBSE; 24410334Shelge goto hard; 24510334Shelge } 24610334Shelge if (hpecc(io, BSE) == 0) 24710334Shelge goto success; 24811084Ssam io->i_error = EBSE; 24911084Ssam goto hard; 25011084Ssam } 251*11140Ssam 252*11140Ssam /* 253*11140Ssam * Skip sector handling. 254*11140Ssam */ 25511084Ssam if (RM80 && er2&HPER2_SSE) { 25610647Shelge skipsect: 25710334Shelge (void) hpecc(io, SSE); 25811084Ssam ssect = 1; 25910334Shelge goto success; 26011084Ssam } 261*11140Ssam /* 262*11140Ssam * ECC correction. 263*11140Ssam */ 26411084Ssam if ((er1 & (HPER1_DCK|HPER1_ECH)) == HPER1_DCK) { 26510864Ssam if (hpecc(io, ECC) == 0) 26610334Shelge goto success; 26711084Ssam io->i_error = EECC; 26811084Ssam return (-1); 26910608Ssam } 27010608Ssam if (ML11 && (io->i_errcnt >= 16)) 27111084Ssam goto hard0; 27210608Ssam /* fall thru to retry */ 27310334Shelge hpaddr->hpcs1 = HP_DCLR|HP_GO; 27410864Ssam HPWAIT(hpaddr); 275*11140Ssam 276*11140Ssam /* 277*11140Ssam * Every fourth retry recalibrate. 278*11140Ssam */ 27910608Ssam if (((io->i_errcnt&07) == 4) ) { 28010334Shelge hpaddr->hpcs1 = HP_RECAL|HP_GO; 28110608Ssam hprecal = 1; 28211084Ssam goto again; 28310334Shelge } 284*11140Ssam 285*11140Ssam /* 286*11140Ssam * Recalibration state machine. 287*11140Ssam */ 28810334Shelge switch (hprecal) { 28910334Shelge 29010334Shelge case 1: 29110334Shelge hpaddr->hpdc = cn; 29210334Shelge hpaddr->hpcs1 = HP_SEEK|HP_GO; 29311084Ssam hprecal = 2; 29411084Ssam goto again; 29510864Ssam 29610334Shelge case 2: 29710608Ssam if (io->i_errcnt < 16 || (io->i_flgs & F_READ) == 0) 29810334Shelge goto donerecal; 29910334Shelge hpaddr->hpof = hp_offset[io->i_errcnt & 017]|HPOF_FMT22; 30010334Shelge hpaddr->hpcs1 = HP_OFFSET|HP_GO; 30111084Ssam hprecal = 3; 30211084Ssam goto again; 30310864Ssam 30411084Ssam case 3: 30510334Shelge donerecal: 30610334Shelge hprecal = 0; 30711084Ssam goto again; 30810334Shelge } 30910608Ssam if (io->i_errcnt >= 16) { 31010608Ssam hpaddr->hpcs1 = HP_RTC|HP_GO; 31110608Ssam while (hpaddr->hpds & HPDS_PIP) 31210608Ssam ; 31310334Shelge } 31411084Ssam goto again; 31511084Ssam 316*11140Ssam success: 317*11140Ssam /* 318*11140Ssam * On successful error recovery, bump 319*11140Ssam * block number to advance to next portion 320*11140Ssam * of i/o transfer. 321*11140Ssam */ 32210334Shelge bn++; 32310334Shelge if ((bn-startblock) * sectsiz < bytecnt) { 324*11140Ssam again: 32510334Shelge io->i_bn = bn; 32610334Shelge io->i_ma = membase + (io->i_bn - startblock)*sectsiz; 32710334Shelge io->i_cc = bytecnt - (io->i_bn - startblock)*sectsiz; 32810334Shelge #ifdef HPDEBUG 32910608Ssam printf("restart: bl %d, byte %d, mem 0x%x hprecal %d\n", 33010608Ssam io->i_bn, io->i_cc, io->i_ma, hprecal); 33110334Shelge #endif 33210626Shelge goto restart; 33310334Shelge } 33410334Shelge return (bytecnt); 33510334Shelge } 33610864Ssam 33710334Shelge hpecc(io, flag) 33810334Shelge register struct iob *io; 33910334Shelge int flag; 34010334Shelge { 34110334Shelge register unit = io->i_unit; 34210334Shelge register struct mba_regs *mbp = mbamba(unit); 34310334Shelge register struct hpdevice *rp = (struct hpdevice *)mbadrv(unit); 34410353Shelge register struct st *st = &hpst[hp_type[unit]]; 34511084Ssam int npf, bn, cn, tn, sn, bcr; 34610334Shelge 34710334Shelge if (bcr = MASKREG(mbp->mba_bcr>>16)) 34810334Shelge bcr |= 0xffff0000; /* sxt */ 34911084Ssam npf = (bcr + io->i_cc) / sectsiz; /* # sectors read */ 35011084Ssam bn = io->i_bn + npf + ssect; /* physical block #*/ 35111084Ssam 352*11140Ssam /* 353*11140Ssam * ECC correction logic. 354*11140Ssam */ 355*11140Ssam if (flag == ECC) { 35610334Shelge register int i; 35710334Shelge caddr_t addr; 35810334Shelge int bit, byte, mask, ecccnt = 0; 35910334Shelge 36010413Shelge printf("hp%d: soft ecc sn%d\n", unit, bn); 36110334Shelge mask = MASKREG(rp->hpec2); 36211084Ssam i = MASKREG(rp->hpec1) - 1; /* -1 makes 0 origin */ 36310334Shelge bit = i&07; 36410334Shelge i = (i&~07)>>3; 36510334Shelge byte = i; 36610334Shelge rp->hpcs1 = HP_DCLR | HP_GO; 36710334Shelge while (i <sectsiz && npf*sectsiz + i < io->i_cc && bit > -11) { 36810334Shelge addr = io->i_ma + (npf*sectsiz) + byte; 36910334Shelge #ifdef HPECCDEBUG 37010334Shelge printf("addr %x old:%x ",addr, (*addr&0xff)); 37110334Shelge #endif 372*11140Ssam /* don't correct in-core copy during wcheck */ 37310334Shelge if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) 374*11140Ssam *addr ^= (mask << bit); 37510334Shelge #ifdef HPECCDEBUG 37610334Shelge printf("new:%x\n",(*addr&0xff)); 37710334Shelge #endif 378*11140Ssam byte++, i++; 37910334Shelge bit -= 8; 38011109Ssam if ((io->i_flgs & F_ECCLM) && ecccnt++ >= MAXECC) 38111084Ssam return (1); 38210334Shelge } 38310334Shelge return(0); 38411084Ssam } 38510334Shelge 38611084Ssam /* 38711084Ssam * Skip sector error. 38811084Ssam * Set skip-sector-inhibit and 38911084Ssam * read next sector 39011084Ssam */ 391*11140Ssam if (flag == SSE) { 39210334Shelge rp->hpcs1 = HP_DCLR | HP_GO; 39310864Ssam HPWAIT(rp); 39410334Shelge rp->hpof |= HPOF_SSEI; 39511084Ssam return (0); 396*11140Ssam } 39710334Shelge 398*11140Ssam /* 399*11140Ssam * Bad block forwarding. 400*11140Ssam */ 401*11140Ssam if (flag == BSE) { 40210626Shelge int bbn; 40311084Ssam 40410626Shelge rp->hpcs1 = HP_DCLR | HP_GO; 40510334Shelge #ifdef HPDEBUG 40610334Shelge printf("hpecc: BSE @ bn %d\n", bn); 40710334Shelge #endif 40810334Shelge cn = bn/st->nspc; 40910334Shelge sn = bn%st->nspc; 41010334Shelge tn = sn/st->nsect; 41110626Shelge sn = sn%st->nsect; 41210626Shelge bcr += sectsiz; 41310626Shelge if ((bbn = isbad(&hpbad[unit], cn, tn, sn)) < 0) 41411084Ssam return (1); 41510626Shelge bbn = st->ncyl*st->nspc - st->nsect - 1 - bbn; 41610626Shelge cn = bbn/st->nspc; 41710626Shelge sn = bbn%st->nspc; 41810626Shelge tn = sn/st->nsect; 41910626Shelge sn = sn%st->nsect; 42010626Shelge io->i_cc = sectsiz; 42110626Shelge io->i_ma += npf*sectsiz; 42210626Shelge #ifdef HPDEBUG 42310626Shelge printf("revector to cn %d tn %d sn %d mem: 0x%x\n", 42410626Shelge cn, tn, sn, io->i_ma); 42510626Shelge #endif 42610647Shelge rp->hpof &= ~HPOF_SSEI; /* clear skip sector inhibit if set */ 42710626Shelge mbp->mba_sr = -1; 42810334Shelge rp->hpdc = cn; 42910334Shelge rp->hpda = (tn<<8) + sn; 43010334Shelge mbastart(io,io->i_flgs); 43110334Shelge io->i_errcnt = 0; /* error has been corrected */ 43210864Ssam HPWAIT(rp); 43310864Ssam return (rp->hpds&HPDS_ERR); 43410334Shelge } 43511084Ssam printf("hpecc: flag=%d\n", flag); 43611084Ssam return (1); 43710334Shelge } 438*11140Ssam 43910334Shelge /*ARGSUSED*/ 44010334Shelge hpioctl(io, cmd, arg) 44110334Shelge struct iob *io; 44210334Shelge int cmd; 44310334Shelge caddr_t arg; 44410334Shelge { 44510647Shelge register unit = io->i_unit; 44610647Shelge struct st *st = &hpst[hp_type[unit]], *tmp; 44710647Shelge struct mba_drv *drv = mbadrv(unit); 44810334Shelge 44910334Shelge switch(cmd) { 45010334Shelge 45110334Shelge case SAIODEVDATA: 45210334Shelge if ((drv->mbd_dt&MBDT_TAP) == 0) { 45310353Shelge tmp = (struct st *)arg; 45410353Shelge *tmp = *st; 45511084Ssam return (0); 45610334Shelge } 45711084Ssam return (ECMD); 45810334Shelge 45911084Ssam case SAIOSSI: /* skip-sector-inhibit */ 46011084Ssam if (drv->mbd_dt&MBDT_TAP) 46111084Ssam return (ECMD); 46211084Ssam if ((io->i_flgs&F_SSI) == 0) { 46311084Ssam /* make sure this is done once only */ 46411084Ssam io->i_flgs |= F_SSI; 46511084Ssam st->nsect++; 46611084Ssam st->nspc += st->ntrak; 46710864Ssam } 46811084Ssam return (0); 46910626Shelge 47011084Ssam case SAIONOSSI: /* remove skip-sector-inhibit */ 47110626Shelge if (io->i_flgs & F_SSI) { 47210626Shelge io->i_flgs &= ~F_SSI; 47310626Shelge drv->mbd_of &= ~HPOF_SSEI; 47410626Shelge st->nsect--; 47510626Shelge st->nspc -= st->ntrak; 47610626Shelge } 47710626Shelge return(0); 47810626Shelge 47910864Ssam case SAIOSSDEV: /* drive have skip sector? */ 48011084Ssam return (RM80 ? 0 : ECMD); 48110334Shelge } 48211084Ssam return (ECMD); 48310334Shelge } 484