xref: /titanic_51/usr/src/uts/common/io/cmlb.c (revision 65908c77dfc02644236ba18bffe67b5ed6f23135)
13ccda647Slclee /*
23ccda647Slclee  * CDDL HEADER START
33ccda647Slclee  *
43ccda647Slclee  * The contents of this file are subject to the terms of the
5e8fb11a1Sshidokht  * Common Development and Distribution License (the "License").
6e8fb11a1Sshidokht  * You may not use this file except in compliance with the License.
73ccda647Slclee  *
83ccda647Slclee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93ccda647Slclee  * or http://www.opensolaris.org/os/licensing.
103ccda647Slclee  * See the License for the specific language governing permissions
113ccda647Slclee  * and limitations under the License.
123ccda647Slclee  *
133ccda647Slclee  * When distributing Covered Code, include this CDDL HEADER in each
143ccda647Slclee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153ccda647Slclee  * If applicable, add the following below this CDDL HEADER, with the
163ccda647Slclee  * fields enclosed by brackets "[]" replaced with your own identifying
173ccda647Slclee  * information: Portions Copyright [yyyy] [name of copyright owner]
183ccda647Slclee  *
193ccda647Slclee  * CDDL HEADER END
203ccda647Slclee  */
213ccda647Slclee 
223ccda647Slclee /*
2348794f22SSriram Popuri  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
243ccda647Slclee  * Use is subject to license terms.
253ccda647Slclee  */
263ccda647Slclee 
273ccda647Slclee /*
283ccda647Slclee  * This module provides support for labeling operations for target
293ccda647Slclee  * drivers.
303ccda647Slclee  */
313ccda647Slclee 
323ccda647Slclee #include <sys/scsi/scsi.h>
333ccda647Slclee #include <sys/sunddi.h>
343ccda647Slclee #include <sys/dklabel.h>
353ccda647Slclee #include <sys/dkio.h>
363ccda647Slclee #include <sys/vtoc.h>
373ccda647Slclee #include <sys/dktp/fdisk.h>
383ccda647Slclee #include <sys/vtrace.h>
393ccda647Slclee #include <sys/efi_partition.h>
403ccda647Slclee #include <sys/cmlb.h>
413ccda647Slclee #include <sys/cmlb_impl.h>
42b9ccdc5aScth #include <sys/ddi_impldefs.h>
433ccda647Slclee 
443ccda647Slclee /*
453ccda647Slclee  * Driver minor node structure and data table
463ccda647Slclee  */
473ccda647Slclee struct driver_minor_data {
483ccda647Slclee 	char	*name;
493ccda647Slclee 	minor_t	minor;
503ccda647Slclee 	int	type;
513ccda647Slclee };
523ccda647Slclee 
533ccda647Slclee static struct driver_minor_data dk_minor_data[] = {
543ccda647Slclee 	{"a", 0, S_IFBLK},
553ccda647Slclee 	{"b", 1, S_IFBLK},
563ccda647Slclee 	{"c", 2, S_IFBLK},
573ccda647Slclee 	{"d", 3, S_IFBLK},
583ccda647Slclee 	{"e", 4, S_IFBLK},
593ccda647Slclee 	{"f", 5, S_IFBLK},
603ccda647Slclee 	{"g", 6, S_IFBLK},
613ccda647Slclee 	{"h", 7, S_IFBLK},
623ccda647Slclee #if defined(_SUNOS_VTOC_16)
633ccda647Slclee 	{"i", 8, S_IFBLK},
643ccda647Slclee 	{"j", 9, S_IFBLK},
653ccda647Slclee 	{"k", 10, S_IFBLK},
663ccda647Slclee 	{"l", 11, S_IFBLK},
673ccda647Slclee 	{"m", 12, S_IFBLK},
683ccda647Slclee 	{"n", 13, S_IFBLK},
693ccda647Slclee 	{"o", 14, S_IFBLK},
703ccda647Slclee 	{"p", 15, S_IFBLK},
713ccda647Slclee #endif			/* defined(_SUNOS_VTOC_16) */
723ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
733ccda647Slclee 	{"q", 16, S_IFBLK},
743ccda647Slclee 	{"r", 17, S_IFBLK},
753ccda647Slclee 	{"s", 18, S_IFBLK},
763ccda647Slclee 	{"t", 19, S_IFBLK},
773ccda647Slclee 	{"u", 20, S_IFBLK},
783ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
793ccda647Slclee 	{"a,raw", 0, S_IFCHR},
803ccda647Slclee 	{"b,raw", 1, S_IFCHR},
813ccda647Slclee 	{"c,raw", 2, S_IFCHR},
823ccda647Slclee 	{"d,raw", 3, S_IFCHR},
833ccda647Slclee 	{"e,raw", 4, S_IFCHR},
843ccda647Slclee 	{"f,raw", 5, S_IFCHR},
853ccda647Slclee 	{"g,raw", 6, S_IFCHR},
863ccda647Slclee 	{"h,raw", 7, S_IFCHR},
873ccda647Slclee #if defined(_SUNOS_VTOC_16)
883ccda647Slclee 	{"i,raw", 8, S_IFCHR},
893ccda647Slclee 	{"j,raw", 9, S_IFCHR},
903ccda647Slclee 	{"k,raw", 10, S_IFCHR},
913ccda647Slclee 	{"l,raw", 11, S_IFCHR},
923ccda647Slclee 	{"m,raw", 12, S_IFCHR},
933ccda647Slclee 	{"n,raw", 13, S_IFCHR},
943ccda647Slclee 	{"o,raw", 14, S_IFCHR},
953ccda647Slclee 	{"p,raw", 15, S_IFCHR},
963ccda647Slclee #endif			/* defined(_SUNOS_VTOC_16) */
973ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
983ccda647Slclee 	{"q,raw", 16, S_IFCHR},
993ccda647Slclee 	{"r,raw", 17, S_IFCHR},
1003ccda647Slclee 	{"s,raw", 18, S_IFCHR},
1013ccda647Slclee 	{"t,raw", 19, S_IFCHR},
1023ccda647Slclee 	{"u,raw", 20, S_IFCHR},
1033ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1043ccda647Slclee 	{0}
1053ccda647Slclee };
1063ccda647Slclee 
1073ccda647Slclee static struct driver_minor_data dk_minor_data_efi[] = {
1083ccda647Slclee 	{"a", 0, S_IFBLK},
1093ccda647Slclee 	{"b", 1, S_IFBLK},
1103ccda647Slclee 	{"c", 2, S_IFBLK},
1113ccda647Slclee 	{"d", 3, S_IFBLK},
1123ccda647Slclee 	{"e", 4, S_IFBLK},
1133ccda647Slclee 	{"f", 5, S_IFBLK},
1143ccda647Slclee 	{"g", 6, S_IFBLK},
1153ccda647Slclee 	{"wd", 7, S_IFBLK},
1167e70b434SShidokht Yadegari #if defined(_SUNOS_VTOC_16)
1177e70b434SShidokht Yadegari 	{"i", 8, S_IFBLK},
1187e70b434SShidokht Yadegari 	{"j", 9, S_IFBLK},
1197e70b434SShidokht Yadegari 	{"k", 10, S_IFBLK},
1207e70b434SShidokht Yadegari 	{"l", 11, S_IFBLK},
1217e70b434SShidokht Yadegari 	{"m", 12, S_IFBLK},
1227e70b434SShidokht Yadegari 	{"n", 13, S_IFBLK},
1237e70b434SShidokht Yadegari 	{"o", 14, S_IFBLK},
1247e70b434SShidokht Yadegari 	{"p", 15, S_IFBLK},
1257e70b434SShidokht Yadegari #endif			/* defined(_SUNOS_VTOC_16) */
1263ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
1273ccda647Slclee 	{"q", 16, S_IFBLK},
1283ccda647Slclee 	{"r", 17, S_IFBLK},
1293ccda647Slclee 	{"s", 18, S_IFBLK},
1303ccda647Slclee 	{"t", 19, S_IFBLK},
1313ccda647Slclee 	{"u", 20, S_IFBLK},
1323ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1333ccda647Slclee 	{"a,raw", 0, S_IFCHR},
1343ccda647Slclee 	{"b,raw", 1, S_IFCHR},
1353ccda647Slclee 	{"c,raw", 2, S_IFCHR},
1363ccda647Slclee 	{"d,raw", 3, S_IFCHR},
1373ccda647Slclee 	{"e,raw", 4, S_IFCHR},
1383ccda647Slclee 	{"f,raw", 5, S_IFCHR},
1393ccda647Slclee 	{"g,raw", 6, S_IFCHR},
1403ccda647Slclee 	{"wd,raw", 7, S_IFCHR},
1417e70b434SShidokht Yadegari #if defined(_SUNOS_VTOC_16)
1427e70b434SShidokht Yadegari 	{"i,raw", 8, S_IFCHR},
1437e70b434SShidokht Yadegari 	{"j,raw", 9, S_IFCHR},
1447e70b434SShidokht Yadegari 	{"k,raw", 10, S_IFCHR},
1457e70b434SShidokht Yadegari 	{"l,raw", 11, S_IFCHR},
1467e70b434SShidokht Yadegari 	{"m,raw", 12, S_IFCHR},
1477e70b434SShidokht Yadegari 	{"n,raw", 13, S_IFCHR},
1487e70b434SShidokht Yadegari 	{"o,raw", 14, S_IFCHR},
1497e70b434SShidokht Yadegari 	{"p,raw", 15, S_IFCHR},
1507e70b434SShidokht Yadegari #endif			/* defined(_SUNOS_VTOC_16) */
1513ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
1523ccda647Slclee 	{"q,raw", 16, S_IFCHR},
1533ccda647Slclee 	{"r,raw", 17, S_IFCHR},
1543ccda647Slclee 	{"s,raw", 18, S_IFCHR},
1553ccda647Slclee 	{"t,raw", 19, S_IFCHR},
1563ccda647Slclee 	{"u,raw", 20, S_IFCHR},
1573ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1583ccda647Slclee 	{0}
1593ccda647Slclee };
1603ccda647Slclee 
161b9ccdc5aScth /*
162b9ccdc5aScth  * Declare the dynamic properties implemented in prop_op(9E) implementation
163b9ccdc5aScth  * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
164b9ccdc5aScth  * of drivers that call cmlb_attach().
165b9ccdc5aScth  */
166b9ccdc5aScth static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
167b9ccdc5aScth 	{"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
168b9ccdc5aScth 	{"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
169b9ccdc5aScth 	{"device-nblocks",	DDI_PROP_TYPE_INT64},
170b9ccdc5aScth 	{"device-blksize",	DDI_PROP_TYPE_INT},
171b9ccdc5aScth 	{NULL}
172b9ccdc5aScth };
1733ccda647Slclee 
17406bbe1e0Sedp /*
17506bbe1e0Sedp  * External kernel interfaces
17606bbe1e0Sedp  */
1773ccda647Slclee extern struct mod_ops mod_miscops;
1783ccda647Slclee 
17906bbe1e0Sedp extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
18006bbe1e0Sedp     int spec_type, minor_t minor_num);
18106bbe1e0Sedp 
1823ccda647Slclee /*
1833ccda647Slclee  * Global buffer and mutex for debug logging
1843ccda647Slclee  */
1853ccda647Slclee static char	cmlb_log_buffer[1024];
1863ccda647Slclee static kmutex_t	cmlb_log_mutex;
1873ccda647Slclee 
1883ccda647Slclee 
189e8fb11a1Sshidokht struct cmlb_lun *cmlb_debug_cl = NULL;
1903ccda647Slclee uint_t cmlb_level_mask = 0x0;
1913ccda647Slclee 
1923ccda647Slclee int cmlb_rot_delay = 4;	/* default rotational delay */
1933ccda647Slclee 
1943ccda647Slclee static struct modlmisc modlmisc = {
1953ccda647Slclee 	&mod_miscops,   /* Type of module */
196342440ecSPrasad Singamsetty 	"Common Labeling module"
1973ccda647Slclee };
1983ccda647Slclee 
1993ccda647Slclee static struct modlinkage modlinkage = {
2003ccda647Slclee 	MODREV_1, (void *)&modlmisc, NULL
2013ccda647Slclee };
2023ccda647Slclee 
2033ccda647Slclee /* Local function prototypes */
204e8fb11a1Sshidokht static dev_t cmlb_make_device(struct cmlb_lun *cl);
2057f0b8309SEdward Pilatowicz static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
206e8fb11a1Sshidokht     int flags, void *tg_cookie);
207e8fb11a1Sshidokht static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
208e8fb11a1Sshidokht     void *tg_cookie);
209e8fb11a1Sshidokht static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
210e8fb11a1Sshidokht     void *tg_cookie);
2113ccda647Slclee static void cmlb_swap_efi_gpt(efi_gpt_t *e);
2123ccda647Slclee static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
2133ccda647Slclee static int cmlb_validate_efi(efi_gpt_t *labp);
214e8fb11a1Sshidokht static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
215e8fb11a1Sshidokht     void *tg_cookie);
216e8fb11a1Sshidokht static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
217e8fb11a1Sshidokht static int  cmlb_uselabel(struct cmlb_lun *cl,  struct dk_label *l, int flags);
218e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
219e8fb11a1Sshidokht static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
220e8fb11a1Sshidokht #endif
221e8fb11a1Sshidokht static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
222e8fb11a1Sshidokht static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
223e8fb11a1Sshidokht static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
224e8fb11a1Sshidokht     void *tg_cookie);
225e8fb11a1Sshidokht static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
226e8fb11a1Sshidokht static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
227e8fb11a1Sshidokht static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
228e8fb11a1Sshidokht static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
229e8fb11a1Sshidokht static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
2307f0b8309SEdward Pilatowicz static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
2313ccda647Slclee 
2323ccda647Slclee #if defined(__i386) || defined(__amd64)
233e8fb11a1Sshidokht static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
2343ccda647Slclee #endif
2353ccda647Slclee 
2363ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
2377f0b8309SEdward Pilatowicz static boolean_t  cmlb_has_max_chs_vals(struct ipart *fdp);
2383ccda647Slclee #endif
2393ccda647Slclee 
2403ccda647Slclee #if defined(_SUNOS_VTOC_16)
241e8fb11a1Sshidokht static void cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g);
2423ccda647Slclee #endif
2433ccda647Slclee 
244e8fb11a1Sshidokht static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
245e8fb11a1Sshidokht     void *tg_cookie);
246e8fb11a1Sshidokht static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
247e8fb11a1Sshidokht static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
248e8fb11a1Sshidokht     void *tg_cookie);
249e8fb11a1Sshidokht static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
250e8fb11a1Sshidokht static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
251e8fb11a1Sshidokht     void *tg_cookie);
252e8fb11a1Sshidokht static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
253e8fb11a1Sshidokht     int flag, void *tg_cookie);
254e8fb11a1Sshidokht static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
255e8fb11a1Sshidokht     void *tg_cookie);
256342440ecSPrasad Singamsetty static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
257342440ecSPrasad Singamsetty     void *tg_cookie);
258e8fb11a1Sshidokht static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
259e8fb11a1Sshidokht     int flag, void *tg_cookie);
260342440ecSPrasad Singamsetty static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
261342440ecSPrasad Singamsetty     int flag, void *tg_cookie);
262e8fb11a1Sshidokht static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
263e8fb11a1Sshidokht     void *tg_cookie);
264e8fb11a1Sshidokht static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
265e8fb11a1Sshidokht     void *tg_cookie);
266e8fb11a1Sshidokht static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
267e8fb11a1Sshidokht     void *tg_cookie);
2683ccda647Slclee 
2693ccda647Slclee #if defined(__i386) || defined(__amd64)
270e8fb11a1Sshidokht static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
271e8fb11a1Sshidokht static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag);
272e8fb11a1Sshidokht static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
2733ccda647Slclee     int flag);
274342440ecSPrasad Singamsetty static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
275342440ecSPrasad Singamsetty     int flag);
2763ccda647Slclee #endif
2773ccda647Slclee 
278e8fb11a1Sshidokht static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
2793ccda647Slclee static void cmlb_v_log(dev_info_t *dev, char *label, uint_t level,
2803ccda647Slclee     const char *fmt, va_list ap);
2813ccda647Slclee static void cmlb_log(dev_info_t *dev, char *label, uint_t level,
2823ccda647Slclee     const char *fmt, ...);
2833ccda647Slclee 
2843ccda647Slclee int
2853ccda647Slclee _init(void)
2863ccda647Slclee {
2873ccda647Slclee 	mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
2883ccda647Slclee 	return (mod_install(&modlinkage));
2893ccda647Slclee }
2903ccda647Slclee 
2913ccda647Slclee int
2923ccda647Slclee _info(struct modinfo *modinfop)
2933ccda647Slclee {
2943ccda647Slclee 	return (mod_info(&modlinkage, modinfop));
2953ccda647Slclee }
2963ccda647Slclee 
2973ccda647Slclee int
2983ccda647Slclee _fini(void)
2993ccda647Slclee {
3003ccda647Slclee 	int err;
3013ccda647Slclee 
3023ccda647Slclee 	if ((err = mod_remove(&modlinkage)) != 0) {
3033ccda647Slclee 		return (err);
3043ccda647Slclee 	}
3053ccda647Slclee 
3063ccda647Slclee 	mutex_destroy(&cmlb_log_mutex);
3073ccda647Slclee 	return (err);
3083ccda647Slclee }
3093ccda647Slclee 
3103ccda647Slclee /*
3113ccda647Slclee  * cmlb_dbg is used for debugging to log additional info
3123ccda647Slclee  * Level of output is controlled via cmlb_level_mask setting.
3133ccda647Slclee  */
3143ccda647Slclee static void
315e8fb11a1Sshidokht cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
3163ccda647Slclee {
3173ccda647Slclee 	va_list		ap;
3183ccda647Slclee 	dev_info_t	*dev;
3193ccda647Slclee 	uint_t		level_mask = 0;
3203ccda647Slclee 
321e8fb11a1Sshidokht 	ASSERT(cl != NULL);
322e8fb11a1Sshidokht 	dev = CMLB_DEVINFO(cl);
3233ccda647Slclee 	ASSERT(dev != NULL);
3243ccda647Slclee 	/*
3253ccda647Slclee 	 * Filter messages based on the global component and level masks,
326e8fb11a1Sshidokht 	 * also print if cl matches the value of cmlb_debug_cl, or if
327e8fb11a1Sshidokht 	 * cmlb_debug_cl is set to NULL.
3283ccda647Slclee 	 */
3293ccda647Slclee 	if (comp & CMLB_TRACE)
3303ccda647Slclee 		level_mask |= CMLB_LOGMASK_TRACE;
3313ccda647Slclee 
3323ccda647Slclee 	if (comp & CMLB_INFO)
3333ccda647Slclee 		level_mask |= CMLB_LOGMASK_INFO;
3343ccda647Slclee 
3353ccda647Slclee 	if (comp & CMLB_ERROR)
3363ccda647Slclee 		level_mask |= CMLB_LOGMASK_ERROR;
3373ccda647Slclee 
3383ccda647Slclee 	if ((cmlb_level_mask & level_mask) &&
339e8fb11a1Sshidokht 	    ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
3403ccda647Slclee 		va_start(ap, fmt);
341e8fb11a1Sshidokht 		cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
3423ccda647Slclee 		va_end(ap);
3433ccda647Slclee 	}
3443ccda647Slclee }
3453ccda647Slclee 
3463ccda647Slclee /*
3473ccda647Slclee  * cmlb_log is basically a duplicate of scsi_log. It is redefined here
3483ccda647Slclee  * so that this module does not depend on scsi module.
3493ccda647Slclee  */
3503ccda647Slclee static void
3513ccda647Slclee cmlb_log(dev_info_t *dev, char *label, uint_t level, const char *fmt, ...)
3523ccda647Slclee {
3533ccda647Slclee 	va_list		ap;
3543ccda647Slclee 
3553ccda647Slclee 	va_start(ap, fmt);
3563ccda647Slclee 	cmlb_v_log(dev, label, level, fmt, ap);
3573ccda647Slclee 	va_end(ap);
3583ccda647Slclee }
3593ccda647Slclee 
3603ccda647Slclee static void
3613ccda647Slclee cmlb_v_log(dev_info_t *dev, char *label, uint_t level, const char *fmt,
3623ccda647Slclee     va_list ap)
3633ccda647Slclee {
3643ccda647Slclee 	static char 	name[256];
3653ccda647Slclee 	int 		log_only = 0;
3663ccda647Slclee 	int 		boot_only = 0;
3673ccda647Slclee 	int 		console_only = 0;
3683ccda647Slclee 
3693ccda647Slclee 	mutex_enter(&cmlb_log_mutex);
3703ccda647Slclee 
3713ccda647Slclee 	if (dev) {
3723ccda647Slclee 		if (level == CE_PANIC || level == CE_WARN ||
3733ccda647Slclee 		    level == CE_NOTE) {
3743ccda647Slclee 			(void) sprintf(name, "%s (%s%d):\n",
3753ccda647Slclee 			    ddi_pathname(dev, cmlb_log_buffer),
3763ccda647Slclee 			    label, ddi_get_instance(dev));
3773ccda647Slclee 		} else {
3783ccda647Slclee 			name[0] = '\0';
3793ccda647Slclee 		}
3803ccda647Slclee 	} else {
3813ccda647Slclee 		(void) sprintf(name, "%s:", label);
3823ccda647Slclee 	}
3833ccda647Slclee 
3843ccda647Slclee 	(void) vsprintf(cmlb_log_buffer, fmt, ap);
3853ccda647Slclee 
3863ccda647Slclee 	switch (cmlb_log_buffer[0]) {
3873ccda647Slclee 	case '!':
3883ccda647Slclee 		log_only = 1;
3893ccda647Slclee 		break;
3903ccda647Slclee 	case '?':
3913ccda647Slclee 		boot_only = 1;
3923ccda647Slclee 		break;
3933ccda647Slclee 	case '^':
3943ccda647Slclee 		console_only = 1;
3953ccda647Slclee 		break;
3963ccda647Slclee 	}
3973ccda647Slclee 
3983ccda647Slclee 	switch (level) {
3993ccda647Slclee 	case CE_NOTE:
4003ccda647Slclee 		level = CE_CONT;
4013ccda647Slclee 		/* FALLTHROUGH */
4023ccda647Slclee 	case CE_CONT:
4033ccda647Slclee 	case CE_WARN:
4043ccda647Slclee 	case CE_PANIC:
4053ccda647Slclee 		if (boot_only) {
4063ccda647Slclee 			cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
4073ccda647Slclee 		} else if (console_only) {
4083ccda647Slclee 			cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
4093ccda647Slclee 		} else if (log_only) {
4103ccda647Slclee 			cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
4113ccda647Slclee 		} else {
4123ccda647Slclee 			cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
4133ccda647Slclee 		}
4143ccda647Slclee 		break;
4153ccda647Slclee 	case CE_IGNORE:
4163ccda647Slclee 		break;
4173ccda647Slclee 	default:
4183ccda647Slclee 		cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
4193ccda647Slclee 		break;
4203ccda647Slclee 	}
4213ccda647Slclee 	mutex_exit(&cmlb_log_mutex);
4223ccda647Slclee }
4233ccda647Slclee 
4243ccda647Slclee 
4253ccda647Slclee /*
4263ccda647Slclee  * cmlb_alloc_handle:
4273ccda647Slclee  *
4283ccda647Slclee  *	Allocates a handle.
4293ccda647Slclee  *
4303ccda647Slclee  * Arguments:
4313ccda647Slclee  *	cmlbhandlep	pointer to handle
4323ccda647Slclee  *
4333ccda647Slclee  * Notes:
4343ccda647Slclee  *	Allocates a handle and stores the allocated handle in the area
4353ccda647Slclee  *	pointed to by cmlbhandlep
4363ccda647Slclee  *
4373ccda647Slclee  * Context:
4383ccda647Slclee  *	Kernel thread only (can sleep).
4393ccda647Slclee  */
4403ccda647Slclee void
4413ccda647Slclee cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
4423ccda647Slclee {
443e8fb11a1Sshidokht 	struct cmlb_lun 	*cl;
4443ccda647Slclee 
445e8fb11a1Sshidokht 	cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
4463ccda647Slclee 	ASSERT(cmlbhandlep != NULL);
4473ccda647Slclee 
448e8fb11a1Sshidokht 	cl->cl_state = CMLB_INITED;
449e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
450e8fb11a1Sshidokht 	mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
4513ccda647Slclee 
452e8fb11a1Sshidokht 	*cmlbhandlep = (cmlb_handle_t)(cl);
4533ccda647Slclee }
4543ccda647Slclee 
4553ccda647Slclee /*
4563ccda647Slclee  * cmlb_free_handle
4573ccda647Slclee  *
4583ccda647Slclee  *	Frees handle.
4593ccda647Slclee  *
4603ccda647Slclee  * Arguments:
4613ccda647Slclee  *	cmlbhandlep	pointer to handle
4623ccda647Slclee  */
4633ccda647Slclee void
4643ccda647Slclee cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
4653ccda647Slclee {
466e8fb11a1Sshidokht 	struct cmlb_lun 	*cl;
4673ccda647Slclee 
468e8fb11a1Sshidokht 	cl = (struct cmlb_lun *)*cmlbhandlep;
469e8fb11a1Sshidokht 	if (cl != NULL) {
470e8fb11a1Sshidokht 		mutex_destroy(CMLB_MUTEX(cl));
471e8fb11a1Sshidokht 		kmem_free(cl, sizeof (struct cmlb_lun));
4723ccda647Slclee 	}
4733ccda647Slclee 
4743ccda647Slclee }
4753ccda647Slclee 
4763ccda647Slclee /*
4773ccda647Slclee  * cmlb_attach:
4783ccda647Slclee  *
4793ccda647Slclee  *	Attach handle to device, create minor nodes for device.
4803ccda647Slclee  *
4813ccda647Slclee  * Arguments:
4823ccda647Slclee  * 	devi		pointer to device's dev_info structure.
4833ccda647Slclee  * 	tgopsp		pointer to array of functions cmlb can use to callback
4843ccda647Slclee  *			to target driver.
4853ccda647Slclee  *
4863ccda647Slclee  *	device_type	Peripheral device type as defined in
4873ccda647Slclee  *			scsi/generic/inquiry.h
4883ccda647Slclee  *
4893ccda647Slclee  *	is_removable	whether or not device is removable.
4903ccda647Slclee  *
491e8fb11a1Sshidokht  *	is_hotpluggable	whether or not device is hotpluggable.
492e8fb11a1Sshidokht  *
4933ccda647Slclee  *	node_type	minor node type (as used by ddi_create_minor_node)
4943ccda647Slclee  *
4953ccda647Slclee  *	alter_behavior
4963ccda647Slclee  *			bit flags:
4973ccda647Slclee  *
4983ccda647Slclee  *			CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
4993ccda647Slclee  *			an alternate slice for the default label, if
5003ccda647Slclee  *			device type is DTYPE_DIRECT an architectures default
5013ccda647Slclee  *			label type is VTOC16.
5023ccda647Slclee  *			Otherwise alternate slice will no be created.
5033ccda647Slclee  *
5043ccda647Slclee  *
5053ccda647Slclee  *			CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
5063ccda647Slclee  *			geometry and label for DKIOCGGEOM and DKIOCGVTOC
5073ccda647Slclee  *			on architecture with VTOC8 label types.
5083ccda647Slclee  *
509e8fb11a1Sshidokht  * 			CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
510e8fb11a1Sshidokht  *                      one bug in obtaining capacity (in sd):
511e8fb11a1Sshidokht  *			SCSI READ_CAPACITY command returns the LBA number of the
512e8fb11a1Sshidokht  *			last logical block, but sd once treated this number as
513e8fb11a1Sshidokht  *			disks' capacity on x86 platform. And LBAs are addressed
514e8fb11a1Sshidokht  *			based 0. So the last block was lost on x86 platform.
515e8fb11a1Sshidokht  *
516e8fb11a1Sshidokht  *			Now, we remove this workaround. In order for present sd
517e8fb11a1Sshidokht  *			driver to work with disks which are labeled/partitioned
518e8fb11a1Sshidokht  *			via previous sd, we add workaround as follows:
519e8fb11a1Sshidokht  *
520e8fb11a1Sshidokht  *			1) Locate backup EFI label: cmlb searches the next to
521e8fb11a1Sshidokht  *			   last
522e8fb11a1Sshidokht  *			   block for backup EFI label. If fails, it will
523e8fb11a1Sshidokht  *			   turn to the last block for backup EFI label;
524e8fb11a1Sshidokht  *
525e8fb11a1Sshidokht  *			2) Clear backup EFI label: cmlb first search the last
526e8fb11a1Sshidokht  *			   block for backup EFI label, and will search the
527e8fb11a1Sshidokht  *			   next to last block only if failed for the last
528e8fb11a1Sshidokht  *			   block.
529e8fb11a1Sshidokht  *
530e8fb11a1Sshidokht  *			3) Calculate geometry:refer to cmlb_convert_geometry()
531e8fb11a1Sshidokht  *			   If capacity increasing by 1 causes disks' capacity
532342440ecSPrasad Singamsetty  *			   to cross over the limits in geometry calculation,
533e8fb11a1Sshidokht  *			   geometry info will change. This will raise an issue:
534e8fb11a1Sshidokht  *			   In case that primary VTOC label is destroyed, format
535e8fb11a1Sshidokht  *			   commandline can restore it via backup VTOC labels.
536e8fb11a1Sshidokht  *			   And format locates backup VTOC labels by use of
537e8fb11a1Sshidokht  *			   geometry. So changing geometry will
538e8fb11a1Sshidokht  *			   prevent format from finding backup VTOC labels. To
539e8fb11a1Sshidokht  *			   eliminate this side effect for compatibility,
540e8fb11a1Sshidokht  *			   sd uses (capacity -1) to calculate geometry;
541e8fb11a1Sshidokht  *
542e8fb11a1Sshidokht  *			4) 1TB disks: some important data structures use
543e8fb11a1Sshidokht  *			   32-bit signed long/int (for example, daddr_t),
544e8fb11a1Sshidokht  *			   so that sd doesn't support a disk with capacity
545e8fb11a1Sshidokht  *			   larger than 1TB on 32-bit platform. However,
546e8fb11a1Sshidokht  *			   for exactly 1TB disk, it was treated as (1T - 512)B
547e8fb11a1Sshidokht  *			   in the past, and could have valid Solaris
548e8fb11a1Sshidokht  *			   partitions. To workaround this, if an exactly 1TB
549e8fb11a1Sshidokht  *			   disk has Solaris fdisk partition, it will be allowed
550e8fb11a1Sshidokht  *			   to work with sd.
551e8fb11a1Sshidokht  *
552e8fb11a1Sshidokht  *
5533ccda647Slclee  *
554843e1988Sjohnlev  *			CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
555843e1988Sjohnlev  *			the entire disk, if there is no valid partition info.
556843e1988Sjohnlev  *			If there is a valid Solaris partition, s0 and s2 will
557843e1988Sjohnlev  *			only cover the entire Solaris partition.
558843e1988Sjohnlev  *
559843e1988Sjohnlev  *
5603ccda647Slclee  *	cmlbhandle	cmlb handle associated with device
5613ccda647Slclee  *
562e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
563e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
564e8fb11a1Sshidokht  *
5653ccda647Slclee  * Notes:
5663ccda647Slclee  *	Assumes a default label based on capacity for non-removable devices.
5673ccda647Slclee  *	If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
5683ccda647Slclee  *	for the architecture).
5693ccda647Slclee  *
5703ccda647Slclee  *	For removable devices, default label type is assumed to be VTOC
5713ccda647Slclee  *	type. Create minor nodes based on a default label type.
5723ccda647Slclee  *	Label on the media is not validated.
5733ccda647Slclee  *	minor number consists of:
5743ccda647Slclee  *		if _SUNOS_VTOC_8 is defined
5753ccda647Slclee  *			lowest 3 bits is taken as partition number
5763ccda647Slclee  *			the rest is instance number
5773ccda647Slclee  *		if _SUNOS_VTOC_16 is defined
5783ccda647Slclee  *			lowest 6 bits is taken as partition number
5793ccda647Slclee  *			the rest is instance number
5803ccda647Slclee  *
5813ccda647Slclee  *
5823ccda647Slclee  * Return values:
5833ccda647Slclee  *	0 	Success
5843ccda647Slclee  * 	ENXIO 	creating minor nodes failed.
585e8fb11a1Sshidokht  *	EINVAL  invalid arg, unsupported tg_ops version
5863ccda647Slclee  */
5873ccda647Slclee int
5883ccda647Slclee cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
5897f0b8309SEdward Pilatowicz     boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
590e8fb11a1Sshidokht     int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
5913ccda647Slclee {
5923ccda647Slclee 
593e8fb11a1Sshidokht 	struct cmlb_lun	*cl = (struct cmlb_lun *)cmlbhandle;
5943ccda647Slclee 	diskaddr_t	cap;
5953ccda647Slclee 	int		status;
5963ccda647Slclee 
5977f0b8309SEdward Pilatowicz 	ASSERT(VALID_BOOLEAN(is_removable));
5987f0b8309SEdward Pilatowicz 	ASSERT(VALID_BOOLEAN(is_hotpluggable));
5997f0b8309SEdward Pilatowicz 
600e8fb11a1Sshidokht 	if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
601e8fb11a1Sshidokht 		return (EINVAL);
6023ccda647Slclee 
603e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
604e8fb11a1Sshidokht 
605e8fb11a1Sshidokht 	CMLB_DEVINFO(cl) = devi;
606e8fb11a1Sshidokht 	cl->cmlb_tg_ops = tgopsp;
607e8fb11a1Sshidokht 	cl->cl_device_type = device_type;
608e8fb11a1Sshidokht 	cl->cl_is_removable = is_removable;
609e8fb11a1Sshidokht 	cl->cl_is_hotpluggable = is_hotpluggable;
610e8fb11a1Sshidokht 	cl->cl_node_type = node_type;
611e8fb11a1Sshidokht 	cl->cl_sys_blocksize = DEV_BSIZE;
6127f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
613e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_VTOC;
614e8fb11a1Sshidokht 	cl->cl_alter_behavior = alter_behavior;
615e8fb11a1Sshidokht 	cl->cl_reserved = -1;
616342440ecSPrasad Singamsetty 	cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
6173ccda647Slclee 
6187f0b8309SEdward Pilatowicz 	if (!is_removable) {
619e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
620e8fb11a1Sshidokht 		status = DK_TG_GETCAP(cl, &cap, tg_cookie);
621e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
622342440ecSPrasad Singamsetty 		if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
623342440ecSPrasad Singamsetty 			/* set default EFI if > 2TB */
624e8fb11a1Sshidokht 			cl->cl_def_labeltype = CMLB_LABEL_EFI;
6253ccda647Slclee 		}
6263ccda647Slclee 	}
6273ccda647Slclee 
6283ccda647Slclee 	/* create minor nodes based on default label type */
629e8fb11a1Sshidokht 	cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
630e8fb11a1Sshidokht 	cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
6313ccda647Slclee 
632e8fb11a1Sshidokht 	if (cmlb_create_minor_nodes(cl) != 0) {
633e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
6343ccda647Slclee 		return (ENXIO);
6353ccda647Slclee 	}
6363ccda647Slclee 
637b9ccdc5aScth 	/* Define the dynamic properties for devinfo spapshots. */
638b9ccdc5aScth 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
639b9ccdc5aScth 
640e8fb11a1Sshidokht 	cl->cl_state = CMLB_ATTACHED;
6413ccda647Slclee 
642e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
6433ccda647Slclee 	return (0);
6443ccda647Slclee }
6453ccda647Slclee 
6463ccda647Slclee /*
6473ccda647Slclee  * cmlb_detach:
6483ccda647Slclee  *
6493ccda647Slclee  * Invalidate in-core labeling data and remove all minor nodes for
6503ccda647Slclee  * the device associate with handle.
6513ccda647Slclee  *
6523ccda647Slclee  * Arguments:
6533ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
6543ccda647Slclee  *
655e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
656e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
657e8fb11a1Sshidokht  *
6583ccda647Slclee  */
659e8fb11a1Sshidokht /*ARGSUSED1*/
6603ccda647Slclee void
661e8fb11a1Sshidokht cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
6623ccda647Slclee {
663e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
6643ccda647Slclee 
665e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
666e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
6677f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
668e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
669b9ccdc5aScth 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
670e8fb11a1Sshidokht 	cl->cl_state = CMLB_INITED;
671e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
6723ccda647Slclee }
6733ccda647Slclee 
6743ccda647Slclee /*
6753ccda647Slclee  * cmlb_validate:
6763ccda647Slclee  *
6773ccda647Slclee  *	Validates label.
6783ccda647Slclee  *
6793ccda647Slclee  * Arguments
6803ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
6813ccda647Slclee  *
682e8fb11a1Sshidokht  *	flags		operation flags. used for verbosity control
683e8fb11a1Sshidokht  *
684e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
685e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
686e8fb11a1Sshidokht  *
687e8fb11a1Sshidokht  *
6883ccda647Slclee  * Notes:
6893ccda647Slclee  *	If new label type is different from the current, adjust minor nodes
6903ccda647Slclee  *	accordingly.
6913ccda647Slclee  *
6923ccda647Slclee  * Return values:
6933ccda647Slclee  *	0		success
6943ccda647Slclee  *			Note: having fdisk but no solaris partition is assumed
6953ccda647Slclee  *			success.
6963ccda647Slclee  *
6973ccda647Slclee  *	ENOMEM		memory allocation failed
6983ccda647Slclee  *	EIO		i/o errors during read or get capacity
6993ccda647Slclee  * 	EACCESS		reservation conflicts
7003ccda647Slclee  * 	EINVAL		label was corrupt, or no default label was assumed
7013ccda647Slclee  *	ENXIO		invalid handle
7023ccda647Slclee  */
7033ccda647Slclee int
704e8fb11a1Sshidokht cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
7053ccda647Slclee {
706e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
7073ccda647Slclee 	int 		rval;
7083ccda647Slclee 	int  		ret = 0;
7093ccda647Slclee 
7103ccda647Slclee 	/*
711e8fb11a1Sshidokht 	 * Temp work-around checking cl for NULL since there is a bug
7123ccda647Slclee 	 * in sd_detach calling this routine from taskq_dispatch
7133ccda647Slclee 	 * inited function.
7143ccda647Slclee 	 */
715e8fb11a1Sshidokht 	if (cl == NULL)
7163ccda647Slclee 		return (ENXIO);
7173ccda647Slclee 
718e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
719e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
720e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
7213ccda647Slclee 		return (ENXIO);
7223ccda647Slclee 	}
7233ccda647Slclee 
7247f0b8309SEdward Pilatowicz 	rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
725e8fb11a1Sshidokht 	    flags, tg_cookie);
7263ccda647Slclee 
7273ccda647Slclee 	if (rval == ENOTSUP) {
7287f0b8309SEdward Pilatowicz 		if (cl->cl_f_geometry_is_valid) {
729e8fb11a1Sshidokht 			cl->cl_cur_labeltype = CMLB_LABEL_EFI;
7303ccda647Slclee 			ret = 0;
7313ccda647Slclee 		} else {
7323ccda647Slclee 			ret = EINVAL;
7333ccda647Slclee 		}
7343ccda647Slclee 	} else {
7353ccda647Slclee 		ret = rval;
7363ccda647Slclee 		if (ret == 0)
737e8fb11a1Sshidokht 			cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
7383ccda647Slclee 	}
7393ccda647Slclee 
7403ccda647Slclee 	if (ret == 0)
741e8fb11a1Sshidokht 		(void) cmlb_create_minor_nodes(cl);
7423ccda647Slclee 
743e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
7443ccda647Slclee 	return (ret);
7453ccda647Slclee }
7463ccda647Slclee 
7473ccda647Slclee /*
7483ccda647Slclee  * cmlb_invalidate:
7493ccda647Slclee  *	Invalidate in core label data
7503ccda647Slclee  *
7513ccda647Slclee  * Arguments:
7523ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
753e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
754e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
7553ccda647Slclee  */
756e8fb11a1Sshidokht /*ARGSUSED1*/
7573ccda647Slclee void
758e8fb11a1Sshidokht cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
7593ccda647Slclee {
760e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
7613ccda647Slclee 
762e8fb11a1Sshidokht 	if (cl == NULL)
7633ccda647Slclee 		return;
7643ccda647Slclee 
765e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
7667f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
767e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
7683ccda647Slclee }
7693ccda647Slclee 
7703ccda647Slclee /*
771e8fb11a1Sshidokht  * cmlb_is_valid
772e8fb11a1Sshidokht  * 	Get status on whether the incore label/geom data is valid
773e8fb11a1Sshidokht  *
774e8fb11a1Sshidokht  * Arguments:
775e8fb11a1Sshidokht  *	cmlbhandle      cmlb handle associated with device.
776e8fb11a1Sshidokht  *
777e8fb11a1Sshidokht  * Return values:
7787f0b8309SEdward Pilatowicz  *	B_TRUE if incore label/geom data is valid.
7797f0b8309SEdward Pilatowicz  *	B_FALSE otherwise.
780e8fb11a1Sshidokht  *
781e8fb11a1Sshidokht  */
782e8fb11a1Sshidokht 
783e8fb11a1Sshidokht 
7847f0b8309SEdward Pilatowicz boolean_t
785e8fb11a1Sshidokht cmlb_is_valid(cmlb_handle_t cmlbhandle)
786e8fb11a1Sshidokht {
787e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
788e8fb11a1Sshidokht 
789e8fb11a1Sshidokht 	if (cmlbhandle == NULL)
7907f0b8309SEdward Pilatowicz 		return (B_FALSE);
791e8fb11a1Sshidokht 
792e8fb11a1Sshidokht 	return (cl->cl_f_geometry_is_valid);
793e8fb11a1Sshidokht 
794e8fb11a1Sshidokht }
795e8fb11a1Sshidokht 
796e8fb11a1Sshidokht 
797e8fb11a1Sshidokht 
798e8fb11a1Sshidokht /*
7993ccda647Slclee  * cmlb_close:
8003ccda647Slclee  *
8013ccda647Slclee  * Close the device, revert to a default label minor node for the device,
8023ccda647Slclee  * if it is removable.
8033ccda647Slclee  *
8043ccda647Slclee  * Arguments:
8053ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
8063ccda647Slclee  *
807e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
808e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
8093ccda647Slclee  * Return values:
8103ccda647Slclee  *	0	Success
8113ccda647Slclee  * 	ENXIO	Re-creating minor node failed.
8123ccda647Slclee  */
813e8fb11a1Sshidokht /*ARGSUSED1*/
8143ccda647Slclee int
815e8fb11a1Sshidokht cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
8163ccda647Slclee {
817e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8183ccda647Slclee 
819e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
8207f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
8213ccda647Slclee 
8223ccda647Slclee 	/* revert to default minor node for this device */
823e8fb11a1Sshidokht 	if (ISREMOVABLE(cl)) {
824e8fb11a1Sshidokht 		cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
825e8fb11a1Sshidokht 		(void) cmlb_create_minor_nodes(cl);
8263ccda647Slclee 	}
8273ccda647Slclee 
828e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
8293ccda647Slclee 	return (0);
8303ccda647Slclee }
8313ccda647Slclee 
8323ccda647Slclee /*
8333ccda647Slclee  * cmlb_get_devid_block:
8343ccda647Slclee  *	 get the block number where device id is stored.
8353ccda647Slclee  *
8363ccda647Slclee  * Arguments:
8373ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
8383ccda647Slclee  *	devidblockp	pointer to block number.
839e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
840e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
8413ccda647Slclee  *
8423ccda647Slclee  * Notes:
8433ccda647Slclee  *	It stores the block number of device id in the area pointed to
8443ccda647Slclee  *	by devidblockp.
8453ccda647Slclee  * 	with the block number of device id.
8463ccda647Slclee  *
8473ccda647Slclee  * Return values:
8483ccda647Slclee  *	0	success
8493ccda647Slclee  *	EINVAL 	device id does not apply to current label type.
8503ccda647Slclee  */
851e8fb11a1Sshidokht /*ARGSUSED2*/
8523ccda647Slclee int
853e8fb11a1Sshidokht cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
854e8fb11a1Sshidokht     void *tg_cookie)
8553ccda647Slclee {
8563ccda647Slclee 	daddr_t			spc, blk, head, cyl;
857e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8583ccda647Slclee 
859e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
860e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
861e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
8623ccda647Slclee 		return (EINVAL);
8633ccda647Slclee 	}
8643ccda647Slclee 
8657f0b8309SEdward Pilatowicz 	if ((!cl->cl_f_geometry_is_valid) ||
866e8fb11a1Sshidokht 	    (cl->cl_solaris_size < DK_LABEL_LOC)) {
867e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
868e8fb11a1Sshidokht 		return (EINVAL);
869e8fb11a1Sshidokht 	}
870e8fb11a1Sshidokht 
871e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
872e8fb11a1Sshidokht 		if (cl->cl_reserved != -1) {
873e8fb11a1Sshidokht 			blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
874e8fb11a1Sshidokht 		} else {
875e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
876e8fb11a1Sshidokht 			return (EINVAL);
877e8fb11a1Sshidokht 		}
878e8fb11a1Sshidokht 	} else {
8795d22422eSml40262 		/* if the disk is unlabeled, don't write a devid to it */
880342440ecSPrasad Singamsetty 		if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
8815d22422eSml40262 			mutex_exit(CMLB_MUTEX(cl));
8825d22422eSml40262 			return (EINVAL);
8835d22422eSml40262 		}
8845d22422eSml40262 
8853ccda647Slclee 		/* this geometry doesn't allow us to write a devid */
886e8fb11a1Sshidokht 		if (cl->cl_g.dkg_acyl < 2) {
887e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
8883ccda647Slclee 			return (EINVAL);
8893ccda647Slclee 		}
8903ccda647Slclee 
8913ccda647Slclee 		/*
8923ccda647Slclee 		 * Subtract 2 guarantees that the next to last cylinder
8933ccda647Slclee 		 * is used
8943ccda647Slclee 		 */
895e8fb11a1Sshidokht 		cyl  = cl->cl_g.dkg_ncyl  + cl->cl_g.dkg_acyl - 2;
896e8fb11a1Sshidokht 		spc  = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
897e8fb11a1Sshidokht 		head = cl->cl_g.dkg_nhead - 1;
8985d22422eSml40262 		blk  = cl->cl_solaris_offset +
8995d22422eSml40262 		    (cyl * (spc - cl->cl_g.dkg_apc)) +
900e8fb11a1Sshidokht 		    (head * cl->cl_g.dkg_nsect) + 1;
9013ccda647Slclee 	}
902e8fb11a1Sshidokht 
9033ccda647Slclee 	*devidblockp = blk;
904e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
9053ccda647Slclee 	return (0);
9063ccda647Slclee }
9073ccda647Slclee 
9083ccda647Slclee /*
9093ccda647Slclee  * cmlb_partinfo:
9103ccda647Slclee  *	Get partition info for specified partition number.
9113ccda647Slclee  *
9123ccda647Slclee  * Arguments:
9133ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
9143ccda647Slclee  *	part		partition number
9153ccda647Slclee  *	nblocksp	pointer to number of blocks
9163ccda647Slclee  *	startblockp	pointer to starting block
9173ccda647Slclee  *	partnamep	pointer to name of partition
9183ccda647Slclee  *	tagp		pointer to tag info
919e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
920e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
9213ccda647Slclee  *
9223ccda647Slclee  *
9233ccda647Slclee  * Notes:
9243ccda647Slclee  *	If in-core label is not valid, this functions tries to revalidate
9253ccda647Slclee  *	the label. If label is valid, it stores the total number of blocks
9263ccda647Slclee  *	in this partition in the area pointed to by nblocksp, starting
9273ccda647Slclee  *	block number in area pointed to by startblockp,  pointer to partition
9283ccda647Slclee  *	name in area pointed to by partnamep, and tag value in area
9293ccda647Slclee  *	pointed by tagp.
9303ccda647Slclee  *	For EFI labels, tag value will be set to 0.
9313ccda647Slclee  *
9323ccda647Slclee  *	For all nblocksp, startblockp and partnamep, tagp, a value of NULL
9333ccda647Slclee  *	indicates the corresponding info is not requested.
9343ccda647Slclee  *
9353ccda647Slclee  *
9363ccda647Slclee  * Return values:
9373ccda647Slclee  *	0	success
9383ccda647Slclee  *	EINVAL  no valid label or requested partition number is invalid.
9393ccda647Slclee  *
9403ccda647Slclee  */
9413ccda647Slclee int
9423ccda647Slclee cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
943e8fb11a1Sshidokht     diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
9443ccda647Slclee {
9453ccda647Slclee 
946e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
9473ccda647Slclee 	int rval;
9483ccda647Slclee 
949e8fb11a1Sshidokht 	ASSERT(cl != NULL);
950e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
951e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
952e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
9533ccda647Slclee 		return (EINVAL);
9543ccda647Slclee 	}
9553ccda647Slclee 
9563ccda647Slclee 	if (part  < 0 || part >= MAXPART) {
9573ccda647Slclee 		rval = EINVAL;
9583ccda647Slclee 	} else {
9597f0b8309SEdward Pilatowicz 		if (!cl->cl_f_geometry_is_valid)
9607f0b8309SEdward Pilatowicz 			(void) cmlb_validate_geometry((struct cmlb_lun *)cl,
9617f0b8309SEdward Pilatowicz 			    B_FALSE, 0, tg_cookie);
962e8fb11a1Sshidokht 
963342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_16)
9647f0b8309SEdward Pilatowicz 		if (((!cl->cl_f_geometry_is_valid) ||
965342440ecSPrasad Singamsetty 		    (part < NDKMAP && cl->cl_solaris_size == 0)) &&
966342440ecSPrasad Singamsetty 		    (part != P0_RAW_DISK)) {
967342440ecSPrasad Singamsetty #else
9687f0b8309SEdward Pilatowicz 		if ((!cl->cl_f_geometry_is_valid) ||
969e8fb11a1Sshidokht 		    (part < NDKMAP && cl->cl_solaris_size == 0)) {
970342440ecSPrasad Singamsetty #endif
9713ccda647Slclee 			rval = EINVAL;
9723ccda647Slclee 		} else {
9733ccda647Slclee 			if (startblockp != NULL)
974e8fb11a1Sshidokht 				*startblockp = (diskaddr_t)cl->cl_offset[part];
9753ccda647Slclee 
9763ccda647Slclee 			if (nblocksp != NULL)
9773ccda647Slclee 				*nblocksp = (diskaddr_t)
978e8fb11a1Sshidokht 				    cl->cl_map[part].dkl_nblk;
9793ccda647Slclee 
9803ccda647Slclee 			if (tagp != NULL)
981e8fb11a1Sshidokht 				if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
9823ccda647Slclee 					*tagp = V_UNASSIGNED;
9833ccda647Slclee 				else
984e8fb11a1Sshidokht 					*tagp = cl->cl_vtoc.v_part[part].p_tag;
9853ccda647Slclee 			rval = 0;
9863ccda647Slclee 		}
9873ccda647Slclee 
9883ccda647Slclee 		/* consistent with behavior of sd for getting minor name */
9893ccda647Slclee 		if (partnamep != NULL)
9903ccda647Slclee 			*partnamep = dk_minor_data[part].name;
9913ccda647Slclee 
9923ccda647Slclee 	}
9933ccda647Slclee 
994e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
9953ccda647Slclee 	return (rval);
9963ccda647Slclee }
9973ccda647Slclee 
998af007057Syl194034 /*
999af007057Syl194034  * cmlb_efi_label_capacity:
1000af007057Syl194034  *	Get capacity stored in EFI disk label.
1001af007057Syl194034  *
1002af007057Syl194034  * Arguments:
1003af007057Syl194034  *	cmlbhandle	cmlb handle associated with device.
1004af007057Syl194034  *	capacity	pointer to capacity stored in EFI disk label.
1005af007057Syl194034  *	tg_cookie	cookie from target driver to be passed back to target
1006af007057Syl194034  *			driver when we call back to it through tg_ops.
1007af007057Syl194034  *
1008af007057Syl194034  *
1009af007057Syl194034  * Notes:
1010af007057Syl194034  *	If in-core label is not valid, this functions tries to revalidate
1011af007057Syl194034  *	the label. If label is valid and is an EFI label, it stores the capacity
1012af007057Syl194034  *      in disk label in the area pointed to by capacity.
1013af007057Syl194034  *
1014af007057Syl194034  *
1015af007057Syl194034  * Return values:
1016af007057Syl194034  *	0	success
1017af007057Syl194034  *	EINVAL  no valid EFI label or capacity is NULL.
1018af007057Syl194034  *
1019af007057Syl194034  */
1020af007057Syl194034 int
1021af007057Syl194034 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1022af007057Syl194034     void *tg_cookie)
1023af007057Syl194034 {
1024af007057Syl194034 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1025af007057Syl194034 	int rval;
1026af007057Syl194034 
1027af007057Syl194034 	ASSERT(cl != NULL);
1028af007057Syl194034 	mutex_enter(CMLB_MUTEX(cl));
1029af007057Syl194034 	if (cl->cl_state < CMLB_ATTACHED) {
1030af007057Syl194034 		mutex_exit(CMLB_MUTEX(cl));
1031af007057Syl194034 		return (EINVAL);
1032af007057Syl194034 	}
1033af007057Syl194034 
10347f0b8309SEdward Pilatowicz 	if (!cl->cl_f_geometry_is_valid)
10357f0b8309SEdward Pilatowicz 		(void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1036af007057Syl194034 		    0, tg_cookie);
1037af007057Syl194034 
10387f0b8309SEdward Pilatowicz 	if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1039af007057Syl194034 	    (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1040af007057Syl194034 		rval = EINVAL;
1041af007057Syl194034 	} else {
1042af007057Syl194034 		*capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1043af007057Syl194034 		rval = 0;
1044af007057Syl194034 	}
1045af007057Syl194034 
1046af007057Syl194034 	mutex_exit(CMLB_MUTEX(cl));
1047af007057Syl194034 	return (rval);
1048af007057Syl194034 }
1049af007057Syl194034 
1050e8fb11a1Sshidokht /* Caller should make sure Test Unit Ready succeeds before calling this. */
10513ccda647Slclee /*ARGSUSED*/
10523ccda647Slclee int
10533ccda647Slclee cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1054e8fb11a1Sshidokht     int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
10553ccda647Slclee {
10563ccda647Slclee 
10573ccda647Slclee 	int err;
1058e8fb11a1Sshidokht 	struct cmlb_lun *cl;
10593ccda647Slclee 
1060e8fb11a1Sshidokht 	cl = (struct cmlb_lun *)cmlbhandle;
10613ccda647Slclee 
1062e8fb11a1Sshidokht 	ASSERT(cl != NULL);
10633ccda647Slclee 
1064e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
1065e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
1066e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
10673ccda647Slclee 		return (EIO);
10683ccda647Slclee 	}
10693ccda647Slclee 
10703ccda647Slclee 	switch (cmd) {
1071342440ecSPrasad Singamsetty 		case DKIOCSEXTVTOC:
10723ccda647Slclee 		case DKIOCSGEOM:
10733ccda647Slclee 		case DKIOCSETEFI:
10743ccda647Slclee 		case DKIOCSMBOOT:
10753ccda647Slclee 			break;
1076342440ecSPrasad Singamsetty 		case DKIOCSVTOC:
1077342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
1078342440ecSPrasad Singamsetty 		case DKIOCPARTINFO:
1079342440ecSPrasad Singamsetty #endif
1080342440ecSPrasad Singamsetty 			if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1081342440ecSPrasad Singamsetty 				mutex_exit(CMLB_MUTEX(cl));
1082342440ecSPrasad Singamsetty 				return (EOVERFLOW);
1083342440ecSPrasad Singamsetty 			}
1084342440ecSPrasad Singamsetty 			break;
10853ccda647Slclee 		default:
1086342440ecSPrasad Singamsetty 			(void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1087adc076afSshidokht 			    tg_cookie);
1088e8fb11a1Sshidokht 
1089e8fb11a1Sshidokht 			switch (cmd) {
1090e8fb11a1Sshidokht 			case DKIOCGVTOC:
1091342440ecSPrasad Singamsetty 			case DKIOCGAPART:
1092e8fb11a1Sshidokht 			case DKIOCSAPART:
1093e8fb11a1Sshidokht 
1094342440ecSPrasad Singamsetty 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1095342440ecSPrasad Singamsetty 					/* GPT label on disk */
1096342440ecSPrasad Singamsetty 					mutex_exit(CMLB_MUTEX(cl));
1097342440ecSPrasad Singamsetty 					return (ENOTSUP);
1098342440ecSPrasad Singamsetty 				} else if
1099342440ecSPrasad Singamsetty 				    (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1100342440ecSPrasad Singamsetty 					mutex_exit(CMLB_MUTEX(cl));
1101342440ecSPrasad Singamsetty 					return (EOVERFLOW);
1102342440ecSPrasad Singamsetty 				}
1103342440ecSPrasad Singamsetty 				break;
1104342440ecSPrasad Singamsetty 
1105342440ecSPrasad Singamsetty 			case DKIOCGGEOM:
1106342440ecSPrasad Singamsetty 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1107342440ecSPrasad Singamsetty 					/* GPT label on disk */
1108e8fb11a1Sshidokht 					mutex_exit(CMLB_MUTEX(cl));
1109e8fb11a1Sshidokht 					return (ENOTSUP);
1110e8fb11a1Sshidokht 				}
1111342440ecSPrasad Singamsetty 				break;
1112342440ecSPrasad Singamsetty 			default:
1113342440ecSPrasad Singamsetty 				break;
11143ccda647Slclee 			}
1115e8fb11a1Sshidokht 	}
11163ccda647Slclee 
1117e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
11183ccda647Slclee 
11193ccda647Slclee 	switch (cmd) {
11203ccda647Slclee 	case DKIOCGGEOM:
1121e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1122e8fb11a1Sshidokht 		err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
11233ccda647Slclee 		break;
11243ccda647Slclee 
11253ccda647Slclee 	case DKIOCSGEOM:
1126e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1127e8fb11a1Sshidokht 		err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
11283ccda647Slclee 		break;
11293ccda647Slclee 
11303ccda647Slclee 	case DKIOCGAPART:
1131e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1132e8fb11a1Sshidokht 		err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1133e8fb11a1Sshidokht 		    flag, tg_cookie);
11343ccda647Slclee 		break;
11353ccda647Slclee 
11363ccda647Slclee 	case DKIOCSAPART:
1137e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1138e8fb11a1Sshidokht 		err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
11393ccda647Slclee 		break;
11403ccda647Slclee 
11413ccda647Slclee 	case DKIOCGVTOC:
1142e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1143e8fb11a1Sshidokht 		err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
11443ccda647Slclee 		break;
11453ccda647Slclee 
1146342440ecSPrasad Singamsetty 	case DKIOCGEXTVTOC:
1147342440ecSPrasad Singamsetty 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1148342440ecSPrasad Singamsetty 		err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1149342440ecSPrasad Singamsetty 		break;
1150342440ecSPrasad Singamsetty 
11513ccda647Slclee 	case DKIOCGETEFI:
1152e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1153e8fb11a1Sshidokht 		err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
11543ccda647Slclee 		break;
11553ccda647Slclee 
11563ccda647Slclee 	case DKIOCPARTITION:
1157e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1158e8fb11a1Sshidokht 		err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
11593ccda647Slclee 		break;
11603ccda647Slclee 
11613ccda647Slclee 	case DKIOCSVTOC:
1162e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1163e8fb11a1Sshidokht 		err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1164e8fb11a1Sshidokht 		    tg_cookie);
11653ccda647Slclee 		break;
11663ccda647Slclee 
1167342440ecSPrasad Singamsetty 	case DKIOCSEXTVTOC:
1168342440ecSPrasad Singamsetty 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1169342440ecSPrasad Singamsetty 		err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1170342440ecSPrasad Singamsetty 		    tg_cookie);
1171342440ecSPrasad Singamsetty 		break;
1172342440ecSPrasad Singamsetty 
11733ccda647Slclee 	case DKIOCSETEFI:
1174e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1175e8fb11a1Sshidokht 		err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
11763ccda647Slclee 		break;
11773ccda647Slclee 
11783ccda647Slclee 	case DKIOCGMBOOT:
1179e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1180e8fb11a1Sshidokht 		err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
11813ccda647Slclee 		break;
11823ccda647Slclee 
11833ccda647Slclee 	case DKIOCSMBOOT:
1184e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1185e8fb11a1Sshidokht 		err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
11863ccda647Slclee 		break;
11873ccda647Slclee 	case DKIOCG_PHYGEOM:
1188e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
11893ccda647Slclee #if defined(__i386) || defined(__amd64)
1190e8fb11a1Sshidokht 		err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag);
11913ccda647Slclee #else
11923ccda647Slclee 		err = ENOTTY;
11933ccda647Slclee #endif
11943ccda647Slclee 		break;
11953ccda647Slclee 	case DKIOCG_VIRTGEOM:
1196e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
11973ccda647Slclee #if defined(__i386) || defined(__amd64)
1198e8fb11a1Sshidokht 		err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
11993ccda647Slclee #else
12003ccda647Slclee 		err = ENOTTY;
12013ccda647Slclee #endif
12023ccda647Slclee 		break;
12033ccda647Slclee 	case DKIOCPARTINFO:
1204e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
12053ccda647Slclee #if defined(__i386) || defined(__amd64)
1206e8fb11a1Sshidokht 		err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
12073ccda647Slclee #else
12083ccda647Slclee 		err = ENOTTY;
12093ccda647Slclee #endif
12103ccda647Slclee 		break;
1211342440ecSPrasad Singamsetty 	case DKIOCEXTPARTINFO:
1212342440ecSPrasad Singamsetty 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1213342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
1214342440ecSPrasad Singamsetty 		err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1215342440ecSPrasad Singamsetty #else
1216342440ecSPrasad Singamsetty 		err = ENOTTY;
1217342440ecSPrasad Singamsetty #endif
1218342440ecSPrasad Singamsetty 		break;
1219f85c7842SSuhasini Peddada 
12203ccda647Slclee 	default:
12213ccda647Slclee 		err = ENOTTY;
12223ccda647Slclee 
12233ccda647Slclee 	}
1224b9ccdc5aScth 
1225b9ccdc5aScth 	/*
1226b9ccdc5aScth 	 * An ioctl that succeeds and changed ('set') size(9P) information
1227b9ccdc5aScth 	 * needs to invalidate the cached devinfo snapshot to avoid having
1228b9ccdc5aScth 	 * old information being returned in a snapshots.
1229b9ccdc5aScth 	 *
1230b9ccdc5aScth 	 * NB: When available, call ddi_change_minor_node() to clear
1231b9ccdc5aScth 	 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1232b9ccdc5aScth 	 */
1233b9ccdc5aScth 	if (err == 0) {
1234b9ccdc5aScth 		switch (cmd) {
1235b9ccdc5aScth 		case DKIOCSGEOM:
1236b9ccdc5aScth 		case DKIOCSAPART:
1237b9ccdc5aScth 		case DKIOCSVTOC:
1238342440ecSPrasad Singamsetty 		case DKIOCSEXTVTOC:
1239b9ccdc5aScth 		case DKIOCSETEFI:
1240b9ccdc5aScth 			i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1241b9ccdc5aScth 			    i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1242b9ccdc5aScth 		}
1243b9ccdc5aScth 	}
12443ccda647Slclee 	return (err);
12453ccda647Slclee }
12463ccda647Slclee 
12473ccda647Slclee dev_t
1248e8fb11a1Sshidokht cmlb_make_device(struct cmlb_lun *cl)
12493ccda647Slclee {
12505c066ec2SJerry Gilliam 	return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1251e8fb11a1Sshidokht 	    ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
12523ccda647Slclee }
12533ccda647Slclee 
12543ccda647Slclee /*
12553ccda647Slclee  * Function: cmlb_check_update_blockcount
12563ccda647Slclee  *
12573ccda647Slclee  * Description: If current capacity value is invalid, obtains the
12583ccda647Slclee  *		current capacity from target driver.
12593ccda647Slclee  *
12603ccda647Slclee  * Return Code: 0	success
12613ccda647Slclee  *		EIO	failure
12623ccda647Slclee  */
12633ccda647Slclee static int
1264e8fb11a1Sshidokht cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
12653ccda647Slclee {
12663ccda647Slclee 	int status;
12673ccda647Slclee 	diskaddr_t capacity;
1268e8fb11a1Sshidokht 	uint32_t lbasize;
12693ccda647Slclee 
1270e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
12713ccda647Slclee 
12727f0b8309SEdward Pilatowicz 	if (cl->cl_f_geometry_is_valid)
12737f0b8309SEdward Pilatowicz 		return (0);
12747f0b8309SEdward Pilatowicz 
1275e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
1276e8fb11a1Sshidokht 	status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1277e8fb11a1Sshidokht 	if (status != 0) {
1278e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
1279e8fb11a1Sshidokht 		return (EIO);
1280e8fb11a1Sshidokht 	}
1281e8fb11a1Sshidokht 
1282e8fb11a1Sshidokht 	status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1283e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
1284e8fb11a1Sshidokht 	if (status != 0)
1285e8fb11a1Sshidokht 		return (EIO);
1286e8fb11a1Sshidokht 
1287e8fb11a1Sshidokht 	if ((capacity != 0) && (lbasize != 0)) {
1288e8fb11a1Sshidokht 		cl->cl_blockcount = capacity;
1289e8fb11a1Sshidokht 		cl->cl_tgt_blocksize = lbasize;
1290*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		if (!cl->cl_is_removable) {
1291*65908c77Syu, larry liu - Sun Microsystems - Beijing China 			cl->cl_sys_blocksize = lbasize;
1292*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		}
12933ccda647Slclee 		return (0);
12947f0b8309SEdward Pilatowicz 	} else {
12953ccda647Slclee 		return (EIO);
12967f0b8309SEdward Pilatowicz 	}
12973ccda647Slclee }
12983ccda647Slclee 
129906bbe1e0Sedp static int
130006bbe1e0Sedp cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
130106bbe1e0Sedp     minor_t minor_num, char *node_type, int flag, boolean_t internal)
130206bbe1e0Sedp {
13037f0b8309SEdward Pilatowicz 	ASSERT(VALID_BOOLEAN(internal));
13047f0b8309SEdward Pilatowicz 
130506bbe1e0Sedp 	if (internal)
130606bbe1e0Sedp 		return (ddi_create_internal_pathname(dip,
130706bbe1e0Sedp 		    name, spec_type, minor_num));
130806bbe1e0Sedp 	else
130906bbe1e0Sedp 		return (ddi_create_minor_node(dip,
131006bbe1e0Sedp 		    name, spec_type, minor_num, node_type, flag));
131106bbe1e0Sedp }
131206bbe1e0Sedp 
13133ccda647Slclee /*
13143ccda647Slclee  *    Function: cmlb_create_minor_nodes
13153ccda647Slclee  *
13163ccda647Slclee  * Description: Create or adjust the minor device nodes for the instance.
13173ccda647Slclee  * 		Minor nodes are created based on default label type,
13183ccda647Slclee  *		current label type and last label type we created
13193ccda647Slclee  *		minor nodes based on.
13203ccda647Slclee  *
13213ccda647Slclee  *
1322e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
13233ccda647Slclee  *
13243ccda647Slclee  * Return Code: 0 success
13253ccda647Slclee  *		ENXIO	failure.
13263ccda647Slclee  *
13273ccda647Slclee  *     Context: Kernel thread context
13283ccda647Slclee  */
13293ccda647Slclee static int
1330e8fb11a1Sshidokht cmlb_create_minor_nodes(struct cmlb_lun *cl)
13313ccda647Slclee {
13323ccda647Slclee 	struct driver_minor_data	*dmdp;
13333ccda647Slclee 	int				instance;
13343ccda647Slclee 	char				name[48];
13353ccda647Slclee 	cmlb_label_t			newlabeltype;
133606bbe1e0Sedp 	boolean_t			internal;
13373ccda647Slclee 
1338e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1339e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
13403ccda647Slclee 
13417f0b8309SEdward Pilatowicz 	internal = VOID2BOOLEAN(
13427f0b8309SEdward Pilatowicz 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
13433ccda647Slclee 
13443ccda647Slclee 	/* check the most common case */
1345e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1346e8fb11a1Sshidokht 	    cl->cl_last_labeltype == cl->cl_cur_labeltype) {
13473ccda647Slclee 		/* do nothing */
13483ccda647Slclee 		return (0);
13493ccda647Slclee 	}
13503ccda647Slclee 
1351e8fb11a1Sshidokht 	if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
13523ccda647Slclee 		/* we should never get here */
13533ccda647Slclee 		return (ENXIO);
13543ccda647Slclee 	}
13553ccda647Slclee 
1356e8fb11a1Sshidokht 	if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
13573ccda647Slclee 		/* first time during attach */
1358e8fb11a1Sshidokht 		newlabeltype = cl->cl_def_labeltype;
13593ccda647Slclee 
1360e8fb11a1Sshidokht 		instance = ddi_get_instance(CMLB_DEVINFO(cl));
13613ccda647Slclee 
13623ccda647Slclee 		/* Create all the minor nodes for this target. */
13633ccda647Slclee 		dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
13643ccda647Slclee 		    dk_minor_data;
13653ccda647Slclee 		while (dmdp->name != NULL) {
13663ccda647Slclee 
13673ccda647Slclee 			(void) sprintf(name, "%s", dmdp->name);
13683ccda647Slclee 
136906bbe1e0Sedp 			if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
13703ccda647Slclee 			    dmdp->type,
13713ccda647Slclee 			    (instance << CMLBUNIT_SHIFT) | dmdp->minor,
137206bbe1e0Sedp 			    cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
13733ccda647Slclee 				/*
13743ccda647Slclee 				 * Clean up any nodes that may have been
13753ccda647Slclee 				 * created, in case this fails in the middle
13763ccda647Slclee 				 * of the loop.
13773ccda647Slclee 				 */
1378e8fb11a1Sshidokht 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
13793ccda647Slclee 				return (ENXIO);
13803ccda647Slclee 			}
13813ccda647Slclee 			dmdp++;
13823ccda647Slclee 		}
1383e8fb11a1Sshidokht 		cl->cl_last_labeltype = newlabeltype;
13843ccda647Slclee 		return (0);
13853ccda647Slclee 	}
13863ccda647Slclee 
13873ccda647Slclee 	/* Not first time  */
1388e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1389e8fb11a1Sshidokht 		if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
13903ccda647Slclee 			/* close time, revert to default. */
1391e8fb11a1Sshidokht 			newlabeltype = cl->cl_def_labeltype;
13923ccda647Slclee 		} else {
13933ccda647Slclee 			/*
13943ccda647Slclee 			 * do nothing since the type for which we last created
13953ccda647Slclee 			 * nodes matches the default
13963ccda647Slclee 			 */
13973ccda647Slclee 			return (0);
13983ccda647Slclee 		}
13993ccda647Slclee 	} else {
1400e8fb11a1Sshidokht 		if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
14013ccda647Slclee 			/* We are not closing, use current label type */
1402e8fb11a1Sshidokht 			newlabeltype = cl->cl_cur_labeltype;
14033ccda647Slclee 		} else {
14043ccda647Slclee 			/*
14053ccda647Slclee 			 * do nothing since the type for which we last created
14063ccda647Slclee 			 * nodes matches the current label type
14073ccda647Slclee 			 */
14083ccda647Slclee 			return (0);
14093ccda647Slclee 		}
14103ccda647Slclee 	}
14113ccda647Slclee 
1412e8fb11a1Sshidokht 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
14133ccda647Slclee 
14143ccda647Slclee 	/*
14153ccda647Slclee 	 * Currently we only fix up the s7 node when we are switching
14163ccda647Slclee 	 * label types from or to EFI. This is consistent with
14173ccda647Slclee 	 * current behavior of sd.
14183ccda647Slclee 	 */
14193ccda647Slclee 	if (newlabeltype == CMLB_LABEL_EFI &&
1420e8fb11a1Sshidokht 	    cl->cl_last_labeltype != CMLB_LABEL_EFI) {
14213ccda647Slclee 		/* from vtoc to EFI */
1422e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1423e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
142406bbe1e0Sedp 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
14253ccda647Slclee 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
142606bbe1e0Sedp 		    cl->cl_node_type, NULL, internal);
142706bbe1e0Sedp 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
14283ccda647Slclee 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
142906bbe1e0Sedp 		    cl->cl_node_type, NULL, internal);
14303ccda647Slclee 	} else {
14313ccda647Slclee 		/* from efi to vtoc */
1432e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1433e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
143406bbe1e0Sedp 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
14353ccda647Slclee 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
143606bbe1e0Sedp 		    cl->cl_node_type, NULL, internal);
143706bbe1e0Sedp 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
14383ccda647Slclee 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
143906bbe1e0Sedp 		    cl->cl_node_type, NULL, internal);
14403ccda647Slclee 	}
14413ccda647Slclee 
1442e8fb11a1Sshidokht 	cl->cl_last_labeltype = newlabeltype;
14433ccda647Slclee 	return (0);
14443ccda647Slclee }
14453ccda647Slclee 
14463ccda647Slclee /*
14473ccda647Slclee  *    Function: cmlb_validate_geometry
14483ccda647Slclee  *
14493ccda647Slclee  * Description: Read the label from the disk (if present). Update the unit's
14503ccda647Slclee  *		geometry and vtoc information from the data in the label.
14513ccda647Slclee  *		Verify that the label is valid.
14523ccda647Slclee  *
1453e8fb11a1Sshidokht  *   Arguments:
1454e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
1455e8fb11a1Sshidokht  *
1456e8fb11a1Sshidokht  *	forcerevalid	force revalidation even if we are already valid.
1457e8fb11a1Sshidokht  *	flags		operation flags from target driver. Used for verbosity
1458e8fb11a1Sshidokht  *			control	at this time.
1459e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1460e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
14613ccda647Slclee  *
14623ccda647Slclee  * Return Code: 0 - Successful completion
1463e8fb11a1Sshidokht  *		EINVAL  - Invalid value in cl->cl_tgt_blocksize or
1464e8fb11a1Sshidokht  *			  cl->cl_blockcount; or label on disk is corrupted
14653ccda647Slclee  *			  or unreadable.
14663ccda647Slclee  *		EACCES  - Reservation conflict at the device.
14673ccda647Slclee  *		ENOMEM  - Resource allocation error
14683ccda647Slclee  *		ENOTSUP - geometry not applicable
14693ccda647Slclee  *
14703ccda647Slclee  *     Context: Kernel thread only (can sleep).
14713ccda647Slclee  */
14723ccda647Slclee static int
14737f0b8309SEdward Pilatowicz cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1474e8fb11a1Sshidokht     void *tg_cookie)
14753ccda647Slclee {
14763ccda647Slclee 	int		label_error = 0;
14773ccda647Slclee 	diskaddr_t	capacity;
14783ccda647Slclee 	int		count;
14793ccda647Slclee 
1480e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
14817f0b8309SEdward Pilatowicz 	ASSERT(VALID_BOOLEAN(forcerevalid));
14823ccda647Slclee 
14837f0b8309SEdward Pilatowicz 	if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1484e8fb11a1Sshidokht 		if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
14853ccda647Slclee 			return (ENOTSUP);
14863ccda647Slclee 		return (0);
14873ccda647Slclee 	}
14883ccda647Slclee 
1489e8fb11a1Sshidokht 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
14903ccda647Slclee 		return (EIO);
14913ccda647Slclee 
1492e8fb11a1Sshidokht 	capacity = cl->cl_blockcount;
14933ccda647Slclee 
14943ccda647Slclee #if defined(_SUNOS_VTOC_16)
14953ccda647Slclee 	/*
14963ccda647Slclee 	 * Set up the "whole disk" fdisk partition; this should always
14973ccda647Slclee 	 * exist, regardless of whether the disk contains an fdisk table
14983ccda647Slclee 	 * or vtoc.
14993ccda647Slclee 	 */
1500e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1501342440ecSPrasad Singamsetty 	cl->cl_offset[P0_RAW_DISK] = 0;
15023ccda647Slclee 	/*
1503342440ecSPrasad Singamsetty 	 * note if capacity > int32_max(1TB) we are in 64bit environment
1504342440ecSPrasad Singamsetty 	 * so no truncation happens
15053ccda647Slclee 	 */
1506e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_nblk  = capacity;
15073ccda647Slclee #endif
15083ccda647Slclee 	/*
15093ccda647Slclee 	 * Refresh the logical and physical geometry caches.
15103ccda647Slclee 	 * (data from MODE SENSE format/rigid disk geometry pages,
15113ccda647Slclee 	 * and scsi_ifgetcap("geometry").
15123ccda647Slclee 	 */
1513e8fb11a1Sshidokht 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
15143ccda647Slclee 
1515342440ecSPrasad Singamsetty 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1516e8fb11a1Sshidokht 	label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
15173ccda647Slclee 	if (label_error == 0) {
15183ccda647Slclee 
15193ccda647Slclee 		/* found a valid EFI label */
1520e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl,
15213ccda647Slclee 		    "cmlb_validate_geometry: found EFI label\n");
15223ccda647Slclee 		/*
15233ccda647Slclee 		 * solaris_size and geometry_is_valid are set in
15243ccda647Slclee 		 * cmlb_use_efi
15253ccda647Slclee 		 */
15263ccda647Slclee 		return (ENOTSUP);
15273ccda647Slclee 	}
15283ccda647Slclee 
15293ccda647Slclee 	/* NO EFI label found */
15303ccda647Slclee 
1531342440ecSPrasad Singamsetty 	if (capacity > CMLB_EXTVTOC_LIMIT) {
15323ccda647Slclee 		if (label_error == ESRCH) {
15333ccda647Slclee 			/*
1534342440ecSPrasad Singamsetty 			 * they've configured a LUN over 2TB, but used
15353ccda647Slclee 			 * format.dat to restrict format's view of the
1536342440ecSPrasad Singamsetty 			 * capacity to be under 2TB in some earlier Solaris
1537342440ecSPrasad Singamsetty 			 * release.
15383ccda647Slclee 			 */
1539342440ecSPrasad Singamsetty 			/* i.e > 2TB with a VTOC < 2TB */
1540342440ecSPrasad Singamsetty 			if (!(flags & CMLB_SILENT) &&
1541342440ecSPrasad Singamsetty 			    (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
15423ccda647Slclee 
1543e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1544342440ecSPrasad Singamsetty 				    CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1545342440ecSPrasad Singamsetty 				    "due to VTOC label. To use the full "
1546342440ecSPrasad Singamsetty 				    "capacity of the disk, use format(1M) to "
1547342440ecSPrasad Singamsetty 				    "relabel the disk with EFI/GPT label.\n",
1548342440ecSPrasad Singamsetty 				    CMLB_LABEL(cl),
1549342440ecSPrasad Singamsetty 				    ddi_get_instance(CMLB_DEVINFO(cl)));
1550342440ecSPrasad Singamsetty 
1551342440ecSPrasad Singamsetty 				cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1552e8fb11a1Sshidokht 			}
15533ccda647Slclee 		} else {
15543ccda647Slclee 				return (ENOTSUP);
15553ccda647Slclee 		}
15563ccda647Slclee 	}
15573ccda647Slclee 
15583ccda647Slclee 	label_error = 0;
15593ccda647Slclee 
15603ccda647Slclee 	/*
15613ccda647Slclee 	 * at this point it is either labeled with a VTOC or it is
1562e8fb11a1Sshidokht 	 * under 1TB (<= 1TB actually for off-by-1)
15633ccda647Slclee 	 */
15643ccda647Slclee 
15653ccda647Slclee 	/*
1566e8fb11a1Sshidokht 	 * Only DIRECT ACCESS devices will have Scl labels.
1567e8fb11a1Sshidokht 	 * CD's supposedly have a Scl label, too
15683ccda647Slclee 	 */
1569e8fb11a1Sshidokht 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
15703ccda647Slclee 		struct	dk_label *dkl;
15713ccda647Slclee 		offset_t label_addr;
15723ccda647Slclee 		int	rval;
15733ccda647Slclee 		size_t	buffer_size;
15743ccda647Slclee 
15753ccda647Slclee 		/*
1576e8fb11a1Sshidokht 		 * Note: This will set up cl->cl_solaris_size and
1577e8fb11a1Sshidokht 		 * cl->cl_solaris_offset.
15783ccda647Slclee 		 */
1579e8fb11a1Sshidokht 		rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1580e8fb11a1Sshidokht 		if ((rval != 0) && !ISCD(cl)) {
1581e8fb11a1Sshidokht 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
15823ccda647Slclee 			return (rval);
15833ccda647Slclee 		}
15843ccda647Slclee 
1585e8fb11a1Sshidokht 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
15863ccda647Slclee 			/*
15873ccda647Slclee 			 * Found fdisk table but no Solaris partition entry,
15883ccda647Slclee 			 * so don't call cmlb_uselabel() and don't create
15893ccda647Slclee 			 * a default label.
15903ccda647Slclee 			 */
15913ccda647Slclee 			label_error = 0;
15927f0b8309SEdward Pilatowicz 			cl->cl_f_geometry_is_valid = B_TRUE;
15933ccda647Slclee 			goto no_solaris_partition;
15943ccda647Slclee 		}
15953ccda647Slclee 
1596e8fb11a1Sshidokht 		label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1597e8fb11a1Sshidokht 
1598*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		buffer_size = cl->cl_sys_blocksize;
15993ccda647Slclee 
1600e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
16013ccda647Slclee 		    "label_addr: 0x%x allocation size: 0x%x\n",
16023ccda647Slclee 		    label_addr, buffer_size);
16033ccda647Slclee 
16043ccda647Slclee 		if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
16053ccda647Slclee 			return (ENOMEM);
16063ccda647Slclee 
1607e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
1608e8fb11a1Sshidokht 		rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1609e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
16103ccda647Slclee 
16113ccda647Slclee 		switch (rval) {
16123ccda647Slclee 		case 0:
16133ccda647Slclee 			/*
16143ccda647Slclee 			 * cmlb_uselabel will establish that the geometry
16153ccda647Slclee 			 * is valid.
16163ccda647Slclee 			 */
1617e8fb11a1Sshidokht 			if (cmlb_uselabel(cl,
1618e8fb11a1Sshidokht 			    (struct dk_label *)(uintptr_t)dkl, flags) !=
16193ccda647Slclee 			    CMLB_LABEL_IS_VALID) {
16203ccda647Slclee 				label_error = EINVAL;
16213ccda647Slclee 			} else
1622342440ecSPrasad Singamsetty 				cl->cl_label_from_media = CMLB_LABEL_VTOC;
16233ccda647Slclee 			break;
16243ccda647Slclee 		case EACCES:
16253ccda647Slclee 			label_error = EACCES;
16263ccda647Slclee 			break;
16273ccda647Slclee 		default:
16283ccda647Slclee 			label_error = EINVAL;
16293ccda647Slclee 			break;
16303ccda647Slclee 		}
16313ccda647Slclee 
16323ccda647Slclee 		kmem_free(dkl, buffer_size);
16333ccda647Slclee 	}
16343ccda647Slclee 
16353ccda647Slclee 	/*
16363ccda647Slclee 	 * If a valid label was not found, AND if no reservation conflict
16373ccda647Slclee 	 * was detected, then go ahead and create a default label (4069506).
16383ccda647Slclee 	 *
16393ccda647Slclee 	 * Note: currently, for VTOC_8 devices, the default label is created
1640e8fb11a1Sshidokht 	 * for removables and hotpluggables only.  For VTOC_16 devices, the
1641e8fb11a1Sshidokht 	 * default label will be created for all devices.
16423ccda647Slclee 	 * (see cmlb_build_default_label)
16433ccda647Slclee 	 */
16443ccda647Slclee #if defined(_SUNOS_VTOC_8)
1645e8fb11a1Sshidokht 	if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1646e8fb11a1Sshidokht 	    (label_error != EACCES)) {
16473ccda647Slclee #elif defined(_SUNOS_VTOC_16)
16483ccda647Slclee 	if (label_error != EACCES) {
16493ccda647Slclee #endif
16507f0b8309SEdward Pilatowicz 		if (!cl->cl_f_geometry_is_valid) {
1651e8fb11a1Sshidokht 			cmlb_build_default_label(cl, tg_cookie);
16523ccda647Slclee 		}
16533ccda647Slclee 		label_error = 0;
16543ccda647Slclee 	}
16553ccda647Slclee 
16563ccda647Slclee no_solaris_partition:
16573ccda647Slclee 
16583ccda647Slclee #if defined(_SUNOS_VTOC_16)
16593ccda647Slclee 	/*
16603ccda647Slclee 	 * If we have valid geometry, set up the remaining fdisk partitions.
16613ccda647Slclee 	 * Note that dkl_cylno is not used for the fdisk map entries, so
16623ccda647Slclee 	 * we set it to an entirely bogus value.
16633ccda647Slclee 	 */
1664f85c7842SSuhasini Peddada 	for (count = 0; count < FD_NUMPART; count++) {
1665342440ecSPrasad Singamsetty 		cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1666e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
1667e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_nblk;
16683ccda647Slclee 
1669e8fb11a1Sshidokht 		cl->cl_offset[FDISK_P1 + count] =
1670e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_start;
16713ccda647Slclee 	}
16723ccda647Slclee #endif
16733ccda647Slclee 
16743ccda647Slclee 	for (count = 0; count < NDKMAP; count++) {
16753ccda647Slclee #if defined(_SUNOS_VTOC_8)
1676e8fb11a1Sshidokht 		struct dk_map *lp  = &cl->cl_map[count];
1677e8fb11a1Sshidokht 		cl->cl_offset[count] =
1678e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
16793ccda647Slclee #elif defined(_SUNOS_VTOC_16)
1680e8fb11a1Sshidokht 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
16813ccda647Slclee 
1682e8fb11a1Sshidokht 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
16833ccda647Slclee #else
16843ccda647Slclee #error "No VTOC format defined."
16853ccda647Slclee #endif
16863ccda647Slclee 	}
16873ccda647Slclee 
16883ccda647Slclee 	return (label_error);
16893ccda647Slclee }
16903ccda647Slclee 
16913ccda647Slclee #if defined(_SUNOS_VTOC_16)
16923ccda647Slclee /*
16933ccda647Slclee  *    Function: cmlb_convert_geometry
16943ccda647Slclee  *
16953ccda647Slclee  * Description: Convert physical geometry into a dk_geom structure. In
16963ccda647Slclee  *		other words, make sure we don't wrap 16-bit values.
16973ccda647Slclee  *		e.g. converting from geom_cache to dk_geom
16983ccda647Slclee  *
16993ccda647Slclee  *     Context: Kernel thread only
17003ccda647Slclee  */
17013ccda647Slclee static void
1702e8fb11a1Sshidokht cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g)
17033ccda647Slclee {
17043ccda647Slclee 
17053ccda647Slclee 	/* Unlabeled SCSI floppy device */
17063ccda647Slclee 	if (capacity <= 0x1000) {
1707e8fb11a1Sshidokht 		cl_g->dkg_nhead = 2;
1708e8fb11a1Sshidokht 		cl_g->dkg_ncyl = 80;
1709e8fb11a1Sshidokht 		cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
17103ccda647Slclee 		return;
17113ccda647Slclee 	}
17123ccda647Slclee 
17133ccda647Slclee 	/*
1714342440ecSPrasad Singamsetty 	 * For all devices we calculate cylinders using the heads and sectors
1715342440ecSPrasad Singamsetty 	 * we assign based on capacity of the device.  The algorithm is
1716342440ecSPrasad Singamsetty 	 * designed to be compatible with the way other operating systems
1717342440ecSPrasad Singamsetty 	 * lay out fdisk tables for X86 and to insure that the cylinders never
1718342440ecSPrasad Singamsetty 	 * exceed 65535 to prevent problems with X86 ioctls that report
1719342440ecSPrasad Singamsetty 	 * geometry.
1720342440ecSPrasad Singamsetty 	 * For some smaller disk sizes we report geometry that matches those
1721342440ecSPrasad Singamsetty 	 * used by X86 BIOS usage. For larger disks, we use SPT that are
1722342440ecSPrasad Singamsetty 	 * multiples of 63, since other OSes that are not limited to 16-bits
1723342440ecSPrasad Singamsetty 	 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
17243ccda647Slclee 	 *
1725342440ecSPrasad Singamsetty 	 * The following table (in order) illustrates some end result
1726342440ecSPrasad Singamsetty 	 * calculations:
1727342440ecSPrasad Singamsetty 	 *
1728342440ecSPrasad Singamsetty 	 * Maximum number of blocks 		nhead	nsect
1729342440ecSPrasad Singamsetty 	 *
1730342440ecSPrasad Singamsetty 	 * 2097152 (1GB)			64	32
1731342440ecSPrasad Singamsetty 	 * 16777216 (8GB)			128	32
1732342440ecSPrasad Singamsetty 	 * 1052819775 (502.02GB)		255  	63
1733342440ecSPrasad Singamsetty 	 * 2105639550 (0.98TB)			255	126
1734342440ecSPrasad Singamsetty 	 * 3158459325 (1.47TB)			255  	189
1735342440ecSPrasad Singamsetty 	 * 4211279100 (1.96TB)			255  	252
1736342440ecSPrasad Singamsetty 	 * 5264098875 (2.45TB)			255  	315
1737342440ecSPrasad Singamsetty 	 * ...
17383ccda647Slclee 	 */
17393ccda647Slclee 
1740342440ecSPrasad Singamsetty 	if (capacity <= 0x200000) {
1741342440ecSPrasad Singamsetty 		cl_g->dkg_nhead = 64;
1742342440ecSPrasad Singamsetty 		cl_g->dkg_nsect = 32;
1743342440ecSPrasad Singamsetty 	} else if (capacity <= 0x01000000) {
1744342440ecSPrasad Singamsetty 		cl_g->dkg_nhead = 128;
1745342440ecSPrasad Singamsetty 		cl_g->dkg_nsect = 32;
1746342440ecSPrasad Singamsetty 	} else {
1747342440ecSPrasad Singamsetty 		cl_g->dkg_nhead = 255;
1748342440ecSPrasad Singamsetty 
1749342440ecSPrasad Singamsetty 		/* make nsect be smallest multiple of 63 */
1750342440ecSPrasad Singamsetty 		cl_g->dkg_nsect = ((capacity +
1751342440ecSPrasad Singamsetty 		    (UINT16_MAX * 255 * 63) - 1) /
1752342440ecSPrasad Singamsetty 		    (UINT16_MAX * 255 * 63)) * 63;
1753342440ecSPrasad Singamsetty 
1754342440ecSPrasad Singamsetty 		if (cl_g->dkg_nsect == 0)
1755342440ecSPrasad Singamsetty 			cl_g->dkg_nsect = (UINT16_MAX / 63) * 63;
1756342440ecSPrasad Singamsetty 	}
1757342440ecSPrasad Singamsetty 
17583ccda647Slclee }
17593ccda647Slclee #endif
17603ccda647Slclee 
17613ccda647Slclee /*
17623ccda647Slclee  *    Function: cmlb_resync_geom_caches
17633ccda647Slclee  *
17643ccda647Slclee  * Description: (Re)initialize both geometry caches: the virtual geometry
17653ccda647Slclee  *            information is extracted from the HBA (the "geometry"
17663ccda647Slclee  *            capability), and the physical geometry cache data is
17673ccda647Slclee  *            generated by issuing MODE SENSE commands.
17683ccda647Slclee  *
1769e8fb11a1Sshidokht  *   Arguments:
1770e8fb11a1Sshidokht  *	cl 		driver soft state (unit) structure
1771e8fb11a1Sshidokht  *	capacity	disk capacity in #blocks
1772e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1773e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
17743ccda647Slclee  *
17753ccda647Slclee  *     Context: Kernel thread only (can sleep).
17763ccda647Slclee  */
17773ccda647Slclee static void
1778e8fb11a1Sshidokht cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1779e8fb11a1Sshidokht     void *tg_cookie)
17803ccda647Slclee {
17813ccda647Slclee 	struct	cmlb_geom 	pgeom;
17823ccda647Slclee 	struct	cmlb_geom	lgeom;
17833ccda647Slclee 	struct 	cmlb_geom	*pgeomp = &pgeom;
17843ccda647Slclee 	unsigned short 		nhead;
17853ccda647Slclee 	unsigned short 		nsect;
17863ccda647Slclee 	int 			spc;
17873ccda647Slclee 	int			ret;
17883ccda647Slclee 
1789e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1790e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
17913ccda647Slclee 
17923ccda647Slclee 	/*
17933ccda647Slclee 	 * Ask the controller for its logical geometry.
17943ccda647Slclee 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
17953ccda647Slclee 	 * then the lgeom cache will be invalid.
17963ccda647Slclee 	 */
1797e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
17983ccda647Slclee 	bzero(&lgeom, sizeof (struct cmlb_geom));
1799e8fb11a1Sshidokht 	ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1800e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
18013ccda647Slclee 
1802e8fb11a1Sshidokht 	bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
18033ccda647Slclee 
18043ccda647Slclee 	/*
18053ccda647Slclee 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
18063ccda647Slclee 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
18073ccda647Slclee 	 */
1808e8fb11a1Sshidokht 	if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1809e8fb11a1Sshidokht 	    cl->cl_lgeom.g_nhead == 0) {
18103ccda647Slclee 		/*
18113ccda647Slclee 		 * Note: Perhaps this needs to be more adaptive? The rationale
18123ccda647Slclee 		 * is that, if there's no HBA geometry from the HBA driver, any
18133ccda647Slclee 		 * guess is good, since this is the physical geometry. If MODE
18143ccda647Slclee 		 * SENSE fails this gives a max cylinder size for non-LBA access
18153ccda647Slclee 		 */
18163ccda647Slclee 		nhead = 255;
18173ccda647Slclee 		nsect = 63;
18183ccda647Slclee 	} else {
1819e8fb11a1Sshidokht 		nhead = cl->cl_lgeom.g_nhead;
1820e8fb11a1Sshidokht 		nsect = cl->cl_lgeom.g_nsect;
18213ccda647Slclee 	}
18223ccda647Slclee 
1823e8fb11a1Sshidokht 	if (ISCD(cl)) {
18243ccda647Slclee 		pgeomp->g_nhead = 1;
18253ccda647Slclee 		pgeomp->g_nsect = nsect * nhead;
18263ccda647Slclee 	} else {
18273ccda647Slclee 		pgeomp->g_nhead = nhead;
18283ccda647Slclee 		pgeomp->g_nsect = nsect;
18293ccda647Slclee 	}
18303ccda647Slclee 
18313ccda647Slclee 	spc = pgeomp->g_nhead * pgeomp->g_nsect;
18323ccda647Slclee 	pgeomp->g_capacity = capacity;
18337501b6d5SSheshadri Vasudevan 	if (spc == 0)
18347501b6d5SSheshadri Vasudevan 		pgeomp->g_ncyl = 0;
18357501b6d5SSheshadri Vasudevan 	else
18363ccda647Slclee 		pgeomp->g_ncyl = pgeomp->g_capacity / spc;
18373ccda647Slclee 	pgeomp->g_acyl = 0;
18383ccda647Slclee 
18393ccda647Slclee 	/*
18403ccda647Slclee 	 * Retrieve fresh geometry data from the hardware, stash it
18413ccda647Slclee 	 * here temporarily before we rebuild the incore label.
18423ccda647Slclee 	 *
18433ccda647Slclee 	 * We want to use the MODE SENSE commands to derive the
18443ccda647Slclee 	 * physical geometry of the device, but if either command
18453ccda647Slclee 	 * fails, the logical geometry is used as the fallback for
18463ccda647Slclee 	 * disk label geometry.
18473ccda647Slclee 	 */
18483ccda647Slclee 
1849e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
1850e8fb11a1Sshidokht 	(void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
1851e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
18523ccda647Slclee 
18533ccda647Slclee 	/*
18543ccda647Slclee 	 * Now update the real copy while holding the mutex. This
18553ccda647Slclee 	 * way the global copy is never in an inconsistent state.
18563ccda647Slclee 	 */
1857e8fb11a1Sshidokht 	bcopy(pgeomp, &cl->cl_pgeom,  sizeof (cl->cl_pgeom));
18583ccda647Slclee 
1859e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
18603ccda647Slclee 	    "(cached from lgeom)\n");
1861e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
18623ccda647Slclee 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
1863e8fb11a1Sshidokht 	    cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
1864e8fb11a1Sshidokht 	    cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
1865e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "   lbasize: %d; capacity: %ld; "
1866e8fb11a1Sshidokht 	    "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
1867e8fb11a1Sshidokht 	    cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
1868e8fb11a1Sshidokht 	    cl->cl_pgeom.g_rpm);
18693ccda647Slclee }
18703ccda647Slclee 
18713ccda647Slclee 
18723ccda647Slclee /*
18733ccda647Slclee  *    Function: cmlb_read_fdisk
18743ccda647Slclee  *
18753ccda647Slclee  * Description: utility routine to read the fdisk table.
18763ccda647Slclee  *
1877e8fb11a1Sshidokht  *   Arguments:
1878e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
1879e8fb11a1Sshidokht  *	capacity	disk capacity in #blocks
1880e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1881e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
18823ccda647Slclee  *
18833ccda647Slclee  * Return Code: 0 for success (includes not reading for no_fdisk_present case
18843ccda647Slclee  *		errnos from tg_rw if failed to read the first block.
18853ccda647Slclee  *
18863ccda647Slclee  *     Context: Kernel thread only (can sleep).
18873ccda647Slclee  */
18883ccda647Slclee /*ARGSUSED*/
18893ccda647Slclee static int
1890e8fb11a1Sshidokht cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
18913ccda647Slclee {
18923ccda647Slclee #if defined(_NO_FDISK_PRESENT)
18933ccda647Slclee 
1894e8fb11a1Sshidokht 	cl->cl_solaris_offset = 0;
1895e8fb11a1Sshidokht 	cl->cl_solaris_size = capacity;
1896e8fb11a1Sshidokht 	bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
18973ccda647Slclee 	return (0);
18983ccda647Slclee 
18993ccda647Slclee #elif defined(_FIRMWARE_NEEDS_FDISK)
19003ccda647Slclee 
19013ccda647Slclee 	struct ipart	*fdp;
19023ccda647Slclee 	struct mboot	*mbp;
19033ccda647Slclee 	struct ipart	fdisk[FD_NUMPART];
1904f85c7842SSuhasini Peddada 	int		i;
19053ccda647Slclee 	char		sigbuf[2];
19063ccda647Slclee 	caddr_t		bufp;
19073ccda647Slclee 	int		uidx;
19083ccda647Slclee 	int 		rval;
19093ccda647Slclee 	int		lba = 0;
19103ccda647Slclee 	uint_t		solaris_offset;	/* offset to solaris part. */
19113ccda647Slclee 	daddr_t		solaris_size;	/* size of solaris partition */
19123ccda647Slclee 	uint32_t	blocksize;
19133ccda647Slclee 
1914e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1915e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
19163ccda647Slclee 
19173ccda647Slclee 	/*
19183ccda647Slclee 	 * Start off assuming no fdisk table
19193ccda647Slclee 	 */
19203ccda647Slclee 	solaris_offset = 0;
19213ccda647Slclee 	solaris_size   = capacity;
19223ccda647Slclee 
1923e8fb11a1Sshidokht 	blocksize = cl->cl_tgt_blocksize;
19243ccda647Slclee 
19253ccda647Slclee 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
19263ccda647Slclee 
1927e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
1928e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
1929e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
19303ccda647Slclee 
19313ccda647Slclee 	if (rval != 0) {
1932e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
19333ccda647Slclee 		    "cmlb_read_fdisk: fdisk read err\n");
1934e8fb11a1Sshidokht 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
1935e8fb11a1Sshidokht 		goto done;
19363ccda647Slclee 	}
19373ccda647Slclee 
19383ccda647Slclee 	mbp = (struct mboot *)bufp;
19393ccda647Slclee 
19403ccda647Slclee 	/*
19413ccda647Slclee 	 * The fdisk table does not begin on a 4-byte boundary within the
19423ccda647Slclee 	 * master boot record, so we copy it to an aligned structure to avoid
19433ccda647Slclee 	 * alignment exceptions on some processors.
19443ccda647Slclee 	 */
19453ccda647Slclee 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
19463ccda647Slclee 
19473ccda647Slclee 	/*
19483ccda647Slclee 	 * Check for lba support before verifying sig; sig might not be
19493ccda647Slclee 	 * there, say on a blank disk, but the max_chs mark may still
19503ccda647Slclee 	 * be present.
19513ccda647Slclee 	 *
19523ccda647Slclee 	 * Note: LBA support and BEFs are an x86-only concept but this
19533ccda647Slclee 	 * code should work OK on SPARC as well.
19543ccda647Slclee 	 */
19553ccda647Slclee 
19563ccda647Slclee 	/*
19573ccda647Slclee 	 * First, check for lba-access-ok on root node (or prom root node)
19583ccda647Slclee 	 * if present there, don't need to search fdisk table.
19593ccda647Slclee 	 */
19603ccda647Slclee 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
19613ccda647Slclee 	    "lba-access-ok", 0) != 0) {
19623ccda647Slclee 		/* All drives do LBA; don't search fdisk table */
19633ccda647Slclee 		lba = 1;
19643ccda647Slclee 	} else {
19653ccda647Slclee 		/* Okay, look for mark in fdisk table */
19663ccda647Slclee 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
19673ccda647Slclee 			/* accumulate "lba" value from all partitions */
19683ccda647Slclee 			lba = (lba || cmlb_has_max_chs_vals(fdp));
19693ccda647Slclee 		}
19703ccda647Slclee 	}
19713ccda647Slclee 
19723ccda647Slclee 	if (lba != 0) {
1973e8fb11a1Sshidokht 		dev_t dev = cmlb_make_device(cl);
19743ccda647Slclee 
1975e8fb11a1Sshidokht 		if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
19763ccda647Slclee 		    "lba-access-ok", 0) == 0) {
19773ccda647Slclee 			/* not found; create it */
1978e8fb11a1Sshidokht 			if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
19793ccda647Slclee 			    "lba-access-ok", (caddr_t)NULL, 0) !=
19803ccda647Slclee 			    DDI_PROP_SUCCESS) {
1981e8fb11a1Sshidokht 				cmlb_dbg(CMLB_ERROR,  cl,
19823ccda647Slclee 				    "cmlb_read_fdisk: Can't create lba "
19833ccda647Slclee 				    "property for instance %d\n",
1984e8fb11a1Sshidokht 				    ddi_get_instance(CMLB_DEVINFO(cl)));
19853ccda647Slclee 			}
19863ccda647Slclee 		}
19873ccda647Slclee 	}
19883ccda647Slclee 
19893ccda647Slclee 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
19903ccda647Slclee 
19913ccda647Slclee 	/*
19923ccda647Slclee 	 * Endian-independent signature check
19933ccda647Slclee 	 */
19943ccda647Slclee 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
19953ccda647Slclee 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
1996e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
19973ccda647Slclee 		    "cmlb_read_fdisk: no fdisk\n");
1998e8fb11a1Sshidokht 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
19993ccda647Slclee 		goto done;
20003ccda647Slclee 	}
20013ccda647Slclee 
20023ccda647Slclee #ifdef CMLBDEBUG
2003e8fb11a1Sshidokht 	if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
20043ccda647Slclee 		fdp = fdisk;
2005e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "cmlb_read_fdisk:\n");
2006e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "         relsect    "
20073ccda647Slclee 		    "numsect         sysid       bootid\n");
20083ccda647Slclee 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
2009e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
20103ccda647Slclee 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
20113ccda647Slclee 			    i, fdp->relsect, fdp->numsect,
20123ccda647Slclee 			    fdp->systid, fdp->bootid);
20133ccda647Slclee 		}
20143ccda647Slclee 	}
20153ccda647Slclee #endif
20163ccda647Slclee 
20173ccda647Slclee 	/*
20183ccda647Slclee 	 * Try to find the unix partition
20193ccda647Slclee 	 */
20203ccda647Slclee 	uidx = -1;
20213ccda647Slclee 	solaris_offset = 0;
20223ccda647Slclee 	solaris_size   = 0;
20233ccda647Slclee 
20243ccda647Slclee 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2025342440ecSPrasad Singamsetty 		uint32_t relsect;
2026342440ecSPrasad Singamsetty 		uint32_t numsect;
20273ccda647Slclee 
20283ccda647Slclee 		if (fdp->numsect == 0) {
2029e8fb11a1Sshidokht 			cl->cl_fmap[i].fmap_start = 0;
2030e8fb11a1Sshidokht 			cl->cl_fmap[i].fmap_nblk  = 0;
20313ccda647Slclee 			continue;
20323ccda647Slclee 		}
20333ccda647Slclee 
20343ccda647Slclee 		/*
20353ccda647Slclee 		 * Data in the fdisk table is little-endian.
20363ccda647Slclee 		 */
20373ccda647Slclee 		relsect = LE_32(fdp->relsect);
20383ccda647Slclee 		numsect = LE_32(fdp->numsect);
20393ccda647Slclee 
2040e8fb11a1Sshidokht 		cl->cl_fmap[i].fmap_start = relsect;
2041e8fb11a1Sshidokht 		cl->cl_fmap[i].fmap_nblk  = numsect;
20423ccda647Slclee 
20433ccda647Slclee 		if (fdp->systid != SUNIXOS &&
20443ccda647Slclee 		    fdp->systid != SUNIXOS2 &&
20453ccda647Slclee 		    fdp->systid != EFI_PMBR) {
20463ccda647Slclee 			continue;
20473ccda647Slclee 		}
20483ccda647Slclee 
20493ccda647Slclee 		/*
20503ccda647Slclee 		 * use the last active solaris partition id found
20513ccda647Slclee 		 * (there should only be 1 active partition id)
20523ccda647Slclee 		 *
20533ccda647Slclee 		 * if there are no active solaris partition id
20543ccda647Slclee 		 * then use the first inactive solaris partition id
20553ccda647Slclee 		 */
20563ccda647Slclee 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
20573ccda647Slclee 			uidx = i;
20583ccda647Slclee 			solaris_offset = relsect;
20593ccda647Slclee 			solaris_size   = numsect;
20603ccda647Slclee 		}
20613ccda647Slclee 	}
2062f85c7842SSuhasini Peddada 
2063e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "fdisk 0x%x 0x%lx",
2064e8fb11a1Sshidokht 	    cl->cl_solaris_offset, cl->cl_solaris_size);
20653ccda647Slclee done:
20663ccda647Slclee 
20673ccda647Slclee 	/*
20683ccda647Slclee 	 * Clear the VTOC info, only if the Solaris partition entry
20693ccda647Slclee 	 * has moved, changed size, been deleted, or if the size of
20703ccda647Slclee 	 * the partition is too small to even fit the label sector.
20713ccda647Slclee 	 */
2072e8fb11a1Sshidokht 	if ((cl->cl_solaris_offset != solaris_offset) ||
2073e8fb11a1Sshidokht 	    (cl->cl_solaris_size != solaris_size) ||
20743ccda647Slclee 	    solaris_size <= DK_LABEL_LOC) {
2075e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "fdisk moved 0x%x 0x%lx",
20763ccda647Slclee 		    solaris_offset, solaris_size);
2077e8fb11a1Sshidokht 		bzero(&cl->cl_g, sizeof (struct dk_geom));
2078e8fb11a1Sshidokht 		bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2079e8fb11a1Sshidokht 		bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
20807f0b8309SEdward Pilatowicz 		cl->cl_f_geometry_is_valid = B_FALSE;
20813ccda647Slclee 	}
2082e8fb11a1Sshidokht 	cl->cl_solaris_offset = solaris_offset;
2083e8fb11a1Sshidokht 	cl->cl_solaris_size = solaris_size;
20843ccda647Slclee 	kmem_free(bufp, blocksize);
20853ccda647Slclee 	return (rval);
20863ccda647Slclee 
20873ccda647Slclee #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
20883ccda647Slclee #error "fdisk table presence undetermined for this platform."
20893ccda647Slclee #endif	/* #if defined(_NO_FDISK_PRESENT) */
20903ccda647Slclee }
20913ccda647Slclee 
20923ccda647Slclee static void
20933ccda647Slclee cmlb_swap_efi_gpt(efi_gpt_t *e)
20943ccda647Slclee {
20953ccda647Slclee 	_NOTE(ASSUMING_PROTECTED(*e))
20963ccda647Slclee 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
20973ccda647Slclee 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
20983ccda647Slclee 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
20993ccda647Slclee 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
21003ccda647Slclee 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
21013ccda647Slclee 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
21023ccda647Slclee 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
21033ccda647Slclee 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
21043ccda647Slclee 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
21053ccda647Slclee 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
21063ccda647Slclee 	e->efi_gpt_NumberOfPartitionEntries =
21073ccda647Slclee 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
21083ccda647Slclee 	e->efi_gpt_SizeOfPartitionEntry =
21093ccda647Slclee 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
21103ccda647Slclee 	e->efi_gpt_PartitionEntryArrayCRC32 =
21113ccda647Slclee 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
21123ccda647Slclee }
21133ccda647Slclee 
21143ccda647Slclee static void
21153ccda647Slclee cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
21163ccda647Slclee {
21173ccda647Slclee 	int i;
21183ccda647Slclee 
21193ccda647Slclee 	_NOTE(ASSUMING_PROTECTED(*p))
21203ccda647Slclee 	for (i = 0; i < nparts; i++) {
21213ccda647Slclee 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
21223ccda647Slclee 		    p[i].efi_gpe_PartitionTypeGUID);
21233ccda647Slclee 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
21243ccda647Slclee 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
21253ccda647Slclee 		/* PartitionAttrs */
21263ccda647Slclee 	}
21273ccda647Slclee }
21283ccda647Slclee 
21293ccda647Slclee static int
21303ccda647Slclee cmlb_validate_efi(efi_gpt_t *labp)
21313ccda647Slclee {
21323ccda647Slclee 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
21333ccda647Slclee 		return (EINVAL);
21343ccda647Slclee 	/* at least 96 bytes in this version of the spec. */
21353ccda647Slclee 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
21363ccda647Slclee 	    labp->efi_gpt_HeaderSize)
21373ccda647Slclee 		return (EINVAL);
21383ccda647Slclee 	/* this should be 128 bytes */
21393ccda647Slclee 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
21403ccda647Slclee 		return (EINVAL);
21413ccda647Slclee 	return (0);
21423ccda647Slclee }
21433ccda647Slclee 
2144ab92f716Sshidokht /*
21457f0b8309SEdward Pilatowicz  * This function returns B_FALSE if there is a valid MBR signature and no
21467f0b8309SEdward Pilatowicz  * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2147ab92f716Sshidokht  *
2148ab92f716Sshidokht  * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2149ab92f716Sshidokht  * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2150ab92f716Sshidokht  * where a PMBR entry is not the only one. Also, if the first block has been
2151ab92f716Sshidokht  * corrupted, currently best attempt to allow data access would be to try to
2152ab92f716Sshidokht  * check for GPT headers. Hence in case of more than one partition entry, but
2153ab92f716Sshidokht  * at least one EFI_PMBR partition type or no valid magic number, the function
21547f0b8309SEdward Pilatowicz  * returns B_TRUE to continue with looking for GPT header.
2155ab92f716Sshidokht  */
2156ab92f716Sshidokht 
21577f0b8309SEdward Pilatowicz static boolean_t
21587f0b8309SEdward Pilatowicz cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2159ab92f716Sshidokht {
2160ab92f716Sshidokht 	struct ipart	*fdp;
2161ab92f716Sshidokht 	struct mboot	*mbp = (struct mboot *)buf;
2162ab92f716Sshidokht 	struct ipart	fdisk[FD_NUMPART];
2163ab92f716Sshidokht 	int		i;
2164ab92f716Sshidokht 
2165342440ecSPrasad Singamsetty 	if (is_mbr != NULL)
21667f0b8309SEdward Pilatowicz 		*is_mbr = B_TRUE;
2167342440ecSPrasad Singamsetty 
2168342440ecSPrasad Singamsetty 	if (LE_16(mbp->signature) != MBB_MAGIC) {
2169342440ecSPrasad Singamsetty 		if (is_mbr != NULL)
21707f0b8309SEdward Pilatowicz 			*is_mbr = B_FALSE;
21717f0b8309SEdward Pilatowicz 		return (B_TRUE);
2172342440ecSPrasad Singamsetty 	}
2173ab92f716Sshidokht 
2174ab92f716Sshidokht 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2175ab92f716Sshidokht 
2176ab92f716Sshidokht 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2177ab92f716Sshidokht 		if (fdp->systid == EFI_PMBR)
21787f0b8309SEdward Pilatowicz 			return (B_TRUE);
2179ab92f716Sshidokht 	}
2180ab92f716Sshidokht 
21817f0b8309SEdward Pilatowicz 	return (B_FALSE);
2182ab92f716Sshidokht }
2183ab92f716Sshidokht 
21843ccda647Slclee static int
2185e8fb11a1Sshidokht cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2186e8fb11a1Sshidokht     void *tg_cookie)
21873ccda647Slclee {
21883ccda647Slclee 	int		i;
21893ccda647Slclee 	int		rval = 0;
21903ccda647Slclee 	efi_gpe_t	*partitions;
21913ccda647Slclee 	uchar_t		*buf;
21923ccda647Slclee 	uint_t		lbasize;	/* is really how much to read */
2193e8fb11a1Sshidokht 	diskaddr_t	cap = 0;
21943ccda647Slclee 	uint_t		nparts;
21953ccda647Slclee 	diskaddr_t	gpe_lba;
2196af007057Syl194034 	diskaddr_t	alternate_lba;
21971cec2d8cSshidokht 	int		iofailed = 0;
2198e8fb11a1Sshidokht 	struct uuid	uuid_type_reserved = EFI_RESERVED;
2199342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
22007f0b8309SEdward Pilatowicz 	boolean_t 	is_mbr;
2201342440ecSPrasad Singamsetty #endif
22023ccda647Slclee 
2203e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
22043ccda647Slclee 
2205e8fb11a1Sshidokht 	lbasize = cl->cl_sys_blocksize;
2206e8fb11a1Sshidokht 
2207e8fb11a1Sshidokht 	cl->cl_reserved = -1;
2208e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
22093ccda647Slclee 
22103ccda647Slclee 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
22113ccda647Slclee 
2212e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf,  0, lbasize, tg_cookie);
22133ccda647Slclee 	if (rval) {
22141cec2d8cSshidokht 		iofailed = 1;
22153ccda647Slclee 		goto done_err;
22163ccda647Slclee 	}
22173ccda647Slclee 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
22183ccda647Slclee 		/* not ours */
22193ccda647Slclee 		rval = ESRCH;
22203ccda647Slclee 		goto done_err;
22213ccda647Slclee 	}
22223ccda647Slclee 
2223342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
22247f0b8309SEdward Pilatowicz 	if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
22257f0b8309SEdward Pilatowicz 		if (is_mbr)
2226342440ecSPrasad Singamsetty 			rval = ESRCH;
2227342440ecSPrasad Singamsetty 		else
2228ab92f716Sshidokht 			rval = EINVAL;
2229ab92f716Sshidokht 		goto done_err;
2230ab92f716Sshidokht 	}
2231342440ecSPrasad Singamsetty #else
22327f0b8309SEdward Pilatowicz 	if (!cmlb_check_efi_mbr(buf, NULL)) {
2233342440ecSPrasad Singamsetty 		rval = EINVAL;
2234342440ecSPrasad Singamsetty 		goto done_err;
2235342440ecSPrasad Singamsetty 	}
2236342440ecSPrasad Singamsetty 
2237342440ecSPrasad Singamsetty #endif
2238ab92f716Sshidokht 
2239e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
22403ccda647Slclee 	if (rval) {
22411cec2d8cSshidokht 		iofailed = 1;
22423ccda647Slclee 		goto done_err;
22433ccda647Slclee 	}
22443ccda647Slclee 	cmlb_swap_efi_gpt((efi_gpt_t *)buf);
22453ccda647Slclee 
22463ccda647Slclee 	if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
22473ccda647Slclee 		/*
22483ccda647Slclee 		 * Couldn't read the primary, try the backup.  Our
22493ccda647Slclee 		 * capacity at this point could be based on CHS, so
22503ccda647Slclee 		 * check what the device reports.
22513ccda647Slclee 		 */
2252e8fb11a1Sshidokht 		rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
22533ccda647Slclee 		if (rval) {
22541cec2d8cSshidokht 			iofailed = 1;
22553ccda647Slclee 			goto done_err;
22563ccda647Slclee 		}
2257e8fb11a1Sshidokht 
2258e8fb11a1Sshidokht 		/*
2259e8fb11a1Sshidokht 		 * CMLB_OFF_BY_ONE case, we check the next to last block first
2260e8fb11a1Sshidokht 		 * for backup GPT header, otherwise check the last block.
2261e8fb11a1Sshidokht 		 */
2262e8fb11a1Sshidokht 
2263e8fb11a1Sshidokht 		if ((rval = DK_TG_READ(cl, buf,
2264e8fb11a1Sshidokht 		    cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2265e8fb11a1Sshidokht 		    lbasize, tg_cookie))
2266e8fb11a1Sshidokht 		    != 0) {
22671cec2d8cSshidokht 			iofailed = 1;
22683ccda647Slclee 			goto done_err;
22693ccda647Slclee 		}
22703ccda647Slclee 		cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2271e8fb11a1Sshidokht 
2272e8fb11a1Sshidokht 		if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2273e8fb11a1Sshidokht 
2274e8fb11a1Sshidokht 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2275e8fb11a1Sshidokht 				goto done_err;
2276e8fb11a1Sshidokht 			if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2277e8fb11a1Sshidokht 			    tg_cookie)) != 0)
2278e8fb11a1Sshidokht 				goto done_err;
2279e8fb11a1Sshidokht 			cmlb_swap_efi_gpt((efi_gpt_t *)buf);
22803ccda647Slclee 			if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
22813ccda647Slclee 				goto done_err;
2282e8fb11a1Sshidokht 		}
2283e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2284e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
22853ccda647Slclee 			    "primary label corrupt; using backup\n");
22863ccda647Slclee 	}
22873ccda647Slclee 
22883ccda647Slclee 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
22893ccda647Slclee 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2290af007057Syl194034 	alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
22913ccda647Slclee 
2292e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
22933ccda647Slclee 	if (rval) {
22941cec2d8cSshidokht 		iofailed = 1;
22953ccda647Slclee 		goto done_err;
22963ccda647Slclee 	}
22973ccda647Slclee 	partitions = (efi_gpe_t *)buf;
22983ccda647Slclee 
22993ccda647Slclee 	if (nparts > MAXPART) {
23003ccda647Slclee 		nparts = MAXPART;
23013ccda647Slclee 	}
23023ccda647Slclee 	cmlb_swap_efi_gpe(nparts, partitions);
23033ccda647Slclee 
2304e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
23053ccda647Slclee 
23063ccda647Slclee 	/* Fill in partition table. */
23073ccda647Slclee 	for (i = 0; i < nparts; i++) {
23083ccda647Slclee 		if (partitions->efi_gpe_StartingLBA != 0 ||
23093ccda647Slclee 		    partitions->efi_gpe_EndingLBA != 0) {
2310e8fb11a1Sshidokht 			cl->cl_map[i].dkl_cylno =
23113ccda647Slclee 			    partitions->efi_gpe_StartingLBA;
2312e8fb11a1Sshidokht 			cl->cl_map[i].dkl_nblk =
23133ccda647Slclee 			    partitions->efi_gpe_EndingLBA -
23143ccda647Slclee 			    partitions->efi_gpe_StartingLBA + 1;
2315e8fb11a1Sshidokht 			cl->cl_offset[i] =
23163ccda647Slclee 			    partitions->efi_gpe_StartingLBA;
23173ccda647Slclee 		}
2318e8fb11a1Sshidokht 
2319e8fb11a1Sshidokht 		if (cl->cl_reserved == -1) {
2320e8fb11a1Sshidokht 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2321e8fb11a1Sshidokht 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2322e8fb11a1Sshidokht 				cl->cl_reserved = i;
2323e8fb11a1Sshidokht 			}
2324e8fb11a1Sshidokht 		}
23253ccda647Slclee 		if (i == WD_NODE) {
23263ccda647Slclee 			/*
23273ccda647Slclee 			 * minor number 7 corresponds to the whole disk
2328af007057Syl194034 			 * if the disk capacity is expanded after disk is
2329af007057Syl194034 			 * labeled, minor number 7 represents the capacity
2330af007057Syl194034 			 * indicated by the disk label.
23313ccda647Slclee 			 */
2332e8fb11a1Sshidokht 			cl->cl_map[i].dkl_cylno = 0;
2333af007057Syl194034 			if (alternate_lba == 1) {
2334af007057Syl194034 				/*
2335af007057Syl194034 				 * We are using backup label. Since we can
2336af007057Syl194034 				 * find a valid label at the end of disk,
2337af007057Syl194034 				 * the disk capacity is not expanded.
2338af007057Syl194034 				 */
2339e8fb11a1Sshidokht 				cl->cl_map[i].dkl_nblk = capacity;
2340af007057Syl194034 			} else {
2341af007057Syl194034 				cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2342af007057Syl194034 			}
2343e8fb11a1Sshidokht 			cl->cl_offset[i] = 0;
23443ccda647Slclee 		}
23453ccda647Slclee 		partitions++;
23463ccda647Slclee 	}
2347e8fb11a1Sshidokht 	cl->cl_solaris_offset = 0;
2348e8fb11a1Sshidokht 	cl->cl_solaris_size = capacity;
2349342440ecSPrasad Singamsetty 	cl->cl_label_from_media = CMLB_LABEL_EFI;
23507f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_TRUE;
2351e8fb11a1Sshidokht 
2352e8fb11a1Sshidokht 	/* clear the vtoc label */
2353e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2354e8fb11a1Sshidokht 
23553ccda647Slclee 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
23563ccda647Slclee 	return (0);
23573ccda647Slclee 
23583ccda647Slclee done_err:
23593ccda647Slclee 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2360e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
2361e8fb11a1Sshidokht done_err1:
23623ccda647Slclee 	/*
23633ccda647Slclee 	 * if we didn't find something that could look like a VTOC
23643ccda647Slclee 	 * and the disk is over 1TB, we know there isn't a valid label.
23653ccda647Slclee 	 * Otherwise let cmlb_uselabel decide what to do.  We only
23663ccda647Slclee 	 * want to invalidate this if we're certain the label isn't
23673ccda647Slclee 	 * valid because cmlb_prop_op will now fail, which in turn
23683ccda647Slclee 	 * causes things like opens and stats on the partition to fail.
23693ccda647Slclee 	 */
2370342440ecSPrasad Singamsetty 	if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
23717f0b8309SEdward Pilatowicz 		cl->cl_f_geometry_is_valid = B_FALSE;
23723ccda647Slclee 	}
23733ccda647Slclee 	return (rval);
23743ccda647Slclee }
23753ccda647Slclee 
23763ccda647Slclee 
23773ccda647Slclee /*
23783ccda647Slclee  *    Function: cmlb_uselabel
23793ccda647Slclee  *
23803ccda647Slclee  * Description: Validate the disk label and update the relevant data (geometry,
23813ccda647Slclee  *		partition, vtoc, and capacity data) in the cmlb_lun struct.
23823ccda647Slclee  *		Marks the geometry of the unit as being valid.
23833ccda647Slclee  *
2384e8fb11a1Sshidokht  *   Arguments: cl: unit struct.
23853ccda647Slclee  *		dk_label: disk label
23863ccda647Slclee  *
23873ccda647Slclee  * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
23883ccda647Slclee  *		partition, vtoc, and capacity data are good.
23893ccda647Slclee  *
23903ccda647Slclee  *		CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
23913ccda647Slclee  *		label; or computed capacity does not jibe with capacity
23923ccda647Slclee  *		reported from the READ CAPACITY command.
23933ccda647Slclee  *
23943ccda647Slclee  *     Context: Kernel thread only (can sleep).
23953ccda647Slclee  */
23963ccda647Slclee static int
2397e8fb11a1Sshidokht cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
23983ccda647Slclee {
23993ccda647Slclee 	short		*sp;
24003ccda647Slclee 	short		sum;
24013ccda647Slclee 	short		count;
24023ccda647Slclee 	int		label_error = CMLB_LABEL_IS_VALID;
24033ccda647Slclee 	int		i;
24043ccda647Slclee 	diskaddr_t	label_capacity;
2405342440ecSPrasad Singamsetty 	uint32_t	part_end;
24063ccda647Slclee 	diskaddr_t	track_capacity;
24073ccda647Slclee #if defined(_SUNOS_VTOC_16)
24083ccda647Slclee 	struct	dkl_partition	*vpartp;
24093ccda647Slclee #endif
2410e8fb11a1Sshidokht 	ASSERT(cl != NULL);
2411e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
24123ccda647Slclee 
24133ccda647Slclee 	/* Validate the magic number of the label. */
24143ccda647Slclee 	if (labp->dkl_magic != DKL_MAGIC) {
24153ccda647Slclee #if defined(__sparc)
2416e8fb11a1Sshidokht 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2417e8fb11a1Sshidokht 			if (!(flags & CMLB_SILENT))
2418e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2419e8fb11a1Sshidokht 				    CE_WARN,
24203ccda647Slclee 				    "Corrupt label; wrong magic number\n");
24213ccda647Slclee 		}
24223ccda647Slclee #endif
24233ccda647Slclee 		return (CMLB_LABEL_IS_INVALID);
24243ccda647Slclee 	}
24253ccda647Slclee 
24263ccda647Slclee 	/* Validate the checksum of the label. */
24273ccda647Slclee 	sp  = (short *)labp;
24283ccda647Slclee 	sum = 0;
24293ccda647Slclee 	count = sizeof (struct dk_label) / sizeof (short);
24303ccda647Slclee 	while (count--)	 {
24313ccda647Slclee 		sum ^= *sp++;
24323ccda647Slclee 	}
24333ccda647Slclee 
24343ccda647Slclee 	if (sum != 0) {
24353ccda647Slclee #if defined(_SUNOS_VTOC_16)
2436e8fb11a1Sshidokht 		if (!ISCD(cl)) {
24373ccda647Slclee #elif defined(_SUNOS_VTOC_8)
2438e8fb11a1Sshidokht 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
24393ccda647Slclee #endif
2440e8fb11a1Sshidokht 			if (!(flags & CMLB_SILENT))
2441e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2442e8fb11a1Sshidokht 				    CE_WARN,
24433ccda647Slclee 				    "Corrupt label - label checksum failed\n");
24443ccda647Slclee 		}
24453ccda647Slclee 		return (CMLB_LABEL_IS_INVALID);
24463ccda647Slclee 	}
24473ccda647Slclee 
24483ccda647Slclee 
24493ccda647Slclee 	/*
24503ccda647Slclee 	 * Fill in geometry structure with data from label.
24513ccda647Slclee 	 */
2452e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2453e8fb11a1Sshidokht 	cl->cl_g.dkg_ncyl   = labp->dkl_ncyl;
2454e8fb11a1Sshidokht 	cl->cl_g.dkg_acyl   = labp->dkl_acyl;
2455e8fb11a1Sshidokht 	cl->cl_g.dkg_bcyl   = 0;
2456e8fb11a1Sshidokht 	cl->cl_g.dkg_nhead  = labp->dkl_nhead;
2457e8fb11a1Sshidokht 	cl->cl_g.dkg_nsect  = labp->dkl_nsect;
2458e8fb11a1Sshidokht 	cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
24593ccda647Slclee 
24603ccda647Slclee #if defined(_SUNOS_VTOC_8)
2461e8fb11a1Sshidokht 	cl->cl_g.dkg_gap1   = labp->dkl_gap1;
2462e8fb11a1Sshidokht 	cl->cl_g.dkg_gap2   = labp->dkl_gap2;
2463e8fb11a1Sshidokht 	cl->cl_g.dkg_bhead  = labp->dkl_bhead;
24643ccda647Slclee #endif
24653ccda647Slclee #if defined(_SUNOS_VTOC_16)
2466e8fb11a1Sshidokht 	cl->cl_dkg_skew = labp->dkl_skew;
24673ccda647Slclee #endif
24683ccda647Slclee 
24693ccda647Slclee #if defined(__i386) || defined(__amd64)
2470e8fb11a1Sshidokht 	cl->cl_g.dkg_apc = labp->dkl_apc;
24713ccda647Slclee #endif
24723ccda647Slclee 
24733ccda647Slclee 	/*
24743ccda647Slclee 	 * Currently we rely on the values in the label being accurate. If
24753ccda647Slclee 	 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
24763ccda647Slclee 	 *
24773ccda647Slclee 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
24783ccda647Slclee 	 * although this command is optional in SCSI-2.
24793ccda647Slclee 	 */
2480e8fb11a1Sshidokht 	cl->cl_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
2481e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
2482e8fb11a1Sshidokht 	    (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
24833ccda647Slclee 
24843ccda647Slclee 	/*
24853ccda647Slclee 	 * The Read and Write reinstruct values may not be valid
24863ccda647Slclee 	 * for older disks.
24873ccda647Slclee 	 */
2488e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
2489e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
24903ccda647Slclee 
24913ccda647Slclee 	/* Fill in partition table. */
24923ccda647Slclee #if defined(_SUNOS_VTOC_8)
24933ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
2494e8fb11a1Sshidokht 		cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
2495e8fb11a1Sshidokht 		cl->cl_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
24963ccda647Slclee 	}
24973ccda647Slclee #endif
24983ccda647Slclee #if  defined(_SUNOS_VTOC_16)
24993ccda647Slclee 	vpartp		= labp->dkl_vtoc.v_part;
25003ccda647Slclee 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
25013ccda647Slclee 
2502e8fb11a1Sshidokht 	/* Prevent divide by zero */
2503e8fb11a1Sshidokht 	if (track_capacity == 0) {
2504e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2505e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2506e8fb11a1Sshidokht 			    "Corrupt label - zero nhead or nsect value\n");
2507e8fb11a1Sshidokht 
2508e8fb11a1Sshidokht 		return (CMLB_LABEL_IS_INVALID);
2509e8fb11a1Sshidokht 	}
2510e8fb11a1Sshidokht 
25113ccda647Slclee 	for (i = 0; i < NDKMAP; i++, vpartp++) {
2512e8fb11a1Sshidokht 		cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
2513e8fb11a1Sshidokht 		cl->cl_map[i].dkl_nblk  = vpartp->p_size;
25143ccda647Slclee 	}
25153ccda647Slclee #endif
25163ccda647Slclee 
25173ccda647Slclee 	/* Fill in VTOC Structure. */
2518e8fb11a1Sshidokht 	bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
25193ccda647Slclee #if defined(_SUNOS_VTOC_8)
25203ccda647Slclee 	/*
25213ccda647Slclee 	 * The 8-slice vtoc does not include the ascii label; save it into
25223ccda647Slclee 	 * the device's soft state structure here.
25233ccda647Slclee 	 */
2524e8fb11a1Sshidokht 	bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
25253ccda647Slclee #endif
25263ccda647Slclee 
25273ccda647Slclee 	/* Now look for a valid capacity. */
2528e8fb11a1Sshidokht 	track_capacity	= (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
2529e8fb11a1Sshidokht 	label_capacity	= (cl->cl_g.dkg_ncyl  * track_capacity);
25303ccda647Slclee 
2531e8fb11a1Sshidokht 	if (cl->cl_g.dkg_acyl) {
25323ccda647Slclee #if defined(__i386) || defined(__amd64)
25333ccda647Slclee 		/* we may have > 1 alts cylinder */
2534e8fb11a1Sshidokht 		label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
25353ccda647Slclee #else
25363ccda647Slclee 		label_capacity += track_capacity;
25373ccda647Slclee #endif
25383ccda647Slclee 	}
25393ccda647Slclee 
25403ccda647Slclee 	/*
2541e8fb11a1Sshidokht 	 * Force check here to ensure the computed capacity is valid.
2542e8fb11a1Sshidokht 	 * If capacity is zero, it indicates an invalid label and
2543e8fb11a1Sshidokht 	 * we should abort updating the relevant data then.
2544e8fb11a1Sshidokht 	 */
2545e8fb11a1Sshidokht 	if (label_capacity == 0) {
2546e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2547e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2548e8fb11a1Sshidokht 			    "Corrupt label - no valid capacity could be "
2549e8fb11a1Sshidokht 			    "retrieved\n");
2550e8fb11a1Sshidokht 
2551e8fb11a1Sshidokht 		return (CMLB_LABEL_IS_INVALID);
2552e8fb11a1Sshidokht 	}
2553e8fb11a1Sshidokht 
2554e8fb11a1Sshidokht 	/* Mark the geometry as valid. */
25557f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_TRUE;
2556e8fb11a1Sshidokht 
2557e8fb11a1Sshidokht 	/*
25583ccda647Slclee 	 * if we got invalidated when mutex exit and entered again,
25593ccda647Slclee 	 * if blockcount different than when we came in, need to
25603ccda647Slclee 	 * retry from beginning of cmlb_validate_geometry.
25613ccda647Slclee 	 * revisit this on next phase of utilizing this for
25623ccda647Slclee 	 * sd.
25633ccda647Slclee 	 */
25643ccda647Slclee 
2565e8fb11a1Sshidokht 	if (label_capacity <= cl->cl_blockcount) {
25663ccda647Slclee #if defined(_SUNOS_VTOC_8)
25673ccda647Slclee 		/*
25683ccda647Slclee 		 * We can't let this happen on drives that are subdivided
25693ccda647Slclee 		 * into logical disks (i.e., that have an fdisk table).
2570e8fb11a1Sshidokht 		 * The cl_blockcount field should always hold the full media
25713ccda647Slclee 		 * size in sectors, period.  This code would overwrite
2572e8fb11a1Sshidokht 		 * cl_blockcount with the size of the Solaris fdisk partition.
25733ccda647Slclee 		 */
2574e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
25753ccda647Slclee 		    "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
2576e8fb11a1Sshidokht 		    label_capacity, cl->cl_blockcount);
2577e8fb11a1Sshidokht 		cl->cl_solaris_size = label_capacity;
25783ccda647Slclee 
25793ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
25803ccda647Slclee 		goto done;
25813ccda647Slclee 	}
25823ccda647Slclee 
2583e8fb11a1Sshidokht 	if (ISCD(cl)) {
25843ccda647Slclee 		/* For CDROMs, we trust that the data in the label is OK. */
25853ccda647Slclee #if defined(_SUNOS_VTOC_8)
25863ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
25873ccda647Slclee 			part_end = labp->dkl_nhead * labp->dkl_nsect *
25883ccda647Slclee 			    labp->dkl_map[i].dkl_cylno +
25893ccda647Slclee 			    labp->dkl_map[i].dkl_nblk  - 1;
25903ccda647Slclee 
25913ccda647Slclee 			if ((labp->dkl_map[i].dkl_nblk) &&
2592e8fb11a1Sshidokht 			    (part_end > cl->cl_blockcount)) {
25937f0b8309SEdward Pilatowicz 				cl->cl_f_geometry_is_valid = B_FALSE;
25943ccda647Slclee 				break;
25953ccda647Slclee 			}
25963ccda647Slclee 		}
25973ccda647Slclee #endif
25983ccda647Slclee #if defined(_SUNOS_VTOC_16)
25993ccda647Slclee 		vpartp = &(labp->dkl_vtoc.v_part[0]);
26003ccda647Slclee 		for (i = 0; i < NDKMAP; i++, vpartp++) {
26013ccda647Slclee 			part_end = vpartp->p_start + vpartp->p_size;
26023ccda647Slclee 			if ((vpartp->p_size > 0) &&
2603e8fb11a1Sshidokht 			    (part_end > cl->cl_blockcount)) {
26047f0b8309SEdward Pilatowicz 				cl->cl_f_geometry_is_valid = B_FALSE;
26053ccda647Slclee 				break;
26063ccda647Slclee 			}
26073ccda647Slclee 		}
26083ccda647Slclee #endif
26093ccda647Slclee 	} else {
2610e8fb11a1Sshidokht 		/* label_capacity > cl->cl_blockcount */
2611e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT)) {
2612e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
26133ccda647Slclee 			    "Corrupt label - bad geometry\n");
2614e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
26153ccda647Slclee 			    "Label says %llu blocks; Drive says %llu blocks\n",
2616e8fb11a1Sshidokht 			    label_capacity, cl->cl_blockcount);
2617e8fb11a1Sshidokht 		}
26187f0b8309SEdward Pilatowicz 		cl->cl_f_geometry_is_valid = B_FALSE;
26193ccda647Slclee 		label_error = CMLB_LABEL_IS_INVALID;
26203ccda647Slclee 	}
26213ccda647Slclee 
26223ccda647Slclee done:
26233ccda647Slclee 
2624e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "cmlb_uselabel: (label geometry)\n");
2625e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
26263ccda647Slclee 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
2627e8fb11a1Sshidokht 	    cl->cl_g.dkg_ncyl,  cl->cl_g.dkg_acyl,
2628e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
26293ccda647Slclee 
2630e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
26313ccda647Slclee 	    "   label_capacity: %d; intrlv: %d; rpm: %d\n",
2632e8fb11a1Sshidokht 	    cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
2633e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "   wrt_reinstr: %d; rd_reinstr: %d\n",
2634e8fb11a1Sshidokht 	    cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
26353ccda647Slclee 
2636e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
26373ccda647Slclee 
26383ccda647Slclee 	return (label_error);
26393ccda647Slclee }
26403ccda647Slclee 
26413ccda647Slclee 
26423ccda647Slclee /*
26433ccda647Slclee  *    Function: cmlb_build_default_label
26443ccda647Slclee  *
26453ccda647Slclee  * Description: Generate a default label for those devices that do not have
26463ccda647Slclee  *		one, e.g., new media, removable cartridges, etc..
26473ccda647Slclee  *
26483ccda647Slclee  *     Context: Kernel thread only
26493ccda647Slclee  */
2650e8fb11a1Sshidokht /*ARGSUSED*/
26513ccda647Slclee static void
2652e8fb11a1Sshidokht cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
26533ccda647Slclee {
26543ccda647Slclee #if defined(_SUNOS_VTOC_16)
26553ccda647Slclee 	uint_t	phys_spc;
26563ccda647Slclee 	uint_t	disksize;
2657e8fb11a1Sshidokht 	struct  dk_geom cl_g;
2658e8fb11a1Sshidokht 	diskaddr_t capacity;
26593ccda647Slclee #endif
26603ccda647Slclee 
2661e8fb11a1Sshidokht 	ASSERT(cl != NULL);
2662e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
26633ccda647Slclee 
26643ccda647Slclee #if defined(_SUNOS_VTOC_8)
26653ccda647Slclee 	/*
26663ccda647Slclee 	 * Note: This is a legacy check for non-removable devices on VTOC_8
26673ccda647Slclee 	 * only. This may be a valid check for VTOC_16 as well.
2668e8fb11a1Sshidokht 	 * Once we understand why there is this difference between SPARC and
2669e8fb11a1Sshidokht 	 * x86 platform, we could remove this legacy check.
26703ccda647Slclee 	 */
2671e8fb11a1Sshidokht 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
26723ccda647Slclee 		return;
26733ccda647Slclee 	}
26743ccda647Slclee #endif
26753ccda647Slclee 
2676e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2677e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2678e8fb11a1Sshidokht 	bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
26793ccda647Slclee 
26803ccda647Slclee #if defined(_SUNOS_VTOC_8)
26813ccda647Slclee 
26823ccda647Slclee 	/*
26833ccda647Slclee 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
26843ccda647Slclee 	 * But it is still necessary to set up various geometry information,
26853ccda647Slclee 	 * and we are doing this here.
26863ccda647Slclee 	 */
26873ccda647Slclee 
26883ccda647Slclee 	/*
26893ccda647Slclee 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
26903ccda647Slclee 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
26913ccda647Slclee 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
26923ccda647Slclee 	 * equal to C*H*S values.  This will cause some truncation of size due
26933ccda647Slclee 	 * to round off errors. For CD-ROMs, this truncation can have adverse
26943ccda647Slclee 	 * side effects, so returning ncyl and nhead as 1. The nsect will
26953ccda647Slclee 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
26963ccda647Slclee 	 */
2697e8fb11a1Sshidokht 	cl->cl_solaris_size = cl->cl_blockcount;
2698e8fb11a1Sshidokht 	if (ISCD(cl)) {
26993ccda647Slclee 		tg_attribute_t tgattribute;
27003ccda647Slclee 		int is_writable;
27013ccda647Slclee 		/*
27023ccda647Slclee 		 * Preserve the old behavior for non-writable
27033ccda647Slclee 		 * medias. Since dkg_nsect is a ushort, it
27043ccda647Slclee 		 * will lose bits as cdroms have more than
27053ccda647Slclee 		 * 65536 sectors. So if we recalculate
27063ccda647Slclee 		 * capacity, it will become much shorter.
27073ccda647Slclee 		 * But the dkg_* information is not
27083ccda647Slclee 		 * used for CDROMs so it is OK. But for
27093ccda647Slclee 		 * Writable CDs we need this information
27103ccda647Slclee 		 * to be valid (for newfs say). So we
27113ccda647Slclee 		 * make nsect and nhead > 1 that way
27123ccda647Slclee 		 * nsect can still stay within ushort limit
27133ccda647Slclee 		 * without losing any bits.
27143ccda647Slclee 		 */
27153ccda647Slclee 
27163ccda647Slclee 		bzero(&tgattribute, sizeof (tg_attribute_t));
27173ccda647Slclee 
2718e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
2719e8fb11a1Sshidokht 		is_writable =
2720e8fb11a1Sshidokht 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
27213ccda647Slclee 		    tgattribute.media_is_writable : 1;
2722e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
27233ccda647Slclee 
27243ccda647Slclee 		if (is_writable) {
2725e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
2726e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
2727e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
2728342440ecSPrasad Singamsetty 			cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
2729e8fb11a1Sshidokht 			    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
27303ccda647Slclee 		} else {
2731e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl  = 1;
2732e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 1;
2733e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
27343ccda647Slclee 		}
27353ccda647Slclee 	} else {
2736e8fb11a1Sshidokht 		if (cl->cl_blockcount <= 0x1000) {
27373ccda647Slclee 			/* unlabeled SCSI floppy device */
2738e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 2;
2739e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 80;
2740e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
2741e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x200000) {
2742e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
2743e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
2744e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (64 * 32);
27453ccda647Slclee 		} else {
2746e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 255;
274798a65dd4Sshidokht 
274898a65dd4Sshidokht 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
274998a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63) - 1) /
275098a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63)) * 63;
275198a65dd4Sshidokht 
275298a65dd4Sshidokht 			if (cl->cl_g.dkg_nsect == 0)
275398a65dd4Sshidokht 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
275498a65dd4Sshidokht 
275598a65dd4Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
275698a65dd4Sshidokht 			    (255 * cl->cl_g.dkg_nsect);
27573ccda647Slclee 		}
275898a65dd4Sshidokht 
2759e8fb11a1Sshidokht 		cl->cl_solaris_size =
2760342440ecSPrasad Singamsetty 		    (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
2761342440ecSPrasad Singamsetty 		    cl->cl_g.dkg_nsect;
27623ccda647Slclee 
27633ccda647Slclee 	}
27643ccda647Slclee 
2765e8fb11a1Sshidokht 	cl->cl_g.dkg_acyl	= 0;
2766e8fb11a1Sshidokht 	cl->cl_g.dkg_bcyl	= 0;
2767e8fb11a1Sshidokht 	cl->cl_g.dkg_rpm	= 200;
2768e8fb11a1Sshidokht 	cl->cl_asciilabel[0]	= '\0';
2769e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl	= cl->cl_g.dkg_ncyl;
27703ccda647Slclee 
2771e8fb11a1Sshidokht 	cl->cl_map[0].dkl_cylno = 0;
2772e8fb11a1Sshidokht 	cl->cl_map[0].dkl_nblk  = cl->cl_solaris_size;
27733ccda647Slclee 
2774e8fb11a1Sshidokht 	cl->cl_map[2].dkl_cylno = 0;
2775e8fb11a1Sshidokht 	cl->cl_map[2].dkl_nblk  = cl->cl_solaris_size;
27763ccda647Slclee 
27773ccda647Slclee #elif defined(_SUNOS_VTOC_16)
27783ccda647Slclee 
2779e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
27803ccda647Slclee 		/*
27813ccda647Slclee 		 * Got fdisk table but no solaris entry therefore
27823ccda647Slclee 		 * don't create a default label
27833ccda647Slclee 		 */
27847f0b8309SEdward Pilatowicz 		cl->cl_f_geometry_is_valid = B_TRUE;
27853ccda647Slclee 		return;
27863ccda647Slclee 	}
27873ccda647Slclee 
27883ccda647Slclee 	/*
27893ccda647Slclee 	 * For CDs we continue to use the physical geometry to calculate
27903ccda647Slclee 	 * number of cylinders. All other devices must convert the
27913ccda647Slclee 	 * physical geometry (cmlb_geom) to values that will fit
27923ccda647Slclee 	 * in a dk_geom structure.
27933ccda647Slclee 	 */
2794e8fb11a1Sshidokht 	if (ISCD(cl)) {
2795e8fb11a1Sshidokht 		phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
27963ccda647Slclee 	} else {
27973ccda647Slclee 		/* Convert physical geometry to disk geometry */
2798e8fb11a1Sshidokht 		bzero(&cl_g, sizeof (struct dk_geom));
2799e8fb11a1Sshidokht 
2800e8fb11a1Sshidokht 		/*
2801e8fb11a1Sshidokht 		 * Refer to comments related to off-by-1 at the
2802e8fb11a1Sshidokht 		 * header of this file.
2803b9ccdc5aScth 		 * Before calculating geometry, capacity should be
2804e8fb11a1Sshidokht 		 * decreased by 1.
2805e8fb11a1Sshidokht 		 */
2806e8fb11a1Sshidokht 
2807e8fb11a1Sshidokht 		if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
2808e8fb11a1Sshidokht 			capacity = cl->cl_blockcount - 1;
2809e8fb11a1Sshidokht 		else
2810e8fb11a1Sshidokht 			capacity = cl->cl_blockcount;
2811e8fb11a1Sshidokht 
2812e8fb11a1Sshidokht 
2813e8fb11a1Sshidokht 		cmlb_convert_geometry(capacity, &cl_g);
2814e8fb11a1Sshidokht 		bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
2815e8fb11a1Sshidokht 		phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
28163ccda647Slclee 	}
28173ccda647Slclee 
28187501b6d5SSheshadri Vasudevan 	if (phys_spc == 0)
28197501b6d5SSheshadri Vasudevan 		return;
2820e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
2821843e1988Sjohnlev 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2822843e1988Sjohnlev 		/* disable devid */
2823843e1988Sjohnlev 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
2824843e1988Sjohnlev 		disksize = cl->cl_solaris_size;
2825843e1988Sjohnlev 	} else {
2826e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = DK_ACYL;
2827e8fb11a1Sshidokht 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
2828e8fb11a1Sshidokht 		disksize = cl->cl_g.dkg_ncyl * phys_spc;
2829843e1988Sjohnlev 	}
28303ccda647Slclee 
2831e8fb11a1Sshidokht 	if (ISCD(cl)) {
28323ccda647Slclee 		/*
28333ccda647Slclee 		 * CD's don't use the "heads * sectors * cyls"-type of
28343ccda647Slclee 		 * geometry, but instead use the entire capacity of the media.
28353ccda647Slclee 		 */
2836e8fb11a1Sshidokht 		disksize = cl->cl_solaris_size;
2837e8fb11a1Sshidokht 		cl->cl_g.dkg_nhead = 1;
2838e8fb11a1Sshidokht 		cl->cl_g.dkg_nsect = 1;
2839e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm =
2840e8fb11a1Sshidokht 		    (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
28413ccda647Slclee 
2842e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_start = 0;
2843e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2844e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_tag   = V_BACKUP;
2845e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_flag  = V_UNMNT;
28463ccda647Slclee 
2847e8fb11a1Sshidokht 		cl->cl_map[0].dkl_cylno = 0;
2848e8fb11a1Sshidokht 		cl->cl_map[0].dkl_nblk  = disksize;
2849e8fb11a1Sshidokht 		cl->cl_offset[0] = 0;
28503ccda647Slclee 
28513ccda647Slclee 	} else {
28523ccda647Slclee 		/*
28533ccda647Slclee 		 * Hard disks and removable media cartridges
28543ccda647Slclee 		 */
2855e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm =
2856e8fb11a1Sshidokht 		    (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
2857e8fb11a1Sshidokht 		cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
28583ccda647Slclee 
28593ccda647Slclee 		/* Add boot slice */
2860e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_start = 0;
2861e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_size  = phys_spc;
2862e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_tag   = V_BOOT;
2863e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_flag  = V_UNMNT;
28643ccda647Slclee 
2865e8fb11a1Sshidokht 		cl->cl_map[8].dkl_cylno = 0;
2866e8fb11a1Sshidokht 		cl->cl_map[8].dkl_nblk  = phys_spc;
2867e8fb11a1Sshidokht 		cl->cl_offset[8] = 0;
28683ccda647Slclee 
2869e8fb11a1Sshidokht 		if ((cl->cl_alter_behavior &
28703ccda647Slclee 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
2871e8fb11a1Sshidokht 		    cl->cl_device_type == DTYPE_DIRECT) {
2872e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_start = phys_spc;
2873e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_size  = 2 * phys_spc;
2874e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_tag   = V_ALTSCTR;
2875e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_flag  = 0;
28763ccda647Slclee 
2877e8fb11a1Sshidokht 			cl->cl_map[9].dkl_cylno = 1;
2878e8fb11a1Sshidokht 			cl->cl_map[9].dkl_nblk  = 2 * phys_spc;
2879e8fb11a1Sshidokht 			cl->cl_offset[9] = phys_spc;
28803ccda647Slclee 		}
28813ccda647Slclee 	}
28823ccda647Slclee 
2883e8fb11a1Sshidokht 	cl->cl_g.dkg_apc = 0;
28843ccda647Slclee 
28853ccda647Slclee 	/* Add backup slice */
2886e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_start = 0;
2887e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_size  = disksize;
2888e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
2889e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
28903ccda647Slclee 
2891e8fb11a1Sshidokht 	cl->cl_map[2].dkl_cylno = 0;
2892e8fb11a1Sshidokht 	cl->cl_map[2].dkl_nblk  = disksize;
2893e8fb11a1Sshidokht 	cl->cl_offset[2] = 0;
28943ccda647Slclee 
2895843e1988Sjohnlev 	/*
2896843e1988Sjohnlev 	 * single slice (s0) covering the entire disk
2897843e1988Sjohnlev 	 */
2898843e1988Sjohnlev 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2899843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_start = 0;
2900843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_tag   = V_UNASSIGNED;
2901843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_flag  = 0;
2902843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2903843e1988Sjohnlev 		cl->cl_map[0].dkl_cylno = 0;
2904843e1988Sjohnlev 		cl->cl_map[0].dkl_nblk  = disksize;
2905843e1988Sjohnlev 		cl->cl_offset[0] = 0;
2906843e1988Sjohnlev 	}
2907843e1988Sjohnlev 
2908e8fb11a1Sshidokht 	(void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
2909e8fb11a1Sshidokht 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
2910e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
29113ccda647Slclee 
29123ccda647Slclee #else
29133ccda647Slclee #error "No VTOC format defined."
29143ccda647Slclee #endif
29153ccda647Slclee 
2916e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct  = 0;
2917e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = 0;
29183ccda647Slclee 
2919e8fb11a1Sshidokht 	cl->cl_g.dkg_intrlv = 1;
29203ccda647Slclee 
2921e8fb11a1Sshidokht 	cl->cl_vtoc.v_sanity  = VTOC_SANE;
2922c813bb04SGabriel Carrillo 	cl->cl_vtoc.v_nparts = V_NUMPAR;
2923c813bb04SGabriel Carrillo 	cl->cl_vtoc.v_version = V_VERSION;
29243ccda647Slclee 
29257f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_TRUE;
2926342440ecSPrasad Singamsetty 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
29273ccda647Slclee 
2928e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
29293ccda647Slclee 	    "cmlb_build_default_label: Default label created: "
29303ccda647Slclee 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
2931e8fb11a1Sshidokht 	    cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
2932e8fb11a1Sshidokht 	    cl->cl_g.dkg_nsect, cl->cl_blockcount);
29333ccda647Slclee }
29343ccda647Slclee 
29353ccda647Slclee 
29363ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
29373ccda647Slclee /*
29383ccda647Slclee  * Max CHS values, as they are encoded into bytes, for 1022/254/63
29393ccda647Slclee  */
29403ccda647Slclee #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
29413ccda647Slclee #define	LBA_MAX_CYL	(1022 & 0xFF)
29423ccda647Slclee #define	LBA_MAX_HEAD	(254)
29433ccda647Slclee 
29443ccda647Slclee 
29453ccda647Slclee /*
29463ccda647Slclee  *    Function: cmlb_has_max_chs_vals
29473ccda647Slclee  *
29487f0b8309SEdward Pilatowicz  * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
29493ccda647Slclee  *
29503ccda647Slclee  *   Arguments: fdp - ptr to CHS info
29513ccda647Slclee  *
29523ccda647Slclee  * Return Code: True or false
29533ccda647Slclee  *
29543ccda647Slclee  *     Context: Any.
29553ccda647Slclee  */
29567f0b8309SEdward Pilatowicz static boolean_t
29573ccda647Slclee cmlb_has_max_chs_vals(struct ipart *fdp)
29583ccda647Slclee {
29593ccda647Slclee 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
29603ccda647Slclee 	    (fdp->beghead == LBA_MAX_HEAD)	&&
29613ccda647Slclee 	    (fdp->begsect == LBA_MAX_SECT)	&&
29623ccda647Slclee 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
29633ccda647Slclee 	    (fdp->endhead == LBA_MAX_HEAD)	&&
29643ccda647Slclee 	    (fdp->endsect == LBA_MAX_SECT));
29653ccda647Slclee }
29663ccda647Slclee #endif
29673ccda647Slclee 
29683ccda647Slclee /*
29693ccda647Slclee  *    Function: cmlb_dkio_get_geometry
29703ccda647Slclee  *
29713ccda647Slclee  * Description: This routine is the driver entry point for handling user
29723ccda647Slclee  *		requests to get the device geometry (DKIOCGGEOM).
29733ccda647Slclee  *
29743ccda647Slclee  *   Arguments:
2975e8fb11a1Sshidokht  *	arg		pointer to user provided dk_geom structure specifying
29763ccda647Slclee  *			the controller's notion of the current geometry.
2977e8fb11a1Sshidokht  *
2978e8fb11a1Sshidokht  *	flag 		this argument is a pass through to ddi_copyxxx()
29793ccda647Slclee  *			directly from the mode argument of ioctl().
29803ccda647Slclee  *
2981e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
2982e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
2983e8fb11a1Sshidokht  *
29843ccda647Slclee  * Return Code: 0
29853ccda647Slclee  *		EFAULT
29863ccda647Slclee  *		ENXIO
29873ccda647Slclee  *		EIO
29883ccda647Slclee  */
29893ccda647Slclee static int
2990e8fb11a1Sshidokht cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
2991e8fb11a1Sshidokht     void *tg_cookie)
29923ccda647Slclee {
29933ccda647Slclee 	struct dk_geom	*tmp_geom = NULL;
29943ccda647Slclee 	int		rval = 0;
29953ccda647Slclee 
29963ccda647Slclee 	/*
29973ccda647Slclee 	 * cmlb_validate_geometry does not spin a disk up
2998e8fb11a1Sshidokht 	 * if it was spcl down. We need to make sure it
29993ccda647Slclee 	 * is ready.
30003ccda647Slclee 	 */
3001e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
30027f0b8309SEdward Pilatowicz 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
30033ccda647Slclee #if defined(_SUNOS_VTOC_8)
30043ccda647Slclee 	if (rval == EINVAL &&
3005e8fb11a1Sshidokht 	    cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
30063ccda647Slclee 		/*
30073ccda647Slclee 		 * This is to return a default label geometry even when we
30083ccda647Slclee 		 * do not really assume a default label for the device.
30093ccda647Slclee 		 * dad driver utilizes this.
30103ccda647Slclee 		 */
3011342440ecSPrasad Singamsetty 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3012e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
30133ccda647Slclee 			rval = 0;
30143ccda647Slclee 		}
30153ccda647Slclee 	}
30163ccda647Slclee #endif
30173ccda647Slclee 	if (rval) {
3018e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
30193ccda647Slclee 		return (rval);
30203ccda647Slclee 	}
30213ccda647Slclee 
30223ccda647Slclee #if defined(__i386) || defined(__amd64)
3023e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
3024e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
30253ccda647Slclee 		return (EIO);
30263ccda647Slclee 	}
30273ccda647Slclee #endif
30283ccda647Slclee 
30293ccda647Slclee 	/*
30303ccda647Slclee 	 * Make a local copy of the soft state geometry to avoid some potential
30313ccda647Slclee 	 * race conditions associated with holding the mutex and updating the
30323ccda647Slclee 	 * write_reinstruct value
30333ccda647Slclee 	 */
30343ccda647Slclee 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3035e8fb11a1Sshidokht 	bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
30363ccda647Slclee 
30373ccda647Slclee 	if (tmp_geom->dkg_write_reinstruct == 0) {
30383ccda647Slclee 		tmp_geom->dkg_write_reinstruct =
30393ccda647Slclee 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
30403ccda647Slclee 		    cmlb_rot_delay) / (int)60000);
30413ccda647Slclee 	}
3042e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
30433ccda647Slclee 
30443ccda647Slclee 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
30453ccda647Slclee 	    flag);
30463ccda647Slclee 	if (rval != 0) {
30473ccda647Slclee 		rval = EFAULT;
30483ccda647Slclee 	}
30493ccda647Slclee 
30503ccda647Slclee 	kmem_free(tmp_geom, sizeof (struct dk_geom));
30513ccda647Slclee 	return (rval);
30523ccda647Slclee 
30533ccda647Slclee }
30543ccda647Slclee 
30553ccda647Slclee 
30563ccda647Slclee /*
30573ccda647Slclee  *    Function: cmlb_dkio_set_geometry
30583ccda647Slclee  *
30593ccda647Slclee  * Description: This routine is the driver entry point for handling user
30603ccda647Slclee  *		requests to set the device geometry (DKIOCSGEOM). The actual
30613ccda647Slclee  *		device geometry is not updated, just the driver "notion" of it.
30623ccda647Slclee  *
30633ccda647Slclee  *   Arguments:
3064e8fb11a1Sshidokht  *	arg		pointer to user provided dk_geom structure used to set
30653ccda647Slclee  *			the controller's notion of the current geometry.
3066e8fb11a1Sshidokht  *
3067e8fb11a1Sshidokht  *	flag 		this argument is a pass through to ddi_copyxxx()
30683ccda647Slclee  *			directly from the mode argument of ioctl().
30693ccda647Slclee  *
3070e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3071e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3072e8fb11a1Sshidokht  *
30733ccda647Slclee  * Return Code: 0
30743ccda647Slclee  *		EFAULT
30753ccda647Slclee  *		ENXIO
30763ccda647Slclee  *		EIO
30773ccda647Slclee  */
30783ccda647Slclee static int
3079e8fb11a1Sshidokht cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
30803ccda647Slclee {
30813ccda647Slclee 	struct dk_geom	*tmp_geom;
30823ccda647Slclee 	struct dk_map	*lp;
30833ccda647Slclee 	int		rval = 0;
30843ccda647Slclee 	int		i;
30853ccda647Slclee 
30863ccda647Slclee 
30873ccda647Slclee #if defined(__i386) || defined(__amd64)
3088e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
30893ccda647Slclee 		return (EIO);
30903ccda647Slclee 	}
30913ccda647Slclee #endif
30923ccda647Slclee 	/*
30933ccda647Slclee 	 * We need to copy the user specified geometry into local
30943ccda647Slclee 	 * storage and then update the softstate. We don't want to hold
30953ccda647Slclee 	 * the mutex and copyin directly from the user to the soft state
30963ccda647Slclee 	 */
30973ccda647Slclee 	tmp_geom = (struct dk_geom *)
30983ccda647Slclee 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
30993ccda647Slclee 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
31003ccda647Slclee 	if (rval != 0) {
31013ccda647Slclee 		kmem_free(tmp_geom, sizeof (struct dk_geom));
31023ccda647Slclee 		return (EFAULT);
31033ccda647Slclee 	}
31043ccda647Slclee 
3105e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3106e8fb11a1Sshidokht 	bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
31073ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
3108e8fb11a1Sshidokht 		lp  = &cl->cl_map[i];
3109e8fb11a1Sshidokht 		cl->cl_offset[i] =
3110e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
31113ccda647Slclee #if defined(__i386) || defined(__amd64)
3112e8fb11a1Sshidokht 		cl->cl_offset[i] += cl->cl_solaris_offset;
31133ccda647Slclee #endif
31143ccda647Slclee 	}
31157f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
3116e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
31173ccda647Slclee 	kmem_free(tmp_geom, sizeof (struct dk_geom));
31183ccda647Slclee 
31193ccda647Slclee 	return (rval);
31203ccda647Slclee }
31213ccda647Slclee 
31223ccda647Slclee /*
31233ccda647Slclee  *    Function: cmlb_dkio_get_partition
31243ccda647Slclee  *
31253ccda647Slclee  * Description: This routine is the driver entry point for handling user
31263ccda647Slclee  *		requests to get the partition table (DKIOCGAPART).
31273ccda647Slclee  *
31283ccda647Slclee  *   Arguments:
3129e8fb11a1Sshidokht  *	arg		pointer to user provided dk_allmap structure specifying
31303ccda647Slclee  *			the controller's notion of the current partition table.
3131e8fb11a1Sshidokht  *
3132e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
31333ccda647Slclee  *			directly from the mode argument of ioctl().
31343ccda647Slclee  *
3135e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3136e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3137e8fb11a1Sshidokht  *
31383ccda647Slclee  * Return Code: 0
31393ccda647Slclee  *		EFAULT
31403ccda647Slclee  *		ENXIO
31413ccda647Slclee  *		EIO
31423ccda647Slclee  */
31433ccda647Slclee static int
3144e8fb11a1Sshidokht cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3145e8fb11a1Sshidokht     void *tg_cookie)
31463ccda647Slclee {
31473ccda647Slclee 	int		rval = 0;
31483ccda647Slclee 	int		size;
31493ccda647Slclee 
31503ccda647Slclee 	/*
31513ccda647Slclee 	 * Make sure the geometry is valid before getting the partition
31523ccda647Slclee 	 * information.
31533ccda647Slclee 	 */
3154e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
31557f0b8309SEdward Pilatowicz 	if ((rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie)) != 0) {
3156e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
31573ccda647Slclee 		return (rval);
31583ccda647Slclee 	}
3159e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
31603ccda647Slclee 
31613ccda647Slclee #if defined(__i386) || defined(__amd64)
3162e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
31633ccda647Slclee 		return (EIO);
31643ccda647Slclee 	}
31653ccda647Slclee #endif
31663ccda647Slclee 
31673ccda647Slclee #ifdef _MULTI_DATAMODEL
31683ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
31693ccda647Slclee 	case DDI_MODEL_ILP32: {
31703ccda647Slclee 		struct dk_map32 dk_map32[NDKMAP];
31713ccda647Slclee 		int		i;
31723ccda647Slclee 
31733ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
3174e8fb11a1Sshidokht 			dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3175e8fb11a1Sshidokht 			dk_map32[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
31763ccda647Slclee 		}
31773ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map32);
31783ccda647Slclee 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
31793ccda647Slclee 		if (rval != 0) {
31803ccda647Slclee 			rval = EFAULT;
31813ccda647Slclee 		}
31823ccda647Slclee 		break;
31833ccda647Slclee 	}
31843ccda647Slclee 	case DDI_MODEL_NONE:
31853ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map);
3186e8fb11a1Sshidokht 		rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
31873ccda647Slclee 		if (rval != 0) {
31883ccda647Slclee 			rval = EFAULT;
31893ccda647Slclee 		}
31903ccda647Slclee 		break;
31913ccda647Slclee 	}
31923ccda647Slclee #else /* ! _MULTI_DATAMODEL */
31933ccda647Slclee 	size = NDKMAP * sizeof (struct dk_map);
3194e8fb11a1Sshidokht 	rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
31953ccda647Slclee 	if (rval != 0) {
31963ccda647Slclee 		rval = EFAULT;
31973ccda647Slclee 	}
31983ccda647Slclee #endif /* _MULTI_DATAMODEL */
31993ccda647Slclee 	return (rval);
32003ccda647Slclee }
32013ccda647Slclee 
32023ccda647Slclee /*
32033ccda647Slclee  *    Function: cmlb_dkio_set_partition
32043ccda647Slclee  *
32053ccda647Slclee  * Description: This routine is the driver entry point for handling user
32063ccda647Slclee  *		requests to set the partition table (DKIOCSAPART). The actual
32073ccda647Slclee  *		device partition is not updated.
32083ccda647Slclee  *
32093ccda647Slclee  *   Arguments:
32103ccda647Slclee  *		arg  - pointer to user provided dk_allmap structure used to set
32113ccda647Slclee  *			the controller's notion of the partition table.
32123ccda647Slclee  *		flag - this argument is a pass through to ddi_copyxxx()
32133ccda647Slclee  *		       directly from the mode argument of ioctl().
32143ccda647Slclee  *
32153ccda647Slclee  * Return Code: 0
32163ccda647Slclee  *		EINVAL
32173ccda647Slclee  *		EFAULT
32183ccda647Slclee  *		ENXIO
32193ccda647Slclee  *		EIO
32203ccda647Slclee  */
32213ccda647Slclee static int
3222e8fb11a1Sshidokht cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
32233ccda647Slclee {
32243ccda647Slclee 	struct dk_map	dk_map[NDKMAP];
32253ccda647Slclee 	struct dk_map	*lp;
32263ccda647Slclee 	int		rval = 0;
32273ccda647Slclee 	int		size;
32283ccda647Slclee 	int		i;
32293ccda647Slclee #if defined(_SUNOS_VTOC_16)
32303ccda647Slclee 	struct dkl_partition	*vp;
32313ccda647Slclee #endif
32323ccda647Slclee 
32333ccda647Slclee 	/*
32343ccda647Slclee 	 * Set the map for all logical partitions.  We lock
32353ccda647Slclee 	 * the priority just to make sure an interrupt doesn't
32363ccda647Slclee 	 * come in while the map is half updated.
32373ccda647Slclee 	 */
3238e8fb11a1Sshidokht 	_NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3239e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
32403ccda647Slclee 
3241342440ecSPrasad Singamsetty 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3242e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
32433ccda647Slclee 		return (ENOTSUP);
32443ccda647Slclee 	}
3245e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3246e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
32473ccda647Slclee 		return (EIO);
32483ccda647Slclee 	}
32493ccda647Slclee 
32503ccda647Slclee #ifdef _MULTI_DATAMODEL
32513ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
32523ccda647Slclee 	case DDI_MODEL_ILP32: {
32533ccda647Slclee 		struct dk_map32 dk_map32[NDKMAP];
32543ccda647Slclee 
32553ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map32);
32563ccda647Slclee 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
32573ccda647Slclee 		if (rval != 0) {
32583ccda647Slclee 			return (EFAULT);
32593ccda647Slclee 		}
32603ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
32613ccda647Slclee 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
32623ccda647Slclee 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
32633ccda647Slclee 		}
32643ccda647Slclee 		break;
32653ccda647Slclee 	}
32663ccda647Slclee 	case DDI_MODEL_NONE:
32673ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map);
32683ccda647Slclee 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
32693ccda647Slclee 		if (rval != 0) {
32703ccda647Slclee 			return (EFAULT);
32713ccda647Slclee 		}
32723ccda647Slclee 		break;
32733ccda647Slclee 	}
32743ccda647Slclee #else /* ! _MULTI_DATAMODEL */
32753ccda647Slclee 	size = NDKMAP * sizeof (struct dk_map);
32763ccda647Slclee 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
32773ccda647Slclee 	if (rval != 0) {
32783ccda647Slclee 		return (EFAULT);
32793ccda647Slclee 	}
32803ccda647Slclee #endif /* _MULTI_DATAMODEL */
32813ccda647Slclee 
3282e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
32833ccda647Slclee 	/* Note: The size used in this bcopy is set based upon the data model */
3284e8fb11a1Sshidokht 	bcopy(dk_map, cl->cl_map, size);
32853ccda647Slclee #if defined(_SUNOS_VTOC_16)
3286e8fb11a1Sshidokht 	vp = (struct dkl_partition *)&(cl->cl_vtoc);
32873ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
32883ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
3289e8fb11a1Sshidokht 		lp  = &cl->cl_map[i];
3290e8fb11a1Sshidokht 		cl->cl_offset[i] =
3291e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
32923ccda647Slclee #if defined(_SUNOS_VTOC_16)
3293e8fb11a1Sshidokht 		vp->p_start = cl->cl_offset[i];
32943ccda647Slclee 		vp->p_size = lp->dkl_nblk;
32953ccda647Slclee 		vp++;
32963ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
32973ccda647Slclee #if defined(__i386) || defined(__amd64)
3298e8fb11a1Sshidokht 		cl->cl_offset[i] += cl->cl_solaris_offset;
32993ccda647Slclee #endif
33003ccda647Slclee 	}
3301e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
33023ccda647Slclee 	return (rval);
33033ccda647Slclee }
33043ccda647Slclee 
33053ccda647Slclee 
33063ccda647Slclee /*
33073ccda647Slclee  *    Function: cmlb_dkio_get_vtoc
33083ccda647Slclee  *
33093ccda647Slclee  * Description: This routine is the driver entry point for handling user
33103ccda647Slclee  *		requests to get the current volume table of contents
33113ccda647Slclee  *		(DKIOCGVTOC).
33123ccda647Slclee  *
33133ccda647Slclee  *   Arguments:
3314e8fb11a1Sshidokht  *	arg		pointer to user provided vtoc structure specifying
33153ccda647Slclee  *			the current vtoc.
3316e8fb11a1Sshidokht  *
3317e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
33183ccda647Slclee  *			directly from the mode argument of ioctl().
33193ccda647Slclee  *
3320e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3321e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3322e8fb11a1Sshidokht  *
33233ccda647Slclee  * Return Code: 0
33243ccda647Slclee  *		EFAULT
33253ccda647Slclee  *		ENXIO
33263ccda647Slclee  *		EIO
33273ccda647Slclee  */
33283ccda647Slclee static int
3329e8fb11a1Sshidokht cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
33303ccda647Slclee {
33313ccda647Slclee #if defined(_SUNOS_VTOC_8)
33323ccda647Slclee 	struct vtoc	user_vtoc;
33333ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
33343ccda647Slclee 	int		rval = 0;
33353ccda647Slclee 
3336e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3337342440ecSPrasad Singamsetty 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3338342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
3339342440ecSPrasad Singamsetty 		return (EOVERFLOW);
3340342440ecSPrasad Singamsetty 	}
3341342440ecSPrasad Singamsetty 
33427f0b8309SEdward Pilatowicz 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
33433ccda647Slclee 
33443ccda647Slclee #if defined(_SUNOS_VTOC_8)
33453ccda647Slclee 	if (rval == EINVAL &&
3346e8fb11a1Sshidokht 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
33473ccda647Slclee 		/*
33483ccda647Slclee 		 * This is to return a default label even when we do not
33493ccda647Slclee 		 * really assume a default label for the device.
33503ccda647Slclee 		 * dad driver utilizes this.
33513ccda647Slclee 		 */
3352342440ecSPrasad Singamsetty 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3353e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
33543ccda647Slclee 			rval = 0;
33553ccda647Slclee 		}
33563ccda647Slclee 	}
33573ccda647Slclee #endif
33583ccda647Slclee 	if (rval) {
3359e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
33603ccda647Slclee 		return (rval);
33613ccda647Slclee 	}
33623ccda647Slclee 
33633ccda647Slclee #if defined(_SUNOS_VTOC_8)
3364e8fb11a1Sshidokht 	cmlb_build_user_vtoc(cl, &user_vtoc);
3365e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
33663ccda647Slclee 
33673ccda647Slclee #ifdef _MULTI_DATAMODEL
33683ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
33693ccda647Slclee 	case DDI_MODEL_ILP32: {
33703ccda647Slclee 		struct vtoc32 user_vtoc32;
33713ccda647Slclee 
33723ccda647Slclee 		vtoctovtoc32(user_vtoc, user_vtoc32);
33733ccda647Slclee 		if (ddi_copyout(&user_vtoc32, (void *)arg,
33743ccda647Slclee 		    sizeof (struct vtoc32), flag)) {
33753ccda647Slclee 			return (EFAULT);
33763ccda647Slclee 		}
33773ccda647Slclee 		break;
33783ccda647Slclee 	}
33793ccda647Slclee 
33803ccda647Slclee 	case DDI_MODEL_NONE:
33813ccda647Slclee 		if (ddi_copyout(&user_vtoc, (void *)arg,
33823ccda647Slclee 		    sizeof (struct vtoc), flag)) {
33833ccda647Slclee 			return (EFAULT);
33843ccda647Slclee 		}
33853ccda647Slclee 		break;
33863ccda647Slclee 	}
33873ccda647Slclee #else /* ! _MULTI_DATAMODEL */
33883ccda647Slclee 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
33893ccda647Slclee 		return (EFAULT);
33903ccda647Slclee 	}
33913ccda647Slclee #endif /* _MULTI_DATAMODEL */
33923ccda647Slclee 
33933ccda647Slclee #elif defined(_SUNOS_VTOC_16)
3394e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
33953ccda647Slclee 
33963ccda647Slclee #ifdef _MULTI_DATAMODEL
33973ccda647Slclee 	/*
3398e8fb11a1Sshidokht 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
33993ccda647Slclee 	 * 32-bit to maintain compatibility with existing on-disk
34003ccda647Slclee 	 * structures.  Thus, we need to convert the structure when copying
34013ccda647Slclee 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
34023ccda647Slclee 	 * program.  If the target is a 32-bit program, then no conversion
34033ccda647Slclee 	 * is necessary.
34043ccda647Slclee 	 */
34053ccda647Slclee 	/* LINTED: logical expression always true: op "||" */
3406e8fb11a1Sshidokht 	ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
34073ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
34083ccda647Slclee 	case DDI_MODEL_ILP32:
3409e8fb11a1Sshidokht 		if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
3410e8fb11a1Sshidokht 		    sizeof (cl->cl_vtoc), flag)) {
34113ccda647Slclee 			return (EFAULT);
34123ccda647Slclee 		}
34133ccda647Slclee 		break;
34143ccda647Slclee 
34153ccda647Slclee 	case DDI_MODEL_NONE: {
34163ccda647Slclee 		struct vtoc user_vtoc;
34173ccda647Slclee 
3418e8fb11a1Sshidokht 		vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
34193ccda647Slclee 		if (ddi_copyout(&user_vtoc, (void *)arg,
34203ccda647Slclee 		    sizeof (struct vtoc), flag)) {
34213ccda647Slclee 			return (EFAULT);
34223ccda647Slclee 		}
34233ccda647Slclee 		break;
34243ccda647Slclee 	}
34253ccda647Slclee 	}
34263ccda647Slclee #else /* ! _MULTI_DATAMODEL */
3427e8fb11a1Sshidokht 	if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
34283ccda647Slclee 	    flag)) {
34293ccda647Slclee 		return (EFAULT);
34303ccda647Slclee 	}
34313ccda647Slclee #endif /* _MULTI_DATAMODEL */
34323ccda647Slclee #else
34333ccda647Slclee #error "No VTOC format defined."
34343ccda647Slclee #endif
34353ccda647Slclee 
34363ccda647Slclee 	return (rval);
34373ccda647Slclee }
34383ccda647Slclee 
3439342440ecSPrasad Singamsetty 
3440342440ecSPrasad Singamsetty /*
3441342440ecSPrasad Singamsetty  *    Function: cmlb_dkio_get_extvtoc
3442342440ecSPrasad Singamsetty  */
3443342440ecSPrasad Singamsetty static int
3444342440ecSPrasad Singamsetty cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
3445342440ecSPrasad Singamsetty     void *tg_cookie)
3446342440ecSPrasad Singamsetty {
3447342440ecSPrasad Singamsetty 	struct extvtoc	ext_vtoc;
3448342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
3449342440ecSPrasad Singamsetty 	struct vtoc	user_vtoc;
3450342440ecSPrasad Singamsetty #endif	/* defined(_SUNOS_VTOC_8) */
3451342440ecSPrasad Singamsetty 	int		rval = 0;
3452342440ecSPrasad Singamsetty 
3453342440ecSPrasad Singamsetty 	bzero(&ext_vtoc, sizeof (struct extvtoc));
3454342440ecSPrasad Singamsetty 	mutex_enter(CMLB_MUTEX(cl));
34557f0b8309SEdward Pilatowicz 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3456342440ecSPrasad Singamsetty 
3457342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
3458342440ecSPrasad Singamsetty 	if (rval == EINVAL &&
3459342440ecSPrasad Singamsetty 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
3460342440ecSPrasad Singamsetty 		/*
3461342440ecSPrasad Singamsetty 		 * This is to return a default label even when we do not
3462342440ecSPrasad Singamsetty 		 * really assume a default label for the device.
3463342440ecSPrasad Singamsetty 		 * dad driver utilizes this.
3464342440ecSPrasad Singamsetty 		 */
3465342440ecSPrasad Singamsetty 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3466342440ecSPrasad Singamsetty 			cmlb_setup_default_geometry(cl, tg_cookie);
3467342440ecSPrasad Singamsetty 			rval = 0;
3468342440ecSPrasad Singamsetty 		}
3469342440ecSPrasad Singamsetty 	}
3470342440ecSPrasad Singamsetty #endif
3471342440ecSPrasad Singamsetty 	if (rval) {
3472342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
3473342440ecSPrasad Singamsetty 		return (rval);
3474342440ecSPrasad Singamsetty 	}
3475342440ecSPrasad Singamsetty 
3476342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
3477342440ecSPrasad Singamsetty 	cmlb_build_user_vtoc(cl, &user_vtoc);
3478342440ecSPrasad Singamsetty 	mutex_exit(CMLB_MUTEX(cl));
3479342440ecSPrasad Singamsetty 
3480342440ecSPrasad Singamsetty 	/*
3481342440ecSPrasad Singamsetty 	 * Checking callers data model does not make much sense here
3482342440ecSPrasad Singamsetty 	 * since extvtoc will always be equivalent to 64bit vtoc.
3483342440ecSPrasad Singamsetty 	 * What is important is whether the kernel is in 32 or 64 bit
3484342440ecSPrasad Singamsetty 	 */
3485342440ecSPrasad Singamsetty 
3486342440ecSPrasad Singamsetty #ifdef _LP64
3487342440ecSPrasad Singamsetty 		if (ddi_copyout(&user_vtoc, (void *)arg,
3488342440ecSPrasad Singamsetty 		    sizeof (struct extvtoc), flag)) {
3489342440ecSPrasad Singamsetty 			return (EFAULT);
3490342440ecSPrasad Singamsetty 		}
3491342440ecSPrasad Singamsetty #else
3492342440ecSPrasad Singamsetty 		vtoc32tovtoc(user_vtoc, ext_vtoc);
3493342440ecSPrasad Singamsetty 		if (ddi_copyout(&ext_vtoc, (void *)arg,
3494342440ecSPrasad Singamsetty 		    sizeof (struct extvtoc), flag)) {
3495342440ecSPrasad Singamsetty 			return (EFAULT);
3496342440ecSPrasad Singamsetty 		}
3497342440ecSPrasad Singamsetty #endif
3498342440ecSPrasad Singamsetty 
3499342440ecSPrasad Singamsetty #elif defined(_SUNOS_VTOC_16)
3500342440ecSPrasad Singamsetty 	/*
3501342440ecSPrasad Singamsetty 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
3502342440ecSPrasad Singamsetty 	 * 32-bit to maintain compatibility with existing on-disk
3503342440ecSPrasad Singamsetty 	 * structures.  Thus, we need to convert the structure when copying
3504342440ecSPrasad Singamsetty 	 * it out to extvtoc
3505342440ecSPrasad Singamsetty 	 */
3506342440ecSPrasad Singamsetty 	vtoc32tovtoc(cl->cl_vtoc, ext_vtoc);
35071fad2c0eSXiao Raymond Li 	mutex_exit(CMLB_MUTEX(cl));
3508342440ecSPrasad Singamsetty 
3509342440ecSPrasad Singamsetty 	if (ddi_copyout(&ext_vtoc, (void *)arg, sizeof (struct extvtoc), flag))
3510342440ecSPrasad Singamsetty 		return (EFAULT);
3511342440ecSPrasad Singamsetty #else
3512342440ecSPrasad Singamsetty #error "No VTOC format defined."
3513342440ecSPrasad Singamsetty #endif
3514342440ecSPrasad Singamsetty 
3515342440ecSPrasad Singamsetty 	return (rval);
3516342440ecSPrasad Singamsetty }
35173ccda647Slclee static int
3518e8fb11a1Sshidokht cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
35193ccda647Slclee {
35203ccda647Slclee 	dk_efi_t	user_efi;
35213ccda647Slclee 	int		rval = 0;
35223ccda647Slclee 	void		*buffer;
3523e8fb11a1Sshidokht 	diskaddr_t	tgt_lba;
35243ccda647Slclee 
35253ccda647Slclee 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
35263ccda647Slclee 		return (EFAULT);
35273ccda647Slclee 
35283ccda647Slclee 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
35293ccda647Slclee 
3530e8fb11a1Sshidokht 	tgt_lba = user_efi.dki_lba;
3531e8fb11a1Sshidokht 
3532e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3533e8fb11a1Sshidokht 	if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
3534e8fb11a1Sshidokht 	    (cl->cl_tgt_blocksize == 0)) {
3535e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
3536e8fb11a1Sshidokht 		return (EINVAL);
3537e8fb11a1Sshidokht 	}
3538e8fb11a1Sshidokht 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
3539e8fb11a1Sshidokht 		tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
3540e8fb11a1Sshidokht 		    cl->cl_sys_blocksize;
3541e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3542e8fb11a1Sshidokht 
35433ccda647Slclee 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
3544e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
35453ccda647Slclee 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
35463ccda647Slclee 	    user_efi.dki_length, flag) != 0)
35473ccda647Slclee 		rval = EFAULT;
35483ccda647Slclee 
35493ccda647Slclee 	kmem_free(buffer, user_efi.dki_length);
35503ccda647Slclee 	return (rval);
35513ccda647Slclee }
35523ccda647Slclee 
3553e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
35543ccda647Slclee /*
35553ccda647Slclee  *    Function: cmlb_build_user_vtoc
35563ccda647Slclee  *
35573ccda647Slclee  * Description: This routine populates a pass by reference variable with the
35583ccda647Slclee  *		current volume table of contents.
35593ccda647Slclee  *
3560e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
35613ccda647Slclee  *		user_vtoc - pointer to vtoc structure to be populated
35623ccda647Slclee  */
35633ccda647Slclee static void
3564e8fb11a1Sshidokht cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
35653ccda647Slclee {
35663ccda647Slclee 	struct dk_map2		*lpart;
35673ccda647Slclee 	struct dk_map		*lmap;
35683ccda647Slclee 	struct partition	*vpart;
3569342440ecSPrasad Singamsetty 	uint32_t		nblks;
35703ccda647Slclee 	int			i;
35713ccda647Slclee 
3572e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
35733ccda647Slclee 
35743ccda647Slclee 	/*
35753ccda647Slclee 	 * Return vtoc structure fields in the provided VTOC area, addressed
35763ccda647Slclee 	 * by *vtoc.
35773ccda647Slclee 	 */
35783ccda647Slclee 	bzero(user_vtoc, sizeof (struct vtoc));
3579e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
3580e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
3581e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
35823ccda647Slclee 	user_vtoc->v_sanity	= VTOC_SANE;
3583e8fb11a1Sshidokht 	user_vtoc->v_version	= cl->cl_vtoc.v_version;
3584e8fb11a1Sshidokht 	bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
3585e8fb11a1Sshidokht 	user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
3586e8fb11a1Sshidokht 	user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
35873ccda647Slclee 
35883ccda647Slclee 	for (i = 0; i < 10; i++)
3589e8fb11a1Sshidokht 		user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
35903ccda647Slclee 
35913ccda647Slclee 	/*
35923ccda647Slclee 	 * Convert partitioning information.
35933ccda647Slclee 	 *
35943ccda647Slclee 	 * Note the conversion from starting cylinder number
35953ccda647Slclee 	 * to starting sector number.
35963ccda647Slclee 	 */
3597e8fb11a1Sshidokht 	lmap = cl->cl_map;
3598e8fb11a1Sshidokht 	lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
35993ccda647Slclee 	vpart = user_vtoc->v_part;
36003ccda647Slclee 
3601e8fb11a1Sshidokht 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
36023ccda647Slclee 
36033ccda647Slclee 	for (i = 0; i < V_NUMPAR; i++) {
36043ccda647Slclee 		vpart->p_tag	= lpart->p_tag;
36053ccda647Slclee 		vpart->p_flag	= lpart->p_flag;
36063ccda647Slclee 		vpart->p_start	= lmap->dkl_cylno * nblks;
36073ccda647Slclee 		vpart->p_size	= lmap->dkl_nblk;
36083ccda647Slclee 		lmap++;
36093ccda647Slclee 		lpart++;
36103ccda647Slclee 		vpart++;
36113ccda647Slclee 
36123ccda647Slclee 		/* (4364927) */
3613e8fb11a1Sshidokht 		user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
36143ccda647Slclee 	}
36153ccda647Slclee 
3616e8fb11a1Sshidokht 	bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
36173ccda647Slclee }
3618e8fb11a1Sshidokht #endif
36193ccda647Slclee 
36203ccda647Slclee static int
3621e8fb11a1Sshidokht cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3622e8fb11a1Sshidokht     void *tg_cookie)
36233ccda647Slclee {
36243ccda647Slclee 	struct partition64	p64;
36253ccda647Slclee 	int			rval = 0;
36263ccda647Slclee 	uint_t			nparts;
36273ccda647Slclee 	efi_gpe_t		*partitions;
36283ccda647Slclee 	efi_gpt_t		*buffer;
36293ccda647Slclee 	diskaddr_t		gpe_lba;
36303ccda647Slclee 
36313ccda647Slclee 	if (ddi_copyin((const void *)arg, &p64,
36323ccda647Slclee 	    sizeof (struct partition64), flag)) {
36333ccda647Slclee 		return (EFAULT);
36343ccda647Slclee 	}
36353ccda647Slclee 
36363ccda647Slclee 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
3637*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	rval = DK_TG_READ(cl, buffer, 1, cl->cl_sys_blocksize, tg_cookie);
36383ccda647Slclee 	if (rval != 0)
36393ccda647Slclee 		goto done_error;
36403ccda647Slclee 
36413ccda647Slclee 	cmlb_swap_efi_gpt(buffer);
36423ccda647Slclee 
36433ccda647Slclee 	if ((rval = cmlb_validate_efi(buffer)) != 0)
36443ccda647Slclee 		goto done_error;
36453ccda647Slclee 
36463ccda647Slclee 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
36473ccda647Slclee 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
36483ccda647Slclee 	if (p64.p_partno > nparts) {
36493ccda647Slclee 		/* couldn't find it */
36503ccda647Slclee 		rval = ESRCH;
36513ccda647Slclee 		goto done_error;
36523ccda647Slclee 	}
36533ccda647Slclee 	/*
36543ccda647Slclee 	 * if we're dealing with a partition that's out of the normal
36553ccda647Slclee 	 * 16K block, adjust accordingly
36563ccda647Slclee 	 */
36573ccda647Slclee 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
3658e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buffer, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
36593ccda647Slclee 
36603ccda647Slclee 	if (rval) {
36613ccda647Slclee 		goto done_error;
36623ccda647Slclee 	}
36633ccda647Slclee 	partitions = (efi_gpe_t *)buffer;
36643ccda647Slclee 
36653ccda647Slclee 	cmlb_swap_efi_gpe(nparts, partitions);
36663ccda647Slclee 
36673ccda647Slclee 	partitions += p64.p_partno;
36683ccda647Slclee 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
36693ccda647Slclee 	    sizeof (struct uuid));
36703ccda647Slclee 	p64.p_start = partitions->efi_gpe_StartingLBA;
36713ccda647Slclee 	p64.p_size = partitions->efi_gpe_EndingLBA -
36723ccda647Slclee 	    p64.p_start + 1;
36733ccda647Slclee 
36743ccda647Slclee 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
36753ccda647Slclee 		rval = EFAULT;
36763ccda647Slclee 
36773ccda647Slclee done_error:
36783ccda647Slclee 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
36793ccda647Slclee 	return (rval);
36803ccda647Slclee }
36813ccda647Slclee 
36823ccda647Slclee 
36833ccda647Slclee /*
36843ccda647Slclee  *    Function: cmlb_dkio_set_vtoc
36853ccda647Slclee  *
36863ccda647Slclee  * Description: This routine is the driver entry point for handling user
36873ccda647Slclee  *		requests to set the current volume table of contents
36883ccda647Slclee  *		(DKIOCSVTOC).
36893ccda647Slclee  *
3690e8fb11a1Sshidokht  *   Arguments:
3691e8fb11a1Sshidokht  *	dev		the device number
3692e8fb11a1Sshidokht  *	arg		pointer to user provided vtoc structure used to set the
36933ccda647Slclee  *			current vtoc.
3694e8fb11a1Sshidokht  *
3695e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
36963ccda647Slclee  *			directly from the mode argument of ioctl().
36973ccda647Slclee  *
3698e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3699e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3700e8fb11a1Sshidokht  *
37013ccda647Slclee  * Return Code: 0
37023ccda647Slclee  *		EFAULT
37033ccda647Slclee  *		ENXIO
37043ccda647Slclee  *		EINVAL
37053ccda647Slclee  *		ENOTSUP
37063ccda647Slclee  */
37073ccda647Slclee static int
3708e8fb11a1Sshidokht cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3709e8fb11a1Sshidokht     void *tg_cookie)
37103ccda647Slclee {
37113ccda647Slclee 	struct vtoc	user_vtoc;
37123ccda647Slclee 	int		rval = 0;
371306bbe1e0Sedp 	boolean_t	internal;
371406bbe1e0Sedp 
37157f0b8309SEdward Pilatowicz 	internal = VOID2BOOLEAN(
37167f0b8309SEdward Pilatowicz 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
37173ccda647Slclee 
37183ccda647Slclee #ifdef _MULTI_DATAMODEL
37193ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
37203ccda647Slclee 	case DDI_MODEL_ILP32: {
37213ccda647Slclee 		struct vtoc32 user_vtoc32;
37223ccda647Slclee 
37233ccda647Slclee 		if (ddi_copyin((const void *)arg, &user_vtoc32,
37243ccda647Slclee 		    sizeof (struct vtoc32), flag)) {
37253ccda647Slclee 			return (EFAULT);
37263ccda647Slclee 		}
37273ccda647Slclee 		vtoc32tovtoc(user_vtoc32, user_vtoc);
37283ccda647Slclee 		break;
37293ccda647Slclee 	}
37303ccda647Slclee 
37313ccda647Slclee 	case DDI_MODEL_NONE:
37323ccda647Slclee 		if (ddi_copyin((const void *)arg, &user_vtoc,
37333ccda647Slclee 		    sizeof (struct vtoc), flag)) {
37343ccda647Slclee 			return (EFAULT);
37353ccda647Slclee 		}
37363ccda647Slclee 		break;
37373ccda647Slclee 	}
37383ccda647Slclee #else /* ! _MULTI_DATAMODEL */
37393ccda647Slclee 	if (ddi_copyin((const void *)arg, &user_vtoc,
37403ccda647Slclee 	    sizeof (struct vtoc), flag)) {
37413ccda647Slclee 		return (EFAULT);
37423ccda647Slclee 	}
37433ccda647Slclee #endif /* _MULTI_DATAMODEL */
37443ccda647Slclee 
3745e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3746342440ecSPrasad Singamsetty 
3747342440ecSPrasad Singamsetty 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3748e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
3749342440ecSPrasad Singamsetty 		return (EOVERFLOW);
37503ccda647Slclee 	}
3751e8fb11a1Sshidokht 
3752e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
3753e8fb11a1Sshidokht 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
3754e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
3755e8fb11a1Sshidokht 		return (EINVAL);
3756e8fb11a1Sshidokht 	}
3757e8fb11a1Sshidokht #endif
3758e8fb11a1Sshidokht 
3759e8fb11a1Sshidokht 	if (cl->cl_g.dkg_ncyl == 0) {
3760e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
37613ccda647Slclee 		return (EINVAL);
37623ccda647Slclee 	}
37633ccda647Slclee 
3764e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3765e8fb11a1Sshidokht 	cmlb_clear_efi(cl, tg_cookie);
3766e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
3767e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
376848794f22SSriram Popuri 
376948794f22SSriram Popuri 	/*
377048794f22SSriram Popuri 	 * cmlb_dkio_set_vtoc creates duplicate minor nodes when
377148794f22SSriram Popuri 	 * relabeling an SMI disk. To avoid that we remove them
377248794f22SSriram Popuri 	 * before creating.
377348794f22SSriram Popuri 	 * It should be OK to remove a non-existed minor node.
377448794f22SSriram Popuri 	 */
377548794f22SSriram Popuri 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
377648794f22SSriram Popuri 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
377748794f22SSriram Popuri 
377806bbe1e0Sedp 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
37793ccda647Slclee 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
378006bbe1e0Sedp 	    cl->cl_node_type, NULL, internal);
378106bbe1e0Sedp 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
37823ccda647Slclee 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
378306bbe1e0Sedp 	    cl->cl_node_type, NULL, internal);
3784e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
37853ccda647Slclee 
3786e8fb11a1Sshidokht 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
3787e8fb11a1Sshidokht 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
37887f0b8309SEdward Pilatowicz 			if (cmlb_validate_geometry(cl,
37897f0b8309SEdward Pilatowicz 			    B_TRUE, 0, tg_cookie) != 0) {
3790e8fb11a1Sshidokht 				cmlb_dbg(CMLB_ERROR, cl,
37913ccda647Slclee 				    "cmlb_dkio_set_vtoc: "
37923ccda647Slclee 				    "Failed validate geometry\n");
37933ccda647Slclee 			}
3794342440ecSPrasad Singamsetty 			cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
37953ccda647Slclee 		}
37963ccda647Slclee 	}
3797e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
37983ccda647Slclee 	return (rval);
37993ccda647Slclee }
38003ccda647Slclee 
3801342440ecSPrasad Singamsetty /*
3802342440ecSPrasad Singamsetty  *    Function: cmlb_dkio_set_extvtoc
3803342440ecSPrasad Singamsetty  */
3804342440ecSPrasad Singamsetty static int
3805342440ecSPrasad Singamsetty cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3806342440ecSPrasad Singamsetty     void *tg_cookie)
3807342440ecSPrasad Singamsetty {
3808342440ecSPrasad Singamsetty 	int		rval = 0;
3809342440ecSPrasad Singamsetty 	struct vtoc	user_vtoc;
38107e70b434SShidokht Yadegari 	boolean_t	internal;
38117e70b434SShidokht Yadegari 
3812342440ecSPrasad Singamsetty 
3813342440ecSPrasad Singamsetty 	/*
3814342440ecSPrasad Singamsetty 	 * Checking callers data model does not make much sense here
3815342440ecSPrasad Singamsetty 	 * since extvtoc will always be equivalent to 64bit vtoc.
3816342440ecSPrasad Singamsetty 	 * What is important is whether the kernel is in 32 or 64 bit
3817342440ecSPrasad Singamsetty 	 */
3818342440ecSPrasad Singamsetty 
3819342440ecSPrasad Singamsetty #ifdef _LP64
3820342440ecSPrasad Singamsetty 	if (ddi_copyin((const void *)arg, &user_vtoc,
3821342440ecSPrasad Singamsetty 		    sizeof (struct extvtoc), flag)) {
3822342440ecSPrasad Singamsetty 			return (EFAULT);
3823342440ecSPrasad Singamsetty 	}
3824342440ecSPrasad Singamsetty #else
3825342440ecSPrasad Singamsetty 	struct	extvtoc	user_extvtoc;
3826342440ecSPrasad Singamsetty 	if (ddi_copyin((const void *)arg, &user_extvtoc,
3827342440ecSPrasad Singamsetty 		    sizeof (struct extvtoc), flag)) {
3828342440ecSPrasad Singamsetty 			return (EFAULT);
3829342440ecSPrasad Singamsetty 	}
3830342440ecSPrasad Singamsetty 
3831342440ecSPrasad Singamsetty 	vtoctovtoc32(user_extvtoc, user_vtoc);
3832342440ecSPrasad Singamsetty #endif
3833342440ecSPrasad Singamsetty 
38347f0b8309SEdward Pilatowicz 	internal = VOID2BOOLEAN(
38357f0b8309SEdward Pilatowicz 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
3836342440ecSPrasad Singamsetty 	mutex_enter(CMLB_MUTEX(cl));
3837342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
3838342440ecSPrasad Singamsetty 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
3839342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
3840342440ecSPrasad Singamsetty 		return (EINVAL);
3841342440ecSPrasad Singamsetty 	}
3842342440ecSPrasad Singamsetty #endif
3843342440ecSPrasad Singamsetty 
3844342440ecSPrasad Singamsetty 	if (cl->cl_g.dkg_ncyl == 0) {
3845342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
3846342440ecSPrasad Singamsetty 		return (EINVAL);
3847342440ecSPrasad Singamsetty 	}
3848342440ecSPrasad Singamsetty 
3849342440ecSPrasad Singamsetty 	mutex_exit(CMLB_MUTEX(cl));
3850342440ecSPrasad Singamsetty 	cmlb_clear_efi(cl, tg_cookie);
3851342440ecSPrasad Singamsetty 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
3852342440ecSPrasad Singamsetty 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
38537e70b434SShidokht Yadegari 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
3854342440ecSPrasad Singamsetty 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
38557e70b434SShidokht Yadegari 	    cl->cl_node_type, NULL, internal);
38567e70b434SShidokht Yadegari 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
3857342440ecSPrasad Singamsetty 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
38587e70b434SShidokht Yadegari 	    cl->cl_node_type, NULL, internal);
38597e70b434SShidokht Yadegari 
3860342440ecSPrasad Singamsetty 	mutex_enter(CMLB_MUTEX(cl));
3861342440ecSPrasad Singamsetty 
3862342440ecSPrasad Singamsetty 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
3863342440ecSPrasad Singamsetty 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
38647f0b8309SEdward Pilatowicz 			if (cmlb_validate_geometry(cl,
38657f0b8309SEdward Pilatowicz 			    B_TRUE, 0, tg_cookie) != 0) {
3866342440ecSPrasad Singamsetty 				cmlb_dbg(CMLB_ERROR, cl,
3867342440ecSPrasad Singamsetty 				    "cmlb_dkio_set_vtoc: "
3868342440ecSPrasad Singamsetty 				    "Failed validate geometry\n");
3869342440ecSPrasad Singamsetty 			}
3870342440ecSPrasad Singamsetty 		}
3871342440ecSPrasad Singamsetty 	}
3872342440ecSPrasad Singamsetty 	mutex_exit(CMLB_MUTEX(cl));
3873342440ecSPrasad Singamsetty 	return (rval);
3874342440ecSPrasad Singamsetty }
38753ccda647Slclee 
38763ccda647Slclee /*
38773ccda647Slclee  *    Function: cmlb_build_label_vtoc
38783ccda647Slclee  *
38793ccda647Slclee  * Description: This routine updates the driver soft state current volume table
38803ccda647Slclee  *		of contents based on a user specified vtoc.
38813ccda647Slclee  *
3882e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
38833ccda647Slclee  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
38843ccda647Slclee  *			    to update the driver soft state.
38853ccda647Slclee  *
38863ccda647Slclee  * Return Code: 0
38873ccda647Slclee  *		EINVAL
38883ccda647Slclee  */
38893ccda647Slclee static int
3890e8fb11a1Sshidokht cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
38913ccda647Slclee {
38923ccda647Slclee 	struct dk_map		*lmap;
38933ccda647Slclee 	struct partition	*vpart;
3894342440ecSPrasad Singamsetty 	uint_t			nblks;
38953ccda647Slclee #if defined(_SUNOS_VTOC_8)
38963ccda647Slclee 	int			ncyl;
38973ccda647Slclee 	struct dk_map2		*lpart;
38983ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
38993ccda647Slclee 	int			i;
39003ccda647Slclee 
3901e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
39023ccda647Slclee 
39033ccda647Slclee 	/* Sanity-check the vtoc */
39043ccda647Slclee 	if (user_vtoc->v_sanity != VTOC_SANE ||
3905e8fb11a1Sshidokht 	    user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
39063ccda647Slclee 	    user_vtoc->v_nparts != V_NUMPAR) {
3907e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
39083ccda647Slclee 		    "cmlb_build_label_vtoc: vtoc not valid\n");
39093ccda647Slclee 		return (EINVAL);
39103ccda647Slclee 	}
39113ccda647Slclee 
3912e8fb11a1Sshidokht 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
39133ccda647Slclee 	if (nblks == 0) {
3914e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
39153ccda647Slclee 		    "cmlb_build_label_vtoc: geom nblks is 0\n");
39163ccda647Slclee 		return (EINVAL);
39173ccda647Slclee 	}
39183ccda647Slclee 
39193ccda647Slclee #if defined(_SUNOS_VTOC_8)
39203ccda647Slclee 	vpart = user_vtoc->v_part;
39213ccda647Slclee 	for (i = 0; i < V_NUMPAR; i++) {
3922342440ecSPrasad Singamsetty 		if (((unsigned)vpart->p_start % nblks) != 0) {
3923e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
39243ccda647Slclee 			    "cmlb_build_label_vtoc: p_start not multiply of"
39253ccda647Slclee 			    "nblks part %d p_start %d nblks %d\n", i,
39263ccda647Slclee 			    vpart->p_start, nblks);
39273ccda647Slclee 			return (EINVAL);
39283ccda647Slclee 		}
3929342440ecSPrasad Singamsetty 		ncyl = (unsigned)vpart->p_start / nblks;
3930342440ecSPrasad Singamsetty 		ncyl += (unsigned)vpart->p_size / nblks;
3931342440ecSPrasad Singamsetty 		if (((unsigned)vpart->p_size % nblks) != 0) {
39323ccda647Slclee 			ncyl++;
39333ccda647Slclee 		}
3934e8fb11a1Sshidokht 		if (ncyl > (int)cl->cl_g.dkg_ncyl) {
3935e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
39363ccda647Slclee 			    "cmlb_build_label_vtoc: ncyl %d  > dkg_ncyl %d"
39373ccda647Slclee 			    "p_size %ld p_start %ld nblks %d  part number %d"
39383ccda647Slclee 			    "tag %d\n",
3939e8fb11a1Sshidokht 			    ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
39403ccda647Slclee 			    vpart->p_start, nblks,
39413ccda647Slclee 			    i, vpart->p_tag);
39423ccda647Slclee 
39433ccda647Slclee 			return (EINVAL);
39443ccda647Slclee 		}
39453ccda647Slclee 		vpart++;
39463ccda647Slclee 	}
39473ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
39483ccda647Slclee 
39493ccda647Slclee 	/* Put appropriate vtoc structure fields into the disk label */
39503ccda647Slclee #if defined(_SUNOS_VTOC_16)
39513ccda647Slclee 	/*
39523ccda647Slclee 	 * The vtoc is always a 32bit data structure to maintain the
39533ccda647Slclee 	 * on-disk format. Convert "in place" instead of doing bcopy.
39543ccda647Slclee 	 */
3955e8fb11a1Sshidokht 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
39563ccda647Slclee 
39573ccda647Slclee 	/*
39583ccda647Slclee 	 * in the 16-slice vtoc, starting sectors are expressed in
39593ccda647Slclee 	 * numbers *relative* to the start of the Solaris fdisk partition.
39603ccda647Slclee 	 */
3961e8fb11a1Sshidokht 	lmap = cl->cl_map;
39623ccda647Slclee 	vpart = user_vtoc->v_part;
39633ccda647Slclee 
39643ccda647Slclee 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
3965342440ecSPrasad Singamsetty 		lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
3966342440ecSPrasad Singamsetty 		lmap->dkl_nblk = (unsigned)vpart->p_size;
39673ccda647Slclee 	}
39683ccda647Slclee 
39693ccda647Slclee #elif defined(_SUNOS_VTOC_8)
39703ccda647Slclee 
3971e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
3972e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
3973e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
39743ccda647Slclee 
3975e8fb11a1Sshidokht 	cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
3976e8fb11a1Sshidokht 	cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
39773ccda647Slclee 
3978e8fb11a1Sshidokht 	bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
39793ccda647Slclee 
3980e8fb11a1Sshidokht 	cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
39813ccda647Slclee 
39823ccda647Slclee 	for (i = 0; i < 10; i++)
3983e8fb11a1Sshidokht 		cl->cl_vtoc.v_reserved[i] =  user_vtoc->v_reserved[i];
39843ccda647Slclee 
39853ccda647Slclee 	/*
39863ccda647Slclee 	 * Note the conversion from starting sector number
39873ccda647Slclee 	 * to starting cylinder number.
39883ccda647Slclee 	 * Return error if division results in a remainder.
39893ccda647Slclee 	 */
3990e8fb11a1Sshidokht 	lmap = cl->cl_map;
3991e8fb11a1Sshidokht 	lpart = cl->cl_vtoc.v_part;
39923ccda647Slclee 	vpart = user_vtoc->v_part;
39933ccda647Slclee 
39943ccda647Slclee 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
39953ccda647Slclee 		lpart->p_tag  = vpart->p_tag;
39963ccda647Slclee 		lpart->p_flag = vpart->p_flag;
3997342440ecSPrasad Singamsetty 		lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
3998342440ecSPrasad Singamsetty 		lmap->dkl_nblk = (unsigned)vpart->p_size;
39993ccda647Slclee 
40003ccda647Slclee 		lmap++;
40013ccda647Slclee 		lpart++;
40023ccda647Slclee 		vpart++;
40033ccda647Slclee 
40043ccda647Slclee 		/* (4387723) */
40053ccda647Slclee #ifdef _LP64
40063ccda647Slclee 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
4007e8fb11a1Sshidokht 			cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
40083ccda647Slclee 		} else {
4009e8fb11a1Sshidokht 			cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
40103ccda647Slclee 		}
40113ccda647Slclee #else
4012e8fb11a1Sshidokht 		cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
40133ccda647Slclee #endif
40143ccda647Slclee 	}
40153ccda647Slclee 
4016e8fb11a1Sshidokht 	bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
40173ccda647Slclee #else
40183ccda647Slclee #error "No VTOC format defined."
40193ccda647Slclee #endif
40203ccda647Slclee 	return (0);
40213ccda647Slclee }
40223ccda647Slclee 
40233ccda647Slclee /*
40243ccda647Slclee  *    Function: cmlb_clear_efi
40253ccda647Slclee  *
40263ccda647Slclee  * Description: This routine clears all EFI labels.
40273ccda647Slclee  *
4028e8fb11a1Sshidokht  *   Arguments:
4029e8fb11a1Sshidokht  *	cl		 driver soft state (unit) structure
40303ccda647Slclee  *
4031e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4032e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
40333ccda647Slclee  * Return Code: void
40343ccda647Slclee  */
40353ccda647Slclee static void
4036e8fb11a1Sshidokht cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
40373ccda647Slclee {
40383ccda647Slclee 	efi_gpt_t	*gpt;
40393ccda647Slclee 	diskaddr_t	cap;
40403ccda647Slclee 	int		rval;
40413ccda647Slclee 
4042e8fb11a1Sshidokht 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4043e8fb11a1Sshidokht 
4044e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
4045e8fb11a1Sshidokht 	cl->cl_reserved = -1;
4046e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
40473ccda647Slclee 
4048*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	gpt = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
40493ccda647Slclee 
4050*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (DK_TG_READ(cl, gpt, 1, cl->cl_sys_blocksize, tg_cookie) != 0) {
40513ccda647Slclee 		goto done;
40523ccda647Slclee 	}
40533ccda647Slclee 
40543ccda647Slclee 	cmlb_swap_efi_gpt(gpt);
40553ccda647Slclee 	rval = cmlb_validate_efi(gpt);
40563ccda647Slclee 	if (rval == 0) {
40573ccda647Slclee 		/* clear primary */
40583ccda647Slclee 		bzero(gpt, sizeof (efi_gpt_t));
4059*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		if (rval = DK_TG_WRITE(cl, gpt, 1, cl->cl_sys_blocksize,
4060*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		    tg_cookie)) {
4061e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
40623ccda647Slclee 			    "cmlb_clear_efi: clear primary label failed\n");
40633ccda647Slclee 		}
40643ccda647Slclee 	}
40653ccda647Slclee 	/* the backup */
4066e8fb11a1Sshidokht 	rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
40673ccda647Slclee 	if (rval) {
40683ccda647Slclee 		goto done;
40693ccda647Slclee 	}
40703ccda647Slclee 
4071*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	if ((rval = DK_TG_READ(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4072*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	    tg_cookie)) != 0) {
40733ccda647Slclee 		goto done;
40743ccda647Slclee 	}
40753ccda647Slclee 	cmlb_swap_efi_gpt(gpt);
40763ccda647Slclee 	rval = cmlb_validate_efi(gpt);
40773ccda647Slclee 	if (rval == 0) {
40783ccda647Slclee 		/* clear backup */
4079e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE,  cl,
40803ccda647Slclee 		    "cmlb_clear_efi clear backup@%lu\n", cap - 1);
40813ccda647Slclee 		bzero(gpt, sizeof (efi_gpt_t));
4082*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		if ((rval = DK_TG_WRITE(cl,  gpt, cap - 1, cl->cl_sys_blocksize,
4083e8fb11a1Sshidokht 		    tg_cookie))) {
4084e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
40853ccda647Slclee 			    "cmlb_clear_efi: clear backup label failed\n");
40863ccda647Slclee 		}
4087e8fb11a1Sshidokht 	} else {
4088e8fb11a1Sshidokht 		/*
4089e8fb11a1Sshidokht 		 * Refer to comments related to off-by-1 at the
4090e8fb11a1Sshidokht 		 * header of this file
4091e8fb11a1Sshidokht 		 */
4092e8fb11a1Sshidokht 		if ((rval = DK_TG_READ(cl, gpt, cap - 2,
4093*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		    cl->cl_sys_blocksize, tg_cookie)) != 0) {
4094e8fb11a1Sshidokht 			goto done;
4095e8fb11a1Sshidokht 		}
4096e8fb11a1Sshidokht 		cmlb_swap_efi_gpt(gpt);
4097e8fb11a1Sshidokht 		rval = cmlb_validate_efi(gpt);
4098e8fb11a1Sshidokht 		if (rval == 0) {
4099e8fb11a1Sshidokht 			/* clear legacy backup EFI label */
4100e8fb11a1Sshidokht 			cmlb_dbg(CMLB_TRACE,  cl,
4101e8fb11a1Sshidokht 			    "cmlb_clear_efi clear legacy backup@%lu\n",
4102e8fb11a1Sshidokht 			    cap - 2);
4103e8fb11a1Sshidokht 			bzero(gpt, sizeof (efi_gpt_t));
4104e8fb11a1Sshidokht 			if ((rval = DK_TG_WRITE(cl,  gpt, cap - 2,
4105*65908c77Syu, larry liu - Sun Microsystems - Beijing China 			    cl->cl_sys_blocksize, tg_cookie))) {
4106e8fb11a1Sshidokht 				cmlb_dbg(CMLB_INFO,  cl,
4107e8fb11a1Sshidokht 				"cmlb_clear_efi: clear legacy backup label "
4108e8fb11a1Sshidokht 				"failed\n");
4109e8fb11a1Sshidokht 			}
4110e8fb11a1Sshidokht 		}
41113ccda647Slclee 	}
41123ccda647Slclee 
41133ccda647Slclee done:
4114*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	kmem_free(gpt, cl->cl_sys_blocksize);
41153ccda647Slclee }
41163ccda647Slclee 
41173ccda647Slclee /*
41183ccda647Slclee  *    Function: cmlb_set_vtoc
41193ccda647Slclee  *
41203ccda647Slclee  * Description: This routine writes data to the appropriate positions
41213ccda647Slclee  *
4122e8fb11a1Sshidokht  *   Arguments:
4123e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4124e8fb11a1Sshidokht  *
4125e8fb11a1Sshidokht  *	dkl		the data to be written
4126e8fb11a1Sshidokht  *
4127e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4128e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
41293ccda647Slclee  *
41303ccda647Slclee  * Return: void
41313ccda647Slclee  */
41323ccda647Slclee static int
4133e8fb11a1Sshidokht cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
41343ccda647Slclee {
41353ccda647Slclee 	uint_t	label_addr;
41363ccda647Slclee 	int	sec;
4137342440ecSPrasad Singamsetty 	diskaddr_t	blk;
41383ccda647Slclee 	int	head;
41393ccda647Slclee 	int	cyl;
41403ccda647Slclee 	int	rval;
41413ccda647Slclee 
41423ccda647Slclee #if defined(__i386) || defined(__amd64)
4143e8fb11a1Sshidokht 	label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
41443ccda647Slclee #else
41453ccda647Slclee 	/* Write the primary label at block 0 of the solaris partition. */
41463ccda647Slclee 	label_addr = 0;
41473ccda647Slclee #endif
41483ccda647Slclee 
4149e8fb11a1Sshidokht 	rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
4150e8fb11a1Sshidokht 	    tg_cookie);
41513ccda647Slclee 
41523ccda647Slclee 	if (rval != 0) {
41533ccda647Slclee 		return (rval);
41543ccda647Slclee 	}
41553ccda647Slclee 
41563ccda647Slclee 	/*
41573ccda647Slclee 	 * Calculate where the backup labels go.  They are always on
41583ccda647Slclee 	 * the last alternate cylinder, but some older drives put them
41593ccda647Slclee 	 * on head 2 instead of the last head.	They are always on the
41603ccda647Slclee 	 * first 5 odd sectors of the appropriate track.
41613ccda647Slclee 	 *
41623ccda647Slclee 	 * We have no choice at this point, but to believe that the
41633ccda647Slclee 	 * disk label is valid.	 Use the geometry of the disk
41643ccda647Slclee 	 * as described in the label.
41653ccda647Slclee 	 */
41663ccda647Slclee 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
41673ccda647Slclee 	head = dkl->dkl_nhead - 1;
41683ccda647Slclee 
41693ccda647Slclee 	/*
41703ccda647Slclee 	 * Write and verify the backup labels. Make sure we don't try to
41713ccda647Slclee 	 * write past the last cylinder.
41723ccda647Slclee 	 */
41733ccda647Slclee 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
4174342440ecSPrasad Singamsetty 		blk = (diskaddr_t)(
41753ccda647Slclee 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
41763ccda647Slclee 		    (head * dkl->dkl_nsect) + sec);
41773ccda647Slclee #if defined(__i386) || defined(__amd64)
4178e8fb11a1Sshidokht 		blk += cl->cl_solaris_offset;
41793ccda647Slclee #endif
4180e8fb11a1Sshidokht 		rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
4181e8fb11a1Sshidokht 		    tg_cookie);
4182e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
4183342440ecSPrasad Singamsetty 		"cmlb_set_vtoc: wrote backup label %llx\n", blk);
41843ccda647Slclee 		if (rval != 0) {
41853ccda647Slclee 			goto exit;
41863ccda647Slclee 		}
41873ccda647Slclee 	}
41883ccda647Slclee exit:
41893ccda647Slclee 	return (rval);
41903ccda647Slclee }
41913ccda647Slclee 
41923ccda647Slclee /*
41933ccda647Slclee  *    Function: cmlb_clear_vtoc
41943ccda647Slclee  *
41953ccda647Slclee  * Description: This routine clears out the VTOC labels.
41963ccda647Slclee  *
4197e8fb11a1Sshidokht  *   Arguments:
4198e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4199e8fb11a1Sshidokht  *
4200e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4201e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
42023ccda647Slclee  *
42033ccda647Slclee  * Return: void
42043ccda647Slclee  */
42053ccda647Slclee static void
4206e8fb11a1Sshidokht cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
42073ccda647Slclee {
42083ccda647Slclee 	struct dk_label		*dkl;
42093ccda647Slclee 
4210e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
4211*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4212e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
42133ccda647Slclee 	/*
42143ccda647Slclee 	 * cmlb_set_vtoc uses these fields in order to figure out
42153ccda647Slclee 	 * where to overwrite the backup labels
42163ccda647Slclee 	 */
4217e8fb11a1Sshidokht 	dkl->dkl_apc    = cl->cl_g.dkg_apc;
4218e8fb11a1Sshidokht 	dkl->dkl_ncyl   = cl->cl_g.dkg_ncyl;
4219e8fb11a1Sshidokht 	dkl->dkl_acyl   = cl->cl_g.dkg_acyl;
4220e8fb11a1Sshidokht 	dkl->dkl_nhead  = cl->cl_g.dkg_nhead;
4221e8fb11a1Sshidokht 	dkl->dkl_nsect  = cl->cl_g.dkg_nsect;
4222e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
4223e8fb11a1Sshidokht 	(void) cmlb_set_vtoc(cl, dkl, tg_cookie);
4224*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	kmem_free(dkl, cl->cl_sys_blocksize);
42253ccda647Slclee 
4226e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
42273ccda647Slclee }
42283ccda647Slclee 
42293ccda647Slclee /*
42303ccda647Slclee  *    Function: cmlb_write_label
42313ccda647Slclee  *
42323ccda647Slclee  * Description: This routine will validate and write the driver soft state vtoc
42333ccda647Slclee  *		contents to the device.
42343ccda647Slclee  *
4235e8fb11a1Sshidokht  *   Arguments:
4236e8fb11a1Sshidokht  *	cl		cmlb handle
4237e8fb11a1Sshidokht  *
4238e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4239e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4240e8fb11a1Sshidokht  *
42413ccda647Slclee  *
42423ccda647Slclee  * Return Code: the code returned by cmlb_send_scsi_cmd()
42433ccda647Slclee  *		0
42443ccda647Slclee  *		EINVAL
42453ccda647Slclee  *		ENXIO
42463ccda647Slclee  *		ENOMEM
42473ccda647Slclee  */
42483ccda647Slclee static int
4249e8fb11a1Sshidokht cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
42503ccda647Slclee {
42513ccda647Slclee 	struct dk_label	*dkl;
42523ccda647Slclee 	short		sum;
42533ccda647Slclee 	short		*sp;
42543ccda647Slclee 	int		i;
42553ccda647Slclee 	int		rval;
42563ccda647Slclee 
4257e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4258e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
4259*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4260e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
42613ccda647Slclee 
4262e8fb11a1Sshidokht 	bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
4263e8fb11a1Sshidokht 	dkl->dkl_rpm	= cl->cl_g.dkg_rpm;
4264e8fb11a1Sshidokht 	dkl->dkl_pcyl	= cl->cl_g.dkg_pcyl;
4265e8fb11a1Sshidokht 	dkl->dkl_apc	= cl->cl_g.dkg_apc;
4266e8fb11a1Sshidokht 	dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
4267e8fb11a1Sshidokht 	dkl->dkl_ncyl	= cl->cl_g.dkg_ncyl;
4268e8fb11a1Sshidokht 	dkl->dkl_acyl	= cl->cl_g.dkg_acyl;
4269e8fb11a1Sshidokht 	dkl->dkl_nhead	= cl->cl_g.dkg_nhead;
4270e8fb11a1Sshidokht 	dkl->dkl_nsect	= cl->cl_g.dkg_nsect;
42713ccda647Slclee 
42723ccda647Slclee #if defined(_SUNOS_VTOC_8)
4273e8fb11a1Sshidokht 	dkl->dkl_obs1	= cl->cl_g.dkg_obs1;
4274e8fb11a1Sshidokht 	dkl->dkl_obs2	= cl->cl_g.dkg_obs2;
4275e8fb11a1Sshidokht 	dkl->dkl_obs3	= cl->cl_g.dkg_obs3;
42763ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
4277e8fb11a1Sshidokht 		dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
4278e8fb11a1Sshidokht 		dkl->dkl_map[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
42793ccda647Slclee 	}
4280e8fb11a1Sshidokht 	bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
42813ccda647Slclee #elif defined(_SUNOS_VTOC_16)
4282e8fb11a1Sshidokht 	dkl->dkl_skew	= cl->cl_dkg_skew;
42833ccda647Slclee #else
42843ccda647Slclee #error "No VTOC format defined."
42853ccda647Slclee #endif
42863ccda647Slclee 
42873ccda647Slclee 	dkl->dkl_magic			= DKL_MAGIC;
4288e8fb11a1Sshidokht 	dkl->dkl_write_reinstruct	= cl->cl_g.dkg_write_reinstruct;
4289e8fb11a1Sshidokht 	dkl->dkl_read_reinstruct	= cl->cl_g.dkg_read_reinstruct;
42903ccda647Slclee 
42913ccda647Slclee 	/* Construct checksum for the new disk label */
42923ccda647Slclee 	sum = 0;
42933ccda647Slclee 	sp = (short *)dkl;
42943ccda647Slclee 	i = sizeof (struct dk_label) / sizeof (short);
42953ccda647Slclee 	while (i--) {
42963ccda647Slclee 		sum ^= *sp++;
42973ccda647Slclee 	}
42983ccda647Slclee 	dkl->dkl_cksum = sum;
42993ccda647Slclee 
4300e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
43013ccda647Slclee 
4302e8fb11a1Sshidokht 	rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
43033ccda647Slclee exit:
4304*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	kmem_free(dkl, cl->cl_sys_blocksize);
4305e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
43063ccda647Slclee 	return (rval);
43073ccda647Slclee }
43083ccda647Slclee 
43093ccda647Slclee static int
4310e8fb11a1Sshidokht cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4311e8fb11a1Sshidokht     void *tg_cookie)
43123ccda647Slclee {
43133ccda647Slclee 	dk_efi_t	user_efi;
43143ccda647Slclee 	int		rval = 0;
43153ccda647Slclee 	void		*buffer;
4316e8fb11a1Sshidokht 	diskaddr_t	tgt_lba;
431706bbe1e0Sedp 	boolean_t	internal;
43183ccda647Slclee 
43193ccda647Slclee 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
43203ccda647Slclee 		return (EFAULT);
43213ccda647Slclee 
43227f0b8309SEdward Pilatowicz 	internal = VOID2BOOLEAN(
43237f0b8309SEdward Pilatowicz 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
432406bbe1e0Sedp 
43253ccda647Slclee 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
43263ccda647Slclee 
43273ccda647Slclee 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
43283ccda647Slclee 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
43293ccda647Slclee 		rval = EFAULT;
43303ccda647Slclee 	} else {
43313ccda647Slclee 		/*
43323ccda647Slclee 		 * let's clear the vtoc labels and clear the softstate
43333ccda647Slclee 		 * vtoc.
43343ccda647Slclee 		 */
4335e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
4336e8fb11a1Sshidokht 		if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
4337e8fb11a1Sshidokht 			cmlb_dbg(CMLB_TRACE,  cl,
43383ccda647Slclee 			    "cmlb_dkio_set_efi: CLEAR VTOC\n");
4339342440ecSPrasad Singamsetty 			if (cl->cl_label_from_media == CMLB_LABEL_VTOC)
4340e8fb11a1Sshidokht 				cmlb_clear_vtoc(cl, tg_cookie);
4341e8fb11a1Sshidokht 			bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4342e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4343e8fb11a1Sshidokht 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4344e8fb11a1Sshidokht 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
434506bbe1e0Sedp 			(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
43463ccda647Slclee 			    S_IFBLK,
43473ccda647Slclee 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
434806bbe1e0Sedp 			    cl->cl_node_type, NULL, internal);
434906bbe1e0Sedp 			(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
43503ccda647Slclee 			    S_IFCHR,
43513ccda647Slclee 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
435206bbe1e0Sedp 			    cl->cl_node_type, NULL, internal);
43533ccda647Slclee 		} else
4354e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4355e8fb11a1Sshidokht 
4356e8fb11a1Sshidokht 		tgt_lba = user_efi.dki_lba;
4357e8fb11a1Sshidokht 
4358e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
4359e8fb11a1Sshidokht 		if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4360e8fb11a1Sshidokht 		    (cl->cl_tgt_blocksize == 0)) {
4361e8fb11a1Sshidokht 			kmem_free(buffer, user_efi.dki_length);
4362e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4363e8fb11a1Sshidokht 			return (EINVAL);
4364e8fb11a1Sshidokht 		}
4365e8fb11a1Sshidokht 		if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4366e8fb11a1Sshidokht 			tgt_lba = tgt_lba *
4367e8fb11a1Sshidokht 			    cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
4368e8fb11a1Sshidokht 
4369e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
4370e8fb11a1Sshidokht 		rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
4371e8fb11a1Sshidokht 		    tg_cookie);
4372e8fb11a1Sshidokht 
43733ccda647Slclee 		if (rval == 0) {
4374e8fb11a1Sshidokht 			mutex_enter(CMLB_MUTEX(cl));
43757f0b8309SEdward Pilatowicz 			cl->cl_f_geometry_is_valid = B_FALSE;
4376e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
43773ccda647Slclee 		}
43783ccda647Slclee 	}
43793ccda647Slclee 	kmem_free(buffer, user_efi.dki_length);
43803ccda647Slclee 	return (rval);
43813ccda647Slclee }
43823ccda647Slclee 
43833ccda647Slclee /*
43843ccda647Slclee  *    Function: cmlb_dkio_get_mboot
43853ccda647Slclee  *
43863ccda647Slclee  * Description: This routine is the driver entry point for handling user
43873ccda647Slclee  *		requests to get the current device mboot (DKIOCGMBOOT)
43883ccda647Slclee  *
43893ccda647Slclee  *   Arguments:
4390e8fb11a1Sshidokht  *	arg		pointer to user provided mboot structure specifying
43913ccda647Slclee  *			the current mboot.
4392e8fb11a1Sshidokht  *
4393e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
43943ccda647Slclee  *			directly from the mode argument of ioctl().
43953ccda647Slclee  *
4396e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4397e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4398e8fb11a1Sshidokht  *
43993ccda647Slclee  * Return Code: 0
44003ccda647Slclee  *		EINVAL
44013ccda647Slclee  *		EFAULT
44023ccda647Slclee  *		ENXIO
44033ccda647Slclee  */
44043ccda647Slclee static int
4405e8fb11a1Sshidokht cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
44063ccda647Slclee {
44073ccda647Slclee 	struct mboot	*mboot;
44083ccda647Slclee 	int		rval;
44093ccda647Slclee 	size_t		buffer_size;
44103ccda647Slclee 
44113ccda647Slclee 
44123ccda647Slclee #if defined(_SUNOS_VTOC_8)
4413e8fb11a1Sshidokht 	if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
44143ccda647Slclee #elif defined(_SUNOS_VTOC_16)
44153ccda647Slclee 	if (arg == NULL) {
44163ccda647Slclee #endif
44173ccda647Slclee 		return (EINVAL);
44183ccda647Slclee 	}
44193ccda647Slclee 
44203ccda647Slclee 	/*
44213ccda647Slclee 	 * Read the mboot block, located at absolute block 0 on the target.
44223ccda647Slclee 	 */
4423*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	buffer_size = cl->cl_sys_blocksize;
44243ccda647Slclee 
4425e8fb11a1Sshidokht 	cmlb_dbg(CMLB_TRACE,  cl,
44263ccda647Slclee 	    "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
44273ccda647Slclee 
44283ccda647Slclee 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
4429e8fb11a1Sshidokht 	if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
44303ccda647Slclee 		if (ddi_copyout(mboot, (void *)arg,
44313ccda647Slclee 		    sizeof (struct mboot), flag) != 0) {
44323ccda647Slclee 			rval = EFAULT;
44333ccda647Slclee 		}
44343ccda647Slclee 	}
44353ccda647Slclee 	kmem_free(mboot, buffer_size);
44363ccda647Slclee 	return (rval);
44373ccda647Slclee }
44383ccda647Slclee 
44393ccda647Slclee 
44403ccda647Slclee /*
44413ccda647Slclee  *    Function: cmlb_dkio_set_mboot
44423ccda647Slclee  *
44433ccda647Slclee  * Description: This routine is the driver entry point for handling user
44443ccda647Slclee  *		requests to validate and set the device master boot
44453ccda647Slclee  *		(DKIOCSMBOOT).
44463ccda647Slclee  *
44473ccda647Slclee  *   Arguments:
4448e8fb11a1Sshidokht  *	arg		pointer to user provided mboot structure used to set the
44493ccda647Slclee  *			master boot.
4450e8fb11a1Sshidokht  *
4451e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
44523ccda647Slclee  *			directly from the mode argument of ioctl().
44533ccda647Slclee  *
4454e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4455e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4456e8fb11a1Sshidokht  *
44573ccda647Slclee  * Return Code: 0
44583ccda647Slclee  *		EINVAL
44593ccda647Slclee  *		EFAULT
44603ccda647Slclee  *		ENXIO
44613ccda647Slclee  */
44623ccda647Slclee static int
4463e8fb11a1Sshidokht cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
44643ccda647Slclee {
44653ccda647Slclee 	struct mboot	*mboot = NULL;
44663ccda647Slclee 	int		rval;
44673ccda647Slclee 	ushort_t	magic;
44683ccda647Slclee 
44693ccda647Slclee 
4470e8fb11a1Sshidokht 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
44713ccda647Slclee 
44723ccda647Slclee #if defined(_SUNOS_VTOC_8)
4473e8fb11a1Sshidokht 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
44743ccda647Slclee 		return (EINVAL);
44753ccda647Slclee 	}
44763ccda647Slclee #endif
44773ccda647Slclee 
44783ccda647Slclee 	if (arg == NULL) {
44793ccda647Slclee 		return (EINVAL);
44803ccda647Slclee 	}
44813ccda647Slclee 
4482*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	mboot = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
44833ccda647Slclee 
44843ccda647Slclee 	if (ddi_copyin((const void *)arg, mboot,
4485*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	    cl->cl_sys_blocksize, flag) != 0) {
4486*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		kmem_free(mboot, cl->cl_sys_blocksize);
44873ccda647Slclee 		return (EFAULT);
44883ccda647Slclee 	}
44893ccda647Slclee 
44903ccda647Slclee 	/* Is this really a master boot record? */
44913ccda647Slclee 	magic = LE_16(mboot->signature);
44923ccda647Slclee 	if (magic != MBB_MAGIC) {
4493*65908c77Syu, larry liu - Sun Microsystems - Beijing China 		kmem_free(mboot, cl->cl_sys_blocksize);
44943ccda647Slclee 		return (EINVAL);
44953ccda647Slclee 	}
44963ccda647Slclee 
4497e8fb11a1Sshidokht 	rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
44983ccda647Slclee 
4499e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
45003ccda647Slclee #if defined(__i386) || defined(__amd64)
45013ccda647Slclee 	if (rval == 0) {
45023ccda647Slclee 		/*
45033ccda647Slclee 		 * mboot has been written successfully.
45043ccda647Slclee 		 * update the fdisk and vtoc tables in memory
45053ccda647Slclee 		 */
4506e8fb11a1Sshidokht 		rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
45077f0b8309SEdward Pilatowicz 		if ((!cl->cl_f_geometry_is_valid) || (rval != 0)) {
4508e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4509*65908c77Syu, larry liu - Sun Microsystems - Beijing China 			kmem_free(mboot, cl->cl_sys_blocksize);
45103ccda647Slclee 			return (rval);
45113ccda647Slclee 		}
45123ccda647Slclee 	}
4513e8fb11a1Sshidokht 
4514e8fb11a1Sshidokht #ifdef __lock_lint
4515e8fb11a1Sshidokht 	cmlb_setup_default_geometry(cl, tg_cookie);
4516e8fb11a1Sshidokht #endif
4517e8fb11a1Sshidokht 
45183ccda647Slclee #else
45193ccda647Slclee 	if (rval == 0) {
45203ccda647Slclee 		/*
45213ccda647Slclee 		 * mboot has been written successfully.
45223ccda647Slclee 		 * set up the default geometry and VTOC
45233ccda647Slclee 		 */
4524342440ecSPrasad Singamsetty 		if (cl->cl_blockcount <= CMLB_EXTVTOC_LIMIT)
4525e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
45263ccda647Slclee 	}
45273ccda647Slclee #endif
4528342440ecSPrasad Singamsetty 	cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
4529e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
4530*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	kmem_free(mboot, cl->cl_sys_blocksize);
45313ccda647Slclee 	return (rval);
45323ccda647Slclee }
45333ccda647Slclee 
45343ccda647Slclee 
45353ccda647Slclee /*
45363ccda647Slclee  *    Function: cmlb_setup_default_geometry
45373ccda647Slclee  *
45383ccda647Slclee  * Description: This local utility routine sets the default geometry as part of
45393ccda647Slclee  *		setting the device mboot.
45403ccda647Slclee  *
4541e8fb11a1Sshidokht  *   Arguments:
4542e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4543e8fb11a1Sshidokht  *
4544e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4545e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4546e8fb11a1Sshidokht  *
45473ccda647Slclee  *
45483ccda647Slclee  * Note: This may be redundant with cmlb_build_default_label.
45493ccda647Slclee  */
45503ccda647Slclee static void
4551e8fb11a1Sshidokht cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
45523ccda647Slclee {
45533ccda647Slclee 	struct cmlb_geom	pgeom;
45543ccda647Slclee 	struct cmlb_geom	*pgeomp = &pgeom;
45553ccda647Slclee 	int			ret;
45563ccda647Slclee 	int			geom_base_cap = 1;
45573ccda647Slclee 
45583ccda647Slclee 
4559e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
45603ccda647Slclee 
45613ccda647Slclee 	/* zero out the soft state geometry and partition table. */
4562e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
4563e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4564e8fb11a1Sshidokht 	bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
45653ccda647Slclee 
45663ccda647Slclee 	/*
45673ccda647Slclee 	 * For the rpm, we use the minimum for the disk.
45683ccda647Slclee 	 * For the head, cyl and number of sector per track,
45693ccda647Slclee 	 * if the capacity <= 1GB, head = 64, sect = 32.
45703ccda647Slclee 	 * else head = 255, sect 63
45713ccda647Slclee 	 * Note: the capacity should be equal to C*H*S values.
45723ccda647Slclee 	 * This will cause some truncation of size due to
45733ccda647Slclee 	 * round off errors. For CD-ROMs, this truncation can
45743ccda647Slclee 	 * have adverse side effects, so returning ncyl and
45753ccda647Slclee 	 * nhead as 1. The nsect will overflow for most of
45763ccda647Slclee 	 * CD-ROMs as nsect is of type ushort.
45773ccda647Slclee 	 */
4578e8fb11a1Sshidokht 	if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
45793ccda647Slclee 		/*
45803ccda647Slclee 		 * newfs currently can not handle 255 ntracks for SPARC
45813ccda647Slclee 		 * so get the geometry from target driver instead of coming up
45823ccda647Slclee 		 * with one based on capacity.
45833ccda647Slclee 		 */
4584e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
4585e8fb11a1Sshidokht 		ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
4586e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
45873ccda647Slclee 
45883ccda647Slclee 		if (ret == 0) {
45893ccda647Slclee 			geom_base_cap = 0;
45903ccda647Slclee 		} else {
4591e8fb11a1Sshidokht 			cmlb_dbg(CMLB_ERROR,  cl,
45923ccda647Slclee 			    "cmlb_setup_default_geometry: "
45933ccda647Slclee 			    "tg_getphygeom failed %d\n", ret);
45943ccda647Slclee 
45953ccda647Slclee 			/* do default setting, geometry based on capacity */
45963ccda647Slclee 		}
45973ccda647Slclee 	}
45983ccda647Slclee 
45993ccda647Slclee 	if (geom_base_cap) {
4600e8fb11a1Sshidokht 		if (ISCD(cl)) {
4601e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 1;
4602e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 1;
4603e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
4604e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x1000) {
46053ccda647Slclee 			/* Needed for unlabeled SCSI floppies. */
4606e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 2;
4607e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 80;
4608e8fb11a1Sshidokht 			cl->cl_g.dkg_pcyl = 80;
4609e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
4610e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x200000) {
4611e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
4612e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
4613e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
46143ccda647Slclee 		} else {
4615e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 255;
461698a65dd4Sshidokht 
461798a65dd4Sshidokht 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
461898a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63) - 1) /
461998a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63)) * 63;
462098a65dd4Sshidokht 
462198a65dd4Sshidokht 			if (cl->cl_g.dkg_nsect == 0)
462298a65dd4Sshidokht 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
462398a65dd4Sshidokht 
462498a65dd4Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
462598a65dd4Sshidokht 			    (255 * cl->cl_g.dkg_nsect);
46263ccda647Slclee 		}
46273ccda647Slclee 
4628e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = 0;
4629e8fb11a1Sshidokht 		cl->cl_g.dkg_bcyl = 0;
4630e8fb11a1Sshidokht 		cl->cl_g.dkg_intrlv = 1;
4631e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm = 200;
4632e8fb11a1Sshidokht 		if (cl->cl_g.dkg_pcyl == 0)
4633e8fb11a1Sshidokht 			cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
4634e8fb11a1Sshidokht 			    cl->cl_g.dkg_acyl;
46353ccda647Slclee 	} else {
4636e8fb11a1Sshidokht 		cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
4637e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = pgeomp->g_acyl;
4638e8fb11a1Sshidokht 		cl->cl_g.dkg_nhead = pgeomp->g_nhead;
4639e8fb11a1Sshidokht 		cl->cl_g.dkg_nsect = pgeomp->g_nsect;
4640e8fb11a1Sshidokht 		cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
4641e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm = pgeomp->g_rpm;
4642e8fb11a1Sshidokht 		cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
46433ccda647Slclee 	}
46443ccda647Slclee 
4645e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct = 0;
4646e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = 0;
4647e8fb11a1Sshidokht 	cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
4648e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
46493ccda647Slclee 
4650e8fb11a1Sshidokht 	cl->cl_map['a'-'a'].dkl_cylno = 0;
4651e8fb11a1Sshidokht 	cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
46523ccda647Slclee 
4653e8fb11a1Sshidokht 	cl->cl_map['c'-'a'].dkl_cylno = 0;
4654e8fb11a1Sshidokht 	cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
46553ccda647Slclee 
4656e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
4657e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
4658e8fb11a1Sshidokht 	cl->cl_vtoc.v_nparts = V_NUMPAR;
4659e8fb11a1Sshidokht 	cl->cl_vtoc.v_version = V_VERSION;
4660e8fb11a1Sshidokht 	(void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
4661e8fb11a1Sshidokht 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
4662e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
46633ccda647Slclee 
46647f0b8309SEdward Pilatowicz 	cl->cl_f_geometry_is_valid = B_FALSE;
46653ccda647Slclee }
46663ccda647Slclee 
46673ccda647Slclee 
46683ccda647Slclee #if defined(__i386) || defined(__amd64)
46693ccda647Slclee /*
46703ccda647Slclee  *    Function: cmlb_update_fdisk_and_vtoc
46713ccda647Slclee  *
46723ccda647Slclee  * Description: This local utility routine updates the device fdisk and vtoc
46733ccda647Slclee  *		as part of setting the device mboot.
46743ccda647Slclee  *
4675e8fb11a1Sshidokht  *   Arguments:
4676e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4677e8fb11a1Sshidokht  *
4678e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4679e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4680e8fb11a1Sshidokht  *
46813ccda647Slclee  *
46823ccda647Slclee  * Return Code: 0 for success or errno-type return code.
46833ccda647Slclee  *
46843ccda647Slclee  *    Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
46853ccda647Slclee  *		these did exist separately in x86 sd.c.
46863ccda647Slclee  */
46873ccda647Slclee static int
4688e8fb11a1Sshidokht cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
46893ccda647Slclee {
46903ccda647Slclee 	int		count;
46913ccda647Slclee 	int		label_rc = 0;
46923ccda647Slclee 	int		fdisk_rval;
46933ccda647Slclee 	diskaddr_t	capacity;
46943ccda647Slclee 
4695e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
46963ccda647Slclee 
4697e8fb11a1Sshidokht 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
46983ccda647Slclee 		return (EINVAL);
46993ccda647Slclee 
47003ccda647Slclee #if defined(_SUNOS_VTOC_16)
47013ccda647Slclee 	/*
47023ccda647Slclee 	 * Set up the "whole disk" fdisk partition; this should always
47033ccda647Slclee 	 * exist, regardless of whether the disk contains an fdisk table
47043ccda647Slclee 	 * or vtoc.
47053ccda647Slclee 	 */
4706e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
4707e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
47083ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
47093ccda647Slclee 
47103ccda647Slclee 	/*
47113ccda647Slclee 	 * copy the lbasize and capacity so that if they're
4712e8fb11a1Sshidokht 	 * reset while we're not holding the CMLB_MUTEX(cl), we will
4713e8fb11a1Sshidokht 	 * continue to use valid values after the CMLB_MUTEX(cl) is
47143ccda647Slclee 	 * reacquired.
47153ccda647Slclee 	 */
4716e8fb11a1Sshidokht 	capacity = cl->cl_blockcount;
47173ccda647Slclee 
47183ccda647Slclee 	/*
47193ccda647Slclee 	 * refresh the logical and physical geometry caches.
47203ccda647Slclee 	 * (data from mode sense format/rigid disk geometry pages,
47213ccda647Slclee 	 * and scsi_ifgetcap("geometry").
47223ccda647Slclee 	 */
4723e8fb11a1Sshidokht 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
47243ccda647Slclee 
47253ccda647Slclee 	/*
4726e8fb11a1Sshidokht 	 * Only DIRECT ACCESS devices will have Scl labels.
4727e8fb11a1Sshidokht 	 * CD's supposedly have a Scl label, too
47283ccda647Slclee 	 */
4729e8fb11a1Sshidokht 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
4730e8fb11a1Sshidokht 		fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
47313ccda647Slclee 		if (fdisk_rval != 0) {
4732e8fb11a1Sshidokht 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
47333ccda647Slclee 			return (fdisk_rval);
47343ccda647Slclee 		}
47353ccda647Slclee 
4736e8fb11a1Sshidokht 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
47373ccda647Slclee 			/*
47383ccda647Slclee 			 * Found fdisk table but no Solaris partition entry,
47393ccda647Slclee 			 * so don't call cmlb_uselabel() and don't create
47403ccda647Slclee 			 * a default label.
47413ccda647Slclee 			 */
47423ccda647Slclee 			label_rc = 0;
47437f0b8309SEdward Pilatowicz 			cl->cl_f_geometry_is_valid = B_TRUE;
47443ccda647Slclee 			goto no_solaris_partition;
47453ccda647Slclee 		}
47463ccda647Slclee 	} else if (capacity < 0) {
4747e8fb11a1Sshidokht 		ASSERT(mutex_owned(CMLB_MUTEX(cl)));
47483ccda647Slclee 		return (EINVAL);
47493ccda647Slclee 	}
47503ccda647Slclee 
47513ccda647Slclee 	/*
47523ccda647Slclee 	 * For Removable media We reach here if we have found a
47533ccda647Slclee 	 * SOLARIS PARTITION.
47547f0b8309SEdward Pilatowicz 	 * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
47553ccda647Slclee 	 * PARTITION has changed from the previous one, hence we will setup a
47563ccda647Slclee 	 * default VTOC in this case.
47573ccda647Slclee 	 */
47587f0b8309SEdward Pilatowicz 	if (!cl->cl_f_geometry_is_valid) {
47593ccda647Slclee 		/* if we get here it is writable */
47603ccda647Slclee 		/* we are called from SMBOOT, and after a write of fdisk */
4761e8fb11a1Sshidokht 		cmlb_build_default_label(cl, tg_cookie);
47623ccda647Slclee 		label_rc = 0;
47633ccda647Slclee 	}
47643ccda647Slclee 
47653ccda647Slclee no_solaris_partition:
47663ccda647Slclee 
47673ccda647Slclee #if defined(_SUNOS_VTOC_16)
47683ccda647Slclee 	/*
47693ccda647Slclee 	 * If we have valid geometry, set up the remaining fdisk partitions.
47703ccda647Slclee 	 * Note that dkl_cylno is not used for the fdisk map entries, so
47713ccda647Slclee 	 * we set it to an entirely bogus value.
47723ccda647Slclee 	 */
4773f85c7842SSuhasini Peddada 	for (count = 0; count < FD_NUMPART; count++) {
4774342440ecSPrasad Singamsetty 		cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT32_MAX;
4775e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
4776e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_nblk;
4777e8fb11a1Sshidokht 		cl->cl_offset[FDISK_P1 + count] =
4778e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_start;
47793ccda647Slclee 	}
47803ccda647Slclee #endif
47813ccda647Slclee 
47823ccda647Slclee 	for (count = 0; count < NDKMAP; count++) {
47833ccda647Slclee #if defined(_SUNOS_VTOC_8)
4784e8fb11a1Sshidokht 		struct dk_map *lp  = &cl->cl_map[count];
4785e8fb11a1Sshidokht 		cl->cl_offset[count] =
4786e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
47873ccda647Slclee #elif defined(_SUNOS_VTOC_16)
4788e8fb11a1Sshidokht 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
4789e8fb11a1Sshidokht 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
47903ccda647Slclee #else
47913ccda647Slclee #error "No VTOC format defined."
47923ccda647Slclee #endif
47933ccda647Slclee 	}
47943ccda647Slclee 
4795e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
47963ccda647Slclee 	return (label_rc);
47973ccda647Slclee }
47983ccda647Slclee #endif
47993ccda647Slclee 
48003ccda647Slclee #if defined(__i386) || defined(__amd64)
48013ccda647Slclee static int
4802e8fb11a1Sshidokht cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
48033ccda647Slclee {
48043ccda647Slclee 	int err = 0;
48053ccda647Slclee 
48063ccda647Slclee 	/* Return the driver's notion of the media's logical geometry */
48073ccda647Slclee 	struct dk_geom	disk_geom;
48083ccda647Slclee 	struct dk_geom	*dkgp = &disk_geom;
48093ccda647Slclee 
4810e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
48113ccda647Slclee 	/*
48123ccda647Slclee 	 * If there is no HBA geometry available, or
48133ccda647Slclee 	 * if the HBA returned us something that doesn't
48143ccda647Slclee 	 * really fit into an Int 13/function 8 geometry
48153ccda647Slclee 	 * result, just fail the ioctl.  See PSARC 1998/313.
48163ccda647Slclee 	 */
4817e8fb11a1Sshidokht 	if (cl->cl_lgeom.g_nhead == 0 ||
4818e8fb11a1Sshidokht 	    cl->cl_lgeom.g_nsect == 0 ||
4819e8fb11a1Sshidokht 	    cl->cl_lgeom.g_ncyl > 1024) {
4820e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
48213ccda647Slclee 		err = EINVAL;
48223ccda647Slclee 	} else {
4823e8fb11a1Sshidokht 		dkgp->dkg_ncyl	= cl->cl_lgeom.g_ncyl;
4824e8fb11a1Sshidokht 		dkgp->dkg_acyl	= cl->cl_lgeom.g_acyl;
48253ccda647Slclee 		dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
4826e8fb11a1Sshidokht 		dkgp->dkg_nhead	= cl->cl_lgeom.g_nhead;
4827e8fb11a1Sshidokht 		dkgp->dkg_nsect	= cl->cl_lgeom.g_nsect;
48283ccda647Slclee 
48291c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
48303ccda647Slclee 		if (ddi_copyout(dkgp, (void *)arg,
48313ccda647Slclee 		    sizeof (struct dk_geom), flag)) {
48323ccda647Slclee 			err = EFAULT;
48333ccda647Slclee 		} else {
48343ccda647Slclee 			err = 0;
48353ccda647Slclee 		}
48363ccda647Slclee 	}
48373ccda647Slclee 	return (err);
48383ccda647Slclee }
48393ccda647Slclee #endif
48403ccda647Slclee 
48413ccda647Slclee #if defined(__i386) || defined(__amd64)
48423ccda647Slclee static int
4843e8fb11a1Sshidokht cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag)
48443ccda647Slclee {
48453ccda647Slclee 	int err = 0;
4846e8fb11a1Sshidokht 	diskaddr_t capacity;
48473ccda647Slclee 
48483ccda647Slclee 
48493ccda647Slclee 	/* Return the driver's notion of the media physical geometry */
48503ccda647Slclee 	struct dk_geom	disk_geom;
48513ccda647Slclee 	struct dk_geom	*dkgp = &disk_geom;
48523ccda647Slclee 
4853e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
48543ccda647Slclee 
4855e8fb11a1Sshidokht 	if (cl->cl_g.dkg_nhead != 0 &&
4856e8fb11a1Sshidokht 	    cl->cl_g.dkg_nsect != 0) {
48573ccda647Slclee 		/*
48583ccda647Slclee 		 * We succeeded in getting a geometry, but
48593ccda647Slclee 		 * right now it is being reported as just the
48603ccda647Slclee 		 * Solaris fdisk partition, just like for
48613ccda647Slclee 		 * DKIOCGGEOM. We need to change that to be
48623ccda647Slclee 		 * correct for the entire disk now.
48633ccda647Slclee 		 */
4864e8fb11a1Sshidokht 		bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
48653ccda647Slclee 		dkgp->dkg_acyl = 0;
4866e8fb11a1Sshidokht 		dkgp->dkg_ncyl = cl->cl_blockcount /
48673ccda647Slclee 		    (dkgp->dkg_nhead * dkgp->dkg_nsect);
48683ccda647Slclee 	} else {
48693ccda647Slclee 		bzero(dkgp, sizeof (struct dk_geom));
48703ccda647Slclee 		/*
48713ccda647Slclee 		 * This disk does not have a Solaris VTOC
48723ccda647Slclee 		 * so we must present a physical geometry
48733ccda647Slclee 		 * that will remain consistent regardless
48743ccda647Slclee 		 * of how the disk is used. This will ensure
48753ccda647Slclee 		 * that the geometry does not change regardless
48763ccda647Slclee 		 * of the fdisk partition type (ie. EFI, FAT32,
48773ccda647Slclee 		 * Solaris, etc).
48783ccda647Slclee 		 */
4879e8fb11a1Sshidokht 		if (ISCD(cl)) {
4880e8fb11a1Sshidokht 			dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
4881e8fb11a1Sshidokht 			dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
4882e8fb11a1Sshidokht 			dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
4883e8fb11a1Sshidokht 			dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
48843ccda647Slclee 		} else {
4885e8fb11a1Sshidokht 			/*
4886e8fb11a1Sshidokht 			 * Invalid cl_blockcount can generate invalid
4887e8fb11a1Sshidokht 			 * dk_geom and may result in division by zero
4888e8fb11a1Sshidokht 			 * system failure. Should make sure blockcount
4889e8fb11a1Sshidokht 			 * is valid before using it here.
4890e8fb11a1Sshidokht 			 */
4891e8fb11a1Sshidokht 			if (cl->cl_blockcount == 0) {
4892e8fb11a1Sshidokht 				mutex_exit(CMLB_MUTEX(cl));
4893e8fb11a1Sshidokht 				err = EIO;
4894e8fb11a1Sshidokht 				return (err);
4895e8fb11a1Sshidokht 			}
4896e8fb11a1Sshidokht 			/*
4897e8fb11a1Sshidokht 			 * Refer to comments related to off-by-1 at the
4898e8fb11a1Sshidokht 			 * header of this file
4899e8fb11a1Sshidokht 			 */
4900e8fb11a1Sshidokht 			if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
4901e8fb11a1Sshidokht 				capacity = cl->cl_blockcount - 1;
4902e8fb11a1Sshidokht 			else
4903e8fb11a1Sshidokht 				capacity = cl->cl_blockcount;
4904e8fb11a1Sshidokht 
4905e8fb11a1Sshidokht 			cmlb_convert_geometry(capacity, dkgp);
49063ccda647Slclee 			dkgp->dkg_acyl = 0;
4907e8fb11a1Sshidokht 			dkgp->dkg_ncyl = capacity /
49083ccda647Slclee 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
49093ccda647Slclee 		}
49103ccda647Slclee 	}
49113ccda647Slclee 	dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
49123ccda647Slclee 
4913e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
49141c97599bSshidokht 	if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
49153ccda647Slclee 		err = EFAULT;
49161c97599bSshidokht 
49173ccda647Slclee 	return (err);
49183ccda647Slclee }
49193ccda647Slclee #endif
49203ccda647Slclee 
49213ccda647Slclee #if defined(__i386) || defined(__amd64)
49223ccda647Slclee static int
4923e8fb11a1Sshidokht cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
49243ccda647Slclee {
49253ccda647Slclee 	int err = 0;
49263ccda647Slclee 
49273ccda647Slclee 	/*
49283ccda647Slclee 	 * Return parameters describing the selected disk slice.
49293ccda647Slclee 	 * Note: this ioctl is for the intel platform only
49303ccda647Slclee 	 */
49313ccda647Slclee 	int part;
49323ccda647Slclee 
49333ccda647Slclee 	part = CMLBPART(dev);
49343ccda647Slclee 
4935e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
4936e8fb11a1Sshidokht 	/* don't check cl_solaris_size for pN */
4937e8fb11a1Sshidokht 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
49383ccda647Slclee 		err = EIO;
49391c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
49403ccda647Slclee 	} else {
49413ccda647Slclee 		struct part_info p;
49423ccda647Slclee 
4943e8fb11a1Sshidokht 		p.p_start = (daddr_t)cl->cl_offset[part];
4944e8fb11a1Sshidokht 		p.p_length = (int)cl->cl_map[part].dkl_nblk;
49451c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
49463ccda647Slclee #ifdef _MULTI_DATAMODEL
49473ccda647Slclee 		switch (ddi_model_convert_from(flag & FMODELS)) {
49483ccda647Slclee 		case DDI_MODEL_ILP32:
49493ccda647Slclee 		{
49503ccda647Slclee 			struct part_info32 p32;
49513ccda647Slclee 
49523ccda647Slclee 			p32.p_start = (daddr32_t)p.p_start;
49533ccda647Slclee 			p32.p_length = p.p_length;
49543ccda647Slclee 			if (ddi_copyout(&p32, (void *)arg,
49553ccda647Slclee 			    sizeof (p32), flag))
49563ccda647Slclee 				err = EFAULT;
49573ccda647Slclee 			break;
49583ccda647Slclee 		}
49593ccda647Slclee 
49603ccda647Slclee 		case DDI_MODEL_NONE:
49613ccda647Slclee 		{
49623ccda647Slclee 			if (ddi_copyout(&p, (void *)arg, sizeof (p),
49633ccda647Slclee 			    flag))
49643ccda647Slclee 				err = EFAULT;
49653ccda647Slclee 			break;
49663ccda647Slclee 		}
49673ccda647Slclee 		}
49683ccda647Slclee #else /* ! _MULTI_DATAMODEL */
49693ccda647Slclee 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
49703ccda647Slclee 			err = EFAULT;
49713ccda647Slclee #endif /* _MULTI_DATAMODEL */
49723ccda647Slclee 	}
49733ccda647Slclee 	return (err);
49743ccda647Slclee }
4975342440ecSPrasad Singamsetty static int
4976342440ecSPrasad Singamsetty cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
4977342440ecSPrasad Singamsetty {
4978342440ecSPrasad Singamsetty 	int err = 0;
4979342440ecSPrasad Singamsetty 
4980342440ecSPrasad Singamsetty 	/*
4981342440ecSPrasad Singamsetty 	 * Return parameters describing the selected disk slice.
4982342440ecSPrasad Singamsetty 	 * Note: this ioctl is for the intel platform only
4983342440ecSPrasad Singamsetty 	 */
4984342440ecSPrasad Singamsetty 	int part;
4985342440ecSPrasad Singamsetty 
4986342440ecSPrasad Singamsetty 	part = CMLBPART(dev);
4987342440ecSPrasad Singamsetty 
4988342440ecSPrasad Singamsetty 	mutex_enter(CMLB_MUTEX(cl));
4989342440ecSPrasad Singamsetty 	/* don't check cl_solaris_size for pN */
4990342440ecSPrasad Singamsetty 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
4991342440ecSPrasad Singamsetty 		err = EIO;
4992342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
4993342440ecSPrasad Singamsetty 	} else {
4994342440ecSPrasad Singamsetty 		struct extpart_info p;
4995342440ecSPrasad Singamsetty 
4996342440ecSPrasad Singamsetty 		p.p_start = (diskaddr_t)cl->cl_offset[part];
4997342440ecSPrasad Singamsetty 		p.p_length = (diskaddr_t)cl->cl_map[part].dkl_nblk;
4998342440ecSPrasad Singamsetty 		mutex_exit(CMLB_MUTEX(cl));
4999342440ecSPrasad Singamsetty 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5000342440ecSPrasad Singamsetty 			err = EFAULT;
5001342440ecSPrasad Singamsetty 	}
5002342440ecSPrasad Singamsetty 	return (err);
5003342440ecSPrasad Singamsetty }
50043ccda647Slclee #endif
5005b9ccdc5aScth 
5006b9ccdc5aScth int
5007b9ccdc5aScth cmlb_prop_op(cmlb_handle_t cmlbhandle,
5008b9ccdc5aScth     dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
5009b9ccdc5aScth     char *name, caddr_t valuep, int *lengthp, int part, void *tg_cookie)
5010b9ccdc5aScth {
5011b9ccdc5aScth 	struct cmlb_lun	*cl;
5012b9ccdc5aScth 	diskaddr_t	capacity;
5013b9ccdc5aScth 	uint32_t	lbasize;
5014b9ccdc5aScth 	enum		dp { DP_NBLOCKS, DP_BLKSIZE } dp;
5015b9ccdc5aScth 	int		callers_length;
5016b9ccdc5aScth 	caddr_t		buffer;
5017b9ccdc5aScth 	uint64_t	nblocks64;
5018b9ccdc5aScth 	uint_t		dblk;
5019b9ccdc5aScth 
5020b9ccdc5aScth 	/* Always fallback to ddi_prop_op... */
5021b9ccdc5aScth 	cl = (struct cmlb_lun *)cmlbhandle;
5022b9ccdc5aScth 	if (cl == NULL) {
5023b9ccdc5aScth fallback:	return (ddi_prop_op(dev, dip, prop_op, mod_flags,
5024b9ccdc5aScth 		    name, valuep, lengthp));
5025b9ccdc5aScth 	}
5026b9ccdc5aScth 
5027b9ccdc5aScth 	/* Pick up capacity and blocksize information. */
5028b9ccdc5aScth 	capacity = cl->cl_blockcount;
5029b9ccdc5aScth 	if (capacity == 0)
5030b9ccdc5aScth 		goto fallback;
5031b9ccdc5aScth 	lbasize = cl->cl_tgt_blocksize;
5032b9ccdc5aScth 	if (lbasize == 0)
5033b9ccdc5aScth 		lbasize = DEV_BSIZE;	/* 0 -> DEV_BSIZE units */
5034b9ccdc5aScth 
5035b9ccdc5aScth 	/* Check for dynamic property of whole device. */
5036b9ccdc5aScth 	if (dev == DDI_DEV_T_ANY) {
5037b9ccdc5aScth 		/* Fallback to ddi_prop_op if we don't understand.  */
5038b9ccdc5aScth 		if (strcmp(name, "device-nblocks") == 0)
5039b9ccdc5aScth 			dp = DP_NBLOCKS;
5040b9ccdc5aScth 		else if (strcmp(name, "device-blksize") == 0)
5041b9ccdc5aScth 			dp = DP_BLKSIZE;
5042b9ccdc5aScth 		else
5043b9ccdc5aScth 			goto fallback;
5044b9ccdc5aScth 
5045b9ccdc5aScth 		/* get callers length, establish length of our dynamic prop */
5046b9ccdc5aScth 		callers_length = *lengthp;
5047b9ccdc5aScth 		if (dp == DP_NBLOCKS)
5048b9ccdc5aScth 			*lengthp = sizeof (uint64_t);
5049b9ccdc5aScth 		else if (dp == DP_BLKSIZE)
5050b9ccdc5aScth 			*lengthp = sizeof (uint32_t);
5051b9ccdc5aScth 
5052b9ccdc5aScth 		/* service request for the length of the property */
5053b9ccdc5aScth 		if (prop_op == PROP_LEN)
5054b9ccdc5aScth 			return (DDI_PROP_SUCCESS);
5055b9ccdc5aScth 
5056b9ccdc5aScth 		switch (prop_op) {
5057b9ccdc5aScth 		case PROP_LEN_AND_VAL_ALLOC:
5058b9ccdc5aScth 			if ((buffer = kmem_alloc(*lengthp,
5059b9ccdc5aScth 			    (mod_flags & DDI_PROP_CANSLEEP) ?
5060b9ccdc5aScth 			    KM_SLEEP : KM_NOSLEEP)) == NULL)
5061b9ccdc5aScth 				return (DDI_PROP_NO_MEMORY);
5062b9ccdc5aScth 			*(caddr_t *)valuep = buffer;	/* set callers buf */
5063b9ccdc5aScth 			break;
5064b9ccdc5aScth 
5065b9ccdc5aScth 		case PROP_LEN_AND_VAL_BUF:
5066b9ccdc5aScth 			/* the length of the prop and the request must match */
5067b9ccdc5aScth 			if (callers_length != *lengthp)
5068b9ccdc5aScth 				return (DDI_PROP_INVAL_ARG);
5069b9ccdc5aScth 			buffer = valuep;		/* get callers buf */
5070b9ccdc5aScth 			break;
5071b9ccdc5aScth 
5072b9ccdc5aScth 		default:
5073b9ccdc5aScth 			return (DDI_PROP_INVAL_ARG);
5074b9ccdc5aScth 		}
5075b9ccdc5aScth 
5076b9ccdc5aScth 		/* transfer the value into the buffer */
5077b9ccdc5aScth 		if (dp == DP_NBLOCKS)
5078b9ccdc5aScth 			*((uint64_t *)buffer) = capacity;
5079b9ccdc5aScth 		else if (dp == DP_BLKSIZE)
5080b9ccdc5aScth 			*((uint32_t *)buffer) = lbasize;
5081b9ccdc5aScth 
5082b9ccdc5aScth 		return (DDI_PROP_SUCCESS);
5083b9ccdc5aScth 	}
5084b9ccdc5aScth 
5085b9ccdc5aScth 	/*
5086b9ccdc5aScth 	 * Support dynamic size oriented properties of partition. Requests
5087b9ccdc5aScth 	 * issued under conditions where size is valid are passed to
5088b9ccdc5aScth 	 * ddi_prop_op_nblocks with the size information, otherwise the
5089b9ccdc5aScth 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
5090b9ccdc5aScth 	 */
5091b9ccdc5aScth 	if (!cmlb_is_valid(cmlbhandle))
5092b9ccdc5aScth 		goto fallback;
5093b9ccdc5aScth 
5094b9ccdc5aScth 	/* Get partition nblocks value. */
5095b9ccdc5aScth 	(void) cmlb_partinfo(cmlbhandle, part,
5096b9ccdc5aScth 	    (diskaddr_t *)&nblocks64, NULL, NULL, NULL, tg_cookie);
5097b9ccdc5aScth 
5098b9ccdc5aScth 	/*
5099*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * Assume partition information is in sys_blocksize units, compute
5100b9ccdc5aScth 	 * divisor for size(9P) property representation.
5101b9ccdc5aScth 	 */
5102*65908c77Syu, larry liu - Sun Microsystems - Beijing China 	dblk = lbasize / cl->cl_sys_blocksize;
5103b9ccdc5aScth 
5104b9ccdc5aScth 	/* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5105b9ccdc5aScth 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
5106b9ccdc5aScth 	    name, valuep, lengthp, nblocks64 / dblk, lbasize));
5107b9ccdc5aScth }
5108