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