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