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