123344Smckusick /* 223344Smckusick * Copyright (c) 1982 Regents of the University of California. 323344Smckusick * All rights reserved. The Berkeley software License Agreement 423344Smckusick * specifies the terms and conditions for redistribution. 523344Smckusick * 6*24974Smckusick * @(#)tm.c 6.7 (Berkeley) 09/20/85 723344Smckusick */ 81919Swnj 92709Swnj #include "te.h" 103519Sroot #include "ts.h" 116343Swnj #if NTE > 0 121919Swnj /* 132630Swnj * TM11/TE10 tape driver 142471Swnj * 153095Swnj * TODO: 163095Swnj * test driver with more than one slave 173095Swnj * test driver with more than one controller 183095Swnj * test reset code 193095Swnj * what happens if you offline tape during rewind? 203095Swnj * test using file system on tape 211919Swnj */ 229778Ssam #include "../machine/pte.h" 239778Ssam 2417079Sbloom #include "param.h" 2517079Sbloom #include "systm.h" 2617079Sbloom #include "buf.h" 2717079Sbloom #include "dir.h" 2817079Sbloom #include "conf.h" 2917079Sbloom #include "user.h" 3017079Sbloom #include "file.h" 3117079Sbloom #include "map.h" 3217079Sbloom #include "vm.h" 3317079Sbloom #include "ioctl.h" 3417079Sbloom #include "mtio.h" 3517079Sbloom #include "cmap.h" 3617079Sbloom #include "uio.h" 3717079Sbloom #include "kernel.h" 3818321Sralph #include "tty.h" 391919Swnj 408479Sroot #include "../vax/cpu.h" 4117079Sbloom #include "ubareg.h" 4217079Sbloom #include "ubavar.h" 4317079Sbloom #include "tmreg.h" 441919Swnj 453095Swnj /* 463095Swnj * There is a ctmbuf per tape controller. 473095Swnj * It is used as the token to pass to the internal routines 483095Swnj * to execute tape ioctls, and also acts as a lock on the slaves 493095Swnj * on the controller, since there is only one per controller. 503095Swnj * In particular, when the tape is rewinding on close we release 513095Swnj * the user process but any further attempts to use the tape drive 523095Swnj * before the rewind completes will hang waiting for ctmbuf. 533095Swnj */ 543095Swnj struct buf ctmbuf[NTM]; 551919Swnj 563095Swnj /* 573095Swnj * Raw tape operations use rtmbuf. The driver 583095Swnj * notices when rtmbuf is being used and allows the user 593095Swnj * program to continue after errors and read records 603095Swnj * not of the standard length (BSIZE). 613095Swnj */ 623095Swnj struct buf rtmbuf[NTM]; 633095Swnj 643095Swnj /* 653095Swnj * Driver unibus interface routines and variables. 663095Swnj */ 672608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 682982Swnj struct uba_ctlr *tmminfo[NTM]; 693095Swnj struct uba_device *tedinfo[NTE]; 703095Swnj struct buf teutab[NTE]; 713095Swnj short tetotm[NTE]; 722458Swnj u_short tmstd[] = { 0772520, 0 }; 732396Swnj struct uba_driver tmdriver = 743095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 751919Swnj 761919Swnj /* bits in minor device */ 773095Swnj #define TEUNIT(dev) (minor(dev)&03) 783095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 791919Swnj #define T_NOREWIND 04 80*24974Smckusick #define T_1600BPI 0x8 811919Swnj 821919Swnj #define INF (daddr_t)1000000L 831919Swnj 842608Swnj /* 852608Swnj * Software state per tape transport. 863095Swnj * 873095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 883095Swnj * 2. We keep track of the current position on a block tape and seek 893095Swnj * before operations by forward/back spacing if necessary. 903095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 913095Swnj * is open read write and the last thing done is a write we can 923095Swnj * write a standard end of tape mark (two eofs). 933095Swnj * 4. We remember the status registers after the last command, using 943095Swnj * then internally and returning them to the SENSE ioctl. 953095Swnj * 5. We remember the last density the tape was used at. If it is 963095Swnj * not a BOT when we start using it and we are writing, we don't 973095Swnj * let the density be changed. 982608Swnj */ 993095Swnj struct te_softc { 1002608Swnj char sc_openf; /* lock against multiple opens */ 1012608Swnj char sc_lastiow; /* last op was a write */ 1022608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 1033095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 1042608Swnj u_short sc_erreg; /* copy of last erreg */ 1052608Swnj u_short sc_dsreg; /* copy of last dsreg */ 1062608Swnj short sc_resid; /* copy of last bc */ 1073105Swnj #ifdef unneeded 1082670Swnj short sc_lastcmd; /* last command to handle direction changes */ 1092928Swnj #endif 1103095Swnj u_short sc_dens; /* prototype command with density info */ 11118321Sralph short sc_tact; /* timeout is active */ 1123495Sroot daddr_t sc_timo; /* time until timeout expires */ 11318321Sralph struct tty *sc_ttyp; /* record user's tty for errors */ 1146952Swnj } te_softc[NTE]; 1153105Swnj #ifdef unneeded 1163105Swnj int tmgapsdcnt; /* DEBUG */ 1173105Swnj #endif 1181919Swnj 1192608Swnj /* 1203095Swnj * States for um->um_tab.b_active, the per controller state flag. 1213095Swnj * This is used to sequence control in the driver. 1222608Swnj */ 1231919Swnj #define SSEEK 1 /* seeking */ 1241919Swnj #define SIO 2 /* doing seq i/o */ 1251919Swnj #define SCOM 3 /* sending control command */ 1262608Swnj #define SREW 4 /* sending a drive rewind */ 1271919Swnj 1282426Skre /* 1292426Skre * Determine if there is a controller for 1302426Skre * a tm at address reg. Our goal is to make the 1312426Skre * device interrupt. 1322426Skre */ 1332608Swnj tmprobe(reg) 1342396Swnj caddr_t reg; 1352396Swnj { 1363095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1372426Skre 1382608Swnj #ifdef lint 1393105Swnj br = 0; cvec = br; br = cvec; 1404936Swnj tmintr(0); 1412608Swnj #endif 1425692Sroot ((struct tmdevice *)reg)->tmcs = TM_IE; 1432396Swnj /* 1442630Swnj * If this is a tm11, it ought to have interrupted 1452396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1462458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1472458Swnj * Just in case, we will reference one 1482396Swnj * of the more distant registers, and hope for a machine 1492458Swnj * check, or similar disaster if this is a ts. 1502471Swnj * 1512471Swnj * Note: on an 11/780, badaddr will just generate 1522471Swnj * a uba error for a ts; but our caller will notice that 1532471Swnj * so we won't check for it. 1542396Swnj */ 1555692Sroot if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2)) 1562458Swnj return (0); 1577404Skre return (sizeof (struct tmdevice)); 1582396Swnj } 1592396Swnj 1602608Swnj /* 1612608Swnj * Due to a design flaw, we cannot ascertain if the tape 1622608Swnj * exists or not unless it is on line - ie: unless a tape is 1632608Swnj * mounted. This is too servere a restriction to bear, 1642608Swnj * so all units are assumed to exist. 1652608Swnj */ 1662608Swnj /*ARGSUSED*/ 1672574Swnj tmslave(ui, reg) 1682982Swnj struct uba_device *ui; 1692396Swnj caddr_t reg; 1702396Swnj { 1712458Swnj 1722458Swnj return (1); 1732396Swnj } 1742396Swnj 1752608Swnj /* 1763095Swnj * Record attachment of the unit to the controller. 1772608Swnj */ 1782608Swnj /*ARGSUSED*/ 1792608Swnj tmattach(ui) 1802982Swnj struct uba_device *ui; 1812608Swnj { 1823095Swnj /* 1833095Swnj * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf 1843095Swnj * arrays given a te unit number. 1853095Swnj */ 1863095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1872608Swnj } 1882608Swnj 1893495Sroot int tmtimer(); 1902608Swnj /* 1912608Swnj * Open the device. Tapes are unique open 1922608Swnj * devices, so we refuse if it is already open. 1932608Swnj * We also check that a tape is available, and 1943095Swnj * don't block waiting here; if you want to wait 1953095Swnj * for a tape you should timeout in user code. 1962608Swnj */ 197*24974Smckusick 198*24974Smckusick #ifdef AVIV 199*24974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 }; 200*24974Smckusick int tmdiag; 201*24974Smckusick #endif AVIV 202*24974Smckusick 2031919Swnj tmopen(dev, flag) 2041919Swnj dev_t dev; 2051919Swnj int flag; 2061919Swnj { 2073095Swnj register int teunit; 2082982Swnj register struct uba_device *ui; 2093095Swnj register struct te_softc *sc; 2103209Swnj int olddens, dens; 2115437Sroot int s; 2121919Swnj 2133095Swnj teunit = TEUNIT(dev); 2143095Swnj if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf || 2158574Sroot (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2168574Sroot return (ENXIO); 2173209Swnj olddens = sc->sc_dens; 2183209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 219*24974Smckusick #ifndef AVIV 2203209Swnj if ((minor(dev) & T_1600BPI) == 0) 2213209Swnj dens |= TM_D800; 222*24974Smckusick #else AVIV 223*24974Smckusick dens |= tmdens[(minor(dev)>>3)&03]; 224*24974Smckusick #endif AVIV 2253209Swnj sc->sc_dens = dens; 2263141Swnj get: 2272608Swnj tmcommand(dev, TM_SENSE, 1); 2283141Swnj if (sc->sc_erreg&TMER_SDWN) { 2293141Swnj sleep((caddr_t)&lbolt, PZERO+1); 2303141Swnj goto get; 2313141Swnj } 2323209Swnj sc->sc_dens = olddens; 2333710Sroot if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) { 2343710Sroot uprintf("te%d: not online\n", teunit); 2358574Sroot return (EIO); 2361919Swnj } 2373710Sroot if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) { 2383710Sroot uprintf("te%d: no write ring\n", teunit); 2398574Sroot return (EIO); 2403710Sroot } 2413710Sroot if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2423710Sroot dens != sc->sc_dens) { 2433710Sroot uprintf("te%d: can't change density in mid-tape\n", teunit); 2448574Sroot return (EIO); 2453710Sroot } 2462608Swnj sc->sc_openf = 1; 2472471Swnj sc->sc_blkno = (daddr_t)0; 2482471Swnj sc->sc_nxrec = INF; 2492608Swnj sc->sc_lastiow = 0; 2503095Swnj sc->sc_dens = dens; 25118321Sralph sc->sc_ttyp = u.u_ttyp; 2525437Sroot s = spl6(); 2533495Sroot if (sc->sc_tact == 0) { 2543495Sroot sc->sc_timo = INF; 2553495Sroot sc->sc_tact = 1; 2563629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 2573495Sroot } 2585437Sroot splx(s); 2598574Sroot return (0); 2601919Swnj } 2611919Swnj 2622608Swnj /* 2632608Swnj * Close tape device. 2642608Swnj * 2652608Swnj * If tape was open for writing or last operation was 2662608Swnj * a write, then write two EOF's and backspace over the last one. 2672608Swnj * Unless this is a non-rewinding special file, rewind the tape. 2682608Swnj * Make the tape available to others. 2692608Swnj */ 2701919Swnj tmclose(dev, flag) 2711919Swnj register dev_t dev; 2721919Swnj register flag; 2731919Swnj { 2743095Swnj register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 2751919Swnj 2762608Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2772608Swnj tmcommand(dev, TM_WEOF, 1); 2782608Swnj tmcommand(dev, TM_WEOF, 1); 2792608Swnj tmcommand(dev, TM_SREV, 1); 2801919Swnj } 2811919Swnj if ((minor(dev)&T_NOREWIND) == 0) 2823095Swnj /* 2833095Swnj * 0 count means don't hang waiting for rewind complete 2843095Swnj * rather ctmbuf stays busy until the operation completes 2853095Swnj * preventing further opens from completing by 2863095Swnj * preventing a TM_SENSE from completing. 2873095Swnj */ 2883095Swnj tmcommand(dev, TM_REW, 0); 2892471Swnj sc->sc_openf = 0; 2901919Swnj } 2911919Swnj 2922608Swnj /* 2932608Swnj * Execute a command on the tape drive 2942608Swnj * a specified number of times. 2952608Swnj */ 2962574Swnj tmcommand(dev, com, count) 2971919Swnj dev_t dev; 2981919Swnj int com, count; 2991919Swnj { 3001919Swnj register struct buf *bp; 3015437Sroot register int s; 3021919Swnj 3032608Swnj bp = &ctmbuf[TMUNIT(dev)]; 3045437Sroot s = spl5(); 3051919Swnj while (bp->b_flags&B_BUSY) { 3063095Swnj /* 3073095Swnj * This special check is because B_BUSY never 3083095Swnj * gets cleared in the non-waiting rewind case. 3093095Swnj */ 3103141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3113095Swnj break; 3121919Swnj bp->b_flags |= B_WANTED; 3131919Swnj sleep((caddr_t)bp, PRIBIO); 3141919Swnj } 3151919Swnj bp->b_flags = B_BUSY|B_READ; 3165437Sroot splx(s); 3171919Swnj bp->b_dev = dev; 3181919Swnj bp->b_repcnt = -count; 3191919Swnj bp->b_command = com; 3201919Swnj bp->b_blkno = 0; 3211919Swnj tmstrategy(bp); 3223095Swnj /* 3233095Swnj * In case of rewind from close, don't wait. 3243095Swnj * This is the only case where count can be 0. 3253095Swnj */ 3263095Swnj if (count == 0) 3273095Swnj return; 3281919Swnj iowait(bp); 3291919Swnj if (bp->b_flags&B_WANTED) 3301919Swnj wakeup((caddr_t)bp); 3311919Swnj bp->b_flags &= B_ERROR; 3321919Swnj } 3331919Swnj 3342608Swnj /* 3353095Swnj * Queue a tape operation. 3362608Swnj */ 3371919Swnj tmstrategy(bp) 3381919Swnj register struct buf *bp; 3391919Swnj { 3403095Swnj int teunit = TEUNIT(bp->b_dev); 3412982Swnj register struct uba_ctlr *um; 3422608Swnj register struct buf *dp; 3435437Sroot int s; 3441919Swnj 3452608Swnj /* 3462608Swnj * Put transfer at end of unit queue 3472608Swnj */ 3483095Swnj dp = &teutab[teunit]; 3491919Swnj bp->av_forw = NULL; 3505437Sroot s = spl5(); 3513939Sbugs um = tedinfo[teunit]->ui_mi; 3522608Swnj if (dp->b_actf == NULL) { 3532608Swnj dp->b_actf = bp; 3542608Swnj /* 3552608Swnj * Transport not already active... 3562608Swnj * put at end of controller queue. 3572608Swnj */ 3582608Swnj dp->b_forw = NULL; 3592608Swnj if (um->um_tab.b_actf == NULL) 3602608Swnj um->um_tab.b_actf = dp; 3612608Swnj else 3622608Swnj um->um_tab.b_actl->b_forw = dp; 3632608Swnj um->um_tab.b_actl = dp; 3642608Swnj } else 3652608Swnj dp->b_actl->av_forw = bp; 3662608Swnj dp->b_actl = bp; 3672608Swnj /* 3682608Swnj * If the controller is not busy, get 3692608Swnj * it going. 3702608Swnj */ 3712608Swnj if (um->um_tab.b_active == 0) 3722608Swnj tmstart(um); 3735437Sroot splx(s); 3741919Swnj } 3751919Swnj 3762608Swnj /* 3772608Swnj * Start activity on a tm controller. 3782608Swnj */ 3792608Swnj tmstart(um) 3802982Swnj register struct uba_ctlr *um; 3811919Swnj { 3822608Swnj register struct buf *bp, *dp; 3835692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3843095Swnj register struct te_softc *sc; 3852982Swnj register struct uba_device *ui; 3863095Swnj int teunit, cmd; 3872471Swnj daddr_t blkno; 3881919Swnj 3892608Swnj /* 3902608Swnj * Look for an idle transport on the controller. 3912608Swnj */ 3921919Swnj loop: 3932608Swnj if ((dp = um->um_tab.b_actf) == NULL) 3941919Swnj return; 3952608Swnj if ((bp = dp->b_actf) == NULL) { 3962608Swnj um->um_tab.b_actf = dp->b_forw; 3972608Swnj goto loop; 3982608Swnj } 3993095Swnj teunit = TEUNIT(bp->b_dev); 4003095Swnj ui = tedinfo[teunit]; 4012608Swnj /* 4022608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 4032608Swnj */ 4043095Swnj sc = &te_softc[teunit]; 4055692Sroot addr = (struct tmdevice *)um->um_addr; 4062608Swnj addr->tmcs = (ui->ui_slave << 8); 4072471Swnj sc->sc_dsreg = addr->tmcs; 4082471Swnj sc->sc_erreg = addr->tmer; 4092471Swnj sc->sc_resid = addr->tmbc; 4102608Swnj /* 4112608Swnj * Default is that last command was NOT a write command; 4122608Swnj * if we do a write command we will notice this in tmintr(). 4132608Swnj */ 4143493Sroot sc->sc_lastiow = 0; 4152608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4162608Swnj /* 4173095Swnj * Have had a hard error on a non-raw tape 4183095Swnj * or the tape unit is now unavailable 4193095Swnj * (e.g. taken off line). 4202608Swnj */ 4212608Swnj bp->b_flags |= B_ERROR; 4221919Swnj goto next; 4231919Swnj } 4243095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4253095Swnj /* 4263095Swnj * Execute control operation with the specified count. 4273095Swnj */ 4282608Swnj if (bp->b_command == TM_SENSE) 4292608Swnj goto next; 4303495Sroot /* 4313495Sroot * Set next state; give 5 minutes to complete 4323495Sroot * rewind, or 10 seconds per iteration (minimum 60 4333629Sroot * seconds and max 5 minutes) to complete other ops. 4343495Sroot */ 4353495Sroot if (bp->b_command == TM_REW) { 4363495Sroot um->um_tab.b_active = SREW; 4373495Sroot sc->sc_timo = 5 * 60; 4383495Sroot } else { 4393495Sroot um->um_tab.b_active = SCOM; 4404266Swnj sc->sc_timo = 4414266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4423495Sroot } 4432608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4442608Swnj addr->tmbc = bp->b_repcnt; 4452670Swnj goto dobpcmd; 4462608Swnj } 4472608Swnj /* 4483095Swnj * The following checks handle boundary cases for operation 4493095Swnj * on non-raw tapes. On raw tapes the initialization of 4503095Swnj * sc->sc_nxrec by tmphys causes them to be skipped normally 4513095Swnj * (except in the case of retries). 4523095Swnj */ 4537381Ssam if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4543095Swnj /* 4553095Swnj * Can't read past known end-of-file. 4563095Swnj */ 4573095Swnj bp->b_flags |= B_ERROR; 4583095Swnj bp->b_error = ENXIO; 4593095Swnj goto next; 4603095Swnj } 4617381Ssam if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 4623095Swnj bp->b_flags&B_READ) { 4633095Swnj /* 4643095Swnj * Reading at end of file returns 0 bytes. 4653095Swnj */ 4663095Swnj bp->b_resid = bp->b_bcount; 4673095Swnj clrbuf(bp); 4683095Swnj goto next; 4693095Swnj } 4703095Swnj if ((bp->b_flags&B_READ) == 0) 4713095Swnj /* 4723095Swnj * Writing sets EOF 4733095Swnj */ 4747381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4753095Swnj /* 4762608Swnj * If the data transfer command is in the correct place, 4772608Swnj * set up all the registers except the csr, and give 4782608Swnj * control over to the UNIBUS adapter routines, to 4792608Swnj * wait for resources to start the i/o. 4802608Swnj */ 4817381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4822396Swnj addr->tmbc = -bp->b_bcount; 4831919Swnj if ((bp->b_flags&B_READ) == 0) { 4842471Swnj if (um->um_tab.b_errcnt) 4853095Swnj cmd = TM_WIRG; 4861919Swnj else 4873095Swnj cmd = TM_WCOM; 4881919Swnj } else 4893095Swnj cmd = TM_RCOM; 4902471Swnj um->um_tab.b_active = SIO; 4913095Swnj um->um_cmd = sc->sc_dens|cmd; 4922928Swnj #ifdef notdef 4932670Swnj if (tmreverseop(sc->sc_lastcmd)) 4943095Swnj while (addr->tmer & TMER_SDWN) 495*24974Smckusick DELAY(10),tmgapsdcnt++; 4962670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 4972928Swnj #endif 4983495Sroot sc->sc_timo = 60; /* premature, but should serve */ 4993105Swnj (void) ubago(ui); 5001919Swnj return; 5011919Swnj } 5022608Swnj /* 5033095Swnj * Tape positioned incorrectly; 5043095Swnj * set to seek forwards or backwards to the correct spot. 5053095Swnj * This happens for raw tapes only on error retries. 5062608Swnj */ 5072471Swnj um->um_tab.b_active = SSEEK; 5087381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5092670Swnj bp->b_command = TM_SFORW; 5107381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5111919Swnj } else { 5122670Swnj bp->b_command = TM_SREV; 5137381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5141919Swnj } 5153629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5162670Swnj dobpcmd: 5172928Swnj #ifdef notdef 5183095Swnj /* 5193095Swnj * It is strictly necessary to wait for the tape 5203095Swnj * to stop before changing directions, but the TC11 5213095Swnj * handles this for us. 5223095Swnj */ 5232670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5242670Swnj while (addr->tmer & TM_SDWN) 525*24974Smckusick DELAY(10),tmgapsdcnt++; 5262670Swnj sc->sc_lastcmd = bp->b_command; 5272928Swnj #endif 5283095Swnj /* 5293095Swnj * Do the command in bp. 5303095Swnj */ 5313095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5321919Swnj return; 5331919Swnj 5341919Swnj next: 5352608Swnj /* 5362608Swnj * Done with this operation due to error or 5372608Swnj * the fact that it doesn't do anything. 5382608Swnj * Release UBA resources (if any), dequeue 5392608Swnj * the transfer and continue processing this slave. 5402608Swnj */ 5412608Swnj if (um->um_ubinfo) 5422617Swnj ubadone(um); 5432608Swnj um->um_tab.b_errcnt = 0; 5442608Swnj dp->b_actf = bp->av_forw; 5451919Swnj iodone(bp); 5461919Swnj goto loop; 5471919Swnj } 5481919Swnj 5492608Swnj /* 5502608Swnj * The UNIBUS resources we needed have been 5512608Swnj * allocated to us; start the device. 5522608Swnj */ 5532574Swnj tmdgo(um) 5542982Swnj register struct uba_ctlr *um; 5551919Swnj { 5565692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5572471Swnj 5582574Swnj addr->tmba = um->um_ubinfo; 5592574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5602396Swnj } 5612396Swnj 5622608Swnj /* 5632608Swnj * Tm interrupt routine. 5642608Swnj */ 5652471Swnj /*ARGSUSED*/ 5662630Swnj tmintr(tm11) 5672630Swnj int tm11; 5682396Swnj { 5692608Swnj struct buf *dp; 5701919Swnj register struct buf *bp; 5712982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5725692Sroot register struct tmdevice *addr; 5733095Swnj register struct te_softc *sc; 5743095Swnj int teunit; 5751919Swnj register state; 5761919Swnj 5773095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5783095Swnj return; 5793095Swnj bp = dp->b_actf; 5803095Swnj teunit = TEUNIT(bp->b_dev); 5815692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5823524Swnj sc = &te_softc[teunit]; 5832608Swnj /* 5842608Swnj * If last command was a rewind, and tape is still 5852608Swnj * rewinding, wait for the rewind complete interrupt. 5862608Swnj */ 5872608Swnj if (um->um_tab.b_active == SREW) { 5882608Swnj um->um_tab.b_active = SCOM; 5893524Swnj if (addr->tmer&TMER_RWS) { 5903524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 5912608Swnj return; 5923524Swnj } 5931919Swnj } 5942608Swnj /* 5952608Swnj * An operation completed... record status 5962608Swnj */ 5973495Sroot sc->sc_timo = INF; 5982471Swnj sc->sc_dsreg = addr->tmcs; 5992471Swnj sc->sc_erreg = addr->tmer; 6002471Swnj sc->sc_resid = addr->tmbc; 6011919Swnj if ((bp->b_flags & B_READ) == 0) 6022608Swnj sc->sc_lastiow = 1; 6032471Swnj state = um->um_tab.b_active; 6042471Swnj um->um_tab.b_active = 0; 6052608Swnj /* 6062608Swnj * Check for errors. 6072608Swnj */ 6082608Swnj if (addr->tmcs&TM_ERR) { 6093095Swnj while (addr->tmer & TMER_SDWN) 610*24974Smckusick DELAY(10); /* await settle down */ 6112608Swnj /* 6123095Swnj * If we hit the end of the tape file, update our position. 6132608Swnj */ 6143095Swnj if (addr->tmer&TMER_EOF) { 6152608Swnj tmseteof(bp); /* set blkno and nxrec */ 6162608Swnj state = SCOM; /* force completion */ 6172608Swnj /* 6182608Swnj * Stuff bc so it will be unstuffed correctly 6192608Swnj * later to get resid. 6202608Swnj */ 6212396Swnj addr->tmbc = -bp->b_bcount; 6222608Swnj goto opdone; 6231919Swnj } 6242608Swnj /* 6253095Swnj * If we were reading raw tape and the only error was that the 6263095Swnj * record was too long, then we don't consider this an error. 6272608Swnj */ 6283095Swnj if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6293095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6302608Swnj goto ignoreerr; 6312608Swnj /* 6322608Swnj * If error is not hard, and this was an i/o operation 6332608Swnj * retry up to 8 times. 6342608Swnj */ 6353095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6362471Swnj if (++um->um_tab.b_errcnt < 7) { 6372471Swnj sc->sc_blkno++; 6382617Swnj ubadone(um); 6392608Swnj goto opcont; 6401919Swnj } 6412608Swnj } else 6422608Swnj /* 6432608Swnj * Hard or non-i/o errors on non-raw tape 6442608Swnj * cause it to close. 6452608Swnj */ 6463095Swnj if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)]) 6472608Swnj sc->sc_openf = -1; 6482608Swnj /* 6492608Swnj * Couldn't recover error 6502608Swnj */ 65118321Sralph tprintf(sc->sc_ttyp, 65218321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6533095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 654*24974Smckusick #ifdef AVIV 655*24974Smckusick if (tmdiag) { 656*24974Smckusick addr->tmmr = DAB; 657*24974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 658*24974Smckusick addr->tmmr = DTS; 659*24974Smckusick if (addr->tmmr & DTS_MASK) 660*24974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 661*24974Smckusick addr->tmmr = RWERR; 662*24974Smckusick printf(", read/write errors %b\n", 663*24974Smckusick addr->tmmr & RWERR_MASK, 664*24974Smckusick RWERR_BITS); 665*24974Smckusick addr->tmmr = DRSENSE; 666*24974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 667*24974Smckusick DRSENSE_BITS); 668*24974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 669*24974Smckusick } 670*24974Smckusick #endif AVIV 6711919Swnj bp->b_flags |= B_ERROR; 6722608Swnj goto opdone; 6731919Swnj } 6742608Swnj /* 6752608Swnj * Advance tape control FSM. 6762608Swnj */ 6772608Swnj ignoreerr: 6781919Swnj switch (state) { 6791919Swnj 6801919Swnj case SIO: 6812608Swnj /* 6822608Swnj * Read/write increments tape block number 6832608Swnj */ 6842471Swnj sc->sc_blkno++; 6852608Swnj goto opdone; 6861919Swnj 6871919Swnj case SCOM: 6882608Swnj /* 6893095Swnj * For forward/backward space record update current position. 6902608Swnj */ 6913095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 6922608Swnj switch (bp->b_command) { 6931919Swnj 6942608Swnj case TM_SFORW: 6952608Swnj sc->sc_blkno -= bp->b_repcnt; 6963095Swnj break; 6971919Swnj 6982608Swnj case TM_SREV: 6992608Swnj sc->sc_blkno += bp->b_repcnt; 7003095Swnj break; 7011919Swnj } 7023095Swnj goto opdone; 7031919Swnj 7041919Swnj case SSEEK: 7057381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7062608Swnj goto opcont; 7071919Swnj 7081919Swnj default: 7092608Swnj panic("tmintr"); 7102608Swnj } 7112608Swnj opdone: 7122608Swnj /* 7132608Swnj * Reset error count and remove 7142608Swnj * from device queue. 7152608Swnj */ 7162608Swnj um->um_tab.b_errcnt = 0; 7172608Swnj dp->b_actf = bp->av_forw; 71814929Skarels /* 71914929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 72014929Skarels */ 72115081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7222617Swnj ubadone(um); 7232608Swnj iodone(bp); 7242608Swnj /* 7252608Swnj * Circulate slave to end of controller 7262608Swnj * queue to give other slaves a chance. 7272608Swnj */ 7282608Swnj um->um_tab.b_actf = dp->b_forw; 7292608Swnj if (dp->b_actf) { 7302608Swnj dp->b_forw = NULL; 7312608Swnj if (um->um_tab.b_actf == NULL) 7322608Swnj um->um_tab.b_actf = dp; 7332608Swnj else 7342608Swnj um->um_tab.b_actl->b_forw = dp; 7352608Swnj um->um_tab.b_actl = dp; 7362608Swnj } 7372608Swnj if (um->um_tab.b_actf == 0) 7381919Swnj return; 7392608Swnj opcont: 7402608Swnj tmstart(um); 7411919Swnj } 7421919Swnj 7433495Sroot tmtimer(dev) 7443495Sroot int dev; 7453495Sroot { 7463495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7474847Sroot register short x; 7483495Sroot 7493495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7504278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7513495Sroot sc->sc_timo = INF; 7524847Sroot x = spl5(); 7533495Sroot tmintr(TMUNIT(dev)); 7544847Sroot (void) splx(x); 7553495Sroot } 7563629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7573495Sroot } 7583495Sroot 7591919Swnj tmseteof(bp) 7601919Swnj register struct buf *bp; 7611919Swnj { 7623095Swnj register int teunit = TEUNIT(bp->b_dev); 7635692Sroot register struct tmdevice *addr = 7645692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7653095Swnj register struct te_softc *sc = &te_softc[teunit]; 7661919Swnj 7673095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7687381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7691919Swnj /* reversing */ 7707381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 7712471Swnj sc->sc_blkno = sc->sc_nxrec; 7721919Swnj } else { 7731919Swnj /* spacing forward */ 7747381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 7752471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7761919Swnj } 7771919Swnj return; 7781919Swnj } 7791919Swnj /* eof on read */ 7807381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 7811919Swnj } 7821919Swnj 7837732Sroot tmread(dev, uio) 7842608Swnj dev_t dev; 7857732Sroot struct uio *uio; 7861919Swnj { 7878163Sroot int errno; 7881919Swnj 7898163Sroot errno = tmphys(dev, uio); 7908163Sroot if (errno) 7918163Sroot return (errno); 7928163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio)); 7931919Swnj } 7941919Swnj 7957838Sroot tmwrite(dev, uio) 7962608Swnj dev_t dev; 7977838Sroot struct uio *uio; 7981919Swnj { 7998163Sroot int errno; 8001919Swnj 8018163Sroot errno = tmphys(dev, uio); 8028163Sroot if (errno) 8038163Sroot return (errno); 8048163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio)); 8051919Swnj } 8061919Swnj 8073095Swnj /* 8083095Swnj * Check that a raw device exists. 8093095Swnj * If it does, set up sc_blkno and sc_nxrec 8103095Swnj * so that the tape will appear positioned correctly. 8113095Swnj */ 8127732Sroot tmphys(dev, uio) 8132608Swnj dev_t dev; 8147732Sroot struct uio *uio; 8151919Swnj { 8163095Swnj register int teunit = TEUNIT(dev); 8171919Swnj register daddr_t a; 8183095Swnj register struct te_softc *sc; 8193095Swnj register struct uba_device *ui; 8201919Swnj 8217838Sroot if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) 8227732Sroot return (ENXIO); 8233095Swnj sc = &te_softc[teunit]; 8247838Sroot a = bdbtofsb(uio->uio_offset >> 9); 8252471Swnj sc->sc_blkno = a; 8262471Swnj sc->sc_nxrec = a + 1; 8277732Sroot return (0); 8281919Swnj } 8291919Swnj 8302608Swnj tmreset(uban) 8312608Swnj int uban; 8322608Swnj { 8332982Swnj register struct uba_ctlr *um; 8343095Swnj register tm11, teunit; 8352982Swnj register struct uba_device *ui; 8362608Swnj register struct buf *dp; 8372608Swnj 8382630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8392630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8402608Swnj um->um_ubanum != uban) 8412608Swnj continue; 8422928Swnj printf(" tm%d", tm11); 8432608Swnj um->um_tab.b_active = 0; 8442608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8452608Swnj if (um->um_ubinfo) { 8462608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8479355Ssam um->um_ubinfo = 0; 8482608Swnj } 8495692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8503095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8513095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8523095Swnj ui->ui_alive == 0) 8532608Swnj continue; 8543095Swnj dp = &teutab[teunit]; 8552608Swnj dp->b_active = 0; 8562608Swnj dp->b_forw = 0; 8572608Swnj if (um->um_tab.b_actf == NULL) 8582608Swnj um->um_tab.b_actf = dp; 8592608Swnj else 8602608Swnj um->um_tab.b_actl->b_forw = dp; 8612608Swnj um->um_tab.b_actl = dp; 8623495Sroot if (te_softc[teunit].sc_openf > 0) 8633495Sroot te_softc[teunit].sc_openf = -1; 8642608Swnj } 8652608Swnj tmstart(um); 8662608Swnj } 8672608Swnj } 8682608Swnj 8691919Swnj /*ARGSUSED*/ 8707632Ssam tmioctl(dev, cmd, data, flag) 8717632Ssam caddr_t data; 8721919Swnj dev_t dev; 8731919Swnj { 8743095Swnj int teunit = TEUNIT(dev); 8753095Swnj register struct te_softc *sc = &te_softc[teunit]; 8763095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8771919Swnj register callcount; 8781919Swnj int fcount; 8797632Ssam struct mtop *mtop; 8807632Ssam struct mtget *mtget; 8811919Swnj /* we depend of the values and order of the MT codes here */ 8822608Swnj static tmops[] = 8832608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8841919Swnj 8852608Swnj switch (cmd) { 8867632Ssam 8877632Ssam case MTIOCTOP: /* tape operation */ 8887632Ssam mtop = (struct mtop *)data; 8898574Sroot switch (mtop->mt_op) { 8907632Ssam 8912608Swnj case MTWEOF: 8927632Ssam callcount = mtop->mt_count; 8932608Swnj fcount = 1; 8942608Swnj break; 8957632Ssam 8962608Swnj case MTFSF: case MTBSF: 8977632Ssam callcount = mtop->mt_count; 8981919Swnj fcount = INF; 8991919Swnj break; 9007632Ssam 9011919Swnj case MTFSR: case MTBSR: 9021919Swnj callcount = 1; 9037632Ssam fcount = mtop->mt_count; 9041919Swnj break; 9057632Ssam 9062324Skre case MTREW: case MTOFFL: case MTNOP: 9071919Swnj callcount = 1; 9081919Swnj fcount = 1; 9091919Swnj break; 9107632Ssam 9111919Swnj default: 9128574Sroot return (ENXIO); 9131919Swnj } 9148574Sroot if (callcount <= 0 || fcount <= 0) 9158574Sroot return (EINVAL); 9162608Swnj while (--callcount >= 0) { 9177632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 9187632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 9198574Sroot bp->b_resid) 9208574Sroot return (EIO); 9213095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9221919Swnj break; 9231919Swnj } 9248648Sroot return (geterror(bp)); 9257632Ssam 9261919Swnj case MTIOCGET: 9277632Ssam mtget = (struct mtget *)data; 9287632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9297632Ssam mtget->mt_erreg = sc->sc_erreg; 9307632Ssam mtget->mt_resid = sc->sc_resid; 9317632Ssam mtget->mt_type = MT_ISTM; 9328574Sroot break; 9337632Ssam 9341919Swnj default: 9358574Sroot return (ENXIO); 9361919Swnj } 9378574Sroot return (0); 9381919Swnj } 9391919Swnj 9401919Swnj #define DBSIZE 20 9411919Swnj 9422363Swnj tmdump() 9432363Swnj { 9442982Swnj register struct uba_device *ui; 9452396Swnj register struct uba_regs *up; 9465692Sroot register struct tmdevice *addr; 9472426Skre int blk, num; 9482426Skre int start; 9491919Swnj 9502426Skre start = 0; 9512426Skre num = maxfree; 9522426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9533095Swnj if (tedinfo[0] == 0) 9542887Swnj return (ENXIO); 9553095Swnj ui = phys(tedinfo[0], struct uba_device *); 9562396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9573331Swnj ubainit(up); 9582324Skre DELAY(1000000); 9595692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9602396Swnj tmwait(addr); 9612608Swnj addr->tmcs = TM_DCLR | TM_GO; 9621919Swnj while (num > 0) { 9631919Swnj blk = num > DBSIZE ? DBSIZE : num; 9642396Swnj tmdwrite(start, blk, addr, up); 9651919Swnj start += blk; 9661919Swnj num -= blk; 9671919Swnj } 9682426Skre tmeof(addr); 9692426Skre tmeof(addr); 9702426Skre tmwait(addr); 9712887Swnj if (addr->tmcs&TM_ERR) 9722887Swnj return (EIO); 9732608Swnj addr->tmcs = TM_REW | TM_GO; 9742471Swnj tmwait(addr); 9752363Swnj return (0); 9761919Swnj } 9771919Swnj 9782608Swnj tmdwrite(dbuf, num, addr, up) 9792608Swnj register dbuf, num; 9805692Sroot register struct tmdevice *addr; 9812396Swnj struct uba_regs *up; 9821919Swnj { 9832396Swnj register struct pte *io; 9842396Swnj register int npf; 9851928Swnj 9862396Swnj tmwait(addr); 9872396Swnj io = up->uba_map; 9881919Swnj npf = num+1; 9891928Swnj while (--npf != 0) 9902982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9912396Swnj *(int *)io = 0; 9922396Swnj addr->tmbc = -(num*NBPG); 9932396Swnj addr->tmba = 0; 9942608Swnj addr->tmcs = TM_WCOM | TM_GO; 9951919Swnj } 9961919Swnj 9972396Swnj tmwait(addr) 9985692Sroot register struct tmdevice *addr; 9991919Swnj { 10001928Swnj register s; 10011919Swnj 10021919Swnj do 10032396Swnj s = addr->tmcs; 10042608Swnj while ((s & TM_CUR) == 0); 10051919Swnj } 10061919Swnj 10072396Swnj tmeof(addr) 10085692Sroot struct tmdevice *addr; 10091919Swnj { 10101919Swnj 10112396Swnj tmwait(addr); 10122608Swnj addr->tmcs = TM_WEOF | TM_GO; 10131919Swnj } 10141919Swnj #endif 1015