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*44395Smarc * @(#)tm.c 7.12 (Berkeley) 06/28/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 "conf.h" 2517079Sbloom #include "user.h" 2617079Sbloom #include "map.h" 2717079Sbloom #include "vm.h" 2817079Sbloom #include "ioctl.h" 2917079Sbloom #include "mtio.h" 3017079Sbloom #include "cmap.h" 3117079Sbloom #include "uio.h" 3217079Sbloom #include "kernel.h" 3330917Skarels #include "syslog.h" 34*44395Smarc #include "tprintf.h" 351919Swnj 3637511Smckusick #include "machine/pte.h" 378479Sroot #include "../vax/cpu.h" 3817079Sbloom #include "ubareg.h" 3917079Sbloom #include "ubavar.h" 4017079Sbloom #include "tmreg.h" 411919Swnj 423095Swnj /* 433095Swnj * There is a ctmbuf per tape controller. 443095Swnj * It is used as the token to pass to the internal routines 453095Swnj * to execute tape ioctls, and also acts as a lock on the slaves 463095Swnj * on the controller, since there is only one per controller. 473095Swnj * In particular, when the tape is rewinding on close we release 483095Swnj * the user process but any further attempts to use the tape drive 493095Swnj * before the rewind completes will hang waiting for ctmbuf. 503095Swnj */ 513095Swnj struct buf ctmbuf[NTM]; 521919Swnj 533095Swnj /* 543095Swnj * Driver unibus interface routines and variables. 553095Swnj */ 562608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 572982Swnj struct uba_ctlr *tmminfo[NTM]; 583095Swnj struct uba_device *tedinfo[NTE]; 593095Swnj struct buf teutab[NTE]; 603095Swnj short tetotm[NTE]; 612458Swnj u_short tmstd[] = { 0772520, 0 }; 622396Swnj struct uba_driver tmdriver = 633095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 641919Swnj 651919Swnj /* bits in minor device */ 663095Swnj #define TEUNIT(dev) (minor(dev)&03) 673095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 681919Swnj #define T_NOREWIND 04 6924974Smckusick #define T_1600BPI 0x8 701919Swnj 711919Swnj #define INF (daddr_t)1000000L 721919Swnj 732608Swnj /* 742608Swnj * Software state per tape transport. 753095Swnj * 763095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 773095Swnj * 2. We keep track of the current position on a block tape and seek 783095Swnj * before operations by forward/back spacing if necessary. 793095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 803095Swnj * is open read write and the last thing done is a write we can 813095Swnj * write a standard end of tape mark (two eofs). 823095Swnj * 4. We remember the status registers after the last command, using 833095Swnj * then internally and returning them to the SENSE ioctl. 843095Swnj * 5. We remember the last density the tape was used at. If it is 853095Swnj * not a BOT when we start using it and we are writing, we don't 863095Swnj * let the density be changed. 872608Swnj */ 883095Swnj struct te_softc { 892608Swnj char sc_openf; /* lock against multiple opens */ 902608Swnj char sc_lastiow; /* last op was a write */ 912608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 923095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 932608Swnj u_short sc_erreg; /* copy of last erreg */ 9433477Skarels u_short sc_ioerreg; /* copy of last erreg for I/O command */ 952608Swnj u_short sc_dsreg; /* copy of last dsreg */ 962608Swnj short sc_resid; /* copy of last bc */ 973105Swnj #ifdef unneeded 982670Swnj short sc_lastcmd; /* last command to handle direction changes */ 992928Swnj #endif 1003095Swnj u_short sc_dens; /* prototype command with density info */ 10118321Sralph short sc_tact; /* timeout is active */ 1023495Sroot daddr_t sc_timo; /* time until timeout expires */ 10330917Skarels int sc_blks; /* number of I/O operations since open */ 10430917Skarels int sc_softerrs; /* number of soft I/O errors since open */ 105*44395Smarc tpr_t sc_tpr; /* tprintf handle */ 1066952Swnj } te_softc[NTE]; 1073105Swnj #ifdef unneeded 1083105Swnj int tmgapsdcnt; /* DEBUG */ 1093105Swnj #endif 1101919Swnj 1112608Swnj /* 1123095Swnj * States for um->um_tab.b_active, the per controller state flag. 1133095Swnj * This is used to sequence control in the driver. 1142608Swnj */ 1151919Swnj #define SSEEK 1 /* seeking */ 1161919Swnj #define SIO 2 /* doing seq i/o */ 1171919Swnj #define SCOM 3 /* sending control command */ 1182608Swnj #define SREW 4 /* sending a drive rewind */ 1191919Swnj 1202426Skre /* 1212426Skre * Determine if there is a controller for 1222426Skre * a tm at address reg. Our goal is to make the 1232426Skre * device interrupt. 1242426Skre */ 1252608Swnj tmprobe(reg) 1262396Swnj caddr_t reg; 1272396Swnj { 1283095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1292426Skre 1302608Swnj #ifdef lint 1313105Swnj br = 0; cvec = br; br = cvec; 1324936Swnj tmintr(0); 1332608Swnj #endif 1345692Sroot ((struct tmdevice *)reg)->tmcs = TM_IE; 1352396Swnj /* 1362630Swnj * If this is a tm11, it ought to have interrupted 1372396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1382458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1392458Swnj * Just in case, we will reference one 1402396Swnj * of the more distant registers, and hope for a machine 1412458Swnj * check, or similar disaster if this is a ts. 1422471Swnj * 1432471Swnj * Note: on an 11/780, badaddr will just generate 1442471Swnj * a uba error for a ts; but our caller will notice that 1452471Swnj * so we won't check for it. 1462396Swnj */ 1475692Sroot if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2)) 1482458Swnj return (0); 1497404Skre return (sizeof (struct tmdevice)); 1502396Swnj } 1512396Swnj 1522608Swnj /* 1532608Swnj * Due to a design flaw, we cannot ascertain if the tape 1542608Swnj * exists or not unless it is on line - ie: unless a tape is 1552608Swnj * mounted. This is too servere a restriction to bear, 1562608Swnj * so all units are assumed to exist. 1572608Swnj */ 1582608Swnj /*ARGSUSED*/ 1592574Swnj tmslave(ui, reg) 1602982Swnj struct uba_device *ui; 1612396Swnj caddr_t reg; 1622396Swnj { 1632458Swnj 1642458Swnj return (1); 1652396Swnj } 1662396Swnj 1672608Swnj /* 1683095Swnj * Record attachment of the unit to the controller. 1692608Swnj */ 1702608Swnj /*ARGSUSED*/ 1712608Swnj tmattach(ui) 1722982Swnj struct uba_device *ui; 1732608Swnj { 1743095Swnj /* 17534217Sbostic * Tetotm is used in TMUNIT to index the ctmbuf 17634217Sbostic * array given a te unit number. 1773095Swnj */ 1783095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1792608Swnj } 1802608Swnj 1813495Sroot int tmtimer(); 1822608Swnj /* 1832608Swnj * Open the device. Tapes are unique open 1842608Swnj * devices, so we refuse if it is already open. 1852608Swnj * We also check that a tape is available, and 1863095Swnj * don't block waiting here; if you want to wait 1873095Swnj * for a tape you should timeout in user code. 1882608Swnj */ 18924974Smckusick 19024974Smckusick #ifdef AVIV 19124974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 }; 19230917Skarels #endif AVIV 19324974Smckusick int tmdiag; 19424974Smckusick 1951919Swnj tmopen(dev, flag) 1961919Swnj dev_t dev; 1971919Swnj int flag; 1981919Swnj { 1993095Swnj register int teunit; 2002982Swnj register struct uba_device *ui; 2013095Swnj register struct te_softc *sc; 20240725Skarels int olddens, dens, error; 2035437Sroot int s; 2041919Swnj 2053095Swnj teunit = TEUNIT(dev); 20625051Skarels if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2078574Sroot return (ENXIO); 20825051Skarels if ((sc = &te_softc[teunit])->sc_openf) 20925051Skarels return (EBUSY); 21030917Skarels sc->sc_openf = 1; 2113209Swnj olddens = sc->sc_dens; 2123209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 21324974Smckusick #ifndef AVIV 2143209Swnj if ((minor(dev) & T_1600BPI) == 0) 2153209Swnj dens |= TM_D800; 21624974Smckusick #else AVIV 21724974Smckusick dens |= tmdens[(minor(dev)>>3)&03]; 21824974Smckusick #endif AVIV 2193209Swnj sc->sc_dens = dens; 2203141Swnj get: 2212608Swnj tmcommand(dev, TM_SENSE, 1); 2223141Swnj if (sc->sc_erreg&TMER_SDWN) { 22340725Skarels if (error = tsleep((caddr_t)&lbolt, (PZERO+1) | PCATCH, 22440725Skarels devopn, 0)) 22540725Skarels return (error); 2263141Swnj goto get; 2273141Swnj } 2283209Swnj sc->sc_dens = olddens; 2293710Sroot if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) { 2303710Sroot uprintf("te%d: not online\n", teunit); 23130917Skarels sc->sc_openf = 0; 2328574Sroot return (EIO); 2331919Swnj } 2343710Sroot if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) { 2353710Sroot uprintf("te%d: no write ring\n", teunit); 23630917Skarels sc->sc_openf = 0; 2378574Sroot return (EIO); 2383710Sroot } 2393710Sroot if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2403710Sroot dens != sc->sc_dens) { 2413710Sroot uprintf("te%d: can't change density in mid-tape\n", teunit); 24230917Skarels sc->sc_openf = 0; 2438574Sroot return (EIO); 2443710Sroot } 2452471Swnj sc->sc_blkno = (daddr_t)0; 2462471Swnj sc->sc_nxrec = INF; 2472608Swnj sc->sc_lastiow = 0; 2483095Swnj sc->sc_dens = dens; 24930917Skarels sc->sc_blks = 0; 25030917Skarels sc->sc_softerrs = 0; 251*44395Smarc sc->sc_tpr = tprintf_open(); 25226369Skarels s = splclock(); 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); 28930917Skarels if (sc->sc_blks > 100 && sc->sc_softerrs > sc->sc_blks / 100) 29030917Skarels log(LOG_INFO, "te%d: %d soft errors in %d blocks\n", 29130917Skarels TEUNIT(dev), sc->sc_softerrs, sc->sc_blks); 292*44395Smarc tprintf_close(sc->sc_tpr); 2932471Swnj sc->sc_openf = 0; 29430917Skarels return (0); 2951919Swnj } 2961919Swnj 2972608Swnj /* 2982608Swnj * Execute a command on the tape drive 2992608Swnj * a specified number of times. 3002608Swnj */ 3012574Swnj tmcommand(dev, com, count) 3021919Swnj dev_t dev; 3031919Swnj int com, count; 3041919Swnj { 3051919Swnj register struct buf *bp; 3065437Sroot register int s; 3071919Swnj 3082608Swnj bp = &ctmbuf[TMUNIT(dev)]; 3095437Sroot s = spl5(); 3101919Swnj while (bp->b_flags&B_BUSY) { 3113095Swnj /* 3123095Swnj * This special check is because B_BUSY never 3133095Swnj * gets cleared in the non-waiting rewind case. 3143095Swnj */ 3153141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3163095Swnj break; 3171919Swnj bp->b_flags |= B_WANTED; 3181919Swnj sleep((caddr_t)bp, PRIBIO); 3191919Swnj } 3201919Swnj bp->b_flags = B_BUSY|B_READ; 3215437Sroot splx(s); 3221919Swnj bp->b_dev = dev; 3231919Swnj bp->b_repcnt = -count; 3241919Swnj bp->b_command = com; 3251919Swnj bp->b_blkno = 0; 3261919Swnj tmstrategy(bp); 3273095Swnj /* 3283095Swnj * In case of rewind from close, don't wait. 3293095Swnj * This is the only case where count can be 0. 3303095Swnj */ 3313095Swnj if (count == 0) 3323095Swnj return; 3331919Swnj iowait(bp); 3341919Swnj if (bp->b_flags&B_WANTED) 3351919Swnj wakeup((caddr_t)bp); 3361919Swnj bp->b_flags &= B_ERROR; 3371919Swnj } 3381919Swnj 3392608Swnj /* 3403095Swnj * Queue a tape operation. 3412608Swnj */ 3421919Swnj tmstrategy(bp) 3431919Swnj register struct buf *bp; 3441919Swnj { 3453095Swnj int teunit = TEUNIT(bp->b_dev); 3462982Swnj register struct uba_ctlr *um; 3472608Swnj register struct buf *dp; 3485437Sroot int s; 3491919Swnj 3502608Swnj /* 3512608Swnj * Put transfer at end of unit queue 3522608Swnj */ 3533095Swnj dp = &teutab[teunit]; 3541919Swnj bp->av_forw = NULL; 3555437Sroot s = spl5(); 3563939Sbugs um = tedinfo[teunit]->ui_mi; 3572608Swnj if (dp->b_actf == NULL) { 3582608Swnj dp->b_actf = bp; 3592608Swnj /* 3602608Swnj * Transport not already active... 3612608Swnj * put at end of controller queue. 3622608Swnj */ 3632608Swnj dp->b_forw = NULL; 3642608Swnj if (um->um_tab.b_actf == NULL) 3652608Swnj um->um_tab.b_actf = dp; 3662608Swnj else 3672608Swnj um->um_tab.b_actl->b_forw = dp; 3682608Swnj um->um_tab.b_actl = dp; 3692608Swnj } else 3702608Swnj dp->b_actl->av_forw = bp; 3712608Swnj dp->b_actl = bp; 3722608Swnj /* 3732608Swnj * If the controller is not busy, get 3742608Swnj * it going. 3752608Swnj */ 3762608Swnj if (um->um_tab.b_active == 0) 3772608Swnj tmstart(um); 3785437Sroot splx(s); 3791919Swnj } 3801919Swnj 3812608Swnj /* 3822608Swnj * Start activity on a tm controller. 3832608Swnj */ 3842608Swnj tmstart(um) 3852982Swnj register struct uba_ctlr *um; 3861919Swnj { 3872608Swnj register struct buf *bp, *dp; 3885692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3893095Swnj register struct te_softc *sc; 3902982Swnj register struct uba_device *ui; 3913095Swnj int teunit, cmd; 3922471Swnj daddr_t blkno; 3931919Swnj 3942608Swnj /* 3952608Swnj * Look for an idle transport on the controller. 3962608Swnj */ 3971919Swnj loop: 3982608Swnj if ((dp = um->um_tab.b_actf) == NULL) 3991919Swnj return; 4002608Swnj if ((bp = dp->b_actf) == NULL) { 4012608Swnj um->um_tab.b_actf = dp->b_forw; 4022608Swnj goto loop; 4032608Swnj } 4043095Swnj teunit = TEUNIT(bp->b_dev); 4053095Swnj ui = tedinfo[teunit]; 4062608Swnj /* 4072608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 4082608Swnj */ 4093095Swnj sc = &te_softc[teunit]; 4105692Sroot addr = (struct tmdevice *)um->um_addr; 4112608Swnj addr->tmcs = (ui->ui_slave << 8); 4122471Swnj sc->sc_dsreg = addr->tmcs; 4132471Swnj sc->sc_erreg = addr->tmer; 4142471Swnj sc->sc_resid = addr->tmbc; 4152608Swnj /* 4162608Swnj * Default is that last command was NOT a write command; 4172608Swnj * if we do a write command we will notice this in tmintr(). 4182608Swnj */ 4193493Sroot sc->sc_lastiow = 0; 4202608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4212608Swnj /* 4223095Swnj * Have had a hard error on a non-raw tape 4233095Swnj * or the tape unit is now unavailable 4243095Swnj * (e.g. taken off line). 4252608Swnj */ 4262608Swnj bp->b_flags |= B_ERROR; 4271919Swnj goto next; 4281919Swnj } 4293095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4303095Swnj /* 4313095Swnj * Execute control operation with the specified count. 4323095Swnj */ 4332608Swnj if (bp->b_command == TM_SENSE) 4342608Swnj goto next; 4353495Sroot /* 4363495Sroot * Set next state; give 5 minutes to complete 4373495Sroot * rewind, or 10 seconds per iteration (minimum 60 4383629Sroot * seconds and max 5 minutes) to complete other ops. 4393495Sroot */ 4403495Sroot if (bp->b_command == TM_REW) { 4413495Sroot um->um_tab.b_active = SREW; 4423495Sroot sc->sc_timo = 5 * 60; 4433495Sroot } else { 4443495Sroot um->um_tab.b_active = SCOM; 4454266Swnj sc->sc_timo = 4464266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4473495Sroot } 4482608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4492608Swnj addr->tmbc = bp->b_repcnt; 4502670Swnj goto dobpcmd; 4512608Swnj } 4522608Swnj /* 45339938Skarels * For raw I/O, fudge the current block number 45439938Skarels * so we don't seek except on a retry. 45540045Smarc * For raw I/O, fudge the current block number 45640045Smarc * so we don't seek except on a retry. 4573095Swnj */ 45834217Sbostic if (bp->b_flags & B_RAW) { 45934217Sbostic if (um->um_tab.b_errcnt == 0) { 46034217Sbostic sc->sc_blkno = bdbtofsb(bp->b_blkno); 46134217Sbostic sc->sc_nxrec = sc->sc_blkno + 1; 46234217Sbostic } 46339938Skarels } else { 4643095Swnj /* 46534217Sbostic * Handle boundary cases for operation 46634217Sbostic * on non-raw tapes. 4673095Swnj */ 46834217Sbostic if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 46934217Sbostic /* 47034217Sbostic * Can't read past known end-of-file. 47134217Sbostic */ 47234217Sbostic bp->b_flags |= B_ERROR; 47334217Sbostic bp->b_error = ENXIO; 47434217Sbostic goto next; 47534217Sbostic } 47634217Sbostic if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 47734217Sbostic bp->b_flags&B_READ) { 47834217Sbostic /* 47934217Sbostic * Reading at end of file returns 0 bytes. 48034217Sbostic */ 48134217Sbostic bp->b_resid = bp->b_bcount; 48234217Sbostic clrbuf(bp); 48334217Sbostic goto next; 48434217Sbostic } 48534217Sbostic if ((bp->b_flags&B_READ) == 0) 48634217Sbostic /* 48734217Sbostic * Writing sets EOF 48834217Sbostic */ 48934217Sbostic sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4903095Swnj } 4913095Swnj /* 4922608Swnj * If the data transfer command is in the correct place, 4932608Swnj * set up all the registers except the csr, and give 4942608Swnj * control over to the UNIBUS adapter routines, to 4952608Swnj * wait for resources to start the i/o. 4962608Swnj */ 4977381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4982396Swnj addr->tmbc = -bp->b_bcount; 4991919Swnj if ((bp->b_flags&B_READ) == 0) { 50030917Skarels if (um->um_tab.b_errcnt && 50133477Skarels (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL) 5023095Swnj cmd = TM_WIRG; 5031919Swnj else 5043095Swnj cmd = TM_WCOM; 5051919Swnj } else 5063095Swnj cmd = TM_RCOM; 5072471Swnj um->um_tab.b_active = SIO; 5083095Swnj um->um_cmd = sc->sc_dens|cmd; 5092928Swnj #ifdef notdef 5102670Swnj if (tmreverseop(sc->sc_lastcmd)) 5113095Swnj while (addr->tmer & TMER_SDWN) 51224974Smckusick DELAY(10),tmgapsdcnt++; 5132670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 5142928Swnj #endif 5153495Sroot sc->sc_timo = 60; /* premature, but should serve */ 5163105Swnj (void) ubago(ui); 5171919Swnj return; 5181919Swnj } 5192608Swnj /* 5203095Swnj * Tape positioned incorrectly; 5213095Swnj * set to seek forwards or backwards to the correct spot. 5223095Swnj * This happens for raw tapes only on error retries. 5232608Swnj */ 5242471Swnj um->um_tab.b_active = SSEEK; 5257381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5262670Swnj bp->b_command = TM_SFORW; 5277381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5281919Swnj } else { 5292670Swnj bp->b_command = TM_SREV; 5307381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5311919Swnj } 5323629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5332670Swnj dobpcmd: 5342928Swnj #ifdef notdef 5353095Swnj /* 5363095Swnj * It is strictly necessary to wait for the tape 5373095Swnj * to stop before changing directions, but the TC11 5383095Swnj * handles this for us. 5393095Swnj */ 5402670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5412670Swnj while (addr->tmer & TM_SDWN) 54224974Smckusick DELAY(10),tmgapsdcnt++; 5432670Swnj sc->sc_lastcmd = bp->b_command; 5442928Swnj #endif 5453095Swnj /* 5463095Swnj * Do the command in bp. 5473095Swnj */ 5483095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5491919Swnj return; 5501919Swnj 5511919Swnj next: 5522608Swnj /* 5532608Swnj * Done with this operation due to error or 5542608Swnj * the fact that it doesn't do anything. 5552608Swnj * Release UBA resources (if any), dequeue 5562608Swnj * the transfer and continue processing this slave. 5572608Swnj */ 5582608Swnj if (um->um_ubinfo) 5592617Swnj ubadone(um); 5602608Swnj um->um_tab.b_errcnt = 0; 5612608Swnj dp->b_actf = bp->av_forw; 5621919Swnj iodone(bp); 5631919Swnj goto loop; 5641919Swnj } 5651919Swnj 5662608Swnj /* 5672608Swnj * The UNIBUS resources we needed have been 5682608Swnj * allocated to us; start the device. 5692608Swnj */ 5702574Swnj tmdgo(um) 5712982Swnj register struct uba_ctlr *um; 5721919Swnj { 5735692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5742471Swnj 5752574Swnj addr->tmba = um->um_ubinfo; 5762574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5772396Swnj } 5782396Swnj 5792608Swnj /* 5802608Swnj * Tm interrupt routine. 5812608Swnj */ 5822471Swnj /*ARGSUSED*/ 5832630Swnj tmintr(tm11) 5842630Swnj int tm11; 5852396Swnj { 5862608Swnj struct buf *dp; 5871919Swnj register struct buf *bp; 5882982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5895692Sroot register struct tmdevice *addr; 5903095Swnj register struct te_softc *sc; 5913095Swnj int teunit; 5921919Swnj register state; 5931919Swnj 5943095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5953095Swnj return; 5963095Swnj bp = dp->b_actf; 5973095Swnj teunit = TEUNIT(bp->b_dev); 5985692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5993524Swnj sc = &te_softc[teunit]; 6002608Swnj /* 6012608Swnj * If last command was a rewind, and tape is still 6022608Swnj * rewinding, wait for the rewind complete interrupt. 6032608Swnj */ 6042608Swnj if (um->um_tab.b_active == SREW) { 6052608Swnj um->um_tab.b_active = SCOM; 6063524Swnj if (addr->tmer&TMER_RWS) { 6073524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 6082608Swnj return; 6093524Swnj } 6101919Swnj } 6112608Swnj /* 6122608Swnj * An operation completed... record status 6132608Swnj */ 6143495Sroot sc->sc_timo = INF; 61539938Skarels if (um->um_tab.b_active == SIO) 61640045Smarc if (um->um_tab.b_active == SIO) 61733477Skarels sc->sc_ioerreg = addr->tmer; 6182471Swnj sc->sc_dsreg = addr->tmcs; 6192471Swnj sc->sc_erreg = addr->tmer; 6202471Swnj sc->sc_resid = addr->tmbc; 6211919Swnj if ((bp->b_flags & B_READ) == 0) 6222608Swnj sc->sc_lastiow = 1; 6232471Swnj state = um->um_tab.b_active; 6242471Swnj um->um_tab.b_active = 0; 6252608Swnj /* 6262608Swnj * Check for errors. 6272608Swnj */ 6282608Swnj if (addr->tmcs&TM_ERR) { 6293095Swnj while (addr->tmer & TMER_SDWN) 63024974Smckusick DELAY(10); /* await settle down */ 6312608Swnj /* 6323095Swnj * If we hit the end of the tape file, update our position. 6332608Swnj */ 6343095Swnj if (addr->tmer&TMER_EOF) { 6352608Swnj tmseteof(bp); /* set blkno and nxrec */ 6362608Swnj state = SCOM; /* force completion */ 6372608Swnj /* 6382608Swnj * Stuff bc so it will be unstuffed correctly 6392608Swnj * later to get resid. 6402608Swnj */ 6412396Swnj addr->tmbc = -bp->b_bcount; 6422608Swnj goto opdone; 6431919Swnj } 6442608Swnj /* 6453095Swnj * If we were reading raw tape and the only error was that the 6463095Swnj * record was too long, then we don't consider this an error. 6472608Swnj */ 64834217Sbostic if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) && 6493095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6502608Swnj goto ignoreerr; 6512608Swnj /* 6522608Swnj * If error is not hard, and this was an i/o operation 6532608Swnj * retry up to 8 times. 6542608Swnj */ 6553095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 65640045Smarc if (um->um_tab.b_errcnt++ < 8) { 65730917Skarels if (tmdiag) 65830917Skarels log(LOG_DEBUG, 65930917Skarels "te%d: soft error bn%d er=%b\n", 66030917Skarels minor(bp->b_dev)&03, 66130917Skarels bp->b_blkno, sc->sc_erreg, 66230917Skarels TMER_BITS); 66340045Smarc sc->sc_blkno++; /* force backspace */ 6642617Swnj ubadone(um); 6652608Swnj goto opcont; 6661919Swnj } 6672608Swnj } else 6682608Swnj /* 6692608Swnj * Hard or non-i/o errors on non-raw tape 6702608Swnj * cause it to close. 6712608Swnj */ 67234217Sbostic if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0) 6732608Swnj sc->sc_openf = -1; 6742608Swnj /* 6752608Swnj * Couldn't recover error 6762608Swnj */ 677*44395Smarc tprintf(sc->sc_tpr, 67818321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6793095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 68024974Smckusick #ifdef AVIV 68124974Smckusick if (tmdiag) { 68224974Smckusick addr->tmmr = DAB; 68324974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 68424974Smckusick addr->tmmr = DTS; 68524974Smckusick if (addr->tmmr & DTS_MASK) 68624974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 68724974Smckusick addr->tmmr = RWERR; 68824974Smckusick printf(", read/write errors %b\n", 68924974Smckusick addr->tmmr & RWERR_MASK, 69024974Smckusick RWERR_BITS); 69124974Smckusick addr->tmmr = DRSENSE; 69224974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 69324974Smckusick DRSENSE_BITS); 69424974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 69524974Smckusick } 69624974Smckusick #endif AVIV 6971919Swnj bp->b_flags |= B_ERROR; 6982608Swnj goto opdone; 6991919Swnj } 7002608Swnj /* 7012608Swnj * Advance tape control FSM. 7022608Swnj */ 7032608Swnj ignoreerr: 7041919Swnj switch (state) { 7051919Swnj 7061919Swnj case SIO: 7072608Swnj /* 7082608Swnj * Read/write increments tape block number 7092608Swnj */ 7102471Swnj sc->sc_blkno++; 71130917Skarels sc->sc_blks++; 71230917Skarels if (um->um_tab.b_errcnt) 71330917Skarels sc->sc_softerrs++; 7142608Swnj goto opdone; 7151919Swnj 7161919Swnj case SCOM: 7172608Swnj /* 7183095Swnj * For forward/backward space record update current position. 7192608Swnj */ 7203095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 72126292Skarels switch ((int)bp->b_command) { 7221919Swnj 7232608Swnj case TM_SFORW: 7242608Swnj sc->sc_blkno -= bp->b_repcnt; 7253095Swnj break; 7261919Swnj 7272608Swnj case TM_SREV: 7282608Swnj sc->sc_blkno += bp->b_repcnt; 7293095Swnj break; 7301919Swnj } 7313095Swnj goto opdone; 7321919Swnj 7331919Swnj case SSEEK: 7347381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7352608Swnj goto opcont; 7361919Swnj 7371919Swnj default: 7382608Swnj panic("tmintr"); 7392608Swnj } 7402608Swnj opdone: 7412608Swnj /* 7422608Swnj * Reset error count and remove 7432608Swnj * from device queue. 7442608Swnj */ 7452608Swnj um->um_tab.b_errcnt = 0; 7462608Swnj dp->b_actf = bp->av_forw; 74714929Skarels /* 74814929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 74914929Skarels */ 75015081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7512617Swnj ubadone(um); 7522608Swnj iodone(bp); 7532608Swnj /* 7542608Swnj * Circulate slave to end of controller 7552608Swnj * queue to give other slaves a chance. 7562608Swnj */ 7572608Swnj um->um_tab.b_actf = dp->b_forw; 7582608Swnj if (dp->b_actf) { 7592608Swnj dp->b_forw = NULL; 7602608Swnj if (um->um_tab.b_actf == NULL) 7612608Swnj um->um_tab.b_actf = dp; 7622608Swnj else 7632608Swnj um->um_tab.b_actl->b_forw = dp; 7642608Swnj um->um_tab.b_actl = dp; 7652608Swnj } 7662608Swnj if (um->um_tab.b_actf == 0) 7671919Swnj return; 7682608Swnj opcont: 7692608Swnj tmstart(um); 7701919Swnj } 7711919Swnj 7723495Sroot tmtimer(dev) 7733495Sroot int dev; 7743495Sroot { 7753495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7764847Sroot register short x; 7773495Sroot 7783495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7794278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7803495Sroot sc->sc_timo = INF; 7814847Sroot x = spl5(); 7823495Sroot tmintr(TMUNIT(dev)); 7834847Sroot (void) splx(x); 7843495Sroot } 7853629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7863495Sroot } 7873495Sroot 7881919Swnj tmseteof(bp) 7891919Swnj register struct buf *bp; 7901919Swnj { 7913095Swnj register int teunit = TEUNIT(bp->b_dev); 7925692Sroot register struct tmdevice *addr = 7935692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7943095Swnj register struct te_softc *sc = &te_softc[teunit]; 7951919Swnj 7963095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7977381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7981919Swnj /* reversing */ 7997381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 8002471Swnj sc->sc_blkno = sc->sc_nxrec; 8011919Swnj } else { 8021919Swnj /* spacing forward */ 8037381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 8042471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 8051919Swnj } 8061919Swnj return; 8071919Swnj } 8081919Swnj /* eof on read */ 8097381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 8101919Swnj } 8111919Swnj 8122608Swnj tmreset(uban) 8132608Swnj int uban; 8142608Swnj { 8152982Swnj register struct uba_ctlr *um; 8163095Swnj register tm11, teunit; 8172982Swnj register struct uba_device *ui; 8182608Swnj register struct buf *dp; 8192608Swnj 8202630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8212630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8222608Swnj um->um_ubanum != uban) 8232608Swnj continue; 8242928Swnj printf(" tm%d", tm11); 8252608Swnj um->um_tab.b_active = 0; 8262608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8272608Swnj if (um->um_ubinfo) { 8282608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8299355Ssam um->um_ubinfo = 0; 8302608Swnj } 8315692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8323095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8333095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8343095Swnj ui->ui_alive == 0) 8352608Swnj continue; 8363095Swnj dp = &teutab[teunit]; 8372608Swnj dp->b_active = 0; 8382608Swnj dp->b_forw = 0; 8392608Swnj if (um->um_tab.b_actf == NULL) 8402608Swnj um->um_tab.b_actf = dp; 8412608Swnj else 8422608Swnj um->um_tab.b_actl->b_forw = dp; 8432608Swnj um->um_tab.b_actl = dp; 8443495Sroot if (te_softc[teunit].sc_openf > 0) 8453495Sroot te_softc[teunit].sc_openf = -1; 8462608Swnj } 8472608Swnj tmstart(um); 8482608Swnj } 8492608Swnj } 8502608Swnj 8511919Swnj /*ARGSUSED*/ 8527632Ssam tmioctl(dev, cmd, data, flag) 8537632Ssam caddr_t data; 8541919Swnj dev_t dev; 8551919Swnj { 8563095Swnj int teunit = TEUNIT(dev); 8573095Swnj register struct te_softc *sc = &te_softc[teunit]; 8583095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8591919Swnj register callcount; 86040911Ssklower int fcount, error = 0; 8617632Ssam struct mtop *mtop; 8627632Ssam struct mtget *mtget; 8631919Swnj /* we depend of the values and order of the MT codes here */ 8642608Swnj static tmops[] = 8652608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8661919Swnj 8672608Swnj switch (cmd) { 8687632Ssam 8697632Ssam case MTIOCTOP: /* tape operation */ 8707632Ssam mtop = (struct mtop *)data; 8718574Sroot switch (mtop->mt_op) { 8727632Ssam 8732608Swnj case MTWEOF: 8747632Ssam callcount = mtop->mt_count; 8752608Swnj fcount = 1; 8762608Swnj break; 8777632Ssam 8782608Swnj case MTFSF: case MTBSF: 8797632Ssam callcount = mtop->mt_count; 8801919Swnj fcount = INF; 8811919Swnj break; 8827632Ssam 8831919Swnj case MTFSR: case MTBSR: 8841919Swnj callcount = 1; 8857632Ssam fcount = mtop->mt_count; 8861919Swnj break; 8877632Ssam 8882324Skre case MTREW: case MTOFFL: case MTNOP: 8891919Swnj callcount = 1; 8901919Swnj fcount = 1; 8911919Swnj break; 8927632Ssam 8931919Swnj default: 8948574Sroot return (ENXIO); 8951919Swnj } 8968574Sroot if (callcount <= 0 || fcount <= 0) 8978574Sroot return (EINVAL); 8982608Swnj while (--callcount >= 0) { 8997632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 9007632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 9018574Sroot bp->b_resid) 9028574Sroot return (EIO); 9033095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9041919Swnj break; 9051919Swnj } 90640911Ssklower if (bp->b_flags&B_ERROR) 90740911Ssklower if ((error = bp->b_error)==0) 90840911Ssklower return (EIO); 90940911Ssklower return (error); 9107632Ssam 9111919Swnj case MTIOCGET: 9127632Ssam mtget = (struct mtget *)data; 9137632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9147632Ssam mtget->mt_erreg = sc->sc_erreg; 9157632Ssam mtget->mt_resid = sc->sc_resid; 9167632Ssam mtget->mt_type = MT_ISTM; 9178574Sroot break; 9187632Ssam 9191919Swnj default: 9208574Sroot return (ENXIO); 9211919Swnj } 9228574Sroot return (0); 9231919Swnj } 9241919Swnj 9251919Swnj #define DBSIZE 20 9261919Swnj 9272363Swnj tmdump() 9282363Swnj { 9292982Swnj register struct uba_device *ui; 9302396Swnj register struct uba_regs *up; 9315692Sroot register struct tmdevice *addr; 9322426Skre int blk, num; 9332426Skre int start; 9341919Swnj 9352426Skre start = 0; 9362426Skre num = maxfree; 9372426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9383095Swnj if (tedinfo[0] == 0) 9392887Swnj return (ENXIO); 9403095Swnj ui = phys(tedinfo[0], struct uba_device *); 9412396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9423331Swnj ubainit(up); 9432324Skre DELAY(1000000); 9445692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9452396Swnj tmwait(addr); 9462608Swnj addr->tmcs = TM_DCLR | TM_GO; 9471919Swnj while (num > 0) { 9481919Swnj blk = num > DBSIZE ? DBSIZE : num; 9492396Swnj tmdwrite(start, blk, addr, up); 9501919Swnj start += blk; 9511919Swnj num -= blk; 9521919Swnj } 9532426Skre tmeof(addr); 9542426Skre tmeof(addr); 9552426Skre tmwait(addr); 9562887Swnj if (addr->tmcs&TM_ERR) 9572887Swnj return (EIO); 9582608Swnj addr->tmcs = TM_REW | TM_GO; 9592471Swnj tmwait(addr); 9602363Swnj return (0); 9611919Swnj } 9621919Swnj 9632608Swnj tmdwrite(dbuf, num, addr, up) 9642608Swnj register dbuf, num; 9655692Sroot register struct tmdevice *addr; 9662396Swnj struct uba_regs *up; 9671919Swnj { 9682396Swnj register struct pte *io; 9692396Swnj register int npf; 9701928Swnj 9712396Swnj tmwait(addr); 9722396Swnj io = up->uba_map; 9731919Swnj npf = num+1; 9741928Swnj while (--npf != 0) 9752982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9762396Swnj *(int *)io = 0; 9772396Swnj addr->tmbc = -(num*NBPG); 9782396Swnj addr->tmba = 0; 9792608Swnj addr->tmcs = TM_WCOM | TM_GO; 9801919Swnj } 9811919Swnj 9822396Swnj tmwait(addr) 9835692Sroot register struct tmdevice *addr; 9841919Swnj { 9851928Swnj register s; 9861919Swnj 9871919Swnj do 9882396Swnj s = addr->tmcs; 9892608Swnj while ((s & TM_CUR) == 0); 9901919Swnj } 9911919Swnj 9922396Swnj tmeof(addr) 9935692Sroot struct tmdevice *addr; 9941919Swnj { 9951919Swnj 9962396Swnj tmwait(addr); 9972608Swnj addr->tmcs = TM_WEOF | TM_GO; 9981919Swnj } 9991919Swnj #endif 1000