xref: /titanic_53/usr/src/uts/common/io/cmlb.c (revision 98a65dd4de0911ff3f306982d71952f6cc95f1cf)
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 /*
23*98a65dd4Sshidokht  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
243ccda647Slclee  * Use is subject to license terms.
253ccda647Slclee  */
263ccda647Slclee 
273ccda647Slclee #pragma ident	"%Z%%M%	%I%	%E% SMI"
283ccda647Slclee 
293ccda647Slclee /*
303ccda647Slclee  * This module provides support for labeling operations for target
313ccda647Slclee  * drivers.
323ccda647Slclee  */
333ccda647Slclee 
343ccda647Slclee #include <sys/scsi/scsi.h>
353ccda647Slclee #include <sys/sunddi.h>
363ccda647Slclee #include <sys/dklabel.h>
373ccda647Slclee #include <sys/dkio.h>
383ccda647Slclee #include <sys/vtoc.h>
393ccda647Slclee #include <sys/dktp/fdisk.h>
403ccda647Slclee #include <sys/vtrace.h>
413ccda647Slclee #include <sys/efi_partition.h>
423ccda647Slclee #include <sys/cmlb.h>
433ccda647Slclee #include <sys/cmlb_impl.h>
443ccda647Slclee 
453ccda647Slclee /*
463ccda647Slclee  * Driver minor node structure and data table
473ccda647Slclee  */
483ccda647Slclee struct driver_minor_data {
493ccda647Slclee 	char	*name;
503ccda647Slclee 	minor_t	minor;
513ccda647Slclee 	int	type;
523ccda647Slclee };
533ccda647Slclee 
543ccda647Slclee static struct driver_minor_data dk_minor_data[] = {
553ccda647Slclee 	{"a", 0, S_IFBLK},
563ccda647Slclee 	{"b", 1, S_IFBLK},
573ccda647Slclee 	{"c", 2, S_IFBLK},
583ccda647Slclee 	{"d", 3, S_IFBLK},
593ccda647Slclee 	{"e", 4, S_IFBLK},
603ccda647Slclee 	{"f", 5, S_IFBLK},
613ccda647Slclee 	{"g", 6, S_IFBLK},
623ccda647Slclee 	{"h", 7, S_IFBLK},
633ccda647Slclee #if defined(_SUNOS_VTOC_16)
643ccda647Slclee 	{"i", 8, S_IFBLK},
653ccda647Slclee 	{"j", 9, S_IFBLK},
663ccda647Slclee 	{"k", 10, S_IFBLK},
673ccda647Slclee 	{"l", 11, S_IFBLK},
683ccda647Slclee 	{"m", 12, S_IFBLK},
693ccda647Slclee 	{"n", 13, S_IFBLK},
703ccda647Slclee 	{"o", 14, S_IFBLK},
713ccda647Slclee 	{"p", 15, S_IFBLK},
723ccda647Slclee #endif			/* defined(_SUNOS_VTOC_16) */
733ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
743ccda647Slclee 	{"q", 16, S_IFBLK},
753ccda647Slclee 	{"r", 17, S_IFBLK},
763ccda647Slclee 	{"s", 18, S_IFBLK},
773ccda647Slclee 	{"t", 19, S_IFBLK},
783ccda647Slclee 	{"u", 20, S_IFBLK},
793ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
803ccda647Slclee 	{"a,raw", 0, S_IFCHR},
813ccda647Slclee 	{"b,raw", 1, S_IFCHR},
823ccda647Slclee 	{"c,raw", 2, S_IFCHR},
833ccda647Slclee 	{"d,raw", 3, S_IFCHR},
843ccda647Slclee 	{"e,raw", 4, S_IFCHR},
853ccda647Slclee 	{"f,raw", 5, S_IFCHR},
863ccda647Slclee 	{"g,raw", 6, S_IFCHR},
873ccda647Slclee 	{"h,raw", 7, S_IFCHR},
883ccda647Slclee #if defined(_SUNOS_VTOC_16)
893ccda647Slclee 	{"i,raw", 8, S_IFCHR},
903ccda647Slclee 	{"j,raw", 9, S_IFCHR},
913ccda647Slclee 	{"k,raw", 10, S_IFCHR},
923ccda647Slclee 	{"l,raw", 11, S_IFCHR},
933ccda647Slclee 	{"m,raw", 12, S_IFCHR},
943ccda647Slclee 	{"n,raw", 13, S_IFCHR},
953ccda647Slclee 	{"o,raw", 14, S_IFCHR},
963ccda647Slclee 	{"p,raw", 15, S_IFCHR},
973ccda647Slclee #endif			/* defined(_SUNOS_VTOC_16) */
983ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
993ccda647Slclee 	{"q,raw", 16, S_IFCHR},
1003ccda647Slclee 	{"r,raw", 17, S_IFCHR},
1013ccda647Slclee 	{"s,raw", 18, S_IFCHR},
1023ccda647Slclee 	{"t,raw", 19, S_IFCHR},
1033ccda647Slclee 	{"u,raw", 20, S_IFCHR},
1043ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1053ccda647Slclee 	{0}
1063ccda647Slclee };
1073ccda647Slclee 
1083ccda647Slclee static struct driver_minor_data dk_minor_data_efi[] = {
1093ccda647Slclee 	{"a", 0, S_IFBLK},
1103ccda647Slclee 	{"b", 1, S_IFBLK},
1113ccda647Slclee 	{"c", 2, S_IFBLK},
1123ccda647Slclee 	{"d", 3, S_IFBLK},
1133ccda647Slclee 	{"e", 4, S_IFBLK},
1143ccda647Slclee 	{"f", 5, S_IFBLK},
1153ccda647Slclee 	{"g", 6, S_IFBLK},
1163ccda647Slclee 	{"wd", 7, S_IFBLK},
1173ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
1183ccda647Slclee 	{"q", 16, S_IFBLK},
1193ccda647Slclee 	{"r", 17, S_IFBLK},
1203ccda647Slclee 	{"s", 18, S_IFBLK},
1213ccda647Slclee 	{"t", 19, S_IFBLK},
1223ccda647Slclee 	{"u", 20, S_IFBLK},
1233ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1243ccda647Slclee 	{"a,raw", 0, S_IFCHR},
1253ccda647Slclee 	{"b,raw", 1, S_IFCHR},
1263ccda647Slclee 	{"c,raw", 2, S_IFCHR},
1273ccda647Slclee 	{"d,raw", 3, S_IFCHR},
1283ccda647Slclee 	{"e,raw", 4, S_IFCHR},
1293ccda647Slclee 	{"f,raw", 5, S_IFCHR},
1303ccda647Slclee 	{"g,raw", 6, S_IFCHR},
1313ccda647Slclee 	{"wd,raw", 7, S_IFCHR},
1323ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
1333ccda647Slclee 	{"q,raw", 16, S_IFCHR},
1343ccda647Slclee 	{"r,raw", 17, S_IFCHR},
1353ccda647Slclee 	{"s,raw", 18, S_IFCHR},
1363ccda647Slclee 	{"t,raw", 19, S_IFCHR},
1373ccda647Slclee 	{"u,raw", 20, S_IFCHR},
1383ccda647Slclee #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
1393ccda647Slclee 	{0}
1403ccda647Slclee };
1413ccda647Slclee 
1423ccda647Slclee 
1433ccda647Slclee 
1443ccda647Slclee extern struct mod_ops mod_miscops;
1453ccda647Slclee 
1463ccda647Slclee /*
1473ccda647Slclee  * Global buffer and mutex for debug logging
1483ccda647Slclee  */
1493ccda647Slclee static char	cmlb_log_buffer[1024];
1503ccda647Slclee static kmutex_t	cmlb_log_mutex;
1513ccda647Slclee 
1523ccda647Slclee 
153e8fb11a1Sshidokht struct cmlb_lun *cmlb_debug_cl = NULL;
1543ccda647Slclee uint_t cmlb_level_mask = 0x0;
1553ccda647Slclee 
1563ccda647Slclee int cmlb_rot_delay = 4;	/* default rotational delay */
1573ccda647Slclee 
1583ccda647Slclee static struct modlmisc modlmisc = {
1593ccda647Slclee 	&mod_miscops,   /* Type of module */
1603ccda647Slclee 	"Common Labeling module %I%"
1613ccda647Slclee };
1623ccda647Slclee 
1633ccda647Slclee static struct modlinkage modlinkage = {
1643ccda647Slclee 	MODREV_1, (void *)&modlmisc, NULL
1653ccda647Slclee };
1663ccda647Slclee 
1673ccda647Slclee /* Local function prototypes */
168e8fb11a1Sshidokht static dev_t cmlb_make_device(struct cmlb_lun *cl);
169e8fb11a1Sshidokht static int cmlb_validate_geometry(struct cmlb_lun *cl, int forcerevalid,
170e8fb11a1Sshidokht     int flags, void *tg_cookie);
171e8fb11a1Sshidokht static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
172e8fb11a1Sshidokht     void *tg_cookie);
173e8fb11a1Sshidokht static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
174e8fb11a1Sshidokht     void *tg_cookie);
1753ccda647Slclee static void cmlb_swap_efi_gpt(efi_gpt_t *e);
1763ccda647Slclee static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
1773ccda647Slclee static int cmlb_validate_efi(efi_gpt_t *labp);
178e8fb11a1Sshidokht static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
179e8fb11a1Sshidokht     void *tg_cookie);
180e8fb11a1Sshidokht static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
181e8fb11a1Sshidokht static int  cmlb_uselabel(struct cmlb_lun *cl,  struct dk_label *l, int flags);
182e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
183e8fb11a1Sshidokht static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
184e8fb11a1Sshidokht #endif
185e8fb11a1Sshidokht static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
186e8fb11a1Sshidokht static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
187e8fb11a1Sshidokht static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
188e8fb11a1Sshidokht     void *tg_cookie);
189e8fb11a1Sshidokht static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
190e8fb11a1Sshidokht static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
191e8fb11a1Sshidokht static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
192e8fb11a1Sshidokht static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
193e8fb11a1Sshidokht static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
194ab92f716Sshidokht static int cmlb_check_efi_mbr(uchar_t *buf);
1953ccda647Slclee 
1963ccda647Slclee #if defined(__i386) || defined(__amd64)
197e8fb11a1Sshidokht static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
1983ccda647Slclee #endif
1993ccda647Slclee 
2003ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
2013ccda647Slclee static int  cmlb_has_max_chs_vals(struct ipart *fdp);
2023ccda647Slclee #endif
2033ccda647Slclee 
2043ccda647Slclee #if defined(_SUNOS_VTOC_16)
205e8fb11a1Sshidokht static void cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g);
2063ccda647Slclee #endif
2073ccda647Slclee 
208e8fb11a1Sshidokht static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
209e8fb11a1Sshidokht     void *tg_cookie);
210e8fb11a1Sshidokht static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
211e8fb11a1Sshidokht static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
212e8fb11a1Sshidokht     void *tg_cookie);
213e8fb11a1Sshidokht static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
214e8fb11a1Sshidokht static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
215e8fb11a1Sshidokht     void *tg_cookie);
216e8fb11a1Sshidokht static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
217e8fb11a1Sshidokht     int flag, void *tg_cookie);
218e8fb11a1Sshidokht static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
219e8fb11a1Sshidokht     void *tg_cookie);
220e8fb11a1Sshidokht static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
221e8fb11a1Sshidokht     int flag, void *tg_cookie);
222e8fb11a1Sshidokht static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
223e8fb11a1Sshidokht     void *tg_cookie);
224e8fb11a1Sshidokht static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
225e8fb11a1Sshidokht     void *tg_cookie);
226e8fb11a1Sshidokht static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
227e8fb11a1Sshidokht     void *tg_cookie);
2283ccda647Slclee 
2293ccda647Slclee #if defined(__i386) || defined(__amd64)
230e8fb11a1Sshidokht static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
231e8fb11a1Sshidokht static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag);
232e8fb11a1Sshidokht static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
2333ccda647Slclee     int flag);
2343ccda647Slclee #endif
2353ccda647Slclee 
236e8fb11a1Sshidokht static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
2373ccda647Slclee static void cmlb_v_log(dev_info_t *dev, char *label, uint_t level,
2383ccda647Slclee     const char *fmt, va_list ap);
2393ccda647Slclee static void cmlb_log(dev_info_t *dev, char *label, uint_t level,
2403ccda647Slclee     const char *fmt, ...);
2413ccda647Slclee 
2423ccda647Slclee int
2433ccda647Slclee _init(void)
2443ccda647Slclee {
2453ccda647Slclee 	mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
2463ccda647Slclee 	return (mod_install(&modlinkage));
2473ccda647Slclee }
2483ccda647Slclee 
2493ccda647Slclee int
2503ccda647Slclee _info(struct modinfo *modinfop)
2513ccda647Slclee {
2523ccda647Slclee 	return (mod_info(&modlinkage, modinfop));
2533ccda647Slclee }
2543ccda647Slclee 
2553ccda647Slclee int
2563ccda647Slclee _fini(void)
2573ccda647Slclee {
2583ccda647Slclee 	int err;
2593ccda647Slclee 
2603ccda647Slclee 	if ((err = mod_remove(&modlinkage)) != 0) {
2613ccda647Slclee 		return (err);
2623ccda647Slclee 	}
2633ccda647Slclee 
2643ccda647Slclee 	mutex_destroy(&cmlb_log_mutex);
2653ccda647Slclee 	return (err);
2663ccda647Slclee }
2673ccda647Slclee 
2683ccda647Slclee /*
2693ccda647Slclee  * cmlb_dbg is used for debugging to log additional info
2703ccda647Slclee  * Level of output is controlled via cmlb_level_mask setting.
2713ccda647Slclee  */
2723ccda647Slclee static void
273e8fb11a1Sshidokht cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
2743ccda647Slclee {
2753ccda647Slclee 	va_list		ap;
2763ccda647Slclee 	dev_info_t	*dev;
2773ccda647Slclee 	uint_t		level_mask = 0;
2783ccda647Slclee 
279e8fb11a1Sshidokht 	ASSERT(cl != NULL);
280e8fb11a1Sshidokht 	dev = CMLB_DEVINFO(cl);
2813ccda647Slclee 	ASSERT(dev != NULL);
2823ccda647Slclee 	/*
2833ccda647Slclee 	 * Filter messages based on the global component and level masks,
284e8fb11a1Sshidokht 	 * also print if cl matches the value of cmlb_debug_cl, or if
285e8fb11a1Sshidokht 	 * cmlb_debug_cl is set to NULL.
2863ccda647Slclee 	 */
2873ccda647Slclee 	if (comp & CMLB_TRACE)
2883ccda647Slclee 		level_mask |= CMLB_LOGMASK_TRACE;
2893ccda647Slclee 
2903ccda647Slclee 	if (comp & CMLB_INFO)
2913ccda647Slclee 		level_mask |= CMLB_LOGMASK_INFO;
2923ccda647Slclee 
2933ccda647Slclee 	if (comp & CMLB_ERROR)
2943ccda647Slclee 		level_mask |= CMLB_LOGMASK_ERROR;
2953ccda647Slclee 
2963ccda647Slclee 	if ((cmlb_level_mask & level_mask) &&
297e8fb11a1Sshidokht 	    ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
2983ccda647Slclee 		va_start(ap, fmt);
299e8fb11a1Sshidokht 		cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
3003ccda647Slclee 		va_end(ap);
3013ccda647Slclee 	}
3023ccda647Slclee }
3033ccda647Slclee 
3043ccda647Slclee /*
3053ccda647Slclee  * cmlb_log is basically a duplicate of scsi_log. It is redefined here
3063ccda647Slclee  * so that this module does not depend on scsi module.
3073ccda647Slclee  */
3083ccda647Slclee static void
3093ccda647Slclee cmlb_log(dev_info_t *dev, char *label, uint_t level, const char *fmt, ...)
3103ccda647Slclee {
3113ccda647Slclee 	va_list		ap;
3123ccda647Slclee 
3133ccda647Slclee 	va_start(ap, fmt);
3143ccda647Slclee 	cmlb_v_log(dev, label, level, fmt, ap);
3153ccda647Slclee 	va_end(ap);
3163ccda647Slclee }
3173ccda647Slclee 
3183ccda647Slclee static void
3193ccda647Slclee cmlb_v_log(dev_info_t *dev, char *label, uint_t level, const char *fmt,
3203ccda647Slclee     va_list ap)
3213ccda647Slclee {
3223ccda647Slclee 	static char 	name[256];
3233ccda647Slclee 	int 		log_only = 0;
3243ccda647Slclee 	int 		boot_only = 0;
3253ccda647Slclee 	int 		console_only = 0;
3263ccda647Slclee 
3273ccda647Slclee 	mutex_enter(&cmlb_log_mutex);
3283ccda647Slclee 
3293ccda647Slclee 	if (dev) {
3303ccda647Slclee 		if (level == CE_PANIC || level == CE_WARN ||
3313ccda647Slclee 		    level == CE_NOTE) {
3323ccda647Slclee 			(void) sprintf(name, "%s (%s%d):\n",
3333ccda647Slclee 			    ddi_pathname(dev, cmlb_log_buffer),
3343ccda647Slclee 			    label, ddi_get_instance(dev));
3353ccda647Slclee 		} else {
3363ccda647Slclee 			name[0] = '\0';
3373ccda647Slclee 		}
3383ccda647Slclee 	} else {
3393ccda647Slclee 		(void) sprintf(name, "%s:", label);
3403ccda647Slclee 	}
3413ccda647Slclee 
3423ccda647Slclee 	(void) vsprintf(cmlb_log_buffer, fmt, ap);
3433ccda647Slclee 
3443ccda647Slclee 	switch (cmlb_log_buffer[0]) {
3453ccda647Slclee 	case '!':
3463ccda647Slclee 		log_only = 1;
3473ccda647Slclee 		break;
3483ccda647Slclee 	case '?':
3493ccda647Slclee 		boot_only = 1;
3503ccda647Slclee 		break;
3513ccda647Slclee 	case '^':
3523ccda647Slclee 		console_only = 1;
3533ccda647Slclee 		break;
3543ccda647Slclee 	}
3553ccda647Slclee 
3563ccda647Slclee 	switch (level) {
3573ccda647Slclee 	case CE_NOTE:
3583ccda647Slclee 		level = CE_CONT;
3593ccda647Slclee 		/* FALLTHROUGH */
3603ccda647Slclee 	case CE_CONT:
3613ccda647Slclee 	case CE_WARN:
3623ccda647Slclee 	case CE_PANIC:
3633ccda647Slclee 		if (boot_only) {
3643ccda647Slclee 			cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
3653ccda647Slclee 		} else if (console_only) {
3663ccda647Slclee 			cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
3673ccda647Slclee 		} else if (log_only) {
3683ccda647Slclee 			cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
3693ccda647Slclee 		} else {
3703ccda647Slclee 			cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
3713ccda647Slclee 		}
3723ccda647Slclee 		break;
3733ccda647Slclee 	case CE_IGNORE:
3743ccda647Slclee 		break;
3753ccda647Slclee 	default:
3763ccda647Slclee 		cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
3773ccda647Slclee 		break;
3783ccda647Slclee 	}
3793ccda647Slclee 	mutex_exit(&cmlb_log_mutex);
3803ccda647Slclee }
3813ccda647Slclee 
3823ccda647Slclee 
3833ccda647Slclee /*
3843ccda647Slclee  * cmlb_alloc_handle:
3853ccda647Slclee  *
3863ccda647Slclee  *	Allocates a handle.
3873ccda647Slclee  *
3883ccda647Slclee  * Arguments:
3893ccda647Slclee  *	cmlbhandlep	pointer to handle
3903ccda647Slclee  *
3913ccda647Slclee  * Notes:
3923ccda647Slclee  *	Allocates a handle and stores the allocated handle in the area
3933ccda647Slclee  *	pointed to by cmlbhandlep
3943ccda647Slclee  *
3953ccda647Slclee  * Context:
3963ccda647Slclee  *	Kernel thread only (can sleep).
3973ccda647Slclee  */
3983ccda647Slclee void
3993ccda647Slclee cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
4003ccda647Slclee {
401e8fb11a1Sshidokht 	struct cmlb_lun 	*cl;
4023ccda647Slclee 
403e8fb11a1Sshidokht 	cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
4043ccda647Slclee 	ASSERT(cmlbhandlep != NULL);
4053ccda647Slclee 
406e8fb11a1Sshidokht 	cl->cl_state = CMLB_INITED;
407e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
408e8fb11a1Sshidokht 	mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
4093ccda647Slclee 
410e8fb11a1Sshidokht 	*cmlbhandlep = (cmlb_handle_t)(cl);
4113ccda647Slclee }
4123ccda647Slclee 
4133ccda647Slclee /*
4143ccda647Slclee  * cmlb_free_handle
4153ccda647Slclee  *
4163ccda647Slclee  *	Frees handle.
4173ccda647Slclee  *
4183ccda647Slclee  * Arguments:
4193ccda647Slclee  *	cmlbhandlep	pointer to handle
4203ccda647Slclee  */
4213ccda647Slclee void
4223ccda647Slclee cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
4233ccda647Slclee {
424e8fb11a1Sshidokht 	struct cmlb_lun 	*cl;
4253ccda647Slclee 
426e8fb11a1Sshidokht 	cl = (struct cmlb_lun *)*cmlbhandlep;
427e8fb11a1Sshidokht 	if (cl != NULL) {
428e8fb11a1Sshidokht 		mutex_destroy(CMLB_MUTEX(cl));
429e8fb11a1Sshidokht 		kmem_free(cl, sizeof (struct cmlb_lun));
4303ccda647Slclee 	}
4313ccda647Slclee 
4323ccda647Slclee }
4333ccda647Slclee 
4343ccda647Slclee /*
4353ccda647Slclee  * cmlb_attach:
4363ccda647Slclee  *
4373ccda647Slclee  *	Attach handle to device, create minor nodes for device.
4383ccda647Slclee  *
4393ccda647Slclee  * Arguments:
4403ccda647Slclee  * 	devi		pointer to device's dev_info structure.
4413ccda647Slclee  * 	tgopsp		pointer to array of functions cmlb can use to callback
4423ccda647Slclee  *			to target driver.
4433ccda647Slclee  *
4443ccda647Slclee  *	device_type	Peripheral device type as defined in
4453ccda647Slclee  *			scsi/generic/inquiry.h
4463ccda647Slclee  *
4473ccda647Slclee  *	is_removable	whether or not device is removable.
4483ccda647Slclee  *			0 non-removable, 1 removable.
4493ccda647Slclee  *
450e8fb11a1Sshidokht  *	is_hotpluggable	whether or not device is hotpluggable.
451e8fb11a1Sshidokht  *			0 non-hotpluggable, 1 hotpluggable.
452e8fb11a1Sshidokht  *
4533ccda647Slclee  *	node_type	minor node type (as used by ddi_create_minor_node)
4543ccda647Slclee  *
4553ccda647Slclee  *	alter_behavior
4563ccda647Slclee  *			bit flags:
4573ccda647Slclee  *
4583ccda647Slclee  *			CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
4593ccda647Slclee  *			an alternate slice for the default label, if
4603ccda647Slclee  *			device type is DTYPE_DIRECT an architectures default
4613ccda647Slclee  *			label type is VTOC16.
4623ccda647Slclee  *			Otherwise alternate slice will no be created.
4633ccda647Slclee  *
4643ccda647Slclee  *
4653ccda647Slclee  *			CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
4663ccda647Slclee  *			geometry and label for DKIOCGGEOM and DKIOCGVTOC
4673ccda647Slclee  *			on architecture with VTOC8 label types.
4683ccda647Slclee  *
469e8fb11a1Sshidokht  * 			CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
470e8fb11a1Sshidokht  *                      one bug in obtaining capacity (in sd):
471e8fb11a1Sshidokht  *			SCSI READ_CAPACITY command returns the LBA number of the
472e8fb11a1Sshidokht  *			last logical block, but sd once treated this number as
473e8fb11a1Sshidokht  *			disks' capacity on x86 platform. And LBAs are addressed
474e8fb11a1Sshidokht  *			based 0. So the last block was lost on x86 platform.
475e8fb11a1Sshidokht  *
476e8fb11a1Sshidokht  *			Now, we remove this workaround. In order for present sd
477e8fb11a1Sshidokht  *			driver to work with disks which are labeled/partitioned
478e8fb11a1Sshidokht  *			via previous sd, we add workaround as follows:
479e8fb11a1Sshidokht  *
480e8fb11a1Sshidokht  *			1) Locate backup EFI label: cmlb searches the next to
481e8fb11a1Sshidokht  *			   last
482e8fb11a1Sshidokht  *			   block for backup EFI label. If fails, it will
483e8fb11a1Sshidokht  *			   turn to the last block for backup EFI label;
484e8fb11a1Sshidokht  *
485e8fb11a1Sshidokht  *			2) Clear backup EFI label: cmlb first search the last
486e8fb11a1Sshidokht  *			   block for backup EFI label, and will search the
487e8fb11a1Sshidokht  *			   next to last block only if failed for the last
488e8fb11a1Sshidokht  *			   block.
489e8fb11a1Sshidokht  *
490e8fb11a1Sshidokht  *			3) Calculate geometry:refer to cmlb_convert_geometry()
491e8fb11a1Sshidokht  *			   If capacity increasing by 1 causes disks' capacity
492e8fb11a1Sshidokht  *			   to cross over the limits in table CHS_values,
493e8fb11a1Sshidokht  *			   geometry info will change. This will raise an issue:
494e8fb11a1Sshidokht  *			   In case that primary VTOC label is destroyed, format
495e8fb11a1Sshidokht  *			   commandline can restore it via backup VTOC labels.
496e8fb11a1Sshidokht  *			   And format locates backup VTOC labels by use of
497e8fb11a1Sshidokht  *			   geometry. So changing geometry will
498e8fb11a1Sshidokht  *			   prevent format from finding backup VTOC labels. To
499e8fb11a1Sshidokht  *			   eliminate this side effect for compatibility,
500e8fb11a1Sshidokht  *			   sd uses (capacity -1) to calculate geometry;
501e8fb11a1Sshidokht  *
502e8fb11a1Sshidokht  *			4) 1TB disks: some important data structures use
503e8fb11a1Sshidokht  *			   32-bit signed long/int (for example, daddr_t),
504e8fb11a1Sshidokht  *			   so that sd doesn't support a disk with capacity
505e8fb11a1Sshidokht  *			   larger than 1TB on 32-bit platform. However,
506e8fb11a1Sshidokht  *			   for exactly 1TB disk, it was treated as (1T - 512)B
507e8fb11a1Sshidokht  *			   in the past, and could have valid Solaris
508e8fb11a1Sshidokht  *			   partitions. To workaround this, if an exactly 1TB
509e8fb11a1Sshidokht  *			   disk has Solaris fdisk partition, it will be allowed
510e8fb11a1Sshidokht  *			   to work with sd.
511e8fb11a1Sshidokht  *
512e8fb11a1Sshidokht  *
5133ccda647Slclee  *
514843e1988Sjohnlev  *			CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
515843e1988Sjohnlev  *			the entire disk, if there is no valid partition info.
516843e1988Sjohnlev  *			If there is a valid Solaris partition, s0 and s2 will
517843e1988Sjohnlev  *			only cover the entire Solaris partition.
518843e1988Sjohnlev  *
519843e1988Sjohnlev  *
5203ccda647Slclee  *	cmlbhandle	cmlb handle associated with device
5213ccda647Slclee  *
522e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
523e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
524e8fb11a1Sshidokht  *
5253ccda647Slclee  * Notes:
5263ccda647Slclee  *	Assumes a default label based on capacity for non-removable devices.
5273ccda647Slclee  *	If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
5283ccda647Slclee  *	for the architecture).
5293ccda647Slclee  *
5303ccda647Slclee  *	For removable devices, default label type is assumed to be VTOC
5313ccda647Slclee  *	type. Create minor nodes based on a default label type.
5323ccda647Slclee  *	Label on the media is not validated.
5333ccda647Slclee  *	minor number consists of:
5343ccda647Slclee  *		if _SUNOS_VTOC_8 is defined
5353ccda647Slclee  *			lowest 3 bits is taken as partition number
5363ccda647Slclee  *			the rest is instance number
5373ccda647Slclee  *		if _SUNOS_VTOC_16 is defined
5383ccda647Slclee  *			lowest 6 bits is taken as partition number
5393ccda647Slclee  *			the rest is instance number
5403ccda647Slclee  *
5413ccda647Slclee  *
5423ccda647Slclee  * Return values:
5433ccda647Slclee  *	0 	Success
5443ccda647Slclee  * 	ENXIO 	creating minor nodes failed.
545e8fb11a1Sshidokht  *	EINVAL  invalid arg, unsupported tg_ops version
5463ccda647Slclee  */
5473ccda647Slclee int
5483ccda647Slclee cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
549e8fb11a1Sshidokht     int is_removable, int is_hotpluggable, char *node_type,
550e8fb11a1Sshidokht     int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
5513ccda647Slclee {
5523ccda647Slclee 
553e8fb11a1Sshidokht 	struct cmlb_lun	*cl = (struct cmlb_lun *)cmlbhandle;
5543ccda647Slclee 	diskaddr_t	cap;
5553ccda647Slclee 	int		status;
5563ccda647Slclee 
557e8fb11a1Sshidokht 	if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
558e8fb11a1Sshidokht 		return (EINVAL);
5593ccda647Slclee 
560e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
561e8fb11a1Sshidokht 
562e8fb11a1Sshidokht 	CMLB_DEVINFO(cl) = devi;
563e8fb11a1Sshidokht 	cl->cmlb_tg_ops = tgopsp;
564e8fb11a1Sshidokht 	cl->cl_device_type = device_type;
565e8fb11a1Sshidokht 	cl->cl_is_removable = is_removable;
566e8fb11a1Sshidokht 	cl->cl_is_hotpluggable = is_hotpluggable;
567e8fb11a1Sshidokht 	cl->cl_node_type = node_type;
568e8fb11a1Sshidokht 	cl->cl_sys_blocksize = DEV_BSIZE;
569e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
570e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_VTOC;
571e8fb11a1Sshidokht 	cl->cl_alter_behavior = alter_behavior;
572e8fb11a1Sshidokht 	cl->cl_reserved = -1;
5733ccda647Slclee 
5743ccda647Slclee 	if (is_removable != 0) {
575e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
576e8fb11a1Sshidokht 		status = DK_TG_GETCAP(cl, &cap, tg_cookie);
577e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
5783ccda647Slclee 		if (status == 0 && cap > DK_MAX_BLOCKS) {
5793ccda647Slclee 			/* set default EFI if > 1TB */
580e8fb11a1Sshidokht 			cl->cl_def_labeltype = CMLB_LABEL_EFI;
5813ccda647Slclee 		}
5823ccda647Slclee 	}
5833ccda647Slclee 
5843ccda647Slclee 	/* create minor nodes based on default label type */
585e8fb11a1Sshidokht 	cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
586e8fb11a1Sshidokht 	cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
5873ccda647Slclee 
588e8fb11a1Sshidokht 	if (cmlb_create_minor_nodes(cl) != 0) {
589e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
5903ccda647Slclee 		return (ENXIO);
5913ccda647Slclee 	}
5923ccda647Slclee 
593e8fb11a1Sshidokht 	cl->cl_state = CMLB_ATTACHED;
5943ccda647Slclee 
595e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
5963ccda647Slclee 	return (0);
5973ccda647Slclee }
5983ccda647Slclee 
5993ccda647Slclee /*
6003ccda647Slclee  * cmlb_detach:
6013ccda647Slclee  *
6023ccda647Slclee  * Invalidate in-core labeling data and remove all minor nodes for
6033ccda647Slclee  * the device associate with handle.
6043ccda647Slclee  *
6053ccda647Slclee  * Arguments:
6063ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
6073ccda647Slclee  *
608e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
609e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
610e8fb11a1Sshidokht  *
6113ccda647Slclee  */
612e8fb11a1Sshidokht /*ARGSUSED1*/
6133ccda647Slclee void
614e8fb11a1Sshidokht cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
6153ccda647Slclee {
616e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
6173ccda647Slclee 
618e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
619e8fb11a1Sshidokht 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
620e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
621e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
622e8fb11a1Sshidokht 	cl->cl_state = CMLB_INITED;
623e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
6243ccda647Slclee }
6253ccda647Slclee 
6263ccda647Slclee /*
6273ccda647Slclee  * cmlb_validate:
6283ccda647Slclee  *
6293ccda647Slclee  *	Validates label.
6303ccda647Slclee  *
6313ccda647Slclee  * Arguments
6323ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
6333ccda647Slclee  *
634e8fb11a1Sshidokht  *	flags		operation flags. used for verbosity control
635e8fb11a1Sshidokht  *
636e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
637e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
638e8fb11a1Sshidokht  *
639e8fb11a1Sshidokht  *
6403ccda647Slclee  * Notes:
6413ccda647Slclee  *	If new label type is different from the current, adjust minor nodes
6423ccda647Slclee  *	accordingly.
6433ccda647Slclee  *
6443ccda647Slclee  * Return values:
6453ccda647Slclee  *	0		success
6463ccda647Slclee  *			Note: having fdisk but no solaris partition is assumed
6473ccda647Slclee  *			success.
6483ccda647Slclee  *
6493ccda647Slclee  *	ENOMEM		memory allocation failed
6503ccda647Slclee  *	EIO		i/o errors during read or get capacity
6513ccda647Slclee  * 	EACCESS		reservation conflicts
6523ccda647Slclee  * 	EINVAL		label was corrupt, or no default label was assumed
6533ccda647Slclee  *	ENXIO		invalid handle
6543ccda647Slclee  */
6553ccda647Slclee int
656e8fb11a1Sshidokht cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
6573ccda647Slclee {
658e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
6593ccda647Slclee 	int 		rval;
6603ccda647Slclee 	int  		ret = 0;
6613ccda647Slclee 
6623ccda647Slclee 	/*
663e8fb11a1Sshidokht 	 * Temp work-around checking cl for NULL since there is a bug
6643ccda647Slclee 	 * in sd_detach calling this routine from taskq_dispatch
6653ccda647Slclee 	 * inited function.
6663ccda647Slclee 	 */
667e8fb11a1Sshidokht 	if (cl == NULL)
6683ccda647Slclee 		return (ENXIO);
6693ccda647Slclee 
670e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
671e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
672e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
6733ccda647Slclee 		return (ENXIO);
6743ccda647Slclee 	}
6753ccda647Slclee 
676e8fb11a1Sshidokht 	rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, 1,
677e8fb11a1Sshidokht 	    flags, tg_cookie);
6783ccda647Slclee 
6793ccda647Slclee 	if (rval == ENOTSUP) {
680e8fb11a1Sshidokht 		if (cl->cl_f_geometry_is_valid == TRUE) {
681e8fb11a1Sshidokht 			cl->cl_cur_labeltype = CMLB_LABEL_EFI;
6823ccda647Slclee 			ret = 0;
6833ccda647Slclee 		} else {
6843ccda647Slclee 			ret = EINVAL;
6853ccda647Slclee 		}
6863ccda647Slclee 	} else {
6873ccda647Slclee 		ret = rval;
6883ccda647Slclee 		if (ret == 0)
689e8fb11a1Sshidokht 			cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
6903ccda647Slclee 	}
6913ccda647Slclee 
6923ccda647Slclee 	if (ret == 0)
693e8fb11a1Sshidokht 		(void) cmlb_create_minor_nodes(cl);
6943ccda647Slclee 
695e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
6963ccda647Slclee 	return (ret);
6973ccda647Slclee }
6983ccda647Slclee 
6993ccda647Slclee /*
7003ccda647Slclee  * cmlb_invalidate:
7013ccda647Slclee  *	Invalidate in core label data
7023ccda647Slclee  *
7033ccda647Slclee  * Arguments:
7043ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
705e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
706e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
7073ccda647Slclee  */
708e8fb11a1Sshidokht /*ARGSUSED1*/
7093ccda647Slclee void
710e8fb11a1Sshidokht cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
7113ccda647Slclee {
712e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
7133ccda647Slclee 
714e8fb11a1Sshidokht 	if (cl == NULL)
7153ccda647Slclee 		return;
7163ccda647Slclee 
717e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
718e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
719e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
7203ccda647Slclee }
7213ccda647Slclee 
7223ccda647Slclee /*
723e8fb11a1Sshidokht  * cmlb_is_valid
724e8fb11a1Sshidokht  * 	Get status on whether the incore label/geom data is valid
725e8fb11a1Sshidokht  *
726e8fb11a1Sshidokht  * Arguments:
727e8fb11a1Sshidokht  *	cmlbhandle      cmlb handle associated with device.
728e8fb11a1Sshidokht  *
729e8fb11a1Sshidokht  * Return values:
730e8fb11a1Sshidokht  *	TRUE if incore label/geom data is valid.
731e8fb11a1Sshidokht  *	FALSE otherwise.
732e8fb11a1Sshidokht  *
733e8fb11a1Sshidokht  */
734e8fb11a1Sshidokht 
735e8fb11a1Sshidokht 
736e8fb11a1Sshidokht int
737e8fb11a1Sshidokht cmlb_is_valid(cmlb_handle_t cmlbhandle)
738e8fb11a1Sshidokht {
739e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
740e8fb11a1Sshidokht 
741e8fb11a1Sshidokht 	if (cmlbhandle == NULL)
742e8fb11a1Sshidokht 		return (FALSE);
743e8fb11a1Sshidokht 
744e8fb11a1Sshidokht 	return (cl->cl_f_geometry_is_valid);
745e8fb11a1Sshidokht 
746e8fb11a1Sshidokht }
747e8fb11a1Sshidokht 
748e8fb11a1Sshidokht 
749e8fb11a1Sshidokht 
750e8fb11a1Sshidokht /*
7513ccda647Slclee  * cmlb_close:
7523ccda647Slclee  *
7533ccda647Slclee  * Close the device, revert to a default label minor node for the device,
7543ccda647Slclee  * if it is removable.
7553ccda647Slclee  *
7563ccda647Slclee  * Arguments:
7573ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
7583ccda647Slclee  *
759e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
760e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
7613ccda647Slclee  * Return values:
7623ccda647Slclee  *	0	Success
7633ccda647Slclee  * 	ENXIO	Re-creating minor node failed.
7643ccda647Slclee  */
765e8fb11a1Sshidokht /*ARGSUSED1*/
7663ccda647Slclee int
767e8fb11a1Sshidokht cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
7683ccda647Slclee {
769e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
7703ccda647Slclee 
771e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
772e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
7733ccda647Slclee 
7743ccda647Slclee 	/* revert to default minor node for this device */
775e8fb11a1Sshidokht 	if (ISREMOVABLE(cl)) {
776e8fb11a1Sshidokht 		cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
777e8fb11a1Sshidokht 		(void) cmlb_create_minor_nodes(cl);
7783ccda647Slclee 	}
7793ccda647Slclee 
780e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
7813ccda647Slclee 	return (0);
7823ccda647Slclee }
7833ccda647Slclee 
7843ccda647Slclee /*
7853ccda647Slclee  * cmlb_get_devid_block:
7863ccda647Slclee  *	 get the block number where device id is stored.
7873ccda647Slclee  *
7883ccda647Slclee  * Arguments:
7893ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
7903ccda647Slclee  *	devidblockp	pointer to block number.
791e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
792e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
7933ccda647Slclee  *
7943ccda647Slclee  * Notes:
7953ccda647Slclee  *	It stores the block number of device id in the area pointed to
7963ccda647Slclee  *	by devidblockp.
7973ccda647Slclee  * 	with the block number of device id.
7983ccda647Slclee  *
7993ccda647Slclee  * Return values:
8003ccda647Slclee  *	0	success
8013ccda647Slclee  *	EINVAL 	device id does not apply to current label type.
8023ccda647Slclee  */
803e8fb11a1Sshidokht /*ARGSUSED2*/
8043ccda647Slclee int
805e8fb11a1Sshidokht cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
806e8fb11a1Sshidokht     void *tg_cookie)
8073ccda647Slclee {
8083ccda647Slclee 	daddr_t			spc, blk, head, cyl;
809e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8103ccda647Slclee 
811e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
812e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
813e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
8143ccda647Slclee 		return (EINVAL);
8153ccda647Slclee 	}
8163ccda647Slclee 
817e8fb11a1Sshidokht 	if ((cl->cl_f_geometry_is_valid == FALSE) ||
818e8fb11a1Sshidokht 	    (cl->cl_solaris_size < DK_LABEL_LOC)) {
819e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
820e8fb11a1Sshidokht 		return (EINVAL);
821e8fb11a1Sshidokht 	}
822e8fb11a1Sshidokht 
823e8fb11a1Sshidokht 
824e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
825e8fb11a1Sshidokht 		if (cl->cl_reserved != -1) {
826e8fb11a1Sshidokht 			blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
827e8fb11a1Sshidokht 		} else {
828e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
829e8fb11a1Sshidokht 			return (EINVAL);
830e8fb11a1Sshidokht 		}
831e8fb11a1Sshidokht 	} else {
8323ccda647Slclee 		/* this geometry doesn't allow us to write a devid */
833e8fb11a1Sshidokht 		if (cl->cl_g.dkg_acyl < 2) {
834e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
8353ccda647Slclee 			return (EINVAL);
8363ccda647Slclee 		}
8373ccda647Slclee 
8383ccda647Slclee 		/*
8393ccda647Slclee 		 * Subtract 2 guarantees that the next to last cylinder
8403ccda647Slclee 		 * is used
8413ccda647Slclee 		 */
842e8fb11a1Sshidokht 		cyl  = cl->cl_g.dkg_ncyl  + cl->cl_g.dkg_acyl - 2;
843e8fb11a1Sshidokht 		spc  = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
844e8fb11a1Sshidokht 		head = cl->cl_g.dkg_nhead - 1;
845e8fb11a1Sshidokht 		blk  = (cyl * (spc - cl->cl_g.dkg_apc)) +
846e8fb11a1Sshidokht 		    (head * cl->cl_g.dkg_nsect) + 1;
8473ccda647Slclee 	}
848e8fb11a1Sshidokht 
8493ccda647Slclee 	*devidblockp = blk;
850e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
8513ccda647Slclee 	return (0);
8523ccda647Slclee }
8533ccda647Slclee 
8543ccda647Slclee /*
8553ccda647Slclee  * cmlb_partinfo:
8563ccda647Slclee  *	Get partition info for specified partition number.
8573ccda647Slclee  *
8583ccda647Slclee  * Arguments:
8593ccda647Slclee  *	cmlbhandle	cmlb handle associated with device.
8603ccda647Slclee  *	part		partition number
8613ccda647Slclee  *	nblocksp	pointer to number of blocks
8623ccda647Slclee  *	startblockp	pointer to starting block
8633ccda647Slclee  *	partnamep	pointer to name of partition
8643ccda647Slclee  *	tagp		pointer to tag info
865e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
866e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
8673ccda647Slclee  *
8683ccda647Slclee  *
8693ccda647Slclee  * Notes:
8703ccda647Slclee  *	If in-core label is not valid, this functions tries to revalidate
8713ccda647Slclee  *	the label. If label is valid, it stores the total number of blocks
8723ccda647Slclee  *	in this partition in the area pointed to by nblocksp, starting
8733ccda647Slclee  *	block number in area pointed to by startblockp,  pointer to partition
8743ccda647Slclee  *	name in area pointed to by partnamep, and tag value in area
8753ccda647Slclee  *	pointed by tagp.
8763ccda647Slclee  *	For EFI labels, tag value will be set to 0.
8773ccda647Slclee  *
8783ccda647Slclee  *	For all nblocksp, startblockp and partnamep, tagp, a value of NULL
8793ccda647Slclee  *	indicates the corresponding info is not requested.
8803ccda647Slclee  *
8813ccda647Slclee  *
8823ccda647Slclee  * Return values:
8833ccda647Slclee  *	0	success
8843ccda647Slclee  *	EINVAL  no valid label or requested partition number is invalid.
8853ccda647Slclee  *
8863ccda647Slclee  */
8873ccda647Slclee int
8883ccda647Slclee cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
889e8fb11a1Sshidokht     diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
8903ccda647Slclee {
8913ccda647Slclee 
892e8fb11a1Sshidokht 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8933ccda647Slclee 	int rval;
8943ccda647Slclee 
895e8fb11a1Sshidokht 	ASSERT(cl != NULL);
896e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
897e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
898e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
8993ccda647Slclee 		return (EINVAL);
9003ccda647Slclee 	}
9013ccda647Slclee 
9023ccda647Slclee 	if (part  < 0 || part >= MAXPART) {
9033ccda647Slclee 		rval = EINVAL;
9043ccda647Slclee 	} else {
905e8fb11a1Sshidokht 		if (cl->cl_f_geometry_is_valid == FALSE)
906e8fb11a1Sshidokht 			(void) cmlb_validate_geometry((struct cmlb_lun *)cl, 0,
907e8fb11a1Sshidokht 			    0, tg_cookie);
908e8fb11a1Sshidokht 
909e8fb11a1Sshidokht 		if ((cl->cl_f_geometry_is_valid == FALSE) ||
910e8fb11a1Sshidokht 		    (part < NDKMAP && cl->cl_solaris_size == 0)) {
9113ccda647Slclee 			rval = EINVAL;
9123ccda647Slclee 		} else {
9133ccda647Slclee 			if (startblockp != NULL)
914e8fb11a1Sshidokht 				*startblockp = (diskaddr_t)cl->cl_offset[part];
9153ccda647Slclee 
9163ccda647Slclee 			if (nblocksp != NULL)
9173ccda647Slclee 				*nblocksp = (diskaddr_t)
918e8fb11a1Sshidokht 				    cl->cl_map[part].dkl_nblk;
9193ccda647Slclee 
9203ccda647Slclee 			if (tagp != NULL)
921e8fb11a1Sshidokht 				if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
9223ccda647Slclee 					*tagp = V_UNASSIGNED;
9233ccda647Slclee 				else
924e8fb11a1Sshidokht 					*tagp = cl->cl_vtoc.v_part[part].p_tag;
9253ccda647Slclee 			rval = 0;
9263ccda647Slclee 		}
9273ccda647Slclee 
9283ccda647Slclee 		/* consistent with behavior of sd for getting minor name */
9293ccda647Slclee 		if (partnamep != NULL)
9303ccda647Slclee 			*partnamep = dk_minor_data[part].name;
9313ccda647Slclee 
9323ccda647Slclee 	}
9333ccda647Slclee 
934e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
9353ccda647Slclee 	return (rval);
9363ccda647Slclee }
9373ccda647Slclee 
938e8fb11a1Sshidokht /* Caller should make sure Test Unit Ready succeeds before calling this. */
9393ccda647Slclee /*ARGSUSED*/
9403ccda647Slclee int
9413ccda647Slclee cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
942e8fb11a1Sshidokht     int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
9433ccda647Slclee {
9443ccda647Slclee 
9453ccda647Slclee 	int err;
946e8fb11a1Sshidokht 	struct cmlb_lun *cl;
947e8fb11a1Sshidokht 	int status;
9483ccda647Slclee 
949e8fb11a1Sshidokht 	cl = (struct cmlb_lun *)cmlbhandle;
9503ccda647Slclee 
951e8fb11a1Sshidokht 	ASSERT(cl != NULL);
9523ccda647Slclee 
953e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
954e8fb11a1Sshidokht 	if (cl->cl_state < CMLB_ATTACHED) {
955e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
9563ccda647Slclee 		return (EIO);
9573ccda647Slclee 	}
9583ccda647Slclee 
9593ccda647Slclee 	switch (cmd) {
960e8fb11a1Sshidokht 		case DKIOCSVTOC:
9613ccda647Slclee 		case DKIOCSGEOM:
9623ccda647Slclee 		case DKIOCSETEFI:
9633ccda647Slclee 		case DKIOCSMBOOT:
9643ccda647Slclee 			break;
9653ccda647Slclee 		default:
966adc076afSshidokht 			status = cmlb_validate_geometry(cl, 1, CMLB_SILENT,
967adc076afSshidokht 			    tg_cookie);
968e8fb11a1Sshidokht 
9693ccda647Slclee 			/*
970e8fb11a1Sshidokht 			 * VTOC related ioctls except SVTOC/SGEOM should
971e8fb11a1Sshidokht 			 * fail if > 1TB disk and there is not already a VTOC
972e8fb11a1Sshidokht 			 * on the disk.i.e either EFI or blank
973e8fb11a1Sshidokht 			 *
974e8fb11a1Sshidokht 			 * PHYGEOM AND VIRTGEOM succeeds when disk is
975e8fb11a1Sshidokht 			 * EFI labeled but <1TB
9763ccda647Slclee 			 */
977e8fb11a1Sshidokht 
978e8fb11a1Sshidokht 			if (status == ENOTSUP &&
979e8fb11a1Sshidokht 			    cl->cl_f_geometry_is_valid == FALSE) {
980e8fb11a1Sshidokht 				switch (cmd) {
981e8fb11a1Sshidokht 				case DKIOCGAPART:
982e8fb11a1Sshidokht 				case DKIOCGGEOM:
983e8fb11a1Sshidokht 				case DKIOCGVTOC:
984e8fb11a1Sshidokht 				case DKIOCSAPART:
985e8fb11a1Sshidokht 				case DKIOCG_PHYGEOM:
986e8fb11a1Sshidokht 				case DKIOCG_VIRTGEOM:
987e8fb11a1Sshidokht 
988e8fb11a1Sshidokht 					mutex_exit(CMLB_MUTEX(cl));
989e8fb11a1Sshidokht 					return (ENOTSUP);
990e8fb11a1Sshidokht 				}
991e8fb11a1Sshidokht 			} else {
992e8fb11a1Sshidokht 				if ((cl->cl_f_geometry_is_valid == TRUE) &&
993e8fb11a1Sshidokht 				    (cl->cl_solaris_size > 0)) {
994e8fb11a1Sshidokht 					if (cl->cl_vtoc.v_sanity != VTOC_SANE) {
995e8fb11a1Sshidokht 					/*
996e8fb11a1Sshidokht 					 * it is EFI, so return ENOTSUP for
997e8fb11a1Sshidokht 					 * these
998e8fb11a1Sshidokht 					 */
9993ccda647Slclee 					switch (cmd) {
10003ccda647Slclee 					case DKIOCGAPART:
10013ccda647Slclee 					case DKIOCGGEOM:
10023ccda647Slclee 					case DKIOCGVTOC:
10033ccda647Slclee 					case DKIOCSVTOC:
10043ccda647Slclee 					case DKIOCSAPART:
1005e8fb11a1Sshidokht 
1006e8fb11a1Sshidokht 						mutex_exit(CMLB_MUTEX(cl));
10073ccda647Slclee 						return (ENOTSUP);
10083ccda647Slclee 					}
10093ccda647Slclee 				}
10103ccda647Slclee 			}
10113ccda647Slclee 		}
1012e8fb11a1Sshidokht 	}
10133ccda647Slclee 
1014e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
10153ccda647Slclee 
10163ccda647Slclee 	switch (cmd) {
10173ccda647Slclee 	case DKIOCGGEOM:
1018e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1019e8fb11a1Sshidokht 		err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
10203ccda647Slclee 		break;
10213ccda647Slclee 
10223ccda647Slclee 	case DKIOCSGEOM:
1023e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1024e8fb11a1Sshidokht 		err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
10253ccda647Slclee 		break;
10263ccda647Slclee 
10273ccda647Slclee 	case DKIOCGAPART:
1028e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1029e8fb11a1Sshidokht 		err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1030e8fb11a1Sshidokht 		    flag, tg_cookie);
10313ccda647Slclee 		break;
10323ccda647Slclee 
10333ccda647Slclee 	case DKIOCSAPART:
1034e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1035e8fb11a1Sshidokht 		err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
10363ccda647Slclee 		break;
10373ccda647Slclee 
10383ccda647Slclee 	case DKIOCGVTOC:
1039e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1040e8fb11a1Sshidokht 		err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
10413ccda647Slclee 		break;
10423ccda647Slclee 
10433ccda647Slclee 	case DKIOCGETEFI:
1044e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1045e8fb11a1Sshidokht 		err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
10463ccda647Slclee 		break;
10473ccda647Slclee 
10483ccda647Slclee 	case DKIOCPARTITION:
1049e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1050e8fb11a1Sshidokht 		err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
10513ccda647Slclee 		break;
10523ccda647Slclee 
10533ccda647Slclee 	case DKIOCSVTOC:
1054e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1055e8fb11a1Sshidokht 		err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1056e8fb11a1Sshidokht 		    tg_cookie);
10573ccda647Slclee 		break;
10583ccda647Slclee 
10593ccda647Slclee 	case DKIOCSETEFI:
1060e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1061e8fb11a1Sshidokht 		err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
10623ccda647Slclee 		break;
10633ccda647Slclee 
10643ccda647Slclee 	case DKIOCGMBOOT:
1065e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1066e8fb11a1Sshidokht 		err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
10673ccda647Slclee 		break;
10683ccda647Slclee 
10693ccda647Slclee 	case DKIOCSMBOOT:
1070e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1071e8fb11a1Sshidokht 		err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
10723ccda647Slclee 		break;
10733ccda647Slclee 	case DKIOCG_PHYGEOM:
1074e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
10753ccda647Slclee #if defined(__i386) || defined(__amd64)
1076e8fb11a1Sshidokht 		err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag);
10773ccda647Slclee #else
10783ccda647Slclee 		err = ENOTTY;
10793ccda647Slclee #endif
10803ccda647Slclee 		break;
10813ccda647Slclee 	case DKIOCG_VIRTGEOM:
1082e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
10833ccda647Slclee #if defined(__i386) || defined(__amd64)
1084e8fb11a1Sshidokht 		err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
10853ccda647Slclee #else
10863ccda647Slclee 		err = ENOTTY;
10873ccda647Slclee #endif
10883ccda647Slclee 		break;
10893ccda647Slclee 	case DKIOCPARTINFO:
1090e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
10913ccda647Slclee #if defined(__i386) || defined(__amd64)
1092e8fb11a1Sshidokht 		err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
10933ccda647Slclee #else
10943ccda647Slclee 		err = ENOTTY;
10953ccda647Slclee #endif
10963ccda647Slclee 		break;
10973ccda647Slclee 
10983ccda647Slclee 	default:
10993ccda647Slclee 		err = ENOTTY;
11003ccda647Slclee 
11013ccda647Slclee 	}
11023ccda647Slclee 	return (err);
11033ccda647Slclee }
11043ccda647Slclee 
11053ccda647Slclee dev_t
1106e8fb11a1Sshidokht cmlb_make_device(struct cmlb_lun *cl)
11073ccda647Slclee {
1108e8fb11a1Sshidokht 	return (makedevice(ddi_name_to_major(ddi_get_name(CMLB_DEVINFO(cl))),
1109e8fb11a1Sshidokht 	    ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
11103ccda647Slclee }
11113ccda647Slclee 
11123ccda647Slclee /*
11133ccda647Slclee  * Function: cmlb_check_update_blockcount
11143ccda647Slclee  *
11153ccda647Slclee  * Description: If current capacity value is invalid, obtains the
11163ccda647Slclee  *		current capacity from target driver.
11173ccda647Slclee  *
11183ccda647Slclee  * Return Code: 0	success
11193ccda647Slclee  *		EIO	failure
11203ccda647Slclee  */
11213ccda647Slclee static int
1122e8fb11a1Sshidokht cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
11233ccda647Slclee {
11243ccda647Slclee 	int status;
11253ccda647Slclee 	diskaddr_t capacity;
1126e8fb11a1Sshidokht 	uint32_t lbasize;
11273ccda647Slclee 
1128e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
11293ccda647Slclee 
1130e8fb11a1Sshidokht 	if (cl->cl_f_geometry_is_valid == FALSE)  {
1131e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
1132e8fb11a1Sshidokht 		status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1133e8fb11a1Sshidokht 		if (status != 0) {
1134e8fb11a1Sshidokht 			mutex_enter(CMLB_MUTEX(cl));
1135e8fb11a1Sshidokht 			return (EIO);
1136e8fb11a1Sshidokht 		}
1137e8fb11a1Sshidokht 
1138e8fb11a1Sshidokht 		status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1139e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
1140e8fb11a1Sshidokht 		if (status != 0)
1141e8fb11a1Sshidokht 			return (EIO);
1142e8fb11a1Sshidokht 
1143e8fb11a1Sshidokht 		if ((capacity != 0) && (lbasize != 0)) {
1144e8fb11a1Sshidokht 			cl->cl_blockcount = capacity;
1145e8fb11a1Sshidokht 			cl->cl_tgt_blocksize = lbasize;
11463ccda647Slclee 			return (0);
11473ccda647Slclee 		} else
11483ccda647Slclee 			return (EIO);
11493ccda647Slclee 	} else
11503ccda647Slclee 		return (0);
11513ccda647Slclee }
11523ccda647Slclee 
11533ccda647Slclee /*
11543ccda647Slclee  *    Function: cmlb_create_minor_nodes
11553ccda647Slclee  *
11563ccda647Slclee  * Description: Create or adjust the minor device nodes for the instance.
11573ccda647Slclee  * 		Minor nodes are created based on default label type,
11583ccda647Slclee  *		current label type and last label type we created
11593ccda647Slclee  *		minor nodes based on.
11603ccda647Slclee  *
11613ccda647Slclee  *
1162e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
11633ccda647Slclee  *
11643ccda647Slclee  * Return Code: 0 success
11653ccda647Slclee  *		ENXIO	failure.
11663ccda647Slclee  *
11673ccda647Slclee  *     Context: Kernel thread context
11683ccda647Slclee  */
11693ccda647Slclee static int
1170e8fb11a1Sshidokht cmlb_create_minor_nodes(struct cmlb_lun *cl)
11713ccda647Slclee {
11723ccda647Slclee 	struct driver_minor_data	*dmdp;
11733ccda647Slclee 	int				instance;
11743ccda647Slclee 	char				name[48];
11753ccda647Slclee 	cmlb_label_t			newlabeltype;
11763ccda647Slclee 
1177e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1178e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
11793ccda647Slclee 
11803ccda647Slclee 
11813ccda647Slclee 	/* check the most common case */
1182e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1183e8fb11a1Sshidokht 	    cl->cl_last_labeltype == cl->cl_cur_labeltype) {
11843ccda647Slclee 		/* do nothing */
11853ccda647Slclee 		return (0);
11863ccda647Slclee 	}
11873ccda647Slclee 
1188e8fb11a1Sshidokht 	if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
11893ccda647Slclee 		/* we should never get here */
11903ccda647Slclee 		return (ENXIO);
11913ccda647Slclee 	}
11923ccda647Slclee 
1193e8fb11a1Sshidokht 	if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
11943ccda647Slclee 		/* first time during attach */
1195e8fb11a1Sshidokht 		newlabeltype = cl->cl_def_labeltype;
11963ccda647Slclee 
1197e8fb11a1Sshidokht 		instance = ddi_get_instance(CMLB_DEVINFO(cl));
11983ccda647Slclee 
11993ccda647Slclee 		/* Create all the minor nodes for this target. */
12003ccda647Slclee 		dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
12013ccda647Slclee 		    dk_minor_data;
12023ccda647Slclee 		while (dmdp->name != NULL) {
12033ccda647Slclee 
12043ccda647Slclee 			(void) sprintf(name, "%s", dmdp->name);
12053ccda647Slclee 
1206e8fb11a1Sshidokht 			if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
12073ccda647Slclee 			    dmdp->type,
12083ccda647Slclee 			    (instance << CMLBUNIT_SHIFT) | dmdp->minor,
1209e8fb11a1Sshidokht 			    cl->cl_node_type, NULL) == DDI_FAILURE) {
12103ccda647Slclee 				/*
12113ccda647Slclee 				 * Clean up any nodes that may have been
12123ccda647Slclee 				 * created, in case this fails in the middle
12133ccda647Slclee 				 * of the loop.
12143ccda647Slclee 				 */
1215e8fb11a1Sshidokht 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
12163ccda647Slclee 				return (ENXIO);
12173ccda647Slclee 			}
12183ccda647Slclee 			dmdp++;
12193ccda647Slclee 		}
1220e8fb11a1Sshidokht 		cl->cl_last_labeltype = newlabeltype;
12213ccda647Slclee 		return (0);
12223ccda647Slclee 	}
12233ccda647Slclee 
12243ccda647Slclee 	/* Not first time  */
1225e8fb11a1Sshidokht 	if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1226e8fb11a1Sshidokht 		if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
12273ccda647Slclee 			/* close time, revert to default. */
1228e8fb11a1Sshidokht 			newlabeltype = cl->cl_def_labeltype;
12293ccda647Slclee 		} else {
12303ccda647Slclee 			/*
12313ccda647Slclee 			 * do nothing since the type for which we last created
12323ccda647Slclee 			 * nodes matches the default
12333ccda647Slclee 			 */
12343ccda647Slclee 			return (0);
12353ccda647Slclee 		}
12363ccda647Slclee 	} else {
1237e8fb11a1Sshidokht 		if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
12383ccda647Slclee 			/* We are not closing, use current label type */
1239e8fb11a1Sshidokht 			newlabeltype = cl->cl_cur_labeltype;
12403ccda647Slclee 		} else {
12413ccda647Slclee 			/*
12423ccda647Slclee 			 * do nothing since the type for which we last created
12433ccda647Slclee 			 * nodes matches the current label type
12443ccda647Slclee 			 */
12453ccda647Slclee 			return (0);
12463ccda647Slclee 		}
12473ccda647Slclee 	}
12483ccda647Slclee 
1249e8fb11a1Sshidokht 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
12503ccda647Slclee 
12513ccda647Slclee 	/*
12523ccda647Slclee 	 * Currently we only fix up the s7 node when we are switching
12533ccda647Slclee 	 * label types from or to EFI. This is consistent with
12543ccda647Slclee 	 * current behavior of sd.
12553ccda647Slclee 	 */
12563ccda647Slclee 	if (newlabeltype == CMLB_LABEL_EFI &&
1257e8fb11a1Sshidokht 	    cl->cl_last_labeltype != CMLB_LABEL_EFI) {
12583ccda647Slclee 		/* from vtoc to EFI */
1259e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1260e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1261e8fb11a1Sshidokht 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd",
12623ccda647Slclee 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1263e8fb11a1Sshidokht 		    cl->cl_node_type, NULL);
1264e8fb11a1Sshidokht 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd,raw",
12653ccda647Slclee 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1266e8fb11a1Sshidokht 		    cl->cl_node_type, NULL);
12673ccda647Slclee 	} else {
12683ccda647Slclee 		/* from efi to vtoc */
1269e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1270e8fb11a1Sshidokht 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1271e8fb11a1Sshidokht 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h",
12723ccda647Slclee 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1273e8fb11a1Sshidokht 		    cl->cl_node_type, NULL);
1274e8fb11a1Sshidokht 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h,raw",
12753ccda647Slclee 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1276e8fb11a1Sshidokht 		    cl->cl_node_type, NULL);
12773ccda647Slclee 	}
12783ccda647Slclee 
1279e8fb11a1Sshidokht 	cl->cl_last_labeltype = newlabeltype;
12803ccda647Slclee 	return (0);
12813ccda647Slclee }
12823ccda647Slclee 
12833ccda647Slclee /*
12843ccda647Slclee  *    Function: cmlb_validate_geometry
12853ccda647Slclee  *
12863ccda647Slclee  * Description: Read the label from the disk (if present). Update the unit's
12873ccda647Slclee  *		geometry and vtoc information from the data in the label.
12883ccda647Slclee  *		Verify that the label is valid.
12893ccda647Slclee  *
1290e8fb11a1Sshidokht  *   Arguments:
1291e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
1292e8fb11a1Sshidokht  *
1293e8fb11a1Sshidokht  *	forcerevalid	force revalidation even if we are already valid.
1294e8fb11a1Sshidokht  *	flags		operation flags from target driver. Used for verbosity
1295e8fb11a1Sshidokht  *			control	at this time.
1296e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1297e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
12983ccda647Slclee  *
12993ccda647Slclee  * Return Code: 0 - Successful completion
1300e8fb11a1Sshidokht  *		EINVAL  - Invalid value in cl->cl_tgt_blocksize or
1301e8fb11a1Sshidokht  *			  cl->cl_blockcount; or label on disk is corrupted
13023ccda647Slclee  *			  or unreadable.
13033ccda647Slclee  *		EACCES  - Reservation conflict at the device.
13043ccda647Slclee  *		ENOMEM  - Resource allocation error
13053ccda647Slclee  *		ENOTSUP - geometry not applicable
13063ccda647Slclee  *
13073ccda647Slclee  *     Context: Kernel thread only (can sleep).
13083ccda647Slclee  */
13093ccda647Slclee static int
1310e8fb11a1Sshidokht cmlb_validate_geometry(struct cmlb_lun *cl, int forcerevalid, int flags,
1311e8fb11a1Sshidokht     void *tg_cookie)
13123ccda647Slclee {
13133ccda647Slclee 	int		label_error = 0;
13143ccda647Slclee 	diskaddr_t	capacity;
13153ccda647Slclee 	int		count;
1316e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
1317e8fb11a1Sshidokht 	int forced_under_1t = 0;
1318e8fb11a1Sshidokht #endif
13193ccda647Slclee 
1320e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
13213ccda647Slclee 
1322e8fb11a1Sshidokht 	if ((cl->cl_f_geometry_is_valid == TRUE) && (forcerevalid == 0)) {
1323e8fb11a1Sshidokht 		if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
13243ccda647Slclee 			return (ENOTSUP);
13253ccda647Slclee 		return (0);
13263ccda647Slclee 	}
13273ccda647Slclee 
1328e8fb11a1Sshidokht 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
13293ccda647Slclee 		return (EIO);
13303ccda647Slclee 
1331e8fb11a1Sshidokht 	capacity = cl->cl_blockcount;
13323ccda647Slclee 
13333ccda647Slclee #if defined(_SUNOS_VTOC_16)
13343ccda647Slclee 	/*
13353ccda647Slclee 	 * Set up the "whole disk" fdisk partition; this should always
13363ccda647Slclee 	 * exist, regardless of whether the disk contains an fdisk table
13373ccda647Slclee 	 * or vtoc.
13383ccda647Slclee 	 */
1339e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
13403ccda647Slclee 	/*
13413ccda647Slclee 	 * note if capacity > uint32_max we should be using efi,
13423ccda647Slclee 	 * and not use p0, so the truncation does not matter.
13433ccda647Slclee 	 */
1344e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_nblk  = capacity;
13453ccda647Slclee #endif
13463ccda647Slclee 	/*
13473ccda647Slclee 	 * Refresh the logical and physical geometry caches.
13483ccda647Slclee 	 * (data from MODE SENSE format/rigid disk geometry pages,
13493ccda647Slclee 	 * and scsi_ifgetcap("geometry").
13503ccda647Slclee 	 */
1351e8fb11a1Sshidokht 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
13523ccda647Slclee 
1353e8fb11a1Sshidokht 	label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
13543ccda647Slclee 	if (label_error == 0) {
13553ccda647Slclee 
13563ccda647Slclee 		/* found a valid EFI label */
1357e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl,
13583ccda647Slclee 		    "cmlb_validate_geometry: found EFI label\n");
13593ccda647Slclee 		/*
13603ccda647Slclee 		 * solaris_size and geometry_is_valid are set in
13613ccda647Slclee 		 * cmlb_use_efi
13623ccda647Slclee 		 */
13633ccda647Slclee 		return (ENOTSUP);
13643ccda647Slclee 	}
13653ccda647Slclee 
13663ccda647Slclee 	/* NO EFI label found */
13673ccda647Slclee 
13683ccda647Slclee 	if (capacity > DK_MAX_BLOCKS) {
13693ccda647Slclee 		if (label_error == ESRCH) {
13703ccda647Slclee 			/*
13713ccda647Slclee 			 * they've configured a LUN over 1TB, but used
13723ccda647Slclee 			 * format.dat to restrict format's view of the
13733ccda647Slclee 			 * capacity to be under 1TB
13743ccda647Slclee 			 */
13753ccda647Slclee 			/* i.e > 1Tb with a VTOC < 1TB */
1376e8fb11a1Sshidokht 			if (!(flags & CMLB_SILENT)) {
1377e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1378e8fb11a1Sshidokht 				    CE_WARN, "is >1TB and has a VTOC label: "
1379e8fb11a1Sshidokht 				    "use format(1M) to either decrease the");
13803ccda647Slclee 
1381e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1382e8fb11a1Sshidokht 				    CE_NOTE, "size to be < 1TB or relabel the "
1383e8fb11a1Sshidokht 				    "disk with an EFI label");
1384e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
1385e8fb11a1Sshidokht 				forced_under_1t = 1;
1386e8fb11a1Sshidokht #endif
1387e8fb11a1Sshidokht 			}
13883ccda647Slclee 		} else {
13893ccda647Slclee 			/* unlabeled disk over 1TB */
1390e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
1391e8fb11a1Sshidokht 
1392e8fb11a1Sshidokht 			/*
1393e8fb11a1Sshidokht 			 * Refer to comments on off-by-1 at the head of the file
1394e8fb11a1Sshidokht 			 * A 1TB disk was treated as (1T - 512)B in the past,
1395e8fb11a1Sshidokht 			 * thus, it might have valid solaris partition. We
1396e8fb11a1Sshidokht 			 * will return ENOTSUP later only if this disk has no
1397e8fb11a1Sshidokht 			 * valid solaris partition.
1398e8fb11a1Sshidokht 			 */
1399e8fb11a1Sshidokht 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ||
1400e8fb11a1Sshidokht 			    (cl->cl_sys_blocksize != cl->cl_tgt_blocksize) ||
1401e8fb11a1Sshidokht 			    (capacity - 1 > DK_MAX_BLOCKS))
1402e8fb11a1Sshidokht #endif
14033ccda647Slclee 				return (ENOTSUP);
14043ccda647Slclee 		}
14053ccda647Slclee 	}
14063ccda647Slclee 
14073ccda647Slclee 	label_error = 0;
14083ccda647Slclee 
14093ccda647Slclee 	/*
14103ccda647Slclee 	 * at this point it is either labeled with a VTOC or it is
1411e8fb11a1Sshidokht 	 * under 1TB (<= 1TB actually for off-by-1)
14123ccda647Slclee 	 */
14133ccda647Slclee 
14143ccda647Slclee 	/*
1415e8fb11a1Sshidokht 	 * Only DIRECT ACCESS devices will have Scl labels.
1416e8fb11a1Sshidokht 	 * CD's supposedly have a Scl label, too
14173ccda647Slclee 	 */
1418e8fb11a1Sshidokht 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
14193ccda647Slclee 		struct	dk_label *dkl;
14203ccda647Slclee 		offset_t label_addr;
14213ccda647Slclee 		int	rval;
14223ccda647Slclee 		size_t	buffer_size;
14233ccda647Slclee 
14243ccda647Slclee 		/*
1425e8fb11a1Sshidokht 		 * Note: This will set up cl->cl_solaris_size and
1426e8fb11a1Sshidokht 		 * cl->cl_solaris_offset.
14273ccda647Slclee 		 */
1428e8fb11a1Sshidokht 		rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1429e8fb11a1Sshidokht 		if ((rval != 0) && !ISCD(cl)) {
1430e8fb11a1Sshidokht 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
14313ccda647Slclee 			return (rval);
14323ccda647Slclee 		}
14333ccda647Slclee 
1434e8fb11a1Sshidokht 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1435e8fb11a1Sshidokht 
1436e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
1437e8fb11a1Sshidokht 			/*
1438e8fb11a1Sshidokht 			 * Refer to comments on off-by-1 at the head of the file
1439e8fb11a1Sshidokht 			 * This is for 1TB disk only. Since that there is no
1440e8fb11a1Sshidokht 			 * solaris partitions, return ENOTSUP as we do for
1441e8fb11a1Sshidokht 			 * >1TB disk.
1442e8fb11a1Sshidokht 			 */
1443e8fb11a1Sshidokht 			if (cl->cl_blockcount > DK_MAX_BLOCKS)
1444e8fb11a1Sshidokht 				return (ENOTSUP);
1445e8fb11a1Sshidokht #endif
14463ccda647Slclee 			/*
14473ccda647Slclee 			 * Found fdisk table but no Solaris partition entry,
14483ccda647Slclee 			 * so don't call cmlb_uselabel() and don't create
14493ccda647Slclee 			 * a default label.
14503ccda647Slclee 			 */
14513ccda647Slclee 			label_error = 0;
1452e8fb11a1Sshidokht 			cl->cl_f_geometry_is_valid = TRUE;
14533ccda647Slclee 			goto no_solaris_partition;
14543ccda647Slclee 		}
14553ccda647Slclee 
1456e8fb11a1Sshidokht 		label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1457e8fb11a1Sshidokht 
1458e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
1459e8fb11a1Sshidokht 		/*
1460e8fb11a1Sshidokht 		 * Refer to comments on off-by-1 at the head of the file
1461e8fb11a1Sshidokht 		 * Now, this 1TB disk has valid solaris partition. It
1462e8fb11a1Sshidokht 		 * must be created by previous sd driver, we have to
1463e8fb11a1Sshidokht 		 * treat it as (1T-512)B.
1464e8fb11a1Sshidokht 		 */
1465e8fb11a1Sshidokht 		if ((cl->cl_blockcount > DK_MAX_BLOCKS) &&
1466e8fb11a1Sshidokht 		    (forced_under_1t != 1)) {
1467e8fb11a1Sshidokht 			/*
1468e8fb11a1Sshidokht 			 * Refer to cmlb_read_fdisk, when there is no
1469e8fb11a1Sshidokht 			 * fdisk partition table, cl_solaris_size is
1470e8fb11a1Sshidokht 			 * set to disk's capacity. In this case, we
1471e8fb11a1Sshidokht 			 * need to adjust it
1472e8fb11a1Sshidokht 			 */
1473e8fb11a1Sshidokht 			if (cl->cl_solaris_size > DK_MAX_BLOCKS)
1474e8fb11a1Sshidokht 				cl->cl_solaris_size = DK_MAX_BLOCKS;
1475e8fb11a1Sshidokht 			cmlb_resync_geom_caches(cl, DK_MAX_BLOCKS, tg_cookie);
1476e8fb11a1Sshidokht 		}
1477e8fb11a1Sshidokht #endif
14783ccda647Slclee 
14793ccda647Slclee 		buffer_size = sizeof (struct dk_label);
14803ccda647Slclee 
1481e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
14823ccda647Slclee 		    "label_addr: 0x%x allocation size: 0x%x\n",
14833ccda647Slclee 		    label_addr, buffer_size);
14843ccda647Slclee 
14853ccda647Slclee 		if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
14863ccda647Slclee 			return (ENOMEM);
14873ccda647Slclee 
1488e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
1489e8fb11a1Sshidokht 		rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1490e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
14913ccda647Slclee 
14923ccda647Slclee 		switch (rval) {
14933ccda647Slclee 		case 0:
14943ccda647Slclee 			/*
14953ccda647Slclee 			 * cmlb_uselabel will establish that the geometry
14963ccda647Slclee 			 * is valid.
14973ccda647Slclee 			 */
1498e8fb11a1Sshidokht 			if (cmlb_uselabel(cl,
1499e8fb11a1Sshidokht 			    (struct dk_label *)(uintptr_t)dkl, flags) !=
15003ccda647Slclee 			    CMLB_LABEL_IS_VALID) {
15013ccda647Slclee 				label_error = EINVAL;
15023ccda647Slclee 			} else
1503e8fb11a1Sshidokht 				cl->cl_vtoc_label_is_from_media = 1;
15043ccda647Slclee 			break;
15053ccda647Slclee 		case EACCES:
15063ccda647Slclee 			label_error = EACCES;
15073ccda647Slclee 			break;
15083ccda647Slclee 		default:
15093ccda647Slclee 			label_error = EINVAL;
15103ccda647Slclee 			break;
15113ccda647Slclee 		}
15123ccda647Slclee 
15133ccda647Slclee 		kmem_free(dkl, buffer_size);
15143ccda647Slclee 	}
15153ccda647Slclee 
15163ccda647Slclee 	/*
15173ccda647Slclee 	 * If a valid label was not found, AND if no reservation conflict
15183ccda647Slclee 	 * was detected, then go ahead and create a default label (4069506).
15193ccda647Slclee 	 *
15203ccda647Slclee 	 * Note: currently, for VTOC_8 devices, the default label is created
1521e8fb11a1Sshidokht 	 * for removables and hotpluggables only.  For VTOC_16 devices, the
1522e8fb11a1Sshidokht 	 * default label will be created for all devices.
15233ccda647Slclee 	 * (see cmlb_build_default_label)
15243ccda647Slclee 	 */
15253ccda647Slclee #if defined(_SUNOS_VTOC_8)
1526e8fb11a1Sshidokht 	if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1527e8fb11a1Sshidokht 	    (label_error != EACCES)) {
15283ccda647Slclee #elif defined(_SUNOS_VTOC_16)
15293ccda647Slclee 	if (label_error != EACCES) {
15303ccda647Slclee #endif
1531e8fb11a1Sshidokht 		if (cl->cl_f_geometry_is_valid == FALSE) {
1532e8fb11a1Sshidokht 			cmlb_build_default_label(cl, tg_cookie);
15333ccda647Slclee 		}
15343ccda647Slclee 		label_error = 0;
15353ccda647Slclee 	}
15363ccda647Slclee 
15373ccda647Slclee no_solaris_partition:
15383ccda647Slclee 
15393ccda647Slclee #if defined(_SUNOS_VTOC_16)
15403ccda647Slclee 	/*
15413ccda647Slclee 	 * If we have valid geometry, set up the remaining fdisk partitions.
15423ccda647Slclee 	 * Note that dkl_cylno is not used for the fdisk map entries, so
15433ccda647Slclee 	 * we set it to an entirely bogus value.
15443ccda647Slclee 	 */
15453ccda647Slclee 	for (count = 0; count < FD_NUMPART; count++) {
1546e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_cylno = -1;
1547e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
1548e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_nblk;
15493ccda647Slclee 
1550e8fb11a1Sshidokht 		cl->cl_offset[FDISK_P1 + count] =
1551e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_start;
15523ccda647Slclee 	}
15533ccda647Slclee #endif
15543ccda647Slclee 
15553ccda647Slclee 	for (count = 0; count < NDKMAP; count++) {
15563ccda647Slclee #if defined(_SUNOS_VTOC_8)
1557e8fb11a1Sshidokht 		struct dk_map *lp  = &cl->cl_map[count];
1558e8fb11a1Sshidokht 		cl->cl_offset[count] =
1559e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
15603ccda647Slclee #elif defined(_SUNOS_VTOC_16)
1561e8fb11a1Sshidokht 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
15623ccda647Slclee 
1563e8fb11a1Sshidokht 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
15643ccda647Slclee #else
15653ccda647Slclee #error "No VTOC format defined."
15663ccda647Slclee #endif
15673ccda647Slclee 	}
15683ccda647Slclee 
15693ccda647Slclee 	return (label_error);
15703ccda647Slclee }
15713ccda647Slclee 
15723ccda647Slclee #if defined(_SUNOS_VTOC_16)
15733ccda647Slclee /*
15743ccda647Slclee  * Macro: MAX_BLKS
15753ccda647Slclee  *
15763ccda647Slclee  *	This macro is used for table entries where we need to have the largest
15773ccda647Slclee  *	possible sector value for that head & SPT (sectors per track)
15783ccda647Slclee  *	combination.  Other entries for some smaller disk sizes are set by
15793ccda647Slclee  *	convention to match those used by X86 BIOS usage.
15803ccda647Slclee  */
15813ccda647Slclee #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
15823ccda647Slclee 
15833ccda647Slclee /*
15843ccda647Slclee  *    Function: cmlb_convert_geometry
15853ccda647Slclee  *
15863ccda647Slclee  * Description: Convert physical geometry into a dk_geom structure. In
15873ccda647Slclee  *		other words, make sure we don't wrap 16-bit values.
15883ccda647Slclee  *		e.g. converting from geom_cache to dk_geom
15893ccda647Slclee  *
15903ccda647Slclee  *     Context: Kernel thread only
15913ccda647Slclee  */
15923ccda647Slclee static void
1593e8fb11a1Sshidokht cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g)
15943ccda647Slclee {
15953ccda647Slclee 	int i;
15963ccda647Slclee 	static const struct chs_values {
15973ccda647Slclee 		uint_t max_cap;		/* Max Capacity for this HS. */
15983ccda647Slclee 		uint_t nhead;		/* Heads to use. */
15993ccda647Slclee 		uint_t nsect;		/* SPT to use. */
16003ccda647Slclee 	} CHS_values[] = {
16013ccda647Slclee 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
16023ccda647Slclee 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
16033ccda647Slclee 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
16043ccda647Slclee 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
16053ccda647Slclee 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
16063ccda647Slclee 	};
16073ccda647Slclee 
16083ccda647Slclee 	/* Unlabeled SCSI floppy device */
16093ccda647Slclee 	if (capacity <= 0x1000) {
1610e8fb11a1Sshidokht 		cl_g->dkg_nhead = 2;
1611e8fb11a1Sshidokht 		cl_g->dkg_ncyl = 80;
1612e8fb11a1Sshidokht 		cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
16133ccda647Slclee 		return;
16143ccda647Slclee 	}
16153ccda647Slclee 
16163ccda647Slclee 	/*
16173ccda647Slclee 	 * For all devices we calculate cylinders using the
16183ccda647Slclee 	 * heads and sectors we assign based on capacity of the
16193ccda647Slclee 	 * device.  The table is designed to be compatible with the
16203ccda647Slclee 	 * way other operating systems lay out fdisk tables for X86
16213ccda647Slclee 	 * and to insure that the cylinders never exceed 65535 to
16223ccda647Slclee 	 * prevent problems with X86 ioctls that report geometry.
16233ccda647Slclee 	 * We use SPT that are multiples of 63, since other OSes that
16243ccda647Slclee 	 * are not limited to 16-bits for cylinders stop at 63 SPT
16253ccda647Slclee 	 * we make do by using multiples of 63 SPT.
16263ccda647Slclee 	 *
16273ccda647Slclee 	 * Note than capacities greater than or equal to 1TB will simply
16283ccda647Slclee 	 * get the largest geometry from the table. This should be okay
16293ccda647Slclee 	 * since disks this large shouldn't be using CHS values anyway.
16303ccda647Slclee 	 */
16313ccda647Slclee 	for (i = 0; CHS_values[i].max_cap < capacity &&
16323ccda647Slclee 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
16333ccda647Slclee 		;
16343ccda647Slclee 
1635e8fb11a1Sshidokht 	cl_g->dkg_nhead = CHS_values[i].nhead;
1636e8fb11a1Sshidokht 	cl_g->dkg_nsect = CHS_values[i].nsect;
16373ccda647Slclee }
16383ccda647Slclee #endif
16393ccda647Slclee 
16403ccda647Slclee /*
16413ccda647Slclee  *    Function: cmlb_resync_geom_caches
16423ccda647Slclee  *
16433ccda647Slclee  * Description: (Re)initialize both geometry caches: the virtual geometry
16443ccda647Slclee  *            information is extracted from the HBA (the "geometry"
16453ccda647Slclee  *            capability), and the physical geometry cache data is
16463ccda647Slclee  *            generated by issuing MODE SENSE commands.
16473ccda647Slclee  *
1648e8fb11a1Sshidokht  *   Arguments:
1649e8fb11a1Sshidokht  *	cl 		driver soft state (unit) structure
1650e8fb11a1Sshidokht  *	capacity	disk capacity in #blocks
1651e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1652e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
16533ccda647Slclee  *
16543ccda647Slclee  *     Context: Kernel thread only (can sleep).
16553ccda647Slclee  */
16563ccda647Slclee static void
1657e8fb11a1Sshidokht cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1658e8fb11a1Sshidokht     void *tg_cookie)
16593ccda647Slclee {
16603ccda647Slclee 	struct	cmlb_geom 	pgeom;
16613ccda647Slclee 	struct	cmlb_geom	lgeom;
16623ccda647Slclee 	struct 	cmlb_geom	*pgeomp = &pgeom;
16633ccda647Slclee 	unsigned short 		nhead;
16643ccda647Slclee 	unsigned short 		nsect;
16653ccda647Slclee 	int 			spc;
16663ccda647Slclee 	int			ret;
16673ccda647Slclee 
1668e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1669e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
16703ccda647Slclee 
16713ccda647Slclee 	/*
16723ccda647Slclee 	 * Ask the controller for its logical geometry.
16733ccda647Slclee 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
16743ccda647Slclee 	 * then the lgeom cache will be invalid.
16753ccda647Slclee 	 */
1676e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
16773ccda647Slclee 	bzero(&lgeom, sizeof (struct cmlb_geom));
1678e8fb11a1Sshidokht 	ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1679e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
16803ccda647Slclee 
1681e8fb11a1Sshidokht 	bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
16823ccda647Slclee 
16833ccda647Slclee 	/*
16843ccda647Slclee 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
16853ccda647Slclee 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
16863ccda647Slclee 	 */
1687e8fb11a1Sshidokht 	if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1688e8fb11a1Sshidokht 	    cl->cl_lgeom.g_nhead == 0) {
16893ccda647Slclee 		/*
16903ccda647Slclee 		 * Note: Perhaps this needs to be more adaptive? The rationale
16913ccda647Slclee 		 * is that, if there's no HBA geometry from the HBA driver, any
16923ccda647Slclee 		 * guess is good, since this is the physical geometry. If MODE
16933ccda647Slclee 		 * SENSE fails this gives a max cylinder size for non-LBA access
16943ccda647Slclee 		 */
16953ccda647Slclee 		nhead = 255;
16963ccda647Slclee 		nsect = 63;
16973ccda647Slclee 	} else {
1698e8fb11a1Sshidokht 		nhead = cl->cl_lgeom.g_nhead;
1699e8fb11a1Sshidokht 		nsect = cl->cl_lgeom.g_nsect;
17003ccda647Slclee 	}
17013ccda647Slclee 
1702e8fb11a1Sshidokht 	if (ISCD(cl)) {
17033ccda647Slclee 		pgeomp->g_nhead = 1;
17043ccda647Slclee 		pgeomp->g_nsect = nsect * nhead;
17053ccda647Slclee 	} else {
17063ccda647Slclee 		pgeomp->g_nhead = nhead;
17073ccda647Slclee 		pgeomp->g_nsect = nsect;
17083ccda647Slclee 	}
17093ccda647Slclee 
17103ccda647Slclee 	spc = pgeomp->g_nhead * pgeomp->g_nsect;
17113ccda647Slclee 	pgeomp->g_capacity = capacity;
17123ccda647Slclee 	pgeomp->g_ncyl = pgeomp->g_capacity / spc;
17133ccda647Slclee 	pgeomp->g_acyl = 0;
17143ccda647Slclee 
17153ccda647Slclee 	/*
17163ccda647Slclee 	 * Retrieve fresh geometry data from the hardware, stash it
17173ccda647Slclee 	 * here temporarily before we rebuild the incore label.
17183ccda647Slclee 	 *
17193ccda647Slclee 	 * We want to use the MODE SENSE commands to derive the
17203ccda647Slclee 	 * physical geometry of the device, but if either command
17213ccda647Slclee 	 * fails, the logical geometry is used as the fallback for
17223ccda647Slclee 	 * disk label geometry.
17233ccda647Slclee 	 */
17243ccda647Slclee 
1725e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
1726e8fb11a1Sshidokht 	(void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
1727e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
17283ccda647Slclee 
17293ccda647Slclee 	/*
17303ccda647Slclee 	 * Now update the real copy while holding the mutex. This
17313ccda647Slclee 	 * way the global copy is never in an inconsistent state.
17323ccda647Slclee 	 */
1733e8fb11a1Sshidokht 	bcopy(pgeomp, &cl->cl_pgeom,  sizeof (cl->cl_pgeom));
17343ccda647Slclee 
1735e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
17363ccda647Slclee 	    "(cached from lgeom)\n");
1737e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
17383ccda647Slclee 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
1739e8fb11a1Sshidokht 	    cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
1740e8fb11a1Sshidokht 	    cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
1741e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "   lbasize: %d; capacity: %ld; "
1742e8fb11a1Sshidokht 	    "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
1743e8fb11a1Sshidokht 	    cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
1744e8fb11a1Sshidokht 	    cl->cl_pgeom.g_rpm);
17453ccda647Slclee }
17463ccda647Slclee 
17473ccda647Slclee 
17483ccda647Slclee /*
17493ccda647Slclee  *    Function: cmlb_read_fdisk
17503ccda647Slclee  *
17513ccda647Slclee  * Description: utility routine to read the fdisk table.
17523ccda647Slclee  *
1753e8fb11a1Sshidokht  *   Arguments:
1754e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
1755e8fb11a1Sshidokht  *	capacity	disk capacity in #blocks
1756e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
1757e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
17583ccda647Slclee  *
17593ccda647Slclee  * Return Code: 0 for success (includes not reading for no_fdisk_present case
17603ccda647Slclee  *		errnos from tg_rw if failed to read the first block.
17613ccda647Slclee  *
17623ccda647Slclee  *     Context: Kernel thread only (can sleep).
17633ccda647Slclee  */
17643ccda647Slclee /*ARGSUSED*/
17653ccda647Slclee static int
1766e8fb11a1Sshidokht cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
17673ccda647Slclee {
17683ccda647Slclee #if defined(_NO_FDISK_PRESENT)
17693ccda647Slclee 
1770e8fb11a1Sshidokht 	cl->cl_solaris_offset = 0;
1771e8fb11a1Sshidokht 	cl->cl_solaris_size = capacity;
1772e8fb11a1Sshidokht 	bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
17733ccda647Slclee 	return (0);
17743ccda647Slclee 
17753ccda647Slclee #elif defined(_FIRMWARE_NEEDS_FDISK)
17763ccda647Slclee 
17773ccda647Slclee 	struct ipart	*fdp;
17783ccda647Slclee 	struct mboot	*mbp;
17793ccda647Slclee 	struct ipart	fdisk[FD_NUMPART];
17803ccda647Slclee 	int		i;
17813ccda647Slclee 	char		sigbuf[2];
17823ccda647Slclee 	caddr_t		bufp;
17833ccda647Slclee 	int		uidx;
17843ccda647Slclee 	int 		rval;
17853ccda647Slclee 	int		lba = 0;
17863ccda647Slclee 	uint_t		solaris_offset;	/* offset to solaris part. */
17873ccda647Slclee 	daddr_t		solaris_size;	/* size of solaris partition */
17883ccda647Slclee 	uint32_t	blocksize;
17893ccda647Slclee 
1790e8fb11a1Sshidokht 	ASSERT(cl != NULL);
1791e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
17923ccda647Slclee 
17933ccda647Slclee 	/*
17943ccda647Slclee 	 * Start off assuming no fdisk table
17953ccda647Slclee 	 */
17963ccda647Slclee 	solaris_offset = 0;
17973ccda647Slclee 	solaris_size   = capacity;
17983ccda647Slclee 
1799e8fb11a1Sshidokht 	blocksize = cl->cl_tgt_blocksize;
18003ccda647Slclee 
18013ccda647Slclee 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
18023ccda647Slclee 
1803e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
1804e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
1805e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
18063ccda647Slclee 
18073ccda647Slclee 	if (rval != 0) {
1808e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
18093ccda647Slclee 		    "cmlb_read_fdisk: fdisk read err\n");
1810e8fb11a1Sshidokht 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
1811e8fb11a1Sshidokht 		goto done;
18123ccda647Slclee 	}
18133ccda647Slclee 
18143ccda647Slclee 	mbp = (struct mboot *)bufp;
18153ccda647Slclee 
18163ccda647Slclee 	/*
18173ccda647Slclee 	 * The fdisk table does not begin on a 4-byte boundary within the
18183ccda647Slclee 	 * master boot record, so we copy it to an aligned structure to avoid
18193ccda647Slclee 	 * alignment exceptions on some processors.
18203ccda647Slclee 	 */
18213ccda647Slclee 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
18223ccda647Slclee 
18233ccda647Slclee 	/*
18243ccda647Slclee 	 * Check for lba support before verifying sig; sig might not be
18253ccda647Slclee 	 * there, say on a blank disk, but the max_chs mark may still
18263ccda647Slclee 	 * be present.
18273ccda647Slclee 	 *
18283ccda647Slclee 	 * Note: LBA support and BEFs are an x86-only concept but this
18293ccda647Slclee 	 * code should work OK on SPARC as well.
18303ccda647Slclee 	 */
18313ccda647Slclee 
18323ccda647Slclee 	/*
18333ccda647Slclee 	 * First, check for lba-access-ok on root node (or prom root node)
18343ccda647Slclee 	 * if present there, don't need to search fdisk table.
18353ccda647Slclee 	 */
18363ccda647Slclee 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
18373ccda647Slclee 	    "lba-access-ok", 0) != 0) {
18383ccda647Slclee 		/* All drives do LBA; don't search fdisk table */
18393ccda647Slclee 		lba = 1;
18403ccda647Slclee 	} else {
18413ccda647Slclee 		/* Okay, look for mark in fdisk table */
18423ccda647Slclee 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
18433ccda647Slclee 			/* accumulate "lba" value from all partitions */
18443ccda647Slclee 			lba = (lba || cmlb_has_max_chs_vals(fdp));
18453ccda647Slclee 		}
18463ccda647Slclee 	}
18473ccda647Slclee 
18483ccda647Slclee 	if (lba != 0) {
1849e8fb11a1Sshidokht 		dev_t dev = cmlb_make_device(cl);
18503ccda647Slclee 
1851e8fb11a1Sshidokht 		if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
18523ccda647Slclee 		    "lba-access-ok", 0) == 0) {
18533ccda647Slclee 			/* not found; create it */
1854e8fb11a1Sshidokht 			if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
18553ccda647Slclee 			    "lba-access-ok", (caddr_t)NULL, 0) !=
18563ccda647Slclee 			    DDI_PROP_SUCCESS) {
1857e8fb11a1Sshidokht 				cmlb_dbg(CMLB_ERROR,  cl,
18583ccda647Slclee 				    "cmlb_read_fdisk: Can't create lba "
18593ccda647Slclee 				    "property for instance %d\n",
1860e8fb11a1Sshidokht 				    ddi_get_instance(CMLB_DEVINFO(cl)));
18613ccda647Slclee 			}
18623ccda647Slclee 		}
18633ccda647Slclee 	}
18643ccda647Slclee 
18653ccda647Slclee 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
18663ccda647Slclee 
18673ccda647Slclee 	/*
18683ccda647Slclee 	 * Endian-independent signature check
18693ccda647Slclee 	 */
18703ccda647Slclee 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
18713ccda647Slclee 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
1872e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
18733ccda647Slclee 		    "cmlb_read_fdisk: no fdisk\n");
1874e8fb11a1Sshidokht 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
18753ccda647Slclee 		goto done;
18763ccda647Slclee 	}
18773ccda647Slclee 
18783ccda647Slclee #ifdef CMLBDEBUG
1879e8fb11a1Sshidokht 	if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
18803ccda647Slclee 		fdp = fdisk;
1881e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "cmlb_read_fdisk:\n");
1882e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "         relsect    "
18833ccda647Slclee 		    "numsect         sysid       bootid\n");
18843ccda647Slclee 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
1885e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
18863ccda647Slclee 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
18873ccda647Slclee 			    i, fdp->relsect, fdp->numsect,
18883ccda647Slclee 			    fdp->systid, fdp->bootid);
18893ccda647Slclee 		}
18903ccda647Slclee 	}
18913ccda647Slclee #endif
18923ccda647Slclee 
18933ccda647Slclee 	/*
18943ccda647Slclee 	 * Try to find the unix partition
18953ccda647Slclee 	 */
18963ccda647Slclee 	uidx = -1;
18973ccda647Slclee 	solaris_offset = 0;
18983ccda647Slclee 	solaris_size   = 0;
18993ccda647Slclee 
19003ccda647Slclee 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
19013ccda647Slclee 		int	relsect;
19023ccda647Slclee 		int	numsect;
19033ccda647Slclee 
19043ccda647Slclee 		if (fdp->numsect == 0) {
1905e8fb11a1Sshidokht 			cl->cl_fmap[i].fmap_start = 0;
1906e8fb11a1Sshidokht 			cl->cl_fmap[i].fmap_nblk  = 0;
19073ccda647Slclee 			continue;
19083ccda647Slclee 		}
19093ccda647Slclee 
19103ccda647Slclee 		/*
19113ccda647Slclee 		 * Data in the fdisk table is little-endian.
19123ccda647Slclee 		 */
19133ccda647Slclee 		relsect = LE_32(fdp->relsect);
19143ccda647Slclee 		numsect = LE_32(fdp->numsect);
19153ccda647Slclee 
1916e8fb11a1Sshidokht 		cl->cl_fmap[i].fmap_start = relsect;
1917e8fb11a1Sshidokht 		cl->cl_fmap[i].fmap_nblk  = numsect;
19183ccda647Slclee 
19193ccda647Slclee 		if (fdp->systid != SUNIXOS &&
19203ccda647Slclee 		    fdp->systid != SUNIXOS2 &&
19213ccda647Slclee 		    fdp->systid != EFI_PMBR) {
19223ccda647Slclee 			continue;
19233ccda647Slclee 		}
19243ccda647Slclee 
19253ccda647Slclee 		/*
19263ccda647Slclee 		 * use the last active solaris partition id found
19273ccda647Slclee 		 * (there should only be 1 active partition id)
19283ccda647Slclee 		 *
19293ccda647Slclee 		 * if there are no active solaris partition id
19303ccda647Slclee 		 * then use the first inactive solaris partition id
19313ccda647Slclee 		 */
19323ccda647Slclee 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
19333ccda647Slclee 			uidx = i;
19343ccda647Slclee 			solaris_offset = relsect;
19353ccda647Slclee 			solaris_size   = numsect;
19363ccda647Slclee 		}
19373ccda647Slclee 	}
19383ccda647Slclee 
1939e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "fdisk 0x%x 0x%lx",
1940e8fb11a1Sshidokht 	    cl->cl_solaris_offset, cl->cl_solaris_size);
19413ccda647Slclee done:
19423ccda647Slclee 
19433ccda647Slclee 	/*
19443ccda647Slclee 	 * Clear the VTOC info, only if the Solaris partition entry
19453ccda647Slclee 	 * has moved, changed size, been deleted, or if the size of
19463ccda647Slclee 	 * the partition is too small to even fit the label sector.
19473ccda647Slclee 	 */
1948e8fb11a1Sshidokht 	if ((cl->cl_solaris_offset != solaris_offset) ||
1949e8fb11a1Sshidokht 	    (cl->cl_solaris_size != solaris_size) ||
19503ccda647Slclee 	    solaris_size <= DK_LABEL_LOC) {
1951e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl, "fdisk moved 0x%x 0x%lx",
19523ccda647Slclee 		    solaris_offset, solaris_size);
1953e8fb11a1Sshidokht 		bzero(&cl->cl_g, sizeof (struct dk_geom));
1954e8fb11a1Sshidokht 		bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
1955e8fb11a1Sshidokht 		bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
1956e8fb11a1Sshidokht 		cl->cl_f_geometry_is_valid = FALSE;
19573ccda647Slclee 	}
1958e8fb11a1Sshidokht 	cl->cl_solaris_offset = solaris_offset;
1959e8fb11a1Sshidokht 	cl->cl_solaris_size = solaris_size;
19603ccda647Slclee 	kmem_free(bufp, blocksize);
19613ccda647Slclee 	return (rval);
19623ccda647Slclee 
19633ccda647Slclee #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
19643ccda647Slclee #error "fdisk table presence undetermined for this platform."
19653ccda647Slclee #endif	/* #if defined(_NO_FDISK_PRESENT) */
19663ccda647Slclee }
19673ccda647Slclee 
19683ccda647Slclee static void
19693ccda647Slclee cmlb_swap_efi_gpt(efi_gpt_t *e)
19703ccda647Slclee {
19713ccda647Slclee 	_NOTE(ASSUMING_PROTECTED(*e))
19723ccda647Slclee 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
19733ccda647Slclee 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
19743ccda647Slclee 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
19753ccda647Slclee 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
19763ccda647Slclee 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
19773ccda647Slclee 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
19783ccda647Slclee 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
19793ccda647Slclee 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
19803ccda647Slclee 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
19813ccda647Slclee 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
19823ccda647Slclee 	e->efi_gpt_NumberOfPartitionEntries =
19833ccda647Slclee 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
19843ccda647Slclee 	e->efi_gpt_SizeOfPartitionEntry =
19853ccda647Slclee 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
19863ccda647Slclee 	e->efi_gpt_PartitionEntryArrayCRC32 =
19873ccda647Slclee 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
19883ccda647Slclee }
19893ccda647Slclee 
19903ccda647Slclee static void
19913ccda647Slclee cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
19923ccda647Slclee {
19933ccda647Slclee 	int i;
19943ccda647Slclee 
19953ccda647Slclee 	_NOTE(ASSUMING_PROTECTED(*p))
19963ccda647Slclee 	for (i = 0; i < nparts; i++) {
19973ccda647Slclee 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
19983ccda647Slclee 		    p[i].efi_gpe_PartitionTypeGUID);
19993ccda647Slclee 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
20003ccda647Slclee 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
20013ccda647Slclee 		/* PartitionAttrs */
20023ccda647Slclee 	}
20033ccda647Slclee }
20043ccda647Slclee 
20053ccda647Slclee static int
20063ccda647Slclee cmlb_validate_efi(efi_gpt_t *labp)
20073ccda647Slclee {
20083ccda647Slclee 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
20093ccda647Slclee 		return (EINVAL);
20103ccda647Slclee 	/* at least 96 bytes in this version of the spec. */
20113ccda647Slclee 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
20123ccda647Slclee 	    labp->efi_gpt_HeaderSize)
20133ccda647Slclee 		return (EINVAL);
20143ccda647Slclee 	/* this should be 128 bytes */
20153ccda647Slclee 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
20163ccda647Slclee 		return (EINVAL);
20173ccda647Slclee 	return (0);
20183ccda647Slclee }
20193ccda647Slclee 
2020ab92f716Sshidokht /*
2021ab92f716Sshidokht  * This function returns FALSE if there is a valid MBR signature and no
2022ab92f716Sshidokht  * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns TRUE.
2023ab92f716Sshidokht  *
2024ab92f716Sshidokht  * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2025ab92f716Sshidokht  * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2026ab92f716Sshidokht  * where a PMBR entry is not the only one. Also, if the first block has been
2027ab92f716Sshidokht  * corrupted, currently best attempt to allow data access would be to try to
2028ab92f716Sshidokht  * check for GPT headers. Hence in case of more than one partition entry, but
2029ab92f716Sshidokht  * at least one EFI_PMBR partition type or no valid magic number, the function
2030ab92f716Sshidokht  * returns TRUE to continue with looking for GPT header.
2031ab92f716Sshidokht  */
2032ab92f716Sshidokht 
2033ab92f716Sshidokht static int
2034ab92f716Sshidokht cmlb_check_efi_mbr(uchar_t *buf)
2035ab92f716Sshidokht {
2036ab92f716Sshidokht 	struct ipart	*fdp;
2037ab92f716Sshidokht 	struct mboot	*mbp = (struct mboot *)buf;
2038ab92f716Sshidokht 	struct ipart	fdisk[FD_NUMPART];
2039ab92f716Sshidokht 	int		i;
2040ab92f716Sshidokht 
2041ab92f716Sshidokht 	if (LE_16(mbp->signature) != MBB_MAGIC)
2042ab92f716Sshidokht 		return (TRUE);
2043ab92f716Sshidokht 
2044ab92f716Sshidokht 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2045ab92f716Sshidokht 
2046ab92f716Sshidokht 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2047ab92f716Sshidokht 		if (fdp->systid == EFI_PMBR)
2048ab92f716Sshidokht 			return (TRUE);
2049ab92f716Sshidokht 	}
2050ab92f716Sshidokht 
2051ab92f716Sshidokht 	return (FALSE);
2052ab92f716Sshidokht }
2053ab92f716Sshidokht 
20543ccda647Slclee static int
2055e8fb11a1Sshidokht cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2056e8fb11a1Sshidokht     void *tg_cookie)
20573ccda647Slclee {
20583ccda647Slclee 	int		i;
20593ccda647Slclee 	int		rval = 0;
20603ccda647Slclee 	efi_gpe_t	*partitions;
20613ccda647Slclee 	uchar_t		*buf;
20623ccda647Slclee 	uint_t		lbasize;	/* is really how much to read */
2063e8fb11a1Sshidokht 	diskaddr_t	cap = 0;
20643ccda647Slclee 	uint_t		nparts;
20653ccda647Slclee 	diskaddr_t	gpe_lba;
20661cec2d8cSshidokht 	int		iofailed = 0;
2067e8fb11a1Sshidokht 	struct uuid	uuid_type_reserved = EFI_RESERVED;
20683ccda647Slclee 
2069e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
20703ccda647Slclee 
2071e8fb11a1Sshidokht 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
2072e8fb11a1Sshidokht 		rval = EINVAL;
2073e8fb11a1Sshidokht 		goto done_err1;
2074e8fb11a1Sshidokht 	}
2075e8fb11a1Sshidokht 
2076e8fb11a1Sshidokht 
2077e8fb11a1Sshidokht 	lbasize = cl->cl_sys_blocksize;
2078e8fb11a1Sshidokht 
2079e8fb11a1Sshidokht 	cl->cl_reserved = -1;
2080e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
20813ccda647Slclee 
20823ccda647Slclee 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
20833ccda647Slclee 
2084e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf, 0, lbasize, tg_cookie);
20853ccda647Slclee 	if (rval) {
20861cec2d8cSshidokht 		iofailed = 1;
20873ccda647Slclee 		goto done_err;
20883ccda647Slclee 	}
20893ccda647Slclee 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
20903ccda647Slclee 		/* not ours */
20913ccda647Slclee 		rval = ESRCH;
20923ccda647Slclee 		goto done_err;
20933ccda647Slclee 	}
20943ccda647Slclee 
2095ab92f716Sshidokht 	if (cmlb_check_efi_mbr(buf) == FALSE) {
2096ab92f716Sshidokht 		rval = EINVAL;
2097ab92f716Sshidokht 		goto done_err;
2098ab92f716Sshidokht 	}
2099ab92f716Sshidokht 
2100e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
21013ccda647Slclee 	if (rval) {
21021cec2d8cSshidokht 		iofailed = 1;
21033ccda647Slclee 		goto done_err;
21043ccda647Slclee 	}
21053ccda647Slclee 	cmlb_swap_efi_gpt((efi_gpt_t *)buf);
21063ccda647Slclee 
21073ccda647Slclee 	if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
21083ccda647Slclee 		/*
21093ccda647Slclee 		 * Couldn't read the primary, try the backup.  Our
21103ccda647Slclee 		 * capacity at this point could be based on CHS, so
21113ccda647Slclee 		 * check what the device reports.
21123ccda647Slclee 		 */
2113e8fb11a1Sshidokht 		rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
21143ccda647Slclee 		if (rval) {
21151cec2d8cSshidokht 			iofailed = 1;
21163ccda647Slclee 			goto done_err;
21173ccda647Slclee 		}
2118e8fb11a1Sshidokht 
2119e8fb11a1Sshidokht 		/*
2120e8fb11a1Sshidokht 		 * CMLB_OFF_BY_ONE case, we check the next to last block first
2121e8fb11a1Sshidokht 		 * for backup GPT header, otherwise check the last block.
2122e8fb11a1Sshidokht 		 */
2123e8fb11a1Sshidokht 
2124e8fb11a1Sshidokht 		if ((rval = DK_TG_READ(cl, buf,
2125e8fb11a1Sshidokht 		    cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2126e8fb11a1Sshidokht 		    lbasize, tg_cookie))
2127e8fb11a1Sshidokht 		    != 0) {
21281cec2d8cSshidokht 			iofailed = 1;
21293ccda647Slclee 			goto done_err;
21303ccda647Slclee 		}
21313ccda647Slclee 		cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2132e8fb11a1Sshidokht 
2133e8fb11a1Sshidokht 		if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2134e8fb11a1Sshidokht 
2135e8fb11a1Sshidokht 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2136e8fb11a1Sshidokht 				goto done_err;
2137e8fb11a1Sshidokht 			if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2138e8fb11a1Sshidokht 			    tg_cookie)) != 0)
2139e8fb11a1Sshidokht 				goto done_err;
2140e8fb11a1Sshidokht 			cmlb_swap_efi_gpt((efi_gpt_t *)buf);
21413ccda647Slclee 			if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
21423ccda647Slclee 				goto done_err;
2143e8fb11a1Sshidokht 		}
2144e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2145e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
21463ccda647Slclee 			    "primary label corrupt; using backup\n");
21473ccda647Slclee 	}
21483ccda647Slclee 
21493ccda647Slclee 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
21503ccda647Slclee 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
21513ccda647Slclee 
2152e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
21533ccda647Slclee 	if (rval) {
21541cec2d8cSshidokht 		iofailed = 1;
21553ccda647Slclee 		goto done_err;
21563ccda647Slclee 	}
21573ccda647Slclee 	partitions = (efi_gpe_t *)buf;
21583ccda647Slclee 
21593ccda647Slclee 	if (nparts > MAXPART) {
21603ccda647Slclee 		nparts = MAXPART;
21613ccda647Slclee 	}
21623ccda647Slclee 	cmlb_swap_efi_gpe(nparts, partitions);
21633ccda647Slclee 
2164e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
21653ccda647Slclee 
21663ccda647Slclee 	/* Fill in partition table. */
21673ccda647Slclee 	for (i = 0; i < nparts; i++) {
21683ccda647Slclee 		if (partitions->efi_gpe_StartingLBA != 0 ||
21693ccda647Slclee 		    partitions->efi_gpe_EndingLBA != 0) {
2170e8fb11a1Sshidokht 			cl->cl_map[i].dkl_cylno =
21713ccda647Slclee 			    partitions->efi_gpe_StartingLBA;
2172e8fb11a1Sshidokht 			cl->cl_map[i].dkl_nblk =
21733ccda647Slclee 			    partitions->efi_gpe_EndingLBA -
21743ccda647Slclee 			    partitions->efi_gpe_StartingLBA + 1;
2175e8fb11a1Sshidokht 			cl->cl_offset[i] =
21763ccda647Slclee 			    partitions->efi_gpe_StartingLBA;
21773ccda647Slclee 		}
2178e8fb11a1Sshidokht 
2179e8fb11a1Sshidokht 		if (cl->cl_reserved == -1) {
2180e8fb11a1Sshidokht 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2181e8fb11a1Sshidokht 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2182e8fb11a1Sshidokht 				cl->cl_reserved = i;
2183e8fb11a1Sshidokht 			}
2184e8fb11a1Sshidokht 		}
21853ccda647Slclee 		if (i == WD_NODE) {
21863ccda647Slclee 			/*
21873ccda647Slclee 			 * minor number 7 corresponds to the whole disk
21883ccda647Slclee 			 */
2189e8fb11a1Sshidokht 			cl->cl_map[i].dkl_cylno = 0;
2190e8fb11a1Sshidokht 			cl->cl_map[i].dkl_nblk = capacity;
2191e8fb11a1Sshidokht 			cl->cl_offset[i] = 0;
21923ccda647Slclee 		}
21933ccda647Slclee 		partitions++;
21943ccda647Slclee 	}
2195e8fb11a1Sshidokht 	cl->cl_solaris_offset = 0;
2196e8fb11a1Sshidokht 	cl->cl_solaris_size = capacity;
2197e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = TRUE;
2198e8fb11a1Sshidokht 
2199e8fb11a1Sshidokht 	/* clear the vtoc label */
2200e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2201e8fb11a1Sshidokht 
22023ccda647Slclee 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
22033ccda647Slclee 	return (0);
22043ccda647Slclee 
22053ccda647Slclee done_err:
22063ccda647Slclee 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2207e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
2208e8fb11a1Sshidokht done_err1:
22093ccda647Slclee 	/*
22103ccda647Slclee 	 * if we didn't find something that could look like a VTOC
22113ccda647Slclee 	 * and the disk is over 1TB, we know there isn't a valid label.
22123ccda647Slclee 	 * Otherwise let cmlb_uselabel decide what to do.  We only
22133ccda647Slclee 	 * want to invalidate this if we're certain the label isn't
22143ccda647Slclee 	 * valid because cmlb_prop_op will now fail, which in turn
22153ccda647Slclee 	 * causes things like opens and stats on the partition to fail.
22163ccda647Slclee 	 */
22171cec2d8cSshidokht 	if ((capacity > DK_MAX_BLOCKS) && (rval != ESRCH) && !iofailed) {
2218e8fb11a1Sshidokht 		cl->cl_f_geometry_is_valid = FALSE;
22193ccda647Slclee 	}
22203ccda647Slclee 	return (rval);
22213ccda647Slclee }
22223ccda647Slclee 
22233ccda647Slclee 
22243ccda647Slclee /*
22253ccda647Slclee  *    Function: cmlb_uselabel
22263ccda647Slclee  *
22273ccda647Slclee  * Description: Validate the disk label and update the relevant data (geometry,
22283ccda647Slclee  *		partition, vtoc, and capacity data) in the cmlb_lun struct.
22293ccda647Slclee  *		Marks the geometry of the unit as being valid.
22303ccda647Slclee  *
2231e8fb11a1Sshidokht  *   Arguments: cl: unit struct.
22323ccda647Slclee  *		dk_label: disk label
22333ccda647Slclee  *
22343ccda647Slclee  * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
22353ccda647Slclee  *		partition, vtoc, and capacity data are good.
22363ccda647Slclee  *
22373ccda647Slclee  *		CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
22383ccda647Slclee  *		label; or computed capacity does not jibe with capacity
22393ccda647Slclee  *		reported from the READ CAPACITY command.
22403ccda647Slclee  *
22413ccda647Slclee  *     Context: Kernel thread only (can sleep).
22423ccda647Slclee  */
22433ccda647Slclee static int
2244e8fb11a1Sshidokht cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
22453ccda647Slclee {
22463ccda647Slclee 	short		*sp;
22473ccda647Slclee 	short		sum;
22483ccda647Slclee 	short		count;
22493ccda647Slclee 	int		label_error = CMLB_LABEL_IS_VALID;
22503ccda647Slclee 	int		i;
22513ccda647Slclee 	diskaddr_t	label_capacity;
22523ccda647Slclee 	int		part_end;
22533ccda647Slclee 	diskaddr_t	track_capacity;
22543ccda647Slclee #if defined(_SUNOS_VTOC_16)
22553ccda647Slclee 	struct	dkl_partition	*vpartp;
22563ccda647Slclee #endif
2257e8fb11a1Sshidokht 	ASSERT(cl != NULL);
2258e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
22593ccda647Slclee 
22603ccda647Slclee 	/* Validate the magic number of the label. */
22613ccda647Slclee 	if (labp->dkl_magic != DKL_MAGIC) {
22623ccda647Slclee #if defined(__sparc)
2263e8fb11a1Sshidokht 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2264e8fb11a1Sshidokht 			if (!(flags & CMLB_SILENT))
2265e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2266e8fb11a1Sshidokht 				    CE_WARN,
22673ccda647Slclee 				    "Corrupt label; wrong magic number\n");
22683ccda647Slclee 		}
22693ccda647Slclee #endif
22703ccda647Slclee 		return (CMLB_LABEL_IS_INVALID);
22713ccda647Slclee 	}
22723ccda647Slclee 
22733ccda647Slclee 	/* Validate the checksum of the label. */
22743ccda647Slclee 	sp  = (short *)labp;
22753ccda647Slclee 	sum = 0;
22763ccda647Slclee 	count = sizeof (struct dk_label) / sizeof (short);
22773ccda647Slclee 	while (count--)	 {
22783ccda647Slclee 		sum ^= *sp++;
22793ccda647Slclee 	}
22803ccda647Slclee 
22813ccda647Slclee 	if (sum != 0) {
22823ccda647Slclee #if defined(_SUNOS_VTOC_16)
2283e8fb11a1Sshidokht 		if (!ISCD(cl)) {
22843ccda647Slclee #elif defined(_SUNOS_VTOC_8)
2285e8fb11a1Sshidokht 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
22863ccda647Slclee #endif
2287e8fb11a1Sshidokht 			if (!(flags & CMLB_SILENT))
2288e8fb11a1Sshidokht 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2289e8fb11a1Sshidokht 				    CE_WARN,
22903ccda647Slclee 				    "Corrupt label - label checksum failed\n");
22913ccda647Slclee 		}
22923ccda647Slclee 		return (CMLB_LABEL_IS_INVALID);
22933ccda647Slclee 	}
22943ccda647Slclee 
22953ccda647Slclee 
22963ccda647Slclee 	/*
22973ccda647Slclee 	 * Fill in geometry structure with data from label.
22983ccda647Slclee 	 */
2299e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2300e8fb11a1Sshidokht 	cl->cl_g.dkg_ncyl   = labp->dkl_ncyl;
2301e8fb11a1Sshidokht 	cl->cl_g.dkg_acyl   = labp->dkl_acyl;
2302e8fb11a1Sshidokht 	cl->cl_g.dkg_bcyl   = 0;
2303e8fb11a1Sshidokht 	cl->cl_g.dkg_nhead  = labp->dkl_nhead;
2304e8fb11a1Sshidokht 	cl->cl_g.dkg_nsect  = labp->dkl_nsect;
2305e8fb11a1Sshidokht 	cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
23063ccda647Slclee 
23073ccda647Slclee #if defined(_SUNOS_VTOC_8)
2308e8fb11a1Sshidokht 	cl->cl_g.dkg_gap1   = labp->dkl_gap1;
2309e8fb11a1Sshidokht 	cl->cl_g.dkg_gap2   = labp->dkl_gap2;
2310e8fb11a1Sshidokht 	cl->cl_g.dkg_bhead  = labp->dkl_bhead;
23113ccda647Slclee #endif
23123ccda647Slclee #if defined(_SUNOS_VTOC_16)
2313e8fb11a1Sshidokht 	cl->cl_dkg_skew = labp->dkl_skew;
23143ccda647Slclee #endif
23153ccda647Slclee 
23163ccda647Slclee #if defined(__i386) || defined(__amd64)
2317e8fb11a1Sshidokht 	cl->cl_g.dkg_apc = labp->dkl_apc;
23183ccda647Slclee #endif
23193ccda647Slclee 
23203ccda647Slclee 	/*
23213ccda647Slclee 	 * Currently we rely on the values in the label being accurate. If
23223ccda647Slclee 	 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
23233ccda647Slclee 	 *
23243ccda647Slclee 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
23253ccda647Slclee 	 * although this command is optional in SCSI-2.
23263ccda647Slclee 	 */
2327e8fb11a1Sshidokht 	cl->cl_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
2328e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
2329e8fb11a1Sshidokht 	    (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
23303ccda647Slclee 
23313ccda647Slclee 	/*
23323ccda647Slclee 	 * The Read and Write reinstruct values may not be valid
23333ccda647Slclee 	 * for older disks.
23343ccda647Slclee 	 */
2335e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
2336e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
23373ccda647Slclee 
23383ccda647Slclee 	/* Fill in partition table. */
23393ccda647Slclee #if defined(_SUNOS_VTOC_8)
23403ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
2341e8fb11a1Sshidokht 		cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
2342e8fb11a1Sshidokht 		cl->cl_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
23433ccda647Slclee 	}
23443ccda647Slclee #endif
23453ccda647Slclee #if  defined(_SUNOS_VTOC_16)
23463ccda647Slclee 	vpartp		= labp->dkl_vtoc.v_part;
23473ccda647Slclee 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
23483ccda647Slclee 
2349e8fb11a1Sshidokht 	/* Prevent divide by zero */
2350e8fb11a1Sshidokht 	if (track_capacity == 0) {
2351e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2352e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2353e8fb11a1Sshidokht 			    "Corrupt label - zero nhead or nsect value\n");
2354e8fb11a1Sshidokht 
2355e8fb11a1Sshidokht 		return (CMLB_LABEL_IS_INVALID);
2356e8fb11a1Sshidokht 	}
2357e8fb11a1Sshidokht 
23583ccda647Slclee 	for (i = 0; i < NDKMAP; i++, vpartp++) {
2359e8fb11a1Sshidokht 		cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
2360e8fb11a1Sshidokht 		cl->cl_map[i].dkl_nblk  = vpartp->p_size;
23613ccda647Slclee 	}
23623ccda647Slclee #endif
23633ccda647Slclee 
23643ccda647Slclee 	/* Fill in VTOC Structure. */
2365e8fb11a1Sshidokht 	bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
23663ccda647Slclee #if defined(_SUNOS_VTOC_8)
23673ccda647Slclee 	/*
23683ccda647Slclee 	 * The 8-slice vtoc does not include the ascii label; save it into
23693ccda647Slclee 	 * the device's soft state structure here.
23703ccda647Slclee 	 */
2371e8fb11a1Sshidokht 	bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
23723ccda647Slclee #endif
23733ccda647Slclee 
23743ccda647Slclee 	/* Now look for a valid capacity. */
2375e8fb11a1Sshidokht 	track_capacity	= (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
2376e8fb11a1Sshidokht 	label_capacity	= (cl->cl_g.dkg_ncyl  * track_capacity);
23773ccda647Slclee 
2378e8fb11a1Sshidokht 	if (cl->cl_g.dkg_acyl) {
23793ccda647Slclee #if defined(__i386) || defined(__amd64)
23803ccda647Slclee 		/* we may have > 1 alts cylinder */
2381e8fb11a1Sshidokht 		label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
23823ccda647Slclee #else
23833ccda647Slclee 		label_capacity += track_capacity;
23843ccda647Slclee #endif
23853ccda647Slclee 	}
23863ccda647Slclee 
23873ccda647Slclee 	/*
2388e8fb11a1Sshidokht 	 * Force check here to ensure the computed capacity is valid.
2389e8fb11a1Sshidokht 	 * If capacity is zero, it indicates an invalid label and
2390e8fb11a1Sshidokht 	 * we should abort updating the relevant data then.
2391e8fb11a1Sshidokht 	 */
2392e8fb11a1Sshidokht 	if (label_capacity == 0) {
2393e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT))
2394e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2395e8fb11a1Sshidokht 			    "Corrupt label - no valid capacity could be "
2396e8fb11a1Sshidokht 			    "retrieved\n");
2397e8fb11a1Sshidokht 
2398e8fb11a1Sshidokht 		return (CMLB_LABEL_IS_INVALID);
2399e8fb11a1Sshidokht 	}
2400e8fb11a1Sshidokht 
2401e8fb11a1Sshidokht 	/* Mark the geometry as valid. */
2402e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = TRUE;
2403e8fb11a1Sshidokht 
2404e8fb11a1Sshidokht 	/*
24053ccda647Slclee 	 * if we got invalidated when mutex exit and entered again,
24063ccda647Slclee 	 * if blockcount different than when we came in, need to
24073ccda647Slclee 	 * retry from beginning of cmlb_validate_geometry.
24083ccda647Slclee 	 * revisit this on next phase of utilizing this for
24093ccda647Slclee 	 * sd.
24103ccda647Slclee 	 */
24113ccda647Slclee 
2412e8fb11a1Sshidokht 	if (label_capacity <= cl->cl_blockcount) {
24133ccda647Slclee #if defined(_SUNOS_VTOC_8)
24143ccda647Slclee 		/*
24153ccda647Slclee 		 * We can't let this happen on drives that are subdivided
24163ccda647Slclee 		 * into logical disks (i.e., that have an fdisk table).
2417e8fb11a1Sshidokht 		 * The cl_blockcount field should always hold the full media
24183ccda647Slclee 		 * size in sectors, period.  This code would overwrite
2419e8fb11a1Sshidokht 		 * cl_blockcount with the size of the Solaris fdisk partition.
24203ccda647Slclee 		 */
2421e8fb11a1Sshidokht 		cmlb_dbg(CMLB_ERROR,  cl,
24223ccda647Slclee 		    "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
2423e8fb11a1Sshidokht 		    label_capacity, cl->cl_blockcount);
2424e8fb11a1Sshidokht 		cl->cl_solaris_size = label_capacity;
24253ccda647Slclee 
24263ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
24273ccda647Slclee 		goto done;
24283ccda647Slclee 	}
24293ccda647Slclee 
2430e8fb11a1Sshidokht 	if (ISCD(cl)) {
24313ccda647Slclee 		/* For CDROMs, we trust that the data in the label is OK. */
24323ccda647Slclee #if defined(_SUNOS_VTOC_8)
24333ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
24343ccda647Slclee 			part_end = labp->dkl_nhead * labp->dkl_nsect *
24353ccda647Slclee 			    labp->dkl_map[i].dkl_cylno +
24363ccda647Slclee 			    labp->dkl_map[i].dkl_nblk  - 1;
24373ccda647Slclee 
24383ccda647Slclee 			if ((labp->dkl_map[i].dkl_nblk) &&
2439e8fb11a1Sshidokht 			    (part_end > cl->cl_blockcount)) {
2440e8fb11a1Sshidokht 				cl->cl_f_geometry_is_valid = FALSE;
24413ccda647Slclee 				break;
24423ccda647Slclee 			}
24433ccda647Slclee 		}
24443ccda647Slclee #endif
24453ccda647Slclee #if defined(_SUNOS_VTOC_16)
24463ccda647Slclee 		vpartp = &(labp->dkl_vtoc.v_part[0]);
24473ccda647Slclee 		for (i = 0; i < NDKMAP; i++, vpartp++) {
24483ccda647Slclee 			part_end = vpartp->p_start + vpartp->p_size;
24493ccda647Slclee 			if ((vpartp->p_size > 0) &&
2450e8fb11a1Sshidokht 			    (part_end > cl->cl_blockcount)) {
2451e8fb11a1Sshidokht 				cl->cl_f_geometry_is_valid = FALSE;
24523ccda647Slclee 				break;
24533ccda647Slclee 			}
24543ccda647Slclee 		}
24553ccda647Slclee #endif
24563ccda647Slclee 	} else {
2457e8fb11a1Sshidokht 		/* label_capacity > cl->cl_blockcount */
2458e8fb11a1Sshidokht 		if (!(flags & CMLB_SILENT)) {
2459e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
24603ccda647Slclee 			    "Corrupt label - bad geometry\n");
2461e8fb11a1Sshidokht 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
24623ccda647Slclee 			    "Label says %llu blocks; Drive says %llu blocks\n",
2463e8fb11a1Sshidokht 			    label_capacity, cl->cl_blockcount);
2464e8fb11a1Sshidokht 		}
2465e8fb11a1Sshidokht 		cl->cl_f_geometry_is_valid = FALSE;
24663ccda647Slclee 		label_error = CMLB_LABEL_IS_INVALID;
24673ccda647Slclee 	}
24683ccda647Slclee 
24693ccda647Slclee done:
24703ccda647Slclee 
2471e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "cmlb_uselabel: (label geometry)\n");
2472e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
24733ccda647Slclee 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
2474e8fb11a1Sshidokht 	    cl->cl_g.dkg_ncyl,  cl->cl_g.dkg_acyl,
2475e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
24763ccda647Slclee 
2477e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
24783ccda647Slclee 	    "   label_capacity: %d; intrlv: %d; rpm: %d\n",
2479e8fb11a1Sshidokht 	    cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
2480e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl, "   wrt_reinstr: %d; rd_reinstr: %d\n",
2481e8fb11a1Sshidokht 	    cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
24823ccda647Slclee 
2483e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
24843ccda647Slclee 
24853ccda647Slclee 	return (label_error);
24863ccda647Slclee }
24873ccda647Slclee 
24883ccda647Slclee 
24893ccda647Slclee /*
24903ccda647Slclee  *    Function: cmlb_build_default_label
24913ccda647Slclee  *
24923ccda647Slclee  * Description: Generate a default label for those devices that do not have
24933ccda647Slclee  *		one, e.g., new media, removable cartridges, etc..
24943ccda647Slclee  *
24953ccda647Slclee  *     Context: Kernel thread only
24963ccda647Slclee  */
2497e8fb11a1Sshidokht /*ARGSUSED*/
24983ccda647Slclee static void
2499e8fb11a1Sshidokht cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
25003ccda647Slclee {
25013ccda647Slclee #if defined(_SUNOS_VTOC_16)
25023ccda647Slclee 	uint_t	phys_spc;
25033ccda647Slclee 	uint_t	disksize;
2504e8fb11a1Sshidokht 	struct  dk_geom cl_g;
2505e8fb11a1Sshidokht 	diskaddr_t capacity;
25063ccda647Slclee #endif
25073ccda647Slclee 
2508e8fb11a1Sshidokht 	ASSERT(cl != NULL);
2509e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
25103ccda647Slclee 
25113ccda647Slclee #if defined(_SUNOS_VTOC_8)
25123ccda647Slclee 	/*
25133ccda647Slclee 	 * Note: This is a legacy check for non-removable devices on VTOC_8
25143ccda647Slclee 	 * only. This may be a valid check for VTOC_16 as well.
2515e8fb11a1Sshidokht 	 * Once we understand why there is this difference between SPARC and
2516e8fb11a1Sshidokht 	 * x86 platform, we could remove this legacy check.
25173ccda647Slclee 	 */
2518e8fb11a1Sshidokht 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
25193ccda647Slclee 		return;
25203ccda647Slclee 	}
25213ccda647Slclee #endif
25223ccda647Slclee 
2523e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2524e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2525e8fb11a1Sshidokht 	bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
25263ccda647Slclee 
25273ccda647Slclee #if defined(_SUNOS_VTOC_8)
25283ccda647Slclee 
25293ccda647Slclee 	/*
25303ccda647Slclee 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
25313ccda647Slclee 	 * But it is still necessary to set up various geometry information,
25323ccda647Slclee 	 * and we are doing this here.
25333ccda647Slclee 	 */
25343ccda647Slclee 
25353ccda647Slclee 	/*
25363ccda647Slclee 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
25373ccda647Slclee 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
25383ccda647Slclee 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
25393ccda647Slclee 	 * equal to C*H*S values.  This will cause some truncation of size due
25403ccda647Slclee 	 * to round off errors. For CD-ROMs, this truncation can have adverse
25413ccda647Slclee 	 * side effects, so returning ncyl and nhead as 1. The nsect will
25423ccda647Slclee 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
25433ccda647Slclee 	 */
2544e8fb11a1Sshidokht 	cl->cl_solaris_size = cl->cl_blockcount;
2545e8fb11a1Sshidokht 	if (ISCD(cl)) {
25463ccda647Slclee 		tg_attribute_t tgattribute;
25473ccda647Slclee 		int is_writable;
25483ccda647Slclee 		/*
25493ccda647Slclee 		 * Preserve the old behavior for non-writable
25503ccda647Slclee 		 * medias. Since dkg_nsect is a ushort, it
25513ccda647Slclee 		 * will lose bits as cdroms have more than
25523ccda647Slclee 		 * 65536 sectors. So if we recalculate
25533ccda647Slclee 		 * capacity, it will become much shorter.
25543ccda647Slclee 		 * But the dkg_* information is not
25553ccda647Slclee 		 * used for CDROMs so it is OK. But for
25563ccda647Slclee 		 * Writable CDs we need this information
25573ccda647Slclee 		 * to be valid (for newfs say). So we
25583ccda647Slclee 		 * make nsect and nhead > 1 that way
25593ccda647Slclee 		 * nsect can still stay within ushort limit
25603ccda647Slclee 		 * without losing any bits.
25613ccda647Slclee 		 */
25623ccda647Slclee 
25633ccda647Slclee 		bzero(&tgattribute, sizeof (tg_attribute_t));
25643ccda647Slclee 
2565e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
2566e8fb11a1Sshidokht 		is_writable =
2567e8fb11a1Sshidokht 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
25683ccda647Slclee 		    tgattribute.media_is_writable : 1;
2569e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
25703ccda647Slclee 
25713ccda647Slclee 		if (is_writable) {
2572e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
2573e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
2574e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
2575e8fb11a1Sshidokht 			cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
2576e8fb11a1Sshidokht 			    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
25773ccda647Slclee 		} else {
2578e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl  = 1;
2579e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 1;
2580e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
25813ccda647Slclee 		}
25823ccda647Slclee 	} else {
2583e8fb11a1Sshidokht 		if (cl->cl_blockcount <= 0x1000) {
25843ccda647Slclee 			/* unlabeled SCSI floppy device */
2585e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 2;
2586e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 80;
2587e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
2588e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x200000) {
2589e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
2590e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
2591e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (64 * 32);
25923ccda647Slclee 		} else {
2593e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 255;
2594*98a65dd4Sshidokht 
2595*98a65dd4Sshidokht 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
2596*98a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63) - 1) /
2597*98a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63)) * 63;
2598*98a65dd4Sshidokht 
2599*98a65dd4Sshidokht 			if (cl->cl_g.dkg_nsect == 0)
2600*98a65dd4Sshidokht 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
2601*98a65dd4Sshidokht 
2602*98a65dd4Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
2603*98a65dd4Sshidokht 			    (255 * cl->cl_g.dkg_nsect);
26043ccda647Slclee 		}
2605*98a65dd4Sshidokht 
2606e8fb11a1Sshidokht 		cl->cl_solaris_size =
2607e8fb11a1Sshidokht 		    cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
26083ccda647Slclee 
26093ccda647Slclee 	}
26103ccda647Slclee 
2611e8fb11a1Sshidokht 	cl->cl_g.dkg_acyl	= 0;
2612e8fb11a1Sshidokht 	cl->cl_g.dkg_bcyl	= 0;
2613e8fb11a1Sshidokht 	cl->cl_g.dkg_rpm	= 200;
2614e8fb11a1Sshidokht 	cl->cl_asciilabel[0]	= '\0';
2615e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl	= cl->cl_g.dkg_ncyl;
26163ccda647Slclee 
2617e8fb11a1Sshidokht 	cl->cl_map[0].dkl_cylno = 0;
2618e8fb11a1Sshidokht 	cl->cl_map[0].dkl_nblk  = cl->cl_solaris_size;
26193ccda647Slclee 
2620e8fb11a1Sshidokht 	cl->cl_map[2].dkl_cylno = 0;
2621e8fb11a1Sshidokht 	cl->cl_map[2].dkl_nblk  = cl->cl_solaris_size;
26223ccda647Slclee 
26233ccda647Slclee #elif defined(_SUNOS_VTOC_16)
26243ccda647Slclee 
2625e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
26263ccda647Slclee 		/*
26273ccda647Slclee 		 * Got fdisk table but no solaris entry therefore
26283ccda647Slclee 		 * don't create a default label
26293ccda647Slclee 		 */
2630e8fb11a1Sshidokht 		cl->cl_f_geometry_is_valid = TRUE;
26313ccda647Slclee 		return;
26323ccda647Slclee 	}
26333ccda647Slclee 
26343ccda647Slclee 	/*
26353ccda647Slclee 	 * For CDs we continue to use the physical geometry to calculate
26363ccda647Slclee 	 * number of cylinders. All other devices must convert the
26373ccda647Slclee 	 * physical geometry (cmlb_geom) to values that will fit
26383ccda647Slclee 	 * in a dk_geom structure.
26393ccda647Slclee 	 */
2640e8fb11a1Sshidokht 	if (ISCD(cl)) {
2641e8fb11a1Sshidokht 		phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
26423ccda647Slclee 	} else {
26433ccda647Slclee 		/* Convert physical geometry to disk geometry */
2644e8fb11a1Sshidokht 		bzero(&cl_g, sizeof (struct dk_geom));
2645e8fb11a1Sshidokht 
2646e8fb11a1Sshidokht 		/*
2647e8fb11a1Sshidokht 		 * Refer to comments related to off-by-1 at the
2648e8fb11a1Sshidokht 		 * header of this file.
2649e8fb11a1Sshidokht 		 * Before caculating geometry, capacity should be
2650e8fb11a1Sshidokht 		 * decreased by 1.
2651e8fb11a1Sshidokht 		 */
2652e8fb11a1Sshidokht 
2653e8fb11a1Sshidokht 		if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
2654e8fb11a1Sshidokht 			capacity = cl->cl_blockcount - 1;
2655e8fb11a1Sshidokht 		else
2656e8fb11a1Sshidokht 			capacity = cl->cl_blockcount;
2657e8fb11a1Sshidokht 
2658e8fb11a1Sshidokht 
2659e8fb11a1Sshidokht 		cmlb_convert_geometry(capacity, &cl_g);
2660e8fb11a1Sshidokht 		bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
2661e8fb11a1Sshidokht 		phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
26623ccda647Slclee 	}
26633ccda647Slclee 
2664e8fb11a1Sshidokht 	ASSERT(phys_spc != 0);
2665e8fb11a1Sshidokht 	cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
2666843e1988Sjohnlev 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2667843e1988Sjohnlev 		/* disable devid */
2668843e1988Sjohnlev 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
2669843e1988Sjohnlev 		disksize = cl->cl_solaris_size;
2670843e1988Sjohnlev 	} else {
2671e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = DK_ACYL;
2672e8fb11a1Sshidokht 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
2673e8fb11a1Sshidokht 		disksize = cl->cl_g.dkg_ncyl * phys_spc;
2674843e1988Sjohnlev 	}
26753ccda647Slclee 
2676e8fb11a1Sshidokht 	if (ISCD(cl)) {
26773ccda647Slclee 		/*
26783ccda647Slclee 		 * CD's don't use the "heads * sectors * cyls"-type of
26793ccda647Slclee 		 * geometry, but instead use the entire capacity of the media.
26803ccda647Slclee 		 */
2681e8fb11a1Sshidokht 		disksize = cl->cl_solaris_size;
2682e8fb11a1Sshidokht 		cl->cl_g.dkg_nhead = 1;
2683e8fb11a1Sshidokht 		cl->cl_g.dkg_nsect = 1;
2684e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm =
2685e8fb11a1Sshidokht 		    (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
26863ccda647Slclee 
2687e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_start = 0;
2688e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2689e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_tag   = V_BACKUP;
2690e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[0].p_flag  = V_UNMNT;
26913ccda647Slclee 
2692e8fb11a1Sshidokht 		cl->cl_map[0].dkl_cylno = 0;
2693e8fb11a1Sshidokht 		cl->cl_map[0].dkl_nblk  = disksize;
2694e8fb11a1Sshidokht 		cl->cl_offset[0] = 0;
26953ccda647Slclee 
26963ccda647Slclee 	} else {
26973ccda647Slclee 		/*
26983ccda647Slclee 		 * Hard disks and removable media cartridges
26993ccda647Slclee 		 */
2700e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm =
2701e8fb11a1Sshidokht 		    (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
2702e8fb11a1Sshidokht 		cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
27033ccda647Slclee 
27043ccda647Slclee 		/* Add boot slice */
2705e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_start = 0;
2706e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_size  = phys_spc;
2707e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_tag   = V_BOOT;
2708e8fb11a1Sshidokht 		cl->cl_vtoc.v_part[8].p_flag  = V_UNMNT;
27093ccda647Slclee 
2710e8fb11a1Sshidokht 		cl->cl_map[8].dkl_cylno = 0;
2711e8fb11a1Sshidokht 		cl->cl_map[8].dkl_nblk  = phys_spc;
2712e8fb11a1Sshidokht 		cl->cl_offset[8] = 0;
27133ccda647Slclee 
2714e8fb11a1Sshidokht 		if ((cl->cl_alter_behavior &
27153ccda647Slclee 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
2716e8fb11a1Sshidokht 		    cl->cl_device_type == DTYPE_DIRECT) {
2717e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_start = phys_spc;
2718e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_size  = 2 * phys_spc;
2719e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_tag   = V_ALTSCTR;
2720e8fb11a1Sshidokht 			cl->cl_vtoc.v_part[9].p_flag  = 0;
27213ccda647Slclee 
2722e8fb11a1Sshidokht 			cl->cl_map[9].dkl_cylno = 1;
2723e8fb11a1Sshidokht 			cl->cl_map[9].dkl_nblk  = 2 * phys_spc;
2724e8fb11a1Sshidokht 			cl->cl_offset[9] = phys_spc;
27253ccda647Slclee 		}
27263ccda647Slclee 	}
27273ccda647Slclee 
2728e8fb11a1Sshidokht 	cl->cl_g.dkg_apc = 0;
2729e8fb11a1Sshidokht 	cl->cl_vtoc.v_nparts = V_NUMPAR;
2730e8fb11a1Sshidokht 	cl->cl_vtoc.v_version = V_VERSION;
27313ccda647Slclee 
27323ccda647Slclee 	/* Add backup slice */
2733e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_start = 0;
2734e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_size  = disksize;
2735e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
2736e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
27373ccda647Slclee 
2738e8fb11a1Sshidokht 	cl->cl_map[2].dkl_cylno = 0;
2739e8fb11a1Sshidokht 	cl->cl_map[2].dkl_nblk  = disksize;
2740e8fb11a1Sshidokht 	cl->cl_offset[2] = 0;
27413ccda647Slclee 
2742843e1988Sjohnlev 	/*
2743843e1988Sjohnlev 	 * single slice (s0) covering the entire disk
2744843e1988Sjohnlev 	 */
2745843e1988Sjohnlev 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2746843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_start = 0;
2747843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_tag   = V_UNASSIGNED;
2748843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_flag  = 0;
2749843e1988Sjohnlev 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2750843e1988Sjohnlev 		cl->cl_map[0].dkl_cylno = 0;
2751843e1988Sjohnlev 		cl->cl_map[0].dkl_nblk  = disksize;
2752843e1988Sjohnlev 		cl->cl_offset[0] = 0;
2753843e1988Sjohnlev 	}
2754843e1988Sjohnlev 
2755e8fb11a1Sshidokht 	(void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
2756e8fb11a1Sshidokht 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
2757e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
27583ccda647Slclee 
27593ccda647Slclee #else
27603ccda647Slclee #error "No VTOC format defined."
27613ccda647Slclee #endif
27623ccda647Slclee 
2763e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct  = 0;
2764e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = 0;
27653ccda647Slclee 
2766e8fb11a1Sshidokht 	cl->cl_g.dkg_intrlv = 1;
27673ccda647Slclee 
2768e8fb11a1Sshidokht 	cl->cl_vtoc.v_sanity  = VTOC_SANE;
27693ccda647Slclee 
2770e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = TRUE;
2771e8fb11a1Sshidokht 	cl->cl_vtoc_label_is_from_media = 0;
27723ccda647Slclee 
2773e8fb11a1Sshidokht 	cmlb_dbg(CMLB_INFO,  cl,
27743ccda647Slclee 	    "cmlb_build_default_label: Default label created: "
27753ccda647Slclee 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
2776e8fb11a1Sshidokht 	    cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
2777e8fb11a1Sshidokht 	    cl->cl_g.dkg_nsect, cl->cl_blockcount);
27783ccda647Slclee }
27793ccda647Slclee 
27803ccda647Slclee 
27813ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
27823ccda647Slclee /*
27833ccda647Slclee  * Max CHS values, as they are encoded into bytes, for 1022/254/63
27843ccda647Slclee  */
27853ccda647Slclee #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
27863ccda647Slclee #define	LBA_MAX_CYL	(1022 & 0xFF)
27873ccda647Slclee #define	LBA_MAX_HEAD	(254)
27883ccda647Slclee 
27893ccda647Slclee 
27903ccda647Slclee /*
27913ccda647Slclee  *    Function: cmlb_has_max_chs_vals
27923ccda647Slclee  *
27933ccda647Slclee  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
27943ccda647Slclee  *
27953ccda647Slclee  *   Arguments: fdp - ptr to CHS info
27963ccda647Slclee  *
27973ccda647Slclee  * Return Code: True or false
27983ccda647Slclee  *
27993ccda647Slclee  *     Context: Any.
28003ccda647Slclee  */
28013ccda647Slclee static int
28023ccda647Slclee cmlb_has_max_chs_vals(struct ipart *fdp)
28033ccda647Slclee {
28043ccda647Slclee 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
28053ccda647Slclee 	    (fdp->beghead == LBA_MAX_HEAD)	&&
28063ccda647Slclee 	    (fdp->begsect == LBA_MAX_SECT)	&&
28073ccda647Slclee 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
28083ccda647Slclee 	    (fdp->endhead == LBA_MAX_HEAD)	&&
28093ccda647Slclee 	    (fdp->endsect == LBA_MAX_SECT));
28103ccda647Slclee }
28113ccda647Slclee #endif
28123ccda647Slclee 
28133ccda647Slclee /*
28143ccda647Slclee  *    Function: cmlb_dkio_get_geometry
28153ccda647Slclee  *
28163ccda647Slclee  * Description: This routine is the driver entry point for handling user
28173ccda647Slclee  *		requests to get the device geometry (DKIOCGGEOM).
28183ccda647Slclee  *
28193ccda647Slclee  *   Arguments:
2820e8fb11a1Sshidokht  *	arg		pointer to user provided dk_geom structure specifying
28213ccda647Slclee  *			the controller's notion of the current geometry.
2822e8fb11a1Sshidokht  *
2823e8fb11a1Sshidokht  *	flag 		this argument is a pass through to ddi_copyxxx()
28243ccda647Slclee  *			directly from the mode argument of ioctl().
28253ccda647Slclee  *
2826e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
2827e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
2828e8fb11a1Sshidokht  *
28293ccda647Slclee  * Return Code: 0
28303ccda647Slclee  *		EFAULT
28313ccda647Slclee  *		ENXIO
28323ccda647Slclee  *		EIO
28333ccda647Slclee  */
28343ccda647Slclee static int
2835e8fb11a1Sshidokht cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
2836e8fb11a1Sshidokht     void *tg_cookie)
28373ccda647Slclee {
28383ccda647Slclee 	struct dk_geom	*tmp_geom = NULL;
28393ccda647Slclee 	int		rval = 0;
28403ccda647Slclee 
28413ccda647Slclee 	/*
28423ccda647Slclee 	 * cmlb_validate_geometry does not spin a disk up
2843e8fb11a1Sshidokht 	 * if it was spcl down. We need to make sure it
28443ccda647Slclee 	 * is ready.
28453ccda647Slclee 	 */
2846e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
2847e8fb11a1Sshidokht 	rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie);
28483ccda647Slclee #if defined(_SUNOS_VTOC_8)
28493ccda647Slclee 	if (rval == EINVAL &&
2850e8fb11a1Sshidokht 	    cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
28513ccda647Slclee 		/*
28523ccda647Slclee 		 * This is to return a default label geometry even when we
28533ccda647Slclee 		 * do not really assume a default label for the device.
28543ccda647Slclee 		 * dad driver utilizes this.
28553ccda647Slclee 		 */
2856e8fb11a1Sshidokht 		if (cl->cl_blockcount <= DK_MAX_BLOCKS) {
2857e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
28583ccda647Slclee 			rval = 0;
28593ccda647Slclee 		}
28603ccda647Slclee 	}
28613ccda647Slclee #endif
28623ccda647Slclee 	if (rval) {
2863e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
28643ccda647Slclee 		return (rval);
28653ccda647Slclee 	}
28663ccda647Slclee 
28673ccda647Slclee #if defined(__i386) || defined(__amd64)
2868e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
2869e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
28703ccda647Slclee 		return (EIO);
28713ccda647Slclee 	}
28723ccda647Slclee #endif
28733ccda647Slclee 
28743ccda647Slclee 	/*
28753ccda647Slclee 	 * Make a local copy of the soft state geometry to avoid some potential
28763ccda647Slclee 	 * race conditions associated with holding the mutex and updating the
28773ccda647Slclee 	 * write_reinstruct value
28783ccda647Slclee 	 */
28793ccda647Slclee 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
2880e8fb11a1Sshidokht 	bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
28813ccda647Slclee 
28823ccda647Slclee 	if (tmp_geom->dkg_write_reinstruct == 0) {
28833ccda647Slclee 		tmp_geom->dkg_write_reinstruct =
28843ccda647Slclee 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
28853ccda647Slclee 		    cmlb_rot_delay) / (int)60000);
28863ccda647Slclee 	}
2887e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
28883ccda647Slclee 
28893ccda647Slclee 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
28903ccda647Slclee 	    flag);
28913ccda647Slclee 	if (rval != 0) {
28923ccda647Slclee 		rval = EFAULT;
28933ccda647Slclee 	}
28943ccda647Slclee 
28953ccda647Slclee 	kmem_free(tmp_geom, sizeof (struct dk_geom));
28963ccda647Slclee 	return (rval);
28973ccda647Slclee 
28983ccda647Slclee }
28993ccda647Slclee 
29003ccda647Slclee 
29013ccda647Slclee /*
29023ccda647Slclee  *    Function: cmlb_dkio_set_geometry
29033ccda647Slclee  *
29043ccda647Slclee  * Description: This routine is the driver entry point for handling user
29053ccda647Slclee  *		requests to set the device geometry (DKIOCSGEOM). The actual
29063ccda647Slclee  *		device geometry is not updated, just the driver "notion" of it.
29073ccda647Slclee  *
29083ccda647Slclee  *   Arguments:
2909e8fb11a1Sshidokht  *	arg		pointer to user provided dk_geom structure used to set
29103ccda647Slclee  *			the controller's notion of the current geometry.
2911e8fb11a1Sshidokht  *
2912e8fb11a1Sshidokht  *	flag 		this argument is a pass through to ddi_copyxxx()
29133ccda647Slclee  *			directly from the mode argument of ioctl().
29143ccda647Slclee  *
2915e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
2916e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
2917e8fb11a1Sshidokht  *
29183ccda647Slclee  * Return Code: 0
29193ccda647Slclee  *		EFAULT
29203ccda647Slclee  *		ENXIO
29213ccda647Slclee  *		EIO
29223ccda647Slclee  */
29233ccda647Slclee static int
2924e8fb11a1Sshidokht cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
29253ccda647Slclee {
29263ccda647Slclee 	struct dk_geom	*tmp_geom;
29273ccda647Slclee 	struct dk_map	*lp;
29283ccda647Slclee 	int		rval = 0;
29293ccda647Slclee 	int		i;
29303ccda647Slclee 
29313ccda647Slclee 
29323ccda647Slclee #if defined(__i386) || defined(__amd64)
2933e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
29343ccda647Slclee 		return (EIO);
29353ccda647Slclee 	}
29363ccda647Slclee #endif
29373ccda647Slclee 	/*
29383ccda647Slclee 	 * We need to copy the user specified geometry into local
29393ccda647Slclee 	 * storage and then update the softstate. We don't want to hold
29403ccda647Slclee 	 * the mutex and copyin directly from the user to the soft state
29413ccda647Slclee 	 */
29423ccda647Slclee 	tmp_geom = (struct dk_geom *)
29433ccda647Slclee 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
29443ccda647Slclee 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
29453ccda647Slclee 	if (rval != 0) {
29463ccda647Slclee 		kmem_free(tmp_geom, sizeof (struct dk_geom));
29473ccda647Slclee 		return (EFAULT);
29483ccda647Slclee 	}
29493ccda647Slclee 
2950e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
2951e8fb11a1Sshidokht 	bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
29523ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
2953e8fb11a1Sshidokht 		lp  = &cl->cl_map[i];
2954e8fb11a1Sshidokht 		cl->cl_offset[i] =
2955e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
29563ccda647Slclee #if defined(__i386) || defined(__amd64)
2957e8fb11a1Sshidokht 		cl->cl_offset[i] += cl->cl_solaris_offset;
29583ccda647Slclee #endif
29593ccda647Slclee 	}
2960e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
2961e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
29623ccda647Slclee 	kmem_free(tmp_geom, sizeof (struct dk_geom));
29633ccda647Slclee 
29643ccda647Slclee 	return (rval);
29653ccda647Slclee }
29663ccda647Slclee 
29673ccda647Slclee /*
29683ccda647Slclee  *    Function: cmlb_dkio_get_partition
29693ccda647Slclee  *
29703ccda647Slclee  * Description: This routine is the driver entry point for handling user
29713ccda647Slclee  *		requests to get the partition table (DKIOCGAPART).
29723ccda647Slclee  *
29733ccda647Slclee  *   Arguments:
2974e8fb11a1Sshidokht  *	arg		pointer to user provided dk_allmap structure specifying
29753ccda647Slclee  *			the controller's notion of the current partition table.
2976e8fb11a1Sshidokht  *
2977e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
29783ccda647Slclee  *			directly from the mode argument of ioctl().
29793ccda647Slclee  *
2980e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
2981e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
2982e8fb11a1Sshidokht  *
29833ccda647Slclee  * Return Code: 0
29843ccda647Slclee  *		EFAULT
29853ccda647Slclee  *		ENXIO
29863ccda647Slclee  *		EIO
29873ccda647Slclee  */
29883ccda647Slclee static int
2989e8fb11a1Sshidokht cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
2990e8fb11a1Sshidokht     void *tg_cookie)
29913ccda647Slclee {
29923ccda647Slclee 	int		rval = 0;
29933ccda647Slclee 	int		size;
29943ccda647Slclee 
29953ccda647Slclee 	/*
29963ccda647Slclee 	 * Make sure the geometry is valid before getting the partition
29973ccda647Slclee 	 * information.
29983ccda647Slclee 	 */
2999e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3000e8fb11a1Sshidokht 	if ((rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie)) != 0) {
3001e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
30023ccda647Slclee 		return (rval);
30033ccda647Slclee 	}
3004e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
30053ccda647Slclee 
30063ccda647Slclee #if defined(__i386) || defined(__amd64)
3007e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
30083ccda647Slclee 		return (EIO);
30093ccda647Slclee 	}
30103ccda647Slclee #endif
30113ccda647Slclee 
30123ccda647Slclee #ifdef _MULTI_DATAMODEL
30133ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
30143ccda647Slclee 	case DDI_MODEL_ILP32: {
30153ccda647Slclee 		struct dk_map32 dk_map32[NDKMAP];
30163ccda647Slclee 		int		i;
30173ccda647Slclee 
30183ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
3019e8fb11a1Sshidokht 			dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3020e8fb11a1Sshidokht 			dk_map32[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
30213ccda647Slclee 		}
30223ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map32);
30233ccda647Slclee 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
30243ccda647Slclee 		if (rval != 0) {
30253ccda647Slclee 			rval = EFAULT;
30263ccda647Slclee 		}
30273ccda647Slclee 		break;
30283ccda647Slclee 	}
30293ccda647Slclee 	case DDI_MODEL_NONE:
30303ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map);
3031e8fb11a1Sshidokht 		rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
30323ccda647Slclee 		if (rval != 0) {
30333ccda647Slclee 			rval = EFAULT;
30343ccda647Slclee 		}
30353ccda647Slclee 		break;
30363ccda647Slclee 	}
30373ccda647Slclee #else /* ! _MULTI_DATAMODEL */
30383ccda647Slclee 	size = NDKMAP * sizeof (struct dk_map);
3039e8fb11a1Sshidokht 	rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
30403ccda647Slclee 	if (rval != 0) {
30413ccda647Slclee 		rval = EFAULT;
30423ccda647Slclee 	}
30433ccda647Slclee #endif /* _MULTI_DATAMODEL */
30443ccda647Slclee 	return (rval);
30453ccda647Slclee }
30463ccda647Slclee 
30473ccda647Slclee /*
30483ccda647Slclee  *    Function: cmlb_dkio_set_partition
30493ccda647Slclee  *
30503ccda647Slclee  * Description: This routine is the driver entry point for handling user
30513ccda647Slclee  *		requests to set the partition table (DKIOCSAPART). The actual
30523ccda647Slclee  *		device partition is not updated.
30533ccda647Slclee  *
30543ccda647Slclee  *   Arguments:
30553ccda647Slclee  *		arg  - pointer to user provided dk_allmap structure used to set
30563ccda647Slclee  *			the controller's notion of the partition table.
30573ccda647Slclee  *		flag - this argument is a pass through to ddi_copyxxx()
30583ccda647Slclee  *		       directly from the mode argument of ioctl().
30593ccda647Slclee  *
30603ccda647Slclee  * Return Code: 0
30613ccda647Slclee  *		EINVAL
30623ccda647Slclee  *		EFAULT
30633ccda647Slclee  *		ENXIO
30643ccda647Slclee  *		EIO
30653ccda647Slclee  */
30663ccda647Slclee static int
3067e8fb11a1Sshidokht cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
30683ccda647Slclee {
30693ccda647Slclee 	struct dk_map	dk_map[NDKMAP];
30703ccda647Slclee 	struct dk_map	*lp;
30713ccda647Slclee 	int		rval = 0;
30723ccda647Slclee 	int		size;
30733ccda647Slclee 	int		i;
30743ccda647Slclee #if defined(_SUNOS_VTOC_16)
30753ccda647Slclee 	struct dkl_partition	*vp;
30763ccda647Slclee #endif
30773ccda647Slclee 
30783ccda647Slclee 	/*
30793ccda647Slclee 	 * Set the map for all logical partitions.  We lock
30803ccda647Slclee 	 * the priority just to make sure an interrupt doesn't
30813ccda647Slclee 	 * come in while the map is half updated.
30823ccda647Slclee 	 */
3083e8fb11a1Sshidokht 	_NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3084e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
30853ccda647Slclee 
3086e8fb11a1Sshidokht 	if (cl->cl_blockcount > DK_MAX_BLOCKS) {
3087e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
30883ccda647Slclee 		return (ENOTSUP);
30893ccda647Slclee 	}
3090e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3091e8fb11a1Sshidokht 	if (cl->cl_solaris_size == 0) {
30923ccda647Slclee 		return (EIO);
30933ccda647Slclee 	}
30943ccda647Slclee 
30953ccda647Slclee #ifdef _MULTI_DATAMODEL
30963ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
30973ccda647Slclee 	case DDI_MODEL_ILP32: {
30983ccda647Slclee 		struct dk_map32 dk_map32[NDKMAP];
30993ccda647Slclee 
31003ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map32);
31013ccda647Slclee 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
31023ccda647Slclee 		if (rval != 0) {
31033ccda647Slclee 			return (EFAULT);
31043ccda647Slclee 		}
31053ccda647Slclee 		for (i = 0; i < NDKMAP; i++) {
31063ccda647Slclee 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
31073ccda647Slclee 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
31083ccda647Slclee 		}
31093ccda647Slclee 		break;
31103ccda647Slclee 	}
31113ccda647Slclee 	case DDI_MODEL_NONE:
31123ccda647Slclee 		size = NDKMAP * sizeof (struct dk_map);
31133ccda647Slclee 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
31143ccda647Slclee 		if (rval != 0) {
31153ccda647Slclee 			return (EFAULT);
31163ccda647Slclee 		}
31173ccda647Slclee 		break;
31183ccda647Slclee 	}
31193ccda647Slclee #else /* ! _MULTI_DATAMODEL */
31203ccda647Slclee 	size = NDKMAP * sizeof (struct dk_map);
31213ccda647Slclee 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
31223ccda647Slclee 	if (rval != 0) {
31233ccda647Slclee 		return (EFAULT);
31243ccda647Slclee 	}
31253ccda647Slclee #endif /* _MULTI_DATAMODEL */
31263ccda647Slclee 
3127e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
31283ccda647Slclee 	/* Note: The size used in this bcopy is set based upon the data model */
3129e8fb11a1Sshidokht 	bcopy(dk_map, cl->cl_map, size);
31303ccda647Slclee #if defined(_SUNOS_VTOC_16)
3131e8fb11a1Sshidokht 	vp = (struct dkl_partition *)&(cl->cl_vtoc);
31323ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
31333ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
3134e8fb11a1Sshidokht 		lp  = &cl->cl_map[i];
3135e8fb11a1Sshidokht 		cl->cl_offset[i] =
3136e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
31373ccda647Slclee #if defined(_SUNOS_VTOC_16)
3138e8fb11a1Sshidokht 		vp->p_start = cl->cl_offset[i];
31393ccda647Slclee 		vp->p_size = lp->dkl_nblk;
31403ccda647Slclee 		vp++;
31413ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
31423ccda647Slclee #if defined(__i386) || defined(__amd64)
3143e8fb11a1Sshidokht 		cl->cl_offset[i] += cl->cl_solaris_offset;
31443ccda647Slclee #endif
31453ccda647Slclee 	}
3146e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
31473ccda647Slclee 	return (rval);
31483ccda647Slclee }
31493ccda647Slclee 
31503ccda647Slclee 
31513ccda647Slclee /*
31523ccda647Slclee  *    Function: cmlb_dkio_get_vtoc
31533ccda647Slclee  *
31543ccda647Slclee  * Description: This routine is the driver entry point for handling user
31553ccda647Slclee  *		requests to get the current volume table of contents
31563ccda647Slclee  *		(DKIOCGVTOC).
31573ccda647Slclee  *
31583ccda647Slclee  *   Arguments:
3159e8fb11a1Sshidokht  *	arg		pointer to user provided vtoc structure specifying
31603ccda647Slclee  *			the current vtoc.
3161e8fb11a1Sshidokht  *
3162e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
31633ccda647Slclee  *			directly from the mode argument of ioctl().
31643ccda647Slclee  *
3165e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3166e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3167e8fb11a1Sshidokht  *
31683ccda647Slclee  * Return Code: 0
31693ccda647Slclee  *		EFAULT
31703ccda647Slclee  *		ENXIO
31713ccda647Slclee  *		EIO
31723ccda647Slclee  */
31733ccda647Slclee static int
3174e8fb11a1Sshidokht cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
31753ccda647Slclee {
31763ccda647Slclee #if defined(_SUNOS_VTOC_8)
31773ccda647Slclee 	struct vtoc	user_vtoc;
31783ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
31793ccda647Slclee 	int		rval = 0;
31803ccda647Slclee 
3181e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3182e8fb11a1Sshidokht 	rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie);
31833ccda647Slclee 
31843ccda647Slclee #if defined(_SUNOS_VTOC_8)
31853ccda647Slclee 	if (rval == EINVAL &&
3186e8fb11a1Sshidokht 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
31873ccda647Slclee 		/*
31883ccda647Slclee 		 * This is to return a default label even when we do not
31893ccda647Slclee 		 * really assume a default label for the device.
31903ccda647Slclee 		 * dad driver utilizes this.
31913ccda647Slclee 		 */
3192e8fb11a1Sshidokht 		if (cl->cl_blockcount <= DK_MAX_BLOCKS) {
3193e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
31943ccda647Slclee 			rval = 0;
31953ccda647Slclee 		}
31963ccda647Slclee 	}
31973ccda647Slclee #endif
31983ccda647Slclee 	if (rval) {
3199e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
32003ccda647Slclee 		return (rval);
32013ccda647Slclee 	}
32023ccda647Slclee 
32033ccda647Slclee #if defined(_SUNOS_VTOC_8)
3204e8fb11a1Sshidokht 	cmlb_build_user_vtoc(cl, &user_vtoc);
3205e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
32063ccda647Slclee 
32073ccda647Slclee #ifdef _MULTI_DATAMODEL
32083ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
32093ccda647Slclee 	case DDI_MODEL_ILP32: {
32103ccda647Slclee 		struct vtoc32 user_vtoc32;
32113ccda647Slclee 
32123ccda647Slclee 		vtoctovtoc32(user_vtoc, user_vtoc32);
32133ccda647Slclee 		if (ddi_copyout(&user_vtoc32, (void *)arg,
32143ccda647Slclee 		    sizeof (struct vtoc32), flag)) {
32153ccda647Slclee 			return (EFAULT);
32163ccda647Slclee 		}
32173ccda647Slclee 		break;
32183ccda647Slclee 	}
32193ccda647Slclee 
32203ccda647Slclee 	case DDI_MODEL_NONE:
32213ccda647Slclee 		if (ddi_copyout(&user_vtoc, (void *)arg,
32223ccda647Slclee 		    sizeof (struct vtoc), flag)) {
32233ccda647Slclee 			return (EFAULT);
32243ccda647Slclee 		}
32253ccda647Slclee 		break;
32263ccda647Slclee 	}
32273ccda647Slclee #else /* ! _MULTI_DATAMODEL */
32283ccda647Slclee 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
32293ccda647Slclee 		return (EFAULT);
32303ccda647Slclee 	}
32313ccda647Slclee #endif /* _MULTI_DATAMODEL */
32323ccda647Slclee 
32333ccda647Slclee #elif defined(_SUNOS_VTOC_16)
3234e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
32353ccda647Slclee 
32363ccda647Slclee #ifdef _MULTI_DATAMODEL
32373ccda647Slclee 	/*
3238e8fb11a1Sshidokht 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
32393ccda647Slclee 	 * 32-bit to maintain compatibility with existing on-disk
32403ccda647Slclee 	 * structures.  Thus, we need to convert the structure when copying
32413ccda647Slclee 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
32423ccda647Slclee 	 * program.  If the target is a 32-bit program, then no conversion
32433ccda647Slclee 	 * is necessary.
32443ccda647Slclee 	 */
32453ccda647Slclee 	/* LINTED: logical expression always true: op "||" */
3246e8fb11a1Sshidokht 	ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
32473ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
32483ccda647Slclee 	case DDI_MODEL_ILP32:
3249e8fb11a1Sshidokht 		if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
3250e8fb11a1Sshidokht 		    sizeof (cl->cl_vtoc), flag)) {
32513ccda647Slclee 			return (EFAULT);
32523ccda647Slclee 		}
32533ccda647Slclee 		break;
32543ccda647Slclee 
32553ccda647Slclee 	case DDI_MODEL_NONE: {
32563ccda647Slclee 		struct vtoc user_vtoc;
32573ccda647Slclee 
3258e8fb11a1Sshidokht 		vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
32593ccda647Slclee 		if (ddi_copyout(&user_vtoc, (void *)arg,
32603ccda647Slclee 		    sizeof (struct vtoc), flag)) {
32613ccda647Slclee 			return (EFAULT);
32623ccda647Slclee 		}
32633ccda647Slclee 		break;
32643ccda647Slclee 	}
32653ccda647Slclee 	}
32663ccda647Slclee #else /* ! _MULTI_DATAMODEL */
3267e8fb11a1Sshidokht 	if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
32683ccda647Slclee 	    flag)) {
32693ccda647Slclee 		return (EFAULT);
32703ccda647Slclee 	}
32713ccda647Slclee #endif /* _MULTI_DATAMODEL */
32723ccda647Slclee #else
32733ccda647Slclee #error "No VTOC format defined."
32743ccda647Slclee #endif
32753ccda647Slclee 
32763ccda647Slclee 	return (rval);
32773ccda647Slclee }
32783ccda647Slclee 
32793ccda647Slclee static int
3280e8fb11a1Sshidokht cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
32813ccda647Slclee {
32823ccda647Slclee 	dk_efi_t	user_efi;
32833ccda647Slclee 	int		rval = 0;
32843ccda647Slclee 	void		*buffer;
3285e8fb11a1Sshidokht 	diskaddr_t	tgt_lba;
32863ccda647Slclee 
32873ccda647Slclee 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
32883ccda647Slclee 		return (EFAULT);
32893ccda647Slclee 
32903ccda647Slclee 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
32913ccda647Slclee 
3292e8fb11a1Sshidokht 	tgt_lba = user_efi.dki_lba;
3293e8fb11a1Sshidokht 
3294e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3295e8fb11a1Sshidokht 	if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
3296e8fb11a1Sshidokht 	    (cl->cl_tgt_blocksize == 0)) {
3297e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
3298e8fb11a1Sshidokht 		return (EINVAL);
3299e8fb11a1Sshidokht 	}
3300e8fb11a1Sshidokht 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
3301e8fb11a1Sshidokht 		tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
3302e8fb11a1Sshidokht 		    cl->cl_sys_blocksize;
3303e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3304e8fb11a1Sshidokht 
33053ccda647Slclee 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
3306e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
33073ccda647Slclee 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
33083ccda647Slclee 	    user_efi.dki_length, flag) != 0)
33093ccda647Slclee 		rval = EFAULT;
33103ccda647Slclee 
33113ccda647Slclee 	kmem_free(buffer, user_efi.dki_length);
33123ccda647Slclee 	return (rval);
33133ccda647Slclee }
33143ccda647Slclee 
3315e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
33163ccda647Slclee /*
33173ccda647Slclee  *    Function: cmlb_build_user_vtoc
33183ccda647Slclee  *
33193ccda647Slclee  * Description: This routine populates a pass by reference variable with the
33203ccda647Slclee  *		current volume table of contents.
33213ccda647Slclee  *
3322e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
33233ccda647Slclee  *		user_vtoc - pointer to vtoc structure to be populated
33243ccda647Slclee  */
33253ccda647Slclee static void
3326e8fb11a1Sshidokht cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
33273ccda647Slclee {
33283ccda647Slclee 	struct dk_map2		*lpart;
33293ccda647Slclee 	struct dk_map		*lmap;
33303ccda647Slclee 	struct partition	*vpart;
33313ccda647Slclee 	int			nblks;
33323ccda647Slclee 	int			i;
33333ccda647Slclee 
3334e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
33353ccda647Slclee 
33363ccda647Slclee 	/*
33373ccda647Slclee 	 * Return vtoc structure fields in the provided VTOC area, addressed
33383ccda647Slclee 	 * by *vtoc.
33393ccda647Slclee 	 */
33403ccda647Slclee 	bzero(user_vtoc, sizeof (struct vtoc));
3341e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
3342e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
3343e8fb11a1Sshidokht 	user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
33443ccda647Slclee 	user_vtoc->v_sanity	= VTOC_SANE;
3345e8fb11a1Sshidokht 	user_vtoc->v_version	= cl->cl_vtoc.v_version;
3346e8fb11a1Sshidokht 	bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
3347e8fb11a1Sshidokht 	user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
3348e8fb11a1Sshidokht 	user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
33493ccda647Slclee 
33503ccda647Slclee 	for (i = 0; i < 10; i++)
3351e8fb11a1Sshidokht 		user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
33523ccda647Slclee 
33533ccda647Slclee 	/*
33543ccda647Slclee 	 * Convert partitioning information.
33553ccda647Slclee 	 *
33563ccda647Slclee 	 * Note the conversion from starting cylinder number
33573ccda647Slclee 	 * to starting sector number.
33583ccda647Slclee 	 */
3359e8fb11a1Sshidokht 	lmap = cl->cl_map;
3360e8fb11a1Sshidokht 	lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
33613ccda647Slclee 	vpart = user_vtoc->v_part;
33623ccda647Slclee 
3363e8fb11a1Sshidokht 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
33643ccda647Slclee 
33653ccda647Slclee 	for (i = 0; i < V_NUMPAR; i++) {
33663ccda647Slclee 		vpart->p_tag	= lpart->p_tag;
33673ccda647Slclee 		vpart->p_flag	= lpart->p_flag;
33683ccda647Slclee 		vpart->p_start	= lmap->dkl_cylno * nblks;
33693ccda647Slclee 		vpart->p_size	= lmap->dkl_nblk;
33703ccda647Slclee 		lmap++;
33713ccda647Slclee 		lpart++;
33723ccda647Slclee 		vpart++;
33733ccda647Slclee 
33743ccda647Slclee 		/* (4364927) */
3375e8fb11a1Sshidokht 		user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
33763ccda647Slclee 	}
33773ccda647Slclee 
3378e8fb11a1Sshidokht 	bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
33793ccda647Slclee }
3380e8fb11a1Sshidokht #endif
33813ccda647Slclee 
33823ccda647Slclee static int
3383e8fb11a1Sshidokht cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3384e8fb11a1Sshidokht     void *tg_cookie)
33853ccda647Slclee {
33863ccda647Slclee 	struct partition64	p64;
33873ccda647Slclee 	int			rval = 0;
33883ccda647Slclee 	uint_t			nparts;
33893ccda647Slclee 	efi_gpe_t		*partitions;
33903ccda647Slclee 	efi_gpt_t		*buffer;
33913ccda647Slclee 	diskaddr_t		gpe_lba;
33923ccda647Slclee 
33933ccda647Slclee 	if (ddi_copyin((const void *)arg, &p64,
33943ccda647Slclee 	    sizeof (struct partition64), flag)) {
33953ccda647Slclee 		return (EFAULT);
33963ccda647Slclee 	}
33973ccda647Slclee 
33983ccda647Slclee 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
3399e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buffer, 1, DEV_BSIZE, tg_cookie);
34003ccda647Slclee 	if (rval != 0)
34013ccda647Slclee 		goto done_error;
34023ccda647Slclee 
34033ccda647Slclee 	cmlb_swap_efi_gpt(buffer);
34043ccda647Slclee 
34053ccda647Slclee 	if ((rval = cmlb_validate_efi(buffer)) != 0)
34063ccda647Slclee 		goto done_error;
34073ccda647Slclee 
34083ccda647Slclee 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
34093ccda647Slclee 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
34103ccda647Slclee 	if (p64.p_partno > nparts) {
34113ccda647Slclee 		/* couldn't find it */
34123ccda647Slclee 		rval = ESRCH;
34133ccda647Slclee 		goto done_error;
34143ccda647Slclee 	}
34153ccda647Slclee 	/*
34163ccda647Slclee 	 * if we're dealing with a partition that's out of the normal
34173ccda647Slclee 	 * 16K block, adjust accordingly
34183ccda647Slclee 	 */
34193ccda647Slclee 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
3420e8fb11a1Sshidokht 	rval = DK_TG_READ(cl, buffer, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
34213ccda647Slclee 
34223ccda647Slclee 	if (rval) {
34233ccda647Slclee 		goto done_error;
34243ccda647Slclee 	}
34253ccda647Slclee 	partitions = (efi_gpe_t *)buffer;
34263ccda647Slclee 
34273ccda647Slclee 	cmlb_swap_efi_gpe(nparts, partitions);
34283ccda647Slclee 
34293ccda647Slclee 	partitions += p64.p_partno;
34303ccda647Slclee 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
34313ccda647Slclee 	    sizeof (struct uuid));
34323ccda647Slclee 	p64.p_start = partitions->efi_gpe_StartingLBA;
34333ccda647Slclee 	p64.p_size = partitions->efi_gpe_EndingLBA -
34343ccda647Slclee 	    p64.p_start + 1;
34353ccda647Slclee 
34363ccda647Slclee 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
34373ccda647Slclee 		rval = EFAULT;
34383ccda647Slclee 
34393ccda647Slclee done_error:
34403ccda647Slclee 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
34413ccda647Slclee 	return (rval);
34423ccda647Slclee }
34433ccda647Slclee 
34443ccda647Slclee 
34453ccda647Slclee /*
34463ccda647Slclee  *    Function: cmlb_dkio_set_vtoc
34473ccda647Slclee  *
34483ccda647Slclee  * Description: This routine is the driver entry point for handling user
34493ccda647Slclee  *		requests to set the current volume table of contents
34503ccda647Slclee  *		(DKIOCSVTOC).
34513ccda647Slclee  *
3452e8fb11a1Sshidokht  *   Arguments:
3453e8fb11a1Sshidokht  *	dev		the device number
3454e8fb11a1Sshidokht  *	arg		pointer to user provided vtoc structure used to set the
34553ccda647Slclee  *			current vtoc.
3456e8fb11a1Sshidokht  *
3457e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
34583ccda647Slclee  *			directly from the mode argument of ioctl().
34593ccda647Slclee  *
3460e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3461e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3462e8fb11a1Sshidokht  *
34633ccda647Slclee  * Return Code: 0
34643ccda647Slclee  *		EFAULT
34653ccda647Slclee  *		ENXIO
34663ccda647Slclee  *		EINVAL
34673ccda647Slclee  *		ENOTSUP
34683ccda647Slclee  */
34693ccda647Slclee static int
3470e8fb11a1Sshidokht cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3471e8fb11a1Sshidokht     void *tg_cookie)
34723ccda647Slclee {
34733ccda647Slclee 	struct vtoc	user_vtoc;
34743ccda647Slclee 	int		rval = 0;
34753ccda647Slclee 
34763ccda647Slclee #ifdef _MULTI_DATAMODEL
34773ccda647Slclee 	switch (ddi_model_convert_from(flag & FMODELS)) {
34783ccda647Slclee 	case DDI_MODEL_ILP32: {
34793ccda647Slclee 		struct vtoc32 user_vtoc32;
34803ccda647Slclee 
34813ccda647Slclee 		if (ddi_copyin((const void *)arg, &user_vtoc32,
34823ccda647Slclee 		    sizeof (struct vtoc32), flag)) {
34833ccda647Slclee 			return (EFAULT);
34843ccda647Slclee 		}
34853ccda647Slclee 		vtoc32tovtoc(user_vtoc32, user_vtoc);
34863ccda647Slclee 		break;
34873ccda647Slclee 	}
34883ccda647Slclee 
34893ccda647Slclee 	case DDI_MODEL_NONE:
34903ccda647Slclee 		if (ddi_copyin((const void *)arg, &user_vtoc,
34913ccda647Slclee 		    sizeof (struct vtoc), flag)) {
34923ccda647Slclee 			return (EFAULT);
34933ccda647Slclee 		}
34943ccda647Slclee 		break;
34953ccda647Slclee 	}
34963ccda647Slclee #else /* ! _MULTI_DATAMODEL */
34973ccda647Slclee 	if (ddi_copyin((const void *)arg, &user_vtoc,
34983ccda647Slclee 	    sizeof (struct vtoc), flag)) {
34993ccda647Slclee 		return (EFAULT);
35003ccda647Slclee 	}
35013ccda647Slclee #endif /* _MULTI_DATAMODEL */
35023ccda647Slclee 
3503e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3504e8fb11a1Sshidokht 	if (cl->cl_blockcount > DK_MAX_BLOCKS) {
3505e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
35063ccda647Slclee 		return (ENOTSUP);
35073ccda647Slclee 	}
3508e8fb11a1Sshidokht 
3509e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
3510e8fb11a1Sshidokht 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
3511e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
3512e8fb11a1Sshidokht 		return (EINVAL);
3513e8fb11a1Sshidokht 	}
3514e8fb11a1Sshidokht #endif
3515e8fb11a1Sshidokht 
3516e8fb11a1Sshidokht 	if (cl->cl_g.dkg_ncyl == 0) {
3517e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
35183ccda647Slclee 		return (EINVAL);
35193ccda647Slclee 	}
35203ccda647Slclee 
3521e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3522e8fb11a1Sshidokht 	cmlb_clear_efi(cl, tg_cookie);
3523e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
3524e8fb11a1Sshidokht 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
3525e8fb11a1Sshidokht 	(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h",
35263ccda647Slclee 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3527e8fb11a1Sshidokht 	    cl->cl_node_type, NULL);
3528e8fb11a1Sshidokht 	(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h,raw",
35293ccda647Slclee 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3530e8fb11a1Sshidokht 	    cl->cl_node_type, NULL);
3531e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
35323ccda647Slclee 
3533e8fb11a1Sshidokht 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
3534e8fb11a1Sshidokht 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
3535e8fb11a1Sshidokht 			if (cmlb_validate_geometry(cl, 1, 0, tg_cookie) != 0) {
3536e8fb11a1Sshidokht 				cmlb_dbg(CMLB_ERROR, cl,
35373ccda647Slclee 				    "cmlb_dkio_set_vtoc: "
35383ccda647Slclee 				    "Failed validate geometry\n");
35393ccda647Slclee 			}
35403ccda647Slclee 		}
35413ccda647Slclee 	}
3542e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
35433ccda647Slclee 	return (rval);
35443ccda647Slclee }
35453ccda647Slclee 
35463ccda647Slclee 
35473ccda647Slclee /*
35483ccda647Slclee  *    Function: cmlb_build_label_vtoc
35493ccda647Slclee  *
35503ccda647Slclee  * Description: This routine updates the driver soft state current volume table
35513ccda647Slclee  *		of contents based on a user specified vtoc.
35523ccda647Slclee  *
3553e8fb11a1Sshidokht  *   Arguments: cl - driver soft state (unit) structure
35543ccda647Slclee  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
35553ccda647Slclee  *			    to update the driver soft state.
35563ccda647Slclee  *
35573ccda647Slclee  * Return Code: 0
35583ccda647Slclee  *		EINVAL
35593ccda647Slclee  */
35603ccda647Slclee static int
3561e8fb11a1Sshidokht cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
35623ccda647Slclee {
35633ccda647Slclee 	struct dk_map		*lmap;
35643ccda647Slclee 	struct partition	*vpart;
35653ccda647Slclee 	int			nblks;
35663ccda647Slclee #if defined(_SUNOS_VTOC_8)
35673ccda647Slclee 	int			ncyl;
35683ccda647Slclee 	struct dk_map2		*lpart;
35693ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
35703ccda647Slclee 	int			i;
35713ccda647Slclee 
3572e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
35733ccda647Slclee 
35743ccda647Slclee 	/* Sanity-check the vtoc */
35753ccda647Slclee 	if (user_vtoc->v_sanity != VTOC_SANE ||
3576e8fb11a1Sshidokht 	    user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
35773ccda647Slclee 	    user_vtoc->v_nparts != V_NUMPAR) {
3578e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
35793ccda647Slclee 		    "cmlb_build_label_vtoc: vtoc not valid\n");
35803ccda647Slclee 		return (EINVAL);
35813ccda647Slclee 	}
35823ccda647Slclee 
3583e8fb11a1Sshidokht 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
35843ccda647Slclee 	if (nblks == 0) {
3585e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
35863ccda647Slclee 		    "cmlb_build_label_vtoc: geom nblks is 0\n");
35873ccda647Slclee 		return (EINVAL);
35883ccda647Slclee 	}
35893ccda647Slclee 
35903ccda647Slclee #if defined(_SUNOS_VTOC_8)
35913ccda647Slclee 	vpart = user_vtoc->v_part;
35923ccda647Slclee 	for (i = 0; i < V_NUMPAR; i++) {
35933ccda647Slclee 		if ((vpart->p_start % nblks) != 0) {
3594e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
35953ccda647Slclee 			    "cmlb_build_label_vtoc: p_start not multiply of"
35963ccda647Slclee 			    "nblks part %d p_start %d nblks %d\n", i,
35973ccda647Slclee 			    vpart->p_start, nblks);
35983ccda647Slclee 			return (EINVAL);
35993ccda647Slclee 		}
36003ccda647Slclee 		ncyl = vpart->p_start / nblks;
36013ccda647Slclee 		ncyl += vpart->p_size / nblks;
36023ccda647Slclee 		if ((vpart->p_size % nblks) != 0) {
36033ccda647Slclee 			ncyl++;
36043ccda647Slclee 		}
3605e8fb11a1Sshidokht 		if (ncyl > (int)cl->cl_g.dkg_ncyl) {
3606e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
36073ccda647Slclee 			    "cmlb_build_label_vtoc: ncyl %d  > dkg_ncyl %d"
36083ccda647Slclee 			    "p_size %ld p_start %ld nblks %d  part number %d"
36093ccda647Slclee 			    "tag %d\n",
3610e8fb11a1Sshidokht 			    ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
36113ccda647Slclee 			    vpart->p_start, nblks,
36123ccda647Slclee 			    i, vpart->p_tag);
36133ccda647Slclee 
36143ccda647Slclee 			return (EINVAL);
36153ccda647Slclee 		}
36163ccda647Slclee 		vpart++;
36173ccda647Slclee 	}
36183ccda647Slclee #endif	/* defined(_SUNOS_VTOC_8) */
36193ccda647Slclee 
36203ccda647Slclee 	/* Put appropriate vtoc structure fields into the disk label */
36213ccda647Slclee #if defined(_SUNOS_VTOC_16)
36223ccda647Slclee 	/*
36233ccda647Slclee 	 * The vtoc is always a 32bit data structure to maintain the
36243ccda647Slclee 	 * on-disk format. Convert "in place" instead of doing bcopy.
36253ccda647Slclee 	 */
3626e8fb11a1Sshidokht 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
36273ccda647Slclee 
36283ccda647Slclee 	/*
36293ccda647Slclee 	 * in the 16-slice vtoc, starting sectors are expressed in
36303ccda647Slclee 	 * numbers *relative* to the start of the Solaris fdisk partition.
36313ccda647Slclee 	 */
3632e8fb11a1Sshidokht 	lmap = cl->cl_map;
36333ccda647Slclee 	vpart = user_vtoc->v_part;
36343ccda647Slclee 
36353ccda647Slclee 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
36363ccda647Slclee 		lmap->dkl_cylno = vpart->p_start / nblks;
36373ccda647Slclee 		lmap->dkl_nblk = vpart->p_size;
36383ccda647Slclee 	}
36393ccda647Slclee 
36403ccda647Slclee #elif defined(_SUNOS_VTOC_8)
36413ccda647Slclee 
3642e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
3643e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
3644e8fb11a1Sshidokht 	cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
36453ccda647Slclee 
3646e8fb11a1Sshidokht 	cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
3647e8fb11a1Sshidokht 	cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
36483ccda647Slclee 
3649e8fb11a1Sshidokht 	bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
36503ccda647Slclee 
3651e8fb11a1Sshidokht 	cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
36523ccda647Slclee 
36533ccda647Slclee 	for (i = 0; i < 10; i++)
3654e8fb11a1Sshidokht 		cl->cl_vtoc.v_reserved[i] =  user_vtoc->v_reserved[i];
36553ccda647Slclee 
36563ccda647Slclee 	/*
36573ccda647Slclee 	 * Note the conversion from starting sector number
36583ccda647Slclee 	 * to starting cylinder number.
36593ccda647Slclee 	 * Return error if division results in a remainder.
36603ccda647Slclee 	 */
3661e8fb11a1Sshidokht 	lmap = cl->cl_map;
3662e8fb11a1Sshidokht 	lpart = cl->cl_vtoc.v_part;
36633ccda647Slclee 	vpart = user_vtoc->v_part;
36643ccda647Slclee 
36653ccda647Slclee 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
36663ccda647Slclee 		lpart->p_tag  = vpart->p_tag;
36673ccda647Slclee 		lpart->p_flag = vpart->p_flag;
36683ccda647Slclee 		lmap->dkl_cylno = vpart->p_start / nblks;
36693ccda647Slclee 		lmap->dkl_nblk = vpart->p_size;
36703ccda647Slclee 
36713ccda647Slclee 		lmap++;
36723ccda647Slclee 		lpart++;
36733ccda647Slclee 		vpart++;
36743ccda647Slclee 
36753ccda647Slclee 		/* (4387723) */
36763ccda647Slclee #ifdef _LP64
36773ccda647Slclee 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
3678e8fb11a1Sshidokht 			cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
36793ccda647Slclee 		} else {
3680e8fb11a1Sshidokht 			cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
36813ccda647Slclee 		}
36823ccda647Slclee #else
3683e8fb11a1Sshidokht 		cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
36843ccda647Slclee #endif
36853ccda647Slclee 	}
36863ccda647Slclee 
3687e8fb11a1Sshidokht 	bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
36883ccda647Slclee #else
36893ccda647Slclee #error "No VTOC format defined."
36903ccda647Slclee #endif
36913ccda647Slclee 	return (0);
36923ccda647Slclee }
36933ccda647Slclee 
36943ccda647Slclee /*
36953ccda647Slclee  *    Function: cmlb_clear_efi
36963ccda647Slclee  *
36973ccda647Slclee  * Description: This routine clears all EFI labels.
36983ccda647Slclee  *
3699e8fb11a1Sshidokht  *   Arguments:
3700e8fb11a1Sshidokht  *	cl		 driver soft state (unit) structure
37013ccda647Slclee  *
3702e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3703e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
37043ccda647Slclee  * Return Code: void
37053ccda647Slclee  */
37063ccda647Slclee static void
3707e8fb11a1Sshidokht cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
37083ccda647Slclee {
37093ccda647Slclee 	efi_gpt_t	*gpt;
37103ccda647Slclee 	diskaddr_t	cap;
37113ccda647Slclee 	int		rval;
37123ccda647Slclee 
3713e8fb11a1Sshidokht 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
3714e8fb11a1Sshidokht 
3715e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
3716e8fb11a1Sshidokht 	cl->cl_reserved = -1;
3717e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
37183ccda647Slclee 
37193ccda647Slclee 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
37203ccda647Slclee 
3721e8fb11a1Sshidokht 	if (DK_TG_READ(cl, gpt, 1, DEV_BSIZE, tg_cookie) != 0) {
37223ccda647Slclee 		goto done;
37233ccda647Slclee 	}
37243ccda647Slclee 
37253ccda647Slclee 	cmlb_swap_efi_gpt(gpt);
37263ccda647Slclee 	rval = cmlb_validate_efi(gpt);
37273ccda647Slclee 	if (rval == 0) {
37283ccda647Slclee 		/* clear primary */
37293ccda647Slclee 		bzero(gpt, sizeof (efi_gpt_t));
3730e8fb11a1Sshidokht 		if (rval = DK_TG_WRITE(cl, gpt, 1, EFI_LABEL_SIZE, tg_cookie)) {
3731e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
37323ccda647Slclee 			    "cmlb_clear_efi: clear primary label failed\n");
37333ccda647Slclee 		}
37343ccda647Slclee 	}
37353ccda647Slclee 	/* the backup */
3736e8fb11a1Sshidokht 	rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
37373ccda647Slclee 	if (rval) {
37383ccda647Slclee 		goto done;
37393ccda647Slclee 	}
37403ccda647Slclee 
3741e8fb11a1Sshidokht 	if ((rval = DK_TG_READ(cl, gpt, cap - 1, EFI_LABEL_SIZE, tg_cookie))
3742e8fb11a1Sshidokht 	    != 0) {
37433ccda647Slclee 		goto done;
37443ccda647Slclee 	}
37453ccda647Slclee 	cmlb_swap_efi_gpt(gpt);
37463ccda647Slclee 	rval = cmlb_validate_efi(gpt);
37473ccda647Slclee 	if (rval == 0) {
37483ccda647Slclee 		/* clear backup */
3749e8fb11a1Sshidokht 		cmlb_dbg(CMLB_TRACE,  cl,
37503ccda647Slclee 		    "cmlb_clear_efi clear backup@%lu\n", cap - 1);
37513ccda647Slclee 		bzero(gpt, sizeof (efi_gpt_t));
3752e8fb11a1Sshidokht 		if ((rval = DK_TG_WRITE(cl,  gpt, cap - 1, EFI_LABEL_SIZE,
3753e8fb11a1Sshidokht 		    tg_cookie))) {
3754e8fb11a1Sshidokht 			cmlb_dbg(CMLB_INFO,  cl,
37553ccda647Slclee 			    "cmlb_clear_efi: clear backup label failed\n");
37563ccda647Slclee 		}
3757e8fb11a1Sshidokht 	} else {
3758e8fb11a1Sshidokht 		/*
3759e8fb11a1Sshidokht 		 * Refer to comments related to off-by-1 at the
3760e8fb11a1Sshidokht 		 * header of this file
3761e8fb11a1Sshidokht 		 */
3762e8fb11a1Sshidokht 		if ((rval = DK_TG_READ(cl, gpt, cap - 2,
3763e8fb11a1Sshidokht 		    EFI_LABEL_SIZE, tg_cookie)) != 0) {
3764e8fb11a1Sshidokht 			goto done;
3765e8fb11a1Sshidokht 		}
3766e8fb11a1Sshidokht 		cmlb_swap_efi_gpt(gpt);
3767e8fb11a1Sshidokht 		rval = cmlb_validate_efi(gpt);
3768e8fb11a1Sshidokht 		if (rval == 0) {
3769e8fb11a1Sshidokht 			/* clear legacy backup EFI label */
3770e8fb11a1Sshidokht 			cmlb_dbg(CMLB_TRACE,  cl,
3771e8fb11a1Sshidokht 			    "cmlb_clear_efi clear legacy backup@%lu\n",
3772e8fb11a1Sshidokht 			    cap - 2);
3773e8fb11a1Sshidokht 			bzero(gpt, sizeof (efi_gpt_t));
3774e8fb11a1Sshidokht 			if ((rval = DK_TG_WRITE(cl,  gpt, cap - 2,
3775e8fb11a1Sshidokht 			    EFI_LABEL_SIZE, tg_cookie))) {
3776e8fb11a1Sshidokht 				cmlb_dbg(CMLB_INFO,  cl,
3777e8fb11a1Sshidokht 				"cmlb_clear_efi: clear legacy backup label "
3778e8fb11a1Sshidokht 				"failed\n");
3779e8fb11a1Sshidokht 			}
3780e8fb11a1Sshidokht 		}
37813ccda647Slclee 	}
37823ccda647Slclee 
37833ccda647Slclee done:
37843ccda647Slclee 	kmem_free(gpt, sizeof (efi_gpt_t));
37853ccda647Slclee }
37863ccda647Slclee 
37873ccda647Slclee /*
37883ccda647Slclee  *    Function: cmlb_set_vtoc
37893ccda647Slclee  *
37903ccda647Slclee  * Description: This routine writes data to the appropriate positions
37913ccda647Slclee  *
3792e8fb11a1Sshidokht  *   Arguments:
3793e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
3794e8fb11a1Sshidokht  *
3795e8fb11a1Sshidokht  *	dkl		the data to be written
3796e8fb11a1Sshidokht  *
3797e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3798e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
37993ccda647Slclee  *
38003ccda647Slclee  * Return: void
38013ccda647Slclee  */
38023ccda647Slclee static int
3803e8fb11a1Sshidokht cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
38043ccda647Slclee {
38053ccda647Slclee 	uint_t	label_addr;
38063ccda647Slclee 	int	sec;
38073ccda647Slclee 	int	blk;
38083ccda647Slclee 	int	head;
38093ccda647Slclee 	int	cyl;
38103ccda647Slclee 	int	rval;
38113ccda647Slclee 
38123ccda647Slclee #if defined(__i386) || defined(__amd64)
3813e8fb11a1Sshidokht 	label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
38143ccda647Slclee #else
38153ccda647Slclee 	/* Write the primary label at block 0 of the solaris partition. */
38163ccda647Slclee 	label_addr = 0;
38173ccda647Slclee #endif
38183ccda647Slclee 
3819e8fb11a1Sshidokht 	rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
3820e8fb11a1Sshidokht 	    tg_cookie);
38213ccda647Slclee 
38223ccda647Slclee 	if (rval != 0) {
38233ccda647Slclee 		return (rval);
38243ccda647Slclee 	}
38253ccda647Slclee 
38263ccda647Slclee 	/*
38273ccda647Slclee 	 * Calculate where the backup labels go.  They are always on
38283ccda647Slclee 	 * the last alternate cylinder, but some older drives put them
38293ccda647Slclee 	 * on head 2 instead of the last head.	They are always on the
38303ccda647Slclee 	 * first 5 odd sectors of the appropriate track.
38313ccda647Slclee 	 *
38323ccda647Slclee 	 * We have no choice at this point, but to believe that the
38333ccda647Slclee 	 * disk label is valid.	 Use the geometry of the disk
38343ccda647Slclee 	 * as described in the label.
38353ccda647Slclee 	 */
38363ccda647Slclee 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
38373ccda647Slclee 	head = dkl->dkl_nhead - 1;
38383ccda647Slclee 
38393ccda647Slclee 	/*
38403ccda647Slclee 	 * Write and verify the backup labels. Make sure we don't try to
38413ccda647Slclee 	 * write past the last cylinder.
38423ccda647Slclee 	 */
38433ccda647Slclee 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
38443ccda647Slclee 		blk = (daddr_t)(
38453ccda647Slclee 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
38463ccda647Slclee 		    (head * dkl->dkl_nsect) + sec);
38473ccda647Slclee #if defined(__i386) || defined(__amd64)
3848e8fb11a1Sshidokht 		blk += cl->cl_solaris_offset;
38493ccda647Slclee #endif
3850e8fb11a1Sshidokht 		rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
3851e8fb11a1Sshidokht 		    tg_cookie);
3852e8fb11a1Sshidokht 		cmlb_dbg(CMLB_INFO,  cl,
38533ccda647Slclee 		"cmlb_set_vtoc: wrote backup label %d\n", blk);
38543ccda647Slclee 		if (rval != 0) {
38553ccda647Slclee 			goto exit;
38563ccda647Slclee 		}
38573ccda647Slclee 	}
38583ccda647Slclee exit:
38593ccda647Slclee 	return (rval);
38603ccda647Slclee }
38613ccda647Slclee 
38623ccda647Slclee /*
38633ccda647Slclee  *    Function: cmlb_clear_vtoc
38643ccda647Slclee  *
38653ccda647Slclee  * Description: This routine clears out the VTOC labels.
38663ccda647Slclee  *
3867e8fb11a1Sshidokht  *   Arguments:
3868e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
3869e8fb11a1Sshidokht  *
3870e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3871e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
38723ccda647Slclee  *
38733ccda647Slclee  * Return: void
38743ccda647Slclee  */
38753ccda647Slclee static void
3876e8fb11a1Sshidokht cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
38773ccda647Slclee {
38783ccda647Slclee 	struct dk_label		*dkl;
38793ccda647Slclee 
3880e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
38813ccda647Slclee 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
3882e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
38833ccda647Slclee 	/*
38843ccda647Slclee 	 * cmlb_set_vtoc uses these fields in order to figure out
38853ccda647Slclee 	 * where to overwrite the backup labels
38863ccda647Slclee 	 */
3887e8fb11a1Sshidokht 	dkl->dkl_apc    = cl->cl_g.dkg_apc;
3888e8fb11a1Sshidokht 	dkl->dkl_ncyl   = cl->cl_g.dkg_ncyl;
3889e8fb11a1Sshidokht 	dkl->dkl_acyl   = cl->cl_g.dkg_acyl;
3890e8fb11a1Sshidokht 	dkl->dkl_nhead  = cl->cl_g.dkg_nhead;
3891e8fb11a1Sshidokht 	dkl->dkl_nsect  = cl->cl_g.dkg_nsect;
3892e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
3893e8fb11a1Sshidokht 	(void) cmlb_set_vtoc(cl, dkl, tg_cookie);
38943ccda647Slclee 	kmem_free(dkl, sizeof (struct dk_label));
38953ccda647Slclee 
3896e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
38973ccda647Slclee }
38983ccda647Slclee 
38993ccda647Slclee /*
39003ccda647Slclee  *    Function: cmlb_write_label
39013ccda647Slclee  *
39023ccda647Slclee  * Description: This routine will validate and write the driver soft state vtoc
39033ccda647Slclee  *		contents to the device.
39043ccda647Slclee  *
3905e8fb11a1Sshidokht  *   Arguments:
3906e8fb11a1Sshidokht  *	cl		cmlb handle
3907e8fb11a1Sshidokht  *
3908e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
3909e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
3910e8fb11a1Sshidokht  *
39113ccda647Slclee  *
39123ccda647Slclee  * Return Code: the code returned by cmlb_send_scsi_cmd()
39133ccda647Slclee  *		0
39143ccda647Slclee  *		EINVAL
39153ccda647Slclee  *		ENXIO
39163ccda647Slclee  *		ENOMEM
39173ccda647Slclee  */
39183ccda647Slclee static int
3919e8fb11a1Sshidokht cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
39203ccda647Slclee {
39213ccda647Slclee 	struct dk_label	*dkl;
39223ccda647Slclee 	short		sum;
39233ccda647Slclee 	short		*sp;
39243ccda647Slclee 	int		i;
39253ccda647Slclee 	int		rval;
39263ccda647Slclee 
3927e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3928e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
39293ccda647Slclee 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
3930e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
39313ccda647Slclee 
3932e8fb11a1Sshidokht 	bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
3933e8fb11a1Sshidokht 	dkl->dkl_rpm	= cl->cl_g.dkg_rpm;
3934e8fb11a1Sshidokht 	dkl->dkl_pcyl	= cl->cl_g.dkg_pcyl;
3935e8fb11a1Sshidokht 	dkl->dkl_apc	= cl->cl_g.dkg_apc;
3936e8fb11a1Sshidokht 	dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
3937e8fb11a1Sshidokht 	dkl->dkl_ncyl	= cl->cl_g.dkg_ncyl;
3938e8fb11a1Sshidokht 	dkl->dkl_acyl	= cl->cl_g.dkg_acyl;
3939e8fb11a1Sshidokht 	dkl->dkl_nhead	= cl->cl_g.dkg_nhead;
3940e8fb11a1Sshidokht 	dkl->dkl_nsect	= cl->cl_g.dkg_nsect;
39413ccda647Slclee 
39423ccda647Slclee #if defined(_SUNOS_VTOC_8)
3943e8fb11a1Sshidokht 	dkl->dkl_obs1	= cl->cl_g.dkg_obs1;
3944e8fb11a1Sshidokht 	dkl->dkl_obs2	= cl->cl_g.dkg_obs2;
3945e8fb11a1Sshidokht 	dkl->dkl_obs3	= cl->cl_g.dkg_obs3;
39463ccda647Slclee 	for (i = 0; i < NDKMAP; i++) {
3947e8fb11a1Sshidokht 		dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3948e8fb11a1Sshidokht 		dkl->dkl_map[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
39493ccda647Slclee 	}
3950e8fb11a1Sshidokht 	bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
39513ccda647Slclee #elif defined(_SUNOS_VTOC_16)
3952e8fb11a1Sshidokht 	dkl->dkl_skew	= cl->cl_dkg_skew;
39533ccda647Slclee #else
39543ccda647Slclee #error "No VTOC format defined."
39553ccda647Slclee #endif
39563ccda647Slclee 
39573ccda647Slclee 	dkl->dkl_magic			= DKL_MAGIC;
3958e8fb11a1Sshidokht 	dkl->dkl_write_reinstruct	= cl->cl_g.dkg_write_reinstruct;
3959e8fb11a1Sshidokht 	dkl->dkl_read_reinstruct	= cl->cl_g.dkg_read_reinstruct;
39603ccda647Slclee 
39613ccda647Slclee 	/* Construct checksum for the new disk label */
39623ccda647Slclee 	sum = 0;
39633ccda647Slclee 	sp = (short *)dkl;
39643ccda647Slclee 	i = sizeof (struct dk_label) / sizeof (short);
39653ccda647Slclee 	while (i--) {
39663ccda647Slclee 		sum ^= *sp++;
39673ccda647Slclee 	}
39683ccda647Slclee 	dkl->dkl_cksum = sum;
39693ccda647Slclee 
3970e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
39713ccda647Slclee 
3972e8fb11a1Sshidokht 	rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
39733ccda647Slclee exit:
39743ccda647Slclee 	kmem_free(dkl, sizeof (struct dk_label));
3975e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
39763ccda647Slclee 	return (rval);
39773ccda647Slclee }
39783ccda647Slclee 
39793ccda647Slclee static int
3980e8fb11a1Sshidokht cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3981e8fb11a1Sshidokht     void *tg_cookie)
39823ccda647Slclee {
39833ccda647Slclee 	dk_efi_t	user_efi;
39843ccda647Slclee 	int		rval = 0;
39853ccda647Slclee 	void		*buffer;
3986e8fb11a1Sshidokht 	diskaddr_t	tgt_lba;
39873ccda647Slclee 
39883ccda647Slclee 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
39893ccda647Slclee 		return (EFAULT);
39903ccda647Slclee 
39913ccda647Slclee 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
39923ccda647Slclee 
39933ccda647Slclee 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
39943ccda647Slclee 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
39953ccda647Slclee 		rval = EFAULT;
39963ccda647Slclee 	} else {
39973ccda647Slclee 		/*
39983ccda647Slclee 		 * let's clear the vtoc labels and clear the softstate
39993ccda647Slclee 		 * vtoc.
40003ccda647Slclee 		 */
4001e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
4002e8fb11a1Sshidokht 		if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
4003e8fb11a1Sshidokht 			cmlb_dbg(CMLB_TRACE,  cl,
40043ccda647Slclee 			    "cmlb_dkio_set_efi: CLEAR VTOC\n");
4005e8fb11a1Sshidokht 			if (cl->cl_vtoc_label_is_from_media)
4006e8fb11a1Sshidokht 				cmlb_clear_vtoc(cl, tg_cookie);
4007e8fb11a1Sshidokht 			bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4008e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4009e8fb11a1Sshidokht 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4010e8fb11a1Sshidokht 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4011e8fb11a1Sshidokht 			(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd",
40123ccda647Slclee 			    S_IFBLK,
40133ccda647Slclee 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4014e8fb11a1Sshidokht 			    cl->cl_node_type, NULL);
4015e8fb11a1Sshidokht 			(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd,raw",
40163ccda647Slclee 			    S_IFCHR,
40173ccda647Slclee 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4018e8fb11a1Sshidokht 			    cl->cl_node_type, NULL);
40193ccda647Slclee 		} else
4020e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4021e8fb11a1Sshidokht 
4022e8fb11a1Sshidokht 		tgt_lba = user_efi.dki_lba;
4023e8fb11a1Sshidokht 
4024e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
4025e8fb11a1Sshidokht 		if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4026e8fb11a1Sshidokht 		    (cl->cl_tgt_blocksize == 0)) {
4027e8fb11a1Sshidokht 			kmem_free(buffer, user_efi.dki_length);
4028e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
4029e8fb11a1Sshidokht 			return (EINVAL);
4030e8fb11a1Sshidokht 		}
4031e8fb11a1Sshidokht 		if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4032e8fb11a1Sshidokht 			tgt_lba = tgt_lba *
4033e8fb11a1Sshidokht 			    cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
4034e8fb11a1Sshidokht 
4035e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
4036e8fb11a1Sshidokht 		rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
4037e8fb11a1Sshidokht 		    tg_cookie);
4038e8fb11a1Sshidokht 
40393ccda647Slclee 		if (rval == 0) {
4040e8fb11a1Sshidokht 			mutex_enter(CMLB_MUTEX(cl));
4041e8fb11a1Sshidokht 			cl->cl_f_geometry_is_valid = FALSE;
4042e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
40433ccda647Slclee 		}
40443ccda647Slclee 	}
40453ccda647Slclee 	kmem_free(buffer, user_efi.dki_length);
40463ccda647Slclee 	return (rval);
40473ccda647Slclee }
40483ccda647Slclee 
40493ccda647Slclee /*
40503ccda647Slclee  *    Function: cmlb_dkio_get_mboot
40513ccda647Slclee  *
40523ccda647Slclee  * Description: This routine is the driver entry point for handling user
40533ccda647Slclee  *		requests to get the current device mboot (DKIOCGMBOOT)
40543ccda647Slclee  *
40553ccda647Slclee  *   Arguments:
4056e8fb11a1Sshidokht  *	arg		pointer to user provided mboot structure specifying
40573ccda647Slclee  *			the current mboot.
4058e8fb11a1Sshidokht  *
4059e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
40603ccda647Slclee  *			directly from the mode argument of ioctl().
40613ccda647Slclee  *
4062e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4063e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4064e8fb11a1Sshidokht  *
40653ccda647Slclee  * Return Code: 0
40663ccda647Slclee  *		EINVAL
40673ccda647Slclee  *		EFAULT
40683ccda647Slclee  *		ENXIO
40693ccda647Slclee  */
40703ccda647Slclee static int
4071e8fb11a1Sshidokht cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
40723ccda647Slclee {
40733ccda647Slclee 	struct mboot	*mboot;
40743ccda647Slclee 	int		rval;
40753ccda647Slclee 	size_t		buffer_size;
40763ccda647Slclee 
40773ccda647Slclee 
40783ccda647Slclee #if defined(_SUNOS_VTOC_8)
4079e8fb11a1Sshidokht 	if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
40803ccda647Slclee #elif defined(_SUNOS_VTOC_16)
40813ccda647Slclee 	if (arg == NULL) {
40823ccda647Slclee #endif
40833ccda647Slclee 		return (EINVAL);
40843ccda647Slclee 	}
40853ccda647Slclee 
40863ccda647Slclee 	/*
40873ccda647Slclee 	 * Read the mboot block, located at absolute block 0 on the target.
40883ccda647Slclee 	 */
40893ccda647Slclee 	buffer_size = sizeof (struct mboot);
40903ccda647Slclee 
4091e8fb11a1Sshidokht 	cmlb_dbg(CMLB_TRACE,  cl,
40923ccda647Slclee 	    "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
40933ccda647Slclee 
40943ccda647Slclee 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
4095e8fb11a1Sshidokht 	if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
40963ccda647Slclee 		if (ddi_copyout(mboot, (void *)arg,
40973ccda647Slclee 		    sizeof (struct mboot), flag) != 0) {
40983ccda647Slclee 			rval = EFAULT;
40993ccda647Slclee 		}
41003ccda647Slclee 	}
41013ccda647Slclee 	kmem_free(mboot, buffer_size);
41023ccda647Slclee 	return (rval);
41033ccda647Slclee }
41043ccda647Slclee 
41053ccda647Slclee 
41063ccda647Slclee /*
41073ccda647Slclee  *    Function: cmlb_dkio_set_mboot
41083ccda647Slclee  *
41093ccda647Slclee  * Description: This routine is the driver entry point for handling user
41103ccda647Slclee  *		requests to validate and set the device master boot
41113ccda647Slclee  *		(DKIOCSMBOOT).
41123ccda647Slclee  *
41133ccda647Slclee  *   Arguments:
4114e8fb11a1Sshidokht  *	arg		pointer to user provided mboot structure used to set the
41153ccda647Slclee  *			master boot.
4116e8fb11a1Sshidokht  *
4117e8fb11a1Sshidokht  *	flag		this argument is a pass through to ddi_copyxxx()
41183ccda647Slclee  *			directly from the mode argument of ioctl().
41193ccda647Slclee  *
4120e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4121e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4122e8fb11a1Sshidokht  *
41233ccda647Slclee  * Return Code: 0
41243ccda647Slclee  *		EINVAL
41253ccda647Slclee  *		EFAULT
41263ccda647Slclee  *		ENXIO
41273ccda647Slclee  */
41283ccda647Slclee static int
4129e8fb11a1Sshidokht cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
41303ccda647Slclee {
41313ccda647Slclee 	struct mboot	*mboot = NULL;
41323ccda647Slclee 	int		rval;
41333ccda647Slclee 	ushort_t	magic;
41343ccda647Slclee 
41353ccda647Slclee 
4136e8fb11a1Sshidokht 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
41373ccda647Slclee 
41383ccda647Slclee #if defined(_SUNOS_VTOC_8)
4139e8fb11a1Sshidokht 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
41403ccda647Slclee 		return (EINVAL);
41413ccda647Slclee 	}
41423ccda647Slclee #endif
41433ccda647Slclee 
41443ccda647Slclee 	if (arg == NULL) {
41453ccda647Slclee 		return (EINVAL);
41463ccda647Slclee 	}
41473ccda647Slclee 
41483ccda647Slclee 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
41493ccda647Slclee 
41503ccda647Slclee 	if (ddi_copyin((const void *)arg, mboot,
41513ccda647Slclee 	    sizeof (struct mboot), flag) != 0) {
41523ccda647Slclee 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
41533ccda647Slclee 		return (EFAULT);
41543ccda647Slclee 	}
41553ccda647Slclee 
41563ccda647Slclee 	/* Is this really a master boot record? */
41573ccda647Slclee 	magic = LE_16(mboot->signature);
41583ccda647Slclee 	if (magic != MBB_MAGIC) {
41593ccda647Slclee 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
41603ccda647Slclee 		return (EINVAL);
41613ccda647Slclee 	}
41623ccda647Slclee 
4163e8fb11a1Sshidokht 	rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
41643ccda647Slclee 
4165e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
41663ccda647Slclee #if defined(__i386) || defined(__amd64)
41673ccda647Slclee 	if (rval == 0) {
41683ccda647Slclee 		/*
41693ccda647Slclee 		 * mboot has been written successfully.
41703ccda647Slclee 		 * update the fdisk and vtoc tables in memory
41713ccda647Slclee 		 */
4172e8fb11a1Sshidokht 		rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
4173e8fb11a1Sshidokht 		if ((cl->cl_f_geometry_is_valid == FALSE) || (rval != 0)) {
4174e8fb11a1Sshidokht 			mutex_exit(CMLB_MUTEX(cl));
41753ccda647Slclee 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
41763ccda647Slclee 			return (rval);
41773ccda647Slclee 		}
41783ccda647Slclee 	}
4179e8fb11a1Sshidokht 
4180e8fb11a1Sshidokht #ifdef __lock_lint
4181e8fb11a1Sshidokht 	cmlb_setup_default_geometry(cl, tg_cookie);
4182e8fb11a1Sshidokht #endif
4183e8fb11a1Sshidokht 
41843ccda647Slclee #else
41853ccda647Slclee 	if (rval == 0) {
41863ccda647Slclee 		/*
41873ccda647Slclee 		 * mboot has been written successfully.
41883ccda647Slclee 		 * set up the default geometry and VTOC
41893ccda647Slclee 		 */
4190e8fb11a1Sshidokht 		if (cl->cl_blockcount <= DK_MAX_BLOCKS)
4191e8fb11a1Sshidokht 			cmlb_setup_default_geometry(cl, tg_cookie);
41923ccda647Slclee 	}
41933ccda647Slclee #endif
4194e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
41953ccda647Slclee 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
41963ccda647Slclee 	return (rval);
41973ccda647Slclee }
41983ccda647Slclee 
41993ccda647Slclee 
42003ccda647Slclee /*
42013ccda647Slclee  *    Function: cmlb_setup_default_geometry
42023ccda647Slclee  *
42033ccda647Slclee  * Description: This local utility routine sets the default geometry as part of
42043ccda647Slclee  *		setting the device mboot.
42053ccda647Slclee  *
4206e8fb11a1Sshidokht  *   Arguments:
4207e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4208e8fb11a1Sshidokht  *
4209e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4210e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4211e8fb11a1Sshidokht  *
42123ccda647Slclee  *
42133ccda647Slclee  * Note: This may be redundant with cmlb_build_default_label.
42143ccda647Slclee  */
42153ccda647Slclee static void
4216e8fb11a1Sshidokht cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
42173ccda647Slclee {
42183ccda647Slclee 	struct cmlb_geom	pgeom;
42193ccda647Slclee 	struct cmlb_geom	*pgeomp = &pgeom;
42203ccda647Slclee 	int			ret;
42213ccda647Slclee 	int			geom_base_cap = 1;
42223ccda647Slclee 
42233ccda647Slclee 
4224e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
42253ccda647Slclee 
42263ccda647Slclee 	/* zero out the soft state geometry and partition table. */
4227e8fb11a1Sshidokht 	bzero(&cl->cl_g, sizeof (struct dk_geom));
4228e8fb11a1Sshidokht 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4229e8fb11a1Sshidokht 	bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
42303ccda647Slclee 
42313ccda647Slclee 	/*
42323ccda647Slclee 	 * For the rpm, we use the minimum for the disk.
42333ccda647Slclee 	 * For the head, cyl and number of sector per track,
42343ccda647Slclee 	 * if the capacity <= 1GB, head = 64, sect = 32.
42353ccda647Slclee 	 * else head = 255, sect 63
42363ccda647Slclee 	 * Note: the capacity should be equal to C*H*S values.
42373ccda647Slclee 	 * This will cause some truncation of size due to
42383ccda647Slclee 	 * round off errors. For CD-ROMs, this truncation can
42393ccda647Slclee 	 * have adverse side effects, so returning ncyl and
42403ccda647Slclee 	 * nhead as 1. The nsect will overflow for most of
42413ccda647Slclee 	 * CD-ROMs as nsect is of type ushort.
42423ccda647Slclee 	 */
4243e8fb11a1Sshidokht 	if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
42443ccda647Slclee 		/*
42453ccda647Slclee 		 * newfs currently can not handle 255 ntracks for SPARC
42463ccda647Slclee 		 * so get the geometry from target driver instead of coming up
42473ccda647Slclee 		 * with one based on capacity.
42483ccda647Slclee 		 */
4249e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
4250e8fb11a1Sshidokht 		ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
4251e8fb11a1Sshidokht 		mutex_enter(CMLB_MUTEX(cl));
42523ccda647Slclee 
42533ccda647Slclee 		if (ret  == 0) {
42543ccda647Slclee 			geom_base_cap = 0;
42553ccda647Slclee 		} else {
4256e8fb11a1Sshidokht 			cmlb_dbg(CMLB_ERROR,  cl,
42573ccda647Slclee 			    "cmlb_setup_default_geometry: "
42583ccda647Slclee 			    "tg_getphygeom failed %d\n", ret);
42593ccda647Slclee 
42603ccda647Slclee 			/* do default setting, geometry based on capacity */
42613ccda647Slclee 		}
42623ccda647Slclee 	}
42633ccda647Slclee 
42643ccda647Slclee 	if (geom_base_cap) {
4265e8fb11a1Sshidokht 		if (ISCD(cl)) {
4266e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 1;
4267e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 1;
4268e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
4269e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x1000) {
42703ccda647Slclee 			/* Needed for unlabeled SCSI floppies. */
4271e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 2;
4272e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = 80;
4273e8fb11a1Sshidokht 			cl->cl_g.dkg_pcyl = 80;
4274e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
4275e8fb11a1Sshidokht 		} else if (cl->cl_blockcount <= 0x200000) {
4276e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 64;
4277e8fb11a1Sshidokht 			cl->cl_g.dkg_nsect = 32;
4278e8fb11a1Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
42793ccda647Slclee 		} else {
4280e8fb11a1Sshidokht 			cl->cl_g.dkg_nhead = 255;
4281*98a65dd4Sshidokht 
4282*98a65dd4Sshidokht 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
4283*98a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63) - 1) /
4284*98a65dd4Sshidokht 			    (UINT16_MAX * 255 * 63)) * 63;
4285*98a65dd4Sshidokht 
4286*98a65dd4Sshidokht 			if (cl->cl_g.dkg_nsect == 0)
4287*98a65dd4Sshidokht 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
4288*98a65dd4Sshidokht 
4289*98a65dd4Sshidokht 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
4290*98a65dd4Sshidokht 			    (255 * cl->cl_g.dkg_nsect);
42913ccda647Slclee 		}
42923ccda647Slclee 
4293e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = 0;
4294e8fb11a1Sshidokht 		cl->cl_g.dkg_bcyl = 0;
4295e8fb11a1Sshidokht 		cl->cl_g.dkg_intrlv = 1;
4296e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm = 200;
4297e8fb11a1Sshidokht 		if (cl->cl_g.dkg_pcyl == 0)
4298e8fb11a1Sshidokht 			cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
4299e8fb11a1Sshidokht 			    cl->cl_g.dkg_acyl;
43003ccda647Slclee 	} else {
4301e8fb11a1Sshidokht 		cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
4302e8fb11a1Sshidokht 		cl->cl_g.dkg_acyl = pgeomp->g_acyl;
4303e8fb11a1Sshidokht 		cl->cl_g.dkg_nhead = pgeomp->g_nhead;
4304e8fb11a1Sshidokht 		cl->cl_g.dkg_nsect = pgeomp->g_nsect;
4305e8fb11a1Sshidokht 		cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
4306e8fb11a1Sshidokht 		cl->cl_g.dkg_rpm = pgeomp->g_rpm;
4307e8fb11a1Sshidokht 		cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
43083ccda647Slclee 	}
43093ccda647Slclee 
4310e8fb11a1Sshidokht 	cl->cl_g.dkg_read_reinstruct = 0;
4311e8fb11a1Sshidokht 	cl->cl_g.dkg_write_reinstruct = 0;
4312e8fb11a1Sshidokht 	cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
4313e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
43143ccda647Slclee 
4315e8fb11a1Sshidokht 	cl->cl_map['a'-'a'].dkl_cylno = 0;
4316e8fb11a1Sshidokht 	cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
43173ccda647Slclee 
4318e8fb11a1Sshidokht 	cl->cl_map['c'-'a'].dkl_cylno = 0;
4319e8fb11a1Sshidokht 	cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
43203ccda647Slclee 
4321e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
4322e8fb11a1Sshidokht 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
4323e8fb11a1Sshidokht 	cl->cl_vtoc.v_nparts = V_NUMPAR;
4324e8fb11a1Sshidokht 	cl->cl_vtoc.v_version = V_VERSION;
4325e8fb11a1Sshidokht 	(void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
4326e8fb11a1Sshidokht 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
4327e8fb11a1Sshidokht 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
43283ccda647Slclee 
4329e8fb11a1Sshidokht 	cl->cl_f_geometry_is_valid = FALSE;
43303ccda647Slclee }
43313ccda647Slclee 
43323ccda647Slclee 
43333ccda647Slclee #if defined(__i386) || defined(__amd64)
43343ccda647Slclee /*
43353ccda647Slclee  *    Function: cmlb_update_fdisk_and_vtoc
43363ccda647Slclee  *
43373ccda647Slclee  * Description: This local utility routine updates the device fdisk and vtoc
43383ccda647Slclee  *		as part of setting the device mboot.
43393ccda647Slclee  *
4340e8fb11a1Sshidokht  *   Arguments:
4341e8fb11a1Sshidokht  *	cl		driver soft state (unit) structure
4342e8fb11a1Sshidokht  *
4343e8fb11a1Sshidokht  *	tg_cookie	cookie from target driver to be passed back to target
4344e8fb11a1Sshidokht  *			driver when we call back to it through tg_ops.
4345e8fb11a1Sshidokht  *
43463ccda647Slclee  *
43473ccda647Slclee  * Return Code: 0 for success or errno-type return code.
43483ccda647Slclee  *
43493ccda647Slclee  *    Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
43503ccda647Slclee  *		these did exist separately in x86 sd.c.
43513ccda647Slclee  */
43523ccda647Slclee static int
4353e8fb11a1Sshidokht cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
43543ccda647Slclee {
43553ccda647Slclee 	int		count;
43563ccda647Slclee 	int		label_rc = 0;
43573ccda647Slclee 	int		fdisk_rval;
43583ccda647Slclee 	diskaddr_t	capacity;
43593ccda647Slclee 
4360e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
43613ccda647Slclee 
4362e8fb11a1Sshidokht 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
43633ccda647Slclee 		return (EINVAL);
43643ccda647Slclee 
43653ccda647Slclee #if defined(_SUNOS_VTOC_16)
43663ccda647Slclee 	/*
43673ccda647Slclee 	 * Set up the "whole disk" fdisk partition; this should always
43683ccda647Slclee 	 * exist, regardless of whether the disk contains an fdisk table
43693ccda647Slclee 	 * or vtoc.
43703ccda647Slclee 	 */
4371e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
4372e8fb11a1Sshidokht 	cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
43733ccda647Slclee #endif	/* defined(_SUNOS_VTOC_16) */
43743ccda647Slclee 
43753ccda647Slclee 	/*
43763ccda647Slclee 	 * copy the lbasize and capacity so that if they're
4377e8fb11a1Sshidokht 	 * reset while we're not holding the CMLB_MUTEX(cl), we will
4378e8fb11a1Sshidokht 	 * continue to use valid values after the CMLB_MUTEX(cl) is
43793ccda647Slclee 	 * reacquired.
43803ccda647Slclee 	 */
4381e8fb11a1Sshidokht 	capacity = cl->cl_blockcount;
43823ccda647Slclee 
43833ccda647Slclee 	/*
43843ccda647Slclee 	 * refresh the logical and physical geometry caches.
43853ccda647Slclee 	 * (data from mode sense format/rigid disk geometry pages,
43863ccda647Slclee 	 * and scsi_ifgetcap("geometry").
43873ccda647Slclee 	 */
4388e8fb11a1Sshidokht 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
43893ccda647Slclee 
43903ccda647Slclee 	/*
4391e8fb11a1Sshidokht 	 * Only DIRECT ACCESS devices will have Scl labels.
4392e8fb11a1Sshidokht 	 * CD's supposedly have a Scl label, too
43933ccda647Slclee 	 */
4394e8fb11a1Sshidokht 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
4395e8fb11a1Sshidokht 		fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
43963ccda647Slclee 		if (fdisk_rval != 0) {
4397e8fb11a1Sshidokht 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
43983ccda647Slclee 			return (fdisk_rval);
43993ccda647Slclee 		}
44003ccda647Slclee 
4401e8fb11a1Sshidokht 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
44023ccda647Slclee 			/*
44033ccda647Slclee 			 * Found fdisk table but no Solaris partition entry,
44043ccda647Slclee 			 * so don't call cmlb_uselabel() and don't create
44053ccda647Slclee 			 * a default label.
44063ccda647Slclee 			 */
44073ccda647Slclee 			label_rc = 0;
4408e8fb11a1Sshidokht 			cl->cl_f_geometry_is_valid = TRUE;
44093ccda647Slclee 			goto no_solaris_partition;
44103ccda647Slclee 		}
44113ccda647Slclee 	} else if (capacity < 0) {
4412e8fb11a1Sshidokht 		ASSERT(mutex_owned(CMLB_MUTEX(cl)));
44133ccda647Slclee 		return (EINVAL);
44143ccda647Slclee 	}
44153ccda647Slclee 
44163ccda647Slclee 	/*
44173ccda647Slclee 	 * For Removable media We reach here if we have found a
44183ccda647Slclee 	 * SOLARIS PARTITION.
4419e8fb11a1Sshidokht 	 * If cl_f_geometry_is_valid is FALSE it indicates that the SOLARIS
44203ccda647Slclee 	 * PARTITION has changed from the previous one, hence we will setup a
44213ccda647Slclee 	 * default VTOC in this case.
44223ccda647Slclee 	 */
4423e8fb11a1Sshidokht 	if (cl->cl_f_geometry_is_valid == FALSE) {
44243ccda647Slclee 		/* if we get here it is writable */
44253ccda647Slclee 		/* we are called from SMBOOT, and after a write of fdisk */
4426e8fb11a1Sshidokht 		cmlb_build_default_label(cl, tg_cookie);
44273ccda647Slclee 		label_rc = 0;
44283ccda647Slclee 	}
44293ccda647Slclee 
44303ccda647Slclee no_solaris_partition:
44313ccda647Slclee 
44323ccda647Slclee #if defined(_SUNOS_VTOC_16)
44333ccda647Slclee 	/*
44343ccda647Slclee 	 * If we have valid geometry, set up the remaining fdisk partitions.
44353ccda647Slclee 	 * Note that dkl_cylno is not used for the fdisk map entries, so
44363ccda647Slclee 	 * we set it to an entirely bogus value.
44373ccda647Slclee 	 */
44383ccda647Slclee 	for (count = 0; count < FD_NUMPART; count++) {
4439e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_cylno = -1;
4440e8fb11a1Sshidokht 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
4441e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_nblk;
4442e8fb11a1Sshidokht 		cl->cl_offset[FDISK_P1 + count] =
4443e8fb11a1Sshidokht 		    cl->cl_fmap[count].fmap_start;
44443ccda647Slclee 	}
44453ccda647Slclee #endif
44463ccda647Slclee 
44473ccda647Slclee 	for (count = 0; count < NDKMAP; count++) {
44483ccda647Slclee #if defined(_SUNOS_VTOC_8)
4449e8fb11a1Sshidokht 		struct dk_map *lp  = &cl->cl_map[count];
4450e8fb11a1Sshidokht 		cl->cl_offset[count] =
4451e8fb11a1Sshidokht 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
44523ccda647Slclee #elif defined(_SUNOS_VTOC_16)
4453e8fb11a1Sshidokht 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
4454e8fb11a1Sshidokht 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
44553ccda647Slclee #else
44563ccda647Slclee #error "No VTOC format defined."
44573ccda647Slclee #endif
44583ccda647Slclee 	}
44593ccda647Slclee 
4460e8fb11a1Sshidokht 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
44613ccda647Slclee 	return (label_rc);
44623ccda647Slclee }
44633ccda647Slclee #endif
44643ccda647Slclee 
44653ccda647Slclee #if defined(__i386) || defined(__amd64)
44663ccda647Slclee static int
4467e8fb11a1Sshidokht cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
44683ccda647Slclee {
44693ccda647Slclee 	int err = 0;
44703ccda647Slclee 
44713ccda647Slclee 	/* Return the driver's notion of the media's logical geometry */
44723ccda647Slclee 	struct dk_geom	disk_geom;
44733ccda647Slclee 	struct dk_geom	*dkgp = &disk_geom;
44743ccda647Slclee 
4475e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
44763ccda647Slclee 	/*
44773ccda647Slclee 	 * If there is no HBA geometry available, or
44783ccda647Slclee 	 * if the HBA returned us something that doesn't
44793ccda647Slclee 	 * really fit into an Int 13/function 8 geometry
44803ccda647Slclee 	 * result, just fail the ioctl.  See PSARC 1998/313.
44813ccda647Slclee 	 */
4482e8fb11a1Sshidokht 	if (cl->cl_lgeom.g_nhead == 0 ||
4483e8fb11a1Sshidokht 	    cl->cl_lgeom.g_nsect == 0 ||
4484e8fb11a1Sshidokht 	    cl->cl_lgeom.g_ncyl > 1024) {
4485e8fb11a1Sshidokht 		mutex_exit(CMLB_MUTEX(cl));
44863ccda647Slclee 		err = EINVAL;
44873ccda647Slclee 	} else {
4488e8fb11a1Sshidokht 		dkgp->dkg_ncyl	= cl->cl_lgeom.g_ncyl;
4489e8fb11a1Sshidokht 		dkgp->dkg_acyl	= cl->cl_lgeom.g_acyl;
44903ccda647Slclee 		dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
4491e8fb11a1Sshidokht 		dkgp->dkg_nhead	= cl->cl_lgeom.g_nhead;
4492e8fb11a1Sshidokht 		dkgp->dkg_nsect	= cl->cl_lgeom.g_nsect;
44933ccda647Slclee 
44941c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
44953ccda647Slclee 		if (ddi_copyout(dkgp, (void *)arg,
44963ccda647Slclee 		    sizeof (struct dk_geom), flag)) {
44973ccda647Slclee 			err = EFAULT;
44983ccda647Slclee 		} else {
44993ccda647Slclee 			err = 0;
45003ccda647Slclee 		}
45013ccda647Slclee 	}
45023ccda647Slclee 	return (err);
45033ccda647Slclee }
45043ccda647Slclee #endif
45053ccda647Slclee 
45063ccda647Slclee #if defined(__i386) || defined(__amd64)
45073ccda647Slclee static int
4508e8fb11a1Sshidokht cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag)
45093ccda647Slclee {
45103ccda647Slclee 	int err = 0;
4511e8fb11a1Sshidokht 	diskaddr_t capacity;
45123ccda647Slclee 
45133ccda647Slclee 
45143ccda647Slclee 	/* Return the driver's notion of the media physical geometry */
45153ccda647Slclee 	struct dk_geom	disk_geom;
45163ccda647Slclee 	struct dk_geom	*dkgp = &disk_geom;
45173ccda647Slclee 
4518e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
45193ccda647Slclee 
4520e8fb11a1Sshidokht 	if (cl->cl_g.dkg_nhead != 0 &&
4521e8fb11a1Sshidokht 	    cl->cl_g.dkg_nsect != 0) {
45223ccda647Slclee 		/*
45233ccda647Slclee 		 * We succeeded in getting a geometry, but
45243ccda647Slclee 		 * right now it is being reported as just the
45253ccda647Slclee 		 * Solaris fdisk partition, just like for
45263ccda647Slclee 		 * DKIOCGGEOM. We need to change that to be
45273ccda647Slclee 		 * correct for the entire disk now.
45283ccda647Slclee 		 */
4529e8fb11a1Sshidokht 		bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
45303ccda647Slclee 		dkgp->dkg_acyl = 0;
4531e8fb11a1Sshidokht 		dkgp->dkg_ncyl = cl->cl_blockcount /
45323ccda647Slclee 		    (dkgp->dkg_nhead * dkgp->dkg_nsect);
45333ccda647Slclee 	} else {
45343ccda647Slclee 		bzero(dkgp, sizeof (struct dk_geom));
45353ccda647Slclee 		/*
45363ccda647Slclee 		 * This disk does not have a Solaris VTOC
45373ccda647Slclee 		 * so we must present a physical geometry
45383ccda647Slclee 		 * that will remain consistent regardless
45393ccda647Slclee 		 * of how the disk is used. This will ensure
45403ccda647Slclee 		 * that the geometry does not change regardless
45413ccda647Slclee 		 * of the fdisk partition type (ie. EFI, FAT32,
45423ccda647Slclee 		 * Solaris, etc).
45433ccda647Slclee 		 */
4544e8fb11a1Sshidokht 		if (ISCD(cl)) {
4545e8fb11a1Sshidokht 			dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
4546e8fb11a1Sshidokht 			dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
4547e8fb11a1Sshidokht 			dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
4548e8fb11a1Sshidokht 			dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
45493ccda647Slclee 		} else {
4550e8fb11a1Sshidokht 			/*
4551e8fb11a1Sshidokht 			 * Invalid cl_blockcount can generate invalid
4552e8fb11a1Sshidokht 			 * dk_geom and may result in division by zero
4553e8fb11a1Sshidokht 			 * system failure. Should make sure blockcount
4554e8fb11a1Sshidokht 			 * is valid before using it here.
4555e8fb11a1Sshidokht 			 */
4556e8fb11a1Sshidokht 			if (cl->cl_blockcount == 0) {
4557e8fb11a1Sshidokht 				mutex_exit(CMLB_MUTEX(cl));
4558e8fb11a1Sshidokht 				err = EIO;
4559e8fb11a1Sshidokht 				return (err);
4560e8fb11a1Sshidokht 			}
4561e8fb11a1Sshidokht 			/*
4562e8fb11a1Sshidokht 			 * Refer to comments related to off-by-1 at the
4563e8fb11a1Sshidokht 			 * header of this file
4564e8fb11a1Sshidokht 			 */
4565e8fb11a1Sshidokht 			if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
4566e8fb11a1Sshidokht 				capacity = cl->cl_blockcount - 1;
4567e8fb11a1Sshidokht 			else
4568e8fb11a1Sshidokht 				capacity = cl->cl_blockcount;
4569e8fb11a1Sshidokht 
4570e8fb11a1Sshidokht 			cmlb_convert_geometry(capacity, dkgp);
45713ccda647Slclee 			dkgp->dkg_acyl = 0;
4572e8fb11a1Sshidokht 			dkgp->dkg_ncyl = capacity /
45733ccda647Slclee 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
45743ccda647Slclee 		}
45753ccda647Slclee 	}
45763ccda647Slclee 	dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
45773ccda647Slclee 
4578e8fb11a1Sshidokht 	mutex_exit(CMLB_MUTEX(cl));
45791c97599bSshidokht 	if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
45803ccda647Slclee 		err = EFAULT;
45811c97599bSshidokht 
45823ccda647Slclee 	return (err);
45833ccda647Slclee }
45843ccda647Slclee #endif
45853ccda647Slclee 
45863ccda647Slclee #if defined(__i386) || defined(__amd64)
45873ccda647Slclee static int
4588e8fb11a1Sshidokht cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
45893ccda647Slclee {
45903ccda647Slclee 	int err = 0;
45913ccda647Slclee 
45923ccda647Slclee 	/*
45933ccda647Slclee 	 * Return parameters describing the selected disk slice.
45943ccda647Slclee 	 * Note: this ioctl is for the intel platform only
45953ccda647Slclee 	 */
45963ccda647Slclee 	int part;
45973ccda647Slclee 
45983ccda647Slclee 	part = CMLBPART(dev);
45993ccda647Slclee 
4600e8fb11a1Sshidokht 	mutex_enter(CMLB_MUTEX(cl));
4601e8fb11a1Sshidokht 	/* don't check cl_solaris_size for pN */
4602e8fb11a1Sshidokht 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
46033ccda647Slclee 		err = EIO;
46041c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
46053ccda647Slclee 	} else {
46063ccda647Slclee 		struct part_info p;
46073ccda647Slclee 
4608e8fb11a1Sshidokht 		p.p_start = (daddr_t)cl->cl_offset[part];
4609e8fb11a1Sshidokht 		p.p_length = (int)cl->cl_map[part].dkl_nblk;
46101c97599bSshidokht 		mutex_exit(CMLB_MUTEX(cl));
46113ccda647Slclee #ifdef _MULTI_DATAMODEL
46123ccda647Slclee 		switch (ddi_model_convert_from(flag & FMODELS)) {
46133ccda647Slclee 		case DDI_MODEL_ILP32:
46143ccda647Slclee 		{
46153ccda647Slclee 			struct part_info32 p32;
46163ccda647Slclee 
46173ccda647Slclee 			p32.p_start = (daddr32_t)p.p_start;
46183ccda647Slclee 			p32.p_length = p.p_length;
46193ccda647Slclee 			if (ddi_copyout(&p32, (void *)arg,
46203ccda647Slclee 			    sizeof (p32), flag))
46213ccda647Slclee 				err = EFAULT;
46223ccda647Slclee 			break;
46233ccda647Slclee 		}
46243ccda647Slclee 
46253ccda647Slclee 		case DDI_MODEL_NONE:
46263ccda647Slclee 		{
46273ccda647Slclee 			if (ddi_copyout(&p, (void *)arg, sizeof (p),
46283ccda647Slclee 			    flag))
46293ccda647Slclee 				err = EFAULT;
46303ccda647Slclee 			break;
46313ccda647Slclee 		}
46323ccda647Slclee 		}
46333ccda647Slclee #else /* ! _MULTI_DATAMODEL */
46343ccda647Slclee 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
46353ccda647Slclee 			err = EFAULT;
46363ccda647Slclee #endif /* _MULTI_DATAMODEL */
46373ccda647Slclee 	}
46383ccda647Slclee 	return (err);
46393ccda647Slclee }
46403ccda647Slclee #endif
4641