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