1*11386Ssam /* up.c 4.12 83/03/02 */ 29974Ssam 310023Ssam /* 410023Ssam * UNIBUS peripheral standalone driver 510023Ssam * with ECC correction and bad block forwarding. 610023Ssam * Also supports header operation and write 710023Ssam * check for data and/or header. 810023Ssam */ 99974Ssam #include "../h/param.h" 109974Ssam #include "../h/inode.h" 119974Ssam #include "../h/fs.h" 129974Ssam #include "../h/dkbad.h" 139974Ssam #include "../h/vmmac.h" 149974Ssam 159974Ssam #include "../vax/pte.h" 169974Ssam #include "../vaxuba/upreg.h" 179974Ssam #include "../vaxuba/ubareg.h" 189974Ssam 1910023Ssam #include "saio.h" 209974Ssam #include "savax.h" 219974Ssam 2210352Shelge #define MAXBADDESC 126 /* max number of bad sectors recorded */ 2310352Shelge #define SECTSIZ 512 /* sector size in bytes */ 2410352Shelge #define HDRSIZ 4 /* number of bytes in sector header */ 2511365Ssam 2611118Ssam #define MAXECC 5 /* max # bad bits allowed on ecc w/ F_ECCLM */ 2710352Shelge 2810023Ssam u_short ubastd[] = { 0776700 }; 299974Ssam 3011365Ssam char up_gottype[MAXNUBA*8]; 3111365Ssam char up_type[MAXNUBA*8]; 3211143Ssam extern struct st upst[]; 3310023Ssam 3411365Ssam struct dkbad upbad[MAXNUBA*8]; /* bad sector table */ 3511365Ssam int sectsiz; /* real sector size */ 3611365Ssam int updebug[MAXNUBA*8]; 3711365Ssam #define UPF_BSEDEBUG 01 /* debugging bad sector forwarding */ 3811365Ssam #define UPF_ECCDEBUG 02 /* debugging ecc correction */ 3911365Ssam 409974Ssam u_char up_offset[16] = { 419974Ssam UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400, 429974Ssam UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 439974Ssam UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200, 449974Ssam 0, 0, 0, 0 459974Ssam }; 469974Ssam 479974Ssam upopen(io) 489974Ssam register struct iob *io; 499974Ssam { 5010352Shelge register unit = io->i_unit; 5110023Ssam register struct updevice *upaddr; 5211118Ssam register struct st *st; 539974Ssam 5411143Ssam if (io->i_boff < 0 || io->i_boff > 7) 5510023Ssam _stop("up bad unit"); 5610352Shelge upaddr = (struct updevice *)ubamem(unit, ubastd[0]); 5711085Ssam while ((upaddr->upcs1 & UP_DVA) == 0) 589974Ssam ; 5910352Shelge if (up_gottype[unit] == 0) { 6010023Ssam register int i; 6110023Ssam struct iob tio; 6210023Ssam 6311118Ssam up_type[unit] = upmaptype(unit, upaddr); 6411118Ssam if (up_type[unit] < 0) 6510023Ssam _stop("unknown drive type"); 6611118Ssam st = &upst[up_type[unit]]; 6711143Ssam if (st->off[io->i_boff] == -1) 6811143Ssam _stop("up bad unit"); 6910023Ssam /* 7011365Ssam * Read in the bad sector table. 7110023Ssam */ 7210023Ssam tio = *io; 7310023Ssam tio.i_bn = st->nspc * st->ncyl - st->nsect; 749974Ssam tio.i_ma = (char *)&upbad[tio.i_unit]; 7510638Shelge tio.i_cc = sizeof (struct dkbad); 7610023Ssam tio.i_flgs |= F_RDDATA; 7710023Ssam for (i = 0; i < 5; i++) { 7810638Shelge if (upstrategy(&tio, READ) == sizeof (struct dkbad)) 7910023Ssam break; 809974Ssam tio.i_bn += 2; 819974Ssam } 829974Ssam if (i == 5) { 8310023Ssam printf("Unable to read bad sector table\n"); 8410352Shelge for (i = 0; i < MAXBADDESC; i++) { 8510352Shelge upbad[unit].bt_bad[i].bt_cyl = -1; 8610352Shelge upbad[unit].bt_bad[i].bt_trksec = -1; 879974Ssam } 889974Ssam } 8910352Shelge up_gottype[unit] = 1; 909974Ssam } 919974Ssam io->i_boff = st->off[io->i_boff] * st->nspc; 9210023Ssam io->i_flgs &= ~F_TYPEMASK; 939974Ssam } 949974Ssam 959974Ssam upstrategy(io, func) 969974Ssam register struct iob *io; 979974Ssam { 9811317Ssam int cn, tn, sn, o; 9910352Shelge register unit = io->i_unit; 1009974Ssam daddr_t bn; 1019974Ssam int recal, info, waitdry; 1029974Ssam register struct updevice *upaddr = 10310352Shelge (struct updevice *)ubamem(unit, ubastd[0]); 10410352Shelge register struct st *st = &upst[up_type[unit]]; 10511317Ssam int doprintf = 0; 1069974Ssam 10710352Shelge sectsiz = SECTSIZ; 10811085Ssam if (io->i_flgs & (F_HDR|F_HCHECK)) 10910352Shelge sectsiz += HDRSIZ; 11011365Ssam upaddr->upcs2 = unit % 8; 1119974Ssam if ((upaddr->upds & UPDS_VV) == 0) { 1129974Ssam upaddr->upcs1 = UP_DCLR|UP_GO; 1139974Ssam upaddr->upcs1 = UP_PRESET|UP_GO; 1149974Ssam upaddr->upof = UPOF_FMT22; 1159974Ssam } 11611085Ssam if ((upaddr->upds & UPDS_DREADY) == 0) 1179974Ssam _stop("up not ready"); 1189974Ssam info = ubasetup(io, 1); 1199974Ssam upaddr->upwc = -io->i_cc / sizeof (short); 12011085Ssam recal = 0; 12111085Ssam io->i_errcnt = 0; 12211085Ssam 12310638Shelge restart: 12411317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 12511317Ssam upaddr->upba = info + o; 12611317Ssam bn = io->i_bn + o / sectsiz; 12711384Ssam if (doprintf && updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 128*11386Ssam printf("wc=%d o=%d i_bn=%d bn=%d\n", 12911365Ssam upaddr->upwc, o, io->i_bn, bn); 13010023Ssam while((upaddr->upds & UPDS_DRY) == 0) 13110023Ssam ; 13210352Shelge if (upstart(io, bn) != 0) { 13310352Shelge ubafree(io, info); 1349974Ssam return (-1); 13510352Shelge } 1369974Ssam do { 1379974Ssam DELAY(25); 1389974Ssam } while ((upaddr->upcs1 & UP_RDY) == 0); 13911085Ssam /* 14011085Ssam * If transfer has completed, free UNIBUS 14111085Ssam * resources and return transfer size. 14211085Ssam */ 14311085Ssam if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) { 14410352Shelge ubafree(io, info); 14511085Ssam return (io->i_cc); 14610352Shelge } 14711365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 14811365Ssam printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 149*11386Ssam upaddr->updc, upaddr->upda>>8, (upaddr->upda&0x1f)-1); 150*11386Ssam printf("cs2=%b er1=%b er2=%b wc=%d\n", 15111365Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 152*11386Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc); 15311365Ssam } 1549974Ssam waitdry = 0; 155*11386Ssam while ((upaddr->upds&UPDS_DRY) == 0 && ++waitdry < sectsiz) 15610023Ssam DELAY(5); 15711085Ssam if (upaddr->uper1&UPER1_WLE) { 15811085Ssam /* 15911085Ssam * Give up on write locked devices immediately. 16011085Ssam */ 16111085Ssam printf("up%d: write locked\n", unit); 16211085Ssam return (-1); 16311085Ssam } 1649974Ssam if (++io->i_errcnt > 27) { 1659974Ssam /* 1669974Ssam * After 28 retries (16 without offset, and 1679974Ssam * 12 with offset positioning) give up. 16811317Ssam * But first, if the error is a header CRC, 16911317Ssam * check if a replacement sector exists in 17011317Ssam * the bad sector table. 1719974Ssam */ 17211317Ssam if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 17311317Ssam upecc(io, BSE) == 0) 17411317Ssam goto success; 17510023Ssam io->i_error = EHER; 17610023Ssam if (upaddr->upcs2 & UPCS2_WCE) 17710023Ssam io->i_error = EWCK; 17810352Shelge hard: 17911085Ssam bn = io->i_bn + 18011085Ssam (io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz; 1819974Ssam cn = bn/st->nspc; 1829974Ssam sn = bn%st->nspc; 1839974Ssam tn = sn/st->nsect; 1849974Ssam sn = sn%st->nsect; 18511085Ssam printf( 18611085Ssam "up error: (cyl,trk,sec)=(%d,%d,%d) cs2=%b er1=%b er2=%b\n", 18711085Ssam cn, tn, sn, 18811085Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 18911085Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS); 1909974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 19110352Shelge io->i_errblk = bn; 19211085Ssam return (io->i_cc + upaddr->upwc * sizeof(short)); 19311085Ssam } 19411085Ssam if (upaddr->uper2 & UPER2_BSE) { 19511143Ssam if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 1969974Ssam goto success; 19711085Ssam io->i_error = EBSE; 19811085Ssam goto hard; 1999974Ssam } 20011085Ssam /* 201*11386Ssam * ECC error. If a soft error, correct it; 202*11386Ssam * otherwise fall through and retry the transfer. 20311085Ssam */ 204*11386Ssam if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH)) == UPER1_DCK) { 205*11386Ssam if (upecc(io, ECC) == 0) 206*11386Ssam goto success; 207*11386Ssam io->i_error = EECC; 208*11386Ssam goto hard; 20911085Ssam } 2109974Ssam /* 2119974Ssam * Clear drive error and, every eight attempts, 2129974Ssam * (starting with the fourth) 2139974Ssam * recalibrate to clear the slate. 2149974Ssam */ 2159974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 21610638Shelge if ((io->i_errcnt&07) == 4 ) { 2179974Ssam upaddr->upcs1 = UP_RECAL|UP_GO; 21810638Shelge recal = 1; 21910638Shelge goto restart; 2209974Ssam } 2219974Ssam /* 2229974Ssam * Advance recalibration finite state machine 2239974Ssam * if recalibrate in progress, through 2249974Ssam * RECAL 2259974Ssam * SEEK 2269974Ssam * OFFSET (optional) 2279974Ssam * RETRY 2289974Ssam */ 2299974Ssam switch (recal) { 2309974Ssam 2319974Ssam case 1: 2329974Ssam upaddr->updc = cn; 2339974Ssam upaddr->upcs1 = UP_SEEK|UP_GO; 23411085Ssam recal = 2; 23510638Shelge goto restart; 23610023Ssam 2379974Ssam case 2: 2389974Ssam if (io->i_errcnt < 16 || (func & READ) == 0) 2399974Ssam goto donerecal; 2409974Ssam upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 2419974Ssam upaddr->upcs1 = UP_OFFSET|UP_GO; 24211085Ssam recal = 3; 24310638Shelge goto restart; 24410023Ssam 2459974Ssam donerecal: 2469974Ssam case 3: 2479974Ssam recal = 0; 2489974Ssam break; 2499974Ssam } 2509974Ssam /* 25110638Shelge * If we were offset positioning, 25210638Shelge * return to centerline. 2539974Ssam */ 25410638Shelge if (io->i_errcnt >= 16) { 25510638Shelge upaddr->upof = UPOF_FMT22; 25610638Shelge upaddr->upcs1 = UP_RTC|UP_GO; 25710638Shelge while ((upaddr->upds&UPDS_DRY) == 0) 25810638Shelge DELAY(25); 2599974Ssam } 26010638Shelge goto restart; 26111085Ssam 2629974Ssam success: 263*11386Ssam #define rounddown(x, y) (((x) / (y)) * (y)) 264*11386Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 265*11386Ssam if (upaddr->upwc) { 26611317Ssam doprintf++; 26710638Shelge goto restart; 26811317Ssam } 2699974Ssam /* 27011365Ssam * Release UNIBUS 2719974Ssam */ 2729974Ssam ubafree(io, info); 2739974Ssam return (io->i_cc); 2749974Ssam } 2759974Ssam 2769974Ssam /* 27711143Ssam * Correct an ECC error, and restart the 27811143Ssam * i/o to complete the transfer (if necessary). 27911143Ssam * This is quite complicated because the transfer 28011143Ssam * may be going to an odd memory address base and/or 2819974Ssam * across a page boundary. 2829974Ssam */ 28310023Ssam upecc(io, flag) 2849974Ssam register struct iob *io; 2859974Ssam int flag; 2869974Ssam { 28711365Ssam register i, unit = io->i_unit; 2889974Ssam register struct updevice *up = 28911365Ssam (struct updevice *)ubamem(unit, ubastd[0]); 29010352Shelge register struct st *st; 2919974Ssam caddr_t addr; 29210410Shelge int bn, twc, npf, mask, cn, tn, sn; 29310352Shelge daddr_t bbn; 2949974Ssam 2959974Ssam /* 29611143Ssam * Npf is the number of sectors transferred 29711143Ssam * before the sector containing the ECC error; 29811143Ssam * bn is the current block number. 2999974Ssam */ 30010352Shelge twc = up->upwc; 30111143Ssam npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 30211365Ssam if (updebug[unit] & UPF_ECCDEBUG) 303*11386Ssam printf("npf=%d mask=0x%x ec1=%d wc=%d\n", 304*11386Ssam npf, up->upec2, up->upec1, twc); 30511317Ssam bn = io->i_bn + npf; 30611365Ssam st = &upst[up_type[unit]]; 30710410Shelge cn = bn/st->nspc; 30810410Shelge sn = bn%st->nspc; 30910410Shelge tn = sn/st->nsect; 31010410Shelge sn = sn%st->nsect; 31111143Ssam 3129974Ssam /* 31311143Ssam * ECC correction. 3149974Ssam */ 3159974Ssam if (flag == ECC) { 31611317Ssam int bit, o, ecccnt; 31711085Ssam 31810352Shelge ecccnt = 0; 3199974Ssam mask = up->upec2; 32011365Ssam printf("up%d: soft ecc sn%d\n", unit, bn); 3219974Ssam /* 32211317Ssam * Compute the byte and bit position of 32311317Ssam * the error. o is the byte offset in 32411317Ssam * the transfer at which the correction 32511317Ssam * applied. 3269974Ssam */ 32711317Ssam npf--; 3289974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 329*11386Ssam bit = i & 07; 330*11386Ssam o = (i & ~07) >> 3; 3319974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3329974Ssam /* 33311143Ssam * Correct while possible bits remain of mask. 33411143Ssam * Since mask contains 11 bits, we continue while 33511143Ssam * the bit offset is > -11. Also watch out for 33611143Ssam * end of this block and the end of the transfer. 3379974Ssam */ 33811317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3399974Ssam /* 34011143Ssam * addr = 34111317Ssam * (base address of transfer) + 34211143Ssam * (# sectors transferred before the error) * 34311143Ssam * (sector size) + 34411317Ssam * (byte offset to incorrect data) 3459974Ssam */ 34611317Ssam addr = io->i_ma + (npf * sectsiz) + o; 34711317Ssam /* 34811317Ssam * No data transfer occurs with a write check, 34911317Ssam * so don't correct the resident copy of data. 35011317Ssam */ 35111365Ssam if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 35211365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35311365Ssam printf("addr=0x%x old=0x%x ", addr, 35411365Ssam (*addr&0xff)); 35510352Shelge *addr ^= (mask << bit); 35611365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35711365Ssam printf("new=0x%x\n", (*addr&0xff)); 35811365Ssam } 35911317Ssam o++, bit -= 8; 36011085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 36111085Ssam return (1); 3629974Ssam } 36311085Ssam return (0); 36411085Ssam } 36511143Ssam 36611143Ssam /* 36711143Ssam * Bad sector forwarding. 36811143Ssam */ 36911085Ssam if (flag == BSE) { 3709974Ssam /* 37111143Ssam * If not in bad sector table, 37211143Ssam * indicate a hard error to caller. 3739974Ssam */ 37410352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 37511365Ssam if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 37611085Ssam return (1); 377*11386Ssam bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn; 37810352Shelge twc = up->upwc + sectsiz; 37911085Ssam up->upwc = - (sectsiz / sizeof (short)); 38011365Ssam if (updebug[unit] & UPF_BSEDEBUG) 38111365Ssam printf("revector sn %d to %d\n", sn, bbn); 3829974Ssam /* 38311143Ssam * Clear the drive & read the replacement 38411143Ssam * sector. If this is in the middle of a 38511143Ssam * transfer, then set up the controller 38611143Ssam * registers in a normal fashion. 38711143Ssam * The UNIBUS address need not be changed. 38811143Ssam */ 389*11386Ssam while ((up->upcs1 & UP_RDY) == 0) 3909974Ssam ; 391*11386Ssam if (upstart(io, bbn)) 39210352Shelge return (1); /* error */ 39310352Shelge io->i_errcnt = 0; /* success */ 3949974Ssam do { 3959974Ssam DELAY(25); 396*11386Ssam } while ((up->upcs1 & UP_RDY) == 0) ; 397*11386Ssam if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) { 398*11386Ssam up->upwc = twc - sectsiz; 39910352Shelge return (1); 4009974Ssam } 4019974Ssam } 40210638Shelge if (twc) 4039974Ssam up->upwc = twc; 40410352Shelge return (0); 4059974Ssam } 4069974Ssam 4079974Ssam upstart(io, bn) 40810023Ssam register struct iob *io; 40910023Ssam daddr_t bn; 4109974Ssam { 4119974Ssam register struct updevice *upaddr = 41210023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 41310352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4149974Ssam int sn, tn; 4159974Ssam 4169974Ssam sn = bn%st->nspc; 4179974Ssam tn = sn/st->nsect; 4189974Ssam sn %= st->nsect; 4199974Ssam upaddr->updc = bn/st->nspc; 4209974Ssam upaddr->upda = (tn << 8) + sn; 42110352Shelge switch (io->i_flgs & F_TYPEMASK) { 42210023Ssam 42310023Ssam case F_RDDATA: 42410023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4259974Ssam break; 42610023Ssam 42710023Ssam case F_WRDATA: 42810023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4299974Ssam break; 43010023Ssam 43110023Ssam case F_HDR|F_RDDATA: 43210023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 43310023Ssam break; 43410023Ssam 43510023Ssam case F_HDR|F_WRDATA: 43610023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 43710023Ssam break; 43810023Ssam 43910023Ssam case F_CHECK|F_WRDATA: 44010023Ssam case F_CHECK|F_RDDATA: 4419974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4429974Ssam break; 44310023Ssam 44410023Ssam case F_HCHECK|F_WRDATA: 44510023Ssam case F_HCHECK|F_RDDATA: 4469974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4479974Ssam break; 44810023Ssam 4499974Ssam default: 45010023Ssam io->i_error = ECMD; 45110023Ssam io->i_flgs &= ~F_TYPEMASK; 45210023Ssam return (1); 4539974Ssam } 45410023Ssam return (0); 4559974Ssam } 4569974Ssam 45710023Ssam /*ARGSUSED*/ 45810023Ssam upioctl(io, cmd, arg) 45910023Ssam struct iob *io; 46010023Ssam int cmd; 46110023Ssam caddr_t arg; 46210023Ssam { 46311365Ssam int unit = io->i_unit, flag; 46411365Ssam struct st *st = &upst[up_type[unit]], *tmp; 46510352Shelge 46610352Shelge switch(cmd) { 46710352Shelge 46811365Ssam case SAIODEBUG: 46911365Ssam flag = (int)arg; 47011365Ssam if (flag > 0) 47111365Ssam updebug[unit] |= flag; 47211365Ssam else 47311365Ssam updebug[unit] &= ~flag; 47411365Ssam return (0); 47511365Ssam 47610352Shelge case SAIODEVDATA: 47710352Shelge tmp = (struct st *)arg; 47810352Shelge *tmp = *st; 47911085Ssam return (0); 48010352Shelge } 48111085Ssam return (ECMD); 48210023Ssam } 483