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