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