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