1*3519Sroot /* tm.c 4.35 81/04/14 */ 21919Swnj 32709Swnj #include "te.h" 4*3519Sroot #include "ts.h" 52630Swnj #if NTM > 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 */ 161919Swnj #include "../h/param.h" 173141Swnj #include "../h/systm.h" 181919Swnj #include "../h/buf.h" 191919Swnj #include "../h/dir.h" 201919Swnj #include "../h/conf.h" 211919Swnj #include "../h/user.h" 221919Swnj #include "../h/file.h" 231919Swnj #include "../h/map.h" 241919Swnj #include "../h/pte.h" 252574Swnj #include "../h/vm.h" 262982Swnj #include "../h/ubareg.h" 272982Swnj #include "../h/ubavar.h" 281919Swnj #include "../h/mtio.h" 291919Swnj #include "../h/ioctl.h" 302363Swnj #include "../h/cmap.h" 312396Swnj #include "../h/cpu.h" 321919Swnj 332396Swnj #include "../h/tmreg.h" 341919Swnj 353095Swnj /* 363095Swnj * There is a ctmbuf per tape controller. 373095Swnj * It is used as the token to pass to the internal routines 383095Swnj * to execute tape ioctls, and also acts as a lock on the slaves 393095Swnj * on the controller, since there is only one per controller. 403095Swnj * In particular, when the tape is rewinding on close we release 413095Swnj * the user process but any further attempts to use the tape drive 423095Swnj * before the rewind completes will hang waiting for ctmbuf. 433095Swnj */ 443095Swnj struct buf ctmbuf[NTM]; 451919Swnj 463095Swnj /* 473095Swnj * Raw tape operations use rtmbuf. The driver 483095Swnj * notices when rtmbuf is being used and allows the user 493095Swnj * program to continue after errors and read records 503095Swnj * not of the standard length (BSIZE). 513095Swnj */ 523095Swnj struct buf rtmbuf[NTM]; 533095Swnj 543095Swnj /* 553095Swnj * Driver unibus interface routines and variables. 563095Swnj */ 572608Swnj int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr(); 582982Swnj struct uba_ctlr *tmminfo[NTM]; 593095Swnj struct uba_device *tedinfo[NTE]; 603095Swnj struct buf teutab[NTE]; 613095Swnj short tetotm[NTE]; 622458Swnj u_short tmstd[] = { 0772520, 0 }; 632396Swnj struct uba_driver tmdriver = 643095Swnj { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 }; 651919Swnj 661919Swnj /* bits in minor device */ 673095Swnj #define TEUNIT(dev) (minor(dev)&03) 683095Swnj #define TMUNIT(dev) (tetotm[TEUNIT(dev)]) 691919Swnj #define T_NOREWIND 04 701919Swnj #define T_1600BPI 08 711919Swnj 721919Swnj #define INF (daddr_t)1000000L 731919Swnj 742608Swnj /* 752608Swnj * Software state per tape transport. 763095Swnj * 773095Swnj * 1. A tape drive is a unique-open device; we refuse opens when it is already. 783095Swnj * 2. We keep track of the current position on a block tape and seek 793095Swnj * before operations by forward/back spacing if necessary. 803095Swnj * 3. We remember if the last operation was a write on a tape, so if a tape 813095Swnj * is open read write and the last thing done is a write we can 823095Swnj * write a standard end of tape mark (two eofs). 833095Swnj * 4. We remember the status registers after the last command, using 843095Swnj * then internally and returning them to the SENSE ioctl. 853095Swnj * 5. We remember the last density the tape was used at. If it is 863095Swnj * not a BOT when we start using it and we are writing, we don't 873095Swnj * let the density be changed. 882608Swnj */ 893095Swnj struct te_softc { 902608Swnj char sc_openf; /* lock against multiple opens */ 912608Swnj char sc_lastiow; /* last op was a write */ 922608Swnj daddr_t sc_blkno; /* block number, for block device tape */ 933095Swnj daddr_t sc_nxrec; /* position of end of tape, if known */ 942608Swnj u_short sc_erreg; /* copy of last erreg */ 952608Swnj u_short sc_dsreg; /* copy of last dsreg */ 962608Swnj short sc_resid; /* copy of last bc */ 973105Swnj #ifdef unneeded 982670Swnj short sc_lastcmd; /* last command to handle direction changes */ 992928Swnj #endif 1003095Swnj u_short sc_dens; /* prototype command with density info */ 1013495Sroot daddr_t sc_timo; /* time until timeout expires */ 1023495Sroot short sc_tact; /* timeout is active */ 1033095Swnj } te_softc[NTM]; 1043105Swnj #ifdef unneeded 1053105Swnj int tmgapsdcnt; /* DEBUG */ 1063105Swnj #endif 1071919Swnj 1082608Swnj /* 1093095Swnj * States for um->um_tab.b_active, the per controller state flag. 1103095Swnj * This is used to sequence control in the driver. 1112608Swnj */ 1121919Swnj #define SSEEK 1 /* seeking */ 1131919Swnj #define SIO 2 /* doing seq i/o */ 1141919Swnj #define SCOM 3 /* sending control command */ 1152608Swnj #define SREW 4 /* sending a drive rewind */ 1161919Swnj 117*3519Sroot #if NTS > 0 1182426Skre /* 119*3519Sroot * Kludge to get around fact that we don't really 120*3519Sroot * check if a ts is there... if there are both tm's and ts's 121*3519Sroot * declared in the system, then this driver sets havetm to 1 122*3519Sroot * if it finds a tm, and ts just pretends there isn't a ts. 123*3519Sroot */ 124*3519Sroot int havetm = 0; 125*3519Sroot #endif 126*3519Sroot /* 1272426Skre * Determine if there is a controller for 1282426Skre * a tm at address reg. Our goal is to make the 1292426Skre * device interrupt. 1302426Skre */ 1312608Swnj tmprobe(reg) 1322396Swnj caddr_t reg; 1332396Swnj { 1343095Swnj register int br, cvec; /* must be r11,r10; value-result */ 1352426Skre 1362608Swnj #ifdef lint 1373105Swnj br = 0; cvec = br; br = cvec; 1382608Swnj #endif 1392608Swnj ((struct device *)reg)->tmcs = TM_IE; 1402396Swnj /* 1412630Swnj * If this is a tm11, it ought to have interrupted 1422396Swnj * by now, if it isn't (ie: it is a ts04) then we just 1432458Swnj * hope that it didn't interrupt, so autoconf will ignore it. 1442458Swnj * Just in case, we will reference one 1452396Swnj * of the more distant registers, and hope for a machine 1462458Swnj * check, or similar disaster if this is a ts. 1472471Swnj * 1482471Swnj * Note: on an 11/780, badaddr will just generate 1492471Swnj * a uba error for a ts; but our caller will notice that 1502471Swnj * so we won't check for it. 1512396Swnj */ 1523105Swnj if (badaddr((caddr_t)&((struct device *)reg)->tmrd, 2)) 1532458Swnj return (0); 1542458Swnj return (1); 1552396Swnj } 1562396Swnj 1572608Swnj /* 1582608Swnj * Due to a design flaw, we cannot ascertain if the tape 1592608Swnj * exists or not unless it is on line - ie: unless a tape is 1602608Swnj * mounted. This is too servere a restriction to bear, 1612608Swnj * so all units are assumed to exist. 1622608Swnj */ 1632608Swnj /*ARGSUSED*/ 1642574Swnj tmslave(ui, reg) 1652982Swnj struct uba_device *ui; 1662396Swnj caddr_t reg; 1672396Swnj { 1682458Swnj 1692458Swnj return (1); 1702396Swnj } 1712396Swnj 1722608Swnj /* 1733095Swnj * Record attachment of the unit to the controller. 1742608Swnj */ 1752608Swnj /*ARGSUSED*/ 1762608Swnj tmattach(ui) 1772982Swnj struct uba_device *ui; 1782608Swnj { 1792608Swnj 180*3519Sroot #if NTS > 0 181*3519Sroot havetm = 1; 182*3519Sroot #endif 1833095Swnj /* 1843095Swnj * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf 1853095Swnj * arrays given a te unit number. 1863095Swnj */ 1873095Swnj tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr; 1882608Swnj } 1892608Swnj 1903495Sroot int tmtimer(); 1912608Swnj /* 1922608Swnj * Open the device. Tapes are unique open 1932608Swnj * devices, so we refuse if it is already open. 1942608Swnj * We also check that a tape is available, and 1953095Swnj * don't block waiting here; if you want to wait 1963095Swnj * for a tape you should timeout in user code. 1972608Swnj */ 1981919Swnj tmopen(dev, flag) 1991919Swnj dev_t dev; 2001919Swnj int flag; 2011919Swnj { 2023095Swnj register int teunit; 2032982Swnj register struct uba_device *ui; 2043095Swnj register struct te_softc *sc; 2053209Swnj int olddens, dens; 2061919Swnj 2073095Swnj teunit = TEUNIT(dev); 2083095Swnj if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf || 2093095Swnj (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) { 2102608Swnj u.u_error = ENXIO; 2111919Swnj return; 2121919Swnj } 2133209Swnj olddens = sc->sc_dens; 2143209Swnj dens = TM_IE | TM_GO | (ui->ui_slave << 8); 2153209Swnj if ((minor(dev) & T_1600BPI) == 0) 2163209Swnj dens |= TM_D800; 2173209Swnj sc->sc_dens = dens; 2183141Swnj get: 2192608Swnj tmcommand(dev, TM_SENSE, 1); 2203141Swnj if (sc->sc_erreg&TMER_SDWN) { 2213141Swnj sleep((caddr_t)&lbolt, PZERO+1); 2223141Swnj goto get; 2233141Swnj } 2243209Swnj sc->sc_dens = olddens; 2253095Swnj if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR) || 2263174Swnj (flag&FWRITE) && (sc->sc_erreg&TMER_WRL) || 2273095Swnj (sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) && 2283174Swnj dens != sc->sc_dens) { 2293095Swnj /* 2303095Swnj * Not online or density switch in mid-tape or write locked. 2313095Swnj */ 2322471Swnj u.u_error = EIO; 2332608Swnj return; 2341919Swnj } 2352608Swnj sc->sc_openf = 1; 2362471Swnj sc->sc_blkno = (daddr_t)0; 2372471Swnj sc->sc_nxrec = INF; 2382608Swnj sc->sc_lastiow = 0; 2393095Swnj sc->sc_dens = dens; 2403495Sroot (void) spl6(); 2413495Sroot if (sc->sc_tact == 0) { 2423495Sroot sc->sc_timo = INF; 2433495Sroot sc->sc_tact = 1; 2443495Sroot timeout(tmtimer, dev, 5*hz); 2453495Sroot } 2463495Sroot (void) spl0(); 2471919Swnj } 2481919Swnj 2492608Swnj /* 2502608Swnj * Close tape device. 2512608Swnj * 2522608Swnj * If tape was open for writing or last operation was 2532608Swnj * a write, then write two EOF's and backspace over the last one. 2542608Swnj * Unless this is a non-rewinding special file, rewind the tape. 2552608Swnj * Make the tape available to others. 2562608Swnj */ 2571919Swnj tmclose(dev, flag) 2581919Swnj register dev_t dev; 2591919Swnj register flag; 2601919Swnj { 2613095Swnj register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 2621919Swnj 2632608Swnj if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) { 2642608Swnj tmcommand(dev, TM_WEOF, 1); 2652608Swnj tmcommand(dev, TM_WEOF, 1); 2662608Swnj tmcommand(dev, TM_SREV, 1); 2671919Swnj } 2681919Swnj if ((minor(dev)&T_NOREWIND) == 0) 2693095Swnj /* 2703095Swnj * 0 count means don't hang waiting for rewind complete 2713095Swnj * rather ctmbuf stays busy until the operation completes 2723095Swnj * preventing further opens from completing by 2733095Swnj * preventing a TM_SENSE from completing. 2743095Swnj */ 2753095Swnj tmcommand(dev, TM_REW, 0); 2762471Swnj sc->sc_openf = 0; 2771919Swnj } 2781919Swnj 2792608Swnj /* 2802608Swnj * Execute a command on the tape drive 2812608Swnj * a specified number of times. 2822608Swnj */ 2832574Swnj tmcommand(dev, com, count) 2841919Swnj dev_t dev; 2851919Swnj int com, count; 2861919Swnj { 2871919Swnj register struct buf *bp; 2881919Swnj 2892608Swnj bp = &ctmbuf[TMUNIT(dev)]; 2901919Swnj (void) spl5(); 2911919Swnj while (bp->b_flags&B_BUSY) { 2923095Swnj /* 2933095Swnj * This special check is because B_BUSY never 2943095Swnj * gets cleared in the non-waiting rewind case. 2953095Swnj */ 2963141Swnj if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE)) 2973095Swnj break; 2981919Swnj bp->b_flags |= B_WANTED; 2991919Swnj sleep((caddr_t)bp, PRIBIO); 3001919Swnj } 3011919Swnj bp->b_flags = B_BUSY|B_READ; 3021919Swnj (void) spl0(); 3031919Swnj bp->b_dev = dev; 3041919Swnj bp->b_repcnt = -count; 3051919Swnj bp->b_command = com; 3061919Swnj bp->b_blkno = 0; 3071919Swnj tmstrategy(bp); 3083095Swnj /* 3093095Swnj * In case of rewind from close, don't wait. 3103095Swnj * This is the only case where count can be 0. 3113095Swnj */ 3123095Swnj if (count == 0) 3133095Swnj return; 3141919Swnj iowait(bp); 3151919Swnj if (bp->b_flags&B_WANTED) 3161919Swnj wakeup((caddr_t)bp); 3171919Swnj bp->b_flags &= B_ERROR; 3181919Swnj } 3191919Swnj 3202608Swnj /* 3213095Swnj * Queue a tape operation. 3222608Swnj */ 3231919Swnj tmstrategy(bp) 3241919Swnj register struct buf *bp; 3251919Swnj { 3263095Swnj int teunit = TEUNIT(bp->b_dev); 3272982Swnj register struct uba_ctlr *um; 3282608Swnj register struct buf *dp; 3291919Swnj 3302608Swnj /* 3312608Swnj * Put transfer at end of unit queue 3322608Swnj */ 3333095Swnj dp = &teutab[teunit]; 3341919Swnj bp->av_forw = NULL; 3351919Swnj (void) spl5(); 3362608Swnj if (dp->b_actf == NULL) { 3372608Swnj dp->b_actf = bp; 3382608Swnj /* 3392608Swnj * Transport not already active... 3402608Swnj * put at end of controller queue. 3412608Swnj */ 3422608Swnj dp->b_forw = NULL; 3433095Swnj um = tedinfo[teunit]->ui_mi; 3442608Swnj if (um->um_tab.b_actf == NULL) 3452608Swnj um->um_tab.b_actf = dp; 3462608Swnj else 3472608Swnj um->um_tab.b_actl->b_forw = dp; 3482608Swnj um->um_tab.b_actl = dp; 3492608Swnj } else 3502608Swnj dp->b_actl->av_forw = bp; 3512608Swnj dp->b_actl = bp; 3522608Swnj /* 3532608Swnj * If the controller is not busy, get 3542608Swnj * it going. 3552608Swnj */ 3562608Swnj if (um->um_tab.b_active == 0) 3572608Swnj tmstart(um); 3581919Swnj (void) spl0(); 3591919Swnj } 3601919Swnj 3612608Swnj /* 3622608Swnj * Start activity on a tm controller. 3632608Swnj */ 3642608Swnj tmstart(um) 3652982Swnj register struct uba_ctlr *um; 3661919Swnj { 3672608Swnj register struct buf *bp, *dp; 3682608Swnj register struct device *addr = (struct device *)um->um_addr; 3693095Swnj register struct te_softc *sc; 3702982Swnj register struct uba_device *ui; 3713095Swnj int teunit, cmd; 3722471Swnj daddr_t blkno; 3731919Swnj 3742608Swnj /* 3752608Swnj * Look for an idle transport on the controller. 3762608Swnj */ 3771919Swnj loop: 3782608Swnj if ((dp = um->um_tab.b_actf) == NULL) 3791919Swnj return; 3802608Swnj if ((bp = dp->b_actf) == NULL) { 3812608Swnj um->um_tab.b_actf = dp->b_forw; 3822608Swnj goto loop; 3832608Swnj } 3843095Swnj teunit = TEUNIT(bp->b_dev); 3853095Swnj ui = tedinfo[teunit]; 3862608Swnj /* 3872608Swnj * Record pre-transfer status (e.g. for TM_SENSE) 3882608Swnj */ 3893095Swnj sc = &te_softc[teunit]; 3902608Swnj addr = (struct device *)um->um_addr; 3912608Swnj addr->tmcs = (ui->ui_slave << 8); 3922471Swnj sc->sc_dsreg = addr->tmcs; 3932471Swnj sc->sc_erreg = addr->tmer; 3942471Swnj sc->sc_resid = addr->tmbc; 3952608Swnj /* 3962608Swnj * Default is that last command was NOT a write command; 3972608Swnj * if we do a write command we will notice this in tmintr(). 3982608Swnj */ 3993493Sroot sc->sc_lastiow = 0; 4002608Swnj if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) { 4012608Swnj /* 4023095Swnj * Have had a hard error on a non-raw tape 4033095Swnj * or the tape unit is now unavailable 4043095Swnj * (e.g. taken off line). 4052608Swnj */ 4062608Swnj bp->b_flags |= B_ERROR; 4071919Swnj goto next; 4081919Swnj } 4093095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 4103095Swnj /* 4113095Swnj * Execute control operation with the specified count. 4123095Swnj */ 4132608Swnj if (bp->b_command == TM_SENSE) 4142608Swnj goto next; 4153495Sroot /* 4163495Sroot * Set next state; give 5 minutes to complete 4173495Sroot * rewind, or 10 seconds per iteration (minimum 60 4183495Sroot * seconds and max 5 minute) to complete other ops. 4193495Sroot */ 4203495Sroot if (bp->b_command == TM_REW) { 4213495Sroot um->um_tab.b_active = SREW; 4223495Sroot sc->sc_timo = 5 * 60; 4233495Sroot } else { 4243495Sroot um->um_tab.b_active = SCOM; 4253495Sroot sc->sc_timo = min(max(10 * bp->b_repcnt, 60), 5 * 60); 4263495Sroot } 4272608Swnj if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV) 4282608Swnj addr->tmbc = bp->b_repcnt; 4292670Swnj goto dobpcmd; 4302608Swnj } 4312608Swnj /* 4323095Swnj * The following checks handle boundary cases for operation 4333095Swnj * on non-raw tapes. On raw tapes the initialization of 4343095Swnj * sc->sc_nxrec by tmphys causes them to be skipped normally 4353095Swnj * (except in the case of retries). 4363095Swnj */ 4373095Swnj if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) { 4383095Swnj /* 4393095Swnj * Can't read past known end-of-file. 4403095Swnj */ 4413095Swnj bp->b_flags |= B_ERROR; 4423095Swnj bp->b_error = ENXIO; 4433095Swnj goto next; 4443095Swnj } 4453095Swnj if (dbtofsb(bp->b_blkno) == sc->sc_nxrec && 4463095Swnj bp->b_flags&B_READ) { 4473095Swnj /* 4483095Swnj * Reading at end of file returns 0 bytes. 4493095Swnj */ 4503095Swnj bp->b_resid = bp->b_bcount; 4513095Swnj clrbuf(bp); 4523095Swnj goto next; 4533095Swnj } 4543095Swnj if ((bp->b_flags&B_READ) == 0) 4553095Swnj /* 4563095Swnj * Writing sets EOF 4573095Swnj */ 4583095Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1; 4593095Swnj /* 4602608Swnj * If the data transfer command is in the correct place, 4612608Swnj * set up all the registers except the csr, and give 4622608Swnj * control over to the UNIBUS adapter routines, to 4632608Swnj * wait for resources to start the i/o. 4642608Swnj */ 4652471Swnj if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) { 4662396Swnj addr->tmbc = -bp->b_bcount; 4671919Swnj if ((bp->b_flags&B_READ) == 0) { 4682471Swnj if (um->um_tab.b_errcnt) 4693095Swnj cmd = TM_WIRG; 4701919Swnj else 4713095Swnj cmd = TM_WCOM; 4721919Swnj } else 4733095Swnj cmd = TM_RCOM; 4742471Swnj um->um_tab.b_active = SIO; 4753095Swnj um->um_cmd = sc->sc_dens|cmd; 4762928Swnj #ifdef notdef 4772670Swnj if (tmreverseop(sc->sc_lastcmd)) 4783095Swnj while (addr->tmer & TMER_SDWN) 4792670Swnj tmgapsdcnt++; 4802670Swnj sc->sc_lastcmd = TM_RCOM; /* will serve */ 4812928Swnj #endif 4823495Sroot sc->sc_timo = 60; /* premature, but should serve */ 4833105Swnj (void) ubago(ui); 4841919Swnj return; 4851919Swnj } 4862608Swnj /* 4873095Swnj * Tape positioned incorrectly; 4883095Swnj * set to seek forwards or backwards to the correct spot. 4893095Swnj * This happens for raw tapes only on error retries. 4902608Swnj */ 4912471Swnj um->um_tab.b_active = SSEEK; 4921919Swnj if (blkno < dbtofsb(bp->b_blkno)) { 4932670Swnj bp->b_command = TM_SFORW; 4942396Swnj addr->tmbc = blkno - dbtofsb(bp->b_blkno); 4951919Swnj } else { 4962670Swnj bp->b_command = TM_SREV; 4972396Swnj addr->tmbc = dbtofsb(bp->b_blkno) - blkno; 4981919Swnj } 4993495Sroot sc->sc_timo = min(max(10 * -addr->tmbc, 60), 5 * 60); 5002670Swnj dobpcmd: 5012928Swnj #ifdef notdef 5023095Swnj /* 5033095Swnj * It is strictly necessary to wait for the tape 5043095Swnj * to stop before changing directions, but the TC11 5053095Swnj * handles this for us. 5063095Swnj */ 5072670Swnj if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command)) 5082670Swnj while (addr->tmer & TM_SDWN) 5092670Swnj tmgapsdcnt++; 5102670Swnj sc->sc_lastcmd = bp->b_command; 5112928Swnj #endif 5123095Swnj /* 5133095Swnj * Do the command in bp. 5143095Swnj */ 5153095Swnj addr->tmcs = (sc->sc_dens | bp->b_command); 5161919Swnj return; 5171919Swnj 5181919Swnj next: 5192608Swnj /* 5202608Swnj * Done with this operation due to error or 5212608Swnj * the fact that it doesn't do anything. 5222608Swnj * Release UBA resources (if any), dequeue 5232608Swnj * the transfer and continue processing this slave. 5242608Swnj */ 5252608Swnj if (um->um_ubinfo) 5262617Swnj ubadone(um); 5272608Swnj um->um_tab.b_errcnt = 0; 5282608Swnj dp->b_actf = bp->av_forw; 5291919Swnj iodone(bp); 5301919Swnj goto loop; 5311919Swnj } 5321919Swnj 5332608Swnj /* 5342608Swnj * The UNIBUS resources we needed have been 5352608Swnj * allocated to us; start the device. 5362608Swnj */ 5372574Swnj tmdgo(um) 5382982Swnj register struct uba_ctlr *um; 5391919Swnj { 5402574Swnj register struct device *addr = (struct device *)um->um_addr; 5412471Swnj 5422574Swnj addr->tmba = um->um_ubinfo; 5432574Swnj addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30); 5442396Swnj } 5452396Swnj 5462608Swnj /* 5472608Swnj * Tm interrupt routine. 5482608Swnj */ 5492471Swnj /*ARGSUSED*/ 5502630Swnj tmintr(tm11) 5512630Swnj int tm11; 5522396Swnj { 5532608Swnj struct buf *dp; 5541919Swnj register struct buf *bp; 5552982Swnj register struct uba_ctlr *um = tmminfo[tm11]; 5563095Swnj register struct device *addr; 5573095Swnj register struct te_softc *sc; 5583095Swnj int teunit; 5591919Swnj register state; 5601919Swnj 5613095Swnj if ((dp = um->um_tab.b_actf) == NULL) 5623095Swnj return; 5633095Swnj bp = dp->b_actf; 5643095Swnj teunit = TEUNIT(bp->b_dev); 5653095Swnj addr = (struct device *)tedinfo[teunit]->ui_addr; 5662608Swnj /* 5672608Swnj * If last command was a rewind, and tape is still 5682608Swnj * rewinding, wait for the rewind complete interrupt. 5692608Swnj */ 5702608Swnj if (um->um_tab.b_active == SREW) { 5712608Swnj um->um_tab.b_active = SCOM; 5723095Swnj if (addr->tmer&TMER_RWS) 5732608Swnj return; 5741919Swnj } 5752608Swnj /* 5762608Swnj * An operation completed... record status 5772608Swnj */ 5783095Swnj sc = &te_softc[teunit]; 5793495Sroot sc->sc_timo = INF; 5802471Swnj sc->sc_dsreg = addr->tmcs; 5812471Swnj sc->sc_erreg = addr->tmer; 5822471Swnj sc->sc_resid = addr->tmbc; 5831919Swnj if ((bp->b_flags & B_READ) == 0) 5842608Swnj sc->sc_lastiow = 1; 5852471Swnj state = um->um_tab.b_active; 5862471Swnj um->um_tab.b_active = 0; 5872608Swnj /* 5882608Swnj * Check for errors. 5892608Swnj */ 5902608Swnj if (addr->tmcs&TM_ERR) { 5913095Swnj while (addr->tmer & TMER_SDWN) 5921919Swnj ; /* await settle down */ 5932608Swnj /* 5943095Swnj * If we hit the end of the tape file, update our position. 5952608Swnj */ 5963095Swnj if (addr->tmer&TMER_EOF) { 5972608Swnj tmseteof(bp); /* set blkno and nxrec */ 5982608Swnj state = SCOM; /* force completion */ 5992608Swnj /* 6002608Swnj * Stuff bc so it will be unstuffed correctly 6012608Swnj * later to get resid. 6022608Swnj */ 6032396Swnj addr->tmbc = -bp->b_bcount; 6042608Swnj goto opdone; 6051919Swnj } 6062608Swnj /* 6073095Swnj * If we were reading raw tape and the only error was that the 6083095Swnj * record was too long, then we don't consider this an error. 6092608Swnj */ 6103095Swnj if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) && 6113095Swnj (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE) 6122608Swnj goto ignoreerr; 6132608Swnj /* 6142608Swnj * If error is not hard, and this was an i/o operation 6152608Swnj * retry up to 8 times. 6162608Swnj */ 6173095Swnj if ((addr->tmer&TMER_HARD)==0 && state==SIO) { 6182471Swnj if (++um->um_tab.b_errcnt < 7) { 6192471Swnj sc->sc_blkno++; 6202617Swnj ubadone(um); 6212608Swnj goto opcont; 6221919Swnj } 6232608Swnj } else 6242608Swnj /* 6252608Swnj * Hard or non-i/o errors on non-raw tape 6262608Swnj * cause it to close. 6272608Swnj */ 6283095Swnj if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)]) 6292608Swnj sc->sc_openf = -1; 6302608Swnj /* 6312608Swnj * Couldn't recover error 6322608Swnj */ 6332928Swnj printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03, 6343095Swnj bp->b_blkno, sc->sc_erreg, TMER_BITS); 6351919Swnj bp->b_flags |= B_ERROR; 6362608Swnj goto opdone; 6371919Swnj } 6382608Swnj /* 6392608Swnj * Advance tape control FSM. 6402608Swnj */ 6412608Swnj ignoreerr: 6421919Swnj switch (state) { 6431919Swnj 6441919Swnj case SIO: 6452608Swnj /* 6462608Swnj * Read/write increments tape block number 6472608Swnj */ 6482471Swnj sc->sc_blkno++; 6492608Swnj goto opdone; 6501919Swnj 6511919Swnj case SCOM: 6522608Swnj /* 6533095Swnj * For forward/backward space record update current position. 6542608Swnj */ 6553095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) 6562608Swnj switch (bp->b_command) { 6571919Swnj 6582608Swnj case TM_SFORW: 6592608Swnj sc->sc_blkno -= bp->b_repcnt; 6603095Swnj break; 6611919Swnj 6622608Swnj case TM_SREV: 6632608Swnj sc->sc_blkno += bp->b_repcnt; 6643095Swnj break; 6651919Swnj } 6663095Swnj goto opdone; 6671919Swnj 6681919Swnj case SSEEK: 6692471Swnj sc->sc_blkno = dbtofsb(bp->b_blkno); 6702608Swnj goto opcont; 6711919Swnj 6721919Swnj default: 6732608Swnj panic("tmintr"); 6742608Swnj } 6752608Swnj opdone: 6762608Swnj /* 6772608Swnj * Reset error count and remove 6782608Swnj * from device queue. 6792608Swnj */ 6802608Swnj um->um_tab.b_errcnt = 0; 6812608Swnj dp->b_actf = bp->av_forw; 6822608Swnj bp->b_resid = -addr->tmbc; 6832617Swnj ubadone(um); 6842608Swnj iodone(bp); 6852608Swnj /* 6862608Swnj * Circulate slave to end of controller 6872608Swnj * queue to give other slaves a chance. 6882608Swnj */ 6892608Swnj um->um_tab.b_actf = dp->b_forw; 6902608Swnj if (dp->b_actf) { 6912608Swnj dp->b_forw = NULL; 6922608Swnj if (um->um_tab.b_actf == NULL) 6932608Swnj um->um_tab.b_actf = dp; 6942608Swnj else 6952608Swnj um->um_tab.b_actl->b_forw = dp; 6962608Swnj um->um_tab.b_actl = dp; 6972608Swnj } 6982608Swnj if (um->um_tab.b_actf == 0) 6991919Swnj return; 7002608Swnj opcont: 7012608Swnj tmstart(um); 7021919Swnj } 7031919Swnj 7043495Sroot tmtimer(dev) 7053495Sroot int dev; 7063495Sroot { 7073495Sroot register struct te_softc *sc = &te_softc[TEUNIT(dev)]; 7083495Sroot 7093495Sroot if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) { 7103495Sroot printf("te%d: lost interrupt\n"); 7113495Sroot sc->sc_timo = INF; 7123495Sroot (void) spl5(); 7133495Sroot tmintr(TMUNIT(dev)); 7143495Sroot (void) spl0(); 7153495Sroot } 7163495Sroot timeout(tmtimer, dev, 5*hz); 7173495Sroot } 7183495Sroot 7191919Swnj tmseteof(bp) 7201919Swnj register struct buf *bp; 7211919Swnj { 7223095Swnj register int teunit = TEUNIT(bp->b_dev); 7232396Swnj register struct device *addr = 7243095Swnj (struct device *)tedinfo[teunit]->ui_addr; 7253095Swnj register struct te_softc *sc = &te_softc[teunit]; 7261919Swnj 7273095Swnj if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) { 7282471Swnj if (sc->sc_blkno > dbtofsb(bp->b_blkno)) { 7291919Swnj /* reversing */ 7302471Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc; 7312471Swnj sc->sc_blkno = sc->sc_nxrec; 7321919Swnj } else { 7331919Swnj /* spacing forward */ 7342471Swnj sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc; 7352471Swnj sc->sc_nxrec = sc->sc_blkno - 1; 7361919Swnj } 7371919Swnj return; 7381919Swnj } 7391919Swnj /* eof on read */ 7402471Swnj sc->sc_nxrec = dbtofsb(bp->b_blkno); 7411919Swnj } 7421919Swnj 7431919Swnj tmread(dev) 7442608Swnj dev_t dev; 7451919Swnj { 7461919Swnj 7471919Swnj tmphys(dev); 7482982Swnj if (u.u_error) 7492982Swnj return; 7502608Swnj physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys); 7511919Swnj } 7521919Swnj 7531919Swnj tmwrite(dev) 7542608Swnj dev_t dev; 7551919Swnj { 7561919Swnj 7571919Swnj tmphys(dev); 7582982Swnj if (u.u_error) 7592982Swnj return; 7602608Swnj physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys); 7611919Swnj } 7621919Swnj 7633095Swnj /* 7643095Swnj * Check that a raw device exists. 7653095Swnj * If it does, set up sc_blkno and sc_nxrec 7663095Swnj * so that the tape will appear positioned correctly. 7673095Swnj */ 7681919Swnj tmphys(dev) 7692608Swnj dev_t dev; 7701919Swnj { 7713095Swnj register int teunit = TEUNIT(dev); 7721919Swnj register daddr_t a; 7733095Swnj register struct te_softc *sc; 7743095Swnj register struct uba_device *ui; 7751919Swnj 7763095Swnj if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) { 7772982Swnj u.u_error = ENXIO; 7782982Swnj return; 7792982Swnj } 7803095Swnj sc = &te_softc[teunit]; 7811919Swnj a = dbtofsb(u.u_offset >> 9); 7822471Swnj sc->sc_blkno = a; 7832471Swnj sc->sc_nxrec = a + 1; 7841919Swnj } 7851919Swnj 7862608Swnj tmreset(uban) 7872608Swnj int uban; 7882608Swnj { 7892982Swnj register struct uba_ctlr *um; 7903095Swnj register tm11, teunit; 7912982Swnj register struct uba_device *ui; 7922608Swnj register struct buf *dp; 7932608Swnj 7942630Swnj for (tm11 = 0; tm11 < NTM; tm11++) { 7952630Swnj if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 || 7962608Swnj um->um_ubanum != uban) 7972608Swnj continue; 7982928Swnj printf(" tm%d", tm11); 7992608Swnj um->um_tab.b_active = 0; 8002608Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 8012608Swnj if (um->um_ubinfo) { 8022608Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 8032617Swnj ubadone(um); 8042608Swnj } 8052608Swnj ((struct device *)(um->um_addr))->tmcs = TM_DCLR; 8063095Swnj for (teunit = 0; teunit < NTE; teunit++) { 8073095Swnj if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um || 8083095Swnj ui->ui_alive == 0) 8092608Swnj continue; 8103095Swnj dp = &teutab[teunit]; 8112608Swnj dp->b_active = 0; 8122608Swnj dp->b_forw = 0; 8132608Swnj if (um->um_tab.b_actf == NULL) 8142608Swnj um->um_tab.b_actf = dp; 8152608Swnj else 8162608Swnj um->um_tab.b_actl->b_forw = dp; 8172608Swnj um->um_tab.b_actl = dp; 8183495Sroot if (te_softc[teunit].sc_openf > 0) 8193495Sroot te_softc[teunit].sc_openf = -1; 8202608Swnj } 8212608Swnj tmstart(um); 8222608Swnj } 8232608Swnj } 8242608Swnj 8251919Swnj /*ARGSUSED*/ 8261919Swnj tmioctl(dev, cmd, addr, flag) 8271919Swnj caddr_t addr; 8281919Swnj dev_t dev; 8291919Swnj { 8303095Swnj int teunit = TEUNIT(dev); 8313095Swnj register struct te_softc *sc = &te_softc[teunit]; 8323095Swnj register struct buf *bp = &ctmbuf[TMUNIT(dev)]; 8331919Swnj register callcount; 8341919Swnj int fcount; 8351919Swnj struct mtop mtop; 8361919Swnj struct mtget mtget; 8371919Swnj /* we depend of the values and order of the MT codes here */ 8382608Swnj static tmops[] = 8392608Swnj {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE}; 8401919Swnj 8412608Swnj switch (cmd) { 8421919Swnj case MTIOCTOP: /* tape operation */ 8431919Swnj if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) { 8441919Swnj u.u_error = EFAULT; 8451919Swnj return; 8461919Swnj } 8471919Swnj switch(mtop.mt_op) { 8482608Swnj case MTWEOF: 8491919Swnj callcount = mtop.mt_count; 8502608Swnj fcount = 1; 8512608Swnj break; 8522608Swnj case MTFSF: case MTBSF: 8532608Swnj callcount = mtop.mt_count; 8541919Swnj fcount = INF; 8551919Swnj break; 8561919Swnj case MTFSR: case MTBSR: 8571919Swnj callcount = 1; 8581919Swnj fcount = mtop.mt_count; 8591919Swnj break; 8602324Skre case MTREW: case MTOFFL: case MTNOP: 8611919Swnj callcount = 1; 8621919Swnj fcount = 1; 8631919Swnj break; 8641919Swnj default: 8651919Swnj u.u_error = ENXIO; 8661919Swnj return; 8671919Swnj } 8682608Swnj if (callcount <= 0 || fcount <= 0) { 8691919Swnj u.u_error = ENXIO; 8702608Swnj return; 8712608Swnj } 8722608Swnj while (--callcount >= 0) { 8732574Swnj tmcommand(dev, tmops[mtop.mt_op], fcount); 8741919Swnj if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) && 8752608Swnj bp->b_resid) { 8761919Swnj u.u_error = EIO; 8771919Swnj break; 8781919Swnj } 8793095Swnj if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT) 8801919Swnj break; 8811919Swnj } 8822608Swnj geterror(bp); 8831919Swnj return; 8841919Swnj case MTIOCGET: 8852471Swnj mtget.mt_dsreg = sc->sc_dsreg; 8862471Swnj mtget.mt_erreg = sc->sc_erreg; 8872471Swnj mtget.mt_resid = sc->sc_resid; 8883482Sroot mtget.mt_type = MT_ISTM; 8891919Swnj if (copyout((caddr_t)&mtget, addr, sizeof(mtget))) 8901919Swnj u.u_error = EFAULT; 8911919Swnj return; 8921919Swnj default: 8931919Swnj u.u_error = ENXIO; 8941919Swnj } 8951919Swnj } 8961919Swnj 8971919Swnj #define DBSIZE 20 8981919Swnj 8992363Swnj tmdump() 9002363Swnj { 9012982Swnj register struct uba_device *ui; 9022396Swnj register struct uba_regs *up; 9032396Swnj register struct device *addr; 9042426Skre int blk, num; 9052426Skre int start; 9061919Swnj 9072426Skre start = 0; 9082426Skre num = maxfree; 9092426Skre #define phys(a,b) ((b)((int)(a)&0x7fffffff)) 9103095Swnj if (tedinfo[0] == 0) 9112887Swnj return (ENXIO); 9123095Swnj ui = phys(tedinfo[0], struct uba_device *); 9132396Swnj up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba; 9143331Swnj ubainit(up); 9152324Skre DELAY(1000000); 9162396Swnj addr = (struct device *)ui->ui_physaddr; 9172396Swnj tmwait(addr); 9182608Swnj addr->tmcs = TM_DCLR | TM_GO; 9191919Swnj while (num > 0) { 9201919Swnj blk = num > DBSIZE ? DBSIZE : num; 9212396Swnj tmdwrite(start, blk, addr, up); 9221919Swnj start += blk; 9231919Swnj num -= blk; 9241919Swnj } 9252426Skre tmeof(addr); 9262426Skre tmeof(addr); 9272426Skre tmwait(addr); 9282887Swnj if (addr->tmcs&TM_ERR) 9292887Swnj return (EIO); 9302608Swnj addr->tmcs = TM_REW | TM_GO; 9312471Swnj tmwait(addr); 9322363Swnj return (0); 9331919Swnj } 9341919Swnj 9352608Swnj tmdwrite(dbuf, num, addr, up) 9362608Swnj register dbuf, num; 9372396Swnj register struct device *addr; 9382396Swnj struct uba_regs *up; 9391919Swnj { 9402396Swnj register struct pte *io; 9412396Swnj register int npf; 9421928Swnj 9432396Swnj tmwait(addr); 9442396Swnj io = up->uba_map; 9451919Swnj npf = num+1; 9461928Swnj while (--npf != 0) 9472982Swnj *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV); 9482396Swnj *(int *)io = 0; 9492396Swnj addr->tmbc = -(num*NBPG); 9502396Swnj addr->tmba = 0; 9512608Swnj addr->tmcs = TM_WCOM | TM_GO; 9521919Swnj } 9531919Swnj 9542396Swnj tmwait(addr) 9552396Swnj register struct device *addr; 9561919Swnj { 9571928Swnj register s; 9581919Swnj 9591919Swnj do 9602396Swnj s = addr->tmcs; 9612608Swnj while ((s & TM_CUR) == 0); 9621919Swnj } 9631919Swnj 9642396Swnj tmeof(addr) 9652396Swnj struct device *addr; 9661919Swnj { 9671919Swnj 9682396Swnj tmwait(addr); 9692608Swnj addr->tmcs = TM_WEOF | TM_GO; 9701919Swnj } 9711919Swnj #endif 972