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