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