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