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