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