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