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