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