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