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