1 /* $NetBSD: mt.c,v 1.49 2009/05/26 06:38:26 he Exp $ */
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
70 #include <sys/ioctl.h>
74 #include <sys/errno.h>
75 #include <sys/syslog.h>
77 #include <sys/kernel.h>
78 #include <sys/tprintf.h>
79 #include <sys/device.h>
82 #include <hp300/dev/hpibvar.h>
84 #include <hp300/dev/mtreg.h>
88 static const struct mtinfo
{
93 { MT7979AID
, "7979A" },
95 { MT7974AID
, "7974A" },
97 static const int nmtinfo
= sizeof(mtinfo
) / sizeof(mtinfo
[0]);
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 */
115 struct bufq_state
*sc_tab
;/* buf queue */
121 #define dlog if (mtdebug) log
123 #define dlog if (0) log
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 *);
165 mtmatch(device_t parent
, cfdata_t cf
, void *aux
)
167 struct hpibbus_attach_args
*ha
= aux
;
169 return mtident(NULL
, ha
);
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
;
180 if (mtident(sc
, ha
) == 0) {
181 aprint_error(": impossible!\n");
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
;
206 mtident(struct mt_softc
*sc
, struct hpibbus_attach_args
*ha
)
210 for (i
= 0; i
< nmtinfo
; i
++) {
211 if (ha
->ha_id
== mtinfo
[i
].hwid
) {
213 sc
->sc_type
= mtinfo
[i
].hwid
;
214 aprint_normal(": %s tape\n", mtinfo
[i
].desc
);
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.
229 mtreaddsj(struct mt_softc
*sc
, int ecmd
)
233 if (sc
->sc_flags
& MTF_STATTIMEO
)
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
;
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)
247 sc
->sc_flags
|= MTF_DSJTIMEO
;
250 sc
->sc_recvtimeo
= 0;
251 sc
->sc_statindex
= 0;
252 dlog(LOG_DEBUG
, "%s readdsj: 0x%x", device_xname(sc
->sc_dev
),
254 sc
->sc_lastecmd
= ecmd
;
255 switch (sc
->sc_lastdsj
) {
257 if (ecmd
& MTE_DSJ_FORCE
)
262 sc
->sc_lastecmd
= MTE_COMPLETE
;
267 log(LOG_ERR
, "%s readdsj: DSJ 0x%x\n", device_xname(sc
->sc_dev
),
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) {
282 sc
->sc_statindex
+= retval
;
283 sc
->sc_flags
|= MTF_STATCONT
;
285 sc
->sc_flags
|= MTF_STATTIMEO
;
288 log(LOG_ERR
, "%s readdsj: can't read status",
289 device_xname(sc
->sc_dev
));
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
);
299 (void) hpibsend(sc
->sc_hpibno
, sc
->sc_slave
,
300 MTL_ECMD
, &(sc
->sc_lastecmd
), 1);
301 return (int)sc
->sc_lastdsj
;
305 mtopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
311 sc
= device_lookup_private(&mt_cd
, UNIT(dev
));
315 if ((sc
->sc_flags
& MTF_EXISTS
) == 0)
318 dlog(LOG_DEBUG
, "%s open: flags 0x%x", device_xname(sc
->sc_dev
),
320 if (sc
->sc_flags
& MTF_OPEN
)
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)
328 if ((sc
->sc_stat1
& (SR1_BOT
| SR1_ONLINE
)) == SR1_ONLINE
)
329 (void) mtcommand(dev
, MTREW
, 0);
332 if ((error
= mtcommand(dev
, MTNOP
, 0)) != 0)
334 if (!(sc
->sc_flags
& MTF_REW
))
336 error
= kpause("mt", true, hz
, NULL
);
337 if (error
!= 0 && error
!= EWOULDBLOCK
) {
342 if ((flag
& FWRITE
) && (sc
->sc_stat1
& SR1_RO
)) {
346 if (!(sc
->sc_stat1
& SR1_ONLINE
)) {
347 uprintf("%s: not online\n", device_xname(sc
->sc_dev
));
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
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.
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
);
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
));
392 (req_den
== T_800BPI
? MTSET800BPI
: (
393 req_den
== T_1600BPI
? MTSET1600BPI
: (
394 req_den
== T_6250BPI
? MTSET6250BPI
: (
395 sc
->sc_type
== MT7980ID
398 if (mtcommand(dev
, mtset_density
, 0) == 0)
399 sc
->sc_density
= req_den
;
404 sc
->sc_flags
&= ~MTF_OPEN
;
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
);
425 mtcommand(dev_t dev
, int cmd
, int cnt
)
430 bp
= getiobuf(NULL
, true);
434 bp
->b_cflags
= BC_BUSY
;
439 if (bp
->b_error
!= 0) {
450 * Only thing to check here is for legal record lengths (writes only).
453 mtstrategy(struct buf
*bp
)
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
463 if (bp
->b_bcount
& ((1 << WRITE_BITS_IGNORED
) - 1)) {
465 "%s: write record must be multiple of %d\n",
466 device_xname(sc
->sc_dev
), 1 << WRITE_BITS_IGNORED
);
471 if (sc
->sc_stat2
& SR2_LONGREC
) {
472 switch (sc
->sc_density
) {
483 if (bp
->b_bcount
> s
) {
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 */
496 bufq_put(sc
->sc_tab
, bp
);
497 if (sc
->sc_active
== 0) {
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
))
514 spl_mtintr(void *arg
)
516 struct mt_softc
*sc
= arg
;
519 hpibppclear(sc
->sc_hpibno
);
525 spl_mtstart(void *arg
)
536 struct mt_softc
*sc
= arg
;
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
))
548 if (sc
->sc_flags
& MTF_REW
) {
549 if (!hpibpptest(sc
->sc_hpibno
, sc
->sc_slave
))
551 switch (mtreaddsj(sc
, MTE_DSJ_FORCE
|MTE_COMPLETE
|MTE_IDLE
)) {
555 if ((sc
->sc_stat1
& SR1_BOT
) ||
556 !(sc
->sc_stat1
& SR1_ONLINE
)) {
557 sc
->sc_flags
&= ~MTF_REW
;
562 * -2 means "timeout" reading DSJ, which is probably
563 * temporary. This is considered OK when doing a NOP,
566 if (sc
->sc_flags
& (MTF_DSJTIMEO
| MTF_STATTIMEO
)) {
567 callout_reset(&sc
->sc_start_ch
, hz
>> 5,
572 if (bp
->b_cmd
!= MTNOP
|| !(bp
->b_flags
& B_CMD
)) {
582 if (bp
->b_flags
& B_CMD
) {
583 if (sc
->sc_flags
& MTF_PASTEOT
) {
588 bp
->b_error
= ENOSPC
;
595 sc
->sc_flags
&= ~(MTF_PASTEOT
| MTF_ATEOT
);
601 if (sc
->sc_flags
& MTF_HITEOF
)
603 cmdbuf
[0] = MTTC_FSF
;
607 if (sc
->sc_flags
& MTF_HITBOF
)
609 cmdbuf
[0] = MTTC_BSF
;
613 sc
->sc_flags
|= MTF_REW
;
614 cmdbuf
[0] = MTTC_REWOFF
;
618 cmdbuf
[0] = MTTC_WFM
;
622 cmdbuf
[0] = MTTC_BSR
;
626 cmdbuf
[0] = MTTC_FSR
;
630 sc
->sc_flags
|= MTF_REW
;
631 cmdbuf
[0] = MTTC_REW
;
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
)) {
646 * If this fails, perform a device clear
647 * to fix any protocol problems and (most
648 * likely) get the status.
654 callout_reset(&sc
->sc_start_ch
, hz
>> 5,
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
));
670 callout_reset(&sc
->sc_intr_ch
, 4 * hz
, spl_mtintr
, sc
);
671 hpibawait(sc
->sc_hpibno
);
675 cmdbuf
[0] = MTTC_800
;
679 cmdbuf
[0] = MTTC_1600
;
683 cmdbuf
[0] = MTTC_6250
;
687 cmdbuf
[0] = MTTC_DC6250
;
691 if (sc
->sc_flags
& MTF_PASTEOT
) {
692 bp
->b_error
= ENOSPC
;
695 if (bp
->b_flags
& B_READ
) {
696 sc
->sc_flags
|= MTF_IO
;
697 cmdbuf
[0] = MTTC_READ
;
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
;
705 if (hpibsend(sc
->sc_hpibno
, sc
->sc_slave
, MTL_TCMD
, cmdbuf
, cmdcount
)
707 if (sc
->sc_flags
& MTF_REW
)
709 hpibawait(sc
->sc_hpibno
);
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
;
721 sc
->sc_flags
&= ~(MTF_HITEOF
| MTF_HITBOF
);
722 (void)bufq_get(sc
->sc_tab
);
724 hpibfree(device_parent(sc
->sc_dev
), &sc
->sc_hq
);
725 if ((bp
= bufq_peek(sc
->sc_tab
)) == NULL
)
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
739 struct mt_softc
*sc
= arg
;
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);
753 struct mt_softc
*sc
= arg
;
758 bp
= bufq_peek(sc
->sc_tab
);
760 log(LOG_ERR
, "%s intr: bp == NULL", device_xname(sc
->sc_dev
));
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)) {
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
;
792 (void) hpibawait(sc
->sc_hpibno
);
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
);
805 log(LOG_ERR
, "%s intr: can't get drive stat",
806 device_xname(sc
->sc_dev
));
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
;
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
))
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
;
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
;
874 i
= hpibrecv(sc
->sc_hpibno
, sc
->sc_slave
, MTT_BCNT
, cmdbuf
, 2);
876 log(LOG_ERR
, "%s intr: can't get xfer length\n",
877 device_xname(sc
->sc_dev
));
880 i
= (int) *((u_short
*) cmdbuf
);
881 if (i
<= bp
->b_bcount
) {
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
,
890 "%s: record (%d) larger than wanted (%d)\n",
891 device_xname(sc
->sc_dev
), i
, bp
->b_bcount
);
893 sc
->sc_flags
&= ~MTF_IO
;
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
);
906 hpibfree(device_parent(sc
->sc_dev
), &sc
->sc_hq
);
907 if (bufq_peek(sc
->sc_tab
) == NULL
)
914 mtread(dev_t dev
, struct uio
*uio
, int flags
)
917 return physio(mtstrategy
, NULL
, dev
, B_READ
, minphys
, uio
);
921 mtwrite(dev_t dev
, struct uio
*uio
, int flags
)
924 return physio(mtstrategy
, NULL
, dev
, B_WRITE
, minphys
, uio
);
928 mtioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
935 op
= (struct mtop
*)data
;
954 return mtcommand(dev
, op
->mt_op
, cnt
);