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