xref: /netbsd-src/sys/arch/hp300/dev/ct.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: ct.c,v 1.22 1997/10/04 09:59:40 thorpej 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 sc_tab;
119 	struct	buf *sc_bp;
120 	struct	buf sc_bufstore;	/* XXX */
121 	int	sc_blkno;
122 	int	sc_cmd;
123 	int	sc_resid;
124 	char	*sc_addr;
125 	int	sc_flags;
126 	short	sc_type;
127 	tpr_t	sc_tpr;
128 	struct	hpibqueue sc_hq;	/* entry on hpib job queue */
129 	int	sc_eofp;
130 	int	sc_eofs[EOFS];
131 };
132 
133 /* flags */
134 #define	CTF_OPEN	0x01
135 #define	CTF_ALIVE	0x02
136 #define	CTF_WRT		0x04
137 #define	CTF_CMD		0x08
138 #define	CTF_IO		0x10
139 #define	CTF_BEOF	0x20
140 #define	CTF_AEOF	0x40
141 #define	CTF_EOT		0x80
142 #define	CTF_STATWAIT	0x100
143 #define CTF_CANSTREAM	0x200
144 #define	CTF_WRTTN	0x400
145 
146 int	ctmatch __P((struct device *, struct cfdata *, void *));
147 void	ctattach __P((struct device *, struct device *, void *));
148 
149 struct cfattach ct_ca = {
150 	sizeof(struct ct_softc), ctmatch, ctattach
151 };
152 
153 struct cfdriver ct_cd = {
154 	NULL, "ct", DV_TAPE
155 };
156 
157 int	ctident __P((struct device *, struct ct_softc *,
158 	    struct hpibbus_attach_args *));
159 
160 void	ctreset __P((struct ct_softc *));
161 void	ctaddeof __P((struct ct_softc *));
162 void	ctustart __P((struct ct_softc *));
163 void	cteof __P((struct ct_softc *, struct buf *));
164 void	ctdone __P((struct ct_softc *, struct buf *));
165 
166 void	ctstart __P((void *));
167 void	ctgo __P((void *));
168 void	ctintr __P((void *));
169 
170 void	ctcommand __P((dev_t, int, int));
171 
172 cdev_decl(ct);
173 bdev_decl(ct);
174 
175 struct	ctinfo {
176 	short	hwid;
177 	short	punit;
178 	char	*desc;
179 } ctinfo[] = {
180 	{ CT7946ID,	1,	"7946A"	},
181 	{ CT7912PID,	1,	"7912P"	},
182 	{ CT7914PID,	1,	"7914P"	},
183 	{ CT9144ID,	0,	"9144"	},
184 	{ CT9145ID,	0,	"9145"	},
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 	sc->sc_tab.b_actb = &sc->sc_tab.b_actf;
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 		type = CT9144;
294 		canstream = 1;
295 		break;
296 
297 	case CT7912PID:
298 	case CT7914PID:
299 		type = CT88140;
300 		break;
301 	}
302 
303 	if (sc != NULL) {
304 		sc->sc_type = type;
305 		sc->sc_flags = canstream ? CTF_CANSTREAM : 0;
306 		printf(": %s %stape\n", ctinfo[id].desc,
307 		    canstream ? "streaming " : "");
308 	}
309 
310 	return (1);
311 }
312 
313 void
314 ctreset(sc)
315 	struct ct_softc *sc;
316 {
317 	int ctlr, slave;
318 	u_char stat;
319 
320 	ctlr = sc->sc_dev.dv_parent->dv_unit;
321 	slave = sc->sc_slave;
322 
323 	sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
324 	sc->sc_clear.cmd = C_CLEAR;
325 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
326 	hpibswait(ctlr, slave);
327 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
328 
329 	sc->sc_src.unit = C_SUNIT(CTCTLR);
330 	sc->sc_src.nop = C_NOP;
331 	sc->sc_src.cmd = C_SREL;
332 	sc->sc_src.param = C_REL;
333 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
334 	hpibswait(ctlr, slave);
335 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
336 
337 	sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
338 	sc->sc_ssmc.cmd = C_SSM;
339 	sc->sc_ssmc.refm = REF_MASK;
340 	sc->sc_ssmc.fefm = FEF_MASK;
341 	sc->sc_ssmc.aefm = AEF_MASK;
342 	sc->sc_ssmc.iefm = IEF_MASK;
343 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
344 	hpibswait(ctlr, slave);
345 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
346 
347 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
348 	sc->sc_soptc.nop = C_NOP;
349 	sc->sc_soptc.cmd = C_SOPT;
350 	sc->sc_soptc.opt = C_SPAR;
351 	hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
352 	hpibswait(ctlr, slave);
353 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
354 }
355 
356 /*ARGSUSED*/
357 int
358 ctopen(dev, flag, type, p)
359 	dev_t dev;
360 	int flag, type;
361 	struct proc *p;
362 {
363 	struct ct_softc *sc;
364 	u_char stat;
365 	int cc, ctlr, slave;
366 
367 	if (UNIT(dev) >= ct_cd.cd_ndevs ||
368 	    (sc = ct_cd.cd_devs[UNIT(dev)]) == NULL ||
369 	    (sc->sc_flags & CTF_ALIVE) == 0)
370 		return (ENXIO);
371 
372 	if (sc->sc_flags & CTF_OPEN)
373 		return (EBUSY);
374 
375 	ctlr = sc->sc_dev.dv_parent->dv_unit;
376 	slave = sc->sc_slave;
377 
378 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
379 	sc->sc_soptc.nop = C_NOP;
380 	sc->sc_soptc.cmd = C_SOPT;
381 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
382 		sc->sc_soptc.opt = C_SPAR | C_IMRPT;
383 	else
384 		sc->sc_soptc.opt = C_SPAR;
385 
386 	/*
387 	 * Check the return of hpibsend() and hpibswait().
388 	 * Drive could be loading/unloading a tape. If not checked,
389 	 * driver hangs.
390 	 */
391 	cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
392 	if (cc != sizeof(sc->sc_soptc))
393 		return (EBUSY);
394 
395 	hpibswait(ctlr, slave);
396 	cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
397 	if (cc != sizeof(stat))
398 		return(EBUSY);
399 
400 	sc->sc_tpr = tprintf_open(p);
401 	sc->sc_flags |= CTF_OPEN;
402 	return(0);
403 }
404 
405 /*ARGSUSED*/
406 int
407 ctclose(dev, flag, fmt, p)
408 	dev_t dev;
409 	int flag, fmt;
410 	struct proc *p;
411 {
412 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
413 
414 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
415 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
416 		ctcommand(dev, MTWEOF, 2);
417 		ctcommand(dev, MTBSR, 1);
418 		if (sc->sc_eofp == EOFS - 1)
419 			sc->sc_eofs[EOFS - 1]--;
420 		else
421 			sc->sc_eofp--;
422 #ifdef DEBUG
423 		if(ctdebug & CT_BSF)
424 			printf("%s: ctclose backup eofs prt %d blk %d\n",
425 			       sc->sc_dev.dv_xname, sc->sc_eofp,
426 			       sc->sc_eofs[sc->sc_eofp]);
427 #endif
428 	}
429 	if ((minor(dev) & CT_NOREW) == 0)
430 		ctcommand(dev, MTREW, 1);
431 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
432 	tprintf_close(sc->sc_tpr);
433 #ifdef DEBUG
434 	if (ctdebug & CDB_FILES)
435 		printf("ctclose: flags %x\n", sc->sc_flags);
436 #endif
437 	return(0);	/* XXX */
438 }
439 
440 void
441 ctcommand(dev, cmd, cnt)
442 	dev_t dev;
443 	int cmd;
444 	int cnt;
445 {
446 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
447 	struct buf *bp = &sc->sc_bufstore;
448 	struct buf *nbp = 0;
449 
450 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
451 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
452 		ctcommand(dev, MTREW, 1);
453 		ctcommand(dev, MTFSF, cnt);
454 		cnt = 2;
455 		cmd = MTBSR;
456 	}
457 
458 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
459 		cnt = 1;
460 		cmd = MTREW;
461 	}
462 
463 	sc->sc_flags |= CTF_CMD;
464 	sc->sc_bp = bp;
465 	sc->sc_cmd = cmd;
466 	bp->b_dev = dev;
467 	if (cmd == MTFSF) {
468 		nbp = (struct buf *)geteblk(MAXBSIZE);
469 		bp->b_un.b_addr = nbp->b_un.b_addr;
470 		bp->b_bcount = MAXBSIZE;
471 	}
472 
473 	while (cnt-- > 0) {
474 		bp->b_flags = B_BUSY;
475 		if (cmd == MTBSF) {
476 			sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
477 			sc->sc_eofp--;
478 #ifdef DEBUG
479 			if (ctdebug & CT_BSF)
480 				printf("%s: backup eof pos %d blk %d\n",
481 				    sc->sc_dev.dv_xname, sc->sc_eofp,
482 				    sc->sc_eofs[sc->sc_eofp]);
483 #endif
484 		}
485 		ctstrategy(bp);
486 		iowait(bp);
487 	}
488 	bp->b_flags = 0;
489 	sc->sc_flags &= ~CTF_CMD;
490 	if (nbp)
491 		brelse(nbp);
492 }
493 
494 void
495 ctstrategy(bp)
496 	struct buf *bp;
497 {
498 	struct buf *dp;
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 	dp = &sc->sc_tab;
506 	bp->b_actf = NULL;
507 	s = splbio();
508 	bp->b_actb = dp->b_actb;
509 	*(dp->b_actb) = bp;
510 	dp->b_actb = &bp->b_actf;
511 	if (dp->b_active == 0) {
512 		dp->b_active = 1;
513 		ctustart(sc);
514 	}
515 	splx(s);
516 }
517 
518 void
519 ctustart(sc)
520 	struct ct_softc *sc;
521 {
522 	struct buf *bp;
523 
524 	bp = sc->sc_tab.b_actf;
525 	sc->sc_addr = bp->b_un.b_addr;
526 	sc->sc_resid = bp->b_bcount;
527 	if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
528 		ctstart(sc);
529 }
530 
531 void
532 ctstart(arg)
533 	void *arg;
534 {
535 	struct ct_softc *sc = arg;
536 	struct buf *bp;
537 	int i, ctlr, slave;
538 
539 	ctlr = sc->sc_dev.dv_parent->dv_unit;
540 	slave = sc->sc_slave;
541 
542 	bp = sc->sc_tab.b_actf;
543 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
544 		switch(sc->sc_cmd) {
545 		case MTFSF:
546 			bp->b_flags |= B_READ;
547 			goto mustio;
548 
549 		case MTBSF:
550 			goto gotaddr;
551 
552 		case MTOFFL:
553 			sc->sc_blkno = 0;
554 			sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
555 			sc->sc_ul.cmd = C_UNLOAD;
556 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ul,
557 			    sizeof(sc->sc_ul));
558 			break;
559 
560 		case MTWEOF:
561 			sc->sc_blkno++;
562 			sc->sc_flags |= CTF_WRT;
563 			sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
564 			sc->sc_wfm.cmd = C_WFM;
565 			hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm,
566 			    sizeof(sc->sc_wfm));
567 			ctaddeof(sc);
568 			break;
569 
570 		case MTBSR:
571 			sc->sc_blkno--;
572 			goto gotaddr;
573 
574 		case MTFSR:
575 			sc->sc_blkno++;
576 			goto gotaddr;
577 
578 		case MTREW:
579 			sc->sc_blkno = 0;
580 #ifdef DEBUG
581 			if(ctdebug & CT_BSF)
582 				printf("%s: clearing eofs\n",
583 				    sc->sc_dev.dv_xname);
584 #endif
585 			for (i=0; i<EOFS; i++)
586 				sc->sc_eofs[i] = 0;
587 			sc->sc_eofp = 0;
588 
589 gotaddr:
590 			sc->sc_ioc.saddr = C_SADDR;
591 			sc->sc_ioc.addr0 = 0;
592 			sc->sc_ioc.addr = sc->sc_blkno;
593 			sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
594 			sc->sc_ioc.nop2 = C_NOP;
595 			sc->sc_ioc.slen = C_SLEN;
596 			sc->sc_ioc.len = 0;
597 			sc->sc_ioc.nop3 = C_NOP;
598 			sc->sc_ioc.cmd = C_READ;
599 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc,
600 			    sizeof(sc->sc_ioc));
601 			break;
602 		}
603 	} else {
604 mustio:
605 		if ((bp->b_flags & B_READ) &&
606 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
607 #ifdef DEBUG
608 			if (ctdebug & CDB_FILES)
609 				printf("ctstart: before flags %x\n",
610 				    sc->sc_flags);
611 #endif
612 			if (sc->sc_flags & CTF_BEOF) {
613 				sc->sc_flags &= ~CTF_BEOF;
614 				sc->sc_flags |= CTF_AEOF;
615 #ifdef DEBUG
616 				if (ctdebug & CDB_FILES)
617 					printf("ctstart: after flags %x\n",
618 					    sc->sc_flags);
619 #endif
620 			}
621 			bp->b_resid = bp->b_bcount;
622 			ctdone(sc, bp);
623 			return;
624 		}
625 		sc->sc_flags |= CTF_IO;
626 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
627 		sc->sc_ioc.saddr = C_SADDR;
628 		sc->sc_ioc.addr0 = 0;
629 		sc->sc_ioc.addr = sc->sc_blkno;
630 		sc->sc_ioc.nop2 = C_NOP;
631 		sc->sc_ioc.slen = C_SLEN;
632 		sc->sc_ioc.len = sc->sc_resid;
633 		sc->sc_ioc.nop3 = C_NOP;
634 		if (bp->b_flags & B_READ)
635 			sc->sc_ioc.cmd = C_READ;
636 		else {
637 			sc->sc_ioc.cmd = C_WRITE;
638 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
639 		}
640 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
641 	}
642 	hpibawait(ctlr);
643 }
644 
645 void
646 ctgo(arg)
647 	void *arg;
648 {
649 	struct ct_softc *sc = arg;
650 	struct buf *bp;
651 	int rw;
652 
653 	bp = sc->sc_tab.b_actf;
654 	rw = bp->b_flags & B_READ;
655 	hpibgo(sc->sc_dev.dv_parent->dv_unit, sc->sc_slave, C_EXEC,
656 	    sc->sc_addr, sc->sc_resid, rw, rw != 0);
657 }
658 
659 /*
660  * Hideous grue to handle EOF/EOT (mostly for reads)
661  */
662 void
663 cteof(sc, bp)
664 	struct ct_softc *sc;
665 	struct buf *bp;
666 {
667 	long blks;
668 
669 	/*
670 	 * EOT on a write is an error.
671 	 */
672 	if ((bp->b_flags & B_READ) == 0) {
673 		bp->b_resid = bp->b_bcount;
674 		bp->b_flags |= B_ERROR;
675 		bp->b_error = ENOSPC;
676 		sc->sc_flags |= CTF_EOT;
677 		return;
678 	}
679 	/*
680 	 * Use returned block position to determine how many blocks
681 	 * we really read and update b_resid.
682 	 */
683 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
684 #ifdef DEBUG
685 	if (ctdebug & CDB_FILES)
686 		printf("cteof: bc %ld oblk %d nblk %ld read %ld, resid %ld\n",
687 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
688 		       blks, bp->b_bcount - CTKTOB(blks));
689 #endif
690 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
691 		blks = 0;
692 		sc->sc_blkno++;
693 	}
694 	else {
695 		sc->sc_blkno = sc->sc_stat.c_blk;
696 	}
697 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
698 	/*
699 	 * If we are at physical EOV or were after an EOF,
700 	 * we are now at logical EOT.
701 	 */
702 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
703 	    (sc->sc_flags & CTF_AEOF)) {
704 		sc->sc_flags |= CTF_EOT;
705 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
706 	}
707 	/*
708 	 * If we were before an EOF or we have just completed a FSF,
709 	 * we are now after EOF.
710 	 */
711 	else if ((sc->sc_flags & CTF_BEOF) ||
712 		 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) {
713 		sc->sc_flags |= CTF_AEOF;
714 		sc->sc_flags &= ~CTF_BEOF;
715 	}
716 	/*
717 	 * Otherwise if we read something we are now before EOF
718 	 * (and no longer after EOF).
719 	 */
720 	else if (blks) {
721 		sc->sc_flags |= CTF_BEOF;
722 		sc->sc_flags &= ~CTF_AEOF;
723 	}
724 	/*
725 	 * Finally, if we didn't read anything we just passed an EOF
726 	 */
727 	else
728 		sc->sc_flags |= CTF_AEOF;
729 #ifdef DEBUG
730 	if (ctdebug & CDB_FILES)
731 		printf("cteof: leaving flags %x\n", sc->sc_flags);
732 #endif
733 }
734 
735 /* ARGSUSED */
736 void
737 ctintr(arg)
738 	void *arg;
739 {
740 	struct ct_softc *sc = arg;
741 	struct buf *bp;
742 	u_char stat;
743 	int ctlr, slave, unit;
744 
745 	ctlr = sc->sc_dev.dv_parent->dv_unit;
746 	slave = sc->sc_slave;
747 	unit = sc->sc_dev.dv_unit;
748 
749 	bp = sc->sc_tab.b_actf;
750 	if (bp == NULL) {
751 		printf("%s: bp == NULL\n", sc->sc_dev.dv_xname);
752 		return;
753 	}
754 	if (sc->sc_flags & CTF_IO) {
755 		sc->sc_flags &= ~CTF_IO;
756 		if (hpibustart(ctlr))
757 			ctgo(sc);
758 		return;
759 	}
760 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
761 		if (hpibpptest(ctlr, slave) == 0) {
762 			sc->sc_flags |= CTF_STATWAIT;
763 			hpibawait(ctlr);
764 			return;
765 		}
766 	} else
767 		sc->sc_flags &= ~CTF_STATWAIT;
768 	hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
769 #ifdef DEBUG
770 	if (ctdebug & CDB_FILES)
771 		printf("ctintr: before flags %x\n", sc->sc_flags);
772 #endif
773 	if (stat) {
774 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
775 		sc->sc_rsc.cmd = C_STATUS;
776 		hpibsend(ctlr, slave, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
777 		hpibrecv(ctlr, slave, C_EXEC, &sc->sc_stat,
778 		    sizeof(sc->sc_stat));
779 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
780 #ifdef DEBUG
781 		if (ctdebug & CDB_FILES)
782 			printf("ctintr: return stat 0x%x, A%x F%x blk %ld\n",
783 			       stat, sc->sc_stat.c_aef,
784 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
785 #endif
786 		if (stat == 0) {
787 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
788 				cteof(sc, bp);
789 				ctaddeof(sc);
790 				goto done;
791 			}
792 			if (sc->sc_stat.c_fef & FEF_PF) {
793 				ctreset(sc);
794 				ctstart(sc);
795 				return;
796 			}
797 			if (sc->sc_stat.c_fef & FEF_REXMT) {
798 				ctstart(sc);
799 				return;
800 			}
801 			if (sc->sc_stat.c_aef & 0x5800) {
802 				if (sc->sc_stat.c_aef & 0x4000)
803 					tprintf(sc->sc_tpr,
804 						"%s: uninitialized media\n",
805 						sc->sc_dev.dv_xname);
806 				if (sc->sc_stat.c_aef & 0x1000)
807 					tprintf(sc->sc_tpr,
808 						"%s: not ready\n",
809 						sc->sc_dev.dv_xname);
810 				if (sc->sc_stat.c_aef & 0x0800)
811 					tprintf(sc->sc_tpr,
812 						"%s: write protect\n",
813 						sc->sc_dev.dv_xname);
814 			} else {
815 				printf("%s err: v%d u%d ru%d bn%ld, ",
816 				       sc->sc_dev.dv_xname,
817 				       (sc->sc_stat.c_vu>>4)&0xF,
818 				       sc->sc_stat.c_vu&0xF,
819 				       sc->sc_stat.c_pend,
820 				       sc->sc_stat.c_blk);
821 				printf("R0x%x F0x%x A0x%x I0x%x\n",
822 				       sc->sc_stat.c_ref,
823 				       sc->sc_stat.c_fef,
824 				       sc->sc_stat.c_aef,
825 				       sc->sc_stat.c_ief);
826 			}
827 		} else
828 			printf("%s: request status failed\n",
829 			    sc->sc_dev.dv_xname);
830 		bp->b_flags |= B_ERROR;
831 		bp->b_error = EIO;
832 		goto done;
833 	} else
834 		bp->b_resid = 0;
835 	if (sc->sc_flags & CTF_CMD) {
836 		switch (sc->sc_cmd) {
837 		case MTFSF:
838 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
839 			sc->sc_blkno += CTBTOK(sc->sc_resid);
840 			ctstart(sc);
841 			return;
842 		case MTBSF:
843 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
844 			break;
845 		case MTBSR:
846 			sc->sc_flags &= ~CTF_BEOF;
847 			if (sc->sc_flags & CTF_EOT) {
848 				sc->sc_flags |= CTF_AEOF;
849 				sc->sc_flags &= ~CTF_EOT;
850 			} else if (sc->sc_flags & CTF_AEOF) {
851 				sc->sc_flags |= CTF_BEOF;
852 				sc->sc_flags &= ~CTF_AEOF;
853 			}
854 			break;
855 		case MTWEOF:
856 			sc->sc_flags &= ~CTF_BEOF;
857 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
858 				sc->sc_flags |= CTF_EOT;
859 				sc->sc_flags &= ~CTF_AEOF;
860 			} else
861 				sc->sc_flags |= CTF_AEOF;
862 			break;
863 		case MTREW:
864 		case MTOFFL:
865 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
866 			break;
867 		}
868 	} else {
869 		sc->sc_flags &= ~CTF_AEOF;
870 		sc->sc_blkno += CTBTOK(sc->sc_resid);
871 	}
872 done:
873 #ifdef DEBUG
874 	if (ctdebug & CDB_FILES)
875 		printf("ctintr: after flags %x\n", sc->sc_flags);
876 #endif
877 	ctdone(sc, bp);
878 }
879 
880 void
881 ctdone(sc, bp)
882 	struct ct_softc *sc;
883 	struct buf *bp;
884 {
885 	struct buf *dp;
886 
887 	if ((dp = bp->b_actf) != NULL)
888 		dp->b_actb = bp->b_actb;
889 	else
890 		sc->sc_tab.b_actb = bp->b_actb;
891 	*bp->b_actb = dp;
892 	biodone(bp);
893 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
894 	if (sc->sc_tab.b_actf == NULL) {
895 		sc->sc_tab.b_active = 0;
896 		return;
897 	}
898 	ctustart(sc);
899 }
900 
901 int
902 ctread(dev, uio, flags)
903 	dev_t dev;
904 	struct uio *uio;
905 	int flags;
906 {
907 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
908 }
909 
910 int
911 ctwrite(dev, uio, flags)
912 	dev_t dev;
913 	struct uio *uio;
914 	int flags;
915 {
916 	/* XXX: check for hardware write-protect? */
917 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
918 }
919 
920 /*ARGSUSED*/
921 int
922 ctioctl(dev, cmd, data, flag, p)
923 	dev_t dev;
924 	u_long cmd;
925 	int flag;
926 	caddr_t data;
927 	struct proc *p;
928 {
929 	struct mtop *op;
930 	int cnt;
931 
932 	switch (cmd) {
933 
934 	case MTIOCTOP:
935 		op = (struct mtop *)data;
936 		switch(op->mt_op) {
937 
938 		case MTWEOF:
939 		case MTFSF:
940 		case MTBSR:
941 		case MTBSF:
942 		case MTFSR:
943 			cnt = op->mt_count;
944 			break;
945 
946 		case MTREW:
947 		case MTOFFL:
948 			cnt = 1;
949 			break;
950 
951 		default:
952 			return(EINVAL);
953 		}
954 		ctcommand(dev, op->mt_op, cnt);
955 		break;
956 
957 	case MTIOCGET:
958 		break;
959 
960 	default:
961 		return(EINVAL);
962 	}
963 	return(0);
964 }
965 
966 /* ARGSUSED */
967 int
968 ctdump(dev, blkno, va, size)
969 	dev_t dev;
970 	daddr_t blkno;
971 	caddr_t va;
972 	size_t size;
973 {
974 
975 	return (ENODEV);
976 }
977 
978 void
979 ctaddeof(sc)
980 	struct ct_softc *sc;
981 {
982 
983 	if (sc->sc_eofp == EOFS - 1)
984 		sc->sc_eofs[EOFS - 1]++;
985 	else {
986 		sc->sc_eofp++;
987 		if (sc->sc_eofp == EOFS - 1)
988 			sc->sc_eofs[EOFS - 1] = EOFS;
989 		else
990 			/* save blkno */
991 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
992 	}
993 #ifdef DEBUG
994 	if (ctdebug & CT_BSF)
995 		printf("%s: add eof pos %d blk %d\n",
996 		       sc->sc_dev.dv_xname, sc->sc_eofp,
997 		       sc->sc_eofs[sc->sc_eofp]);
998 #endif
999 }
1000