xref: /titanic_51/usr/src/uts/common/io/cmlb.c (revision 4e9cfc9a015e8ca7d41f7d018c74dc8a692305b3)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This module provides support for labeling operations for target
31  * drivers.
32  */
33 
34 #include <sys/scsi/scsi.h>
35 #include <sys/sunddi.h>
36 #include <sys/dklabel.h>
37 #include <sys/dkio.h>
38 #include <sys/vtoc.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/vtrace.h>
41 #include <sys/efi_partition.h>
42 #include <sys/cmlb.h>
43 #include <sys/cmlb_impl.h>
44 
45 /*
46  * Driver minor node structure and data table
47  */
48 struct driver_minor_data {
49 	char	*name;
50 	minor_t	minor;
51 	int	type;
52 };
53 
54 static struct driver_minor_data dk_minor_data[] = {
55 	{"a", 0, S_IFBLK},
56 	{"b", 1, S_IFBLK},
57 	{"c", 2, S_IFBLK},
58 	{"d", 3, S_IFBLK},
59 	{"e", 4, S_IFBLK},
60 	{"f", 5, S_IFBLK},
61 	{"g", 6, S_IFBLK},
62 	{"h", 7, S_IFBLK},
63 #if defined(_SUNOS_VTOC_16)
64 	{"i", 8, S_IFBLK},
65 	{"j", 9, S_IFBLK},
66 	{"k", 10, S_IFBLK},
67 	{"l", 11, S_IFBLK},
68 	{"m", 12, S_IFBLK},
69 	{"n", 13, S_IFBLK},
70 	{"o", 14, S_IFBLK},
71 	{"p", 15, S_IFBLK},
72 #endif			/* defined(_SUNOS_VTOC_16) */
73 #if defined(_FIRMWARE_NEEDS_FDISK)
74 	{"q", 16, S_IFBLK},
75 	{"r", 17, S_IFBLK},
76 	{"s", 18, S_IFBLK},
77 	{"t", 19, S_IFBLK},
78 	{"u", 20, S_IFBLK},
79 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
80 	{"a,raw", 0, S_IFCHR},
81 	{"b,raw", 1, S_IFCHR},
82 	{"c,raw", 2, S_IFCHR},
83 	{"d,raw", 3, S_IFCHR},
84 	{"e,raw", 4, S_IFCHR},
85 	{"f,raw", 5, S_IFCHR},
86 	{"g,raw", 6, S_IFCHR},
87 	{"h,raw", 7, S_IFCHR},
88 #if defined(_SUNOS_VTOC_16)
89 	{"i,raw", 8, S_IFCHR},
90 	{"j,raw", 9, S_IFCHR},
91 	{"k,raw", 10, S_IFCHR},
92 	{"l,raw", 11, S_IFCHR},
93 	{"m,raw", 12, S_IFCHR},
94 	{"n,raw", 13, S_IFCHR},
95 	{"o,raw", 14, S_IFCHR},
96 	{"p,raw", 15, S_IFCHR},
97 #endif			/* defined(_SUNOS_VTOC_16) */
98 #if defined(_FIRMWARE_NEEDS_FDISK)
99 	{"q,raw", 16, S_IFCHR},
100 	{"r,raw", 17, S_IFCHR},
101 	{"s,raw", 18, S_IFCHR},
102 	{"t,raw", 19, S_IFCHR},
103 	{"u,raw", 20, S_IFCHR},
104 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
105 	{0}
106 };
107 
108 static struct driver_minor_data dk_minor_data_efi[] = {
109 	{"a", 0, S_IFBLK},
110 	{"b", 1, S_IFBLK},
111 	{"c", 2, S_IFBLK},
112 	{"d", 3, S_IFBLK},
113 	{"e", 4, S_IFBLK},
114 	{"f", 5, S_IFBLK},
115 	{"g", 6, S_IFBLK},
116 	{"wd", 7, S_IFBLK},
117 #if defined(_FIRMWARE_NEEDS_FDISK)
118 	{"q", 16, S_IFBLK},
119 	{"r", 17, S_IFBLK},
120 	{"s", 18, S_IFBLK},
121 	{"t", 19, S_IFBLK},
122 	{"u", 20, S_IFBLK},
123 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
124 	{"a,raw", 0, S_IFCHR},
125 	{"b,raw", 1, S_IFCHR},
126 	{"c,raw", 2, S_IFCHR},
127 	{"d,raw", 3, S_IFCHR},
128 	{"e,raw", 4, S_IFCHR},
129 	{"f,raw", 5, S_IFCHR},
130 	{"g,raw", 6, S_IFCHR},
131 	{"wd,raw", 7, S_IFCHR},
132 #if defined(_FIRMWARE_NEEDS_FDISK)
133 	{"q,raw", 16, S_IFCHR},
134 	{"r,raw", 17, S_IFCHR},
135 	{"s,raw", 18, S_IFCHR},
136 	{"t,raw", 19, S_IFCHR},
137 	{"u,raw", 20, S_IFCHR},
138 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
139 	{0}
140 };
141 
142 
143 
144 extern struct mod_ops mod_miscops;
145 
146 /*
147  * Global buffer and mutex for debug logging
148  */
149 static char	cmlb_log_buffer[1024];
150 static kmutex_t	cmlb_log_mutex;
151 
152 
153 struct cmlb_lun *cmlb_debug_cl = NULL;
154 uint_t cmlb_level_mask = 0x0;
155 
156 int cmlb_rot_delay = 4;	/* default rotational delay */
157 
158 static struct modlmisc modlmisc = {
159 	&mod_miscops,   /* Type of module */
160 	"Common Labeling module %I%"
161 };
162 
163 static struct modlinkage modlinkage = {
164 	MODREV_1, (void *)&modlmisc, NULL
165 };
166 
167 /* Local function prototypes */
168 static dev_t cmlb_make_device(struct cmlb_lun *cl);
169 static int cmlb_validate_geometry(struct cmlb_lun *cl, int forcerevalid,
170     int flags, void *tg_cookie);
171 static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
172     void *tg_cookie);
173 static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
174     void *tg_cookie);
175 static void cmlb_swap_efi_gpt(efi_gpt_t *e);
176 static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
177 static int cmlb_validate_efi(efi_gpt_t *labp);
178 static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
179     void *tg_cookie);
180 static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
181 static int  cmlb_uselabel(struct cmlb_lun *cl,  struct dk_label *l, int flags);
182 #if defined(_SUNOS_VTOC_8)
183 static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
184 #endif
185 static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
186 static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
187 static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
188     void *tg_cookie);
189 static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
190 static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
191 static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
192 static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
193 static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
194 
195 #if defined(__i386) || defined(__amd64)
196 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
197 #endif
198 
199 #if defined(_FIRMWARE_NEEDS_FDISK)
200 static int  cmlb_has_max_chs_vals(struct ipart *fdp);
201 #endif
202 
203 #if defined(_SUNOS_VTOC_16)
204 static void cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g);
205 #endif
206 
207 static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
208     void *tg_cookie);
209 static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
210 static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
211     void *tg_cookie);
212 static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
213 static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
214     void *tg_cookie);
215 static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
216     int flag, void *tg_cookie);
217 static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
218     void *tg_cookie);
219 static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
220     int flag, void *tg_cookie);
221 static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
222     void *tg_cookie);
223 static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
224     void *tg_cookie);
225 static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
226     void *tg_cookie);
227 
228 #if defined(__i386) || defined(__amd64)
229 static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
230 static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag);
231 static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
232     int flag);
233 #endif
234 
235 static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
236 static void cmlb_v_log(dev_info_t *dev, char *label, uint_t level,
237     const char *fmt, va_list ap);
238 static void cmlb_log(dev_info_t *dev, char *label, uint_t level,
239     const char *fmt, ...);
240 
241 int
242 _init(void)
243 {
244 	mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
245 	return (mod_install(&modlinkage));
246 }
247 
248 int
249 _info(struct modinfo *modinfop)
250 {
251 	return (mod_info(&modlinkage, modinfop));
252 }
253 
254 int
255 _fini(void)
256 {
257 	int err;
258 
259 	if ((err = mod_remove(&modlinkage)) != 0) {
260 		return (err);
261 	}
262 
263 	mutex_destroy(&cmlb_log_mutex);
264 	return (err);
265 }
266 
267 /*
268  * cmlb_dbg is used for debugging to log additional info
269  * Level of output is controlled via cmlb_level_mask setting.
270  */
271 static void
272 cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
273 {
274 	va_list		ap;
275 	dev_info_t	*dev;
276 	uint_t		level_mask = 0;
277 
278 	ASSERT(cl != NULL);
279 	dev = CMLB_DEVINFO(cl);
280 	ASSERT(dev != NULL);
281 	/*
282 	 * Filter messages based on the global component and level masks,
283 	 * also print if cl matches the value of cmlb_debug_cl, or if
284 	 * cmlb_debug_cl is set to NULL.
285 	 */
286 	if (comp & CMLB_TRACE)
287 		level_mask |= CMLB_LOGMASK_TRACE;
288 
289 	if (comp & CMLB_INFO)
290 		level_mask |= CMLB_LOGMASK_INFO;
291 
292 	if (comp & CMLB_ERROR)
293 		level_mask |= CMLB_LOGMASK_ERROR;
294 
295 	if ((cmlb_level_mask & level_mask) &&
296 	    ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
297 		va_start(ap, fmt);
298 		cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
299 		va_end(ap);
300 	}
301 }
302 
303 /*
304  * cmlb_log is basically a duplicate of scsi_log. It is redefined here
305  * so that this module does not depend on scsi module.
306  */
307 static void
308 cmlb_log(dev_info_t *dev, char *label, uint_t level, const char *fmt, ...)
309 {
310 	va_list		ap;
311 
312 	va_start(ap, fmt);
313 	cmlb_v_log(dev, label, level, fmt, ap);
314 	va_end(ap);
315 }
316 
317 static void
318 cmlb_v_log(dev_info_t *dev, char *label, uint_t level, const char *fmt,
319     va_list ap)
320 {
321 	static char 	name[256];
322 	int 		log_only = 0;
323 	int 		boot_only = 0;
324 	int 		console_only = 0;
325 
326 	mutex_enter(&cmlb_log_mutex);
327 
328 	if (dev) {
329 		if (level == CE_PANIC || level == CE_WARN ||
330 		    level == CE_NOTE) {
331 			(void) sprintf(name, "%s (%s%d):\n",
332 			    ddi_pathname(dev, cmlb_log_buffer),
333 			    label, ddi_get_instance(dev));
334 		} else {
335 			name[0] = '\0';
336 		}
337 	} else {
338 		(void) sprintf(name, "%s:", label);
339 	}
340 
341 	(void) vsprintf(cmlb_log_buffer, fmt, ap);
342 
343 	switch (cmlb_log_buffer[0]) {
344 	case '!':
345 		log_only = 1;
346 		break;
347 	case '?':
348 		boot_only = 1;
349 		break;
350 	case '^':
351 		console_only = 1;
352 		break;
353 	}
354 
355 	switch (level) {
356 	case CE_NOTE:
357 		level = CE_CONT;
358 		/* FALLTHROUGH */
359 	case CE_CONT:
360 	case CE_WARN:
361 	case CE_PANIC:
362 		if (boot_only) {
363 			cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
364 		} else if (console_only) {
365 			cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
366 		} else if (log_only) {
367 			cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
368 		} else {
369 			cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
370 		}
371 		break;
372 	case CE_IGNORE:
373 		break;
374 	default:
375 		cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
376 		break;
377 	}
378 	mutex_exit(&cmlb_log_mutex);
379 }
380 
381 
382 /*
383  * cmlb_alloc_handle:
384  *
385  *	Allocates a handle.
386  *
387  * Arguments:
388  *	cmlbhandlep	pointer to handle
389  *
390  * Notes:
391  *	Allocates a handle and stores the allocated handle in the area
392  *	pointed to by cmlbhandlep
393  *
394  * Context:
395  *	Kernel thread only (can sleep).
396  */
397 void
398 cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
399 {
400 	struct cmlb_lun 	*cl;
401 
402 	cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
403 	ASSERT(cmlbhandlep != NULL);
404 
405 	cl->cl_state = CMLB_INITED;
406 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
407 	mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
408 
409 	*cmlbhandlep = (cmlb_handle_t)(cl);
410 }
411 
412 /*
413  * cmlb_free_handle
414  *
415  *	Frees handle.
416  *
417  * Arguments:
418  *	cmlbhandlep	pointer to handle
419  */
420 void
421 cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
422 {
423 	struct cmlb_lun 	*cl;
424 
425 	cl = (struct cmlb_lun *)*cmlbhandlep;
426 	if (cl != NULL) {
427 		mutex_destroy(CMLB_MUTEX(cl));
428 		kmem_free(cl, sizeof (struct cmlb_lun));
429 	}
430 
431 }
432 
433 /*
434  * cmlb_attach:
435  *
436  *	Attach handle to device, create minor nodes for device.
437  *
438  * Arguments:
439  * 	devi		pointer to device's dev_info structure.
440  * 	tgopsp		pointer to array of functions cmlb can use to callback
441  *			to target driver.
442  *
443  *	device_type	Peripheral device type as defined in
444  *			scsi/generic/inquiry.h
445  *
446  *	is_removable	whether or not device is removable.
447  *			0 non-removable, 1 removable.
448  *
449  *	is_hotpluggable	whether or not device is hotpluggable.
450  *			0 non-hotpluggable, 1 hotpluggable.
451  *
452  *	node_type	minor node type (as used by ddi_create_minor_node)
453  *
454  *	alter_behavior
455  *			bit flags:
456  *
457  *			CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
458  *			an alternate slice for the default label, if
459  *			device type is DTYPE_DIRECT an architectures default
460  *			label type is VTOC16.
461  *			Otherwise alternate slice will no be created.
462  *
463  *
464  *			CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
465  *			geometry and label for DKIOCGGEOM and DKIOCGVTOC
466  *			on architecture with VTOC8 label types.
467  *
468  * 			CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
469  *                      one bug in obtaining capacity (in sd):
470  *			SCSI READ_CAPACITY command returns the LBA number of the
471  *			last logical block, but sd once treated this number as
472  *			disks' capacity on x86 platform. And LBAs are addressed
473  *			based 0. So the last block was lost on x86 platform.
474  *
475  *			Now, we remove this workaround. In order for present sd
476  *			driver to work with disks which are labeled/partitioned
477  *			via previous sd, we add workaround as follows:
478  *
479  *			1) Locate backup EFI label: cmlb searches the next to
480  *			   last
481  *			   block for backup EFI label. If fails, it will
482  *			   turn to the last block for backup EFI label;
483  *
484  *			2) Clear backup EFI label: cmlb first search the last
485  *			   block for backup EFI label, and will search the
486  *			   next to last block only if failed for the last
487  *			   block.
488  *
489  *			3) Calculate geometry:refer to cmlb_convert_geometry()
490  *			   If capacity increasing by 1 causes disks' capacity
491  *			   to cross over the limits in table CHS_values,
492  *			   geometry info will change. This will raise an issue:
493  *			   In case that primary VTOC label is destroyed, format
494  *			   commandline can restore it via backup VTOC labels.
495  *			   And format locates backup VTOC labels by use of
496  *			   geometry. So changing geometry will
497  *			   prevent format from finding backup VTOC labels. To
498  *			   eliminate this side effect for compatibility,
499  *			   sd uses (capacity -1) to calculate geometry;
500  *
501  *			4) 1TB disks: some important data structures use
502  *			   32-bit signed long/int (for example, daddr_t),
503  *			   so that sd doesn't support a disk with capacity
504  *			   larger than 1TB on 32-bit platform. However,
505  *			   for exactly 1TB disk, it was treated as (1T - 512)B
506  *			   in the past, and could have valid Solaris
507  *			   partitions. To workaround this, if an exactly 1TB
508  *			   disk has Solaris fdisk partition, it will be allowed
509  *			   to work with sd.
510  *
511  *
512  *
513  *			CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
514  *			the entire disk, if there is no valid partition info.
515  *			If there is a valid Solaris partition, s0 and s2 will
516  *			only cover the entire Solaris partition.
517  *
518  *
519  *	cmlbhandle	cmlb handle associated with device
520  *
521  *	tg_cookie	cookie from target driver to be passed back to target
522  *			driver when we call back to it through tg_ops.
523  *
524  * Notes:
525  *	Assumes a default label based on capacity for non-removable devices.
526  *	If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
527  *	for the architecture).
528  *
529  *	For removable devices, default label type is assumed to be VTOC
530  *	type. Create minor nodes based on a default label type.
531  *	Label on the media is not validated.
532  *	minor number consists of:
533  *		if _SUNOS_VTOC_8 is defined
534  *			lowest 3 bits is taken as partition number
535  *			the rest is instance number
536  *		if _SUNOS_VTOC_16 is defined
537  *			lowest 6 bits is taken as partition number
538  *			the rest is instance number
539  *
540  *
541  * Return values:
542  *	0 	Success
543  * 	ENXIO 	creating minor nodes failed.
544  *	EINVAL  invalid arg, unsupported tg_ops version
545  */
546 int
547 cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
548     int is_removable, int is_hotpluggable, char *node_type,
549     int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
550 {
551 
552 	struct cmlb_lun	*cl = (struct cmlb_lun *)cmlbhandle;
553 	diskaddr_t	cap;
554 	int		status;
555 
556 	if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
557 		return (EINVAL);
558 
559 	mutex_enter(CMLB_MUTEX(cl));
560 
561 	CMLB_DEVINFO(cl) = devi;
562 	cl->cmlb_tg_ops = tgopsp;
563 	cl->cl_device_type = device_type;
564 	cl->cl_is_removable = is_removable;
565 	cl->cl_is_hotpluggable = is_hotpluggable;
566 	cl->cl_node_type = node_type;
567 	cl->cl_sys_blocksize = DEV_BSIZE;
568 	cl->cl_f_geometry_is_valid = FALSE;
569 	cl->cl_def_labeltype = CMLB_LABEL_VTOC;
570 	cl->cl_alter_behavior = alter_behavior;
571 	cl->cl_reserved = -1;
572 
573 	if (is_removable != 0) {
574 		mutex_exit(CMLB_MUTEX(cl));
575 		status = DK_TG_GETCAP(cl, &cap, tg_cookie);
576 		mutex_enter(CMLB_MUTEX(cl));
577 		if (status == 0 && cap > DK_MAX_BLOCKS) {
578 			/* set default EFI if > 1TB */
579 			cl->cl_def_labeltype = CMLB_LABEL_EFI;
580 		}
581 	}
582 
583 	/* create minor nodes based on default label type */
584 	cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
585 	cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
586 
587 	if (cmlb_create_minor_nodes(cl) != 0) {
588 		mutex_exit(CMLB_MUTEX(cl));
589 		return (ENXIO);
590 	}
591 
592 	cl->cl_state = CMLB_ATTACHED;
593 
594 	mutex_exit(CMLB_MUTEX(cl));
595 	return (0);
596 }
597 
598 /*
599  * cmlb_detach:
600  *
601  * Invalidate in-core labeling data and remove all minor nodes for
602  * the device associate with handle.
603  *
604  * Arguments:
605  *	cmlbhandle	cmlb handle associated with device.
606  *
607  *	tg_cookie	cookie from target driver to be passed back to target
608  *			driver when we call back to it through tg_ops.
609  *
610  */
611 /*ARGSUSED1*/
612 void
613 cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
614 {
615 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
616 
617 	mutex_enter(CMLB_MUTEX(cl));
618 	cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
619 	cl->cl_f_geometry_is_valid = FALSE;
620 	ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
621 	cl->cl_state = CMLB_INITED;
622 	mutex_exit(CMLB_MUTEX(cl));
623 }
624 
625 /*
626  * cmlb_validate:
627  *
628  *	Validates label.
629  *
630  * Arguments
631  *	cmlbhandle	cmlb handle associated with device.
632  *
633  *	flags		operation flags. used for verbosity control
634  *
635  *	tg_cookie	cookie from target driver to be passed back to target
636  *			driver when we call back to it through tg_ops.
637  *
638  *
639  * Notes:
640  *	If new label type is different from the current, adjust minor nodes
641  *	accordingly.
642  *
643  * Return values:
644  *	0		success
645  *			Note: having fdisk but no solaris partition is assumed
646  *			success.
647  *
648  *	ENOMEM		memory allocation failed
649  *	EIO		i/o errors during read or get capacity
650  * 	EACCESS		reservation conflicts
651  * 	EINVAL		label was corrupt, or no default label was assumed
652  *	ENXIO		invalid handle
653  */
654 int
655 cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
656 {
657 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
658 	int 		rval;
659 	int  		ret = 0;
660 
661 	/*
662 	 * Temp work-around checking cl for NULL since there is a bug
663 	 * in sd_detach calling this routine from taskq_dispatch
664 	 * inited function.
665 	 */
666 	if (cl == NULL)
667 		return (ENXIO);
668 
669 	mutex_enter(CMLB_MUTEX(cl));
670 	if (cl->cl_state < CMLB_ATTACHED) {
671 		mutex_exit(CMLB_MUTEX(cl));
672 		return (ENXIO);
673 	}
674 
675 	rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, 1,
676 	    flags, tg_cookie);
677 
678 	if (rval == ENOTSUP) {
679 		if (cl->cl_f_geometry_is_valid == TRUE) {
680 			cl->cl_cur_labeltype = CMLB_LABEL_EFI;
681 			ret = 0;
682 		} else {
683 			ret = EINVAL;
684 		}
685 	} else {
686 		ret = rval;
687 		if (ret == 0)
688 			cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
689 	}
690 
691 	if (ret == 0)
692 		(void) cmlb_create_minor_nodes(cl);
693 
694 	mutex_exit(CMLB_MUTEX(cl));
695 	return (ret);
696 }
697 
698 /*
699  * cmlb_invalidate:
700  *	Invalidate in core label data
701  *
702  * Arguments:
703  *	cmlbhandle	cmlb handle associated with device.
704  *	tg_cookie	cookie from target driver to be passed back to target
705  *			driver when we call back to it through tg_ops.
706  */
707 /*ARGSUSED1*/
708 void
709 cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
710 {
711 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
712 
713 	if (cl == NULL)
714 		return;
715 
716 	mutex_enter(CMLB_MUTEX(cl));
717 	cl->cl_f_geometry_is_valid = FALSE;
718 	mutex_exit(CMLB_MUTEX(cl));
719 }
720 
721 /*
722  * cmlb_is_valid
723  * 	Get status on whether the incore label/geom data is valid
724  *
725  * Arguments:
726  *	cmlbhandle      cmlb handle associated with device.
727  *
728  * Return values:
729  *	TRUE if incore label/geom data is valid.
730  *	FALSE otherwise.
731  *
732  */
733 
734 
735 int
736 cmlb_is_valid(cmlb_handle_t cmlbhandle)
737 {
738 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
739 
740 	if (cmlbhandle == NULL)
741 		return (FALSE);
742 
743 	return (cl->cl_f_geometry_is_valid);
744 
745 }
746 
747 
748 
749 /*
750  * cmlb_close:
751  *
752  * Close the device, revert to a default label minor node for the device,
753  * if it is removable.
754  *
755  * Arguments:
756  *	cmlbhandle	cmlb handle associated with device.
757  *
758  *	tg_cookie	cookie from target driver to be passed back to target
759  *			driver when we call back to it through tg_ops.
760  * Return values:
761  *	0	Success
762  * 	ENXIO	Re-creating minor node failed.
763  */
764 /*ARGSUSED1*/
765 int
766 cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
767 {
768 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
769 
770 	mutex_enter(CMLB_MUTEX(cl));
771 	cl->cl_f_geometry_is_valid = FALSE;
772 
773 	/* revert to default minor node for this device */
774 	if (ISREMOVABLE(cl)) {
775 		cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
776 		(void) cmlb_create_minor_nodes(cl);
777 	}
778 
779 	mutex_exit(CMLB_MUTEX(cl));
780 	return (0);
781 }
782 
783 /*
784  * cmlb_get_devid_block:
785  *	 get the block number where device id is stored.
786  *
787  * Arguments:
788  *	cmlbhandle	cmlb handle associated with device.
789  *	devidblockp	pointer to block number.
790  *	tg_cookie	cookie from target driver to be passed back to target
791  *			driver when we call back to it through tg_ops.
792  *
793  * Notes:
794  *	It stores the block number of device id in the area pointed to
795  *	by devidblockp.
796  * 	with the block number of device id.
797  *
798  * Return values:
799  *	0	success
800  *	EINVAL 	device id does not apply to current label type.
801  */
802 /*ARGSUSED2*/
803 int
804 cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
805     void *tg_cookie)
806 {
807 	daddr_t			spc, blk, head, cyl;
808 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
809 
810 	mutex_enter(CMLB_MUTEX(cl));
811 	if (cl->cl_state < CMLB_ATTACHED) {
812 		mutex_exit(CMLB_MUTEX(cl));
813 		return (EINVAL);
814 	}
815 
816 	if ((cl->cl_f_geometry_is_valid == FALSE) ||
817 	    (cl->cl_solaris_size < DK_LABEL_LOC)) {
818 		mutex_exit(CMLB_MUTEX(cl));
819 		return (EINVAL);
820 	}
821 
822 
823 	if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
824 		if (cl->cl_reserved != -1) {
825 			blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
826 		} else {
827 			mutex_exit(CMLB_MUTEX(cl));
828 			return (EINVAL);
829 		}
830 	} else {
831 		/* this geometry doesn't allow us to write a devid */
832 		if (cl->cl_g.dkg_acyl < 2) {
833 			mutex_exit(CMLB_MUTEX(cl));
834 			return (EINVAL);
835 		}
836 
837 		/*
838 		 * Subtract 2 guarantees that the next to last cylinder
839 		 * is used
840 		 */
841 		cyl  = cl->cl_g.dkg_ncyl  + cl->cl_g.dkg_acyl - 2;
842 		spc  = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
843 		head = cl->cl_g.dkg_nhead - 1;
844 		blk  = (cyl * (spc - cl->cl_g.dkg_apc)) +
845 		    (head * cl->cl_g.dkg_nsect) + 1;
846 	}
847 
848 	*devidblockp = blk;
849 	mutex_exit(CMLB_MUTEX(cl));
850 	return (0);
851 }
852 
853 /*
854  * cmlb_partinfo:
855  *	Get partition info for specified partition number.
856  *
857  * Arguments:
858  *	cmlbhandle	cmlb handle associated with device.
859  *	part		partition number
860  *	nblocksp	pointer to number of blocks
861  *	startblockp	pointer to starting block
862  *	partnamep	pointer to name of partition
863  *	tagp		pointer to tag info
864  *	tg_cookie	cookie from target driver to be passed back to target
865  *			driver when we call back to it through tg_ops.
866  *
867  *
868  * Notes:
869  *	If in-core label is not valid, this functions tries to revalidate
870  *	the label. If label is valid, it stores the total number of blocks
871  *	in this partition in the area pointed to by nblocksp, starting
872  *	block number in area pointed to by startblockp,  pointer to partition
873  *	name in area pointed to by partnamep, and tag value in area
874  *	pointed by tagp.
875  *	For EFI labels, tag value will be set to 0.
876  *
877  *	For all nblocksp, startblockp and partnamep, tagp, a value of NULL
878  *	indicates the corresponding info is not requested.
879  *
880  *
881  * Return values:
882  *	0	success
883  *	EINVAL  no valid label or requested partition number is invalid.
884  *
885  */
886 int
887 cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
888     diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
889 {
890 
891 	struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
892 	int rval;
893 
894 	ASSERT(cl != NULL);
895 	mutex_enter(CMLB_MUTEX(cl));
896 	if (cl->cl_state < CMLB_ATTACHED) {
897 		mutex_exit(CMLB_MUTEX(cl));
898 		return (EINVAL);
899 	}
900 
901 	if (part  < 0 || part >= MAXPART) {
902 		rval = EINVAL;
903 	} else {
904 		if (cl->cl_f_geometry_is_valid == FALSE)
905 			(void) cmlb_validate_geometry((struct cmlb_lun *)cl, 0,
906 			    0, tg_cookie);
907 
908 		if ((cl->cl_f_geometry_is_valid == FALSE) ||
909 		    (part < NDKMAP && cl->cl_solaris_size == 0)) {
910 			rval = EINVAL;
911 		} else {
912 			if (startblockp != NULL)
913 				*startblockp = (diskaddr_t)cl->cl_offset[part];
914 
915 			if (nblocksp != NULL)
916 				*nblocksp = (diskaddr_t)
917 				    cl->cl_map[part].dkl_nblk;
918 
919 			if (tagp != NULL)
920 				if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
921 					*tagp = V_UNASSIGNED;
922 				else
923 					*tagp = cl->cl_vtoc.v_part[part].p_tag;
924 			rval = 0;
925 		}
926 
927 		/* consistent with behavior of sd for getting minor name */
928 		if (partnamep != NULL)
929 			*partnamep = dk_minor_data[part].name;
930 
931 	}
932 
933 	mutex_exit(CMLB_MUTEX(cl));
934 	return (rval);
935 }
936 
937 /* Caller should make sure Test Unit Ready succeeds before calling this. */
938 /*ARGSUSED*/
939 int
940 cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
941     int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
942 {
943 
944 	int err;
945 	struct cmlb_lun *cl;
946 	int status;
947 
948 	cl = (struct cmlb_lun *)cmlbhandle;
949 
950 	ASSERT(cl != NULL);
951 
952 	mutex_enter(CMLB_MUTEX(cl));
953 	if (cl->cl_state < CMLB_ATTACHED) {
954 		mutex_exit(CMLB_MUTEX(cl));
955 		return (EIO);
956 	}
957 
958 	switch (cmd) {
959 		case DKIOCSVTOC:
960 		case DKIOCSGEOM:
961 		case DKIOCSETEFI:
962 		case DKIOCSMBOOT:
963 			break;
964 		default:
965 			status = cmlb_validate_geometry(cl, 1, CMLB_SILENT,
966 			    tg_cookie);
967 
968 			/*
969 			 * VTOC related ioctls except SVTOC/SGEOM should
970 			 * fail if > 1TB disk and there is not already a VTOC
971 			 * on the disk.i.e either EFI or blank
972 			 *
973 			 * PHYGEOM AND VIRTGEOM succeeds when disk is
974 			 * EFI labeled but <1TB
975 			 */
976 
977 			if (status == ENOTSUP &&
978 			    cl->cl_f_geometry_is_valid == FALSE) {
979 				switch (cmd) {
980 				case DKIOCGAPART:
981 				case DKIOCGGEOM:
982 				case DKIOCGVTOC:
983 				case DKIOCSAPART:
984 				case DKIOCG_PHYGEOM:
985 				case DKIOCG_VIRTGEOM:
986 
987 					mutex_exit(CMLB_MUTEX(cl));
988 					return (ENOTSUP);
989 				}
990 			} else {
991 				if ((cl->cl_f_geometry_is_valid == TRUE) &&
992 				    (cl->cl_solaris_size > 0)) {
993 					if (cl->cl_vtoc.v_sanity != VTOC_SANE) {
994 					/*
995 					 * it is EFI, so return ENOTSUP for
996 					 * these
997 					 */
998 					switch (cmd) {
999 					case DKIOCGAPART:
1000 					case DKIOCGGEOM:
1001 					case DKIOCGVTOC:
1002 					case DKIOCSVTOC:
1003 					case DKIOCSAPART:
1004 
1005 						mutex_exit(CMLB_MUTEX(cl));
1006 						return (ENOTSUP);
1007 					}
1008 				}
1009 			}
1010 		}
1011 	}
1012 
1013 	mutex_exit(CMLB_MUTEX(cl));
1014 
1015 	switch (cmd) {
1016 	case DKIOCGGEOM:
1017 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1018 		err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
1019 		break;
1020 
1021 	case DKIOCSGEOM:
1022 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1023 		err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
1024 		break;
1025 
1026 	case DKIOCGAPART:
1027 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1028 		err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1029 		    flag, tg_cookie);
1030 		break;
1031 
1032 	case DKIOCSAPART:
1033 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1034 		err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
1035 		break;
1036 
1037 	case DKIOCGVTOC:
1038 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1039 		err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
1040 		break;
1041 
1042 	case DKIOCGETEFI:
1043 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1044 		err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
1045 		break;
1046 
1047 	case DKIOCPARTITION:
1048 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1049 		err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
1050 		break;
1051 
1052 	case DKIOCSVTOC:
1053 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1054 		err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1055 		    tg_cookie);
1056 		break;
1057 
1058 	case DKIOCSETEFI:
1059 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1060 		err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
1061 		break;
1062 
1063 	case DKIOCGMBOOT:
1064 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1065 		err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1066 		break;
1067 
1068 	case DKIOCSMBOOT:
1069 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1070 		err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1071 		break;
1072 	case DKIOCG_PHYGEOM:
1073 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
1074 #if defined(__i386) || defined(__amd64)
1075 		err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag);
1076 #else
1077 		err = ENOTTY;
1078 #endif
1079 		break;
1080 	case DKIOCG_VIRTGEOM:
1081 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
1082 #if defined(__i386) || defined(__amd64)
1083 		err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
1084 #else
1085 		err = ENOTTY;
1086 #endif
1087 		break;
1088 	case DKIOCPARTINFO:
1089 		cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1090 #if defined(__i386) || defined(__amd64)
1091 		err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
1092 #else
1093 		err = ENOTTY;
1094 #endif
1095 		break;
1096 
1097 	default:
1098 		err = ENOTTY;
1099 
1100 	}
1101 	return (err);
1102 }
1103 
1104 dev_t
1105 cmlb_make_device(struct cmlb_lun *cl)
1106 {
1107 	return (makedevice(ddi_name_to_major(ddi_get_name(CMLB_DEVINFO(cl))),
1108 	    ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
1109 }
1110 
1111 /*
1112  * Function: cmlb_check_update_blockcount
1113  *
1114  * Description: If current capacity value is invalid, obtains the
1115  *		current capacity from target driver.
1116  *
1117  * Return Code: 0	success
1118  *		EIO	failure
1119  */
1120 static int
1121 cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
1122 {
1123 	int status;
1124 	diskaddr_t capacity;
1125 	uint32_t lbasize;
1126 
1127 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1128 
1129 	if (cl->cl_f_geometry_is_valid == FALSE)  {
1130 		mutex_exit(CMLB_MUTEX(cl));
1131 		status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1132 		if (status != 0) {
1133 			mutex_enter(CMLB_MUTEX(cl));
1134 			return (EIO);
1135 		}
1136 
1137 		status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1138 		mutex_enter(CMLB_MUTEX(cl));
1139 		if (status != 0)
1140 			return (EIO);
1141 
1142 		if ((capacity != 0) && (lbasize != 0)) {
1143 			cl->cl_blockcount = capacity;
1144 			cl->cl_tgt_blocksize = lbasize;
1145 			return (0);
1146 		} else
1147 			return (EIO);
1148 	} else
1149 		return (0);
1150 }
1151 
1152 /*
1153  *    Function: cmlb_create_minor_nodes
1154  *
1155  * Description: Create or adjust the minor device nodes for the instance.
1156  * 		Minor nodes are created based on default label type,
1157  *		current label type and last label type we created
1158  *		minor nodes based on.
1159  *
1160  *
1161  *   Arguments: cl - driver soft state (unit) structure
1162  *
1163  * Return Code: 0 success
1164  *		ENXIO	failure.
1165  *
1166  *     Context: Kernel thread context
1167  */
1168 static int
1169 cmlb_create_minor_nodes(struct cmlb_lun *cl)
1170 {
1171 	struct driver_minor_data	*dmdp;
1172 	int				instance;
1173 	char				name[48];
1174 	cmlb_label_t			newlabeltype;
1175 
1176 	ASSERT(cl != NULL);
1177 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1178 
1179 
1180 	/* check the most common case */
1181 	if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1182 	    cl->cl_last_labeltype == cl->cl_cur_labeltype) {
1183 		/* do nothing */
1184 		return (0);
1185 	}
1186 
1187 	if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
1188 		/* we should never get here */
1189 		return (ENXIO);
1190 	}
1191 
1192 	if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
1193 		/* first time during attach */
1194 		newlabeltype = cl->cl_def_labeltype;
1195 
1196 		instance = ddi_get_instance(CMLB_DEVINFO(cl));
1197 
1198 		/* Create all the minor nodes for this target. */
1199 		dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
1200 		    dk_minor_data;
1201 		while (dmdp->name != NULL) {
1202 
1203 			(void) sprintf(name, "%s", dmdp->name);
1204 
1205 			if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
1206 			    dmdp->type,
1207 			    (instance << CMLBUNIT_SHIFT) | dmdp->minor,
1208 			    cl->cl_node_type, NULL) == DDI_FAILURE) {
1209 				/*
1210 				 * Clean up any nodes that may have been
1211 				 * created, in case this fails in the middle
1212 				 * of the loop.
1213 				 */
1214 				ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1215 				return (ENXIO);
1216 			}
1217 			dmdp++;
1218 		}
1219 		cl->cl_last_labeltype = newlabeltype;
1220 		return (0);
1221 	}
1222 
1223 	/* Not first time  */
1224 	if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1225 		if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
1226 			/* close time, revert to default. */
1227 			newlabeltype = cl->cl_def_labeltype;
1228 		} else {
1229 			/*
1230 			 * do nothing since the type for which we last created
1231 			 * nodes matches the default
1232 			 */
1233 			return (0);
1234 		}
1235 	} else {
1236 		if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
1237 			/* We are not closing, use current label type */
1238 			newlabeltype = cl->cl_cur_labeltype;
1239 		} else {
1240 			/*
1241 			 * do nothing since the type for which we last created
1242 			 * nodes matches the current label type
1243 			 */
1244 			return (0);
1245 		}
1246 	}
1247 
1248 	instance = ddi_get_instance(CMLB_DEVINFO(cl));
1249 
1250 	/*
1251 	 * Currently we only fix up the s7 node when we are switching
1252 	 * label types from or to EFI. This is consistent with
1253 	 * current behavior of sd.
1254 	 */
1255 	if (newlabeltype == CMLB_LABEL_EFI &&
1256 	    cl->cl_last_labeltype != CMLB_LABEL_EFI) {
1257 		/* from vtoc to EFI */
1258 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1259 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1260 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd",
1261 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1262 		    cl->cl_node_type, NULL);
1263 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd,raw",
1264 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1265 		    cl->cl_node_type, NULL);
1266 	} else {
1267 		/* from efi to vtoc */
1268 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1269 		ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1270 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h",
1271 		    S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1272 		    cl->cl_node_type, NULL);
1273 		(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h,raw",
1274 		    S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
1275 		    cl->cl_node_type, NULL);
1276 	}
1277 
1278 	cl->cl_last_labeltype = newlabeltype;
1279 	return (0);
1280 }
1281 
1282 /*
1283  *    Function: cmlb_validate_geometry
1284  *
1285  * Description: Read the label from the disk (if present). Update the unit's
1286  *		geometry and vtoc information from the data in the label.
1287  *		Verify that the label is valid.
1288  *
1289  *   Arguments:
1290  *	cl		driver soft state (unit) structure
1291  *
1292  *	forcerevalid	force revalidation even if we are already valid.
1293  *	flags		operation flags from target driver. Used for verbosity
1294  *			control	at this time.
1295  *	tg_cookie	cookie from target driver to be passed back to target
1296  *			driver when we call back to it through tg_ops.
1297  *
1298  * Return Code: 0 - Successful completion
1299  *		EINVAL  - Invalid value in cl->cl_tgt_blocksize or
1300  *			  cl->cl_blockcount; or label on disk is corrupted
1301  *			  or unreadable.
1302  *		EACCES  - Reservation conflict at the device.
1303  *		ENOMEM  - Resource allocation error
1304  *		ENOTSUP - geometry not applicable
1305  *
1306  *     Context: Kernel thread only (can sleep).
1307  */
1308 static int
1309 cmlb_validate_geometry(struct cmlb_lun *cl, int forcerevalid, int flags,
1310     void *tg_cookie)
1311 {
1312 	int		label_error = 0;
1313 	diskaddr_t	capacity;
1314 	int		count;
1315 #if defined(__i386) || defined(__amd64)
1316 	int forced_under_1t = 0;
1317 #endif
1318 
1319 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1320 
1321 	if ((cl->cl_f_geometry_is_valid == TRUE) && (forcerevalid == 0)) {
1322 		if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
1323 			return (ENOTSUP);
1324 		return (0);
1325 	}
1326 
1327 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
1328 		return (EIO);
1329 
1330 	capacity = cl->cl_blockcount;
1331 
1332 #if defined(_SUNOS_VTOC_16)
1333 	/*
1334 	 * Set up the "whole disk" fdisk partition; this should always
1335 	 * exist, regardless of whether the disk contains an fdisk table
1336 	 * or vtoc.
1337 	 */
1338 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1339 	/*
1340 	 * note if capacity > uint32_max we should be using efi,
1341 	 * and not use p0, so the truncation does not matter.
1342 	 */
1343 	cl->cl_map[P0_RAW_DISK].dkl_nblk  = capacity;
1344 #endif
1345 	/*
1346 	 * Refresh the logical and physical geometry caches.
1347 	 * (data from MODE SENSE format/rigid disk geometry pages,
1348 	 * and scsi_ifgetcap("geometry").
1349 	 */
1350 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
1351 
1352 	label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
1353 	if (label_error == 0) {
1354 
1355 		/* found a valid EFI label */
1356 		cmlb_dbg(CMLB_TRACE, cl,
1357 		    "cmlb_validate_geometry: found EFI label\n");
1358 		/*
1359 		 * solaris_size and geometry_is_valid are set in
1360 		 * cmlb_use_efi
1361 		 */
1362 		return (ENOTSUP);
1363 	}
1364 
1365 	/* NO EFI label found */
1366 
1367 	if (capacity > DK_MAX_BLOCKS) {
1368 		if (label_error == ESRCH) {
1369 			/*
1370 			 * they've configured a LUN over 1TB, but used
1371 			 * format.dat to restrict format's view of the
1372 			 * capacity to be under 1TB
1373 			 */
1374 			/* i.e > 1Tb with a VTOC < 1TB */
1375 			if (!(flags & CMLB_SILENT)) {
1376 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1377 				    CE_WARN, "is >1TB and has a VTOC label: "
1378 				    "use format(1M) to either decrease the");
1379 
1380 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1381 				    CE_NOTE, "size to be < 1TB or relabel the "
1382 				    "disk with an EFI label");
1383 #if defined(__i386) || defined(__amd64)
1384 				forced_under_1t = 1;
1385 #endif
1386 			}
1387 		} else {
1388 			/* unlabeled disk over 1TB */
1389 #if defined(__i386) || defined(__amd64)
1390 
1391 			/*
1392 			 * Refer to comments on off-by-1 at the head of the file
1393 			 * A 1TB disk was treated as (1T - 512)B in the past,
1394 			 * thus, it might have valid solaris partition. We
1395 			 * will return ENOTSUP later only if this disk has no
1396 			 * valid solaris partition.
1397 			 */
1398 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ||
1399 			    (cl->cl_sys_blocksize != cl->cl_tgt_blocksize) ||
1400 			    (capacity - 1 > DK_MAX_BLOCKS))
1401 #endif
1402 				return (ENOTSUP);
1403 		}
1404 	}
1405 
1406 	label_error = 0;
1407 
1408 	/*
1409 	 * at this point it is either labeled with a VTOC or it is
1410 	 * under 1TB (<= 1TB actually for off-by-1)
1411 	 */
1412 
1413 	/*
1414 	 * Only DIRECT ACCESS devices will have Scl labels.
1415 	 * CD's supposedly have a Scl label, too
1416 	 */
1417 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
1418 		struct	dk_label *dkl;
1419 		offset_t label_addr;
1420 		int	rval;
1421 		size_t	buffer_size;
1422 
1423 		/*
1424 		 * Note: This will set up cl->cl_solaris_size and
1425 		 * cl->cl_solaris_offset.
1426 		 */
1427 		rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1428 		if ((rval != 0) && !ISCD(cl)) {
1429 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1430 			return (rval);
1431 		}
1432 
1433 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1434 
1435 #if defined(__i386) || defined(__amd64)
1436 			/*
1437 			 * Refer to comments on off-by-1 at the head of the file
1438 			 * This is for 1TB disk only. Since that there is no
1439 			 * solaris partitions, return ENOTSUP as we do for
1440 			 * >1TB disk.
1441 			 */
1442 			if (cl->cl_blockcount > DK_MAX_BLOCKS)
1443 				return (ENOTSUP);
1444 #endif
1445 			/*
1446 			 * Found fdisk table but no Solaris partition entry,
1447 			 * so don't call cmlb_uselabel() and don't create
1448 			 * a default label.
1449 			 */
1450 			label_error = 0;
1451 			cl->cl_f_geometry_is_valid = TRUE;
1452 			goto no_solaris_partition;
1453 		}
1454 
1455 		label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1456 
1457 #if defined(__i386) || defined(__amd64)
1458 		/*
1459 		 * Refer to comments on off-by-1 at the head of the file
1460 		 * Now, this 1TB disk has valid solaris partition. It
1461 		 * must be created by previous sd driver, we have to
1462 		 * treat it as (1T-512)B.
1463 		 */
1464 		if ((cl->cl_blockcount > DK_MAX_BLOCKS) &&
1465 		    (forced_under_1t != 1)) {
1466 			/*
1467 			 * Refer to cmlb_read_fdisk, when there is no
1468 			 * fdisk partition table, cl_solaris_size is
1469 			 * set to disk's capacity. In this case, we
1470 			 * need to adjust it
1471 			 */
1472 			if (cl->cl_solaris_size > DK_MAX_BLOCKS)
1473 				cl->cl_solaris_size = DK_MAX_BLOCKS;
1474 			cmlb_resync_geom_caches(cl, DK_MAX_BLOCKS, tg_cookie);
1475 		}
1476 #endif
1477 
1478 		buffer_size = sizeof (struct dk_label);
1479 
1480 		cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
1481 		    "label_addr: 0x%x allocation size: 0x%x\n",
1482 		    label_addr, buffer_size);
1483 
1484 		if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
1485 			return (ENOMEM);
1486 
1487 		mutex_exit(CMLB_MUTEX(cl));
1488 		rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1489 		mutex_enter(CMLB_MUTEX(cl));
1490 
1491 		switch (rval) {
1492 		case 0:
1493 			/*
1494 			 * cmlb_uselabel will establish that the geometry
1495 			 * is valid.
1496 			 */
1497 			if (cmlb_uselabel(cl,
1498 			    (struct dk_label *)(uintptr_t)dkl, flags) !=
1499 			    CMLB_LABEL_IS_VALID) {
1500 				label_error = EINVAL;
1501 			} else
1502 				cl->cl_vtoc_label_is_from_media = 1;
1503 			break;
1504 		case EACCES:
1505 			label_error = EACCES;
1506 			break;
1507 		default:
1508 			label_error = EINVAL;
1509 			break;
1510 		}
1511 
1512 		kmem_free(dkl, buffer_size);
1513 	}
1514 
1515 	/*
1516 	 * If a valid label was not found, AND if no reservation conflict
1517 	 * was detected, then go ahead and create a default label (4069506).
1518 	 *
1519 	 * Note: currently, for VTOC_8 devices, the default label is created
1520 	 * for removables and hotpluggables only.  For VTOC_16 devices, the
1521 	 * default label will be created for all devices.
1522 	 * (see cmlb_build_default_label)
1523 	 */
1524 #if defined(_SUNOS_VTOC_8)
1525 	if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1526 	    (label_error != EACCES)) {
1527 #elif defined(_SUNOS_VTOC_16)
1528 	if (label_error != EACCES) {
1529 #endif
1530 		if (cl->cl_f_geometry_is_valid == FALSE) {
1531 			cmlb_build_default_label(cl, tg_cookie);
1532 		}
1533 		label_error = 0;
1534 	}
1535 
1536 no_solaris_partition:
1537 
1538 #if defined(_SUNOS_VTOC_16)
1539 	/*
1540 	 * If we have valid geometry, set up the remaining fdisk partitions.
1541 	 * Note that dkl_cylno is not used for the fdisk map entries, so
1542 	 * we set it to an entirely bogus value.
1543 	 */
1544 	for (count = 0; count < FD_NUMPART; count++) {
1545 		cl->cl_map[FDISK_P1 + count].dkl_cylno = -1;
1546 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
1547 		    cl->cl_fmap[count].fmap_nblk;
1548 
1549 		cl->cl_offset[FDISK_P1 + count] =
1550 		    cl->cl_fmap[count].fmap_start;
1551 	}
1552 #endif
1553 
1554 	for (count = 0; count < NDKMAP; count++) {
1555 #if defined(_SUNOS_VTOC_8)
1556 		struct dk_map *lp  = &cl->cl_map[count];
1557 		cl->cl_offset[count] =
1558 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
1559 #elif defined(_SUNOS_VTOC_16)
1560 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
1561 
1562 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
1563 #else
1564 #error "No VTOC format defined."
1565 #endif
1566 	}
1567 
1568 	return (label_error);
1569 }
1570 
1571 #if defined(_SUNOS_VTOC_16)
1572 /*
1573  * Macro: MAX_BLKS
1574  *
1575  *	This macro is used for table entries where we need to have the largest
1576  *	possible sector value for that head & SPT (sectors per track)
1577  *	combination.  Other entries for some smaller disk sizes are set by
1578  *	convention to match those used by X86 BIOS usage.
1579  */
1580 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
1581 
1582 /*
1583  *    Function: cmlb_convert_geometry
1584  *
1585  * Description: Convert physical geometry into a dk_geom structure. In
1586  *		other words, make sure we don't wrap 16-bit values.
1587  *		e.g. converting from geom_cache to dk_geom
1588  *
1589  *     Context: Kernel thread only
1590  */
1591 static void
1592 cmlb_convert_geometry(diskaddr_t capacity, struct dk_geom *cl_g)
1593 {
1594 	int i;
1595 	static const struct chs_values {
1596 		uint_t max_cap;		/* Max Capacity for this HS. */
1597 		uint_t nhead;		/* Heads to use. */
1598 		uint_t nsect;		/* SPT to use. */
1599 	} CHS_values[] = {
1600 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
1601 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
1602 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
1603 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
1604 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
1605 	};
1606 
1607 	/* Unlabeled SCSI floppy device */
1608 	if (capacity <= 0x1000) {
1609 		cl_g->dkg_nhead = 2;
1610 		cl_g->dkg_ncyl = 80;
1611 		cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
1612 		return;
1613 	}
1614 
1615 	/*
1616 	 * For all devices we calculate cylinders using the
1617 	 * heads and sectors we assign based on capacity of the
1618 	 * device.  The table is designed to be compatible with the
1619 	 * way other operating systems lay out fdisk tables for X86
1620 	 * and to insure that the cylinders never exceed 65535 to
1621 	 * prevent problems with X86 ioctls that report geometry.
1622 	 * We use SPT that are multiples of 63, since other OSes that
1623 	 * are not limited to 16-bits for cylinders stop at 63 SPT
1624 	 * we make do by using multiples of 63 SPT.
1625 	 *
1626 	 * Note than capacities greater than or equal to 1TB will simply
1627 	 * get the largest geometry from the table. This should be okay
1628 	 * since disks this large shouldn't be using CHS values anyway.
1629 	 */
1630 	for (i = 0; CHS_values[i].max_cap < capacity &&
1631 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
1632 		;
1633 
1634 	cl_g->dkg_nhead = CHS_values[i].nhead;
1635 	cl_g->dkg_nsect = CHS_values[i].nsect;
1636 }
1637 #endif
1638 
1639 /*
1640  *    Function: cmlb_resync_geom_caches
1641  *
1642  * Description: (Re)initialize both geometry caches: the virtual geometry
1643  *            information is extracted from the HBA (the "geometry"
1644  *            capability), and the physical geometry cache data is
1645  *            generated by issuing MODE SENSE commands.
1646  *
1647  *   Arguments:
1648  *	cl 		driver soft state (unit) structure
1649  *	capacity	disk capacity in #blocks
1650  *	tg_cookie	cookie from target driver to be passed back to target
1651  *			driver when we call back to it through tg_ops.
1652  *
1653  *     Context: Kernel thread only (can sleep).
1654  */
1655 static void
1656 cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1657     void *tg_cookie)
1658 {
1659 	struct	cmlb_geom 	pgeom;
1660 	struct	cmlb_geom	lgeom;
1661 	struct 	cmlb_geom	*pgeomp = &pgeom;
1662 	unsigned short 		nhead;
1663 	unsigned short 		nsect;
1664 	int 			spc;
1665 	int			ret;
1666 
1667 	ASSERT(cl != NULL);
1668 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1669 
1670 	/*
1671 	 * Ask the controller for its logical geometry.
1672 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1673 	 * then the lgeom cache will be invalid.
1674 	 */
1675 	mutex_exit(CMLB_MUTEX(cl));
1676 	bzero(&lgeom, sizeof (struct cmlb_geom));
1677 	ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1678 	mutex_enter(CMLB_MUTEX(cl));
1679 
1680 	bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
1681 
1682 	/*
1683 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1684 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1685 	 */
1686 	if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1687 	    cl->cl_lgeom.g_nhead == 0) {
1688 		/*
1689 		 * Note: Perhaps this needs to be more adaptive? The rationale
1690 		 * is that, if there's no HBA geometry from the HBA driver, any
1691 		 * guess is good, since this is the physical geometry. If MODE
1692 		 * SENSE fails this gives a max cylinder size for non-LBA access
1693 		 */
1694 		nhead = 255;
1695 		nsect = 63;
1696 	} else {
1697 		nhead = cl->cl_lgeom.g_nhead;
1698 		nsect = cl->cl_lgeom.g_nsect;
1699 	}
1700 
1701 	if (ISCD(cl)) {
1702 		pgeomp->g_nhead = 1;
1703 		pgeomp->g_nsect = nsect * nhead;
1704 	} else {
1705 		pgeomp->g_nhead = nhead;
1706 		pgeomp->g_nsect = nsect;
1707 	}
1708 
1709 	spc = pgeomp->g_nhead * pgeomp->g_nsect;
1710 	pgeomp->g_capacity = capacity;
1711 	pgeomp->g_ncyl = pgeomp->g_capacity / spc;
1712 	pgeomp->g_acyl = 0;
1713 
1714 	/*
1715 	 * Retrieve fresh geometry data from the hardware, stash it
1716 	 * here temporarily before we rebuild the incore label.
1717 	 *
1718 	 * We want to use the MODE SENSE commands to derive the
1719 	 * physical geometry of the device, but if either command
1720 	 * fails, the logical geometry is used as the fallback for
1721 	 * disk label geometry.
1722 	 */
1723 
1724 	mutex_exit(CMLB_MUTEX(cl));
1725 	(void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
1726 	mutex_enter(CMLB_MUTEX(cl));
1727 
1728 	/*
1729 	 * Now update the real copy while holding the mutex. This
1730 	 * way the global copy is never in an inconsistent state.
1731 	 */
1732 	bcopy(pgeomp, &cl->cl_pgeom,  sizeof (cl->cl_pgeom));
1733 
1734 	cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
1735 	    "(cached from lgeom)\n");
1736 	cmlb_dbg(CMLB_INFO,  cl,
1737 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
1738 	    cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
1739 	    cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
1740 	cmlb_dbg(CMLB_INFO,  cl, "   lbasize: %d; capacity: %ld; "
1741 	    "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
1742 	    cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
1743 	    cl->cl_pgeom.g_rpm);
1744 }
1745 
1746 
1747 /*
1748  *    Function: cmlb_read_fdisk
1749  *
1750  * Description: utility routine to read the fdisk table.
1751  *
1752  *   Arguments:
1753  *	cl		driver soft state (unit) structure
1754  *	capacity	disk capacity in #blocks
1755  *	tg_cookie	cookie from target driver to be passed back to target
1756  *			driver when we call back to it through tg_ops.
1757  *
1758  * Return Code: 0 for success (includes not reading for no_fdisk_present case
1759  *		errnos from tg_rw if failed to read the first block.
1760  *
1761  *     Context: Kernel thread only (can sleep).
1762  */
1763 /*ARGSUSED*/
1764 static int
1765 cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
1766 {
1767 #if defined(_NO_FDISK_PRESENT)
1768 
1769 	cl->cl_solaris_offset = 0;
1770 	cl->cl_solaris_size = capacity;
1771 	bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
1772 	return (0);
1773 
1774 #elif defined(_FIRMWARE_NEEDS_FDISK)
1775 
1776 	struct ipart	*fdp;
1777 	struct mboot	*mbp;
1778 	struct ipart	fdisk[FD_NUMPART];
1779 	int		i;
1780 	char		sigbuf[2];
1781 	caddr_t		bufp;
1782 	int		uidx;
1783 	int 		rval;
1784 	int		lba = 0;
1785 	uint_t		solaris_offset;	/* offset to solaris part. */
1786 	daddr_t		solaris_size;	/* size of solaris partition */
1787 	uint32_t	blocksize;
1788 
1789 	ASSERT(cl != NULL);
1790 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1791 
1792 	/*
1793 	 * Start off assuming no fdisk table
1794 	 */
1795 	solaris_offset = 0;
1796 	solaris_size   = capacity;
1797 
1798 	blocksize = cl->cl_tgt_blocksize;
1799 
1800 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
1801 
1802 	mutex_exit(CMLB_MUTEX(cl));
1803 	rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
1804 	mutex_enter(CMLB_MUTEX(cl));
1805 
1806 	if (rval != 0) {
1807 		cmlb_dbg(CMLB_ERROR,  cl,
1808 		    "cmlb_read_fdisk: fdisk read err\n");
1809 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
1810 		goto done;
1811 	}
1812 
1813 	mbp = (struct mboot *)bufp;
1814 
1815 	/*
1816 	 * The fdisk table does not begin on a 4-byte boundary within the
1817 	 * master boot record, so we copy it to an aligned structure to avoid
1818 	 * alignment exceptions on some processors.
1819 	 */
1820 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
1821 
1822 	/*
1823 	 * Check for lba support before verifying sig; sig might not be
1824 	 * there, say on a blank disk, but the max_chs mark may still
1825 	 * be present.
1826 	 *
1827 	 * Note: LBA support and BEFs are an x86-only concept but this
1828 	 * code should work OK on SPARC as well.
1829 	 */
1830 
1831 	/*
1832 	 * First, check for lba-access-ok on root node (or prom root node)
1833 	 * if present there, don't need to search fdisk table.
1834 	 */
1835 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
1836 	    "lba-access-ok", 0) != 0) {
1837 		/* All drives do LBA; don't search fdisk table */
1838 		lba = 1;
1839 	} else {
1840 		/* Okay, look for mark in fdisk table */
1841 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
1842 			/* accumulate "lba" value from all partitions */
1843 			lba = (lba || cmlb_has_max_chs_vals(fdp));
1844 		}
1845 	}
1846 
1847 	if (lba != 0) {
1848 		dev_t dev = cmlb_make_device(cl);
1849 
1850 		if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
1851 		    "lba-access-ok", 0) == 0) {
1852 			/* not found; create it */
1853 			if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
1854 			    "lba-access-ok", (caddr_t)NULL, 0) !=
1855 			    DDI_PROP_SUCCESS) {
1856 				cmlb_dbg(CMLB_ERROR,  cl,
1857 				    "cmlb_read_fdisk: Can't create lba "
1858 				    "property for instance %d\n",
1859 				    ddi_get_instance(CMLB_DEVINFO(cl)));
1860 			}
1861 		}
1862 	}
1863 
1864 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
1865 
1866 	/*
1867 	 * Endian-independent signature check
1868 	 */
1869 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
1870 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
1871 		cmlb_dbg(CMLB_ERROR,  cl,
1872 		    "cmlb_read_fdisk: no fdisk\n");
1873 		bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
1874 		goto done;
1875 	}
1876 
1877 #ifdef CMLBDEBUG
1878 	if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
1879 		fdp = fdisk;
1880 		cmlb_dbg(CMLB_INFO,  cl, "cmlb_read_fdisk:\n");
1881 		cmlb_dbg(CMLB_INFO,  cl, "         relsect    "
1882 		    "numsect         sysid       bootid\n");
1883 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
1884 			cmlb_dbg(CMLB_INFO,  cl,
1885 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
1886 			    i, fdp->relsect, fdp->numsect,
1887 			    fdp->systid, fdp->bootid);
1888 		}
1889 	}
1890 #endif
1891 
1892 	/*
1893 	 * Try to find the unix partition
1894 	 */
1895 	uidx = -1;
1896 	solaris_offset = 0;
1897 	solaris_size   = 0;
1898 
1899 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
1900 		int	relsect;
1901 		int	numsect;
1902 
1903 		if (fdp->numsect == 0) {
1904 			cl->cl_fmap[i].fmap_start = 0;
1905 			cl->cl_fmap[i].fmap_nblk  = 0;
1906 			continue;
1907 		}
1908 
1909 		/*
1910 		 * Data in the fdisk table is little-endian.
1911 		 */
1912 		relsect = LE_32(fdp->relsect);
1913 		numsect = LE_32(fdp->numsect);
1914 
1915 		cl->cl_fmap[i].fmap_start = relsect;
1916 		cl->cl_fmap[i].fmap_nblk  = numsect;
1917 
1918 		if (fdp->systid != SUNIXOS &&
1919 		    fdp->systid != SUNIXOS2 &&
1920 		    fdp->systid != EFI_PMBR) {
1921 			continue;
1922 		}
1923 
1924 		/*
1925 		 * use the last active solaris partition id found
1926 		 * (there should only be 1 active partition id)
1927 		 *
1928 		 * if there are no active solaris partition id
1929 		 * then use the first inactive solaris partition id
1930 		 */
1931 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
1932 			uidx = i;
1933 			solaris_offset = relsect;
1934 			solaris_size   = numsect;
1935 		}
1936 	}
1937 
1938 	cmlb_dbg(CMLB_INFO,  cl, "fdisk 0x%x 0x%lx",
1939 	    cl->cl_solaris_offset, cl->cl_solaris_size);
1940 done:
1941 
1942 	/*
1943 	 * Clear the VTOC info, only if the Solaris partition entry
1944 	 * has moved, changed size, been deleted, or if the size of
1945 	 * the partition is too small to even fit the label sector.
1946 	 */
1947 	if ((cl->cl_solaris_offset != solaris_offset) ||
1948 	    (cl->cl_solaris_size != solaris_size) ||
1949 	    solaris_size <= DK_LABEL_LOC) {
1950 		cmlb_dbg(CMLB_INFO,  cl, "fdisk moved 0x%x 0x%lx",
1951 		    solaris_offset, solaris_size);
1952 		bzero(&cl->cl_g, sizeof (struct dk_geom));
1953 		bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
1954 		bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
1955 		cl->cl_f_geometry_is_valid = FALSE;
1956 	}
1957 	cl->cl_solaris_offset = solaris_offset;
1958 	cl->cl_solaris_size = solaris_size;
1959 	kmem_free(bufp, blocksize);
1960 	return (rval);
1961 
1962 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
1963 #error "fdisk table presence undetermined for this platform."
1964 #endif	/* #if defined(_NO_FDISK_PRESENT) */
1965 }
1966 
1967 static void
1968 cmlb_swap_efi_gpt(efi_gpt_t *e)
1969 {
1970 	_NOTE(ASSUMING_PROTECTED(*e))
1971 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
1972 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
1973 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
1974 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
1975 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
1976 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
1977 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
1978 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
1979 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
1980 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
1981 	e->efi_gpt_NumberOfPartitionEntries =
1982 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
1983 	e->efi_gpt_SizeOfPartitionEntry =
1984 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
1985 	e->efi_gpt_PartitionEntryArrayCRC32 =
1986 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
1987 }
1988 
1989 static void
1990 cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
1991 {
1992 	int i;
1993 
1994 	_NOTE(ASSUMING_PROTECTED(*p))
1995 	for (i = 0; i < nparts; i++) {
1996 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
1997 		    p[i].efi_gpe_PartitionTypeGUID);
1998 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
1999 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
2000 		/* PartitionAttrs */
2001 	}
2002 }
2003 
2004 static int
2005 cmlb_validate_efi(efi_gpt_t *labp)
2006 {
2007 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
2008 		return (EINVAL);
2009 	/* at least 96 bytes in this version of the spec. */
2010 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
2011 	    labp->efi_gpt_HeaderSize)
2012 		return (EINVAL);
2013 	/* this should be 128 bytes */
2014 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
2015 		return (EINVAL);
2016 	return (0);
2017 }
2018 
2019 static int
2020 cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2021     void *tg_cookie)
2022 {
2023 	int		i;
2024 	int		rval = 0;
2025 	efi_gpe_t	*partitions;
2026 	uchar_t		*buf;
2027 	uint_t		lbasize;	/* is really how much to read */
2028 	diskaddr_t	cap = 0;
2029 	uint_t		nparts;
2030 	diskaddr_t	gpe_lba;
2031 	int		iofailed = 0;
2032 	struct uuid	uuid_type_reserved = EFI_RESERVED;
2033 
2034 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2035 
2036 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
2037 		rval = EINVAL;
2038 		goto done_err1;
2039 	}
2040 
2041 
2042 	lbasize = cl->cl_sys_blocksize;
2043 
2044 	cl->cl_reserved = -1;
2045 	mutex_exit(CMLB_MUTEX(cl));
2046 
2047 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
2048 
2049 	rval = DK_TG_READ(cl, buf, 0, lbasize, tg_cookie);
2050 	if (rval) {
2051 		iofailed = 1;
2052 		goto done_err;
2053 	}
2054 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
2055 		/* not ours */
2056 		rval = ESRCH;
2057 		goto done_err;
2058 	}
2059 
2060 	rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
2061 	if (rval) {
2062 		iofailed = 1;
2063 		goto done_err;
2064 	}
2065 	cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2066 
2067 	if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2068 		/*
2069 		 * Couldn't read the primary, try the backup.  Our
2070 		 * capacity at this point could be based on CHS, so
2071 		 * check what the device reports.
2072 		 */
2073 		rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
2074 		if (rval) {
2075 			iofailed = 1;
2076 			goto done_err;
2077 		}
2078 
2079 		/*
2080 		 * CMLB_OFF_BY_ONE case, we check the next to last block first
2081 		 * for backup GPT header, otherwise check the last block.
2082 		 */
2083 
2084 		if ((rval = DK_TG_READ(cl, buf,
2085 		    cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2086 		    lbasize, tg_cookie))
2087 		    != 0) {
2088 			iofailed = 1;
2089 			goto done_err;
2090 		}
2091 		cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2092 
2093 		if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2094 
2095 			if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2096 				goto done_err;
2097 			if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2098 			    tg_cookie)) != 0)
2099 				goto done_err;
2100 			cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2101 			if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
2102 				goto done_err;
2103 		}
2104 		if (!(flags & CMLB_SILENT))
2105 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2106 			    "primary label corrupt; using backup\n");
2107 	}
2108 
2109 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
2110 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2111 
2112 	rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
2113 	if (rval) {
2114 		iofailed = 1;
2115 		goto done_err;
2116 	}
2117 	partitions = (efi_gpe_t *)buf;
2118 
2119 	if (nparts > MAXPART) {
2120 		nparts = MAXPART;
2121 	}
2122 	cmlb_swap_efi_gpe(nparts, partitions);
2123 
2124 	mutex_enter(CMLB_MUTEX(cl));
2125 
2126 	/* Fill in partition table. */
2127 	for (i = 0; i < nparts; i++) {
2128 		if (partitions->efi_gpe_StartingLBA != 0 ||
2129 		    partitions->efi_gpe_EndingLBA != 0) {
2130 			cl->cl_map[i].dkl_cylno =
2131 			    partitions->efi_gpe_StartingLBA;
2132 			cl->cl_map[i].dkl_nblk =
2133 			    partitions->efi_gpe_EndingLBA -
2134 			    partitions->efi_gpe_StartingLBA + 1;
2135 			cl->cl_offset[i] =
2136 			    partitions->efi_gpe_StartingLBA;
2137 		}
2138 
2139 		if (cl->cl_reserved == -1) {
2140 			if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2141 			    &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2142 				cl->cl_reserved = i;
2143 			}
2144 		}
2145 		if (i == WD_NODE) {
2146 			/*
2147 			 * minor number 7 corresponds to the whole disk
2148 			 */
2149 			cl->cl_map[i].dkl_cylno = 0;
2150 			cl->cl_map[i].dkl_nblk = capacity;
2151 			cl->cl_offset[i] = 0;
2152 		}
2153 		partitions++;
2154 	}
2155 	cl->cl_solaris_offset = 0;
2156 	cl->cl_solaris_size = capacity;
2157 	cl->cl_f_geometry_is_valid = TRUE;
2158 
2159 	/* clear the vtoc label */
2160 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2161 
2162 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2163 	return (0);
2164 
2165 done_err:
2166 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2167 	mutex_enter(CMLB_MUTEX(cl));
2168 done_err1:
2169 	/*
2170 	 * if we didn't find something that could look like a VTOC
2171 	 * and the disk is over 1TB, we know there isn't a valid label.
2172 	 * Otherwise let cmlb_uselabel decide what to do.  We only
2173 	 * want to invalidate this if we're certain the label isn't
2174 	 * valid because cmlb_prop_op will now fail, which in turn
2175 	 * causes things like opens and stats on the partition to fail.
2176 	 */
2177 	if ((capacity > DK_MAX_BLOCKS) && (rval != ESRCH) && !iofailed) {
2178 		cl->cl_f_geometry_is_valid = FALSE;
2179 	}
2180 	return (rval);
2181 }
2182 
2183 
2184 /*
2185  *    Function: cmlb_uselabel
2186  *
2187  * Description: Validate the disk label and update the relevant data (geometry,
2188  *		partition, vtoc, and capacity data) in the cmlb_lun struct.
2189  *		Marks the geometry of the unit as being valid.
2190  *
2191  *   Arguments: cl: unit struct.
2192  *		dk_label: disk label
2193  *
2194  * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
2195  *		partition, vtoc, and capacity data are good.
2196  *
2197  *		CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
2198  *		label; or computed capacity does not jibe with capacity
2199  *		reported from the READ CAPACITY command.
2200  *
2201  *     Context: Kernel thread only (can sleep).
2202  */
2203 static int
2204 cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
2205 {
2206 	short		*sp;
2207 	short		sum;
2208 	short		count;
2209 	int		label_error = CMLB_LABEL_IS_VALID;
2210 	int		i;
2211 	diskaddr_t	label_capacity;
2212 	int		part_end;
2213 	diskaddr_t	track_capacity;
2214 #if defined(_SUNOS_VTOC_16)
2215 	struct	dkl_partition	*vpartp;
2216 #endif
2217 	ASSERT(cl != NULL);
2218 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2219 
2220 	/* Validate the magic number of the label. */
2221 	if (labp->dkl_magic != DKL_MAGIC) {
2222 #if defined(__sparc)
2223 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2224 			if (!(flags & CMLB_SILENT))
2225 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2226 				    CE_WARN,
2227 				    "Corrupt label; wrong magic number\n");
2228 		}
2229 #endif
2230 		return (CMLB_LABEL_IS_INVALID);
2231 	}
2232 
2233 	/* Validate the checksum of the label. */
2234 	sp  = (short *)labp;
2235 	sum = 0;
2236 	count = sizeof (struct dk_label) / sizeof (short);
2237 	while (count--)	 {
2238 		sum ^= *sp++;
2239 	}
2240 
2241 	if (sum != 0) {
2242 #if defined(_SUNOS_VTOC_16)
2243 		if (!ISCD(cl)) {
2244 #elif defined(_SUNOS_VTOC_8)
2245 		if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2246 #endif
2247 			if (!(flags & CMLB_SILENT))
2248 				cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
2249 				    CE_WARN,
2250 				    "Corrupt label - label checksum failed\n");
2251 		}
2252 		return (CMLB_LABEL_IS_INVALID);
2253 	}
2254 
2255 
2256 	/*
2257 	 * Fill in geometry structure with data from label.
2258 	 */
2259 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2260 	cl->cl_g.dkg_ncyl   = labp->dkl_ncyl;
2261 	cl->cl_g.dkg_acyl   = labp->dkl_acyl;
2262 	cl->cl_g.dkg_bcyl   = 0;
2263 	cl->cl_g.dkg_nhead  = labp->dkl_nhead;
2264 	cl->cl_g.dkg_nsect  = labp->dkl_nsect;
2265 	cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
2266 
2267 #if defined(_SUNOS_VTOC_8)
2268 	cl->cl_g.dkg_gap1   = labp->dkl_gap1;
2269 	cl->cl_g.dkg_gap2   = labp->dkl_gap2;
2270 	cl->cl_g.dkg_bhead  = labp->dkl_bhead;
2271 #endif
2272 #if defined(_SUNOS_VTOC_16)
2273 	cl->cl_dkg_skew = labp->dkl_skew;
2274 #endif
2275 
2276 #if defined(__i386) || defined(__amd64)
2277 	cl->cl_g.dkg_apc = labp->dkl_apc;
2278 #endif
2279 
2280 	/*
2281 	 * Currently we rely on the values in the label being accurate. If
2282 	 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
2283 	 *
2284 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
2285 	 * although this command is optional in SCSI-2.
2286 	 */
2287 	cl->cl_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
2288 	cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
2289 	    (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
2290 
2291 	/*
2292 	 * The Read and Write reinstruct values may not be valid
2293 	 * for older disks.
2294 	 */
2295 	cl->cl_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
2296 	cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
2297 
2298 	/* Fill in partition table. */
2299 #if defined(_SUNOS_VTOC_8)
2300 	for (i = 0; i < NDKMAP; i++) {
2301 		cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
2302 		cl->cl_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
2303 	}
2304 #endif
2305 #if  defined(_SUNOS_VTOC_16)
2306 	vpartp		= labp->dkl_vtoc.v_part;
2307 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
2308 
2309 	/* Prevent divide by zero */
2310 	if (track_capacity == 0) {
2311 		if (!(flags & CMLB_SILENT))
2312 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2313 			    "Corrupt label - zero nhead or nsect value\n");
2314 
2315 		return (CMLB_LABEL_IS_INVALID);
2316 	}
2317 
2318 	for (i = 0; i < NDKMAP; i++, vpartp++) {
2319 		cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
2320 		cl->cl_map[i].dkl_nblk  = vpartp->p_size;
2321 	}
2322 #endif
2323 
2324 	/* Fill in VTOC Structure. */
2325 	bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
2326 #if defined(_SUNOS_VTOC_8)
2327 	/*
2328 	 * The 8-slice vtoc does not include the ascii label; save it into
2329 	 * the device's soft state structure here.
2330 	 */
2331 	bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
2332 #endif
2333 
2334 	/* Now look for a valid capacity. */
2335 	track_capacity	= (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
2336 	label_capacity	= (cl->cl_g.dkg_ncyl  * track_capacity);
2337 
2338 	if (cl->cl_g.dkg_acyl) {
2339 #if defined(__i386) || defined(__amd64)
2340 		/* we may have > 1 alts cylinder */
2341 		label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
2342 #else
2343 		label_capacity += track_capacity;
2344 #endif
2345 	}
2346 
2347 	/*
2348 	 * Force check here to ensure the computed capacity is valid.
2349 	 * If capacity is zero, it indicates an invalid label and
2350 	 * we should abort updating the relevant data then.
2351 	 */
2352 	if (label_capacity == 0) {
2353 		if (!(flags & CMLB_SILENT))
2354 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2355 			    "Corrupt label - no valid capacity could be "
2356 			    "retrieved\n");
2357 
2358 		return (CMLB_LABEL_IS_INVALID);
2359 	}
2360 
2361 	/* Mark the geometry as valid. */
2362 	cl->cl_f_geometry_is_valid = TRUE;
2363 
2364 	/*
2365 	 * if we got invalidated when mutex exit and entered again,
2366 	 * if blockcount different than when we came in, need to
2367 	 * retry from beginning of cmlb_validate_geometry.
2368 	 * revisit this on next phase of utilizing this for
2369 	 * sd.
2370 	 */
2371 
2372 	if (label_capacity <= cl->cl_blockcount) {
2373 #if defined(_SUNOS_VTOC_8)
2374 		/*
2375 		 * We can't let this happen on drives that are subdivided
2376 		 * into logical disks (i.e., that have an fdisk table).
2377 		 * The cl_blockcount field should always hold the full media
2378 		 * size in sectors, period.  This code would overwrite
2379 		 * cl_blockcount with the size of the Solaris fdisk partition.
2380 		 */
2381 		cmlb_dbg(CMLB_ERROR,  cl,
2382 		    "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
2383 		    label_capacity, cl->cl_blockcount);
2384 		cl->cl_solaris_size = label_capacity;
2385 
2386 #endif	/* defined(_SUNOS_VTOC_8) */
2387 		goto done;
2388 	}
2389 
2390 	if (ISCD(cl)) {
2391 		/* For CDROMs, we trust that the data in the label is OK. */
2392 #if defined(_SUNOS_VTOC_8)
2393 		for (i = 0; i < NDKMAP; i++) {
2394 			part_end = labp->dkl_nhead * labp->dkl_nsect *
2395 			    labp->dkl_map[i].dkl_cylno +
2396 			    labp->dkl_map[i].dkl_nblk  - 1;
2397 
2398 			if ((labp->dkl_map[i].dkl_nblk) &&
2399 			    (part_end > cl->cl_blockcount)) {
2400 				cl->cl_f_geometry_is_valid = FALSE;
2401 				break;
2402 			}
2403 		}
2404 #endif
2405 #if defined(_SUNOS_VTOC_16)
2406 		vpartp = &(labp->dkl_vtoc.v_part[0]);
2407 		for (i = 0; i < NDKMAP; i++, vpartp++) {
2408 			part_end = vpartp->p_start + vpartp->p_size;
2409 			if ((vpartp->p_size > 0) &&
2410 			    (part_end > cl->cl_blockcount)) {
2411 				cl->cl_f_geometry_is_valid = FALSE;
2412 				break;
2413 			}
2414 		}
2415 #endif
2416 	} else {
2417 		/* label_capacity > cl->cl_blockcount */
2418 		if (!(flags & CMLB_SILENT)) {
2419 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2420 			    "Corrupt label - bad geometry\n");
2421 			cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
2422 			    "Label says %llu blocks; Drive says %llu blocks\n",
2423 			    label_capacity, cl->cl_blockcount);
2424 		}
2425 		cl->cl_f_geometry_is_valid = FALSE;
2426 		label_error = CMLB_LABEL_IS_INVALID;
2427 	}
2428 
2429 done:
2430 
2431 	cmlb_dbg(CMLB_INFO,  cl, "cmlb_uselabel: (label geometry)\n");
2432 	cmlb_dbg(CMLB_INFO,  cl,
2433 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
2434 	    cl->cl_g.dkg_ncyl,  cl->cl_g.dkg_acyl,
2435 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
2436 
2437 	cmlb_dbg(CMLB_INFO,  cl,
2438 	    "   label_capacity: %d; intrlv: %d; rpm: %d\n",
2439 	    cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
2440 	cmlb_dbg(CMLB_INFO,  cl, "   wrt_reinstr: %d; rd_reinstr: %d\n",
2441 	    cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
2442 
2443 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2444 
2445 	return (label_error);
2446 }
2447 
2448 
2449 /*
2450  *    Function: cmlb_build_default_label
2451  *
2452  * Description: Generate a default label for those devices that do not have
2453  *		one, e.g., new media, removable cartridges, etc..
2454  *
2455  *     Context: Kernel thread only
2456  */
2457 /*ARGSUSED*/
2458 static void
2459 cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
2460 {
2461 #if defined(_SUNOS_VTOC_16)
2462 	uint_t	phys_spc;
2463 	uint_t	disksize;
2464 	struct  dk_geom cl_g;
2465 	diskaddr_t capacity;
2466 #endif
2467 
2468 	ASSERT(cl != NULL);
2469 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2470 
2471 #if defined(_SUNOS_VTOC_8)
2472 	/*
2473 	 * Note: This is a legacy check for non-removable devices on VTOC_8
2474 	 * only. This may be a valid check for VTOC_16 as well.
2475 	 * Once we understand why there is this difference between SPARC and
2476 	 * x86 platform, we could remove this legacy check.
2477 	 */
2478 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2479 		return;
2480 	}
2481 #endif
2482 
2483 	bzero(&cl->cl_g, sizeof (struct dk_geom));
2484 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2485 	bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
2486 
2487 #if defined(_SUNOS_VTOC_8)
2488 
2489 	/*
2490 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
2491 	 * But it is still necessary to set up various geometry information,
2492 	 * and we are doing this here.
2493 	 */
2494 
2495 	/*
2496 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
2497 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
2498 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
2499 	 * equal to C*H*S values.  This will cause some truncation of size due
2500 	 * to round off errors. For CD-ROMs, this truncation can have adverse
2501 	 * side effects, so returning ncyl and nhead as 1. The nsect will
2502 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
2503 	 */
2504 	cl->cl_solaris_size = cl->cl_blockcount;
2505 	if (ISCD(cl)) {
2506 		tg_attribute_t tgattribute;
2507 		int is_writable;
2508 		/*
2509 		 * Preserve the old behavior for non-writable
2510 		 * medias. Since dkg_nsect is a ushort, it
2511 		 * will lose bits as cdroms have more than
2512 		 * 65536 sectors. So if we recalculate
2513 		 * capacity, it will become much shorter.
2514 		 * But the dkg_* information is not
2515 		 * used for CDROMs so it is OK. But for
2516 		 * Writable CDs we need this information
2517 		 * to be valid (for newfs say). So we
2518 		 * make nsect and nhead > 1 that way
2519 		 * nsect can still stay within ushort limit
2520 		 * without losing any bits.
2521 		 */
2522 
2523 		bzero(&tgattribute, sizeof (tg_attribute_t));
2524 
2525 		mutex_exit(CMLB_MUTEX(cl));
2526 		is_writable =
2527 		    (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
2528 		    tgattribute.media_is_writable : 1;
2529 		mutex_enter(CMLB_MUTEX(cl));
2530 
2531 		if (is_writable) {
2532 			cl->cl_g.dkg_nhead = 64;
2533 			cl->cl_g.dkg_nsect = 32;
2534 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
2535 			cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
2536 			    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
2537 		} else {
2538 			cl->cl_g.dkg_ncyl  = 1;
2539 			cl->cl_g.dkg_nhead = 1;
2540 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
2541 		}
2542 	} else {
2543 		if (cl->cl_blockcount <= 0x1000) {
2544 			/* unlabeled SCSI floppy device */
2545 			cl->cl_g.dkg_nhead = 2;
2546 			cl->cl_g.dkg_ncyl = 80;
2547 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
2548 		} else if (cl->cl_blockcount <= 0x200000) {
2549 			cl->cl_g.dkg_nhead = 64;
2550 			cl->cl_g.dkg_nsect = 32;
2551 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (64 * 32);
2552 		} else {
2553 			cl->cl_g.dkg_nhead = 255;
2554 			cl->cl_g.dkg_nsect = 63;
2555 			cl->cl_g.dkg_ncyl  = cl->cl_blockcount / (255 * 63);
2556 		}
2557 		cl->cl_solaris_size =
2558 		    cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
2559 
2560 	}
2561 
2562 	cl->cl_g.dkg_acyl	= 0;
2563 	cl->cl_g.dkg_bcyl	= 0;
2564 	cl->cl_g.dkg_rpm	= 200;
2565 	cl->cl_asciilabel[0]	= '\0';
2566 	cl->cl_g.dkg_pcyl	= cl->cl_g.dkg_ncyl;
2567 
2568 	cl->cl_map[0].dkl_cylno = 0;
2569 	cl->cl_map[0].dkl_nblk  = cl->cl_solaris_size;
2570 
2571 	cl->cl_map[2].dkl_cylno = 0;
2572 	cl->cl_map[2].dkl_nblk  = cl->cl_solaris_size;
2573 
2574 #elif defined(_SUNOS_VTOC_16)
2575 
2576 	if (cl->cl_solaris_size == 0) {
2577 		/*
2578 		 * Got fdisk table but no solaris entry therefore
2579 		 * don't create a default label
2580 		 */
2581 		cl->cl_f_geometry_is_valid = TRUE;
2582 		return;
2583 	}
2584 
2585 	/*
2586 	 * For CDs we continue to use the physical geometry to calculate
2587 	 * number of cylinders. All other devices must convert the
2588 	 * physical geometry (cmlb_geom) to values that will fit
2589 	 * in a dk_geom structure.
2590 	 */
2591 	if (ISCD(cl)) {
2592 		phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
2593 	} else {
2594 		/* Convert physical geometry to disk geometry */
2595 		bzero(&cl_g, sizeof (struct dk_geom));
2596 
2597 		/*
2598 		 * Refer to comments related to off-by-1 at the
2599 		 * header of this file.
2600 		 * Before caculating geometry, capacity should be
2601 		 * decreased by 1.
2602 		 */
2603 
2604 		if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
2605 			capacity = cl->cl_blockcount - 1;
2606 		else
2607 			capacity = cl->cl_blockcount;
2608 
2609 
2610 		cmlb_convert_geometry(capacity, &cl_g);
2611 		bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
2612 		phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
2613 	}
2614 
2615 	ASSERT(phys_spc != 0);
2616 	cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
2617 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2618 		/* disable devid */
2619 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
2620 		disksize = cl->cl_solaris_size;
2621 	} else {
2622 		cl->cl_g.dkg_acyl = DK_ACYL;
2623 		cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
2624 		disksize = cl->cl_g.dkg_ncyl * phys_spc;
2625 	}
2626 
2627 	if (ISCD(cl)) {
2628 		/*
2629 		 * CD's don't use the "heads * sectors * cyls"-type of
2630 		 * geometry, but instead use the entire capacity of the media.
2631 		 */
2632 		disksize = cl->cl_solaris_size;
2633 		cl->cl_g.dkg_nhead = 1;
2634 		cl->cl_g.dkg_nsect = 1;
2635 		cl->cl_g.dkg_rpm =
2636 		    (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
2637 
2638 		cl->cl_vtoc.v_part[0].p_start = 0;
2639 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2640 		cl->cl_vtoc.v_part[0].p_tag   = V_BACKUP;
2641 		cl->cl_vtoc.v_part[0].p_flag  = V_UNMNT;
2642 
2643 		cl->cl_map[0].dkl_cylno = 0;
2644 		cl->cl_map[0].dkl_nblk  = disksize;
2645 		cl->cl_offset[0] = 0;
2646 
2647 	} else {
2648 		/*
2649 		 * Hard disks and removable media cartridges
2650 		 */
2651 		cl->cl_g.dkg_rpm =
2652 		    (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
2653 		cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
2654 
2655 		/* Add boot slice */
2656 		cl->cl_vtoc.v_part[8].p_start = 0;
2657 		cl->cl_vtoc.v_part[8].p_size  = phys_spc;
2658 		cl->cl_vtoc.v_part[8].p_tag   = V_BOOT;
2659 		cl->cl_vtoc.v_part[8].p_flag  = V_UNMNT;
2660 
2661 		cl->cl_map[8].dkl_cylno = 0;
2662 		cl->cl_map[8].dkl_nblk  = phys_spc;
2663 		cl->cl_offset[8] = 0;
2664 
2665 		if ((cl->cl_alter_behavior &
2666 		    CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
2667 		    cl->cl_device_type == DTYPE_DIRECT) {
2668 			cl->cl_vtoc.v_part[9].p_start = phys_spc;
2669 			cl->cl_vtoc.v_part[9].p_size  = 2 * phys_spc;
2670 			cl->cl_vtoc.v_part[9].p_tag   = V_ALTSCTR;
2671 			cl->cl_vtoc.v_part[9].p_flag  = 0;
2672 
2673 			cl->cl_map[9].dkl_cylno = 1;
2674 			cl->cl_map[9].dkl_nblk  = 2 * phys_spc;
2675 			cl->cl_offset[9] = phys_spc;
2676 		}
2677 	}
2678 
2679 	cl->cl_g.dkg_apc = 0;
2680 	cl->cl_vtoc.v_nparts = V_NUMPAR;
2681 	cl->cl_vtoc.v_version = V_VERSION;
2682 
2683 	/* Add backup slice */
2684 	cl->cl_vtoc.v_part[2].p_start = 0;
2685 	cl->cl_vtoc.v_part[2].p_size  = disksize;
2686 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
2687 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
2688 
2689 	cl->cl_map[2].dkl_cylno = 0;
2690 	cl->cl_map[2].dkl_nblk  = disksize;
2691 	cl->cl_offset[2] = 0;
2692 
2693 	/*
2694 	 * single slice (s0) covering the entire disk
2695 	 */
2696 	if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
2697 		cl->cl_vtoc.v_part[0].p_start = 0;
2698 		cl->cl_vtoc.v_part[0].p_tag   = V_UNASSIGNED;
2699 		cl->cl_vtoc.v_part[0].p_flag  = 0;
2700 		cl->cl_vtoc.v_part[0].p_size  = disksize;
2701 		cl->cl_map[0].dkl_cylno = 0;
2702 		cl->cl_map[0].dkl_nblk  = disksize;
2703 		cl->cl_offset[0] = 0;
2704 	}
2705 
2706 	(void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
2707 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
2708 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
2709 
2710 #else
2711 #error "No VTOC format defined."
2712 #endif
2713 
2714 	cl->cl_g.dkg_read_reinstruct  = 0;
2715 	cl->cl_g.dkg_write_reinstruct = 0;
2716 
2717 	cl->cl_g.dkg_intrlv = 1;
2718 
2719 	cl->cl_vtoc.v_sanity  = VTOC_SANE;
2720 
2721 	cl->cl_f_geometry_is_valid = TRUE;
2722 	cl->cl_vtoc_label_is_from_media = 0;
2723 
2724 	cmlb_dbg(CMLB_INFO,  cl,
2725 	    "cmlb_build_default_label: Default label created: "
2726 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
2727 	    cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
2728 	    cl->cl_g.dkg_nsect, cl->cl_blockcount);
2729 }
2730 
2731 
2732 #if defined(_FIRMWARE_NEEDS_FDISK)
2733 /*
2734  * Max CHS values, as they are encoded into bytes, for 1022/254/63
2735  */
2736 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
2737 #define	LBA_MAX_CYL	(1022 & 0xFF)
2738 #define	LBA_MAX_HEAD	(254)
2739 
2740 
2741 /*
2742  *    Function: cmlb_has_max_chs_vals
2743  *
2744  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
2745  *
2746  *   Arguments: fdp - ptr to CHS info
2747  *
2748  * Return Code: True or false
2749  *
2750  *     Context: Any.
2751  */
2752 static int
2753 cmlb_has_max_chs_vals(struct ipart *fdp)
2754 {
2755 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
2756 	    (fdp->beghead == LBA_MAX_HEAD)	&&
2757 	    (fdp->begsect == LBA_MAX_SECT)	&&
2758 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
2759 	    (fdp->endhead == LBA_MAX_HEAD)	&&
2760 	    (fdp->endsect == LBA_MAX_SECT));
2761 }
2762 #endif
2763 
2764 /*
2765  *    Function: cmlb_dkio_get_geometry
2766  *
2767  * Description: This routine is the driver entry point for handling user
2768  *		requests to get the device geometry (DKIOCGGEOM).
2769  *
2770  *   Arguments:
2771  *	arg		pointer to user provided dk_geom structure specifying
2772  *			the controller's notion of the current geometry.
2773  *
2774  *	flag 		this argument is a pass through to ddi_copyxxx()
2775  *			directly from the mode argument of ioctl().
2776  *
2777  *	tg_cookie	cookie from target driver to be passed back to target
2778  *			driver when we call back to it through tg_ops.
2779  *
2780  * Return Code: 0
2781  *		EFAULT
2782  *		ENXIO
2783  *		EIO
2784  */
2785 static int
2786 cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
2787     void *tg_cookie)
2788 {
2789 	struct dk_geom	*tmp_geom = NULL;
2790 	int		rval = 0;
2791 
2792 	/*
2793 	 * cmlb_validate_geometry does not spin a disk up
2794 	 * if it was spcl down. We need to make sure it
2795 	 * is ready.
2796 	 */
2797 	mutex_enter(CMLB_MUTEX(cl));
2798 	rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie);
2799 #if defined(_SUNOS_VTOC_8)
2800 	if (rval == EINVAL &&
2801 	    cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
2802 		/*
2803 		 * This is to return a default label geometry even when we
2804 		 * do not really assume a default label for the device.
2805 		 * dad driver utilizes this.
2806 		 */
2807 		if (cl->cl_blockcount <= DK_MAX_BLOCKS) {
2808 			cmlb_setup_default_geometry(cl, tg_cookie);
2809 			rval = 0;
2810 		}
2811 	}
2812 #endif
2813 	if (rval) {
2814 		mutex_exit(CMLB_MUTEX(cl));
2815 		return (rval);
2816 	}
2817 
2818 #if defined(__i386) || defined(__amd64)
2819 	if (cl->cl_solaris_size == 0) {
2820 		mutex_exit(CMLB_MUTEX(cl));
2821 		return (EIO);
2822 	}
2823 #endif
2824 
2825 	/*
2826 	 * Make a local copy of the soft state geometry to avoid some potential
2827 	 * race conditions associated with holding the mutex and updating the
2828 	 * write_reinstruct value
2829 	 */
2830 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
2831 	bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
2832 
2833 	if (tmp_geom->dkg_write_reinstruct == 0) {
2834 		tmp_geom->dkg_write_reinstruct =
2835 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
2836 		    cmlb_rot_delay) / (int)60000);
2837 	}
2838 	mutex_exit(CMLB_MUTEX(cl));
2839 
2840 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
2841 	    flag);
2842 	if (rval != 0) {
2843 		rval = EFAULT;
2844 	}
2845 
2846 	kmem_free(tmp_geom, sizeof (struct dk_geom));
2847 	return (rval);
2848 
2849 }
2850 
2851 
2852 /*
2853  *    Function: cmlb_dkio_set_geometry
2854  *
2855  * Description: This routine is the driver entry point for handling user
2856  *		requests to set the device geometry (DKIOCSGEOM). The actual
2857  *		device geometry is not updated, just the driver "notion" of it.
2858  *
2859  *   Arguments:
2860  *	arg		pointer to user provided dk_geom structure used to set
2861  *			the controller's notion of the current geometry.
2862  *
2863  *	flag 		this argument is a pass through to ddi_copyxxx()
2864  *			directly from the mode argument of ioctl().
2865  *
2866  *	tg_cookie	cookie from target driver to be passed back to target
2867  *			driver when we call back to it through tg_ops.
2868  *
2869  * Return Code: 0
2870  *		EFAULT
2871  *		ENXIO
2872  *		EIO
2873  */
2874 static int
2875 cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
2876 {
2877 	struct dk_geom	*tmp_geom;
2878 	struct dk_map	*lp;
2879 	int		rval = 0;
2880 	int		i;
2881 
2882 
2883 #if defined(__i386) || defined(__amd64)
2884 	if (cl->cl_solaris_size == 0) {
2885 		return (EIO);
2886 	}
2887 #endif
2888 	/*
2889 	 * We need to copy the user specified geometry into local
2890 	 * storage and then update the softstate. We don't want to hold
2891 	 * the mutex and copyin directly from the user to the soft state
2892 	 */
2893 	tmp_geom = (struct dk_geom *)
2894 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
2895 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
2896 	if (rval != 0) {
2897 		kmem_free(tmp_geom, sizeof (struct dk_geom));
2898 		return (EFAULT);
2899 	}
2900 
2901 	mutex_enter(CMLB_MUTEX(cl));
2902 	bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
2903 	for (i = 0; i < NDKMAP; i++) {
2904 		lp  = &cl->cl_map[i];
2905 		cl->cl_offset[i] =
2906 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
2907 #if defined(__i386) || defined(__amd64)
2908 		cl->cl_offset[i] += cl->cl_solaris_offset;
2909 #endif
2910 	}
2911 	cl->cl_f_geometry_is_valid = FALSE;
2912 	mutex_exit(CMLB_MUTEX(cl));
2913 	kmem_free(tmp_geom, sizeof (struct dk_geom));
2914 
2915 	return (rval);
2916 }
2917 
2918 /*
2919  *    Function: cmlb_dkio_get_partition
2920  *
2921  * Description: This routine is the driver entry point for handling user
2922  *		requests to get the partition table (DKIOCGAPART).
2923  *
2924  *   Arguments:
2925  *	arg		pointer to user provided dk_allmap structure specifying
2926  *			the controller's notion of the current partition table.
2927  *
2928  *	flag		this argument is a pass through to ddi_copyxxx()
2929  *			directly from the mode argument of ioctl().
2930  *
2931  *	tg_cookie	cookie from target driver to be passed back to target
2932  *			driver when we call back to it through tg_ops.
2933  *
2934  * Return Code: 0
2935  *		EFAULT
2936  *		ENXIO
2937  *		EIO
2938  */
2939 static int
2940 cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
2941     void *tg_cookie)
2942 {
2943 	int		rval = 0;
2944 	int		size;
2945 
2946 	/*
2947 	 * Make sure the geometry is valid before getting the partition
2948 	 * information.
2949 	 */
2950 	mutex_enter(CMLB_MUTEX(cl));
2951 	if ((rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie)) != 0) {
2952 		mutex_exit(CMLB_MUTEX(cl));
2953 		return (rval);
2954 	}
2955 	mutex_exit(CMLB_MUTEX(cl));
2956 
2957 #if defined(__i386) || defined(__amd64)
2958 	if (cl->cl_solaris_size == 0) {
2959 		return (EIO);
2960 	}
2961 #endif
2962 
2963 #ifdef _MULTI_DATAMODEL
2964 	switch (ddi_model_convert_from(flag & FMODELS)) {
2965 	case DDI_MODEL_ILP32: {
2966 		struct dk_map32 dk_map32[NDKMAP];
2967 		int		i;
2968 
2969 		for (i = 0; i < NDKMAP; i++) {
2970 			dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
2971 			dk_map32[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
2972 		}
2973 		size = NDKMAP * sizeof (struct dk_map32);
2974 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
2975 		if (rval != 0) {
2976 			rval = EFAULT;
2977 		}
2978 		break;
2979 	}
2980 	case DDI_MODEL_NONE:
2981 		size = NDKMAP * sizeof (struct dk_map);
2982 		rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
2983 		if (rval != 0) {
2984 			rval = EFAULT;
2985 		}
2986 		break;
2987 	}
2988 #else /* ! _MULTI_DATAMODEL */
2989 	size = NDKMAP * sizeof (struct dk_map);
2990 	rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
2991 	if (rval != 0) {
2992 		rval = EFAULT;
2993 	}
2994 #endif /* _MULTI_DATAMODEL */
2995 	return (rval);
2996 }
2997 
2998 /*
2999  *    Function: cmlb_dkio_set_partition
3000  *
3001  * Description: This routine is the driver entry point for handling user
3002  *		requests to set the partition table (DKIOCSAPART). The actual
3003  *		device partition is not updated.
3004  *
3005  *   Arguments:
3006  *		arg  - pointer to user provided dk_allmap structure used to set
3007  *			the controller's notion of the partition table.
3008  *		flag - this argument is a pass through to ddi_copyxxx()
3009  *		       directly from the mode argument of ioctl().
3010  *
3011  * Return Code: 0
3012  *		EINVAL
3013  *		EFAULT
3014  *		ENXIO
3015  *		EIO
3016  */
3017 static int
3018 cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
3019 {
3020 	struct dk_map	dk_map[NDKMAP];
3021 	struct dk_map	*lp;
3022 	int		rval = 0;
3023 	int		size;
3024 	int		i;
3025 #if defined(_SUNOS_VTOC_16)
3026 	struct dkl_partition	*vp;
3027 #endif
3028 
3029 	/*
3030 	 * Set the map for all logical partitions.  We lock
3031 	 * the priority just to make sure an interrupt doesn't
3032 	 * come in while the map is half updated.
3033 	 */
3034 	_NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3035 	mutex_enter(CMLB_MUTEX(cl));
3036 
3037 	if (cl->cl_blockcount > DK_MAX_BLOCKS) {
3038 		mutex_exit(CMLB_MUTEX(cl));
3039 		return (ENOTSUP);
3040 	}
3041 	mutex_exit(CMLB_MUTEX(cl));
3042 	if (cl->cl_solaris_size == 0) {
3043 		return (EIO);
3044 	}
3045 
3046 #ifdef _MULTI_DATAMODEL
3047 	switch (ddi_model_convert_from(flag & FMODELS)) {
3048 	case DDI_MODEL_ILP32: {
3049 		struct dk_map32 dk_map32[NDKMAP];
3050 
3051 		size = NDKMAP * sizeof (struct dk_map32);
3052 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
3053 		if (rval != 0) {
3054 			return (EFAULT);
3055 		}
3056 		for (i = 0; i < NDKMAP; i++) {
3057 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
3058 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
3059 		}
3060 		break;
3061 	}
3062 	case DDI_MODEL_NONE:
3063 		size = NDKMAP * sizeof (struct dk_map);
3064 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
3065 		if (rval != 0) {
3066 			return (EFAULT);
3067 		}
3068 		break;
3069 	}
3070 #else /* ! _MULTI_DATAMODEL */
3071 	size = NDKMAP * sizeof (struct dk_map);
3072 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
3073 	if (rval != 0) {
3074 		return (EFAULT);
3075 	}
3076 #endif /* _MULTI_DATAMODEL */
3077 
3078 	mutex_enter(CMLB_MUTEX(cl));
3079 	/* Note: The size used in this bcopy is set based upon the data model */
3080 	bcopy(dk_map, cl->cl_map, size);
3081 #if defined(_SUNOS_VTOC_16)
3082 	vp = (struct dkl_partition *)&(cl->cl_vtoc);
3083 #endif	/* defined(_SUNOS_VTOC_16) */
3084 	for (i = 0; i < NDKMAP; i++) {
3085 		lp  = &cl->cl_map[i];
3086 		cl->cl_offset[i] =
3087 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3088 #if defined(_SUNOS_VTOC_16)
3089 		vp->p_start = cl->cl_offset[i];
3090 		vp->p_size = lp->dkl_nblk;
3091 		vp++;
3092 #endif	/* defined(_SUNOS_VTOC_16) */
3093 #if defined(__i386) || defined(__amd64)
3094 		cl->cl_offset[i] += cl->cl_solaris_offset;
3095 #endif
3096 	}
3097 	mutex_exit(CMLB_MUTEX(cl));
3098 	return (rval);
3099 }
3100 
3101 
3102 /*
3103  *    Function: cmlb_dkio_get_vtoc
3104  *
3105  * Description: This routine is the driver entry point for handling user
3106  *		requests to get the current volume table of contents
3107  *		(DKIOCGVTOC).
3108  *
3109  *   Arguments:
3110  *	arg		pointer to user provided vtoc structure specifying
3111  *			the current vtoc.
3112  *
3113  *	flag		this argument is a pass through to ddi_copyxxx()
3114  *			directly from the mode argument of ioctl().
3115  *
3116  *	tg_cookie	cookie from target driver to be passed back to target
3117  *			driver when we call back to it through tg_ops.
3118  *
3119  * Return Code: 0
3120  *		EFAULT
3121  *		ENXIO
3122  *		EIO
3123  */
3124 static int
3125 cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
3126 {
3127 #if defined(_SUNOS_VTOC_8)
3128 	struct vtoc	user_vtoc;
3129 #endif	/* defined(_SUNOS_VTOC_8) */
3130 	int		rval = 0;
3131 
3132 	mutex_enter(CMLB_MUTEX(cl));
3133 	rval = cmlb_validate_geometry(cl, 1, 0, tg_cookie);
3134 
3135 #if defined(_SUNOS_VTOC_8)
3136 	if (rval == EINVAL &&
3137 	    (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
3138 		/*
3139 		 * This is to return a default label even when we do not
3140 		 * really assume a default label for the device.
3141 		 * dad driver utilizes this.
3142 		 */
3143 		if (cl->cl_blockcount <= DK_MAX_BLOCKS) {
3144 			cmlb_setup_default_geometry(cl, tg_cookie);
3145 			rval = 0;
3146 		}
3147 	}
3148 #endif
3149 	if (rval) {
3150 		mutex_exit(CMLB_MUTEX(cl));
3151 		return (rval);
3152 	}
3153 
3154 #if defined(_SUNOS_VTOC_8)
3155 	cmlb_build_user_vtoc(cl, &user_vtoc);
3156 	mutex_exit(CMLB_MUTEX(cl));
3157 
3158 #ifdef _MULTI_DATAMODEL
3159 	switch (ddi_model_convert_from(flag & FMODELS)) {
3160 	case DDI_MODEL_ILP32: {
3161 		struct vtoc32 user_vtoc32;
3162 
3163 		vtoctovtoc32(user_vtoc, user_vtoc32);
3164 		if (ddi_copyout(&user_vtoc32, (void *)arg,
3165 		    sizeof (struct vtoc32), flag)) {
3166 			return (EFAULT);
3167 		}
3168 		break;
3169 	}
3170 
3171 	case DDI_MODEL_NONE:
3172 		if (ddi_copyout(&user_vtoc, (void *)arg,
3173 		    sizeof (struct vtoc), flag)) {
3174 			return (EFAULT);
3175 		}
3176 		break;
3177 	}
3178 #else /* ! _MULTI_DATAMODEL */
3179 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
3180 		return (EFAULT);
3181 	}
3182 #endif /* _MULTI_DATAMODEL */
3183 
3184 #elif defined(_SUNOS_VTOC_16)
3185 	mutex_exit(CMLB_MUTEX(cl));
3186 
3187 #ifdef _MULTI_DATAMODEL
3188 	/*
3189 	 * The cl_vtoc structure is a "struct dk_vtoc"  which is always
3190 	 * 32-bit to maintain compatibility with existing on-disk
3191 	 * structures.  Thus, we need to convert the structure when copying
3192 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
3193 	 * program.  If the target is a 32-bit program, then no conversion
3194 	 * is necessary.
3195 	 */
3196 	/* LINTED: logical expression always true: op "||" */
3197 	ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
3198 	switch (ddi_model_convert_from(flag & FMODELS)) {
3199 	case DDI_MODEL_ILP32:
3200 		if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
3201 		    sizeof (cl->cl_vtoc), flag)) {
3202 			return (EFAULT);
3203 		}
3204 		break;
3205 
3206 	case DDI_MODEL_NONE: {
3207 		struct vtoc user_vtoc;
3208 
3209 		vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
3210 		if (ddi_copyout(&user_vtoc, (void *)arg,
3211 		    sizeof (struct vtoc), flag)) {
3212 			return (EFAULT);
3213 		}
3214 		break;
3215 	}
3216 	}
3217 #else /* ! _MULTI_DATAMODEL */
3218 	if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
3219 	    flag)) {
3220 		return (EFAULT);
3221 	}
3222 #endif /* _MULTI_DATAMODEL */
3223 #else
3224 #error "No VTOC format defined."
3225 #endif
3226 
3227 	return (rval);
3228 }
3229 
3230 static int
3231 cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
3232 {
3233 	dk_efi_t	user_efi;
3234 	int		rval = 0;
3235 	void		*buffer;
3236 	diskaddr_t	tgt_lba;
3237 
3238 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
3239 		return (EFAULT);
3240 
3241 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
3242 
3243 	tgt_lba = user_efi.dki_lba;
3244 
3245 	mutex_enter(CMLB_MUTEX(cl));
3246 	if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
3247 	    (cl->cl_tgt_blocksize == 0)) {
3248 		mutex_exit(CMLB_MUTEX(cl));
3249 		return (EINVAL);
3250 	}
3251 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
3252 		tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
3253 		    cl->cl_sys_blocksize;
3254 	mutex_exit(CMLB_MUTEX(cl));
3255 
3256 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
3257 	rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
3258 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
3259 	    user_efi.dki_length, flag) != 0)
3260 		rval = EFAULT;
3261 
3262 	kmem_free(buffer, user_efi.dki_length);
3263 	return (rval);
3264 }
3265 
3266 #if defined(_SUNOS_VTOC_8)
3267 /*
3268  *    Function: cmlb_build_user_vtoc
3269  *
3270  * Description: This routine populates a pass by reference variable with the
3271  *		current volume table of contents.
3272  *
3273  *   Arguments: cl - driver soft state (unit) structure
3274  *		user_vtoc - pointer to vtoc structure to be populated
3275  */
3276 static void
3277 cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
3278 {
3279 	struct dk_map2		*lpart;
3280 	struct dk_map		*lmap;
3281 	struct partition	*vpart;
3282 	int			nblks;
3283 	int			i;
3284 
3285 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3286 
3287 	/*
3288 	 * Return vtoc structure fields in the provided VTOC area, addressed
3289 	 * by *vtoc.
3290 	 */
3291 	bzero(user_vtoc, sizeof (struct vtoc));
3292 	user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
3293 	user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
3294 	user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
3295 	user_vtoc->v_sanity	= VTOC_SANE;
3296 	user_vtoc->v_version	= cl->cl_vtoc.v_version;
3297 	bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
3298 	user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
3299 	user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
3300 
3301 	for (i = 0; i < 10; i++)
3302 		user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
3303 
3304 	/*
3305 	 * Convert partitioning information.
3306 	 *
3307 	 * Note the conversion from starting cylinder number
3308 	 * to starting sector number.
3309 	 */
3310 	lmap = cl->cl_map;
3311 	lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
3312 	vpart = user_vtoc->v_part;
3313 
3314 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
3315 
3316 	for (i = 0; i < V_NUMPAR; i++) {
3317 		vpart->p_tag	= lpart->p_tag;
3318 		vpart->p_flag	= lpart->p_flag;
3319 		vpart->p_start	= lmap->dkl_cylno * nblks;
3320 		vpart->p_size	= lmap->dkl_nblk;
3321 		lmap++;
3322 		lpart++;
3323 		vpart++;
3324 
3325 		/* (4364927) */
3326 		user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
3327 	}
3328 
3329 	bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
3330 }
3331 #endif
3332 
3333 static int
3334 cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3335     void *tg_cookie)
3336 {
3337 	struct partition64	p64;
3338 	int			rval = 0;
3339 	uint_t			nparts;
3340 	efi_gpe_t		*partitions;
3341 	efi_gpt_t		*buffer;
3342 	diskaddr_t		gpe_lba;
3343 
3344 	if (ddi_copyin((const void *)arg, &p64,
3345 	    sizeof (struct partition64), flag)) {
3346 		return (EFAULT);
3347 	}
3348 
3349 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
3350 	rval = DK_TG_READ(cl, buffer, 1, DEV_BSIZE, tg_cookie);
3351 	if (rval != 0)
3352 		goto done_error;
3353 
3354 	cmlb_swap_efi_gpt(buffer);
3355 
3356 	if ((rval = cmlb_validate_efi(buffer)) != 0)
3357 		goto done_error;
3358 
3359 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
3360 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
3361 	if (p64.p_partno > nparts) {
3362 		/* couldn't find it */
3363 		rval = ESRCH;
3364 		goto done_error;
3365 	}
3366 	/*
3367 	 * if we're dealing with a partition that's out of the normal
3368 	 * 16K block, adjust accordingly
3369 	 */
3370 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
3371 	rval = DK_TG_READ(cl, buffer, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
3372 
3373 	if (rval) {
3374 		goto done_error;
3375 	}
3376 	partitions = (efi_gpe_t *)buffer;
3377 
3378 	cmlb_swap_efi_gpe(nparts, partitions);
3379 
3380 	partitions += p64.p_partno;
3381 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
3382 	    sizeof (struct uuid));
3383 	p64.p_start = partitions->efi_gpe_StartingLBA;
3384 	p64.p_size = partitions->efi_gpe_EndingLBA -
3385 	    p64.p_start + 1;
3386 
3387 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
3388 		rval = EFAULT;
3389 
3390 done_error:
3391 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
3392 	return (rval);
3393 }
3394 
3395 
3396 /*
3397  *    Function: cmlb_dkio_set_vtoc
3398  *
3399  * Description: This routine is the driver entry point for handling user
3400  *		requests to set the current volume table of contents
3401  *		(DKIOCSVTOC).
3402  *
3403  *   Arguments:
3404  *	dev		the device number
3405  *	arg		pointer to user provided vtoc structure used to set the
3406  *			current vtoc.
3407  *
3408  *	flag		this argument is a pass through to ddi_copyxxx()
3409  *			directly from the mode argument of ioctl().
3410  *
3411  *	tg_cookie	cookie from target driver to be passed back to target
3412  *			driver when we call back to it through tg_ops.
3413  *
3414  * Return Code: 0
3415  *		EFAULT
3416  *		ENXIO
3417  *		EINVAL
3418  *		ENOTSUP
3419  */
3420 static int
3421 cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3422     void *tg_cookie)
3423 {
3424 	struct vtoc	user_vtoc;
3425 	int		rval = 0;
3426 
3427 #ifdef _MULTI_DATAMODEL
3428 	switch (ddi_model_convert_from(flag & FMODELS)) {
3429 	case DDI_MODEL_ILP32: {
3430 		struct vtoc32 user_vtoc32;
3431 
3432 		if (ddi_copyin((const void *)arg, &user_vtoc32,
3433 		    sizeof (struct vtoc32), flag)) {
3434 			return (EFAULT);
3435 		}
3436 		vtoc32tovtoc(user_vtoc32, user_vtoc);
3437 		break;
3438 	}
3439 
3440 	case DDI_MODEL_NONE:
3441 		if (ddi_copyin((const void *)arg, &user_vtoc,
3442 		    sizeof (struct vtoc), flag)) {
3443 			return (EFAULT);
3444 		}
3445 		break;
3446 	}
3447 #else /* ! _MULTI_DATAMODEL */
3448 	if (ddi_copyin((const void *)arg, &user_vtoc,
3449 	    sizeof (struct vtoc), flag)) {
3450 		return (EFAULT);
3451 	}
3452 #endif /* _MULTI_DATAMODEL */
3453 
3454 	mutex_enter(CMLB_MUTEX(cl));
3455 	if (cl->cl_blockcount > DK_MAX_BLOCKS) {
3456 		mutex_exit(CMLB_MUTEX(cl));
3457 		return (ENOTSUP);
3458 	}
3459 
3460 #if defined(__i386) || defined(__amd64)
3461 	if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
3462 		mutex_exit(CMLB_MUTEX(cl));
3463 		return (EINVAL);
3464 	}
3465 #endif
3466 
3467 	if (cl->cl_g.dkg_ncyl == 0) {
3468 		mutex_exit(CMLB_MUTEX(cl));
3469 		return (EINVAL);
3470 	}
3471 
3472 	mutex_exit(CMLB_MUTEX(cl));
3473 	cmlb_clear_efi(cl, tg_cookie);
3474 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
3475 	ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
3476 	(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h",
3477 	    S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3478 	    cl->cl_node_type, NULL);
3479 	(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "h,raw",
3480 	    S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3481 	    cl->cl_node_type, NULL);
3482 	mutex_enter(CMLB_MUTEX(cl));
3483 
3484 	if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
3485 		if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
3486 			if (cmlb_validate_geometry(cl, 1, 0, tg_cookie) != 0) {
3487 				cmlb_dbg(CMLB_ERROR, cl,
3488 				    "cmlb_dkio_set_vtoc: "
3489 				    "Failed validate geometry\n");
3490 			}
3491 		}
3492 	}
3493 	mutex_exit(CMLB_MUTEX(cl));
3494 	return (rval);
3495 }
3496 
3497 
3498 /*
3499  *    Function: cmlb_build_label_vtoc
3500  *
3501  * Description: This routine updates the driver soft state current volume table
3502  *		of contents based on a user specified vtoc.
3503  *
3504  *   Arguments: cl - driver soft state (unit) structure
3505  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
3506  *			    to update the driver soft state.
3507  *
3508  * Return Code: 0
3509  *		EINVAL
3510  */
3511 static int
3512 cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
3513 {
3514 	struct dk_map		*lmap;
3515 	struct partition	*vpart;
3516 	int			nblks;
3517 #if defined(_SUNOS_VTOC_8)
3518 	int			ncyl;
3519 	struct dk_map2		*lpart;
3520 #endif	/* defined(_SUNOS_VTOC_8) */
3521 	int			i;
3522 
3523 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3524 
3525 	/* Sanity-check the vtoc */
3526 	if (user_vtoc->v_sanity != VTOC_SANE ||
3527 	    user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
3528 	    user_vtoc->v_nparts != V_NUMPAR) {
3529 		cmlb_dbg(CMLB_INFO,  cl,
3530 		    "cmlb_build_label_vtoc: vtoc not valid\n");
3531 		return (EINVAL);
3532 	}
3533 
3534 	nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
3535 	if (nblks == 0) {
3536 		cmlb_dbg(CMLB_INFO,  cl,
3537 		    "cmlb_build_label_vtoc: geom nblks is 0\n");
3538 		return (EINVAL);
3539 	}
3540 
3541 #if defined(_SUNOS_VTOC_8)
3542 	vpart = user_vtoc->v_part;
3543 	for (i = 0; i < V_NUMPAR; i++) {
3544 		if ((vpart->p_start % nblks) != 0) {
3545 			cmlb_dbg(CMLB_INFO,  cl,
3546 			    "cmlb_build_label_vtoc: p_start not multiply of"
3547 			    "nblks part %d p_start %d nblks %d\n", i,
3548 			    vpart->p_start, nblks);
3549 			return (EINVAL);
3550 		}
3551 		ncyl = vpart->p_start / nblks;
3552 		ncyl += vpart->p_size / nblks;
3553 		if ((vpart->p_size % nblks) != 0) {
3554 			ncyl++;
3555 		}
3556 		if (ncyl > (int)cl->cl_g.dkg_ncyl) {
3557 			cmlb_dbg(CMLB_INFO,  cl,
3558 			    "cmlb_build_label_vtoc: ncyl %d  > dkg_ncyl %d"
3559 			    "p_size %ld p_start %ld nblks %d  part number %d"
3560 			    "tag %d\n",
3561 			    ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
3562 			    vpart->p_start, nblks,
3563 			    i, vpart->p_tag);
3564 
3565 			return (EINVAL);
3566 		}
3567 		vpart++;
3568 	}
3569 #endif	/* defined(_SUNOS_VTOC_8) */
3570 
3571 	/* Put appropriate vtoc structure fields into the disk label */
3572 #if defined(_SUNOS_VTOC_16)
3573 	/*
3574 	 * The vtoc is always a 32bit data structure to maintain the
3575 	 * on-disk format. Convert "in place" instead of doing bcopy.
3576 	 */
3577 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
3578 
3579 	/*
3580 	 * in the 16-slice vtoc, starting sectors are expressed in
3581 	 * numbers *relative* to the start of the Solaris fdisk partition.
3582 	 */
3583 	lmap = cl->cl_map;
3584 	vpart = user_vtoc->v_part;
3585 
3586 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
3587 		lmap->dkl_cylno = vpart->p_start / nblks;
3588 		lmap->dkl_nblk = vpart->p_size;
3589 	}
3590 
3591 #elif defined(_SUNOS_VTOC_8)
3592 
3593 	cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
3594 	cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
3595 	cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
3596 
3597 	cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
3598 	cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
3599 
3600 	bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
3601 
3602 	cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
3603 
3604 	for (i = 0; i < 10; i++)
3605 		cl->cl_vtoc.v_reserved[i] =  user_vtoc->v_reserved[i];
3606 
3607 	/*
3608 	 * Note the conversion from starting sector number
3609 	 * to starting cylinder number.
3610 	 * Return error if division results in a remainder.
3611 	 */
3612 	lmap = cl->cl_map;
3613 	lpart = cl->cl_vtoc.v_part;
3614 	vpart = user_vtoc->v_part;
3615 
3616 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
3617 		lpart->p_tag  = vpart->p_tag;
3618 		lpart->p_flag = vpart->p_flag;
3619 		lmap->dkl_cylno = vpart->p_start / nblks;
3620 		lmap->dkl_nblk = vpart->p_size;
3621 
3622 		lmap++;
3623 		lpart++;
3624 		vpart++;
3625 
3626 		/* (4387723) */
3627 #ifdef _LP64
3628 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
3629 			cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
3630 		} else {
3631 			cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
3632 		}
3633 #else
3634 		cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
3635 #endif
3636 	}
3637 
3638 	bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
3639 #else
3640 #error "No VTOC format defined."
3641 #endif
3642 	return (0);
3643 }
3644 
3645 /*
3646  *    Function: cmlb_clear_efi
3647  *
3648  * Description: This routine clears all EFI labels.
3649  *
3650  *   Arguments:
3651  *	cl		 driver soft state (unit) structure
3652  *
3653  *	tg_cookie	cookie from target driver to be passed back to target
3654  *			driver when we call back to it through tg_ops.
3655  * Return Code: void
3656  */
3657 static void
3658 cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
3659 {
3660 	efi_gpt_t	*gpt;
3661 	diskaddr_t	cap;
3662 	int		rval;
3663 
3664 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
3665 
3666 	mutex_enter(CMLB_MUTEX(cl));
3667 	cl->cl_reserved = -1;
3668 	mutex_exit(CMLB_MUTEX(cl));
3669 
3670 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
3671 
3672 	if (DK_TG_READ(cl, gpt, 1, DEV_BSIZE, tg_cookie) != 0) {
3673 		goto done;
3674 	}
3675 
3676 	cmlb_swap_efi_gpt(gpt);
3677 	rval = cmlb_validate_efi(gpt);
3678 	if (rval == 0) {
3679 		/* clear primary */
3680 		bzero(gpt, sizeof (efi_gpt_t));
3681 		if (rval = DK_TG_WRITE(cl, gpt, 1, EFI_LABEL_SIZE, tg_cookie)) {
3682 			cmlb_dbg(CMLB_INFO,  cl,
3683 			    "cmlb_clear_efi: clear primary label failed\n");
3684 		}
3685 	}
3686 	/* the backup */
3687 	rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
3688 	if (rval) {
3689 		goto done;
3690 	}
3691 
3692 	if ((rval = DK_TG_READ(cl, gpt, cap - 1, EFI_LABEL_SIZE, tg_cookie))
3693 	    != 0) {
3694 		goto done;
3695 	}
3696 	cmlb_swap_efi_gpt(gpt);
3697 	rval = cmlb_validate_efi(gpt);
3698 	if (rval == 0) {
3699 		/* clear backup */
3700 		cmlb_dbg(CMLB_TRACE,  cl,
3701 		    "cmlb_clear_efi clear backup@%lu\n", cap - 1);
3702 		bzero(gpt, sizeof (efi_gpt_t));
3703 		if ((rval = DK_TG_WRITE(cl,  gpt, cap - 1, EFI_LABEL_SIZE,
3704 		    tg_cookie))) {
3705 			cmlb_dbg(CMLB_INFO,  cl,
3706 			    "cmlb_clear_efi: clear backup label failed\n");
3707 		}
3708 	} else {
3709 		/*
3710 		 * Refer to comments related to off-by-1 at the
3711 		 * header of this file
3712 		 */
3713 		if ((rval = DK_TG_READ(cl, gpt, cap - 2,
3714 		    EFI_LABEL_SIZE, tg_cookie)) != 0) {
3715 			goto done;
3716 		}
3717 		cmlb_swap_efi_gpt(gpt);
3718 		rval = cmlb_validate_efi(gpt);
3719 		if (rval == 0) {
3720 			/* clear legacy backup EFI label */
3721 			cmlb_dbg(CMLB_TRACE,  cl,
3722 			    "cmlb_clear_efi clear legacy backup@%lu\n",
3723 			    cap - 2);
3724 			bzero(gpt, sizeof (efi_gpt_t));
3725 			if ((rval = DK_TG_WRITE(cl,  gpt, cap - 2,
3726 			    EFI_LABEL_SIZE, tg_cookie))) {
3727 				cmlb_dbg(CMLB_INFO,  cl,
3728 				"cmlb_clear_efi: clear legacy backup label "
3729 				"failed\n");
3730 			}
3731 		}
3732 	}
3733 
3734 done:
3735 	kmem_free(gpt, sizeof (efi_gpt_t));
3736 }
3737 
3738 /*
3739  *    Function: cmlb_set_vtoc
3740  *
3741  * Description: This routine writes data to the appropriate positions
3742  *
3743  *   Arguments:
3744  *	cl		driver soft state (unit) structure
3745  *
3746  *	dkl		the data to be written
3747  *
3748  *	tg_cookie	cookie from target driver to be passed back to target
3749  *			driver when we call back to it through tg_ops.
3750  *
3751  * Return: void
3752  */
3753 static int
3754 cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
3755 {
3756 	uint_t	label_addr;
3757 	int	sec;
3758 	int	blk;
3759 	int	head;
3760 	int	cyl;
3761 	int	rval;
3762 
3763 #if defined(__i386) || defined(__amd64)
3764 	label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
3765 #else
3766 	/* Write the primary label at block 0 of the solaris partition. */
3767 	label_addr = 0;
3768 #endif
3769 
3770 	rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
3771 	    tg_cookie);
3772 
3773 	if (rval != 0) {
3774 		return (rval);
3775 	}
3776 
3777 	/*
3778 	 * Calculate where the backup labels go.  They are always on
3779 	 * the last alternate cylinder, but some older drives put them
3780 	 * on head 2 instead of the last head.	They are always on the
3781 	 * first 5 odd sectors of the appropriate track.
3782 	 *
3783 	 * We have no choice at this point, but to believe that the
3784 	 * disk label is valid.	 Use the geometry of the disk
3785 	 * as described in the label.
3786 	 */
3787 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
3788 	head = dkl->dkl_nhead - 1;
3789 
3790 	/*
3791 	 * Write and verify the backup labels. Make sure we don't try to
3792 	 * write past the last cylinder.
3793 	 */
3794 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
3795 		blk = (daddr_t)(
3796 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
3797 		    (head * dkl->dkl_nsect) + sec);
3798 #if defined(__i386) || defined(__amd64)
3799 		blk += cl->cl_solaris_offset;
3800 #endif
3801 		rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
3802 		    tg_cookie);
3803 		cmlb_dbg(CMLB_INFO,  cl,
3804 		"cmlb_set_vtoc: wrote backup label %d\n", blk);
3805 		if (rval != 0) {
3806 			goto exit;
3807 		}
3808 	}
3809 exit:
3810 	return (rval);
3811 }
3812 
3813 /*
3814  *    Function: cmlb_clear_vtoc
3815  *
3816  * Description: This routine clears out the VTOC labels.
3817  *
3818  *   Arguments:
3819  *	cl		driver soft state (unit) structure
3820  *
3821  *	tg_cookie	cookie from target driver to be passed back to target
3822  *			driver when we call back to it through tg_ops.
3823  *
3824  * Return: void
3825  */
3826 static void
3827 cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
3828 {
3829 	struct dk_label		*dkl;
3830 
3831 	mutex_exit(CMLB_MUTEX(cl));
3832 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
3833 	mutex_enter(CMLB_MUTEX(cl));
3834 	/*
3835 	 * cmlb_set_vtoc uses these fields in order to figure out
3836 	 * where to overwrite the backup labels
3837 	 */
3838 	dkl->dkl_apc    = cl->cl_g.dkg_apc;
3839 	dkl->dkl_ncyl   = cl->cl_g.dkg_ncyl;
3840 	dkl->dkl_acyl   = cl->cl_g.dkg_acyl;
3841 	dkl->dkl_nhead  = cl->cl_g.dkg_nhead;
3842 	dkl->dkl_nsect  = cl->cl_g.dkg_nsect;
3843 	mutex_exit(CMLB_MUTEX(cl));
3844 	(void) cmlb_set_vtoc(cl, dkl, tg_cookie);
3845 	kmem_free(dkl, sizeof (struct dk_label));
3846 
3847 	mutex_enter(CMLB_MUTEX(cl));
3848 }
3849 
3850 /*
3851  *    Function: cmlb_write_label
3852  *
3853  * Description: This routine will validate and write the driver soft state vtoc
3854  *		contents to the device.
3855  *
3856  *   Arguments:
3857  *	cl		cmlb handle
3858  *
3859  *	tg_cookie	cookie from target driver to be passed back to target
3860  *			driver when we call back to it through tg_ops.
3861  *
3862  *
3863  * Return Code: the code returned by cmlb_send_scsi_cmd()
3864  *		0
3865  *		EINVAL
3866  *		ENXIO
3867  *		ENOMEM
3868  */
3869 static int
3870 cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
3871 {
3872 	struct dk_label	*dkl;
3873 	short		sum;
3874 	short		*sp;
3875 	int		i;
3876 	int		rval;
3877 
3878 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3879 	mutex_exit(CMLB_MUTEX(cl));
3880 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
3881 	mutex_enter(CMLB_MUTEX(cl));
3882 
3883 	bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
3884 	dkl->dkl_rpm	= cl->cl_g.dkg_rpm;
3885 	dkl->dkl_pcyl	= cl->cl_g.dkg_pcyl;
3886 	dkl->dkl_apc	= cl->cl_g.dkg_apc;
3887 	dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
3888 	dkl->dkl_ncyl	= cl->cl_g.dkg_ncyl;
3889 	dkl->dkl_acyl	= cl->cl_g.dkg_acyl;
3890 	dkl->dkl_nhead	= cl->cl_g.dkg_nhead;
3891 	dkl->dkl_nsect	= cl->cl_g.dkg_nsect;
3892 
3893 #if defined(_SUNOS_VTOC_8)
3894 	dkl->dkl_obs1	= cl->cl_g.dkg_obs1;
3895 	dkl->dkl_obs2	= cl->cl_g.dkg_obs2;
3896 	dkl->dkl_obs3	= cl->cl_g.dkg_obs3;
3897 	for (i = 0; i < NDKMAP; i++) {
3898 		dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3899 		dkl->dkl_map[i].dkl_nblk  = cl->cl_map[i].dkl_nblk;
3900 	}
3901 	bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
3902 #elif defined(_SUNOS_VTOC_16)
3903 	dkl->dkl_skew	= cl->cl_dkg_skew;
3904 #else
3905 #error "No VTOC format defined."
3906 #endif
3907 
3908 	dkl->dkl_magic			= DKL_MAGIC;
3909 	dkl->dkl_write_reinstruct	= cl->cl_g.dkg_write_reinstruct;
3910 	dkl->dkl_read_reinstruct	= cl->cl_g.dkg_read_reinstruct;
3911 
3912 	/* Construct checksum for the new disk label */
3913 	sum = 0;
3914 	sp = (short *)dkl;
3915 	i = sizeof (struct dk_label) / sizeof (short);
3916 	while (i--) {
3917 		sum ^= *sp++;
3918 	}
3919 	dkl->dkl_cksum = sum;
3920 
3921 	mutex_exit(CMLB_MUTEX(cl));
3922 
3923 	rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
3924 exit:
3925 	kmem_free(dkl, sizeof (struct dk_label));
3926 	mutex_enter(CMLB_MUTEX(cl));
3927 	return (rval);
3928 }
3929 
3930 static int
3931 cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
3932     void *tg_cookie)
3933 {
3934 	dk_efi_t	user_efi;
3935 	int		rval = 0;
3936 	void		*buffer;
3937 	diskaddr_t	tgt_lba;
3938 
3939 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
3940 		return (EFAULT);
3941 
3942 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
3943 
3944 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
3945 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
3946 		rval = EFAULT;
3947 	} else {
3948 		/*
3949 		 * let's clear the vtoc labels and clear the softstate
3950 		 * vtoc.
3951 		 */
3952 		mutex_enter(CMLB_MUTEX(cl));
3953 		if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
3954 			cmlb_dbg(CMLB_TRACE,  cl,
3955 			    "cmlb_dkio_set_efi: CLEAR VTOC\n");
3956 			if (cl->cl_vtoc_label_is_from_media)
3957 				cmlb_clear_vtoc(cl, tg_cookie);
3958 			bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3959 			mutex_exit(CMLB_MUTEX(cl));
3960 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
3961 			ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
3962 			(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd",
3963 			    S_IFBLK,
3964 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3965 			    cl->cl_node_type, NULL);
3966 			(void) ddi_create_minor_node(CMLB_DEVINFO(cl), "wd,raw",
3967 			    S_IFCHR,
3968 			    (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
3969 			    cl->cl_node_type, NULL);
3970 		} else
3971 			mutex_exit(CMLB_MUTEX(cl));
3972 
3973 		tgt_lba = user_efi.dki_lba;
3974 
3975 		mutex_enter(CMLB_MUTEX(cl));
3976 		if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
3977 		    (cl->cl_tgt_blocksize == 0)) {
3978 			kmem_free(buffer, user_efi.dki_length);
3979 			mutex_exit(CMLB_MUTEX(cl));
3980 			return (EINVAL);
3981 		}
3982 		if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
3983 			tgt_lba = tgt_lba *
3984 			    cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
3985 
3986 		mutex_exit(CMLB_MUTEX(cl));
3987 		rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
3988 		    tg_cookie);
3989 
3990 		if (rval == 0) {
3991 			mutex_enter(CMLB_MUTEX(cl));
3992 			cl->cl_f_geometry_is_valid = FALSE;
3993 			mutex_exit(CMLB_MUTEX(cl));
3994 		}
3995 	}
3996 	kmem_free(buffer, user_efi.dki_length);
3997 	return (rval);
3998 }
3999 
4000 /*
4001  *    Function: cmlb_dkio_get_mboot
4002  *
4003  * Description: This routine is the driver entry point for handling user
4004  *		requests to get the current device mboot (DKIOCGMBOOT)
4005  *
4006  *   Arguments:
4007  *	arg		pointer to user provided mboot structure specifying
4008  *			the current mboot.
4009  *
4010  *	flag		this argument is a pass through to ddi_copyxxx()
4011  *			directly from the mode argument of ioctl().
4012  *
4013  *	tg_cookie	cookie from target driver to be passed back to target
4014  *			driver when we call back to it through tg_ops.
4015  *
4016  * Return Code: 0
4017  *		EINVAL
4018  *		EFAULT
4019  *		ENXIO
4020  */
4021 static int
4022 cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
4023 {
4024 	struct mboot	*mboot;
4025 	int		rval;
4026 	size_t		buffer_size;
4027 
4028 
4029 #if defined(_SUNOS_VTOC_8)
4030 	if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
4031 #elif defined(_SUNOS_VTOC_16)
4032 	if (arg == NULL) {
4033 #endif
4034 		return (EINVAL);
4035 	}
4036 
4037 	/*
4038 	 * Read the mboot block, located at absolute block 0 on the target.
4039 	 */
4040 	buffer_size = sizeof (struct mboot);
4041 
4042 	cmlb_dbg(CMLB_TRACE,  cl,
4043 	    "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
4044 
4045 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
4046 	if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
4047 		if (ddi_copyout(mboot, (void *)arg,
4048 		    sizeof (struct mboot), flag) != 0) {
4049 			rval = EFAULT;
4050 		}
4051 	}
4052 	kmem_free(mboot, buffer_size);
4053 	return (rval);
4054 }
4055 
4056 
4057 /*
4058  *    Function: cmlb_dkio_set_mboot
4059  *
4060  * Description: This routine is the driver entry point for handling user
4061  *		requests to validate and set the device master boot
4062  *		(DKIOCSMBOOT).
4063  *
4064  *   Arguments:
4065  *	arg		pointer to user provided mboot structure used to set the
4066  *			master boot.
4067  *
4068  *	flag		this argument is a pass through to ddi_copyxxx()
4069  *			directly from the mode argument of ioctl().
4070  *
4071  *	tg_cookie	cookie from target driver to be passed back to target
4072  *			driver when we call back to it through tg_ops.
4073  *
4074  * Return Code: 0
4075  *		EINVAL
4076  *		EFAULT
4077  *		ENXIO
4078  */
4079 static int
4080 cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
4081 {
4082 	struct mboot	*mboot = NULL;
4083 	int		rval;
4084 	ushort_t	magic;
4085 
4086 
4087 	ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4088 
4089 #if defined(_SUNOS_VTOC_8)
4090 	if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
4091 		return (EINVAL);
4092 	}
4093 #endif
4094 
4095 	if (arg == NULL) {
4096 		return (EINVAL);
4097 	}
4098 
4099 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
4100 
4101 	if (ddi_copyin((const void *)arg, mboot,
4102 	    sizeof (struct mboot), flag) != 0) {
4103 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
4104 		return (EFAULT);
4105 	}
4106 
4107 	/* Is this really a master boot record? */
4108 	magic = LE_16(mboot->signature);
4109 	if (magic != MBB_MAGIC) {
4110 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
4111 		return (EINVAL);
4112 	}
4113 
4114 	rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
4115 
4116 	mutex_enter(CMLB_MUTEX(cl));
4117 #if defined(__i386) || defined(__amd64)
4118 	if (rval == 0) {
4119 		/*
4120 		 * mboot has been written successfully.
4121 		 * update the fdisk and vtoc tables in memory
4122 		 */
4123 		rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
4124 		if ((cl->cl_f_geometry_is_valid == FALSE) || (rval != 0)) {
4125 			mutex_exit(CMLB_MUTEX(cl));
4126 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
4127 			return (rval);
4128 		}
4129 	}
4130 
4131 #ifdef __lock_lint
4132 	cmlb_setup_default_geometry(cl, tg_cookie);
4133 #endif
4134 
4135 #else
4136 	if (rval == 0) {
4137 		/*
4138 		 * mboot has been written successfully.
4139 		 * set up the default geometry and VTOC
4140 		 */
4141 		if (cl->cl_blockcount <= DK_MAX_BLOCKS)
4142 			cmlb_setup_default_geometry(cl, tg_cookie);
4143 	}
4144 #endif
4145 	mutex_exit(CMLB_MUTEX(cl));
4146 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
4147 	return (rval);
4148 }
4149 
4150 
4151 /*
4152  *    Function: cmlb_setup_default_geometry
4153  *
4154  * Description: This local utility routine sets the default geometry as part of
4155  *		setting the device mboot.
4156  *
4157  *   Arguments:
4158  *	cl		driver soft state (unit) structure
4159  *
4160  *	tg_cookie	cookie from target driver to be passed back to target
4161  *			driver when we call back to it through tg_ops.
4162  *
4163  *
4164  * Note: This may be redundant with cmlb_build_default_label.
4165  */
4166 static void
4167 cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
4168 {
4169 	struct cmlb_geom	pgeom;
4170 	struct cmlb_geom	*pgeomp = &pgeom;
4171 	int			ret;
4172 	int			geom_base_cap = 1;
4173 
4174 
4175 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4176 
4177 	/* zero out the soft state geometry and partition table. */
4178 	bzero(&cl->cl_g, sizeof (struct dk_geom));
4179 	bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4180 	bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
4181 
4182 	/*
4183 	 * For the rpm, we use the minimum for the disk.
4184 	 * For the head, cyl and number of sector per track,
4185 	 * if the capacity <= 1GB, head = 64, sect = 32.
4186 	 * else head = 255, sect 63
4187 	 * Note: the capacity should be equal to C*H*S values.
4188 	 * This will cause some truncation of size due to
4189 	 * round off errors. For CD-ROMs, this truncation can
4190 	 * have adverse side effects, so returning ncyl and
4191 	 * nhead as 1. The nsect will overflow for most of
4192 	 * CD-ROMs as nsect is of type ushort.
4193 	 */
4194 	if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
4195 		/*
4196 		 * newfs currently can not handle 255 ntracks for SPARC
4197 		 * so get the geometry from target driver instead of coming up
4198 		 * with one based on capacity.
4199 		 */
4200 		mutex_exit(CMLB_MUTEX(cl));
4201 		ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
4202 		mutex_enter(CMLB_MUTEX(cl));
4203 
4204 		if (ret  == 0) {
4205 			geom_base_cap = 0;
4206 		} else {
4207 			cmlb_dbg(CMLB_ERROR,  cl,
4208 			    "cmlb_setup_default_geometry: "
4209 			    "tg_getphygeom failed %d\n", ret);
4210 
4211 			/* do default setting, geometry based on capacity */
4212 		}
4213 	}
4214 
4215 	if (geom_base_cap) {
4216 		if (ISCD(cl)) {
4217 			cl->cl_g.dkg_ncyl = 1;
4218 			cl->cl_g.dkg_nhead = 1;
4219 			cl->cl_g.dkg_nsect = cl->cl_blockcount;
4220 		} else if (cl->cl_blockcount <= 0x1000) {
4221 			/* Needed for unlabeled SCSI floppies. */
4222 			cl->cl_g.dkg_nhead = 2;
4223 			cl->cl_g.dkg_ncyl = 80;
4224 			cl->cl_g.dkg_pcyl = 80;
4225 			cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
4226 		} else if (cl->cl_blockcount <= 0x200000) {
4227 			cl->cl_g.dkg_nhead = 64;
4228 			cl->cl_g.dkg_nsect = 32;
4229 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
4230 		} else {
4231 			cl->cl_g.dkg_nhead = 255;
4232 			cl->cl_g.dkg_nsect = 63;
4233 			cl->cl_g.dkg_ncyl = cl->cl_blockcount / (255 * 63);
4234 		}
4235 
4236 		cl->cl_g.dkg_acyl = 0;
4237 		cl->cl_g.dkg_bcyl = 0;
4238 		cl->cl_g.dkg_intrlv = 1;
4239 		cl->cl_g.dkg_rpm = 200;
4240 		if (cl->cl_g.dkg_pcyl == 0)
4241 			cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
4242 			    cl->cl_g.dkg_acyl;
4243 	} else {
4244 		cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
4245 		cl->cl_g.dkg_acyl = pgeomp->g_acyl;
4246 		cl->cl_g.dkg_nhead = pgeomp->g_nhead;
4247 		cl->cl_g.dkg_nsect = pgeomp->g_nsect;
4248 		cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
4249 		cl->cl_g.dkg_rpm = pgeomp->g_rpm;
4250 		cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
4251 	}
4252 
4253 	cl->cl_g.dkg_read_reinstruct = 0;
4254 	cl->cl_g.dkg_write_reinstruct = 0;
4255 	cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
4256 	    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
4257 
4258 	cl->cl_map['a'-'a'].dkl_cylno = 0;
4259 	cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
4260 
4261 	cl->cl_map['c'-'a'].dkl_cylno = 0;
4262 	cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
4263 
4264 	cl->cl_vtoc.v_part[2].p_tag   = V_BACKUP;
4265 	cl->cl_vtoc.v_part[2].p_flag  = V_UNMNT;
4266 	cl->cl_vtoc.v_nparts = V_NUMPAR;
4267 	cl->cl_vtoc.v_version = V_VERSION;
4268 	(void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
4269 	    " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
4270 	    cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
4271 
4272 	cl->cl_f_geometry_is_valid = FALSE;
4273 }
4274 
4275 
4276 #if defined(__i386) || defined(__amd64)
4277 /*
4278  *    Function: cmlb_update_fdisk_and_vtoc
4279  *
4280  * Description: This local utility routine updates the device fdisk and vtoc
4281  *		as part of setting the device mboot.
4282  *
4283  *   Arguments:
4284  *	cl		driver soft state (unit) structure
4285  *
4286  *	tg_cookie	cookie from target driver to be passed back to target
4287  *			driver when we call back to it through tg_ops.
4288  *
4289  *
4290  * Return Code: 0 for success or errno-type return code.
4291  *
4292  *    Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
4293  *		these did exist separately in x86 sd.c.
4294  */
4295 static int
4296 cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
4297 {
4298 	int		count;
4299 	int		label_rc = 0;
4300 	int		fdisk_rval;
4301 	diskaddr_t	capacity;
4302 
4303 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4304 
4305 	if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
4306 		return (EINVAL);
4307 
4308 #if defined(_SUNOS_VTOC_16)
4309 	/*
4310 	 * Set up the "whole disk" fdisk partition; this should always
4311 	 * exist, regardless of whether the disk contains an fdisk table
4312 	 * or vtoc.
4313 	 */
4314 	cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
4315 	cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
4316 #endif	/* defined(_SUNOS_VTOC_16) */
4317 
4318 	/*
4319 	 * copy the lbasize and capacity so that if they're
4320 	 * reset while we're not holding the CMLB_MUTEX(cl), we will
4321 	 * continue to use valid values after the CMLB_MUTEX(cl) is
4322 	 * reacquired.
4323 	 */
4324 	capacity = cl->cl_blockcount;
4325 
4326 	/*
4327 	 * refresh the logical and physical geometry caches.
4328 	 * (data from mode sense format/rigid disk geometry pages,
4329 	 * and scsi_ifgetcap("geometry").
4330 	 */
4331 	cmlb_resync_geom_caches(cl, capacity, tg_cookie);
4332 
4333 	/*
4334 	 * Only DIRECT ACCESS devices will have Scl labels.
4335 	 * CD's supposedly have a Scl label, too
4336 	 */
4337 	if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
4338 		fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
4339 		if (fdisk_rval != 0) {
4340 			ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4341 			return (fdisk_rval);
4342 		}
4343 
4344 		if (cl->cl_solaris_size <= DK_LABEL_LOC) {
4345 			/*
4346 			 * Found fdisk table but no Solaris partition entry,
4347 			 * so don't call cmlb_uselabel() and don't create
4348 			 * a default label.
4349 			 */
4350 			label_rc = 0;
4351 			cl->cl_f_geometry_is_valid = TRUE;
4352 			goto no_solaris_partition;
4353 		}
4354 	} else if (capacity < 0) {
4355 		ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4356 		return (EINVAL);
4357 	}
4358 
4359 	/*
4360 	 * For Removable media We reach here if we have found a
4361 	 * SOLARIS PARTITION.
4362 	 * If cl_f_geometry_is_valid is FALSE it indicates that the SOLARIS
4363 	 * PARTITION has changed from the previous one, hence we will setup a
4364 	 * default VTOC in this case.
4365 	 */
4366 	if (cl->cl_f_geometry_is_valid == FALSE) {
4367 		/* if we get here it is writable */
4368 		/* we are called from SMBOOT, and after a write of fdisk */
4369 		cmlb_build_default_label(cl, tg_cookie);
4370 		label_rc = 0;
4371 	}
4372 
4373 no_solaris_partition:
4374 
4375 #if defined(_SUNOS_VTOC_16)
4376 	/*
4377 	 * If we have valid geometry, set up the remaining fdisk partitions.
4378 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4379 	 * we set it to an entirely bogus value.
4380 	 */
4381 	for (count = 0; count < FD_NUMPART; count++) {
4382 		cl->cl_map[FDISK_P1 + count].dkl_cylno = -1;
4383 		cl->cl_map[FDISK_P1 + count].dkl_nblk =
4384 		    cl->cl_fmap[count].fmap_nblk;
4385 		cl->cl_offset[FDISK_P1 + count] =
4386 		    cl->cl_fmap[count].fmap_start;
4387 	}
4388 #endif
4389 
4390 	for (count = 0; count < NDKMAP; count++) {
4391 #if defined(_SUNOS_VTOC_8)
4392 		struct dk_map *lp  = &cl->cl_map[count];
4393 		cl->cl_offset[count] =
4394 		    cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
4395 #elif defined(_SUNOS_VTOC_16)
4396 		struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
4397 		cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
4398 #else
4399 #error "No VTOC format defined."
4400 #endif
4401 	}
4402 
4403 	ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4404 	return (label_rc);
4405 }
4406 #endif
4407 
4408 #if defined(__i386) || defined(__amd64)
4409 static int
4410 cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
4411 {
4412 	int err = 0;
4413 
4414 	/* Return the driver's notion of the media's logical geometry */
4415 	struct dk_geom	disk_geom;
4416 	struct dk_geom	*dkgp = &disk_geom;
4417 
4418 	mutex_enter(CMLB_MUTEX(cl));
4419 	/*
4420 	 * If there is no HBA geometry available, or
4421 	 * if the HBA returned us something that doesn't
4422 	 * really fit into an Int 13/function 8 geometry
4423 	 * result, just fail the ioctl.  See PSARC 1998/313.
4424 	 */
4425 	if (cl->cl_lgeom.g_nhead == 0 ||
4426 	    cl->cl_lgeom.g_nsect == 0 ||
4427 	    cl->cl_lgeom.g_ncyl > 1024) {
4428 		mutex_exit(CMLB_MUTEX(cl));
4429 		err = EINVAL;
4430 	} else {
4431 		dkgp->dkg_ncyl	= cl->cl_lgeom.g_ncyl;
4432 		dkgp->dkg_acyl	= cl->cl_lgeom.g_acyl;
4433 		dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
4434 		dkgp->dkg_nhead	= cl->cl_lgeom.g_nhead;
4435 		dkgp->dkg_nsect	= cl->cl_lgeom.g_nsect;
4436 
4437 		mutex_exit(CMLB_MUTEX(cl));
4438 		if (ddi_copyout(dkgp, (void *)arg,
4439 		    sizeof (struct dk_geom), flag)) {
4440 			err = EFAULT;
4441 		} else {
4442 			err = 0;
4443 		}
4444 	}
4445 	return (err);
4446 }
4447 #endif
4448 
4449 #if defined(__i386) || defined(__amd64)
4450 static int
4451 cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t  arg, int flag)
4452 {
4453 	int err = 0;
4454 	diskaddr_t capacity;
4455 
4456 
4457 	/* Return the driver's notion of the media physical geometry */
4458 	struct dk_geom	disk_geom;
4459 	struct dk_geom	*dkgp = &disk_geom;
4460 
4461 	mutex_enter(CMLB_MUTEX(cl));
4462 
4463 	if (cl->cl_g.dkg_nhead != 0 &&
4464 	    cl->cl_g.dkg_nsect != 0) {
4465 		/*
4466 		 * We succeeded in getting a geometry, but
4467 		 * right now it is being reported as just the
4468 		 * Solaris fdisk partition, just like for
4469 		 * DKIOCGGEOM. We need to change that to be
4470 		 * correct for the entire disk now.
4471 		 */
4472 		bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
4473 		dkgp->dkg_acyl = 0;
4474 		dkgp->dkg_ncyl = cl->cl_blockcount /
4475 		    (dkgp->dkg_nhead * dkgp->dkg_nsect);
4476 	} else {
4477 		bzero(dkgp, sizeof (struct dk_geom));
4478 		/*
4479 		 * This disk does not have a Solaris VTOC
4480 		 * so we must present a physical geometry
4481 		 * that will remain consistent regardless
4482 		 * of how the disk is used. This will ensure
4483 		 * that the geometry does not change regardless
4484 		 * of the fdisk partition type (ie. EFI, FAT32,
4485 		 * Solaris, etc).
4486 		 */
4487 		if (ISCD(cl)) {
4488 			dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
4489 			dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
4490 			dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
4491 			dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
4492 		} else {
4493 			/*
4494 			 * Invalid cl_blockcount can generate invalid
4495 			 * dk_geom and may result in division by zero
4496 			 * system failure. Should make sure blockcount
4497 			 * is valid before using it here.
4498 			 */
4499 			if (cl->cl_blockcount == 0) {
4500 				mutex_exit(CMLB_MUTEX(cl));
4501 				err = EIO;
4502 				return (err);
4503 			}
4504 			/*
4505 			 * Refer to comments related to off-by-1 at the
4506 			 * header of this file
4507 			 */
4508 			if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
4509 				capacity = cl->cl_blockcount - 1;
4510 			else
4511 				capacity = cl->cl_blockcount;
4512 
4513 			cmlb_convert_geometry(capacity, dkgp);
4514 			dkgp->dkg_acyl = 0;
4515 			dkgp->dkg_ncyl = capacity /
4516 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
4517 		}
4518 	}
4519 	dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
4520 
4521 	mutex_exit(CMLB_MUTEX(cl));
4522 	if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
4523 		err = EFAULT;
4524 
4525 	return (err);
4526 }
4527 #endif
4528 
4529 #if defined(__i386) || defined(__amd64)
4530 static int
4531 cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t  arg, int flag)
4532 {
4533 	int err = 0;
4534 
4535 	/*
4536 	 * Return parameters describing the selected disk slice.
4537 	 * Note: this ioctl is for the intel platform only
4538 	 */
4539 	int part;
4540 
4541 	part = CMLBPART(dev);
4542 
4543 	mutex_enter(CMLB_MUTEX(cl));
4544 	/* don't check cl_solaris_size for pN */
4545 	if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
4546 		err = EIO;
4547 		mutex_exit(CMLB_MUTEX(cl));
4548 	} else {
4549 		struct part_info p;
4550 
4551 		p.p_start = (daddr_t)cl->cl_offset[part];
4552 		p.p_length = (int)cl->cl_map[part].dkl_nblk;
4553 		mutex_exit(CMLB_MUTEX(cl));
4554 #ifdef _MULTI_DATAMODEL
4555 		switch (ddi_model_convert_from(flag & FMODELS)) {
4556 		case DDI_MODEL_ILP32:
4557 		{
4558 			struct part_info32 p32;
4559 
4560 			p32.p_start = (daddr32_t)p.p_start;
4561 			p32.p_length = p.p_length;
4562 			if (ddi_copyout(&p32, (void *)arg,
4563 			    sizeof (p32), flag))
4564 				err = EFAULT;
4565 			break;
4566 		}
4567 
4568 		case DDI_MODEL_NONE:
4569 		{
4570 			if (ddi_copyout(&p, (void *)arg, sizeof (p),
4571 			    flag))
4572 				err = EFAULT;
4573 			break;
4574 		}
4575 		}
4576 #else /* ! _MULTI_DATAMODEL */
4577 		if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
4578 			err = EFAULT;
4579 #endif /* _MULTI_DATAMODEL */
4580 	}
4581 	return (err);
4582 }
4583 #endif
4584