xref: /titanic_51/usr/src/uts/common/io/cmlb.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This module provides support for labeling operations for target
29  * drivers.
30  */
31 
32 #include <sys/scsi/scsi.h>
33 #include <sys/sunddi.h>
34 #include <sys/dklabel.h>
35 #include <sys/dkio.h>
36 #include <sys/vtoc.h>
37 #include <sys/dktp/fdisk.h>
38 #include <sys/vtrace.h>
39 #include <sys/efi_partition.h>
40 #include <sys/cmlb.h>
41 #include <sys/cmlb_impl.h>
42 #if defined(__i386) || defined(__amd64)
43 #include <sys/fs/dv_node.h>
44 #endif
45 #include <sys/ddi_impldefs.h>
46 
47 /*
48  * Driver minor node structure and data table
49  */
50 struct driver_minor_data {
51 	char	*name;
52 	minor_t	minor;
53 	int	type;
54 };
55 
56 static struct driver_minor_data dk_minor_data[] = {
57 	{"a", 0, S_IFBLK},
58 	{"b", 1, S_IFBLK},
59 	{"c", 2, S_IFBLK},
60 	{"d", 3, S_IFBLK},
61 	{"e", 4, S_IFBLK},
62 	{"f", 5, S_IFBLK},
63 	{"g", 6, S_IFBLK},
64 	{"h", 7, S_IFBLK},
65 #if defined(_SUNOS_VTOC_16)
66 	{"i", 8, S_IFBLK},
67 	{"j", 9, S_IFBLK},
68 	{"k", 10, S_IFBLK},
69 	{"l", 11, S_IFBLK},
70 	{"m", 12, S_IFBLK},
71 	{"n", 13, S_IFBLK},
72 	{"o", 14, S_IFBLK},
73 	{"p", 15, S_IFBLK},
74 #endif			/* defined(_SUNOS_VTOC_16) */
75 #if defined(_FIRMWARE_NEEDS_FDISK)
76 	{"q", 16, S_IFBLK},
77 	{"r", 17, S_IFBLK},
78 	{"s", 18, S_IFBLK},
79 	{"t", 19, S_IFBLK},
80 	{"u", 20, S_IFBLK},
81 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
82 	{"a,raw", 0, S_IFCHR},
83 	{"b,raw", 1, S_IFCHR},
84 	{"c,raw", 2, S_IFCHR},
85 	{"d,raw", 3, S_IFCHR},
86 	{"e,raw", 4, S_IFCHR},
87 	{"f,raw", 5, S_IFCHR},
88 	{"g,raw", 6, S_IFCHR},
89 	{"h,raw", 7, S_IFCHR},
90 #if defined(_SUNOS_VTOC_16)
91 	{"i,raw", 8, S_IFCHR},
92 	{"j,raw", 9, S_IFCHR},
93 	{"k,raw", 10, S_IFCHR},
94 	{"l,raw", 11, S_IFCHR},
95 	{"m,raw", 12, S_IFCHR},
96 	{"n,raw", 13, S_IFCHR},
97 	{"o,raw", 14, S_IFCHR},
98 	{"p,raw", 15, S_IFCHR},
99 #endif			/* defined(_SUNOS_VTOC_16) */
100 #if defined(_FIRMWARE_NEEDS_FDISK)
101 	{"q,raw", 16, S_IFCHR},
102 	{"r,raw", 17, S_IFCHR},
103 	{"s,raw", 18, S_IFCHR},
104 	{"t,raw", 19, S_IFCHR},
105 	{"u,raw", 20, S_IFCHR},
106 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
107 	{0}
108 };
109 
110 #if defined(__i386) || defined(__amd64)
111 #if defined(_FIRMWARE_NEEDS_FDISK)
112 static struct driver_minor_data dk_ext_minor_data[] = {
113 	{"p5", 21, S_IFBLK},
114 	{"p6", 22, S_IFBLK},
115 	{"p7", 23, S_IFBLK},
116 	{"p8", 24, S_IFBLK},
117 	{"p9", 25, S_IFBLK},
118 	{"p10", 26, S_IFBLK},
119 	{"p11", 27, S_IFBLK},
120 	{"p12", 28, S_IFBLK},
121 	{"p13", 29, S_IFBLK},
122 	{"p14", 30, S_IFBLK},
123 	{"p15", 31, S_IFBLK},
124 	{"p16", 32, S_IFBLK},
125 	{"p17", 33, S_IFBLK},
126 	{"p18", 34, S_IFBLK},
127 	{"p19", 35, S_IFBLK},
128 	{"p20", 36, S_IFBLK},
129 	{"p21", 37, S_IFBLK},
130 	{"p22", 38, S_IFBLK},
131 	{"p23", 39, S_IFBLK},
132 	{"p24", 40, S_IFBLK},
133 	{"p25", 41, S_IFBLK},
134 	{"p26", 42, S_IFBLK},
135 	{"p27", 43, S_IFBLK},
136 	{"p28", 44, S_IFBLK},
137 	{"p29", 45, S_IFBLK},
138 	{"p30", 46, S_IFBLK},
139 	{"p31", 47, S_IFBLK},
140 	{"p32", 48, S_IFBLK},
141 	{"p33", 49, S_IFBLK},
142 	{"p34", 50, S_IFBLK},
143 	{"p35", 51, S_IFBLK},
144 	{"p36", 52, S_IFBLK},
145 	{"p5,raw", 21, S_IFCHR},
146 	{"p6,raw", 22, S_IFCHR},
147 	{"p7,raw", 23, S_IFCHR},
148 	{"p8,raw", 24, S_IFCHR},
149 	{"p9,raw", 25, S_IFCHR},
150 	{"p10,raw", 26, S_IFCHR},
151 	{"p11,raw", 27, S_IFCHR},
152 	{"p12,raw", 28, S_IFCHR},
153 	{"p13,raw", 29, S_IFCHR},
154 	{"p14,raw", 30, S_IFCHR},
155 	{"p15,raw", 31, S_IFCHR},
156 	{"p16,raw", 32, S_IFCHR},
157 	{"p17,raw", 33, S_IFCHR},
158 	{"p18,raw", 34, S_IFCHR},
159 	{"p19,raw", 35, S_IFCHR},
160 	{"p20,raw", 36, S_IFCHR},
161 	{"p21,raw", 37, S_IFCHR},
162 	{"p22,raw", 38, S_IFCHR},
163 	{"p23,raw", 39, S_IFCHR},
164 	{"p24,raw", 40, S_IFCHR},
165 	{"p25,raw", 41, S_IFCHR},
166 	{"p26,raw", 42, S_IFCHR},
167 	{"p27,raw", 43, S_IFCHR},
168 	{"p28,raw", 44, S_IFCHR},
169 	{"p29,raw", 45, S_IFCHR},
170 	{"p30,raw", 46, S_IFCHR},
171 	{"p31,raw", 47, S_IFCHR},
172 	{"p32,raw", 48, S_IFCHR},
173 	{"p33,raw", 49, S_IFCHR},
174 	{"p34,raw", 50, S_IFCHR},
175 	{"p35,raw", 51, S_IFCHR},
176 	{"p36,raw", 52, S_IFCHR},
177 	{0}
178 };
179 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
180 #endif			/* if defined(__i386) || defined(__amd64) */
181 
182 static struct driver_minor_data dk_minor_data_efi[] = {
183 	{"a", 0, S_IFBLK},
184 	{"b", 1, S_IFBLK},
185 	{"c", 2, S_IFBLK},
186 	{"d", 3, S_IFBLK},
187 	{"e", 4, S_IFBLK},
188 	{"f", 5, S_IFBLK},
189 	{"g", 6, S_IFBLK},
190 	{"wd", 7, S_IFBLK},
191 #if defined(_SUNOS_VTOC_16)
192 	{"i", 8, S_IFBLK},
193 	{"j", 9, S_IFBLK},
194 	{"k", 10, S_IFBLK},
195 	{"l", 11, S_IFBLK},
196 	{"m", 12, S_IFBLK},
197 	{"n", 13, S_IFBLK},
198 	{"o", 14, S_IFBLK},
199 	{"p", 15, S_IFBLK},
200 #endif			/* defined(_SUNOS_VTOC_16) */
201 #if defined(_FIRMWARE_NEEDS_FDISK)
202 	{"q", 16, S_IFBLK},
203 	{"r", 17, S_IFBLK},
204 	{"s", 18, S_IFBLK},
205 	{"t", 19, S_IFBLK},
206 	{"u", 20, S_IFBLK},
207 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
208 	{"a,raw", 0, S_IFCHR},
209 	{"b,raw", 1, S_IFCHR},
210 	{"c,raw", 2, S_IFCHR},
211 	{"d,raw", 3, S_IFCHR},
212 	{"e,raw", 4, S_IFCHR},
213 	{"f,raw", 5, S_IFCHR},
214 	{"g,raw", 6, S_IFCHR},
215 	{"wd,raw", 7, S_IFCHR},
216 #if defined(_SUNOS_VTOC_16)
217 	{"i,raw", 8, S_IFCHR},
218 	{"j,raw", 9, S_IFCHR},
219 	{"k,raw", 10, S_IFCHR},
220 	{"l,raw", 11, S_IFCHR},
221 	{"m,raw", 12, S_IFCHR},
222 	{"n,raw", 13, S_IFCHR},
223 	{"o,raw", 14, S_IFCHR},
224 	{"p,raw", 15, S_IFCHR},
225 #endif			/* defined(_SUNOS_VTOC_16) */
226 #if defined(_FIRMWARE_NEEDS_FDISK)
227 	{"q,raw", 16, S_IFCHR},
228 	{"r,raw", 17, S_IFCHR},
229 	{"s,raw", 18, S_IFCHR},
230 	{"t,raw", 19, S_IFCHR},
231 	{"u,raw", 20, S_IFCHR},
232 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
233 	{0}
234 };
235 
236 /*
237  * Declare the dynamic properties implemented in prop_op(9E) implementation
238  * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
239  * of drivers that call cmlb_attach().
240  */
241 static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
242 	{"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
243 	{"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
244 	{"device-nblocks",	DDI_PROP_TYPE_INT64},
245 	{"device-blksize",	DDI_PROP_TYPE_INT},
246 	{NULL}
247 };
248 
249 /*
250  * This implies an upper limit of 8192 GPT partitions
251  * in one transfer for GUID Partition Entry Array.
252  */
253 len_t cmlb_tg_max_efi_xfer = 1024 * 1024;
254 
255 /*
256  * External kernel interfaces
257  */
258 extern struct mod_ops mod_miscops;
259 
260 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
261     int spec_type, minor_t minor_num);
262 
263 /*
264  * Global buffer and mutex for debug logging
265  */
266 static char	cmlb_log_buffer[1024];
267 static kmutex_t	cmlb_log_mutex;
268 
269 
270 struct cmlb_lun *cmlb_debug_cl = NULL;
271 uint_t cmlb_level_mask = 0x0;
272 
273 int cmlb_rot_delay = 4;	/* default rotational delay */
274 
275 static struct modlmisc modlmisc = {
276 	&mod_miscops,   /* Type of module */
277 	"Common Labeling module"
278 };
279 
280 static struct modlinkage modlinkage = {
281 	MODREV_1, (void *)&modlmisc, NULL
282 };
283 
284 /* Local function prototypes */
285 static dev_t cmlb_make_device(struct cmlb_lun *cl);
286 static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
287     int flags, void *tg_cookie);
288 static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
289     void *tg_cookie);
290 static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
291     void *tg_cookie);
292 static void cmlb_swap_efi_gpt(efi_gpt_t *e);
293 static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
294 static int cmlb_validate_efi(efi_gpt_t *labp);
295 static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
296     void *tg_cookie);
297 static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
298 static int  cmlb_uselabel(struct cmlb_lun *cl,  struct dk_label *l, int flags);
299 #if defined(_SUNOS_VTOC_8)
300 static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
301 #endif
302 static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
303 static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
304 static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
305     void *tg_cookie);
306 static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
307 static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
308 static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
309 static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
310 static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
311 static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
312 
313 #if defined(__i386) || defined(__amd64)
314 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
315 #endif
316 
317 #if defined(_FIRMWARE_NEEDS_FDISK)
318 static boolean_t  cmlb_has_max_chs_vals(struct ipart *fdp);
319 #endif
320 
321 #if defined(_SUNOS_VTOC_16)
322 static void cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
323     struct dk_geom *cl_g, void *tg_cookie);
324 #endif
325 
326 static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
327     void *tg_cookie);
328 static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
329 static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
330     void *tg_cookie);
331 static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
332 static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
333     void *tg_cookie);
334 static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
335     int flag, void *tg_cookie);
336 static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
337     void *tg_cookie);
338 static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
339     void *tg_cookie);
340 static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
341     int flag, void *tg_cookie);
342 static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
343     int flag, void *tg_cookie);
344 static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
345     void *tg_cookie);
346 static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
347     void *tg_cookie);
348 static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
349     void *tg_cookie);
350 
351 #if defined(__i386) || defined(__amd64)
352 static int cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
353     void *tg_cookie);
354 static int cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart,
355     uint32_t start, uint32_t size);
356 static int cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start,
357     void *tg_cookie);
358 static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
359 static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag,
360     void *tg_cookie);
361 static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
362     int flag);
363 static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
364     int flag);
365 #endif
366 
367 static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
368 static void cmlb_v_log(dev_info_t *dev, const char *label, uint_t level,
369     const char *fmt, va_list ap);
370 static void cmlb_log(dev_info_t *dev, const char *label, uint_t level,
371     const char *fmt, ...);
372 
373 int
374 _init(void)
375 {
376 	mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
377 	return (mod_install(&modlinkage));
378 }
379 
380 int
381 _info(struct modinfo *modinfop)
382 {
383 	return (mod_info(&modlinkage, modinfop));
384 }
385 
386 int
387 _fini(void)
388 {
389 	int err;
390 
391 	if ((err = mod_remove(&modlinkage)) != 0) {
392 		return (err);
393 	}
394 
395 	mutex_destroy(&cmlb_log_mutex);
396 	return (err);
397 }
398 
399 /*
400  * cmlb_dbg is used for debugging to log additional info
401  * Level of output is controlled via cmlb_level_mask setting.
402  */
403 static void
404 cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
405 {
406 	va_list		ap;
407 	dev_info_t	*dev;
408 	uint_t		level_mask = 0;
409 
410 	ASSERT(cl != NULL);
411 	dev = CMLB_DEVINFO(cl);
412 	ASSERT(dev != NULL);
413 	/*
414 	 * Filter messages based on the global component and level masks,
415 	 * also print if cl matches the value of cmlb_debug_cl, or if
416 	 * cmlb_debug_cl is set to NULL.
417 	 */
418 	if (comp & CMLB_TRACE)
419 		level_mask |= CMLB_LOGMASK_TRACE;
420 
421 	if (comp & CMLB_INFO)
422 		level_mask |= CMLB_LOGMASK_INFO;
423 
424 	if (comp & CMLB_ERROR)
425 		level_mask |= CMLB_LOGMASK_ERROR;
426 
427 	if ((cmlb_level_mask & level_mask) &&
428 	    ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
429 		va_start(ap, fmt);
430 		cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
431 		va_end(ap);
432 	}
433 }
434 
435 /*
436  * cmlb_log is basically a duplicate of scsi_log. It is redefined here
437  * so that this module does not depend on scsi module.
438  */
439 static void
440 cmlb_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt, ...)
441 {
442 	va_list		ap;
443 
444 	va_start(ap, fmt);
445 	cmlb_v_log(dev, label, level, fmt, ap);
446 	va_end(ap);
447 }
448 
449 static void
450 cmlb_v_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt,
451     va_list ap)
452 {
453 	static char 	name[256];
454 	int 		log_only = 0;
455 	int 		boot_only = 0;
456 	int 		console_only = 0;
457 
458 	mutex_enter(&cmlb_log_mutex);
459 
460 	if (dev) {
461 		if (level == CE_PANIC || level == CE_WARN ||
462 		    level == CE_NOTE) {
463 			(void) sprintf(name, "%s (%s%d):\n",
464 			    ddi_pathname(dev, cmlb_log_buffer),
465 			    label, ddi_get_instance(dev));
466 		} else {
467 			name[0] = '\0';
468 		}
469 	} else {
470 		(void) sprintf(name, "%s:", label);
471 	}
472 
473 	(void) vsprintf(cmlb_log_buffer, fmt, ap);
474 
475 	switch (cmlb_log_buffer[0]) {
476 	case '!':
477 		log_only = 1;
478 		break;
479 	case '?':
480 		boot_only = 1;
481 		break;
482 	case '^':
483 		console_only = 1;
484 		break;
485 	}
486 
487 	switch (level) {
488 	case CE_NOTE:
489 		level = CE_CONT;
490 		/* FALLTHROUGH */
491 	case CE_CONT:
492 	case CE_WARN:
493 	case CE_PANIC:
494 		if (boot_only) {
495 			cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
496 		} else if (console_only) {
497 			cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
498 		} else if (log_only) {
499 			cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
500 		} else {
501 			cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
502 		}
503 		break;
504 	case CE_IGNORE:
505 		break;
506 	default:
507 		cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
508 		break;
509 	}
510 	mutex_exit(&cmlb_log_mutex);
511 }
512 
513 
514 /*
515  * cmlb_alloc_handle:
516  *
517  *	Allocates a handle.
518  *
519  * Arguments:
520  *	cmlbhandlep	pointer to handle
521  *
522  * Notes:
523  *	Allocates a handle and stores the allocated handle in the area
524  *	pointed to by cmlbhandlep
525  *
526  * Context:
527  *	Kernel thread only (can sleep).
528  */
529 void
530 cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
531 {
532 	struct cmlb_lun 	*cl;
533 
534 	cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
535 	ASSERT(cmlbhandlep != NULL);
536 
537 	cl->cl_state = CMLB_INITED;
538 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
539 	mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
540 
541 	*cmlbhandlep = (cmlb_handle_t)(cl);
542 }
543 
544 /*
545  * cmlb_free_handle
546  *
547  *	Frees handle.
548  *
549  * Arguments:
550  *	cmlbhandlep	pointer to handle
551  */
552 void
553 cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
554 {
555 	struct cmlb_lun 	*cl;
556 
557 	cl = (struct cmlb_lun *)*cmlbhandlep;
558 	if (cl != NULL) {
559 		mutex_destroy(CMLB_MUTEX(cl));
560 		kmem_free(cl, sizeof (struct cmlb_lun));
561 	}
562 
563 }
564 
565 /*
566  * cmlb_attach:
567  *
568  *	Attach handle to device, create minor nodes for device.
569  *
570  * Arguments:
571  * 	devi		pointer to device's dev_info structure.
572  * 	tgopsp		pointer to array of functions cmlb can use to callback
573  *			to target driver.
574  *
575  *	device_type	Peripheral device type as defined in
576  *			scsi/generic/inquiry.h
577  *
578  *	is_removable	whether or not device is removable.
579  *
580  *	is_hotpluggable	whether or not device is hotpluggable.
581  *
582  *	node_type	minor node type (as used by ddi_create_minor_node)
583  *
584  *	alter_behavior
585  *			bit flags:
586  *
587  *			CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
588  *			an alternate slice for the default label, if
589  *			device type is DTYPE_DIRECT an architectures default
590  *			label type is VTOC16.
591  *			Otherwise alternate slice will no be created.
592  *
593  *
594  *			CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
595  *			geometry and label for DKIOCGGEOM and DKIOCGVTOC
596  *			on architecture with VTOC8 label types.
597  *
598  * 			CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
599  *                      one bug in obtaining capacity (in sd):
600  *			SCSI READ_CAPACITY command returns the LBA number of the
601  *			last logical block, but sd once treated this number as
602  *			disks' capacity on x86 platform. And LBAs are addressed
603  *			based 0. So the last block was lost on x86 platform.
604  *
605  *			Now, we remove this workaround. In order for present sd
606  *			driver to work with disks which are labeled/partitioned
607  *			via previous sd, we add workaround as follows:
608  *
609  *			1) Locate backup EFI label: cmlb searches the next to
610  *			   last
611  *			   block for backup EFI label. If fails, it will
612  *			   turn to the last block for backup EFI label;
613  *
614  *			2) Clear backup EFI label: cmlb first search the last
615  *			   block for backup EFI label, and will search the
616  *			   next to last block only if failed for the last
617  *			   block.
618  *
619  *			3) Calculate geometry:refer to cmlb_convert_geometry()
620  *			   If capacity increasing by 1 causes disks' capacity
621  *			   to cross over the limits in geometry calculation,
622  *			   geometry info will change. This will raise an issue:
623  *			   In case that primary VTOC label is destroyed, format
624  *			   commandline can restore it via backup VTOC labels.
625  *			   And format locates backup VTOC labels by use of
626  *			   geometry. So changing geometry will
627  *			   prevent format from finding backup VTOC labels. To
628  *			   eliminate this side effect for compatibility,
629  *			   sd uses (capacity -1) to calculate geometry;
630  *
631  *			4) 1TB disks: some important data structures use
632  *			   32-bit signed long/int (for example, daddr_t),
633  *			   so that sd doesn't support a disk with capacity
634  *			   larger than 1TB on 32-bit platform. However,
635  *			   for exactly 1TB disk, it was treated as (1T - 512)B
636  *			   in the past, and could have valid Solaris
637  *			   partitions. To workaround this, if an exactly 1TB
638  *			   disk has Solaris fdisk partition, it will be allowed
639  *			   to work with sd.
640  *
641  *
642  *
643  *			CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
644  *			the entire disk, if there is no valid partition info.
645  *			If there is a valid Solaris partition, s0 and s2 will
646  *			only cover the entire Solaris partition.
647  *
648  *
649  *	cmlbhandle	cmlb handle associated with device
650  *
651  *	tg_cookie	cookie from target driver to be passed back to target
652  *			driver when we call back to it through tg_ops.
653  *
654  * Notes:
655  *	Assumes a default label based on capacity for non-removable devices.
656  *	If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
657  *	for the architecture).
658  *
659  *	For removable devices, default label type is assumed to be VTOC
660  *	type. Create minor nodes based on a default label type.
661  *	Label on the media is not validated.
662  *	minor number consists of:
663  *		if _SUNOS_VTOC_8 is defined
664  *			lowest 3 bits is taken as partition number
665  *			the rest is instance number
666  *		if _SUNOS_VTOC_16 is defined
667  *			lowest 6 bits is taken as partition number
668  *			the rest is instance number
669  *
670  *
671  * Return values:
672  *	0 	Success
673  * 	ENXIO 	creating minor nodes failed.
674  *	EINVAL  invalid arg, unsupported tg_ops version
675  */
676 int
677 cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
678     boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
679     int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
680 {
681 
682 	struct cmlb_lun	*cl = (struct cmlb_lun *)cmlbhandle;
683 	diskaddr_t	cap;
684 	int		status;
685 
686 	ASSERT(VALID_BOOLEAN(is_removable));
687 	ASSERT(VALID_BOOLEAN(is_hotpluggable));
688 
689 	if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
690 		return (EINVAL);
691 
692 	mutex_enter(CMLB_MUTEX(cl));
693 
694 	CMLB_DEVINFO(cl) = devi;
695 	cl->cmlb_tg_ops = tgopsp;
696 	cl->cl_device_type = device_type;
697 	cl->cl_is_removable = is_removable;
698 	cl->cl_is_hotpluggable = is_hotpluggable;
699 	cl->cl_node_type = node_type;
700 	cl->cl_sys_blocksize = DEV_BSIZE;
701 	cl->cl_f_geometry_is_valid = B_FALSE;
702 	cl->cl_def_labeltype = CMLB_LABEL_VTOC;
703 	cl->cl_alter_behavior = alter_behavior;
704 	cl->cl_reserved = -1;
705 	cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
706 #if defined(__i386) || defined(__amd64)
707 	cl->cl_logical_drive_count = 0;
708 #endif
709 
710 	if (!is_removable) {
711 		mutex_exit(CMLB_MUTEX(cl));
712 		status = DK_TG_GETCAP(cl, &cap, tg_cookie);
713 		mutex_enter(CMLB_MUTEX(cl));
714 		if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
715 			/* set default EFI if > 2TB */
716 			cl->cl_def_labeltype = CMLB_LABEL_EFI;
717 		}
718 	}
719 
720 	/* create minor nodes based on default label type */
721 	cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
722 	cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
723 
724 	if (cmlb_create_minor_nodes(cl) != 0) {
725 		mutex_exit(CMLB_MUTEX(cl));
726 		return (ENXIO);
727 	}
728 
729 	/* Define the dynamic properties for devinfo spapshots. */
730 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
731 
732 	cl->cl_state = CMLB_ATTACHED;
733 
734 	mutex_exit(CMLB_MUTEX(cl));
735 	return (0);
736 }
737 
738 /*
739  * cmlb_detach:
740  *
741  * Invalidate in-core labeling data and remove all minor nodes for
742  * the device associate with handle.
743  *
744  * Arguments:
745  *	cmlbhandle	cmlb handle associated with device.
746  *
747  *	tg_cookie	cookie from target driver to be passed back to target
748  *			driver when we call back to it through tg_ops.
749  *
750  */
751 /*ARGSUSED1*/
752 void
753 cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
754 {
755 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
756 
757 	mutex_enter(CMLB_MUTEX(cl));
758 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
759 	cl->cl_f_geometry_is_valid = B_FALSE;
760 	ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
761 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
762 	cl->cl_state = CMLB_INITED;
763 	mutex_exit(CMLB_MUTEX(cl));
764 }
765 
766 /*
767  * cmlb_validate:
768  *
769  *	Validates label.
770  *
771  * Arguments
772  *	cmlbhandle	cmlb handle associated with device.
773  *
774  *	flags		operation flags. used for verbosity control
775  *
776  *	tg_cookie	cookie from target driver to be passed back to target
777  *			driver when we call back to it through tg_ops.
778  *
779  *
780  * Notes:
781  *	If new label type is different from the current, adjust minor nodes
782  *	accordingly.
783  *
784  * Return values:
785  *	0		success
786  *			Note: having fdisk but no solaris partition is assumed
787  *			success.
788  *
789  *	ENOMEM		memory allocation failed
790  *	EIO		i/o errors during read or get capacity
791  * 	EACCESS		reservation conflicts
792  * 	EINVAL		label was corrupt, or no default label was assumed
793  *	ENXIO		invalid handle
794  */
795 int
796 cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
797 {
798 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
799 	int 		rval;
800 	int  		ret = 0;
801 
802 	/*
803 	 * Temp work-around checking cl for NULL since there is a bug
804 	 * in sd_detach calling this routine from taskq_dispatch
805 	 * inited function.
806 	 */
807 	if (cl == NULL)
808 		return (ENXIO);
809 
810 	mutex_enter(CMLB_MUTEX(cl));
811 	if (cl->cl_state < CMLB_ATTACHED) {
812 		mutex_exit(CMLB_MUTEX(cl));
813 		return (ENXIO);
814 	}
815 
816 	rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
817 	    flags, tg_cookie);
818 
819 	if (rval == ENOTSUP) {
820 		if (cl->cl_f_geometry_is_valid) {
821 			cl->cl_cur_labeltype = CMLB_LABEL_EFI;
822 			ret = 0;
823 		} else {
824 			ret = EINVAL;
825 		}
826 	} else {
827 		ret = rval;
828 		if (ret == 0)
829 			cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
830 	}
831 
832 	if (ret == 0)
833 		(void) cmlb_create_minor_nodes(cl);
834 
835 	mutex_exit(CMLB_MUTEX(cl));
836 	return (ret);
837 }
838 
839 /*
840  * cmlb_invalidate:
841  *	Invalidate in core label data
842  *
843  * Arguments:
844  *	cmlbhandle	cmlb handle associated with device.
845  *	tg_cookie	cookie from target driver to be passed back to target
846  *			driver when we call back to it through tg_ops.
847  */
848 /*ARGSUSED1*/
849 void
850 cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
851 {
852 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
853 
854 	if (cl == NULL)
855 		return;
856 
857 	mutex_enter(CMLB_MUTEX(cl));
858 	cl->cl_f_geometry_is_valid = B_FALSE;
859 	mutex_exit(CMLB_MUTEX(cl));
860 }
861 
862 /*
863  * cmlb_is_valid
864  * 	Get status on whether the incore label/geom data is valid
865  *
866  * Arguments:
867  *	cmlbhandle      cmlb handle associated with device.
868  *
869  * Return values:
870  *	B_TRUE if incore label/geom data is valid.
871  *	B_FALSE otherwise.
872  *
873  */
874 
875 
876 boolean_t
877 cmlb_is_valid(cmlb_handle_t cmlbhandle)
878 {
879 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
880 
881 	if (cmlbhandle == NULL)
882 		return (B_FALSE);
883 
884 	return (cl->cl_f_geometry_is_valid);
885 
886 }
887 
888 
889 
890 /*
891  * cmlb_close:
892  *
893  * Close the device, revert to a default label minor node for the device,
894  * if it is removable.
895  *
896  * Arguments:
897  *	cmlbhandle	cmlb handle associated with device.
898  *
899  *	tg_cookie	cookie from target driver to be passed back to target
900  *			driver when we call back to it through tg_ops.
901  * Return values:
902  *	0	Success
903  * 	ENXIO	Re-creating minor node failed.
904  */
905 /*ARGSUSED1*/
906 int
907 cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
908 {
909 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
910 
911 	mutex_enter(CMLB_MUTEX(cl));
912 	cl->cl_f_geometry_is_valid = B_FALSE;
913 
914 	/* revert to default minor node for this device */
915 	if (ISREMOVABLE(cl)) {
916 		cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
917 		(void) cmlb_create_minor_nodes(cl);
918 	}
919 
920 	mutex_exit(CMLB_MUTEX(cl));
921 	return (0);
922 }
923 
924 /*
925  * cmlb_get_devid_block:
926  *	 get the block number where device id is stored.
927  *
928  * Arguments:
929  *	cmlbhandle	cmlb handle associated with device.
930  *	devidblockp	pointer to block number.
931  *	tg_cookie	cookie from target driver to be passed back to target
932  *			driver when we call back to it through tg_ops.
933  *
934  * Notes:
935  *	It stores the block number of device id in the area pointed to
936  *	by devidblockp.
937  * 	with the block number of device id.
938  *
939  * Return values:
940  *	0	success
941  *	EINVAL 	device id does not apply to current label type.
942  */
943 /*ARGSUSED2*/
944 int
945 cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
946     void *tg_cookie)
947 {
948 	daddr_t			spc, blk, head, cyl;
949 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
950 
951 	mutex_enter(CMLB_MUTEX(cl));
952 	if (cl->cl_state < CMLB_ATTACHED) {
953 		mutex_exit(CMLB_MUTEX(cl));
954 		return (EINVAL);
955 	}
956 
957 	if ((!cl->cl_f_geometry_is_valid) ||
958 	    (cl->cl_solaris_size < DK_LABEL_LOC)) {
959 		mutex_exit(CMLB_MUTEX(cl));
960 		return (EINVAL);
961 	}
962 
963 	if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
964 		if (cl->cl_reserved != -1) {
965 			blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
966 		} else {
967 			mutex_exit(CMLB_MUTEX(cl));
968 			return (EINVAL);
969 		}
970 	} else {
971 		/* if the disk is unlabeled, don't write a devid to it */
972 		if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
973 			mutex_exit(CMLB_MUTEX(cl));
974 			return (EINVAL);
975 		}
976 
977 		/* this geometry doesn't allow us to write a devid */
978 		if (cl->cl_g.dkg_acyl < 2) {
979 			mutex_exit(CMLB_MUTEX(cl));
980 			return (EINVAL);
981 		}
982 
983 		/*
984 		 * Subtract 2 guarantees that the next to last cylinder
985 		 * is used
986 		 */
987 		cyl  = cl->cl_g.dkg_ncyl  + cl->cl_g.dkg_acyl - 2;
988 		spc  = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
989 		head = cl->cl_g.dkg_nhead - 1;
990 		blk  = cl->cl_solaris_offset +
991 		    (cyl * (spc - cl->cl_g.dkg_apc)) +
992 		    (head * cl->cl_g.dkg_nsect) + 1;
993 	}
994 
995 	*devidblockp = blk;
996 	mutex_exit(CMLB_MUTEX(cl));
997 	return (0);
998 }
999 
1000 /*
1001  * cmlb_partinfo:
1002  *	Get partition info for specified partition number.
1003  *
1004  * Arguments:
1005  *	cmlbhandle	cmlb handle associated with device.
1006  *	part		partition number
1007  *	nblocksp	pointer to number of blocks
1008  *	startblockp	pointer to starting block
1009  *	partnamep	pointer to name of partition
1010  *	tagp		pointer to tag info
1011  *	tg_cookie	cookie from target driver to be passed back to target
1012  *			driver when we call back to it through tg_ops.
1013  *
1014  *
1015  * Notes:
1016  *	If in-core label is not valid, this functions tries to revalidate
1017  *	the label. If label is valid, it stores the total number of blocks
1018  *	in this partition in the area pointed to by nblocksp, starting
1019  *	block number in area pointed to by startblockp,  pointer to partition
1020  *	name in area pointed to by partnamep, and tag value in area
1021  *	pointed by tagp.
1022  *	For EFI labels, tag value will be set to 0.
1023  *
1024  *	For all nblocksp, startblockp and partnamep, tagp, a value of NULL
1025  *	indicates the corresponding info is not requested.
1026  *
1027  *
1028  * Return values:
1029  *	0	success
1030  *	EINVAL  no valid label or requested partition number is invalid.
1031  *
1032  */
1033 int
1034 cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
1035     diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
1036 {
1037 
1038 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1039 	int rval;
1040 #if defined(__i386) || defined(__amd64)
1041 	int ext_part;
1042 #endif
1043 
1044 	ASSERT(cl != NULL);
1045 	mutex_enter(CMLB_MUTEX(cl));
1046 	if (cl->cl_state < CMLB_ATTACHED) {
1047 		mutex_exit(CMLB_MUTEX(cl));
1048 		return (EINVAL);
1049 	}
1050 
1051 	if (part  < 0 || part >= MAXPART) {
1052 		rval = EINVAL;
1053 	} else {
1054 		if (!cl->cl_f_geometry_is_valid)
1055 			(void) cmlb_validate_geometry((struct cmlb_lun *)cl,
1056 			    B_FALSE, 0, tg_cookie);
1057 
1058 #if defined(_SUNOS_VTOC_16)
1059 		if (((!cl->cl_f_geometry_is_valid) ||
1060 		    (part < NDKMAP && cl->cl_solaris_size == 0)) &&
1061 		    (part != P0_RAW_DISK)) {
1062 #else
1063 		if ((!cl->cl_f_geometry_is_valid) ||
1064 		    (part < NDKMAP && cl->cl_solaris_size == 0)) {
1065 #endif
1066 			rval = EINVAL;
1067 		} else {
1068 			if (startblockp != NULL)
1069 				*startblockp = (diskaddr_t)cl->cl_offset[part];
1070 
1071 			if (nblocksp != NULL)
1072 				*nblocksp = (diskaddr_t)
1073 				    cl->cl_map[part].dkl_nblk;
1074 
1075 			if (tagp != NULL)
1076 				*tagp =
1077 				    ((cl->cl_cur_labeltype == CMLB_LABEL_EFI) ||
1078 				    (part >= NDKMAP)) ? V_UNASSIGNED :
1079 				    cl->cl_vtoc.v_part[part].p_tag;
1080 			rval = 0;
1081 		}
1082 
1083 		/* consistent with behavior of sd for getting minor name */
1084 		if (partnamep != NULL) {
1085 #if defined(__i386) || defined(__amd64)
1086 #if defined(_FIRMWARE_NEEDS_FDISK)
1087 		if (part > FDISK_P4) {
1088 			ext_part = part-FDISK_P4-1;
1089 			*partnamep = dk_ext_minor_data[ext_part].name;
1090 		} else
1091 #endif
1092 #endif
1093 			*partnamep = dk_minor_data[part].name;
1094 		}
1095 
1096 	}
1097 
1098 	mutex_exit(CMLB_MUTEX(cl));
1099 	return (rval);
1100 }
1101 
1102 /*
1103  * cmlb_efi_label_capacity:
1104  *	Get capacity stored in EFI disk label.
1105  *
1106  * Arguments:
1107  *	cmlbhandle	cmlb handle associated with device.
1108  *	capacity	pointer to capacity stored in EFI disk label.
1109  *	tg_cookie	cookie from target driver to be passed back to target
1110  *			driver when we call back to it through tg_ops.
1111  *
1112  *
1113  * Notes:
1114  *	If in-core label is not valid, this functions tries to revalidate
1115  *	the label. If label is valid and is an EFI label, it stores the capacity
1116  *      in disk label in the area pointed to by capacity.
1117  *
1118  *
1119  * Return values:
1120  *	0	success
1121  *	EINVAL  no valid EFI label or capacity is NULL.
1122  *
1123  */
1124 int
1125 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1126     void *tg_cookie)
1127 {
1128 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1129 	int rval;
1130 
1131 	ASSERT(cl != NULL);
1132 	mutex_enter(CMLB_MUTEX(cl));
1133 	if (cl->cl_state < CMLB_ATTACHED) {
1134 		mutex_exit(CMLB_MUTEX(cl));
1135 		return (EINVAL);
1136 	}
1137 
1138 	if (!cl->cl_f_geometry_is_valid)
1139 		(void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1140 		    0, tg_cookie);
1141 
1142 	if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1143 	    (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1144 		rval = EINVAL;
1145 	} else {
1146 		*capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1147 		rval = 0;
1148 	}
1149 
1150 	mutex_exit(CMLB_MUTEX(cl));
1151 	return (rval);
1152 }
1153 
1154 /* Caller should make sure Test Unit Ready succeeds before calling this. */
1155 /*ARGSUSED*/
1156 int
1157 cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1158     int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
1159 {
1160 
1161 	int err;
1162 	struct cmlb_lun *cl;
1163 
1164 	cl = (struct cmlb_lun *)cmlbhandle;
1165 
1166 	ASSERT(cl != NULL);
1167 
1168 	mutex_enter(CMLB_MUTEX(cl));
1169 	if (cl->cl_state < CMLB_ATTACHED) {
1170 		mutex_exit(CMLB_MUTEX(cl));
1171 		return (EIO);
1172 	}
1173 
1174 	switch (cmd) {
1175 		case DKIOCSEXTVTOC:
1176 		case DKIOCSGEOM:
1177 		case DKIOCSETEFI:
1178 		case DKIOCSMBOOT:
1179 #if defined(__i386) || defined(__amd64)
1180 		case DKIOCSETEXTPART:
1181 #endif
1182 			break;
1183 		case DKIOCSVTOC:
1184 #if defined(__i386) || defined(__amd64)
1185 		case DKIOCPARTINFO:
1186 #endif
1187 			if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1188 				mutex_exit(CMLB_MUTEX(cl));
1189 				return (EOVERFLOW);
1190 			}
1191 			break;
1192 		default:
1193 			(void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1194 			    tg_cookie);
1195 
1196 			switch (cmd) {
1197 			case DKIOCGVTOC:
1198 			case DKIOCGAPART:
1199 			case DKIOCSAPART:
1200 
1201 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1202 					/* GPT label on disk */
1203 					mutex_exit(CMLB_MUTEX(cl));
1204 					return (ENOTSUP);
1205 				} else if
1206 				    (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1207 					mutex_exit(CMLB_MUTEX(cl));
1208 					return (EOVERFLOW);
1209 				}
1210 				break;
1211 
1212 			case DKIOCGGEOM:
1213 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1214 					/* GPT label on disk */
1215 					mutex_exit(CMLB_MUTEX(cl));
1216 					return (ENOTSUP);
1217 				}
1218 				break;
1219 			default:
1220 				break;
1221 			}
1222 	}
1223 
1224 	mutex_exit(CMLB_MUTEX(cl));
1225 
1226 	switch (cmd) {
1227 	case DKIOCGGEOM:
1228 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1229 		err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
1230 		break;
1231 
1232 	case DKIOCSGEOM:
1233 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1234 		err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
1235 		break;
1236 
1237 	case DKIOCGAPART:
1238 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1239 		err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1240 		    flag, tg_cookie);
1241 		break;
1242 
1243 	case DKIOCSAPART:
1244 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1245 		err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
1246 		break;
1247 
1248 	case DKIOCGVTOC:
1249 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1250 		err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
1251 		break;
1252 
1253 	case DKIOCGEXTVTOC:
1254 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1255 		err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1256 		break;
1257 
1258 	case DKIOCGETEFI:
1259 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1260 		err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
1261 		break;
1262 
1263 	case DKIOCPARTITION:
1264 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1265 		err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
1266 		break;
1267 
1268 	case DKIOCSVTOC:
1269 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1270 		err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1271 		    tg_cookie);
1272 		break;
1273 
1274 	case DKIOCSEXTVTOC:
1275 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1276 		err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1277 		    tg_cookie);
1278 		break;
1279 
1280 	case DKIOCSETEFI:
1281 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1282 		err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
1283 		break;
1284 
1285 	case DKIOCGMBOOT:
1286 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1287 		err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1288 		break;
1289 
1290 	case DKIOCSMBOOT:
1291 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1292 		err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1293 		break;
1294 	case DKIOCG_PHYGEOM:
1295 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
1296 #if defined(__i386) || defined(__amd64)
1297 		err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag, tg_cookie);
1298 #else
1299 		err = ENOTTY;
1300 #endif
1301 		break;
1302 	case DKIOCG_VIRTGEOM:
1303 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
1304 #if defined(__i386) || defined(__amd64)
1305 		err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
1306 #else
1307 		err = ENOTTY;
1308 #endif
1309 		break;
1310 	case DKIOCPARTINFO:
1311 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1312 #if defined(__i386) || defined(__amd64)
1313 		err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
1314 #else
1315 		err = ENOTTY;
1316 #endif
1317 		break;
1318 	case DKIOCEXTPARTINFO:
1319 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1320 #if defined(__i386) || defined(__amd64)
1321 		err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1322 #else
1323 		err = ENOTTY;
1324 #endif
1325 		break;
1326 #if defined(__i386) || defined(__amd64)
1327 	case DKIOCSETEXTPART:
1328 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEXTPART");
1329 		err = cmlb_dkio_set_ext_part(cl, (caddr_t)arg, flag, tg_cookie);
1330 		break;
1331 #endif
1332 	default:
1333 		err = ENOTTY;
1334 
1335 	}
1336 
1337 	/*
1338 	 * An ioctl that succeeds and changed ('set') size(9P) information
1339 	 * needs to invalidate the cached devinfo snapshot to avoid having
1340 	 * old information being returned in a snapshots.
1341 	 *
1342 	 * NB: When available, call ddi_change_minor_node() to clear
1343 	 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1344 	 */
1345 	if (err == 0) {
1346 		switch (cmd) {
1347 		case DKIOCSGEOM:
1348 		case DKIOCSAPART:
1349 		case DKIOCSVTOC:
1350 		case DKIOCSEXTVTOC:
1351 		case DKIOCSETEFI:
1352 			i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1353 			    i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1354 		}
1355 	}
1356 	return (err);
1357 }
1358 
1359 dev_t
1360 cmlb_make_device(struct cmlb_lun *cl)
1361 {
1362 	return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1363 	    ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
1364 }
1365 
1366 /*
1367  * Function: cmlb_check_update_blockcount
1368  *
1369  * Description: If current capacity value is invalid, obtains the
1370  *		current capacity from target driver.
1371  *
1372  * Return Code: 0	success
1373  *		EIO	failure
1374  */
1375 static int
1376 cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
1377 {
1378 	int status;
1379 	diskaddr_t capacity;
1380 	uint32_t lbasize;
1381 
1382 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1383 
1384 	if (cl->cl_f_geometry_is_valid)
1385 		return (0);
1386 
1387 	mutex_exit(CMLB_MUTEX(cl));
1388 	status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1389 	if (status != 0) {
1390 		mutex_enter(CMLB_MUTEX(cl));
1391 		return (EIO);
1392 	}
1393 
1394 	status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1395 	mutex_enter(CMLB_MUTEX(cl));
1396 	if (status != 0)
1397 		return (EIO);
1398 
1399 	if ((capacity != 0) && (lbasize != 0)) {
1400 		cl->cl_blockcount = capacity;
1401 		cl->cl_tgt_blocksize = lbasize;
1402 		if (!cl->cl_is_removable) {
1403 			cl->cl_sys_blocksize = lbasize;
1404 		}
1405 		return (0);
1406 	} else {
1407 		return (EIO);
1408 	}
1409 }
1410 
1411 static int
1412 cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
1413     minor_t minor_num, char *node_type, int flag, boolean_t internal)
1414 {
1415 	ASSERT(VALID_BOOLEAN(internal));
1416 
1417 	if (internal)
1418 		return (ddi_create_internal_pathname(dip,
1419 		    name, spec_type, minor_num));
1420 	else
1421 		return (ddi_create_minor_node(dip,
1422 		    name, spec_type, minor_num, node_type, flag));
1423 }
1424 
1425 /*
1426  *    Function: cmlb_create_minor_nodes
1427  *
1428  * Description: Create or adjust the minor device nodes for the instance.
1429  * 		Minor nodes are created based on default label type,
1430  *		current label type and last label type we created
1431  *		minor nodes based on.
1432  *
1433  *
1434  *   Arguments: cl - driver soft state (unit) structure
1435  *
1436  * Return Code: 0 success
1437  *		ENXIO	failure.
1438  *
1439  *     Context: Kernel thread context
1440  */
1441 static int
1442 cmlb_create_minor_nodes(struct cmlb_lun *cl)
1443 {
1444 	struct driver_minor_data	*dmdp;
1445 	int				instance;
1446 	char				name[48];
1447 	cmlb_label_t			newlabeltype;
1448 	boolean_t			internal;
1449 
1450 	ASSERT(cl != NULL);
1451 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1452 
1453 	internal = VOID2BOOLEAN(
1454 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
1455 
1456 	/* check the most common case */
1457 	if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1458 	    cl->cl_last_labeltype == cl->cl_cur_labeltype) {
1459 		/* do nothing */
1460 		return (0);
1461 	}
1462 
1463 	if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
1464 		/* we should never get here */
1465 		return (ENXIO);
1466 	}
1467 
1468 	if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
1469 		/* first time during attach */
1470 		newlabeltype = cl->cl_def_labeltype;
1471 
1472 		instance = ddi_get_instance(CMLB_DEVINFO(cl));
1473 
1474 		/* Create all the minor nodes for this target. */
1475 		dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
1476 		    dk_minor_data;
1477 		while (dmdp->name != NULL) {
1478 
1479 			(void) sprintf(name, "%s", dmdp->name);
1480 
1481 			if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
1482 			    dmdp->type,
1483 			    (instance << CMLBUNIT_SHIFT) | dmdp->minor,
1484 			    cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1485 				/*
1486 				 * Clean up any nodes that may have been
1487 				 * created, in case this fails in the middle
1488 				 * of the loop.
1489 				 */
1490 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1491 				return (ENXIO);
1492 			}
1493 			dmdp++;
1494 		}
1495 		cl->cl_last_labeltype = newlabeltype;
1496 		return (0);
1497 	}
1498 
1499 	/* Not first time  */
1500 	if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1501 		if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
1502 			/* close time, revert to default. */
1503 			newlabeltype = cl->cl_def_labeltype;
1504 		} else {
1505 			/*
1506 			 * do nothing since the type for which we last created
1507 			 * nodes matches the default
1508 			 */
1509 			return (0);
1510 		}
1511 	} else {
1512 		if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
1513 			/* We are not closing, use current label type */
1514 			newlabeltype = cl->cl_cur_labeltype;
1515 		} else {
1516 			/*
1517 			 * do nothing since the type for which we last created
1518 			 * nodes matches the current label type
1519 			 */
1520 			return (0);
1521 		}
1522 	}
1523 
1524 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
1525 
1526 	/*
1527 	 * Currently we only fix up the s7 node when we are switching
1528 	 * label types from or to EFI. This is consistent with
1529 	 * current behavior of sd.
1530 	 */
1531 	if (newlabeltype == CMLB_LABEL_EFI &&
1532 	    cl->cl_last_labeltype != CMLB_LABEL_EFI) {
1533 		/* from vtoc to EFI */
1534 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1535 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1536 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
1537 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1538 		    cl->cl_node_type, NULL, internal);
1539 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
1540 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1541 		    cl->cl_node_type, NULL, internal);
1542 	} else {
1543 		/* from efi to vtoc */
1544 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1545 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1546 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
1547 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1548 		    cl->cl_node_type, NULL, internal);
1549 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
1550 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1551 		    cl->cl_node_type, NULL, internal);
1552 	}
1553 
1554 	cl->cl_last_labeltype = newlabeltype;
1555 	return (0);
1556 }
1557 
1558 /*
1559  *    Function: cmlb_validate_geometry
1560  *
1561  * Description: Read the label from the disk (if present). Update the unit's
1562  *		geometry and vtoc information from the data in the label.
1563  *		Verify that the label is valid.
1564  *
1565  *   Arguments:
1566  *	cl		driver soft state (unit) structure
1567  *
1568  *	forcerevalid	force revalidation even if we are already valid.
1569  *	flags		operation flags from target driver. Used for verbosity
1570  *			control	at this time.
1571  *	tg_cookie	cookie from target driver to be passed back to target
1572  *			driver when we call back to it through tg_ops.
1573  *
1574  * Return Code: 0 - Successful completion
1575  *		EINVAL  - Invalid value in cl->cl_tgt_blocksize or
1576  *			  cl->cl_blockcount; or label on disk is corrupted
1577  *			  or unreadable.
1578  *		EACCES  - Reservation conflict at the device.
1579  *		ENOMEM  - Resource allocation error
1580  *		ENOTSUP - geometry not applicable
1581  *
1582  *     Context: Kernel thread only (can sleep).
1583  */
1584 static int
1585 cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1586     void *tg_cookie)
1587 {
1588 	int		label_error = 0;
1589 	diskaddr_t	capacity;
1590 	int		count;
1591 
1592 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1593 	ASSERT(VALID_BOOLEAN(forcerevalid));
1594 
1595 	if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1596 		if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
1597 			return (ENOTSUP);
1598 		return (0);
1599 	}
1600 
1601 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
1602 		return (EIO);
1603 
1604 	capacity = cl->cl_blockcount;
1605 
1606 #if defined(_SUNOS_VTOC_16)
1607 	/*
1608 	 * Set up the "whole disk" fdisk partition; this should always
1609 	 * exist, regardless of whether the disk contains an fdisk table
1610 	 * or vtoc.
1611 	 */
1612 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1613 	cl->cl_offset[P0_RAW_DISK] = 0;
1614 	/*
1615 	 * note if capacity > int32_max(1TB) we are in 64bit environment
1616 	 * so no truncation happens
1617 	 */
1618 	cl->cl_map[P0_RAW_DISK].dkl_nblk  = capacity;
1619 #endif
1620 	/*
1621 	 * Refresh the logical and physical geometry caches.
1622 	 * (data from MODE SENSE format/rigid disk geometry pages,
1623 	 * and scsi_ifgetcap("geometry").
1624 	 */
1625 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
1626 
1627 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1628 	label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
1629 	if (label_error == 0) {
1630 
1631 		/* found a valid EFI label */
1632 		cmlb_dbg(CMLB_TRACE, cl,
1633 		    "cmlb_validate_geometry: found EFI label\n");
1634 		/*
1635 		 * solaris_size and geometry_is_valid are set in
1636 		 * cmlb_use_efi
1637 		 */
1638 		return (ENOTSUP);
1639 	}
1640 
1641 	/* NO EFI label found */
1642 
1643 	if (capacity > CMLB_EXTVTOC_LIMIT) {
1644 		if (label_error == ESRCH) {
1645 			/*
1646 			 * they've configured a LUN over 2TB, but used
1647 			 * format.dat to restrict format's view of the
1648 			 * capacity to be under 2TB in some earlier Solaris
1649 			 * release.
1650 			 */
1651 			/* i.e > 2TB with a VTOC < 2TB */
1652 			if (!(flags & CMLB_SILENT) &&
1653 			    (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
1654 
1655 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1656 				    CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1657 				    "due to VTOC label. To use the full "
1658 				    "capacity of the disk, use format(1M) to "
1659 				    "relabel the disk with EFI/GPT label.\n",
1660 				    CMLB_LABEL(cl),
1661 				    ddi_get_instance(CMLB_DEVINFO(cl)));
1662 
1663 				cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1664 			}
1665 		} else {
1666 				return (ENOTSUP);
1667 		}
1668 	}
1669 
1670 	label_error = 0;
1671 
1672 	/*
1673 	 * at this point it is either labeled with a VTOC or it is
1674 	 * under 1TB (<= 1TB actually for off-by-1)
1675 	 */
1676 
1677 	/*
1678 	 * Only DIRECT ACCESS devices will have Scl labels.
1679 	 * CD's supposedly have a Scl label, too
1680 	 */
1681 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
1682 		struct	dk_label *dkl;
1683 		offset_t label_addr;
1684 		int	rval;
1685 		size_t	buffer_size;
1686 
1687 		/*
1688 		 * Note: This will set up cl->cl_solaris_size and
1689 		 * cl->cl_solaris_offset.
1690 		 */
1691 		rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1692 		if ((rval != 0) && !ISCD(cl)) {
1693 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1694 			return (rval);
1695 		}
1696 
1697 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1698 			/*
1699 			 * Found fdisk table but no Solaris partition entry,
1700 			 * so don't call cmlb_uselabel() and don't create
1701 			 * a default label.
1702 			 */
1703 			label_error = 0;
1704 			cl->cl_f_geometry_is_valid = B_TRUE;
1705 			goto no_solaris_partition;
1706 		}
1707 
1708 		label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1709 
1710 		buffer_size = cl->cl_sys_blocksize;
1711 
1712 		cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
1713 		    "label_addr: 0x%x allocation size: 0x%x\n",
1714 		    label_addr, buffer_size);
1715 
1716 		if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
1717 			return (ENOMEM);
1718 
1719 		mutex_exit(CMLB_MUTEX(cl));
1720 		rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1721 		mutex_enter(CMLB_MUTEX(cl));
1722 
1723 		switch (rval) {
1724 		case 0:
1725 			/*
1726 			 * cmlb_uselabel will establish that the geometry
1727 			 * is valid.
1728 			 */
1729 			if (cmlb_uselabel(cl,
1730 			    (struct dk_label *)(uintptr_t)dkl, flags) !=
1731 			    CMLB_LABEL_IS_VALID) {
1732 				label_error = EINVAL;
1733 			} else
1734 				cl->cl_label_from_media = CMLB_LABEL_VTOC;
1735 			break;
1736 		case EACCES:
1737 			label_error = EACCES;
1738 			break;
1739 		default:
1740 			label_error = EINVAL;
1741 			break;
1742 		}
1743 
1744 		kmem_free(dkl, buffer_size);
1745 	}
1746 
1747 	/*
1748 	 * If a valid label was not found, AND if no reservation conflict
1749 	 * was detected, then go ahead and create a default label (4069506).
1750 	 *
1751 	 * Note: currently, for VTOC_8 devices, the default label is created
1752 	 * for removables and hotpluggables only.  For VTOC_16 devices, the
1753 	 * default label will be created for all devices.
1754 	 * (see cmlb_build_default_label)
1755 	 */
1756 #if defined(_SUNOS_VTOC_8)
1757 	if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1758 	    (label_error != EACCES)) {
1759 #elif defined(_SUNOS_VTOC_16)
1760 	if (label_error != EACCES) {
1761 #endif
1762 		if (!cl->cl_f_geometry_is_valid) {
1763 			cmlb_build_default_label(cl, tg_cookie);
1764 		}
1765 		label_error = 0;
1766 	}
1767 
1768 no_solaris_partition:
1769 
1770 #if defined(_SUNOS_VTOC_16)
1771 	/*
1772 	 * If we have valid geometry, set up the remaining fdisk partitions.
1773 	 * Note that dkl_cylno is not used for the fdisk map entries, so
1774 	 * we set it to an entirely bogus value.
1775 	 */
1776 	for (count = 0; count < FDISK_PARTS; count++) {
1777 		cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1778 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
1779 		    cl->cl_fmap[count].fmap_nblk;
1780 
1781 		cl->cl_offset[FDISK_P1 + count] =
1782 		    cl->cl_fmap[count].fmap_start;
1783 	}
1784 #endif
1785 
1786 	for (count = 0; count < NDKMAP; count++) {
1787 #if defined(_SUNOS_VTOC_8)
1788 		struct dk_map *lp  = &cl->cl_map[count];
1789 		cl->cl_offset[count] =
1790 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
1791 #elif defined(_SUNOS_VTOC_16)
1792 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
1793 
1794 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
1795 #else
1796 #error "No VTOC format defined."
1797 #endif
1798 	}
1799 
1800 	return (label_error);
1801 }
1802 
1803 #if defined(_SUNOS_VTOC_16)
1804 /*
1805  *    Function: cmlb_convert_geometry
1806  *
1807  * Description: Convert physical geometry into a dk_geom structure. In
1808  *		other words, make sure we don't wrap 16-bit values.
1809  *		e.g. converting from geom_cache to dk_geom
1810  *
1811  *     Context: Kernel thread only
1812  */
1813 static void
1814 cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
1815     struct dk_geom *cl_g, void *tg_cookie)
1816 {
1817 
1818 	ASSERT(cl != NULL);
1819 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1820 
1821 	/* Unlabeled SCSI floppy device */
1822 	if (capacity < 160) {
1823 		/* Less than 80K */
1824 		cl_g->dkg_nhead = 1;
1825 		cl_g->dkg_ncyl = capacity;
1826 		cl_g->dkg_nsect = 1;
1827 		return;
1828 	} else if (capacity <= 0x1000) {
1829 		cl_g->dkg_nhead = 2;
1830 		cl_g->dkg_ncyl = 80;
1831 		cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
1832 		return;
1833 	}
1834 
1835 	/*
1836 	 * For all devices we calculate cylinders using the heads and sectors
1837 	 * we assign based on capacity of the device.  The algorithm is
1838 	 * designed to be compatible with the way other operating systems
1839 	 * lay out fdisk tables for X86 and to insure that the cylinders never
1840 	 * exceed 65535 to prevent problems with X86 ioctls that report
1841 	 * geometry.
1842 	 * For some smaller disk sizes we report geometry that matches those
1843 	 * used by X86 BIOS usage. For larger disks, we use SPT that are
1844 	 * multiples of 63, since other OSes that are not limited to 16-bits
1845 	 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
1846 	 *
1847 	 * The following table (in order) illustrates some end result
1848 	 * calculations:
1849 	 *
1850 	 * Maximum number of blocks 		nhead	nsect
1851 	 *
1852 	 * 2097152 (1GB)			64	32
1853 	 * 16777216 (8GB)			128	32
1854 	 * 1052819775 (502.02GB)		255  	63
1855 	 * 2105639550 (0.98TB)			255	126
1856 	 * 3158459325 (1.47TB)			255  	189
1857 	 * 4211279100 (1.96TB)			255  	252
1858 	 * 5264098875 (2.45TB)			255  	315
1859 	 * ...
1860 	 *
1861 	 * For Solid State Drive(SSD), it uses 4K page size inside and may be
1862 	 * double with every new generation. If the I/O is not aligned with
1863 	 * page size on SSDs, SSDs perform a lot slower.
1864 	 * By default, Solaris partition starts from cylinder 1. It will be
1865 	 * misaligned even with 4K if using heads(255) and SPT(63). To
1866 	 * workaround the problem, if the device is SSD, we use heads(224) and
1867 	 * SPT multiple of 56. Thus the default Solaris partition starts from
1868 	 * a position that aligns with 128K on a 512 bytes sector size SSD.
1869 	 */
1870 
1871 	if (capacity <= 0x200000) {
1872 		cl_g->dkg_nhead = 64;
1873 		cl_g->dkg_nsect = 32;
1874 	} else if (capacity <= 0x01000000) {
1875 		cl_g->dkg_nhead = 128;
1876 		cl_g->dkg_nsect = 32;
1877 	} else {
1878 		tg_attribute_t tgattribute;
1879 		int is_solid_state;
1880 		unsigned short nhead;
1881 		unsigned short nsect;
1882 
1883 		bzero(&tgattribute, sizeof (tg_attribute_t));
1884 
1885 		mutex_exit(CMLB_MUTEX(cl));
1886 		is_solid_state =
1887 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
1888 		    tgattribute.media_is_solid_state : FALSE;
1889 		mutex_enter(CMLB_MUTEX(cl));
1890 
1891 		if (is_solid_state) {
1892 			nhead = 224;
1893 			nsect = 56;
1894 		} else {
1895 			nhead = 255;
1896 			nsect = 63;
1897 		}
1898 
1899 		cl_g->dkg_nhead = nhead;
1900 
1901 		/* make dkg_nsect be smallest multiple of nsect */
1902 		cl_g->dkg_nsect = ((capacity +
1903 		    (UINT16_MAX * nhead * nsect) - 1) /
1904 		    (UINT16_MAX * nhead * nsect)) * nsect;
1905 
1906 		if (cl_g->dkg_nsect == 0)
1907 			cl_g->dkg_nsect = (UINT16_MAX / nsect) * nsect;
1908 	}
1909 
1910 }
1911 #endif
1912 
1913 /*
1914  *    Function: cmlb_resync_geom_caches
1915  *
1916  * Description: (Re)initialize both geometry caches: the virtual geometry
1917  *            information is extracted from the HBA (the "geometry"
1918  *            capability), and the physical geometry cache data is
1919  *            generated by issuing MODE SENSE commands.
1920  *
1921  *   Arguments:
1922  *	cl 		driver soft state (unit) structure
1923  *	capacity	disk capacity in #blocks
1924  *	tg_cookie	cookie from target driver to be passed back to target
1925  *			driver when we call back to it through tg_ops.
1926  *
1927  *     Context: Kernel thread only (can sleep).
1928  */
1929 static void
1930 cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1931     void *tg_cookie)
1932 {
1933 	struct	cmlb_geom 	pgeom;
1934 	struct	cmlb_geom	lgeom;
1935 	struct 	cmlb_geom	*pgeomp = &pgeom;
1936 	unsigned short 		nhead;
1937 	unsigned short 		nsect;
1938 	int 			spc;
1939 	int			ret;
1940 
1941 	ASSERT(cl != NULL);
1942 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1943 
1944 	/*
1945 	 * Ask the controller for its logical geometry.
1946 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1947 	 * then the lgeom cache will be invalid.
1948 	 */
1949 	mutex_exit(CMLB_MUTEX(cl));
1950 	bzero(&lgeom, sizeof (struct cmlb_geom));
1951 	ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1952 	mutex_enter(CMLB_MUTEX(cl));
1953 
1954 	bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
1955 
1956 	/*
1957 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1958 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1959 	 */
1960 	if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1961 	    cl->cl_lgeom.g_nhead == 0) {
1962 		/*
1963 		 * Note: Perhaps this needs to be more adaptive? The rationale
1964 		 * is that, if there's no HBA geometry from the HBA driver, any
1965 		 * guess is good, since this is the physical geometry. If MODE
1966 		 * SENSE fails this gives a max cylinder size for non-LBA access
1967 		 */
1968 		nhead = 255;
1969 		nsect = 63;
1970 	} else {
1971 		nhead = cl->cl_lgeom.g_nhead;
1972 		nsect = cl->cl_lgeom.g_nsect;
1973 	}
1974 
1975 	if (ISCD(cl)) {
1976 		pgeomp->g_nhead = 1;
1977 		pgeomp->g_nsect = nsect * nhead;
1978 	} else {
1979 		pgeomp->g_nhead = nhead;
1980 		pgeomp->g_nsect = nsect;
1981 	}
1982 
1983 	spc = pgeomp->g_nhead * pgeomp->g_nsect;
1984 	pgeomp->g_capacity = capacity;
1985 	if (spc == 0)
1986 		pgeomp->g_ncyl = 0;
1987 	else
1988 		pgeomp->g_ncyl = pgeomp->g_capacity / spc;
1989 	pgeomp->g_acyl = 0;
1990 
1991 	/*
1992 	 * Retrieve fresh geometry data from the hardware, stash it
1993 	 * here temporarily before we rebuild the incore label.
1994 	 *
1995 	 * We want to use the MODE SENSE commands to derive the
1996 	 * physical geometry of the device, but if either command
1997 	 * fails, the logical geometry is used as the fallback for
1998 	 * disk label geometry.
1999 	 */
2000 
2001 	mutex_exit(CMLB_MUTEX(cl));
2002 	(void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
2003 	mutex_enter(CMLB_MUTEX(cl));
2004 
2005 	/*
2006 	 * Now update the real copy while holding the mutex. This
2007 	 * way the global copy is never in an inconsistent state.
2008 	 */
2009 	bcopy(pgeomp, &cl->cl_pgeom,  sizeof (cl->cl_pgeom));
2010 
2011 	cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
2012 	    "(cached from lgeom)\n");
2013 	cmlb_dbg(CMLB_INFO,  cl,
2014 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2015 	    cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
2016 	    cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
2017 	cmlb_dbg(CMLB_INFO,  cl, "   lbasize: %d; capacity: %ld; "
2018 	    "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
2019 	    cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
2020 	    cl->cl_pgeom.g_rpm);
2021 }
2022 
2023 
2024 #if defined(__i386) || defined(__amd64)
2025 /*
2026  *    Function: cmlb_update_ext_minor_nodes
2027  *
2028  * Description: Routine to add/remove extended partition device nodes
2029  *
2030  *   Arguments:
2031  *	cl		driver soft state (unit) structure
2032  *	num_parts	Number of logical drives found on the LUN
2033  *
2034  * Should be called with the mutex held
2035  *
2036  * Return Code: 0 for success
2037  *
2038  *     Context: User and Kernel thread
2039  *
2040  */
2041 static int
2042 cmlb_update_ext_minor_nodes(struct cmlb_lun *cl, int num_parts)
2043 {
2044 	int				i, count;
2045 	char				name[48];
2046 	int				instance;
2047 	struct driver_minor_data	*demdp, *demdpr;
2048 	char				*devnm;
2049 	dev_info_t			*pdip;
2050 	boolean_t 			internal;
2051 
2052 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2053 	ASSERT(cl->cl_update_ext_minor_nodes == 1);
2054 
2055 	internal = VOID2BOOLEAN(
2056 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
2057 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
2058 	demdp = dk_ext_minor_data;
2059 	demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2060 
2061 
2062 	if (cl->cl_logical_drive_count) {
2063 		for (i = 0; i < cl->cl_logical_drive_count; i++) {
2064 			(void) sprintf(name, "%s", demdp->name);
2065 			ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2066 			(void) sprintf(name, "%s", demdpr->name);
2067 			ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2068 			demdp++;
2069 			demdpr++;
2070 		}
2071 		/* There are existing device nodes. Remove them */
2072 		devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2073 		(void) ddi_deviname(cl->cl_devi, devnm);
2074 		pdip = ddi_get_parent(cl->cl_devi);
2075 		(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2076 		kmem_free(devnm, MAXNAMELEN + 1);
2077 	}
2078 
2079 	demdp = dk_ext_minor_data;
2080 	demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2081 
2082 	for (i = 0; i < num_parts; i++) {
2083 		(void) sprintf(name, "%s", demdp->name);
2084 		if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
2085 		    demdp->type,
2086 		    (instance << CMLBUNIT_SHIFT) | demdp->minor,
2087 		    cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
2088 			/*
2089 			 * Clean up any nodes that may have been
2090 			 * created, in case this fails in the middle
2091 			 * of the loop.
2092 			 */
2093 			ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2094 			cl->cl_logical_drive_count = 0;
2095 			return (ENXIO);
2096 		}
2097 		(void) sprintf(name, "%s", demdpr->name);
2098 		if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
2099 		    demdpr->type,
2100 		    (instance << CMLBUNIT_SHIFT) | demdpr->minor,
2101 		    cl->cl_node_type, NULL) == DDI_FAILURE) {
2102 			/*
2103 			 * Clean up any nodes that may have been
2104 			 * created, in case this fails in the middle
2105 			 * of the loop.
2106 			 */
2107 			ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2108 			cl->cl_logical_drive_count = 0;
2109 			return (ENXIO);
2110 		}
2111 		demdp++;
2112 		demdpr++;
2113 	}
2114 
2115 	/* Update the cl_map array for logical drives */
2116 	for (count = 0; count < MAX_EXT_PARTS; count++) {
2117 		cl->cl_map[FDISK_P4 + 1 + count].dkl_cylno = UINT32_MAX;
2118 		cl->cl_map[FDISK_P4 + 1 + count].dkl_nblk =
2119 		    cl->cl_fmap[FD_NUMPART + count].fmap_nblk;
2120 		cl->cl_offset[FDISK_P4 + 1 + count] =
2121 		    cl->cl_fmap[FD_NUMPART + count].fmap_start;
2122 	}
2123 
2124 	cl->cl_logical_drive_count = i;
2125 	cl->cl_update_ext_minor_nodes = 0;
2126 	return (0);
2127 }
2128 /*
2129  *    Function: cmlb_validate_ext_part
2130  *
2131  * Description: utility routine to validate an extended partition's
2132  *		metadata as found on disk
2133  *
2134  *   Arguments:
2135  *	cl		driver soft state (unit) structure
2136  *	part		partition number of the extended partition
2137  *	epart		partition number of the logical drive
2138  *	start		absolute sector number of the start of the logical
2139  *			drive being validated
2140  *	size		size of logical drive being validated
2141  *
2142  * Return Code: 0 for success
2143  *
2144  *     Context: User and Kernel thread
2145  *
2146  * Algorithm :
2147  * Error cases are :
2148  *	1. If start block is lesser than or equal to the end block
2149  *	2. If either start block or end block is beyond the bounadry
2150  *	   of the extended partition.
2151  *	3. start or end block overlap with existing partitions.
2152  *		To check this, first make sure that the start block doesnt
2153  *		overlap with existing partitions. Then, calculate the
2154  *		possible end block for the given start block that doesnt
2155  *		overlap with existing partitions. This can be calculated by
2156  *		first setting the possible end block to the end of the
2157  *		extended partition (optimistic) and then, checking if there
2158  *		is any other partition that lies after the start of the
2159  *		partition being validated. If so, set the possible end to
2160  *		one block less than the beginning of the next nearest partition
2161  *		If the actual end block is greater than the calculated end
2162  *		block, we have an overlap.
2163  *
2164  */
2165 static int
2166 cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart, uint32_t start,
2167     uint32_t size)
2168 {
2169 	int i;
2170 	uint32_t end = start + size - 1;
2171 	uint32_t ext_start = cl->cl_fmap[part].fmap_start;
2172 	uint32_t ext_end = ext_start + cl->cl_fmap[part].fmap_nblk - 1;
2173 	uint32_t ts, te;
2174 	uint32_t poss_end = ext_end;
2175 
2176 	if (end <= start) {
2177 		return (1);
2178 	}
2179 
2180 	/*
2181 	 * Check if the logical drive boundaries are within that of the
2182 	 * extended partition.
2183 	 */
2184 	if (start <= ext_start || start > ext_end || end <= ext_start ||
2185 	    end > ext_end) {
2186 		return (1);
2187 	}
2188 
2189 	/*
2190 	 * epart will be equal to FD_NUMPART if it is the first logical drive.
2191 	 * There is no need to check for overlaps with other logical drives,
2192 	 * since it is the only logical drive that we have come across so far.
2193 	 */
2194 	if (epart == FD_NUMPART) {
2195 		return (0);
2196 	}
2197 
2198 	/* Check for overlaps with existing logical drives */
2199 	i = FD_NUMPART;
2200 	ts = cl->cl_fmap[FD_NUMPART].fmap_start;
2201 	te = ts + cl->cl_fmap[FD_NUMPART].fmap_nblk - 1;
2202 
2203 	while ((i < epart) && ts && te) {
2204 		if (start >= ts && start <= te) {
2205 			return (1);
2206 		}
2207 
2208 		if ((ts < poss_end) && (ts > start)) {
2209 			poss_end = ts - 1;
2210 		}
2211 
2212 		i++;
2213 		ts = cl->cl_fmap[i].fmap_start;
2214 		te = ts + cl->cl_fmap[i].fmap_nblk - 1;
2215 	}
2216 
2217 	if (end > poss_end) {
2218 		return (1);
2219 	}
2220 
2221 	return (0);
2222 }
2223 
2224 
2225 /*
2226  *    Function: cmlb_is_linux_swap
2227  *
2228  * Description: utility routine to verify if a partition is a linux swap
2229  *		partition or not.
2230  *
2231  *   Arguments:
2232  *	cl		driver soft state (unit) structure
2233  *	part_start	absolute sector number of the start of the partition
2234  *			being verified
2235  *	tg_cookie	cookie from target driver to be passed back to target
2236  *			driver when we call back to it through tg_ops.
2237  *
2238  * Return Code: 0 for success
2239  *
2240  *     Context: User and Kernel thread
2241  *
2242  * Notes:
2243  *	The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2244  *	last 10 bytes of a disk block whose size is that of the linux page
2245  *	size. This disk block is found at the beginning of the swap partition.
2246  */
2247 static int
2248 cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start, void *tg_cookie)
2249 {
2250 	int		i;
2251 	int		rval = -1;
2252 	uint32_t	seek_offset;
2253 	uint32_t	linux_pg_size;
2254 	char 		*buf, *linux_swap_magic;
2255 	int		sec_sz = cl->cl_sys_blocksize;
2256 	/* Known linux kernel page sizes */
2257 	uint32_t	linux_pg_size_arr[] = {4096, };
2258 
2259 	ASSERT(cl != NULL);
2260 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2261 
2262 	if ((buf = kmem_zalloc(sec_sz, KM_NOSLEEP)) == NULL) {
2263 		return (ENOMEM);
2264 	}
2265 
2266 	/*
2267 	 * Check if there is a sane Solaris VTOC
2268 	 * If there is a valid vtoc, no need to lookup
2269 	 * for the linux swap signature.
2270 	 */
2271 	mutex_exit(CMLB_MUTEX(cl));
2272 	rval = DK_TG_READ(cl, buf, part_start + DK_LABEL_LOC,
2273 	    sec_sz, tg_cookie);
2274 	mutex_enter(CMLB_MUTEX(cl));
2275 	if (rval != 0) {
2276 		cmlb_dbg(CMLB_ERROR,  cl,
2277 		    "cmlb_is_linux_swap: disk vtoc read err\n");
2278 		rval = EIO;
2279 		goto done;
2280 	}
2281 
2282 	if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
2283 	    (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
2284 		rval = -1;
2285 		goto done;
2286 	}
2287 
2288 
2289 	/* No valid vtoc, so check for linux swap signature */
2290 	linux_swap_magic = buf + sec_sz - 10;
2291 
2292 	for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
2293 		linux_pg_size = linux_pg_size_arr[i];
2294 		seek_offset = linux_pg_size/sec_sz - 1;
2295 		seek_offset += part_start;
2296 
2297 		mutex_exit(CMLB_MUTEX(cl));
2298 		rval = DK_TG_READ(cl, buf, seek_offset, sec_sz, tg_cookie);
2299 		mutex_enter(CMLB_MUTEX(cl));
2300 
2301 		if (rval != 0) {
2302 			cmlb_dbg(CMLB_ERROR,  cl,
2303 			    "cmlb_is_linux_swap: disk read err\n");
2304 			rval = EIO;
2305 			break;
2306 		}
2307 
2308 		rval = -1;
2309 
2310 		if ((strncmp(linux_swap_magic, "SWAP-SPACE", 10) == 0) ||
2311 		    (strncmp(linux_swap_magic, "SWAPSPACE2", 10) == 0)) {
2312 			/* Found a linux swap */
2313 			rval = 0;
2314 			break;
2315 		}
2316 	}
2317 
2318 done:
2319 	kmem_free(buf, sec_sz);
2320 	return (rval);
2321 }
2322 #endif
2323 
2324 /*
2325  *    Function: cmlb_read_fdisk
2326  *
2327  * Description: utility routine to read the fdisk table.
2328  *
2329  *   Arguments:
2330  *	cl		driver soft state (unit) structure
2331  *	capacity	disk capacity in #blocks
2332  *	tg_cookie	cookie from target driver to be passed back to target
2333  *			driver when we call back to it through tg_ops.
2334  *
2335  * Return Code: 0 for success (includes not reading for no_fdisk_present case
2336  *		errnos from tg_rw if failed to read the first block.
2337  *
2338  *     Context: Kernel thread only (can sleep).
2339  */
2340 /*ARGSUSED*/
2341 static int
2342 cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
2343 {
2344 #if defined(_NO_FDISK_PRESENT)
2345 
2346 	cl->cl_solaris_offset = 0;
2347 	cl->cl_solaris_size = capacity;
2348 	bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2349 	return (0);
2350 
2351 #elif defined(_FIRMWARE_NEEDS_FDISK)
2352 
2353 	struct ipart	*fdp;
2354 	struct mboot	*mbp;
2355 	struct ipart	fdisk[FD_NUMPART];
2356 	int		i, k;
2357 	char		sigbuf[2];
2358 	caddr_t		bufp;
2359 	int		uidx;
2360 	int 		rval;
2361 	int		lba = 0;
2362 	uint_t		solaris_offset;	/* offset to solaris part. */
2363 	daddr_t		solaris_size;	/* size of solaris partition */
2364 	uint32_t	blocksize;
2365 #if defined(__i386) || defined(__amd64)
2366 	struct ipart	eparts[2];
2367 	struct ipart	*efdp1 = &eparts[0];
2368 	struct ipart	*efdp2 = &eparts[1];
2369 	int		ext_part_exists = 0;
2370 	int		ld_count = 0;
2371 #endif
2372 
2373 	ASSERT(cl != NULL);
2374 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2375 
2376 	/*
2377 	 * Start off assuming no fdisk table
2378 	 */
2379 	solaris_offset = 0;
2380 	solaris_size   = capacity;
2381 
2382 	blocksize = cl->cl_tgt_blocksize;
2383 
2384 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
2385 
2386 	mutex_exit(CMLB_MUTEX(cl));
2387 	rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
2388 	mutex_enter(CMLB_MUTEX(cl));
2389 
2390 	if (rval != 0) {
2391 		cmlb_dbg(CMLB_ERROR,  cl,
2392 		    "cmlb_read_fdisk: fdisk read err\n");
2393 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2394 		goto done;
2395 	}
2396 
2397 	mbp = (struct mboot *)bufp;
2398 
2399 	/*
2400 	 * The fdisk table does not begin on a 4-byte boundary within the
2401 	 * master boot record, so we copy it to an aligned structure to avoid
2402 	 * alignment exceptions on some processors.
2403 	 */
2404 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2405 
2406 	/*
2407 	 * Check for lba support before verifying sig; sig might not be
2408 	 * there, say on a blank disk, but the max_chs mark may still
2409 	 * be present.
2410 	 *
2411 	 * Note: LBA support and BEFs are an x86-only concept but this
2412 	 * code should work OK on SPARC as well.
2413 	 */
2414 
2415 	/*
2416 	 * First, check for lba-access-ok on root node (or prom root node)
2417 	 * if present there, don't need to search fdisk table.
2418 	 */
2419 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
2420 	    "lba-access-ok", 0) != 0) {
2421 		/* All drives do LBA; don't search fdisk table */
2422 		lba = 1;
2423 	} else {
2424 		/* Okay, look for mark in fdisk table */
2425 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2426 			/* accumulate "lba" value from all partitions */
2427 			lba = (lba || cmlb_has_max_chs_vals(fdp));
2428 		}
2429 	}
2430 
2431 	if (lba != 0) {
2432 		dev_t dev = cmlb_make_device(cl);
2433 
2434 		if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
2435 		    "lba-access-ok", 0) == 0) {
2436 			/* not found; create it */
2437 			if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
2438 			    "lba-access-ok", (caddr_t)NULL, 0) !=
2439 			    DDI_PROP_SUCCESS) {
2440 				cmlb_dbg(CMLB_ERROR,  cl,
2441 				    "cmlb_read_fdisk: Can't create lba "
2442 				    "property for instance %d\n",
2443 				    ddi_get_instance(CMLB_DEVINFO(cl)));
2444 			}
2445 		}
2446 	}
2447 
2448 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
2449 
2450 	/*
2451 	 * Endian-independent signature check
2452 	 */
2453 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
2454 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
2455 		cmlb_dbg(CMLB_ERROR,  cl,
2456 		    "cmlb_read_fdisk: no fdisk\n");
2457 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2458 		goto done;
2459 	}
2460 
2461 #ifdef CMLBDEBUG
2462 	if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
2463 		fdp = fdisk;
2464 		cmlb_dbg(CMLB_INFO,  cl, "cmlb_read_fdisk:\n");
2465 		cmlb_dbg(CMLB_INFO,  cl, "         relsect    "
2466 		    "numsect         sysid       bootid\n");
2467 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
2468 			cmlb_dbg(CMLB_INFO,  cl,
2469 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
2470 			    i, fdp->relsect, fdp->numsect,
2471 			    fdp->systid, fdp->bootid);
2472 		}
2473 	}
2474 #endif
2475 
2476 	/*
2477 	 * Try to find the unix partition
2478 	 */
2479 	uidx = -1;
2480 	solaris_offset = 0;
2481 	solaris_size   = 0;
2482 
2483 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2484 		uint32_t relsect;
2485 		uint32_t numsect;
2486 		uchar_t systid;
2487 #if defined(__i386) || defined(__amd64)
2488 		/*
2489 		 * Stores relative block offset from the beginning of the
2490 		 * Extended Partition.
2491 		 */
2492 		int	ext_relsect = 0;
2493 #endif
2494 
2495 		if (fdp->numsect == 0) {
2496 			cl->cl_fmap[i].fmap_start = 0;
2497 			cl->cl_fmap[i].fmap_nblk  = 0;
2498 			continue;
2499 		}
2500 
2501 		/*
2502 		 * Data in the fdisk table is little-endian.
2503 		 */
2504 		relsect = LE_32(fdp->relsect);
2505 		numsect = LE_32(fdp->numsect);
2506 
2507 		cl->cl_fmap[i].fmap_start = relsect;
2508 		cl->cl_fmap[i].fmap_nblk  = numsect;
2509 		cl->cl_fmap[i].fmap_systid = LE_8(fdp->systid);
2510 
2511 #if defined(__i386) || defined(__amd64)
2512 		/* Support only one extended partition per LUN */
2513 		if ((fdp->systid == EXTDOS || fdp->systid == FDISK_EXTLBA) &&
2514 		    (ext_part_exists == 0)) {
2515 			int j;
2516 			uint32_t logdrive_offset;
2517 			uint32_t ext_numsect;
2518 			uint32_t abs_secnum;
2519 
2520 			ext_part_exists = 1;
2521 
2522 			for (j = FD_NUMPART; j < FDISK_PARTS; j++) {
2523 				mutex_exit(CMLB_MUTEX(cl));
2524 				rval = DK_TG_READ(cl, bufp,
2525 				    (relsect + ext_relsect), blocksize,
2526 				    tg_cookie);
2527 				mutex_enter(CMLB_MUTEX(cl));
2528 
2529 				if (rval != 0) {
2530 					cmlb_dbg(CMLB_ERROR,  cl,
2531 					    "cmlb_read_fdisk: Extended "
2532 					    "partition read err\n");
2533 					goto done;
2534 				}
2535 				/*
2536 				 * The first ipart entry provides the offset
2537 				 * at which the logical drive starts off from
2538 				 * the beginning of the container partition
2539 				 * and the size of the logical drive.
2540 				 * The second ipart entry provides the offset
2541 				 * of the next container partition from the
2542 				 * beginning of the extended partition.
2543 				 */
2544 				bcopy(&bufp[FDISK_PART_TABLE_START], eparts,
2545 				    sizeof (eparts));
2546 				logdrive_offset = LE_32(efdp1->relsect);
2547 				ext_numsect = LE_32(efdp1->numsect);
2548 				systid = LE_8(efdp1->systid);
2549 				if (logdrive_offset <= 0 || ext_numsect <= 0)
2550 					break;
2551 				abs_secnum = relsect + ext_relsect +
2552 				    logdrive_offset;
2553 
2554 				/* Boundary condition and overlap checking */
2555 				if (cmlb_validate_ext_part(cl, i, j, abs_secnum,
2556 				    ext_numsect)) {
2557 					break;
2558 				}
2559 
2560 				if ((cl->cl_fmap[j].fmap_start != abs_secnum) ||
2561 				    (cl->cl_fmap[j].fmap_nblk != ext_numsect) ||
2562 				    (cl->cl_fmap[j].fmap_systid != systid)) {
2563 					/*
2564 					 * Indicates change from previous
2565 					 * partinfo. Need to recreate
2566 					 * logical device nodes.
2567 					 */
2568 					cl->cl_update_ext_minor_nodes = 1;
2569 				}
2570 				cl->cl_fmap[j].fmap_start = abs_secnum;
2571 				cl->cl_fmap[j].fmap_nblk  = ext_numsect;
2572 				cl->cl_fmap[j].fmap_systid = systid;
2573 				ld_count++;
2574 
2575 				if ((efdp1->systid == SUNIXOS &&
2576 				    (cmlb_is_linux_swap(cl, abs_secnum,
2577 				    tg_cookie) != 0)) ||
2578 				    efdp1->systid == SUNIXOS2) {
2579 					if (uidx == -1) {
2580 						uidx = 0;
2581 						solaris_offset = abs_secnum;
2582 						solaris_size = ext_numsect;
2583 					}
2584 				}
2585 
2586 				if ((ext_relsect = LE_32(efdp2->relsect)) == 0)
2587 					break;
2588 			}
2589 		}
2590 
2591 #endif
2592 
2593 		if (fdp->systid != SUNIXOS &&
2594 		    fdp->systid != SUNIXOS2 &&
2595 		    fdp->systid != EFI_PMBR) {
2596 			continue;
2597 		}
2598 
2599 		/*
2600 		 * use the last active solaris partition id found
2601 		 * (there should only be 1 active partition id)
2602 		 *
2603 		 * if there are no active solaris partition id
2604 		 * then use the first inactive solaris partition id
2605 		 */
2606 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
2607 #if defined(__i386) || defined(__amd64)
2608 			if (fdp->systid != SUNIXOS ||
2609 			    (fdp->systid == SUNIXOS &&
2610 			    (cmlb_is_linux_swap(cl, relsect,
2611 			    tg_cookie) != 0))) {
2612 #endif
2613 				uidx = i;
2614 				solaris_offset = relsect;
2615 				solaris_size   = numsect;
2616 #if defined(__i386) || defined(__amd64)
2617 			}
2618 #endif
2619 		}
2620 	}
2621 #if defined(__i386) || defined(__amd64)
2622 	if (ld_count < cl->cl_logical_drive_count) {
2623 		/*
2624 		 * Some/all logical drives were deleted. Clear out
2625 		 * the fmap entries correspoding to those deleted drives.
2626 		 */
2627 		for (k = ld_count + FD_NUMPART;
2628 		    k < cl->cl_logical_drive_count + FD_NUMPART; k++) {
2629 			cl->cl_fmap[k].fmap_start = 0;
2630 			cl->cl_fmap[k].fmap_nblk  = 0;
2631 			cl->cl_fmap[k].fmap_systid = 0;
2632 		}
2633 		cl->cl_update_ext_minor_nodes = 1;
2634 	}
2635 	if (cl->cl_update_ext_minor_nodes) {
2636 		rval = cmlb_update_ext_minor_nodes(cl, ld_count);
2637 		if (rval != 0) {
2638 			goto done;
2639 		}
2640 	}
2641 #endif
2642 	cmlb_dbg(CMLB_INFO,  cl, "fdisk 0x%x 0x%lx",
2643 	    cl->cl_solaris_offset, cl->cl_solaris_size);
2644 done:
2645 
2646 	/*
2647 	 * Clear the VTOC info, only if the Solaris partition entry
2648 	 * has moved, changed size, been deleted, or if the size of
2649 	 * the partition is too small to even fit the label sector.
2650 	 */
2651 	if ((cl->cl_solaris_offset != solaris_offset) ||
2652 	    (cl->cl_solaris_size != solaris_size) ||
2653 	    solaris_size <= DK_LABEL_LOC) {
2654 		cmlb_dbg(CMLB_INFO,  cl, "fdisk moved 0x%x 0x%lx",
2655 		    solaris_offset, solaris_size);
2656 		bzero(&cl->cl_g, sizeof (struct dk_geom));
2657 		bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2658 		bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
2659 		cl->cl_f_geometry_is_valid = B_FALSE;
2660 	}
2661 	cl->cl_solaris_offset = solaris_offset;
2662 	cl->cl_solaris_size = solaris_size;
2663 	kmem_free(bufp, blocksize);
2664 	return (rval);
2665 
2666 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
2667 #error "fdisk table presence undetermined for this platform."
2668 #endif	/* #if defined(_NO_FDISK_PRESENT) */
2669 }
2670 
2671 static void
2672 cmlb_swap_efi_gpt(efi_gpt_t *e)
2673 {
2674 	_NOTE(ASSUMING_PROTECTED(*e))
2675 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
2676 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
2677 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
2678 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
2679 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
2680 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
2681 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
2682 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
2683 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
2684 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
2685 	e->efi_gpt_NumberOfPartitionEntries =
2686 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
2687 	e->efi_gpt_SizeOfPartitionEntry =
2688 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
2689 	e->efi_gpt_PartitionEntryArrayCRC32 =
2690 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
2691 }
2692 
2693 static void
2694 cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
2695 {
2696 	int i;
2697 
2698 	_NOTE(ASSUMING_PROTECTED(*p))
2699 	for (i = 0; i < nparts; i++) {
2700 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
2701 		    p[i].efi_gpe_PartitionTypeGUID);
2702 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
2703 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
2704 		/* PartitionAttrs */
2705 	}
2706 }
2707 
2708 static int
2709 cmlb_validate_efi(efi_gpt_t *labp)
2710 {
2711 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
2712 		return (EINVAL);
2713 	/* at least 96 bytes in this version of the spec. */
2714 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
2715 	    labp->efi_gpt_HeaderSize)
2716 		return (EINVAL);
2717 	/* this should be 128 bytes */
2718 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
2719 		return (EINVAL);
2720 	return (0);
2721 }
2722 
2723 /*
2724  * This function returns B_FALSE if there is a valid MBR signature and no
2725  * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2726  *
2727  * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2728  * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2729  * where a PMBR entry is not the only one. Also, if the first block has been
2730  * corrupted, currently best attempt to allow data access would be to try to
2731  * check for GPT headers. Hence in case of more than one partition entry, but
2732  * at least one EFI_PMBR partition type or no valid magic number, the function
2733  * returns B_TRUE to continue with looking for GPT header.
2734  */
2735 
2736 static boolean_t
2737 cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2738 {
2739 	struct ipart	*fdp;
2740 	struct mboot	*mbp = (struct mboot *)buf;
2741 	struct ipart	fdisk[FD_NUMPART];
2742 	int		i;
2743 
2744 	if (is_mbr != NULL)
2745 		*is_mbr = B_TRUE;
2746 
2747 	if (LE_16(mbp->signature) != MBB_MAGIC) {
2748 		if (is_mbr != NULL)
2749 			*is_mbr = B_FALSE;
2750 		return (B_TRUE);
2751 	}
2752 
2753 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2754 
2755 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2756 		if (fdp->systid == EFI_PMBR)
2757 			return (B_TRUE);
2758 	}
2759 
2760 	return (B_FALSE);
2761 }
2762 
2763 static int
2764 cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2765     void *tg_cookie)
2766 {
2767 	int		i;
2768 	int		rval = 0;
2769 	efi_gpe_t	*partitions;
2770 	uchar_t		*buf;
2771 	uint_t		lbasize;	/* is really how much to read */
2772 	diskaddr_t	cap = 0;
2773 	uint_t		nparts;
2774 	diskaddr_t	gpe_lba;
2775 	diskaddr_t	alternate_lba;
2776 	int		iofailed = 0;
2777 	struct uuid	uuid_type_reserved = EFI_RESERVED;
2778 #if defined(_FIRMWARE_NEEDS_FDISK)
2779 	boolean_t 	is_mbr;
2780 #endif
2781 
2782 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2783 
2784 	lbasize = cl->cl_sys_blocksize;
2785 
2786 	cl->cl_reserved = -1;
2787 	mutex_exit(CMLB_MUTEX(cl));
2788 
2789 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
2790 
2791 	rval = DK_TG_READ(cl, buf,  0, lbasize, tg_cookie);
2792 	if (rval) {
2793 		iofailed = 1;
2794 		goto done_err;
2795 	}
2796 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
2797 		/* not ours */
2798 		rval = ESRCH;
2799 		goto done_err;
2800 	}
2801 
2802 #if defined(_FIRMWARE_NEEDS_FDISK)
2803 	if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
2804 		if (is_mbr)
2805 			rval = ESRCH;
2806 		else
2807 			rval = EINVAL;
2808 		goto done_err;
2809 	}
2810 #else
2811 	if (!cmlb_check_efi_mbr(buf, NULL)) {
2812 		rval = EINVAL;
2813 		goto done_err;
2814 	}
2815 
2816 #endif
2817 
2818 	rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
2819 	if (rval) {
2820 		iofailed = 1;
2821 		goto done_err;
2822 	}
2823 	cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2824 
2825 	if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2826 		/*
2827 		 * Couldn't read the primary, try the backup.  Our
2828 		 * capacity at this point could be based on CHS, so
2829 		 * check what the device reports.
2830 		 */
2831 		rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
2832 		if (rval) {
2833 			iofailed = 1;
2834 			goto done_err;
2835 		}
2836 
2837 		/*
2838 		 * CMLB_OFF_BY_ONE case, we check the next to last block first
2839 		 * for backup GPT header, otherwise check the last block.
2840 		 */
2841 
2842 		if ((rval = DK_TG_READ(cl, buf,
2843 		    cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2844 		    lbasize, tg_cookie))
2845 		    != 0) {
2846 			iofailed = 1;
2847 			goto done_err;
2848 		}
2849 		cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2850 
2851 		if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2852 
2853 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2854 				goto done_err;
2855 			if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2856 			    tg_cookie)) != 0)
2857 				goto done_err;
2858 			cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2859 			if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
2860 				goto done_err;
2861 		}
2862 		if (!(flags & CMLB_SILENT))
2863 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2864 			    "primary label corrupt; using backup\n");
2865 	}
2866 
2867 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
2868 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2869 	alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
2870 
2871 	rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
2872 	if (rval) {
2873 		iofailed = 1;
2874 		goto done_err;
2875 	}
2876 	partitions = (efi_gpe_t *)buf;
2877 
2878 	if (nparts > MAXPART) {
2879 		nparts = MAXPART;
2880 	}
2881 	cmlb_swap_efi_gpe(nparts, partitions);
2882 
2883 	mutex_enter(CMLB_MUTEX(cl));
2884 
2885 	/* Fill in partition table. */
2886 	for (i = 0; i < nparts; i++) {
2887 		if (partitions->efi_gpe_StartingLBA != 0 ||
2888 		    partitions->efi_gpe_EndingLBA != 0) {
2889 			cl->cl_map[i].dkl_cylno =
2890 			    partitions->efi_gpe_StartingLBA;
2891 			cl->cl_map[i].dkl_nblk =
2892 			    partitions->efi_gpe_EndingLBA -
2893 			    partitions->efi_gpe_StartingLBA + 1;
2894 			cl->cl_offset[i] =
2895 			    partitions->efi_gpe_StartingLBA;
2896 		}
2897 
2898 		if (cl->cl_reserved == -1) {
2899 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2900 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2901 				cl->cl_reserved = i;
2902 			}
2903 		}
2904 		if (i == WD_NODE) {
2905 			/*
2906 			 * minor number 7 corresponds to the whole disk
2907 			 * if the disk capacity is expanded after disk is
2908 			 * labeled, minor number 7 represents the capacity
2909 			 * indicated by the disk label.
2910 			 */
2911 			cl->cl_map[i].dkl_cylno = 0;
2912 			if (alternate_lba == 1) {
2913 				/*
2914 				 * We are using backup label. Since we can
2915 				 * find a valid label at the end of disk,
2916 				 * the disk capacity is not expanded.
2917 				 */
2918 				cl->cl_map[i].dkl_nblk = capacity;
2919 			} else {
2920 				cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2921 			}
2922 			cl->cl_offset[i] = 0;
2923 		}
2924 		partitions++;
2925 	}
2926 	cl->cl_solaris_offset = 0;
2927 	cl->cl_solaris_size = capacity;
2928 	cl->cl_label_from_media = CMLB_LABEL_EFI;
2929 	cl->cl_f_geometry_is_valid = B_TRUE;
2930 
2931 	/* clear the vtoc label */
2932 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2933 
2934 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2935 	return (0);
2936 
2937 done_err:
2938 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2939 	mutex_enter(CMLB_MUTEX(cl));
2940 done_err1:
2941 	/*
2942 	 * if we didn't find something that could look like a VTOC
2943 	 * and the disk is over 1TB, we know there isn't a valid label.
2944 	 * Otherwise let cmlb_uselabel decide what to do.  We only
2945 	 * want to invalidate this if we're certain the label isn't
2946 	 * valid because cmlb_prop_op will now fail, which in turn
2947 	 * causes things like opens and stats on the partition to fail.
2948 	 */
2949 	if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
2950 		cl->cl_f_geometry_is_valid = B_FALSE;
2951 	}
2952 	return (rval);
2953 }
2954 
2955 
2956 /*
2957  *    Function: cmlb_uselabel
2958  *
2959  * Description: Validate the disk label and update the relevant data (geometry,
2960  *		partition, vtoc, and capacity data) in the cmlb_lun struct.
2961  *		Marks the geometry of the unit as being valid.
2962  *
2963  *   Arguments: cl: unit struct.
2964  *		dk_label: disk label
2965  *
2966  * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
2967  *		partition, vtoc, and capacity data are good.
2968  *
2969  *		CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
2970  *		label; or computed capacity does not jibe with capacity
2971  *		reported from the READ CAPACITY command.
2972  *
2973  *     Context: Kernel thread only (can sleep).
2974  */
2975 static int
2976 cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
2977 {
2978 	short		*sp;
2979 	short		sum;
2980 	short		count;
2981 	int		label_error = CMLB_LABEL_IS_VALID;
2982 	int		i;
2983 	diskaddr_t	label_capacity;
2984 	uint32_t	part_end;
2985 	diskaddr_t	track_capacity;
2986 #if defined(_SUNOS_VTOC_16)
2987 	struct	dkl_partition	*vpartp;
2988 #endif
2989 	ASSERT(cl != NULL);
2990 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2991 
2992 	/* Validate the magic number of the label. */
2993 	if (labp->dkl_magic != DKL_MAGIC) {
2994 #if defined(__sparc)
2995 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2996 			if (!(flags & CMLB_SILENT))
2997 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2998 				    CE_WARN,
2999 				    "Corrupt label; wrong magic number\n");
3000 		}
3001 #endif
3002 		return (CMLB_LABEL_IS_INVALID);
3003 	}
3004 
3005 	/* Validate the checksum of the label. */
3006 	sp  = (short *)labp;
3007 	sum = 0;
3008 	count = sizeof (struct dk_label) / sizeof (short);
3009 	while (count--)	 {
3010 		sum ^= *sp++;
3011 	}
3012 
3013 	if (sum != 0) {
3014 #if defined(_SUNOS_VTOC_16)
3015 		if (!ISCD(cl)) {
3016 #elif defined(_SUNOS_VTOC_8)
3017 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3018 #endif
3019 			if (!(flags & CMLB_SILENT))
3020 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3021 				    CE_WARN,
3022 				    "Corrupt label - label checksum failed\n");
3023 		}
3024 		return (CMLB_LABEL_IS_INVALID);
3025 	}
3026 
3027 
3028 	/*
3029 	 * Fill in geometry structure with data from label.
3030 	 */
3031 	bzero(&cl->cl_g, sizeof (struct dk_geom));
3032 	cl->cl_g.dkg_ncyl   = labp->dkl_ncyl;
3033 	cl->cl_g.dkg_acyl   = labp->dkl_acyl;
3034 	cl->cl_g.dkg_bcyl   = 0;
3035 	cl->cl_g.dkg_nhead  = labp->dkl_nhead;
3036 	cl->cl_g.dkg_nsect  = labp->dkl_nsect;
3037 	cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
3038 
3039 #if defined(_SUNOS_VTOC_8)
3040 	cl->cl_g.dkg_gap1   = labp->dkl_gap1;
3041 	cl->cl_g.dkg_gap2   = labp->dkl_gap2;
3042 	cl->cl_g.dkg_bhead  = labp->dkl_bhead;
3043 #endif
3044 #if defined(_SUNOS_VTOC_16)
3045 	cl->cl_dkg_skew = labp->dkl_skew;
3046 #endif
3047 
3048 #if defined(__i386) || defined(__amd64)
3049 	cl->cl_g.dkg_apc = labp->dkl_apc;
3050 #endif
3051 
3052 	/*
3053 	 * Currently we rely on the values in the label being accurate. If
3054 	 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
3055 	 *
3056 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
3057 	 * although this command is optional in SCSI-2.
3058 	 */
3059 	cl->cl_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
3060 	cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
3061 	    (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
3062 
3063 	/*
3064 	 * The Read and Write reinstruct values may not be valid
3065 	 * for older disks.
3066 	 */
3067 	cl->cl_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
3068 	cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
3069 
3070 	/* Fill in partition table. */
3071 #if defined(_SUNOS_VTOC_8)
3072 	for (i = 0; i < NDKMAP; i++) {
3073 		cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
3074 		cl->cl_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
3075 	}
3076 #endif
3077 #if  defined(_SUNOS_VTOC_16)
3078 	vpartp		= labp->dkl_vtoc.v_part;
3079 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
3080 
3081 	/* Prevent divide by zero */
3082 	if (track_capacity == 0) {
3083 		if (!(flags & CMLB_SILENT))
3084 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3085 			    "Corrupt label - zero nhead or nsect value\n");
3086 
3087 		return (CMLB_LABEL_IS_INVALID);
3088 	}
3089 
3090 	for (i = 0; i < NDKMAP; i++, vpartp++) {
3091 		cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
3092 		cl->cl_map[i].dkl_nblk  = vpartp->p_size;
3093 	}
3094 #endif
3095 
3096 	/* Fill in VTOC Structure. */
3097 	bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
3098 #if defined(_SUNOS_VTOC_8)
3099 	/*
3100 	 * The 8-slice vtoc does not include the ascii label; save it into
3101 	 * the device's soft state structure here.
3102 	 */
3103 	bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
3104 #endif
3105 
3106 	/* Now look for a valid capacity. */
3107 	track_capacity	= (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
3108 	label_capacity	= (cl->cl_g.dkg_ncyl  * track_capacity);
3109 
3110 	if (cl->cl_g.dkg_acyl) {
3111 #if defined(__i386) || defined(__amd64)
3112 		/* we may have > 1 alts cylinder */
3113 		label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
3114 #else
3115 		label_capacity += track_capacity;
3116 #endif
3117 	}
3118 
3119 	/*
3120 	 * Force check here to ensure the computed capacity is valid.
3121 	 * If capacity is zero, it indicates an invalid label and
3122 	 * we should abort updating the relevant data then.
3123 	 */
3124 	if (label_capacity == 0) {
3125 		if (!(flags & CMLB_SILENT))
3126 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3127 			    "Corrupt label - no valid capacity could be "
3128 			    "retrieved\n");
3129 
3130 		return (CMLB_LABEL_IS_INVALID);
3131 	}
3132 
3133 	/* Mark the geometry as valid. */
3134 	cl->cl_f_geometry_is_valid = B_TRUE;
3135 
3136 	/*
3137 	 * if we got invalidated when mutex exit and entered again,
3138 	 * if blockcount different than when we came in, need to
3139 	 * retry from beginning of cmlb_validate_geometry.
3140 	 * revisit this on next phase of utilizing this for
3141 	 * sd.
3142 	 */
3143 
3144 	if (label_capacity <= cl->cl_blockcount) {
3145 #if defined(_SUNOS_VTOC_8)
3146 		/*
3147 		 * We can't let this happen on drives that are subdivided
3148 		 * into logical disks (i.e., that have an fdisk table).
3149 		 * The cl_blockcount field should always hold the full media
3150 		 * size in sectors, period.  This code would overwrite
3151 		 * cl_blockcount with the size of the Solaris fdisk partition.
3152 		 */
3153 		cmlb_dbg(CMLB_ERROR,  cl,
3154 		    "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3155 		    label_capacity, cl->cl_blockcount);
3156 		cl->cl_solaris_size = label_capacity;
3157 
3158 #endif	/* defined(_SUNOS_VTOC_8) */
3159 		goto done;
3160 	}
3161 
3162 	if (ISCD(cl)) {
3163 		/* For CDROMs, we trust that the data in the label is OK. */
3164 #if defined(_SUNOS_VTOC_8)
3165 		for (i = 0; i < NDKMAP; i++) {
3166 			part_end = labp->dkl_nhead * labp->dkl_nsect *
3167 			    labp->dkl_map[i].dkl_cylno +
3168 			    labp->dkl_map[i].dkl_nblk  - 1;
3169 
3170 			if ((labp->dkl_map[i].dkl_nblk) &&
3171 			    (part_end > cl->cl_blockcount)) {
3172 				cl->cl_f_geometry_is_valid = B_FALSE;
3173 				break;
3174 			}
3175 		}
3176 #endif
3177 #if defined(_SUNOS_VTOC_16)
3178 		vpartp = &(labp->dkl_vtoc.v_part[0]);
3179 		for (i = 0; i < NDKMAP; i++, vpartp++) {
3180 			part_end = vpartp->p_start + vpartp->p_size;
3181 			if ((vpartp->p_size > 0) &&
3182 			    (part_end > cl->cl_blockcount)) {
3183 				cl->cl_f_geometry_is_valid = B_FALSE;
3184 				break;
3185 			}
3186 		}
3187 #endif
3188 	} else {
3189 		/* label_capacity > cl->cl_blockcount */
3190 		if (!(flags & CMLB_SILENT)) {
3191 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3192 			    "Corrupt label - bad geometry\n");
3193 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
3194 			    "Label says %llu blocks; Drive says %llu blocks\n",
3195 			    label_capacity, cl->cl_blockcount);
3196 		}
3197 		cl->cl_f_geometry_is_valid = B_FALSE;
3198 		label_error = CMLB_LABEL_IS_INVALID;
3199 	}
3200 
3201 done:
3202 
3203 	cmlb_dbg(CMLB_INFO,  cl, "cmlb_uselabel: (label geometry)\n");
3204 	cmlb_dbg(CMLB_INFO,  cl,
3205 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3206 	    cl->cl_g.dkg_ncyl,  cl->cl_g.dkg_acyl,
3207 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3208 
3209 	cmlb_dbg(CMLB_INFO,  cl,
3210 	    "   label_capacity: %d; intrlv: %d; rpm: %d\n",
3211 	    cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
3212 	cmlb_dbg(CMLB_INFO,  cl, "   wrt_reinstr: %d; rd_reinstr: %d\n",
3213 	    cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
3214 
3215 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3216 
3217 	return (label_error);
3218 }
3219 
3220 
3221 /*
3222  *    Function: cmlb_build_default_label
3223  *
3224  * Description: Generate a default label for those devices that do not have
3225  *		one, e.g., new media, removable cartridges, etc..
3226  *
3227  *     Context: Kernel thread only
3228  */
3229 /*ARGSUSED*/
3230 static void
3231 cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
3232 {
3233 #if defined(_SUNOS_VTOC_16)
3234 	uint_t	phys_spc;
3235 	uint_t	disksize;
3236 	struct  dk_geom cl_g;
3237 	diskaddr_t capacity;
3238 #endif
3239 
3240 	ASSERT(cl != NULL);
3241 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3242 
3243 #if defined(_SUNOS_VTOC_8)
3244 	/*
3245 	 * Note: This is a legacy check for non-removable devices on VTOC_8
3246 	 * only. This may be a valid check for VTOC_16 as well.
3247 	 * Once we understand why there is this difference between SPARC and
3248 	 * x86 platform, we could remove this legacy check.
3249 	 */
3250 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3251 		return;
3252 	}
3253 #endif
3254 
3255 	bzero(&cl->cl_g, sizeof (struct dk_geom));
3256 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3257 	bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
3258 
3259 #if defined(_SUNOS_VTOC_8)
3260 
3261 	/*
3262 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
3263 	 * But it is still necessary to set up various geometry information,
3264 	 * and we are doing this here.
3265 	 */
3266 
3267 	/*
3268 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
3269 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
3270 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
3271 	 * equal to C*H*S values.  This will cause some truncation of size due
3272 	 * to round off errors. For CD-ROMs, this truncation can have adverse
3273 	 * side effects, so returning ncyl and nhead as 1. The nsect will
3274 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
3275 	 */
3276 	cl->cl_solaris_size = cl->cl_blockcount;
3277 	if (ISCD(cl)) {
3278 		tg_attribute_t tgattribute;
3279 		int is_writable;
3280 		/*
3281 		 * Preserve the old behavior for non-writable
3282 		 * medias. Since dkg_nsect is a ushort, it
3283 		 * will lose bits as cdroms have more than
3284 		 * 65536 sectors. So if we recalculate
3285 		 * capacity, it will become much shorter.
3286 		 * But the dkg_* information is not
3287 		 * used for CDROMs so it is OK. But for
3288 		 * Writable CDs we need this information
3289 		 * to be valid (for newfs say). So we
3290 		 * make nsect and nhead > 1 that way
3291 		 * nsect can still stay within ushort limit
3292 		 * without losing any bits.
3293 		 */
3294 
3295 		bzero(&tgattribute, sizeof (tg_attribute_t));
3296 
3297 		mutex_exit(CMLB_MUTEX(cl));
3298 		is_writable =
3299 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
3300 		    tgattribute.media_is_writable : 1;
3301 		mutex_enter(CMLB_MUTEX(cl));
3302 
3303 		if (is_writable) {
3304 			cl->cl_g.dkg_nhead = 64;
3305 			cl->cl_g.dkg_nsect = 32;
3306 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3307 			cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
3308 			    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3309 		} else {
3310 			cl->cl_g.dkg_ncyl  = 1;
3311 			cl->cl_g.dkg_nhead = 1;
3312 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
3313 		}
3314 	} else {
3315 		if (cl->cl_blockcount < 160) {
3316 			/* Less than 80K */
3317 			cl->cl_g.dkg_nhead = 1;
3318 			cl->cl_g.dkg_ncyl = cl->cl_blockcount;
3319 			cl->cl_g.dkg_nsect = 1;
3320 		} else if (cl->cl_blockcount <= 0x1000) {
3321 			/* unlabeled SCSI floppy device */
3322 			cl->cl_g.dkg_nhead = 2;
3323 			cl->cl_g.dkg_ncyl = 80;
3324 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
3325 		} else if (cl->cl_blockcount <= 0x200000) {
3326 			cl->cl_g.dkg_nhead = 64;
3327 			cl->cl_g.dkg_nsect = 32;
3328 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (64 * 32);
3329 		} else {
3330 			cl->cl_g.dkg_nhead = 255;
3331 
3332 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
3333 			    (UINT16_MAX * 255 * 63) - 1) /
3334 			    (UINT16_MAX * 255 * 63)) * 63;
3335 
3336 			if (cl->cl_g.dkg_nsect == 0)
3337 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
3338 
3339 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
3340 			    (255 * cl->cl_g.dkg_nsect);
3341 		}
3342 
3343 		cl->cl_solaris_size =
3344 		    (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
3345 		    cl->cl_g.dkg_nsect;
3346 
3347 	}
3348 
3349 	cl->cl_g.dkg_acyl	= 0;
3350 	cl->cl_g.dkg_bcyl	= 0;
3351 	cl->cl_g.dkg_rpm	= 200;
3352 	cl->cl_asciilabel[0]	= '\0';
3353 	cl->cl_g.dkg_pcyl	= cl->cl_g.dkg_ncyl;
3354 
3355 	cl->cl_map[0].dkl_cylno = 0;
3356 	cl->cl_map[0].dkl_nblk  = cl->cl_solaris_size;
3357 
3358 	cl->cl_map[2].dkl_cylno = 0;
3359 	cl->cl_map[2].dkl_nblk  = cl->cl_solaris_size;
3360 
3361 #elif defined(_SUNOS_VTOC_16)
3362 
3363 	if (cl->cl_solaris_size == 0) {
3364 		/*
3365 		 * Got fdisk table but no solaris entry therefore
3366 		 * don't create a default label
3367 		 */
3368 		cl->cl_f_geometry_is_valid = B_TRUE;
3369 		return;
3370 	}
3371 
3372 	/*
3373 	 * For CDs we continue to use the physical geometry to calculate
3374 	 * number of cylinders. All other devices must convert the
3375 	 * physical geometry (cmlb_geom) to values that will fit
3376 	 * in a dk_geom structure.
3377 	 */
3378 	if (ISCD(cl)) {
3379 		phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
3380 	} else {
3381 		/* Convert physical geometry to disk geometry */
3382 		bzero(&cl_g, sizeof (struct dk_geom));
3383 
3384 		/*
3385 		 * Refer to comments related to off-by-1 at the
3386 		 * header of this file.
3387 		 * Before calculating geometry, capacity should be
3388 		 * decreased by 1.
3389 		 */
3390 
3391 		if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
3392 			capacity = cl->cl_blockcount - 1;
3393 		else
3394 			capacity = cl->cl_blockcount;
3395 
3396 
3397 		cmlb_convert_geometry(cl, capacity, &cl_g, tg_cookie);
3398 		bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
3399 		phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3400 	}
3401 
3402 	if (phys_spc == 0)
3403 		return;
3404 	cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
3405 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3406 		/* disable devid */
3407 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
3408 		disksize = cl->cl_solaris_size;
3409 	} else {
3410 		cl->cl_g.dkg_acyl = DK_ACYL;
3411 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
3412 		disksize = cl->cl_g.dkg_ncyl * phys_spc;
3413 	}
3414 
3415 	if (ISCD(cl)) {
3416 		/*
3417 		 * CD's don't use the "heads * sectors * cyls"-type of
3418 		 * geometry, but instead use the entire capacity of the media.
3419 		 */
3420 		disksize = cl->cl_solaris_size;
3421 		cl->cl_g.dkg_nhead = 1;
3422 		cl->cl_g.dkg_nsect = 1;
3423 		cl->cl_g.dkg_rpm =
3424 		    (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
3425 
3426 		cl->cl_vtoc.v_part[0].p_start = 0;
3427 		cl->cl_vtoc.v_part[0].p_size  = disksize;
3428 		cl->cl_vtoc.v_part[0].p_tag   = V_BACKUP;
3429 		cl->cl_vtoc.v_part[0].p_flag  = V_UNMNT;
3430 
3431 		cl->cl_map[0].dkl_cylno = 0;
3432 		cl->cl_map[0].dkl_nblk  = disksize;
3433 		cl->cl_offset[0] = 0;
3434 
3435 	} else {
3436 		/*
3437 		 * Hard disks and removable media cartridges
3438 		 */
3439 		cl->cl_g.dkg_rpm =
3440 		    (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
3441 		cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
3442 
3443 		/* Add boot slice */
3444 		cl->cl_vtoc.v_part[8].p_start = 0;
3445 		cl->cl_vtoc.v_part[8].p_size  = phys_spc;
3446 		cl->cl_vtoc.v_part[8].p_tag   = V_BOOT;
3447 		cl->cl_vtoc.v_part[8].p_flag  = V_UNMNT;
3448 
3449 		cl->cl_map[8].dkl_cylno = 0;
3450 		cl->cl_map[8].dkl_nblk  = phys_spc;
3451 		cl->cl_offset[8] = 0;
3452 
3453 		if ((cl->cl_alter_behavior &
3454 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
3455 		    cl->cl_device_type == DTYPE_DIRECT) {
3456 			cl->cl_vtoc.v_part[9].p_start = phys_spc;
3457 			cl->cl_vtoc.v_part[9].p_size  = 2 * phys_spc;
3458 			cl->cl_vtoc.v_part[9].p_tag   = V_ALTSCTR;
3459 			cl->cl_vtoc.v_part[9].p_flag  = 0;
3460 
3461 			cl->cl_map[9].dkl_cylno = 1;
3462 			cl->cl_map[9].dkl_nblk  = 2 * phys_spc;
3463 			cl->cl_offset[9] = phys_spc;
3464 		}
3465 	}
3466 
3467 	cl->cl_g.dkg_apc = 0;
3468 
3469 	/* Add backup slice */
3470 	cl->cl_vtoc.v_part[2].p_start = 0;
3471 	cl->cl_vtoc.v_part[2].p_size  = disksize;
3472 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
3473 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
3474 
3475 	cl->cl_map[2].dkl_cylno = 0;
3476 	cl->cl_map[2].dkl_nblk  = disksize;
3477 	cl->cl_offset[2] = 0;
3478 
3479 	/*
3480 	 * single slice (s0) covering the entire disk
3481 	 */
3482 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3483 		cl->cl_vtoc.v_part[0].p_start = 0;
3484 		cl->cl_vtoc.v_part[0].p_tag   = V_UNASSIGNED;
3485 		cl->cl_vtoc.v_part[0].p_flag  = 0;
3486 		cl->cl_vtoc.v_part[0].p_size  = disksize;
3487 		cl->cl_map[0].dkl_cylno = 0;
3488 		cl->cl_map[0].dkl_nblk  = disksize;
3489 		cl->cl_offset[0] = 0;
3490 	}
3491 
3492 	(void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
3493 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3494 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3495 
3496 #else
3497 #error "No VTOC format defined."
3498 #endif
3499 
3500 	cl->cl_g.dkg_read_reinstruct  = 0;
3501 	cl->cl_g.dkg_write_reinstruct = 0;
3502 
3503 	cl->cl_g.dkg_intrlv = 1;
3504 
3505 	cl->cl_vtoc.v_sanity  = VTOC_SANE;
3506 	cl->cl_vtoc.v_nparts = V_NUMPAR;
3507 	cl->cl_vtoc.v_version = V_VERSION;
3508 
3509 	cl->cl_f_geometry_is_valid = B_TRUE;
3510 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
3511 
3512 	cmlb_dbg(CMLB_INFO,  cl,
3513 	    "cmlb_build_default_label: Default label created: "
3514 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3515 	    cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
3516 	    cl->cl_g.dkg_nsect, cl->cl_blockcount);
3517 }
3518 
3519 
3520 #if defined(_FIRMWARE_NEEDS_FDISK)
3521 /*
3522  * Max CHS values, as they are encoded into bytes, for 1022/254/63
3523  */
3524 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
3525 #define	LBA_MAX_CYL	(1022 & 0xFF)
3526 #define	LBA_MAX_HEAD	(254)
3527 
3528 
3529 /*
3530  *    Function: cmlb_has_max_chs_vals
3531  *
3532  * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
3533  *
3534  *   Arguments: fdp - ptr to CHS info
3535  *
3536  * Return Code: True or false
3537  *
3538  *     Context: Any.
3539  */
3540 static boolean_t
3541 cmlb_has_max_chs_vals(struct ipart *fdp)
3542 {
3543 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
3544 	    (fdp->beghead == LBA_MAX_HEAD)	&&
3545 	    (fdp->begsect == LBA_MAX_SECT)	&&
3546 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
3547 	    (fdp->endhead == LBA_MAX_HEAD)	&&
3548 	    (fdp->endsect == LBA_MAX_SECT));
3549 }
3550 #endif
3551 
3552 /*
3553  *    Function: cmlb_dkio_get_geometry
3554  *
3555  * Description: This routine is the driver entry point for handling user
3556  *		requests to get the device geometry (DKIOCGGEOM).
3557  *
3558  *   Arguments:
3559  *	arg		pointer to user provided dk_geom structure specifying
3560  *			the controller's notion of the current geometry.
3561  *
3562  *	flag 		this argument is a pass through to ddi_copyxxx()
3563  *			directly from the mode argument of ioctl().
3564  *
3565  *	tg_cookie	cookie from target driver to be passed back to target
3566  *			driver when we call back to it through tg_ops.
3567  *
3568  * Return Code: 0
3569  *		EFAULT
3570  *		ENXIO
3571  *		EIO
3572  */
3573 static int
3574 cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
3575     void *tg_cookie)
3576 {
3577 	struct dk_geom	*tmp_geom = NULL;
3578 	int		rval = 0;
3579 
3580 	/*
3581 	 * cmlb_validate_geometry does not spin a disk up
3582 	 * if it was spcl down. We need to make sure it
3583 	 * is ready.
3584 	 */
3585 	mutex_enter(CMLB_MUTEX(cl));
3586 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3587 #if defined(_SUNOS_VTOC_8)
3588 	if (rval == EINVAL &&
3589 	    cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
3590 		/*
3591 		 * This is to return a default label geometry even when we
3592 		 * do not really assume a default label for the device.
3593 		 * dad driver utilizes this.
3594 		 */
3595 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3596 			cmlb_setup_default_geometry(cl, tg_cookie);
3597 			rval = 0;
3598 		}
3599 	}
3600 #endif
3601 	if (rval) {
3602 		mutex_exit(CMLB_MUTEX(cl));
3603 		return (rval);
3604 	}
3605 
3606 #if defined(__i386) || defined(__amd64)
3607 	if (cl->cl_solaris_size == 0) {
3608 		mutex_exit(CMLB_MUTEX(cl));
3609 		return (EIO);
3610 	}
3611 #endif
3612 
3613 	/*
3614 	 * Make a local copy of the soft state geometry to avoid some potential
3615 	 * race conditions associated with holding the mutex and updating the
3616 	 * write_reinstruct value
3617 	 */
3618 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3619 	bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
3620 
3621 	if (tmp_geom->dkg_write_reinstruct == 0) {
3622 		tmp_geom->dkg_write_reinstruct =
3623 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
3624 		    cmlb_rot_delay) / (int)60000);
3625 	}
3626 	mutex_exit(CMLB_MUTEX(cl));
3627 
3628 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
3629 	    flag);
3630 	if (rval != 0) {
3631 		rval = EFAULT;
3632 	}
3633 
3634 	kmem_free(tmp_geom, sizeof (struct dk_geom));
3635 	return (rval);
3636 
3637 }
3638 
3639 
3640 /*
3641  *    Function: cmlb_dkio_set_geometry
3642  *
3643  * Description: This routine is the driver entry point for handling user
3644  *		requests to set the device geometry (DKIOCSGEOM). The actual
3645  *		device geometry is not updated, just the driver "notion" of it.
3646  *
3647  *   Arguments:
3648  *	arg		pointer to user provided dk_geom structure used to set
3649  *			the controller's notion of the current geometry.
3650  *
3651  *	flag 		this argument is a pass through to ddi_copyxxx()
3652  *			directly from the mode argument of ioctl().
3653  *
3654  *	tg_cookie	cookie from target driver to be passed back to target
3655  *			driver when we call back to it through tg_ops.
3656  *
3657  * Return Code: 0
3658  *		EFAULT
3659  *		ENXIO
3660  *		EIO
3661  */
3662 static int
3663 cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
3664 {
3665 	struct dk_geom	*tmp_geom;
3666 	struct dk_map	*lp;
3667 	int		rval = 0;
3668 	int		i;
3669 
3670 
3671 #if defined(__i386) || defined(__amd64)
3672 	if (cl->cl_solaris_size == 0) {
3673 		return (EIO);
3674 	}
3675 #endif
3676 	/*
3677 	 * We need to copy the user specified geometry into local
3678 	 * storage and then update the softstate. We don't want to hold
3679 	 * the mutex and copyin directly from the user to the soft state
3680 	 */
3681 	tmp_geom = (struct dk_geom *)
3682 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3683 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
3684 	if (rval != 0) {
3685 		kmem_free(tmp_geom, sizeof (struct dk_geom));
3686 		return (EFAULT);
3687 	}
3688 
3689 	mutex_enter(CMLB_MUTEX(cl));
3690 	bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
3691 	for (i = 0; i < NDKMAP; i++) {
3692 		lp  = &cl->cl_map[i];
3693 		cl->cl_offset[i] =
3694 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3695 #if defined(__i386) || defined(__amd64)
3696 		cl->cl_offset[i] += cl->cl_solaris_offset;
3697 #endif
3698 	}
3699 	cl->cl_f_geometry_is_valid = B_FALSE;
3700 	mutex_exit(CMLB_MUTEX(cl));
3701 	kmem_free(tmp_geom, sizeof (struct dk_geom));
3702 
3703 	return (rval);
3704 }
3705 
3706 /*
3707  *    Function: cmlb_dkio_get_partition
3708  *
3709  * Description: This routine is the driver entry point for handling user
3710  *		requests to get the partition table (DKIOCGAPART).
3711  *
3712  *   Arguments:
3713  *	arg		pointer to user provided dk_allmap structure specifying
3714  *			the controller's notion of the current partition table.
3715  *
3716  *	flag		this argument is a pass through to ddi_copyxxx()
3717  *			directly from the mode argument of ioctl().
3718  *
3719  *	tg_cookie	cookie from target driver to be passed back to target
3720  *			driver when we call back to it through tg_ops.
3721  *
3722  * Return Code: 0
3723  *		EFAULT
3724  *		ENXIO
3725  *		EIO
3726  */
3727 static int
3728 cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3729     void *tg_cookie)
3730 {
3731 	int		rval = 0;
3732 	int		size;
3733 
3734 	/*
3735 	 * Make sure the geometry is valid before getting the partition
3736 	 * information.
3737 	 */
3738 	mutex_enter(CMLB_MUTEX(cl));
3739 	if ((rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie)) != 0) {
3740 		mutex_exit(CMLB_MUTEX(cl));
3741 		return (rval);
3742 	}
3743 	mutex_exit(CMLB_MUTEX(cl));
3744 
3745 #if defined(__i386) || defined(__amd64)
3746 	if (cl->cl_solaris_size == 0) {
3747 		return (EIO);
3748 	}
3749 #endif
3750 
3751 #ifdef _MULTI_DATAMODEL
3752 	switch (ddi_model_convert_from(flag & FMODELS)) {
3753 	case DDI_MODEL_ILP32: {
3754 		struct dk_map32 dk_map32[NDKMAP];
3755 		int		i;
3756 
3757 		for (i = 0; i < NDKMAP; i++) {
3758 			dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3759 			dk_map32[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
3760 		}
3761 		size = NDKMAP * sizeof (struct dk_map32);
3762 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
3763 		if (rval != 0) {
3764 			rval = EFAULT;
3765 		}
3766 		break;
3767 	}
3768 	case DDI_MODEL_NONE:
3769 		size = NDKMAP * sizeof (struct dk_map);
3770 		rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3771 		if (rval != 0) {
3772 			rval = EFAULT;
3773 		}
3774 		break;
3775 	}
3776 #else /* ! _MULTI_DATAMODEL */
3777 	size = NDKMAP * sizeof (struct dk_map);
3778 	rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3779 	if (rval != 0) {
3780 		rval = EFAULT;
3781 	}
3782 #endif /* _MULTI_DATAMODEL */
3783 	return (rval);
3784 }
3785 
3786 /*
3787  *    Function: cmlb_dkio_set_partition
3788  *
3789  * Description: This routine is the driver entry point for handling user
3790  *		requests to set the partition table (DKIOCSAPART). The actual
3791  *		device partition is not updated.
3792  *
3793  *   Arguments:
3794  *		arg  - pointer to user provided dk_allmap structure used to set
3795  *			the controller's notion of the partition table.
3796  *		flag - this argument is a pass through to ddi_copyxxx()
3797  *		       directly from the mode argument of ioctl().
3798  *
3799  * Return Code: 0
3800  *		EINVAL
3801  *		EFAULT
3802  *		ENXIO
3803  *		EIO
3804  */
3805 static int
3806 cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
3807 {
3808 	struct dk_map	dk_map[NDKMAP];
3809 	struct dk_map	*lp;
3810 	int		rval = 0;
3811 	int		size;
3812 	int		i;
3813 #if defined(_SUNOS_VTOC_16)
3814 	struct dkl_partition	*vp;
3815 #endif
3816 
3817 	/*
3818 	 * Set the map for all logical partitions.  We lock
3819 	 * the priority just to make sure an interrupt doesn't
3820 	 * come in while the map is half updated.
3821 	 */
3822 	_NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3823 	mutex_enter(CMLB_MUTEX(cl));
3824 
3825 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3826 		mutex_exit(CMLB_MUTEX(cl));
3827 		return (ENOTSUP);
3828 	}
3829 	mutex_exit(CMLB_MUTEX(cl));
3830 	if (cl->cl_solaris_size == 0) {
3831 		return (EIO);
3832 	}
3833 
3834 #ifdef _MULTI_DATAMODEL
3835 	switch (ddi_model_convert_from(flag & FMODELS)) {
3836 	case DDI_MODEL_ILP32: {
3837 		struct dk_map32 dk_map32[NDKMAP];
3838 
3839 		size = NDKMAP * sizeof (struct dk_map32);
3840 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
3841 		if (rval != 0) {
3842 			return (EFAULT);
3843 		}
3844 		for (i = 0; i < NDKMAP; i++) {
3845 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
3846 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
3847 		}
3848 		break;
3849 	}
3850 	case DDI_MODEL_NONE:
3851 		size = NDKMAP * sizeof (struct dk_map);
3852 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
3853 		if (rval != 0) {
3854 			return (EFAULT);
3855 		}
3856 		break;
3857 	}
3858 #else /* ! _MULTI_DATAMODEL */
3859 	size = NDKMAP * sizeof (struct dk_map);
3860 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
3861 	if (rval != 0) {
3862 		return (EFAULT);
3863 	}
3864 #endif /* _MULTI_DATAMODEL */
3865 
3866 	mutex_enter(CMLB_MUTEX(cl));
3867 	/* Note: The size used in this bcopy is set based upon the data model */
3868 	bcopy(dk_map, cl->cl_map, size);
3869 #if defined(_SUNOS_VTOC_16)
3870 	vp = (struct dkl_partition *)&(cl->cl_vtoc);
3871 #endif	/* defined(_SUNOS_VTOC_16) */
3872 	for (i = 0; i < NDKMAP; i++) {
3873 		lp  = &cl->cl_map[i];
3874 		cl->cl_offset[i] =
3875 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3876 #if defined(_SUNOS_VTOC_16)
3877 		vp->p_start = cl->cl_offset[i];
3878 		vp->p_size = lp->dkl_nblk;
3879 		vp++;
3880 #endif	/* defined(_SUNOS_VTOC_16) */
3881 #if defined(__i386) || defined(__amd64)
3882 		cl->cl_offset[i] += cl->cl_solaris_offset;
3883 #endif
3884 	}
3885 	mutex_exit(CMLB_MUTEX(cl));
3886 	return (rval);
3887 }
3888 
3889 
3890 /*
3891  *    Function: cmlb_dkio_get_vtoc
3892  *
3893  * Description: This routine is the driver entry point for handling user
3894  *		requests to get the current volume table of contents
3895  *		(DKIOCGVTOC).
3896  *
3897  *   Arguments:
3898  *	arg		pointer to user provided vtoc structure specifying
3899  *			the current vtoc.
3900  *
3901  *	flag		this argument is a pass through to ddi_copyxxx()
3902  *			directly from the mode argument of ioctl().
3903  *
3904  *	tg_cookie	cookie from target driver to be passed back to target
3905  *			driver when we call back to it through tg_ops.
3906  *
3907  * Return Code: 0
3908  *		EFAULT
3909  *		ENXIO
3910  *		EIO
3911  */
3912 static int
3913 cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
3914 {
3915 #if defined(_SUNOS_VTOC_8)
3916 	struct vtoc	user_vtoc;
3917 #endif	/* defined(_SUNOS_VTOC_8) */
3918 	int		rval = 0;
3919 
3920 	mutex_enter(CMLB_MUTEX(cl));
3921 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3922 		mutex_exit(CMLB_MUTEX(cl));
3923 		return (EOVERFLOW);
3924 	}
3925 
3926 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3927 
3928 #if defined(_SUNOS_VTOC_8)
3929 	if (rval == EINVAL &&
3930 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
3931 		/*
3932 		 * This is to return a default label even when we do not
3933 		 * really assume a default label for the device.
3934 		 * dad driver utilizes this.
3935 		 */
3936 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3937 			cmlb_setup_default_geometry(cl, tg_cookie);
3938 			rval = 0;
3939 		}
3940 	}
3941 #endif
3942 	if (rval) {
3943 		mutex_exit(CMLB_MUTEX(cl));
3944 		return (rval);
3945 	}
3946 
3947 #if defined(_SUNOS_VTOC_8)
3948 	cmlb_build_user_vtoc(cl, &user_vtoc);
3949 	mutex_exit(CMLB_MUTEX(cl));
3950 
3951 #ifdef _MULTI_DATAMODEL
3952 	switch (ddi_model_convert_from(flag & FMODELS)) {
3953 	case DDI_MODEL_ILP32: {
3954 		struct vtoc32 user_vtoc32;
3955 
3956 		vtoctovtoc32(user_vtoc, user_vtoc32);
3957 		if (ddi_copyout(&user_vtoc32, (void *)arg,
3958 		    sizeof (struct vtoc32), flag)) {
3959 			return (EFAULT);
3960 		}
3961 		break;
3962 	}
3963 
3964 	case DDI_MODEL_NONE:
3965 		if (ddi_copyout(&user_vtoc, (void *)arg,
3966 		    sizeof (struct vtoc), flag)) {
3967 			return (EFAULT);
3968 		}
3969 		break;
3970 	}
3971 #else /* ! _MULTI_DATAMODEL */
3972 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
3973 		return (EFAULT);
3974 	}
3975 #endif /* _MULTI_DATAMODEL */
3976 
3977 #elif defined(_SUNOS_VTOC_16)
3978 	mutex_exit(CMLB_MUTEX(cl));
3979 
3980 #ifdef _MULTI_DATAMODEL
3981 	/*
3982 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
3983 	 * 32-bit to maintain compatibility with existing on-disk
3984 	 * structures.  Thus, we need to convert the structure when copying
3985 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
3986 	 * program.  If the target is a 32-bit program, then no conversion
3987 	 * is necessary.
3988 	 */
3989 	/* LINTED: logical expression always true: op "||" */
3990 	ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
3991 	switch (ddi_model_convert_from(flag & FMODELS)) {
3992 	case DDI_MODEL_ILP32:
3993 		if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
3994 		    sizeof (cl->cl_vtoc), flag)) {
3995 			return (EFAULT);
3996 		}
3997 		break;
3998 
3999 	case DDI_MODEL_NONE: {
4000 		struct vtoc user_vtoc;
4001 
4002 		vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
4003 		if (ddi_copyout(&user_vtoc, (void *)arg,
4004 		    sizeof (struct vtoc), flag)) {
4005 			return (EFAULT);
4006 		}
4007 		break;
4008 	}
4009 	}
4010 #else /* ! _MULTI_DATAMODEL */
4011 	if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
4012 	    flag)) {
4013 		return (EFAULT);
4014 	}
4015 #endif /* _MULTI_DATAMODEL */
4016 #else
4017 #error "No VTOC format defined."
4018 #endif
4019 
4020 	return (rval);
4021 }
4022 
4023 
4024 /*
4025  *    Function: cmlb_dkio_get_extvtoc
4026  */
4027 static int
4028 cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
4029     void *tg_cookie)
4030 {
4031 	struct extvtoc	ext_vtoc;
4032 #if defined(_SUNOS_VTOC_8)
4033 	struct vtoc	user_vtoc;
4034 #endif	/* defined(_SUNOS_VTOC_8) */
4035 	int		rval = 0;
4036 
4037 	bzero(&ext_vtoc, sizeof (struct extvtoc));
4038 	mutex_enter(CMLB_MUTEX(cl));
4039 	rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
4040 
4041 #if defined(_SUNOS_VTOC_8)
4042 	if (rval == EINVAL &&
4043 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
4044 		/*
4045 		 * This is to return a default label even when we do not
4046 		 * really assume a default label for the device.
4047 		 * dad driver utilizes this.
4048 		 */
4049 		if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
4050 			cmlb_setup_default_geometry(cl, tg_cookie);
4051 			rval = 0;
4052 		}
4053 	}
4054 #endif
4055 	if (rval) {
4056 		mutex_exit(CMLB_MUTEX(cl));
4057 		return (rval);
4058 	}
4059 
4060 #if defined(_SUNOS_VTOC_8)
4061 	cmlb_build_user_vtoc(cl, &user_vtoc);
4062 	mutex_exit(CMLB_MUTEX(cl));
4063 
4064 	/*
4065 	 * Checking callers data model does not make much sense here
4066 	 * since extvtoc will always be equivalent to 64bit vtoc.
4067 	 * What is important is whether the kernel is in 32 or 64 bit
4068 	 */
4069 
4070 #ifdef _LP64
4071 		if (ddi_copyout(&user_vtoc, (void *)arg,
4072 		    sizeof (struct extvtoc), flag)) {
4073 			return (EFAULT);
4074 		}
4075 #else
4076 		vtoc32tovtoc(user_vtoc, ext_vtoc);
4077 		if (ddi_copyout(&ext_vtoc, (void *)arg,
4078 		    sizeof (struct extvtoc), flag)) {
4079 			return (EFAULT);
4080 		}
4081 #endif
4082 
4083 #elif defined(_SUNOS_VTOC_16)
4084 	/*
4085 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
4086 	 * 32-bit to maintain compatibility with existing on-disk
4087 	 * structures.  Thus, we need to convert the structure when copying
4088 	 * it out to extvtoc
4089 	 */
4090 	vtoc32tovtoc(cl->cl_vtoc, ext_vtoc);
4091 	mutex_exit(CMLB_MUTEX(cl));
4092 
4093 	if (ddi_copyout(&ext_vtoc, (void *)arg, sizeof (struct extvtoc), flag))
4094 		return (EFAULT);
4095 #else
4096 #error "No VTOC format defined."
4097 #endif
4098 
4099 	return (rval);
4100 }
4101 
4102 /*
4103  * This routine implements the DKIOCGETEFI ioctl. This ioctl is currently
4104  * used to read the GPT Partition Table Header (primary/backup), the GUID
4105  * partition Entry Array (primary/backup), and the MBR.
4106  */
4107 static int
4108 cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
4109 {
4110 	dk_efi_t	user_efi;
4111 	int		rval = 0;
4112 	void		*buffer;
4113 	diskaddr_t	tgt_lba;
4114 
4115 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4116 		return (EFAULT);
4117 
4118 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4119 
4120 	if (user_efi.dki_length > cmlb_tg_max_efi_xfer)
4121 		return (EINVAL);
4122 
4123 	tgt_lba = user_efi.dki_lba;
4124 
4125 	mutex_enter(CMLB_MUTEX(cl));
4126 	if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4127 	    (cl->cl_tgt_blocksize == 0)) {
4128 		mutex_exit(CMLB_MUTEX(cl));
4129 		return (EINVAL);
4130 	}
4131 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4132 		tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
4133 		    cl->cl_sys_blocksize;
4134 	mutex_exit(CMLB_MUTEX(cl));
4135 
4136 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4137 	rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
4138 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
4139 	    user_efi.dki_length, flag) != 0)
4140 		rval = EFAULT;
4141 
4142 	kmem_free(buffer, user_efi.dki_length);
4143 	return (rval);
4144 }
4145 
4146 #if defined(_SUNOS_VTOC_8)
4147 /*
4148  *    Function: cmlb_build_user_vtoc
4149  *
4150  * Description: This routine populates a pass by reference variable with the
4151  *		current volume table of contents.
4152  *
4153  *   Arguments: cl - driver soft state (unit) structure
4154  *		user_vtoc - pointer to vtoc structure to be populated
4155  */
4156 static void
4157 cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4158 {
4159 	struct dk_map2		*lpart;
4160 	struct dk_map		*lmap;
4161 	struct partition	*vpart;
4162 	uint32_t		nblks;
4163 	int			i;
4164 
4165 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4166 
4167 	/*
4168 	 * Return vtoc structure fields in the provided VTOC area, addressed
4169 	 * by *vtoc.
4170 	 */
4171 	bzero(user_vtoc, sizeof (struct vtoc));
4172 	user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
4173 	user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
4174 	user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
4175 	user_vtoc->v_sanity	= VTOC_SANE;
4176 	user_vtoc->v_version	= cl->cl_vtoc.v_version;
4177 	bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
4178 	user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
4179 	user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
4180 
4181 	for (i = 0; i < 10; i++)
4182 		user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
4183 
4184 	/*
4185 	 * Convert partitioning information.
4186 	 *
4187 	 * Note the conversion from starting cylinder number
4188 	 * to starting sector number.
4189 	 */
4190 	lmap = cl->cl_map;
4191 	lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
4192 	vpart = user_vtoc->v_part;
4193 
4194 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4195 
4196 	for (i = 0; i < V_NUMPAR; i++) {
4197 		vpart->p_tag	= lpart->p_tag;
4198 		vpart->p_flag	= lpart->p_flag;
4199 		vpart->p_start	= lmap->dkl_cylno * nblks;
4200 		vpart->p_size	= lmap->dkl_nblk;
4201 		lmap++;
4202 		lpart++;
4203 		vpart++;
4204 
4205 		/* (4364927) */
4206 		user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
4207 	}
4208 
4209 	bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
4210 }
4211 #endif
4212 
4213 static int
4214 cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
4215     void *tg_cookie)
4216 {
4217 	struct partition64	p64;
4218 	int			rval = 0;
4219 	uint_t			nparts;
4220 	efi_gpe_t		*partitions;
4221 	efi_gpt_t		*buffer;
4222 	diskaddr_t		gpe_lba;
4223 	int			n_gpe_per_blk = 0;
4224 
4225 	if (ddi_copyin((const void *)arg, &p64,
4226 	    sizeof (struct partition64), flag)) {
4227 		return (EFAULT);
4228 	}
4229 
4230 	buffer = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4231 	rval = DK_TG_READ(cl, buffer, 1, cl->cl_sys_blocksize, tg_cookie);
4232 	if (rval != 0)
4233 		goto done_error;
4234 
4235 	cmlb_swap_efi_gpt(buffer);
4236 
4237 	if ((rval = cmlb_validate_efi(buffer)) != 0)
4238 		goto done_error;
4239 
4240 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
4241 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
4242 	if (p64.p_partno >= nparts) {
4243 		/* couldn't find it */
4244 		rval = ESRCH;
4245 		goto done_error;
4246 	}
4247 	/*
4248 	 * Read the block that contains the requested GPE.
4249 	 */
4250 	n_gpe_per_blk = cl->cl_sys_blocksize / sizeof (efi_gpe_t);
4251 	gpe_lba += p64.p_partno / n_gpe_per_blk;
4252 	rval = DK_TG_READ(cl, buffer, gpe_lba, cl->cl_sys_blocksize, tg_cookie);
4253 
4254 	if (rval) {
4255 		goto done_error;
4256 	}
4257 	partitions = (efi_gpe_t *)buffer;
4258 	partitions += p64.p_partno % n_gpe_per_blk;
4259 
4260 	/* Byte swap only the requested GPE */
4261 	cmlb_swap_efi_gpe(1, partitions);
4262 
4263 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
4264 	    sizeof (struct uuid));
4265 	p64.p_start = partitions->efi_gpe_StartingLBA;
4266 	p64.p_size = partitions->efi_gpe_EndingLBA -
4267 	    p64.p_start + 1;
4268 
4269 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
4270 		rval = EFAULT;
4271 
4272 done_error:
4273 	kmem_free(buffer, cl->cl_sys_blocksize);
4274 	return (rval);
4275 }
4276 
4277 
4278 /*
4279  *    Function: cmlb_dkio_set_vtoc
4280  *
4281  * Description: This routine is the driver entry point for handling user
4282  *		requests to set the current volume table of contents
4283  *		(DKIOCSVTOC).
4284  *
4285  *   Arguments:
4286  *	dev		the device number
4287  *	arg		pointer to user provided vtoc structure used to set the
4288  *			current vtoc.
4289  *
4290  *	flag		this argument is a pass through to ddi_copyxxx()
4291  *			directly from the mode argument of ioctl().
4292  *
4293  *	tg_cookie	cookie from target driver to be passed back to target
4294  *			driver when we call back to it through tg_ops.
4295  *
4296  * Return Code: 0
4297  *		EFAULT
4298  *		ENXIO
4299  *		EINVAL
4300  *		ENOTSUP
4301  */
4302 static int
4303 cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4304     void *tg_cookie)
4305 {
4306 	struct vtoc	user_vtoc;
4307 	int		rval = 0;
4308 	boolean_t	internal;
4309 
4310 	internal = VOID2BOOLEAN(
4311 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4312 
4313 #ifdef _MULTI_DATAMODEL
4314 	switch (ddi_model_convert_from(flag & FMODELS)) {
4315 	case DDI_MODEL_ILP32: {
4316 		struct vtoc32 user_vtoc32;
4317 
4318 		if (ddi_copyin((const void *)arg, &user_vtoc32,
4319 		    sizeof (struct vtoc32), flag)) {
4320 			return (EFAULT);
4321 		}
4322 		vtoc32tovtoc(user_vtoc32, user_vtoc);
4323 		break;
4324 	}
4325 
4326 	case DDI_MODEL_NONE:
4327 		if (ddi_copyin((const void *)arg, &user_vtoc,
4328 		    sizeof (struct vtoc), flag)) {
4329 			return (EFAULT);
4330 		}
4331 		break;
4332 	}
4333 #else /* ! _MULTI_DATAMODEL */
4334 	if (ddi_copyin((const void *)arg, &user_vtoc,
4335 	    sizeof (struct vtoc), flag)) {
4336 		return (EFAULT);
4337 	}
4338 #endif /* _MULTI_DATAMODEL */
4339 
4340 	mutex_enter(CMLB_MUTEX(cl));
4341 
4342 	if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
4343 		mutex_exit(CMLB_MUTEX(cl));
4344 		return (EOVERFLOW);
4345 	}
4346 
4347 #if defined(__i386) || defined(__amd64)
4348 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4349 		mutex_exit(CMLB_MUTEX(cl));
4350 		return (EINVAL);
4351 	}
4352 #endif
4353 
4354 	if (cl->cl_g.dkg_ncyl == 0) {
4355 		mutex_exit(CMLB_MUTEX(cl));
4356 		return (EINVAL);
4357 	}
4358 
4359 	mutex_exit(CMLB_MUTEX(cl));
4360 	cmlb_clear_efi(cl, tg_cookie);
4361 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4362 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4363 
4364 	/*
4365 	 * cmlb_dkio_set_vtoc creates duplicate minor nodes when
4366 	 * relabeling an SMI disk. To avoid that we remove them
4367 	 * before creating.
4368 	 * It should be OK to remove a non-existed minor node.
4369 	 */
4370 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4371 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4372 
4373 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4374 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4375 	    cl->cl_node_type, NULL, internal);
4376 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4377 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4378 	    cl->cl_node_type, NULL, internal);
4379 	mutex_enter(CMLB_MUTEX(cl));
4380 
4381 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4382 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4383 			if (cmlb_validate_geometry(cl,
4384 			    B_TRUE, 0, tg_cookie) != 0) {
4385 				cmlb_dbg(CMLB_ERROR, cl,
4386 				    "cmlb_dkio_set_vtoc: "
4387 				    "Failed validate geometry\n");
4388 			}
4389 			cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
4390 		}
4391 	}
4392 	mutex_exit(CMLB_MUTEX(cl));
4393 	return (rval);
4394 }
4395 
4396 /*
4397  *    Function: cmlb_dkio_set_extvtoc
4398  */
4399 static int
4400 cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4401     void *tg_cookie)
4402 {
4403 	int		rval = 0;
4404 	struct vtoc	user_vtoc;
4405 	boolean_t	internal;
4406 
4407 
4408 	/*
4409 	 * Checking callers data model does not make much sense here
4410 	 * since extvtoc will always be equivalent to 64bit vtoc.
4411 	 * What is important is whether the kernel is in 32 or 64 bit
4412 	 */
4413 
4414 #ifdef _LP64
4415 	if (ddi_copyin((const void *)arg, &user_vtoc,
4416 		    sizeof (struct extvtoc), flag)) {
4417 			return (EFAULT);
4418 	}
4419 #else
4420 	struct	extvtoc	user_extvtoc;
4421 	if (ddi_copyin((const void *)arg, &user_extvtoc,
4422 		    sizeof (struct extvtoc), flag)) {
4423 			return (EFAULT);
4424 	}
4425 
4426 	vtoctovtoc32(user_extvtoc, user_vtoc);
4427 #endif
4428 
4429 	internal = VOID2BOOLEAN(
4430 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4431 	mutex_enter(CMLB_MUTEX(cl));
4432 #if defined(__i386) || defined(__amd64)
4433 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4434 		mutex_exit(CMLB_MUTEX(cl));
4435 		return (EINVAL);
4436 	}
4437 #endif
4438 
4439 	if (cl->cl_g.dkg_ncyl == 0) {
4440 		mutex_exit(CMLB_MUTEX(cl));
4441 		return (EINVAL);
4442 	}
4443 
4444 	mutex_exit(CMLB_MUTEX(cl));
4445 	cmlb_clear_efi(cl, tg_cookie);
4446 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4447 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4448 	/*
4449 	 * cmlb_dkio_set_extvtoc creates duplicate minor nodes when
4450 	 * relabeling an SMI disk. To avoid that we remove them
4451 	 * before creating.
4452 	 * It should be OK to remove a non-existed minor node.
4453 	 */
4454 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4455 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4456 
4457 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4458 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4459 	    cl->cl_node_type, NULL, internal);
4460 	(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4461 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4462 	    cl->cl_node_type, NULL, internal);
4463 
4464 	mutex_enter(CMLB_MUTEX(cl));
4465 
4466 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4467 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4468 			if (cmlb_validate_geometry(cl,
4469 			    B_TRUE, 0, tg_cookie) != 0) {
4470 				cmlb_dbg(CMLB_ERROR, cl,
4471 				    "cmlb_dkio_set_vtoc: "
4472 				    "Failed validate geometry\n");
4473 			}
4474 		}
4475 	}
4476 	mutex_exit(CMLB_MUTEX(cl));
4477 	return (rval);
4478 }
4479 
4480 /*
4481  *    Function: cmlb_build_label_vtoc
4482  *
4483  * Description: This routine updates the driver soft state current volume table
4484  *		of contents based on a user specified vtoc.
4485  *
4486  *   Arguments: cl - driver soft state (unit) structure
4487  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
4488  *			    to update the driver soft state.
4489  *
4490  * Return Code: 0
4491  *		EINVAL
4492  */
4493 static int
4494 cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4495 {
4496 	struct dk_map		*lmap;
4497 	struct partition	*vpart;
4498 	uint_t			nblks;
4499 #if defined(_SUNOS_VTOC_8)
4500 	int			ncyl;
4501 	struct dk_map2		*lpart;
4502 #endif	/* defined(_SUNOS_VTOC_8) */
4503 	int			i;
4504 
4505 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4506 
4507 	/* Sanity-check the vtoc */
4508 	if (user_vtoc->v_sanity != VTOC_SANE ||
4509 	    user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
4510 	    user_vtoc->v_nparts != V_NUMPAR) {
4511 		cmlb_dbg(CMLB_INFO,  cl,
4512 		    "cmlb_build_label_vtoc: vtoc not valid\n");
4513 		return (EINVAL);
4514 	}
4515 
4516 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4517 	if (nblks == 0) {
4518 		cmlb_dbg(CMLB_INFO,  cl,
4519 		    "cmlb_build_label_vtoc: geom nblks is 0\n");
4520 		return (EINVAL);
4521 	}
4522 
4523 #if defined(_SUNOS_VTOC_8)
4524 	vpart = user_vtoc->v_part;
4525 	for (i = 0; i < V_NUMPAR; i++) {
4526 		if (((unsigned)vpart->p_start % nblks) != 0) {
4527 			cmlb_dbg(CMLB_INFO,  cl,
4528 			    "cmlb_build_label_vtoc: p_start not multiply of"
4529 			    "nblks part %d p_start %d nblks %d\n", i,
4530 			    vpart->p_start, nblks);
4531 			return (EINVAL);
4532 		}
4533 		ncyl = (unsigned)vpart->p_start / nblks;
4534 		ncyl += (unsigned)vpart->p_size / nblks;
4535 		if (((unsigned)vpart->p_size % nblks) != 0) {
4536 			ncyl++;
4537 		}
4538 		if (ncyl > (int)cl->cl_g.dkg_ncyl) {
4539 			cmlb_dbg(CMLB_INFO,  cl,
4540 			    "cmlb_build_label_vtoc: ncyl %d  > dkg_ncyl %d"
4541 			    "p_size %ld p_start %ld nblks %d  part number %d"
4542 			    "tag %d\n",
4543 			    ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
4544 			    vpart->p_start, nblks,
4545 			    i, vpart->p_tag);
4546 
4547 			return (EINVAL);
4548 		}
4549 		vpart++;
4550 	}
4551 #endif	/* defined(_SUNOS_VTOC_8) */
4552 
4553 	/* Put appropriate vtoc structure fields into the disk label */
4554 #if defined(_SUNOS_VTOC_16)
4555 	/*
4556 	 * The vtoc is always a 32bit data structure to maintain the
4557 	 * on-disk format. Convert "in place" instead of doing bcopy.
4558 	 */
4559 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
4560 
4561 	/*
4562 	 * in the 16-slice vtoc, starting sectors are expressed in
4563 	 * numbers *relative* to the start of the Solaris fdisk partition.
4564 	 */
4565 	lmap = cl->cl_map;
4566 	vpart = user_vtoc->v_part;
4567 
4568 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
4569 		lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4570 		lmap->dkl_nblk = (unsigned)vpart->p_size;
4571 	}
4572 
4573 #elif defined(_SUNOS_VTOC_8)
4574 
4575 	cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
4576 	cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
4577 	cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
4578 
4579 	cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
4580 	cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
4581 
4582 	bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
4583 
4584 	cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
4585 
4586 	for (i = 0; i < 10; i++)
4587 		cl->cl_vtoc.v_reserved[i] =  user_vtoc->v_reserved[i];
4588 
4589 	/*
4590 	 * Note the conversion from starting sector number
4591 	 * to starting cylinder number.
4592 	 * Return error if division results in a remainder.
4593 	 */
4594 	lmap = cl->cl_map;
4595 	lpart = cl->cl_vtoc.v_part;
4596 	vpart = user_vtoc->v_part;
4597 
4598 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
4599 		lpart->p_tag  = vpart->p_tag;
4600 		lpart->p_flag = vpart->p_flag;
4601 		lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4602 		lmap->dkl_nblk = (unsigned)vpart->p_size;
4603 
4604 		lmap++;
4605 		lpart++;
4606 		vpart++;
4607 
4608 		/* (4387723) */
4609 #ifdef _LP64
4610 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
4611 			cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
4612 		} else {
4613 			cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4614 		}
4615 #else
4616 		cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4617 #endif
4618 	}
4619 
4620 	bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
4621 #else
4622 #error "No VTOC format defined."
4623 #endif
4624 	return (0);
4625 }
4626 
4627 /*
4628  *    Function: cmlb_clear_efi
4629  *
4630  * Description: This routine clears all EFI labels.
4631  *
4632  *   Arguments:
4633  *	cl		 driver soft state (unit) structure
4634  *
4635  *	tg_cookie	cookie from target driver to be passed back to target
4636  *			driver when we call back to it through tg_ops.
4637  * Return Code: void
4638  */
4639 static void
4640 cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
4641 {
4642 	efi_gpt_t	*gpt;
4643 	diskaddr_t	cap;
4644 	int		rval;
4645 
4646 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4647 
4648 	mutex_enter(CMLB_MUTEX(cl));
4649 	cl->cl_reserved = -1;
4650 	mutex_exit(CMLB_MUTEX(cl));
4651 
4652 	gpt = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4653 
4654 	if (DK_TG_READ(cl, gpt, 1, cl->cl_sys_blocksize, tg_cookie) != 0) {
4655 		goto done;
4656 	}
4657 
4658 	cmlb_swap_efi_gpt(gpt);
4659 	rval = cmlb_validate_efi(gpt);
4660 	if (rval == 0) {
4661 		/* clear primary */
4662 		bzero(gpt, sizeof (efi_gpt_t));
4663 		if (rval = DK_TG_WRITE(cl, gpt, 1, cl->cl_sys_blocksize,
4664 		    tg_cookie)) {
4665 			cmlb_dbg(CMLB_INFO,  cl,
4666 			    "cmlb_clear_efi: clear primary label failed\n");
4667 		}
4668 	}
4669 	/* the backup */
4670 	rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
4671 	if (rval) {
4672 		goto done;
4673 	}
4674 
4675 	if ((rval = DK_TG_READ(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4676 	    tg_cookie)) != 0) {
4677 		goto done;
4678 	}
4679 	cmlb_swap_efi_gpt(gpt);
4680 	rval = cmlb_validate_efi(gpt);
4681 	if (rval == 0) {
4682 		/* clear backup */
4683 		cmlb_dbg(CMLB_TRACE,  cl,
4684 		    "cmlb_clear_efi clear backup@%lu\n", cap - 1);
4685 		bzero(gpt, sizeof (efi_gpt_t));
4686 		if ((rval = DK_TG_WRITE(cl,  gpt, cap - 1, cl->cl_sys_blocksize,
4687 		    tg_cookie))) {
4688 			cmlb_dbg(CMLB_INFO,  cl,
4689 			    "cmlb_clear_efi: clear backup label failed\n");
4690 		}
4691 	} else {
4692 		/*
4693 		 * Refer to comments related to off-by-1 at the
4694 		 * header of this file
4695 		 */
4696 		if ((rval = DK_TG_READ(cl, gpt, cap - 2,
4697 		    cl->cl_sys_blocksize, tg_cookie)) != 0) {
4698 			goto done;
4699 		}
4700 		cmlb_swap_efi_gpt(gpt);
4701 		rval = cmlb_validate_efi(gpt);
4702 		if (rval == 0) {
4703 			/* clear legacy backup EFI label */
4704 			cmlb_dbg(CMLB_TRACE,  cl,
4705 			    "cmlb_clear_efi clear legacy backup@%lu\n",
4706 			    cap - 2);
4707 			bzero(gpt, sizeof (efi_gpt_t));
4708 			if ((rval = DK_TG_WRITE(cl,  gpt, cap - 2,
4709 			    cl->cl_sys_blocksize, tg_cookie))) {
4710 				cmlb_dbg(CMLB_INFO,  cl,
4711 				"cmlb_clear_efi: clear legacy backup label "
4712 				"failed\n");
4713 			}
4714 		}
4715 	}
4716 
4717 done:
4718 	kmem_free(gpt, cl->cl_sys_blocksize);
4719 }
4720 
4721 /*
4722  *    Function: cmlb_set_vtoc
4723  *
4724  * Description: This routine writes data to the appropriate positions
4725  *
4726  *   Arguments:
4727  *	cl		driver soft state (unit) structure
4728  *
4729  *	dkl		the data to be written
4730  *
4731  *	tg_cookie	cookie from target driver to be passed back to target
4732  *			driver when we call back to it through tg_ops.
4733  *
4734  * Return: void
4735  */
4736 static int
4737 cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
4738 {
4739 	uint_t	label_addr;
4740 	int	sec;
4741 	diskaddr_t	blk;
4742 	int	head;
4743 	int	cyl;
4744 	int	rval;
4745 
4746 #if defined(__i386) || defined(__amd64)
4747 	label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
4748 #else
4749 	/* Write the primary label at block 0 of the solaris partition. */
4750 	label_addr = 0;
4751 #endif
4752 
4753 	rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
4754 	    tg_cookie);
4755 
4756 	if (rval != 0) {
4757 		return (rval);
4758 	}
4759 
4760 	/*
4761 	 * Calculate where the backup labels go.  They are always on
4762 	 * the last alternate cylinder, but some older drives put them
4763 	 * on head 2 instead of the last head.	They are always on the
4764 	 * first 5 odd sectors of the appropriate track.
4765 	 *
4766 	 * We have no choice at this point, but to believe that the
4767 	 * disk label is valid.	 Use the geometry of the disk
4768 	 * as described in the label.
4769 	 */
4770 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
4771 	head = dkl->dkl_nhead - 1;
4772 
4773 	/*
4774 	 * Write and verify the backup labels. Make sure we don't try to
4775 	 * write past the last cylinder.
4776 	 */
4777 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
4778 		blk = (diskaddr_t)(
4779 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
4780 		    (head * dkl->dkl_nsect) + sec);
4781 #if defined(__i386) || defined(__amd64)
4782 		blk += cl->cl_solaris_offset;
4783 #endif
4784 		rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
4785 		    tg_cookie);
4786 		cmlb_dbg(CMLB_INFO,  cl,
4787 		"cmlb_set_vtoc: wrote backup label %llx\n", blk);
4788 		if (rval != 0) {
4789 			goto exit;
4790 		}
4791 	}
4792 exit:
4793 	return (rval);
4794 }
4795 
4796 /*
4797  *    Function: cmlb_clear_vtoc
4798  *
4799  * Description: This routine clears out the VTOC labels.
4800  *
4801  *   Arguments:
4802  *	cl		driver soft state (unit) structure
4803  *
4804  *	tg_cookie	cookie from target driver to be passed back to target
4805  *			driver when we call back to it through tg_ops.
4806  *
4807  * Return: void
4808  */
4809 static void
4810 cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
4811 {
4812 	struct dk_label		*dkl;
4813 
4814 	mutex_exit(CMLB_MUTEX(cl));
4815 	dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4816 	mutex_enter(CMLB_MUTEX(cl));
4817 	/*
4818 	 * cmlb_set_vtoc uses these fields in order to figure out
4819 	 * where to overwrite the backup labels
4820 	 */
4821 	dkl->dkl_apc    = cl->cl_g.dkg_apc;
4822 	dkl->dkl_ncyl   = cl->cl_g.dkg_ncyl;
4823 	dkl->dkl_acyl   = cl->cl_g.dkg_acyl;
4824 	dkl->dkl_nhead  = cl->cl_g.dkg_nhead;
4825 	dkl->dkl_nsect  = cl->cl_g.dkg_nsect;
4826 	mutex_exit(CMLB_MUTEX(cl));
4827 	(void) cmlb_set_vtoc(cl, dkl, tg_cookie);
4828 	kmem_free(dkl, cl->cl_sys_blocksize);
4829 
4830 	mutex_enter(CMLB_MUTEX(cl));
4831 }
4832 
4833 /*
4834  *    Function: cmlb_write_label
4835  *
4836  * Description: This routine will validate and write the driver soft state vtoc
4837  *		contents to the device.
4838  *
4839  *   Arguments:
4840  *	cl		cmlb handle
4841  *
4842  *	tg_cookie	cookie from target driver to be passed back to target
4843  *			driver when we call back to it through tg_ops.
4844  *
4845  *
4846  * Return Code: the code returned by cmlb_send_scsi_cmd()
4847  *		0
4848  *		EINVAL
4849  *		ENXIO
4850  *		ENOMEM
4851  */
4852 static int
4853 cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
4854 {
4855 	struct dk_label	*dkl;
4856 	short		sum;
4857 	short		*sp;
4858 	int		i;
4859 	int		rval;
4860 
4861 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4862 	mutex_exit(CMLB_MUTEX(cl));
4863 	dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4864 	mutex_enter(CMLB_MUTEX(cl));
4865 
4866 	bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
4867 	dkl->dkl_rpm	= cl->cl_g.dkg_rpm;
4868 	dkl->dkl_pcyl	= cl->cl_g.dkg_pcyl;
4869 	dkl->dkl_apc	= cl->cl_g.dkg_apc;
4870 	dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
4871 	dkl->dkl_ncyl	= cl->cl_g.dkg_ncyl;
4872 	dkl->dkl_acyl	= cl->cl_g.dkg_acyl;
4873 	dkl->dkl_nhead	= cl->cl_g.dkg_nhead;
4874 	dkl->dkl_nsect	= cl->cl_g.dkg_nsect;
4875 
4876 #if defined(_SUNOS_VTOC_8)
4877 	dkl->dkl_obs1	= cl->cl_g.dkg_obs1;
4878 	dkl->dkl_obs2	= cl->cl_g.dkg_obs2;
4879 	dkl->dkl_obs3	= cl->cl_g.dkg_obs3;
4880 	for (i = 0; i < NDKMAP; i++) {
4881 		dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
4882 		dkl->dkl_map[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
4883 	}
4884 	bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
4885 #elif defined(_SUNOS_VTOC_16)
4886 	dkl->dkl_skew	= cl->cl_dkg_skew;
4887 #else
4888 #error "No VTOC format defined."
4889 #endif
4890 
4891 	dkl->dkl_magic			= DKL_MAGIC;
4892 	dkl->dkl_write_reinstruct	= cl->cl_g.dkg_write_reinstruct;
4893 	dkl->dkl_read_reinstruct	= cl->cl_g.dkg_read_reinstruct;
4894 
4895 	/* Construct checksum for the new disk label */
4896 	sum = 0;
4897 	sp = (short *)dkl;
4898 	i = sizeof (struct dk_label) / sizeof (short);
4899 	while (i--) {
4900 		sum ^= *sp++;
4901 	}
4902 	dkl->dkl_cksum = sum;
4903 
4904 	mutex_exit(CMLB_MUTEX(cl));
4905 
4906 	rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
4907 exit:
4908 	kmem_free(dkl, cl->cl_sys_blocksize);
4909 	mutex_enter(CMLB_MUTEX(cl));
4910 	return (rval);
4911 }
4912 
4913 /*
4914  * This routine implements the DKIOCSETEFI ioctl. This ioctl is currently
4915  * used to write (or clear) the GPT Partition Table header (primary/backup)
4916  * and GUID partition Entry Array (primary/backup). It is also used to write
4917  * the Protective MBR.
4918  */
4919 static int
4920 cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4921     void *tg_cookie)
4922 {
4923 	dk_efi_t	user_efi;
4924 	int		rval = 0;
4925 	void		*buffer;
4926 	diskaddr_t	tgt_lba;
4927 	boolean_t	internal;
4928 
4929 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4930 		return (EFAULT);
4931 
4932 	internal = VOID2BOOLEAN(
4933 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4934 
4935 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4936 
4937 	if (user_efi.dki_length > cmlb_tg_max_efi_xfer)
4938 		return (EINVAL);
4939 
4940 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4941 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
4942 		rval = EFAULT;
4943 	} else {
4944 		/*
4945 		 * let's clear the vtoc labels and clear the softstate
4946 		 * vtoc.
4947 		 */
4948 		mutex_enter(CMLB_MUTEX(cl));
4949 		if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
4950 			cmlb_dbg(CMLB_TRACE,  cl,
4951 			    "cmlb_dkio_set_efi: CLEAR VTOC\n");
4952 			if (cl->cl_label_from_media == CMLB_LABEL_VTOC)
4953 				cmlb_clear_vtoc(cl, tg_cookie);
4954 			bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4955 			mutex_exit(CMLB_MUTEX(cl));
4956 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4957 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4958 			(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
4959 			    S_IFBLK,
4960 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4961 			    cl->cl_node_type, NULL, internal);
4962 			(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
4963 			    S_IFCHR,
4964 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
4965 			    cl->cl_node_type, NULL, internal);
4966 		} else
4967 			mutex_exit(CMLB_MUTEX(cl));
4968 
4969 		tgt_lba = user_efi.dki_lba;
4970 
4971 		mutex_enter(CMLB_MUTEX(cl));
4972 		if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4973 		    (cl->cl_tgt_blocksize == 0)) {
4974 			kmem_free(buffer, user_efi.dki_length);
4975 			mutex_exit(CMLB_MUTEX(cl));
4976 			return (EINVAL);
4977 		}
4978 		if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4979 			tgt_lba = tgt_lba *
4980 			    cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
4981 
4982 		mutex_exit(CMLB_MUTEX(cl));
4983 		rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
4984 		    tg_cookie);
4985 
4986 		if (rval == 0) {
4987 			mutex_enter(CMLB_MUTEX(cl));
4988 			cl->cl_f_geometry_is_valid = B_FALSE;
4989 			mutex_exit(CMLB_MUTEX(cl));
4990 		}
4991 	}
4992 	kmem_free(buffer, user_efi.dki_length);
4993 	return (rval);
4994 }
4995 
4996 /*
4997  *    Function: cmlb_dkio_get_mboot
4998  *
4999  * Description: This routine is the driver entry point for handling user
5000  *		requests to get the current device mboot (DKIOCGMBOOT)
5001  *
5002  *   Arguments:
5003  *	arg		pointer to user provided mboot structure specifying
5004  *			the current mboot.
5005  *
5006  *	flag		this argument is a pass through to ddi_copyxxx()
5007  *			directly from the mode argument of ioctl().
5008  *
5009  *	tg_cookie	cookie from target driver to be passed back to target
5010  *			driver when we call back to it through tg_ops.
5011  *
5012  * Return Code: 0
5013  *		EINVAL
5014  *		EFAULT
5015  *		ENXIO
5016  */
5017 static int
5018 cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5019 {
5020 	struct mboot	*mboot;
5021 	int		rval;
5022 	size_t		buffer_size;
5023 
5024 
5025 #if defined(_SUNOS_VTOC_8)
5026 	if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
5027 #elif defined(_SUNOS_VTOC_16)
5028 	if (arg == NULL) {
5029 #endif
5030 		return (EINVAL);
5031 	}
5032 
5033 	/*
5034 	 * Read the mboot block, located at absolute block 0 on the target.
5035 	 */
5036 	buffer_size = cl->cl_sys_blocksize;
5037 
5038 	cmlb_dbg(CMLB_TRACE,  cl,
5039 	    "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
5040 
5041 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
5042 	if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
5043 		if (ddi_copyout(mboot, (void *)arg,
5044 		    sizeof (struct mboot), flag) != 0) {
5045 			rval = EFAULT;
5046 		}
5047 	}
5048 	kmem_free(mboot, buffer_size);
5049 	return (rval);
5050 }
5051 
5052 
5053 /*
5054  *    Function: cmlb_dkio_set_mboot
5055  *
5056  * Description: This routine is the driver entry point for handling user
5057  *		requests to validate and set the device master boot
5058  *		(DKIOCSMBOOT).
5059  *
5060  *   Arguments:
5061  *	arg		pointer to user provided mboot structure used to set the
5062  *			master boot.
5063  *
5064  *	flag		this argument is a pass through to ddi_copyxxx()
5065  *			directly from the mode argument of ioctl().
5066  *
5067  *	tg_cookie	cookie from target driver to be passed back to target
5068  *			driver when we call back to it through tg_ops.
5069  *
5070  * Return Code: 0
5071  *		EINVAL
5072  *		EFAULT
5073  *		ENXIO
5074  */
5075 static int
5076 cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5077 {
5078 	struct mboot	*mboot = NULL;
5079 	int		rval;
5080 	ushort_t	magic;
5081 
5082 
5083 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5084 
5085 #if defined(_SUNOS_VTOC_8)
5086 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
5087 		return (EINVAL);
5088 	}
5089 #endif
5090 
5091 	if (arg == NULL) {
5092 		return (EINVAL);
5093 	}
5094 
5095 	mboot = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
5096 
5097 	if (ddi_copyin((const void *)arg, mboot,
5098 	    cl->cl_sys_blocksize, flag) != 0) {
5099 		kmem_free(mboot, cl->cl_sys_blocksize);
5100 		return (EFAULT);
5101 	}
5102 
5103 	/* Is this really a master boot record? */
5104 	magic = LE_16(mboot->signature);
5105 	if (magic != MBB_MAGIC) {
5106 		kmem_free(mboot, cl->cl_sys_blocksize);
5107 		return (EINVAL);
5108 	}
5109 
5110 	rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
5111 
5112 	mutex_enter(CMLB_MUTEX(cl));
5113 #if defined(__i386) || defined(__amd64)
5114 	if (rval == 0) {
5115 		/*
5116 		 * mboot has been written successfully.
5117 		 * update the fdisk and vtoc tables in memory
5118 		 */
5119 		rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
5120 		if ((!cl->cl_f_geometry_is_valid) || (rval != 0)) {
5121 			mutex_exit(CMLB_MUTEX(cl));
5122 			kmem_free(mboot, cl->cl_sys_blocksize);
5123 			return (rval);
5124 		}
5125 	}
5126 
5127 #ifdef __lock_lint
5128 	cmlb_setup_default_geometry(cl, tg_cookie);
5129 #endif
5130 
5131 #else
5132 	if (rval == 0) {
5133 		/*
5134 		 * mboot has been written successfully.
5135 		 * set up the default geometry and VTOC
5136 		 */
5137 		if (cl->cl_blockcount <= CMLB_EXTVTOC_LIMIT)
5138 			cmlb_setup_default_geometry(cl, tg_cookie);
5139 	}
5140 #endif
5141 	cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
5142 	mutex_exit(CMLB_MUTEX(cl));
5143 	kmem_free(mboot, cl->cl_sys_blocksize);
5144 	return (rval);
5145 }
5146 
5147 
5148 #if defined(__i386) || defined(__amd64)
5149 /*ARGSUSED*/
5150 static int
5151 cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
5152     void *tg_cookie)
5153 {
5154 	int fdisk_rval;
5155 	diskaddr_t capacity;
5156 
5157 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5158 
5159 	mutex_enter(CMLB_MUTEX(cl));
5160 	capacity = cl->cl_blockcount;
5161 	fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5162 	if (fdisk_rval != 0) {
5163 		mutex_exit(CMLB_MUTEX(cl));
5164 		return (fdisk_rval);
5165 	}
5166 
5167 	mutex_exit(CMLB_MUTEX(cl));
5168 	return (fdisk_rval);
5169 }
5170 #endif
5171 
5172 /*
5173  *    Function: cmlb_setup_default_geometry
5174  *
5175  * Description: This local utility routine sets the default geometry as part of
5176  *		setting the device mboot.
5177  *
5178  *   Arguments:
5179  *	cl		driver soft state (unit) structure
5180  *
5181  *	tg_cookie	cookie from target driver to be passed back to target
5182  *			driver when we call back to it through tg_ops.
5183  *
5184  *
5185  * Note: This may be redundant with cmlb_build_default_label.
5186  */
5187 static void
5188 cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
5189 {
5190 	struct cmlb_geom	pgeom;
5191 	struct cmlb_geom	*pgeomp = &pgeom;
5192 	int			ret;
5193 	int			geom_base_cap = 1;
5194 
5195 
5196 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5197 
5198 	/* zero out the soft state geometry and partition table. */
5199 	bzero(&cl->cl_g, sizeof (struct dk_geom));
5200 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5201 	bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
5202 
5203 	/*
5204 	 * For the rpm, we use the minimum for the disk.
5205 	 * For the head, cyl and number of sector per track,
5206 	 * if the capacity <= 1GB, head = 64, sect = 32.
5207 	 * else head = 255, sect 63
5208 	 * Note: the capacity should be equal to C*H*S values.
5209 	 * This will cause some truncation of size due to
5210 	 * round off errors. For CD-ROMs, this truncation can
5211 	 * have adverse side effects, so returning ncyl and
5212 	 * nhead as 1. The nsect will overflow for most of
5213 	 * CD-ROMs as nsect is of type ushort.
5214 	 */
5215 	if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
5216 		/*
5217 		 * newfs currently can not handle 255 ntracks for SPARC
5218 		 * so get the geometry from target driver instead of coming up
5219 		 * with one based on capacity.
5220 		 */
5221 		mutex_exit(CMLB_MUTEX(cl));
5222 		ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
5223 		mutex_enter(CMLB_MUTEX(cl));
5224 
5225 		if (ret == 0) {
5226 			geom_base_cap = 0;
5227 		} else {
5228 			cmlb_dbg(CMLB_ERROR,  cl,
5229 			    "cmlb_setup_default_geometry: "
5230 			    "tg_getphygeom failed %d\n", ret);
5231 
5232 			/* do default setting, geometry based on capacity */
5233 		}
5234 	}
5235 
5236 	if (geom_base_cap) {
5237 		if (ISCD(cl)) {
5238 			cl->cl_g.dkg_ncyl = 1;
5239 			cl->cl_g.dkg_nhead = 1;
5240 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
5241 		} else if (cl->cl_blockcount < 160) {
5242 			/* Less than 80K */
5243 			cl->cl_g.dkg_nhead = 1;
5244 			cl->cl_g.dkg_ncyl = cl->cl_blockcount;
5245 			cl->cl_g.dkg_nsect = 1;
5246 		} else if (cl->cl_blockcount <= 0x1000) {
5247 			/* Needed for unlabeled SCSI floppies. */
5248 			cl->cl_g.dkg_nhead = 2;
5249 			cl->cl_g.dkg_ncyl = 80;
5250 			cl->cl_g.dkg_pcyl = 80;
5251 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
5252 		} else if (cl->cl_blockcount <= 0x200000) {
5253 			cl->cl_g.dkg_nhead = 64;
5254 			cl->cl_g.dkg_nsect = 32;
5255 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
5256 		} else {
5257 			cl->cl_g.dkg_nhead = 255;
5258 
5259 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
5260 			    (UINT16_MAX * 255 * 63) - 1) /
5261 			    (UINT16_MAX * 255 * 63)) * 63;
5262 
5263 			if (cl->cl_g.dkg_nsect == 0)
5264 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
5265 
5266 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
5267 			    (255 * cl->cl_g.dkg_nsect);
5268 		}
5269 
5270 		cl->cl_g.dkg_acyl = 0;
5271 		cl->cl_g.dkg_bcyl = 0;
5272 		cl->cl_g.dkg_intrlv = 1;
5273 		cl->cl_g.dkg_rpm = 200;
5274 		if (cl->cl_g.dkg_pcyl == 0)
5275 			cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
5276 			    cl->cl_g.dkg_acyl;
5277 	} else {
5278 		cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
5279 		cl->cl_g.dkg_acyl = pgeomp->g_acyl;
5280 		cl->cl_g.dkg_nhead = pgeomp->g_nhead;
5281 		cl->cl_g.dkg_nsect = pgeomp->g_nsect;
5282 		cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
5283 		cl->cl_g.dkg_rpm = pgeomp->g_rpm;
5284 		cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
5285 	}
5286 
5287 	cl->cl_g.dkg_read_reinstruct = 0;
5288 	cl->cl_g.dkg_write_reinstruct = 0;
5289 	cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
5290 	    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
5291 
5292 	cl->cl_map['a'-'a'].dkl_cylno = 0;
5293 	cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
5294 
5295 	cl->cl_map['c'-'a'].dkl_cylno = 0;
5296 	cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
5297 
5298 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
5299 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
5300 	cl->cl_vtoc.v_nparts = V_NUMPAR;
5301 	cl->cl_vtoc.v_version = V_VERSION;
5302 	(void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
5303 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
5304 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
5305 
5306 	cl->cl_f_geometry_is_valid = B_FALSE;
5307 }
5308 
5309 
5310 #if defined(__i386) || defined(__amd64)
5311 /*
5312  *    Function: cmlb_update_fdisk_and_vtoc
5313  *
5314  * Description: This local utility routine updates the device fdisk and vtoc
5315  *		as part of setting the device mboot.
5316  *
5317  *   Arguments:
5318  *	cl		driver soft state (unit) structure
5319  *
5320  *	tg_cookie	cookie from target driver to be passed back to target
5321  *			driver when we call back to it through tg_ops.
5322  *
5323  *
5324  * Return Code: 0 for success or errno-type return code.
5325  *
5326  *    Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
5327  *		these did exist separately in x86 sd.c.
5328  */
5329 static int
5330 cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
5331 {
5332 	int		count;
5333 	int		label_rc = 0;
5334 	int		fdisk_rval;
5335 	diskaddr_t	capacity;
5336 
5337 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5338 
5339 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
5340 		return (EINVAL);
5341 
5342 #if defined(_SUNOS_VTOC_16)
5343 	/*
5344 	 * Set up the "whole disk" fdisk partition; this should always
5345 	 * exist, regardless of whether the disk contains an fdisk table
5346 	 * or vtoc.
5347 	 */
5348 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
5349 	cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
5350 #endif	/* defined(_SUNOS_VTOC_16) */
5351 
5352 	/*
5353 	 * copy the lbasize and capacity so that if they're
5354 	 * reset while we're not holding the CMLB_MUTEX(cl), we will
5355 	 * continue to use valid values after the CMLB_MUTEX(cl) is
5356 	 * reacquired.
5357 	 */
5358 	capacity = cl->cl_blockcount;
5359 
5360 	/*
5361 	 * refresh the logical and physical geometry caches.
5362 	 * (data from mode sense format/rigid disk geometry pages,
5363 	 * and scsi_ifgetcap("geometry").
5364 	 */
5365 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
5366 
5367 	/*
5368 	 * Only DIRECT ACCESS devices will have Scl labels.
5369 	 * CD's supposedly have a Scl label, too
5370 	 */
5371 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
5372 		fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5373 		if (fdisk_rval != 0) {
5374 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5375 			return (fdisk_rval);
5376 		}
5377 
5378 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
5379 			/*
5380 			 * Found fdisk table but no Solaris partition entry,
5381 			 * so don't call cmlb_uselabel() and don't create
5382 			 * a default label.
5383 			 */
5384 			label_rc = 0;
5385 			cl->cl_f_geometry_is_valid = B_TRUE;
5386 			goto no_solaris_partition;
5387 		}
5388 	} else if (capacity < 0) {
5389 		ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5390 		return (EINVAL);
5391 	}
5392 
5393 	/*
5394 	 * For Removable media We reach here if we have found a
5395 	 * SOLARIS PARTITION.
5396 	 * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
5397 	 * PARTITION has changed from the previous one, hence we will setup a
5398 	 * default VTOC in this case.
5399 	 */
5400 	if (!cl->cl_f_geometry_is_valid) {
5401 		/* if we get here it is writable */
5402 		/* we are called from SMBOOT, and after a write of fdisk */
5403 		cmlb_build_default_label(cl, tg_cookie);
5404 		label_rc = 0;
5405 	}
5406 
5407 no_solaris_partition:
5408 
5409 #if defined(_SUNOS_VTOC_16)
5410 	/*
5411 	 * If we have valid geometry, set up the remaining fdisk partitions.
5412 	 * Note that dkl_cylno is not used for the fdisk map entries, so
5413 	 * we set it to an entirely bogus value.
5414 	 */
5415 	for (count = 0; count < FDISK_PARTS; count++) {
5416 		cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT32_MAX;
5417 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
5418 		    cl->cl_fmap[count].fmap_nblk;
5419 		cl->cl_offset[FDISK_P1 + count] =
5420 		    cl->cl_fmap[count].fmap_start;
5421 	}
5422 #endif
5423 
5424 	for (count = 0; count < NDKMAP; count++) {
5425 #if defined(_SUNOS_VTOC_8)
5426 		struct dk_map *lp  = &cl->cl_map[count];
5427 		cl->cl_offset[count] =
5428 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
5429 #elif defined(_SUNOS_VTOC_16)
5430 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
5431 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
5432 #else
5433 #error "No VTOC format defined."
5434 #endif
5435 	}
5436 
5437 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5438 	return (label_rc);
5439 }
5440 #endif
5441 
5442 #if defined(__i386) || defined(__amd64)
5443 static int
5444 cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
5445 {
5446 	int err = 0;
5447 
5448 	/* Return the driver's notion of the media's logical geometry */
5449 	struct dk_geom	disk_geom;
5450 	struct dk_geom	*dkgp = &disk_geom;
5451 
5452 	mutex_enter(CMLB_MUTEX(cl));
5453 	/*
5454 	 * If there is no HBA geometry available, or
5455 	 * if the HBA returned us something that doesn't
5456 	 * really fit into an Int 13/function 8 geometry
5457 	 * result, just fail the ioctl.  See PSARC 1998/313.
5458 	 */
5459 	if (cl->cl_lgeom.g_nhead == 0 ||
5460 	    cl->cl_lgeom.g_nsect == 0 ||
5461 	    cl->cl_lgeom.g_ncyl > 1024) {
5462 		mutex_exit(CMLB_MUTEX(cl));
5463 		err = EINVAL;
5464 	} else {
5465 		dkgp->dkg_ncyl	= cl->cl_lgeom.g_ncyl;
5466 		dkgp->dkg_acyl	= cl->cl_lgeom.g_acyl;
5467 		dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
5468 		dkgp->dkg_nhead	= cl->cl_lgeom.g_nhead;
5469 		dkgp->dkg_nsect	= cl->cl_lgeom.g_nsect;
5470 
5471 		mutex_exit(CMLB_MUTEX(cl));
5472 		if (ddi_copyout(dkgp, (void *)arg,
5473 		    sizeof (struct dk_geom), flag)) {
5474 			err = EFAULT;
5475 		} else {
5476 			err = 0;
5477 		}
5478 	}
5479 	return (err);
5480 }
5481 #endif
5482 
5483 #if defined(__i386) || defined(__amd64)
5484 static int
5485 cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag,
5486     void *tg_cookie)
5487 {
5488 	int err = 0;
5489 	diskaddr_t capacity;
5490 
5491 
5492 	/* Return the driver's notion of the media physical geometry */
5493 	struct dk_geom	disk_geom;
5494 	struct dk_geom	*dkgp = &disk_geom;
5495 
5496 	mutex_enter(CMLB_MUTEX(cl));
5497 
5498 	if (cl->cl_g.dkg_nhead != 0 &&
5499 	    cl->cl_g.dkg_nsect != 0) {
5500 		/*
5501 		 * We succeeded in getting a geometry, but
5502 		 * right now it is being reported as just the
5503 		 * Solaris fdisk partition, just like for
5504 		 * DKIOCGGEOM. We need to change that to be
5505 		 * correct for the entire disk now.
5506 		 */
5507 		bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
5508 		dkgp->dkg_acyl = 0;
5509 		dkgp->dkg_ncyl = cl->cl_blockcount /
5510 		    (dkgp->dkg_nhead * dkgp->dkg_nsect);
5511 	} else {
5512 		bzero(dkgp, sizeof (struct dk_geom));
5513 		/*
5514 		 * This disk does not have a Solaris VTOC
5515 		 * so we must present a physical geometry
5516 		 * that will remain consistent regardless
5517 		 * of how the disk is used. This will ensure
5518 		 * that the geometry does not change regardless
5519 		 * of the fdisk partition type (ie. EFI, FAT32,
5520 		 * Solaris, etc).
5521 		 */
5522 		if (ISCD(cl)) {
5523 			dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
5524 			dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
5525 			dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
5526 			dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
5527 		} else {
5528 			/*
5529 			 * Invalid cl_blockcount can generate invalid
5530 			 * dk_geom and may result in division by zero
5531 			 * system failure. Should make sure blockcount
5532 			 * is valid before using it here.
5533 			 */
5534 			if (cl->cl_blockcount == 0) {
5535 				mutex_exit(CMLB_MUTEX(cl));
5536 				err = EIO;
5537 				return (err);
5538 			}
5539 			/*
5540 			 * Refer to comments related to off-by-1 at the
5541 			 * header of this file
5542 			 */
5543 			if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
5544 				capacity = cl->cl_blockcount - 1;
5545 			else
5546 				capacity = cl->cl_blockcount;
5547 
5548 			cmlb_convert_geometry(cl, capacity, dkgp, tg_cookie);
5549 			dkgp->dkg_acyl = 0;
5550 			dkgp->dkg_ncyl = capacity /
5551 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
5552 		}
5553 	}
5554 	dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5555 
5556 	mutex_exit(CMLB_MUTEX(cl));
5557 	if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
5558 		err = EFAULT;
5559 
5560 	return (err);
5561 }
5562 #endif
5563 
5564 #if defined(__i386) || defined(__amd64)
5565 static int
5566 cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
5567 {
5568 	int err = 0;
5569 
5570 	/*
5571 	 * Return parameters describing the selected disk slice.
5572 	 * Note: this ioctl is for the intel platform only
5573 	 */
5574 	int part;
5575 
5576 	part = CMLBPART(dev);
5577 
5578 	mutex_enter(CMLB_MUTEX(cl));
5579 	/* don't check cl_solaris_size for pN */
5580 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5581 		err = EIO;
5582 		mutex_exit(CMLB_MUTEX(cl));
5583 	} else {
5584 		struct part_info p;
5585 
5586 		p.p_start = (daddr_t)cl->cl_offset[part];
5587 		p.p_length = (int)cl->cl_map[part].dkl_nblk;
5588 		mutex_exit(CMLB_MUTEX(cl));
5589 #ifdef _MULTI_DATAMODEL
5590 		switch (ddi_model_convert_from(flag & FMODELS)) {
5591 		case DDI_MODEL_ILP32:
5592 		{
5593 			struct part_info32 p32;
5594 
5595 			p32.p_start = (daddr32_t)p.p_start;
5596 			p32.p_length = p.p_length;
5597 			if (ddi_copyout(&p32, (void *)arg,
5598 			    sizeof (p32), flag))
5599 				err = EFAULT;
5600 			break;
5601 		}
5602 
5603 		case DDI_MODEL_NONE:
5604 		{
5605 			if (ddi_copyout(&p, (void *)arg, sizeof (p),
5606 			    flag))
5607 				err = EFAULT;
5608 			break;
5609 		}
5610 		}
5611 #else /* ! _MULTI_DATAMODEL */
5612 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5613 			err = EFAULT;
5614 #endif /* _MULTI_DATAMODEL */
5615 	}
5616 	return (err);
5617 }
5618 static int
5619 cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
5620 {
5621 	int err = 0;
5622 
5623 	/*
5624 	 * Return parameters describing the selected disk slice.
5625 	 * Note: this ioctl is for the intel platform only
5626 	 */
5627 	int part;
5628 
5629 	part = CMLBPART(dev);
5630 
5631 	mutex_enter(CMLB_MUTEX(cl));
5632 	/* don't check cl_solaris_size for pN */
5633 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5634 		err = EIO;
5635 		mutex_exit(CMLB_MUTEX(cl));
5636 	} else {
5637 		struct extpart_info p;
5638 
5639 		p.p_start = (diskaddr_t)cl->cl_offset[part];
5640 		p.p_length = (diskaddr_t)cl->cl_map[part].dkl_nblk;
5641 		mutex_exit(CMLB_MUTEX(cl));
5642 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5643 			err = EFAULT;
5644 	}
5645 	return (err);
5646 }
5647 #endif
5648 
5649 int
5650 cmlb_prop_op(cmlb_handle_t cmlbhandle,
5651     dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
5652     char *name, caddr_t valuep, int *lengthp, int part, void *tg_cookie)
5653 {
5654 	struct cmlb_lun	*cl;
5655 	diskaddr_t	capacity;
5656 	uint32_t	lbasize;
5657 	enum		dp { DP_NBLOCKS, DP_BLKSIZE } dp;
5658 	int		callers_length;
5659 	caddr_t		buffer;
5660 	uint64_t	nblocks64;
5661 	uint_t		dblk;
5662 
5663 	/* Always fallback to ddi_prop_op... */
5664 	cl = (struct cmlb_lun *)cmlbhandle;
5665 	if (cl == NULL) {
5666 fallback:	return (ddi_prop_op(dev, dip, prop_op, mod_flags,
5667 		    name, valuep, lengthp));
5668 	}
5669 
5670 	/* Pick up capacity and blocksize information. */
5671 	capacity = cl->cl_blockcount;
5672 	if (capacity == 0)
5673 		goto fallback;
5674 	lbasize = cl->cl_tgt_blocksize;
5675 	if (lbasize == 0)
5676 		lbasize = DEV_BSIZE;	/* 0 -> DEV_BSIZE units */
5677 
5678 	/* Check for dynamic property of whole device. */
5679 	if (dev == DDI_DEV_T_ANY) {
5680 		/* Fallback to ddi_prop_op if we don't understand.  */
5681 		if (strcmp(name, "device-nblocks") == 0)
5682 			dp = DP_NBLOCKS;
5683 		else if (strcmp(name, "device-blksize") == 0)
5684 			dp = DP_BLKSIZE;
5685 		else
5686 			goto fallback;
5687 
5688 		/* get callers length, establish length of our dynamic prop */
5689 		callers_length = *lengthp;
5690 		if (dp == DP_NBLOCKS)
5691 			*lengthp = sizeof (uint64_t);
5692 		else if (dp == DP_BLKSIZE)
5693 			*lengthp = sizeof (uint32_t);
5694 
5695 		/* service request for the length of the property */
5696 		if (prop_op == PROP_LEN)
5697 			return (DDI_PROP_SUCCESS);
5698 
5699 		switch (prop_op) {
5700 		case PROP_LEN_AND_VAL_ALLOC:
5701 			if ((buffer = kmem_alloc(*lengthp,
5702 			    (mod_flags & DDI_PROP_CANSLEEP) ?
5703 			    KM_SLEEP : KM_NOSLEEP)) == NULL)
5704 				return (DDI_PROP_NO_MEMORY);
5705 			*(caddr_t *)valuep = buffer;	/* set callers buf */
5706 			break;
5707 
5708 		case PROP_LEN_AND_VAL_BUF:
5709 			/* the length of the prop and the request must match */
5710 			if (callers_length != *lengthp)
5711 				return (DDI_PROP_INVAL_ARG);
5712 			buffer = valuep;		/* get callers buf */
5713 			break;
5714 
5715 		default:
5716 			return (DDI_PROP_INVAL_ARG);
5717 		}
5718 
5719 		/* transfer the value into the buffer */
5720 		if (dp == DP_NBLOCKS)
5721 			*((uint64_t *)buffer) = capacity;
5722 		else if (dp == DP_BLKSIZE)
5723 			*((uint32_t *)buffer) = lbasize;
5724 
5725 		return (DDI_PROP_SUCCESS);
5726 	}
5727 
5728 	/*
5729 	 * Support dynamic size oriented properties of partition. Requests
5730 	 * issued under conditions where size is valid are passed to
5731 	 * ddi_prop_op_nblocks with the size information, otherwise the
5732 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
5733 	 */
5734 	if (!cmlb_is_valid(cmlbhandle))
5735 		goto fallback;
5736 
5737 	/* Get partition nblocks value. */
5738 	(void) cmlb_partinfo(cmlbhandle, part,
5739 	    (diskaddr_t *)&nblocks64, NULL, NULL, NULL, tg_cookie);
5740 
5741 	/*
5742 	 * Assume partition information is in sys_blocksize units, compute
5743 	 * divisor for size(9P) property representation.
5744 	 */
5745 	dblk = lbasize / cl->cl_sys_blocksize;
5746 
5747 	/* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5748 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
5749 	    name, valuep, lengthp, nblocks64 / dblk, lbasize));
5750 }
5751