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