Change to includes. No more ../h
[unix-history] / usr / src / sys / vax / uba / tm.c
CommitLineData
ff360633 1/* tm.c 6.4 84/08/29 */
d75e5c3e 2
443c8066 3#include "te.h"
23e0e9b8 4#include "ts.h"
2ac8d080 5#if NTE > 0
d75e5c3e 6/*
afb907f2 7 * TM11/TE10 tape driver
7e00c42b 8 *
d565635a
BJ
9 * TODO:
10 * test driver with more than one slave
11 * test driver with more than one controller
12 * test reset code
d565635a
BJ
13 * what happens if you offline tape during rewind?
14 * test using file system on tape
d75e5c3e 15 */
961945a8
SL
16#include "../machine/pte.h"
17
ff360633
JB
18#include "param.h"
19#include "systm.h"
20#include "buf.h"
21#include "dir.h"
22#include "conf.h"
23#include "user.h"
24#include "file.h"
25#include "map.h"
26#include "vm.h"
27#include "ioctl.h"
28#include "mtio.h"
29#include "cmap.h"
30#include "uio.h"
31#include "kernel.h"
d75e5c3e 32
896962b1 33#include "../vax/cpu.h"
ff360633
JB
34#include "ubareg.h"
35#include "ubavar.h"
36#include "tmreg.h"
d75e5c3e 37
d565635a
BJ
38/*
39 * There is a ctmbuf per tape controller.
40 * It is used as the token to pass to the internal routines
41 * to execute tape ioctls, and also acts as a lock on the slaves
42 * on the controller, since there is only one per controller.
43 * In particular, when the tape is rewinding on close we release
44 * the user process but any further attempts to use the tape drive
45 * before the rewind completes will hang waiting for ctmbuf.
46 */
47struct buf ctmbuf[NTM];
48
49/*
50 * Raw tape operations use rtmbuf. The driver
51 * notices when rtmbuf is being used and allows the user
52 * program to continue after errors and read records
53 * not of the standard length (BSIZE).
54 */
55struct buf rtmbuf[NTM];
d75e5c3e 56
d565635a
BJ
57/*
58 * Driver unibus interface routines and variables.
59 */
71236e46 60int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
89bd2f01 61struct uba_ctlr *tmminfo[NTM];
d565635a
BJ
62struct uba_device *tedinfo[NTE];
63struct buf teutab[NTE];
64short tetotm[NTE];
d763a2b7 65u_short tmstd[] = { 0772520, 0 };
3f3a34c3 66struct uba_driver tmdriver =
d565635a 67 { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
d75e5c3e
BJ
68
69/* bits in minor device */
d565635a
BJ
70#define TEUNIT(dev) (minor(dev)&03)
71#define TMUNIT(dev) (tetotm[TEUNIT(dev)])
d75e5c3e
BJ
72#define T_NOREWIND 04
73#define T_1600BPI 08
74
75#define INF (daddr_t)1000000L
76
71236e46
BJ
77/*
78 * Software state per tape transport.
d565635a
BJ
79 *
80 * 1. A tape drive is a unique-open device; we refuse opens when it is already.
81 * 2. We keep track of the current position on a block tape and seek
82 * before operations by forward/back spacing if necessary.
83 * 3. We remember if the last operation was a write on a tape, so if a tape
84 * is open read write and the last thing done is a write we can
85 * write a standard end of tape mark (two eofs).
86 * 4. We remember the status registers after the last command, using
87 * then internally and returning them to the SENSE ioctl.
88 * 5. We remember the last density the tape was used at. If it is
89 * not a BOT when we start using it and we are writing, we don't
90 * let the density be changed.
71236e46 91 */
d565635a 92struct te_softc {
71236e46
BJ
93 char sc_openf; /* lock against multiple opens */
94 char sc_lastiow; /* last op was a write */
95 daddr_t sc_blkno; /* block number, for block device tape */
d565635a 96 daddr_t sc_nxrec; /* position of end of tape, if known */
71236e46
BJ
97 u_short sc_erreg; /* copy of last erreg */
98 u_short sc_dsreg; /* copy of last dsreg */
99 short sc_resid; /* copy of last bc */
a0eab615 100#ifdef unneeded
d1330646 101 short sc_lastcmd; /* last command to handle direction changes */
80905075 102#endif
d565635a 103 u_short sc_dens; /* prototype command with density info */
a752fee0
BJ
104 daddr_t sc_timo; /* time until timeout expires */
105 short sc_tact; /* timeout is active */
04b2deaa 106} te_softc[NTE];
a0eab615
BJ
107#ifdef unneeded
108int tmgapsdcnt; /* DEBUG */
109#endif
d75e5c3e 110
71236e46 111/*
d565635a
BJ
112 * States for um->um_tab.b_active, the per controller state flag.
113 * This is used to sequence control in the driver.
71236e46 114 */
d75e5c3e
BJ
115#define SSEEK 1 /* seeking */
116#define SIO 2 /* doing seq i/o */
117#define SCOM 3 /* sending control command */
71236e46 118#define SREW 4 /* sending a drive rewind */
d75e5c3e 119
5aa9d5ea
RE
120/*
121 * Determine if there is a controller for
122 * a tm at address reg. Our goal is to make the
123 * device interrupt.
5aa9d5ea 124 */
71236e46 125tmprobe(reg)
3f3a34c3
BJ
126 caddr_t reg;
127{
d565635a 128 register int br, cvec; /* must be r11,r10; value-result */
5aa9d5ea 129
71236e46 130#ifdef lint
a0eab615 131 br = 0; cvec = br; br = cvec;
89b8a44c 132 tmintr(0);
71236e46 133#endif
a870f835 134 ((struct tmdevice *)reg)->tmcs = TM_IE;
3f3a34c3 135 /*
afb907f2 136 * If this is a tm11, it ought to have interrupted
3f3a34c3 137 * by now, if it isn't (ie: it is a ts04) then we just
d763a2b7
BJ
138 * hope that it didn't interrupt, so autoconf will ignore it.
139 * Just in case, we will reference one
3f3a34c3 140 * of the more distant registers, and hope for a machine
d763a2b7 141 * check, or similar disaster if this is a ts.
7e00c42b
BJ
142 *
143 * Note: on an 11/780, badaddr will just generate
144 * a uba error for a ts; but our caller will notice that
145 * so we won't check for it.
3f3a34c3 146 */
a870f835 147 if (badaddr((caddr_t)&((struct tmdevice *)reg)->tmrd, 2))
d763a2b7 148 return (0);
9c0adba0 149 return (sizeof (struct tmdevice));
3f3a34c3
BJ
150}
151
71236e46
BJ
152/*
153 * Due to a design flaw, we cannot ascertain if the tape
154 * exists or not unless it is on line - ie: unless a tape is
155 * mounted. This is too servere a restriction to bear,
156 * so all units are assumed to exist.
157 */
158/*ARGSUSED*/
e4271c68 159tmslave(ui, reg)
89bd2f01 160 struct uba_device *ui;
3f3a34c3
BJ
161 caddr_t reg;
162{
d763a2b7 163
d763a2b7 164 return (1);
3f3a34c3
BJ
165}
166
71236e46 167/*
d565635a 168 * Record attachment of the unit to the controller.
71236e46
BJ
169 */
170/*ARGSUSED*/
171tmattach(ui)
89bd2f01 172 struct uba_device *ui;
71236e46 173{
d565635a
BJ
174 /*
175 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
176 * arrays given a te unit number.
177 */
178 tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
71236e46
BJ
179}
180
a752fee0 181int tmtimer();
71236e46
BJ
182/*
183 * Open the device. Tapes are unique open
184 * devices, so we refuse if it is already open.
185 * We also check that a tape is available, and
d565635a
BJ
186 * don't block waiting here; if you want to wait
187 * for a tape you should timeout in user code.
71236e46 188 */
d75e5c3e
BJ
189tmopen(dev, flag)
190 dev_t dev;
191 int flag;
192{
d565635a 193 register int teunit;
89bd2f01 194 register struct uba_device *ui;
d565635a 195 register struct te_softc *sc;
971424d9 196 int olddens, dens;
2311123d 197 int s;
d75e5c3e 198
d565635a
BJ
199 teunit = TEUNIT(dev);
200 if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
7da157da
BJ
201 (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0)
202 return (ENXIO);
971424d9
BJ
203 olddens = sc->sc_dens;
204 dens = TM_IE | TM_GO | (ui->ui_slave << 8);
205 if ((minor(dev) & T_1600BPI) == 0)
206 dens |= TM_D800;
207 sc->sc_dens = dens;
27a897a2 208get:
71236e46 209 tmcommand(dev, TM_SENSE, 1);
27a897a2
BJ
210 if (sc->sc_erreg&TMER_SDWN) {
211 sleep((caddr_t)&lbolt, PZERO+1);
212 goto get;
213 }
971424d9 214 sc->sc_dens = olddens;
a1706887
BJ
215 if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR)) {
216 uprintf("te%d: not online\n", teunit);
7da157da 217 return (EIO);
a1706887
BJ
218 }
219 if ((flag&FWRITE) && (sc->sc_erreg&TMER_WRL)) {
220 uprintf("te%d: no write ring\n", teunit);
7da157da 221 return (EIO);
a1706887
BJ
222 }
223 if ((sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
224 dens != sc->sc_dens) {
225 uprintf("te%d: can't change density in mid-tape\n", teunit);
7da157da 226 return (EIO);
d75e5c3e 227 }
71236e46 228 sc->sc_openf = 1;
7e00c42b
BJ
229 sc->sc_blkno = (daddr_t)0;
230 sc->sc_nxrec = INF;
71236e46 231 sc->sc_lastiow = 0;
d565635a 232 sc->sc_dens = dens;
2311123d 233 s = spl6();
a752fee0
BJ
234 if (sc->sc_tact == 0) {
235 sc->sc_timo = INF;
236 sc->sc_tact = 1;
c27ba986 237 timeout(tmtimer, (caddr_t)dev, 5*hz);
a752fee0 238 }
2311123d 239 splx(s);
7da157da 240 return (0);
d75e5c3e
BJ
241}
242
71236e46
BJ
243/*
244 * Close tape device.
245 *
246 * If tape was open for writing or last operation was
247 * a write, then write two EOF's and backspace over the last one.
248 * Unless this is a non-rewinding special file, rewind the tape.
249 * Make the tape available to others.
250 */
d75e5c3e
BJ
251tmclose(dev, flag)
252 register dev_t dev;
253 register flag;
254{
d565635a 255 register struct te_softc *sc = &te_softc[TEUNIT(dev)];
d75e5c3e 256
71236e46
BJ
257 if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
258 tmcommand(dev, TM_WEOF, 1);
259 tmcommand(dev, TM_WEOF, 1);
260 tmcommand(dev, TM_SREV, 1);
d75e5c3e
BJ
261 }
262 if ((minor(dev)&T_NOREWIND) == 0)
d565635a
BJ
263 /*
264 * 0 count means don't hang waiting for rewind complete
265 * rather ctmbuf stays busy until the operation completes
266 * preventing further opens from completing by
267 * preventing a TM_SENSE from completing.
268 */
269 tmcommand(dev, TM_REW, 0);
7e00c42b 270 sc->sc_openf = 0;
d75e5c3e
BJ
271}
272
71236e46
BJ
273/*
274 * Execute a command on the tape drive
275 * a specified number of times.
276 */
e4271c68 277tmcommand(dev, com, count)
d75e5c3e
BJ
278 dev_t dev;
279 int com, count;
280{
281 register struct buf *bp;
2311123d 282 register int s;
d75e5c3e 283
71236e46 284 bp = &ctmbuf[TMUNIT(dev)];
2311123d 285 s = spl5();
d75e5c3e 286 while (bp->b_flags&B_BUSY) {
d565635a
BJ
287 /*
288 * This special check is because B_BUSY never
289 * gets cleared in the non-waiting rewind case.
290 */
27a897a2 291 if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
d565635a 292 break;
d75e5c3e
BJ
293 bp->b_flags |= B_WANTED;
294 sleep((caddr_t)bp, PRIBIO);
295 }
296 bp->b_flags = B_BUSY|B_READ;
2311123d 297 splx(s);
d75e5c3e
BJ
298 bp->b_dev = dev;
299 bp->b_repcnt = -count;
300 bp->b_command = com;
301 bp->b_blkno = 0;
302 tmstrategy(bp);
d565635a
BJ
303 /*
304 * In case of rewind from close, don't wait.
305 * This is the only case where count can be 0.
306 */
307 if (count == 0)
308 return;
d75e5c3e
BJ
309 iowait(bp);
310 if (bp->b_flags&B_WANTED)
311 wakeup((caddr_t)bp);
312 bp->b_flags &= B_ERROR;
313}
314
71236e46 315/*
d565635a 316 * Queue a tape operation.
71236e46 317 */
d75e5c3e
BJ
318tmstrategy(bp)
319 register struct buf *bp;
320{
d565635a 321 int teunit = TEUNIT(bp->b_dev);
89bd2f01 322 register struct uba_ctlr *um;
71236e46 323 register struct buf *dp;
2311123d 324 int s;
d75e5c3e 325
71236e46
BJ
326 /*
327 * Put transfer at end of unit queue
328 */
d565635a 329 dp = &teutab[teunit];
d75e5c3e 330 bp->av_forw = NULL;
2311123d 331 s = spl5();
06e2c4fb 332 um = tedinfo[teunit]->ui_mi;
71236e46
BJ
333 if (dp->b_actf == NULL) {
334 dp->b_actf = bp;
335 /*
336 * Transport not already active...
337 * put at end of controller queue.
338 */
339 dp->b_forw = NULL;
71236e46
BJ
340 if (um->um_tab.b_actf == NULL)
341 um->um_tab.b_actf = dp;
342 else
343 um->um_tab.b_actl->b_forw = dp;
344 um->um_tab.b_actl = dp;
345 } else
346 dp->b_actl->av_forw = bp;
347 dp->b_actl = bp;
348 /*
349 * If the controller is not busy, get
350 * it going.
351 */
352 if (um->um_tab.b_active == 0)
353 tmstart(um);
2311123d 354 splx(s);
d75e5c3e
BJ
355}
356
71236e46
BJ
357/*
358 * Start activity on a tm controller.
359 */
360tmstart(um)
89bd2f01 361 register struct uba_ctlr *um;
d75e5c3e 362{
71236e46 363 register struct buf *bp, *dp;
a870f835 364 register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
d565635a 365 register struct te_softc *sc;
89bd2f01 366 register struct uba_device *ui;
d565635a 367 int teunit, cmd;
7e00c42b 368 daddr_t blkno;
d75e5c3e 369
71236e46
BJ
370 /*
371 * Look for an idle transport on the controller.
372 */
d75e5c3e 373loop:
71236e46 374 if ((dp = um->um_tab.b_actf) == NULL)
d75e5c3e 375 return;
71236e46
BJ
376 if ((bp = dp->b_actf) == NULL) {
377 um->um_tab.b_actf = dp->b_forw;
378 goto loop;
379 }
d565635a
BJ
380 teunit = TEUNIT(bp->b_dev);
381 ui = tedinfo[teunit];
71236e46
BJ
382 /*
383 * Record pre-transfer status (e.g. for TM_SENSE)
384 */
d565635a 385 sc = &te_softc[teunit];
a870f835 386 addr = (struct tmdevice *)um->um_addr;
71236e46 387 addr->tmcs = (ui->ui_slave << 8);
7e00c42b
BJ
388 sc->sc_dsreg = addr->tmcs;
389 sc->sc_erreg = addr->tmer;
390 sc->sc_resid = addr->tmbc;
71236e46
BJ
391 /*
392 * Default is that last command was NOT a write command;
393 * if we do a write command we will notice this in tmintr().
394 */
bc9da2ca 395 sc->sc_lastiow = 0;
71236e46
BJ
396 if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
397 /*
d565635a
BJ
398 * Have had a hard error on a non-raw tape
399 * or the tape unit is now unavailable
400 * (e.g. taken off line).
71236e46
BJ
401 */
402 bp->b_flags |= B_ERROR;
d75e5c3e
BJ
403 goto next;
404 }
d565635a
BJ
405 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
406 /*
407 * Execute control operation with the specified count.
408 */
71236e46
BJ
409 if (bp->b_command == TM_SENSE)
410 goto next;
a752fee0
BJ
411 /*
412 * Set next state; give 5 minutes to complete
413 * rewind, or 10 seconds per iteration (minimum 60
c27ba986 414 * seconds and max 5 minutes) to complete other ops.
a752fee0
BJ
415 */
416 if (bp->b_command == TM_REW) {
417 um->um_tab.b_active = SREW;
418 sc->sc_timo = 5 * 60;
419 } else {
420 um->um_tab.b_active = SCOM;
0218811c
BJ
421 sc->sc_timo =
422 imin(imax(10*(int)-bp->b_repcnt,60),5*60);
a752fee0 423 }
71236e46
BJ
424 if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
425 addr->tmbc = bp->b_repcnt;
d1330646 426 goto dobpcmd;
71236e46 427 }
d565635a
BJ
428 /*
429 * The following checks handle boundary cases for operation
430 * on non-raw tapes. On raw tapes the initialization of
431 * sc->sc_nxrec by tmphys causes them to be skipped normally
432 * (except in the case of retries).
433 */
fb739082 434 if (bdbtofsb(bp->b_blkno) > sc->sc_nxrec) {
d565635a
BJ
435 /*
436 * Can't read past known end-of-file.
437 */
438 bp->b_flags |= B_ERROR;
439 bp->b_error = ENXIO;
440 goto next;
441 }
fb739082 442 if (bdbtofsb(bp->b_blkno) == sc->sc_nxrec &&
d565635a
BJ
443 bp->b_flags&B_READ) {
444 /*
445 * Reading at end of file returns 0 bytes.
446 */
447 bp->b_resid = bp->b_bcount;
448 clrbuf(bp);
449 goto next;
450 }
451 if ((bp->b_flags&B_READ) == 0)
452 /*
453 * Writing sets EOF
454 */
fb739082 455 sc->sc_nxrec = bdbtofsb(bp->b_blkno) + 1;
71236e46
BJ
456 /*
457 * If the data transfer command is in the correct place,
458 * set up all the registers except the csr, and give
459 * control over to the UNIBUS adapter routines, to
460 * wait for resources to start the i/o.
461 */
fb739082 462 if ((blkno = sc->sc_blkno) == bdbtofsb(bp->b_blkno)) {
3f3a34c3 463 addr->tmbc = -bp->b_bcount;
d75e5c3e 464 if ((bp->b_flags&B_READ) == 0) {
7e00c42b 465 if (um->um_tab.b_errcnt)
d565635a 466 cmd = TM_WIRG;
d75e5c3e 467 else
d565635a 468 cmd = TM_WCOM;
d75e5c3e 469 } else
d565635a 470 cmd = TM_RCOM;
7e00c42b 471 um->um_tab.b_active = SIO;
d565635a 472 um->um_cmd = sc->sc_dens|cmd;
80905075 473#ifdef notdef
d1330646 474 if (tmreverseop(sc->sc_lastcmd))
d565635a 475 while (addr->tmer & TMER_SDWN)
d1330646 476 tmgapsdcnt++;
d1330646 477 sc->sc_lastcmd = TM_RCOM; /* will serve */
80905075 478#endif
a752fee0 479 sc->sc_timo = 60; /* premature, but should serve */
a0eab615 480 (void) ubago(ui);
d75e5c3e
BJ
481 return;
482 }
71236e46 483 /*
d565635a
BJ
484 * Tape positioned incorrectly;
485 * set to seek forwards or backwards to the correct spot.
486 * This happens for raw tapes only on error retries.
71236e46 487 */
7e00c42b 488 um->um_tab.b_active = SSEEK;
fb739082 489 if (blkno < bdbtofsb(bp->b_blkno)) {
d1330646 490 bp->b_command = TM_SFORW;
fb739082 491 addr->tmbc = blkno - bdbtofsb(bp->b_blkno);
d75e5c3e 492 } else {
d1330646 493 bp->b_command = TM_SREV;
fb739082 494 addr->tmbc = bdbtofsb(bp->b_blkno) - blkno;
d75e5c3e 495 }
c27ba986 496 sc->sc_timo = imin(imax(10 * -addr->tmbc, 60), 5 * 60);
d1330646 497dobpcmd:
80905075 498#ifdef notdef
d565635a
BJ
499 /*
500 * It is strictly necessary to wait for the tape
501 * to stop before changing directions, but the TC11
502 * handles this for us.
503 */
d1330646
BJ
504 if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
505 while (addr->tmer & TM_SDWN)
506 tmgapsdcnt++;
d1330646 507 sc->sc_lastcmd = bp->b_command;
80905075 508#endif
d565635a
BJ
509 /*
510 * Do the command in bp.
511 */
512 addr->tmcs = (sc->sc_dens | bp->b_command);
d75e5c3e
BJ
513 return;
514
515next:
71236e46
BJ
516 /*
517 * Done with this operation due to error or
518 * the fact that it doesn't do anything.
519 * Release UBA resources (if any), dequeue
520 * the transfer and continue processing this slave.
521 */
522 if (um->um_ubinfo)
0801d37f 523 ubadone(um);
71236e46
BJ
524 um->um_tab.b_errcnt = 0;
525 dp->b_actf = bp->av_forw;
d75e5c3e
BJ
526 iodone(bp);
527 goto loop;
528}
529
71236e46
BJ
530/*
531 * The UNIBUS resources we needed have been
532 * allocated to us; start the device.
533 */
e4271c68 534tmdgo(um)
89bd2f01 535 register struct uba_ctlr *um;
3f3a34c3 536{
a870f835 537 register struct tmdevice *addr = (struct tmdevice *)um->um_addr;
7e00c42b 538
e4271c68
BJ
539 addr->tmba = um->um_ubinfo;
540 addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
3f3a34c3
BJ
541}
542
71236e46
BJ
543/*
544 * Tm interrupt routine.
545 */
7e00c42b 546/*ARGSUSED*/
afb907f2
BJ
547tmintr(tm11)
548 int tm11;
d75e5c3e 549{
71236e46 550 struct buf *dp;
d75e5c3e 551 register struct buf *bp;
89bd2f01 552 register struct uba_ctlr *um = tmminfo[tm11];
a870f835 553 register struct tmdevice *addr;
d565635a
BJ
554 register struct te_softc *sc;
555 int teunit;
d75e5c3e
BJ
556 register state;
557
d565635a
BJ
558 if ((dp = um->um_tab.b_actf) == NULL)
559 return;
560 bp = dp->b_actf;
561 teunit = TEUNIT(bp->b_dev);
a870f835 562 addr = (struct tmdevice *)tedinfo[teunit]->ui_addr;
6a476a42 563 sc = &te_softc[teunit];
71236e46
BJ
564 /*
565 * If last command was a rewind, and tape is still
566 * rewinding, wait for the rewind complete interrupt.
567 */
568 if (um->um_tab.b_active == SREW) {
569 um->um_tab.b_active = SCOM;
6a476a42
BJ
570 if (addr->tmer&TMER_RWS) {
571 sc->sc_timo = 5*60; /* 5 minutes */
71236e46 572 return;
6a476a42 573 }
d75e5c3e 574 }
71236e46
BJ
575 /*
576 * An operation completed... record status
577 */
a752fee0 578 sc->sc_timo = INF;
7e00c42b
BJ
579 sc->sc_dsreg = addr->tmcs;
580 sc->sc_erreg = addr->tmer;
581 sc->sc_resid = addr->tmbc;
d75e5c3e 582 if ((bp->b_flags & B_READ) == 0)
71236e46 583 sc->sc_lastiow = 1;
7e00c42b
BJ
584 state = um->um_tab.b_active;
585 um->um_tab.b_active = 0;
71236e46
BJ
586 /*
587 * Check for errors.
588 */
589 if (addr->tmcs&TM_ERR) {
d565635a 590 while (addr->tmer & TMER_SDWN)
d75e5c3e 591 ; /* await settle down */
71236e46 592 /*
d565635a 593 * If we hit the end of the tape file, update our position.
71236e46 594 */
d565635a 595 if (addr->tmer&TMER_EOF) {
71236e46
BJ
596 tmseteof(bp); /* set blkno and nxrec */
597 state = SCOM; /* force completion */
598 /*
599 * Stuff bc so it will be unstuffed correctly
600 * later to get resid.
601 */
3f3a34c3 602 addr->tmbc = -bp->b_bcount;
71236e46 603 goto opdone;
d75e5c3e 604 }
71236e46 605 /*
d565635a
BJ
606 * If we were reading raw tape and the only error was that the
607 * record was too long, then we don't consider this an error.
71236e46 608 */
d565635a
BJ
609 if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
610 (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
71236e46
BJ
611 goto ignoreerr;
612 /*
613 * If error is not hard, and this was an i/o operation
614 * retry up to 8 times.
615 */
d565635a 616 if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
7e00c42b 617 if (++um->um_tab.b_errcnt < 7) {
7e00c42b 618 sc->sc_blkno++;
0801d37f 619 ubadone(um);
71236e46 620 goto opcont;
d75e5c3e 621 }
71236e46
BJ
622 } else
623 /*
624 * Hard or non-i/o errors on non-raw tape
625 * cause it to close.
626 */
d565635a 627 if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
628 sc->sc_openf = -1;
629 /*
630 * Couldn't recover error
631 */
80905075 632 printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
d565635a 633 bp->b_blkno, sc->sc_erreg, TMER_BITS);
d75e5c3e 634 bp->b_flags |= B_ERROR;
71236e46 635 goto opdone;
d75e5c3e 636 }
71236e46
BJ
637 /*
638 * Advance tape control FSM.
639 */
640ignoreerr:
d75e5c3e
BJ
641 switch (state) {
642
643 case SIO:
71236e46
BJ
644 /*
645 * Read/write increments tape block number
646 */
7e00c42b 647 sc->sc_blkno++;
71236e46 648 goto opdone;
d75e5c3e
BJ
649
650 case SCOM:
71236e46 651 /*
d565635a 652 * For forward/backward space record update current position.
71236e46 653 */
d565635a 654 if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
655 switch (bp->b_command) {
656
657 case TM_SFORW:
658 sc->sc_blkno -= bp->b_repcnt;
d565635a 659 break;
71236e46
BJ
660
661 case TM_SREV:
662 sc->sc_blkno += bp->b_repcnt;
d565635a 663 break;
d75e5c3e 664 }
d565635a 665 goto opdone;
d75e5c3e
BJ
666
667 case SSEEK:
fb739082 668 sc->sc_blkno = bdbtofsb(bp->b_blkno);
71236e46 669 goto opcont;
d75e5c3e
BJ
670
671 default:
71236e46 672 panic("tmintr");
d75e5c3e 673 }
71236e46
BJ
674opdone:
675 /*
676 * Reset error count and remove
677 * from device queue.
678 */
679 um->um_tab.b_errcnt = 0;
680 dp->b_actf = bp->av_forw;
c620085d
MK
681 /*
682 * Check resid; watch out for resid >32767 (tmbc not negative).
683 */
74e6ae07 684 bp->b_resid = ((int) -addr->tmbc) & 0xffff;
0801d37f 685 ubadone(um);
71236e46
BJ
686 iodone(bp);
687 /*
688 * Circulate slave to end of controller
689 * queue to give other slaves a chance.
690 */
691 um->um_tab.b_actf = dp->b_forw;
692 if (dp->b_actf) {
693 dp->b_forw = NULL;
694 if (um->um_tab.b_actf == NULL)
695 um->um_tab.b_actf = dp;
696 else
697 um->um_tab.b_actl->b_forw = dp;
698 um->um_tab.b_actl = dp;
699 }
700 if (um->um_tab.b_actf == 0)
701 return;
702opcont:
703 tmstart(um);
d75e5c3e
BJ
704}
705
a752fee0
BJ
706tmtimer(dev)
707 int dev;
708{
709 register struct te_softc *sc = &te_softc[TEUNIT(dev)];
afa0df56 710 register short x;
a752fee0
BJ
711
712 if (sc->sc_timo != INF && (sc->sc_timo -= 5) < 0) {
aa4e15a3 713 printf("te%d: lost interrupt\n", TEUNIT(dev));
a752fee0 714 sc->sc_timo = INF;
afa0df56 715 x = spl5();
a752fee0 716 tmintr(TMUNIT(dev));
afa0df56 717 (void) splx(x);
a752fee0 718 }
c27ba986 719 timeout(tmtimer, (caddr_t)dev, 5*hz);
a752fee0
BJ
720}
721
d75e5c3e
BJ
722tmseteof(bp)
723 register struct buf *bp;
724{
d565635a 725 register int teunit = TEUNIT(bp->b_dev);
a870f835
BJ
726 register struct tmdevice *addr =
727 (struct tmdevice *)tedinfo[teunit]->ui_addr;
d565635a 728 register struct te_softc *sc = &te_softc[teunit];
d75e5c3e 729
d565635a 730 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
fb739082 731 if (sc->sc_blkno > bdbtofsb(bp->b_blkno)) {
d75e5c3e 732 /* reversing */
fb739082 733 sc->sc_nxrec = bdbtofsb(bp->b_blkno) - addr->tmbc;
7e00c42b 734 sc->sc_blkno = sc->sc_nxrec;
d75e5c3e
BJ
735 } else {
736 /* spacing forward */
fb739082 737 sc->sc_blkno = bdbtofsb(bp->b_blkno) + addr->tmbc;
7e00c42b 738 sc->sc_nxrec = sc->sc_blkno - 1;
d75e5c3e
BJ
739 }
740 return;
741 }
742 /* eof on read */
fb739082 743 sc->sc_nxrec = bdbtofsb(bp->b_blkno);
d75e5c3e
BJ
744}
745
740e4029 746tmread(dev, uio)
71236e46 747 dev_t dev;
740e4029 748 struct uio *uio;
d75e5c3e 749{
0cd5eac7 750 int errno;
d75e5c3e 751
0cd5eac7
BJ
752 errno = tmphys(dev, uio);
753 if (errno)
754 return (errno);
755 return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys, uio));
d75e5c3e
BJ
756}
757
406ddcbe 758tmwrite(dev, uio)
71236e46 759 dev_t dev;
406ddcbe 760 struct uio *uio;
d75e5c3e 761{
0cd5eac7 762 int errno;
d75e5c3e 763
0cd5eac7
BJ
764 errno = tmphys(dev, uio);
765 if (errno)
766 return (errno);
767 return (physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys, uio));
d75e5c3e
BJ
768}
769
d565635a
BJ
770/*
771 * Check that a raw device exists.
772 * If it does, set up sc_blkno and sc_nxrec
773 * so that the tape will appear positioned correctly.
774 */
740e4029 775tmphys(dev, uio)
71236e46 776 dev_t dev;
740e4029 777 struct uio *uio;
d75e5c3e 778{
d565635a 779 register int teunit = TEUNIT(dev);
d75e5c3e 780 register daddr_t a;
d565635a
BJ
781 register struct te_softc *sc;
782 register struct uba_device *ui;
d75e5c3e 783
406ddcbe 784 if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0)
740e4029 785 return (ENXIO);
d565635a 786 sc = &te_softc[teunit];
406ddcbe 787 a = bdbtofsb(uio->uio_offset >> 9);
7e00c42b
BJ
788 sc->sc_blkno = a;
789 sc->sc_nxrec = a + 1;
740e4029 790 return (0);
d75e5c3e
BJ
791}
792
71236e46
BJ
793tmreset(uban)
794 int uban;
795{
89bd2f01 796 register struct uba_ctlr *um;
d565635a 797 register tm11, teunit;
89bd2f01 798 register struct uba_device *ui;
71236e46
BJ
799 register struct buf *dp;
800
afb907f2
BJ
801 for (tm11 = 0; tm11 < NTM; tm11++) {
802 if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
71236e46
BJ
803 um->um_ubanum != uban)
804 continue;
80905075 805 printf(" tm%d", tm11);
71236e46
BJ
806 um->um_tab.b_active = 0;
807 um->um_tab.b_actf = um->um_tab.b_actl = 0;
808 if (um->um_ubinfo) {
809 printf("<%d>", (um->um_ubinfo>>28)&0xf);
96b997eb 810 um->um_ubinfo = 0;
71236e46 811 }
a870f835 812 ((struct tmdevice *)(um->um_addr))->tmcs = TM_DCLR;
d565635a
BJ
813 for (teunit = 0; teunit < NTE; teunit++) {
814 if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
815 ui->ui_alive == 0)
71236e46 816 continue;
d565635a 817 dp = &teutab[teunit];
71236e46
BJ
818 dp->b_active = 0;
819 dp->b_forw = 0;
820 if (um->um_tab.b_actf == NULL)
821 um->um_tab.b_actf = dp;
822 else
823 um->um_tab.b_actl->b_forw = dp;
824 um->um_tab.b_actl = dp;
a752fee0
BJ
825 if (te_softc[teunit].sc_openf > 0)
826 te_softc[teunit].sc_openf = -1;
71236e46
BJ
827 }
828 tmstart(um);
829 }
830}
831
d75e5c3e 832/*ARGSUSED*/
942f05a9
SL
833tmioctl(dev, cmd, data, flag)
834 caddr_t data;
d75e5c3e
BJ
835 dev_t dev;
836{
d565635a
BJ
837 int teunit = TEUNIT(dev);
838 register struct te_softc *sc = &te_softc[teunit];
839 register struct buf *bp = &ctmbuf[TMUNIT(dev)];
d75e5c3e
BJ
840 register callcount;
841 int fcount;
942f05a9
SL
842 struct mtop *mtop;
843 struct mtget *mtget;
d75e5c3e 844 /* we depend of the values and order of the MT codes here */
71236e46
BJ
845 static tmops[] =
846 {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
d75e5c3e 847
71236e46 848 switch (cmd) {
942f05a9
SL
849
850 case MTIOCTOP: /* tape operation */
851 mtop = (struct mtop *)data;
7da157da 852 switch (mtop->mt_op) {
942f05a9 853
71236e46 854 case MTWEOF:
942f05a9 855 callcount = mtop->mt_count;
71236e46
BJ
856 fcount = 1;
857 break;
942f05a9 858
71236e46 859 case MTFSF: case MTBSF:
942f05a9 860 callcount = mtop->mt_count;
d75e5c3e
BJ
861 fcount = INF;
862 break;
942f05a9 863
d75e5c3e
BJ
864 case MTFSR: case MTBSR:
865 callcount = 1;
942f05a9 866 fcount = mtop->mt_count;
d75e5c3e 867 break;
942f05a9 868
77115c00 869 case MTREW: case MTOFFL: case MTNOP:
d75e5c3e
BJ
870 callcount = 1;
871 fcount = 1;
872 break;
942f05a9 873
d75e5c3e 874 default:
7da157da 875 return (ENXIO);
71236e46 876 }
7da157da
BJ
877 if (callcount <= 0 || fcount <= 0)
878 return (EINVAL);
71236e46 879 while (--callcount >= 0) {
942f05a9
SL
880 tmcommand(dev, tmops[mtop->mt_op], fcount);
881 if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
7da157da
BJ
882 bp->b_resid)
883 return (EIO);
d565635a 884 if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
d75e5c3e
BJ
885 break;
886 }
a1edc12b 887 return (geterror(bp));
942f05a9 888
d75e5c3e 889 case MTIOCGET:
942f05a9
SL
890 mtget = (struct mtget *)data;
891 mtget->mt_dsreg = sc->sc_dsreg;
892 mtget->mt_erreg = sc->sc_erreg;
893 mtget->mt_resid = sc->sc_resid;
894 mtget->mt_type = MT_ISTM;
7da157da 895 break;
942f05a9 896
d75e5c3e 897 default:
7da157da 898 return (ENXIO);
d75e5c3e 899 }
7da157da 900 return (0);
d75e5c3e
BJ
901}
902
903#define DBSIZE 20
904
7455bf3e 905tmdump()
d75e5c3e 906{
89bd2f01 907 register struct uba_device *ui;
3f3a34c3 908 register struct uba_regs *up;
a870f835 909 register struct tmdevice *addr;
5aa9d5ea
RE
910 int blk, num;
911 int start;
d75e5c3e 912
5aa9d5ea
RE
913 start = 0;
914 num = maxfree;
915#define phys(a,b) ((b)((int)(a)&0x7fffffff))
d565635a 916 if (tedinfo[0] == 0)
2536c16c 917 return (ENXIO);
d565635a 918 ui = phys(tedinfo[0], struct uba_device *);
3f3a34c3 919 up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
57229699 920 ubainit(up);
77115c00 921 DELAY(1000000);
a870f835 922 addr = (struct tmdevice *)ui->ui_physaddr;
3f3a34c3 923 tmwait(addr);
71236e46 924 addr->tmcs = TM_DCLR | TM_GO;
d75e5c3e
BJ
925 while (num > 0) {
926 blk = num > DBSIZE ? DBSIZE : num;
3f3a34c3 927 tmdwrite(start, blk, addr, up);
d75e5c3e
BJ
928 start += blk;
929 num -= blk;
930 }
5aa9d5ea
RE
931 tmeof(addr);
932 tmeof(addr);
7e00c42b 933 tmwait(addr);
2536c16c
BJ
934 if (addr->tmcs&TM_ERR)
935 return (EIO);
71236e46 936 addr->tmcs = TM_REW | TM_GO;
5aa9d5ea 937 tmwait(addr);
7455bf3e 938 return (0);
d75e5c3e
BJ
939}
940
71236e46
BJ
941tmdwrite(dbuf, num, addr, up)
942 register dbuf, num;
a870f835 943 register struct tmdevice *addr;
3f3a34c3 944 struct uba_regs *up;
d75e5c3e 945{
3f3a34c3
BJ
946 register struct pte *io;
947 register int npf;
0a51498e 948
3f3a34c3 949 tmwait(addr);
3f3a34c3 950 io = up->uba_map;
d75e5c3e 951 npf = num+1;
0a51498e 952 while (--npf != 0)
89bd2f01 953 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
3f3a34c3
BJ
954 *(int *)io = 0;
955 addr->tmbc = -(num*NBPG);
956 addr->tmba = 0;
71236e46 957 addr->tmcs = TM_WCOM | TM_GO;
d75e5c3e
BJ
958}
959
3f3a34c3 960tmwait(addr)
a870f835 961 register struct tmdevice *addr;
d75e5c3e 962{
0a51498e 963 register s;
d75e5c3e
BJ
964
965 do
3f3a34c3 966 s = addr->tmcs;
71236e46 967 while ((s & TM_CUR) == 0);
d75e5c3e
BJ
968}
969
3f3a34c3 970tmeof(addr)
a870f835 971 struct tmdevice *addr;
d75e5c3e
BJ
972{
973
3f3a34c3 974 tmwait(addr);
71236e46 975 addr->tmcs = TM_WEOF | TM_GO;
d75e5c3e
BJ
976}
977#endif