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*39938Skarels * @(#)tm.c 7.6 (Berkeley) 01/17/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" 371919Swnj 3837511Smckusick #include "machine/pte.h" 398479Sroot #include "../vax/cpu.h" 4017079Sbloom #include "ubareg.h" 4117079Sbloom #include "ubavar.h" 4217079Sbloom #include "tmreg.h" 431919Swnj 443095Swnj /* 453095Swnj * There is a ctmbuf per tape controller. 463095Swnj * It is used as the token to pass to the internal routines 473095Swnj * to execute tape ioctls, and also acts as a lock on the slaves 483095Swnj * on the controller, since there is only one per controller. 493095Swnj * In particular, when the tape is rewinding on close we release 503095Swnj * the user process but any further attempts to use the tape drive 513095Swnj * before the rewind completes will hang waiting for ctmbuf. 523095Swnj */ 533095Swnj struct buf ctmbuf[NTM]; 541919Swnj 553095Swnj /* 563095Swnj * Driver unibus interface routines and variables. 573095Swnj */ 582608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 592982Swnj struct uba_ctlr *tmminfo[NTM]; 603095Swnj struct uba_device *tedinfo[NTE]; 613095Swnj struct buf teutab[NTE]; 623095Swnj short tetotm[NTE]; 632458Swnj u_short tmstd[] = { 0772520, 0 }; 642396Swnj struct uba_driver tmdriver = 653095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 661919Swnj 671919Swnj /* bits in minor device */ 683095Swnj #define TEUNIT(dev) (minor(dev)&03) 693095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 701919Swnj #define T_NOREWIND 04 7124974Smckusick #define T_1600BPI 0x8 721919Swnj 731919Swnj #define INF (daddr_t)1000000L 741919Swnj 752608Swnj /* 762608Swnj * Software state per tape transport. 773095Swnj * 783095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 793095Swnj * 2. We keep track of the current position on a block tape and seek 803095Swnj * before operations by forward/back spacing if necessary. 813095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 823095Swnj * is open read write and the last thing done is a write we can 833095Swnj * write a standard end of tape mark (two eofs). 843095Swnj * 4. We remember the status registers after the last command, using 853095Swnj * then internally and returning them to the SENSE ioctl. 863095Swnj * 5. We remember the last density the tape was used at. If it is 873095Swnj * not a BOT when we start using it and we are writing, we don't 883095Swnj * let the density be changed. 892608Swnj */ 903095Swnj struct te_softc { 912608Swnj char sc_openf; /* lock against multiple opens */ 922608Swnj char sc_lastiow; /* last op was a write */ 932608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 943095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 952608Swnj u_short sc_erreg; /* copy of last erreg */ 9633477Skarels u_short sc_ioerreg; /* copy of last erreg for I/O command */ 972608Swnj u_short sc_dsreg; /* copy of last dsreg */ 982608Swnj short sc_resid; /* copy of last bc */ 993105Swnj #ifdef unneeded 1002670Swnj short sc_lastcmd; /* last command to handle direction changes */ 1012928Swnj #endif 1023095Swnj u_short sc_dens; /* prototype command with density info */ 10318321Sralph short sc_tact; /* timeout is active */ 1043495Sroot daddr_t sc_timo; /* time until timeout expires */ 10530917Skarels int sc_blks; /* number of I/O operations since open */ 10630917Skarels int sc_softerrs; /* number of soft I/O errors since open */ 10718321Sralph struct tty *sc_ttyp; /* record user's tty for errors */ 1086952Swnj } te_softc[NTE]; 1093105Swnj #ifdef unneeded 1103105Swnj int tmgapsdcnt; /* DEBUG */ 1113105Swnj #endif 1121919Swnj 1132608Swnj /* 1143095Swnj * States for um->um_tab.b_active, the per controller state flag. 1153095Swnj * This is used to sequence control in the driver. 1162608Swnj */ 1171919Swnj #define SSEEK 1 /* seeking */ 1181919Swnj #define SIO 2 /* doing seq i/o */ 1191919Swnj #define SCOM 3 /* sending control command */ 1202608Swnj #define SREW 4 /* sending a drive rewind */ 1211919Swnj 1222426Skre /* 1232426Skre * Determine if there is a controller for 1242426Skre * a tm at address reg. Our goal is to make the 1252426Skre * device interrupt. 1262426Skre */ 1272608Swnj tmprobe(reg) 1282396Swnj caddr_t reg; 1292396Swnj { 1303095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1312426Skre 1322608Swnj #ifdef lint 1333105Swnj br = 0; cvec = br; br = cvec; 1344936Swnj tmintr(0); 1352608Swnj #endif 1365692Sroot ((struct tmdevice *)reg)->tmcs = TM_IE; 1372396Swnj /* 1382630Swnj * If this is a tm11, it ought to have interrupted 1392396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1402458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1412458Swnj * Just in case, we will reference one 1422396Swnj * of the more distant registers, and hope for a machine 1432458Swnj * check, or similar disaster if this is a ts. 1442471Swnj * 1452471Swnj * Note: on an 11/780, badaddr will just generate 1462471Swnj * a uba error for a ts; but our caller will notice that 1472471Swnj * so we won't check for it. 1482396Swnj */ 1495692Sroot if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2)) 1502458Swnj return (0); 1517404Skre return (sizeof (struct tmdevice)); 1522396Swnj } 1532396Swnj 1542608Swnj /* 1552608Swnj * Due to a design flaw, we cannot ascertain if the tape 1562608Swnj * exists or not unless it is on line - ie: unless a tape is 1572608Swnj * mounted. This is too servere a restriction to bear, 1582608Swnj * so all units are assumed to exist. 1592608Swnj */ 1602608Swnj /*ARGSUSED*/ 1612574Swnj tmslave(ui, reg) 1622982Swnj struct uba_device *ui; 1632396Swnj caddr_t reg; 1642396Swnj { 1652458Swnj 1662458Swnj return (1); 1672396Swnj } 1682396Swnj 1692608Swnj /* 1703095Swnj * Record attachment of the unit to the controller. 1712608Swnj */ 1722608Swnj /*ARGSUSED*/ 1732608Swnj tmattach(ui) 1742982Swnj struct uba_device *ui; 1752608Swnj { 1763095Swnj /* 17734217Sbostic * Tetotm is used in TMUNIT to index the ctmbuf 17834217Sbostic * array given a te unit number. 1793095Swnj */ 1803095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1812608Swnj } 1822608Swnj 1833495Sroot int tmtimer(); 1842608Swnj /* 1852608Swnj * Open the device. Tapes are unique open 1862608Swnj * devices, so we refuse if it is already open. 1872608Swnj * We also check that a tape is available, and 1883095Swnj * don't block waiting here; if you want to wait 1893095Swnj * for a tape you should timeout in user code. 1902608Swnj */ 19124974Smckusick 19224974Smckusick #ifdef AVIV 19324974Smckusick int tmdens[4] = { 0x6000, 0x0000, 0x2000, 0 }; 19430917Skarels #endif AVIV 19524974Smckusick int tmdiag; 19624974Smckusick 1971919Swnj tmopen(dev, flag) 1981919Swnj dev_t dev; 1991919Swnj int flag; 2001919Swnj { 2013095Swnj register int teunit; 2022982Swnj register struct uba_device *ui; 2033095Swnj register struct te_softc *sc; 2043209Swnj int olddens, dens; 2055437Sroot int s; 2061919Swnj 2073095Swnj teunit = TEUNIT(dev); 20825051Skarels if (teunit>=NTE || (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2098574Sroot return (ENXIO); 21025051Skarels if ((sc = &te_softc[teunit])->sc_openf) 21125051Skarels return (EBUSY); 21230917Skarels sc->sc_openf = 1; 2133209Swnj olddens = sc->sc_dens; 2143209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 21524974Smckusick #ifndef AVIV 2163209Swnj if ((minor(dev) & T_1600BPI) == 0) 2173209Swnj dens |= TM_D800; 21824974Smckusick #else AVIV 21924974Smckusick dens |= tmdens[(minor(dev)>>3)&03]; 22024974Smckusick #endif AVIV 2213209Swnj sc->sc_dens = dens; 2223141Swnj get: 2232608Swnj tmcommand(dev, TM_SENSE, 1); 2243141Swnj if (sc->sc_erreg&TMER_SDWN) { 2253141Swnj sleep((caddr_t)&lbolt, PZERO+1); 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; 25118321Sralph sc->sc_ttyp = u.u_ttyp; 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); 2922471Swnj sc->sc_openf = 0; 29330917Skarels return (0); 2941919Swnj } 2951919Swnj 2962608Swnj /* 2972608Swnj * Execute a command on the tape drive 2982608Swnj * a specified number of times. 2992608Swnj */ 3002574Swnj tmcommand(dev, com, count) 3011919Swnj dev_t dev; 3021919Swnj int com, count; 3031919Swnj { 3041919Swnj register struct buf *bp; 3055437Sroot register int s; 3061919Swnj 3072608Swnj bp = &ctmbuf[TMUNIT(dev)]; 3085437Sroot s = spl5(); 3091919Swnj while (bp->b_flags&B_BUSY) { 3103095Swnj /* 3113095Swnj * This special check is because B_BUSY never 3123095Swnj * gets cleared in the non-waiting rewind case. 3133095Swnj */ 3143141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 3153095Swnj break; 3161919Swnj bp->b_flags |= B_WANTED; 3171919Swnj sleep((caddr_t)bp, PRIBIO); 3181919Swnj } 3191919Swnj bp->b_flags = B_BUSY|B_READ; 3205437Sroot splx(s); 3211919Swnj bp->b_dev = dev; 3221919Swnj bp->b_repcnt = -count; 3231919Swnj bp->b_command = com; 3241919Swnj bp->b_blkno = 0; 3251919Swnj tmstrategy(bp); 3263095Swnj /* 3273095Swnj * In case of rewind from close, don't wait. 3283095Swnj * This is the only case where count can be 0. 3293095Swnj */ 3303095Swnj if (count == 0) 3313095Swnj return; 3321919Swnj iowait(bp); 3331919Swnj if (bp->b_flags&B_WANTED) 3341919Swnj wakeup((caddr_t)bp); 3351919Swnj bp->b_flags &= B_ERROR; 3361919Swnj } 3371919Swnj 3382608Swnj /* 3393095Swnj * Queue a tape operation. 3402608Swnj */ 3411919Swnj tmstrategy(bp) 3421919Swnj register struct buf *bp; 3431919Swnj { 3443095Swnj int teunit = TEUNIT(bp->b_dev); 3452982Swnj register struct uba_ctlr *um; 3462608Swnj register struct buf *dp; 3475437Sroot int s; 3481919Swnj 3492608Swnj /* 3502608Swnj * Put transfer at end of unit queue 3512608Swnj */ 3523095Swnj dp = &teutab[teunit]; 3531919Swnj bp->av_forw = NULL; 3545437Sroot s = spl5(); 3553939Sbugs um = tedinfo[teunit]->ui_mi; 3562608Swnj if (dp->b_actf == NULL) { 3572608Swnj dp->b_actf = bp; 3582608Swnj /* 3592608Swnj * Transport not already active... 3602608Swnj * put at end of controller queue. 3612608Swnj */ 3622608Swnj dp->b_forw = NULL; 3632608Swnj if (um->um_tab.b_actf == NULL) 3642608Swnj um->um_tab.b_actf = dp; 3652608Swnj else 3662608Swnj um->um_tab.b_actl->b_forw = dp; 3672608Swnj um->um_tab.b_actl = dp; 3682608Swnj } else 3692608Swnj dp->b_actl->av_forw = bp; 3702608Swnj dp->b_actl = bp; 3712608Swnj /* 3722608Swnj * If the controller is not busy, get 3732608Swnj * it going. 3742608Swnj */ 3752608Swnj if (um->um_tab.b_active == 0) 3762608Swnj tmstart(um); 3775437Sroot splx(s); 3781919Swnj } 3791919Swnj 3802608Swnj /* 3812608Swnj * Start activity on a tm controller. 3822608Swnj */ 3832608Swnj tmstart(um) 3842982Swnj register struct uba_ctlr *um; 3851919Swnj { 3862608Swnj register struct buf *bp, *dp; 3875692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3883095Swnj register struct te_softc *sc; 3892982Swnj register struct uba_device *ui; 3903095Swnj int teunit, cmd; 3912471Swnj daddr_t blkno; 3921919Swnj 3932608Swnj /* 3942608Swnj * Look for an idle transport on the controller. 3952608Swnj */ 3961919Swnj loop: 3972608Swnj if ((dp = um->um_tab.b_actf) == NULL) 3981919Swnj return; 3992608Swnj if ((bp = dp->b_actf) == NULL) { 4002608Swnj um->um_tab.b_actf = dp->b_forw; 4012608Swnj goto loop; 4022608Swnj } 4033095Swnj teunit = TEUNIT(bp->b_dev); 4043095Swnj ui = tedinfo[teunit]; 4052608Swnj /* 4062608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 4072608Swnj */ 4083095Swnj sc = &te_softc[teunit]; 4095692Sroot addr = (struct tmdevice *)um->um_addr; 4102608Swnj addr->tmcs = (ui->ui_slave << 8); 4112471Swnj sc->sc_dsreg = addr->tmcs; 4122471Swnj sc->sc_erreg = addr->tmer; 4132471Swnj sc->sc_resid = addr->tmbc; 4142608Swnj /* 4152608Swnj * Default is that last command was NOT a write command; 4162608Swnj * if we do a write command we will notice this in tmintr(). 4172608Swnj */ 4183493Sroot sc->sc_lastiow = 0; 4192608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4202608Swnj /* 4213095Swnj * Have had a hard error on a non-raw tape 4223095Swnj * or the tape unit is now unavailable 4233095Swnj * (e.g. taken off line). 4242608Swnj */ 4252608Swnj bp->b_flags |= B_ERROR; 4261919Swnj goto next; 4271919Swnj } 4283095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4293095Swnj /* 4303095Swnj * Execute control operation with the specified count. 4313095Swnj */ 4322608Swnj if (bp->b_command == TM_SENSE) 4332608Swnj goto next; 4343495Sroot /* 4353495Sroot * Set next state; give 5 minutes to complete 4363495Sroot * rewind, or 10 seconds per iteration (minimum 60 4373629Sroot * seconds and max 5 minutes) to complete other ops. 4383495Sroot */ 4393495Sroot if (bp->b_command == TM_REW) { 4403495Sroot um->um_tab.b_active = SREW; 4413495Sroot sc->sc_timo = 5 * 60; 4423495Sroot } else { 4433495Sroot um->um_tab.b_active = SCOM; 4444266Swnj sc->sc_timo = 4454266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4463495Sroot } 4472608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4482608Swnj addr->tmbc = bp->b_repcnt; 4492670Swnj goto dobpcmd; 4502608Swnj } 4512608Swnj /* 452*39938Skarels * For raw I/O, fudge the current block number 453*39938Skarels * so we don't seek except on a retry. 4543095Swnj */ 45534217Sbostic if (bp->b_flags & B_RAW) { 45634217Sbostic if (um->um_tab.b_errcnt == 0) { 45734217Sbostic sc->sc_blkno = bdbtofsb(bp->b_blkno); 45834217Sbostic sc->sc_nxrec = sc->sc_blkno + 1; 45934217Sbostic } 460*39938Skarels } else { 4613095Swnj /* 46234217Sbostic * Handle boundary cases for operation 46334217Sbostic * on non-raw tapes. 4643095Swnj */ 46534217Sbostic if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 46634217Sbostic /* 46734217Sbostic * Can't read past known end-of-file. 46834217Sbostic */ 46934217Sbostic bp->b_flags |= B_ERROR; 47034217Sbostic bp->b_error = ENXIO; 47134217Sbostic goto next; 47234217Sbostic } 47334217Sbostic if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 47434217Sbostic bp->b_flags&B_READ) { 47534217Sbostic /* 47634217Sbostic * Reading at end of file returns 0 bytes. 47734217Sbostic */ 47834217Sbostic bp->b_resid = bp->b_bcount; 47934217Sbostic clrbuf(bp); 48034217Sbostic goto next; 48134217Sbostic } 48234217Sbostic if ((bp->b_flags&B_READ) == 0) 48334217Sbostic /* 48434217Sbostic * Writing sets EOF 48534217Sbostic */ 48634217Sbostic sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4873095Swnj } 4883095Swnj /* 4892608Swnj * If the data transfer command is in the correct place, 4902608Swnj * set up all the registers except the csr, and give 4912608Swnj * control over to the UNIBUS adapter routines, to 4922608Swnj * wait for resources to start the i/o. 4932608Swnj */ 4947381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4952396Swnj addr->tmbc = -bp->b_bcount; 4961919Swnj if ((bp->b_flags&B_READ) == 0) { 49730917Skarels if (um->um_tab.b_errcnt && 49833477Skarels (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL) 4993095Swnj cmd = TM_WIRG; 5001919Swnj else 5013095Swnj cmd = TM_WCOM; 5021919Swnj } else 5033095Swnj cmd = TM_RCOM; 5042471Swnj um->um_tab.b_active = SIO; 5053095Swnj um->um_cmd = sc->sc_dens|cmd; 5062928Swnj #ifdef notdef 5072670Swnj if (tmreverseop(sc->sc_lastcmd)) 5083095Swnj while (addr->tmer & TMER_SDWN) 50924974Smckusick DELAY(10),tmgapsdcnt++; 5102670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 5112928Swnj #endif 5123495Sroot sc->sc_timo = 60; /* premature, but should serve */ 5133105Swnj (void) ubago(ui); 5141919Swnj return; 5151919Swnj } 5162608Swnj /* 5173095Swnj * Tape positioned incorrectly; 5183095Swnj * set to seek forwards or backwards to the correct spot. 5193095Swnj * This happens for raw tapes only on error retries. 5202608Swnj */ 5212471Swnj um->um_tab.b_active = SSEEK; 5227381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5232670Swnj bp->b_command = TM_SFORW; 5247381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5251919Swnj } else { 5262670Swnj bp->b_command = TM_SREV; 5277381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5281919Swnj } 5293629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5302670Swnj dobpcmd: 5312928Swnj #ifdef notdef 5323095Swnj /* 5333095Swnj * It is strictly necessary to wait for the tape 5343095Swnj * to stop before changing directions, but the TC11 5353095Swnj * handles this for us. 5363095Swnj */ 5372670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5382670Swnj while (addr->tmer & TM_SDWN) 53924974Smckusick DELAY(10),tmgapsdcnt++; 5402670Swnj sc->sc_lastcmd = bp->b_command; 5412928Swnj #endif 5423095Swnj /* 5433095Swnj * Do the command in bp. 5443095Swnj */ 5453095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5461919Swnj return; 5471919Swnj 5481919Swnj next: 5492608Swnj /* 5502608Swnj * Done with this operation due to error or 5512608Swnj * the fact that it doesn't do anything. 5522608Swnj * Release UBA resources (if any), dequeue 5532608Swnj * the transfer and continue processing this slave. 5542608Swnj */ 5552608Swnj if (um->um_ubinfo) 5562617Swnj ubadone(um); 5572608Swnj um->um_tab.b_errcnt = 0; 5582608Swnj dp->b_actf = bp->av_forw; 5591919Swnj iodone(bp); 5601919Swnj goto loop; 5611919Swnj } 5621919Swnj 5632608Swnj /* 5642608Swnj * The UNIBUS resources we needed have been 5652608Swnj * allocated to us; start the device. 5662608Swnj */ 5672574Swnj tmdgo(um) 5682982Swnj register struct uba_ctlr *um; 5691919Swnj { 5705692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5712471Swnj 5722574Swnj addr->tmba = um->um_ubinfo; 5732574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5742396Swnj } 5752396Swnj 5762608Swnj /* 5772608Swnj * Tm interrupt routine. 5782608Swnj */ 5792471Swnj /*ARGSUSED*/ 5802630Swnj tmintr(tm11) 5812630Swnj int tm11; 5822396Swnj { 5832608Swnj struct buf *dp; 5841919Swnj register struct buf *bp; 5852982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5865692Sroot register struct tmdevice *addr; 5873095Swnj register struct te_softc *sc; 5883095Swnj int teunit; 5891919Swnj register state; 5901919Swnj 5913095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5923095Swnj return; 5933095Swnj bp = dp->b_actf; 5943095Swnj teunit = TEUNIT(bp->b_dev); 5955692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5963524Swnj sc = &te_softc[teunit]; 5972608Swnj /* 5982608Swnj * If last command was a rewind, and tape is still 5992608Swnj * rewinding, wait for the rewind complete interrupt. 6002608Swnj */ 6012608Swnj if (um->um_tab.b_active == SREW) { 6022608Swnj um->um_tab.b_active = SCOM; 6033524Swnj if (addr->tmer&TMER_RWS) { 6043524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 6052608Swnj return; 6063524Swnj } 6071919Swnj } 6082608Swnj /* 6092608Swnj * An operation completed... record status 6102608Swnj */ 6113495Sroot sc->sc_timo = INF; 612*39938Skarels if (um->um_tab.b_active == SIO) 61333477Skarels sc->sc_ioerreg = addr->tmer; 6142471Swnj sc->sc_dsreg = addr->tmcs; 6152471Swnj sc->sc_erreg = addr->tmer; 6162471Swnj sc->sc_resid = addr->tmbc; 6171919Swnj if ((bp->b_flags & B_READ) == 0) 6182608Swnj sc->sc_lastiow = 1; 6192471Swnj state = um->um_tab.b_active; 6202471Swnj um->um_tab.b_active = 0; 6212608Swnj /* 6222608Swnj * Check for errors. 6232608Swnj */ 6242608Swnj if (addr->tmcs&TM_ERR) { 6253095Swnj while (addr->tmer & TMER_SDWN) 62624974Smckusick DELAY(10); /* await settle down */ 6272608Swnj /* 6283095Swnj * If we hit the end of the tape file, update our position. 6292608Swnj */ 6303095Swnj if (addr->tmer&TMER_EOF) { 6312608Swnj tmseteof(bp); /* set blkno and nxrec */ 6322608Swnj state = SCOM; /* force completion */ 6332608Swnj /* 6342608Swnj * Stuff bc so it will be unstuffed correctly 6352608Swnj * later to get resid. 6362608Swnj */ 6372396Swnj addr->tmbc = -bp->b_bcount; 6382608Swnj goto opdone; 6391919Swnj } 6402608Swnj /* 6413095Swnj * If we were reading raw tape and the only error was that the 6423095Swnj * record was too long, then we don't consider this an error. 6432608Swnj */ 64434217Sbostic if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) && 6453095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6462608Swnj goto ignoreerr; 6472608Swnj /* 6482608Swnj * If error is not hard, and this was an i/o operation 6492608Swnj * retry up to 8 times. 6502608Swnj */ 6513095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6522471Swnj if (++um->um_tab.b_errcnt < 7) { 65330917Skarels if (tmdiag) 65430917Skarels log(LOG_DEBUG, 65530917Skarels "te%d: soft error bn%d er=%b\n", 65630917Skarels minor(bp->b_dev)&03, 65730917Skarels bp->b_blkno, sc->sc_erreg, 65830917Skarels TMER_BITS); 6592471Swnj sc->sc_blkno++; 6602617Swnj ubadone(um); 6612608Swnj goto opcont; 6621919Swnj } 6632608Swnj } else 6642608Swnj /* 6652608Swnj * Hard or non-i/o errors on non-raw tape 6662608Swnj * cause it to close. 6672608Swnj */ 66834217Sbostic if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0) 6692608Swnj sc->sc_openf = -1; 6702608Swnj /* 6712608Swnj * Couldn't recover error 6722608Swnj */ 67318321Sralph tprintf(sc->sc_ttyp, 67418321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6753095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 67624974Smckusick #ifdef AVIV 67724974Smckusick if (tmdiag) { 67824974Smckusick addr->tmmr = DAB; 67924974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 68024974Smckusick addr->tmmr = DTS; 68124974Smckusick if (addr->tmmr & DTS_MASK) 68224974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 68324974Smckusick addr->tmmr = RWERR; 68424974Smckusick printf(", read/write errors %b\n", 68524974Smckusick addr->tmmr & RWERR_MASK, 68624974Smckusick RWERR_BITS); 68724974Smckusick addr->tmmr = DRSENSE; 68824974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 68924974Smckusick DRSENSE_BITS); 69024974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 69124974Smckusick } 69224974Smckusick #endif AVIV 6931919Swnj bp->b_flags |= B_ERROR; 6942608Swnj goto opdone; 6951919Swnj } 6962608Swnj /* 6972608Swnj * Advance tape control FSM. 6982608Swnj */ 6992608Swnj ignoreerr: 7001919Swnj switch (state) { 7011919Swnj 7021919Swnj case SIO: 7032608Swnj /* 7042608Swnj * Read/write increments tape block number 7052608Swnj */ 7062471Swnj sc->sc_blkno++; 70730917Skarels sc->sc_blks++; 70830917Skarels if (um->um_tab.b_errcnt) 70930917Skarels sc->sc_softerrs++; 7102608Swnj goto opdone; 7111919Swnj 7121919Swnj case SCOM: 7132608Swnj /* 7143095Swnj * For forward/backward space record update current position. 7152608Swnj */ 7163095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 71726292Skarels switch ((int)bp->b_command) { 7181919Swnj 7192608Swnj case TM_SFORW: 7202608Swnj sc->sc_blkno -= bp->b_repcnt; 7213095Swnj break; 7221919Swnj 7232608Swnj case TM_SREV: 7242608Swnj sc->sc_blkno += bp->b_repcnt; 7253095Swnj break; 7261919Swnj } 7273095Swnj goto opdone; 7281919Swnj 7291919Swnj case SSEEK: 7307381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7312608Swnj goto opcont; 7321919Swnj 7331919Swnj default: 7342608Swnj panic("tmintr"); 7352608Swnj } 7362608Swnj opdone: 7372608Swnj /* 7382608Swnj * Reset error count and remove 7392608Swnj * from device queue. 7402608Swnj */ 7412608Swnj um->um_tab.b_errcnt = 0; 7422608Swnj dp->b_actf = bp->av_forw; 74314929Skarels /* 74414929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 74514929Skarels */ 74615081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7472617Swnj ubadone(um); 7482608Swnj iodone(bp); 7492608Swnj /* 7502608Swnj * Circulate slave to end of controller 7512608Swnj * queue to give other slaves a chance. 7522608Swnj */ 7532608Swnj um->um_tab.b_actf = dp->b_forw; 7542608Swnj if (dp->b_actf) { 7552608Swnj dp->b_forw = NULL; 7562608Swnj if (um->um_tab.b_actf == NULL) 7572608Swnj um->um_tab.b_actf = dp; 7582608Swnj else 7592608Swnj um->um_tab.b_actl->b_forw = dp; 7602608Swnj um->um_tab.b_actl = dp; 7612608Swnj } 7622608Swnj if (um->um_tab.b_actf == 0) 7631919Swnj return; 7642608Swnj opcont: 7652608Swnj tmstart(um); 7661919Swnj } 7671919Swnj 7683495Sroot tmtimer(dev) 7693495Sroot int dev; 7703495Sroot { 7713495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7724847Sroot register short x; 7733495Sroot 7743495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7754278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7763495Sroot sc->sc_timo = INF; 7774847Sroot x = spl5(); 7783495Sroot tmintr(TMUNIT(dev)); 7794847Sroot (void) splx(x); 7803495Sroot } 7813629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7823495Sroot } 7833495Sroot 7841919Swnj tmseteof(bp) 7851919Swnj register struct buf *bp; 7861919Swnj { 7873095Swnj register int teunit = TEUNIT(bp->b_dev); 7885692Sroot register struct tmdevice *addr = 7895692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7903095Swnj register struct te_softc *sc = &te_softc[teunit]; 7911919Swnj 7923095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7937381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7941919Swnj /* reversing */ 7957381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 7962471Swnj sc->sc_blkno = sc->sc_nxrec; 7971919Swnj } else { 7981919Swnj /* spacing forward */ 7997381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 8002471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 8011919Swnj } 8021919Swnj return; 8031919Swnj } 8041919Swnj /* eof on read */ 8057381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 8061919Swnj } 8071919Swnj 8082608Swnj tmreset(uban) 8092608Swnj int uban; 8102608Swnj { 8112982Swnj register struct uba_ctlr *um; 8123095Swnj register tm11, teunit; 8132982Swnj register struct uba_device *ui; 8142608Swnj register struct buf *dp; 8152608Swnj 8162630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8172630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8182608Swnj um->um_ubanum != uban) 8192608Swnj continue; 8202928Swnj printf(" tm%d", tm11); 8212608Swnj um->um_tab.b_active = 0; 8222608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8232608Swnj if (um->um_ubinfo) { 8242608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8259355Ssam um->um_ubinfo = 0; 8262608Swnj } 8275692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8283095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8293095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8303095Swnj ui->ui_alive == 0) 8312608Swnj continue; 8323095Swnj dp = &teutab[teunit]; 8332608Swnj dp->b_active = 0; 8342608Swnj dp->b_forw = 0; 8352608Swnj if (um->um_tab.b_actf == NULL) 8362608Swnj um->um_tab.b_actf = dp; 8372608Swnj else 8382608Swnj um->um_tab.b_actl->b_forw = dp; 8392608Swnj um->um_tab.b_actl = dp; 8403495Sroot if (te_softc[teunit].sc_openf > 0) 8413495Sroot te_softc[teunit].sc_openf = -1; 8422608Swnj } 8432608Swnj tmstart(um); 8442608Swnj } 8452608Swnj } 8462608Swnj 8471919Swnj /*ARGSUSED*/ 8487632Ssam tmioctl(dev, cmd, data, flag) 8497632Ssam caddr_t data; 8501919Swnj dev_t dev; 8511919Swnj { 8523095Swnj int teunit = TEUNIT(dev); 8533095Swnj register struct te_softc *sc = &te_softc[teunit]; 8543095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8551919Swnj register callcount; 8561919Swnj int fcount; 8577632Ssam struct mtop *mtop; 8587632Ssam struct mtget *mtget; 8591919Swnj /* we depend of the values and order of the MT codes here */ 8602608Swnj static tmops[] = 8612608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8621919Swnj 8632608Swnj switch (cmd) { 8647632Ssam 8657632Ssam case MTIOCTOP: /* tape operation */ 8667632Ssam mtop = (struct mtop *)data; 8678574Sroot switch (mtop->mt_op) { 8687632Ssam 8692608Swnj case MTWEOF: 8707632Ssam callcount = mtop->mt_count; 8712608Swnj fcount = 1; 8722608Swnj break; 8737632Ssam 8742608Swnj case MTFSF: case MTBSF: 8757632Ssam callcount = mtop->mt_count; 8761919Swnj fcount = INF; 8771919Swnj break; 8787632Ssam 8791919Swnj case MTFSR: case MTBSR: 8801919Swnj callcount = 1; 8817632Ssam fcount = mtop->mt_count; 8821919Swnj break; 8837632Ssam 8842324Skre case MTREW: case MTOFFL: case MTNOP: 8851919Swnj callcount = 1; 8861919Swnj fcount = 1; 8871919Swnj break; 8887632Ssam 8891919Swnj default: 8908574Sroot return (ENXIO); 8911919Swnj } 8928574Sroot if (callcount <= 0 || fcount <= 0) 8938574Sroot return (EINVAL); 8942608Swnj while (--callcount >= 0) { 8957632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 8967632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 8978574Sroot bp->b_resid) 8988574Sroot return (EIO); 8993095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9001919Swnj break; 9011919Swnj } 9028648Sroot return (geterror(bp)); 9037632Ssam 9041919Swnj case MTIOCGET: 9057632Ssam mtget = (struct mtget *)data; 9067632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9077632Ssam mtget->mt_erreg = sc->sc_erreg; 9087632Ssam mtget->mt_resid = sc->sc_resid; 9097632Ssam mtget->mt_type = MT_ISTM; 9108574Sroot break; 9117632Ssam 9121919Swnj default: 9138574Sroot return (ENXIO); 9141919Swnj } 9158574Sroot return (0); 9161919Swnj } 9171919Swnj 9181919Swnj #define DBSIZE 20 9191919Swnj 9202363Swnj tmdump() 9212363Swnj { 9222982Swnj register struct uba_device *ui; 9232396Swnj register struct uba_regs *up; 9245692Sroot register struct tmdevice *addr; 9252426Skre int blk, num; 9262426Skre int start; 9271919Swnj 9282426Skre start = 0; 9292426Skre num = maxfree; 9302426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9313095Swnj if (tedinfo[0] == 0) 9322887Swnj return (ENXIO); 9333095Swnj ui = phys(tedinfo[0], struct uba_device *); 9342396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9353331Swnj ubainit(up); 9362324Skre DELAY(1000000); 9375692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9382396Swnj tmwait(addr); 9392608Swnj addr->tmcs = TM_DCLR | TM_GO; 9401919Swnj while (num > 0) { 9411919Swnj blk = num > DBSIZE ? DBSIZE : num; 9422396Swnj tmdwrite(start, blk, addr, up); 9431919Swnj start += blk; 9441919Swnj num -= blk; 9451919Swnj } 9462426Skre tmeof(addr); 9472426Skre tmeof(addr); 9482426Skre tmwait(addr); 9492887Swnj if (addr->tmcs&TM_ERR) 9502887Swnj return (EIO); 9512608Swnj addr->tmcs = TM_REW | TM_GO; 9522471Swnj tmwait(addr); 9532363Swnj return (0); 9541919Swnj } 9551919Swnj 9562608Swnj tmdwrite(dbuf, num, addr, up) 9572608Swnj register dbuf, num; 9585692Sroot register struct tmdevice *addr; 9592396Swnj struct uba_regs *up; 9601919Swnj { 9612396Swnj register struct pte *io; 9622396Swnj register int npf; 9631928Swnj 9642396Swnj tmwait(addr); 9652396Swnj io = up->uba_map; 9661919Swnj npf = num+1; 9671928Swnj while (--npf != 0) 9682982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9692396Swnj *(int *)io = 0; 9702396Swnj addr->tmbc = -(num*NBPG); 9712396Swnj addr->tmba = 0; 9722608Swnj addr->tmcs = TM_WCOM | TM_GO; 9731919Swnj } 9741919Swnj 9752396Swnj tmwait(addr) 9765692Sroot register struct tmdevice *addr; 9771919Swnj { 9781928Swnj register s; 9791919Swnj 9801919Swnj do 9812396Swnj s = addr->tmcs; 9822608Swnj while ((s & TM_CUR) == 0); 9831919Swnj } 9841919Swnj 9852396Swnj tmeof(addr) 9865692Sroot struct tmdevice *addr; 9871919Swnj { 9881919Swnj 9892396Swnj tmwait(addr); 9902608Swnj addr->tmcs = TM_WEOF | TM_GO; 9911919Swnj } 9921919Swnj #endif 993