xref: /onnv-gate/usr/src/uts/intel/io/dktp/disk/cmdk.c (revision 7573:2fcc705aedc0)
11709Smlf /*
21709Smlf  * CDDL HEADER START
31709Smlf  *
41709Smlf  * The contents of this file are subject to the terms of the
53525Sshidokht  * Common Development and Distribution License (the "License").
63525Sshidokht  * You may not use this file except in compliance with the License.
71709Smlf  *
81709Smlf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91709Smlf  * or http://www.opensolaris.org/os/licensing.
101709Smlf  * See the License for the specific language governing permissions
111709Smlf  * and limitations under the License.
121709Smlf  *
131709Smlf  * When distributing Covered Code, include this CDDL HEADER in each
141709Smlf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151709Smlf  * If applicable, add the following below this CDDL HEADER, with the
161709Smlf  * fields enclosed by brackets "[]" replaced with your own identifying
171709Smlf  * information: Portions Copyright [yyyy] [name of copyright owner]
181709Smlf  *
191709Smlf  * CDDL HEADER END
201709Smlf  */
217563SPrasad.Singamsetty@Sun.COM 
221709Smlf /*
236318Sedp  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241709Smlf  * Use is subject to license terms.
251709Smlf  */
261709Smlf 
271709Smlf #include <sys/scsi/scsi.h>
281709Smlf #include <sys/dktp/cm.h>
291709Smlf #include <sys/dktp/quetypes.h>
301709Smlf #include <sys/dktp/queue.h>
311709Smlf #include <sys/dktp/fctypes.h>
321709Smlf #include <sys/dktp/flowctrl.h>
331709Smlf #include <sys/dktp/cmdev.h>
341709Smlf #include <sys/dkio.h>
351709Smlf #include <sys/dktp/tgdk.h>
361709Smlf #include <sys/dktp/dadk.h>
371709Smlf #include <sys/dktp/bbh.h>
381709Smlf #include <sys/dktp/altsctr.h>
391709Smlf #include <sys/dktp/cmdk.h>
401709Smlf 
411709Smlf #include <sys/stat.h>
421709Smlf #include <sys/vtoc.h>
431709Smlf #include <sys/file.h>
441709Smlf #include <sys/dktp/dadkio.h>
451709Smlf #include <sys/aio_req.h>
461709Smlf 
471709Smlf #include <sys/cmlb.h>
481709Smlf 
491709Smlf /*
501709Smlf  * Local Static Data
511709Smlf  */
521709Smlf #ifdef CMDK_DEBUG
531709Smlf #define	DENT	0x0001
541709Smlf #define	DIO	0x0002
551709Smlf 
561709Smlf static	int	cmdk_debug = DIO;
571709Smlf #endif
581709Smlf 
591709Smlf #ifndef	TRUE
601709Smlf #define	TRUE	1
611709Smlf #endif
621709Smlf 
631709Smlf #ifndef	FALSE
641709Smlf #define	FALSE	0
651709Smlf #endif
661709Smlf 
671709Smlf /*
681709Smlf  * NDKMAP is the base number for accessing the fdisk partitions.
691709Smlf  * c?d?p0 --> cmdk@?,?:q
701709Smlf  */
711709Smlf #define	PARTITION0_INDEX	(NDKMAP + 0)
721709Smlf 
731709Smlf #define	DKTP_DATA		(dkp->dk_tgobjp)->tg_data
741709Smlf #define	DKTP_EXT		(dkp->dk_tgobjp)->tg_ext
751709Smlf 
761709Smlf static void *cmdk_state;
771709Smlf 
781709Smlf /*
791709Smlf  * the cmdk_attach_mutex protects cmdk_max_instance in multi-threaded
801709Smlf  * attach situations
811709Smlf  */
821709Smlf static kmutex_t cmdk_attach_mutex;
831709Smlf static int cmdk_max_instance = 0;
841709Smlf 
851709Smlf /*
861709Smlf  * Panic dumpsys state
871709Smlf  * There is only a single flag that is not mutex locked since
881709Smlf  * the system is prevented from thread switching and cmdk_dump
891709Smlf  * will only be called in a single threaded operation.
901709Smlf  */
911709Smlf static int	cmdk_indump;
921709Smlf 
931709Smlf /*
941709Smlf  * Local Function Prototypes
951709Smlf  */
961709Smlf static int cmdk_create_obj(dev_info_t *dip, struct cmdk *dkp);
971709Smlf static void cmdk_destroy_obj(dev_info_t *dip, struct cmdk *dkp);
981709Smlf static void cmdkmin(struct buf *bp);
991709Smlf static int cmdkrw(dev_t dev, struct uio *uio, int flag);
1001709Smlf static int cmdkarw(dev_t dev, struct aio_req *aio, int flag);
1011709Smlf 
1021709Smlf /*
1031709Smlf  * Bad Block Handling Functions Prototypes
1041709Smlf  */
1051709Smlf static void cmdk_bbh_reopen(struct cmdk *dkp);
1061709Smlf static opaque_t cmdk_bbh_gethandle(opaque_t bbh_data, struct buf *bp);
1071709Smlf static bbh_cookie_t cmdk_bbh_htoc(opaque_t bbh_data, opaque_t handle);
1081709Smlf static void cmdk_bbh_freehandle(opaque_t bbh_data, opaque_t handle);
1091709Smlf static void cmdk_bbh_close(struct cmdk *dkp);
1101709Smlf static void cmdk_bbh_setalts_idx(struct cmdk *dkp);
1111709Smlf static int cmdk_bbh_bsearch(struct alts_ent *buf, int cnt, daddr32_t key);
1121709Smlf 
1131709Smlf static struct bbh_objops cmdk_bbh_ops = {
1141709Smlf 	nulldev,
1151709Smlf 	nulldev,
1161709Smlf 	cmdk_bbh_gethandle,
1171709Smlf 	cmdk_bbh_htoc,
1181709Smlf 	cmdk_bbh_freehandle,
1191709Smlf 	0, 0
1201709Smlf };
1211709Smlf 
1221709Smlf static int cmdkopen(dev_t *dev_p, int flag, int otyp, cred_t *credp);
1231709Smlf static int cmdkclose(dev_t dev, int flag, int otyp, cred_t *credp);
1241709Smlf static int cmdkstrategy(struct buf *bp);
1251709Smlf static int cmdkdump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1261709Smlf static int cmdkioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1271709Smlf static int cmdkread(dev_t dev, struct uio *uio, cred_t *credp);
1281709Smlf static int cmdkwrite(dev_t dev, struct uio *uio, cred_t *credp);
1291709Smlf static int cmdk_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1301709Smlf     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1311709Smlf static int cmdkaread(dev_t dev, struct aio_req *aio, cred_t *credp);
1321709Smlf static int cmdkawrite(dev_t dev, struct aio_req *aio, cred_t *credp);
1331709Smlf 
1341709Smlf /*
1351709Smlf  * Device driver ops vector
1361709Smlf  */
1371709Smlf 
1381709Smlf static struct cb_ops cmdk_cb_ops = {
1391709Smlf 	cmdkopen, 		/* open */
1401709Smlf 	cmdkclose, 		/* close */
1411709Smlf 	cmdkstrategy, 		/* strategy */
1421709Smlf 	nodev, 			/* print */
1431709Smlf 	cmdkdump, 		/* dump */
1441709Smlf 	cmdkread, 		/* read */
1451709Smlf 	cmdkwrite, 		/* write */
1461709Smlf 	cmdkioctl, 		/* ioctl */
1471709Smlf 	nodev, 			/* devmap */
1481709Smlf 	nodev, 			/* mmap */
1491709Smlf 	nodev, 			/* segmap */
1501709Smlf 	nochpoll, 		/* poll */
1511709Smlf 	cmdk_prop_op, 		/* cb_prop_op */
1521709Smlf 	0, 			/* streamtab  */
1531709Smlf 	D_64BIT | D_MP | D_NEW,	/* Driver comaptibility flag */
1541709Smlf 	CB_REV,			/* cb_rev */
1551709Smlf 	cmdkaread,		/* async read */
1561709Smlf 	cmdkawrite		/* async write */
1571709Smlf };
1581709Smlf 
1591709Smlf static int cmdkinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1601709Smlf     void **result);
1611709Smlf static int cmdkprobe(dev_info_t *dip);
1621709Smlf static int cmdkattach(dev_info_t *dip, ddi_attach_cmd_t cmd);
1631709Smlf static int cmdkdetach(dev_info_t *dip, ddi_detach_cmd_t cmd);
1641709Smlf 
1655295Srandyf static void cmdk_setup_pm(dev_info_t *dip, struct cmdk *dkp);
1665295Srandyf static int cmdkresume(dev_info_t *dip);
1675295Srandyf static int cmdksuspend(dev_info_t *dip);
1685295Srandyf static int cmdkpower(dev_info_t *dip, int component, int level);
1695295Srandyf 
1701709Smlf struct dev_ops cmdk_ops = {
1711709Smlf 	DEVO_REV, 		/* devo_rev, */
1721709Smlf 	0, 			/* refcnt  */
1731709Smlf 	cmdkinfo,		/* info */
1741709Smlf 	nulldev, 		/* identify */
1751709Smlf 	cmdkprobe, 		/* probe */
1761709Smlf 	cmdkattach, 		/* attach */
1771709Smlf 	cmdkdetach,		/* detach */
1781709Smlf 	nodev, 			/* reset */
1791709Smlf 	&cmdk_cb_ops, 		/* driver operations */
1805295Srandyf 	(struct bus_ops *)0,	/* bus operations */
1815295Srandyf 	cmdkpower		/* power */
1821709Smlf };
1831709Smlf 
1841709Smlf /*
1851709Smlf  * This is the loadable module wrapper.
1861709Smlf  */
1871709Smlf #include <sys/modctl.h>
1881709Smlf 
1891709Smlf extern struct mod_ops mod_driverops;
1901709Smlf 
1911709Smlf static struct modldrv modldrv = {
1921709Smlf 	&mod_driverops, 	/* Type of module. This one is a driver */
1937542SRichard.Bean@Sun.COM 	"Common Direct Access Disk",
1941709Smlf 	&cmdk_ops, 				/* driver ops 		*/
1951709Smlf };
1961709Smlf 
1971709Smlf static struct modlinkage modlinkage = {
1981709Smlf 	MODREV_1, (void *)&modldrv, NULL
1991709Smlf };
2001709Smlf 
2011709Smlf /* Function prototypes for cmlb callbacks */
2021709Smlf 
2031709Smlf static int cmdk_lb_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr,
2043525Sshidokht     diskaddr_t start, size_t length, void *tg_cookie);
2053525Sshidokht 
2063525Sshidokht static int cmdk_lb_getinfo(dev_info_t *dip, int cmd,  void *arg,
2073525Sshidokht     void *tg_cookie);
2081709Smlf 
2091709Smlf static void cmdk_devid_setup(struct cmdk *dkp);
2101709Smlf static int cmdk_devid_modser(struct cmdk *dkp);
2111709Smlf static int cmdk_get_modser(struct cmdk *dkp, int ioccmd, char *buf, int len);
2121709Smlf static int cmdk_devid_fabricate(struct cmdk *dkp);
2131709Smlf static int cmdk_devid_read(struct cmdk *dkp);
2141709Smlf 
2151709Smlf static cmlb_tg_ops_t cmdk_lb_ops = {
2163525Sshidokht 	TG_DK_OPS_VERSION_1,
2171709Smlf 	cmdk_lb_rdwr,
2183525Sshidokht 	cmdk_lb_getinfo
2191709Smlf };
2201709Smlf 
2216318Sedp static boolean_t
2226318Sedp cmdk_isopen(struct cmdk *dkp, dev_t dev)
2236318Sedp {
2246318Sedp 	int		part, otyp;
2256318Sedp 	ulong_t		partbit;
2266318Sedp 
2276318Sedp 	ASSERT(MUTEX_HELD((&dkp->dk_mutex)));
2286318Sedp 
2296318Sedp 	part = CMDKPART(dev);
2306318Sedp 	partbit = 1 << part;
2316318Sedp 
2326318Sedp 	/* account for close */
2336318Sedp 	if (dkp->dk_open_lyr[part] != 0)
2346318Sedp 		return (B_TRUE);
2356318Sedp 	for (otyp = 0; otyp < OTYPCNT; otyp++)
2366318Sedp 		if (dkp->dk_open_reg[otyp] & partbit)
2376318Sedp 			return (B_TRUE);
2386318Sedp 	return (B_FALSE);
2396318Sedp }
2406318Sedp 
2411709Smlf int
2421709Smlf _init(void)
2431709Smlf {
2441709Smlf 	int 	rval;
2451709Smlf 
2461709Smlf 	if (rval = ddi_soft_state_init(&cmdk_state, sizeof (struct cmdk), 7))
2471709Smlf 		return (rval);
2481709Smlf 
2491709Smlf 	mutex_init(&cmdk_attach_mutex, NULL, MUTEX_DRIVER, NULL);
2501709Smlf 	if ((rval = mod_install(&modlinkage)) != 0) {
2511709Smlf 		mutex_destroy(&cmdk_attach_mutex);
2521709Smlf 		ddi_soft_state_fini(&cmdk_state);
2531709Smlf 	}
2541709Smlf 	return (rval);
2551709Smlf }
2561709Smlf 
2571709Smlf int
2581709Smlf _fini(void)
2591709Smlf {
2601709Smlf 	return (EBUSY);
2611709Smlf 
2621709Smlf 	/*
2631709Smlf 	 * This has been commented out until cmdk is a true
2641709Smlf 	 * unloadable module. Right now x86's are panicking on
2651709Smlf 	 * a diskless reconfig boot.
2661709Smlf 	 */
2671709Smlf 
2681709Smlf #if 0 	/* bugid 1186679 */
2691709Smlf 	int	rval;
2701709Smlf 
2711709Smlf 	rval = mod_remove(&modlinkage);
2721709Smlf 	if (rval != 0)
2731709Smlf 		return (rval);
2741709Smlf 
2751709Smlf 	mutex_destroy(&cmdk_attach_mutex);
2761709Smlf 	ddi_soft_state_fini(&cmdk_state);
2771709Smlf 
2781709Smlf 	return (0);
2791709Smlf #endif
2801709Smlf }
2811709Smlf 
2821709Smlf int
2831709Smlf _info(struct modinfo *modinfop)
2841709Smlf {
2851709Smlf 	return (mod_info(&modlinkage, modinfop));
2861709Smlf }
2871709Smlf 
2881709Smlf /*
2891709Smlf  * Autoconfiguration Routines
2901709Smlf  */
2911709Smlf static int
2921709Smlf cmdkprobe(dev_info_t *dip)
2931709Smlf {
2941709Smlf 	int 	instance;
2951709Smlf 	int	status;
2961709Smlf 	struct	cmdk	*dkp;
2971709Smlf 
2981709Smlf 	instance = ddi_get_instance(dip);
2991709Smlf 
3001709Smlf 	if (ddi_get_soft_state(cmdk_state, instance))
3011709Smlf 		return (DDI_PROBE_PARTIAL);
3021709Smlf 
3031709Smlf 	if ((ddi_soft_state_zalloc(cmdk_state, instance) != DDI_SUCCESS) ||
3041709Smlf 	    ((dkp = ddi_get_soft_state(cmdk_state, instance)) == NULL))
3051709Smlf 		return (DDI_PROBE_PARTIAL);
3061709Smlf 
3071709Smlf 	mutex_init(&dkp->dk_mutex, NULL, MUTEX_DRIVER, NULL);
3081709Smlf 	rw_init(&dkp->dk_bbh_mutex, NULL, RW_DRIVER, NULL);
3091709Smlf 	dkp->dk_dip = dip;
3101709Smlf 	mutex_enter(&dkp->dk_mutex);
3111709Smlf 
3121709Smlf 	dkp->dk_dev = makedevice(ddi_driver_major(dip),
3131709Smlf 	    ddi_get_instance(dip) << CMDK_UNITSHF);
3141709Smlf 
3151709Smlf 	/* linkage to dadk and strategy */
3161709Smlf 	if (cmdk_create_obj(dip, dkp) != DDI_SUCCESS) {
3171709Smlf 		mutex_exit(&dkp->dk_mutex);
3181709Smlf 		mutex_destroy(&dkp->dk_mutex);
3191709Smlf 		rw_destroy(&dkp->dk_bbh_mutex);
3201709Smlf 		ddi_soft_state_free(cmdk_state, instance);
3211709Smlf 		return (DDI_PROBE_PARTIAL);
3221709Smlf 	}
3231709Smlf 
3241709Smlf 	status = dadk_probe(DKTP_DATA, KM_NOSLEEP);
3251709Smlf 	if (status != DDI_PROBE_SUCCESS) {
3261709Smlf 		cmdk_destroy_obj(dip, dkp);	/* dadk/strategy linkage  */
3271709Smlf 		mutex_exit(&dkp->dk_mutex);
3281709Smlf 		mutex_destroy(&dkp->dk_mutex);
3291709Smlf 		rw_destroy(&dkp->dk_bbh_mutex);
3301709Smlf 		ddi_soft_state_free(cmdk_state, instance);
3311709Smlf 		return (status);
3321709Smlf 	}
3331709Smlf 
3341709Smlf 	mutex_exit(&dkp->dk_mutex);
3351709Smlf #ifdef CMDK_DEBUG
3361709Smlf 	if (cmdk_debug & DENT)
3371709Smlf 		PRF("cmdkprobe: instance= %d name= `%s`\n",
3381709Smlf 		    instance, ddi_get_name_addr(dip));
3391709Smlf #endif
3401709Smlf 	return (status);
3411709Smlf }
3421709Smlf 
3431709Smlf static int
3441709Smlf cmdkattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3451709Smlf {
3461709Smlf 	int 		instance;
3471709Smlf 	struct		cmdk *dkp;
3481709Smlf 	char 		*node_type;
3491709Smlf 
3505295Srandyf 	switch (cmd) {
3515295Srandyf 	case DDI_ATTACH:
3525295Srandyf 		break;
3535295Srandyf 	case DDI_RESUME:
3545295Srandyf 		return (cmdkresume(dip));
3555295Srandyf 	default:
3561709Smlf 		return (DDI_FAILURE);
3575295Srandyf 	}
3581709Smlf 
3591709Smlf 	instance = ddi_get_instance(dip);
3601709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
3611709Smlf 		return (DDI_FAILURE);
3621709Smlf 
3635295Srandyf 	dkp->dk_pm_level = CMDK_SPINDLE_UNINIT;
3645295Srandyf 	mutex_init(&dkp->dk_mutex, NULL, MUTEX_DRIVER, NULL);
3655295Srandyf 
3661709Smlf 	mutex_enter(&dkp->dk_mutex);
3671709Smlf 
3681709Smlf 	/* dadk_attach is an empty function that only returns SUCCESS */
3691709Smlf 	(void) dadk_attach(DKTP_DATA);
3701709Smlf 
3711709Smlf 	node_type = (DKTP_EXT->tg_nodetype);
3721709Smlf 
3731709Smlf 	/*
3741709Smlf 	 * this open allows cmlb to read the device
3751709Smlf 	 * and determine the label types
3761709Smlf 	 * so that cmlb can create minor nodes for device
3771709Smlf 	 */
3781709Smlf 
3791709Smlf 	/* open the target disk	 */
3801709Smlf 	if (dadk_open(DKTP_DATA, 0) != DDI_SUCCESS)
3811709Smlf 		goto fail2;
3821709Smlf 
3837563SPrasad.Singamsetty@Sun.COM #ifdef _ILP32
3847563SPrasad.Singamsetty@Sun.COM 	{
3857563SPrasad.Singamsetty@Sun.COM 		struct  tgdk_geom phyg;
3867563SPrasad.Singamsetty@Sun.COM 		(void) dadk_getphygeom(DKTP_DATA, &phyg);
3877563SPrasad.Singamsetty@Sun.COM 		if ((phyg.g_cap - 1) > DK_MAX_BLOCKS) {
3887563SPrasad.Singamsetty@Sun.COM 			(void) dadk_close(DKTP_DATA);
3897563SPrasad.Singamsetty@Sun.COM 			goto fail2;
3907563SPrasad.Singamsetty@Sun.COM 		}
3917563SPrasad.Singamsetty@Sun.COM 	}
3927563SPrasad.Singamsetty@Sun.COM #endif
3937563SPrasad.Singamsetty@Sun.COM 
3947563SPrasad.Singamsetty@Sun.COM 
3951709Smlf 	/* mark as having opened target */
3961709Smlf 	dkp->dk_flag |= CMDK_TGDK_OPEN;
3971709Smlf 
3981709Smlf 	cmlb_alloc_handle((cmlb_handle_t *)&dkp->dk_cmlbhandle);
3991709Smlf 
4001709Smlf 	if (cmlb_attach(dip,
4011709Smlf 	    &cmdk_lb_ops,
4021709Smlf 	    DTYPE_DIRECT,		/* device_type */
4031709Smlf 	    0,				/* removable */
4043525Sshidokht 	    0,				/* hot pluggable XXX */
4051709Smlf 	    node_type,
4061709Smlf 	    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT,	/* alter_behaviour */
4073525Sshidokht 	    dkp->dk_cmlbhandle,
4083525Sshidokht 	    0) != 0)
4091709Smlf 		goto fail1;
4101709Smlf 
4111709Smlf 	/* Calling validate will create minor nodes according to disk label */
4123525Sshidokht 	(void) cmlb_validate(dkp->dk_cmlbhandle, 0, 0);
4131709Smlf 
4141709Smlf 	/* set bbh (Bad Block Handling) */
4151709Smlf 	cmdk_bbh_reopen(dkp);
4161709Smlf 
4171709Smlf 	/* setup devid string */
4181709Smlf 	cmdk_devid_setup(dkp);
4191709Smlf 
4201709Smlf 	mutex_enter(&cmdk_attach_mutex);
4211709Smlf 	if (instance > cmdk_max_instance)
4221709Smlf 		cmdk_max_instance = instance;
4231709Smlf 	mutex_exit(&cmdk_attach_mutex);
4241709Smlf 
4251709Smlf 	mutex_exit(&dkp->dk_mutex);
4261709Smlf 
4271709Smlf 	/*
4281709Smlf 	 * Add a zero-length attribute to tell the world we support
4291709Smlf 	 * kernel ioctls (for layered drivers)
4301709Smlf 	 */
4311709Smlf 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
4321709Smlf 	    DDI_KERNEL_IOCTL, NULL, 0);
4331709Smlf 	ddi_report_dev(dip);
4341709Smlf 
4355295Srandyf 	/*
4365295Srandyf 	 * Initialize power management
4375295Srandyf 	 */
4385295Srandyf 	mutex_init(&dkp->dk_pm_mutex, NULL, MUTEX_DRIVER, NULL);
4395295Srandyf 	cv_init(&dkp->dk_suspend_cv,   NULL, CV_DRIVER, NULL);
4405295Srandyf 	cmdk_setup_pm(dip, dkp);
4415295Srandyf 
4421709Smlf 	return (DDI_SUCCESS);
4431709Smlf 
4441709Smlf fail1:
4451709Smlf 	cmlb_free_handle(&dkp->dk_cmlbhandle);
4461709Smlf 	(void) dadk_close(DKTP_DATA);
4471709Smlf fail2:
4481709Smlf 	cmdk_destroy_obj(dip, dkp);
4491709Smlf 	rw_destroy(&dkp->dk_bbh_mutex);
4501709Smlf 	mutex_exit(&dkp->dk_mutex);
4511709Smlf 	mutex_destroy(&dkp->dk_mutex);
4521709Smlf 	ddi_soft_state_free(cmdk_state, instance);
4531709Smlf 	return (DDI_FAILURE);
4541709Smlf }
4551709Smlf 
4561709Smlf 
4571709Smlf static int
4581709Smlf cmdkdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4591709Smlf {
4601709Smlf 	struct cmdk	*dkp;
4611709Smlf 	int 		instance;
4621709Smlf 	int		max_instance;
4631709Smlf 
4645295Srandyf 	switch (cmd) {
4655295Srandyf 	case DDI_DETACH:
4665295Srandyf 		/* return (DDI_FAILURE); */
4675295Srandyf 		break;
4685295Srandyf 	case DDI_SUSPEND:
4695295Srandyf 		return (cmdksuspend(dip));
4705295Srandyf 	default:
4711709Smlf #ifdef CMDK_DEBUG
4721709Smlf 		if (cmdk_debug & DIO) {
4731709Smlf 			PRF("cmdkdetach: cmd = %d unknown\n", cmd);
4741709Smlf 		}
4751709Smlf #endif
4761709Smlf 		return (DDI_FAILURE);
4771709Smlf 	}
4781709Smlf 
4791709Smlf 	mutex_enter(&cmdk_attach_mutex);
4801709Smlf 	max_instance = cmdk_max_instance;
4811709Smlf 	mutex_exit(&cmdk_attach_mutex);
4821709Smlf 
4831709Smlf 	/* check if any instance of driver is open */
4841709Smlf 	for (instance = 0; instance < max_instance; instance++) {
4851709Smlf 		dkp = ddi_get_soft_state(cmdk_state, instance);
4861709Smlf 		if (!dkp)
4871709Smlf 			continue;
4881709Smlf 		if (dkp->dk_flag & CMDK_OPEN)
4891709Smlf 			return (DDI_FAILURE);
4901709Smlf 	}
4911709Smlf 
4921709Smlf 	instance = ddi_get_instance(dip);
4931709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
4941709Smlf 		return (DDI_SUCCESS);
4951709Smlf 
4961709Smlf 	mutex_enter(&dkp->dk_mutex);
4971709Smlf 
4981709Smlf 	/*
4991709Smlf 	 * The cmdk_part_info call at the end of cmdkattach may have
5001709Smlf 	 * caused cmdk_reopen to do a TGDK_OPEN, make sure we close on
5011709Smlf 	 * detach for case when cmdkopen/cmdkclose never occurs.
5021709Smlf 	 */
5031709Smlf 	if (dkp->dk_flag & CMDK_TGDK_OPEN) {
5041709Smlf 		dkp->dk_flag &= ~CMDK_TGDK_OPEN;
5051709Smlf 		(void) dadk_close(DKTP_DATA);
5061709Smlf 	}
5071709Smlf 
5083525Sshidokht 	cmlb_detach(dkp->dk_cmlbhandle, 0);
5091709Smlf 	cmlb_free_handle(&dkp->dk_cmlbhandle);
5101709Smlf 	ddi_prop_remove_all(dip);
5111709Smlf 
5121709Smlf 	cmdk_destroy_obj(dip, dkp);	/* dadk/strategy linkage  */
5131709Smlf 	mutex_exit(&dkp->dk_mutex);
5141709Smlf 	mutex_destroy(&dkp->dk_mutex);
5151709Smlf 	rw_destroy(&dkp->dk_bbh_mutex);
5165295Srandyf 	mutex_destroy(&dkp->dk_pm_mutex);
5175295Srandyf 	cv_destroy(&dkp->dk_suspend_cv);
5181709Smlf 	ddi_soft_state_free(cmdk_state, instance);
5191709Smlf 
5201709Smlf 	return (DDI_SUCCESS);
5211709Smlf }
5221709Smlf 
5231709Smlf static int
5241709Smlf cmdkinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5251709Smlf {
5261709Smlf 	dev_t		dev = (dev_t)arg;
5271709Smlf 	int 		instance;
5281709Smlf 	struct	cmdk	*dkp;
5291709Smlf 
5301709Smlf #ifdef lint
5311709Smlf 	dip = dip;	/* no one ever uses this */
5321709Smlf #endif
5331709Smlf #ifdef CMDK_DEBUG
5341709Smlf 	if (cmdk_debug & DENT)
5351709Smlf 		PRF("cmdkinfo: call\n");
5361709Smlf #endif
5371709Smlf 	instance = CMDKUNIT(dev);
5381709Smlf 
5391709Smlf 	switch (infocmd) {
5401709Smlf 		case DDI_INFO_DEVT2DEVINFO:
5411709Smlf 			if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
5421709Smlf 				return (DDI_FAILURE);
5431709Smlf 			*result = (void *) dkp->dk_dip;
5441709Smlf 			break;
5451709Smlf 		case DDI_INFO_DEVT2INSTANCE:
5461709Smlf 			*result = (void *)(intptr_t)instance;
5471709Smlf 			break;
5481709Smlf 		default:
5491709Smlf 			return (DDI_FAILURE);
5501709Smlf 	}
5511709Smlf 	return (DDI_SUCCESS);
5521709Smlf }
5531709Smlf 
5545295Srandyf /*
5555295Srandyf  * Initialize the power management components
5565295Srandyf  */
5575295Srandyf static void
5585295Srandyf cmdk_setup_pm(dev_info_t *dip, struct cmdk *dkp)
5595295Srandyf {
5605295Srandyf 	char *pm_comp[] = { "NAME=cmdk", "0=off", "1=on", NULL };
5615295Srandyf 
5625295Srandyf 	/*
5635295Srandyf 	 * Since the cmdk device does not the 'reg' property,
5645295Srandyf 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5655295Srandyf 	 * The following code is to tell cpr that this device
5665295Srandyf 	 * DOES need to be suspended and resumed.
5675295Srandyf 	 */
5685295Srandyf 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
5695295Srandyf 	    "pm-hardware-state", "needs-suspend-resume");
5705295Srandyf 
5715295Srandyf 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
5725295Srandyf 	    "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) {
5735295Srandyf 		if (pm_raise_power(dip, 0, CMDK_SPINDLE_ON) == DDI_SUCCESS) {
5745295Srandyf 			mutex_enter(&dkp->dk_pm_mutex);
5755295Srandyf 			dkp->dk_pm_level = CMDK_SPINDLE_ON;
5765295Srandyf 			dkp->dk_pm_is_enabled = 1;
5775295Srandyf 			mutex_exit(&dkp->dk_pm_mutex);
5785295Srandyf 		} else {
5795295Srandyf 			mutex_enter(&dkp->dk_pm_mutex);
5805295Srandyf 			dkp->dk_pm_level = CMDK_SPINDLE_OFF;
5815295Srandyf 			dkp->dk_pm_is_enabled = 0;
5825295Srandyf 			mutex_exit(&dkp->dk_pm_mutex);
5835295Srandyf 		}
5845295Srandyf 	} else {
5855295Srandyf 		mutex_enter(&dkp->dk_pm_mutex);
5865295Srandyf 		dkp->dk_pm_level = CMDK_SPINDLE_UNINIT;
5875295Srandyf 		dkp->dk_pm_is_enabled = 0;
5885295Srandyf 		mutex_exit(&dkp->dk_pm_mutex);
5895295Srandyf 	}
5905295Srandyf }
5915295Srandyf 
5925295Srandyf /*
5935295Srandyf  * suspend routine, it will be run when get the command
5945295Srandyf  * DDI_SUSPEND at detach(9E) from system power management
5955295Srandyf  */
5965295Srandyf static int
5975295Srandyf cmdksuspend(dev_info_t *dip)
5985295Srandyf {
5995295Srandyf 	struct cmdk	*dkp;
6005295Srandyf 	int		instance;
6015295Srandyf 	clock_t		count = 0;
6025295Srandyf 
6035295Srandyf 	instance = ddi_get_instance(dip);
6045295Srandyf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
6055295Srandyf 		return (DDI_FAILURE);
6065295Srandyf 	mutex_enter(&dkp->dk_mutex);
6075295Srandyf 	if (dkp->dk_flag & CMDK_SUSPEND) {
6085295Srandyf 		mutex_exit(&dkp->dk_mutex);
6095295Srandyf 		return (DDI_SUCCESS);
6105295Srandyf 	}
6115295Srandyf 	dkp->dk_flag |= CMDK_SUSPEND;
6125295Srandyf 
6135295Srandyf 	/* need to wait a while */
6145295Srandyf 	while (dadk_getcmds(DKTP_DATA) != 0) {
6155295Srandyf 		delay(drv_usectohz(1000000));
6165295Srandyf 		if (count > 60) {
6175295Srandyf 			dkp->dk_flag &= ~CMDK_SUSPEND;
6185295Srandyf 			cv_broadcast(&dkp->dk_suspend_cv);
6195295Srandyf 			mutex_exit(&dkp->dk_mutex);
6205295Srandyf 			return (DDI_FAILURE);
6215295Srandyf 		}
6225295Srandyf 		count++;
6235295Srandyf 	}
6245295Srandyf 	mutex_exit(&dkp->dk_mutex);
6255295Srandyf 	return (DDI_SUCCESS);
6265295Srandyf }
6275295Srandyf 
6285295Srandyf /*
6295295Srandyf  * resume routine, it will be run when get the command
6305295Srandyf  * DDI_RESUME at attach(9E) from system power management
6315295Srandyf  */
6325295Srandyf static int
6335295Srandyf cmdkresume(dev_info_t *dip)
6345295Srandyf {
6355295Srandyf 	struct cmdk	*dkp;
6365295Srandyf 	int		instance;
6375295Srandyf 
6385295Srandyf 	instance = ddi_get_instance(dip);
6395295Srandyf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
6405295Srandyf 		return (DDI_FAILURE);
6415295Srandyf 	mutex_enter(&dkp->dk_mutex);
6425295Srandyf 	if (!(dkp->dk_flag & CMDK_SUSPEND)) {
6435295Srandyf 		mutex_exit(&dkp->dk_mutex);
6445295Srandyf 		return (DDI_FAILURE);
6455295Srandyf 	}
6465295Srandyf 	dkp->dk_pm_level = CMDK_SPINDLE_ON;
6475295Srandyf 	dkp->dk_flag &= ~CMDK_SUSPEND;
6485295Srandyf 	cv_broadcast(&dkp->dk_suspend_cv);
6495295Srandyf 	mutex_exit(&dkp->dk_mutex);
6505295Srandyf 	return (DDI_SUCCESS);
6515295Srandyf 
6525295Srandyf }
6535295Srandyf 
6545295Srandyf /*
6555295Srandyf  * power management entry point, it was used to
6565295Srandyf  * change power management component.
6575295Srandyf  * Actually, the real hard drive suspend/resume
6585295Srandyf  * was handled in ata, so this function is not
6595295Srandyf  * doing any real work other than verifying that
6605295Srandyf  * the disk is idle.
6615295Srandyf  */
6625295Srandyf static int
6635295Srandyf cmdkpower(dev_info_t *dip, int component, int level)
6645295Srandyf {
6655295Srandyf 	struct cmdk	*dkp;
6665295Srandyf 	int		instance;
6675295Srandyf 
6685295Srandyf 	instance = ddi_get_instance(dip);
6695295Srandyf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) ||
6705295Srandyf 	    component != 0 || level > CMDK_SPINDLE_ON ||
6715295Srandyf 	    level < CMDK_SPINDLE_OFF) {
6725295Srandyf 		return (DDI_FAILURE);
6735295Srandyf 	}
6745295Srandyf 
6755295Srandyf 	mutex_enter(&dkp->dk_pm_mutex);
6765295Srandyf 	if (dkp->dk_pm_is_enabled && dkp->dk_pm_level == level) {
6775295Srandyf 		mutex_exit(&dkp->dk_pm_mutex);
6785295Srandyf 		return (DDI_SUCCESS);
6795295Srandyf 	}
6805295Srandyf 	mutex_exit(&dkp->dk_pm_mutex);
6815295Srandyf 
6825295Srandyf 	if ((level == CMDK_SPINDLE_OFF) &&
6835295Srandyf 	    (dadk_getcmds(DKTP_DATA) != 0)) {
6845295Srandyf 		return (DDI_FAILURE);
6855295Srandyf 	}
6865295Srandyf 
6875295Srandyf 	mutex_enter(&dkp->dk_pm_mutex);
6885295Srandyf 	dkp->dk_pm_level = level;
6895295Srandyf 	mutex_exit(&dkp->dk_pm_mutex);
6905295Srandyf 	return (DDI_SUCCESS);
6915295Srandyf }
6925295Srandyf 
6931709Smlf static int
6941709Smlf cmdk_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
6951709Smlf     char *name, caddr_t valuep, int *lengthp)
6961709Smlf {
6971709Smlf 	struct	cmdk	*dkp;
6981709Smlf 
6991709Smlf #ifdef CMDK_DEBUG
7001709Smlf 	if (cmdk_debug & DENT)
7011709Smlf 		PRF("cmdk_prop_op: call\n");
7021709Smlf #endif
7031709Smlf 
7041709Smlf 	dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip));
7057224Scth 	if (dkp == NULL)
7067224Scth 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
7077224Scth 		    name, valuep, lengthp));
7081709Smlf 
7097224Scth 	return (cmlb_prop_op(dkp->dk_cmlbhandle,
7107224Scth 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
7117224Scth 	    CMDKPART(dev), NULL));
7121709Smlf }
7131709Smlf 
7141709Smlf /*
7151709Smlf  * dump routine
7161709Smlf  */
7171709Smlf static int
7181709Smlf cmdkdump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
7191709Smlf {
7201709Smlf 	int 		instance;
7211709Smlf 	struct	cmdk	*dkp;
7221709Smlf 	diskaddr_t	p_lblksrt;
7231709Smlf 	diskaddr_t	p_lblkcnt;
7241709Smlf 	struct	buf	local;
7251709Smlf 	struct	buf	*bp;
7261709Smlf 
7271709Smlf #ifdef CMDK_DEBUG
7281709Smlf 	if (cmdk_debug & DENT)
7291709Smlf 		PRF("cmdkdump: call\n");
7301709Smlf #endif
7311709Smlf 	instance = CMDKUNIT(dev);
7321709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) || (blkno < 0))
7331709Smlf 		return (ENXIO);
7341709Smlf 
7351709Smlf 	if (cmlb_partinfo(
7361709Smlf 	    dkp->dk_cmlbhandle,
7371709Smlf 	    CMDKPART(dev),
7381709Smlf 	    &p_lblkcnt,
7391709Smlf 	    &p_lblksrt,
7401709Smlf 	    NULL,
7413525Sshidokht 	    NULL,
7423525Sshidokht 	    0)) {
7431709Smlf 		return (ENXIO);
7441709Smlf 	}
7451709Smlf 
7461709Smlf 	if ((blkno+nblk) > p_lblkcnt)
7471709Smlf 		return (EINVAL);
7481709Smlf 
7491709Smlf 	cmdk_indump = 1;	/* Tell disk targets we are panic dumpping */
7501709Smlf 
7511709Smlf 	bp = &local;
7521709Smlf 	bzero(bp, sizeof (*bp));
7531709Smlf 	bp->b_flags = B_BUSY;
7541709Smlf 	bp->b_un.b_addr = addr;
7551709Smlf 	bp->b_bcount = nblk << SCTRSHFT;
7561709Smlf 	SET_BP_SEC(bp, ((ulong_t)(p_lblksrt + blkno)));
7571709Smlf 
7581709Smlf 	(void) dadk_dump(DKTP_DATA, bp);
7591709Smlf 	return (bp->b_error);
7601709Smlf }
7611709Smlf 
7621709Smlf /*
7631709Smlf  * Copy in the dadkio_rwcmd according to the user's data model.  If needed,
7641709Smlf  * convert it for our internal use.
7651709Smlf  */
7661709Smlf static int
7671709Smlf rwcmd_copyin(struct dadkio_rwcmd *rwcmdp, caddr_t inaddr, int flag)
7681709Smlf {
7691709Smlf 	switch (ddi_model_convert_from(flag)) {
7701709Smlf 		case DDI_MODEL_ILP32: {
7711709Smlf 			struct dadkio_rwcmd32 cmd32;
7721709Smlf 
7731709Smlf 			if (ddi_copyin(inaddr, &cmd32,
7741709Smlf 			    sizeof (struct dadkio_rwcmd32), flag)) {
7751709Smlf 				return (EFAULT);
7761709Smlf 			}
7771709Smlf 
7781709Smlf 			rwcmdp->cmd = cmd32.cmd;
7791709Smlf 			rwcmdp->flags = cmd32.flags;
7807563SPrasad.Singamsetty@Sun.COM 			rwcmdp->blkaddr = (blkaddr_t)cmd32.blkaddr;
7811709Smlf 			rwcmdp->buflen = cmd32.buflen;
7821709Smlf 			rwcmdp->bufaddr = (caddr_t)(intptr_t)cmd32.bufaddr;
7831709Smlf 			/*
7841709Smlf 			 * Note: we do not convert the 'status' field,
7851709Smlf 			 * as it should not contain valid data at this
7861709Smlf 			 * point.
7871709Smlf 			 */
7881709Smlf 			bzero(&rwcmdp->status, sizeof (rwcmdp->status));
7891709Smlf 			break;
7901709Smlf 		}
7911709Smlf 		case DDI_MODEL_NONE: {
7921709Smlf 			if (ddi_copyin(inaddr, rwcmdp,
7931709Smlf 			    sizeof (struct dadkio_rwcmd), flag)) {
7941709Smlf 				return (EFAULT);
7951709Smlf 			}
7961709Smlf 		}
7971709Smlf 	}
7981709Smlf 	return (0);
7991709Smlf }
8001709Smlf 
8011709Smlf /*
8021709Smlf  * If necessary, convert the internal rwcmdp and status to the appropriate
8031709Smlf  * data model and copy it out to the user.
8041709Smlf  */
8051709Smlf static int
8061709Smlf rwcmd_copyout(struct dadkio_rwcmd *rwcmdp, caddr_t outaddr, int flag)
8071709Smlf {
8081709Smlf 	switch (ddi_model_convert_from(flag)) {
8091709Smlf 		case DDI_MODEL_ILP32: {
8101709Smlf 			struct dadkio_rwcmd32 cmd32;
8111709Smlf 
8121709Smlf 			cmd32.cmd = rwcmdp->cmd;
8131709Smlf 			cmd32.flags = rwcmdp->flags;
8141709Smlf 			cmd32.blkaddr = rwcmdp->blkaddr;
8151709Smlf 			cmd32.buflen = rwcmdp->buflen;
8161709Smlf 			ASSERT64(((uintptr_t)rwcmdp->bufaddr >> 32) == 0);
8171709Smlf 			cmd32.bufaddr = (caddr32_t)(uintptr_t)rwcmdp->bufaddr;
8181709Smlf 
8191709Smlf 			cmd32.status.status = rwcmdp->status.status;
8201709Smlf 			cmd32.status.resid = rwcmdp->status.resid;
8211709Smlf 			cmd32.status.failed_blk_is_valid =
8221709Smlf 			    rwcmdp->status.failed_blk_is_valid;
8231709Smlf 			cmd32.status.failed_blk = rwcmdp->status.failed_blk;
8241709Smlf 			cmd32.status.fru_code_is_valid =
8251709Smlf 			    rwcmdp->status.fru_code_is_valid;
8261709Smlf 			cmd32.status.fru_code = rwcmdp->status.fru_code;
8271709Smlf 
8281709Smlf 			bcopy(rwcmdp->status.add_error_info,
8291709Smlf 			    cmd32.status.add_error_info, DADKIO_ERROR_INFO_LEN);
8301709Smlf 
8311709Smlf 			if (ddi_copyout(&cmd32, outaddr,
8321709Smlf 			    sizeof (struct dadkio_rwcmd32), flag))
8331709Smlf 				return (EFAULT);
8341709Smlf 			break;
8351709Smlf 		}
8361709Smlf 		case DDI_MODEL_NONE: {
8371709Smlf 			if (ddi_copyout(rwcmdp, outaddr,
8381709Smlf 			    sizeof (struct dadkio_rwcmd), flag))
8391709Smlf 			return (EFAULT);
8401709Smlf 		}
8411709Smlf 	}
8421709Smlf 	return (0);
8431709Smlf }
8441709Smlf 
8451709Smlf /*
8461709Smlf  * ioctl routine
8471709Smlf  */
8481709Smlf static int
8491709Smlf cmdkioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, int *rvalp)
8501709Smlf {
8511709Smlf 	int 		instance;
8521709Smlf 	struct scsi_device *devp;
8531709Smlf 	struct cmdk	*dkp;
8541709Smlf 	char 		data[NBPSCTR];
8551709Smlf 
8561709Smlf 	instance = CMDKUNIT(dev);
8571709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
8581709Smlf 		return (ENXIO);
8591709Smlf 
8605295Srandyf 	mutex_enter(&dkp->dk_mutex);
8615295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
8625295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
8635295Srandyf 	}
8645295Srandyf 	mutex_exit(&dkp->dk_mutex);
8655295Srandyf 
8661709Smlf 	bzero(data, sizeof (data));
8671709Smlf 
8681709Smlf 	switch (cmd) {
8691709Smlf 
8701709Smlf 	case DKIOCGMEDIAINFO: {
8711709Smlf 		struct dk_minfo	media_info;
8721709Smlf 		struct  tgdk_geom phyg;
8731709Smlf 
8741709Smlf 		/* dadk_getphygeom always returns success */
8751709Smlf 		(void) dadk_getphygeom(DKTP_DATA, &phyg);
8761709Smlf 
8771709Smlf 		media_info.dki_lbsize = phyg.g_secsiz;
8781709Smlf 		media_info.dki_capacity = phyg.g_cap;
8791709Smlf 		media_info.dki_media_type = DK_FIXED_DISK;
8801709Smlf 
8811709Smlf 		if (ddi_copyout(&media_info, (void *)arg,
8821709Smlf 		    sizeof (struct dk_minfo), flag)) {
8831709Smlf 			return (EFAULT);
8841709Smlf 		} else {
8851709Smlf 			return (0);
8861709Smlf 		}
8871709Smlf 	}
8881709Smlf 
8891709Smlf 	case DKIOCINFO: {
8901709Smlf 		struct dk_cinfo *info = (struct dk_cinfo *)data;
8911709Smlf 
8921709Smlf 		/* controller information */
8931709Smlf 		info->dki_ctype = (DKTP_EXT->tg_ctype);
8941709Smlf 		info->dki_cnum = ddi_get_instance(ddi_get_parent(dkp->dk_dip));
8951709Smlf 		(void) strcpy(info->dki_cname,
8961709Smlf 		    ddi_get_name(ddi_get_parent(dkp->dk_dip)));
8971709Smlf 
8981709Smlf 		/* Unit Information */
8991709Smlf 		info->dki_unit = ddi_get_instance(dkp->dk_dip);
9001709Smlf 		devp = ddi_get_driver_private(dkp->dk_dip);
9011709Smlf 		info->dki_slave = (CMDEV_TARG(devp)<<3) | CMDEV_LUN(devp);
9021709Smlf 		(void) strcpy(info->dki_dname, ddi_driver_name(dkp->dk_dip));
9031709Smlf 		info->dki_flags = DKI_FMTVOL;
9041709Smlf 		info->dki_partition = CMDKPART(dev);
9051709Smlf 
9061709Smlf 		info->dki_maxtransfer = maxphys / DEV_BSIZE;
9071709Smlf 		info->dki_addr = 1;
9081709Smlf 		info->dki_space = 0;
9091709Smlf 		info->dki_prio = 0;
9101709Smlf 		info->dki_vec = 0;
9111709Smlf 
9121709Smlf 		if (ddi_copyout(data, (void *)arg, sizeof (*info), flag))
9131709Smlf 			return (EFAULT);
9141709Smlf 		else
9151709Smlf 			return (0);
9161709Smlf 	}
9171709Smlf 
9181709Smlf 	case DKIOCSTATE: {
9191709Smlf 		int	state;
9201709Smlf 		int	rval;
9211709Smlf 		diskaddr_t	p_lblksrt;
9221709Smlf 		diskaddr_t	p_lblkcnt;
9231709Smlf 
9241709Smlf 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag))
9251709Smlf 			return (EFAULT);
9261709Smlf 
9271709Smlf 		/* dadk_check_media blocks until state changes */
9281709Smlf 		if (rval = dadk_check_media(DKTP_DATA, &state))
9291709Smlf 			return (rval);
9301709Smlf 
9311709Smlf 		if (state == DKIO_INSERTED) {
9321709Smlf 
9333525Sshidokht 			if (cmlb_validate(dkp->dk_cmlbhandle, 0, 0) != 0)
9341709Smlf 				return (ENXIO);
9351709Smlf 
9361709Smlf 			if (cmlb_partinfo(dkp->dk_cmlbhandle, CMDKPART(dev),
9373525Sshidokht 			    &p_lblkcnt, &p_lblksrt, NULL, NULL, 0))
9381709Smlf 				return (ENXIO);
9391709Smlf 
9401709Smlf 			if (p_lblkcnt <= 0)
9411709Smlf 				return (ENXIO);
9421709Smlf 		}
9431709Smlf 
9441709Smlf 		if (ddi_copyout(&state, (caddr_t)arg, sizeof (int), flag))
9451709Smlf 			return (EFAULT);
9461709Smlf 
9471709Smlf 		return (0);
9481709Smlf 	}
9491709Smlf 
9501709Smlf 	/*
9511709Smlf 	 * is media removable?
9521709Smlf 	 */
9531709Smlf 	case DKIOCREMOVABLE: {
9541709Smlf 		int i;
9551709Smlf 
9561709Smlf 		i = (DKTP_EXT->tg_rmb) ? 1 : 0;
9571709Smlf 
9581709Smlf 		if (ddi_copyout(&i, (caddr_t)arg, sizeof (int), flag))
9591709Smlf 			return (EFAULT);
9601709Smlf 
9611709Smlf 		return (0);
9621709Smlf 	}
9631709Smlf 
9641709Smlf 	case DKIOCADDBAD:
9651709Smlf 		/*
9661709Smlf 		 * This is not an update mechanism to add bad blocks
9671709Smlf 		 * to the bad block structures stored on disk.
9681709Smlf 		 *
9691709Smlf 		 * addbadsec(1M) will update the bad block data on disk
9701709Smlf 		 * and use this ioctl to force the driver to re-initialize
9711709Smlf 		 * the list of bad blocks in the driver.
9721709Smlf 		 */
9731709Smlf 
9741709Smlf 		/* start BBH */
9751709Smlf 		cmdk_bbh_reopen(dkp);
9761709Smlf 		return (0);
9771709Smlf 
9781709Smlf 	case DKIOCG_PHYGEOM:
9791709Smlf 	case DKIOCG_VIRTGEOM:
9801709Smlf 	case DKIOCGGEOM:
9811709Smlf 	case DKIOCSGEOM:
9821709Smlf 	case DKIOCGAPART:
9831709Smlf 	case DKIOCSAPART:
9841709Smlf 	case DKIOCGVTOC:
9851709Smlf 	case DKIOCSVTOC:
9861709Smlf 	case DKIOCPARTINFO:
9877563SPrasad.Singamsetty@Sun.COM 	case DKIOCGEXTVTOC:
9887563SPrasad.Singamsetty@Sun.COM 	case DKIOCSEXTVTOC:
9897563SPrasad.Singamsetty@Sun.COM 	case DKIOCEXTPARTINFO:
9901709Smlf 	case DKIOCGMBOOT:
9911709Smlf 	case DKIOCSMBOOT:
9921709Smlf 	case DKIOCGETEFI:
9931709Smlf 	case DKIOCSETEFI:
9941709Smlf 	case DKIOCPARTITION:
9951709Smlf 	{
9961709Smlf 		int rc;
9971709Smlf 
9983525Sshidokht 		rc = cmlb_ioctl(dkp->dk_cmlbhandle, dev, cmd, arg, flag,
9993525Sshidokht 		    credp, rvalp, 0);
10001709Smlf 		if (cmd == DKIOCSVTOC)
10011709Smlf 			cmdk_devid_setup(dkp);
10021709Smlf 		return (rc);
10031709Smlf 	}
10041709Smlf 
10051709Smlf 	case DIOCTL_RWCMD: {
10061709Smlf 		struct	dadkio_rwcmd *rwcmdp;
10071709Smlf 		int	status;
10081709Smlf 
10091709Smlf 		rwcmdp = kmem_alloc(sizeof (struct dadkio_rwcmd), KM_SLEEP);
10101709Smlf 
10111709Smlf 		status = rwcmd_copyin(rwcmdp, (caddr_t)arg, flag);
10121709Smlf 
10131709Smlf 		if (status == 0) {
10141709Smlf 			bzero(&(rwcmdp->status), sizeof (struct dadkio_status));
10151709Smlf 			status = dadk_ioctl(DKTP_DATA,
10161709Smlf 			    dev,
10171709Smlf 			    cmd,
10181709Smlf 			    (uintptr_t)rwcmdp,
10191709Smlf 			    flag,
10201709Smlf 			    credp,
10211709Smlf 			    rvalp);
10221709Smlf 		}
10231709Smlf 		if (status == 0)
10241709Smlf 			status = rwcmd_copyout(rwcmdp, (caddr_t)arg, flag);
10251709Smlf 
10261709Smlf 		kmem_free(rwcmdp, sizeof (struct dadkio_rwcmd));
10271709Smlf 		return (status);
10281709Smlf 	}
10291709Smlf 
10301709Smlf 	default:
10311709Smlf 		return (dadk_ioctl(DKTP_DATA,
10321709Smlf 		    dev,
10331709Smlf 		    cmd,
10341709Smlf 		    arg,
10351709Smlf 		    flag,
10361709Smlf 		    credp,
10371709Smlf 		    rvalp));
10381709Smlf 	}
10391709Smlf }
10401709Smlf 
10411709Smlf /*ARGSUSED1*/
10421709Smlf static int
10431709Smlf cmdkclose(dev_t dev, int flag, int otyp, cred_t *credp)
10441709Smlf {
10451709Smlf 	int		part;
10461709Smlf 	ulong_t		partbit;
10471709Smlf 	int 		instance;
10481709Smlf 	struct cmdk	*dkp;
10491709Smlf 	int		lastclose = 1;
10501709Smlf 	int		i;
10511709Smlf 
10521709Smlf 	instance = CMDKUNIT(dev);
10531709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)) ||
10541709Smlf 	    (otyp >= OTYPCNT))
10551709Smlf 		return (ENXIO);
10561709Smlf 
10571709Smlf 	mutex_enter(&dkp->dk_mutex);
10581709Smlf 
10591709Smlf 	/* check if device has been opened */
10606318Sedp 	ASSERT(cmdk_isopen(dkp, dev));
10611709Smlf 	if (!(dkp->dk_flag & CMDK_OPEN)) {
10621709Smlf 		mutex_exit(&dkp->dk_mutex);
10631709Smlf 		return (ENXIO);
10641709Smlf 	}
10651709Smlf 
10665295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
10675295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
10685295Srandyf 	}
10695295Srandyf 
10701709Smlf 	part = CMDKPART(dev);
10711709Smlf 	partbit = 1 << part;
10721709Smlf 
10731709Smlf 	/* account for close */
10741709Smlf 	if (otyp == OTYP_LYR) {
10756318Sedp 		ASSERT(dkp->dk_open_lyr[part] > 0);
10761709Smlf 		if (dkp->dk_open_lyr[part])
10771709Smlf 			dkp->dk_open_lyr[part]--;
10786318Sedp 	} else {
10796318Sedp 		ASSERT((dkp->dk_open_reg[otyp] & partbit) != 0);
10801709Smlf 		dkp->dk_open_reg[otyp] &= ~partbit;
10816318Sedp 	}
10821709Smlf 	dkp->dk_open_exl &= ~partbit;
10831709Smlf 
10841709Smlf 	for (i = 0; i < CMDK_MAXPART; i++)
10851709Smlf 		if (dkp->dk_open_lyr[i] != 0) {
10861709Smlf 			lastclose = 0;
10871709Smlf 			break;
10881709Smlf 		}
10891709Smlf 
10901709Smlf 	if (lastclose)
10911709Smlf 		for (i = 0; i < OTYPCNT; i++)
10921709Smlf 			if (dkp->dk_open_reg[i] != 0) {
10931709Smlf 				lastclose = 0;
10941709Smlf 				break;
10951709Smlf 			}
10961709Smlf 
10971709Smlf 	mutex_exit(&dkp->dk_mutex);
10981709Smlf 
10991709Smlf 	if (lastclose)
11003525Sshidokht 		cmlb_invalidate(dkp->dk_cmlbhandle, 0);
11011709Smlf 
11021709Smlf 	return (DDI_SUCCESS);
11031709Smlf }
11041709Smlf 
11051709Smlf /*ARGSUSED3*/
11061709Smlf static int
11071709Smlf cmdkopen(dev_t *dev_p, int flag, int otyp, cred_t *credp)
11081709Smlf {
11091709Smlf 	dev_t		dev = *dev_p;
11101709Smlf 	int 		part;
11111709Smlf 	ulong_t		partbit;
11121709Smlf 	int 		instance;
11131709Smlf 	struct	cmdk	*dkp;
11141709Smlf 	diskaddr_t	p_lblksrt;
11151709Smlf 	diskaddr_t	p_lblkcnt;
11161709Smlf 	int		i;
11171709Smlf 	int		nodelay;
11181709Smlf 
11191709Smlf 	instance = CMDKUNIT(dev);
11201709Smlf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
11211709Smlf 		return (ENXIO);
11221709Smlf 
11231709Smlf 	if (otyp >= OTYPCNT)
11241709Smlf 		return (EINVAL);
11251709Smlf 
11265295Srandyf 	mutex_enter(&dkp->dk_mutex);
11275295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
11285295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
11295295Srandyf 	}
11305295Srandyf 	mutex_exit(&dkp->dk_mutex);
11315295Srandyf 
11321709Smlf 	part = CMDKPART(dev);
11331709Smlf 	partbit = 1 << part;
11341709Smlf 	nodelay = (flag & (FNDELAY | FNONBLOCK));
11351709Smlf 
11361709Smlf 	mutex_enter(&dkp->dk_mutex);
11371709Smlf 
11383525Sshidokht 	if (cmlb_validate(dkp->dk_cmlbhandle, 0, 0) != 0) {
11391709Smlf 
11401709Smlf 		/* fail if not doing non block open */
11411709Smlf 		if (!nodelay) {
11421709Smlf 			mutex_exit(&dkp->dk_mutex);
11431709Smlf 			return (ENXIO);
11441709Smlf 		}
11451709Smlf 	} else if (cmlb_partinfo(dkp->dk_cmlbhandle, part, &p_lblkcnt,
11463525Sshidokht 	    &p_lblksrt, NULL, NULL, 0) == 0) {
11471709Smlf 
11481709Smlf 		if (p_lblkcnt <= 0 && (!nodelay || otyp != OTYP_CHR)) {
11491709Smlf 			mutex_exit(&dkp->dk_mutex);
11501709Smlf 			return (ENXIO);
11511709Smlf 		}
11521709Smlf 	} else {
11531709Smlf 		/* fail if not doing non block open */
11541709Smlf 		if (!nodelay) {
11551709Smlf 			mutex_exit(&dkp->dk_mutex);
11561709Smlf 			return (ENXIO);
11571709Smlf 		}
11581709Smlf 	}
11591709Smlf 
11601709Smlf 	if ((DKTP_EXT->tg_rdonly) && (flag & FWRITE)) {
11611709Smlf 		mutex_exit(&dkp->dk_mutex);
11621709Smlf 		return (EROFS);
11631709Smlf 	}
11641709Smlf 
11651709Smlf 	/* check for part already opend exclusively */
11661709Smlf 	if (dkp->dk_open_exl & partbit)
11671709Smlf 		goto excl_open_fail;
11681709Smlf 
11691709Smlf 	/* check if we can establish exclusive open */
11701709Smlf 	if (flag & FEXCL) {
11711709Smlf 		if (dkp->dk_open_lyr[part])
11721709Smlf 			goto excl_open_fail;
11731709Smlf 		for (i = 0; i < OTYPCNT; i++) {
11741709Smlf 			if (dkp->dk_open_reg[i] & partbit)
11751709Smlf 				goto excl_open_fail;
11761709Smlf 		}
11771709Smlf 	}
11781709Smlf 
11791709Smlf 	/* open will succeed, account for open */
11801709Smlf 	dkp->dk_flag |= CMDK_OPEN;
11811709Smlf 	if (otyp == OTYP_LYR)
11821709Smlf 		dkp->dk_open_lyr[part]++;
11831709Smlf 	else
11841709Smlf 		dkp->dk_open_reg[otyp] |= partbit;
11851709Smlf 	if (flag & FEXCL)
11861709Smlf 		dkp->dk_open_exl |= partbit;
11871709Smlf 
11881709Smlf 	mutex_exit(&dkp->dk_mutex);
11891709Smlf 	return (DDI_SUCCESS);
11901709Smlf 
11911709Smlf excl_open_fail:
11921709Smlf 	mutex_exit(&dkp->dk_mutex);
11931709Smlf 	return (EBUSY);
11941709Smlf }
11951709Smlf 
11961709Smlf /*
11971709Smlf  * read routine
11981709Smlf  */
11991709Smlf /*ARGSUSED2*/
12001709Smlf static int
12011709Smlf cmdkread(dev_t dev, struct uio *uio, cred_t *credp)
12021709Smlf {
12031709Smlf 	return (cmdkrw(dev, uio, B_READ));
12041709Smlf }
12051709Smlf 
12061709Smlf /*
12071709Smlf  * async read routine
12081709Smlf  */
12091709Smlf /*ARGSUSED2*/
12101709Smlf static int
12111709Smlf cmdkaread(dev_t dev, struct aio_req *aio, cred_t *credp)
12121709Smlf {
12131709Smlf 	return (cmdkarw(dev, aio, B_READ));
12141709Smlf }
12151709Smlf 
12161709Smlf /*
12171709Smlf  * write routine
12181709Smlf  */
12191709Smlf /*ARGSUSED2*/
12201709Smlf static int
12211709Smlf cmdkwrite(dev_t dev, struct uio *uio, cred_t *credp)
12221709Smlf {
12231709Smlf 	return (cmdkrw(dev, uio, B_WRITE));
12241709Smlf }
12251709Smlf 
12261709Smlf /*
12271709Smlf  * async write routine
12281709Smlf  */
12291709Smlf /*ARGSUSED2*/
12301709Smlf static int
12311709Smlf cmdkawrite(dev_t dev, struct aio_req *aio, cred_t *credp)
12321709Smlf {
12331709Smlf 	return (cmdkarw(dev, aio, B_WRITE));
12341709Smlf }
12351709Smlf 
12361709Smlf static void
12371709Smlf cmdkmin(struct buf *bp)
12381709Smlf {
12391709Smlf 	if (bp->b_bcount > DK_MAXRECSIZE)
12401709Smlf 		bp->b_bcount = DK_MAXRECSIZE;
12411709Smlf }
12421709Smlf 
12431709Smlf static int
12441709Smlf cmdkrw(dev_t dev, struct uio *uio, int flag)
12451709Smlf {
12465295Srandyf 	int 		instance;
12475295Srandyf 	struct	cmdk	*dkp;
12485295Srandyf 
12495295Srandyf 	instance = CMDKUNIT(dev);
12505295Srandyf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
12515295Srandyf 		return (ENXIO);
12525295Srandyf 
12535295Srandyf 	mutex_enter(&dkp->dk_mutex);
12545295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
12555295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
12565295Srandyf 	}
12575295Srandyf 	mutex_exit(&dkp->dk_mutex);
12585295Srandyf 
12591709Smlf 	return (physio(cmdkstrategy, (struct buf *)0, dev, flag, cmdkmin, uio));
12601709Smlf }
12611709Smlf 
12621709Smlf static int
12631709Smlf cmdkarw(dev_t dev, struct aio_req *aio, int flag)
12641709Smlf {
12655295Srandyf 	int 		instance;
12665295Srandyf 	struct	cmdk	*dkp;
12675295Srandyf 
12685295Srandyf 	instance = CMDKUNIT(dev);
12695295Srandyf 	if (!(dkp = ddi_get_soft_state(cmdk_state, instance)))
12705295Srandyf 		return (ENXIO);
12715295Srandyf 
12725295Srandyf 	mutex_enter(&dkp->dk_mutex);
12735295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
12745295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
12755295Srandyf 	}
12765295Srandyf 	mutex_exit(&dkp->dk_mutex);
12775295Srandyf 
12781709Smlf 	return (aphysio(cmdkstrategy, anocancel, dev, flag, cmdkmin, aio));
12791709Smlf }
12801709Smlf 
12811709Smlf /*
12821709Smlf  * strategy routine
12831709Smlf  */
12841709Smlf static int
12851709Smlf cmdkstrategy(struct buf *bp)
12861709Smlf {
12871709Smlf 	int 		instance;
12881709Smlf 	struct	cmdk 	*dkp;
12891709Smlf 	long		d_cnt;
12901709Smlf 	diskaddr_t	p_lblksrt;
12911709Smlf 	diskaddr_t	p_lblkcnt;
12921709Smlf 
12931709Smlf 	instance = CMDKUNIT(bp->b_edev);
12941709Smlf 	if (cmdk_indump || !(dkp = ddi_get_soft_state(cmdk_state, instance)) ||
12951709Smlf 	    (dkblock(bp) < 0)) {
12961709Smlf 		bp->b_resid = bp->b_bcount;
12971709Smlf 		SETBPERR(bp, ENXIO);
12981709Smlf 		biodone(bp);
12991709Smlf 		return (0);
13001709Smlf 	}
13011709Smlf 
13025295Srandyf 	mutex_enter(&dkp->dk_mutex);
13036318Sedp 	ASSERT(cmdk_isopen(dkp, bp->b_edev));
13045295Srandyf 	while (dkp->dk_flag & CMDK_SUSPEND) {
13055295Srandyf 		cv_wait(&dkp->dk_suspend_cv, &dkp->dk_mutex);
13065295Srandyf 	}
13075295Srandyf 	mutex_exit(&dkp->dk_mutex);
13085295Srandyf 
13091709Smlf 	bp->b_flags &= ~(B_DONE|B_ERROR);
13101709Smlf 	bp->b_resid = 0;
13111709Smlf 	bp->av_back = NULL;
13121709Smlf 
13131709Smlf 	/*
13141709Smlf 	 * only re-read the vtoc if necessary (force == FALSE)
13151709Smlf 	 */
13163525Sshidokht 	if (cmlb_partinfo(dkp->dk_cmlbhandle, CMDKPART(bp->b_edev),
13173525Sshidokht 	    &p_lblkcnt, &p_lblksrt, NULL, NULL, 0)) {
13181709Smlf 		SETBPERR(bp, ENXIO);
13191709Smlf 	}
13201709Smlf 
13211709Smlf 	if ((bp->b_bcount & (NBPSCTR-1)) || (dkblock(bp) > p_lblkcnt))
13221709Smlf 		SETBPERR(bp, ENXIO);
13231709Smlf 
13241709Smlf 	if ((bp->b_flags & B_ERROR) || (dkblock(bp) == p_lblkcnt)) {
13251709Smlf 		bp->b_resid = bp->b_bcount;
13261709Smlf 		biodone(bp);
13271709Smlf 		return (0);
13281709Smlf 	}
13291709Smlf 
13301709Smlf 	d_cnt = bp->b_bcount >> SCTRSHFT;
13311709Smlf 	if ((dkblock(bp) + d_cnt) > p_lblkcnt) {
13321709Smlf 		bp->b_resid = ((dkblock(bp) + d_cnt) - p_lblkcnt) << SCTRSHFT;
13331709Smlf 		bp->b_bcount -= bp->b_resid;
13341709Smlf 	}
13351709Smlf 
13361709Smlf 	SET_BP_SEC(bp, ((ulong_t)(p_lblksrt + dkblock(bp))));
13371709Smlf 	if (dadk_strategy(DKTP_DATA, bp) != DDI_SUCCESS) {
13381709Smlf 		bp->b_resid += bp->b_bcount;
13391709Smlf 		biodone(bp);
13401709Smlf 	}
13411709Smlf 	return (0);
13421709Smlf }
13431709Smlf 
13441709Smlf static int
13451709Smlf cmdk_create_obj(dev_info_t *dip, struct cmdk *dkp)
13461709Smlf {
13471709Smlf 	struct scsi_device *devp;
13481709Smlf 	opaque_t	queobjp = NULL;
13491709Smlf 	opaque_t	flcobjp = NULL;
13501709Smlf 	char		que_keyvalp[64];
13511709Smlf 	int		que_keylen;
13521709Smlf 	char		flc_keyvalp[64];
13531709Smlf 	int		flc_keylen;
13541709Smlf 
13551709Smlf 	ASSERT(mutex_owned(&dkp->dk_mutex));
13561709Smlf 
13571709Smlf 	/* Create linkage to queueing routines based on property */
13581709Smlf 	que_keylen = sizeof (que_keyvalp);
13591709Smlf 	if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF,
13601709Smlf 	    DDI_PROP_CANSLEEP, "queue", que_keyvalp, &que_keylen) !=
13611709Smlf 	    DDI_PROP_SUCCESS) {
13621709Smlf 		cmn_err(CE_WARN, "cmdk_create_obj: queue property undefined");
13631709Smlf 		return (DDI_FAILURE);
13641709Smlf 	}
13651709Smlf 	que_keyvalp[que_keylen] = (char)0;
13661709Smlf 
13671709Smlf 	if (strcmp(que_keyvalp, "qfifo") == 0) {
13681709Smlf 		queobjp = (opaque_t)qfifo_create();
13691709Smlf 	} else if (strcmp(que_keyvalp, "qsort") == 0) {
13701709Smlf 		queobjp = (opaque_t)qsort_create();
13711709Smlf 	} else {
13721709Smlf 		return (DDI_FAILURE);
13731709Smlf 	}
13741709Smlf 
13751709Smlf 	/* Create linkage to dequeueing routines based on property */
13761709Smlf 	flc_keylen = sizeof (flc_keyvalp);
13771709Smlf 	if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF,
13781709Smlf 	    DDI_PROP_CANSLEEP, "flow_control", flc_keyvalp, &flc_keylen) !=
13791709Smlf 	    DDI_PROP_SUCCESS) {
13801709Smlf 		cmn_err(CE_WARN,
13811709Smlf 		    "cmdk_create_obj: flow-control property undefined");
13821709Smlf 		return (DDI_FAILURE);
13831709Smlf 	}
13841709Smlf 
13851709Smlf 	flc_keyvalp[flc_keylen] = (char)0;
13861709Smlf 
13871709Smlf 	if (strcmp(flc_keyvalp, "dsngl") == 0) {
13881709Smlf 		flcobjp = (opaque_t)dsngl_create();
13891709Smlf 	} else if (strcmp(flc_keyvalp, "dmult") == 0) {
13901709Smlf 		flcobjp = (opaque_t)dmult_create();
13911709Smlf 	} else {
13921709Smlf 		return (DDI_FAILURE);
13931709Smlf 	}
13941709Smlf 
13951709Smlf 	/* populate bbh_obj object stored in dkp */
13961709Smlf 	dkp->dk_bbh_obj.bbh_data = dkp;
13971709Smlf 	dkp->dk_bbh_obj.bbh_ops = &cmdk_bbh_ops;
13981709Smlf 
13991709Smlf 	/* create linkage to dadk */
14001709Smlf 	dkp->dk_tgobjp = (opaque_t)dadk_create();
14011709Smlf 
14021709Smlf 	devp = ddi_get_driver_private(dip);
14031709Smlf 	(void) dadk_init(DKTP_DATA, devp, flcobjp, queobjp, &dkp->dk_bbh_obj,
14041709Smlf 	    NULL);
14051709Smlf 
14061709Smlf 	return (DDI_SUCCESS);
14071709Smlf }
14081709Smlf 
14091709Smlf static void
14101709Smlf cmdk_destroy_obj(dev_info_t *dip, struct cmdk *dkp)
14111709Smlf {
14121709Smlf 	char		que_keyvalp[64];
14131709Smlf 	int		que_keylen;
14141709Smlf 	char		flc_keyvalp[64];
14151709Smlf 	int		flc_keylen;
14161709Smlf 
14171709Smlf 	ASSERT(mutex_owned(&dkp->dk_mutex));
14181709Smlf 
14191709Smlf 	(void) dadk_free((dkp->dk_tgobjp));
14201709Smlf 	dkp->dk_tgobjp = NULL;
14211709Smlf 
14221709Smlf 	que_keylen = sizeof (que_keyvalp);
14231709Smlf 	if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF,
14241709Smlf 	    DDI_PROP_CANSLEEP, "queue", que_keyvalp, &que_keylen) !=
14251709Smlf 	    DDI_PROP_SUCCESS) {
14261709Smlf 		cmn_err(CE_WARN, "cmdk_destroy_obj: queue property undefined");
14271709Smlf 		return;
14281709Smlf 	}
14291709Smlf 	que_keyvalp[que_keylen] = (char)0;
14301709Smlf 
14311709Smlf 	flc_keylen = sizeof (flc_keyvalp);
14321709Smlf 	if (ddi_prop_op(DDI_DEV_T_NONE, dip, PROP_LEN_AND_VAL_BUF,
14331709Smlf 	    DDI_PROP_CANSLEEP, "flow_control", flc_keyvalp, &flc_keylen) !=
14341709Smlf 	    DDI_PROP_SUCCESS) {
14351709Smlf 		cmn_err(CE_WARN,
14361709Smlf 		    "cmdk_destroy_obj: flow-control property undefined");
14371709Smlf 		return;
14381709Smlf 	}
14391709Smlf 	flc_keyvalp[flc_keylen] = (char)0;
14401709Smlf }
14413525Sshidokht /*ARGSUSED5*/
14421709Smlf static int
14433525Sshidokht cmdk_lb_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr,
14443525Sshidokht     diskaddr_t start, size_t count, void *tg_cookie)
14451709Smlf {
14461709Smlf 	struct cmdk	*dkp;
14471709Smlf 	opaque_t	handle;
14481709Smlf 	int		rc = 0;
14491709Smlf 	char		*bufa;
14501709Smlf 
14511709Smlf 	dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip));
14521709Smlf 	if (dkp == NULL)
14531709Smlf 		return (ENXIO);
14541709Smlf 
14551709Smlf 	if (cmd != TG_READ && cmd != TG_WRITE)
14561709Smlf 		return (EINVAL);
14571709Smlf 
14581709Smlf 	/* count must be multiple of 512 */
14591709Smlf 	count = (count + NBPSCTR - 1) & -NBPSCTR;
14601709Smlf 	handle = dadk_iob_alloc(DKTP_DATA, start, count, KM_SLEEP);
14611709Smlf 	if (!handle)
14621709Smlf 		return (ENOMEM);
14631709Smlf 
14641709Smlf 	if (cmd == TG_READ) {
14651709Smlf 		bufa = dadk_iob_xfer(DKTP_DATA, handle, B_READ);
14661709Smlf 		if (!bufa)
14671709Smlf 			rc = EIO;
14681709Smlf 		else
14691709Smlf 			bcopy(bufa, bufaddr, count);
14701709Smlf 	} else {
14711709Smlf 		bufa = dadk_iob_htoc(DKTP_DATA, handle);
14721709Smlf 		bcopy(bufaddr, bufa, count);
14731709Smlf 		bufa = dadk_iob_xfer(DKTP_DATA, handle, B_WRITE);
14741709Smlf 		if (!bufa)
14751709Smlf 			rc = EIO;
14761709Smlf 	}
14771709Smlf 	(void) dadk_iob_free(DKTP_DATA, handle);
14781709Smlf 
14791709Smlf 	return (rc);
14801709Smlf }
14811709Smlf 
14823525Sshidokht /*ARGSUSED3*/
14831709Smlf static int
14843525Sshidokht cmdk_lb_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
14851709Smlf {
14863525Sshidokht 
14871709Smlf 	struct cmdk		*dkp;
14881709Smlf 	struct tgdk_geom	phyg;
14891709Smlf 
14901709Smlf 
14911709Smlf 	dkp = ddi_get_soft_state(cmdk_state, ddi_get_instance(dip));
14921709Smlf 	if (dkp == NULL)
14931709Smlf 		return (ENXIO);
14941709Smlf 
14953525Sshidokht 	switch (cmd) {
14963525Sshidokht 	case TG_GETPHYGEOM: {
14973525Sshidokht 		cmlb_geom_t *phygeomp = (cmlb_geom_t *)arg;
14983525Sshidokht 
14993525Sshidokht 		/* dadk_getphygeom always returns success */
15003525Sshidokht 		(void) dadk_getphygeom(DKTP_DATA, &phyg);
15013525Sshidokht 
15023525Sshidokht 		phygeomp->g_capacity	= phyg.g_cap;
15033525Sshidokht 		phygeomp->g_nsect	= phyg.g_sec;
15043525Sshidokht 		phygeomp->g_nhead	= phyg.g_head;
15053525Sshidokht 		phygeomp->g_acyl	= phyg.g_acyl;
15063525Sshidokht 		phygeomp->g_ncyl	= phyg.g_cyl;
15073525Sshidokht 		phygeomp->g_secsize	= phyg.g_secsiz;
15083525Sshidokht 		phygeomp->g_intrlv	= 1;
15093525Sshidokht 		phygeomp->g_rpm		= 3600;
15101709Smlf 
15113525Sshidokht 		return (0);
15123525Sshidokht 	}
15133525Sshidokht 
15143525Sshidokht 	case TG_GETVIRTGEOM: {
15153525Sshidokht 		cmlb_geom_t *virtgeomp = (cmlb_geom_t *)arg;
15163525Sshidokht 		diskaddr_t		capacity;
15173525Sshidokht 
15183525Sshidokht 		(void) dadk_getgeom(DKTP_DATA, &phyg);
15193525Sshidokht 		capacity = phyg.g_cap;
15203525Sshidokht 
15213525Sshidokht 		/*
15223525Sshidokht 		 * If the controller returned us something that doesn't
15233525Sshidokht 		 * really fit into an Int 13/function 8 geometry
15243525Sshidokht 		 * result, just fail the ioctl.  See PSARC 1998/313.
15253525Sshidokht 		 */
15263525Sshidokht 		if (capacity < 0 || capacity >= 63 * 254 * 1024)
15273525Sshidokht 			return (EINVAL);
15281709Smlf 
15293525Sshidokht 		virtgeomp->g_capacity	= capacity;
15303525Sshidokht 		virtgeomp->g_nsect	= 63;
15313525Sshidokht 		virtgeomp->g_nhead	= 254;
15323525Sshidokht 		virtgeomp->g_ncyl	= capacity / (63 * 254);
15333525Sshidokht 		virtgeomp->g_acyl	= 0;
15343525Sshidokht 		virtgeomp->g_secsize	= 512;
15353525Sshidokht 		virtgeomp->g_intrlv	= 1;
15363525Sshidokht 		virtgeomp->g_rpm	= 3600;
15373525Sshidokht 
15383525Sshidokht 		return (0);
15393525Sshidokht 	}
15403525Sshidokht 
15413525Sshidokht 	case TG_GETCAPACITY:
15423525Sshidokht 	case TG_GETBLOCKSIZE:
15433525Sshidokht 	{
15441709Smlf 
15453525Sshidokht 		/* dadk_getphygeom always returns success */
15463525Sshidokht 		(void) dadk_getphygeom(DKTP_DATA, &phyg);
15473525Sshidokht 		if (cmd == TG_GETCAPACITY)
15483525Sshidokht 			*(diskaddr_t *)arg = phyg.g_cap;
15493525Sshidokht 		else
15503525Sshidokht 			*(uint32_t *)arg = (uint32_t)phyg.g_secsiz;
15513525Sshidokht 
15523525Sshidokht 		return (0);
15533525Sshidokht 	}
15543525Sshidokht 
15553525Sshidokht 	case TG_GETATTR: {
15563525Sshidokht 		tg_attribute_t *tgattribute = (tg_attribute_t *)arg;
15573525Sshidokht 		if ((DKTP_EXT->tg_rdonly))
15583525Sshidokht 			tgattribute->media_is_writable = FALSE;
15593525Sshidokht 		else
15603525Sshidokht 			tgattribute->media_is_writable = TRUE;
15613525Sshidokht 
15623525Sshidokht 		return (0);
15633525Sshidokht 	}
15643525Sshidokht 
15653525Sshidokht 	default:
15663525Sshidokht 		return (ENOTTY);
15673525Sshidokht 	}
15681709Smlf }
15691709Smlf 
15701709Smlf 
15711709Smlf 
15721709Smlf 
15731709Smlf 
15741709Smlf /*
15751709Smlf  * Create and register the devid.
15761709Smlf  * There are 4 different ways we can get a device id:
15771709Smlf  *    1. Already have one - nothing to do
15781709Smlf  *    2. Build one from the drive's model and serial numbers
15791709Smlf  *    3. Read one from the disk (first sector of last track)
15801709Smlf  *    4. Fabricate one and write it on the disk.
15811709Smlf  * If any of these succeeds, register the deviceid
15821709Smlf  */
15831709Smlf static void
15841709Smlf cmdk_devid_setup(struct cmdk *dkp)
15851709Smlf {
15861709Smlf 	int	rc;
15871709Smlf 
15881709Smlf 	/* Try options until one succeeds, or all have failed */
15891709Smlf 
15901709Smlf 	/* 1. All done if already registered */
15911709Smlf 	if (dkp->dk_devid != NULL)
15921709Smlf 		return;
15931709Smlf 
15941709Smlf 	/* 2. Build a devid from the model and serial number */
15951709Smlf 	rc = cmdk_devid_modser(dkp);
15961709Smlf 	if (rc != DDI_SUCCESS) {
15971709Smlf 		/* 3. Read devid from the disk, if present */
15981709Smlf 		rc = cmdk_devid_read(dkp);
15991709Smlf 
16001709Smlf 		/* 4. otherwise make one up and write it on the disk */
16011709Smlf 		if (rc != DDI_SUCCESS)
16021709Smlf 			rc = cmdk_devid_fabricate(dkp);
16031709Smlf 	}
16041709Smlf 
16051709Smlf 	/* If we managed to get a devid any of the above ways, register it */
16061709Smlf 	if (rc == DDI_SUCCESS)
16071709Smlf 		(void) ddi_devid_register(dkp->dk_dip, dkp->dk_devid);
16081709Smlf 
16091709Smlf }
16101709Smlf 
16111709Smlf /*
16121709Smlf  * Build a devid from the model and serial number
16131709Smlf  * Return DDI_SUCCESS or DDI_FAILURE.
16141709Smlf  */
16151709Smlf static int
16161709Smlf cmdk_devid_modser(struct cmdk *dkp)
16171709Smlf {
16181709Smlf 	int	rc = DDI_FAILURE;
16191709Smlf 	char	*hwid;
16201709Smlf 	int	modlen;
16211709Smlf 	int	serlen;
16221709Smlf 
16231709Smlf 	/*
16241709Smlf 	 * device ID is a concatenation of model number, '=', serial number.
16251709Smlf 	 */
16261709Smlf 	hwid = kmem_alloc(CMDK_HWIDLEN, KM_SLEEP);
16271709Smlf 	modlen = cmdk_get_modser(dkp, DIOCTL_GETMODEL, hwid, CMDK_HWIDLEN);
16281709Smlf 	if (modlen == 0) {
16291709Smlf 		rc = DDI_FAILURE;
16301709Smlf 		goto err;
16311709Smlf 	}
16321709Smlf 	hwid[modlen++] = '=';
16331709Smlf 	serlen = cmdk_get_modser(dkp, DIOCTL_GETSERIAL,
16341709Smlf 	    hwid + modlen, CMDK_HWIDLEN - modlen);
16351709Smlf 	if (serlen == 0) {
16361709Smlf 		rc = DDI_FAILURE;
16371709Smlf 		goto err;
16381709Smlf 	}
16391709Smlf 	hwid[modlen + serlen] = 0;
16401709Smlf 
16411709Smlf 	/* Initialize the device ID, trailing NULL not included */
16421709Smlf 	rc = ddi_devid_init(dkp->dk_dip, DEVID_ATA_SERIAL, modlen + serlen,
16431709Smlf 	    hwid, (ddi_devid_t *)&dkp->dk_devid);
16441709Smlf 	if (rc != DDI_SUCCESS) {
16451709Smlf 		rc = DDI_FAILURE;
16461709Smlf 		goto err;
16471709Smlf 	}
16481709Smlf 
16491709Smlf 	rc = DDI_SUCCESS;
16501709Smlf 
16511709Smlf err:
16521709Smlf 	kmem_free(hwid, CMDK_HWIDLEN);
16531709Smlf 	return (rc);
16541709Smlf }
16551709Smlf 
16561709Smlf static int
16571709Smlf cmdk_get_modser(struct cmdk *dkp, int ioccmd, char *buf, int len)
16581709Smlf {
16591709Smlf 	dadk_ioc_string_t strarg;
16601709Smlf 	int		rval;
16611709Smlf 	char		*s;
16621709Smlf 	char		ch;
16631709Smlf 	boolean_t	ret;
16641709Smlf 	int		i;
16651709Smlf 	int		tb;
16661709Smlf 
16671709Smlf 	strarg.is_buf = buf;
16681709Smlf 	strarg.is_size = len;
16691709Smlf 	if (dadk_ioctl(DKTP_DATA,
16701709Smlf 	    dkp->dk_dev,
16711709Smlf 	    ioccmd,
16721709Smlf 	    (uintptr_t)&strarg,
16731709Smlf 	    FNATIVE | FKIOCTL,
16741709Smlf 	    NULL,
16751709Smlf 	    &rval) != 0)
16761709Smlf 		return (0);
16771709Smlf 
16781709Smlf 	/*
16791709Smlf 	 * valid model/serial string must contain a non-zero non-space
16801709Smlf 	 * trim trailing spaces/NULL
16811709Smlf 	 */
16821709Smlf 	ret = B_FALSE;
16831709Smlf 	s = buf;
16841709Smlf 	for (i = 0; i < strarg.is_size; i++) {
16851709Smlf 		ch = *s++;
16861709Smlf 		if (ch != ' ' && ch != '\0')
16871709Smlf 			tb = i + 1;
16881709Smlf 		if (ch != ' ' && ch != '\0' && ch != '0')
16891709Smlf 			ret = B_TRUE;
16901709Smlf 	}
16911709Smlf 
16921709Smlf 	if (ret == B_FALSE)
16931709Smlf 		return (0);
16941709Smlf 
16951709Smlf 	return (tb);
16961709Smlf }
16971709Smlf 
16981709Smlf /*
16991709Smlf  * Read a devid from on the first block of the last track of
17001709Smlf  * the last cylinder.  Make sure what we read is a valid devid.
17011709Smlf  * Return DDI_SUCCESS or DDI_FAILURE.
17021709Smlf  */
17031709Smlf static int
17041709Smlf cmdk_devid_read(struct cmdk *dkp)
17051709Smlf {
17061709Smlf 	diskaddr_t	blk;
17071709Smlf 	struct dk_devid *dkdevidp;
17081709Smlf 	uint_t		*ip;
17091709Smlf 	int		chksum;
17101709Smlf 	int		i, sz;
1711*7573SMark.Logan@Sun.COM 	tgdk_iob_handle	handle = NULL;
17121709Smlf 	int		rc = DDI_FAILURE;
17131709Smlf 
17143525Sshidokht 	if (cmlb_get_devid_block(dkp->dk_cmlbhandle, &blk, 0))
17151709Smlf 		goto err;
17161709Smlf 
17171709Smlf 	/* read the devid */
17181709Smlf 	handle = dadk_iob_alloc(DKTP_DATA, blk, NBPSCTR, KM_SLEEP);
17191709Smlf 	if (handle == NULL)
17201709Smlf 		goto err;
17211709Smlf 
17221709Smlf 	dkdevidp = (struct dk_devid *)dadk_iob_xfer(DKTP_DATA, handle, B_READ);
17231709Smlf 	if (dkdevidp == NULL)
17241709Smlf 		goto err;
17251709Smlf 
17261709Smlf 	/* Validate the revision */
17271709Smlf 	if ((dkdevidp->dkd_rev_hi != DK_DEVID_REV_MSB) ||
17281709Smlf 	    (dkdevidp->dkd_rev_lo != DK_DEVID_REV_LSB))
17291709Smlf 		goto err;
17301709Smlf 
17311709Smlf 	/* Calculate the checksum */
17321709Smlf 	chksum = 0;
17331709Smlf 	ip = (uint_t *)dkdevidp;
17341709Smlf 	for (i = 0; i < ((NBPSCTR - sizeof (int))/sizeof (int)); i++)
17351709Smlf 		chksum ^= ip[i];
17361709Smlf 	if (DKD_GETCHKSUM(dkdevidp) != chksum)
17371709Smlf 		goto err;
17381709Smlf 
17391709Smlf 	/* Validate the device id */
17401709Smlf 	if (ddi_devid_valid((ddi_devid_t)dkdevidp->dkd_devid) != DDI_SUCCESS)
17411709Smlf 		goto err;
17421709Smlf 
17431709Smlf 	/* keep a copy of the device id */
17441709Smlf 	sz = ddi_devid_sizeof((ddi_devid_t)dkdevidp->dkd_devid);
17451709Smlf 	dkp->dk_devid = kmem_alloc(sz, KM_SLEEP);
17461709Smlf 	bcopy(dkdevidp->dkd_devid, dkp->dk_devid, sz);
17471709Smlf 
17481709Smlf 	rc = DDI_SUCCESS;
17491709Smlf 
17501709Smlf err:
17511709Smlf 	if (handle != NULL)
17521709Smlf 		(void) dadk_iob_free(DKTP_DATA, handle);
17531709Smlf 	return (rc);
17541709Smlf }
17551709Smlf 
17561709Smlf /*
17571709Smlf  * Create a devid and write it on the first block of the last track of
17581709Smlf  * the last cylinder.
17591709Smlf  * Return DDI_SUCCESS or DDI_FAILURE.
17601709Smlf  */
17611709Smlf static int
17621709Smlf cmdk_devid_fabricate(struct cmdk *dkp)
17631709Smlf {
17641709Smlf 	ddi_devid_t	devid = NULL;	/* devid made by ddi_devid_init  */
17651709Smlf 	struct dk_devid	*dkdevidp;	/* devid struct stored on disk */
17661709Smlf 	diskaddr_t	blk;
17671709Smlf 	tgdk_iob_handle	handle = NULL;
17681709Smlf 	uint_t		*ip, chksum;
17691709Smlf 	int		i;
17701709Smlf 	int		rc;
17711709Smlf 
17721709Smlf 	rc = ddi_devid_init(dkp->dk_dip, DEVID_FAB, 0, NULL, &devid);
17731709Smlf 	if (rc != DDI_SUCCESS)
17741709Smlf 		goto err;
17751709Smlf 
17763525Sshidokht 	if (cmlb_get_devid_block(dkp->dk_cmlbhandle, &blk, 0)) {
17771709Smlf 		/* no device id block address */
17781709Smlf 		return (DDI_FAILURE);
17791709Smlf 	}
17801709Smlf 
17811709Smlf 	handle = dadk_iob_alloc(DKTP_DATA, blk, NBPSCTR, KM_SLEEP);
17821709Smlf 	if (!handle)
17831709Smlf 		goto err;
17841709Smlf 
17851709Smlf 	/* Locate the buffer */
17861709Smlf 	dkdevidp = (struct dk_devid *)dadk_iob_htoc(DKTP_DATA, handle);
17871709Smlf 
17881709Smlf 	/* Fill in the revision */
17891709Smlf 	bzero(dkdevidp, NBPSCTR);
17901709Smlf 	dkdevidp->dkd_rev_hi = DK_DEVID_REV_MSB;
17911709Smlf 	dkdevidp->dkd_rev_lo = DK_DEVID_REV_LSB;
17921709Smlf 
17931709Smlf 	/* Copy in the device id */
17941709Smlf 	i = ddi_devid_sizeof(devid);
17951709Smlf 	if (i > DK_DEVID_SIZE)
17961709Smlf 		goto err;
17971709Smlf 	bcopy(devid, dkdevidp->dkd_devid, i);
17981709Smlf 
17991709Smlf 	/* Calculate the chksum */
18001709Smlf 	chksum = 0;
18011709Smlf 	ip = (uint_t *)dkdevidp;
18021709Smlf 	for (i = 0; i < ((NBPSCTR - sizeof (int))/sizeof (int)); i++)
18031709Smlf 		chksum ^= ip[i];
18041709Smlf 
18051709Smlf 	/* Fill in the checksum */
18061709Smlf 	DKD_FORMCHKSUM(chksum, dkdevidp);
18071709Smlf 
18081709Smlf 	/* write the devid */
18091709Smlf 	(void) dadk_iob_xfer(DKTP_DATA, handle, B_WRITE);
18101709Smlf 
18111709Smlf 	dkp->dk_devid = devid;
18121709Smlf 
18131709Smlf 	rc = DDI_SUCCESS;
18141709Smlf 
18151709Smlf err:
18161709Smlf 	if (handle != NULL)
18171709Smlf 		(void) dadk_iob_free(DKTP_DATA, handle);
18181709Smlf 
18191709Smlf 	if (rc != DDI_SUCCESS && devid != NULL)
18201709Smlf 		ddi_devid_free(devid);
18211709Smlf 
18221709Smlf 	return (rc);
18231709Smlf }
18241709Smlf 
18251709Smlf static void
18261709Smlf cmdk_bbh_free_alts(struct cmdk *dkp)
18271709Smlf {
18281709Smlf 	if (dkp->dk_alts_hdl) {
18291709Smlf 		(void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl);
18301709Smlf 		kmem_free(dkp->dk_slc_cnt,
18311709Smlf 		    NDKMAP * (sizeof (uint32_t) + sizeof (struct alts_ent *)));
18321709Smlf 		dkp->dk_alts_hdl = NULL;
18331709Smlf 	}
18341709Smlf }
18351709Smlf 
18361709Smlf static void
18371709Smlf cmdk_bbh_reopen(struct cmdk *dkp)
18381709Smlf {
18391709Smlf 	tgdk_iob_handle 	handle = NULL;
18401709Smlf 	diskaddr_t		slcb, slcn, slce;
18411709Smlf 	struct	alts_parttbl	*ap;
18421709Smlf 	struct	alts_ent	*enttblp;
18431709Smlf 	uint32_t		altused;
18441709Smlf 	uint32_t		altbase;
18451709Smlf 	uint32_t		altlast;
18461709Smlf 	int			alts;
18471709Smlf 	uint16_t		vtoctag;
18481709Smlf 	int			i, j;
18491709Smlf 
18501709Smlf 	/* find slice with V_ALTSCTR tag */
18511709Smlf 	for (alts = 0; alts < NDKMAP; alts++) {
18521709Smlf 		if (cmlb_partinfo(
18531709Smlf 		    dkp->dk_cmlbhandle,
18541709Smlf 		    alts,
18551709Smlf 		    &slcn,
18561709Smlf 		    &slcb,
18571709Smlf 		    NULL,
18583525Sshidokht 		    &vtoctag,
18593525Sshidokht 		    0)) {
18601709Smlf 			goto empty;	/* no partition table exists */
18611709Smlf 		}
18621709Smlf 
18631709Smlf 		if (vtoctag == V_ALTSCTR && slcn > 1)
18641709Smlf 			break;
18651709Smlf 	}
18661709Smlf 	if (alts >= NDKMAP) {
18671709Smlf 		goto empty;	/* no V_ALTSCTR slice defined */
18681709Smlf 	}
18691709Smlf 
18701709Smlf 	/* read in ALTS label block */
18711709Smlf 	handle = dadk_iob_alloc(DKTP_DATA, slcb, NBPSCTR, KM_SLEEP);
18721709Smlf 	if (!handle) {
18731709Smlf 		goto empty;
18741709Smlf 	}
18751709Smlf 
18761709Smlf 	ap = (struct alts_parttbl *)dadk_iob_xfer(DKTP_DATA, handle, B_READ);
18771709Smlf 	if (!ap || (ap->alts_sanity != ALTS_SANITY)) {
18781709Smlf 		goto empty;
18791709Smlf 	}
18801709Smlf 
18811709Smlf 	altused = ap->alts_ent_used;	/* number of BB entries */
18821709Smlf 	altbase = ap->alts_ent_base;	/* blk offset from begin slice */
18831709Smlf 	altlast = ap->alts_ent_end;	/* blk offset to last block */
18841709Smlf 	/* ((altused * sizeof (struct alts_ent) + NBPSCTR - 1) & ~NBPSCTR) */
18851709Smlf 
18861709Smlf 	if (altused == 0 ||
18871709Smlf 	    altbase < 1 ||
18881709Smlf 	    altbase > altlast ||
18891709Smlf 	    altlast >= slcn) {
18901709Smlf 		goto empty;
18911709Smlf 	}
18921709Smlf 	(void) dadk_iob_free(DKTP_DATA, handle);
18931709Smlf 
18941709Smlf 	/* read in ALTS remapping table */
18951709Smlf 	handle = dadk_iob_alloc(DKTP_DATA,
18961709Smlf 	    slcb + altbase,
18971709Smlf 	    (altlast - altbase + 1) << SCTRSHFT, KM_SLEEP);
18981709Smlf 	if (!handle) {
18991709Smlf 		goto empty;
19001709Smlf 	}
19011709Smlf 
19021709Smlf 	enttblp = (struct alts_ent *)dadk_iob_xfer(DKTP_DATA, handle, B_READ);
19031709Smlf 	if (!enttblp) {
19041709Smlf 		goto empty;
19051709Smlf 	}
19061709Smlf 
19071709Smlf 	rw_enter(&dkp->dk_bbh_mutex, RW_WRITER);
19081709Smlf 
19091709Smlf 	/* allocate space for dk_slc_cnt and dk_slc_ent tables */
19101709Smlf 	if (dkp->dk_slc_cnt == NULL) {
19111709Smlf 		dkp->dk_slc_cnt = kmem_alloc(NDKMAP *
19121709Smlf 		    (sizeof (long) + sizeof (struct alts_ent *)), KM_SLEEP);
19131709Smlf 	}
19141709Smlf 	dkp->dk_slc_ent = (struct alts_ent **)(dkp->dk_slc_cnt + NDKMAP);
19151709Smlf 
19161709Smlf 	/* free previous BB table (if any) */
19171709Smlf 	if (dkp->dk_alts_hdl) {
19181709Smlf 		(void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl);
19191709Smlf 		dkp->dk_alts_hdl = NULL;
19201709Smlf 		dkp->dk_altused = 0;
19211709Smlf 	}
19221709Smlf 
19231709Smlf 	/* save linkage to new BB table */
19241709Smlf 	dkp->dk_alts_hdl = handle;
19251709Smlf 	dkp->dk_altused = altused;
19261709Smlf 
19271709Smlf 	/*
19281709Smlf 	 * build indexes to BB table by slice
19291709Smlf 	 * effectively we have
19301709Smlf 	 *	struct alts_ent *enttblp[altused];
19311709Smlf 	 *
19321709Smlf 	 *	uint32_t	dk_slc_cnt[NDKMAP];
19331709Smlf 	 *	struct alts_ent *dk_slc_ent[NDKMAP];
19341709Smlf 	 */
19351709Smlf 	for (i = 0; i < NDKMAP; i++) {
19361709Smlf 		if (cmlb_partinfo(
19371709Smlf 		    dkp->dk_cmlbhandle,
19381709Smlf 		    i,
19391709Smlf 		    &slcn,
19401709Smlf 		    &slcb,
19411709Smlf 		    NULL,
19423525Sshidokht 		    NULL,
19433525Sshidokht 		    0)) {
19441709Smlf 			goto empty1;
19451709Smlf 		}
19461709Smlf 
19471709Smlf 		dkp->dk_slc_cnt[i] = 0;
19481709Smlf 		if (slcn == 0)
19491709Smlf 			continue;	/* slice is not allocated */
19501709Smlf 
19511709Smlf 		/* last block in slice */
19521709Smlf 		slce = slcb + slcn - 1;
19531709Smlf 
19541709Smlf 		/* find first remap entry in after beginnning of slice */
19551709Smlf 		for (j = 0; j < altused; j++) {
19561709Smlf 			if (enttblp[j].bad_start + enttblp[j].bad_end >= slcb)
19571709Smlf 				break;
19581709Smlf 		}
19591709Smlf 		dkp->dk_slc_ent[i] = enttblp + j;
19601709Smlf 
19611709Smlf 		/* count remap entrys until end of slice */
19621709Smlf 		for (; j < altused && enttblp[j].bad_start <= slce; j++) {
19631709Smlf 			dkp->dk_slc_cnt[i] += 1;
19641709Smlf 		}
19651709Smlf 	}
19661709Smlf 
19671709Smlf 	rw_exit(&dkp->dk_bbh_mutex);
19681709Smlf 	return;
19691709Smlf 
19701709Smlf empty:
19711709Smlf 	rw_enter(&dkp->dk_bbh_mutex, RW_WRITER);
19721709Smlf empty1:
19731709Smlf 	if (handle && handle != dkp->dk_alts_hdl)
19741709Smlf 		(void) dadk_iob_free(DKTP_DATA, handle);
19751709Smlf 
19761709Smlf 	if (dkp->dk_alts_hdl) {
19771709Smlf 		(void) dadk_iob_free(DKTP_DATA, dkp->dk_alts_hdl);
19781709Smlf 		dkp->dk_alts_hdl = NULL;
19791709Smlf 	}
19801709Smlf 
19811709Smlf 	rw_exit(&dkp->dk_bbh_mutex);
19821709Smlf }
19831709Smlf 
19841709Smlf /*ARGSUSED*/
19851709Smlf static bbh_cookie_t
19861709Smlf cmdk_bbh_htoc(opaque_t bbh_data, opaque_t handle)
19871709Smlf {
19881709Smlf 	struct	bbh_handle *hp;
19891709Smlf 	bbh_cookie_t ckp;
19901709Smlf 
19911709Smlf 	hp = (struct  bbh_handle *)handle;
19921709Smlf 	ckp = hp->h_cktab + hp->h_idx;
19931709Smlf 	hp->h_idx++;
19941709Smlf 	return (ckp);
19951709Smlf }
19961709Smlf 
19971709Smlf /*ARGSUSED*/
19981709Smlf static void
19991709Smlf cmdk_bbh_freehandle(opaque_t bbh_data, opaque_t handle)
20001709Smlf {
20011709Smlf 	struct	bbh_handle *hp;
20021709Smlf 
20031709Smlf 	hp = (struct  bbh_handle *)handle;
20041709Smlf 	kmem_free(handle, (sizeof (struct bbh_handle) +
20051709Smlf 	    (hp->h_totck * (sizeof (struct bbh_cookie)))));
20061709Smlf }
20071709Smlf 
20081709Smlf 
20091709Smlf /*
20101709Smlf  *	cmdk_bbh_gethandle remaps the bad sectors to alternates.
20111709Smlf  *	There are 7 different cases when the comparison is made
20121709Smlf  *	between the bad sector cluster and the disk section.
20131709Smlf  *
20141709Smlf  *	bad sector cluster	gggggggggggbbbbbbbggggggggggg
20151709Smlf  *	case 1:			   ddddd
20161709Smlf  *	case 2:				   -d-----
20171709Smlf  *	case 3:					     ddddd
20181709Smlf  *	case 4:			         dddddddddddd
20191709Smlf  *	case 5:			      ddddddd-----
20201709Smlf  *	case 6:			           ---ddddddd
20211709Smlf  *	case 7:			           ddddddd
20221709Smlf  *
20231709Smlf  *	where:  g = good sector,	b = bad sector
20241709Smlf  *		d = sector in disk section
20251709Smlf  *		- = disk section may be extended to cover those disk area
20261709Smlf  */
20271709Smlf 
20281709Smlf static opaque_t
20291709Smlf cmdk_bbh_gethandle(opaque_t bbh_data, struct buf *bp)
20301709Smlf {
20311709Smlf 	struct cmdk		*dkp = (struct cmdk *)bbh_data;
20321709Smlf 	struct bbh_handle	*hp;
20331709Smlf 	struct bbh_cookie	*ckp;
20341709Smlf 	struct alts_ent		*altp;
20351709Smlf 	uint32_t		alts_used;
20361709Smlf 	uint32_t		part = CMDKPART(bp->b_edev);
20371709Smlf 	daddr32_t		lastsec;
20381709Smlf 	long			d_count;
20391709Smlf 	int			i;
20401709Smlf 	int			idx;
20411709Smlf 	int			cnt;
20421709Smlf 
20431709Smlf 	if (part >= V_NUMPAR)
20441709Smlf 		return (NULL);
20451709Smlf 
20461709Smlf 	/*
20471709Smlf 	 * This if statement is atomic and it will succeed
20481709Smlf 	 * if there are no bad blocks (almost always)
20491709Smlf 	 *
20501709Smlf 	 * so this if is performed outside of the rw_enter for speed
20511709Smlf 	 * and then repeated inside the rw_enter for safety
20521709Smlf 	 */
20531709Smlf 	if (!dkp->dk_alts_hdl) {
20541709Smlf 		return (NULL);
20551709Smlf 	}
20561709Smlf 
20571709Smlf 	rw_enter(&dkp->dk_bbh_mutex, RW_READER);
20581709Smlf 
20591709Smlf 	if (dkp->dk_alts_hdl == NULL) {
20601709Smlf 		rw_exit(&dkp->dk_bbh_mutex);
20611709Smlf 		return (NULL);
20621709Smlf 	}
20631709Smlf 
20641709Smlf 	alts_used = dkp->dk_slc_cnt[part];
20651709Smlf 	if (alts_used == 0) {
20661709Smlf 		rw_exit(&dkp->dk_bbh_mutex);
20671709Smlf 		return (NULL);
20681709Smlf 	}
20691709Smlf 	altp = dkp->dk_slc_ent[part];
20701709Smlf 
20711709Smlf 	/*
20721709Smlf 	 * binary search for the largest bad sector index in the alternate
20731709Smlf 	 * entry table which overlaps or larger than the starting d_sec
20741709Smlf 	 */
20751709Smlf 	i = cmdk_bbh_bsearch(altp, alts_used, GET_BP_SEC(bp));
20761709Smlf 	/* if starting sector is > the largest bad sector, return */
20771709Smlf 	if (i == -1) {
20781709Smlf 		rw_exit(&dkp->dk_bbh_mutex);
20791709Smlf 		return (NULL);
20801709Smlf 	}
20811709Smlf 	/* i is the starting index.  Set altp to the starting entry addr */
20821709Smlf 	altp += i;
20831709Smlf 
20841709Smlf 	d_count = bp->b_bcount >> SCTRSHFT;
20851709Smlf 	lastsec = GET_BP_SEC(bp) + d_count - 1;
20861709Smlf 
20871709Smlf 	/* calculate the number of bad sectors */
20881709Smlf 	for (idx = i, cnt = 0; idx < alts_used; idx++, altp++, cnt++) {
20891709Smlf 		if (lastsec < altp->bad_start)
20901709Smlf 			break;
20911709Smlf 	}
20921709Smlf 
20931709Smlf 	if (!cnt) {
20941709Smlf 		rw_exit(&dkp->dk_bbh_mutex);
20951709Smlf 		return (NULL);
20961709Smlf 	}
20971709Smlf 
20981709Smlf 	/* calculate the maximum number of reserved cookies */
20991709Smlf 	cnt <<= 1;
21001709Smlf 	cnt++;
21011709Smlf 
21021709Smlf 	/* allocate the handle */
21031709Smlf 	hp = (struct bbh_handle *)kmem_zalloc((sizeof (*hp) +
21041709Smlf 	    (cnt * sizeof (*ckp))), KM_SLEEP);
21051709Smlf 
21061709Smlf 	hp->h_idx = 0;
21071709Smlf 	hp->h_totck = cnt;
21081709Smlf 	ckp = hp->h_cktab = (struct bbh_cookie *)(hp + 1);
21091709Smlf 	ckp[0].ck_sector = GET_BP_SEC(bp);
21101709Smlf 	ckp[0].ck_seclen = d_count;
21111709Smlf 
21121709Smlf 	altp = dkp->dk_slc_ent[part];
21131709Smlf 	altp += i;
21141709Smlf 	for (idx = 0; i < alts_used; i++, altp++) {
21151709Smlf 		/* CASE 1: */
21161709Smlf 		if (lastsec < altp->bad_start)
21171709Smlf 			break;
21181709Smlf 
21191709Smlf 		/* CASE 3: */
21201709Smlf 		if (ckp[idx].ck_sector > altp->bad_end)
21211709Smlf 			continue;
21221709Smlf 
21231709Smlf 		/* CASE 2 and 7: */
21241709Smlf 		if ((ckp[idx].ck_sector >= altp->bad_start) &&
21251709Smlf 		    (lastsec <= altp->bad_end)) {
21261709Smlf 			ckp[idx].ck_sector = altp->good_start +
21271709Smlf 			    ckp[idx].ck_sector - altp->bad_start;
21281709Smlf 			break;
21291709Smlf 		}
21301709Smlf 
21311709Smlf 		/* at least one bad sector in our section.  break it. */
21321709Smlf 		/* CASE 5: */
21331709Smlf 		if ((lastsec >= altp->bad_start) &&
21345295Srandyf 		    (lastsec <= altp->bad_end)) {
21351709Smlf 			ckp[idx+1].ck_seclen = lastsec - altp->bad_start + 1;
21361709Smlf 			ckp[idx].ck_seclen -= ckp[idx+1].ck_seclen;
21371709Smlf 			ckp[idx+1].ck_sector = altp->good_start;
21381709Smlf 			break;
21391709Smlf 		}
21401709Smlf 		/* CASE 6: */
21411709Smlf 		if ((ckp[idx].ck_sector <= altp->bad_end) &&
21421709Smlf 		    (ckp[idx].ck_sector >= altp->bad_start)) {
21431709Smlf 			ckp[idx+1].ck_seclen = ckp[idx].ck_seclen;
21441709Smlf 			ckp[idx].ck_seclen = altp->bad_end -
21451709Smlf 			    ckp[idx].ck_sector + 1;
21461709Smlf 			ckp[idx+1].ck_seclen -= ckp[idx].ck_seclen;
21471709Smlf 			ckp[idx].ck_sector = altp->good_start +
21481709Smlf 			    ckp[idx].ck_sector - altp->bad_start;
21491709Smlf 			idx++;
21501709Smlf 			ckp[idx].ck_sector = altp->bad_end + 1;
21511709Smlf 			continue;	/* check rest of section */
21521709Smlf 		}
21531709Smlf 
21541709Smlf 		/* CASE 4: */
21551709Smlf 		ckp[idx].ck_seclen = altp->bad_start - ckp[idx].ck_sector;
21561709Smlf 		ckp[idx+1].ck_sector = altp->good_start;
21571709Smlf 		ckp[idx+1].ck_seclen = altp->bad_end - altp->bad_start + 1;
21581709Smlf 		idx += 2;
21591709Smlf 		ckp[idx].ck_sector = altp->bad_end + 1;
21601709Smlf 		ckp[idx].ck_seclen = lastsec - altp->bad_end;
21611709Smlf 	}
21621709Smlf 
21631709Smlf 	rw_exit(&dkp->dk_bbh_mutex);
21641709Smlf 	return ((opaque_t)hp);
21651709Smlf }
21661709Smlf 
21671709Smlf static int
21681709Smlf cmdk_bbh_bsearch(struct alts_ent *buf, int cnt, daddr32_t key)
21691709Smlf {
21701709Smlf 	int	i;
21711709Smlf 	int	ind;
21721709Smlf 	int	interval;
21731709Smlf 	int	mystatus = -1;
21741709Smlf 
21751709Smlf 	if (!cnt)
21761709Smlf 		return (mystatus);
21771709Smlf 
21781709Smlf 	ind = 1; /* compiler complains about possible uninitialized var	*/
21791709Smlf 	for (i = 1; i <= cnt; i <<= 1)
21801709Smlf 		ind = i;
21811709Smlf 
21821709Smlf 	for (interval = ind; interval; ) {
21831709Smlf 		if ((key >= buf[ind-1].bad_start) &&
21841709Smlf 		    (key <= buf[ind-1].bad_end)) {
21851709Smlf 			return (ind-1);
21861709Smlf 		} else {
21871709Smlf 			interval >>= 1;
21881709Smlf 			if (key < buf[ind-1].bad_start) {
21891709Smlf 				/* record the largest bad sector index */
21901709Smlf 				mystatus = ind-1;
21911709Smlf 				if (!interval)
21921709Smlf 					break;
21931709Smlf 				ind = ind - interval;
21941709Smlf 			} else {
21951709Smlf 				/*
21961709Smlf 				 * if key is larger than the last element
21971709Smlf 				 * then break
21981709Smlf 				 */
21991709Smlf 				if ((ind == cnt) || !interval)
22001709Smlf 					break;
22011709Smlf 				if ((ind+interval) <= cnt)
22021709Smlf 					ind += interval;
22031709Smlf 			}
22041709Smlf 		}
22051709Smlf 	}
22061709Smlf 	return (mystatus);
22071709Smlf }
2208