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