xref: /freebsd/sys/cam/scsi/scsi_sa.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * $FreeBSD$
3  *
4  * Implementation of SCSI Sequential Access Peripheral driver for CAM.
5  *
6  * Copyright (c) 1999, 2000 Matthew Jacob
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification, immediately at the beginning of the file.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #endif
38 #include <sys/types.h>
39 #include <sys/bio.h>
40 #include <sys/malloc.h>
41 #include <sys/mtio.h>
42 #ifdef _KERNEL
43 #include <sys/conf.h>
44 #endif
45 #include <sys/devicestat.h>
46 #include <machine/limits.h>
47 
48 #ifndef _KERNEL
49 #include <stdio.h>
50 #include <string.h>
51 #endif
52 
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_extend.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
59 
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_sa.h>
63 
64 #ifdef _KERNEL
65 
66 #include <opt_sa.h>
67 
68 #ifndef SA_SPACE_TIMEOUT
69 #define SA_SPACE_TIMEOUT	1 * 60
70 #endif
71 #ifndef SA_REWIND_TIMEOUT
72 #define SA_REWIND_TIMEOUT	2 * 60
73 #endif
74 #ifndef SA_ERASE_TIMEOUT
75 #define SA_ERASE_TIMEOUT	4 * 60
76 #endif
77 
78 #define	REWIND_TIMEOUT		(SA_REWIND_TIMEOUT * 60 * 1000)
79 #define	ERASE_TIMEOUT		(SA_ERASE_TIMEOUT * 60 * 1000)
80 #define	SPACE_TIMEOUT		(SA_SPACE_TIMEOUT * 60 * 1000)
81 
82 /*
83  * Additional options that can be set for config: SA_1FM_AT_EOT
84  */
85 
86 #ifndef	UNUSED_PARAMETER
87 #define	UNUSED_PARAMETER(x)	x = x
88 #endif
89 
90 #define	QFRLS(ccb)	\
91 	if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0)	\
92 		cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
93 
94 /*
95  * Driver states
96  */
97 
98 
99 typedef enum {
100 	SA_STATE_NORMAL, SA_STATE_ABNORMAL
101 } sa_state;
102 
103 #define ccb_pflags	ppriv_field0
104 #define ccb_bp	 	ppriv_ptr1
105 
106 #define	SA_CCB_BUFFER_IO	0x0
107 #define	SA_CCB_WAITING		0x1
108 #define	SA_CCB_TYPEMASK		0x1
109 #define	SA_POSITION_UPDATED	0x2
110 
111 #define	Set_CCB_Type(x, type)				\
112 	x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK;	\
113 	x->ccb_h.ccb_pflags |= type
114 
115 #define	CCB_Type(x)	(x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
116 
117 
118 
119 typedef enum {
120 	SA_FLAG_OPEN		= 0x0001,
121 	SA_FLAG_FIXED		= 0x0002,
122 	SA_FLAG_TAPE_LOCKED	= 0x0004,
123 	SA_FLAG_TAPE_MOUNTED	= 0x0008,
124 	SA_FLAG_TAPE_WP		= 0x0010,
125 	SA_FLAG_TAPE_WRITTEN	= 0x0020,
126 	SA_FLAG_EOM_PENDING	= 0x0040,
127 	SA_FLAG_EIO_PENDING	= 0x0080,
128 	SA_FLAG_EOF_PENDING	= 0x0100,
129 	SA_FLAG_ERR_PENDING	= (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
130 				   SA_FLAG_EOF_PENDING),
131 	SA_FLAG_INVALID		= 0x0200,
132 	SA_FLAG_COMP_ENABLED	= 0x0400,
133 	SA_FLAG_COMP_SUPP	= 0x0800,
134 	SA_FLAG_COMP_UNSUPP	= 0x1000,
135 	SA_FLAG_TAPE_FROZEN	= 0x2000
136 } sa_flags;
137 
138 typedef enum {
139 	SA_MODE_REWIND		= 0x00,
140 	SA_MODE_NOREWIND	= 0x01,
141 	SA_MODE_OFFLINE		= 0x02
142 } sa_mode;
143 
144 typedef enum {
145 	SA_PARAM_NONE		= 0x00,
146 	SA_PARAM_BLOCKSIZE	= 0x01,
147 	SA_PARAM_DENSITY	= 0x02,
148 	SA_PARAM_COMPRESSION	= 0x04,
149 	SA_PARAM_BUFF_MODE	= 0x08,
150 	SA_PARAM_NUMBLOCKS	= 0x10,
151 	SA_PARAM_WP		= 0x20,
152 	SA_PARAM_SPEED		= 0x40,
153 	SA_PARAM_ALL		= 0x7f
154 } sa_params;
155 
156 typedef enum {
157 	SA_QUIRK_NONE		= 0x00,
158 	SA_QUIRK_NOCOMP		= 0x01,	/* Can't deal with compression at all */
159 	SA_QUIRK_FIXED		= 0x02,	/* Force fixed mode */
160 	SA_QUIRK_VARIABLE	= 0x04,	/* Force variable mode */
161 	SA_QUIRK_2FM		= 0x08,	/* Needs Two File Marks at EOD */
162 	SA_QUIRK_1FM		= 0x10,	/* No more than 1 File Mark at EOD */
163 	SA_QUIRK_NODREAD	= 0x20,	/* Don't try and dummy read density */
164 	SA_QUIRK_NO_MODESEL	= 0x40,	/* Don't do mode select at all */
165 	SA_QUIRK_NO_CPAGE	= 0x80	/* Don't use DEVICE COMPRESSION page */
166 } sa_quirks;
167 
168 /* units are bits 4-7, 16-21 (1024 units) */
169 #define SAUNIT(DEV) \
170 	(((minor(DEV) & 0xF0) >> 4) |  ((minor(DEV) & 0x3f0000) >> 16))
171 
172 #define SAMODE(z) ((minor(z) & 0x3))
173 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
174 #define	SA_IS_CTRL(z) (minor(z) & (1 << 29))
175 
176 #define SA_NOT_CTLDEV	0
177 #define SA_CTLDEV	1
178 
179 #define SA_ATYPE_R	0
180 #define SA_ATYPE_NR	1
181 #define SA_ATYPE_ER	2
182 
183 #define SAMINOR(ctl, unit, mode, access) \
184 	((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
185 	(mode << 0x2) | (access & 0x3))
186 
187 #define SA_NUM_MODES	4
188 struct sa_devs {
189 	dev_t	ctl_dev;
190 	struct sa_mode_devs {
191 		dev_t	r_dev;
192 		dev_t	nr_dev;
193 		dev_t	er_dev;
194 	} mode_devs[SA_NUM_MODES];
195 };
196 
197 struct sa_softc {
198 	sa_state	state;
199 	sa_flags	flags;
200 	sa_quirks	quirks;
201 	struct		bio_queue_head bio_queue;
202 	int		queue_count;
203 	struct		devstat device_stats;
204 	struct sa_devs	devs;
205 	int		blk_gran;
206 	int		blk_mask;
207 	int		blk_shift;
208 	u_int32_t	max_blk;
209 	u_int32_t	min_blk;
210 	u_int32_t	comp_algorithm;
211 	u_int32_t	saved_comp_algorithm;
212 	u_int32_t	media_blksize;
213 	u_int32_t	last_media_blksize;
214 	u_int32_t	media_numblks;
215 	u_int8_t	media_density;
216 	u_int8_t	speed;
217 	u_int8_t	scsi_rev;
218 	u_int8_t	dsreg;		/* mtio mt_dsreg, redux */
219 	int		buffer_mode;
220 	int		filemarks;
221 	union		ccb saved_ccb;
222 	int		last_resid_was_io;
223 
224 	/*
225 	 * Relative to BOT Location.
226 	 */
227 	daddr_t		fileno;
228 	daddr_t		blkno;
229 
230 	/*
231 	 * Latched Error Info
232 	 */
233 	struct {
234 		struct scsi_sense_data _last_io_sense;
235 		u_int32_t _last_io_resid;
236 		u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
237 		struct scsi_sense_data _last_ctl_sense;
238 		u_int32_t _last_ctl_resid;
239 		u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
240 #define	last_io_sense	errinfo._last_io_sense
241 #define	last_io_resid	errinfo._last_io_resid
242 #define	last_io_cdb	errinfo._last_io_cdb
243 #define	last_ctl_sense	errinfo._last_ctl_sense
244 #define	last_ctl_resid	errinfo._last_ctl_resid
245 #define	last_ctl_cdb	errinfo._last_ctl_cdb
246 	} errinfo;
247 	/*
248 	 * Misc other flags/state
249 	 */
250 	u_int32_t
251 				: 31,
252 		ctrl_mode	: 1;	/* control device open */
253 };
254 
255 struct sa_quirk_entry {
256 	struct scsi_inquiry_pattern inq_pat;	/* matching pattern */
257 	sa_quirks quirks;	/* specific quirk type */
258 	u_int32_t prefblk;	/* preferred blocksize when in fixed mode */
259 };
260 
261 static struct sa_quirk_entry sa_quirk_table[] =
262 {
263 	{
264 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
265 		  "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
266 		   SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
267 	},
268 	{
269 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
270 		  "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
271 	},
272 	{
273 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
274 		  "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
275 	},
276 	{
277 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
278 		  "Python*", "*"}, SA_QUIRK_NODREAD, 0
279 	},
280 	{
281 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
282 		  "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
283 	},
284 	{
285 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
286 		  "VIPER 2525 25462", "-011"},
287 		  SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
288 	},
289 	{
290 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
291 		  "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
292 	},
293 #if	0
294 	{
295 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
296 		  "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
297 	},
298 #endif
299 	{
300 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
301 		  "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
302 	},
303 	{
304 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
305 		  "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
306 	},
307 	{
308 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
309 		  "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
310 	},
311 	{
312 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
313 		  "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
314 	},
315 	{
316 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
317 		  "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
318 	},
319 	{	/* jreynold@primenet.com */
320 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
321 		"STT8000N*", "*"}, SA_QUIRK_1FM, 0
322 	},
323 	{	/* mike@sentex.net */
324 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
325 		"STT20000*", "*"}, SA_QUIRK_1FM, 0
326 	},
327 	{
328 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
329 		  " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
330 	},
331 	{
332 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
333 		  " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
334 	},
335 	{
336 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
337 		  " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
338 	},
339 	{
340 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
341 		  " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
342 	},
343 	{
344 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
345 		  " SLR*", "*"}, SA_QUIRK_1FM, 0
346 	},
347 	{
348 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
349 		  "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
350 	},
351 	{
352 		{ T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
353 		  "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
354 	}
355 };
356 
357 static	d_open_t	saopen;
358 static	d_close_t	saclose;
359 static	d_strategy_t	sastrategy;
360 static	d_ioctl_t	saioctl;
361 static	periph_init_t	sainit;
362 static	periph_ctor_t	saregister;
363 static	periph_oninv_t	saoninvalidate;
364 static	periph_dtor_t	sacleanup;
365 static	periph_start_t	sastart;
366 static	void		saasync(void *callback_arg, u_int32_t code,
367 				struct cam_path *path, void *arg);
368 static	void		sadone(struct cam_periph *periph,
369 			       union ccb *start_ccb);
370 static  int		saerror(union ccb *ccb, u_int32_t cam_flags,
371 				u_int32_t sense_flags);
372 static int		samarkswanted(struct cam_periph *);
373 static int		sacheckeod(struct cam_periph *periph);
374 static int		sagetparams(struct cam_periph *periph,
375 				    sa_params params_to_get,
376 				    u_int32_t *blocksize, u_int8_t *density,
377 				    u_int32_t *numblocks, int *buff_mode,
378 				    u_int8_t *write_protect, u_int8_t *speed,
379 				    int *comp_supported, int *comp_enabled,
380 				    u_int32_t *comp_algorithm,
381 				    sa_comp_t *comp_page);
382 static int		sasetparams(struct cam_periph *periph,
383 				    sa_params params_to_set,
384 				    u_int32_t blocksize, u_int8_t density,
385 				    u_int32_t comp_algorithm,
386 				    u_int32_t sense_flags);
387 static void		saprevent(struct cam_periph *periph, int action);
388 static int		sarewind(struct cam_periph *periph);
389 static int		saspace(struct cam_periph *periph, int count,
390 				scsi_space_code code);
391 static int		samount(struct cam_periph *, int, dev_t);
392 static int		saretension(struct cam_periph *periph);
393 static int		sareservereleaseunit(struct cam_periph *periph,
394 					     int reserve);
395 static int		saloadunload(struct cam_periph *periph, int load);
396 static int		saerase(struct cam_periph *periph, int longerase);
397 static int		sawritefilemarks(struct cam_periph *periph,
398 					 int nmarks, int setmarks);
399 static int		sardpos(struct cam_periph *periph, int, u_int32_t *);
400 static int		sasetpos(struct cam_periph *periph, int, u_int32_t *);
401 
402 
403 static struct periph_driver sadriver =
404 {
405 	sainit, "sa",
406 	TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
407 };
408 
409 PERIPHDRIVER_DECLARE(sa, sadriver);
410 
411 /* For 2.2-stable support */
412 #ifndef D_TAPE
413 #define D_TAPE 0
414 #endif
415 
416 #define SA_CDEV_MAJOR 14
417 
418 static struct cdevsw sa_cdevsw = {
419 	/* open */	saopen,
420 	/* close */	saclose,
421 	/* read */	physread,
422 	/* write */	physwrite,
423 	/* ioctl */	saioctl,
424 	/* poll */	nopoll,
425 	/* mmap */	nommap,
426 	/* strategy */	sastrategy,
427 	/* name */	"sa",
428 	/* maj */	SA_CDEV_MAJOR,
429 	/* dump */	nodump,
430 	/* psize */	nopsize,
431 	/* flags */	D_TAPE,
432 };
433 
434 static struct extend_array *saperiphs;
435 
436 static int
437 saopen(dev_t dev, int flags, int fmt, struct proc *p)
438 {
439 	struct cam_periph *periph;
440 	struct sa_softc *softc;
441 	int unit;
442 	int mode;
443 	int density;
444 	int error;
445 	int s;
446 
447 	unit = SAUNIT(dev);
448 	mode = SAMODE(dev);
449 	density = SADENSITY(dev);
450 
451 	s = splsoftcam();
452 	periph = cam_extend_get(saperiphs, unit);
453 	if (periph == NULL) {
454 		(void) splx(s);
455 		return (ENXIO);
456 	}
457 	softc = (struct sa_softc *)periph->softc;
458 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
459 		splx(s);
460 		return (error);
461 	}
462 	splx(s);
463 
464 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
465 	    ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
466 
467 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
468 		cam_periph_unlock(periph);
469 		return (ENXIO);
470 	}
471 	if (SA_IS_CTRL(dev)) {
472 		softc->ctrl_mode = 1;
473 		cam_periph_unlock(periph);
474 		return (0);
475 	}
476 
477 
478 	if (softc->flags & SA_FLAG_OPEN) {
479 		error = EBUSY;
480 	} else if (softc->flags & SA_FLAG_INVALID) {
481 		error = ENXIO;
482 	} else {
483 		/*
484 		 * The function samount ensures media is loaded and ready.
485 		 * It also does a device RESERVE if the tape isn't yet mounted.
486 		 */
487 		error = samount(periph, flags, dev);
488 	}
489 
490 	if (error) {
491 		cam_periph_release(periph);
492 	} else {
493 		saprevent(periph, PR_PREVENT);
494 		softc->flags |= SA_FLAG_OPEN;
495 	}
496 	cam_periph_unlock(periph);
497 	return (error);
498 }
499 
500 static int
501 saclose(dev_t dev, int flag, int fmt, struct proc *p)
502 {
503 	struct	cam_periph *periph;
504 	struct	sa_softc *softc;
505 	int	unit, mode, error, writing, tmp;
506 	int	closedbits = SA_FLAG_OPEN;
507 
508 	unit = SAUNIT(dev);
509 	mode = SAMODE(dev);
510 	periph = cam_extend_get(saperiphs, unit);
511 	if (periph == NULL)
512 		return (ENXIO);
513 
514 	softc = (struct sa_softc *)periph->softc;
515 
516 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
517 	    ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
518 
519 
520 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
521 		return (error);
522 	}
523 
524 	if (SA_IS_CTRL(dev)) {
525 		softc->ctrl_mode = 0;
526 		cam_periph_release(periph);
527 		cam_periph_unlock(periph);
528 		return (0);
529 	}
530 
531 	/*
532 	 * Were we writing the tape?
533 	 */
534 	writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
535 
536 	/*
537 	 * See whether or not we need to write filemarks. If this
538 	 * fails, we probably have to assume we've lost tape
539 	 * position.
540 	 */
541 	error = sacheckeod(periph);
542 	if (error) {
543 		xpt_print_path(periph->path);
544 		printf("failed to write terminating filemark(s)\n");
545 		softc->flags |= SA_FLAG_TAPE_FROZEN;
546 	}
547 
548 	/*
549 	 * Whatever we end up doing, allow users to eject tapes from here on.
550 	 */
551 	saprevent(periph, PR_ALLOW);
552 
553 	/*
554 	 * Decide how to end...
555 	 */
556 	if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
557 		closedbits |= SA_FLAG_TAPE_FROZEN;
558 	} else switch (mode) {
559 	case SA_MODE_OFFLINE:
560 		/*
561 		 * An 'offline' close is an unconditional release of
562 		 * frozen && mount conditions, irrespective of whether
563 		 * these operations succeeded. The reason for this is
564 		 * to allow at least some kind of programmatic way
565 		 * around our state getting all fouled up. If somebody
566 		 * issues an 'offline' command, that will be allowed
567 		 * to clear state.
568 		 */
569 		(void) sarewind(periph);
570 		(void) saloadunload(periph, FALSE);
571 		closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
572 		break;
573 	case SA_MODE_REWIND:
574 		/*
575 		 * If the rewind fails, return an error- if anyone cares,
576 		 * but not overwriting any previous error.
577 		 *
578 		 * We don't clear the notion of mounted here, but we do
579 		 * clear the notion of frozen if we successfully rewound.
580 		 */
581 		tmp = sarewind(periph);
582 		if (tmp) {
583 			if (error != 0)
584 				error = tmp;
585 		} else {
586 			closedbits |= SA_FLAG_TAPE_FROZEN;
587 		}
588 		break;
589 	case SA_MODE_NOREWIND:
590 		/*
591 		 * If we're not rewinding/unloading the tape, find out
592 		 * whether we need to back up over one of two filemarks
593 		 * we wrote (if we wrote two filemarks) so that appends
594 		 * from this point on will be sane.
595 		 */
596 		if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
597 			tmp = saspace(periph, -1, SS_FILEMARKS);
598 			if (tmp) {
599 				xpt_print_path(periph->path);
600 				printf("unable to backspace over one of double"
601 				   " filemarks at end of tape\n");
602 				xpt_print_path(periph->path);
603 				printf("it is possible that this device"
604 				   " needs a SA_QUIRK_1FM quirk set for it\n");
605 				softc->flags |= SA_FLAG_TAPE_FROZEN;
606 			}
607 		}
608 		break;
609 	default:
610 		xpt_print_path(periph->path);
611 		panic("unknown mode 0x%x in saclose\n", mode);
612 		/* NOTREACHED */
613 		break;
614 	}
615 
616 	/*
617 	 * We wish to note here that there are no more filemarks to be written.
618 	 */
619 	softc->filemarks = 0;
620 	softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
621 
622 	/*
623 	 * And we are no longer open for business.
624 	 */
625 	softc->flags &= ~closedbits;
626 
627 	/*
628 	 * Inform users if tape state if frozen....
629 	 */
630 	if (softc->flags & SA_FLAG_TAPE_FROZEN) {
631 		xpt_print_path(periph->path);
632 		printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
633 		    "command to clear this state.\n");
634 	}
635 
636 	/* release the device if it is no longer mounted */
637 	if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
638 		sareservereleaseunit(periph, FALSE);
639 
640 	cam_periph_unlock(periph);
641 	cam_periph_release(periph);
642 
643 	return (error);
644 }
645 
646 /*
647  * Actually translate the requested transfer into one the physical driver
648  * can understand.  The transfer is described by a buf and will include
649  * only one physical transfer.
650  */
651 static void
652 sastrategy(struct bio *bp)
653 {
654 	struct cam_periph *periph;
655 	struct sa_softc *softc;
656 	u_int  unit;
657 	int    s;
658 
659 	bp->bio_resid = bp->bio_bcount;
660 	if (SA_IS_CTRL(bp->bio_dev)) {
661 		biofinish(bp, NULL, EINVAL);
662 		return;
663 	}
664 	unit = SAUNIT(bp->bio_dev);
665 	periph = cam_extend_get(saperiphs, unit);
666 	if (periph == NULL) {
667 		biofinish(bp, NULL, ENXIO);
668 		return;
669 	}
670 	softc = (struct sa_softc *)periph->softc;
671 
672 	s = splsoftcam();
673 
674 	if (softc->flags & SA_FLAG_INVALID) {
675 		splx(s);
676 		biofinish(bp, NULL, ENXIO);
677 		return;
678 	}
679 
680 	if (softc->flags & SA_FLAG_TAPE_FROZEN) {
681 		splx(s);
682 		biofinish(bp, NULL, EPERM);
683 		return;
684 	}
685 
686 	splx(s);
687 
688 	/*
689 	 * If it's a null transfer, return immediatly
690 	 */
691 	if (bp->bio_bcount == 0) {
692 		biodone(bp);
693 		return;
694 	}
695 
696 	/* valid request?  */
697 	if (softc->flags & SA_FLAG_FIXED) {
698 		/*
699 		 * Fixed block device.  The byte count must
700 		 * be a multiple of our block size.
701 		 */
702 		if (((softc->blk_mask != ~0) &&
703 		    ((bp->bio_bcount & softc->blk_mask) != 0)) ||
704 		    ((softc->blk_mask == ~0) &&
705 		    ((bp->bio_bcount % softc->min_blk) != 0))) {
706 			xpt_print_path(periph->path);
707 			printf("Invalid request.  Fixed block device "
708 			       "requests must be a multiple "
709 			       "of %d bytes\n", softc->min_blk);
710 			biofinish(bp, NULL, EINVAL);
711 			return;
712 		}
713 	} else if ((bp->bio_bcount > softc->max_blk) ||
714 		   (bp->bio_bcount < softc->min_blk) ||
715 		   (bp->bio_bcount & softc->blk_mask) != 0) {
716 
717 		xpt_print_path(periph->path);
718 		printf("Invalid request.  Variable block device "
719 		    "requests must be ");
720 		if (softc->blk_mask != 0) {
721 			printf("a multiple of %d ", (0x1 << softc->blk_gran));
722 		}
723 		printf("between %d and %d bytes\n", softc->min_blk,
724 		    softc->max_blk);
725 		biofinish(bp, NULL, EINVAL);
726 		return;
727         }
728 
729 	/*
730 	 * Mask interrupts so that the device cannot be invalidated until
731 	 * after we are in the queue.  Otherwise, we might not properly
732 	 * clean up one of the buffers.
733 	 */
734 	s = splbio();
735 
736 	/*
737 	 * Place it at the end of the queue.
738 	 */
739 	bioq_insert_tail(&softc->bio_queue, bp);
740 
741 	softc->queue_count++;
742 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
743 	    "%s byte %s queue count now %d\n", (int) bp->bio_bcount,
744 	     (softc->flags & SA_FLAG_FIXED)?  "fixed" : "variable",
745 	     (bp->bio_cmd == BIO_READ)? "read" : "write", softc->queue_count));
746 
747 	splx(s);
748 
749 	/*
750 	 * Schedule ourselves for performing the work.
751 	 */
752 	xpt_schedule(periph, 1);
753 
754 	return;
755 }
756 
757 static int
758 saioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
759 {
760 	struct cam_periph *periph;
761 	struct sa_softc *softc;
762 	scsi_space_code spaceop;
763 	int didlockperiph = 0;
764 	int s;
765 	int unit;
766 	int mode;
767 	int density;
768 	int error = 0;
769 
770 	unit = SAUNIT(dev);
771 	mode = SAMODE(dev);
772 	density = SADENSITY(dev);
773 	error = 0;		/* shut up gcc */
774 	spaceop = 0;		/* shut up gcc */
775 
776 	periph = cam_extend_get(saperiphs, unit);
777 	if (periph == NULL)
778 		return (ENXIO);
779 
780 	softc = (struct sa_softc *)periph->softc;
781 
782 	/*
783 	 * Check for control mode accesses. We allow MTIOCGET and
784 	 * MTIOCERRSTAT (but need to be the only one open in order
785 	 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
786 	 * and MTCOMP (but need to be the only one accessing this
787 	 * device to run those).
788 	 */
789 
790 	if (SA_IS_CTRL(dev)) {
791 		switch (cmd) {
792 		case MTIOCGETEOTMODEL:
793 		case MTIOCGET:
794 			break;
795 		case MTIOCERRSTAT:
796 			/*
797 			 * If the periph isn't already locked, lock it
798 			 * so our MTIOCERRSTAT can reset latched error stats.
799 			 *
800 			 * If the periph is already locked, skip it because
801 			 * we're just getting status and it'll be up to the
802 			 * other thread that has this device open to do
803 			 * an MTIOCERRSTAT that would clear latched status.
804 			 */
805 			s = splsoftcam();
806 			if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
807 				error = cam_periph_lock(periph, PRIBIO|PCATCH);
808 				if (error != 0) {
809 					splx(s);
810 					return (error);
811 				}
812 				didlockperiph = 1;
813 			}
814 			break;
815 
816 		case MTIOCSETEOTMODEL:
817 		case MTSETBSIZ:
818 		case MTSETDNSTY:
819 		case MTCOMP:
820 			/*
821 			 * We need to acquire the peripheral here rather
822 			 * than at open time because we are sharing writable
823 			 * access to data structures.
824 			 */
825 			s = splsoftcam();
826 			error = cam_periph_lock(periph, PRIBIO|PCATCH);
827 			if (error != 0) {
828 				splx(s);
829 				return (error);
830 			}
831 			didlockperiph = 1;
832 			break;
833 
834 		default:
835 			return (EINVAL);
836 		}
837 	}
838 
839 	/*
840 	 * Find the device that the user is talking about
841 	 */
842 	switch (cmd) {
843 	case MTIOCGET:
844 	{
845 		struct mtget *g = (struct mtget *)arg;
846 
847 		/*
848 		 * If this isn't the control mode device, actually go out
849 		 * and ask the drive again what it's set to.
850 		 */
851 		if (!SA_IS_CTRL(dev)) {
852 			u_int8_t write_protect;
853 			int comp_enabled, comp_supported;
854 			error = sagetparams(periph, SA_PARAM_ALL,
855 			    &softc->media_blksize, &softc->media_density,
856 			    &softc->media_numblks, &softc->buffer_mode,
857 			    &write_protect, &softc->speed, &comp_supported,
858 			    &comp_enabled, &softc->comp_algorithm, NULL);
859 			if (error)
860 				break;
861 			if (write_protect)
862 				softc->flags |= SA_FLAG_TAPE_WP;
863 			else
864 				softc->flags &= ~SA_FLAG_TAPE_WP;
865 			softc->flags &= ~(SA_FLAG_COMP_SUPP|
866 			    SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
867 			if (comp_supported) {
868 				if (softc->saved_comp_algorithm == 0)
869 					softc->saved_comp_algorithm =
870 					    softc->comp_algorithm;
871 				softc->flags |= SA_FLAG_COMP_SUPP;
872 				if (comp_enabled)
873 					softc->flags |= SA_FLAG_COMP_ENABLED;
874 			} else
875 				softc->flags |= SA_FLAG_COMP_UNSUPP;
876 		}
877 		bzero(g, sizeof(struct mtget));
878 		g->mt_type = MT_ISAR;
879 		if (softc->flags & SA_FLAG_COMP_UNSUPP) {
880 			g->mt_comp = MT_COMP_UNSUPP;
881 			g->mt_comp0 = MT_COMP_UNSUPP;
882 			g->mt_comp1 = MT_COMP_UNSUPP;
883 			g->mt_comp2 = MT_COMP_UNSUPP;
884 			g->mt_comp3 = MT_COMP_UNSUPP;
885 		} else {
886 			if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
887 				g->mt_comp = MT_COMP_DISABLED;
888 			} else {
889 				g->mt_comp = softc->comp_algorithm;
890 			}
891 			g->mt_comp0 = softc->comp_algorithm;
892 			g->mt_comp1 = softc->comp_algorithm;
893 			g->mt_comp2 = softc->comp_algorithm;
894 			g->mt_comp3 = softc->comp_algorithm;
895 		}
896 		g->mt_density = softc->media_density;
897 		g->mt_density0 = softc->media_density;
898 		g->mt_density1 = softc->media_density;
899 		g->mt_density2 = softc->media_density;
900 		g->mt_density3 = softc->media_density;
901 		g->mt_blksiz = softc->media_blksize;
902 		g->mt_blksiz0 = softc->media_blksize;
903 		g->mt_blksiz1 = softc->media_blksize;
904 		g->mt_blksiz2 = softc->media_blksize;
905 		g->mt_blksiz3 = softc->media_blksize;
906 		g->mt_fileno = softc->fileno;
907 		g->mt_blkno = softc->blkno;
908 		g->mt_dsreg = (short) softc->dsreg;
909 		/*
910 		 * Yes, we know that this is likely to overflow
911 		 */
912 		if (softc->last_resid_was_io) {
913 			if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
914 				if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
915 					softc->last_io_resid = 0;
916 				}
917 			}
918 		} else {
919 			if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
920 				if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
921 					softc->last_ctl_resid = 0;
922 				}
923 			}
924 		}
925 		if (g->mt_resid) {
926 		}
927 		error = 0;
928 		break;
929 	}
930 	case MTIOCERRSTAT:
931 	{
932 		struct scsi_tape_errors *sep =
933 		    &((union mterrstat *)arg)->scsi_errstat;
934 
935 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
936 		    ("saioctl: MTIOCERRSTAT\n"));
937 
938 		bzero(sep, sizeof(*sep));
939 		sep->io_resid = softc->last_io_resid;
940 		bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
941 		    sizeof (sep->io_sense));
942 		bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
943 		    sizeof (sep->io_cdb));
944 		sep->ctl_resid = softc->last_ctl_resid;
945 		bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
946 		    sizeof (sep->ctl_sense));
947 		bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
948 		    sizeof (sep->ctl_cdb));
949 
950 		if (SA_IS_CTRL(dev) == 0 || didlockperiph)
951 			bzero((caddr_t) &softc->errinfo,
952 			    sizeof (softc->errinfo));
953 		error = 0;
954 		break;
955 	}
956 	case MTIOCTOP:
957 	{
958 		struct mtop *mt;
959 		int    count;
960 
961 		mt = (struct mtop *)arg;
962 
963 		CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
964 			 ("saioctl: op=0x%x count=0x%x\n",
965 			  mt->mt_op, mt->mt_count));
966 
967 		count = mt->mt_count;
968 		switch (mt->mt_op) {
969 		case MTWEOF:	/* write an end-of-file marker */
970 			/*
971 			 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
972 			 * flag because by keeping track of filemarks
973 			 * we have last written we know ehether or not
974 			 * we need to write more when we close the device.
975 			 */
976 			error = sawritefilemarks(periph, count, FALSE);
977 			break;
978 		case MTWSS:	/* write a setmark */
979 			error = sawritefilemarks(periph, count, TRUE);
980 			break;
981 		case MTBSR:	/* backward space record */
982 		case MTFSR:	/* forward space record */
983 		case MTBSF:	/* backward space file */
984 		case MTFSF:	/* forward space file */
985 		case MTBSS:	/* backward space setmark */
986 		case MTFSS:	/* forward space setmark */
987 		case MTEOD:	/* space to end of recorded medium */
988 		{
989 			int nmarks;
990 
991 			spaceop = SS_FILEMARKS;
992 			nmarks = softc->filemarks;
993 			error = sacheckeod(periph);
994 			if (error) {
995 				xpt_print_path(periph->path);
996 				printf("EOD check prior to spacing failed\n");
997 				softc->flags |= SA_FLAG_EIO_PENDING;
998 				break;
999 			}
1000 			nmarks -= softc->filemarks;
1001 			switch(mt->mt_op) {
1002 			case MTBSR:
1003 				count = -count;
1004 				/* FALLTHROUGH */
1005 			case MTFSR:
1006 				spaceop = SS_BLOCKS;
1007 				break;
1008 			case MTBSF:
1009 				count = -count;
1010 				/* FALLTHROUGH */
1011 			case MTFSF:
1012 				break;
1013 			case MTBSS:
1014 				count = -count;
1015 				/* FALLTHROUGH */
1016 			case MTFSS:
1017 				spaceop = SS_SETMARKS;
1018 				break;
1019 			case MTEOD:
1020 				spaceop = SS_EOD;
1021 				count = 0;
1022 				nmarks = 0;
1023 				break;
1024 			default:
1025 				error = EINVAL;
1026 				break;
1027 			}
1028 			if (error)
1029 				break;
1030 
1031 			nmarks = softc->filemarks;
1032 			/*
1033 			 * XXX: Why are we checking again?
1034 			 */
1035 			error = sacheckeod(periph);
1036 			if (error)
1037 				break;
1038 			nmarks -= softc->filemarks;
1039 			error = saspace(periph, count - nmarks, spaceop);
1040 			/*
1041 			 * At this point, clear that we've written the tape
1042 			 * and that we've written any filemarks. We really
1043 			 * don't know what the applications wishes to do next-
1044 			 * the sacheckeod's will make sure we terminated the
1045 			 * tape correctly if we'd been writing, but the next
1046 			 * action the user application takes will set again
1047 			 * whether we need to write filemarks.
1048 			 */
1049 			softc->flags &=
1050 			    ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1051 			softc->filemarks = 0;
1052 			break;
1053 		}
1054 		case MTREW:	/* rewind */
1055 			(void) sacheckeod(periph);
1056 			error = sarewind(periph);
1057 			/* see above */
1058 			softc->flags &=
1059 			    ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1060 			softc->filemarks = 0;
1061 			break;
1062 		case MTERASE:	/* erase */
1063 			error = saerase(periph, count);
1064 			softc->flags &=
1065 			    ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1066 			break;
1067 		case MTRETENS:	/* re-tension tape */
1068 			error = saretension(periph);
1069 			softc->flags &=
1070 			    ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1071 			break;
1072 		case MTOFFL:	/* rewind and put the drive offline */
1073 
1074 			(void) sacheckeod(periph);
1075 			/* see above */
1076 			softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1077 			softc->filemarks = 0;
1078 
1079 			error = sarewind(periph);
1080 			/* clear the frozen flag anyway */
1081 			softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1082 
1083 			/*
1084 			 * Be sure to allow media removal before ejecting.
1085 			 */
1086 
1087 			saprevent(periph, PR_ALLOW);
1088 			if (error == 0) {
1089 				error = saloadunload(periph, FALSE);
1090 				if (error == 0) {
1091 					softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1092 				}
1093 			}
1094 			break;
1095 
1096 		case MTNOP:	/* no operation, sets status only */
1097 		case MTCACHE:	/* enable controller cache */
1098 		case MTNOCACHE:	/* disable controller cache */
1099 			error = 0;
1100 			break;
1101 
1102 		case MTSETBSIZ:	/* Set block size for device */
1103 
1104 			error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1105 					    0, 0, 0);
1106 			if (error == 0) {
1107 				softc->last_media_blksize =
1108 				    softc->media_blksize;
1109 				softc->media_blksize = count;
1110 				if (count) {
1111 					softc->flags |= SA_FLAG_FIXED;
1112 					if (powerof2(count)) {
1113 						softc->blk_shift =
1114 						    ffs(count) - 1;
1115 						softc->blk_mask = count - 1;
1116 					} else {
1117 						softc->blk_mask = ~0;
1118 						softc->blk_shift = 0;
1119 					}
1120 					/*
1121 					 * Make the user's desire 'persistent'.
1122 					 */
1123 					softc->quirks &= ~SA_QUIRK_VARIABLE;
1124 					softc->quirks |= SA_QUIRK_FIXED;
1125 				} else {
1126 					softc->flags &= ~SA_FLAG_FIXED;
1127 					if (softc->max_blk == 0) {
1128 						softc->max_blk = ~0;
1129 					}
1130 					softc->blk_shift = 0;
1131 					if (softc->blk_gran != 0) {
1132 						softc->blk_mask =
1133 						    softc->blk_gran - 1;
1134 					} else {
1135 						softc->blk_mask = 0;
1136 					}
1137 					/*
1138 					 * Make the user's desire 'persistent'.
1139 					 */
1140 					softc->quirks |= SA_QUIRK_VARIABLE;
1141 					softc->quirks &= ~SA_QUIRK_FIXED;
1142 				}
1143 			}
1144 			break;
1145 		case MTSETDNSTY:	/* Set density for device and mode */
1146 			if (count > UCHAR_MAX) {
1147 				error = EINVAL;
1148 				break;
1149 			} else {
1150 				error = sasetparams(periph, SA_PARAM_DENSITY,
1151 						    0, count, 0, 0);
1152 			}
1153 			break;
1154 		case MTCOMP:	/* enable compression */
1155 			/*
1156 			 * Some devices don't support compression, and
1157 			 * don't like it if you ask them for the
1158 			 * compression page.
1159 			 */
1160 			if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1161 			    (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1162 				error = ENODEV;
1163 				break;
1164 			}
1165 			error = sasetparams(periph, SA_PARAM_COMPRESSION,
1166 			    0, 0, count, SF_NO_PRINT);
1167 			break;
1168 		default:
1169 			error = EINVAL;
1170 		}
1171 		break;
1172 	}
1173 	case MTIOCIEOT:
1174 	case MTIOCEEOT:
1175 		error = 0;
1176 		break;
1177 	case MTIOCRDSPOS:
1178 		error = sardpos(periph, 0, (u_int32_t *) arg);
1179 		break;
1180 	case MTIOCRDHPOS:
1181 		error = sardpos(periph, 1, (u_int32_t *) arg);
1182 		break;
1183 	case MTIOCSLOCATE:
1184 		error = sasetpos(periph, 0, (u_int32_t *) arg);
1185 		break;
1186 	case MTIOCHLOCATE:
1187 		error = sasetpos(periph, 1, (u_int32_t *) arg);
1188 		break;
1189 	case MTIOCGETEOTMODEL:
1190 		error = 0;
1191 		if (softc->quirks & SA_QUIRK_1FM)
1192 			mode = 1;
1193 		else
1194 			mode = 2;
1195 		*((u_int32_t *) arg) = mode;
1196 		break;
1197 	case MTIOCSETEOTMODEL:
1198 		error = 0;
1199 		switch (*((u_int32_t *) arg)) {
1200 		case 1:
1201 			softc->quirks &= ~SA_QUIRK_2FM;
1202 			softc->quirks |= SA_QUIRK_1FM;
1203 			break;
1204 		case 2:
1205 			softc->quirks &= ~SA_QUIRK_1FM;
1206 			softc->quirks |= SA_QUIRK_2FM;
1207 			break;
1208 		default:
1209 			error = EINVAL;
1210 			break;
1211 		}
1212 		break;
1213 	default:
1214 		error = cam_periph_ioctl(periph, cmd, arg, saerror);
1215 		break;
1216 	}
1217 
1218 	/*
1219 	 * Check to see if we cleared a frozen state
1220 	 */
1221 	if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1222 		switch(cmd) {
1223 		case MTIOCRDSPOS:
1224 		case MTIOCRDHPOS:
1225 		case MTIOCSLOCATE:
1226 		case MTIOCHLOCATE:
1227 			softc->fileno = (daddr_t) -1;
1228 			softc->blkno = (daddr_t) -1;
1229 			softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1230 			xpt_print_path(periph->path);
1231 			printf("tape state now unfrozen.\n");
1232 			break;
1233 		default:
1234 			break;
1235 		}
1236 	}
1237 	if (didlockperiph) {
1238 		cam_periph_unlock(periph);
1239 	}
1240 	return (error);
1241 }
1242 
1243 static void
1244 sainit(void)
1245 {
1246 	cam_status status;
1247 	struct cam_path *path;
1248 
1249 	/*
1250 	 * Create our extend array for storing the devices we attach to.
1251 	 */
1252 	saperiphs = cam_extend_new();
1253 	if (saperiphs == NULL) {
1254 		printf("sa: Failed to alloc extend array!\n");
1255 		return;
1256 	}
1257 
1258 	/*
1259 	 * Install a global async callback.
1260 	 */
1261 	status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1262 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1263 
1264 	if (status == CAM_REQ_CMP) {
1265 		/* Register the async callbacks of interrest */
1266 		struct ccb_setasync csa; /*
1267 					  * This is an immediate CCB,
1268 					  * so using the stack is OK
1269 					  */
1270 		xpt_setup_ccb(&csa.ccb_h, path, 5);
1271 		csa.ccb_h.func_code = XPT_SASYNC_CB;
1272 		csa.event_enable = AC_FOUND_DEVICE;
1273 		csa.callback = saasync;
1274 		csa.callback_arg = NULL;
1275 		xpt_action((union ccb *)&csa);
1276 		status = csa.ccb_h.status;
1277 		xpt_free_path(path);
1278 	}
1279 
1280 	if (status != CAM_REQ_CMP) {
1281 		printf("sa: Failed to attach master async callback "
1282 		       "due to status 0x%x!\n", status);
1283 	}
1284 }
1285 
1286 static void
1287 saoninvalidate(struct cam_periph *periph)
1288 {
1289 	struct sa_softc *softc;
1290 	struct bio *q_bp;
1291 	struct ccb_setasync csa;
1292 	int s;
1293 
1294 	softc = (struct sa_softc *)periph->softc;
1295 
1296 	/*
1297 	 * De-register any async callbacks.
1298 	 */
1299 	xpt_setup_ccb(&csa.ccb_h, periph->path,
1300 		      /* priority */ 5);
1301 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1302 	csa.event_enable = 0;
1303 	csa.callback = saasync;
1304 	csa.callback_arg = periph;
1305 	xpt_action((union ccb *)&csa);
1306 
1307 	softc->flags |= SA_FLAG_INVALID;
1308 
1309 	/*
1310 	 * Although the oninvalidate() routines are always called at
1311 	 * splsoftcam, we need to be at splbio() here to keep the buffer
1312 	 * queue from being modified while we traverse it.
1313 	 */
1314 	s = splbio();
1315 
1316 	/*
1317 	 * Return all queued I/O with ENXIO.
1318 	 * XXX Handle any transactions queued to the card
1319 	 *     with XPT_ABORT_CCB.
1320 	 */
1321 	while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
1322 		bioq_remove(&softc->bio_queue, q_bp);
1323 		q_bp->bio_resid = q_bp->bio_bcount;
1324 		biofinish(q_bp, NULL, ENXIO);
1325 	}
1326 	softc->queue_count = 0;
1327 	splx(s);
1328 
1329 	xpt_print_path(periph->path);
1330 	printf("lost device\n");
1331 
1332 }
1333 
1334 static void
1335 sacleanup(struct cam_periph *periph)
1336 {
1337 	struct sa_softc *softc;
1338 	int i;
1339 
1340 	softc = (struct sa_softc *)periph->softc;
1341 
1342 	devstat_remove_entry(&softc->device_stats);
1343 
1344 	destroy_dev(softc->devs.ctl_dev);
1345 
1346 	for (i = 0; i < SA_NUM_MODES; i++) {
1347 		destroy_dev(softc->devs.mode_devs[i].r_dev);
1348 		destroy_dev(softc->devs.mode_devs[i].nr_dev);
1349 		destroy_dev(softc->devs.mode_devs[i].er_dev);
1350 	}
1351 
1352 	cam_extend_release(saperiphs, periph->unit_number);
1353 	xpt_print_path(periph->path);
1354 	printf("removing device entry\n");
1355 	free(softc, M_DEVBUF);
1356 }
1357 
1358 static void
1359 saasync(void *callback_arg, u_int32_t code,
1360 	struct cam_path *path, void *arg)
1361 {
1362 	struct cam_periph *periph;
1363 
1364 	periph = (struct cam_periph *)callback_arg;
1365 	switch (code) {
1366 	case AC_FOUND_DEVICE:
1367 	{
1368 		struct ccb_getdev *cgd;
1369 		cam_status status;
1370 
1371 		cgd = (struct ccb_getdev *)arg;
1372 
1373 		if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1374 			break;
1375 
1376 		/*
1377 		 * Allocate a peripheral instance for
1378 		 * this device and start the probe
1379 		 * process.
1380 		 */
1381 		status = cam_periph_alloc(saregister, saoninvalidate,
1382 					  sacleanup, sastart,
1383 					  "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1384 					  saasync, AC_FOUND_DEVICE, cgd);
1385 
1386 		if (status != CAM_REQ_CMP
1387 		 && status != CAM_REQ_INPROG)
1388 			printf("saasync: Unable to probe new device "
1389 				"due to status 0x%x\n", status);
1390 		break;
1391 	}
1392 	default:
1393 		cam_periph_async(periph, code, path, arg);
1394 		break;
1395 	}
1396 }
1397 
1398 static cam_status
1399 saregister(struct cam_periph *periph, void *arg)
1400 {
1401 	struct sa_softc *softc;
1402 	struct ccb_setasync csa;
1403 	struct ccb_getdev *cgd;
1404 	caddr_t match;
1405 	int i;
1406 
1407 	cgd = (struct ccb_getdev *)arg;
1408 	if (periph == NULL) {
1409 		printf("saregister: periph was NULL!!\n");
1410 		return (CAM_REQ_CMP_ERR);
1411 	}
1412 
1413 	if (cgd == NULL) {
1414 		printf("saregister: no getdev CCB, can't register device\n");
1415 		return (CAM_REQ_CMP_ERR);
1416 	}
1417 
1418 	softc = (struct sa_softc *)
1419 	    malloc(sizeof (*softc), M_DEVBUF, M_NOWAIT | M_ZERO);
1420 	if (softc == NULL) {
1421 		printf("saregister: Unable to probe new device. "
1422 		       "Unable to allocate softc\n");
1423 		return (CAM_REQ_CMP_ERR);
1424 	}
1425 	softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1426 	softc->state = SA_STATE_NORMAL;
1427 	softc->fileno = (daddr_t) -1;
1428 	softc->blkno = (daddr_t) -1;
1429 
1430 	bioq_init(&softc->bio_queue);
1431 	periph->softc = softc;
1432 	cam_extend_set(saperiphs, periph->unit_number, periph);
1433 
1434 	/*
1435 	 * See if this device has any quirks.
1436 	 */
1437 	match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1438 			       (caddr_t)sa_quirk_table,
1439 			       sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1440 			       sizeof(*sa_quirk_table), scsi_inquiry_match);
1441 
1442 	if (match != NULL) {
1443 		softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1444 		softc->last_media_blksize =
1445 		    ((struct sa_quirk_entry *)match)->prefblk;
1446 #ifdef	CAMDEBUG
1447 		xpt_print_path(periph->path);
1448 		printf("found quirk entry %d\n", (int)
1449 		    (((struct sa_quirk_entry *) match) - sa_quirk_table));
1450 #endif
1451 	} else
1452 		softc->quirks = SA_QUIRK_NONE;
1453 
1454 	/*
1455  	 * The SA driver supports a blocksize, but we don't know the
1456 	 * blocksize until we media is inserted.  So, set a flag to
1457 	 * indicate that the blocksize is unavailable right now.
1458 	 */
1459 	devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1460 	    DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1461 	    DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1462 
1463 	softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1464 	    periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1465 	    0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1466 
1467 	for (i = 0; i < SA_NUM_MODES; i++) {
1468 
1469 		softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1470 		    SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1471 		    UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1472 		    periph->periph_name, periph->unit_number, i);
1473 
1474 		softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1475 		    SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1476 		    UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1477 		    periph->periph_name, periph->unit_number, i);
1478 
1479 
1480 		softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1481 		    SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1482 		    UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1483 		    periph->periph_name, periph->unit_number, i);
1484 
1485 		/*
1486 		 * Make the (well known) aliases for the first mode.
1487 		 */
1488 		if (i == 0) {
1489 			make_dev_alias(softc->devs.mode_devs[i].r_dev,
1490 			   "%s%d", periph->periph_name, periph->unit_number);
1491 			make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1492 			    "n%s%d", periph->periph_name, periph->unit_number);
1493 			make_dev_alias(softc->devs.mode_devs[i].er_dev,
1494 			    "e%s%d", periph->periph_name, periph->unit_number);
1495 		}
1496 	}
1497 
1498 	/*
1499 	 * Add an async callback so that we get
1500 	 * notified if this device goes away.
1501 	 */
1502 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1503 	csa.ccb_h.func_code = XPT_SASYNC_CB;
1504 	csa.event_enable = AC_LOST_DEVICE;
1505 	csa.callback = saasync;
1506 	csa.callback_arg = periph;
1507 	xpt_action((union ccb *)&csa);
1508 
1509 	xpt_announce_periph(periph, NULL);
1510 
1511 	return (CAM_REQ_CMP);
1512 }
1513 
1514 static void
1515 sastart(struct cam_periph *periph, union ccb *start_ccb)
1516 {
1517 	struct sa_softc *softc;
1518 
1519 	softc = (struct sa_softc *)periph->softc;
1520 
1521 	CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastart"));
1522 
1523 
1524 	switch (softc->state) {
1525 	case SA_STATE_NORMAL:
1526 	{
1527 		/* Pull a buffer from the queue and get going on it */
1528 		struct bio *bp;
1529 		int s;
1530 
1531 		/*
1532 		 * See if there is a buf with work for us to do..
1533 		 */
1534 		s = splbio();
1535 		bp = bioq_first(&softc->bio_queue);
1536 		if (periph->immediate_priority <= periph->pinfo.priority) {
1537 			CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1538 					("queuing for immediate ccb\n"));
1539 			Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1540 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1541 					  periph_links.sle);
1542 			periph->immediate_priority = CAM_PRIORITY_NONE;
1543 			splx(s);
1544 			wakeup(&periph->ccb_list);
1545 		} else if (bp == NULL) {
1546 			splx(s);
1547 			xpt_release_ccb(start_ccb);
1548 		} else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1549 			struct bio *done_bp;
1550 			softc->queue_count--;
1551 			bioq_remove(&softc->bio_queue, bp);
1552 			bp->bio_resid = bp->bio_bcount;
1553 			bp->bio_flags |= BIO_ERROR;
1554 			if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1555 				if (bp->bio_cmd == BIO_WRITE)
1556 					bp->bio_error = ENOSPC;
1557 				else
1558 					bp->bio_error = EIO;
1559 			}
1560 			if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1561 				bp->bio_error = EIO;
1562 			}
1563 			if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1564 				bp->bio_error = EIO;
1565 			}
1566 			done_bp = bp;
1567 			bp = bioq_first(&softc->bio_queue);
1568 			/*
1569 			 * Only if we have no other buffers queued up
1570 			 * do we clear the pending error flag.
1571 			 */
1572 			if (bp == NULL)
1573 				softc->flags &= ~SA_FLAG_ERR_PENDING;
1574 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1575 			    ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1576 			    "%d more buffers queued up\n",
1577 			    (softc->flags & SA_FLAG_ERR_PENDING),
1578 			    (bp != NULL)? "not " : " ", softc->queue_count));
1579 			splx(s);
1580 			xpt_release_ccb(start_ccb);
1581 			biodone(done_bp);
1582 		} else {
1583 			u_int32_t length;
1584 
1585 			bioq_remove(&softc->bio_queue, bp);
1586 			softc->queue_count--;
1587 
1588 			if ((softc->flags & SA_FLAG_FIXED) != 0) {
1589 				if (softc->blk_shift != 0) {
1590 					length =
1591 					    bp->bio_bcount >> softc->blk_shift;
1592 				} else if (softc->media_blksize != 0) {
1593 					length = bp->bio_bcount /
1594 					    softc->media_blksize;
1595 				} else {
1596 					bp->bio_error = EIO;
1597 					xpt_print_path(periph->path);
1598 					printf("zero blocksize for "
1599 					    "FIXED length writes?\n");
1600 					splx(s);
1601 					biodone(bp);
1602 					break;
1603 				}
1604 				CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1605 				    ("Fixed Record Count is %d\n", length));
1606 			} else {
1607 				length = bp->bio_bcount;
1608 				CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1609 				    ("Variable Record Count is %d\n", length));
1610 			}
1611 			devstat_start_transaction(&softc->device_stats);
1612 			/*
1613 			 * Some people have theorized that we should
1614 			 * suppress illegal length indication if we are
1615 			 * running in variable block mode so that we don't
1616 			 * have to request sense every time our requested
1617 			 * block size is larger than the written block.
1618 			 * The residual information from the ccb allows
1619 			 * us to identify this situation anyway.  The only
1620 			 * problem with this is that we will not get
1621 			 * information about blocks that are larger than
1622 			 * our read buffer unless we set the block size
1623 			 * in the mode page to something other than 0.
1624 			 *
1625 			 * I believe that this is a non-issue. If user apps
1626 			 * don't adjust their read size to match our record
1627 			 * size, that's just life. Anyway, the typical usage
1628 			 * would be to issue, e.g., 64KB reads and occasionally
1629 			 * have to do deal with 512 byte or 1KB intermediate
1630 			 * records.
1631 			 */
1632 			softc->dsreg = (bp->bio_cmd == BIO_READ)?
1633 			    MTIO_DSREG_RD : MTIO_DSREG_WR;
1634 			scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1635 			    MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ),
1636 			    FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1637 			    length, bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1638 			    120 * 60 * 1000);
1639 			start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1640 			Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1641 			start_ccb->ccb_h.ccb_bp = bp;
1642 			bp = bioq_first(&softc->bio_queue);
1643 			splx(s);
1644 			xpt_action(start_ccb);
1645 		}
1646 
1647 		if (bp != NULL) {
1648 			/* Have more work to do, so ensure we stay scheduled */
1649 			xpt_schedule(periph, 1);
1650 		}
1651 		break;
1652 	}
1653 	case SA_STATE_ABNORMAL:
1654 	default:
1655 		panic("state 0x%x in sastart", softc->state);
1656 		break;
1657 	}
1658 }
1659 
1660 
1661 static void
1662 sadone(struct cam_periph *periph, union ccb *done_ccb)
1663 {
1664 	struct sa_softc *softc;
1665 	struct ccb_scsiio *csio;
1666 
1667 	softc = (struct sa_softc *)periph->softc;
1668 	csio = &done_ccb->csio;
1669 	switch (CCB_Type(csio)) {
1670 	case SA_CCB_BUFFER_IO:
1671 	{
1672 		struct bio *bp;
1673 		int error;
1674 
1675 		softc->dsreg = MTIO_DSREG_REST;
1676 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1677 		error = 0;
1678 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1679 			if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1680 				/*
1681 				 * A retry was scheduled, so just return.
1682 				 */
1683 				return;
1684 			}
1685 		}
1686 
1687 		if (error == EIO) {
1688 			int s;
1689 			struct bio *q_bp;
1690 
1691 			/*
1692 			 * Catastrophic error. Mark the tape as frozen
1693 			 * (we no longer know tape position).
1694 			 *
1695 			 * Return all queued I/O with EIO, and unfreeze
1696 			 * our queue so that future transactions that
1697 			 * attempt to fix this problem can get to the
1698 			 * device.
1699 			 *
1700 			 */
1701 
1702 			s = splbio();
1703 			softc->flags |= SA_FLAG_TAPE_FROZEN;
1704 			while ((q_bp = bioq_first(&softc->bio_queue)) != NULL) {
1705 				bioq_remove(&softc->bio_queue, q_bp);
1706 				q_bp->bio_resid = q_bp->bio_bcount;
1707 				biofinish(q_bp, NULL, EIO);
1708 			}
1709 			splx(s);
1710 		}
1711 		if (error != 0) {
1712 			bp->bio_resid = bp->bio_bcount;
1713 			bp->bio_error = error;
1714 			bp->bio_flags |= BIO_ERROR;
1715 			/*
1716 			 * In the error case, position is updated in saerror.
1717 			 */
1718 		} else {
1719 			bp->bio_resid = csio->resid;
1720 			bp->bio_error = 0;
1721 			if (csio->resid != 0) {
1722 				bp->bio_flags |= BIO_ERROR;
1723 			}
1724 			if (bp->bio_cmd == BIO_WRITE) {
1725 				softc->flags |= SA_FLAG_TAPE_WRITTEN;
1726 				softc->filemarks = 0;
1727 			}
1728 			if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1729 			    (softc->blkno != (daddr_t) -1)) {
1730 				if ((softc->flags & SA_FLAG_FIXED) != 0) {
1731 					u_int32_t l;
1732 					if (softc->blk_shift != 0) {
1733 						l = bp->bio_bcount >>
1734 							softc->blk_shift;
1735 					} else {
1736 						l = bp->bio_bcount /
1737 							softc->media_blksize;
1738 					}
1739 					softc->blkno += (daddr_t) l;
1740 				} else {
1741 					softc->blkno++;
1742 				}
1743 			}
1744 		}
1745 		/*
1746 		 * If we had an error (immediate or pending),
1747 		 * release the device queue now.
1748 		 */
1749 		if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1750 			cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1751 #ifdef	CAMDEBUG
1752 		if (error || bp->bio_resid) {
1753 			CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1754 			    	  ("error %d resid %ld count %ld\n", error,
1755 				  bp->bio_resid, bp->bio_bcount));
1756 		}
1757 #endif
1758 		biofinish(bp, &softc->device_stats, 0);
1759 		break;
1760 	}
1761 	case SA_CCB_WAITING:
1762 	{
1763 		/* Caller will release the CCB */
1764 		wakeup(&done_ccb->ccb_h.cbfcnp);
1765 		return;
1766 	}
1767 	}
1768 	xpt_release_ccb(done_ccb);
1769 }
1770 
1771 /*
1772  * Mount the tape (make sure it's ready for I/O).
1773  */
1774 static int
1775 samount(struct cam_periph *periph, int oflags, dev_t dev)
1776 {
1777 	struct	sa_softc *softc;
1778 	union	ccb *ccb;
1779 	int	error;
1780 
1781 	/*
1782 	 * oflags can be checked for 'kind' of open (read-only check) - later
1783 	 * dev can be checked for a control-mode or compression open - later
1784 	 */
1785 	UNUSED_PARAMETER(oflags);
1786 	UNUSED_PARAMETER(dev);
1787 
1788 
1789 	softc = (struct sa_softc *)periph->softc;
1790 
1791 	/*
1792 	 * This should determine if something has happend since the last
1793 	 * open/mount that would invalidate the mount. We do *not* want
1794 	 * to retry this command- we just want the status. But we only
1795 	 * do this if we're mounted already- if we're not mounted,
1796 	 * we don't care about the unit read state and can instead use
1797 	 * this opportunity to attempt to reserve the tape unit.
1798 	 */
1799 
1800 	if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1801 		ccb = cam_periph_getccb(periph, 1);
1802 		scsi_test_unit_ready(&ccb->csio, 0, sadone,
1803 		    MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, 5 * 60 * 1000);
1804 		error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1805 		    &softc->device_stats);
1806 		QFRLS(ccb);
1807 		if (error == ENXIO) {
1808 			softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1809 			scsi_test_unit_ready(&ccb->csio, 0, sadone,
1810 			    MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, 5 * 60 * 1000);
1811 			error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1812 			    &softc->device_stats);
1813 			QFRLS(ccb);
1814 		} else if (error) {
1815 			/*
1816 			 * We don't need to freeze the tape because we
1817 			 * will now attempt to rewind/load it.
1818 			 */
1819 			softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1820 			if (CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) {
1821 				xpt_print_path(ccb->ccb_h.path);
1822 				printf("error %d on TUR in samount\n", error);
1823 			}
1824 		}
1825 	} else {
1826 		error = sareservereleaseunit(periph, TRUE);
1827 		if (error) {
1828 			return (error);
1829 		}
1830 		ccb = cam_periph_getccb(periph, 1);
1831 		scsi_test_unit_ready(&ccb->csio, 0, sadone,
1832 		    MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, 5 * 60 * 1000);
1833 		error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1834 		    &softc->device_stats);
1835 		QFRLS(ccb);
1836 	}
1837 
1838 	if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1839 		struct scsi_read_block_limits_data *rblim = NULL;
1840 		int comp_enabled, comp_supported;
1841 		u_int8_t write_protect, guessing = 0;
1842 
1843 		/*
1844 		 * Clear out old state.
1845 		 */
1846 		softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1847 				  SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1848 				  SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1849 		softc->filemarks = 0;
1850 
1851 		/*
1852 		 * *Very* first off, make sure we're loaded to BOT.
1853 		 */
1854 		scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1855 		    FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1856 		error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1857 		    &softc->device_stats);
1858 		QFRLS(ccb);
1859 
1860 		/*
1861 		 * In case this doesn't work, do a REWIND instead
1862 		 */
1863 		if (error) {
1864 			scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1865 			    FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1866 			error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1867 				&softc->device_stats);
1868 			QFRLS(ccb);
1869 		}
1870 		if (error) {
1871 			xpt_release_ccb(ccb);
1872 			goto exit;
1873 		}
1874 
1875 		/*
1876 		 * Do a dummy test read to force access to the
1877 		 * media so that the drive will really know what's
1878 		 * there. We actually don't really care what the
1879 		 * blocksize on tape is and don't expect to really
1880 		 * read a full record.
1881 		 */
1882 		rblim = (struct  scsi_read_block_limits_data *)
1883 		    malloc(8192, M_TEMP, M_WAITOK);
1884 		if (rblim == NULL) {
1885 			xpt_print_path(ccb->ccb_h.path);
1886 			printf("no memory for test read\n");
1887 			xpt_release_ccb(ccb);
1888 			error = ENOMEM;
1889 			goto exit;
1890 		}
1891 
1892 		if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1893 			scsi_sa_read_write(&ccb->csio, 0, sadone,
1894 			    MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1895 			    (void *) rblim, 8192, SSD_FULL_SIZE,
1896 			    120 * 60 * 1000);
1897 			(void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1898 			    &softc->device_stats);
1899 			QFRLS(ccb);
1900 			scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1901 			    FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1902 			error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1903 			    SF_NO_PRINT | SF_RETRY_UA,
1904 			    &softc->device_stats);
1905 			QFRLS(ccb);
1906 			if (error) {
1907 				xpt_print_path(ccb->ccb_h.path);
1908 				printf("unable to rewind after test read\n");
1909 				xpt_release_ccb(ccb);
1910 				goto exit;
1911 			}
1912 		}
1913 
1914 		/*
1915 		 * Next off, determine block limits.
1916 		 */
1917 		scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1918 		    rblim, SSD_FULL_SIZE, 5000);
1919 
1920 		error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1921 		    SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1922 
1923 		QFRLS(ccb);
1924 		xpt_release_ccb(ccb);
1925 
1926 		if (error != 0) {
1927 			/*
1928 			 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1929 			 * a MANDATORY command. Anyway- it doesn't matter-
1930 			 * we can proceed anyway.
1931 			 */
1932 			softc->blk_gran = 0;
1933 			softc->max_blk = ~0;
1934 			softc->min_blk = 0;
1935 		} else {
1936 			if (softc->scsi_rev >= SCSI_REV_SPC) {
1937 				softc->blk_gran = RBL_GRAN(rblim);
1938 			} else {
1939 				softc->blk_gran = 0;
1940 			}
1941 			/*
1942 			 * We take max_blk == min_blk to mean a default to
1943 			 * fixed mode- but note that whatever we get out of
1944 			 * sagetparams below will actually determine whether
1945 			 * we are actually *in* fixed mode.
1946 			 */
1947 			softc->max_blk = scsi_3btoul(rblim->maximum);
1948 			softc->min_blk = scsi_2btoul(rblim->minimum);
1949 
1950 
1951 		}
1952 		/*
1953 		 * Next, perform a mode sense to determine
1954 		 * current density, blocksize, compression etc.
1955 		 */
1956 		error = sagetparams(periph, SA_PARAM_ALL,
1957 				    &softc->media_blksize,
1958 				    &softc->media_density,
1959 				    &softc->media_numblks,
1960 				    &softc->buffer_mode, &write_protect,
1961 				    &softc->speed, &comp_supported,
1962 				    &comp_enabled, &softc->comp_algorithm,
1963 				    NULL);
1964 
1965 		if (error != 0) {
1966 			/*
1967 			 * We could work a little harder here. We could
1968 			 * adjust our attempts to get information. It
1969 			 * might be an ancient tape drive. If someone
1970 			 * nudges us, we'll do that.
1971 			 */
1972 			goto exit;
1973 		}
1974 
1975 		/*
1976 		 * If no quirk has determined that this is a device that is
1977 		 * preferred to be in fixed or variable mode, now is the time
1978 		 * to find out.
1979 	 	 */
1980 		if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
1981 			guessing = 1;
1982 			/*
1983 			 * This could be expensive to find out. Luckily we
1984 			 * only need to do this once. If we start out in
1985 			 * 'default' mode, try and set ourselves to one
1986 			 * of the densities that would determine a wad
1987 			 * of other stuff. Go from highest to lowest.
1988 			 */
1989 			if (softc->media_density == SCSI_DEFAULT_DENSITY) {
1990 				int i;
1991 				static u_int8_t ctry[] = {
1992 					SCSI_DENSITY_HALFINCH_PE,
1993 					SCSI_DENSITY_HALFINCH_6250C,
1994 					SCSI_DENSITY_HALFINCH_6250,
1995 					SCSI_DENSITY_HALFINCH_1600,
1996 					SCSI_DENSITY_HALFINCH_800,
1997 					SCSI_DENSITY_QIC_4GB,
1998 					SCSI_DENSITY_QIC_2GB,
1999 					SCSI_DENSITY_QIC_525_320,
2000 					SCSI_DENSITY_QIC_150,
2001 					SCSI_DENSITY_QIC_120,
2002 					SCSI_DENSITY_QIC_24,
2003 					SCSI_DENSITY_QIC_11_9TRK,
2004 					SCSI_DENSITY_QIC_11_4TRK,
2005 					SCSI_DENSITY_QIC_1320,
2006 					SCSI_DENSITY_QIC_3080,
2007 					0
2008 				};
2009 				for (i = 0; ctry[i]; i++) {
2010 					error = sasetparams(periph,
2011 					    SA_PARAM_DENSITY, 0, ctry[i],
2012 					    0, SF_NO_PRINT);
2013 					if (error == 0) {
2014 						softc->media_density = ctry[i];
2015 						break;
2016 					}
2017 				}
2018 			}
2019 			switch (softc->media_density) {
2020 			case SCSI_DENSITY_QIC_11_4TRK:
2021 			case SCSI_DENSITY_QIC_11_9TRK:
2022 			case SCSI_DENSITY_QIC_24:
2023 			case SCSI_DENSITY_QIC_120:
2024 			case SCSI_DENSITY_QIC_150:
2025 			case SCSI_DENSITY_QIC_525_320:
2026 			case SCSI_DENSITY_QIC_1320:
2027 			case SCSI_DENSITY_QIC_3080:
2028 				softc->quirks &= ~SA_QUIRK_2FM;
2029 				softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2030 				softc->last_media_blksize = 512;
2031 				break;
2032 			case SCSI_DENSITY_QIC_4GB:
2033 			case SCSI_DENSITY_QIC_2GB:
2034 				softc->quirks &= ~SA_QUIRK_2FM;
2035 				softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2036 				softc->last_media_blksize = 1024;
2037 				break;
2038 			default:
2039 				softc->last_media_blksize =
2040 				    softc->media_blksize;
2041 				softc->quirks |= SA_QUIRK_VARIABLE;
2042 				break;
2043 			}
2044 		}
2045 
2046 		/*
2047 		 * If no quirk has determined that this is a device that needs
2048 		 * to have 2 Filemarks at EOD, now is the time to find out.
2049 		 */
2050 
2051 		if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2052 			switch (softc->media_density) {
2053 			case SCSI_DENSITY_HALFINCH_800:
2054 			case SCSI_DENSITY_HALFINCH_1600:
2055 			case SCSI_DENSITY_HALFINCH_6250:
2056 			case SCSI_DENSITY_HALFINCH_6250C:
2057 			case SCSI_DENSITY_HALFINCH_PE:
2058 				softc->quirks &= ~SA_QUIRK_1FM;
2059 				softc->quirks |= SA_QUIRK_2FM;
2060 				break;
2061 			default:
2062 				break;
2063 			}
2064 		}
2065 
2066 		/*
2067 		 * Now validate that some info we got makes sense.
2068 		 */
2069 		if ((softc->max_blk < softc->media_blksize) ||
2070 		    (softc->min_blk > softc->media_blksize &&
2071 		    softc->media_blksize)) {
2072 			xpt_print_path(ccb->ccb_h.path);
2073 			printf("BLOCK LIMITS (%d..%d) could not match current "
2074 			    "block settings (%d)- adjusting\n", softc->min_blk,
2075 			    softc->max_blk, softc->media_blksize);
2076 			softc->max_blk = softc->min_blk =
2077 			    softc->media_blksize;
2078 		}
2079 
2080 		/*
2081 		 * Now put ourselves into the right frame of mind based
2082 		 * upon quirks...
2083 		 */
2084 tryagain:
2085 		/*
2086 		 * If we want to be in FIXED mode and our current blocksize
2087 		 * is not equal to our last blocksize (if nonzero), try and
2088 		 * set ourselves to this last blocksize (as the 'preferred'
2089 		 * block size).  The initial quirkmatch at registry sets the
2090 		 * initial 'last' blocksize. If, for whatever reason, this
2091 		 * 'last' blocksize is zero, set the blocksize to 512,
2092 		 * or min_blk if that's larger.
2093 		 */
2094 		if ((softc->quirks & SA_QUIRK_FIXED) &&
2095 		    (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2096 		    (softc->media_blksize != softc->last_media_blksize)) {
2097 			softc->media_blksize = softc->last_media_blksize;
2098 			if (softc->media_blksize == 0) {
2099 				softc->media_blksize = 512;
2100 				if (softc->media_blksize < softc->min_blk) {
2101 					softc->media_blksize = softc->min_blk;
2102 				}
2103 			}
2104 			error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2105 			    softc->media_blksize, 0, 0, SF_NO_PRINT);
2106 			if (error) {
2107 				xpt_print_path(ccb->ccb_h.path);
2108 				printf("unable to set fixed blocksize to %d\n",
2109 				     softc->media_blksize);
2110 				goto exit;
2111 			}
2112 		}
2113 
2114 		if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2115 		    (softc->media_blksize != 0)) {
2116 			softc->last_media_blksize = softc->media_blksize;
2117 			softc->media_blksize = 0;
2118 			error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2119 			    0, 0, 0, SF_NO_PRINT);
2120 			if (error) {
2121 				/*
2122 				 * If this fails and we were guessing, just
2123 				 * assume that we got it wrong and go try
2124 				 * fixed block mode. Don't even check against
2125 				 * density code at this point.
2126 				 */
2127 				if (guessing) {
2128 					softc->quirks &= ~SA_QUIRK_VARIABLE;
2129 					softc->quirks |= SA_QUIRK_FIXED;
2130 					if (softc->last_media_blksize == 0)
2131 						softc->last_media_blksize = 512;
2132 					goto tryagain;
2133 				}
2134 				xpt_print_path(ccb->ccb_h.path);
2135 				printf("unable to set variable blocksize\n");
2136 				goto exit;
2137 			}
2138 		}
2139 
2140 		/*
2141 		 * Now that we have the current block size,
2142 		 * set up some parameters for sastart's usage.
2143 		 */
2144 		if (softc->media_blksize) {
2145 			softc->flags |= SA_FLAG_FIXED;
2146 			if (powerof2(softc->media_blksize)) {
2147 				softc->blk_shift =
2148 				    ffs(softc->media_blksize) - 1;
2149 				softc->blk_mask = softc->media_blksize - 1;
2150 			} else {
2151 				softc->blk_mask = ~0;
2152 				softc->blk_shift = 0;
2153 			}
2154 		} else {
2155 			/*
2156 			 * The SCSI-3 spec allows 0 to mean "unspecified".
2157 			 * The SCSI-1 spec allows 0 to mean 'infinite'.
2158 			 *
2159 			 * Either works here.
2160 			 */
2161 			if (softc->max_blk == 0) {
2162 				softc->max_blk = ~0;
2163 			}
2164 			softc->blk_shift = 0;
2165 			if (softc->blk_gran != 0) {
2166 				softc->blk_mask = softc->blk_gran - 1;
2167 			} else {
2168 				softc->blk_mask = 0;
2169 			}
2170 		}
2171 
2172 		if (write_protect)
2173 			softc->flags |= SA_FLAG_TAPE_WP;
2174 
2175 		if (comp_supported) {
2176 			if (softc->saved_comp_algorithm == 0)
2177 				softc->saved_comp_algorithm =
2178 				    softc->comp_algorithm;
2179 			softc->flags |= SA_FLAG_COMP_SUPP;
2180 			if (comp_enabled)
2181 				softc->flags |= SA_FLAG_COMP_ENABLED;
2182 		} else
2183 			softc->flags |= SA_FLAG_COMP_UNSUPP;
2184 
2185 		if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2186 		    (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2187 			error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2188 			    0, 0, SF_NO_PRINT);
2189 			if (error == 0)
2190 				softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2191 			xpt_print_path(ccb->ccb_h.path);
2192 			printf("unable to set buffered mode\n");
2193 			error = 0;	/* not an error */
2194 		}
2195 
2196 
2197 		if (error == 0) {
2198 			softc->flags |= SA_FLAG_TAPE_MOUNTED;
2199 		}
2200 exit:
2201 		if (rblim != NULL)
2202 			free(rblim, M_TEMP);
2203 
2204 		if (error != 0) {
2205 			softc->dsreg = MTIO_DSREG_NIL;
2206 		} else {
2207 			softc->fileno = softc->blkno = 0;
2208 			softc->dsreg = MTIO_DSREG_REST;
2209 		}
2210 #ifdef	SA_1FM_AT_EOD
2211 		if ((softc->quirks & SA_QUIRK_2FM) == 0)
2212 			softc->quirks |= SA_QUIRK_1FM;
2213 #else
2214 		if ((softc->quirks & SA_QUIRK_1FM) == 0)
2215 			softc->quirks |= SA_QUIRK_2FM;
2216 #endif
2217 	} else
2218 		xpt_release_ccb(ccb);
2219 
2220 	/*
2221 	 * If we return an error, we're not mounted any more,
2222 	 * so release any device reservation.
2223 	 */
2224 	if (error != 0) {
2225 		(void) sareservereleaseunit(periph, FALSE);
2226 	}
2227 	return (error);
2228 }
2229 
2230 /*
2231  * How many filemarks do we need to write if we were to terminate the
2232  * tape session right now? Note that this can be a negative number
2233  */
2234 
2235 static int
2236 samarkswanted(struct cam_periph *periph)
2237 {
2238 	int	markswanted;
2239 	struct	sa_softc *softc;
2240 
2241 	softc = (struct sa_softc *)periph->softc;
2242 	markswanted = 0;
2243 	if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2244 		markswanted++;
2245 		if (softc->quirks & SA_QUIRK_2FM)
2246 			markswanted++;
2247 	}
2248 	markswanted -= softc->filemarks;
2249 	return (markswanted);
2250 }
2251 
2252 static int
2253 sacheckeod(struct cam_periph *periph)
2254 {
2255 	int	error;
2256 	int	markswanted;
2257 	struct	sa_softc *softc;
2258 
2259 	softc = (struct sa_softc *)periph->softc;
2260 	markswanted = samarkswanted(periph);
2261 
2262 	if (markswanted > 0) {
2263 		error = sawritefilemarks(periph, markswanted, FALSE);
2264 	} else {
2265 		error = 0;
2266 	}
2267 	return (error);
2268 }
2269 
2270 static int
2271 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2272 {
2273 	static const char *toobig =
2274 	    "%d-byte tape record bigger than suplied buffer\n";
2275 	struct	cam_periph *periph;
2276 	struct	sa_softc *softc;
2277 	struct	ccb_scsiio *csio;
2278 	struct	scsi_sense_data *sense;
2279 	u_int32_t resid = 0;
2280 	int32_t	info = 0;
2281 	int	error_code, sense_key, asc, ascq;
2282 	int	error, defer_action, no_actual_error = FALSE;
2283 
2284 	periph = xpt_path_periph(ccb->ccb_h.path);
2285 	softc = (struct sa_softc *)periph->softc;
2286 	csio = &ccb->csio;
2287 	sense = &csio->sense_data;
2288 	scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2289 	error = 0;
2290 
2291 	/*
2292 	 * Calculate/latch up, any residuals...
2293 	 */
2294 	if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR) {
2295 		if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2296 			info = (int32_t) scsi_4btoul(sense->info);
2297 			resid = info;
2298 			if ((softc->flags & SA_FLAG_FIXED) != 0)
2299 				resid *= softc->media_blksize;
2300 		} else {
2301 			resid = csio->dxfer_len;
2302 			info = resid;
2303 			if ((softc->flags & SA_FLAG_FIXED) != 0) {
2304 				if (softc->media_blksize)
2305 					info /= softc->media_blksize;
2306 			}
2307 		}
2308 		if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2309 			bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2310 			    sizeof (struct scsi_sense_data));
2311 			bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2312 			    (int) csio->cdb_len);
2313 			softc->last_io_resid = resid;
2314 			softc->last_resid_was_io = 1;
2315 		} else {
2316 			bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2317 			    sizeof (struct scsi_sense_data));
2318 			bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2319 			    (int) csio->cdb_len);
2320 			softc->last_ctl_resid = resid;
2321 			softc->last_resid_was_io = 0;
2322 		}
2323 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Key 0x%x ASC/ASCQ "
2324 		    "0x%x 0x%x flags 0x%x resid %d dxfer_len %d\n", sense_key,
2325 		    asc, ascq, sense->flags & ~SSD_KEY_RESERVED, resid,
2326 		    csio->dxfer_len));
2327 	} else {
2328 		CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Cam Status 0x%x\n",
2329 		    csio->ccb_h.status & CAM_STATUS_MASK));
2330 	}
2331 
2332 	/*
2333 	 * If it's neither a SCSI Check Condition Error nor a non-read/write
2334 	 * command, let the common code deal with it the error setting.
2335 	 */
2336 	if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_SCSI_STATUS_ERROR ||
2337 	    (CCB_Type(csio) == SA_CCB_WAITING)) {
2338 		return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2339 	}
2340 
2341 	/*
2342 	 * Calculate whether we'll defer action.
2343 	 */
2344 
2345 	if (resid > 0 && resid < csio->dxfer_len &&
2346 	    (softc->flags & SA_FLAG_FIXED) != 0) {
2347 		defer_action = TRUE;
2348 	} else {
2349 		defer_action = FALSE;
2350 	}
2351 
2352 	/*
2353 	 * Handle filemark, end of tape, mismatched record sizes....
2354 	 * From this point out, we're only handling read/write cases.
2355 	 * Handle writes && reads differently.
2356 	 */
2357 
2358 	if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2359 		if (sense->flags & SSD_FILEMARK) {
2360 			xpt_print_path(csio->ccb_h.path);
2361 			printf("filemark detected on write?\n");
2362 			if (softc->fileno != (daddr_t) -1) {
2363 				softc->fileno++;
2364 				softc->blkno = 0;
2365 				csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2366 			}
2367 		}
2368 		if (sense->flags & SSD_EOM) {
2369 			csio->resid = resid;
2370 			if (defer_action) {
2371 				error = -1;
2372 				softc->flags |= SA_FLAG_EOM_PENDING;
2373 			} else {
2374 				error = ENOSPC;
2375 			}
2376 		}
2377 	} else {
2378 		if (sense_key == SSD_KEY_BLANK_CHECK) {
2379 			csio->resid = resid;
2380 			if (defer_action) {
2381 				error = -1;
2382 				softc->flags |= SA_FLAG_EOM_PENDING;
2383 			} else {
2384 				error = EIO;
2385 			}
2386 		}
2387 		if (sense->flags & SSD_FILEMARK) {
2388 			csio->resid = resid;
2389 			if (defer_action) {
2390 				error = -1;
2391 				softc->flags |= SA_FLAG_EOF_PENDING;
2392 			} else {
2393 				no_actual_error = TRUE;
2394 			}
2395 			/*
2396 			 * Unconditionally, if we detected a filemark on a read,
2397 			 * mark that we've run moved a file ahead.
2398 			 */
2399 			if (softc->fileno != (daddr_t) -1) {
2400 				softc->fileno++;
2401 				softc->blkno = 0;
2402 				csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2403 			}
2404 		}
2405 	}
2406 	/*
2407 	 * Incorrect Length usually applies to read, but can apply to writes.
2408 	 */
2409 	if (error == 0 && (sense->flags & SSD_ILI)) {
2410 		if (info < 0) {
2411 			xpt_print_path(csio->ccb_h.path);
2412 			printf(toobig, csio->dxfer_len - info);
2413 			csio->resid = csio->dxfer_len;
2414 			error = EIO;
2415 		} else {
2416 			csio->resid = resid;
2417 			if ((softc->flags & SA_FLAG_FIXED) != 0) {
2418 				if (defer_action)
2419 					softc->flags |= SA_FLAG_EIO_PENDING;
2420 				else
2421 					error = EIO;
2422 			} else {
2423 				no_actual_error = TRUE;
2424 			}
2425 			/*
2426 			 * Bump the block number if we hadn't seen a filemark.
2427 			 * Do this independent of errors (we've moved anyway).
2428 			 */
2429 			if ((sense->flags & SSD_FILEMARK) == 0) {
2430 				if (softc->blkno != (daddr_t) -1) {
2431 					softc->blkno++;
2432 					csio->ccb_h.ccb_pflags |=
2433 					   SA_POSITION_UPDATED;
2434 				}
2435 			}
2436 		}
2437 	}
2438 	if (error == 0 && !no_actual_error)
2439 		return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2440 	if (no_actual_error) {
2441 		if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2442 			cam_release_devq(ccb->ccb_h.path,
2443 					 /* relsim_flags */0,
2444 					 /* openings */0,
2445 					 /* timeout */0,
2446 					 /* getcount_only */ FALSE);
2447 		return (0);
2448 	}
2449 
2450 	if (error == -1)
2451 		return (0);
2452 	else
2453 		return (error);
2454 }
2455 
2456 static int
2457 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2458 	    u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2459 	    int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2460 	    int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2461 	    sa_comp_t *tcs)
2462 {
2463 	union ccb *ccb;
2464 	void *mode_buffer;
2465 	struct scsi_mode_header_6 *mode_hdr;
2466 	struct scsi_mode_blk_desc *mode_blk;
2467 	int mode_buffer_len;
2468 	struct sa_softc *softc;
2469 	u_int8_t cpage;
2470 	int error;
2471 	cam_status status;
2472 
2473 	softc = (struct sa_softc *)periph->softc;
2474 	ccb = cam_periph_getccb(periph, 1);
2475 	if (softc->quirks & SA_QUIRK_NO_CPAGE)
2476 		cpage = SA_DEVICE_CONFIGURATION_PAGE;
2477 	else
2478 		cpage = SA_DATA_COMPRESSION_PAGE;
2479 
2480 retry:
2481 	mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2482 
2483 	if (params_to_get & SA_PARAM_COMPRESSION) {
2484 		if (softc->quirks & SA_QUIRK_NOCOMP) {
2485 			*comp_supported = FALSE;
2486 			params_to_get &= ~SA_PARAM_COMPRESSION;
2487 		} else
2488 			mode_buffer_len += sizeof (sa_comp_t);
2489 	}
2490 
2491 	mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK | M_ZERO);
2492 	mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2493 	mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2494 
2495 	/* it is safe to retry this */
2496 	scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2497 	    SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2498 	    cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2499 	    SSD_FULL_SIZE, 5000);
2500 
2501 	error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2502 	    &softc->device_stats);
2503 	QFRLS(ccb);
2504 
2505 	status = ccb->ccb_h.status & CAM_STATUS_MASK;
2506 
2507 	if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2508 		/*
2509 		 * Hmm. Let's see if we can try another page...
2510 		 * If we've already done that, give up on compression
2511 		 * for this device and remember this for the future
2512 		 * and attempt the request without asking for compression
2513 		 * info.
2514 		 */
2515 		if (cpage == SA_DATA_COMPRESSION_PAGE) {
2516 			cpage = SA_DEVICE_CONFIGURATION_PAGE;
2517 			goto retry;
2518 		}
2519 		softc->quirks |= SA_QUIRK_NOCOMP;
2520 		free(mode_buffer, M_TEMP);
2521 		goto retry;
2522 	} else if (status == CAM_SCSI_STATUS_ERROR) {
2523 		/* Tell the user about the fatal error. */
2524 		scsi_sense_print(&ccb->csio);
2525 		goto sagetparamsexit;
2526 	}
2527 
2528 	/*
2529 	 * If the user only wants the compression information, and
2530 	 * the device doesn't send back the block descriptor, it's
2531 	 * no big deal.  If the user wants more than just
2532 	 * compression, though, and the device doesn't pass back the
2533 	 * block descriptor, we need to send another mode sense to
2534 	 * get the block descriptor.
2535 	 */
2536 	if ((mode_hdr->blk_desc_len == 0) &&
2537 	    (params_to_get & SA_PARAM_COMPRESSION) &&
2538 	    (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2539 
2540 		/*
2541 		 * Decrease the mode buffer length by the size of
2542 		 * the compression page, to make sure the data
2543 		 * there doesn't get overwritten.
2544 		 */
2545 		mode_buffer_len -= sizeof (sa_comp_t);
2546 
2547 		/*
2548 		 * Now move the compression page that we presumably
2549 		 * got back down the memory chunk a little bit so
2550 		 * it doesn't get spammed.
2551 		 */
2552 		bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2553 		bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2554 
2555 		/*
2556 		 * Now, we issue another mode sense and just ask
2557 		 * for the block descriptor, etc.
2558 		 */
2559 
2560 		scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2561 		    SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2562 		    mode_buffer, mode_buffer_len, SSD_FULL_SIZE, 5000);
2563 
2564 		error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2565 		    &softc->device_stats);
2566 		QFRLS(ccb);
2567 
2568 		if (error != 0)
2569 			goto sagetparamsexit;
2570 	}
2571 
2572 	if (params_to_get & SA_PARAM_BLOCKSIZE)
2573 		*blocksize = scsi_3btoul(mode_blk->blklen);
2574 
2575 	if (params_to_get & SA_PARAM_NUMBLOCKS)
2576 		*numblocks = scsi_3btoul(mode_blk->nblocks);
2577 
2578 	if (params_to_get & SA_PARAM_BUFF_MODE)
2579 		*buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2580 
2581 	if (params_to_get & SA_PARAM_DENSITY)
2582 		*density = mode_blk->density;
2583 
2584 	if (params_to_get & SA_PARAM_WP)
2585 		*write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2586 
2587 	if (params_to_get & SA_PARAM_SPEED)
2588 		*speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2589 
2590 	if (params_to_get & SA_PARAM_COMPRESSION) {
2591 		sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2592 		if (cpage == SA_DATA_COMPRESSION_PAGE) {
2593 			struct scsi_data_compression_page *cp = &ntcs->dcomp;
2594 			*comp_supported =
2595 			    (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2596 			*comp_enabled =
2597 			    (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2598 			*comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2599 		} else {
2600 			struct scsi_dev_conf_page *cp = &ntcs->dconf;
2601 			/*
2602 			 * We don't really know whether this device supports
2603 			 * Data Compression if the the algorithm field is
2604 			 * zero. Just say we do.
2605 			 */
2606 			*comp_supported = TRUE;
2607 			*comp_enabled =
2608 			    (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2609 			*comp_algorithm = cp->sel_comp_alg;
2610 		}
2611 		if (tcs != NULL)
2612 			bcopy(ntcs, tcs, sizeof (sa_comp_t));
2613 	}
2614 
2615 	if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2616 		int idx;
2617 		char *xyz = mode_buffer;
2618 		xpt_print_path(periph->path);
2619 		printf("Mode Sense Data=");
2620 		for (idx = 0; idx < mode_buffer_len; idx++)
2621 			printf(" 0x%02x", xyz[idx] & 0xff);
2622 		printf("\n");
2623 	}
2624 
2625 sagetparamsexit:
2626 
2627 	xpt_release_ccb(ccb);
2628 	free(mode_buffer, M_TEMP);
2629 	return (error);
2630 }
2631 
2632 /*
2633  * The purpose of this function is to set one of four different parameters
2634  * for a tape drive:
2635  *	- blocksize
2636  *	- density
2637  *	- compression / compression algorithm
2638  *	- buffering mode
2639  *
2640  * The assumption is that this will be called from saioctl(), and therefore
2641  * from a process context.  Thus the waiting malloc calls below.  If that
2642  * assumption ever changes, the malloc calls should be changed to be
2643  * NOWAIT mallocs.
2644  *
2645  * Any or all of the four parameters may be set when this function is
2646  * called.  It should handle setting more than one parameter at once.
2647  */
2648 static int
2649 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2650 	    u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2651 	    u_int32_t sense_flags)
2652 {
2653 	struct sa_softc *softc;
2654 	u_int32_t current_blocksize;
2655 	u_int32_t current_calg;
2656 	u_int8_t current_density;
2657 	u_int8_t current_speed;
2658 	int comp_enabled, comp_supported;
2659 	void *mode_buffer;
2660 	int mode_buffer_len;
2661 	struct scsi_mode_header_6 *mode_hdr;
2662 	struct scsi_mode_blk_desc *mode_blk;
2663 	sa_comp_t *ccomp, *cpage;
2664 	int buff_mode;
2665 	union ccb *ccb = NULL;
2666 	int error;
2667 
2668 	softc = (struct sa_softc *)periph->softc;
2669 
2670 	ccomp = malloc(sizeof (sa_comp_t), M_TEMP, M_WAITOK);
2671 
2672 	/*
2673 	 * Since it doesn't make sense to set the number of blocks, or
2674 	 * write protection, we won't try to get the current value.  We
2675 	 * always want to get the blocksize, so we can set it back to the
2676 	 * proper value.
2677 	 */
2678 	error = sagetparams(periph,
2679 	    params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2680 	    &current_blocksize, &current_density, NULL, &buff_mode, NULL,
2681 	    &current_speed, &comp_supported, &comp_enabled,
2682 	    &current_calg, ccomp);
2683 
2684 	if (error != 0) {
2685 		free(ccomp, M_TEMP);
2686 		return (error);
2687 	}
2688 
2689 	mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2690 	if (params_to_set & SA_PARAM_COMPRESSION)
2691 		mode_buffer_len += sizeof (sa_comp_t);
2692 
2693 	mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK | M_ZERO);
2694 
2695 	mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2696 	mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2697 
2698 	ccb = cam_periph_getccb(periph, 1);
2699 
2700 retry:
2701 
2702 	if (params_to_set & SA_PARAM_COMPRESSION) {
2703 		if (mode_blk) {
2704 			cpage = (sa_comp_t *)&mode_blk[1];
2705 		} else {
2706 			cpage = (sa_comp_t *)&mode_hdr[1];
2707 		}
2708 		bcopy(ccomp, cpage, sizeof (sa_comp_t));
2709 		cpage->hdr.pagecode &= ~0x80;
2710 	} else
2711 		cpage = NULL;
2712 
2713 	/*
2714 	 * If the caller wants us to set the blocksize, use the one they
2715 	 * pass in.  Otherwise, use the blocksize we got back from the
2716 	 * mode select above.
2717 	 */
2718 	if (mode_blk) {
2719 		if (params_to_set & SA_PARAM_BLOCKSIZE)
2720 			scsi_ulto3b(blocksize, mode_blk->blklen);
2721 		else
2722 			scsi_ulto3b(current_blocksize, mode_blk->blklen);
2723 
2724 		/*
2725 		 * Set density if requested, else preserve old density.
2726 		 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2727 		 * devices, else density we've latched up in our softc.
2728 		 */
2729 		if (params_to_set & SA_PARAM_DENSITY) {
2730 			mode_blk->density = density;
2731 		} else if (softc->scsi_rev > SCSI_REV_CCS) {
2732 			mode_blk->density = SCSI_SAME_DENSITY;
2733 		} else {
2734 			mode_blk->density = softc->media_density;
2735 		}
2736 	}
2737 
2738 	/*
2739 	 * For mode selects, these two fields must be zero.
2740 	 */
2741 	mode_hdr->data_length = 0;
2742 	mode_hdr->medium_type = 0;
2743 
2744 	/* set the speed to the current value */
2745 	mode_hdr->dev_spec = current_speed;
2746 
2747 	/* set single-initiator buffering mode */
2748 	mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2749 
2750 	if (mode_blk)
2751 		mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2752 	else
2753 		mode_hdr->blk_desc_len = 0;
2754 
2755 	/*
2756 	 * First, if the user wants us to set the compression algorithm or
2757 	 * just turn compression on, check to make sure that this drive
2758 	 * supports compression.
2759 	 */
2760 	if (params_to_set & SA_PARAM_COMPRESSION) {
2761 		/*
2762 		 * If the compression algorithm is 0, disable compression.
2763 		 * If the compression algorithm is non-zero, enable
2764 		 * compression and set the compression type to the
2765 		 * specified compression algorithm, unless the algorithm is
2766 		 * MT_COMP_ENABLE.  In that case, we look at the
2767 		 * compression algorithm that is currently set and if it is
2768 		 * non-zero, we leave it as-is.  If it is zero, and we have
2769 		 * saved a compression algorithm from a time when
2770 		 * compression was enabled before, set the compression to
2771 		 * the saved value.
2772 		 */
2773 		switch (ccomp->hdr.pagecode & ~0x80) {
2774 		case SA_DATA_COMPRESSION_PAGE:
2775 		if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2776 			struct scsi_data_compression_page *dcp = &cpage->dcomp;
2777 			if (calg == 0) {
2778 				/*
2779 				 * Disable compression, but leave the
2780 				 * decompression and the capability bit
2781 				 * alone.
2782 				 */
2783 				dcp->dce_and_dcc = SA_DCP_DCC;
2784 				dcp->dde_and_red |= SA_DCP_DDE;
2785 				break;
2786 			}
2787 			/* enable compression && decompression */
2788 			dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2789 			dcp->dde_and_red |= SA_DCP_DDE;
2790 			/*
2791 			 * If there, use compression algorithm from caller.
2792 			 * Otherwise, if there's a saved compression algorithm
2793 			 * and there is no current algorithm, use the saved
2794 			 * algorithm. Else parrot back what we got and hope
2795 			 * for the best.
2796 			 */
2797 			if (calg != MT_COMP_ENABLE) {
2798 				scsi_ulto4b(calg, dcp->comp_algorithm);
2799 				scsi_ulto4b(calg, dcp->decomp_algorithm);
2800 			} else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2801 			    softc->saved_comp_algorithm != 0) {
2802 				scsi_ulto4b(softc->saved_comp_algorithm,
2803 				    dcp->comp_algorithm);
2804 				scsi_ulto4b(softc->saved_comp_algorithm,
2805 				    dcp->decomp_algorithm);
2806 			}
2807 			break;
2808 		}
2809 		case SA_DEVICE_CONFIGURATION_PAGE:
2810 		{
2811 			struct scsi_dev_conf_page *dcp = &cpage->dconf;
2812 			if (calg == 0) {
2813 				dcp->sel_comp_alg = SA_COMP_NONE;
2814 				break;
2815 			}
2816 			if (calg != MT_COMP_ENABLE) {
2817 				dcp->sel_comp_alg = calg;
2818 			} else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2819 			    softc->saved_comp_algorithm != 0) {
2820 				dcp->sel_comp_alg = softc->saved_comp_algorithm;
2821 			}
2822 			break;
2823 		}
2824 		default:
2825 			/*
2826 			 * The drive doesn't seem to support compression,
2827 			 * so turn off the set compression bit.
2828 			 */
2829 			params_to_set &= ~SA_PARAM_COMPRESSION;
2830 			xpt_print_path(periph->path);
2831 			printf("device does not seem to support compression\n");
2832 
2833 			/*
2834 			 * If that was the only thing the user wanted us to set,
2835 			 * clean up allocated resources and return with
2836 			 * 'operation not supported'.
2837 			 */
2838 			if (params_to_set == SA_PARAM_NONE) {
2839 				free(mode_buffer, M_TEMP);
2840 				xpt_release_ccb(ccb);
2841 				return (ENODEV);
2842 			}
2843 
2844 			/*
2845 			 * That wasn't the only thing the user wanted us to set.
2846 			 * So, decrease the stated mode buffer length by the
2847 			 * size of the compression mode page.
2848 			 */
2849 			mode_buffer_len -= sizeof(sa_comp_t);
2850 		}
2851 	}
2852 
2853 	/* It is safe to retry this operation */
2854 	scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2855 	    (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2856 	    FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, 5000);
2857 
2858 	error = cam_periph_runccb(ccb, saerror, 0,
2859 	    sense_flags, &softc->device_stats);
2860 	QFRLS(ccb);
2861 
2862 	if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2863 		int idx;
2864 		char *xyz = mode_buffer;
2865 		xpt_print_path(periph->path);
2866 		printf("Err%d, Mode Select Data=", error);
2867 		for (idx = 0; idx < mode_buffer_len; idx++)
2868 			printf(" 0x%02x", xyz[idx] & 0xff);
2869 		printf("\n");
2870 	}
2871 
2872 
2873 	if (error) {
2874 		/*
2875 		 * If we can, try without setting density/blocksize.
2876 		 */
2877 		if (mode_blk) {
2878 			if ((params_to_set &
2879 			    (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2880 				mode_blk = NULL;
2881 				goto retry;
2882 			}
2883 		} else {
2884 			mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2885 			cpage = (sa_comp_t *)&mode_blk[1];
2886 		}
2887 
2888 		/*
2889 		 * If we were setting the blocksize, and that failed, we
2890 		 * want to set it to its original value.  If we weren't
2891 		 * setting the blocksize, we don't want to change it.
2892 		 */
2893 		scsi_ulto3b(current_blocksize, mode_blk->blklen);
2894 
2895 		/*
2896 		 * Set density if requested, else preserve old density.
2897 		 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2898 		 * devices, else density we've latched up in our softc.
2899 		 */
2900 		if (params_to_set & SA_PARAM_DENSITY) {
2901 			mode_blk->density = current_density;
2902 		} else if (softc->scsi_rev > SCSI_REV_CCS) {
2903 			mode_blk->density = SCSI_SAME_DENSITY;
2904 		} else {
2905 			mode_blk->density = softc->media_density;
2906 		}
2907 
2908 		if (params_to_set & SA_PARAM_COMPRESSION)
2909 			bcopy(ccomp, cpage, sizeof (sa_comp_t));
2910 
2911 		/*
2912 		 * The retry count is the only CCB field that might have been
2913 		 * changed that we care about, so reset it back to 1.
2914 		 */
2915 		ccb->ccb_h.retry_count = 1;
2916 		cam_periph_runccb(ccb, saerror, 0, sense_flags,
2917 		    &softc->device_stats);
2918 		QFRLS(ccb);
2919 	}
2920 
2921 	xpt_release_ccb(ccb);
2922 
2923 	if (ccomp != NULL)
2924 		free(ccomp, M_TEMP);
2925 
2926 	if (params_to_set & SA_PARAM_COMPRESSION) {
2927 		if (error) {
2928 			softc->flags &= ~SA_FLAG_COMP_ENABLED;
2929 			/*
2930 			 * Even if we get an error setting compression,
2931 			 * do not say that we don't support it. We could
2932 			 * have been wrong, or it may be media specific.
2933 			 *	softc->flags &= ~SA_FLAG_COMP_SUPP;
2934 			 */
2935 			softc->saved_comp_algorithm = softc->comp_algorithm;
2936 			softc->comp_algorithm = 0;
2937 		} else {
2938 			softc->flags |= SA_FLAG_COMP_ENABLED;
2939 			softc->comp_algorithm = calg;
2940 		}
2941 	}
2942 
2943 	free(mode_buffer, M_TEMP);
2944 	return (error);
2945 }
2946 
2947 static void
2948 saprevent(struct cam_periph *periph, int action)
2949 {
2950 	struct	sa_softc *softc;
2951 	union	ccb *ccb;
2952 	int	error, sf;
2953 
2954 	softc = (struct sa_softc *)periph->softc;
2955 
2956 	if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
2957 		return;
2958 	if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
2959 		return;
2960 
2961 	/*
2962 	 * We can be quiet about illegal requests.
2963 	 */
2964 	if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2965 		sf = 0;
2966 	} else
2967 		sf = SF_QUIET_IR;
2968 
2969 	ccb = cam_periph_getccb(periph, 1);
2970 
2971 	/* It is safe to retry this operation */
2972 	scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
2973 	    SSD_FULL_SIZE, 100000);
2974 
2975 	error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
2976 	QFRLS(ccb);
2977 	if (error == 0) {
2978 		if (action == PR_ALLOW)
2979 			softc->flags &= ~SA_FLAG_TAPE_LOCKED;
2980 		else
2981 			softc->flags |= SA_FLAG_TAPE_LOCKED;
2982 	}
2983 
2984 	xpt_release_ccb(ccb);
2985 }
2986 
2987 static int
2988 sarewind(struct cam_periph *periph)
2989 {
2990 	union	ccb *ccb;
2991 	struct	sa_softc *softc;
2992 	int	error;
2993 
2994 	softc = (struct sa_softc *)periph->softc;
2995 
2996 	ccb = cam_periph_getccb(periph, 1);
2997 
2998 	/* It is safe to retry this operation */
2999 	scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3000 	    SSD_FULL_SIZE, REWIND_TIMEOUT);
3001 
3002 	softc->dsreg = MTIO_DSREG_REW;
3003 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3004 	softc->dsreg = MTIO_DSREG_REST;
3005 
3006 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3007 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3008 
3009 	xpt_release_ccb(ccb);
3010 	if (error == 0)
3011 		softc->fileno = softc->blkno = (daddr_t) 0;
3012 	else
3013 		softc->fileno = softc->blkno = (daddr_t) -1;
3014 	return (error);
3015 }
3016 
3017 static int
3018 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3019 {
3020 	union	ccb *ccb;
3021 	struct	sa_softc *softc;
3022 	int	error;
3023 
3024 	softc = (struct sa_softc *)periph->softc;
3025 
3026 	ccb = cam_periph_getccb(periph, 1);
3027 
3028 	/* This cannot be retried */
3029 
3030 	scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3031 	    SSD_FULL_SIZE, SPACE_TIMEOUT);
3032 
3033 	/*
3034 	 * Clear residual because we will be using it.
3035 	 */
3036 	softc->last_ctl_resid = 0;
3037 
3038 	softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3039 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3040 	softc->dsreg = MTIO_DSREG_REST;
3041 
3042 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3043 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3044 
3045 	xpt_release_ccb(ccb);
3046 
3047 	/*
3048 	 * If a spacing operation has failed, we need to invalidate
3049 	 * this mount.
3050 	 *
3051 	 * If the spacing operation was setmarks or to end of recorded data,
3052 	 * we no longer know our relative position.
3053 	 *
3054 	 * If the spacing operations was spacing files in reverse, we
3055 	 * take account of the residual, but still check against less
3056 	 * than zero- if we've gone negative, we must have hit BOT.
3057 	 *
3058 	 * If the spacing operations was spacing records in reverse and
3059 	 * we have a residual, we've either hit BOT or hit a filemark.
3060 	 * In the former case, we know our new record number (0). In
3061 	 * the latter case, we have absolutely no idea what the real
3062 	 * record number is- we've stopped between the end of the last
3063 	 * record in the previous file and the filemark that stopped
3064 	 * our spacing backwards.
3065 	 */
3066 	if (error) {
3067 		softc->fileno = softc->blkno = (daddr_t) -1;
3068 	} else if (code == SS_SETMARKS || code == SS_EOD) {
3069 		softc->fileno = softc->blkno = (daddr_t) -1;
3070 	} else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3071 		softc->fileno += (count - softc->last_ctl_resid);
3072 		if (softc->fileno < 0)	/* we must of hit BOT */
3073 			softc->fileno = 0;
3074 		softc->blkno = 0;
3075 	} else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3076 		softc->blkno += (count - softc->last_ctl_resid);
3077 		if (count < 0) {
3078 			if (softc->last_ctl_resid || softc->blkno < 0) {
3079 				if (softc->fileno == 0) {
3080 					softc->blkno = 0;
3081 				} else {
3082 					softc->blkno = (daddr_t) -1;
3083 				}
3084 			}
3085 		}
3086 	}
3087 	return (error);
3088 }
3089 
3090 static int
3091 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3092 {
3093 	union	ccb *ccb;
3094 	struct	sa_softc *softc;
3095 	int	error, nwm = 0;
3096 
3097 	softc = (struct sa_softc *)periph->softc;
3098 
3099 	ccb = cam_periph_getccb(periph, 1);
3100 	/*
3101 	 * Clear residual because we will be using it.
3102 	 */
3103 	softc->last_ctl_resid = 0;
3104 
3105 	softc->dsreg = MTIO_DSREG_FMK;
3106 	/* this *must* not be retried */
3107 	scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3108 	    FALSE, setmarks, nmarks, SSD_FULL_SIZE, 60000);
3109 	softc->dsreg = MTIO_DSREG_REST;
3110 
3111 
3112 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3113 
3114 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3115 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3116 
3117 	if (error == 0 && nmarks) {
3118 		struct sa_softc *softc = (struct sa_softc *)periph->softc;
3119 		nwm = nmarks - softc->last_ctl_resid;
3120 		softc->filemarks += nwm;
3121 	}
3122 
3123 	xpt_release_ccb(ccb);
3124 
3125 	/*
3126 	 * Update relative positions (if we're doing that).
3127 	 */
3128 	if (error) {
3129 		softc->fileno = softc->blkno = (daddr_t) -1;
3130 	} else if (softc->fileno != (daddr_t) -1) {
3131 		softc->fileno += nwm;
3132 		softc->blkno = 0;
3133 	}
3134 	return (error);
3135 }
3136 
3137 static int
3138 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3139 {
3140 	struct scsi_tape_position_data loc;
3141 	union ccb *ccb;
3142 	struct sa_softc *softc = (struct sa_softc *)periph->softc;
3143 	int error;
3144 
3145 	/*
3146 	 * We try and flush any buffered writes here if we were writing
3147 	 * and we're trying to get hardware block position. It eats
3148 	 * up performance substantially, but I'm wary of drive firmware.
3149 	 *
3150 	 * I think that *logical* block position is probably okay-
3151 	 * but hardware block position might have to wait for data
3152 	 * to hit media to be valid. Caveat Emptor.
3153 	 */
3154 
3155 	if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3156 		error = sawritefilemarks(periph, 0, 0);
3157 		if (error && error != EACCES)
3158 			return (error);
3159 	}
3160 
3161 	ccb = cam_periph_getccb(periph, 1);
3162 	scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3163 	    hard, &loc, SSD_FULL_SIZE, 5000);
3164 	softc->dsreg = MTIO_DSREG_RBSY;
3165 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3166 	softc->dsreg = MTIO_DSREG_REST;
3167 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3168 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3169 
3170 	if (error == 0) {
3171 		if (loc.flags & SA_RPOS_UNCERTAIN) {
3172 			error = EINVAL;		/* nothing is certain */
3173 		} else {
3174 			*blkptr = scsi_4btoul(loc.firstblk);
3175 		}
3176 	}
3177 
3178 	xpt_release_ccb(ccb);
3179 	return (error);
3180 }
3181 
3182 static int
3183 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3184 {
3185 	union ccb *ccb;
3186 	struct sa_softc *softc;
3187 	int error;
3188 
3189 	/*
3190 	 * We used to try and flush any buffered writes here.
3191 	 * Now we push this onto user applications to either
3192 	 * flush the pending writes themselves (via a zero count
3193 	 * WRITE FILEMARKS command) or they can trust their tape
3194 	 * drive to do this correctly for them.
3195  	 */
3196 
3197 	softc = (struct sa_softc *)periph->softc;
3198 	ccb = cam_periph_getccb(periph, 1);
3199 
3200 
3201 	scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3202 	    hard, *blkptr, SSD_FULL_SIZE, 60 * 60 * 1000);
3203 
3204 	softc->dsreg = MTIO_DSREG_POS;
3205 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3206 	softc->dsreg = MTIO_DSREG_REST;
3207 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3208 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3209 	xpt_release_ccb(ccb);
3210 	/*
3211 	 * Note relative file && block number position as now unknown.
3212 	 */
3213 	softc->fileno = softc->blkno = (daddr_t) -1;
3214 	return (error);
3215 }
3216 
3217 static int
3218 saretension(struct cam_periph *periph)
3219 {
3220 	union ccb *ccb;
3221 	struct sa_softc *softc;
3222 	int error;
3223 
3224 	softc = (struct sa_softc *)periph->softc;
3225 
3226 	ccb = cam_periph_getccb(periph, 1);
3227 
3228 	/* It is safe to retry this operation */
3229 	scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3230 	    FALSE, TRUE,  TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3231 
3232 	softc->dsreg = MTIO_DSREG_TEN;
3233 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3234 	softc->dsreg = MTIO_DSREG_REST;
3235 
3236 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3237 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3238 	xpt_release_ccb(ccb);
3239 	if (error == 0)
3240 		softc->fileno = softc->blkno = (daddr_t) 0;
3241 	else
3242 		softc->fileno = softc->blkno = (daddr_t) -1;
3243 	return (error);
3244 }
3245 
3246 static int
3247 sareservereleaseunit(struct cam_periph *periph, int reserve)
3248 {
3249 	union ccb *ccb;
3250 	struct sa_softc *softc;
3251 	int error;
3252 
3253 	softc = (struct sa_softc *)periph->softc;
3254 	ccb = cam_periph_getccb(periph,  1);
3255 
3256 	/* It is safe to retry this operation */
3257 	scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3258 	    FALSE,  0, SSD_FULL_SIZE,  5000, reserve);
3259 	softc->dsreg = MTIO_DSREG_RBSY;
3260 	error = cam_periph_runccb(ccb, saerror, 0,
3261 	    SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3262 	softc->dsreg = MTIO_DSREG_REST;
3263 	QFRLS(ccb);
3264 	xpt_release_ccb(ccb);
3265 
3266 	/*
3267 	 * If the error was Illegal Request, then the device doesn't support
3268 	 * RESERVE/RELEASE. This is not an error.
3269 	 */
3270 	if (error == EINVAL) {
3271 		error = 0;
3272 	}
3273 
3274 	return (error);
3275 }
3276 
3277 static int
3278 saloadunload(struct cam_periph *periph, int load)
3279 {
3280 	union	ccb *ccb;
3281 	struct	sa_softc *softc;
3282 	int	error;
3283 
3284 	softc = (struct sa_softc *)periph->softc;
3285 
3286 	ccb = cam_periph_getccb(periph, 1);
3287 
3288 	/* It is safe to retry this operation */
3289 	scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3290 	    FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3291 
3292 	softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3293 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3294 	softc->dsreg = MTIO_DSREG_REST;
3295 	QFRLS(ccb);
3296 	xpt_release_ccb(ccb);
3297 
3298 	if (error || load == 0)
3299 		softc->fileno = softc->blkno = (daddr_t) -1;
3300 	else if (error == 0)
3301 		softc->fileno = softc->blkno = (daddr_t) 0;
3302 	return (error);
3303 }
3304 
3305 static int
3306 saerase(struct cam_periph *periph, int longerase)
3307 {
3308 
3309 	union	ccb *ccb;
3310 	struct	sa_softc *softc;
3311 	int error;
3312 
3313 	softc = (struct sa_softc *)periph->softc;
3314 
3315 	ccb = cam_periph_getccb(periph, 1);
3316 
3317 	scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3318 	    SSD_FULL_SIZE, ERASE_TIMEOUT);
3319 
3320 	softc->dsreg = MTIO_DSREG_ZER;
3321 	error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3322 	softc->dsreg = MTIO_DSREG_REST;
3323 
3324 	if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3325 		cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3326 	xpt_release_ccb(ccb);
3327 	return (error);
3328 }
3329 
3330 #endif /* _KERNEL */
3331 
3332 /*
3333  * Read tape block limits command.
3334  */
3335 void
3336 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3337 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3338 		   u_int8_t tag_action,
3339 		   struct scsi_read_block_limits_data *rlimit_buf,
3340 		   u_int8_t sense_len, u_int32_t timeout)
3341 {
3342 	struct scsi_read_block_limits *scsi_cmd;
3343 
3344 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3345 	     (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3346 	     sizeof(*scsi_cmd), timeout);
3347 
3348 	scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3349 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3350 	scsi_cmd->opcode = READ_BLOCK_LIMITS;
3351 }
3352 
3353 void
3354 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3355 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3356 		   u_int8_t tag_action, int readop, int sli,
3357 		   int fixed, u_int32_t length, u_int8_t *data_ptr,
3358 		   u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3359 {
3360 	struct scsi_sa_rw *scsi_cmd;
3361 
3362 	scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3363 	scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3364 	scsi_cmd->sli_fixed = 0;
3365 	if (sli && readop)
3366 		scsi_cmd->sli_fixed |= SAR_SLI;
3367 	if (fixed)
3368 		scsi_cmd->sli_fixed |= SARW_FIXED;
3369 	scsi_ulto3b(length, scsi_cmd->length);
3370 	scsi_cmd->control = 0;
3371 
3372 	cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3373 	    tag_action, data_ptr, dxfer_len, sense_len,
3374 	    sizeof(*scsi_cmd), timeout);
3375 }
3376 
3377 void
3378 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3379 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
3380 		 u_int8_t tag_action, int immediate, int eot,
3381 		 int reten, int load, u_int8_t sense_len,
3382 		 u_int32_t timeout)
3383 {
3384 	struct scsi_load_unload *scsi_cmd;
3385 
3386 	scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3387 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3388 	scsi_cmd->opcode = LOAD_UNLOAD;
3389 	if (immediate)
3390 		scsi_cmd->immediate = SLU_IMMED;
3391 	if (eot)
3392 		scsi_cmd->eot_reten_load |= SLU_EOT;
3393 	if (reten)
3394 		scsi_cmd->eot_reten_load |= SLU_RETEN;
3395 	if (load)
3396 		scsi_cmd->eot_reten_load |= SLU_LOAD;
3397 
3398 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3399 	    NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3400 }
3401 
3402 void
3403 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3404 	    void (*cbfcnp)(struct cam_periph *, union ccb *),
3405 	    u_int8_t tag_action, int immediate, u_int8_t sense_len,
3406 	    u_int32_t timeout)
3407 {
3408 	struct scsi_rewind *scsi_cmd;
3409 
3410 	scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3411 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3412 	scsi_cmd->opcode = REWIND;
3413 	if (immediate)
3414 		scsi_cmd->immediate = SREW_IMMED;
3415 
3416 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3417 	    0, sense_len, sizeof(*scsi_cmd), timeout);
3418 }
3419 
3420 void
3421 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3422 	   void (*cbfcnp)(struct cam_periph *, union ccb *),
3423 	   u_int8_t tag_action, scsi_space_code code,
3424 	   u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3425 {
3426 	struct scsi_space *scsi_cmd;
3427 
3428 	scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3429 	scsi_cmd->opcode = SPACE;
3430 	scsi_cmd->code = code;
3431 	scsi_ulto3b(count, scsi_cmd->count);
3432 	scsi_cmd->control = 0;
3433 
3434 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3435 	    0, sense_len, sizeof(*scsi_cmd), timeout);
3436 }
3437 
3438 void
3439 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3440 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
3441 		     u_int8_t tag_action, int immediate, int setmark,
3442 		     u_int32_t num_marks, u_int8_t sense_len,
3443 		     u_int32_t timeout)
3444 {
3445 	struct scsi_write_filemarks *scsi_cmd;
3446 
3447 	scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3448 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3449 	scsi_cmd->opcode = WRITE_FILEMARKS;
3450 	if (immediate)
3451 		scsi_cmd->byte2 |= SWFMRK_IMMED;
3452 	if (setmark)
3453 		scsi_cmd->byte2 |= SWFMRK_WSMK;
3454 
3455 	scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3456 
3457 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3458 	    0, sense_len, sizeof(*scsi_cmd), timeout);
3459 }
3460 
3461 /*
3462  * The reserve and release unit commands differ only by their opcodes.
3463  */
3464 void
3465 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3466 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
3467 			  u_int8_t tag_action, int third_party,
3468 			  int third_party_id, u_int8_t sense_len,
3469 			  u_int32_t timeout, int reserve)
3470 {
3471 	struct scsi_reserve_release_unit *scsi_cmd;
3472 
3473 	scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3474 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3475 
3476 	if (reserve)
3477 		scsi_cmd->opcode = RESERVE_UNIT;
3478 	else
3479 		scsi_cmd->opcode = RELEASE_UNIT;
3480 
3481 	if (third_party) {
3482 		scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3483 		scsi_cmd->lun_thirdparty |=
3484 			((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3485 	}
3486 
3487 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3488 	    0, sense_len, sizeof(*scsi_cmd), timeout);
3489 }
3490 
3491 void
3492 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3493 	   void (*cbfcnp)(struct cam_periph *, union ccb *),
3494 	   u_int8_t tag_action, int immediate, int long_erase,
3495 	   u_int8_t sense_len, u_int32_t timeout)
3496 {
3497 	struct scsi_erase *scsi_cmd;
3498 
3499 	scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3500 	bzero(scsi_cmd, sizeof(*scsi_cmd));
3501 
3502 	scsi_cmd->opcode = ERASE;
3503 
3504 	if (immediate)
3505 		scsi_cmd->lun_imm_long |= SE_IMMED;
3506 
3507 	if (long_erase)
3508 		scsi_cmd->lun_imm_long |= SE_LONG;
3509 
3510 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3511 	    0, sense_len, sizeof(*scsi_cmd), timeout);
3512 }
3513 
3514 /*
3515  * Read Tape Position command.
3516  */
3517 void
3518 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3519 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3520 		   u_int8_t tag_action, int hardsoft,
3521 		   struct scsi_tape_position_data *sbp,
3522 		   u_int8_t sense_len, u_int32_t timeout)
3523 {
3524 	struct scsi_tape_read_position *scmd;
3525 
3526 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3527 	    (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3528 	scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3529 	bzero(scmd, sizeof(*scmd));
3530 	scmd->opcode = READ_POSITION;
3531 	scmd->byte1 = hardsoft;
3532 }
3533 
3534 /*
3535  * Set Tape Position command.
3536  */
3537 void
3538 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3539 		   void (*cbfcnp)(struct cam_periph *, union ccb *),
3540 		   u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3541 		   u_int8_t sense_len, u_int32_t timeout)
3542 {
3543 	struct scsi_tape_locate *scmd;
3544 
3545 	cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3546 	    (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3547 	scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3548 	bzero(scmd, sizeof(*scmd));
3549 	scmd->opcode = LOCATE;
3550 	if (hardsoft)
3551 		scmd->byte1 |= SA_SPOS_BT;
3552 	scsi_ulto4b(blkno, scmd->blkaddr);
3553 }
3554