1*15067Skarels /* up.c 6.2 83/09/23 */ 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]); 57*15067Skarels 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 } 929974Ssam io->i_boff = st->off[io->i_boff] * st->nspc; 9310023Ssam io->i_flgs &= ~F_TYPEMASK; 949974Ssam } 959974Ssam 969974Ssam upstrategy(io, func) 979974Ssam register struct iob *io; 989974Ssam { 9911317Ssam int cn, tn, sn, o; 10010352Shelge register unit = io->i_unit; 1019974Ssam daddr_t bn; 1029974Ssam int recal, info, waitdry; 1039974Ssam register struct updevice *upaddr = 10410352Shelge (struct updevice *)ubamem(unit, ubastd[0]); 10510352Shelge register struct st *st = &upst[up_type[unit]]; 10611317Ssam int doprintf = 0; 1079974Ssam 10810352Shelge sectsiz = SECTSIZ; 10911085Ssam if (io->i_flgs & (F_HDR|F_HCHECK)) 11010352Shelge sectsiz += HDRSIZ; 11111365Ssam upaddr->upcs2 = unit % 8; 1129974Ssam if ((upaddr->upds & UPDS_VV) == 0) { 1139974Ssam upaddr->upcs1 = UP_DCLR|UP_GO; 1149974Ssam upaddr->upcs1 = UP_PRESET|UP_GO; 1159974Ssam upaddr->upof = UPOF_FMT22; 1169974Ssam } 11711085Ssam if ((upaddr->upds & UPDS_DREADY) == 0) 1189974Ssam _stop("up not ready"); 1199974Ssam info = ubasetup(io, 1); 1209974Ssam upaddr->upwc = -io->i_cc / sizeof (short); 12111085Ssam recal = 0; 12211085Ssam io->i_errcnt = 0; 12311085Ssam 12410638Shelge restart: 12511317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 12611317Ssam upaddr->upba = info + o; 12711317Ssam bn = io->i_bn + o / sectsiz; 12811384Ssam if (doprintf && updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 12911386Ssam printf("wc=%d o=%d i_bn=%d bn=%d\n", 13011365Ssam upaddr->upwc, o, io->i_bn, bn); 13110023Ssam while((upaddr->upds & UPDS_DRY) == 0) 13210023Ssam ; 13310352Shelge if (upstart(io, bn) != 0) { 13410352Shelge ubafree(io, info); 1359974Ssam return (-1); 13610352Shelge } 1379974Ssam do { 1389974Ssam DELAY(25); 1399974Ssam } while ((upaddr->upcs1 & UP_RDY) == 0); 14011085Ssam /* 14111085Ssam * If transfer has completed, free UNIBUS 14211085Ssam * resources and return transfer size. 14311085Ssam */ 144*15067Skarels if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) 145*15067Skarels goto done; 14611365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 14711365Ssam printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 148*15067Skarels upaddr->updc, upaddr->upda>>8, upaddr->upda&0xff); 14911386Ssam printf("cs2=%b er1=%b er2=%b wc=%d\n", 15011365Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 15111386Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc); 15211365Ssam } 1539974Ssam waitdry = 0; 15411386Ssam while ((upaddr->upds&UPDS_DRY) == 0 && ++waitdry < sectsiz) 15510023Ssam DELAY(5); 15611085Ssam if (upaddr->uper1&UPER1_WLE) { 15711085Ssam /* 15811085Ssam * Give up on write locked devices immediately. 15911085Ssam */ 16011085Ssam printf("up%d: write locked\n", unit); 16111085Ssam return (-1); 16211085Ssam } 1639974Ssam if (++io->i_errcnt > 27) { 1649974Ssam /* 1659974Ssam * After 28 retries (16 without offset, and 1669974Ssam * 12 with offset positioning) give up. 16711317Ssam * But first, if the error is a header CRC, 16811317Ssam * check if a replacement sector exists in 16911317Ssam * the bad sector table. 1709974Ssam */ 17111317Ssam if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 17211317Ssam upecc(io, BSE) == 0) 17311317Ssam goto success; 17410023Ssam io->i_error = EHER; 17510023Ssam if (upaddr->upcs2 & UPCS2_WCE) 17610023Ssam io->i_error = EWCK; 17710352Shelge hard: 17811085Ssam bn = io->i_bn + 17911085Ssam (io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz; 1809974Ssam cn = bn/st->nspc; 1819974Ssam sn = bn%st->nspc; 1829974Ssam tn = sn/st->nsect; 1839974Ssam sn = sn%st->nsect; 18411085Ssam printf( 18511085Ssam "up error: (cyl,trk,sec)=(%d,%d,%d) cs2=%b er1=%b er2=%b\n", 18611085Ssam cn, tn, sn, 18711085Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 18811085Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS); 1899974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 19010352Shelge io->i_errblk = bn; 191*15067Skarels if (io->i_errcnt >= 16) { 192*15067Skarels upaddr->upof = UPOF_FMT22; 193*15067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 194*15067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 195*15067Skarels DELAY(25); 196*15067Skarels } 19711085Ssam return (io->i_cc + upaddr->upwc * sizeof(short)); 19811085Ssam } 19911085Ssam if (upaddr->uper2 & UPER2_BSE) { 20011143Ssam if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 2019974Ssam goto success; 20211085Ssam io->i_error = EBSE; 20311085Ssam goto hard; 2049974Ssam } 20511085Ssam /* 20611386Ssam * ECC error. If a soft error, correct it; 20711386Ssam * otherwise fall through and retry the transfer. 20811085Ssam */ 209*15067Skarels if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH|UPER1_HCRC)) == UPER1_DCK) { 21011386Ssam if (upecc(io, ECC) == 0) 211*15067Skarels #ifdef F_SEVRE 212*15067Skarels if (io->i_flgs & F_SEVRE) 213*15067Skarels return (-1); 214*15067Skarels else 215*15067Skarels #endif 21611386Ssam goto success; 21711386Ssam io->i_error = EECC; 21811386Ssam goto hard; 21911085Ssam } 220*15067Skarels #ifdef F_SEVRE 221*15067Skarels if (io->i_flgs & F_SEVRE) 222*15067Skarels goto hard; 223*15067Skarels #endif 2249974Ssam /* 2259974Ssam * Clear drive error and, every eight attempts, 2269974Ssam * (starting with the fourth) 2279974Ssam * recalibrate to clear the slate. 2289974Ssam */ 2299974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 23010638Shelge if ((io->i_errcnt&07) == 4 ) { 2319974Ssam upaddr->upcs1 = UP_RECAL|UP_GO; 232*15067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 233*15067Skarels DELAY(25); 2349974Ssam upaddr->updc = cn; 2359974Ssam upaddr->upcs1 = UP_SEEK|UP_GO; 236*15067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 237*15067Skarels DELAY(25); 238*15067Skarels } 239*15067Skarels if (io->i_errcnt >= 16 && (func & READ)) { 2409974Ssam upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 2419974Ssam upaddr->upcs1 = UP_OFFSET|UP_GO; 24210638Shelge while ((upaddr->upds&UPDS_DRY) == 0) 24310638Shelge DELAY(25); 2449974Ssam } 24510638Shelge goto restart; 24611085Ssam 2479974Ssam success: 24811386Ssam #define rounddown(x, y) (((x) / (y)) * (y)) 24911386Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 25011386Ssam if (upaddr->upwc) { 25111317Ssam doprintf++; 25210638Shelge goto restart; 25311317Ssam } 254*15067Skarels done: 2559974Ssam /* 25611365Ssam * Release UNIBUS 2579974Ssam */ 2589974Ssam ubafree(io, info); 259*15067Skarels /* 260*15067Skarels * If we were offset positioning, 261*15067Skarels * return to centerline. 262*15067Skarels */ 263*15067Skarels if (io->i_errcnt >= 16) { 264*15067Skarels upaddr->upof = UPOF_FMT22; 265*15067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 266*15067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 267*15067Skarels DELAY(25); 268*15067Skarels } 2699974Ssam return (io->i_cc); 2709974Ssam } 2719974Ssam 2729974Ssam /* 27311143Ssam * Correct an ECC error, and restart the 27411143Ssam * i/o to complete the transfer (if necessary). 27511143Ssam * This is quite complicated because the transfer 27611143Ssam * may be going to an odd memory address base and/or 2779974Ssam * across a page boundary. 2789974Ssam */ 27910023Ssam upecc(io, flag) 2809974Ssam register struct iob *io; 2819974Ssam int flag; 2829974Ssam { 28311365Ssam register i, unit = io->i_unit; 2849974Ssam register struct updevice *up = 28511365Ssam (struct updevice *)ubamem(unit, ubastd[0]); 28610352Shelge register struct st *st; 2879974Ssam caddr_t addr; 28810410Shelge int bn, twc, npf, mask, cn, tn, sn; 28910352Shelge daddr_t bbn; 2909974Ssam 2919974Ssam /* 29211143Ssam * Npf is the number of sectors transferred 29311143Ssam * before the sector containing the ECC error; 29411143Ssam * bn is the current block number. 2959974Ssam */ 29610352Shelge twc = up->upwc; 29711143Ssam npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 298*15067Skarels if (flag == ECC) 299*15067Skarels npf--; 30011365Ssam if (updebug[unit] & UPF_ECCDEBUG) 30111386Ssam printf("npf=%d mask=0x%x ec1=%d wc=%d\n", 30211386Ssam npf, up->upec2, up->upec1, twc); 30311317Ssam bn = io->i_bn + npf; 30411365Ssam st = &upst[up_type[unit]]; 30510410Shelge cn = bn/st->nspc; 30610410Shelge sn = bn%st->nspc; 30710410Shelge tn = sn/st->nsect; 30810410Shelge sn = sn%st->nsect; 30911143Ssam 3109974Ssam /* 31111143Ssam * ECC correction. 3129974Ssam */ 3139974Ssam if (flag == ECC) { 31411317Ssam int bit, o, ecccnt; 31511085Ssam 31610352Shelge ecccnt = 0; 3179974Ssam mask = up->upec2; 31811365Ssam printf("up%d: soft ecc sn%d\n", unit, bn); 3199974Ssam /* 32011317Ssam * Compute the byte and bit position of 32111317Ssam * the error. o is the byte offset in 32211317Ssam * the transfer at which the correction 32311317Ssam * applied. 3249974Ssam */ 3259974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 32611386Ssam bit = i & 07; 32711386Ssam o = (i & ~07) >> 3; 3289974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3299974Ssam /* 33011143Ssam * Correct while possible bits remain of mask. 33111143Ssam * Since mask contains 11 bits, we continue while 33211143Ssam * the bit offset is > -11. Also watch out for 33311143Ssam * end of this block and the end of the transfer. 3349974Ssam */ 33511317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3369974Ssam /* 33711143Ssam * addr = 33811317Ssam * (base address of transfer) + 33911143Ssam * (# sectors transferred before the error) * 34011143Ssam * (sector size) + 34111317Ssam * (byte offset to incorrect data) 3429974Ssam */ 34311317Ssam addr = io->i_ma + (npf * sectsiz) + o; 34411317Ssam /* 34511317Ssam * No data transfer occurs with a write check, 34611317Ssam * so don't correct the resident copy of data. 34711317Ssam */ 34811365Ssam if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 34911365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35011365Ssam printf("addr=0x%x old=0x%x ", addr, 35111365Ssam (*addr&0xff)); 35210352Shelge *addr ^= (mask << bit); 35311365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35411365Ssam printf("new=0x%x\n", (*addr&0xff)); 35511365Ssam } 35611317Ssam o++, bit -= 8; 35711085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 35811085Ssam return (1); 3599974Ssam } 360*15067Skarels #ifdef F_SEVRE 361*15067Skarels if (io->i_flgs & F_SEVRE) { 362*15067Skarels io->i_error = EECC; 363*15067Skarels io->i_bn = bn; 364*15067Skarels return(1); 365*15067Skarels } 366*15067Skarels #endif 36711085Ssam return (0); 36811085Ssam } 36911143Ssam 37011143Ssam /* 37111143Ssam * Bad sector forwarding. 37211143Ssam */ 37311085Ssam if (flag == BSE) { 3749974Ssam /* 37511143Ssam * If not in bad sector table, 37611143Ssam * indicate a hard error to caller. 3779974Ssam */ 37810352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 37911365Ssam if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 38011085Ssam return (1); 38111386Ssam bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn; 38210352Shelge twc = up->upwc + sectsiz; 38311085Ssam up->upwc = - (sectsiz / sizeof (short)); 38411365Ssam if (updebug[unit] & UPF_BSEDEBUG) 38511365Ssam printf("revector sn %d to %d\n", sn, bbn); 3869974Ssam /* 38711143Ssam * Clear the drive & read the replacement 38811143Ssam * sector. If this is in the middle of a 38911143Ssam * transfer, then set up the controller 39011143Ssam * registers in a normal fashion. 39111143Ssam * The UNIBUS address need not be changed. 39211143Ssam */ 39311386Ssam while ((up->upcs1 & UP_RDY) == 0) 3949974Ssam ; 39511386Ssam if (upstart(io, bbn)) 39610352Shelge return (1); /* error */ 39710352Shelge io->i_errcnt = 0; /* success */ 3989974Ssam do { 3999974Ssam DELAY(25); 40011386Ssam } while ((up->upcs1 & UP_RDY) == 0) ; 40111386Ssam if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) { 40211386Ssam up->upwc = twc - sectsiz; 40310352Shelge return (1); 4049974Ssam } 4059974Ssam } 40610638Shelge if (twc) 4079974Ssam up->upwc = twc; 40810352Shelge return (0); 4099974Ssam } 4109974Ssam 4119974Ssam upstart(io, bn) 41210023Ssam register struct iob *io; 41310023Ssam daddr_t bn; 4149974Ssam { 4159974Ssam register struct updevice *upaddr = 41610023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 41710352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4189974Ssam int sn, tn; 4199974Ssam 4209974Ssam sn = bn%st->nspc; 4219974Ssam tn = sn/st->nsect; 4229974Ssam sn %= st->nsect; 4239974Ssam upaddr->updc = bn/st->nspc; 4249974Ssam upaddr->upda = (tn << 8) + sn; 42510352Shelge switch (io->i_flgs & F_TYPEMASK) { 42610023Ssam 42710023Ssam case F_RDDATA: 42810023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4299974Ssam break; 43010023Ssam 43110023Ssam case F_WRDATA: 43210023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4339974Ssam break; 43410023Ssam 43510023Ssam case F_HDR|F_RDDATA: 43610023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 43710023Ssam break; 43810023Ssam 43910023Ssam case F_HDR|F_WRDATA: 44010023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 44110023Ssam break; 44210023Ssam 44310023Ssam case F_CHECK|F_WRDATA: 44410023Ssam case F_CHECK|F_RDDATA: 4459974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4469974Ssam break; 44710023Ssam 44810023Ssam case F_HCHECK|F_WRDATA: 44910023Ssam case F_HCHECK|F_RDDATA: 4509974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4519974Ssam break; 45210023Ssam 4539974Ssam default: 45410023Ssam io->i_error = ECMD; 45510023Ssam io->i_flgs &= ~F_TYPEMASK; 45610023Ssam return (1); 4579974Ssam } 45810023Ssam return (0); 4599974Ssam } 4609974Ssam 46110023Ssam /*ARGSUSED*/ 46210023Ssam upioctl(io, cmd, arg) 46310023Ssam struct iob *io; 46410023Ssam int cmd; 46510023Ssam caddr_t arg; 46610023Ssam { 46711365Ssam int unit = io->i_unit, flag; 46811365Ssam struct st *st = &upst[up_type[unit]], *tmp; 46910352Shelge 47010352Shelge switch(cmd) { 47110352Shelge 47211365Ssam case SAIODEBUG: 47311365Ssam flag = (int)arg; 47411365Ssam if (flag > 0) 47511365Ssam updebug[unit] |= flag; 47611365Ssam else 47711365Ssam updebug[unit] &= ~flag; 47811365Ssam return (0); 47911365Ssam 48010352Shelge case SAIODEVDATA: 48110352Shelge tmp = (struct st *)arg; 48210352Shelge *tmp = *st; 48311085Ssam return (0); 48410352Shelge } 48511085Ssam return (ECMD); 48610023Ssam } 487