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