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