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