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