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