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