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