1: /* 2: * SCCS id @(#)tm.c 2.1 (Berkeley) 8/5/83 3: */ 4: 5: #include "tm.h" 6: #if NTM > 0 7: #include "param.h" 8: #include <sys/buf.h> 9: #include <sys/dir.h> 10: #include <sys/conf.h> 11: #include <sys/file.h> 12: #include <sys/user.h> 13: #include <sys/systm.h> 14: #include <sys/tmreg.h> 15: #ifdef TM_IOCTL 16: #include <sys/mtio.h> 17: #endif 18: 19: struct tmdevice *TMADDR; 20: 21: struct buf tmtab; 22: struct buf ctmbuf; 23: /* 24: * Raw tape operations use rtmbuf. The driver 25: * notices when rtmbuf is being used and allows the user 26: * program to continue after errors and read records 27: * not of the standard length (BSIZE). 28: */ 29: struct buf rtmbuf; 30: 31: /* 32: * Software state per tape transport: 33: * 34: * 1. A tape drive is a unique-open device: we refuse opens when it is already. 35: * 2. We keep track of the current position on a block tape and seek 36: * before operations by forward/back spacing if necessary. 37: * 3. We remember if the last operation was a write on a tape, so if a tape 38: * is open read write and the last thing done is a write we can 39: * write a standard end of tape mark (two eofs). 40: * 4. We remember the status registers after the last command, using 41: * them internally and returning them to the SENSE ioctl. 42: * 5. We remember the last density the tape was used at. If it is 43: * not a BOT when we start using it and we are writing, we don't 44: * let the density be changed. 45: */ 46: 47: struct te_softc { 48: char sc_openf; 49: char sc_lastiow; 50: daddr_t sc_blkno; 51: daddr_t sc_nxrec; 52: u_short sc_erreg; 53: #ifdef TM_IOCTL 54: u_short sc_dsreg; 55: short sc_resid; 56: #endif 57: u_short sc_dens; 58: #ifdef TM_TIMEOUT 59: short sc_timo; 60: short sc_tact; 61: #endif 62: } te_softc[NTM]; 63: 64: 65: /* 66: * States for tmtab.b_active, the state flag. 67: * This is used to sequence control in the driver. 68: */ 69: #define SSEEK 1 70: #define SIO 2 71: #define SCOM 3 72: #define SREW 4 73: 74: #define TEUNIT(dev) (minor(dev) & 077) 75: 76: /* bits in minor device */ 77: #define T_1600BPI 0100 78: #define T_NOREWIND 0200 79: 80: #define INF 32760 81: 82: #ifdef TM_TIMEOUT 83: int tmtimer (); 84: #endif 85: 86: tmattach(addr, unit) 87: struct tmdevice *addr; 88: { 89: /* 90: * This driver supports only one controller. 91: */ 92: if (unit == 0) { 93: TMADDR = addr; 94: return(1); 95: } 96: return(0); 97: } 98: 99: /* 100: * Open the device. Tapes are unique open 101: * devices so we refuse if it is alredy open. 102: * We also check that the tape is available and 103: * don't block waiting here: if you want to wait 104: * for a tape you should timeout in user code. 105: */ 106: tmopen(dev, flag) 107: register dev_t dev; 108: { 109: int s; 110: u_short olddens, dens; 111: register teunit = TEUNIT(dev); 112: register struct te_softc *sc = &te_softc[teunit]; 113: 114: if (teunit >= NTM || TMADDR == (struct tmdevice *) NULL) { 115: u.u_error = ENXIO; 116: return; 117: } 118: else 119: if (sc->sc_openf) { 120: u.u_error = EBUSY; 121: return; 122: } 123: olddens = sc->sc_dens; 124: #ifdef DDMT 125: dens = TM_IE | TM_GO | (teunit << 8); 126: if ((minor(dev) & T_1600BPI) == 0) 127: dens |= TM_D800; 128: #else 129: dens = TM_IE | TM_GO | TM_D800 | (teunit << 8); 130: #endif 131: sc->sc_dens = dens; 132: 133: tmtab.b_flags |= B_TAPE; 134: get: 135: tmcommand(dev, TM_SENSE, 1); 136: if (sc->sc_erreg & TMER_SDWN) { 137: sleep ((caddr_t) &lbolt, PZERO+1); 138: goto get; 139: } 140: sc->sc_dens = olddens; 141: 142: if ((sc->sc_erreg & (TMER_SELR | TMER_TUR)) != (TMER_SELR | TMER_TUR)) { 143: uprintf("te%d: not online\n", teunit); 144: u.u_error = EIO; 145: return; 146: } 147: if ((flag & FWRITE) && (sc->sc_erreg & TMER_WRL)) { 148: uprintf("te%d: no write ring\n", teunit); 149: u.u_error = EIO; 150: return; 151: } 152: #ifdef DDMT 153: if ((sc->sc_erreg & TMER_BOT) == 0 && (flag & FWRITE) && dens != sc->sc_dens) { 154: uprintf("te%d: can't change density in mid-tape\n", teunit); 155: u.u_error = EIO; 156: return; 157: } 158: #endif 159: sc->sc_openf = 1; 160: sc->sc_blkno = (daddr_t) 0; 161: sc->sc_nxrec = (daddr_t) 65535; 162: sc->sc_lastiow = 0; 163: sc->sc_dens = dens; 164: #ifdef TM_TIMEOUT 165: s = spl6(); 166: if (sc->sc_tact == 0) { 167: sc->sc_timo = INF; 168: sc->sc_tact = 1; 169: timeout(tmtimer, (caddr_t) dev, 5 * hz); 170: } 171: splx(s); 172: #endif 173: } 174: 175: /* 176: * Close tape device. 177: * 178: * If tape was open for writing or last operation was 179: * a write, then write two EOF's and backspace over the last one. 180: * Unless this is a non-rewinding special file, rewind the tape. 181: * Make the tape available to others. 182: */ 183: tmclose(dev, flag) 184: register dev_t dev; 185: register flag; 186: { 187: register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 188: 189: if (flag == FWRITE || (flag & FWRITE) && sc->sc_lastiow) { 190: tmcommand(dev, TM_WEOF, 1); 191: tmcommand(dev, TM_WEOF, 1); 192: tmcommand(dev, TM_SREV, 1); 193: } 194: if ((minor(dev) & T_NOREWIND) == 0) 195: /* 196: * 0 count means don't hang waiting for rewind complete. 197: * Rather ctmbuf stays busy until the operation completes 198: * preventing further opens from completing by 199: * preventing a TM_SENSE from completing. 200: */ 201: tmcommand(dev, TM_REW, 0); 202: sc->sc_openf = 0; 203: } 204: 205: /* 206: * Execute a command on the tape drive 207: * a specified number of times. 208: */ 209: tmcommand(dev, com, count) 210: register dev_t dev; 211: register u_short count; 212: { 213: int s; 214: register struct buf *bp; 215: 216: bp = &ctmbuf; 217: s = spl5(); 218: while (bp->b_flags & B_BUSY) { 219: /* 220: * This special check is because B_BUSY never 221: * gets cleared in the non-waiting rewind case. 222: */ 223: if (bp->b_repcnt == 0 && (bp->b_flags & B_DONE)) 224: break; 225: bp->b_flags |= B_WANTED; 226: sleep((caddr_t)bp, PRIBIO); 227: } 228: bp->b_flags = B_BUSY | B_READ; 229: splx(s); 230: bp->b_dev = dev; 231: bp->b_repcnt = -count; 232: bp->b_command = com; 233: bp->b_blkno = (daddr_t) 0; 234: tmstrategy(bp); 235: /* 236: * In case of rewind from close, don't wait. 237: * This is the only case where count can be 0. 238: */ 239: if (count == 0) 240: return; 241: iowait(bp); 242: if (bp->b_flags & B_WANTED) 243: wakeup((caddr_t)bp); 244: bp->b_flags &= B_ERROR; 245: } 246: 247: /* 248: * Queue a tape operation. 249: */ 250: tmstrategy(bp) 251: register struct buf *bp; 252: { 253: register s; 254: 255: #ifdef UNIBUS_MAP 256: if (bp != &ctmbuf) 257: mapalloc(bp); 258: #endif 259: bp->av_forw = NULL; 260: s = spl5(); 261: if (tmtab.b_actf == NULL) 262: tmtab.b_actf = bp; 263: else 264: tmtab.b_actl->av_forw = bp; 265: tmtab.b_actl = bp; 266: /* 267: * If the controller is not busy, get 268: * it going. 269: */ 270: if (tmtab.b_active == 0) 271: tmstart(); 272: splx(s); 273: } 274: 275: tmstart() 276: { 277: daddr_t blkno; 278: int cmd, teunit; 279: register struct tmdevice *tmaddr = TMADDR; 280: register struct buf *bp; 281: register struct te_softc *sc; 282: 283: loop: 284: if ((bp = tmtab.b_actf) == NULL) 285: return; 286: teunit = TEUNIT(bp->b_dev); 287: /* 288: * Record pre-transfer status (e.g. for TM_SENSE). 289: */ 290: sc = &te_softc[teunit]; 291: tmaddr->tmcs = teunit << 8; 292: sc->sc_erreg = tmaddr->tmer; 293: #ifdef TM_IOCTL 294: sc->sc_dsreg = tmaddr->tmcs; 295: sc->sc_resid = tmaddr->tmbc; 296: #endif 297: /* 298: * Default is that the last command was NOT a write command; 299: * if we do a write command we will notice this in tmintr(). 300: */ 301: sc->sc_lastiow = 0; 302: if (sc->sc_openf < 0 || (tmaddr->tmcs & TM_CUR) == 0) { 303: /* 304: * Have had a hard error on a non-raw tape 305: * or the tape unit is now unavailable 306: * (e.g. taken off line). 307: */ 308: bp->b_flags |= B_ERROR; 309: goto next; 310: } 311: if (bp == &ctmbuf) { 312: /* 313: * Execute control operation with the specified count. 314: */ 315: if (bp->b_command == TM_SENSE) { 316: goto next; 317: } 318: #ifdef TM_TIMEOUT 319: /* 320: * Set next state; give 5 minutes to complete 321: * rewind or 10 seconds per iteration (minimum 60 322: * seconds and max 5 minutes) to complete other ops. 323: */ 324: #else 325: /* 326: * Set next state. 327: */ 328: #endif 329: if (bp->b_command == TM_REW) { 330: tmtab.b_active = SREW; 331: #ifdef TM_TIMEOUT 332: sc->sc_timo = 5 * 60; 333: #endif 334: } else { 335: tmtab.b_active = SCOM; 336: #ifdef TM_TIMEOUT 337: sc->sc_timo = MIN(MAX(10 * bp->b_repcnt, 60), 5 * 60); 338: #endif 339: } 340: if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 341: tmaddr->tmbc = bp->b_repcnt; 342: goto dobpcmd; 343: } 344: /* 345: * The following checks handle boundary cases for operation 346: * on non-raw tapes. On raw tapes the initialization of 347: * sc->sc_nxrec by tmphys causes them to be skipped normally 348: * (except in the case of retries). 349: */ 350: if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) { 351: /* 352: * Can't read past known end-of-file. 353: */ 354: bp->b_flags |= B_ERROR; 355: bp->b_error = ENXIO; 356: goto next; 357: } 358: if (dbtofsb(bp->b_blkno) == sc->sc_nxrec && bp->b_flags & B_READ) { 359: /* 360: * Reading at end of file returns 0 bytes. 361: * Buffer will be cleared (if written) in writei. 362: */ 363: bp->b_resid = bp->b_bcount; 364: goto next; 365: } 366: if ((bp->b_flags & B_READ) == 0) 367: /* 368: * Writing sets EOF. 369: */ 370: sc->sc_nxrec = dbtofsb(bp->b_blkno) + (daddr_t) 1; 371: /* 372: * If the data transfer command is in the correct place, 373: * set up all registers and do the transfer. 374: */ 375: if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) { 376: tmaddr->tmbc = -bp->b_bcount; 377: if ((bp->b_flags & B_READ) == 0) { 378: if (tmtab.b_errcnt) 379: cmd = TM_WIRG; 380: else 381: cmd = TM_WCOM; 382: } else 383: cmd = TM_RCOM; 384: tmtab.b_active = SIO; 385: tmaddr->tmba = bp->b_un.b_addr; 386: cmd = sc->sc_dens | ((bp->b_xmem & 03) << 4) | cmd; 387: #ifdef TM_TIMEOUT 388: sc->sc_timo = 60; /* premature, but should serve */ 389: #endif 390: tmaddr->tmcs = cmd; 391: return; 392: } 393: /* 394: * Tape positioned incorrectly; 395: * set to seek forward or backward to the correct spot. 396: * This happens for raw tapes only on error retries. 397: */ 398: tmtab.b_active = SSEEK; 399: if (blkno < dbtofsb(bp->b_blkno)) { 400: bp->b_command = TM_SFORW; 401: tmaddr->tmbc = (short) (blkno - dbtofsb(bp->b_blkno)); 402: } else { 403: bp->b_command = TM_SREV; 404: tmaddr->tmbc = (short) (dbtofsb(bp->b_blkno) - blkno); 405: } 406: #ifdef TM_TIMEOUT 407: sc->sc_timo = MIN(MAX(10 * -tmaddr->tmbc, 60), 5 * 60); 408: #endif 409: 410: dobpcmd: 411: /* 412: * Do the command in bp. 413: */ 414: tmaddr->tmcs = (sc->sc_dens | bp->b_command); 415: return; 416: 417: next: 418: /* 419: * Done with this operation due to error or 420: * the fact that it doesn't do anything. 421: * Dequeue the transfer and continue processing. 422: */ 423: tmtab.b_errcnt = 0; 424: tmtab.b_actf = bp->av_forw; 425: iodone(bp); 426: goto loop; 427: } 428: 429: /* 430: * The interrupt routine. 431: */ 432: tmintr() 433: { 434: register struct tmdevice *tmaddr = TMADDR; 435: register struct buf *bp; 436: int teunit; 437: int state; 438: register struct te_softc *sc; 439: 440: if ((bp = tmtab.b_actf) == NULL) 441: return; 442: teunit = TEUNIT(bp->b_dev); 443: sc = &te_softc[teunit]; 444: /* 445: * If last command was a rewind, and tape is still 446: * rewinding, wait for the rewind complete interrupt. 447: */ 448: if (tmtab.b_active == SREW) { 449: tmtab.b_active = SCOM; 450: if (tmaddr->tmer & TMER_RWS) { 451: #ifdef TM_TIMEOUT 452: sc->sc_timo = 5 * 60; /* 5 minutes */ 453: #endif 454: return; 455: } 456: } 457: /* 458: * An operation completed... record status. 459: */ 460: #ifdef TM_TIMEOUT 461: sc->sc_timo = INF; 462: #endif 463: sc->sc_erreg = tmaddr->tmer; 464: #ifdef TM_IOCTL 465: sc->sc_dsreg = tmaddr->tmcs; 466: sc->sc_resid = tmaddr->tmbc; 467: #endif 468: if ((bp->b_flags & B_READ) == 0) 469: sc->sc_lastiow = 1; 470: state = tmtab.b_active; 471: tmtab.b_active = 0; 472: /* 473: * Check for errors. 474: */ 475: if (tmaddr->tmcs & TM_ERR) { 476: while(tmaddr->tmer & TMER_SDWN) 477: ; /* await settle down */ 478: /* 479: * If we hit the end of the tape file, update our position. 480: */ 481: if (tmaddr->tmer & TMER_EOF) { 482: tmseteof(bp); /* set blkno and nxrec */ 483: state = SCOM; /* force completion */ 484: /* 485: * Stuff bc so it will be unstuffed correctly 486: * later to get resid. 487: */ 488: tmaddr->tmbc = -bp->b_bcount; 489: goto opdone; 490: } 491: /* 492: * If we were reading raw tape and the only error was that the 493: * record was too long, then we don't consider this an error. 494: */ 495: if (bp == &rtmbuf && (bp->b_flags & B_READ) && 496: (tmaddr->tmer & (TMER_HARD | TMER_SOFT)) == TMER_RLE) 497: goto ignoreerr; 498: /* 499: * If error is not hard, and this was an i/o operation 500: * retry up to 8 times. 501: */ 502: if ((tmaddr->tmer & TMER_HARD) == 0 && state == SIO) { 503: if (++tmtab.b_errcnt < 7) { 504: sc->sc_blkno++; 505: goto opcont; 506: } 507: } else 508: /* 509: * Hard or non-i/o errors on non-raw tape 510: * cause it to close. 511: */ 512: if (sc->sc_openf > 0 && bp != &rtmbuf) 513: sc->sc_openf = -1; 514: /* 515: * Couldn't recover error 516: */ 517: #ifdef UCB_DEVERR 518: printf("te%d: hard error bn%D er=%b\n", 519: teunit, bp->b_blkno, sc->sc_erreg, TMER_BITS); 520: #else 521: deverror(bp, sc->sc_erreg, 0); 522: #endif 523: bp->b_flags |= B_ERROR; 524: goto opdone; 525: } 526: /* 527: * Advance tape control finite state machine. 528: */ 529: ignoreerr: 530: switch (state) { 531: case SIO: 532: /* 533: * Read/write increments tape block number. 534: */ 535: sc->sc_blkno++; 536: goto opdone; 537: case SCOM: 538: /* 539: * For forward/backward space record update current position. 540: */ 541: if (bp == &ctmbuf) 542: switch (bp->b_command) { 543: case TM_SFORW: 544: sc->sc_blkno -= (daddr_t) (bp->b_repcnt); 545: break; 546: case TM_SREV: 547: sc->sc_blkno += (daddr_t) (bp->b_repcnt); 548: break; 549: } 550: goto opdone; 551: case SSEEK: 552: sc->sc_blkno = dbtofsb(bp->b_blkno); 553: goto opcont; 554: default: 555: panic("tmintr"); 556: } 557: opdone: 558: /* 559: * Reset error count and remove 560: * from device queue. 561: */ 562: tmtab.b_errcnt = 0; 563: tmtab.b_actf = bp->av_forw; 564: bp->b_resid = -tmaddr->tmbc; 565: iodone(bp); 566: 567: opcont: 568: tmstart(); 569: } 570: 571: #ifdef TM_TIMEOUT 572: tmtimer(dev) 573: register dev_t dev; 574: { 575: register s; 576: register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 577: 578: if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 579: printf("te%d: lost interrupt\n", TEUNIT(dev)); 580: sc->sc_timo = INF; 581: s = spl5(); 582: tmintr(); 583: splx(s); 584: } 585: timeout(tmtimer, (caddr_t) dev, 5 * hz); 586: } 587: #endif 588: 589: tmseteof(bp) 590: register struct buf *bp; 591: { 592: register struct tmdevice *tmaddr = TMADDR; 593: daddr_t bn = dbtofsb(bp->b_blkno); 594: register struct te_softc *sc = &te_softc[TEUNIT(bp->b_dev)]; 595: 596: if (bp == &ctmbuf) { 597: if (sc->sc_blkno > bn) { 598: /* reversing */ 599: sc->sc_nxrec = bn - (daddr_t) (tmaddr->tmbc); 600: sc->sc_blkno = sc->sc_nxrec; 601: } else { 602: /* spacing forward */ 603: sc->sc_blkno = bn + (daddr_t) (tmaddr->tmbc); 604: sc->sc_nxrec = sc->sc_blkno - (daddr_t) 1; 605: } 606: return; 607: } 608: /* eof on read */ 609: sc->sc_nxrec = bn; 610: } 611: 612: tmread(dev) 613: register dev_t dev; 614: { 615: tmphys(dev); 616: bphysio(tmstrategy, &rtmbuf, dev, B_READ); 617: } 618: 619: tmwrite(dev) 620: register dev_t dev; 621: { 622: tmphys(dev); 623: bphysio(tmstrategy, &rtmbuf, dev, B_WRITE); 624: } 625: 626: /* 627: * Set up sc_blkno and sc_nxrec 628: * so that the tape will appear positioned correctly. 629: */ 630: tmphys(dev) 631: dev_t dev; 632: { 633: daddr_t a; 634: register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 635: 636: a = dbtofsb(u.u_offset >> 9); 637: sc->sc_blkno = a; 638: sc->sc_nxrec = a + 1; 639: } 640: 641: #ifdef TM_IOCTL 642: /*ARGSUSED*/ 643: tmioctl(dev, cmd, addr, flag) 644: dev_t dev; 645: caddr_t addr; 646: { 647: register struct buf *bp = &ctmbuf; 648: register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 649: register callcount; 650: u_short fcount; 651: struct mtop mtop; 652: struct mtget mtget; 653: /* we depend on the values and order of the MT codes here */ 654: static tmops[] = {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 655: 656: switch (cmd) { 657: 658: case MTIOCTOP: /* tape operation */ 659: if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) { 660: u.u_error = EFAULT; 661: return; 662: } 663: switch(mtop.mt_op) { 664: case MTWEOF: 665: callcount = mtop.mt_count; 666: fcount = 1; 667: break; 668: case MTFSF: 669: case MTBSF: 670: callcount = mtop.mt_count; 671: fcount = INF; 672: break; 673: case MTFSR: 674: case MTBSR: 675: callcount = 1; 676: fcount = mtop.mt_count; 677: break; 678: case MTREW: 679: case MTOFFL: 680: case MTNOP: 681: callcount = 1; 682: fcount = 1; 683: break; 684: default: 685: u.u_error = ENXIO; 686: return; 687: } 688: if (callcount <= 0 || fcount <= 0) { 689: u.u_error = ENXIO; 690: return; 691: } 692: while (--callcount >= 0) { 693: tmcommand(dev, tmops[mtop.mt_op], fcount); 694: if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) && 695: bp->b_resid) { 696: u.u_error = EIO; 697: break; 698: } 699: if ((bp->b_flags & B_ERROR) || sc->sc_erreg & TMER_BOT) 700: break; 701: } 702: geterror(bp); 703: return; 704: case MTIOCGET: 705: mtget.mt_erreg = sc->sc_erreg; 706: mtget.mt_dsreg = sc->sc_dsreg; 707: mtget.mt_resid = sc->sc_resid; 708: mtget.mt_type = MT_ISTM; 709: if (copyout((caddr_t)&mtget, addr, sizeof(mtget))) 710: u.u_error = EFAULT; 711: return; 712: default: 713: u.u_error = ENXIO; 714: } 715: } 716: #endif 717: #endif NTM