Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.202.18.3
      1 /*	$NetBSD: st.c,v 1.202.18.3 2008/06/29 09:33:10 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.3 2008/06/29 09:33:10 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%x (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 immediatly
   1101 	 */
   1102 	if (bp->b_bcount == 0)
   1103 		goto done;
   1104 
   1105 	/* If offset is negative, error */
   1106 	if (bp->b_blkno < 0) {
   1107 		bp->b_error = EINVAL;
   1108 		goto done;
   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 done;
   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 done;
   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 done:
   1151 	/*
   1152 	 * Correctly set the buf to indicate a completed xfer
   1153 	 */
   1154 	bp->b_resid = bp->b_bcount;
   1155 	biodone(bp);
   1156 	return;
   1157 }
   1158 
   1159 /*
   1160  * ststart looks to see if there is a buf waiting for the device
   1161  * and that the device is not already busy. If both are true,
   1162  * It dequeues the buf and creates a scsi command to perform the
   1163  * transfer required. The transfer request will call scsipi_done
   1164  * on completion, which will in turn call this routine again
   1165  * so that the next queued transfer is performed.
   1166  * The bufs are queued by the strategy routine (ststrategy)
   1167  *
   1168  * This routine is also called after other non-queued requests
   1169  * have been made of the scsi driver, to ensure that the queue
   1170  * continues to be drained.
   1171  * ststart() is called at splbio
   1172  */
   1173 static void
   1174 ststart(struct scsipi_periph *periph)
   1175 {
   1176 	struct st_softc *st = (void *)periph->periph_dev;
   1177 	struct buf *bp;
   1178 	struct scsi_rw_tape cmd;
   1179 	struct scsipi_xfer *xs;
   1180 	int flags, error;
   1181 
   1182 	SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
   1183 	/*
   1184 	 * See if there is a buf to do and we are not already
   1185 	 * doing one
   1186 	 */
   1187 	while (periph->periph_active < periph->periph_openings) {
   1188 		/* if a special awaits, let it proceed first */
   1189 		if (periph->periph_flags & PERIPH_WAITING) {
   1190 			periph->periph_flags &= ~PERIPH_WAITING;
   1191 			wakeup((void *)periph);
   1192 			return;
   1193 		}
   1194 
   1195 		/*
   1196 		 * If the device has been unmounted by the user
   1197 		 * then throw away all requests until done.
   1198 		 */
   1199 		if (__predict_false((st->flags & ST_MOUNTED) == 0 ||
   1200 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
   1201 			if ((bp = BUFQ_GET(st->buf_queue)) != NULL) {
   1202 				/* make sure that one implies the other.. */
   1203 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
   1204 				bp->b_error = EIO;
   1205 				bp->b_resid = bp->b_bcount;
   1206 				biodone(bp);
   1207 				continue;
   1208 			} else {
   1209 				return;
   1210 			}
   1211 		}
   1212 
   1213 		if ((bp = BUFQ_PEEK(st->buf_queue)) == NULL)
   1214 			return;
   1215 
   1216 		iostat_busy(st->stats);
   1217 
   1218 		/*
   1219 		 * only FIXEDBLOCK devices have pending I/O or space operations.
   1220 		 */
   1221 		if (st->flags & ST_FIXEDBLOCKS) {
   1222 			/*
   1223 			 * If we are at a filemark but have not reported it yet
   1224 			 * then we should report it now
   1225 			 */
   1226 			if (st->flags & ST_AT_FILEMARK) {
   1227 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1228 					/*
   1229 					 * Handling of ST_AT_FILEMARK in
   1230 					 * st_space will fill in the right file
   1231 					 * mark count.
   1232 					 * Back up over filemark
   1233 					 */
   1234 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
   1235 						BUFQ_GET(st->buf_queue);
   1236 						bp->b_error = EIO;
   1237 						bp->b_resid = bp->b_bcount;
   1238 						biodone(bp);
   1239 						continue;
   1240 					}
   1241 				} else {
   1242 					BUFQ_GET(st->buf_queue);
   1243 					bp->b_resid = bp->b_bcount;
   1244 					bp->b_error = 0;
   1245 					st->flags &= ~ST_AT_FILEMARK;
   1246 					biodone(bp);
   1247 					continue;	/* seek more work */
   1248 				}
   1249 			}
   1250 		}
   1251 		/*
   1252 		 * If we are at EOM but have not reported it
   1253 		 * yet then we should report it now.
   1254 		 */
   1255 		if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
   1256 			BUFQ_GET(st->buf_queue);
   1257 			bp->b_resid = bp->b_bcount;
   1258 			if (st->flags & ST_EIO_PENDING)
   1259 				bp->b_error = EIO;
   1260 			st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
   1261 			biodone(bp);
   1262 			continue;	/* seek more work */
   1263 		}
   1264 
   1265 		/*
   1266 		 * Fill out the scsi command
   1267 		 */
   1268 		memset(&cmd, 0, sizeof(cmd));
   1269 		flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
   1270 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1271 			cmd.opcode = WRITE;
   1272 			st->flags &= ~ST_FM_WRITTEN;
   1273 			flags |= XS_CTL_DATA_OUT;
   1274 		} else {
   1275 			cmd.opcode = READ;
   1276 			flags |= XS_CTL_DATA_IN;
   1277 		}
   1278 
   1279 		/*
   1280 		 * Handle "fixed-block-mode" tape drives by using the
   1281 		 * block count instead of the length.
   1282 		 */
   1283 		if (st->flags & ST_FIXEDBLOCKS) {
   1284 			cmd.byte2 |= SRW_FIXED;
   1285 			_lto3b(bp->b_bcount / st->blksize, cmd.len);
   1286 		} else
   1287 			_lto3b(bp->b_bcount, cmd.len);
   1288 
   1289 		/*
   1290 		 * Clear 'position updated' indicator
   1291 		 */
   1292 		st->flags &= ~ST_POSUPDATED;
   1293 
   1294 		/*
   1295 		 * go ask the adapter to do all this for us
   1296 		 */
   1297 		xs = scsipi_make_xs(periph,
   1298 		    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1299 		    (u_char *)bp->b_data, bp->b_bcount,
   1300 		    0, ST_IO_TIME, bp, flags);
   1301 		if (__predict_false(xs == NULL)) {
   1302 			/*
   1303 			 * out of memory. Keep this buffer in the queue, and
   1304 			 * retry later.
   1305 			 */
   1306 			callout_reset(&st->sc_callout, hz / 2, strestart,
   1307 			    periph);
   1308 			return;
   1309 		}
   1310 		/*
   1311 		 * need to dequeue the buffer before queuing the command,
   1312 		 * because cdstart may be called recursively from the
   1313 		 * HBA driver
   1314 		 */
   1315 #ifdef DIAGNOSTIC
   1316 		if (BUFQ_GET(st->buf_queue) != bp)
   1317 			panic("ststart(): dequeued wrong buf");
   1318 #else
   1319 		BUFQ_GET(st->buf_queue);
   1320 #endif
   1321 		error = scsipi_execute_xs(xs);
   1322 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
   1323 		KASSERT(error == 0);
   1324 	} /* go back and see if we can cram more work in.. */
   1325 }
   1326 
   1327 static void
   1328 strestart(void *v)
   1329 {
   1330 	int s = splbio();
   1331 	ststart((struct scsipi_periph *)v);
   1332 	splx(s);
   1333 }
   1334 
   1335 
   1336 static void
   1337 stdone(struct scsipi_xfer *xs, int error)
   1338 {
   1339 	struct st_softc *st = (void *)xs->xs_periph->periph_dev;
   1340 	struct buf *bp = xs->bp;
   1341 
   1342 	if (bp) {
   1343 		bp->b_error = error;
   1344 		bp->b_resid = xs->resid;
   1345 		/*
   1346 		 * buggy device ? A SDLT320 can report an info
   1347 		 * field of 0x3de8000 on a Media Error/Write Error
   1348 		 * for this CBD: 0x0a 00 00 80 00 00
   1349 		 */
   1350 		if (bp->b_resid > bp->b_bcount || bp->b_resid < 0)
   1351 			bp->b_resid = bp->b_bcount;
   1352 
   1353 		if ((bp->b_flags & B_READ) == B_WRITE)
   1354 			st->flags |= ST_WRITTEN;
   1355 		else
   1356 			st->flags &= ~ST_WRITTEN;
   1357 
   1358 		iostat_unbusy(st->stats, bp->b_bcount,
   1359 			     ((bp->b_flags & B_READ) == B_READ));
   1360 
   1361 #if NRND > 0
   1362 		rnd_add_uint32(&st->rnd_source, bp->b_blkno);
   1363 #endif
   1364 
   1365 		if ((st->flags & ST_POSUPDATED) == 0) {
   1366 			if (error) {
   1367 				st->fileno = st->blkno = -1;
   1368 			} else if (st->blkno != -1) {
   1369 				if (st->flags & ST_FIXEDBLOCKS)
   1370 					st->blkno +=
   1371 					    (bp->b_bcount / st->blksize);
   1372 				else
   1373 					st->blkno++;
   1374 			}
   1375 		}
   1376 
   1377 		biodone(bp);
   1378 	}
   1379 }
   1380 
   1381 static int
   1382 stread(dev_t dev, struct uio *uio, int iomode)
   1383 {
   1384 	struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
   1385 
   1386 	return (physio(ststrategy, NULL, dev, B_READ,
   1387 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1388 }
   1389 
   1390 static int
   1391 stwrite(dev_t dev, struct uio *uio, int iomode)
   1392 {
   1393 	struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
   1394 
   1395 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1396 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1397 }
   1398 
   1399 /*
   1400  * Perform special action on behalf of the user;
   1401  * knows about the internals of this device
   1402  */
   1403 static int
   1404 stioctl(dev_t dev, u_long cmd, void *arg, int flag, struct lwp *l)
   1405 {
   1406 	int error = 0;
   1407 	int unit;
   1408 	int number, nmarks, dsty;
   1409 	int flags;
   1410 	struct st_softc *st;
   1411 	int hold_blksize;
   1412 	u_int8_t hold_density;
   1413 	struct mtop *mt = (struct mtop *) arg;
   1414 
   1415 	/*
   1416 	 * Find the device that the user is talking about
   1417 	 */
   1418 	flags = 0;		/* give error messages, act on errors etc. */
   1419 	unit = STUNIT(dev);
   1420 	dsty = STDSTY(dev);
   1421 	st = device_lookup_private(&st_cd, unit);
   1422 	hold_blksize = st->blksize;
   1423 	hold_density = st->density;
   1424 
   1425 	switch ((u_int) cmd) {
   1426 
   1427 	case MTIOCGET: {
   1428 		struct mtget *g = (struct mtget *) arg;
   1429 		/*
   1430 		 * (to get the current state of READONLY)
   1431 		 */
   1432 		error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
   1433 		if (error) {
   1434 			/*
   1435 			 * Ignore the error if in control mode;
   1436 			 * this is mandated by st(4).
   1437 			 */
   1438 			if (STMODE(dev) != CTRL_MODE)
   1439 				break;
   1440 			error = 0;
   1441 		}
   1442 		SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
   1443 		memset(g, 0, sizeof(struct mtget));
   1444 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1445 		g->mt_blksiz = st->blksize;
   1446 		g->mt_density = st->density;
   1447 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1448 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1449 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1450 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1451 		g->mt_mdensity[0] = st->modes[0].density;
   1452 		g->mt_mdensity[1] = st->modes[1].density;
   1453 		g->mt_mdensity[2] = st->modes[2].density;
   1454 		g->mt_mdensity[3] = st->modes[3].density;
   1455 		g->mt_fileno = st->fileno;
   1456 		g->mt_blkno = st->blkno;
   1457 		if (st->flags & ST_READONLY)
   1458 			g->mt_dsreg |= MT_DS_RDONLY;
   1459 		if (st->flags & ST_MOUNTED)
   1460 			g->mt_dsreg |= MT_DS_MOUNTED;
   1461 		g->mt_resid = st->mt_resid;
   1462 		g->mt_erreg = st->mt_erreg;
   1463 		/*
   1464 		 * clear latched errors.
   1465 		 */
   1466 		st->mt_resid = 0;
   1467 		st->mt_erreg = 0;
   1468 		st->asc = 0;
   1469 		st->ascq = 0;
   1470 		break;
   1471 	}
   1472 	case MTIOCTOP: {
   1473 
   1474 		SC_DEBUG(st->sc_periph, SCSIPI_DB1,
   1475 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
   1476 			mt->mt_count));
   1477 
   1478 		/* compat: in U*x it is a short */
   1479 		number = mt->mt_count;
   1480 		switch ((short) (mt->mt_op)) {
   1481 		case MTWEOF:	/* write an end-of-file record */
   1482 			error = st_write_filemarks(st, number, flags);
   1483 			break;
   1484 		case MTBSF:	/* backward space file */
   1485 			number = -number;
   1486 		case MTFSF:	/* forward space file */
   1487 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1488 			if (!error)
   1489 				error = st_space(st, number - nmarks,
   1490 				    SP_FILEMARKS, flags);
   1491 			break;
   1492 		case MTBSR:	/* backward space record */
   1493 			number = -number;
   1494 		case MTFSR:	/* forward space record */
   1495 			error = st_check_eod(st, true, &nmarks, flags);
   1496 			if (!error)
   1497 				error = st_space(st, number, SP_BLKS, flags);
   1498 			break;
   1499 		case MTREW:	/* rewind */
   1500 			error = st_rewind(st, 0, flags);
   1501 			break;
   1502 		case MTOFFL:	/* rewind and put the drive offline */
   1503 			st_unmount(st, EJECT);
   1504 			break;
   1505 		case MTNOP:	/* no operation, sets status only */
   1506 			break;
   1507 		case MTRETEN:	/* retension the tape */
   1508 			error = st_load(st, LD_RETENSION, flags);
   1509 			if (!error)
   1510 				error = st_load(st, LD_LOAD, flags);
   1511 			break;
   1512 		case MTEOM:	/* forward space to end of media */
   1513 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1514 			if (!error)
   1515 				error = st_space(st, 1, SP_EOM, flags);
   1516 			break;
   1517 		case MTCACHE:	/* enable controller cache */
   1518 			st->flags &= ~ST_DONTBUFFER;
   1519 			goto try_new_value;
   1520 		case MTNOCACHE:	/* disable controller cache */
   1521 			st->flags |= ST_DONTBUFFER;
   1522 			goto try_new_value;
   1523 		case MTERASE:	/* erase volume */
   1524 			error = st_erase(st, number, flags);
   1525 			break;
   1526 		case MTSETBSIZ:	/* Set block size for device */
   1527 #ifdef	NOTYET
   1528 			if (!(st->flags & ST_NEW_MOUNT)) {
   1529 				uprintf("re-mount tape before changing blocksize");
   1530 				error = EINVAL;
   1531 				break;
   1532 			}
   1533 #endif
   1534 			if (number == 0)
   1535 				st->flags &= ~ST_FIXEDBLOCKS;
   1536 			else {
   1537 				if ((st->blkmin || st->blkmax) &&
   1538 				    (number < st->blkmin ||
   1539 				    number > st->blkmax)) {
   1540 					error = EINVAL;
   1541 					break;
   1542 				}
   1543 				st->flags |= ST_FIXEDBLOCKS;
   1544 			}
   1545 			st->blksize = number;
   1546 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1547 			goto try_new_value;
   1548 
   1549 		case MTSETDNSTY:	/* Set density for device and mode */
   1550 			/*
   1551 			 * Any number >= 0 and <= 0xff is legal. Numbers
   1552 			 * above 0x80 are 'vendor unique'.
   1553 			 */
   1554 			if (number < 0 || number > 255) {
   1555 				error = EINVAL;
   1556 				break;
   1557 			} else
   1558 				st->density = number;
   1559 			goto try_new_value;
   1560 
   1561 		case MTCMPRESS:
   1562 			error = st->ops(st, (number == 0) ?
   1563 			    ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
   1564 			    XS_CTL_SILENT);
   1565 			break;
   1566 
   1567 		case MTEWARN:
   1568 			if (number)
   1569 				st->flags |= ST_EARLYWARN;
   1570 			else
   1571 				st->flags &= ~ST_EARLYWARN;
   1572 			break;
   1573 
   1574 		default:
   1575 			error = EINVAL;
   1576 		}
   1577 		break;
   1578 	}
   1579 	case MTIOCIEOT:
   1580 	case MTIOCEEOT:
   1581 		break;
   1582 
   1583 	case MTIOCRDSPOS:
   1584 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1585 		break;
   1586 
   1587 	case MTIOCRDHPOS:
   1588 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1589 		break;
   1590 
   1591 	case MTIOCSLOCATE:
   1592 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1593 		break;
   1594 
   1595 	case MTIOCHLOCATE:
   1596 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1597 		break;
   1598 
   1599 
   1600 	default:
   1601 		error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
   1602 					flag, l);
   1603 		break;
   1604 	}
   1605 	return (error);
   1606 /*-----------------------------*/
   1607 try_new_value:
   1608 	/*
   1609 	 * Check that the mode being asked for is aggreeable to the
   1610 	 * drive. If not, put it back the way it was.
   1611 	 *
   1612 	 * If in control mode, we can make (persistent) mode changes
   1613 	 * even if no medium is loaded (see st(4)).
   1614 	 */
   1615 	if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
   1616 	    (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
   1617 		/* put it back as it was */
   1618 		aprint_error_dev(&st->sc_dev, "cannot set selected mode\n");
   1619 		st->density = hold_density;
   1620 		st->blksize = hold_blksize;
   1621 		if (st->blksize)
   1622 			st->flags |= ST_FIXEDBLOCKS;
   1623 		else
   1624 			st->flags &= ~ST_FIXEDBLOCKS;
   1625 		return (error);
   1626 	}
   1627 	/*
   1628 	 * As the drive liked it, if we are setting a new default,
   1629 	 * set it into the structures as such.
   1630 	 *
   1631 	 * The means for deciding this are not finalised yet- but
   1632 	 * if the device was opened in Control Mode, the values
   1633 	 * are persistent now across mounts.
   1634 	 */
   1635 	if (STMODE(dev) == CTRL_MODE) {
   1636 		switch ((short) (mt->mt_op)) {
   1637 		case MTSETBSIZ:
   1638 			st->modes[dsty].blksize = st->blksize;
   1639 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1640 			break;
   1641 		case MTSETDNSTY:
   1642 			st->modes[dsty].density = st->density;
   1643 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1644 			break;
   1645 		}
   1646 	}
   1647 	return (0);
   1648 }
   1649 
   1650 /*
   1651  * Do a synchronous read.
   1652  */
   1653 static int
   1654 st_read(struct st_softc *st, char *bf, int size, int flags)
   1655 {
   1656 	struct scsi_rw_tape cmd;
   1657 
   1658 	/*
   1659 	 * If it's a null transfer, return immediatly
   1660 	 */
   1661 	if (size == 0)
   1662 		return (0);
   1663 	memset(&cmd, 0, sizeof(cmd));
   1664 	cmd.opcode = READ;
   1665 	if (st->flags & ST_FIXEDBLOCKS) {
   1666 		cmd.byte2 |= SRW_FIXED;
   1667 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1668 		    cmd.len);
   1669 	} else
   1670 		_lto3b(size, cmd.len);
   1671 	return (scsipi_command(st->sc_periph,
   1672 	    (void *)&cmd, sizeof(cmd), (void *)bf, size, 0, ST_IO_TIME, NULL,
   1673 	    flags | XS_CTL_DATA_IN));
   1674 }
   1675 
   1676 /*
   1677  * issue an erase command
   1678  */
   1679 static int
   1680 st_erase(struct st_softc *st, int full, int flags)
   1681 {
   1682 	int tmo;
   1683 	struct scsi_erase cmd;
   1684 
   1685 	/*
   1686 	 * Full erase means set LONG bit in erase command, which asks
   1687 	 * the drive to erase the entire unit.  Without this bit, we're
   1688 	 * asking the drive to write an erase gap.
   1689 	 */
   1690 	memset(&cmd, 0, sizeof(cmd));
   1691 	cmd.opcode = ERASE;
   1692 	if (full) {
   1693 		cmd.byte2 = SE_LONG;
   1694 		tmo = ST_SPC_TIME;
   1695 	} else {
   1696 		tmo = ST_IO_TIME;
   1697 	}
   1698 
   1699 	/*
   1700 	 * XXX We always do this asynchronously, for now, unless the device
   1701 	 * has the ST_Q_ERASE_NOIMM quirk.  How long should we wait if we
   1702 	 * want to (eventually) to it synchronously?
   1703 	 */
   1704 	if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
   1705 		cmd.byte2 |= SE_IMMED;
   1706 
   1707 	return (scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1708 	    ST_RETRIES, tmo, NULL, flags));
   1709 }
   1710 
   1711 /*
   1712  * skip N blocks/filemarks/seq filemarks/eom
   1713  */
   1714 static int
   1715 st_space(struct st_softc *st, int number, u_int what, int flags)
   1716 {
   1717 	struct scsi_space cmd;
   1718 	int error;
   1719 
   1720 	switch (what) {
   1721 	case SP_BLKS:
   1722 		if (st->flags & ST_PER_ACTION) {
   1723 			if (number > 0) {
   1724 				st->flags &= ~ST_PER_ACTION;
   1725 				return (EIO);
   1726 			} else if (number < 0) {
   1727 				if (st->flags & ST_AT_FILEMARK) {
   1728 					/*
   1729 					 * Handling of ST_AT_FILEMARK
   1730 					 * in st_space will fill in the
   1731 					 * right file mark count.
   1732 					 */
   1733 					error = st_space(st, 0, SP_FILEMARKS,
   1734 					    flags);
   1735 					if (error)
   1736 						return (error);
   1737 				}
   1738 				if (st->flags & ST_BLANK_READ) {
   1739 					st->flags &= ~ST_BLANK_READ;
   1740 					return (EIO);
   1741 				}
   1742 				st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
   1743 			}
   1744 		}
   1745 		break;
   1746 	case SP_FILEMARKS:
   1747 		if (st->flags & ST_EIO_PENDING) {
   1748 			if (number > 0) {
   1749 				/* pretend we just discovered the error */
   1750 				st->flags &= ~ST_EIO_PENDING;
   1751 				return (EIO);
   1752 			} else if (number < 0) {
   1753 				/* back away from the error */
   1754 				st->flags &= ~ST_EIO_PENDING;
   1755 			}
   1756 		}
   1757 		if (st->flags & ST_AT_FILEMARK) {
   1758 			st->flags &= ~ST_AT_FILEMARK;
   1759 			number--;
   1760 		}
   1761 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1762 			/* back away from unwritten tape */
   1763 			st->flags &= ~ST_BLANK_READ;
   1764 			number++;	/* XXX dubious */
   1765 		}
   1766 		break;
   1767 	case SP_EOM:
   1768 		if (st->flags & ST_EOM_PENDING) {
   1769 			/* we're already there */
   1770 			st->flags &= ~ST_EOM_PENDING;
   1771 			return (0);
   1772 		}
   1773 		if (st->flags & ST_EIO_PENDING) {
   1774 			/* pretend we just discovered the error */
   1775 			st->flags &= ~ST_EIO_PENDING;
   1776 			return (EIO);
   1777 		}
   1778 		if (st->flags & ST_AT_FILEMARK)
   1779 			st->flags &= ~ST_AT_FILEMARK;
   1780 		break;
   1781 	}
   1782 	if (number == 0)
   1783 		return (0);
   1784 
   1785 	memset(&cmd, 0, sizeof(cmd));
   1786 	cmd.opcode = SPACE;
   1787 	cmd.byte2 = what;
   1788 	_lto3b(number, cmd.number);
   1789 
   1790 	st->flags &= ~ST_POSUPDATED;
   1791 	st->last_ctl_resid = 0;
   1792 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1793 	    0, ST_SPC_TIME, NULL, flags);
   1794 
   1795 	if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
   1796 		number = number - st->last_ctl_resid;
   1797 		if (what == SP_BLKS) {
   1798 			if (st->blkno != -1) {
   1799 				st->blkno += number;
   1800 			}
   1801 		} else if (what == SP_FILEMARKS) {
   1802 			if (st->fileno != -1) {
   1803 				st->fileno += number;
   1804 				if (number > 0) {
   1805 					st->blkno = 0;
   1806 				} else if (number < 0) {
   1807 					st->blkno = -1;
   1808 				}
   1809 			}
   1810 		} else if (what == SP_EOM) {
   1811 			/*
   1812 			 * This loses us relative position.
   1813 			 */
   1814 			st->fileno = st->blkno = -1;
   1815 		}
   1816 	}
   1817 	return (error);
   1818 }
   1819 
   1820 /*
   1821  * write N filemarks
   1822  */
   1823 static int
   1824 st_write_filemarks(struct st_softc *st, int number, int flags)
   1825 {
   1826 	int error;
   1827 	struct scsi_write_filemarks cmd;
   1828 
   1829 	/*
   1830 	 * It's hard to write a negative number of file marks.
   1831 	 * Don't try.
   1832 	 */
   1833 	if (number < 0)
   1834 		return (EINVAL);
   1835 	switch (number) {
   1836 	case 0:		/* really a command to sync the drive's buffers */
   1837 		break;
   1838 	case 1:
   1839 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1840 			st->flags &= ~ST_WRITTEN;
   1841 		else
   1842 			st->flags |= ST_FM_WRITTEN;
   1843 		st->flags &= ~ST_PER_ACTION;
   1844 		break;
   1845 	default:
   1846 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1847 	}
   1848 
   1849 	memset(&cmd, 0, sizeof(cmd));
   1850 	cmd.opcode = WRITE_FILEMARKS;
   1851 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1852 		cmd.byte2 = SR_IMMED;
   1853 	/*
   1854 	 * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
   1855 	 * WRITE_FILEMARKS is still used to flush the buffer
   1856 	 */
   1857 	if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
   1858 		_lto3b(number, cmd.number);
   1859 
   1860 	/* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
   1861 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1862 	    0, ST_IO_TIME * 4, NULL, flags);
   1863 	if (error == 0 && st->fileno != -1) {
   1864 		st->fileno += number;
   1865 	}
   1866 	return (error);
   1867 }
   1868 
   1869 /*
   1870  * Make sure the right number of file marks is on tape if the
   1871  * tape has been written.  If the position argument is true,
   1872  * leave the tape positioned where it was originally.
   1873  *
   1874  * nmarks returns the number of marks to skip (or, if position
   1875  * true, which were skipped) to get back original position.
   1876  */
   1877 static int
   1878 st_check_eod(struct st_softc *st, boolean position, int *nmarks, int flags)
   1879 {
   1880 	int error;
   1881 
   1882 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1883 	default:
   1884 		*nmarks = 0;
   1885 		return (0);
   1886 	case ST_WRITTEN:
   1887 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1888 		*nmarks = 1;
   1889 		break;
   1890 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1891 		*nmarks = 2;
   1892 	}
   1893 	error = st_write_filemarks(st, *nmarks, flags);
   1894 	if (position && !error)
   1895 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1896 	return (error);
   1897 }
   1898 
   1899 /*
   1900  * load/unload/retension
   1901  */
   1902 static int
   1903 st_load(struct st_softc *st, u_int type, int flags)
   1904 {
   1905 	int error;
   1906 	struct scsi_load cmd;
   1907 
   1908 	if (type != LD_LOAD) {
   1909 		int nmarks;
   1910 
   1911 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1912 		if (error) {
   1913 			aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
   1914 			    "unload, errno=%d\n", error);
   1915 			return (error);
   1916 		}
   1917 	}
   1918 	if (st->quirks & ST_Q_IGNORE_LOADS) {
   1919 		if (type == LD_LOAD) {
   1920 			/*
   1921 			 * If we ignore loads, at least we should try a rewind.
   1922 			 */
   1923 			return st_rewind(st, 0, flags);
   1924 		}
   1925 		/* otherwise, we should do what's asked of us */
   1926 	}
   1927 
   1928 	memset(&cmd, 0, sizeof(cmd));
   1929 	cmd.opcode = LOAD;
   1930 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1931 		cmd.byte2 = SR_IMMED;
   1932 	cmd.how = type;
   1933 
   1934 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1935 	    ST_RETRIES, ST_SPC_TIME, NULL, flags);
   1936 	if (error) {
   1937 		aprint_error_dev(&st->sc_dev, "error %d in st_load (op %d)\n",
   1938 		    error, type);
   1939 	}
   1940 	return (error);
   1941 }
   1942 
   1943 /*
   1944  *  Rewind the device
   1945  */
   1946 static int
   1947 st_rewind(struct st_softc *st, u_int immediate, int flags)
   1948 {
   1949 	struct scsi_rewind cmd;
   1950 	int error;
   1951 	int nmarks;
   1952 	int timeout;
   1953 
   1954 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1955 	if (error) {
   1956 		aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
   1957 		    "rewind, errno=%d\n", error);
   1958 		return (error);
   1959 	}
   1960 	st->flags &= ~ST_PER_ACTION;
   1961 
   1962 	/* If requestor asked for immediate response, set a short timeout */
   1963 	timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
   1964 
   1965 	/*
   1966 	 * ATAPI tapes always need immediate to be set
   1967 	 */
   1968 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1969 		immediate = SR_IMMED;
   1970 
   1971 	memset(&cmd, 0, sizeof(cmd));
   1972 	cmd.opcode = REWIND;
   1973 	cmd.byte2 = immediate;
   1974 
   1975 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1976 	    ST_RETRIES, timeout, NULL, flags);
   1977 	if (error) {
   1978 		aprint_error_dev(&st->sc_dev, "error %d trying to rewind\n",
   1979 		    error);
   1980 		/* lost position */
   1981 		st->fileno = st->blkno = -1;
   1982 	} else {
   1983 		st->fileno = st->blkno = 0;
   1984 	}
   1985 	return (error);
   1986 }
   1987 
   1988 static int
   1989 st_rdpos(struct st_softc *st, int hard, u_int32_t *blkptr)
   1990 {
   1991 	int error;
   1992 	u_int8_t posdata[20];
   1993 	struct scsi_tape_read_position cmd;
   1994 
   1995 	/*
   1996 	 * We try and flush any buffered writes here if we were writing
   1997 	 * and we're trying to get hardware block position. It eats
   1998 	 * up performance substantially, but I'm wary of drive firmware.
   1999 	 *
   2000 	 * I think that *logical* block position is probably okay-
   2001 	 * but hardware block position might have to wait for data
   2002 	 * to hit media to be valid. Caveat Emptor.
   2003 	 */
   2004 
   2005 	if (hard && (st->flags & ST_WRITTEN)) {
   2006 		/*
   2007 		 * First flush any pending writes...
   2008 		 */
   2009 		error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2010 		/*
   2011 		 * The latter case is for 'write protected' tapes
   2012 		 * which are too stupid to recognize a zero count
   2013 		 * for writing filemarks as a no-op.
   2014 		 */
   2015 		if (error != 0 && error != EACCES && error != EROFS)
   2016 			return (error);
   2017 	}
   2018 
   2019 	memset(&cmd, 0, sizeof(cmd));
   2020 	memset(&posdata, 0, sizeof(posdata));
   2021 	cmd.opcode = READ_POSITION;
   2022 	if (hard)
   2023 		cmd.byte1 = 1;
   2024 
   2025 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd),
   2026 	    (void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
   2027 	    XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
   2028 
   2029 	if (error == 0) {
   2030 #if	0
   2031 		printf("posdata:");
   2032 		for (hard = 0; hard < sizeof(posdata); hard++)
   2033 			printf("%02x ", posdata[hard] & 0xff);
   2034 		printf("\n");
   2035 #endif
   2036 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   2037 			error = EINVAL;
   2038 		else
   2039 			*blkptr = _4btol(&posdata[4]);
   2040 	}
   2041 	return (error);
   2042 }
   2043 
   2044 static int
   2045 st_setpos(struct st_softc *st, int hard, u_int32_t *blkptr)
   2046 {
   2047 	int error;
   2048 	struct scsi_tape_locate cmd;
   2049 
   2050 	/*
   2051 	 * We used to try and flush any buffered writes here.
   2052 	 * Now we push this onto user applications to either
   2053 	 * flush the pending writes themselves (via a zero count
   2054 	 * WRITE FILEMARKS command) or they can trust their tape
   2055 	 * drive to do this correctly for them.
   2056 	 *
   2057 	 * There are very ugly performance limitations otherwise.
   2058 	 */
   2059 
   2060 	memset(&cmd, 0, sizeof(cmd));
   2061 	cmd.opcode = LOCATE;
   2062 	if (hard)
   2063 		cmd.byte2 = 1 << 2;
   2064 	_lto4b(*blkptr, cmd.blkaddr);
   2065 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   2066 	    ST_RETRIES, ST_SPC_TIME, NULL, 0);
   2067 	/*
   2068 	 * Note file && block number position now unknown (if
   2069 	 * these things ever start being maintained in this driver)
   2070 	 */
   2071 	st->fileno = st->blkno = -1;
   2072 	return (error);
   2073 }
   2074 
   2075 
   2076 /*
   2077  * Look at the returned sense and act on the error and determine
   2078  * the unix error number to pass back..., 0 (== report no error),
   2079  * -1 = retry the operation, -2 continue error processing.
   2080  */
   2081 static int
   2082 st_interpret_sense(struct scsipi_xfer *xs)
   2083 {
   2084 	struct scsipi_periph *periph = xs->xs_periph;
   2085 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
   2086 	struct buf *bp = xs->bp;
   2087 	struct st_softc *st = (void *)periph->periph_dev;
   2088 	int retval = EJUSTRETURN;
   2089 	int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
   2090 	u_int8_t key;
   2091 	int32_t info;
   2092 
   2093 	/*
   2094 	 * If it isn't a extended or extended/deferred error, let
   2095 	 * the generic code handle it.
   2096 	 */
   2097 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
   2098 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
   2099 		return (retval);
   2100 
   2101 	if (sense->response_code & SSD_RCODE_VALID)
   2102 		info = _4btol(sense->info);
   2103 	else
   2104 		info = (st->flags & ST_FIXEDBLOCKS) ?
   2105 		    xs->datalen / st->blksize : xs->datalen;
   2106 	key = SSD_SENSE_KEY(sense->flags);
   2107 	st->mt_erreg = key;
   2108 	st->asc = sense->asc;
   2109 	st->ascq = sense->ascq;
   2110 	st->mt_resid = (short) info;
   2111 
   2112 	if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
   2113 		/* Not Ready, Logical Unit Is in Process Of Becoming Ready */
   2114 		if (!callout_pending(&periph->periph_callout))
   2115 			scsipi_periph_freeze(periph, 1);
   2116 		callout_reset(&periph->periph_callout,
   2117 		    hz, scsipi_periph_timed_thaw, periph);
   2118 		return (ERESTART);
   2119 	}
   2120 
   2121 	/*
   2122 	 * If the device is not open yet, let generic handle
   2123 	 */
   2124 	if ((periph->periph_flags & PERIPH_OPEN) == 0) {
   2125 		return (retval);
   2126 	}
   2127 
   2128 	xs->resid = info;
   2129 	if (st->flags & ST_FIXEDBLOCKS) {
   2130 		if (bp) {
   2131 			xs->resid *= st->blksize;
   2132 			st->last_io_resid = xs->resid;
   2133 		} else {
   2134 			st->last_ctl_resid = xs->resid;
   2135 		}
   2136 		if (key == SKEY_VOLUME_OVERFLOW) {
   2137 			st->flags |= ST_EIO_PENDING;
   2138 			if (bp)
   2139 				bp->b_resid = xs->resid;
   2140 		} else if (sense->flags & SSD_EOM) {
   2141 			if ((st->flags & ST_EARLYWARN) == 0)
   2142 				st->flags |= ST_EIO_PENDING;
   2143 			st->flags |= ST_EOM_PENDING;
   2144 			if (bp) {
   2145 #if 0
   2146 				bp->b_resid = xs->resid;
   2147 #else
   2148 				/*
   2149 				 * Grotesque as it seems, the few times
   2150 				 * I've actually seen a non-zero resid,
   2151 				 * the tape drive actually lied and had
   2152 				 * written all the data!
   2153 				 */
   2154 				bp->b_resid = 0;
   2155 #endif
   2156 			}
   2157 		}
   2158 		if (sense->flags & SSD_FILEMARK) {
   2159 			st->flags |= ST_AT_FILEMARK;
   2160 			if (bp)
   2161 				bp->b_resid = xs->resid;
   2162 			if (st->fileno != (daddr_t) -1) {
   2163 				st->fileno++;
   2164 				st->blkno = 0;
   2165 				st->flags |= ST_POSUPDATED;
   2166 			}
   2167 		}
   2168 		if (sense->flags & SSD_ILI) {
   2169 			st->flags |= ST_EIO_PENDING;
   2170 			if (bp)
   2171 				bp->b_resid = xs->resid;
   2172 			if (sense->response_code & SSD_RCODE_VALID &&
   2173 			    (xs->xs_control & XS_CTL_SILENT) == 0)
   2174 				aprint_error_dev(&st->sc_dev, "block wrong size, %d blocks "
   2175 				    "residual\n", info);
   2176 
   2177 			/*
   2178 			 * This quirk code helps the drive read
   2179 			 * the first tape block, regardless of
   2180 			 * format.  That is required for these
   2181 			 * drives to return proper MODE SENSE
   2182 			 * information.
   2183 			 */
   2184 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   2185 			    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   2186 				st->blksize -= 512;
   2187 			else if ((st->flags & ST_POSUPDATED) == 0) {
   2188 				if (st->blkno != (daddr_t) -1) {
   2189 					st->blkno +=
   2190 					    (xs->datalen / st->blksize);
   2191 					st->flags |= ST_POSUPDATED;
   2192 				}
   2193 			}
   2194 		}
   2195 		/*
   2196 		 * If data wanted and no data was transferred, do it immediately
   2197 		 */
   2198 		if (xs->datalen && xs->resid >= xs->datalen) {
   2199 			if (st->flags & ST_EIO_PENDING)
   2200 				return (EIO);
   2201 			if (st->flags & ST_AT_FILEMARK) {
   2202 				if (bp)
   2203 					bp->b_resid = xs->resid;
   2204 				return (0);
   2205 			}
   2206 		}
   2207 	} else {		/* must be variable mode */
   2208 		if (bp) {
   2209 			st->last_io_resid = xs->resid;
   2210 		} else {
   2211 			st->last_ctl_resid = xs->resid;
   2212 		}
   2213 		if (sense->flags & SSD_EOM) {
   2214 			/*
   2215 			 * The current semantics of this
   2216 			 * driver requires EOM detection
   2217 			 * to return EIO unless early
   2218 			 * warning detection is enabled
   2219 			 * for variable mode (this is always
   2220 			 * on for fixed block mode).
   2221 			 */
   2222 			if (st->flags & ST_EARLYWARN) {
   2223 				st->flags |= ST_EOM_PENDING;
   2224 				retval = 0;
   2225 			} else {
   2226 				retval = EIO;
   2227 			}
   2228 
   2229 			/*
   2230 			 * If it's an unadorned EOM detection,
   2231 			 * suppress printing an error.
   2232 			 */
   2233 			if (key == SKEY_NO_SENSE) {
   2234 				doprint = 0;
   2235 			}
   2236 		} else if (sense->flags & SSD_FILEMARK) {
   2237 			retval = 0;
   2238 			if (st->fileno != (daddr_t) -1) {
   2239 				st->fileno++;
   2240 				st->blkno = 0;
   2241 				st->flags |= ST_POSUPDATED;
   2242 			}
   2243 		} else if (sense->flags & SSD_ILI) {
   2244 			if (info < 0) {
   2245 				/*
   2246 				 * The tape record was bigger than the read
   2247 				 * we issued.
   2248 				 */
   2249 				if ((xs->xs_control & XS_CTL_SILENT) == 0) {
   2250 					aprint_error_dev(&st->sc_dev,
   2251 					    "%d-byte tape record too big"
   2252 					    " for %d-byte user buffer\n",
   2253 					    xs->datalen - info, xs->datalen);
   2254 				}
   2255 				retval = EIO;
   2256 			} else {
   2257 				retval = 0;
   2258 				if (st->blkno != (daddr_t) -1) {
   2259 					st->blkno++;
   2260 					st->flags |= ST_POSUPDATED;
   2261 				}
   2262 			}
   2263 		}
   2264 		if (bp)
   2265 			bp->b_resid = info;
   2266 	}
   2267 
   2268 #ifndef SCSIPI_DEBUG
   2269 	if (retval == 0 && key == SKEY_NO_SENSE)
   2270 		doprint = 0;
   2271 #endif
   2272 	if (key == SKEY_BLANK_CHECK) {
   2273 		/*
   2274 		 * This quirk code helps the drive read the
   2275 		 * first tape block, regardless of format.  That
   2276 		 * is required for these drives to return proper
   2277 		 * MODE SENSE information.
   2278 		 */
   2279 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2280 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   2281 			/* still starting */
   2282 			st->blksize -= 512;
   2283 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2284 			st->flags |= ST_BLANK_READ;
   2285 			xs->resid = xs->datalen;
   2286 			if (bp) {
   2287 				bp->b_resid = xs->resid;
   2288 				/* return an EOF */
   2289 			}
   2290 			retval = 0;
   2291 			/* lost position */
   2292 			st->fileno = st->blkno = -1;
   2293 		}
   2294 	}
   2295 
   2296 	/*
   2297 	 * If generic sense processing will continue, we should not
   2298 	 * print sense info here.
   2299 	 */
   2300 	if (retval == EJUSTRETURN)
   2301 		doprint = 0;
   2302 
   2303 	if (doprint) {
   2304 #ifdef	SCSIVERBOSE
   2305 		scsipi_print_sense(xs, 0);
   2306 #else
   2307 		scsipi_printaddr(periph);
   2308 		printf("Sense Key 0x%02x", key);
   2309 		if ((sense->response_code & SSD_RCODE_VALID) != 0) {
   2310 			switch (key) {
   2311 			case SKEY_NOT_READY:
   2312 			case SKEY_ILLEGAL_REQUEST:
   2313 			case SKEY_UNIT_ATTENTION:
   2314 			case SKEY_DATA_PROTECT:
   2315 				break;
   2316 			case SKEY_VOLUME_OVERFLOW:
   2317 			case SKEY_BLANK_CHECK:
   2318 				printf(", requested size: %d (decimal)", info);
   2319 				break;
   2320 			case SKEY_ABORTED_COMMAND:
   2321 				if (xs->xs_retries)
   2322 					printf(", retrying");
   2323 				printf(", cmd 0x%x, info 0x%x",
   2324 				    xs->cmd->opcode, info);
   2325 				break;
   2326 			default:
   2327 				printf(", info = %d (decimal)", info);
   2328 			}
   2329 		}
   2330 		if (sense->extra_len != 0) {
   2331 			int n;
   2332 			printf(", data =");
   2333 			for (n = 0; n < sense->extra_len; n++)
   2334 				printf(" %02x", sense->csi[n]);
   2335 		}
   2336 		printf("\n");
   2337 #endif
   2338 	}
   2339 	return (retval);
   2340 }
   2341 
   2342 /*
   2343  * The quirk here is that the drive returns some value to st_mode_sense
   2344  * incorrectly until the tape has actually passed by the head.
   2345  *
   2346  * The method is to set the drive to large fixed-block state (user-specified
   2347  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2348  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2349  * work, as a last resort, try variable- length blocks.  The result will be
   2350  * the ability to do an accurate st_mode_sense.
   2351  *
   2352  * We know we can do a rewind because we just did a load, which implies rewind.
   2353  * Rewind seems preferable to space backward if we have a virgin tape.
   2354  *
   2355  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2356  * error processing, both part of st_interpret_sense.
   2357  */
   2358 static int
   2359 st_touch_tape(struct st_softc *st)
   2360 {
   2361 	char *bf;
   2362 	int readsize;
   2363 	int error;
   2364 
   2365 	bf = malloc(1024, M_TEMP, M_NOWAIT);
   2366 	if (bf == NULL)
   2367 		return (ENOMEM);
   2368 
   2369 	if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
   2370 		goto bad;
   2371 
   2372 	/*
   2373 	 * If the block size is already known from the
   2374 	 * sense data, use it. Else start probing at 1024.
   2375 	 */
   2376 	if (st->media_blksize > 0)
   2377 		st->blksize = st->media_blksize;
   2378 	else
   2379 		st->blksize = 1024;
   2380 
   2381 	do {
   2382 		switch (st->blksize) {
   2383 		case 512:
   2384 		case 1024:
   2385 			readsize = st->blksize;
   2386 			st->flags |= ST_FIXEDBLOCKS;
   2387 			break;
   2388 		default:
   2389 			readsize = 1;
   2390 			st->flags &= ~ST_FIXEDBLOCKS;
   2391 		}
   2392 		if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
   2393 		    != 0) {
   2394 			/*
   2395 			 * The device did not agree with the proposed
   2396 			 * block size. If we exhausted our options,
   2397 			 * return failure, else try another.
   2398 			 */
   2399 			if (readsize == 1)
   2400 				goto bad;
   2401 			st->blksize -= 512;
   2402 			continue;
   2403 		}
   2404 		st_read(st, bf, readsize, XS_CTL_SILENT);	/* XXX */
   2405 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2406 bad:			free(bf, M_TEMP);
   2407 			return (error);
   2408 		}
   2409 	} while (readsize != 1 && readsize > st->blksize);
   2410 
   2411 	free(bf, M_TEMP);
   2412 	return (0);
   2413 }
   2414 
   2415 static int
   2416 stdump(dev_t dev, daddr_t blkno, void *va,
   2417     size_t size)
   2418 {
   2419 
   2420 	/* Not implemented. */
   2421 	return (ENXIO);
   2422 }
   2423