xref: /illumos-gate/usr/src/uts/common/io/cmlb.c (revision 86ef0a63)
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 2012 DEY Storage Systems, Inc.  All rights reserved.
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  * Copyright 2016 Toomas Soome <tsoome@me.com>
27  * Copyright (c) 2019, Joyent, Inc.
28  */
29 
30 /*
31  * This module provides support for labeling operations for target
32  * drivers.
33  */
34 
35 #include <sys/scsi/scsi.h>
36 #include <sys/sunddi.h>
37 #include <sys/dklabel.h>
38 #include <sys/dkio.h>
39 #include <sys/vtoc.h>
40 #include <sys/dktp/fdisk.h>
41 #include <sys/vtrace.h>
42 #include <sys/efi_partition.h>
43 #include <sys/cmlb.h>
44 #include <sys/cmlb_impl.h>
45 #if defined(__x86)
46 #include <sys/fs/dv_node.h>
47 #endif
48 #include <sys/ddi_impldefs.h>
49 
50 /*
51  * Driver minor node structure and data table
52  */
53 struct driver_minor_data {
54 	char	*name;
55 	minor_t	minor;
56 	int	type;
57 };
58 
59 static struct driver_minor_data dk_minor_data[] = {
60 	{"a", 0, S_IFBLK},
61 	{"b", 1, S_IFBLK},
62 	{"c", 2, S_IFBLK},
63 	{"d", 3, S_IFBLK},
64 	{"e", 4, S_IFBLK},
65 	{"f", 5, S_IFBLK},
66 	{"g", 6, S_IFBLK},
67 	{"h", 7, S_IFBLK},
68 #if defined(_SUNOS_VTOC_16)
69 	{"i", 8, S_IFBLK},
70 	{"j", 9, S_IFBLK},
71 	{"k", 10, S_IFBLK},
72 	{"l", 11, S_IFBLK},
73 	{"m", 12, S_IFBLK},
74 	{"n", 13, S_IFBLK},
75 	{"o", 14, S_IFBLK},
76 	{"p", 15, S_IFBLK},
77 #endif			/* defined(_SUNOS_VTOC_16) */
78 #if defined(_FIRMWARE_NEEDS_FDISK)
79 	{"q", 16, S_IFBLK},
80 	{"r", 17, S_IFBLK},
81 	{"s", 18, S_IFBLK},
82 	{"t", 19, S_IFBLK},
83 	{"u", 20, S_IFBLK},
84 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
85 	{"a,raw", 0, S_IFCHR},
86 	{"b,raw", 1, S_IFCHR},
87 	{"c,raw", 2, S_IFCHR},
88 	{"d,raw", 3, S_IFCHR},
89 	{"e,raw", 4, S_IFCHR},
90 	{"f,raw", 5, S_IFCHR},
91 	{"g,raw", 6, S_IFCHR},
92 	{"h,raw", 7, S_IFCHR},
93 #if defined(_SUNOS_VTOC_16)
94 	{"i,raw", 8, S_IFCHR},
95 	{"j,raw", 9, S_IFCHR},
96 	{"k,raw", 10, S_IFCHR},
97 	{"l,raw", 11, S_IFCHR},
98 	{"m,raw", 12, S_IFCHR},
99 	{"n,raw", 13, S_IFCHR},
100 	{"o,raw", 14, S_IFCHR},
101 	{"p,raw", 15, S_IFCHR},
102 #endif			/* defined(_SUNOS_VTOC_16) */
103 #if defined(_FIRMWARE_NEEDS_FDISK)
104 	{"q,raw", 16, S_IFCHR},
105 	{"r,raw", 17, S_IFCHR},
106 	{"s,raw", 18, S_IFCHR},
107 	{"t,raw", 19, S_IFCHR},
108 	{"u,raw", 20, S_IFCHR},
109 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
110 	{0}
111 };
112 
113 #if defined(__x86)
114 #if defined(_FIRMWARE_NEEDS_FDISK)
115 static struct driver_minor_data dk_ext_minor_data[] = {
116 	{"p5", 21, S_IFBLK},
117 	{"p6", 22, S_IFBLK},
118 	{"p7", 23, S_IFBLK},
119 	{"p8", 24, S_IFBLK},
120 	{"p9", 25, S_IFBLK},
121 	{"p10", 26, S_IFBLK},
122 	{"p11", 27, S_IFBLK},
123 	{"p12", 28, S_IFBLK},
124 	{"p13", 29, S_IFBLK},
125 	{"p14", 30, S_IFBLK},
126 	{"p15", 31, S_IFBLK},
127 	{"p16", 32, S_IFBLK},
128 	{"p17", 33, S_IFBLK},
129 	{"p18", 34, S_IFBLK},
130 	{"p19", 35, S_IFBLK},
131 	{"p20", 36, S_IFBLK},
132 	{"p21", 37, S_IFBLK},
133 	{"p22", 38, S_IFBLK},
134 	{"p23", 39, S_IFBLK},
135 	{"p24", 40, S_IFBLK},
136 	{"p25", 41, S_IFBLK},
137 	{"p26", 42, S_IFBLK},
138 	{"p27", 43, S_IFBLK},
139 	{"p28", 44, S_IFBLK},
140 	{"p29", 45, S_IFBLK},
141 	{"p30", 46, S_IFBLK},
142 	{"p31", 47, S_IFBLK},
143 	{"p32", 48, S_IFBLK},
144 	{"p33", 49, S_IFBLK},
145 	{"p34", 50, S_IFBLK},
146 	{"p35", 51, S_IFBLK},
147 	{"p36", 52, S_IFBLK},
148 	{"p5,raw", 21, S_IFCHR},
149 	{"p6,raw", 22, S_IFCHR},
150 	{"p7,raw", 23, S_IFCHR},
151 	{"p8,raw", 24, S_IFCHR},
152 	{"p9,raw", 25, S_IFCHR},
153 	{"p10,raw", 26, S_IFCHR},
154 	{"p11,raw", 27, S_IFCHR},
155 	{"p12,raw", 28, S_IFCHR},
156 	{"p13,raw", 29, S_IFCHR},
157 	{"p14,raw", 30, S_IFCHR},
158 	{"p15,raw", 31, S_IFCHR},
159 	{"p16,raw", 32, S_IFCHR},
160 	{"p17,raw", 33, S_IFCHR},
161 	{"p18,raw", 34, S_IFCHR},
162 	{"p19,raw", 35, S_IFCHR},
163 	{"p20,raw", 36, S_IFCHR},
164 	{"p21,raw", 37, S_IFCHR},
165 	{"p22,raw", 38, S_IFCHR},
166 	{"p23,raw", 39, S_IFCHR},
167 	{"p24,raw", 40, S_IFCHR},
168 	{"p25,raw", 41, S_IFCHR},
169 	{"p26,raw", 42, S_IFCHR},
170 	{"p27,raw", 43, S_IFCHR},
171 	{"p28,raw", 44, S_IFCHR},
172 	{"p29,raw", 45, S_IFCHR},
173 	{"p30,raw", 46, S_IFCHR},
174 	{"p31,raw", 47, S_IFCHR},
175 	{"p32,raw", 48, S_IFCHR},
176 	{"p33,raw", 49, S_IFCHR},
177 	{"p34,raw", 50, S_IFCHR},
178 	{"p35,raw", 51, S_IFCHR},
179 	{"p36,raw", 52, S_IFCHR},
180 	{0}
181 };
182 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
183 #endif			/* if defined(__x86) */
184 
185 static struct driver_minor_data dk_minor_data_efi[] = {
186 	{"a", 0, S_IFBLK},
187 	{"b", 1, S_IFBLK},
188 	{"c", 2, S_IFBLK},
189 	{"d", 3, S_IFBLK},
190 	{"e", 4, S_IFBLK},
191 	{"f", 5, S_IFBLK},
192 	{"g", 6, S_IFBLK},
193 	{"wd", 7, S_IFBLK},
194 #if defined(_SUNOS_VTOC_16)
195 	{"i", 8, S_IFBLK},
196 	{"j", 9, S_IFBLK},
197 	{"k", 10, S_IFBLK},
198 	{"l", 11, S_IFBLK},
199 	{"m", 12, S_IFBLK},
200 	{"n", 13, S_IFBLK},
201 	{"o", 14, S_IFBLK},
202 	{"p", 15, S_IFBLK},
203 #endif			/* defined(_SUNOS_VTOC_16) */
204 #if defined(_FIRMWARE_NEEDS_FDISK)
205 	{"q", 16, S_IFBLK},
206 	{"r", 17, S_IFBLK},
207 	{"s", 18, S_IFBLK},
208 	{"t", 19, S_IFBLK},
209 	{"u", 20, S_IFBLK},
210 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
211 	{"a,raw", 0, S_IFCHR},
212 	{"b,raw", 1, S_IFCHR},
213 	{"c,raw", 2, S_IFCHR},
214 	{"d,raw", 3, S_IFCHR},
215 	{"e,raw", 4, S_IFCHR},
216 	{"f,raw", 5, S_IFCHR},
217 	{"g,raw", 6, S_IFCHR},
218 	{"wd,raw", 7, S_IFCHR},
219 #if defined(_SUNOS_VTOC_16)
220 	{"i,raw", 8, S_IFCHR},
221 	{"j,raw", 9, S_IFCHR},
222 	{"k,raw", 10, S_IFCHR},
223 	{"l,raw", 11, S_IFCHR},
224 	{"m,raw", 12, S_IFCHR},
225 	{"n,raw", 13, S_IFCHR},
226 	{"o,raw", 14, S_IFCHR},
227 	{"p,raw", 15, S_IFCHR},
228 #endif			/* defined(_SUNOS_VTOC_16) */
229 #if defined(_FIRMWARE_NEEDS_FDISK)
230 	{"q,raw", 16, S_IFCHR},
231 	{"r,raw", 17, S_IFCHR},
232 	{"s,raw", 18, S_IFCHR},
233 	{"t,raw", 19, S_IFCHR},
234 	{"u,raw", 20, S_IFCHR},
235 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
236 	{0}
237 };
238 
239 /*
240  * Declare the dynamic properties implemented in prop_op(9E) implementation
241  * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
242  * of drivers that call cmlb_attach().
243  */
244 static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
245 	{"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
246 	{"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
247 	{"device-nblocks",	DDI_PROP_TYPE_INT64},
248 	{"device-blksize",	DDI_PROP_TYPE_INT},
249 	{"device-solid-state",	DDI_PROP_TYPE_INT},
250 	{"device-rotational",	DDI_PROP_TYPE_INT},
251 	{NULL}
252 };
253 
254 /*
255  * This implies an upper limit of 8192 GPT partitions
256  * in one transfer for GUID Partition Entry Array.
257  */
258 len_t cmlb_tg_max_efi_xfer = 1024 * 1024;
259 
260 /*
261  * External kernel interfaces
262  */
263 extern struct mod_ops mod_miscops;
264 
265 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
266     int spec_type, minor_t minor_num);
267 
268 /*
269  * Global buffer and mutex for debug logging
270  */
271 static char	cmlb_log_buffer[1024];
272 static kmutex_t	cmlb_log_mutex;
273 
274 
275 struct cmlb_lun *cmlb_debug_cl = NULL;
276 uint_t cmlb_level_mask = 0x0;
277 
278 int cmlb_rot_delay = 4;	/* default rotational delay */
279 
280 static struct modlmisc modlmisc = {
281 	&mod_miscops,   /* Type of module */
282 	"Common Labeling module"
283 };
284 
285 static struct modlinkage modlinkage = {
286 	MODREV_1, (void *)&modlmisc, NULL
287 };
288 
289 /* Local function prototypes */
290 static dev_t cmlb_make_device(struct cmlb_lun *cl);
291 static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
292     int flags, void *tg_cookie);
293 static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
294     void *tg_cookie);
295 static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
296     void *tg_cookie);
297 static void cmlb_swap_efi_gpt(efi_gpt_t *e);
298 static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
299 static int cmlb_validate_efi(efi_gpt_t *labp);
300 static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
301     void *tg_cookie);
302 static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
303 static int  cmlb_uselabel(struct cmlb_lun *cl,  struct dk_label *l, int flags);
304 #if defined(_SUNOS_VTOC_8)
305 static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
306 #endif
307 static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
308 static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
309 static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
310     void *tg_cookie);
311 static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
312 static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
313 static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
314 static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
315 static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
316 static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
317 
318 #if defined(__x86)
319 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
320 #endif
321 
322 #if defined(_FIRMWARE_NEEDS_FDISK)
323 static boolean_t  cmlb_has_max_chs_vals(struct ipart *fdp);
324 #endif
325 
326 #if defined(_SUNOS_VTOC_16)
327 static void cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
328     struct dk_geom *cl_g, void *tg_cookie);
329 #endif
330 
331 static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
332     void *tg_cookie);
333 static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
334 static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
335     void *tg_cookie);
336 static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
337 static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
338     void *tg_cookie);
339 static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
340     int flag, void *tg_cookie);
341 static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
342     void *tg_cookie);
343 static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
344     void *tg_cookie);
345 static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
346     int flag, void *tg_cookie);
347 static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
348     int flag, void *tg_cookie);
349 static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
350     void *tg_cookie);
351 static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
352     void *tg_cookie);
353 static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
354     void *tg_cookie);
355 
356 #if defined(__x86)
357 static int cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
358     void *tg_cookie);
359 static int cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart,
360     uint32_t start, uint32_t size);
361 static int cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start,
362     void *tg_cookie);
363 static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
364 static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag,
365     void *tg_cookie);
366 static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
367     int flag);
368 static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
369     int flag);
370 #endif
371 
372 static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
373 static void cmlb_v_log(dev_info_t *dev, const char *label, uint_t level,
374     const char *fmt, va_list ap);
375 static void cmlb_log(dev_info_t *dev, const char *label, uint_t level,
376     const char *fmt, ...);
377 
378 int
379 _init(void)
380 {
381 	mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
382 	return (mod_install(&modlinkage));
383 }
384 
385 int
386 _info(struct modinfo *modinfop)
387 {
388 	return (mod_info(&modlinkage, modinfop));
389 }
390 
391 int
392 _fini(void)
393 {
394 	int err;
395 
396 	if ((err = mod_remove(&modlinkage)) != 0) {
397 		return (err);
398 	}
399 
400 	mutex_destroy(&cmlb_log_mutex);
401 	return (err);
402 }
403 
404 /*
405  * cmlb_dbg is used for debugging to log additional info
406  * Level of output is controlled via cmlb_level_mask setting.
407  */
408 static void
409 cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
410 {
411 	va_list		ap;
412 	dev_info_t	*dev;
413 	uint_t		level_mask = 0;
414 
415 	ASSERT(cl != NULL);
416 	dev = CMLB_DEVINFO(cl);
417 	ASSERT(dev != NULL);
418 	/*
419 	 * Filter messages based on the global component and level masks,
420 	 * also print if cl matches the value of cmlb_debug_cl, or if
421 	 * cmlb_debug_cl is set to NULL.
422 	 */
423 	if (comp & CMLB_TRACE)
424 		level_mask |= CMLB_LOGMASK_TRACE;
425 
426 	if (comp & CMLB_INFO)
427 		level_mask |= CMLB_LOGMASK_INFO;
428 
429 	if (comp & CMLB_ERROR)
430 		level_mask |= CMLB_LOGMASK_ERROR;
431 
432 	if ((cmlb_level_mask & level_mask) &&
433 	    ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
434 		va_start(ap, fmt);
435 		cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
436 		va_end(ap);
437 	}
438 }
439 
440 /*
441  * cmlb_log is basically a duplicate of scsi_log. It is redefined here
442  * so that this module does not depend on scsi module.
443  */
444 static void
445 cmlb_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt, ...)
446 {
447 	va_list		ap;
448 
449 	va_start(ap, fmt);
450 	cmlb_v_log(dev, label, level, fmt, ap);
451 	va_end(ap);
452 }
453 
454 static void
455 cmlb_v_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt,
456     va_list ap)
457 {
458 	static char	name[256];
459 	int		log_only = 0;
460 	int		boot_only = 0;
461 	int		console_only = 0;
462 
463 	mutex_enter(&cmlb_log_mutex);
464 
465 	if (dev) {
466 		if (level == CE_PANIC || level == CE_WARN ||
467 		    level == CE_NOTE) {
468 			(void) sprintf(name, "%s (%s%d):\n",
469 			    ddi_pathname(dev, cmlb_log_buffer),
470 			    label, ddi_get_instance(dev));
471 		} else {
472 			name[0] = '\0';
473 		}
474 	} else {
475 		(void) sprintf(name, "%s:", label);
476 	}
477 
478 	(void) vsprintf(cmlb_log_buffer, fmt, ap);
479 
480 	switch (cmlb_log_buffer[0]) {
481 	case '!':
482 		log_only = 1;
483 		break;
484 	case '?':
485 		boot_only = 1;
486 		break;
487 	case '^':
488 		console_only = 1;
489 		break;
490 	}
491 
492 	switch (level) {
493 	case CE_NOTE:
494 		level = CE_CONT;
495 		/* FALLTHROUGH */
496 	case CE_CONT:
497 	case CE_WARN:
498 	case CE_PANIC:
499 		if (boot_only) {
500 			cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
501 		} else if (console_only) {
502 			cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
503 		} else if (log_only) {
504 			cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
505 		} else {
506 			cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
507 		}
508 		break;
509 	case CE_IGNORE:
510 		break;
511 	default:
512 		cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
513 		break;
514 	}
515 	mutex_exit(&cmlb_log_mutex);
516 }
517 
518 
519 /*
520  * cmlb_alloc_handle:
521  *
522  *	Allocates a handle.
523  *
524  * Arguments:
525  *	cmlbhandlep	pointer to handle
526  *
527  * Notes:
528  *	Allocates a handle and stores the allocated handle in the area
529  *	pointed to by cmlbhandlep
530  *
531  * Context:
532  *	Kernel thread only (can sleep).
533  */
534 void
535 cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
536 {
537 	struct cmlb_lun	*cl;
538 
539 	cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
540 	ASSERT(cmlbhandlep != NULL);
541 
542 	cl->cl_state = CMLB_INITED;
543 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
544 	mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
545 
546 	*cmlbhandlep = (cmlb_handle_t)(cl);
547 }
548 
549 /*
550  * cmlb_free_handle
551  *
552  *	Frees handle.
553  *
554  * Arguments:
555  *	cmlbhandlep	pointer to handle
556  */
557 void
558 cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
559 {
560 	struct cmlb_lun	*cl;
561 
562 	cl = (struct cmlb_lun *)*cmlbhandlep;
563 	if (cl != NULL) {
564 		mutex_destroy(CMLB_MUTEX(cl));
565 		kmem_free(cl, sizeof (struct cmlb_lun));
566 	}
567 
568 }
569 
570 /*
571  * cmlb_attach:
572  *
573  *	Attach handle to device, create minor nodes for device.
574  *
575  * Arguments:
576  *	devi		pointer to device's dev_info structure.
577  *	tgopsp		pointer to array of functions cmlb can use to callback
578  *			to target driver.
579  *
580  *	device_type	Peripheral device type as defined in
581  *			scsi/generic/inquiry.h
582  *
583  *	is_removable	whether or not device is removable.
584  *
585  *	is_hotpluggable	whether or not device is hotpluggable.
586  *
587  *	node_type	minor node type (as used by ddi_create_minor_node)
588  *
589  *	alter_behavior
590  *			bit flags:
591  *
592  *			CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
593  *			an alternate slice for the default label, if
594  *			device type is DTYPE_DIRECT an architectures default
595  *			label type is VTOC16.
596  *			Otherwise alternate slice will no be created.
597  *
598  *
599  *			CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
600  *			geometry and label for DKIOCGGEOM and DKIOCGVTOC
601  *			on architecture with VTOC8 label types.
602  *
603  *			CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
604  *                      one bug in obtaining capacity (in sd):
605  *			SCSI READ_CAPACITY command returns the LBA number of the
606  *			last logical block, but sd once treated this number as
607  *			disks' capacity on x86 platform. And LBAs are addressed
608  *			based 0. So the last block was lost on x86 platform.
609  *
610  *			Now, we remove this workaround. In order for present sd
611  *			driver to work with disks which are labeled/partitioned
612  *			via previous sd, we add workaround as follows:
613  *
614  *			1) Locate backup EFI label: cmlb searches the next to
615  *			   last
616  *			   block for backup EFI label. If fails, it will
617  *			   turn to the last block for backup EFI label;
618  *
619  *			2) Clear backup EFI label: cmlb first search the last
620  *			   block for backup EFI label, and will search the
621  *			   next to last block only if failed for the last
622  *			   block.
623  *
624  *			3) Calculate geometry:refer to cmlb_convert_geometry()
625  *			   If capacity increasing by 1 causes disks' capacity
626  *			   to cross over the limits in geometry calculation,
627  *			   geometry info will change. This will raise an issue:
628  *			   In case that primary VTOC label is destroyed, format
629  *			   commandline can restore it via backup VTOC labels.
630  *			   And format locates backup VTOC labels by use of
631  *			   geometry. So changing geometry will
632  *			   prevent format from finding backup VTOC labels. To
633  *			   eliminate this side effect for compatibility,
634  *			   sd uses (capacity -1) to calculate geometry;
635  *
636  *			4) 1TB disks: some important data structures use
637  *			   32-bit signed long/int (for example, daddr_t),
638  *			   so that sd doesn't support a disk with capacity
639  *			   larger than 1TB on 32-bit platform. However,
640  *			   for exactly 1TB disk, it was treated as (1T - 512)B
641  *			   in the past, and could have valid Solaris
642  *			   partitions. To workaround this, if an exactly 1TB
643  *			   disk has Solaris fdisk partition, it will be allowed
644  *			   to work with sd.
645  *
646  *
647  *
648  *			CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
649  *			the entire disk, if there is no valid partition info.
650  *			If there is a valid Solaris partition, s0 and s2 will
651  *			only cover the entire Solaris partition.
652  *
653  *			CMLB_CREATE_P0_MINOR_NODE: create p0 node covering
654  *			the entire disk. Used by lofi to ensure presence of
655  *			whole disk device node in case of LOFI_MAP_FILE ioctl.
656  *
657  *	cmlbhandle	cmlb handle associated with device
658  *
659  *	tg_cookie	cookie from target driver to be passed back to target
660  *			driver when we call back to it through tg_ops.
661  *
662  * Notes:
663  *	Assumes a default label based on capacity for non-removable devices.
664  *	If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
665  *	for the architecture).
666  *
667  *	For removable devices, default label type is assumed to be VTOC
668  *	type. Create minor nodes based on a default label type.
669  *	Label on the media is not validated.
670  *	minor number consists of:
671  *		if _SUNOS_VTOC_8 is defined
672  *			lowest 3 bits is taken as partition number
673  *			the rest is instance number
674  *		if _SUNOS_VTOC_16 is defined
675  *			lowest 6 bits is taken as partition number
676  *			the rest is instance number
677  *
678  *
679  * Return values:
680  *	0	Success
681  *	ENXIO	creating minor nodes failed.
682  *	EINVAL  invalid arg, unsupported tg_ops version
683  */
684 int
685 cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
686     boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
687     int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
688 {
689 
690 	struct cmlb_lun	*cl = (struct cmlb_lun *)cmlbhandle;
691 	diskaddr_t	cap;
692 	int		status;
693 
694 	ASSERT(VALID_BOOLEAN(is_removable));
695 	ASSERT(VALID_BOOLEAN(is_hotpluggable));
696 
697 	if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
698 		return (EINVAL);
699 
700 	mutex_enter(CMLB_MUTEX(cl));
701 
702 	CMLB_DEVINFO(cl) = devi;
703 	cl->cmlb_tg_ops = tgopsp;
704 	cl->cl_device_type = device_type;
705 	cl->cl_is_removable = is_removable;
706 	cl->cl_is_hotpluggable = is_hotpluggable;
707 	cl->cl_node_type = node_type;
708 	cl->cl_sys_blocksize = DEV_BSIZE;
709 	cl->cl_f_geometry_is_valid = B_FALSE;
710 	cl->cl_def_labeltype = CMLB_LABEL_VTOC;
711 	cl->cl_alter_behavior = alter_behavior;
712 	cl->cl_reserved = -1;
713 	cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
714 #if defined(__x86)
715 	cl->cl_logical_drive_count = 0;
716 #endif
717 
718 	if (!is_removable) {
719 		mutex_exit(CMLB_MUTEX(cl));
720 		status = DK_TG_GETCAP(cl, &cap, tg_cookie);
721 		mutex_enter(CMLB_MUTEX(cl));
722 		if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
723 			/* set default EFI if > 2TB */
724 			cl->cl_def_labeltype = CMLB_LABEL_EFI;
725 		}
726 	}
727 
728 	/* create minor nodes based on default label type */
729 	cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
730 	cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
731 
732 	if (cmlb_create_minor_nodes(cl) != 0) {
733 		mutex_exit(CMLB_MUTEX(cl));
734 		return (ENXIO);
735 	}
736 
737 	/* Define the dynamic properties for devinfo spapshots. */
738 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
739 
740 	cl->cl_state = CMLB_ATTACHED;
741 
742 	mutex_exit(CMLB_MUTEX(cl));
743 	return (0);
744 }
745 
746 /*
747  * cmlb_detach:
748  *
749  * Invalidate in-core labeling data and remove all minor nodes for
750  * the device associate with handle.
751  *
752  * Arguments:
753  *	cmlbhandle	cmlb handle associated with device.
754  *
755  *	tg_cookie	cookie from target driver to be passed back to target
756  *			driver when we call back to it through tg_ops.
757  *
758  */
759 /*ARGSUSED1*/
760 void
761 cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
762 {
763 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
764 
765 	mutex_enter(CMLB_MUTEX(cl));
766 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
767 	cl->cl_f_geometry_is_valid = B_FALSE;
768 	ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
769 	i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
770 	cl->cl_state = CMLB_INITED;
771 	mutex_exit(CMLB_MUTEX(cl));
772 }
773 
774 /*
775  * cmlb_validate:
776  *
777  *	Validates label.
778  *
779  * Arguments
780  *	cmlbhandle	cmlb handle associated with device.
781  *
782  *	flags		operation flags. used for verbosity control
783  *
784  *	tg_cookie	cookie from target driver to be passed back to target
785  *			driver when we call back to it through tg_ops.
786  *
787  *
788  * Notes:
789  *	If new label type is different from the current, adjust minor nodes
790  *	accordingly.
791  *
792  * Return values:
793  *	0		success
794  *			Note: having fdisk but no solaris partition is assumed
795  *			success.
796  *
797  *	ENOMEM		memory allocation failed
798  *	EIO		i/o errors during read or get capacity
799  *	EACCESS		reservation conflicts
800  *	EINVAL		label was corrupt, or no default label was assumed
801  *	ENXIO		invalid handle
802  */
803 int
804 cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
805 {
806 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
807 	int		rval;
808 	int		ret = 0;
809 
810 	/*
811 	 * Temp work-around checking cl for NULL since there is a bug
812 	 * in sd_detach calling this routine from taskq_dispatch
813 	 * inited function.
814 	 */
815 	if (cl == NULL)
816 		return (ENXIO);
817 
818 	mutex_enter(CMLB_MUTEX(cl));
819 	if (cl->cl_state < CMLB_ATTACHED) {
820 		mutex_exit(CMLB_MUTEX(cl));
821 		return (ENXIO);
822 	}
823 
824 	rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
825 	    flags, tg_cookie);
826 
827 	if (rval == ENOTSUP) {
828 		if (cl->cl_f_geometry_is_valid) {
829 			cl->cl_cur_labeltype = CMLB_LABEL_EFI;
830 			ret = 0;
831 		} else {
832 			ret = EINVAL;
833 		}
834 	} else {
835 		ret = rval;
836 		if (ret == 0)
837 			cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
838 	}
839 
840 	if (ret == 0)
841 		(void) cmlb_create_minor_nodes(cl);
842 
843 	mutex_exit(CMLB_MUTEX(cl));
844 	return (ret);
845 }
846 
847 /*
848  * cmlb_invalidate:
849  *	Invalidate in core label data
850  *
851  * Arguments:
852  *	cmlbhandle	cmlb handle associated with device.
853  *	tg_cookie	cookie from target driver to be passed back to target
854  *			driver when we call back to it through tg_ops.
855  */
856 /*ARGSUSED1*/
857 void
858 cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
859 {
860 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
861 
862 	if (cl == NULL)
863 		return;
864 
865 	mutex_enter(CMLB_MUTEX(cl));
866 	cl->cl_f_geometry_is_valid = B_FALSE;
867 	mutex_exit(CMLB_MUTEX(cl));
868 }
869 
870 /*
871  * cmlb_is_valid
872  *	Get status on whether the incore label/geom data is valid
873  *
874  * Arguments:
875  *	cmlbhandle      cmlb handle associated with device.
876  *
877  * Return values:
878  *	B_TRUE if incore label/geom data is valid.
879  *	B_FALSE otherwise.
880  *
881  */
882 
883 
884 boolean_t
885 cmlb_is_valid(cmlb_handle_t cmlbhandle)
886 {
887 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
888 
889 	if (cmlbhandle == NULL)
890 		return (B_FALSE);
891 
892 	return (cl->cl_f_geometry_is_valid);
893 
894 }
895 
896 
897 
898 /*
899  * cmlb_close:
900  *
901  * Close the device, revert to a default label minor node for the device,
902  * if it is removable.
903  *
904  * Arguments:
905  *	cmlbhandle	cmlb handle associated with device.
906  *
907  *	tg_cookie	cookie from target driver to be passed back to target
908  *			driver when we call back to it through tg_ops.
909  * Return values:
910  *	0	Success
911  *	ENXIO	Re-creating minor node failed.
912  */
913 /*ARGSUSED1*/
914 int
915 cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
916 {
917 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
918 
919 	mutex_enter(CMLB_MUTEX(cl));
920 	cl->cl_f_geometry_is_valid = B_FALSE;
921 
922 	/* revert to default minor node for this device */
923 	if (ISREMOVABLE(cl)) {
924 		cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
925 		(void) cmlb_create_minor_nodes(cl);
926 	}
927 
928 	mutex_exit(CMLB_MUTEX(cl));
929 	return (0);
930 }
931 
932 /*
933  * cmlb_get_devid_block:
934  *	 get the block number where device id is stored.
935  *
936  * Arguments:
937  *	cmlbhandle	cmlb handle associated with device.
938  *	devidblockp	pointer to block number.
939  *	tg_cookie	cookie from target driver to be passed back to target
940  *			driver when we call back to it through tg_ops.
941  *
942  * Notes:
943  *	It stores the block number of device id in the area pointed to
944  *	by devidblockp.
945  *	with the block number of device id.
946  *
947  * Return values:
948  *	0	success
949  *	EINVAL	device id does not apply to current label type.
950  */
951 /*ARGSUSED2*/
952 int
953 cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
954     void *tg_cookie)
955 {
956 	daddr_t			spc, blk, head, cyl;
957 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
958 
959 	mutex_enter(CMLB_MUTEX(cl));
960 	if (cl->cl_state < CMLB_ATTACHED) {
961 		mutex_exit(CMLB_MUTEX(cl));
962 		return (EINVAL);
963 	}
964 
965 	if ((!cl->cl_f_geometry_is_valid) ||
966 	    (cl->cl_solaris_size < DK_LABEL_LOC)) {
967 		mutex_exit(CMLB_MUTEX(cl));
968 		return (EINVAL);
969 	}
970 
971 	if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
972 		if (cl->cl_reserved != -1) {
973 			blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
974 		} else {
975 			mutex_exit(CMLB_MUTEX(cl));
976 			return (EINVAL);
977 		}
978 	} else {
979 		/* if the disk is unlabeled, don't write a devid to it */
980 		if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
981 			mutex_exit(CMLB_MUTEX(cl));
982 			return (EINVAL);
983 		}
984 
985 		/* this geometry doesn't allow us to write a devid */
986 		if (cl->cl_g.dkg_acyl < 2) {
987 			mutex_exit(CMLB_MUTEX(cl));
988 			return (EINVAL);
989 		}
990 
991 		/*
992 		 * Subtract 2 guarantees that the next to last cylinder
993 		 * is used
994 		 */
995 		cyl  = cl->cl_g.dkg_ncyl  + cl->cl_g.dkg_acyl - 2;
996 		spc  = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
997 		head = cl->cl_g.dkg_nhead - 1;
998 		blk  = cl->cl_solaris_offset +
999 		    (cyl * (spc - cl->cl_g.dkg_apc)) +
1000 		    (head * cl->cl_g.dkg_nsect) + 1;
1001 	}
1002 
1003 	*devidblockp = blk;
1004 	mutex_exit(CMLB_MUTEX(cl));
1005 	return (0);
1006 }
1007 
1008 /*
1009  * cmlb_partinfo:
1010  *	Get partition info for specified partition number.
1011  *
1012  * Arguments:
1013  *	cmlbhandle	cmlb handle associated with device.
1014  *	part		partition number
1015  *	nblocksp	pointer to number of blocks
1016  *	startblockp	pointer to starting block
1017  *	partnamep	pointer to name of partition
1018  *	tagp		pointer to tag info
1019  *	tg_cookie	cookie from target driver to be passed back to target
1020  *			driver when we call back to it through tg_ops.
1021  *
1022  *
1023  * Notes:
1024  *	If in-core label is not valid, this functions tries to revalidate
1025  *	the label. If label is valid, it stores the total number of blocks
1026  *	in this partition in the area pointed to by nblocksp, starting
1027  *	block number in area pointed to by startblockp,  pointer to partition
1028  *	name in area pointed to by partnamep, and tag value in area
1029  *	pointed by tagp.
1030  *	For EFI labels, tag value will be set to 0.
1031  *
1032  *	For all nblocksp, startblockp and partnamep, tagp, a value of NULL
1033  *	indicates the corresponding info is not requested.
1034  *
1035  *
1036  * Return values:
1037  *	0	success
1038  *	EINVAL  no valid label or requested partition number is invalid.
1039  *
1040  */
1041 int
1042 cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
1043     diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
1044 {
1045 
1046 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1047 	int rval;
1048 #if defined(__x86)
1049 	int ext_part;
1050 #endif
1051 
1052 	ASSERT(cl != NULL);
1053 	mutex_enter(CMLB_MUTEX(cl));
1054 	if (cl->cl_state < CMLB_ATTACHED) {
1055 		mutex_exit(CMLB_MUTEX(cl));
1056 		return (EINVAL);
1057 	}
1058 
1059 	if (part  < 0 || part >= MAXPART) {
1060 		rval = EINVAL;
1061 	} else {
1062 		if (!cl->cl_f_geometry_is_valid)
1063 			(void) cmlb_validate_geometry((struct cmlb_lun *)cl,
1064 			    B_FALSE, 0, tg_cookie);
1065 
1066 		if (((!cl->cl_f_geometry_is_valid) ||
1067 		    (part < NDKMAP && cl->cl_solaris_size == 0)) &&
1068 		    (part != P0_RAW_DISK)) {
1069 			rval = EINVAL;
1070 		} else {
1071 			if (startblockp != NULL)
1072 				*startblockp = (diskaddr_t)cl->cl_offset[part];
1073 
1074 			if (nblocksp != NULL)
1075 				*nblocksp = (diskaddr_t)
1076 				    cl->cl_map[part].dkl_nblk;
1077 
1078 			if (tagp != NULL)
1079 				*tagp =
1080 				    ((cl->cl_cur_labeltype == CMLB_LABEL_EFI) ||
1081 				    (part >= NDKMAP)) ? V_UNASSIGNED :
1082 				    cl->cl_vtoc.v_part[part].p_tag;
1083 			rval = 0;
1084 		}
1085 
1086 		/* consistent with behavior of sd for getting minor name */
1087 		if (partnamep != NULL) {
1088 #if defined(__x86)
1089 #if defined(_FIRMWARE_NEEDS_FDISK)
1090 		if (part > FDISK_P4) {
1091 			ext_part = part-FDISK_P4-1;
1092 			*partnamep = dk_ext_minor_data[ext_part].name;
1093 		} else
1094 #endif
1095 #endif
1096 			*partnamep = dk_minor_data[part].name;
1097 		}
1098 
1099 	}
1100 
1101 	mutex_exit(CMLB_MUTEX(cl));
1102 	return (rval);
1103 }
1104 
1105 /*
1106  * cmlb_efi_label_capacity:
1107  *	Get capacity stored in EFI disk label.
1108  *
1109  * Arguments:
1110  *	cmlbhandle	cmlb handle associated with device.
1111  *	capacity	pointer to capacity stored in EFI disk label.
1112  *	tg_cookie	cookie from target driver to be passed back to target
1113  *			driver when we call back to it through tg_ops.
1114  *
1115  *
1116  * Notes:
1117  *	If in-core label is not valid, this functions tries to revalidate
1118  *	the label. If label is valid and is an EFI label, it stores the capacity
1119  *      in disk label in the area pointed to by capacity.
1120  *
1121  *
1122  * Return values:
1123  *	0	success
1124  *	EINVAL  no valid EFI label or capacity is NULL.
1125  *
1126  */
1127 int
1128 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1129     void *tg_cookie)
1130 {
1131 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1132 	int rval;
1133 
1134 	ASSERT(cl != NULL);
1135 	mutex_enter(CMLB_MUTEX(cl));
1136 	if (cl->cl_state < CMLB_ATTACHED) {
1137 		mutex_exit(CMLB_MUTEX(cl));
1138 		return (EINVAL);
1139 	}
1140 
1141 	if (!cl->cl_f_geometry_is_valid)
1142 		(void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1143 		    0, tg_cookie);
1144 
1145 	if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1146 	    (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1147 		rval = EINVAL;
1148 	} else {
1149 		*capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1150 		rval = 0;
1151 	}
1152 
1153 	mutex_exit(CMLB_MUTEX(cl));
1154 	return (rval);
1155 }
1156 
1157 /* Caller should make sure Test Unit Ready succeeds before calling this. */
1158 /*ARGSUSED*/
1159 int
1160 cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1161     int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
1162 {
1163 
1164 	int err;
1165 	struct cmlb_lun *cl;
1166 
1167 	cl = (struct cmlb_lun *)cmlbhandle;
1168 
1169 	ASSERT(cl != NULL);
1170 
1171 	mutex_enter(CMLB_MUTEX(cl));
1172 	if (cl->cl_state < CMLB_ATTACHED) {
1173 		mutex_exit(CMLB_MUTEX(cl));
1174 		return (EIO);
1175 	}
1176 
1177 	switch (cmd) {
1178 		case DKIOCSEXTVTOC:
1179 		case DKIOCSGEOM:
1180 		case DKIOCSETEFI:
1181 		case DKIOCSMBOOT:
1182 #if defined(__x86)
1183 		case DKIOCSETEXTPART:
1184 #endif
1185 			break;
1186 		case DKIOCSVTOC:
1187 #if defined(__x86)
1188 		case DKIOCPARTINFO:
1189 #endif
1190 			if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1191 				mutex_exit(CMLB_MUTEX(cl));
1192 				return (EOVERFLOW);
1193 			}
1194 			break;
1195 		default:
1196 			(void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1197 			    tg_cookie);
1198 
1199 			switch (cmd) {
1200 			case DKIOCGVTOC:
1201 			case DKIOCGAPART:
1202 			case DKIOCSAPART:
1203 
1204 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1205 					/* GPT label on disk */
1206 					mutex_exit(CMLB_MUTEX(cl));
1207 					return (ENOTSUP);
1208 				} else if
1209 				    (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1210 					mutex_exit(CMLB_MUTEX(cl));
1211 					return (EOVERFLOW);
1212 				}
1213 				break;
1214 
1215 			case DKIOCGGEOM:
1216 				if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1217 					/* GPT label on disk */
1218 					mutex_exit(CMLB_MUTEX(cl));
1219 					return (ENOTSUP);
1220 				}
1221 				break;
1222 			default:
1223 				break;
1224 			}
1225 	}
1226 
1227 	mutex_exit(CMLB_MUTEX(cl));
1228 
1229 	switch (cmd) {
1230 	case DKIOCGGEOM:
1231 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1232 		err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
1233 		break;
1234 
1235 	case DKIOCSGEOM:
1236 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1237 		err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
1238 		break;
1239 
1240 	case DKIOCGAPART:
1241 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1242 		err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1243 		    flag, tg_cookie);
1244 		break;
1245 
1246 	case DKIOCSAPART:
1247 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1248 		err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
1249 		break;
1250 
1251 	case DKIOCGVTOC:
1252 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1253 		err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
1254 		break;
1255 
1256 	case DKIOCGEXTVTOC:
1257 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1258 		err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1259 		break;
1260 
1261 	case DKIOCGETEFI:
1262 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1263 		err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
1264 		break;
1265 
1266 	case DKIOCPARTITION:
1267 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1268 		err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
1269 		break;
1270 
1271 	case DKIOCSVTOC:
1272 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1273 		err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1274 		    tg_cookie);
1275 		break;
1276 
1277 	case DKIOCSEXTVTOC:
1278 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1279 		err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1280 		    tg_cookie);
1281 		break;
1282 
1283 	case DKIOCSETEFI:
1284 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1285 		err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
1286 		break;
1287 
1288 	case DKIOCGMBOOT:
1289 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1290 		err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1291 		break;
1292 
1293 	case DKIOCSMBOOT:
1294 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1295 		err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1296 		break;
1297 	case DKIOCG_PHYGEOM:
1298 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
1299 #if defined(__x86)
1300 		err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag, tg_cookie);
1301 #else
1302 		err = ENOTTY;
1303 #endif
1304 		break;
1305 	case DKIOCG_VIRTGEOM:
1306 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
1307 #if defined(__x86)
1308 		err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
1309 #else
1310 		err = ENOTTY;
1311 #endif
1312 		break;
1313 	case DKIOCPARTINFO:
1314 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1315 #if defined(__x86)
1316 		err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
1317 #else
1318 		err = ENOTTY;
1319 #endif
1320 		break;
1321 	case DKIOCEXTPARTINFO:
1322 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1323 #if defined(__x86)
1324 		err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1325 #else
1326 		err = ENOTTY;
1327 #endif
1328 		break;
1329 #if defined(__x86)
1330 	case DKIOCSETEXTPART:
1331 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEXTPART");
1332 		err = cmlb_dkio_set_ext_part(cl, (caddr_t)arg, flag, tg_cookie);
1333 		break;
1334 #endif
1335 	default:
1336 		err = ENOTTY;
1337 
1338 	}
1339 
1340 	/*
1341 	 * An ioctl that succeeds and changed ('set') size(9P) information
1342 	 * needs to invalidate the cached devinfo snapshot to avoid having
1343 	 * old information being returned in a snapshots.
1344 	 *
1345 	 * NB: When available, call ddi_change_minor_node() to clear
1346 	 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1347 	 */
1348 	if (err == 0) {
1349 		switch (cmd) {
1350 		case DKIOCSGEOM:
1351 		case DKIOCSAPART:
1352 		case DKIOCSVTOC:
1353 		case DKIOCSEXTVTOC:
1354 		case DKIOCSETEFI:
1355 			i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1356 			    i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1357 		}
1358 	}
1359 	return (err);
1360 }
1361 
1362 dev_t
1363 cmlb_make_device(struct cmlb_lun *cl)
1364 {
1365 	if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1366 		return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1367 		    ddi_get_instance(
1368 		    CMLB_DEVINFO(cl)) << CMLBUNIT_FORCE_P0_SHIFT));
1369 	} else {
1370 		return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1371 		    ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
1372 	}
1373 }
1374 
1375 /*
1376  * Function: cmlb_check_update_blockcount
1377  *
1378  * Description: If current capacity value is invalid, obtains the
1379  *		current capacity from target driver.
1380  *
1381  * Return Code: 0	success
1382  *		EIO	failure
1383  */
1384 static int
1385 cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
1386 {
1387 	int status;
1388 	diskaddr_t capacity;
1389 	uint32_t lbasize;
1390 
1391 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1392 
1393 	if (cl->cl_f_geometry_is_valid)
1394 		return (0);
1395 
1396 	mutex_exit(CMLB_MUTEX(cl));
1397 	status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1398 	if (status != 0) {
1399 		mutex_enter(CMLB_MUTEX(cl));
1400 		return (EIO);
1401 	}
1402 
1403 	status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1404 	mutex_enter(CMLB_MUTEX(cl));
1405 	if (status != 0)
1406 		return (EIO);
1407 
1408 	if ((capacity != 0) && (lbasize != 0)) {
1409 		cl->cl_blockcount = capacity;
1410 		cl->cl_tgt_blocksize = lbasize;
1411 		if (!cl->cl_is_removable) {
1412 			cl->cl_sys_blocksize = lbasize;
1413 		}
1414 		return (0);
1415 	} else {
1416 		return (EIO);
1417 	}
1418 }
1419 
1420 static int
1421 cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
1422     minor_t minor_num, char *node_type, int flag, boolean_t internal)
1423 {
1424 	ASSERT(VALID_BOOLEAN(internal));
1425 
1426 	if (internal)
1427 		return (ddi_create_internal_pathname(dip,
1428 		    name, spec_type, minor_num));
1429 	else
1430 		return (ddi_create_minor_node(dip,
1431 		    name, spec_type, minor_num, node_type, flag));
1432 }
1433 
1434 /*
1435  *    Function: cmlb_create_minor_nodes
1436  *
1437  * Description: Create or adjust the minor device nodes for the instance.
1438  *		Minor nodes are created based on default label type,
1439  *		current label type and last label type we created
1440  *		minor nodes based on.
1441  *
1442  *
1443  *   Arguments: cl - driver soft state (unit) structure
1444  *
1445  * Return Code: 0 success
1446  *		ENXIO	failure.
1447  *
1448  *     Context: Kernel thread context
1449  */
1450 static int
1451 cmlb_create_minor_nodes(struct cmlb_lun *cl)
1452 {
1453 	struct driver_minor_data	*dmdp;
1454 	int				instance, shift;
1455 	char				name[48];
1456 	cmlb_label_t			newlabeltype;
1457 	boolean_t			internal;
1458 
1459 	ASSERT(cl != NULL);
1460 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1461 
1462 	internal = VOID2BOOLEAN(
1463 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
1464 
1465 	if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
1466 		shift = CMLBUNIT_FORCE_P0_SHIFT;
1467 	else
1468 		shift = CMLBUNIT_SHIFT;
1469 
1470 	/* check the most common case */
1471 	if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1472 	    cl->cl_last_labeltype == cl->cl_cur_labeltype) {
1473 		/* do nothing */
1474 		return (0);
1475 	}
1476 
1477 	if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
1478 		/* we should never get here */
1479 		return (ENXIO);
1480 	}
1481 
1482 	if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
1483 		/* first time during attach */
1484 		newlabeltype = cl->cl_def_labeltype;
1485 
1486 		instance = ddi_get_instance(CMLB_DEVINFO(cl));
1487 
1488 		/* Create all the minor nodes for this target. */
1489 		dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
1490 		    dk_minor_data;
1491 		while (dmdp->name != NULL) {
1492 
1493 			(void) sprintf(name, "%s", dmdp->name);
1494 
1495 			if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
1496 			    dmdp->type,
1497 			    (instance << shift) | dmdp->minor,
1498 			    cl->cl_node_type, 0, internal) == DDI_FAILURE) {
1499 				/*
1500 				 * Clean up any nodes that may have been
1501 				 * created, in case this fails in the middle
1502 				 * of the loop.
1503 				 */
1504 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1505 				return (ENXIO);
1506 			}
1507 			dmdp++;
1508 		}
1509 		cl->cl_last_labeltype = newlabeltype;
1510 #if defined(_SUNOS_VTOC_8)
1511 		/*
1512 		 * "emulate" p0 device for sparc, used by lofi
1513 		 */
1514 		if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1515 			if (cmlb_create_minor(CMLB_DEVINFO(cl), "q", S_IFBLK,
1516 			    (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1517 			    cl->cl_node_type, 0, internal) == DDI_FAILURE) {
1518 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1519 				return (ENXIO);
1520 			}
1521 
1522 			if (cmlb_create_minor(CMLB_DEVINFO(cl), "q,raw",
1523 			    S_IFCHR,
1524 			    (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1525 			    cl->cl_node_type, 0, internal) == DDI_FAILURE) {
1526 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1527 				return (ENXIO);
1528 			}
1529 		}
1530 #endif	/* defined(_SUNOS_VTOC_8) */
1531 		return (0);
1532 	}
1533 
1534 	/* Not first time  */
1535 	if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1536 		if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
1537 			/* close time, revert to default. */
1538 			newlabeltype = cl->cl_def_labeltype;
1539 		} else {
1540 			/*
1541 			 * do nothing since the type for which we last created
1542 			 * nodes matches the default
1543 			 */
1544 			return (0);
1545 		}
1546 	} else {
1547 		if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
1548 			/* We are not closing, use current label type */
1549 			newlabeltype = cl->cl_cur_labeltype;
1550 		} else {
1551 			/*
1552 			 * do nothing since the type for which we last created
1553 			 * nodes matches the current label type
1554 			 */
1555 			return (0);
1556 		}
1557 	}
1558 
1559 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
1560 
1561 	/*
1562 	 * Currently we only fix up the s7 node when we are switching
1563 	 * label types from or to EFI. This is consistent with
1564 	 * current behavior of sd.
1565 	 */
1566 	if (newlabeltype == CMLB_LABEL_EFI &&
1567 	    cl->cl_last_labeltype != CMLB_LABEL_EFI) {
1568 		/* from vtoc to EFI */
1569 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1570 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1571 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
1572 		    S_IFBLK, (instance << shift) | WD_NODE,
1573 		    cl->cl_node_type, 0, internal);
1574 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
1575 		    S_IFCHR, (instance << shift) | WD_NODE,
1576 		    cl->cl_node_type, 0, internal);
1577 	} else {
1578 		/* from efi to vtoc */
1579 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1580 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1581 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
1582 		    S_IFBLK, (instance << shift) | WD_NODE,
1583 		    cl->cl_node_type, 0, internal);
1584 		(void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
1585 		    S_IFCHR, (instance << shift) | WD_NODE,
1586 		    cl->cl_node_type, 0, internal);
1587 	}
1588 
1589 	cl->cl_last_labeltype = newlabeltype;
1590 	return (0);
1591 }
1592 
1593 /*
1594  *    Function: cmlb_validate_geometry
1595  *
1596  * Description: Read the label from the disk (if present). Update the unit's
1597  *		geometry and vtoc information from the data in the label.
1598  *		Verify that the label is valid.
1599  *
1600  *   Arguments:
1601  *	cl		driver soft state (unit) structure
1602  *
1603  *	forcerevalid	force revalidation even if we are already valid.
1604  *	flags		operation flags from target driver. Used for verbosity
1605  *			control	at this time.
1606  *	tg_cookie	cookie from target driver to be passed back to target
1607  *			driver when we call back to it through tg_ops.
1608  *
1609  * Return Code: 0 - Successful completion
1610  *		EINVAL  - Invalid value in cl->cl_tgt_blocksize or
1611  *			  cl->cl_blockcount; or label on disk is corrupted
1612  *			  or unreadable.
1613  *		EACCES  - Reservation conflict at the device.
1614  *		ENOMEM  - Resource allocation error
1615  *		ENOTSUP - geometry not applicable
1616  *
1617  *     Context: Kernel thread only (can sleep).
1618  */
1619 static int
1620 cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1621     void *tg_cookie)
1622 {
1623 	int		label_error = 0;
1624 	diskaddr_t	capacity;
1625 	int		count;
1626 
1627 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1628 	ASSERT(VALID_BOOLEAN(forcerevalid));
1629 
1630 	if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1631 		if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
1632 			return (ENOTSUP);
1633 		return (0);
1634 	}
1635 
1636 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
1637 		return (EIO);
1638 
1639 	capacity = cl->cl_blockcount;
1640 
1641 	/*
1642 	 * Set up the "whole disk" fdisk partition; this should always
1643 	 * exist, regardless of whether the disk contains an fdisk table
1644 	 * or vtoc.
1645 	 */
1646 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1647 	cl->cl_offset[P0_RAW_DISK] = 0;
1648 	/*
1649 	 * note if capacity > int32_max(1TB) we are in 64bit environment
1650 	 * so no truncation happens
1651 	 */
1652 	cl->cl_map[P0_RAW_DISK].dkl_nblk  = capacity;
1653 
1654 	/*
1655 	 * Refresh the logical and physical geometry caches.
1656 	 * (data from MODE SENSE format/rigid disk geometry pages,
1657 	 * and scsi_ifgetcap("geometry").
1658 	 */
1659 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
1660 
1661 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1662 	label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
1663 	if (label_error == 0) {
1664 
1665 		/* found a valid EFI label */
1666 		cmlb_dbg(CMLB_TRACE, cl,
1667 		    "cmlb_validate_geometry: found EFI label\n");
1668 		/*
1669 		 * solaris_size and geometry_is_valid are set in
1670 		 * cmlb_use_efi
1671 		 */
1672 		return (ENOTSUP);
1673 	}
1674 
1675 	/* NO EFI label found */
1676 
1677 	if (capacity > CMLB_EXTVTOC_LIMIT) {
1678 		if (label_error == ESRCH) {
1679 			/*
1680 			 * they've configured a LUN over 2TB, but used
1681 			 * format.dat to restrict format's view of the
1682 			 * capacity to be under 2TB in some earlier Solaris
1683 			 * release.
1684 			 */
1685 			/* i.e > 2TB with a VTOC < 2TB */
1686 			if (!(flags & CMLB_SILENT) &&
1687 			    (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
1688 
1689 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1690 				    CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1691 				    "due to VTOC label. To use the full "
1692 				    "capacity of the disk, use format(1M) to "
1693 				    "relabel the disk with EFI/GPT label.\n",
1694 				    CMLB_LABEL(cl),
1695 				    ddi_get_instance(CMLB_DEVINFO(cl)));
1696 
1697 				cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1698 			}
1699 		} else {
1700 				return (ENOTSUP);
1701 		}
1702 	}
1703 
1704 	label_error = 0;
1705 
1706 	/*
1707 	 * at this point it is either labeled with a VTOC or it is
1708 	 * under 1TB (<= 1TB actually for off-by-1)
1709 	 */
1710 
1711 	/*
1712 	 * Only DIRECT ACCESS devices will have Scl labels.
1713 	 * CD's supposedly have a Scl label, too
1714 	 */
1715 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
1716 		struct	dk_label *dkl;
1717 		offset_t label_addr;
1718 		int	rval;
1719 		size_t	buffer_size;
1720 
1721 		/*
1722 		 * Note: This will set up cl->cl_solaris_size and
1723 		 * cl->cl_solaris_offset.
1724 		 */
1725 		rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1726 		if ((rval != 0) && !ISCD(cl)) {
1727 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1728 			return (rval);
1729 		}
1730 
1731 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1732 			/*
1733 			 * Found fdisk table but no Solaris partition entry,
1734 			 * so don't call cmlb_uselabel() and don't create
1735 			 * a default label.
1736 			 */
1737 			label_error = 0;
1738 			cl->cl_f_geometry_is_valid = B_TRUE;
1739 			goto no_solaris_partition;
1740 		}
1741 
1742 		label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1743 
1744 		buffer_size = cl->cl_sys_blocksize;
1745 
1746 		cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
1747 		    "label_addr: 0x%x allocation size: 0x%x\n",
1748 		    label_addr, buffer_size);
1749 
1750 		if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
1751 			return (ENOMEM);
1752 
1753 		mutex_exit(CMLB_MUTEX(cl));
1754 		rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1755 		mutex_enter(CMLB_MUTEX(cl));
1756 
1757 		switch (rval) {
1758 		case 0:
1759 			/*
1760 			 * cmlb_uselabel will establish that the geometry
1761 			 * is valid.
1762 			 */
1763 			if (cmlb_uselabel(cl,
1764 			    (struct dk_label *)(uintptr_t)dkl, flags) !=
1765 			    CMLB_LABEL_IS_VALID) {
1766 				label_error = EINVAL;
1767 			} else
1768 				cl->cl_label_from_media = CMLB_LABEL_VTOC;
1769 			break;
1770 		case EACCES:
1771 			label_error = EACCES;
1772 			break;
1773 		default:
1774 			label_error = EINVAL;
1775 			break;
1776 		}
1777 
1778 		kmem_free(dkl, buffer_size);
1779 	}
1780 
1781 	/*
1782 	 * If a valid label was not found, AND if no reservation conflict
1783 	 * was detected, then go ahead and create a default label (4069506).
1784 	 *
1785 	 * Note: currently, for VTOC_8 devices, the default label is created
1786 	 * for removables and hotpluggables only.  For VTOC_16 devices, the
1787 	 * default label will be created for all devices.
1788 	 * (see cmlb_build_default_label)
1789 	 */
1790 #if defined(_SUNOS_VTOC_8)
1791 	if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1792 	    (label_error != EACCES)) {
1793 #elif defined(_SUNOS_VTOC_16)
1794 	if (label_error != EACCES) {
1795 #endif
1796 		if (!cl->cl_f_geometry_is_valid) {
1797 			cmlb_build_default_label(cl, tg_cookie);
1798 		}
1799 		label_error = 0;
1800 	}
1801 
1802 no_solaris_partition:
1803 
1804 #if defined(_SUNOS_VTOC_16)
1805 	/*
1806 	 * If we have valid geometry, set up the remaining fdisk partitions.
1807 	 * Note that dkl_cylno is not used for the fdisk map entries, so
1808 	 * we set it to an entirely bogus value.
1809 	 */
1810 	for (count = 0; count < FDISK_PARTS; count++) {
1811 		cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1812 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
1813 		    cl->cl_fmap[count].fmap_nblk;
1814 
1815 		cl->cl_offset[FDISK_P1 + count] =
1816 		    cl->cl_fmap[count].fmap_start;
1817 	}
1818 #endif
1819 
1820 	for (count = 0; count < NDKMAP; count++) {
1821 #if defined(_SUNOS_VTOC_8)
1822 		struct dk_map *lp  = &cl->cl_map[count];
1823 		cl->cl_offset[count] =
1824 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
1825 #elif defined(_SUNOS_VTOC_16)
1826 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
1827 
1828 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
1829 #else
1830 #error "No VTOC format defined."
1831 #endif
1832 	}
1833 
1834 	return (label_error);
1835 }
1836 
1837 #if defined(_SUNOS_VTOC_16)
1838 /*
1839  *    Function: cmlb_convert_geometry
1840  *
1841  * Description: Convert physical geometry into a dk_geom structure. In
1842  *		other words, make sure we don't wrap 16-bit values.
1843  *		e.g. converting from geom_cache to dk_geom
1844  *
1845  *     Context: Kernel thread only
1846  */
1847 static void
1848 cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
1849     struct dk_geom *cl_g, void *tg_cookie)
1850 {
1851 
1852 	ASSERT(cl != NULL);
1853 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1854 
1855 	/* Unlabeled SCSI floppy device */
1856 	if (capacity < 160) {
1857 		/* Less than 80K */
1858 		cl_g->dkg_nhead = 1;
1859 		cl_g->dkg_ncyl = capacity;
1860 		cl_g->dkg_nsect = 1;
1861 		return;
1862 	} else if (capacity <= 0x1000) {
1863 		cl_g->dkg_nhead = 2;
1864 		cl_g->dkg_ncyl = 80;
1865 		cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
1866 		return;
1867 	}
1868 
1869 	/*
1870 	 * For all devices we calculate cylinders using the heads and sectors
1871 	 * we assign based on capacity of the device.  The algorithm is
1872 	 * designed to be compatible with the way other operating systems
1873 	 * lay out fdisk tables for X86 and to insure that the cylinders never
1874 	 * exceed 65535 to prevent problems with X86 ioctls that report
1875 	 * geometry.
1876 	 * For some smaller disk sizes we report geometry that matches those
1877 	 * used by X86 BIOS usage. For larger disks, we use SPT that are
1878 	 * multiples of 63, since other OSes that are not limited to 16-bits
1879 	 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
1880 	 *
1881 	 * The following table (in order) illustrates some end result
1882 	 * calculations:
1883 	 *
1884 	 * Maximum number of blocks		nhead	nsect
1885 	 *
1886 	 * 2097152 (1GB)			64	32
1887 	 * 16777216 (8GB)			128	32
1888 	 * 1052819775 (502.02GB)		255	63
1889 	 * 2105639550 (0.98TB)			255	126
1890 	 * 3158459325 (1.47TB)			255	189
1891 	 * 4211279100 (1.96TB)			255	252
1892 	 * 5264098875 (2.45TB)			255	315
1893 	 * ...
1894 	 *
1895 	 * For Solid State Drive(SSD), it uses 4K page size inside and may be
1896 	 * double with every new generation. If the I/O is not aligned with
1897 	 * page size on SSDs, SSDs perform a lot slower.
1898 	 * By default, Solaris partition starts from cylinder 1. It will be
1899 	 * misaligned even with 4K if using heads(255) and SPT(63). To
1900 	 * workaround the problem, if the device is SSD, we use heads(224) and
1901 	 * SPT multiple of 56. Thus the default Solaris partition starts from
1902 	 * a position that aligns with 128K on a 512 bytes sector size SSD.
1903 	 */
1904 
1905 	if (capacity <= 0x200000) {
1906 		cl_g->dkg_nhead = 64;
1907 		cl_g->dkg_nsect = 32;
1908 	} else if (capacity <= 0x01000000) {
1909 		cl_g->dkg_nhead = 128;
1910 		cl_g->dkg_nsect = 32;
1911 	} else {
1912 		tg_attribute_t tgattribute;
1913 		int is_solid_state;
1914 		unsigned short nhead;
1915 		unsigned short nsect;
1916 
1917 		bzero(&tgattribute, sizeof (tg_attribute_t));
1918 
1919 		mutex_exit(CMLB_MUTEX(cl));
1920 		is_solid_state =
1921 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
1922 		    tgattribute.media_is_solid_state : FALSE;
1923 		mutex_enter(CMLB_MUTEX(cl));
1924 
1925 		if (is_solid_state) {
1926 			nhead = 224;
1927 			nsect = 56;
1928 		} else {
1929 			nhead = 255;
1930 			nsect = 63;
1931 		}
1932 
1933 		cl_g->dkg_nhead = nhead;
1934 
1935 		/* make dkg_nsect be smallest multiple of nsect */
1936 		cl_g->dkg_nsect = ((capacity +
1937 		    (UINT16_MAX * nhead * nsect) - 1) /
1938 		    (UINT16_MAX * nhead * nsect)) * nsect;
1939 
1940 		if (cl_g->dkg_nsect == 0)
1941 			cl_g->dkg_nsect = (UINT16_MAX / nsect) * nsect;
1942 	}
1943 
1944 }
1945 #endif
1946 
1947 /*
1948  *    Function: cmlb_resync_geom_caches
1949  *
1950  * Description: (Re)initialize both geometry caches: the virtual geometry
1951  *            information is extracted from the HBA (the "geometry"
1952  *            capability), and the physical geometry cache data is
1953  *            generated by issuing MODE SENSE commands.
1954  *
1955  *   Arguments:
1956  *	cl		driver soft state (unit) structure
1957  *	capacity	disk capacity in #blocks
1958  *	tg_cookie	cookie from target driver to be passed back to target
1959  *			driver when we call back to it through tg_ops.
1960  *
1961  *     Context: Kernel thread only (can sleep).
1962  */
1963 static void
1964 cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1965     void *tg_cookie)
1966 {
1967 	struct	cmlb_geom	pgeom;
1968 	struct	cmlb_geom	lgeom;
1969 	struct	cmlb_geom	*pgeomp = &pgeom;
1970 	unsigned short		nhead;
1971 	unsigned short		nsect;
1972 	int			spc;
1973 	int			ret;
1974 
1975 	ASSERT(cl != NULL);
1976 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1977 
1978 	/*
1979 	 * Ask the controller for its logical geometry.
1980 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1981 	 * then the lgeom cache will be invalid.
1982 	 */
1983 	mutex_exit(CMLB_MUTEX(cl));
1984 	bzero(&lgeom, sizeof (struct cmlb_geom));
1985 	ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1986 	mutex_enter(CMLB_MUTEX(cl));
1987 
1988 	bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
1989 
1990 	/*
1991 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1992 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1993 	 */
1994 	if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1995 	    cl->cl_lgeom.g_nhead == 0) {
1996 		/*
1997 		 * Note: Perhaps this needs to be more adaptive? The rationale
1998 		 * is that, if there's no HBA geometry from the HBA driver, any
1999 		 * guess is good, since this is the physical geometry. If MODE
2000 		 * SENSE fails this gives a max cylinder size for non-LBA access
2001 		 */
2002 		nhead = 255;
2003 		nsect = 63;
2004 	} else {
2005 		nhead = cl->cl_lgeom.g_nhead;
2006 		nsect = cl->cl_lgeom.g_nsect;
2007 	}
2008 
2009 	if (ISCD(cl)) {
2010 		pgeomp->g_nhead = 1;
2011 		pgeomp->g_nsect = nsect * nhead;
2012 	} else {
2013 		pgeomp->g_nhead = nhead;
2014 		pgeomp->g_nsect = nsect;
2015 	}
2016 
2017 	spc = pgeomp->g_nhead * pgeomp->g_nsect;
2018 	pgeomp->g_capacity = capacity;
2019 	if (spc == 0)
2020 		pgeomp->g_ncyl = 0;
2021 	else
2022 		pgeomp->g_ncyl = pgeomp->g_capacity / spc;
2023 	pgeomp->g_acyl = 0;
2024 
2025 	/*
2026 	 * Retrieve fresh geometry data from the hardware, stash it
2027 	 * here temporarily before we rebuild the incore label.
2028 	 *
2029 	 * We want to use the MODE SENSE commands to derive the
2030 	 * physical geometry of the device, but if either command
2031 	 * fails, the logical geometry is used as the fallback for
2032 	 * disk label geometry.
2033 	 */
2034 
2035 	mutex_exit(CMLB_MUTEX(cl));
2036 	(void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
2037 	mutex_enter(CMLB_MUTEX(cl));
2038 
2039 	/*
2040 	 * Now update the real copy while holding the mutex. This
2041 	 * way the global copy is never in an inconsistent state.
2042 	 */
2043 	bcopy(pgeomp, &cl->cl_pgeom,  sizeof (cl->cl_pgeom));
2044 
2045 	cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
2046 	    "(cached from lgeom)\n");
2047 	cmlb_dbg(CMLB_INFO,  cl,
2048 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2049 	    cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
2050 	    cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
2051 	cmlb_dbg(CMLB_INFO,  cl, "   lbasize: %d; capacity: %ld; "
2052 	    "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
2053 	    cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
2054 	    cl->cl_pgeom.g_rpm);
2055 }
2056 
2057 
2058 #if defined(__x86)
2059 /*
2060  *    Function: cmlb_update_ext_minor_nodes
2061  *
2062  * Description: Routine to add/remove extended partition device nodes
2063  *
2064  *   Arguments:
2065  *	cl		driver soft state (unit) structure
2066  *	num_parts	Number of logical drives found on the LUN
2067  *
2068  * Should be called with the mutex held
2069  *
2070  * Return Code: 0 for success
2071  *
2072  *     Context: User and Kernel thread
2073  *
2074  */
2075 static int
2076 cmlb_update_ext_minor_nodes(struct cmlb_lun *cl, int num_parts)
2077 {
2078 	int				i, count, shift;
2079 	char				name[48];
2080 	int				instance;
2081 	struct driver_minor_data	*demdp, *demdpr;
2082 	char				*devnm;
2083 	dev_info_t			*pdip;
2084 	boolean_t			internal;
2085 
2086 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2087 	ASSERT(cl->cl_update_ext_minor_nodes == 1);
2088 
2089 	internal = VOID2BOOLEAN(
2090 	    (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
2091 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
2092 	demdp = dk_ext_minor_data;
2093 	demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2094 
2095 	if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
2096 		shift = CMLBUNIT_FORCE_P0_SHIFT;
2097 	else
2098 		shift = CMLBUNIT_SHIFT;
2099 
2100 	if (cl->cl_logical_drive_count) {
2101 		for (i = 0; i < cl->cl_logical_drive_count; i++) {
2102 			(void) sprintf(name, "%s", demdp->name);
2103 			ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2104 			(void) sprintf(name, "%s", demdpr->name);
2105 			ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2106 			demdp++;
2107 			demdpr++;
2108 		}
2109 		/* There are existing device nodes. Remove them */
2110 		devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2111 		(void) ddi_deviname(cl->cl_devi, devnm);
2112 		pdip = ddi_get_parent(cl->cl_devi);
2113 		(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2114 		kmem_free(devnm, MAXNAMELEN + 1);
2115 	}
2116 
2117 	demdp = dk_ext_minor_data;
2118 	demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2119 
2120 	for (i = 0; i < num_parts; i++) {
2121 		(void) sprintf(name, "%s", demdp->name);
2122 		if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
2123 		    demdp->type,
2124 		    (instance << shift) | demdp->minor,
2125 		    cl->cl_node_type, 0, internal) == DDI_FAILURE) {
2126 			/*
2127 			 * Clean up any nodes that may have been
2128 			 * created, in case this fails in the middle
2129 			 * of the loop.
2130 			 */
2131 			ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2132 			cl->cl_logical_drive_count = 0;
2133 			return (ENXIO);
2134 		}
2135 		(void) sprintf(name, "%s", demdpr->name);
2136 		if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
2137 		    demdpr->type,
2138 		    (instance << shift) | demdpr->minor,
2139 		    cl->cl_node_type, 0) == DDI_FAILURE) {
2140 			/*
2141 			 * Clean up any nodes that may have been
2142 			 * created, in case this fails in the middle
2143 			 * of the loop.
2144 			 */
2145 			ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2146 			cl->cl_logical_drive_count = 0;
2147 			return (ENXIO);
2148 		}
2149 		demdp++;
2150 		demdpr++;
2151 	}
2152 
2153 	/* Update the cl_map array for logical drives */
2154 	for (count = 0; count < MAX_EXT_PARTS; count++) {
2155 		cl->cl_map[FDISK_P4 + 1 + count].dkl_cylno = UINT32_MAX;
2156 		cl->cl_map[FDISK_P4 + 1 + count].dkl_nblk =
2157 		    cl->cl_fmap[FD_NUMPART + count].fmap_nblk;
2158 		cl->cl_offset[FDISK_P4 + 1 + count] =
2159 		    cl->cl_fmap[FD_NUMPART + count].fmap_start;
2160 	}
2161 
2162 	cl->cl_logical_drive_count = i;
2163 	cl->cl_update_ext_minor_nodes = 0;
2164 	return (0);
2165 }
2166 /*
2167  *    Function: cmlb_validate_ext_part
2168  *
2169  * Description: utility routine to validate an extended partition's
2170  *		metadata as found on disk
2171  *
2172  *   Arguments:
2173  *	cl		driver soft state (unit) structure
2174  *	part		partition number of the extended partition
2175  *	epart		partition number of the logical drive
2176  *	start		absolute sector number of the start of the logical
2177  *			drive being validated
2178  *	size		size of logical drive being validated
2179  *
2180  * Return Code: 0 for success
2181  *
2182  *     Context: User and Kernel thread
2183  *
2184  * Algorithm :
2185  * Error cases are :
2186  *	1. If start block is lesser than or equal to the end block
2187  *	2. If either start block or end block is beyond the bounadry
2188  *	   of the extended partition.
2189  *	3. start or end block overlap with existing partitions.
2190  *		To check this, first make sure that the start block doesnt
2191  *		overlap with existing partitions. Then, calculate the
2192  *		possible end block for the given start block that doesnt
2193  *		overlap with existing partitions. This can be calculated by
2194  *		first setting the possible end block to the end of the
2195  *		extended partition (optimistic) and then, checking if there
2196  *		is any other partition that lies after the start of the
2197  *		partition being validated. If so, set the possible end to
2198  *		one block less than the beginning of the next nearest partition
2199  *		If the actual end block is greater than the calculated end
2200  *		block, we have an overlap.
2201  *
2202  */
2203 static int
2204 cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart, uint32_t start,
2205     uint32_t size)
2206 {
2207 	int i;
2208 	uint32_t end = start + size - 1;
2209 	uint32_t ext_start = cl->cl_fmap[part].fmap_start;
2210 	uint32_t ext_end = ext_start + cl->cl_fmap[part].fmap_nblk - 1;
2211 	uint32_t ts, te;
2212 	uint32_t poss_end = ext_end;
2213 
2214 	if (end <= start) {
2215 		return (1);
2216 	}
2217 
2218 	/*
2219 	 * Check if the logical drive boundaries are within that of the
2220 	 * extended partition.
2221 	 */
2222 	if (start <= ext_start || start > ext_end || end <= ext_start ||
2223 	    end > ext_end) {
2224 		return (1);
2225 	}
2226 
2227 	/*
2228 	 * epart will be equal to FD_NUMPART if it is the first logical drive.
2229 	 * There is no need to check for overlaps with other logical drives,
2230 	 * since it is the only logical drive that we have come across so far.
2231 	 */
2232 	if (epart == FD_NUMPART) {
2233 		return (0);
2234 	}
2235 
2236 	/* Check for overlaps with existing logical drives */
2237 	i = FD_NUMPART;
2238 	ts = cl->cl_fmap[FD_NUMPART].fmap_start;
2239 	te = ts + cl->cl_fmap[FD_NUMPART].fmap_nblk - 1;
2240 
2241 	while ((i < epart) && ts && te) {
2242 		if (start >= ts && start <= te) {
2243 			return (1);
2244 		}
2245 
2246 		if ((ts < poss_end) && (ts > start)) {
2247 			poss_end = ts - 1;
2248 		}
2249 
2250 		i++;
2251 		ts = cl->cl_fmap[i].fmap_start;
2252 		te = ts + cl->cl_fmap[i].fmap_nblk - 1;
2253 	}
2254 
2255 	if (end > poss_end) {
2256 		return (1);
2257 	}
2258 
2259 	return (0);
2260 }
2261 
2262 
2263 /*
2264  *    Function: cmlb_is_linux_swap
2265  *
2266  * Description: utility routine to verify if a partition is a linux swap
2267  *		partition or not.
2268  *
2269  *   Arguments:
2270  *	cl		driver soft state (unit) structure
2271  *	part_start	absolute sector number of the start of the partition
2272  *			being verified
2273  *	tg_cookie	cookie from target driver to be passed back to target
2274  *			driver when we call back to it through tg_ops.
2275  *
2276  * Return Code: 0 for success
2277  *
2278  *     Context: User and Kernel thread
2279  *
2280  * Notes:
2281  *	The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2282  *	last 10 bytes of a disk block whose size is that of the linux page
2283  *	size. This disk block is found at the beginning of the swap partition.
2284  */
2285 static int
2286 cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start, void *tg_cookie)
2287 {
2288 	int		i;
2289 	int		rval = -1;
2290 	uint32_t	seek_offset;
2291 	uint32_t	linux_pg_size;
2292 	char		*buf, *linux_swap_magic;
2293 	int		sec_sz = cl->cl_sys_blocksize;
2294 	/* Known linux kernel page sizes */
2295 	uint32_t	linux_pg_size_arr[] = {4096, };
2296 
2297 	ASSERT(cl != NULL);
2298 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2299 
2300 	if ((buf = kmem_zalloc(sec_sz, KM_NOSLEEP)) == NULL) {
2301 		return (ENOMEM);
2302 	}
2303 
2304 	/*
2305 	 * Check if there is a sane Solaris VTOC
2306 	 * If there is a valid vtoc, no need to lookup
2307 	 * for the linux swap signature.
2308 	 */
2309 	mutex_exit(CMLB_MUTEX(cl));
2310 	rval = DK_TG_READ(cl, buf, part_start + DK_LABEL_LOC,
2311 	    sec_sz, tg_cookie);
2312 	mutex_enter(CMLB_MUTEX(cl));
2313 	if (rval != 0) {
2314 		cmlb_dbg(CMLB_ERROR,  cl,
2315 		    "cmlb_is_linux_swap: disk vtoc read err\n");
2316 		rval = EIO;
2317 		goto done;
2318 	}
2319 
2320 	if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
2321 	    (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
2322 		rval = -1;
2323 		goto done;
2324 	}
2325 
2326 
2327 	/* No valid vtoc, so check for linux swap signature */
2328 	linux_swap_magic = buf + sec_sz - 10;
2329 
2330 	for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
2331 		linux_pg_size = linux_pg_size_arr[i];
2332 		seek_offset = linux_pg_size/sec_sz - 1;
2333 		seek_offset += part_start;
2334 
2335 		mutex_exit(CMLB_MUTEX(cl));
2336 		rval = DK_TG_READ(cl, buf, seek_offset, sec_sz, tg_cookie);
2337 		mutex_enter(CMLB_MUTEX(cl));
2338 
2339 		if (rval != 0) {
2340 			cmlb_dbg(CMLB_ERROR,  cl,
2341 			    "cmlb_is_linux_swap: disk read err\n");
2342 			rval = EIO;
2343 			break;
2344 		}
2345 
2346 		rval = -1;
2347 
2348 		if ((strncmp(linux_swap_magic, "SWAP-SPACE", 10) == 0) ||
2349 		    (strncmp(linux_swap_magic, "SWAPSPACE2", 10) == 0)) {
2350 			/* Found a linux swap */
2351 			rval = 0;
2352 			break;
2353 		}
2354 	}
2355 
2356 done:
2357 	kmem_free(buf, sec_sz);
2358 	return (rval);
2359 }
2360 #endif
2361 
2362 /*
2363  *    Function: cmlb_read_fdisk
2364  *
2365  * Description: utility routine to read the fdisk table.
2366  *
2367  *   Arguments:
2368  *	cl		driver soft state (unit) structure
2369  *	capacity	disk capacity in #blocks
2370  *	tg_cookie	cookie from target driver to be passed back to target
2371  *			driver when we call back to it through tg_ops.
2372  *
2373  * Return Code: 0 for success (includes not reading for no_fdisk_present case
2374  *		errnos from tg_rw if failed to read the first block.
2375  *
2376  *     Context: Kernel thread only (can sleep).
2377  */
2378 /*ARGSUSED*/
2379 static int
2380 cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
2381 {
2382 #if defined(_NO_FDISK_PRESENT)
2383 
2384 	cl->cl_solaris_offset = 0;
2385 	cl->cl_solaris_size = capacity;
2386 	bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2387 	return (0);
2388 
2389 #elif defined(_FIRMWARE_NEEDS_FDISK)
2390 
2391 	struct ipart	*fdp;
2392 	struct mboot	*mbp;
2393 	struct ipart	fdisk[FD_NUMPART];
2394 	int		i, k;
2395 	char		sigbuf[2];
2396 	caddr_t		bufp;
2397 	int		uidx;
2398 	int		rval;
2399 	int		lba = 0;
2400 	uint_t		solaris_offset;	/* offset to solaris part. */
2401 	daddr_t		solaris_size;	/* size of solaris partition */
2402 	uint32_t	blocksize;
2403 #if defined(__x86)
2404 	struct ipart	eparts[2];
2405 	struct ipart	*efdp1 = &eparts[0];
2406 	struct ipart	*efdp2 = &eparts[1];
2407 	int		ext_part_exists = 0;
2408 	int		ld_count = 0;
2409 #endif
2410 
2411 	ASSERT(cl != NULL);
2412 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2413 
2414 	/*
2415 	 * Start off assuming no fdisk table
2416 	 */
2417 	solaris_offset = 0;
2418 	solaris_size   = capacity;
2419 
2420 	blocksize = cl->cl_tgt_blocksize;
2421 
2422 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
2423 
2424 	mutex_exit(CMLB_MUTEX(cl));
2425 	rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
2426 	mutex_enter(CMLB_MUTEX(cl));
2427 
2428 	if (rval != 0) {
2429 		cmlb_dbg(CMLB_ERROR,  cl,
2430 		    "cmlb_read_fdisk: fdisk read err\n");
2431 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2432 		goto done;
2433 	}
2434 
2435 	mbp = (struct mboot *)bufp;
2436 
2437 	/*
2438 	 * The fdisk table does not begin on a 4-byte boundary within the
2439 	 * master boot record, so we copy it to an aligned structure to avoid
2440 	 * alignment exceptions on some processors.
2441 	 */
2442 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2443 
2444 	/*
2445 	 * Check for lba support before verifying sig; sig might not be
2446 	 * there, say on a blank disk, but the max_chs mark may still
2447 	 * be present.
2448 	 *
2449 	 * Note: LBA support and BEFs are an x86-only concept but this
2450 	 * code should work OK on SPARC as well.
2451 	 */
2452 
2453 	/*
2454 	 * First, check for lba-access-ok on root node (or prom root node)
2455 	 * if present there, don't need to search fdisk table.
2456 	 */
2457 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
2458 	    "lba-access-ok", 0) != 0) {
2459 		/* All drives do LBA; don't search fdisk table */
2460 		lba = 1;
2461 	} else {
2462 		/* Okay, look for mark in fdisk table */
2463 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2464 			/* accumulate "lba" value from all partitions */
2465 			lba = (lba || cmlb_has_max_chs_vals(fdp));
2466 		}
2467 	}
2468 
2469 	if (lba != 0) {
2470 		dev_t dev = cmlb_make_device(cl);
2471 
2472 		if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
2473 		    "lba-access-ok", 0) == 0) {
2474 			/* not found; create it */
2475 			if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
2476 			    "lba-access-ok", (caddr_t)NULL, 0) !=
2477 			    DDI_PROP_SUCCESS) {
2478 				cmlb_dbg(CMLB_ERROR,  cl,
2479 				    "cmlb_read_fdisk: Can't create lba "
2480 				    "property for instance %d\n",
2481 				    ddi_get_instance(CMLB_DEVINFO(cl)));
2482 			}
2483 		}
2484 	}
2485 
2486 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
2487 
2488 	/*
2489 	 * Endian-independent signature check
2490 	 */
2491 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
2492 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
2493 		cmlb_dbg(CMLB_ERROR,  cl,
2494 		    "cmlb_read_fdisk: no fdisk\n");
2495 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2496 		goto done;
2497 	}
2498 
2499 #ifdef CMLBDEBUG
2500 	if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
2501 		fdp = fdisk;
2502 		cmlb_dbg(CMLB_INFO,  cl, "cmlb_read_fdisk:\n");
2503 		cmlb_dbg(CMLB_INFO,  cl, "         relsect    "
2504 		    "numsect         sysid       bootid\n");
2505 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
2506 			cmlb_dbg(CMLB_INFO,  cl,
2507 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
2508 			    i, fdp->relsect, fdp->numsect,
2509 			    fdp->systid, fdp->bootid);
2510 		}
2511 	}
2512 #endif
2513 
2514 	/*
2515 	 * Try to find the unix partition
2516 	 */
2517 	uidx = -1;
2518 	solaris_offset = 0;
2519 	solaris_size   = 0;
2520 
2521 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2522 		uint32_t relsect;
2523 		uint32_t numsect;
2524 		uchar_t systid;
2525 #if defined(__x86)
2526 		/*
2527 		 * Stores relative block offset from the beginning of the
2528 		 * Extended Partition.
2529 		 */
2530 		int	ext_relsect = 0;
2531 #endif
2532 
2533 		if (fdp->numsect == 0) {
2534 			cl->cl_fmap[i].fmap_start = 0;
2535 			cl->cl_fmap[i].fmap_nblk  = 0;
2536 			continue;
2537 		}
2538 
2539 		/*
2540 		 * Data in the fdisk table is little-endian.
2541 		 */
2542 		relsect = LE_32(fdp->relsect);
2543 		numsect = LE_32(fdp->numsect);
2544 
2545 		cl->cl_fmap[i].fmap_start = relsect;
2546 		cl->cl_fmap[i].fmap_nblk  = numsect;
2547 		cl->cl_fmap[i].fmap_systid = LE_8(fdp->systid);
2548 
2549 #if defined(__x86)
2550 		/* Support only one extended partition per LUN */
2551 		if ((fdp->systid == EXTDOS || fdp->systid == FDISK_EXTLBA) &&
2552 		    (ext_part_exists == 0)) {
2553 			int j;
2554 			uint32_t logdrive_offset;
2555 			uint32_t ext_numsect;
2556 			uint32_t abs_secnum;
2557 
2558 			ext_part_exists = 1;
2559 
2560 			for (j = FD_NUMPART; j < FDISK_PARTS; j++) {
2561 				mutex_exit(CMLB_MUTEX(cl));
2562 				rval = DK_TG_READ(cl, bufp,
2563 				    (relsect + ext_relsect), blocksize,
2564 				    tg_cookie);
2565 				mutex_enter(CMLB_MUTEX(cl));
2566 
2567 				if (rval != 0) {
2568 					cmlb_dbg(CMLB_ERROR,  cl,
2569 					    "cmlb_read_fdisk: Extended "
2570 					    "partition read err\n");
2571 					goto done;
2572 				}
2573 				/*
2574 				 * The first ipart entry provides the offset
2575 				 * at which the logical drive starts off from
2576 				 * the beginning of the container partition
2577 				 * and the size of the logical drive.
2578 				 * The second ipart entry provides the offset
2579 				 * of the next container partition from the
2580 				 * beginning of the extended partition.
2581 				 */
2582 				bcopy(&bufp[FDISK_PART_TABLE_START], eparts,
2583 				    sizeof (eparts));
2584 				logdrive_offset = LE_32(efdp1->relsect);
2585 				ext_numsect = LE_32(efdp1->numsect);
2586 				systid = LE_8(efdp1->systid);
2587 				if (logdrive_offset <= 0 || ext_numsect <= 0)
2588 					break;
2589 				abs_secnum = relsect + ext_relsect +
2590 				    logdrive_offset;
2591 
2592 				/* Boundary condition and overlap checking */
2593 				if (cmlb_validate_ext_part(cl, i, j, abs_secnum,
2594 				    ext_numsect)) {
2595 					break;
2596 				}
2597 
2598 				if ((cl->cl_fmap[j].fmap_start != abs_secnum) ||
2599 				    (cl->cl_fmap[j].fmap_nblk != ext_numsect) ||
2600 				    (cl->cl_fmap[j].fmap_systid != systid)) {
2601 					/*
2602 					 * Indicates change from previous
2603 					 * partinfo. Need to recreate
2604 					 * logical device nodes.
2605 					 */
2606 					cl->cl_update_ext_minor_nodes = 1;
2607 				}
2608 				cl->cl_fmap[j].fmap_start = abs_secnum;
2609 				cl->cl_fmap[j].fmap_nblk  = ext_numsect;
2610 				cl->cl_fmap[j].fmap_systid = systid;
2611 				ld_count++;
2612 
2613 				if ((efdp1->systid == SUNIXOS &&
2614 				    (cmlb_is_linux_swap(cl, abs_secnum,
2615 				    tg_cookie) != 0)) ||
2616 				    efdp1->systid == SUNIXOS2) {
2617 					if (uidx == -1) {
2618 						uidx = 0;
2619 						solaris_offset = abs_secnum;
2620 						solaris_size = ext_numsect;
2621 					}
2622 				}
2623 
2624 				if ((ext_relsect = LE_32(efdp2->relsect)) == 0)
2625 					break;
2626 			}
2627 		}
2628 
2629 #endif
2630 
2631 		if (fdp->systid != SUNIXOS &&
2632 		    fdp->systid != SUNIXOS2 &&
2633 		    fdp->systid != EFI_PMBR) {
2634 			continue;
2635 		}
2636 
2637 		/*
2638 		 * use the last active solaris partition id found
2639 		 * (there should only be 1 active partition id)
2640 		 *
2641 		 * if there are no active solaris partition id
2642 		 * then use the first inactive solaris partition id
2643 		 */
2644 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
2645 #if defined(__x86)
2646 			if (fdp->systid != SUNIXOS ||
2647 			    (fdp->systid == SUNIXOS &&
2648 			    (cmlb_is_linux_swap(cl, relsect,
2649 			    tg_cookie) != 0))) {
2650 #endif
2651 				uidx = i;
2652 				solaris_offset = relsect;
2653 				solaris_size   = numsect;
2654 #if defined(__x86)
2655 			}
2656 #endif
2657 		}
2658 	}
2659 #if defined(__x86)
2660 	if (ld_count < cl->cl_logical_drive_count) {
2661 		/*
2662 		 * Some/all logical drives were deleted. Clear out
2663 		 * the fmap entries correspoding to those deleted drives.
2664 		 */
2665 		for (k = ld_count + FD_NUMPART;
2666 		    k < cl->cl_logical_drive_count + FD_NUMPART; k++) {
2667 			cl->cl_fmap[k].fmap_start = 0;
2668 			cl->cl_fmap[k].fmap_nblk  = 0;
2669 			cl->cl_fmap[k].fmap_systid = 0;
2670 		}
2671 		cl->cl_update_ext_minor_nodes = 1;
2672 	}
2673 	if (cl->cl_update_ext_minor_nodes) {
2674 		rval = cmlb_update_ext_minor_nodes(cl, ld_count);
2675 		if (rval != 0) {
2676 			goto done;
2677 		}
2678 	}
2679 #endif
2680 	cmlb_dbg(CMLB_INFO,  cl, "fdisk 0x%x 0x%lx",
2681 	    cl->cl_solaris_offset, cl->cl_solaris_size);
2682 done:
2683 
2684 	/*
2685 	 * Clear the VTOC info, only if the Solaris partition entry
2686 	 * has moved, changed size, been deleted, or if the size of
2687 	 * the partition is too small to even fit the label sector.
2688 	 */
2689 	if ((cl->cl_solaris_offset != solaris_offset) ||
2690 	    (cl->cl_solaris_size != solaris_size) ||
2691 	    solaris_size <= DK_LABEL_LOC) {
2692 		cmlb_dbg(CMLB_INFO,  cl, "fdisk moved 0x%x 0x%lx",
2693 		    solaris_offset, solaris_size);
2694 		bzero(&cl->cl_g, sizeof (struct dk_geom));
2695 		bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2696 		bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
2697 		cl->cl_f_geometry_is_valid = B_FALSE;
2698 	}
2699 	cl->cl_solaris_offset = solaris_offset;
2700 	cl->cl_solaris_size = solaris_size;
2701 	kmem_free(bufp, blocksize);
2702 	return (rval);
2703 
2704 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
2705 #error "fdisk table presence undetermined for this platform."
2706 #endif	/* #if defined(_NO_FDISK_PRESENT) */
2707 }
2708 
2709 static void
2710 cmlb_swap_efi_gpt(efi_gpt_t *e)
2711 {
2712 	_NOTE(ASSUMING_PROTECTED(*e))
2713 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
2714 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
2715 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
2716 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
2717 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
2718 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
2719 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
2720 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
2721 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
2722 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
2723 	e->efi_gpt_NumberOfPartitionEntries =
2724 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
2725 	e->efi_gpt_SizeOfPartitionEntry =
2726 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
2727 	e->efi_gpt_PartitionEntryArrayCRC32 =
2728 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
2729 }
2730 
2731 static void
2732 cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
2733 {
2734 	int i;
2735 
2736 	_NOTE(ASSUMING_PROTECTED(*p))
2737 	for (i = 0; i < nparts; i++) {
2738 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
2739 		    p[i].efi_gpe_PartitionTypeGUID);
2740 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
2741 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
2742 		/* PartitionAttrs */
2743 	}
2744 }
2745 
2746 static int
2747 cmlb_validate_efi(efi_gpt_t *labp)
2748 {
2749 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
2750 		return (EINVAL);
2751 	/* at least 92 bytes in this version of the spec. */
2752 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
2753 	    labp->efi_gpt_HeaderSize)
2754 		return (EINVAL);
2755 	/* this should be 128 bytes */
2756 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
2757 		return (EINVAL);
2758 	return (0);
2759 }
2760 
2761 /*
2762  * This function returns B_FALSE if there is a valid MBR signature and no
2763  * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2764  *
2765  * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2766  * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2767  * where a PMBR entry is not the only one. Also, if the first block has been
2768  * corrupted, currently best attempt to allow data access would be to try to
2769  * check for GPT headers. Hence in case of more than one partition entry, but
2770  * at least one EFI_PMBR partition type or no valid magic number, the function
2771  * returns B_TRUE to continue with looking for GPT header.
2772  */
2773 
2774 static boolean_t
2775 cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2776 {
2777 	struct ipart	*fdp;
2778 	struct mboot	*mbp = (struct mboot *)buf;
2779 	struct ipart	fdisk[FD_NUMPART];
2780 	int		i;
2781 
2782 	if (is_mbr != NULL)
2783 		*is_mbr = B_TRUE;
2784 
2785 	if (LE_16(mbp->signature) != MBB_MAGIC) {
2786 		if (is_mbr != NULL)
2787 			*is_mbr = B_FALSE;
2788 		return (B_TRUE);
2789 	}
2790 
2791 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2792 
2793 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2794 		if (fdp->systid == EFI_PMBR)
2795 			return (B_TRUE);
2796 	}
2797 
2798 	return (B_FALSE);
2799 }
2800 
2801 static int
2802 cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2803     void *tg_cookie)
2804 {
2805 	int		i;
2806 	int		rval = 0;
2807 	efi_gpe_t	*partitions;
2808 	uchar_t		*buf;
2809 	uint_t		lbasize;	/* is really how much to read */
2810 	diskaddr_t	cap = 0;
2811 	uint_t		nparts;
2812 	diskaddr_t	gpe_lba;
2813 	diskaddr_t	alternate_lba;
2814 	int		iofailed = 0;
2815 	struct uuid	uuid_type_reserved = EFI_RESERVED;
2816 #if defined(_FIRMWARE_NEEDS_FDISK)
2817 	boolean_t	is_mbr;
2818 #endif
2819 
2820 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2821 
2822 	lbasize = cl->cl_sys_blocksize;
2823 
2824 	cl->cl_reserved = -1;
2825 	mutex_exit(CMLB_MUTEX(cl));
2826 
2827 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
2828 
2829 	rval = DK_TG_READ(cl, buf,  0, lbasize, tg_cookie);
2830 	if (rval) {
2831 		iofailed = 1;
2832 		goto done_err;
2833 	}
2834 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
2835 		/* not ours */
2836 		rval = ESRCH;
2837 		goto done_err;
2838 	}
2839 
2840 #if defined(_FIRMWARE_NEEDS_FDISK)
2841 	if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
2842 		if (is_mbr)
2843 			rval = ESRCH;
2844 		else
2845 			rval = EINVAL;
2846 		goto done_err;
2847 	}
2848 #else
2849 	if (!cmlb_check_efi_mbr(buf, NULL)) {
2850 		rval = EINVAL;
2851 		goto done_err;
2852 	}
2853 
2854 #endif
2855 
2856 	rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
2857 	if (rval) {
2858 		iofailed = 1;
2859 		goto done_err;
2860 	}
2861 	cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2862 
2863 	if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2864 		/*
2865 		 * Couldn't read the primary, try the backup.  Our
2866 		 * capacity at this point could be based on CHS, so
2867 		 * check what the device reports.
2868 		 */
2869 		rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
2870 		if (rval) {
2871 			iofailed = 1;
2872 			goto done_err;
2873 		}
2874 
2875 		/*
2876 		 * CMLB_OFF_BY_ONE case, we check the next to last block first
2877 		 * for backup GPT header, otherwise check the last block.
2878 		 */
2879 
2880 		if ((rval = DK_TG_READ(cl, buf,
2881 		    cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2882 		    lbasize, tg_cookie))
2883 		    != 0) {
2884 			iofailed = 1;
2885 			goto done_err;
2886 		}
2887 		cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2888 
2889 		if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2890 
2891 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2892 				goto done_err;
2893 			if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2894 			    tg_cookie)) != 0)
2895 				goto done_err;
2896 			cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2897 			if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
2898 				goto done_err;
2899 		}
2900 		if (!(flags & CMLB_SILENT))
2901 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2902 			    "primary label corrupt; using backup\n");
2903 	}
2904 
2905 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
2906 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2907 	alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
2908 
2909 	rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
2910 	if (rval) {
2911 		iofailed = 1;
2912 		goto done_err;
2913 	}
2914 	partitions = (efi_gpe_t *)buf;
2915 
2916 	if (nparts > MAXPART) {
2917 		nparts = MAXPART;
2918 	}
2919 	cmlb_swap_efi_gpe(nparts, partitions);
2920 
2921 	mutex_enter(CMLB_MUTEX(cl));
2922 
2923 	/* Fill in partition table. */
2924 	for (i = 0; i < nparts; i++) {
2925 		if (partitions->efi_gpe_StartingLBA != 0 ||
2926 		    partitions->efi_gpe_EndingLBA != 0) {
2927 			cl->cl_map[i].dkl_cylno =
2928 			    partitions->efi_gpe_StartingLBA;
2929 			cl->cl_map[i].dkl_nblk =
2930 			    partitions->efi_gpe_EndingLBA -
2931 			    partitions->efi_gpe_StartingLBA + 1;
2932 			cl->cl_offset[i] =
2933 			    partitions->efi_gpe_StartingLBA;
2934 		}
2935 
2936 		if (cl->cl_reserved == -1) {
2937 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2938 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2939 				cl->cl_reserved = i;
2940 			}
2941 		}
2942 		if (i == WD_NODE) {
2943 			/*
2944 			 * minor number 7 corresponds to the whole disk
2945 			 * if the disk capacity is expanded after disk is
2946 			 * labeled, minor number 7 represents the capacity
2947 			 * indicated by the disk label.
2948 			 */
2949 			cl->cl_map[i].dkl_cylno = 0;
2950 			if (alternate_lba == 1) {
2951 				/*
2952 				 * We are using backup label. Since we can
2953 				 * find a valid label at the end of disk,
2954 				 * the disk capacity is not expanded.
2955 				 */
2956 				cl->cl_map[i].dkl_nblk = capacity;
2957 			} else {
2958 				cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2959 			}
2960 			cl->cl_offset[i] = 0;
2961 		}
2962 		partitions++;
2963 	}
2964 	cl->cl_solaris_offset = 0;
2965 	cl->cl_solaris_size = capacity;
2966 	cl->cl_label_from_media = CMLB_LABEL_EFI;
2967 	cl->cl_f_geometry_is_valid = B_TRUE;
2968 
2969 	/* clear the vtoc label */
2970 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2971 
2972 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2973 	return (0);
2974 
2975 done_err:
2976 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2977 	mutex_enter(CMLB_MUTEX(cl));
2978 done_err1:
2979 	/*
2980 	 * if we didn't find something that could look like a VTOC
2981 	 * and the disk is over 1TB, we know there isn't a valid label.
2982 	 * Otherwise let cmlb_uselabel decide what to do.  We only
2983 	 * want to invalidate this if we're certain the label isn't
2984 	 * valid because cmlb_prop_op will now fail, which in turn
2985 	 * causes things like opens and stats on the partition to fail.
2986 	 */
2987 	if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
2988 		cl->cl_f_geometry_is_valid = B_FALSE;
2989 	}
2990 	return (rval);
2991 }
2992 
2993 
2994 /*
2995  *    Function: cmlb_uselabel
2996  *
2997  * Description: Validate the disk label and update the relevant data (geometry,
2998  *		partition, vtoc, and capacity data) in the cmlb_lun struct.
2999  *		Marks the geometry of the unit as being valid.
3000  *
3001  *   Arguments: cl: unit struct.
3002  *		dk_label: disk label
3003  *
3004  * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
3005  *		partition, vtoc, and capacity data are good.
3006  *
3007  *		CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
3008  *		label; or computed capacity does not jibe with capacity
3009  *		reported from the READ CAPACITY command.
3010  *
3011  *     Context: Kernel thread only (can sleep).
3012  */
3013 static int
3014 cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
3015 {
3016 	short		*sp;
3017 	short		sum;
3018 	short		count;
3019 	int		label_error = CMLB_LABEL_IS_VALID;
3020 	int		i;
3021 	diskaddr_t	label_capacity;
3022 	uint32_t	part_end;
3023 	diskaddr_t	track_capacity;
3024 #if defined(_SUNOS_VTOC_16)
3025 	struct	dkl_partition	*vpartp;
3026 #endif
3027 	ASSERT(cl != NULL);
3028 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3029 
3030 	/* Validate the magic number of the label. */
3031 	if (labp->dkl_magic != DKL_MAGIC) {
3032 #if defined(__sparc)
3033 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3034 			if (!(flags & CMLB_SILENT))
3035 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3036 				    CE_WARN,
3037 				    "Corrupt label; wrong magic number\n");
3038 		}
3039 #endif
3040 		return (CMLB_LABEL_IS_INVALID);
3041 	}
3042 
3043 	/* Validate the checksum of the label. */
3044 	sp  = (short *)labp;
3045 	sum = 0;
3046 	count = sizeof (struct dk_label) / sizeof (short);
3047 	while (count--)	 {
3048 		sum ^= *sp++;
3049 	}
3050 
3051 	if (sum != 0) {
3052 #if defined(_SUNOS_VTOC_16)
3053 		if (!ISCD(cl)) {
3054 #elif defined(_SUNOS_VTOC_8)
3055 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3056 #endif
3057 			if (!(flags & CMLB_SILENT))
3058 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3059 				    CE_WARN,
3060 				    "Corrupt label - label checksum failed\n");
3061 		}
3062 		return (CMLB_LABEL_IS_INVALID);
3063 	}
3064 
3065 
3066 	/*
3067 	 * Fill in geometry structure with data from label.
3068 	 */
3069 	bzero(&cl->cl_g, sizeof (struct dk_geom));
3070 	cl->cl_g.dkg_ncyl   = labp->dkl_ncyl;
3071 	cl->cl_g.dkg_acyl   = labp->dkl_acyl;
3072 	cl->cl_g.dkg_bcyl   = 0;
3073 	cl->cl_g.dkg_nhead  = labp->dkl_nhead;
3074 	cl->cl_g.dkg_nsect  = labp->dkl_nsect;
3075 	cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
3076 
3077 #if defined(_SUNOS_VTOC_8)
3078 	cl->cl_g.dkg_gap1   = labp->dkl_gap1;
3079 	cl->cl_g.dkg_gap2   = labp->dkl_gap2;
3080 	cl->cl_g.dkg_bhead  = labp->dkl_bhead;
3081 #endif
3082 #if defined(_SUNOS_VTOC_16)
3083 	cl->cl_dkg_skew = labp->dkl_skew;
3084 #endif
3085 
3086 #if defined(__x86)
3087 	cl->cl_g.dkg_apc = labp->dkl_apc;
3088 #endif
3089 
3090 	/*
3091 	 * Currently we rely on the values in the label being accurate. If
3092 	 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
3093 	 *
3094 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
3095 	 * although this command is optional in SCSI-2.
3096 	 */
3097 	cl->cl_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
3098 	cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
3099 	    (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
3100 
3101 	/*
3102 	 * The Read and Write reinstruct values may not be valid
3103 	 * for older disks.
3104 	 */
3105 	cl->cl_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
3106 	cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
3107 
3108 	/* Fill in partition table. */
3109 #if defined(_SUNOS_VTOC_8)
3110 	for (i = 0; i < NDKMAP; i++) {
3111 		cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
3112 		cl->cl_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
3113 	}
3114 #endif
3115 #if  defined(_SUNOS_VTOC_16)
3116 	vpartp		= labp->dkl_vtoc.v_part;
3117 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
3118 
3119 	/* Prevent divide by zero */
3120 	if (track_capacity == 0) {
3121 		if (!(flags & CMLB_SILENT))
3122 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3123 			    "Corrupt label - zero nhead or nsect value\n");
3124 
3125 		return (CMLB_LABEL_IS_INVALID);
3126 	}
3127 
3128 	for (i = 0; i < NDKMAP; i++, vpartp++) {
3129 		cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
3130 		cl->cl_map[i].dkl_nblk  = vpartp->p_size;
3131 	}
3132 #endif
3133 
3134 	/* Fill in VTOC Structure. */
3135 	bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
3136 #if defined(_SUNOS_VTOC_8)
3137 	/*
3138 	 * The 8-slice vtoc does not include the ascii label; save it into
3139 	 * the device's soft state structure here.
3140 	 */
3141 	bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
3142 #endif
3143 
3144 	/* Now look for a valid capacity. */
3145 	track_capacity	= (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
3146 	label_capacity	= (cl->cl_g.dkg_ncyl  * track_capacity);
3147 
3148 	if (cl->cl_g.dkg_acyl) {
3149 #if defined(__x86)
3150 		/* we may have > 1 alts cylinder */
3151 		label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
3152 #else
3153 		label_capacity += track_capacity;
3154 #endif
3155 	}
3156 
3157 	/*
3158 	 * Force check here to ensure the computed capacity is valid.
3159 	 * If capacity is zero, it indicates an invalid label and
3160 	 * we should abort updating the relevant data then.
3161 	 */
3162 	if (label_capacity == 0) {
3163 		if (!(flags & CMLB_SILENT))
3164 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3165 			    "Corrupt label - no valid capacity could be "
3166 			    "retrieved\n");
3167 
3168 		return (CMLB_LABEL_IS_INVALID);
3169 	}
3170 
3171 	/* Mark the geometry as valid. */
3172 	cl->cl_f_geometry_is_valid = B_TRUE;
3173 
3174 	/*
3175 	 * if we got invalidated when mutex exit and entered again,
3176 	 * if blockcount different than when we came in, need to
3177 	 * retry from beginning of cmlb_validate_geometry.
3178 	 * revisit this on next phase of utilizing this for
3179 	 * sd.
3180 	 */
3181 
3182 	if (label_capacity <= cl->cl_blockcount) {
3183 #if defined(_SUNOS_VTOC_8)
3184 		/*
3185 		 * We can't let this happen on drives that are subdivided
3186 		 * into logical disks (i.e., that have an fdisk table).
3187 		 * The cl_blockcount field should always hold the full media
3188 		 * size in sectors, period.  This code would overwrite
3189 		 * cl_blockcount with the size of the Solaris fdisk partition.
3190 		 */
3191 		cmlb_dbg(CMLB_ERROR,  cl,
3192 		    "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3193 		    label_capacity, cl->cl_blockcount);
3194 		cl->cl_solaris_size = label_capacity;
3195 
3196 #endif	/* defined(_SUNOS_VTOC_8) */
3197 		goto done;
3198 	}
3199 
3200 	if (ISCD(cl)) {
3201 		/* For CDROMs, we trust that the data in the label is OK. */
3202 #if defined(_SUNOS_VTOC_8)
3203 		for (i = 0; i < NDKMAP; i++) {
3204 			part_end = labp->dkl_nhead * labp->dkl_nsect *
3205 			    labp->dkl_map[i].dkl_cylno +
3206 			    labp->dkl_map[i].dkl_nblk  - 1;
3207 
3208 			if ((labp->dkl_map[i].dkl_nblk) &&
3209 			    (part_end > cl->cl_blockcount)) {
3210 				cl->cl_f_geometry_is_valid = B_FALSE;
3211 				break;
3212 			}
3213 		}
3214 #endif
3215 #if defined(_SUNOS_VTOC_16)
3216 		vpartp = &(labp->dkl_vtoc.v_part[0]);
3217 		for (i = 0; i < NDKMAP; i++, vpartp++) {
3218 			part_end = vpartp->p_start + vpartp->p_size;
3219 			if ((vpartp->p_size > 0) &&
3220 			    (part_end > cl->cl_blockcount)) {
3221 				cl->cl_f_geometry_is_valid = B_FALSE;
3222 				break;
3223 			}
3224 		}
3225 #endif
3226 	} else {
3227 		/* label_capacity > cl->cl_blockcount */
3228 		if (!(flags & CMLB_SILENT)) {
3229 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3230 			    "Corrupt label - bad geometry\n");
3231 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
3232 			    "Label says %llu blocks; Drive says %llu blocks\n",
3233 			    label_capacity, cl->cl_blockcount);
3234 		}
3235 		cl->cl_f_geometry_is_valid = B_FALSE;
3236 		label_error = CMLB_LABEL_IS_INVALID;
3237 	}
3238 
3239 done:
3240 
3241 	cmlb_dbg(CMLB_INFO,  cl, "cmlb_uselabel: (label geometry)\n");
3242 	cmlb_dbg(CMLB_INFO,  cl,
3243 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3244 	    cl->cl_g.dkg_ncyl,  cl->cl_g.dkg_acyl,
3245 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3246 
3247 	cmlb_dbg(CMLB_INFO,  cl,
3248 	    "   label_capacity: %d; intrlv: %d; rpm: %d\n",
3249 	    cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
3250 	cmlb_dbg(CMLB_INFO,  cl, "   wrt_reinstr: %d; rd_reinstr: %d\n",
3251 	    cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
3252 
3253 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3254 
3255 	return (label_error);
3256 }
3257 
3258 
3259 /*
3260  *    Function: cmlb_build_default_label
3261  *
3262  * Description: Generate a default label for those devices that do not have
3263  *		one, e.g., new media, removable cartridges, etc..
3264  *
3265  *     Context: Kernel thread only
3266  */
3267 /*ARGSUSED*/
3268 static void
3269 cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
3270 {
3271 #if defined(_SUNOS_VTOC_16)
3272 	uint_t	phys_spc;
3273 	uint_t	disksize;
3274 	struct  dk_geom cl_g;
3275 	diskaddr_t capacity;
3276 #endif
3277 
3278 	ASSERT(cl != NULL);
3279 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3280 
3281 #if defined(_SUNOS_VTOC_8)
3282 	/*
3283 	 * Note: This is a legacy check for non-removable devices on VTOC_8
3284 	 * only. This may be a valid check for VTOC_16 as well.
3285 	 * Once we understand why there is this difference between SPARC and
3286 	 * x86 platform, we could remove this legacy check.
3287 	 */
3288 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3289 		return;
3290 	}
3291 #endif
3292 
3293 	bzero(&cl->cl_g, sizeof (struct dk_geom));
3294 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3295 	bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
3296 
3297 #if defined(_SUNOS_VTOC_8)
3298 
3299 	/*
3300 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
3301 	 * But it is still necessary to set up various geometry information,
3302 	 * and we are doing this here.
3303 	 */
3304 
3305 	/*
3306 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
3307 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
3308 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
3309 	 * equal to C*H*S values.  This will cause some truncation of size due
3310 	 * to round off errors. For CD-ROMs, this truncation can have adverse
3311 	 * side effects, so returning ncyl and nhead as 1. The nsect will
3312 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
3313 	 */
3314 	cl->cl_solaris_size = cl->cl_blockcount;
3315 	if (ISCD(cl)) {
3316 		tg_attribute_t tgattribute;
3317 		int is_writable;
3318 		/*
3319 		 * Preserve the old behavior for non-writable
3320 		 * medias. Since dkg_nsect is a ushort, it
3321 		 * will lose bits as cdroms have more than
3322 		 * 65536 sectors. So if we recalculate
3323 		 * capacity, it will become much shorter.
3324 		 * But the dkg_* information is not
3325 		 * used for CDROMs so it is OK. But for
3326 		 * Writable CDs we need this information
3327 		 * to be valid (for newfs say). So we
3328 		 * make nsect and nhead > 1 that way
3329 		 * nsect can still stay within ushort limit
3330 		 * without losing any bits.
3331 		 */
3332 
3333 		bzero(&tgattribute, sizeof (tg_attribute_t));
3334 
3335 		mutex_exit(CMLB_MUTEX(cl));
3336 		is_writable =
3337 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
3338 		    tgattribute.media_is_writable : 1;
3339 		mutex_enter(CMLB_MUTEX(cl));
3340 
3341 		if (is_writable) {
3342 			cl->cl_g.dkg_nhead = 64;
3343 			cl->cl_g.dkg_nsect = 32;
3344 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3345 			cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
3346 			    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3347 		} else {
3348 			cl->cl_g.dkg_ncyl  = 1;
3349 			cl->cl_g.dkg_nhead = 1;
3350 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
3351 		}
3352 	} else {
3353 		if (cl->cl_blockcount < 160) {
3354 			/* Less than 80K */
3355 			cl->cl_g.dkg_nhead = 1;
3356 			cl->cl_g.dkg_ncyl = cl->cl_blockcount;
3357 			cl->cl_g.dkg_nsect = 1;
3358 		} else if (cl->cl_blockcount <= 0x1000) {
3359 			/* unlabeled SCSI floppy device */
3360 			cl->cl_g.dkg_nhead = 2;
3361 			cl->cl_g.dkg_ncyl = 80;
3362 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
3363 		} else if (cl->cl_blockcount <= 0x200000) {
3364 			cl->cl_g.dkg_nhead = 64;
3365 			cl->cl_g.dkg_nsect = 32;
3366 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (64 * 32);
3367 		} else {
3368 			cl->cl_g.dkg_nhead = 255;
3369 
3370 			cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
3371 			    (UINT16_MAX * 255 * 63) - 1) /
3372 			    (UINT16_MAX * 255 * 63)) * 63;
3373 
3374 			if (cl->cl_g.dkg_nsect == 0)
3375 				cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
3376 
3377 			cl->cl_g.dkg_ncyl = cl->cl_blockcount /
3378 			    (255 * cl->cl_g.dkg_nsect);
3379 		}
3380 
3381 		cl->cl_solaris_size =
3382 		    (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
3383 		    cl->cl_g.dkg_nsect;
3384 
3385 	}
3386 
3387 	cl->cl_g.dkg_acyl	= 0;
3388 	cl->cl_g.dkg_bcyl	= 0;
3389 	cl->cl_g.dkg_rpm	= 200;
3390 	cl->cl_asciilabel[0]	= '\0';
3391 	cl->cl_g.dkg_pcyl	= cl->cl_g.dkg_ncyl;
3392 
3393 	cl->cl_map[0].dkl_cylno = 0;
3394 	cl->cl_map[0].dkl_nblk  = cl->cl_solaris_size;
3395 
3396 	cl->cl_map[2].dkl_cylno = 0;
3397 	cl->cl_map[2].dkl_nblk  = cl->cl_solaris_size;
3398 
3399 #elif defined(_SUNOS_VTOC_16)
3400 
3401 	if (cl->cl_solaris_size == 0) {
3402 		/*
3403 		 * Got fdisk table but no solaris entry therefore
3404 		 * don't create a default label
3405 		 */
3406 		cl->cl_f_geometry_is_valid = B_TRUE;
3407 		return;
3408 	}
3409 
3410 	/*
3411 	 * For CDs we continue to use the physical geometry to calculate
3412 	 * number of cylinders. All other devices must convert the
3413 	 * physical geometry (cmlb_geom) to values that will fit
3414 	 * in a dk_geom structure.
3415 	 */
3416 	if (ISCD(cl)) {
3417 		phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
3418 	} else {
3419 		/* Convert physical geometry to disk geometry */
3420 		bzero(&cl_g, sizeof (struct dk_geom));
3421 
3422 		/*
3423 		 * Refer to comments related to off-by-1 at the
3424 		 * header of this file.
3425 		 * Before calculating geometry, capacity should be
3426 		 * decreased by 1.
3427 		 */
3428 
3429 		if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
3430 			capacity = cl->cl_blockcount - 1;
3431 		else
3432 			capacity = cl->cl_blockcount;
3433 
3434 
3435 		cmlb_convert_geometry(cl, capacity, &cl_g, tg_cookie);
3436 		bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
3437 		phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3438 	}
3439 
3440 	if (phys_spc == 0)
3441 		return;
3442 	cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
3443 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3444 		/* disable devid */
3445 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
3446 		disksize = cl->cl_solaris_size;
3447 	} else {
3448 		cl->cl_g.dkg_acyl = DK_ACYL;
3449 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
3450 		disksize = cl->cl_g.dkg_ncyl * phys_spc;
3451 	}
3452 
3453 	if (ISCD(cl)) {
3454 		/*
3455 		 * CD's don't use the "heads * sectors * cyls"-type of
3456 		 * geometry, but instead use the entire capacity of the media.
3457 		 */
3458 		disksize = cl->cl_solaris_size;
3459 		cl->cl_g.dkg_nhead = 1;
3460 		cl->cl_g.dkg_nsect = 1;
3461 		cl->cl_g.dkg_rpm =
3462 		    (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
3463 
3464 		cl->cl_vtoc.v_part[0].p_start = 0;
3465 		cl->cl_vtoc.v_part[0].p_size  = disksize;
3466 		cl->cl_vtoc.v_part[0].p_tag   = V_BACKUP;
3467 		cl->cl_vtoc.v_part[0].p_flag  = V_UNMNT;
3468 
3469 		cl->cl_map[0].dkl_cylno = 0;
3470 		cl->cl_map[0].dkl_nblk  = disksize;
3471 		cl->cl_offset[0] = 0;
3472 
3473 	} else {
3474 		/*
3475 		 * Hard disks and removable media cartridges
3476 		 */
3477 		cl->cl_g.dkg_rpm =
3478 		    (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
3479 		cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
3480 
3481 		/* Add boot slice */
3482 		cl->cl_vtoc.v_part[8].p_start = 0;
3483 		cl->cl_vtoc.v_part[8].p_size  = phys_spc;
3484 		cl->cl_vtoc.v_part[8].p_tag   = V_BOOT;
3485 		cl->cl_vtoc.v_part[8].p_flag  = V_UNMNT;
3486 
3487 		cl->cl_map[8].dkl_cylno = 0;
3488 		cl->cl_map[8].dkl_nblk  = phys_spc;
3489 		cl->cl_offset[8] = 0;
3490 
3491 		if ((cl->cl_alter_behavior &
3492 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
3493 		    cl->cl_device_type == DTYPE_DIRECT) {
3494 			cl->cl_vtoc.v_part[9].p_start = phys_spc;
3495 			cl->cl_vtoc.v_part[9].p_size  = 2 * phys_spc;
3496 			cl->cl_vtoc.v_part[9].p_tag   = V_ALTSCTR;
3497 			cl->cl_vtoc.v_part[9].p_flag  = 0;
3498 
3499 			cl->cl_map[9].dkl_cylno = 1;
3500 			cl->cl_map[9].dkl_nblk  = 2 * phys_spc;
3501 			cl->cl_offset[9] = phys_spc;
3502 		}
3503 	}
3504 
3505 	cl->cl_g.dkg_apc = 0;
3506 
3507 	/* Add backup slice */
3508 	cl->cl_vtoc.v_part[2].p_start = 0;
3509 	cl->cl_vtoc.v_part[2].p_size  = disksize;
3510 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
3511 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
3512 
3513 	cl->cl_map[2].dkl_cylno = 0;
3514 	cl->cl_map[2].dkl_nblk  = disksize;
3515 	cl->cl_offset[2] = 0;
3516 
3517 	/*
3518 	 * single slice (s0) covering the entire disk
3519 	 */
3520 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3521 		cl->cl_vtoc.v_part[0].p_start = 0;
3522 		cl->cl_vtoc.v_part[0].p_tag   = V_UNASSIGNED;
3523 		cl->cl_vtoc.v_part[0].p_flag  = 0;
3524 		cl->cl_vtoc.v_part[0].p_size  = disksize;
3525 		cl->cl_map[0].dkl_cylno = 0;
3526 		cl->cl_map[0].dkl_nblk  = disksize;
3527 		cl->cl_offset[0] = 0;
3528 	}
3529 
3530 	(void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
3531 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3532 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3533 
3534 #else
3535 #error "No VTOC format defined."
3536 #endif
3537 
3538 	cl->cl_g.dkg_read_reinstruct  = 0;
3539 	cl->cl_g.dkg_write_reinstruct = 0;
3540 
3541 	cl->cl_g.dkg_intrlv = 1;
3542 
3543 	cl->cl_vtoc.v_sanity  = VTOC_SANE;
3544 	cl->cl_vtoc.v_nparts = V_NUMPAR;
3545 	cl->cl_vtoc.v_version = V_VERSION;
3546 
3547 	cl->cl_f_geometry_is_valid = B_TRUE;
3548 	cl->cl_label_from_media = CMLB_LABEL_UNDEF;
3549 
3550 	cmlb_dbg(CMLB_INFO,  cl,
3551 	    "cmlb_build_default_label: Default label created: "
3552 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3553 	    cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
3554 	    cl->cl_g.dkg_nsect, cl->cl_blockcount);
3555 }
3556 
3557 
3558 #if defined(_FIRMWARE_NEEDS_FDISK)
3559 /*
3560  * Max CHS values, as they are encoded into bytes, for 1022/254/63
3561  */
3562 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
3563 #define	LBA_MAX_CYL	(1022 & 0xFF)