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