1*3711Sroot /* ts.c 4.15 81/05/09 */ 21900Swnj 31941Swnj #include "ts.h" 43528Swnj #include "te.h" 51900Swnj #if NTS > 0 63647Swnj #if TSDEBUG 73327Swnj #define printd if(tsdebug)printf 83327Swnj int tsdebug; 93647Swnj #endif 101900Swnj /* 111900Swnj * TS11 tape driver 123244Swnj * 133244Swnj * TODO: 143244Swnj * test driver with more than one controller 153244Swnj * test reset code 163244Swnj * test dump code 173244Swnj * test rewinds without hanging in driver 183244Swnj * what happens if you offline tape during rewind? 193244Swnj * test using file system on tape 201900Swnj */ 211900Swnj #include "../h/param.h" 221900Swnj #include "../h/systm.h" 231900Swnj #include "../h/buf.h" 243244Swnj #include "../h/dir.h" 251900Swnj #include "../h/conf.h" 263244Swnj #include "../h/user.h" 271900Swnj #include "../h/file.h" 283244Swnj #include "../h/map.h" 291900Swnj #include "../h/pte.h" 301947Swnj #include "../h/vm.h" 313244Swnj #include "../h/ubareg.h" 323244Swnj #include "../h/ubavar.h" 333244Swnj #include "../h/mtio.h" 343244Swnj #include "../h/ioctl.h" 353244Swnj #include "../h/cmap.h" 363244Swnj #include "../h/cpu.h" 371900Swnj 383244Swnj #include "../h/tsreg.h" 391900Swnj 403244Swnj /* 413244Swnj * There is a ctsbuf per tape controller. 423244Swnj * It is used as the token to pass to the internal routines 433244Swnj * to execute tape ioctls. 443244Swnj * In particular, when the tape is rewinding on close we release 453244Swnj * the user process but any further attempts to use the tape drive 463244Swnj * before the rewind completes will hang waiting for ctsbuf. 473244Swnj */ 483244Swnj struct buf ctsbuf[NTS]; 491900Swnj 503244Swnj /* 513244Swnj * Raw tape operations use rtsbuf. The driver 523244Swnj * notices when rtsbuf is being used and allows the user 533244Swnj * program to continue after errors and read records 543244Swnj * not of the standard length (BSIZE). 553244Swnj */ 563244Swnj struct buf rtsbuf[NTS]; 571900Swnj 583244Swnj /* 593244Swnj * Driver unibus interface routines and variables. 603244Swnj */ 613244Swnj int tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr(); 623244Swnj struct uba_ctlr *tsminfo[NTS]; 633244Swnj struct uba_device *tsdinfo[NTS]; 643327Swnj struct buf tsbuf[NTS]; 653244Swnj u_short tsstd[] = { 0772520, 0 }; 663244Swnj /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/ 673244Swnj struct uba_driver zsdriver = 683327Swnj { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 }; 691900Swnj 703244Swnj /* bits in minor device */ 713244Swnj #define TSUNIT(dev) (minor(dev)&03) 723244Swnj #define T_NOREWIND 04 731900Swnj 743244Swnj #define INF (daddr_t)1000000L 751900Swnj 763244Swnj /* 773244Swnj * Software state per tape transport. 783244Swnj * Also contains hardware state in message packets. 793244Swnj * 803244Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 813244Swnj * 2. We keep track of the current position on a block tape and seek 823244Swnj * before operations by forward/back spacing if necessary. 833244Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 843244Swnj * is open read write and the last thing done is a write we can 853244Swnj * write a standard end of tape mark (two eofs). 863244Swnj * 4. We remember the status registers after the last command, using 873244Swnj * then internally and returning them to the SENSE ioctl. 883244Swnj */ 893244Swnj struct ts_softc { 903244Swnj char sc_openf; /* lock against multiple opens */ 913244Swnj char sc_lastiow; /* last op was a write */ 923327Swnj short sc_resid; /* copy of last bc */ 933244Swnj daddr_t sc_blkno; /* block number, for block device tape */ 943244Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 953244Swnj struct ts_cmd sc_cmd; /* the command packet - ADDR MUST BE 0 MOD 4 */ 963244Swnj struct ts_sts sc_sts; /* status packet, for returned status */ 973244Swnj struct ts_char sc_char; /* characteristics packet */ 983244Swnj u_short sc_uba; /* Unibus addr of cmd pkt for tsdb */ 993244Swnj } ts_softc[NTS]; 1001900Swnj 1013244Swnj struct ts_softc *ts_ubaddr; /* Unibus address of ts_softc */ 1021900Swnj 1033244Swnj /* 1043244Swnj * States for um->um_tab.b_active, the per controller state flag. 1053244Swnj * This is used to sequence control in the driver. 1063244Swnj */ 1073244Swnj #define SSEEK 1 /* seeking */ 1083244Swnj #define SIO 2 /* doing seq i/o */ 1093244Swnj #define SCOM 3 /* sending control command */ 1103244Swnj #define SREW 4 /* sending a drive rewind */ 1111900Swnj 1123520Sroot #if NTM > 0 1133520Sroot /* kludge... see tm.c */ 1143520Sroot extern havetm; 1153520Sroot #endif 1163244Swnj /* 1173244Swnj * Determine if there is a controller for 1183244Swnj * a ts at address reg. Our goal is to make the 1193244Swnj * device interrupt. 1203244Swnj */ 1213244Swnj tsprobe(reg) 1223244Swnj caddr_t reg; 1233244Swnj { 1243244Swnj register int br, cvec; /* must be r11,r10; value-result */ 1251900Swnj 1263244Swnj #ifdef lint 1273244Swnj br = 0; cvec = br; br = cvec; 1283244Swnj #endif 1293244Swnj /****************/ 1303244Swnj /* */ 1313244Swnj /* K L U D G E */ 1323244Swnj /* */ 1333244Swnj /****************/ 1341900Swnj 1353520Sroot #if NTM > 0 1363520Sroot if (havetm) 1373520Sroot return (0); 1383520Sroot #endif 1393244Swnj /* IT'S TOO HARD TO MAKE THIS THING INTERRUPT 1403244Swnj JUST TO FIND ITS VECTOR */ 1413244Swnj cvec = 0224; 1423244Swnj br = 0x15; 1433244Swnj } 1441900Swnj 1453244Swnj /* 1463244Swnj * TS11 only supports one drive per controller; 1473244Swnj * check for ui_slave == 0. 1483244Swnj * 1493244Swnj * DO WE REALLY NEED THIS ROUTINE??? 1503244Swnj */ 1513244Swnj /*ARGSUSED*/ 1523244Swnj tsslave(ui, reg) 1533244Swnj struct uba_device *ui; 1543244Swnj caddr_t reg; 1553244Swnj { 1561900Swnj 1573244Swnj if (ui->ui_slave) /* non-zero slave not allowed */ 1583244Swnj return(0); 1593244Swnj return (1); 1603244Swnj } 1611900Swnj 1623244Swnj /* 1633244Swnj * Record attachment of the unit to the controller. 1643244Swnj * 1653244Swnj * SHOULD THIS ROUTINE DO ANYTHING??? 1663244Swnj */ 1673244Swnj /*ARGSUSED*/ 1683244Swnj tsattach(ui) 1693244Swnj struct uba_device *ui; 1703244Swnj { 1711900Swnj 1723244Swnj } 1731900Swnj 1743244Swnj /* 1753244Swnj * Open the device. Tapes are unique open 1763244Swnj * devices, so we refuse if it is already open. 1773244Swnj * We also check that a tape is available, and 1783244Swnj * don't block waiting here; if you want to wait 1793244Swnj * for a tape you should timeout in user code. 1803244Swnj */ 1811900Swnj tsopen(dev, flag) 1823244Swnj dev_t dev; 1833244Swnj int flag; 1841900Swnj { 1853244Swnj register int tsunit; 1863244Swnj register struct uba_device *ui; 1873244Swnj register struct ts_softc *sc; 1881900Swnj 1893244Swnj tsunit = TSUNIT(dev); 1903244Swnj if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf || 1913244Swnj (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) { 1921900Swnj u.u_error = ENXIO; 1931900Swnj return; 1941900Swnj } 1953244Swnj if (tsinit(tsunit)) { 1961900Swnj u.u_error = ENXIO; 1973647Swnj #ifdef TSDEBUG 1983327Swnj printd("init failed\n"); 1993647Swnj #endif 2001900Swnj return; 2011900Swnj } 2023647Swnj #ifdef TSDEBUG 2033327Swnj printd("init ok\n"); 2043647Swnj #endif 2053244Swnj tscommand(dev, TS_SENSE, 1); 2063647Swnj #ifdef TSDEBUG 2073327Swnj printd("sense xs0 %o\n", sc->sc_sts.s_xs0); 2083647Swnj #endif 209*3711Sroot if ((sc->sc_sts.s_xs0&TS_ONL) == 0) { 210*3711Sroot uprintf("ts%d: not online\n", tsunit); 2113244Swnj u.u_error = EIO; 2123244Swnj return; 2131900Swnj } 214*3711Sroot if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK))) { 215*3711Sroot uprintf("ts%d: no write ring\n", tsunit); 216*3711Sroot u.u_error = EIO; 217*3711Sroot return; 218*3711Sroot } 2193244Swnj sc->sc_openf = 1; 2203244Swnj sc->sc_blkno = (daddr_t)0; 2213244Swnj sc->sc_nxrec = INF; 2223244Swnj sc->sc_lastiow = 0; 2231900Swnj } 2241900Swnj 2253244Swnj /* 2263244Swnj * Close tape device. 2273244Swnj * 2283244Swnj * If tape was open for writing or last operation was 2293244Swnj * a write, then write two EOF's and backspace over the last one. 2303244Swnj * Unless this is a non-rewinding special file, rewind the tape. 2313244Swnj * Make the tape available to others. 2323244Swnj */ 2331900Swnj tsclose(dev, flag) 2343244Swnj register dev_t dev; 2353244Swnj register flag; 2361900Swnj { 2373244Swnj register struct ts_softc *sc = &ts_softc[TSUNIT(dev)]; 2381900Swnj 2393244Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2403244Swnj tscommand(dev, TS_WEOF, 1); 2413244Swnj tscommand(dev, TS_WEOF, 1); 2423244Swnj tscommand(dev, TS_SREV, 1); 2431900Swnj } 2443244Swnj if ((minor(dev)&T_NOREWIND) == 0) 2453244Swnj /* 2463244Swnj * 0 count means don't hang waiting for rewind complete 2473244Swnj * rather ctsbuf stays busy until the operation completes 2483244Swnj * preventing further opens from completing by 2493244Swnj * preventing a TS_SENSE from completing. 2503244Swnj */ 2513244Swnj tscommand(dev, TS_REW, 0); 2523244Swnj sc->sc_openf = 0; 2531900Swnj } 2541900Swnj 2553244Swnj /* 2563244Swnj * Initialize the TS11. Set up Unibus mapping for command 2573244Swnj * packets and set device characteristics. 2583244Swnj */ 2593244Swnj tsinit(unit) 2603244Swnj register int unit; 2611900Swnj { 2623244Swnj register struct ts_softc *sc = &ts_softc[unit]; 2633244Swnj register struct uba_ctlr *um = tsminfo[unit]; 2643244Swnj register struct device *addr = (struct device *)um->um_addr; 2653244Swnj register int i; 2663244Swnj 2673244Swnj /* 2683244Swnj * Map the command and message packets into Unibus 2693244Swnj * address space. We do all the command and message 2703244Swnj * packets at once to minimize the amount of Unibus 2713244Swnj * mapping necessary. 2723244Swnj */ 2733244Swnj if (ts_ubaddr == 0) { 2743244Swnj ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc; 2753244Swnj ctsbuf[unit].b_bcount = sizeof(ts_softc); 2763244Swnj i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0); 2773244Swnj i &= 0777777; 2783244Swnj ts_ubaddr = (struct ts_softc *)i; 2793244Swnj /* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */ 2803244Swnj if (ts_ubaddr == 0) 2813244Swnj printf("ts%d: zero ubaddr\n", unit); 2823244Swnj } 2833244Swnj /* 2843244Swnj * Now initialize the TS11 controller. 2853244Swnj * Set the characteristics. 2863244Swnj */ 2873668Swnj if (addr->tssr & (TS_NBA|TS_OFL)) { 2883244Swnj addr->tssr = 0; /* subsystem initialize */ 2893244Swnj tswait(addr); 2903244Swnj i = (int)&ts_ubaddr[unit].sc_cmd; /* Unibus addr of cmd */ 2913327Swnj if (i&3) { 2923327Swnj printf("addr mod 4 != 0\n"); 2933327Swnj return(1); 2943327Swnj } 2953244Swnj sc->sc_uba = (u_short)(i + ((i>>16)&3)); 2963244Swnj sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts; 2973244Swnj sc->sc_char.char_size = sizeof(struct ts_sts); 2983244Swnj sc->sc_char.char_mode = TS_ESS; 2993244Swnj sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR; 3003327Swnj i = (int)&ts_ubaddr[unit].sc_char; 3013327Swnj sc->sc_cmd.c_loba = i; 3023327Swnj sc->sc_cmd.c_hiba = (i>>16)&3; 3033244Swnj sc->sc_cmd.c_size = sizeof(struct ts_char); 3043244Swnj addr->tsdb = sc->sc_uba; 3053244Swnj tswait(addr); 3063327Swnj /* 3073327Swnj printd("%o %o %o %o %o %o %o %o\n", addr->tssr, sc->sc_sts.s_sts, sc->sc_sts.s_len, sc->sc_sts.s_rbpcr, sc->sc_sts.s_xs0, sc->sc_sts.s_xs1,sc->sc_sts.s_xs1,sc->sc_sts.s_xs2,sc->sc_sts.s_xs3); 3083327Swnj */ 3093327Swnj if (addr->tssr & TS_NBA) 3103327Swnj return(1); 3113244Swnj } 3123244Swnj return(0); 3133244Swnj } 3143244Swnj 3153244Swnj /* 3163244Swnj * Execute a command on the tape drive 3173244Swnj * a specified number of times. 3183244Swnj */ 3193244Swnj tscommand(dev, com, count) 3203244Swnj dev_t dev; 3213244Swnj int com, count; 3223244Swnj { 3231900Swnj register struct buf *bp; 3241900Swnj 3253244Swnj bp = &ctsbuf[TSUNIT(dev)]; 3263244Swnj (void) spl5(); 3273244Swnj while (bp->b_flags&B_BUSY) { 3283244Swnj /* 3293244Swnj * This special check is because B_BUSY never 3303244Swnj * gets cleared in the non-waiting rewind case. 3313244Swnj */ 3323244Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3333244Swnj break; 3341900Swnj bp->b_flags |= B_WANTED; 3351900Swnj sleep((caddr_t)bp, PRIBIO); 3361900Swnj } 3373244Swnj bp->b_flags = B_BUSY|B_READ; 3383244Swnj (void) spl0(); 3393647Swnj #ifdef TSDEBUG 3403327Swnj printd("command %o dev %x count %d\n", com, dev, count); 3413647Swnj #endif 3423244Swnj bp->b_dev = dev; 3433244Swnj bp->b_repcnt = count; 3443244Swnj bp->b_command = com; 3451900Swnj bp->b_blkno = 0; 3461900Swnj tsstrategy(bp); 3473244Swnj /* 3483244Swnj * In case of rewind from close, don't wait. 3493244Swnj * This is the only case where count can be 0. 3503244Swnj */ 3513244Swnj if (count == 0) 3523244Swnj return; 3531900Swnj iowait(bp); 3543244Swnj if (bp->b_flags&B_WANTED) 3551900Swnj wakeup((caddr_t)bp); 3563244Swnj bp->b_flags &= B_ERROR; 3571900Swnj } 3581900Swnj 3593244Swnj /* 3603244Swnj * Queue a tape operation. 3613244Swnj */ 3621900Swnj tsstrategy(bp) 3633244Swnj register struct buf *bp; 3641900Swnj { 3653244Swnj int tsunit = TSUNIT(bp->b_dev); 3663244Swnj register struct uba_ctlr *um; 3673327Swnj register struct buf *dp; 3681900Swnj 3693244Swnj /* 3703244Swnj * Put transfer at end of controller queue 3713244Swnj */ 3721900Swnj bp->av_forw = NULL; 3733244Swnj um = tsdinfo[tsunit]->ui_mi; 3743327Swnj dp = &tsbuf[tsunit]; 3753244Swnj (void) spl5(); 3763327Swnj if (dp->b_actf == NULL) 3773327Swnj dp->b_actf = bp; 3781900Swnj else 3793327Swnj dp->b_actl->av_forw = bp; 3803327Swnj dp->b_actl = bp; 3813327Swnj um->um_tab.b_actf = um->um_tab.b_actl = dp; 3823244Swnj /* 3833244Swnj * If the controller is not busy, get 3843244Swnj * it going. 3853244Swnj */ 3863244Swnj if (um->um_tab.b_active == 0) 3873244Swnj tsstart(um); 3883244Swnj (void) spl0(); 3891900Swnj } 3901900Swnj 3913244Swnj /* 3923244Swnj * Start activity on a ts controller. 3933244Swnj */ 3943244Swnj tsstart(um) 3953244Swnj register struct uba_ctlr *um; 3961900Swnj { 3971900Swnj register struct buf *bp; 3983244Swnj register struct device *addr = (struct device *)um->um_addr; 3993244Swnj register struct ts_softc *sc; 4003244Swnj register struct ts_cmd *tc; 4013244Swnj register struct uba_device *ui; 4023244Swnj int tsunit, cmd; 4031900Swnj daddr_t blkno; 4041900Swnj 4053244Swnj /* 4063244Swnj * Start the controller if there is something for it to do. 4073244Swnj */ 4083244Swnj loop: 4093327Swnj if ((bp = um->um_tab.b_actf->b_actf) == NULL) 4101900Swnj return; 4113244Swnj tsunit = TSUNIT(bp->b_dev); 4123244Swnj ui = tsdinfo[tsunit]; 4133244Swnj sc = &ts_softc[tsunit]; 4143244Swnj tc = &sc->sc_cmd; 4153244Swnj /* 4163244Swnj * Default is that last command was NOT a write command; 4173244Swnj * if we do a write command we will notice this in tsintr(). 4183244Swnj */ 4193656Swnj sc->sc_lastiow = 0; 4203244Swnj if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) { 4213244Swnj /* 4223244Swnj * Have had a hard error on a non-raw tape 4233244Swnj * or the tape unit is now unavailable 4243244Swnj * (e.g. taken off line). 4253244Swnj */ 4263244Swnj bp->b_flags |= B_ERROR; 4273244Swnj goto next; 4283244Swnj } 4293244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) { 4303244Swnj /* 4313244Swnj * Execute control operation with the specified count. 4323244Swnj */ 4333244Swnj um->um_tab.b_active = 4343244Swnj bp->b_command == TS_REW ? SREW : SCOM; 4353244Swnj tc->c_repcnt = bp->b_repcnt; 4363647Swnj #ifdef TSDEBUG 4373327Swnj printd("strat: do cmd\n"); 4383647Swnj #endif 4393244Swnj goto dobpcmd; 4403244Swnj } 4413244Swnj /* 4423244Swnj * The following checks handle boundary cases for operation 4433244Swnj * on non-raw tapes. On raw tapes the initialization of 4443244Swnj * sc->sc_nxrec by tsphys causes them to be skipped normally 4453244Swnj * (except in the case of retries). 4463244Swnj */ 4473244Swnj if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4483244Swnj /* 4493244Swnj * Can't read past known end-of-file. 4503244Swnj */ 4513244Swnj bp->b_flags |= B_ERROR; 4523244Swnj bp->b_error = ENXIO; 4533244Swnj goto next; 4543244Swnj } 4553244Swnj if (dbtofsb(bp->b_blkno) == sc->sc_nxrec && 4563244Swnj bp->b_flags&B_READ) { 4573244Swnj /* 4583244Swnj * Reading at end of file returns 0 bytes. 4593244Swnj */ 4603244Swnj bp->b_resid = bp->b_bcount; 4613244Swnj clrbuf(bp); 4623244Swnj goto next; 4633244Swnj } 4643244Swnj if ((bp->b_flags&B_READ) == 0) 4653244Swnj /* 4663244Swnj * Writing sets EOF 4673244Swnj */ 4683244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1; 4693244Swnj /* 4703244Swnj * If the data transfer command is in the correct place, 4713244Swnj * set up all the registers except the csr, and give 4723244Swnj * control over to the UNIBUS adapter routines, to 4733244Swnj * wait for resources to start the i/o. 4743244Swnj */ 4753244Swnj if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) { 4763244Swnj tc->c_size = bp->b_bcount; 4773244Swnj if ((bp->b_flags&B_READ) == 0) 4783244Swnj cmd = TS_WCOM; 4791900Swnj else 4803244Swnj cmd = TS_RCOM; 4813244Swnj if (um->um_tab.b_errcnt) 4823244Swnj cmd |= TS_RETRY; 4833244Swnj um->um_tab.b_active = SIO; 4843327Swnj tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd; 4853647Swnj #ifdef TSDEBUG 4863327Swnj printd("r/w %o size %d\n", tc->c_cmd, tc->c_size); 4873647Swnj #endif 4883244Swnj (void) ubago(ui); 4893244Swnj return; 4903244Swnj } 4913244Swnj /* 4923244Swnj * Tape positioned incorrectly; 4933244Swnj * set to seek forwards or backwards to the correct spot. 4943244Swnj * This happens for raw tapes only on error retries. 4953244Swnj */ 4963244Swnj um->um_tab.b_active = SSEEK; 4973647Swnj #ifdef TSDEBUG 4983327Swnj printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno); 4993647Swnj #endif 5003244Swnj if (blkno < dbtofsb(bp->b_blkno)) { 5013244Swnj bp->b_command = TS_SFORW; 5023244Swnj tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno; 5031900Swnj } else { 5043244Swnj bp->b_command = TS_SREV; 5053244Swnj tc->c_repcnt = blkno - dbtofsb(bp->b_blkno); 5061900Swnj } 5073244Swnj dobpcmd: 5083244Swnj /* 5093244Swnj * Do the command in bp. 5103244Swnj */ 5113327Swnj tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command; 5123244Swnj addr->tsdb = sc->sc_uba; 5131900Swnj return; 5141900Swnj 5153244Swnj next: 5163244Swnj /* 5173244Swnj * Done with this operation due to error or 5183244Swnj * the fact that it doesn't do anything. 5193244Swnj * Release UBA resources (if any), dequeue 5203244Swnj * the transfer and continue processing this slave. 5213244Swnj */ 5223244Swnj if (um->um_ubinfo) 5233244Swnj ubadone(um); 5243244Swnj um->um_tab.b_errcnt = 0; 5253327Swnj um->um_tab.b_actf->b_actf = bp->av_forw; 5261900Swnj iodone(bp); 5271900Swnj goto loop; 5281900Swnj } 5291900Swnj 5303244Swnj /* 5313244Swnj * The UNIBUS resources we needed have been 5323244Swnj * allocated to us; start the device. 5333244Swnj */ 5343244Swnj tsdgo(um) 5353244Swnj register struct uba_ctlr *um; 5361900Swnj { 5373244Swnj register struct device *addr = (struct device *)um->um_addr; 5383244Swnj register struct ts_softc *sc = &ts_softc[um->um_ctlr]; 5393327Swnj register int i; 5403244Swnj 5413327Swnj i = um->um_ubinfo & 0777777; 5423647Swnj #ifdef TSDEBUG 5433327Swnj printd("dgo addr %o\n", i); 5443647Swnj #endif 5453327Swnj sc->sc_cmd.c_loba = i; 5463327Swnj sc->sc_cmd.c_hiba = (i>>16)&3; 5473244Swnj addr->tsdb = sc->sc_uba; 5483244Swnj } 5493244Swnj 5503244Swnj /* 5513244Swnj * Ts interrupt routine. 5523244Swnj */ 5533244Swnj /*ARGSUSED*/ 5543244Swnj tsintr(ts11) 5553244Swnj int ts11; 5563244Swnj { 5571900Swnj register struct buf *bp; 5583244Swnj register struct uba_ctlr *um = tsminfo[ts11]; 5593244Swnj register struct device *addr; 5603244Swnj register struct ts_softc *sc; 5613244Swnj int tsunit; 5623244Swnj register state; 5631900Swnj 5643647Swnj #ifdef TSDEBUG 5653327Swnj printd("intr\n"); 5663647Swnj #endif 5673327Swnj if ((bp = um->um_tab.b_actf->b_actf) == NULL) 5681900Swnj return; 5693244Swnj tsunit = TSUNIT(bp->b_dev); 5703244Swnj addr = (struct device *)tsdinfo[tsunit]->ui_addr; 5713244Swnj /* 5723244Swnj * If last command was a rewind, and tape is still 5733244Swnj * rewinding, wait for the rewind complete interrupt. 5743244Swnj * 5753244Swnj * SHOULD NEVER GET AN INTERRUPT IN THIS STATE. 5763244Swnj */ 5773244Swnj if (um->um_tab.b_active == SREW) { 5783244Swnj um->um_tab.b_active = SCOM; 5793244Swnj if ((addr->tssr&TS_SSR) == 0) 5803244Swnj return; 5813244Swnj } 5823244Swnj /* 5833244Swnj * An operation completed... record status 5843244Swnj */ 5853647Swnj #ifdef TSDEBUG 5863327Swnj printd(" ok1\n"); 5873647Swnj #endif 5883244Swnj sc = &ts_softc[tsunit]; 5893244Swnj if ((bp->b_flags & B_READ) == 0) 5903244Swnj sc->sc_lastiow = 1; 5913244Swnj state = um->um_tab.b_active; 5923244Swnj um->um_tab.b_active = 0; 5933244Swnj /* 5943244Swnj * Check for errors. 5953244Swnj */ 5963244Swnj if (addr->tssr&TS_SC) { 5973244Swnj switch (addr->tssr & TS_TC) { 5983244Swnj case TS_UNREC: /* unrecoverable */ 5993244Swnj case TS_FATAL: /* fatal error */ 6003244Swnj case TS_ATTN: /* attention (shouldn't happen) */ 6013244Swnj case TS_RECNM: /* recoverable, no motion */ 6023244Swnj break; 6031900Swnj 6043244Swnj case TS_SUCC: /* success termination */ 6053244Swnj printf("ts%d: success\n", TSUNIT(minor(bp->b_dev))); 6063244Swnj goto ignoreerr; 6071900Swnj 6083244Swnj case TS_ALERT: /* tape status alert */ 6093244Swnj /* 6103244Swnj * If we hit the end of the tape file, 6113244Swnj * update our position. 6123244Swnj */ 6133244Swnj if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) { 6143244Swnj tsseteof(bp); /* set blkno and nxrec */ 6153244Swnj state = SCOM; /* force completion */ 6163244Swnj /* 6173244Swnj * Stuff bc so it will be unstuffed correctly 6183244Swnj * later to get resid. 6193244Swnj */ 6203244Swnj sc->sc_sts.s_rbpcr = bp->b_bcount; 6213244Swnj goto opdone; 6223244Swnj } 6233244Swnj /* 6243244Swnj * If we were reading raw tape and the record was too long 6253244Swnj * or too short, then we don't consider this an error. 6263244Swnj */ 6273244Swnj if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6283244Swnj sc->sc_sts.s_xs0&(TS_RLS|TS_RLL)) 6293244Swnj goto ignoreerr; 6303244Swnj case TS_RECOV: /* recoverable, tape moved */ 6313244Swnj /* 6323244Swnj * If this was an i/o operation retry up to 8 times. 6333244Swnj */ 6343244Swnj if (state==SIO) { 6353244Swnj if (++um->um_tab.b_errcnt < 7) { 6363244Swnj ubadone(um); 6373244Swnj goto opcont; 6383244Swnj } else 6393244Swnj sc->sc_blkno++; 6403244Swnj } else { 6413244Swnj /* 6423244Swnj * Non-i/o errors on non-raw tape 6433244Swnj * cause it to close. 6443244Swnj */ 6453244Swnj if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)]) 6463244Swnj sc->sc_openf = -1; 6473244Swnj } 6481900Swnj break; 6493244Swnj 6503244Swnj case TS_REJECT: /* function reject */ 6513244Swnj if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE) 6523244Swnj printf("ts%d: write locked\n", TSUNIT(bp->b_dev)); 6533244Swnj if ((sc->sc_sts.s_xs0 & TS_ONL) == 0) 6543244Swnj printf("ts%d: offline\n", TSUNIT(bp->b_dev)); 6551900Swnj break; 6561900Swnj } 6573244Swnj /* 6583244Swnj * Couldn't recover error 6593244Swnj */ 6603647Swnj printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev), 6613244Swnj bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS); 6623647Swnj if (sc->sc_sts.s_xs1) 6633647Swnj printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS); 6643647Swnj if (sc->sc_sts.s_xs2) 6653647Swnj printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS); 6663647Swnj if (sc->sc_sts.s_xs3) 6673647Swnj printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS); 6683647Swnj printf("\n"); 6693244Swnj bp->b_flags |= B_ERROR; 6703244Swnj goto opdone; 6713244Swnj } 6723244Swnj /* 6733244Swnj * Advance tape control FSM. 6743244Swnj */ 6753244Swnj ignoreerr: 6763244Swnj switch (state) { 6771900Swnj 6783244Swnj case SIO: 6793244Swnj /* 6803244Swnj * Read/write increments tape block number 6813244Swnj */ 6823244Swnj sc->sc_blkno++; 6833244Swnj goto opdone; 6841900Swnj 6851900Swnj case SCOM: 6863244Swnj /* 6873244Swnj * For forward/backward space record update current position. 6883244Swnj */ 6893244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) 6903244Swnj switch (bp->b_command) { 6911900Swnj 6923244Swnj case TS_SFORW: 6933244Swnj sc->sc_blkno += bp->b_repcnt; 6943244Swnj break; 6951900Swnj 6963244Swnj case TS_SREV: 6973244Swnj sc->sc_blkno -= bp->b_repcnt; 6983244Swnj break; 6993244Swnj } 7003244Swnj goto opdone; 7013244Swnj 7023244Swnj case SSEEK: 7033244Swnj sc->sc_blkno = dbtofsb(bp->b_blkno); 7043244Swnj goto opcont; 7053244Swnj 7061900Swnj default: 7073244Swnj panic("tsintr"); 7081900Swnj } 7093244Swnj opdone: 7103244Swnj /* 7113244Swnj * Reset error count and remove 7123244Swnj * from device queue. 7133244Swnj */ 7143244Swnj um->um_tab.b_errcnt = 0; 7153327Swnj um->um_tab.b_actf->b_actf = bp->av_forw; 7163244Swnj bp->b_resid = sc->sc_sts.s_rbpcr; 7173244Swnj ubadone(um); 7183647Swnj #ifdef TSDEBUG 7193327Swnj printd(" iodone\n"); 7203647Swnj #endif 7213244Swnj iodone(bp); 7223327Swnj if (um->um_tab.b_actf->b_actf == 0) 7233244Swnj return; 7243244Swnj opcont: 7253244Swnj tsstart(um); 7263244Swnj } 7273244Swnj 7283244Swnj tsseteof(bp) 7293244Swnj register struct buf *bp; 7303244Swnj { 7313244Swnj register int tsunit = TSUNIT(bp->b_dev); 7323244Swnj register struct ts_softc *sc = &ts_softc[tsunit]; 7333244Swnj 7343244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) { 7353244Swnj if (sc->sc_blkno > dbtofsb(bp->b_blkno)) { 7363244Swnj /* reversing */ 7373244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr; 7383244Swnj sc->sc_blkno = sc->sc_nxrec; 7393244Swnj } else { 7403244Swnj /* spacing forward */ 7413244Swnj sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr; 7423244Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7431900Swnj } 7443244Swnj return; 7453244Swnj } 7463244Swnj /* eof on read */ 7473244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno); 7481900Swnj } 7491900Swnj 7501900Swnj tsread(dev) 7513244Swnj dev_t dev; 7521900Swnj { 7533244Swnj 7541900Swnj tsphys(dev); 7553244Swnj if (u.u_error) 7563244Swnj return; 7573244Swnj physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys); 7581900Swnj } 7591900Swnj 7601900Swnj tswrite(dev) 7613244Swnj dev_t dev; 7621900Swnj { 7633244Swnj 7641900Swnj tsphys(dev); 7653244Swnj if (u.u_error) 7663244Swnj return; 7673244Swnj physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys); 7681900Swnj } 7691900Swnj 7703244Swnj /* 7713244Swnj * Check that a raw device exists. 7723244Swnj * If it does, set up sc_blkno and sc_nxrec 7733244Swnj * so that the tape will appear positioned correctly. 7743244Swnj */ 7751900Swnj tsphys(dev) 7763244Swnj dev_t dev; 7771900Swnj { 7783244Swnj register int tsunit = TSUNIT(dev); 7793244Swnj register daddr_t a; 7803244Swnj register struct ts_softc *sc; 7813244Swnj register struct uba_device *ui; 7821900Swnj 7833244Swnj if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) { 7843244Swnj u.u_error = ENXIO; 7853244Swnj return; 7863244Swnj } 7873244Swnj sc = &ts_softc[tsunit]; 7883244Swnj a = dbtofsb(u.u_offset >> 9); 7893244Swnj sc->sc_blkno = a; 7903244Swnj sc->sc_nxrec = a + 1; 7911900Swnj } 7921918Swnj 7933244Swnj tsreset(uban) 7943244Swnj int uban; 7951918Swnj { 7963244Swnj register struct uba_ctlr *um; 7973244Swnj register ts11; 7983244Swnj register struct buf *dp; 7991918Swnj 8003244Swnj for (ts11 = 0; ts11 < NTS; ts11++) { 8013244Swnj if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 || 8023244Swnj um->um_ubanum != uban) 8033244Swnj continue; 8043244Swnj printf(" ts%d", ts11); 8053244Swnj um->um_tab.b_active = 0; 8063244Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8073244Swnj ts_softc[ts11].sc_openf = -1; 8083244Swnj if (um->um_ubinfo) { 8093244Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8103244Swnj ubadone(um); 8113244Swnj } 8123244Swnj tsinit(ts11); 8133244Swnj tsstart(um); 8141918Swnj } 8151918Swnj } 8161918Swnj 8173244Swnj /*ARGSUSED*/ 8183244Swnj tsioctl(dev, cmd, addr, flag) 8193244Swnj caddr_t addr; 8203244Swnj dev_t dev; 8211918Swnj { 8223244Swnj int tsunit = TSUNIT(dev); 8233244Swnj register struct ts_softc *sc = &ts_softc[tsunit]; 8243244Swnj register struct buf *bp = &ctsbuf[TSUNIT(dev)]; 8253244Swnj register callcount; 8263244Swnj int fcount; 8273244Swnj struct mtop mtop; 8283244Swnj struct mtget mtget; 8293244Swnj /* we depend of the values and order of the MT codes here */ 8303244Swnj static tsops[] = 8313656Swnj {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE}; 8321918Swnj 8333244Swnj switch (cmd) { 8343244Swnj case MTIOCTOP: /* tape operation */ 8353244Swnj if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) { 8363244Swnj u.u_error = EFAULT; 8373244Swnj return; 8383244Swnj } 8393244Swnj switch(mtop.mt_op) { 8403244Swnj case MTWEOF: 8413244Swnj callcount = mtop.mt_count; 8423244Swnj fcount = 1; 8433244Swnj break; 8443244Swnj case MTFSF: case MTBSF: 8453244Swnj case MTFSR: case MTBSR: 8463244Swnj callcount = 1; 8473244Swnj fcount = mtop.mt_count; 8483244Swnj break; 8493244Swnj case MTREW: case MTOFFL: case MTNOP: 8503244Swnj callcount = 1; 8513244Swnj fcount = 1; 8523244Swnj break; 8533244Swnj default: 8543244Swnj u.u_error = ENXIO; 8553244Swnj return; 8563244Swnj } 8573244Swnj if (callcount <= 0 || fcount <= 0) { 8583244Swnj u.u_error = ENXIO; 8593244Swnj return; 8603244Swnj } 8613244Swnj while (--callcount >= 0) { 8623244Swnj tscommand(dev, tsops[mtop.mt_op], fcount); 8633244Swnj if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) && 8643244Swnj bp->b_resid) { 8653244Swnj u.u_error = EIO; 8663244Swnj break; 8673244Swnj } 8683244Swnj if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT) 8693244Swnj break; 8703244Swnj } 8713244Swnj geterror(bp); 8723244Swnj return; 8733244Swnj case MTIOCGET: 8743244Swnj mtget.mt_dsreg = 0; 8753244Swnj mtget.mt_erreg = sc->sc_sts.s_xs0; 8763244Swnj mtget.mt_resid = sc->sc_resid; 8773480Stoy mtget.mt_type = MT_ISTS; 8783244Swnj if (copyout((caddr_t)&mtget, addr, sizeof(mtget))) 8793244Swnj u.u_error = EFAULT; 8803244Swnj return; 8813244Swnj default: 8823244Swnj u.u_error = ENXIO; 8833244Swnj } 8841918Swnj } 8851918Swnj 8863244Swnj #define DBSIZE 20 8873244Swnj 8883244Swnj tsdump() 8891918Swnj { 8903244Swnj register struct uba_device *ui; 8913244Swnj register struct uba_regs *up; 8923244Swnj register struct device *addr; 8933244Swnj int blk, num; 8943244Swnj int start; 8951918Swnj 8963244Swnj start = 0; 8973244Swnj num = maxfree; 8983244Swnj #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 8993244Swnj if (tsdinfo[0] == 0) 9003244Swnj return (ENXIO); 9013244Swnj ui = phys(tsdinfo[0], struct uba_device *); 9023244Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9033327Swnj ubainit(up); 9043244Swnj DELAY(1000000); 9053244Swnj addr = (struct device *)ui->ui_physaddr; 9063244Swnj addr->tssr = 0; 9073244Swnj tswait(addr); 9083244Swnj while (num > 0) { 9093244Swnj blk = num > DBSIZE ? DBSIZE : num; 9103244Swnj tsdwrite(start, blk, addr, up); 9113244Swnj start += blk; 9123244Swnj num -= blk; 9133244Swnj } 9143244Swnj tseof(addr); 9153244Swnj tseof(addr); 9163244Swnj tswait(addr); 9173244Swnj if (addr->tssr&TS_SC) 9183244Swnj return (EIO); 9193244Swnj addr->tssr = 0; 9203244Swnj tswait(addr); 9213244Swnj return (0); 9221918Swnj } 9231918Swnj 9243244Swnj tsdwrite(dbuf, num, addr, up) 9253244Swnj register dbuf, num; 9263244Swnj register struct device *addr; 9273244Swnj struct uba_regs *up; 9281918Swnj { 9293244Swnj register struct pte *io; 9303244Swnj register int npf; 9311918Swnj 9323244Swnj tswait(addr); 9333244Swnj io = up->uba_map; 9343244Swnj npf = num+1; 9353244Swnj while (--npf != 0) 9363244Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9373244Swnj *(int *)io = 0; 9383244Swnj #ifdef notyet 9393244Swnj addr->tsbc = -(num*NBPG); 9403244Swnj addr->tsba = 0; 9413244Swnj addr->tscs = TS_WCOM | TM_GO; 9423244Swnj #endif 9431918Swnj } 9441918Swnj 9453244Swnj tswait(addr) 9463244Swnj register struct device *addr; 9471918Swnj { 9483244Swnj register s; 9491918Swnj 9503244Swnj do 9513244Swnj s = addr->tssr; 9523244Swnj while ((s & TS_SSR) == 0); 9531918Swnj } 9541918Swnj 9553244Swnj tseof(addr) 9563244Swnj struct device *addr; 9571918Swnj { 9581918Swnj 9593244Swnj tswait(addr); 9603244Swnj #ifdef notyet 9613244Swnj addr->tscs = TS_WEOF | TM_GO; 9623244Swnj #endif 9631918Swnj } 9641900Swnj #endif 965