Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.75
      1 /*	$NetBSD: st.c,v 1.75 1997/09/29 19:33:03 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 operations */
    267 };
    268 
    269 
    270 #ifdef __BROKEN_INDIRECT_CONFIG
    271 int	stmatch __P((struct device *, void *, void *));
    272 #else
    273 int	stmatch __P((struct device *, struct cfdata *, void *));
    274 #endif
    275 void	stattach __P((struct device *, struct device *, void *));
    276 void	st_identify_drive __P((struct st_softc *,
    277 								struct scsipi_inquiry_pattern *));
    278 void	st_loadquirks __P((struct st_softc *));
    279 int	st_mount_tape __P((dev_t, int));
    280 void	st_unmount __P((struct st_softc *, boolean));
    281 int	st_decide_mode __P((struct st_softc *, boolean));
    282 void	ststart __P((void *));
    283 int	st_read __P((struct st_softc *, char *, int, int));
    284 int	st_read_block_limits __P((struct st_softc *, int));
    285 int	st_mode_sense __P((struct st_softc *, int));
    286 int	st_mode_select __P((struct st_softc *, int));
    287 int	st_cmprss __P((struct st_softc *, int));
    288 int	st_space __P((struct st_softc *, int, u_int, int));
    289 int	st_write_filemarks __P((struct st_softc *, int, int));
    290 int	st_check_eod __P((struct st_softc *, boolean, int *, int));
    291 int	st_load __P((struct st_softc *, u_int, int));
    292 int	st_rewind __P((struct st_softc *, u_int, int));
    293 int	st_interpret_sense __P((struct scsipi_xfer *));
    294 int	st_touch_tape __P((struct st_softc *));
    295 int	st_erase __P((struct st_softc *, int full, int flags));
    296 int	st_rdpos __P((struct st_softc *, int, u_int32_t *));
    297 int	st_setpos __P((struct st_softc *, int, u_int32_t *));
    298 
    299 struct cfattach st_ca = {
    300 	sizeof(struct st_softc), stmatch, stattach
    301 };
    302 
    303 struct cfdriver st_cd = {
    304 	NULL, "st", DV_TAPE
    305 };
    306 
    307 struct scsipi_device st_switch = {
    308 	st_interpret_sense,
    309 	ststart,
    310 	NULL,
    311 	NULL,
    312 };
    313 
    314 #define	ST_INFO_VALID	0x0001
    315 #define	ST_BLOCK_SET	0x0002	/* block size, mode set by ioctl      */
    316 #define	ST_WRITTEN	0x0004	/* data have been written, EOD needed */
    317 #define	ST_FIXEDBLOCKS	0x0008
    318 #define	ST_AT_FILEMARK	0x0010
    319 #define	ST_EIO_PENDING	0x0020	/* we couldn't report it then (had data) */
    320 #define	ST_NEW_MOUNT	0x0040	/* still need to decide mode              */
    321 #define	ST_READONLY	0x0080	/* st_mode_sense says write protected */
    322 #define	ST_FM_WRITTEN	0x0100	/*
    323 				 * EOF file mark written  -- used with
    324 				 * ~ST_WRITTEN to indicate that multiple file
    325 				 * marks have been written
    326 				 */
    327 #define	ST_BLANK_READ	0x0200	/* BLANK CHECK encountered already */
    328 #define	ST_2FM_AT_EOD	0x0400	/* write 2 file marks at EOD */
    329 #define	ST_MOUNTED	0x0800	/* Device is presently mounted */
    330 #define	ST_DONTBUFFER	0x1000	/* Disable buffering/caching */
    331 
    332 #define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
    333 #define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
    334 			 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
    335 			 ST_2FM_AT_EOD | ST_PER_ACTION)
    336 
    337 struct scsipi_inquiry_pattern st_patterns[] = {
    338 	{T_SEQUENTIAL, T_REMOV,
    339 	 "",         "",                 ""},
    340 };
    341 
    342 int
    343 stmatch(parent, match, aux)
    344 	struct device *parent;
    345 #ifdef __BROKEN_INDIRECT_CONFIG
    346 	void *match;
    347 #else
    348 	struct cfdata *match;
    349 #endif
    350 	void *aux;
    351 {
    352 	struct scsipibus_attach_args *sa = aux;
    353 	int priority;
    354 
    355 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    356 	    (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
    357 	    sizeof(st_patterns[0]), &priority);
    358 	return (priority);
    359 }
    360 
    361 /*
    362  * The routine called by the low level scsi routine when it discovers
    363  * A device suitable for this driver
    364  */
    365 void
    366 stattach(parent, self, aux)
    367 	struct device *parent, *self;
    368 	void *aux;
    369 {
    370 	struct st_softc *st = (void *)self;
    371 	struct scsipibus_attach_args *sa = aux;
    372 	struct scsipi_link *sc_link = sa->sa_sc_link;
    373 
    374 	SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
    375 
    376 	/*
    377 	 * Store information needed to contact our base driver
    378 	 */
    379 	st->sc_link = sc_link;
    380 	sc_link->device = &st_switch;
    381 	sc_link->device_softc = st;
    382 	sc_link->openings = 1;
    383 
    384 	/*
    385 	 * Check if the drive is a known criminal and take
    386 	 * Any steps needed to bring it into line
    387 	 */
    388 	st_identify_drive(st, &sa->sa_inqbuf);
    389 
    390 	/*
    391 	 * Use the subdriver to request information regarding
    392 	 * the drive. We cannot use interrupts yet, so the
    393 	 * request must specify this.
    394 	 */
    395 	printf("\n");
    396 	printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
    397 	if (scsipi_test_unit_ready(sc_link,
    398 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) ||
    399 	    st_mode_sense(st,
    400 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE))
    401 		printf("drive empty\n");
    402 	else {
    403 		printf("density code 0x%x, ", st->media_density);
    404 		if (st->media_blksize > 0)
    405 			printf("%d-byte", st->media_blksize);
    406 		else
    407 			printf("variable");
    408 		printf(" blocks, write-%s\n",
    409 		    (st->flags & ST_READONLY) ? "protected" : "enabled");
    410 	}
    411 
    412 	/*
    413 	 * Set up the buf queue for this device
    414 	 */
    415 	st->buf_queue.b_active = 0;
    416 	st->buf_queue.b_actf = 0;
    417 	st->buf_queue.b_actb = &st->buf_queue.b_actf;
    418 }
    419 
    420 /*
    421  * Use the inquiry routine in 'scsi_base' to get drive info so we can
    422  * Further tailor our behaviour.
    423  */
    424 void
    425 st_identify_drive(st, inqbuf)
    426 	struct st_softc *st;
    427 	struct scsipi_inquiry_pattern *inqbuf;
    428 {
    429 	struct st_quirk_inquiry_pattern *finger;
    430 	int priority;
    431 
    432 	finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
    433 	    (caddr_t)st_quirk_patterns,
    434 	    sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]),
    435 	    sizeof(st_quirk_patterns[0]), &priority);
    436 	if (priority != 0) {
    437 		st->quirkdata = &finger->quirkdata;
    438 		st->drive_quirks = finger->quirkdata.quirks;
    439 		st->quirks = finger->quirkdata.quirks;	/* start value */
    440 		st->page_0_size = finger->quirkdata.page_0_size;
    441 		st_loadquirks(st);
    442 	}
    443 }
    444 
    445 /*
    446  * initialise the subdevices to the default (QUIRK) state.
    447  * this will remove any setting made by the system operator or previous
    448  * operations.
    449  */
    450 void
    451 st_loadquirks(st)
    452 	struct st_softc *st;
    453 {
    454 	int i;
    455 	struct	modes *mode;
    456 	struct	modes *mode2;
    457 
    458 	mode = st->quirkdata->modes;
    459 	mode2 = st->modes;
    460 	for (i = 0; i < 4; i++) {
    461 		bzero(mode2, sizeof(struct modes));
    462 		st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
    463 		    DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
    464 		    DENSITY_SET_BY_USER);
    465 		if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
    466 			mode2->blksize = mode->blksize;
    467 			st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
    468 		}
    469 		if (mode->density) {
    470 			mode2->density = mode->density;
    471 			st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
    472 		}
    473 		mode++;
    474 		mode2++;
    475 	}
    476 }
    477 
    478 /*
    479  * open the device.
    480  */
    481 int
    482 stopen(dev, flags, mode, p)
    483 	dev_t dev;
    484 	int flags;
    485 	int mode;
    486 	struct proc *p;
    487 {
    488 	int unit;
    489 	u_int stmode, dsty;
    490 	int error = 0;
    491 	struct st_softc *st;
    492 	struct scsipi_link *sc_link;
    493 
    494 	unit = STUNIT(dev);
    495 	if (unit >= st_cd.cd_ndevs)
    496 		return ENXIO;
    497 	st = st_cd.cd_devs[unit];
    498 	if (!st)
    499 		return ENXIO;
    500 
    501 	stmode = STMODE(dev);
    502 	dsty = STDSTY(dev);
    503 	sc_link = st->sc_link;
    504 
    505 	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    506 	    unit, st_cd.cd_ndevs));
    507 
    508 	/*
    509 	 * Only allow one at a time
    510 	 */
    511 	if (sc_link->flags & SDEV_OPEN) {
    512 		printf("%s: already open\n", st->sc_dev.dv_xname);
    513 		return EBUSY;
    514 	}
    515 
    516 	/*
    517 	 * Catch any unit attention errors.
    518 	 */
    519 	error = scsipi_test_unit_ready(sc_link,
    520 				     SCSI_IGNORE_MEDIA_CHANGE |
    521 				     (stmode == CTLMODE ?
    522 					SCSI_IGNORE_NOT_READY : 0));
    523 	if (error)
    524 		goto bad;
    525 
    526 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    527 
    528 	/*
    529 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    530 	 * then the device has been opened to set defaults
    531 	 * This mode does NOT ALLOW I/O, only ioctls
    532 	 */
    533 	if (stmode == CTLMODE)
    534 		return 0;
    535 
    536 	/*
    537 	 * if it's a different mode, or if the media has been
    538 	 * invalidated, unmount the tape from the previous
    539 	 * session but continue with open processing
    540 	 */
    541 	if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
    542 		st_unmount(st, NOEJECT);
    543 
    544 	/*
    545 	 * If we are not mounted, then we should start a new
    546 	 * mount session.
    547 	 */
    548 	if (!(st->flags & ST_MOUNTED)) {
    549 		st_mount_tape(dev, flags);
    550 		st->last_dsty = dsty;
    551 	}
    552 
    553 	/*
    554 	 * Make sure that a tape opened in write-only mode will have
    555 	 * file marks written on it when closed, even if not written to.
    556 	 * This is for SUN compatibility
    557 	 */
    558 	if ((flags & O_ACCMODE) == FWRITE)
    559 		st->flags |= ST_WRITTEN;
    560 
    561 	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
    562 	return 0;
    563 
    564 bad:
    565 	st_unmount(st, NOEJECT);
    566 	sc_link->flags &= ~SDEV_OPEN;
    567 	return error;
    568 }
    569 
    570 /*
    571  * close the device.. only called if we are the LAST
    572  * occurence of an open device
    573  */
    574 int
    575 stclose(dev, flags, mode, p)
    576 	dev_t dev;
    577 	int flags;
    578 	int mode;
    579 	struct proc *p;
    580 {
    581 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
    582 
    583 	SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
    584 	if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
    585 		st_write_filemarks(st, 1, 0);
    586 	switch (STMODE(dev)) {
    587 	case 0:
    588 	case 3:		/* for now */
    589 		st_unmount(st, NOEJECT);
    590 		break;
    591 	case 1:
    592 		/* leave mounted unless media seems to have been removed */
    593 		if (!(st->sc_link->flags & SDEV_MEDIA_LOADED))
    594 			st_unmount(st, NOEJECT);
    595 		break;
    596 	case 2:
    597 		st_unmount(st, EJECT);
    598 		break;
    599 	}
    600 	st->sc_link->flags &= ~SDEV_OPEN;
    601 
    602 	return 0;
    603 }
    604 
    605 /*
    606  * Start a new mount session.
    607  * Copy in all the default parameters from the selected device mode.
    608  * and try guess any that seem to be defaulted.
    609  */
    610 int
    611 st_mount_tape(dev, flags)
    612 	dev_t dev;
    613 	int flags;
    614 {
    615 	int unit;
    616 	u_int mode, dsty;
    617 	struct st_softc *st;
    618 	struct scsipi_link *sc_link;
    619 	int error = 0;
    620 
    621 	unit = STUNIT(dev);
    622 	mode = STMODE(dev);
    623 	dsty = STDSTY(dev);
    624 	st = st_cd.cd_devs[unit];
    625 	sc_link = st->sc_link;
    626 
    627 	if (st->flags & ST_MOUNTED)
    628 		return 0;
    629 
    630 	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
    631 	st->flags |= ST_NEW_MOUNT;
    632 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    633 	/*
    634 	 * If the media is new, then make sure we give it a chance to
    635 	 * to do a 'load' instruction.  (We assume it is new.)
    636 	 */
    637 	if ((error = st_load(st, LD_LOAD, 0)) != 0)
    638 		return error;
    639 	/*
    640 	 * Throw another dummy instruction to catch
    641 	 * 'Unit attention' errors. Some drives appear to give
    642 	 * these after doing a Load instruction.
    643 	 * (noteably some DAT drives)
    644 	 */
    645 	scsipi_test_unit_ready(sc_link, SCSI_SILENT);	/* XXX */
    646 
    647 	/*
    648 	 * Some devices can't tell you much until they have been
    649 	 * asked to look at the media. This quirk does this.
    650 	 */
    651 	if (st->quirks & ST_Q_SENSE_HELP)
    652 		if ((error = st_touch_tape(st)) != 0)
    653 			return error;
    654 	/*
    655 	 * Load the physical device parameters
    656 	 * loads: blkmin, blkmax
    657 	 */
    658 	if ((error = st_read_block_limits(st, 0)) != 0)
    659 		return error;
    660 	/*
    661 	 * Load the media dependent parameters
    662 	 * includes: media_blksize,media_density,numblks
    663 	 * As we have a tape in, it should be reflected here.
    664 	 * If not you may need the "quirk" above.
    665 	 */
    666 	if ((error = st_mode_sense(st, 0)) != 0)
    667 		return error;
    668 	/*
    669 	 * If we have gained a permanent density from somewhere,
    670 	 * then use it in preference to the one supplied by
    671 	 * default by the driver.
    672 	 */
    673 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    674 		st->density = st->modes[dsty].density;
    675 	else
    676 		st->density = st->media_density;
    677 	/*
    678 	 * If we have gained a permanent blocksize
    679 	 * then use it in preference to the one supplied by
    680 	 * default by the driver.
    681 	 */
    682 	st->flags &= ~ST_FIXEDBLOCKS;
    683 	if (st->modeflags[dsty] & (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, (struct scsipi_generic *) &cmd,
   1045 		    sizeof(cmd), (u_char *) bp->b_data, bp->b_bcount, 0,
   1046 		    100000, bp, flags | SCSI_NOSLEEP))
   1047 			printf("%s: not queued\n", st->sc_dev.dv_xname);
   1048 	} /* go back and see if we can cram more work in.. */
   1049 }
   1050 
   1051 int
   1052 stread(dev, uio, iomode)
   1053 	dev_t dev;
   1054 	struct uio *uio;
   1055 	int iomode;
   1056 {
   1057 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1058 
   1059 	return (physio(ststrategy, NULL, dev, B_READ,
   1060 		       st->sc_link->adapter->scsipi_minphys, uio));
   1061 }
   1062 
   1063 int
   1064 stwrite(dev, uio, iomode)
   1065 	dev_t dev;
   1066 	struct uio *uio;
   1067 	int iomode;
   1068 {
   1069 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1070 
   1071 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1072 		       st->sc_link->adapter->scsipi_minphys, uio));
   1073 }
   1074 
   1075 /*
   1076  * Perform special action on behalf of the user;
   1077  * knows about the internals of this device
   1078  */
   1079 int
   1080 stioctl(dev, cmd, arg, flag, p)
   1081 	dev_t dev;
   1082 	u_long cmd;
   1083 	caddr_t arg;
   1084 	int flag;
   1085 	struct proc *p;
   1086 {
   1087 	int error = 0;
   1088 	int unit;
   1089 	int number, nmarks, dsty;
   1090 	int flags;
   1091 	struct st_softc *st;
   1092 	int hold_blksize;
   1093 	u_int8_t hold_density;
   1094 	struct mtop *mt = (struct mtop *) arg;
   1095 
   1096 	/*
   1097 	 * Find the device that the user is talking about
   1098 	 */
   1099 	flags = 0;		/* give error messages, act on errors etc. */
   1100 	unit = STUNIT(dev);
   1101 	dsty = STDSTY(dev);
   1102 	st = st_cd.cd_devs[unit];
   1103 	hold_blksize = st->blksize;
   1104 	hold_density = st->density;
   1105 
   1106 	switch ((u_int) cmd) {
   1107 
   1108 	case MTIOCGET: {
   1109 		struct mtget *g = (struct mtget *) arg;
   1110 
   1111 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1112 		bzero(g, sizeof(struct mtget));
   1113 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1114 		g->mt_blksiz = st->blksize;
   1115 		g->mt_density = st->density;
   1116 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1117 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1118 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1119 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1120 		g->mt_mdensity[0] = st->modes[0].density;
   1121 		g->mt_mdensity[1] = st->modes[1].density;
   1122 		g->mt_mdensity[2] = st->modes[2].density;
   1123 		g->mt_mdensity[3] = st->modes[3].density;
   1124 		break;
   1125 	}
   1126 	case MTIOCTOP: {
   1127 
   1128 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1129 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
   1130 
   1131 		/* compat: in U*x it is a short */
   1132 		number = mt->mt_count;
   1133 		switch ((short) (mt->mt_op)) {
   1134 		case MTWEOF:	/* write an end-of-file record */
   1135 			error = st_write_filemarks(st, number, flags);
   1136 			break;
   1137 		case MTBSF:	/* backward space file */
   1138 			number = -number;
   1139 		case MTFSF:	/* forward space file */
   1140 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1141 			if (!error)
   1142 				error = st_space(st, number - nmarks,
   1143 				    SP_FILEMARKS, flags);
   1144 			break;
   1145 		case MTBSR:	/* backward space record */
   1146 			number = -number;
   1147 		case MTFSR:	/* forward space record */
   1148 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1149 			if (!error)
   1150 				error = st_space(st, number, SP_BLKS, flags);
   1151 			break;
   1152 		case MTREW:	/* rewind */
   1153 			error = st_rewind(st, 0, flags);
   1154 			break;
   1155 		case MTOFFL:	/* rewind and put the drive offline */
   1156 			st_unmount(st, EJECT);
   1157 			break;
   1158 		case MTNOP:	/* no operation, sets status only */
   1159 			break;
   1160 		case MTRETEN:	/* retension the tape */
   1161 			error = st_load(st, LD_RETENSION, flags);
   1162 			if (!error)
   1163 				error = st_load(st, LD_LOAD, flags);
   1164 			break;
   1165 		case MTEOM:	/* forward space to end of media */
   1166 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1167 			if (!error)
   1168 				error = st_space(st, 1, SP_EOM, flags);
   1169 			break;
   1170 		case MTCACHE:	/* enable controller cache */
   1171 			st->flags &= ~ST_DONTBUFFER;
   1172 			goto try_new_value;
   1173 		case MTNOCACHE:	/* disable controller cache */
   1174 			st->flags |= ST_DONTBUFFER;
   1175 			goto try_new_value;
   1176 		case MTERASE:	/* erase volume */
   1177 			error = st_erase(st, number, flags);
   1178 			break;
   1179 		case MTSETBSIZ:	/* Set block size for device */
   1180 #ifdef	NOTYET
   1181 			if (!(st->flags & ST_NEW_MOUNT)) {
   1182 				uprintf("re-mount tape before changing blocksize");
   1183 				error = EINVAL;
   1184 				break;
   1185 			}
   1186 #endif
   1187 			if (number == 0) {
   1188 				st->flags &= ~ST_FIXEDBLOCKS;
   1189 			} else {
   1190 				if ((st->blkmin || st->blkmax) &&
   1191 				    (number < st->blkmin ||
   1192 				    number > st->blkmax)) {
   1193 					error = EINVAL;
   1194 					break;
   1195 				}
   1196 				st->flags |= ST_FIXEDBLOCKS;
   1197 			}
   1198 			st->blksize = number;
   1199 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1200 			goto try_new_value;
   1201 
   1202 		case MTSETDNSTY:	/* Set density for device and mode */
   1203 			if (number > SCSI_2_MAX_DENSITY_CODE) {
   1204 				error = EINVAL;
   1205 				break;
   1206 			} else
   1207 				st->density = number;
   1208 			goto try_new_value;
   1209 
   1210 		case MTCMPRESS:
   1211 			error = st_cmprss(st, number);
   1212 			break;
   1213 
   1214 		default:
   1215 			error = EINVAL;
   1216 		}
   1217 		break;
   1218 	}
   1219 	case MTIOCIEOT:
   1220 	case MTIOCEEOT:
   1221 		break;
   1222 
   1223 	case MTIOCRDSPOS:
   1224 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1225 		break;
   1226 
   1227 	case MTIOCRDHPOS:
   1228 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1229 		break;
   1230 
   1231 	case MTIOCSLOCATE:
   1232 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1233 		break;
   1234 
   1235 	case MTIOCHLOCATE:
   1236 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1237 		break;
   1238 
   1239 	default:
   1240 		if (STMODE(dev) == CTLMODE)
   1241 			error = scsipi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p);
   1242 		else
   1243 			error = ENOTTY;
   1244 		break;
   1245 	}
   1246 	return error;
   1247 /*-----------------------------*/
   1248 try_new_value:
   1249 	/*
   1250 	 * Check that the mode being asked for is aggreeable to the
   1251 	 * drive. If not, put it back the way it was.
   1252 	 */
   1253 	if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
   1254 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1255 		st->density = hold_density;
   1256 		st->blksize = hold_blksize;
   1257 		if (st->blksize)
   1258 			st->flags |= ST_FIXEDBLOCKS;
   1259 		else
   1260 			st->flags &= ~ST_FIXEDBLOCKS;
   1261 		return error;
   1262 	}
   1263 	/*
   1264 	 * As the drive liked it, if we are setting a new default,
   1265 	 * set it into the structures as such.
   1266 	 *
   1267 	 * The means for deciding this are not finalised yet
   1268 	 */
   1269 	if (STMODE(dev) == 0x03) {
   1270 		/* special mode */
   1271 		/* XXX */
   1272 		switch ((short) (mt->mt_op)) {
   1273 		case MTSETBSIZ:
   1274 			st->modes[dsty].blksize = st->blksize;
   1275 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1276 			break;
   1277 		case MTSETDNSTY:
   1278 			st->modes[dsty].density = st->density;
   1279 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1280 			break;
   1281 		}
   1282 	}
   1283 	return 0;
   1284 }
   1285 
   1286 /*
   1287  * Do a synchronous read.
   1288  */
   1289 int
   1290 st_read(st, buf, size, flags)
   1291 	struct st_softc *st;
   1292 	int size;
   1293 	int flags;
   1294 	char *buf;
   1295 {
   1296 	struct scsi_rw_tape cmd;
   1297 
   1298 	/*
   1299 	 * If it's a null transfer, return immediatly
   1300 	 */
   1301 	if (size == 0)
   1302 		return 0;
   1303 	bzero(&cmd, sizeof(cmd));
   1304 	cmd.opcode = READ;
   1305 	if (st->flags & ST_FIXEDBLOCKS) {
   1306 		cmd.byte2 |= SRW_FIXED;
   1307 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1308 		    cmd.len);
   1309 	} else
   1310 		_lto3b(size, cmd.len);
   1311 	return st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *) &cmd,
   1312 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
   1313 	    flags | SCSI_DATA_IN);
   1314 }
   1315 
   1316 /*
   1317  * Ask the drive what it's min and max blk sizes are.
   1318  */
   1319 int
   1320 st_read_block_limits(st, flags)
   1321 	struct st_softc *st;
   1322 	int flags;
   1323 {
   1324 	struct scsi_block_limits cmd;
   1325 	struct scsi_block_limits_data block_limits;
   1326 	struct scsipi_link *sc_link = st->sc_link;
   1327 	int error;
   1328 
   1329 	/*
   1330 	 * First check if we have it all loaded
   1331 	 */
   1332 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1333 		return 0;
   1334 
   1335 	/*
   1336 	 * do a 'Read Block Limits'
   1337 	 */
   1338 	bzero(&cmd, sizeof(cmd));
   1339 	cmd.opcode = READ_BLOCK_LIMITS;
   1340 
   1341 	/*
   1342 	 * do the command, update the global values
   1343 	 */
   1344 	error = sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &cmd,
   1345 			      sizeof(cmd), (u_char *) &block_limits,
   1346 			      sizeof(block_limits), ST_RETRIES, 5000,
   1347 			      NULL, flags | SCSI_DATA_IN);
   1348 	if (error)
   1349 		return error;
   1350 
   1351 	st->blkmin = _2btol(block_limits.min_length);
   1352 	st->blkmax = _3btol(block_limits.max_length);
   1353 
   1354 	SC_DEBUG(sc_link, SDEV_DB3,
   1355 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1356 	return 0;
   1357 }
   1358 
   1359 /*
   1360  * Get the scsi driver to send a full inquiry to the
   1361  * device and use the results to fill out the global
   1362  * parameter structure.
   1363  *
   1364  * called from:
   1365  * attach
   1366  * open
   1367  * ioctl (to reset original blksize)
   1368  */
   1369 int
   1370 st_mode_sense(st, flags)
   1371 	struct st_softc *st;
   1372 	int flags;
   1373 {
   1374 	u_int scsipi_sense_len;
   1375 	int error;
   1376 	struct scsi_mode_sense cmd;
   1377 	struct scsipi_sense {
   1378 		struct scsi_mode_header header;
   1379 		struct scsi_blk_desc blk_desc;
   1380 		u_char sense_data[MAX_PAGE_0_SIZE];
   1381 	} scsipi_sense;
   1382 	struct scsipi_link *sc_link = st->sc_link;
   1383 
   1384 	scsipi_sense_len = 12 + st->page_0_size;
   1385 
   1386 	/*
   1387 	 * Set up a mode sense
   1388 	 */
   1389 	bzero(&cmd, sizeof(cmd));
   1390 	cmd.opcode = SCSI_MODE_SENSE;
   1391 	cmd.length = scsipi_sense_len;
   1392 
   1393 	/*
   1394 	 * do the command, but we don't need the results
   1395 	 * just print them for our interest's sake, if asked,
   1396 	 * or if we need it as a template for the mode select
   1397 	 * store it away.
   1398 	 */
   1399 	error = sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &cmd,
   1400 			      sizeof(cmd), (u_char *) &scsipi_sense,
   1401 			      scsipi_sense_len, ST_RETRIES, 5000, NULL,
   1402 			      flags | SCSI_DATA_IN);
   1403 	if (error)
   1404 		return error;
   1405 
   1406 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
   1407 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
   1408 	st->media_density = scsipi_sense.blk_desc.density;
   1409 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1410 		st->flags |= ST_READONLY;
   1411 	SC_DEBUG(sc_link, SDEV_DB3,
   1412 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1413 	    st->media_density, st->media_blksize,
   1414 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1415 	SC_DEBUG(sc_link, SDEV_DB3,
   1416 	    ("%sbuffered\n",
   1417 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1418 	if (st->page_0_size)
   1419 		bcopy(scsipi_sense.sense_data, st->sense_data, st->page_0_size);
   1420 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1421 	return 0;
   1422 }
   1423 
   1424 /*
   1425  * Send a filled out parameter structure to the drive to
   1426  * set it into the desire modes etc.
   1427  */
   1428 int
   1429 st_mode_select(st, flags)
   1430 	struct st_softc *st;
   1431 	int flags;
   1432 {
   1433 	u_int scsi_select_len;
   1434 	struct scsi_mode_select cmd;
   1435 	struct scsi_select {
   1436 		struct scsi_mode_header header;
   1437 		struct scsi_blk_desc blk_desc;
   1438 		u_char sense_data[MAX_PAGE_0_SIZE];
   1439 	} scsi_select;
   1440 	struct scsipi_link *sc_link = st->sc_link;
   1441 
   1442 	scsi_select_len = 12 + st->page_0_size;
   1443 
   1444 	/*
   1445 	 * This quirk deals with drives that have only one valid mode
   1446 	 * and think this gives them license to reject all mode selects,
   1447 	 * even if the selected mode is the one that is supported.
   1448 	 */
   1449 	if (st->quirks & ST_Q_UNIMODAL) {
   1450 		SC_DEBUG(sc_link, SDEV_DB3,
   1451 		    ("not setting density 0x%x blksize 0x%x\n",
   1452 		    st->density, st->blksize));
   1453 		return 0;
   1454 	}
   1455 
   1456 	/*
   1457 	 * Set up for a mode select
   1458 	 */
   1459 	bzero(&cmd, sizeof(cmd));
   1460 	cmd.opcode = SCSI_MODE_SELECT;
   1461 	cmd.length = scsi_select_len;
   1462 
   1463 	bzero(&scsi_select, scsi_select_len);
   1464 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1465 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
   1466 	scsi_select.blk_desc.density = st->density;
   1467 	if (st->flags & ST_DONTBUFFER)
   1468 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
   1469 	else
   1470 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1471 	if (st->flags & ST_FIXEDBLOCKS)
   1472 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1473 	if (st->page_0_size)
   1474 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1475 
   1476 	/*
   1477 	 * do the command
   1478 	 */
   1479 	return sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &cmd,
   1480 	    sizeof(cmd), (u_char *) &scsi_select, scsi_select_len,
   1481 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
   1482 }
   1483 
   1484 int
   1485 st_cmprss(st, onoff)
   1486 	struct st_softc *st;
   1487 	int onoff;
   1488 {
   1489 	u_int scsi_dlen;
   1490 	struct scsi_mode_select mcmd;
   1491 	struct scsi_mode_sense scmd;
   1492 	struct scsi_select {
   1493 		struct scsi_mode_header header;
   1494 		struct scsi_blk_desc blk_desc;
   1495 		u_char pdata[max(sizeof (struct scsi_tape_dev_conf_page),
   1496 			 sizeof (struct scsi_tape_dev_compression_page))];
   1497 	} scsi_pdata;
   1498 	struct scsi_tape_dev_conf_page *ptr;
   1499 	struct scsi_tape_dev_compression_page *cptr;
   1500 	struct scsipi_link *sc_link = st->sc_link;
   1501 	int error, ison, flags;
   1502 
   1503 	scsi_dlen = sizeof (scsi_pdata);
   1504 	bzero(&scsi_pdata, scsi_dlen);
   1505 
   1506 	/*
   1507 	 * Set up for a mode sense.
   1508 	 * Do DATA COMPRESSION page first.
   1509 	 */
   1510 	bzero(&scmd, sizeof (scmd));
   1511 	scmd.opcode = SCSI_MODE_SENSE;
   1512 	scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
   1513 	scmd.length = scsi_dlen;
   1514 
   1515 	flags = SCSI_SILENT;
   1516 
   1517 	/*
   1518 	 * Do the MODE SENSE command...
   1519 	 */
   1520 again:
   1521 	error = sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &scmd,
   1522 			sizeof(scmd), (u_char *) &scsi_pdata, scsi_dlen,
   1523 			ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN);
   1524 
   1525 	if (error) {
   1526 		if (scmd.byte2 != SMS_DBD) {
   1527 			scmd.byte2 = SMS_DBD;
   1528 			goto again;
   1529 		}
   1530 		return error;
   1531 	}
   1532 
   1533 	if (scsi_pdata.header.blk_desc_len) {
   1534 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
   1535 	} else {
   1536 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
   1537 	}
   1538 
   1539 	if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
   1540 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
   1541 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
   1542 		if (onoff)
   1543 			cptr->dce_dcc |= DCP_DCE;
   1544 		else
   1545 			cptr->dce_dcc &= ~DCP_DCE;
   1546 		cptr->pagecode &= ~0x80;
   1547 	} else {
   1548 		ison =  (ptr->sel_comp_alg != 0);
   1549 		if (onoff)
   1550 			ptr->sel_comp_alg = 1;
   1551 		else
   1552 			ptr->sel_comp_alg = 0;
   1553 		ptr->pagecode &= ~0x80;
   1554 	}
   1555 	onoff = (onoff)? 1 : 0;
   1556 	/*
   1557 	 * There might be a virtue in actually doing the MODE SELECTS,
   1558 	 * but let's not clog the bus over it.
   1559 	 */
   1560 	if (onoff == ison)
   1561 		return (0);
   1562 
   1563 	/*
   1564 	 * Set up for a mode select
   1565 	 */
   1566 
   1567 	scsi_pdata.header.data_length = 0;
   1568 	scsi_pdata.header.medium_type = 0;
   1569 	if ((st->flags & ST_DONTBUFFER) == 0)
   1570 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
   1571 	else
   1572 		scsi_pdata.header.dev_spec = 0;
   1573 
   1574 	bzero(&mcmd, sizeof(mcmd));
   1575 	mcmd.opcode = SCSI_MODE_SELECT;
   1576 	mcmd.byte2 = SMS_PF;
   1577 	mcmd.length = scsi_dlen;
   1578 	if (scsi_pdata.header.blk_desc_len) {
   1579 		scsi_pdata.blk_desc.density = 0;
   1580 		scsi_pdata.blk_desc.nblocks[0] = 0;
   1581 		scsi_pdata.blk_desc.nblocks[1] = 0;
   1582 		scsi_pdata.blk_desc.nblocks[2] = 0;
   1583 	}
   1584 
   1585 	/*
   1586 	 * do the command
   1587 	 */
   1588 	error = sc_link->scsipi_cmd(sc_link, (struct scsipi_generic *) &mcmd,
   1589 	    sizeof(mcmd), (u_char *) &scsi_pdata, scsi_dlen,
   1590 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
   1591 
   1592 	if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
   1593 		/*
   1594 		 * Try DEVICE CONFIGURATION page.
   1595 		 */
   1596 		scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
   1597 		goto again;
   1598 	}
   1599 	return (error);
   1600 }
   1601 /*
   1602  * issue an erase command
   1603  */
   1604 int
   1605 st_erase(st, full, flags)
   1606 	struct st_softc *st;
   1607 	int full, flags;
   1608 {
   1609 	struct scsi_erase cmd;
   1610 
   1611 	/*
   1612 	 * Full erase means set LONG bit in erase command, which asks
   1613 	 * the drive to erase the entire unit.  Without this bit, we're
   1614 	 * asking the drive to write an erase gap.
   1615 	 */
   1616 	bzero(&cmd, sizeof(cmd));
   1617 	cmd.opcode = ERASE;
   1618 	if (full)
   1619 		cmd.byte2 = SE_IMMED|SE_LONG;
   1620 	else
   1621 		cmd.byte2 = SE_IMMED;
   1622 
   1623 	/*
   1624 	 * XXX We always do this asynchronously, for now.  How long should
   1625 	 * we wait if we want to (eventually) to it synchronously?
   1626 	 */
   1627 	return (st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *)&cmd,
   1628 	    sizeof(cmd), 0, 0, ST_RETRIES, 5000, NULL, flags));
   1629 }
   1630 
   1631 /*
   1632  * skip N blocks/filemarks/seq filemarks/eom
   1633  */
   1634 int
   1635 st_space(st, number, what, flags)
   1636 	struct st_softc *st;
   1637 	u_int what;
   1638 	int flags;
   1639 	int number;
   1640 {
   1641 	struct scsi_space cmd;
   1642 	int error;
   1643 
   1644 	switch (what) {
   1645 	case SP_BLKS:
   1646 		if (st->flags & ST_PER_ACTION) {
   1647 			if (number > 0) {
   1648 				st->flags &= ~ST_PER_ACTION;
   1649 				return EIO;
   1650 			} else if (number < 0) {
   1651 				if (st->flags & ST_AT_FILEMARK) {
   1652 					/*
   1653 					 * Handling of ST_AT_FILEMARK
   1654 					 * in st_space will fill in the
   1655 					 * right file mark count.
   1656 					 */
   1657 					error = st_space(st, 0, SP_FILEMARKS,
   1658 						flags);
   1659 					if (error)
   1660 						return error;
   1661 				}
   1662 				if (st->flags & ST_BLANK_READ) {
   1663 					st->flags &= ~ST_BLANK_READ;
   1664 					return EIO;
   1665 				}
   1666 				st->flags &= ~ST_EIO_PENDING;
   1667 			}
   1668 		}
   1669 		break;
   1670 	case SP_FILEMARKS:
   1671 		if (st->flags & ST_EIO_PENDING) {
   1672 			if (number > 0) {
   1673 				/* pretend we just discovered the error */
   1674 				st->flags &= ~ST_EIO_PENDING;
   1675 				return EIO;
   1676 			} else if (number < 0) {
   1677 				/* back away from the error */
   1678 				st->flags &= ~ST_EIO_PENDING;
   1679 			}
   1680 		}
   1681 		if (st->flags & ST_AT_FILEMARK) {
   1682 			st->flags &= ~ST_AT_FILEMARK;
   1683 			number--;
   1684 		}
   1685 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1686 			/* back away from unwritten tape */
   1687 			st->flags &= ~ST_BLANK_READ;
   1688 			number++;	/* XXX dubious */
   1689 		}
   1690 		break;
   1691 	case SP_EOM:
   1692 		if (st->flags & ST_EIO_PENDING) {
   1693 			/* pretend we just discovered the error */
   1694 			st->flags &= ~ST_EIO_PENDING;
   1695 			return EIO;
   1696 		}
   1697 		if (st->flags & ST_AT_FILEMARK)
   1698 			st->flags &= ~ST_AT_FILEMARK;
   1699 		break;
   1700 	}
   1701 	if (number == 0)
   1702 		return 0;
   1703 
   1704 	bzero(&cmd, sizeof(cmd));
   1705 	cmd.opcode = SPACE;
   1706 	cmd.byte2 = what;
   1707 	_lto3b(number, cmd.number);
   1708 
   1709 	return st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *) &cmd,
   1710 	    sizeof(cmd), 0, 0, 0, 900000, NULL, flags);
   1711 }
   1712 
   1713 /*
   1714  * write N filemarks
   1715  */
   1716 int
   1717 st_write_filemarks(st, number, flags)
   1718 	struct st_softc *st;
   1719 	int flags;
   1720 	int number;
   1721 {
   1722 	struct scsi_write_filemarks cmd;
   1723 
   1724 	/*
   1725 	 * It's hard to write a negative number of file marks.
   1726 	 * Don't try.
   1727 	 */
   1728 	if (number < 0)
   1729 		return EINVAL;
   1730 	switch (number) {
   1731 	case 0:		/* really a command to sync the drive's buffers */
   1732 		break;
   1733 	case 1:
   1734 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1735 			st->flags &= ~ST_WRITTEN;
   1736 		else
   1737 			st->flags |= ST_FM_WRITTEN;
   1738 		st->flags &= ~ST_PER_ACTION;
   1739 		break;
   1740 	default:
   1741 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1742 	}
   1743 
   1744 	bzero(&cmd, sizeof(cmd));
   1745 	cmd.opcode = WRITE_FILEMARKS;
   1746 	_lto3b(number, cmd.number);
   1747 
   1748 	return st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *) &cmd,
   1749 	    sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
   1750 }
   1751 
   1752 /*
   1753  * Make sure the right number of file marks is on tape if the
   1754  * tape has been written.  If the position argument is true,
   1755  * leave the tape positioned where it was originally.
   1756  *
   1757  * nmarks returns the number of marks to skip (or, if position
   1758  * true, which were skipped) to get back original position.
   1759  */
   1760 int
   1761 st_check_eod(st, position, nmarks, flags)
   1762 	struct st_softc *st;
   1763 	boolean position;
   1764 	int *nmarks;
   1765 	int flags;
   1766 {
   1767 	int error;
   1768 
   1769 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1770 	default:
   1771 		*nmarks = 0;
   1772 		return 0;
   1773 	case ST_WRITTEN:
   1774 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1775 		*nmarks = 1;
   1776 		break;
   1777 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1778 		*nmarks = 2;
   1779 	}
   1780 	error = st_write_filemarks(st, *nmarks, flags);
   1781 	if (position && !error)
   1782 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1783 	return error;
   1784 }
   1785 
   1786 /*
   1787  * load/unload/retension
   1788  */
   1789 int
   1790 st_load(st, type, flags)
   1791 	struct st_softc *st;
   1792 	u_int type;
   1793 	int flags;
   1794 {
   1795 	struct scsi_load cmd;
   1796 
   1797 	if (type != LD_LOAD) {
   1798 		int error;
   1799 		int nmarks;
   1800 
   1801 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1802 		if (error)
   1803 			return error;
   1804 	}
   1805 	if (st->quirks & ST_Q_IGNORE_LOADS)
   1806 		return 0;
   1807 
   1808 	bzero(&cmd, sizeof(cmd));
   1809 	cmd.opcode = LOAD;
   1810 	cmd.how = type;
   1811 
   1812 	return st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *) &cmd,
   1813 	    sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
   1814 }
   1815 
   1816 /*
   1817  *  Rewind the device
   1818  */
   1819 int
   1820 st_rewind(st, immediate, flags)
   1821 	struct st_softc *st;
   1822 	u_int immediate;
   1823 	int flags;
   1824 {
   1825 	struct scsi_rewind cmd;
   1826 	int error;
   1827 	int nmarks;
   1828 
   1829 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1830 	if (error)
   1831 		return error;
   1832 	st->flags &= ~ST_PER_ACTION;
   1833 
   1834 	bzero(&cmd, sizeof(cmd));
   1835 	cmd.opcode = REWIND;
   1836 	cmd.byte2 = immediate;
   1837 
   1838 	return st->sc_link->scsipi_cmd(st->sc_link, (struct scsipi_generic *) &cmd,
   1839 	    sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
   1840 	    flags);
   1841 }
   1842 
   1843 int
   1844 st_rdpos(st, hard, blkptr)
   1845 	struct st_softc *st;
   1846 	int hard;
   1847 	u_int32_t *blkptr;
   1848 {
   1849 	int error;
   1850 	u_int8_t posdata[20];
   1851 	struct scsi_tape_read_position cmd;
   1852 
   1853 	/*
   1854 	 * First flush any pending writes...
   1855 	 */
   1856 	error = st_write_filemarks(st, 0, SCSI_SILENT);
   1857 	if (error)
   1858 		return (error);
   1859 
   1860 	bzero(&cmd, sizeof (cmd));
   1861 	bzero(&posdata, sizeof (posdata));
   1862 	cmd.opcode = READ_POSITION;
   1863 	if (hard)
   1864 		cmd.byte1 = 1;
   1865 
   1866 	error = st->sc_link->scsipi_cmd(st->sc_link,
   1867 	    (struct scsipi_generic *) &cmd, sizeof (cmd), (u_char *) &posdata,
   1868 	    sizeof (posdata), ST_RETRIES, 30000, NULL, SCSI_DATA_IN);
   1869 
   1870 	if (error == 0) {
   1871 
   1872 #if	0
   1873 		printf("posdata:");
   1874 		for (hard = 0; hard < sizeof (posdata); hard++)
   1875 			printf("%02x ", posdata[hard] & 0xff);
   1876 		printf("\n");
   1877 #endif
   1878 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   1879 			error = EINVAL;
   1880 		else {
   1881 			*blkptr = _4btol(&posdata[4]);
   1882 		}
   1883 	}
   1884 	return (error);
   1885 }
   1886 
   1887 int
   1888 st_setpos(st, hard, blkptr)
   1889 	struct st_softc *st;
   1890 	int hard;
   1891 	u_int32_t *blkptr;
   1892 {
   1893 	int error;
   1894 	struct scsipi_generic cmd;
   1895 
   1896 	/*
   1897 	 * First flush any pending writes...
   1898 	 */
   1899 	error = st_write_filemarks(st, 0, SCSI_SILENT);
   1900 	if (error)
   1901 		return (error);
   1902 
   1903 	bzero(&cmd, sizeof (cmd));
   1904 	cmd.opcode = LOCATE;
   1905 	if (hard)
   1906 		cmd.bytes[0] = 1 << 2;
   1907 	_lto4b(*blkptr, &cmd.bytes[2]);
   1908 	error = st->sc_link->scsipi_cmd(st->sc_link, &cmd, sizeof (cmd), NULL,
   1909 		0, ST_RETRIES, 5000, NULL, 0);
   1910 	/*
   1911 	 * XXX: Note file && block number position now unknown (if
   1912 	 * XXX: these things ever start being maintained in this driver)
   1913 	 */
   1914 	return (error);
   1915 }
   1916 
   1917 
   1918 /*
   1919  * Look at the returned sense and act on the error and detirmine
   1920  * The unix error number to pass back... (0 = report no error)
   1921  *                            (-1 = continue processing)
   1922  */
   1923 int
   1924 st_interpret_sense(xs)
   1925 	struct scsipi_xfer *xs;
   1926 {
   1927 	struct scsipi_link *sc_link = xs->sc_link;
   1928 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   1929 	struct buf *bp = xs->bp;
   1930 	struct st_softc *st = sc_link->device_softc;
   1931 	u_int8_t key;
   1932 	int32_t info;
   1933 
   1934 	/*
   1935 	 * Get the sense fields and work out what code
   1936 	 */
   1937 	if (sense->error_code & SSD_ERRCODE_VALID)
   1938 		info = _4btol(sense->info);
   1939 	else
   1940 		info = xs->datalen;	/* bad choice if fixed blocks */
   1941 	if ((sense->error_code & SSD_ERRCODE) != 0x70)
   1942 		return -1;	/* let the generic code handle it */
   1943 #ifdef SCSIVERBOSE
   1944 	else if ((xs->flags & SCSI_SILENT) == 0)
   1945 		scsi_print_sense(xs, 0);    /* tell folks what happened */
   1946 #endif
   1947 	if (st->flags & ST_FIXEDBLOCKS) {
   1948 		xs->resid = info * st->blksize;
   1949 		if (sense->flags & SSD_EOM) {
   1950 			st->flags |= ST_EIO_PENDING;
   1951 			if (bp)
   1952 				bp->b_resid = xs->resid;
   1953 		}
   1954 		if (sense->flags & SSD_FILEMARK) {
   1955 			st->flags |= ST_AT_FILEMARK;
   1956 			if (bp)
   1957 				bp->b_resid = xs->resid;
   1958 		}
   1959 		if (sense->flags & SSD_ILI) {
   1960 			st->flags |= ST_EIO_PENDING;
   1961 			if (bp)
   1962 				bp->b_resid = xs->resid;
   1963 			if (sense->error_code & SSD_ERRCODE_VALID &&
   1964 			    (xs->flags & SCSI_SILENT) == 0)
   1965 				printf("%s: block wrong size, %d blocks residual\n",
   1966 				    st->sc_dev.dv_xname, info);
   1967 
   1968 			/*
   1969 			 * This quirk code helps the drive read
   1970 			 * the first tape block, regardless of
   1971 			 * format.  That is required for these
   1972 			 * drives to return proper MODE SENSE
   1973 			 * information.
   1974 			 */
   1975 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   1976 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   1977 				st->blksize -= 512;
   1978 		}
   1979 		/*
   1980 		 * If no data was tranfered, do it immediatly
   1981 		 */
   1982 		if (xs->resid >= xs->datalen) {
   1983 			if (st->flags & ST_EIO_PENDING)
   1984 				return EIO;
   1985 			if (st->flags & ST_AT_FILEMARK) {
   1986 				if (bp)
   1987 					bp->b_resid = xs->resid;
   1988 				return 0;
   1989 			}
   1990 		}
   1991 	} else {		/* must be variable mode */
   1992 		xs->resid = xs->datalen;	/* to be sure */
   1993 		if (sense->flags & SSD_EOM)
   1994 			return EIO;
   1995 		if (sense->flags & SSD_FILEMARK) {
   1996 			if (bp)
   1997 				bp->b_resid = bp->b_bcount;
   1998 			return 0;
   1999 		}
   2000 		if (sense->flags & SSD_ILI) {
   2001 			if (info < 0) {
   2002 				/*
   2003 				 * the record was bigger than the read
   2004 				 */
   2005 				if ((xs->flags & SCSI_SILENT) == 0)
   2006 					printf("%s: %d-byte record too big\n",
   2007 					    st->sc_dev.dv_xname,
   2008 					    xs->datalen - info);
   2009 				return EIO;
   2010 			}
   2011 			xs->resid = info;
   2012 			if (bp)
   2013 				bp->b_resid = info;
   2014 		}
   2015 	}
   2016 	key = sense->flags & SSD_KEY;
   2017 
   2018 	if (key == 0x8) {
   2019 		/*
   2020 		 * This quirk code helps the drive read the
   2021 		 * first tape block, regardless of format.  That
   2022 		 * is required for these drives to return proper
   2023 		 * MODE SENSE information.
   2024 		 */
   2025 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2026 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   2027 			/* still starting */
   2028 			st->blksize -= 512;
   2029 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2030 			st->flags |= ST_BLANK_READ;
   2031 			xs->resid = xs->datalen;
   2032 			if (bp) {
   2033 				bp->b_resid = xs->resid;
   2034 				/* return an EOF */
   2035 			}
   2036 			return 0;
   2037 		}
   2038 	}
   2039 	return -1;		/* let the default/generic handler handle it */
   2040 }
   2041 
   2042 /*
   2043  * The quirk here is that the drive returns some value to st_mode_sense
   2044  * incorrectly until the tape has actually passed by the head.
   2045  *
   2046  * The method is to set the drive to large fixed-block state (user-specified
   2047  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2048  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2049  * work, as a last resort, try variable- length blocks.  The result will be
   2050  * the ability to do an accurate st_mode_sense.
   2051  *
   2052  * We know we can do a rewind because we just did a load, which implies rewind.
   2053  * Rewind seems preferable to space backward if we have a virgin tape.
   2054  *
   2055  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2056  * error processing, both part of st_interpret_sense.
   2057  */
   2058 int
   2059 st_touch_tape(st)
   2060 	struct st_softc *st;
   2061 {
   2062 	char *buf;
   2063 	int readsize;
   2064 	int error;
   2065 
   2066 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   2067 	if (!buf)
   2068 		return ENOMEM;
   2069 
   2070 	if ((error = st_mode_sense(st, 0)) != 0)
   2071 		goto bad;
   2072 	st->blksize = 1024;
   2073 	do {
   2074 		switch (st->blksize) {
   2075 		case 512:
   2076 		case 1024:
   2077 			readsize = st->blksize;
   2078 			st->flags |= ST_FIXEDBLOCKS;
   2079 			break;
   2080 		default:
   2081 			readsize = 1;
   2082 			st->flags &= ~ST_FIXEDBLOCKS;
   2083 		}
   2084 		if ((error = st_mode_select(st, 0)) != 0)
   2085 			goto bad;
   2086 		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
   2087 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2088 bad:			free(buf, M_TEMP);
   2089 			return error;
   2090 		}
   2091 	} while (readsize != 1 && readsize > st->blksize);
   2092 
   2093 	free(buf, M_TEMP);
   2094 	return 0;
   2095 }
   2096 
   2097 int
   2098 stdump(dev, blkno, va, size)
   2099 	dev_t dev;
   2100 	daddr_t blkno;
   2101 	caddr_t va;
   2102 	size_t size;
   2103 {
   2104 
   2105 	/* Not implemented. */
   2106 	return ENXIO;
   2107 }
   2108