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