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