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