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