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