1/*-
2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-4-Clause)
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions, and the following disclaimer,
13 *    without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*-
31 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
32 * All rights reserved.
33 *
34 * Partially based on an autochanger driver written by Stefan Grefen
35 * and on an autochanger driver written by the Systems Programming Group
36 * at the University of Utah Computer Science Department.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgements:
48 *	This product includes software developed by Jason R. Thorpe
49 *	for And Communications, http://www.and.com/
50 * 4. The name of the author may not be used to endorse or promote products
51 *    derived from this software without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
54 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
57 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
58 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
59 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
60 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * $NetBSD: ch.c,v 1.34 1998/08/31 22:28:06 cgd Exp $
66 */
67
68#include <sys/cdefs.h>
69__FBSDID("$FreeBSD$");
70
71#include <sys/param.h>
72#include <sys/queue.h>
73#include <sys/systm.h>
74#include <sys/kernel.h>
75#include <sys/types.h>
76#include <sys/malloc.h>
77#include <sys/fcntl.h>
78#include <sys/conf.h>
79#include <sys/chio.h>
80#include <sys/errno.h>
81#include <sys/devicestat.h>
82
83#include <cam/cam.h>
84#include <cam/cam_ccb.h>
85#include <cam/cam_periph.h>
86#include <cam/cam_xpt_periph.h>
87#include <cam/cam_debug.h>
88
89#include <cam/scsi/scsi_all.h>
90#include <cam/scsi/scsi_message.h>
91#include <cam/scsi/scsi_ch.h>
92
93/*
94 * Timeout definitions for various changer related commands.  They may
95 * be too short for some devices (especially the timeout for INITIALIZE
96 * ELEMENT STATUS).
97 */
98
99static const u_int32_t	CH_TIMEOUT_MODE_SENSE                = 6000;
100static const u_int32_t	CH_TIMEOUT_MOVE_MEDIUM               = 15 * 60 * 1000;
101static const u_int32_t	CH_TIMEOUT_EXCHANGE_MEDIUM           = 15 * 60 * 1000;
102static const u_int32_t	CH_TIMEOUT_POSITION_TO_ELEMENT       = 15 * 60 * 1000;
103static const u_int32_t	CH_TIMEOUT_READ_ELEMENT_STATUS       = 5 * 60 * 1000;
104static const u_int32_t	CH_TIMEOUT_SEND_VOLTAG		     = 10000;
105static const u_int32_t	CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
106
107typedef enum {
108	CH_FLAG_INVALID		= 0x001
109} ch_flags;
110
111typedef enum {
112	CH_STATE_PROBE,
113	CH_STATE_NORMAL
114} ch_state;
115
116typedef enum {
117	CH_CCB_PROBE
118} ch_ccb_types;
119
120typedef enum {
121	CH_Q_NONE	= 0x00,
122	CH_Q_NO_DBD	= 0x01,
123	CH_Q_NO_DVCID	= 0x02
124} ch_quirks;
125
126#define CH_Q_BIT_STRING	\
127	"\020"		\
128	"\001NO_DBD"	\
129	"\002NO_DVCID"
130
131#define ccb_state	ppriv_field0
132#define ccb_bp		ppriv_ptr1
133
134struct scsi_mode_sense_data {
135	struct scsi_mode_header_6 header;
136	struct scsi_mode_blk_desc blk_desc;
137	union {
138		struct page_element_address_assignment ea;
139		struct page_transport_geometry_parameters tg;
140		struct page_device_capabilities cap;
141	} pages;
142};
143
144struct ch_softc {
145	ch_flags	flags;
146	ch_state	state;
147	ch_quirks	quirks;
148	union ccb	saved_ccb;
149	struct devstat	*device_stats;
150	struct cdev     *dev;
151	int		open_count;
152
153	int		sc_picker;	/* current picker */
154
155	/*
156	 * The following information is obtained from the
157	 * element address assignment page.
158	 */
159	int		sc_firsts[CHET_MAX + 1];	/* firsts */
160	int		sc_counts[CHET_MAX + 1];	/* counts */
161
162	/*
163	 * The following mask defines the legal combinations
164	 * of elements for the MOVE MEDIUM command.
165	 */
166	u_int8_t	sc_movemask[CHET_MAX + 1];
167
168	/*
169	 * As above, but for EXCHANGE MEDIUM.
170	 */
171	u_int8_t	sc_exchangemask[CHET_MAX + 1];
172
173	/*
174	 * Quirks; see below.  XXX KDM not implemented yet
175	 */
176	int		sc_settledelay;	/* delay for settle */
177};
178
179static	d_open_t	chopen;
180static	d_close_t	chclose;
181static	d_ioctl_t	chioctl;
182static	periph_init_t	chinit;
183static  periph_ctor_t	chregister;
184static	periph_oninv_t	choninvalidate;
185static  periph_dtor_t   chcleanup;
186static  periph_start_t  chstart;
187static	void		chasync(void *callback_arg, u_int32_t code,
188				struct cam_path *path, void *arg);
189static	void		chdone(struct cam_periph *periph,
190			       union ccb *done_ccb);
191static	int		cherror(union ccb *ccb, u_int32_t cam_flags,
192				u_int32_t sense_flags);
193static	int		chmove(struct cam_periph *periph,
194			       struct changer_move *cm);
195static	int		chexchange(struct cam_periph *periph,
196				   struct changer_exchange *ce);
197static	int		chposition(struct cam_periph *periph,
198				   struct changer_position *cp);
199static	int		chgetelemstatus(struct cam_periph *periph,
200				int scsi_version, u_long cmd,
201				struct changer_element_status_request *csr);
202static	int		chsetvoltag(struct cam_periph *periph,
203				    struct changer_set_voltag_request *csvr);
204static	int		chielem(struct cam_periph *periph,
205				unsigned int timeout);
206static	int		chgetparams(struct cam_periph *periph);
207static	int		chscsiversion(struct cam_periph *periph);
208
209static struct periph_driver chdriver =
210{
211	chinit, "ch",
212	TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
213};
214
215PERIPHDRIVER_DECLARE(ch, chdriver);
216
217static struct cdevsw ch_cdevsw = {
218	.d_version =	D_VERSION,
219	.d_flags =	D_TRACKCLOSE,
220	.d_open =	chopen,
221	.d_close =	chclose,
222	.d_ioctl =	chioctl,
223	.d_name =	"ch",
224};
225
226static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
227
228static void
229chinit(void)
230{
231	cam_status status;
232
233	/*
234	 * Install a global async callback.  This callback will
235	 * receive async callbacks like "new device found".
236	 */
237	status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
238
239	if (status != CAM_REQ_CMP) {
240		printf("ch: Failed to attach master async callback "
241		       "due to status 0x%x!\n", status);
242	}
243}
244
245static void
246chdevgonecb(void *arg)
247{
248	struct ch_softc   *softc;
249	struct cam_periph *periph;
250	struct mtx *mtx;
251	int i;
252
253	periph = (struct cam_periph *)arg;
254	mtx = cam_periph_mtx(periph);
255	mtx_lock(mtx);
256
257	softc = (struct ch_softc *)periph->softc;
258	KASSERT(softc->open_count >= 0, ("Negative open count %d",
259		softc->open_count));
260
261	/*
262	 * When we get this callback, we will get no more close calls from
263	 * devfs.  So if we have any dangling opens, we need to release the
264	 * reference held for that particular context.
265	 */
266	for (i = 0; i < softc->open_count; i++)
267		cam_periph_release_locked(periph);
268
269	softc->open_count = 0;
270
271	/*
272	 * Release the reference held for the device node, it is gone now.
273	 */
274	cam_periph_release_locked(periph);
275
276	/*
277	 * We reference the lock directly here, instead of using
278	 * cam_periph_unlock().  The reason is that the final call to
279	 * cam_periph_release_locked() above could result in the periph
280	 * getting freed.  If that is the case, dereferencing the periph
281	 * with a cam_periph_unlock() call would cause a page fault.
282	 */
283	mtx_unlock(mtx);
284}
285
286static void
287choninvalidate(struct cam_periph *periph)
288{
289	struct ch_softc *softc;
290
291	softc = (struct ch_softc *)periph->softc;
292
293	/*
294	 * De-register any async callbacks.
295	 */
296	xpt_register_async(0, chasync, periph, periph->path);
297
298	softc->flags |= CH_FLAG_INVALID;
299
300	/*
301	 * Tell devfs this device has gone away, and ask for a callback
302	 * when it has cleaned up its state.
303	 */
304	destroy_dev_sched_cb(softc->dev, chdevgonecb, periph);
305}
306
307static void
308chcleanup(struct cam_periph *periph)
309{
310	struct ch_softc *softc;
311
312	softc = (struct ch_softc *)periph->softc;
313
314	devstat_remove_entry(softc->device_stats);
315
316	free(softc, M_DEVBUF);
317}
318
319static void
320chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
321{
322	struct cam_periph *periph;
323
324	periph = (struct cam_periph *)callback_arg;
325
326	switch(code) {
327	case AC_FOUND_DEVICE:
328	{
329		struct ccb_getdev *cgd;
330		cam_status status;
331
332		cgd = (struct ccb_getdev *)arg;
333		if (cgd == NULL)
334			break;
335
336		if (cgd->protocol != PROTO_SCSI)
337			break;
338		if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
339			break;
340		if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
341			break;
342
343		/*
344		 * Allocate a peripheral instance for
345		 * this device and start the probe
346		 * process.
347		 */
348		status = cam_periph_alloc(chregister, choninvalidate,
349					  chcleanup, chstart, "ch",
350					  CAM_PERIPH_BIO, path,
351					  chasync, AC_FOUND_DEVICE, cgd);
352
353		if (status != CAM_REQ_CMP
354		 && status != CAM_REQ_INPROG)
355			printf("chasync: Unable to probe new device "
356			       "due to status 0x%x\n", status);
357
358		break;
359
360	}
361	default:
362		cam_periph_async(periph, code, path, arg);
363		break;
364	}
365}
366
367static cam_status
368chregister(struct cam_periph *periph, void *arg)
369{
370	struct ch_softc *softc;
371	struct ccb_getdev *cgd;
372	struct ccb_pathinq cpi;
373	struct make_dev_args args;
374	int error;
375
376	cgd = (struct ccb_getdev *)arg;
377	if (cgd == NULL) {
378		printf("chregister: no getdev CCB, can't register device\n");
379		return(CAM_REQ_CMP_ERR);
380	}
381
382	softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
383
384	if (softc == NULL) {
385		printf("chregister: Unable to probe new device. "
386		       "Unable to allocate softc\n");
387		return(CAM_REQ_CMP_ERR);
388	}
389
390	bzero(softc, sizeof(*softc));
391	softc->state = CH_STATE_PROBE;
392	periph->softc = softc;
393	softc->quirks = CH_Q_NONE;
394
395	/*
396	 * The DVCID and CURDATA bits were not introduced until the SMC
397	 * spec.  If this device claims SCSI-2 or earlier support, then it
398	 * very likely does not support these bits.
399	 */
400	if (cgd->inq_data.version <= SCSI_REV_2)
401		softc->quirks |= CH_Q_NO_DVCID;
402
403	xpt_path_inq(&cpi, periph->path);
404
405	/*
406	 * Changers don't have a blocksize, and obviously don't support
407	 * tagged queueing.
408	 */
409	cam_periph_unlock(periph);
410	softc->device_stats = devstat_new_entry("ch",
411			  periph->unit_number, 0,
412			  DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
413			  SID_TYPE(&cgd->inq_data) |
414			  XPORT_DEVSTAT_TYPE(cpi.transport),
415			  DEVSTAT_PRIORITY_OTHER);
416
417	/*
418	 * Acquire a reference to the periph before we create the devfs
419	 * instance for it.  We'll release this reference once the devfs
420	 * instance has been freed.
421	 */
422	if (cam_periph_acquire(periph) != 0) {
423		xpt_print(periph->path, "%s: lost periph during "
424			  "registration!\n", __func__);
425		cam_periph_lock(periph);
426		return (CAM_REQ_CMP_ERR);
427	}
428
429
430	/* Register the device */
431	make_dev_args_init(&args);
432	args.mda_devsw = &ch_cdevsw;
433	args.mda_unit = periph->unit_number;
434	args.mda_uid = UID_ROOT;
435	args.mda_gid = GID_OPERATOR;
436	args.mda_mode = 0600;
437	args.mda_si_drv1 = periph;
438	error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
439	    periph->unit_number);
440	cam_periph_lock(periph);
441	if (error != 0) {
442		cam_periph_release_locked(periph);
443		return (CAM_REQ_CMP_ERR);
444	}
445
446	/*
447	 * Add an async callback so that we get
448	 * notified if this device goes away.
449	 */
450	xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
451
452	/*
453	 * Lock this periph until we are setup.
454	 * This first call can't block
455	 */
456	(void)cam_periph_hold(periph, PRIBIO);
457	xpt_schedule(periph, CAM_PRIORITY_DEV);
458
459	return(CAM_REQ_CMP);
460}
461
462static int
463chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
464{
465	struct cam_periph *periph;
466	struct ch_softc *softc;
467	int error;
468
469	periph = (struct cam_periph *)dev->si_drv1;
470	if (cam_periph_acquire(periph) != 0)
471		return (ENXIO);
472
473	softc = (struct ch_softc *)periph->softc;
474
475	cam_periph_lock(periph);
476
477	if (softc->flags & CH_FLAG_INVALID) {
478		cam_periph_release_locked(periph);
479		cam_periph_unlock(periph);
480		return(ENXIO);
481	}
482
483	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
484		cam_periph_unlock(periph);
485		cam_periph_release(periph);
486		return (error);
487	}
488
489	/*
490	 * Load information about this changer device into the softc.
491	 */
492	if ((error = chgetparams(periph)) != 0) {
493		cam_periph_unhold(periph);
494		cam_periph_release_locked(periph);
495		cam_periph_unlock(periph);
496		return(error);
497	}
498
499	cam_periph_unhold(periph);
500
501	softc->open_count++;
502
503	cam_periph_unlock(periph);
504
505	return(error);
506}
507
508static int
509chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
510{
511	struct	cam_periph *periph;
512	struct  ch_softc *softc;
513	struct mtx *mtx;
514
515	periph = (struct cam_periph *)dev->si_drv1;
516	mtx = cam_periph_mtx(periph);
517	mtx_lock(mtx);
518
519	softc = (struct ch_softc *)periph->softc;
520	softc->open_count--;
521
522	cam_periph_release_locked(periph);
523
524	/*
525	 * We reference the lock directly here, instead of using
526	 * cam_periph_unlock().  The reason is that the call to
527	 * cam_periph_release_locked() above could result in the periph
528	 * getting freed.  If that is the case, dereferencing the periph
529	 * with a cam_periph_unlock() call would cause a page fault.
530	 *
531	 * cam_periph_release() avoids this problem using the same method,
532	 * but we're manually acquiring and dropping the lock here to
533	 * protect the open count and avoid another lock acquisition and
534	 * release.
535	 */
536	mtx_unlock(mtx);
537
538	return(0);
539}
540
541static void
542chstart(struct cam_periph *periph, union ccb *start_ccb)
543{
544	struct ch_softc *softc;
545
546	softc = (struct ch_softc *)periph->softc;
547
548	switch (softc->state) {
549	case CH_STATE_NORMAL:
550	{
551		xpt_release_ccb(start_ccb);
552		break;
553	}
554	case CH_STATE_PROBE:
555	{
556		int mode_buffer_len;
557		void *mode_buffer;
558
559		/*
560		 * Include the block descriptor when calculating the mode
561		 * buffer length,
562		 */
563		mode_buffer_len = sizeof(struct scsi_mode_header_6) +
564				  sizeof(struct scsi_mode_blk_desc) +
565				 sizeof(struct page_element_address_assignment);
566
567		mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
568
569		if (mode_buffer == NULL) {
570			printf("chstart: couldn't malloc mode sense data\n");
571			break;
572		}
573		bzero(mode_buffer, mode_buffer_len);
574
575		/*
576		 * Get the element address assignment page.
577		 */
578		scsi_mode_sense(&start_ccb->csio,
579				/* retries */ 1,
580				/* cbfcnp */ chdone,
581				/* tag_action */ MSG_SIMPLE_Q_TAG,
582				/* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
583					FALSE : TRUE,
584				/* pc */ SMS_PAGE_CTRL_CURRENT,
585				/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
586				/* param_buf */ (u_int8_t *)mode_buffer,
587				/* param_len */ mode_buffer_len,
588				/* sense_len */ SSD_FULL_SIZE,
589				/* timeout */ CH_TIMEOUT_MODE_SENSE);
590
591		start_ccb->ccb_h.ccb_bp = NULL;
592		start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
593		xpt_action(start_ccb);
594		break;
595	}
596	}
597}
598
599static void
600chdone(struct cam_periph *periph, union ccb *done_ccb)
601{
602	struct ch_softc *softc;
603	struct ccb_scsiio *csio;
604
605	softc = (struct ch_softc *)periph->softc;
606	csio = &done_ccb->csio;
607
608	switch(done_ccb->ccb_h.ccb_state) {
609	case CH_CCB_PROBE:
610	{
611		struct scsi_mode_header_6 *mode_header;
612		struct page_element_address_assignment *ea;
613		char announce_buf[80];
614
615
616		mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
617
618		ea = (struct page_element_address_assignment *)
619			find_mode_page_6(mode_header);
620
621		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
622
623			softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
624			softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
625			softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
626			softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
627			softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
628			softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
629			softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
630			softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
631			softc->sc_picker = softc->sc_firsts[CHET_MT];
632
633#define PLURAL(c)	(c) == 1 ? "" : "s"
634			snprintf(announce_buf, sizeof(announce_buf),
635				"%d slot%s, %d drive%s, "
636				"%d picker%s, %d portal%s",
637		    		softc->sc_counts[CHET_ST],
638				PLURAL(softc->sc_counts[CHET_ST]),
639		    		softc->sc_counts[CHET_DT],
640				PLURAL(softc->sc_counts[CHET_DT]),
641		    		softc->sc_counts[CHET_MT],
642				PLURAL(softc->sc_counts[CHET_MT]),
643		    		softc->sc_counts[CHET_IE],
644				PLURAL(softc->sc_counts[CHET_IE]));
645#undef PLURAL
646			if (announce_buf[0] != '\0') {
647				xpt_announce_periph(periph, announce_buf);
648				xpt_announce_quirks(periph, softc->quirks,
649				    CH_Q_BIT_STRING);
650			}
651		} else {
652			int error;
653
654			error = cherror(done_ccb, CAM_RETRY_SELTO,
655					SF_RETRY_UA | SF_NO_PRINT);
656			/*
657			 * Retry any UNIT ATTENTION type errors.  They
658			 * are expected at boot.
659			 */
660			if (error == ERESTART) {
661				/*
662				 * A retry was scheduled, so
663				 * just return.
664				 */
665				return;
666			} else if (error != 0) {
667				struct scsi_mode_sense_6 *sms;
668				int frozen, retry_scheduled;
669
670				sms = (struct scsi_mode_sense_6 *)
671					done_ccb->csio.cdb_io.cdb_bytes;
672				frozen = (done_ccb->ccb_h.status &
673				    CAM_DEV_QFRZN) != 0;
674
675				/*
676				 * Check to see if block descriptors were
677				 * disabled.  Some devices don't like that.
678				 * We're taking advantage of the fact that
679				 * the first few bytes of the 6 and 10 byte
680				 * mode sense commands are the same.  If
681				 * block descriptors were disabled, enable
682				 * them and re-send the command.
683				 */
684				if ((sms->byte2 & SMS_DBD) != 0 &&
685				    (periph->flags & CAM_PERIPH_INVALID) == 0) {
686					sms->byte2 &= ~SMS_DBD;
687					xpt_action(done_ccb);
688					softc->quirks |= CH_Q_NO_DBD;
689					retry_scheduled = 1;
690				} else
691					retry_scheduled = 0;
692
693				/* Don't wedge this device's queue */
694				if (frozen)
695					cam_release_devq(done_ccb->ccb_h.path,
696						 /*relsim_flags*/0,
697						 /*reduction*/0,
698						 /*timeout*/0,
699						 /*getcount_only*/0);
700
701				if (retry_scheduled)
702					return;
703
704				if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
705				    == CAM_SCSI_STATUS_ERROR)
706					scsi_sense_print(&done_ccb->csio);
707				else {
708					xpt_print(periph->path,
709					    "got CAM status %#x\n",
710					    done_ccb->ccb_h.status);
711				}
712				xpt_print(periph->path, "fatal error, failed "
713				    "to attach to device\n");
714
715				cam_periph_invalidate(periph);
716
717			}
718		}
719		softc->state = CH_STATE_NORMAL;
720		free(mode_header, M_SCSICH);
721		/*
722		 * Since our peripheral may be invalidated by an error
723		 * above or an external event, we must release our CCB
724		 * before releasing the probe lock on the peripheral.
725		 * The peripheral will only go away once the last lock
726		 * is removed, and we need it around for the CCB release
727		 * operation.
728		 */
729		xpt_release_ccb(done_ccb);
730		cam_periph_unhold(periph);
731		return;
732	}
733	default:
734		break;
735	}
736	xpt_release_ccb(done_ccb);
737}
738
739static int
740cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
741{
742	struct ch_softc *softc;
743	struct cam_periph *periph;
744
745	periph = xpt_path_periph(ccb->ccb_h.path);
746	softc = (struct ch_softc *)periph->softc;
747
748	return (cam_periph_error(ccb, cam_flags, sense_flags));
749}
750
751static int
752chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
753{
754	struct cam_periph *periph;
755	struct ch_softc *softc;
756	int error;
757
758	periph = (struct cam_periph *)dev->si_drv1;
759	cam_periph_lock(periph);
760	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
761
762	softc = (struct ch_softc *)periph->softc;
763
764	error = 0;
765
766	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
767		  ("trying to do ioctl %#lx\n", cmd));
768
769	/*
770	 * If this command can change the device's state, we must
771	 * have the device open for writing.
772	 */
773	switch (cmd) {
774	case CHIOGPICKER:
775	case CHIOGPARAMS:
776	case OCHIOGSTATUS:
777	case CHIOGSTATUS:
778		break;
779
780	default:
781		if ((flag & FWRITE) == 0) {
782			cam_periph_unlock(periph);
783			return (EBADF);
784		}
785	}
786
787	switch (cmd) {
788	case CHIOMOVE:
789		error = chmove(periph, (struct changer_move *)addr);
790		break;
791
792	case CHIOEXCHANGE:
793		error = chexchange(periph, (struct changer_exchange *)addr);
794		break;
795
796	case CHIOPOSITION:
797		error = chposition(periph, (struct changer_position *)addr);
798		break;
799
800	case CHIOGPICKER:
801		*(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
802		break;
803
804	case CHIOSPICKER:
805	{
806		int new_picker = *(int *)addr;
807
808		if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
809			error = EINVAL;
810			break;
811		}
812		softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
813		break;
814	}
815	case CHIOGPARAMS:
816	{
817		struct changer_params *cp = (struct changer_params *)addr;
818
819		cp->cp_npickers = softc->sc_counts[CHET_MT];
820		cp->cp_nslots = softc->sc_counts[CHET_ST];
821		cp->cp_nportals = softc->sc_counts[CHET_IE];
822		cp->cp_ndrives = softc->sc_counts[CHET_DT];
823		break;
824	}
825	case CHIOIELEM:
826		error = chielem(periph, *(unsigned int *)addr);
827		break;
828
829	case OCHIOGSTATUS:
830	{
831		error = chgetelemstatus(periph, SCSI_REV_2, cmd,
832		    (struct changer_element_status_request *)addr);
833		break;
834	}
835
836	case CHIOGSTATUS:
837	{
838		int scsi_version;
839
840		scsi_version = chscsiversion(periph);
841		if (scsi_version >= SCSI_REV_0) {
842			error = chgetelemstatus(periph, scsi_version, cmd,
843			    (struct changer_element_status_request *)addr);
844	  	}
845		else { /* unable to determine the SCSI version */
846			cam_periph_unlock(periph);
847			return (ENXIO);
848		}
849		break;
850	}
851
852	case CHIOSETVOLTAG:
853	{
854		error = chsetvoltag(periph,
855				    (struct changer_set_voltag_request *) addr);
856		break;
857	}
858
859	/* Implement prevent/allow? */
860
861	default:
862		error = cam_periph_ioctl(periph, cmd, addr, cherror);
863		break;
864	}
865
866	cam_periph_unlock(periph);
867	return (error);
868}
869
870static int
871chmove(struct cam_periph *periph, struct changer_move *cm)
872{
873	struct ch_softc *softc;
874	u_int16_t fromelem, toelem;
875	union ccb *ccb;
876	int error;
877
878	error = 0;
879	softc = (struct ch_softc *)periph->softc;
880
881	/*
882	 * Check arguments.
883	 */
884	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
885		return (EINVAL);
886	if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
887	    (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
888		return (ENODEV);
889
890	/*
891	 * Check the request against the changer's capabilities.
892	 */
893	if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
894		return (ENODEV);
895
896	/*
897	 * Calculate the source and destination elements.
898	 */
899	fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
900	toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
901
902	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
903
904	scsi_move_medium(&ccb->csio,
905			 /* retries */ 1,
906			 /* cbfcnp */ chdone,
907			 /* tag_action */ MSG_SIMPLE_Q_TAG,
908			 /* tea */ softc->sc_picker,
909			 /* src */ fromelem,
910			 /* dst */ toelem,
911			 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
912			 /* sense_len */ SSD_FULL_SIZE,
913			 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
914
915	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
916				  /*sense_flags*/ SF_RETRY_UA,
917				  softc->device_stats);
918
919	xpt_release_ccb(ccb);
920
921	return(error);
922}
923
924static int
925chexchange(struct cam_periph *periph, struct changer_exchange *ce)
926{
927	struct ch_softc *softc;
928	u_int16_t src, dst1, dst2;
929	union ccb *ccb;
930	int error;
931
932	error = 0;
933	softc = (struct ch_softc *)periph->softc;
934	/*
935	 * Check arguments.
936	 */
937	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
938	    (ce->ce_sdsttype > CHET_DT))
939		return (EINVAL);
940	if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
941	    (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
942	    (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
943		return (ENODEV);
944
945	/*
946	 * Check the request against the changer's capabilities.
947	 */
948	if (((softc->sc_exchangemask[ce->ce_srctype] &
949	     (1 << ce->ce_fdsttype)) == 0) ||
950	    ((softc->sc_exchangemask[ce->ce_fdsttype] &
951	     (1 << ce->ce_sdsttype)) == 0))
952		return (ENODEV);
953
954	/*
955	 * Calculate the source and destination elements.
956	 */
957	src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
958	dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
959	dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
960
961	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
962
963	scsi_exchange_medium(&ccb->csio,
964			     /* retries */ 1,
965			     /* cbfcnp */ chdone,
966			     /* tag_action */ MSG_SIMPLE_Q_TAG,
967			     /* tea */ softc->sc_picker,
968			     /* src */ src,
969			     /* dst1 */ dst1,
970			     /* dst2 */ dst2,
971			     /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
972			                   TRUE : FALSE,
973			     /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
974			                   TRUE : FALSE,
975			     /* sense_len */ SSD_FULL_SIZE,
976			     /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
977
978	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
979				  /*sense_flags*/ SF_RETRY_UA,
980				  softc->device_stats);
981
982	xpt_release_ccb(ccb);
983
984	return(error);
985}
986
987static int
988chposition(struct cam_periph *periph, struct changer_position *cp)
989{
990	struct ch_softc *softc;
991	u_int16_t dst;
992	union ccb *ccb;
993	int error;
994
995	error = 0;
996	softc = (struct ch_softc *)periph->softc;
997
998	/*
999	 * Check arguments.
1000	 */
1001	if (cp->cp_type > CHET_DT)
1002		return (EINVAL);
1003	if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
1004		return (ENODEV);
1005
1006	/*
1007	 * Calculate the destination element.
1008	 */
1009	dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
1010
1011	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1012
1013	scsi_position_to_element(&ccb->csio,
1014				 /* retries */ 1,
1015				 /* cbfcnp */ chdone,
1016				 /* tag_action */ MSG_SIMPLE_Q_TAG,
1017				 /* tea */ softc->sc_picker,
1018				 /* dst */ dst,
1019				 /* invert */ (cp->cp_flags & CP_INVERT) ?
1020					      TRUE : FALSE,
1021				 /* sense_len */ SSD_FULL_SIZE,
1022				 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
1023
1024	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1025				  /*sense_flags*/ SF_RETRY_UA,
1026				  softc->device_stats);
1027
1028	xpt_release_ccb(ccb);
1029
1030	return(error);
1031}
1032
1033/*
1034 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
1035 * to host native byte order in the volume serial number.  The volume
1036 * label as returned by the changer is transferred to user mode as
1037 * nul-terminated string.  Volume labels are truncated at the first
1038 * space, as suggested by SCSI-2.
1039 */
1040static	void
1041copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
1042{
1043	int i;
1044	for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
1045		char c = voltag->vif[i];
1046		if (c && c != ' ')
1047			uvoltag->cv_volid[i] = c;
1048	        else
1049			break;
1050	}
1051	uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
1052}
1053
1054/*
1055 * Copy an element status descriptor to a user-mode
1056 * changer_element_status structure.
1057 */
1058static void
1059copy_element_status(struct ch_softc *softc,
1060		    u_int16_t flags,
1061		    struct read_element_status_descriptor *desc,
1062		    struct changer_element_status *ces,
1063		    int scsi_version)
1064{
1065	u_int16_t eaddr = scsi_2btoul(desc->eaddr);
1066	u_int16_t et;
1067	struct volume_tag *pvol_tag = NULL, *avol_tag = NULL;
1068	struct read_element_status_device_id *devid = NULL;
1069
1070	ces->ces_int_addr = eaddr;
1071	/* set up logical address in element status */
1072	for (et = CHET_MT; et <= CHET_DT; et++) {
1073		if ((softc->sc_firsts[et] <= eaddr)
1074		    && ((softc->sc_firsts[et] + softc->sc_counts[et])
1075			> eaddr)) {
1076			ces->ces_addr = eaddr - softc->sc_firsts[et];
1077			ces->ces_type = et;
1078			break;
1079		}
1080	}
1081
1082	ces->ces_flags = desc->flags1;
1083
1084	ces->ces_sensecode = desc->sense_code;
1085	ces->ces_sensequal = desc->sense_qual;
1086
1087	if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1088		ces->ces_flags |= CES_INVERT;
1089
1090	if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1091
1092		eaddr = scsi_2btoul(desc->ssea);
1093
1094		/* convert source address to logical format */
1095		for (et = CHET_MT; et <= CHET_DT; et++) {
1096			if ((softc->sc_firsts[et] <= eaddr)
1097			    && ((softc->sc_firsts[et] + softc->sc_counts[et])
1098				> eaddr)) {
1099				ces->ces_source_addr =
1100					eaddr - softc->sc_firsts[et];
1101				ces->ces_source_type = et;
1102				ces->ces_flags |= CES_SOURCE_VALID;
1103				break;
1104			}
1105		}
1106
1107		if (!(ces->ces_flags & CES_SOURCE_VALID))
1108			printf("ch: warning: could not map element source "
1109			       "address %ud to a valid element type\n",
1110			       eaddr);
1111	}
1112
1113	/*
1114	 * pvoltag and avoltag are common between SCSI-2 and later versions
1115	 */
1116	if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1117		pvol_tag = &desc->voltag_devid.pvoltag;
1118	if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1119		avol_tag = (flags & READ_ELEMENT_STATUS_PVOLTAG) ?
1120		    &desc->voltag_devid.voltag[1] :&desc->voltag_devid.pvoltag;
1121	/*
1122	 * For SCSI-3 and later, element status can carry designator and
1123	 * other information.
1124	 */
1125	if (scsi_version >= SCSI_REV_SPC) {
1126		if ((flags & READ_ELEMENT_STATUS_PVOLTAG) ^
1127		    (flags & READ_ELEMENT_STATUS_AVOLTAG))
1128			devid = &desc->voltag_devid.pvol_and_devid.devid;
1129		else if (!(flags & READ_ELEMENT_STATUS_PVOLTAG) &&
1130			 !(flags & READ_ELEMENT_STATUS_AVOLTAG))
1131			devid = &desc->voltag_devid.devid;
1132		else /* Have both PVOLTAG and AVOLTAG */
1133			devid = &desc->voltag_devid.vol_tags_and_devid.devid;
1134	}
1135
1136	if (pvol_tag)
1137		copy_voltag(&(ces->ces_pvoltag), pvol_tag);
1138	if (avol_tag)
1139		copy_voltag(&(ces->ces_pvoltag), avol_tag);
1140	if (devid != NULL) {
1141		if (devid->designator_length > 0) {
1142			bcopy((void *)devid->designator,
1143			      (void *)ces->ces_designator,
1144			      devid->designator_length);
1145			ces->ces_designator_length = devid->designator_length;
1146			/*
1147			 * Make sure we are always NUL terminated.  The
1148			 * This won't matter for the binary code set,
1149			 * since the user will only pay attention to the
1150			 * length field.
1151			 */
1152			ces->ces_designator[devid->designator_length]= '\0';
1153		}
1154		if (devid->piv_assoc_designator_type &
1155		    READ_ELEMENT_STATUS_PIV_SET) {
1156			ces->ces_flags |= CES_PIV;
1157			ces->ces_protocol_id =
1158			    READ_ELEMENT_STATUS_PROTOCOL_ID(
1159			    devid->prot_code_set);
1160		}
1161		ces->ces_code_set =
1162		    READ_ELEMENT_STATUS_CODE_SET(devid->prot_code_set);
1163		ces->ces_assoc = READ_ELEMENT_STATUS_ASSOCIATION(
1164		    devid->piv_assoc_designator_type);
1165		ces->ces_designator_type = READ_ELEMENT_STATUS_DESIGNATOR_TYPE(
1166		    devid->piv_assoc_designator_type);
1167	} else if (scsi_version > SCSI_REV_2) {
1168		/* SCSI-SPC and No devid, no designator */
1169		ces->ces_designator_length = 0;
1170		ces->ces_designator[0] = '\0';
1171		ces->ces_protocol_id = CES_PROTOCOL_ID_FCP_4;
1172	}
1173
1174	if (scsi_version <= SCSI_REV_2) {
1175		if (desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1176		    READ_ELEMENT_STATUS_DT_IDVALID) {
1177			ces->ces_flags |= CES_SCSIID_VALID;
1178			ces->ces_scsi_id =
1179			    desc->dt_or_obsolete.scsi_2.dt_scsi_addr;
1180		}
1181
1182		if (desc->dt_or_obsolete.scsi_2.dt_scsi_addr &
1183		    READ_ELEMENT_STATUS_DT_LUVALID) {
1184			ces->ces_flags |= CES_LUN_VALID;
1185			ces->ces_scsi_lun =
1186			    desc->dt_or_obsolete.scsi_2.dt_scsi_flags &
1187			    READ_ELEMENT_STATUS_DT_LUNMASK;
1188		}
1189	}
1190}
1191
1192static int
1193chgetelemstatus(struct cam_periph *periph, int scsi_version, u_long cmd,
1194		struct changer_element_status_request *cesr)
1195{
1196	struct read_element_status_header *st_hdr;
1197	struct read_element_status_page_header *pg_hdr;
1198	struct read_element_status_descriptor *desc;
1199	caddr_t data = NULL;
1200	size_t size, desclen;
1201	u_int avail, i;
1202	int curdata, dvcid, sense_flags;
1203	int try_no_dvcid = 0;
1204	struct changer_element_status *user_data = NULL;
1205	struct ch_softc *softc;
1206	union ccb *ccb;
1207	int chet = cesr->cesr_element_type;
1208	int error = 0;
1209	int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1210
1211	softc = (struct ch_softc *)periph->softc;
1212
1213	/* perform argument checking */
1214
1215	/*
1216	 * Perform a range check on the cesr_element_{base,count}
1217	 * request argument fields.
1218	 */
1219	if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1220	    || (cesr->cesr_element_base + cesr->cesr_element_count)
1221	        > softc->sc_counts[chet])
1222		return (EINVAL);
1223
1224	/*
1225	 * Request one descriptor for the given element type.  This
1226	 * is used to determine the size of the descriptor so that
1227	 * we can allocate enough storage for all of them.  We assume
1228	 * that the first one can fit into 1k.
1229	 */
1230	cam_periph_unlock(periph);
1231	data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1232
1233	cam_periph_lock(periph);
1234	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1235
1236	sense_flags = SF_RETRY_UA;
1237	if (softc->quirks & CH_Q_NO_DVCID) {
1238		dvcid = 0;
1239		curdata = 0;
1240	} else {
1241		dvcid = 1;
1242		curdata = 1;
1243		/*
1244		 * Don't print anything for an Illegal Request, because
1245		 * these flags can cause some changers to complain.  We'll
1246		 * retry without them if we get an error.
1247		 */
1248		sense_flags |= SF_QUIET_IR;
1249	}
1250
1251retry_einval:
1252
1253	scsi_read_element_status(&ccb->csio,
1254				 /* retries */ 1,
1255				 /* cbfcnp */ chdone,
1256				 /* tag_action */ MSG_SIMPLE_Q_TAG,
1257				 /* voltag */ want_voltags,
1258				 /* sea */ softc->sc_firsts[chet],
1259				 /* curdata */ curdata,
1260				 /* dvcid */ dvcid,
1261				 /* count */ 1,
1262				 /* data_ptr */ data,
1263				 /* dxfer_len */ 1024,
1264				 /* sense_len */ SSD_FULL_SIZE,
1265				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1266
1267	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1268				  /*sense_flags*/ sense_flags,
1269				  softc->device_stats);
1270
1271	/*
1272	 * An Illegal Request sense key (only used if there is no asc/ascq)
1273	 * or 0x24,0x00 for an ASC/ASCQ both map to EINVAL.  If dvcid or
1274	 * curdata are set (we set both or neither), try turning them off
1275	 * and see if the command is successful.
1276	 */
1277	if ((error == EINVAL)
1278	 && (dvcid || curdata))  {
1279		dvcid = 0;
1280		curdata = 0;
1281		error = 0;
1282		/* At this point we want to report any Illegal Request */
1283		sense_flags &= ~SF_QUIET_IR;
1284		try_no_dvcid = 1;
1285		goto retry_einval;
1286	}
1287
1288	/*
1289	 * In this case, we tried a read element status with dvcid and
1290	 * curdata set, and it failed.  We retried without those bits, and
1291	 * it succeeded.  Suggest to the user that he set a quirk, so we
1292	 * don't go through the retry process the first time in the future.
1293	 * This should only happen on changers that claim SCSI-3 or higher,
1294	 * but don't support these bits.
1295	 */
1296	if ((try_no_dvcid != 0)
1297	 && (error == 0))
1298		softc->quirks |= CH_Q_NO_DVCID;
1299
1300	if (error)
1301		goto done;
1302	cam_periph_unlock(periph);
1303
1304	st_hdr = (struct read_element_status_header *)data;
1305	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1306		  sizeof(struct read_element_status_header));
1307	desclen = scsi_2btoul(pg_hdr->edl);
1308
1309	size = sizeof(struct read_element_status_header) +
1310	       sizeof(struct read_element_status_page_header) +
1311	       (desclen * cesr->cesr_element_count);
1312	/*
1313	 * Reallocate storage for descriptors and get them from the
1314	 * device.
1315	 */
1316	free(data, M_DEVBUF);
1317	data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1318
1319	cam_periph_lock(periph);
1320	scsi_read_element_status(&ccb->csio,
1321				 /* retries */ 1,
1322				 /* cbfcnp */ chdone,
1323				 /* tag_action */ MSG_SIMPLE_Q_TAG,
1324				 /* voltag */ want_voltags,
1325				 /* sea */ softc->sc_firsts[chet]
1326				 + cesr->cesr_element_base,
1327				 /* curdata */ curdata,
1328				 /* dvcid */ dvcid,
1329				 /* count */ cesr->cesr_element_count,
1330				 /* data_ptr */ data,
1331				 /* dxfer_len */ size,
1332				 /* sense_len */ SSD_FULL_SIZE,
1333				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1334
1335	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1336				  /*sense_flags*/ SF_RETRY_UA,
1337				  softc->device_stats);
1338
1339	if (error)
1340		goto done;
1341	cam_periph_unlock(periph);
1342
1343	/*
1344	 * Fill in the user status array.
1345	 */
1346	st_hdr = (struct read_element_status_header *)data;
1347	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1348		  sizeof(struct read_element_status_header));
1349	avail = scsi_2btoul(st_hdr->count);
1350
1351	if (avail != cesr->cesr_element_count) {
1352		xpt_print(periph->path,
1353		    "warning, READ ELEMENT STATUS avail != count\n");
1354	}
1355
1356	user_data = (struct changer_element_status *)
1357		malloc(avail * sizeof(struct changer_element_status),
1358		       M_DEVBUF, M_WAITOK | M_ZERO);
1359
1360	desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1361		sizeof(struct read_element_status_header) +
1362		sizeof(struct read_element_status_page_header));
1363	/*
1364	 * Set up the individual element status structures
1365	 */
1366	for (i = 0; i < avail; ++i) {
1367		struct changer_element_status *ces;
1368
1369		/*
1370		 * In the changer_element_status structure, fields from
1371		 * the beginning to the field of ces_scsi_lun are common
1372		 * between SCSI-2 and SCSI-3, while all the rest are new
1373		 * from SCSI-3. In order to maintain backward compatibility
1374		 * of the chio command, the ces pointer, below, is computed
1375		 * such that it lines up with the structure boundary
1376		 * corresponding to the SCSI version.
1377		 */
1378		ces = cmd == OCHIOGSTATUS ?
1379		    (struct changer_element_status *)
1380		    ((unsigned char *)user_data + i *
1381		     (offsetof(struct changer_element_status,ces_scsi_lun)+1)):
1382		    &user_data[i];
1383
1384		copy_element_status(softc, pg_hdr->flags, desc,
1385				    ces, scsi_version);
1386
1387		desc = (struct read_element_status_descriptor *)
1388		       ((unsigned char *)desc + desclen);
1389	}
1390
1391	/* Copy element status structures out to userspace. */
1392	if (cmd == OCHIOGSTATUS)
1393		error = copyout(user_data,
1394				cesr->cesr_element_status,
1395				avail* (offsetof(struct changer_element_status,
1396				ces_scsi_lun) + 1));
1397	else
1398		error = copyout(user_data,
1399				cesr->cesr_element_status,
1400				avail * sizeof(struct changer_element_status));
1401
1402	cam_periph_lock(periph);
1403
1404 done:
1405	xpt_release_ccb(ccb);
1406
1407	if (data != NULL)
1408		free(data, M_DEVBUF);
1409	if (user_data != NULL)
1410		free(user_data, M_DEVBUF);
1411
1412	return (error);
1413}
1414
1415static int
1416chielem(struct cam_periph *periph,
1417	unsigned int timeout)
1418{
1419	union ccb *ccb;
1420	struct ch_softc *softc;
1421	int error;
1422
1423	if (!timeout) {
1424		timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1425	} else {
1426		timeout *= 1000;
1427	}
1428
1429	error = 0;
1430	softc = (struct ch_softc *)periph->softc;
1431
1432	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1433
1434	scsi_initialize_element_status(&ccb->csio,
1435				      /* retries */ 1,
1436				      /* cbfcnp */ chdone,
1437				      /* tag_action */ MSG_SIMPLE_Q_TAG,
1438				      /* sense_len */ SSD_FULL_SIZE,
1439				      /* timeout */ timeout);
1440
1441	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1442				  /*sense_flags*/ SF_RETRY_UA,
1443				  softc->device_stats);
1444
1445	xpt_release_ccb(ccb);
1446
1447	return(error);
1448}
1449
1450static int
1451chsetvoltag(struct cam_periph *periph,
1452	    struct changer_set_voltag_request *csvr)
1453{
1454	union ccb *ccb;
1455	struct ch_softc *softc;
1456	u_int16_t ea;
1457	u_int8_t sac;
1458	struct scsi_send_volume_tag_parameters ssvtp;
1459	int error;
1460	int i;
1461
1462	error = 0;
1463	softc = (struct ch_softc *)periph->softc;
1464
1465	bzero(&ssvtp, sizeof(ssvtp));
1466	for (i=0; i<sizeof(ssvtp.vitf); i++) {
1467		ssvtp.vitf[i] = ' ';
1468	}
1469
1470	/*
1471	 * Check arguments.
1472	 */
1473	if (csvr->csvr_type > CHET_DT)
1474		return EINVAL;
1475	if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1476		return ENODEV;
1477
1478	ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1479
1480	if (csvr->csvr_flags & CSVR_ALTERNATE) {
1481		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1482		case CSVR_MODE_SET:
1483			sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1484			break;
1485		case CSVR_MODE_REPLACE:
1486			sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1487			break;
1488		case CSVR_MODE_CLEAR:
1489			sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1490			break;
1491		default:
1492			error = EINVAL;
1493			goto out;
1494		}
1495	} else {
1496		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1497		case CSVR_MODE_SET:
1498			sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1499			break;
1500		case CSVR_MODE_REPLACE:
1501			sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1502			break;
1503		case CSVR_MODE_CLEAR:
1504			sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1505			break;
1506		default:
1507			error = EINVAL;
1508			goto out;
1509		}
1510	}
1511
1512	memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1513	       min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1514	scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1515
1516	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1517
1518	scsi_send_volume_tag(&ccb->csio,
1519			     /* retries */ 1,
1520			     /* cbfcnp */ chdone,
1521			     /* tag_action */ MSG_SIMPLE_Q_TAG,
1522			     /* element_address */ ea,
1523			     /* send_action_code */ sac,
1524			     /* parameters */ &ssvtp,
1525			     /* sense_len */ SSD_FULL_SIZE,
1526			     /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1527
1528	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1529				  /*sense_flags*/ SF_RETRY_UA,
1530				  softc->device_stats);
1531
1532	xpt_release_ccb(ccb);
1533
1534 out:
1535	return error;
1536}
1537
1538static int
1539chgetparams(struct cam_periph *periph)
1540{
1541	union ccb *ccb;
1542	struct ch_softc *softc;
1543	void *mode_buffer;
1544	int mode_buffer_len;
1545	struct page_element_address_assignment *ea;
1546	struct page_device_capabilities *cap;
1547	int error, from, dbd;
1548	u_int8_t *moves, *exchanges;
1549
1550	error = 0;
1551
1552	softc = (struct ch_softc *)periph->softc;
1553
1554	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
1555
1556	/*
1557	 * The scsi_mode_sense_data structure is just a convenience
1558	 * structure that allows us to easily calculate the worst-case
1559	 * storage size of the mode sense buffer.
1560	 */
1561	mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1562
1563	mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1564
1565	if (mode_buffer == NULL) {
1566		printf("chgetparams: couldn't malloc mode sense data\n");
1567		xpt_release_ccb(ccb);
1568		return(ENOSPC);
1569	}
1570
1571	bzero(mode_buffer, mode_buffer_len);
1572
1573	if (softc->quirks & CH_Q_NO_DBD)
1574		dbd = FALSE;
1575	else
1576		dbd = TRUE;
1577
1578	/*
1579	 * Get the element address assignment page.
1580	 */
1581	scsi_mode_sense(&ccb->csio,
1582			/* retries */ 1,
1583			/* cbfcnp */ chdone,
1584			/* tag_action */ MSG_SIMPLE_Q_TAG,
1585			/* dbd */ dbd,
1586			/* pc */ SMS_PAGE_CTRL_CURRENT,
1587			/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1588			/* param_buf */ (u_int8_t *)mode_buffer,
1589			/* param_len */ mode_buffer_len,
1590			/* sense_len */ SSD_FULL_SIZE,
1591			/* timeout */ CH_TIMEOUT_MODE_SENSE);
1592
1593	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1594				  /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1595				  softc->device_stats);
1596
1597	if (error) {
1598		if (dbd) {
1599			struct scsi_mode_sense_6 *sms;
1600
1601			sms = (struct scsi_mode_sense_6 *)
1602				ccb->csio.cdb_io.cdb_bytes;
1603
1604			sms->byte2 &= ~SMS_DBD;
1605			error = cam_periph_runccb(ccb, cherror,
1606						  /*cam_flags*/ CAM_RETRY_SELTO,
1607				  		  /*sense_flags*/ SF_RETRY_UA,
1608						  softc->device_stats);
1609		} else {
1610			/*
1611			 * Since we disabled sense printing above, print
1612			 * out the sense here since we got an error.
1613			 */
1614			scsi_sense_print(&ccb->csio);
1615		}
1616
1617		if (error) {
1618			xpt_print(periph->path,
1619			    "chgetparams: error getting element "
1620			    "address page\n");
1621			xpt_release_ccb(ccb);
1622			free(mode_buffer, M_SCSICH);
1623			return(error);
1624		}
1625	}
1626
1627	ea = (struct page_element_address_assignment *)
1628		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1629
1630	softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1631	softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1632	softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1633	softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1634	softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1635	softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1636	softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1637	softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1638
1639	bzero(mode_buffer, mode_buffer_len);
1640
1641	/*
1642	 * Now get the device capabilities page.
1643	 */
1644	scsi_mode_sense(&ccb->csio,
1645			/* retries */ 1,
1646			/* cbfcnp */ chdone,
1647			/* tag_action */ MSG_SIMPLE_Q_TAG,
1648			/* dbd */ dbd,
1649			/* pc */ SMS_PAGE_CTRL_CURRENT,
1650			/* page */ CH_DEVICE_CAP_PAGE,
1651			/* param_buf */ (u_int8_t *)mode_buffer,
1652			/* param_len */ mode_buffer_len,
1653			/* sense_len */ SSD_FULL_SIZE,
1654			/* timeout */ CH_TIMEOUT_MODE_SENSE);
1655
1656	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1657				  /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1658				  softc->device_stats);
1659
1660	if (error) {
1661		if (dbd) {
1662			struct scsi_mode_sense_6 *sms;
1663
1664			sms = (struct scsi_mode_sense_6 *)
1665				ccb->csio.cdb_io.cdb_bytes;
1666
1667			sms->byte2 &= ~SMS_DBD;
1668			error = cam_periph_runccb(ccb, cherror,
1669						  /*cam_flags*/ CAM_RETRY_SELTO,
1670				  		  /*sense_flags*/ SF_RETRY_UA,
1671						  softc->device_stats);
1672		} else {
1673			/*
1674			 * Since we disabled sense printing above, print
1675			 * out the sense here since we got an error.
1676			 */
1677			scsi_sense_print(&ccb->csio);
1678		}
1679
1680		if (error) {
1681			xpt_print(periph->path,
1682			    "chgetparams: error getting device "
1683			    "capabilities page\n");
1684			xpt_release_ccb(ccb);
1685			free(mode_buffer, M_SCSICH);
1686			return(error);
1687		}
1688	}
1689
1690	xpt_release_ccb(ccb);
1691
1692	cap = (struct page_device_capabilities *)
1693		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1694
1695	bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1696	bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1697	moves = cap->move_from;
1698	exchanges = cap->exchange_with;
1699	for (from = CHET_MT; from <= CHET_MAX; ++from) {
1700		softc->sc_movemask[from] = moves[from];
1701		softc->sc_exchangemask[from] = exchanges[from];
1702	}
1703
1704	free(mode_buffer, M_SCSICH);
1705
1706	return(error);
1707}
1708
1709static int
1710chscsiversion(struct cam_periph *periph)
1711{
1712	struct scsi_inquiry_data *inq_data;
1713	struct ccb_getdev *cgd;
1714	int dev_scsi_version;
1715
1716	cam_periph_assert(periph, MA_OWNED);
1717	if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) == NULL)
1718		return (-1);
1719	/*
1720	 * Get the device information.
1721	 */
1722	xpt_setup_ccb(&cgd->ccb_h,
1723		      periph->path,
1724		      CAM_PRIORITY_NORMAL);
1725	cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1726	xpt_action((union ccb *)cgd);
1727
1728	if (cgd->ccb_h.status != CAM_REQ_CMP) {
1729		xpt_free_ccb((union ccb *)cgd);
1730		return -1;
1731	}
1732
1733	inq_data = &cgd->inq_data;
1734	dev_scsi_version = inq_data->version;
1735	xpt_free_ccb((union ccb *)cgd);
1736
1737	return dev_scsi_version;
1738}
1739
1740void
1741scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1742		 void (*cbfcnp)(struct cam_periph *, union ccb *),
1743		 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1744		 u_int32_t dst, int invert, u_int8_t sense_len,
1745		 u_int32_t timeout)
1746{
1747	struct scsi_move_medium *scsi_cmd;
1748
1749	scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1750	bzero(scsi_cmd, sizeof(*scsi_cmd));
1751
1752	scsi_cmd->opcode = MOVE_MEDIUM;
1753
1754	scsi_ulto2b(tea, scsi_cmd->tea);
1755	scsi_ulto2b(src, scsi_cmd->src);
1756	scsi_ulto2b(dst, scsi_cmd->dst);
1757
1758	if (invert)
1759		scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1760
1761	cam_fill_csio(csio,
1762		      retries,
1763		      cbfcnp,
1764		      /*flags*/ CAM_DIR_NONE,
1765		      tag_action,
1766		      /*data_ptr*/ NULL,
1767		      /*dxfer_len*/ 0,
1768		      sense_len,
1769		      sizeof(*scsi_cmd),
1770		      timeout);
1771}
1772
1773void
1774scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1775		     void (*cbfcnp)(struct cam_periph *, union ccb *),
1776		     u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1777		     u_int32_t dst1, u_int32_t dst2, int invert1,
1778		     int invert2, u_int8_t sense_len, u_int32_t timeout)
1779{
1780	struct scsi_exchange_medium *scsi_cmd;
1781
1782	scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1783	bzero(scsi_cmd, sizeof(*scsi_cmd));
1784
1785	scsi_cmd->opcode = EXCHANGE_MEDIUM;
1786
1787	scsi_ulto2b(tea, scsi_cmd->tea);
1788	scsi_ulto2b(src, scsi_cmd->src);
1789	scsi_ulto2b(dst1, scsi_cmd->fdst);
1790	scsi_ulto2b(dst2, scsi_cmd->sdst);
1791
1792	if (invert1)
1793		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1794
1795	if (invert2)
1796		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1797
1798	cam_fill_csio(csio,
1799		      retries,
1800		      cbfcnp,
1801		      /*flags*/ CAM_DIR_NONE,
1802		      tag_action,
1803		      /*data_ptr*/ NULL,
1804		      /*dxfer_len*/ 0,
1805		      sense_len,
1806		      sizeof(*scsi_cmd),
1807		      timeout);
1808}
1809
1810void
1811scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1812			 void (*cbfcnp)(struct cam_periph *, union ccb *),
1813			 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1814			 int invert, u_int8_t sense_len, u_int32_t timeout)
1815{
1816	struct scsi_position_to_element *scsi_cmd;
1817
1818	scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1819	bzero(scsi_cmd, sizeof(*scsi_cmd));
1820
1821	scsi_cmd->opcode = POSITION_TO_ELEMENT;
1822
1823	scsi_ulto2b(tea, scsi_cmd->tea);
1824	scsi_ulto2b(dst, scsi_cmd->dst);
1825
1826	if (invert)
1827		scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1828
1829	cam_fill_csio(csio,
1830		      retries,
1831		      cbfcnp,
1832		      /*flags*/ CAM_DIR_NONE,
1833		      tag_action,
1834		      /*data_ptr*/ NULL,
1835		      /*dxfer_len*/ 0,
1836		      sense_len,
1837		      sizeof(*scsi_cmd),
1838		      timeout);
1839}
1840
1841void
1842scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1843			 void (*cbfcnp)(struct cam_periph *, union ccb *),
1844			 u_int8_t tag_action, int voltag, u_int32_t sea,
1845			 int curdata, int dvcid,
1846			 u_int32_t count, u_int8_t *data_ptr,
1847			 u_int32_t dxfer_len, u_int8_t sense_len,
1848			 u_int32_t timeout)
1849{
1850	struct scsi_read_element_status *scsi_cmd;
1851
1852	scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1853	bzero(scsi_cmd, sizeof(*scsi_cmd));
1854
1855	scsi_cmd->opcode = READ_ELEMENT_STATUS;
1856
1857	scsi_ulto2b(sea, scsi_cmd->sea);
1858	scsi_ulto2b(count, scsi_cmd->count);
1859	scsi_ulto3b(dxfer_len, scsi_cmd->len);
1860	if (dvcid)
1861		scsi_cmd->flags |= READ_ELEMENT_STATUS_DVCID;
1862	if (curdata)
1863		scsi_cmd->flags |= READ_ELEMENT_STATUS_CURDATA;
1864
1865	if (voltag)
1866		scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1867
1868	cam_fill_csio(csio,
1869		      retries,
1870		      cbfcnp,
1871		      /*flags*/ CAM_DIR_IN,
1872		      tag_action,
1873		      data_ptr,
1874		      dxfer_len,
1875		      sense_len,
1876		      sizeof(*scsi_cmd),
1877		      timeout);
1878}
1879
1880void
1881scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1882			       void (*cbfcnp)(struct cam_periph *, union ccb *),
1883			       u_int8_t tag_action, u_int8_t sense_len,
1884			       u_int32_t timeout)
1885{
1886	struct scsi_initialize_element_status *scsi_cmd;
1887
1888	scsi_cmd = (struct scsi_initialize_element_status *)
1889		    &csio->cdb_io.cdb_bytes;
1890	bzero(scsi_cmd, sizeof(*scsi_cmd));
1891
1892	scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1893
1894	cam_fill_csio(csio,
1895		      retries,
1896		      cbfcnp,
1897		      /*flags*/ CAM_DIR_NONE,
1898		      tag_action,
1899		      /* data_ptr */ NULL,
1900		      /* dxfer_len */ 0,
1901		      sense_len,
1902		      sizeof(*scsi_cmd),
1903		      timeout);
1904}
1905
1906void
1907scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1908		     void (*cbfcnp)(struct cam_periph *, union ccb *),
1909		     u_int8_t tag_action,
1910		     u_int16_t element_address,
1911		     u_int8_t send_action_code,
1912		     struct scsi_send_volume_tag_parameters *parameters,
1913		     u_int8_t sense_len, u_int32_t timeout)
1914{
1915	struct scsi_send_volume_tag *scsi_cmd;
1916
1917	scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1918	bzero(scsi_cmd, sizeof(*scsi_cmd));
1919
1920	scsi_cmd->opcode = SEND_VOLUME_TAG;
1921	scsi_ulto2b(element_address, scsi_cmd->ea);
1922	scsi_cmd->sac = send_action_code;
1923	scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1924
1925	cam_fill_csio(csio,
1926		      retries,
1927		      cbfcnp,
1928		      /*flags*/ CAM_DIR_OUT,
1929		      tag_action,
1930		      /* data_ptr */ (u_int8_t *) parameters,
1931		      sizeof(*parameters),
1932		      sense_len,
1933		      sizeof(*scsi_cmd),
1934		      timeout);
1935}
1936