Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.105
      1 /*	$NetBSD: st.c,v 1.105 1998/11/17 14:38:43 bouyer Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Originally written by Julian Elischer (julian (at) tfs.com)
     41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     42  *
     43  * TRW Financial Systems, in accordance with their agreement with Carnegie
     44  * Mellon University, makes this software available to CMU to distribute
     45  * or use in any manner that they see fit as long as this message is kept with
     46  * the software. For this reason TFS also grants any other persons or
     47  * organisations permission to use or modify this software.
     48  *
     49  * TFS supplies this software to be publicly redistributed
     50  * on the understanding that TFS is not responsible for the correct
     51  * functioning of this software in any circumstances.
     52  *
     53  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     54  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     55  *
     56  * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
     57  */
     58 
     59 #include "opt_scsi.h"
     60 #include "rnd.h"
     61 
     62 #include <sys/types.h>
     63 #include <sys/param.h>
     64 #include <sys/systm.h>
     65 #include <sys/fcntl.h>
     66 #include <sys/errno.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/malloc.h>
     69 #include <sys/buf.h>
     70 #include <sys/proc.h>
     71 #include <sys/user.h>
     72 #include <sys/mtio.h>
     73 #include <sys/device.h>
     74 #include <sys/conf.h>
     75 #if NRND > 0
     76 #include <sys/rnd.h>
     77 #endif
     78 
     79 #include <dev/scsipi/scsipi_all.h>
     80 #include <dev/scsipi/scsi_all.h>
     81 #include <dev/scsipi/scsi_tape.h>
     82 #include <dev/scsipi/scsiconf.h>
     83 
     84 /* Defines for device specific stuff */
     85 #define DEF_FIXED_BSIZE  512
     86 #define	ST_RETRIES	4	/* only on non IO commands */
     87 
     88 #define STMODE(z)	( minor(z)       & 0x03)
     89 #define STDSTY(z)	((minor(z) >> 2) & 0x03)
     90 #define STUNIT(z)	((minor(z) >> 4)       )
     91 
     92 #define NORMAL_MODE	0
     93 #define NOREW_MODE	1
     94 #define EJECT_MODE	2
     95 #define CTRL_MODE	3
     96 
     97 #define	FALSE		0
     98 #define	TRUE		1
     99 
    100 #define	ST_IO_TIME	(3 * 60 * 1000)		/* 3 minutes */
    101 #define	ST_CTL_TIME	(30 * 1000)		/* 30 seconds */
    102 #define	ST_SPC_TIME	(4 * 60 * 60 * 1000)	/* 4 hours */
    103 
    104 /*
    105  * Define various devices that we know mis-behave in some way,
    106  * and note how they are bad, so we can correct for them
    107  */
    108 struct modes {
    109 	u_int quirks;			/* same definitions as in quirkdata */
    110 	int blksize;
    111 	u_int8_t density;
    112 };
    113 
    114 struct quirkdata {
    115 	u_int quirks;
    116 #define	ST_Q_FORCE_BLKSIZE	0x0001
    117 #define	ST_Q_SENSE_HELP		0x0002	/* must do READ for good MODE SENSE */
    118 #define	ST_Q_IGNORE_LOADS	0x0004
    119 #define	ST_Q_BLKSIZE		0x0008	/* variable-block media_blksize > 0 */
    120 #define	ST_Q_UNIMODAL		0x0010	/* unimode drive rejects mode select */
    121 	u_int page_0_size;
    122 #define	MAX_PAGE_0_SIZE	64
    123 	struct modes modes[4];
    124 };
    125 
    126 struct st_quirk_inquiry_pattern {
    127 	struct scsipi_inquiry_pattern pattern;
    128 	struct quirkdata quirkdata;
    129 };
    130 
    131 struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
    132 	{{T_SEQUENTIAL, T_REMOV,
    133 	 "        ", "                ", "    "}, {0, 0, {
    134 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    135 		{ST_Q_FORCE_BLKSIZE, 512, QIC_24},	/* minor 4-7 */
    136 		{ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600},	/* minor 8-11 */
    137 		{ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250}	/* minor 12-15 */
    138 	}}},
    139 	{{T_SEQUENTIAL, T_REMOV,
    140 	 "TANDBERG", " TDC 3600       ", ""},     {0, 12, {
    141 		{0, 0, 0},				/* minor 0-3 */
    142 		{ST_Q_FORCE_BLKSIZE, 0, QIC_525},	/* minor 4-7 */
    143 		{0, 0, QIC_150},			/* minor 8-11 */
    144 		{0, 0, QIC_120}				/* minor 12-15 */
    145 	}}},
    146  	{{T_SEQUENTIAL, T_REMOV,
    147  	 "TANDBERG", " TDC 3800       ", ""},     {0, 0, {
    148 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    149 		{0, 0, QIC_525},			/* minor 4-7 */
    150 		{0, 0, QIC_150},			/* minor 8-11 */
    151 		{0, 0, QIC_120}				/* minor 12-15 */
    152 	}}},
    153 	/*
    154 	 * lacking a manual for the 4200, it's not clear what the
    155 	 * specific density codes should be- the device is a 2.5GB
    156 	 * capable QIC drive, those density codes aren't readily
    157 	 * availabel. The 'default' will just have to do.
    158 	 */
    159  	{{T_SEQUENTIAL, T_REMOV,
    160  	 "TANDBERG", " TDC 4200       ", ""},     {0, 0, {
    161 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    162 		{0, 0, QIC_525},			/* minor 4-7 */
    163 		{0, 0, QIC_150},			/* minor 8-11 */
    164 		{0, 0, QIC_120}				/* minor 12-15 */
    165 	}}},
    166 	/*
    167 	 * At least -005 and -007 need this.  I'll assume they all do unless I
    168 	 * hear otherwise.  - mycroft, 31MAR1994
    169 	 */
    170 	{{T_SEQUENTIAL, T_REMOV,
    171 	 "ARCHIVE ", "VIPER 2525 25462", ""},     {0, 0, {
    172 		{ST_Q_SENSE_HELP, 0, 0},		/* minor 0-3 */
    173 		{ST_Q_SENSE_HELP, 0, QIC_525},		/* minor 4-7 */
    174 		{0, 0, QIC_150},			/* minor 8-11 */
    175 		{0, 0, QIC_120}				/* minor 12-15 */
    176 	}}},
    177 	/*
    178 	 * One user reports that this works for his tape drive.  It probably
    179 	 * needs more work.  - mycroft, 09APR1994
    180 	 */
    181 	{{T_SEQUENTIAL, T_REMOV,
    182 	 "SANKYO  ", "CP525           ", ""},    {0, 0, {
    183 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    184 		{ST_Q_FORCE_BLKSIZE, 512, QIC_525},	/* minor 4-7 */
    185 		{0, 0, QIC_150},			/* minor 8-11 */
    186 		{0, 0, QIC_120}				/* minor 12-15 */
    187 	}}},
    188 	{{T_SEQUENTIAL, T_REMOV,
    189 	 "ANRITSU ", "DMT780          ", ""},     {0, 0, {
    190 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    191 		{ST_Q_FORCE_BLKSIZE, 512, QIC_525},	/* minor 4-7 */
    192 		{0, 0, QIC_150},			/* minor 8-11 */
    193 		{0, 0, QIC_120}				/* minor 12-15 */
    194 	}}},
    195 	{{T_SEQUENTIAL, T_REMOV,
    196 	 "ARCHIVE ", "VIPER 150  21247", ""},     {0, 12, {
    197 		{ST_Q_SENSE_HELP, 0, 0},		/* minor 0-3 */
    198 		{0, 0, QIC_150},			/* minor 4-7 */
    199 		{0, 0, QIC_120},			/* minor 8-11 */
    200 		{0, 0, QIC_24}				/* minor 12-15 */
    201 	}}},
    202 	{{T_SEQUENTIAL, T_REMOV,
    203 	 "ARCHIVE ", "VIPER 150  21531", ""},     {0, 12, {
    204 		{ST_Q_SENSE_HELP, 0, 0},		/* minor 0-3 */
    205 		{0, 0, QIC_150},			/* minor 4-7 */
    206 		{0, 0, QIC_120},			/* minor 8-11 */
    207 		{0, 0, QIC_24}				/* minor 12-15 */
    208 	}}},
    209 	{{T_SEQUENTIAL, T_REMOV,
    210 	 "WANGTEK ", "5099ES SCSI", ""},          {0, 0, {
    211 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    212 		{0, 0, QIC_11},				/* minor 4-7 */
    213 		{0, 0, QIC_24},				/* minor 8-11 */
    214 		{0, 0, QIC_24}				/* minor 12-15 */
    215 	}}},
    216 	{{T_SEQUENTIAL, T_REMOV,
    217 	 "WANGTEK ", "5150ES SCSI", ""},          {0, 0, {
    218 		{ST_Q_FORCE_BLKSIZE, 512, 0},		/* minor 0-3 */
    219 		{0, 0, QIC_24},				/* minor 4-7 */
    220 		{0, 0, QIC_120},			/* minor 8-11 */
    221 		{0, 0, QIC_150}				/* minor 12-15 */
    222 	}}},
    223 	{{T_SEQUENTIAL, T_REMOV,
    224 	 "WANGTEK ", "5525ES SCSI REV7", ""},     {0, 0, {
    225 		{0, 0, 0},				/* minor 0-3 */
    226 		{ST_Q_BLKSIZE, 0, QIC_525},		/* minor 4-7 */
    227 		{0, 0, QIC_150},			/* minor 8-11 */
    228 		{0, 0, QIC_120}				/* minor 12-15 */
    229 	}}},
    230 	{{T_SEQUENTIAL, T_REMOV,
    231 	 "WangDAT ", "Model 1300      ", ""},     {0, 0, {
    232 		{0, 0, 0},				/* minor 0-3 */
    233 		{ST_Q_FORCE_BLKSIZE, 512, DDS},		/* minor 4-7 */
    234 		{ST_Q_FORCE_BLKSIZE, 1024, DDS},	/* minor 8-11 */
    235 		{ST_Q_FORCE_BLKSIZE, 0, DDS}		/* minor 12-15 */
    236 	}}},
    237 	{{T_SEQUENTIAL, T_REMOV,
    238 	 "EXABYTE ", "EXB-8200        ", "263H"}, {0, 5, {
    239 		{0, 0, 0},				/* minor 0-3 */
    240 		{0, 0, 0},				/* minor 4-7 */
    241 		{0, 0, 0},				/* minor 8-11 */
    242 		{0, 0, 0}				/* minor 12-15 */
    243 	}}},
    244 	{{T_SEQUENTIAL, T_REMOV,
    245 	 "STK",      "9490",             ""},
    246 				{ST_Q_FORCE_BLKSIZE|ST_Q_IGNORE_LOADS, 0, {
    247 		{0, 0, 0},				/* minor 0-3 */
    248 		{0, 0, 0},				/* minor 4-7 */
    249 		{0, 0, 0},				/* minor 8-11 */
    250 		{0, 0, 0}				/* minor 12-15 */
    251 	}}},
    252 	{{T_SEQUENTIAL, T_REMOV,
    253 	 "STK",      "SD-3",             ""},
    254 				{ST_Q_FORCE_BLKSIZE|ST_Q_IGNORE_LOADS, 0, {
    255 		{0, 0, 0},				/* minor 0-3 */
    256 		{0, 0, 0},				/* minor 4-7 */
    257 		{0, 0, 0},				/* minor 8-11 */
    258 		{0, 0, 0}				/* minor 12-15 */
    259 	}}},
    260 	{{T_SEQUENTIAL, T_REMOV,
    261 	 "IBM",      "03590",            ""},     {ST_Q_IGNORE_LOADS, 0, {
    262 		{0, 0, 0},				/* minor 0-3 */
    263 		{0, 0, 0},				/* minor 4-7 */
    264 		{0, 0, 0},				/* minor 8-11 */
    265 		{0, 0, 0}				/* minor 12-15 */
    266 	}}},
    267 	{{T_SEQUENTIAL, T_REMOV,
    268 	 "HP      ", "T4000s          ", ""},     {ST_Q_UNIMODAL, 0, {
    269 		{0, 0, QIC_3095},			/* minor 0-3 */
    270 		{0, 0, QIC_3095},			/* minor 4-7 */
    271 		{0, 0, QIC_3095},			/* minor 8-11 */
    272 		{0, 0, QIC_3095},			/* minor 12-15 */
    273 	}}},
    274 #if 0
    275 	{{T_SEQUENTIAL, T_REMOV,
    276 	 "EXABYTE ", "EXB-8200        ", ""},     {0, 12, {
    277 		{0, 0, 0},				/* minor 0-3 */
    278 		{0, 0, 0},				/* minor 4-7 */
    279 		{0, 0, 0},				/* minor 8-11 */
    280 		{0, 0, 0}				/* minor 12-15 */
    281 	}}},
    282 #endif
    283 };
    284 
    285 #define NOEJECT 0
    286 #define EJECT 1
    287 
    288 struct st_softc {
    289 	struct device sc_dev;
    290 /*--------------------present operating parameters, flags etc.---------------*/
    291 	int flags;		/* see below                         */
    292 	u_int quirks;		/* quirks for the open mode          */
    293 	int blksize;		/* blksize we are using              */
    294 	u_int8_t density;	/* present density                   */
    295 	u_int page_0_size;	/* size of page 0 data		     */
    296 	u_int last_dsty;	/* last density opened               */
    297 	short mt_resid;		/* last (short) resid                */
    298 	short mt_erreg;		/* last error (sense key) seen       */
    299 /*--------------------device/scsi parameters---------------------------------*/
    300 	struct scsipi_link *sc_link;	/* our link to the adpter etc.       */
    301 /*--------------------parameters reported by the device ---------------------*/
    302 	int blkmin;		/* min blk size                       */
    303 	int blkmax;		/* max blk size                       */
    304 	struct quirkdata *quirkdata;	/* if we have a rogue entry          */
    305 /*--------------------parameters reported by the device for this media-------*/
    306 	u_long numblks;		/* nominal blocks capacity            */
    307 	int media_blksize;	/* 0 if not ST_FIXEDBLOCKS            */
    308 	u_int8_t media_density;	/* this is what it said when asked    */
    309 /*--------------------quirks for the whole drive-----------------------------*/
    310 	u_int drive_quirks;	/* quirks of this drive               */
    311 /*--------------------How we should set up when opening each minor device----*/
    312 	struct modes modes[4];	/* plus more for each mode            */
    313 	u_int8_t  modeflags[4];	/* flags for the modes                */
    314 #define DENSITY_SET_BY_USER	0x01
    315 #define DENSITY_SET_BY_QUIRK	0x02
    316 #define BLKSIZE_SET_BY_USER	0x04
    317 #define BLKSIZE_SET_BY_QUIRK	0x08
    318 /*--------------------storage for sense data returned by the drive-----------*/
    319 	u_char sense_data[MAX_PAGE_0_SIZE];	/*
    320 						 * additional sense data needed
    321 						 * for mode sense/select.
    322 						 */
    323 	struct buf buf_queue;		/* the queue of pending IO */
    324 					/* operations */
    325 #if NRND > 0
    326 	rndsource_element_t	rnd_source;
    327 #endif
    328 };
    329 
    330 
    331 int	stmatch __P((struct device *, struct cfdata *, void *));
    332 void	stattach __P((struct device *, struct device *, void *));
    333 void	st_identify_drive __P((struct st_softc *,
    334 	    struct scsipi_inquiry_pattern *));
    335 void	st_loadquirks __P((struct st_softc *));
    336 int	st_mount_tape __P((dev_t, int));
    337 void	st_unmount __P((struct st_softc *, boolean));
    338 int	st_decide_mode __P((struct st_softc *, boolean));
    339 void	ststart __P((void *));
    340 void	stdone __P((struct scsipi_xfer *));
    341 int	st_read __P((struct st_softc *, char *, int, int));
    342 int	st_read_block_limits __P((struct st_softc *, int));
    343 int	st_mode_sense __P((struct st_softc *, int));
    344 int	st_mode_select __P((struct st_softc *, int));
    345 int	st_cmprss __P((struct st_softc *, int));
    346 int	st_space __P((struct st_softc *, int, u_int, int));
    347 int	st_write_filemarks __P((struct st_softc *, int, int));
    348 int	st_check_eod __P((struct st_softc *, boolean, int *, int));
    349 int	st_load __P((struct st_softc *, u_int, int));
    350 int	st_rewind __P((struct st_softc *, u_int, int));
    351 int	st_interpret_sense __P((struct scsipi_xfer *));
    352 int	st_touch_tape __P((struct st_softc *));
    353 int	st_erase __P((struct st_softc *, int full, int flags));
    354 int	st_rdpos __P((struct st_softc *, int, u_int32_t *));
    355 int	st_setpos __P((struct st_softc *, int, u_int32_t *));
    356 
    357 struct cfattach st_ca = {
    358 	sizeof(struct st_softc), stmatch, stattach
    359 };
    360 
    361 extern struct cfdriver st_cd;
    362 
    363 struct scsipi_device st_switch = {
    364 	st_interpret_sense,
    365 	ststart,
    366 	NULL,
    367 	stdone
    368 };
    369 
    370 #define	ST_INFO_VALID	0x0001
    371 #define	ST_BLOCK_SET	0x0002	/* block size, mode set by ioctl      */
    372 #define	ST_WRITTEN	0x0004	/* data has been written, EOD needed */
    373 #define	ST_FIXEDBLOCKS	0x0008
    374 #define	ST_AT_FILEMARK	0x0010
    375 #define	ST_EIO_PENDING	0x0020	/* error reporting deferred until next op */
    376 #define	ST_NEW_MOUNT	0x0040	/* still need to decide mode             */
    377 #define	ST_READONLY	0x0080	/* st_mode_sense says write protected */
    378 #define	ST_FM_WRITTEN	0x0100	/*
    379 				 * EOF file mark written  -- used with
    380 				 * ~ST_WRITTEN to indicate that multiple file
    381 				 * marks have been written
    382 				 */
    383 #define	ST_BLANK_READ	0x0200	/* BLANK CHECK encountered already */
    384 #define	ST_2FM_AT_EOD	0x0400	/* write 2 file marks at EOD */
    385 #define	ST_MOUNTED	0x0800	/* Device is presently mounted */
    386 #define	ST_DONTBUFFER	0x1000	/* Disable buffering/caching */
    387 #define	ST_EARLYWARN	0x2000	/* Do (deferred) EOM for variable mode */
    388 #define	ST_EOM_PENDING	0x4000	/* EOM reporting deferred until next op */
    389 
    390 #define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \
    391 			 ST_BLANK_READ)
    392 #define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN |	\
    393 			 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN |	\
    394 			 ST_2FM_AT_EOD | ST_PER_ACTION)
    395 
    396 #if	defined(ST_ENABLE_EARLYWARN)
    397 #define	ST_INIT_FLAGS	ST_EARLYWARN
    398 #else
    399 #define	ST_INIT_FLAGS	0
    400 #endif
    401 
    402 struct scsipi_inquiry_pattern st_patterns[] = {
    403 	{T_SEQUENTIAL, T_REMOV,
    404 	 "",         "",                 ""},
    405 };
    406 
    407 int
    408 stmatch(parent, match, aux)
    409 	struct device *parent;
    410 	struct cfdata *match;
    411 	void *aux;
    412 {
    413 	struct scsipibus_attach_args *sa = aux;
    414 	int priority;
    415 
    416 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    417 	    (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
    418 	    sizeof(st_patterns[0]), &priority);
    419 	return (priority);
    420 }
    421 
    422 /*
    423  * The routine called by the low level scsi routine when it discovers
    424  * A device suitable for this driver
    425  */
    426 void
    427 stattach(parent, self, aux)
    428 	struct device *parent, *self;
    429 	void *aux;
    430 {
    431 	struct st_softc *st = (void *)self;
    432 	struct scsipibus_attach_args *sa = aux;
    433 	struct scsipi_link *sc_link = sa->sa_sc_link;
    434 
    435 	SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
    436 
    437 	/*
    438 	 * Store information needed to contact our base driver
    439 	 */
    440 	st->sc_link = sc_link;
    441 	sc_link->device = &st_switch;
    442 	sc_link->device_softc = st;
    443 	sc_link->openings = 1;
    444 
    445 	/*
    446 	 * Set initial flags
    447 	 */
    448 
    449 	st->flags = ST_INIT_FLAGS;
    450 
    451 	/*
    452 	 * Check if the drive is a known criminal and take
    453 	 * Any steps needed to bring it into line
    454 	 */
    455 	st_identify_drive(st, &sa->sa_inqbuf);
    456 	/*
    457 	 * Use the subdriver to request information regarding
    458 	 * the drive. We cannot use interrupts yet, so the
    459 	 * request must specify this.
    460 	 */
    461 	printf("\n");
    462 	printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
    463 	if (scsipi_test_unit_ready(sc_link,
    464 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) ||
    465 	    st_mode_sense(st,
    466 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE))
    467 		printf("drive empty\n");
    468 	else {
    469 		printf("density code 0x%x, ", st->media_density);
    470 		if (st->media_blksize > 0)
    471 			printf("%d-byte", st->media_blksize);
    472 		else
    473 			printf("variable");
    474 		printf(" blocks, write-%s\n",
    475 		    (st->flags & ST_READONLY) ? "protected" : "enabled");
    476 	}
    477 
    478 	/*
    479 	 * Set up the buf queue for this device
    480 	 */
    481 	st->buf_queue.b_active = 0;
    482 	st->buf_queue.b_actf = 0;
    483 	st->buf_queue.b_actb = &st->buf_queue.b_actf;
    484 
    485 #if NRND > 0
    486 	rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname, RND_TYPE_TAPE);
    487 #endif
    488 }
    489 
    490 /*
    491  * Use the inquiry routine in 'scsi_base' to get drive info so we can
    492  * Further tailor our behaviour.
    493  */
    494 void
    495 st_identify_drive(st, inqbuf)
    496 	struct st_softc *st;
    497 	struct scsipi_inquiry_pattern *inqbuf;
    498 {
    499 	struct st_quirk_inquiry_pattern *finger;
    500 	int priority;
    501 
    502 	finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
    503 	    (caddr_t)st_quirk_patterns,
    504 	    sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
    505 	    sizeof(st_quirk_patterns[0]), &priority);
    506 	if (priority != 0) {
    507 		st->quirkdata = &finger->quirkdata;
    508 		st->drive_quirks = finger->quirkdata.quirks;
    509 		st->quirks = finger->quirkdata.quirks;	/* start value */
    510 		st->page_0_size = finger->quirkdata.page_0_size;
    511 		st_loadquirks(st);
    512 	}
    513 }
    514 
    515 /*
    516  * initialise the subdevices to the default (QUIRK) state.
    517  * this will remove any setting made by the system operator or previous
    518  * operations.
    519  */
    520 void
    521 st_loadquirks(st)
    522 	struct st_softc *st;
    523 {
    524 	int i;
    525 	struct	modes *mode;
    526 	struct	modes *mode2;
    527 
    528 	mode = st->quirkdata->modes;
    529 	mode2 = st->modes;
    530 	for (i = 0; i < 4; i++) {
    531 		bzero(mode2, sizeof(struct modes));
    532 		st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
    533 		    DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
    534 		    DENSITY_SET_BY_USER);
    535 		if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
    536 			mode2->blksize = mode->blksize;
    537 			st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
    538 		}
    539 		if (mode->density) {
    540 			mode2->density = mode->density;
    541 			st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
    542 		}
    543 		mode++;
    544 		mode2++;
    545 	}
    546 }
    547 
    548 /*
    549  * open the device.
    550  */
    551 int
    552 stopen(dev, flags, mode, p)
    553 	dev_t dev;
    554 	int flags;
    555 	int mode;
    556 	struct proc *p;
    557 {
    558 	int unit;
    559 	u_int stmode, dsty;
    560 	int error = 0;
    561 	struct st_softc *st;
    562 	struct scsipi_link *sc_link;
    563 
    564 	unit = STUNIT(dev);
    565 	if (unit >= st_cd.cd_ndevs)
    566 		return (ENXIO);
    567 	st = st_cd.cd_devs[unit];
    568 	if (st == NULL)
    569 		return (ENXIO);
    570 
    571 	stmode = STMODE(dev);
    572 	dsty = STDSTY(dev);
    573 	sc_link = st->sc_link;
    574 
    575 	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    576 	    unit, st_cd.cd_ndevs));
    577 
    578 
    579 	/*
    580 	 * Only allow one at a time
    581 	 */
    582 	if (sc_link->flags & SDEV_OPEN) {
    583 		printf("%s: already open\n", st->sc_dev.dv_xname);
    584 		return (EBUSY);
    585 	}
    586 
    587 	/*
    588 	 * clear any latched errors.
    589 	 */
    590 	st->mt_resid = 0;
    591 	st->mt_erreg = 0;
    592 
    593 	/*
    594 	 * Catch any unit attention errors.
    595 	 */
    596 	error = scsipi_test_unit_ready(sc_link, SCSI_IGNORE_MEDIA_CHANGE |
    597 	    (stmode == CTRL_MODE ? SCSI_SILENT : 0));
    598 	if (error && stmode != CTRL_MODE) {
    599 		goto bad;
    600 	}
    601 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    602 
    603 	/*
    604 	 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
    605 	 * been opened to set defaults and perform other, usually non-I/O
    606 	 * related, operations. In this case, do a quick check to see
    607 	 * whether the unit actually had a tape loaded (this will be known
    608 	 * as to whether or not we got a NOT READY for the above
    609 	 * unit attention). If a tape is there, go do a mount sequence.
    610 	 */
    611 	if (stmode == CTRL_MODE && st->mt_erreg == SKEY_NOT_READY) {
    612 		return (0);
    613 	}
    614 
    615 	/*
    616 	 * If it's a different mode, or if the media has been
    617 	 * invalidated, unmount the tape from the previous
    618 	 * session but continue with open processing
    619 	 */
    620 	if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
    621 		st_unmount(st, NOEJECT);
    622 
    623 	/*
    624 	 * If we are not mounted, then we should start a new
    625 	 * mount session.
    626 	 */
    627 	if (!(st->flags & ST_MOUNTED)) {
    628 		st_mount_tape(dev, flags);
    629 		st->last_dsty = dsty;
    630 	}
    631 
    632 	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
    633 	return (0);
    634 
    635 bad:
    636 	st_unmount(st, NOEJECT);
    637 	sc_link->flags &= ~SDEV_OPEN;
    638 	return (error);
    639 }
    640 
    641 /*
    642  * close the device.. only called if we are the LAST
    643  * occurence of an open device
    644  */
    645 int
    646 stclose(dev, flags, mode, p)
    647 	dev_t dev;
    648 	int flags;
    649 	int mode;
    650 	struct proc *p;
    651 {
    652 	int stxx, error = 0;
    653 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
    654 
    655 	SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
    656 
    657 	/*
    658 	 * Make sure that a tape opened in write-only mode will have
    659 	 * file marks written on it when closed, even if not written to.
    660 	 *
    661 	 * This is for SUN compatibility. Actually, the Sun way of
    662 	 * things is to:
    663 	 *
    664 	 *	only write filemarks if there are fmks to be written and
    665 	 *   		- open for write (possibly read/write)
    666 	 *		- the last operation was a write
    667 	 * 	or:
    668 	 *		- opened for wronly
    669 	 *		- no data was written (including filemarks)
    670 	 */
    671 
    672 	stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
    673 	if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
    674 	    ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
    675 		int nm;
    676 		error = st_check_eod(st, FALSE, &nm, 0);
    677 	}
    678 
    679 	switch (STMODE(dev)) {
    680 	case NORMAL_MODE:
    681 		st_unmount(st, NOEJECT);
    682 		break;
    683 	case NOREW_MODE:
    684 	case CTRL_MODE:
    685 		/*
    686 		 * Leave mounted unless media seems to have been removed.
    687 		 *
    688 		 * Otherwise, if we're to terminate a tape with more than one
    689 		 * filemark [ and because we're not rewinding here ], backspace
    690 		 * one filemark so that later appends will see an unbroken
    691 		 * sequence of:
    692 		 *
    693 		 *	file - FMK - file - FMK ... file - FMK FMK (EOM)
    694 		 */
    695 		if (!(st->sc_link->flags & SDEV_MEDIA_LOADED)) {
    696 			st_unmount(st, NOEJECT);
    697 		} else if (error == 0) {
    698 			/*
    699 			 * ST_WRITTEN was preserved from above.
    700 			 *
    701 			 * All we need to know here is:
    702 			 *
    703 			 *	Were we writing this tape and was the last
    704 			 *	operation a write?
    705 			 *
    706 			 *	Are there supposed to be 2FM at EOD?
    707 			 *
    708 			 * If both statements are true, then we backspace
    709 			 * one filemark.
    710 			 */
    711 			stxx |= (st->flags & ST_2FM_AT_EOD);
    712 			if ((flags & FWRITE) != 0 &&
    713 			    (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
    714 				error = st_space(st, -1, SP_FILEMARKS, 0);
    715 			}
    716 		}
    717 		break;
    718 	case EJECT_MODE:
    719 		st_unmount(st, EJECT);
    720 		break;
    721 	}
    722 	st->sc_link->flags &= ~SDEV_OPEN;
    723 
    724 	return (error);
    725 }
    726 
    727 /*
    728  * Start a new mount session.
    729  * Copy in all the default parameters from the selected device mode.
    730  * and try guess any that seem to be defaulted.
    731  */
    732 int
    733 st_mount_tape(dev, flags)
    734 	dev_t dev;
    735 	int flags;
    736 {
    737 	int unit;
    738 	u_int dsty;
    739 	struct st_softc *st;
    740 	struct scsipi_link *sc_link;
    741 	int error = 0;
    742 
    743 	unit = STUNIT(dev);
    744 	dsty = STDSTY(dev);
    745 	st = st_cd.cd_devs[unit];
    746 	sc_link = st->sc_link;
    747 
    748 	if (st->flags & ST_MOUNTED)
    749 		return (0);
    750 
    751 	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
    752 	st->flags |= ST_NEW_MOUNT;
    753 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    754 	/*
    755 	 * If the media is new, then make sure we give it a chance to
    756 	 * to do a 'load' instruction.  (We assume it is new.)
    757 	 */
    758 	if ((error = st_load(st, LD_LOAD, 0)) != 0)
    759 		return (error);
    760 	/*
    761 	 * Throw another dummy instruction to catch
    762 	 * 'Unit attention' errors. Many drives give
    763 	 * these after doing a Load instruction (with
    764 	 * the MEDIUM MAY HAVE CHANGED asc/ascq).
    765 	 */
    766 	scsipi_test_unit_ready(sc_link, SCSI_SILENT);	/* XXX */
    767 
    768 	/*
    769 	 * Some devices can't tell you much until they have been
    770 	 * asked to look at the media. This quirk does this.
    771 	 */
    772 	if (st->quirks & ST_Q_SENSE_HELP)
    773 		if ((error = st_touch_tape(st)) != 0)
    774 			return (error);
    775 	/*
    776 	 * Load the physical device parameters
    777 	 * loads: blkmin, blkmax
    778 	 */
    779 	if ((error = st_read_block_limits(st, 0)) != 0)
    780 		return (error);
    781 	/*
    782 	 * Load the media dependent parameters
    783 	 * includes: media_blksize,media_density,numblks
    784 	 * As we have a tape in, it should be reflected here.
    785 	 * If not you may need the "quirk" above.
    786 	 */
    787 	if ((error = st_mode_sense(st, 0)) != 0)
    788 		return (error);
    789 	/*
    790 	 * If we have gained a permanent density from somewhere,
    791 	 * then use it in preference to the one supplied by
    792 	 * default by the driver.
    793 	 */
    794 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    795 		st->density = st->modes[dsty].density;
    796 	else
    797 		st->density = st->media_density;
    798 	/*
    799 	 * If we have gained a permanent blocksize
    800 	 * then use it in preference to the one supplied by
    801 	 * default by the driver.
    802 	 */
    803 	st->flags &= ~ST_FIXEDBLOCKS;
    804 	if (st->modeflags[dsty] &
    805 	    (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
    806 		st->blksize = st->modes[dsty].blksize;
    807 		if (st->blksize)
    808 			st->flags |= ST_FIXEDBLOCKS;
    809 	} else {
    810 		if ((error = st_decide_mode(st, FALSE)) != 0)
    811 			return (error);
    812 	}
    813 	if ((error = st_mode_select(st, 0)) != 0) {
    814 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
    815 		return (error);
    816 	}
    817 	scsipi_prevent(sc_link, PR_PREVENT,
    818 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    819 	st->flags &= ~ST_NEW_MOUNT;
    820 	st->flags |= ST_MOUNTED;
    821 	sc_link->flags |= SDEV_MEDIA_LOADED;	/* move earlier? */
    822 
    823 	return (0);
    824 }
    825 
    826 /*
    827  * End the present mount session.
    828  * Rewind, and optionally eject the tape.
    829  * Reset various flags to indicate that all new
    830  * operations require another mount operation
    831  */
    832 void
    833 st_unmount(st, eject)
    834 	struct st_softc *st;
    835 	boolean eject;
    836 {
    837 	struct scsipi_link *sc_link = st->sc_link;
    838 	int nmarks;
    839 
    840 	if (!(st->flags & ST_MOUNTED))
    841 		return;
    842 	SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
    843 	st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY);
    844 	st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
    845 	scsipi_prevent(sc_link, PR_ALLOW,
    846 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    847 	if (eject)
    848 		st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
    849 	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
    850 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
    851 }
    852 
    853 /*
    854  * Given all we know about the device, media, mode, 'quirks' and
    855  * initial operation, make a decision as to how we should be set
    856  * to run (regarding blocking and EOD marks)
    857  */
    858 int
    859 st_decide_mode(st, first_read)
    860 	struct st_softc *st;
    861 	boolean	first_read;
    862 {
    863 #ifdef SCSIDEBUG
    864 	struct scsipi_link *sc_link = st->sc_link;
    865 #endif
    866 
    867 	SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
    868 
    869 	/*
    870 	 * If the drive can only handle fixed-length blocks and only at
    871 	 * one size, perhaps we should just do that.
    872 	 */
    873 	if (st->blkmin && (st->blkmin == st->blkmax)) {
    874 		st->flags |= ST_FIXEDBLOCKS;
    875 		st->blksize = st->blkmin;
    876 		SC_DEBUG(sc_link, SDEV_DB3,
    877 		    ("blkmin == blkmax of %d\n", st->blkmin));
    878 		goto done;
    879 	}
    880 	/*
    881 	 * If the tape density mandates (or even suggests) use of fixed
    882 	 * or variable-length blocks, comply.
    883 	 */
    884 	switch (st->density) {
    885 	case HALFINCH_800:
    886 	case HALFINCH_1600:
    887 	case HALFINCH_6250:
    888 	case DDS:
    889 		st->flags &= ~ST_FIXEDBLOCKS;
    890 		st->blksize = 0;
    891 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
    892 		goto done;
    893 	case QIC_11:
    894 	case QIC_24:
    895 	case QIC_120:
    896 	case QIC_150:
    897 	case QIC_525:
    898 	case QIC_1320:
    899 		st->flags |= ST_FIXEDBLOCKS;
    900 		if (st->media_blksize > 0)
    901 			st->blksize = st->media_blksize;
    902 		else
    903 			st->blksize = DEF_FIXED_BSIZE;
    904 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
    905 		goto done;
    906 	}
    907 	/*
    908 	 * If we're about to read the tape, perhaps we should choose
    909 	 * fixed or variable-length blocks and block size according to
    910 	 * what the drive found on the tape.
    911 	 */
    912 	if (first_read &&
    913 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
    914 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
    915 	    (st->media_blksize == 1024))) {
    916 		if (st->media_blksize > 0)
    917 			st->flags |= ST_FIXEDBLOCKS;
    918 		else
    919 			st->flags &= ~ST_FIXEDBLOCKS;
    920 		st->blksize = st->media_blksize;
    921 		SC_DEBUG(sc_link, SDEV_DB3,
    922 		    ("Used media_blksize of %d\n", st->media_blksize));
    923 		goto done;
    924 	}
    925 	/*
    926 	 * We're getting no hints from any direction.  Choose variable-
    927 	 * length blocks arbitrarily.
    928 	 */
    929 	st->flags &= ~ST_FIXEDBLOCKS;
    930 	st->blksize = 0;
    931 	SC_DEBUG(sc_link, SDEV_DB3,
    932 	    ("Give up and default to variable mode\n"));
    933 
    934 done:
    935 	/*
    936 	 * Decide whether or not to write two file marks to signify end-
    937 	 * of-data.  Make the decision as a function of density.  If
    938 	 * the decision is not to use a second file mark, the SCSI BLANK
    939 	 * CHECK condition code will be recognized as end-of-data when
    940 	 * first read.
    941 	 * (I think this should be a by-product of fixed/variable..julian)
    942 	 */
    943 	switch (st->density) {
    944 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
    945 	case QIC_11:
    946 	case QIC_24:
    947 	case QIC_120:
    948 	case QIC_150:
    949 	case QIC_525:
    950 	case QIC_1320:
    951 		st->flags &= ~ST_2FM_AT_EOD;
    952 		break;
    953 	default:
    954 		st->flags |= ST_2FM_AT_EOD;
    955 	}
    956 	return (0);
    957 }
    958 
    959 /*
    960  * Actually translate the requested transfer into
    961  * one the physical driver can understand
    962  * The transfer is described by a buf and will include
    963  * only one physical transfer.
    964  */
    965 void
    966 ststrategy(bp)
    967 	struct buf *bp;
    968 {
    969 	struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
    970 	struct buf *dp;
    971 	int s;
    972 
    973 	SC_DEBUG(st->sc_link, SDEV_DB1,
    974 	    ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    975 	/*
    976 	 * If it's a null transfer, return immediatly
    977 	 */
    978 	if (bp->b_bcount == 0)
    979 		goto done;
    980 	/*
    981 	 * Odd sized request on fixed drives are verboten
    982 	 */
    983 	if (st->flags & ST_FIXEDBLOCKS) {
    984 		if (bp->b_bcount % st->blksize) {
    985 			printf("%s: bad request, must be multiple of %d\n",
    986 			    st->sc_dev.dv_xname, st->blksize);
    987 			bp->b_error = EIO;
    988 			goto bad;
    989 		}
    990 	}
    991 	/*
    992 	 * as are out-of-range requests on variable drives.
    993 	 */
    994 	else if (bp->b_bcount < st->blkmin ||
    995 	    (st->blkmax && bp->b_bcount > st->blkmax)) {
    996 		printf("%s: bad request, must be between %d and %d\n",
    997 		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
    998 		bp->b_error = EIO;
    999 		goto bad;
   1000 	}
   1001 	s = splbio();
   1002 
   1003 	/*
   1004 	 * Place it in the queue of activities for this tape
   1005 	 * at the end (a bit silly because we only have on user..
   1006 	 * (but it could fork()))
   1007 	 */
   1008 	dp = &st->buf_queue;
   1009 	bp->b_actf = NULL;
   1010 	bp->b_actb = dp->b_actb;
   1011 	*dp->b_actb = bp;
   1012 	dp->b_actb = &bp->b_actf;
   1013 
   1014 	/*
   1015 	 * Tell the device to get going on the transfer if it's
   1016 	 * not doing anything, otherwise just wait for completion
   1017 	 * (All a bit silly if we're only allowing 1 open but..)
   1018 	 */
   1019 	ststart(st);
   1020 
   1021 	splx(s);
   1022 	return;
   1023 bad:
   1024 	bp->b_flags |= B_ERROR;
   1025 done:
   1026 	/*
   1027 	 * Correctly set the buf to indicate a completed xfer
   1028 	 */
   1029 	bp->b_resid = bp->b_bcount;
   1030 	biodone(bp);
   1031 	return;
   1032 }
   1033 
   1034 /*
   1035  * ststart looks to see if there is a buf waiting for the device
   1036  * and that the device is not already busy. If both are true,
   1037  * It dequeues the buf and creates a scsi command to perform the
   1038  * transfer required. The transfer request will call scsipi_done
   1039  * on completion, which will in turn call this routine again
   1040  * so that the next queued transfer is performed.
   1041  * The bufs are queued by the strategy routine (ststrategy)
   1042  *
   1043  * This routine is also called after other non-queued requests
   1044  * have been made of the scsi driver, to ensure that the queue
   1045  * continues to be drained.
   1046  * ststart() is called at splbio
   1047  */
   1048 void
   1049 ststart(v)
   1050 	void *v;
   1051 {
   1052 	struct st_softc *st = v;
   1053 	struct scsipi_link *sc_link = st->sc_link;
   1054 	register struct buf *bp, *dp;
   1055 	struct scsi_rw_tape cmd;
   1056 	int flags;
   1057 
   1058 	SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
   1059 	/*
   1060 	 * See if there is a buf to do and we are not already
   1061 	 * doing one
   1062 	 */
   1063 	while (sc_link->openings > 0) {
   1064 		/* if a special awaits, let it proceed first */
   1065 		if (sc_link->flags & SDEV_WAITING) {
   1066 			sc_link->flags &= ~SDEV_WAITING;
   1067 			wakeup((caddr_t)sc_link);
   1068 			return;
   1069 		}
   1070 
   1071 		dp = &st->buf_queue;
   1072 		if ((bp = dp->b_actf) == NULL)
   1073 			return;
   1074 		if ((dp = bp->b_actf) != NULL)
   1075 			dp->b_actb = bp->b_actb;
   1076 		else
   1077 			st->buf_queue.b_actb = bp->b_actb;
   1078 		*bp->b_actb = dp;
   1079 
   1080 		/*
   1081 		 * if the device has been unmounted byt the user
   1082 		 * then throw away all requests until done
   1083 		 */
   1084 		if (!(st->flags & ST_MOUNTED) ||
   1085 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   1086 			/* make sure that one implies the other.. */
   1087 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
   1088 			bp->b_flags |= B_ERROR;
   1089 			bp->b_error = EIO;
   1090 			bp->b_resid = bp->b_bcount;
   1091 			biodone(bp);
   1092 			continue;
   1093 		}
   1094 		/*
   1095 		 * only FIXEDBLOCK devices have pending I/O or space operations.
   1096 		 */
   1097 		if (st->flags & ST_FIXEDBLOCKS) {
   1098 			/*
   1099 			 * If we are at a filemark but have not reported it yet
   1100 			 * then we should report it now
   1101 			 */
   1102 			if (st->flags & ST_AT_FILEMARK) {
   1103 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1104 					/*
   1105 					 * Handling of ST_AT_FILEMARK in
   1106 					 * st_space will fill in the right file
   1107 					 * mark count.
   1108 					 * Back up over filemark
   1109 					 */
   1110 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
   1111 						bp->b_flags |= B_ERROR;
   1112 						bp->b_error = EIO;
   1113 						biodone(bp);
   1114 						continue;
   1115 					}
   1116 				} else {
   1117 					bp->b_resid = bp->b_bcount;
   1118 					bp->b_error = 0;
   1119 					bp->b_flags &= ~B_ERROR;
   1120 					st->flags &= ~ST_AT_FILEMARK;
   1121 					biodone(bp);
   1122 					continue;	/* seek more work */
   1123 				}
   1124 			}
   1125 		}
   1126 		/*
   1127 		 * If we are at EOM but have not reported it
   1128 		 * yet then we should report it now.
   1129 		 */
   1130 		if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
   1131 			bp->b_resid = bp->b_bcount;
   1132 			if (st->flags & ST_EIO_PENDING) {
   1133 				bp->b_error = EIO;
   1134 				bp->b_flags |= B_ERROR;
   1135 			}
   1136 			st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
   1137 			biodone(bp);
   1138 			continue;	/* seek more work */
   1139 		}
   1140 
   1141 		/*
   1142 		 *  Fill out the scsi command
   1143 		 */
   1144 		bzero(&cmd, sizeof(cmd));
   1145 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1146 			cmd.opcode = WRITE;
   1147 			st->flags &= ~ST_FM_WRITTEN;
   1148 			flags = SCSI_DATA_OUT;
   1149 		} else {
   1150 			cmd.opcode = READ;
   1151 			flags = SCSI_DATA_IN;
   1152 		}
   1153 
   1154 		/*
   1155 		 * Handle "fixed-block-mode" tape drives by using the
   1156 		 * block count instead of the length.
   1157 		 */
   1158 		if (st->flags & ST_FIXEDBLOCKS) {
   1159 			cmd.byte2 |= SRW_FIXED;
   1160 			_lto3b(bp->b_bcount / st->blksize, cmd.len);
   1161 		} else
   1162 			_lto3b(bp->b_bcount, cmd.len);
   1163 
   1164 		/*
   1165 		 * go ask the adapter to do all this for us
   1166 		 */
   1167 		if (scsipi_command(sc_link,
   1168 		    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1169 		    (u_char *)bp->b_data, bp->b_bcount,
   1170 		    0, ST_IO_TIME, bp, flags | SCSI_NOSLEEP))
   1171 			printf("%s: not queued\n", st->sc_dev.dv_xname);
   1172 	} /* go back and see if we can cram more work in.. */
   1173 }
   1174 
   1175 void
   1176 stdone(xs)
   1177 	struct scsipi_xfer *xs;
   1178 {
   1179 
   1180 	if (xs->bp != NULL) {
   1181 		struct st_softc *st = xs->sc_link->device_softc;
   1182 		if ((xs->bp->b_flags & B_READ) == B_WRITE) {
   1183 			st->flags |= ST_WRITTEN;
   1184 		} else {
   1185 			st->flags &= ~ST_WRITTEN;
   1186 		}
   1187 #if NRND > 0
   1188 		rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
   1189 #endif
   1190 	}
   1191 }
   1192 
   1193 int
   1194 stread(dev, uio, iomode)
   1195 	dev_t dev;
   1196 	struct uio *uio;
   1197 	int iomode;
   1198 {
   1199 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1200 
   1201 	return (physio(ststrategy, NULL, dev, B_READ,
   1202 	    st->sc_link->adapter->scsipi_minphys, uio));
   1203 }
   1204 
   1205 int
   1206 stwrite(dev, uio, iomode)
   1207 	dev_t dev;
   1208 	struct uio *uio;
   1209 	int iomode;
   1210 {
   1211 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1212 
   1213 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1214 	    st->sc_link->adapter->scsipi_minphys, uio));
   1215 }
   1216 
   1217 /*
   1218  * Perform special action on behalf of the user;
   1219  * knows about the internals of this device
   1220  */
   1221 int
   1222 stioctl(dev, cmd, arg, flag, p)
   1223 	dev_t dev;
   1224 	u_long cmd;
   1225 	caddr_t arg;
   1226 	int flag;
   1227 	struct proc *p;
   1228 {
   1229 	int error = 0;
   1230 	int unit;
   1231 	int number, nmarks, dsty;
   1232 	int flags;
   1233 	struct st_softc *st;
   1234 	int hold_blksize;
   1235 	u_int8_t hold_density;
   1236 	struct mtop *mt = (struct mtop *) arg;
   1237 
   1238 	/*
   1239 	 * Find the device that the user is talking about
   1240 	 */
   1241 	flags = 0;		/* give error messages, act on errors etc. */
   1242 	unit = STUNIT(dev);
   1243 	dsty = STDSTY(dev);
   1244 	st = st_cd.cd_devs[unit];
   1245 	hold_blksize = st->blksize;
   1246 	hold_density = st->density;
   1247 
   1248 	switch ((u_int) cmd) {
   1249 
   1250 	case MTIOCGET: {
   1251 		struct mtget *g = (struct mtget *) arg;
   1252 		/*
   1253 		 * (to get the current state of READONLY)
   1254 		 */
   1255 		error = st_mode_sense(st, SCSI_SILENT);
   1256 		if (error)
   1257 			break;
   1258 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1259 		bzero(g, sizeof(struct mtget));
   1260 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1261 		g->mt_blksiz = st->blksize;
   1262 		g->mt_density = st->density;
   1263 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1264 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1265 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1266 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1267 		g->mt_mdensity[0] = st->modes[0].density;
   1268 		g->mt_mdensity[1] = st->modes[1].density;
   1269 		g->mt_mdensity[2] = st->modes[2].density;
   1270 		g->mt_mdensity[3] = st->modes[3].density;
   1271 		if (st->flags & ST_READONLY)
   1272 			g->mt_dsreg |= MT_DS_RDONLY;
   1273 		if (st->flags & ST_MOUNTED)
   1274 			g->mt_dsreg |= MT_DS_MOUNTED;
   1275 		g->mt_resid = st->mt_resid;
   1276 		g->mt_erreg = st->mt_erreg;
   1277 		/*
   1278 		 * clear latched errors.
   1279 		 */
   1280 		st->mt_resid = 0;
   1281 		st->mt_erreg = 0;
   1282 		break;
   1283 	}
   1284 	case MTIOCTOP: {
   1285 
   1286 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1287 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
   1288 			mt->mt_count));
   1289 
   1290 		/* compat: in U*x it is a short */
   1291 		number = mt->mt_count;
   1292 		switch ((short) (mt->mt_op)) {
   1293 		case MTWEOF:	/* write an end-of-file record */
   1294 			error = st_write_filemarks(st, number, flags);
   1295 			break;
   1296 		case MTBSF:	/* backward space file */
   1297 			number = -number;
   1298 		case MTFSF:	/* forward space file */
   1299 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1300 			if (!error)
   1301 				error = st_space(st, number - nmarks,
   1302 				    SP_FILEMARKS, flags);
   1303 			break;
   1304 		case MTBSR:	/* backward space record */
   1305 			number = -number;
   1306 		case MTFSR:	/* forward space record */
   1307 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1308 			if (!error)
   1309 				error = st_space(st, number, SP_BLKS, flags);
   1310 			break;
   1311 		case MTREW:	/* rewind */
   1312 			error = st_rewind(st, 0, flags);
   1313 			break;
   1314 		case MTOFFL:	/* rewind and put the drive offline */
   1315 			st_unmount(st, EJECT);
   1316 			break;
   1317 		case MTNOP:	/* no operation, sets status only */
   1318 			break;
   1319 		case MTRETEN:	/* retension the tape */
   1320 			error = st_load(st, LD_RETENSION, flags);
   1321 			if (!error)
   1322 				error = st_load(st, LD_LOAD, flags);
   1323 			break;
   1324 		case MTEOM:	/* forward space to end of media */
   1325 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1326 			if (!error)
   1327 				error = st_space(st, 1, SP_EOM, flags);
   1328 			break;
   1329 		case MTCACHE:	/* enable controller cache */
   1330 			st->flags &= ~ST_DONTBUFFER;
   1331 			goto try_new_value;
   1332 		case MTNOCACHE:	/* disable controller cache */
   1333 			st->flags |= ST_DONTBUFFER;
   1334 			goto try_new_value;
   1335 		case MTERASE:	/* erase volume */
   1336 			error = st_erase(st, number, flags);
   1337 			break;
   1338 		case MTSETBSIZ:	/* Set block size for device */
   1339 #ifdef	NOTYET
   1340 			if (!(st->flags & ST_NEW_MOUNT)) {
   1341 				uprintf("re-mount tape before changing blocksize");
   1342 				error = EINVAL;
   1343 				break;
   1344 			}
   1345 #endif
   1346 			if (number == 0)
   1347 				st->flags &= ~ST_FIXEDBLOCKS;
   1348 			else {
   1349 				if ((st->blkmin || st->blkmax) &&
   1350 				    (number < st->blkmin ||
   1351 				    number > st->blkmax)) {
   1352 					error = EINVAL;
   1353 					break;
   1354 				}
   1355 				st->flags |= ST_FIXEDBLOCKS;
   1356 			}
   1357 			st->blksize = number;
   1358 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1359 			goto try_new_value;
   1360 
   1361 		case MTSETDNSTY:	/* Set density for device and mode */
   1362 			/*
   1363 			 * Any number >= 0 and <= 0xff is legal. Numbers
   1364 			 * above 0x80 are 'vendor unique'.
   1365 			 */
   1366 			if (number < 0 || number > 255) {
   1367 				error = EINVAL;
   1368 				break;
   1369 			} else
   1370 				st->density = number;
   1371 			goto try_new_value;
   1372 
   1373 		case MTCMPRESS:
   1374 			error = st_cmprss(st, number);
   1375 			break;
   1376 
   1377 		case MTEWARN:
   1378 			if (number)
   1379 				st->flags |= ST_EARLYWARN;
   1380 			else
   1381 				st->flags &= ~ST_EARLYWARN;
   1382 			break;
   1383 
   1384 		default:
   1385 			error = EINVAL;
   1386 		}
   1387 		break;
   1388 	}
   1389 	case MTIOCIEOT:
   1390 	case MTIOCEEOT:
   1391 		break;
   1392 
   1393 	case MTIOCRDSPOS:
   1394 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1395 		break;
   1396 
   1397 	case MTIOCRDHPOS:
   1398 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1399 		break;
   1400 
   1401 	case MTIOCSLOCATE:
   1402 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1403 		break;
   1404 
   1405 	case MTIOCHLOCATE:
   1406 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1407 		break;
   1408 
   1409 
   1410 	default:
   1411 		error = scsipi_do_ioctl(st->sc_link, dev, cmd, arg,
   1412 					flag, p);
   1413 		break;
   1414 	}
   1415 	return (error);
   1416 /*-----------------------------*/
   1417 try_new_value:
   1418 	/*
   1419 	 * Check that the mode being asked for is aggreeable to the
   1420 	 * drive. If not, put it back the way it was.
   1421 	 */
   1422 	if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
   1423 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1424 		st->density = hold_density;
   1425 		st->blksize = hold_blksize;
   1426 		if (st->blksize)
   1427 			st->flags |= ST_FIXEDBLOCKS;
   1428 		else
   1429 			st->flags &= ~ST_FIXEDBLOCKS;
   1430 		return (error);
   1431 	}
   1432 	/*
   1433 	 * As the drive liked it, if we are setting a new default,
   1434 	 * set it into the structures as such.
   1435 	 *
   1436 	 * The means for deciding this are not finalised yet- but
   1437 	 * if the device was opened in Control Mode, the values
   1438 	 * are persistent now across mounts.
   1439 	 */
   1440 	if (STMODE(dev) == CTRL_MODE) {
   1441 		switch ((short) (mt->mt_op)) {
   1442 		case MTSETBSIZ:
   1443 			st->modes[dsty].blksize = st->blksize;
   1444 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1445 			break;
   1446 		case MTSETDNSTY:
   1447 			st->modes[dsty].density = st->density;
   1448 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1449 			break;
   1450 		}
   1451 	}
   1452 	return (0);
   1453 }
   1454 
   1455 /*
   1456  * Do a synchronous read.
   1457  */
   1458 int
   1459 st_read(st, buf, size, flags)
   1460 	struct st_softc *st;
   1461 	int size;
   1462 	int flags;
   1463 	char *buf;
   1464 {
   1465 	struct scsi_rw_tape cmd;
   1466 
   1467 	/*
   1468 	 * If it's a null transfer, return immediatly
   1469 	 */
   1470 	if (size == 0)
   1471 		return (0);
   1472 	bzero(&cmd, sizeof(cmd));
   1473 	cmd.opcode = READ;
   1474 	if (st->flags & ST_FIXEDBLOCKS) {
   1475 		cmd.byte2 |= SRW_FIXED;
   1476 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1477 		    cmd.len);
   1478 	} else
   1479 		_lto3b(size, cmd.len);
   1480 	return (scsipi_command(st->sc_link,
   1481 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1482 	    (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | SCSI_DATA_IN));
   1483 }
   1484 
   1485 /*
   1486  * Ask the drive what it's min and max blk sizes are.
   1487  */
   1488 int
   1489 st_read_block_limits(st, flags)
   1490 	struct st_softc *st;
   1491 	int flags;
   1492 {
   1493 	struct scsi_block_limits cmd;
   1494 	struct scsi_block_limits_data block_limits;
   1495 	struct scsipi_link *sc_link = st->sc_link;
   1496 	int error;
   1497 
   1498 	/*
   1499 	 * First check if we have it all loaded
   1500 	 */
   1501 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1502 		return (0);
   1503 
   1504 	/*
   1505 	 * do a 'Read Block Limits'
   1506 	 */
   1507 	bzero(&cmd, sizeof(cmd));
   1508 	cmd.opcode = READ_BLOCK_LIMITS;
   1509 
   1510 	/*
   1511 	 * do the command, update the global values
   1512 	 */
   1513 	error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1514 	    sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
   1515 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | SCSI_DATA_IN);
   1516 	if (error)
   1517 		return (error);
   1518 
   1519 	st->blkmin = _2btol(block_limits.min_length);
   1520 	st->blkmax = _3btol(block_limits.max_length);
   1521 
   1522 	SC_DEBUG(sc_link, SDEV_DB3,
   1523 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1524 	return (0);
   1525 }
   1526 
   1527 /*
   1528  * Get the scsi driver to send a full inquiry to the
   1529  * device and use the results to fill out the global
   1530  * parameter structure.
   1531  *
   1532  * called from:
   1533  * attach
   1534  * open
   1535  * ioctl (to reset original blksize)
   1536  */
   1537 int
   1538 st_mode_sense(st, flags)
   1539 	struct st_softc *st;
   1540 	int flags;
   1541 {
   1542 	u_int scsipi_sense_len;
   1543 	int error;
   1544 	struct scsi_mode_sense cmd;
   1545 	struct scsipi_sense {
   1546 		struct scsi_mode_header header;
   1547 		struct scsi_blk_desc blk_desc;
   1548 		u_char sense_data[MAX_PAGE_0_SIZE];
   1549 	} scsipi_sense;
   1550 	struct scsipi_link *sc_link = st->sc_link;
   1551 
   1552 	scsipi_sense_len = 12 + st->page_0_size;
   1553 
   1554 	/*
   1555 	 * Set up a mode sense
   1556 	 */
   1557 	bzero(&cmd, sizeof(cmd));
   1558 	cmd.opcode = SCSI_MODE_SENSE;
   1559 	cmd.length = scsipi_sense_len;
   1560 
   1561 	/*
   1562 	 * do the command, but we don't need the results
   1563 	 * just print them for our interest's sake, if asked,
   1564 	 * or if we need it as a template for the mode select
   1565 	 * store it away.
   1566 	 */
   1567 	error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1568 	    sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len,
   1569 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | SCSI_DATA_IN);
   1570 	if (error)
   1571 		return (error);
   1572 
   1573 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
   1574 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
   1575 	st->media_density = scsipi_sense.blk_desc.density;
   1576 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1577 		st->flags |= ST_READONLY;
   1578 	else
   1579 		st->flags &= ~ST_READONLY;
   1580 	SC_DEBUG(sc_link, SDEV_DB3,
   1581 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1582 	    st->media_density, st->media_blksize,
   1583 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1584 	SC_DEBUG(sc_link, SDEV_DB3,
   1585 	    ("%sbuffered\n",
   1586 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1587 	if (st->page_0_size)
   1588 		bcopy(scsipi_sense.sense_data, st->sense_data,
   1589 		    st->page_0_size);
   1590 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1591 	return (0);
   1592 }
   1593 
   1594 /*
   1595  * Send a filled out parameter structure to the drive to
   1596  * set it into the desire modes etc.
   1597  */
   1598 int
   1599 st_mode_select(st, flags)
   1600 	struct st_softc *st;
   1601 	int flags;
   1602 {
   1603 	u_int scsi_select_len;
   1604 	struct scsi_mode_select cmd;
   1605 	struct scsi_select {
   1606 		struct scsi_mode_header header;
   1607 		struct scsi_blk_desc blk_desc;
   1608 		u_char sense_data[MAX_PAGE_0_SIZE];
   1609 	} scsi_select;
   1610 	struct scsipi_link *sc_link = st->sc_link;
   1611 
   1612 	scsi_select_len = 12 + st->page_0_size;
   1613 
   1614 	/*
   1615 	 * This quirk deals with drives that have only one valid mode
   1616 	 * and think this gives them license to reject all mode selects,
   1617 	 * even if the selected mode is the one that is supported.
   1618 	 */
   1619 	if (st->quirks & ST_Q_UNIMODAL) {
   1620 		SC_DEBUG(sc_link, SDEV_DB3,
   1621 		    ("not setting density 0x%x blksize 0x%x\n",
   1622 		    st->density, st->blksize));
   1623 		return (0);
   1624 	}
   1625 
   1626 	/*
   1627 	 * Set up for a mode select
   1628 	 */
   1629 	bzero(&cmd, sizeof(cmd));
   1630 	cmd.opcode = SCSI_MODE_SELECT;
   1631 	cmd.length = scsi_select_len;
   1632 
   1633 	bzero(&scsi_select, scsi_select_len);
   1634 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1635 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
   1636 	scsi_select.blk_desc.density = st->density;
   1637 	if (st->flags & ST_DONTBUFFER)
   1638 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
   1639 	else
   1640 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1641 	if (st->flags & ST_FIXEDBLOCKS)
   1642 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1643 	if (st->page_0_size)
   1644 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1645 
   1646 	/*
   1647 	 * do the command
   1648 	 */
   1649 	return (scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1650 	    sizeof(cmd), (u_char *)&scsi_select, scsi_select_len,
   1651 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | SCSI_DATA_OUT));
   1652 }
   1653 
   1654 int
   1655 st_cmprss(st, onoff)
   1656 	struct st_softc *st;
   1657 	int onoff;
   1658 {
   1659 	u_int scsi_dlen;
   1660 	struct scsi_mode_select mcmd;
   1661 	struct scsi_mode_sense scmd;
   1662 	struct scsi_select {
   1663 		struct scsi_mode_header header;
   1664 		struct scsi_blk_desc blk_desc;
   1665 		u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page),
   1666 		    sizeof(struct scsi_tape_dev_compression_page))];
   1667 	} scsi_pdata;
   1668 	struct scsi_tape_dev_conf_page *ptr;
   1669 	struct scsi_tape_dev_compression_page *cptr;
   1670 	struct scsipi_link *sc_link = st->sc_link;
   1671 	int error, ison, flags;
   1672 
   1673 	scsi_dlen = sizeof(scsi_pdata);
   1674 	bzero(&scsi_pdata, scsi_dlen);
   1675 
   1676 	/*
   1677 	 * Set up for a mode sense.
   1678 	 * Do DATA COMPRESSION page first.
   1679 	 */
   1680 	bzero(&scmd, sizeof(scmd));
   1681 	scmd.opcode = SCSI_MODE_SENSE;
   1682 	scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
   1683 	scmd.length = scsi_dlen;
   1684 
   1685 	flags = SCSI_SILENT;
   1686 
   1687 	/*
   1688 	 * Do the MODE SENSE command...
   1689 	 */
   1690 again:
   1691 	bzero(&scsi_pdata, scsi_dlen);
   1692 	error = scsipi_command(sc_link,
   1693 	    (struct scsipi_generic *)&scmd, sizeof(scmd),
   1694 	    (u_char *)&scsi_pdata, scsi_dlen,
   1695 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | SCSI_DATA_IN);
   1696 
   1697 	if (error) {
   1698 		if (scmd.byte2 != SMS_DBD) {
   1699 			scmd.byte2 = SMS_DBD;
   1700 			goto again;
   1701 		}
   1702 		/*
   1703 		 * Try a different page?
   1704 		 */
   1705 		if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
   1706 			scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1707 			scmd.byte2 = 0;
   1708 			goto again;
   1709 		}
   1710 		return (error);
   1711 	}
   1712 
   1713 	if (scsi_pdata.header.blk_desc_len)
   1714 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
   1715 	else
   1716 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
   1717 
   1718 	if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
   1719 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
   1720 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
   1721 		if (onoff)
   1722 			cptr->dce_dcc |= DCP_DCE;
   1723 		else
   1724 			cptr->dce_dcc &= ~DCP_DCE;
   1725 		cptr->pagecode &= ~0x80;
   1726 	} else {
   1727 		ison =  (ptr->sel_comp_alg != 0);
   1728 		if (onoff)
   1729 			ptr->sel_comp_alg = 1;
   1730 		else
   1731 			ptr->sel_comp_alg = 0;
   1732 		ptr->pagecode &= ~0x80;
   1733 		ptr->byte2 = 0;
   1734 		ptr->active_partition = 0;
   1735 		ptr->wb_full_ratio = 0;
   1736 		ptr->rb_empty_ratio = 0;
   1737 		ptr->byte8 &= ~0x30;
   1738 		ptr->gap_size = 0;
   1739 		ptr->byte10 &= ~0xe7;
   1740 		ptr->ew_bufsize[0] = 0;
   1741 		ptr->ew_bufsize[1] = 0;
   1742 		ptr->ew_bufsize[2] = 0;
   1743 		ptr->reserved = 0;
   1744 	}
   1745 	onoff = onoff ? 1 : 0;
   1746 	/*
   1747 	 * There might be a virtue in actually doing the MODE SELECTS,
   1748 	 * but let's not clog the bus over it.
   1749 	 */
   1750 	if (onoff == ison)
   1751 		return (0);
   1752 
   1753 	/*
   1754 	 * Set up for a mode select
   1755 	 */
   1756 
   1757 	scsi_pdata.header.data_length = 0;
   1758 	scsi_pdata.header.medium_type = 0;
   1759 	if ((st->flags & ST_DONTBUFFER) == 0)
   1760 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
   1761 	else
   1762 		scsi_pdata.header.dev_spec = 0;
   1763 
   1764 	bzero(&mcmd, sizeof(mcmd));
   1765 	mcmd.opcode = SCSI_MODE_SELECT;
   1766 	mcmd.byte2 = SMS_PF;
   1767 	mcmd.length = scsi_dlen;
   1768 	if (scsi_pdata.header.blk_desc_len) {
   1769 		scsi_pdata.blk_desc.density = 0;
   1770 		scsi_pdata.blk_desc.nblocks[0] = 0;
   1771 		scsi_pdata.blk_desc.nblocks[1] = 0;
   1772 		scsi_pdata.blk_desc.nblocks[2] = 0;
   1773 	}
   1774 
   1775 	/*
   1776 	 * Do the command
   1777 	 */
   1778 	error = scsipi_command(sc_link,
   1779 	    (struct scsipi_generic *)&mcmd, sizeof(mcmd),
   1780 	    (u_char *)&scsi_pdata, scsi_dlen,
   1781 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | SCSI_DATA_OUT);
   1782 
   1783 	if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
   1784 		/*
   1785 		 * Try DEVICE CONFIGURATION page.
   1786 		 */
   1787 		scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1788 		goto again;
   1789 	}
   1790 	return (error);
   1791 }
   1792 /*
   1793  * issue an erase command
   1794  */
   1795 int
   1796 st_erase(st, full, flags)
   1797 	struct st_softc *st;
   1798 	int full, flags;
   1799 {
   1800 	int tmo;
   1801 	struct scsi_erase cmd;
   1802 
   1803 	/*
   1804 	 * Full erase means set LONG bit in erase command, which asks
   1805 	 * the drive to erase the entire unit.  Without this bit, we're
   1806 	 * asking the drive to write an erase gap.
   1807 	 */
   1808 	bzero(&cmd, sizeof(cmd));
   1809 	cmd.opcode = ERASE;
   1810 	if (full) {
   1811 		cmd.byte2 = SE_IMMED|SE_LONG;
   1812 		tmo = ST_SPC_TIME;
   1813 	} else {
   1814 		tmo = ST_IO_TIME;
   1815 		cmd.byte2 = SE_IMMED;
   1816 	}
   1817 
   1818 	/*
   1819 	 * XXX We always do this asynchronously, for now.  How long should
   1820 	 * we wait if we want to (eventually) to it synchronously?
   1821 	 */
   1822 	return (scsipi_command(st->sc_link,
   1823 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1824 	    0, 0, ST_RETRIES, tmo, NULL, flags));
   1825 }
   1826 
   1827 /*
   1828  * skip N blocks/filemarks/seq filemarks/eom
   1829  */
   1830 int
   1831 st_space(st, number, what, flags)
   1832 	struct st_softc *st;
   1833 	u_int what;
   1834 	int flags;
   1835 	int number;
   1836 {
   1837 	struct scsi_space cmd;
   1838 	int error;
   1839 
   1840 	switch (what) {
   1841 	case SP_BLKS:
   1842 		if (st->flags & ST_PER_ACTION) {
   1843 			if (number > 0) {
   1844 				st->flags &= ~ST_PER_ACTION;
   1845 				return (EIO);
   1846 			} else if (number < 0) {
   1847 				if (st->flags & ST_AT_FILEMARK) {
   1848 					/*
   1849 					 * Handling of ST_AT_FILEMARK
   1850 					 * in st_space will fill in the
   1851 					 * right file mark count.
   1852 					 */
   1853 					error = st_space(st, 0, SP_FILEMARKS,
   1854 					    flags);
   1855 					if (error)
   1856 						return (error);
   1857 				}
   1858 				if (st->flags & ST_BLANK_READ) {
   1859 					st->flags &= ~ST_BLANK_READ;
   1860 					return (EIO);
   1861 				}
   1862 				st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
   1863 			}
   1864 		}
   1865 		break;
   1866 	case SP_FILEMARKS:
   1867 		if (st->flags & ST_EIO_PENDING) {
   1868 			if (number > 0) {
   1869 				/* pretend we just discovered the error */
   1870 				st->flags &= ~ST_EIO_PENDING;
   1871 				return (EIO);
   1872 			} else if (number < 0) {
   1873 				/* back away from the error */
   1874 				st->flags &= ~ST_EIO_PENDING;
   1875 			}
   1876 		}
   1877 		if (st->flags & ST_AT_FILEMARK) {
   1878 			st->flags &= ~ST_AT_FILEMARK;
   1879 			number--;
   1880 		}
   1881 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1882 			/* back away from unwritten tape */
   1883 			st->flags &= ~ST_BLANK_READ;
   1884 			number++;	/* XXX dubious */
   1885 		}
   1886 		break;
   1887 	case SP_EOM:
   1888 		if (st->flags & ST_EOM_PENDING) {
   1889 			/* we're already there */
   1890 			st->flags &= ~ST_EOM_PENDING;
   1891 			return (0);
   1892 		}
   1893 		if (st->flags & ST_EIO_PENDING) {
   1894 			/* pretend we just discovered the error */
   1895 			st->flags &= ~ST_EIO_PENDING;
   1896 			return (EIO);
   1897 		}
   1898 		if (st->flags & ST_AT_FILEMARK)
   1899 			st->flags &= ~ST_AT_FILEMARK;
   1900 		break;
   1901 	}
   1902 	if (number == 0)
   1903 		return (0);
   1904 
   1905 	bzero(&cmd, sizeof(cmd));
   1906 	cmd.opcode = SPACE;
   1907 	cmd.byte2 = what;
   1908 	_lto3b(number, cmd.number);
   1909 
   1910 	return (scsipi_command(st->sc_link,
   1911 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1912 	    0, 0, 0, ST_SPC_TIME, NULL, flags));
   1913 }
   1914 
   1915 /*
   1916  * write N filemarks
   1917  */
   1918 int
   1919 st_write_filemarks(st, number, flags)
   1920 	struct st_softc *st;
   1921 	int flags;
   1922 	int number;
   1923 {
   1924 	struct scsi_write_filemarks cmd;
   1925 
   1926 	/*
   1927 	 * It's hard to write a negative number of file marks.
   1928 	 * Don't try.
   1929 	 */
   1930 	if (number < 0)
   1931 		return (EINVAL);
   1932 	switch (number) {
   1933 	case 0:		/* really a command to sync the drive's buffers */
   1934 		break;
   1935 	case 1:
   1936 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1937 			st->flags &= ~ST_WRITTEN;
   1938 		else
   1939 			st->flags |= ST_FM_WRITTEN;
   1940 		st->flags &= ~ST_PER_ACTION;
   1941 		break;
   1942 	default:
   1943 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1944 	}
   1945 
   1946 	bzero(&cmd, sizeof(cmd));
   1947 	cmd.opcode = WRITE_FILEMARKS;
   1948 	_lto3b(number, cmd.number);
   1949 
   1950 	return (scsipi_command(st->sc_link,
   1951 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1952 	    0, 0, 0, ST_IO_TIME * 4, NULL, flags));
   1953 }
   1954 
   1955 /*
   1956  * Make sure the right number of file marks is on tape if the
   1957  * tape has been written.  If the position argument is true,
   1958  * leave the tape positioned where it was originally.
   1959  *
   1960  * nmarks returns the number of marks to skip (or, if position
   1961  * true, which were skipped) to get back original position.
   1962  */
   1963 int
   1964 st_check_eod(st, position, nmarks, flags)
   1965 	struct st_softc *st;
   1966 	boolean position;
   1967 	int *nmarks;
   1968 	int flags;
   1969 {
   1970 	int error;
   1971 
   1972 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1973 	default:
   1974 		*nmarks = 0;
   1975 		return (0);
   1976 	case ST_WRITTEN:
   1977 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1978 		*nmarks = 1;
   1979 		break;
   1980 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1981 		*nmarks = 2;
   1982 	}
   1983 	error = st_write_filemarks(st, *nmarks, flags);
   1984 	if (position && !error)
   1985 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1986 	return (error);
   1987 }
   1988 
   1989 /*
   1990  * load/unload/retension
   1991  */
   1992 int
   1993 st_load(st, type, flags)
   1994 	struct st_softc *st;
   1995 	u_int type;
   1996 	int flags;
   1997 {
   1998 	struct scsi_load cmd;
   1999 
   2000 	if (type != LD_LOAD) {
   2001 		int error;
   2002 		int nmarks;
   2003 
   2004 		error = st_check_eod(st, FALSE, &nmarks, flags);
   2005 		if (error)
   2006 			return (error);
   2007 	}
   2008 	if (st->quirks & ST_Q_IGNORE_LOADS) {
   2009 		if (type == LD_LOAD) {
   2010 			/*
   2011 			 * If we ignore loads, at least we should try a rewind.
   2012 			 */
   2013 			return st_rewind(st, 0, flags);
   2014 		}
   2015 		return (0);
   2016 	}
   2017 
   2018 	bzero(&cmd, sizeof(cmd));
   2019 	cmd.opcode = LOAD;
   2020 	cmd.how = type;
   2021 
   2022 	return (scsipi_command(st->sc_link,
   2023 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   2024 	    0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags));
   2025 }
   2026 
   2027 /*
   2028  *  Rewind the device
   2029  */
   2030 int
   2031 st_rewind(st, immediate, flags)
   2032 	struct st_softc *st;
   2033 	u_int immediate;
   2034 	int flags;
   2035 {
   2036 	struct scsi_rewind cmd;
   2037 	int error;
   2038 	int nmarks;
   2039 
   2040 	error = st_check_eod(st, FALSE, &nmarks, flags);
   2041 	if (error)
   2042 		return (error);
   2043 	st->flags &= ~ST_PER_ACTION;
   2044 
   2045 	bzero(&cmd, sizeof(cmd));
   2046 	cmd.opcode = REWIND;
   2047 	cmd.byte2 = immediate;
   2048 
   2049 	return (scsipi_command(st->sc_link,
   2050 	    (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
   2051 	    immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags));
   2052 }
   2053 
   2054 int
   2055 st_rdpos(st, hard, blkptr)
   2056 	struct st_softc *st;
   2057 	int hard;
   2058 	u_int32_t *blkptr;
   2059 {
   2060 	int error;
   2061 	u_int8_t posdata[20];
   2062 	struct scsi_tape_read_position cmd;
   2063 
   2064 	/*
   2065 	 * First flush any pending writes...
   2066 	 */
   2067 	error = st_write_filemarks(st, 0, SCSI_SILENT);
   2068 
   2069 	/*
   2070 	 * The latter case is for 'write protected' tapes
   2071 	 * which are too stupid to recognize a zero count
   2072 	 * for writing filemarks as a no-op.
   2073 	 */
   2074 	if (error != 0 && error != EACCES)
   2075 		return (error);
   2076 
   2077 	bzero(&cmd, sizeof(cmd));
   2078 	bzero(&posdata, sizeof(posdata));
   2079 	cmd.opcode = READ_POSITION;
   2080 	if (hard)
   2081 		cmd.byte1 = 1;
   2082 
   2083 	error = scsipi_command(st->sc_link,
   2084 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
   2085 	    sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
   2086 	    SCSI_SILENT | SCSI_DATA_IN);
   2087 
   2088 	if (error == 0) {
   2089 #if	0
   2090 		printf("posdata:");
   2091 		for (hard = 0; hard < sizeof(posdata); hard++)
   2092 			printf("%02x ", posdata[hard] & 0xff);
   2093 		printf("\n");
   2094 #endif
   2095 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   2096 			error = EINVAL;
   2097 		else
   2098 			*blkptr = _4btol(&posdata[4]);
   2099 	}
   2100 	return (error);
   2101 }
   2102 
   2103 int
   2104 st_setpos(st, hard, blkptr)
   2105 	struct st_softc *st;
   2106 	int hard;
   2107 	u_int32_t *blkptr;
   2108 {
   2109 	int error;
   2110 	struct scsi_tape_locate cmd;
   2111 
   2112 	/*
   2113 	 * First flush any pending writes. Strictly speaking,
   2114 	 * we're not supposed to have to worry about this,
   2115 	 * but let's be untrusting.
   2116 	 */
   2117 	error = st_write_filemarks(st, 0, SCSI_SILENT);
   2118 
   2119 	/*
   2120 	 * The latter case is for 'write protected' tapes
   2121 	 * which are too stupid to recognize a zero count
   2122 	 * for writing filemarks as a no-op.
   2123 	 */
   2124 	if (error != 0 && error != EACCES)
   2125 		return (error);
   2126 
   2127 	bzero(&cmd, sizeof(cmd));
   2128 	cmd.opcode = LOCATE;
   2129 	if (hard)
   2130 		cmd.byte2 = 1 << 2;
   2131 	_lto4b(*blkptr, cmd.blkaddr);
   2132 	error = scsipi_command(st->sc_link,
   2133 		(struct scsipi_generic *)&cmd, sizeof(cmd),
   2134 		NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
   2135 	/*
   2136 	 * XXX: Note file && block number position now unknown (if
   2137 	 * XXX: these things ever start being maintained in this driver)
   2138 	 */
   2139 	return (error);
   2140 }
   2141 
   2142 
   2143 /*
   2144  * Look at the returned sense and act on the error and determine
   2145  * the unix error number to pass back..., 0 (== report no error),
   2146  * -1 = retry the operation, -2 continue error processing.
   2147  */
   2148 int
   2149 st_interpret_sense(xs)
   2150 	struct scsipi_xfer *xs;
   2151 {
   2152 	struct scsipi_link *sc_link = xs->sc_link;
   2153 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   2154 	struct buf *bp = xs->bp;
   2155 	struct st_softc *st = sc_link->device_softc;
   2156 	int retval = SCSIRET_CONTINUE;
   2157 	int doprint = ((xs->flags & SCSI_SILENT) == 0);
   2158 	u_int8_t key;
   2159 	int32_t info;
   2160 
   2161 	/*
   2162 	 * If it isn't a extended or extended/deferred error, let
   2163 	 * the generic code handle it.
   2164 	 */
   2165 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
   2166 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFFERRED */
   2167 		return (retval);
   2168 	}
   2169 
   2170 	if (sense->error_code & SSD_ERRCODE_VALID)
   2171 		info = _4btol(sense->info);
   2172 	else
   2173 		info = xs->datalen;	/* bad choice if fixed blocks */
   2174 	key = sense->flags & SSD_KEY;
   2175 	st->mt_erreg = key;
   2176 	st->mt_resid = (short) info;
   2177 
   2178 	/*
   2179 	 * If the device is not open yet, let generic handle
   2180 	 */
   2181 	if ((sc_link->flags & SDEV_OPEN) == 0) {
   2182 		return (retval);
   2183 	}
   2184 
   2185 
   2186 	if (st->flags & ST_FIXEDBLOCKS) {
   2187 		xs->resid = info * st->blksize;
   2188 		if (sense->flags & SSD_EOM) {
   2189 			if ((st->flags & ST_EARLYWARN) == 0)
   2190 				st->flags |= ST_EIO_PENDING;
   2191 			st->flags |= ST_EOM_PENDING;
   2192 			if (bp)
   2193 				bp->b_resid = xs->resid;
   2194 		}
   2195 		if (sense->flags & SSD_FILEMARK) {
   2196 			st->flags |= ST_AT_FILEMARK;
   2197 			if (bp)
   2198 				bp->b_resid = xs->resid;
   2199 		}
   2200 		if (sense->flags & SSD_ILI) {
   2201 			st->flags |= ST_EIO_PENDING;
   2202 			if (bp)
   2203 				bp->b_resid = xs->resid;
   2204 			if (sense->error_code & SSD_ERRCODE_VALID &&
   2205 			    (xs->flags & SCSI_SILENT) == 0)
   2206 				printf("%s: block wrong size, %d blocks "
   2207 				    "residual\n", st->sc_dev.dv_xname, info);
   2208 
   2209 			/*
   2210 			 * This quirk code helps the drive read
   2211 			 * the first tape block, regardless of
   2212 			 * format.  That is required for these
   2213 			 * drives to return proper MODE SENSE
   2214 			 * information.
   2215 			 */
   2216 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   2217 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   2218 				st->blksize -= 512;
   2219 		}
   2220 		/*
   2221 		 * If data wanted and no data was tranfered, do it immediatly
   2222 		 */
   2223 		if (xs->datalen && xs->resid >= xs->datalen) {
   2224 			if (st->flags & ST_EIO_PENDING)
   2225 				return (EIO);
   2226 			if (st->flags & ST_AT_FILEMARK) {
   2227 				if (bp)
   2228 					bp->b_resid = xs->resid;
   2229 				return (SCSIRET_NOERROR);
   2230 			}
   2231 		}
   2232 	} else {		/* must be variable mode */
   2233 		if (sense->flags & SSD_EOM) {
   2234 			/*
   2235 			 * The current semantics of this
   2236 			 * driver requires EOM detection
   2237 			 * to return EIO unless early
   2238 			 * warning detection is enabled
   2239 			 * for variable mode (this is always
   2240 			 * on for fixed block mode).
   2241 			 */
   2242 			if (st->flags & ST_EARLYWARN) {
   2243 				st->flags |= ST_EOM_PENDING;
   2244 				retval = SCSIRET_NOERROR;
   2245 			} else {
   2246 				retval = EIO;
   2247 			}
   2248 
   2249 			/*
   2250 			 * If it's an unadorned EOM detection,
   2251 			 * suppress printing an error.
   2252 			 */
   2253 			if (key == SKEY_NO_SENSE) {
   2254 				doprint = 0;
   2255 			}
   2256 		} else if (sense->flags & SSD_FILEMARK) {
   2257 			retval = SCSIRET_NOERROR;
   2258 		} else if (sense->flags & SSD_ILI) {
   2259 			if (info < 0) {
   2260 				/*
   2261 				 * The tape record was bigger than the read
   2262 				 * we issued.
   2263 				 */
   2264 				if ((xs->flags & SCSI_SILENT) == 0) {
   2265 					printf("%s: %d-byte tape record too big"
   2266 					    " for %d-byte user buffer\n",
   2267 					    st->sc_dev.dv_xname,
   2268 					    xs->datalen - info, xs->datalen);
   2269 				}
   2270 				retval = EIO;
   2271 			} else {
   2272 				retval = SCSIRET_NOERROR;
   2273 			}
   2274 		}
   2275 		xs->resid = info;
   2276 		if (bp)
   2277 			bp->b_resid = info;
   2278 	}
   2279 
   2280 #ifndef	SCSIDEBUG
   2281 	if (retval == SCSIRET_NOERROR && key == SKEY_NO_SENSE) {
   2282 		doprint = 0;
   2283 	}
   2284 #endif
   2285 	if (key == SKEY_BLANK_CHECK) {
   2286 		/*
   2287 		 * This quirk code helps the drive read the
   2288 		 * first tape block, regardless of format.  That
   2289 		 * is required for these drives to return proper
   2290 		 * MODE SENSE information.
   2291 		 */
   2292 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2293 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   2294 			/* still starting */
   2295 			st->blksize -= 512;
   2296 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2297 			st->flags |= ST_BLANK_READ;
   2298 			xs->resid = xs->datalen;
   2299 			if (bp) {
   2300 				bp->b_resid = xs->resid;
   2301 				/* return an EOF */
   2302 			}
   2303 			retval = SCSIRET_NOERROR;
   2304 		}
   2305 	}
   2306 #ifdef	SCSIVERBOSE
   2307 	if (doprint) {
   2308 		scsipi_print_sense(xs, 0);
   2309 	}
   2310 #else
   2311 	if (doprint) {
   2312 		xs->sc_link->sc_print_addr(xs->sc_link);
   2313 		printf("Sense Key 0x%02x", key);
   2314 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
   2315 			switch (key) {
   2316 			case SKEY_NOT_READY:
   2317 			case SKEY_ILLEGAL_REQUEST:
   2318 			case SKEY_UNIT_ATTENTION:
   2319 			case SKEY_WRITE_PROTECT:
   2320 				break;
   2321 			case SKEY_BLANK_CHECK:
   2322 				printf(", requested size: %d (decimal)", info);
   2323 				break;
   2324 			case SKEY_ABORTED_COMMAND:
   2325 				if (xs->retries)
   2326 					printf(", retrying");
   2327 				printf(", cmd 0x%x, info 0x%x",
   2328 				    xs->cmd->opcode, info);
   2329 				break;
   2330 			default:
   2331 				printf(", info = %d (decimal)", info);
   2332 			}
   2333 		}
   2334 		if (sense->extra_len != 0) {
   2335 			int n;
   2336 			printf(", data =");
   2337 			for (n = 0; n < sense->extra_len; n++)
   2338 				printf(" %02x", sense->cmd_spec_info[n]);
   2339 		}
   2340 		printf("\n");
   2341 	}
   2342 #endif
   2343 	return (retval);
   2344 }
   2345 
   2346 /*
   2347  * The quirk here is that the drive returns some value to st_mode_sense
   2348  * incorrectly until the tape has actually passed by the head.
   2349  *
   2350  * The method is to set the drive to large fixed-block state (user-specified
   2351  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2352  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2353  * work, as a last resort, try variable- length blocks.  The result will be
   2354  * the ability to do an accurate st_mode_sense.
   2355  *
   2356  * We know we can do a rewind because we just did a load, which implies rewind.
   2357  * Rewind seems preferable to space backward if we have a virgin tape.
   2358  *
   2359  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2360  * error processing, both part of st_interpret_sense.
   2361  */
   2362 int
   2363 st_touch_tape(st)
   2364 	struct st_softc *st;
   2365 {
   2366 	char *buf;
   2367 	int readsize;
   2368 	int error;
   2369 
   2370 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   2371 	if (buf == NULL)
   2372 		return (ENOMEM);
   2373 
   2374 	if ((error = st_mode_sense(st, 0)) != 0)
   2375 		goto bad;
   2376 	st->blksize = 1024;
   2377 	do {
   2378 		switch (st->blksize) {
   2379 		case 512:
   2380 		case 1024:
   2381 			readsize = st->blksize;
   2382 			st->flags |= ST_FIXEDBLOCKS;
   2383 			break;
   2384 		default:
   2385 			readsize = 1;
   2386 			st->flags &= ~ST_FIXEDBLOCKS;
   2387 		}
   2388 		if ((error = st_mode_select(st, 0)) != 0)
   2389 			goto bad;
   2390 		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
   2391 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2392 bad:			free(buf, M_TEMP);
   2393 			return (error);
   2394 		}
   2395 	} while (readsize != 1 && readsize > st->blksize);
   2396 
   2397 	free(buf, M_TEMP);
   2398 	return (0);
   2399 }
   2400 
   2401 int
   2402 stdump(dev, blkno, va, size)
   2403 	dev_t dev;
   2404 	daddr_t blkno;
   2405 	caddr_t va;
   2406 	size_t size;
   2407 {
   2408 
   2409 	/* Not implemented. */
   2410 	return (ENXIO);
   2411 }
   2412