mt.c revision 1.11 1 /* $NetBSD: mt.c,v 1.11 2000/01/21 23:29:03 thorpej 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) 1992, The University of Utah and
41 * the Computer Systems Laboratory at the University of Utah (CSL).
42 * All rights reserved.
43 *
44 * Permission to use, copy, modify and distribute this software is hereby
45 * granted provided that (1) source code retains these copyright, permission,
46 * and disclaimer notices, and (2) redistributions including binaries
47 * reproduce the notices in supporting documentation, and (3) all advertising
48 * materials mentioning features or use of this software display the following
49 * acknowledgement: ``This product includes software developed by the
50 * Computer Systems Laboratory at the University of Utah.''
51 *
52 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
53 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
54 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * CSL requests users of this software to return to csl-dist (at) cs.utah.edu any
57 * improvements that they make and grant CSL redistribution rights.
58 *
59 * Utah $Hdr: mt.c 1.8 95/09/12$
60 */
61 /* @(#)mt.c 3.9 90/07/10 mt Xinu
62 *
63 * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
64 * Original version contributed by Mt. Xinu.
65 * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
66 * Computer Science, Victoria University of Wellington
67 */
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/buf.h>
72 #include <sys/ioctl.h>
73 #include <sys/mtio.h>
74 #include <sys/file.h>
75 #include <sys/proc.h>
76 #include <sys/errno.h>
77 #include <sys/syslog.h>
78 #include <sys/tty.h>
79 #include <sys/kernel.h>
80 #include <sys/tprintf.h>
81 #include <sys/device.h>
82 #include <sys/conf.h>
83
84 #include <hp300/dev/hpibvar.h>
85
86 #include <hp300/dev/mtreg.h>
87
88 struct mtinfo {
89 u_short hwid;
90 char *desc;
91 } mtinfo[] = {
92 { MT7978ID, "7978" },
93 { MT7979AID, "7979A" },
94 { MT7980ID, "7980" },
95 { MT7974AID, "7974A" },
96 };
97 int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
98
99 struct mt_softc {
100 struct device sc_dev;
101 int sc_hpibno; /* logical HPIB this slave it attached to */
102 int sc_slave; /* HPIB slave address (0-6) */
103 short sc_flags; /* see below */
104 u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */
105 u_char sc_lastecmd; /* place for End Command in mtreaddsj() */
106 short sc_recvtimeo; /* count of hpibsend timeouts to prevent hang */
107 short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */
108 struct mt_stat sc_stat;/* status bytes last read from device */
109 short sc_density; /* current density of tape (mtio.h format) */
110 short sc_type; /* tape drive model (hardware IDs) */
111 struct hpibqueue sc_hq; /* HPIB device queue member */
112 tpr_t sc_ttyp;
113 struct buf_queue sc_tab;/* buf queue */
114 int sc_active;
115 struct buf sc_bufstore; /* XXX buffer storage */
116 };
117
118 #ifdef DEBUG
119 int mtdebug = 0;
120 #define dlog if (mtdebug) log
121 #else
122 #define dlog if (0) log
123 #endif
124
125 #define UNIT(x) (minor(x) & 3)
126
127 #define B_CMD B_XXX /* command buf instead of data */
128 #define b_cmd b_blkno /* blkno holds cmd when B_CMD */
129
130 int mtmatch __P((struct device *, struct cfdata *, void *));
131 void mtattach __P((struct device *, struct device *, void *));
132
133 struct cfattach mt_ca = {
134 sizeof(struct mt_softc), mtmatch, mtattach
135 };
136
137 extern struct cfdriver mt_cd;
138
139 int mtident __P((struct mt_softc *, struct hpibbus_attach_args *));
140 void mtustart __P((struct mt_softc *));
141 int mtreaddsj __P((struct mt_softc *, int));
142 int mtcommand __P((dev_t, int, int));
143 void spl_mtintr __P((void *));
144 void spl_mtstart __P((void *));
145
146 void mtstart __P((void *));
147 void mtgo __P((void *));
148 void mtintr __P((void *));
149
150 bdev_decl(mt);
151 cdev_decl(mt);
152
153 int
154 mtmatch(parent, match, aux)
155 struct device *parent;
156 struct cfdata *match;
157 void *aux;
158 {
159 struct hpibbus_attach_args *ha = aux;
160
161 return (mtident(NULL, ha));
162 }
163
164 void
165 mtattach(parent, self, aux)
166 struct device *parent, *self;
167 void *aux;
168 {
169 struct mt_softc *sc = (struct mt_softc *)self;
170 struct hpibbus_attach_args *ha = aux;
171 int unit, hpibno, slave;
172
173 if (mtident(sc, ha) == 0) {
174 printf("\n%s: impossible!\n", sc->sc_dev.dv_xname);
175 return;
176 }
177
178 unit = self->dv_unit;
179 hpibno = parent->dv_unit;
180 slave = ha->ha_slave;
181
182 BUFQ_INIT(&sc->sc_tab);
183
184 sc->sc_hpibno = hpibno;
185 sc->sc_slave = slave;
186 sc->sc_flags = MTF_EXISTS;
187
188 /* Initialize hpib job queue entry. */
189 sc->sc_hq.hq_softc = sc;
190 sc->sc_hq.hq_slave = sc->sc_slave;
191 sc->sc_hq.hq_start = mtstart;
192 sc->sc_hq.hq_go = mtgo;
193 sc->sc_hq.hq_intr = mtintr;
194 }
195
196 int
197 mtident(sc, ha)
198 struct mt_softc *sc;
199 struct hpibbus_attach_args *ha;
200 {
201 int i;
202
203 for (i = 0; i < nmtinfo; i++) {
204 if (ha->ha_id == mtinfo[i].hwid) {
205 if (sc != NULL) {
206 sc->sc_type = mtinfo[i].hwid;
207 printf(": %s tape\n", mtinfo[i].desc);
208 }
209 return (1);
210 }
211 }
212 return (0);
213 }
214
215 /*
216 * Perform a read of "Device Status Jump" register and update the
217 * status if necessary. If status is read, the given "ecmd" is also
218 * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure
219 * and -2 on "temporary" failure.
220 */
221 int
222 mtreaddsj(sc, ecmd)
223 struct mt_softc *sc;
224 int ecmd;
225 {
226 int retval;
227
228 if (sc->sc_flags & MTF_STATTIMEO)
229 goto getstats;
230 retval = hpibrecv(sc->sc_hpibno,
231 (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
232 MTT_DSJ, &(sc->sc_lastdsj), 1);
233 sc->sc_flags &= ~MTF_DSJTIMEO;
234 if (retval != 1) {
235 dlog(LOG_DEBUG, "%s can't hpibrecv DSJ",
236 sc->sc_dev.dv_xname);
237 if (sc->sc_recvtimeo == 0)
238 sc->sc_recvtimeo = hz;
239 if (--sc->sc_recvtimeo == 0)
240 return (-1);
241 if (retval == 0)
242 sc->sc_flags |= MTF_DSJTIMEO;
243 return (-2);
244 }
245 sc->sc_recvtimeo = 0;
246 sc->sc_statindex = 0;
247 dlog(LOG_DEBUG, "%s readdsj: 0x%x", sc->sc_dev.dv_xname,
248 sc->sc_lastdsj);
249 sc->sc_lastecmd = ecmd;
250 switch (sc->sc_lastdsj) {
251 case 0:
252 if (ecmd & MTE_DSJ_FORCE)
253 break;
254 return (0);
255
256 case 2:
257 sc->sc_lastecmd = MTE_COMPLETE;
258 case 1:
259 break;
260
261 default:
262 log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname,
263 sc->sc_lastdsj);
264 return (-1);
265 }
266 getstats:
267 retval = hpibrecv(sc->sc_hpibno,
268 (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
269 MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
270 sizeof(sc->sc_stat) - sc->sc_statindex);
271 sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
272 if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
273 if (sc->sc_recvtimeo == 0)
274 sc->sc_recvtimeo = hz;
275 if (--sc->sc_recvtimeo != 0) {
276 if (retval >= 0) {
277 sc->sc_statindex += retval;
278 sc->sc_flags |= MTF_STATCONT;
279 }
280 sc->sc_flags |= MTF_STATTIMEO;
281 return (-2);
282 }
283 log(LOG_ERR, "%s readdsj: can't read status",
284 sc->sc_dev.dv_xname);
285 return (-1);
286 }
287 sc->sc_recvtimeo = 0;
288 sc->sc_statindex = 0;
289 dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x",
290 sc->sc_dev.dv_xname,
291 sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
292 sc->sc_stat4, sc->sc_stat5, sc->sc_stat6);
293 if (sc->sc_lastecmd)
294 (void) hpibsend(sc->sc_hpibno, sc->sc_slave,
295 MTL_ECMD, &(sc->sc_lastecmd), 1);
296 return ((int) sc->sc_lastdsj);
297 }
298
299 int
300 mtopen(dev, flag, mode, p)
301 dev_t dev;
302 int flag, mode;
303 struct proc *p;
304 {
305 int unit = UNIT(dev);
306 struct mt_softc *sc;
307 int req_den;
308 int error;
309
310 if (unit >= mt_cd.cd_ndevs ||
311 (sc = mt_cd.cd_devs[unit]) == NULL ||
312 (sc->sc_flags & MTF_EXISTS) == 0)
313 return (ENXIO);
314
315 dlog(LOG_DEBUG, "%s open: flags 0x%x", sc->sc_dev.dv_xname,
316 sc->sc_flags);
317 if (sc->sc_flags & MTF_OPEN)
318 return (EBUSY);
319 sc->sc_flags |= MTF_OPEN;
320 sc->sc_ttyp = tprintf_open(p);
321 if ((sc->sc_flags & MTF_ALIVE) == 0) {
322 error = mtcommand(dev, MTRESET, 0);
323 if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
324 goto errout;
325 if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
326 (void) mtcommand(dev, MTREW, 0);
327 }
328 for (;;) {
329 if ((error = mtcommand(dev, MTNOP, 0)) != 0)
330 goto errout;
331 if (!(sc->sc_flags & MTF_REW))
332 break;
333 if (tsleep((caddr_t) &lbolt, PCATCH | (PZERO + 1),
334 "mt", 0) != 0) {
335 error = EINTR;
336 goto errout;
337 }
338 }
339 if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
340 error = EROFS;
341 goto errout;
342 }
343 if (!(sc->sc_stat1 & SR1_ONLINE)) {
344 uprintf("%s: not online\n", sc->sc_dev.dv_xname);
345 error = EIO;
346 goto errout;
347 }
348 /*
349 * Select density:
350 * - find out what density the drive is set to
351 * (i.e. the density of the current tape)
352 * - if we are going to write
353 * - if we're not at the beginning of the tape
354 * - complain if we want to change densities
355 * - otherwise, select the mtcommand to set the density
356 *
357 * If the drive doesn't support it then don't change the recorded
358 * density.
359 *
360 * The original MOREbsd code had these additional conditions
361 * for the mid-tape change
362 *
363 * req_den != T_BADBPI &&
364 * sc->sc_density != T_6250BPI
365 *
366 * which suggests that it would be possible to write multiple
367 * densities if req_den == T_BAD_BPI or the current tape
368 * density was 6250. Testing of our 7980 suggests that the
369 * device cannot change densities mid-tape.
370 *
371 * ajv (at) comp.vuw.ac.nz
372 */
373 sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
374 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
375 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
376 req_den = (dev & T_DENSEL);
377
378 if (flag & FWRITE) {
379 if (!(sc->sc_stat1 & SR1_BOT)) {
380 if (sc->sc_density != req_den) {
381 uprintf("%s: can't change density mid-tape\n",
382 sc->sc_dev.dv_xname);
383 error = EIO;
384 goto errout;
385 }
386 }
387 else {
388 int mtset_density =
389 (req_den == T_800BPI ? MTSET800BPI : (
390 req_den == T_1600BPI ? MTSET1600BPI : (
391 req_den == T_6250BPI ? MTSET6250BPI : (
392 sc->sc_type == MT7980ID
393 ? MTSET6250DC
394 : MTSET6250BPI))));
395 if (mtcommand(dev, mtset_density, 0) == 0)
396 sc->sc_density = req_den;
397 }
398 }
399 return (0);
400 errout:
401 sc->sc_flags &= ~MTF_OPEN;
402 return (error);
403 }
404
405 int
406 mtclose(dev, flag, fmt, p)
407 dev_t dev;
408 int flag, fmt;
409 struct proc *p;
410 {
411 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
412
413 if (sc->sc_flags & MTF_WRT) {
414 (void) mtcommand(dev, MTWEOF, 2);
415 (void) mtcommand(dev, MTBSF, 0);
416 }
417 if ((minor(dev) & T_NOREWIND) == 0)
418 (void) mtcommand(dev, MTREW, 0);
419 sc->sc_flags &= ~MTF_OPEN;
420 tprintf_close(sc->sc_ttyp);
421 return (0);
422 }
423
424 int
425 mtcommand(dev, cmd, cnt)
426 dev_t dev;
427 int cmd;
428 int cnt;
429 {
430 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
431 struct buf *bp = &sc->sc_bufstore;
432 int error = 0;
433
434 #if 1
435 if (bp->b_flags & B_BUSY)
436 return (EBUSY);
437 #endif
438 bp->b_cmd = cmd;
439 bp->b_dev = dev;
440 do {
441 bp->b_flags = B_BUSY | B_CMD;
442 mtstrategy(bp);
443 biowait(bp);
444 if (bp->b_flags & B_ERROR) {
445 error = (int) (unsigned) bp->b_error;
446 break;
447 }
448 } while (--cnt > 0);
449 #if 0
450 bp->b_flags = 0 /*&= ~B_BUSY*/;
451 #else
452 bp->b_flags &= ~B_BUSY;
453 #endif
454 return (error);
455 }
456
457 /*
458 * Only thing to check here is for legal record lengths (writes only).
459 */
460 void
461 mtstrategy(bp)
462 struct buf *bp;
463 {
464 struct mt_softc *sc;
465 int unit;
466 int s;
467
468 unit = UNIT(bp->b_dev);
469 sc = mt_cd.cd_devs[unit];
470 dlog(LOG_DEBUG, "%s strategy", sc->sc_dev.dv_xname);
471 if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
472 #define WRITE_BITS_IGNORED 8
473 #if 0
474 if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
475 tprintf(sc->sc_ttyp,
476 "%s: write record must be multiple of %d\n",
477 sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED);
478 goto error;
479 }
480 #endif
481 s = 16 * 1024;
482 if (sc->sc_stat2 & SR2_LONGREC) {
483 switch (sc->sc_density) {
484 case T_1600BPI:
485 s = 32 * 1024;
486 break;
487
488 case T_6250BPI:
489 case T_BADBPI:
490 s = 60 * 1024;
491 break;
492 }
493 }
494 if (bp->b_bcount > s) {
495 tprintf(sc->sc_ttyp,
496 "%s: write record (%ld) too big: limit (%d)\n",
497 sc->sc_dev.dv_xname, bp->b_bcount, s);
498 #if 0 /* XXX see above */
499 error:
500 #endif
501 bp->b_flags |= B_ERROR;
502 bp->b_error = EIO;
503 biodone(bp);
504 return;
505 }
506 }
507 s = splbio();
508 BUFQ_INSERT_TAIL(&sc->sc_tab, bp);
509 if (sc->sc_active == 0) {
510 sc->sc_active = 1;
511 mtustart(sc);
512 }
513 splx(s);
514 }
515
516 void
517 mtustart(sc)
518 struct mt_softc *sc;
519 {
520
521 dlog(LOG_DEBUG, "%s ustart", sc->sc_dev.dv_xname);
522 if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
523 mtstart(sc);
524 }
525
526 void
527 spl_mtintr(arg)
528 void *arg;
529 {
530 struct mt_softc *sc = arg;
531 int s = splbio();
532
533 hpibppclear(sc->sc_hpibno);
534 mtintr(sc);
535 (void) splx(s);
536 }
537
538 void
539 spl_mtstart(arg)
540 void *arg;
541 {
542 int s = splbio();
543
544 mtstart(arg);
545 (void) splx(s);
546 }
547
548 void
549 mtstart(arg)
550 void *arg;
551 {
552 struct mt_softc *sc = arg;
553 struct buf *bp, *dp;
554 short cmdcount = 1;
555 u_char cmdbuf[2];
556
557 dlog(LOG_DEBUG, "%s start", sc->sc_dev.dv_xname);
558 sc->sc_flags &= ~MTF_WRT;
559 bp = BUFQ_FIRST(&sc->sc_tab);
560 if ((sc->sc_flags & MTF_ALIVE) == 0 &&
561 ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
562 goto fatalerror;
563
564 if (sc->sc_flags & MTF_REW) {
565 if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
566 goto stillrew;
567 switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
568 case 0:
569 case 1:
570 stillrew:
571 if ((sc->sc_stat1 & SR1_BOT) ||
572 !(sc->sc_stat1 & SR1_ONLINE)) {
573 sc->sc_flags &= ~MTF_REW;
574 break;
575 }
576 case -2:
577 /*
578 * -2 means "timeout" reading DSJ, which is probably
579 * temporary. This is considered OK when doing a NOP,
580 * but not otherwise.
581 */
582 if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
583 timeout(spl_mtstart, sc, hz >> 5);
584 return;
585 }
586 case 2:
587 if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
588 bp->b_error = EBUSY;
589 goto errdone;
590 }
591 goto done;
592
593 default:
594 goto fatalerror;
595 }
596 }
597 if (bp->b_flags & B_CMD) {
598 if (sc->sc_flags & MTF_PASTEOT) {
599 switch(bp->b_cmd) {
600 case MTFSF:
601 case MTWEOF:
602 case MTFSR:
603 bp->b_error = ENOSPC;
604 goto errdone;
605
606 case MTBSF:
607 case MTOFFL:
608 case MTBSR:
609 case MTREW:
610 sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
611 break;
612 }
613 }
614 switch(bp->b_cmd) {
615 case MTFSF:
616 if (sc->sc_flags & MTF_HITEOF)
617 goto done;
618 cmdbuf[0] = MTTC_FSF;
619 break;
620
621 case MTBSF:
622 if (sc->sc_flags & MTF_HITBOF)
623 goto done;
624 cmdbuf[0] = MTTC_BSF;
625 break;
626
627 case MTOFFL:
628 sc->sc_flags |= MTF_REW;
629 cmdbuf[0] = MTTC_REWOFF;
630 break;
631
632 case MTWEOF:
633 cmdbuf[0] = MTTC_WFM;
634 break;
635
636 case MTBSR:
637 cmdbuf[0] = MTTC_BSR;
638 break;
639
640 case MTFSR:
641 cmdbuf[0] = MTTC_FSR;
642 break;
643
644 case MTREW:
645 sc->sc_flags |= MTF_REW;
646 cmdbuf[0] = MTTC_REW;
647 break;
648
649 case MTNOP:
650 /*
651 * NOP is supposed to set status bits.
652 * Force readdsj to do it.
653 */
654 switch (mtreaddsj(sc,
655 MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
656 default:
657 goto done;
658
659 case -1:
660 /*
661 * If this fails, perform a device clear
662 * to fix any protocol problems and (most
663 * likely) get the status.
664 */
665 bp->b_cmd = MTRESET;
666 break;
667
668 case -2:
669 timeout(spl_mtstart, sc, hz >> 5);
670 return;
671 }
672
673 case MTRESET:
674 /*
675 * 1) selected device clear (send with "-2" secondary)
676 * 2) set timeout, then wait for "service request"
677 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
678 */
679 if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
680 log(LOG_ERR, "%s can't reset",
681 sc->sc_dev.dv_xname);
682 goto fatalerror;
683 }
684 timeout(spl_mtintr, sc, 4 * hz);
685 hpibawait(sc->sc_hpibno);
686 return;
687
688 case MTSET800BPI:
689 cmdbuf[0] = MTTC_800;
690 break;
691
692 case MTSET1600BPI:
693 cmdbuf[0] = MTTC_1600;
694 break;
695
696 case MTSET6250BPI:
697 cmdbuf[0] = MTTC_6250;
698 break;
699
700 case MTSET6250DC:
701 cmdbuf[0] = MTTC_DC6250;
702 break;
703 }
704 } else {
705 if (sc->sc_flags & MTF_PASTEOT) {
706 bp->b_error = ENOSPC;
707 goto errdone;
708 }
709 if (bp->b_flags & B_READ) {
710 sc->sc_flags |= MTF_IO;
711 cmdbuf[0] = MTTC_READ;
712 } else {
713 sc->sc_flags |= MTF_WRT | MTF_IO;
714 cmdbuf[0] = MTTC_WRITE;
715 cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
716 cmdcount = 2;
717 }
718 }
719 if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
720 == cmdcount) {
721 if (sc->sc_flags & MTF_REW)
722 goto done;
723 hpibawait(sc->sc_hpibno);
724 return;
725 }
726 fatalerror:
727 /*
728 * If anything fails, the drive is probably hosed, so mark it not
729 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
730 * if, last we heard, it was REWinding, remember that).
731 */
732 sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
733 bp->b_error = EIO;
734 errdone:
735 bp->b_flags |= B_ERROR;
736 done:
737 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
738 BUFQ_REMOVE(&sc->sc_tab, bp);
739 biodone(bp);
740 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
741 if ((bp = BUFQ_FIRST(&sc->sc_tab)) == NULL)
742 sc->sc_active = 0;
743 else
744 mtustart(sc);
745 }
746
747 /*
748 * The Utah code had a bug which meant that the driver was unable to read.
749 * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
750 * -- ajv (at) comp.vuw.ac.nz
751 */
752 void
753 mtgo(arg)
754 void *arg;
755 {
756 struct mt_softc *sc = arg;
757 struct buf *bp;
758 int rw;
759
760 dlog(LOG_DEBUG, "%s go", sc->sc_dev.dv_xname);
761 bp = BUFQ_FIRST(&sc->sc_tab);
762 rw = bp->b_flags & B_READ;
763 hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
764 bp->b_un.b_addr, bp->b_bcount, rw, rw != 0);
765 }
766
767 void
768 mtintr(arg)
769 void *arg;
770 {
771 struct mt_softc *sc = arg;
772 struct buf *bp, *dp;
773 int i;
774 u_char cmdbuf[4];
775
776 bp = BUFQ_FIRST(&sc->sc_tab);
777 if (bp == NULL) {
778 log(LOG_ERR, "%s intr: bp == NULL", sc->sc_dev.dv_xname);
779 return;
780 }
781
782 dlog(LOG_DEBUG, "%s intr", sc->sc_dev.dv_xname);
783
784 /*
785 * Some operation completed. Read status bytes and report errors.
786 * Clear EOF flags here `cause they're set once on specific conditions
787 * below when a command succeeds.
788 * A DSJ of 2 always means keep waiting. If the command was READ
789 * (and we're in data DMA phase) stop data transfer first.
790 */
791 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
792 if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
793 !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
794 cmdbuf[0] = MTE_STOP;
795 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
796 }
797 switch (mtreaddsj(sc, 0)) {
798 case 0:
799 break;
800
801 case 1:
802 /*
803 * If we're in the middle of a READ/WRITE and have yet to
804 * start the data transfer, a DSJ of one should terminate it.
805 */
806 sc->sc_flags &= ~MTF_IO;
807 break;
808
809 case 2:
810 (void) hpibawait(sc->sc_hpibno);
811 return;
812
813 case -2:
814 /*
815 * -2 means that the drive failed to respond quickly enough
816 * to the request for DSJ. It's probably just "busy" figuring
817 * it out and will know in a little bit...
818 */
819 timeout(spl_mtintr, sc, hz >> 5);
820 return;
821
822 default:
823 log(LOG_ERR, "%s intr: can't get drive stat",
824 sc->sc_dev.dv_xname);
825 goto error;
826 }
827 if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
828 i = sc->sc_stat4 & SR4_ERCLMASK;
829 log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
830 sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" :
831 (i == SR4_PROTOCOL ? "protocol" :
832 (i == SR4_SELFTEST ? "selftest" : "unknown")),
833 sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
834 sc->sc_stat3, sc->sc_stat5);
835
836 if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
837 untimeout(spl_mtintr, sc);
838 if (sc->sc_stat3 & SR3_POWERUP)
839 sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
840 goto error;
841 }
842 /*
843 * Report and clear any soft errors.
844 */
845 if (sc->sc_stat1 & SR1_SOFTERR) {
846 log(LOG_WARNING, "%s: soft error, retry %d\n",
847 sc->sc_dev.dv_xname, sc->sc_stat4 & SR4_RETRYMASK);
848 sc->sc_stat1 &= ~SR1_SOFTERR;
849 }
850 /*
851 * We've initiated a read or write, but haven't actually started to
852 * DMA the data yet. At this point, the drive's ready.
853 */
854 if (sc->sc_flags & MTF_IO) {
855 sc->sc_flags &= ~MTF_IO;
856 if (hpibustart(sc->sc_hpibno))
857 mtgo(sc);
858 return;
859 }
860 /*
861 * Check for End Of Tape - we're allowed to hit EOT and then write (or
862 * read) one more record. If we get here and have not already hit EOT,
863 * return ENOSPC to inform the process that it's hit it. If we get
864 * here and HAVE already hit EOT, don't allow any more operations that
865 * move the tape forward.
866 */
867 if (sc->sc_stat1 & SR1_EOT) {
868 if (sc->sc_flags & MTF_ATEOT)
869 sc->sc_flags |= MTF_PASTEOT;
870 else {
871 bp->b_flags |= B_ERROR;
872 bp->b_error = ENOSPC;
873 sc->sc_flags |= MTF_ATEOT;
874 }
875 }
876 /*
877 * If a motion command was being executed, check for Tape Marks.
878 * If we were doing data, make sure we got the right amount, and
879 * check for hitting tape marks on reads.
880 */
881 if (bp->b_flags & B_CMD) {
882 if (sc->sc_stat1 & SR1_EOF) {
883 if (bp->b_cmd == MTFSR)
884 sc->sc_flags |= MTF_HITEOF;
885 if (bp->b_cmd == MTBSR)
886 sc->sc_flags |= MTF_HITBOF;
887 }
888 if (bp->b_cmd == MTRESET) {
889 untimeout(spl_mtintr, sc);
890 sc->sc_flags |= MTF_ALIVE;
891 }
892 } else {
893 i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
894 if (i != 2) {
895 log(LOG_ERR, "%s intr: can't get xfer length\n",
896 sc->sc_dev.dv_xname);
897 goto error;
898 }
899 i = (int) *((u_short *) cmdbuf);
900 if (i <= bp->b_bcount) {
901 if (i == 0)
902 sc->sc_flags |= MTF_HITEOF;
903 bp->b_resid = bp->b_bcount - i;
904 dlog(LOG_DEBUG, "%s intr: bcount %ld, resid %ld",
905 sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid);
906 } else {
907 tprintf(sc->sc_ttyp,
908 "%s: record (%d) larger than wanted (%ld)\n",
909 sc->sc_dev.dv_xname, i, bp->b_bcount);
910 error:
911 sc->sc_flags &= ~MTF_IO;
912 bp->b_error = EIO;
913 bp->b_flags |= B_ERROR;
914 }
915 }
916 /*
917 * The operation is completely done.
918 * Let the drive know with an END command.
919 */
920 cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
921 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
922 bp->b_flags &= ~B_CMD;
923 BUFQ_REMOVE(&sc->sc_tab, bp);
924 biodone(bp);
925 hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
926 if (BUFQ_FIRST(&sc->sc_tab) == NULL)
927 sc->sc_active = 0;
928 else
929 mtustart(sc);
930 }
931
932 int
933 mtread(dev, uio, flags)
934 dev_t dev;
935 struct uio *uio;
936 int flags;
937 {
938 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
939
940 return(physio(mtstrategy, &sc->sc_bufstore,
941 dev, B_READ, minphys, uio));
942 }
943
944 int
945 mtwrite(dev, uio, flags)
946 dev_t dev;
947 struct uio *uio;
948 int flags;
949 {
950 struct mt_softc *sc = mt_cd.cd_devs[UNIT(dev)];
951
952 return(physio(mtstrategy, &sc->sc_bufstore,
953 dev, B_WRITE, minphys, uio));
954 }
955
956 int
957 mtioctl(dev, cmd, data, flag, p)
958 dev_t dev;
959 u_long cmd;
960 caddr_t data;
961 int flag;
962 struct proc *p;
963 {
964 struct mtop *op;
965 int cnt;
966
967 switch (cmd) {
968 case MTIOCTOP:
969 op = (struct mtop *)data;
970 switch(op->mt_op) {
971 case MTWEOF:
972 case MTFSF:
973 case MTBSR:
974 case MTBSF:
975 case MTFSR:
976 cnt = op->mt_count;
977 break;
978
979 case MTOFFL:
980 case MTREW:
981 case MTNOP:
982 cnt = 0;
983 break;
984
985 default:
986 return (EINVAL);
987 }
988 return (mtcommand(dev, op->mt_op, cnt));
989
990 case MTIOCGET:
991 break;
992
993 default:
994 return (EINVAL);
995 }
996 return (0);
997 }
998
999 /*ARGSUSED*/
1000 int
1001 mtdump(dev, blkno, va, size)
1002 dev_t dev;
1003 daddr_t blkno;
1004 caddr_t va;
1005 size_t size;
1006 {
1007 return (ENODEV);
1008 }
1009