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