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