1*11365Ssam /* up.c 4.10 83/03/01 */ 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 */ 25*11365Ssam 2611118Ssam #define MAXECC 5 /* max # bad bits allowed on ecc w/ F_ECCLM */ 2710352Shelge 2810023Ssam u_short ubastd[] = { 0776700 }; 299974Ssam 30*11365Ssam char up_gottype[MAXNUBA*8]; 31*11365Ssam char up_type[MAXNUBA*8]; 3211143Ssam extern struct st upst[]; 3310023Ssam 34*11365Ssam struct dkbad upbad[MAXNUBA*8]; /* bad sector table */ 35*11365Ssam int sectsiz; /* real sector size */ 36*11365Ssam int updebug[MAXNUBA*8]; 37*11365Ssam #define UPF_BSEDEBUG 01 /* debugging bad sector forwarding */ 38*11365Ssam #define UPF_ECCDEBUG 02 /* debugging ecc correction */ 39*11365Ssam 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 /* 70*11365Ssam * 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; 110*11365Ssam 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 #define rounddown(x, y) (((x) / (y)) * (y)) 12511317Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 12611317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 12711317Ssam upaddr->upba = info + o; 12811317Ssam bn = io->i_bn + o / sectsiz; 129*11365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 130*11365Ssam printf("upwc=%d o=%d i_bn=%d bn=%d\n", 131*11365Ssam 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 */ 14511085Ssam if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) { 14610352Shelge ubafree(io, info); 14711085Ssam return (io->i_cc); 14810352Shelge } 149*11365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 150*11365Ssam printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 151*11365Ssam upaddr->updc, upaddr->upda>>8, (upaddr->upda&0x1f-1)); 152*11365Ssam printf("cs2=%b er1=%b er2=%b wc=%x\n", 153*11365Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 154*11365Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS,-upaddr->upwc); 155*11365Ssam } 1569974Ssam waitdry = 0; 15710352Shelge while ((upaddr->upds & UPDS_DRY) == 0 && ++waitdry < sectsiz) 15810023Ssam DELAY(5); 15911085Ssam if (upaddr->uper1&UPER1_WLE) { 16011085Ssam /* 16111085Ssam * Give up on write locked devices immediately. 16211085Ssam */ 16311085Ssam printf("up%d: write locked\n", unit); 16411085Ssam return (-1); 16511085Ssam } 1669974Ssam if (++io->i_errcnt > 27) { 1679974Ssam /* 1689974Ssam * After 28 retries (16 without offset, and 1699974Ssam * 12 with offset positioning) give up. 17011317Ssam * But first, if the error is a header CRC, 17111317Ssam * check if a replacement sector exists in 17211317Ssam * the bad sector table. 1739974Ssam */ 17411317Ssam if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 17511317Ssam upecc(io, BSE) == 0) 17611317Ssam goto success; 17710023Ssam io->i_error = EHER; 17810023Ssam if (upaddr->upcs2 & UPCS2_WCE) 17910023Ssam io->i_error = EWCK; 18010352Shelge hard: 18111085Ssam bn = io->i_bn + 18211085Ssam (io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz; 1839974Ssam cn = bn/st->nspc; 1849974Ssam sn = bn%st->nspc; 1859974Ssam tn = sn/st->nsect; 1869974Ssam sn = sn%st->nsect; 18711085Ssam printf( 18811085Ssam "up error: (cyl,trk,sec)=(%d,%d,%d) cs2=%b er1=%b er2=%b\n", 18911085Ssam cn, tn, sn, 19011085Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 19111085Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS); 1929974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 19310352Shelge io->i_errblk = bn; 19411085Ssam return (io->i_cc + upaddr->upwc * sizeof(short)); 19511085Ssam } 19611085Ssam if (upaddr->uper2 & UPER2_BSE) { 19711143Ssam if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 1989974Ssam goto success; 19911085Ssam io->i_error = EBSE; 20011085Ssam goto hard; 2019974Ssam } 20211085Ssam /* 20311085Ssam * Retriable error. 20411085Ssam * If a soft ecc, correct it 20511085Ssam * Otherwise fall through and retry the transfer 20611085Ssam */ 20711085Ssam if (upaddr->uper1 & UPER1_DCK) { 2089974Ssam /* 20911085Ssam * If a write check command is active, all 21011085Ssam * ecc errors give UPER1_ECH. 2119974Ssam */ 21211085Ssam if ((upaddr->uper1 & UPER1_ECH) == 0 || 21311085Ssam (upaddr->upcs2 & UPCS2_WCE)) { 21411085Ssam if (upecc(io, ECC) == 0) 21511085Ssam goto success; 21611085Ssam io->i_error = EECC; 21711085Ssam goto hard; 21811085Ssam } 21911085Ssam } 2209974Ssam /* 2219974Ssam * Clear drive error and, every eight attempts, 2229974Ssam * (starting with the fourth) 2239974Ssam * recalibrate to clear the slate. 2249974Ssam */ 2259974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 22610638Shelge if ((io->i_errcnt&07) == 4 ) { 2279974Ssam upaddr->upcs1 = UP_RECAL|UP_GO; 22810638Shelge recal = 1; 22910638Shelge goto restart; 2309974Ssam } 2319974Ssam /* 2329974Ssam * Advance recalibration finite state machine 2339974Ssam * if recalibrate in progress, through 2349974Ssam * RECAL 2359974Ssam * SEEK 2369974Ssam * OFFSET (optional) 2379974Ssam * RETRY 2389974Ssam */ 2399974Ssam switch (recal) { 2409974Ssam 2419974Ssam case 1: 2429974Ssam upaddr->updc = cn; 2439974Ssam upaddr->upcs1 = UP_SEEK|UP_GO; 24411085Ssam recal = 2; 24510638Shelge goto restart; 24610023Ssam 2479974Ssam case 2: 2489974Ssam if (io->i_errcnt < 16 || (func & READ) == 0) 2499974Ssam goto donerecal; 2509974Ssam upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 2519974Ssam upaddr->upcs1 = UP_OFFSET|UP_GO; 25211085Ssam recal = 3; 25310638Shelge goto restart; 25410023Ssam 2559974Ssam donerecal: 2569974Ssam case 3: 2579974Ssam recal = 0; 2589974Ssam break; 2599974Ssam } 2609974Ssam /* 26110638Shelge * If we were offset positioning, 26210638Shelge * return to centerline. 2639974Ssam */ 26410638Shelge if (io->i_errcnt >= 16) { 26510638Shelge upaddr->upof = UPOF_FMT22; 26610638Shelge upaddr->upcs1 = UP_RTC|UP_GO; 26710638Shelge while ((upaddr->upds&UPDS_DRY) == 0) 26810638Shelge DELAY(25); 2699974Ssam } 27010638Shelge goto restart; 27111085Ssam 2729974Ssam success: 27311317Ssam if (upaddr->upwc != 0) { 27411317Ssam doprintf++; 27510638Shelge goto restart; 27611317Ssam } 2779974Ssam /* 278*11365Ssam * Release UNIBUS 2799974Ssam */ 2809974Ssam ubafree(io, info); 2819974Ssam return (io->i_cc); 2829974Ssam } 2839974Ssam 2849974Ssam /* 28511143Ssam * Correct an ECC error, and restart the 28611143Ssam * i/o to complete the transfer (if necessary). 28711143Ssam * This is quite complicated because the transfer 28811143Ssam * may be going to an odd memory address base and/or 2899974Ssam * across a page boundary. 2909974Ssam */ 29110023Ssam upecc(io, flag) 2929974Ssam register struct iob *io; 2939974Ssam int flag; 2949974Ssam { 295*11365Ssam register i, unit = io->i_unit; 2969974Ssam register struct updevice *up = 297*11365Ssam (struct updevice *)ubamem(unit, ubastd[0]); 29810352Shelge register struct st *st; 2999974Ssam caddr_t addr; 30010410Shelge int bn, twc, npf, mask, cn, tn, sn; 30110352Shelge daddr_t bbn; 3029974Ssam 3039974Ssam /* 30411143Ssam * Npf is the number of sectors transferred 30511143Ssam * before the sector containing the ECC error; 30611143Ssam * bn is the current block number. 3079974Ssam */ 30810352Shelge twc = up->upwc; 30911143Ssam npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 310*11365Ssam if (updebug[unit] & UPF_ECCDEBUG) 311*11365Ssam printf("npf=%d mask=0x%x pos=%d wc=0x%x\n", 312*11365Ssam npf, up->upec2, up->upec1, -twc); 31311317Ssam bn = io->i_bn + npf; 314*11365Ssam st = &upst[up_type[unit]]; 31510410Shelge cn = bn/st->nspc; 31610410Shelge sn = bn%st->nspc; 31710410Shelge tn = sn/st->nsect; 31810410Shelge sn = sn%st->nsect; 31911143Ssam 3209974Ssam /* 32111143Ssam * ECC correction. 3229974Ssam */ 3239974Ssam if (flag == ECC) { 32411317Ssam int bit, o, ecccnt; 32511085Ssam 32610352Shelge ecccnt = 0; 3279974Ssam mask = up->upec2; 328*11365Ssam printf("up%d: soft ecc sn%d\n", unit, bn); 3299974Ssam /* 33011317Ssam * Compute the byte and bit position of 33111317Ssam * the error. o is the byte offset in 33211317Ssam * the transfer at which the correction 33311317Ssam * applied. 3349974Ssam */ 33511317Ssam npf--; 3369974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 3379974Ssam bit = i&07; 33811317Ssam o = (i&~07) >> 3; 3399974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3409974Ssam /* 34111143Ssam * Correct while possible bits remain of mask. 34211143Ssam * Since mask contains 11 bits, we continue while 34311143Ssam * the bit offset is > -11. Also watch out for 34411143Ssam * end of this block and the end of the transfer. 3459974Ssam */ 34611317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3479974Ssam /* 34811143Ssam * addr = 34911317Ssam * (base address of transfer) + 35011143Ssam * (# sectors transferred before the error) * 35111143Ssam * (sector size) + 35211317Ssam * (byte offset to incorrect data) 3539974Ssam */ 35411317Ssam addr = io->i_ma + (npf * sectsiz) + o; 35511317Ssam /* 35611317Ssam * No data transfer occurs with a write check, 35711317Ssam * so don't correct the resident copy of data. 35811317Ssam */ 359*11365Ssam if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 360*11365Ssam if (updebug[unit] & UPF_ECCDEBUG) 361*11365Ssam printf("addr=0x%x old=0x%x ", addr, 362*11365Ssam (*addr&0xff)); 36310352Shelge *addr ^= (mask << bit); 364*11365Ssam if (updebug[unit] & UPF_ECCDEBUG) 365*11365Ssam printf("new=0x%x\n", (*addr&0xff)); 366*11365Ssam } 36711317Ssam o++, bit -= 8; 36811085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 36911085Ssam return (1); 3709974Ssam } 37111085Ssam return (0); 37211085Ssam } 37311143Ssam 37411143Ssam /* 37511143Ssam * Bad sector forwarding. 37611143Ssam */ 37711085Ssam if (flag == BSE) { 3789974Ssam /* 37911143Ssam * If not in bad sector table, 38011143Ssam * indicate a hard error to caller. 3819974Ssam */ 38210352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 383*11365Ssam if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 38411085Ssam return (1); 3859974Ssam bbn = st->ncyl * st->nspc -st->nsect - 1 - bbn; 38610352Shelge twc = up->upwc + sectsiz; 38711085Ssam up->upwc = - (sectsiz / sizeof (short)); 388*11365Ssam if (updebug[unit] & UPF_BSEDEBUG) 389*11365Ssam printf("revector sn %d to %d\n", sn, bbn); 3909974Ssam /* 39111143Ssam * Clear the drive & read the replacement 39211143Ssam * sector. If this is in the middle of a 39311143Ssam * transfer, then set up the controller 39411143Ssam * registers in a normal fashion. 39511143Ssam * The UNIBUS address need not be changed. 39611143Ssam */ 39710023Ssam while (up->upcs1 & UP_RDY == 0) 3989974Ssam ; 39910023Ssam if (upstart(io, bbn) != 0) 40010352Shelge return (1); /* error */ 40110352Shelge io->i_errcnt = 0; /* success */ 4029974Ssam do { 4039974Ssam DELAY(25); 4049974Ssam } while ( up->upcs1 & UP_RDY == 0) ; 4059974Ssam if (up->upds & UPDS_ERR || up->upcs1 & UP_TRE) { 40610352Shelge up->upwc = twc -sectsiz; 40710352Shelge return (1); 4089974Ssam } 4099974Ssam } 41010638Shelge if (twc) 4119974Ssam up->upwc = twc; 41210352Shelge return (0); 4139974Ssam } 4149974Ssam 4159974Ssam upstart(io, bn) 41610023Ssam register struct iob *io; 41710023Ssam daddr_t bn; 4189974Ssam { 4199974Ssam register struct updevice *upaddr = 42010023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 42110352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4229974Ssam int sn, tn; 4239974Ssam 4249974Ssam sn = bn%st->nspc; 4259974Ssam tn = sn/st->nsect; 4269974Ssam sn %= st->nsect; 4279974Ssam upaddr->updc = bn/st->nspc; 4289974Ssam upaddr->upda = (tn << 8) + sn; 42910352Shelge switch (io->i_flgs & F_TYPEMASK) { 43010023Ssam 43110023Ssam case F_RDDATA: 43210023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4339974Ssam break; 43410023Ssam 43510023Ssam case F_WRDATA: 43610023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4379974Ssam break; 43810023Ssam 43910023Ssam case F_HDR|F_RDDATA: 44010023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 44110023Ssam break; 44210023Ssam 44310023Ssam case F_HDR|F_WRDATA: 44410023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 44510023Ssam break; 44610023Ssam 44710023Ssam case F_CHECK|F_WRDATA: 44810023Ssam case F_CHECK|F_RDDATA: 4499974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4509974Ssam break; 45110023Ssam 45210023Ssam case F_HCHECK|F_WRDATA: 45310023Ssam case F_HCHECK|F_RDDATA: 4549974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4559974Ssam break; 45610023Ssam 4579974Ssam default: 45810023Ssam io->i_error = ECMD; 45910023Ssam io->i_flgs &= ~F_TYPEMASK; 46010023Ssam return (1); 4619974Ssam } 46210023Ssam return (0); 4639974Ssam } 4649974Ssam 46510023Ssam /*ARGSUSED*/ 46610023Ssam upioctl(io, cmd, arg) 46710023Ssam struct iob *io; 46810023Ssam int cmd; 46910023Ssam caddr_t arg; 47010023Ssam { 471*11365Ssam int unit = io->i_unit, flag; 472*11365Ssam struct st *st = &upst[up_type[unit]], *tmp; 47310352Shelge 47410352Shelge switch(cmd) { 47510352Shelge 476*11365Ssam case SAIODEBUG: 477*11365Ssam flag = (int)arg; 478*11365Ssam if (flag > 0) 479*11365Ssam updebug[unit] |= flag; 480*11365Ssam else 481*11365Ssam updebug[unit] &= ~flag; 482*11365Ssam return (0); 483*11365Ssam 48410352Shelge case SAIODEVDATA: 48510352Shelge tmp = (struct st *)arg; 48610352Shelge *tmp = *st; 48711085Ssam return (0); 48810352Shelge } 48911085Ssam return (ECMD); 49010023Ssam } 491