Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.52
      1 /*	$NetBSD: st.c,v 1.52 1995/08/12 21:36:52 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Charles Hannum.  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 by Charles Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) tfs.com)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     48  */
     49 
     50 /*
     51  * To do:
     52  * work out some better way of guessing what a good timeout is going
     53  * to be depending on whether we expect to retension or not.
     54  */
     55 
     56 #include <sys/types.h>
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/fcntl.h>
     60 #include <sys/errno.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/malloc.h>
     63 #include <sys/buf.h>
     64 #include <sys/proc.h>
     65 #include <sys/user.h>
     66 #include <sys/mtio.h>
     67 #include <sys/device.h>
     68 
     69 #include <scsi/scsi_all.h>
     70 #include <scsi/scsi_tape.h>
     71 #include <scsi/scsiconf.h>
     72 
     73 /* Defines for device specific stuff */
     74 #define DEF_FIXED_BSIZE  512
     75 #define	ST_RETRIES	4	/* only on non IO commands */
     76 
     77 #define STMODE(z)	( minor(z)       & 0x03)
     78 #define STDSTY(z)	((minor(z) >> 2) & 0x03)
     79 #define STUNIT(z)	((minor(z) >> 4)       )
     80 #define CTLMODE	3
     81 
     82 #define SCSI_2_MAX_DENSITY_CODE	0x17	/* maximum density code specified
     83 					 * in SCSI II spec. */
     84 /*
     85  * Define various devices that we know mis-behave in some way,
     86  * and note how they are bad, so we can correct for them
     87  */
     88 struct modes {
     89 	u_int quirks;			/* same definitions as in quirkdata */
     90 	int blksize;
     91 	u_int8_t density;
     92 };
     93 
     94 struct quirkdata {
     95 	u_int quirks;
     96 #define	ST_Q_FORCE_BLKSIZE	0x0001
     97 #define	ST_Q_SENSE_HELP		0x0002	/* must do READ for good MODE SENSE */
     98 #define	ST_Q_IGNORE_LOADS	0x0004
     99 #define	ST_Q_BLKSIZE		0x0008	/* variable-block media_blksize > 0 */
    100 	u_int page_0_size;
    101 #define	MAX_PAGE_0_SIZE	64
    102 	struct modes modes[4];
    103 };
    104 
    105 struct st_quirk_inquiry_pattern {
    106 	struct scsi_inquiry_pattern pattern;
    107 	struct quirkdata quirkdata;
    108 };
    109 
    110 struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
    111 	{T_SEQUENTIAL, T_REMOV,
    112 	 "        ", "                ", "    ", 0, 0, {
    113 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    114 		ST_Q_FORCE_BLKSIZE, 512, QIC_24,	/* minor 4-7 */
    115 		ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600,	/* minor 8-11 */
    116 		ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250	/* minor 12-15 */
    117 	}},
    118 	{T_SEQUENTIAL, T_REMOV,
    119 	 "TANDBERG", " TDC 3600       ", "",     0, 12, {
    120 		0, 0, 0,				/* minor 0-3 */
    121 		ST_Q_FORCE_BLKSIZE, 0, QIC_525,		/* minor 4-7 */
    122 		0, 0, QIC_150,				/* minor 8-11 */
    123 		0, 0, QIC_120				/* minor 12-15 */
    124 	}},
    125 	/*
    126 	 * At least -005 and -007 need this.  I'll assume they all do unless I
    127 	 * hear otherwise.  - mycroft, 31MAR1994
    128 	 */
    129 	{T_SEQUENTIAL, T_REMOV,
    130 	 "ARCHIVE ", "VIPER 2525 25462", "",     0, 0, {
    131 		ST_Q_SENSE_HELP, 0, 0,			/* minor 0-3 */
    132 		ST_Q_SENSE_HELP, 0, QIC_525,		/* minor 4-7 */
    133 		0, 0, QIC_150,				/* minor 8-11 */
    134 		0, 0, QIC_120				/* minor 12-15 */
    135 	}},
    136 	/*
    137 	 * One user reports that this works for his tape drive.  It probably
    138 	 * needs more work.  - mycroft, 09APR1994
    139 	 */
    140 	{T_SEQUENTIAL, T_REMOV,
    141 	 "SANKYO  ", "CP525           ", "",     0, 0, {
    142 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    143 		ST_Q_FORCE_BLKSIZE, 512, QIC_525,	/* minor 4-7 */
    144 		0, 0, QIC_150,				/* minor 8-11 */
    145 		0, 0, QIC_120				/* minor 12-15 */
    146 	}},
    147 	{T_SEQUENTIAL, T_REMOV,
    148 	 "ANRITSU ", "DMT780          ", "",     0, 0, {
    149 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    150 		ST_Q_FORCE_BLKSIZE, 512, QIC_525,	/* minor 4-7 */
    151 		0, 0, QIC_150,				/* minor 8-11 */
    152 		0, 0, QIC_120				/* minor 12-15 */
    153 	}},
    154 	{T_SEQUENTIAL, T_REMOV,
    155 	 "ARCHIVE ", "VIPER 150  21247", "",     0, 12, {
    156 		0, 0, 0,				/* minor 0-3 */
    157 		0, 0, QIC_150,				/* minor 4-7 */
    158 		0, 0, QIC_120,				/* minor 8-11 */
    159 		0, 0, QIC_24				/* minor 12-15 */
    160 	}},
    161 	{T_SEQUENTIAL, T_REMOV,
    162 	 "WANGTEK ", "5525ES SCSI REV7", "",     0, 0, {
    163 		0, 0, 0,				/* minor 0-3 */
    164 		ST_Q_BLKSIZE, 0, QIC_525,		/* minor 4-7 */
    165 		0, 0, QIC_150,				/* minor 8-11 */
    166 		0, 0, QIC_120				/* minor 12-15 */
    167 	}},
    168 	{T_SEQUENTIAL, T_REMOV,
    169 	 "WangDAT ", "Model 1300      ", "",     0, 0, {
    170 		0, 0, 0,				/* minor 0-3 */
    171 		ST_Q_FORCE_BLKSIZE, 512, DDS,		/* minor 4-7 */
    172 		ST_Q_FORCE_BLKSIZE, 1024, DDS,		/* minor 8-11 */
    173 		ST_Q_FORCE_BLKSIZE, 0, DDS		/* minor 12-15 */
    174 	}},
    175 #if 0
    176 	{T_SEQUENTIAL, T_REMOV,
    177 	 "EXABYTE ", "EXB-8200        ", "",     0, 12, {
    178 		0, 0, 0,				/* minor 0-3 */
    179 		0, 0, 0,				/* minor 4-7 */
    180 		0, 0, 0,				/* minor 8-11 */
    181 		0, 0, 0					/* minor 12-15 */
    182 	}},
    183 #endif
    184 };
    185 
    186 #define NOEJECT 0
    187 #define EJECT 1
    188 
    189 struct st_softc {
    190 	struct device sc_dev;
    191 /*--------------------present operating parameters, flags etc.----------------*/
    192 	int flags;		/* see below                          */
    193 	u_int quirks;		/* quirks for the open mode           */
    194 	int blksize;		/* blksize we are using                */
    195 	u_int8_t density;	/* present density                    */
    196 	u_int page_0_size;	/* size of page 0 data		      */
    197 	u_int last_dsty;	/* last density opened               */
    198 /*--------------------device/scsi parameters----------------------------------*/
    199 	struct scsi_link *sc_link;	/* our link to the adpter etc.        */
    200 /*--------------------parameters reported by the device ----------------------*/
    201 	int blkmin;		/* min blk size                       */
    202 	int blkmax;		/* max blk size                       */
    203 	struct quirkdata *quirkdata;	/* if we have a rogue entry           */
    204 /*--------------------parameters reported by the device for this media--------*/
    205 	u_long numblks;		/* nominal blocks capacity            */
    206 	int media_blksize;	/* 0 if not ST_FIXEDBLOCKS            */
    207 	u_int8_t media_density;	/* this is what it said when asked    */
    208 /*--------------------quirks for the whole drive------------------------------*/
    209 	u_int drive_quirks;	/* quirks of this drive               */
    210 /*--------------------How we should set up when opening each minor device----*/
    211 	struct modes modes[4];	/* plus more for each mode            */
    212 	u_int8_t  modeflags[4];	/* flags for the modes                */
    213 #define DENSITY_SET_BY_USER	0x01
    214 #define DENSITY_SET_BY_QUIRK	0x02
    215 #define BLKSIZE_SET_BY_USER	0x04
    216 #define BLKSIZE_SET_BY_QUIRK	0x08
    217 /*--------------------storage for sense data returned by the drive------------*/
    218 	u_char sense_data[MAX_PAGE_0_SIZE];	/*
    219 						 * additional sense data needed
    220 						 * for mode sense/select.
    221 						 */
    222 	struct buf buf_queue;		/* the queue of pending IO operations */
    223 };
    224 
    225 int stmatch __P((struct device *, void *, void *));
    226 void stattach __P((struct device *, struct device *, void *));
    227 
    228 struct cfdriver stcd = {
    229 	NULL, "st", stmatch, stattach, DV_TAPE, sizeof(struct st_softc)
    230 };
    231 
    232 int	st_space __P((struct st_softc *, int number, u_int what, int flags));
    233 int	st_rewind __P((struct st_softc *, u_int immediate, int flags));
    234 int	st_mode_sense __P((struct st_softc *, int flags));
    235 int	st_decide_mode __P((struct st_softc *, boolean first_read));
    236 int	st_read_block_limits __P((struct st_softc *, int flags));
    237 int	st_touch_tape __P((struct st_softc *));
    238 int	st_write_filemarks __P((struct st_softc *, int number, int flags));
    239 int	st_load __P((struct st_softc *, u_int type, int flags));
    240 int	st_mode_select __P((struct st_softc *, int flags));
    241 void    ststrategy();
    242 int	st_check_eod();
    243 void    ststart();
    244 void	st_unmount();
    245 int	st_mount_tape();
    246 void	st_loadquirks();
    247 void	st_identify_drive();
    248 int	st_interpret_sense();
    249 
    250 struct scsi_device st_switch = {
    251 	st_interpret_sense,
    252 	ststart,
    253 	NULL,
    254 	NULL,
    255 };
    256 
    257 #define	ST_INFO_VALID	0x0001
    258 #define	ST_BLOCK_SET	0x0002	/* block size, mode set by ioctl      */
    259 #define	ST_WRITTEN	0x0004	/* data have been written, EOD needed */
    260 #define	ST_FIXEDBLOCKS	0x0008
    261 #define	ST_AT_FILEMARK	0x0010
    262 #define	ST_EIO_PENDING	0x0020	/* we couldn't report it then (had data) */
    263 #define	ST_NEW_MOUNT	0x0040	/* still need to decide mode              */
    264 #define	ST_READONLY	0x0080	/* st_mode_sense says write protected */
    265 #define	ST_FM_WRITTEN	0x0100	/*
    266 				 * EOF file mark written  -- used with
    267 				 * ~ST_WRITTEN to indicate that multiple file
    268 				 * marks have been written
    269 				 */
    270 #define	ST_BLANK_READ	0x0200	/* BLANK CHECK encountered already */
    271 #define	ST_2FM_AT_EOD	0x0400	/* write 2 file marks at EOD */
    272 #define	ST_MOUNTED	0x0800	/* Device is presently mounted */
    273 
    274 #define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
    275 #define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
    276 			 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
    277 			 ST_2FM_AT_EOD | ST_PER_ACTION)
    278 
    279 struct scsi_inquiry_pattern st_patterns[] = {
    280 	{T_SEQUENTIAL, T_REMOV,
    281 	 "",         "",                 ""},
    282 };
    283 
    284 int
    285 stmatch(parent, match, aux)
    286 	struct device *parent;
    287 	void *match, *aux;
    288 {
    289 	struct cfdata *cf = match;
    290 	struct scsibus_attach_args *sa = aux;
    291 	int priority;
    292 
    293 	(void)scsi_inqmatch(sa->sa_inqbuf,
    294 	    (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
    295 	    sizeof(st_patterns[0]), &priority);
    296 	return (priority);
    297 }
    298 
    299 /*
    300  * The routine called by the low level scsi routine when it discovers
    301  * A device suitable for this driver
    302  */
    303 void
    304 stattach(parent, self, aux)
    305 	struct device *parent, *self;
    306 	void *aux;
    307 {
    308 	struct st_softc *st = (void *)self;
    309 	struct scsibus_attach_args *sa = aux;
    310 	struct scsi_link *sc_link = sa->sa_sc_link;
    311 
    312 	SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
    313 
    314 	/*
    315 	 * Store information needed to contact our base driver
    316 	 */
    317 	st->sc_link = sc_link;
    318 	sc_link->device = &st_switch;
    319 	sc_link->device_softc = st;
    320 	sc_link->openings = 1;
    321 
    322 	/*
    323 	 * Check if the drive is a known criminal and take
    324 	 * Any steps needed to bring it into line
    325 	 */
    326 	st_identify_drive(st, sa->sa_inqbuf);
    327 
    328 	/*
    329 	 * Use the subdriver to request information regarding
    330 	 * the drive. We cannot use interrupts yet, so the
    331 	 * request must specify this.
    332 	 */
    333 	printf(": %s", st->quirkdata ? "rogue, " : "");
    334 	if (scsi_test_unit_ready(sc_link,
    335 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) ||
    336 	    st_mode_sense(st,
    337 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE))
    338 		printf("drive empty\n");
    339 	else {
    340 		printf("density code 0x%x, ", st->media_density);
    341 		if (st->media_blksize > 0)
    342 			printf("%d-byte", st->media_blksize);
    343 		else
    344 			printf("variable");
    345 		printf(" blocks, write-%s\n",
    346 		    (st->flags & ST_READONLY) ? "protected" : "enabled");
    347 	}
    348 
    349 	/*
    350 	 * Set up the buf queue for this device
    351 	 */
    352 	st->buf_queue.b_active = 0;
    353 	st->buf_queue.b_actf = 0;
    354 	st->buf_queue.b_actb = &st->buf_queue.b_actf;
    355 }
    356 
    357 /*
    358  * Use the inquiry routine in 'scsi_base' to get drive info so we can
    359  * Further tailor our behaviour.
    360  */
    361 void
    362 st_identify_drive(st, inqbuf)
    363 	struct st_softc *st;
    364 	struct scsi_inquiry_data *inqbuf;
    365 {
    366 	struct st_quirk_inquiry_pattern *finger;
    367 	int priority;
    368 
    369 	finger = (struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf,
    370 	    (caddr_t)st_quirk_patterns,
    371 	    sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]),
    372 	    sizeof(st_quirk_patterns[0]), &priority);
    373 	if (priority != 0) {
    374 		st->quirkdata = &finger->quirkdata;
    375 		st->drive_quirks = finger->quirkdata.quirks;
    376 		st->quirks = finger->quirkdata.quirks;	/* start value */
    377 		st->page_0_size = finger->quirkdata.page_0_size;
    378 		st_loadquirks(st);
    379 	}
    380 }
    381 
    382 /*
    383  * initialise the subdevices to the default (QUIRK) state.
    384  * this will remove any setting made by the system operator or previous
    385  * operations.
    386  */
    387 void
    388 st_loadquirks(st)
    389 	struct st_softc *st;
    390 {
    391 	int i;
    392 	struct	modes *mode;
    393 	struct	modes *mode2;
    394 
    395 	mode = st->quirkdata->modes;
    396 	mode2 = st->modes;
    397 	for (i = 0; i < 4; i++) {
    398 		bzero(mode2, sizeof(struct modes));
    399 		st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
    400 		    DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
    401 		    DENSITY_SET_BY_USER);
    402 		if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
    403 			mode2->blksize = mode->blksize;
    404 			st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
    405 		}
    406 		if (mode->density) {
    407 			mode2->density = mode->density;
    408 			st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
    409 		}
    410 		mode++;
    411 		mode2++;
    412 	}
    413 }
    414 
    415 /*
    416  * open the device.
    417  */
    418 int
    419 stopen(dev, flags)
    420 	dev_t dev;
    421 	int flags;
    422 {
    423 	int unit;
    424 	u_int mode, dsty;
    425 	int error = 0;
    426 	struct st_softc *st;
    427 	struct scsi_link *sc_link;
    428 
    429 	unit = STUNIT(dev);
    430 	if (unit >= stcd.cd_ndevs)
    431 		return ENXIO;
    432 	st = stcd.cd_devs[unit];
    433 	if (!st)
    434 		return ENXIO;
    435 
    436 	mode = STMODE(dev);
    437 	dsty = STDSTY(dev);
    438 	sc_link = st->sc_link;
    439 
    440 	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    441 	    unit, stcd.cd_ndevs));
    442 
    443 	/*
    444 	 * Only allow one at a time
    445 	 */
    446 	if (sc_link->flags & SDEV_OPEN) {
    447 		printf("%s: already open\n", st->sc_dev.dv_xname);
    448 		return EBUSY;
    449 	}
    450 
    451 	/*
    452 	 * Catch any unit attention errors.
    453 	 */
    454 	if (error = scsi_test_unit_ready(sc_link,
    455 	    SCSI_IGNORE_MEDIA_CHANGE | (mode == CTLMODE ? SCSI_IGNORE_NOT_READY : 0)))
    456 		goto bad;
    457 
    458 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    459 
    460 	/*
    461 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    462 	 * then the device has been opened to set defaults
    463 	 * This mode does NOT ALLOW I/O, only ioctls
    464 	 */
    465 	if (mode == CTLMODE)
    466 		return 0;
    467 
    468 	/*
    469 	 * if it's a different mode, or if the media has been
    470 	 * invalidated, unmount the tape from the previous
    471 	 * session but continue with open processing
    472 	 */
    473 	if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
    474 		st_unmount(st, NOEJECT);
    475 
    476 	/*
    477 	 * If we are not mounted, then we should start a new
    478 	 * mount session.
    479 	 */
    480 	if (!(st->flags & ST_MOUNTED)) {
    481 		st_mount_tape(dev, flags);
    482 		st->last_dsty = dsty;
    483 	}
    484 
    485 	/*
    486 	 * Make sure that a tape opened in write-only mode will have
    487 	 * file marks written on it when closed, even if not written to.
    488 	 * This is for SUN compatibility
    489 	 */
    490 	if ((flags & O_ACCMODE) == FWRITE)
    491 		st->flags |= ST_WRITTEN;
    492 
    493 	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
    494 	return 0;
    495 
    496 bad:
    497 	st_unmount(st, NOEJECT);
    498 	sc_link->flags &= ~SDEV_OPEN;
    499 	return error;
    500 }
    501 
    502 /*
    503  * close the device.. only called if we are the LAST
    504  * occurence of an open device
    505  */
    506 int
    507 stclose(dev)
    508 	dev_t dev;
    509 {
    510 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
    511 
    512 	SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
    513 	if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
    514 		st_write_filemarks(st, 1, 0);
    515 	switch (STMODE(dev)) {
    516 	case 0:
    517 	case 3:		/* for now */
    518 		st_unmount(st, NOEJECT);
    519 		break;
    520 	case 1:
    521 		/* leave mounted unless media seems to have been removed */
    522 		if (!(st->sc_link->flags & SDEV_MEDIA_LOADED))
    523 			st_unmount(st, NOEJECT);
    524 		break;
    525 	case 2:
    526 		st_unmount(st, EJECT);
    527 		break;
    528 	}
    529 	st->sc_link->flags &= ~SDEV_OPEN;
    530 
    531 	return 0;
    532 }
    533 
    534 /*
    535  * Start a new mount session.
    536  * Copy in all the default parameters from the selected device mode.
    537  * and try guess any that seem to be defaulted.
    538  */
    539 int
    540 st_mount_tape(dev, flags)
    541 	dev_t dev;
    542 	int flags;
    543 {
    544 	int unit;
    545 	u_int mode, dsty;
    546 	struct st_softc *st;
    547 	struct scsi_link *sc_link;
    548 	int error = 0;
    549 
    550 	unit = STUNIT(dev);
    551 	mode = STMODE(dev);
    552 	dsty = STDSTY(dev);
    553 	st = stcd.cd_devs[unit];
    554 	sc_link = st->sc_link;
    555 
    556 	if (st->flags & ST_MOUNTED)
    557 		return 0;
    558 
    559 	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
    560 	st->flags |= ST_NEW_MOUNT;
    561 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    562 	/*
    563 	 * If the media is new, then make sure we give it a chance to
    564 	 * to do a 'load' instruction.  (We assume it is new.)
    565 	 */
    566 	if (error = st_load(st, LD_LOAD, 0))
    567 		return error;
    568 	/*
    569 	 * Throw another dummy instruction to catch
    570 	 * 'Unit attention' errors. Some drives appear to give
    571 	 * these after doing a Load instruction.
    572 	 * (noteably some DAT drives)
    573 	 */
    574 	scsi_test_unit_ready(sc_link, SCSI_SILENT);	/* XXX */
    575 
    576 	/*
    577 	 * Some devices can't tell you much until they have been
    578 	 * asked to look at the media. This quirk does this.
    579 	 */
    580 	if (st->quirks & ST_Q_SENSE_HELP)
    581 		if (error = st_touch_tape(st))
    582 			return error;
    583 	/*
    584 	 * Load the physical device parameters
    585 	 * loads: blkmin, blkmax
    586 	 */
    587 	if (error = st_read_block_limits(st, 0))
    588 		return error;
    589 	/*
    590 	 * Load the media dependent parameters
    591 	 * includes: media_blksize,media_density,numblks
    592 	 * As we have a tape in, it should be reflected here.
    593 	 * If not you may need the "quirk" above.
    594 	 */
    595 	if (error = st_mode_sense(st, 0))
    596 		return error;
    597 	/*
    598 	 * If we have gained a permanent density from somewhere,
    599 	 * then use it in preference to the one supplied by
    600 	 * default by the driver.
    601 	 */
    602 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    603 		st->density = st->modes[dsty].density;
    604 	else
    605 		st->density = st->media_density;
    606 	/*
    607 	 * If we have gained a permanent blocksize
    608 	 * then use it in preference to the one supplied by
    609 	 * default by the driver.
    610 	 */
    611 	st->flags &= ~ST_FIXEDBLOCKS;
    612 	if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
    613 		st->blksize = st->modes[dsty].blksize;
    614 		if (st->blksize)
    615 			st->flags |= ST_FIXEDBLOCKS;
    616 	} else {
    617 		if (error = st_decide_mode(st, FALSE))
    618 			return error;
    619 	}
    620 	if (error = st_mode_select(st, 0)) {
    621 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
    622 		return error;
    623 	}
    624 	scsi_prevent(sc_link, PR_PREVENT,
    625 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    626 	st->flags &= ~ST_NEW_MOUNT;
    627 	st->flags |= ST_MOUNTED;
    628 	sc_link->flags |= SDEV_MEDIA_LOADED;	/* move earlier? */
    629 
    630 	return 0;
    631 }
    632 
    633 /*
    634  * End the present mount session.
    635  * Rewind, and optionally eject the tape.
    636  * Reset various flags to indicate that all new
    637  * operations require another mount operation
    638  */
    639 void
    640 st_unmount(st, eject)
    641 	struct st_softc *st;
    642 	boolean eject;
    643 {
    644 	struct scsi_link *sc_link = st->sc_link;
    645 	int nmarks;
    646 
    647 	if (!(st->flags & ST_MOUNTED))
    648 		return;
    649 	SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
    650 	st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY);
    651 	st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
    652 	scsi_prevent(sc_link, PR_ALLOW,
    653 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    654 	if (eject)
    655 		st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
    656 	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
    657 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
    658 }
    659 
    660 /*
    661  * Given all we know about the device, media, mode, 'quirks' and
    662  * initial operation, make a decision as to how we should be set
    663  * to run (regarding blocking and EOD marks)
    664  */
    665 int
    666 st_decide_mode(st, first_read)
    667 	struct st_softc *st;
    668 	boolean	first_read;
    669 {
    670 #ifdef SCSIDEBUG
    671 	struct scsi_link *sc_link = st->sc_link;
    672 #endif
    673 
    674 	SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
    675 
    676 	/*
    677 	 * If the drive can only handle fixed-length blocks and only at
    678 	 * one size, perhaps we should just do that.
    679 	 */
    680 	if (st->blkmin && (st->blkmin == st->blkmax)) {
    681 		st->flags |= ST_FIXEDBLOCKS;
    682 		st->blksize = st->blkmin;
    683 		SC_DEBUG(sc_link, SDEV_DB3,
    684 		    ("blkmin == blkmax of %d\n", st->blkmin));
    685 		goto done;
    686 	}
    687 	/*
    688 	 * If the tape density mandates (or even suggests) use of fixed
    689 	 * or variable-length blocks, comply.
    690 	 */
    691 	switch (st->density) {
    692 	case HALFINCH_800:
    693 	case HALFINCH_1600:
    694 	case HALFINCH_6250:
    695 	case DDS:
    696 		st->flags &= ~ST_FIXEDBLOCKS;
    697 		st->blksize = 0;
    698 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
    699 		goto done;
    700 	case QIC_11:
    701 	case QIC_24:
    702 	case QIC_120:
    703 	case QIC_150:
    704 	case QIC_525:
    705 	case QIC_1320:
    706 		st->flags |= ST_FIXEDBLOCKS;
    707 		if (st->media_blksize > 0)
    708 			st->blksize = st->media_blksize;
    709 		else
    710 			st->blksize = DEF_FIXED_BSIZE;
    711 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
    712 		goto done;
    713 	}
    714 	/*
    715 	 * If we're about to read the tape, perhaps we should choose
    716 	 * fixed or variable-length blocks and block size according to
    717 	 * what the drive found on the tape.
    718 	 */
    719 	if (first_read &&
    720 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
    721 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
    722 	    (st->media_blksize == 1024))) {
    723 		if (st->media_blksize > 0)
    724 			st->flags |= ST_FIXEDBLOCKS;
    725 		else
    726 			st->flags &= ~ST_FIXEDBLOCKS;
    727 		st->blksize = st->media_blksize;
    728 		SC_DEBUG(sc_link, SDEV_DB3,
    729 		    ("Used media_blksize of %d\n", st->media_blksize));
    730 		goto done;
    731 	}
    732 	/*
    733 	 * We're getting no hints from any direction.  Choose variable-
    734 	 * length blocks arbitrarily.
    735 	 */
    736 	st->flags &= ~ST_FIXEDBLOCKS;
    737 	st->blksize = 0;
    738 	SC_DEBUG(sc_link, SDEV_DB3,
    739 	    ("Give up and default to variable mode\n"));
    740 
    741 done:
    742 	/*
    743 	 * Decide whether or not to write two file marks to signify end-
    744 	 * of-data.  Make the decision as a function of density.  If
    745 	 * the decision is not to use a second file mark, the SCSI BLANK
    746 	 * CHECK condition code will be recognized as end-of-data when
    747 	 * first read.
    748 	 * (I think this should be a by-product of fixed/variable..julian)
    749 	 */
    750 	switch (st->density) {
    751 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
    752 	case QIC_11:
    753 	case QIC_24:
    754 	case QIC_120:
    755 	case QIC_150:
    756 	case QIC_525:
    757 	case QIC_1320:
    758 		st->flags &= ~ST_2FM_AT_EOD;
    759 		break;
    760 	default:
    761 		st->flags |= ST_2FM_AT_EOD;
    762 	}
    763 	return 0;
    764 }
    765 
    766 /*
    767  * Actually translate the requested transfer into
    768  * one the physical driver can understand
    769  * The transfer is described by a buf and will include
    770  * only one physical transfer.
    771  */
    772 void
    773 ststrategy(bp)
    774 	struct buf *bp;
    775 {
    776 	struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)];
    777 	struct buf *dp;
    778 	int opri;
    779 
    780 	SC_DEBUG(st->sc_link, SDEV_DB1,
    781 	    ("ststrategy %d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    782 	/*
    783 	 * If it's a null transfer, return immediatly
    784 	 */
    785 	if (bp->b_bcount == 0)
    786 		goto done;
    787 	/*
    788 	 * Odd sized request on fixed drives are verboten
    789 	 */
    790 	if (st->flags & ST_FIXEDBLOCKS) {
    791 		if (bp->b_bcount % st->blksize) {
    792 			printf("%s: bad request, must be multiple of %d\n",
    793 			    st->sc_dev.dv_xname, st->blksize);
    794 			bp->b_error = EIO;
    795 			goto bad;
    796 		}
    797 	}
    798 	/*
    799 	 * as are out-of-range requests on variable drives.
    800 	 */
    801 	else if (bp->b_bcount < st->blkmin ||
    802 		 (st->blkmax && bp->b_bcount > st->blkmax)) {
    803 		printf("%s: bad request, must be between %d and %d\n",
    804 		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
    805 		bp->b_error = EIO;
    806 		goto bad;
    807 	}
    808 	opri = splbio();
    809 
    810 	/*
    811 	 * Place it in the queue of activities for this tape
    812 	 * at the end (a bit silly because we only have on user..
    813 	 * (but it could fork()))
    814 	 */
    815 	dp = &st->buf_queue;
    816 	bp->b_actf = NULL;
    817 	bp->b_actb = dp->b_actb;
    818 	*dp->b_actb = bp;
    819 	dp->b_actb = &bp->b_actf;
    820 
    821 	/*
    822 	 * Tell the device to get going on the transfer if it's
    823 	 * not doing anything, otherwise just wait for completion
    824 	 * (All a bit silly if we're only allowing 1 open but..)
    825 	 */
    826 	ststart(st);
    827 
    828 	splx(opri);
    829 	return;
    830 bad:
    831 	bp->b_flags |= B_ERROR;
    832 done:
    833 	/*
    834 	 * Correctly set the buf to indicate a completed xfer
    835 	 */
    836 	iodone(bp);
    837 	return;
    838 }
    839 
    840 /*
    841  * ststart looks to see if there is a buf waiting for the device
    842  * and that the device is not already busy. If both are true,
    843  * It dequeues the buf and creates a scsi command to perform the
    844  * transfer required. The transfer request will call scsi_done
    845  * on completion, which will in turn call this routine again
    846  * so that the next queued transfer is performed.
    847  * The bufs are queued by the strategy routine (ststrategy)
    848  *
    849  * This routine is also called after other non-queued requests
    850  * have been made of the scsi driver, to ensure that the queue
    851  * continues to be drained.
    852  * ststart() is called at splbio
    853  */
    854 void
    855 ststart(st)
    856 	struct st_softc *st;
    857 {
    858 	struct scsi_link *sc_link = st->sc_link;
    859 	register struct buf *bp, *dp;
    860 	struct scsi_rw_tape cmd;
    861 	int flags;
    862 
    863 	SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
    864 	/*
    865 	 * See if there is a buf to do and we are not already
    866 	 * doing one
    867 	 */
    868 	while (sc_link->openings > 0) {
    869 		/* if a special awaits, let it proceed first */
    870 		if (sc_link->flags & SDEV_WAITING) {
    871 			sc_link->flags &= ~SDEV_WAITING;
    872 			wakeup((caddr_t)sc_link);
    873 			return;
    874 		}
    875 
    876 		bp = st->buf_queue.b_actf;
    877 		if (!bp)
    878 			return;	/* no work to bother with */
    879 		if (dp = bp->b_actf)
    880 			dp->b_actb = bp->b_actb;
    881 		else
    882 			st->buf_queue.b_actb = bp->b_actb;
    883 		*bp->b_actb = dp;
    884 
    885 		/*
    886 		 * if the device has been unmounted byt the user
    887 		 * then throw away all requests until done
    888 		 */
    889 		if (!(st->flags & ST_MOUNTED) ||
    890 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
    891 			/* make sure that one implies the other.. */
    892 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
    893 			bp->b_flags |= B_ERROR;
    894 			bp->b_error = EIO;
    895 			biodone(bp);
    896 			continue;
    897 		}
    898 		/*
    899 		 * only FIXEDBLOCK devices have pending operations
    900 		 */
    901 		if (st->flags & ST_FIXEDBLOCKS) {
    902 			/*
    903 			 * If we are at a filemark but have not reported it yet
    904 			 * then we should report it now
    905 			 */
    906 			if (st->flags & ST_AT_FILEMARK) {
    907 				if ((bp->b_flags & B_READ) == B_WRITE) {
    908 					/*
    909 					 * Handling of ST_AT_FILEMARK in
    910 					 * st_space will fill in the right file
    911 					 * mark count.
    912 					 * Back up over filemark
    913 					 */
    914 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
    915 						bp->b_flags |= B_ERROR;
    916 						bp->b_error = EIO;
    917 						biodone(bp);
    918 						continue;
    919 					}
    920 				} else {
    921 					bp->b_resid = bp->b_bcount;
    922 					bp->b_error = 0;
    923 					bp->b_flags &= ~B_ERROR;
    924 					st->flags &= ~ST_AT_FILEMARK;
    925 					biodone(bp);
    926 					continue;	/* seek more work */
    927 				}
    928 			}
    929 			/*
    930 			 * If we are at EIO (e.g. EOM) but have not reported it
    931 			 * yet then we should report it now
    932 			 */
    933 			if (st->flags & ST_EIO_PENDING) {
    934 				bp->b_resid = bp->b_bcount;
    935 				bp->b_error = EIO;
    936 				bp->b_flags |= B_ERROR;
    937 				st->flags &= ~ST_EIO_PENDING;
    938 				biodone(bp);
    939 				continue;	/* seek more work */
    940 			}
    941 		}
    942 
    943 		/*
    944 		 *  Fill out the scsi command
    945 		 */
    946 		bzero(&cmd, sizeof(cmd));
    947 		if ((bp->b_flags & B_READ) == B_WRITE) {
    948 			cmd.opcode = WRITE;
    949 			st->flags &= ~ST_FM_WRITTEN;
    950 			st->flags |= ST_WRITTEN;
    951 			flags = SCSI_DATA_OUT;
    952 		} else {
    953 			cmd.opcode = READ;
    954 			flags = SCSI_DATA_IN;
    955 		}
    956 
    957 		/*
    958 		 * Handle "fixed-block-mode" tape drives by using the
    959 		 * block count instead of the length.
    960 		 */
    961 		if (st->flags & ST_FIXEDBLOCKS) {
    962 			cmd.byte2 |= SRW_FIXED;
    963 			lto3b(bp->b_bcount / st->blksize, cmd.len);
    964 		} else
    965 			lto3b(bp->b_bcount, cmd.len);
    966 
    967 		/*
    968 		 * go ask the adapter to do all this for us
    969 		 */
    970 		if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    971 		    sizeof(cmd), (u_char *) bp->b_data, bp->b_bcount, 0,
    972 		    100000, bp, flags | SCSI_NOSLEEP))
    973 			printf("%s: not queued\n", st->sc_dev.dv_xname);
    974 	} /* go back and see if we can cram more work in.. */
    975 }
    976 
    977 int
    978 stread(dev, uio)
    979 	dev_t dev;
    980 	struct uio *uio;
    981 {
    982 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
    983 
    984 	return (physio(ststrategy, NULL, dev, B_READ,
    985 		       st->sc_link->adapter->scsi_minphys, uio));
    986 }
    987 
    988 int
    989 stwrite(dev, uio)
    990 	dev_t dev;
    991 	struct uio *uio;
    992 {
    993 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
    994 
    995 	return (physio(ststrategy, NULL, dev, B_WRITE,
    996 		       st->sc_link->adapter->scsi_minphys, uio));
    997 }
    998 
    999 /*
   1000  * Perform special action on behalf of the user;
   1001  * knows about the internals of this device
   1002  */
   1003 int
   1004 stioctl(dev, cmd, arg, flag, p)
   1005 	dev_t dev;
   1006 	u_long cmd;
   1007 	caddr_t arg;
   1008 	int flag;
   1009 	struct proc *p;
   1010 {
   1011 	int error = 0;
   1012 	int unit;
   1013 	int number, nmarks, dsty;
   1014 	int flags;
   1015 	struct st_softc *st;
   1016 	int hold_blksize;
   1017 	u_int8_t hold_density;
   1018 	struct mtop *mt = (struct mtop *) arg;
   1019 
   1020 	/*
   1021 	 * Find the device that the user is talking about
   1022 	 */
   1023 	flags = 0;		/* give error messages, act on errors etc. */
   1024 	unit = STUNIT(dev);
   1025 	dsty = STDSTY(dev);
   1026 	st = stcd.cd_devs[unit];
   1027 	hold_blksize = st->blksize;
   1028 	hold_density = st->density;
   1029 
   1030 	switch (cmd) {
   1031 
   1032 	case MTIOCGET: {
   1033 		struct mtget *g = (struct mtget *) arg;
   1034 
   1035 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1036 		bzero(g, sizeof(struct mtget));
   1037 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1038 		g->mt_blksiz = st->blksize;
   1039 		g->mt_density = st->density;
   1040 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1041 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1042 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1043 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1044 		g->mt_mdensity[0] = st->modes[0].density;
   1045 		g->mt_mdensity[1] = st->modes[1].density;
   1046 		g->mt_mdensity[2] = st->modes[2].density;
   1047 		g->mt_mdensity[3] = st->modes[3].density;
   1048 		break;
   1049 	}
   1050 	case MTIOCTOP: {
   1051 
   1052 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1053 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
   1054 
   1055 		/* compat: in U*x it is a short */
   1056 		number = mt->mt_count;
   1057 		switch ((short) (mt->mt_op)) {
   1058 		case MTWEOF:	/* write an end-of-file record */
   1059 			error = st_write_filemarks(st, number, flags);
   1060 			break;
   1061 		case MTBSF:	/* backward space file */
   1062 			number = -number;
   1063 		case MTFSF:	/* forward space file */
   1064 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1065 			if (!error)
   1066 				error = st_space(st, number - nmarks,
   1067 				    SP_FILEMARKS, flags);
   1068 			break;
   1069 		case MTBSR:	/* backward space record */
   1070 			number = -number;
   1071 		case MTFSR:	/* forward space record */
   1072 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1073 			if (!error)
   1074 				error = st_space(st, number, SP_BLKS, flags);
   1075 			break;
   1076 		case MTREW:	/* rewind */
   1077 			error = st_rewind(st, 0, flags);
   1078 			break;
   1079 		case MTOFFL:	/* rewind and put the drive offline */
   1080 			st_unmount(st, EJECT);
   1081 			break;
   1082 		case MTNOP:	/* no operation, sets status only */
   1083 			break;
   1084 		case MTRETEN:	/* retension the tape */
   1085 			error = st_load(st, LD_RETENSION, flags);
   1086 			if (!error)
   1087 				error = st_load(st, LD_LOAD, flags);
   1088 			break;
   1089 		case MTEOM:	/* forward space to end of media */
   1090 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1091 			if (!error)
   1092 				error = st_space(st, 1, SP_EOM, flags);
   1093 			break;
   1094 		case MTCACHE:	/* enable controller cache */
   1095 		case MTNOCACHE:	/* disable controller cache */
   1096 			break;
   1097 		case MTSETBSIZ:	/* Set block size for device */
   1098 #ifdef	NOTYET
   1099 			if (!(st->flags & ST_NEW_MOUNT)) {
   1100 				uprintf("re-mount tape before changing blocksize");
   1101 				error = EINVAL;
   1102 				break;
   1103 			}
   1104 #endif
   1105 			if (number == 0) {
   1106 				st->flags &= ~ST_FIXEDBLOCKS;
   1107 			} else {
   1108 				if ((st->blkmin || st->blkmax) &&
   1109 				    (number < st->blkmin ||
   1110 				    number > st->blkmax)) {
   1111 					error = EINVAL;
   1112 					break;
   1113 				}
   1114 				st->flags |= ST_FIXEDBLOCKS;
   1115 			}
   1116 			st->blksize = number;
   1117 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1118 			goto try_new_value;
   1119 
   1120 		case MTSETDNSTY:	/* Set density for device and mode */
   1121 			if (number > SCSI_2_MAX_DENSITY_CODE)
   1122 				error = EINVAL;
   1123 			else
   1124 				st->density = number;
   1125 			goto try_new_value;
   1126 
   1127 		default:
   1128 			error = EINVAL;
   1129 		}
   1130 		break;
   1131 	}
   1132 	case MTIOCIEOT:
   1133 	case MTIOCEEOT:
   1134 		break;
   1135 	default:
   1136 		if (STMODE(dev) == CTLMODE)
   1137 			error = scsi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p);
   1138 		else
   1139 			error = ENOTTY;
   1140 		break;
   1141 	}
   1142 	return error;
   1143 /*-----------------------------*/
   1144 try_new_value:
   1145 	/*
   1146 	 * Check that the mode being asked for is aggreeable to the
   1147 	 * drive. If not, put it back the way it was.
   1148 	 */
   1149 	if (error = st_mode_select(st, 0)) {	/* put it back as it was */
   1150 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1151 		st->density = hold_density;
   1152 		st->blksize = hold_blksize;
   1153 		if (st->blksize)
   1154 			st->flags |= ST_FIXEDBLOCKS;
   1155 		else
   1156 			st->flags &= ~ST_FIXEDBLOCKS;
   1157 		return error;
   1158 	}
   1159 	/*
   1160 	 * As the drive liked it, if we are setting a new default,
   1161 	 * set it into the structures as such.
   1162 	 *
   1163 	 * The means for deciding this are not finalised yet
   1164 	 */
   1165 	if (STMODE(dev) == 0x03) {
   1166 		/* special mode */
   1167 		/* XXX */
   1168 		switch ((short) (mt->mt_op)) {
   1169 		case MTSETBSIZ:
   1170 			st->modes[dsty].blksize = st->blksize;
   1171 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1172 			break;
   1173 		case MTSETDNSTY:
   1174 			st->modes[dsty].density = st->density;
   1175 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1176 			break;
   1177 		}
   1178 	}
   1179 	return 0;
   1180 }
   1181 
   1182 /*
   1183  * Do a synchronous read.
   1184  */
   1185 int
   1186 st_read(st, buf, size, flags)
   1187 	struct st_softc *st;
   1188 	int size;
   1189 	int flags;
   1190 	char *buf;
   1191 {
   1192 	struct scsi_rw_tape cmd;
   1193 
   1194 	/*
   1195 	 * If it's a null transfer, return immediatly
   1196 	 */
   1197 	if (size == 0)
   1198 		return 0;
   1199 	bzero(&cmd, sizeof(cmd));
   1200 	cmd.opcode = READ;
   1201 	if (st->flags & ST_FIXEDBLOCKS) {
   1202 		cmd.byte2 |= SRW_FIXED;
   1203 		lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1204 		    cmd.len);
   1205 	} else
   1206 		lto3b(size, cmd.len);
   1207 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1208 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
   1209 	    flags | SCSI_DATA_IN);
   1210 }
   1211 
   1212 #ifdef	__STDC__
   1213 #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0)
   1214 #else
   1215 #define b2tol(a)	(((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
   1216 #endif
   1217 
   1218 /*
   1219  * Ask the drive what it's min and max blk sizes are.
   1220  */
   1221 int
   1222 st_read_block_limits(st, flags)
   1223 	struct st_softc *st;
   1224 	int flags;
   1225 {
   1226 	struct scsi_block_limits cmd;
   1227 	struct scsi_block_limits_data block_limits;
   1228 	struct scsi_link *sc_link = st->sc_link;
   1229 	int error;
   1230 
   1231 	/*
   1232 	 * First check if we have it all loaded
   1233 	 */
   1234 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1235 		return 0;
   1236 
   1237 	/*
   1238 	 * do a 'Read Block Limits'
   1239 	 */
   1240 	bzero(&cmd, sizeof(cmd));
   1241 	cmd.opcode = READ_BLOCK_LIMITS;
   1242 
   1243 	/*
   1244 	 * do the command, update the global values
   1245 	 */
   1246 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1247 	    sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits),
   1248 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1249 		return error;
   1250 
   1251 	st->blkmin = b2tol(block_limits.min_length);
   1252 	st->blkmax = _3btol(&block_limits.max_length_2);
   1253 
   1254 	SC_DEBUG(sc_link, SDEV_DB3,
   1255 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1256 	return 0;
   1257 }
   1258 
   1259 /*
   1260  * Get the scsi driver to send a full inquiry to the
   1261  * device and use the results to fill out the global
   1262  * parameter structure.
   1263  *
   1264  * called from:
   1265  * attach
   1266  * open
   1267  * ioctl (to reset original blksize)
   1268  */
   1269 int
   1270 st_mode_sense(st, flags)
   1271 	struct st_softc *st;
   1272 	int flags;
   1273 {
   1274 	u_int scsi_sense_len;
   1275 	int error;
   1276 	struct scsi_mode_sense cmd;
   1277 	struct scsi_sense {
   1278 		struct scsi_mode_header header;
   1279 		struct scsi_blk_desc blk_desc;
   1280 		u_char sense_data[MAX_PAGE_0_SIZE];
   1281 	} scsi_sense;
   1282 	struct scsi_link *sc_link = st->sc_link;
   1283 
   1284 	scsi_sense_len = 12 + st->page_0_size;
   1285 
   1286 	/*
   1287 	 * Set up a mode sense
   1288 	 */
   1289 	bzero(&cmd, sizeof(cmd));
   1290 	cmd.opcode = MODE_SENSE;
   1291 	cmd.length = scsi_sense_len;
   1292 
   1293 	/*
   1294 	 * do the command, but we don't need the results
   1295 	 * just print them for our interest's sake, if asked,
   1296 	 * or if we need it as a template for the mode select
   1297 	 * store it away.
   1298 	 */
   1299 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1300 	    sizeof(cmd), (u_char *) &scsi_sense, scsi_sense_len,
   1301 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1302 		return error;
   1303 
   1304 	st->numblks = _3btol(scsi_sense.blk_desc.nblocks);
   1305 	st->media_blksize = _3btol(scsi_sense.blk_desc.blklen);
   1306 	st->media_density = scsi_sense.blk_desc.density;
   1307 	if (scsi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1308 		st->flags |= ST_READONLY;
   1309 	SC_DEBUG(sc_link, SDEV_DB3,
   1310 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1311 	    st->media_density, st->media_blksize,
   1312 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1313 	SC_DEBUG(sc_link, SDEV_DB3,
   1314 	    ("%sbuffered\n",
   1315 	    scsi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1316 	if (st->page_0_size)
   1317 		bcopy(scsi_sense.sense_data, st->sense_data, st->page_0_size);
   1318 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1319 	return 0;
   1320 }
   1321 
   1322 /*
   1323  * Send a filled out parameter structure to the drive to
   1324  * set it into the desire modes etc.
   1325  */
   1326 int
   1327 st_mode_select(st, flags)
   1328 	struct st_softc *st;
   1329 	int flags;
   1330 {
   1331 	u_int scsi_select_len;
   1332 	int error;
   1333 	struct scsi_mode_select cmd;
   1334 	struct scsi_select {
   1335 		struct scsi_mode_header header;
   1336 		struct scsi_blk_desc blk_desc;
   1337 		u_char sense_data[MAX_PAGE_0_SIZE];
   1338 	} scsi_select;
   1339 	struct scsi_link *sc_link = st->sc_link;
   1340 
   1341 	scsi_select_len = 12 + st->page_0_size;
   1342 
   1343 	/*
   1344 	 * Set up for a mode select
   1345 	 */
   1346 	bzero(&cmd, sizeof(cmd));
   1347 	cmd.opcode = MODE_SELECT;
   1348 	cmd.length = scsi_select_len;
   1349 
   1350 	bzero(&scsi_select, scsi_select_len);
   1351 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1352 	scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1353 	scsi_select.blk_desc.density = st->density;
   1354 	if (st->flags & ST_FIXEDBLOCKS)
   1355 		lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1356 	if (st->page_0_size)
   1357 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1358 
   1359 	/*
   1360 	 * do the command
   1361 	 */
   1362 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1363 	    sizeof(cmd), (u_char *) &scsi_select, scsi_select_len,
   1364 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
   1365 }
   1366 
   1367 /*
   1368  * skip N blocks/filemarks/seq filemarks/eom
   1369  */
   1370 int
   1371 st_space(st, number, what, flags)
   1372 	struct st_softc *st;
   1373 	u_int what;
   1374 	int flags;
   1375 	int number;
   1376 {
   1377 	struct scsi_space cmd;
   1378 	int error;
   1379 
   1380 	switch (what) {
   1381 	case SP_BLKS:
   1382 		if (st->flags & ST_PER_ACTION) {
   1383 			if (number > 0) {
   1384 				st->flags &= ~ST_PER_ACTION;
   1385 				return EIO;
   1386 			} else if (number < 0) {
   1387 				if (st->flags & ST_AT_FILEMARK) {
   1388 					/*
   1389 					 * Handling of ST_AT_FILEMARK
   1390 					 * in st_space will fill in the
   1391 					 * right file mark count.
   1392 					 */
   1393 					error = st_space(st, 0, SP_FILEMARKS,
   1394 						flags);
   1395 					if (error)
   1396 						return error;
   1397 				}
   1398 				if (st->flags & ST_BLANK_READ) {
   1399 					st->flags &= ~ST_BLANK_READ;
   1400 					return EIO;
   1401 				}
   1402 				st->flags &= ~ST_EIO_PENDING;
   1403 			}
   1404 		}
   1405 		break;
   1406 	case SP_FILEMARKS:
   1407 		if (st->flags & ST_EIO_PENDING) {
   1408 			if (number > 0) {
   1409 				/* pretend we just discovered the error */
   1410 				st->flags &= ~ST_EIO_PENDING;
   1411 				return EIO;
   1412 			} else if (number < 0) {
   1413 				/* back away from the error */
   1414 				st->flags &= ~ST_EIO_PENDING;
   1415 			}
   1416 		}
   1417 		if (st->flags & ST_AT_FILEMARK) {
   1418 			st->flags &= ~ST_AT_FILEMARK;
   1419 			number--;
   1420 		}
   1421 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1422 			/* back away from unwritten tape */
   1423 			st->flags &= ~ST_BLANK_READ;
   1424 			number++;	/* XXX dubious */
   1425 		}
   1426 		break;
   1427 	case SP_EOM:
   1428 		if (st->flags & ST_EIO_PENDING) {
   1429 			/* pretend we just discovered the error */
   1430 			st->flags &= ~ST_EIO_PENDING;
   1431 			return EIO;
   1432 		}
   1433 		if (st->flags & ST_AT_FILEMARK)
   1434 			st->flags &= ~ST_AT_FILEMARK;
   1435 		break;
   1436 	}
   1437 	if (number == 0)
   1438 		return 0;
   1439 
   1440 	bzero(&cmd, sizeof(cmd));
   1441 	cmd.opcode = SPACE;
   1442 	cmd.byte2 = what;
   1443 	lto3b(number, cmd.number);
   1444 
   1445 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1446 	    sizeof(cmd), 0, 0, 0, 900000, NULL, flags);
   1447 }
   1448 
   1449 /*
   1450  * write N filemarks
   1451  */
   1452 int
   1453 st_write_filemarks(st, number, flags)
   1454 	struct st_softc *st;
   1455 	int flags;
   1456 	int number;
   1457 {
   1458 	struct scsi_write_filemarks cmd;
   1459 
   1460 	/*
   1461 	 * It's hard to write a negative number of file marks.
   1462 	 * Don't try.
   1463 	 */
   1464 	if (number < 0)
   1465 		return EINVAL;
   1466 	switch (number) {
   1467 	case 0:		/* really a command to sync the drive's buffers */
   1468 		break;
   1469 	case 1:
   1470 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1471 			st->flags &= ~ST_WRITTEN;
   1472 		else
   1473 			st->flags |= ST_FM_WRITTEN;
   1474 		st->flags &= ~ST_PER_ACTION;
   1475 		break;
   1476 	default:
   1477 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1478 	}
   1479 
   1480 	bzero(&cmd, sizeof(cmd));
   1481 	cmd.opcode = WRITE_FILEMARKS;
   1482 	lto3b(number, cmd.number);
   1483 
   1484 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1485 	    sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
   1486 }
   1487 
   1488 /*
   1489  * Make sure the right number of file marks is on tape if the
   1490  * tape has been written.  If the position argument is true,
   1491  * leave the tape positioned where it was originally.
   1492  *
   1493  * nmarks returns the number of marks to skip (or, if position
   1494  * true, which were skipped) to get back original position.
   1495  */
   1496 int
   1497 st_check_eod(st, position, nmarks, flags)
   1498 	struct st_softc *st;
   1499 	boolean position;
   1500 	int *nmarks;
   1501 	int flags;
   1502 {
   1503 	int error;
   1504 
   1505 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1506 	default:
   1507 		*nmarks = 0;
   1508 		return 0;
   1509 	case ST_WRITTEN:
   1510 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1511 		*nmarks = 1;
   1512 		break;
   1513 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1514 		*nmarks = 2;
   1515 	}
   1516 	error = st_write_filemarks(st, *nmarks, flags);
   1517 	if (position && !error)
   1518 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1519 	return error;
   1520 }
   1521 
   1522 /*
   1523  * load/unload/retension
   1524  */
   1525 int
   1526 st_load(st, type, flags)
   1527 	struct st_softc *st;
   1528 	u_int type;
   1529 	int flags;
   1530 {
   1531 	struct scsi_load cmd;
   1532 
   1533 	if (type != LD_LOAD) {
   1534 		int error;
   1535 		int nmarks;
   1536 
   1537 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1538 		if (error)
   1539 			return error;
   1540 	}
   1541 	if (st->quirks & ST_Q_IGNORE_LOADS)
   1542 		return 0;
   1543 
   1544 	bzero(&cmd, sizeof(cmd));
   1545 	cmd.opcode = LOAD;
   1546 	cmd.how = type;
   1547 
   1548 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1549 	    sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
   1550 }
   1551 
   1552 /*
   1553  *  Rewind the device
   1554  */
   1555 int
   1556 st_rewind(st, immediate, flags)
   1557 	struct st_softc *st;
   1558 	u_int immediate;
   1559 	int flags;
   1560 {
   1561 	struct scsi_rewind cmd;
   1562 	int error;
   1563 	int nmarks;
   1564 
   1565 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1566 	if (error)
   1567 		return error;
   1568 	st->flags &= ~ST_PER_ACTION;
   1569 
   1570 	bzero(&cmd, sizeof(cmd));
   1571 	cmd.opcode = REWIND;
   1572 	cmd.byte2 = immediate;
   1573 
   1574 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1575 	    sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
   1576 	    flags);
   1577 }
   1578 
   1579 /*
   1580  * Look at the returned sense and act on the error and detirmine
   1581  * The unix error number to pass back... (0 = report no error)
   1582  *                            (-1 = continue processing)
   1583  */
   1584 int
   1585 st_interpret_sense(xs)
   1586 	struct scsi_xfer *xs;
   1587 {
   1588 	struct scsi_link *sc_link = xs->sc_link;
   1589 	struct scsi_sense_data *sense = &xs->sense;
   1590 	struct buf *bp = xs->bp;
   1591 	struct st_softc *st = sc_link->device_softc;
   1592 	u_int8_t key;
   1593 	u_int32_t info;
   1594 
   1595 	/*
   1596 	 * Get the sense fields and work out what code
   1597 	 */
   1598 	if (sense->error_code & SSD_ERRCODE_VALID) {
   1599 		bcopy(sense->extended_info, &info, sizeof info);
   1600 		info = ntohl(info);
   1601 	} else
   1602 		info = xs->datalen;	/* bad choice if fixed blocks */
   1603 	if ((sense->error_code & SSD_ERRCODE) != 0x70)
   1604 		return -1;	/* let the generic code handle it */
   1605 	if (st->flags & ST_FIXEDBLOCKS) {
   1606 		xs->resid = info * st->blksize;
   1607 		if (sense->extended_flags & SSD_EOM) {
   1608 			st->flags |= ST_EIO_PENDING;
   1609 			if (bp)
   1610 				bp->b_resid = xs->resid;
   1611 		}
   1612 		if (sense->extended_flags & SSD_FILEMARK) {
   1613 			st->flags |= ST_AT_FILEMARK;
   1614 			if (bp)
   1615 				bp->b_resid = xs->resid;
   1616 		}
   1617 		if (sense->extended_flags & SSD_ILI) {
   1618 			st->flags |= ST_EIO_PENDING;
   1619 			if (bp)
   1620 				bp->b_resid = xs->resid;
   1621 			if (sense->error_code & SSD_ERRCODE_VALID &&
   1622 			    (xs->flags & SCSI_SILENT) == 0)
   1623 				printf("%s: block wrong size, %d blocks residual\n",
   1624 				    st->sc_dev.dv_xname, info);
   1625 
   1626 			/*
   1627 			 * This quirk code helps the drive read
   1628 			 * the first tape block, regardless of
   1629 			 * format.  That is required for these
   1630 			 * drives to return proper MODE SENSE
   1631 			 * information.
   1632 			 */
   1633 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   1634 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   1635 				st->blksize -= 512;
   1636 		}
   1637 		/*
   1638 		 * If no data was tranfered, do it immediatly
   1639 		 */
   1640 		if (xs->resid >= xs->datalen) {
   1641 			if (st->flags & ST_EIO_PENDING)
   1642 				return EIO;
   1643 			if (st->flags & ST_AT_FILEMARK) {
   1644 				if (bp)
   1645 					bp->b_resid = xs->resid;
   1646 				return 0;
   1647 			}
   1648 		}
   1649 	} else {		/* must be variable mode */
   1650 		xs->resid = xs->datalen;	/* to be sure */
   1651 		if (sense->extended_flags & SSD_EOM)
   1652 			return EIO;
   1653 		if (sense->extended_flags & SSD_FILEMARK) {
   1654 			if (bp)
   1655 				bp->b_resid = bp->b_bcount;
   1656 			return 0;
   1657 		}
   1658 		if (sense->extended_flags & SSD_ILI) {
   1659 			if (info < 0) {
   1660 				/*
   1661 				 * the record was bigger than the read
   1662 				 */
   1663 				if ((xs->flags & SCSI_SILENT) == 0)
   1664 					printf("%s: %d-byte record too big\n",
   1665 					    st->sc_dev.dv_xname,
   1666 					    xs->datalen - info);
   1667 				return EIO;
   1668 			}
   1669 			xs->resid = info;
   1670 			if (bp)
   1671 				bp->b_resid = info;
   1672 		}
   1673 	}
   1674 	key = sense->extended_flags & SSD_KEY;
   1675 
   1676 	if (key == 0x8) {
   1677 		/*
   1678 		 * This quirk code helps the drive read the
   1679 		 * first tape block, regardless of format.  That
   1680 		 * is required for these drives to return proper
   1681 		 * MODE SENSE information.
   1682 		 */
   1683 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   1684 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   1685 			/* still starting */
   1686 			st->blksize -= 512;
   1687 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   1688 			st->flags |= ST_BLANK_READ;
   1689 			xs->resid = xs->datalen;
   1690 			if (bp) {
   1691 				bp->b_resid = xs->resid;
   1692 				/* return an EOF */
   1693 			}
   1694 			return 0;
   1695 		}
   1696 	}
   1697 	return -1;		/* let the default/generic handler handle it */
   1698 }
   1699 
   1700 /*
   1701  * The quirk here is that the drive returns some value to st_mode_sense
   1702  * incorrectly until the tape has actually passed by the head.
   1703  *
   1704  * The method is to set the drive to large fixed-block state (user-specified
   1705  * density and 1024-byte blocks), then read and rewind to get it to sense the
   1706  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   1707  * work, as a last resort, try variable- length blocks.  The result will be
   1708  * the ability to do an accurate st_mode_sense.
   1709  *
   1710  * We know we can do a rewind because we just did a load, which implies rewind.
   1711  * Rewind seems preferable to space backward if we have a virgin tape.
   1712  *
   1713  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   1714  * error processing, both part of st_interpret_sense.
   1715  */
   1716 int
   1717 st_touch_tape(st)
   1718 	struct st_softc *st;
   1719 {
   1720 	char *buf;
   1721 	int readsize;
   1722 	int error;
   1723 
   1724 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   1725 	if (!buf)
   1726 		return ENOMEM;
   1727 
   1728 	if (error = st_mode_sense(st, 0))
   1729 		goto bad;
   1730 	st->blksize = 1024;
   1731 	do {
   1732 		switch (st->blksize) {
   1733 		case 512:
   1734 		case 1024:
   1735 			readsize = st->blksize;
   1736 			st->flags |= ST_FIXEDBLOCKS;
   1737 			break;
   1738 		default:
   1739 			readsize = 1;
   1740 			st->flags &= ~ST_FIXEDBLOCKS;
   1741 		}
   1742 		if (error = st_mode_select(st, 0))
   1743 			goto bad;
   1744 		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
   1745 		if (error = st_rewind(st, 0, 0)) {
   1746 bad:			free(buf, M_TEMP);
   1747 			return error;
   1748 		}
   1749 	} while (readsize != 1 && readsize > st->blksize);
   1750 
   1751 	free(buf, M_TEMP);
   1752 	return 0;
   1753 }
   1754 
   1755 int
   1756 stdump(dev, blkno, va, size)
   1757 	dev_t dev;
   1758 	daddr_t blkno;
   1759 	caddr_t va;
   1760 	size_t size;
   1761 {
   1762 
   1763 	/* Not implemented. */
   1764 	return ENXIO;
   1765 }
   1766