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