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