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