xref: /freebsd/sys/cam/scsi/scsi_ch.c (revision c552ebe12dd9f65e195a4b2835ec4b57e6bc79ba)
1898b0535SWarner Losh /*-
276babe50SJustin T. Gibbs  * Copyright (c) 1997 Justin T. Gibbs.
32a888f93SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
476babe50SJustin T. Gibbs  * All rights reserved.
576babe50SJustin T. Gibbs  *
676babe50SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
776babe50SJustin T. Gibbs  * modification, are permitted provided that the following conditions
876babe50SJustin T. Gibbs  * are met:
976babe50SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
1076babe50SJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
1176babe50SJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
1276babe50SJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
1376babe50SJustin T. Gibbs  *    derived from this software without specific prior written permission.
1476babe50SJustin T. Gibbs  *
1576babe50SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1676babe50SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1776babe50SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1876babe50SJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1976babe50SJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2076babe50SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2176babe50SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2276babe50SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2376babe50SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2476babe50SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2576babe50SJustin T. Gibbs  * SUCH DAMAGE.
2676babe50SJustin T. Gibbs  */
27ee709e70SDavid E. O'Brien 
2876babe50SJustin T. Gibbs /*
2976babe50SJustin T. Gibbs  * Derived from the NetBSD SCSI changer driver.
3076babe50SJustin T. Gibbs  *
3176babe50SJustin T. Gibbs  *	$NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
3276babe50SJustin T. Gibbs  *
3376babe50SJustin T. Gibbs  */
34898b0535SWarner Losh /*-
3576babe50SJustin T. Gibbs  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
3676babe50SJustin T. Gibbs  * All rights reserved.
3776babe50SJustin T. Gibbs  *
3876babe50SJustin T. Gibbs  * Partially based on an autochanger driver written by Stefan Grefen
3976babe50SJustin T. Gibbs  * and on an autochanger driver written by the Systems Programming Group
4076babe50SJustin T. Gibbs  * at the University of Utah Computer Science Department.
4176babe50SJustin T. Gibbs  *
4276babe50SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
4376babe50SJustin T. Gibbs  * modification, are permitted provided that the following conditions
4476babe50SJustin T. Gibbs  * are met:
4576babe50SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
4676babe50SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
4776babe50SJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
4876babe50SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
4976babe50SJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
5076babe50SJustin T. Gibbs  * 3. All advertising materials mentioning features or use of this software
5176babe50SJustin T. Gibbs  *    must display the following acknowledgements:
5276babe50SJustin T. Gibbs  *	This product includes software developed by Jason R. Thorpe
5376babe50SJustin T. Gibbs  *	for And Communications, http://www.and.com/
5476babe50SJustin T. Gibbs  * 4. The name of the author may not be used to endorse or promote products
5576babe50SJustin T. Gibbs  *    derived from this software without specific prior written permission.
5676babe50SJustin T. Gibbs  *
5776babe50SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5876babe50SJustin T. Gibbs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5976babe50SJustin T. Gibbs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6076babe50SJustin T. Gibbs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6176babe50SJustin T. Gibbs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
6276babe50SJustin T. Gibbs  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
6376babe50SJustin T. Gibbs  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
6476babe50SJustin T. Gibbs  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6576babe50SJustin T. Gibbs  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6676babe50SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6776babe50SJustin T. Gibbs  * SUCH DAMAGE.
6876babe50SJustin T. Gibbs  */
6976babe50SJustin T. Gibbs 
70ee709e70SDavid E. O'Brien #include <sys/cdefs.h>
71ee709e70SDavid E. O'Brien __FBSDID("$FreeBSD$");
72ee709e70SDavid E. O'Brien 
7376babe50SJustin T. Gibbs #include <sys/param.h>
7476babe50SJustin T. Gibbs #include <sys/queue.h>
7576babe50SJustin T. Gibbs #include <sys/systm.h>
7676babe50SJustin T. Gibbs #include <sys/kernel.h>
7776babe50SJustin T. Gibbs #include <sys/types.h>
7876babe50SJustin T. Gibbs #include <sys/malloc.h>
7976babe50SJustin T. Gibbs #include <sys/fcntl.h>
8076babe50SJustin T. Gibbs #include <sys/conf.h>
8176babe50SJustin T. Gibbs #include <sys/chio.h>
8276babe50SJustin T. Gibbs #include <sys/errno.h>
8376babe50SJustin T. Gibbs #include <sys/devicestat.h>
8476babe50SJustin T. Gibbs 
8576babe50SJustin T. Gibbs #include <cam/cam.h>
8676babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
8776babe50SJustin T. Gibbs #include <cam/cam_periph.h>
8876babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
8976babe50SJustin T. Gibbs #include <cam/cam_debug.h>
9076babe50SJustin T. Gibbs 
9176babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
9276babe50SJustin T. Gibbs #include <cam/scsi/scsi_message.h>
9376babe50SJustin T. Gibbs #include <cam/scsi/scsi_ch.h>
9476babe50SJustin T. Gibbs 
9576babe50SJustin T. Gibbs /*
9676babe50SJustin T. Gibbs  * Timeout definitions for various changer related commands.  They may
9776babe50SJustin T. Gibbs  * be too short for some devices (especially the timeout for INITIALIZE
9876babe50SJustin T. Gibbs  * ELEMENT STATUS).
9976babe50SJustin T. Gibbs  */
10076babe50SJustin T. Gibbs 
1013c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_MODE_SENSE                = 6000;
1023c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_MOVE_MEDIUM               = 100000;
1033c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_EXCHANGE_MEDIUM           = 100000;
1043c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_POSITION_TO_ELEMENT       = 100000;
1053c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_READ_ELEMENT_STATUS       = 10000;
1063c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_SEND_VOLTAG		     = 10000;
1073c68dd1aSEivind Eklund static const u_int32_t	CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
10876babe50SJustin T. Gibbs 
10976babe50SJustin T. Gibbs typedef enum {
110*c552ebe1SKenneth D. Merry 	CH_FLAG_INVALID		= 0x001
11176babe50SJustin T. Gibbs } ch_flags;
11276babe50SJustin T. Gibbs 
11376babe50SJustin T. Gibbs typedef enum {
11476babe50SJustin T. Gibbs 	CH_STATE_PROBE,
11576babe50SJustin T. Gibbs 	CH_STATE_NORMAL
11676babe50SJustin T. Gibbs } ch_state;
11776babe50SJustin T. Gibbs 
11876babe50SJustin T. Gibbs typedef enum {
11976babe50SJustin T. Gibbs 	CH_CCB_PROBE,
12076babe50SJustin T. Gibbs 	CH_CCB_WAITING
12176babe50SJustin T. Gibbs } ch_ccb_types;
12276babe50SJustin T. Gibbs 
123696db222SKenneth D. Merry typedef enum {
124696db222SKenneth D. Merry 	CH_Q_NONE	= 0x00,
125696db222SKenneth D. Merry 	CH_Q_NO_DBD	= 0x01
126696db222SKenneth D. Merry } ch_quirks;
127696db222SKenneth D. Merry 
12876babe50SJustin T. Gibbs #define ccb_state	ppriv_field0
12976babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
13076babe50SJustin T. Gibbs 
13176babe50SJustin T. Gibbs struct scsi_mode_sense_data {
13276babe50SJustin T. Gibbs 	struct scsi_mode_header_6 header;
133696db222SKenneth D. Merry 	struct scsi_mode_blk_desc blk_desc;
13476babe50SJustin T. Gibbs 	union {
13576babe50SJustin T. Gibbs 		struct page_element_address_assignment ea;
13676babe50SJustin T. Gibbs 		struct page_transport_geometry_parameters tg;
13776babe50SJustin T. Gibbs 		struct page_device_capabilities cap;
13876babe50SJustin T. Gibbs 	} pages;
13976babe50SJustin T. Gibbs };
14076babe50SJustin T. Gibbs 
14176babe50SJustin T. Gibbs struct ch_softc {
14276babe50SJustin T. Gibbs 	ch_flags	flags;
14376babe50SJustin T. Gibbs 	ch_state	state;
144696db222SKenneth D. Merry 	ch_quirks	quirks;
14576babe50SJustin T. Gibbs 	union ccb	saved_ccb;
146a9d2245eSPoul-Henning Kamp 	struct devstat	*device_stats;
14789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
14876babe50SJustin T. Gibbs 
14976babe50SJustin T. Gibbs 	int		sc_picker;	/* current picker */
15076babe50SJustin T. Gibbs 
15176babe50SJustin T. Gibbs 	/*
15276babe50SJustin T. Gibbs 	 * The following information is obtained from the
15376babe50SJustin T. Gibbs 	 * element address assignment page.
15476babe50SJustin T. Gibbs 	 */
15556824e13SPoul-Henning Kamp 	int		sc_firsts[CHET_MAX + 1];	/* firsts */
15656824e13SPoul-Henning Kamp 	int		sc_counts[CHET_MAX + 1];	/* counts */
15776babe50SJustin T. Gibbs 
15876babe50SJustin T. Gibbs 	/*
15976babe50SJustin T. Gibbs 	 * The following mask defines the legal combinations
16076babe50SJustin T. Gibbs 	 * of elements for the MOVE MEDIUM command.
16176babe50SJustin T. Gibbs 	 */
16256824e13SPoul-Henning Kamp 	u_int8_t	sc_movemask[CHET_MAX + 1];
16376babe50SJustin T. Gibbs 
16476babe50SJustin T. Gibbs 	/*
16576babe50SJustin T. Gibbs 	 * As above, but for EXCHANGE MEDIUM.
16676babe50SJustin T. Gibbs 	 */
16756824e13SPoul-Henning Kamp 	u_int8_t	sc_exchangemask[CHET_MAX + 1];
16876babe50SJustin T. Gibbs 
16976babe50SJustin T. Gibbs 	/*
17076babe50SJustin T. Gibbs 	 * Quirks; see below.  XXX KDM not implemented yet
17176babe50SJustin T. Gibbs 	 */
17276babe50SJustin T. Gibbs 	int		sc_settledelay;	/* delay for settle */
17376babe50SJustin T. Gibbs };
17476babe50SJustin T. Gibbs 
17576babe50SJustin T. Gibbs static	d_open_t	chopen;
17676babe50SJustin T. Gibbs static	d_close_t	chclose;
17776babe50SJustin T. Gibbs static	d_ioctl_t	chioctl;
17876babe50SJustin T. Gibbs static	periph_init_t	chinit;
17976babe50SJustin T. Gibbs static  periph_ctor_t	chregister;
180ee9c90c7SKenneth D. Merry static	periph_oninv_t	choninvalidate;
18176babe50SJustin T. Gibbs static  periph_dtor_t   chcleanup;
18276babe50SJustin T. Gibbs static  periph_start_t  chstart;
18376babe50SJustin T. Gibbs static	void		chasync(void *callback_arg, u_int32_t code,
18476babe50SJustin T. Gibbs 				struct cam_path *path, void *arg);
18576babe50SJustin T. Gibbs static	void		chdone(struct cam_periph *periph,
18676babe50SJustin T. Gibbs 			       union ccb *done_ccb);
18776babe50SJustin T. Gibbs static	int		cherror(union ccb *ccb, u_int32_t cam_flags,
18876babe50SJustin T. Gibbs 				u_int32_t sense_flags);
18976babe50SJustin T. Gibbs static	int		chmove(struct cam_periph *periph,
19076babe50SJustin T. Gibbs 			       struct changer_move *cm);
19176babe50SJustin T. Gibbs static	int		chexchange(struct cam_periph *periph,
19276babe50SJustin T. Gibbs 				   struct changer_exchange *ce);
19376babe50SJustin T. Gibbs static	int		chposition(struct cam_periph *periph,
19476babe50SJustin T. Gibbs 				   struct changer_position *cp);
19576babe50SJustin T. Gibbs static	int		chgetelemstatus(struct cam_periph *periph,
19676babe50SJustin T. Gibbs 				struct changer_element_status_request *csr);
19776babe50SJustin T. Gibbs static	int		chsetvoltag(struct cam_periph *periph,
19876babe50SJustin T. Gibbs 				    struct changer_set_voltag_request *csvr);
19976babe50SJustin T. Gibbs static	int		chielem(struct cam_periph *periph,
20076babe50SJustin T. Gibbs 				unsigned int timeout);
20176babe50SJustin T. Gibbs static	int		chgetparams(struct cam_periph *periph);
20276babe50SJustin T. Gibbs 
20376babe50SJustin T. Gibbs static struct periph_driver chdriver =
20476babe50SJustin T. Gibbs {
20576babe50SJustin T. Gibbs 	chinit, "ch",
20676babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
20776babe50SJustin T. Gibbs };
20876babe50SJustin T. Gibbs 
2090b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(ch, chdriver);
21076babe50SJustin T. Gibbs 
2114e2f199eSPoul-Henning Kamp static struct cdevsw ch_cdevsw = {
212dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
213*c552ebe1SKenneth D. Merry 	.d_flags =	D_TRACKCLOSE,
2147ac40f5fSPoul-Henning Kamp 	.d_open =	chopen,
2157ac40f5fSPoul-Henning Kamp 	.d_close =	chclose,
2167ac40f5fSPoul-Henning Kamp 	.d_ioctl =	chioctl,
2177ac40f5fSPoul-Henning Kamp 	.d_name =	"ch",
21876babe50SJustin T. Gibbs };
21976babe50SJustin T. Gibbs 
220d745c852SEd Schouten static MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
2210dd50e9bSScott Long 
22237c84183SPoul-Henning Kamp static void
22376babe50SJustin T. Gibbs chinit(void)
22476babe50SJustin T. Gibbs {
22576babe50SJustin T. Gibbs 	cam_status status;
22676babe50SJustin T. Gibbs 
22776babe50SJustin T. Gibbs 	/*
22876babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
22976babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
23076babe50SJustin T. Gibbs 	 */
23185d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
23276babe50SJustin T. Gibbs 
23376babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
23476babe50SJustin T. Gibbs 		printf("ch: Failed to attach master async callback "
23576babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
23676babe50SJustin T. Gibbs 	}
23776babe50SJustin T. Gibbs }
23876babe50SJustin T. Gibbs 
23976babe50SJustin T. Gibbs static void
240ee9c90c7SKenneth D. Merry choninvalidate(struct cam_periph *periph)
241ee9c90c7SKenneth D. Merry {
242ee9c90c7SKenneth D. Merry 	struct ch_softc *softc;
243ee9c90c7SKenneth D. Merry 
244ee9c90c7SKenneth D. Merry 	softc = (struct ch_softc *)periph->softc;
245ee9c90c7SKenneth D. Merry 
246ee9c90c7SKenneth D. Merry 	/*
247ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
248ee9c90c7SKenneth D. Merry 	 */
24985d92640SScott Long 	xpt_register_async(0, chasync, periph, periph->path);
250ee9c90c7SKenneth D. Merry 
251ee9c90c7SKenneth D. Merry 	softc->flags |= CH_FLAG_INVALID;
252ee9c90c7SKenneth D. Merry 
253f0d9af51SMatt Jacob 	xpt_print(periph->path, "lost device\n");
254ee9c90c7SKenneth D. Merry 
255ee9c90c7SKenneth D. Merry }
256ee9c90c7SKenneth D. Merry 
257ee9c90c7SKenneth D. Merry static void
25876babe50SJustin T. Gibbs chcleanup(struct cam_periph *periph)
25976babe50SJustin T. Gibbs {
260ee9c90c7SKenneth D. Merry 	struct ch_softc *softc;
26176babe50SJustin T. Gibbs 
262ee9c90c7SKenneth D. Merry 	softc = (struct ch_softc *)periph->softc;
263ee9c90c7SKenneth D. Merry 
264f0d9af51SMatt Jacob 	xpt_print(periph->path, "removing device entry\n");
2655f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
2665f3fed85SEdward Tomasz Napierala 	cam_periph_unlock(periph);
2675f3fed85SEdward Tomasz Napierala 	destroy_dev(softc->dev);
2685f3fed85SEdward Tomasz Napierala 	cam_periph_lock(periph);
269ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
27076babe50SJustin T. Gibbs }
27176babe50SJustin T. Gibbs 
27276babe50SJustin T. Gibbs static void
27376babe50SJustin T. Gibbs chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
27476babe50SJustin T. Gibbs {
27576babe50SJustin T. Gibbs 	struct cam_periph *periph;
27676babe50SJustin T. Gibbs 
27776babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
27876babe50SJustin T. Gibbs 
27976babe50SJustin T. Gibbs 	switch(code) {
28076babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
28176babe50SJustin T. Gibbs 	{
28276babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
28376babe50SJustin T. Gibbs 		cam_status status;
28476babe50SJustin T. Gibbs 
28576babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
286c5ff3b2fSMatt Jacob 		if (cgd == NULL)
287c5ff3b2fSMatt Jacob 			break;
28876babe50SJustin T. Gibbs 
28952c9ce25SScott Long 		if (cgd->protocol != PROTO_SCSI)
29052c9ce25SScott Long 			break;
29152c9ce25SScott Long 
29210b6172aSMatt Jacob 		if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
29376babe50SJustin T. Gibbs 			break;
29476babe50SJustin T. Gibbs 
29576babe50SJustin T. Gibbs 		/*
29676babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
29776babe50SJustin T. Gibbs 		 * this device and start the probe
29876babe50SJustin T. Gibbs 		 * process.
29976babe50SJustin T. Gibbs 		 */
300ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(chregister, choninvalidate,
301ee9c90c7SKenneth D. Merry 					  chcleanup, chstart, "ch",
302ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
30376babe50SJustin T. Gibbs 					  chasync, AC_FOUND_DEVICE, cgd);
30476babe50SJustin T. Gibbs 
30576babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
30676babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
30776babe50SJustin T. Gibbs 			printf("chasync: Unable to probe new device "
30876babe50SJustin T. Gibbs 			       "due to status 0x%x\n", status);
30976babe50SJustin T. Gibbs 
31076babe50SJustin T. Gibbs 		break;
31176babe50SJustin T. Gibbs 
31276babe50SJustin T. Gibbs 	}
31376babe50SJustin T. Gibbs 	default:
314516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
31576babe50SJustin T. Gibbs 		break;
31676babe50SJustin T. Gibbs 	}
31776babe50SJustin T. Gibbs }
31876babe50SJustin T. Gibbs 
31976babe50SJustin T. Gibbs static cam_status
32076babe50SJustin T. Gibbs chregister(struct cam_periph *periph, void *arg)
32176babe50SJustin T. Gibbs {
32276babe50SJustin T. Gibbs 	struct ch_softc *softc;
32376babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
324b8b6b5d3SAlexander Motin 	struct ccb_pathinq cpi;
32576babe50SJustin T. Gibbs 
32676babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
32776babe50SJustin T. Gibbs 	if (periph == NULL) {
32876babe50SJustin T. Gibbs 		printf("chregister: periph was NULL!!\n");
32976babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
33076babe50SJustin T. Gibbs 	}
33176babe50SJustin T. Gibbs 
33276babe50SJustin T. Gibbs 	if (cgd == NULL) {
33376babe50SJustin T. Gibbs 		printf("chregister: no getdev CCB, can't register device\n");
33476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
33576babe50SJustin T. Gibbs 	}
33676babe50SJustin T. Gibbs 
33776babe50SJustin T. Gibbs 	softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
33876babe50SJustin T. Gibbs 
33976babe50SJustin T. Gibbs 	if (softc == NULL) {
34076babe50SJustin T. Gibbs 		printf("chregister: Unable to probe new device. "
34176babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
34276babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
34376babe50SJustin T. Gibbs 	}
34476babe50SJustin T. Gibbs 
34576babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
34676babe50SJustin T. Gibbs 	softc->state = CH_STATE_PROBE;
34776babe50SJustin T. Gibbs 	periph->softc = softc;
348696db222SKenneth D. Merry 	softc->quirks = CH_Q_NONE;
34976babe50SJustin T. Gibbs 
350b8b6b5d3SAlexander Motin 	bzero(&cpi, sizeof(cpi));
351b8b6b5d3SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
352b8b6b5d3SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
353b8b6b5d3SAlexander Motin 	xpt_action((union ccb *)&cpi);
354b8b6b5d3SAlexander Motin 
35576babe50SJustin T. Gibbs 	/*
35676babe50SJustin T. Gibbs 	 * Changers don't have a blocksize, and obviously don't support
35776babe50SJustin T. Gibbs 	 * tagged queueing.
35876babe50SJustin T. Gibbs 	 */
35985d92640SScott Long 	cam_periph_unlock(periph);
360a9d2245eSPoul-Henning Kamp 	softc->device_stats = devstat_new_entry("ch",
36176babe50SJustin T. Gibbs 			  periph->unit_number, 0,
36276babe50SJustin T. Gibbs 			  DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
363b8b6b5d3SAlexander Motin 			  SID_TYPE(&cgd->inq_data) |
364b8b6b5d3SAlexander Motin 			  XPORT_DEVSTAT_TYPE(cpi.transport),
3652a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_OTHER);
36676babe50SJustin T. Gibbs 
3672728bfbdSMatt Jacob 	/* Register the device */
3682728bfbdSMatt Jacob 	softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
3692728bfbdSMatt Jacob 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
3702728bfbdSMatt Jacob 			      periph->unit_number);
3712b83592fSScott Long 	cam_periph_lock(periph);
372e2a5fdf9SNate Lawson 	softc->dev->si_drv1 = periph;
3732728bfbdSMatt Jacob 
37476babe50SJustin T. Gibbs 	/*
37576babe50SJustin T. Gibbs 	 * Add an async callback so that we get
37676babe50SJustin T. Gibbs 	 * notified if this device goes away.
37776babe50SJustin T. Gibbs 	 */
37885d92640SScott Long 	xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
37976babe50SJustin T. Gibbs 
38076babe50SJustin T. Gibbs 	/*
3812b83592fSScott Long 	 * Lock this periph until we are setup.
38276babe50SJustin T. Gibbs 	 * This first call can't block
38376babe50SJustin T. Gibbs 	 */
3842b83592fSScott Long 	(void)cam_periph_hold(periph, PRIBIO);
38583c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_DEV);
38676babe50SJustin T. Gibbs 
38776babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
38876babe50SJustin T. Gibbs }
38976babe50SJustin T. Gibbs 
39076babe50SJustin T. Gibbs static int
39189c9c53dSPoul-Henning Kamp chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
39276babe50SJustin T. Gibbs {
39376babe50SJustin T. Gibbs 	struct cam_periph *periph;
39476babe50SJustin T. Gibbs 	struct ch_softc *softc;
395e2a5fdf9SNate Lawson 	int error;
39676babe50SJustin T. Gibbs 
397e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
3982b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
39976babe50SJustin T. Gibbs 		return (ENXIO);
40076babe50SJustin T. Gibbs 
40176babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
40276babe50SJustin T. Gibbs 
4032b83592fSScott Long 	cam_periph_lock(periph);
4042b83592fSScott Long 
405ee9c90c7SKenneth D. Merry 	if (softc->flags & CH_FLAG_INVALID) {
406*c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
4072b83592fSScott Long 		cam_periph_unlock(periph);
40876babe50SJustin T. Gibbs 		return(ENXIO);
409ee9c90c7SKenneth D. Merry 	}
41076babe50SJustin T. Gibbs 
4112b83592fSScott Long 	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
4122b83592fSScott Long 		cam_periph_unlock(periph);
4132b83592fSScott Long 		cam_periph_release(periph);
4142b83592fSScott Long 		return (error);
41576babe50SJustin T. Gibbs 	}
41676babe50SJustin T. Gibbs 
41776babe50SJustin T. Gibbs 	/*
41876babe50SJustin T. Gibbs 	 * Load information about this changer device into the softc.
41976babe50SJustin T. Gibbs 	 */
42076babe50SJustin T. Gibbs 	if ((error = chgetparams(periph)) != 0) {
421*c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
42276babe50SJustin T. Gibbs 		cam_periph_unlock(periph);
42376babe50SJustin T. Gibbs 		return(error);
42476babe50SJustin T. Gibbs 	}
42576babe50SJustin T. Gibbs 
4262b83592fSScott Long 	cam_periph_unhold(periph);
42776babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
42876babe50SJustin T. Gibbs 
42976babe50SJustin T. Gibbs 	return(error);
43076babe50SJustin T. Gibbs }
43176babe50SJustin T. Gibbs 
43276babe50SJustin T. Gibbs static int
43389c9c53dSPoul-Henning Kamp chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
43476babe50SJustin T. Gibbs {
43576babe50SJustin T. Gibbs 	struct	cam_periph *periph;
43676babe50SJustin T. Gibbs 	struct	ch_softc *softc;
437e2a5fdf9SNate Lawson 	int	error;
43876babe50SJustin T. Gibbs 
43976babe50SJustin T. Gibbs 	error = 0;
44076babe50SJustin T. Gibbs 
441e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
44276babe50SJustin T. Gibbs 	if (periph == NULL)
44376babe50SJustin T. Gibbs 		return(ENXIO);
44476babe50SJustin T. Gibbs 
44576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
44676babe50SJustin T. Gibbs 
44776babe50SJustin T. Gibbs 	cam_periph_release(periph);
44876babe50SJustin T. Gibbs 
44976babe50SJustin T. Gibbs 	return(0);
45076babe50SJustin T. Gibbs }
45176babe50SJustin T. Gibbs 
45276babe50SJustin T. Gibbs static void
45376babe50SJustin T. Gibbs chstart(struct cam_periph *periph, union ccb *start_ccb)
45476babe50SJustin T. Gibbs {
45576babe50SJustin T. Gibbs 	struct ch_softc *softc;
45676babe50SJustin T. Gibbs 
45776babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
45876babe50SJustin T. Gibbs 
45976babe50SJustin T. Gibbs 	switch (softc->state) {
46076babe50SJustin T. Gibbs 	case CH_STATE_NORMAL:
46176babe50SJustin T. Gibbs 	{
46276babe50SJustin T. Gibbs 		if (periph->immediate_priority <= periph->pinfo.priority){
46376babe50SJustin T. Gibbs 			start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
46476babe50SJustin T. Gibbs 
46576babe50SJustin T. Gibbs 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
46676babe50SJustin T. Gibbs 					  periph_links.sle);
46776babe50SJustin T. Gibbs 			periph->immediate_priority = CAM_PRIORITY_NONE;
46876babe50SJustin T. Gibbs 			wakeup(&periph->ccb_list);
4692b83592fSScott Long 		}
47076babe50SJustin T. Gibbs 		break;
47176babe50SJustin T. Gibbs 	}
47276babe50SJustin T. Gibbs 	case CH_STATE_PROBE:
47376babe50SJustin T. Gibbs 	{
474696db222SKenneth D. Merry 		int mode_buffer_len;
475696db222SKenneth D. Merry 		void *mode_buffer;
47676babe50SJustin T. Gibbs 
477696db222SKenneth D. Merry 		/*
478696db222SKenneth D. Merry 		 * Include the block descriptor when calculating the mode
479696db222SKenneth D. Merry 		 * buffer length,
480696db222SKenneth D. Merry 		 */
481696db222SKenneth D. Merry 		mode_buffer_len = sizeof(struct scsi_mode_header_6) +
482696db222SKenneth D. Merry 				  sizeof(struct scsi_mode_blk_desc) +
483696db222SKenneth D. Merry 				 sizeof(struct page_element_address_assignment);
48476babe50SJustin T. Gibbs 
4850dd50e9bSScott Long 		mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
486696db222SKenneth D. Merry 
487696db222SKenneth D. Merry 		if (mode_buffer == NULL) {
48876babe50SJustin T. Gibbs 			printf("chstart: couldn't malloc mode sense data\n");
48976babe50SJustin T. Gibbs 			break;
49076babe50SJustin T. Gibbs 		}
491696db222SKenneth D. Merry 		bzero(mode_buffer, mode_buffer_len);
49276babe50SJustin T. Gibbs 
49376babe50SJustin T. Gibbs 		/*
49476babe50SJustin T. Gibbs 		 * Get the element address assignment page.
49576babe50SJustin T. Gibbs 		 */
49676babe50SJustin T. Gibbs 		scsi_mode_sense(&start_ccb->csio,
49776babe50SJustin T. Gibbs 				/* retries */ 1,
49876babe50SJustin T. Gibbs 				/* cbfcnp */ chdone,
49976babe50SJustin T. Gibbs 				/* tag_action */ MSG_SIMPLE_Q_TAG,
500696db222SKenneth D. Merry 				/* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
501696db222SKenneth D. Merry 					FALSE : TRUE,
50276babe50SJustin T. Gibbs 				/* page_code */ SMS_PAGE_CTRL_CURRENT,
50376babe50SJustin T. Gibbs 				/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
504696db222SKenneth D. Merry 				/* param_buf */ (u_int8_t *)mode_buffer,
505696db222SKenneth D. Merry 				/* param_len */ mode_buffer_len,
50676babe50SJustin T. Gibbs 				/* sense_len */ SSD_FULL_SIZE,
50776babe50SJustin T. Gibbs 				/* timeout */ CH_TIMEOUT_MODE_SENSE);
50876babe50SJustin T. Gibbs 
50976babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_bp = NULL;
51076babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
51176babe50SJustin T. Gibbs 		xpt_action(start_ccb);
51276babe50SJustin T. Gibbs 		break;
51376babe50SJustin T. Gibbs 	}
51476babe50SJustin T. Gibbs 	}
51576babe50SJustin T. Gibbs }
51676babe50SJustin T. Gibbs 
51776babe50SJustin T. Gibbs static void
51876babe50SJustin T. Gibbs chdone(struct cam_periph *periph, union ccb *done_ccb)
51976babe50SJustin T. Gibbs {
52076babe50SJustin T. Gibbs 	struct ch_softc *softc;
52176babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
52276babe50SJustin T. Gibbs 
52376babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
52476babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
52576babe50SJustin T. Gibbs 
52676babe50SJustin T. Gibbs 	switch(done_ccb->ccb_h.ccb_state) {
52776babe50SJustin T. Gibbs 	case CH_CCB_PROBE:
52876babe50SJustin T. Gibbs 	{
529696db222SKenneth D. Merry 		struct scsi_mode_header_6 *mode_header;
530696db222SKenneth D. Merry 		struct page_element_address_assignment *ea;
53176babe50SJustin T. Gibbs 		char announce_buf[80];
53276babe50SJustin T. Gibbs 
533696db222SKenneth D. Merry 
534696db222SKenneth D. Merry 		mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
535696db222SKenneth D. Merry 
536696db222SKenneth D. Merry 		ea = (struct page_element_address_assignment *)
537696db222SKenneth D. Merry 			find_mode_page_6(mode_header);
53876babe50SJustin T. Gibbs 
53976babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
54076babe50SJustin T. Gibbs 
541696db222SKenneth D. Merry 			softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
542696db222SKenneth D. Merry 			softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
543696db222SKenneth D. Merry 			softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
544696db222SKenneth D. Merry 			softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
545696db222SKenneth D. Merry 			softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
546696db222SKenneth D. Merry 			softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
547696db222SKenneth D. Merry 			softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
548696db222SKenneth D. Merry 			softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
54976babe50SJustin T. Gibbs 			softc->sc_picker = softc->sc_firsts[CHET_MT];
55076babe50SJustin T. Gibbs 
55176babe50SJustin T. Gibbs #define PLURAL(c)	(c) == 1 ? "" : "s"
5522127f260SArchie Cobbs 			snprintf(announce_buf, sizeof(announce_buf),
5532127f260SArchie Cobbs 				"%d slot%s, %d drive%s, "
55476babe50SJustin T. Gibbs 				"%d picker%s, %d portal%s",
55576babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_ST],
55676babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_ST]),
55776babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_DT],
55876babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_DT]),
55976babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_MT],
56076babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_MT]),
56176babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_IE],
56276babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_IE]));
56376babe50SJustin T. Gibbs #undef PLURAL
56476babe50SJustin T. Gibbs 		} else {
56576babe50SJustin T. Gibbs 			int error;
56676babe50SJustin T. Gibbs 
5673393f8daSKenneth D. Merry 			error = cherror(done_ccb, CAM_RETRY_SELTO,
5683393f8daSKenneth D. Merry 					SF_RETRY_UA | SF_NO_PRINT);
56976babe50SJustin T. Gibbs 			/*
57076babe50SJustin T. Gibbs 			 * Retry any UNIT ATTENTION type errors.  They
57176babe50SJustin T. Gibbs 			 * are expected at boot.
57276babe50SJustin T. Gibbs 			 */
57376babe50SJustin T. Gibbs 			if (error == ERESTART) {
57476babe50SJustin T. Gibbs 				/*
57576babe50SJustin T. Gibbs 				 * A retry was scheuled, so
57676babe50SJustin T. Gibbs 				 * just return.
57776babe50SJustin T. Gibbs 				 */
57876babe50SJustin T. Gibbs 				return;
57976babe50SJustin T. Gibbs 			} else if (error != 0) {
580696db222SKenneth D. Merry 				int retry_scheduled;
581696db222SKenneth D. Merry 				struct scsi_mode_sense_6 *sms;
582696db222SKenneth D. Merry 
583696db222SKenneth D. Merry 				sms = (struct scsi_mode_sense_6 *)
584696db222SKenneth D. Merry 					done_ccb->csio.cdb_io.cdb_bytes;
585696db222SKenneth D. Merry 
586696db222SKenneth D. Merry 				/*
587696db222SKenneth D. Merry 				 * Check to see if block descriptors were
588696db222SKenneth D. Merry 				 * disabled.  Some devices don't like that.
589696db222SKenneth D. Merry 				 * We're taking advantage of the fact that
590696db222SKenneth D. Merry 				 * the first few bytes of the 6 and 10 byte
591696db222SKenneth D. Merry 				 * mode sense commands are the same.  If
592696db222SKenneth D. Merry 				 * block descriptors were disabled, enable
593696db222SKenneth D. Merry 				 * them and re-send the command.
594696db222SKenneth D. Merry 				 */
595696db222SKenneth D. Merry 				if (sms->byte2 & SMS_DBD) {
596696db222SKenneth D. Merry 					sms->byte2 &= ~SMS_DBD;
597696db222SKenneth D. Merry 					xpt_action(done_ccb);
598696db222SKenneth D. Merry 					softc->quirks |= CH_Q_NO_DBD;
599696db222SKenneth D. Merry 					retry_scheduled = 1;
600696db222SKenneth D. Merry 				} else
601696db222SKenneth D. Merry 					retry_scheduled = 0;
602696db222SKenneth D. Merry 
60376babe50SJustin T. Gibbs 				/* Don't wedge this device's queue */
604d84c90a6SAlexander Motin 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
60576babe50SJustin T. Gibbs 					cam_release_devq(done_ccb->ccb_h.path,
60676babe50SJustin T. Gibbs 						 /*relsim_flags*/0,
60776babe50SJustin T. Gibbs 						 /*reduction*/0,
60876babe50SJustin T. Gibbs 						 /*timeout*/0,
60976babe50SJustin T. Gibbs 						 /*getcount_only*/0);
610696db222SKenneth D. Merry 
611696db222SKenneth D. Merry 				if (retry_scheduled)
612696db222SKenneth D. Merry 					return;
613696db222SKenneth D. Merry 
614696db222SKenneth D. Merry 				if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
615696db222SKenneth D. Merry 				    == CAM_SCSI_STATUS_ERROR)
616696db222SKenneth D. Merry 					scsi_sense_print(&done_ccb->csio);
617696db222SKenneth D. Merry 				else {
618f0d9af51SMatt Jacob 					xpt_print(periph->path,
619f0d9af51SMatt Jacob 					    "got CAM status %#x\n",
620696db222SKenneth D. Merry 					    done_ccb->ccb_h.status);
621696db222SKenneth D. Merry 				}
622f0d9af51SMatt Jacob 				xpt_print(periph->path, "fatal error, failed "
623f0d9af51SMatt Jacob 				    "to attach to device\n");
624696db222SKenneth D. Merry 
625696db222SKenneth D. Merry 				cam_periph_invalidate(periph);
626696db222SKenneth D. Merry 
627696db222SKenneth D. Merry 				announce_buf[0] = '\0';
62876babe50SJustin T. Gibbs 			}
62976babe50SJustin T. Gibbs 		}
630696db222SKenneth D. Merry 		if (announce_buf[0] != '\0')
63176babe50SJustin T. Gibbs 			xpt_announce_periph(periph, announce_buf);
63276babe50SJustin T. Gibbs 		softc->state = CH_STATE_NORMAL;
6330dd50e9bSScott Long 		free(mode_header, M_SCSICH);
634a68fce35SKenneth D. Merry 		/*
635a68fce35SKenneth D. Merry 		 * Since our peripheral may be invalidated by an error
636a68fce35SKenneth D. Merry 		 * above or an external event, we must release our CCB
637a68fce35SKenneth D. Merry 		 * before releasing the probe lock on the peripheral.
638a68fce35SKenneth D. Merry 		 * The peripheral will only go away once the last lock
639a68fce35SKenneth D. Merry 		 * is removed, and we need it around for the CCB release
640a68fce35SKenneth D. Merry 		 * operation.
641a68fce35SKenneth D. Merry 		 */
642a68fce35SKenneth D. Merry 		xpt_release_ccb(done_ccb);
6432b83592fSScott Long 		cam_periph_unhold(periph);
644a68fce35SKenneth D. Merry 		return;
64576babe50SJustin T. Gibbs 	}
64676babe50SJustin T. Gibbs 	case CH_CCB_WAITING:
64776babe50SJustin T. Gibbs 	{
64876babe50SJustin T. Gibbs 		/* Caller will release the CCB */
64976babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
65076babe50SJustin T. Gibbs 		return;
65176babe50SJustin T. Gibbs 	}
652a68fce35SKenneth D. Merry 	default:
653a68fce35SKenneth D. Merry 		break;
65476babe50SJustin T. Gibbs 	}
65576babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
65676babe50SJustin T. Gibbs }
65776babe50SJustin T. Gibbs 
65876babe50SJustin T. Gibbs static int
65976babe50SJustin T. Gibbs cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
66076babe50SJustin T. Gibbs {
66176babe50SJustin T. Gibbs 	struct ch_softc *softc;
66276babe50SJustin T. Gibbs 	struct cam_periph *periph;
66376babe50SJustin T. Gibbs 
66476babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
66576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
66676babe50SJustin T. Gibbs 
66776babe50SJustin T. Gibbs 	return (cam_periph_error(ccb, cam_flags, sense_flags,
66876babe50SJustin T. Gibbs 				 &softc->saved_ccb));
66976babe50SJustin T. Gibbs }
67076babe50SJustin T. Gibbs 
67176babe50SJustin T. Gibbs static int
67289c9c53dSPoul-Henning Kamp chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
67376babe50SJustin T. Gibbs {
67476babe50SJustin T. Gibbs 	struct cam_periph *periph;
67576babe50SJustin T. Gibbs 	struct ch_softc *softc;
67676babe50SJustin T. Gibbs 	int error;
67776babe50SJustin T. Gibbs 
678e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
67976babe50SJustin T. Gibbs 	if (periph == NULL)
68076babe50SJustin T. Gibbs 		return(ENXIO);
68176babe50SJustin T. Gibbs 
6822b83592fSScott Long 	cam_periph_lock(periph);
68376babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
68476babe50SJustin T. Gibbs 
68576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
68676babe50SJustin T. Gibbs 
68776babe50SJustin T. Gibbs 	error = 0;
68876babe50SJustin T. Gibbs 
68976babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
6906326180fSKenneth D. Merry 		  ("trying to do ioctl %#lx\n", cmd));
69176babe50SJustin T. Gibbs 
69276babe50SJustin T. Gibbs 	/*
69376babe50SJustin T. Gibbs 	 * If this command can change the device's state, we must
69476babe50SJustin T. Gibbs 	 * have the device open for writing.
69576babe50SJustin T. Gibbs 	 */
69676babe50SJustin T. Gibbs 	switch (cmd) {
69776babe50SJustin T. Gibbs 	case CHIOGPICKER:
69876babe50SJustin T. Gibbs 	case CHIOGPARAMS:
69976babe50SJustin T. Gibbs 	case CHIOGSTATUS:
70076babe50SJustin T. Gibbs 		break;
70176babe50SJustin T. Gibbs 
70276babe50SJustin T. Gibbs 	default:
7032b83592fSScott Long 		if ((flag & FWRITE) == 0) {
7042b83592fSScott Long 			cam_periph_unlock(periph);
70576babe50SJustin T. Gibbs 			return (EBADF);
70676babe50SJustin T. Gibbs 		}
7072b83592fSScott Long 	}
70876babe50SJustin T. Gibbs 
70976babe50SJustin T. Gibbs 	switch (cmd) {
71076babe50SJustin T. Gibbs 	case CHIOMOVE:
71176babe50SJustin T. Gibbs 		error = chmove(periph, (struct changer_move *)addr);
71276babe50SJustin T. Gibbs 		break;
71376babe50SJustin T. Gibbs 
71476babe50SJustin T. Gibbs 	case CHIOEXCHANGE:
71576babe50SJustin T. Gibbs 		error = chexchange(periph, (struct changer_exchange *)addr);
71676babe50SJustin T. Gibbs 		break;
71776babe50SJustin T. Gibbs 
71876babe50SJustin T. Gibbs 	case CHIOPOSITION:
71976babe50SJustin T. Gibbs 		error = chposition(periph, (struct changer_position *)addr);
72076babe50SJustin T. Gibbs 		break;
72176babe50SJustin T. Gibbs 
72276babe50SJustin T. Gibbs 	case CHIOGPICKER:
72376babe50SJustin T. Gibbs 		*(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
72476babe50SJustin T. Gibbs 		break;
72576babe50SJustin T. Gibbs 
72676babe50SJustin T. Gibbs 	case CHIOSPICKER:
72776babe50SJustin T. Gibbs 	{
72876babe50SJustin T. Gibbs 		int new_picker = *(int *)addr;
72976babe50SJustin T. Gibbs 
7302b83592fSScott Long 		if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
7312b83592fSScott Long 			error = EINVAL;
7322b83592fSScott Long 			break;
7332b83592fSScott Long 		}
73476babe50SJustin T. Gibbs 		softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
73576babe50SJustin T. Gibbs 		break;
73676babe50SJustin T. Gibbs 	}
73776babe50SJustin T. Gibbs 	case CHIOGPARAMS:
73876babe50SJustin T. Gibbs 	{
73976babe50SJustin T. Gibbs 		struct changer_params *cp = (struct changer_params *)addr;
74076babe50SJustin T. Gibbs 
74176babe50SJustin T. Gibbs 		cp->cp_npickers = softc->sc_counts[CHET_MT];
74276babe50SJustin T. Gibbs 		cp->cp_nslots = softc->sc_counts[CHET_ST];
74376babe50SJustin T. Gibbs 		cp->cp_nportals = softc->sc_counts[CHET_IE];
74476babe50SJustin T. Gibbs 		cp->cp_ndrives = softc->sc_counts[CHET_DT];
74576babe50SJustin T. Gibbs 		break;
74676babe50SJustin T. Gibbs 	}
74776babe50SJustin T. Gibbs 	case CHIOIELEM:
74876babe50SJustin T. Gibbs 		error = chielem(periph, *(unsigned int *)addr);
74976babe50SJustin T. Gibbs 		break;
75076babe50SJustin T. Gibbs 
75176babe50SJustin T. Gibbs 	case CHIOGSTATUS:
75276babe50SJustin T. Gibbs 	{
75376babe50SJustin T. Gibbs 		error = chgetelemstatus(periph,
75476babe50SJustin T. Gibbs 			       (struct changer_element_status_request *) addr);
75576babe50SJustin T. Gibbs 		break;
75676babe50SJustin T. Gibbs 	}
75776babe50SJustin T. Gibbs 
75876babe50SJustin T. Gibbs 	case CHIOSETVOLTAG:
75976babe50SJustin T. Gibbs 	{
76076babe50SJustin T. Gibbs 		error = chsetvoltag(periph,
76176babe50SJustin T. Gibbs 				    (struct changer_set_voltag_request *) addr);
76276babe50SJustin T. Gibbs 		break;
76376babe50SJustin T. Gibbs 	}
76476babe50SJustin T. Gibbs 
76576babe50SJustin T. Gibbs 	/* Implement prevent/allow? */
76676babe50SJustin T. Gibbs 
76776babe50SJustin T. Gibbs 	default:
76876babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, cherror);
76976babe50SJustin T. Gibbs 		break;
77076babe50SJustin T. Gibbs 	}
77176babe50SJustin T. Gibbs 
7722b83592fSScott Long 	cam_periph_unlock(periph);
77376babe50SJustin T. Gibbs 	return (error);
77476babe50SJustin T. Gibbs }
77576babe50SJustin T. Gibbs 
77676babe50SJustin T. Gibbs static int
77776babe50SJustin T. Gibbs chmove(struct cam_periph *periph, struct changer_move *cm)
77876babe50SJustin T. Gibbs {
77976babe50SJustin T. Gibbs 	struct ch_softc *softc;
78076babe50SJustin T. Gibbs 	u_int16_t fromelem, toelem;
78176babe50SJustin T. Gibbs 	union ccb *ccb;
78276babe50SJustin T. Gibbs 	int error;
78376babe50SJustin T. Gibbs 
78476babe50SJustin T. Gibbs 	error = 0;
78576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
78676babe50SJustin T. Gibbs 
78776babe50SJustin T. Gibbs 	/*
78876babe50SJustin T. Gibbs 	 * Check arguments.
78976babe50SJustin T. Gibbs 	 */
79076babe50SJustin T. Gibbs 	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
79176babe50SJustin T. Gibbs 		return (EINVAL);
79276babe50SJustin T. Gibbs 	if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
79376babe50SJustin T. Gibbs 	    (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
79476babe50SJustin T. Gibbs 		return (ENODEV);
79576babe50SJustin T. Gibbs 
79676babe50SJustin T. Gibbs 	/*
79776babe50SJustin T. Gibbs 	 * Check the request against the changer's capabilities.
79876babe50SJustin T. Gibbs 	 */
79976babe50SJustin T. Gibbs 	if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
80010fc9d90SJustin T. Gibbs 		return (ENODEV);
80176babe50SJustin T. Gibbs 
80276babe50SJustin T. Gibbs 	/*
80376babe50SJustin T. Gibbs 	 * Calculate the source and destination elements.
80476babe50SJustin T. Gibbs 	 */
80576babe50SJustin T. Gibbs 	fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
80676babe50SJustin T. Gibbs 	toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
80776babe50SJustin T. Gibbs 
808bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
80976babe50SJustin T. Gibbs 
81076babe50SJustin T. Gibbs 	scsi_move_medium(&ccb->csio,
81176babe50SJustin T. Gibbs 			 /* retries */ 1,
81276babe50SJustin T. Gibbs 			 /* cbfcnp */ chdone,
81376babe50SJustin T. Gibbs 			 /* tag_action */ MSG_SIMPLE_Q_TAG,
81476babe50SJustin T. Gibbs 			 /* tea */ softc->sc_picker,
81576babe50SJustin T. Gibbs 			 /* src */ fromelem,
81676babe50SJustin T. Gibbs 			 /* dst */ toelem,
81776babe50SJustin T. Gibbs 			 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
81876babe50SJustin T. Gibbs 			 /* sense_len */ SSD_FULL_SIZE,
81976babe50SJustin T. Gibbs 			 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
82076babe50SJustin T. Gibbs 
8213393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
8223393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
823a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
82476babe50SJustin T. Gibbs 
82576babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
82676babe50SJustin T. Gibbs 
82776babe50SJustin T. Gibbs 	return(error);
82876babe50SJustin T. Gibbs }
82976babe50SJustin T. Gibbs 
83076babe50SJustin T. Gibbs static int
83176babe50SJustin T. Gibbs chexchange(struct cam_periph *periph, struct changer_exchange *ce)
83276babe50SJustin T. Gibbs {
83376babe50SJustin T. Gibbs 	struct ch_softc *softc;
83476babe50SJustin T. Gibbs 	u_int16_t src, dst1, dst2;
83576babe50SJustin T. Gibbs 	union ccb *ccb;
83676babe50SJustin T. Gibbs 	int error;
83776babe50SJustin T. Gibbs 
83876babe50SJustin T. Gibbs 	error = 0;
83976babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
84076babe50SJustin T. Gibbs 	/*
84176babe50SJustin T. Gibbs 	 * Check arguments.
84276babe50SJustin T. Gibbs 	 */
84376babe50SJustin T. Gibbs 	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
84476babe50SJustin T. Gibbs 	    (ce->ce_sdsttype > CHET_DT))
84576babe50SJustin T. Gibbs 		return (EINVAL);
84676babe50SJustin T. Gibbs 	if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
84776babe50SJustin T. Gibbs 	    (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
84876babe50SJustin T. Gibbs 	    (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
84976babe50SJustin T. Gibbs 		return (ENODEV);
85076babe50SJustin T. Gibbs 
85176babe50SJustin T. Gibbs 	/*
85276babe50SJustin T. Gibbs 	 * Check the request against the changer's capabilities.
85376babe50SJustin T. Gibbs 	 */
85476babe50SJustin T. Gibbs 	if (((softc->sc_exchangemask[ce->ce_srctype] &
85576babe50SJustin T. Gibbs 	     (1 << ce->ce_fdsttype)) == 0) ||
85676babe50SJustin T. Gibbs 	    ((softc->sc_exchangemask[ce->ce_fdsttype] &
85776babe50SJustin T. Gibbs 	     (1 << ce->ce_sdsttype)) == 0))
85810fc9d90SJustin T. Gibbs 		return (ENODEV);
85976babe50SJustin T. Gibbs 
86076babe50SJustin T. Gibbs 	/*
86176babe50SJustin T. Gibbs 	 * Calculate the source and destination elements.
86276babe50SJustin T. Gibbs 	 */
86376babe50SJustin T. Gibbs 	src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
86476babe50SJustin T. Gibbs 	dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
86576babe50SJustin T. Gibbs 	dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
86676babe50SJustin T. Gibbs 
867bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
86876babe50SJustin T. Gibbs 
86976babe50SJustin T. Gibbs 	scsi_exchange_medium(&ccb->csio,
87076babe50SJustin T. Gibbs 			     /* retries */ 1,
87176babe50SJustin T. Gibbs 			     /* cbfcnp */ chdone,
87276babe50SJustin T. Gibbs 			     /* tag_action */ MSG_SIMPLE_Q_TAG,
87376babe50SJustin T. Gibbs 			     /* tea */ softc->sc_picker,
87476babe50SJustin T. Gibbs 			     /* src */ src,
87576babe50SJustin T. Gibbs 			     /* dst1 */ dst1,
87676babe50SJustin T. Gibbs 			     /* dst2 */ dst2,
87776babe50SJustin T. Gibbs 			     /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
87876babe50SJustin T. Gibbs 			                   TRUE : FALSE,
87976babe50SJustin T. Gibbs 			     /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
88076babe50SJustin T. Gibbs 			                   TRUE : FALSE,
88176babe50SJustin T. Gibbs 			     /* sense_len */ SSD_FULL_SIZE,
88276babe50SJustin T. Gibbs 			     /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
88376babe50SJustin T. Gibbs 
8843393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
8853393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
886a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
88776babe50SJustin T. Gibbs 
88876babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
88976babe50SJustin T. Gibbs 
89076babe50SJustin T. Gibbs 	return(error);
89176babe50SJustin T. Gibbs }
89276babe50SJustin T. Gibbs 
89376babe50SJustin T. Gibbs static int
89476babe50SJustin T. Gibbs chposition(struct cam_periph *periph, struct changer_position *cp)
89576babe50SJustin T. Gibbs {
89676babe50SJustin T. Gibbs 	struct ch_softc *softc;
89776babe50SJustin T. Gibbs 	u_int16_t dst;
89876babe50SJustin T. Gibbs 	union ccb *ccb;
89976babe50SJustin T. Gibbs 	int error;
90076babe50SJustin T. Gibbs 
90176babe50SJustin T. Gibbs 	error = 0;
90276babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
90376babe50SJustin T. Gibbs 
90476babe50SJustin T. Gibbs 	/*
90576babe50SJustin T. Gibbs 	 * Check arguments.
90676babe50SJustin T. Gibbs 	 */
90776babe50SJustin T. Gibbs 	if (cp->cp_type > CHET_DT)
90876babe50SJustin T. Gibbs 		return (EINVAL);
90976babe50SJustin T. Gibbs 	if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
91076babe50SJustin T. Gibbs 		return (ENODEV);
91176babe50SJustin T. Gibbs 
91276babe50SJustin T. Gibbs 	/*
91376babe50SJustin T. Gibbs 	 * Calculate the destination element.
91476babe50SJustin T. Gibbs 	 */
91576babe50SJustin T. Gibbs 	dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
91676babe50SJustin T. Gibbs 
917bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
91876babe50SJustin T. Gibbs 
91976babe50SJustin T. Gibbs 	scsi_position_to_element(&ccb->csio,
92076babe50SJustin T. Gibbs 				 /* retries */ 1,
92176babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
92276babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
92376babe50SJustin T. Gibbs 				 /* tea */ softc->sc_picker,
92476babe50SJustin T. Gibbs 				 /* dst */ dst,
92576babe50SJustin T. Gibbs 				 /* invert */ (cp->cp_flags & CP_INVERT) ?
92676babe50SJustin T. Gibbs 					      TRUE : FALSE,
92776babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
92876babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
92976babe50SJustin T. Gibbs 
9303393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
9313393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
932a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
93376babe50SJustin T. Gibbs 
93476babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
93576babe50SJustin T. Gibbs 
93676babe50SJustin T. Gibbs 	return(error);
93776babe50SJustin T. Gibbs }
93876babe50SJustin T. Gibbs 
93976babe50SJustin T. Gibbs /*
94076babe50SJustin T. Gibbs  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
94176babe50SJustin T. Gibbs  * to host native byte order in the volume serial number.  The volume
94276babe50SJustin T. Gibbs  * label as returned by the changer is transferred to user mode as
94376babe50SJustin T. Gibbs  * nul-terminated string.  Volume labels are truncated at the first
94476babe50SJustin T. Gibbs  * space, as suggested by SCSI-2.
94576babe50SJustin T. Gibbs  */
94676babe50SJustin T. Gibbs static	void
94776babe50SJustin T. Gibbs copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
94876babe50SJustin T. Gibbs {
94976babe50SJustin T. Gibbs 	int i;
95076babe50SJustin T. Gibbs 	for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
95176babe50SJustin T. Gibbs 		char c = voltag->vif[i];
95276babe50SJustin T. Gibbs 		if (c && c != ' ')
95376babe50SJustin T. Gibbs 			uvoltag->cv_volid[i] = c;
95476babe50SJustin T. Gibbs 	        else
95576babe50SJustin T. Gibbs 			break;
95676babe50SJustin T. Gibbs 	}
95776babe50SJustin T. Gibbs 	uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
95876babe50SJustin T. Gibbs }
95976babe50SJustin T. Gibbs 
96076babe50SJustin T. Gibbs /*
96176babe50SJustin T. Gibbs  * Copy an an element status descriptor to a user-mode
96276babe50SJustin T. Gibbs  * changer_element_status structure.
96376babe50SJustin T. Gibbs  */
96476babe50SJustin T. Gibbs 
96576babe50SJustin T. Gibbs static	void
96676babe50SJustin T. Gibbs copy_element_status(struct ch_softc *softc,
96776babe50SJustin T. Gibbs 		    u_int16_t flags,
96876babe50SJustin T. Gibbs 		    struct read_element_status_descriptor *desc,
96976babe50SJustin T. Gibbs 		    struct changer_element_status *ces)
97076babe50SJustin T. Gibbs {
97176babe50SJustin T. Gibbs 	u_int16_t eaddr = scsi_2btoul(desc->eaddr);
97276babe50SJustin T. Gibbs 	u_int16_t et;
97376babe50SJustin T. Gibbs 
97476babe50SJustin T. Gibbs 	ces->ces_int_addr = eaddr;
97576babe50SJustin T. Gibbs 	/* set up logical address in element status */
97676babe50SJustin T. Gibbs 	for (et = CHET_MT; et <= CHET_DT; et++) {
97776babe50SJustin T. Gibbs 		if ((softc->sc_firsts[et] <= eaddr)
97876babe50SJustin T. Gibbs 		    && ((softc->sc_firsts[et] + softc->sc_counts[et])
97976babe50SJustin T. Gibbs 			> eaddr)) {
98076babe50SJustin T. Gibbs 			ces->ces_addr = eaddr - softc->sc_firsts[et];
98176babe50SJustin T. Gibbs 			ces->ces_type = et;
98276babe50SJustin T. Gibbs 			break;
98376babe50SJustin T. Gibbs 		}
98476babe50SJustin T. Gibbs 	}
98576babe50SJustin T. Gibbs 
98676babe50SJustin T. Gibbs 	ces->ces_flags = desc->flags1;
98776babe50SJustin T. Gibbs 
98876babe50SJustin T. Gibbs 	ces->ces_sensecode = desc->sense_code;
98976babe50SJustin T. Gibbs 	ces->ces_sensequal = desc->sense_qual;
99076babe50SJustin T. Gibbs 
99176babe50SJustin T. Gibbs 	if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
99276babe50SJustin T. Gibbs 		ces->ces_flags |= CES_INVERT;
99376babe50SJustin T. Gibbs 
99476babe50SJustin T. Gibbs 	if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
99576babe50SJustin T. Gibbs 
99676babe50SJustin T. Gibbs 		eaddr = scsi_2btoul(desc->ssea);
99776babe50SJustin T. Gibbs 
99876babe50SJustin T. Gibbs 		/* convert source address to logical format */
99976babe50SJustin T. Gibbs 		for (et = CHET_MT; et <= CHET_DT; et++) {
100076babe50SJustin T. Gibbs 			if ((softc->sc_firsts[et] <= eaddr)
100176babe50SJustin T. Gibbs 			    && ((softc->sc_firsts[et] + softc->sc_counts[et])
100276babe50SJustin T. Gibbs 				> eaddr)) {
100376babe50SJustin T. Gibbs 				ces->ces_source_addr =
100476babe50SJustin T. Gibbs 					eaddr - softc->sc_firsts[et];
100576babe50SJustin T. Gibbs 				ces->ces_source_type = et;
100676babe50SJustin T. Gibbs 				ces->ces_flags |= CES_SOURCE_VALID;
100776babe50SJustin T. Gibbs 				break;
100876babe50SJustin T. Gibbs 			}
100976babe50SJustin T. Gibbs 		}
101076babe50SJustin T. Gibbs 
101176babe50SJustin T. Gibbs 		if (!(ces->ces_flags & CES_SOURCE_VALID))
101276babe50SJustin T. Gibbs 			printf("ch: warning: could not map element source "
1013a68fce35SKenneth D. Merry 			       "address %ud to a valid element type\n",
101476babe50SJustin T. Gibbs 			       eaddr);
101576babe50SJustin T. Gibbs 	}
101676babe50SJustin T. Gibbs 
101776babe50SJustin T. Gibbs 
101876babe50SJustin T. Gibbs 	if (flags & READ_ELEMENT_STATUS_PVOLTAG)
101976babe50SJustin T. Gibbs 		copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
102076babe50SJustin T. Gibbs 	if (flags & READ_ELEMENT_STATUS_AVOLTAG)
102176babe50SJustin T. Gibbs 		copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
102276babe50SJustin T. Gibbs 
102376babe50SJustin T. Gibbs 	if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
102476babe50SJustin T. Gibbs 		ces->ces_flags |= CES_SCSIID_VALID;
102576babe50SJustin T. Gibbs 		ces->ces_scsi_id = desc->dt_scsi_addr;
102676babe50SJustin T. Gibbs 	}
102776babe50SJustin T. Gibbs 
102876babe50SJustin T. Gibbs 	if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
102976babe50SJustin T. Gibbs 		ces->ces_flags |= CES_LUN_VALID;
103076babe50SJustin T. Gibbs 		ces->ces_scsi_lun =
103176babe50SJustin T. Gibbs 			desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
103276babe50SJustin T. Gibbs 	}
103376babe50SJustin T. Gibbs }
103476babe50SJustin T. Gibbs 
103576babe50SJustin T. Gibbs static int
103676babe50SJustin T. Gibbs chgetelemstatus(struct cam_periph *periph,
103776babe50SJustin T. Gibbs 		struct changer_element_status_request *cesr)
103876babe50SJustin T. Gibbs {
103976babe50SJustin T. Gibbs 	struct read_element_status_header *st_hdr;
104076babe50SJustin T. Gibbs 	struct read_element_status_page_header *pg_hdr;
104176babe50SJustin T. Gibbs 	struct read_element_status_descriptor *desc;
104276babe50SJustin T. Gibbs 	caddr_t data = NULL;
104376babe50SJustin T. Gibbs 	size_t size, desclen;
104476babe50SJustin T. Gibbs 	int avail, i, error = 0;
104576babe50SJustin T. Gibbs 	struct changer_element_status *user_data = NULL;
104676babe50SJustin T. Gibbs 	struct ch_softc *softc;
104776babe50SJustin T. Gibbs 	union ccb *ccb;
104876babe50SJustin T. Gibbs 	int chet = cesr->cesr_element_type;
104976babe50SJustin T. Gibbs 	int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
105076babe50SJustin T. Gibbs 
105176babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
105276babe50SJustin T. Gibbs 
105376babe50SJustin T. Gibbs 	/* perform argument checking */
105476babe50SJustin T. Gibbs 
105576babe50SJustin T. Gibbs 	/*
105676babe50SJustin T. Gibbs 	 * Perform a range check on the cesr_element_{base,count}
105776babe50SJustin T. Gibbs 	 * request argument fields.
105876babe50SJustin T. Gibbs 	 */
105976babe50SJustin T. Gibbs 	if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
106076babe50SJustin T. Gibbs 	    || (cesr->cesr_element_base + cesr->cesr_element_count)
106176babe50SJustin T. Gibbs 	        > softc->sc_counts[chet])
106276babe50SJustin T. Gibbs 		return (EINVAL);
106376babe50SJustin T. Gibbs 
106476babe50SJustin T. Gibbs 	/*
106576babe50SJustin T. Gibbs 	 * Request one descriptor for the given element type.  This
106676babe50SJustin T. Gibbs 	 * is used to determine the size of the descriptor so that
106776babe50SJustin T. Gibbs 	 * we can allocate enough storage for all of them.  We assume
106876babe50SJustin T. Gibbs 	 * that the first one can fit into 1k.
106976babe50SJustin T. Gibbs 	 */
10702b83592fSScott Long 	cam_periph_unlock(periph);
1071a163d034SWarner Losh 	data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
107276babe50SJustin T. Gibbs 
10732b83592fSScott Long 	cam_periph_lock(periph);
1074bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
107576babe50SJustin T. Gibbs 
107676babe50SJustin T. Gibbs 	scsi_read_element_status(&ccb->csio,
107776babe50SJustin T. Gibbs 				 /* retries */ 1,
107876babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
107976babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
108076babe50SJustin T. Gibbs 				 /* voltag */ want_voltags,
108176babe50SJustin T. Gibbs 				 /* sea */ softc->sc_firsts[chet],
108276babe50SJustin T. Gibbs 				 /* count */ 1,
108376babe50SJustin T. Gibbs 				 /* data_ptr */ data,
108476babe50SJustin T. Gibbs 				 /* dxfer_len */ 1024,
108576babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
108676babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
108776babe50SJustin T. Gibbs 
10883393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
10893393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1090a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
109176babe50SJustin T. Gibbs 
109276babe50SJustin T. Gibbs 	if (error)
109376babe50SJustin T. Gibbs 		goto done;
10942b83592fSScott Long 	cam_periph_unlock(periph);
109576babe50SJustin T. Gibbs 
109676babe50SJustin T. Gibbs 	st_hdr = (struct read_element_status_header *)data;
10971195ae18SBruce Evans 	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
109876babe50SJustin T. Gibbs 		  sizeof(struct read_element_status_header));
109976babe50SJustin T. Gibbs 	desclen = scsi_2btoul(pg_hdr->edl);
110076babe50SJustin T. Gibbs 
110176babe50SJustin T. Gibbs 	size = sizeof(struct read_element_status_header) +
110276babe50SJustin T. Gibbs 	       sizeof(struct read_element_status_page_header) +
110376babe50SJustin T. Gibbs 	       (desclen * cesr->cesr_element_count);
110476babe50SJustin T. Gibbs 
110576babe50SJustin T. Gibbs 	/*
110676babe50SJustin T. Gibbs 	 * Reallocate storage for descriptors and get them from the
110776babe50SJustin T. Gibbs 	 * device.
110876babe50SJustin T. Gibbs 	 */
110976babe50SJustin T. Gibbs 	free(data, M_DEVBUF);
1110a163d034SWarner Losh 	data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
111176babe50SJustin T. Gibbs 
11122b83592fSScott Long 	cam_periph_lock(periph);
111376babe50SJustin T. Gibbs 	scsi_read_element_status(&ccb->csio,
111476babe50SJustin T. Gibbs 				 /* retries */ 1,
111576babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
111676babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
111776babe50SJustin T. Gibbs 				 /* voltag */ want_voltags,
111876babe50SJustin T. Gibbs 				 /* sea */ softc->sc_firsts[chet]
111976babe50SJustin T. Gibbs 				 + cesr->cesr_element_base,
112076babe50SJustin T. Gibbs 				 /* count */ cesr->cesr_element_count,
112176babe50SJustin T. Gibbs 				 /* data_ptr */ data,
112276babe50SJustin T. Gibbs 				 /* dxfer_len */ size,
112376babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
112476babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
112576babe50SJustin T. Gibbs 
11263393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
11273393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1128a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
112976babe50SJustin T. Gibbs 
113076babe50SJustin T. Gibbs 	if (error)
113176babe50SJustin T. Gibbs 		goto done;
11322b83592fSScott Long 	cam_periph_unlock(periph);
113376babe50SJustin T. Gibbs 
113476babe50SJustin T. Gibbs 	/*
113576babe50SJustin T. Gibbs 	 * Fill in the user status array.
113676babe50SJustin T. Gibbs 	 */
113776babe50SJustin T. Gibbs 	st_hdr = (struct read_element_status_header *)data;
1138b3b73720SKenneth D. Merry 	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1139b3b73720SKenneth D. Merry 		  sizeof(struct read_element_status_header));
114076babe50SJustin T. Gibbs 	avail = scsi_2btoul(st_hdr->count);
114176babe50SJustin T. Gibbs 
114276babe50SJustin T. Gibbs 	if (avail != cesr->cesr_element_count) {
1143f0d9af51SMatt Jacob 		xpt_print(periph->path,
1144f0d9af51SMatt Jacob 		    "warning, READ ELEMENT STATUS avail != count\n");
114576babe50SJustin T. Gibbs 	}
114676babe50SJustin T. Gibbs 
114776babe50SJustin T. Gibbs 	user_data = (struct changer_element_status *)
114876babe50SJustin T. Gibbs 		malloc(avail * sizeof(struct changer_element_status),
1149a163d034SWarner Losh 		       M_DEVBUF, M_WAITOK | M_ZERO);
115076babe50SJustin T. Gibbs 
11511195ae18SBruce Evans 	desc = (struct read_element_status_descriptor *)((uintptr_t)data +
115276babe50SJustin T. Gibbs 		sizeof(struct read_element_status_header) +
115376babe50SJustin T. Gibbs 		sizeof(struct read_element_status_page_header));
115476babe50SJustin T. Gibbs 	/*
115576babe50SJustin T. Gibbs 	 * Set up the individual element status structures
115676babe50SJustin T. Gibbs 	 */
115776babe50SJustin T. Gibbs 	for (i = 0; i < avail; ++i) {
115876babe50SJustin T. Gibbs 		struct changer_element_status *ces = &(user_data[i]);
115976babe50SJustin T. Gibbs 
116076babe50SJustin T. Gibbs 		copy_element_status(softc, pg_hdr->flags, desc, ces);
116176babe50SJustin T. Gibbs 
11621195ae18SBruce Evans 		desc = (struct read_element_status_descriptor *)
11631195ae18SBruce Evans 		       ((uintptr_t)desc + desclen);
116476babe50SJustin T. Gibbs 	}
116576babe50SJustin T. Gibbs 
116676babe50SJustin T. Gibbs 	/* Copy element status structures out to userspace. */
116776babe50SJustin T. Gibbs 	error = copyout(user_data,
116876babe50SJustin T. Gibbs 			cesr->cesr_element_status,
116976babe50SJustin T. Gibbs 			avail * sizeof(struct changer_element_status));
11702b83592fSScott Long 	cam_periph_lock(periph);
117176babe50SJustin T. Gibbs 
117276babe50SJustin T. Gibbs  done:
117376babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
117476babe50SJustin T. Gibbs 
117576babe50SJustin T. Gibbs 	if (data != NULL)
117676babe50SJustin T. Gibbs 		free(data, M_DEVBUF);
117776babe50SJustin T. Gibbs 	if (user_data != NULL)
117876babe50SJustin T. Gibbs 		free(user_data, M_DEVBUF);
117976babe50SJustin T. Gibbs 
118076babe50SJustin T. Gibbs 	return (error);
118176babe50SJustin T. Gibbs }
118276babe50SJustin T. Gibbs 
118376babe50SJustin T. Gibbs static int
118476babe50SJustin T. Gibbs chielem(struct cam_periph *periph,
118576babe50SJustin T. Gibbs 	unsigned int timeout)
118676babe50SJustin T. Gibbs {
118776babe50SJustin T. Gibbs 	union ccb *ccb;
118876babe50SJustin T. Gibbs 	struct ch_softc *softc;
118976babe50SJustin T. Gibbs 	int error;
119076babe50SJustin T. Gibbs 
119176babe50SJustin T. Gibbs 	if (!timeout) {
119276babe50SJustin T. Gibbs 		timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
119376babe50SJustin T. Gibbs 	} else {
119476babe50SJustin T. Gibbs 		timeout *= 1000;
119576babe50SJustin T. Gibbs 	}
119676babe50SJustin T. Gibbs 
119776babe50SJustin T. Gibbs 	error = 0;
119876babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
119976babe50SJustin T. Gibbs 
1200bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
120176babe50SJustin T. Gibbs 
120276babe50SJustin T. Gibbs 	scsi_initialize_element_status(&ccb->csio,
120376babe50SJustin T. Gibbs 				      /* retries */ 1,
120476babe50SJustin T. Gibbs 				      /* cbfcnp */ chdone,
120576babe50SJustin T. Gibbs 				      /* tag_action */ MSG_SIMPLE_Q_TAG,
120676babe50SJustin T. Gibbs 				      /* sense_len */ SSD_FULL_SIZE,
120776babe50SJustin T. Gibbs 				      /* timeout */ timeout);
120876babe50SJustin T. Gibbs 
12093393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
12103393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1211a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
121276babe50SJustin T. Gibbs 
121376babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
121476babe50SJustin T. Gibbs 
121576babe50SJustin T. Gibbs 	return(error);
121676babe50SJustin T. Gibbs }
121776babe50SJustin T. Gibbs 
121876babe50SJustin T. Gibbs static int
121976babe50SJustin T. Gibbs chsetvoltag(struct cam_periph *periph,
122076babe50SJustin T. Gibbs 	    struct changer_set_voltag_request *csvr)
122176babe50SJustin T. Gibbs {
122276babe50SJustin T. Gibbs 	union ccb *ccb;
122376babe50SJustin T. Gibbs 	struct ch_softc *softc;
122476babe50SJustin T. Gibbs 	u_int16_t ea;
122576babe50SJustin T. Gibbs 	u_int8_t sac;
122676babe50SJustin T. Gibbs 	struct scsi_send_volume_tag_parameters ssvtp;
122776babe50SJustin T. Gibbs 	int error;
122876babe50SJustin T. Gibbs 	int i;
122976babe50SJustin T. Gibbs 
123076babe50SJustin T. Gibbs 	error = 0;
123176babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
123276babe50SJustin T. Gibbs 
123376babe50SJustin T. Gibbs 	bzero(&ssvtp, sizeof(ssvtp));
123476babe50SJustin T. Gibbs 	for (i=0; i<sizeof(ssvtp.vitf); i++) {
123576babe50SJustin T. Gibbs 		ssvtp.vitf[i] = ' ';
123676babe50SJustin T. Gibbs 	}
123776babe50SJustin T. Gibbs 
123876babe50SJustin T. Gibbs 	/*
123976babe50SJustin T. Gibbs 	 * Check arguments.
124076babe50SJustin T. Gibbs 	 */
124176babe50SJustin T. Gibbs 	if (csvr->csvr_type > CHET_DT)
124276babe50SJustin T. Gibbs 		return EINVAL;
124376babe50SJustin T. Gibbs 	if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
124476babe50SJustin T. Gibbs 		return ENODEV;
124576babe50SJustin T. Gibbs 
124676babe50SJustin T. Gibbs 	ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
124776babe50SJustin T. Gibbs 
124876babe50SJustin T. Gibbs 	if (csvr->csvr_flags & CSVR_ALTERNATE) {
124976babe50SJustin T. Gibbs 		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
125076babe50SJustin T. Gibbs 		case CSVR_MODE_SET:
125176babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
125276babe50SJustin T. Gibbs 			break;
125376babe50SJustin T. Gibbs 		case CSVR_MODE_REPLACE:
125476babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
125576babe50SJustin T. Gibbs 			break;
125676babe50SJustin T. Gibbs 		case CSVR_MODE_CLEAR:
125776babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
125876babe50SJustin T. Gibbs 			break;
125976babe50SJustin T. Gibbs 		default:
126076babe50SJustin T. Gibbs 			error = EINVAL;
126176babe50SJustin T. Gibbs 			goto out;
126276babe50SJustin T. Gibbs 		}
126376babe50SJustin T. Gibbs 	} else {
126476babe50SJustin T. Gibbs 		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
126576babe50SJustin T. Gibbs 		case CSVR_MODE_SET:
126676babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
126776babe50SJustin T. Gibbs 			break;
126876babe50SJustin T. Gibbs 		case CSVR_MODE_REPLACE:
126976babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
127076babe50SJustin T. Gibbs 			break;
127176babe50SJustin T. Gibbs 		case CSVR_MODE_CLEAR:
127276babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
127376babe50SJustin T. Gibbs 			break;
127476babe50SJustin T. Gibbs 		default:
127576babe50SJustin T. Gibbs 			error = EINVAL;
127676babe50SJustin T. Gibbs 			goto out;
127776babe50SJustin T. Gibbs 		}
127876babe50SJustin T. Gibbs 	}
127976babe50SJustin T. Gibbs 
128076babe50SJustin T. Gibbs 	memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
128176babe50SJustin T. Gibbs 	       min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
128276babe50SJustin T. Gibbs 	scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
128376babe50SJustin T. Gibbs 
1284bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
128576babe50SJustin T. Gibbs 
128676babe50SJustin T. Gibbs 	scsi_send_volume_tag(&ccb->csio,
128776babe50SJustin T. Gibbs 			     /* retries */ 1,
128876babe50SJustin T. Gibbs 			     /* cbfcnp */ chdone,
128976babe50SJustin T. Gibbs 			     /* tag_action */ MSG_SIMPLE_Q_TAG,
129076babe50SJustin T. Gibbs 			     /* element_address */ ea,
129176babe50SJustin T. Gibbs 			     /* send_action_code */ sac,
129276babe50SJustin T. Gibbs 			     /* parameters */ &ssvtp,
129376babe50SJustin T. Gibbs 			     /* sense_len */ SSD_FULL_SIZE,
129476babe50SJustin T. Gibbs 			     /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
129576babe50SJustin T. Gibbs 
12963393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
12973393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1298a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
129976babe50SJustin T. Gibbs 
130076babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
130176babe50SJustin T. Gibbs 
130276babe50SJustin T. Gibbs  out:
130376babe50SJustin T. Gibbs 	return error;
130476babe50SJustin T. Gibbs }
130576babe50SJustin T. Gibbs 
130676babe50SJustin T. Gibbs static int
130776babe50SJustin T. Gibbs chgetparams(struct cam_periph *periph)
130876babe50SJustin T. Gibbs {
130976babe50SJustin T. Gibbs 	union ccb *ccb;
131076babe50SJustin T. Gibbs 	struct ch_softc *softc;
1311696db222SKenneth D. Merry 	void *mode_buffer;
1312696db222SKenneth D. Merry 	int mode_buffer_len;
1313696db222SKenneth D. Merry 	struct page_element_address_assignment *ea;
1314696db222SKenneth D. Merry 	struct page_device_capabilities *cap;
1315696db222SKenneth D. Merry 	int error, from, dbd;
131676babe50SJustin T. Gibbs 	u_int8_t *moves, *exchanges;
131776babe50SJustin T. Gibbs 
131876babe50SJustin T. Gibbs 	error = 0;
131976babe50SJustin T. Gibbs 
132076babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
132176babe50SJustin T. Gibbs 
1322bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
132376babe50SJustin T. Gibbs 
1324696db222SKenneth D. Merry 	/*
1325696db222SKenneth D. Merry 	 * The scsi_mode_sense_data structure is just a convenience
1326696db222SKenneth D. Merry 	 * structure that allows us to easily calculate the worst-case
1327696db222SKenneth D. Merry 	 * storage size of the mode sense buffer.
1328696db222SKenneth D. Merry 	 */
1329696db222SKenneth D. Merry 	mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1330696db222SKenneth D. Merry 
13310dd50e9bSScott Long 	mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1332696db222SKenneth D. Merry 
1333696db222SKenneth D. Merry 	if (mode_buffer == NULL) {
133476babe50SJustin T. Gibbs 		printf("chgetparams: couldn't malloc mode sense data\n");
133576babe50SJustin T. Gibbs 		return(ENOSPC);
133676babe50SJustin T. Gibbs 	}
133776babe50SJustin T. Gibbs 
1338696db222SKenneth D. Merry 	bzero(mode_buffer, mode_buffer_len);
1339696db222SKenneth D. Merry 
1340696db222SKenneth D. Merry 	if (softc->quirks & CH_Q_NO_DBD)
1341696db222SKenneth D. Merry 		dbd = FALSE;
1342696db222SKenneth D. Merry 	else
1343696db222SKenneth D. Merry 		dbd = TRUE;
134476babe50SJustin T. Gibbs 
134576babe50SJustin T. Gibbs 	/*
134676babe50SJustin T. Gibbs 	 * Get the element address assignment page.
134776babe50SJustin T. Gibbs 	 */
134876babe50SJustin T. Gibbs 	scsi_mode_sense(&ccb->csio,
134976babe50SJustin T. Gibbs 			/* retries */ 1,
135076babe50SJustin T. Gibbs 			/* cbfcnp */ chdone,
135176babe50SJustin T. Gibbs 			/* tag_action */ MSG_SIMPLE_Q_TAG,
1352696db222SKenneth D. Merry 			/* dbd */ dbd,
135376babe50SJustin T. Gibbs 			/* page_code */ SMS_PAGE_CTRL_CURRENT,
135476babe50SJustin T. Gibbs 			/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1355696db222SKenneth D. Merry 			/* param_buf */ (u_int8_t *)mode_buffer,
1356696db222SKenneth D. Merry 			/* param_len */ mode_buffer_len,
135776babe50SJustin T. Gibbs 			/* sense_len */ SSD_FULL_SIZE,
135876babe50SJustin T. Gibbs 			/* timeout */ CH_TIMEOUT_MODE_SENSE);
135976babe50SJustin T. Gibbs 
13603393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
13613393f8daSKenneth D. Merry 				  /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1362a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
136376babe50SJustin T. Gibbs 
136476babe50SJustin T. Gibbs 	if (error) {
1365696db222SKenneth D. Merry 		if (dbd) {
1366696db222SKenneth D. Merry 			struct scsi_mode_sense_6 *sms;
1367696db222SKenneth D. Merry 
1368696db222SKenneth D. Merry 			sms = (struct scsi_mode_sense_6 *)
1369696db222SKenneth D. Merry 				ccb->csio.cdb_io.cdb_bytes;
1370696db222SKenneth D. Merry 
1371696db222SKenneth D. Merry 			sms->byte2 &= ~SMS_DBD;
13723393f8daSKenneth D. Merry 			error = cam_periph_runccb(ccb, cherror,
13733393f8daSKenneth D. Merry 						  /*cam_flags*/ CAM_RETRY_SELTO,
13743393f8daSKenneth D. Merry 				  		  /*sense_flags*/ SF_RETRY_UA,
1375a9d2245eSPoul-Henning Kamp 						  softc->device_stats);
1376696db222SKenneth D. Merry 		} else {
1377696db222SKenneth D. Merry 			/*
1378696db222SKenneth D. Merry 			 * Since we disabled sense printing above, print
1379696db222SKenneth D. Merry 			 * out the sense here since we got an error.
1380696db222SKenneth D. Merry 			 */
1381696db222SKenneth D. Merry 			scsi_sense_print(&ccb->csio);
138276babe50SJustin T. Gibbs 		}
138376babe50SJustin T. Gibbs 
1384696db222SKenneth D. Merry 		if (error) {
1385f0d9af51SMatt Jacob 			xpt_print(periph->path,
1386f0d9af51SMatt Jacob 			    "chgetparams: error getting element "
1387696db222SKenneth D. Merry 			    "address page\n");
1388696db222SKenneth D. Merry 			xpt_release_ccb(ccb);
13890dd50e9bSScott Long 			free(mode_buffer, M_SCSICH);
1390696db222SKenneth D. Merry 			return(error);
1391696db222SKenneth D. Merry 		}
1392696db222SKenneth D. Merry 	}
139376babe50SJustin T. Gibbs 
1394696db222SKenneth D. Merry 	ea = (struct page_element_address_assignment *)
1395696db222SKenneth D. Merry 		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1396696db222SKenneth D. Merry 
1397696db222SKenneth D. Merry 	softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1398696db222SKenneth D. Merry 	softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1399696db222SKenneth D. Merry 	softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1400696db222SKenneth D. Merry 	softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1401696db222SKenneth D. Merry 	softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1402696db222SKenneth D. Merry 	softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1403696db222SKenneth D. Merry 	softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1404696db222SKenneth D. Merry 	softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1405696db222SKenneth D. Merry 
1406696db222SKenneth D. Merry 	bzero(mode_buffer, mode_buffer_len);
140776babe50SJustin T. Gibbs 
140876babe50SJustin T. Gibbs 	/*
140976babe50SJustin T. Gibbs 	 * Now get the device capabilities page.
141076babe50SJustin T. Gibbs 	 */
141176babe50SJustin T. Gibbs 	scsi_mode_sense(&ccb->csio,
141276babe50SJustin T. Gibbs 			/* retries */ 1,
141376babe50SJustin T. Gibbs 			/* cbfcnp */ chdone,
141476babe50SJustin T. Gibbs 			/* tag_action */ MSG_SIMPLE_Q_TAG,
1415696db222SKenneth D. Merry 			/* dbd */ dbd,
141676babe50SJustin T. Gibbs 			/* page_code */ SMS_PAGE_CTRL_CURRENT,
141776babe50SJustin T. Gibbs 			/* page */ CH_DEVICE_CAP_PAGE,
1418696db222SKenneth D. Merry 			/* param_buf */ (u_int8_t *)mode_buffer,
1419696db222SKenneth D. Merry 			/* param_len */ mode_buffer_len,
142076babe50SJustin T. Gibbs 			/* sense_len */ SSD_FULL_SIZE,
142176babe50SJustin T. Gibbs 			/* timeout */ CH_TIMEOUT_MODE_SENSE);
142276babe50SJustin T. Gibbs 
14233393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
14243393f8daSKenneth D. Merry 				  /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1425a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
142676babe50SJustin T. Gibbs 
1427696db222SKenneth D. Merry 	if (error) {
1428696db222SKenneth D. Merry 		if (dbd) {
1429696db222SKenneth D. Merry 			struct scsi_mode_sense_6 *sms;
1430696db222SKenneth D. Merry 
1431696db222SKenneth D. Merry 			sms = (struct scsi_mode_sense_6 *)
1432696db222SKenneth D. Merry 				ccb->csio.cdb_io.cdb_bytes;
1433696db222SKenneth D. Merry 
1434696db222SKenneth D. Merry 			sms->byte2 &= ~SMS_DBD;
14353393f8daSKenneth D. Merry 			error = cam_periph_runccb(ccb, cherror,
14363393f8daSKenneth D. Merry 						  /*cam_flags*/ CAM_RETRY_SELTO,
14373393f8daSKenneth D. Merry 				  		  /*sense_flags*/ SF_RETRY_UA,
1438a9d2245eSPoul-Henning Kamp 						  softc->device_stats);
1439696db222SKenneth D. Merry 		} else {
1440696db222SKenneth D. Merry 			/*
1441696db222SKenneth D. Merry 			 * Since we disabled sense printing above, print
1442696db222SKenneth D. Merry 			 * out the sense here since we got an error.
1443696db222SKenneth D. Merry 			 */
1444696db222SKenneth D. Merry 			scsi_sense_print(&ccb->csio);
1445696db222SKenneth D. Merry 		}
144676babe50SJustin T. Gibbs 
144776babe50SJustin T. Gibbs 		if (error) {
1448f0d9af51SMatt Jacob 			xpt_print(periph->path,
1449f0d9af51SMatt Jacob 			    "chgetparams: error getting device "
1450696db222SKenneth D. Merry 			    "capabilities page\n");
1451696db222SKenneth D. Merry 			xpt_release_ccb(ccb);
14520dd50e9bSScott Long 			free(mode_buffer, M_SCSICH);
145376babe50SJustin T. Gibbs 			return(error);
145476babe50SJustin T. Gibbs 		}
1455696db222SKenneth D. Merry 	}
145676babe50SJustin T. Gibbs 
1457696db222SKenneth D. Merry 	xpt_release_ccb(ccb);
1458696db222SKenneth D. Merry 
1459696db222SKenneth D. Merry 	cap = (struct page_device_capabilities *)
1460696db222SKenneth D. Merry 		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
146176babe50SJustin T. Gibbs 
146276babe50SJustin T. Gibbs 	bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
146376babe50SJustin T. Gibbs 	bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
146456824e13SPoul-Henning Kamp 	moves = cap->move_from;
146556824e13SPoul-Henning Kamp 	exchanges = cap->exchange_with;
146656824e13SPoul-Henning Kamp 	for (from = CHET_MT; from <= CHET_MAX; ++from) {
146776babe50SJustin T. Gibbs 		softc->sc_movemask[from] = moves[from];
146876babe50SJustin T. Gibbs 		softc->sc_exchangemask[from] = exchanges[from];
146976babe50SJustin T. Gibbs 	}
147076babe50SJustin T. Gibbs 
14710dd50e9bSScott Long 	free(mode_buffer, M_SCSICH);
1472696db222SKenneth D. Merry 
147376babe50SJustin T. Gibbs 	return(error);
147476babe50SJustin T. Gibbs }
147576babe50SJustin T. Gibbs 
147676babe50SJustin T. Gibbs void
147776babe50SJustin T. Gibbs scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
147876babe50SJustin T. Gibbs 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
147976babe50SJustin T. Gibbs 		 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
148076babe50SJustin T. Gibbs 		 u_int32_t dst, int invert, u_int8_t sense_len,
148176babe50SJustin T. Gibbs 		 u_int32_t timeout)
148276babe50SJustin T. Gibbs {
148376babe50SJustin T. Gibbs 	struct scsi_move_medium *scsi_cmd;
148476babe50SJustin T. Gibbs 
148576babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
148676babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
148776babe50SJustin T. Gibbs 
148876babe50SJustin T. Gibbs 	scsi_cmd->opcode = MOVE_MEDIUM;
148976babe50SJustin T. Gibbs 
149076babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
149176babe50SJustin T. Gibbs 	scsi_ulto2b(src, scsi_cmd->src);
149276babe50SJustin T. Gibbs 	scsi_ulto2b(dst, scsi_cmd->dst);
149376babe50SJustin T. Gibbs 
149476babe50SJustin T. Gibbs 	if (invert)
149576babe50SJustin T. Gibbs 		scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
149676babe50SJustin T. Gibbs 
149776babe50SJustin T. Gibbs 	cam_fill_csio(csio,
149876babe50SJustin T. Gibbs 		      retries,
149976babe50SJustin T. Gibbs 		      cbfcnp,
150076babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
150176babe50SJustin T. Gibbs 		      tag_action,
150276babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
150376babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
150476babe50SJustin T. Gibbs 		      sense_len,
150576babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
150676babe50SJustin T. Gibbs 		      timeout);
150776babe50SJustin T. Gibbs }
150876babe50SJustin T. Gibbs 
150976babe50SJustin T. Gibbs void
151076babe50SJustin T. Gibbs scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
151176babe50SJustin T. Gibbs 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
151276babe50SJustin T. Gibbs 		     u_int8_t tag_action, u_int32_t tea, u_int32_t src,
151376babe50SJustin T. Gibbs 		     u_int32_t dst1, u_int32_t dst2, int invert1,
151476babe50SJustin T. Gibbs 		     int invert2, u_int8_t sense_len, u_int32_t timeout)
151576babe50SJustin T. Gibbs {
151676babe50SJustin T. Gibbs 	struct scsi_exchange_medium *scsi_cmd;
151776babe50SJustin T. Gibbs 
151876babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
151976babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
152076babe50SJustin T. Gibbs 
152176babe50SJustin T. Gibbs 	scsi_cmd->opcode = EXCHANGE_MEDIUM;
152276babe50SJustin T. Gibbs 
152376babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
152476babe50SJustin T. Gibbs 	scsi_ulto2b(src, scsi_cmd->src);
152576babe50SJustin T. Gibbs 	scsi_ulto2b(dst1, scsi_cmd->fdst);
152676babe50SJustin T. Gibbs 	scsi_ulto2b(dst2, scsi_cmd->sdst);
152776babe50SJustin T. Gibbs 
152876babe50SJustin T. Gibbs 	if (invert1)
152976babe50SJustin T. Gibbs 		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
153076babe50SJustin T. Gibbs 
153176babe50SJustin T. Gibbs 	if (invert2)
153276babe50SJustin T. Gibbs 		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
153376babe50SJustin T. Gibbs 
153476babe50SJustin T. Gibbs 	cam_fill_csio(csio,
153576babe50SJustin T. Gibbs 		      retries,
153676babe50SJustin T. Gibbs 		      cbfcnp,
153776babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
153876babe50SJustin T. Gibbs 		      tag_action,
153976babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
154076babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
154176babe50SJustin T. Gibbs 		      sense_len,
154276babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
154376babe50SJustin T. Gibbs 		      timeout);
154476babe50SJustin T. Gibbs }
154576babe50SJustin T. Gibbs 
154676babe50SJustin T. Gibbs void
154776babe50SJustin T. Gibbs scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
154876babe50SJustin T. Gibbs 			 void (*cbfcnp)(struct cam_periph *, union ccb *),
154976babe50SJustin T. Gibbs 			 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
155076babe50SJustin T. Gibbs 			 int invert, u_int8_t sense_len, u_int32_t timeout)
155176babe50SJustin T. Gibbs {
155276babe50SJustin T. Gibbs 	struct scsi_position_to_element *scsi_cmd;
155376babe50SJustin T. Gibbs 
155476babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
155576babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
155676babe50SJustin T. Gibbs 
155776babe50SJustin T. Gibbs 	scsi_cmd->opcode = POSITION_TO_ELEMENT;
155876babe50SJustin T. Gibbs 
155976babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
156076babe50SJustin T. Gibbs 	scsi_ulto2b(dst, scsi_cmd->dst);
156176babe50SJustin T. Gibbs 
156276babe50SJustin T. Gibbs 	if (invert)
156376babe50SJustin T. Gibbs 		scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
156476babe50SJustin T. Gibbs 
156576babe50SJustin T. Gibbs 	cam_fill_csio(csio,
156676babe50SJustin T. Gibbs 		      retries,
156776babe50SJustin T. Gibbs 		      cbfcnp,
156876babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
156976babe50SJustin T. Gibbs 		      tag_action,
157076babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
157176babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
157276babe50SJustin T. Gibbs 		      sense_len,
157376babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
157476babe50SJustin T. Gibbs 		      timeout);
157576babe50SJustin T. Gibbs }
157676babe50SJustin T. Gibbs 
157776babe50SJustin T. Gibbs void
157876babe50SJustin T. Gibbs scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
157976babe50SJustin T. Gibbs 			 void (*cbfcnp)(struct cam_periph *, union ccb *),
158076babe50SJustin T. Gibbs 			 u_int8_t tag_action, int voltag, u_int32_t sea,
158176babe50SJustin T. Gibbs 			 u_int32_t count, u_int8_t *data_ptr,
158276babe50SJustin T. Gibbs 			 u_int32_t dxfer_len, u_int8_t sense_len,
158376babe50SJustin T. Gibbs 			 u_int32_t timeout)
158476babe50SJustin T. Gibbs {
158576babe50SJustin T. Gibbs 	struct scsi_read_element_status *scsi_cmd;
158676babe50SJustin T. Gibbs 
158776babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
158876babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
158976babe50SJustin T. Gibbs 
159076babe50SJustin T. Gibbs 	scsi_cmd->opcode = READ_ELEMENT_STATUS;
159176babe50SJustin T. Gibbs 
159276babe50SJustin T. Gibbs 	scsi_ulto2b(sea, scsi_cmd->sea);
159376babe50SJustin T. Gibbs 	scsi_ulto2b(count, scsi_cmd->count);
159476babe50SJustin T. Gibbs 	scsi_ulto3b(dxfer_len, scsi_cmd->len);
159576babe50SJustin T. Gibbs 
159676babe50SJustin T. Gibbs 	if (voltag)
159776babe50SJustin T. Gibbs 		scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
159876babe50SJustin T. Gibbs 
159976babe50SJustin T. Gibbs 	cam_fill_csio(csio,
160076babe50SJustin T. Gibbs 		      retries,
160176babe50SJustin T. Gibbs 		      cbfcnp,
160276babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_IN,
160376babe50SJustin T. Gibbs 		      tag_action,
160476babe50SJustin T. Gibbs 		      data_ptr,
160576babe50SJustin T. Gibbs 		      dxfer_len,
160676babe50SJustin T. Gibbs 		      sense_len,
160776babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
160876babe50SJustin T. Gibbs 		      timeout);
160976babe50SJustin T. Gibbs }
161076babe50SJustin T. Gibbs 
161176babe50SJustin T. Gibbs void
161276babe50SJustin T. Gibbs scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
161376babe50SJustin T. Gibbs 			       void (*cbfcnp)(struct cam_periph *, union ccb *),
161476babe50SJustin T. Gibbs 			       u_int8_t tag_action, u_int8_t sense_len,
161576babe50SJustin T. Gibbs 			       u_int32_t timeout)
161676babe50SJustin T. Gibbs {
161776babe50SJustin T. Gibbs 	struct scsi_initialize_element_status *scsi_cmd;
161876babe50SJustin T. Gibbs 
161976babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_initialize_element_status *)
162076babe50SJustin T. Gibbs 		    &csio->cdb_io.cdb_bytes;
162176babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
162276babe50SJustin T. Gibbs 
162376babe50SJustin T. Gibbs 	scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
162476babe50SJustin T. Gibbs 
162576babe50SJustin T. Gibbs 	cam_fill_csio(csio,
162676babe50SJustin T. Gibbs 		      retries,
162776babe50SJustin T. Gibbs 		      cbfcnp,
162876babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
162976babe50SJustin T. Gibbs 		      tag_action,
163076babe50SJustin T. Gibbs 		      /* data_ptr */ NULL,
163176babe50SJustin T. Gibbs 		      /* dxfer_len */ 0,
163276babe50SJustin T. Gibbs 		      sense_len,
163376babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
163476babe50SJustin T. Gibbs 		      timeout);
163576babe50SJustin T. Gibbs }
163676babe50SJustin T. Gibbs 
163776babe50SJustin T. Gibbs void
163876babe50SJustin T. Gibbs scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
163976babe50SJustin T. Gibbs 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
164076babe50SJustin T. Gibbs 		     u_int8_t tag_action,
164176babe50SJustin T. Gibbs 		     u_int16_t element_address,
164276babe50SJustin T. Gibbs 		     u_int8_t send_action_code,
164376babe50SJustin T. Gibbs 		     struct scsi_send_volume_tag_parameters *parameters,
164476babe50SJustin T. Gibbs 		     u_int8_t sense_len, u_int32_t timeout)
164576babe50SJustin T. Gibbs {
164676babe50SJustin T. Gibbs 	struct scsi_send_volume_tag *scsi_cmd;
164776babe50SJustin T. Gibbs 
164876babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
164976babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
165076babe50SJustin T. Gibbs 
165176babe50SJustin T. Gibbs 	scsi_cmd->opcode = SEND_VOLUME_TAG;
165276babe50SJustin T. Gibbs 	scsi_ulto2b(element_address, scsi_cmd->ea);
165376babe50SJustin T. Gibbs 	scsi_cmd->sac = send_action_code;
165476babe50SJustin T. Gibbs 	scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
165576babe50SJustin T. Gibbs 
165676babe50SJustin T. Gibbs 	cam_fill_csio(csio,
165776babe50SJustin T. Gibbs 		      retries,
165876babe50SJustin T. Gibbs 		      cbfcnp,
165976babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_OUT,
166076babe50SJustin T. Gibbs 		      tag_action,
166176babe50SJustin T. Gibbs 		      /* data_ptr */ (u_int8_t *) parameters,
166276babe50SJustin T. Gibbs 		      sizeof(*parameters),
166376babe50SJustin T. Gibbs 		      sense_len,
166476babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
166576babe50SJustin T. Gibbs 		      timeout);
166676babe50SJustin T. Gibbs }
1667