Home | History | Annotate | Line # | Download | only in scsipi
st.c revision 1.55
      1 /*	$NetBSD: st.c,v 1.55 1995/11/30 10:12:04 pk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) tfs.com)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     48  */
     49 
     50 /*
     51  * To do:
     52  * work out some better way of guessing what a good timeout is going
     53  * to be depending on whether we expect to retension or not.
     54  */
     55 
     56 #include <sys/types.h>
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/fcntl.h>
     60 #include <sys/errno.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/malloc.h>
     63 #include <sys/buf.h>
     64 #include <sys/proc.h>
     65 #include <sys/user.h>
     66 #include <sys/mtio.h>
     67 #include <sys/device.h>
     68 
     69 #include <scsi/scsi_all.h>
     70 #include <scsi/scsi_tape.h>
     71 #include <scsi/scsiconf.h>
     72 
     73 /* Defines for device specific stuff */
     74 #define DEF_FIXED_BSIZE  512
     75 #define	ST_RETRIES	4	/* only on non IO commands */
     76 
     77 #define STMODE(z)	( minor(z)       & 0x03)
     78 #define STDSTY(z)	((minor(z) >> 2) & 0x03)
     79 #define STUNIT(z)	((minor(z) >> 4)       )
     80 #define CTLMODE	3
     81 
     82 #define SCSI_2_MAX_DENSITY_CODE	0x17	/* maximum density code specified
     83 					 * in SCSI II spec. */
     84 /*
     85  * Define various devices that we know mis-behave in some way,
     86  * and note how they are bad, so we can correct for them
     87  */
     88 struct modes {
     89 	u_int quirks;			/* same definitions as in quirkdata */
     90 	int blksize;
     91 	u_int8_t density;
     92 };
     93 
     94 struct quirkdata {
     95 	u_int quirks;
     96 #define	ST_Q_FORCE_BLKSIZE	0x0001
     97 #define	ST_Q_SENSE_HELP		0x0002	/* must do READ for good MODE SENSE */
     98 #define	ST_Q_IGNORE_LOADS	0x0004
     99 #define	ST_Q_BLKSIZE		0x0008	/* variable-block media_blksize > 0 */
    100 	u_int page_0_size;
    101 #define	MAX_PAGE_0_SIZE	64
    102 	struct modes modes[4];
    103 };
    104 
    105 struct st_quirk_inquiry_pattern {
    106 	struct scsi_inquiry_pattern pattern;
    107 	struct quirkdata quirkdata;
    108 };
    109 
    110 struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
    111 	{T_SEQUENTIAL, T_REMOV,
    112 	 "        ", "                ", "    ", 0, 0, {
    113 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    114 		ST_Q_FORCE_BLKSIZE, 512, QIC_24,	/* minor 4-7 */
    115 		ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600,	/* minor 8-11 */
    116 		ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250	/* minor 12-15 */
    117 	}},
    118 	{T_SEQUENTIAL, T_REMOV,
    119 	 "TANDBERG", " TDC 3600       ", "",     0, 12, {
    120 		0, 0, 0,				/* minor 0-3 */
    121 		ST_Q_FORCE_BLKSIZE, 0, QIC_525,		/* minor 4-7 */
    122 		0, 0, QIC_150,				/* minor 8-11 */
    123 		0, 0, QIC_120				/* minor 12-15 */
    124 	}},
    125 	{T_SEQUENTIAL, T_REMOV,
    126 	 "TANDBERG", " TDC 3800       ", "",     0, 0, {
    127 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    128 		0, 0, QIC_525,				/* minor 4-7 */
    129 		0, 0, QIC_150,				/* minor 8-11 */
    130 		0, 0, QIC_120				/* minor 12-15 */
    131 	}},
    132 	/*
    133 	 * At least -005 and -007 need this.  I'll assume they all do unless I
    134 	 * hear otherwise.  - mycroft, 31MAR1994
    135 	 */
    136 	{T_SEQUENTIAL, T_REMOV,
    137 	 "ARCHIVE ", "VIPER 2525 25462", "",     0, 0, {
    138 		ST_Q_SENSE_HELP, 0, 0,			/* minor 0-3 */
    139 		ST_Q_SENSE_HELP, 0, QIC_525,		/* minor 4-7 */
    140 		0, 0, QIC_150,				/* minor 8-11 */
    141 		0, 0, QIC_120				/* minor 12-15 */
    142 	}},
    143 	/*
    144 	 * One user reports that this works for his tape drive.  It probably
    145 	 * needs more work.  - mycroft, 09APR1994
    146 	 */
    147 	{T_SEQUENTIAL, T_REMOV,
    148 	 "SANKYO  ", "CP525           ", "",     0, 0, {
    149 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    150 		ST_Q_FORCE_BLKSIZE, 512, QIC_525,	/* minor 4-7 */
    151 		0, 0, QIC_150,				/* minor 8-11 */
    152 		0, 0, QIC_120				/* minor 12-15 */
    153 	}},
    154 	{T_SEQUENTIAL, T_REMOV,
    155 	 "ANRITSU ", "DMT780          ", "",     0, 0, {
    156 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    157 		ST_Q_FORCE_BLKSIZE, 512, QIC_525,	/* minor 4-7 */
    158 		0, 0, QIC_150,				/* minor 8-11 */
    159 		0, 0, QIC_120				/* minor 12-15 */
    160 	}},
    161 	{T_SEQUENTIAL, T_REMOV,
    162 	 "ARCHIVE ", "VIPER 150  21247", "",     0, 12, {
    163 		0, 0, 0,				/* minor 0-3 */
    164 		0, 0, QIC_150,				/* minor 4-7 */
    165 		0, 0, QIC_120,				/* minor 8-11 */
    166 		0, 0, QIC_24				/* minor 12-15 */
    167 	}},
    168 	{T_SEQUENTIAL, T_REMOV,
    169 	 "WANGTEK ", "5099ES SCSI", "",          0, 0, {
    170 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    171 		0, 0, QIC_11,				/* minor 4-7 */
    172 		0, 0, QIC_24,				/* minor 8-11 */
    173 		0, 0, QIC_24,				/* minor 12-15 */
    174 	}},
    175 	{T_SEQUENTIAL, T_REMOV,
    176 	 "WANGTEK ", "5150ES SCSI", "",          0, 0, {
    177 		ST_Q_FORCE_BLKSIZE, 512, 0,		/* minor 0-3 */
    178 		0, 0, QIC_24,				/* minor 4-7 */
    179 		0, 0, QIC_120,				/* minor 8-11 */
    180 		0, 0, QIC_150				/* minor 12-15 */
    181 	}},
    182 	{T_SEQUENTIAL, T_REMOV,
    183 	 "WANGTEK ", "5525ES SCSI REV7", "",     0, 0, {
    184 		0, 0, 0,				/* minor 0-3 */
    185 		ST_Q_BLKSIZE, 0, QIC_525,		/* minor 4-7 */
    186 		0, 0, QIC_150,				/* minor 8-11 */
    187 		0, 0, QIC_120				/* minor 12-15 */
    188 	}},
    189 	{T_SEQUENTIAL, T_REMOV,
    190 	 "WangDAT ", "Model 1300      ", "",     0, 0, {
    191 		0, 0, 0,				/* minor 0-3 */
    192 		ST_Q_FORCE_BLKSIZE, 512, DDS,		/* minor 4-7 */
    193 		ST_Q_FORCE_BLKSIZE, 1024, DDS,		/* minor 8-11 */
    194 		ST_Q_FORCE_BLKSIZE, 0, DDS		/* minor 12-15 */
    195 	}},
    196 	{T_SEQUENTIAL, T_REMOV,
    197 	 "EXABYTE ", "EXB-8200        ", "263H", 0, 5, {
    198 		0, 0, 0,				/* minor 0-3 */
    199 		0, 0, 0,				/* minor 4-7 */
    200 		0, 0, 0,				/* minor 8-11 */
    201 		0, 0, 0					/* minor 12-15 */
    202 	}},
    203 #if 0
    204 	{T_SEQUENTIAL, T_REMOV,
    205 	 "EXABYTE ", "EXB-8200        ", "",     0, 12, {
    206 		0, 0, 0,				/* minor 0-3 */
    207 		0, 0, 0,				/* minor 4-7 */
    208 		0, 0, 0,				/* minor 8-11 */
    209 		0, 0, 0					/* minor 12-15 */
    210 	}},
    211 #endif
    212 };
    213 
    214 #define NOEJECT 0
    215 #define EJECT 1
    216 
    217 struct st_softc {
    218 	struct device sc_dev;
    219 /*--------------------present operating parameters, flags etc.----------------*/
    220 	int flags;		/* see below                          */
    221 	u_int quirks;		/* quirks for the open mode           */
    222 	int blksize;		/* blksize we are using                */
    223 	u_int8_t density;	/* present density                    */
    224 	u_int page_0_size;	/* size of page 0 data		      */
    225 	u_int last_dsty;	/* last density opened               */
    226 /*--------------------device/scsi parameters----------------------------------*/
    227 	struct scsi_link *sc_link;	/* our link to the adpter etc.        */
    228 /*--------------------parameters reported by the device ----------------------*/
    229 	int blkmin;		/* min blk size                       */
    230 	int blkmax;		/* max blk size                       */
    231 	struct quirkdata *quirkdata;	/* if we have a rogue entry           */
    232 /*--------------------parameters reported by the device for this media--------*/
    233 	u_long numblks;		/* nominal blocks capacity            */
    234 	int media_blksize;	/* 0 if not ST_FIXEDBLOCKS            */
    235 	u_int8_t media_density;	/* this is what it said when asked    */
    236 /*--------------------quirks for the whole drive------------------------------*/
    237 	u_int drive_quirks;	/* quirks of this drive               */
    238 /*--------------------How we should set up when opening each minor device----*/
    239 	struct modes modes[4];	/* plus more for each mode            */
    240 	u_int8_t  modeflags[4];	/* flags for the modes                */
    241 #define DENSITY_SET_BY_USER	0x01
    242 #define DENSITY_SET_BY_QUIRK	0x02
    243 #define BLKSIZE_SET_BY_USER	0x04
    244 #define BLKSIZE_SET_BY_QUIRK	0x08
    245 /*--------------------storage for sense data returned by the drive------------*/
    246 	u_char sense_data[MAX_PAGE_0_SIZE];	/*
    247 						 * additional sense data needed
    248 						 * for mode sense/select.
    249 						 */
    250 	struct buf buf_queue;		/* the queue of pending IO operations */
    251 };
    252 
    253 int stmatch __P((struct device *, void *, void *));
    254 void stattach __P((struct device *, struct device *, void *));
    255 
    256 struct cfdriver stcd = {
    257 	NULL, "st", stmatch, stattach, DV_TAPE, sizeof(struct st_softc)
    258 };
    259 
    260 int	st_space __P((struct st_softc *, int number, u_int what, int flags));
    261 int	st_rewind __P((struct st_softc *, u_int immediate, int flags));
    262 int	st_mode_sense __P((struct st_softc *, int flags));
    263 int	st_decide_mode __P((struct st_softc *, boolean first_read));
    264 int	st_read_block_limits __P((struct st_softc *, int flags));
    265 int	st_touch_tape __P((struct st_softc *));
    266 int	st_write_filemarks __P((struct st_softc *, int number, int flags));
    267 int	st_load __P((struct st_softc *, u_int type, int flags));
    268 int	st_mode_select __P((struct st_softc *, int flags));
    269 void    ststrategy();
    270 int	st_check_eod();
    271 void    ststart();
    272 void	st_unmount();
    273 int	st_mount_tape();
    274 void	st_loadquirks();
    275 void	st_identify_drive();
    276 int	st_interpret_sense();
    277 
    278 struct scsi_device st_switch = {
    279 	st_interpret_sense,
    280 	ststart,
    281 	NULL,
    282 	NULL,
    283 };
    284 
    285 #define	ST_INFO_VALID	0x0001
    286 #define	ST_BLOCK_SET	0x0002	/* block size, mode set by ioctl      */
    287 #define	ST_WRITTEN	0x0004	/* data have been written, EOD needed */
    288 #define	ST_FIXEDBLOCKS	0x0008
    289 #define	ST_AT_FILEMARK	0x0010
    290 #define	ST_EIO_PENDING	0x0020	/* we couldn't report it then (had data) */
    291 #define	ST_NEW_MOUNT	0x0040	/* still need to decide mode              */
    292 #define	ST_READONLY	0x0080	/* st_mode_sense says write protected */
    293 #define	ST_FM_WRITTEN	0x0100	/*
    294 				 * EOF file mark written  -- used with
    295 				 * ~ST_WRITTEN to indicate that multiple file
    296 				 * marks have been written
    297 				 */
    298 #define	ST_BLANK_READ	0x0200	/* BLANK CHECK encountered already */
    299 #define	ST_2FM_AT_EOD	0x0400	/* write 2 file marks at EOD */
    300 #define	ST_MOUNTED	0x0800	/* Device is presently mounted */
    301 
    302 #define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
    303 #define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
    304 			 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
    305 			 ST_2FM_AT_EOD | ST_PER_ACTION)
    306 
    307 struct scsi_inquiry_pattern st_patterns[] = {
    308 	{T_SEQUENTIAL, T_REMOV,
    309 	 "",         "",                 ""},
    310 };
    311 
    312 int
    313 stmatch(parent, match, aux)
    314 	struct device *parent;
    315 	void *match, *aux;
    316 {
    317 	struct cfdata *cf = match;
    318 	struct scsibus_attach_args *sa = aux;
    319 	int priority;
    320 
    321 	(void)scsi_inqmatch(sa->sa_inqbuf,
    322 	    (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
    323 	    sizeof(st_patterns[0]), &priority);
    324 	return (priority);
    325 }
    326 
    327 /*
    328  * The routine called by the low level scsi routine when it discovers
    329  * A device suitable for this driver
    330  */
    331 void
    332 stattach(parent, self, aux)
    333 	struct device *parent, *self;
    334 	void *aux;
    335 {
    336 	struct st_softc *st = (void *)self;
    337 	struct scsibus_attach_args *sa = aux;
    338 	struct scsi_link *sc_link = sa->sa_sc_link;
    339 
    340 	SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
    341 
    342 	/*
    343 	 * Store information needed to contact our base driver
    344 	 */
    345 	st->sc_link = sc_link;
    346 	sc_link->device = &st_switch;
    347 	sc_link->device_softc = st;
    348 	sc_link->openings = 1;
    349 
    350 	/*
    351 	 * Check if the drive is a known criminal and take
    352 	 * Any steps needed to bring it into line
    353 	 */
    354 	st_identify_drive(st, sa->sa_inqbuf);
    355 
    356 	/*
    357 	 * Use the subdriver to request information regarding
    358 	 * the drive. We cannot use interrupts yet, so the
    359 	 * request must specify this.
    360 	 */
    361 	printf(": %s", st->quirkdata ? "rogue, " : "");
    362 	if (scsi_test_unit_ready(sc_link,
    363 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) ||
    364 	    st_mode_sense(st,
    365 	    SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE))
    366 		printf("drive empty\n");
    367 	else {
    368 		printf("density code 0x%x, ", st->media_density);
    369 		if (st->media_blksize > 0)
    370 			printf("%d-byte", st->media_blksize);
    371 		else
    372 			printf("variable");
    373 		printf(" blocks, write-%s\n",
    374 		    (st->flags & ST_READONLY) ? "protected" : "enabled");
    375 	}
    376 
    377 	/*
    378 	 * Set up the buf queue for this device
    379 	 */
    380 	st->buf_queue.b_active = 0;
    381 	st->buf_queue.b_actf = 0;
    382 	st->buf_queue.b_actb = &st->buf_queue.b_actf;
    383 }
    384 
    385 /*
    386  * Use the inquiry routine in 'scsi_base' to get drive info so we can
    387  * Further tailor our behaviour.
    388  */
    389 void
    390 st_identify_drive(st, inqbuf)
    391 	struct st_softc *st;
    392 	struct scsi_inquiry_data *inqbuf;
    393 {
    394 	struct st_quirk_inquiry_pattern *finger;
    395 	int priority;
    396 
    397 	finger = (struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf,
    398 	    (caddr_t)st_quirk_patterns,
    399 	    sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]),
    400 	    sizeof(st_quirk_patterns[0]), &priority);
    401 	if (priority != 0) {
    402 		st->quirkdata = &finger->quirkdata;
    403 		st->drive_quirks = finger->quirkdata.quirks;
    404 		st->quirks = finger->quirkdata.quirks;	/* start value */
    405 		st->page_0_size = finger->quirkdata.page_0_size;
    406 		st_loadquirks(st);
    407 	}
    408 }
    409 
    410 /*
    411  * initialise the subdevices to the default (QUIRK) state.
    412  * this will remove any setting made by the system operator or previous
    413  * operations.
    414  */
    415 void
    416 st_loadquirks(st)
    417 	struct st_softc *st;
    418 {
    419 	int i;
    420 	struct	modes *mode;
    421 	struct	modes *mode2;
    422 
    423 	mode = st->quirkdata->modes;
    424 	mode2 = st->modes;
    425 	for (i = 0; i < 4; i++) {
    426 		bzero(mode2, sizeof(struct modes));
    427 		st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
    428 		    DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
    429 		    DENSITY_SET_BY_USER);
    430 		if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
    431 			mode2->blksize = mode->blksize;
    432 			st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
    433 		}
    434 		if (mode->density) {
    435 			mode2->density = mode->density;
    436 			st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
    437 		}
    438 		mode++;
    439 		mode2++;
    440 	}
    441 }
    442 
    443 /*
    444  * open the device.
    445  */
    446 int
    447 stopen(dev, flags)
    448 	dev_t dev;
    449 	int flags;
    450 {
    451 	int unit;
    452 	u_int mode, dsty;
    453 	int error = 0;
    454 	struct st_softc *st;
    455 	struct scsi_link *sc_link;
    456 
    457 	unit = STUNIT(dev);
    458 	if (unit >= stcd.cd_ndevs)
    459 		return ENXIO;
    460 	st = stcd.cd_devs[unit];
    461 	if (!st)
    462 		return ENXIO;
    463 
    464 	mode = STMODE(dev);
    465 	dsty = STDSTY(dev);
    466 	sc_link = st->sc_link;
    467 
    468 	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
    469 	    unit, stcd.cd_ndevs));
    470 
    471 	/*
    472 	 * Only allow one at a time
    473 	 */
    474 	if (sc_link->flags & SDEV_OPEN) {
    475 		printf("%s: already open\n", st->sc_dev.dv_xname);
    476 		return EBUSY;
    477 	}
    478 
    479 	/*
    480 	 * Catch any unit attention errors.
    481 	 */
    482 	if (error = scsi_test_unit_ready(sc_link,
    483 	    SCSI_IGNORE_MEDIA_CHANGE | (mode == CTLMODE ? SCSI_IGNORE_NOT_READY : 0)))
    484 		goto bad;
    485 
    486 	sc_link->flags |= SDEV_OPEN;	/* unit attn are now errors */
    487 
    488 	/*
    489 	 * If the mode is 3 (e.g. minor = 3,7,11,15)
    490 	 * then the device has been opened to set defaults
    491 	 * This mode does NOT ALLOW I/O, only ioctls
    492 	 */
    493 	if (mode == CTLMODE)
    494 		return 0;
    495 
    496 	/*
    497 	 * if it's a different mode, or if the media has been
    498 	 * invalidated, unmount the tape from the previous
    499 	 * session but continue with open processing
    500 	 */
    501 	if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
    502 		st_unmount(st, NOEJECT);
    503 
    504 	/*
    505 	 * If we are not mounted, then we should start a new
    506 	 * mount session.
    507 	 */
    508 	if (!(st->flags & ST_MOUNTED)) {
    509 		st_mount_tape(dev, flags);
    510 		st->last_dsty = dsty;
    511 	}
    512 
    513 	/*
    514 	 * Make sure that a tape opened in write-only mode will have
    515 	 * file marks written on it when closed, even if not written to.
    516 	 * This is for SUN compatibility
    517 	 */
    518 	if ((flags & O_ACCMODE) == FWRITE)
    519 		st->flags |= ST_WRITTEN;
    520 
    521 	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
    522 	return 0;
    523 
    524 bad:
    525 	st_unmount(st, NOEJECT);
    526 	sc_link->flags &= ~SDEV_OPEN;
    527 	return error;
    528 }
    529 
    530 /*
    531  * close the device.. only called if we are the LAST
    532  * occurence of an open device
    533  */
    534 int
    535 stclose(dev)
    536 	dev_t dev;
    537 {
    538 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
    539 
    540 	SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
    541 	if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
    542 		st_write_filemarks(st, 1, 0);
    543 	switch (STMODE(dev)) {
    544 	case 0:
    545 	case 3:		/* for now */
    546 		st_unmount(st, NOEJECT);
    547 		break;
    548 	case 1:
    549 		/* leave mounted unless media seems to have been removed */
    550 		if (!(st->sc_link->flags & SDEV_MEDIA_LOADED))
    551 			st_unmount(st, NOEJECT);
    552 		break;
    553 	case 2:
    554 		st_unmount(st, EJECT);
    555 		break;
    556 	}
    557 	st->sc_link->flags &= ~SDEV_OPEN;
    558 
    559 	return 0;
    560 }
    561 
    562 /*
    563  * Start a new mount session.
    564  * Copy in all the default parameters from the selected device mode.
    565  * and try guess any that seem to be defaulted.
    566  */
    567 int
    568 st_mount_tape(dev, flags)
    569 	dev_t dev;
    570 	int flags;
    571 {
    572 	int unit;
    573 	u_int mode, dsty;
    574 	struct st_softc *st;
    575 	struct scsi_link *sc_link;
    576 	int error = 0;
    577 
    578 	unit = STUNIT(dev);
    579 	mode = STMODE(dev);
    580 	dsty = STDSTY(dev);
    581 	st = stcd.cd_devs[unit];
    582 	sc_link = st->sc_link;
    583 
    584 	if (st->flags & ST_MOUNTED)
    585 		return 0;
    586 
    587 	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
    588 	st->flags |= ST_NEW_MOUNT;
    589 	st->quirks = st->drive_quirks | st->modes[dsty].quirks;
    590 	/*
    591 	 * If the media is new, then make sure we give it a chance to
    592 	 * to do a 'load' instruction.  (We assume it is new.)
    593 	 */
    594 	if (error = st_load(st, LD_LOAD, 0))
    595 		return error;
    596 	/*
    597 	 * Throw another dummy instruction to catch
    598 	 * 'Unit attention' errors. Some drives appear to give
    599 	 * these after doing a Load instruction.
    600 	 * (noteably some DAT drives)
    601 	 */
    602 	scsi_test_unit_ready(sc_link, SCSI_SILENT);	/* XXX */
    603 
    604 	/*
    605 	 * Some devices can't tell you much until they have been
    606 	 * asked to look at the media. This quirk does this.
    607 	 */
    608 	if (st->quirks & ST_Q_SENSE_HELP)
    609 		if (error = st_touch_tape(st))
    610 			return error;
    611 	/*
    612 	 * Load the physical device parameters
    613 	 * loads: blkmin, blkmax
    614 	 */
    615 	if (error = st_read_block_limits(st, 0))
    616 		return error;
    617 	/*
    618 	 * Load the media dependent parameters
    619 	 * includes: media_blksize,media_density,numblks
    620 	 * As we have a tape in, it should be reflected here.
    621 	 * If not you may need the "quirk" above.
    622 	 */
    623 	if (error = st_mode_sense(st, 0))
    624 		return error;
    625 	/*
    626 	 * If we have gained a permanent density from somewhere,
    627 	 * then use it in preference to the one supplied by
    628 	 * default by the driver.
    629 	 */
    630 	if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
    631 		st->density = st->modes[dsty].density;
    632 	else
    633 		st->density = st->media_density;
    634 	/*
    635 	 * If we have gained a permanent blocksize
    636 	 * then use it in preference to the one supplied by
    637 	 * default by the driver.
    638 	 */
    639 	st->flags &= ~ST_FIXEDBLOCKS;
    640 	if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
    641 		st->blksize = st->modes[dsty].blksize;
    642 		if (st->blksize)
    643 			st->flags |= ST_FIXEDBLOCKS;
    644 	} else {
    645 		if (error = st_decide_mode(st, FALSE))
    646 			return error;
    647 	}
    648 	if (error = st_mode_select(st, 0)) {
    649 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
    650 		return error;
    651 	}
    652 	scsi_prevent(sc_link, PR_PREVENT,
    653 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    654 	st->flags &= ~ST_NEW_MOUNT;
    655 	st->flags |= ST_MOUNTED;
    656 	sc_link->flags |= SDEV_MEDIA_LOADED;	/* move earlier? */
    657 
    658 	return 0;
    659 }
    660 
    661 /*
    662  * End the present mount session.
    663  * Rewind, and optionally eject the tape.
    664  * Reset various flags to indicate that all new
    665  * operations require another mount operation
    666  */
    667 void
    668 st_unmount(st, eject)
    669 	struct st_softc *st;
    670 	boolean eject;
    671 {
    672 	struct scsi_link *sc_link = st->sc_link;
    673 	int nmarks;
    674 
    675 	if (!(st->flags & ST_MOUNTED))
    676 		return;
    677 	SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
    678 	st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY);
    679 	st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
    680 	scsi_prevent(sc_link, PR_ALLOW,
    681 	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    682 	if (eject)
    683 		st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
    684 	st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
    685 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
    686 }
    687 
    688 /*
    689  * Given all we know about the device, media, mode, 'quirks' and
    690  * initial operation, make a decision as to how we should be set
    691  * to run (regarding blocking and EOD marks)
    692  */
    693 int
    694 st_decide_mode(st, first_read)
    695 	struct st_softc *st;
    696 	boolean	first_read;
    697 {
    698 #ifdef SCSIDEBUG
    699 	struct scsi_link *sc_link = st->sc_link;
    700 #endif
    701 
    702 	SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
    703 
    704 	/*
    705 	 * If the drive can only handle fixed-length blocks and only at
    706 	 * one size, perhaps we should just do that.
    707 	 */
    708 	if (st->blkmin && (st->blkmin == st->blkmax)) {
    709 		st->flags |= ST_FIXEDBLOCKS;
    710 		st->blksize = st->blkmin;
    711 		SC_DEBUG(sc_link, SDEV_DB3,
    712 		    ("blkmin == blkmax of %d\n", st->blkmin));
    713 		goto done;
    714 	}
    715 	/*
    716 	 * If the tape density mandates (or even suggests) use of fixed
    717 	 * or variable-length blocks, comply.
    718 	 */
    719 	switch (st->density) {
    720 	case HALFINCH_800:
    721 	case HALFINCH_1600:
    722 	case HALFINCH_6250:
    723 	case DDS:
    724 		st->flags &= ~ST_FIXEDBLOCKS;
    725 		st->blksize = 0;
    726 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
    727 		goto done;
    728 	case QIC_11:
    729 	case QIC_24:
    730 	case QIC_120:
    731 	case QIC_150:
    732 	case QIC_525:
    733 	case QIC_1320:
    734 		st->flags |= ST_FIXEDBLOCKS;
    735 		if (st->media_blksize > 0)
    736 			st->blksize = st->media_blksize;
    737 		else
    738 			st->blksize = DEF_FIXED_BSIZE;
    739 		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
    740 		goto done;
    741 	}
    742 	/*
    743 	 * If we're about to read the tape, perhaps we should choose
    744 	 * fixed or variable-length blocks and block size according to
    745 	 * what the drive found on the tape.
    746 	 */
    747 	if (first_read &&
    748 	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
    749 	    (st->media_blksize == DEF_FIXED_BSIZE) ||
    750 	    (st->media_blksize == 1024))) {
    751 		if (st->media_blksize > 0)
    752 			st->flags |= ST_FIXEDBLOCKS;
    753 		else
    754 			st->flags &= ~ST_FIXEDBLOCKS;
    755 		st->blksize = st->media_blksize;
    756 		SC_DEBUG(sc_link, SDEV_DB3,
    757 		    ("Used media_blksize of %d\n", st->media_blksize));
    758 		goto done;
    759 	}
    760 	/*
    761 	 * We're getting no hints from any direction.  Choose variable-
    762 	 * length blocks arbitrarily.
    763 	 */
    764 	st->flags &= ~ST_FIXEDBLOCKS;
    765 	st->blksize = 0;
    766 	SC_DEBUG(sc_link, SDEV_DB3,
    767 	    ("Give up and default to variable mode\n"));
    768 
    769 done:
    770 	/*
    771 	 * Decide whether or not to write two file marks to signify end-
    772 	 * of-data.  Make the decision as a function of density.  If
    773 	 * the decision is not to use a second file mark, the SCSI BLANK
    774 	 * CHECK condition code will be recognized as end-of-data when
    775 	 * first read.
    776 	 * (I think this should be a by-product of fixed/variable..julian)
    777 	 */
    778 	switch (st->density) {
    779 /*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
    780 	case QIC_11:
    781 	case QIC_24:
    782 	case QIC_120:
    783 	case QIC_150:
    784 	case QIC_525:
    785 	case QIC_1320:
    786 		st->flags &= ~ST_2FM_AT_EOD;
    787 		break;
    788 	default:
    789 		st->flags |= ST_2FM_AT_EOD;
    790 	}
    791 	return 0;
    792 }
    793 
    794 /*
    795  * Actually translate the requested transfer into
    796  * one the physical driver can understand
    797  * The transfer is described by a buf and will include
    798  * only one physical transfer.
    799  */
    800 void
    801 ststrategy(bp)
    802 	struct buf *bp;
    803 {
    804 	struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)];
    805 	struct buf *dp;
    806 	int s;
    807 
    808 	SC_DEBUG(st->sc_link, SDEV_DB1,
    809 	    ("ststrategy %d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    810 	/*
    811 	 * If it's a null transfer, return immediatly
    812 	 */
    813 	if (bp->b_bcount == 0)
    814 		goto done;
    815 	/*
    816 	 * Odd sized request on fixed drives are verboten
    817 	 */
    818 	if (st->flags & ST_FIXEDBLOCKS) {
    819 		if (bp->b_bcount % st->blksize) {
    820 			printf("%s: bad request, must be multiple of %d\n",
    821 			    st->sc_dev.dv_xname, st->blksize);
    822 			bp->b_error = EIO;
    823 			goto bad;
    824 		}
    825 	}
    826 	/*
    827 	 * as are out-of-range requests on variable drives.
    828 	 */
    829 	else if (bp->b_bcount < st->blkmin ||
    830 		 (st->blkmax && bp->b_bcount > st->blkmax)) {
    831 		printf("%s: bad request, must be between %d and %d\n",
    832 		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
    833 		bp->b_error = EIO;
    834 		goto bad;
    835 	}
    836 	s = splbio();
    837 
    838 	/*
    839 	 * Place it in the queue of activities for this tape
    840 	 * at the end (a bit silly because we only have on user..
    841 	 * (but it could fork()))
    842 	 */
    843 	dp = &st->buf_queue;
    844 	bp->b_actf = NULL;
    845 	bp->b_actb = dp->b_actb;
    846 	*dp->b_actb = bp;
    847 	dp->b_actb = &bp->b_actf;
    848 
    849 	/*
    850 	 * Tell the device to get going on the transfer if it's
    851 	 * not doing anything, otherwise just wait for completion
    852 	 * (All a bit silly if we're only allowing 1 open but..)
    853 	 */
    854 	ststart(st);
    855 
    856 	splx(s);
    857 	return;
    858 bad:
    859 	bp->b_flags |= B_ERROR;
    860 done:
    861 	/*
    862 	 * Correctly set the buf to indicate a completed xfer
    863 	 */
    864 	iodone(bp);
    865 	return;
    866 }
    867 
    868 /*
    869  * ststart looks to see if there is a buf waiting for the device
    870  * and that the device is not already busy. If both are true,
    871  * It dequeues the buf and creates a scsi command to perform the
    872  * transfer required. The transfer request will call scsi_done
    873  * on completion, which will in turn call this routine again
    874  * so that the next queued transfer is performed.
    875  * The bufs are queued by the strategy routine (ststrategy)
    876  *
    877  * This routine is also called after other non-queued requests
    878  * have been made of the scsi driver, to ensure that the queue
    879  * continues to be drained.
    880  * ststart() is called at splbio
    881  */
    882 void
    883 ststart(st)
    884 	struct st_softc *st;
    885 {
    886 	struct scsi_link *sc_link = st->sc_link;
    887 	register struct buf *bp, *dp;
    888 	struct scsi_rw_tape cmd;
    889 	int flags;
    890 
    891 	SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
    892 	/*
    893 	 * See if there is a buf to do and we are not already
    894 	 * doing one
    895 	 */
    896 	while (sc_link->openings > 0) {
    897 		/* if a special awaits, let it proceed first */
    898 		if (sc_link->flags & SDEV_WAITING) {
    899 			sc_link->flags &= ~SDEV_WAITING;
    900 			wakeup((caddr_t)sc_link);
    901 			return;
    902 		}
    903 
    904 		bp = st->buf_queue.b_actf;
    905 		if (!bp)
    906 			return;	/* no work to bother with */
    907 		if (dp = bp->b_actf)
    908 			dp->b_actb = bp->b_actb;
    909 		else
    910 			st->buf_queue.b_actb = bp->b_actb;
    911 		*bp->b_actb = dp;
    912 
    913 		/*
    914 		 * if the device has been unmounted byt the user
    915 		 * then throw away all requests until done
    916 		 */
    917 		if (!(st->flags & ST_MOUNTED) ||
    918 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
    919 			/* make sure that one implies the other.. */
    920 			sc_link->flags &= ~SDEV_MEDIA_LOADED;
    921 			bp->b_flags |= B_ERROR;
    922 			bp->b_error = EIO;
    923 			biodone(bp);
    924 			continue;
    925 		}
    926 		/*
    927 		 * only FIXEDBLOCK devices have pending operations
    928 		 */
    929 		if (st->flags & ST_FIXEDBLOCKS) {
    930 			/*
    931 			 * If we are at a filemark but have not reported it yet
    932 			 * then we should report it now
    933 			 */
    934 			if (st->flags & ST_AT_FILEMARK) {
    935 				if ((bp->b_flags & B_READ) == B_WRITE) {
    936 					/*
    937 					 * Handling of ST_AT_FILEMARK in
    938 					 * st_space will fill in the right file
    939 					 * mark count.
    940 					 * Back up over filemark
    941 					 */
    942 					if (st_space(st, 0, SP_FILEMARKS, 0)) {
    943 						bp->b_flags |= B_ERROR;
    944 						bp->b_error = EIO;
    945 						biodone(bp);
    946 						continue;
    947 					}
    948 				} else {
    949 					bp->b_resid = bp->b_bcount;
    950 					bp->b_error = 0;
    951 					bp->b_flags &= ~B_ERROR;
    952 					st->flags &= ~ST_AT_FILEMARK;
    953 					biodone(bp);
    954 					continue;	/* seek more work */
    955 				}
    956 			}
    957 			/*
    958 			 * If we are at EIO (e.g. EOM) but have not reported it
    959 			 * yet then we should report it now
    960 			 */
    961 			if (st->flags & ST_EIO_PENDING) {
    962 				bp->b_resid = bp->b_bcount;
    963 				bp->b_error = EIO;
    964 				bp->b_flags |= B_ERROR;
    965 				st->flags &= ~ST_EIO_PENDING;
    966 				biodone(bp);
    967 				continue;	/* seek more work */
    968 			}
    969 		}
    970 
    971 		/*
    972 		 *  Fill out the scsi command
    973 		 */
    974 		bzero(&cmd, sizeof(cmd));
    975 		if ((bp->b_flags & B_READ) == B_WRITE) {
    976 			cmd.opcode = WRITE;
    977 			st->flags &= ~ST_FM_WRITTEN;
    978 			st->flags |= ST_WRITTEN;
    979 			flags = SCSI_DATA_OUT;
    980 		} else {
    981 			cmd.opcode = READ;
    982 			flags = SCSI_DATA_IN;
    983 		}
    984 
    985 		/*
    986 		 * Handle "fixed-block-mode" tape drives by using the
    987 		 * block count instead of the length.
    988 		 */
    989 		if (st->flags & ST_FIXEDBLOCKS) {
    990 			cmd.byte2 |= SRW_FIXED;
    991 			lto3b(bp->b_bcount / st->blksize, cmd.len);
    992 		} else
    993 			lto3b(bp->b_bcount, cmd.len);
    994 
    995 		/*
    996 		 * go ask the adapter to do all this for us
    997 		 */
    998 		if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    999 		    sizeof(cmd), (u_char *) bp->b_data, bp->b_bcount, 0,
   1000 		    100000, bp, flags | SCSI_NOSLEEP))
   1001 			printf("%s: not queued\n", st->sc_dev.dv_xname);
   1002 	} /* go back and see if we can cram more work in.. */
   1003 }
   1004 
   1005 int
   1006 stread(dev, uio)
   1007 	dev_t dev;
   1008 	struct uio *uio;
   1009 {
   1010 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
   1011 
   1012 	return (physio(ststrategy, NULL, dev, B_READ,
   1013 		       st->sc_link->adapter->scsi_minphys, uio));
   1014 }
   1015 
   1016 int
   1017 stwrite(dev, uio)
   1018 	dev_t dev;
   1019 	struct uio *uio;
   1020 {
   1021 	struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
   1022 
   1023 	return (physio(ststrategy, NULL, dev, B_WRITE,
   1024 		       st->sc_link->adapter->scsi_minphys, uio));
   1025 }
   1026 
   1027 /*
   1028  * Perform special action on behalf of the user;
   1029  * knows about the internals of this device
   1030  */
   1031 int
   1032 stioctl(dev, cmd, arg, flag, p)
   1033 	dev_t dev;
   1034 	u_long cmd;
   1035 	caddr_t arg;
   1036 	int flag;
   1037 	struct proc *p;
   1038 {
   1039 	int error = 0;
   1040 	int unit;
   1041 	int number, nmarks, dsty;
   1042 	int flags;
   1043 	struct st_softc *st;
   1044 	int hold_blksize;
   1045 	u_int8_t hold_density;
   1046 	struct mtop *mt = (struct mtop *) arg;
   1047 
   1048 	/*
   1049 	 * Find the device that the user is talking about
   1050 	 */
   1051 	flags = 0;		/* give error messages, act on errors etc. */
   1052 	unit = STUNIT(dev);
   1053 	dsty = STDSTY(dev);
   1054 	st = stcd.cd_devs[unit];
   1055 	hold_blksize = st->blksize;
   1056 	hold_density = st->density;
   1057 
   1058 	switch (cmd) {
   1059 
   1060 	case MTIOCGET: {
   1061 		struct mtget *g = (struct mtget *) arg;
   1062 
   1063 		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
   1064 		bzero(g, sizeof(struct mtget));
   1065 		g->mt_type = 0x7;	/* Ultrix compat *//*? */
   1066 		g->mt_blksiz = st->blksize;
   1067 		g->mt_density = st->density;
   1068 		g->mt_mblksiz[0] = st->modes[0].blksize;
   1069 		g->mt_mblksiz[1] = st->modes[1].blksize;
   1070 		g->mt_mblksiz[2] = st->modes[2].blksize;
   1071 		g->mt_mblksiz[3] = st->modes[3].blksize;
   1072 		g->mt_mdensity[0] = st->modes[0].density;
   1073 		g->mt_mdensity[1] = st->modes[1].density;
   1074 		g->mt_mdensity[2] = st->modes[2].density;
   1075 		g->mt_mdensity[3] = st->modes[3].density;
   1076 		break;
   1077 	}
   1078 	case MTIOCTOP: {
   1079 
   1080 		SC_DEBUG(st->sc_link, SDEV_DB1,
   1081 		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
   1082 
   1083 		/* compat: in U*x it is a short */
   1084 		number = mt->mt_count;
   1085 		switch ((short) (mt->mt_op)) {
   1086 		case MTWEOF:	/* write an end-of-file record */
   1087 			error = st_write_filemarks(st, number, flags);
   1088 			break;
   1089 		case MTBSF:	/* backward space file */
   1090 			number = -number;
   1091 		case MTFSF:	/* forward space file */
   1092 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1093 			if (!error)
   1094 				error = st_space(st, number - nmarks,
   1095 				    SP_FILEMARKS, flags);
   1096 			break;
   1097 		case MTBSR:	/* backward space record */
   1098 			number = -number;
   1099 		case MTFSR:	/* forward space record */
   1100 			error = st_check_eod(st, TRUE, &nmarks, flags);
   1101 			if (!error)
   1102 				error = st_space(st, number, SP_BLKS, flags);
   1103 			break;
   1104 		case MTREW:	/* rewind */
   1105 			error = st_rewind(st, 0, flags);
   1106 			break;
   1107 		case MTOFFL:	/* rewind and put the drive offline */
   1108 			st_unmount(st, EJECT);
   1109 			break;
   1110 		case MTNOP:	/* no operation, sets status only */
   1111 			break;
   1112 		case MTRETEN:	/* retension the tape */
   1113 			error = st_load(st, LD_RETENSION, flags);
   1114 			if (!error)
   1115 				error = st_load(st, LD_LOAD, flags);
   1116 			break;
   1117 		case MTEOM:	/* forward space to end of media */
   1118 			error = st_check_eod(st, FALSE, &nmarks, flags);
   1119 			if (!error)
   1120 				error = st_space(st, 1, SP_EOM, flags);
   1121 			break;
   1122 		case MTCACHE:	/* enable controller cache */
   1123 		case MTNOCACHE:	/* disable controller cache */
   1124 			break;
   1125 		case MTSETBSIZ:	/* Set block size for device */
   1126 #ifdef	NOTYET
   1127 			if (!(st->flags & ST_NEW_MOUNT)) {
   1128 				uprintf("re-mount tape before changing blocksize");
   1129 				error = EINVAL;
   1130 				break;
   1131 			}
   1132 #endif
   1133 			if (number == 0) {
   1134 				st->flags &= ~ST_FIXEDBLOCKS;
   1135 			} else {
   1136 				if ((st->blkmin || st->blkmax) &&
   1137 				    (number < st->blkmin ||
   1138 				    number > st->blkmax)) {
   1139 					error = EINVAL;
   1140 					break;
   1141 				}
   1142 				st->flags |= ST_FIXEDBLOCKS;
   1143 			}
   1144 			st->blksize = number;
   1145 			st->flags |= ST_BLOCK_SET;	/*XXX */
   1146 			goto try_new_value;
   1147 
   1148 		case MTSETDNSTY:	/* Set density for device and mode */
   1149 			if (number > SCSI_2_MAX_DENSITY_CODE)
   1150 				error = EINVAL;
   1151 			else
   1152 				st->density = number;
   1153 			goto try_new_value;
   1154 
   1155 		default:
   1156 			error = EINVAL;
   1157 		}
   1158 		break;
   1159 	}
   1160 	case MTIOCIEOT:
   1161 	case MTIOCEEOT:
   1162 		break;
   1163 	default:
   1164 		if (STMODE(dev) == CTLMODE)
   1165 			error = scsi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p);
   1166 		else
   1167 			error = ENOTTY;
   1168 		break;
   1169 	}
   1170 	return error;
   1171 /*-----------------------------*/
   1172 try_new_value:
   1173 	/*
   1174 	 * Check that the mode being asked for is aggreeable to the
   1175 	 * drive. If not, put it back the way it was.
   1176 	 */
   1177 	if (error = st_mode_select(st, 0)) {	/* put it back as it was */
   1178 		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
   1179 		st->density = hold_density;
   1180 		st->blksize = hold_blksize;
   1181 		if (st->blksize)
   1182 			st->flags |= ST_FIXEDBLOCKS;
   1183 		else
   1184 			st->flags &= ~ST_FIXEDBLOCKS;
   1185 		return error;
   1186 	}
   1187 	/*
   1188 	 * As the drive liked it, if we are setting a new default,
   1189 	 * set it into the structures as such.
   1190 	 *
   1191 	 * The means for deciding this are not finalised yet
   1192 	 */
   1193 	if (STMODE(dev) == 0x03) {
   1194 		/* special mode */
   1195 		/* XXX */
   1196 		switch ((short) (mt->mt_op)) {
   1197 		case MTSETBSIZ:
   1198 			st->modes[dsty].blksize = st->blksize;
   1199 			st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
   1200 			break;
   1201 		case MTSETDNSTY:
   1202 			st->modes[dsty].density = st->density;
   1203 			st->modeflags[dsty] |= DENSITY_SET_BY_USER;
   1204 			break;
   1205 		}
   1206 	}
   1207 	return 0;
   1208 }
   1209 
   1210 /*
   1211  * Do a synchronous read.
   1212  */
   1213 int
   1214 st_read(st, buf, size, flags)
   1215 	struct st_softc *st;
   1216 	int size;
   1217 	int flags;
   1218 	char *buf;
   1219 {
   1220 	struct scsi_rw_tape cmd;
   1221 
   1222 	/*
   1223 	 * If it's a null transfer, return immediatly
   1224 	 */
   1225 	if (size == 0)
   1226 		return 0;
   1227 	bzero(&cmd, sizeof(cmd));
   1228 	cmd.opcode = READ;
   1229 	if (st->flags & ST_FIXEDBLOCKS) {
   1230 		cmd.byte2 |= SRW_FIXED;
   1231 		lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
   1232 		    cmd.len);
   1233 	} else
   1234 		lto3b(size, cmd.len);
   1235 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1236 	    sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
   1237 	    flags | SCSI_DATA_IN);
   1238 }
   1239 
   1240 #ifdef	__STDC__
   1241 #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0)
   1242 #else
   1243 #define b2tol(a)	(((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
   1244 #endif
   1245 
   1246 /*
   1247  * Ask the drive what it's min and max blk sizes are.
   1248  */
   1249 int
   1250 st_read_block_limits(st, flags)
   1251 	struct st_softc *st;
   1252 	int flags;
   1253 {
   1254 	struct scsi_block_limits cmd;
   1255 	struct scsi_block_limits_data block_limits;
   1256 	struct scsi_link *sc_link = st->sc_link;
   1257 	int error;
   1258 
   1259 	/*
   1260 	 * First check if we have it all loaded
   1261 	 */
   1262 	if ((sc_link->flags & SDEV_MEDIA_LOADED))
   1263 		return 0;
   1264 
   1265 	/*
   1266 	 * do a 'Read Block Limits'
   1267 	 */
   1268 	bzero(&cmd, sizeof(cmd));
   1269 	cmd.opcode = READ_BLOCK_LIMITS;
   1270 
   1271 	/*
   1272 	 * do the command, update the global values
   1273 	 */
   1274 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1275 	    sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits),
   1276 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1277 		return error;
   1278 
   1279 	st->blkmin = b2tol(block_limits.min_length);
   1280 	st->blkmax = _3btol(&block_limits.max_length_2);
   1281 
   1282 	SC_DEBUG(sc_link, SDEV_DB3,
   1283 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
   1284 	return 0;
   1285 }
   1286 
   1287 /*
   1288  * Get the scsi driver to send a full inquiry to the
   1289  * device and use the results to fill out the global
   1290  * parameter structure.
   1291  *
   1292  * called from:
   1293  * attach
   1294  * open
   1295  * ioctl (to reset original blksize)
   1296  */
   1297 int
   1298 st_mode_sense(st, flags)
   1299 	struct st_softc *st;
   1300 	int flags;
   1301 {
   1302 	u_int scsi_sense_len;
   1303 	int error;
   1304 	struct scsi_mode_sense cmd;
   1305 	struct scsi_sense {
   1306 		struct scsi_mode_header header;
   1307 		struct scsi_blk_desc blk_desc;
   1308 		u_char sense_data[MAX_PAGE_0_SIZE];
   1309 	} scsi_sense;
   1310 	struct scsi_link *sc_link = st->sc_link;
   1311 
   1312 	scsi_sense_len = 12 + st->page_0_size;
   1313 
   1314 	/*
   1315 	 * Set up a mode sense
   1316 	 */
   1317 	bzero(&cmd, sizeof(cmd));
   1318 	cmd.opcode = MODE_SENSE;
   1319 	cmd.length = scsi_sense_len;
   1320 
   1321 	/*
   1322 	 * do the command, but we don't need the results
   1323 	 * just print them for our interest's sake, if asked,
   1324 	 * or if we need it as a template for the mode select
   1325 	 * store it away.
   1326 	 */
   1327 	if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1328 	    sizeof(cmd), (u_char *) &scsi_sense, scsi_sense_len,
   1329 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
   1330 		return error;
   1331 
   1332 	st->numblks = _3btol(scsi_sense.blk_desc.nblocks);
   1333 	st->media_blksize = _3btol(scsi_sense.blk_desc.blklen);
   1334 	st->media_density = scsi_sense.blk_desc.density;
   1335 	if (scsi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
   1336 		st->flags |= ST_READONLY;
   1337 	SC_DEBUG(sc_link, SDEV_DB3,
   1338 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
   1339 	    st->media_density, st->media_blksize,
   1340 	    st->flags & ST_READONLY ? "protected" : "enabled"));
   1341 	SC_DEBUG(sc_link, SDEV_DB3,
   1342 	    ("%sbuffered\n",
   1343 	    scsi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
   1344 	if (st->page_0_size)
   1345 		bcopy(scsi_sense.sense_data, st->sense_data, st->page_0_size);
   1346 	sc_link->flags |= SDEV_MEDIA_LOADED;
   1347 	return 0;
   1348 }
   1349 
   1350 /*
   1351  * Send a filled out parameter structure to the drive to
   1352  * set it into the desire modes etc.
   1353  */
   1354 int
   1355 st_mode_select(st, flags)
   1356 	struct st_softc *st;
   1357 	int flags;
   1358 {
   1359 	u_int scsi_select_len;
   1360 	int error;
   1361 	struct scsi_mode_select cmd;
   1362 	struct scsi_select {
   1363 		struct scsi_mode_header header;
   1364 		struct scsi_blk_desc blk_desc;
   1365 		u_char sense_data[MAX_PAGE_0_SIZE];
   1366 	} scsi_select;
   1367 	struct scsi_link *sc_link = st->sc_link;
   1368 
   1369 	scsi_select_len = 12 + st->page_0_size;
   1370 
   1371 	/*
   1372 	 * Set up for a mode select
   1373 	 */
   1374 	bzero(&cmd, sizeof(cmd));
   1375 	cmd.opcode = MODE_SELECT;
   1376 	cmd.length = scsi_select_len;
   1377 
   1378 	bzero(&scsi_select, scsi_select_len);
   1379 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
   1380 	scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
   1381 	scsi_select.blk_desc.density = st->density;
   1382 	if (st->flags & ST_FIXEDBLOCKS)
   1383 		lto3b(st->blksize, scsi_select.blk_desc.blklen);
   1384 	if (st->page_0_size)
   1385 		bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
   1386 
   1387 	/*
   1388 	 * do the command
   1389 	 */
   1390 	return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
   1391 	    sizeof(cmd), (u_char *) &scsi_select, scsi_select_len,
   1392 	    ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
   1393 }
   1394 
   1395 /*
   1396  * skip N blocks/filemarks/seq filemarks/eom
   1397  */
   1398 int
   1399 st_space(st, number, what, flags)
   1400 	struct st_softc *st;
   1401 	u_int what;
   1402 	int flags;
   1403 	int number;
   1404 {
   1405 	struct scsi_space cmd;
   1406 	int error;
   1407 
   1408 	switch (what) {
   1409 	case SP_BLKS:
   1410 		if (st->flags & ST_PER_ACTION) {
   1411 			if (number > 0) {
   1412 				st->flags &= ~ST_PER_ACTION;
   1413 				return EIO;
   1414 			} else if (number < 0) {
   1415 				if (st->flags & ST_AT_FILEMARK) {
   1416 					/*
   1417 					 * Handling of ST_AT_FILEMARK
   1418 					 * in st_space will fill in the
   1419 					 * right file mark count.
   1420 					 */
   1421 					error = st_space(st, 0, SP_FILEMARKS,
   1422 						flags);
   1423 					if (error)
   1424 						return error;
   1425 				}
   1426 				if (st->flags & ST_BLANK_READ) {
   1427 					st->flags &= ~ST_BLANK_READ;
   1428 					return EIO;
   1429 				}
   1430 				st->flags &= ~ST_EIO_PENDING;
   1431 			}
   1432 		}
   1433 		break;
   1434 	case SP_FILEMARKS:
   1435 		if (st->flags & ST_EIO_PENDING) {
   1436 			if (number > 0) {
   1437 				/* pretend we just discovered the error */
   1438 				st->flags &= ~ST_EIO_PENDING;
   1439 				return EIO;
   1440 			} else if (number < 0) {
   1441 				/* back away from the error */
   1442 				st->flags &= ~ST_EIO_PENDING;
   1443 			}
   1444 		}
   1445 		if (st->flags & ST_AT_FILEMARK) {
   1446 			st->flags &= ~ST_AT_FILEMARK;
   1447 			number--;
   1448 		}
   1449 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
   1450 			/* back away from unwritten tape */
   1451 			st->flags &= ~ST_BLANK_READ;
   1452 			number++;	/* XXX dubious */
   1453 		}
   1454 		break;
   1455 	case SP_EOM:
   1456 		if (st->flags & ST_EIO_PENDING) {
   1457 			/* pretend we just discovered the error */
   1458 			st->flags &= ~ST_EIO_PENDING;
   1459 			return EIO;
   1460 		}
   1461 		if (st->flags & ST_AT_FILEMARK)
   1462 			st->flags &= ~ST_AT_FILEMARK;
   1463 		break;
   1464 	}
   1465 	if (number == 0)
   1466 		return 0;
   1467 
   1468 	bzero(&cmd, sizeof(cmd));
   1469 	cmd.opcode = SPACE;
   1470 	cmd.byte2 = what;
   1471 	lto3b(number, cmd.number);
   1472 
   1473 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1474 	    sizeof(cmd), 0, 0, 0, 900000, NULL, flags);
   1475 }
   1476 
   1477 /*
   1478  * write N filemarks
   1479  */
   1480 int
   1481 st_write_filemarks(st, number, flags)
   1482 	struct st_softc *st;
   1483 	int flags;
   1484 	int number;
   1485 {
   1486 	struct scsi_write_filemarks cmd;
   1487 
   1488 	/*
   1489 	 * It's hard to write a negative number of file marks.
   1490 	 * Don't try.
   1491 	 */
   1492 	if (number < 0)
   1493 		return EINVAL;
   1494 	switch (number) {
   1495 	case 0:		/* really a command to sync the drive's buffers */
   1496 		break;
   1497 	case 1:
   1498 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
   1499 			st->flags &= ~ST_WRITTEN;
   1500 		else
   1501 			st->flags |= ST_FM_WRITTEN;
   1502 		st->flags &= ~ST_PER_ACTION;
   1503 		break;
   1504 	default:
   1505 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
   1506 	}
   1507 
   1508 	bzero(&cmd, sizeof(cmd));
   1509 	cmd.opcode = WRITE_FILEMARKS;
   1510 	lto3b(number, cmd.number);
   1511 
   1512 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1513 	    sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
   1514 }
   1515 
   1516 /*
   1517  * Make sure the right number of file marks is on tape if the
   1518  * tape has been written.  If the position argument is true,
   1519  * leave the tape positioned where it was originally.
   1520  *
   1521  * nmarks returns the number of marks to skip (or, if position
   1522  * true, which were skipped) to get back original position.
   1523  */
   1524 int
   1525 st_check_eod(st, position, nmarks, flags)
   1526 	struct st_softc *st;
   1527 	boolean position;
   1528 	int *nmarks;
   1529 	int flags;
   1530 {
   1531 	int error;
   1532 
   1533 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
   1534 	default:
   1535 		*nmarks = 0;
   1536 		return 0;
   1537 	case ST_WRITTEN:
   1538 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
   1539 		*nmarks = 1;
   1540 		break;
   1541 	case ST_WRITTEN | ST_2FM_AT_EOD:
   1542 		*nmarks = 2;
   1543 	}
   1544 	error = st_write_filemarks(st, *nmarks, flags);
   1545 	if (position && !error)
   1546 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
   1547 	return error;
   1548 }
   1549 
   1550 /*
   1551  * load/unload/retension
   1552  */
   1553 int
   1554 st_load(st, type, flags)
   1555 	struct st_softc *st;
   1556 	u_int type;
   1557 	int flags;
   1558 {
   1559 	struct scsi_load cmd;
   1560 
   1561 	if (type != LD_LOAD) {
   1562 		int error;
   1563 		int nmarks;
   1564 
   1565 		error = st_check_eod(st, FALSE, &nmarks, flags);
   1566 		if (error)
   1567 			return error;
   1568 	}
   1569 	if (st->quirks & ST_Q_IGNORE_LOADS)
   1570 		return 0;
   1571 
   1572 	bzero(&cmd, sizeof(cmd));
   1573 	cmd.opcode = LOAD;
   1574 	cmd.how = type;
   1575 
   1576 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1577 	    sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
   1578 }
   1579 
   1580 /*
   1581  *  Rewind the device
   1582  */
   1583 int
   1584 st_rewind(st, immediate, flags)
   1585 	struct st_softc *st;
   1586 	u_int immediate;
   1587 	int flags;
   1588 {
   1589 	struct scsi_rewind cmd;
   1590 	int error;
   1591 	int nmarks;
   1592 
   1593 	error = st_check_eod(st, FALSE, &nmarks, flags);
   1594 	if (error)
   1595 		return error;
   1596 	st->flags &= ~ST_PER_ACTION;
   1597 
   1598 	bzero(&cmd, sizeof(cmd));
   1599 	cmd.opcode = REWIND;
   1600 	cmd.byte2 = immediate;
   1601 
   1602 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
   1603 	    sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
   1604 	    flags);
   1605 }
   1606 
   1607 /*
   1608  * Look at the returned sense and act on the error and detirmine
   1609  * The unix error number to pass back... (0 = report no error)
   1610  *                            (-1 = continue processing)
   1611  */
   1612 int
   1613 st_interpret_sense(xs)
   1614 	struct scsi_xfer *xs;
   1615 {
   1616 	struct scsi_link *sc_link = xs->sc_link;
   1617 	struct scsi_sense_data *sense = &xs->sense;
   1618 	struct buf *bp = xs->bp;
   1619 	struct st_softc *st = sc_link->device_softc;
   1620 	u_int8_t key;
   1621 	u_int32_t info;
   1622 
   1623 	/*
   1624 	 * Get the sense fields and work out what code
   1625 	 */
   1626 	if (sense->error_code & SSD_ERRCODE_VALID) {
   1627 		bcopy(sense->extended_info, &info, sizeof info);
   1628 		info = ntohl(info);
   1629 	} else
   1630 		info = xs->datalen;	/* bad choice if fixed blocks */
   1631 	if ((sense->error_code & SSD_ERRCODE) != 0x70)
   1632 		return -1;	/* let the generic code handle it */
   1633 	if (st->flags & ST_FIXEDBLOCKS) {
   1634 		xs->resid = info * st->blksize;
   1635 		if (sense->extended_flags & SSD_EOM) {
   1636 			st->flags |= ST_EIO_PENDING;
   1637 			if (bp)
   1638 				bp->b_resid = xs->resid;
   1639 		}
   1640 		if (sense->extended_flags & SSD_FILEMARK) {
   1641 			st->flags |= ST_AT_FILEMARK;
   1642 			if (bp)
   1643 				bp->b_resid = xs->resid;
   1644 		}
   1645 		if (sense->extended_flags & SSD_ILI) {
   1646 			st->flags |= ST_EIO_PENDING;
   1647 			if (bp)
   1648 				bp->b_resid = xs->resid;
   1649 			if (sense->error_code & SSD_ERRCODE_VALID &&
   1650 			    (xs->flags & SCSI_SILENT) == 0)
   1651 				printf("%s: block wrong size, %d blocks residual\n",
   1652 				    st->sc_dev.dv_xname, info);
   1653 
   1654 			/*
   1655 			 * This quirk code helps the drive read
   1656 			 * the first tape block, regardless of
   1657 			 * format.  That is required for these
   1658 			 * drives to return proper MODE SENSE
   1659 			 * information.
   1660 			 */
   1661 			if ((st->quirks & ST_Q_SENSE_HELP) &&
   1662 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
   1663 				st->blksize -= 512;
   1664 		}
   1665 		/*
   1666 		 * If no data was tranfered, do it immediatly
   1667 		 */
   1668 		if (xs->resid >= xs->datalen) {
   1669 			if (st->flags & ST_EIO_PENDING)
   1670 				return EIO;
   1671 			if (st->flags & ST_AT_FILEMARK) {
   1672 				if (bp)
   1673 					bp->b_resid = xs->resid;
   1674 				return 0;
   1675 			}
   1676 		}
   1677 	} else {		/* must be variable mode */
   1678 		xs->resid = xs->datalen;	/* to be sure */
   1679 		if (sense->extended_flags & SSD_EOM)
   1680 			return EIO;
   1681 		if (sense->extended_flags & SSD_FILEMARK) {
   1682 			if (bp)
   1683 				bp->b_resid = bp->b_bcount;
   1684 			return 0;
   1685 		}
   1686 		if (sense->extended_flags & SSD_ILI) {
   1687 			if (info < 0) {
   1688 				/*
   1689 				 * the record was bigger than the read
   1690 				 */
   1691 				if ((xs->flags & SCSI_SILENT) == 0)
   1692 					printf("%s: %d-byte record too big\n",
   1693 					    st->sc_dev.dv_xname,
   1694 					    xs->datalen - info);
   1695 				return EIO;
   1696 			}
   1697 			xs->resid = info;
   1698 			if (bp)
   1699 				bp->b_resid = info;
   1700 		}
   1701 	}
   1702 	key = sense->extended_flags & SSD_KEY;
   1703 
   1704 	if (key == 0x8) {
   1705 		/*
   1706 		 * This quirk code helps the drive read the
   1707 		 * first tape block, regardless of format.  That
   1708 		 * is required for these drives to return proper
   1709 		 * MODE SENSE information.
   1710 		 */
   1711 		if ((st->quirks & ST_Q_SENSE_HELP) &&
   1712 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
   1713 			/* still starting */
   1714 			st->blksize -= 512;
   1715 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
   1716 			st->flags |= ST_BLANK_READ;
   1717 			xs->resid = xs->datalen;
   1718 			if (bp) {
   1719 				bp->b_resid = xs->resid;
   1720 				/* return an EOF */
   1721 			}
   1722 			return 0;
   1723 		}
   1724 	}
   1725 	return -1;		/* let the default/generic handler handle it */
   1726 }
   1727 
   1728 /*
   1729  * The quirk here is that the drive returns some value to st_mode_sense
   1730  * incorrectly until the tape has actually passed by the head.
   1731  *
   1732  * The method is to set the drive to large fixed-block state (user-specified
   1733  * density and 1024-byte blocks), then read and rewind to get it to sense the
   1734  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
   1735  * work, as a last resort, try variable- length blocks.  The result will be
   1736  * the ability to do an accurate st_mode_sense.
   1737  *
   1738  * We know we can do a rewind because we just did a load, which implies rewind.
   1739  * Rewind seems preferable to space backward if we have a virgin tape.
   1740  *
   1741  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
   1742  * error processing, both part of st_interpret_sense.
   1743  */
   1744 int
   1745 st_touch_tape(st)
   1746 	struct st_softc *st;
   1747 {
   1748 	char *buf;
   1749 	int readsize;
   1750 	int error;
   1751 
   1752 	buf = malloc(1024, M_TEMP, M_NOWAIT);
   1753 	if (!buf)
   1754 		return ENOMEM;
   1755 
   1756 	if (error = st_mode_sense(st, 0))
   1757 		goto bad;
   1758 	st->blksize = 1024;
   1759 	do {
   1760 		switch (st->blksize) {
   1761 		case 512:
   1762 		case 1024:
   1763 			readsize = st->blksize;
   1764 			st->flags |= ST_FIXEDBLOCKS;
   1765 			break;
   1766 		default:
   1767 			readsize = 1;
   1768 			st->flags &= ~ST_FIXEDBLOCKS;
   1769 		}
   1770 		if (error = st_mode_select(st, 0))
   1771 			goto bad;
   1772 		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
   1773 		if (error = st_rewind(st, 0, 0)) {
   1774 bad:			free(buf, M_TEMP);
   1775 			return error;
   1776 		}
   1777 	} while (readsize != 1 && readsize > st->blksize);
   1778 
   1779 	free(buf, M_TEMP);
   1780 	return 0;
   1781 }
   1782 
   1783 int
   1784 stdump(dev, blkno, va, size)
   1785 	dev_t dev;
   1786 	daddr_t blkno;
   1787 	caddr_t va;
   1788 	size_t size;
   1789 {
   1790 
   1791 	/* Not implemented. */
   1792 	return ENXIO;
   1793 }
   1794