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