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