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