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