1*15948Skarels /* up.c 6.3 84/02/06 */ 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]); 5715067Skarels upaddr->upcs2 = unit % 8; 5811085Ssam while ((upaddr->upcs1 & UP_DVA) == 0) 599974Ssam ; 6010352Shelge if (up_gottype[unit] == 0) { 6110023Ssam register int i; 6210023Ssam struct iob tio; 6310023Ssam 6411118Ssam up_type[unit] = upmaptype(unit, upaddr); 6511118Ssam if (up_type[unit] < 0) 6610023Ssam _stop("unknown drive type"); 6711118Ssam st = &upst[up_type[unit]]; 6811143Ssam if (st->off[io->i_boff] == -1) 6911143Ssam _stop("up bad unit"); 7010023Ssam /* 7111365Ssam * Read in the bad sector table. 7210023Ssam */ 7310023Ssam tio = *io; 7410023Ssam tio.i_bn = st->nspc * st->ncyl - st->nsect; 759974Ssam tio.i_ma = (char *)&upbad[tio.i_unit]; 7610638Shelge tio.i_cc = sizeof (struct dkbad); 7710023Ssam tio.i_flgs |= F_RDDATA; 7810023Ssam for (i = 0; i < 5; i++) { 7910638Shelge if (upstrategy(&tio, READ) == sizeof (struct dkbad)) 8010023Ssam break; 819974Ssam tio.i_bn += 2; 829974Ssam } 839974Ssam if (i == 5) { 8410023Ssam printf("Unable to read bad sector table\n"); 8510352Shelge for (i = 0; i < MAXBADDESC; i++) { 8610352Shelge upbad[unit].bt_bad[i].bt_cyl = -1; 8710352Shelge upbad[unit].bt_bad[i].bt_trksec = -1; 889974Ssam } 899974Ssam } 9010352Shelge up_gottype[unit] = 1; 919974Ssam } 92*15948Skarels st = &upst[up_type[unit]]; 939974Ssam io->i_boff = st->off[io->i_boff] * st->nspc; 9410023Ssam io->i_flgs &= ~F_TYPEMASK; 959974Ssam } 969974Ssam 979974Ssam upstrategy(io, func) 989974Ssam register struct iob *io; 999974Ssam { 10011317Ssam int cn, tn, sn, o; 10110352Shelge register unit = io->i_unit; 1029974Ssam daddr_t bn; 1039974Ssam int recal, info, waitdry; 1049974Ssam register struct updevice *upaddr = 10510352Shelge (struct updevice *)ubamem(unit, ubastd[0]); 10610352Shelge register struct st *st = &upst[up_type[unit]]; 10711317Ssam int doprintf = 0; 1089974Ssam 10910352Shelge sectsiz = SECTSIZ; 11011085Ssam if (io->i_flgs & (F_HDR|F_HCHECK)) 11110352Shelge sectsiz += HDRSIZ; 11211365Ssam upaddr->upcs2 = unit % 8; 1139974Ssam if ((upaddr->upds & UPDS_VV) == 0) { 1149974Ssam upaddr->upcs1 = UP_DCLR|UP_GO; 1159974Ssam upaddr->upcs1 = UP_PRESET|UP_GO; 1169974Ssam upaddr->upof = UPOF_FMT22; 1179974Ssam } 11811085Ssam if ((upaddr->upds & UPDS_DREADY) == 0) 1199974Ssam _stop("up not ready"); 1209974Ssam info = ubasetup(io, 1); 1219974Ssam upaddr->upwc = -io->i_cc / sizeof (short); 12211085Ssam recal = 0; 12311085Ssam io->i_errcnt = 0; 12411085Ssam 12510638Shelge restart: 12611317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 12711317Ssam upaddr->upba = info + o; 12811317Ssam bn = io->i_bn + o / sectsiz; 12911384Ssam if (doprintf && updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 13011386Ssam printf("wc=%d o=%d i_bn=%d bn=%d\n", 13111365Ssam upaddr->upwc, o, io->i_bn, bn); 13210023Ssam while((upaddr->upds & UPDS_DRY) == 0) 13310023Ssam ; 13410352Shelge if (upstart(io, bn) != 0) { 13510352Shelge ubafree(io, info); 1369974Ssam return (-1); 13710352Shelge } 1389974Ssam do { 1399974Ssam DELAY(25); 1409974Ssam } while ((upaddr->upcs1 & UP_RDY) == 0); 14111085Ssam /* 14211085Ssam * If transfer has completed, free UNIBUS 14311085Ssam * resources and return transfer size. 14411085Ssam */ 14515067Skarels if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) 14615067Skarels goto done; 14711365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 14811365Ssam printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 14915067Skarels upaddr->updc, upaddr->upda>>8, upaddr->upda&0xff); 15011386Ssam printf("cs2=%b er1=%b er2=%b wc=%d\n", 15111365Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 15211386Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc); 15311365Ssam } 1549974Ssam waitdry = 0; 15511386Ssam 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; 19215067Skarels if (io->i_errcnt >= 16) { 19315067Skarels upaddr->upof = UPOF_FMT22; 19415067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 19515067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 19615067Skarels DELAY(25); 19715067Skarels } 19811085Ssam return (io->i_cc + upaddr->upwc * sizeof(short)); 19911085Ssam } 20011085Ssam if (upaddr->uper2 & UPER2_BSE) { 20111143Ssam if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 2029974Ssam goto success; 20311085Ssam io->i_error = EBSE; 20411085Ssam goto hard; 2059974Ssam } 20611085Ssam /* 20711386Ssam * ECC error. If a soft error, correct it; 20811386Ssam * otherwise fall through and retry the transfer. 20911085Ssam */ 21015067Skarels if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH|UPER1_HCRC)) == UPER1_DCK) { 21111386Ssam if (upecc(io, ECC) == 0) 21215067Skarels #ifdef F_SEVRE 21315067Skarels if (io->i_flgs & F_SEVRE) 21415067Skarels return (-1); 21515067Skarels else 21615067Skarels #endif 21711386Ssam goto success; 21811386Ssam io->i_error = EECC; 21911386Ssam goto hard; 22011085Ssam } 22115067Skarels #ifdef F_SEVRE 22215067Skarels if (io->i_flgs & F_SEVRE) 22315067Skarels goto hard; 22415067Skarels #endif 2259974Ssam /* 2269974Ssam * Clear drive error and, every eight attempts, 2279974Ssam * (starting with the fourth) 2289974Ssam * recalibrate to clear the slate. 2299974Ssam */ 2309974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 23110638Shelge if ((io->i_errcnt&07) == 4 ) { 2329974Ssam upaddr->upcs1 = UP_RECAL|UP_GO; 23315067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 23415067Skarels DELAY(25); 2359974Ssam upaddr->updc = cn; 2369974Ssam upaddr->upcs1 = UP_SEEK|UP_GO; 23715067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 23815067Skarels DELAY(25); 23915067Skarels } 24015067Skarels if (io->i_errcnt >= 16 && (func & READ)) { 2419974Ssam upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 2429974Ssam upaddr->upcs1 = UP_OFFSET|UP_GO; 24310638Shelge while ((upaddr->upds&UPDS_DRY) == 0) 24410638Shelge DELAY(25); 2459974Ssam } 24610638Shelge goto restart; 24711085Ssam 2489974Ssam success: 24911386Ssam #define rounddown(x, y) (((x) / (y)) * (y)) 25011386Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 25111386Ssam if (upaddr->upwc) { 25211317Ssam doprintf++; 25310638Shelge goto restart; 25411317Ssam } 25515067Skarels done: 2569974Ssam /* 25711365Ssam * Release UNIBUS 2589974Ssam */ 2599974Ssam ubafree(io, info); 26015067Skarels /* 26115067Skarels * If we were offset positioning, 26215067Skarels * return to centerline. 26315067Skarels */ 26415067Skarels if (io->i_errcnt >= 16) { 26515067Skarels upaddr->upof = UPOF_FMT22; 26615067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 26715067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 26815067Skarels DELAY(25); 26915067Skarels } 2709974Ssam return (io->i_cc); 2719974Ssam } 2729974Ssam 2739974Ssam /* 27411143Ssam * Correct an ECC error, and restart the 27511143Ssam * i/o to complete the transfer (if necessary). 27611143Ssam * This is quite complicated because the transfer 27711143Ssam * may be going to an odd memory address base and/or 2789974Ssam * across a page boundary. 2799974Ssam */ 28010023Ssam upecc(io, flag) 2819974Ssam register struct iob *io; 2829974Ssam int flag; 2839974Ssam { 28411365Ssam register i, unit = io->i_unit; 2859974Ssam register struct updevice *up = 28611365Ssam (struct updevice *)ubamem(unit, ubastd[0]); 28710352Shelge register struct st *st; 2889974Ssam caddr_t addr; 28910410Shelge int bn, twc, npf, mask, cn, tn, sn; 29010352Shelge daddr_t bbn; 2919974Ssam 2929974Ssam /* 29311143Ssam * Npf is the number of sectors transferred 29411143Ssam * before the sector containing the ECC error; 29511143Ssam * bn is the current block number. 2969974Ssam */ 29710352Shelge twc = up->upwc; 29811143Ssam npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 29915067Skarels if (flag == ECC) 30015067Skarels npf--; 30111365Ssam if (updebug[unit] & UPF_ECCDEBUG) 30211386Ssam printf("npf=%d mask=0x%x ec1=%d wc=%d\n", 30311386Ssam npf, up->upec2, up->upec1, twc); 30411317Ssam bn = io->i_bn + npf; 30511365Ssam st = &upst[up_type[unit]]; 30610410Shelge cn = bn/st->nspc; 30710410Shelge sn = bn%st->nspc; 30810410Shelge tn = sn/st->nsect; 30910410Shelge sn = sn%st->nsect; 31011143Ssam 3119974Ssam /* 31211143Ssam * ECC correction. 3139974Ssam */ 3149974Ssam if (flag == ECC) { 31511317Ssam int bit, o, ecccnt; 31611085Ssam 31710352Shelge ecccnt = 0; 3189974Ssam mask = up->upec2; 31911365Ssam printf("up%d: soft ecc sn%d\n", unit, bn); 3209974Ssam /* 32111317Ssam * Compute the byte and bit position of 32211317Ssam * the error. o is the byte offset in 32311317Ssam * the transfer at which the correction 32411317Ssam * applied. 3259974Ssam */ 3269974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 32711386Ssam bit = i & 07; 32811386Ssam o = (i & ~07) >> 3; 3299974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3309974Ssam /* 33111143Ssam * Correct while possible bits remain of mask. 33211143Ssam * Since mask contains 11 bits, we continue while 33311143Ssam * the bit offset is > -11. Also watch out for 33411143Ssam * end of this block and the end of the transfer. 3359974Ssam */ 33611317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3379974Ssam /* 33811143Ssam * addr = 33911317Ssam * (base address of transfer) + 34011143Ssam * (# sectors transferred before the error) * 34111143Ssam * (sector size) + 34211317Ssam * (byte offset to incorrect data) 3439974Ssam */ 34411317Ssam addr = io->i_ma + (npf * sectsiz) + o; 34511317Ssam /* 34611317Ssam * No data transfer occurs with a write check, 34711317Ssam * so don't correct the resident copy of data. 34811317Ssam */ 34911365Ssam if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 35011365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35111365Ssam printf("addr=0x%x old=0x%x ", addr, 35211365Ssam (*addr&0xff)); 35310352Shelge *addr ^= (mask << bit); 35411365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35511365Ssam printf("new=0x%x\n", (*addr&0xff)); 35611365Ssam } 35711317Ssam o++, bit -= 8; 35811085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 35911085Ssam return (1); 3609974Ssam } 36115067Skarels #ifdef F_SEVRE 36215067Skarels if (io->i_flgs & F_SEVRE) { 36315067Skarels io->i_error = EECC; 36415067Skarels io->i_bn = bn; 36515067Skarels return(1); 36615067Skarels } 36715067Skarels #endif 36811085Ssam return (0); 36911085Ssam } 37011143Ssam 37111143Ssam /* 37211143Ssam * Bad sector forwarding. 37311143Ssam */ 37411085Ssam if (flag == BSE) { 3759974Ssam /* 37611143Ssam * If not in bad sector table, 37711143Ssam * indicate a hard error to caller. 3789974Ssam */ 37910352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 38011365Ssam if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 38111085Ssam return (1); 38211386Ssam bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn; 38310352Shelge twc = up->upwc + sectsiz; 38411085Ssam up->upwc = - (sectsiz / sizeof (short)); 38511365Ssam if (updebug[unit] & UPF_BSEDEBUG) 38611365Ssam printf("revector sn %d to %d\n", sn, bbn); 3879974Ssam /* 38811143Ssam * Clear the drive & read the replacement 38911143Ssam * sector. If this is in the middle of a 39011143Ssam * transfer, then set up the controller 39111143Ssam * registers in a normal fashion. 39211143Ssam * The UNIBUS address need not be changed. 39311143Ssam */ 39411386Ssam while ((up->upcs1 & UP_RDY) == 0) 3959974Ssam ; 39611386Ssam if (upstart(io, bbn)) 39710352Shelge return (1); /* error */ 39810352Shelge io->i_errcnt = 0; /* success */ 3999974Ssam do { 4009974Ssam DELAY(25); 40111386Ssam } while ((up->upcs1 & UP_RDY) == 0) ; 40211386Ssam if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) { 40311386Ssam up->upwc = twc - sectsiz; 40410352Shelge return (1); 4059974Ssam } 4069974Ssam } 40710638Shelge if (twc) 4089974Ssam up->upwc = twc; 40910352Shelge return (0); 4109974Ssam } 4119974Ssam 4129974Ssam upstart(io, bn) 41310023Ssam register struct iob *io; 41410023Ssam daddr_t bn; 4159974Ssam { 4169974Ssam register struct updevice *upaddr = 41710023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 41810352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4199974Ssam int sn, tn; 4209974Ssam 4219974Ssam sn = bn%st->nspc; 4229974Ssam tn = sn/st->nsect; 4239974Ssam sn %= st->nsect; 4249974Ssam upaddr->updc = bn/st->nspc; 4259974Ssam upaddr->upda = (tn << 8) + sn; 42610352Shelge switch (io->i_flgs & F_TYPEMASK) { 42710023Ssam 42810023Ssam case F_RDDATA: 42910023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4309974Ssam break; 43110023Ssam 43210023Ssam case F_WRDATA: 43310023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4349974Ssam break; 43510023Ssam 43610023Ssam case F_HDR|F_RDDATA: 43710023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 43810023Ssam break; 43910023Ssam 44010023Ssam case F_HDR|F_WRDATA: 44110023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 44210023Ssam break; 44310023Ssam 44410023Ssam case F_CHECK|F_WRDATA: 44510023Ssam case F_CHECK|F_RDDATA: 4469974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4479974Ssam break; 44810023Ssam 44910023Ssam case F_HCHECK|F_WRDATA: 45010023Ssam case F_HCHECK|F_RDDATA: 4519974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4529974Ssam break; 45310023Ssam 4549974Ssam default: 45510023Ssam io->i_error = ECMD; 45610023Ssam io->i_flgs &= ~F_TYPEMASK; 45710023Ssam return (1); 4589974Ssam } 45910023Ssam return (0); 4609974Ssam } 4619974Ssam 46210023Ssam /*ARGSUSED*/ 46310023Ssam upioctl(io, cmd, arg) 46410023Ssam struct iob *io; 46510023Ssam int cmd; 46610023Ssam caddr_t arg; 46710023Ssam { 46811365Ssam int unit = io->i_unit, flag; 46911365Ssam struct st *st = &upst[up_type[unit]], *tmp; 47010352Shelge 47110352Shelge switch(cmd) { 47210352Shelge 47311365Ssam case SAIODEBUG: 47411365Ssam flag = (int)arg; 47511365Ssam if (flag > 0) 47611365Ssam updebug[unit] |= flag; 47711365Ssam else 47811365Ssam updebug[unit] &= ~flag; 47911365Ssam return (0); 48011365Ssam 48110352Shelge case SAIODEVDATA: 48210352Shelge tmp = (struct st *)arg; 48310352Shelge *tmp = *st; 48411085Ssam return (0); 48510352Shelge } 48611085Ssam return (ECMD); 48710023Ssam } 488