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