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