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