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