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