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