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*37511Smckusick * @(#)tm.c 7.5 (Berkeley) 04/25/89 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 38*37511Smckusick #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 /* 45234217Sbostic * For raw I/O, save the current block 45334217Sbostic * number in case we have to 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 } 4603095Swnj } 46134217Sbostic else { 4623095Swnj /* 46334217Sbostic * Handle boundary cases for operation 46434217Sbostic * on non-raw tapes. 4653095Swnj */ 46634217Sbostic if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 46734217Sbostic /* 46834217Sbostic * Can't read past known end-of-file. 46934217Sbostic */ 47034217Sbostic bp->b_flags |= B_ERROR; 47134217Sbostic bp->b_error = ENXIO; 47234217Sbostic goto next; 47334217Sbostic } 47434217Sbostic if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 47534217Sbostic bp->b_flags&B_READ) { 47634217Sbostic /* 47734217Sbostic * Reading at end of file returns 0 bytes. 47834217Sbostic */ 47934217Sbostic bp->b_resid = bp->b_bcount; 48034217Sbostic clrbuf(bp); 48134217Sbostic goto next; 48234217Sbostic } 48334217Sbostic if ((bp->b_flags&B_READ) == 0) 48434217Sbostic /* 48534217Sbostic * Writing sets EOF 48634217Sbostic */ 48734217Sbostic sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4883095Swnj } 4893095Swnj /* 4902608Swnj * If the data transfer command is in the correct place, 4912608Swnj * set up all the registers except the csr, and give 4922608Swnj * control over to the UNIBUS adapter routines, to 4932608Swnj * wait for resources to start the i/o. 4942608Swnj */ 4957381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4962396Swnj addr->tmbc = -bp->b_bcount; 4971919Swnj if ((bp->b_flags&B_READ) == 0) { 49830917Skarels if (um->um_tab.b_errcnt && 49933477Skarels (sc->sc_ioerreg&(TMER_HARD|TMER_SOFT)) != TMER_BGL) 5003095Swnj cmd = TM_WIRG; 5011919Swnj else 5023095Swnj cmd = TM_WCOM; 5031919Swnj } else 5043095Swnj cmd = TM_RCOM; 5052471Swnj um->um_tab.b_active = SIO; 5063095Swnj um->um_cmd = sc->sc_dens|cmd; 5072928Swnj #ifdef notdef 5082670Swnj if (tmreverseop(sc->sc_lastcmd)) 5093095Swnj while (addr->tmer & TMER_SDWN) 51024974Smckusick DELAY(10),tmgapsdcnt++; 5112670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 5122928Swnj #endif 5133495Sroot sc->sc_timo = 60; /* premature, but should serve */ 5143105Swnj (void) ubago(ui); 5151919Swnj return; 5161919Swnj } 5172608Swnj /* 5183095Swnj * Tape positioned incorrectly; 5193095Swnj * set to seek forwards or backwards to the correct spot. 5203095Swnj * This happens for raw tapes only on error retries. 5212608Swnj */ 5222471Swnj um->um_tab.b_active = SSEEK; 5237381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 5242670Swnj bp->b_command = TM_SFORW; 5257381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 5261919Swnj } else { 5272670Swnj bp->b_command = TM_SREV; 5287381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 5291919Swnj } 5303629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 5312670Swnj dobpcmd: 5322928Swnj #ifdef notdef 5333095Swnj /* 5343095Swnj * It is strictly necessary to wait for the tape 5353095Swnj * to stop before changing directions, but the TC11 5363095Swnj * handles this for us. 5373095Swnj */ 5382670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5392670Swnj while (addr->tmer & TM_SDWN) 54024974Smckusick DELAY(10),tmgapsdcnt++; 5412670Swnj sc->sc_lastcmd = bp->b_command; 5422928Swnj #endif 5433095Swnj /* 5443095Swnj * Do the command in bp. 5453095Swnj */ 5463095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5471919Swnj return; 5481919Swnj 5491919Swnj next: 5502608Swnj /* 5512608Swnj * Done with this operation due to error or 5522608Swnj * the fact that it doesn't do anything. 5532608Swnj * Release UBA resources (if any), dequeue 5542608Swnj * the transfer and continue processing this slave. 5552608Swnj */ 5562608Swnj if (um->um_ubinfo) 5572617Swnj ubadone(um); 5582608Swnj um->um_tab.b_errcnt = 0; 5592608Swnj dp->b_actf = bp->av_forw; 5601919Swnj iodone(bp); 5611919Swnj goto loop; 5621919Swnj } 5631919Swnj 5642608Swnj /* 5652608Swnj * The UNIBUS resources we needed have been 5662608Swnj * allocated to us; start the device. 5672608Swnj */ 5682574Swnj tmdgo(um) 5692982Swnj register struct uba_ctlr *um; 5701919Swnj { 5715692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5722471Swnj 5732574Swnj addr->tmba = um->um_ubinfo; 5742574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5752396Swnj } 5762396Swnj 5772608Swnj /* 5782608Swnj * Tm interrupt routine. 5792608Swnj */ 5802471Swnj /*ARGSUSED*/ 5812630Swnj tmintr(tm11) 5822630Swnj int tm11; 5832396Swnj { 5842608Swnj struct buf *dp; 5851919Swnj register struct buf *bp; 5862982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5875692Sroot register struct tmdevice *addr; 5883095Swnj register struct te_softc *sc; 5893095Swnj int teunit; 5901919Swnj register state; 5911919Swnj 5923095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5933095Swnj return; 5943095Swnj bp = dp->b_actf; 5953095Swnj teunit = TEUNIT(bp->b_dev); 5965692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5973524Swnj sc = &te_softc[teunit]; 5982608Swnj /* 5992608Swnj * If last command was a rewind, and tape is still 6002608Swnj * rewinding, wait for the rewind complete interrupt. 6012608Swnj */ 6022608Swnj if (um->um_tab.b_active == SREW) { 6032608Swnj um->um_tab.b_active = SCOM; 6043524Swnj if (addr->tmer&TMER_RWS) { 6053524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 6062608Swnj return; 6073524Swnj } 6081919Swnj } 6092608Swnj /* 6102608Swnj * An operation completed... record status 6112608Swnj */ 6123495Sroot sc->sc_timo = INF; 61333477Skarels if (um->um_tab.b_active = SIO) 61433477Skarels sc->sc_ioerreg = addr->tmer; 6152471Swnj sc->sc_dsreg = addr->tmcs; 6162471Swnj sc->sc_erreg = addr->tmer; 6172471Swnj sc->sc_resid = addr->tmbc; 6181919Swnj if ((bp->b_flags & B_READ) == 0) 6192608Swnj sc->sc_lastiow = 1; 6202471Swnj state = um->um_tab.b_active; 6212471Swnj um->um_tab.b_active = 0; 6222608Swnj /* 6232608Swnj * Check for errors. 6242608Swnj */ 6252608Swnj if (addr->tmcs&TM_ERR) { 6263095Swnj while (addr->tmer & TMER_SDWN) 62724974Smckusick DELAY(10); /* await settle down */ 6282608Swnj /* 6293095Swnj * If we hit the end of the tape file, update our position. 6302608Swnj */ 6313095Swnj if (addr->tmer&TMER_EOF) { 6322608Swnj tmseteof(bp); /* set blkno and nxrec */ 6332608Swnj state = SCOM; /* force completion */ 6342608Swnj /* 6352608Swnj * Stuff bc so it will be unstuffed correctly 6362608Swnj * later to get resid. 6372608Swnj */ 6382396Swnj addr->tmbc = -bp->b_bcount; 6392608Swnj goto opdone; 6401919Swnj } 6412608Swnj /* 6423095Swnj * If we were reading raw tape and the only error was that the 6433095Swnj * record was too long, then we don't consider this an error. 6442608Swnj */ 64534217Sbostic if ((bp->b_flags & (B_READ|B_RAW)) == (B_READ|B_RAW) && 6463095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6472608Swnj goto ignoreerr; 6482608Swnj /* 6492608Swnj * If error is not hard, and this was an i/o operation 6502608Swnj * retry up to 8 times. 6512608Swnj */ 6523095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6532471Swnj if (++um->um_tab.b_errcnt < 7) { 65430917Skarels if (tmdiag) 65530917Skarels log(LOG_DEBUG, 65630917Skarels "te%d: soft error bn%d er=%b\n", 65730917Skarels minor(bp->b_dev)&03, 65830917Skarels bp->b_blkno, sc->sc_erreg, 65930917Skarels TMER_BITS); 6602471Swnj sc->sc_blkno++; 6612617Swnj ubadone(um); 6622608Swnj goto opcont; 6631919Swnj } 6642608Swnj } else 6652608Swnj /* 6662608Swnj * Hard or non-i/o errors on non-raw tape 6672608Swnj * cause it to close. 6682608Swnj */ 66934217Sbostic if ((bp->b_flags&B_RAW) == 0 && sc->sc_openf > 0) 6702608Swnj sc->sc_openf = -1; 6712608Swnj /* 6722608Swnj * Couldn't recover error 6732608Swnj */ 67418321Sralph tprintf(sc->sc_ttyp, 67518321Sralph "te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6763095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 67724974Smckusick #ifdef AVIV 67824974Smckusick if (tmdiag) { 67924974Smckusick addr->tmmr = DAB; 68024974Smckusick printf("reject code 0%o", addr->tmmr & DAB_MASK); 68124974Smckusick addr->tmmr = DTS; 68224974Smckusick if (addr->tmmr & DTS_MASK) 68324974Smckusick printf(", dead track 0%o", addr->tmmr & DTS_MASK); 68424974Smckusick addr->tmmr = RWERR; 68524974Smckusick printf(", read/write errors %b\n", 68624974Smckusick addr->tmmr & RWERR_MASK, 68724974Smckusick RWERR_BITS); 68824974Smckusick addr->tmmr = DRSENSE; 68924974Smckusick printf("drive sense %b, ", addr->tmmr & DRSENSE_MASK, 69024974Smckusick DRSENSE_BITS); 69124974Smckusick printf("fsr %b\n", addr->tmfsr, FSR_BITS); 69224974Smckusick } 69324974Smckusick #endif AVIV 6941919Swnj bp->b_flags |= B_ERROR; 6952608Swnj goto opdone; 6961919Swnj } 6972608Swnj /* 6982608Swnj * Advance tape control FSM. 6992608Swnj */ 7002608Swnj ignoreerr: 7011919Swnj switch (state) { 7021919Swnj 7031919Swnj case SIO: 7042608Swnj /* 7052608Swnj * Read/write increments tape block number 7062608Swnj */ 7072471Swnj sc->sc_blkno++; 70830917Skarels sc->sc_blks++; 70930917Skarels if (um->um_tab.b_errcnt) 71030917Skarels sc->sc_softerrs++; 7112608Swnj goto opdone; 7121919Swnj 7131919Swnj case SCOM: 7142608Swnj /* 7153095Swnj * For forward/backward space record update current position. 7162608Swnj */ 7173095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 71826292Skarels switch ((int)bp->b_command) { 7191919Swnj 7202608Swnj case TM_SFORW: 7212608Swnj sc->sc_blkno -= bp->b_repcnt; 7223095Swnj break; 7231919Swnj 7242608Swnj case TM_SREV: 7252608Swnj sc->sc_blkno += bp->b_repcnt; 7263095Swnj break; 7271919Swnj } 7283095Swnj goto opdone; 7291919Swnj 7301919Swnj case SSEEK: 7317381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 7322608Swnj goto opcont; 7331919Swnj 7341919Swnj default: 7352608Swnj panic("tmintr"); 7362608Swnj } 7372608Swnj opdone: 7382608Swnj /* 7392608Swnj * Reset error count and remove 7402608Swnj * from device queue. 7412608Swnj */ 7422608Swnj um->um_tab.b_errcnt = 0; 7432608Swnj dp->b_actf = bp->av_forw; 74414929Skarels /* 74514929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 74614929Skarels */ 74715081Skarels bp->b_resid = ((int) -addr->tmbc) & 0xffff; 7482617Swnj ubadone(um); 7492608Swnj iodone(bp); 7502608Swnj /* 7512608Swnj * Circulate slave to end of controller 7522608Swnj * queue to give other slaves a chance. 7532608Swnj */ 7542608Swnj um->um_tab.b_actf = dp->b_forw; 7552608Swnj if (dp->b_actf) { 7562608Swnj dp->b_forw = NULL; 7572608Swnj if (um->um_tab.b_actf == NULL) 7582608Swnj um->um_tab.b_actf = dp; 7592608Swnj else 7602608Swnj um->um_tab.b_actl->b_forw = dp; 7612608Swnj um->um_tab.b_actl = dp; 7622608Swnj } 7632608Swnj if (um->um_tab.b_actf == 0) 7641919Swnj return; 7652608Swnj opcont: 7662608Swnj tmstart(um); 7671919Swnj } 7681919Swnj 7693495Sroot tmtimer(dev) 7703495Sroot int dev; 7713495Sroot { 7723495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7734847Sroot register short x; 7743495Sroot 7753495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7764278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7773495Sroot sc->sc_timo = INF; 7784847Sroot x = spl5(); 7793495Sroot tmintr(TMUNIT(dev)); 7804847Sroot (void) splx(x); 7813495Sroot } 7823629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7833495Sroot } 7843495Sroot 7851919Swnj tmseteof(bp) 7861919Swnj register struct buf *bp; 7871919Swnj { 7883095Swnj register int teunit = TEUNIT(bp->b_dev); 7895692Sroot register struct tmdevice *addr = 7905692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7913095Swnj register struct te_softc *sc = &te_softc[teunit]; 7921919Swnj 7933095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7947381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7951919Swnj /* reversing */ 7967381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 7972471Swnj sc->sc_blkno = sc->sc_nxrec; 7981919Swnj } else { 7991919Swnj /* spacing forward */ 8007381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 8012471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 8021919Swnj } 8031919Swnj return; 8041919Swnj } 8051919Swnj /* eof on read */ 8067381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 8071919Swnj } 8081919Swnj 8092608Swnj tmreset(uban) 8102608Swnj int uban; 8112608Swnj { 8122982Swnj register struct uba_ctlr *um; 8133095Swnj register tm11, teunit; 8142982Swnj register struct uba_device *ui; 8152608Swnj register struct buf *dp; 8162608Swnj 8172630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8182630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8192608Swnj um->um_ubanum != uban) 8202608Swnj continue; 8212928Swnj printf(" tm%d", tm11); 8222608Swnj um->um_tab.b_active = 0; 8232608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8242608Swnj if (um->um_ubinfo) { 8252608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8269355Ssam um->um_ubinfo = 0; 8272608Swnj } 8285692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8293095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8303095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8313095Swnj ui->ui_alive == 0) 8322608Swnj continue; 8333095Swnj dp = &teutab[teunit]; 8342608Swnj dp->b_active = 0; 8352608Swnj dp->b_forw = 0; 8362608Swnj if (um->um_tab.b_actf == NULL) 8372608Swnj um->um_tab.b_actf = dp; 8382608Swnj else 8392608Swnj um->um_tab.b_actl->b_forw = dp; 8402608Swnj um->um_tab.b_actl = dp; 8413495Sroot if (te_softc[teunit].sc_openf > 0) 8423495Sroot te_softc[teunit].sc_openf = -1; 8432608Swnj } 8442608Swnj tmstart(um); 8452608Swnj } 8462608Swnj } 8472608Swnj 8481919Swnj /*ARGSUSED*/ 8497632Ssam tmioctl(dev, cmd, data, flag) 8507632Ssam caddr_t data; 8511919Swnj dev_t dev; 8521919Swnj { 8533095Swnj int teunit = TEUNIT(dev); 8543095Swnj register struct te_softc *sc = &te_softc[teunit]; 8553095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8561919Swnj register callcount; 8571919Swnj int fcount; 8587632Ssam struct mtop *mtop; 8597632Ssam struct mtget *mtget; 8601919Swnj /* we depend of the values and order of the MT codes here */ 8612608Swnj static tmops[] = 8622608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8631919Swnj 8642608Swnj switch (cmd) { 8657632Ssam 8667632Ssam case MTIOCTOP: /* tape operation */ 8677632Ssam mtop = (struct mtop *)data; 8688574Sroot switch (mtop->mt_op) { 8697632Ssam 8702608Swnj case MTWEOF: 8717632Ssam callcount = mtop->mt_count; 8722608Swnj fcount = 1; 8732608Swnj break; 8747632Ssam 8752608Swnj case MTFSF: case MTBSF: 8767632Ssam callcount = mtop->mt_count; 8771919Swnj fcount = INF; 8781919Swnj break; 8797632Ssam 8801919Swnj case MTFSR: case MTBSR: 8811919Swnj callcount = 1; 8827632Ssam fcount = mtop->mt_count; 8831919Swnj break; 8847632Ssam 8852324Skre case MTREW: case MTOFFL: case MTNOP: 8861919Swnj callcount = 1; 8871919Swnj fcount = 1; 8881919Swnj break; 8897632Ssam 8901919Swnj default: 8918574Sroot return (ENXIO); 8921919Swnj } 8938574Sroot if (callcount <= 0 || fcount <= 0) 8948574Sroot return (EINVAL); 8952608Swnj while (--callcount >= 0) { 8967632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 8977632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 8988574Sroot bp->b_resid) 8998574Sroot return (EIO); 9003095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 9011919Swnj break; 9021919Swnj } 9038648Sroot return (geterror(bp)); 9047632Ssam 9051919Swnj case MTIOCGET: 9067632Ssam mtget = (struct mtget *)data; 9077632Ssam mtget->mt_dsreg = sc->sc_dsreg; 9087632Ssam mtget->mt_erreg = sc->sc_erreg; 9097632Ssam mtget->mt_resid = sc->sc_resid; 9107632Ssam mtget->mt_type = MT_ISTM; 9118574Sroot break; 9127632Ssam 9131919Swnj default: 9148574Sroot return (ENXIO); 9151919Swnj } 9168574Sroot return (0); 9171919Swnj } 9181919Swnj 9191919Swnj #define DBSIZE 20 9201919Swnj 9212363Swnj tmdump() 9222363Swnj { 9232982Swnj register struct uba_device *ui; 9242396Swnj register struct uba_regs *up; 9255692Sroot register struct tmdevice *addr; 9262426Skre int blk, num; 9272426Skre int start; 9281919Swnj 9292426Skre start = 0; 9302426Skre num = maxfree; 9312426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9323095Swnj if (tedinfo[0] == 0) 9332887Swnj return (ENXIO); 9343095Swnj ui = phys(tedinfo[0], struct uba_device *); 9352396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9363331Swnj ubainit(up); 9372324Skre DELAY(1000000); 9385692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9392396Swnj tmwait(addr); 9402608Swnj addr->tmcs = TM_DCLR | TM_GO; 9411919Swnj while (num > 0) { 9421919Swnj blk = num > DBSIZE ? DBSIZE : num; 9432396Swnj tmdwrite(start, blk, addr, up); 9441919Swnj start += blk; 9451919Swnj num -= blk; 9461919Swnj } 9472426Skre tmeof(addr); 9482426Skre tmeof(addr); 9492426Skre tmwait(addr); 9502887Swnj if (addr->tmcs&TM_ERR) 9512887Swnj return (EIO); 9522608Swnj addr->tmcs = TM_REW | TM_GO; 9532471Swnj tmwait(addr); 9542363Swnj return (0); 9551919Swnj } 9561919Swnj 9572608Swnj tmdwrite(dbuf, num, addr, up) 9582608Swnj register dbuf, num; 9595692Sroot register struct tmdevice *addr; 9602396Swnj struct uba_regs *up; 9611919Swnj { 9622396Swnj register struct pte *io; 9632396Swnj register int npf; 9641928Swnj 9652396Swnj tmwait(addr); 9662396Swnj io = up->uba_map; 9671919Swnj npf = num+1; 9681928Swnj while (--npf != 0) 9692982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9702396Swnj *(int *)io = 0; 9712396Swnj addr->tmbc = -(num*NBPG); 9722396Swnj addr->tmba = 0; 9732608Swnj addr->tmcs = TM_WCOM | TM_GO; 9741919Swnj } 9751919Swnj 9762396Swnj tmwait(addr) 9775692Sroot register struct tmdevice *addr; 9781919Swnj { 9791928Swnj register s; 9801919Swnj 9811919Swnj do 9822396Swnj s = addr->tmcs; 9832608Swnj while ((s & TM_CUR) == 0); 9841919Swnj } 9851919Swnj 9862396Swnj tmeof(addr) 9875692Sroot struct tmdevice *addr; 9881919Swnj { 9891919Swnj 9902396Swnj tmwait(addr); 9912608Swnj addr->tmcs = TM_WEOF | TM_GO; 9921919Swnj } 9931919Swnj #endif 994