Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.117
      1 /*	$NetBSD: st.c,v 1.117 2000/01/21 23:40:00 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Originally written by Julian Elischer (julian (at) tfs.com)
     41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     42  *
     43  * TRW Financial Systems, in accordance with their agreement with Carnegie
     44  * Mellon University, makes this software available to CMU to distribute
     45  * or use in any manner that they see fit as long as this message is kept with
     46  * the software. For this reason TFS also grants any other persons or
     47  * organisations permission to use or modify this software.
     48  *
     49  * TFS supplies this software to be publicly redistributed
     50  * on the understanding that TFS is not responsible for the correct
     51  * functioning of this software in any circumstances.
     52  *
     53  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     54  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     55  *
     56  * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
     57  */
     58 
     59 #include "opt_scsi.h"
     60 #include "rnd.h"
     61 
     62 #include <sys/types.h>
     63 #include <sys/param.h>
     64 #include <sys/systm.h>
     65 #include <sys/fcntl.h>
     66 #include <sys/errno.h>
     67 #include <sys/ioctl.h>
     68 #include <sys/malloc.h>
     69 #include <sys/buf.h>
     70 #include <sys/proc.h>
     71 #include <sys/user.h>
     72 #include <sys/mtio.h>
     73 #include <sys/device.h>
     74 #include <sys/conf.h>
     75 #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 		st->flags |= ST_FIXEDBLOCKS;
   1007 		if (st->media_blksize > 0)
   1008 			st->blksize = st->media_blksize;
   1009 		else
   1010 			st->blksize = DEF_FIXED_BSIZE;
   1011 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
   1012 		goto done;
   1013 	}
   1014 	/*
   1015 	 * If we're about to read the tape, perhaps we should choose
   1016 	 * fixed or variable-length blocks and block size according to
   1017 	 * what the drive found on the tape.
   1018 	 */
   1019 	if (first_read &&
   1020 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
   1021 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
   1022 	    (st->media_blksize == 1024))) {
   1023 		if (st->media_blksize > 0)
   1024 			st->flags |= ST_FIXEDBLOCKS;
   1025 		else
   1026 			st->flags &= ~ST_FIXEDBLOCKS;
   1027 		st->blksize = st->media_blksize;
   1028 		SC_DEBUG(sc_link, SDEV_DB3,
   1029 		    ("Used media_blksize of %d\n", st->media_blksize));
   1030 		goto done;
   1031 	}
   1032 	/*
   1033 	 * We're getting no hints from any direction.  Choose variable-
   1034 	 * length blocks arbitrarily.
   1035 	 */
   1036 	st->flags &= ~ST_FIXEDBLOCKS;
   1037 	st->blksize = 0;
   1038 	SC_DEBUG(sc_link, SDEV_DB3,
   1039 	    ("Give up and default to variable mode\n"));
   1040 
   1041 done:
   1042 	/*
   1043 	 * Decide whether or not to write two file marks to signify end-
   1044 	 * of-data.  Make the decision as a function of density.  If
   1045 	 * the decision is not to use a second file mark, the SCSI BLANK
   1046 	 * CHECK condition code will be recognized as end-of-data when
   1047 	 * first read.
   1048 	 * (I think this should be a by-product of fixed/variable..julian)
   1049 	 */
   1050 	switch (st->density) {
   1051 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
   1052 	case QIC_11:
   1053 	case QIC_24:
   1054 	case QIC_120:
   1055 	case QIC_150:
   1056 	case QIC_525:
   1057 	case QIC_1320:
   1058 		st->flags &= ~ST_2FM_AT_EOD;
   1059 		break;
   1060 	default:
   1061 		st->flags |= ST_2FM_AT_EOD;
   1062 	}
   1063 	return (0);
   1064 }
   1065 
   1066 /*
   1067  * Actually translate the requested transfer into
   1068  * one the physical driver can understand
   1069  * The transfer is described by a buf and will include
   1070  * only one physical transfer.
   1071  */
   1072 void
   1073 ststrategy(bp)
   1074 	struct buf *bp;
   1075 {
   1076 	struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
   1077 	int s;
   1078 
   1079 	SC_DEBUG(st->sc_link, SDEV_DB1,
   1080 	    ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
   1081 	/*
   1082 	 * If it's a null transfer, return immediatly
   1083 	 */
   1084 	if (bp->b_bcount == 0)
   1085 		goto done;
   1086 
   1087 	/* If offset is negative, error */
   1088 	if (bp->b_blkno < 0) {
   1089 		bp->b_error = EINVAL;
   1090 		goto bad;
   1091 	}
   1092 
   1093 	/*
   1094 	 * Odd sized request on fixed drives are verboten
   1095 	 */
   1096 	if (st->flags & ST_FIXEDBLOCKS) {
   1097 		if (bp->b_bcount % st->blksize) {
   1098 			printf("%s: bad request, must be multiple of %d\n",
   1099 			    st->sc_dev.dv_xname, st->blksize);
   1100 			bp->b_error = EIO;
   1101 			goto bad;
   1102 		}
   1103 	}
   1104 	/*
   1105 	 * as are out-of-range requests on variable drives.
   1106 	 */
   1107 	else if (bp->b_bcount < st->blkmin ||
   1108 	    (st->blkmax && bp->b_bcount > st->blkmax)) {
   1109 		printf("%s: bad request, must be between %d and %d\n",
   1110 		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
   1111 		bp->b_error = EIO;
   1112 		goto bad;
   1113 	}
   1114 	s = splbio();
   1115 
   1116 	/*
   1117 	 * Place it in the queue of activities for this tape
   1118 	 * at the end (a bit silly because we only have on user..
   1119 	 * (but it could fork()))
   1120 	 */
   1121 	BUFQ_INSERT_TAIL(&st->buf_queue, bp);
   1122 
   1123 	/*
   1124 	 * Tell the device to get going on the transfer if it's
   1125 	 * not doing anything, otherwise just wait for completion
   1126 	 * (All a bit silly if we're only allowing 1 open but..)
   1127 	 */
   1128 	ststart(st);
   1129 
   1130 	splx(s);
   1131 	return;
   1132 bad:
   1133 	bp->b_flags |= B_ERROR;
   1134 done:
   1135 	/*
   1136 	 * Correctly set the buf to indicate a completed xfer
   1137 	 */
   1138 	bp->b_resid = bp->b_bcount;
   1139 	biodone(bp);
   1140 	return;
   1141 }
   1142 
   1143 /*
   1144  * ststart looks to see if there is a buf waiting for the device
   1145  * and that the device is not already busy. If both are true,
   1146  * It dequeues the buf and creates a scsi command to perform the
   1147  * transfer required. The transfer request will call scsipi_done
   1148  * on completion, which will in turn call this routine again
   1149  * so that the next queued transfer is performed.
   1150  * The bufs are queued by the strategy routine (ststrategy)
   1151  *
   1152  * This routine is also called after other non-queued requests
   1153  * have been made of the scsi driver, to ensure that the queue
   1154  * continues to be drained.
   1155  * ststart() is called at splbio
   1156  */
   1157 void
   1158 ststart(v)
   1159 	void *v;
   1160 {
   1161 	struct st_softc *st = v;
   1162 	struct scsipi_link *sc_link = st->sc_link;
   1163 	register struct buf *bp;
   1164 	struct scsi_rw_tape cmd;
   1165 	int flags, error;
   1166 
   1167 	SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
   1168 	/*
   1169 	 * See if there is a buf to do and we are not already
   1170 	 * doing one
   1171 	 */
   1172 	while (sc_link->active < sc_link->openings) {
   1173 		/* if a special awaits, let it proceed first */
   1174 		if (sc_link->flags & SDEV_WAITING) {
   1175 			sc_link->flags &= ~SDEV_WAITING;
   1176 			wakeup((caddr_t)sc_link);
   1177 			return;
   1178 		}
   1179 
   1180 		if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
   1181 			return;
   1182 		BUFQ_REMOVE(&st->buf_queue, bp);
   1183 
   1184 		/*
   1185 		 * if the device has been unmounted bye the user
   1186 		 * then throw away all requests until done
   1187 		 */
   1188 		if (!(st->flags & ST_MOUNTED) ||
   1189 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   1190 			/* make sure that one implies the other.. */
   1191 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
   1192 			bp->b_flags |= B_ERROR;
   1193 			bp->b_error = EIO;
   1194 			bp->b_resid = bp->b_bcount;
   1195 			biodone(bp);
   1196 			continue;
   1197 		}
   1198 		/*
   1199 		 * only FIXEDBLOCK devices have pending I/O or space operations.
   1200 		 */
   1201 		if (st->flags & ST_FIXEDBLOCKS) {
   1202 			/*
   1203 			 * If we are at a filemark but have not reported it yet
   1204 			 * then we should report it now
   1205 			 */
   1206 			if (st->flags & ST_AT_FILEMARK) {
   1207 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1208 					/*
   1209 					 * Handling of ST_AT_FILEMARK in
   1210 					 * st_space will fill in the right file
   1211 					 * mark count.
   1212 					 * Back up over filemark
   1213 					 */
   1214 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
   1215 						bp->b_flags |= B_ERROR;
   1216 						bp->b_error = EIO;
   1217 						biodone(bp);
   1218 						continue;
   1219 					}
   1220 				} else {
   1221 					bp->b_resid = bp->b_bcount;
   1222 					bp->b_error = 0;
   1223 					bp->b_flags &= ~B_ERROR;
   1224 					st->flags &= ~ST_AT_FILEMARK;
   1225 					biodone(bp);
   1226 					continue;	/* seek more work */
   1227 				}
   1228 			}
   1229 		}
   1230 		/*
   1231 		 * If we are at EOM but have not reported it
   1232 		 * yet then we should report it now.
   1233 		 */
   1234 		if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
   1235 			bp->b_resid = bp->b_bcount;
   1236 			if (st->flags & ST_EIO_PENDING) {
   1237 				bp->b_error = EIO;
   1238 				bp->b_flags |= B_ERROR;
   1239 			}
   1240 			st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
   1241 			biodone(bp);
   1242 			continue;	/* seek more work */
   1243 		}
   1244 
   1245 		/*
   1246 		 *  Fill out the scsi command
   1247 		 */
   1248 		bzero(&cmd, sizeof(cmd));
   1249 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1250 			cmd.opcode = WRITE;
   1251 			st->flags &= ~ST_FM_WRITTEN;
   1252 			flags = XS_CTL_DATA_OUT;
   1253 		} else {
   1254 			cmd.opcode = READ;
   1255 			flags = XS_CTL_DATA_IN;
   1256 		}
   1257 
   1258 		/*
   1259 		 * Handle "fixed-block-mode" tape drives by using the
   1260 		 * block count instead of the length.
   1261 		 */
   1262 		if (st->flags & ST_FIXEDBLOCKS) {
   1263 			cmd.byte2 |= SRW_FIXED;
   1264 			_lto3b(bp->b_bcount / st->blksize, cmd.len);
   1265 		} else
   1266 			_lto3b(bp->b_bcount, cmd.len);
   1267 
   1268 		/*
   1269 		 * go ask the adapter to do all this for us
   1270 		 * XXX Really need NOSLEEP?
   1271 		 */
   1272 		error = scsipi_command(sc_link,
   1273 		    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1274 		    (u_char *)bp->b_data, bp->b_bcount,
   1275 		    0, ST_IO_TIME, bp, flags | XS_CTL_NOSLEEP | XS_CTL_ASYNC);
   1276 		if (error) {
   1277 			printf("%s: not queued, error %d\n",
   1278 			    st->sc_dev.dv_xname, error);
   1279 		}
   1280 	} /* go back and see if we can cram more work in.. */
   1281 }
   1282 
   1283 void
   1284 stdone(xs)
   1285 	struct scsipi_xfer *xs;
   1286 {
   1287 
   1288 	if (xs->bp != NULL) {
   1289 		struct st_softc *st = xs->sc_link->device_softc;
   1290 		if ((xs->bp->b_flags & B_READ) == B_WRITE) {
   1291 			st->flags |= ST_WRITTEN;
   1292 		} else {
   1293 			st->flags &= ~ST_WRITTEN;
   1294 		}
   1295 #if NRND > 0
   1296 		rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
   1297 #endif
   1298 	}
   1299 }
   1300 
   1301 int
   1302 stread(dev, uio, iomode)
   1303 	dev_t dev;
   1304 	struct uio *uio;
   1305 	int iomode;
   1306 {
   1307 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1308 
   1309 	return (physio(ststrategy, NULL, dev, B_READ,
   1310 	    st->sc_link->adapter->scsipi_minphys, uio));
   1311 }
   1312 
   1313 int
   1314 stwrite(dev, uio, iomode)
   1315 	dev_t dev;
   1316 	struct uio *uio;
   1317 	int iomode;
   1318 {
   1319 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1320 
   1321 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1322 	    st->sc_link->adapter->scsipi_minphys, uio));
   1323 }
   1324 
   1325 /*
   1326  * Perform special action on behalf of the user;
   1327  * knows about the internals of this device
   1328  */
   1329 int
   1330 stioctl(dev, cmd, arg, flag, p)
   1331 	dev_t dev;
   1332 	u_long cmd;
   1333 	caddr_t arg;
   1334 	int flag;
   1335 	struct proc *p;
   1336 {
   1337 	int error = 0;
   1338 	int unit;
   1339 	int number, nmarks, dsty;
   1340 	int flags;
   1341 	struct st_softc *st;
   1342 	int hold_blksize;
   1343 	u_int8_t hold_density;
   1344 	struct mtop *mt = (struct mtop *) arg;
   1345 
   1346 	/*
   1347 	 * Find the device that the user is talking about
   1348 	 */
   1349 	flags = 0;		/* give error messages, act on errors etc. */
   1350 	unit = STUNIT(dev);
   1351 	dsty = STDSTY(dev);
   1352 	st = st_cd.cd_devs[unit];
   1353 	hold_blksize = st->blksize;
   1354 	hold_density = st->density;
   1355 
   1356 	switch ((u_int) cmd) {
   1357 
   1358 	case MTIOCGET: {
   1359 		struct mtget *g = (struct mtget *) arg;
   1360 		/*
   1361 		 * (to get the current state of READONLY)
   1362 		 */
   1363 		error = st_mode_sense(st, XS_CTL_SILENT);
   1364 		if (error)
   1365 			break;
   1366 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1367 		bzero(g, sizeof(struct mtget));
   1368 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1369 		g->mt_blksiz = st->blksize;
   1370 		g->mt_density = st->density;
   1371 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1372 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1373 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1374 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1375 		g->mt_mdensity[0] = st->modes[0].density;
   1376 		g->mt_mdensity[1] = st->modes[1].density;
   1377 		g->mt_mdensity[2] = st->modes[2].density;
   1378 		g->mt_mdensity[3] = st->modes[3].density;
   1379 		if (st->flags & ST_READONLY)
   1380 			g->mt_dsreg |= MT_DS_RDONLY;
   1381 		if (st->flags & ST_MOUNTED)
   1382 			g->mt_dsreg |= MT_DS_MOUNTED;
   1383 		g->mt_resid = st->mt_resid;
   1384 		g->mt_erreg = st->mt_erreg;
   1385 		/*
   1386 		 * clear latched errors.
   1387 		 */
   1388 		st->mt_resid = 0;
   1389 		st->mt_erreg = 0;
   1390 		st->asc = 0;
   1391 		st->ascq = 0;
   1392 		break;
   1393 	}
   1394 	case MTIOCTOP: {
   1395 
   1396 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1397 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
   1398 			mt->mt_count));
   1399 
   1400 		/* compat: in U*x it is a short */
   1401 		number = mt->mt_count;
   1402 		switch ((short) (mt->mt_op)) {
   1403 		case MTWEOF:	/* write an end-of-file record */
   1404 			error = st_write_filemarks(st, number, flags);
   1405 			break;
   1406 		case MTBSF:	/* backward space file */
   1407 			number = -number;
   1408 		case MTFSF:	/* forward space file */
   1409 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1410 			if (!error)
   1411 				error = st_space(st, number - nmarks,
   1412 				    SP_FILEMARKS, flags);
   1413 			break;
   1414 		case MTBSR:	/* backward space record */
   1415 			number = -number;
   1416 		case MTFSR:	/* forward space record */
   1417 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1418 			if (!error)
   1419 				error = st_space(st, number, SP_BLKS, flags);
   1420 			break;
   1421 		case MTREW:	/* rewind */
   1422 			error = st_rewind(st, 0, flags);
   1423 			break;
   1424 		case MTOFFL:	/* rewind and put the drive offline */
   1425 			st_unmount(st, EJECT);
   1426 			break;
   1427 		case MTNOP:	/* no operation, sets status only */
   1428 			break;
   1429 		case MTRETEN:	/* retension the tape */
   1430 			error = st_load(st, LD_RETENSION, flags);
   1431 			if (!error)
   1432 				error = st_load(st, LD_LOAD, flags);
   1433 			break;
   1434 		case MTEOM:	/* forward space to end of media */
   1435 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1436 			if (!error)
   1437 				error = st_space(st, 1, SP_EOM, flags);
   1438 			break;
   1439 		case MTCACHE:	/* enable controller cache */
   1440 			st->flags &= ~ST_DONTBUFFER;
   1441 			goto try_new_value;
   1442 		case MTNOCACHE:	/* disable controller cache */
   1443 			st->flags |= ST_DONTBUFFER;
   1444 			goto try_new_value;
   1445 		case MTERASE:	/* erase volume */
   1446 			error = st_erase(st, number, flags);
   1447 			break;
   1448 		case MTSETBSIZ:	/* Set block size for device */
   1449 #ifdef	NOTYET
   1450 			if (!(st->flags & ST_NEW_MOUNT)) {
   1451 				uprintf("re-mount tape before changing blocksize");
   1452 				error = EINVAL;
   1453 				break;
   1454 			}
   1455 #endif
   1456 			if (number == 0)
   1457 				st->flags &= ~ST_FIXEDBLOCKS;
   1458 			else {
   1459 				if ((st->blkmin || st->blkmax) &&
   1460 				    (number < st->blkmin ||
   1461 				    number > st->blkmax)) {
   1462 					error = EINVAL;
   1463 					break;
   1464 				}
   1465 				st->flags |= ST_FIXEDBLOCKS;
   1466 			}
   1467 			st->blksize = number;
   1468 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1469 			goto try_new_value;
   1470 
   1471 		case MTSETDNSTY:	/* Set density for device and mode */
   1472 			/*
   1473 			 * Any number >= 0 and <= 0xff is legal. Numbers
   1474 			 * above 0x80 are 'vendor unique'.
   1475 			 */
   1476 			if (number < 0 || number > 255) {
   1477 				error = EINVAL;
   1478 				break;
   1479 			} else
   1480 				st->density = number;
   1481 			goto try_new_value;
   1482 
   1483 		case MTCMPRESS:
   1484 			error = st_cmprss(st, number);
   1485 			break;
   1486 
   1487 		case MTEWARN:
   1488 			if (number)
   1489 				st->flags |= ST_EARLYWARN;
   1490 			else
   1491 				st->flags &= ~ST_EARLYWARN;
   1492 			break;
   1493 
   1494 		default:
   1495 			error = EINVAL;
   1496 		}
   1497 		break;
   1498 	}
   1499 	case MTIOCIEOT:
   1500 	case MTIOCEEOT:
   1501 		break;
   1502 
   1503 	case MTIOCRDSPOS:
   1504 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1505 		break;
   1506 
   1507 	case MTIOCRDHPOS:
   1508 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1509 		break;
   1510 
   1511 	case MTIOCSLOCATE:
   1512 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1513 		break;
   1514 
   1515 	case MTIOCHLOCATE:
   1516 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1517 		break;
   1518 
   1519 
   1520 	default:
   1521 		error = scsipi_do_ioctl(st->sc_link, dev, cmd, arg,
   1522 					flag, p);
   1523 		break;
   1524 	}
   1525 	return (error);
   1526 /*-----------------------------*/
   1527 try_new_value:
   1528 	/*
   1529 	 * Check that the mode being asked for is aggreeable to the
   1530 	 * drive. If not, put it back the way it was.
   1531 	 */
   1532 	if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
   1533 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1534 		st->density = hold_density;
   1535 		st->blksize = hold_blksize;
   1536 		if (st->blksize)
   1537 			st->flags |= ST_FIXEDBLOCKS;
   1538 		else
   1539 			st->flags &= ~ST_FIXEDBLOCKS;
   1540 		return (error);
   1541 	}
   1542 	/*
   1543 	 * As the drive liked it, if we are setting a new default,
   1544 	 * set it into the structures as such.
   1545 	 *
   1546 	 * The means for deciding this are not finalised yet- but
   1547 	 * if the device was opened in Control Mode, the values
   1548 	 * are persistent now across mounts.
   1549 	 */
   1550 	if (STMODE(dev) == CTRL_MODE) {
   1551 		switch ((short) (mt->mt_op)) {
   1552 		case MTSETBSIZ:
   1553 			st->modes[dsty].blksize = st->blksize;
   1554 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1555 			break;
   1556 		case MTSETDNSTY:
   1557 			st->modes[dsty].density = st->density;
   1558 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1559 			break;
   1560 		}
   1561 	}
   1562 	return (0);
   1563 }
   1564 
   1565 /*
   1566  * Do a synchronous read.
   1567  */
   1568 int
   1569 st_read(st, buf, size, flags)
   1570 	struct st_softc *st;
   1571 	int size;
   1572 	int flags;
   1573 	char *buf;
   1574 {
   1575 	struct scsi_rw_tape cmd;
   1576 
   1577 	/*
   1578 	 * If it's a null transfer, return immediatly
   1579 	 */
   1580 	if (size == 0)
   1581 		return (0);
   1582 	bzero(&cmd, sizeof(cmd));
   1583 	cmd.opcode = READ;
   1584 	if (st->flags & ST_FIXEDBLOCKS) {
   1585 		cmd.byte2 |= SRW_FIXED;
   1586 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1587 		    cmd.len);
   1588 	} else
   1589 		_lto3b(size, cmd.len);
   1590 	return (scsipi_command(st->sc_link,
   1591 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1592 	    (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
   1593 }
   1594 
   1595 /*
   1596  * Ask the drive what it's min and max blk sizes are.
   1597  */
   1598 int
   1599 st_read_block_limits(st, flags)
   1600 	struct st_softc *st;
   1601 	int flags;
   1602 {
   1603 	struct scsi_block_limits cmd;
   1604 	struct scsi_block_limits_data block_limits;
   1605 	struct scsipi_link *sc_link = st->sc_link;
   1606 	int error;
   1607 
   1608 	/*
   1609 	 * First check if we have it all loaded
   1610 	 */
   1611 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1612 		return (0);
   1613 
   1614 	/*
   1615 	 * do a 'Read Block Limits'
   1616 	 */
   1617 	bzero(&cmd, sizeof(cmd));
   1618 	cmd.opcode = READ_BLOCK_LIMITS;
   1619 
   1620 	/*
   1621 	 * do the command, update the global values
   1622 	 */
   1623 	error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1624 	    sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
   1625 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1626 	if (error)
   1627 		return (error);
   1628 
   1629 	st->blkmin = _2btol(block_limits.min_length);
   1630 	st->blkmax = _3btol(block_limits.max_length);
   1631 
   1632 	SC_DEBUG(sc_link, SDEV_DB3,
   1633 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1634 	return (0);
   1635 }
   1636 
   1637 /*
   1638  * Get the scsi driver to send a full inquiry to the
   1639  * device and use the results to fill out the global
   1640  * parameter structure.
   1641  *
   1642  * called from:
   1643  * attach
   1644  * open
   1645  * ioctl (to reset original blksize)
   1646  */
   1647 int
   1648 st_mode_sense(st, flags)
   1649 	struct st_softc *st;
   1650 	int flags;
   1651 {
   1652 	u_int scsipi_sense_len;
   1653 	int error;
   1654 	struct scsi_mode_sense cmd;
   1655 	struct scsipi_sense {
   1656 		struct scsi_mode_header header;
   1657 		struct scsi_blk_desc blk_desc;
   1658 		u_char sense_data[MAX_PAGE_0_SIZE];
   1659 	} scsipi_sense;
   1660 	struct scsipi_link *sc_link = st->sc_link;
   1661 
   1662 	scsipi_sense_len = 12 + st->page_0_size;
   1663 
   1664 	/*
   1665 	 * Set up a mode sense
   1666 	 */
   1667 	bzero(&cmd, sizeof(cmd));
   1668 	cmd.opcode = SCSI_MODE_SENSE;
   1669 	cmd.length = scsipi_sense_len;
   1670 
   1671 	/*
   1672 	 * do the command, but we don't need the results
   1673 	 * just print them for our interest's sake, if asked,
   1674 	 * or if we need it as a template for the mode select
   1675 	 * store it away.
   1676 	 */
   1677 	error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1678 	    sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len,
   1679 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1680 	if (error)
   1681 		return (error);
   1682 
   1683 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
   1684 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
   1685 	st->media_density = scsipi_sense.blk_desc.density;
   1686 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1687 		st->flags |= ST_READONLY;
   1688 	else
   1689 		st->flags &= ~ST_READONLY;
   1690 	SC_DEBUG(sc_link, SDEV_DB3,
   1691 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1692 	    st->media_density, st->media_blksize,
   1693 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1694 	SC_DEBUG(sc_link, SDEV_DB3,
   1695 	    ("%sbuffered\n",
   1696 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1697 	if (st->page_0_size)
   1698 		bcopy(scsipi_sense.sense_data, st->sense_data,
   1699 		    st->page_0_size);
   1700 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1701 	return (0);
   1702 }
   1703 
   1704 /*
   1705  * Send a filled out parameter structure to the drive to
   1706  * set it into the desire modes etc.
   1707  */
   1708 int
   1709 st_mode_select(st, flags)
   1710 	struct st_softc *st;
   1711 	int flags;
   1712 {
   1713 	u_int scsi_select_len;
   1714 	struct scsi_mode_select cmd;
   1715 	struct scsi_select {
   1716 		struct scsi_mode_header header;
   1717 		struct scsi_blk_desc blk_desc;
   1718 		u_char sense_data[MAX_PAGE_0_SIZE];
   1719 	} scsi_select;
   1720 	struct scsipi_link *sc_link = st->sc_link;
   1721 
   1722 	scsi_select_len = 12 + st->page_0_size;
   1723 
   1724 	/*
   1725 	 * This quirk deals with drives that have only one valid mode
   1726 	 * and think this gives them license to reject all mode selects,
   1727 	 * even if the selected mode is the one that is supported.
   1728 	 */
   1729 	if (st->quirks & ST_Q_UNIMODAL) {
   1730 		SC_DEBUG(sc_link, SDEV_DB3,
   1731 		    ("not setting density 0x%x blksize 0x%x\n",
   1732 		    st->density, st->blksize));
   1733 		return (0);
   1734 	}
   1735 
   1736 	/*
   1737 	 * Set up for a mode select
   1738 	 */
   1739 	bzero(&cmd, sizeof(cmd));
   1740 	cmd.opcode = SCSI_MODE_SELECT;
   1741 	cmd.length = scsi_select_len;
   1742 
   1743 	bzero(&scsi_select, scsi_select_len);
   1744 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1745 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
   1746 	scsi_select.blk_desc.density = st->density;
   1747 	if (st->flags & ST_DONTBUFFER)
   1748 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
   1749 	else
   1750 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1751 	if (st->flags & ST_FIXEDBLOCKS)
   1752 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1753 	if (st->page_0_size)
   1754 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1755 
   1756 	/*
   1757 	 * do the command
   1758 	 */
   1759 	return (scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
   1760 	    sizeof(cmd), (u_char *)&scsi_select, scsi_select_len,
   1761 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT));
   1762 }
   1763 
   1764 int
   1765 st_cmprss(st, onoff)
   1766 	struct st_softc *st;
   1767 	int onoff;
   1768 {
   1769 	u_int scsi_dlen;
   1770 	struct scsi_mode_select mcmd;
   1771 	struct scsi_mode_sense scmd;
   1772 	struct scsi_select {
   1773 		struct scsi_mode_header header;
   1774 		struct scsi_blk_desc blk_desc;
   1775 		u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page),
   1776 		    sizeof(struct scsi_tape_dev_compression_page))];
   1777 	} scsi_pdata;
   1778 	struct scsi_tape_dev_conf_page *ptr;
   1779 	struct scsi_tape_dev_compression_page *cptr;
   1780 	struct scsipi_link *sc_link = st->sc_link;
   1781 	int error, ison, flags;
   1782 
   1783 	scsi_dlen = sizeof(scsi_pdata);
   1784 	bzero(&scsi_pdata, scsi_dlen);
   1785 
   1786 	/*
   1787 	 * Set up for a mode sense.
   1788 	 * Do DATA COMPRESSION page first.
   1789 	 */
   1790 	bzero(&scmd, sizeof(scmd));
   1791 	scmd.opcode = SCSI_MODE_SENSE;
   1792 	scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
   1793 	scmd.length = scsi_dlen;
   1794 
   1795 	flags = XS_CTL_SILENT;
   1796 
   1797 	/*
   1798 	 * Do the MODE SENSE command...
   1799 	 */
   1800 again:
   1801 	bzero(&scsi_pdata, scsi_dlen);
   1802 	error = scsipi_command(sc_link,
   1803 	    (struct scsipi_generic *)&scmd, sizeof(scmd),
   1804 	    (u_char *)&scsi_pdata, scsi_dlen,
   1805 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
   1806 
   1807 	if (error) {
   1808 		if (scmd.byte2 != SMS_DBD) {
   1809 			scmd.byte2 = SMS_DBD;
   1810 			goto again;
   1811 		}
   1812 		/*
   1813 		 * Try a different page?
   1814 		 */
   1815 		if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
   1816 			scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1817 			scmd.byte2 = 0;
   1818 			goto again;
   1819 		}
   1820 		return (error);
   1821 	}
   1822 
   1823 	if (scsi_pdata.header.blk_desc_len)
   1824 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
   1825 	else
   1826 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
   1827 
   1828 	if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
   1829 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
   1830 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
   1831 		if (onoff)
   1832 			cptr->dce_dcc |= DCP_DCE;
   1833 		else
   1834 			cptr->dce_dcc &= ~DCP_DCE;
   1835 		cptr->pagecode &= ~0x80;
   1836 	} else {
   1837 		ison =  (ptr->sel_comp_alg != 0);
   1838 		if (onoff)
   1839 			ptr->sel_comp_alg = 1;
   1840 		else
   1841 			ptr->sel_comp_alg = 0;
   1842 		ptr->pagecode &= ~0x80;
   1843 		ptr->byte2 = 0;
   1844 		ptr->active_partition = 0;
   1845 		ptr->wb_full_ratio = 0;
   1846 		ptr->rb_empty_ratio = 0;
   1847 		ptr->byte8 &= ~0x30;
   1848 		ptr->gap_size = 0;
   1849 		ptr->byte10 &= ~0xe7;
   1850 		ptr->ew_bufsize[0] = 0;
   1851 		ptr->ew_bufsize[1] = 0;
   1852 		ptr->ew_bufsize[2] = 0;
   1853 		ptr->reserved = 0;
   1854 	}
   1855 	onoff = onoff ? 1 : 0;
   1856 	/*
   1857 	 * There might be a virtue in actually doing the MODE SELECTS,
   1858 	 * but let's not clog the bus over it.
   1859 	 */
   1860 	if (onoff == ison)
   1861 		return (0);
   1862 
   1863 	/*
   1864 	 * Set up for a mode select
   1865 	 */
   1866 
   1867 	scsi_pdata.header.data_length = 0;
   1868 	scsi_pdata.header.medium_type = 0;
   1869 	if ((st->flags & ST_DONTBUFFER) == 0)
   1870 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
   1871 	else
   1872 		scsi_pdata.header.dev_spec = 0;
   1873 
   1874 	bzero(&mcmd, sizeof(mcmd));
   1875 	mcmd.opcode = SCSI_MODE_SELECT;
   1876 	mcmd.byte2 = SMS_PF;
   1877 	mcmd.length = scsi_dlen;
   1878 	if (scsi_pdata.header.blk_desc_len) {
   1879 		scsi_pdata.blk_desc.density = 0;
   1880 		scsi_pdata.blk_desc.nblocks[0] = 0;
   1881 		scsi_pdata.blk_desc.nblocks[1] = 0;
   1882 		scsi_pdata.blk_desc.nblocks[2] = 0;
   1883 	}
   1884 
   1885 	/*
   1886 	 * Do the command
   1887 	 */
   1888 	error = scsipi_command(sc_link,
   1889 	    (struct scsipi_generic *)&mcmd, sizeof(mcmd),
   1890 	    (u_char *)&scsi_pdata, scsi_dlen,
   1891 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_OUT);
   1892 
   1893 	if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
   1894 		/*
   1895 		 * Try DEVICE CONFIGURATION page.
   1896 		 */
   1897 		scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1898 		goto again;
   1899 	}
   1900 	return (error);
   1901 }
   1902 /*
   1903  * issue an erase command
   1904  */
   1905 int
   1906 st_erase(st, full, flags)
   1907 	struct st_softc *st;
   1908 	int full, flags;
   1909 {
   1910 	int tmo;
   1911 	struct scsi_erase cmd;
   1912 
   1913 	/*
   1914 	 * Full erase means set LONG bit in erase command, which asks
   1915 	 * the drive to erase the entire unit.  Without this bit, we're
   1916 	 * asking the drive to write an erase gap.
   1917 	 */
   1918 	bzero(&cmd, sizeof(cmd));
   1919 	cmd.opcode = ERASE;
   1920 	if (full) {
   1921 		cmd.byte2 = SE_IMMED|SE_LONG;
   1922 		tmo = ST_SPC_TIME;
   1923 	} else {
   1924 		tmo = ST_IO_TIME;
   1925 		cmd.byte2 = SE_IMMED;
   1926 	}
   1927 
   1928 	/*
   1929 	 * XXX We always do this asynchronously, for now.  How long should
   1930 	 * we wait if we want to (eventually) to it synchronously?
   1931 	 */
   1932 	return (scsipi_command(st->sc_link,
   1933 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1934 	    0, 0, ST_RETRIES, tmo, NULL, flags));
   1935 }
   1936 
   1937 /*
   1938  * skip N blocks/filemarks/seq filemarks/eom
   1939  */
   1940 int
   1941 st_space(st, number, what, flags)
   1942 	struct st_softc *st;
   1943 	u_int what;
   1944 	int flags;
   1945 	int number;
   1946 {
   1947 	struct scsi_space cmd;
   1948 	int error;
   1949 
   1950 	switch (what) {
   1951 	case SP_BLKS:
   1952 		if (st->flags & ST_PER_ACTION) {
   1953 			if (number > 0) {
   1954 				st->flags &= ~ST_PER_ACTION;
   1955 				return (EIO);
   1956 			} else if (number < 0) {
   1957 				if (st->flags & ST_AT_FILEMARK) {
   1958 					/*
   1959 					 * Handling of ST_AT_FILEMARK
   1960 					 * in st_space will fill in the
   1961 					 * right file mark count.
   1962 					 */
   1963 					error = st_space(st, 0, SP_FILEMARKS,
   1964 					    flags);
   1965 					if (error)
   1966 						return (error);
   1967 				}
   1968 				if (st->flags & ST_BLANK_READ) {
   1969 					st->flags &= ~ST_BLANK_READ;
   1970 					return (EIO);
   1971 				}
   1972 				st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
   1973 			}
   1974 		}
   1975 		break;
   1976 	case SP_FILEMARKS:
   1977 		if (st->flags & ST_EIO_PENDING) {
   1978 			if (number > 0) {
   1979 				/* pretend we just discovered the error */
   1980 				st->flags &= ~ST_EIO_PENDING;
   1981 				return (EIO);
   1982 			} else if (number < 0) {
   1983 				/* back away from the error */
   1984 				st->flags &= ~ST_EIO_PENDING;
   1985 			}
   1986 		}
   1987 		if (st->flags & ST_AT_FILEMARK) {
   1988 			st->flags &= ~ST_AT_FILEMARK;
   1989 			number--;
   1990 		}
   1991 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1992 			/* back away from unwritten tape */
   1993 			st->flags &= ~ST_BLANK_READ;
   1994 			number++;	/* XXX dubious */
   1995 		}
   1996 		break;
   1997 	case SP_EOM:
   1998 		if (st->flags & ST_EOM_PENDING) {
   1999 			/* we're already there */
   2000 			st->flags &= ~ST_EOM_PENDING;
   2001 			return (0);
   2002 		}
   2003 		if (st->flags & ST_EIO_PENDING) {
   2004 			/* pretend we just discovered the error */
   2005 			st->flags &= ~ST_EIO_PENDING;
   2006 			return (EIO);
   2007 		}
   2008 		if (st->flags & ST_AT_FILEMARK)
   2009 			st->flags &= ~ST_AT_FILEMARK;
   2010 		break;
   2011 	}
   2012 	if (number == 0)
   2013 		return (0);
   2014 
   2015 	bzero(&cmd, sizeof(cmd));
   2016 	cmd.opcode = SPACE;
   2017 	cmd.byte2 = what;
   2018 	_lto3b(number, cmd.number);
   2019 
   2020 	return (scsipi_command(st->sc_link,
   2021 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   2022 	    0, 0, 0, ST_SPC_TIME, NULL, flags));
   2023 }
   2024 
   2025 /*
   2026  * write N filemarks
   2027  */
   2028 int
   2029 st_write_filemarks(st, number, flags)
   2030 	struct st_softc *st;
   2031 	int flags;
   2032 	int number;
   2033 {
   2034 	struct scsi_write_filemarks cmd;
   2035 
   2036 	/*
   2037 	 * It's hard to write a negative number of file marks.
   2038 	 * Don't try.
   2039 	 */
   2040 	if (number < 0)
   2041 		return (EINVAL);
   2042 	switch (number) {
   2043 	case 0:		/* really a command to sync the drive's buffers */
   2044 		break;
   2045 	case 1:
   2046 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   2047 			st->flags &= ~ST_WRITTEN;
   2048 		else
   2049 			st->flags |= ST_FM_WRITTEN;
   2050 		st->flags &= ~ST_PER_ACTION;
   2051 		break;
   2052 	default:
   2053 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   2054 	}
   2055 
   2056 	bzero(&cmd, sizeof(cmd));
   2057 	cmd.opcode = WRITE_FILEMARKS;
   2058 	_lto3b(number, cmd.number);
   2059 
   2060 	return (scsipi_command(st->sc_link,
   2061 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   2062 	    0, 0, 0, ST_IO_TIME * 4, NULL, flags));
   2063 }
   2064 
   2065 /*
   2066  * Make sure the right number of file marks is on tape if the
   2067  * tape has been written.  If the position argument is true,
   2068  * leave the tape positioned where it was originally.
   2069  *
   2070  * nmarks returns the number of marks to skip (or, if position
   2071  * true, which were skipped) to get back original position.
   2072  */
   2073 int
   2074 st_check_eod(st, position, nmarks, flags)
   2075 	struct st_softc *st;
   2076 	boolean position;
   2077 	int *nmarks;
   2078 	int flags;
   2079 {
   2080 	int error;
   2081 
   2082 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   2083 	default:
   2084 		*nmarks = 0;
   2085 		return (0);
   2086 	case ST_WRITTEN:
   2087 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   2088 		*nmarks = 1;
   2089 		break;
   2090 	case ST_WRITTEN | ST_2FM_AT_EOD:
   2091 		*nmarks = 2;
   2092 	}
   2093 	error = st_write_filemarks(st, *nmarks, flags);
   2094 	if (position && !error)
   2095 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   2096 	return (error);
   2097 }
   2098 
   2099 /*
   2100  * load/unload/retension
   2101  */
   2102 int
   2103 st_load(st, type, flags)
   2104 	struct st_softc *st;
   2105 	u_int type;
   2106 	int flags;
   2107 {
   2108 	int error;
   2109 	struct scsi_load cmd;
   2110 
   2111 	if (type != LD_LOAD) {
   2112 		int nmarks;
   2113 
   2114 		error = st_check_eod(st, FALSE, &nmarks, flags);
   2115 		if (error) {
   2116 			printf("%s: failed to write closing filemarks at "
   2117 			    "unload, errno=%d\n", st->sc_dev.dv_xname, error);
   2118 			return (error);
   2119 		}
   2120 	}
   2121 	if (st->quirks & ST_Q_IGNORE_LOADS) {
   2122 		if (type == LD_LOAD) {
   2123 			/*
   2124 			 * If we ignore loads, at least we should try a rewind.
   2125 			 */
   2126 			return st_rewind(st, 0, flags);
   2127 		}
   2128 		/* otherwise, we should do what's asked of us */
   2129 	}
   2130 
   2131 	bzero(&cmd, sizeof(cmd));
   2132 	cmd.opcode = LOAD;
   2133 	cmd.how = type;
   2134 
   2135 	error = scsipi_command(st->sc_link,
   2136 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
   2137 	    0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
   2138 	if (error) {
   2139 		printf("%s: error %d in st_load (op %d)\n",
   2140 		    st->sc_dev.dv_xname, error, type);
   2141 	}
   2142 	return (error);
   2143 }
   2144 
   2145 /*
   2146  *  Rewind the device
   2147  */
   2148 int
   2149 st_rewind(st, immediate, flags)
   2150 	struct st_softc *st;
   2151 	u_int immediate;
   2152 	int flags;
   2153 {
   2154 	struct scsi_rewind cmd;
   2155 	int error;
   2156 	int nmarks;
   2157 
   2158 	error = st_check_eod(st, FALSE, &nmarks, flags);
   2159 	if (error) {
   2160 		printf("%s: failed to write closing filemarks at "
   2161 		    "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
   2162 		return (error);
   2163 	}
   2164 	st->flags &= ~ST_PER_ACTION;
   2165 
   2166 	bzero(&cmd, sizeof(cmd));
   2167 	cmd.opcode = REWIND;
   2168 	cmd.byte2 = immediate;
   2169 
   2170 	error = scsipi_command(st->sc_link,
   2171 	    (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
   2172 	    immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
   2173 	if (error) {
   2174 		printf("%s: error %d trying to rewind\n",
   2175 		    st->sc_dev.dv_xname, error);
   2176 	}
   2177 	return (error);
   2178 }
   2179 
   2180 int
   2181 st_rdpos(st, hard, blkptr)
   2182 	struct st_softc *st;
   2183 	int hard;
   2184 	u_int32_t *blkptr;
   2185 {
   2186 	int error;
   2187 	u_int8_t posdata[20];
   2188 	struct scsi_tape_read_position cmd;
   2189 
   2190 	/*
   2191 	 * First flush any pending writes...
   2192 	 */
   2193 	error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2194 
   2195 	/*
   2196 	 * The latter case is for 'write protected' tapes
   2197 	 * which are too stupid to recognize a zero count
   2198 	 * for writing filemarks as a no-op.
   2199 	 */
   2200 	if (error != 0 && error != EACCES && error != EROFS)
   2201 		return (error);
   2202 
   2203 	bzero(&cmd, sizeof(cmd));
   2204 	bzero(&posdata, sizeof(posdata));
   2205 	cmd.opcode = READ_POSITION;
   2206 	if (hard)
   2207 		cmd.byte1 = 1;
   2208 
   2209 	error = scsipi_command(st->sc_link,
   2210 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
   2211 	    sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
   2212 	    XS_CTL_SILENT | XS_CTL_DATA_IN);
   2213 
   2214 	if (error == 0) {
   2215 #if	0
   2216 		printf("posdata:");
   2217 		for (hard = 0; hard < sizeof(posdata); hard++)
   2218 			printf("%02x ", posdata[hard] & 0xff);
   2219 		printf("\n");
   2220 #endif
   2221 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   2222 			error = EINVAL;
   2223 		else
   2224 			*blkptr = _4btol(&posdata[4]);
   2225 	}
   2226 	return (error);
   2227 }
   2228 
   2229 int
   2230 st_setpos(st, hard, blkptr)
   2231 	struct st_softc *st;
   2232 	int hard;
   2233 	u_int32_t *blkptr;
   2234 {
   2235 	int error;
   2236 	struct scsi_tape_locate cmd;
   2237 
   2238 	/*
   2239 	 * First flush any pending writes. Strictly speaking,
   2240 	 * we're not supposed to have to worry about this,
   2241 	 * but let's be untrusting.
   2242 	 */
   2243 	error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2244 
   2245 	/*
   2246 	 * The latter case is for 'write protected' tapes
   2247 	 * which are too stupid to recognize a zero count
   2248 	 * for writing filemarks as a no-op.
   2249 	 */
   2250 	if (error != 0 && error != EACCES && error != EROFS)
   2251 		return (error);
   2252 
   2253 	bzero(&cmd, sizeof(cmd));
   2254 	cmd.opcode = LOCATE;
   2255 	if (hard)
   2256 		cmd.byte2 = 1 << 2;
   2257 	_lto4b(*blkptr, cmd.blkaddr);
   2258 	error = scsipi_command(st->sc_link,
   2259 		(struct scsipi_generic *)&cmd, sizeof(cmd),
   2260 		NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
   2261 	/*
   2262 	 * XXX: Note file && block number position now unknown (if
   2263 	 * XXX: these things ever start being maintained in this driver)
   2264 	 */
   2265 	return (error);
   2266 }
   2267 
   2268 
   2269 /*
   2270  * Look at the returned sense and act on the error and determine
   2271  * the unix error number to pass back..., 0 (== report no error),
   2272  * -1 = retry the operation, -2 continue error processing.
   2273  */
   2274 int
   2275 st_interpret_sense(xs)
   2276 	struct scsipi_xfer *xs;
   2277 {
   2278 	struct scsipi_link *sc_link = xs->sc_link;
   2279 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   2280 	struct buf *bp = xs->bp;
   2281 	struct st_softc *st = sc_link->device_softc;
   2282 	int retval = SCSIRET_CONTINUE;
   2283 	int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
   2284 	u_int8_t key;
   2285 	int32_t info;
   2286 
   2287 	/*
   2288 	 * If it isn't a extended or extended/deferred error, let
   2289 	 * the generic code handle it.
   2290 	 */
   2291 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
   2292 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFFERRED */
   2293 		return (retval);
   2294 	}
   2295 
   2296 	if (sense->error_code & SSD_ERRCODE_VALID)
   2297 		info = _4btol(sense->info);
   2298 	else
   2299 		info = xs->datalen;	/* bad choice if fixed blocks */
   2300 	key = sense->flags & SSD_KEY;
   2301 	st->mt_erreg = key;
   2302 	st->asc = sense->add_sense_code;
   2303 	st->ascq = sense->add_sense_code_qual;
   2304 	st->mt_resid = (short) info;
   2305 
   2306 	/*
   2307 	 * If the device is not open yet, let generic handle
   2308 	 */
   2309 	if ((sc_link->flags & SDEV_OPEN) == 0) {
   2310 		return (retval);
   2311 	}
   2312 
   2313 
   2314 	if (st->flags & ST_FIXEDBLOCKS) {
   2315 		xs->resid = info * st->blksize;
   2316 		if (sense->flags & SSD_EOM) {
   2317 			if ((st->flags & ST_EARLYWARN) == 0)
   2318 				st->flags |= ST_EIO_PENDING;
   2319 			st->flags |= ST_EOM_PENDING;
   2320 			if (bp)
   2321 				bp->b_resid = xs->resid;
   2322 		}
   2323 		if (sense->flags & SSD_FILEMARK) {
   2324 			st->flags |= ST_AT_FILEMARK;
   2325 			if (bp)
   2326 				bp->b_resid = xs->resid;
   2327 		}
   2328 		if (sense->flags & SSD_ILI) {
   2329 			st->flags |= ST_EIO_PENDING;
   2330 			if (bp)
   2331 				bp->b_resid = xs->resid;
   2332 			if (sense->error_code & SSD_ERRCODE_VALID &&
   2333 			    (xs->xs_control & XS_CTL_SILENT) == 0)
   2334 				printf("%s: block wrong size, %d blocks "
   2335 				    "residual\n", st->sc_dev.dv_xname, info);
   2336 
   2337 			/*
   2338 			 * This quirk code helps the drive read
   2339 			 * the first tape block, regardless of
   2340 			 * format.  That is required for these
   2341 			 * drives to return proper MODE SENSE
   2342 			 * information.
   2343 			 */
   2344 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   2345 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   2346 				st->blksize -= 512;
   2347 		}
   2348 		/*
   2349 		 * If data wanted and no data was tranfered, do it immediatly
   2350 		 */
   2351 		if (xs->datalen && xs->resid >= xs->datalen) {
   2352 			if (st->flags & ST_EIO_PENDING)
   2353 				return (EIO);
   2354 			if (st->flags & ST_AT_FILEMARK) {
   2355 				if (bp)
   2356 					bp->b_resid = xs->resid;
   2357 				return (SCSIRET_NOERROR);
   2358 			}
   2359 		}
   2360 	} else {		/* must be variable mode */
   2361 		if (sense->flags & SSD_EOM) {
   2362 			/*
   2363 			 * The current semantics of this
   2364 			 * driver requires EOM detection
   2365 			 * to return EIO unless early
   2366 			 * warning detection is enabled
   2367 			 * for variable mode (this is always
   2368 			 * on for fixed block mode).
   2369 			 */
   2370 			if (st->flags & ST_EARLYWARN) {
   2371 				st->flags |= ST_EOM_PENDING;
   2372 				retval = SCSIRET_NOERROR;
   2373 			} else {
   2374 				retval = EIO;
   2375 			}
   2376 
   2377 			/*
   2378 			 * If it's an unadorned EOM detection,
   2379 			 * suppress printing an error.
   2380 			 */
   2381 			if (key == SKEY_NO_SENSE) {
   2382 				doprint = 0;
   2383 			}
   2384 		} else if (sense->flags & SSD_FILEMARK) {
   2385 			retval = SCSIRET_NOERROR;
   2386 		} else if (sense->flags & SSD_ILI) {
   2387 			if (info < 0) {
   2388 				/*
   2389 				 * The tape record was bigger than the read
   2390 				 * we issued.
   2391 				 */
   2392 				if ((xs->xs_control & XS_CTL_SILENT) == 0) {
   2393 					printf("%s: %d-byte tape record too big"
   2394 					    " for %d-byte user buffer\n",
   2395 					    st->sc_dev.dv_xname,
   2396 					    xs->datalen - info, xs->datalen);
   2397 				}
   2398 				retval = EIO;
   2399 			} else {
   2400 				retval = SCSIRET_NOERROR;
   2401 			}
   2402 		}
   2403 		xs->resid = info;
   2404 		if (bp)
   2405 			bp->b_resid = info;
   2406 	}
   2407 
   2408 #ifndef	SCSIDEBUG
   2409 	if (retval == SCSIRET_NOERROR && key == SKEY_NO_SENSE) {
   2410 		doprint = 0;
   2411 	}
   2412 #endif
   2413 	if (key == SKEY_BLANK_CHECK) {
   2414 		/*
   2415 		 * This quirk code helps the drive read the
   2416 		 * first tape block, regardless of format.  That
   2417 		 * is required for these drives to return proper
   2418 		 * MODE SENSE information.
   2419 		 */
   2420 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2421 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   2422 			/* still starting */
   2423 			st->blksize -= 512;
   2424 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2425 			st->flags |= ST_BLANK_READ;
   2426 			xs->resid = xs->datalen;
   2427 			if (bp) {
   2428 				bp->b_resid = xs->resid;
   2429 				/* return an EOF */
   2430 			}
   2431 			retval = SCSIRET_NOERROR;
   2432 		}
   2433 	}
   2434 #ifdef	SCSIVERBOSE
   2435 	if (doprint) {
   2436 		scsipi_print_sense(xs, 0);
   2437 	}
   2438 #else
   2439 	if (doprint) {
   2440 		xs->sc_link->sc_print_addr(xs->sc_link);
   2441 		printf("Sense Key 0x%02x", key);
   2442 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
   2443 			switch (key) {
   2444 			case SKEY_NOT_READY:
   2445 			case SKEY_ILLEGAL_REQUEST:
   2446 			case SKEY_UNIT_ATTENTION:
   2447 			case SKEY_WRITE_PROTECT:
   2448 				break;
   2449 			case SKEY_BLANK_CHECK:
   2450 				printf(", requested size: %d (decimal)", info);
   2451 				break;
   2452 			case SKEY_ABORTED_COMMAND:
   2453 				if (xs->retries)
   2454 					printf(", retrying");
   2455 				printf(", cmd 0x%x, info 0x%x",
   2456 				    xs->cmd->opcode, info);
   2457 				break;
   2458 			default:
   2459 				printf(", info = %d (decimal)", info);
   2460 			}
   2461 		}
   2462 		if (sense->extra_len != 0) {
   2463 			int n;
   2464 			printf(", data =");
   2465 			for (n = 0; n < sense->extra_len; n++)
   2466 				printf(" %02x", sense->cmd_spec_info[n]);
   2467 		}
   2468 		printf("\n");
   2469 	}
   2470 #endif
   2471 	return (retval);
   2472 }
   2473 
   2474 /*
   2475  * The quirk here is that the drive returns some value to st_mode_sense
   2476  * incorrectly until the tape has actually passed by the head.
   2477  *
   2478  * The method is to set the drive to large fixed-block state (user-specified
   2479  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2480  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2481  * work, as a last resort, try variable- length blocks.  The result will be
   2482  * the ability to do an accurate st_mode_sense.
   2483  *
   2484  * We know we can do a rewind because we just did a load, which implies rewind.
   2485  * Rewind seems preferable to space backward if we have a virgin tape.
   2486  *
   2487  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2488  * error processing, both part of st_interpret_sense.
   2489  */
   2490 int
   2491 st_touch_tape(st)
   2492 	struct st_softc *st;
   2493 {
   2494 	char *buf;
   2495 	int readsize;
   2496 	int error;
   2497 
   2498 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   2499 	if (buf == NULL)
   2500 		return (ENOMEM);
   2501 
   2502 	if ((error = st_mode_sense(st, 0)) != 0)
   2503 		goto bad;
   2504 	st->blksize = 1024;
   2505 	do {
   2506 		switch (st->blksize) {
   2507 		case 512:
   2508 		case 1024:
   2509 			readsize = st->blksize;
   2510 			st->flags |= ST_FIXEDBLOCKS;
   2511 			break;
   2512 		default:
   2513 			readsize = 1;
   2514 			st->flags &= ~ST_FIXEDBLOCKS;
   2515 		}
   2516 		if ((error = st_mode_select(st, 0)) != 0)
   2517 			goto bad;
   2518 		st_read(st, buf, readsize, XS_CTL_SILENT);	/* XXX */
   2519 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2520 bad:			free(buf, M_TEMP);
   2521 			return (error);
   2522 		}
   2523 	} while (readsize != 1 && readsize > st->blksize);
   2524 
   2525 	free(buf, M_TEMP);
   2526 	return (0);
   2527 }
   2528 
   2529 int
   2530 stdump(dev, blkno, va, size)
   2531 	dev_t dev;
   2532 	daddr_t blkno;
   2533 	caddr_t va;
   2534 	size_t size;
   2535 {
   2536 
   2537 	/* Not implemented. */
   2538 	return (ENXIO);
   2539 }
   2540