ct.c revision 1.63 1 /* $NetBSD: ct.c,v 1.63 2021/07/05 14:51:23 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.63 2021/07/05 14:51:23 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 cs80_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,
288 sizeof(desc));
289 hpibrecv(device_unit(parent), ha->ha_slave, C_QSTAT, &stat,
290 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 aprint_normal(": %s %stape\n", ctinfo[id].desc,
323 canstream ? "streaming " : "");
324 }
325
326 return 1;
327 }
328
329 static void
330 ctreset(struct ct_softc *sc)
331 {
332 int ctlr, slave;
333 uint8_t stat;
334
335 ctlr = device_unit(device_parent(sc->sc_dev));
336 slave = sc->sc_slave;
337
338 sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
339 sc->sc_clear.cmd = C_CLEAR;
340 hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
341 hpibswait(ctlr, slave);
342 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
343
344 sc->sc_src.unit = C_SUNIT(CTCTLR);
345 sc->sc_src.nop = C_NOP;
346 sc->sc_src.cmd = C_SREL;
347 sc->sc_src.param = C_REL;
348 hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
349 hpibswait(ctlr, slave);
350 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
351
352 sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
353 sc->sc_ssmc.cmd = C_SSM;
354 sc->sc_ssmc.refm = REF_MASK;
355 sc->sc_ssmc.fefm = FEF_MASK;
356 sc->sc_ssmc.aefm = AEF_MASK;
357 sc->sc_ssmc.iefm = IEF_MASK;
358 hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
359 hpibswait(ctlr, slave);
360 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
361
362 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
363 sc->sc_soptc.nop = C_NOP;
364 sc->sc_soptc.cmd = C_SOPT;
365 sc->sc_soptc.opt = C_SPAR;
366 hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
367 hpibswait(ctlr, slave);
368 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
369 }
370
371 /*ARGSUSED*/
372 static int
373 ctopen(dev_t dev, int flag, int type, struct lwp *l)
374 {
375 struct ct_softc *sc;
376 uint8_t stat;
377 int cc, ctlr, slave;
378
379 sc = device_lookup_private(&ct_cd, UNIT(dev));
380 if (sc == NULL)
381 return ENXIO;
382
383 if ((sc->sc_flags & CTF_ALIVE) == 0)
384 return ENXIO;
385
386 if (sc->sc_flags & CTF_OPEN)
387 return EBUSY;
388
389 ctlr = device_unit(device_parent(sc->sc_dev));
390 slave = sc->sc_slave;
391
392 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
393 sc->sc_soptc.nop = C_NOP;
394 sc->sc_soptc.cmd = C_SOPT;
395 if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
396 sc->sc_soptc.opt = C_SPAR | C_IMRPT;
397 else
398 sc->sc_soptc.opt = C_SPAR;
399
400 /*
401 * Check the return of hpibsend() and hpibswait().
402 * Drive could be loading/unloading a tape. If not checked,
403 * driver hangs.
404 */
405 cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
406 if (cc != sizeof(sc->sc_soptc))
407 return EBUSY;
408
409 hpibswait(ctlr, slave);
410 cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
411 if (cc != sizeof(stat))
412 return EBUSY;
413
414 sc->sc_tpr = tprintf_open(l->l_proc);
415 sc->sc_flags |= CTF_OPEN;
416 return 0;
417 }
418
419 /*ARGSUSED*/
420 static int
421 ctclose(dev_t dev, int flag, int fmt, struct lwp *l)
422 {
423 struct ct_softc *sc = device_lookup_private(&ct_cd,UNIT(dev));
424
425 if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
426 (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
427 ctcommand(dev, MTWEOF, 2);
428 ctcommand(dev, MTBSR, 1);
429 if (sc->sc_eofp == EOFS - 1)
430 sc->sc_eofs[EOFS - 1]--;
431 else
432 sc->sc_eofp--;
433 #ifdef DEBUG
434 if(ctdebug & CT_BSF)
435 printf("%s: ctclose backup eofs prt %d blk %d\n",
436 device_xname(sc->sc_dev), sc->sc_eofp,
437 sc->sc_eofs[sc->sc_eofp]);
438 #endif
439 }
440 if ((minor(dev) & CT_NOREW) == 0)
441 ctcommand(dev, MTREW, 1);
442 sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
443 tprintf_close(sc->sc_tpr);
444 #ifdef DEBUG
445 if (ctdebug & CDB_FILES)
446 printf("ctclose: flags %x\n", sc->sc_flags);
447 #endif
448 return 0; /* XXX */
449 }
450
451 static void
452 ctcommand(dev_t dev, int cmd, int cnt)
453 {
454 struct ct_softc *sc = device_lookup_private(&ct_cd,UNIT(dev));
455 struct buf *bp = &sc->sc_bufstore;
456 struct buf *nbp = 0;
457
458 if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
459 cnt = sc->sc_eofs[EOFS - 1] - cnt;
460 ctcommand(dev, MTREW, 1);
461 ctcommand(dev, MTFSF, cnt);
462 cnt = 2;
463 cmd = MTBSR;
464 }
465
466 if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
467 cnt = 1;
468 cmd = MTREW;
469 }
470
471 sc->sc_flags |= CTF_CMD;
472 sc->sc_bp = bp;
473 sc->sc_cmd = cmd;
474 bp->b_dev = dev;
475 if (cmd == MTFSF) {
476 nbp = (struct buf *)geteblk(MAXBSIZE);
477 bp->b_data = nbp->b_data;
478 bp->b_bcount = MAXBSIZE;
479 }
480
481 while (cnt-- > 0) {
482 bp->b_cflags = BC_BUSY;
483 if (cmd == MTBSF) {
484 sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
485 sc->sc_eofp--;
486 #ifdef DEBUG
487 if (ctdebug & CT_BSF)
488 printf("%s: backup eof pos %d blk %d\n",
489 device_xname(sc->sc_dev), sc->sc_eofp,
490 sc->sc_eofs[sc->sc_eofp]);
491 #endif
492 }
493 ctstrategy(bp);
494 biowait(bp);
495 }
496 bp->b_flags = 0;
497 sc->sc_flags &= ~CTF_CMD;
498 if (nbp)
499 brelse(nbp, 0);
500 }
501
502 static void
503 ctstrategy(struct buf *bp)
504 {
505 int s;
506 struct ct_softc *sc;
507
508 sc = device_lookup_private(&ct_cd, UNIT(bp->b_dev));
509
510 s = splbio();
511 bufq_put(sc->sc_tab, bp);
512 if (sc->sc_active == 0) {
513 sc->sc_active = 1;
514 ctustart(sc);
515 }
516 splx(s);
517 }
518
519 static void
520 ctustart(struct ct_softc *sc)
521 {
522 struct buf *bp;
523
524 bp = bufq_peek(sc->sc_tab);
525 sc->sc_addr = bp->b_data;
526 sc->sc_resid = bp->b_bcount;
527 if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
528 ctstart(sc);
529 }
530
531 static void
532 ctstart(void *arg)
533 {
534 struct ct_softc *sc = arg;
535 struct buf *bp;
536 int i, ctlr, slave;
537
538 ctlr = device_unit(device_parent(sc->sc_dev));
539 slave = sc->sc_slave;
540
541 bp = bufq_peek(sc->sc_tab);
542 if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
543 switch(sc->sc_cmd) {
544 case MTFSF:
545 bp->b_flags |= B_READ;
546 goto mustio;
547
548 case MTBSF:
549 goto gotaddr;
550
551 case MTOFFL:
552 sc->sc_blkno = 0;
553 sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
554 sc->sc_ul.cmd = C_UNLOAD;
555 hpibsend(ctlr, slave, C_CMD, &sc->sc_ul,
556 sizeof(sc->sc_ul));
557 break;
558
559 case MTWEOF:
560 sc->sc_blkno++;
561 sc->sc_flags |= CTF_WRT;
562 sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
563 sc->sc_wfm.cmd = C_WFM;
564 hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm,
565 sizeof(sc->sc_wfm));
566 ctaddeof(sc);
567 break;
568
569 case MTBSR:
570 sc->sc_blkno--;
571 goto gotaddr;
572
573 case MTFSR:
574 sc->sc_blkno++;
575 goto gotaddr;
576
577 case MTREW:
578 sc->sc_blkno = 0;
579 #ifdef DEBUG
580 if(ctdebug & CT_BSF)
581 printf("%s: clearing eofs\n",
582 device_xname(sc->sc_dev));
583 #endif
584 for (i=0; i<EOFS; i++)
585 sc->sc_eofs[i] = 0;
586 sc->sc_eofp = 0;
587
588 gotaddr:
589 sc->sc_ioc.saddr = C_SADDR;
590 sc->sc_ioc.addr0 = 0;
591 sc->sc_ioc.addr = sc->sc_blkno;
592 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
593 sc->sc_ioc.nop2 = C_NOP;
594 sc->sc_ioc.slen = C_SLEN;
595 sc->sc_ioc.len = 0;
596 sc->sc_ioc.nop3 = C_NOP;
597 sc->sc_ioc.cmd = C_READ;
598 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc,
599 sizeof(sc->sc_ioc));
600 break;
601 }
602 } else {
603 mustio:
604 if ((bp->b_flags & B_READ) &&
605 sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
606 #ifdef DEBUG
607 if (ctdebug & CDB_FILES)
608 printf("ctstart: before flags %x\n",
609 sc->sc_flags);
610 #endif
611 if (sc->sc_flags & CTF_BEOF) {
612 sc->sc_flags &= ~CTF_BEOF;
613 sc->sc_flags |= CTF_AEOF;
614 #ifdef DEBUG
615 if (ctdebug & CDB_FILES)
616 printf("ctstart: after flags %x\n",
617 sc->sc_flags);
618 #endif
619 }
620 bp->b_resid = bp->b_bcount;
621 ctdone(sc, bp);
622 return;
623 }
624 sc->sc_flags |= CTF_IO;
625 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
626 sc->sc_ioc.saddr = C_SADDR;
627 sc->sc_ioc.addr0 = 0;
628 sc->sc_ioc.addr = sc->sc_blkno;
629 sc->sc_ioc.nop2 = C_NOP;
630 sc->sc_ioc.slen = C_SLEN;
631 sc->sc_ioc.len = sc->sc_resid;
632 sc->sc_ioc.nop3 = C_NOP;
633 if (bp->b_flags & B_READ)
634 sc->sc_ioc.cmd = C_READ;
635 else {
636 sc->sc_ioc.cmd = C_WRITE;
637 sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
638 }
639 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
640 }
641 hpibawait(ctlr);
642 }
643
644 static void
645 ctgo(void *arg)
646 {
647 struct ct_softc *sc = arg;
648 struct buf *bp;
649 int rw;
650
651 bp = bufq_peek(sc->sc_tab);
652 rw = bp->b_flags & B_READ;
653 hpibgo(device_unit(device_parent(sc->sc_dev)), sc->sc_slave, C_EXEC,
654 sc->sc_addr, sc->sc_resid, rw, rw != 0);
655 }
656
657 /*
658 * Hideous grue to handle EOF/EOT (mostly for reads)
659 */
660 static void
661 cteof(struct ct_softc *sc, struct buf *bp)
662 {
663 long blks;
664
665 /*
666 * EOT on a write is an error.
667 */
668 if ((bp->b_flags & B_READ) == 0) {
669 bp->b_resid = bp->b_bcount;
670 bp->b_error = ENOSPC;
671 sc->sc_flags |= CTF_EOT;
672 return;
673 }
674 /*
675 * Use returned block position to determine how many blocks
676 * we really read and update b_resid.
677 */
678 blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
679 #ifdef DEBUG
680 if (ctdebug & CDB_FILES)
681 printf("cteof: bc %d oblk %d nblk %ld read %ld, resid %ld\n",
682 bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
683 blks, bp->b_bcount - CTKTOB(blks));
684 #endif
685 if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
686 blks = 0;
687 sc->sc_blkno++;
688 }
689 else {
690 sc->sc_blkno = sc->sc_stat.c_blk;
691 }
692 bp->b_resid = bp->b_bcount - CTKTOB(blks);
693 /*
694 * If we are at physical EOV or were after an EOF,
695 * we are now at logical EOT.
696 */
697 if ((sc->sc_stat.c_aef & AEF_EOV) ||
698 (sc->sc_flags & CTF_AEOF)) {
699 sc->sc_flags |= CTF_EOT;
700 sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
701 }
702 /*
703 * If we were before an EOF or we have just completed a FSF,
704 * we are now after EOF.
705 */
706 else if ((sc->sc_flags & CTF_BEOF) ||
707 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) {
708 sc->sc_flags |= CTF_AEOF;
709 sc->sc_flags &= ~CTF_BEOF;
710 }
711 /*
712 * Otherwise if we read something we are now before EOF
713 * (and no longer after EOF).
714 */
715 else if (blks) {
716 sc->sc_flags |= CTF_BEOF;
717 sc->sc_flags &= ~CTF_AEOF;
718 }
719 /*
720 * Finally, if we didn't read anything we just passed an EOF
721 */
722 else
723 sc->sc_flags |= CTF_AEOF;
724 #ifdef DEBUG
725 if (ctdebug & CDB_FILES)
726 printf("cteof: leaving flags %x\n", sc->sc_flags);
727 #endif
728 }
729
730 /* ARGSUSED */
731 static void
732 ctintr(void *arg)
733 {
734 struct ct_softc *sc = arg;
735 struct buf *bp;
736 uint8_t stat;
737 int ctlr, slave;
738
739 ctlr = device_unit(device_parent(sc->sc_dev));
740 slave = sc->sc_slave;
741
742 bp = bufq_peek(sc->sc_tab);
743 if (bp == NULL) {
744 printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
745 return;
746 }
747 if (sc->sc_flags & CTF_IO) {
748 sc->sc_flags &= ~CTF_IO;
749 if (hpibustart(ctlr))
750 ctgo(sc);
751 return;
752 }
753 if ((sc->sc_flags & CTF_STATWAIT) == 0) {
754 if (hpibpptest(ctlr, slave) == 0) {
755 sc->sc_flags |= CTF_STATWAIT;
756 hpibawait(ctlr);
757 return;
758 }
759 } else
760 sc->sc_flags &= ~CTF_STATWAIT;
761 hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
762 #ifdef DEBUG
763 if (ctdebug & CDB_FILES)
764 printf("ctintr: before flags %x\n", sc->sc_flags);
765 #endif
766 if (stat) {
767 sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
768 sc->sc_rsc.cmd = C_STATUS;
769 hpibsend(ctlr, slave, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
770 hpibrecv(ctlr, slave, C_EXEC, &sc->sc_stat,
771 sizeof(sc->sc_stat));
772 hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
773 #ifdef DEBUG
774 if (ctdebug & CDB_FILES)
775 printf("ctintr: return stat 0x%x, A%x F%x blk %ld\n",
776 stat, sc->sc_stat.c_aef,
777 sc->sc_stat.c_fef, sc->sc_stat.c_blk);
778 #endif
779 if (stat == 0) {
780 if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
781 cteof(sc, bp);
782 ctaddeof(sc);
783 goto done;
784 }
785 if (sc->sc_stat.c_fef & FEF_PF) {
786 ctreset(sc);
787 ctstart(sc);
788 return;
789 }
790 if (sc->sc_stat.c_fef & FEF_REXMT) {
791 ctstart(sc);
792 return;
793 }
794 if (sc->sc_stat.c_aef & 0x5800) {
795 if (sc->sc_stat.c_aef & 0x4000)
796 tprintf(sc->sc_tpr,
797 "%s: uninitialized media\n",
798 device_xname(sc->sc_dev));
799 if (sc->sc_stat.c_aef & 0x1000)
800 tprintf(sc->sc_tpr,
801 "%s: not ready\n",
802 device_xname(sc->sc_dev));
803 if (sc->sc_stat.c_aef & 0x0800)
804 tprintf(sc->sc_tpr,
805 "%s: write protect\n",
806 device_xname(sc->sc_dev));
807 } else {
808 printf("%s err: v%d u%d ru%d bn%ld, ",
809 device_xname(sc->sc_dev),
810 (sc->sc_stat.c_vu >> 4) & 0xF,
811 sc->sc_stat.c_vu & 0xF,
812 sc->sc_stat.c_pend,
813 sc->sc_stat.c_blk);
814 printf("R0x%x F0x%x A0x%x I0x%x\n",
815 sc->sc_stat.c_ref,
816 sc->sc_stat.c_fef,
817 sc->sc_stat.c_aef,
818 sc->sc_stat.c_ief);
819 }
820 } else
821 printf("%s: request status failed\n",
822 device_xname(sc->sc_dev));
823 bp->b_error = EIO;
824 goto done;
825 } else
826 bp->b_resid = 0;
827 if (sc->sc_flags & CTF_CMD) {
828 switch (sc->sc_cmd) {
829 case MTFSF:
830 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
831 sc->sc_blkno += CTBTOK(sc->sc_resid);
832 ctstart(sc);
833 return;
834 case MTBSF:
835 sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
836 break;
837 case MTBSR:
838 sc->sc_flags &= ~CTF_BEOF;
839 if (sc->sc_flags & CTF_EOT) {
840 sc->sc_flags |= CTF_AEOF;
841 sc->sc_flags &= ~CTF_EOT;
842 } else if (sc->sc_flags & CTF_AEOF) {
843 sc->sc_flags |= CTF_BEOF;
844 sc->sc_flags &= ~CTF_AEOF;
845 }
846 break;
847 case MTWEOF:
848 sc->sc_flags &= ~CTF_BEOF;
849 if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
850 sc->sc_flags |= CTF_EOT;
851 sc->sc_flags &= ~CTF_AEOF;
852 } else
853 sc->sc_flags |= CTF_AEOF;
854 break;
855 case MTREW:
856 case MTOFFL:
857 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
858 break;
859 }
860 } else {
861 sc->sc_flags &= ~CTF_AEOF;
862 sc->sc_blkno += CTBTOK(sc->sc_resid);
863 }
864 done:
865 #ifdef DEBUG
866 if (ctdebug & CDB_FILES)
867 printf("ctintr: after flags %x\n", sc->sc_flags);
868 #endif
869 ctdone(sc, bp);
870 }
871
872 static void
873 ctdone(struct ct_softc *sc, struct buf *bp)
874 {
875
876 (void)bufq_get(sc->sc_tab);
877 biodone(bp);
878 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
879 if (bufq_peek(sc->sc_tab) == NULL) {
880 sc->sc_active = 0;
881 return;
882 }
883 ctustart(sc);
884 }
885
886 static int
887 ctread(dev_t dev, struct uio *uio, int flags)
888 {
889
890 return physio(ctstrategy, NULL, dev, B_READ, minphys, uio);
891 }
892
893 static int
894 ctwrite(dev_t dev, struct uio *uio, int flags)
895 {
896
897 /* XXX: check for hardware write-protect? */
898 return physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio);
899 }
900
901 /*ARGSUSED*/
902 static int
903 ctioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
904 {
905 struct mtop *op;
906 int cnt;
907
908 switch (cmd) {
909
910 case MTIOCTOP:
911 op = (struct mtop *)data;
912 switch(op->mt_op) {
913
914 case MTWEOF:
915 case MTFSF:
916 case MTBSR:
917 case MTBSF:
918 case MTFSR:
919 cnt = op->mt_count;
920 break;
921
922 case MTREW:
923 case MTOFFL:
924 cnt = 1;
925 break;
926
927 default:
928 return EINVAL;
929 }
930 ctcommand(dev, op->mt_op, cnt);
931 break;
932
933 case MTIOCGET:
934 break;
935
936 default:
937 return EINVAL;
938 }
939 return 0;
940 }
941
942 static void
943 ctaddeof(struct ct_softc *sc)
944 {
945
946 if (sc->sc_eofp == EOFS - 1)
947 sc->sc_eofs[EOFS - 1]++;
948 else {
949 sc->sc_eofp++;
950 if (sc->sc_eofp == EOFS - 1)
951 sc->sc_eofs[EOFS - 1] = EOFS;
952 else
953 /* save blkno */
954 sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
955 }
956 #ifdef DEBUG
957 if (ctdebug & CT_BSF)
958 printf("%s: add eof pos %d blk %d\n",
959 device_xname(sc->sc_dev), sc->sc_eofp,
960 sc->sc_eofs[sc->sc_eofp]);
961 #endif
962 }
963