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