No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hp300 / dev / mt.c
blobc74cab2a8e47d62764a8793f17de750b718fa9e2
1 /* $NetBSD: mt.c,v 1.49 2009/05/26 06:38:26 he Exp $ */
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
33 * Copyright (c) 1992, The University of Utah and
34 * the Computer Systems Laboratory at the University of Utah (CSL).
35 * All rights reserved.
37 * Permission to use, copy, modify and distribute this software is hereby
38 * granted provided that (1) source code retains these copyright, permission,
39 * and disclaimer notices, and (2) redistributions including binaries
40 * reproduce the notices in supporting documentation, and (3) all advertising
41 * materials mentioning features or use of this software display the following
42 * acknowledgement: ``This product includes software developed by the
43 * Computer Systems Laboratory at the University of Utah.''
45 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
46 * IS" CONDITION. THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
47 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
50 * improvements that they make and grant CSL redistribution rights.
52 * Utah $Hdr: mt.c 1.8 95/09/12$
54 /* @(#)mt.c 3.9 90/07/10 mt Xinu
56 * Magnetic tape driver (7974a, 7978a/b, 7979a, 7980a, 7980xc)
57 * Original version contributed by Mt. Xinu.
58 * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
59 * Computer Science, Victoria University of Wellington
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.49 2009/05/26 06:38:26 he Exp $");
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/callout.h>
68 #include <sys/buf.h>
69 #include <sys/bufq.h>
70 #include <sys/ioctl.h>
71 #include <sys/mtio.h>
72 #include <sys/file.h>
73 #include <sys/proc.h>
74 #include <sys/errno.h>
75 #include <sys/syslog.h>
76 #include <sys/tty.h>
77 #include <sys/kernel.h>
78 #include <sys/tprintf.h>
79 #include <sys/device.h>
80 #include <sys/conf.h>
82 #include <hp300/dev/hpibvar.h>
84 #include <hp300/dev/mtreg.h>
86 #include "ioconf.h"
88 static const struct mtinfo {
89 u_short hwid;
90 const char *desc;
91 } mtinfo[] = {
92 { MT7978ID, "7978" },
93 { MT7979AID, "7979A" },
94 { MT7980ID, "7980" },
95 { MT7974AID, "7974A" },
97 static const int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
99 struct mt_softc {
100 device_t sc_dev;
101 struct callout sc_start_ch;
102 struct callout sc_intr_ch;
103 int sc_hpibno; /* logical HPIB this slave it attached to */
104 int sc_slave; /* HPIB slave address (0-6) */
105 short sc_flags; /* see below */
106 u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */
107 u_char sc_lastecmd; /* place for End Command in mtreaddsj() */
108 short sc_recvtimeo; /* count of hpibsend timeouts to prevent hang */
109 short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */
110 struct mt_stat sc_stat;/* status bytes last read from device */
111 short sc_density; /* current density of tape (mtio.h format) */
112 short sc_type; /* tape drive model (hardware IDs) */
113 struct hpibqueue sc_hq; /* HPIB device queue member */
114 tpr_t sc_ttyp;
115 struct bufq_state *sc_tab;/* buf queue */
116 int sc_active;
119 #ifdef DEBUG
120 int mtdebug = 0;
121 #define dlog if (mtdebug) log
122 #else
123 #define dlog if (0) log
124 #endif
126 #define UNIT(x) (minor(x) & 3)
128 #define B_CMD B_DEVPRIVATE /* command buf instead of data */
129 #define b_cmd b_blkno /* blkno holds cmd when B_CMD */
131 static int mtmatch(device_t, cfdata_t, void *);
132 static void mtattach(device_t, device_t, void *);
134 CFATTACH_DECL_NEW(mt, sizeof(struct mt_softc),
135 mtmatch, mtattach, NULL, NULL);
137 static dev_type_open(mtopen);
138 static dev_type_close(mtclose);
139 static dev_type_read(mtread);
140 static dev_type_write(mtwrite);
141 static dev_type_ioctl(mtioctl);
142 static dev_type_strategy(mtstrategy);
144 const struct bdevsw mt_bdevsw = {
145 mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE
148 const struct cdevsw mt_cdevsw = {
149 mtopen, mtclose, mtread, mtwrite, mtioctl,
150 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
153 static int mtident(struct mt_softc *, struct hpibbus_attach_args *);
154 static void mtustart(struct mt_softc *);
155 static int mtreaddsj(struct mt_softc *, int);
156 static int mtcommand(dev_t, int, int);
157 static void spl_mtintr(void *);
158 static void spl_mtstart(void *);
160 static void mtstart(void *);
161 static void mtgo(void *);
162 static void mtintr(void *);
164 static int
165 mtmatch(device_t parent, cfdata_t cf, void *aux)
167 struct hpibbus_attach_args *ha = aux;
169 return mtident(NULL, ha);
172 static void
173 mtattach(device_t parent, device_t self, void *aux)
175 struct mt_softc *sc = device_private(self);
176 struct hpibbus_attach_args *ha = aux;
177 int unit, hpibno, slave;
179 sc->sc_dev = self;
180 if (mtident(sc, ha) == 0) {
181 aprint_error(": impossible!\n");
182 return;
185 unit = device_unit(self);
186 hpibno = device_unit(parent);
187 slave = ha->ha_slave;
189 bufq_alloc(&sc->sc_tab, "fcfs", 0);
190 callout_init(&sc->sc_start_ch, 0);
191 callout_init(&sc->sc_intr_ch, 0);
193 sc->sc_hpibno = hpibno;
194 sc->sc_slave = slave;
195 sc->sc_flags = MTF_EXISTS;
197 /* Initialize hpib job queue entry. */
198 sc->sc_hq.hq_softc = sc;
199 sc->sc_hq.hq_slave = sc->sc_slave;
200 sc->sc_hq.hq_start = mtstart;
201 sc->sc_hq.hq_go = mtgo;
202 sc->sc_hq.hq_intr = mtintr;
205 static int
206 mtident(struct mt_softc *sc, struct hpibbus_attach_args *ha)
208 int i;
210 for (i = 0; i < nmtinfo; i++) {
211 if (ha->ha_id == mtinfo[i].hwid) {
212 if (sc != NULL) {
213 sc->sc_type = mtinfo[i].hwid;
214 aprint_normal(": %s tape\n", mtinfo[i].desc);
216 return 1;
219 return 0;
223 * Perform a read of "Device Status Jump" register and update the
224 * status if necessary. If status is read, the given "ecmd" is also
225 * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure
226 * and -2 on "temporary" failure.
228 static int
229 mtreaddsj(struct mt_softc *sc, int ecmd)
231 int retval;
233 if (sc->sc_flags & MTF_STATTIMEO)
234 goto getstats;
235 retval = hpibrecv(sc->sc_hpibno,
236 (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
237 MTT_DSJ, &(sc->sc_lastdsj), 1);
238 sc->sc_flags &= ~MTF_DSJTIMEO;
239 if (retval != 1) {
240 dlog(LOG_DEBUG, "%s can't hpibrecv DSJ",
241 device_xname(sc->sc_dev));
242 if (sc->sc_recvtimeo == 0)
243 sc->sc_recvtimeo = hz;
244 if (--sc->sc_recvtimeo == 0)
245 return -1;
246 if (retval == 0)
247 sc->sc_flags |= MTF_DSJTIMEO;
248 return -2;
250 sc->sc_recvtimeo = 0;
251 sc->sc_statindex = 0;
252 dlog(LOG_DEBUG, "%s readdsj: 0x%x", device_xname(sc->sc_dev),
253 sc->sc_lastdsj);
254 sc->sc_lastecmd = ecmd;
255 switch (sc->sc_lastdsj) {
256 case 0:
257 if (ecmd & MTE_DSJ_FORCE)
258 break;
259 return 0;
261 case 2:
262 sc->sc_lastecmd = MTE_COMPLETE;
263 case 1:
264 break;
266 default:
267 log(LOG_ERR, "%s readdsj: DSJ 0x%x\n", device_xname(sc->sc_dev),
268 sc->sc_lastdsj);
269 return -1;
271 getstats:
272 retval = hpibrecv(sc->sc_hpibno,
273 (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave,
274 MTT_STAT, ((char *)&(sc->sc_stat)) + sc->sc_statindex,
275 sizeof(sc->sc_stat) - sc->sc_statindex);
276 sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
277 if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
278 if (sc->sc_recvtimeo == 0)
279 sc->sc_recvtimeo = hz;
280 if (--sc->sc_recvtimeo != 0) {
281 if (retval >= 0) {
282 sc->sc_statindex += retval;
283 sc->sc_flags |= MTF_STATCONT;
285 sc->sc_flags |= MTF_STATTIMEO;
286 return -2;
288 log(LOG_ERR, "%s readdsj: can't read status",
289 device_xname(sc->sc_dev));
290 return -1;
292 sc->sc_recvtimeo = 0;
293 sc->sc_statindex = 0;
294 dlog(LOG_DEBUG, "%s readdsj: status is %x %x %x %x %x %x",
295 device_xname(sc->sc_dev),
296 sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
297 sc->sc_stat4, sc->sc_stat5, sc->sc_stat6);
298 if (sc->sc_lastecmd)
299 (void) hpibsend(sc->sc_hpibno, sc->sc_slave,
300 MTL_ECMD, &(sc->sc_lastecmd), 1);
301 return (int)sc->sc_lastdsj;
304 static int
305 mtopen(dev_t dev, int flag, int mode, struct lwp *l)
307 struct mt_softc *sc;
308 int req_den;
309 int error;
311 sc = device_lookup_private(&mt_cd, UNIT(dev));
312 if (sc == NULL)
313 return ENXIO;
315 if ((sc->sc_flags & MTF_EXISTS) == 0)
316 return ENXIO;
318 dlog(LOG_DEBUG, "%s open: flags 0x%x", device_xname(sc->sc_dev),
319 sc->sc_flags);
320 if (sc->sc_flags & MTF_OPEN)
321 return EBUSY;
322 sc->sc_flags |= MTF_OPEN;
323 sc->sc_ttyp = tprintf_open(l->l_proc);
324 if ((sc->sc_flags & MTF_ALIVE) == 0) {
325 error = mtcommand(dev, MTRESET, 0);
326 if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
327 goto errout;
328 if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
329 (void) mtcommand(dev, MTREW, 0);
331 for (;;) {
332 if ((error = mtcommand(dev, MTNOP, 0)) != 0)
333 goto errout;
334 if (!(sc->sc_flags & MTF_REW))
335 break;
336 error = kpause("mt", true, hz, NULL);
337 if (error != 0 && error != EWOULDBLOCK) {
338 error = EINTR;
339 goto errout;
342 if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
343 error = EROFS;
344 goto errout;
346 if (!(sc->sc_stat1 & SR1_ONLINE)) {
347 uprintf("%s: not online\n", device_xname(sc->sc_dev));
348 error = EIO;
349 goto errout;
352 * Select density:
353 * - find out what density the drive is set to
354 * (i.e. the density of the current tape)
355 * - if we are going to write
356 * - if we're not at the beginning of the tape
357 * - complain if we want to change densities
358 * - otherwise, select the mtcommand to set the density
360 * If the drive doesn't support it then don't change the recorded
361 * density.
363 * The original MOREbsd code had these additional conditions
364 * for the mid-tape change
366 * req_den != T_BADBPI &&
367 * sc->sc_density != T_6250BPI
369 * which suggests that it would be possible to write multiple
370 * densities if req_den == T_BAD_BPI or the current tape
371 * density was 6250. Testing of our 7980 suggests that the
372 * device cannot change densities mid-tape.
374 * ajv@comp.vuw.ac.nz
376 sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
377 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
378 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
379 req_den = (dev & T_DENSEL);
381 if (flag & FWRITE) {
382 if (!(sc->sc_stat1 & SR1_BOT)) {
383 if (sc->sc_density != req_den) {
384 uprintf("%s: can't change density mid-tape\n",
385 device_xname(sc->sc_dev));
386 error = EIO;
387 goto errout;
390 else {
391 int mtset_density =
392 (req_den == T_800BPI ? MTSET800BPI : (
393 req_den == T_1600BPI ? MTSET1600BPI : (
394 req_den == T_6250BPI ? MTSET6250BPI : (
395 sc->sc_type == MT7980ID
396 ? MTSET6250DC
397 : MTSET6250BPI))));
398 if (mtcommand(dev, mtset_density, 0) == 0)
399 sc->sc_density = req_den;
402 return 0;
403 errout:
404 sc->sc_flags &= ~MTF_OPEN;
405 return error;
408 static int
409 mtclose(dev_t dev, int flag, int fmt, struct lwp *l)
411 struct mt_softc *sc = device_lookup_private(&mt_cd,UNIT(dev));
413 if (sc->sc_flags & MTF_WRT) {
414 (void) mtcommand(dev, MTWEOF, 2);
415 (void) mtcommand(dev, MTBSF, 0);
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;
424 static int
425 mtcommand(dev_t dev, int cmd, int cnt)
427 int error = 0;
428 buf_t *bp;
430 bp = getiobuf(NULL, true);
431 bp->b_cmd = cmd;
432 bp->b_dev = dev;
433 do {
434 bp->b_cflags = BC_BUSY;
435 bp->b_flags = B_CMD;
436 bp->b_oflags = 0;
437 mtstrategy(bp);
438 biowait(bp);
439 if (bp->b_error != 0) {
440 error = bp->b_error;
441 break;
443 } while (--cnt > 0);
444 putiobuf(bp);
446 return error;
450 * Only thing to check here is for legal record lengths (writes only).
452 static void
453 mtstrategy(struct buf *bp)
455 struct mt_softc *sc;
456 int s;
458 sc = device_lookup_private(&mt_cd,UNIT(bp->b_dev));
459 dlog(LOG_DEBUG, "%s strategy", device_xname(sc->sc_dev));
460 if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
461 #define WRITE_BITS_IGNORED 8
462 #if 0
463 if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
464 tprintf(sc->sc_ttyp,
465 "%s: write record must be multiple of %d\n",
466 device_xname(sc->sc_dev), 1 << WRITE_BITS_IGNORED);
467 goto error;
469 #endif
470 s = 16 * 1024;
471 if (sc->sc_stat2 & SR2_LONGREC) {
472 switch (sc->sc_density) {
473 case T_1600BPI:
474 s = 32 * 1024;
475 break;
477 case T_6250BPI:
478 case T_BADBPI:
479 s = 60 * 1024;
480 break;
483 if (bp->b_bcount > s) {
484 tprintf(sc->sc_ttyp,
485 "%s: write record (%d) too big: limit (%d)\n",
486 device_xname(sc->sc_dev), bp->b_bcount, s);
487 #if 0 /* XXX see above */
488 error:
489 #endif
490 bp->b_error = EIO;
491 biodone(bp);
492 return;
495 s = splbio();
496 bufq_put(sc->sc_tab, bp);
497 if (sc->sc_active == 0) {
498 sc->sc_active = 1;
499 mtustart(sc);
501 splx(s);
504 static void
505 mtustart(struct mt_softc *sc)
508 dlog(LOG_DEBUG, "%s ustart", device_xname(sc->sc_dev));
509 if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
510 mtstart(sc);
513 static void
514 spl_mtintr(void *arg)
516 struct mt_softc *sc = arg;
517 int s = splbio();
519 hpibppclear(sc->sc_hpibno);
520 mtintr(sc);
521 splx(s);
524 static void
525 spl_mtstart(void *arg)
527 int s = splbio();
529 mtstart(arg);
530 splx(s);
533 static void
534 mtstart(void *arg)
536 struct mt_softc *sc = arg;
537 struct buf *bp;
538 short cmdcount = 1;
539 u_char cmdbuf[2];
541 dlog(LOG_DEBUG, "%s start", device_xname(sc->sc_dev));
542 sc->sc_flags &= ~MTF_WRT;
543 bp = bufq_peek(sc->sc_tab);
544 if ((sc->sc_flags & MTF_ALIVE) == 0 &&
545 ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
546 goto fatalerror;
548 if (sc->sc_flags & MTF_REW) {
549 if (!hpibpptest(sc->sc_hpibno, sc->sc_slave))
550 goto stillrew;
551 switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
552 case 0:
553 case 1:
554 stillrew:
555 if ((sc->sc_stat1 & SR1_BOT) ||
556 !(sc->sc_stat1 & SR1_ONLINE)) {
557 sc->sc_flags &= ~MTF_REW;
558 break;
560 case -2:
562 * -2 means "timeout" reading DSJ, which is probably
563 * temporary. This is considered OK when doing a NOP,
564 * but not otherwise.
566 if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
567 callout_reset(&sc->sc_start_ch, hz >> 5,
568 spl_mtstart, sc);
569 return;
571 case 2:
572 if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
573 bp->b_error = EBUSY;
574 goto done;
576 goto done;
578 default:
579 goto fatalerror;
582 if (bp->b_flags & B_CMD) {
583 if (sc->sc_flags & MTF_PASTEOT) {
584 switch(bp->b_cmd) {
585 case MTFSF:
586 case MTWEOF:
587 case MTFSR:
588 bp->b_error = ENOSPC;
589 goto done;
591 case MTBSF:
592 case MTOFFL:
593 case MTBSR:
594 case MTREW:
595 sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
596 break;
599 switch(bp->b_cmd) {
600 case MTFSF:
601 if (sc->sc_flags & MTF_HITEOF)
602 goto done;
603 cmdbuf[0] = MTTC_FSF;
604 break;
606 case MTBSF:
607 if (sc->sc_flags & MTF_HITBOF)
608 goto done;
609 cmdbuf[0] = MTTC_BSF;
610 break;
612 case MTOFFL:
613 sc->sc_flags |= MTF_REW;
614 cmdbuf[0] = MTTC_REWOFF;
615 break;
617 case MTWEOF:
618 cmdbuf[0] = MTTC_WFM;
619 break;
621 case MTBSR:
622 cmdbuf[0] = MTTC_BSR;
623 break;
625 case MTFSR:
626 cmdbuf[0] = MTTC_FSR;
627 break;
629 case MTREW:
630 sc->sc_flags |= MTF_REW;
631 cmdbuf[0] = MTTC_REW;
632 break;
634 case MTNOP:
636 * NOP is supposed to set status bits.
637 * Force readdsj to do it.
639 switch (mtreaddsj(sc,
640 MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
641 default:
642 goto done;
644 case -1:
646 * If this fails, perform a device clear
647 * to fix any protocol problems and (most
648 * likely) get the status.
650 bp->b_cmd = MTRESET;
651 break;
653 case -2:
654 callout_reset(&sc->sc_start_ch, hz >> 5,
655 spl_mtstart, sc);
656 return;
659 case MTRESET:
661 * 1) selected device clear (send with "-2" secondary)
662 * 2) set timeout, then wait for "service request"
663 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
665 if (hpibsend(sc->sc_hpibno, sc->sc_slave, -2, NULL, 0)){
666 log(LOG_ERR, "%s can't reset",
667 device_xname(sc->sc_dev));
668 goto fatalerror;
670 callout_reset(&sc->sc_intr_ch, 4 * hz, spl_mtintr, sc);
671 hpibawait(sc->sc_hpibno);
672 return;
674 case MTSET800BPI:
675 cmdbuf[0] = MTTC_800;
676 break;
678 case MTSET1600BPI:
679 cmdbuf[0] = MTTC_1600;
680 break;
682 case MTSET6250BPI:
683 cmdbuf[0] = MTTC_6250;
684 break;
686 case MTSET6250DC:
687 cmdbuf[0] = MTTC_DC6250;
688 break;
690 } else {
691 if (sc->sc_flags & MTF_PASTEOT) {
692 bp->b_error = ENOSPC;
693 goto done;
695 if (bp->b_flags & B_READ) {
696 sc->sc_flags |= MTF_IO;
697 cmdbuf[0] = MTTC_READ;
698 } else {
699 sc->sc_flags |= MTF_WRT | MTF_IO;
700 cmdbuf[0] = MTTC_WRITE;
701 cmdbuf[1] = (bp->b_bcount + ((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
702 cmdcount = 2;
705 if (hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
706 == cmdcount) {
707 if (sc->sc_flags & MTF_REW)
708 goto done;
709 hpibawait(sc->sc_hpibno);
710 return;
712 fatalerror:
714 * If anything fails, the drive is probably hosed, so mark it not
715 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
716 * if, last we heard, it was REWinding, remember that).
718 sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
719 bp->b_error = EIO;
720 done:
721 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
722 (void)bufq_get(sc->sc_tab);
723 biodone(bp);
724 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
725 if ((bp = bufq_peek(sc->sc_tab)) == NULL)
726 sc->sc_active = 0;
727 else
728 mtustart(sc);
732 * The Utah code had a bug which meant that the driver was unable to read.
733 * "rw" was initialized to bp->b_flags & B_READ before "bp" was initialized.
734 * -- ajv@comp.vuw.ac.nz
736 static void
737 mtgo(void *arg)
739 struct mt_softc *sc = arg;
740 struct buf *bp;
741 int rw;
743 dlog(LOG_DEBUG, "%s go", device_xname(sc->sc_dev));
744 bp = bufq_peek(sc->sc_tab);
745 rw = bp->b_flags & B_READ;
746 hpibgo(sc->sc_hpibno, sc->sc_slave, rw ? MTT_READ : MTL_WRITE,
747 bp->b_data, bp->b_bcount, rw, rw != 0);
750 static void
751 mtintr(void *arg)
753 struct mt_softc *sc = arg;
754 struct buf *bp;
755 int i;
756 u_char cmdbuf[4];
758 bp = bufq_peek(sc->sc_tab);
759 if (bp == NULL) {
760 log(LOG_ERR, "%s intr: bp == NULL", device_xname(sc->sc_dev));
761 return;
764 dlog(LOG_DEBUG, "%s intr", device_xname(sc->sc_dev));
767 * Some operation completed. Read status bytes and report errors.
768 * Clear EOF flags here `cause they're set once on specific conditions
769 * below when a command succeeds.
770 * A DSJ of 2 always means keep waiting. If the command was READ
771 * (and we're in data DMA phase) stop data transfer first.
773 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
774 if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
775 !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
776 cmdbuf[0] = MTE_STOP;
777 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD,cmdbuf,1);
779 switch (mtreaddsj(sc, 0)) {
780 case 0:
781 break;
783 case 1:
785 * If we're in the middle of a READ/WRITE and have yet to
786 * start the data transfer, a DSJ of one should terminate it.
788 sc->sc_flags &= ~MTF_IO;
789 break;
791 case 2:
792 (void) hpibawait(sc->sc_hpibno);
793 return;
795 case -2:
797 * -2 means that the drive failed to respond quickly enough
798 * to the request for DSJ. It's probably just "busy" figuring
799 * it out and will know in a little bit...
801 callout_reset(&sc->sc_intr_ch, hz >> 5, spl_mtintr, sc);
802 return;
804 default:
805 log(LOG_ERR, "%s intr: can't get drive stat",
806 device_xname(sc->sc_dev));
807 goto error;
809 if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
810 i = sc->sc_stat4 & SR4_ERCLMASK;
811 log(LOG_ERR, "%s: %s error, retry %d, SR2/3 %x/%x, code %d",
812 device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
813 (i == SR4_PROTOCOL ? "protocol" :
814 (i == SR4_SELFTEST ? "selftest" : "unknown")),
815 sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
816 sc->sc_stat3, sc->sc_stat5);
818 if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
819 callout_stop(&sc->sc_intr_ch);
820 if (sc->sc_stat3 & SR3_POWERUP)
821 sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
822 goto error;
825 * Report and clear any soft errors.
827 if (sc->sc_stat1 & SR1_SOFTERR) {
828 log(LOG_WARNING, "%s: soft error, retry %d\n",
829 device_xname(sc->sc_dev), sc->sc_stat4 & SR4_RETRYMASK);
830 sc->sc_stat1 &= ~SR1_SOFTERR;
833 * We've initiated a read or write, but haven't actually started to
834 * DMA the data yet. At this point, the drive's ready.
836 if (sc->sc_flags & MTF_IO) {
837 sc->sc_flags &= ~MTF_IO;
838 if (hpibustart(sc->sc_hpibno))
839 mtgo(sc);
840 return;
843 * Check for End Of Tape - we're allowed to hit EOT and then write (or
844 * read) one more record. If we get here and have not already hit EOT,
845 * return ENOSPC to inform the process that it's hit it. If we get
846 * here and HAVE already hit EOT, don't allow any more operations that
847 * move the tape forward.
849 if (sc->sc_stat1 & SR1_EOT) {
850 if (sc->sc_flags & MTF_ATEOT)
851 sc->sc_flags |= MTF_PASTEOT;
852 else {
853 bp->b_error = ENOSPC;
854 sc->sc_flags |= MTF_ATEOT;
858 * If a motion command was being executed, check for Tape Marks.
859 * If we were doing data, make sure we got the right amount, and
860 * check for hitting tape marks on reads.
862 if (bp->b_flags & B_CMD) {
863 if (sc->sc_stat1 & SR1_EOF) {
864 if (bp->b_cmd == MTFSR)
865 sc->sc_flags |= MTF_HITEOF;
866 if (bp->b_cmd == MTBSR)
867 sc->sc_flags |= MTF_HITBOF;
869 if (bp->b_cmd == MTRESET) {
870 callout_stop(&sc->sc_intr_ch);
871 sc->sc_flags |= MTF_ALIVE;
873 } else {
874 i = hpibrecv(sc->sc_hpibno, sc->sc_slave, MTT_BCNT, cmdbuf, 2);
875 if (i != 2) {
876 log(LOG_ERR, "%s intr: can't get xfer length\n",
877 device_xname(sc->sc_dev));
878 goto error;
880 i = (int) *((u_short *) cmdbuf);
881 if (i <= bp->b_bcount) {
882 if (i == 0)
883 sc->sc_flags |= MTF_HITEOF;
884 bp->b_resid = bp->b_bcount - i;
885 dlog(LOG_DEBUG, "%s intr: bcount %d, resid %d",
886 device_xname(sc->sc_dev), bp->b_bcount,
887 bp->b_resid);
888 } else {
889 tprintf(sc->sc_ttyp,
890 "%s: record (%d) larger than wanted (%d)\n",
891 device_xname(sc->sc_dev), i, bp->b_bcount);
892 error:
893 sc->sc_flags &= ~MTF_IO;
894 bp->b_error = EIO;
898 * The operation is completely done.
899 * Let the drive know with an END command.
901 cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
902 (void) hpibsend(sc->sc_hpibno, sc->sc_slave, MTL_ECMD, cmdbuf, 1);
903 bp->b_flags &= ~B_CMD;
904 (void)bufq_get(sc->sc_tab);
905 biodone(bp);
906 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
907 if (bufq_peek(sc->sc_tab) == NULL)
908 sc->sc_active = 0;
909 else
910 mtustart(sc);
913 static int
914 mtread(dev_t dev, struct uio *uio, int flags)
917 return physio(mtstrategy, NULL, dev, B_READ, minphys, uio);
920 static int
921 mtwrite(dev_t dev, struct uio *uio, int flags)
924 return physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio);
927 static int
928 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
930 struct mtop *op;
931 int cnt;
933 switch (cmd) {
934 case MTIOCTOP:
935 op = (struct mtop *)data;
936 switch(op->mt_op) {
937 case MTWEOF:
938 case MTFSF:
939 case MTBSR:
940 case MTBSF:
941 case MTFSR:
942 cnt = op->mt_count;
943 break;
945 case MTOFFL:
946 case MTREW:
947 case MTNOP:
948 cnt = 0;
949 break;
951 default:
952 return EINVAL;
954 return mtcommand(dev, op->mt_op, cnt);
956 case MTIOCGET:
957 break;
959 default:
960 return EINVAL;
962 return 0;