123344Smckusick /* 229242Smckusick * Copyright (c) 1982, 1986 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*30917Skarels * @(#)tm.c 7.2 (Berkeley) 04/17/87 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 */ 2217079Sbloom #include "param.h" 2317079Sbloom #include "systm.h" 2417079Sbloom #include "buf.h" 2517079Sbloom #include "dir.h" 2617079Sbloom #include "conf.h" 2717079Sbloom #include "user.h" 2817079Sbloom #include "file.h" 2917079Sbloom #include "map.h" 3017079Sbloom #include "vm.h" 3117079Sbloom #include "ioctl.h" 3217079Sbloom #include "mtio.h" 3317079Sbloom #include "cmap.h" 3417079Sbloom #include "uio.h" 3517079Sbloom #include "kernel.h" 3618321Sralph #include "tty.h" 37*30917Skarels #include "syslog.h" 381919Swnj 39*30917Skarels #include "../machine/pte.h" 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 8024974Smckusick #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 */ 113*30917Skarels int sc_blks; /* number of I/O operations since open */ 114*30917Skarels int sc_softerrs; /* number of soft I/O errors since open */ 11518321Sralph struct tty *sc_ttyp; /* record user's tty for errors */ 1166952Swnj } te_softc[NTE]; 1173105Swnj #ifdef unneeded 1183105Swnj int tmgapsdcnt; /* DEBUG */ 1193105Swnj #endif 1201919Swnj 1212608Swnj /* 1223095Swnj * States for um->um_tab.b_active, the per controller state flag. 1233095Swnj * This is used to sequence control in the driver. 1242608Swnj */ 1251919Swnj #define SSEEK 1 /* seeking */ 1261919Swnj #define SIO 2 /* doing seq i/o */ 1271919Swnj #define SCOM 3 /* sending control command */ 1282608Swnj #define SREW 4 /* sending a drive rewind */ 1291919Swnj 1302426Skre /* 1312426Skre * Determine if there is a controller for 1322426Skre * a tm at address reg. Our goal is to make the 1332426Skre * device interrupt. 1342426Skre */ 1352608Swnj tmprobe(reg) 1362396Swnj caddr_t reg; 1372396Swnj { 1383095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1392426Skre 1402608Swnj #ifdef lint 1413105Swnj br = 0; cvec = br; br = cvec; 1424936Swnj tmintr(0); 1432608Swnj #endif 1445692Sroot ((struct tmdevice *)reg)->tmcs = TM_IE; 1452396Swnj /* 1462630Swnj * If this is a tm11, it ought to have interrupted 1472396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1482458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1492458Swnj * Just in case, we will reference one 1502396Swnj * of the more distant registers, and hope for a machine 1512458Swnj * check, or similar disaster if this is a ts. 1522471Swnj * 1532471Swnj * Note: on an 11/780, badaddr will just generate 1542471Swnj * a uba error for a ts; but our caller will notice that 1552471Swnj * so we won't check for it. 1562396Swnj */ 1575692Sroot if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2)) 1582458Swnj return (0); 1597404Skre return (sizeof (struct tmdevice)); 1602396Swnj } 1612396Swnj 1622608Swnj /* 1632608Swnj * Due to a design flaw, we cannot ascertain if the tape 1642608Swnj * exists or not unless it is on line - ie: unless a tape is 1652608Swnj * mounted. This is too servere a restriction to bear, 1662608Swnj * so all units are assumed to exist. 1672608Swnj */ 1682608Swnj /*ARGSUSED*/ 1692574Swnj tmslave(ui, reg) 1702982Swnj struct uba_device *ui; 1712396Swnj caddr_t reg; 1722396Swnj { 1732458Swnj 1742458Swnj return (1); 1752396Swnj } 1762396Swnj 1772608Swnj /* 1783095Swnj * Record attachment of the unit to the controller. 1792608Swnj */ 1802608Swnj /*ARGSUSED*/ 1812608Swnj tmattach(ui) 1822982Swnj struct uba_device *ui; 1832608Swnj { 1843095Swnj /* 1853095Swnj * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf 1863095Swnj * arrays given a te unit number. 1873095Swnj */ 1883095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1892608Swnj } 1902608Swnj 1913495Sroot int tmtimer(); 1922608Swnj /* 1932608Swnj * Open the device. Tapes are unique open 1942608Swnj * devices, so we refuse if it is already open. 1952608Swnj * We also check that a tape is available, and 1963095Swnj * don't block waiting here; if you want to wait 1973095Swnj * for a tape you should timeout in user code. 1982608Swnj */ 19924974Smckusick 20024974Smckusick #ifdef AVIV 20124974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 }; 202*30917Skarels #endif AVIV 20324974Smckusick int tmdiag; 20424974Smckusick 2051919Swnj tmopen(dev, flag) 2061919Swnj dev_t dev; 2071919Swnj int flag; 2081919Swnj { 2093095Swnj register int teunit; 2102982Swnj register struct uba_device *ui; 2113095Swnj register struct te_softc *sc; 2123209Swnj int olddens, dens; 2135437Sroot int s; 2141919Swnj 2153095Swnj teunit = TEUNIT(dev); 21625051Skarels if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2178574Sroot return (ENXIO); 21825051Skarels if ((sc = &te_softc[teunit])->sc_openf) 21925051Skarels return (EBUSY); 220*30917Skarels sc->sc_openf = 1; 2213209Swnj olddens = sc->sc_dens; 2223209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 22324974Smckusick #ifndef AVIV 2243209Swnj if ((minor(dev) & T_1600BPI) == 0) 2253209Swnj dens |= TM_D800; 22624974Smckusick #else AVIV 22724974Smckusick dens |= tmdens[(minor(dev)>>3)&03]; 22824974Smckusick #endif AVIV 2293209Swnj sc->sc_dens = dens; 2303141Swnj get: 2312608Swnj tmcommand(dev, TM_SENSE, 1); 2323141Swnj if (sc->sc_erreg&TMER_SDWN) { 2333141Swnj sleep((caddr_t)&lbolt, PZERO+1); 2343141Swnj goto get; 2353141Swnj } 2363209Swnj sc->sc_dens = olddens; 2373710Sroot if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) { 2383710Sroot uprintf("te%d: not online\n", teunit); 239*30917Skarels sc->sc_openf = 0; 2408574Sroot return (EIO); 2411919Swnj } 2423710Sroot if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) { 2433710Sroot uprintf("te%d: no write ring\n", teunit); 244*30917Skarels sc->sc_openf = 0; 2458574Sroot return (EIO); 2463710Sroot } 2473710Sroot if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2483710Sroot dens != sc->sc_dens) { 2493710Sroot uprintf("te%d: can't change density in mid-tape\n", teunit); 250*30917Skarels sc->sc_openf = 0; 2518574Sroot return (EIO); 2523710Sroot } 2532471Swnj sc->sc_blkno = (daddr_t)0; 2542471Swnj sc->sc_nxrec = INF; 2552608Swnj sc->sc_lastiow = 0; 2563095Swnj sc->sc_dens = dens; 257*30917Skarels sc->sc_blks = 0; 258*30917Skarels sc->sc_softerrs = 0; 25918321Sralph sc->sc_ttyp = u.u_ttyp; 26026369Skarels s = splclock(); 2613495Sroot if (sc->sc_tact == 0) { 2623495Sroot sc->sc_timo = INF; 2633495Sroot sc->sc_tact = 1; 2643629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 2653495Sroot } 2665437Sroot splx(s); 2678574Sroot return (0); 2681919Swnj } 2691919Swnj 2702608Swnj /* 2712608Swnj * Close tape device. 2722608Swnj * 2732608Swnj * If tape was open for writing or last operation was 2742608Swnj * a write, then write two EOF's and backspace over the last one. 2752608Swnj * Unless this is a non-rewinding special file, rewind the tape. 2762608Swnj * Make the tape available to others. 2772608Swnj */ 2781919Swnj tmclose(dev, flag) 2791919Swnj register dev_t dev; 2801919Swnj register flag; 2811919Swnj { 2823095Swnj register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 2831919Swnj 2842608Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2852608Swnj tmcommand(dev, TM_WEOF, 1); 2862608Swnj tmcommand(dev, TM_WEOF, 1); 2872608Swnj tmcommand(dev, TM_SREV, 1); 2881919Swnj } 2891919Swnj if ((minor(dev)&T_NOREWIND) == 0) 2903095Swnj /* 2913095Swnj * 0 count means don't hang waiting for rewind complete 2923095Swnj * rather ctmbuf stays busy until the operation completes 2933095Swnj * preventing further opens from completing by 2943095Swnj * preventing a TM_SENSE from completing. 2953095Swnj */ 2963095Swnj tmcommand(dev, TM_REW, 0); 297*30917Skarels if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100) 298*30917Skarels log(LOG_INFO, "te%d: %d soft errors in %d blocks\n", 299*30917Skarels TEUNIT(dev), sc->sc_softerrs, sc->sc_blks); 3002471Swnj sc->sc_openf = 0; 301*30917Skarels return (0); 3021919Swnj } 3031919Swnj 3042608Swnj /* 3052608Swnj * Execute a command on the tape drive 3062608Swnj * a specified number of times. 3072608Swnj */ 3082574Swnj tmcommand(dev, com, count) 3091919Swnj dev_t dev; 3101919Swnj int com, count; 3111919Swnj { 3121919Swnj register struct buf *bp; 3135437Sroot register int s; 3141919Swnj 3152608Swnj bp = &ctmbuf[TMUNIT(dev)]; 3165437Sroot s = spl5(); 3171919Swnj while (bp->b_flags&B_BUSY) { 3183095Swnj /* 3193095Swnj * This special check is because B_BUSY never 3203095Swnj * gets cleared in the non-waiting rewind case. 3213095Swnj */ 3223141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3233095Swnj break; 3241919Swnj bp->b_flags |= B_WANTED; 3251919Swnj sleep((caddr_t)bp, PRIBIO); 3261919Swnj } 3271919Swnj bp->b_flags = B_BUSY|B_READ; 3285437Sroot splx(s); 3291919Swnj bp->b_dev = dev; 3301919Swnj bp->b_repcnt = -count; 3311919Swnj bp->b_command = com; 3321919Swnj bp->b_blkno = 0; 3331919Swnj tmstrategy(bp); 3343095Swnj /* 3353095Swnj * In case of rewind from close, don't wait. 3363095Swnj * This is the only case where count can be 0. 3373095Swnj */ 3383095Swnj if (count == 0) 3393095Swnj return; 3401919Swnj iowait(bp); 3411919Swnj if (bp->b_flags&B_WANTED) 3421919Swnj wakeup((caddr_t)bp); 3431919Swnj bp->b_flags &= B_ERROR; 3441919Swnj } 3451919Swnj 3462608Swnj /* 3473095Swnj * Queue a tape operation. 3482608Swnj */ 3491919Swnj tmstrategy(bp) 3501919Swnj register struct buf *bp; 3511919Swnj { 3523095Swnj int teunit = TEUNIT(bp->b_dev); 3532982Swnj register struct uba_ctlr *um; 3542608Swnj register struct buf *dp; 3555437Sroot int s; 3561919Swnj 3572608Swnj /* 3582608Swnj * Put transfer at end of unit queue 3592608Swnj */ 3603095Swnj dp = &teutab[teunit]; 3611919Swnj bp->av_forw = NULL; 3625437Sroot s = spl5(); 3633939Sbugs um = tedinfo[teunit]->ui_mi; 3642608Swnj if (dp->b_actf == NULL) { 3652608Swnj dp->b_actf = bp; 3662608Swnj /* 3672608Swnj * Transport not already active... 3682608Swnj * put at end of controller queue. 3692608Swnj */ 3702608Swnj dp->b_forw = NULL; 3712608Swnj if (um->um_tab.b_actf == NULL) 3722608Swnj um->um_tab.b_actf = dp; 3732608Swnj else 3742608Swnj um->um_tab.b_actl->b_forw = dp; 3752608Swnj um->um_tab.b_actl = dp; 3762608Swnj } else 3772608Swnj dp->b_actl->av_forw = bp; 3782608Swnj dp->b_actl = bp; 3792608Swnj /* 3802608Swnj * If the controller is not busy, get 3812608Swnj * it going. 3822608Swnj */ 3832608Swnj if (um->um_tab.b_active == 0) 3842608Swnj tmstart(um); 3855437Sroot splx(s); 3861919Swnj } 3871919Swnj 3882608Swnj /* 3892608Swnj * Start activity on a tm controller. 3902608Swnj */ 3912608Swnj tmstart(um) 3922982Swnj register struct uba_ctlr *um; 3931919Swnj { 3942608Swnj register struct buf *bp, *dp; 3955692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3963095Swnj register struct te_softc *sc; 3972982Swnj register struct uba_device *ui; 3983095Swnj int teunit, cmd; 3992471Swnj daddr_t blkno; 4001919Swnj 4012608Swnj /* 4022608Swnj * Look for an idle transport on the controller. 4032608Swnj */ 4041919Swnj loop: 4052608Swnj if ((dp = um->um_tab.b_actf) == NULL) 4061919Swnj return; 4072608Swnj if ((bp = dp->b_actf) == NULL) { 4082608Swnj um->um_tab.b_actf = dp->b_forw; 4092608Swnj goto loop; 4102608Swnj } 4113095Swnj teunit = TEUNIT(bp->b_dev); 4123095Swnj ui = tedinfo[teunit]; 4132608Swnj /* 4142608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 4152608Swnj */ 4163095Swnj sc = &te_softc[teunit]; 4175692Sroot addr = (struct tmdevice *)um->um_addr; 4182608Swnj addr->tmcs = (ui->ui_slave << 8); 4192471Swnj sc->sc_dsreg = addr->tmcs; 4202471Swnj sc->sc_erreg = addr->tmer; 4212471Swnj sc->sc_resid = addr->tmbc; 4222608Swnj /* 4232608Swnj * Default is that last command was NOT a write command; 4242608Swnj * if we do a write command we will notice this in tmintr(). 4252608Swnj */ 4263493Sroot sc->sc_lastiow = 0; 4272608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4282608Swnj /* 4293095Swnj * Have had a hard error on a non-raw tape 4303095Swnj * or the tape unit is now unavailable 4313095Swnj * (e.g. taken off line). 4322608Swnj */ 4332608Swnj bp->b_flags |= B_ERROR; 4341919Swnj goto next; 4351919Swnj } 4363095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4373095Swnj /* 4383095Swnj * Execute control operation with the specified count. 4393095Swnj */ 4402608Swnj if (bp->b_command == TM_SENSE) 4412608Swnj goto next; 4423495Sroot /* 4433495Sroot * Set next state; give 5 minutes to complete 4443495Sroot * rewind, or 10 seconds per iteration (minimum 60 4453629Sroot * seconds and max 5 minutes) to complete other ops. 4463495Sroot */ 4473495Sroot if (bp->b_command == TM_REW) { 4483495Sroot um->um_tab.b_active = SREW; 4493495Sroot sc->sc_timo = 5 * 60; 4503495Sroot } else { 4513495Sroot um->um_tab.b_active = SCOM; 4524266Swnj sc->sc_timo = 4534266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4543495Sroot } 4552608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4562608Swnj addr->tmbc = bp->b_repcnt; 4572670Swnj goto dobpcmd; 4582608Swnj } 4592608Swnj /* 4603095Swnj * The following checks handle boundary cases for operation 4613095Swnj * on non-raw tapes. On raw tapes the initialization of 4623095Swnj * sc->sc_nxrec by tmphys causes them to be skipped normally 4633095Swnj * (except in the case of retries). 4643095Swnj */ 4657381Ssam if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4663095Swnj /* 4673095Swnj * Can't read past known end-of-file. 4683095Swnj */ 4693095Swnj bp->b_flags |= B_ERROR; 4703095Swnj bp->b_error = ENXIO; 4713095Swnj goto next; 4723095Swnj } 4737381Ssam if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 4743095Swnj bp->b_flags&B_READ) { 4753095Swnj /* 4763095Swnj * Reading at end of file returns 0 bytes. 4773095Swnj */ 4783095Swnj bp->b_resid = bp->b_bcount; 4793095Swnj clrbuf(bp); 4803095Swnj goto next; 4813095Swnj } 4823095Swnj if ((bp->b_flags&B_READ) == 0) 4833095Swnj /* 4843095Swnj * Writing sets EOF 4853095Swnj */ 4867381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4873095Swnj /* 4882608Swnj * If the data transfer command is in the correct place, 4892608Swnj * set up all the registers except the csr, and give 4902608Swnj * control over to the UNIBUS adapter routines, to 4912608Swnj * wait for resources to start the i/o. 4922608Swnj */ 4937381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4942396Swnj addr->tmbc = -bp->b_bcount; 4951919Swnj if ((bp->b_flags&B_READ) == 0) { 496*30917Skarels if (um->um_tab.b_errcnt && 497*30917Skarels (sc->sc_erreg & (TMER_HARD|TMER_SOFT)) != TMER_BGL) 4983095Swnj cmd = TM_WIRG; 4991919Swnj else 5003095Swnj cmd = TM_WCOM; 5011919Swnj } else 5023095Swnj cmd = TM_RCOM; 5032471Swnj um->um_tab.b_active = SIO; 5043095Swnj um->um_cmd = sc->sc_dens|cmd; 5052928Swnj #ifdef notdef 5062670Swnj if (tmreverseop(sc->sc_lastcmd)) 5073095Swnj while (addr->tmer & TMER_SDWN) 50824974Smckusick DELAY(10),tmgapsdcnt++; 5092670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 5102928Swnj #endif 5113495Sroot sc->sc_timo = 60; /* premature, but should serve */ 5123105Swnj (void) ubago(ui); 5131919Swnj return; 5141919Swnj } 5152608Swnj /* 5163095Swnj * Tape positioned incorrectly; 5173095Swnj * set to seek forwards or backwards to the correct spot. 5183095Swnj * This happens for raw tapes only on error retries. 5192608Swnj */ 5202471Swnj um->um_tab.b_active = SSEEK; 5217381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5222670Swnj bp->b_command = TM_SFORW; 5237381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5241919Swnj } else { 5252670Swnj bp->b_command = TM_SREV; 5267381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5271919Swnj } 5283629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5292670Swnj dobpcmd: 5302928Swnj #ifdef notdef 5313095Swnj /* 5323095Swnj * It is strictly necessary to wait for the tape 5333095Swnj * to stop before changing directions, but the TC11 5343095Swnj * handles this for us. 5353095Swnj */ 5362670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5372670Swnj while (addr->tmer & TM_SDWN) 53824974Smckusick DELAY(10),tmgapsdcnt++; 5392670Swnj sc->sc_lastcmd = bp->b_command; 5402928Swnj #endif 5413095Swnj /* 5423095Swnj * Do the command in bp. 5433095Swnj */ 5443095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5451919Swnj return; 5461919Swnj 5471919Swnj next: 5482608Swnj /* 5492608Swnj * Done with this operation due to error or 5502608Swnj * the fact that it doesn't do anything. 5512608Swnj * Release UBA resources (if any), dequeue 5522608Swnj * the transfer and continue processing this slave. 5532608Swnj */ 5542608Swnj if (um->um_ubinfo) 5552617Swnj ubadone(um); 5562608Swnj um->um_tab.b_errcnt = 0; 5572608Swnj dp->b_actf = bp->av_forw; 5581919Swnj iodone(bp); 5591919Swnj goto loop; 5601919Swnj } 5611919Swnj 5622608Swnj /* 5632608Swnj * The UNIBUS resources we needed have been 5642608Swnj * allocated to us; start the device. 5652608Swnj */ 5662574Swnj tmdgo(um) 5672982Swnj register struct uba_ctlr *um; 5681919Swnj { 5695692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5702471Swnj 5712574Swnj addr->tmba = um->um_ubinfo; 5722574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5732396Swnj } 5742396Swnj 5752608Swnj /* 5762608Swnj * Tm interrupt routine. 5772608Swnj */ 5782471Swnj /*ARGSUSED*/ 5792630Swnj tmintr(tm11) 5802630Swnj int tm11; 5812396Swnj { 5822608Swnj struct buf *dp; 5831919Swnj register struct buf *bp; 5842982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5855692Sroot register struct tmdevice *addr; 5863095Swnj register struct te_softc *sc; 5873095Swnj int teunit; 5881919Swnj register state; 5891919Swnj 5903095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5913095Swnj return; 5923095Swnj bp = dp->b_actf; 5933095Swnj teunit = TEUNIT(bp->b_dev); 5945692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5953524Swnj sc = &te_softc[teunit]; 5962608Swnj /* 5972608Swnj * If last command was a rewind, and tape is still 5982608Swnj * rewinding, wait for the rewind complete interrupt. 5992608Swnj */ 6002608Swnj if (um->um_tab.b_active == SREW) { 6012608Swnj um->um_tab.b_active = SCOM; 6023524Swnj if (addr->tmer&TMER_RWS) { 6033524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 6042608Swnj return; 6053524Swnj } 6061919Swnj } 6072608Swnj /* 6082608Swnj * An operation completed... record status 6092608Swnj */ 6103495Sroot sc->sc_timo = INF; 6112471Swnj sc->sc_dsreg = addr->tmcs; 6122471Swnj sc->sc_erreg = addr->tmer; 6132471Swnj sc->sc_resid = addr->tmbc; 6141919Swnj if ((bp->b_flags & B_READ) == 0) 6152608Swnj sc->sc_lastiow = 1; 6162471Swnj state = um->um_tab.b_active; 6172471Swnj um->um_tab.b_active = 0; 6182608Swnj /* 6192608Swnj * Check for errors. 6202608Swnj */ 6212608Swnj if (addr->tmcs&TM_ERR) { 6223095Swnj while (addr->tmer & TMER_SDWN) 62324974Smckusick DELAY(10); /* await settle down */ 6242608Swnj /* 6253095Swnj * If we hit the end of the tape file, update our position. 6262608Swnj */ 6273095Swnj if (addr->tmer&TMER_EOF) { 6282608Swnj tmseteof(bp); /* set blkno and nxrec */ 6292608Swnj state = SCOM; /* force completion */ 6302608Swnj /* 6312608Swnj * Stuff bc so it will be unstuffed correctly 6322608Swnj * later to get resid. 6332608Swnj */ 6342396Swnj addr->tmbc = -bp->b_bcount; 6352608Swnj goto opdone; 6361919Swnj } 6372608Swnj /* 6383095Swnj * If we were reading raw tape and the only error was that the 6393095Swnj * record was too long, then we don't consider this an error. 6402608Swnj */ 6413095Swnj if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6423095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6432608Swnj goto ignoreerr; 6442608Swnj /* 6452608Swnj * If error is not hard, and this was an i/o operation 6462608Swnj * retry up to 8 times. 6472608Swnj */ 6483095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6492471Swnj if (++um->um_tab.b_errcnt < 7) { 650*30917Skarels if (tmdiag) 651*30917Skarels log(LOG_DEBUG, 652*30917Skarels "te%d: soft error bn%d er=%b\n", 653*30917Skarels minor(bp->b_dev)&03, 654*30917Skarels bp->b_blkno, sc->sc_erreg, 655*30917Skarels TMER_BITS); 6562471Swnj sc->sc_blkno++; 6572617Swnj ubadone(um); 6582608Swnj goto opcont; 6591919Swnj } 6602608Swnj } else 6612608Swnj /* 6622608Swnj * Hard or non-i/o errors on non-raw tape 6632608Swnj * cause it to close. 6642608Swnj */ 6653095Swnj if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)]) 6662608Swnj sc->sc_openf = -1; 6672608Swnj /* 6682608Swnj * Couldn't recover error 6692608Swnj */ 67018321Sralph tprintf(sc->sc_ttyp, 67118321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6723095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 67324974Smckusick #ifdef AVIV 67424974Smckusick if (tmdiag) { 67524974Smckusick addr->tmmr = DAB; 67624974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 67724974Smckusick addr->tmmr = DTS; 67824974Smckusick if (addr->tmmr & DTS_MASK) 67924974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 68024974Smckusick addr->tmmr = RWERR; 68124974Smckusick printf(", read/write errors %b\n", 68224974Smckusick addr->tmmr & RWERR_MASK, 68324974Smckusick RWERR_BITS); 68424974Smckusick addr->tmmr = DRSENSE; 68524974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 68624974Smckusick DRSENSE_BITS); 68724974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 68824974Smckusick } 68924974Smckusick #endif AVIV 6901919Swnj bp->b_flags |= B_ERROR; 6912608Swnj goto opdone; 6921919Swnj } 6932608Swnj /* 6942608Swnj * Advance tape control FSM. 6952608Swnj */ 6962608Swnj ignoreerr: 6971919Swnj switch (state) { 6981919Swnj 6991919Swnj case SIO: 7002608Swnj /* 7012608Swnj * Read/write increments tape block number 7022608Swnj */ 7032471Swnj sc->sc_blkno++; 704*30917Skarels sc->sc_blks++; 705*30917Skarels if (um->um_tab.b_errcnt) 706*30917Skarels sc->sc_softerrs++; 7072608Swnj goto opdone; 7081919Swnj 7091919Swnj case SCOM: 7102608Swnj /* 7113095Swnj * For forward/backward space record update current position. 7122608Swnj */ 7133095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 71426292Skarels switch ((int)bp->b_command) { 7151919Swnj 7162608Swnj case TM_SFORW: 7172608Swnj sc->sc_blkno -= bp->b_repcnt; 7183095Swnj break; 7191919Swnj 7202608Swnj case TM_SREV: 7212608Swnj sc->sc_blkno += bp->b_repcnt; 7223095Swnj break; 7231919Swnj } 7243095Swnj goto opdone; 7251919Swnj 7261919Swnj case SSEEK: 7277381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7282608Swnj goto opcont; 7291919Swnj 7301919Swnj default: 7312608Swnj panic("tmintr"); 7322608Swnj } 7332608Swnj opdone: 7342608Swnj /* 7352608Swnj * Reset error count and remove 7362608Swnj * from device queue. 7372608Swnj */ 7382608Swnj um->um_tab.b_errcnt = 0; 7392608Swnj dp->b_actf = bp->av_forw; 74014929Skarels /* 74114929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 74214929Skarels */ 74315081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7442617Swnj ubadone(um); 7452608Swnj iodone(bp); 7462608Swnj /* 7472608Swnj * Circulate slave to end of controller 7482608Swnj * queue to give other slaves a chance. 7492608Swnj */ 7502608Swnj um->um_tab.b_actf = dp->b_forw; 7512608Swnj if (dp->b_actf) { 7522608Swnj dp->b_forw = NULL; 7532608Swnj if (um->um_tab.b_actf == NULL) 7542608Swnj um->um_tab.b_actf = dp; 7552608Swnj else 7562608Swnj um->um_tab.b_actl->b_forw = dp; 7572608Swnj um->um_tab.b_actl = dp; 7582608Swnj } 7592608Swnj if (um->um_tab.b_actf == 0) 7601919Swnj return; 7612608Swnj opcont: 7622608Swnj tmstart(um); 7631919Swnj } 7641919Swnj 7653495Sroot tmtimer(dev) 7663495Sroot int dev; 7673495Sroot { 7683495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7694847Sroot register short x; 7703495Sroot 7713495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7724278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7733495Sroot sc->sc_timo = INF; 7744847Sroot x = spl5(); 7753495Sroot tmintr(TMUNIT(dev)); 7764847Sroot (void) splx(x); 7773495Sroot } 7783629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7793495Sroot } 7803495Sroot 7811919Swnj tmseteof(bp) 7821919Swnj register struct buf *bp; 7831919Swnj { 7843095Swnj register int teunit = TEUNIT(bp->b_dev); 7855692Sroot register struct tmdevice *addr = 7865692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7873095Swnj register struct te_softc *sc = &te_softc[teunit]; 7881919Swnj 7893095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7907381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7911919Swnj /* reversing */ 7927381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 7932471Swnj sc->sc_blkno = sc->sc_nxrec; 7941919Swnj } else { 7951919Swnj /* spacing forward */ 7967381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 7972471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7981919Swnj } 7991919Swnj return; 8001919Swnj } 8011919Swnj /* eof on read */ 8027381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 8031919Swnj } 8041919Swnj 8057732Sroot tmread(dev, uio) 8062608Swnj dev_t dev; 8077732Sroot struct uio *uio; 8081919Swnj { 8098163Sroot int errno; 8101919Swnj 8118163Sroot errno = tmphys(dev, uio); 8128163Sroot if (errno) 8138163Sroot return (errno); 8148163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio)); 8151919Swnj } 8161919Swnj 8177838Sroot tmwrite(dev, uio) 8182608Swnj dev_t dev; 8197838Sroot struct uio *uio; 8201919Swnj { 8218163Sroot int errno; 8221919Swnj 8238163Sroot errno = tmphys(dev, uio); 8248163Sroot if (errno) 8258163Sroot return (errno); 8268163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio)); 8271919Swnj } 8281919Swnj 8293095Swnj /* 8303095Swnj * Check that a raw device exists. 8313095Swnj * If it does, set up sc_blkno and sc_nxrec 8323095Swnj * so that the tape will appear positioned correctly. 8333095Swnj */ 8347732Sroot tmphys(dev, uio) 8352608Swnj dev_t dev; 8367732Sroot struct uio *uio; 8371919Swnj { 8383095Swnj register int teunit = TEUNIT(dev); 8391919Swnj register daddr_t a; 8403095Swnj register struct te_softc *sc; 8413095Swnj register struct uba_device *ui; 8421919Swnj 8437838Sroot if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) 8447732Sroot return (ENXIO); 8453095Swnj sc = &te_softc[teunit]; 8467838Sroot a = bdbtofsb(uio->uio_offset >> 9); 8472471Swnj sc->sc_blkno = a; 8482471Swnj sc->sc_nxrec = a + 1; 8497732Sroot return (0); 8501919Swnj } 8511919Swnj 8522608Swnj tmreset(uban) 8532608Swnj int uban; 8542608Swnj { 8552982Swnj register struct uba_ctlr *um; 8563095Swnj register tm11, teunit; 8572982Swnj register struct uba_device *ui; 8582608Swnj register struct buf *dp; 8592608Swnj 8602630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8612630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8622608Swnj um->um_ubanum != uban) 8632608Swnj continue; 8642928Swnj printf(" tm%d", tm11); 8652608Swnj um->um_tab.b_active = 0; 8662608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8672608Swnj if (um->um_ubinfo) { 8682608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8699355Ssam um->um_ubinfo = 0; 8702608Swnj } 8715692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8723095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8733095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8743095Swnj ui->ui_alive == 0) 8752608Swnj continue; 8763095Swnj dp = &teutab[teunit]; 8772608Swnj dp->b_active = 0; 8782608Swnj dp->b_forw = 0; 8792608Swnj if (um->um_tab.b_actf == NULL) 8802608Swnj um->um_tab.b_actf = dp; 8812608Swnj else 8822608Swnj um->um_tab.b_actl->b_forw = dp; 8832608Swnj um->um_tab.b_actl = dp; 8843495Sroot if (te_softc[teunit].sc_openf > 0) 8853495Sroot te_softc[teunit].sc_openf = -1; 8862608Swnj } 8872608Swnj tmstart(um); 8882608Swnj } 8892608Swnj } 8902608Swnj 8911919Swnj /*ARGSUSED*/ 8927632Ssam tmioctl(dev, cmd, data, flag) 8937632Ssam caddr_t data; 8941919Swnj dev_t dev; 8951919Swnj { 8963095Swnj int teunit = TEUNIT(dev); 8973095Swnj register struct te_softc *sc = &te_softc[teunit]; 8983095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8991919Swnj register callcount; 9001919Swnj int fcount; 9017632Ssam struct mtop *mtop; 9027632Ssam struct mtget *mtget; 9031919Swnj /* we depend of the values and order of the MT codes here */ 9042608Swnj static tmops[] = 9052608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 9061919Swnj 9072608Swnj switch (cmd) { 9087632Ssam 9097632Ssam case MTIOCTOP: /* tape operation */ 9107632Ssam mtop = (struct mtop *)data; 9118574Sroot switch (mtop->mt_op) { 9127632Ssam 9132608Swnj case MTWEOF: 9147632Ssam callcount = mtop->mt_count; 9152608Swnj fcount = 1; 9162608Swnj break; 9177632Ssam 9182608Swnj case MTFSF: case MTBSF: 9197632Ssam callcount = mtop->mt_count; 9201919Swnj fcount = INF; 9211919Swnj break; 9227632Ssam 9231919Swnj case MTFSR: case MTBSR: 9241919Swnj callcount = 1; 9257632Ssam fcount = mtop->mt_count; 9261919Swnj break; 9277632Ssam 9282324Skre case MTREW: case MTOFFL: case MTNOP: 9291919Swnj callcount = 1; 9301919Swnj fcount = 1; 9311919Swnj break; 9327632Ssam 9331919Swnj default: 9348574Sroot return (ENXIO); 9351919Swnj } 9368574Sroot if (callcount <= 0 || fcount <= 0) 9378574Sroot return (EINVAL); 9382608Swnj while (--callcount >= 0) { 9397632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 9407632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 9418574Sroot bp->b_resid) 9428574Sroot return (EIO); 9433095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9441919Swnj break; 9451919Swnj } 9468648Sroot return (geterror(bp)); 9477632Ssam 9481919Swnj case MTIOCGET: 9497632Ssam mtget = (struct mtget *)data; 9507632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9517632Ssam mtget->mt_erreg = sc->sc_erreg; 9527632Ssam mtget->mt_resid = sc->sc_resid; 9537632Ssam mtget->mt_type = MT_ISTM; 9548574Sroot break; 9557632Ssam 9561919Swnj default: 9578574Sroot return (ENXIO); 9581919Swnj } 9598574Sroot return (0); 9601919Swnj } 9611919Swnj 9621919Swnj #define DBSIZE 20 9631919Swnj 9642363Swnj tmdump() 9652363Swnj { 9662982Swnj register struct uba_device *ui; 9672396Swnj register struct uba_regs *up; 9685692Sroot register struct tmdevice *addr; 9692426Skre int blk, num; 9702426Skre int start; 9711919Swnj 9722426Skre start = 0; 9732426Skre num = maxfree; 9742426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9753095Swnj if (tedinfo[0] == 0) 9762887Swnj return (ENXIO); 9773095Swnj ui = phys(tedinfo[0], struct uba_device *); 9782396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9793331Swnj ubainit(up); 9802324Skre DELAY(1000000); 9815692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9822396Swnj tmwait(addr); 9832608Swnj addr->tmcs = TM_DCLR | TM_GO; 9841919Swnj while (num > 0) { 9851919Swnj blk = num > DBSIZE ? DBSIZE : num; 9862396Swnj tmdwrite(start, blk, addr, up); 9871919Swnj start += blk; 9881919Swnj num -= blk; 9891919Swnj } 9902426Skre tmeof(addr); 9912426Skre tmeof(addr); 9922426Skre tmwait(addr); 9932887Swnj if (addr->tmcs&TM_ERR) 9942887Swnj return (EIO); 9952608Swnj addr->tmcs = TM_REW | TM_GO; 9962471Swnj tmwait(addr); 9972363Swnj return (0); 9981919Swnj } 9991919Swnj 10002608Swnj tmdwrite(dbuf, num, addr, up) 10012608Swnj register dbuf, num; 10025692Sroot register struct tmdevice *addr; 10032396Swnj struct uba_regs *up; 10041919Swnj { 10052396Swnj register struct pte *io; 10062396Swnj register int npf; 10071928Swnj 10082396Swnj tmwait(addr); 10092396Swnj io = up->uba_map; 10101919Swnj npf = num+1; 10111928Swnj while (--npf != 0) 10122982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 10132396Swnj *(int *)io = 0; 10142396Swnj addr->tmbc = -(num*NBPG); 10152396Swnj addr->tmba = 0; 10162608Swnj addr->tmcs = TM_WCOM | TM_GO; 10171919Swnj } 10181919Swnj 10192396Swnj tmwait(addr) 10205692Sroot register struct tmdevice *addr; 10211919Swnj { 10221928Swnj register s; 10231919Swnj 10241919Swnj do 10252396Swnj s = addr->tmcs; 10262608Swnj while ((s & TM_CUR) == 0); 10271919Swnj } 10281919Swnj 10292396Swnj tmeof(addr) 10305692Sroot struct tmdevice *addr; 10311919Swnj { 10321919Swnj 10332396Swnj tmwait(addr); 10342608Swnj addr->tmcs = TM_WEOF | TM_GO; 10351919Swnj } 10361919Swnj #endif 1037