xref: /netbsd-src/sys/arch/hp300/dev/mt.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: mt.c,v 1.27 2004/10/28 07:07:36 yamt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1992, The University of Utah and
41  * the Computer Systems Laboratory at the University of Utah (CSL).
42  * All rights reserved.
43  *
44  * Permission to use, copy, modify and distribute this software is hereby
45  * granted provided that (1) source code retains these copyright, permission,
46  * and disclaimer notices, and (2) redistributions including binaries
47  * reproduce the notices in supporting documentation, and (3) all advertising
48  * materials mentioning features or use of this software display the following
49  * acknowledgement: ``This product includes software developed by the
50  * Computer Systems Laboratory at the University of Utah.''
51  *
52  * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
53  * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
54  * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55  *
56  * CSL requests users of this software to return to csl-dist@cs.utah.edu any
57  * improvements that they make and grant CSL redistribution rights.
58  *
59  *	Utah $Hdr: mt.c 1.8 95/09/12$
60  */
61 /*	@(#)mt.c	3.9	90/07/10	mt Xinu
62  *
63  * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
64  * Original version contributed by Mt. Xinu.
65  * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
66  * Computer Science, Victoria University of Wellington
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.27 2004/10/28 07:07:36 yamt Exp $");
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/callout.h>
75 #include <sys/buf.h>
76 #include <sys/bufq.h>
77 #include <sys/ioctl.h>
78 #include <sys/mtio.h>
79 #include <sys/file.h>
80 #include <sys/proc.h>
81 #include <sys/errno.h>
82 #include <sys/syslog.h>
83 #include <sys/tty.h>
84 #include <sys/kernel.h>
85 #include <sys/tprintf.h>
86 #include <sys/device.h>
87 #include <sys/conf.h>
88 
89 #include <hp300/dev/hpibvar.h>
90 
91 #include <hp300/dev/mtreg.h>
92 
93 static const struct mtinfo {
94 	u_short	hwid;
95 	char	*desc;
96 } mtinfo[] = {
97 	{ MT7978ID,	"7978"	},
98 	{ MT7979AID,	"7979A"	},
99 	{ MT7980ID,	"7980"	},
100 	{ MT7974AID,	"7974A"	},
101 };
102 static const int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
103 
104 struct	mt_softc {
105 	struct	device sc_dev;
106 	struct	callout sc_start_ch;
107 	struct	callout sc_intr_ch;
108 	int	sc_hpibno;	/* logical HPIB this slave it attached to */
109 	int	sc_slave;	/* HPIB slave address (0-6) */
110 	short	sc_flags;	/* see below */
111 	u_char	sc_lastdsj;	/* place for DSJ in mtreaddsj() */
112 	u_char	sc_lastecmd;	/* place for End Command in mtreaddsj() */
113 	short	sc_recvtimeo;	/* count of hpibsend timeouts to prevent hang */
114 	short	sc_statindex;	/* index for next sc_stat when MTF_STATTIMEO */
115 	struct	mt_stat sc_stat;/* status bytes last read from device */
116 	short	sc_density;	/* current density of tape (mtio.h format) */
117 	short	sc_type;	/* tape drive model (hardware IDs) */
118 	struct	hpibqueue sc_hq; /* HPIB device queue member */
119 	tpr_t	sc_ttyp;
120 	struct bufq_state sc_tab;/* buf queue */
121 	int	sc_active;
122 	struct buf sc_bufstore;	/* XXX buffer storage */
123 };
124 
125 #ifdef DEBUG
126 int	mtdebug = 0;
127 #define	dlog	if (mtdebug) log
128 #else
129 #define	dlog	if (0) log
130 #endif
131 
132 #define	UNIT(x)		(minor(x) & 3)
133 
134 #define B_CMD		B_XXX		/* command buf instead of data */
135 #define	b_cmd		b_blkno		/* blkno holds cmd when B_CMD */
136 
137 static int	mtmatch(struct device *, struct cfdata *, void *);
138 static void	mtattach(struct device *, struct device *, void *);
139 
140 CFATTACH_DECL(mt, sizeof(struct mt_softc),
141     mtmatch, mtattach, NULL, NULL);
142 
143 extern struct cfdriver mt_cd;
144 
145 static dev_type_open(mtopen);
146 static dev_type_close(mtclose);
147 static dev_type_read(mtread);
148 static dev_type_write(mtwrite);
149 static dev_type_ioctl(mtioctl);
150 static dev_type_strategy(mtstrategy);
151 
152 const struct bdevsw mt_bdevsw = {
153 	mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE
154 };
155 
156 const struct cdevsw mt_cdevsw = {
157 	mtopen, mtclose, mtread, mtwrite, mtioctl,
158 	nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
159 };
160 
161 static int	mtident(struct mt_softc *, struct hpibbus_attach_args *);
162 static void	mtustart(struct mt_softc *);
163 static int	mtreaddsj(struct mt_softc *, int);
164 static int	mtcommand(dev_t, int, int);
165 static void	spl_mtintr(void *);
166 static void	spl_mtstart(void *);
167 
168 static void	mtstart(void *);
169 static void	mtgo(void *);
170 static void	mtintr(void *);
171 
172 static int
173 mtmatch(struct device *parent, struct cfdata *match, void *aux)
174 {
175 	struct hpibbus_attach_args *ha = aux;
176 
177 	return (mtident(NULL, ha));
178 }
179 
180 static void
181 mtattach(struct device *parent, struct device *self, void *aux)
182 {
183 	struct mt_softc *sc = (struct mt_softc *)self;
184 	struct hpibbus_attach_args *ha = aux;
185 	int unit, hpibno, slave;
186 
187 	if (mtident(sc, ha) == 0) {
188 		printf("\n%s: impossible!\n", sc->sc_dev.dv_xname);
189 		return;
190 	}
191 
192 	unit = self->dv_unit;
193 	hpibno = parent->dv_unit;
194 	slave = ha->ha_slave;
195 
196 	bufq_alloc(&sc->sc_tab, BUFQ_FCFS);
197 	callout_init(&sc->sc_start_ch);
198 	callout_init(&sc->sc_intr_ch);
199 
200 	sc->sc_hpibno = hpibno;
201 	sc->sc_slave = slave;
202 	sc->sc_flags = MTF_EXISTS;
203 
204 	/* Initialize hpib job queue entry. */
205 	sc->sc_hq.hq_softc = sc;
206 	sc->sc_hq.hq_slave = sc->sc_slave;
207 	sc->sc_hq.hq_start = mtstart;
208 	sc->sc_hq.hq_go = mtgo;
209 	sc->sc_hq.hq_intr = mtintr;
210 }
211 
212 static int
213 mtident(struct mt_softc *sc, struct hpibbus_attach_args *ha)
214 {
215 	int i;
216 
217 	for (i = 0; i < nmtinfo; i++) {
218 		if (ha->ha_id == mtinfo[i].hwid) {
219 			if (sc != NULL) {
220 				sc->sc_type = mtinfo[i].hwid;
221 				printf(": %s tape\n", mtinfo[i].desc);
222 			}
223 			return (1);
224 		}
225 	}
226 	return (0);
227 }
228 
229 /*
230  * Perform a read of "Device Status Jump" register and update the
231  * status if necessary.  If status is read, the given "ecmd" is also
232  * performed, unless "ecmd" is zero.  Returns DSJ value, -1 on failure
233  * and -2 on "temporary" failure.
234  */
235 static int
236 mtreaddsj(struct mt_softc *sc, int ecmd)
237 {
238 	int retval;
239 
240 	if (sc->sc_flags & MTF_STATTIMEO)
241 		goto getstats;
242 	retval = hpibrecv(sc->sc_hpibno,
243 	    (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
244 	    MTT_DSJ, &(sc->sc_lastdsj), 1);
245 	sc->sc_flags &= ~MTF_DSJTIMEO;
246 	if (retval != 1) {
247 		dlog(LOG_DEBUG, "%s can't hpibrecv DSJ",
248 		    sc->sc_dev.dv_xname);
249 		if (sc->sc_recvtimeo == 0)
250 			sc->sc_recvtimeo = hz;
251 		if (--sc->sc_recvtimeo == 0)
252 			return (-1);
253 		if (retval == 0)
254 			sc->sc_flags |= MTF_DSJTIMEO;
255 		return (-2);
256 	}
257 	sc->sc_recvtimeo = 0;
258 	sc->sc_statindex = 0;
259 	dlog(LOG_DEBUG, "%s readdsj: 0x%x", sc->sc_dev.dv_xname,
260 	    sc->sc_lastdsj);
261 	sc->sc_lastecmd = ecmd;
262 	switch (sc->sc_lastdsj) {
263 	    case 0:
264 		if (ecmd & MTE_DSJ_FORCE)
265 			break;
266 		return (0);
267 
268 	    case 2:
269 		sc->sc_lastecmd = MTE_COMPLETE;
270 	    case 1:
271 		break;
272 
273 	    default:
274 		log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname,
275 		    sc->sc_lastdsj);
276 		return (-1);
277 	}
278     getstats:
279 	retval = hpibrecv(sc->sc_hpibno,
280 	    (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
281 	    MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
282 	    sizeof(sc->sc_stat) - sc->sc_statindex);
283 	sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
284 	if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
285 		if (sc->sc_recvtimeo == 0)
286 			sc->sc_recvtimeo = hz;
287 		if (--sc->sc_recvtimeo != 0) {
288 			if (retval >= 0) {
289 				sc->sc_statindex += retval;
290 				sc->sc_flags |= MTF_STATCONT;
291 			}
292 			sc->sc_flags |= MTF_STATTIMEO;
293 			return (-2);
294 		}
295 		log(LOG_ERR, "%s readdsj: can't read status",
296 		    sc->sc_dev.dv_xname);
297 		return (-1);
298 	}
299 	sc->sc_recvtimeo = 0;
300 	sc->sc_statindex = 0;
301 	dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x",
302 	    sc->sc_dev.dv_xname,
303 	    sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
304 	    sc->sc_stat4, sc->sc_stat5, sc->sc_stat6);
305 	if (sc->sc_lastecmd)
306 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave,
307 		    MTL_ECMD, &(sc->sc_lastecmd), 1);
308 	return ((int) sc->sc_lastdsj);
309 }
310 
311 static int
312 mtopen(dev_t dev, int flag, int mode, struct proc *p)
313 {
314 	int unit = UNIT(dev);
315 	struct mt_softc *sc;
316 	int req_den;
317 	int error;
318 
319 	if (unit >= mt_cd.cd_ndevs ||
320 	    (sc = mt_cd.cd_devs[unit]) == NULL ||
321 	    (sc->sc_flags & MTF_EXISTS) == 0)
322 		return (ENXIO);
323 
324 	dlog(LOG_DEBUG, "%s open: flags 0x%x", sc->sc_dev.dv_xname,
325 	    sc->sc_flags);
326 	if (sc->sc_flags & MTF_OPEN)
327 		return (EBUSY);
328 	sc->sc_flags |= MTF_OPEN;
329 	sc->sc_ttyp = tprintf_open(p);
330 	if ((sc->sc_flags & MTF_ALIVE) == 0) {
331 		error = mtcommand(dev, MTRESET, 0);
332 		if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
333 			goto errout;
334 		if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
335 			(void) mtcommand(dev, MTREW, 0);
336 	}
337 	for (;;) {
338 		if ((error = mtcommand(dev, MTNOP, 0)) != 0)
339 			goto errout;
340 		if (!(sc->sc_flags & MTF_REW))
341 			break;
342 		if (tsleep((caddr_t) &lbolt, PCATCH | (PZERO + 1),
343 		    "mt", 0) != 0) {
344 			error = EINTR;
345 			goto errout;
346 		}
347 	}
348 	if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
349 		error = EROFS;
350 		goto errout;
351 	}
352 	if (!(sc->sc_stat1 & SR1_ONLINE)) {
353 		uprintf("%s: not online\n", sc->sc_dev.dv_xname);
354 		error = EIO;
355 		goto errout;
356 	}
357 	/*
358 	 * Select density:
359 	 *  - find out what density the drive is set to
360 	 *	(i.e. the density of the current tape)
361 	 *  - if we are going to write
362 	 *    - if we're not at the beginning of the tape
363 	 *      - complain if we want to change densities
364 	 *    - otherwise, select the mtcommand to set the density
365 	 *
366 	 * If the drive doesn't support it then don't change the recorded
367 	 * density.
368 	 *
369 	 * The original MOREbsd code had these additional conditions
370 	 * for the mid-tape change
371 	 *
372 	 *	req_den != T_BADBPI &&
373 	 *	sc->sc_density != T_6250BPI
374 	 *
375 	 * which suggests that it would be possible to write multiple
376 	 * densities if req_den == T_BAD_BPI or the current tape
377 	 * density was 6250.  Testing of our 7980 suggests that the
378 	 * device cannot change densities mid-tape.
379 	 *
380 	 * ajv@comp.vuw.ac.nz
381 	 */
382 	sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
383 			 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
384 			 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
385 	req_den = (dev & T_DENSEL);
386 
387 	if (flag & FWRITE) {
388 		if (!(sc->sc_stat1 & SR1_BOT)) {
389 			if (sc->sc_density != req_den) {
390 				uprintf("%s: can't change density mid-tape\n",
391 				    sc->sc_dev.dv_xname);
392 				error = EIO;
393 				goto errout;
394 			}
395 		}
396 		else {
397 			int mtset_density =
398 			    (req_den == T_800BPI  ? MTSET800BPI : (
399 			     req_den == T_1600BPI ? MTSET1600BPI : (
400 			     req_den == T_6250BPI ? MTSET6250BPI : (
401 			     sc->sc_type == MT7980ID
402 						  ? MTSET6250DC
403 						  : MTSET6250BPI))));
404 			if (mtcommand(dev, mtset_density, 0) == 0)
405 				sc->sc_density = req_den;
406 		}
407 	}
408 	return (0);
409 errout:
410 	sc->sc_flags &= ~MTF_OPEN;
411 	return (error);
412 }
413 
414 static int
415 mtclose(dev_t dev, int flag, int fmt, struct proc *p)
416 {
417 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
418 
419 	if (sc->sc_flags & MTF_WRT) {
420 		(void) mtcommand(dev, MTWEOF, 2);
421 		(void) mtcommand(dev, MTBSF, 0);
422 	}
423 	if ((minor(dev) & T_NOREWIND) == 0)
424 		(void) mtcommand(dev, MTREW, 0);
425 	sc->sc_flags &= ~MTF_OPEN;
426 	tprintf_close(sc->sc_ttyp);
427 	return (0);
428 }
429 
430 static int
431 mtcommand(dev_t dev, int cmd, int cnt)
432 {
433 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
434 	struct buf *bp = &sc->sc_bufstore;
435 	int error = 0;
436 
437 #if 1
438 	if (bp->b_flags & B_BUSY)
439 		return (EBUSY);
440 #endif
441 	bp->b_cmd = cmd;
442 	bp->b_dev = dev;
443 	do {
444 		bp->b_flags = B_BUSY | B_CMD;
445 		mtstrategy(bp);
446 		biowait(bp);
447 		if (bp->b_flags & B_ERROR) {
448 			error = (int) (unsigned) bp->b_error;
449 			break;
450 		}
451 	} while (--cnt > 0);
452 #if 0
453 	bp->b_flags = 0 /*&= ~B_BUSY*/;
454 #else
455 	bp->b_flags &= ~B_BUSY;
456 #endif
457 	return (error);
458 }
459 
460 /*
461  * Only thing to check here is for legal record lengths (writes only).
462  */
463 static void
464 mtstrategy(struct buf *bp)
465 {
466 	struct mt_softc *sc;
467 	int unit;
468 	int s;
469 
470 	unit = UNIT(bp->b_dev);
471 	sc = mt_cd.cd_devs[unit];
472 	dlog(LOG_DEBUG, "%s strategy", sc->sc_dev.dv_xname);
473 	if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
474 #define WRITE_BITS_IGNORED	8
475 #if 0
476 		if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
477 			tprintf(sc->sc_ttyp,
478 				"%s: write record must be multiple of %d\n",
479 				sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED);
480 			goto error;
481 		}
482 #endif
483 		s = 16 * 1024;
484 		if (sc->sc_stat2 & SR2_LONGREC) {
485 			switch (sc->sc_density) {
486 			    case T_1600BPI:
487 				s = 32 * 1024;
488 				break;
489 
490 			    case T_6250BPI:
491 			    case T_BADBPI:
492 				s = 60 * 1024;
493 				break;
494 			}
495 		}
496 		if (bp->b_bcount > s) {
497 			tprintf(sc->sc_ttyp,
498 				"%s: write record (%d) too big: limit (%d)\n",
499 				sc->sc_dev.dv_xname, bp->b_bcount, s);
500 #if 0 /* XXX see above */
501 	    error:
502 #endif
503 			bp->b_flags |= B_ERROR;
504 			bp->b_error = EIO;
505 			biodone(bp);
506 			return;
507 		}
508 	}
509 	s = splbio();
510 	BUFQ_PUT(&sc->sc_tab, bp);
511 	if (sc->sc_active == 0) {
512 		sc->sc_active = 1;
513 		mtustart(sc);
514 	}
515 	splx(s);
516 }
517 
518 static void
519 mtustart(struct mt_softc *sc)
520 {
521 
522 	dlog(LOG_DEBUG, "%s ustart", sc->sc_dev.dv_xname);
523 	if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
524 		mtstart(sc);
525 }
526 
527 static void
528 spl_mtintr(void *arg)
529 {
530 	struct mt_softc *sc = arg;
531 	int s = splbio();
532 
533 	hpibppclear(sc->sc_hpibno);
534 	mtintr(sc);
535 	splx(s);
536 }
537 
538 static void
539 spl_mtstart(void *arg)
540 {
541 	int s = splbio();
542 
543 	mtstart(arg);
544 	splx(s);
545 }
546 
547 static void
548 mtstart(void *arg)
549 {
550 	struct mt_softc *sc = arg;
551 	struct buf *bp;
552 	short	cmdcount = 1;
553 	u_char	cmdbuf[2];
554 
555 	dlog(LOG_DEBUG, "%s start", sc->sc_dev.dv_xname);
556 	sc->sc_flags &= ~MTF_WRT;
557 	bp = BUFQ_PEEK(&sc->sc_tab);
558 	if ((sc->sc_flags & MTF_ALIVE) == 0 &&
559 	    ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
560 		goto fatalerror;
561 
562 	if (sc->sc_flags & MTF_REW) {
563 		if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
564 			goto stillrew;
565 		switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
566 		    case 0:
567 		    case 1:
568 		stillrew:
569 			if ((sc->sc_stat1 & SR1_BOT) ||
570 			    !(sc->sc_stat1 & SR1_ONLINE)) {
571 				sc->sc_flags &= ~MTF_REW;
572 				break;
573 			}
574 		    case -2:
575 			/*
576 			 * -2 means "timeout" reading DSJ, which is probably
577 			 * temporary.  This is considered OK when doing a NOP,
578 			 * but not otherwise.
579 			 */
580 			if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
581 				callout_reset(&sc->sc_start_ch, hz >> 5,
582 				    spl_mtstart, sc);
583 				return;
584 			}
585 		    case 2:
586 			if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
587 				bp->b_error = EBUSY;
588 				goto errdone;
589 			}
590 			goto done;
591 
592 		    default:
593 			goto fatalerror;
594 		}
595 	}
596 	if (bp->b_flags & B_CMD) {
597 		if (sc->sc_flags & MTF_PASTEOT) {
598 			switch(bp->b_cmd) {
599 			    case MTFSF:
600 			    case MTWEOF:
601 			    case MTFSR:
602 				bp->b_error = ENOSPC;
603 				goto errdone;
604 
605 			    case MTBSF:
606 			    case MTOFFL:
607 			    case MTBSR:
608 			    case MTREW:
609 				sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
610 				break;
611 			}
612 		}
613 		switch(bp->b_cmd) {
614 		    case MTFSF:
615 			if (sc->sc_flags & MTF_HITEOF)
616 				goto done;
617 			cmdbuf[0] = MTTC_FSF;
618 			break;
619 
620 		    case MTBSF:
621 			if (sc->sc_flags & MTF_HITBOF)
622 				goto done;
623 			cmdbuf[0] = MTTC_BSF;
624 			break;
625 
626 		    case MTOFFL:
627 			sc->sc_flags |= MTF_REW;
628 			cmdbuf[0] = MTTC_REWOFF;
629 			break;
630 
631 		    case MTWEOF:
632 			cmdbuf[0] = MTTC_WFM;
633 			break;
634 
635 		    case MTBSR:
636 			cmdbuf[0] = MTTC_BSR;
637 			break;
638 
639 		    case MTFSR:
640 			cmdbuf[0] = MTTC_FSR;
641 			break;
642 
643 		    case MTREW:
644 			sc->sc_flags |= MTF_REW;
645 			cmdbuf[0] = MTTC_REW;
646 			break;
647 
648 		    case MTNOP:
649 			/*
650 			 * NOP is supposed to set status bits.
651 			 * Force readdsj to do it.
652 			 */
653 			switch (mtreaddsj(sc,
654 			  MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
655 			    default:
656 				goto done;
657 
658 			    case -1:
659 				/*
660 				 * If this fails, perform a device clear
661 				 * to fix any protocol problems and (most
662 				 * likely) get the status.
663 				 */
664 				bp->b_cmd = MTRESET;
665 				break;
666 
667 			    case -2:
668 				callout_reset(&sc->sc_start_ch, hz >> 5,
669 				    spl_mtstart, sc);
670 				return;
671 			}
672 
673 		    case MTRESET:
674 			/*
675 			 * 1) selected device clear (send with "-2" secondary)
676 			 * 2) set timeout, then wait for "service request"
677 			 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
678 			 */
679 			if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
680 				log(LOG_ERR, "%s can't reset",
681 				    sc->sc_dev.dv_xname);
682 				goto fatalerror;
683 			}
684 			callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc);
685 			hpibawait(sc->sc_hpibno);
686 			return;
687 
688 		    case MTSET800BPI:
689 			cmdbuf[0] = MTTC_800;
690 			break;
691 
692 		    case MTSET1600BPI:
693 			cmdbuf[0] = MTTC_1600;
694 			break;
695 
696 		    case MTSET6250BPI:
697 			cmdbuf[0] = MTTC_6250;
698 			break;
699 
700 		    case MTSET6250DC:
701 			cmdbuf[0] = MTTC_DC6250;
702 			break;
703 		}
704 	} else {
705 		if (sc->sc_flags & MTF_PASTEOT) {
706 			bp->b_error = ENOSPC;
707 			goto errdone;
708 		}
709 		if (bp->b_flags & B_READ) {
710 			sc->sc_flags |= MTF_IO;
711 			cmdbuf[0] = MTTC_READ;
712 		} else {
713 			sc->sc_flags |= MTF_WRT | MTF_IO;
714 			cmdbuf[0] = MTTC_WRITE;
715 			cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
716 			cmdcount = 2;
717 		}
718 	}
719 	if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
720 	    == cmdcount) {
721 		if (sc->sc_flags & MTF_REW)
722 			goto done;
723 		hpibawait(sc->sc_hpibno);
724 		return;
725 	}
726 fatalerror:
727 	/*
728 	 * If anything fails, the drive is probably hosed, so mark it not
729 	 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
730 	 * if, last we heard, it was REWinding, remember that).
731 	 */
732 	sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
733 	bp->b_error = EIO;
734 errdone:
735 	bp->b_flags |= B_ERROR;
736 done:
737 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
738 	(void)BUFQ_GET(&sc->sc_tab);
739 	biodone(bp);
740 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
741 	if ((bp = BUFQ_PEEK(&sc->sc_tab)) == NULL)
742 		sc->sc_active = 0;
743 	else
744 		mtustart(sc);
745 }
746 
747 /*
748  * The Utah code had a bug which meant that the driver was unable to read.
749  * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
750  *   -- ajv@comp.vuw.ac.nz
751  */
752 static void
753 mtgo(void *arg)
754 {
755 	struct mt_softc *sc = arg;
756 	struct buf *bp;
757 	int rw;
758 
759 	dlog(LOG_DEBUG, "%s go", sc->sc_dev.dv_xname);
760 	bp = BUFQ_PEEK(&sc->sc_tab);
761 	rw = bp->b_flags & B_READ;
762 	hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
763 	    bp->b_data, bp->b_bcount, rw, rw != 0);
764 }
765 
766 static void
767 mtintr(void *arg)
768 {
769 	struct mt_softc *sc = arg;
770 	struct buf *bp;
771 	int i;
772 	u_char cmdbuf[4];
773 
774 	bp = BUFQ_PEEK(&sc->sc_tab);
775 	if (bp == NULL) {
776 		log(LOG_ERR, "%s intr: bp == NULL", sc->sc_dev.dv_xname);
777 		return;
778 	}
779 
780 	dlog(LOG_DEBUG, "%s intr", sc->sc_dev.dv_xname);
781 
782 	/*
783 	 * Some operation completed.  Read status bytes and report errors.
784 	 * Clear EOF flags here `cause they're set once on specific conditions
785 	 * below when a command succeeds.
786 	 * A DSJ of 2 always means keep waiting.  If the command was READ
787 	 * (and we're in data DMA phase) stop data transfer first.
788 	 */
789 	sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
790 	if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
791 	    !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
792 		cmdbuf[0] = MTE_STOP;
793 		(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
794 	}
795 	switch (mtreaddsj(sc, 0)) {
796 	    case 0:
797 		break;
798 
799 	    case 1:
800 		/*
801 		 * If we're in the middle of a READ/WRITE and have yet to
802 		 * start the data transfer, a DSJ of one should terminate it.
803 		 */
804 		sc->sc_flags &= ~MTF_IO;
805 		break;
806 
807 	    case 2:
808 		(void) hpibawait(sc->sc_hpibno);
809 		return;
810 
811 	    case -2:
812 		/*
813 		 * -2 means that the drive failed to respond quickly enough
814 		 * to the request for DSJ.  It's probably just "busy" figuring
815 		 * it out and will know in a little bit...
816 		 */
817 		callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc);
818 		return;
819 
820 	    default:
821 		log(LOG_ERR, "%s intr: can't get drive stat",
822 		    sc->sc_dev.dv_xname);
823 		goto error;
824 	}
825 	if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
826 		i = sc->sc_stat4 & SR4_ERCLMASK;
827 		log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
828 			sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" :
829 			(i == SR4_PROTOCOL ? "protocol" :
830 			(i == SR4_SELFTEST ? "selftest" : "unknown")),
831 			sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
832 			sc->sc_stat3, sc->sc_stat5);
833 
834 		if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
835 			callout_stop(&sc->sc_intr_ch);
836 		if (sc->sc_stat3 & SR3_POWERUP)
837 			sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
838 		goto error;
839 	}
840 	/*
841 	 * Report and clear any soft errors.
842 	 */
843 	if (sc->sc_stat1 & SR1_SOFTERR) {
844 		log(LOG_WARNING, "%s: soft error, retry %d\n",
845 			sc->sc_dev.dv_xname, sc->sc_stat4 & SR4_RETRYMASK);
846 		sc->sc_stat1 &= ~SR1_SOFTERR;
847 	}
848 	/*
849 	 * We've initiated a read or write, but haven't actually started to
850 	 * DMA the data yet.  At this point, the drive's ready.
851 	 */
852 	if (sc->sc_flags & MTF_IO) {
853 		sc->sc_flags &= ~MTF_IO;
854 		if (hpibustart(sc->sc_hpibno))
855 			mtgo(sc);
856 		return;
857 	}
858 	/*
859 	 * Check for End Of Tape - we're allowed to hit EOT and then write (or
860 	 * read) one more record.  If we get here and have not already hit EOT,
861 	 * return ENOSPC to inform the process that it's hit it.  If we get
862 	 * here and HAVE already hit EOT, don't allow any more operations that
863 	 * move the tape forward.
864 	 */
865 	if (sc->sc_stat1 & SR1_EOT) {
866 		if (sc->sc_flags & MTF_ATEOT)
867 			sc->sc_flags |= MTF_PASTEOT;
868 		else {
869 			bp->b_flags |= B_ERROR;
870 			bp->b_error = ENOSPC;
871 			sc->sc_flags |= MTF_ATEOT;
872 		}
873 	}
874 	/*
875 	 * If a motion command was being executed, check for Tape Marks.
876 	 * If we were doing data, make sure we got the right amount, and
877 	 * check for hitting tape marks on reads.
878 	 */
879 	if (bp->b_flags & B_CMD) {
880 		if (sc->sc_stat1 & SR1_EOF) {
881 			if (bp->b_cmd == MTFSR)
882 				sc->sc_flags |= MTF_HITEOF;
883 			if (bp->b_cmd == MTBSR)
884 				sc->sc_flags |= MTF_HITBOF;
885 		}
886 		if (bp->b_cmd == MTRESET) {
887 			callout_stop(&sc->sc_intr_ch);
888 			sc->sc_flags |= MTF_ALIVE;
889 		}
890 	} else {
891 		i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
892 		if (i != 2) {
893 			log(LOG_ERR, "%s intr: can't get xfer length\n",
894 			    sc->sc_dev.dv_xname);
895 			goto error;
896 		}
897 		i = (int) *((u_short *) cmdbuf);
898 		if (i <= bp->b_bcount) {
899 			if (i == 0)
900 				sc->sc_flags |= MTF_HITEOF;
901 			bp->b_resid = bp->b_bcount - i;
902 			dlog(LOG_DEBUG, "%s intr: bcount %d, resid %d",
903 			    sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid);
904 		} else {
905 			tprintf(sc->sc_ttyp,
906 				"%s: record (%d) larger than wanted (%d)\n",
907 				sc->sc_dev.dv_xname, i, bp->b_bcount);
908     error:
909 			sc->sc_flags &= ~MTF_IO;
910 			bp->b_error = EIO;
911 			bp->b_flags |= B_ERROR;
912 		}
913 	}
914 	/*
915 	 * The operation is completely done.
916 	 * Let the drive know with an END command.
917 	 */
918 	cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
919 	(void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
920 	bp->b_flags &= ~B_CMD;
921 	(void)BUFQ_GET(&sc->sc_tab);
922 	biodone(bp);
923 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
924 	if (BUFQ_PEEK(&sc->sc_tab) == NULL)
925 		sc->sc_active = 0;
926 	else
927 		mtustart(sc);
928 }
929 
930 static int
931 mtread(dev_t dev, struct uio *uio, int flags)
932 {
933 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
934 
935 	return(physio(mtstrategy, &sc->sc_bufstore,
936 	    dev, B_READ, minphys, uio));
937 }
938 
939 static int
940 mtwrite(dev_t dev, struct uio *uio, int flags)
941 {
942 	struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
943 
944 	return(physio(mtstrategy, &sc->sc_bufstore,
945 	    dev, B_WRITE, minphys, uio));
946 }
947 
948 static int
949 mtioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
950 {
951 	struct mtop *op;
952 	int cnt;
953 
954 	switch (cmd) {
955 	    case MTIOCTOP:
956 		op = (struct mtop *)data;
957 		switch(op->mt_op) {
958 		    case MTWEOF:
959 		    case MTFSF:
960 		    case MTBSR:
961 		    case MTBSF:
962 		    case MTFSR:
963 			cnt = op->mt_count;
964 			break;
965 
966 		    case MTOFFL:
967 		    case MTREW:
968 		    case MTNOP:
969 			cnt = 0;
970 			break;
971 
972 		    default:
973 			return (EINVAL);
974 		}
975 		return (mtcommand(dev, op->mt_op, cnt));
976 
977 	    case MTIOCGET:
978 		break;
979 
980 	    default:
981 		return (EINVAL);
982 	}
983 	return (0);
984 }
985