xref: /freebsd/sys/cam/scsi/scsi_ch.c (revision 83c5d981ac176d1cfae8d6dab3c0420ed9d43477)
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 {
11076babe50SJustin T. Gibbs 	CH_FLAG_INVALID		= 0x001,
11176babe50SJustin T. Gibbs 	CH_FLAG_OPEN		= 0x002
11276babe50SJustin T. Gibbs } ch_flags;
11376babe50SJustin T. Gibbs 
11476babe50SJustin T. Gibbs typedef enum {
11576babe50SJustin T. Gibbs 	CH_STATE_PROBE,
11676babe50SJustin T. Gibbs 	CH_STATE_NORMAL
11776babe50SJustin T. Gibbs } ch_state;
11876babe50SJustin T. Gibbs 
11976babe50SJustin T. Gibbs typedef enum {
12076babe50SJustin T. Gibbs 	CH_CCB_PROBE,
12176babe50SJustin T. Gibbs 	CH_CCB_WAITING
12276babe50SJustin T. Gibbs } ch_ccb_types;
12376babe50SJustin T. Gibbs 
124696db222SKenneth D. Merry typedef enum {
125696db222SKenneth D. Merry 	CH_Q_NONE	= 0x00,
126696db222SKenneth D. Merry 	CH_Q_NO_DBD	= 0x01
127696db222SKenneth D. Merry } ch_quirks;
128696db222SKenneth D. Merry 
12976babe50SJustin T. Gibbs #define ccb_state	ppriv_field0
13076babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
13176babe50SJustin T. Gibbs 
13276babe50SJustin T. Gibbs struct scsi_mode_sense_data {
13376babe50SJustin T. Gibbs 	struct scsi_mode_header_6 header;
134696db222SKenneth D. Merry 	struct scsi_mode_blk_desc blk_desc;
13576babe50SJustin T. Gibbs 	union {
13676babe50SJustin T. Gibbs 		struct page_element_address_assignment ea;
13776babe50SJustin T. Gibbs 		struct page_transport_geometry_parameters tg;
13876babe50SJustin T. Gibbs 		struct page_device_capabilities cap;
13976babe50SJustin T. Gibbs 	} pages;
14076babe50SJustin T. Gibbs };
14176babe50SJustin T. Gibbs 
14276babe50SJustin T. Gibbs struct ch_softc {
14376babe50SJustin T. Gibbs 	ch_flags	flags;
14476babe50SJustin T. Gibbs 	ch_state	state;
145696db222SKenneth D. Merry 	ch_quirks	quirks;
14676babe50SJustin T. Gibbs 	union ccb	saved_ccb;
147a9d2245eSPoul-Henning Kamp 	struct devstat	*device_stats;
14889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
14976babe50SJustin T. Gibbs 
15076babe50SJustin T. Gibbs 	int		sc_picker;	/* current picker */
15176babe50SJustin T. Gibbs 
15276babe50SJustin T. Gibbs 	/*
15376babe50SJustin T. Gibbs 	 * The following information is obtained from the
15476babe50SJustin T. Gibbs 	 * element address assignment page.
15576babe50SJustin T. Gibbs 	 */
15656824e13SPoul-Henning Kamp 	int		sc_firsts[CHET_MAX + 1];	/* firsts */
15756824e13SPoul-Henning Kamp 	int		sc_counts[CHET_MAX + 1];	/* counts */
15876babe50SJustin T. Gibbs 
15976babe50SJustin T. Gibbs 	/*
16076babe50SJustin T. Gibbs 	 * The following mask defines the legal combinations
16176babe50SJustin T. Gibbs 	 * of elements for the MOVE MEDIUM command.
16276babe50SJustin T. Gibbs 	 */
16356824e13SPoul-Henning Kamp 	u_int8_t	sc_movemask[CHET_MAX + 1];
16476babe50SJustin T. Gibbs 
16576babe50SJustin T. Gibbs 	/*
16676babe50SJustin T. Gibbs 	 * As above, but for EXCHANGE MEDIUM.
16776babe50SJustin T. Gibbs 	 */
16856824e13SPoul-Henning Kamp 	u_int8_t	sc_exchangemask[CHET_MAX + 1];
16976babe50SJustin T. Gibbs 
17076babe50SJustin T. Gibbs 	/*
17176babe50SJustin T. Gibbs 	 * Quirks; see below.  XXX KDM not implemented yet
17276babe50SJustin T. Gibbs 	 */
17376babe50SJustin T. Gibbs 	int		sc_settledelay;	/* delay for settle */
17476babe50SJustin T. Gibbs };
17576babe50SJustin T. Gibbs 
17676babe50SJustin T. Gibbs static	d_open_t	chopen;
17776babe50SJustin T. Gibbs static	d_close_t	chclose;
17876babe50SJustin T. Gibbs static	d_ioctl_t	chioctl;
17976babe50SJustin T. Gibbs static	periph_init_t	chinit;
18076babe50SJustin T. Gibbs static  periph_ctor_t	chregister;
181ee9c90c7SKenneth D. Merry static	periph_oninv_t	choninvalidate;
18276babe50SJustin T. Gibbs static  periph_dtor_t   chcleanup;
18376babe50SJustin T. Gibbs static  periph_start_t  chstart;
18476babe50SJustin T. Gibbs static	void		chasync(void *callback_arg, u_int32_t code,
18576babe50SJustin T. Gibbs 				struct cam_path *path, void *arg);
18676babe50SJustin T. Gibbs static	void		chdone(struct cam_periph *periph,
18776babe50SJustin T. Gibbs 			       union ccb *done_ccb);
18876babe50SJustin T. Gibbs static	int		cherror(union ccb *ccb, u_int32_t cam_flags,
18976babe50SJustin T. Gibbs 				u_int32_t sense_flags);
19076babe50SJustin T. Gibbs static	int		chmove(struct cam_periph *periph,
19176babe50SJustin T. Gibbs 			       struct changer_move *cm);
19276babe50SJustin T. Gibbs static	int		chexchange(struct cam_periph *periph,
19376babe50SJustin T. Gibbs 				   struct changer_exchange *ce);
19476babe50SJustin T. Gibbs static	int		chposition(struct cam_periph *periph,
19576babe50SJustin T. Gibbs 				   struct changer_position *cp);
19676babe50SJustin T. Gibbs static	int		chgetelemstatus(struct cam_periph *periph,
19776babe50SJustin T. Gibbs 				struct changer_element_status_request *csr);
19876babe50SJustin T. Gibbs static	int		chsetvoltag(struct cam_periph *periph,
19976babe50SJustin T. Gibbs 				    struct changer_set_voltag_request *csvr);
20076babe50SJustin T. Gibbs static	int		chielem(struct cam_periph *periph,
20176babe50SJustin T. Gibbs 				unsigned int timeout);
20276babe50SJustin T. Gibbs static	int		chgetparams(struct cam_periph *periph);
20376babe50SJustin T. Gibbs 
20476babe50SJustin T. Gibbs static struct periph_driver chdriver =
20576babe50SJustin T. Gibbs {
20676babe50SJustin T. Gibbs 	chinit, "ch",
20776babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
20876babe50SJustin T. Gibbs };
20976babe50SJustin T. Gibbs 
2100b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(ch, chdriver);
21176babe50SJustin T. Gibbs 
2124e2f199eSPoul-Henning Kamp static struct cdevsw ch_cdevsw = {
213dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
2142b83592fSScott Long 	.d_flags =	0,
2157ac40f5fSPoul-Henning Kamp 	.d_open =	chopen,
2167ac40f5fSPoul-Henning Kamp 	.d_close =	chclose,
2177ac40f5fSPoul-Henning Kamp 	.d_ioctl =	chioctl,
2187ac40f5fSPoul-Henning Kamp 	.d_name =	"ch",
21976babe50SJustin T. Gibbs };
22076babe50SJustin T. Gibbs 
2210dd50e9bSScott Long MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
2220dd50e9bSScott Long 
22337c84183SPoul-Henning Kamp static void
22476babe50SJustin T. Gibbs chinit(void)
22576babe50SJustin T. Gibbs {
22676babe50SJustin T. Gibbs 	cam_status status;
22776babe50SJustin T. Gibbs 
22876babe50SJustin T. Gibbs 	/*
22976babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
23076babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
23176babe50SJustin T. Gibbs 	 */
23285d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
23376babe50SJustin T. Gibbs 
23476babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
23576babe50SJustin T. Gibbs 		printf("ch: Failed to attach master async callback "
23676babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
23776babe50SJustin T. Gibbs 	}
23876babe50SJustin T. Gibbs }
23976babe50SJustin T. Gibbs 
24076babe50SJustin T. Gibbs static void
241ee9c90c7SKenneth D. Merry choninvalidate(struct cam_periph *periph)
242ee9c90c7SKenneth D. Merry {
243ee9c90c7SKenneth D. Merry 	struct ch_softc *softc;
244ee9c90c7SKenneth D. Merry 
245ee9c90c7SKenneth D. Merry 	softc = (struct ch_softc *)periph->softc;
246ee9c90c7SKenneth D. Merry 
247ee9c90c7SKenneth D. Merry 	/*
248ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
249ee9c90c7SKenneth D. Merry 	 */
25085d92640SScott Long 	xpt_register_async(0, chasync, periph, periph->path);
251ee9c90c7SKenneth D. Merry 
252ee9c90c7SKenneth D. Merry 	softc->flags |= CH_FLAG_INVALID;
253ee9c90c7SKenneth D. Merry 
254f0d9af51SMatt Jacob 	xpt_print(periph->path, "lost device\n");
255ee9c90c7SKenneth D. Merry 
256ee9c90c7SKenneth D. Merry }
257ee9c90c7SKenneth D. Merry 
258ee9c90c7SKenneth D. Merry static void
25976babe50SJustin T. Gibbs chcleanup(struct cam_periph *periph)
26076babe50SJustin T. Gibbs {
261ee9c90c7SKenneth D. Merry 	struct ch_softc *softc;
26276babe50SJustin T. Gibbs 
263ee9c90c7SKenneth D. Merry 	softc = (struct ch_softc *)periph->softc;
264ee9c90c7SKenneth D. Merry 
265f0d9af51SMatt Jacob 	xpt_print(periph->path, "removing device entry\n");
2665f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
2675f3fed85SEdward Tomasz Napierala 	cam_periph_unlock(periph);
2685f3fed85SEdward Tomasz Napierala 	destroy_dev(softc->dev);
2695f3fed85SEdward Tomasz Napierala 	cam_periph_lock(periph);
270ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
27176babe50SJustin T. Gibbs }
27276babe50SJustin T. Gibbs 
27376babe50SJustin T. Gibbs static void
27476babe50SJustin T. Gibbs chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
27576babe50SJustin T. Gibbs {
27676babe50SJustin T. Gibbs 	struct cam_periph *periph;
27776babe50SJustin T. Gibbs 
27876babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
27976babe50SJustin T. Gibbs 
28076babe50SJustin T. Gibbs 	switch(code) {
28176babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
28276babe50SJustin T. Gibbs 	{
28376babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
28476babe50SJustin T. Gibbs 		cam_status status;
28576babe50SJustin T. Gibbs 
28676babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
287c5ff3b2fSMatt Jacob 		if (cgd == NULL)
288c5ff3b2fSMatt Jacob 			break;
28976babe50SJustin T. Gibbs 
29052c9ce25SScott Long 		if (cgd->protocol != PROTO_SCSI)
29152c9ce25SScott Long 			break;
29252c9ce25SScott Long 
29310b6172aSMatt Jacob 		if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
29476babe50SJustin T. Gibbs 			break;
29576babe50SJustin T. Gibbs 
29676babe50SJustin T. Gibbs 		/*
29776babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
29876babe50SJustin T. Gibbs 		 * this device and start the probe
29976babe50SJustin T. Gibbs 		 * process.
30076babe50SJustin T. Gibbs 		 */
301ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(chregister, choninvalidate,
302ee9c90c7SKenneth D. Merry 					  chcleanup, chstart, "ch",
303ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
30476babe50SJustin T. Gibbs 					  chasync, AC_FOUND_DEVICE, cgd);
30576babe50SJustin T. Gibbs 
30676babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
30776babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
30876babe50SJustin T. Gibbs 			printf("chasync: Unable to probe new device "
30976babe50SJustin T. Gibbs 			       "due to status 0x%x\n", status);
31076babe50SJustin T. Gibbs 
31176babe50SJustin T. Gibbs 		break;
31276babe50SJustin T. Gibbs 
31376babe50SJustin T. Gibbs 	}
31476babe50SJustin T. Gibbs 	default:
315516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
31676babe50SJustin T. Gibbs 		break;
31776babe50SJustin T. Gibbs 	}
31876babe50SJustin T. Gibbs }
31976babe50SJustin T. Gibbs 
32076babe50SJustin T. Gibbs static cam_status
32176babe50SJustin T. Gibbs chregister(struct cam_periph *periph, void *arg)
32276babe50SJustin T. Gibbs {
32376babe50SJustin T. Gibbs 	struct ch_softc *softc;
32476babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
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 
35076babe50SJustin T. Gibbs 	/*
35176babe50SJustin T. Gibbs 	 * Changers don't have a blocksize, and obviously don't support
35276babe50SJustin T. Gibbs 	 * tagged queueing.
35376babe50SJustin T. Gibbs 	 */
35485d92640SScott Long 	cam_periph_unlock(periph);
355a9d2245eSPoul-Henning Kamp 	softc->device_stats = devstat_new_entry("ch",
35676babe50SJustin T. Gibbs 			  periph->unit_number, 0,
35776babe50SJustin T. Gibbs 			  DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
35810b6172aSMatt Jacob 			  SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
3592a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_OTHER);
36076babe50SJustin T. Gibbs 
3612728bfbdSMatt Jacob 	/* Register the device */
3622728bfbdSMatt Jacob 	softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
3632728bfbdSMatt Jacob 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
3642728bfbdSMatt Jacob 			      periph->unit_number);
3652b83592fSScott Long 	cam_periph_lock(periph);
366e2a5fdf9SNate Lawson 	softc->dev->si_drv1 = periph;
3672728bfbdSMatt Jacob 
36876babe50SJustin T. Gibbs 	/*
36976babe50SJustin T. Gibbs 	 * Add an async callback so that we get
37076babe50SJustin T. Gibbs 	 * notified if this device goes away.
37176babe50SJustin T. Gibbs 	 */
37285d92640SScott Long 	xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
37376babe50SJustin T. Gibbs 
37476babe50SJustin T. Gibbs 	/*
3752b83592fSScott Long 	 * Lock this periph until we are setup.
37676babe50SJustin T. Gibbs 	 * This first call can't block
37776babe50SJustin T. Gibbs 	 */
3782b83592fSScott Long 	(void)cam_periph_hold(periph, PRIBIO);
37983c5d981SAlexander Motin 	xpt_schedule(periph, CAM_PRIORITY_DEV);
38076babe50SJustin T. Gibbs 
38176babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
38276babe50SJustin T. Gibbs }
38376babe50SJustin T. Gibbs 
38476babe50SJustin T. Gibbs static int
38589c9c53dSPoul-Henning Kamp chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
38676babe50SJustin T. Gibbs {
38776babe50SJustin T. Gibbs 	struct cam_periph *periph;
38876babe50SJustin T. Gibbs 	struct ch_softc *softc;
389e2a5fdf9SNate Lawson 	int error;
39076babe50SJustin T. Gibbs 
391e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
3922b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
39376babe50SJustin T. Gibbs 		return (ENXIO);
39476babe50SJustin T. Gibbs 
39576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
39676babe50SJustin T. Gibbs 
3972b83592fSScott Long 	cam_periph_lock(periph);
3982b83592fSScott Long 
399ee9c90c7SKenneth D. Merry 	if (softc->flags & CH_FLAG_INVALID) {
4002b83592fSScott Long 		cam_periph_unlock(periph);
4012b83592fSScott Long 		cam_periph_release(periph);
40276babe50SJustin T. Gibbs 		return(ENXIO);
403ee9c90c7SKenneth D. Merry 	}
40476babe50SJustin T. Gibbs 
4052b83592fSScott Long 	if ((softc->flags & CH_FLAG_OPEN) == 0)
40676babe50SJustin T. Gibbs 		softc->flags |= CH_FLAG_OPEN;
4072b83592fSScott Long 	else
4082b83592fSScott Long 		cam_periph_release(periph);
4092b83592fSScott Long 
4102b83592fSScott Long 	if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
4112b83592fSScott Long 		cam_periph_unlock(periph);
4122b83592fSScott Long 		cam_periph_release(periph);
4132b83592fSScott Long 		return (error);
41476babe50SJustin T. Gibbs 	}
41576babe50SJustin T. Gibbs 
41676babe50SJustin T. Gibbs 	/*
41776babe50SJustin T. Gibbs 	 * Load information about this changer device into the softc.
41876babe50SJustin T. Gibbs 	 */
41976babe50SJustin T. Gibbs 	if ((error = chgetparams(periph)) != 0) {
42076babe50SJustin T. Gibbs 		softc->flags &= ~CH_FLAG_OPEN;
42176babe50SJustin T. Gibbs 		cam_periph_unlock(periph);
42276babe50SJustin T. Gibbs 		cam_periph_release(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 
4472b83592fSScott Long 	cam_periph_lock(periph);
44876babe50SJustin T. Gibbs 
44976babe50SJustin T. Gibbs 	softc->flags &= ~CH_FLAG_OPEN;
45076babe50SJustin T. Gibbs 
45176babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
45276babe50SJustin T. Gibbs 	cam_periph_release(periph);
45376babe50SJustin T. Gibbs 
45476babe50SJustin T. Gibbs 	return(0);
45576babe50SJustin T. Gibbs }
45676babe50SJustin T. Gibbs 
45776babe50SJustin T. Gibbs static void
45876babe50SJustin T. Gibbs chstart(struct cam_periph *periph, union ccb *start_ccb)
45976babe50SJustin T. Gibbs {
46076babe50SJustin T. Gibbs 	struct ch_softc *softc;
46176babe50SJustin T. Gibbs 
46276babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
46376babe50SJustin T. Gibbs 
46476babe50SJustin T. Gibbs 	switch (softc->state) {
46576babe50SJustin T. Gibbs 	case CH_STATE_NORMAL:
46676babe50SJustin T. Gibbs 	{
46776babe50SJustin T. Gibbs 		if (periph->immediate_priority <= periph->pinfo.priority){
46876babe50SJustin T. Gibbs 			start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
46976babe50SJustin T. Gibbs 
47076babe50SJustin T. Gibbs 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
47176babe50SJustin T. Gibbs 					  periph_links.sle);
47276babe50SJustin T. Gibbs 			periph->immediate_priority = CAM_PRIORITY_NONE;
47376babe50SJustin T. Gibbs 			wakeup(&periph->ccb_list);
4742b83592fSScott Long 		}
47576babe50SJustin T. Gibbs 		break;
47676babe50SJustin T. Gibbs 	}
47776babe50SJustin T. Gibbs 	case CH_STATE_PROBE:
47876babe50SJustin T. Gibbs 	{
479696db222SKenneth D. Merry 		int mode_buffer_len;
480696db222SKenneth D. Merry 		void *mode_buffer;
48176babe50SJustin T. Gibbs 
482696db222SKenneth D. Merry 		/*
483696db222SKenneth D. Merry 		 * Include the block descriptor when calculating the mode
484696db222SKenneth D. Merry 		 * buffer length,
485696db222SKenneth D. Merry 		 */
486696db222SKenneth D. Merry 		mode_buffer_len = sizeof(struct scsi_mode_header_6) +
487696db222SKenneth D. Merry 				  sizeof(struct scsi_mode_blk_desc) +
488696db222SKenneth D. Merry 				 sizeof(struct page_element_address_assignment);
48976babe50SJustin T. Gibbs 
4900dd50e9bSScott Long 		mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
491696db222SKenneth D. Merry 
492696db222SKenneth D. Merry 		if (mode_buffer == NULL) {
49376babe50SJustin T. Gibbs 			printf("chstart: couldn't malloc mode sense data\n");
49476babe50SJustin T. Gibbs 			break;
49576babe50SJustin T. Gibbs 		}
496696db222SKenneth D. Merry 		bzero(mode_buffer, mode_buffer_len);
49776babe50SJustin T. Gibbs 
49876babe50SJustin T. Gibbs 		/*
49976babe50SJustin T. Gibbs 		 * Get the element address assignment page.
50076babe50SJustin T. Gibbs 		 */
50176babe50SJustin T. Gibbs 		scsi_mode_sense(&start_ccb->csio,
50276babe50SJustin T. Gibbs 				/* retries */ 1,
50376babe50SJustin T. Gibbs 				/* cbfcnp */ chdone,
50476babe50SJustin T. Gibbs 				/* tag_action */ MSG_SIMPLE_Q_TAG,
505696db222SKenneth D. Merry 				/* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
506696db222SKenneth D. Merry 					FALSE : TRUE,
50776babe50SJustin T. Gibbs 				/* page_code */ SMS_PAGE_CTRL_CURRENT,
50876babe50SJustin T. Gibbs 				/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
509696db222SKenneth D. Merry 				/* param_buf */ (u_int8_t *)mode_buffer,
510696db222SKenneth D. Merry 				/* param_len */ mode_buffer_len,
51176babe50SJustin T. Gibbs 				/* sense_len */ SSD_FULL_SIZE,
51276babe50SJustin T. Gibbs 				/* timeout */ CH_TIMEOUT_MODE_SENSE);
51376babe50SJustin T. Gibbs 
51476babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_bp = NULL;
51576babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
51676babe50SJustin T. Gibbs 		xpt_action(start_ccb);
51776babe50SJustin T. Gibbs 		break;
51876babe50SJustin T. Gibbs 	}
51976babe50SJustin T. Gibbs 	}
52076babe50SJustin T. Gibbs }
52176babe50SJustin T. Gibbs 
52276babe50SJustin T. Gibbs static void
52376babe50SJustin T. Gibbs chdone(struct cam_periph *periph, union ccb *done_ccb)
52476babe50SJustin T. Gibbs {
52576babe50SJustin T. Gibbs 	struct ch_softc *softc;
52676babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
52776babe50SJustin T. Gibbs 
52876babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
52976babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
53076babe50SJustin T. Gibbs 
53176babe50SJustin T. Gibbs 	switch(done_ccb->ccb_h.ccb_state) {
53276babe50SJustin T. Gibbs 	case CH_CCB_PROBE:
53376babe50SJustin T. Gibbs 	{
534696db222SKenneth D. Merry 		struct scsi_mode_header_6 *mode_header;
535696db222SKenneth D. Merry 		struct page_element_address_assignment *ea;
53676babe50SJustin T. Gibbs 		char announce_buf[80];
53776babe50SJustin T. Gibbs 
538696db222SKenneth D. Merry 
539696db222SKenneth D. Merry 		mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
540696db222SKenneth D. Merry 
541696db222SKenneth D. Merry 		ea = (struct page_element_address_assignment *)
542696db222SKenneth D. Merry 			find_mode_page_6(mode_header);
54376babe50SJustin T. Gibbs 
54476babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
54576babe50SJustin T. Gibbs 
546696db222SKenneth D. Merry 			softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
547696db222SKenneth D. Merry 			softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
548696db222SKenneth D. Merry 			softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
549696db222SKenneth D. Merry 			softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
550696db222SKenneth D. Merry 			softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
551696db222SKenneth D. Merry 			softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
552696db222SKenneth D. Merry 			softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
553696db222SKenneth D. Merry 			softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
55476babe50SJustin T. Gibbs 			softc->sc_picker = softc->sc_firsts[CHET_MT];
55576babe50SJustin T. Gibbs 
55676babe50SJustin T. Gibbs #define PLURAL(c)	(c) == 1 ? "" : "s"
5572127f260SArchie Cobbs 			snprintf(announce_buf, sizeof(announce_buf),
5582127f260SArchie Cobbs 				"%d slot%s, %d drive%s, "
55976babe50SJustin T. Gibbs 				"%d picker%s, %d portal%s",
56076babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_ST],
56176babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_ST]),
56276babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_DT],
56376babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_DT]),
56476babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_MT],
56576babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_MT]),
56676babe50SJustin T. Gibbs 		    		softc->sc_counts[CHET_IE],
56776babe50SJustin T. Gibbs 				PLURAL(softc->sc_counts[CHET_IE]));
56876babe50SJustin T. Gibbs #undef PLURAL
56976babe50SJustin T. Gibbs 		} else {
57076babe50SJustin T. Gibbs 			int error;
57176babe50SJustin T. Gibbs 
5723393f8daSKenneth D. Merry 			error = cherror(done_ccb, CAM_RETRY_SELTO,
5733393f8daSKenneth D. Merry 					SF_RETRY_UA | SF_NO_PRINT);
57476babe50SJustin T. Gibbs 			/*
57576babe50SJustin T. Gibbs 			 * Retry any UNIT ATTENTION type errors.  They
57676babe50SJustin T. Gibbs 			 * are expected at boot.
57776babe50SJustin T. Gibbs 			 */
57876babe50SJustin T. Gibbs 			if (error == ERESTART) {
57976babe50SJustin T. Gibbs 				/*
58076babe50SJustin T. Gibbs 				 * A retry was scheuled, so
58176babe50SJustin T. Gibbs 				 * just return.
58276babe50SJustin T. Gibbs 				 */
58376babe50SJustin T. Gibbs 				return;
58476babe50SJustin T. Gibbs 			} else if (error != 0) {
585696db222SKenneth D. Merry 				int retry_scheduled;
586696db222SKenneth D. Merry 				struct scsi_mode_sense_6 *sms;
587696db222SKenneth D. Merry 
588696db222SKenneth D. Merry 				sms = (struct scsi_mode_sense_6 *)
589696db222SKenneth D. Merry 					done_ccb->csio.cdb_io.cdb_bytes;
590696db222SKenneth D. Merry 
591696db222SKenneth D. Merry 				/*
592696db222SKenneth D. Merry 				 * Check to see if block descriptors were
593696db222SKenneth D. Merry 				 * disabled.  Some devices don't like that.
594696db222SKenneth D. Merry 				 * We're taking advantage of the fact that
595696db222SKenneth D. Merry 				 * the first few bytes of the 6 and 10 byte
596696db222SKenneth D. Merry 				 * mode sense commands are the same.  If
597696db222SKenneth D. Merry 				 * block descriptors were disabled, enable
598696db222SKenneth D. Merry 				 * them and re-send the command.
599696db222SKenneth D. Merry 				 */
600696db222SKenneth D. Merry 				if (sms->byte2 & SMS_DBD) {
601696db222SKenneth D. Merry 					sms->byte2 &= ~SMS_DBD;
602696db222SKenneth D. Merry 					xpt_action(done_ccb);
603696db222SKenneth D. Merry 					softc->quirks |= CH_Q_NO_DBD;
604696db222SKenneth D. Merry 					retry_scheduled = 1;
605696db222SKenneth D. Merry 				} else
606696db222SKenneth D. Merry 					retry_scheduled = 0;
607696db222SKenneth D. Merry 
60876babe50SJustin T. Gibbs 				/* Don't wedge this device's queue */
609d84c90a6SAlexander Motin 				if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
61076babe50SJustin T. Gibbs 					cam_release_devq(done_ccb->ccb_h.path,
61176babe50SJustin T. Gibbs 						 /*relsim_flags*/0,
61276babe50SJustin T. Gibbs 						 /*reduction*/0,
61376babe50SJustin T. Gibbs 						 /*timeout*/0,
61476babe50SJustin T. Gibbs 						 /*getcount_only*/0);
615696db222SKenneth D. Merry 
616696db222SKenneth D. Merry 				if (retry_scheduled)
617696db222SKenneth D. Merry 					return;
618696db222SKenneth D. Merry 
619696db222SKenneth D. Merry 				if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
620696db222SKenneth D. Merry 				    == CAM_SCSI_STATUS_ERROR)
621696db222SKenneth D. Merry 					scsi_sense_print(&done_ccb->csio);
622696db222SKenneth D. Merry 				else {
623f0d9af51SMatt Jacob 					xpt_print(periph->path,
624f0d9af51SMatt Jacob 					    "got CAM status %#x\n",
625696db222SKenneth D. Merry 					    done_ccb->ccb_h.status);
626696db222SKenneth D. Merry 				}
627f0d9af51SMatt Jacob 				xpt_print(periph->path, "fatal error, failed "
628f0d9af51SMatt Jacob 				    "to attach to device\n");
629696db222SKenneth D. Merry 
630696db222SKenneth D. Merry 				cam_periph_invalidate(periph);
631696db222SKenneth D. Merry 
632696db222SKenneth D. Merry 				announce_buf[0] = '\0';
63376babe50SJustin T. Gibbs 			}
63476babe50SJustin T. Gibbs 		}
635696db222SKenneth D. Merry 		if (announce_buf[0] != '\0')
63676babe50SJustin T. Gibbs 			xpt_announce_periph(periph, announce_buf);
63776babe50SJustin T. Gibbs 		softc->state = CH_STATE_NORMAL;
6380dd50e9bSScott Long 		free(mode_header, M_SCSICH);
639a68fce35SKenneth D. Merry 		/*
640a68fce35SKenneth D. Merry 		 * Since our peripheral may be invalidated by an error
641a68fce35SKenneth D. Merry 		 * above or an external event, we must release our CCB
642a68fce35SKenneth D. Merry 		 * before releasing the probe lock on the peripheral.
643a68fce35SKenneth D. Merry 		 * The peripheral will only go away once the last lock
644a68fce35SKenneth D. Merry 		 * is removed, and we need it around for the CCB release
645a68fce35SKenneth D. Merry 		 * operation.
646a68fce35SKenneth D. Merry 		 */
647a68fce35SKenneth D. Merry 		xpt_release_ccb(done_ccb);
6482b83592fSScott Long 		cam_periph_unhold(periph);
649a68fce35SKenneth D. Merry 		return;
65076babe50SJustin T. Gibbs 	}
65176babe50SJustin T. Gibbs 	case CH_CCB_WAITING:
65276babe50SJustin T. Gibbs 	{
65376babe50SJustin T. Gibbs 		/* Caller will release the CCB */
65476babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
65576babe50SJustin T. Gibbs 		return;
65676babe50SJustin T. Gibbs 	}
657a68fce35SKenneth D. Merry 	default:
658a68fce35SKenneth D. Merry 		break;
65976babe50SJustin T. Gibbs 	}
66076babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
66176babe50SJustin T. Gibbs }
66276babe50SJustin T. Gibbs 
66376babe50SJustin T. Gibbs static int
66476babe50SJustin T. Gibbs cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
66576babe50SJustin T. Gibbs {
66676babe50SJustin T. Gibbs 	struct ch_softc *softc;
66776babe50SJustin T. Gibbs 	struct cam_periph *periph;
66876babe50SJustin T. Gibbs 
66976babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
67076babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
67176babe50SJustin T. Gibbs 
67276babe50SJustin T. Gibbs 	return (cam_periph_error(ccb, cam_flags, sense_flags,
67376babe50SJustin T. Gibbs 				 &softc->saved_ccb));
67476babe50SJustin T. Gibbs }
67576babe50SJustin T. Gibbs 
67676babe50SJustin T. Gibbs static int
67789c9c53dSPoul-Henning Kamp chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
67876babe50SJustin T. Gibbs {
67976babe50SJustin T. Gibbs 	struct cam_periph *periph;
68076babe50SJustin T. Gibbs 	struct ch_softc *softc;
68176babe50SJustin T. Gibbs 	int error;
68276babe50SJustin T. Gibbs 
683e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
68476babe50SJustin T. Gibbs 	if (periph == NULL)
68576babe50SJustin T. Gibbs 		return(ENXIO);
68676babe50SJustin T. Gibbs 
6872b83592fSScott Long 	cam_periph_lock(periph);
68876babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
68976babe50SJustin T. Gibbs 
69076babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
69176babe50SJustin T. Gibbs 
69276babe50SJustin T. Gibbs 	error = 0;
69376babe50SJustin T. Gibbs 
69476babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
6956326180fSKenneth D. Merry 		  ("trying to do ioctl %#lx\n", cmd));
69676babe50SJustin T. Gibbs 
69776babe50SJustin T. Gibbs 	/*
69876babe50SJustin T. Gibbs 	 * If this command can change the device's state, we must
69976babe50SJustin T. Gibbs 	 * have the device open for writing.
70076babe50SJustin T. Gibbs 	 */
70176babe50SJustin T. Gibbs 	switch (cmd) {
70276babe50SJustin T. Gibbs 	case CHIOGPICKER:
70376babe50SJustin T. Gibbs 	case CHIOGPARAMS:
70476babe50SJustin T. Gibbs 	case CHIOGSTATUS:
70576babe50SJustin T. Gibbs 		break;
70676babe50SJustin T. Gibbs 
70776babe50SJustin T. Gibbs 	default:
7082b83592fSScott Long 		if ((flag & FWRITE) == 0) {
7092b83592fSScott Long 			cam_periph_unlock(periph);
71076babe50SJustin T. Gibbs 			return (EBADF);
71176babe50SJustin T. Gibbs 		}
7122b83592fSScott Long 	}
71376babe50SJustin T. Gibbs 
71476babe50SJustin T. Gibbs 	switch (cmd) {
71576babe50SJustin T. Gibbs 	case CHIOMOVE:
71676babe50SJustin T. Gibbs 		error = chmove(periph, (struct changer_move *)addr);
71776babe50SJustin T. Gibbs 		break;
71876babe50SJustin T. Gibbs 
71976babe50SJustin T. Gibbs 	case CHIOEXCHANGE:
72076babe50SJustin T. Gibbs 		error = chexchange(periph, (struct changer_exchange *)addr);
72176babe50SJustin T. Gibbs 		break;
72276babe50SJustin T. Gibbs 
72376babe50SJustin T. Gibbs 	case CHIOPOSITION:
72476babe50SJustin T. Gibbs 		error = chposition(periph, (struct changer_position *)addr);
72576babe50SJustin T. Gibbs 		break;
72676babe50SJustin T. Gibbs 
72776babe50SJustin T. Gibbs 	case CHIOGPICKER:
72876babe50SJustin T. Gibbs 		*(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
72976babe50SJustin T. Gibbs 		break;
73076babe50SJustin T. Gibbs 
73176babe50SJustin T. Gibbs 	case CHIOSPICKER:
73276babe50SJustin T. Gibbs 	{
73376babe50SJustin T. Gibbs 		int new_picker = *(int *)addr;
73476babe50SJustin T. Gibbs 
7352b83592fSScott Long 		if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
7362b83592fSScott Long 			error = EINVAL;
7372b83592fSScott Long 			break;
7382b83592fSScott Long 		}
73976babe50SJustin T. Gibbs 		softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
74076babe50SJustin T. Gibbs 		break;
74176babe50SJustin T. Gibbs 	}
74276babe50SJustin T. Gibbs 	case CHIOGPARAMS:
74376babe50SJustin T. Gibbs 	{
74476babe50SJustin T. Gibbs 		struct changer_params *cp = (struct changer_params *)addr;
74576babe50SJustin T. Gibbs 
74676babe50SJustin T. Gibbs 		cp->cp_npickers = softc->sc_counts[CHET_MT];
74776babe50SJustin T. Gibbs 		cp->cp_nslots = softc->sc_counts[CHET_ST];
74876babe50SJustin T. Gibbs 		cp->cp_nportals = softc->sc_counts[CHET_IE];
74976babe50SJustin T. Gibbs 		cp->cp_ndrives = softc->sc_counts[CHET_DT];
75076babe50SJustin T. Gibbs 		break;
75176babe50SJustin T. Gibbs 	}
75276babe50SJustin T. Gibbs 	case CHIOIELEM:
75376babe50SJustin T. Gibbs 		error = chielem(periph, *(unsigned int *)addr);
75476babe50SJustin T. Gibbs 		break;
75576babe50SJustin T. Gibbs 
75676babe50SJustin T. Gibbs 	case CHIOGSTATUS:
75776babe50SJustin T. Gibbs 	{
75876babe50SJustin T. Gibbs 		error = chgetelemstatus(periph,
75976babe50SJustin T. Gibbs 			       (struct changer_element_status_request *) addr);
76076babe50SJustin T. Gibbs 		break;
76176babe50SJustin T. Gibbs 	}
76276babe50SJustin T. Gibbs 
76376babe50SJustin T. Gibbs 	case CHIOSETVOLTAG:
76476babe50SJustin T. Gibbs 	{
76576babe50SJustin T. Gibbs 		error = chsetvoltag(periph,
76676babe50SJustin T. Gibbs 				    (struct changer_set_voltag_request *) addr);
76776babe50SJustin T. Gibbs 		break;
76876babe50SJustin T. Gibbs 	}
76976babe50SJustin T. Gibbs 
77076babe50SJustin T. Gibbs 	/* Implement prevent/allow? */
77176babe50SJustin T. Gibbs 
77276babe50SJustin T. Gibbs 	default:
77376babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, cherror);
77476babe50SJustin T. Gibbs 		break;
77576babe50SJustin T. Gibbs 	}
77676babe50SJustin T. Gibbs 
7772b83592fSScott Long 	cam_periph_unlock(periph);
77876babe50SJustin T. Gibbs 	return (error);
77976babe50SJustin T. Gibbs }
78076babe50SJustin T. Gibbs 
78176babe50SJustin T. Gibbs static int
78276babe50SJustin T. Gibbs chmove(struct cam_periph *periph, struct changer_move *cm)
78376babe50SJustin T. Gibbs {
78476babe50SJustin T. Gibbs 	struct ch_softc *softc;
78576babe50SJustin T. Gibbs 	u_int16_t fromelem, toelem;
78676babe50SJustin T. Gibbs 	union ccb *ccb;
78776babe50SJustin T. Gibbs 	int error;
78876babe50SJustin T. Gibbs 
78976babe50SJustin T. Gibbs 	error = 0;
79076babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
79176babe50SJustin T. Gibbs 
79276babe50SJustin T. Gibbs 	/*
79376babe50SJustin T. Gibbs 	 * Check arguments.
79476babe50SJustin T. Gibbs 	 */
79576babe50SJustin T. Gibbs 	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
79676babe50SJustin T. Gibbs 		return (EINVAL);
79776babe50SJustin T. Gibbs 	if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
79876babe50SJustin T. Gibbs 	    (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
79976babe50SJustin T. Gibbs 		return (ENODEV);
80076babe50SJustin T. Gibbs 
80176babe50SJustin T. Gibbs 	/*
80276babe50SJustin T. Gibbs 	 * Check the request against the changer's capabilities.
80376babe50SJustin T. Gibbs 	 */
80476babe50SJustin T. Gibbs 	if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
80510fc9d90SJustin T. Gibbs 		return (ENODEV);
80676babe50SJustin T. Gibbs 
80776babe50SJustin T. Gibbs 	/*
80876babe50SJustin T. Gibbs 	 * Calculate the source and destination elements.
80976babe50SJustin T. Gibbs 	 */
81076babe50SJustin T. Gibbs 	fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
81176babe50SJustin T. Gibbs 	toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
81276babe50SJustin T. Gibbs 
813bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
81476babe50SJustin T. Gibbs 
81576babe50SJustin T. Gibbs 	scsi_move_medium(&ccb->csio,
81676babe50SJustin T. Gibbs 			 /* retries */ 1,
81776babe50SJustin T. Gibbs 			 /* cbfcnp */ chdone,
81876babe50SJustin T. Gibbs 			 /* tag_action */ MSG_SIMPLE_Q_TAG,
81976babe50SJustin T. Gibbs 			 /* tea */ softc->sc_picker,
82076babe50SJustin T. Gibbs 			 /* src */ fromelem,
82176babe50SJustin T. Gibbs 			 /* dst */ toelem,
82276babe50SJustin T. Gibbs 			 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
82376babe50SJustin T. Gibbs 			 /* sense_len */ SSD_FULL_SIZE,
82476babe50SJustin T. Gibbs 			 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
82576babe50SJustin T. Gibbs 
8263393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
8273393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
828a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
82976babe50SJustin T. Gibbs 
83076babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
83176babe50SJustin T. Gibbs 
83276babe50SJustin T. Gibbs 	return(error);
83376babe50SJustin T. Gibbs }
83476babe50SJustin T. Gibbs 
83576babe50SJustin T. Gibbs static int
83676babe50SJustin T. Gibbs chexchange(struct cam_periph *periph, struct changer_exchange *ce)
83776babe50SJustin T. Gibbs {
83876babe50SJustin T. Gibbs 	struct ch_softc *softc;
83976babe50SJustin T. Gibbs 	u_int16_t src, dst1, dst2;
84076babe50SJustin T. Gibbs 	union ccb *ccb;
84176babe50SJustin T. Gibbs 	int error;
84276babe50SJustin T. Gibbs 
84376babe50SJustin T. Gibbs 	error = 0;
84476babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
84576babe50SJustin T. Gibbs 	/*
84676babe50SJustin T. Gibbs 	 * Check arguments.
84776babe50SJustin T. Gibbs 	 */
84876babe50SJustin T. Gibbs 	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
84976babe50SJustin T. Gibbs 	    (ce->ce_sdsttype > CHET_DT))
85076babe50SJustin T. Gibbs 		return (EINVAL);
85176babe50SJustin T. Gibbs 	if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
85276babe50SJustin T. Gibbs 	    (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
85376babe50SJustin T. Gibbs 	    (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
85476babe50SJustin T. Gibbs 		return (ENODEV);
85576babe50SJustin T. Gibbs 
85676babe50SJustin T. Gibbs 	/*
85776babe50SJustin T. Gibbs 	 * Check the request against the changer's capabilities.
85876babe50SJustin T. Gibbs 	 */
85976babe50SJustin T. Gibbs 	if (((softc->sc_exchangemask[ce->ce_srctype] &
86076babe50SJustin T. Gibbs 	     (1 << ce->ce_fdsttype)) == 0) ||
86176babe50SJustin T. Gibbs 	    ((softc->sc_exchangemask[ce->ce_fdsttype] &
86276babe50SJustin T. Gibbs 	     (1 << ce->ce_sdsttype)) == 0))
86310fc9d90SJustin T. Gibbs 		return (ENODEV);
86476babe50SJustin T. Gibbs 
86576babe50SJustin T. Gibbs 	/*
86676babe50SJustin T. Gibbs 	 * Calculate the source and destination elements.
86776babe50SJustin T. Gibbs 	 */
86876babe50SJustin T. Gibbs 	src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
86976babe50SJustin T. Gibbs 	dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
87076babe50SJustin T. Gibbs 	dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
87176babe50SJustin T. Gibbs 
872bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
87376babe50SJustin T. Gibbs 
87476babe50SJustin T. Gibbs 	scsi_exchange_medium(&ccb->csio,
87576babe50SJustin T. Gibbs 			     /* retries */ 1,
87676babe50SJustin T. Gibbs 			     /* cbfcnp */ chdone,
87776babe50SJustin T. Gibbs 			     /* tag_action */ MSG_SIMPLE_Q_TAG,
87876babe50SJustin T. Gibbs 			     /* tea */ softc->sc_picker,
87976babe50SJustin T. Gibbs 			     /* src */ src,
88076babe50SJustin T. Gibbs 			     /* dst1 */ dst1,
88176babe50SJustin T. Gibbs 			     /* dst2 */ dst2,
88276babe50SJustin T. Gibbs 			     /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
88376babe50SJustin T. Gibbs 			                   TRUE : FALSE,
88476babe50SJustin T. Gibbs 			     /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
88576babe50SJustin T. Gibbs 			                   TRUE : FALSE,
88676babe50SJustin T. Gibbs 			     /* sense_len */ SSD_FULL_SIZE,
88776babe50SJustin T. Gibbs 			     /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
88876babe50SJustin T. Gibbs 
8893393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
8903393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
891a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
89276babe50SJustin T. Gibbs 
89376babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
89476babe50SJustin T. Gibbs 
89576babe50SJustin T. Gibbs 	return(error);
89676babe50SJustin T. Gibbs }
89776babe50SJustin T. Gibbs 
89876babe50SJustin T. Gibbs static int
89976babe50SJustin T. Gibbs chposition(struct cam_periph *periph, struct changer_position *cp)
90076babe50SJustin T. Gibbs {
90176babe50SJustin T. Gibbs 	struct ch_softc *softc;
90276babe50SJustin T. Gibbs 	u_int16_t dst;
90376babe50SJustin T. Gibbs 	union ccb *ccb;
90476babe50SJustin T. Gibbs 	int error;
90576babe50SJustin T. Gibbs 
90676babe50SJustin T. Gibbs 	error = 0;
90776babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
90876babe50SJustin T. Gibbs 
90976babe50SJustin T. Gibbs 	/*
91076babe50SJustin T. Gibbs 	 * Check arguments.
91176babe50SJustin T. Gibbs 	 */
91276babe50SJustin T. Gibbs 	if (cp->cp_type > CHET_DT)
91376babe50SJustin T. Gibbs 		return (EINVAL);
91476babe50SJustin T. Gibbs 	if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
91576babe50SJustin T. Gibbs 		return (ENODEV);
91676babe50SJustin T. Gibbs 
91776babe50SJustin T. Gibbs 	/*
91876babe50SJustin T. Gibbs 	 * Calculate the destination element.
91976babe50SJustin T. Gibbs 	 */
92076babe50SJustin T. Gibbs 	dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
92176babe50SJustin T. Gibbs 
922bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
92376babe50SJustin T. Gibbs 
92476babe50SJustin T. Gibbs 	scsi_position_to_element(&ccb->csio,
92576babe50SJustin T. Gibbs 				 /* retries */ 1,
92676babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
92776babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
92876babe50SJustin T. Gibbs 				 /* tea */ softc->sc_picker,
92976babe50SJustin T. Gibbs 				 /* dst */ dst,
93076babe50SJustin T. Gibbs 				 /* invert */ (cp->cp_flags & CP_INVERT) ?
93176babe50SJustin T. Gibbs 					      TRUE : FALSE,
93276babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
93376babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
93476babe50SJustin T. Gibbs 
9353393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
9363393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
937a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
93876babe50SJustin T. Gibbs 
93976babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
94076babe50SJustin T. Gibbs 
94176babe50SJustin T. Gibbs 	return(error);
94276babe50SJustin T. Gibbs }
94376babe50SJustin T. Gibbs 
94476babe50SJustin T. Gibbs /*
94576babe50SJustin T. Gibbs  * Copy a volume tag to a volume_tag struct, converting SCSI byte order
94676babe50SJustin T. Gibbs  * to host native byte order in the volume serial number.  The volume
94776babe50SJustin T. Gibbs  * label as returned by the changer is transferred to user mode as
94876babe50SJustin T. Gibbs  * nul-terminated string.  Volume labels are truncated at the first
94976babe50SJustin T. Gibbs  * space, as suggested by SCSI-2.
95076babe50SJustin T. Gibbs  */
95176babe50SJustin T. Gibbs static	void
95276babe50SJustin T. Gibbs copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
95376babe50SJustin T. Gibbs {
95476babe50SJustin T. Gibbs 	int i;
95576babe50SJustin T. Gibbs 	for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
95676babe50SJustin T. Gibbs 		char c = voltag->vif[i];
95776babe50SJustin T. Gibbs 		if (c && c != ' ')
95876babe50SJustin T. Gibbs 			uvoltag->cv_volid[i] = c;
95976babe50SJustin T. Gibbs 	        else
96076babe50SJustin T. Gibbs 			break;
96176babe50SJustin T. Gibbs 	}
96276babe50SJustin T. Gibbs 	uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
96376babe50SJustin T. Gibbs }
96476babe50SJustin T. Gibbs 
96576babe50SJustin T. Gibbs /*
96676babe50SJustin T. Gibbs  * Copy an an element status descriptor to a user-mode
96776babe50SJustin T. Gibbs  * changer_element_status structure.
96876babe50SJustin T. Gibbs  */
96976babe50SJustin T. Gibbs 
97076babe50SJustin T. Gibbs static	void
97176babe50SJustin T. Gibbs copy_element_status(struct ch_softc *softc,
97276babe50SJustin T. Gibbs 		    u_int16_t flags,
97376babe50SJustin T. Gibbs 		    struct read_element_status_descriptor *desc,
97476babe50SJustin T. Gibbs 		    struct changer_element_status *ces)
97576babe50SJustin T. Gibbs {
97676babe50SJustin T. Gibbs 	u_int16_t eaddr = scsi_2btoul(desc->eaddr);
97776babe50SJustin T. Gibbs 	u_int16_t et;
97876babe50SJustin T. Gibbs 
97976babe50SJustin T. Gibbs 	ces->ces_int_addr = eaddr;
98076babe50SJustin T. Gibbs 	/* set up logical address in element status */
98176babe50SJustin T. Gibbs 	for (et = CHET_MT; et <= CHET_DT; et++) {
98276babe50SJustin T. Gibbs 		if ((softc->sc_firsts[et] <= eaddr)
98376babe50SJustin T. Gibbs 		    && ((softc->sc_firsts[et] + softc->sc_counts[et])
98476babe50SJustin T. Gibbs 			> eaddr)) {
98576babe50SJustin T. Gibbs 			ces->ces_addr = eaddr - softc->sc_firsts[et];
98676babe50SJustin T. Gibbs 			ces->ces_type = et;
98776babe50SJustin T. Gibbs 			break;
98876babe50SJustin T. Gibbs 		}
98976babe50SJustin T. Gibbs 	}
99076babe50SJustin T. Gibbs 
99176babe50SJustin T. Gibbs 	ces->ces_flags = desc->flags1;
99276babe50SJustin T. Gibbs 
99376babe50SJustin T. Gibbs 	ces->ces_sensecode = desc->sense_code;
99476babe50SJustin T. Gibbs 	ces->ces_sensequal = desc->sense_qual;
99576babe50SJustin T. Gibbs 
99676babe50SJustin T. Gibbs 	if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
99776babe50SJustin T. Gibbs 		ces->ces_flags |= CES_INVERT;
99876babe50SJustin T. Gibbs 
99976babe50SJustin T. Gibbs 	if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
100076babe50SJustin T. Gibbs 
100176babe50SJustin T. Gibbs 		eaddr = scsi_2btoul(desc->ssea);
100276babe50SJustin T. Gibbs 
100376babe50SJustin T. Gibbs 		/* convert source address to logical format */
100476babe50SJustin T. Gibbs 		for (et = CHET_MT; et <= CHET_DT; et++) {
100576babe50SJustin T. Gibbs 			if ((softc->sc_firsts[et] <= eaddr)
100676babe50SJustin T. Gibbs 			    && ((softc->sc_firsts[et] + softc->sc_counts[et])
100776babe50SJustin T. Gibbs 				> eaddr)) {
100876babe50SJustin T. Gibbs 				ces->ces_source_addr =
100976babe50SJustin T. Gibbs 					eaddr - softc->sc_firsts[et];
101076babe50SJustin T. Gibbs 				ces->ces_source_type = et;
101176babe50SJustin T. Gibbs 				ces->ces_flags |= CES_SOURCE_VALID;
101276babe50SJustin T. Gibbs 				break;
101376babe50SJustin T. Gibbs 			}
101476babe50SJustin T. Gibbs 		}
101576babe50SJustin T. Gibbs 
101676babe50SJustin T. Gibbs 		if (!(ces->ces_flags & CES_SOURCE_VALID))
101776babe50SJustin T. Gibbs 			printf("ch: warning: could not map element source "
1018a68fce35SKenneth D. Merry 			       "address %ud to a valid element type\n",
101976babe50SJustin T. Gibbs 			       eaddr);
102076babe50SJustin T. Gibbs 	}
102176babe50SJustin T. Gibbs 
102276babe50SJustin T. Gibbs 
102376babe50SJustin T. Gibbs 	if (flags & READ_ELEMENT_STATUS_PVOLTAG)
102476babe50SJustin T. Gibbs 		copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
102576babe50SJustin T. Gibbs 	if (flags & READ_ELEMENT_STATUS_AVOLTAG)
102676babe50SJustin T. Gibbs 		copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
102776babe50SJustin T. Gibbs 
102876babe50SJustin T. Gibbs 	if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
102976babe50SJustin T. Gibbs 		ces->ces_flags |= CES_SCSIID_VALID;
103076babe50SJustin T. Gibbs 		ces->ces_scsi_id = desc->dt_scsi_addr;
103176babe50SJustin T. Gibbs 	}
103276babe50SJustin T. Gibbs 
103376babe50SJustin T. Gibbs 	if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
103476babe50SJustin T. Gibbs 		ces->ces_flags |= CES_LUN_VALID;
103576babe50SJustin T. Gibbs 		ces->ces_scsi_lun =
103676babe50SJustin T. Gibbs 			desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
103776babe50SJustin T. Gibbs 	}
103876babe50SJustin T. Gibbs }
103976babe50SJustin T. Gibbs 
104076babe50SJustin T. Gibbs static int
104176babe50SJustin T. Gibbs chgetelemstatus(struct cam_periph *periph,
104276babe50SJustin T. Gibbs 		struct changer_element_status_request *cesr)
104376babe50SJustin T. Gibbs {
104476babe50SJustin T. Gibbs 	struct read_element_status_header *st_hdr;
104576babe50SJustin T. Gibbs 	struct read_element_status_page_header *pg_hdr;
104676babe50SJustin T. Gibbs 	struct read_element_status_descriptor *desc;
104776babe50SJustin T. Gibbs 	caddr_t data = NULL;
104876babe50SJustin T. Gibbs 	size_t size, desclen;
104976babe50SJustin T. Gibbs 	int avail, i, error = 0;
105076babe50SJustin T. Gibbs 	struct changer_element_status *user_data = NULL;
105176babe50SJustin T. Gibbs 	struct ch_softc *softc;
105276babe50SJustin T. Gibbs 	union ccb *ccb;
105376babe50SJustin T. Gibbs 	int chet = cesr->cesr_element_type;
105476babe50SJustin T. Gibbs 	int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
105576babe50SJustin T. Gibbs 
105676babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
105776babe50SJustin T. Gibbs 
105876babe50SJustin T. Gibbs 	/* perform argument checking */
105976babe50SJustin T. Gibbs 
106076babe50SJustin T. Gibbs 	/*
106176babe50SJustin T. Gibbs 	 * Perform a range check on the cesr_element_{base,count}
106276babe50SJustin T. Gibbs 	 * request argument fields.
106376babe50SJustin T. Gibbs 	 */
106476babe50SJustin T. Gibbs 	if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
106576babe50SJustin T. Gibbs 	    || (cesr->cesr_element_base + cesr->cesr_element_count)
106676babe50SJustin T. Gibbs 	        > softc->sc_counts[chet])
106776babe50SJustin T. Gibbs 		return (EINVAL);
106876babe50SJustin T. Gibbs 
106976babe50SJustin T. Gibbs 	/*
107076babe50SJustin T. Gibbs 	 * Request one descriptor for the given element type.  This
107176babe50SJustin T. Gibbs 	 * is used to determine the size of the descriptor so that
107276babe50SJustin T. Gibbs 	 * we can allocate enough storage for all of them.  We assume
107376babe50SJustin T. Gibbs 	 * that the first one can fit into 1k.
107476babe50SJustin T. Gibbs 	 */
10752b83592fSScott Long 	cam_periph_unlock(periph);
1076a163d034SWarner Losh 	data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
107776babe50SJustin T. Gibbs 
10782b83592fSScott Long 	cam_periph_lock(periph);
1079bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
108076babe50SJustin T. Gibbs 
108176babe50SJustin T. Gibbs 	scsi_read_element_status(&ccb->csio,
108276babe50SJustin T. Gibbs 				 /* retries */ 1,
108376babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
108476babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
108576babe50SJustin T. Gibbs 				 /* voltag */ want_voltags,
108676babe50SJustin T. Gibbs 				 /* sea */ softc->sc_firsts[chet],
108776babe50SJustin T. Gibbs 				 /* count */ 1,
108876babe50SJustin T. Gibbs 				 /* data_ptr */ data,
108976babe50SJustin T. Gibbs 				 /* dxfer_len */ 1024,
109076babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
109176babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
109276babe50SJustin T. Gibbs 
10933393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
10943393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1095a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
109676babe50SJustin T. Gibbs 
109776babe50SJustin T. Gibbs 	if (error)
109876babe50SJustin T. Gibbs 		goto done;
10992b83592fSScott Long 	cam_periph_unlock(periph);
110076babe50SJustin T. Gibbs 
110176babe50SJustin T. Gibbs 	st_hdr = (struct read_element_status_header *)data;
11021195ae18SBruce Evans 	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
110376babe50SJustin T. Gibbs 		  sizeof(struct read_element_status_header));
110476babe50SJustin T. Gibbs 	desclen = scsi_2btoul(pg_hdr->edl);
110576babe50SJustin T. Gibbs 
110676babe50SJustin T. Gibbs 	size = sizeof(struct read_element_status_header) +
110776babe50SJustin T. Gibbs 	       sizeof(struct read_element_status_page_header) +
110876babe50SJustin T. Gibbs 	       (desclen * cesr->cesr_element_count);
110976babe50SJustin T. Gibbs 
111076babe50SJustin T. Gibbs 	/*
111176babe50SJustin T. Gibbs 	 * Reallocate storage for descriptors and get them from the
111276babe50SJustin T. Gibbs 	 * device.
111376babe50SJustin T. Gibbs 	 */
111476babe50SJustin T. Gibbs 	free(data, M_DEVBUF);
1115a163d034SWarner Losh 	data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
111676babe50SJustin T. Gibbs 
11172b83592fSScott Long 	cam_periph_lock(periph);
111876babe50SJustin T. Gibbs 	scsi_read_element_status(&ccb->csio,
111976babe50SJustin T. Gibbs 				 /* retries */ 1,
112076babe50SJustin T. Gibbs 				 /* cbfcnp */ chdone,
112176babe50SJustin T. Gibbs 				 /* tag_action */ MSG_SIMPLE_Q_TAG,
112276babe50SJustin T. Gibbs 				 /* voltag */ want_voltags,
112376babe50SJustin T. Gibbs 				 /* sea */ softc->sc_firsts[chet]
112476babe50SJustin T. Gibbs 				 + cesr->cesr_element_base,
112576babe50SJustin T. Gibbs 				 /* count */ cesr->cesr_element_count,
112676babe50SJustin T. Gibbs 				 /* data_ptr */ data,
112776babe50SJustin T. Gibbs 				 /* dxfer_len */ size,
112876babe50SJustin T. Gibbs 				 /* sense_len */ SSD_FULL_SIZE,
112976babe50SJustin T. Gibbs 				 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
113076babe50SJustin T. Gibbs 
11313393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
11323393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1133a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
113476babe50SJustin T. Gibbs 
113576babe50SJustin T. Gibbs 	if (error)
113676babe50SJustin T. Gibbs 		goto done;
11372b83592fSScott Long 	cam_periph_unlock(periph);
113876babe50SJustin T. Gibbs 
113976babe50SJustin T. Gibbs 	/*
114076babe50SJustin T. Gibbs 	 * Fill in the user status array.
114176babe50SJustin T. Gibbs 	 */
114276babe50SJustin T. Gibbs 	st_hdr = (struct read_element_status_header *)data;
1143b3b73720SKenneth D. Merry 	pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1144b3b73720SKenneth D. Merry 		  sizeof(struct read_element_status_header));
114576babe50SJustin T. Gibbs 	avail = scsi_2btoul(st_hdr->count);
114676babe50SJustin T. Gibbs 
114776babe50SJustin T. Gibbs 	if (avail != cesr->cesr_element_count) {
1148f0d9af51SMatt Jacob 		xpt_print(periph->path,
1149f0d9af51SMatt Jacob 		    "warning, READ ELEMENT STATUS avail != count\n");
115076babe50SJustin T. Gibbs 	}
115176babe50SJustin T. Gibbs 
115276babe50SJustin T. Gibbs 	user_data = (struct changer_element_status *)
115376babe50SJustin T. Gibbs 		malloc(avail * sizeof(struct changer_element_status),
1154a163d034SWarner Losh 		       M_DEVBUF, M_WAITOK | M_ZERO);
115576babe50SJustin T. Gibbs 
11561195ae18SBruce Evans 	desc = (struct read_element_status_descriptor *)((uintptr_t)data +
115776babe50SJustin T. Gibbs 		sizeof(struct read_element_status_header) +
115876babe50SJustin T. Gibbs 		sizeof(struct read_element_status_page_header));
115976babe50SJustin T. Gibbs 	/*
116076babe50SJustin T. Gibbs 	 * Set up the individual element status structures
116176babe50SJustin T. Gibbs 	 */
116276babe50SJustin T. Gibbs 	for (i = 0; i < avail; ++i) {
116376babe50SJustin T. Gibbs 		struct changer_element_status *ces = &(user_data[i]);
116476babe50SJustin T. Gibbs 
116576babe50SJustin T. Gibbs 		copy_element_status(softc, pg_hdr->flags, desc, ces);
116676babe50SJustin T. Gibbs 
11671195ae18SBruce Evans 		desc = (struct read_element_status_descriptor *)
11681195ae18SBruce Evans 		       ((uintptr_t)desc + desclen);
116976babe50SJustin T. Gibbs 	}
117076babe50SJustin T. Gibbs 
117176babe50SJustin T. Gibbs 	/* Copy element status structures out to userspace. */
117276babe50SJustin T. Gibbs 	error = copyout(user_data,
117376babe50SJustin T. Gibbs 			cesr->cesr_element_status,
117476babe50SJustin T. Gibbs 			avail * sizeof(struct changer_element_status));
11752b83592fSScott Long 	cam_periph_lock(periph);
117676babe50SJustin T. Gibbs 
117776babe50SJustin T. Gibbs  done:
117876babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
117976babe50SJustin T. Gibbs 
118076babe50SJustin T. Gibbs 	if (data != NULL)
118176babe50SJustin T. Gibbs 		free(data, M_DEVBUF);
118276babe50SJustin T. Gibbs 	if (user_data != NULL)
118376babe50SJustin T. Gibbs 		free(user_data, M_DEVBUF);
118476babe50SJustin T. Gibbs 
118576babe50SJustin T. Gibbs 	return (error);
118676babe50SJustin T. Gibbs }
118776babe50SJustin T. Gibbs 
118876babe50SJustin T. Gibbs static int
118976babe50SJustin T. Gibbs chielem(struct cam_periph *periph,
119076babe50SJustin T. Gibbs 	unsigned int timeout)
119176babe50SJustin T. Gibbs {
119276babe50SJustin T. Gibbs 	union ccb *ccb;
119376babe50SJustin T. Gibbs 	struct ch_softc *softc;
119476babe50SJustin T. Gibbs 	int error;
119576babe50SJustin T. Gibbs 
119676babe50SJustin T. Gibbs 	if (!timeout) {
119776babe50SJustin T. Gibbs 		timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
119876babe50SJustin T. Gibbs 	} else {
119976babe50SJustin T. Gibbs 		timeout *= 1000;
120076babe50SJustin T. Gibbs 	}
120176babe50SJustin T. Gibbs 
120276babe50SJustin T. Gibbs 	error = 0;
120376babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
120476babe50SJustin T. Gibbs 
1205bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
120676babe50SJustin T. Gibbs 
120776babe50SJustin T. Gibbs 	scsi_initialize_element_status(&ccb->csio,
120876babe50SJustin T. Gibbs 				      /* retries */ 1,
120976babe50SJustin T. Gibbs 				      /* cbfcnp */ chdone,
121076babe50SJustin T. Gibbs 				      /* tag_action */ MSG_SIMPLE_Q_TAG,
121176babe50SJustin T. Gibbs 				      /* sense_len */ SSD_FULL_SIZE,
121276babe50SJustin T. Gibbs 				      /* timeout */ timeout);
121376babe50SJustin T. Gibbs 
12143393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
12153393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1216a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
121776babe50SJustin T. Gibbs 
121876babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
121976babe50SJustin T. Gibbs 
122076babe50SJustin T. Gibbs 	return(error);
122176babe50SJustin T. Gibbs }
122276babe50SJustin T. Gibbs 
122376babe50SJustin T. Gibbs static int
122476babe50SJustin T. Gibbs chsetvoltag(struct cam_periph *periph,
122576babe50SJustin T. Gibbs 	    struct changer_set_voltag_request *csvr)
122676babe50SJustin T. Gibbs {
122776babe50SJustin T. Gibbs 	union ccb *ccb;
122876babe50SJustin T. Gibbs 	struct ch_softc *softc;
122976babe50SJustin T. Gibbs 	u_int16_t ea;
123076babe50SJustin T. Gibbs 	u_int8_t sac;
123176babe50SJustin T. Gibbs 	struct scsi_send_volume_tag_parameters ssvtp;
123276babe50SJustin T. Gibbs 	int error;
123376babe50SJustin T. Gibbs 	int i;
123476babe50SJustin T. Gibbs 
123576babe50SJustin T. Gibbs 	error = 0;
123676babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
123776babe50SJustin T. Gibbs 
123876babe50SJustin T. Gibbs 	bzero(&ssvtp, sizeof(ssvtp));
123976babe50SJustin T. Gibbs 	for (i=0; i<sizeof(ssvtp.vitf); i++) {
124076babe50SJustin T. Gibbs 		ssvtp.vitf[i] = ' ';
124176babe50SJustin T. Gibbs 	}
124276babe50SJustin T. Gibbs 
124376babe50SJustin T. Gibbs 	/*
124476babe50SJustin T. Gibbs 	 * Check arguments.
124576babe50SJustin T. Gibbs 	 */
124676babe50SJustin T. Gibbs 	if (csvr->csvr_type > CHET_DT)
124776babe50SJustin T. Gibbs 		return EINVAL;
124876babe50SJustin T. Gibbs 	if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
124976babe50SJustin T. Gibbs 		return ENODEV;
125076babe50SJustin T. Gibbs 
125176babe50SJustin T. Gibbs 	ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
125276babe50SJustin T. Gibbs 
125376babe50SJustin T. Gibbs 	if (csvr->csvr_flags & CSVR_ALTERNATE) {
125476babe50SJustin T. Gibbs 		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
125576babe50SJustin T. Gibbs 		case CSVR_MODE_SET:
125676babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
125776babe50SJustin T. Gibbs 			break;
125876babe50SJustin T. Gibbs 		case CSVR_MODE_REPLACE:
125976babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
126076babe50SJustin T. Gibbs 			break;
126176babe50SJustin T. Gibbs 		case CSVR_MODE_CLEAR:
126276babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
126376babe50SJustin T. Gibbs 			break;
126476babe50SJustin T. Gibbs 		default:
126576babe50SJustin T. Gibbs 			error = EINVAL;
126676babe50SJustin T. Gibbs 			goto out;
126776babe50SJustin T. Gibbs 		}
126876babe50SJustin T. Gibbs 	} else {
126976babe50SJustin T. Gibbs 		switch (csvr->csvr_flags & CSVR_MODE_MASK) {
127076babe50SJustin T. Gibbs 		case CSVR_MODE_SET:
127176babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
127276babe50SJustin T. Gibbs 			break;
127376babe50SJustin T. Gibbs 		case CSVR_MODE_REPLACE:
127476babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
127576babe50SJustin T. Gibbs 			break;
127676babe50SJustin T. Gibbs 		case CSVR_MODE_CLEAR:
127776babe50SJustin T. Gibbs 			sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
127876babe50SJustin T. Gibbs 			break;
127976babe50SJustin T. Gibbs 		default:
128076babe50SJustin T. Gibbs 			error = EINVAL;
128176babe50SJustin T. Gibbs 			goto out;
128276babe50SJustin T. Gibbs 		}
128376babe50SJustin T. Gibbs 	}
128476babe50SJustin T. Gibbs 
128576babe50SJustin T. Gibbs 	memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
128676babe50SJustin T. Gibbs 	       min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
128776babe50SJustin T. Gibbs 	scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
128876babe50SJustin T. Gibbs 
1289bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
129076babe50SJustin T. Gibbs 
129176babe50SJustin T. Gibbs 	scsi_send_volume_tag(&ccb->csio,
129276babe50SJustin T. Gibbs 			     /* retries */ 1,
129376babe50SJustin T. Gibbs 			     /* cbfcnp */ chdone,
129476babe50SJustin T. Gibbs 			     /* tag_action */ MSG_SIMPLE_Q_TAG,
129576babe50SJustin T. Gibbs 			     /* element_address */ ea,
129676babe50SJustin T. Gibbs 			     /* send_action_code */ sac,
129776babe50SJustin T. Gibbs 			     /* parameters */ &ssvtp,
129876babe50SJustin T. Gibbs 			     /* sense_len */ SSD_FULL_SIZE,
129976babe50SJustin T. Gibbs 			     /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
130076babe50SJustin T. Gibbs 
13013393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
13023393f8daSKenneth D. Merry 				  /*sense_flags*/ SF_RETRY_UA,
1303a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
130476babe50SJustin T. Gibbs 
130576babe50SJustin T. Gibbs 	xpt_release_ccb(ccb);
130676babe50SJustin T. Gibbs 
130776babe50SJustin T. Gibbs  out:
130876babe50SJustin T. Gibbs 	return error;
130976babe50SJustin T. Gibbs }
131076babe50SJustin T. Gibbs 
131176babe50SJustin T. Gibbs static int
131276babe50SJustin T. Gibbs chgetparams(struct cam_periph *periph)
131376babe50SJustin T. Gibbs {
131476babe50SJustin T. Gibbs 	union ccb *ccb;
131576babe50SJustin T. Gibbs 	struct ch_softc *softc;
1316696db222SKenneth D. Merry 	void *mode_buffer;
1317696db222SKenneth D. Merry 	int mode_buffer_len;
1318696db222SKenneth D. Merry 	struct page_element_address_assignment *ea;
1319696db222SKenneth D. Merry 	struct page_device_capabilities *cap;
1320696db222SKenneth D. Merry 	int error, from, dbd;
132176babe50SJustin T. Gibbs 	u_int8_t *moves, *exchanges;
132276babe50SJustin T. Gibbs 
132376babe50SJustin T. Gibbs 	error = 0;
132476babe50SJustin T. Gibbs 
132576babe50SJustin T. Gibbs 	softc = (struct ch_softc *)periph->softc;
132676babe50SJustin T. Gibbs 
1327bbfa4aa1SAlexander Motin 	ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
132876babe50SJustin T. Gibbs 
1329696db222SKenneth D. Merry 	/*
1330696db222SKenneth D. Merry 	 * The scsi_mode_sense_data structure is just a convenience
1331696db222SKenneth D. Merry 	 * structure that allows us to easily calculate the worst-case
1332696db222SKenneth D. Merry 	 * storage size of the mode sense buffer.
1333696db222SKenneth D. Merry 	 */
1334696db222SKenneth D. Merry 	mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1335696db222SKenneth D. Merry 
13360dd50e9bSScott Long 	mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
1337696db222SKenneth D. Merry 
1338696db222SKenneth D. Merry 	if (mode_buffer == NULL) {
133976babe50SJustin T. Gibbs 		printf("chgetparams: couldn't malloc mode sense data\n");
134076babe50SJustin T. Gibbs 		return(ENOSPC);
134176babe50SJustin T. Gibbs 	}
134276babe50SJustin T. Gibbs 
1343696db222SKenneth D. Merry 	bzero(mode_buffer, mode_buffer_len);
1344696db222SKenneth D. Merry 
1345696db222SKenneth D. Merry 	if (softc->quirks & CH_Q_NO_DBD)
1346696db222SKenneth D. Merry 		dbd = FALSE;
1347696db222SKenneth D. Merry 	else
1348696db222SKenneth D. Merry 		dbd = TRUE;
134976babe50SJustin T. Gibbs 
135076babe50SJustin T. Gibbs 	/*
135176babe50SJustin T. Gibbs 	 * Get the element address assignment page.
135276babe50SJustin T. Gibbs 	 */
135376babe50SJustin T. Gibbs 	scsi_mode_sense(&ccb->csio,
135476babe50SJustin T. Gibbs 			/* retries */ 1,
135576babe50SJustin T. Gibbs 			/* cbfcnp */ chdone,
135676babe50SJustin T. Gibbs 			/* tag_action */ MSG_SIMPLE_Q_TAG,
1357696db222SKenneth D. Merry 			/* dbd */ dbd,
135876babe50SJustin T. Gibbs 			/* page_code */ SMS_PAGE_CTRL_CURRENT,
135976babe50SJustin T. Gibbs 			/* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1360696db222SKenneth D. Merry 			/* param_buf */ (u_int8_t *)mode_buffer,
1361696db222SKenneth D. Merry 			/* param_len */ mode_buffer_len,
136276babe50SJustin T. Gibbs 			/* sense_len */ SSD_FULL_SIZE,
136376babe50SJustin T. Gibbs 			/* timeout */ CH_TIMEOUT_MODE_SENSE);
136476babe50SJustin T. Gibbs 
13653393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
13663393f8daSKenneth D. Merry 				  /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1367a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
136876babe50SJustin T. Gibbs 
136976babe50SJustin T. Gibbs 	if (error) {
1370696db222SKenneth D. Merry 		if (dbd) {
1371696db222SKenneth D. Merry 			struct scsi_mode_sense_6 *sms;
1372696db222SKenneth D. Merry 
1373696db222SKenneth D. Merry 			sms = (struct scsi_mode_sense_6 *)
1374696db222SKenneth D. Merry 				ccb->csio.cdb_io.cdb_bytes;
1375696db222SKenneth D. Merry 
1376696db222SKenneth D. Merry 			sms->byte2 &= ~SMS_DBD;
13773393f8daSKenneth D. Merry 			error = cam_periph_runccb(ccb, cherror,
13783393f8daSKenneth D. Merry 						  /*cam_flags*/ CAM_RETRY_SELTO,
13793393f8daSKenneth D. Merry 				  		  /*sense_flags*/ SF_RETRY_UA,
1380a9d2245eSPoul-Henning Kamp 						  softc->device_stats);
1381696db222SKenneth D. Merry 		} else {
1382696db222SKenneth D. Merry 			/*
1383696db222SKenneth D. Merry 			 * Since we disabled sense printing above, print
1384696db222SKenneth D. Merry 			 * out the sense here since we got an error.
1385696db222SKenneth D. Merry 			 */
1386696db222SKenneth D. Merry 			scsi_sense_print(&ccb->csio);
138776babe50SJustin T. Gibbs 		}
138876babe50SJustin T. Gibbs 
1389696db222SKenneth D. Merry 		if (error) {
1390f0d9af51SMatt Jacob 			xpt_print(periph->path,
1391f0d9af51SMatt Jacob 			    "chgetparams: error getting element "
1392696db222SKenneth D. Merry 			    "address page\n");
1393696db222SKenneth D. Merry 			xpt_release_ccb(ccb);
13940dd50e9bSScott Long 			free(mode_buffer, M_SCSICH);
1395696db222SKenneth D. Merry 			return(error);
1396696db222SKenneth D. Merry 		}
1397696db222SKenneth D. Merry 	}
139876babe50SJustin T. Gibbs 
1399696db222SKenneth D. Merry 	ea = (struct page_element_address_assignment *)
1400696db222SKenneth D. Merry 		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1401696db222SKenneth D. Merry 
1402696db222SKenneth D. Merry 	softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1403696db222SKenneth D. Merry 	softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1404696db222SKenneth D. Merry 	softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1405696db222SKenneth D. Merry 	softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1406696db222SKenneth D. Merry 	softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1407696db222SKenneth D. Merry 	softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1408696db222SKenneth D. Merry 	softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1409696db222SKenneth D. Merry 	softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1410696db222SKenneth D. Merry 
1411696db222SKenneth D. Merry 	bzero(mode_buffer, mode_buffer_len);
141276babe50SJustin T. Gibbs 
141376babe50SJustin T. Gibbs 	/*
141476babe50SJustin T. Gibbs 	 * Now get the device capabilities page.
141576babe50SJustin T. Gibbs 	 */
141676babe50SJustin T. Gibbs 	scsi_mode_sense(&ccb->csio,
141776babe50SJustin T. Gibbs 			/* retries */ 1,
141876babe50SJustin T. Gibbs 			/* cbfcnp */ chdone,
141976babe50SJustin T. Gibbs 			/* tag_action */ MSG_SIMPLE_Q_TAG,
1420696db222SKenneth D. Merry 			/* dbd */ dbd,
142176babe50SJustin T. Gibbs 			/* page_code */ SMS_PAGE_CTRL_CURRENT,
142276babe50SJustin T. Gibbs 			/* page */ CH_DEVICE_CAP_PAGE,
1423696db222SKenneth D. Merry 			/* param_buf */ (u_int8_t *)mode_buffer,
1424696db222SKenneth D. Merry 			/* param_len */ mode_buffer_len,
142576babe50SJustin T. Gibbs 			/* sense_len */ SSD_FULL_SIZE,
142676babe50SJustin T. Gibbs 			/* timeout */ CH_TIMEOUT_MODE_SENSE);
142776babe50SJustin T. Gibbs 
14283393f8daSKenneth D. Merry 	error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
14293393f8daSKenneth D. Merry 				  /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1430a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
143176babe50SJustin T. Gibbs 
1432696db222SKenneth D. Merry 	if (error) {
1433696db222SKenneth D. Merry 		if (dbd) {
1434696db222SKenneth D. Merry 			struct scsi_mode_sense_6 *sms;
1435696db222SKenneth D. Merry 
1436696db222SKenneth D. Merry 			sms = (struct scsi_mode_sense_6 *)
1437696db222SKenneth D. Merry 				ccb->csio.cdb_io.cdb_bytes;
1438696db222SKenneth D. Merry 
1439696db222SKenneth D. Merry 			sms->byte2 &= ~SMS_DBD;
14403393f8daSKenneth D. Merry 			error = cam_periph_runccb(ccb, cherror,
14413393f8daSKenneth D. Merry 						  /*cam_flags*/ CAM_RETRY_SELTO,
14423393f8daSKenneth D. Merry 				  		  /*sense_flags*/ SF_RETRY_UA,
1443a9d2245eSPoul-Henning Kamp 						  softc->device_stats);
1444696db222SKenneth D. Merry 		} else {
1445696db222SKenneth D. Merry 			/*
1446696db222SKenneth D. Merry 			 * Since we disabled sense printing above, print
1447696db222SKenneth D. Merry 			 * out the sense here since we got an error.
1448696db222SKenneth D. Merry 			 */
1449696db222SKenneth D. Merry 			scsi_sense_print(&ccb->csio);
1450696db222SKenneth D. Merry 		}
145176babe50SJustin T. Gibbs 
145276babe50SJustin T. Gibbs 		if (error) {
1453f0d9af51SMatt Jacob 			xpt_print(periph->path,
1454f0d9af51SMatt Jacob 			    "chgetparams: error getting device "
1455696db222SKenneth D. Merry 			    "capabilities page\n");
1456696db222SKenneth D. Merry 			xpt_release_ccb(ccb);
14570dd50e9bSScott Long 			free(mode_buffer, M_SCSICH);
145876babe50SJustin T. Gibbs 			return(error);
145976babe50SJustin T. Gibbs 		}
1460696db222SKenneth D. Merry 	}
146176babe50SJustin T. Gibbs 
1462696db222SKenneth D. Merry 	xpt_release_ccb(ccb);
1463696db222SKenneth D. Merry 
1464696db222SKenneth D. Merry 	cap = (struct page_device_capabilities *)
1465696db222SKenneth D. Merry 		find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
146676babe50SJustin T. Gibbs 
146776babe50SJustin T. Gibbs 	bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
146876babe50SJustin T. Gibbs 	bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
146956824e13SPoul-Henning Kamp 	moves = cap->move_from;
147056824e13SPoul-Henning Kamp 	exchanges = cap->exchange_with;
147156824e13SPoul-Henning Kamp 	for (from = CHET_MT; from <= CHET_MAX; ++from) {
147276babe50SJustin T. Gibbs 		softc->sc_movemask[from] = moves[from];
147376babe50SJustin T. Gibbs 		softc->sc_exchangemask[from] = exchanges[from];
147476babe50SJustin T. Gibbs 	}
147576babe50SJustin T. Gibbs 
14760dd50e9bSScott Long 	free(mode_buffer, M_SCSICH);
1477696db222SKenneth D. Merry 
147876babe50SJustin T. Gibbs 	return(error);
147976babe50SJustin T. Gibbs }
148076babe50SJustin T. Gibbs 
148176babe50SJustin T. Gibbs void
148276babe50SJustin T. Gibbs scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
148376babe50SJustin T. Gibbs 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
148476babe50SJustin T. Gibbs 		 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
148576babe50SJustin T. Gibbs 		 u_int32_t dst, int invert, u_int8_t sense_len,
148676babe50SJustin T. Gibbs 		 u_int32_t timeout)
148776babe50SJustin T. Gibbs {
148876babe50SJustin T. Gibbs 	struct scsi_move_medium *scsi_cmd;
148976babe50SJustin T. Gibbs 
149076babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
149176babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
149276babe50SJustin T. Gibbs 
149376babe50SJustin T. Gibbs 	scsi_cmd->opcode = MOVE_MEDIUM;
149476babe50SJustin T. Gibbs 
149576babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
149676babe50SJustin T. Gibbs 	scsi_ulto2b(src, scsi_cmd->src);
149776babe50SJustin T. Gibbs 	scsi_ulto2b(dst, scsi_cmd->dst);
149876babe50SJustin T. Gibbs 
149976babe50SJustin T. Gibbs 	if (invert)
150076babe50SJustin T. Gibbs 		scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
150176babe50SJustin T. Gibbs 
150276babe50SJustin T. Gibbs 	cam_fill_csio(csio,
150376babe50SJustin T. Gibbs 		      retries,
150476babe50SJustin T. Gibbs 		      cbfcnp,
150576babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
150676babe50SJustin T. Gibbs 		      tag_action,
150776babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
150876babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
150976babe50SJustin T. Gibbs 		      sense_len,
151076babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
151176babe50SJustin T. Gibbs 		      timeout);
151276babe50SJustin T. Gibbs }
151376babe50SJustin T. Gibbs 
151476babe50SJustin T. Gibbs void
151576babe50SJustin T. Gibbs scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
151676babe50SJustin T. Gibbs 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
151776babe50SJustin T. Gibbs 		     u_int8_t tag_action, u_int32_t tea, u_int32_t src,
151876babe50SJustin T. Gibbs 		     u_int32_t dst1, u_int32_t dst2, int invert1,
151976babe50SJustin T. Gibbs 		     int invert2, u_int8_t sense_len, u_int32_t timeout)
152076babe50SJustin T. Gibbs {
152176babe50SJustin T. Gibbs 	struct scsi_exchange_medium *scsi_cmd;
152276babe50SJustin T. Gibbs 
152376babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
152476babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
152576babe50SJustin T. Gibbs 
152676babe50SJustin T. Gibbs 	scsi_cmd->opcode = EXCHANGE_MEDIUM;
152776babe50SJustin T. Gibbs 
152876babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
152976babe50SJustin T. Gibbs 	scsi_ulto2b(src, scsi_cmd->src);
153076babe50SJustin T. Gibbs 	scsi_ulto2b(dst1, scsi_cmd->fdst);
153176babe50SJustin T. Gibbs 	scsi_ulto2b(dst2, scsi_cmd->sdst);
153276babe50SJustin T. Gibbs 
153376babe50SJustin T. Gibbs 	if (invert1)
153476babe50SJustin T. Gibbs 		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
153576babe50SJustin T. Gibbs 
153676babe50SJustin T. Gibbs 	if (invert2)
153776babe50SJustin T. Gibbs 		scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
153876babe50SJustin T. Gibbs 
153976babe50SJustin T. Gibbs 	cam_fill_csio(csio,
154076babe50SJustin T. Gibbs 		      retries,
154176babe50SJustin T. Gibbs 		      cbfcnp,
154276babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
154376babe50SJustin T. Gibbs 		      tag_action,
154476babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
154576babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
154676babe50SJustin T. Gibbs 		      sense_len,
154776babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
154876babe50SJustin T. Gibbs 		      timeout);
154976babe50SJustin T. Gibbs }
155076babe50SJustin T. Gibbs 
155176babe50SJustin T. Gibbs void
155276babe50SJustin T. Gibbs scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
155376babe50SJustin T. Gibbs 			 void (*cbfcnp)(struct cam_periph *, union ccb *),
155476babe50SJustin T. Gibbs 			 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
155576babe50SJustin T. Gibbs 			 int invert, u_int8_t sense_len, u_int32_t timeout)
155676babe50SJustin T. Gibbs {
155776babe50SJustin T. Gibbs 	struct scsi_position_to_element *scsi_cmd;
155876babe50SJustin T. Gibbs 
155976babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
156076babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
156176babe50SJustin T. Gibbs 
156276babe50SJustin T. Gibbs 	scsi_cmd->opcode = POSITION_TO_ELEMENT;
156376babe50SJustin T. Gibbs 
156476babe50SJustin T. Gibbs 	scsi_ulto2b(tea, scsi_cmd->tea);
156576babe50SJustin T. Gibbs 	scsi_ulto2b(dst, scsi_cmd->dst);
156676babe50SJustin T. Gibbs 
156776babe50SJustin T. Gibbs 	if (invert)
156876babe50SJustin T. Gibbs 		scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
156976babe50SJustin T. Gibbs 
157076babe50SJustin T. Gibbs 	cam_fill_csio(csio,
157176babe50SJustin T. Gibbs 		      retries,
157276babe50SJustin T. Gibbs 		      cbfcnp,
157376babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
157476babe50SJustin T. Gibbs 		      tag_action,
157576babe50SJustin T. Gibbs 		      /*data_ptr*/ NULL,
157676babe50SJustin T. Gibbs 		      /*dxfer_len*/ 0,
157776babe50SJustin T. Gibbs 		      sense_len,
157876babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
157976babe50SJustin T. Gibbs 		      timeout);
158076babe50SJustin T. Gibbs }
158176babe50SJustin T. Gibbs 
158276babe50SJustin T. Gibbs void
158376babe50SJustin T. Gibbs scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
158476babe50SJustin T. Gibbs 			 void (*cbfcnp)(struct cam_periph *, union ccb *),
158576babe50SJustin T. Gibbs 			 u_int8_t tag_action, int voltag, u_int32_t sea,
158676babe50SJustin T. Gibbs 			 u_int32_t count, u_int8_t *data_ptr,
158776babe50SJustin T. Gibbs 			 u_int32_t dxfer_len, u_int8_t sense_len,
158876babe50SJustin T. Gibbs 			 u_int32_t timeout)
158976babe50SJustin T. Gibbs {
159076babe50SJustin T. Gibbs 	struct scsi_read_element_status *scsi_cmd;
159176babe50SJustin T. Gibbs 
159276babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
159376babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
159476babe50SJustin T. Gibbs 
159576babe50SJustin T. Gibbs 	scsi_cmd->opcode = READ_ELEMENT_STATUS;
159676babe50SJustin T. Gibbs 
159776babe50SJustin T. Gibbs 	scsi_ulto2b(sea, scsi_cmd->sea);
159876babe50SJustin T. Gibbs 	scsi_ulto2b(count, scsi_cmd->count);
159976babe50SJustin T. Gibbs 	scsi_ulto3b(dxfer_len, scsi_cmd->len);
160076babe50SJustin T. Gibbs 
160176babe50SJustin T. Gibbs 	if (voltag)
160276babe50SJustin T. Gibbs 		scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
160376babe50SJustin T. Gibbs 
160476babe50SJustin T. Gibbs 	cam_fill_csio(csio,
160576babe50SJustin T. Gibbs 		      retries,
160676babe50SJustin T. Gibbs 		      cbfcnp,
160776babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_IN,
160876babe50SJustin T. Gibbs 		      tag_action,
160976babe50SJustin T. Gibbs 		      data_ptr,
161076babe50SJustin T. Gibbs 		      dxfer_len,
161176babe50SJustin T. Gibbs 		      sense_len,
161276babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
161376babe50SJustin T. Gibbs 		      timeout);
161476babe50SJustin T. Gibbs }
161576babe50SJustin T. Gibbs 
161676babe50SJustin T. Gibbs void
161776babe50SJustin T. Gibbs scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
161876babe50SJustin T. Gibbs 			       void (*cbfcnp)(struct cam_periph *, union ccb *),
161976babe50SJustin T. Gibbs 			       u_int8_t tag_action, u_int8_t sense_len,
162076babe50SJustin T. Gibbs 			       u_int32_t timeout)
162176babe50SJustin T. Gibbs {
162276babe50SJustin T. Gibbs 	struct scsi_initialize_element_status *scsi_cmd;
162376babe50SJustin T. Gibbs 
162476babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_initialize_element_status *)
162576babe50SJustin T. Gibbs 		    &csio->cdb_io.cdb_bytes;
162676babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
162776babe50SJustin T. Gibbs 
162876babe50SJustin T. Gibbs 	scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
162976babe50SJustin T. Gibbs 
163076babe50SJustin T. Gibbs 	cam_fill_csio(csio,
163176babe50SJustin T. Gibbs 		      retries,
163276babe50SJustin T. Gibbs 		      cbfcnp,
163376babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_NONE,
163476babe50SJustin T. Gibbs 		      tag_action,
163576babe50SJustin T. Gibbs 		      /* data_ptr */ NULL,
163676babe50SJustin T. Gibbs 		      /* dxfer_len */ 0,
163776babe50SJustin T. Gibbs 		      sense_len,
163876babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
163976babe50SJustin T. Gibbs 		      timeout);
164076babe50SJustin T. Gibbs }
164176babe50SJustin T. Gibbs 
164276babe50SJustin T. Gibbs void
164376babe50SJustin T. Gibbs scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
164476babe50SJustin T. Gibbs 		     void (*cbfcnp)(struct cam_periph *, union ccb *),
164576babe50SJustin T. Gibbs 		     u_int8_t tag_action,
164676babe50SJustin T. Gibbs 		     u_int16_t element_address,
164776babe50SJustin T. Gibbs 		     u_int8_t send_action_code,
164876babe50SJustin T. Gibbs 		     struct scsi_send_volume_tag_parameters *parameters,
164976babe50SJustin T. Gibbs 		     u_int8_t sense_len, u_int32_t timeout)
165076babe50SJustin T. Gibbs {
165176babe50SJustin T. Gibbs 	struct scsi_send_volume_tag *scsi_cmd;
165276babe50SJustin T. Gibbs 
165376babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
165476babe50SJustin T. Gibbs 	bzero(scsi_cmd, sizeof(*scsi_cmd));
165576babe50SJustin T. Gibbs 
165676babe50SJustin T. Gibbs 	scsi_cmd->opcode = SEND_VOLUME_TAG;
165776babe50SJustin T. Gibbs 	scsi_ulto2b(element_address, scsi_cmd->ea);
165876babe50SJustin T. Gibbs 	scsi_cmd->sac = send_action_code;
165976babe50SJustin T. Gibbs 	scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
166076babe50SJustin T. Gibbs 
166176babe50SJustin T. Gibbs 	cam_fill_csio(csio,
166276babe50SJustin T. Gibbs 		      retries,
166376babe50SJustin T. Gibbs 		      cbfcnp,
166476babe50SJustin T. Gibbs 		      /*flags*/ CAM_DIR_OUT,
166576babe50SJustin T. Gibbs 		      tag_action,
166676babe50SJustin T. Gibbs 		      /* data_ptr */ (u_int8_t *) parameters,
166776babe50SJustin T. Gibbs 		      sizeof(*parameters),
166876babe50SJustin T. Gibbs 		      sense_len,
166976babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
167076babe50SJustin T. Gibbs 		      timeout);
167176babe50SJustin T. Gibbs }
1672