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