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