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