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