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