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