Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.202.18.2
      1 /*	$NetBSD: st.c,v 1.202.18.2 2008/06/02 13:23:51 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.2 2008/06/02 13:23:51 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 	if (unit >= st_cd.cd_ndevs)
    568 		return (ENXIO);
    569 	st = st_cd.cd_devs[unit];
    570 	if (st == NULL)
    571 		return (ENXIO);
    572 
    573 	stmode = STMODE(dev);
    574 	dsty = STDSTY(dev);
    575 
    576 	periph = st->sc_periph;
    577 	adapt = periph->periph_channel->chan_adapter;
    578 
    579 	SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    580 	    unit, st_cd.cd_ndevs));
    581 
    582 
    583 	/*
    584 	 * Only allow one at a time
    585 	 */
    586 	if (periph->periph_flags & PERIPH_OPEN) {
    587 		aprint_error_dev(&st->sc_dev, "already open\n");
    588 		return (EBUSY);
    589 	}
    590 
    591 	if ((error = scsipi_adapter_addref(adapt)) != 0)
    592 		return (error);
    593 
    594 	/*
    595 	 * clear any latched errors.
    596 	 */
    597 	st->mt_resid = 0;
    598 	st->mt_erreg = 0;
    599 	st->asc = 0;
    600 	st->ascq = 0;
    601 
    602 	/*
    603 	 * Catch any unit attention errors. Be silent about this
    604 	 * unless we're already mounted. We ignore media change
    605 	 * if we're in control mode or not mounted yet.
    606 	 */
    607 	if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
    608 #ifdef SCSIDEBUG
    609 		sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
    610 #else
    611 		sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
    612 #endif
    613 	} else
    614 		sflags = 0;
    615 
    616 	/*
    617 	 * If we're already mounted or we aren't configured for
    618 	 * a mount delay, only try a test unit ready once. Otherwise,
    619 	 * try up to ST_MOUNT_DELAY times with a rest interval of
    620 	 * one second between each try.
    621 	 */
    622 
    623 	if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
    624 		ntries = 1;
    625 	} else {
    626 		ntries = ST_MOUNT_DELAY;
    627 	}
    628 
    629 	for (error = tries = 0; tries < ntries; tries++) {
    630 		int slpintr, oflags;
    631 
    632 		/*
    633 		 * If we had no error, or we're opening the control mode
    634 		 * device, we jump out right away.
    635 		 */
    636 
    637 		error = scsipi_test_unit_ready(periph, sflags);
    638 		if (error == 0 || stmode == CTRL_MODE) {
    639 			break;
    640 		}
    641 
    642 		/*
    643 		 * We had an error.
    644 		 *
    645 		 * If we're already mounted or we aren't configured for
    646 		 * a mount delay, or the error isn't a NOT READY error,
    647 		 * skip to the error exit now.
    648 		 */
    649 		if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
    650 		    (st->mt_key != SKEY_NOT_READY)) {
    651 			goto bad;
    652 		}
    653 
    654 		/*
    655 		 * clear any latched errors.
    656 		 */
    657 		st->mt_resid = 0;
    658 		st->mt_erreg = 0;
    659 		st->asc = 0;
    660 		st->ascq = 0;
    661 
    662 		/*
    663 		 * Fake that we have the device open so
    664 		 * we block other apps from getting in.
    665 		 */
    666 
    667 		oflags = periph->periph_flags;
    668 		periph->periph_flags |= PERIPH_OPEN;
    669 
    670 		slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
    671 
    672 		periph->periph_flags = oflags;	/* restore flags */
    673 		if (slpintr) {
    674 			goto bad;
    675 		}
    676 	}
    677 
    678 
    679 	/*
    680 	 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
    681 	 * been opened to set defaults and perform other, usually non-I/O
    682 	 * related, operations. In this case, do a quick check to see
    683 	 * whether the unit actually had a tape loaded (this will be known
    684 	 * as to whether or not we got a NOT READY for the above
    685 	 * unit attention). If a tape is there, go do a mount sequence.
    686 	 */
    687 	if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) {
    688 		periph->periph_flags |= PERIPH_OPEN;
    689 		return (0);
    690 	}
    691 
    692 	/*
    693 	 * If we get this far and had an error set, that means we failed
    694 	 * to pass the 'test unit ready' test for the non-controlmode device,
    695 	 * so we bounce the open.
    696 	 */
    697 
    698 	if (error)
    699 		return (error);
    700 
    701 	/*
    702 	 * Else, we're now committed to saying we're open.
    703 	 */
    704 
    705 	periph->periph_flags |= PERIPH_OPEN; /* unit attn are now errors */
    706 
    707 	/*
    708 	 * If it's a different mode, or if the media has been
    709 	 * invalidated, unmount the tape from the previous
    710 	 * session but continue with open processing
    711 	 */
    712 	if (st->last_dsty != dsty ||
    713 	    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
    714 		st_unmount(st, NOEJECT);
    715 
    716 	/*
    717 	 * If we are not mounted, then we should start a new
    718 	 * mount session.
    719 	 */
    720 	if (!(st->flags & ST_MOUNTED)) {
    721 		if ((error = st_mount_tape(dev, flags)) != 0)
    722 			goto bad;
    723 		st->last_dsty = dsty;
    724 	}
    725 	if (!(st->quirks & ST_Q_NOPREVENT)) {
    726 		scsipi_prevent(periph, SPAMR_PREVENT_DT,
    727 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
    728 	}
    729 
    730 	SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
    731 	return (0);
    732 
    733 bad:
    734 	st_unmount(st, NOEJECT);
    735 	scsipi_adapter_delref(adapt);
    736 	periph->periph_flags &= ~PERIPH_OPEN;
    737 	return (error);
    738 }
    739 
    740 /*
    741  * close the device.. only called if we are the LAST
    742  * occurence of an open device
    743  */
    744 static int
    745 stclose(dev_t dev, int flags, int mode, struct lwp *l)
    746 {
    747 	int stxx, error = 0;
    748 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
    749 	struct scsipi_periph *periph = st->sc_periph;
    750 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    751 
    752 	SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
    753 
    754 	/*
    755 	 * Make sure that a tape opened in write-only mode will have
    756 	 * file marks written on it when closed, even if not written to.
    757 	 *
    758 	 * This is for SUN compatibility. Actually, the Sun way of
    759 	 * things is to:
    760 	 *
    761 	 *	only write filemarks if there are fmks to be written and
    762 	 *   		- open for write (possibly read/write)
    763 	 *		- the last operation was a write
    764 	 * 	or:
    765 	 *		- opened for wronly
    766 	 *		- no data was written (including filemarks)
    767 	 */
    768 
    769 	stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
    770 	if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
    771 	    ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
    772 		int nm;
    773 		error = st_check_eod(st, FALSE, &nm, 0);
    774 	}
    775 
    776 	/*
    777 	 * Allow robots to eject tape if needed.
    778 	 */
    779 	scsipi_prevent(periph, SPAMR_ALLOW,
    780 	    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
    781 
    782 	switch (STMODE(dev)) {
    783 	case NORMAL_MODE:
    784 		st_unmount(st, NOEJECT);
    785 		break;
    786 	case NOREW_MODE:
    787 	case CTRL_MODE:
    788 		/*
    789 		 * Leave mounted unless media seems to have been removed.
    790 		 *
    791 		 * Otherwise, if we're to terminate a tape with more than one
    792 		 * filemark [ and because we're not rewinding here ], backspace
    793 		 * one filemark so that later appends will see an unbroken
    794 		 * sequence of:
    795 		 *
    796 		 *	file - FMK - file - FMK ... file - FMK FMK (EOM)
    797 		 */
    798 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    799 			st_unmount(st, NOEJECT);
    800 		} else if (error == 0) {
    801 			/*
    802 			 * ST_WRITTEN was preserved from above.
    803 			 *
    804 			 * All we need to know here is:
    805 			 *
    806 			 *	Were we writing this tape and was the last
    807 			 *	operation a write?
    808 			 *
    809 			 *	Are there supposed to be 2FM at EOD?
    810 			 *
    811 			 * If both statements are true, then we backspace
    812 			 * one filemark.
    813 			 */
    814 			stxx |= (st->flags & ST_2FM_AT_EOD);
    815 			if ((flags & FWRITE) != 0 &&
    816 			    (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
    817 				error = st_space(st, -1, SP_FILEMARKS, 0);
    818 			}
    819 		}
    820 		break;
    821 	case EJECT_MODE:
    822 		st_unmount(st, EJECT);
    823 		break;
    824 	}
    825 
    826 	scsipi_wait_drain(periph);
    827 
    828 	scsipi_adapter_delref(adapt);
    829 	periph->periph_flags &= ~PERIPH_OPEN;
    830 
    831 	return (error);
    832 }
    833 
    834 /*
    835  * Start a new mount session.
    836  * Copy in all the default parameters from the selected device mode.
    837  * and try guess any that seem to be defaulted.
    838  */
    839 static int
    840 st_mount_tape(dev_t dev, int flags)
    841 {
    842 	int unit;
    843 	u_int dsty;
    844 	struct st_softc *st;
    845 	struct scsipi_periph *periph;
    846 	int error = 0;
    847 
    848 	unit = STUNIT(dev);
    849 	dsty = STDSTY(dev);
    850 	st = st_cd.cd_devs[unit];
    851 	periph = st->sc_periph;
    852 
    853 	if (st->flags & ST_MOUNTED)
    854 		return (0);
    855 
    856 	SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
    857 	st->flags |= ST_NEW_MOUNT;
    858 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    859 	/*
    860 	 * If the media is new, then make sure we give it a chance to
    861 	 * to do a 'load' instruction.  (We assume it is new.)
    862 	 */
    863 	if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
    864 		return (error);
    865 	/*
    866 	 * Throw another dummy instruction to catch
    867 	 * 'Unit attention' errors. Many drives give
    868 	 * these after doing a Load instruction (with
    869 	 * the MEDIUM MAY HAVE CHANGED asc/ascq).
    870 	 */
    871 	scsipi_test_unit_ready(periph, XS_CTL_SILENT);	/* XXX */
    872 
    873 	/*
    874 	 * Some devices can't tell you much until they have been
    875 	 * asked to look at the media. This quirk does this.
    876 	 */
    877 	if (st->quirks & ST_Q_SENSE_HELP)
    878 		if ((error = st_touch_tape(st)) != 0)
    879 			return (error);
    880 	/*
    881 	 * Load the physical device parameters
    882 	 * loads: blkmin, blkmax
    883 	 */
    884 	if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0)
    885 		return (error);
    886 	/*
    887 	 * Load the media dependent parameters
    888 	 * includes: media_blksize,media_density,numblks
    889 	 * As we have a tape in, it should be reflected here.
    890 	 * If not you may need the "quirk" above.
    891 	 */
    892 	if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
    893 		return (error);
    894 	/*
    895 	 * If we have gained a permanent density from somewhere,
    896 	 * then use it in preference to the one supplied by
    897 	 * default by the driver.
    898 	 */
    899 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    900 		st->density = st->modes[dsty].density;
    901 	else
    902 		st->density = st->media_density;
    903 	/*
    904 	 * If we have gained a permanent blocksize
    905 	 * then use it in preference to the one supplied by
    906 	 * default by the driver.
    907 	 */
    908 	st->flags &= ~ST_FIXEDBLOCKS;
    909 	if (st->modeflags[dsty] &
    910 	    (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
    911 		st->blksize = st->modes[dsty].blksize;
    912 		if (st->blksize)
    913 			st->flags |= ST_FIXEDBLOCKS;
    914 	} else {
    915 		if ((error = st_decide_mode(st, FALSE)) != 0)
    916 			return (error);
    917 	}
    918 	if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
    919 		/* ATAPI will return ENODEV for this, and this may be OK */
    920 		if (error != ENODEV) {
    921 			aprint_error_dev(&st->sc_dev, "cannot set selected mode\n");
    922 			return (error);
    923 		}
    924 	}
    925 	st->flags &= ~ST_NEW_MOUNT;
    926 	st->flags |= ST_MOUNTED;
    927 	periph->periph_flags |= PERIPH_MEDIA_LOADED;	/* move earlier? */
    928 	st->blkno = st->fileno = (daddr_t) 0;
    929 	return (0);
    930 }
    931 
    932 /*
    933  * End the present mount session.
    934  * Rewind, and optionally eject the tape.
    935  * Reset various flags to indicate that all new
    936  * operations require another mount operation
    937  */
    938 static void
    939 st_unmount(struct st_softc *st, boolean eject)
    940 {
    941 	struct scsipi_periph *periph = st->sc_periph;
    942 	int nmarks;
    943 
    944 	if ((st->flags & ST_MOUNTED) == 0)
    945 		return;
    946 	SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
    947 	st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
    948 	st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
    949 
    950 	/*
    951 	 * Section 9.3.3 of the SCSI specs states that a device shall return
    952 	 * the density value specified in the last succesfull MODE SELECT
    953 	 * after an unload operation, in case it is not able to
    954 	 * automatically determine the density of the new medium.
    955 	 *
    956 	 * So we instruct the device to use the default density, which will
    957 	 * prevent the use of stale density values (in particular,
    958 	 * in st_touch_tape().
    959 	 */
    960 	st->density = 0;
    961 	if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) {
    962 		aprint_error_dev(&st->sc_dev, "WARNING: cannot revert to default density\n");
    963 	}
    964 
    965 	if (eject) {
    966 		if (!(st->quirks & ST_Q_NOPREVENT)) {
    967 			scsipi_prevent(periph, SPAMR_ALLOW,
    968 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    969 			    XS_CTL_IGNORE_NOT_READY);
    970 		}
    971 		st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
    972 		st->blkno = st->fileno = (daddr_t) -1;
    973 	} else {
    974 		st->blkno = st->fileno = (daddr_t) 0;
    975 	}
    976 	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
    977 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    978 }
    979 
    980 /*
    981  * Given all we know about the device, media, mode, 'quirks' and
    982  * initial operation, make a decision as to how we should be set
    983  * to run (regarding blocking and EOD marks)
    984  */
    985 int
    986 st_decide_mode(struct st_softc *st, boolean first_read)
    987 {
    988 
    989 	SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
    990 
    991 	/*
    992 	 * If the drive can only handle fixed-length blocks and only at
    993 	 * one size, perhaps we should just do that.
    994 	 */
    995 	if (st->blkmin && (st->blkmin == st->blkmax)) {
    996 		st->flags |= ST_FIXEDBLOCKS;
    997 		st->blksize = st->blkmin;
    998 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
    999 		    ("blkmin == blkmax of %d\n", st->blkmin));
   1000 		goto done;
   1001 	}
   1002 	/*
   1003 	 * If the tape density mandates (or even suggests) use of fixed
   1004 	 * or variable-length blocks, comply.
   1005 	 */
   1006 	switch (st->density) {
   1007 	case HALFINCH_800:
   1008 	case HALFINCH_1600:
   1009 	case HALFINCH_6250:
   1010 	case DDS:
   1011 		st->flags &= ~ST_FIXEDBLOCKS;
   1012 		st->blksize = 0;
   1013 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
   1014 		    ("density specified variable\n"));
   1015 		goto done;
   1016 	case QIC_11:
   1017 	case QIC_24:
   1018 	case QIC_120:
   1019 	case QIC_150:
   1020 	case QIC_525:
   1021 	case QIC_1320:
   1022 	case QIC_3095:
   1023 	case QIC_3220:
   1024 		st->flags |= ST_FIXEDBLOCKS;
   1025 		if (st->media_blksize > 0)
   1026 			st->blksize = st->media_blksize;
   1027 		else
   1028 			st->blksize = DEF_FIXED_BSIZE;
   1029 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
   1030 		    ("density specified fixed\n"));
   1031 		goto done;
   1032 	}
   1033 	/*
   1034 	 * If we're about to read the tape, perhaps we should choose
   1035 	 * fixed or variable-length blocks and block size according to
   1036 	 * what the drive found on the tape.
   1037 	 */
   1038 	if (first_read &&
   1039 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
   1040 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
   1041 	    (st->media_blksize == 1024))) {
   1042 		if (st->media_blksize > 0)
   1043 			st->flags |= ST_FIXEDBLOCKS;
   1044 		else
   1045 			st->flags &= ~ST_FIXEDBLOCKS;
   1046 		st->blksize = st->media_blksize;
   1047 		SC_DEBUG(st->sc_periph, SCSIPI_DB3,
   1048 		    ("Used media_blksize of %d\n", st->media_blksize));
   1049 		goto done;
   1050 	}
   1051 	/*
   1052 	 * We're getting no hints from any direction.  Choose variable-
   1053 	 * length blocks arbitrarily.
   1054 	 */
   1055 	st->flags &= ~ST_FIXEDBLOCKS;
   1056 	st->blksize = 0;
   1057 	SC_DEBUG(st->sc_periph, SCSIPI_DB3,
   1058 	    ("Give up and default to variable mode\n"));
   1059 
   1060 done:
   1061 	/*
   1062 	 * Decide whether or not to write two file marks to signify end-
   1063 	 * of-data.  Make the decision as a function of density.  If
   1064 	 * the decision is not to use a second file mark, the SCSI BLANK
   1065 	 * CHECK condition code will be recognized as end-of-data when
   1066 	 * first read.
   1067 	 * (I think this should be a by-product of fixed/variable..julian)
   1068 	 */
   1069 	switch (st->density) {
   1070 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
   1071 	case QIC_11:
   1072 	case QIC_24:
   1073 	case QIC_120:
   1074 	case QIC_150:
   1075 	case QIC_525:
   1076 	case QIC_1320:
   1077 	case QIC_3095:
   1078 	case QIC_3220:
   1079 		st->flags &= ~ST_2FM_AT_EOD;
   1080 		break;
   1081 	default:
   1082 		st->flags |= ST_2FM_AT_EOD;
   1083 	}
   1084 	return (0);
   1085 }
   1086 
   1087 /*
   1088  * Actually translate the requested transfer into
   1089  * one the physical driver can understand
   1090  * The transfer is described by a buf and will include
   1091  * only one physical transfer.
   1092  */
   1093 static void
   1094 ststrategy(struct buf *bp)
   1095 {
   1096 	struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
   1097 	int s;
   1098 
   1099 	SC_DEBUG(st->sc_periph, SCSIPI_DB1,
   1100 	    ("ststrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
   1101 	/*
   1102 	 * If it's a null transfer, return immediatly
   1103 	 */
   1104 	if (bp->b_bcount == 0)
   1105 		goto done;
   1106 
   1107 	/* If offset is negative, error */
   1108 	if (bp->b_blkno < 0) {
   1109 		bp->b_error = EINVAL;
   1110 		goto done;
   1111 	}
   1112 
   1113 	/*
   1114 	 * Odd sized request on fixed drives are verboten
   1115 	 */
   1116 	if (st->flags & ST_FIXEDBLOCKS) {
   1117 		if (bp->b_bcount % st->blksize) {
   1118 			aprint_error_dev(&st->sc_dev, "bad request, must be multiple of %d\n",
   1119 			    st->blksize);
   1120 			bp->b_error = EIO;
   1121 			goto done;
   1122 		}
   1123 	}
   1124 	/*
   1125 	 * as are out-of-range requests on variable drives.
   1126 	 */
   1127 	else if (bp->b_bcount < st->blkmin ||
   1128 	    (st->blkmax && bp->b_bcount > st->blkmax)) {
   1129 		aprint_error_dev(&st->sc_dev, "bad request, must be between %d and %d\n",
   1130 		    st->blkmin, st->blkmax);
   1131 		bp->b_error = EIO;
   1132 		goto done;
   1133 	}
   1134 	s = splbio();
   1135 
   1136 	/*
   1137 	 * Place it in the queue of activities for this tape
   1138 	 * at the end (a bit silly because we only have on user..
   1139 	 * (but it could fork()))
   1140 	 */
   1141 	BUFQ_PUT(st->buf_queue, bp);
   1142 
   1143 	/*
   1144 	 * Tell the device to get going on the transfer if it's
   1145 	 * not doing anything, otherwise just wait for completion
   1146 	 * (All a bit silly if we're only allowing 1 open but..)
   1147 	 */
   1148 	ststart(st->sc_periph);
   1149 
   1150 	splx(s);
   1151 	return;
   1152 done:
   1153 	/*
   1154 	 * Correctly set the buf to indicate a completed xfer
   1155 	 */
   1156 	bp->b_resid = bp->b_bcount;
   1157 	biodone(bp);
   1158 	return;
   1159 }
   1160 
   1161 /*
   1162  * ststart looks to see if there is a buf waiting for the device
   1163  * and that the device is not already busy. If both are true,
   1164  * It dequeues the buf and creates a scsi command to perform the
   1165  * transfer required. The transfer request will call scsipi_done
   1166  * on completion, which will in turn call this routine again
   1167  * so that the next queued transfer is performed.
   1168  * The bufs are queued by the strategy routine (ststrategy)
   1169  *
   1170  * This routine is also called after other non-queued requests
   1171  * have been made of the scsi driver, to ensure that the queue
   1172  * continues to be drained.
   1173  * ststart() is called at splbio
   1174  */
   1175 static void
   1176 ststart(struct scsipi_periph *periph)
   1177 {
   1178 	struct st_softc *st = (void *)periph->periph_dev;
   1179 	struct buf *bp;
   1180 	struct scsi_rw_tape cmd;
   1181 	struct scsipi_xfer *xs;
   1182 	int flags, error;
   1183 
   1184 	SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
   1185 	/*
   1186 	 * See if there is a buf to do and we are not already
   1187 	 * doing one
   1188 	 */
   1189 	while (periph->periph_active < periph->periph_openings) {
   1190 		/* if a special awaits, let it proceed first */
   1191 		if (periph->periph_flags & PERIPH_WAITING) {
   1192 			periph->periph_flags &= ~PERIPH_WAITING;
   1193 			wakeup((void *)periph);
   1194 			return;
   1195 		}
   1196 
   1197 		/*
   1198 		 * If the device has been unmounted by the user
   1199 		 * then throw away all requests until done.
   1200 		 */
   1201 		if (__predict_false((st->flags & ST_MOUNTED) == 0 ||
   1202 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
   1203 			if ((bp = BUFQ_GET(st->buf_queue)) != NULL) {
   1204 				/* make sure that one implies the other.. */
   1205 				periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
   1206 				bp->b_error = EIO;
   1207 				bp->b_resid = bp->b_bcount;
   1208 				biodone(bp);
   1209 				continue;
   1210 			} else {
   1211 				return;
   1212 			}
   1213 		}
   1214 
   1215 		if ((bp = BUFQ_PEEK(st->buf_queue)) == NULL)
   1216 			return;
   1217 
   1218 		iostat_busy(st->stats);
   1219 
   1220 		/*
   1221 		 * only FIXEDBLOCK devices have pending I/O or space operations.
   1222 		 */
   1223 		if (st->flags & ST_FIXEDBLOCKS) {
   1224 			/*
   1225 			 * If we are at a filemark but have not reported it yet
   1226 			 * then we should report it now
   1227 			 */
   1228 			if (st->flags & ST_AT_FILEMARK) {
   1229 				if ((bp->b_flags & B_READ) == B_WRITE) {
   1230 					/*
   1231 					 * Handling of ST_AT_FILEMARK in
   1232 					 * st_space will fill in the right file
   1233 					 * mark count.
   1234 					 * Back up over filemark
   1235 					 */
   1236 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
   1237 						BUFQ_GET(st->buf_queue);
   1238 						bp->b_error = EIO;
   1239 						bp->b_resid = bp->b_bcount;
   1240 						biodone(bp);
   1241 						continue;
   1242 					}
   1243 				} else {
   1244 					BUFQ_GET(st->buf_queue);
   1245 					bp->b_resid = bp->b_bcount;
   1246 					bp->b_error = 0;
   1247 					st->flags &= ~ST_AT_FILEMARK;
   1248 					biodone(bp);
   1249 					continue;	/* seek more work */
   1250 				}
   1251 			}
   1252 		}
   1253 		/*
   1254 		 * If we are at EOM but have not reported it
   1255 		 * yet then we should report it now.
   1256 		 */
   1257 		if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
   1258 			BUFQ_GET(st->buf_queue);
   1259 			bp->b_resid = bp->b_bcount;
   1260 			if (st->flags & ST_EIO_PENDING)
   1261 				bp->b_error = EIO;
   1262 			st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
   1263 			biodone(bp);
   1264 			continue;	/* seek more work */
   1265 		}
   1266 
   1267 		/*
   1268 		 * Fill out the scsi command
   1269 		 */
   1270 		memset(&cmd, 0, sizeof(cmd));
   1271 		flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
   1272 		if ((bp->b_flags & B_READ) == B_WRITE) {
   1273 			cmd.opcode = WRITE;
   1274 			st->flags &= ~ST_FM_WRITTEN;
   1275 			flags |= XS_CTL_DATA_OUT;
   1276 		} else {
   1277 			cmd.opcode = READ;
   1278 			flags |= XS_CTL_DATA_IN;
   1279 		}
   1280 
   1281 		/*
   1282 		 * Handle "fixed-block-mode" tape drives by using the
   1283 		 * block count instead of the length.
   1284 		 */
   1285 		if (st->flags & ST_FIXEDBLOCKS) {
   1286 			cmd.byte2 |= SRW_FIXED;
   1287 			_lto3b(bp->b_bcount / st->blksize, cmd.len);
   1288 		} else
   1289 			_lto3b(bp->b_bcount, cmd.len);
   1290 
   1291 		/*
   1292 		 * Clear 'position updated' indicator
   1293 		 */
   1294 		st->flags &= ~ST_POSUPDATED;
   1295 
   1296 		/*
   1297 		 * go ask the adapter to do all this for us
   1298 		 */
   1299 		xs = scsipi_make_xs(periph,
   1300 		    (struct scsipi_generic *)&cmd, sizeof(cmd),
   1301 		    (u_char *)bp->b_data, bp->b_bcount,
   1302 		    0, ST_IO_TIME, bp, flags);
   1303 		if (__predict_false(xs == NULL)) {
   1304 			/*
   1305 			 * out of memory. Keep this buffer in the queue, and
   1306 			 * retry later.
   1307 			 */
   1308 			callout_reset(&st->sc_callout, hz / 2, strestart,
   1309 			    periph);
   1310 			return;
   1311 		}
   1312 		/*
   1313 		 * need to dequeue the buffer before queuing the command,
   1314 		 * because cdstart may be called recursively from the
   1315 		 * HBA driver
   1316 		 */
   1317 #ifdef DIAGNOSTIC
   1318 		if (BUFQ_GET(st->buf_queue) != bp)
   1319 			panic("ststart(): dequeued wrong buf");
   1320 #else
   1321 		BUFQ_GET(st->buf_queue);
   1322 #endif
   1323 		error = scsipi_execute_xs(xs);
   1324 		/* with a scsipi_xfer preallocated, scsipi_command can't fail */
   1325 		KASSERT(error == 0);
   1326 	} /* go back and see if we can cram more work in.. */
   1327 }
   1328 
   1329 static void
   1330 strestart(void *v)
   1331 {
   1332 	int s = splbio();
   1333 	ststart((struct scsipi_periph *)v);
   1334 	splx(s);
   1335 }
   1336 
   1337 
   1338 static void
   1339 stdone(struct scsipi_xfer *xs, int error)
   1340 {
   1341 	struct st_softc *st = (void *)xs->xs_periph->periph_dev;
   1342 	struct buf *bp = xs->bp;
   1343 
   1344 	if (bp) {
   1345 		bp->b_error = error;
   1346 		bp->b_resid = xs->resid;
   1347 		/*
   1348 		 * buggy device ? A SDLT320 can report an info
   1349 		 * field of 0x3de8000 on a Media Error/Write Error
   1350 		 * for this CBD: 0x0a 00 00 80 00 00
   1351 		 */
   1352 		if (bp->b_resid > bp->b_bcount || bp->b_resid < 0)
   1353 			bp->b_resid = bp->b_bcount;
   1354 
   1355 		if ((bp->b_flags & B_READ) == B_WRITE)
   1356 			st->flags |= ST_WRITTEN;
   1357 		else
   1358 			st->flags &= ~ST_WRITTEN;
   1359 
   1360 		iostat_unbusy(st->stats, bp->b_bcount,
   1361 			     ((bp->b_flags & B_READ) == B_READ));
   1362 
   1363 #if NRND > 0
   1364 		rnd_add_uint32(&st->rnd_source, bp->b_blkno);
   1365 #endif
   1366 
   1367 		if ((st->flags & ST_POSUPDATED) == 0) {
   1368 			if (error) {
   1369 				st->fileno = st->blkno = -1;
   1370 			} else if (st->blkno != -1) {
   1371 				if (st->flags & ST_FIXEDBLOCKS)
   1372 					st->blkno +=
   1373 					    (bp->b_bcount / st->blksize);
   1374 				else
   1375 					st->blkno++;
   1376 			}
   1377 		}
   1378 
   1379 		biodone(bp);
   1380 	}
   1381 }
   1382 
   1383 static int
   1384 stread(dev_t dev, struct uio *uio, int iomode)
   1385 {
   1386 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1387 
   1388 	return (physio(ststrategy, NULL, dev, B_READ,
   1389 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1390 }
   1391 
   1392 static int
   1393 stwrite(dev_t dev, struct uio *uio, int iomode)
   1394 {
   1395 	struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
   1396 
   1397 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1398 	    st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
   1399 }
   1400 
   1401 /*
   1402  * Perform special action on behalf of the user;
   1403  * knows about the internals of this device
   1404  */
   1405 static int
   1406 stioctl(dev_t dev, u_long cmd, void *arg, int flag, struct lwp *l)
   1407 {
   1408 	int error = 0;
   1409 	int unit;
   1410 	int number, nmarks, dsty;
   1411 	int flags;
   1412 	struct st_softc *st;
   1413 	int hold_blksize;
   1414 	u_int8_t hold_density;
   1415 	struct mtop *mt = (struct mtop *) arg;
   1416 
   1417 	/*
   1418 	 * Find the device that the user is talking about
   1419 	 */
   1420 	flags = 0;		/* give error messages, act on errors etc. */
   1421 	unit = STUNIT(dev);
   1422 	dsty = STDSTY(dev);
   1423 	st = st_cd.cd_devs[unit];
   1424 	hold_blksize = st->blksize;
   1425 	hold_density = st->density;
   1426 
   1427 	switch ((u_int) cmd) {
   1428 
   1429 	case MTIOCGET: {
   1430 		struct mtget *g = (struct mtget *) arg;
   1431 		/*
   1432 		 * (to get the current state of READONLY)
   1433 		 */
   1434 		error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
   1435 		if (error) {
   1436 			/*
   1437 			 * Ignore the error if in control mode;
   1438 			 * this is mandated by st(4).
   1439 			 */
   1440 			if (STMODE(dev) != CTRL_MODE)
   1441 				break;
   1442 			error = 0;
   1443 		}
   1444 		SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
   1445 		memset(g, 0, sizeof(struct mtget));
   1446 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1447 		g->mt_blksiz = st->blksize;
   1448 		g->mt_density = st->density;
   1449 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1450 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1451 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1452 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1453 		g->mt_mdensity[0] = st->modes[0].density;
   1454 		g->mt_mdensity[1] = st->modes[1].density;
   1455 		g->mt_mdensity[2] = st->modes[2].density;
   1456 		g->mt_mdensity[3] = st->modes[3].density;
   1457 		g->mt_fileno = st->fileno;
   1458 		g->mt_blkno = st->blkno;
   1459 		if (st->flags & ST_READONLY)
   1460 			g->mt_dsreg |= MT_DS_RDONLY;
   1461 		if (st->flags & ST_MOUNTED)
   1462 			g->mt_dsreg |= MT_DS_MOUNTED;
   1463 		g->mt_resid = st->mt_resid;
   1464 		g->mt_erreg = st->mt_erreg;
   1465 		/*
   1466 		 * clear latched errors.
   1467 		 */
   1468 		st->mt_resid = 0;
   1469 		st->mt_erreg = 0;
   1470 		st->asc = 0;
   1471 		st->ascq = 0;
   1472 		break;
   1473 	}
   1474 	case MTIOCTOP: {
   1475 
   1476 		SC_DEBUG(st->sc_periph, SCSIPI_DB1,
   1477 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
   1478 			mt->mt_count));
   1479 
   1480 		/* compat: in U*x it is a short */
   1481 		number = mt->mt_count;
   1482 		switch ((short) (mt->mt_op)) {
   1483 		case MTWEOF:	/* write an end-of-file record */
   1484 			error = st_write_filemarks(st, number, flags);
   1485 			break;
   1486 		case MTBSF:	/* backward space file */
   1487 			number = -number;
   1488 		case MTFSF:	/* forward space file */
   1489 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1490 			if (!error)
   1491 				error = st_space(st, number - nmarks,
   1492 				    SP_FILEMARKS, flags);
   1493 			break;
   1494 		case MTBSR:	/* backward space record */
   1495 			number = -number;
   1496 		case MTFSR:	/* forward space record */
   1497 			error = st_check_eod(st, true, &nmarks, flags);
   1498 			if (!error)
   1499 				error = st_space(st, number, SP_BLKS, flags);
   1500 			break;
   1501 		case MTREW:	/* rewind */
   1502 			error = st_rewind(st, 0, flags);
   1503 			break;
   1504 		case MTOFFL:	/* rewind and put the drive offline */
   1505 			st_unmount(st, EJECT);
   1506 			break;
   1507 		case MTNOP:	/* no operation, sets status only */
   1508 			break;
   1509 		case MTRETEN:	/* retension the tape */
   1510 			error = st_load(st, LD_RETENSION, flags);
   1511 			if (!error)
   1512 				error = st_load(st, LD_LOAD, flags);
   1513 			break;
   1514 		case MTEOM:	/* forward space to end of media */
   1515 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1516 			if (!error)
   1517 				error = st_space(st, 1, SP_EOM, flags);
   1518 			break;
   1519 		case MTCACHE:	/* enable controller cache */
   1520 			st->flags &= ~ST_DONTBUFFER;
   1521 			goto try_new_value;
   1522 		case MTNOCACHE:	/* disable controller cache */
   1523 			st->flags |= ST_DONTBUFFER;
   1524 			goto try_new_value;
   1525 		case MTERASE:	/* erase volume */
   1526 			error = st_erase(st, number, flags);
   1527 			break;
   1528 		case MTSETBSIZ:	/* Set block size for device */
   1529 #ifdef	NOTYET
   1530 			if (!(st->flags & ST_NEW_MOUNT)) {
   1531 				uprintf("re-mount tape before changing blocksize");
   1532 				error = EINVAL;
   1533 				break;
   1534 			}
   1535 #endif
   1536 			if (number == 0)
   1537 				st->flags &= ~ST_FIXEDBLOCKS;
   1538 			else {
   1539 				if ((st->blkmin || st->blkmax) &&
   1540 				    (number < st->blkmin ||
   1541 				    number > st->blkmax)) {
   1542 					error = EINVAL;
   1543 					break;
   1544 				}
   1545 				st->flags |= ST_FIXEDBLOCKS;
   1546 			}
   1547 			st->blksize = number;
   1548 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1549 			goto try_new_value;
   1550 
   1551 		case MTSETDNSTY:	/* Set density for device and mode */
   1552 			/*
   1553 			 * Any number >= 0 and <= 0xff is legal. Numbers
   1554 			 * above 0x80 are 'vendor unique'.
   1555 			 */
   1556 			if (number < 0 || number > 255) {
   1557 				error = EINVAL;
   1558 				break;
   1559 			} else
   1560 				st->density = number;
   1561 			goto try_new_value;
   1562 
   1563 		case MTCMPRESS:
   1564 			error = st->ops(st, (number == 0) ?
   1565 			    ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
   1566 			    XS_CTL_SILENT);
   1567 			break;
   1568 
   1569 		case MTEWARN:
   1570 			if (number)
   1571 				st->flags |= ST_EARLYWARN;
   1572 			else
   1573 				st->flags &= ~ST_EARLYWARN;
   1574 			break;
   1575 
   1576 		default:
   1577 			error = EINVAL;
   1578 		}
   1579 		break;
   1580 	}
   1581 	case MTIOCIEOT:
   1582 	case MTIOCEEOT:
   1583 		break;
   1584 
   1585 	case MTIOCRDSPOS:
   1586 		error = st_rdpos(st, 0, (u_int32_t *) arg);
   1587 		break;
   1588 
   1589 	case MTIOCRDHPOS:
   1590 		error = st_rdpos(st, 1, (u_int32_t *) arg);
   1591 		break;
   1592 
   1593 	case MTIOCSLOCATE:
   1594 		error = st_setpos(st, 0, (u_int32_t *) arg);
   1595 		break;
   1596 
   1597 	case MTIOCHLOCATE:
   1598 		error = st_setpos(st, 1, (u_int32_t *) arg);
   1599 		break;
   1600 
   1601 
   1602 	default:
   1603 		error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
   1604 					flag, l);
   1605 		break;
   1606 	}
   1607 	return (error);
   1608 /*-----------------------------*/
   1609 try_new_value:
   1610 	/*
   1611 	 * Check that the mode being asked for is aggreeable to the
   1612 	 * drive. If not, put it back the way it was.
   1613 	 *
   1614 	 * If in control mode, we can make (persistent) mode changes
   1615 	 * even if no medium is loaded (see st(4)).
   1616 	 */
   1617 	if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
   1618 	    (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
   1619 		/* put it back as it was */
   1620 		aprint_error_dev(&st->sc_dev, "cannot set selected mode\n");
   1621 		st->density = hold_density;
   1622 		st->blksize = hold_blksize;
   1623 		if (st->blksize)
   1624 			st->flags |= ST_FIXEDBLOCKS;
   1625 		else
   1626 			st->flags &= ~ST_FIXEDBLOCKS;
   1627 		return (error);
   1628 	}
   1629 	/*
   1630 	 * As the drive liked it, if we are setting a new default,
   1631 	 * set it into the structures as such.
   1632 	 *
   1633 	 * The means for deciding this are not finalised yet- but
   1634 	 * if the device was opened in Control Mode, the values
   1635 	 * are persistent now across mounts.
   1636 	 */
   1637 	if (STMODE(dev) == CTRL_MODE) {
   1638 		switch ((short) (mt->mt_op)) {
   1639 		case MTSETBSIZ:
   1640 			st->modes[dsty].blksize = st->blksize;
   1641 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1642 			break;
   1643 		case MTSETDNSTY:
   1644 			st->modes[dsty].density = st->density;
   1645 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1646 			break;
   1647 		}
   1648 	}
   1649 	return (0);
   1650 }
   1651 
   1652 /*
   1653  * Do a synchronous read.
   1654  */
   1655 static int
   1656 st_read(struct st_softc *st, char *bf, int size, int flags)
   1657 {
   1658 	struct scsi_rw_tape cmd;
   1659 
   1660 	/*
   1661 	 * If it's a null transfer, return immediatly
   1662 	 */
   1663 	if (size == 0)
   1664 		return (0);
   1665 	memset(&cmd, 0, sizeof(cmd));
   1666 	cmd.opcode = READ;
   1667 	if (st->flags & ST_FIXEDBLOCKS) {
   1668 		cmd.byte2 |= SRW_FIXED;
   1669 		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1670 		    cmd.len);
   1671 	} else
   1672 		_lto3b(size, cmd.len);
   1673 	return (scsipi_command(st->sc_periph,
   1674 	    (void *)&cmd, sizeof(cmd), (void *)bf, size, 0, ST_IO_TIME, NULL,
   1675 	    flags | XS_CTL_DATA_IN));
   1676 }
   1677 
   1678 /*
   1679  * issue an erase command
   1680  */
   1681 static int
   1682 st_erase(struct st_softc *st, int full, int flags)
   1683 {
   1684 	int tmo;
   1685 	struct scsi_erase cmd;
   1686 
   1687 	/*
   1688 	 * Full erase means set LONG bit in erase command, which asks
   1689 	 * the drive to erase the entire unit.  Without this bit, we're
   1690 	 * asking the drive to write an erase gap.
   1691 	 */
   1692 	memset(&cmd, 0, sizeof(cmd));
   1693 	cmd.opcode = ERASE;
   1694 	if (full) {
   1695 		cmd.byte2 = SE_LONG;
   1696 		tmo = ST_SPC_TIME;
   1697 	} else {
   1698 		tmo = ST_IO_TIME;
   1699 	}
   1700 
   1701 	/*
   1702 	 * XXX We always do this asynchronously, for now, unless the device
   1703 	 * has the ST_Q_ERASE_NOIMM quirk.  How long should we wait if we
   1704 	 * want to (eventually) to it synchronously?
   1705 	 */
   1706 	if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
   1707 		cmd.byte2 |= SE_IMMED;
   1708 
   1709 	return (scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1710 	    ST_RETRIES, tmo, NULL, flags));
   1711 }
   1712 
   1713 /*
   1714  * skip N blocks/filemarks/seq filemarks/eom
   1715  */
   1716 static int
   1717 st_space(struct st_softc *st, int number, u_int what, int flags)
   1718 {
   1719 	struct scsi_space cmd;
   1720 	int error;
   1721 
   1722 	switch (what) {
   1723 	case SP_BLKS:
   1724 		if (st->flags & ST_PER_ACTION) {
   1725 			if (number > 0) {
   1726 				st->flags &= ~ST_PER_ACTION;
   1727 				return (EIO);
   1728 			} else if (number < 0) {
   1729 				if (st->flags & ST_AT_FILEMARK) {
   1730 					/*
   1731 					 * Handling of ST_AT_FILEMARK
   1732 					 * in st_space will fill in the
   1733 					 * right file mark count.
   1734 					 */
   1735 					error = st_space(st, 0, SP_FILEMARKS,
   1736 					    flags);
   1737 					if (error)
   1738 						return (error);
   1739 				}
   1740 				if (st->flags & ST_BLANK_READ) {
   1741 					st->flags &= ~ST_BLANK_READ;
   1742 					return (EIO);
   1743 				}
   1744 				st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
   1745 			}
   1746 		}
   1747 		break;
   1748 	case SP_FILEMARKS:
   1749 		if (st->flags & ST_EIO_PENDING) {
   1750 			if (number > 0) {
   1751 				/* pretend we just discovered the error */
   1752 				st->flags &= ~ST_EIO_PENDING;
   1753 				return (EIO);
   1754 			} else if (number < 0) {
   1755 				/* back away from the error */
   1756 				st->flags &= ~ST_EIO_PENDING;
   1757 			}
   1758 		}
   1759 		if (st->flags & ST_AT_FILEMARK) {
   1760 			st->flags &= ~ST_AT_FILEMARK;
   1761 			number--;
   1762 		}
   1763 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1764 			/* back away from unwritten tape */
   1765 			st->flags &= ~ST_BLANK_READ;
   1766 			number++;	/* XXX dubious */
   1767 		}
   1768 		break;
   1769 	case SP_EOM:
   1770 		if (st->flags & ST_EOM_PENDING) {
   1771 			/* we're already there */
   1772 			st->flags &= ~ST_EOM_PENDING;
   1773 			return (0);
   1774 		}
   1775 		if (st->flags & ST_EIO_PENDING) {
   1776 			/* pretend we just discovered the error */
   1777 			st->flags &= ~ST_EIO_PENDING;
   1778 			return (EIO);
   1779 		}
   1780 		if (st->flags & ST_AT_FILEMARK)
   1781 			st->flags &= ~ST_AT_FILEMARK;
   1782 		break;
   1783 	}
   1784 	if (number == 0)
   1785 		return (0);
   1786 
   1787 	memset(&cmd, 0, sizeof(cmd));
   1788 	cmd.opcode = SPACE;
   1789 	cmd.byte2 = what;
   1790 	_lto3b(number, cmd.number);
   1791 
   1792 	st->flags &= ~ST_POSUPDATED;
   1793 	st->last_ctl_resid = 0;
   1794 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1795 	    0, ST_SPC_TIME, NULL, flags);
   1796 
   1797 	if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
   1798 		number = number - st->last_ctl_resid;
   1799 		if (what == SP_BLKS) {
   1800 			if (st->blkno != -1) {
   1801 				st->blkno += number;
   1802 			}
   1803 		} else if (what == SP_FILEMARKS) {
   1804 			if (st->fileno != -1) {
   1805 				st->fileno += number;
   1806 				if (number > 0) {
   1807 					st->blkno = 0;
   1808 				} else if (number < 0) {
   1809 					st->blkno = -1;
   1810 				}
   1811 			}
   1812 		} else if (what == SP_EOM) {
   1813 			/*
   1814 			 * This loses us relative position.
   1815 			 */
   1816 			st->fileno = st->blkno = -1;
   1817 		}
   1818 	}
   1819 	return (error);
   1820 }
   1821 
   1822 /*
   1823  * write N filemarks
   1824  */
   1825 static int
   1826 st_write_filemarks(struct st_softc *st, int number, int flags)
   1827 {
   1828 	int error;
   1829 	struct scsi_write_filemarks cmd;
   1830 
   1831 	/*
   1832 	 * It's hard to write a negative number of file marks.
   1833 	 * Don't try.
   1834 	 */
   1835 	if (number < 0)
   1836 		return (EINVAL);
   1837 	switch (number) {
   1838 	case 0:		/* really a command to sync the drive's buffers */
   1839 		break;
   1840 	case 1:
   1841 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1842 			st->flags &= ~ST_WRITTEN;
   1843 		else
   1844 			st->flags |= ST_FM_WRITTEN;
   1845 		st->flags &= ~ST_PER_ACTION;
   1846 		break;
   1847 	default:
   1848 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1849 	}
   1850 
   1851 	memset(&cmd, 0, sizeof(cmd));
   1852 	cmd.opcode = WRITE_FILEMARKS;
   1853 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1854 		cmd.byte2 = SR_IMMED;
   1855 	/*
   1856 	 * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
   1857 	 * WRITE_FILEMARKS is still used to flush the buffer
   1858 	 */
   1859 	if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
   1860 		_lto3b(number, cmd.number);
   1861 
   1862 	/* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
   1863 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1864 	    0, ST_IO_TIME * 4, NULL, flags);
   1865 	if (error == 0 && st->fileno != -1) {
   1866 		st->fileno += number;
   1867 	}
   1868 	return (error);
   1869 }
   1870 
   1871 /*
   1872  * Make sure the right number of file marks is on tape if the
   1873  * tape has been written.  If the position argument is true,
   1874  * leave the tape positioned where it was originally.
   1875  *
   1876  * nmarks returns the number of marks to skip (or, if position
   1877  * true, which were skipped) to get back original position.
   1878  */
   1879 static int
   1880 st_check_eod(struct st_softc *st, boolean position, int *nmarks, int flags)
   1881 {
   1882 	int error;
   1883 
   1884 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1885 	default:
   1886 		*nmarks = 0;
   1887 		return (0);
   1888 	case ST_WRITTEN:
   1889 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1890 		*nmarks = 1;
   1891 		break;
   1892 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1893 		*nmarks = 2;
   1894 	}
   1895 	error = st_write_filemarks(st, *nmarks, flags);
   1896 	if (position && !error)
   1897 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1898 	return (error);
   1899 }
   1900 
   1901 /*
   1902  * load/unload/retension
   1903  */
   1904 static int
   1905 st_load(struct st_softc *st, u_int type, int flags)
   1906 {
   1907 	int error;
   1908 	struct scsi_load cmd;
   1909 
   1910 	if (type != LD_LOAD) {
   1911 		int nmarks;
   1912 
   1913 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1914 		if (error) {
   1915 			aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
   1916 			    "unload, errno=%d\n", error);
   1917 			return (error);
   1918 		}
   1919 	}
   1920 	if (st->quirks & ST_Q_IGNORE_LOADS) {
   1921 		if (type == LD_LOAD) {
   1922 			/*
   1923 			 * If we ignore loads, at least we should try a rewind.
   1924 			 */
   1925 			return st_rewind(st, 0, flags);
   1926 		}
   1927 		/* otherwise, we should do what's asked of us */
   1928 	}
   1929 
   1930 	memset(&cmd, 0, sizeof(cmd));
   1931 	cmd.opcode = LOAD;
   1932 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1933 		cmd.byte2 = SR_IMMED;
   1934 	cmd.how = type;
   1935 
   1936 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1937 	    ST_RETRIES, ST_SPC_TIME, NULL, flags);
   1938 	if (error) {
   1939 		aprint_error_dev(&st->sc_dev, "error %d in st_load (op %d)\n",
   1940 		    error, type);
   1941 	}
   1942 	return (error);
   1943 }
   1944 
   1945 /*
   1946  *  Rewind the device
   1947  */
   1948 static int
   1949 st_rewind(struct st_softc *st, u_int immediate, int flags)
   1950 {
   1951 	struct scsi_rewind cmd;
   1952 	int error;
   1953 	int nmarks;
   1954 	int timeout;
   1955 
   1956 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1957 	if (error) {
   1958 		aprint_error_dev(&st->sc_dev, "failed to write closing filemarks at "
   1959 		    "rewind, errno=%d\n", error);
   1960 		return (error);
   1961 	}
   1962 	st->flags &= ~ST_PER_ACTION;
   1963 
   1964 	/* If requestor asked for immediate response, set a short timeout */
   1965 	timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
   1966 
   1967 	/*
   1968 	 * ATAPI tapes always need immediate to be set
   1969 	 */
   1970 	if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
   1971 		immediate = SR_IMMED;
   1972 
   1973 	memset(&cmd, 0, sizeof(cmd));
   1974 	cmd.opcode = REWIND;
   1975 	cmd.byte2 = immediate;
   1976 
   1977 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   1978 	    ST_RETRIES, timeout, NULL, flags);
   1979 	if (error) {
   1980 		aprint_error_dev(&st->sc_dev, "error %d trying to rewind\n",
   1981 		    error);
   1982 		/* lost position */
   1983 		st->fileno = st->blkno = -1;
   1984 	} else {
   1985 		st->fileno = st->blkno = 0;
   1986 	}
   1987 	return (error);
   1988 }
   1989 
   1990 static int
   1991 st_rdpos(struct st_softc *st, int hard, u_int32_t *blkptr)
   1992 {
   1993 	int error;
   1994 	u_int8_t posdata[20];
   1995 	struct scsi_tape_read_position cmd;
   1996 
   1997 	/*
   1998 	 * We try and flush any buffered writes here if we were writing
   1999 	 * and we're trying to get hardware block position. It eats
   2000 	 * up performance substantially, but I'm wary of drive firmware.
   2001 	 *
   2002 	 * I think that *logical* block position is probably okay-
   2003 	 * but hardware block position might have to wait for data
   2004 	 * to hit media to be valid. Caveat Emptor.
   2005 	 */
   2006 
   2007 	if (hard && (st->flags & ST_WRITTEN)) {
   2008 		/*
   2009 		 * First flush any pending writes...
   2010 		 */
   2011 		error = st_write_filemarks(st, 0, XS_CTL_SILENT);
   2012 		/*
   2013 		 * The latter case is for 'write protected' tapes
   2014 		 * which are too stupid to recognize a zero count
   2015 		 * for writing filemarks as a no-op.
   2016 		 */
   2017 		if (error != 0 && error != EACCES && error != EROFS)
   2018 			return (error);
   2019 	}
   2020 
   2021 	memset(&cmd, 0, sizeof(cmd));
   2022 	memset(&posdata, 0, sizeof(posdata));
   2023 	cmd.opcode = READ_POSITION;
   2024 	if (hard)
   2025 		cmd.byte1 = 1;
   2026 
   2027 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd),
   2028 	    (void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
   2029 	    XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
   2030 
   2031 	if (error == 0) {
   2032 #if	0
   2033 		printf("posdata:");
   2034 		for (hard = 0; hard < sizeof(posdata); hard++)
   2035 			printf("%02x ", posdata[hard] & 0xff);
   2036 		printf("\n");
   2037 #endif
   2038 		if (posdata[0] & 0x4)	/* Block Position Unknown */
   2039 			error = EINVAL;
   2040 		else
   2041 			*blkptr = _4btol(&posdata[4]);
   2042 	}
   2043 	return (error);
   2044 }
   2045 
   2046 static int
   2047 st_setpos(struct st_softc *st, int hard, u_int32_t *blkptr)
   2048 {
   2049 	int error;
   2050 	struct scsi_tape_locate cmd;
   2051 
   2052 	/*
   2053 	 * We used to try and flush any buffered writes here.
   2054 	 * Now we push this onto user applications to either
   2055 	 * flush the pending writes themselves (via a zero count
   2056 	 * WRITE FILEMARKS command) or they can trust their tape
   2057 	 * drive to do this correctly for them.
   2058 	 *
   2059 	 * There are very ugly performance limitations otherwise.
   2060 	 */
   2061 
   2062 	memset(&cmd, 0, sizeof(cmd));
   2063 	cmd.opcode = LOCATE;
   2064 	if (hard)
   2065 		cmd.byte2 = 1 << 2;
   2066 	_lto4b(*blkptr, cmd.blkaddr);
   2067 	error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
   2068 	    ST_RETRIES, ST_SPC_TIME, NULL, 0);
   2069 	/*
   2070 	 * Note file && block number position now unknown (if
   2071 	 * these things ever start being maintained in this driver)
   2072 	 */
   2073 	st->fileno = st->blkno = -1;
   2074 	return (error);
   2075 }
   2076 
   2077 
   2078 /*
   2079  * Look at the returned sense and act on the error and determine
   2080  * the unix error number to pass back..., 0 (== report no error),
   2081  * -1 = retry the operation, -2 continue error processing.
   2082  */
   2083 static int
   2084 st_interpret_sense(struct scsipi_xfer *xs)
   2085 {
   2086 	struct scsipi_periph *periph = xs->xs_periph;
   2087 	struct scsi_sense_data *sense = &xs->sense.scsi_sense;
   2088 	struct buf *bp = xs->bp;
   2089 	struct st_softc *st = (void *)periph->periph_dev;
   2090 	int retval = EJUSTRETURN;
   2091 	int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
   2092 	u_int8_t key;
   2093 	int32_t info;
   2094 
   2095 	/*
   2096 	 * If it isn't a extended or extended/deferred error, let
   2097 	 * the generic code handle it.
   2098 	 */
   2099 	if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
   2100 	    SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
   2101 		return (retval);
   2102 
   2103 	if (sense->response_code & SSD_RCODE_VALID)
   2104 		info = _4btol(sense->info);
   2105 	else
   2106 		info = (st->flags & ST_FIXEDBLOCKS) ?
   2107 		    xs->datalen / st->blksize : xs->datalen;
   2108 	key = SSD_SENSE_KEY(sense->flags);
   2109 	st->mt_erreg = key;
   2110 	st->asc = sense->asc;
   2111 	st->ascq = sense->ascq;
   2112 	st->mt_resid = (short) info;
   2113 
   2114 	if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
   2115 		/* Not Ready, Logical Unit Is in Process Of Becoming Ready */
   2116 		if (!callout_pending(&periph->periph_callout))
   2117 			scsipi_periph_freeze(periph, 1);
   2118 		callout_reset(&periph->periph_callout,
   2119 		    hz, scsipi_periph_timed_thaw, periph);
   2120 		return (ERESTART);
   2121 	}
   2122 
   2123 	/*
   2124 	 * If the device is not open yet, let generic handle
   2125 	 */
   2126 	if ((periph->periph_flags & PERIPH_OPEN) == 0) {
   2127 		return (retval);
   2128 	}
   2129 
   2130 	xs->resid = info;
   2131 	if (st->flags & ST_FIXEDBLOCKS) {
   2132 		if (bp) {
   2133 			xs->resid *= st->blksize;
   2134 			st->last_io_resid = xs->resid;
   2135 		} else {
   2136 			st->last_ctl_resid = xs->resid;
   2137 		}
   2138 		if (key == SKEY_VOLUME_OVERFLOW) {
   2139 			st->flags |= ST_EIO_PENDING;
   2140 			if (bp)
   2141 				bp->b_resid = xs->resid;
   2142 		} else if (sense->flags & SSD_EOM) {
   2143 			if ((st->flags & ST_EARLYWARN) == 0)
   2144 				st->flags |= ST_EIO_PENDING;
   2145 			st->flags |= ST_EOM_PENDING;
   2146 			if (bp) {
   2147 #if 0
   2148 				bp->b_resid = xs->resid;
   2149 #else
   2150 				/*
   2151 				 * Grotesque as it seems, the few times
   2152 				 * I've actually seen a non-zero resid,
   2153 				 * the tape drive actually lied and had
   2154 				 * written all the data!
   2155 				 */
   2156 				bp->b_resid = 0;
   2157 #endif
   2158 			}
   2159 		}
   2160 		if (sense->flags & SSD_FILEMARK) {
   2161 			st->flags |= ST_AT_FILEMARK;
   2162 			if (bp)
   2163 				bp->b_resid = xs->resid;
   2164 			if (st->fileno != (daddr_t) -1) {
   2165 				st->fileno++;
   2166 				st->blkno = 0;
   2167 				st->flags |= ST_POSUPDATED;
   2168 			}
   2169 		}
   2170 		if (sense->flags & SSD_ILI) {
   2171 			st->flags |= ST_EIO_PENDING;
   2172 			if (bp)
   2173 				bp->b_resid = xs->resid;
   2174 			if (sense->response_code & SSD_RCODE_VALID &&
   2175 			    (xs->xs_control & XS_CTL_SILENT) == 0)
   2176 				aprint_error_dev(&st->sc_dev, "block wrong size, %d blocks "
   2177 				    "residual\n", info);
   2178 
   2179 			/*
   2180 			 * This quirk code helps the drive read
   2181 			 * the first tape block, regardless of
   2182 			 * format.  That is required for these
   2183 			 * drives to return proper MODE SENSE
   2184 			 * information.
   2185 			 */
   2186 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   2187 			    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   2188 				st->blksize -= 512;
   2189 			else if ((st->flags & ST_POSUPDATED) == 0) {
   2190 				if (st->blkno != (daddr_t) -1) {
   2191 					st->blkno +=
   2192 					    (xs->datalen / st->blksize);
   2193 					st->flags |= ST_POSUPDATED;
   2194 				}
   2195 			}
   2196 		}
   2197 		/*
   2198 		 * If data wanted and no data was transferred, do it immediately
   2199 		 */
   2200 		if (xs->datalen && xs->resid >= xs->datalen) {
   2201 			if (st->flags & ST_EIO_PENDING)
   2202 				return (EIO);
   2203 			if (st->flags & ST_AT_FILEMARK) {
   2204 				if (bp)
   2205 					bp->b_resid = xs->resid;
   2206 				return (0);
   2207 			}
   2208 		}
   2209 	} else {		/* must be variable mode */
   2210 		if (bp) {
   2211 			st->last_io_resid = xs->resid;
   2212 		} else {
   2213 			st->last_ctl_resid = xs->resid;
   2214 		}
   2215 		if (sense->flags & SSD_EOM) {
   2216 			/*
   2217 			 * The current semantics of this
   2218 			 * driver requires EOM detection
   2219 			 * to return EIO unless early
   2220 			 * warning detection is enabled
   2221 			 * for variable mode (this is always
   2222 			 * on for fixed block mode).
   2223 			 */
   2224 			if (st->flags & ST_EARLYWARN) {
   2225 				st->flags |= ST_EOM_PENDING;
   2226 				retval = 0;
   2227 			} else {
   2228 				retval = EIO;
   2229 			}
   2230 
   2231 			/*
   2232 			 * If it's an unadorned EOM detection,
   2233 			 * suppress printing an error.
   2234 			 */
   2235 			if (key == SKEY_NO_SENSE) {
   2236 				doprint = 0;
   2237 			}
   2238 		} else if (sense->flags & SSD_FILEMARK) {
   2239 			retval = 0;
   2240 			if (st->fileno != (daddr_t) -1) {
   2241 				st->fileno++;
   2242 				st->blkno = 0;
   2243 				st->flags |= ST_POSUPDATED;
   2244 			}
   2245 		} else if (sense->flags & SSD_ILI) {
   2246 			if (info < 0) {
   2247 				/*
   2248 				 * The tape record was bigger than the read
   2249 				 * we issued.
   2250 				 */
   2251 				if ((xs->xs_control & XS_CTL_SILENT) == 0) {
   2252 					aprint_error_dev(&st->sc_dev,
   2253 					    "%d-byte tape record too big"
   2254 					    " for %d-byte user buffer\n",
   2255 					    xs->datalen - info, xs->datalen);
   2256 				}
   2257 				retval = EIO;
   2258 			} else {
   2259 				retval = 0;
   2260 				if (st->blkno != (daddr_t) -1) {
   2261 					st->blkno++;
   2262 					st->flags |= ST_POSUPDATED;
   2263 				}
   2264 			}
   2265 		}
   2266 		if (bp)
   2267 			bp->b_resid = info;
   2268 	}
   2269 
   2270 #ifndef SCSIPI_DEBUG
   2271 	if (retval == 0 && key == SKEY_NO_SENSE)
   2272 		doprint = 0;
   2273 #endif
   2274 	if (key == SKEY_BLANK_CHECK) {
   2275 		/*
   2276 		 * This quirk code helps the drive read the
   2277 		 * first tape block, regardless of format.  That
   2278 		 * is required for these drives to return proper
   2279 		 * MODE SENSE information.
   2280 		 */
   2281 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   2282 		    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
   2283 			/* still starting */
   2284 			st->blksize -= 512;
   2285 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   2286 			st->flags |= ST_BLANK_READ;
   2287 			xs->resid = xs->datalen;
   2288 			if (bp) {
   2289 				bp->b_resid = xs->resid;
   2290 				/* return an EOF */
   2291 			}
   2292 			retval = 0;
   2293 			/* lost position */
   2294 			st->fileno = st->blkno = -1;
   2295 		}
   2296 	}
   2297 
   2298 	/*
   2299 	 * If generic sense processing will continue, we should not
   2300 	 * print sense info here.
   2301 	 */
   2302 	if (retval == EJUSTRETURN)
   2303 		doprint = 0;
   2304 
   2305 	if (doprint) {
   2306 #ifdef	SCSIVERBOSE
   2307 		scsipi_print_sense(xs, 0);
   2308 #else
   2309 		scsipi_printaddr(periph);
   2310 		printf("Sense Key 0x%02x", key);
   2311 		if ((sense->response_code & SSD_RCODE_VALID) != 0) {
   2312 			switch (key) {
   2313 			case SKEY_NOT_READY:
   2314 			case SKEY_ILLEGAL_REQUEST:
   2315 			case SKEY_UNIT_ATTENTION:
   2316 			case SKEY_DATA_PROTECT:
   2317 				break;
   2318 			case SKEY_VOLUME_OVERFLOW:
   2319 			case SKEY_BLANK_CHECK:
   2320 				printf(", requested size: %d (decimal)", info);
   2321 				break;
   2322 			case SKEY_ABORTED_COMMAND:
   2323 				if (xs->xs_retries)
   2324 					printf(", retrying");
   2325 				printf(", cmd 0x%x, info 0x%x",
   2326 				    xs->cmd->opcode, info);
   2327 				break;
   2328 			default:
   2329 				printf(", info = %d (decimal)", info);
   2330 			}
   2331 		}
   2332 		if (sense->extra_len != 0) {
   2333 			int n;
   2334 			printf(", data =");
   2335 			for (n = 0; n < sense->extra_len; n++)
   2336 				printf(" %02x", sense->csi[n]);
   2337 		}
   2338 		printf("\n");
   2339 #endif
   2340 	}
   2341 	return (retval);
   2342 }
   2343 
   2344 /*
   2345  * The quirk here is that the drive returns some value to st_mode_sense
   2346  * incorrectly until the tape has actually passed by the head.
   2347  *
   2348  * The method is to set the drive to large fixed-block state (user-specified
   2349  * density and 1024-byte blocks), then read and rewind to get it to sense the
   2350  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   2351  * work, as a last resort, try variable- length blocks.  The result will be
   2352  * the ability to do an accurate st_mode_sense.
   2353  *
   2354  * We know we can do a rewind because we just did a load, which implies rewind.
   2355  * Rewind seems preferable to space backward if we have a virgin tape.
   2356  *
   2357  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   2358  * error processing, both part of st_interpret_sense.
   2359  */
   2360 static int
   2361 st_touch_tape(struct st_softc *st)
   2362 {
   2363 	char *bf;
   2364 	int readsize;
   2365 	int error;
   2366 
   2367 	bf = malloc(1024, M_TEMP, M_NOWAIT);
   2368 	if (bf == NULL)
   2369 		return (ENOMEM);
   2370 
   2371 	if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
   2372 		goto bad;
   2373 
   2374 	/*
   2375 	 * If the block size is already known from the
   2376 	 * sense data, use it. Else start probing at 1024.
   2377 	 */
   2378 	if (st->media_blksize > 0)
   2379 		st->blksize = st->media_blksize;
   2380 	else
   2381 		st->blksize = 1024;
   2382 
   2383 	do {
   2384 		switch (st->blksize) {
   2385 		case 512:
   2386 		case 1024:
   2387 			readsize = st->blksize;
   2388 			st->flags |= ST_FIXEDBLOCKS;
   2389 			break;
   2390 		default:
   2391 			readsize = 1;
   2392 			st->flags &= ~ST_FIXEDBLOCKS;
   2393 		}
   2394 		if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
   2395 		    != 0) {
   2396 			/*
   2397 			 * The device did not agree with the proposed
   2398 			 * block size. If we exhausted our options,
   2399 			 * return failure, else try another.
   2400 			 */
   2401 			if (readsize == 1)
   2402 				goto bad;
   2403 			st->blksize -= 512;
   2404 			continue;
   2405 		}
   2406 		st_read(st, bf, readsize, XS_CTL_SILENT);	/* XXX */
   2407 		if ((error = st_rewind(st, 0, 0)) != 0) {
   2408 bad:			free(bf, M_TEMP);
   2409 			return (error);
   2410 		}
   2411 	} while (readsize != 1 && readsize > st->blksize);
   2412 
   2413 	free(bf, M_TEMP);
   2414 	return (0);
   2415 }
   2416 
   2417 static int
   2418 stdump(dev_t dev, daddr_t blkno, void *va,
   2419     size_t size)
   2420 {
   2421 
   2422 	/* Not implemented. */
   2423 	return (ENXIO);
   2424 }
   2425