1*23248Smckusick /* 2*23248Smckusick * Copyright (c) 1982 Regents of the University of California. 3*23248Smckusick * All rights reserved. The Berkeley software License Agreement 4*23248Smckusick * specifies the terms and conditions for redistribution. 5*23248Smckusick * 6*23248Smckusick * @(#)up.c 6.4 (Berkeley) 06/08/85 7*23248Smckusick */ 89974Ssam 910023Ssam /* 1010023Ssam * UNIBUS peripheral standalone driver 1110023Ssam * with ECC correction and bad block forwarding. 1210023Ssam * Also supports header operation and write 1310023Ssam * check for data and/or header. 1410023Ssam */ 159974Ssam #include "../h/param.h" 169974Ssam #include "../h/inode.h" 179974Ssam #include "../h/fs.h" 189974Ssam #include "../h/dkbad.h" 199974Ssam #include "../h/vmmac.h" 209974Ssam 219974Ssam #include "../vax/pte.h" 229974Ssam #include "../vaxuba/upreg.h" 239974Ssam #include "../vaxuba/ubareg.h" 249974Ssam 2510023Ssam #include "saio.h" 269974Ssam #include "savax.h" 279974Ssam 2810352Shelge #define MAXBADDESC 126 /* max number of bad sectors recorded */ 2910352Shelge #define SECTSIZ 512 /* sector size in bytes */ 3010352Shelge #define HDRSIZ 4 /* number of bytes in sector header */ 3111365Ssam 3211118Ssam #define MAXECC 5 /* max # bad bits allowed on ecc w/ F_ECCLM */ 3310352Shelge 3410023Ssam u_short ubastd[] = { 0776700 }; 359974Ssam 3611365Ssam char up_gottype[MAXNUBA*8]; 3711365Ssam char up_type[MAXNUBA*8]; 3811143Ssam extern struct st upst[]; 3910023Ssam 4011365Ssam struct dkbad upbad[MAXNUBA*8]; /* bad sector table */ 4111365Ssam int sectsiz; /* real sector size */ 4211365Ssam int updebug[MAXNUBA*8]; 4311365Ssam #define UPF_BSEDEBUG 01 /* debugging bad sector forwarding */ 4411365Ssam #define UPF_ECCDEBUG 02 /* debugging ecc correction */ 4511365Ssam 469974Ssam u_char up_offset[16] = { 479974Ssam UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400, 489974Ssam UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 499974Ssam UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200, 509974Ssam 0, 0, 0, 0 519974Ssam }; 529974Ssam 539974Ssam upopen(io) 549974Ssam register struct iob *io; 559974Ssam { 5610352Shelge register unit = io->i_unit; 5710023Ssam register struct updevice *upaddr; 5811118Ssam register struct st *st; 599974Ssam 6011143Ssam if (io->i_boff < 0 || io->i_boff > 7) 6110023Ssam _stop("up bad unit"); 6210352Shelge upaddr = (struct updevice *)ubamem(unit, ubastd[0]); 6315067Skarels upaddr->upcs2 = unit % 8; 6411085Ssam while ((upaddr->upcs1 & UP_DVA) == 0) 659974Ssam ; 6610352Shelge if (up_gottype[unit] == 0) { 6710023Ssam register int i; 6810023Ssam struct iob tio; 6910023Ssam 7011118Ssam up_type[unit] = upmaptype(unit, upaddr); 7111118Ssam if (up_type[unit] < 0) 7210023Ssam _stop("unknown drive type"); 7311118Ssam st = &upst[up_type[unit]]; 7411143Ssam if (st->off[io->i_boff] == -1) 7511143Ssam _stop("up bad unit"); 7610023Ssam /* 7711365Ssam * Read in the bad sector table. 7810023Ssam */ 7910023Ssam tio = *io; 8010023Ssam tio.i_bn = st->nspc * st->ncyl - st->nsect; 819974Ssam tio.i_ma = (char *)&upbad[tio.i_unit]; 8210638Shelge tio.i_cc = sizeof (struct dkbad); 8310023Ssam tio.i_flgs |= F_RDDATA; 8410023Ssam for (i = 0; i < 5; i++) { 8510638Shelge if (upstrategy(&tio, READ) == sizeof (struct dkbad)) 8610023Ssam break; 879974Ssam tio.i_bn += 2; 889974Ssam } 899974Ssam if (i == 5) { 9010023Ssam printf("Unable to read bad sector table\n"); 9110352Shelge for (i = 0; i < MAXBADDESC; i++) { 9210352Shelge upbad[unit].bt_bad[i].bt_cyl = -1; 9310352Shelge upbad[unit].bt_bad[i].bt_trksec = -1; 949974Ssam } 959974Ssam } 9610352Shelge up_gottype[unit] = 1; 979974Ssam } 9815948Skarels st = &upst[up_type[unit]]; 999974Ssam io->i_boff = st->off[io->i_boff] * st->nspc; 10010023Ssam io->i_flgs &= ~F_TYPEMASK; 1019974Ssam } 1029974Ssam 1039974Ssam upstrategy(io, func) 1049974Ssam register struct iob *io; 1059974Ssam { 10611317Ssam int cn, tn, sn, o; 10710352Shelge register unit = io->i_unit; 1089974Ssam daddr_t bn; 1099974Ssam int recal, info, waitdry; 1109974Ssam register struct updevice *upaddr = 11110352Shelge (struct updevice *)ubamem(unit, ubastd[0]); 11210352Shelge register struct st *st = &upst[up_type[unit]]; 11311317Ssam int doprintf = 0; 1149974Ssam 11510352Shelge sectsiz = SECTSIZ; 11611085Ssam if (io->i_flgs & (F_HDR|F_HCHECK)) 11710352Shelge sectsiz += HDRSIZ; 11811365Ssam upaddr->upcs2 = unit % 8; 1199974Ssam if ((upaddr->upds & UPDS_VV) == 0) { 1209974Ssam upaddr->upcs1 = UP_DCLR|UP_GO; 1219974Ssam upaddr->upcs1 = UP_PRESET|UP_GO; 1229974Ssam upaddr->upof = UPOF_FMT22; 1239974Ssam } 12411085Ssam if ((upaddr->upds & UPDS_DREADY) == 0) 1259974Ssam _stop("up not ready"); 1269974Ssam info = ubasetup(io, 1); 1279974Ssam upaddr->upwc = -io->i_cc / sizeof (short); 12811085Ssam recal = 0; 12911085Ssam io->i_errcnt = 0; 13011085Ssam 13110638Shelge restart: 13211317Ssam o = io->i_cc + (upaddr->upwc * sizeof (short)); 13311317Ssam upaddr->upba = info + o; 13411317Ssam bn = io->i_bn + o / sectsiz; 13511384Ssam if (doprintf && updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 13611386Ssam printf("wc=%d o=%d i_bn=%d bn=%d\n", 13711365Ssam upaddr->upwc, o, io->i_bn, bn); 13810023Ssam while((upaddr->upds & UPDS_DRY) == 0) 13910023Ssam ; 14010352Shelge if (upstart(io, bn) != 0) { 14110352Shelge ubafree(io, info); 1429974Ssam return (-1); 14310352Shelge } 1449974Ssam do { 1459974Ssam DELAY(25); 1469974Ssam } while ((upaddr->upcs1 & UP_RDY) == 0); 14711085Ssam /* 14811085Ssam * If transfer has completed, free UNIBUS 14911085Ssam * resources and return transfer size. 15011085Ssam */ 15115067Skarels if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) 15215067Skarels goto done; 15311365Ssam if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 15411365Ssam printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 15515067Skarels upaddr->updc, upaddr->upda>>8, upaddr->upda&0xff); 15611386Ssam printf("cs2=%b er1=%b er2=%b wc=%d\n", 15711365Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 15811386Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc); 15911365Ssam } 1609974Ssam waitdry = 0; 16111386Ssam while ((upaddr->upds&UPDS_DRY) == 0 && ++waitdry < sectsiz) 16210023Ssam DELAY(5); 16311085Ssam if (upaddr->uper1&UPER1_WLE) { 16411085Ssam /* 16511085Ssam * Give up on write locked devices immediately. 16611085Ssam */ 16711085Ssam printf("up%d: write locked\n", unit); 16811085Ssam return (-1); 16911085Ssam } 1709974Ssam if (++io->i_errcnt > 27) { 1719974Ssam /* 1729974Ssam * After 28 retries (16 without offset, and 1739974Ssam * 12 with offset positioning) give up. 17411317Ssam * But first, if the error is a header CRC, 17511317Ssam * check if a replacement sector exists in 17611317Ssam * the bad sector table. 1779974Ssam */ 17811317Ssam if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 17911317Ssam upecc(io, BSE) == 0) 18011317Ssam goto success; 18110023Ssam io->i_error = EHER; 18210023Ssam if (upaddr->upcs2 & UPCS2_WCE) 18310023Ssam io->i_error = EWCK; 18410352Shelge hard: 18511085Ssam bn = io->i_bn + 18611085Ssam (io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz; 1879974Ssam cn = bn/st->nspc; 1889974Ssam sn = bn%st->nspc; 1899974Ssam tn = sn/st->nsect; 1909974Ssam sn = sn%st->nsect; 19111085Ssam printf( 19211085Ssam "up error: (cyl,trk,sec)=(%d,%d,%d) cs2=%b er1=%b er2=%b\n", 19311085Ssam cn, tn, sn, 19411085Ssam upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 19511085Ssam UPER1_BITS, upaddr->uper2, UPER2_BITS); 1969974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 19710352Shelge io->i_errblk = bn; 19815067Skarels if (io->i_errcnt >= 16) { 19915067Skarels upaddr->upof = UPOF_FMT22; 20015067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 20115067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 20215067Skarels DELAY(25); 20315067Skarels } 20411085Ssam return (io->i_cc + upaddr->upwc * sizeof(short)); 20511085Ssam } 20611085Ssam if (upaddr->uper2 & UPER2_BSE) { 20711143Ssam if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 2089974Ssam goto success; 20911085Ssam io->i_error = EBSE; 21011085Ssam goto hard; 2119974Ssam } 21211085Ssam /* 21311386Ssam * ECC error. If a soft error, correct it; 21411386Ssam * otherwise fall through and retry the transfer. 21511085Ssam */ 21615067Skarels if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH|UPER1_HCRC)) == UPER1_DCK) { 21711386Ssam if (upecc(io, ECC) == 0) 21815067Skarels #ifdef F_SEVRE 21915067Skarels if (io->i_flgs & F_SEVRE) 22015067Skarels return (-1); 22115067Skarels else 22215067Skarels #endif 22311386Ssam goto success; 22411386Ssam io->i_error = EECC; 22511386Ssam goto hard; 22611085Ssam } 22715067Skarels #ifdef F_SEVRE 22815067Skarels if (io->i_flgs & F_SEVRE) 22915067Skarels goto hard; 23015067Skarels #endif 2319974Ssam /* 2329974Ssam * Clear drive error and, every eight attempts, 2339974Ssam * (starting with the fourth) 2349974Ssam * recalibrate to clear the slate. 2359974Ssam */ 2369974Ssam upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 23710638Shelge if ((io->i_errcnt&07) == 4 ) { 2389974Ssam upaddr->upcs1 = UP_RECAL|UP_GO; 23915067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 24015067Skarels DELAY(25); 2419974Ssam upaddr->updc = cn; 2429974Ssam upaddr->upcs1 = UP_SEEK|UP_GO; 24315067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 24415067Skarels DELAY(25); 24515067Skarels } 24615067Skarels if (io->i_errcnt >= 16 && (func & READ)) { 2479974Ssam upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 2489974Ssam upaddr->upcs1 = UP_OFFSET|UP_GO; 24910638Shelge while ((upaddr->upds&UPDS_DRY) == 0) 25010638Shelge DELAY(25); 2519974Ssam } 25210638Shelge goto restart; 25311085Ssam 2549974Ssam success: 25511386Ssam #define rounddown(x, y) (((x) / (y)) * (y)) 25611386Ssam upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 25711386Ssam if (upaddr->upwc) { 25811317Ssam doprintf++; 25910638Shelge goto restart; 26011317Ssam } 26115067Skarels done: 2629974Ssam /* 26311365Ssam * Release UNIBUS 2649974Ssam */ 2659974Ssam ubafree(io, info); 26615067Skarels /* 26715067Skarels * If we were offset positioning, 26815067Skarels * return to centerline. 26915067Skarels */ 27015067Skarels if (io->i_errcnt >= 16) { 27115067Skarels upaddr->upof = UPOF_FMT22; 27215067Skarels upaddr->upcs1 = UP_RTC|UP_GO; 27315067Skarels while ((upaddr->upds&UPDS_DRY) == 0) 27415067Skarels DELAY(25); 27515067Skarels } 2769974Ssam return (io->i_cc); 2779974Ssam } 2789974Ssam 2799974Ssam /* 28011143Ssam * Correct an ECC error, and restart the 28111143Ssam * i/o to complete the transfer (if necessary). 28211143Ssam * This is quite complicated because the transfer 28311143Ssam * may be going to an odd memory address base and/or 2849974Ssam * across a page boundary. 2859974Ssam */ 28610023Ssam upecc(io, flag) 2879974Ssam register struct iob *io; 2889974Ssam int flag; 2899974Ssam { 29011365Ssam register i, unit = io->i_unit; 2919974Ssam register struct updevice *up = 29211365Ssam (struct updevice *)ubamem(unit, ubastd[0]); 29310352Shelge register struct st *st; 2949974Ssam caddr_t addr; 29510410Shelge int bn, twc, npf, mask, cn, tn, sn; 29610352Shelge daddr_t bbn; 2979974Ssam 2989974Ssam /* 29911143Ssam * Npf is the number of sectors transferred 30011143Ssam * before the sector containing the ECC error; 30111143Ssam * bn is the current block number. 3029974Ssam */ 30310352Shelge twc = up->upwc; 30411143Ssam npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 30515067Skarels if (flag == ECC) 30615067Skarels npf--; 30711365Ssam if (updebug[unit] & UPF_ECCDEBUG) 30811386Ssam printf("npf=%d mask=0x%x ec1=%d wc=%d\n", 30911386Ssam npf, up->upec2, up->upec1, twc); 31011317Ssam bn = io->i_bn + npf; 31111365Ssam st = &upst[up_type[unit]]; 31210410Shelge cn = bn/st->nspc; 31310410Shelge sn = bn%st->nspc; 31410410Shelge tn = sn/st->nsect; 31510410Shelge sn = sn%st->nsect; 31611143Ssam 3179974Ssam /* 31811143Ssam * ECC correction. 3199974Ssam */ 3209974Ssam if (flag == ECC) { 32111317Ssam int bit, o, ecccnt; 32211085Ssam 32310352Shelge ecccnt = 0; 3249974Ssam mask = up->upec2; 32511365Ssam printf("up%d: soft ecc sn%d\n", unit, bn); 3269974Ssam /* 32711317Ssam * Compute the byte and bit position of 32811317Ssam * the error. o is the byte offset in 32911317Ssam * the transfer at which the correction 33011317Ssam * applied. 3319974Ssam */ 3329974Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 33311386Ssam bit = i & 07; 33411386Ssam o = (i & ~07) >> 3; 3359974Ssam up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 3369974Ssam /* 33711143Ssam * Correct while possible bits remain of mask. 33811143Ssam * Since mask contains 11 bits, we continue while 33911143Ssam * the bit offset is > -11. Also watch out for 34011143Ssam * end of this block and the end of the transfer. 3419974Ssam */ 34211317Ssam while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 3439974Ssam /* 34411143Ssam * addr = 34511317Ssam * (base address of transfer) + 34611143Ssam * (# sectors transferred before the error) * 34711143Ssam * (sector size) + 34811317Ssam * (byte offset to incorrect data) 3499974Ssam */ 35011317Ssam addr = io->i_ma + (npf * sectsiz) + o; 35111317Ssam /* 35211317Ssam * No data transfer occurs with a write check, 35311317Ssam * so don't correct the resident copy of data. 35411317Ssam */ 35511365Ssam if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 35611365Ssam if (updebug[unit] & UPF_ECCDEBUG) 35711365Ssam printf("addr=0x%x old=0x%x ", addr, 35811365Ssam (*addr&0xff)); 35910352Shelge *addr ^= (mask << bit); 36011365Ssam if (updebug[unit] & UPF_ECCDEBUG) 36111365Ssam printf("new=0x%x\n", (*addr&0xff)); 36211365Ssam } 36311317Ssam o++, bit -= 8; 36411085Ssam if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 36511085Ssam return (1); 3669974Ssam } 36715067Skarels #ifdef F_SEVRE 36815067Skarels if (io->i_flgs & F_SEVRE) { 36915067Skarels io->i_error = EECC; 37015067Skarels io->i_bn = bn; 37115067Skarels return(1); 37215067Skarels } 37315067Skarels #endif 37411085Ssam return (0); 37511085Ssam } 37611143Ssam 37711143Ssam /* 37811143Ssam * Bad sector forwarding. 37911143Ssam */ 38011085Ssam if (flag == BSE) { 3819974Ssam /* 38211143Ssam * If not in bad sector table, 38311143Ssam * indicate a hard error to caller. 3849974Ssam */ 38510352Shelge up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 38611365Ssam if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 38711085Ssam return (1); 38811386Ssam bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn; 38910352Shelge twc = up->upwc + sectsiz; 39011085Ssam up->upwc = - (sectsiz / sizeof (short)); 39111365Ssam if (updebug[unit] & UPF_BSEDEBUG) 39211365Ssam printf("revector sn %d to %d\n", sn, bbn); 3939974Ssam /* 39411143Ssam * Clear the drive & read the replacement 39511143Ssam * sector. If this is in the middle of a 39611143Ssam * transfer, then set up the controller 39711143Ssam * registers in a normal fashion. 39811143Ssam * The UNIBUS address need not be changed. 39911143Ssam */ 40011386Ssam while ((up->upcs1 & UP_RDY) == 0) 4019974Ssam ; 40211386Ssam if (upstart(io, bbn)) 40310352Shelge return (1); /* error */ 40410352Shelge io->i_errcnt = 0; /* success */ 4059974Ssam do { 4069974Ssam DELAY(25); 40711386Ssam } while ((up->upcs1 & UP_RDY) == 0) ; 40811386Ssam if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) { 40911386Ssam up->upwc = twc - sectsiz; 41010352Shelge return (1); 4119974Ssam } 4129974Ssam } 41310638Shelge if (twc) 4149974Ssam up->upwc = twc; 41510352Shelge return (0); 4169974Ssam } 4179974Ssam 4189974Ssam upstart(io, bn) 41910023Ssam register struct iob *io; 42010023Ssam daddr_t bn; 4219974Ssam { 4229974Ssam register struct updevice *upaddr = 42310023Ssam (struct updevice *)ubamem(io->i_unit, ubastd[0]); 42410352Shelge register struct st *st = &upst[up_type[io->i_unit]]; 4259974Ssam int sn, tn; 4269974Ssam 4279974Ssam sn = bn%st->nspc; 4289974Ssam tn = sn/st->nsect; 4299974Ssam sn %= st->nsect; 4309974Ssam upaddr->updc = bn/st->nspc; 4319974Ssam upaddr->upda = (tn << 8) + sn; 43210352Shelge switch (io->i_flgs & F_TYPEMASK) { 43310023Ssam 43410023Ssam case F_RDDATA: 43510023Ssam upaddr->upcs1 = UP_RCOM|UP_GO; 4369974Ssam break; 43710023Ssam 43810023Ssam case F_WRDATA: 43910023Ssam upaddr->upcs1 = UP_WCOM|UP_GO; 4409974Ssam break; 44110023Ssam 44210023Ssam case F_HDR|F_RDDATA: 44310023Ssam upaddr->upcs1 = UP_RHDR|UP_GO; 44410023Ssam break; 44510023Ssam 44610023Ssam case F_HDR|F_WRDATA: 44710023Ssam upaddr->upcs1 = UP_WHDR|UP_GO; 44810023Ssam break; 44910023Ssam 45010023Ssam case F_CHECK|F_WRDATA: 45110023Ssam case F_CHECK|F_RDDATA: 4529974Ssam upaddr->upcs1 = UP_WCDATA|UP_GO; 4539974Ssam break; 45410023Ssam 45510023Ssam case F_HCHECK|F_WRDATA: 45610023Ssam case F_HCHECK|F_RDDATA: 4579974Ssam upaddr->upcs1 = UP_WCHDR|UP_GO; 4589974Ssam break; 45910023Ssam 4609974Ssam default: 46110023Ssam io->i_error = ECMD; 46210023Ssam io->i_flgs &= ~F_TYPEMASK; 46310023Ssam return (1); 4649974Ssam } 46510023Ssam return (0); 4669974Ssam } 4679974Ssam 46810023Ssam /*ARGSUSED*/ 46910023Ssam upioctl(io, cmd, arg) 47010023Ssam struct iob *io; 47110023Ssam int cmd; 47210023Ssam caddr_t arg; 47310023Ssam { 47411365Ssam int unit = io->i_unit, flag; 47511365Ssam struct st *st = &upst[up_type[unit]], *tmp; 47610352Shelge 47710352Shelge switch(cmd) { 47810352Shelge 47911365Ssam case SAIODEBUG: 48011365Ssam flag = (int)arg; 48111365Ssam if (flag > 0) 48211365Ssam updebug[unit] |= flag; 48311365Ssam else 48411365Ssam updebug[unit] &= ~flag; 48511365Ssam return (0); 48611365Ssam 48710352Shelge case SAIODEVDATA: 48810352Shelge tmp = (struct st *)arg; 48910352Shelge *tmp = *st; 49011085Ssam return (0); 49110352Shelge } 49211085Ssam return (ECMD); 49310023Ssam } 494