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