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