Home | History | Annotate | Line # | Download | only in mscp
mscp_tape.c revision 1.35
      1 /*	$NetBSD: mscp_tape.c,v 1.35 2009/03/14 15:36:19 dsl Exp $ */
      2 /*
      3  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      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
     20  *
     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.
     31  */
     32 
     33 
     34 /*
     35  * MSCP tape device driver
     36  */
     37 
     38 /*
     39  * TODO
     40  *	Write status handling code.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: mscp_tape.c,v 1.35 2009/03/14 15:36:19 dsl Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/device.h>
     48 #include <sys/kernel.h>
     49 #include <sys/buf.h>
     50 #include <sys/bufq.h>
     51 #include <sys/ioccom.h>
     52 #include <sys/mtio.h>
     53 #include <sys/fcntl.h>
     54 #include <sys/malloc.h>
     55 #include <sys/systm.h>
     56 #include <sys/proc.h>
     57 #include <sys/conf.h>
     58 
     59 #include <sys/bus.h>
     60 #include <sys/cpu.h>
     61 
     62 #include <dev/mscp/mscp.h>
     63 #include <dev/mscp/mscpreg.h>
     64 #include <dev/mscp/mscpvar.h>
     65 
     66 #include "locators.h"
     67 
     68 /*
     69  * Drive status, per drive
     70  */
     71 struct mt_softc {
     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 */
     79 };
     80 
     81 #define MT_OFFLINE	0
     82 #define MT_ONLINE	1
     83 
     84 int	mtmatch(struct device *, struct cfdata *, void *);
     85 void	mtattach(struct device *, struct device *, void *);
     86 void	mtdgram(struct device *, struct mscp *, struct mscp_softc *);
     87 void	mtiodone(struct device *, struct buf *);
     88 int	mtonline(struct device *, struct mscp *);
     89 int	mtgotstatus(struct device *, struct mscp *);
     90 int	mtioerror(struct device *, struct mscp *, struct buf *);
     91 void	mtfillin(struct buf *, struct mscp *);
     92 int	mtcmd(struct mt_softc *, int, int, int);
     93 void	mtcmddone(struct device *, struct mscp *);
     94 int	mt_putonline(struct mt_softc *);
     95 
     96 struct	mscp_device mt_device = {
     97 	mtdgram,
     98 	mtiodone,
     99 	mtonline,
    100 	mtgotstatus,
    101 	0,
    102 	mtioerror,
    103 	0,
    104 	mtfillin,
    105 	mtcmddone,
    106 };
    107 
    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)
    112 
    113 CFATTACH_DECL(mt, sizeof(struct mt_softc),
    114     mtmatch, mtattach, NULL, NULL);
    115 
    116 extern struct cfdriver mt_cd;
    117 
    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);
    125 
    126 const struct bdevsw mt_bdevsw = {
    127 	mtopen, mtclose, mtstrategy, mtioctl, mtdump, nosize, D_TAPE
    128 };
    129 
    130 const struct cdevsw mt_cdevsw = {
    131 	mtopen, mtclose, mtread, mtwrite, mtioctl,
    132 	nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
    133 };
    134 
    135 /*
    136  * More driver definitions, for generic MSCP code.
    137  */
    138 
    139 int
    140 mtmatch(struct device *parent, struct cfdata *cf, void *aux)
    141 {
    142 	struct	drive_attach_args *da = aux;
    143 	struct	mscp *mp = da->da_mp;
    144 
    145 	if ((da->da_typ & MSCPBUS_TAPE) == 0)
    146 		return 0;
    147 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
    148 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
    149 		return 0;
    150 	return 1;
    151 }
    152 
    153 /*
    154  * The attach routine only checks and prints drive type.
    155  */
    156 void
    157 mtattach(parent, self, aux)
    158 	struct	device *parent, *self;
    159 	void	*aux;
    160 {
    161 	struct	mt_softc *mt = device_private(self);
    162 	struct	drive_attach_args *da = aux;
    163 	struct	mscp *mp = da->da_mp;
    164 	struct	mscp_softc *mi = (void *)parent;
    165 
    166 	mt->mt_hwunit = mp->mscp_unit;
    167 	mi->mi_dp[mp->mscp_unit] = self;
    168 
    169 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
    170 }
    171 
    172 /*
    173  * (Try to) put the drive online. This is done the first time the
    174  * drive is opened, or if it has fallen offline.
    175  */
    176 int
    177 mt_putonline(struct mt_softc *mt)
    178 {
    179 	struct	mscp *mp;
    180 	struct	mscp_softc *mi =
    181 	    (struct mscp_softc *)device_parent(&mt->mt_dev);
    182 	volatile int i;
    183 
    184 	((volatile struct mt_softc *) mt)->mt_state = MT_OFFLINE;
    185 	mp = mscp_getcp(mi, MSCP_WAIT);
    186 	mp->mscp_opcode = M_OP_ONLINE;
    187 	mp->mscp_unit = mt->mt_hwunit;
    188 	mp->mscp_cmdref = (long)&mt->mt_state;
    189 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
    190 
    191 	/* Poll away */
    192 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
    193 	if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz))
    194 		return MSCP_FAILED;
    195 
    196 	if ((volatile int)mt->mt_state != MT_ONLINE)
    197 		return MSCP_FAILED;
    198 
    199 	return MSCP_DONE;
    200 }
    201 /*
    202  * Open a drive.
    203  */
    204 /*ARGSUSED*/
    205 int
    206 mtopen(dev, flag, fmt, l)
    207 	dev_t dev;
    208 	int flag, fmt;
    209 	struct	lwp *l;
    210 {
    211 	struct mt_softc *mt;
    212 	int unit;
    213 
    214 	/*
    215 	 * Make sure this is a reasonable open request.
    216 	 */
    217 	unit = mtunit(dev);
    218 	mt = device_lookup_private(&mt_cd, unit);
    219 	if (!mt)
    220 		return ENXIO;
    221 
    222 	if (mt->mt_inuse)
    223 			return EBUSY;
    224 	mt->mt_inuse = 1;
    225 
    226 	if (mt_putonline(mt) == MSCP_FAILED) {
    227 		mt->mt_inuse = 0;
    228 		return EIO;
    229 	}
    230 
    231 	return 0;
    232 }
    233 
    234 /* ARGSUSED */
    235 int
    236 mtclose(dev, flags, fmt, l)
    237 	dev_t dev;
    238 	int flags, fmt;
    239 	struct	lwp *l;
    240 {
    241 	int unit = mtunit(dev);
    242 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
    243 
    244 	/*
    245 	 * If we just have finished a writing, write EOT marks.
    246 	 */
    247 	if ((flags & FWRITE) && mt->mt_waswrite) {
    248 		mtcmd(mt, MTWEOF, 0, 0);
    249 		mtcmd(mt, MTWEOF, 0, 0);
    250 		mtcmd(mt, MTBSR, 1, 0);
    251 	}
    252 	if (mtnorewind(dev) == 0)
    253 		mtcmd(mt, MTREW, 0, 1);
    254 	if (mt->mt_serex)
    255 		mtcmd(mt, -1, 0, 0);
    256 
    257 	mt->mt_inuse = 0; /* Release the tape */
    258 	return 0;
    259 }
    260 
    261 void
    262 mtstrategy(struct buf *bp)
    263 {
    264 	int unit;
    265 	struct mt_softc *mt;
    266 
    267 	/*
    268 	 * Make sure this is a reasonable drive to use.
    269 	 */
    270 	unit = mtunit(bp->b_dev);
    271 	if ((mt = device_lookup_private(&mt_cd, unit)) == NULL) {
    272 		bp->b_error = ENXIO;
    273 		biodone(bp);
    274 		return;
    275 	}
    276 
    277 	mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1;
    278 	mscp_strategy(bp, device_parent(&mt->mt_dev));
    279 	return;
    280 }
    281 
    282 int
    283 mtread(dev_t dev, struct uio *uio, int flag)
    284 {
    285 
    286 	return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
    287 }
    288 
    289 int
    290 mtwrite(dev_t dev, struct uio *uio, int flag)
    291 {
    292 
    293 	return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
    294 }
    295 
    296 void
    297 mtiodone(struct device *usc, struct buf *bp)
    298 {
    299 
    300 	biodone(bp);
    301 }
    302 
    303 /*
    304  * Fill in drive addresses in a mscp packet waiting for transfer.
    305  */
    306 void
    307 mtfillin(struct buf *bp, struct mscp *mp)
    308 {
    309 	int unit = mtunit(bp->b_dev);
    310 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
    311 
    312 	mp->mscp_unit = mt->mt_hwunit;
    313 	if (mt->mt_serex == 2) {
    314 		mp->mscp_modifier = M_MD_CLSEX;
    315 		mt->mt_serex = 0;
    316 	} else
    317 		mp->mscp_modifier = 0;
    318 
    319 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
    320 }
    321 
    322 /*
    323  * Handle an error datagram.
    324  */
    325 void
    326 mtdgram(struct device *usc, struct mscp *mp, struct mscp_softc *mi)
    327 {
    328 	if (mscp_decodeerror(usc == NULL?"unconf mt" : device_xname(usc), mp, mi))
    329 		return;
    330 }
    331 
    332 /*
    333  * A drive came on line, make sure it really _is_ on line before
    334  * trying to use it.
    335  */
    336 int
    337 mtonline(struct device *usc, struct mscp *mp)
    338 {
    339 	struct mt_softc *mt = (void *)usc;
    340 
    341 	wakeup((void *)&mt->mt_state);
    342 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
    343 		mt->mt_state = MT_ONLINE;
    344 
    345 	return (MSCP_DONE);
    346 }
    347 
    348 /*
    349  * We got some (configured) unit's status.  Return DONE.
    350  */
    351 int
    352 mtgotstatus(struct device *usc, struct mscp *mp)
    353 {
    354 	return (MSCP_DONE);
    355 }
    356 
    357 static const char *mt_ioerrs[] = {
    358 	"invalid command",	/* 1 M_ST_INVALCMD */
    359 	"command aborted",	/* 2 M_ST_ABORTED */
    360 	"unit offline",		/* 3 M_ST_OFFLINE */
    361 	"unknown",		/* 4 M_ST_AVAILABLE */
    362 	"unknown",		/* 5 M_ST_MFMTERR */
    363 	"unit write protected", /* 6 M_ST_WRPROT */
    364 	"compare error",	/* 7 M_ST_COMPERR */
    365 	"data error",		/* 8 M_ST_DATAERR */
    366 	"host buffer access error",	/* 9 M_ST_HOSTBUFERR */
    367 	"controller error",	/* 10 M_ST_CTLRERR */
    368 	"drive error",		/* 11 M_ST_DRIVEERR */
    369 	"formatter error",	/* 12 M_ST_FORMATTERR */
    370 	"BOT encountered",	/* 13 M_ST_BOT */
    371 	"tape mark encountered",/* 14 M_ST_TAPEMARK */
    372 	"unknown",		/* 15 */
    373 	"record data truncated",/* 16 M_ST_RDTRUNC */
    374 };
    375 
    376 /*
    377  * An I/O error, may be because of a tapemark encountered.
    378  * Check that before failing.
    379  */
    380 /*ARGSUSED*/
    381 int
    382 mtioerror(struct device *usc, struct mscp *mp, struct buf *bp)
    383 {
    384 	struct mt_softc *mt = (void *)usc;
    385 	int st = mp->mscp_status & M_ST_MASK;
    386 
    387 	if (mp->mscp_flags & M_EF_SEREX)
    388 		mt->mt_serex = 1;
    389 	if (st == M_ST_TAPEMARK)
    390 		mt->mt_serex = 2;
    391 	else {
    392 		if (st && st < 17)
    393 			printf("%s: error %d (%s)\n", device_xname(&mt->mt_dev), st,
    394 			    mt_ioerrs[st-1]);
    395 		else
    396 			printf("%s: error %d\n", device_xname(&mt->mt_dev), st);
    397 		bp->b_error = EROFS;
    398 	}
    399 
    400 	return (MSCP_DONE);
    401 }
    402 
    403 /*
    404  * I/O controls.
    405  */
    406 int
    407 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    408 {
    409 	int unit = mtunit(dev);
    410 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
    411 	struct mtop *mtop;
    412 	int error = 0;
    413 
    414 	switch (cmd) {
    415 
    416 	case MTIOCTOP:
    417 		mtop = (void *)data;
    418 		if (mtop->mt_op == MTWEOF) {
    419 			while (mtop->mt_count-- > 0)
    420 				if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
    421 					break;
    422 		} else
    423 			error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
    424 
    425 	case MTIOCGET:
    426 		((struct mtget *)data)->mt_type = MT_ISTMSCP;
    427 		/* XXX we need to fill in more fields here */
    428 		break;
    429 
    430 	default:
    431 		error = ENXIO;
    432 		break;
    433 	}
    434 	return (error);
    435 }
    436 
    437 /*
    438  * No crash dump support...
    439  */
    440 int
    441 mtdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    442 {
    443 	return -1;
    444 }
    445 
    446 /*
    447  * Send a command to the tape drive. Wait until the command is
    448  * finished before returning.
    449  * This routine must only be called when there are no data transfer
    450  * active on this device. Can we be sure of this? Or does the ctlr
    451  * queue up all command packets and take them in sequential order?
    452  * It sure would be nice if my manual stated this... /ragge
    453  */
    454 int
    455 mtcmd(mt, cmd, count, complete)
    456 	struct mt_softc *mt;
    457 	int cmd, count, complete;
    458 {
    459 	struct mscp *mp;
    460 	struct mscp_softc *mi = (void *)device_parent(&mt->mt_dev);
    461 	volatile int i;
    462 
    463 	mp = mscp_getcp(mi, MSCP_WAIT);
    464 
    465 	mt->mt_ioctlerr = 0;
    466 	mp->mscp_unit = mt->mt_hwunit;
    467 	mp->mscp_cmdref = -1;
    468 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
    469 
    470 	switch (cmd) {
    471 	case MTWEOF:
    472 		mp->mscp_opcode = M_OP_WRITM;
    473 		break;
    474 
    475 	case MTBSF:
    476 		mp->mscp_modifier = M_MD_REVERSE;
    477 	case MTFSF:
    478 		mp->mscp_opcode = M_OP_POS;
    479 		mp->mscp_seq.seq_buffer = count;
    480 		break;
    481 
    482 	case MTBSR:
    483 		mp->mscp_modifier = M_MD_REVERSE;
    484 	case MTFSR:
    485 		mp->mscp_opcode = M_OP_POS;
    486 		mp->mscp_modifier |= M_MD_OBJCOUNT;
    487 		mp->mscp_seq.seq_bytecount = count;
    488 		break;
    489 
    490 	case MTREW:
    491 		mp->mscp_opcode = M_OP_POS;
    492 		mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX;
    493 		if (complete)
    494 			mp->mscp_modifier |= M_MD_IMMEDIATE;
    495 		mt->mt_serex = 0;
    496 		break;
    497 
    498 	case MTOFFL:
    499 		mp->mscp_opcode = M_OP_AVAILABLE;
    500 		mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX;
    501 		mt->mt_serex = 0;
    502 		break;
    503 
    504 	case MTNOP:
    505 		mp->mscp_opcode = M_OP_GETUNITST;
    506 		break;
    507 
    508 	case -1: /* Clear serious exception only */
    509 		mp->mscp_opcode = M_OP_POS;
    510 		mp->mscp_modifier = M_MD_CLSEX;
    511 		mt->mt_serex = 0;
    512 		break;
    513 
    514 	default:
    515 		printf("Bad ioctl %x\n", cmd);
    516 		mp->mscp_opcode = M_OP_POS;
    517 		break;
    518 	}
    519 
    520 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
    521 	tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0);
    522 	return mt->mt_ioctlerr;
    523 }
    524 
    525 /*
    526  * Called from bus routines whenever a non-data transfer is finished.
    527  */
    528 void
    529 mtcmddone(struct device *usc, struct mscp *mp)
    530 {
    531 	struct mt_softc *mt = (void *)usc;
    532 
    533 	if (mp->mscp_status) {
    534 		mt->mt_ioctlerr = EIO;
    535 		printf("%s: bad status %x\n", device_xname(&mt->mt_dev),
    536 		    mp->mscp_status);
    537 	}
    538 	wakeup(&mt->mt_inuse);
    539 }
    540