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