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*40045Smarc * @(#)tm.c 7.7 (Berkeley) 02/08/90 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 controller 173095Swnj * test reset code 183095Swnj * what happens if you offline tape during rewind? 193095Swnj * test using file system on tape 201919Swnj */ 2117079Sbloom #include "param.h" 2217079Sbloom #include "systm.h" 2317079Sbloom #include "buf.h" 2417079Sbloom #include "dir.h" 2517079Sbloom #include "conf.h" 2617079Sbloom #include "user.h" 2717079Sbloom #include "file.h" 2817079Sbloom #include "map.h" 2917079Sbloom #include "vm.h" 3017079Sbloom #include "ioctl.h" 3117079Sbloom #include "mtio.h" 3217079Sbloom #include "cmap.h" 3317079Sbloom #include "uio.h" 3417079Sbloom #include "kernel.h" 3518321Sralph #include "tty.h" 3630917Skarels #include "syslog.h" 37*40045Smarc #include "tsleep.h" 381919Swnj 3937511Smckusick #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 * Driver unibus interface routines and variables. 583095Swnj */ 592608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 602982Swnj struct uba_ctlr *tmminfo[NTM]; 613095Swnj struct uba_device *tedinfo[NTE]; 623095Swnj struct buf teutab[NTE]; 633095Swnj short tetotm[NTE]; 642458Swnj u_short tmstd[] = { 0772520, 0 }; 652396Swnj struct uba_driver tmdriver = 663095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 671919Swnj 681919Swnj /* bits in minor device */ 693095Swnj #define TEUNIT(dev) (minor(dev)&03) 703095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 711919Swnj #define T_NOREWIND 04 7224974Smckusick #define T_1600BPI 0x8 731919Swnj 741919Swnj #define INF (daddr_t)1000000L 751919Swnj 762608Swnj /* 772608Swnj * Software state per tape transport. 783095Swnj * 793095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 803095Swnj * 2. We keep track of the current position on a block tape and seek 813095Swnj * before operations by forward/back spacing if necessary. 823095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 833095Swnj * is open read write and the last thing done is a write we can 843095Swnj * write a standard end of tape mark (two eofs). 853095Swnj * 4. We remember the status registers after the last command, using 863095Swnj * then internally and returning them to the SENSE ioctl. 873095Swnj * 5. We remember the last density the tape was used at. If it is 883095Swnj * not a BOT when we start using it and we are writing, we don't 893095Swnj * let the density be changed. 902608Swnj */ 913095Swnj struct te_softc { 922608Swnj char sc_openf; /* lock against multiple opens */ 932608Swnj char sc_lastiow; /* last op was a write */ 942608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 953095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 962608Swnj u_short sc_erreg; /* copy of last erreg */ 9733477Skarels u_short sc_ioerreg; /* copy of last erreg for I/O command */ 982608Swnj u_short sc_dsreg; /* copy of last dsreg */ 992608Swnj short sc_resid; /* copy of last bc */ 1003105Swnj #ifdef unneeded 1012670Swnj short sc_lastcmd; /* last command to handle direction changes */ 1022928Swnj #endif 1033095Swnj u_short sc_dens; /* prototype command with density info */ 10418321Sralph short sc_tact; /* timeout is active */ 1053495Sroot daddr_t sc_timo; /* time until timeout expires */ 10630917Skarels int sc_blks; /* number of I/O operations since open */ 10730917Skarels int sc_softerrs; /* number of soft I/O errors since open */ 108*40045Smarc caddr_t sc_ctty; /* users controlling terminal (vnode) */ 1096952Swnj } te_softc[NTE]; 1103105Swnj #ifdef unneeded 1113105Swnj int tmgapsdcnt; /* DEBUG */ 1123105Swnj #endif 1131919Swnj 1142608Swnj /* 1153095Swnj * States for um->um_tab.b_active, the per controller state flag. 1163095Swnj * This is used to sequence control in the driver. 1172608Swnj */ 1181919Swnj #define SSEEK 1 /* seeking */ 1191919Swnj #define SIO 2 /* doing seq i/o */ 1201919Swnj #define SCOM 3 /* sending control command */ 1212608Swnj #define SREW 4 /* sending a drive rewind */ 1221919Swnj 1232426Skre /* 1242426Skre * Determine if there is a controller for 1252426Skre * a tm at address reg. Our goal is to make the 1262426Skre * device interrupt. 1272426Skre */ 1282608Swnj tmprobe(reg) 1292396Swnj caddr_t reg; 1302396Swnj { 1313095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1322426Skre 1332608Swnj #ifdef lint 1343105Swnj br = 0; cvec = br; br = cvec; 1354936Swnj tmintr(0); 1362608Swnj #endif 1375692Sroot ((struct tmdevice *)reg)->tmcs = TM_IE; 1382396Swnj /* 1392630Swnj * If this is a tm11, it ought to have interrupted 1402396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1412458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1422458Swnj * Just in case, we will reference one 1432396Swnj * of the more distant registers, and hope for a machine 1442458Swnj * check, or similar disaster if this is a ts. 1452471Swnj * 1462471Swnj * Note: on an 11/780, badaddr will just generate 1472471Swnj * a uba error for a ts; but our caller will notice that 1482471Swnj * so we won't check for it. 1492396Swnj */ 1505692Sroot if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2)) 1512458Swnj return (0); 1527404Skre return (sizeof (struct tmdevice)); 1532396Swnj } 1542396Swnj 1552608Swnj /* 1562608Swnj * Due to a design flaw, we cannot ascertain if the tape 1572608Swnj * exists or not unless it is on line - ie: unless a tape is 1582608Swnj * mounted. This is too servere a restriction to bear, 1592608Swnj * so all units are assumed to exist. 1602608Swnj */ 1612608Swnj /*ARGSUSED*/ 1622574Swnj tmslave(ui, reg) 1632982Swnj struct uba_device *ui; 1642396Swnj caddr_t reg; 1652396Swnj { 1662458Swnj 1672458Swnj return (1); 1682396Swnj } 1692396Swnj 1702608Swnj /* 1713095Swnj * Record attachment of the unit to the controller. 1722608Swnj */ 1732608Swnj /*ARGSUSED*/ 1742608Swnj tmattach(ui) 1752982Swnj struct uba_device *ui; 1762608Swnj { 1773095Swnj /* 17834217Sbostic * Tetotm is used in TMUNIT to index the ctmbuf 17934217Sbostic * array given a te unit number. 1803095Swnj */ 1813095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1822608Swnj } 1832608Swnj 1843495Sroot int tmtimer(); 1852608Swnj /* 1862608Swnj * Open the device. Tapes are unique open 1872608Swnj * devices, so we refuse if it is already open. 1882608Swnj * We also check that a tape is available, and 1893095Swnj * don't block waiting here; if you want to wait 1903095Swnj * for a tape you should timeout in user code. 1912608Swnj */ 19224974Smckusick 19324974Smckusick #ifdef AVIV 19424974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 }; 19530917Skarels #endif AVIV 19624974Smckusick int tmdiag; 19724974Smckusick 1981919Swnj tmopen(dev, flag) 1991919Swnj dev_t dev; 2001919Swnj int flag; 2011919Swnj { 2023095Swnj register int teunit; 2032982Swnj register struct uba_device *ui; 2043095Swnj register struct te_softc *sc; 2053209Swnj int olddens, dens; 2065437Sroot int s; 2071919Swnj 2083095Swnj teunit = TEUNIT(dev); 20925051Skarels if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2108574Sroot return (ENXIO); 21125051Skarels if ((sc = &te_softc[teunit])->sc_openf) 21225051Skarels return (EBUSY); 21330917Skarels sc->sc_openf = 1; 2143209Swnj olddens = sc->sc_dens; 2153209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 21624974Smckusick #ifndef AVIV 2173209Swnj if ((minor(dev) & T_1600BPI) == 0) 2183209Swnj dens |= TM_D800; 21924974Smckusick #else AVIV 22024974Smckusick dens |= tmdens[(minor(dev)>>3)&03]; 22124974Smckusick #endif AVIV 2223209Swnj sc->sc_dens = dens; 2233141Swnj get: 2242608Swnj tmcommand(dev, TM_SENSE, 1); 2253141Swnj if (sc->sc_erreg&TMER_SDWN) { 226*40045Smarc tsleep((caddr_t)&lbolt, PZERO+1, SLP_TM_OPN, 0); 2273141Swnj goto get; 2283141Swnj } 2293209Swnj sc->sc_dens = olddens; 2303710Sroot if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) { 2313710Sroot uprintf("te%d: not online\n", teunit); 23230917Skarels sc->sc_openf = 0; 2338574Sroot return (EIO); 2341919Swnj } 2353710Sroot if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) { 2363710Sroot uprintf("te%d: no write ring\n", teunit); 23730917Skarels sc->sc_openf = 0; 2388574Sroot return (EIO); 2393710Sroot } 2403710Sroot if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2413710Sroot dens != sc->sc_dens) { 2423710Sroot uprintf("te%d: can't change density in mid-tape\n", teunit); 24330917Skarels sc->sc_openf = 0; 2448574Sroot return (EIO); 2453710Sroot } 2462471Swnj sc->sc_blkno = (daddr_t)0; 2472471Swnj sc->sc_nxrec = INF; 2482608Swnj sc->sc_lastiow = 0; 2493095Swnj sc->sc_dens = dens; 25030917Skarels sc->sc_blks = 0; 25130917Skarels sc->sc_softerrs = 0; 252*40045Smarc sc->sc_ctty = (caddr_t)(u.u_procp->p_flag&SCTTY ? 253*40045Smarc u.u_procp->p_session->s_ttyvp : 0); 25426369Skarels s = splclock(); 2553495Sroot if (sc->sc_tact == 0) { 2563495Sroot sc->sc_timo = INF; 2573495Sroot sc->sc_tact = 1; 2583629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 2593495Sroot } 2605437Sroot splx(s); 2618574Sroot return (0); 2621919Swnj } 2631919Swnj 2642608Swnj /* 2652608Swnj * Close tape device. 2662608Swnj * 2672608Swnj * If tape was open for writing or last operation was 2682608Swnj * a write, then write two EOF's and backspace over the last one. 2692608Swnj * Unless this is a non-rewinding special file, rewind the tape. 2702608Swnj * Make the tape available to others. 2712608Swnj */ 2721919Swnj tmclose(dev, flag) 2731919Swnj register dev_t dev; 2741919Swnj register flag; 2751919Swnj { 2763095Swnj register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 2771919Swnj 2782608Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2792608Swnj tmcommand(dev, TM_WEOF, 1); 2802608Swnj tmcommand(dev, TM_WEOF, 1); 2812608Swnj tmcommand(dev, TM_SREV, 1); 2821919Swnj } 2831919Swnj if ((minor(dev)&T_NOREWIND) == 0) 2843095Swnj /* 2853095Swnj * 0 count means don't hang waiting for rewind complete 2863095Swnj * rather ctmbuf stays busy until the operation completes 2873095Swnj * preventing further opens from completing by 2883095Swnj * preventing a TM_SENSE from completing. 2893095Swnj */ 2903095Swnj tmcommand(dev, TM_REW, 0); 29130917Skarels if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100) 29230917Skarels log(LOG_INFO, "te%d: %d soft errors in %d blocks\n", 29330917Skarels TEUNIT(dev), sc->sc_softerrs, sc->sc_blks); 2942471Swnj sc->sc_openf = 0; 29530917Skarels return (0); 2961919Swnj } 2971919Swnj 2982608Swnj /* 2992608Swnj * Execute a command on the tape drive 3002608Swnj * a specified number of times. 3012608Swnj */ 3022574Swnj tmcommand(dev, com, count) 3031919Swnj dev_t dev; 3041919Swnj int com, count; 3051919Swnj { 3061919Swnj register struct buf *bp; 3075437Sroot register int s; 3081919Swnj 3092608Swnj bp = &ctmbuf[TMUNIT(dev)]; 3105437Sroot s = spl5(); 3111919Swnj while (bp->b_flags&B_BUSY) { 3123095Swnj /* 3133095Swnj * This special check is because B_BUSY never 3143095Swnj * gets cleared in the non-waiting rewind case. 3153095Swnj */ 3163141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3173095Swnj break; 3181919Swnj bp->b_flags |= B_WANTED; 3191919Swnj sleep((caddr_t)bp, PRIBIO); 3201919Swnj } 3211919Swnj bp->b_flags = B_BUSY|B_READ; 3225437Sroot splx(s); 3231919Swnj bp->b_dev = dev; 3241919Swnj bp->b_repcnt = -count; 3251919Swnj bp->b_command = com; 3261919Swnj bp->b_blkno = 0; 3271919Swnj tmstrategy(bp); 3283095Swnj /* 3293095Swnj * In case of rewind from close, don't wait. 3303095Swnj * This is the only case where count can be 0. 3313095Swnj */ 3323095Swnj if (count == 0) 3333095Swnj return; 3341919Swnj iowait(bp); 3351919Swnj if (bp->b_flags&B_WANTED) 3361919Swnj wakeup((caddr_t)bp); 3371919Swnj bp->b_flags &= B_ERROR; 3381919Swnj } 3391919Swnj 3402608Swnj /* 3413095Swnj * Queue a tape operation. 3422608Swnj */ 3431919Swnj tmstrategy(bp) 3441919Swnj register struct buf *bp; 3451919Swnj { 3463095Swnj int teunit = TEUNIT(bp->b_dev); 3472982Swnj register struct uba_ctlr *um; 3482608Swnj register struct buf *dp; 3495437Sroot int s; 3501919Swnj 3512608Swnj /* 3522608Swnj * Put transfer at end of unit queue 3532608Swnj */ 3543095Swnj dp = &teutab[teunit]; 3551919Swnj bp->av_forw = NULL; 3565437Sroot s = spl5(); 3573939Sbugs um = tedinfo[teunit]->ui_mi; 3582608Swnj if (dp->b_actf == NULL) { 3592608Swnj dp->b_actf = bp; 3602608Swnj /* 3612608Swnj * Transport not already active... 3622608Swnj * put at end of controller queue. 3632608Swnj */ 3642608Swnj dp->b_forw = NULL; 3652608Swnj if (um->um_tab.b_actf == NULL) 3662608Swnj um->um_tab.b_actf = dp; 3672608Swnj else 3682608Swnj um->um_tab.b_actl->b_forw = dp; 3692608Swnj um->um_tab.b_actl = dp; 3702608Swnj } else 3712608Swnj dp->b_actl->av_forw = bp; 3722608Swnj dp->b_actl = bp; 3732608Swnj /* 3742608Swnj * If the controller is not busy, get 3752608Swnj * it going. 3762608Swnj */ 3772608Swnj if (um->um_tab.b_active == 0) 3782608Swnj tmstart(um); 3795437Sroot splx(s); 3801919Swnj } 3811919Swnj 3822608Swnj /* 3832608Swnj * Start activity on a tm controller. 3842608Swnj */ 3852608Swnj tmstart(um) 3862982Swnj register struct uba_ctlr *um; 3871919Swnj { 3882608Swnj register struct buf *bp, *dp; 3895692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3903095Swnj register struct te_softc *sc; 3912982Swnj register struct uba_device *ui; 3923095Swnj int teunit, cmd; 3932471Swnj daddr_t blkno; 3941919Swnj 3952608Swnj /* 3962608Swnj * Look for an idle transport on the controller. 3972608Swnj */ 3981919Swnj loop: 3992608Swnj if ((dp = um->um_tab.b_actf) == NULL) 4001919Swnj return; 4012608Swnj if ((bp = dp->b_actf) == NULL) { 4022608Swnj um->um_tab.b_actf = dp->b_forw; 4032608Swnj goto loop; 4042608Swnj } 4053095Swnj teunit = TEUNIT(bp->b_dev); 4063095Swnj ui = tedinfo[teunit]; 4072608Swnj /* 4082608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 4092608Swnj */ 4103095Swnj sc = &te_softc[teunit]; 4115692Sroot addr = (struct tmdevice *)um->um_addr; 4122608Swnj addr->tmcs = (ui->ui_slave << 8); 4132471Swnj sc->sc_dsreg = addr->tmcs; 4142471Swnj sc->sc_erreg = addr->tmer; 4152471Swnj sc->sc_resid = addr->tmbc; 4162608Swnj /* 4172608Swnj * Default is that last command was NOT a write command; 4182608Swnj * if we do a write command we will notice this in tmintr(). 4192608Swnj */ 4203493Sroot sc->sc_lastiow = 0; 4212608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4222608Swnj /* 4233095Swnj * Have had a hard error on a non-raw tape 4243095Swnj * or the tape unit is now unavailable 4253095Swnj * (e.g. taken off line). 4262608Swnj */ 4272608Swnj bp->b_flags |= B_ERROR; 4281919Swnj goto next; 4291919Swnj } 4303095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4313095Swnj /* 4323095Swnj * Execute control operation with the specified count. 4333095Swnj */ 4342608Swnj if (bp->b_command == TM_SENSE) 4352608Swnj goto next; 4363495Sroot /* 4373495Sroot * Set next state; give 5 minutes to complete 4383495Sroot * rewind, or 10 seconds per iteration (minimum 60 4393629Sroot * seconds and max 5 minutes) to complete other ops. 4403495Sroot */ 4413495Sroot if (bp->b_command == TM_REW) { 4423495Sroot um->um_tab.b_active = SREW; 4433495Sroot sc->sc_timo = 5 * 60; 4443495Sroot } else { 4453495Sroot um->um_tab.b_active = SCOM; 4464266Swnj sc->sc_timo = 4474266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4483495Sroot } 4492608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4502608Swnj addr->tmbc = bp->b_repcnt; 4512670Swnj goto dobpcmd; 4522608Swnj } 4532608Swnj /* 45439938Skarels * For raw I/O, fudge the current block number 45539938Skarels * so we don't seek except on a retry. 456*40045Smarc * For raw I/O, fudge the current block number 457*40045Smarc * so we don't seek except on a retry. 4583095Swnj */ 45934217Sbostic if (bp->b_flags & B_RAW) { 46034217Sbostic if (um->um_tab.b_errcnt == 0) { 46134217Sbostic sc->sc_blkno = bdbtofsb(bp->b_blkno); 46234217Sbostic sc->sc_nxrec = sc->sc_blkno + 1; 46334217Sbostic } 46439938Skarels } else { 465*40045Smarc } else { 4663095Swnj /* 46734217Sbostic * Handle boundary cases for operation 46834217Sbostic * on non-raw tapes. 4693095Swnj */ 47034217Sbostic if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 47134217Sbostic /* 47234217Sbostic * Can't read past known end-of-file. 47334217Sbostic */ 47434217Sbostic bp->b_flags |= B_ERROR; 47534217Sbostic bp->b_error = ENXIO; 47634217Sbostic goto next; 47734217Sbostic } 47834217Sbostic if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 47934217Sbostic bp->b_flags&B_READ) { 48034217Sbostic /* 48134217Sbostic * Reading at end of file returns 0 bytes. 48234217Sbostic */ 48334217Sbostic bp->b_resid = bp->b_bcount; 48434217Sbostic clrbuf(bp); 48534217Sbostic goto next; 48634217Sbostic } 48734217Sbostic if ((bp->b_flags&B_READ) == 0) 48834217Sbostic /* 48934217Sbostic * Writing sets EOF 49034217Sbostic */ 49134217Sbostic sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4923095Swnj } 4933095Swnj /* 4942608Swnj * If the data transfer command is in the correct place, 4952608Swnj * set up all the registers except the csr, and give 4962608Swnj * control over to the UNIBUS adapter routines, to 4972608Swnj * wait for resources to start the i/o. 4982608Swnj */ 4997381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 5002396Swnj addr->tmbc = -bp->b_bcount; 5011919Swnj if ((bp->b_flags&B_READ) == 0) { 50230917Skarels if (um->um_tab.b_errcnt && 50333477Skarels (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL) 5043095Swnj cmd = TM_WIRG; 5051919Swnj else 5063095Swnj cmd = TM_WCOM; 5071919Swnj } else 5083095Swnj cmd = TM_RCOM; 5092471Swnj um->um_tab.b_active = SIO; 5103095Swnj um->um_cmd = sc->sc_dens|cmd; 5112928Swnj #ifdef notdef 5122670Swnj if (tmreverseop(sc->sc_lastcmd)) 5133095Swnj while (addr->tmer & TMER_SDWN) 51424974Smckusick DELAY(10),tmgapsdcnt++; 5152670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 5162928Swnj #endif 5173495Sroot sc->sc_timo = 60; /* premature, but should serve */ 5183105Swnj (void) ubago(ui); 5191919Swnj return; 5201919Swnj } 5212608Swnj /* 5223095Swnj * Tape positioned incorrectly; 5233095Swnj * set to seek forwards or backwards to the correct spot. 5243095Swnj * This happens for raw tapes only on error retries. 5252608Swnj */ 5262471Swnj um->um_tab.b_active = SSEEK; 5277381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5282670Swnj bp->b_command = TM_SFORW; 5297381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5301919Swnj } else { 5312670Swnj bp->b_command = TM_SREV; 5327381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5331919Swnj } 5343629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5352670Swnj dobpcmd: 5362928Swnj #ifdef notdef 5373095Swnj /* 5383095Swnj * It is strictly necessary to wait for the tape 5393095Swnj * to stop before changing directions, but the TC11 5403095Swnj * handles this for us. 5413095Swnj */ 5422670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5432670Swnj while (addr->tmer & TM_SDWN) 54424974Smckusick DELAY(10),tmgapsdcnt++; 5452670Swnj sc->sc_lastcmd = bp->b_command; 5462928Swnj #endif 5473095Swnj /* 5483095Swnj * Do the command in bp. 5493095Swnj */ 5503095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5511919Swnj return; 5521919Swnj 5531919Swnj next: 5542608Swnj /* 5552608Swnj * Done with this operation due to error or 5562608Swnj * the fact that it doesn't do anything. 5572608Swnj * Release UBA resources (if any), dequeue 5582608Swnj * the transfer and continue processing this slave. 5592608Swnj */ 5602608Swnj if (um->um_ubinfo) 5612617Swnj ubadone(um); 5622608Swnj um->um_tab.b_errcnt = 0; 5632608Swnj dp->b_actf = bp->av_forw; 5641919Swnj iodone(bp); 5651919Swnj goto loop; 5661919Swnj } 5671919Swnj 5682608Swnj /* 5692608Swnj * The UNIBUS resources we needed have been 5702608Swnj * allocated to us; start the device. 5712608Swnj */ 5722574Swnj tmdgo(um) 5732982Swnj register struct uba_ctlr *um; 5741919Swnj { 5755692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5762471Swnj 5772574Swnj addr->tmba = um->um_ubinfo; 5782574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5792396Swnj } 5802396Swnj 5812608Swnj /* 5822608Swnj * Tm interrupt routine. 5832608Swnj */ 5842471Swnj /*ARGSUSED*/ 5852630Swnj tmintr(tm11) 5862630Swnj int tm11; 5872396Swnj { 5882608Swnj struct buf *dp; 5891919Swnj register struct buf *bp; 5902982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5915692Sroot register struct tmdevice *addr; 5923095Swnj register struct te_softc *sc; 5933095Swnj int teunit; 5941919Swnj register state; 5951919Swnj 5963095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5973095Swnj return; 5983095Swnj bp = dp->b_actf; 5993095Swnj teunit = TEUNIT(bp->b_dev); 6005692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 6013524Swnj sc = &te_softc[teunit]; 6022608Swnj /* 6032608Swnj * If last command was a rewind, and tape is still 6042608Swnj * rewinding, wait for the rewind complete interrupt. 6052608Swnj */ 6062608Swnj if (um->um_tab.b_active == SREW) { 6072608Swnj um->um_tab.b_active = SCOM; 6083524Swnj if (addr->tmer&TMER_RWS) { 6093524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 6102608Swnj return; 6113524Swnj } 6121919Swnj } 6132608Swnj /* 6142608Swnj * An operation completed... record status 6152608Swnj */ 6163495Sroot sc->sc_timo = INF; 61739938Skarels if (um->um_tab.b_active == SIO) 618*40045Smarc if (um->um_tab.b_active == SIO) 61933477Skarels sc->sc_ioerreg = addr->tmer; 6202471Swnj sc->sc_dsreg = addr->tmcs; 6212471Swnj sc->sc_erreg = addr->tmer; 6222471Swnj sc->sc_resid = addr->tmbc; 6231919Swnj if ((bp->b_flags & B_READ) == 0) 6242608Swnj sc->sc_lastiow = 1; 6252471Swnj state = um->um_tab.b_active; 6262471Swnj um->um_tab.b_active = 0; 6272608Swnj /* 6282608Swnj * Check for errors. 6292608Swnj */ 6302608Swnj if (addr->tmcs&TM_ERR) { 6313095Swnj while (addr->tmer & TMER_SDWN) 63224974Smckusick DELAY(10); /* await settle down */ 6332608Swnj /* 6343095Swnj * If we hit the end of the tape file, update our position. 6352608Swnj */ 6363095Swnj if (addr->tmer&TMER_EOF) { 6372608Swnj tmseteof(bp); /* set blkno and nxrec */ 6382608Swnj state = SCOM; /* force completion */ 6392608Swnj /* 6402608Swnj * Stuff bc so it will be unstuffed correctly 6412608Swnj * later to get resid. 6422608Swnj */ 6432396Swnj addr->tmbc = -bp->b_bcount; 6442608Swnj goto opdone; 6451919Swnj } 6462608Swnj /* 6473095Swnj * If we were reading raw tape and the only error was that the 6483095Swnj * record was too long, then we don't consider this an error. 6492608Swnj */ 65034217Sbostic if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) && 6513095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6522608Swnj goto ignoreerr; 6532608Swnj /* 6542608Swnj * If error is not hard, and this was an i/o operation 6552608Swnj * retry up to 8 times. 6562608Swnj */ 6573095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 658*40045Smarc if (um->um_tab.b_errcnt++ < 8) { 65930917Skarels if (tmdiag) 66030917Skarels log(LOG_DEBUG, 66130917Skarels "te%d: soft error bn%d er=%b\n", 66230917Skarels minor(bp->b_dev)&03, 66330917Skarels bp->b_blkno, sc->sc_erreg, 66430917Skarels TMER_BITS); 665*40045Smarc sc->sc_blkno++; /* force backspace */ 6662617Swnj ubadone(um); 6672608Swnj goto opcont; 6681919Swnj } 6692608Swnj } else 6702608Swnj /* 6712608Swnj * Hard or non-i/o errors on non-raw tape 6722608Swnj * cause it to close. 6732608Swnj */ 67434217Sbostic if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0) 6752608Swnj sc->sc_openf = -1; 6762608Swnj /* 6772608Swnj * Couldn't recover error 6782608Swnj */ 679*40045Smarc tprintf(sc->sc_ctty, 68018321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6813095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 68224974Smckusick #ifdef AVIV 68324974Smckusick if (tmdiag) { 68424974Smckusick addr->tmmr = DAB; 68524974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 68624974Smckusick addr->tmmr = DTS; 68724974Smckusick if (addr->tmmr & DTS_MASK) 68824974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 68924974Smckusick addr->tmmr = RWERR; 69024974Smckusick printf(", read/write errors %b\n", 69124974Smckusick addr->tmmr & RWERR_MASK, 69224974Smckusick RWERR_BITS); 69324974Smckusick addr->tmmr = DRSENSE; 69424974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 69524974Smckusick DRSENSE_BITS); 69624974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 69724974Smckusick } 69824974Smckusick #endif AVIV 6991919Swnj bp->b_flags |= B_ERROR; 7002608Swnj goto opdone; 7011919Swnj } 7022608Swnj /* 7032608Swnj * Advance tape control FSM. 7042608Swnj */ 7052608Swnj ignoreerr: 7061919Swnj switch (state) { 7071919Swnj 7081919Swnj case SIO: 7092608Swnj /* 7102608Swnj * Read/write increments tape block number 7112608Swnj */ 7122471Swnj sc->sc_blkno++; 71330917Skarels sc->sc_blks++; 71430917Skarels if (um->um_tab.b_errcnt) 71530917Skarels sc->sc_softerrs++; 7162608Swnj goto opdone; 7171919Swnj 7181919Swnj case SCOM: 7192608Swnj /* 7203095Swnj * For forward/backward space record update current position. 7212608Swnj */ 7223095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 72326292Skarels switch ((int)bp->b_command) { 7241919Swnj 7252608Swnj case TM_SFORW: 7262608Swnj sc->sc_blkno -= bp->b_repcnt; 7273095Swnj break; 7281919Swnj 7292608Swnj case TM_SREV: 7302608Swnj sc->sc_blkno += bp->b_repcnt; 7313095Swnj break; 7321919Swnj } 7333095Swnj goto opdone; 7341919Swnj 7351919Swnj case SSEEK: 7367381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7372608Swnj goto opcont; 7381919Swnj 7391919Swnj default: 7402608Swnj panic("tmintr"); 7412608Swnj } 7422608Swnj opdone: 7432608Swnj /* 7442608Swnj * Reset error count and remove 7452608Swnj * from device queue. 7462608Swnj */ 7472608Swnj um->um_tab.b_errcnt = 0; 7482608Swnj dp->b_actf = bp->av_forw; 74914929Skarels /* 75014929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 75114929Skarels */ 75215081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7532617Swnj ubadone(um); 7542608Swnj iodone(bp); 7552608Swnj /* 7562608Swnj * Circulate slave to end of controller 7572608Swnj * queue to give other slaves a chance. 7582608Swnj */ 7592608Swnj um->um_tab.b_actf = dp->b_forw; 7602608Swnj if (dp->b_actf) { 7612608Swnj dp->b_forw = NULL; 7622608Swnj if (um->um_tab.b_actf == NULL) 7632608Swnj um->um_tab.b_actf = dp; 7642608Swnj else 7652608Swnj um->um_tab.b_actl->b_forw = dp; 7662608Swnj um->um_tab.b_actl = dp; 7672608Swnj } 7682608Swnj if (um->um_tab.b_actf == 0) 7691919Swnj return; 7702608Swnj opcont: 7712608Swnj tmstart(um); 7721919Swnj } 7731919Swnj 7743495Sroot tmtimer(dev) 7753495Sroot int dev; 7763495Sroot { 7773495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7784847Sroot register short x; 7793495Sroot 7803495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7814278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7823495Sroot sc->sc_timo = INF; 7834847Sroot x = spl5(); 7843495Sroot tmintr(TMUNIT(dev)); 7854847Sroot (void) splx(x); 7863495Sroot } 7873629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7883495Sroot } 7893495Sroot 7901919Swnj tmseteof(bp) 7911919Swnj register struct buf *bp; 7921919Swnj { 7933095Swnj register int teunit = TEUNIT(bp->b_dev); 7945692Sroot register struct tmdevice *addr = 7955692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7963095Swnj register struct te_softc *sc = &te_softc[teunit]; 7971919Swnj 7983095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7997381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 8001919Swnj /* reversing */ 8017381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 8022471Swnj sc->sc_blkno = sc->sc_nxrec; 8031919Swnj } else { 8041919Swnj /* spacing forward */ 8057381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 8062471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 8071919Swnj } 8081919Swnj return; 8091919Swnj } 8101919Swnj /* eof on read */ 8117381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 8121919Swnj } 8131919Swnj 8142608Swnj tmreset(uban) 8152608Swnj int uban; 8162608Swnj { 8172982Swnj register struct uba_ctlr *um; 8183095Swnj register tm11, teunit; 8192982Swnj register struct uba_device *ui; 8202608Swnj register struct buf *dp; 8212608Swnj 8222630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8232630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8242608Swnj um->um_ubanum != uban) 8252608Swnj continue; 8262928Swnj printf(" tm%d", tm11); 8272608Swnj um->um_tab.b_active = 0; 8282608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8292608Swnj if (um->um_ubinfo) { 8302608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8319355Ssam um->um_ubinfo = 0; 8322608Swnj } 8335692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8343095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8353095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8363095Swnj ui->ui_alive == 0) 8372608Swnj continue; 8383095Swnj dp = &teutab[teunit]; 8392608Swnj dp->b_active = 0; 8402608Swnj dp->b_forw = 0; 8412608Swnj if (um->um_tab.b_actf == NULL) 8422608Swnj um->um_tab.b_actf = dp; 8432608Swnj else 8442608Swnj um->um_tab.b_actl->b_forw = dp; 8452608Swnj um->um_tab.b_actl = dp; 8463495Sroot if (te_softc[teunit].sc_openf > 0) 8473495Sroot te_softc[teunit].sc_openf = -1; 8482608Swnj } 8492608Swnj tmstart(um); 8502608Swnj } 8512608Swnj } 8522608Swnj 8531919Swnj /*ARGSUSED*/ 8547632Ssam tmioctl(dev, cmd, data, flag) 8557632Ssam caddr_t data; 8561919Swnj dev_t dev; 8571919Swnj { 8583095Swnj int teunit = TEUNIT(dev); 8593095Swnj register struct te_softc *sc = &te_softc[teunit]; 8603095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8611919Swnj register callcount; 8621919Swnj int fcount; 8637632Ssam struct mtop *mtop; 8647632Ssam struct mtget *mtget; 8651919Swnj /* we depend of the values and order of the MT codes here */ 8662608Swnj static tmops[] = 8672608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8681919Swnj 8692608Swnj switch (cmd) { 8707632Ssam 8717632Ssam case MTIOCTOP: /* tape operation */ 8727632Ssam mtop = (struct mtop *)data; 8738574Sroot switch (mtop->mt_op) { 8747632Ssam 8752608Swnj case MTWEOF: 8767632Ssam callcount = mtop->mt_count; 8772608Swnj fcount = 1; 8782608Swnj break; 8797632Ssam 8802608Swnj case MTFSF: case MTBSF: 8817632Ssam callcount = mtop->mt_count; 8821919Swnj fcount = INF; 8831919Swnj break; 8847632Ssam 8851919Swnj case MTFSR: case MTBSR: 8861919Swnj callcount = 1; 8877632Ssam fcount = mtop->mt_count; 8881919Swnj break; 8897632Ssam 8902324Skre case MTREW: case MTOFFL: case MTNOP: 8911919Swnj callcount = 1; 8921919Swnj fcount = 1; 8931919Swnj break; 8947632Ssam 8951919Swnj default: 8968574Sroot return (ENXIO); 8971919Swnj } 8988574Sroot if (callcount <= 0 || fcount <= 0) 8998574Sroot return (EINVAL); 9002608Swnj while (--callcount >= 0) { 9017632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 9027632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 9038574Sroot bp->b_resid) 9048574Sroot return (EIO); 9053095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9061919Swnj break; 9071919Swnj } 9088648Sroot return (geterror(bp)); 9097632Ssam 9101919Swnj case MTIOCGET: 9117632Ssam mtget = (struct mtget *)data; 9127632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9137632Ssam mtget->mt_erreg = sc->sc_erreg; 9147632Ssam mtget->mt_resid = sc->sc_resid; 9157632Ssam mtget->mt_type = MT_ISTM; 9168574Sroot break; 9177632Ssam 9181919Swnj default: 9198574Sroot return (ENXIO); 9201919Swnj } 9218574Sroot return (0); 9221919Swnj } 9231919Swnj 9241919Swnj #define DBSIZE 20 9251919Swnj 9262363Swnj tmdump() 9272363Swnj { 9282982Swnj register struct uba_device *ui; 9292396Swnj register struct uba_regs *up; 9305692Sroot register struct tmdevice *addr; 9312426Skre int blk, num; 9322426Skre int start; 9331919Swnj 9342426Skre start = 0; 9352426Skre num = maxfree; 9362426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9373095Swnj if (tedinfo[0] == 0) 9382887Swnj return (ENXIO); 9393095Swnj ui = phys(tedinfo[0], struct uba_device *); 9402396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9413331Swnj ubainit(up); 9422324Skre DELAY(1000000); 9435692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9442396Swnj tmwait(addr); 9452608Swnj addr->tmcs = TM_DCLR | TM_GO; 9461919Swnj while (num > 0) { 9471919Swnj blk = num > DBSIZE ? DBSIZE : num; 9482396Swnj tmdwrite(start, blk, addr, up); 9491919Swnj start += blk; 9501919Swnj num -= blk; 9511919Swnj } 9522426Skre tmeof(addr); 9532426Skre tmeof(addr); 9542426Skre tmwait(addr); 9552887Swnj if (addr->tmcs&TM_ERR) 9562887Swnj return (EIO); 9572608Swnj addr->tmcs = TM_REW | TM_GO; 9582471Swnj tmwait(addr); 9592363Swnj return (0); 9601919Swnj } 9611919Swnj 9622608Swnj tmdwrite(dbuf, num, addr, up) 9632608Swnj register dbuf, num; 9645692Sroot register struct tmdevice *addr; 9652396Swnj struct uba_regs *up; 9661919Swnj { 9672396Swnj register struct pte *io; 9682396Swnj register int npf; 9691928Swnj 9702396Swnj tmwait(addr); 9712396Swnj io = up->uba_map; 9721919Swnj npf = num+1; 9731928Swnj while (--npf != 0) 9742982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9752396Swnj *(int *)io = 0; 9762396Swnj addr->tmbc = -(num*NBPG); 9772396Swnj addr->tmba = 0; 9782608Swnj addr->tmcs = TM_WCOM | TM_GO; 9791919Swnj } 9801919Swnj 9812396Swnj tmwait(addr) 9825692Sroot register struct tmdevice *addr; 9831919Swnj { 9841928Swnj register s; 9851919Swnj 9861919Swnj do 9872396Swnj s = addr->tmcs; 9882608Swnj while ((s & TM_CUR) == 0); 9891919Swnj } 9901919Swnj 9912396Swnj tmeof(addr) 9925692Sroot struct tmdevice *addr; 9931919Swnj { 9941919Swnj 9952396Swnj tmwait(addr); 9962608Swnj addr->tmcs = TM_WEOF | TM_GO; 9971919Swnj } 9981919Swnj #endif 999