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