1*11317Ssam /* up.c 4.9 83/02/27 */ 2*11317Ssam #define UPECCDEBUG 39974Ssam 410023Ssam /* 510023Ssam * UNIBUS peripheral standalone driver 610023Ssam * with ECC correction and bad block forwarding. 710023Ssam * Also supports header operation and write 810023Ssam * check for data and/or header. 910023Ssam */ 1010352Shelge 119974Ssam #include "../h/param.h" 129974Ssam #include "../h/inode.h" 139974Ssam #include "../h/fs.h" 149974Ssam #include "../h/dkbad.h" 159974Ssam #include "../h/vmmac.h" 169974Ssam 179974Ssam #include "../vax/pte.h" 189974Ssam #include "../vaxuba/upreg.h" 199974Ssam #include "../vaxuba/ubareg.h" 209974Ssam 2110023Ssam #include "saio.h" 229974Ssam #include "savax.h" 239974Ssam 2410352Shelge #define MAXBADDESC 126 /* max number of bad sectors recorded */ 2510352Shelge #define SECTSIZ 512 /* sector size in bytes */ 2610352Shelge #define HDRSIZ 4 /* number of bytes in sector header */ 2711118Ssam #define MAXECC 5 /* max # bad bits allowed on ecc w/ F_ECCLM */ 2810352Shelge 2910023Ssam u_short ubastd[] = { 0776700 }; 309974Ssam 319974Ssam char up_gottype[MAXNUBA*8] = { 0 }; 329974Ssam char up_type[MAXNUBA*8] = { 0 }; 3311143Ssam extern struct st upst[]; 3410023Ssam 359974Ssam u_char up_offset[16] = { 369974Ssam UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400, 379974Ssam UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 389974Ssam UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200, 399974Ssam 0, 0, 0, 0 409974Ssam }; 419974Ssam 4210023Ssam struct dkbad upbad[MAXNUBA*8]; /* bad sector table */ 4310352Shelge int sectsiz; /* real sector size */ 4410023Ssam 459974Ssam upopen(io) 469974Ssam register struct iob *io; 479974Ssam { 4810352Shelge register unit = io->i_unit; 4910023Ssam register struct updevice *upaddr; 5011118Ssam register struct st *st; 519974Ssam 5211143Ssam if (io->i_boff < 0 || io->i_boff > 7) 5310023Ssam _stop("up bad unit"); 5410352Shelge upaddr = (struct updevice *)ubamem(unit, ubastd[0]); 5511085Ssam while ((upaddr->upcs1 & UP_DVA) == 0) 569974Ssam ; 5710352Shelge if (up_gottype[unit] == 0) { 5810023Ssam register int i; 5910023Ssam struct iob tio; 6010023Ssam 6111118Ssam up_type[unit] = upmaptype(unit, upaddr); 6211118Ssam if (up_type[unit] < 0) 6310023Ssam _stop("unknown drive type"); 6411118Ssam st = &upst[up_type[unit]]; 6511143Ssam if (st->off[io->i_boff] == -1) 6611143Ssam _stop("up bad unit"); 6710023Ssam /* 6810023Ssam * Read in the bad sector table: 6910023Ssam * copy the contents of the io structure 7010023Ssam * to tio for use during the bb pointer 7110023Ssam * read operation. 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 { 99*11317Ssam 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]]; 106*11317Ssam int doprintf = 0; 1079974Ssam 10810352Shelge sectsiz = SECTSIZ; 10911085Ssam if (io->i_flgs & (F_HDR|F_HCHECK)) 11010352Shelge sectsiz += HDRSIZ; 1119974Ssam upaddr->upcs2 = unit; 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: 125*11317Ssam #define rounddown(x, y) (((x) / (y)) * (y)) 126*11317Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 127*11317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 128*11317Ssam upaddr->upba = info + o; 129*11317Ssam bn = io->i_bn + o / sectsiz; 130*11317Ssam if (doprintf) 131*11317Ssam printf("upwc %d o %d i_bn %d bn %d\n", upaddr->upwc, 132*11317Ssam o, io->i_bn, bn); 13310023Ssam while((upaddr->upds & UPDS_DRY) == 0) 13410023Ssam ; 13510352Shelge if (upstart(io, bn) != 0) { 13610352Shelge ubafree(io, info); 1379974Ssam return (-1); 13810352Shelge } 1399974Ssam do { 1409974Ssam DELAY(25); 1419974Ssam } while ((upaddr->upcs1 & UP_RDY) == 0); 14211085Ssam /* 14311085Ssam * If transfer has completed, free UNIBUS 14411085Ssam * resources and return transfer size. 14511085Ssam */ 14611085Ssam if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) { 14710352Shelge ubafree(io, info); 14811085Ssam return (io->i_cc); 14910352Shelge } 1509974Ssam #ifdef LOGALLERRS 1519974Ssam printf("uper: (c,t,s)=(%d,%d,%d) cs2=%b er1=%b er2=%b wc=%x\n", 15211085Ssam upaddr->updc, upaddr->upda>>8, (upaddr->upda&0x1f-1), 15311085Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 15411085Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS,-upaddr->upwc); 1559974Ssam #endif 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. 170*11317Ssam * But first, if the error is a header CRC, 171*11317Ssam * check if a replacement sector exists in 172*11317Ssam * the bad sector table. 1739974Ssam */ 174*11317Ssam if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 175*11317Ssam upecc(io, BSE) == 0) 176*11317Ssam 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: 273*11317Ssam if (upaddr->upwc != 0) { 274*11317Ssam doprintf++; 27510638Shelge goto restart; 276*11317Ssam } 2779974Ssam /* 2789974Ssam * 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 { 2959974Ssam register struct updevice *up = 2969974Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 29710352Shelge register struct st *st; 2989974Ssam register int i; 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; 3109974Ssam #ifdef UPECCDEBUG 31111143Ssam printf("npf %d mask 0x%x pos %d wc 0x%x\n", 312*11317Ssam npf, up->upec2, up->upec1, -twc); 3139974Ssam #endif 314*11317Ssam bn = io->i_bn + npf; 3159974Ssam st = &upst[up_type[io->i_unit]]; 31610410Shelge cn = bn/st->nspc; 31710410Shelge sn = bn%st->nspc; 31810410Shelge tn = sn/st->nsect; 31910410Shelge sn = sn%st->nsect; 32011143Ssam 3219974Ssam /* 32211143Ssam * ECC correction. 3239974Ssam */ 3249974Ssam if (flag == ECC) { 325*11317Ssam int bit, o, ecccnt; 32611085Ssam 32710352Shelge ecccnt = 0; 3289974Ssam mask = up->upec2; 32910638Shelge printf("up%d: soft ecc sn%d\n", io->i_unit, bn); 3309974Ssam /* 331*11317Ssam * Compute the byte and bit position of 332*11317Ssam * the error. o is the byte offset in 333*11317Ssam * the transfer at which the correction 334*11317Ssam * applied. 3359974Ssam */ 336*11317Ssam npf--; 3379974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 3389974Ssam bit = i&07; 339*11317Ssam o = (i&~07) >> 3; 3409974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3419974Ssam /* 34211143Ssam * Correct while possible bits remain of mask. 34311143Ssam * Since mask contains 11 bits, we continue while 34411143Ssam * the bit offset is > -11. Also watch out for 34511143Ssam * end of this block and the end of the transfer. 3469974Ssam */ 347*11317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3489974Ssam /* 34911143Ssam * addr = 350*11317Ssam * (base address of transfer) + 35111143Ssam * (# sectors transferred before the error) * 35211143Ssam * (sector size) + 353*11317Ssam * (byte offset to incorrect data) 3549974Ssam */ 355*11317Ssam addr = io->i_ma + (npf * sectsiz) + o; 3569974Ssam #ifdef UPECCDEBUG 357*11317Ssam printf("addr %x old: %x ", addr, (*addr&0xff)); 3589974Ssam #endif 359*11317Ssam /* 360*11317Ssam * No data transfer occurs with a write check, 361*11317Ssam * so don't correct the resident copy of data. 362*11317Ssam */ 36310352Shelge if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) 36410352Shelge *addr ^= (mask << bit); 3659974Ssam #ifdef UPECCDEBUG 36610352Shelge printf("new: %x\n", (*addr&0xff)); 3679974Ssam #endif 368*11317Ssam o++, bit -= 8; 36911085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 37011085Ssam return (1); 3719974Ssam } 37211085Ssam return (0); 37311085Ssam } 37411143Ssam 37511143Ssam /* 37611143Ssam * Bad sector forwarding. 37711143Ssam */ 37811085Ssam if (flag == BSE) { 3799974Ssam /* 38011143Ssam * If not in bad sector table, 38111143Ssam * indicate a hard error to caller. 3829974Ssam */ 38310352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 38410410Shelge if ((bbn = isbad(&upbad[io->i_unit], cn, tn, sn)) < 0) 38511085Ssam return (1); 3869974Ssam bbn = st->ncyl * st->nspc -st->nsect - 1 - bbn; 38710352Shelge twc = up->upwc + sectsiz; 38811085Ssam up->upwc = - (sectsiz / sizeof (short)); 3899974Ssam #ifdef UPECCDEBUG 3909974Ssam printf("revector to block %d\n", bbn); 3919974Ssam #endif 3929974Ssam /* 39311143Ssam * Clear the drive & read the replacement 39411143Ssam * sector. If this is in the middle of a 39511143Ssam * transfer, then set up the controller 39611143Ssam * registers in a normal fashion. 39711143Ssam * The UNIBUS address need not be changed. 39811143Ssam */ 39910023Ssam while (up->upcs1 & UP_RDY == 0) 4009974Ssam ; 40110023Ssam if (upstart(io, bbn) != 0) 40210352Shelge return (1); /* error */ 40310352Shelge io->i_errcnt = 0; /* success */ 4049974Ssam do { 4059974Ssam DELAY(25); 4069974Ssam } while ( up->upcs1 & UP_RDY == 0) ; 4079974Ssam if (up->upds & UPDS_ERR || up->upcs1 & UP_TRE) { 40810352Shelge up->upwc = twc -sectsiz; 40910352Shelge return (1); 4109974Ssam } 4119974Ssam } 41210638Shelge if (twc) 4139974Ssam up->upwc = twc; 41410352Shelge return (0); 4159974Ssam } 4169974Ssam 4179974Ssam upstart(io, bn) 41810023Ssam register struct iob *io; 41910023Ssam daddr_t bn; 4209974Ssam { 4219974Ssam register struct updevice *upaddr = 42210023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 42310352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4249974Ssam int sn, tn; 4259974Ssam 4269974Ssam sn = bn%st->nspc; 4279974Ssam tn = sn/st->nsect; 4289974Ssam sn %= st->nsect; 4299974Ssam upaddr->updc = bn/st->nspc; 4309974Ssam upaddr->upda = (tn << 8) + sn; 43110352Shelge switch (io->i_flgs & F_TYPEMASK) { 43210023Ssam 43310023Ssam case F_RDDATA: 43410023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4359974Ssam break; 43610023Ssam 43710023Ssam case F_WRDATA: 43810023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4399974Ssam break; 44010023Ssam 44110023Ssam case F_HDR|F_RDDATA: 44210023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 44310023Ssam break; 44410023Ssam 44510023Ssam case F_HDR|F_WRDATA: 44610023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 44710023Ssam break; 44810023Ssam 44910023Ssam case F_CHECK|F_WRDATA: 45010023Ssam case F_CHECK|F_RDDATA: 4519974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4529974Ssam break; 45310023Ssam 45410023Ssam case F_HCHECK|F_WRDATA: 45510023Ssam case F_HCHECK|F_RDDATA: 4569974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4579974Ssam break; 45810023Ssam 4599974Ssam default: 46010023Ssam io->i_error = ECMD; 46110023Ssam io->i_flgs &= ~F_TYPEMASK; 46210023Ssam return (1); 4639974Ssam } 46410023Ssam return (0); 4659974Ssam } 4669974Ssam 46710023Ssam /*ARGSUSED*/ 46810023Ssam upioctl(io, cmd, arg) 46910023Ssam struct iob *io; 47010023Ssam int cmd; 47110023Ssam caddr_t arg; 47210023Ssam { 47310352Shelge struct st *st = &upst[up_type[io->i_unit]], *tmp; 47410352Shelge 47510352Shelge switch(cmd) { 47610352Shelge 47710352Shelge case SAIODEVDATA: 47810352Shelge tmp = (struct st *)arg; 47910352Shelge *tmp = *st; 48011085Ssam return (0); 48110352Shelge } 48211085Ssam return (ECMD); 48310023Ssam } 484