Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.114.2.3
      1 /*	$NetBSD: st.c,v 1.114.2.3 1999/11/01 22:54:21 thorpej 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, 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, 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_periph *sc_periph;/* 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((struct st_softc *, int, int));
    337 void	st_unmount __P((struct st_softc *, boolean));
    338 int	st_decide_mode __P((struct st_softc *, boolean));
    339 void	ststart __P((struct scsipi_periph *));
    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 const struct scsipi_periphsw 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_periph *periph = sa->sa_periph;
    434 
    435 	SC_DEBUG(periph, SCSIPI_DB2, ("stattach: "));
    436 
    437 	/*
    438 	 * Store information needed to contact our base driver
    439 	 */
    440 	st->sc_periph = periph;
    441 	periph->periph_dev = &st->sc_dev;
    442 	periph->periph_switch = &st_switch;
    443 
    444 	/*
    445 	 * Set initial flags
    446 	 */
    447 
    448 	st->flags = ST_INIT_FLAGS;
    449 
    450 	/*
    451 	 * Check if the drive is a known criminal and take
    452 	 * Any steps needed to bring it into line
    453 	 */
    454 	st_identify_drive(st, &sa->sa_inqbuf);
    455 	/*
    456 	 * Use the subdriver to request information regarding
    457 	 * the drive. We cannot use interrupts yet, so the
    458 	 * request must specify this.
    459 	 */
    460 	printf("\n");
    461 	printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
    462 	if (scsipi_test_unit_ready(periph,
    463 	    XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
    464 	    st_mode_sense(st,
    465 	    XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
    466 		printf("drive empty\n");
    467 	else {
    468 		printf("density code 0x%x, ", st->media_density);
    469 		if (st->media_blksize > 0)
    470 			printf("%d-byte", st->media_blksize);
    471 		else
    472 			printf("variable");
    473 		printf(" blocks, write-%s\n",
    474 		    (st->flags & ST_READONLY) ? "protected" : "enabled");
    475 	}
    476 
    477 	/*
    478 	 * Set up the buf queue for this device
    479 	 */
    480 	st->buf_queue.b_active = 0;
    481 	st->buf_queue.b_actf = 0;
    482 	st->buf_queue.b_actb = &st->buf_queue.b_actf;
    483 
    484 #if NRND > 0
    485 	rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname,
    486 			  RND_TYPE_TAPE, 0);
    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;
    561 	struct st_softc *st;
    562 	struct scsipi_periph *periph;
    563 	struct scsipi_adapter *adapt;
    564 
    565 	unit = STUNIT(dev);
    566 	if (unit >= st_cd.cd_ndevs)
    567 		return (ENXIO);
    568 	st = st_cd.cd_devs[unit];
    569 	if (st == NULL)
    570 		return (ENXIO);
    571 
    572 	stmode = STMODE(dev);
    573 	dsty = STDSTY(dev);
    574 
    575 	periph = st->sc_periph;
    576 	adapt = periph->periph_channel->chan_adapter;
    577 
    578 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    579 	    unit, st_cd.cd_ndevs));
    580 
    581 
    582 	/*
    583 	 * Only allow one at a time
    584 	 */
    585 	if (periph->periph_flags & PERIPH_OPEN) {
    586 		printf("%s: already open\n", st->sc_dev.dv_xname);
    587 		return (EBUSY);
    588 	}
    589 
    590 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    591 		return (error);
    592 
    593 	/*
    594 	 * clear any latched errors.
    595 	 */
    596 	st->mt_resid = 0;
    597 	st->mt_erreg = 0;
    598 
    599 	/*
    600 	 * Catch any unit attention errors.
    601 	 */
    602 	error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_MEDIA_CHANGE |
    603 	    (stmode == CTRL_MODE ? XS_CTL_SILENT : 0));
    604 	if (error && stmode != CTRL_MODE) {
    605 		goto bad;
    606 	}
    607 	periph->periph_flags |= PERIPH_OPEN;	/* unit attn are now errors */
    608 
    609 	/*
    610 	 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
    611 	 * been opened to set defaults and perform other, usually non-I/O
    612 	 * related, operations. In this case, do a quick check to see
    613 	 * whether the unit actually had a tape loaded (this will be known
    614 	 * as to whether or not we got a NOT READY for the above
    615 	 * unit attention). If a tape is there, go do a mount sequence.
    616 	 */
    617 	if (stmode == CTRL_MODE && st->mt_erreg == SKEY_NOT_READY) {
    618 		return (0);
    619 	}
    620 
    621 	/*
    622 	 * If it's a different mode, or if the media has been
    623 	 * invalidated, unmount the tape from the previous
    624 	 * session but continue with open processing
    625 	 */
    626 	if (st->last_dsty != dsty ||
    627 	    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
    628 		st_unmount(st, NOEJECT);
    629 
    630 	/*
    631 	 * If we are not mounted, then we should start a new
    632 	 * mount session.
    633 	 */
    634 	if ((st->flags & ST_MOUNTED) == 0) {
    635 		st_mount_tape(st, dsty, flags);
    636 		st->last_dsty = dsty;
    637 	}
    638 
    639 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    640 	return (0);
    641 
    642 bad:
    643 	st_unmount(st, NOEJECT);
    644 	scsipi_adapter_delref(adapt);
    645 	periph->periph_flags &= ~PERIPH_OPEN;
    646 	return (error);
    647 }
    648 
    649 /*
    650  * close the device.. only called if we are the LAST
    651  * occurence of an open device
    652  */
    653 int
    654 stclose(dev, flags, mode, p)
    655 	dev_t dev;
    656 	int flags;
    657 	int mode;
    658 	struct proc *p;
    659 {
    660 	int stxx, error = 0;
    661 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
    662 	struct scsipi_periph *periph = st->sc_periph;
    663 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    664 
    665 	SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
    666 
    667 	/*
    668 	 * Make sure that a tape opened in write-only mode will have
    669 	 * file marks written on it when closed, even if not written to.
    670 	 *
    671 	 * This is for SUN compatibility. Actually, the Sun way of
    672 	 * things is to:
    673 	 *
    674 	 *	only write filemarks if there are fmks to be written and
    675 	 *   		- open for write (possibly read/write)
    676 	 *		- the last operation was a write
    677 	 * 	or:
    678 	 *		- opened for wronly
    679 	 *		- no data was written (including filemarks)
    680 	 */
    681 
    682 	stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
    683 	if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
    684 	    ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
    685 		int nm;
    686 		error = st_check_eod(st, FALSE, &nm, 0);
    687 	}
    688 
    689 	switch (STMODE(dev)) {
    690 	case NORMAL_MODE:
    691 		st_unmount(st, NOEJECT);
    692 		break;
    693 	case NOREW_MODE:
    694 	case CTRL_MODE:
    695 		/*
    696 		 * Leave mounted unless media seems to have been removed.
    697 		 *
    698 		 * Otherwise, if we're to terminate a tape with more than one
    699 		 * filemark [ and because we're not rewinding here ], backspace
    700 		 * one filemark so that later appends will see an unbroken
    701 		 * sequence of:
    702 		 *
    703 		 *	file - FMK - file - FMK ... file - FMK FMK (EOM)
    704 		 */
    705 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    706 			st_unmount(st, NOEJECT);
    707 		} else if (error == 0) {
    708 			/*
    709 			 * ST_WRITTEN was preserved from above.
    710 			 *
    711 			 * All we need to know here is:
    712 			 *
    713 			 *	Were we writing this tape and was the last
    714 			 *	operation a write?
    715 			 *
    716 			 *	Are there supposed to be 2FM at EOD?
    717 			 *
    718 			 * If both statements are true, then we backspace
    719 			 * one filemark.
    720 			 */
    721 			stxx |= (st->flags & ST_2FM_AT_EOD);
    722 			if ((flags & FWRITE) != 0 &&
    723 			    (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
    724 				error = st_space(st, -1, SP_FILEMARKS, 0);
    725 			}
    726 		}
    727 		break;
    728 	case EJECT_MODE:
    729 		st_unmount(st, EJECT);
    730 		break;
    731 	}
    732 
    733 	scsipi_wait_drain(periph);
    734 
    735 	scsipi_adapter_delref(adapt);
    736 	periph->periph_flags &= ~PERIPH_OPEN;
    737 
    738 	return (error);
    739 }
    740 
    741 /*
    742  * Start a new mount session.
    743  * Copy in all the default parameters from the selected device mode.
    744  * and try guess any that seem to be defaulted.
    745  */
    746 int
    747 st_mount_tape(st, dsty, flags)
    748 	struct st_softc *st;
    749 	int dsty, flags;
    750 {
    751 	struct scsipi_periph *periph = st->sc_periph;
    752 	int error = 0;
    753 
    754 	if (st->flags & ST_MOUNTED)
    755 		return (0);
    756 
    757 	SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
    758 	st->flags |= ST_NEW_MOUNT;
    759 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    760 	/*
    761 	 * If the media is new, then make sure we give it a chance to
    762 	 * to do a 'load' instruction.  (We assume it is new.)
    763 	 */
    764 	if ((error = st_load(st, LD_LOAD, 0)) != 0)
    765 		return (error);
    766 	/*
    767 	 * Throw another dummy instruction to catch
    768 	 * 'Unit attention' errors. Many drives give
    769 	 * these after doing a Load instruction (with
    770 	 * the MEDIUM MAY HAVE CHANGED asc/ascq).
    771 	 */
    772 	scsipi_test_unit_ready(periph, XS_CTL_SILENT);	/* XXX */
    773 
    774 	/*
    775 	 * Some devices can't tell you much until they have been
    776 	 * asked to look at the media. This quirk does this.
    777 	 */
    778 	if (st->quirks & ST_Q_SENSE_HELP)
    779 		if ((error = st_touch_tape(st)) != 0)
    780 			return (error);
    781 	/*
    782 	 * Load the physical device parameters
    783 	 * loads: blkmin, blkmax
    784 	 */
    785 	if ((error = st_read_block_limits(st, 0)) != 0)
    786 		return (error);
    787 	/*
    788 	 * Load the media dependent parameters
    789 	 * includes: media_blksize,media_density,numblks
    790 	 * As we have a tape in, it should be reflected here.
    791 	 * If not you may need the "quirk" above.
    792 	 */
    793 	if ((error = st_mode_sense(st, 0)) != 0)
    794 		return (error);
    795 	/*
    796 	 * If we have gained a permanent density from somewhere,
    797 	 * then use it in preference to the one supplied by
    798 	 * default by the driver.
    799 	 */
    800 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    801 		st->density = st->modes[dsty].density;
    802 	else
    803 		st->density = st->media_density;
    804 	/*
    805 	 * If we have gained a permanent blocksize
    806 	 * then use it in preference to the one supplied by
    807 	 * default by the driver.
    808 	 */
    809 	st->flags &= ~ST_FIXEDBLOCKS;
    810 	if (st->modeflags[dsty] &
    811 	    (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
    812 		st->blksize = st->modes[dsty].blksize;
    813 		if (st->blksize)
    814 			st->flags |= ST_FIXEDBLOCKS;
    815 	} else {
    816 		if ((error = st_decide_mode(st, FALSE)) != 0)
    817 			return (error);
    818 	}
    819 	if ((error = st_mode_select(st, 0)) != 0) {
    820 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
    821 		return (error);
    822 	}
    823 	scsipi_prevent(periph, PR_PREVENT,
    824 	    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
    825 	st->flags &= ~ST_NEW_MOUNT;
    826 	st->flags |= ST_MOUNTED;
    827 	periph->periph_flags |= PERIPH_MEDIA_LOADED;	/* move earlier? */
    828 
    829 	return (0);
    830 }
    831 
    832 /*
    833  * End the present mount session.
    834  * Rewind, and optionally eject the tape.
    835  * Reset various flags to indicate that all new
    836  * operations require another mount operation
    837  */
    838 void
    839 st_unmount(st, eject)
    840 	struct st_softc *st;
    841 	boolean eject;
    842 {
    843 	struct scsipi_periph *periph = st->sc_periph;
    844 	int nmarks;
    845 
    846 	if ((st->flags & ST_MOUNTED) == 0)
    847 		return;
    848 	SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
    849 	st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
    850 	st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
    851 	scsipi_prevent(periph, PR_ALLOW,
    852 	    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
    853 	if (eject)
    854 		st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
    855 	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
    856 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    857 }
    858 
    859 /*
    860  * Given all we know about the device, media, mode, 'quirks' and
    861  * initial operation, make a decision as to how we should be set
    862  * to run (regarding blocking and EOD marks)
    863  */
    864 int
    865 st_decide_mode(st, first_read)
    866 	struct st_softc *st;
    867 	boolean	first_read;
    868 {
    869 
    870 	SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
    871 
    872 	/*
    873 	 * If the drive can only handle fixed-length blocks and only at
    874 	 * one size, perhaps we should just do that.
    875 	 */
    876 	if (st->blkmin && (st->blkmin == st->blkmax)) {
    877 		st->flags |= ST_FIXEDBLOCKS;
    878 		st->blksize = st->blkmin;
    879 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    880 		    ("blkmin == blkmax of %d\n", st->blkmin));
    881 		goto done;
    882 	}
    883 	/*
    884 	 * If the tape density mandates (or even suggests) use of fixed
    885 	 * or variable-length blocks, comply.
    886 	 */
    887 	switch (st->density) {
    888 	case HALFINCH_800:
    889 	case HALFINCH_1600:
    890 	case HALFINCH_6250:
    891 	case DDS:
    892 		st->flags &= ~ST_FIXEDBLOCKS;
    893 		st->blksize = 0;
    894 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    895 		    ("density specified variable\n"));
    896 		goto done;
    897 	case QIC_11:
    898 	case QIC_24:
    899 	case QIC_120:
    900 	case QIC_150:
    901 	case QIC_525:
    902 	case QIC_1320:
    903 		st->flags |= ST_FIXEDBLOCKS;
    904 		if (st->media_blksize > 0)
    905 			st->blksize = st->media_blksize;
    906 		else
    907 			st->blksize = DEF_FIXED_BSIZE;
    908 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    909 		    ("density specified fixed\n"));
    910 		goto done;
    911 	}
    912 	/*
    913 	 * If we're about to read the tape, perhaps we should choose
    914 	 * fixed or variable-length blocks and block size according to
    915 	 * what the drive found on the tape.
    916 	 */
    917 	if (first_read &&
    918 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
    919 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
    920 	    (st->media_blksize == 1024))) {
    921 		if (st->media_blksize > 0)
    922 			st->flags |= ST_FIXEDBLOCKS;
    923 		else
    924 			st->flags &= ~ST_FIXEDBLOCKS;
    925 		st->blksize = st->media_blksize;
    926 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    927 		    ("Used media_blksize of %d\n", st->media_blksize));
    928 		goto done;
    929 	}
    930 	/*
    931 	 * We're getting no hints from any direction.  Choose variable-
    932 	 * length blocks arbitrarily.
    933 	 */
    934 	st->flags &= ~ST_FIXEDBLOCKS;
    935 	st->blksize = 0;
    936 	SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    937 	    ("Give up and default to variable mode\n"));
    938 
    939 done:
    940 	/*
    941 	 * Decide whether or not to write two file marks to signify end-
    942 	 * of-data.  Make the decision as a function of density.  If
    943 	 * the decision is not to use a second file mark, the SCSI BLANK
    944 	 * CHECK condition code will be recognized as end-of-data when
    945 	 * first read.
    946 	 * (I think this should be a by-product of fixed/variable..julian)
    947 	 */
    948 	switch (st->density) {
    949 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
    950 	case QIC_11:
    951 	case QIC_24:
    952 	case QIC_120:
    953 	case QIC_150:
    954 	case QIC_525:
    955 	case QIC_1320:
    956 		st->flags &= ~ST_2FM_AT_EOD;
    957 		break;
    958 	default:
    959 		st->flags |= ST_2FM_AT_EOD;
    960 	}
    961 	return (0);
    962 }
    963 
    964 /*
    965  * Actually translate the requested transfer into
    966  * one the physical driver can understand
    967  * The transfer is described by a buf and will include
    968  * only one physical transfer.
    969  */
    970 void
    971 ststrategy(bp)
    972 	struct buf *bp;
    973 {
    974 	struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
    975 	struct buf *dp;
    976 	int s;
    977 
    978 	SC_DEBUG(st->sc_periph, SCSIPI_DB1,
    979 	    ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    980 	/*
    981 	 * If it's a null transfer, return immediatly
    982 	 */
    983 	if (bp->b_bcount == 0)
    984 		goto done;
    985 
    986 	/* If offset is negative, error */
    987 	if (bp->b_blkno < 0) {
    988 		bp->b_error = EINVAL;
    989 		goto bad;
    990 	}
    991 
    992 	/*
    993 	 * Odd sized request on fixed drives are verboten
    994 	 */
    995 	if (st->flags & ST_FIXEDBLOCKS) {
    996 		if (bp->b_bcount % st->blksize) {
    997 			printf("%s: bad request, must be multiple of %d\n",
    998 			    st->sc_dev.dv_xname, st->blksize);
    999 			bp->b_error = EIO;
   1000 			goto bad;
   1001 		}
   1002 	}
   1003 	/*
   1004 	 * as are out-of-range requests on variable drives.
   1005 	 */
   1006 	else if (bp->b_bcount < st->blkmin ||
   1007 	    (st->blkmax && bp->b_bcount > st->blkmax)) {
   1008 		printf("%s: bad request, must be between %d and %d\n",
   1009 		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
   1010 		bp->b_error = EIO;
   1011 		goto bad;
   1012 	}
   1013 	s = splbio();
   1014 
   1015 	/*
   1016 	 * Place it in the queue of activities for this tape
   1017 	 * at the end (a bit silly because we only have on user..
   1018 	 * (but it could fork()))
   1019 	 */
   1020 	dp = &st->buf_queue;
   1021 	bp->b_actf = NULL;
   1022 	bp->b_actb = dp->b_actb;
   1023 	*dp->b_actb = bp;
   1024 	dp->b_actb = &bp->b_actf;
   1025 
   1026 	/*
   1027 	 * Tell the device to get going on the transfer if it's
   1028 	 * not doing anything, otherwise just wait for completion
   1029 	 * (All a bit silly if we're only allowing 1 open but..)
   1030 	 */
   1031 	ststart(st->sc_periph);
   1032 
   1033 	splx(s);
   1034 	return;
   1035 bad:
   1036 	bp->b_flags |= B_ERROR;
   1037 done:
   1038 	/*
   1039 	 * Correctly set the buf to indicate a completed xfer
   1040 	 */
   1041 	bp->b_resid = bp->b_bcount;
   1042 	biodone(bp);
   1043 	return;
   1044 }
   1045 
   1046 /*
   1047  * ststart looks to see if there is a buf waiting for the device
   1048  * and that the device is not already busy. If both are true,
   1049  * It dequeues the buf and creates a scsi command to perform the
   1050  * transfer required. The transfer request will call scsipi_done
   1051  * on completion, which will in turn call this routine again
   1052  * so that the next queued transfer is performed.
   1053  * The bufs are queued by the strategy routine (ststrategy)
   1054  *
   1055  * This routine is also called after other non-queued requests
   1056  * have been made of the scsi driver, to ensure that the queue
   1057  * continues to be drained.
   1058  * ststart() is called at splbio
   1059  */
   1060 void
   1061 ststart(periph)
   1062 	struct scsipi_periph *periph;
   1063 {
   1064 	struct st_softc *st = (void *)periph->periph_dev;
   1065 	register struct buf *bp, *dp;
   1066 	struct scsi_rw_tape cmd;
   1067 	int flags, error;
   1068 
   1069 	SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
   1070 	/*
   1071 	 * See if there is a buf to do and we are not already
   1072 	 * doing one
   1073 	 */
   1074 	while (periph->periph_active < periph->periph_openings) {
   1075 		/* if a special awaits, let it proceed first */
   1076 		if (periph->periph_flags & PERIPH_WAITING) {
   1077 			periph->periph_flags &= ~PERIPH_WAITING;
   1078 			wakeup((caddr_t)periph);
   1079 			return;
   1080 		}
   1081 
   1082 		dp = &st->buf_queue;
   1083 		if ((bp = dp->b_actf) == NULL)
   1084 			return;
   1085 		if ((dp = bp->b_actf) != NULL)
   1086 			dp->b_actb = bp->b_actb;
   1087 		else
   1088 			st->buf_queue.b_actb = bp->b_actb;
   1089 		*bp->b_actb = dp;
   1090 
   1091 		/*
   1092 		 * if the device has been unmounted bye the user
   1093 		 * then throw away all requests until done
   1094 		 */
   1095 		if ((st->flags & ST_MOUNTED) == 0 ||
   1096 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   1097 			/* make sure that one implies the other.. */
   1098 			periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
   1099 			bp->b_flags |= B_ERROR;
   1100 			bp->b_error = EIO;
   1101 			bp->b_resid = bp->b_bcount;
   1102 			biodone(bp);
   1103 			continue;
   1104 		}
   1105 		/*
   1106 		 * only FIXEDBLOCK devices have pending I/O or space operations.
   1107 		 */
   1108 		if (st->flags & ST_FIXEDBLOCKS) {
   1109 			/*
   1110 			 * If we are at a filemark but have not reported it yet
   1111 			 * then we should report it now
   1112 			 */
   1113 			if (st->flags & ST_AT_FILEMARK) {
   1114 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1115 					/*
   1116 					 * Handling of ST_AT_FILEMARK in
   1117 					 * st_space will fill in the right file
   1118 					 * mark count.
   1119 					 * Back up over filemark
   1120 					 */
   1121 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
   1122 						bp->b_flags |= B_ERROR;
   1123 						bp->b_error = EIO;
   1124 						biodone(bp);
   1125 						continue;
   1126 					}
   1127 				} else {
   1128 					bp->b_resid = bp->b_bcount;
   1129 					bp->b_error = 0;
   1130 					bp->b_flags &= ~B_ERROR;
   1131 					st->flags &= ~ST_AT_FILEMARK;
   1132 					biodone(bp);
   1133 					continue;	/* seek more work */
   1134 				}
   1135 			}
   1136 		}
   1137 		/*
   1138 		 * If we are at EOM but have not reported it
   1139 		 * yet then we should report it now.
   1140 		 */
   1141 		if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
   1142 			bp->b_resid = bp->b_bcount;
   1143 			if (st->flags & ST_EIO_PENDING) {
   1144 				bp->b_error = EIO;
   1145 				bp->b_flags |= B_ERROR;
   1146 			}
   1147 			st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
   1148 			biodone(bp);
   1149 			continue;	/* seek more work */
   1150 		}
   1151 
   1152 		/*
   1153 		 * Fill out the scsi command
   1154 		 */
   1155 		bzero(&cmd, sizeof(cmd));
   1156 		flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
   1157 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1158 			cmd.opcode = WRITE;
   1159 			st->flags &= ~ST_FM_WRITTEN;
   1160 			flags |= XS_CTL_DATA_OUT;
   1161 		} else {
   1162 			cmd.opcode = READ;
   1163 			flags |= XS_CTL_DATA_IN;
   1164 		}
   1165 
   1166 		/*
   1167 		 * Handle "fixed-block-mode" tape drives by using the
   1168 		 * block count instead of the length.
   1169 		 */
   1170 		if (st->flags & ST_FIXEDBLOCKS) {
   1171 			cmd.byte2 |= SRW_FIXED;
   1172 			_lto3b(bp->b_bcount / st->blksize, cmd.len);
   1173 		} else
   1174 			_lto3b(bp->b_bcount, cmd.len);
   1175 
   1176 		/*
   1177 		 * go ask the adapter to do all this for us
   1178 		 */
   1179 		error = scsipi_command(periph,
   1180 		    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1181 		    (u_char *)bp->b_data, bp->b_bcount,
   1182 		    0, ST_IO_TIME, bp, flags);
   1183 		if (error) {
   1184 			printf("%s: not queued, error %d\n",
   1185 			    st->sc_dev.dv_xname, error);
   1186 		}
   1187 	} /* go back and see if we can cram more work in.. */
   1188 }
   1189 
   1190 void
   1191 stdone(xs)
   1192 	struct scsipi_xfer *xs;
   1193 {
   1194 	struct st_softc *st = (void *)xs->xs_periph->periph_dev;
   1195 
   1196 	if (xs->bp != NULL) {
   1197 		if ((xs->bp->b_flags & B_READ) == B_WRITE) {
   1198 			st->flags |= ST_WRITTEN;
   1199 		} else {
   1200 			st->flags &= ~ST_WRITTEN;
   1201 		}
   1202 #if NRND > 0
   1203 		rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
   1204 #endif
   1205 	}
   1206 }
   1207 
   1208 int
   1209 stread(dev, uio, iomode)
   1210 	dev_t dev;
   1211 	struct uio *uio;
   1212 	int iomode;
   1213 {
   1214 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1215 
   1216 	return (physio(ststrategy, NULL, dev, B_READ,
   1217 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1218 }
   1219 
   1220 int
   1221 stwrite(dev, uio, iomode)
   1222 	dev_t dev;
   1223 	struct uio *uio;
   1224 	int iomode;
   1225 {
   1226 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1227 
   1228 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1229 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1230 }
   1231 
   1232 /*
   1233  * Perform special action on behalf of the user;
   1234  * knows about the internals of this device
   1235  */
   1236 int
   1237 stioctl(dev, cmd, arg, flag, p)
   1238 	dev_t dev;
   1239 	u_long cmd;
   1240 	caddr_t arg;
   1241 	int flag;
   1242 	struct proc *p;
   1243 {
   1244 	int error = 0;
   1245 	int unit;
   1246 	int number, nmarks, dsty;
   1247 	int flags;
   1248 	struct st_softc *st;
   1249 	int hold_blksize;
   1250 	u_int8_t hold_density;
   1251 	struct mtop *mt = (struct mtop *) arg;
   1252 
   1253 	/*
   1254 	 * Find the device that the user is talking about
   1255 	 */
   1256 	flags = 0;		/* give error messages, act on errors etc. */
   1257 	unit = STUNIT(dev);
   1258 	dsty = STDSTY(dev);
   1259 	st = st_cd.cd_devs[unit];
   1260 	hold_blksize = st->blksize;
   1261 	hold_density = st->density;
   1262 
   1263 	switch ((u_int) cmd) {
   1264 
   1265 	case MTIOCGET: {
   1266 		struct mtget *g = (struct mtget *) arg;
   1267 		/*
   1268 		 * (to get the current state of READONLY)
   1269 		 */
   1270 		error = st_mode_sense(st, XS_CTL_SILENT);
   1271 		if (error)
   1272 			break;
   1273 		SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
   1274 		bzero(g, sizeof(struct mtget));
   1275 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1276 		g->mt_blksiz = st->blksize;
   1277 		g->mt_density = st->density;
   1278 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1279 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1280 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1281 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1282 		g->mt_mdensity[0] = st->modes[0].density;
   1283 		g->mt_mdensity[1] = st->modes[1].density;
   1284 		g->mt_mdensity[2] = st->modes[2].density;
   1285 		g->mt_mdensity[3] = st->modes[3].density;
   1286 		if (st->flags & ST_READONLY)
   1287 			g->mt_dsreg |= MT_DS_RDONLY;
   1288 		if (st->flags & ST_MOUNTED)
   1289 			g->mt_dsreg |= MT_DS_MOUNTED;
   1290 		g->mt_resid = st->mt_resid;
   1291 		g->mt_erreg = st->mt_erreg;
   1292 		/*
   1293 		 * clear latched errors.
   1294 		 */
   1295 		st->mt_resid = 0;
   1296 		st->mt_erreg = 0;
   1297 		break;
   1298 	}
   1299 	case MTIOCTOP: {
   1300 
   1301 		SC_DEBUG(st->sc_periph, SCSIPI_DB1,
   1302 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
   1303 			mt->mt_count));
   1304 
   1305 		/* compat: in U*x it is a short */
   1306 		number = mt->mt_count;
   1307 		switch ((short) (mt->mt_op)) {
   1308 		case MTWEOF:	/* write an end-of-file record */
   1309 			error = st_write_filemarks(st, number, flags);
   1310 			break;
   1311 		case MTBSF:	/* backward space file */
   1312 			number = -number;
   1313 		case MTFSF:	/* forward space file */
   1314 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1315 			if (!error)
   1316 				error = st_space(st, number - nmarks,
   1317 				    SP_FILEMARKS, flags);
   1318 			break;
   1319 		case MTBSR:	/* backward space record */
   1320 			number = -number;
   1321 		case MTFSR:	/* forward space record */
   1322 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1323 			if (!error)
   1324 				error = st_space(st, number, SP_BLKS, flags);
   1325 			break;
   1326 		case MTREW:	/* rewind */
   1327 			error = st_rewind(st, 0, flags);
   1328 			break;
   1329 		case MTOFFL:	/* rewind and put the drive offline */
   1330 			st_unmount(st, EJECT);
   1331 			break;
   1332 		case MTNOP:	/* no operation, sets status only */
   1333 			break;
   1334 		case MTRETEN:	/* retension the tape */
   1335 			error = st_load(st, LD_RETENSION, flags);
   1336 			if (!error)
   1337 				error = st_load(st, LD_LOAD, flags);
   1338 			break;
   1339 		case MTEOM:	/* forward space to end of media */
   1340 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1341 			if (!error)
   1342 				error = st_space(st, 1, SP_EOM, flags);
   1343 			break;
   1344 		case MTCACHE:	/* enable controller cache */
   1345 			st->flags &= ~ST_DONTBUFFER;
   1346 			goto try_new_value;
   1347 		case MTNOCACHE:	/* disable controller cache */
   1348 			st->flags |= ST_DONTBUFFER;
   1349 			goto try_new_value;
   1350 		case MTERASE:	/* erase volume */
   1351 			error = st_erase(st, number, flags);
   1352 			break;
   1353 		case MTSETBSIZ:	/* Set block size for device */
   1354 #ifdef	NOTYET
   1355 			if (!(st->flags & ST_NEW_MOUNT)) {
   1356 				uprintf("re-mount tape before changing blocksize");
   1357 				error = EINVAL;
   1358 				break;
   1359 			}
   1360 #endif
   1361 			if (number == 0)
   1362 				st->flags &= ~ST_FIXEDBLOCKS;
   1363 			else {
   1364 				if ((st->blkmin || st->blkmax) &&
   1365 				    (number < st->blkmin ||
   1366 				    number > st->blkmax)) {
   1367 					error = EINVAL;
   1368 					break;
   1369 				}
   1370 				st->flags |= ST_FIXEDBLOCKS;
   1371 			}
   1372 			st->blksize = number;
   1373 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1374 			goto try_new_value;
   1375 
   1376 		case MTSETDNSTY:	/* Set density for device and mode */
   1377 			/*
   1378 			 * Any number >= 0 and <= 0xff is legal. Numbers
   1379 			 * above 0x80 are 'vendor unique'.
   1380 			 */
   1381 			if (number < 0 || number > 255) {
   1382 				error = EINVAL;
   1383 				break;
   1384 			} else
   1385 				st->density = number;
   1386 			goto try_new_value;
   1387 
   1388 		case MTCMPRESS:
   1389 			error = st_cmprss(st, number);
   1390 			break;
   1391 
   1392 		case MTEWARN:
   1393 			if (number)
   1394 				st->flags |= ST_EARLYWARN;
   1395 			else
   1396 				st->flags &= ~ST_EARLYWARN;
   1397 			break;
   1398 
   1399 		default:
   1400 			error = EINVAL;
   1401 		}
   1402 		break;
   1403 	}
   1404 	case MTIOCIEOT:
   1405 	case MTIOCEEOT:
   1406 		break;
   1407 
   1408 	case MTIOCRDSPOS:
   1409 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1410 		break;
   1411 
   1412 	case MTIOCRDHPOS:
   1413 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1414 		break;
   1415 
   1416 	case MTIOCSLOCATE:
   1417 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1418 		break;
   1419 
   1420 	case MTIOCHLOCATE:
   1421 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1422 		break;
   1423 
   1424 
   1425 	default:
   1426 		error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
   1427 					flag, p);
   1428 		break;
   1429 	}
   1430 	return (error);
   1431 /*-----------------------------*/
   1432 try_new_value:
   1433 	/*
   1434 	 * Check that the mode being asked for is aggreeable to the
   1435 	 * drive. If not, put it back the way it was.
   1436 	 */
   1437 	if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
   1438 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1439 		st->density = hold_density;
   1440 		st->blksize = hold_blksize;
   1441 		if (st->blksize)
   1442 			st->flags |= ST_FIXEDBLOCKS;
   1443 		else
   1444 			st->flags &= ~ST_FIXEDBLOCKS;
   1445 		return (error);
   1446 	}
   1447 	/*
   1448 	 * As the drive liked it, if we are setting a new default,
   1449 	 * set it into the structures as such.
   1450 	 *
   1451 	 * The means for deciding this are not finalised yet- but
   1452 	 * if the device was opened in Control Mode, the values
   1453 	 * are persistent now across mounts.
   1454 	 */
   1455 	if (STMODE(dev) == CTRL_MODE) {
   1456 		switch ((short) (mt->mt_op)) {
   1457 		case MTSETBSIZ:
   1458 			st->modes[dsty].blksize = st->blksize;
   1459 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1460 			break;
   1461 		case MTSETDNSTY:
   1462 			st->modes[dsty].density = st->density;
   1463 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1464 			break;
   1465 		}
   1466 	}
   1467 	return (0);
   1468 }
   1469 
   1470 /*
   1471  * Do a synchronous read.
   1472  */
   1473 int
   1474 st_read(st, buf, size, flags)
   1475 	struct st_softc *st;
   1476 	int size;
   1477 	int flags;
   1478 	char *buf;
   1479 {
   1480 	struct scsi_rw_tape cmd;
   1481 
   1482 	/*
   1483 	 * If it's a null transfer, return immediatly
   1484 	 */
   1485 	if (size == 0)
   1486 		return (0);
   1487 	bzero(&cmd, sizeof(cmd));
   1488 	cmd.opcode = READ;
   1489 	if (st->flags & ST_FIXEDBLOCKS) {
   1490 		cmd.byte2 |= SRW_FIXED;
   1491 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1492 		    cmd.len);
   1493 	} else
   1494 		_lto3b(size, cmd.len);
   1495 	return (scsipi_command(st->sc_periph,
   1496 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1497 	    (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
   1498 }
   1499 
   1500 /*
   1501  * Ask the drive what it's min and max blk sizes are.
   1502  */
   1503 int
   1504 st_read_block_limits(st, flags)
   1505 	struct st_softc *st;
   1506 	int flags;
   1507 {
   1508 	struct scsi_block_limits cmd;
   1509 	struct scsi_block_limits_data block_limits;
   1510 	struct scsipi_periph *periph = st->sc_periph;
   1511 	int error;
   1512 
   1513 	/*
   1514 	 * First check if we have it all loaded
   1515 	 */
   1516 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED))
   1517 		return (0);
   1518 
   1519 	/*
   1520 	 * do a 'Read Block Limits'
   1521 	 */
   1522 	bzero(&cmd, sizeof(cmd));
   1523 	cmd.opcode = READ_BLOCK_LIMITS;
   1524 
   1525 	/*
   1526 	 * do the command, update the global values
   1527 	 */
   1528 	error = scsipi_command(periph, (struct scsipi_generic *)&cmd,
   1529 	    sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
   1530 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1531 	if (error)
   1532 		return (error);
   1533 
   1534 	st->blkmin = _2btol(block_limits.min_length);
   1535 	st->blkmax = _3btol(block_limits.max_length);
   1536 
   1537 	SC_DEBUG(periph, SCSIPI_DB3,
   1538 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1539 	return (0);
   1540 }
   1541 
   1542 /*
   1543  * Get the scsi driver to send a full inquiry to the
   1544  * device and use the results to fill out the global
   1545  * parameter structure.
   1546  *
   1547  * called from:
   1548  * attach
   1549  * open
   1550  * ioctl (to reset original blksize)
   1551  */
   1552 int
   1553 st_mode_sense(st, flags)
   1554 	struct st_softc *st;
   1555 	int flags;
   1556 {
   1557 	u_int scsipi_sense_len;
   1558 	int error;
   1559 	struct scsi_mode_sense cmd;
   1560 	struct scsipi_sense {
   1561 		struct scsi_mode_header header;
   1562 		struct scsi_blk_desc blk_desc;
   1563 		u_char sense_data[MAX_PAGE_0_SIZE];
   1564 	} scsipi_sense;
   1565 	struct scsipi_periph *periph = st->sc_periph;
   1566 
   1567 	scsipi_sense_len = 12 + st->page_0_size;
   1568 
   1569 	/*
   1570 	 * Set up a mode sense
   1571 	 */
   1572 	bzero(&cmd, sizeof(cmd));
   1573 	cmd.opcode = SCSI_MODE_SENSE;
   1574 	cmd.length = scsipi_sense_len;
   1575 
   1576 	/*
   1577 	 * do the command, but we don't need the results
   1578 	 * just print them for our interest's sake, if asked,
   1579 	 * or if we need it as a template for the mode select
   1580 	 * store it away.
   1581 	 */
   1582 	error = scsipi_command(periph, (struct scsipi_generic *)&cmd,
   1583 	    sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len,
   1584 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1585 	if (error)
   1586 		return (error);
   1587 
   1588 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
   1589 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
   1590 	st->media_density = scsipi_sense.blk_desc.density;
   1591 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1592 		st->flags |= ST_READONLY;
   1593 	else
   1594 		st->flags &= ~ST_READONLY;
   1595 	SC_DEBUG(periph, SCSIPI_DB3,
   1596 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1597 	    st->media_density, st->media_blksize,
   1598 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1599 	SC_DEBUG(periph, SCSIPI_DB3,
   1600 	    ("%sbuffered\n",
   1601 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1602 	if (st->page_0_size)
   1603 		bcopy(scsipi_sense.sense_data, st->sense_data,
   1604 		    st->page_0_size);
   1605 	periph->periph_flags |= PERIPH_MEDIA_LOADED;
   1606 	return (0);
   1607 }
   1608 
   1609 /*
   1610  * Send a filled out parameter structure to the drive to
   1611  * set it into the desire modes etc.
   1612  */
   1613 int
   1614 st_mode_select(st, flags)
   1615 	struct st_softc *st;
   1616 	int flags;
   1617 {
   1618 	u_int scsi_select_len;
   1619 	struct scsi_mode_select cmd;
   1620 	struct scsi_select {
   1621 		struct scsi_mode_header header;
   1622 		struct scsi_blk_desc blk_desc;
   1623 		u_char sense_data[MAX_PAGE_0_SIZE];
   1624 	} scsi_select;
   1625 	struct scsipi_periph *periph = st->sc_periph;
   1626 
   1627 	scsi_select_len = 12 + st->page_0_size;
   1628 
   1629 	/*
   1630 	 * This quirk deals with drives that have only one valid mode
   1631 	 * and think this gives them license to reject all mode selects,
   1632 	 * even if the selected mode is the one that is supported.
   1633 	 */
   1634 	if (st->quirks & ST_Q_UNIMODAL) {
   1635 		SC_DEBUG(periph, SCSIPI_DB3,
   1636 		    ("not setting density 0x%x blksize 0x%x\n",
   1637 		    st->density, st->blksize));
   1638 		return (0);
   1639 	}
   1640 
   1641 	/*
   1642 	 * Set up for a mode select
   1643 	 */
   1644 	bzero(&cmd, sizeof(cmd));
   1645 	cmd.opcode = SCSI_MODE_SELECT;
   1646 	cmd.length = scsi_select_len;
   1647 
   1648 	bzero(&scsi_select, scsi_select_len);
   1649 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1650 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
   1651 	scsi_select.blk_desc.density = st->density;
   1652 	if (st->flags & ST_DONTBUFFER)
   1653 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
   1654 	else
   1655 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1656 	if (st->flags & ST_FIXEDBLOCKS)
   1657 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1658 	if (st->page_0_size)
   1659 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1660 
   1661 	/*
   1662 	 * do the command
   1663 	 */
   1664 	return (scsipi_command(periph, (struct scsipi_generic *)&cmd,
   1665 	    sizeof(cmd), (u_char *)&scsi_select, scsi_select_len,
   1666 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT));
   1667 }
   1668 
   1669 int
   1670 st_cmprss(st, onoff)
   1671 	struct st_softc *st;
   1672 	int onoff;
   1673 {
   1674 	u_int scsi_dlen;
   1675 	struct scsi_mode_select mcmd;
   1676 	struct scsi_mode_sense scmd;
   1677 	struct scsi_select {
   1678 		struct scsi_mode_header header;
   1679 		struct scsi_blk_desc blk_desc;
   1680 		u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page),
   1681 		    sizeof(struct scsi_tape_dev_compression_page))];
   1682 	} scsi_pdata;
   1683 	struct scsi_tape_dev_conf_page *ptr;
   1684 	struct scsi_tape_dev_compression_page *cptr;
   1685 	struct scsipi_periph *periph = st->sc_periph;
   1686 	int error, ison, flags;
   1687 
   1688 	scsi_dlen = sizeof(scsi_pdata);
   1689 	bzero(&scsi_pdata, scsi_dlen);
   1690 
   1691 	/*
   1692 	 * Set up for a mode sense.
   1693 	 * Do DATA COMPRESSION page first.
   1694 	 */
   1695 	bzero(&scmd, sizeof(scmd));
   1696 	scmd.opcode = SCSI_MODE_SENSE;
   1697 	scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
   1698 	scmd.length = scsi_dlen;
   1699 
   1700 	flags = XS_CTL_SILENT;
   1701 
   1702 	/*
   1703 	 * Do the MODE SENSE command...
   1704 	 */
   1705 again:
   1706 	bzero(&scsi_pdata, scsi_dlen);
   1707 	error = scsipi_command(periph,
   1708 	    (struct scsipi_generic *)&scmd, sizeof(scmd),
   1709 	    (u_char *)&scsi_pdata, scsi_dlen,
   1710 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1711 
   1712 	if (error) {
   1713 		if (scmd.byte2 != SMS_DBD) {
   1714 			scmd.byte2 = SMS_DBD;
   1715 			goto again;
   1716 		}
   1717 		/*
   1718 		 * Try a different page?
   1719 		 */
   1720 		if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
   1721 			scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1722 			scmd.byte2 = 0;
   1723 			goto again;
   1724 		}
   1725 		return (error);
   1726 	}
   1727 
   1728 	if (scsi_pdata.header.blk_desc_len)
   1729 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
   1730 	else
   1731 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
   1732 
   1733 	if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
   1734 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
   1735 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
   1736 		if (onoff)
   1737 			cptr->dce_dcc |= DCP_DCE;
   1738 		else
   1739 			cptr->dce_dcc &= ~DCP_DCE;
   1740 		cptr->pagecode &= ~0x80;
   1741 	} else {
   1742 		ison =  (ptr->sel_comp_alg != 0);
   1743 		if (onoff)
   1744 			ptr->sel_comp_alg = 1;
   1745 		else
   1746 			ptr->sel_comp_alg = 0;
   1747 		ptr->pagecode &= ~0x80;
   1748 		ptr->byte2 = 0;
   1749 		ptr->active_partition = 0;
   1750 		ptr->wb_full_ratio = 0;
   1751 		ptr->rb_empty_ratio = 0;
   1752 		ptr->byte8 &= ~0x30;
   1753 		ptr->gap_size = 0;
   1754 		ptr->byte10 &= ~0xe7;
   1755 		ptr->ew_bufsize[0] = 0;
   1756 		ptr->ew_bufsize[1] = 0;
   1757 		ptr->ew_bufsize[2] = 0;
   1758 		ptr->reserved = 0;
   1759 	}
   1760 	onoff = onoff ? 1 : 0;
   1761 	/*
   1762 	 * There might be a virtue in actually doing the MODE SELECTS,
   1763 	 * but let's not clog the bus over it.
   1764 	 */
   1765 	if (onoff == ison)
   1766 		return (0);
   1767 
   1768 	/*
   1769 	 * Set up for a mode select
   1770 	 */
   1771 
   1772 	scsi_pdata.header.data_length = 0;
   1773 	scsi_pdata.header.medium_type = 0;
   1774 	if ((st->flags & ST_DONTBUFFER) == 0)
   1775 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
   1776 	else
   1777 		scsi_pdata.header.dev_spec = 0;
   1778 
   1779 	bzero(&mcmd, sizeof(mcmd));
   1780 	mcmd.opcode = SCSI_MODE_SELECT;
   1781 	mcmd.byte2 = SMS_PF;
   1782 	mcmd.length = scsi_dlen;
   1783 	if (scsi_pdata.header.blk_desc_len) {
   1784 		scsi_pdata.blk_desc.density = 0;
   1785 		scsi_pdata.blk_desc.nblocks[0] = 0;
   1786 		scsi_pdata.blk_desc.nblocks[1] = 0;
   1787 		scsi_pdata.blk_desc.nblocks[2] = 0;
   1788 	}
   1789 
   1790 	/*
   1791 	 * Do the command
   1792 	 */
   1793 	error = scsipi_command(periph,
   1794 	    (struct scsipi_generic *)&mcmd, sizeof(mcmd),
   1795 	    (u_char *)&scsi_pdata, scsi_dlen,
   1796 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT);
   1797 
   1798 	if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
   1799 		/*
   1800 		 * Try DEVICE CONFIGURATION page.
   1801 		 */
   1802 		scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1803 		goto again;
   1804 	}
   1805 	return (error);
   1806 }
   1807 /*
   1808  * issue an erase command
   1809  */
   1810 int
   1811 st_erase(st, full, flags)
   1812 	struct st_softc *st;
   1813 	int full, flags;
   1814 {
   1815 	int tmo;
   1816 	struct scsi_erase cmd;
   1817 
   1818 	/*
   1819 	 * Full erase means set LONG bit in erase command, which asks
   1820 	 * the drive to erase the entire unit.  Without this bit, we're
   1821 	 * asking the drive to write an erase gap.
   1822 	 */
   1823 	bzero(&cmd, sizeof(cmd));
   1824 	cmd.opcode = ERASE;
   1825 	if (full) {
   1826 		cmd.byte2 = SE_IMMED|SE_LONG;
   1827 		tmo = ST_SPC_TIME;
   1828 	} else {
   1829 		tmo = ST_IO_TIME;
   1830 		cmd.byte2 = SE_IMMED;
   1831 	}
   1832 
   1833 	/*
   1834 	 * XXX We always do this asynchronously, for now.  How long should
   1835 	 * we wait if we want to (eventually) to it synchronously?
   1836 	 */
   1837 	return (scsipi_command(st->sc_periph,
   1838 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1839 	    0, 0, ST_RETRIES, tmo, NULL, flags));
   1840 }
   1841 
   1842 /*
   1843  * skip N blocks/filemarks/seq filemarks/eom
   1844  */
   1845 int
   1846 st_space(st, number, what, flags)
   1847 	struct st_softc *st;
   1848 	u_int what;
   1849 	int flags;
   1850 	int number;
   1851 {
   1852 	struct scsi_space cmd;
   1853 	int error;
   1854 
   1855 	switch (what) {
   1856 	case SP_BLKS:
   1857 		if (st->flags & ST_PER_ACTION) {
   1858 			if (number > 0) {
   1859 				st->flags &= ~ST_PER_ACTION;
   1860 				return (EIO);
   1861 			} else if (number < 0) {
   1862 				if (st->flags & ST_AT_FILEMARK) {
   1863 					/*
   1864 					 * Handling of ST_AT_FILEMARK
   1865 					 * in st_space will fill in the
   1866 					 * right file mark count.
   1867 					 */
   1868 					error = st_space(st, 0, SP_FILEMARKS,
   1869 					    flags);
   1870 					if (error)
   1871 						return (error);
   1872 				}
   1873 				if (st->flags & ST_BLANK_READ) {
   1874 					st->flags &= ~ST_BLANK_READ;
   1875 					return (EIO);
   1876 				}
   1877 				st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
   1878 			}
   1879 		}
   1880 		break;
   1881 	case SP_FILEMARKS:
   1882 		if (st->flags & ST_EIO_PENDING) {
   1883 			if (number > 0) {
   1884 				/* pretend we just discovered the error */
   1885 				st->flags &= ~ST_EIO_PENDING;
   1886 				return (EIO);
   1887 			} else if (number < 0) {
   1888 				/* back away from the error */
   1889 				st->flags &= ~ST_EIO_PENDING;
   1890 			}
   1891 		}
   1892 		if (st->flags & ST_AT_FILEMARK) {
   1893 			st->flags &= ~ST_AT_FILEMARK;
   1894 			number--;
   1895 		}
   1896 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1897 			/* back away from unwritten tape */
   1898 			st->flags &= ~ST_BLANK_READ;
   1899 			number++;	/* XXX dubious */
   1900 		}
   1901 		break;
   1902 	case SP_EOM:
   1903 		if (st->flags & ST_EOM_PENDING) {
   1904 			/* we're already there */
   1905 			st->flags &= ~ST_EOM_PENDING;
   1906 			return (0);
   1907 		}
   1908 		if (st->flags & ST_EIO_PENDING) {
   1909 			/* pretend we just discovered the error */
   1910 			st->flags &= ~ST_EIO_PENDING;
   1911 			return (EIO);
   1912 		}
   1913 		if (st->flags & ST_AT_FILEMARK)
   1914 			st->flags &= ~ST_AT_FILEMARK;
   1915 		break;
   1916 	}
   1917 	if (number == 0)
   1918 		return (0);
   1919 
   1920 	bzero(&cmd, sizeof(cmd));
   1921 	cmd.opcode = SPACE;
   1922 	cmd.byte2 = what;
   1923 	_lto3b(number, cmd.number);
   1924 
   1925 	return (scsipi_command(st->sc_periph,
   1926 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1927 	    0, 0, 0, ST_SPC_TIME, NULL, flags));
   1928 }
   1929 
   1930 /*
   1931  * write N filemarks
   1932  */
   1933 int
   1934 st_write_filemarks(st, number, flags)
   1935 	struct st_softc *st;
   1936 	int flags;
   1937 	int number;
   1938 {
   1939 	struct scsi_write_filemarks cmd;
   1940 
   1941 	/*
   1942 	 * It's hard to write a negative number of file marks.
   1943 	 * Don't try.
   1944 	 */
   1945 	if (number < 0)
   1946 		return (EINVAL);
   1947 	switch (number) {
   1948 	case 0:		/* really a command to sync the drive's buffers */
   1949 		break;
   1950 	case 1:
   1951 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1952 			st->flags &= ~ST_WRITTEN;
   1953 		else
   1954 			st->flags |= ST_FM_WRITTEN;
   1955 		st->flags &= ~ST_PER_ACTION;
   1956 		break;
   1957 	default:
   1958 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1959 	}
   1960 
   1961 	bzero(&cmd, sizeof(cmd));
   1962 	cmd.opcode = WRITE_FILEMARKS;
   1963 	_lto3b(number, cmd.number);
   1964 
   1965 	return (scsipi_command(st->sc_periph,
   1966 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1967 	    0, 0, 0, ST_IO_TIME * 4, NULL, flags));
   1968 }
   1969 
   1970 /*
   1971  * Make sure the right number of file marks is on tape if the
   1972  * tape has been written.  If the position argument is true,
   1973  * leave the tape positioned where it was originally.
   1974  *
   1975  * nmarks returns the number of marks to skip (or, if position
   1976  * true, which were skipped) to get back original position.
   1977  */
   1978 int
   1979 st_check_eod(st, position, nmarks, flags)
   1980 	struct st_softc *st;
   1981 	boolean position;
   1982 	int *nmarks;
   1983 	int flags;
   1984 {
   1985 	int error;
   1986 
   1987 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1988 	default:
   1989 		*nmarks = 0;
   1990 		return (0);
   1991 	case ST_WRITTEN:
   1992 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1993 		*nmarks = 1;
   1994 		break;
   1995 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1996 		*nmarks = 2;
   1997 	}
   1998 	error = st_write_filemarks(st, *nmarks, flags);
   1999 	if (position && !error)
   2000 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   2001 	return (error);
   2002 }
   2003 
   2004 /*
   2005  * load/unload/retension
   2006  */
   2007 int
   2008 st_load(st, type, flags)
   2009 	struct st_softc *st;
   2010 	u_int type;
   2011 	int flags;
   2012 {
   2013 	int error;
   2014 	struct scsi_load cmd;
   2015 
   2016 	if (type != LD_LOAD) {
   2017 		int nmarks;
   2018 
   2019 		error = st_check_eod(st, FALSE, &nmarks, flags);
   2020 		if (error) {
   2021 			printf("%s: failed to write closing filemarks at "
   2022 			    "unload, errno=%d\n", st->sc_dev.dv_xname, error);
   2023 			return (error);
   2024 		}
   2025 	}
   2026 	if (st->quirks & ST_Q_IGNORE_LOADS) {
   2027 		if (type == LD_LOAD) {
   2028 			/*
   2029 			 * If we ignore loads, at least we should try a rewind.
   2030 			 */
   2031 			return st_rewind(st, 0, flags);
   2032 		}
   2033 		/* otherwise, we should do what's asked of us */
   2034 	}
   2035 
   2036 	bzero(&cmd, sizeof(cmd));
   2037 	cmd.opcode = LOAD;
   2038 	cmd.how = type;
   2039 
   2040 	error = scsipi_command(st->sc_periph,
   2041 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   2042 	    0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
   2043 	if (error) {
   2044 		printf("%s: error %d in st_load (op %d)\n",
   2045 		    st->sc_dev.dv_xname, error, type);
   2046 	}
   2047 	return (error);
   2048 }
   2049 
   2050 /*
   2051  *  Rewind the device
   2052  */
   2053 int
   2054 st_rewind(st, immediate, flags)
   2055 	struct st_softc *st;
   2056 	u_int immediate;
   2057 	int flags;
   2058 {
   2059 	struct scsi_rewind cmd;
   2060 	int error;
   2061 	int nmarks;
   2062 
   2063 	error = st_check_eod(st, FALSE, &nmarks, flags);
   2064 	if (error) {
   2065 		printf("%s: failed to write closing filemarks at "
   2066 		    "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
   2067 		return (error);
   2068 	}
   2069 	st->flags &= ~ST_PER_ACTION;
   2070 
   2071 	bzero(&cmd, sizeof(cmd));
   2072 	cmd.opcode = REWIND;
   2073 	cmd.byte2 = immediate;
   2074 
   2075 	error = scsipi_command(st->sc_periph,
   2076 	    (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
   2077 	    immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
   2078 	if (error) {
   2079 		printf("%s: error %d trying to rewind\n",
   2080 		    st->sc_dev.dv_xname, error);
   2081 	}
   2082 	return (error);
   2083 }
   2084 
   2085 int
   2086 st_rdpos(st, hard, blkptr)
   2087 	struct st_softc *st;
   2088 	int hard;
   2089 	u_int32_t *blkptr;
   2090 {
   2091 	int error;
   2092 	u_int8_t posdata[20];
   2093 	struct scsi_tape_read_position cmd;
   2094 
   2095 	/*
   2096 	 * First flush any pending writes...
   2097 	 */
   2098 	error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2099 
   2100 	/*
   2101 	 * The latter case is for 'write protected' tapes
   2102 	 * which are too stupid to recognize a zero count
   2103 	 * for writing filemarks as a no-op.
   2104 	 */
   2105 	if (error != 0 && error != EACCES && error != EROFS)
   2106 		return (error);
   2107 
   2108 	bzero(&cmd, sizeof(cmd));
   2109 	bzero(&posdata, sizeof(posdata));
   2110 	cmd.opcode = READ_POSITION;
   2111 	if (hard)
   2112 		cmd.byte1 = 1;
   2113 
   2114 	error = scsipi_command(st->sc_periph,
   2115 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
   2116 	    sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
   2117 	    XS_CTL_SILENT | XS_CTL_DATA_IN);
   2118 
   2119 	if (error == 0) {
   2120 #if	0
   2121 		printf("posdata:");
   2122 		for (hard = 0; hard < sizeof(posdata); hard++)
   2123 			printf("%02x ", posdata[hard] & 0xff);
   2124 		printf("\n");
   2125 #endif
   2126 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   2127 			error = EINVAL;
   2128 		else
   2129 			*blkptr = _4btol(&posdata[4]);
   2130 	}
   2131 	return (error);
   2132 }
   2133 
   2134 int
   2135 st_setpos(st, hard, blkptr)
   2136 	struct st_softc *st;
   2137 	int hard;
   2138 	u_int32_t *blkptr;
   2139 {
   2140 	int error;
   2141 	struct scsi_tape_locate cmd;
   2142 
   2143 	/*
   2144 	 * First flush any pending writes. Strictly speaking,
   2145 	 * we're not supposed to have to worry about this,
   2146 	 * but let's be untrusting.
   2147 	 */
   2148 	error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2149 
   2150 	/*
   2151 	 * The latter case is for 'write protected' tapes
   2152 	 * which are too stupid to recognize a zero count
   2153 	 * for writing filemarks as a no-op.
   2154 	 */
   2155 	if (error != 0 && error != EACCES && error != EROFS)
   2156 		return (error);
   2157 
   2158 	bzero(&cmd, sizeof(cmd));
   2159 	cmd.opcode = LOCATE;
   2160 	if (hard)
   2161 		cmd.byte2 = 1 << 2;
   2162 	_lto4b(*blkptr, cmd.blkaddr);
   2163 	error = scsipi_command(st->sc_periph,
   2164 		(struct scsipi_generic *)&cmd, sizeof(cmd),
   2165 		NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
   2166 	/*
   2167 	 * XXX: Note file && block number position now unknown (if
   2168 	 * XXX: these things ever start being maintained in this driver)
   2169 	 */
   2170 	return (error);
   2171 }
   2172 
   2173 
   2174 /*
   2175  * Look at the returned sense and act on the error and determine
   2176  * the unix error number to pass back..., 0 (== report no error),
   2177  * -1 = retry the operation, -2 continue error processing.
   2178  */
   2179 int
   2180 st_interpret_sense(xs)
   2181 	struct scsipi_xfer *xs;
   2182 {
   2183 	struct scsipi_periph *periph = xs->xs_periph;
   2184 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   2185 	struct buf *bp = xs->bp;
   2186 	struct st_softc *st = (void *)periph->periph_dev;
   2187 	int retval = EJUSTRETURN;
   2188 	int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
   2189 	u_int8_t key;
   2190 	int32_t info;
   2191 
   2192 	/*
   2193 	 * If it isn't a extended or extended/deferred error, let
   2194 	 * the generic code handle it.
   2195 	 */
   2196 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
   2197 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFFERRED */
   2198 		return (retval);
   2199 	}
   2200 
   2201 	if (sense->error_code & SSD_ERRCODE_VALID)
   2202 		info = _4btol(sense->info);
   2203 	else
   2204 		info = xs->datalen;	/* bad choice if fixed blocks */
   2205 	key = sense->flags & SSD_KEY;
   2206 	st->mt_erreg = key;
   2207 	st->mt_resid = (short) info;
   2208 
   2209 	/*
   2210 	 * If the device is not open yet, let generic handle
   2211 	 */
   2212 	if ((periph->periph_flags & PERIPH_OPEN) == 0) {
   2213 		return (retval);
   2214 	}
   2215 
   2216 
   2217 	if (st->flags & ST_FIXEDBLOCKS) {
   2218 		xs->resid = info * st->blksize;
   2219 		if (sense->flags & SSD_EOM) {
   2220 			if ((st->flags & ST_EARLYWARN) == 0)
   2221 				st->flags |= ST_EIO_PENDING;
   2222 			st->flags |= ST_EOM_PENDING;
   2223 			if (bp)
   2224 				bp->b_resid = xs->resid;
   2225 		}
   2226 		if (sense->flags & SSD_FILEMARK) {
   2227 			st->flags |= ST_AT_FILEMARK;
   2228 			if (bp)
   2229 				bp->b_resid = xs->resid;
   2230 		}
   2231 		if (sense->flags & SSD_ILI) {
   2232 			st->flags |= ST_EIO_PENDING;
   2233 			if (bp)
   2234 				bp->b_resid = xs->resid;
   2235 			if (sense->error_code & SSD_ERRCODE_VALID &&
   2236 			    (xs->xs_control & XS_CTL_SILENT) == 0)
   2237 				printf("%s: block wrong size, %d blocks "
   2238 				    "residual\n", st->sc_dev.dv_xname, info);
   2239 
   2240 			/*
   2241 			 * This quirk code helps the drive read
   2242 			 * the first tape block, regardless of
   2243 			 * format.  That is required for these
   2244 			 * drives to return proper MODE SENSE
   2245 			 * information.
   2246 			 */
   2247 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   2248 			    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   2249 				st->blksize -= 512;
   2250 		}
   2251 		/*
   2252 		 * If data wanted and no data was tranfered, do it immediatly
   2253 		 */
   2254 		if (xs->datalen && xs->resid >= xs->datalen) {
   2255 			if (st->flags & ST_EIO_PENDING)
   2256 				return (EIO);
   2257 			if (st->flags & ST_AT_FILEMARK) {
   2258 				if (bp)
   2259 					bp->b_resid = xs->resid;
   2260 				return (0);
   2261 			}
   2262 		}
   2263 	} else {		/* must be variable mode */
   2264 		if (sense->flags & SSD_EOM) {
   2265 			/*
   2266 			 * The current semantics of this
   2267 			 * driver requires EOM detection
   2268 			 * to return EIO unless early
   2269 			 * warning detection is enabled
   2270 			 * for variable mode (this is always
   2271 			 * on for fixed block mode).
   2272 			 */
   2273 			if (st->flags & ST_EARLYWARN) {
   2274 				st->flags |= ST_EOM_PENDING;
   2275 				retval = 0;
   2276 			} else {
   2277 				retval = EIO;
   2278 			}
   2279 
   2280 			/*
   2281 			 * If it's an unadorned EOM detection,
   2282 			 * suppress printing an error.
   2283 			 */
   2284 			if (key == SKEY_NO_SENSE) {
   2285 				doprint = 0;
   2286 			}
   2287 		} else if (sense->flags & SSD_FILEMARK) {
   2288 			retval = 0;
   2289 		} else if (sense->flags & SSD_ILI) {
   2290 			if (info < 0) {
   2291 				/*
   2292 				 * The tape record was bigger than the read
   2293 				 * we issued.
   2294 				 */
   2295 				if ((xs->xs_control & XS_CTL_SILENT) == 0) {
   2296 					printf("%s: %d-byte tape record too big"
   2297 					    " for %d-byte user buffer\n",
   2298 					    st->sc_dev.dv_xname,
   2299 					    xs->datalen - info, xs->datalen);
   2300 				}
   2301 				retval = EIO;
   2302 			} else {
   2303 				retval = 0;
   2304 			}
   2305 		}
   2306 		xs->resid = info;
   2307 		if (bp)
   2308 			bp->b_resid = info;
   2309 	}
   2310 
   2311 #ifdef SCSIPI_DEBUG
   2312 	if (retval == 0 && key == SKEY_NO_SENSE)
   2313 		doprint = 0;
   2314 #endif
   2315 	if (key == SKEY_BLANK_CHECK) {
   2316 		/*
   2317 		 * This quirk code helps the drive read the
   2318 		 * first tape block, regardless of format.  That
   2319 		 * is required for these drives to return proper
   2320 		 * MODE SENSE information.
   2321 		 */
   2322 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2323 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   2324 			/* still starting */
   2325 			st->blksize -= 512;
   2326 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2327 			st->flags |= ST_BLANK_READ;
   2328 			xs->resid = xs->datalen;
   2329 			if (bp) {
   2330 				bp->b_resid = xs->resid;
   2331 				/* return an EOF */
   2332 			}
   2333 			retval = 0;
   2334 		}
   2335 	}
   2336 #ifdef	SCSIVERBOSE
   2337 	if (doprint) {
   2338 		scsipi_print_sense(xs, 0);
   2339 	}
   2340 #else
   2341 	if (doprint) {
   2342 		scsipi_printaddr(periph);
   2343 		printf("Sense Key 0x%02x", key);
   2344 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
   2345 			switch (key) {
   2346 			case SKEY_NOT_READY:
   2347 			case SKEY_ILLEGAL_REQUEST:
   2348 			case SKEY_UNIT_ATTENTION:
   2349 			case SKEY_WRITE_PROTECT:
   2350 				break;
   2351 			case SKEY_BLANK_CHECK:
   2352 				printf(", requested size: %d (decimal)", info);
   2353 				break;
   2354 			case SKEY_ABORTED_COMMAND:
   2355 				if (xs->retries)
   2356 					printf(", retrying");
   2357 				printf(", cmd 0x%x, info 0x%x",
   2358 				    xs->cmd->opcode, info);
   2359 				break;
   2360 			default:
   2361 				printf(", info = %d (decimal)", info);
   2362 			}
   2363 		}
   2364 		if (sense->extra_len != 0) {
   2365 			int n;
   2366 			printf(", data =");
   2367 			for (n = 0; n < sense->extra_len; n++)
   2368 				printf(" %02x", sense->cmd_spec_info[n]);
   2369 		}
   2370 		printf("\n");
   2371 	}
   2372 #endif
   2373 	return (retval);
   2374 }
   2375 
   2376 /*
   2377  * The quirk here is that the drive returns some value to st_mode_sense
   2378  * incorrectly until the tape has actually passed by the head.
   2379  *
   2380  * The method is to set the drive to large fixed-block state (user-specified
   2381  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2382  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2383  * work, as a last resort, try variable- length blocks.  The result will be
   2384  * the ability to do an accurate st_mode_sense.
   2385  *
   2386  * We know we can do a rewind because we just did a load, which implies rewind.
   2387  * Rewind seems preferable to space backward if we have a virgin tape.
   2388  *
   2389  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2390  * error processing, both part of st_interpret_sense.
   2391  */
   2392 int
   2393 st_touch_tape(st)
   2394 	struct st_softc *st;
   2395 {
   2396 	char *buf;
   2397 	int readsize;
   2398 	int error;
   2399 
   2400 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   2401 	if (buf == NULL)
   2402 		return (ENOMEM);
   2403 
   2404 	if ((error = st_mode_sense(st, 0)) != 0)
   2405 		goto bad;
   2406 	st->blksize = 1024;
   2407 	do {
   2408 		switch (st->blksize) {
   2409 		case 512:
   2410 		case 1024:
   2411 			readsize = st->blksize;
   2412 			st->flags |= ST_FIXEDBLOCKS;
   2413 			break;
   2414 		default:
   2415 			readsize = 1;
   2416 			st->flags &= ~ST_FIXEDBLOCKS;
   2417 		}
   2418 		if ((error = st_mode_select(st, 0)) != 0)
   2419 			goto bad;
   2420 		st_read(st, buf, readsize, XS_CTL_SILENT);	/* XXX */
   2421 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2422 bad:			free(buf, M_TEMP);
   2423 			return (error);
   2424 		}
   2425 	} while (readsize != 1 && readsize > st->blksize);
   2426 
   2427 	free(buf, M_TEMP);
   2428 	return (0);
   2429 }
   2430 
   2431 int
   2432 stdump(dev, blkno, va, size)
   2433 	dev_t dev;
   2434 	daddr_t blkno;
   2435 	caddr_t va;
   2436 	size_t size;
   2437 {
   2438 
   2439 	/* Not implemented. */
   2440 	return (ENXIO);
   2441 }
   2442