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