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