1 /* $NetBSD: mscp_tape.c,v 1.37 2009/05/12 13:16:17 cegger Exp $ */
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed at Ludd, University of
17 * Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * MSCP tape device driver
40 * Write status handling code.
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: mscp_tape.c,v 1.37 2009/05/12 13:16:17 cegger Exp $");
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/kernel.h>
51 #include <sys/ioccom.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/systm.h>
62 #include <dev/mscp/mscp.h>
63 #include <dev/mscp/mscpreg.h>
64 #include <dev/mscp/mscpvar.h>
69 * Drive status, per drive
72 struct device mt_dev
; /* Autoconf struct */
73 int mt_state
; /* open/closed state */
74 int mt_hwunit
; /* Hardware unit number */
75 int mt_inuse
; /* Locks the tape drive for others */
76 int mt_waswrite
; /* Last operation was a write op */
77 int mt_serex
; /* Got serious exception */
78 int mt_ioctlerr
; /* Error after last ioctl */
84 int mtmatch(device_t
, cfdata_t
, void *);
85 void mtattach(device_t
, device_t
, void *);
86 void mtdgram(device_t
, struct mscp
*, struct mscp_softc
*);
87 void mtiodone(device_t
, struct buf
*);
88 int mtonline(device_t
, struct mscp
*);
89 int mtgotstatus(device_t
, struct mscp
*);
90 int mtioerror(device_t
, struct mscp
*, struct buf
*);
91 void mtfillin(struct buf
*, struct mscp
*);
92 int mtcmd(struct mt_softc
*, int, int, int);
93 void mtcmddone(device_t
, struct mscp
*);
94 int mt_putonline(struct mt_softc
*);
96 struct mscp_device mt_device
= {
108 /* This is not good, should allow more than 4 tapes/device type */
109 #define mtunit(dev) (minor(dev) & T_UNIT)
110 #define mtnorewind(dev) (dev & T_NOREWIND)
111 #define mthdensity(dev) (dev & T_1600BPI)
113 CFATTACH_DECL(mt
, sizeof(struct mt_softc
),
114 mtmatch
, mtattach
, NULL
, NULL
);
116 extern struct cfdriver mt_cd
;
118 dev_type_open(mtopen
);
119 dev_type_close(mtclose
);
120 dev_type_read(mtread
);
121 dev_type_write(mtwrite
);
122 dev_type_ioctl(mtioctl
);
123 dev_type_strategy(mtstrategy
);
124 dev_type_dump(mtdump
);
126 const struct bdevsw mt_bdevsw
= {
127 mtopen
, mtclose
, mtstrategy
, mtioctl
, mtdump
, nosize
, D_TAPE
130 const struct cdevsw mt_cdevsw
= {
131 mtopen
, mtclose
, mtread
, mtwrite
, mtioctl
,
132 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_TAPE
136 * More driver definitions, for generic MSCP code.
140 mtmatch(device_t parent
, cfdata_t cf
, void *aux
)
142 struct drive_attach_args
*da
= aux
;
143 struct mscp
*mp
= da
->da_mp
;
145 if ((da
->da_typ
& MSCPBUS_TAPE
) == 0)
147 if (cf
->cf_loc
[MSCPBUSCF_DRIVE
] != MSCPBUSCF_DRIVE_DEFAULT
&&
148 cf
->cf_loc
[MSCPBUSCF_DRIVE
] != mp
->mscp_unit
)
154 * The attach routine only checks and prints drive type.
157 mtattach(device_t parent
, device_t self
, void *aux
)
159 struct mt_softc
*mt
= device_private(self
);
160 struct drive_attach_args
*da
= aux
;
161 struct mscp
*mp
= da
->da_mp
;
162 struct mscp_softc
*mi
= (void *)parent
;
164 mt
->mt_hwunit
= mp
->mscp_unit
;
165 mi
->mi_dp
[mp
->mscp_unit
] = self
;
167 disk_printtype(mp
->mscp_unit
, mp
->mscp_guse
.guse_mediaid
);
171 * (Try to) put the drive online. This is done the first time the
172 * drive is opened, or if it has fallen offline.
175 mt_putonline(struct mt_softc
*mt
)
178 struct mscp_softc
*mi
=
179 (struct mscp_softc
*)device_parent(&mt
->mt_dev
);
182 ((volatile struct mt_softc
*) mt
)->mt_state
= MT_OFFLINE
;
183 mp
= mscp_getcp(mi
, MSCP_WAIT
);
184 mp
->mscp_opcode
= M_OP_ONLINE
;
185 mp
->mscp_unit
= mt
->mt_hwunit
;
186 mp
->mscp_cmdref
= (long)&mt
->mt_state
;
187 *mp
->mscp_addr
|= MSCP_OWN
| MSCP_INT
;
190 i
= bus_space_read_2(mi
->mi_iot
, mi
->mi_iph
, 0);
191 if (tsleep(&mt
->mt_state
, PRIBIO
, "mtonline", 240 * hz
))
194 if ((volatile int)mt
->mt_state
!= MT_ONLINE
)
204 mtopen(dev_t dev
, int flag
, int fmt
, struct lwp
*l
)
210 * Make sure this is a reasonable open request.
213 mt
= device_lookup_private(&mt_cd
, unit
);
221 if (mt_putonline(mt
) == MSCP_FAILED
) {
231 mtclose(dev_t dev
, int flags
, int fmt
, struct lwp
*l
)
233 int unit
= mtunit(dev
);
234 struct mt_softc
*mt
= device_lookup_private(&mt_cd
, unit
);
237 * If we just have finished a writing, write EOT marks.
239 if ((flags
& FWRITE
) && mt
->mt_waswrite
) {
240 mtcmd(mt
, MTWEOF
, 0, 0);
241 mtcmd(mt
, MTWEOF
, 0, 0);
242 mtcmd(mt
, MTBSR
, 1, 0);
244 if (mtnorewind(dev
) == 0)
245 mtcmd(mt
, MTREW
, 0, 1);
249 mt
->mt_inuse
= 0; /* Release the tape */
254 mtstrategy(struct buf
*bp
)
260 * Make sure this is a reasonable drive to use.
262 unit
= mtunit(bp
->b_dev
);
263 if ((mt
= device_lookup_private(&mt_cd
, unit
)) == NULL
) {
269 mt
->mt_waswrite
= bp
->b_flags
& B_READ
? 0 : 1;
270 mscp_strategy(bp
, device_parent(&mt
->mt_dev
));
275 mtread(dev_t dev
, struct uio
*uio
, int flag
)
278 return (physio(mtstrategy
, NULL
, dev
, B_READ
, minphys
, uio
));
282 mtwrite(dev_t dev
, struct uio
*uio
, int flag
)
285 return (physio(mtstrategy
, NULL
, dev
, B_WRITE
, minphys
, uio
));
289 mtiodone(device_t usc
, struct buf
*bp
)
296 * Fill in drive addresses in a mscp packet waiting for transfer.
299 mtfillin(struct buf
*bp
, struct mscp
*mp
)
301 int unit
= mtunit(bp
->b_dev
);
302 struct mt_softc
*mt
= device_lookup_private(&mt_cd
, unit
);
304 mp
->mscp_unit
= mt
->mt_hwunit
;
305 if (mt
->mt_serex
== 2) {
306 mp
->mscp_modifier
= M_MD_CLSEX
;
309 mp
->mscp_modifier
= 0;
311 mp
->mscp_seq
.seq_bytecount
= bp
->b_bcount
;
315 * Handle an error datagram.
318 mtdgram(device_t usc
, struct mscp
*mp
, struct mscp_softc
*mi
)
320 if (mscp_decodeerror(usc
== NULL
?"unconf mt" : device_xname(usc
), mp
, mi
))
325 * A drive came on line, make sure it really _is_ on line before
329 mtonline(device_t usc
, struct mscp
*mp
)
331 struct mt_softc
*mt
= (void *)usc
;
333 wakeup((void *)&mt
->mt_state
);
334 if ((mp
->mscp_status
& M_ST_MASK
) == M_ST_SUCCESS
)
335 mt
->mt_state
= MT_ONLINE
;
341 * We got some (configured) unit's status. Return DONE.
344 mtgotstatus(device_t usc
, struct mscp
*mp
)
349 static const char *mt_ioerrs
[] = {
350 "invalid command", /* 1 M_ST_INVALCMD */
351 "command aborted", /* 2 M_ST_ABORTED */
352 "unit offline", /* 3 M_ST_OFFLINE */
353 "unknown", /* 4 M_ST_AVAILABLE */
354 "unknown", /* 5 M_ST_MFMTERR */
355 "unit write protected", /* 6 M_ST_WRPROT */
356 "compare error", /* 7 M_ST_COMPERR */
357 "data error", /* 8 M_ST_DATAERR */
358 "host buffer access error", /* 9 M_ST_HOSTBUFERR */
359 "controller error", /* 10 M_ST_CTLRERR */
360 "drive error", /* 11 M_ST_DRIVEERR */
361 "formatter error", /* 12 M_ST_FORMATTERR */
362 "BOT encountered", /* 13 M_ST_BOT */
363 "tape mark encountered",/* 14 M_ST_TAPEMARK */
365 "record data truncated",/* 16 M_ST_RDTRUNC */
369 * An I/O error, may be because of a tapemark encountered.
370 * Check that before failing.
374 mtioerror(device_t usc
, struct mscp
*mp
, struct buf
*bp
)
376 struct mt_softc
*mt
= (void *)usc
;
377 int st
= mp
->mscp_status
& M_ST_MASK
;
379 if (mp
->mscp_flags
& M_EF_SEREX
)
381 if (st
== M_ST_TAPEMARK
)
385 printf("%s: error %d (%s)\n", device_xname(&mt
->mt_dev
), st
,
388 printf("%s: error %d\n", device_xname(&mt
->mt_dev
), st
);
399 mtioctl(dev_t dev
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
401 int unit
= mtunit(dev
);
402 struct mt_softc
*mt
= device_lookup_private(&mt_cd
, unit
);
410 if (mtop
->mt_op
== MTWEOF
) {
411 while (mtop
->mt_count
-- > 0)
412 if ((error
= mtcmd(mt
, mtop
->mt_op
, 0, 0)))
415 error
= mtcmd(mt
, mtop
->mt_op
, mtop
->mt_count
, 0);
418 ((struct mtget
*)data
)->mt_type
= MT_ISTMSCP
;
419 /* XXX we need to fill in more fields here */
430 * No crash dump support...
433 mtdump(dev_t dev
, daddr_t blkno
, void *va
, size_t size
)
439 * Send a command to the tape drive. Wait until the command is
440 * finished before returning.
441 * This routine must only be called when there are no data transfer
442 * active on this device. Can we be sure of this? Or does the ctlr
443 * queue up all command packets and take them in sequential order?
444 * It sure would be nice if my manual stated this... /ragge
447 mtcmd(struct mt_softc
*mt
, int cmd
, int count
, int complete
)
450 struct mscp_softc
*mi
= (void *)device_parent(&mt
->mt_dev
);
453 mp
= mscp_getcp(mi
, MSCP_WAIT
);
456 mp
->mscp_unit
= mt
->mt_hwunit
;
457 mp
->mscp_cmdref
= -1;
458 *mp
->mscp_addr
|= MSCP_OWN
| MSCP_INT
;
462 mp
->mscp_opcode
= M_OP_WRITM
;
466 mp
->mscp_modifier
= M_MD_REVERSE
;
468 mp
->mscp_opcode
= M_OP_POS
;
469 mp
->mscp_seq
.seq_buffer
= count
;
473 mp
->mscp_modifier
= M_MD_REVERSE
;
475 mp
->mscp_opcode
= M_OP_POS
;
476 mp
->mscp_modifier
|= M_MD_OBJCOUNT
;
477 mp
->mscp_seq
.seq_bytecount
= count
;
481 mp
->mscp_opcode
= M_OP_POS
;
482 mp
->mscp_modifier
= M_MD_REWIND
| M_MD_CLSEX
;
484 mp
->mscp_modifier
|= M_MD_IMMEDIATE
;
489 mp
->mscp_opcode
= M_OP_AVAILABLE
;
490 mp
->mscp_modifier
= M_MD_UNLOAD
| M_MD_CLSEX
;
495 mp
->mscp_opcode
= M_OP_GETUNITST
;
498 case -1: /* Clear serious exception only */
499 mp
->mscp_opcode
= M_OP_POS
;
500 mp
->mscp_modifier
= M_MD_CLSEX
;
505 printf("Bad ioctl %x\n", cmd
);
506 mp
->mscp_opcode
= M_OP_POS
;
510 i
= bus_space_read_2(mi
->mi_iot
, mi
->mi_iph
, 0);
511 tsleep(&mt
->mt_inuse
, PRIBIO
, "mtioctl", 0);
512 return mt
->mt_ioctlerr
;
516 * Called from bus routines whenever a non-data transfer is finished.
519 mtcmddone(device_t usc
, struct mscp
*mp
)
521 struct mt_softc
*mt
= (void *)usc
;
523 if (mp
->mscp_status
) {
524 mt
->mt_ioctlerr
= EIO
;
525 printf("%s: bad status %x\n", device_xname(&mt
->mt_dev
),
528 wakeup(&mt
->mt_inuse
);