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