/* $NetBSD: mscp_tape.c,v 1.44 2024/01/11 06:19:49 mrg Exp $ */
/*
* Copyright (c) 1996 Ludd, University of Lule}, Sweden.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed at Ludd, University of
* Lule}, Sweden and its contributors.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Drive status, per drive
*/
struct mt_softc {
device_t mt_dev; /* Autoconf struct */
int mt_state; /* open/closed state */
int mt_hwunit; /* Hardware unit number */
int mt_inuse; /* Locks the tape drive for others */
int mt_waswrite; /* Last operation was a write op */
int mt_serex; /* Got serious exception */
int mt_ioctlerr; /* Error after last ioctl */
};
/* This is not good, should allow more than 4 tapes/device type */
#define mtunit(dev) (minor(dev) & T_UNIT)
#define mtnorewind(dev) (dev & T_NOREWIND)
#define mthdensity(dev) (dev & T_1600BPI)
/*
* (Try to) put the drive online. This is done the first time the
* drive is opened, or if it has fallen offline.
*/
int
mt_putonline(struct mt_softc *mt)
{
struct mscp *mp;
struct mscp_softc *mi =
device_private(device_parent(mt->mt_dev));
/* ARGSUSED */
int
mtclose(dev_t dev, int flags, int fmt, struct lwp *l)
{
int unit = mtunit(dev);
struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
/*
* If we just have finished a writing, write EOT marks.
*/
if ((flags & FWRITE) && mt->mt_waswrite) {
mtcmd(mt, MTWEOF, 0, 0);
mtcmd(mt, MTWEOF, 0, 0);
mtcmd(mt, MTBSR, 1, 0);
}
if (mtnorewind(dev) == 0)
mtcmd(mt, MTREW, 0, 1);
if (mt->mt_serex)
mtcmd(mt, -1, 0, 0);
void
mtstrategy(struct buf *bp)
{
int unit;
struct mt_softc *mt;
/*
* Make sure this is a reasonable drive to use.
*/
unit = mtunit(bp->b_dev);
if ((mt = device_lookup_private(&mt_cd, unit)) == NULL) {
bp->b_error = ENXIO;
biodone(bp);
return;
}
/*
* Fill in drive addresses in a mscp packet waiting for transfer.
*/
void
mtfillin(struct buf *bp, struct mscp *mp)
{
int unit = mtunit(bp->b_dev);
struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
/*
* A drive came on line, make sure it really _is_ on line before
* trying to use it.
*/
int
mtonline(device_t usc, struct mscp *mp)
{
struct mt_softc *mt = (void *)usc;
/*
* An I/O error, may be because of a tapemark encountered.
* Check that before failing.
*/
/*ARGSUSED*/
int
mtioerror(device_t usc, struct mscp *mp, struct buf *bp)
{
struct mt_softc *mt = (void *)usc;
int st = mp->mscp_status & M_ST_MASK;
if (mp->mscp_flags & M_EF_SEREX)
mt->mt_serex = 1;
if (st == M_ST_TAPEMARK)
mt->mt_serex = 2;
else {
if (st && st < 17)
printf("%s: error %d (%s)\n", device_xname(mt->mt_dev), st,
mt_ioerrs[st-1]);
else
printf("%s: error %d\n", device_xname(mt->mt_dev), st);
bp->b_error = EROFS;
}
return (MSCP_DONE);
}
/*
* I/O controls.
*/
int
mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
int unit = mtunit(dev);
struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
struct mtop *mtop;
int error = 0;
switch (cmd) {
case MTIOCTOP:
mtop = (void *)data;
if (mtop->mt_op == MTWEOF) {
while (mtop->mt_count-- > 0)
if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
break;
} else
error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
case MTIOCGET:
((struct mtget *)data)->mt_type = MT_ISTMSCP;
/* XXX we need to fill in more fields here */
break;
/*
* No crash dump support...
*/
int
mtdump(dev_t dev, daddr_t blkno, void *va, size_t size)
{
return -1;
}
/*
* Send a command to the tape drive. Wait until the command is
* finished before returning.
* This routine must only be called when there are no data transfer
* active on this device. Can we be sure of this? Or does the ctlr
* queue up all command packets and take them in sequential order?
* It sure would be nice if my manual stated this... /ragge
*/
int
mtcmd(struct mt_softc *mt, int cmd, int count, int complete)
{
struct mscp *mp;
struct mscp_softc *mi = device_private(device_parent(mt->mt_dev));
/*
* Called from bus routines whenever a non-data transfer is finished.
*/
void
mtcmddone(device_t usc, struct mscp *mp)
{
struct mt_softc *mt = (void *)usc;
if (mp->mscp_status) {
mt->mt_ioctlerr = EIO;
printf("%s: bad status %x\n", device_xname(mt->mt_dev),
mp->mscp_status);
}
wakeup(&mt->mt_inuse);
}