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