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