1 /* up.c 4.12 83/03/02 */ 2 3 /* 4 * UNIBUS peripheral standalone driver 5 * with ECC correction and bad block forwarding. 6 * Also supports header operation and write 7 * check for data and/or header. 8 */ 9 #include "../h/param.h" 10 #include "../h/inode.h" 11 #include "../h/fs.h" 12 #include "../h/dkbad.h" 13 #include "../h/vmmac.h" 14 15 #include "../vax/pte.h" 16 #include "../vaxuba/upreg.h" 17 #include "../vaxuba/ubareg.h" 18 19 #include "saio.h" 20 #include "savax.h" 21 22 #define MAXBADDESC 126 /* max number of bad sectors recorded */ 23 #define SECTSIZ 512 /* sector size in bytes */ 24 #define HDRSIZ 4 /* number of bytes in sector header */ 25 26 #define MAXECC 5 /* max # bad bits allowed on ecc w/ F_ECCLM */ 27 28 u_short ubastd[] = { 0776700 }; 29 30 char up_gottype[MAXNUBA*8]; 31 char up_type[MAXNUBA*8]; 32 extern struct st upst[]; 33 34 struct dkbad upbad[MAXNUBA*8]; /* bad sector table */ 35 int sectsiz; /* real sector size */ 36 int updebug[MAXNUBA*8]; 37 #define UPF_BSEDEBUG 01 /* debugging bad sector forwarding */ 38 #define UPF_ECCDEBUG 02 /* debugging ecc correction */ 39 40 u_char up_offset[16] = { 41 UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400, 42 UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 43 UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200, 44 0, 0, 0, 0 45 }; 46 47 upopen(io) 48 register struct iob *io; 49 { 50 register unit = io->i_unit; 51 register struct updevice *upaddr; 52 register struct st *st; 53 54 if (io->i_boff < 0 || io->i_boff > 7) 55 _stop("up bad unit"); 56 upaddr = (struct updevice *)ubamem(unit, ubastd[0]); 57 while ((upaddr->upcs1 & UP_DVA) == 0) 58 ; 59 if (up_gottype[unit] == 0) { 60 register int i; 61 struct iob tio; 62 63 up_type[unit] = upmaptype(unit, upaddr); 64 if (up_type[unit] < 0) 65 _stop("unknown drive type"); 66 st = &upst[up_type[unit]]; 67 if (st->off[io->i_boff] == -1) 68 _stop("up bad unit"); 69 /* 70 * Read in the bad sector table. 71 */ 72 tio = *io; 73 tio.i_bn = st->nspc * st->ncyl - st->nsect; 74 tio.i_ma = (char *)&upbad[tio.i_unit]; 75 tio.i_cc = sizeof (struct dkbad); 76 tio.i_flgs |= F_RDDATA; 77 for (i = 0; i < 5; i++) { 78 if (upstrategy(&tio, READ) == sizeof (struct dkbad)) 79 break; 80 tio.i_bn += 2; 81 } 82 if (i == 5) { 83 printf("Unable to read bad sector table\n"); 84 for (i = 0; i < MAXBADDESC; i++) { 85 upbad[unit].bt_bad[i].bt_cyl = -1; 86 upbad[unit].bt_bad[i].bt_trksec = -1; 87 } 88 } 89 up_gottype[unit] = 1; 90 } 91 io->i_boff = st->off[io->i_boff] * st->nspc; 92 io->i_flgs &= ~F_TYPEMASK; 93 } 94 95 upstrategy(io, func) 96 register struct iob *io; 97 { 98 int cn, tn, sn, o; 99 register unit = io->i_unit; 100 daddr_t bn; 101 int recal, info, waitdry; 102 register struct updevice *upaddr = 103 (struct updevice *)ubamem(unit, ubastd[0]); 104 register struct st *st = &upst[up_type[unit]]; 105 int doprintf = 0; 106 107 sectsiz = SECTSIZ; 108 if (io->i_flgs & (F_HDR|F_HCHECK)) 109 sectsiz += HDRSIZ; 110 upaddr->upcs2 = unit % 8; 111 if ((upaddr->upds & UPDS_VV) == 0) { 112 upaddr->upcs1 = UP_DCLR|UP_GO; 113 upaddr->upcs1 = UP_PRESET|UP_GO; 114 upaddr->upof = UPOF_FMT22; 115 } 116 if ((upaddr->upds & UPDS_DREADY) == 0) 117 _stop("up not ready"); 118 info = ubasetup(io, 1); 119 upaddr->upwc = -io->i_cc / sizeof (short); 120 recal = 0; 121 io->i_errcnt = 0; 122 123 restart: 124 o = io->i_cc + (upaddr->upwc * sizeof (short)); 125 upaddr->upba = info + o; 126 bn = io->i_bn + o / sectsiz; 127 if (doprintf && updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) 128 printf("wc=%d o=%d i_bn=%d bn=%d\n", 129 upaddr->upwc, o, io->i_bn, bn); 130 while((upaddr->upds & UPDS_DRY) == 0) 131 ; 132 if (upstart(io, bn) != 0) { 133 ubafree(io, info); 134 return (-1); 135 } 136 do { 137 DELAY(25); 138 } while ((upaddr->upcs1 & UP_RDY) == 0); 139 /* 140 * If transfer has completed, free UNIBUS 141 * resources and return transfer size. 142 */ 143 if ((upaddr->upds&UPDS_ERR) == 0 && (upaddr->upcs1&UP_TRE) == 0) { 144 ubafree(io, info); 145 return (io->i_cc); 146 } 147 if (updebug[unit] & (UPF_ECCDEBUG|UPF_BSEDEBUG)) { 148 printf("up error: (cyl,trk,sec)=(%d,%d,%d) ", 149 upaddr->updc, upaddr->upda>>8, (upaddr->upda&0x1f)-1); 150 printf("cs2=%b er1=%b er2=%b wc=%d\n", 151 upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 152 UPER1_BITS, upaddr->uper2, UPER2_BITS, upaddr->upwc); 153 } 154 waitdry = 0; 155 while ((upaddr->upds&UPDS_DRY) == 0 && ++waitdry < sectsiz) 156 DELAY(5); 157 if (upaddr->uper1&UPER1_WLE) { 158 /* 159 * Give up on write locked devices immediately. 160 */ 161 printf("up%d: write locked\n", unit); 162 return (-1); 163 } 164 if (++io->i_errcnt > 27) { 165 /* 166 * After 28 retries (16 without offset, and 167 * 12 with offset positioning) give up. 168 * But first, if the error is a header CRC, 169 * check if a replacement sector exists in 170 * the bad sector table. 171 */ 172 if ((upaddr->uper1&UPER1_HCRC) && (io->i_flgs&F_NBSF) == 0 && 173 upecc(io, BSE) == 0) 174 goto success; 175 io->i_error = EHER; 176 if (upaddr->upcs2 & UPCS2_WCE) 177 io->i_error = EWCK; 178 hard: 179 bn = io->i_bn + 180 (io->i_cc + upaddr->upwc * sizeof (short)) / sectsiz; 181 cn = bn/st->nspc; 182 sn = bn%st->nspc; 183 tn = sn/st->nsect; 184 sn = sn%st->nsect; 185 printf( 186 "up error: (cyl,trk,sec)=(%d,%d,%d) cs2=%b er1=%b er2=%b\n", 187 cn, tn, sn, 188 upaddr->upcs2, UPCS2_BITS, upaddr->uper1, 189 UPER1_BITS, upaddr->uper2, UPER2_BITS); 190 upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 191 io->i_errblk = bn; 192 return (io->i_cc + upaddr->upwc * sizeof(short)); 193 } 194 if (upaddr->uper2 & UPER2_BSE) { 195 if ((io->i_flgs&F_NBSF) == 0 && upecc(io, BSE) == 0) 196 goto success; 197 io->i_error = EBSE; 198 goto hard; 199 } 200 /* 201 * ECC error. If a soft error, correct it; 202 * otherwise fall through and retry the transfer. 203 */ 204 if ((upaddr->uper1 & (UPER1_DCK|UPER1_ECH)) == UPER1_DCK) { 205 if (upecc(io, ECC) == 0) 206 goto success; 207 io->i_error = EECC; 208 goto hard; 209 } 210 /* 211 * Clear drive error and, every eight attempts, 212 * (starting with the fourth) 213 * recalibrate to clear the slate. 214 */ 215 upaddr->upcs1 = UP_TRE|UP_DCLR|UP_GO; 216 if ((io->i_errcnt&07) == 4 ) { 217 upaddr->upcs1 = UP_RECAL|UP_GO; 218 recal = 1; 219 goto restart; 220 } 221 /* 222 * Advance recalibration finite state machine 223 * if recalibrate in progress, through 224 * RECAL 225 * SEEK 226 * OFFSET (optional) 227 * RETRY 228 */ 229 switch (recal) { 230 231 case 1: 232 upaddr->updc = cn; 233 upaddr->upcs1 = UP_SEEK|UP_GO; 234 recal = 2; 235 goto restart; 236 237 case 2: 238 if (io->i_errcnt < 16 || (func & READ) == 0) 239 goto donerecal; 240 upaddr->upof = up_offset[io->i_errcnt & 017] | UPOF_FMT22; 241 upaddr->upcs1 = UP_OFFSET|UP_GO; 242 recal = 3; 243 goto restart; 244 245 donerecal: 246 case 3: 247 recal = 0; 248 break; 249 } 250 /* 251 * If we were offset positioning, 252 * return to centerline. 253 */ 254 if (io->i_errcnt >= 16) { 255 upaddr->upof = UPOF_FMT22; 256 upaddr->upcs1 = UP_RTC|UP_GO; 257 while ((upaddr->upds&UPDS_DRY) == 0) 258 DELAY(25); 259 } 260 goto restart; 261 262 success: 263 #define rounddown(x, y) (((x) / (y)) * (y)) 264 upaddr->upwc = rounddown(upaddr->upwc, sectsiz / sizeof (short)); 265 if (upaddr->upwc) { 266 doprintf++; 267 goto restart; 268 } 269 /* 270 * Release UNIBUS 271 */ 272 ubafree(io, info); 273 return (io->i_cc); 274 } 275 276 /* 277 * Correct an ECC error, and restart the 278 * i/o to complete the transfer (if necessary). 279 * This is quite complicated because the transfer 280 * may be going to an odd memory address base and/or 281 * across a page boundary. 282 */ 283 upecc(io, flag) 284 register struct iob *io; 285 int flag; 286 { 287 register i, unit = io->i_unit; 288 register struct updevice *up = 289 (struct updevice *)ubamem(unit, ubastd[0]); 290 register struct st *st; 291 caddr_t addr; 292 int bn, twc, npf, mask, cn, tn, sn; 293 daddr_t bbn; 294 295 /* 296 * Npf is the number of sectors transferred 297 * before the sector containing the ECC error; 298 * bn is the current block number. 299 */ 300 twc = up->upwc; 301 npf = ((twc * sizeof(short)) + io->i_cc) / sectsiz; 302 if (updebug[unit] & UPF_ECCDEBUG) 303 printf("npf=%d mask=0x%x ec1=%d wc=%d\n", 304 npf, up->upec2, up->upec1, twc); 305 bn = io->i_bn + npf; 306 st = &upst[up_type[unit]]; 307 cn = bn/st->nspc; 308 sn = bn%st->nspc; 309 tn = sn/st->nsect; 310 sn = sn%st->nsect; 311 312 /* 313 * ECC correction. 314 */ 315 if (flag == ECC) { 316 int bit, o, ecccnt; 317 318 ecccnt = 0; 319 mask = up->upec2; 320 printf("up%d: soft ecc sn%d\n", unit, bn); 321 /* 322 * Compute the byte and bit position of 323 * the error. o is the byte offset in 324 * the transfer at which the correction 325 * applied. 326 */ 327 npf--; 328 i = up->upec1 - 1; /* -1 makes 0 origin */ 329 bit = i & 07; 330 o = (i & ~07) >> 3; 331 up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 332 /* 333 * Correct while possible bits remain of mask. 334 * Since mask contains 11 bits, we continue while 335 * the bit offset is > -11. Also watch out for 336 * end of this block and the end of the transfer. 337 */ 338 while (o < sectsiz && (npf*sectsiz)+o < io->i_cc && bit > -11) { 339 /* 340 * addr = 341 * (base address of transfer) + 342 * (# sectors transferred before the error) * 343 * (sector size) + 344 * (byte offset to incorrect data) 345 */ 346 addr = io->i_ma + (npf * sectsiz) + o; 347 /* 348 * No data transfer occurs with a write check, 349 * so don't correct the resident copy of data. 350 */ 351 if ((io->i_flgs & (F_CHECK|F_HCHECK)) == 0) { 352 if (updebug[unit] & UPF_ECCDEBUG) 353 printf("addr=0x%x old=0x%x ", addr, 354 (*addr&0xff)); 355 *addr ^= (mask << bit); 356 if (updebug[unit] & UPF_ECCDEBUG) 357 printf("new=0x%x\n", (*addr&0xff)); 358 } 359 o++, bit -= 8; 360 if ((io->i_flgs&F_ECCLM) && ++ecccnt > MAXECC) 361 return (1); 362 } 363 return (0); 364 } 365 366 /* 367 * Bad sector forwarding. 368 */ 369 if (flag == BSE) { 370 /* 371 * If not in bad sector table, 372 * indicate a hard error to caller. 373 */ 374 up->upcs1 = UP_TRE|UP_DCLR|UP_GO; 375 if ((bbn = isbad(&upbad[unit], cn, tn, sn)) < 0) 376 return (1); 377 bbn = (st->ncyl * st->nspc) - st->nsect - 1 - bbn; 378 twc = up->upwc + sectsiz; 379 up->upwc = - (sectsiz / sizeof (short)); 380 if (updebug[unit] & UPF_BSEDEBUG) 381 printf("revector sn %d to %d\n", sn, bbn); 382 /* 383 * Clear the drive & read the replacement 384 * sector. If this is in the middle of a 385 * transfer, then set up the controller 386 * registers in a normal fashion. 387 * The UNIBUS address need not be changed. 388 */ 389 while ((up->upcs1 & UP_RDY) == 0) 390 ; 391 if (upstart(io, bbn)) 392 return (1); /* error */ 393 io->i_errcnt = 0; /* success */ 394 do { 395 DELAY(25); 396 } while ((up->upcs1 & UP_RDY) == 0) ; 397 if ((up->upds & UPDS_ERR) || (up->upcs1 & UP_TRE)) { 398 up->upwc = twc - sectsiz; 399 return (1); 400 } 401 } 402 if (twc) 403 up->upwc = twc; 404 return (0); 405 } 406 407 upstart(io, bn) 408 register struct iob *io; 409 daddr_t bn; 410 { 411 register struct updevice *upaddr = 412 (struct updevice *)ubamem(io->i_unit, ubastd[0]); 413 register struct st *st = &upst[up_type[io->i_unit]]; 414 int sn, tn; 415 416 sn = bn%st->nspc; 417 tn = sn/st->nsect; 418 sn %= st->nsect; 419 upaddr->updc = bn/st->nspc; 420 upaddr->upda = (tn << 8) + sn; 421 switch (io->i_flgs & F_TYPEMASK) { 422 423 case F_RDDATA: 424 upaddr->upcs1 = UP_RCOM|UP_GO; 425 break; 426 427 case F_WRDATA: 428 upaddr->upcs1 = UP_WCOM|UP_GO; 429 break; 430 431 case F_HDR|F_RDDATA: 432 upaddr->upcs1 = UP_RHDR|UP_GO; 433 break; 434 435 case F_HDR|F_WRDATA: 436 upaddr->upcs1 = UP_WHDR|UP_GO; 437 break; 438 439 case F_CHECK|F_WRDATA: 440 case F_CHECK|F_RDDATA: 441 upaddr->upcs1 = UP_WCDATA|UP_GO; 442 break; 443 444 case F_HCHECK|F_WRDATA: 445 case F_HCHECK|F_RDDATA: 446 upaddr->upcs1 = UP_WCHDR|UP_GO; 447 break; 448 449 default: 450 io->i_error = ECMD; 451 io->i_flgs &= ~F_TYPEMASK; 452 return (1); 453 } 454 return (0); 455 } 456 457 /*ARGSUSED*/ 458 upioctl(io, cmd, arg) 459 struct iob *io; 460 int cmd; 461 caddr_t arg; 462 { 463 int unit = io->i_unit, flag; 464 struct st *st = &upst[up_type[unit]], *tmp; 465 466 switch(cmd) { 467 468 case SAIODEBUG: 469 flag = (int)arg; 470 if (flag > 0) 471 updebug[unit] |= flag; 472 else 473 updebug[unit] &= ~flag; 474 return (0); 475 476 case SAIODEVDATA: 477 tmp = (struct st *)arg; 478 *tmp = *st; 479 return (0); 480 } 481 return (ECMD); 482 } 483