1*14929Skarels /* tm.c 6.2 83/09/08 */ 21919Swnj 32709Swnj #include "te.h" 43519Sroot #include "ts.h" 56343Swnj #if NTE > 0 61919Swnj /* 72630Swnj * TM11/TE10 tape driver 82471Swnj * 93095Swnj * TODO: 103095Swnj * test driver with more than one slave 113095Swnj * test driver with more than one controller 123095Swnj * test reset code 133095Swnj * what happens if you offline tape during rewind? 143095Swnj * test using file system on tape 151919Swnj */ 169778Ssam #include "../machine/pte.h" 179778Ssam 181919Swnj #include "../h/param.h" 193141Swnj #include "../h/systm.h" 201919Swnj #include "../h/buf.h" 211919Swnj #include "../h/dir.h" 221919Swnj #include "../h/conf.h" 231919Swnj #include "../h/user.h" 241919Swnj #include "../h/file.h" 251919Swnj #include "../h/map.h" 262574Swnj #include "../h/vm.h" 277632Ssam #include "../h/ioctl.h" 281919Swnj #include "../h/mtio.h" 292363Swnj #include "../h/cmap.h" 307732Sroot #include "../h/uio.h" 318610Sroot #include "../h/kernel.h" 321919Swnj 338479Sroot #include "../vax/cpu.h" 348479Sroot #include "../vaxuba/ubareg.h" 358479Sroot #include "../vaxuba/ubavar.h" 368479Sroot #include "../vaxuba/tmreg.h" 371919Swnj 383095Swnj /* 393095Swnj * There is a ctmbuf per tape controller. 403095Swnj * It is used as the token to pass to the internal routines 413095Swnj * to execute tape ioctls, and also acts as a lock on the slaves 423095Swnj * on the controller, since there is only one per controller. 433095Swnj * In particular, when the tape is rewinding on close we release 443095Swnj * the user process but any further attempts to use the tape drive 453095Swnj * before the rewind completes will hang waiting for ctmbuf. 463095Swnj */ 473095Swnj struct buf ctmbuf[NTM]; 481919Swnj 493095Swnj /* 503095Swnj * Raw tape operations use rtmbuf. The driver 513095Swnj * notices when rtmbuf is being used and allows the user 523095Swnj * program to continue after errors and read records 533095Swnj * not of the standard length (BSIZE). 543095Swnj */ 553095Swnj struct buf rtmbuf[NTM]; 563095Swnj 573095Swnj /* 583095Swnj * Driver unibus interface routines and variables. 593095Swnj */ 602608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 612982Swnj struct uba_ctlr *tmminfo[NTM]; 623095Swnj struct uba_device *tedinfo[NTE]; 633095Swnj struct buf teutab[NTE]; 643095Swnj short tetotm[NTE]; 652458Swnj u_short tmstd[] = { 0772520, 0 }; 662396Swnj struct uba_driver tmdriver = 673095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 681919Swnj 691919Swnj /* bits in minor device */ 703095Swnj #define TEUNIT(dev) (minor(dev)&03) 713095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 721919Swnj #define T_NOREWIND 04 731919Swnj #define T_1600BPI 08 741919Swnj 751919Swnj #define INF (daddr_t)1000000L 761919Swnj 772608Swnj /* 782608Swnj * Software state per tape transport. 793095Swnj * 803095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 813095Swnj * 2. We keep track of the current position on a block tape and seek 823095Swnj * before operations by forward/back spacing if necessary. 833095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 843095Swnj * is open read write and the last thing done is a write we can 853095Swnj * write a standard end of tape mark (two eofs). 863095Swnj * 4. We remember the status registers after the last command, using 873095Swnj * then internally and returning them to the SENSE ioctl. 883095Swnj * 5. We remember the last density the tape was used at. If it is 893095Swnj * not a BOT when we start using it and we are writing, we don't 903095Swnj * let the density be changed. 912608Swnj */ 923095Swnj struct te_softc { 932608Swnj char sc_openf; /* lock against multiple opens */ 942608Swnj char sc_lastiow; /* last op was a write */ 952608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 963095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 972608Swnj u_short sc_erreg; /* copy of last erreg */ 982608Swnj u_short sc_dsreg; /* copy of last dsreg */ 992608Swnj short sc_resid; /* copy of last bc */ 1003105Swnj #ifdef unneeded 1012670Swnj short sc_lastcmd; /* last command to handle direction changes */ 1022928Swnj #endif 1033095Swnj u_short sc_dens; /* prototype command with density info */ 1043495Sroot daddr_t sc_timo; /* time until timeout expires */ 1053495Sroot short sc_tact; /* timeout is active */ 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 /* 1753095Swnj * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf 1763095Swnj * arrays 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 */ 1891919Swnj tmopen(dev, flag) 1901919Swnj dev_t dev; 1911919Swnj int flag; 1921919Swnj { 1933095Swnj register int teunit; 1942982Swnj register struct uba_device *ui; 1953095Swnj register struct te_softc *sc; 1963209Swnj int olddens, dens; 1975437Sroot int s; 1981919Swnj 1993095Swnj teunit = TEUNIT(dev); 2003095Swnj if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf || 2018574Sroot (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) 2028574Sroot return (ENXIO); 2033209Swnj olddens = sc->sc_dens; 2043209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 2053209Swnj if ((minor(dev) & T_1600BPI) == 0) 2063209Swnj dens |= TM_D800; 2073209Swnj sc->sc_dens = dens; 2083141Swnj get: 2092608Swnj tmcommand(dev, TM_SENSE, 1); 2103141Swnj if (sc->sc_erreg&TMER_SDWN) { 2113141Swnj sleep((caddr_t)&lbolt, PZERO+1); 2123141Swnj goto get; 2133141Swnj } 2143209Swnj sc->sc_dens = olddens; 2153710Sroot if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) { 2163710Sroot uprintf("te%d: not online\n", teunit); 2178574Sroot return (EIO); 2181919Swnj } 2193710Sroot if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) { 2203710Sroot uprintf("te%d: no write ring\n", teunit); 2218574Sroot return (EIO); 2223710Sroot } 2233710Sroot if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2243710Sroot dens != sc->sc_dens) { 2253710Sroot uprintf("te%d: can't change density in mid-tape\n", teunit); 2268574Sroot return (EIO); 2273710Sroot } 2282608Swnj sc->sc_openf = 1; 2292471Swnj sc->sc_blkno = (daddr_t)0; 2302471Swnj sc->sc_nxrec = INF; 2312608Swnj sc->sc_lastiow = 0; 2323095Swnj sc->sc_dens = dens; 2335437Sroot s = spl6(); 2343495Sroot if (sc->sc_tact == 0) { 2353495Sroot sc->sc_timo = INF; 2363495Sroot sc->sc_tact = 1; 2373629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 2383495Sroot } 2395437Sroot splx(s); 2408574Sroot return (0); 2411919Swnj } 2421919Swnj 2432608Swnj /* 2442608Swnj * Close tape device. 2452608Swnj * 2462608Swnj * If tape was open for writing or last operation was 2472608Swnj * a write, then write two EOF's and backspace over the last one. 2482608Swnj * Unless this is a non-rewinding special file, rewind the tape. 2492608Swnj * Make the tape available to others. 2502608Swnj */ 2511919Swnj tmclose(dev, flag) 2521919Swnj register dev_t dev; 2531919Swnj register flag; 2541919Swnj { 2553095Swnj register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 2561919Swnj 2572608Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2582608Swnj tmcommand(dev, TM_WEOF, 1); 2592608Swnj tmcommand(dev, TM_WEOF, 1); 2602608Swnj tmcommand(dev, TM_SREV, 1); 2611919Swnj } 2621919Swnj if ((minor(dev)&T_NOREWIND) == 0) 2633095Swnj /* 2643095Swnj * 0 count means don't hang waiting for rewind complete 2653095Swnj * rather ctmbuf stays busy until the operation completes 2663095Swnj * preventing further opens from completing by 2673095Swnj * preventing a TM_SENSE from completing. 2683095Swnj */ 2693095Swnj tmcommand(dev, TM_REW, 0); 2702471Swnj sc->sc_openf = 0; 2711919Swnj } 2721919Swnj 2732608Swnj /* 2742608Swnj * Execute a command on the tape drive 2752608Swnj * a specified number of times. 2762608Swnj */ 2772574Swnj tmcommand(dev, com, count) 2781919Swnj dev_t dev; 2791919Swnj int com, count; 2801919Swnj { 2811919Swnj register struct buf *bp; 2825437Sroot register int s; 2831919Swnj 2842608Swnj bp = &ctmbuf[TMUNIT(dev)]; 2855437Sroot s = spl5(); 2861919Swnj while (bp->b_flags&B_BUSY) { 2873095Swnj /* 2883095Swnj * This special check is because B_BUSY never 2893095Swnj * gets cleared in the non-waiting rewind case. 2903095Swnj */ 2913141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 2923095Swnj break; 2931919Swnj bp->b_flags |= B_WANTED; 2941919Swnj sleep((caddr_t)bp, PRIBIO); 2951919Swnj } 2961919Swnj bp->b_flags = B_BUSY|B_READ; 2975437Sroot splx(s); 2981919Swnj bp->b_dev = dev; 2991919Swnj bp->b_repcnt = -count; 3001919Swnj bp->b_command = com; 3011919Swnj bp->b_blkno = 0; 3021919Swnj tmstrategy(bp); 3033095Swnj /* 3043095Swnj * In case of rewind from close, don't wait. 3053095Swnj * This is the only case where count can be 0. 3063095Swnj */ 3073095Swnj if (count == 0) 3083095Swnj return; 3091919Swnj iowait(bp); 3101919Swnj if (bp->b_flags&B_WANTED) 3111919Swnj wakeup((caddr_t)bp); 3121919Swnj bp->b_flags &= B_ERROR; 3131919Swnj } 3141919Swnj 3152608Swnj /* 3163095Swnj * Queue a tape operation. 3172608Swnj */ 3181919Swnj tmstrategy(bp) 3191919Swnj register struct buf *bp; 3201919Swnj { 3213095Swnj int teunit = TEUNIT(bp->b_dev); 3222982Swnj register struct uba_ctlr *um; 3232608Swnj register struct buf *dp; 3245437Sroot int s; 3251919Swnj 3262608Swnj /* 3272608Swnj * Put transfer at end of unit queue 3282608Swnj */ 3293095Swnj dp = &teutab[teunit]; 3301919Swnj bp->av_forw = NULL; 3315437Sroot s = spl5(); 3323939Sbugs um = tedinfo[teunit]->ui_mi; 3332608Swnj if (dp->b_actf == NULL) { 3342608Swnj dp->b_actf = bp; 3352608Swnj /* 3362608Swnj * Transport not already active... 3372608Swnj * put at end of controller queue. 3382608Swnj */ 3392608Swnj dp->b_forw = NULL; 3402608Swnj if (um->um_tab.b_actf == NULL) 3412608Swnj um->um_tab.b_actf = dp; 3422608Swnj else 3432608Swnj um->um_tab.b_actl->b_forw = dp; 3442608Swnj um->um_tab.b_actl = dp; 3452608Swnj } else 3462608Swnj dp->b_actl->av_forw = bp; 3472608Swnj dp->b_actl = bp; 3482608Swnj /* 3492608Swnj * If the controller is not busy, get 3502608Swnj * it going. 3512608Swnj */ 3522608Swnj if (um->um_tab.b_active == 0) 3532608Swnj tmstart(um); 3545437Sroot splx(s); 3551919Swnj } 3561919Swnj 3572608Swnj /* 3582608Swnj * Start activity on a tm controller. 3592608Swnj */ 3602608Swnj tmstart(um) 3612982Swnj register struct uba_ctlr *um; 3621919Swnj { 3632608Swnj register struct buf *bp, *dp; 3645692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 3653095Swnj register struct te_softc *sc; 3662982Swnj register struct uba_device *ui; 3673095Swnj int teunit, cmd; 3682471Swnj daddr_t blkno; 3691919Swnj 3702608Swnj /* 3712608Swnj * Look for an idle transport on the controller. 3722608Swnj */ 3731919Swnj loop: 3742608Swnj if ((dp = um->um_tab.b_actf) == NULL) 3751919Swnj return; 3762608Swnj if ((bp = dp->b_actf) == NULL) { 3772608Swnj um->um_tab.b_actf = dp->b_forw; 3782608Swnj goto loop; 3792608Swnj } 3803095Swnj teunit = TEUNIT(bp->b_dev); 3813095Swnj ui = tedinfo[teunit]; 3822608Swnj /* 3832608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 3842608Swnj */ 3853095Swnj sc = &te_softc[teunit]; 3865692Sroot addr = (struct tmdevice *)um->um_addr; 3872608Swnj addr->tmcs = (ui->ui_slave << 8); 3882471Swnj sc->sc_dsreg = addr->tmcs; 3892471Swnj sc->sc_erreg = addr->tmer; 3902471Swnj sc->sc_resid = addr->tmbc; 3912608Swnj /* 3922608Swnj * Default is that last command was NOT a write command; 3932608Swnj * if we do a write command we will notice this in tmintr(). 3942608Swnj */ 3953493Sroot sc->sc_lastiow = 0; 3962608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 3972608Swnj /* 3983095Swnj * Have had a hard error on a non-raw tape 3993095Swnj * or the tape unit is now unavailable 4003095Swnj * (e.g. taken off line). 4012608Swnj */ 4022608Swnj bp->b_flags |= B_ERROR; 4031919Swnj goto next; 4041919Swnj } 4053095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4063095Swnj /* 4073095Swnj * Execute control operation with the specified count. 4083095Swnj */ 4092608Swnj if (bp->b_command == TM_SENSE) 4102608Swnj goto next; 4113495Sroot /* 4123495Sroot * Set next state; give 5 minutes to complete 4133495Sroot * rewind, or 10 seconds per iteration (minimum 60 4143629Sroot * seconds and max 5 minutes) to complete other ops. 4153495Sroot */ 4163495Sroot if (bp->b_command == TM_REW) { 4173495Sroot um->um_tab.b_active = SREW; 4183495Sroot sc->sc_timo = 5 * 60; 4193495Sroot } else { 4203495Sroot um->um_tab.b_active = SCOM; 4214266Swnj sc->sc_timo = 4224266Swnj imin(imax(10*(int)-bp->b_repcnt,60),5*60); 4233495Sroot } 4242608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4252608Swnj addr->tmbc = bp->b_repcnt; 4262670Swnj goto dobpcmd; 4272608Swnj } 4282608Swnj /* 4293095Swnj * The following checks handle boundary cases for operation 4303095Swnj * on non-raw tapes. On raw tapes the initialization of 4313095Swnj * sc->sc_nxrec by tmphys causes them to be skipped normally 4323095Swnj * (except in the case of retries). 4333095Swnj */ 4347381Ssam if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4353095Swnj /* 4363095Swnj * Can't read past known end-of-file. 4373095Swnj */ 4383095Swnj bp->b_flags |= B_ERROR; 4393095Swnj bp->b_error = ENXIO; 4403095Swnj goto next; 4413095Swnj } 4427381Ssam if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec && 4433095Swnj bp->b_flags&B_READ) { 4443095Swnj /* 4453095Swnj * Reading at end of file returns 0 bytes. 4463095Swnj */ 4473095Swnj bp->b_resid = bp->b_bcount; 4483095Swnj clrbuf(bp); 4493095Swnj goto next; 4503095Swnj } 4513095Swnj if ((bp->b_flags&B_READ) == 0) 4523095Swnj /* 4533095Swnj * Writing sets EOF 4543095Swnj */ 4557381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1; 4563095Swnj /* 4572608Swnj * If the data transfer command is in the correct place, 4582608Swnj * set up all the registers except the csr, and give 4592608Swnj * control over to the UNIBUS adapter routines, to 4602608Swnj * wait for resources to start the i/o. 4612608Swnj */ 4627381Ssam if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) { 4632396Swnj addr->tmbc = -bp->b_bcount; 4641919Swnj if ((bp->b_flags&B_READ) == 0) { 4652471Swnj if (um->um_tab.b_errcnt) 4663095Swnj cmd = TM_WIRG; 4671919Swnj else 4683095Swnj cmd = TM_WCOM; 4691919Swnj } else 4703095Swnj cmd = TM_RCOM; 4712471Swnj um->um_tab.b_active = SIO; 4723095Swnj um->um_cmd = sc->sc_dens|cmd; 4732928Swnj #ifdef notdef 4742670Swnj if (tmreverseop(sc->sc_lastcmd)) 4753095Swnj while (addr->tmer & TMER_SDWN) 4762670Swnj tmgapsdcnt++; 4772670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 4782928Swnj #endif 4793495Sroot sc->sc_timo = 60; /* premature, but should serve */ 4803105Swnj (void) ubago(ui); 4811919Swnj return; 4821919Swnj } 4832608Swnj /* 4843095Swnj * Tape positioned incorrectly; 4853095Swnj * set to seek forwards or backwards to the correct spot. 4863095Swnj * This happens for raw tapes only on error retries. 4872608Swnj */ 4882471Swnj um->um_tab.b_active = SSEEK; 4897381Ssam if (blkno < bdbtofsb(bp->b_blkno)) { 4902670Swnj bp->b_command = TM_SFORW; 4917381Ssam addr->tmbc = blkno - bdbtofsb(bp->b_blkno); 4921919Swnj } else { 4932670Swnj bp->b_command = TM_SREV; 4947381Ssam addr->tmbc = bdbtofsb(bp->b_blkno) - blkno; 4951919Swnj } 4963629Sroot sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60); 4972670Swnj dobpcmd: 4982928Swnj #ifdef notdef 4993095Swnj /* 5003095Swnj * It is strictly necessary to wait for the tape 5013095Swnj * to stop before changing directions, but the TC11 5023095Swnj * handles this for us. 5033095Swnj */ 5042670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5052670Swnj while (addr->tmer & TM_SDWN) 5062670Swnj tmgapsdcnt++; 5072670Swnj sc->sc_lastcmd = bp->b_command; 5082928Swnj #endif 5093095Swnj /* 5103095Swnj * Do the command in bp. 5113095Swnj */ 5123095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5131919Swnj return; 5141919Swnj 5151919Swnj next: 5162608Swnj /* 5172608Swnj * Done with this operation due to error or 5182608Swnj * the fact that it doesn't do anything. 5192608Swnj * Release UBA resources (if any), dequeue 5202608Swnj * the transfer and continue processing this slave. 5212608Swnj */ 5222608Swnj if (um->um_ubinfo) 5232617Swnj ubadone(um); 5242608Swnj um->um_tab.b_errcnt = 0; 5252608Swnj dp->b_actf = bp->av_forw; 5261919Swnj iodone(bp); 5271919Swnj goto loop; 5281919Swnj } 5291919Swnj 5302608Swnj /* 5312608Swnj * The UNIBUS resources we needed have been 5322608Swnj * allocated to us; start the device. 5332608Swnj */ 5342574Swnj tmdgo(um) 5352982Swnj register struct uba_ctlr *um; 5361919Swnj { 5375692Sroot register struct tmdevice *addr = (struct tmdevice *)um->um_addr; 5382471Swnj 5392574Swnj addr->tmba = um->um_ubinfo; 5402574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5412396Swnj } 5422396Swnj 5432608Swnj /* 5442608Swnj * Tm interrupt routine. 5452608Swnj */ 5462471Swnj /*ARGSUSED*/ 5472630Swnj tmintr(tm11) 5482630Swnj int tm11; 5492396Swnj { 5502608Swnj struct buf *dp; 5511919Swnj register struct buf *bp; 5522982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5535692Sroot register struct tmdevice *addr; 5543095Swnj register struct te_softc *sc; 5553095Swnj int teunit; 5561919Swnj register state; 5571919Swnj 5583095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5593095Swnj return; 5603095Swnj bp = dp->b_actf; 5613095Swnj teunit = TEUNIT(bp->b_dev); 5625692Sroot addr = (struct tmdevice *)tedinfo[teunit]->ui_addr; 5633524Swnj sc = &te_softc[teunit]; 5642608Swnj /* 5652608Swnj * If last command was a rewind, and tape is still 5662608Swnj * rewinding, wait for the rewind complete interrupt. 5672608Swnj */ 5682608Swnj if (um->um_tab.b_active == SREW) { 5692608Swnj um->um_tab.b_active = SCOM; 5703524Swnj if (addr->tmer&TMER_RWS) { 5713524Swnj sc->sc_timo = 5*60; /* 5 minutes */ 5722608Swnj return; 5733524Swnj } 5741919Swnj } 5752608Swnj /* 5762608Swnj * An operation completed... record status 5772608Swnj */ 5783495Sroot sc->sc_timo = INF; 5792471Swnj sc->sc_dsreg = addr->tmcs; 5802471Swnj sc->sc_erreg = addr->tmer; 5812471Swnj sc->sc_resid = addr->tmbc; 5821919Swnj if ((bp->b_flags & B_READ) == 0) 5832608Swnj sc->sc_lastiow = 1; 5842471Swnj state = um->um_tab.b_active; 5852471Swnj um->um_tab.b_active = 0; 5862608Swnj /* 5872608Swnj * Check for errors. 5882608Swnj */ 5892608Swnj if (addr->tmcs&TM_ERR) { 5903095Swnj while (addr->tmer & TMER_SDWN) 5911919Swnj ; /* await settle down */ 5922608Swnj /* 5933095Swnj * If we hit the end of the tape file, update our position. 5942608Swnj */ 5953095Swnj if (addr->tmer&TMER_EOF) { 5962608Swnj tmseteof(bp); /* set blkno and nxrec */ 5972608Swnj state = SCOM; /* force completion */ 5982608Swnj /* 5992608Swnj * Stuff bc so it will be unstuffed correctly 6002608Swnj * later to get resid. 6012608Swnj */ 6022396Swnj addr->tmbc = -bp->b_bcount; 6032608Swnj goto opdone; 6041919Swnj } 6052608Swnj /* 6063095Swnj * If we were reading raw tape and the only error was that the 6073095Swnj * record was too long, then we don't consider this an error. 6082608Swnj */ 6093095Swnj if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6103095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6112608Swnj goto ignoreerr; 6122608Swnj /* 6132608Swnj * If error is not hard, and this was an i/o operation 6142608Swnj * retry up to 8 times. 6152608Swnj */ 6163095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6172471Swnj if (++um->um_tab.b_errcnt < 7) { 6182471Swnj sc->sc_blkno++; 6192617Swnj ubadone(um); 6202608Swnj goto opcont; 6211919Swnj } 6222608Swnj } else 6232608Swnj /* 6242608Swnj * Hard or non-i/o errors on non-raw tape 6252608Swnj * cause it to close. 6262608Swnj */ 6273095Swnj if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)]) 6282608Swnj sc->sc_openf = -1; 6292608Swnj /* 6302608Swnj * Couldn't recover error 6312608Swnj */ 6322928Swnj printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6333095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 6341919Swnj bp->b_flags |= B_ERROR; 6352608Swnj goto opdone; 6361919Swnj } 6372608Swnj /* 6382608Swnj * Advance tape control FSM. 6392608Swnj */ 6402608Swnj ignoreerr: 6411919Swnj switch (state) { 6421919Swnj 6431919Swnj case SIO: 6442608Swnj /* 6452608Swnj * Read/write increments tape block number 6462608Swnj */ 6472471Swnj sc->sc_blkno++; 6482608Swnj goto opdone; 6491919Swnj 6501919Swnj case SCOM: 6512608Swnj /* 6523095Swnj * For forward/backward space record update current position. 6532608Swnj */ 6543095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 6552608Swnj switch (bp->b_command) { 6561919Swnj 6572608Swnj case TM_SFORW: 6582608Swnj sc->sc_blkno -= bp->b_repcnt; 6593095Swnj break; 6601919Swnj 6612608Swnj case TM_SREV: 6622608Swnj sc->sc_blkno += bp->b_repcnt; 6633095Swnj break; 6641919Swnj } 6653095Swnj goto opdone; 6661919Swnj 6671919Swnj case SSEEK: 6687381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno); 6692608Swnj goto opcont; 6701919Swnj 6711919Swnj default: 6722608Swnj panic("tmintr"); 6732608Swnj } 6742608Swnj opdone: 6752608Swnj /* 6762608Swnj * Reset error count and remove 6772608Swnj * from device queue. 6782608Swnj */ 6792608Swnj um->um_tab.b_errcnt = 0; 6802608Swnj dp->b_actf = bp->av_forw; 681*14929Skarels /* 682*14929Skarels * Check resid; watch out for resid >32767 (tmbc not negative). 683*14929Skarels */ 684*14929Skarels if (addr->tmbc <= 0) 685*14929Skarels bp->b_resid = -addr->tmbc; 686*14929Skarels else 687*14929Skarels bp->b_resid = 0x10000 - ((long)(u_short) addr->tmbc); 6882617Swnj ubadone(um); 6892608Swnj iodone(bp); 6902608Swnj /* 6912608Swnj * Circulate slave to end of controller 6922608Swnj * queue to give other slaves a chance. 6932608Swnj */ 6942608Swnj um->um_tab.b_actf = dp->b_forw; 6952608Swnj if (dp->b_actf) { 6962608Swnj dp->b_forw = NULL; 6972608Swnj if (um->um_tab.b_actf == NULL) 6982608Swnj um->um_tab.b_actf = dp; 6992608Swnj else 7002608Swnj um->um_tab.b_actl->b_forw = dp; 7012608Swnj um->um_tab.b_actl = dp; 7022608Swnj } 7032608Swnj if (um->um_tab.b_actf == 0) 7041919Swnj return; 7052608Swnj opcont: 7062608Swnj tmstart(um); 7071919Swnj } 7081919Swnj 7093495Sroot tmtimer(dev) 7103495Sroot int dev; 7113495Sroot { 7123495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7134847Sroot register short x; 7143495Sroot 7153495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7164278Sroot printf("te%d: lost interrupt\n", TEUNIT(dev)); 7173495Sroot sc->sc_timo = INF; 7184847Sroot x = spl5(); 7193495Sroot tmintr(TMUNIT(dev)); 7204847Sroot (void) splx(x); 7213495Sroot } 7223629Sroot timeout(tmtimer, (caddr_t)dev, 5*hz); 7233495Sroot } 7243495Sroot 7251919Swnj tmseteof(bp) 7261919Swnj register struct buf *bp; 7271919Swnj { 7283095Swnj register int teunit = TEUNIT(bp->b_dev); 7295692Sroot register struct tmdevice *addr = 7305692Sroot (struct tmdevice *)tedinfo[teunit]->ui_addr; 7313095Swnj register struct te_softc *sc = &te_softc[teunit]; 7321919Swnj 7333095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7347381Ssam if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) { 7351919Swnj /* reversing */ 7367381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc; 7372471Swnj sc->sc_blkno = sc->sc_nxrec; 7381919Swnj } else { 7391919Swnj /* spacing forward */ 7407381Ssam sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc; 7412471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7421919Swnj } 7431919Swnj return; 7441919Swnj } 7451919Swnj /* eof on read */ 7467381Ssam sc->sc_nxrec = bdbtofsb(bp->b_blkno); 7471919Swnj } 7481919Swnj 7497732Sroot tmread(dev, uio) 7502608Swnj dev_t dev; 7517732Sroot struct uio *uio; 7521919Swnj { 7538163Sroot int errno; 7541919Swnj 7558163Sroot errno = tmphys(dev, uio); 7568163Sroot if (errno) 7578163Sroot return (errno); 7588163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio)); 7591919Swnj } 7601919Swnj 7617838Sroot tmwrite(dev, uio) 7622608Swnj dev_t dev; 7637838Sroot struct uio *uio; 7641919Swnj { 7658163Sroot int errno; 7661919Swnj 7678163Sroot errno = tmphys(dev, uio); 7688163Sroot if (errno) 7698163Sroot return (errno); 7708163Sroot return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio)); 7711919Swnj } 7721919Swnj 7733095Swnj /* 7743095Swnj * Check that a raw device exists. 7753095Swnj * If it does, set up sc_blkno and sc_nxrec 7763095Swnj * so that the tape will appear positioned correctly. 7773095Swnj */ 7787732Sroot tmphys(dev, uio) 7792608Swnj dev_t dev; 7807732Sroot struct uio *uio; 7811919Swnj { 7823095Swnj register int teunit = TEUNIT(dev); 7831919Swnj register daddr_t a; 7843095Swnj register struct te_softc *sc; 7853095Swnj register struct uba_device *ui; 7861919Swnj 7877838Sroot if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) 7887732Sroot return (ENXIO); 7893095Swnj sc = &te_softc[teunit]; 7907838Sroot a = bdbtofsb(uio->uio_offset >> 9); 7912471Swnj sc->sc_blkno = a; 7922471Swnj sc->sc_nxrec = a + 1; 7937732Sroot return (0); 7941919Swnj } 7951919Swnj 7962608Swnj tmreset(uban) 7972608Swnj int uban; 7982608Swnj { 7992982Swnj register struct uba_ctlr *um; 8003095Swnj register tm11, teunit; 8012982Swnj register struct uba_device *ui; 8022608Swnj register struct buf *dp; 8032608Swnj 8042630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 8052630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 8062608Swnj um->um_ubanum != uban) 8072608Swnj continue; 8082928Swnj printf(" tm%d", tm11); 8092608Swnj um->um_tab.b_active = 0; 8102608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8112608Swnj if (um->um_ubinfo) { 8122608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8139355Ssam um->um_ubinfo = 0; 8142608Swnj } 8155692Sroot ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR; 8163095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8173095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8183095Swnj ui->ui_alive == 0) 8192608Swnj continue; 8203095Swnj dp = &teutab[teunit]; 8212608Swnj dp->b_active = 0; 8222608Swnj dp->b_forw = 0; 8232608Swnj if (um->um_tab.b_actf == NULL) 8242608Swnj um->um_tab.b_actf = dp; 8252608Swnj else 8262608Swnj um->um_tab.b_actl->b_forw = dp; 8272608Swnj um->um_tab.b_actl = dp; 8283495Sroot if (te_softc[teunit].sc_openf > 0) 8293495Sroot te_softc[teunit].sc_openf = -1; 8302608Swnj } 8312608Swnj tmstart(um); 8322608Swnj } 8332608Swnj } 8342608Swnj 8351919Swnj /*ARGSUSED*/ 8367632Ssam tmioctl(dev, cmd, data, flag) 8377632Ssam caddr_t data; 8381919Swnj dev_t dev; 8391919Swnj { 8403095Swnj int teunit = TEUNIT(dev); 8413095Swnj register struct te_softc *sc = &te_softc[teunit]; 8423095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8431919Swnj register callcount; 8441919Swnj int fcount; 8457632Ssam struct mtop *mtop; 8467632Ssam struct mtget *mtget; 8471919Swnj /* we depend of the values and order of the MT codes here */ 8482608Swnj static tmops[] = 8492608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8501919Swnj 8512608Swnj switch (cmd) { 8527632Ssam 8537632Ssam case MTIOCTOP: /* tape operation */ 8547632Ssam mtop = (struct mtop *)data; 8558574Sroot switch (mtop->mt_op) { 8567632Ssam 8572608Swnj case MTWEOF: 8587632Ssam callcount = mtop->mt_count; 8592608Swnj fcount = 1; 8602608Swnj break; 8617632Ssam 8622608Swnj case MTFSF: case MTBSF: 8637632Ssam callcount = mtop->mt_count; 8641919Swnj fcount = INF; 8651919Swnj break; 8667632Ssam 8671919Swnj case MTFSR: case MTBSR: 8681919Swnj callcount = 1; 8697632Ssam fcount = mtop->mt_count; 8701919Swnj break; 8717632Ssam 8722324Skre case MTREW: case MTOFFL: case MTNOP: 8731919Swnj callcount = 1; 8741919Swnj fcount = 1; 8751919Swnj break; 8767632Ssam 8771919Swnj default: 8788574Sroot return (ENXIO); 8791919Swnj } 8808574Sroot if (callcount <= 0 || fcount <= 0) 8818574Sroot return (EINVAL); 8822608Swnj while (--callcount >= 0) { 8837632Ssam tmcommand(dev, tmops[mtop->mt_op], fcount); 8847632Ssam if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) && 8858574Sroot bp->b_resid) 8868574Sroot return (EIO); 8873095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 8881919Swnj break; 8891919Swnj } 8908648Sroot return (geterror(bp)); 8917632Ssam 8921919Swnj case MTIOCGET: 8937632Ssam mtget = (struct mtget *)data; 8947632Ssam mtget->mt_dsreg = sc->sc_dsreg; 8957632Ssam mtget->mt_erreg = sc->sc_erreg; 8967632Ssam mtget->mt_resid = sc->sc_resid; 8977632Ssam mtget->mt_type = MT_ISTM; 8988574Sroot break; 8997632Ssam 9001919Swnj default: 9018574Sroot return (ENXIO); 9021919Swnj } 9038574Sroot return (0); 9041919Swnj } 9051919Swnj 9061919Swnj #define DBSIZE 20 9071919Swnj 9082363Swnj tmdump() 9092363Swnj { 9102982Swnj register struct uba_device *ui; 9112396Swnj register struct uba_regs *up; 9125692Sroot register struct tmdevice *addr; 9132426Skre int blk, num; 9142426Skre int start; 9151919Swnj 9162426Skre start = 0; 9172426Skre num = maxfree; 9182426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9193095Swnj if (tedinfo[0] == 0) 9202887Swnj return (ENXIO); 9213095Swnj ui = phys(tedinfo[0], struct uba_device *); 9222396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9233331Swnj ubainit(up); 9242324Skre DELAY(1000000); 9255692Sroot addr = (struct tmdevice *)ui->ui_physaddr; 9262396Swnj tmwait(addr); 9272608Swnj addr->tmcs = TM_DCLR | TM_GO; 9281919Swnj while (num > 0) { 9291919Swnj blk = num > DBSIZE ? DBSIZE : num; 9302396Swnj tmdwrite(start, blk, addr, up); 9311919Swnj start += blk; 9321919Swnj num -= blk; 9331919Swnj } 9342426Skre tmeof(addr); 9352426Skre tmeof(addr); 9362426Skre tmwait(addr); 9372887Swnj if (addr->tmcs&TM_ERR) 9382887Swnj return (EIO); 9392608Swnj addr->tmcs = TM_REW | TM_GO; 9402471Swnj tmwait(addr); 9412363Swnj return (0); 9421919Swnj } 9431919Swnj 9442608Swnj tmdwrite(dbuf, num, addr, up) 9452608Swnj register dbuf, num; 9465692Sroot register struct tmdevice *addr; 9472396Swnj struct uba_regs *up; 9481919Swnj { 9492396Swnj register struct pte *io; 9502396Swnj register int npf; 9511928Swnj 9522396Swnj tmwait(addr); 9532396Swnj io = up->uba_map; 9541919Swnj npf = num+1; 9551928Swnj while (--npf != 0) 9562982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9572396Swnj *(int *)io = 0; 9582396Swnj addr->tmbc = -(num*NBPG); 9592396Swnj addr->tmba = 0; 9602608Swnj addr->tmcs = TM_WCOM | TM_GO; 9611919Swnj } 9621919Swnj 9632396Swnj tmwait(addr) 9645692Sroot register struct tmdevice *addr; 9651919Swnj { 9661928Swnj register s; 9671919Swnj 9681919Swnj do 9692396Swnj s = addr->tmcs; 9702608Swnj while ((s & TM_CUR) == 0); 9711919Swnj } 9721919Swnj 9732396Swnj tmeof(addr) 9745692Sroot struct tmdevice *addr; 9751919Swnj { 9761919Swnj 9772396Swnj tmwait(addr); 9782608Swnj addr->tmcs = TM_WEOF | TM_GO; 9791919Swnj } 9801919Swnj #endif 981