1*3985Sroot /* ts.c 4.18 81/07/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 */ 121*3985Sroot /*ARGSUSED*/ 1223244Swnj tsprobe(reg) 1233244Swnj caddr_t reg; 1243244Swnj { 1253244Swnj register int br, cvec; /* must be r11,r10; value-result */ 1261900Swnj 1273244Swnj #ifdef lint 1283244Swnj br = 0; cvec = br; br = cvec; 1293244Swnj #endif 1303244Swnj /****************/ 1313244Swnj /* */ 1323244Swnj /* K L U D G E */ 1333244Swnj /* */ 1343244Swnj /****************/ 1351900Swnj 1363520Sroot #if NTM > 0 1373520Sroot if (havetm) 1383520Sroot return (0); 1393520Sroot #endif 1403244Swnj /* IT'S TOO HARD TO MAKE THIS THING INTERRUPT 1413244Swnj JUST TO FIND ITS VECTOR */ 1423244Swnj cvec = 0224; 1433244Swnj br = 0x15; 1443982Sroot return (1); 1453244Swnj } 1461900Swnj 1473244Swnj /* 1483244Swnj * TS11 only supports one drive per controller; 1493244Swnj * check for ui_slave == 0. 1503244Swnj * 1513244Swnj * DO WE REALLY NEED THIS ROUTINE??? 1523244Swnj */ 1533244Swnj /*ARGSUSED*/ 1543244Swnj tsslave(ui, reg) 1553244Swnj struct uba_device *ui; 1563244Swnj caddr_t reg; 1573244Swnj { 1581900Swnj 1593244Swnj if (ui->ui_slave) /* non-zero slave not allowed */ 1603244Swnj return(0); 1613244Swnj return (1); 1623244Swnj } 1631900Swnj 1643244Swnj /* 1653244Swnj * Record attachment of the unit to the controller. 1663244Swnj * 1673244Swnj * SHOULD THIS ROUTINE DO ANYTHING??? 1683244Swnj */ 1693244Swnj /*ARGSUSED*/ 1703244Swnj tsattach(ui) 1713244Swnj struct uba_device *ui; 1723244Swnj { 1731900Swnj 1743244Swnj } 1751900Swnj 1763244Swnj /* 1773244Swnj * Open the device. Tapes are unique open 1783244Swnj * devices, so we refuse if it is already open. 1793244Swnj * We also check that a tape is available, and 1803244Swnj * don't block waiting here; if you want to wait 1813244Swnj * for a tape you should timeout in user code. 1823244Swnj */ 1831900Swnj tsopen(dev, flag) 1843244Swnj dev_t dev; 1853244Swnj int flag; 1861900Swnj { 1873244Swnj register int tsunit; 1883244Swnj register struct uba_device *ui; 1893244Swnj register struct ts_softc *sc; 1901900Swnj 1913244Swnj tsunit = TSUNIT(dev); 1923244Swnj if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf || 1933244Swnj (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) { 1941900Swnj u.u_error = ENXIO; 1951900Swnj return; 1961900Swnj } 1973244Swnj if (tsinit(tsunit)) { 1981900Swnj u.u_error = ENXIO; 1993647Swnj #ifdef TSDEBUG 2003327Swnj printd("init failed\n"); 2013647Swnj #endif 2021900Swnj return; 2031900Swnj } 2043647Swnj #ifdef TSDEBUG 2053327Swnj printd("init ok\n"); 2063647Swnj #endif 2073244Swnj tscommand(dev, TS_SENSE, 1); 2083647Swnj #ifdef TSDEBUG 2093327Swnj printd("sense xs0 %o\n", sc->sc_sts.s_xs0); 2103647Swnj #endif 2113711Sroot if ((sc->sc_sts.s_xs0&TS_ONL) == 0) { 2123711Sroot uprintf("ts%d: not online\n", tsunit); 2133244Swnj u.u_error = EIO; 2143244Swnj return; 2151900Swnj } 2163718Sroot if ((flag&(FREAD|FWRITE)) == FWRITE && (sc->sc_sts.s_xs0&TS_WLK)) { 2173711Sroot uprintf("ts%d: no write ring\n", tsunit); 2183711Sroot u.u_error = EIO; 2193711Sroot return; 2203711Sroot } 2213244Swnj sc->sc_openf = 1; 2223244Swnj sc->sc_blkno = (daddr_t)0; 2233244Swnj sc->sc_nxrec = INF; 2243244Swnj sc->sc_lastiow = 0; 2251900Swnj } 2261900Swnj 2273244Swnj /* 2283244Swnj * Close tape device. 2293244Swnj * 2303244Swnj * If tape was open for writing or last operation was 2313244Swnj * a write, then write two EOF's and backspace over the last one. 2323244Swnj * Unless this is a non-rewinding special file, rewind the tape. 2333244Swnj * Make the tape available to others. 2343244Swnj */ 2351900Swnj tsclose(dev, flag) 2363244Swnj register dev_t dev; 2373244Swnj register flag; 2381900Swnj { 2393244Swnj register struct ts_softc *sc = &ts_softc[TSUNIT(dev)]; 2401900Swnj 2413244Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2423244Swnj tscommand(dev, TS_WEOF, 1); 2433244Swnj tscommand(dev, TS_WEOF, 1); 2443244Swnj tscommand(dev, TS_SREV, 1); 2451900Swnj } 2463244Swnj if ((minor(dev)&T_NOREWIND) == 0) 2473244Swnj /* 2483244Swnj * 0 count means don't hang waiting for rewind complete 2493244Swnj * rather ctsbuf stays busy until the operation completes 2503244Swnj * preventing further opens from completing by 2513244Swnj * preventing a TS_SENSE from completing. 2523244Swnj */ 2533244Swnj tscommand(dev, TS_REW, 0); 2543244Swnj sc->sc_openf = 0; 2551900Swnj } 2561900Swnj 2573244Swnj /* 2583244Swnj * Initialize the TS11. Set up Unibus mapping for command 2593244Swnj * packets and set device characteristics. 2603244Swnj */ 2613244Swnj tsinit(unit) 2623244Swnj register int unit; 2631900Swnj { 2643244Swnj register struct ts_softc *sc = &ts_softc[unit]; 2653244Swnj register struct uba_ctlr *um = tsminfo[unit]; 2663244Swnj register struct device *addr = (struct device *)um->um_addr; 2673244Swnj register int i; 2683244Swnj 2693244Swnj /* 2703244Swnj * Map the command and message packets into Unibus 2713244Swnj * address space. We do all the command and message 2723244Swnj * packets at once to minimize the amount of Unibus 2733244Swnj * mapping necessary. 2743244Swnj */ 2753244Swnj if (ts_ubaddr == 0) { 2763244Swnj ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc; 2773244Swnj ctsbuf[unit].b_bcount = sizeof(ts_softc); 2783244Swnj i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0); 2793244Swnj i &= 0777777; 2803244Swnj ts_ubaddr = (struct ts_softc *)i; 2813244Swnj /* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */ 2823244Swnj if (ts_ubaddr == 0) 2833244Swnj printf("ts%d: zero ubaddr\n", unit); 2843244Swnj } 2853244Swnj /* 2863244Swnj * Now initialize the TS11 controller. 2873244Swnj * Set the characteristics. 2883244Swnj */ 2893668Swnj if (addr->tssr & (TS_NBA|TS_OFL)) { 2903244Swnj addr->tssr = 0; /* subsystem initialize */ 2913244Swnj tswait(addr); 2923244Swnj i = (int)&ts_ubaddr[unit].sc_cmd; /* Unibus addr of cmd */ 2933327Swnj if (i&3) { 2943327Swnj printf("addr mod 4 != 0\n"); 2953327Swnj return(1); 2963327Swnj } 2973244Swnj sc->sc_uba = (u_short)(i + ((i>>16)&3)); 2983244Swnj sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts; 2993244Swnj sc->sc_char.char_size = sizeof(struct ts_sts); 3003244Swnj sc->sc_char.char_mode = TS_ESS; 3013244Swnj sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR; 3023327Swnj i = (int)&ts_ubaddr[unit].sc_char; 3033327Swnj sc->sc_cmd.c_loba = i; 3043327Swnj sc->sc_cmd.c_hiba = (i>>16)&3; 3053244Swnj sc->sc_cmd.c_size = sizeof(struct ts_char); 3063244Swnj addr->tsdb = sc->sc_uba; 3073244Swnj tswait(addr); 3083327Swnj /* 3093327Swnj 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); 3103327Swnj */ 3113327Swnj if (addr->tssr & TS_NBA) 3123327Swnj return(1); 3133244Swnj } 3143244Swnj return(0); 3153244Swnj } 3163244Swnj 3173244Swnj /* 3183244Swnj * Execute a command on the tape drive 3193244Swnj * a specified number of times. 3203244Swnj */ 3213244Swnj tscommand(dev, com, count) 3223244Swnj dev_t dev; 3233244Swnj int com, count; 3243244Swnj { 3251900Swnj register struct buf *bp; 3261900Swnj 3273244Swnj bp = &ctsbuf[TSUNIT(dev)]; 3283244Swnj (void) spl5(); 3293244Swnj while (bp->b_flags&B_BUSY) { 3303244Swnj /* 3313244Swnj * This special check is because B_BUSY never 3323244Swnj * gets cleared in the non-waiting rewind case. 3333244Swnj */ 3343244Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3353244Swnj break; 3361900Swnj bp->b_flags |= B_WANTED; 3371900Swnj sleep((caddr_t)bp, PRIBIO); 3381900Swnj } 3393244Swnj bp->b_flags = B_BUSY|B_READ; 3403244Swnj (void) spl0(); 3413647Swnj #ifdef TSDEBUG 3423327Swnj printd("command %o dev %x count %d\n", com, dev, count); 3433647Swnj #endif 3443244Swnj bp->b_dev = dev; 3453244Swnj bp->b_repcnt = count; 3463244Swnj bp->b_command = com; 3471900Swnj bp->b_blkno = 0; 3481900Swnj tsstrategy(bp); 3493244Swnj /* 3503244Swnj * In case of rewind from close, don't wait. 3513244Swnj * This is the only case where count can be 0. 3523244Swnj */ 3533244Swnj if (count == 0) 3543244Swnj return; 3551900Swnj iowait(bp); 3563244Swnj if (bp->b_flags&B_WANTED) 3571900Swnj wakeup((caddr_t)bp); 3583244Swnj bp->b_flags &= B_ERROR; 3591900Swnj } 3601900Swnj 3613244Swnj /* 3623244Swnj * Queue a tape operation. 3633244Swnj */ 3641900Swnj tsstrategy(bp) 3653244Swnj register struct buf *bp; 3661900Swnj { 3673244Swnj int tsunit = TSUNIT(bp->b_dev); 3683244Swnj register struct uba_ctlr *um; 3693327Swnj register struct buf *dp; 3701900Swnj 3713244Swnj /* 3723244Swnj * Put transfer at end of controller queue 3733244Swnj */ 3741900Swnj bp->av_forw = NULL; 3753244Swnj um = tsdinfo[tsunit]->ui_mi; 3763327Swnj dp = &tsbuf[tsunit]; 3773244Swnj (void) spl5(); 3783327Swnj if (dp->b_actf == NULL) 3793327Swnj dp->b_actf = bp; 3801900Swnj else 3813327Swnj dp->b_actl->av_forw = bp; 3823327Swnj dp->b_actl = bp; 3833327Swnj um->um_tab.b_actf = um->um_tab.b_actl = dp; 3843244Swnj /* 3853244Swnj * If the controller is not busy, get 3863244Swnj * it going. 3873244Swnj */ 3883244Swnj if (um->um_tab.b_active == 0) 3893244Swnj tsstart(um); 3903244Swnj (void) spl0(); 3911900Swnj } 3921900Swnj 3933244Swnj /* 3943244Swnj * Start activity on a ts controller. 3953244Swnj */ 3963244Swnj tsstart(um) 3973244Swnj register struct uba_ctlr *um; 3981900Swnj { 3991900Swnj register struct buf *bp; 4003244Swnj register struct device *addr = (struct device *)um->um_addr; 4013244Swnj register struct ts_softc *sc; 4023244Swnj register struct ts_cmd *tc; 4033244Swnj register struct uba_device *ui; 4043244Swnj int tsunit, cmd; 4051900Swnj daddr_t blkno; 4061900Swnj 4073244Swnj /* 4083244Swnj * Start the controller if there is something for it to do. 4093244Swnj */ 4103244Swnj loop: 4113327Swnj if ((bp = um->um_tab.b_actf->b_actf) == NULL) 4121900Swnj return; 4133244Swnj tsunit = TSUNIT(bp->b_dev); 4143244Swnj ui = tsdinfo[tsunit]; 4153244Swnj sc = &ts_softc[tsunit]; 4163244Swnj tc = &sc->sc_cmd; 4173244Swnj /* 4183244Swnj * Default is that last command was NOT a write command; 4193244Swnj * if we do a write command we will notice this in tsintr(). 4203244Swnj */ 4213656Swnj sc->sc_lastiow = 0; 4223244Swnj if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) { 4233244Swnj /* 4243244Swnj * Have had a hard error on a non-raw tape 4253244Swnj * or the tape unit is now unavailable 4263244Swnj * (e.g. taken off line). 4273244Swnj */ 4283244Swnj bp->b_flags |= B_ERROR; 4293244Swnj goto next; 4303244Swnj } 4313244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) { 4323244Swnj /* 4333244Swnj * Execute control operation with the specified count. 4343244Swnj */ 4353244Swnj um->um_tab.b_active = 4363244Swnj bp->b_command == TS_REW ? SREW : SCOM; 4373244Swnj tc->c_repcnt = bp->b_repcnt; 4383647Swnj #ifdef TSDEBUG 4393327Swnj printd("strat: do cmd\n"); 4403647Swnj #endif 4413244Swnj goto dobpcmd; 4423244Swnj } 4433244Swnj /* 4443244Swnj * The following checks handle boundary cases for operation 4453244Swnj * on non-raw tapes. On raw tapes the initialization of 4463244Swnj * sc->sc_nxrec by tsphys causes them to be skipped normally 4473244Swnj * (except in the case of retries). 4483244Swnj */ 4493244Swnj if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4503244Swnj /* 4513244Swnj * Can't read past known end-of-file. 4523244Swnj */ 4533244Swnj bp->b_flags |= B_ERROR; 4543244Swnj bp->b_error = ENXIO; 4553244Swnj goto next; 4563244Swnj } 4573244Swnj if (dbtofsb(bp->b_blkno) == sc->sc_nxrec && 4583244Swnj bp->b_flags&B_READ) { 4593244Swnj /* 4603244Swnj * Reading at end of file returns 0 bytes. 4613244Swnj */ 4623244Swnj bp->b_resid = bp->b_bcount; 4633244Swnj clrbuf(bp); 4643244Swnj goto next; 4653244Swnj } 4663244Swnj if ((bp->b_flags&B_READ) == 0) 4673244Swnj /* 4683244Swnj * Writing sets EOF 4693244Swnj */ 4703244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1; 4713244Swnj /* 4723244Swnj * If the data transfer command is in the correct place, 4733244Swnj * set up all the registers except the csr, and give 4743244Swnj * control over to the UNIBUS adapter routines, to 4753244Swnj * wait for resources to start the i/o. 4763244Swnj */ 4773244Swnj if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) { 4783244Swnj tc->c_size = bp->b_bcount; 4793244Swnj if ((bp->b_flags&B_READ) == 0) 4803244Swnj cmd = TS_WCOM; 4811900Swnj else 4823244Swnj cmd = TS_RCOM; 4833244Swnj if (um->um_tab.b_errcnt) 4843244Swnj cmd |= TS_RETRY; 4853244Swnj um->um_tab.b_active = SIO; 4863327Swnj tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd; 4873647Swnj #ifdef TSDEBUG 4883327Swnj printd("r/w %o size %d\n", tc->c_cmd, tc->c_size); 4893647Swnj #endif 4903244Swnj (void) ubago(ui); 4913244Swnj return; 4923244Swnj } 4933244Swnj /* 4943244Swnj * Tape positioned incorrectly; 4953244Swnj * set to seek forwards or backwards to the correct spot. 4963244Swnj * This happens for raw tapes only on error retries. 4973244Swnj */ 4983244Swnj um->um_tab.b_active = SSEEK; 4993647Swnj #ifdef TSDEBUG 5003327Swnj printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno); 5013647Swnj #endif 5023244Swnj if (blkno < dbtofsb(bp->b_blkno)) { 5033244Swnj bp->b_command = TS_SFORW; 5043244Swnj tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno; 5051900Swnj } else { 5063244Swnj bp->b_command = TS_SREV; 5073244Swnj tc->c_repcnt = blkno - dbtofsb(bp->b_blkno); 5081900Swnj } 5093244Swnj dobpcmd: 5103244Swnj /* 5113244Swnj * Do the command in bp. 5123244Swnj */ 5133327Swnj tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command; 5143244Swnj addr->tsdb = sc->sc_uba; 5151900Swnj return; 5161900Swnj 5173244Swnj next: 5183244Swnj /* 5193244Swnj * Done with this operation due to error or 5203244Swnj * the fact that it doesn't do anything. 5213244Swnj * Release UBA resources (if any), dequeue 5223244Swnj * the transfer and continue processing this slave. 5233244Swnj */ 5243244Swnj if (um->um_ubinfo) 5253244Swnj ubadone(um); 5263244Swnj um->um_tab.b_errcnt = 0; 5273327Swnj um->um_tab.b_actf->b_actf = bp->av_forw; 5281900Swnj iodone(bp); 5291900Swnj goto loop; 5301900Swnj } 5311900Swnj 5323244Swnj /* 5333244Swnj * The UNIBUS resources we needed have been 5343244Swnj * allocated to us; start the device. 5353244Swnj */ 5363244Swnj tsdgo(um) 5373244Swnj register struct uba_ctlr *um; 5381900Swnj { 5393244Swnj register struct device *addr = (struct device *)um->um_addr; 5403244Swnj register struct ts_softc *sc = &ts_softc[um->um_ctlr]; 5413327Swnj register int i; 5423244Swnj 5433327Swnj i = um->um_ubinfo & 0777777; 5443647Swnj #ifdef TSDEBUG 5453327Swnj printd("dgo addr %o\n", i); 5463647Swnj #endif 5473327Swnj sc->sc_cmd.c_loba = i; 5483327Swnj sc->sc_cmd.c_hiba = (i>>16)&3; 5493244Swnj addr->tsdb = sc->sc_uba; 5503244Swnj } 5513244Swnj 5523244Swnj /* 5533244Swnj * Ts interrupt routine. 5543244Swnj */ 5553244Swnj /*ARGSUSED*/ 5563244Swnj tsintr(ts11) 5573244Swnj int ts11; 5583244Swnj { 5591900Swnj register struct buf *bp; 5603244Swnj register struct uba_ctlr *um = tsminfo[ts11]; 5613244Swnj register struct device *addr; 5623244Swnj register struct ts_softc *sc; 5633244Swnj int tsunit; 5643244Swnj register state; 5651900Swnj 5663647Swnj #ifdef TSDEBUG 5673327Swnj printd("intr\n"); 5683647Swnj #endif 5693327Swnj if ((bp = um->um_tab.b_actf->b_actf) == NULL) 5701900Swnj return; 5713244Swnj tsunit = TSUNIT(bp->b_dev); 5723244Swnj addr = (struct device *)tsdinfo[tsunit]->ui_addr; 5733244Swnj /* 5743244Swnj * If last command was a rewind, and tape is still 5753244Swnj * rewinding, wait for the rewind complete interrupt. 5763244Swnj * 5773244Swnj * SHOULD NEVER GET AN INTERRUPT IN THIS STATE. 5783244Swnj */ 5793244Swnj if (um->um_tab.b_active == SREW) { 5803244Swnj um->um_tab.b_active = SCOM; 5813244Swnj if ((addr->tssr&TS_SSR) == 0) 5823244Swnj return; 5833244Swnj } 5843244Swnj /* 5853244Swnj * An operation completed... record status 5863244Swnj */ 5873647Swnj #ifdef TSDEBUG 5883327Swnj printd(" ok1\n"); 5893647Swnj #endif 5903244Swnj sc = &ts_softc[tsunit]; 5913244Swnj if ((bp->b_flags & B_READ) == 0) 5923244Swnj sc->sc_lastiow = 1; 5933244Swnj state = um->um_tab.b_active; 5943244Swnj um->um_tab.b_active = 0; 5953244Swnj /* 5963244Swnj * Check for errors. 5973244Swnj */ 5983244Swnj if (addr->tssr&TS_SC) { 5993244Swnj switch (addr->tssr & TS_TC) { 6003244Swnj case TS_UNREC: /* unrecoverable */ 6013244Swnj case TS_FATAL: /* fatal error */ 6023244Swnj case TS_ATTN: /* attention (shouldn't happen) */ 6033244Swnj case TS_RECNM: /* recoverable, no motion */ 6043244Swnj break; 6051900Swnj 6063244Swnj case TS_SUCC: /* success termination */ 6073244Swnj printf("ts%d: success\n", TSUNIT(minor(bp->b_dev))); 6083244Swnj goto ignoreerr; 6091900Swnj 6103244Swnj case TS_ALERT: /* tape status alert */ 6113244Swnj /* 6123244Swnj * If we hit the end of the tape file, 6133244Swnj * update our position. 6143244Swnj */ 6153244Swnj if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) { 6163244Swnj tsseteof(bp); /* set blkno and nxrec */ 6173244Swnj state = SCOM; /* force completion */ 6183244Swnj /* 6193244Swnj * Stuff bc so it will be unstuffed correctly 6203244Swnj * later to get resid. 6213244Swnj */ 6223244Swnj sc->sc_sts.s_rbpcr = bp->b_bcount; 6233244Swnj goto opdone; 6243244Swnj } 6253244Swnj /* 6263244Swnj * If we were reading raw tape and the record was too long 6273244Swnj * or too short, then we don't consider this an error. 6283244Swnj */ 6293244Swnj if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6303244Swnj sc->sc_sts.s_xs0&(TS_RLS|TS_RLL)) 6313244Swnj goto ignoreerr; 6323244Swnj case TS_RECOV: /* recoverable, tape moved */ 6333244Swnj /* 6343244Swnj * If this was an i/o operation retry up to 8 times. 6353244Swnj */ 6363244Swnj if (state==SIO) { 6373244Swnj if (++um->um_tab.b_errcnt < 7) { 6383244Swnj ubadone(um); 6393244Swnj goto opcont; 6403244Swnj } else 6413244Swnj sc->sc_blkno++; 6423244Swnj } else { 6433244Swnj /* 6443244Swnj * Non-i/o errors on non-raw tape 6453244Swnj * cause it to close. 6463244Swnj */ 6473244Swnj if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)]) 6483244Swnj sc->sc_openf = -1; 6493244Swnj } 6501900Swnj break; 6513244Swnj 6523244Swnj case TS_REJECT: /* function reject */ 6533244Swnj if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE) 6543244Swnj printf("ts%d: write locked\n", TSUNIT(bp->b_dev)); 6553244Swnj if ((sc->sc_sts.s_xs0 & TS_ONL) == 0) 6563244Swnj printf("ts%d: offline\n", TSUNIT(bp->b_dev)); 6571900Swnj break; 6581900Swnj } 6593244Swnj /* 6603244Swnj * Couldn't recover error 6613244Swnj */ 6623647Swnj printf("ts%d: hard error bn%d xs0=%b", TSUNIT(bp->b_dev), 6633244Swnj bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS); 6643647Swnj if (sc->sc_sts.s_xs1) 6653647Swnj printf(" xs1=%b", sc->sc_sts.s_xs1, TSXS1_BITS); 6663647Swnj if (sc->sc_sts.s_xs2) 6673647Swnj printf(" xs2=%b", sc->sc_sts.s_xs2, TSXS2_BITS); 6683647Swnj if (sc->sc_sts.s_xs3) 6693647Swnj printf(" xs3=%b", sc->sc_sts.s_xs3, TSXS3_BITS); 6703647Swnj printf("\n"); 6713244Swnj bp->b_flags |= B_ERROR; 6723244Swnj goto opdone; 6733244Swnj } 6743244Swnj /* 6753244Swnj * Advance tape control FSM. 6763244Swnj */ 6773244Swnj ignoreerr: 6783244Swnj switch (state) { 6791900Swnj 6803244Swnj case SIO: 6813244Swnj /* 6823244Swnj * Read/write increments tape block number 6833244Swnj */ 6843244Swnj sc->sc_blkno++; 6853244Swnj goto opdone; 6861900Swnj 6871900Swnj case SCOM: 6883244Swnj /* 6893244Swnj * For forward/backward space record update current position. 6903244Swnj */ 6913244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) 6923244Swnj switch (bp->b_command) { 6931900Swnj 6943244Swnj case TS_SFORW: 6953244Swnj sc->sc_blkno += bp->b_repcnt; 6963244Swnj break; 6971900Swnj 6983244Swnj case TS_SREV: 6993244Swnj sc->sc_blkno -= bp->b_repcnt; 7003244Swnj break; 7013244Swnj } 7023244Swnj goto opdone; 7033244Swnj 7043244Swnj case SSEEK: 7053244Swnj sc->sc_blkno = dbtofsb(bp->b_blkno); 7063244Swnj goto opcont; 7073244Swnj 7081900Swnj default: 7093244Swnj panic("tsintr"); 7101900Swnj } 7113244Swnj opdone: 7123244Swnj /* 7133244Swnj * Reset error count and remove 7143244Swnj * from device queue. 7153244Swnj */ 7163244Swnj um->um_tab.b_errcnt = 0; 7173327Swnj um->um_tab.b_actf->b_actf = bp->av_forw; 7183244Swnj bp->b_resid = sc->sc_sts.s_rbpcr; 7193244Swnj ubadone(um); 7203647Swnj #ifdef TSDEBUG 7213327Swnj printd(" iodone\n"); 7223647Swnj #endif 7233244Swnj iodone(bp); 7243327Swnj if (um->um_tab.b_actf->b_actf == 0) 7253244Swnj return; 7263244Swnj opcont: 7273244Swnj tsstart(um); 7283244Swnj } 7293244Swnj 7303244Swnj tsseteof(bp) 7313244Swnj register struct buf *bp; 7323244Swnj { 7333244Swnj register int tsunit = TSUNIT(bp->b_dev); 7343244Swnj register struct ts_softc *sc = &ts_softc[tsunit]; 7353244Swnj 7363244Swnj if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) { 7373244Swnj if (sc->sc_blkno > dbtofsb(bp->b_blkno)) { 7383244Swnj /* reversing */ 7393244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr; 7403244Swnj sc->sc_blkno = sc->sc_nxrec; 7413244Swnj } else { 7423244Swnj /* spacing forward */ 7433244Swnj sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr; 7443244Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7451900Swnj } 7463244Swnj return; 7473244Swnj } 7483244Swnj /* eof on read */ 7493244Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno); 7501900Swnj } 7511900Swnj 7521900Swnj tsread(dev) 7533244Swnj dev_t dev; 7541900Swnj { 7553244Swnj 7561900Swnj tsphys(dev); 7573244Swnj if (u.u_error) 7583244Swnj return; 7593244Swnj physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys); 7601900Swnj } 7611900Swnj 7621900Swnj tswrite(dev) 7633244Swnj dev_t dev; 7641900Swnj { 7653244Swnj 7661900Swnj tsphys(dev); 7673244Swnj if (u.u_error) 7683244Swnj return; 7693244Swnj physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys); 7701900Swnj } 7711900Swnj 7723244Swnj /* 7733244Swnj * Check that a raw device exists. 7743244Swnj * If it does, set up sc_blkno and sc_nxrec 7753244Swnj * so that the tape will appear positioned correctly. 7763244Swnj */ 7771900Swnj tsphys(dev) 7783244Swnj dev_t dev; 7791900Swnj { 7803244Swnj register int tsunit = TSUNIT(dev); 7813244Swnj register daddr_t a; 7823244Swnj register struct ts_softc *sc; 7833244Swnj register struct uba_device *ui; 7841900Swnj 7853244Swnj if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) { 7863244Swnj u.u_error = ENXIO; 7873244Swnj return; 7883244Swnj } 7893244Swnj sc = &ts_softc[tsunit]; 7903244Swnj a = dbtofsb(u.u_offset >> 9); 7913244Swnj sc->sc_blkno = a; 7923244Swnj sc->sc_nxrec = a + 1; 7931900Swnj } 7941918Swnj 7953244Swnj tsreset(uban) 7963244Swnj int uban; 7971918Swnj { 7983244Swnj register struct uba_ctlr *um; 7993244Swnj register ts11; 8001918Swnj 8013244Swnj for (ts11 = 0; ts11 < NTS; ts11++) { 8023244Swnj if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 || 8033244Swnj um->um_ubanum != uban) 8043244Swnj continue; 8053244Swnj printf(" ts%d", ts11); 8063244Swnj um->um_tab.b_active = 0; 8073244Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8083244Swnj ts_softc[ts11].sc_openf = -1; 8093244Swnj if (um->um_ubinfo) { 8103244Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8113244Swnj ubadone(um); 8123244Swnj } 8133244Swnj tsinit(ts11); 8143244Swnj tsstart(um); 8151918Swnj } 8161918Swnj } 8171918Swnj 8183244Swnj /*ARGSUSED*/ 8193244Swnj tsioctl(dev, cmd, addr, flag) 8203244Swnj caddr_t addr; 8213244Swnj dev_t dev; 8221918Swnj { 8233244Swnj int tsunit = TSUNIT(dev); 8243244Swnj register struct ts_softc *sc = &ts_softc[tsunit]; 8253244Swnj register struct buf *bp = &ctsbuf[TSUNIT(dev)]; 8263244Swnj register callcount; 8273244Swnj int fcount; 8283244Swnj struct mtop mtop; 8293244Swnj struct mtget mtget; 8303244Swnj /* we depend of the values and order of the MT codes here */ 8313244Swnj static tsops[] = 8323656Swnj {TS_WEOF,TS_SFORWF,TS_SREVF,TS_SFORW,TS_SREV,TS_REW,TS_OFFL,TS_SENSE}; 8331918Swnj 8343244Swnj switch (cmd) { 8353244Swnj case MTIOCTOP: /* tape operation */ 8363244Swnj if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) { 8373244Swnj u.u_error = EFAULT; 8383244Swnj return; 8393244Swnj } 8403244Swnj switch(mtop.mt_op) { 8413244Swnj case MTWEOF: 8423244Swnj callcount = mtop.mt_count; 8433244Swnj fcount = 1; 8443244Swnj break; 8453244Swnj case MTFSF: case MTBSF: 8463244Swnj case MTFSR: case MTBSR: 8473244Swnj callcount = 1; 8483244Swnj fcount = mtop.mt_count; 8493244Swnj break; 8503244Swnj case MTREW: case MTOFFL: case MTNOP: 8513244Swnj callcount = 1; 8523244Swnj fcount = 1; 8533244Swnj break; 8543244Swnj default: 8553244Swnj u.u_error = ENXIO; 8563244Swnj return; 8573244Swnj } 8583244Swnj if (callcount <= 0 || fcount <= 0) { 8593244Swnj u.u_error = ENXIO; 8603244Swnj return; 8613244Swnj } 8623244Swnj while (--callcount >= 0) { 8633244Swnj tscommand(dev, tsops[mtop.mt_op], fcount); 8643244Swnj if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) && 8653244Swnj bp->b_resid) { 8663244Swnj u.u_error = EIO; 8673244Swnj break; 8683244Swnj } 8693244Swnj if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT) 8703244Swnj break; 8713244Swnj } 8723244Swnj geterror(bp); 8733244Swnj return; 8743244Swnj case MTIOCGET: 8753244Swnj mtget.mt_dsreg = 0; 8763244Swnj mtget.mt_erreg = sc->sc_sts.s_xs0; 8773244Swnj mtget.mt_resid = sc->sc_resid; 8783480Stoy mtget.mt_type = MT_ISTS; 8793244Swnj if (copyout((caddr_t)&mtget, addr, sizeof(mtget))) 8803244Swnj u.u_error = EFAULT; 8813244Swnj return; 8823244Swnj default: 8833244Swnj u.u_error = ENXIO; 8843244Swnj } 8851918Swnj } 8861918Swnj 8873244Swnj #define DBSIZE 20 8883244Swnj 8893244Swnj tsdump() 8901918Swnj { 8913244Swnj register struct uba_device *ui; 8923244Swnj register struct uba_regs *up; 8933244Swnj register struct device *addr; 8943244Swnj int blk, num; 8953244Swnj int start; 8961918Swnj 8973244Swnj start = 0; 8983244Swnj num = maxfree; 8993244Swnj #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9003244Swnj if (tsdinfo[0] == 0) 9013244Swnj return (ENXIO); 9023244Swnj ui = phys(tsdinfo[0], struct uba_device *); 9033244Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9043327Swnj ubainit(up); 9053244Swnj DELAY(1000000); 9063244Swnj addr = (struct device *)ui->ui_physaddr; 9073244Swnj addr->tssr = 0; 9083244Swnj tswait(addr); 9093244Swnj while (num > 0) { 9103244Swnj blk = num > DBSIZE ? DBSIZE : num; 9113244Swnj tsdwrite(start, blk, addr, up); 9123244Swnj start += blk; 9133244Swnj num -= blk; 9143244Swnj } 9153244Swnj tseof(addr); 9163244Swnj tseof(addr); 9173244Swnj tswait(addr); 9183244Swnj if (addr->tssr&TS_SC) 9193244Swnj return (EIO); 9203244Swnj addr->tssr = 0; 9213244Swnj tswait(addr); 9223244Swnj return (0); 9231918Swnj } 9241918Swnj 9253244Swnj tsdwrite(dbuf, num, addr, up) 9263244Swnj register dbuf, num; 9273244Swnj register struct device *addr; 9283244Swnj struct uba_regs *up; 9291918Swnj { 9303244Swnj register struct pte *io; 9313244Swnj register int npf; 9321918Swnj 9333244Swnj tswait(addr); 9343244Swnj io = up->uba_map; 9353244Swnj npf = num+1; 9363244Swnj while (--npf != 0) 9373244Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9383244Swnj *(int *)io = 0; 9393244Swnj #ifdef notyet 9403244Swnj addr->tsbc = -(num*NBPG); 9413244Swnj addr->tsba = 0; 9423244Swnj addr->tscs = TS_WCOM | TM_GO; 9433244Swnj #endif 9441918Swnj } 9451918Swnj 9463244Swnj tswait(addr) 9473244Swnj register struct device *addr; 9481918Swnj { 9493244Swnj register s; 9501918Swnj 9513244Swnj do 9523244Swnj s = addr->tssr; 9533244Swnj while ((s & TS_SSR) == 0); 9541918Swnj } 9551918Swnj 9563244Swnj tseof(addr) 9573244Swnj struct device *addr; 9581918Swnj { 9591918Swnj 9603244Swnj tswait(addr); 9613244Swnj #ifdef notyet 9623244Swnj addr->tscs = TS_WEOF | TM_GO; 9633244Swnj #endif 9641918Swnj } 9651900Swnj #endif 966