xref: /netbsd-src/sys/dev/qbus/ts.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ts.c,v 1.21 2007/10/19 12:01:09 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1991 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)tmscp.c	7.16 (Berkeley) 5/9/91
32  */
33 
34 /*
35  * sccsid = "@(#)tmscp.c	1.24	(ULTRIX)	1/21/86";
36  */
37 
38 /************************************************************************
39  *									*
40  *	  Licensed from Digital Equipment Corporation			*
41  *			 Copyright (c)					*
42  *		 Digital Equipment Corporation				*
43  *		     Maynard, Massachusetts				*
44  *			   1985, 1986					*
45  *		      All rights reserved.				*
46  *									*
47  *	  The Information in this software is subject to change		*
48  *   without notice and should not be construed as a commitment		*
49  *   by	 Digital  Equipment  Corporation.   Digital   makes  no		*
50  *   representations about the suitability of this software for		*
51  *   any purpose.  It is supplied "As Is" without expressed  or		*
52  *   implied  warranty.							*
53  *									*
54  *	  If the Regents of the University of California or its		*
55  *   licensees modify the software in a manner creating			*
56  *   derivative copyright rights, appropriate copyright			*
57  *   legends may be placed on  the derivative work in addition		*
58  *   to that set forth above.						*
59  *									*
60  ************************************************************************/
61 
62 /*
63  * TSV05/TS05 device driver, written by Bertram Barth.
64  *
65  * should be TS11 compatible (untested)
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: ts.c,v 1.21 2007/10/19 12:01:09 ad Exp $");
70 
71 #undef	TSDEBUG
72 
73 /*
74  * TODO:
75  *
76  * Keep track of tape position so that lseek et al works.
77  * Use tprintf to inform the user, not the system console.
78  */
79 
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/buf.h>
85 #include <sys/bufq.h>
86 #include <sys/conf.h>
87 #include <sys/errno.h>
88 #include <sys/file.h>
89 #include <sys/syslog.h>
90 #include <sys/ioctl.h>
91 #include <sys/mtio.h>
92 #include <sys/uio.h>
93 #include <sys/proc.h>
94 
95 #include <sys/bus.h>
96 
97 #include <dev/qbus/ubareg.h>
98 #include <dev/qbus/ubavar.h>
99 
100 #include <dev/qbus/tsreg.h>
101 
102 #include "ioconf.h"
103 
104 struct	ts {
105 	struct	cmd cmd;	/* command packet */
106 	struct	chr chr;	/* characteristics packet */
107 	struct	status status;	/* status packet */
108 };
109 
110 /*
111  * Software status, per controller.
112  * also status per tape-unit, since only one unit per controller
113  * (thus we have no struct ts_info)
114  */
115 struct	ts_softc {
116 	struct	device sc_dev;		/* Autoconf ... */
117 	struct	uba_unit sc_unit;	/* Struct common for UBA to talk */
118 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
119 	struct	ubinfo sc_ui;		/* mapping info for struct ts */
120 	struct	uba_unit sc_uu;		/* Struct for UBA to communicate */
121 	bus_space_tag_t sc_iot;
122 	bus_addr_t sc_ioh;
123 	bus_dma_tag_t sc_dmat;
124 	bus_dmamap_t sc_dmam;
125 	volatile struct ts *sc_vts;	/* Memory address of ts struct */
126 	struct	ts *sc_bts;		/* Unibus address of ts struct */
127 	int	sc_type;		/* TS11 or TS05? */
128 	short	sc_waddr;		/* Value to write to TSDB */
129 	struct	bufq_state *sc_bufq;	/* pending I/O requests */
130 
131 	short	sc_mapped;		/* Unibus map allocated ? */
132 	short	sc_state;		/* see below: ST_xxx */
133 	short	sc_rtc;			/* retry count for lcmd */
134 	short	sc_openf;		/* lock against multiple opens */
135 	short	sc_liowf;		/* last operation was write */
136 	struct	buf ts_cbuf;		/* internal cmd buffer (for ioctls) */
137 };
138 
139 #define	XNAME	sc->sc_dev.dv_xname
140 
141 #define	TS_WCSR(csr, val) \
142 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
143 #define TS_RCSR(csr) \
144 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
145 
146 #define LOWORD(x)	((int)(x) & 0xffff)
147 #define HIWORD(x)	(((int)(x) >> 16) & 0x3f)
148 
149 #define	TYPE_TS11	0
150 #define	TYPE_TS05	1
151 #define	TYPE_TU80	2
152 
153 #define	TS_INVALID	0
154 #define	TS_INIT		1
155 #define	TS_RUNNING	2
156 #define	TS_FASTREPOS	3
157 
158 static	void tsintr(void *);
159 static	void tsinit(struct ts_softc *);
160 static	void tscommand(struct ts_softc *, dev_t, int, int);
161 static	int tsstart(struct ts_softc *, int);
162 static	void tswchar(struct ts_softc *);
163 static	int tsreset(struct ts_softc *);
164 static	int tsmatch(struct device *, struct cfdata *, void *);
165 static	void tsattach(struct device *, struct device *, void *);
166 static	int tsready(struct uba_unit *);
167 
168 CFATTACH_DECL(ts, sizeof(struct ts_softc),
169     tsmatch, tsattach, NULL, NULL);
170 
171 dev_type_open(tsopen);
172 dev_type_close(tsclose);
173 dev_type_read(tsread);
174 dev_type_write(tswrite);
175 dev_type_ioctl(tsioctl);
176 dev_type_strategy(tsstrategy);
177 dev_type_dump(tsdump);
178 
179 const struct bdevsw ts_bdevsw = {
180 	tsopen, tsclose, tsstrategy, tsioctl, tsdump, nosize, D_TAPE
181 };
182 
183 const struct cdevsw ts_cdevsw = {
184 	tsopen, tsclose, tsread, tswrite, tsioctl,
185 	nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
186 };
187 
188 /* Bits in minor device */
189 #define TS_UNIT(dev)	(minor(dev)&03)
190 #define TS_HIDENSITY	010
191 
192 #define TS_PRI	LOG_INFO
193 
194 
195 /*
196  * Probe for device. If found, try to raise an interrupt.
197  */
198 int
199 tsmatch(struct device *parent, struct cfdata *match, void *aux)
200 {
201 	struct ts_softc ssc;
202 	struct ts_softc *sc = &ssc;
203 	struct uba_attach_args *ua = aux;
204 	int i;
205 
206 	sc->sc_iot = ua->ua_iot;
207 	sc->sc_ioh = ua->ua_ioh;
208 	sc->sc_mapped = 0;
209 	sc->sc_dev.dv_parent = parent;
210 	strcpy(XNAME, "ts");
211 
212 	/* Try to reset the device */
213 	for (i = 0; i < 3; i++)
214 		if (tsreset(sc) == 1)
215 			break;
216 
217 	if (i == 3)
218 		return 0;
219 
220 	tsinit(sc);
221 	tswchar(sc);		/* write charact. to enable interrupts */
222 				/* completion of this will raise the intr. */
223 
224 	DELAY(1000000);		/* Wait for interrupt */
225 	ubmemfree((void *)parent, &sc->sc_ui);
226 	return 1;
227 }
228 
229 /*
230  */
231 void
232 tsattach(struct device *parent, struct device *self, void *aux)
233 {
234 	struct uba_softc *uh = device_private(parent);
235 	struct ts_softc *sc = device_private(self);
236 	struct uba_attach_args *ua = aux;
237 	int error;
238 	char *t;
239 
240 	sc->sc_iot = ua->ua_iot;
241 	sc->sc_ioh = ua->ua_ioh;
242 	sc->sc_dmat = ua->ua_dmat;
243 
244 	sc->sc_uu.uu_softc = sc;
245 	sc->sc_uu.uu_ready = tsready;
246 
247 	tsinit(sc);	/* reset and map */
248 
249 	if ((error = bus_dmamap_create(sc->sc_dmat, (64*1024), 16, (64*1024),
250 	    0, BUS_DMA_NOWAIT, &sc->sc_dmam)))
251 		return printf(": failed create DMA map %d\n", error);
252 
253 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
254 
255 	/*
256 	 * write the characteristics (again)
257 	 */
258 	sc->sc_state = TS_INIT;		/* tsintr() checks this ... */
259 	tswchar(sc);
260 	if (tsleep(sc, PRIBIO, "tsattach", 100))
261 		return printf(": failed SET CHARACTERISTICS\n");
262 
263 	sc->sc_state = TS_RUNNING;
264 	if (uh->uh_type == UBA_UBA) {
265 		if (sc->sc_vts->status.xst2 & TS_SF_TU80) {
266 			sc->sc_type = TYPE_TU80;
267 			t = "TU80";
268 		} else {
269 			sc->sc_type = TYPE_TS11;
270 			t = "TS11";
271 		}
272 	} else {
273 		sc->sc_type = TYPE_TS05;
274 		t = "TS05";
275 	}
276 
277 	printf("\n%s: %s\n", XNAME, t);
278 	printf("%s: rev %d, extended features %s, transport %s\n",
279 		XNAME, (sc->sc_vts->status.xst2 & TS_SF_MCRL) >> 2,
280 		(sc->sc_vts->status.xst2 & TS_SF_EFES ? "enabled" : "disabled"),
281 		(TS_RCSR(TSSR) & TS_OFL ? "offline" : "online"));
282 
283 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, tsintr,
284 	    sc, &sc->sc_intrcnt);
285 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
286 	    sc->sc_dev.dv_xname, "intr");
287 }
288 
289 /*
290  * Initialize a TS device. Set up UBA mapping registers,
291  * initialize data structures, what else ???
292  */
293 void
294 tsinit(struct ts_softc *sc)
295 {
296 	if (sc->sc_mapped == 0) {
297 
298 		/*
299 		 * Map the communications area and command and message
300 		 * buffer into Unibus address space.
301 		 */
302 		sc->sc_ui.ui_size = sizeof(struct ts);
303 		if ((ubmemalloc((void *)device_parent(&sc->sc_dev),
304 		    &sc->sc_ui, UBA_CANTWAIT)))
305 			return;
306 		sc->sc_vts = (void *)sc->sc_ui.ui_vaddr;
307 		sc->sc_bts = (void *)sc->sc_ui.ui_baddr;
308 		sc->sc_waddr = sc->sc_ui.ui_baddr |
309 		    ((sc->sc_ui.ui_baddr >> 16) & 3);
310 		sc->sc_mapped = 1;
311 	}
312 	tsreset(sc);
313 }
314 
315 /*
316  * Execute a (ioctl) command on the tape drive a specified number of times.
317  * This routine sets up a buffer and calls the strategy routine which
318  * issues the command to the controller.
319  */
320 void
321 tscommand(struct ts_softc *sc, dev_t dev, int cmd, int count)
322 {
323 	struct buf *bp;
324 	int s;
325 
326 #ifdef TSDEBUG
327 	printf("tscommand (%x, %d)\n", cmd, count);
328 #endif
329 
330 	bp = &sc->ts_cbuf;
331 
332 	s = splbio();
333 	while (bp->b_flags & B_BUSY) {
334 		/*
335 		 * This special check is because B_BUSY never
336 		 * gets cleared in the non-waiting rewind case. ???
337 		 */
338 		if (bp->b_bcount == 0 && (bp->b_flags & B_DONE))
339 			break;
340 		bp->b_flags |= B_WANTED;
341 		(void) tsleep(bp, PRIBIO, "tscmd", 0);
342 		/* check MOT-flag !!! */
343 	}
344 	bp->b_flags = B_BUSY | B_READ;
345 	splx(s);
346 
347 	/*
348 	 * Load the buffer.  The b_count field gets used to hold the command
349 	 * count.  the b_resid field gets used to hold the command mneumonic.
350 	 * These 2 fields are "known" to be "safe" to use for this purpose.
351 	 * (Most other drivers also use these fields in this way.)
352 	 */
353 	bp->b_dev = dev;
354 	bp->b_bcount = count;
355 	bp->b_resid = cmd;
356 	bp->b_blkno = 0;
357 	tsstrategy(bp);
358 	/*
359 	 * In case of rewind from close, don't wait.
360 	 * This is the only case where count can be 0.
361 	 */
362 	if (count == 0)
363 		return;
364 	biowait(bp);
365 	if (bp->b_flags & B_WANTED)
366 		wakeup((void *)bp);
367 	bp->b_flags = 0;
368 }
369 
370 /*
371  * Start an I/O operation on TS05 controller
372  */
373 int
374 tsstart(struct ts_softc *sc, int isloaded)
375 {
376 	struct buf *bp;
377 	int cmd;
378 
379 	bp = BUFQ_PEEK(sc->sc_bufq);
380 	if (bp == NULL) {
381 		return 0;
382 	}
383 #ifdef TSDEBUG
384 	printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
385 #endif
386 	/*
387 	 * Check if command is an ioctl or not (ie. read or write).
388 	 * If it's an ioctl then just set the flags for later use;
389 	 * For other commands attempt to setup a buffer pointer.
390 	 */
391 	if (bp == &sc->ts_cbuf) {
392 		switch ((int)bp->b_resid) {
393 		case MTWEOF:
394 			cmd = TS_CMD_WTM;
395 			break;
396 		case MTFSF:
397 			cmd = TS_CMD_STMF;
398 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
399 			break;
400 		case MTBSF:
401 			cmd = TS_CMD_STMR;
402 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
403 			break;
404 		case MTFSR:
405 			cmd = TS_CMD_SRF;
406 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
407 			break;
408 		case MTBSR:
409 			cmd = TS_CMD_SRR;
410 			sc->sc_vts->cmd.cw1 = bp->b_bcount;
411 			break;
412 		case MTREW:
413 			cmd = TS_CMD_RWND;
414 			break;
415 		case MTOFFL:
416 			cmd = TS_CMD_RWUL;
417 			break;
418 		case MTNOP:
419 			cmd = TS_CMD_STAT;
420 			break;
421 		default:
422 			printf ("%s: bad ioctl %d\n", sc->sc_dev.dv_xname,
423 				(int)bp->b_resid);
424 			/* Need a no-op. get status */
425 			cmd = TS_CMD_STAT;
426 		} /* end switch (bp->b_resid) */
427 	} else {
428 		if (isloaded == 0) {
429 			/*
430 			 * now we try to map the buffer into uba map space (???)
431 			 */
432 			if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam,
433 			    bp->b_data,
434 			    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT)) {
435 				uba_enqueue(&sc->sc_uu);
436 				return 0;
437 			}
438 			sc->sc_rtc = 0;
439 		}
440 		sc->sc_vts->cmd.cw1 = LOWORD(sc->sc_dmam->dm_segs[0].ds_addr);
441 		sc->sc_vts->cmd.cw2 = HIWORD(sc->sc_dmam->dm_segs[0].ds_addr);
442 		sc->sc_vts->cmd.cw3 = bp->b_bcount;
443 		bp->b_error = 0; /* Used for error count */
444 #ifdef TSDEBUG
445 		printf("tsstart-1: err %d\n", bp->b_error);
446 #endif
447 		if (bp->b_flags & B_READ)
448 			cmd = TS_CMD_RNF;
449 		else
450 			cmd = TS_CMD_WD;
451 	}
452 
453 	/*
454 	 * Now that the command-buffer is setup, give it to the controller
455 	 */
456 	sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | cmd;
457 #ifdef TSDEBUG
458 	printf("tsstart: sending cmdr %x\n", sc->sc_vts->cmd.cmdr);
459 #endif
460 	TS_WCSR(TSDB, sc->sc_waddr);
461 }
462 
463 /*
464  * Called when there are free uba resources.
465  */
466 int
467 tsready(struct uba_unit *uu)
468 {
469 	struct ts_softc *sc = uu->uu_softc;
470 	struct buf *bp = BUFQ_PEEK(sc->sc_bufq);
471 
472 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam, bp->b_data,
473 	    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT))
474 		return 0;
475 
476 	tsstart(sc, 1);
477 	return 1;
478 }
479 
480 /*
481  * initialize the controller by sending WRITE CHARACTERISTICS command.
482  * contents of command- and message-buffer are assembled during this
483  * function.
484  */
485 void
486 tswchar(struct ts_softc *sc)
487 {
488 	/*
489 	 * assemble and send "WRITE CHARACTERISTICS" command
490 	 */
491 
492 	sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_WCHAR;
493 	sc->sc_vts->cmd.cw1  = LOWORD(&sc->sc_bts->chr);
494 	sc->sc_vts->cmd.cw2  = HIWORD(&sc->sc_bts->chr);
495 	sc->sc_vts->cmd.cw3  = 010;		   /* size of charact.-data */
496 
497 	sc->sc_vts->chr.sadrl = LOWORD(&sc->sc_bts->status);
498 	sc->sc_vts->chr.sadrh = HIWORD(&sc->sc_bts->status);
499 	sc->sc_vts->chr.onesix = (sc->sc_type == TYPE_TS05 ? 020 : 016);
500 	sc->sc_vts->chr.chrw = TS_WC_ESS;
501 	sc->sc_vts->chr.xchrw = TS_WCX_HSP|TS_WCX_RBUF|TS_WCX_WBUF;
502 
503 	TS_WCSR(TSDB, sc->sc_waddr);
504 }
505 
506 /*
507  * Reset the TS11. Return 1 if failed, 0 if succeeded.
508  */
509 int
510 tsreset(struct ts_softc *sc)
511 {
512 	int timeout;
513 
514 	/*
515 	 * reset ctlr by writing into TSSR, then write characteristics
516 	 */
517 	timeout = 0;		/* timeout in 10 seconds */
518 	TS_WCSR(TSSR, 0);	/* start initialization */
519 	while ((TS_RCSR(TSSR) & TS_SSR) == 0) {
520 		DELAY(10000);
521 		if (timeout++ > 1000)
522 			return 0;
523 	}
524 	return 1;
525 }
526 
527 static void
528 prtstat(struct ts_softc *sc, int sr)
529 {
530 	char buf[100];
531 
532 	bitmask_snprintf(sr, TS_TSSR_BITS, buf, sizeof(buf));
533 	printf("%s: TSSR=%s\n", XNAME, buf);
534 	bitmask_snprintf(sc->sc_vts->status.xst0,TS_XST0_BITS,buf,sizeof(buf));
535 	printf("%s: XST0=%s\n", XNAME, buf);
536 }
537 
538 /*
539  * TSV05/TS05 interrupt routine
540  */
541 static void
542 tsintr(void *arg)
543 {
544 	struct ts_softc *sc = arg;
545 	struct buf *bp;
546 
547 	unsigned short sr = TS_RCSR(TSSR);	/* save TSSR */
548 	unsigned short mh = sc->sc_vts->status.hdr;	/* and msg-header */
549 		/* clear the message header ??? */
550 
551 	short ccode = sc->sc_vts->cmd.cmdr & TS_CF_CCODE;
552 
553 #ifdef TSDEBUG
554 	{
555 		char buf[100];
556 		bitmask_snprintf(sr, TS_TSSR_BITS, buf, sizeof(buf));
557 		printf("tsintr: sr %x mh %x\n", sr, mh);
558 		printf("srbits: %s\n", buf);
559 	}
560 #endif
561 	/*
562 	 * There are two different things which can (and should) be checked:
563 	 * the actual (internal) state and the device's result (tssr/msg.hdr)
564 	 *
565 	 * For each state there's only one "normal" interrupt. Anything else
566 	 * has to be checked more intensively. Thus in a first run according
567 	 * to the internal state the expected interrupt is checked/handled.
568 	 *
569 	 * In a second run the remaining (not yet handled) interrupts are
570 	 * checked according to the drive's result.
571 	 */
572 	switch (sc->sc_state) {
573 
574 	case TS_INVALID:
575 		/*
576 		 * Ignore unsolicited interrupts.
577 		 */
578 		log(LOG_WARNING, "%s: stray intr [%x,%x]\n",
579 			sc->sc_dev.dv_xname, sr, mh);
580 		return;
581 
582 	case TS_INIT:
583 		/*
584 		 * Init phase ready.
585 		 */
586 		wakeup(sc);
587 		return;
588 
589 	case TS_RUNNING:
590 	case TS_FASTREPOS:
591 		/*
592 		 * Here we expect interrupts indicating the end of
593 		 * commands or indicating problems.
594 		 */
595 		/*
596 		 * Anything else is handled outside this switch ...
597 		 */
598 		break;
599 
600 	default:
601 		printf ("%s: unexpected interrupt during state %d [%x,%x]\n",
602 			sc->sc_dev.dv_xname, sc->sc_state, sr, mh);
603 		return;
604 	}
605 
606 	/*
607 	 * now we check the termination class.
608 	 */
609 	switch (sr & TS_TC) {
610 
611 	case TS_TC_NORM:
612 		/*
613 		 * Normal termination -- The operation is completed
614 		 * witout incident.
615 		 */
616 		if (sc->sc_state == TS_FASTREPOS) {
617 #ifdef TSDEBUG
618 			printf("Fast repos interrupt\n");
619 #endif
620 			/* Fast repos succeeded, start normal data xfer */
621 			sc->sc_state = TS_RUNNING;
622 			tsstart(sc, 1);
623 			return;
624 		}
625 		sc->sc_liowf = (ccode == TS_CC_WRITE);
626 		break;
627 
628 	case TS_TC_ATTN:
629 		/*
630 		 * Attention condition -- this code indicates that the
631 		 * drive has undergone a status change, such as going
632 		 * off-line or coming on-line.
633 		 * (Without EAI enabled, no Attention interrupts occur.
634 		 * drive status changes are signaled by the VCK flag.)
635 		 */
636 		return;
637 
638 	case TS_TC_TSA:
639 		/*
640 		 * Tape Status Alert -- A status condition is encountered
641 		 * that may have significance to the program. Bits of
642 		 * interest in the extended status registers include
643 		 * TMK, EOT and RLL.
644 		 */
645 #ifdef TSDEBUG
646 		{
647 			char buf[100];
648 			bitmask_snprintf(sc->sc_vts->status.xst0,
649 			    TS_XST0_BITS, buf, sizeof(buf));
650 			printf("TSA: sr %x bits %s\n",
651 			    sc->sc_vts->status.xst0, buf);
652 		}
653 #endif
654 		if (sc->sc_vts->status.xst0 & TS_SF_TMK) {
655 			/* Read to end-of-file. No error. */
656 			break;
657 		}
658 		if (sc->sc_vts->status.xst0 & TS_SF_EOT) {
659 			/* End of tape. Bad. */
660 #ifdef TSDEBUG
661 			printf("TS_TC_TSA: EOT\n");
662 #endif
663 			bp->b_error = EIO;
664 			break;
665 		}
666 		if (sc->sc_vts->status.xst0 & TS_SF_RLS)
667 			break;
668 		if (sc->sc_vts->status.xst0 & TS_SF_TMK) {
669 #ifdef TSDEBUG
670 			printf(("Tape Mark detected"));
671 #endif
672 		}
673 		if (sc->sc_vts->status.xst0 & TS_SF_EOT) {
674 #ifdef TSDEBUG
675 			printf(("End of Tape"));
676 #endif
677 		}
678 #ifndef TSDEBUG
679 		{
680 			char buf[100];
681 			bitmask_snprintf(sc->sc_vts->status.xst0,
682 			    TS_XST0_BITS, buf, sizeof(buf));
683 			printf("TSA: sr %x bits %s\n",
684 			    sc->sc_vts->status.xst0, buf);
685 		}
686 #endif
687 		break;
688 
689 	case TS_TC_FR:
690 		/*
691 		 * Function Reject -- The specified function was not
692 		 * initiated. Bits of interest include OFL, VCK, BOT,
693 		 * WLE, ILC and ILA.
694 		 */
695 		if (sr & TS_OFL)
696 			printf("tape is off-line.\n");
697 #ifdef TSDEBUG
698 		{
699 			char buf[100];
700 			bitmask_snprintf(sc->sc_vts->status.xst0,
701 			    TS_XST0_BITS, buf, sizeof(buf));
702 			printf("FR: sr %x bits %s\n",
703 			    sc->sc_vts->status.xst0, buf);
704 		}
705 #endif
706 		if (sc->sc_vts->status.xst0 & TS_SF_VCK)
707 			printf("Volume check\n");
708 		if (sc->sc_vts->status.xst0 & TS_SF_BOT)
709 			printf("Start of tape.\n");
710 		if (sc->sc_vts->status.xst0 & TS_SF_WLE)
711 			printf("Write Lock Error\n");
712 		if (sc->sc_vts->status.xst0 & TS_SF_EOT)
713 			printf("End of tape.\n");
714 		break;
715 
716 	case TS_TC_TPD:
717 		/*
718 		 * Recoverable Error -- Tape position is a record beyond
719 		 * what its position was when the function was initiated.
720 		 * Suggested recovery procedure is to log the error and
721 		 * issue the appropriate retry command.
722 		 *
723 		 * Do a fast repositioning one record back.
724 		 */
725 		sc->sc_state = TS_FASTREPOS;
726 #ifdef TSDEBUG
727 		printf("TS_TC_TPD: errcnt %d\n", sc->sc_rtc);
728 #endif
729 		if (sc->sc_rtc++ == 8) {
730 			printf("%s: failed 8 retries\n", XNAME);
731 			prtstat(sc, sr);
732 			bp->b_error = EIO;
733 			break;
734 		}
735 		sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_SRR;
736 		sc->sc_vts->cmd.cw1 = 1;
737 		TS_WCSR(TSDB, sc->sc_waddr);
738 		return;
739 
740 		break;
741 
742 	case TS_TC_TNM:
743 		/*
744 		 * Recoverable Error -- Tape position has not changed.
745 		 * Suggested recovery procedure is to log the error and
746 		 * reissue the original command.
747 		 */
748 		if (sc->sc_rtc++ == 8) {
749 			printf("%s: failed 8 retries\n", XNAME);
750 			prtstat(sc, sr);
751 			bp->b_error = EIO;
752 			break;
753 		}
754 		tsstart(sc, 1);
755 		return;
756 
757 	case TS_TC_TPL:
758 		/*
759 		 * Unrecoverable Error -- Tape position has been lost.
760 		 * No valid recovery procedures exist unless the tape
761 		 * has labels or sequence numbers.
762 		 */
763 		printf("Tape position lost\n");
764 		bp->b_error = EIO;
765 		break;
766 
767 	case TS_TC_FCE:
768 		/*
769 		 * Fatal subsytem Error -- The subsytem is incapable
770 		 * of properly performing commands, or at least its
771 		 * integrity is seriously questionable. Refer to the
772 		 * fatal class code field in the TSSR register for
773 		 * additional information on the type of fatal error.
774 		 */
775 		printf ("Fatal Controller Error\n");
776 		prtstat(sc, sr);
777 		break;
778 
779 	default:
780 		printf ("%s: error 0x%x, resetting controller\n",
781 			sc->sc_dev.dv_xname, sr & TS_TC);
782 		tsreset(sc);
783 	}
784 	if ((bp = BUFQ_GET(sc->sc_bufq)) != NULL) {
785 #ifdef TSDEBUG
786 		printf("tsintr2: que %p\n", BUFQ_PEEK(sc->sc_bufq));
787 #endif
788 		if (bp != &sc->ts_cbuf) {	/* no ioctl */
789 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmam);
790 			uba_done((void *)device_parent(&sc->sc_dev));
791 		}
792 		bp->b_resid = sc->sc_vts->status.rbpcr;
793 		biodone (bp);
794 	}
795 	tsstart(sc, 0);
796 }
797 
798 
799 /*
800  * Open a ts device and set the unit online.  If the controller is not
801  * in the run state, call init to initialize the ts controller first.
802  */
803 int
804 tsopen(dev_t dev, int flag, int type, struct proc *p)
805 {
806 	struct ts_softc *sc;
807 	int unit = TS_UNIT(dev);
808 
809 	if (unit >= ts_cd.cd_ndevs)
810 		return ENXIO;
811 
812 	sc = ts_cd.cd_devs[unit];
813 	if (sc == 0)
814 		return ENXIO;
815 
816 	if (sc->sc_state < TS_RUNNING)
817 		return ENXIO;
818 
819 	if (sc->sc_openf)
820 		return EBUSY;
821 	sc->sc_openf = 1;
822 
823 	/*
824 	 * check if transport is really online.
825 	 * (without attention-interrupts enabled, we really don't know
826 	 * the actual state of the transport. Thus we call get-status
827 	 * (ie. MTNOP) once and check the actual status.)
828 	 */
829 	if (TS_RCSR(TSSR) & TS_OFL) {
830 		uprintf("%s: transport is offline.\n", XNAME);
831 		sc->sc_openf = 0;
832 		return EIO;		/* transport is offline */
833 	}
834 	tscommand(sc, dev, MTNOP, 1);
835 	if ((flag & FWRITE) && (sc->sc_vts->status.xst0 & TS_SF_WLK)) {
836 		uprintf("%s: no write ring.\n", XNAME);
837 		sc->sc_openf = 0;
838 		return EROFS;
839 	}
840 	if (sc->sc_vts->status.xst0 & TS_SF_VCK) {
841 		sc->sc_vts->cmd.cmdr = TS_CF_CVC|TS_CF_ACK;
842 		TS_WCSR(TSDB, sc->sc_waddr);
843 	}
844 	tscommand(sc, dev, MTNOP, 1);
845 #ifdef TSDEBUG
846 	{
847 		char buf[100];
848 		bitmask_snprintf(sc->sc_vts->status.xst0,
849 		    TS_XST0_BITS, buf, sizeof(buf));
850 		printf("tsopen: xst0 %s\n", buf);
851 	}
852 #endif
853 	sc->sc_liowf = 0;
854 	return 0;
855 }
856 
857 
858 /*
859  * Close tape device.
860  *
861  * If tape was open for writing or last operation was
862  * a write, then write two EOF's and backspace over the last one.
863  * Unless this is a non-rewinding special file, rewind the tape.
864  *
865  * Make the tape available to others, by clearing openf flag.
866  */
867 int
868 tsclose(dev_t dev, int flag, int type, struct proc *p)
869 {
870 	struct ts_softc *sc = ts_cd.cd_devs[TS_UNIT(dev)];
871 
872 	if (flag == FWRITE || ((flag & FWRITE) && sc->sc_liowf)) {
873 		/*
874 		 * We are writing two tape marks (EOT), but place the tape
875 		 * before the second one, so that another write operation
876 		 * will overwrite the second one and leave and EOF-mark.
877 		 */
878 		tscommand(sc, dev, MTWEOF, 1);	/* Write Tape Mark */
879 		tscommand(sc, dev, MTWEOF, 1);	/* Write Tape Mark */
880 		tscommand(sc, dev, MTBSF, 1);	/* Skip Tape Marks Reverse */
881 	}
882 
883 	if ((dev & T_NOREWIND) == 0)
884 		tscommand(sc, dev, MTREW, 0);
885 
886 	sc->sc_openf = 0;
887 	sc->sc_liowf = 0;
888 	return 0;
889 }
890 
891 
892 /*
893  * Manage buffers and perform block mode read and write operations.
894  */
895 void
896 tsstrategy(struct buf *bp)
897 {
898 	register int unit = TS_UNIT(bp->b_dev);
899 	struct ts_softc *sc = (void *)ts_cd.cd_devs[unit];
900 	int s, empty;
901 
902 #ifdef TSDEBUG
903 	printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
904 #endif
905 	s = splbio ();
906 	empty = (BUFQ_PEEK(sc->sc_bufq) == NULL);
907 	BUFQ_PUT(sc->sc_bufq, bp);
908 	if (empty)
909 		tsstart(sc, 0);
910 	splx(s);
911 }
912 
913 
914 /*
915  * Catch ioctl commands, and call the "command" routine to do them.
916  */
917 int
918 tsioctl(dev, cmd, data, flag, p)
919 	dev_t dev;
920 	u_long cmd;
921 	void *data;
922 	int flag;
923 	struct proc *p;
924 {
925 	struct buf *bp;
926 	struct ts_softc *sc;
927 	struct mtop *mtop;	/* mag tape cmd op to perform */
928 	struct mtget *mtget;	/* mag tape struct to get info in */
929 	int callcount;		/* number of times to call routine */
930 	int scount;			/* number of files/records to space */
931 	int spaceop = 0;		/* flag for skip/space operation */
932 	int error = 0;
933 
934 #ifdef TSDEBUG
935 	printf("tsioctl (%x, %lx, %p, %d)\n", dev, cmd, data, flag);
936 #endif
937 
938 	sc = ts_cd.cd_devs[TS_UNIT(dev)];
939 	bp = &sc->ts_cbuf;
940 
941 	switch (cmd) {
942 	case MTIOCTOP:			/* do a mag tape op */
943 		mtop = (struct mtop *)data;
944 		switch (mtop->mt_op) {
945 		case MTWEOF:		/* write an end-of-file record */
946 			callcount = mtop->mt_count;
947 			scount = 1;
948 			break;
949 		case MTFSR:		/* forward space record */
950 		case MTBSR:		/* backward space record */
951 			spaceop = 1;
952 		case MTFSF:		/* forward space file */
953 		case MTBSF:		/* backward space file */
954 			callcount = 1;
955 			scount = mtop->mt_count;
956 			break;
957 		case MTREW:		/* rewind */
958 		case MTOFFL:		/* rewind and put the drive offline */
959 		case MTNOP:		/* no operation, sets status only */
960 			callcount = 1;
961 			scount = 1;		/* wait for this rewind */
962 			break;
963 		case MTRETEN:		/* retension */
964 		case MTERASE:		/* erase entire tape */
965 		case MTEOM:		/* forward to end of media */
966 		case MTNBSF:		/* backward space to begin of file */
967 		case MTCACHE:		/* enable controller cache */
968 		case MTNOCACHE:		/* disable controller cache */
969 		case MTSETBSIZ:		/* set block size; 0 for variable */
970 		case MTSETDNSTY:	/* set density code for current mode */
971 			printf("ioctl %d not implemented.\n", mtop->mt_op);
972 			return (ENXIO);
973 		default:
974 #ifdef TSDEBUG
975 			printf("invalid ioctl %d\n", mtop->mt_op);
976 #endif
977 			return (ENXIO);
978 		}	/* switch (mtop->mt_op) */
979 
980 		if (callcount <= 0 || scount <= 0) {
981 #ifdef TSDEBUG
982 			printf("invalid values %d/%d\n", callcount, scount);
983 #endif
984 			return (EINVAL);
985 		}
986 		do {
987 			tscommand(sc, dev, mtop->mt_op, scount);
988 			if (spaceop && bp->b_resid) {
989 #ifdef TSDEBUG
990 				printf(("spaceop didn't complete\n"));
991 #endif
992 				return (EIO);
993 			}
994 			if (bp->b_error != 0) {
995 #ifdef TSDEBUG
996 				printf("error in ioctl %d\n", mtop->mt_op);
997 #endif
998 				break;
999 			}
1000 		} while (--callcount > 0);
1001 		if (bp->b_error != 0)
1002 			error = bp->b_error;
1003 		return (error);
1004 
1005 	case MTIOCGET:			/* get tape status */
1006 		mtget = (struct mtget *)data;
1007 		mtget->mt_type = MT_ISTS;
1008 		mtget->mt_dsreg = TS_RCSR(TSSR);
1009 		mtget->mt_erreg = sc->sc_vts->status.xst0;
1010 		mtget->mt_resid = 0;		/* ??? */
1011 		mtget->mt_density = 0;		/* ??? */
1012 		break;
1013 
1014 	case MTIOCIEOT:			/* ignore EOT error */
1015 #ifdef TSDEBUG
1016 		printf(("MTIOCIEOT not implemented.\n"));
1017 #endif
1018 		return (ENXIO);
1019 
1020 	case MTIOCEEOT:			/* enable EOT error */
1021 #ifdef TSDEBUG
1022 		printf(("MTIOCEEOT not implemented.\n"));
1023 #endif
1024 		return (ENXIO);
1025 
1026 	default:
1027 #ifdef TSDEBUG
1028 		printf("invalid ioctl cmd 0x%lx\n", cmd);
1029 #endif
1030 		return (ENXIO);
1031 	}
1032 
1033 	return (0);
1034 }
1035 
1036 
1037 /*
1038  *
1039  */
1040 int
1041 tsread(dev_t dev, struct uio *uio, int flag)
1042 {
1043 	return (physio (tsstrategy, NULL, dev, B_READ, minphys, uio));
1044 }
1045 
1046 /*
1047  *
1048  */
1049 int
1050 tswrite(dev_t dev, struct uio *uio, int flag)
1051 {
1052 	return (physio (tsstrategy, NULL, dev, B_WRITE, minphys, uio));
1053 }
1054 
1055 /*
1056  *
1057  */
1058 int
1059 tsdump(dev, blkno, va, size)
1060 	dev_t dev;
1061 	daddr_t blkno;
1062 	void *va;
1063 	size_t size;
1064 {
1065 	return EIO;
1066 }
1067