1 /* cy.c 1.11 87/04/01 */ 2 3 #include "yc.h" 4 #if NCY > 0 5 /* 6 * Cipher Tapemaster driver. 7 */ 8 #define CYDEBUG 9 #ifdef CYDEBUG 10 int cydebug = 0; 11 #define dlog(params) if (cydebug) log params 12 #else 13 #define dlog(params) /* */ 14 #endif 15 16 #include "param.h" 17 #include "systm.h" 18 #include "vm.h" 19 #include "buf.h" 20 #include "file.h" 21 #include "dir.h" 22 #include "user.h" 23 #include "proc.h" 24 #include "signal.h" 25 #include "uio.h" 26 #include "ioctl.h" 27 #include "mtio.h" 28 #include "errno.h" 29 #include "cmap.h" 30 #include "kernel.h" 31 #include "syslog.h" 32 #include "tty.h" 33 34 #include "../tahoe/cpu.h" 35 #include "../tahoe/mtpr.h" 36 #include "../tahoe/pte.h" 37 38 #include "../tahoevba/vbavar.h" 39 #define CYERROR 40 #include "../tahoevba/cyreg.h" 41 42 /* 43 * There is a ccybuf per tape controller. 44 * It is used as the token to pass to the internal routines 45 * to execute tape ioctls, and also acts as a lock on the slaves 46 * on the controller, since there is only one per controller. 47 * In particular, when the tape is rewinding on close we release 48 * the user process but any further attempts to use the tape drive 49 * before the rewind completes will hang waiting for ccybuf. 50 */ 51 struct buf ccybuf[NCY]; 52 53 /* 54 * Raw tape operations use rcybuf. The driver notices when 55 * rcybuf is being used and allows the user program to contine 56 * after errors and read records not of the standard length. 57 */ 58 struct buf rcybuf[NCY]; 59 60 int cyprobe(), cyslave(), cyattach(); 61 struct buf ycutab[NYC]; 62 short yctocy[NYC]; 63 struct vba_ctlr *cyminfo[NCY]; 64 struct vba_device *ycdinfo[NYC]; 65 long cystd[] = { 0 }; 66 struct vba_driver cydriver = 67 { cyprobe, cyslave, cyattach, 0, cystd, "yc", ycdinfo, "cy", cyminfo }; 68 69 /* bits in minor device */ 70 #define YCUNIT(dev) (minor(dev)&03) 71 #define CYUNIT(dev) (yctocy[YCUNIT(dev)]) 72 #define T_NOREWIND 0x04 73 #define T_1600BPI 0x00 /* pseudo */ 74 #define T_3200BPI 0x08 /* unused */ 75 76 #define INF 1000000L /* close to infinity */ 77 78 /* 79 * Software state and shared command areas per controller. 80 * 81 * The i/o intermediate buffer must be allocated in startup() 82 * so its address will fit in 20-bits (YECH!!!!!!!!!!!!!!). 83 */ 84 struct cy_softc { 85 int cy_bs; /* controller's buffer size */ 86 struct cyscp *cy_scp; /* system configuration block address */ 87 struct cyccb cy_ccb; /* channel control block */ 88 struct cyscb cy_scb; /* system configuration block */ 89 struct cytpb cy_tpb; /* tape parameter block */ 90 struct cytpb cy_nop; /* nop parameter block for cyintr */ 91 struct vb_buf cy_rbuf; /* vba resources */ 92 } cy_softc[NCY]; 93 94 /* 95 * Software state per tape transport. 96 */ 97 struct yc_softc { 98 char yc_openf; /* lock against multiple opens */ 99 char yc_lastiow; /* last operation was a write */ 100 short yc_tact; /* timeout is active */ 101 long yc_timo; /* time until timeout expires */ 102 u_short yc_control; /* copy of last tpcb.tpcontrol */ 103 u_short yc_status; /* copy of last tpcb.tpstatus */ 104 u_short yc_resid; /* copy of last bc */ 105 u_short yc_dens; /* prototype control word with density info */ 106 struct tty *yc_ttyp; /* user's tty for errors */ 107 daddr_t yc_blkno; /* block number, for block device tape */ 108 daddr_t yc_nxrec; /* position of end of tape, if known */ 109 int yc_blksize; /* current tape blocksize estimate */ 110 int yc_blks; /* number of I/O operations since open */ 111 int yc_softerrs; /* number of soft I/O errors since open */ 112 } yc_softc[NYC]; 113 114 /* 115 * States for vm->um_tab.b_active, the per controller state flag. 116 * This is used to sequence control in the driver. 117 */ 118 #define SSEEK 1 /* seeking */ 119 #define SIO 2 /* doing seq i/o */ 120 #define SCOM 3 /* sending control command */ 121 #define SREW 4 /* sending a rewind */ 122 #define SERASE 5 /* erase inter-record gap */ 123 #define SERASED 6 /* erased inter-record gap */ 124 125 /* there's no way to figure these out dynamically? -- yech */ 126 struct cyscp *cyscp[] = 127 { (struct cyscp *)0xc0000c06, (struct cyscp *)0xc0000c16 }; 128 #define NCYSCP (sizeof (cyscp) / sizeof (cyscp[0])) 129 130 cyprobe(reg, vm) 131 caddr_t reg; 132 struct vba_ctlr *vm; 133 { 134 register br, cvec; /* must be r12, r11 */ 135 register struct cy_softc *cy; 136 int ctlr = vm->um_ctlr; 137 138 #ifdef lint 139 br = 0; cvec = br; br = cvec; 140 cyintr(0); 141 #endif 142 if (badcyaddr(reg+1)) 143 return (0); 144 if (ctlr > NCYSCP || cyscp[ctlr] == 0) /* XXX */ 145 return (0); 146 cy = &cy_softc[ctlr]; 147 cy->cy_scp = cyscp[ctlr]; /* XXX */ 148 /* 149 * Tapemaster controller must have interrupt handler 150 * disable interrupt, so we'll just kludge things 151 * (stupid multibus non-vectored interrupt crud). 152 */ 153 if (cyinit(ctlr, reg)) { 154 uncache(&cy->cy_tpb.tpcount); 155 cy->cy_bs = htoms(cy->cy_tpb.tpcount); 156 /* 157 * Setup nop parameter block for clearing interrupts. 158 */ 159 cy->cy_nop.tpcmd = CY_NOP; 160 cy->cy_nop.tpcontrol = 0; 161 /* 162 * Allocate page tables. 163 */ 164 if (cybuf == 0) { 165 printf("no cy buffer!!!\n"); 166 return (0); 167 } 168 cy->cy_rbuf.vb_rawbuf = cybuf + ctlr * CYMAXIO; 169 vbainit(&cy->cy_rbuf, CYMAXIO, VB_20BIT); 170 171 br = 0x13, cvec = 0x80; /* XXX */ 172 return (sizeof (struct cyccb)); 173 } else 174 return (0); 175 } 176 177 /* 178 * Check to see if a drive is attached to a controller. 179 * Since we can only tell that a drive is there if a tape is loaded and 180 * the drive is placed online, we always indicate the slave is present. 181 */ 182 cyslave(vi, addr) 183 struct vba_device *vi; 184 caddr_t addr; 185 { 186 187 #ifdef lint 188 vi = vi; addr = addr; 189 #endif 190 return (1); 191 } 192 193 cyattach(vi) 194 struct vba_device *vi; 195 { 196 register struct cy_softc *cy; 197 int ctlr = vi->ui_mi->um_ctlr; 198 199 yctocy[vi->ui_unit] = ctlr; 200 cy = &cy_softc[ctlr]; 201 if (vi->ui_slave == 0 && cy->cy_bs) 202 printf("; %dkb buffer", cy->cy_bs/1024); 203 } 204 205 /* 206 * Initialize the controller after a controller reset or 207 * during autoconfigure. All of the system control blocks 208 * are initialized and the controller is asked to configure 209 * itself for later use. 210 */ 211 cyinit(ctlr, addr) 212 int ctlr; 213 register caddr_t addr; 214 { 215 register struct cy_softc *cy = &cy_softc[ctlr]; 216 register int *pte; 217 218 /* 219 * Initialize the system configuration pointer. 220 */ 221 /* make kernel writable */ 222 pte = (int *)&Sysmap[btop((int)cy->cy_scp &~ KERNBASE)]; 223 *pte &= ~PG_PROT; *pte |= PG_KW; 224 mtpr(TBIS, cy->cy_scp); 225 /* load the correct values in the scp */ 226 cy->cy_scp->csp_buswidth = CSP_16BITS; 227 cyldmba(cy->cy_scp->csp_scb, (caddr_t)&cy->cy_scb); 228 /* put it back to read-only */ 229 *pte &= ~PG_PROT; *pte |= PG_KR; 230 mtpr(TBIS, cy->cy_scp); 231 232 /* 233 * Init system configuration block. 234 */ 235 cy->cy_scb.csb_fixed = CSB_FIXED; 236 /* set pointer to the channel control block */ 237 cyldmba(cy->cy_scb.csb_ccb, (caddr_t)&cy->cy_ccb); 238 239 /* 240 * Initialize the chanel control block. 241 */ 242 cy->cy_ccb.cbcw = CBCW_CLRINT; 243 cy->cy_ccb.cbgate = GATE_OPEN; 244 /* set pointer to the tape parameter block */ 245 cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb); 246 247 /* 248 * Issue a nop cmd and get the internal buffer size for buffered i/o. 249 */ 250 cy->cy_tpb.tpcmd = CY_NOP; 251 cy->cy_tpb.tpcontrol = CYCW_16BITS; 252 cy->cy_ccb.cbgate = GATE_CLOSED; 253 CY_GO(addr); 254 if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) { 255 uncache(&cy->cy_tpb.tpstatus); 256 printf("cy%d: timeout or err during init, status=%b\n", ctlr, 257 cy->cy_tpb.tpstatus, CYS_BITS); 258 return (0); 259 } 260 cy->cy_tpb.tpcmd = CY_CONFIG; 261 cy->cy_tpb.tpcontrol = CYCW_16BITS; 262 cy->cy_ccb.cbgate = GATE_CLOSED; 263 CY_GO(addr); 264 if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) { 265 uncache(&cy->cy_tpb.tpstatus); 266 printf("cy%d: configuration failure, status=%b\n", ctlr, 267 cy->cy_tpb.tpstatus, CYS_BITS); 268 return (0); 269 } 270 return (1); 271 } 272 273 int cytimer(); 274 /* 275 * Open the device. Tapes are unique open 276 * devices, so we refuse if it is already open. 277 * We also check that a tape is available, and 278 * don't block waiting here; if you want to wait 279 * for a tape you should timeout in user code. 280 */ 281 cyopen(dev, flag) 282 dev_t dev; 283 register int flag; 284 { 285 register int ycunit; 286 register struct vba_device *vi; 287 register struct yc_softc *yc; 288 int s; 289 290 ycunit = YCUNIT(dev); 291 if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0) 292 return (ENXIO); 293 if ((yc = &yc_softc[ycunit])->yc_openf) 294 return (EBUSY); 295 yc->yc_openf = 1; 296 #define PACKUNIT(vi) \ 297 (((vi->ui_slave&1)<<11)|((vi->ui_slave&2)<<9)|((vi->ui_slave&4)>>2)) 298 /* no way to select density */ 299 yc->yc_dens = PACKUNIT(vi)|CYCW_IE|CYCW_16BITS; 300 if (yc->yc_tact == 0) { 301 yc->yc_timo = INF; 302 yc->yc_tact = 1; 303 timeout(cytimer, (caddr_t)dev, 5*hz); 304 } 305 cycommand(dev, CY_SENSE, 1); 306 if ((yc->yc_status&CYS_OL) == 0) { /* not on-line */ 307 uprintf("yc%d: not online\n", ycunit); 308 yc->yc_openf = 0; 309 return (ENXIO); 310 } 311 if ((flag&FWRITE) && (yc->yc_status&CYS_WP)) { 312 uprintf("yc%d: no write ring\n", ycunit); 313 yc->yc_openf = 0; 314 return (ENXIO); 315 } 316 yc->yc_blkno = (daddr_t)0; 317 yc->yc_nxrec = INF; 318 yc->yc_lastiow = 0; 319 yc->yc_blksize = 1024; /* guess > 0 */ 320 yc->yc_blks = 0; 321 yc->yc_softerrs = 0; 322 yc->yc_ttyp = u.u_ttyp; 323 return (0); 324 } 325 326 /* 327 * Close tape device. 328 * 329 * If tape was open for writing or last operation was a write, 330 * then write two EOF's and backspace over the last one. 331 * Unless this is a non-rewinding special file, rewind the tape. 332 * Make the tape available to others. 333 */ 334 cyclose(dev, flag) 335 dev_t dev; 336 int flag; 337 { 338 struct yc_softc *yc = &yc_softc[YCUNIT(dev)]; 339 340 if (flag == FWRITE || (flag&FWRITE) && yc->yc_lastiow) { 341 cycommand(dev, CY_WEOF, 2); 342 cycommand(dev, CY_SREV, 1); 343 } 344 if ((minor(dev)&T_NOREWIND) == 0) 345 /* 346 * 0 count means don't hang waiting for rewind complete 347 * rather ccybuf stays busy until the operation completes 348 * preventing further opens from completing by preventing 349 * a CY_SENSE from completing. 350 */ 351 cycommand(dev, CY_REW, 0); 352 if (yc->yc_blks > 10 && yc->yc_softerrs > yc->yc_blks / 10) 353 log(LOG_INFO, "yc%d: %d soft errors in %d blocks\n", 354 YCUNIT(dev), yc->yc_softerrs, yc->yc_blks); 355 dlog((LOG_INFO, "%d soft errors in %d blocks\n", 356 yc->yc_softerrs, yc->yc_blks)); 357 yc->yc_openf = 0; 358 return (0); 359 } 360 361 /* 362 * Execute a command on the tape drive a specified number of times. 363 */ 364 cycommand(dev, com, count) 365 dev_t dev; 366 int com, count; 367 { 368 register struct buf *bp; 369 int s; 370 371 bp = &ccybuf[CYUNIT(dev)]; 372 s = spl3(); 373 dlog((LOG_INFO, "cycommand(%o, %x, %d), b_flags %x\n", 374 dev, com, count, bp->b_flags)); 375 while (bp->b_flags&B_BUSY) { 376 /* 377 * This special check is because B_BUSY never 378 * gets cleared in the non-waiting rewind case. 379 */ 380 if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 381 break; 382 bp->b_flags |= B_WANTED; 383 sleep((caddr_t)bp, PRIBIO); 384 } 385 bp->b_flags = B_BUSY|B_READ; 386 splx(s); 387 bp->b_dev = dev; 388 bp->b_repcnt = count; 389 bp->b_command = com; 390 bp->b_blkno = 0; 391 cystrategy(bp); 392 /* 393 * In case of rewind from close; don't wait. 394 * This is the only case where count can be 0. 395 */ 396 if (count == 0) 397 return; 398 biowait(bp); 399 if (bp->b_flags&B_WANTED) 400 wakeup((caddr_t)bp); 401 bp->b_flags &= B_ERROR; 402 } 403 404 cystrategy(bp) 405 register struct buf *bp; 406 { 407 int ycunit = YCUNIT(bp->b_dev); 408 register struct vba_ctlr *vm; 409 register struct buf *dp; 410 int s; 411 412 /* 413 * Put transfer at end of unit queue. 414 */ 415 dlog((LOG_INFO, "cystrategy(%o, %x)\n", bp->b_dev, bp->b_command)); 416 dp = &ycutab[ycunit]; 417 bp->av_forw = NULL; 418 vm = ycdinfo[ycunit]->ui_mi; 419 /* BEGIN GROT */ 420 if (bp == &rcybuf[CYUNIT(bp->b_dev)]) { 421 if (bp->b_bcount > CYMAXIO) { 422 uprintf("cy%d: i/o size too large\n", vm->um_ctlr); 423 bp->b_error = EIO; 424 bp->b_resid = bp->b_bcount; 425 bp->b_flags |= B_ERROR; 426 biodone(bp); 427 return; 428 } 429 } 430 /* END GROT */ 431 s = spl3(); 432 if (dp->b_actf == NULL) { 433 dp->b_actf = bp; 434 /* 435 * Transport not already active... 436 * put at end of controller queue. 437 */ 438 dp->b_forw = NULL; 439 if (vm->um_tab.b_actf == NULL) 440 vm->um_tab.b_actf = dp; 441 else 442 vm->um_tab.b_actl->b_forw = dp; 443 } else 444 dp->b_actl->av_forw = bp; 445 dp->b_actl = bp; 446 /* 447 * If the controller is not busy, get it going. 448 */ 449 if (vm->um_tab.b_active == 0) 450 cystart(vm); 451 splx(s); 452 } 453 454 /* 455 * Start activity on a cy controller. 456 */ 457 cystart(vm) 458 register struct vba_ctlr *vm; 459 { 460 register struct buf *bp, *dp; 461 register struct yc_softc *yc; 462 register struct cy_softc *cy; 463 int ycunit; 464 daddr_t blkno; 465 466 dlog((LOG_INFO, "cystart()\n")); 467 /* 468 * Look for an idle transport on the controller. 469 */ 470 loop: 471 if ((dp = vm->um_tab.b_actf) == NULL) 472 return; 473 if ((bp = dp->b_actf) == NULL) { 474 vm->um_tab.b_actf = dp->b_forw; 475 goto loop; 476 } 477 ycunit = YCUNIT(bp->b_dev); 478 yc = &yc_softc[ycunit]; 479 cy = &cy_softc[CYUNIT(bp->b_dev)]; 480 /* 481 * Default is that last command was NOT a write command; 482 * if we do a write command we will notice this in cyintr(). 483 */ 484 yc->yc_lastiow = 0; 485 if (yc->yc_openf < 0 || 486 (bp->b_command != CY_SENSE && (cy->cy_tpb.tpstatus&CYS_OL) == 0)) { 487 /* 488 * Have had a hard error on a non-raw tape 489 * or the tape unit is now unavailable (e.g. 490 * taken off line). 491 */ 492 dlog((LOG_INFO, "openf %d command %x status %b\n", 493 yc->yc_openf, bp->b_command, cy->cy_tpb.tpstatus, CYS_BITS)); 494 bp->b_flags |= B_ERROR; 495 goto next; 496 } 497 if (bp == &ccybuf[CYUNIT(bp->b_dev)]) { 498 /* 499 * Execute control operation with the specified count. 500 * 501 * Set next state; give 5 minutes to complete 502 * rewind or file mark search, or 10 seconds per 503 * iteration (minimum 60 seconds and max 5 minutes) 504 * to complete other ops. 505 */ 506 if (bp->b_command == CY_REW) { 507 vm->um_tab.b_active = SREW; 508 yc->yc_timo = 5*60; 509 } else { 510 vm->um_tab.b_active = SCOM; 511 yc->yc_timo = imin(imax(10*(int)bp->b_repcnt,60),5*60); 512 } 513 cy->cy_tpb.tprec = htoms(bp->b_repcnt); 514 dlog((LOG_INFO, "bpcmd ")); 515 goto dobpcmd; 516 } 517 /* 518 * The following checks handle boundary cases for operation 519 * on no-raw tapes. On raw tapes the initialization of 520 * yc->yc_nxrec by cyphys causes them to be skipped normally 521 * (except in the case of retries). 522 */ 523 if (bp->b_blkno > yc->yc_nxrec) { 524 /* 525 * Can't read past known end-of-file. 526 */ 527 bp->b_flags |= B_ERROR; 528 bp->b_error = ENXIO; 529 goto next; 530 } 531 if (bp->b_blkno == yc->yc_nxrec && bp->b_flags&B_READ) { 532 /* 533 * Reading at end of file returns 0 bytes. 534 */ 535 bp->b_resid = bp->b_bcount; 536 clrbuf(bp); 537 goto next; 538 } 539 if ((bp->b_flags&B_READ) == 0) 540 /* 541 * Writing sets EOF. 542 */ 543 yc->yc_nxrec = bp->b_blkno + 1; 544 if ((blkno = yc->yc_blkno) == bp->b_blkno) { 545 caddr_t addr; 546 int cmd; 547 548 /* 549 * Choose the appropriate i/o command based on the 550 * transfer size, the estimated block size, 551 * and the controller's internal buffer size. 552 * If we're retrying a read on a raw device because 553 * the original try was a buffer request which failed 554 * due to a record length error, then we force the use 555 * of the raw controller read (YECH!!!!). 556 */ 557 if (bp->b_flags&B_READ) { 558 if ((bp->b_bcount > cy->cy_bs && 559 yc->yc_blksize > cy->cy_bs) || vm->um_tab.b_errcnt) 560 cmd = CY_RCOM; 561 else 562 cmd = CY_BRCOM; 563 } else { 564 /* 565 * On write error retries erase the 566 * inter-record gap before rewriting. 567 */ 568 if (vm->um_tab.b_errcnt && 569 vm->um_tab.b_active != SERASED) { 570 vm->um_tab.b_active = SERASE; 571 bp->b_command = CY_ERASE; 572 yc->yc_timo = 60; 573 goto dobpcmd; 574 } 575 cmd = (bp->b_bcount > cy->cy_bs) ? CY_WCOM : CY_BWCOM; 576 } 577 vm->um_tab.b_active = SIO; 578 addr = (caddr_t)vbasetup(bp, &cy->cy_rbuf, 1); 579 cy->cy_tpb.tpcmd = cmd; 580 cy->cy_tpb.tpcontrol = yc->yc_dens; 581 if (cmd == CY_RCOM || cmd == CY_WCOM) 582 cy->cy_tpb.tpcontrol |= CYCW_LOCK; 583 cy->cy_tpb.tpstatus = 0; 584 cy->cy_tpb.tpcount = 0; 585 cyldmba(cy->cy_tpb.tpdata, (caddr_t)addr); 586 cy->cy_tpb.tprec = 0; 587 if (cmd == CY_BRCOM && bp->b_bcount > cy->cy_bs) 588 cy->cy_tpb.tpsize = htoms(cy->cy_bs); 589 else 590 cy->cy_tpb.tpsize = htoms(bp->b_bcount); 591 cyldmba(cy->cy_tpb.tplink, (caddr_t)0); 592 do 593 uncache(&cy->cy_ccb.cbgate); 594 while (cy->cy_ccb.cbgate == GATE_CLOSED); 595 cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb); 596 cy->cy_ccb.cbcw = CBCW_IE; 597 cy->cy_ccb.cbgate = GATE_CLOSED; 598 dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x size %d\n", 599 vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol, 600 htoms(cy->cy_tpb.tpsize))); 601 CY_GO(vm->um_addr); 602 return; 603 } 604 /* 605 * Tape positioned incorrectly; set to seek forwards 606 * or backwards to the correct spot. This happens 607 * for raw tapes only on error retries. 608 */ 609 vm->um_tab.b_active = SSEEK; 610 if (blkno < bp->b_blkno) { 611 bp->b_command = CY_SFORW; 612 cy->cy_tpb.tprec = htoms(bp->b_blkno - blkno); 613 } else { 614 bp->b_command = CY_SREV; 615 cy->cy_tpb.tprec = htoms(blkno - bp->b_blkno); 616 } 617 yc->yc_timo = imin(imax(10 * htoms(cy->cy_tpb.tprec), 60), 5*60); 618 dobpcmd: 619 /* 620 * Do the command in bp. Reverse direction commands 621 * are indicated by having CYCW_REV or'd into their 622 * value. For these we must set the appropriate bit 623 * in the control field. 624 */ 625 if (bp->b_command&CYCW_REV) { 626 cy->cy_tpb.tpcmd = bp->b_command &~ CYCW_REV; 627 cy->cy_tpb.tpcontrol = yc->yc_dens | CYCW_REV; 628 dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol)); 629 } else { 630 cy->cy_tpb.tpcmd = bp->b_command; 631 cy->cy_tpb.tpcontrol = yc->yc_dens; 632 dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol)); 633 } 634 cy->cy_tpb.tpstatus = 0; 635 cy->cy_tpb.tpcount = 0; 636 cyldmba(cy->cy_tpb.tplink, (caddr_t)0); 637 do 638 uncache(&cy->cy_ccb.cbgate); 639 while (cy->cy_ccb.cbgate == GATE_CLOSED); 640 cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb); 641 cy->cy_ccb.cbcw = CBCW_IE; 642 cy->cy_ccb.cbgate = GATE_CLOSED; 643 dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x rec %d\n", 644 vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol, 645 htoms(cy->cy_tpb.tprec))); 646 CY_GO(vm->um_addr); 647 return; 648 next: 649 /* 650 * Done with this operation due to error or the 651 * fact that it doesn't do anything. 652 * Dequeue the transfer and continue 653 * processing this slave. 654 */ 655 vm->um_tab.b_errcnt = 0; 656 dp->b_actf = bp->av_forw; 657 biodone(bp); 658 goto loop; 659 } 660 661 /* 662 * Cy interrupt routine. 663 */ 664 cyintr(cyunit) 665 int cyunit; 666 { 667 struct buf *dp; 668 register struct buf *bp; 669 register struct vba_ctlr *vm = cyminfo[cyunit]; 670 register struct cy_softc *cy; 671 register struct yc_softc *yc; 672 int err; 673 register state; 674 675 dlog((LOG_INFO, "cyintr(%d)\n", cyunit)); 676 /* 677 * First, turn off the interrupt from the controller 678 * (device uses Multibus non-vectored interrupts...yech). 679 */ 680 cy = &cy_softc[vm->um_ctlr]; 681 cy->cy_ccb.cbcw = CBCW_CLRINT; 682 cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_nop); 683 cy->cy_ccb.cbgate = GATE_CLOSED; 684 CY_GO(vm->um_addr); 685 if ((dp = vm->um_tab.b_actf) == NULL) { 686 dlog((LOG_ERR, "cy%d: stray interrupt", vm->um_ctlr)); 687 return; 688 } 689 bp = dp->b_actf; 690 cy = &cy_softc[cyunit]; 691 cyuncachetpb(cy); 692 yc = &yc_softc[YCUNIT(bp->b_dev)]; 693 /* 694 * If last command was a rewind and tape is 695 * still moving, wait for the operation to complete. 696 */ 697 if (vm->um_tab.b_active == SREW) { 698 vm->um_tab.b_active = SCOM; 699 if ((cy->cy_tpb.tpstatus&CYS_RDY) == 0) { 700 yc->yc_timo = 5*60; /* 5 minutes */ 701 return; 702 } 703 } 704 /* 705 * An operation completed...record status. 706 */ 707 yc->yc_timo = INF; 708 yc->yc_control = cy->cy_tpb.tpcontrol; 709 yc->yc_status = cy->cy_tpb.tpstatus; 710 yc->yc_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount); 711 dlog((LOG_INFO, "cmd %x control %b status %b resid %d\n", 712 cy->cy_tpb.tpcmd, yc->yc_control, CYCW_BITS, 713 yc->yc_status, CYS_BITS, yc->yc_resid)); 714 if ((bp->b_flags&B_READ) == 0) 715 yc->yc_lastiow = 1; 716 state = vm->um_tab.b_active; 717 vm->um_tab.b_active = 0; 718 /* 719 * Check for errors. 720 */ 721 if (cy->cy_tpb.tpstatus&CYS_ERR) { 722 err = cy->cy_tpb.tpstatus&CYS_ERR; 723 dlog((LOG_INFO, "error %d\n", err)); 724 /* 725 * If we hit the end of tape file, update our position. 726 */ 727 if (err == CYER_FM) { 728 yc->yc_status |= CYS_FM; 729 state = SCOM; /* force completion */ 730 cyseteof(bp); /* set blkno and nxrec */ 731 goto opdone; 732 } 733 /* 734 * Fix up errors which occur due to backspacing over 735 * the beginning of the tape. 736 */ 737 if (err == CYER_BOT && cy->cy_tpb.tpcontrol&CYCW_REV) { 738 yc->yc_status |= CYS_BOT; 739 goto ignoreerr; 740 } 741 /* 742 * If we were reading raw tape and the only error was that the 743 * record was too long, then we don't consider this an error. 744 */ 745 if (bp == &rcybuf[cyunit] && (bp->b_flags&B_READ) && 746 err == CYER_STROBE) { 747 /* 748 * Retry reads with the command changed to 749 * a raw read if necessary. Setting b_errcnt 750 * here causes cystart (above) to force a CY_RCOM. 751 */ 752 if (htoms(cy->cy_tpb.tprec) > cy->cy_bs && 753 bp->b_bcount > cy->cy_bs && 754 yc->yc_blksize <= cy->cy_bs && 755 vm->um_tab.b_errcnt++ == 0) { 756 yc->yc_blkno++; 757 goto opcont; 758 } else 759 goto ignoreerr; 760 } 761 /* 762 * If error is not hard, and this was an i/o operation 763 * retry up to 8 times. 764 */ 765 if (((1<<err)&CYER_SOFT) && state == SIO) { 766 if (++vm->um_tab.b_errcnt < 7) { 767 yc->yc_blkno++; 768 goto opcont; 769 } 770 } else 771 /* 772 * Hard or non-i/o errors on non-raw tape 773 * cause it to close. 774 */ 775 if (yc->yc_openf > 0 && bp != &rcybuf[cyunit]) 776 yc->yc_openf = -1; 777 /* 778 * Couldn't recover from error. 779 */ 780 tprintf(yc->yc_ttyp, 781 "yc%d: hard error bn%d status=%b, %s\n", YCUNIT(bp->b_dev), 782 bp->b_blkno, yc->yc_status, CYS_BITS, 783 (err < NCYERROR) ? cyerror[err] : ""); 784 bp->b_flags |= B_ERROR; 785 goto opdone; 786 } 787 /* 788 * Advance tape control FSM. 789 */ 790 ignoreerr: 791 /* 792 * If we hit a tape mark update our position. 793 */ 794 if (yc->yc_status&CYS_FM && bp->b_flags&B_READ) { 795 cyseteof(bp); 796 goto opdone; 797 } 798 switch (state) { 799 800 case SIO: 801 /* 802 * Read/write increments tape block number. 803 */ 804 yc->yc_blkno++; 805 yc->yc_blks++; 806 if (vm->um_tab.b_errcnt || yc->yc_status & CYS_CR) 807 yc->yc_softerrs++; 808 yc->yc_blksize = htoms(cy->cy_tpb.tpcount); 809 dlog((LOG_ERR, "blocksize %d", yc->yc_blksize)); 810 goto opdone; 811 812 case SCOM: 813 /* 814 * For forward/backward space record update current position. 815 */ 816 if (bp == &ccybuf[CYUNIT(bp->b_dev)]) 817 switch ((int)bp->b_command) { 818 819 case CY_SFORW: 820 yc->yc_blkno -= bp->b_repcnt; 821 break; 822 823 case CY_SREV: 824 yc->yc_blkno += bp->b_repcnt; 825 break; 826 } 827 goto opdone; 828 829 case SSEEK: 830 yc->yc_blkno = bp->b_blkno; 831 goto opcont; 832 833 case SERASE: 834 /* 835 * Completed erase of the inter-record gap due to a 836 * write error; now retry the write operation. 837 */ 838 vm->um_tab.b_active = SERASED; 839 goto opcont; 840 } 841 842 opdone: 843 /* 844 * Reset error count and remove from device queue. 845 */ 846 vm->um_tab.b_errcnt = 0; 847 dp->b_actf = bp->av_forw; 848 /* 849 * Save resid and release resources. 850 */ 851 bp->b_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount); 852 if (bp != &ccybuf[cyunit]) 853 vbadone(bp, &cy->cy_rbuf); 854 biodone(bp); 855 /* 856 * Circulate slave to end of controller 857 * queue to give other slaves a chance. 858 */ 859 vm->um_tab.b_actf = dp->b_forw; 860 if (dp->b_actf) { 861 dp->b_forw = NULL; 862 if (vm->um_tab.b_actf == NULL) 863 vm->um_tab.b_actf = dp; 864 else 865 vm->um_tab.b_actl->b_forw = dp; 866 } 867 if (vm->um_tab.b_actf == 0) 868 return; 869 opcont: 870 cystart(vm); 871 } 872 873 cytimer(dev) 874 int dev; 875 { 876 register struct yc_softc *yc = &yc_softc[YCUNIT(dev)]; 877 int s; 878 879 if (yc->yc_openf == 0 && yc->yc_timo == INF) { 880 yc->yc_tact = 0; 881 return; 882 } 883 if (yc->yc_timo != INF && (yc->yc_timo -= 5) < 0) { 884 printf("yc%d: lost interrupt\n", YCUNIT(dev)); 885 yc->yc_timo = INF; 886 s = spl3(); 887 cyintr(CYUNIT(dev)); 888 splx(s); 889 } 890 timeout(cytimer, (caddr_t)dev, 5*hz); 891 } 892 893 cyseteof(bp) 894 register struct buf *bp; 895 { 896 register int cyunit = CYUNIT(bp->b_dev); 897 register struct cy_softc *cy = &cy_softc[cyunit]; 898 register struct yc_softc *yc = &yc_softc[YCUNIT(bp->b_dev)]; 899 900 if (bp == &ccybuf[cyunit]) { 901 if (yc->yc_blkno > bp->b_blkno) { 902 /* reversing */ 903 yc->yc_nxrec = bp->b_blkno - htoms(cy->cy_tpb.tpcount); 904 yc->yc_blkno = yc->yc_nxrec; 905 } else { 906 yc->yc_blkno = bp->b_blkno + htoms(cy->cy_tpb.tpcount); 907 yc->yc_nxrec = yc->yc_blkno - 1; 908 } 909 return; 910 } 911 /* eof on read */ 912 yc->yc_nxrec = bp->b_blkno; 913 } 914 915 cyread(dev, uio) 916 dev_t dev; 917 struct uio *uio; 918 { 919 int errno; 920 921 errno = cyphys(dev, uio); 922 if (errno) 923 return (errno); 924 return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_READ, minphys, uio)); 925 } 926 927 cywrite(dev, uio) 928 dev_t dev; 929 struct uio *uio; 930 { 931 int errno; 932 933 errno = cyphys(dev, uio); 934 if (errno) 935 return (errno); 936 return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_WRITE, minphys, uio)); 937 } 938 939 /* 940 * Check that a raw device exits. 941 * If it does, set up the yc_blkno and yc_nxrec 942 * so that the tape will appear positioned correctly. 943 */ 944 cyphys(dev, uio) 945 dev_t dev; 946 struct uio *uio; 947 { 948 register int ycunit = YCUNIT(dev); 949 register daddr_t a; 950 register struct yc_softc *yc; 951 register struct vba_device *vi; 952 953 if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0) 954 return (ENXIO); 955 yc = &yc_softc[ycunit]; 956 a = uio->uio_offset >> DEV_BSHIFT; 957 yc->yc_blkno = a; 958 yc->yc_nxrec = a + 1; 959 return (0); 960 } 961 962 /*ARGSUSED*/ 963 cyioctl(dev, cmd, data, flag) 964 caddr_t data; 965 dev_t dev; 966 { 967 int ycunit = YCUNIT(dev); 968 register struct yc_softc *yc = &yc_softc[ycunit]; 969 register struct buf *bp = &ccybuf[CYUNIT(dev)]; 970 register callcount; 971 int fcount, op; 972 struct mtop *mtop; 973 struct mtget *mtget; 974 /* we depend of the values and order of the MT codes here */ 975 static cyops[] = 976 {CY_WEOF,CY_FSF,CY_BSF,CY_SFORW,CY_SREV,CY_REW,CY_OFFL,CY_SENSE}; 977 978 switch (cmd) { 979 980 case MTIOCTOP: /* tape operation */ 981 mtop = (struct mtop *)data; 982 switch (op = mtop->mt_op) { 983 984 case MTWEOF: 985 callcount = mtop->mt_count; 986 fcount = 1; 987 break; 988 989 case MTFSR: case MTBSR: 990 callcount = 1; 991 fcount = mtop->mt_count; 992 break; 993 994 case MTFSF: case MTBSF: 995 callcount = mtop->mt_count; 996 fcount = 1; 997 break; 998 999 case MTREW: case MTOFFL: case MTNOP: 1000 callcount = 1; 1001 fcount = 1; 1002 break; 1003 1004 default: 1005 return (ENXIO); 1006 } 1007 if (callcount <= 0 || fcount <= 0) 1008 return (EINVAL); 1009 while (--callcount >= 0) { 1010 #ifdef notdef 1011 /* 1012 * Gagh, this controller is the pits... 1013 */ 1014 if (op == MTFSF || op == MTBSF) { 1015 do 1016 cycommand(dev, cyops[op], 1); 1017 while ((bp->b_flags&B_ERROR) == 0 && 1018 (yc->yc_status&(CYS_EOT|CYS_BOT|CYS_FM)) == 0); 1019 } else 1020 #endif 1021 cycommand(dev, cyops[op], fcount); 1022 dlog((LOG_INFO, 1023 "cyioctl: status %x, b_flags %x, resid %d\n", 1024 yc->yc_status, bp->b_flags, bp->b_resid)); 1025 if ((bp->b_flags&B_ERROR) || 1026 (yc->yc_status&(CYS_BOT|CYS_EOT))) 1027 break; 1028 } 1029 bp->b_resid = callcount + 1; 1030 return (geterror(bp)); 1031 1032 case MTIOCGET: 1033 cycommand(dev, CY_SENSE, 1); 1034 mtget = (struct mtget *)data; 1035 mtget->mt_dsreg = yc->yc_status; 1036 mtget->mt_erreg = yc->yc_control; 1037 mtget->mt_resid = yc->yc_resid; 1038 mtget->mt_type = MT_ISCY; 1039 break; 1040 1041 default: 1042 return (ENXIO); 1043 } 1044 return (0); 1045 } 1046 1047 /* 1048 * Poll until the controller is ready. 1049 */ 1050 cywait(cp) 1051 register struct cyccb *cp; 1052 { 1053 register int i = 5000; 1054 1055 uncache(&cp->cbgate); 1056 while (i-- > 0 && cp->cbgate == GATE_CLOSED) { 1057 DELAY(1000); 1058 uncache(&cp->cbgate); 1059 } 1060 return (i <= 0); 1061 } 1062 1063 /* 1064 * Load a 20 bit pointer into a Tapemaster pointer. 1065 */ 1066 cyldmba(reg, value) 1067 register caddr_t reg; 1068 caddr_t value; 1069 { 1070 register int v = (int)value; 1071 1072 *reg++ = v; 1073 *reg++ = v >> 8; 1074 *reg++ = 0; 1075 *reg = (v&0xf0000) >> 12; 1076 } 1077 1078 /* 1079 * Unconditionally reset all controllers to their initial state. 1080 */ 1081 cyreset(vba) 1082 int vba; 1083 { 1084 register caddr_t addr; 1085 register int ctlr; 1086 1087 for (ctlr = 0; ctlr < NCY; ctlr++) 1088 if (cyminfo[ctlr] && cyminfo[ctlr]->um_vbanum == vba) { 1089 addr = cyminfo[ctlr]->um_addr; 1090 CY_RESET(addr); 1091 if (!cyinit(ctlr, addr)) { 1092 printf("cy%d: reset failed\n", ctlr); 1093 cyminfo[ctlr] = NULL; 1094 } 1095 } 1096 } 1097 1098 cyuncachetpb(cy) 1099 struct cy_softc *cy; 1100 { 1101 register long *lp = (long *)&cy->cy_tpb; 1102 register int i; 1103 1104 for (i = 0; i < howmany(sizeof (struct cytpb), sizeof (long)); i++) 1105 uncache(lp++); 1106 } 1107 1108 /* 1109 * Dump routine. 1110 */ 1111 cydump(dev) 1112 dev_t dev; 1113 { 1114 register struct cy_softc *cy; 1115 register int bs, num, start; 1116 register caddr_t addr; 1117 int unit = CYUNIT(dev), error; 1118 1119 if (unit >= NCY || cyminfo[unit] == 0 || 1120 (cy = &cy_softc[unit])->cy_bs == 0 || YCUNIT(dev) >= NYC) 1121 return (ENXIO); 1122 if (cywait(&cy->cy_ccb)) 1123 return (EFAULT); 1124 #define phys(a) ((caddr_t)((int)(a)&~0xc0000000)) 1125 addr = phys(cyminfo[unit]->um_addr); 1126 num = maxfree, start = NBPG*2; 1127 while (num > 0) { 1128 bs = num > btoc(CYMAXIO) ? btoc(CYMAXIO) : num; 1129 error = cydwrite(cy, start, bs, addr); 1130 if (error) 1131 return (error); 1132 start += bs, num -= bs; 1133 } 1134 cyweof(cy, addr); 1135 cyweof(cy, addr); 1136 uncache(&cy->cy_tpb); 1137 if (cy->cy_tpb.tpstatus&CYS_ERR) 1138 return (EIO); 1139 cyrewind(cy, addr); 1140 return (0); 1141 } 1142 1143 cydwrite(cy, pf, npf, addr) 1144 register struct cy_softc *cy; 1145 int pf, npf; 1146 caddr_t addr; 1147 { 1148 1149 cy->cy_tpb.tpcmd = CY_WCOM; 1150 cy->cy_tpb.tpcontrol = CYCW_LOCK|CYCW_25IPS|CYCW_16BITS; 1151 cy->cy_tpb.tpstatus = 0; 1152 cy->cy_tpb.tpsize = htoms(npf*NBPG); 1153 cyldmba(cy->cy_tpb.tplink, (caddr_t)0); 1154 cyldmba(cy->cy_tpb.tpdata, (caddr_t)(pf*NBPG)); 1155 cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb); 1156 cy->cy_ccb.cbgate = GATE_CLOSED; 1157 CY_GO(addr); 1158 if (cywait(&cy->cy_ccb)) 1159 return (EFAULT); 1160 uncache(&cy->cy_tpb); 1161 if (cy->cy_tpb.tpstatus&CYS_ERR) 1162 return (EIO); 1163 return (0); 1164 } 1165 1166 cyweof(cy, addr) 1167 register struct cy_softc *cy; 1168 caddr_t addr; 1169 { 1170 1171 cy->cy_tpb.tpcmd = CY_WEOF; 1172 cy->cy_tpb.tpcount = htoms(1); 1173 cy->cy_ccb.cbgate = GATE_CLOSED; 1174 CY_GO(addr); 1175 (void) cywait(&cy->cy_ccb); 1176 } 1177 1178 cyrewind(cy, addr) 1179 register struct cy_softc *cy; 1180 caddr_t addr; 1181 { 1182 1183 cy->cy_tpb.tpcmd = CY_REW; 1184 cy->cy_tpb.tpcount = htoms(1); 1185 cy->cy_ccb.cbgate = GATE_CLOSED; 1186 CY_GO(addr); 1187 (void) cywait(&cy->cy_ccb); 1188 } 1189 #endif 1190