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