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