ct.c revision 1.52 1 /* $NetBSD: ct.c,v 1.52 2008/01/02 11:48:24 ad 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.52 2008/01/02 11:48:24 ad 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 const 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, "fcfs", 0);
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(device_unit(parent), ha->ha_slave, C_CMD, cmd, sizeof(cmd));
279 hpibrecv(device_unit(parent), ha->ha_slave, C_EXEC, &desc, 37);
280 hpibrecv(device_unit(parent), ha->ha_slave, C_QSTAT, &stat,
281 sizeof(stat));
282
283 memset(name, 0, sizeof(name));
284 if (stat == 0) {
285 n = desc.d_name;
286 for (i = 5; i >= 0; i--) {
287 name[i] = (n & 0xf) + '0';
288 n >>= 4;
289 }
290 }
291
292 switch (ha->ha_id) {
293 case CT7946ID:
294 if (memcmp(name, "079450", 6) == 0)
295 return 0; /* not really a 7946 */
296 /* fall into... */
297 case CT9144ID:
298 case CT9145ID:
299 case CT35401ID:
300 type = CT9144;
301 canstream = 1;
302 break;
303
304 case CT7912PID:
305 case CT7914PID:
306 type = CT88140;
307 break;
308 }
309
310 if (sc != NULL) {
311 sc->sc_type = type;
312 sc->sc_flags = canstream ? CTF_CANSTREAM : 0;
313 printf(": %s %stape\n", ctinfo[id].desc,
314 canstream ? "streaming " : "");
315 }
316
317 return 1;
318 }
319
320 static void
321 ctreset(struct ct_softc *sc)
322 {
323 int ctlr, slave;
324 u_char stat;
325
326 ctlr = device_unit(device_parent(&sc->sc_dev));
327 slave = sc->sc_slave;
328
329 sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
330 sc->sc_clear.cmd = C_CLEAR;
331 hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
332 hpibswait(ctlr, slave);
333 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
334
335 sc->sc_src.unit = C_SUNIT(CTCTLR);
336 sc->sc_src.nop = C_NOP;
337 sc->sc_src.cmd = C_SREL;
338 sc->sc_src.param = C_REL;
339 hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
340 hpibswait(ctlr, slave);
341 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
342
343 sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
344 sc->sc_ssmc.cmd = C_SSM;
345 sc->sc_ssmc.refm = REF_MASK;
346 sc->sc_ssmc.fefm = FEF_MASK;
347 sc->sc_ssmc.aefm = AEF_MASK;
348 sc->sc_ssmc.iefm = IEF_MASK;
349 hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
350 hpibswait(ctlr, slave);
351 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
352
353 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
354 sc->sc_soptc.nop = C_NOP;
355 sc->sc_soptc.cmd = C_SOPT;
356 sc->sc_soptc.opt = C_SPAR;
357 hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
358 hpibswait(ctlr, slave);
359 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
360 }
361
362 /*ARGSUSED*/
363 static int
364 ctopen(dev_t dev, int flag, int type, struct lwp *l)
365 {
366 struct ct_softc *sc;
367 u_char stat;
368 int cc, ctlr, slave;
369
370 if (UNIT(dev) >= ct_cd.cd_ndevs ||
371 (sc = ct_cd.cd_devs[UNIT(dev)]) == NULL ||
372 (sc->sc_flags & CTF_ALIVE) == 0)
373 return ENXIO;
374
375 if (sc->sc_flags & CTF_OPEN)
376 return EBUSY;
377
378 ctlr = device_unit(device_parent(&sc->sc_dev));
379 slave = sc->sc_slave;
380
381 sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
382 sc->sc_soptc.nop = C_NOP;
383 sc->sc_soptc.cmd = C_SOPT;
384 if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
385 sc->sc_soptc.opt = C_SPAR | C_IMRPT;
386 else
387 sc->sc_soptc.opt = C_SPAR;
388
389 /*
390 * Check the return of hpibsend() and hpibswait().
391 * Drive could be loading/unloading a tape. If not checked,
392 * driver hangs.
393 */
394 cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
395 if (cc != sizeof(sc->sc_soptc))
396 return EBUSY;
397
398 hpibswait(ctlr, slave);
399 cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
400 if (cc != sizeof(stat))
401 return EBUSY;
402
403 sc->sc_tpr = tprintf_open(l->l_proc);
404 sc->sc_flags |= CTF_OPEN;
405 return 0;
406 }
407
408 /*ARGSUSED*/
409 static int
410 ctclose(dev_t dev, int flag, int fmt, struct lwp *l)
411 {
412 struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
413
414 if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
415 (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
416 ctcommand(dev, MTWEOF, 2);
417 ctcommand(dev, MTBSR, 1);
418 if (sc->sc_eofp == EOFS - 1)
419 sc->sc_eofs[EOFS - 1]--;
420 else
421 sc->sc_eofp--;
422 #ifdef DEBUG
423 if(ctdebug & CT_BSF)
424 printf("%s: ctclose backup eofs prt %d blk %d\n",
425 sc->sc_dev.dv_xname, sc->sc_eofp,
426 sc->sc_eofs[sc->sc_eofp]);
427 #endif
428 }
429 if ((minor(dev) & CT_NOREW) == 0)
430 ctcommand(dev, MTREW, 1);
431 sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
432 tprintf_close(sc->sc_tpr);
433 #ifdef DEBUG
434 if (ctdebug & CDB_FILES)
435 printf("ctclose: flags %x\n", sc->sc_flags);
436 #endif
437 return 0; /* XXX */
438 }
439
440 static void
441 ctcommand(dev_t dev, int cmd, int cnt)
442 {
443 struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
444 struct buf *bp = &sc->sc_bufstore;
445 struct buf *nbp = 0;
446
447 if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
448 cnt = sc->sc_eofs[EOFS - 1] - cnt;
449 ctcommand(dev, MTREW, 1);
450 ctcommand(dev, MTFSF, cnt);
451 cnt = 2;
452 cmd = MTBSR;
453 }
454
455 if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
456 cnt = 1;
457 cmd = MTREW;
458 }
459
460 sc->sc_flags |= CTF_CMD;
461 sc->sc_bp = bp;
462 sc->sc_cmd = cmd;
463 bp->b_dev = dev;
464 if (cmd == MTFSF) {
465 nbp = (struct buf *)geteblk(MAXBSIZE);
466 bp->b_data = nbp->b_data;
467 bp->b_bcount = MAXBSIZE;
468 }
469
470 while (cnt-- > 0) {
471 bp->b_cflags = BC_BUSY;
472 if (cmd == MTBSF) {
473 sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
474 sc->sc_eofp--;
475 #ifdef DEBUG
476 if (ctdebug & CT_BSF)
477 printf("%s: backup eof pos %d blk %d\n",
478 sc->sc_dev.dv_xname, sc->sc_eofp,
479 sc->sc_eofs[sc->sc_eofp]);
480 #endif
481 }
482 ctstrategy(bp);
483 biowait(bp);
484 }
485 bp->b_flags = 0;
486 sc->sc_flags &= ~CTF_CMD;
487 if (nbp)
488 brelse(nbp, 0);
489 }
490
491 static void
492 ctstrategy(struct buf *bp)
493 {
494 int s, unit;
495 struct ct_softc *sc;
496
497 unit = UNIT(bp->b_dev);
498 sc = ct_cd.cd_devs[unit];
499
500 s = splbio();
501 BUFQ_PUT(sc->sc_tab, bp);
502 if (sc->sc_active == 0) {
503 sc->sc_active = 1;
504 ctustart(sc);
505 }
506 splx(s);
507 }
508
509 static void
510 ctustart(struct ct_softc *sc)
511 {
512 struct buf *bp;
513
514 bp = BUFQ_PEEK(sc->sc_tab);
515 sc->sc_addr = bp->b_data;
516 sc->sc_resid = bp->b_bcount;
517 if (hpibreq(device_parent(&sc->sc_dev), &sc->sc_hq))
518 ctstart(sc);
519 }
520
521 static void
522 ctstart(void *arg)
523 {
524 struct ct_softc *sc = arg;
525 struct buf *bp;
526 int i, ctlr, slave;
527
528 ctlr = device_unit(device_parent(&sc->sc_dev));
529 slave = sc->sc_slave;
530
531 bp = BUFQ_PEEK(sc->sc_tab);
532 if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
533 switch(sc->sc_cmd) {
534 case MTFSF:
535 bp->b_flags |= B_READ;
536 goto mustio;
537
538 case MTBSF:
539 goto gotaddr;
540
541 case MTOFFL:
542 sc->sc_blkno = 0;
543 sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
544 sc->sc_ul.cmd = C_UNLOAD;
545 hpibsend(ctlr, slave, C_CMD, &sc->sc_ul,
546 sizeof(sc->sc_ul));
547 break;
548
549 case MTWEOF:
550 sc->sc_blkno++;
551 sc->sc_flags |= CTF_WRT;
552 sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
553 sc->sc_wfm.cmd = C_WFM;
554 hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm,
555 sizeof(sc->sc_wfm));
556 ctaddeof(sc);
557 break;
558
559 case MTBSR:
560 sc->sc_blkno--;
561 goto gotaddr;
562
563 case MTFSR:
564 sc->sc_blkno++;
565 goto gotaddr;
566
567 case MTREW:
568 sc->sc_blkno = 0;
569 #ifdef DEBUG
570 if(ctdebug & CT_BSF)
571 printf("%s: clearing eofs\n",
572 sc->sc_dev.dv_xname);
573 #endif
574 for (i=0; i<EOFS; i++)
575 sc->sc_eofs[i] = 0;
576 sc->sc_eofp = 0;
577
578 gotaddr:
579 sc->sc_ioc.saddr = C_SADDR;
580 sc->sc_ioc.addr0 = 0;
581 sc->sc_ioc.addr = sc->sc_blkno;
582 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
583 sc->sc_ioc.nop2 = C_NOP;
584 sc->sc_ioc.slen = C_SLEN;
585 sc->sc_ioc.len = 0;
586 sc->sc_ioc.nop3 = C_NOP;
587 sc->sc_ioc.cmd = C_READ;
588 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc,
589 sizeof(sc->sc_ioc));
590 break;
591 }
592 } else {
593 mustio:
594 if ((bp->b_flags & B_READ) &&
595 sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
596 #ifdef DEBUG
597 if (ctdebug & CDB_FILES)
598 printf("ctstart: before flags %x\n",
599 sc->sc_flags);
600 #endif
601 if (sc->sc_flags & CTF_BEOF) {
602 sc->sc_flags &= ~CTF_BEOF;
603 sc->sc_flags |= CTF_AEOF;
604 #ifdef DEBUG
605 if (ctdebug & CDB_FILES)
606 printf("ctstart: after flags %x\n",
607 sc->sc_flags);
608 #endif
609 }
610 bp->b_resid = bp->b_bcount;
611 ctdone(sc, bp);
612 return;
613 }
614 sc->sc_flags |= CTF_IO;
615 sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
616 sc->sc_ioc.saddr = C_SADDR;
617 sc->sc_ioc.addr0 = 0;
618 sc->sc_ioc.addr = sc->sc_blkno;
619 sc->sc_ioc.nop2 = C_NOP;
620 sc->sc_ioc.slen = C_SLEN;
621 sc->sc_ioc.len = sc->sc_resid;
622 sc->sc_ioc.nop3 = C_NOP;
623 if (bp->b_flags & B_READ)
624 sc->sc_ioc.cmd = C_READ;
625 else {
626 sc->sc_ioc.cmd = C_WRITE;
627 sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
628 }
629 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
630 }
631 hpibawait(ctlr);
632 }
633
634 static void
635 ctgo(void *arg)
636 {
637 struct ct_softc *sc = arg;
638 struct buf *bp;
639 int rw;
640
641 bp = BUFQ_PEEK(sc->sc_tab);
642 rw = bp->b_flags & B_READ;
643 hpibgo(device_unit(device_parent(&sc->sc_dev)), sc->sc_slave, C_EXEC,
644 sc->sc_addr, sc->sc_resid, rw, rw != 0);
645 }
646
647 /*
648 * Hideous grue to handle EOF/EOT (mostly for reads)
649 */
650 static void
651 cteof(struct ct_softc *sc, struct buf *bp)
652 {
653 long blks;
654
655 /*
656 * EOT on a write is an error.
657 */
658 if ((bp->b_flags & B_READ) == 0) {
659 bp->b_resid = bp->b_bcount;
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 = device_unit(device_parent(&sc->sc_dev));
730 slave = sc->sc_slave;
731 unit = device_unit(&sc->sc_dev);
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_error = EIO;
815 goto done;
816 } else
817 bp->b_resid = 0;
818 if (sc->sc_flags & CTF_CMD) {
819 switch (sc->sc_cmd) {
820 case MTFSF:
821 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
822 sc->sc_blkno += CTBTOK(sc->sc_resid);
823 ctstart(sc);
824 return;
825 case MTBSF:
826 sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
827 break;
828 case MTBSR:
829 sc->sc_flags &= ~CTF_BEOF;
830 if (sc->sc_flags & CTF_EOT) {
831 sc->sc_flags |= CTF_AEOF;
832 sc->sc_flags &= ~CTF_EOT;
833 } else if (sc->sc_flags & CTF_AEOF) {
834 sc->sc_flags |= CTF_BEOF;
835 sc->sc_flags &= ~CTF_AEOF;
836 }
837 break;
838 case MTWEOF:
839 sc->sc_flags &= ~CTF_BEOF;
840 if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
841 sc->sc_flags |= CTF_EOT;
842 sc->sc_flags &= ~CTF_AEOF;
843 } else
844 sc->sc_flags |= CTF_AEOF;
845 break;
846 case MTREW:
847 case MTOFFL:
848 sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
849 break;
850 }
851 } else {
852 sc->sc_flags &= ~CTF_AEOF;
853 sc->sc_blkno += CTBTOK(sc->sc_resid);
854 }
855 done:
856 #ifdef DEBUG
857 if (ctdebug & CDB_FILES)
858 printf("ctintr: after flags %x\n", sc->sc_flags);
859 #endif
860 ctdone(sc, bp);
861 }
862
863 static void
864 ctdone(struct ct_softc *sc, struct buf *bp)
865 {
866
867 (void)BUFQ_GET(sc->sc_tab);
868 biodone(bp);
869 hpibfree(device_parent(&sc->sc_dev), &sc->sc_hq);
870 if (BUFQ_PEEK(sc->sc_tab) == NULL) {
871 sc->sc_active = 0;
872 return;
873 }
874 ctustart(sc);
875 }
876
877 static int
878 ctread(dev_t dev, struct uio *uio, int flags)
879 {
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
888 /* XXX: check for hardware write-protect? */
889 return physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio);
890 }
891
892 /*ARGSUSED*/
893 static int
894 ctioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
895 {
896 struct mtop *op;
897 int cnt;
898
899 switch (cmd) {
900
901 case MTIOCTOP:
902 op = (struct mtop *)data;
903 switch(op->mt_op) {
904
905 case MTWEOF:
906 case MTFSF:
907 case MTBSR:
908 case MTBSF:
909 case MTFSR:
910 cnt = op->mt_count;
911 break;
912
913 case MTREW:
914 case MTOFFL:
915 cnt = 1;
916 break;
917
918 default:
919 return EINVAL;
920 }
921 ctcommand(dev, op->mt_op, cnt);
922 break;
923
924 case MTIOCGET:
925 break;
926
927 default:
928 return EINVAL;
929 }
930 return 0;
931 }
932
933 static void
934 ctaddeof(struct ct_softc *sc)
935 {
936
937 if (sc->sc_eofp == EOFS - 1)
938 sc->sc_eofs[EOFS - 1]++;
939 else {
940 sc->sc_eofp++;
941 if (sc->sc_eofp == EOFS - 1)
942 sc->sc_eofs[EOFS - 1] = EOFS;
943 else
944 /* save blkno */
945 sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
946 }
947 #ifdef DEBUG
948 if (ctdebug & CT_BSF)
949 printf("%s: add eof pos %d blk %d\n",
950 sc->sc_dev.dv_xname, sc->sc_eofp,
951 sc->sc_eofs[sc->sc_eofp]);
952 #endif
953 }
954