xref: /netbsd-src/sys/arch/hp300/dev/ct.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: ct.c,v 1.26 2001/05/27 09:09:05 kleink 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) 1982, 1990, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)ct.c	8.2 (Berkeley) 1/12/94
72  */
73 
74 /*
75  * CS80 cartridge tape driver (9144, 88140, 9145)
76  *
77  * Reminder:
78  *	C_CC bit (character count option) when used in the CS/80 command
79  *	'set options' will cause the tape not to stream.
80  *
81  * TODO:
82  *	make filesystem compatible
83  *	make block mode work according to mtio(4) spec. (if possible)
84  *	merge with cs80 disk driver
85  *	finish support of 9145
86  */
87 
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/buf.h>
91 #include <sys/conf.h>
92 #include <sys/device.h>
93 #include <sys/ioctl.h>
94 #include <sys/mtio.h>
95 #include <sys/proc.h>
96 #include <sys/tprintf.h>
97 
98 #include <hp300/dev/hpibvar.h>
99 
100 #include <hp300/dev/ctreg.h>
101 
102 /* number of eof marks to remember */
103 #define EOFS	128
104 
105 struct	ct_softc {
106 	struct	device sc_dev;
107 	int	sc_slave;		/* HP-IB slave ID */
108 	int	sc_punit;		/* physical unit */
109 	struct	ct_iocmd sc_ioc;
110 	struct	ct_rscmd sc_rsc;
111 	struct	ct_stat sc_stat;
112 	struct	ct_ssmcmd sc_ssmc;
113 	struct	ct_srcmd sc_src;
114 	struct	ct_soptcmd sc_soptc;
115 	struct	ct_ulcmd sc_ul;
116 	struct	ct_wfmcmd sc_wfm;
117 	struct	ct_clearcmd sc_clear;
118 	struct	buf_queue sc_tab;
119 	int	sc_active;
120 	struct	buf *sc_bp;
121 	struct	buf sc_bufstore;	/* XXX */
122 	int	sc_blkno;
123 	int	sc_cmd;
124 	int	sc_resid;
125 	char	*sc_addr;
126 	int	sc_flags;
127 	short	sc_type;
128 	tpr_t	sc_tpr;
129 	struct	hpibqueue sc_hq;	/* entry on hpib job queue */
130 	int	sc_eofp;
131 	int	sc_eofs[EOFS];
132 };
133 
134 /* flags */
135 #define	CTF_OPEN	0x01
136 #define	CTF_ALIVE	0x02
137 #define	CTF_WRT		0x04
138 #define	CTF_CMD		0x08
139 #define	CTF_IO		0x10
140 #define	CTF_BEOF	0x20
141 #define	CTF_AEOF	0x40
142 #define	CTF_EOT		0x80
143 #define	CTF_STATWAIT	0x100
144 #define CTF_CANSTREAM	0x200
145 #define	CTF_WRTTN	0x400
146 
147 int	ctmatch __P((struct device *, struct cfdata *, void *));
148 void	ctattach __P((struct device *, struct device *, void *));
149 
150 struct cfattach ct_ca = {
151 	sizeof(struct ct_softc), ctmatch, ctattach
152 };
153 
154 extern struct cfdriver ct_cd;
155 
156 int	ctident __P((struct device *, struct ct_softc *,
157 	    struct hpibbus_attach_args *));
158 
159 void	ctreset __P((struct ct_softc *));
160 void	ctaddeof __P((struct ct_softc *));
161 void	ctustart __P((struct ct_softc *));
162 void	cteof __P((struct ct_softc *, struct buf *));
163 void	ctdone __P((struct ct_softc *, struct buf *));
164 
165 void	ctstart __P((void *));
166 void	ctgo __P((void *));
167 void	ctintr __P((void *));
168 
169 void	ctcommand __P((dev_t, int, int));
170 
171 cdev_decl(ct);
172 bdev_decl(ct);
173 
174 struct	ctinfo {
175 	short	hwid;
176 	short	punit;
177 	char	*desc;
178 } ctinfo[] = {
179 	{ CT7946ID,	1,	"7946A"	},
180 	{ CT7912PID,	1,	"7912P"	},
181 	{ CT7914PID,	1,	"7914P"	},
182 	{ CT9144ID,	0,	"9144"	},
183 	{ CT9145ID,	0,	"9145"	},
184 	{ CT35401ID,	0,	"35401A"},
185 };
186 int	nctinfo = sizeof(ctinfo) / sizeof(ctinfo[0]);
187 
188 #define	CT_NOREW	4
189 #define	CT_STREAM	8
190 #define	UNIT(x)		(minor(x) & 3)
191 #define	ctpunit(x)	((x) & 7)
192 
193 #ifdef DEBUG
194 int ctdebug = 0;
195 #define CDB_FILES	0x01
196 #define CT_BSF		0x02
197 #endif
198 
199 int
200 ctmatch(parent, match, aux)
201 	struct device *parent;
202 	struct cfdata *match;
203 	void *aux;
204 {
205 	struct hpibbus_attach_args *ha = aux;
206 
207 	return (ctident(parent, NULL, ha));
208 }
209 
210 void
211 ctattach(parent, self, aux)
212 	struct device *parent, *self;
213 	void *aux;
214 {
215 	struct ct_softc *sc = (struct ct_softc *)self;
216 	struct hpibbus_attach_args *ha = aux;
217 
218 	if (ctident(parent, sc, ha) == 0) {
219 		printf("\n%s: didn't respond to describe command!\n",
220 		    sc->sc_dev.dv_xname);
221 		return;
222 	}
223 
224 	sc->sc_slave = ha->ha_slave;
225 	sc->sc_punit = ha->ha_punit;
226 
227 	BUFQ_INIT(&sc->sc_tab);
228 
229 	/* Initialize hpib job queue entry. */
230 	sc->sc_hq.hq_softc = sc;
231 	sc->sc_hq.hq_slave = sc->sc_slave;
232 	sc->sc_hq.hq_start = ctstart;
233 	sc->sc_hq.hq_go = ctgo;
234 	sc->sc_hq.hq_intr = ctintr;
235 
236 	ctreset(sc);
237 	sc->sc_flags |= CTF_ALIVE;
238 }
239 
240 int
241 ctident(parent, sc, ha)
242 	struct device *parent;
243 	struct ct_softc *sc;
244 	struct hpibbus_attach_args *ha;
245 {
246 	struct ct_describe desc;
247 	u_char stat, cmd[3];
248 	char name[7];
249 	int i, id, n, type, canstream;
250 
251 	type = canstream = 0;
252 
253 	/* Verify that we have a CS80 device. */
254 	if ((ha->ha_id & 0x200) == 0)
255 		return (0);
256 
257 	/* Is it one of the tapes we support? */
258 	for (id = 0; id < nctinfo; id++)
259 		if (ha->ha_id == ctinfo[id].hwid)
260 			break;
261 	if (id == nctinfo)
262 		return (0);
263 
264 	ha->ha_punit = ctinfo[id].punit;
265 
266 	/*
267 	 * So far, so good.  Get drive parameters.  Note command
268 	 * is always issued to unit 0.
269 	 */
270 	cmd[0] = C_SUNIT(0);
271 	cmd[1] = C_SVOL(0);
272 	cmd[2] = C_DESC;
273 	hpibsend(parent->dv_unit, ha->ha_slave, C_CMD, cmd, sizeof(cmd));
274 	hpibrecv(parent->dv_unit, ha->ha_slave, C_EXEC, &desc, 37);
275 	hpibrecv(parent->dv_unit, ha->ha_slave, C_QSTAT, &stat, sizeof(stat));
276 
277 	bzero(name, sizeof(name));
278 	if (stat == 0) {
279 		n = desc.d_name;
280 		for (i = 5; i >= 0; i--) {
281 			name[i] = (n & 0xf) + '0';
282 			n >>= 4;
283 		}
284 	}
285 
286 	switch (ha->ha_id) {
287 	case CT7946ID:
288 		if (bcmp(name, "079450", 6) == 0)
289 			return (0);		/* not really a 7946 */
290 		/* fall into... */
291 	case CT9144ID:
292 	case CT9145ID:
293 	case CT35401ID:
294 		type = CT9144;
295 		canstream = 1;
296 		break;
297 
298 	case CT7912PID:
299 	case CT7914PID:
300 		type = CT88140;
301 		break;
302 	}
303 
304 	if (sc != NULL) {
305 		sc->sc_type = type;
306 		sc->sc_flags = canstream ? CTF_CANSTREAM : 0;
307 		printf(": %s %stape\n", ctinfo[id].desc,
308 		    canstream ? "streaming " : "");
309 	}
310 
311 	return (1);
312 }
313 
314 void
315 ctreset(sc)
316 	struct ct_softc *sc;
317 {
318 	int ctlr, slave;
319 	u_char stat;
320 
321 	ctlr = sc->sc_dev.dv_parent->dv_unit;
322 	slave = sc->sc_slave;
323 
324 	sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
325 	sc->sc_clear.cmd = C_CLEAR;
326 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
327 	hpibswait(ctlr, slave);
328 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
329 
330 	sc->sc_src.unit = C_SUNIT(CTCTLR);
331 	sc->sc_src.nop = C_NOP;
332 	sc->sc_src.cmd = C_SREL;
333 	sc->sc_src.param = C_REL;
334 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
335 	hpibswait(ctlr, slave);
336 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
337 
338 	sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
339 	sc->sc_ssmc.cmd = C_SSM;
340 	sc->sc_ssmc.refm = REF_MASK;
341 	sc->sc_ssmc.fefm = FEF_MASK;
342 	sc->sc_ssmc.aefm = AEF_MASK;
343 	sc->sc_ssmc.iefm = IEF_MASK;
344 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
345 	hpibswait(ctlr, slave);
346 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
347 
348 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
349 	sc->sc_soptc.nop = C_NOP;
350 	sc->sc_soptc.cmd = C_SOPT;
351 	sc->sc_soptc.opt = C_SPAR;
352 	hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
353 	hpibswait(ctlr, slave);
354 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
355 }
356 
357 /*ARGSUSED*/
358 int
359 ctopen(dev, flag, type, p)
360 	dev_t dev;
361 	int flag, type;
362 	struct proc *p;
363 {
364 	struct ct_softc *sc;
365 	u_char stat;
366 	int cc, ctlr, slave;
367 
368 	if (UNIT(dev) >= ct_cd.cd_ndevs ||
369 	    (sc = ct_cd.cd_devs[UNIT(dev)]) == NULL ||
370 	    (sc->sc_flags & CTF_ALIVE) == 0)
371 		return (ENXIO);
372 
373 	if (sc->sc_flags & CTF_OPEN)
374 		return (EBUSY);
375 
376 	ctlr = sc->sc_dev.dv_parent->dv_unit;
377 	slave = sc->sc_slave;
378 
379 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
380 	sc->sc_soptc.nop = C_NOP;
381 	sc->sc_soptc.cmd = C_SOPT;
382 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
383 		sc->sc_soptc.opt = C_SPAR | C_IMRPT;
384 	else
385 		sc->sc_soptc.opt = C_SPAR;
386 
387 	/*
388 	 * Check the return of hpibsend() and hpibswait().
389 	 * Drive could be loading/unloading a tape. If not checked,
390 	 * driver hangs.
391 	 */
392 	cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
393 	if (cc != sizeof(sc->sc_soptc))
394 		return (EBUSY);
395 
396 	hpibswait(ctlr, slave);
397 	cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
398 	if (cc != sizeof(stat))
399 		return(EBUSY);
400 
401 	sc->sc_tpr = tprintf_open(p);
402 	sc->sc_flags |= CTF_OPEN;
403 	return(0);
404 }
405 
406 /*ARGSUSED*/
407 int
408 ctclose(dev, flag, fmt, p)
409 	dev_t dev;
410 	int flag, fmt;
411 	struct proc *p;
412 {
413 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
414 
415 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
416 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
417 		ctcommand(dev, MTWEOF, 2);
418 		ctcommand(dev, MTBSR, 1);
419 		if (sc->sc_eofp == EOFS - 1)
420 			sc->sc_eofs[EOFS - 1]--;
421 		else
422 			sc->sc_eofp--;
423 #ifdef DEBUG
424 		if(ctdebug & CT_BSF)
425 			printf("%s: ctclose backup eofs prt %d blk %d\n",
426 			       sc->sc_dev.dv_xname, sc->sc_eofp,
427 			       sc->sc_eofs[sc->sc_eofp]);
428 #endif
429 	}
430 	if ((minor(dev) & CT_NOREW) == 0)
431 		ctcommand(dev, MTREW, 1);
432 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
433 	tprintf_close(sc->sc_tpr);
434 #ifdef DEBUG
435 	if (ctdebug & CDB_FILES)
436 		printf("ctclose: flags %x\n", sc->sc_flags);
437 #endif
438 	return(0);	/* XXX */
439 }
440 
441 void
442 ctcommand(dev, cmd, cnt)
443 	dev_t dev;
444 	int cmd;
445 	int cnt;
446 {
447 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
448 	struct buf *bp = &sc->sc_bufstore;
449 	struct buf *nbp = 0;
450 
451 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
452 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
453 		ctcommand(dev, MTREW, 1);
454 		ctcommand(dev, MTFSF, cnt);
455 		cnt = 2;
456 		cmd = MTBSR;
457 	}
458 
459 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
460 		cnt = 1;
461 		cmd = MTREW;
462 	}
463 
464 	sc->sc_flags |= CTF_CMD;
465 	sc->sc_bp = bp;
466 	sc->sc_cmd = cmd;
467 	bp->b_dev = dev;
468 	if (cmd == MTFSF) {
469 		nbp = (struct buf *)geteblk(MAXBSIZE);
470 		bp->b_data = nbp->b_data;
471 		bp->b_bcount = MAXBSIZE;
472 	}
473 
474 	while (cnt-- > 0) {
475 		bp->b_flags = B_BUSY;
476 		if (cmd == MTBSF) {
477 			sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
478 			sc->sc_eofp--;
479 #ifdef DEBUG
480 			if (ctdebug & CT_BSF)
481 				printf("%s: backup eof pos %d blk %d\n",
482 				    sc->sc_dev.dv_xname, sc->sc_eofp,
483 				    sc->sc_eofs[sc->sc_eofp]);
484 #endif
485 		}
486 		ctstrategy(bp);
487 		biowait(bp);
488 	}
489 	bp->b_flags = 0;
490 	sc->sc_flags &= ~CTF_CMD;
491 	if (nbp)
492 		brelse(nbp);
493 }
494 
495 void
496 ctstrategy(bp)
497 	struct buf *bp;
498 {
499 	int s, unit;
500 	struct ct_softc *sc;
501 
502 	unit = UNIT(bp->b_dev);
503 	sc = ct_cd.cd_devs[unit];
504 
505 	s = splbio();
506 	BUFQ_INSERT_TAIL(&sc->sc_tab, bp);
507 	if (sc->sc_active == 0) {
508 		sc->sc_active = 1;
509 		ctustart(sc);
510 	}
511 	splx(s);
512 }
513 
514 void
515 ctustart(sc)
516 	struct ct_softc *sc;
517 {
518 	struct buf *bp;
519 
520 	bp = BUFQ_FIRST(&sc->sc_tab);
521 	sc->sc_addr = bp->b_data;
522 	sc->sc_resid = bp->b_bcount;
523 	if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
524 		ctstart(sc);
525 }
526 
527 void
528 ctstart(arg)
529 	void *arg;
530 {
531 	struct ct_softc *sc = arg;
532 	struct buf *bp;
533 	int i, ctlr, slave;
534 
535 	ctlr = sc->sc_dev.dv_parent->dv_unit;
536 	slave = sc->sc_slave;
537 
538 	bp = BUFQ_FIRST(&sc->sc_tab);
539 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
540 		switch(sc->sc_cmd) {
541 		case MTFSF:
542 			bp->b_flags |= B_READ;
543 			goto mustio;
544 
545 		case MTBSF:
546 			goto gotaddr;
547 
548 		case MTOFFL:
549 			sc->sc_blkno = 0;
550 			sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
551 			sc->sc_ul.cmd = C_UNLOAD;
552 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ul,
553 			    sizeof(sc->sc_ul));
554 			break;
555 
556 		case MTWEOF:
557 			sc->sc_blkno++;
558 			sc->sc_flags |= CTF_WRT;
559 			sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
560 			sc->sc_wfm.cmd = C_WFM;
561 			hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm,
562 			    sizeof(sc->sc_wfm));
563 			ctaddeof(sc);
564 			break;
565 
566 		case MTBSR:
567 			sc->sc_blkno--;
568 			goto gotaddr;
569 
570 		case MTFSR:
571 			sc->sc_blkno++;
572 			goto gotaddr;
573 
574 		case MTREW:
575 			sc->sc_blkno = 0;
576 #ifdef DEBUG
577 			if(ctdebug & CT_BSF)
578 				printf("%s: clearing eofs\n",
579 				    sc->sc_dev.dv_xname);
580 #endif
581 			for (i=0; i<EOFS; i++)
582 				sc->sc_eofs[i] = 0;
583 			sc->sc_eofp = 0;
584 
585 gotaddr:
586 			sc->sc_ioc.saddr = C_SADDR;
587 			sc->sc_ioc.addr0 = 0;
588 			sc->sc_ioc.addr = sc->sc_blkno;
589 			sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
590 			sc->sc_ioc.nop2 = C_NOP;
591 			sc->sc_ioc.slen = C_SLEN;
592 			sc->sc_ioc.len = 0;
593 			sc->sc_ioc.nop3 = C_NOP;
594 			sc->sc_ioc.cmd = C_READ;
595 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc,
596 			    sizeof(sc->sc_ioc));
597 			break;
598 		}
599 	} else {
600 mustio:
601 		if ((bp->b_flags & B_READ) &&
602 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
603 #ifdef DEBUG
604 			if (ctdebug & CDB_FILES)
605 				printf("ctstart: before flags %x\n",
606 				    sc->sc_flags);
607 #endif
608 			if (sc->sc_flags & CTF_BEOF) {
609 				sc->sc_flags &= ~CTF_BEOF;
610 				sc->sc_flags |= CTF_AEOF;
611 #ifdef DEBUG
612 				if (ctdebug & CDB_FILES)
613 					printf("ctstart: after flags %x\n",
614 					    sc->sc_flags);
615 #endif
616 			}
617 			bp->b_resid = bp->b_bcount;
618 			ctdone(sc, bp);
619 			return;
620 		}
621 		sc->sc_flags |= CTF_IO;
622 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
623 		sc->sc_ioc.saddr = C_SADDR;
624 		sc->sc_ioc.addr0 = 0;
625 		sc->sc_ioc.addr = sc->sc_blkno;
626 		sc->sc_ioc.nop2 = C_NOP;
627 		sc->sc_ioc.slen = C_SLEN;
628 		sc->sc_ioc.len = sc->sc_resid;
629 		sc->sc_ioc.nop3 = C_NOP;
630 		if (bp->b_flags & B_READ)
631 			sc->sc_ioc.cmd = C_READ;
632 		else {
633 			sc->sc_ioc.cmd = C_WRITE;
634 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
635 		}
636 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
637 	}
638 	hpibawait(ctlr);
639 }
640 
641 void
642 ctgo(arg)
643 	void *arg;
644 {
645 	struct ct_softc *sc = arg;
646 	struct buf *bp;
647 	int rw;
648 
649 	bp = BUFQ_FIRST(&sc->sc_tab);
650 	rw = bp->b_flags & B_READ;
651 	hpibgo(sc->sc_dev.dv_parent->dv_unit, sc->sc_slave, C_EXEC,
652 	    sc->sc_addr, sc->sc_resid, rw, rw != 0);
653 }
654 
655 /*
656  * Hideous grue to handle EOF/EOT (mostly for reads)
657  */
658 void
659 cteof(sc, bp)
660 	struct ct_softc *sc;
661 	struct buf *bp;
662 {
663 	long blks;
664 
665 	/*
666 	 * EOT on a write is an error.
667 	 */
668 	if ((bp->b_flags & B_READ) == 0) {
669 		bp->b_resid = bp->b_bcount;
670 		bp->b_flags |= B_ERROR;
671 		bp->b_error = ENOSPC;
672 		sc->sc_flags |= CTF_EOT;
673 		return;
674 	}
675 	/*
676 	 * Use returned block position to determine how many blocks
677 	 * we really read and update b_resid.
678 	 */
679 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
680 #ifdef DEBUG
681 	if (ctdebug & CDB_FILES)
682 		printf("cteof: bc %ld oblk %d nblk %ld read %ld, resid %ld\n",
683 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
684 		       blks, bp->b_bcount - CTKTOB(blks));
685 #endif
686 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
687 		blks = 0;
688 		sc->sc_blkno++;
689 	}
690 	else {
691 		sc->sc_blkno = sc->sc_stat.c_blk;
692 	}
693 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
694 	/*
695 	 * If we are at physical EOV or were after an EOF,
696 	 * we are now at logical EOT.
697 	 */
698 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
699 	    (sc->sc_flags & CTF_AEOF)) {
700 		sc->sc_flags |= CTF_EOT;
701 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
702 	}
703 	/*
704 	 * If we were before an EOF or we have just completed a FSF,
705 	 * we are now after EOF.
706 	 */
707 	else if ((sc->sc_flags & CTF_BEOF) ||
708 		 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) {
709 		sc->sc_flags |= CTF_AEOF;
710 		sc->sc_flags &= ~CTF_BEOF;
711 	}
712 	/*
713 	 * Otherwise if we read something we are now before EOF
714 	 * (and no longer after EOF).
715 	 */
716 	else if (blks) {
717 		sc->sc_flags |= CTF_BEOF;
718 		sc->sc_flags &= ~CTF_AEOF;
719 	}
720 	/*
721 	 * Finally, if we didn't read anything we just passed an EOF
722 	 */
723 	else
724 		sc->sc_flags |= CTF_AEOF;
725 #ifdef DEBUG
726 	if (ctdebug & CDB_FILES)
727 		printf("cteof: leaving flags %x\n", sc->sc_flags);
728 #endif
729 }
730 
731 /* ARGSUSED */
732 void
733 ctintr(arg)
734 	void *arg;
735 {
736 	struct ct_softc *sc = arg;
737 	struct buf *bp;
738 	u_char stat;
739 	int ctlr, slave, unit;
740 
741 	ctlr = sc->sc_dev.dv_parent->dv_unit;
742 	slave = sc->sc_slave;
743 	unit = sc->sc_dev.dv_unit;
744 
745 	bp = BUFQ_FIRST(&sc->sc_tab);
746 	if (bp == NULL) {
747 		printf("%s: bp == NULL\n", sc->sc_dev.dv_xname);
748 		return;
749 	}
750 	if (sc->sc_flags & CTF_IO) {
751 		sc->sc_flags &= ~CTF_IO;
752 		if (hpibustart(ctlr))
753 			ctgo(sc);
754 		return;
755 	}
756 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
757 		if (hpibpptest(ctlr, slave) == 0) {
758 			sc->sc_flags |= CTF_STATWAIT;
759 			hpibawait(ctlr);
760 			return;
761 		}
762 	} else
763 		sc->sc_flags &= ~CTF_STATWAIT;
764 	hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
765 #ifdef DEBUG
766 	if (ctdebug & CDB_FILES)
767 		printf("ctintr: before flags %x\n", sc->sc_flags);
768 #endif
769 	if (stat) {
770 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
771 		sc->sc_rsc.cmd = C_STATUS;
772 		hpibsend(ctlr, slave, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
773 		hpibrecv(ctlr, slave, C_EXEC, &sc->sc_stat,
774 		    sizeof(sc->sc_stat));
775 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
776 #ifdef DEBUG
777 		if (ctdebug & CDB_FILES)
778 			printf("ctintr: return stat 0x%x, A%x F%x blk %ld\n",
779 			       stat, sc->sc_stat.c_aef,
780 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
781 #endif
782 		if (stat == 0) {
783 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
784 				cteof(sc, bp);
785 				ctaddeof(sc);
786 				goto done;
787 			}
788 			if (sc->sc_stat.c_fef & FEF_PF) {
789 				ctreset(sc);
790 				ctstart(sc);
791 				return;
792 			}
793 			if (sc->sc_stat.c_fef & FEF_REXMT) {
794 				ctstart(sc);
795 				return;
796 			}
797 			if (sc->sc_stat.c_aef & 0x5800) {
798 				if (sc->sc_stat.c_aef & 0x4000)
799 					tprintf(sc->sc_tpr,
800 						"%s: uninitialized media\n",
801 						sc->sc_dev.dv_xname);
802 				if (sc->sc_stat.c_aef & 0x1000)
803 					tprintf(sc->sc_tpr,
804 						"%s: not ready\n",
805 						sc->sc_dev.dv_xname);
806 				if (sc->sc_stat.c_aef & 0x0800)
807 					tprintf(sc->sc_tpr,
808 						"%s: write protect\n",
809 						sc->sc_dev.dv_xname);
810 			} else {
811 				printf("%s err: v%d u%d ru%d bn%ld, ",
812 				       sc->sc_dev.dv_xname,
813 				       (sc->sc_stat.c_vu>>4)&0xF,
814 				       sc->sc_stat.c_vu&0xF,
815 				       sc->sc_stat.c_pend,
816 				       sc->sc_stat.c_blk);
817 				printf("R0x%x F0x%x A0x%x I0x%x\n",
818 				       sc->sc_stat.c_ref,
819 				       sc->sc_stat.c_fef,
820 				       sc->sc_stat.c_aef,
821 				       sc->sc_stat.c_ief);
822 			}
823 		} else
824 			printf("%s: request status failed\n",
825 			    sc->sc_dev.dv_xname);
826 		bp->b_flags |= B_ERROR;
827 		bp->b_error = EIO;
828 		goto done;
829 	} else
830 		bp->b_resid = 0;
831 	if (sc->sc_flags & CTF_CMD) {
832 		switch (sc->sc_cmd) {
833 		case MTFSF:
834 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
835 			sc->sc_blkno += CTBTOK(sc->sc_resid);
836 			ctstart(sc);
837 			return;
838 		case MTBSF:
839 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
840 			break;
841 		case MTBSR:
842 			sc->sc_flags &= ~CTF_BEOF;
843 			if (sc->sc_flags & CTF_EOT) {
844 				sc->sc_flags |= CTF_AEOF;
845 				sc->sc_flags &= ~CTF_EOT;
846 			} else if (sc->sc_flags & CTF_AEOF) {
847 				sc->sc_flags |= CTF_BEOF;
848 				sc->sc_flags &= ~CTF_AEOF;
849 			}
850 			break;
851 		case MTWEOF:
852 			sc->sc_flags &= ~CTF_BEOF;
853 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
854 				sc->sc_flags |= CTF_EOT;
855 				sc->sc_flags &= ~CTF_AEOF;
856 			} else
857 				sc->sc_flags |= CTF_AEOF;
858 			break;
859 		case MTREW:
860 		case MTOFFL:
861 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
862 			break;
863 		}
864 	} else {
865 		sc->sc_flags &= ~CTF_AEOF;
866 		sc->sc_blkno += CTBTOK(sc->sc_resid);
867 	}
868 done:
869 #ifdef DEBUG
870 	if (ctdebug & CDB_FILES)
871 		printf("ctintr: after flags %x\n", sc->sc_flags);
872 #endif
873 	ctdone(sc, bp);
874 }
875 
876 void
877 ctdone(sc, bp)
878 	struct ct_softc *sc;
879 	struct buf *bp;
880 {
881 
882 	BUFQ_REMOVE(&sc->sc_tab, bp);
883 	biodone(bp);
884 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
885 	if (BUFQ_FIRST(&sc->sc_tab) == NULL) {
886 		sc->sc_active = 0;
887 		return;
888 	}
889 	ctustart(sc);
890 }
891 
892 int
893 ctread(dev, uio, flags)
894 	dev_t dev;
895 	struct uio *uio;
896 	int flags;
897 {
898 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
899 }
900 
901 int
902 ctwrite(dev, uio, flags)
903 	dev_t dev;
904 	struct uio *uio;
905 	int flags;
906 {
907 	/* XXX: check for hardware write-protect? */
908 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
909 }
910 
911 /*ARGSUSED*/
912 int
913 ctioctl(dev, cmd, data, flag, p)
914 	dev_t dev;
915 	u_long cmd;
916 	int flag;
917 	caddr_t data;
918 	struct proc *p;
919 {
920 	struct mtop *op;
921 	int cnt;
922 
923 	switch (cmd) {
924 
925 	case MTIOCTOP:
926 		op = (struct mtop *)data;
927 		switch(op->mt_op) {
928 
929 		case MTWEOF:
930 		case MTFSF:
931 		case MTBSR:
932 		case MTBSF:
933 		case MTFSR:
934 			cnt = op->mt_count;
935 			break;
936 
937 		case MTREW:
938 		case MTOFFL:
939 			cnt = 1;
940 			break;
941 
942 		default:
943 			return(EINVAL);
944 		}
945 		ctcommand(dev, op->mt_op, cnt);
946 		break;
947 
948 	case MTIOCGET:
949 		break;
950 
951 	default:
952 		return(EINVAL);
953 	}
954 	return(0);
955 }
956 
957 /* ARGSUSED */
958 int
959 ctdump(dev, blkno, va, size)
960 	dev_t dev;
961 	daddr_t blkno;
962 	caddr_t va;
963 	size_t size;
964 {
965 
966 	return (ENODEV);
967 }
968 
969 void
970 ctaddeof(sc)
971 	struct ct_softc *sc;
972 {
973 
974 	if (sc->sc_eofp == EOFS - 1)
975 		sc->sc_eofs[EOFS - 1]++;
976 	else {
977 		sc->sc_eofp++;
978 		if (sc->sc_eofp == EOFS - 1)
979 			sc->sc_eofs[EOFS - 1] = EOFS;
980 		else
981 			/* save blkno */
982 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
983 	}
984 #ifdef DEBUG
985 	if (ctdebug & CT_BSF)
986 		printf("%s: add eof pos %d blk %d\n",
987 		       sc->sc_dev.dv_xname, sc->sc_eofp,
988 		       sc->sc_eofs[sc->sc_eofp]);
989 #endif
990 }
991