Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.29
      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.29 1994/04/24 05:40:05 mycroft 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 			goto badnews;
    990 		}
    991 		/*
    992 		 * only FIXEDBLOCK devices have pending operations
    993 		 */
    994 		if (st->flags & ST_FIXEDBLOCKS) {
    995 			/*
    996 			 * If we are at a filemark but have not reported it yet
    997 			 * then we should report it now
    998 			 */
    999 			if (st->flags & ST_AT_FILEMARK) {
   1000 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1001 					/*
   1002 					 * Handling of ST_AT_FILEMARK in
   1003 					 * st_space will fill in the right file
   1004 					 * mark count.
   1005 					 * Back up over filemark
   1006 					 */
   1007 					if (st_space(st, 0, SP_FILEMARKS, 0))
   1008 						goto badnews;
   1009 				} else {
   1010 					bp->b_resid = bp->b_bcount;
   1011 					bp->b_error = 0;
   1012 					bp->b_flags &= ~B_ERROR;
   1013 					st->flags &= ~ST_AT_FILEMARK;
   1014 					biodone(bp);
   1015 					continue;	/* seek more work */
   1016 				}
   1017 			}
   1018 			/*
   1019 			 * If we are at EIO (e.g. EOM) but have not reported it
   1020 			 * yet then we should report it now
   1021 			 */
   1022 			if (st->flags & ST_EIO_PENDING) {
   1023 				bp->b_resid = bp->b_bcount;
   1024 				bp->b_error = EIO;
   1025 				bp->b_flags |= B_ERROR;
   1026 				st->flags &= ~ST_EIO_PENDING;
   1027 				biodone(bp);
   1028 				continue;	/* seek more work */
   1029 			}
   1030 		}
   1031 
   1032 		/*
   1033 		 *  Fill out the scsi command
   1034 		 */
   1035 		bzero(&cmd, sizeof(cmd));
   1036 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1037 			cmd.op_code = WRITE;
   1038 			st->flags &= ~ST_FM_WRITTEN;
   1039 			st->flags |= ST_WRITTEN;
   1040 			flags = SCSI_DATA_OUT;
   1041 		} else {
   1042 			cmd.op_code = READ;
   1043 			flags = SCSI_DATA_IN;
   1044 		}
   1045 
   1046 		/*
   1047 		 * Handle "fixed-block-mode" tape drives by using the
   1048 		 * block count instead of the length.
   1049 		 */
   1050 		if (st->flags & ST_FIXEDBLOCKS) {
   1051 			cmd.byte2 |= SRW_FIXED;
   1052 			lto3b(bp->b_bcount / st->blksiz, cmd.len);
   1053 		} else
   1054 			lto3b(bp->b_bcount, cmd.len);
   1055 
   1056 		/*
   1057 		 * go ask the adapter to do all this for us
   1058 		 */
   1059 		if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1060 		    sizeof(cmd), (u_char *) bp->b_un.b_addr, bp->b_bcount, 0,
   1061 		    100000, bp, flags | SCSI_NOSLEEP) != SUCCESSFULLY_QUEUED) {
   1062 badnews:
   1063 			printf("%s: not queued\n", st->sc_dev.dv_xname);
   1064 			bp->b_flags |= B_ERROR;
   1065 			bp->b_error = EIO;
   1066 			biodone(bp);
   1067 		}
   1068 	} /* go back and see if we can cram more work in.. */
   1069 }
   1070 
   1071 /*
   1072  * Perform special action on behalf of the user;
   1073  * knows about the internals of this device
   1074  */
   1075 int
   1076 stioctl(dev, cmd, arg, flag)
   1077 	dev_t dev;
   1078 	int cmd;
   1079 	caddr_t arg;
   1080 	int flag;
   1081 {
   1082 	int error = 0;
   1083 	int unit;
   1084 	int number, nmarks, dsty;
   1085 	int flags;
   1086 	struct st_data *st;
   1087 	u_int hold_blksiz;
   1088 	u_int hold_density;
   1089 	struct mtop *mt = (struct mtop *) arg;
   1090 
   1091 	/*
   1092 	 * Find the device that the user is talking about
   1093 	 */
   1094 	flags = 0;		/* give error messages, act on errors etc. */
   1095 	unit = STUNIT(dev);
   1096 	dsty = STDSTY(dev);
   1097 	st = stcd.cd_devs[unit];
   1098 	hold_blksiz = st->blksiz;
   1099 	hold_density = st->density;
   1100 
   1101 	switch (cmd) {
   1102 
   1103 	case MTIOCGET: {
   1104 		struct mtget *g = (struct mtget *) arg;
   1105 
   1106 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1107 		bzero(g, sizeof(struct mtget));
   1108 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1109 		g->mt_blksiz = st->blksiz;
   1110 		g->mt_density = st->density;
   1111 		g->mt_mblksiz[0] = st->modes[0].blksiz;
   1112 		g->mt_mblksiz[1] = st->modes[1].blksiz;
   1113 		g->mt_mblksiz[2] = st->modes[2].blksiz;
   1114 		g->mt_mblksiz[3] = st->modes[3].blksiz;
   1115 		g->mt_mdensity[0] = st->modes[0].density;
   1116 		g->mt_mdensity[1] = st->modes[1].density;
   1117 		g->mt_mdensity[2] = st->modes[2].density;
   1118 		g->mt_mdensity[3] = st->modes[3].density;
   1119 		break;
   1120 	}
   1121 	case MTIOCTOP: {
   1122 
   1123 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1124 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
   1125 
   1126 		/* compat: in U*x it is a short */
   1127 		number = mt->mt_count;
   1128 		switch ((short) (mt->mt_op)) {
   1129 		case MTWEOF:	/* write an end-of-file record */
   1130 			error = st_write_filemarks(st, number, flags);
   1131 			break;
   1132 		case MTBSF:	/* backward space file */
   1133 			number = -number;
   1134 		case MTFSF:	/* forward space file */
   1135 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1136 			if (!error)
   1137 				error = st_space(st, number - nmarks,
   1138 				    SP_FILEMARKS, flags);
   1139 			break;
   1140 		case MTBSR:	/* backward space record */
   1141 			number = -number;
   1142 		case MTFSR:	/* forward space record */
   1143 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1144 			if (!error)
   1145 				error = st_space(st, number, SP_BLKS, flags);
   1146 			break;
   1147 		case MTREW:	/* rewind */
   1148 			error = st_rewind(st, 0, flags);
   1149 			break;
   1150 		case MTOFFL:	/* rewind and put the drive offline */
   1151 			st_unmount(st, EJECT);
   1152 			break;
   1153 		case MTNOP:	/* no operation, sets status only */
   1154 			break;
   1155 		case MTRETEN:	/* retension the tape */
   1156 			error = st_load(st, LD_RETENSION, flags);
   1157 			if (!error)
   1158 				error = st_load(st, LD_LOAD, flags);
   1159 			break;
   1160 		case MTEOM:	/* forward space to end of media */
   1161 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1162 			if (!error)
   1163 				error = st_space(st, 1, SP_EOM, flags);
   1164 			break;
   1165 		case MTCACHE:	/* enable controller cache */
   1166 		case MTNOCACHE:	/* disable controller cache */
   1167 			break;
   1168 		case MTSETBSIZ:	/* Set block size for device */
   1169 #ifdef	NOTYET
   1170 			if (!(st->flags & ST_NEW_MOUNT)) {
   1171 				uprintf("re-mount tape before changing blocksize");
   1172 				error = EINVAL;
   1173 				break;
   1174 			}
   1175 #endif
   1176 			if (number == 0) {
   1177 				st->flags &= ~ST_FIXEDBLOCKS;
   1178 			} else {
   1179 				if ((st->blkmin || st->blkmax) &&
   1180 				    (number < st->blkmin ||
   1181 				    number > st->blkmax)) {
   1182 					error = EINVAL;
   1183 					break;
   1184 				}
   1185 				st->flags |= ST_FIXEDBLOCKS;
   1186 			}
   1187 			st->blksiz = number;
   1188 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1189 			goto try_new_value;
   1190 
   1191 		case MTSETDNSTY:	/* Set density for device and mode */
   1192 			if (number > SCSI_2_MAX_DENSITY_CODE)
   1193 				error = EINVAL;
   1194 			else
   1195 				st->density = number;
   1196 			goto try_new_value;
   1197 
   1198 		default:
   1199 			error = EINVAL;
   1200 		}
   1201 		break;
   1202 	}
   1203 	case MTIOCIEOT:
   1204 	case MTIOCEEOT:
   1205 		break;
   1206 	default:
   1207 		if (STMODE(dev) == CTLMODE)
   1208 			error = scsi_do_ioctl(st->sc_link,cmd,arg,flag);
   1209 		else
   1210 			error = ENOTTY;
   1211 		break;
   1212 	}
   1213 	return error;
   1214 /*-----------------------------*/
   1215 try_new_value:
   1216 	/*
   1217 	 * Check that the mode being asked for is aggreeable to the
   1218 	 * drive. If not, put it back the way it was.
   1219 	 */
   1220 	if (error = st_mode_select(st, 0)) {	/* put it back as it was */
   1221 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1222 		st->density = hold_density;
   1223 		st->blksiz = hold_blksiz;
   1224 		if (st->blksiz)
   1225 			st->flags |= ST_FIXEDBLOCKS;
   1226 		else
   1227 			st->flags &= ~ST_FIXEDBLOCKS;
   1228 		return error;
   1229 	}
   1230 	/*
   1231 	 * As the drive liked it, if we are setting a new default,
   1232 	 * set it into the structures as such.
   1233 	 *
   1234 	 * The means for deciding this are not finalised yet
   1235 	 */
   1236 	if (STMODE(dev) == 0x03) {
   1237 		/* special mode */
   1238 		/* XXX */
   1239 		switch ((short) (mt->mt_op)) {
   1240 		case MTSETBSIZ:
   1241 			st->modes[dsty].blksiz = st->blksiz;
   1242 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1243 			break;
   1244 		case MTSETDNSTY:
   1245 			st->modes[dsty].density = st->density;
   1246 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1247 			break;
   1248 		}
   1249 	}
   1250 	return 0;
   1251 }
   1252 
   1253 /*
   1254  * Do a synchronous read.
   1255  */
   1256 int
   1257 st_read(st, buf, size, flags)
   1258 	struct st_data *st;
   1259 	u_int size;
   1260 	int flags;
   1261 	char *buf;
   1262 {
   1263 	struct scsi_rw_tape cmd;
   1264 
   1265 	/*
   1266 	 * If it's a null transfer, return immediatly
   1267 	 */
   1268 	if (size == 0)
   1269 		return 0;
   1270 	bzero(&cmd, sizeof(cmd));
   1271 	cmd.op_code = READ;
   1272 	if (st->flags & ST_FIXEDBLOCKS) {
   1273 		cmd.byte2 |= SRW_FIXED;
   1274 		lto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
   1275 		    cmd.len);
   1276 	} else
   1277 		lto3b(size, cmd.len);
   1278 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1279 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
   1280 	    flags | SCSI_DATA_IN);
   1281 }
   1282 
   1283 #ifdef	__STDC__
   1284 #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0)
   1285 #else
   1286 #define b2tol(a)	(((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
   1287 #endif
   1288 
   1289 /*
   1290  * Ask the drive what it's min and max blk sizes are.
   1291  */
   1292 int
   1293 st_read_block_limits(st, flags)
   1294 	struct st_data *st;
   1295 	int flags;
   1296 {
   1297 	struct scsi_block_limits cmd;
   1298 	struct scsi_block_limits_data block_limits;
   1299 	struct scsi_link *sc_link = st->sc_link;
   1300 	int error;
   1301 
   1302 	/*
   1303 	 * First check if we have it all loaded
   1304 	 */
   1305 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1306 		return 0;
   1307 
   1308 	/*
   1309 	 * do a 'Read Block Limits'
   1310 	 */
   1311 	bzero(&cmd, sizeof(cmd));
   1312 	cmd.op_code = READ_BLOCK_LIMITS;
   1313 
   1314 	/*
   1315 	 * do the command, update the global values
   1316 	 */
   1317 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1318 	    sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits),
   1319 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1320 		return error;
   1321 
   1322 	st->blkmin = b2tol(block_limits.min_length);
   1323 	st->blkmax = _3btol(&block_limits.max_length_2);
   1324 
   1325 	SC_DEBUG(sc_link, SDEV_DB3,
   1326 	    ("(%d <= blksiz <= %d)\n", st->blkmin, st->blkmax));
   1327 	return 0;
   1328 }
   1329 
   1330 /*
   1331  * Get the scsi driver to send a full inquiry to the
   1332  * device and use the results to fill out the global
   1333  * parameter structure.
   1334  *
   1335  * called from:
   1336  * attach
   1337  * open
   1338  * ioctl (to reset original blksize)
   1339  */
   1340 int
   1341 st_mode_sense(st, flags)
   1342 	struct st_data *st;
   1343 	int flags;
   1344 {
   1345 	u_int scsi_sense_len;
   1346 	int error;
   1347 	char *scsi_sense_ptr;
   1348 	struct scsi_mode_sense cmd;
   1349 	struct scsi_sense {
   1350 		struct scsi_mode_header header;
   1351 		struct blk_desc blk_desc;
   1352 	} scsi_sense;
   1353 
   1354 	struct scsi_sense_page_0 {
   1355 		struct scsi_mode_header header;
   1356 		struct blk_desc blk_desc;
   1357 		u_char sense_data[PAGE_0_SENSE_DATA_SIZE];
   1358 			/* Tandberg tape drives returns page 00
   1359 			 * with the sense data, whether or not
   1360 			 * you want it (ie the don't like you
   1361 			 * saying you want anything less!!!!!)
   1362 			 * They also expect page 00
   1363 			 * back when you issue a mode select
   1364 			 */
   1365 	} scsi_sense_page_0;
   1366 	struct scsi_link *sc_link = st->sc_link;
   1367 
   1368 	/*
   1369 	 * Define what sort of structure we're working with
   1370 	 */
   1371 	if (st->quirks & ST_Q_NEEDS_PAGE_0) {
   1372 		scsi_sense_len = sizeof(scsi_sense_page_0);
   1373 		scsi_sense_ptr = (char *) &scsi_sense_page_0;
   1374 	} else {
   1375 		scsi_sense_len = sizeof(scsi_sense);
   1376 		scsi_sense_ptr = (char *) &scsi_sense;
   1377 	}
   1378 	/*
   1379 	 * Set up a mode sense
   1380 	 */
   1381 	bzero(&cmd, sizeof(cmd));
   1382 	cmd.op_code = MODE_SENSE;
   1383 	cmd.length = scsi_sense_len;
   1384 
   1385 	/*
   1386 	 * do the command, but we don't need the results
   1387 	 * just print them for our interest's sake, if asked,
   1388 	 * or if we need it as a template for the mode select
   1389 	 * store it away.
   1390 	 */
   1391 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1392 	    sizeof(cmd), (u_char *) scsi_sense_ptr, scsi_sense_len,
   1393 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1394 		return error;
   1395 
   1396 	st->numblks = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.nblocks);
   1397 	st->media_blksiz = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.blklen);
   1398 	st->media_density = ((struct scsi_sense *) scsi_sense_ptr)->blk_desc.density;
   1399 	if (((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
   1400 	    SMH_DSP_WRITE_PROT)
   1401 		st->flags |= ST_READONLY;
   1402 	SC_DEBUG(sc_link, SDEV_DB3,
   1403 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1404 	    st->media_density, st->media_blksiz,
   1405 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1406 	SC_DEBUG(sc_link, SDEV_DB3,
   1407 	    ("%sbuffered\n",
   1408 	    ((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
   1409 	    SMH_DSP_BUFF_MODE ? "" : "un"));
   1410 	if (st->quirks & ST_Q_NEEDS_PAGE_0)
   1411 		bcopy(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data,
   1412 		    st->sense_data,
   1413 		    sizeof(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data));
   1414 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1415 	return 0;
   1416 }
   1417 
   1418 /*
   1419  * Send a filled out parameter structure to the drive to
   1420  * set it into the desire modes etc.
   1421  */
   1422 int
   1423 st_mode_select(st, flags)
   1424 	struct st_data *st;
   1425 	int flags;
   1426 {
   1427 	u_int dat_len;
   1428 	char *dat_ptr;
   1429 	struct scsi_mode_select cmd;
   1430 	struct dat {
   1431 		struct scsi_mode_header header;
   1432 		struct blk_desc blk_desc;
   1433 	} dat;
   1434 	struct dat_page_0 {
   1435 		struct scsi_mode_header header;
   1436 		struct blk_desc blk_desc;
   1437 		u_char sense_data[PAGE_0_SENSE_DATA_SIZE];
   1438 	} dat_page_0;
   1439 
   1440 	/*
   1441 	 * Define what sort of structure we're working with
   1442 	 */
   1443 	if (st->quirks & ST_Q_NEEDS_PAGE_0) {
   1444 		dat_len = sizeof(dat_page_0);
   1445 		dat_ptr = (char *) &dat_page_0;
   1446 	} else {
   1447 		dat_len = sizeof(dat);
   1448 		dat_ptr = (char *) &dat;
   1449 	}
   1450 	/*
   1451 	 * Set up for a mode select
   1452 	 */
   1453 	bzero(&cmd, sizeof(cmd));
   1454 	cmd.op_code = MODE_SELECT;
   1455 	cmd.length = dat_len;
   1456 
   1457 	bzero(dat_ptr, dat_len);
   1458 	((struct dat *) dat_ptr)->header.blk_desc_len = sizeof(struct blk_desc);
   1459 	((struct dat *) dat_ptr)->header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1460 	((struct dat *) dat_ptr)->blk_desc.density = st->density;
   1461 	if (st->flags & ST_FIXEDBLOCKS)
   1462 		lto3b(st->blksiz, ((struct dat *) dat_ptr)->blk_desc.blklen);
   1463 	if (st->quirks & ST_Q_NEEDS_PAGE_0) {
   1464 		bcopy(st->sense_data,
   1465 		    ((struct dat_page_0 *) dat_ptr)->sense_data,
   1466 		    sizeof(((struct dat_page_0 *) dat_ptr)->sense_data));
   1467 		/* the Tandberg tapes need the block size to */
   1468 		/* be set on each mode sense/select. */
   1469 	}
   1470 
   1471 	/*
   1472 	 * do the command
   1473 	 */
   1474 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1475 	    sizeof(cmd), (u_char *) dat_ptr, dat_len, ST_RETRIES, 5000,
   1476 	    NULL, flags | SCSI_DATA_OUT);
   1477 }
   1478 
   1479 /*
   1480  * skip N blocks/filemarks/seq filemarks/eom
   1481  */
   1482 int
   1483 st_space(st, number, what, flags)
   1484 	struct st_data *st;
   1485 	u_int what;
   1486 	int flags;
   1487 	int number;
   1488 {
   1489 	struct scsi_space cmd;
   1490 	int error;
   1491 
   1492 	switch (what) {
   1493 	case SP_BLKS:
   1494 		if (st->flags & ST_PER_ACTION) {
   1495 			if (number > 0) {
   1496 				st->flags &= ~ST_PER_ACTION;
   1497 				return EIO;
   1498 			} else if (number < 0) {
   1499 				if (st->flags & ST_AT_FILEMARK) {
   1500 					/*
   1501 					 * Handling of ST_AT_FILEMARK
   1502 					 * in st_space will fill in the
   1503 					 * right file mark count.
   1504 					 */
   1505 					error = st_space(st, 0, SP_FILEMARKS,
   1506 						flags);
   1507 					if (error)
   1508 						return error;
   1509 				}
   1510 				if (st->flags & ST_BLANK_READ) {
   1511 					st->flags &= ~ST_BLANK_READ;
   1512 					return EIO;
   1513 				}
   1514 				st->flags &= ~ST_EIO_PENDING;
   1515 			}
   1516 		}
   1517 		break;
   1518 	case SP_FILEMARKS:
   1519 		if (st->flags & ST_EIO_PENDING) {
   1520 			if (number > 0) {
   1521 				/* pretend we just discovered the error */
   1522 				st->flags &= ~ST_EIO_PENDING;
   1523 				return EIO;
   1524 			} else if (number < 0) {
   1525 				/* back away from the error */
   1526 				st->flags &= ~ST_EIO_PENDING;
   1527 			}
   1528 		}
   1529 		if (st->flags & ST_AT_FILEMARK) {
   1530 			st->flags &= ~ST_AT_FILEMARK;
   1531 			number--;
   1532 		}
   1533 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1534 			/* back away from unwritten tape */
   1535 			st->flags &= ~ST_BLANK_READ;
   1536 			number++;	/* XXX dubious */
   1537 		}
   1538 		break;
   1539 	case SP_EOM:
   1540 		if (st->flags & ST_EIO_PENDING) {
   1541 			/* pretend we just discovered the error */
   1542 			st->flags &= ~ST_EIO_PENDING;
   1543 			return EIO;
   1544 		}
   1545 		if (st->flags & ST_AT_FILEMARK)
   1546 			st->flags &= ~ST_AT_FILEMARK;
   1547 		break;
   1548 	}
   1549 	if (number == 0)
   1550 		return 0;
   1551 
   1552 	bzero(&cmd, sizeof(cmd));
   1553 	cmd.op_code = SPACE;
   1554 	cmd.byte2 = what;
   1555 	lto3b(number, cmd.number);
   1556 
   1557 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1558 	    sizeof(cmd), 0, 0, 0, 600000, NULL, flags);
   1559 }
   1560 
   1561 /*
   1562  * write N filemarks
   1563  */
   1564 int
   1565 st_write_filemarks(st, number, flags)
   1566 	struct st_data *st;
   1567 	int flags;
   1568 	int number;
   1569 {
   1570 	struct scsi_write_filemarks cmd;
   1571 
   1572 	/*
   1573 	 * It's hard to write a negative number of file marks.
   1574 	 * Don't try.
   1575 	 */
   1576 	if (number < 0)
   1577 		return EINVAL;
   1578 	switch (number) {
   1579 	case 0:		/* really a command to sync the drive's buffers */
   1580 		break;
   1581 	case 1:
   1582 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1583 			st->flags &= ~ST_WRITTEN;
   1584 		else
   1585 			st->flags |= ST_FM_WRITTEN;
   1586 		st->flags &= ~ST_PER_ACTION;
   1587 		break;
   1588 	default:
   1589 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1590 	}
   1591 
   1592 	bzero(&cmd, sizeof(cmd));
   1593 	cmd.op_code = WRITE_FILEMARKS;
   1594 	lto3b(number, cmd.number);
   1595 
   1596 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1597 	    sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
   1598 }
   1599 
   1600 /*
   1601  * Make sure the right number of file marks is on tape if the
   1602  * tape has been written.  If the position argument is true,
   1603  * leave the tape positioned where it was originally.
   1604  *
   1605  * nmarks returns the number of marks to skip (or, if position
   1606  * true, which were skipped) to get back original position.
   1607  */
   1608 int
   1609 st_check_eod(st, position, nmarks, flags)
   1610 	struct st_data *st;
   1611 	boolean position;
   1612 	int *nmarks;
   1613 	int flags;
   1614 {
   1615 	int error;
   1616 
   1617 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1618 	default:
   1619 		*nmarks = 0;
   1620 		return 0;
   1621 	case ST_WRITTEN:
   1622 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1623 		*nmarks = 1;
   1624 		break;
   1625 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1626 		*nmarks = 2;
   1627 	}
   1628 	error = st_write_filemarks(st, *nmarks, flags);
   1629 	if (position && !error)
   1630 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1631 	return error;
   1632 }
   1633 
   1634 /*
   1635  * load/unload/retension
   1636  */
   1637 int
   1638 st_load(st, type, flags)
   1639 	struct st_data *st;
   1640 	u_int type;
   1641 	int flags;
   1642 {
   1643 	struct scsi_load cmd;
   1644 
   1645 	if (type != LD_LOAD) {
   1646 		int error;
   1647 		int nmarks;
   1648 
   1649 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1650 		if (error)
   1651 			return error;
   1652 	}
   1653 	if (st->quirks & ST_Q_IGNORE_LOADS)
   1654 		return 0;
   1655 
   1656 	bzero(&cmd, sizeof(cmd));
   1657 	cmd.op_code = LOAD;
   1658 	cmd.how = type;
   1659 
   1660 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1661 	    sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
   1662 }
   1663 
   1664 /*
   1665  *  Rewind the device
   1666  */
   1667 int
   1668 st_rewind(st, immediate, flags)
   1669 	struct st_data *st;
   1670 	u_int immediate;
   1671 	int flags;
   1672 {
   1673 	struct scsi_rewind cmd;
   1674 	int error;
   1675 	int nmarks;
   1676 
   1677 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1678 	if (error)
   1679 		return error;
   1680 	st->flags &= ~ST_PER_ACTION;
   1681 
   1682 	bzero(&cmd, sizeof(cmd));
   1683 	cmd.op_code = REWIND;
   1684 	cmd.byte2 = immediate;
   1685 
   1686 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1687 	    sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
   1688 	    flags);
   1689 }
   1690 
   1691 /*
   1692  * Look at the returned sense and act on the error and detirmine
   1693  * The unix error number to pass back... (0 = report no error)
   1694  *                            (-1 = continue processing)
   1695  */
   1696 int
   1697 st_interpret_sense(xs)
   1698 	struct scsi_xfer *xs;
   1699 {
   1700 	struct scsi_link *sc_link = xs->sc_link;
   1701 	struct scsi_sense_data *sense = &xs->sense;
   1702 	boolean silent = xs->flags & SCSI_SILENT;
   1703 	struct buf *bp = xs->bp;
   1704 	int unit = sc_link->dev_unit;
   1705 	struct st_data *st = stcd.cd_devs[unit];
   1706 	u_int key;
   1707 	int info;
   1708 
   1709 	/*
   1710 	 * Get the sense fields and work out what code
   1711 	 */
   1712 	if (sense->error_code & SSD_ERRCODE_VALID)
   1713 		info = ntohl(*((int *) sense->ext.extended.info));
   1714 	else
   1715 		info = xs->datalen;	/* bad choice if fixed blocks */
   1716 	if ((sense->error_code & SSD_ERRCODE) != 0x70)
   1717 		return -1;	/* let the generic code handle it */
   1718 	if (st->flags & ST_FIXEDBLOCKS) {
   1719 		xs->resid = info * st->blksiz;
   1720 		if (sense->ext.extended.flags & SSD_EOM) {
   1721 			st->flags |= ST_EIO_PENDING;
   1722 			if (bp)
   1723 				bp->b_resid = xs->resid;
   1724 		}
   1725 		if (sense->ext.extended.flags & SSD_FILEMARK) {
   1726 			st->flags |= ST_AT_FILEMARK;
   1727 			if (bp)
   1728 				bp->b_resid = xs->resid;
   1729 		}
   1730 		if (sense->ext.extended.flags & SSD_ILI) {
   1731 			st->flags |= ST_EIO_PENDING;
   1732 			if (bp)
   1733 				bp->b_resid = xs->resid;
   1734 			if (sense->error_code & SSD_ERRCODE_VALID && !silent)
   1735 				printf("%s: block wrong size, %d blocks residual\n",
   1736 				    st->sc_dev.dv_xname, info);
   1737 
   1738 			/*
   1739 			 * This quirk code helps the drive read
   1740 			 * the first tape block, regardless of
   1741 			 * format.  That is required for these
   1742 			 * drives to return proper MODE SENSE
   1743 			 * information.
   1744 			 */
   1745 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   1746 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   1747 				st->blksiz -= 512;
   1748 		}
   1749 		/*
   1750 		 * If no data was tranfered, do it immediatly
   1751 		 */
   1752 		if (xs->resid >= xs->datalen) {
   1753 			if (st->flags & ST_EIO_PENDING)
   1754 				return EIO;
   1755 			if (st->flags & ST_AT_FILEMARK) {
   1756 				if (bp)
   1757 					bp->b_resid = xs->resid;
   1758 				return 0;
   1759 			}
   1760 		}
   1761 	} else {		/* must be variable mode */
   1762 		xs->resid = xs->datalen;	/* to be sure */
   1763 		if (sense->ext.extended.flags & SSD_EOM)
   1764 			return EIO;
   1765 		if (sense->ext.extended.flags & SSD_FILEMARK) {
   1766 			if (bp)
   1767 				bp->b_resid = bp->b_bcount;
   1768 			return 0;
   1769 		}
   1770 		if (sense->ext.extended.flags & SSD_ILI) {
   1771 			if (info < 0) {
   1772 				/*
   1773 				 * the record was bigger than the read
   1774 				 */
   1775 				if (!silent)
   1776 					printf("%s: %d-byte record too big\n",
   1777 					    st->sc_dev.dv_xname,
   1778 					    xs->datalen - info);
   1779 				return EIO;
   1780 			}
   1781 			xs->resid = info;
   1782 			if (bp)
   1783 				bp->b_resid = info;
   1784 		}
   1785 	}
   1786 	key = sense->ext.extended.flags & SSD_KEY;
   1787 
   1788 	if (key == 0x8) {
   1789 		/*
   1790 		 * This quirk code helps the drive read the
   1791 		 * first tape block, regardless of format.  That
   1792 		 * is required for these drives to return proper
   1793 		 * MODE SENSE information.
   1794 		 */
   1795 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   1796 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   1797 			/* still starting */
   1798 			st->blksiz -= 512;
   1799 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   1800 			st->flags |= ST_BLANK_READ;
   1801 			xs->resid = xs->datalen;
   1802 			if (bp) {
   1803 				bp->b_resid = xs->resid;
   1804 				/* return an EOF */
   1805 			}
   1806 			return 0;
   1807 		}
   1808 	}
   1809 	return -1;		/* let the default/generic handler handle it */
   1810 }
   1811 
   1812 /*
   1813  * The quirk here is that the drive returns some value to st_mode_sense
   1814  * incorrectly until the tape has actually passed by the head.
   1815  *
   1816  * The method is to set the drive to large fixed-block state (user-specified
   1817  * density and 1024-byte blocks), then read and rewind to get it to sense the
   1818  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   1819  * work, as a last resort, try variable- length blocks.  The result will be
   1820  * the ability to do an accurate st_mode_sense.
   1821  *
   1822  * We know we can do a rewind because we just did a load, which implies rewind.
   1823  * Rewind seems preferable to space backward if we have a virgin tape.
   1824  *
   1825  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   1826  * error processing, both part of st_interpret_sense.
   1827  */
   1828 int
   1829 st_touch_tape(st)
   1830 	struct st_data *st;
   1831 {
   1832 	char   *buf;
   1833 	u_int readsiz;
   1834 	int error;
   1835 
   1836 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   1837 	if (!buf)
   1838 		return ENOMEM;
   1839 
   1840 	if (error = st_mode_sense(st, 0))
   1841 		goto bad;
   1842 	st->blksiz = 1024;
   1843 	do {
   1844 		switch (st->blksiz) {
   1845 		case 512:
   1846 		case 1024:
   1847 			readsiz = st->blksiz;
   1848 			st->flags |= ST_FIXEDBLOCKS;
   1849 			break;
   1850 		default:
   1851 			readsiz = 1;
   1852 			st->flags &= ~ST_FIXEDBLOCKS;
   1853 		}
   1854 		if (error = st_mode_select(st, 0))
   1855 			goto bad;
   1856 		st_read(st, buf, readsiz, SCSI_SILENT);
   1857 		if (error = st_rewind(st, 0, 0)) {
   1858 bad:			free(buf, M_TEMP);
   1859 			return error;
   1860 		}
   1861 	} while (readsiz != 1 && readsiz > st->blksiz);
   1862 	free(buf, M_TEMP);
   1863 	return 0;
   1864 }
   1865