xref: /csrg-svn/sys/vax/uba/ts.c (revision 3244)
1 /*	ts.c	4.7	81/03/13	*/
2 
3 #include "ts.h"
4 #if NTS > 0
5 /*
6  * TS11 tape driver
7  *
8  * TODO:
9  *	test driver with more than one controller
10  *	test reset code
11  *	test dump code
12  *	test rewinds without hanging in driver
13  *	what happens if you offline tape during rewind?
14  *	test using file system on tape
15  */
16 #include "../h/param.h"
17 #include "../h/systm.h"
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"
25 #include "../h/vm.h"
26 #include "../h/ubareg.h"
27 #include "../h/ubavar.h"
28 #include "../h/mtio.h"
29 #include "../h/ioctl.h"
30 #include "../h/cmap.h"
31 #include "../h/cpu.h"
32 
33 #include "../h/tsreg.h"
34 
35 /*
36  * There is a ctsbuf per tape controller.
37  * It is used as the token to pass to the internal routines
38  * to execute tape ioctls.
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 ctsbuf.
42  */
43 struct	buf	ctsbuf[NTS];
44 
45 /*
46  * Raw tape operations use rtsbuf.  The driver
47  * notices when rtsbuf is being used and allows the user
48  * program to continue after errors and read records
49  * not of the standard length (BSIZE).
50  */
51 struct	buf	rtsbuf[NTS];
52 
53 /*
54  * Driver unibus interface routines and variables.
55  */
56 int	tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
57 struct	uba_ctlr *tsminfo[NTS];
58 struct	uba_device *tsdinfo[NTS];
59 u_short	tsstd[] = { 0772520, 0 };
60 /*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
61 struct	uba_driver zsdriver =
62  { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "ts", tsminfo, 0 };
63 
64 /* bits in minor device */
65 #define	TSUNIT(dev)	(minor(dev)&03)
66 #define	T_NOREWIND	04
67 
68 #define	INF	(daddr_t)1000000L
69 
70 /*
71  * Software state per tape transport.
72  * Also contains hardware state in message packets.
73  *
74  * 1. A tape drive is a unique-open device; we refuse opens when it is already.
75  * 2. We keep track of the current position on a block tape and seek
76  *    before operations by forward/back spacing if necessary.
77  * 3. We remember if the last operation was a write on a tape, so if a tape
78  *    is open read write and the last thing done is a write we can
79  *    write a standard end of tape mark (two eofs).
80  * 4. We remember the status registers after the last command, using
81  *    then internally and returning them to the SENSE ioctl.
82  */
83 struct	ts_softc {
84 	char	sc_openf;	/* lock against multiple opens */
85 	char	sc_lastiow;	/* last op was a write */
86 	daddr_t	sc_blkno;	/* block number, for block device tape */
87 	daddr_t	sc_nxrec;	/* position of end of tape, if known */
88 	short	sc_resid;	/* copy of last bc */
89 	struct ts_cmd sc_cmd;	/* the command packet - ADDR MUST BE 0 MOD 4 */
90 	struct ts_sts sc_sts;	/* status packet, for returned status */
91 	struct ts_char sc_char;	/* characteristics packet */
92 	u_short	sc_uba;		/* Unibus addr of cmd pkt for tsdb */
93 } ts_softc[NTS];
94 
95 struct ts_softc *ts_ubaddr;	/* Unibus address of ts_softc */
96 
97 /*
98  * States for um->um_tab.b_active, the per controller state flag.
99  * This is used to sequence control in the driver.
100  */
101 #define	SSEEK	1		/* seeking */
102 #define	SIO	2		/* doing seq i/o */
103 #define	SCOM	3		/* sending control command */
104 #define	SREW	4		/* sending a drive rewind */
105 
106 /*
107  * Determine if there is a controller for
108  * a ts at address reg.  Our goal is to make the
109  * device interrupt.
110  */
111 tsprobe(reg)
112 	caddr_t reg;
113 {
114 	register int br, cvec;		/* must be r11,r10; value-result */
115 
116 #ifdef lint
117 	br = 0; cvec = br; br = cvec;
118 #endif
119 	/****************/
120 	/*		*/
121 	/*  K L U D G E */
122 	/*		*/
123 	/****************/
124 
125 	/* IT'S TOO HARD TO MAKE THIS THING INTERRUPT
126 	   JUST TO FIND ITS VECTOR */
127 	cvec = 0224;
128 	br = 0x15;
129 }
130 
131 /*
132  * TS11 only supports one drive per controller;
133  * check for ui_slave == 0.
134  *
135  * DO WE REALLY NEED THIS ROUTINE???
136  */
137 /*ARGSUSED*/
138 tsslave(ui, reg)
139 	struct uba_device *ui;
140 	caddr_t reg;
141 {
142 
143 	if (ui->ui_slave)	/* non-zero slave not allowed */
144 		return(0);
145 	return (1);
146 }
147 
148 /*
149  * Record attachment of the unit to the controller.
150  *
151  * SHOULD THIS ROUTINE DO ANYTHING???
152  */
153 /*ARGSUSED*/
154 tsattach(ui)
155 	struct uba_device *ui;
156 {
157 
158 }
159 
160 /*
161  * Open the device.  Tapes are unique open
162  * devices, so we refuse if it is already open.
163  * We also check that a tape is available, and
164  * don't block waiting here; if you want to wait
165  * for a tape you should timeout in user code.
166  */
167 tsopen(dev, flag)
168 	dev_t dev;
169 	int flag;
170 {
171 	register int tsunit;
172 	register struct uba_device *ui;
173 	register struct ts_softc *sc;
174 
175 	tsunit = TSUNIT(dev);
176 	if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
177 	    (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
178 		u.u_error = ENXIO;
179 		return;
180 	}
181 	if (tsinit(tsunit)) {
182 		u.u_error = ENXIO;
183 		return;
184 	}
185 	tscommand(dev, TS_SENSE, 1);
186 	if ((sc->sc_sts.s_xs0&TS_ONL) == 0 || ((flag&(FREAD|FWRITE)) ==
187 	    FWRITE && (sc->sc_sts.s_xs0&TS_WLK))) {
188 		/*
189 		 * Not online or write locked.
190 		 */
191 		u.u_error = EIO;
192 		return;
193 	}
194 	sc->sc_openf = 1;
195 	sc->sc_blkno = (daddr_t)0;
196 	sc->sc_nxrec = INF;
197 	sc->sc_lastiow = 0;
198 }
199 
200 /*
201  * Close tape device.
202  *
203  * If tape was open for writing or last operation was
204  * a write, then write two EOF's and backspace over the last one.
205  * Unless this is a non-rewinding special file, rewind the tape.
206  * Make the tape available to others.
207  */
208 tsclose(dev, flag)
209 	register dev_t dev;
210 	register flag;
211 {
212 	register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
213 
214 	if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
215 		tscommand(dev, TS_WEOF, 1);
216 		tscommand(dev, TS_WEOF, 1);
217 		tscommand(dev, TS_SREV, 1);
218 	}
219 	if ((minor(dev)&T_NOREWIND) == 0)
220 		/*
221 		 * 0 count means don't hang waiting for rewind complete
222 		 * rather ctsbuf stays busy until the operation completes
223 		 * preventing further opens from completing by
224 		 * preventing a TS_SENSE from completing.
225 		 */
226 		tscommand(dev, TS_REW, 0);
227 	sc->sc_openf = 0;
228 }
229 
230 /*
231  * Initialize the TS11.  Set up Unibus mapping for command
232  * packets and set device characteristics.
233  */
234 tsinit(unit)
235 	register int unit;
236 {
237 	register struct ts_softc *sc = &ts_softc[unit];
238 	register struct uba_ctlr *um = tsminfo[unit];
239 	register struct device *addr = (struct device *)um->um_addr;
240 	register int i;
241 
242 	/*
243 	 * Map the command and message packets into Unibus
244 	 * address space.  We do all the command and message
245 	 * packets at once to minimize the amount of Unibus
246 	 * mapping necessary.
247 	 */
248 	if (ts_ubaddr == 0) {
249 		ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
250 		ctsbuf[unit].b_bcount = sizeof(ts_softc);
251 		i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
252 		i &= 0777777;
253 		ts_ubaddr = (struct ts_softc *)i;
254 		/* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
255 		if (ts_ubaddr == 0)
256 			printf("ts%d: zero ubaddr\n", unit);
257 	}
258 	/*
259 	 * Now initialize the TS11 controller.
260 	 * Set the characteristics.
261 	 */
262 	if (addr->tssr & TS_NBA) {
263 		addr->tssr = 0;		/* subsystem initialize */
264 		tswait(addr);
265 		i = (int)&ts_ubaddr[unit].sc_cmd;	/* Unibus addr of cmd */
266 		sc->sc_uba = (u_short)(i + ((i>>16)&3));
267 		sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
268 		sc->sc_char.char_size = sizeof(struct ts_sts);
269 		sc->sc_char.char_mode = TS_ESS;
270 		sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
271 		sc->sc_cmd.c_addr = (int)&ts_ubaddr[unit].sc_char;
272 		sc->sc_cmd.c_size = sizeof(struct ts_char);
273 		addr->tsdb = sc->sc_uba;
274 		tswait(addr);
275 	}
276 	return(0);
277 }
278 
279 /*
280  * Execute a command on the tape drive
281  * a specified number of times.
282  */
283 tscommand(dev, com, count)
284 	dev_t dev;
285 	int com, count;
286 {
287 	register struct buf *bp;
288 
289 	bp = &ctsbuf[TSUNIT(dev)];
290 	(void) spl5();
291 	while (bp->b_flags&B_BUSY) {
292 		/*
293 		 * This special check is because B_BUSY never
294 		 * gets cleared in the non-waiting rewind case.
295 		 */
296 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
297 			break;
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 	tsstrategy(bp);
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;
314 	iowait(bp);
315 	if (bp->b_flags&B_WANTED)
316 		wakeup((caddr_t)bp);
317 	bp->b_flags &= B_ERROR;
318 }
319 
320 /*
321  * Queue a tape operation.
322  */
323 tsstrategy(bp)
324 	register struct buf *bp;
325 {
326 	int tsunit = TSUNIT(bp->b_dev);
327 	register struct uba_ctlr *um;
328 
329 	/*
330 	 * Put transfer at end of controller queue
331 	 */
332 	bp->av_forw = NULL;
333 	um = tsdinfo[tsunit]->ui_mi;
334 	(void) spl5();
335 	if (um->um_tab.b_actf == NULL)
336 		um->um_tab.b_actf = bp;
337 	else
338 		um->um_tab.b_actl->av_forw = bp;
339 	um->um_tab.b_actl = bp;
340 	/*
341 	 * If the controller is not busy, get
342 	 * it going.
343 	 */
344 	if (um->um_tab.b_active == 0)
345 		tsstart(um);
346 	(void) spl0();
347 }
348 
349 /*
350  * Start activity on a ts controller.
351  */
352 tsstart(um)
353 	register struct uba_ctlr *um;
354 {
355 	register struct buf *bp;
356 	register struct device *addr = (struct device *)um->um_addr;
357 	register struct ts_softc *sc;
358 	register struct ts_cmd *tc;
359 	register struct uba_device *ui;
360 	int tsunit, cmd;
361 	daddr_t blkno;
362 
363 	/*
364 	 * Start the controller if there is something for it to do.
365 	 */
366 loop:
367 	if ((bp = um->um_tab.b_actf) == NULL)
368 		return;
369 	tsunit = TSUNIT(bp->b_dev);
370 	ui = tsdinfo[tsunit];
371 	sc = &ts_softc[tsunit];
372 	tc = &sc->sc_cmd;
373 	/*
374 	 * Default is that last command was NOT a write command;
375 	 * if we do a write command we will notice this in tsintr().
376 	 */
377 	sc->sc_lastiow = 1;
378 	if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
379 		/*
380 		 * Have had a hard error on a non-raw tape
381 		 * or the tape unit is now unavailable
382 		 * (e.g. taken off line).
383 		 */
384 		bp->b_flags |= B_ERROR;
385 		goto next;
386 	}
387 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
388 		/*
389 		 * Execute control operation with the specified count.
390 		 */
391 		um->um_tab.b_active =
392 		    bp->b_command == TS_REW ? SREW : SCOM;
393 		tc->c_repcnt = bp->b_repcnt;
394 		goto dobpcmd;
395 	}
396 	/*
397 	 * The following checks handle boundary cases for operation
398 	 * on non-raw tapes.  On raw tapes the initialization of
399 	 * sc->sc_nxrec by tsphys causes them to be skipped normally
400 	 * (except in the case of retries).
401 	 */
402 	if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
403 		/*
404 		 * Can't read past known end-of-file.
405 		 */
406 		bp->b_flags |= B_ERROR;
407 		bp->b_error = ENXIO;
408 		goto next;
409 	}
410 	if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
411 	    bp->b_flags&B_READ) {
412 		/*
413 		 * Reading at end of file returns 0 bytes.
414 		 */
415 		bp->b_resid = bp->b_bcount;
416 		clrbuf(bp);
417 		goto next;
418 	}
419 	if ((bp->b_flags&B_READ) == 0)
420 		/*
421 		 * Writing sets EOF
422 		 */
423 		sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
424 	/*
425 	 * If the data transfer command is in the correct place,
426 	 * set up all the registers except the csr, and give
427 	 * control over to the UNIBUS adapter routines, to
428 	 * wait for resources to start the i/o.
429 	 */
430 	if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
431 		tc->c_size = bp->b_bcount;
432 		if ((bp->b_flags&B_READ) == 0)
433 			cmd = TS_WCOM;
434 		else
435 			cmd = TS_RCOM;
436 		if (um->um_tab.b_errcnt)
437 			cmd |= TS_RETRY;
438 		um->um_tab.b_active = SIO;
439 		tc->c_cmd = TS_ACK | TS_CVC | cmd;
440 		(void) ubago(ui);
441 		return;
442 	}
443 	/*
444 	 * Tape positioned incorrectly;
445 	 * set to seek forwards or backwards to the correct spot.
446 	 * This happens for raw tapes only on error retries.
447 	 */
448 	um->um_tab.b_active = SSEEK;
449 	if (blkno < dbtofsb(bp->b_blkno)) {
450 		bp->b_command = TS_SFORW;
451 		tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
452 	} else {
453 		bp->b_command = TS_SREV;
454 		tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
455 	}
456 dobpcmd:
457 	/*
458 	 * Do the command in bp.
459 	 */
460 	tc->c_cmd = TS_ACK | TS_CVC | bp->b_command;
461 	addr->tsdb = sc->sc_uba;
462 	return;
463 
464 next:
465 	/*
466 	 * Done with this operation due to error or
467 	 * the fact that it doesn't do anything.
468 	 * Release UBA resources (if any), dequeue
469 	 * the transfer and continue processing this slave.
470 	 */
471 	if (um->um_ubinfo)
472 		ubadone(um);
473 	um->um_tab.b_errcnt = 0;
474 	um->um_tab.b_actf = bp->av_forw;
475 	iodone(bp);
476 	goto loop;
477 }
478 
479 /*
480  * The UNIBUS resources we needed have been
481  * allocated to us; start the device.
482  */
483 tsdgo(um)
484 	register struct uba_ctlr *um;
485 {
486 	register struct device *addr = (struct device *)um->um_addr;
487 	register struct ts_softc *sc = &ts_softc[um->um_ctlr];
488 
489 	sc->sc_cmd.c_addr = um->um_ubinfo & 0777777;
490 	addr->tsdb = sc->sc_uba;
491 }
492 
493 /*
494  * Ts interrupt routine.
495  */
496 /*ARGSUSED*/
497 tsintr(ts11)
498 	int ts11;
499 {
500 	register struct buf *bp;
501 	register struct uba_ctlr *um = tsminfo[ts11];
502 	register struct device *addr;
503 	register struct ts_softc *sc;
504 	int tsunit;
505 	register state;
506 
507 	if ((bp = um->um_tab.b_actf) == NULL)
508 		return;
509 	tsunit = TSUNIT(bp->b_dev);
510 	addr = (struct device *)tsdinfo[tsunit]->ui_addr;
511 	/*
512 	 * If last command was a rewind, and tape is still
513 	 * rewinding, wait for the rewind complete interrupt.
514 	 *
515 	 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
516 	 */
517 	if (um->um_tab.b_active == SREW) {
518 		um->um_tab.b_active = SCOM;
519 		if ((addr->tssr&TS_SSR) == 0)
520 			return;
521 	}
522 	/*
523 	 * An operation completed... record status
524 	 */
525 	sc = &ts_softc[tsunit];
526 	if ((bp->b_flags & B_READ) == 0)
527 		sc->sc_lastiow = 1;
528 	state = um->um_tab.b_active;
529 	um->um_tab.b_active = 0;
530 	/*
531 	 * Check for errors.
532 	 */
533 	if (addr->tssr&TS_SC) {
534 		switch (addr->tssr & TS_TC) {
535 		case TS_UNREC:		/* unrecoverable */
536 		case TS_FATAL:		/* fatal error */
537 		case TS_ATTN:		/* attention (shouldn't happen) */
538 		case TS_RECNM:		/* recoverable, no motion */
539 			break;
540 
541 		case TS_SUCC:		/* success termination */
542 			printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
543 			goto ignoreerr;
544 
545 		case TS_ALERT:		/* tape status alert */
546 			/*
547 			 * If we hit the end of the tape file,
548 			 * update our position.
549 			 */
550 			if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
551 				tsseteof(bp);		/* set blkno and nxrec */
552 				state = SCOM;		/* force completion */
553 				/*
554 				 * Stuff bc so it will be unstuffed correctly
555 				 * later to get resid.
556 				 */
557 				sc->sc_sts.s_rbpcr = bp->b_bcount;
558 				goto opdone;
559 			}
560 			/*
561 			 * If we were reading raw tape and the record was too long
562 			 * or too short, then we don't consider this an error.
563 			 */
564 			if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
565 			    sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
566 				goto ignoreerr;
567 		case TS_RECOV:		/* recoverable, tape moved */
568 			/*
569 			 * If this was an i/o operation retry up to 8 times.
570 			 */
571 			if (state==SIO) {
572 				if (++um->um_tab.b_errcnt < 7) {
573 					ubadone(um);
574 					goto opcont;
575 				} else
576 					sc->sc_blkno++;
577 			} else {
578 				/*
579 				 * Non-i/o errors on non-raw tape
580 				 * cause it to close.
581 				 */
582 				if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
583 					sc->sc_openf = -1;
584 			}
585 			break;
586 
587 		case TS_REJECT:		/* function reject */
588 			if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
589 				printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
590 			if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
591 				printf("ts%d: offline\n", TSUNIT(bp->b_dev));
592 			break;
593 		}
594 		/*
595 		 * Couldn't recover error
596 		 */
597 		printf("ts%d: hard error bn%d xs0=%b\n", TSUNIT(bp->b_dev),
598 		    bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
599 		bp->b_flags |= B_ERROR;
600 		goto opdone;
601 	}
602 	/*
603 	 * Advance tape control FSM.
604 	 */
605 ignoreerr:
606 	switch (state) {
607 
608 	case SIO:
609 		/*
610 		 * Read/write increments tape block number
611 		 */
612 		sc->sc_blkno++;
613 		goto opdone;
614 
615 	case SCOM:
616 		/*
617 		 * For forward/backward space record update current position.
618 		 */
619 		if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
620 		switch (bp->b_command) {
621 
622 		case TS_SFORW:
623 			sc->sc_blkno += bp->b_repcnt;
624 			break;
625 
626 		case TS_SREV:
627 			sc->sc_blkno -= bp->b_repcnt;
628 			break;
629 		}
630 		goto opdone;
631 
632 	case SSEEK:
633 		sc->sc_blkno = dbtofsb(bp->b_blkno);
634 		goto opcont;
635 
636 	default:
637 		panic("tsintr");
638 	}
639 opdone:
640 	/*
641 	 * Reset error count and remove
642 	 * from device queue.
643 	 */
644 	um->um_tab.b_errcnt = 0;
645 	um->um_tab.b_actf = bp->av_forw;
646 	bp->b_resid = sc->sc_sts.s_rbpcr;
647 	ubadone(um);
648 	iodone(bp);
649 	if (um->um_tab.b_actf == 0)
650 		return;
651 opcont:
652 	tsstart(um);
653 }
654 
655 tsseteof(bp)
656 	register struct buf *bp;
657 {
658 	register int tsunit = TSUNIT(bp->b_dev);
659 	register struct ts_softc *sc = &ts_softc[tsunit];
660 
661 	if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
662 		if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
663 			/* reversing */
664 			sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
665 			sc->sc_blkno = sc->sc_nxrec;
666 		} else {
667 			/* spacing forward */
668 			sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
669 			sc->sc_nxrec = sc->sc_blkno - 1;
670 		}
671 		return;
672 	}
673 	/* eof on read */
674 	sc->sc_nxrec = dbtofsb(bp->b_blkno);
675 }
676 
677 tsread(dev)
678 	dev_t dev;
679 {
680 
681 	tsphys(dev);
682 	if (u.u_error)
683 		return;
684 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
685 }
686 
687 tswrite(dev)
688 	dev_t dev;
689 {
690 
691 	tsphys(dev);
692 	if (u.u_error)
693 		return;
694 	physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
695 }
696 
697 /*
698  * Check that a raw device exists.
699  * If it does, set up sc_blkno and sc_nxrec
700  * so that the tape will appear positioned correctly.
701  */
702 tsphys(dev)
703 	dev_t dev;
704 {
705 	register int tsunit = TSUNIT(dev);
706 	register daddr_t a;
707 	register struct ts_softc *sc;
708 	register struct uba_device *ui;
709 
710 	if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
711 		u.u_error = ENXIO;
712 		return;
713 	}
714 	sc = &ts_softc[tsunit];
715 	a = dbtofsb(u.u_offset >> 9);
716 	sc->sc_blkno = a;
717 	sc->sc_nxrec = a + 1;
718 }
719 
720 tsreset(uban)
721 	int uban;
722 {
723 	register struct uba_ctlr *um;
724 	register ts11;
725 	register struct buf *dp;
726 
727 	for (ts11 = 0; ts11 < NTS; ts11++) {
728 		if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
729 		   um->um_ubanum != uban)
730 			continue;
731 		printf(" ts%d", ts11);
732 		um->um_tab.b_active = 0;
733 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
734 		ts_softc[ts11].sc_openf = -1;
735 		if (um->um_ubinfo) {
736 			printf("<%d>", (um->um_ubinfo>>28)&0xf);
737 			ubadone(um);
738 		}
739 		tsinit(ts11);
740 		tsstart(um);
741 	}
742 }
743 
744 /*ARGSUSED*/
745 tsioctl(dev, cmd, addr, flag)
746 	caddr_t addr;
747 	dev_t dev;
748 {
749 	int tsunit = TSUNIT(dev);
750 	register struct ts_softc *sc = &ts_softc[tsunit];
751 	register struct buf *bp = &ctsbuf[TSUNIT(dev)];
752 	register callcount;
753 	int fcount;
754 	struct mtop mtop;
755 	struct mtget mtget;
756 	/* we depend of the values and order of the MT codes here */
757 	static tsops[] =
758 	 {TS_WEOF,TS_SFORW,TS_SREV,TS_SFORWF,TS_SREVF,TS_REW,TS_OFFL,TS_SENSE};
759 
760 	switch (cmd) {
761 	case MTIOCTOP:	/* tape operation */
762 		if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
763 			u.u_error = EFAULT;
764 			return;
765 		}
766 		switch(mtop.mt_op) {
767 		case MTWEOF:
768 			callcount = mtop.mt_count;
769 			fcount = 1;
770 			break;
771 		case MTFSF: case MTBSF:
772 		case MTFSR: case MTBSR:
773 			callcount = 1;
774 			fcount = mtop.mt_count;
775 			break;
776 		case MTREW: case MTOFFL: case MTNOP:
777 			callcount = 1;
778 			fcount = 1;
779 			break;
780 		default:
781 			u.u_error = ENXIO;
782 			return;
783 		}
784 		if (callcount <= 0 || fcount <= 0) {
785 			u.u_error = ENXIO;
786 			return;
787 		}
788 		while (--callcount >= 0) {
789 			tscommand(dev, tsops[mtop.mt_op], fcount);
790 			if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
791 			    bp->b_resid) {
792 				u.u_error = EIO;
793 				break;
794 			}
795 			if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
796 				break;
797 		}
798 		geterror(bp);
799 		return;
800 	case MTIOCGET:
801 		mtget.mt_dsreg = 0;
802 		mtget.mt_erreg = sc->sc_sts.s_xs0;
803 		mtget.mt_resid = sc->sc_resid;
804 		if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
805 			u.u_error = EFAULT;
806 		return;
807 	default:
808 		u.u_error = ENXIO;
809 	}
810 }
811 
812 #define	DBSIZE	20
813 
814 tsdump()
815 {
816 	register struct uba_device *ui;
817 	register struct uba_regs *up;
818 	register struct device *addr;
819 	int blk, num;
820 	int start;
821 
822 	start = 0;
823 	num = maxfree;
824 #define	phys(a,b)	((b)((int)(a)&0x7fffffff))
825 	if (tsdinfo[0] == 0)
826 		return (ENXIO);
827 	ui = phys(tsdinfo[0], struct uba_device *);
828 	up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
829 #if VAX780
830 	if (cpu == VAX_780)
831 		ubainit(up);
832 #endif
833 	DELAY(1000000);
834 	addr = (struct device *)ui->ui_physaddr;
835 	addr->tssr = 0;
836 	tswait(addr);
837 	while (num > 0) {
838 		blk = num > DBSIZE ? DBSIZE : num;
839 		tsdwrite(start, blk, addr, up);
840 		start += blk;
841 		num -= blk;
842 	}
843 	tseof(addr);
844 	tseof(addr);
845 	tswait(addr);
846 	if (addr->tssr&TS_SC)
847 		return (EIO);
848 	addr->tssr = 0;
849 	tswait(addr);
850 	return (0);
851 }
852 
853 tsdwrite(dbuf, num, addr, up)
854 	register dbuf, num;
855 	register struct device *addr;
856 	struct uba_regs *up;
857 {
858 	register struct pte *io;
859 	register int npf;
860 
861 	tswait(addr);
862 	io = up->uba_map;
863 	npf = num+1;
864 	while (--npf != 0)
865 		 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
866 	*(int *)io = 0;
867 #ifdef notyet
868 	addr->tsbc = -(num*NBPG);
869 	addr->tsba = 0;
870 	addr->tscs = TS_WCOM | TM_GO;
871 #endif
872 }
873 
874 tswait(addr)
875 	register struct device *addr;
876 {
877 	register s;
878 
879 	do
880 		s = addr->tssr;
881 	while ((s & TS_SSR) == 0);
882 }
883 
884 tseof(addr)
885 	struct device *addr;
886 {
887 
888 	tswait(addr);
889 #ifdef notyet
890 	addr->tscs = TS_WEOF | TM_GO;
891 #endif
892 }
893 #endif
894