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.
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.
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]
23 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * This module provides support for labeling operations for target
34 #include <sys/scsi/scsi.h>
35 #include <sys/sunddi.h>
36 #include <sys/dklabel.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/vtrace.h>
41 #include <sys/efi_partition.h>
43 #include <sys/cmlb_impl.h>
44 #if defined(__i386) || defined(__amd64)
45 #include <sys/fs/dv_node.h>
47 #include <sys/ddi_impldefs.h>
50 * Driver minor node structure and data table
52 struct driver_minor_data
{
58 static struct driver_minor_data dk_minor_data
[] = {
67 #if defined(_SUNOS_VTOC_16)
76 #endif /* defined(_SUNOS_VTOC_16) */
77 #if defined(_FIRMWARE_NEEDS_FDISK)
83 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
84 {"a,raw", 0, S_IFCHR
},
85 {"b,raw", 1, S_IFCHR
},
86 {"c,raw", 2, S_IFCHR
},
87 {"d,raw", 3, S_IFCHR
},
88 {"e,raw", 4, S_IFCHR
},
89 {"f,raw", 5, S_IFCHR
},
90 {"g,raw", 6, S_IFCHR
},
91 {"h,raw", 7, S_IFCHR
},
92 #if defined(_SUNOS_VTOC_16)
93 {"i,raw", 8, S_IFCHR
},
94 {"j,raw", 9, S_IFCHR
},
95 {"k,raw", 10, S_IFCHR
},
96 {"l,raw", 11, S_IFCHR
},
97 {"m,raw", 12, S_IFCHR
},
98 {"n,raw", 13, S_IFCHR
},
99 {"o,raw", 14, S_IFCHR
},
100 {"p,raw", 15, S_IFCHR
},
101 #endif /* defined(_SUNOS_VTOC_16) */
102 #if defined(_FIRMWARE_NEEDS_FDISK)
103 {"q,raw", 16, S_IFCHR
},
104 {"r,raw", 17, S_IFCHR
},
105 {"s,raw", 18, S_IFCHR
},
106 {"t,raw", 19, S_IFCHR
},
107 {"u,raw", 20, S_IFCHR
},
108 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
112 #if defined(__i386) || defined(__amd64)
113 #if defined(_FIRMWARE_NEEDS_FDISK)
114 static struct driver_minor_data dk_ext_minor_data
[] = {
120 {"p10", 26, S_IFBLK
},
121 {"p11", 27, S_IFBLK
},
122 {"p12", 28, S_IFBLK
},
123 {"p13", 29, S_IFBLK
},
124 {"p14", 30, S_IFBLK
},
125 {"p15", 31, S_IFBLK
},
126 {"p16", 32, S_IFBLK
},
127 {"p17", 33, S_IFBLK
},
128 {"p18", 34, S_IFBLK
},
129 {"p19", 35, S_IFBLK
},
130 {"p20", 36, S_IFBLK
},
131 {"p21", 37, S_IFBLK
},
132 {"p22", 38, S_IFBLK
},
133 {"p23", 39, S_IFBLK
},
134 {"p24", 40, S_IFBLK
},
135 {"p25", 41, S_IFBLK
},
136 {"p26", 42, S_IFBLK
},
137 {"p27", 43, S_IFBLK
},
138 {"p28", 44, S_IFBLK
},
139 {"p29", 45, S_IFBLK
},
140 {"p30", 46, S_IFBLK
},
141 {"p31", 47, S_IFBLK
},
142 {"p32", 48, S_IFBLK
},
143 {"p33", 49, S_IFBLK
},
144 {"p34", 50, S_IFBLK
},
145 {"p35", 51, S_IFBLK
},
146 {"p36", 52, S_IFBLK
},
147 {"p5,raw", 21, S_IFCHR
},
148 {"p6,raw", 22, S_IFCHR
},
149 {"p7,raw", 23, S_IFCHR
},
150 {"p8,raw", 24, S_IFCHR
},
151 {"p9,raw", 25, S_IFCHR
},
152 {"p10,raw", 26, S_IFCHR
},
153 {"p11,raw", 27, S_IFCHR
},
154 {"p12,raw", 28, S_IFCHR
},
155 {"p13,raw", 29, S_IFCHR
},
156 {"p14,raw", 30, S_IFCHR
},
157 {"p15,raw", 31, S_IFCHR
},
158 {"p16,raw", 32, S_IFCHR
},
159 {"p17,raw", 33, S_IFCHR
},
160 {"p18,raw", 34, S_IFCHR
},
161 {"p19,raw", 35, S_IFCHR
},
162 {"p20,raw", 36, S_IFCHR
},
163 {"p21,raw", 37, S_IFCHR
},
164 {"p22,raw", 38, S_IFCHR
},
165 {"p23,raw", 39, S_IFCHR
},
166 {"p24,raw", 40, S_IFCHR
},
167 {"p25,raw", 41, S_IFCHR
},
168 {"p26,raw", 42, S_IFCHR
},
169 {"p27,raw", 43, S_IFCHR
},
170 {"p28,raw", 44, S_IFCHR
},
171 {"p29,raw", 45, S_IFCHR
},
172 {"p30,raw", 46, S_IFCHR
},
173 {"p31,raw", 47, S_IFCHR
},
174 {"p32,raw", 48, S_IFCHR
},
175 {"p33,raw", 49, S_IFCHR
},
176 {"p34,raw", 50, S_IFCHR
},
177 {"p35,raw", 51, S_IFCHR
},
178 {"p36,raw", 52, S_IFCHR
},
181 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
182 #endif /* if defined(__i386) || defined(__amd64) */
184 static struct driver_minor_data dk_minor_data_efi
[] = {
193 #if defined(_SUNOS_VTOC_16)
202 #endif /* defined(_SUNOS_VTOC_16) */
203 #if defined(_FIRMWARE_NEEDS_FDISK)
209 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
210 {"a,raw", 0, S_IFCHR
},
211 {"b,raw", 1, S_IFCHR
},
212 {"c,raw", 2, S_IFCHR
},
213 {"d,raw", 3, S_IFCHR
},
214 {"e,raw", 4, S_IFCHR
},
215 {"f,raw", 5, S_IFCHR
},
216 {"g,raw", 6, S_IFCHR
},
217 {"wd,raw", 7, S_IFCHR
},
218 #if defined(_SUNOS_VTOC_16)
219 {"i,raw", 8, S_IFCHR
},
220 {"j,raw", 9, S_IFCHR
},
221 {"k,raw", 10, S_IFCHR
},
222 {"l,raw", 11, S_IFCHR
},
223 {"m,raw", 12, S_IFCHR
},
224 {"n,raw", 13, S_IFCHR
},
225 {"o,raw", 14, S_IFCHR
},
226 {"p,raw", 15, S_IFCHR
},
227 #endif /* defined(_SUNOS_VTOC_16) */
228 #if defined(_FIRMWARE_NEEDS_FDISK)
229 {"q,raw", 16, S_IFCHR
},
230 {"r,raw", 17, S_IFCHR
},
231 {"s,raw", 18, S_IFCHR
},
232 {"t,raw", 19, S_IFCHR
},
233 {"u,raw", 20, S_IFCHR
},
234 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
239 * Declare the dynamic properties implemented in prop_op(9E) implementation
240 * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
241 * of drivers that call cmlb_attach().
243 static i_ddi_prop_dyn_t cmlb_prop_dyn
[] = {
244 {"Nblocks", DDI_PROP_TYPE_INT64
, S_IFBLK
},
245 {"Size", DDI_PROP_TYPE_INT64
, S_IFCHR
},
246 {"device-nblocks", DDI_PROP_TYPE_INT64
},
247 {"device-blksize", DDI_PROP_TYPE_INT
},
248 {"device-solid-state", DDI_PROP_TYPE_INT
},
249 {"device-rotational", DDI_PROP_TYPE_INT
},
254 * This implies an upper limit of 8192 GPT partitions
255 * in one transfer for GUID Partition Entry Array.
257 len_t cmlb_tg_max_efi_xfer
= 1024 * 1024;
260 * External kernel interfaces
262 extern struct mod_ops mod_miscops
;
264 extern int ddi_create_internal_pathname(dev_info_t
*dip
, char *name
,
265 int spec_type
, minor_t minor_num
);
268 * Global buffer and mutex for debug logging
270 static char cmlb_log_buffer
[1024];
271 static kmutex_t cmlb_log_mutex
;
274 struct cmlb_lun
*cmlb_debug_cl
= NULL
;
275 uint_t cmlb_level_mask
= 0x0;
277 int cmlb_rot_delay
= 4; /* default rotational delay */
279 static struct modlmisc modlmisc
= {
280 &mod_miscops
, /* Type of module */
281 "Common Labeling module"
284 static struct modlinkage modlinkage
= {
285 MODREV_1
, (void *)&modlmisc
, NULL
288 /* Local function prototypes */
289 static dev_t
cmlb_make_device(struct cmlb_lun
*cl
);
290 static int cmlb_validate_geometry(struct cmlb_lun
*cl
, boolean_t forcerevalid
,
291 int flags
, void *tg_cookie
);
292 static void cmlb_resync_geom_caches(struct cmlb_lun
*cl
, diskaddr_t capacity
,
294 static int cmlb_read_fdisk(struct cmlb_lun
*cl
, diskaddr_t capacity
,
296 static void cmlb_swap_efi_gpt(efi_gpt_t
*e
);
297 static void cmlb_swap_efi_gpe(int nparts
, efi_gpe_t
*p
);
298 static int cmlb_validate_efi(efi_gpt_t
*labp
);
299 static int cmlb_use_efi(struct cmlb_lun
*cl
, diskaddr_t capacity
, int flags
,
301 static void cmlb_build_default_label(struct cmlb_lun
*cl
, void *tg_cookie
);
302 static int cmlb_uselabel(struct cmlb_lun
*cl
, struct dk_label
*l
, int flags
);
303 #if defined(_SUNOS_VTOC_8)
304 static void cmlb_build_user_vtoc(struct cmlb_lun
*cl
, struct vtoc
*user_vtoc
);
306 static int cmlb_build_label_vtoc(struct cmlb_lun
*cl
, struct vtoc
*user_vtoc
);
307 static int cmlb_write_label(struct cmlb_lun
*cl
, void *tg_cookie
);
308 static int cmlb_set_vtoc(struct cmlb_lun
*cl
, struct dk_label
*dkl
,
310 static void cmlb_clear_efi(struct cmlb_lun
*cl
, void *tg_cookie
);
311 static void cmlb_clear_vtoc(struct cmlb_lun
*cl
, void *tg_cookie
);
312 static void cmlb_setup_default_geometry(struct cmlb_lun
*cl
, void *tg_cookie
);
313 static int cmlb_create_minor_nodes(struct cmlb_lun
*cl
);
314 static int cmlb_check_update_blockcount(struct cmlb_lun
*cl
, void *tg_cookie
);
315 static boolean_t
cmlb_check_efi_mbr(uchar_t
*buf
, boolean_t
*is_mbr
);
317 #if defined(__i386) || defined(__amd64)
318 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun
*cl
, void *tg_cookie
);
321 #if defined(_FIRMWARE_NEEDS_FDISK)
322 static boolean_t
cmlb_has_max_chs_vals(struct ipart
*fdp
);
325 #if defined(_SUNOS_VTOC_16)
326 static void cmlb_convert_geometry(struct cmlb_lun
*cl
, diskaddr_t capacity
,
327 struct dk_geom
*cl_g
, void *tg_cookie
);
330 static int cmlb_dkio_get_geometry(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
332 static int cmlb_dkio_set_geometry(struct cmlb_lun
*cl
, caddr_t arg
, int flag
);
333 static int cmlb_dkio_get_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
335 static int cmlb_dkio_set_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
);
336 static int cmlb_dkio_get_efi(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
338 static int cmlb_dkio_set_efi(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
,
339 int flag
, void *tg_cookie
);
340 static int cmlb_dkio_get_vtoc(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
342 static int cmlb_dkio_get_extvtoc(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
344 static int cmlb_dkio_set_vtoc(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
,
345 int flag
, void *tg_cookie
);
346 static int cmlb_dkio_set_extvtoc(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
,
347 int flag
, void *tg_cookie
);
348 static int cmlb_dkio_get_mboot(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
350 static int cmlb_dkio_set_mboot(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
352 static int cmlb_dkio_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
355 #if defined(__i386) || defined(__amd64)
356 static int cmlb_dkio_set_ext_part(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
358 static int cmlb_validate_ext_part(struct cmlb_lun
*cl
, int part
, int epart
,
359 uint32_t start
, uint32_t size
);
360 static int cmlb_is_linux_swap(struct cmlb_lun
*cl
, uint32_t part_start
,
362 static int cmlb_dkio_get_virtgeom(struct cmlb_lun
*cl
, caddr_t arg
, int flag
);
363 static int cmlb_dkio_get_phygeom(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
365 static int cmlb_dkio_partinfo(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
,
367 static int cmlb_dkio_extpartinfo(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
,
371 static void cmlb_dbg(uint_t comp
, struct cmlb_lun
*cl
, const char *fmt
, ...);
372 static void cmlb_v_log(dev_info_t
*dev
, const char *label
, uint_t level
,
373 const char *fmt
, va_list ap
);
374 static void cmlb_log(dev_info_t
*dev
, const char *label
, uint_t level
,
375 const char *fmt
, ...);
380 mutex_init(&cmlb_log_mutex
, NULL
, MUTEX_DRIVER
, NULL
);
381 return (mod_install(&modlinkage
));
385 _info(struct modinfo
*modinfop
)
387 return (mod_info(&modlinkage
, modinfop
));
395 if ((err
= mod_remove(&modlinkage
)) != 0) {
399 mutex_destroy(&cmlb_log_mutex
);
404 * cmlb_dbg is used for debugging to log additional info
405 * Level of output is controlled via cmlb_level_mask setting.
408 cmlb_dbg(uint_t comp
, struct cmlb_lun
*cl
, const char *fmt
, ...)
412 uint_t level_mask
= 0;
415 dev
= CMLB_DEVINFO(cl
);
418 * Filter messages based on the global component and level masks,
419 * also print if cl matches the value of cmlb_debug_cl, or if
420 * cmlb_debug_cl is set to NULL.
422 if (comp
& CMLB_TRACE
)
423 level_mask
|= CMLB_LOGMASK_TRACE
;
425 if (comp
& CMLB_INFO
)
426 level_mask
|= CMLB_LOGMASK_INFO
;
428 if (comp
& CMLB_ERROR
)
429 level_mask
|= CMLB_LOGMASK_ERROR
;
431 if ((cmlb_level_mask
& level_mask
) &&
432 ((cmlb_debug_cl
== NULL
) || (cmlb_debug_cl
== cl
))) {
434 cmlb_v_log(dev
, CMLB_LABEL(cl
), CE_CONT
, fmt
, ap
);
440 * cmlb_log is basically a duplicate of scsi_log. It is redefined here
441 * so that this module does not depend on scsi module.
444 cmlb_log(dev_info_t
*dev
, const char *label
, uint_t level
, const char *fmt
, ...)
449 cmlb_v_log(dev
, label
, level
, fmt
, ap
);
454 cmlb_v_log(dev_info_t
*dev
, const char *label
, uint_t level
, const char *fmt
,
457 static char name
[256];
460 int console_only
= 0;
462 mutex_enter(&cmlb_log_mutex
);
465 if (level
== CE_PANIC
|| level
== CE_WARN
||
467 (void) sprintf(name
, "%s (%s%d):\n",
468 ddi_pathname(dev
, cmlb_log_buffer
),
469 label
, ddi_get_instance(dev
));
474 (void) sprintf(name
, "%s:", label
);
477 (void) vsprintf(cmlb_log_buffer
, fmt
, ap
);
479 switch (cmlb_log_buffer
[0]) {
499 cmn_err(level
, "?%s\t%s", name
, &cmlb_log_buffer
[1]);
500 } else if (console_only
) {
501 cmn_err(level
, "^%s\t%s", name
, &cmlb_log_buffer
[1]);
502 } else if (log_only
) {
503 cmn_err(level
, "!%s\t%s", name
, &cmlb_log_buffer
[1]);
505 cmn_err(level
, "%s\t%s", name
, cmlb_log_buffer
);
511 cmn_err(CE_CONT
, "^DEBUG: %s\t%s", name
, cmlb_log_buffer
);
514 mutex_exit(&cmlb_log_mutex
);
521 * Allocates a handle.
524 * cmlbhandlep pointer to handle
527 * Allocates a handle and stores the allocated handle in the area
528 * pointed to by cmlbhandlep
531 * Kernel thread only (can sleep).
534 cmlb_alloc_handle(cmlb_handle_t
*cmlbhandlep
)
538 cl
= kmem_zalloc(sizeof (struct cmlb_lun
), KM_SLEEP
);
539 ASSERT(cmlbhandlep
!= NULL
);
541 cl
->cl_state
= CMLB_INITED
;
542 cl
->cl_def_labeltype
= CMLB_LABEL_UNDEF
;
543 mutex_init(CMLB_MUTEX(cl
), NULL
, MUTEX_DRIVER
, NULL
);
545 *cmlbhandlep
= (cmlb_handle_t
)(cl
);
554 * cmlbhandlep pointer to handle
557 cmlb_free_handle(cmlb_handle_t
*cmlbhandlep
)
561 cl
= (struct cmlb_lun
*)*cmlbhandlep
;
563 mutex_destroy(CMLB_MUTEX(cl
));
564 kmem_free(cl
, sizeof (struct cmlb_lun
));
572 * Attach handle to device, create minor nodes for device.
575 * devi pointer to device's dev_info structure.
576 * tgopsp pointer to array of functions cmlb can use to callback
579 * device_type Peripheral device type as defined in
580 * scsi/generic/inquiry.h
582 * is_removable whether or not device is removable.
584 * is_hotpluggable whether or not device is hotpluggable.
586 * node_type minor node type (as used by ddi_create_minor_node)
591 * CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
592 * an alternate slice for the default label, if
593 * device type is DTYPE_DIRECT an architectures default
594 * label type is VTOC16.
595 * Otherwise alternate slice will no be created.
598 * CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
599 * geometry and label for DKIOCGGEOM and DKIOCGVTOC
600 * on architecture with VTOC8 label types.
602 * CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
603 * one bug in obtaining capacity (in sd):
604 * SCSI READ_CAPACITY command returns the LBA number of the
605 * last logical block, but sd once treated this number as
606 * disks' capacity on x86 platform. And LBAs are addressed
607 * based 0. So the last block was lost on x86 platform.
609 * Now, we remove this workaround. In order for present sd
610 * driver to work with disks which are labeled/partitioned
611 * via previous sd, we add workaround as follows:
613 * 1) Locate backup EFI label: cmlb searches the next to
615 * block for backup EFI label. If fails, it will
616 * turn to the last block for backup EFI label;
618 * 2) Clear backup EFI label: cmlb first search the last
619 * block for backup EFI label, and will search the
620 * next to last block only if failed for the last
623 * 3) Calculate geometry:refer to cmlb_convert_geometry()
624 * If capacity increasing by 1 causes disks' capacity
625 * to cross over the limits in geometry calculation,
626 * geometry info will change. This will raise an issue:
627 * In case that primary VTOC label is destroyed, format
628 * commandline can restore it via backup VTOC labels.
629 * And format locates backup VTOC labels by use of
630 * geometry. So changing geometry will
631 * prevent format from finding backup VTOC labels. To
632 * eliminate this side effect for compatibility,
633 * sd uses (capacity -1) to calculate geometry;
635 * 4) 1TB disks: some important data structures use
636 * 32-bit signed long/int (for example, daddr_t),
637 * so that sd doesn't support a disk with capacity
638 * larger than 1TB on 32-bit platform. However,
639 * for exactly 1TB disk, it was treated as (1T - 512)B
640 * in the past, and could have valid Solaris
641 * partitions. To workaround this, if an exactly 1TB
642 * disk has Solaris fdisk partition, it will be allowed
647 * CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
648 * the entire disk, if there is no valid partition info.
649 * If there is a valid Solaris partition, s0 and s2 will
650 * only cover the entire Solaris partition.
652 * CMLB_CREATE_P0_MINOR_NODE: create p0 node covering
653 * the entire disk. Used by lofi to ensure presence of
654 * whole disk device node in case of LOFI_MAP_FILE ioctl.
656 * cmlbhandle cmlb handle associated with device
658 * tg_cookie cookie from target driver to be passed back to target
659 * driver when we call back to it through tg_ops.
662 * Assumes a default label based on capacity for non-removable devices.
663 * If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
664 * for the architecture).
666 * For removable devices, default label type is assumed to be VTOC
667 * type. Create minor nodes based on a default label type.
668 * Label on the media is not validated.
669 * minor number consists of:
670 * if _SUNOS_VTOC_8 is defined
671 * lowest 3 bits is taken as partition number
672 * the rest is instance number
673 * if _SUNOS_VTOC_16 is defined
674 * lowest 6 bits is taken as partition number
675 * the rest is instance number
680 * ENXIO creating minor nodes failed.
681 * EINVAL invalid arg, unsupported tg_ops version
684 cmlb_attach(dev_info_t
*devi
, cmlb_tg_ops_t
*tgopsp
, int device_type
,
685 boolean_t is_removable
, boolean_t is_hotpluggable
, char *node_type
,
686 int alter_behavior
, cmlb_handle_t cmlbhandle
, void *tg_cookie
)
689 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
693 ASSERT(VALID_BOOLEAN(is_removable
));
694 ASSERT(VALID_BOOLEAN(is_hotpluggable
));
696 if (tgopsp
->tg_version
< TG_DK_OPS_VERSION_1
)
699 mutex_enter(CMLB_MUTEX(cl
));
701 CMLB_DEVINFO(cl
) = devi
;
702 cl
->cmlb_tg_ops
= tgopsp
;
703 cl
->cl_device_type
= device_type
;
704 cl
->cl_is_removable
= is_removable
;
705 cl
->cl_is_hotpluggable
= is_hotpluggable
;
706 cl
->cl_node_type
= node_type
;
707 cl
->cl_sys_blocksize
= DEV_BSIZE
;
708 cl
->cl_f_geometry_is_valid
= B_FALSE
;
709 cl
->cl_def_labeltype
= CMLB_LABEL_VTOC
;
710 cl
->cl_alter_behavior
= alter_behavior
;
711 cl
->cl_reserved
= -1;
712 cl
->cl_msglog_flag
|= CMLB_ALLOW_2TB_WARN
;
713 #if defined(__i386) || defined(__amd64)
714 cl
->cl_logical_drive_count
= 0;
718 mutex_exit(CMLB_MUTEX(cl
));
719 status
= DK_TG_GETCAP(cl
, &cap
, tg_cookie
);
720 mutex_enter(CMLB_MUTEX(cl
));
721 if (status
== 0 && cap
> CMLB_EXTVTOC_LIMIT
) {
722 /* set default EFI if > 2TB */
723 cl
->cl_def_labeltype
= CMLB_LABEL_EFI
;
727 /* create minor nodes based on default label type */
728 cl
->cl_last_labeltype
= CMLB_LABEL_UNDEF
;
729 cl
->cl_cur_labeltype
= CMLB_LABEL_UNDEF
;
731 if (cmlb_create_minor_nodes(cl
) != 0) {
732 mutex_exit(CMLB_MUTEX(cl
));
736 /* Define the dynamic properties for devinfo spapshots. */
737 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl
), cmlb_prop_dyn
);
739 cl
->cl_state
= CMLB_ATTACHED
;
741 mutex_exit(CMLB_MUTEX(cl
));
748 * Invalidate in-core labeling data and remove all minor nodes for
749 * the device associate with handle.
752 * cmlbhandle cmlb handle associated with device.
754 * tg_cookie cookie from target driver to be passed back to target
755 * driver when we call back to it through tg_ops.
760 cmlb_detach(cmlb_handle_t cmlbhandle
, void *tg_cookie
)
762 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
764 mutex_enter(CMLB_MUTEX(cl
));
765 cl
->cl_def_labeltype
= CMLB_LABEL_UNDEF
;
766 cl
->cl_f_geometry_is_valid
= B_FALSE
;
767 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
768 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl
), NULL
);
769 cl
->cl_state
= CMLB_INITED
;
770 mutex_exit(CMLB_MUTEX(cl
));
779 * cmlbhandle cmlb handle associated with device.
781 * flags operation flags. used for verbosity control
783 * tg_cookie cookie from target driver to be passed back to target
784 * driver when we call back to it through tg_ops.
788 * If new label type is different from the current, adjust minor nodes
793 * Note: having fdisk but no solaris partition is assumed
796 * ENOMEM memory allocation failed
797 * EIO i/o errors during read or get capacity
798 * EACCESS reservation conflicts
799 * EINVAL label was corrupt, or no default label was assumed
800 * ENXIO invalid handle
803 cmlb_validate(cmlb_handle_t cmlbhandle
, int flags
, void *tg_cookie
)
805 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
810 * Temp work-around checking cl for NULL since there is a bug
811 * in sd_detach calling this routine from taskq_dispatch
817 mutex_enter(CMLB_MUTEX(cl
));
818 if (cl
->cl_state
< CMLB_ATTACHED
) {
819 mutex_exit(CMLB_MUTEX(cl
));
823 rval
= cmlb_validate_geometry((struct cmlb_lun
*)cmlbhandle
, B_TRUE
,
826 if (rval
== ENOTSUP
) {
827 if (cl
->cl_f_geometry_is_valid
) {
828 cl
->cl_cur_labeltype
= CMLB_LABEL_EFI
;
836 cl
->cl_cur_labeltype
= CMLB_LABEL_VTOC
;
840 (void) cmlb_create_minor_nodes(cl
);
842 mutex_exit(CMLB_MUTEX(cl
));
848 * Invalidate in core label data
851 * cmlbhandle cmlb handle associated with device.
852 * tg_cookie cookie from target driver to be passed back to target
853 * driver when we call back to it through tg_ops.
857 cmlb_invalidate(cmlb_handle_t cmlbhandle
, void *tg_cookie
)
859 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
864 mutex_enter(CMLB_MUTEX(cl
));
865 cl
->cl_f_geometry_is_valid
= B_FALSE
;
866 mutex_exit(CMLB_MUTEX(cl
));
871 * Get status on whether the incore label/geom data is valid
874 * cmlbhandle cmlb handle associated with device.
877 * B_TRUE if incore label/geom data is valid.
884 cmlb_is_valid(cmlb_handle_t cmlbhandle
)
886 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
888 if (cmlbhandle
== NULL
)
891 return (cl
->cl_f_geometry_is_valid
);
900 * Close the device, revert to a default label minor node for the device,
901 * if it is removable.
904 * cmlbhandle cmlb handle associated with device.
906 * tg_cookie cookie from target driver to be passed back to target
907 * driver when we call back to it through tg_ops.
910 * ENXIO Re-creating minor node failed.
914 cmlb_close(cmlb_handle_t cmlbhandle
, void *tg_cookie
)
916 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
918 mutex_enter(CMLB_MUTEX(cl
));
919 cl
->cl_f_geometry_is_valid
= B_FALSE
;
921 /* revert to default minor node for this device */
922 if (ISREMOVABLE(cl
)) {
923 cl
->cl_cur_labeltype
= CMLB_LABEL_UNDEF
;
924 (void) cmlb_create_minor_nodes(cl
);
927 mutex_exit(CMLB_MUTEX(cl
));
932 * cmlb_get_devid_block:
933 * get the block number where device id is stored.
936 * cmlbhandle cmlb handle associated with device.
937 * devidblockp pointer to block number.
938 * tg_cookie cookie from target driver to be passed back to target
939 * driver when we call back to it through tg_ops.
942 * It stores the block number of device id in the area pointed to
944 * with the block number of device id.
948 * EINVAL device id does not apply to current label type.
952 cmlb_get_devid_block(cmlb_handle_t cmlbhandle
, diskaddr_t
*devidblockp
,
955 daddr_t spc
, blk
, head
, cyl
;
956 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
958 mutex_enter(CMLB_MUTEX(cl
));
959 if (cl
->cl_state
< CMLB_ATTACHED
) {
960 mutex_exit(CMLB_MUTEX(cl
));
964 if ((!cl
->cl_f_geometry_is_valid
) ||
965 (cl
->cl_solaris_size
< DK_LABEL_LOC
)) {
966 mutex_exit(CMLB_MUTEX(cl
));
970 if (cl
->cl_cur_labeltype
== CMLB_LABEL_EFI
) {
971 if (cl
->cl_reserved
!= -1) {
972 blk
= cl
->cl_map
[cl
->cl_reserved
].dkl_cylno
;
974 mutex_exit(CMLB_MUTEX(cl
));
978 /* if the disk is unlabeled, don't write a devid to it */
979 if (cl
->cl_label_from_media
!= CMLB_LABEL_VTOC
) {
980 mutex_exit(CMLB_MUTEX(cl
));
984 /* this geometry doesn't allow us to write a devid */
985 if (cl
->cl_g
.dkg_acyl
< 2) {
986 mutex_exit(CMLB_MUTEX(cl
));
991 * Subtract 2 guarantees that the next to last cylinder
994 cyl
= cl
->cl_g
.dkg_ncyl
+ cl
->cl_g
.dkg_acyl
- 2;
995 spc
= cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
;
996 head
= cl
->cl_g
.dkg_nhead
- 1;
997 blk
= cl
->cl_solaris_offset
+
998 (cyl
* (spc
- cl
->cl_g
.dkg_apc
)) +
999 (head
* cl
->cl_g
.dkg_nsect
) + 1;
1003 mutex_exit(CMLB_MUTEX(cl
));
1009 * Get partition info for specified partition number.
1012 * cmlbhandle cmlb handle associated with device.
1013 * part partition number
1014 * nblocksp pointer to number of blocks
1015 * startblockp pointer to starting block
1016 * partnamep pointer to name of partition
1017 * tagp pointer to tag info
1018 * tg_cookie cookie from target driver to be passed back to target
1019 * driver when we call back to it through tg_ops.
1023 * If in-core label is not valid, this functions tries to revalidate
1024 * the label. If label is valid, it stores the total number of blocks
1025 * in this partition in the area pointed to by nblocksp, starting
1026 * block number in area pointed to by startblockp, pointer to partition
1027 * name in area pointed to by partnamep, and tag value in area
1029 * For EFI labels, tag value will be set to 0.
1031 * For all nblocksp, startblockp and partnamep, tagp, a value of NULL
1032 * indicates the corresponding info is not requested.
1037 * EINVAL no valid label or requested partition number is invalid.
1041 cmlb_partinfo(cmlb_handle_t cmlbhandle
, int part
, diskaddr_t
*nblocksp
,
1042 diskaddr_t
*startblockp
, char **partnamep
, uint16_t *tagp
, void *tg_cookie
)
1045 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
1047 #if defined(__i386) || defined(__amd64)
1052 mutex_enter(CMLB_MUTEX(cl
));
1053 if (cl
->cl_state
< CMLB_ATTACHED
) {
1054 mutex_exit(CMLB_MUTEX(cl
));
1058 if (part
< 0 || part
>= MAXPART
) {
1061 if (!cl
->cl_f_geometry_is_valid
)
1062 (void) cmlb_validate_geometry((struct cmlb_lun
*)cl
,
1063 B_FALSE
, 0, tg_cookie
);
1065 if (((!cl
->cl_f_geometry_is_valid
) ||
1066 (part
< NDKMAP
&& cl
->cl_solaris_size
== 0)) &&
1067 (part
!= P0_RAW_DISK
)) {
1070 if (startblockp
!= NULL
)
1071 *startblockp
= (diskaddr_t
)cl
->cl_offset
[part
];
1073 if (nblocksp
!= NULL
)
1074 *nblocksp
= (diskaddr_t
)
1075 cl
->cl_map
[part
].dkl_nblk
;
1079 ((cl
->cl_cur_labeltype
== CMLB_LABEL_EFI
) ||
1080 (part
>= NDKMAP
)) ? V_UNASSIGNED
:
1081 cl
->cl_vtoc
.v_part
[part
].p_tag
;
1085 /* consistent with behavior of sd for getting minor name */
1086 if (partnamep
!= NULL
) {
1087 #if defined(__i386) || defined(__amd64)
1088 #if defined(_FIRMWARE_NEEDS_FDISK)
1089 if (part
> FDISK_P4
) {
1090 ext_part
= part
-FDISK_P4
-1;
1091 *partnamep
= dk_ext_minor_data
[ext_part
].name
;
1095 *partnamep
= dk_minor_data
[part
].name
;
1100 mutex_exit(CMLB_MUTEX(cl
));
1105 * cmlb_efi_label_capacity:
1106 * Get capacity stored in EFI disk label.
1109 * cmlbhandle cmlb handle associated with device.
1110 * capacity pointer to capacity stored in EFI disk label.
1111 * tg_cookie cookie from target driver to be passed back to target
1112 * driver when we call back to it through tg_ops.
1116 * If in-core label is not valid, this functions tries to revalidate
1117 * the label. If label is valid and is an EFI label, it stores the capacity
1118 * in disk label in the area pointed to by capacity.
1123 * EINVAL no valid EFI label or capacity is NULL.
1127 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle
, diskaddr_t
*capacity
,
1130 struct cmlb_lun
*cl
= (struct cmlb_lun
*)cmlbhandle
;
1134 mutex_enter(CMLB_MUTEX(cl
));
1135 if (cl
->cl_state
< CMLB_ATTACHED
) {
1136 mutex_exit(CMLB_MUTEX(cl
));
1140 if (!cl
->cl_f_geometry_is_valid
)
1141 (void) cmlb_validate_geometry((struct cmlb_lun
*)cl
, B_FALSE
,
1144 if ((!cl
->cl_f_geometry_is_valid
) || (capacity
== NULL
) ||
1145 (cl
->cl_cur_labeltype
!= CMLB_LABEL_EFI
)) {
1148 *capacity
= (diskaddr_t
)cl
->cl_map
[WD_NODE
].dkl_nblk
;
1152 mutex_exit(CMLB_MUTEX(cl
));
1156 /* Caller should make sure Test Unit Ready succeeds before calling this. */
1159 cmlb_ioctl(cmlb_handle_t cmlbhandle
, dev_t dev
, int cmd
, intptr_t arg
,
1160 int flag
, cred_t
*cred_p
, int *rval_p
, void *tg_cookie
)
1164 struct cmlb_lun
*cl
;
1166 cl
= (struct cmlb_lun
*)cmlbhandle
;
1170 mutex_enter(CMLB_MUTEX(cl
));
1171 if (cl
->cl_state
< CMLB_ATTACHED
) {
1172 mutex_exit(CMLB_MUTEX(cl
));
1181 #if defined(__i386) || defined(__amd64)
1182 case DKIOCSETEXTPART
:
1186 #if defined(__i386) || defined(__amd64)
1189 if (cl
->cl_blockcount
> CMLB_OLDVTOC_LIMIT
) {
1190 mutex_exit(CMLB_MUTEX(cl
));
1195 (void) cmlb_validate_geometry(cl
, 1, CMLB_SILENT
,
1203 if (cl
->cl_label_from_media
== CMLB_LABEL_EFI
) {
1204 /* GPT label on disk */
1205 mutex_exit(CMLB_MUTEX(cl
));
1208 (cl
->cl_blockcount
> CMLB_OLDVTOC_LIMIT
) {
1209 mutex_exit(CMLB_MUTEX(cl
));
1215 if (cl
->cl_label_from_media
== CMLB_LABEL_EFI
) {
1216 /* GPT label on disk */
1217 mutex_exit(CMLB_MUTEX(cl
));
1226 mutex_exit(CMLB_MUTEX(cl
));
1230 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGGEOM\n");
1231 err
= cmlb_dkio_get_geometry(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1235 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSGEOM\n");
1236 err
= cmlb_dkio_set_geometry(cl
, (caddr_t
)arg
, flag
);
1240 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGAPART\n");
1241 err
= cmlb_dkio_get_partition(cl
, (caddr_t
)arg
,
1246 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSAPART\n");
1247 err
= cmlb_dkio_set_partition(cl
, (caddr_t
)arg
, flag
);
1251 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGVTOC\n");
1252 err
= cmlb_dkio_get_vtoc(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1256 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGVTOC\n");
1257 err
= cmlb_dkio_get_extvtoc(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1261 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGETEFI\n");
1262 err
= cmlb_dkio_get_efi(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1265 case DKIOCPARTITION
:
1266 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCPARTITION\n");
1267 err
= cmlb_dkio_partition(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1271 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSVTOC\n");
1272 err
= cmlb_dkio_set_vtoc(cl
, dev
, (caddr_t
)arg
, flag
,
1277 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSVTOC\n");
1278 err
= cmlb_dkio_set_extvtoc(cl
, dev
, (caddr_t
)arg
, flag
,
1283 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSETEFI\n");
1284 err
= cmlb_dkio_set_efi(cl
, dev
, (caddr_t
)arg
, flag
, tg_cookie
);
1288 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCGMBOOT\n");
1289 err
= cmlb_dkio_get_mboot(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1293 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSMBOOT\n");
1294 err
= cmlb_dkio_set_mboot(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1296 case DKIOCG_PHYGEOM
:
1297 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCG_PHYGEOM\n");
1298 #if defined(__i386) || defined(__amd64)
1299 err
= cmlb_dkio_get_phygeom(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1304 case DKIOCG_VIRTGEOM
:
1305 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCG_VIRTGEOM\n");
1306 #if defined(__i386) || defined(__amd64)
1307 err
= cmlb_dkio_get_virtgeom(cl
, (caddr_t
)arg
, flag
);
1313 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCPARTINFO");
1314 #if defined(__i386) || defined(__amd64)
1315 err
= cmlb_dkio_partinfo(cl
, dev
, (caddr_t
)arg
, flag
);
1320 case DKIOCEXTPARTINFO
:
1321 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCPARTINFO");
1322 #if defined(__i386) || defined(__amd64)
1323 err
= cmlb_dkio_extpartinfo(cl
, dev
, (caddr_t
)arg
, flag
);
1328 #if defined(__i386) || defined(__amd64)
1329 case DKIOCSETEXTPART
:
1330 cmlb_dbg(CMLB_TRACE
, cl
, "DKIOCSETEXTPART");
1331 err
= cmlb_dkio_set_ext_part(cl
, (caddr_t
)arg
, flag
, tg_cookie
);
1340 * An ioctl that succeeds and changed ('set') size(9P) information
1341 * needs to invalidate the cached devinfo snapshot to avoid having
1342 * old information being returned in a snapshots.
1344 * NB: When available, call ddi_change_minor_node() to clear
1345 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1354 i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl
),
1355 i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl
)));
1362 cmlb_make_device(struct cmlb_lun
*cl
)
1364 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
) {
1365 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl
)),
1367 CMLB_DEVINFO(cl
)) << CMLBUNIT_FORCE_P0_SHIFT
));
1369 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl
)),
1370 ddi_get_instance(CMLB_DEVINFO(cl
)) << CMLBUNIT_SHIFT
));
1375 * Function: cmlb_check_update_blockcount
1377 * Description: If current capacity value is invalid, obtains the
1378 * current capacity from target driver.
1380 * Return Code: 0 success
1384 cmlb_check_update_blockcount(struct cmlb_lun
*cl
, void *tg_cookie
)
1387 diskaddr_t capacity
;
1390 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1392 if (cl
->cl_f_geometry_is_valid
)
1395 mutex_exit(CMLB_MUTEX(cl
));
1396 status
= DK_TG_GETCAP(cl
, &capacity
, tg_cookie
);
1398 mutex_enter(CMLB_MUTEX(cl
));
1402 status
= DK_TG_GETBLOCKSIZE(cl
, &lbasize
, tg_cookie
);
1403 mutex_enter(CMLB_MUTEX(cl
));
1407 if ((capacity
!= 0) && (lbasize
!= 0)) {
1408 cl
->cl_blockcount
= capacity
;
1409 cl
->cl_tgt_blocksize
= lbasize
;
1410 if (!cl
->cl_is_removable
) {
1411 cl
->cl_sys_blocksize
= lbasize
;
1420 cmlb_create_minor(dev_info_t
*dip
, char *name
, int spec_type
,
1421 minor_t minor_num
, char *node_type
, int flag
, boolean_t internal
)
1423 ASSERT(VALID_BOOLEAN(internal
));
1426 return (ddi_create_internal_pathname(dip
,
1427 name
, spec_type
, minor_num
));
1429 return (ddi_create_minor_node(dip
,
1430 name
, spec_type
, minor_num
, node_type
, flag
));
1434 * Function: cmlb_create_minor_nodes
1436 * Description: Create or adjust the minor device nodes for the instance.
1437 * Minor nodes are created based on default label type,
1438 * current label type and last label type we created
1439 * minor nodes based on.
1442 * Arguments: cl - driver soft state (unit) structure
1444 * Return Code: 0 success
1447 * Context: Kernel thread context
1450 cmlb_create_minor_nodes(struct cmlb_lun
*cl
)
1452 struct driver_minor_data
*dmdp
;
1453 int instance
, shift
;
1455 cmlb_label_t newlabeltype
;
1459 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1461 internal
= VOID2BOOLEAN(
1462 (cl
->cl_alter_behavior
& (CMLB_INTERNAL_MINOR_NODES
)) != 0);
1464 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
1465 shift
= CMLBUNIT_FORCE_P0_SHIFT
;
1467 shift
= CMLBUNIT_SHIFT
;
1469 /* check the most common case */
1470 if (cl
->cl_cur_labeltype
!= CMLB_LABEL_UNDEF
&&
1471 cl
->cl_last_labeltype
== cl
->cl_cur_labeltype
) {
1476 if (cl
->cl_def_labeltype
== CMLB_LABEL_UNDEF
) {
1477 /* we should never get here */
1481 if (cl
->cl_last_labeltype
== CMLB_LABEL_UNDEF
) {
1482 /* first time during attach */
1483 newlabeltype
= cl
->cl_def_labeltype
;
1485 instance
= ddi_get_instance(CMLB_DEVINFO(cl
));
1487 /* Create all the minor nodes for this target. */
1488 dmdp
= (newlabeltype
== CMLB_LABEL_EFI
) ? dk_minor_data_efi
:
1490 while (dmdp
->name
!= NULL
) {
1492 (void) sprintf(name
, "%s", dmdp
->name
);
1494 if (cmlb_create_minor(CMLB_DEVINFO(cl
), name
,
1496 (instance
<< shift
) | dmdp
->minor
,
1497 cl
->cl_node_type
, NULL
, internal
) == DDI_FAILURE
) {
1499 * Clean up any nodes that may have been
1500 * created, in case this fails in the middle
1503 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
1508 cl
->cl_last_labeltype
= newlabeltype
;
1509 #if defined(_SUNOS_VTOC_8)
1511 * "emulate" p0 device for sparc, used by lofi
1513 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
) {
1514 if (cmlb_create_minor(CMLB_DEVINFO(cl
), "q", S_IFBLK
,
1515 (instance
<< CMLBUNIT_FORCE_P0_SHIFT
) | P0_RAW_DISK
,
1516 cl
->cl_node_type
, NULL
, internal
) == DDI_FAILURE
) {
1517 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
1521 if (cmlb_create_minor(CMLB_DEVINFO(cl
), "q,raw",
1523 (instance
<< CMLBUNIT_FORCE_P0_SHIFT
) | P0_RAW_DISK
,
1524 cl
->cl_node_type
, NULL
, internal
) == DDI_FAILURE
) {
1525 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
1529 #endif /* defined(_SUNOS_VTOC_8) */
1533 /* Not first time */
1534 if (cl
->cl_cur_labeltype
== CMLB_LABEL_UNDEF
) {
1535 if (cl
->cl_last_labeltype
!= cl
->cl_def_labeltype
) {
1536 /* close time, revert to default. */
1537 newlabeltype
= cl
->cl_def_labeltype
;
1540 * do nothing since the type for which we last created
1541 * nodes matches the default
1546 if (cl
->cl_cur_labeltype
!= cl
->cl_last_labeltype
) {
1547 /* We are not closing, use current label type */
1548 newlabeltype
= cl
->cl_cur_labeltype
;
1551 * do nothing since the type for which we last created
1552 * nodes matches the current label type
1558 instance
= ddi_get_instance(CMLB_DEVINFO(cl
));
1561 * Currently we only fix up the s7 node when we are switching
1562 * label types from or to EFI. This is consistent with
1563 * current behavior of sd.
1565 if (newlabeltype
== CMLB_LABEL_EFI
&&
1566 cl
->cl_last_labeltype
!= CMLB_LABEL_EFI
) {
1567 /* from vtoc to EFI */
1568 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h");
1569 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h,raw");
1570 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "wd",
1571 S_IFBLK
, (instance
<< shift
) | WD_NODE
,
1572 cl
->cl_node_type
, NULL
, internal
);
1573 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "wd,raw",
1574 S_IFCHR
, (instance
<< shift
) | WD_NODE
,
1575 cl
->cl_node_type
, NULL
, internal
);
1577 /* from efi to vtoc */
1578 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd");
1579 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd,raw");
1580 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h",
1581 S_IFBLK
, (instance
<< shift
) | WD_NODE
,
1582 cl
->cl_node_type
, NULL
, internal
);
1583 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h,raw",
1584 S_IFCHR
, (instance
<< shift
) | WD_NODE
,
1585 cl
->cl_node_type
, NULL
, internal
);
1588 cl
->cl_last_labeltype
= newlabeltype
;
1593 * Function: cmlb_validate_geometry
1595 * Description: Read the label from the disk (if present). Update the unit's
1596 * geometry and vtoc information from the data in the label.
1597 * Verify that the label is valid.
1600 * cl driver soft state (unit) structure
1602 * forcerevalid force revalidation even if we are already valid.
1603 * flags operation flags from target driver. Used for verbosity
1604 * control at this time.
1605 * tg_cookie cookie from target driver to be passed back to target
1606 * driver when we call back to it through tg_ops.
1608 * Return Code: 0 - Successful completion
1609 * EINVAL - Invalid value in cl->cl_tgt_blocksize or
1610 * cl->cl_blockcount; or label on disk is corrupted
1612 * EACCES - Reservation conflict at the device.
1613 * ENOMEM - Resource allocation error
1614 * ENOTSUP - geometry not applicable
1616 * Context: Kernel thread only (can sleep).
1619 cmlb_validate_geometry(struct cmlb_lun
*cl
, boolean_t forcerevalid
, int flags
,
1622 int label_error
= 0;
1623 diskaddr_t capacity
;
1626 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1627 ASSERT(VALID_BOOLEAN(forcerevalid
));
1629 if ((cl
->cl_f_geometry_is_valid
) && (!forcerevalid
)) {
1630 if (cl
->cl_cur_labeltype
== CMLB_LABEL_EFI
)
1635 if (cmlb_check_update_blockcount(cl
, tg_cookie
) != 0)
1638 capacity
= cl
->cl_blockcount
;
1641 * Set up the "whole disk" fdisk partition; this should always
1642 * exist, regardless of whether the disk contains an fdisk table
1645 cl
->cl_map
[P0_RAW_DISK
].dkl_cylno
= 0;
1646 cl
->cl_offset
[P0_RAW_DISK
] = 0;
1648 * note if capacity > int32_max(1TB) we are in 64bit environment
1649 * so no truncation happens
1651 cl
->cl_map
[P0_RAW_DISK
].dkl_nblk
= capacity
;
1654 * Refresh the logical and physical geometry caches.
1655 * (data from MODE SENSE format/rigid disk geometry pages,
1656 * and scsi_ifgetcap("geometry").
1658 cmlb_resync_geom_caches(cl
, capacity
, tg_cookie
);
1660 cl
->cl_label_from_media
= CMLB_LABEL_UNDEF
;
1661 label_error
= cmlb_use_efi(cl
, capacity
, flags
, tg_cookie
);
1662 if (label_error
== 0) {
1664 /* found a valid EFI label */
1665 cmlb_dbg(CMLB_TRACE
, cl
,
1666 "cmlb_validate_geometry: found EFI label\n");
1668 * solaris_size and geometry_is_valid are set in
1674 /* NO EFI label found */
1676 if (capacity
> CMLB_EXTVTOC_LIMIT
) {
1677 if (label_error
== ESRCH
) {
1679 * they've configured a LUN over 2TB, but used
1680 * format.dat to restrict format's view of the
1681 * capacity to be under 2TB in some earlier Solaris
1684 /* i.e > 2TB with a VTOC < 2TB */
1685 if (!(flags
& CMLB_SILENT
) &&
1686 (cl
->cl_msglog_flag
& CMLB_ALLOW_2TB_WARN
)) {
1688 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
),
1689 CE_NOTE
, "!Disk (%s%d) is limited to 2 TB "
1690 "due to VTOC label. To use the full "
1691 "capacity of the disk, use format(1M) to "
1692 "relabel the disk with EFI/GPT label.\n",
1694 ddi_get_instance(CMLB_DEVINFO(cl
)));
1696 cl
->cl_msglog_flag
&= ~CMLB_ALLOW_2TB_WARN
;
1706 * at this point it is either labeled with a VTOC or it is
1707 * under 1TB (<= 1TB actually for off-by-1)
1711 * Only DIRECT ACCESS devices will have Scl labels.
1712 * CD's supposedly have a Scl label, too
1714 if (cl
->cl_device_type
== DTYPE_DIRECT
|| ISREMOVABLE(cl
)) {
1715 struct dk_label
*dkl
;
1716 offset_t label_addr
;
1721 * Note: This will set up cl->cl_solaris_size and
1722 * cl->cl_solaris_offset.
1724 rval
= cmlb_read_fdisk(cl
, capacity
, tg_cookie
);
1725 if ((rval
!= 0) && !ISCD(cl
)) {
1726 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1730 if (cl
->cl_solaris_size
<= DK_LABEL_LOC
) {
1732 * Found fdisk table but no Solaris partition entry,
1733 * so don't call cmlb_uselabel() and don't create
1737 cl
->cl_f_geometry_is_valid
= B_TRUE
;
1738 goto no_solaris_partition
;
1741 label_addr
= (daddr_t
)(cl
->cl_solaris_offset
+ DK_LABEL_LOC
);
1743 buffer_size
= cl
->cl_sys_blocksize
;
1745 cmlb_dbg(CMLB_TRACE
, cl
, "cmlb_validate_geometry: "
1746 "label_addr: 0x%x allocation size: 0x%x\n",
1747 label_addr
, buffer_size
);
1749 if ((dkl
= kmem_zalloc(buffer_size
, KM_NOSLEEP
)) == NULL
)
1752 mutex_exit(CMLB_MUTEX(cl
));
1753 rval
= DK_TG_READ(cl
, dkl
, label_addr
, buffer_size
, tg_cookie
);
1754 mutex_enter(CMLB_MUTEX(cl
));
1759 * cmlb_uselabel will establish that the geometry
1762 if (cmlb_uselabel(cl
,
1763 (struct dk_label
*)(uintptr_t)dkl
, flags
) !=
1764 CMLB_LABEL_IS_VALID
) {
1765 label_error
= EINVAL
;
1767 cl
->cl_label_from_media
= CMLB_LABEL_VTOC
;
1770 label_error
= EACCES
;
1773 label_error
= EINVAL
;
1777 kmem_free(dkl
, buffer_size
);
1781 * If a valid label was not found, AND if no reservation conflict
1782 * was detected, then go ahead and create a default label (4069506).
1784 * Note: currently, for VTOC_8 devices, the default label is created
1785 * for removables and hotpluggables only. For VTOC_16 devices, the
1786 * default label will be created for all devices.
1787 * (see cmlb_build_default_label)
1789 #if defined(_SUNOS_VTOC_8)
1790 if ((ISREMOVABLE(cl
) || ISHOTPLUGGABLE(cl
)) &&
1791 (label_error
!= EACCES
)) {
1792 #elif defined(_SUNOS_VTOC_16)
1793 if (label_error
!= EACCES
) {
1795 if (!cl
->cl_f_geometry_is_valid
) {
1796 cmlb_build_default_label(cl
, tg_cookie
);
1801 no_solaris_partition
:
1803 #if defined(_SUNOS_VTOC_16)
1805 * If we have valid geometry, set up the remaining fdisk partitions.
1806 * Note that dkl_cylno is not used for the fdisk map entries, so
1807 * we set it to an entirely bogus value.
1809 for (count
= 0; count
< FDISK_PARTS
; count
++) {
1810 cl
->cl_map
[FDISK_P1
+ count
].dkl_cylno
= UINT16_MAX
;
1811 cl
->cl_map
[FDISK_P1
+ count
].dkl_nblk
=
1812 cl
->cl_fmap
[count
].fmap_nblk
;
1814 cl
->cl_offset
[FDISK_P1
+ count
] =
1815 cl
->cl_fmap
[count
].fmap_start
;
1819 for (count
= 0; count
< NDKMAP
; count
++) {
1820 #if defined(_SUNOS_VTOC_8)
1821 struct dk_map
*lp
= &cl
->cl_map
[count
];
1822 cl
->cl_offset
[count
] =
1823 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
* lp
->dkl_cylno
;
1824 #elif defined(_SUNOS_VTOC_16)
1825 struct dkl_partition
*vp
= &cl
->cl_vtoc
.v_part
[count
];
1827 cl
->cl_offset
[count
] = vp
->p_start
+ cl
->cl_solaris_offset
;
1829 #error "No VTOC format defined."
1833 return (label_error
);
1836 #if defined(_SUNOS_VTOC_16)
1838 * Function: cmlb_convert_geometry
1840 * Description: Convert physical geometry into a dk_geom structure. In
1841 * other words, make sure we don't wrap 16-bit values.
1842 * e.g. converting from geom_cache to dk_geom
1844 * Context: Kernel thread only
1847 cmlb_convert_geometry(struct cmlb_lun
*cl
, diskaddr_t capacity
,
1848 struct dk_geom
*cl_g
, void *tg_cookie
)
1852 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1854 /* Unlabeled SCSI floppy device */
1855 if (capacity
< 160) {
1857 cl_g
->dkg_nhead
= 1;
1858 cl_g
->dkg_ncyl
= capacity
;
1859 cl_g
->dkg_nsect
= 1;
1861 } else if (capacity
<= 0x1000) {
1862 cl_g
->dkg_nhead
= 2;
1863 cl_g
->dkg_ncyl
= 80;
1864 cl_g
->dkg_nsect
= capacity
/ (cl_g
->dkg_nhead
* cl_g
->dkg_ncyl
);
1869 * For all devices we calculate cylinders using the heads and sectors
1870 * we assign based on capacity of the device. The algorithm is
1871 * designed to be compatible with the way other operating systems
1872 * lay out fdisk tables for X86 and to insure that the cylinders never
1873 * exceed 65535 to prevent problems with X86 ioctls that report
1875 * For some smaller disk sizes we report geometry that matches those
1876 * used by X86 BIOS usage. For larger disks, we use SPT that are
1877 * multiples of 63, since other OSes that are not limited to 16-bits
1878 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
1880 * The following table (in order) illustrates some end result
1883 * Maximum number of blocks nhead nsect
1885 * 2097152 (1GB) 64 32
1886 * 16777216 (8GB) 128 32
1887 * 1052819775 (502.02GB) 255 63
1888 * 2105639550 (0.98TB) 255 126
1889 * 3158459325 (1.47TB) 255 189
1890 * 4211279100 (1.96TB) 255 252
1891 * 5264098875 (2.45TB) 255 315
1894 * For Solid State Drive(SSD), it uses 4K page size inside and may be
1895 * double with every new generation. If the I/O is not aligned with
1896 * page size on SSDs, SSDs perform a lot slower.
1897 * By default, Solaris partition starts from cylinder 1. It will be
1898 * misaligned even with 4K if using heads(255) and SPT(63). To
1899 * workaround the problem, if the device is SSD, we use heads(224) and
1900 * SPT multiple of 56. Thus the default Solaris partition starts from
1901 * a position that aligns with 128K on a 512 bytes sector size SSD.
1904 if (capacity
<= 0x200000) {
1905 cl_g
->dkg_nhead
= 64;
1906 cl_g
->dkg_nsect
= 32;
1907 } else if (capacity
<= 0x01000000) {
1908 cl_g
->dkg_nhead
= 128;
1909 cl_g
->dkg_nsect
= 32;
1911 tg_attribute_t tgattribute
;
1913 unsigned short nhead
;
1914 unsigned short nsect
;
1916 bzero(&tgattribute
, sizeof (tg_attribute_t
));
1918 mutex_exit(CMLB_MUTEX(cl
));
1920 (DK_TG_GETATTRIBUTE(cl
, &tgattribute
, tg_cookie
) == 0) ?
1921 tgattribute
.media_is_solid_state
: FALSE
;
1922 mutex_enter(CMLB_MUTEX(cl
));
1924 if (is_solid_state
) {
1932 cl_g
->dkg_nhead
= nhead
;
1934 /* make dkg_nsect be smallest multiple of nsect */
1935 cl_g
->dkg_nsect
= ((capacity
+
1936 (UINT16_MAX
* nhead
* nsect
) - 1) /
1937 (UINT16_MAX
* nhead
* nsect
)) * nsect
;
1939 if (cl_g
->dkg_nsect
== 0)
1940 cl_g
->dkg_nsect
= (UINT16_MAX
/ nsect
) * nsect
;
1947 * Function: cmlb_resync_geom_caches
1949 * Description: (Re)initialize both geometry caches: the virtual geometry
1950 * information is extracted from the HBA (the "geometry"
1951 * capability), and the physical geometry cache data is
1952 * generated by issuing MODE SENSE commands.
1955 * cl driver soft state (unit) structure
1956 * capacity disk capacity in #blocks
1957 * tg_cookie cookie from target driver to be passed back to target
1958 * driver when we call back to it through tg_ops.
1960 * Context: Kernel thread only (can sleep).
1963 cmlb_resync_geom_caches(struct cmlb_lun
*cl
, diskaddr_t capacity
,
1966 struct cmlb_geom pgeom
;
1967 struct cmlb_geom lgeom
;
1968 struct cmlb_geom
*pgeomp
= &pgeom
;
1969 unsigned short nhead
;
1970 unsigned short nsect
;
1975 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
1978 * Ask the controller for its logical geometry.
1979 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1980 * then the lgeom cache will be invalid.
1982 mutex_exit(CMLB_MUTEX(cl
));
1983 bzero(&lgeom
, sizeof (struct cmlb_geom
));
1984 ret
= DK_TG_GETVIRTGEOM(cl
, &lgeom
, tg_cookie
);
1985 mutex_enter(CMLB_MUTEX(cl
));
1987 bcopy(&lgeom
, &cl
->cl_lgeom
, sizeof (cl
->cl_lgeom
));
1990 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1991 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1993 if (ret
!= 0 || cl
->cl_lgeom
.g_nsect
== 0 ||
1994 cl
->cl_lgeom
.g_nhead
== 0) {
1996 * Note: Perhaps this needs to be more adaptive? The rationale
1997 * is that, if there's no HBA geometry from the HBA driver, any
1998 * guess is good, since this is the physical geometry. If MODE
1999 * SENSE fails this gives a max cylinder size for non-LBA access
2004 nhead
= cl
->cl_lgeom
.g_nhead
;
2005 nsect
= cl
->cl_lgeom
.g_nsect
;
2009 pgeomp
->g_nhead
= 1;
2010 pgeomp
->g_nsect
= nsect
* nhead
;
2012 pgeomp
->g_nhead
= nhead
;
2013 pgeomp
->g_nsect
= nsect
;
2016 spc
= pgeomp
->g_nhead
* pgeomp
->g_nsect
;
2017 pgeomp
->g_capacity
= capacity
;
2021 pgeomp
->g_ncyl
= pgeomp
->g_capacity
/ spc
;
2025 * Retrieve fresh geometry data from the hardware, stash it
2026 * here temporarily before we rebuild the incore label.
2028 * We want to use the MODE SENSE commands to derive the
2029 * physical geometry of the device, but if either command
2030 * fails, the logical geometry is used as the fallback for
2031 * disk label geometry.
2034 mutex_exit(CMLB_MUTEX(cl
));
2035 (void) DK_TG_GETPHYGEOM(cl
, pgeomp
, tg_cookie
);
2036 mutex_enter(CMLB_MUTEX(cl
));
2039 * Now update the real copy while holding the mutex. This
2040 * way the global copy is never in an inconsistent state.
2042 bcopy(pgeomp
, &cl
->cl_pgeom
, sizeof (cl
->cl_pgeom
));
2044 cmlb_dbg(CMLB_INFO
, cl
, "cmlb_resync_geom_caches: "
2045 "(cached from lgeom)\n");
2046 cmlb_dbg(CMLB_INFO
, cl
,
2047 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2048 cl
->cl_pgeom
.g_ncyl
, cl
->cl_pgeom
.g_acyl
,
2049 cl
->cl_pgeom
.g_nhead
, cl
->cl_pgeom
.g_nsect
);
2050 cmlb_dbg(CMLB_INFO
, cl
, " lbasize: %d; capacity: %ld; "
2051 "intrlv: %d; rpm: %d\n", cl
->cl_pgeom
.g_secsize
,
2052 cl
->cl_pgeom
.g_capacity
, cl
->cl_pgeom
.g_intrlv
,
2053 cl
->cl_pgeom
.g_rpm
);
2057 #if defined(__i386) || defined(__amd64)
2059 * Function: cmlb_update_ext_minor_nodes
2061 * Description: Routine to add/remove extended partition device nodes
2064 * cl driver soft state (unit) structure
2065 * num_parts Number of logical drives found on the LUN
2067 * Should be called with the mutex held
2069 * Return Code: 0 for success
2071 * Context: User and Kernel thread
2075 cmlb_update_ext_minor_nodes(struct cmlb_lun
*cl
, int num_parts
)
2077 int i
, count
, shift
;
2080 struct driver_minor_data
*demdp
, *demdpr
;
2085 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
2086 ASSERT(cl
->cl_update_ext_minor_nodes
== 1);
2088 internal
= VOID2BOOLEAN(
2089 (cl
->cl_alter_behavior
& (CMLB_INTERNAL_MINOR_NODES
)) != 0);
2090 instance
= ddi_get_instance(CMLB_DEVINFO(cl
));
2091 demdp
= dk_ext_minor_data
;
2092 demdpr
= &dk_ext_minor_data
[MAX_EXT_PARTS
];
2094 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
2095 shift
= CMLBUNIT_FORCE_P0_SHIFT
;
2097 shift
= CMLBUNIT_SHIFT
;
2099 if (cl
->cl_logical_drive_count
) {
2100 for (i
= 0; i
< cl
->cl_logical_drive_count
; i
++) {
2101 (void) sprintf(name
, "%s", demdp
->name
);
2102 ddi_remove_minor_node(CMLB_DEVINFO(cl
), name
);
2103 (void) sprintf(name
, "%s", demdpr
->name
);
2104 ddi_remove_minor_node(CMLB_DEVINFO(cl
), name
);
2108 /* There are existing device nodes. Remove them */
2109 devnm
= kmem_alloc(MAXNAMELEN
+ 1, KM_SLEEP
);
2110 (void) ddi_deviname(cl
->cl_devi
, devnm
);
2111 pdip
= ddi_get_parent(cl
->cl_devi
);
2112 (void) devfs_clean(pdip
, devnm
+ 1, DV_CLEAN_FORCE
);
2113 kmem_free(devnm
, MAXNAMELEN
+ 1);
2116 demdp
= dk_ext_minor_data
;
2117 demdpr
= &dk_ext_minor_data
[MAX_EXT_PARTS
];
2119 for (i
= 0; i
< num_parts
; i
++) {
2120 (void) sprintf(name
, "%s", demdp
->name
);
2121 if (cmlb_create_minor(CMLB_DEVINFO(cl
), name
,
2123 (instance
<< shift
) | demdp
->minor
,
2124 cl
->cl_node_type
, NULL
, internal
) == DDI_FAILURE
) {
2126 * Clean up any nodes that may have been
2127 * created, in case this fails in the middle
2130 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
2131 cl
->cl_logical_drive_count
= 0;
2134 (void) sprintf(name
, "%s", demdpr
->name
);
2135 if (ddi_create_minor_node(CMLB_DEVINFO(cl
), name
,
2137 (instance
<< shift
) | demdpr
->minor
,
2138 cl
->cl_node_type
, NULL
) == DDI_FAILURE
) {
2140 * Clean up any nodes that may have been
2141 * created, in case this fails in the middle
2144 ddi_remove_minor_node(CMLB_DEVINFO(cl
), NULL
);
2145 cl
->cl_logical_drive_count
= 0;
2152 /* Update the cl_map array for logical drives */
2153 for (count
= 0; count
< MAX_EXT_PARTS
; count
++) {
2154 cl
->cl_map
[FDISK_P4
+ 1 + count
].dkl_cylno
= UINT32_MAX
;
2155 cl
->cl_map
[FDISK_P4
+ 1 + count
].dkl_nblk
=
2156 cl
->cl_fmap
[FD_NUMPART
+ count
].fmap_nblk
;
2157 cl
->cl_offset
[FDISK_P4
+ 1 + count
] =
2158 cl
->cl_fmap
[FD_NUMPART
+ count
].fmap_start
;
2161 cl
->cl_logical_drive_count
= i
;
2162 cl
->cl_update_ext_minor_nodes
= 0;
2166 * Function: cmlb_validate_ext_part
2168 * Description: utility routine to validate an extended partition's
2169 * metadata as found on disk
2172 * cl driver soft state (unit) structure
2173 * part partition number of the extended partition
2174 * epart partition number of the logical drive
2175 * start absolute sector number of the start of the logical
2176 * drive being validated
2177 * size size of logical drive being validated
2179 * Return Code: 0 for success
2181 * Context: User and Kernel thread
2185 * 1. If start block is lesser than or equal to the end block
2186 * 2. If either start block or end block is beyond the bounadry
2187 * of the extended partition.
2188 * 3. start or end block overlap with existing partitions.
2189 * To check this, first make sure that the start block doesnt
2190 * overlap with existing partitions. Then, calculate the
2191 * possible end block for the given start block that doesnt
2192 * overlap with existing partitions. This can be calculated by
2193 * first setting the possible end block to the end of the
2194 * extended partition (optimistic) and then, checking if there
2195 * is any other partition that lies after the start of the
2196 * partition being validated. If so, set the possible end to
2197 * one block less than the beginning of the next nearest partition
2198 * If the actual end block is greater than the calculated end
2199 * block, we have an overlap.
2203 cmlb_validate_ext_part(struct cmlb_lun
*cl
, int part
, int epart
, uint32_t start
,
2207 uint32_t end
= start
+ size
- 1;
2208 uint32_t ext_start
= cl
->cl_fmap
[part
].fmap_start
;
2209 uint32_t ext_end
= ext_start
+ cl
->cl_fmap
[part
].fmap_nblk
- 1;
2211 uint32_t poss_end
= ext_end
;
2218 * Check if the logical drive boundaries are within that of the
2219 * extended partition.
2221 if (start
<= ext_start
|| start
> ext_end
|| end
<= ext_start
||
2227 * epart will be equal to FD_NUMPART if it is the first logical drive.
2228 * There is no need to check for overlaps with other logical drives,
2229 * since it is the only logical drive that we have come across so far.
2231 if (epart
== FD_NUMPART
) {
2235 /* Check for overlaps with existing logical drives */
2237 ts
= cl
->cl_fmap
[FD_NUMPART
].fmap_start
;
2238 te
= ts
+ cl
->cl_fmap
[FD_NUMPART
].fmap_nblk
- 1;
2240 while ((i
< epart
) && ts
&& te
) {
2241 if (start
>= ts
&& start
<= te
) {
2245 if ((ts
< poss_end
) && (ts
> start
)) {
2250 ts
= cl
->cl_fmap
[i
].fmap_start
;
2251 te
= ts
+ cl
->cl_fmap
[i
].fmap_nblk
- 1;
2254 if (end
> poss_end
) {
2263 * Function: cmlb_is_linux_swap
2265 * Description: utility routine to verify if a partition is a linux swap
2269 * cl driver soft state (unit) structure
2270 * part_start absolute sector number of the start of the partition
2272 * tg_cookie cookie from target driver to be passed back to target
2273 * driver when we call back to it through tg_ops.
2275 * Return Code: 0 for success
2277 * Context: User and Kernel thread
2280 * The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2281 * last 10 bytes of a disk block whose size is that of the linux page
2282 * size. This disk block is found at the beginning of the swap partition.
2285 cmlb_is_linux_swap(struct cmlb_lun
*cl
, uint32_t part_start
, void *tg_cookie
)
2289 uint32_t seek_offset
;
2290 uint32_t linux_pg_size
;
2291 char *buf
, *linux_swap_magic
;
2292 int sec_sz
= cl
->cl_sys_blocksize
;
2293 /* Known linux kernel page sizes */
2294 uint32_t linux_pg_size_arr
[] = {4096, };
2297 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
2299 if ((buf
= kmem_zalloc(sec_sz
, KM_NOSLEEP
)) == NULL
) {
2304 * Check if there is a sane Solaris VTOC
2305 * If there is a valid vtoc, no need to lookup
2306 * for the linux swap signature.
2308 mutex_exit(CMLB_MUTEX(cl
));
2309 rval
= DK_TG_READ(cl
, buf
, part_start
+ DK_LABEL_LOC
,
2311 mutex_enter(CMLB_MUTEX(cl
));
2313 cmlb_dbg(CMLB_ERROR
, cl
,
2314 "cmlb_is_linux_swap: disk vtoc read err\n");
2319 if ((((struct dk_label
*)buf
)->dkl_magic
== DKL_MAGIC
) &&
2320 (((struct dk_label
*)buf
)->dkl_vtoc
.v_sanity
== VTOC_SANE
)) {
2326 /* No valid vtoc, so check for linux swap signature */
2327 linux_swap_magic
= buf
+ sec_sz
- 10;
2329 for (i
= 0; i
< sizeof (linux_pg_size_arr
)/sizeof (uint32_t); i
++) {
2330 linux_pg_size
= linux_pg_size_arr
[i
];
2331 seek_offset
= linux_pg_size
/sec_sz
- 1;
2332 seek_offset
+= part_start
;
2334 mutex_exit(CMLB_MUTEX(cl
));
2335 rval
= DK_TG_READ(cl
, buf
, seek_offset
, sec_sz
, tg_cookie
);
2336 mutex_enter(CMLB_MUTEX(cl
));
2339 cmlb_dbg(CMLB_ERROR
, cl
,
2340 "cmlb_is_linux_swap: disk read err\n");
2347 if ((strncmp(linux_swap_magic
, "SWAP-SPACE", 10) == 0) ||
2348 (strncmp(linux_swap_magic
, "SWAPSPACE2", 10) == 0)) {
2349 /* Found a linux swap */
2356 kmem_free(buf
, sec_sz
);
2362 * Function: cmlb_read_fdisk
2364 * Description: utility routine to read the fdisk table.
2367 * cl driver soft state (unit) structure
2368 * capacity disk capacity in #blocks
2369 * tg_cookie cookie from target driver to be passed back to target
2370 * driver when we call back to it through tg_ops.
2372 * Return Code: 0 for success (includes not reading for no_fdisk_present case
2373 * errnos from tg_rw if failed to read the first block.
2375 * Context: Kernel thread only (can sleep).
2379 cmlb_read_fdisk(struct cmlb_lun
*cl
, diskaddr_t capacity
, void *tg_cookie
)
2381 #if defined(_NO_FDISK_PRESENT)
2383 cl
->cl_solaris_offset
= 0;
2384 cl
->cl_solaris_size
= capacity
;
2385 bzero(cl
->cl_fmap
, sizeof (struct fmap
) * FD_NUMPART
);
2388 #elif defined(_FIRMWARE_NEEDS_FDISK)
2392 struct ipart fdisk
[FD_NUMPART
];
2399 uint_t solaris_offset
; /* offset to solaris part. */
2400 daddr_t solaris_size
; /* size of solaris partition */
2402 #if defined(__i386) || defined(__amd64)
2403 struct ipart eparts
[2];
2404 struct ipart
*efdp1
= &eparts
[0];
2405 struct ipart
*efdp2
= &eparts
[1];
2406 int ext_part_exists
= 0;
2411 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
2414 * Start off assuming no fdisk table
2417 solaris_size
= capacity
;
2419 blocksize
= cl
->cl_tgt_blocksize
;
2421 bufp
= kmem_zalloc(blocksize
, KM_SLEEP
);
2423 mutex_exit(CMLB_MUTEX(cl
));
2424 rval
= DK_TG_READ(cl
, bufp
, 0, blocksize
, tg_cookie
);
2425 mutex_enter(CMLB_MUTEX(cl
));
2428 cmlb_dbg(CMLB_ERROR
, cl
,
2429 "cmlb_read_fdisk: fdisk read err\n");
2430 bzero(cl
->cl_fmap
, sizeof (struct fmap
) * FD_NUMPART
);
2434 mbp
= (struct mboot
*)bufp
;
2437 * The fdisk table does not begin on a 4-byte boundary within the
2438 * master boot record, so we copy it to an aligned structure to avoid
2439 * alignment exceptions on some processors.
2441 bcopy(&mbp
->parts
[0], fdisk
, sizeof (fdisk
));
2444 * Check for lba support before verifying sig; sig might not be
2445 * there, say on a blank disk, but the max_chs mark may still
2448 * Note: LBA support and BEFs are an x86-only concept but this
2449 * code should work OK on SPARC as well.
2453 * First, check for lba-access-ok on root node (or prom root node)
2454 * if present there, don't need to search fdisk table.
2456 if (ddi_getprop(DDI_DEV_T_ANY
, ddi_root_node(), 0,
2457 "lba-access-ok", 0) != 0) {
2458 /* All drives do LBA; don't search fdisk table */
2461 /* Okay, look for mark in fdisk table */
2462 for (fdp
= fdisk
, i
= 0; i
< FD_NUMPART
; i
++, fdp
++) {
2463 /* accumulate "lba" value from all partitions */
2464 lba
= (lba
|| cmlb_has_max_chs_vals(fdp
));
2469 dev_t dev
= cmlb_make_device(cl
);
2471 if (ddi_getprop(dev
, CMLB_DEVINFO(cl
), DDI_PROP_DONTPASS
,
2472 "lba-access-ok", 0) == 0) {
2473 /* not found; create it */
2474 if (ddi_prop_create(dev
, CMLB_DEVINFO(cl
), 0,
2475 "lba-access-ok", (caddr_t
)NULL
, 0) !=
2477 cmlb_dbg(CMLB_ERROR
, cl
,
2478 "cmlb_read_fdisk: Can't create lba "
2479 "property for instance %d\n",
2480 ddi_get_instance(CMLB_DEVINFO(cl
)));
2485 bcopy(&mbp
->signature
, sigbuf
, sizeof (sigbuf
));
2488 * Endian-independent signature check
2490 if (((sigbuf
[1] & 0xFF) != ((MBB_MAGIC
>> 8) & 0xFF)) ||
2491 (sigbuf
[0] != (MBB_MAGIC
& 0xFF))) {
2492 cmlb_dbg(CMLB_ERROR
, cl
,
2493 "cmlb_read_fdisk: no fdisk\n");
2494 bzero(cl
->cl_fmap
, sizeof (struct fmap
) * FD_NUMPART
);
2499 if (cmlb_level_mask
& CMLB_LOGMASK_INFO
) {
2501 cmlb_dbg(CMLB_INFO
, cl
, "cmlb_read_fdisk:\n");
2502 cmlb_dbg(CMLB_INFO
, cl
, " relsect "
2503 "numsect sysid bootid\n");
2504 for (i
= 0; i
< FD_NUMPART
; i
++, fdp
++) {
2505 cmlb_dbg(CMLB_INFO
, cl
,
2506 " %d: %8d %8d 0x%08x 0x%08x\n",
2507 i
, fdp
->relsect
, fdp
->numsect
,
2508 fdp
->systid
, fdp
->bootid
);
2514 * Try to find the unix partition
2520 for (fdp
= fdisk
, i
= 0; i
< FD_NUMPART
; i
++, fdp
++) {
2524 #if defined(__i386) || defined(__amd64)
2526 * Stores relative block offset from the beginning of the
2527 * Extended Partition.
2529 int ext_relsect
= 0;
2532 if (fdp
->numsect
== 0) {
2533 cl
->cl_fmap
[i
].fmap_start
= 0;
2534 cl
->cl_fmap
[i
].fmap_nblk
= 0;
2539 * Data in the fdisk table is little-endian.
2541 relsect
= LE_32(fdp
->relsect
);
2542 numsect
= LE_32(fdp
->numsect
);
2544 cl
->cl_fmap
[i
].fmap_start
= relsect
;
2545 cl
->cl_fmap
[i
].fmap_nblk
= numsect
;
2546 cl
->cl_fmap
[i
].fmap_systid
= LE_8(fdp
->systid
);
2548 #if defined(__i386) || defined(__amd64)
2549 /* Support only one extended partition per LUN */
2550 if ((fdp
->systid
== EXTDOS
|| fdp
->systid
== FDISK_EXTLBA
) &&
2551 (ext_part_exists
== 0)) {
2553 uint32_t logdrive_offset
;
2554 uint32_t ext_numsect
;
2555 uint32_t abs_secnum
;
2557 ext_part_exists
= 1;
2559 for (j
= FD_NUMPART
; j
< FDISK_PARTS
; j
++) {
2560 mutex_exit(CMLB_MUTEX(cl
));
2561 rval
= DK_TG_READ(cl
, bufp
,
2562 (relsect
+ ext_relsect
), blocksize
,
2564 mutex_enter(CMLB_MUTEX(cl
));
2567 cmlb_dbg(CMLB_ERROR
, cl
,
2568 "cmlb_read_fdisk: Extended "
2569 "partition read err\n");
2573 * The first ipart entry provides the offset
2574 * at which the logical drive starts off from
2575 * the beginning of the container partition
2576 * and the size of the logical drive.
2577 * The second ipart entry provides the offset
2578 * of the next container partition from the
2579 * beginning of the extended partition.
2581 bcopy(&bufp
[FDISK_PART_TABLE_START
], eparts
,
2583 logdrive_offset
= LE_32(efdp1
->relsect
);
2584 ext_numsect
= LE_32(efdp1
->numsect
);
2585 systid
= LE_8(efdp1
->systid
);
2586 if (logdrive_offset
<= 0 || ext_numsect
<= 0)
2588 abs_secnum
= relsect
+ ext_relsect
+
2591 /* Boundary condition and overlap checking */
2592 if (cmlb_validate_ext_part(cl
, i
, j
, abs_secnum
,
2597 if ((cl
->cl_fmap
[j
].fmap_start
!= abs_secnum
) ||
2598 (cl
->cl_fmap
[j
].fmap_nblk
!= ext_numsect
) ||
2599 (cl
->cl_fmap
[j
].fmap_systid
!= systid
)) {
2601 * Indicates change from previous
2602 * partinfo. Need to recreate
2603 * logical device nodes.
2605 cl
->cl_update_ext_minor_nodes
= 1;
2607 cl
->cl_fmap
[j
].fmap_start
= abs_secnum
;
2608 cl
->cl_fmap
[j
].fmap_nblk
= ext_numsect
;
2609 cl
->cl_fmap
[j
].fmap_systid
= systid
;
2612 if ((efdp1
->systid
== SUNIXOS
&&
2613 (cmlb_is_linux_swap(cl
, abs_secnum
,
2614 tg_cookie
) != 0)) ||
2615 efdp1
->systid
== SUNIXOS2
) {
2618 solaris_offset
= abs_secnum
;
2619 solaris_size
= ext_numsect
;
2623 if ((ext_relsect
= LE_32(efdp2
->relsect
)) == 0)
2630 if (fdp
->systid
!= SUNIXOS
&&
2631 fdp
->systid
!= SUNIXOS2
&&
2632 fdp
->systid
!= EFI_PMBR
) {
2637 * use the last active solaris partition id found
2638 * (there should only be 1 active partition id)
2640 * if there are no active solaris partition id
2641 * then use the first inactive solaris partition id
2643 if ((uidx
== -1) || (fdp
->bootid
== ACTIVE
)) {
2644 #if defined(__i386) || defined(__amd64)
2645 if (fdp
->systid
!= SUNIXOS
||
2646 (fdp
->systid
== SUNIXOS
&&
2647 (cmlb_is_linux_swap(cl
, relsect
,
2648 tg_cookie
) != 0))) {
2651 solaris_offset
= relsect
;
2652 solaris_size
= numsect
;
2653 #if defined(__i386) || defined(__amd64)
2658 #if defined(__i386) || defined(__amd64)
2659 if (ld_count
< cl
->cl_logical_drive_count
) {
2661 * Some/all logical drives were deleted. Clear out
2662 * the fmap entries correspoding to those deleted drives.
2664 for (k
= ld_count
+ FD_NUMPART
;
2665 k
< cl
->cl_logical_drive_count
+ FD_NUMPART
; k
++) {
2666 cl
->cl_fmap
[k
].fmap_start
= 0;
2667 cl
->cl_fmap
[k
].fmap_nblk
= 0;
2668 cl
->cl_fmap
[k
].fmap_systid
= 0;
2670 cl
->cl_update_ext_minor_nodes
= 1;
2672 if (cl
->cl_update_ext_minor_nodes
) {
2673 rval
= cmlb_update_ext_minor_nodes(cl
, ld_count
);
2679 cmlb_dbg(CMLB_INFO
, cl
, "fdisk 0x%x 0x%lx",
2680 cl
->cl_solaris_offset
, cl
->cl_solaris_size
);
2684 * Clear the VTOC info, only if the Solaris partition entry
2685 * has moved, changed size, been deleted, or if the size of
2686 * the partition is too small to even fit the label sector.
2688 if ((cl
->cl_solaris_offset
!= solaris_offset
) ||
2689 (cl
->cl_solaris_size
!= solaris_size
) ||
2690 solaris_size
<= DK_LABEL_LOC
) {
2691 cmlb_dbg(CMLB_INFO
, cl
, "fdisk moved 0x%x 0x%lx",
2692 solaris_offset
, solaris_size
);
2693 bzero(&cl
->cl_g
, sizeof (struct dk_geom
));
2694 bzero(&cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
2695 bzero(&cl
->cl_map
, NDKMAP
* (sizeof (struct dk_map
)));
2696 cl
->cl_f_geometry_is_valid
= B_FALSE
;
2698 cl
->cl_solaris_offset
= solaris_offset
;
2699 cl
->cl_solaris_size
= solaris_size
;
2700 kmem_free(bufp
, blocksize
);
2703 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */
2704 #error "fdisk table presence undetermined for this platform."
2705 #endif /* #if defined(_NO_FDISK_PRESENT) */
2709 cmlb_swap_efi_gpt(efi_gpt_t
*e
)
2711 _NOTE(ASSUMING_PROTECTED(*e
))
2712 e
->efi_gpt_Signature
= LE_64(e
->efi_gpt_Signature
);
2713 e
->efi_gpt_Revision
= LE_32(e
->efi_gpt_Revision
);
2714 e
->efi_gpt_HeaderSize
= LE_32(e
->efi_gpt_HeaderSize
);
2715 e
->efi_gpt_HeaderCRC32
= LE_32(e
->efi_gpt_HeaderCRC32
);
2716 e
->efi_gpt_MyLBA
= LE_64(e
->efi_gpt_MyLBA
);
2717 e
->efi_gpt_AlternateLBA
= LE_64(e
->efi_gpt_AlternateLBA
);
2718 e
->efi_gpt_FirstUsableLBA
= LE_64(e
->efi_gpt_FirstUsableLBA
);
2719 e
->efi_gpt_LastUsableLBA
= LE_64(e
->efi_gpt_LastUsableLBA
);
2720 UUID_LE_CONVERT(e
->efi_gpt_DiskGUID
, e
->efi_gpt_DiskGUID
);
2721 e
->efi_gpt_PartitionEntryLBA
= LE_64(e
->efi_gpt_PartitionEntryLBA
);
2722 e
->efi_gpt_NumberOfPartitionEntries
=
2723 LE_32(e
->efi_gpt_NumberOfPartitionEntries
);
2724 e
->efi_gpt_SizeOfPartitionEntry
=
2725 LE_32(e
->efi_gpt_SizeOfPartitionEntry
);
2726 e
->efi_gpt_PartitionEntryArrayCRC32
=
2727 LE_32(e
->efi_gpt_PartitionEntryArrayCRC32
);
2731 cmlb_swap_efi_gpe(int nparts
, efi_gpe_t
*p
)
2735 _NOTE(ASSUMING_PROTECTED(*p
))
2736 for (i
= 0; i
< nparts
; i
++) {
2737 UUID_LE_CONVERT(p
[i
].efi_gpe_PartitionTypeGUID
,
2738 p
[i
].efi_gpe_PartitionTypeGUID
);
2739 p
[i
].efi_gpe_StartingLBA
= LE_64(p
[i
].efi_gpe_StartingLBA
);
2740 p
[i
].efi_gpe_EndingLBA
= LE_64(p
[i
].efi_gpe_EndingLBA
);
2741 /* PartitionAttrs */
2746 cmlb_validate_efi(efi_gpt_t
*labp
)
2748 if (labp
->efi_gpt_Signature
!= EFI_SIGNATURE
)
2750 /* at least 96 bytes in this version of the spec. */
2751 if (sizeof (efi_gpt_t
) - sizeof (labp
->efi_gpt_Reserved2
) >
2752 labp
->efi_gpt_HeaderSize
)
2754 /* this should be 128 bytes */
2755 if (labp
->efi_gpt_SizeOfPartitionEntry
!= sizeof (efi_gpe_t
))
2761 * This function returns B_FALSE if there is a valid MBR signature and no
2762 * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2764 * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2765 * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2766 * where a PMBR entry is not the only one. Also, if the first block has been
2767 * corrupted, currently best attempt to allow data access would be to try to
2768 * check for GPT headers. Hence in case of more than one partition entry, but
2769 * at least one EFI_PMBR partition type or no valid magic number, the function
2770 * returns B_TRUE to continue with looking for GPT header.
2774 cmlb_check_efi_mbr(uchar_t
*buf
, boolean_t
*is_mbr
)
2777 struct mboot
*mbp
= (struct mboot
*)buf
;
2778 struct ipart fdisk
[FD_NUMPART
];
2784 if (LE_16(mbp
->signature
) != MBB_MAGIC
) {
2790 bcopy(&mbp
->parts
[0], fdisk
, sizeof (fdisk
));
2792 for (fdp
= fdisk
, i
= 0; i
< FD_NUMPART
; i
++, fdp
++) {
2793 if (fdp
->systid
== EFI_PMBR
)
2801 cmlb_use_efi(struct cmlb_lun
*cl
, diskaddr_t capacity
, int flags
,
2806 efi_gpe_t
*partitions
;
2808 uint_t lbasize
; /* is really how much to read */
2812 diskaddr_t alternate_lba
;
2814 struct uuid uuid_type_reserved
= EFI_RESERVED
;
2815 #if defined(_FIRMWARE_NEEDS_FDISK)
2819 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
2821 lbasize
= cl
->cl_sys_blocksize
;
2823 cl
->cl_reserved
= -1;
2824 mutex_exit(CMLB_MUTEX(cl
));
2826 buf
= kmem_zalloc(EFI_MIN_ARRAY_SIZE
, KM_SLEEP
);
2828 rval
= DK_TG_READ(cl
, buf
, 0, lbasize
, tg_cookie
);
2833 if (((struct dk_label
*)buf
)->dkl_magic
== DKL_MAGIC
) {
2839 #if defined(_FIRMWARE_NEEDS_FDISK)
2840 if (!cmlb_check_efi_mbr(buf
, &is_mbr
)) {
2848 if (!cmlb_check_efi_mbr(buf
, NULL
)) {
2855 rval
= DK_TG_READ(cl
, buf
, 1, lbasize
, tg_cookie
);
2860 cmlb_swap_efi_gpt((efi_gpt_t
*)buf
);
2862 if ((rval
= cmlb_validate_efi((efi_gpt_t
*)buf
)) != 0) {
2864 * Couldn't read the primary, try the backup. Our
2865 * capacity at this point could be based on CHS, so
2866 * check what the device reports.
2868 rval
= DK_TG_GETCAP(cl
, &cap
, tg_cookie
);
2875 * CMLB_OFF_BY_ONE case, we check the next to last block first
2876 * for backup GPT header, otherwise check the last block.
2879 if ((rval
= DK_TG_READ(cl
, buf
,
2880 cap
- ((cl
->cl_alter_behavior
& CMLB_OFF_BY_ONE
) ? 2 : 1),
2881 lbasize
, tg_cookie
))
2886 cmlb_swap_efi_gpt((efi_gpt_t
*)buf
);
2888 if ((rval
= cmlb_validate_efi((efi_gpt_t
*)buf
)) != 0) {
2890 if (!(cl
->cl_alter_behavior
& CMLB_OFF_BY_ONE
))
2892 if ((rval
= DK_TG_READ(cl
, buf
, cap
- 1, lbasize
,
2895 cmlb_swap_efi_gpt((efi_gpt_t
*)buf
);
2896 if ((rval
= cmlb_validate_efi((efi_gpt_t
*)buf
)) != 0)
2899 if (!(flags
& CMLB_SILENT
))
2900 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
), CE_WARN
,
2901 "primary label corrupt; using backup\n");
2904 nparts
= ((efi_gpt_t
*)buf
)->efi_gpt_NumberOfPartitionEntries
;
2905 gpe_lba
= ((efi_gpt_t
*)buf
)->efi_gpt_PartitionEntryLBA
;
2906 alternate_lba
= ((efi_gpt_t
*)buf
)->efi_gpt_AlternateLBA
;
2908 rval
= DK_TG_READ(cl
, buf
, gpe_lba
, EFI_MIN_ARRAY_SIZE
, tg_cookie
);
2913 partitions
= (efi_gpe_t
*)buf
;
2915 if (nparts
> MAXPART
) {
2918 cmlb_swap_efi_gpe(nparts
, partitions
);
2920 mutex_enter(CMLB_MUTEX(cl
));
2922 /* Fill in partition table. */
2923 for (i
= 0; i
< nparts
; i
++) {
2924 if (partitions
->efi_gpe_StartingLBA
!= 0 ||
2925 partitions
->efi_gpe_EndingLBA
!= 0) {
2926 cl
->cl_map
[i
].dkl_cylno
=
2927 partitions
->efi_gpe_StartingLBA
;
2928 cl
->cl_map
[i
].dkl_nblk
=
2929 partitions
->efi_gpe_EndingLBA
-
2930 partitions
->efi_gpe_StartingLBA
+ 1;
2932 partitions
->efi_gpe_StartingLBA
;
2935 if (cl
->cl_reserved
== -1) {
2936 if (bcmp(&partitions
->efi_gpe_PartitionTypeGUID
,
2937 &uuid_type_reserved
, sizeof (struct uuid
)) == 0) {
2938 cl
->cl_reserved
= i
;
2943 * minor number 7 corresponds to the whole disk
2944 * if the disk capacity is expanded after disk is
2945 * labeled, minor number 7 represents the capacity
2946 * indicated by the disk label.
2948 cl
->cl_map
[i
].dkl_cylno
= 0;
2949 if (alternate_lba
== 1) {
2951 * We are using backup label. Since we can
2952 * find a valid label at the end of disk,
2953 * the disk capacity is not expanded.
2955 cl
->cl_map
[i
].dkl_nblk
= capacity
;
2957 cl
->cl_map
[i
].dkl_nblk
= alternate_lba
+ 1;
2959 cl
->cl_offset
[i
] = 0;
2963 cl
->cl_solaris_offset
= 0;
2964 cl
->cl_solaris_size
= capacity
;
2965 cl
->cl_label_from_media
= CMLB_LABEL_EFI
;
2966 cl
->cl_f_geometry_is_valid
= B_TRUE
;
2968 /* clear the vtoc label */
2969 bzero(&cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
2971 kmem_free(buf
, EFI_MIN_ARRAY_SIZE
);
2975 kmem_free(buf
, EFI_MIN_ARRAY_SIZE
);
2976 mutex_enter(CMLB_MUTEX(cl
));
2979 * if we didn't find something that could look like a VTOC
2980 * and the disk is over 1TB, we know there isn't a valid label.
2981 * Otherwise let cmlb_uselabel decide what to do. We only
2982 * want to invalidate this if we're certain the label isn't
2983 * valid because cmlb_prop_op will now fail, which in turn
2984 * causes things like opens and stats on the partition to fail.
2986 if ((capacity
> CMLB_EXTVTOC_LIMIT
) && (rval
!= ESRCH
) && !iofailed
) {
2987 cl
->cl_f_geometry_is_valid
= B_FALSE
;
2994 * Function: cmlb_uselabel
2996 * Description: Validate the disk label and update the relevant data (geometry,
2997 * partition, vtoc, and capacity data) in the cmlb_lun struct.
2998 * Marks the geometry of the unit as being valid.
3000 * Arguments: cl: unit struct.
3001 * dk_label: disk label
3003 * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
3004 * partition, vtoc, and capacity data are good.
3006 * CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
3007 * label; or computed capacity does not jibe with capacity
3008 * reported from the READ CAPACITY command.
3010 * Context: Kernel thread only (can sleep).
3013 cmlb_uselabel(struct cmlb_lun
*cl
, struct dk_label
*labp
, int flags
)
3018 int label_error
= CMLB_LABEL_IS_VALID
;
3020 diskaddr_t label_capacity
;
3022 diskaddr_t track_capacity
;
3023 #if defined(_SUNOS_VTOC_16)
3024 struct dkl_partition
*vpartp
;
3027 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
3029 /* Validate the magic number of the label. */
3030 if (labp
->dkl_magic
!= DKL_MAGIC
) {
3031 #if defined(__sparc)
3032 if (!ISREMOVABLE(cl
) && !ISHOTPLUGGABLE(cl
)) {
3033 if (!(flags
& CMLB_SILENT
))
3034 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
),
3036 "Corrupt label; wrong magic number\n");
3039 return (CMLB_LABEL_IS_INVALID
);
3042 /* Validate the checksum of the label. */
3045 count
= sizeof (struct dk_label
) / sizeof (short);
3051 #if defined(_SUNOS_VTOC_16)
3053 #elif defined(_SUNOS_VTOC_8)
3054 if (!ISREMOVABLE(cl
) && !ISHOTPLUGGABLE(cl
)) {
3056 if (!(flags
& CMLB_SILENT
))
3057 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
),
3059 "Corrupt label - label checksum failed\n");
3061 return (CMLB_LABEL_IS_INVALID
);
3066 * Fill in geometry structure with data from label.
3068 bzero(&cl
->cl_g
, sizeof (struct dk_geom
));
3069 cl
->cl_g
.dkg_ncyl
= labp
->dkl_ncyl
;
3070 cl
->cl_g
.dkg_acyl
= labp
->dkl_acyl
;
3071 cl
->cl_g
.dkg_bcyl
= 0;
3072 cl
->cl_g
.dkg_nhead
= labp
->dkl_nhead
;
3073 cl
->cl_g
.dkg_nsect
= labp
->dkl_nsect
;
3074 cl
->cl_g
.dkg_intrlv
= labp
->dkl_intrlv
;
3076 #if defined(_SUNOS_VTOC_8)
3077 cl
->cl_g
.dkg_gap1
= labp
->dkl_gap1
;
3078 cl
->cl_g
.dkg_gap2
= labp
->dkl_gap2
;
3079 cl
->cl_g
.dkg_bhead
= labp
->dkl_bhead
;
3081 #if defined(_SUNOS_VTOC_16)
3082 cl
->cl_dkg_skew
= labp
->dkl_skew
;
3085 #if defined(__i386) || defined(__amd64)
3086 cl
->cl_g
.dkg_apc
= labp
->dkl_apc
;
3090 * Currently we rely on the values in the label being accurate. If
3091 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
3093 * Note: In the future a MODE SENSE may be used to retrieve this data,
3094 * although this command is optional in SCSI-2.
3096 cl
->cl_g
.dkg_rpm
= (labp
->dkl_rpm
!= 0) ? labp
->dkl_rpm
: 3600;
3097 cl
->cl_g
.dkg_pcyl
= (labp
->dkl_pcyl
!= 0) ? labp
->dkl_pcyl
:
3098 (cl
->cl_g
.dkg_ncyl
+ cl
->cl_g
.dkg_acyl
);
3101 * The Read and Write reinstruct values may not be valid
3104 cl
->cl_g
.dkg_read_reinstruct
= labp
->dkl_read_reinstruct
;
3105 cl
->cl_g
.dkg_write_reinstruct
= labp
->dkl_write_reinstruct
;
3107 /* Fill in partition table. */
3108 #if defined(_SUNOS_VTOC_8)
3109 for (i
= 0; i
< NDKMAP
; i
++) {
3110 cl
->cl_map
[i
].dkl_cylno
= labp
->dkl_map
[i
].dkl_cylno
;
3111 cl
->cl_map
[i
].dkl_nblk
= labp
->dkl_map
[i
].dkl_nblk
;
3114 #if defined(_SUNOS_VTOC_16)
3115 vpartp
= labp
->dkl_vtoc
.v_part
;
3116 track_capacity
= labp
->dkl_nhead
* labp
->dkl_nsect
;
3118 /* Prevent divide by zero */
3119 if (track_capacity
== 0) {
3120 if (!(flags
& CMLB_SILENT
))
3121 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
), CE_WARN
,
3122 "Corrupt label - zero nhead or nsect value\n");
3124 return (CMLB_LABEL_IS_INVALID
);
3127 for (i
= 0; i
< NDKMAP
; i
++, vpartp
++) {
3128 cl
->cl_map
[i
].dkl_cylno
= vpartp
->p_start
/ track_capacity
;
3129 cl
->cl_map
[i
].dkl_nblk
= vpartp
->p_size
;
3133 /* Fill in VTOC Structure. */
3134 bcopy(&labp
->dkl_vtoc
, &cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
3135 #if defined(_SUNOS_VTOC_8)
3137 * The 8-slice vtoc does not include the ascii label; save it into
3138 * the device's soft state structure here.
3140 bcopy(labp
->dkl_asciilabel
, cl
->cl_asciilabel
, LEN_DKL_ASCII
);
3143 /* Now look for a valid capacity. */
3144 track_capacity
= (cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
);
3145 label_capacity
= (cl
->cl_g
.dkg_ncyl
* track_capacity
);
3147 if (cl
->cl_g
.dkg_acyl
) {
3148 #if defined(__i386) || defined(__amd64)
3149 /* we may have > 1 alts cylinder */
3150 label_capacity
+= (track_capacity
* cl
->cl_g
.dkg_acyl
);
3152 label_capacity
+= track_capacity
;
3157 * Force check here to ensure the computed capacity is valid.
3158 * If capacity is zero, it indicates an invalid label and
3159 * we should abort updating the relevant data then.
3161 if (label_capacity
== 0) {
3162 if (!(flags
& CMLB_SILENT
))
3163 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
), CE_WARN
,
3164 "Corrupt label - no valid capacity could be "
3167 return (CMLB_LABEL_IS_INVALID
);
3170 /* Mark the geometry as valid. */
3171 cl
->cl_f_geometry_is_valid
= B_TRUE
;
3174 * if we got invalidated when mutex exit and entered again,
3175 * if blockcount different than when we came in, need to
3176 * retry from beginning of cmlb_validate_geometry.
3177 * revisit this on next phase of utilizing this for
3181 if (label_capacity
<= cl
->cl_blockcount
) {
3182 #if defined(_SUNOS_VTOC_8)
3184 * We can't let this happen on drives that are subdivided
3185 * into logical disks (i.e., that have an fdisk table).
3186 * The cl_blockcount field should always hold the full media
3187 * size in sectors, period. This code would overwrite
3188 * cl_blockcount with the size of the Solaris fdisk partition.
3190 cmlb_dbg(CMLB_ERROR
, cl
,
3191 "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3192 label_capacity
, cl
->cl_blockcount
);
3193 cl
->cl_solaris_size
= label_capacity
;
3195 #endif /* defined(_SUNOS_VTOC_8) */
3200 /* For CDROMs, we trust that the data in the label is OK. */
3201 #if defined(_SUNOS_VTOC_8)
3202 for (i
= 0; i
< NDKMAP
; i
++) {
3203 part_end
= labp
->dkl_nhead
* labp
->dkl_nsect
*
3204 labp
->dkl_map
[i
].dkl_cylno
+
3205 labp
->dkl_map
[i
].dkl_nblk
- 1;
3207 if ((labp
->dkl_map
[i
].dkl_nblk
) &&
3208 (part_end
> cl
->cl_blockcount
)) {
3209 cl
->cl_f_geometry_is_valid
= B_FALSE
;
3214 #if defined(_SUNOS_VTOC_16)
3215 vpartp
= &(labp
->dkl_vtoc
.v_part
[0]);
3216 for (i
= 0; i
< NDKMAP
; i
++, vpartp
++) {
3217 part_end
= vpartp
->p_start
+ vpartp
->p_size
;
3218 if ((vpartp
->p_size
> 0) &&
3219 (part_end
> cl
->cl_blockcount
)) {
3220 cl
->cl_f_geometry_is_valid
= B_FALSE
;
3226 /* label_capacity > cl->cl_blockcount */
3227 if (!(flags
& CMLB_SILENT
)) {
3228 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
), CE_WARN
,
3229 "Corrupt label - bad geometry\n");
3230 cmlb_log(CMLB_DEVINFO(cl
), CMLB_LABEL(cl
), CE_CONT
,
3231 "Label says %llu blocks; Drive says %llu blocks\n",
3232 label_capacity
, cl
->cl_blockcount
);
3234 cl
->cl_f_geometry_is_valid
= B_FALSE
;
3235 label_error
= CMLB_LABEL_IS_INVALID
;
3240 cmlb_dbg(CMLB_INFO
, cl
, "cmlb_uselabel: (label geometry)\n");
3241 cmlb_dbg(CMLB_INFO
, cl
,
3242 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3243 cl
->cl_g
.dkg_ncyl
, cl
->cl_g
.dkg_acyl
,
3244 cl
->cl_g
.dkg_nhead
, cl
->cl_g
.dkg_nsect
);
3246 cmlb_dbg(CMLB_INFO
, cl
,
3247 " label_capacity: %d; intrlv: %d; rpm: %d\n",
3248 cl
->cl_blockcount
, cl
->cl_g
.dkg_intrlv
, cl
->cl_g
.dkg_rpm
);
3249 cmlb_dbg(CMLB_INFO
, cl
, " wrt_reinstr: %d; rd_reinstr: %d\n",
3250 cl
->cl_g
.dkg_write_reinstruct
, cl
->cl_g
.dkg_read_reinstruct
);
3252 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
3254 return (label_error
);
3259 * Function: cmlb_build_default_label
3261 * Description: Generate a default label for those devices that do not have
3262 * one, e.g., new media, removable cartridges, etc..
3264 * Context: Kernel thread only
3268 cmlb_build_default_label(struct cmlb_lun
*cl
, void *tg_cookie
)
3270 #if defined(_SUNOS_VTOC_16)
3273 struct dk_geom cl_g
;
3274 diskaddr_t capacity
;
3278 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
3280 #if defined(_SUNOS_VTOC_8)
3282 * Note: This is a legacy check for non-removable devices on VTOC_8
3283 * only. This may be a valid check for VTOC_16 as well.
3284 * Once we understand why there is this difference between SPARC and
3285 * x86 platform, we could remove this legacy check.
3287 if (!ISREMOVABLE(cl
) && !ISHOTPLUGGABLE(cl
)) {
3292 bzero(&cl
->cl_g
, sizeof (struct dk_geom
));
3293 bzero(&cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
3294 bzero(&cl
->cl_map
, NDKMAP
* (sizeof (struct dk_map
)));
3296 #if defined(_SUNOS_VTOC_8)
3299 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
3300 * But it is still necessary to set up various geometry information,
3301 * and we are doing this here.
3305 * For the rpm, we use the minimum for the disk. For the head, cyl,
3306 * and number of sector per track, if the capacity <= 1GB, head = 64,
3307 * sect = 32. else head = 255, sect 63 Note: the capacity should be
3308 * equal to C*H*S values. This will cause some truncation of size due
3309 * to round off errors. For CD-ROMs, this truncation can have adverse
3310 * side effects, so returning ncyl and nhead as 1. The nsect will
3311 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
3313 cl
->cl_solaris_size
= cl
->cl_blockcount
;
3315 tg_attribute_t tgattribute
;
3318 * Preserve the old behavior for non-writable
3319 * medias. Since dkg_nsect is a ushort, it
3320 * will lose bits as cdroms have more than
3321 * 65536 sectors. So if we recalculate
3322 * capacity, it will become much shorter.
3323 * But the dkg_* information is not
3324 * used for CDROMs so it is OK. But for
3325 * Writable CDs we need this information
3326 * to be valid (for newfs say). So we
3327 * make nsect and nhead > 1 that way
3328 * nsect can still stay within ushort limit
3329 * without losing any bits.
3332 bzero(&tgattribute
, sizeof (tg_attribute_t
));
3334 mutex_exit(CMLB_MUTEX(cl
));
3336 (DK_TG_GETATTRIBUTE(cl
, &tgattribute
, tg_cookie
) == 0) ?
3337 tgattribute
.media_is_writable
: 1;
3338 mutex_enter(CMLB_MUTEX(cl
));
3341 cl
->cl_g
.dkg_nhead
= 64;
3342 cl
->cl_g
.dkg_nsect
= 32;
3343 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
/ (64 * 32);
3344 cl
->cl_solaris_size
= (diskaddr_t
)cl
->cl_g
.dkg_ncyl
*
3345 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
;
3347 cl
->cl_g
.dkg_ncyl
= 1;
3348 cl
->cl_g
.dkg_nhead
= 1;
3349 cl
->cl_g
.dkg_nsect
= cl
->cl_blockcount
;
3352 if (cl
->cl_blockcount
< 160) {
3354 cl
->cl_g
.dkg_nhead
= 1;
3355 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
;
3356 cl
->cl_g
.dkg_nsect
= 1;
3357 } else if (cl
->cl_blockcount
<= 0x1000) {
3358 /* unlabeled SCSI floppy device */
3359 cl
->cl_g
.dkg_nhead
= 2;
3360 cl
->cl_g
.dkg_ncyl
= 80;
3361 cl
->cl_g
.dkg_nsect
= cl
->cl_blockcount
/ (2 * 80);
3362 } else if (cl
->cl_blockcount
<= 0x200000) {
3363 cl
->cl_g
.dkg_nhead
= 64;
3364 cl
->cl_g
.dkg_nsect
= 32;
3365 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
/ (64 * 32);
3367 cl
->cl_g
.dkg_nhead
= 255;
3369 cl
->cl_g
.dkg_nsect
= ((cl
->cl_blockcount
+
3370 (UINT16_MAX
* 255 * 63) - 1) /
3371 (UINT16_MAX
* 255 * 63)) * 63;
3373 if (cl
->cl_g
.dkg_nsect
== 0)
3374 cl
->cl_g
.dkg_nsect
= (UINT16_MAX
/ 63) * 63;
3376 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
/
3377 (255 * cl
->cl_g
.dkg_nsect
);
3380 cl
->cl_solaris_size
=
3381 (diskaddr_t
)cl
->cl_g
.dkg_ncyl
* cl
->cl_g
.dkg_nhead
*
3386 cl
->cl_g
.dkg_acyl
= 0;
3387 cl
->cl_g
.dkg_bcyl
= 0;
3388 cl
->cl_g
.dkg_rpm
= 200;
3389 cl
->cl_asciilabel
[0] = '\0';
3390 cl
->cl_g
.dkg_pcyl
= cl
->cl_g
.dkg_ncyl
;
3392 cl
->cl_map
[0].dkl_cylno
= 0;
3393 cl
->cl_map
[0].dkl_nblk
= cl
->cl_solaris_size
;
3395 cl
->cl_map
[2].dkl_cylno
= 0;
3396 cl
->cl_map
[2].dkl_nblk
= cl
->cl_solaris_size
;
3398 #elif defined(_SUNOS_VTOC_16)
3400 if (cl
->cl_solaris_size
== 0) {
3402 * Got fdisk table but no solaris entry therefore
3403 * don't create a default label
3405 cl
->cl_f_geometry_is_valid
= B_TRUE
;
3410 * For CDs we continue to use the physical geometry to calculate
3411 * number of cylinders. All other devices must convert the
3412 * physical geometry (cmlb_geom) to values that will fit
3413 * in a dk_geom structure.
3416 phys_spc
= cl
->cl_pgeom
.g_nhead
* cl
->cl_pgeom
.g_nsect
;
3418 /* Convert physical geometry to disk geometry */
3419 bzero(&cl_g
, sizeof (struct dk_geom
));
3422 * Refer to comments related to off-by-1 at the
3423 * header of this file.
3424 * Before calculating geometry, capacity should be
3428 if (cl
->cl_alter_behavior
& CMLB_OFF_BY_ONE
)
3429 capacity
= cl
->cl_blockcount
- 1;
3431 capacity
= cl
->cl_blockcount
;
3434 cmlb_convert_geometry(cl
, capacity
, &cl_g
, tg_cookie
);
3435 bcopy(&cl_g
, &cl
->cl_g
, sizeof (cl
->cl_g
));
3436 phys_spc
= cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
;
3441 cl
->cl_g
.dkg_pcyl
= cl
->cl_solaris_size
/ phys_spc
;
3442 if (cl
->cl_alter_behavior
& CMLB_FAKE_LABEL_ONE_PARTITION
) {
3444 cl
->cl_g
.dkg_ncyl
= cl
->cl_g
.dkg_pcyl
;
3445 disksize
= cl
->cl_solaris_size
;
3447 cl
->cl_g
.dkg_acyl
= DK_ACYL
;
3448 cl
->cl_g
.dkg_ncyl
= cl
->cl_g
.dkg_pcyl
- DK_ACYL
;
3449 disksize
= cl
->cl_g
.dkg_ncyl
* phys_spc
;
3454 * CD's don't use the "heads * sectors * cyls"-type of
3455 * geometry, but instead use the entire capacity of the media.
3457 disksize
= cl
->cl_solaris_size
;
3458 cl
->cl_g
.dkg_nhead
= 1;
3459 cl
->cl_g
.dkg_nsect
= 1;
3461 (cl
->cl_pgeom
.g_rpm
== 0) ? 200 : cl
->cl_pgeom
.g_rpm
;
3463 cl
->cl_vtoc
.v_part
[0].p_start
= 0;
3464 cl
->cl_vtoc
.v_part
[0].p_size
= disksize
;
3465 cl
->cl_vtoc
.v_part
[0].p_tag
= V_BACKUP
;
3466 cl
->cl_vtoc
.v_part
[0].p_flag
= V_UNMNT
;
3468 cl
->cl_map
[0].dkl_cylno
= 0;
3469 cl
->cl_map
[0].dkl_nblk
= disksize
;
3470 cl
->cl_offset
[0] = 0;
3474 * Hard disks and removable media cartridges
3477 (cl
->cl_pgeom
.g_rpm
== 0) ? 3600: cl
->cl_pgeom
.g_rpm
;
3478 cl
->cl_vtoc
.v_sectorsz
= cl
->cl_sys_blocksize
;
3480 /* Add boot slice */
3481 cl
->cl_vtoc
.v_part
[8].p_start
= 0;
3482 cl
->cl_vtoc
.v_part
[8].p_size
= phys_spc
;
3483 cl
->cl_vtoc
.v_part
[8].p_tag
= V_BOOT
;
3484 cl
->cl_vtoc
.v_part
[8].p_flag
= V_UNMNT
;
3486 cl
->cl_map
[8].dkl_cylno
= 0;
3487 cl
->cl_map
[8].dkl_nblk
= phys_spc
;
3488 cl
->cl_offset
[8] = 0;
3490 if ((cl
->cl_alter_behavior
&
3491 CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT
) &&
3492 cl
->cl_device_type
== DTYPE_DIRECT
) {
3493 cl
->cl_vtoc
.v_part
[9].p_start
= phys_spc
;
3494 cl
->cl_vtoc
.v_part
[9].p_size
= 2 * phys_spc
;
3495 cl
->cl_vtoc
.v_part
[9].p_tag
= V_ALTSCTR
;
3496 cl
->cl_vtoc
.v_part
[9].p_flag
= 0;
3498 cl
->cl_map
[9].dkl_cylno
= 1;
3499 cl
->cl_map
[9].dkl_nblk
= 2 * phys_spc
;
3500 cl
->cl_offset
[9] = phys_spc
;
3504 cl
->cl_g
.dkg_apc
= 0;
3506 /* Add backup slice */
3507 cl
->cl_vtoc
.v_part
[2].p_start
= 0;
3508 cl
->cl_vtoc
.v_part
[2].p_size
= disksize
;
3509 cl
->cl_vtoc
.v_part
[2].p_tag
= V_BACKUP
;
3510 cl
->cl_vtoc
.v_part
[2].p_flag
= V_UNMNT
;
3512 cl
->cl_map
[2].dkl_cylno
= 0;
3513 cl
->cl_map
[2].dkl_nblk
= disksize
;
3514 cl
->cl_offset
[2] = 0;
3517 * single slice (s0) covering the entire disk
3519 if (cl
->cl_alter_behavior
& CMLB_FAKE_LABEL_ONE_PARTITION
) {
3520 cl
->cl_vtoc
.v_part
[0].p_start
= 0;
3521 cl
->cl_vtoc
.v_part
[0].p_tag
= V_UNASSIGNED
;
3522 cl
->cl_vtoc
.v_part
[0].p_flag
= 0;
3523 cl
->cl_vtoc
.v_part
[0].p_size
= disksize
;
3524 cl
->cl_map
[0].dkl_cylno
= 0;
3525 cl
->cl_map
[0].dkl_nblk
= disksize
;
3526 cl
->cl_offset
[0] = 0;
3529 (void) sprintf(cl
->cl_vtoc
.v_asciilabel
, "DEFAULT cyl %d alt %d"
3530 " hd %d sec %d", cl
->cl_g
.dkg_ncyl
, cl
->cl_g
.dkg_acyl
,
3531 cl
->cl_g
.dkg_nhead
, cl
->cl_g
.dkg_nsect
);
3534 #error "No VTOC format defined."
3537 cl
->cl_g
.dkg_read_reinstruct
= 0;
3538 cl
->cl_g
.dkg_write_reinstruct
= 0;
3540 cl
->cl_g
.dkg_intrlv
= 1;
3542 cl
->cl_vtoc
.v_sanity
= VTOC_SANE
;
3543 cl
->cl_vtoc
.v_nparts
= V_NUMPAR
;
3544 cl
->cl_vtoc
.v_version
= V_VERSION
;
3546 cl
->cl_f_geometry_is_valid
= B_TRUE
;
3547 cl
->cl_label_from_media
= CMLB_LABEL_UNDEF
;
3549 cmlb_dbg(CMLB_INFO
, cl
,
3550 "cmlb_build_default_label: Default label created: "
3551 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3552 cl
->cl_g
.dkg_ncyl
, cl
->cl_g
.dkg_acyl
, cl
->cl_g
.dkg_nhead
,
3553 cl
->cl_g
.dkg_nsect
, cl
->cl_blockcount
);
3557 #if defined(_FIRMWARE_NEEDS_FDISK)
3559 * Max CHS values, as they are encoded into bytes, for 1022/254/63
3561 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2))
3562 #define LBA_MAX_CYL (1022 & 0xFF)
3563 #define LBA_MAX_HEAD (254)
3567 * Function: cmlb_has_max_chs_vals
3569 * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
3571 * Arguments: fdp - ptr to CHS info
3573 * Return Code: True or false
3578 cmlb_has_max_chs_vals(struct ipart
*fdp
)
3580 return ((fdp
->begcyl
== LBA_MAX_CYL
) &&
3581 (fdp
->beghead
== LBA_MAX_HEAD
) &&
3582 (fdp
->begsect
== LBA_MAX_SECT
) &&
3583 (fdp
->endcyl
== LBA_MAX_CYL
) &&
3584 (fdp
->endhead
== LBA_MAX_HEAD
) &&
3585 (fdp
->endsect
== LBA_MAX_SECT
));
3590 * Function: cmlb_dkio_get_geometry
3592 * Description: This routine is the driver entry point for handling user
3593 * requests to get the device geometry (DKIOCGGEOM).
3596 * arg pointer to user provided dk_geom structure specifying
3597 * the controller's notion of the current geometry.
3599 * flag this argument is a pass through to ddi_copyxxx()
3600 * directly from the mode argument of ioctl().
3602 * tg_cookie cookie from target driver to be passed back to target
3603 * driver when we call back to it through tg_ops.
3611 cmlb_dkio_get_geometry(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
3614 struct dk_geom
*tmp_geom
= NULL
;
3618 * cmlb_validate_geometry does not spin a disk up
3619 * if it was spcl down. We need to make sure it
3622 mutex_enter(CMLB_MUTEX(cl
));
3623 rval
= cmlb_validate_geometry(cl
, B_TRUE
, 0, tg_cookie
);
3624 #if defined(_SUNOS_VTOC_8)
3625 if (rval
== EINVAL
&&
3626 cl
->cl_alter_behavior
& CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8
) {
3628 * This is to return a default label geometry even when we
3629 * do not really assume a default label for the device.
3630 * dad driver utilizes this.
3632 if (cl
->cl_blockcount
<= CMLB_OLDVTOC_LIMIT
) {
3633 cmlb_setup_default_geometry(cl
, tg_cookie
);
3639 mutex_exit(CMLB_MUTEX(cl
));
3643 #if defined(__i386) || defined(__amd64)
3644 if (cl
->cl_solaris_size
== 0) {
3645 mutex_exit(CMLB_MUTEX(cl
));
3651 * Make a local copy of the soft state geometry to avoid some potential
3652 * race conditions associated with holding the mutex and updating the
3653 * write_reinstruct value
3655 tmp_geom
= kmem_zalloc(sizeof (struct dk_geom
), KM_SLEEP
);
3656 bcopy(&cl
->cl_g
, tmp_geom
, sizeof (struct dk_geom
));
3658 if (tmp_geom
->dkg_write_reinstruct
== 0) {
3659 tmp_geom
->dkg_write_reinstruct
=
3660 (int)((int)(tmp_geom
->dkg_nsect
* tmp_geom
->dkg_rpm
*
3661 cmlb_rot_delay
) / (int)60000);
3663 mutex_exit(CMLB_MUTEX(cl
));
3665 rval
= ddi_copyout(tmp_geom
, (void *)arg
, sizeof (struct dk_geom
),
3671 kmem_free(tmp_geom
, sizeof (struct dk_geom
));
3678 * Function: cmlb_dkio_set_geometry
3680 * Description: This routine is the driver entry point for handling user
3681 * requests to set the device geometry (DKIOCSGEOM). The actual
3682 * device geometry is not updated, just the driver "notion" of it.
3685 * arg pointer to user provided dk_geom structure used to set
3686 * the controller's notion of the current geometry.
3688 * flag this argument is a pass through to ddi_copyxxx()
3689 * directly from the mode argument of ioctl().
3691 * tg_cookie cookie from target driver to be passed back to target
3692 * driver when we call back to it through tg_ops.
3700 cmlb_dkio_set_geometry(struct cmlb_lun
*cl
, caddr_t arg
, int flag
)
3702 struct dk_geom
*tmp_geom
;
3708 #if defined(__i386) || defined(__amd64)
3709 if (cl
->cl_solaris_size
== 0) {
3714 * We need to copy the user specified geometry into local
3715 * storage and then update the softstate. We don't want to hold
3716 * the mutex and copyin directly from the user to the soft state
3718 tmp_geom
= (struct dk_geom
*)
3719 kmem_zalloc(sizeof (struct dk_geom
), KM_SLEEP
);
3720 rval
= ddi_copyin(arg
, tmp_geom
, sizeof (struct dk_geom
), flag
);
3722 kmem_free(tmp_geom
, sizeof (struct dk_geom
));
3726 mutex_enter(CMLB_MUTEX(cl
));
3727 bcopy(tmp_geom
, &cl
->cl_g
, sizeof (struct dk_geom
));
3728 for (i
= 0; i
< NDKMAP
; i
++) {
3729 lp
= &cl
->cl_map
[i
];
3731 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
* lp
->dkl_cylno
;
3732 #if defined(__i386) || defined(__amd64)
3733 cl
->cl_offset
[i
] += cl
->cl_solaris_offset
;
3736 cl
->cl_f_geometry_is_valid
= B_FALSE
;
3737 mutex_exit(CMLB_MUTEX(cl
));
3738 kmem_free(tmp_geom
, sizeof (struct dk_geom
));
3744 * Function: cmlb_dkio_get_partition
3746 * Description: This routine is the driver entry point for handling user
3747 * requests to get the partition table (DKIOCGAPART).
3750 * arg pointer to user provided dk_allmap structure specifying
3751 * the controller's notion of the current partition table.
3753 * flag this argument is a pass through to ddi_copyxxx()
3754 * directly from the mode argument of ioctl().
3756 * tg_cookie cookie from target driver to be passed back to target
3757 * driver when we call back to it through tg_ops.
3765 cmlb_dkio_get_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
3772 * Make sure the geometry is valid before getting the partition
3775 mutex_enter(CMLB_MUTEX(cl
));
3776 if ((rval
= cmlb_validate_geometry(cl
, B_TRUE
, 0, tg_cookie
)) != 0) {
3777 mutex_exit(CMLB_MUTEX(cl
));
3780 mutex_exit(CMLB_MUTEX(cl
));
3782 #if defined(__i386) || defined(__amd64)
3783 if (cl
->cl_solaris_size
== 0) {
3788 #ifdef _MULTI_DATAMODEL
3789 switch (ddi_model_convert_from(flag
& FMODELS
)) {
3790 case DDI_MODEL_ILP32
: {
3791 struct dk_map32 dk_map32
[NDKMAP
];
3794 for (i
= 0; i
< NDKMAP
; i
++) {
3795 dk_map32
[i
].dkl_cylno
= cl
->cl_map
[i
].dkl_cylno
;
3796 dk_map32
[i
].dkl_nblk
= cl
->cl_map
[i
].dkl_nblk
;
3798 size
= NDKMAP
* sizeof (struct dk_map32
);
3799 rval
= ddi_copyout(dk_map32
, (void *)arg
, size
, flag
);
3805 case DDI_MODEL_NONE
:
3806 size
= NDKMAP
* sizeof (struct dk_map
);
3807 rval
= ddi_copyout(cl
->cl_map
, (void *)arg
, size
, flag
);
3813 #else /* ! _MULTI_DATAMODEL */
3814 size
= NDKMAP
* sizeof (struct dk_map
);
3815 rval
= ddi_copyout(cl
->cl_map
, (void *)arg
, size
, flag
);
3819 #endif /* _MULTI_DATAMODEL */
3824 * Function: cmlb_dkio_set_partition
3826 * Description: This routine is the driver entry point for handling user
3827 * requests to set the partition table (DKIOCSAPART). The actual
3828 * device partition is not updated.
3831 * arg - pointer to user provided dk_allmap structure used to set
3832 * the controller's notion of the partition table.
3833 * flag - this argument is a pass through to ddi_copyxxx()
3834 * directly from the mode argument of ioctl().
3843 cmlb_dkio_set_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
)
3845 struct dk_map dk_map
[NDKMAP
];
3850 #if defined(_SUNOS_VTOC_16)
3851 struct dkl_partition
*vp
;
3855 * Set the map for all logical partitions. We lock
3856 * the priority just to make sure an interrupt doesn't
3857 * come in while the map is half updated.
3859 _NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size
))
3860 mutex_enter(CMLB_MUTEX(cl
));
3862 if (cl
->cl_blockcount
> CMLB_OLDVTOC_LIMIT
) {
3863 mutex_exit(CMLB_MUTEX(cl
));
3866 mutex_exit(CMLB_MUTEX(cl
));
3867 if (cl
->cl_solaris_size
== 0) {
3871 #ifdef _MULTI_DATAMODEL
3872 switch (ddi_model_convert_from(flag
& FMODELS
)) {
3873 case DDI_MODEL_ILP32
: {
3874 struct dk_map32 dk_map32
[NDKMAP
];
3876 size
= NDKMAP
* sizeof (struct dk_map32
);
3877 rval
= ddi_copyin((void *)arg
, dk_map32
, size
, flag
);
3881 for (i
= 0; i
< NDKMAP
; i
++) {
3882 dk_map
[i
].dkl_cylno
= dk_map32
[i
].dkl_cylno
;
3883 dk_map
[i
].dkl_nblk
= dk_map32
[i
].dkl_nblk
;
3887 case DDI_MODEL_NONE
:
3888 size
= NDKMAP
* sizeof (struct dk_map
);
3889 rval
= ddi_copyin((void *)arg
, dk_map
, size
, flag
);
3895 #else /* ! _MULTI_DATAMODEL */
3896 size
= NDKMAP
* sizeof (struct dk_map
);
3897 rval
= ddi_copyin((void *)arg
, dk_map
, size
, flag
);
3901 #endif /* _MULTI_DATAMODEL */
3903 mutex_enter(CMLB_MUTEX(cl
));
3904 /* Note: The size used in this bcopy is set based upon the data model */
3905 bcopy(dk_map
, cl
->cl_map
, size
);
3906 #if defined(_SUNOS_VTOC_16)
3907 vp
= (struct dkl_partition
*)&(cl
->cl_vtoc
);
3908 #endif /* defined(_SUNOS_VTOC_16) */
3909 for (i
= 0; i
< NDKMAP
; i
++) {
3910 lp
= &cl
->cl_map
[i
];
3912 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
* lp
->dkl_cylno
;
3913 #if defined(_SUNOS_VTOC_16)
3914 vp
->p_start
= cl
->cl_offset
[i
];
3915 vp
->p_size
= lp
->dkl_nblk
;
3917 #endif /* defined(_SUNOS_VTOC_16) */
3918 #if defined(__i386) || defined(__amd64)
3919 cl
->cl_offset
[i
] += cl
->cl_solaris_offset
;
3922 mutex_exit(CMLB_MUTEX(cl
));
3928 * Function: cmlb_dkio_get_vtoc
3930 * Description: This routine is the driver entry point for handling user
3931 * requests to get the current volume table of contents
3935 * arg pointer to user provided vtoc structure specifying
3938 * flag this argument is a pass through to ddi_copyxxx()
3939 * directly from the mode argument of ioctl().
3941 * tg_cookie cookie from target driver to be passed back to target
3942 * driver when we call back to it through tg_ops.
3950 cmlb_dkio_get_vtoc(struct cmlb_lun
*cl
, caddr_t arg
, int flag
, void *tg_cookie
)
3952 #if defined(_SUNOS_VTOC_8)
3953 struct vtoc user_vtoc
;
3954 #endif /* defined(_SUNOS_VTOC_8) */
3957 mutex_enter(CMLB_MUTEX(cl
));
3958 if (cl
->cl_blockcount
> CMLB_OLDVTOC_LIMIT
) {
3959 mutex_exit(CMLB_MUTEX(cl
));
3963 rval
= cmlb_validate_geometry(cl
, B_TRUE
, 0, tg_cookie
);
3965 #if defined(_SUNOS_VTOC_8)
3966 if (rval
== EINVAL
&&
3967 (cl
->cl_alter_behavior
& CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8
)) {
3969 * This is to return a default label even when we do not
3970 * really assume a default label for the device.
3971 * dad driver utilizes this.
3973 if (cl
->cl_blockcount
<= CMLB_OLDVTOC_LIMIT
) {
3974 cmlb_setup_default_geometry(cl
, tg_cookie
);
3980 mutex_exit(CMLB_MUTEX(cl
));
3984 #if defined(_SUNOS_VTOC_8)
3985 cmlb_build_user_vtoc(cl
, &user_vtoc
);
3986 mutex_exit(CMLB_MUTEX(cl
));
3988 #ifdef _MULTI_DATAMODEL
3989 switch (ddi_model_convert_from(flag
& FMODELS
)) {
3990 case DDI_MODEL_ILP32
: {
3991 struct vtoc32 user_vtoc32
;
3993 vtoctovtoc32(user_vtoc
, user_vtoc32
);
3994 if (ddi_copyout(&user_vtoc32
, (void *)arg
,
3995 sizeof (struct vtoc32
), flag
)) {
4001 case DDI_MODEL_NONE
:
4002 if (ddi_copyout(&user_vtoc
, (void *)arg
,
4003 sizeof (struct vtoc
), flag
)) {
4008 #else /* ! _MULTI_DATAMODEL */
4009 if (ddi_copyout(&user_vtoc
, (void *)arg
, sizeof (struct vtoc
), flag
)) {
4012 #endif /* _MULTI_DATAMODEL */
4014 #elif defined(_SUNOS_VTOC_16)
4015 mutex_exit(CMLB_MUTEX(cl
));
4017 #ifdef _MULTI_DATAMODEL
4019 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4020 * 32-bit to maintain compatibility with existing on-disk
4021 * structures. Thus, we need to convert the structure when copying
4022 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
4023 * program. If the target is a 32-bit program, then no conversion
4026 /* LINTED: logical expression always true: op "||" */
4027 ASSERT(sizeof (cl
->cl_vtoc
) == sizeof (struct vtoc32
));
4028 switch (ddi_model_convert_from(flag
& FMODELS
)) {
4029 case DDI_MODEL_ILP32
:
4030 if (ddi_copyout(&(cl
->cl_vtoc
), (void *)arg
,
4031 sizeof (cl
->cl_vtoc
), flag
)) {
4036 case DDI_MODEL_NONE
: {
4037 struct vtoc user_vtoc
;
4039 vtoc32tovtoc(cl
->cl_vtoc
, user_vtoc
);
4040 if (ddi_copyout(&user_vtoc
, (void *)arg
,
4041 sizeof (struct vtoc
), flag
)) {
4047 #else /* ! _MULTI_DATAMODEL */
4048 if (ddi_copyout(&(cl
->cl_vtoc
), (void *)arg
, sizeof (cl
->cl_vtoc
),
4052 #endif /* _MULTI_DATAMODEL */
4054 #error "No VTOC format defined."
4062 * Function: cmlb_dkio_get_extvtoc
4065 cmlb_dkio_get_extvtoc(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
4068 struct extvtoc ext_vtoc
;
4069 #if defined(_SUNOS_VTOC_8)
4070 struct vtoc user_vtoc
;
4071 #endif /* defined(_SUNOS_VTOC_8) */
4074 bzero(&ext_vtoc
, sizeof (struct extvtoc
));
4075 mutex_enter(CMLB_MUTEX(cl
));
4076 rval
= cmlb_validate_geometry(cl
, B_TRUE
, 0, tg_cookie
);
4078 #if defined(_SUNOS_VTOC_8)
4079 if (rval
== EINVAL
&&
4080 (cl
->cl_alter_behavior
& CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8
)) {
4082 * This is to return a default label even when we do not
4083 * really assume a default label for the device.
4084 * dad driver utilizes this.
4086 if (cl
->cl_blockcount
<= CMLB_OLDVTOC_LIMIT
) {
4087 cmlb_setup_default_geometry(cl
, tg_cookie
);
4093 mutex_exit(CMLB_MUTEX(cl
));
4097 #if defined(_SUNOS_VTOC_8)
4098 cmlb_build_user_vtoc(cl
, &user_vtoc
);
4099 mutex_exit(CMLB_MUTEX(cl
));
4102 * Checking callers data model does not make much sense here
4103 * since extvtoc will always be equivalent to 64bit vtoc.
4104 * What is important is whether the kernel is in 32 or 64 bit
4108 if (ddi_copyout(&user_vtoc
, (void *)arg
,
4109 sizeof (struct extvtoc
), flag
)) {
4113 vtoc32tovtoc(user_vtoc
, ext_vtoc
);
4114 if (ddi_copyout(&ext_vtoc
, (void *)arg
,
4115 sizeof (struct extvtoc
), flag
)) {
4120 #elif defined(_SUNOS_VTOC_16)
4122 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4123 * 32-bit to maintain compatibility with existing on-disk
4124 * structures. Thus, we need to convert the structure when copying
4127 vtoc32tovtoc(cl
->cl_vtoc
, ext_vtoc
);
4128 mutex_exit(CMLB_MUTEX(cl
));
4130 if (ddi_copyout(&ext_vtoc
, (void *)arg
, sizeof (struct extvtoc
), flag
))
4133 #error "No VTOC format defined."
4140 * This routine implements the DKIOCGETEFI ioctl. This ioctl is currently
4141 * used to read the GPT Partition Table Header (primary/backup), the GUID
4142 * partition Entry Array (primary/backup), and the MBR.
4145 cmlb_dkio_get_efi(struct cmlb_lun
*cl
, caddr_t arg
, int flag
, void *tg_cookie
)
4152 if (ddi_copyin(arg
, &user_efi
, sizeof (dk_efi_t
), flag
))
4155 user_efi
.dki_data
= (void *)(uintptr_t)user_efi
.dki_data_64
;
4157 if (user_efi
.dki_length
== 0 ||
4158 user_efi
.dki_length
> cmlb_tg_max_efi_xfer
)
4161 tgt_lba
= user_efi
.dki_lba
;
4163 mutex_enter(CMLB_MUTEX(cl
));
4164 if ((cmlb_check_update_blockcount(cl
, tg_cookie
) != 0) ||
4165 (cl
->cl_tgt_blocksize
== 0) ||
4166 (user_efi
.dki_length
% cl
->cl_sys_blocksize
)) {
4167 mutex_exit(CMLB_MUTEX(cl
));
4170 if (cl
->cl_tgt_blocksize
!= cl
->cl_sys_blocksize
)
4171 tgt_lba
= tgt_lba
* cl
->cl_tgt_blocksize
/
4172 cl
->cl_sys_blocksize
;
4173 mutex_exit(CMLB_MUTEX(cl
));
4175 buffer
= kmem_alloc(user_efi
.dki_length
, KM_SLEEP
);
4176 rval
= DK_TG_READ(cl
, buffer
, tgt_lba
, user_efi
.dki_length
, tg_cookie
);
4177 if (rval
== 0 && ddi_copyout(buffer
, user_efi
.dki_data
,
4178 user_efi
.dki_length
, flag
) != 0)
4181 kmem_free(buffer
, user_efi
.dki_length
);
4185 #if defined(_SUNOS_VTOC_8)
4187 * Function: cmlb_build_user_vtoc
4189 * Description: This routine populates a pass by reference variable with the
4190 * current volume table of contents.
4192 * Arguments: cl - driver soft state (unit) structure
4193 * user_vtoc - pointer to vtoc structure to be populated
4196 cmlb_build_user_vtoc(struct cmlb_lun
*cl
, struct vtoc
*user_vtoc
)
4198 struct dk_map2
*lpart
;
4199 struct dk_map
*lmap
;
4200 struct partition
*vpart
;
4204 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
4207 * Return vtoc structure fields in the provided VTOC area, addressed
4210 bzero(user_vtoc
, sizeof (struct vtoc
));
4211 user_vtoc
->v_bootinfo
[0] = cl
->cl_vtoc
.v_bootinfo
[0];
4212 user_vtoc
->v_bootinfo
[1] = cl
->cl_vtoc
.v_bootinfo
[1];
4213 user_vtoc
->v_bootinfo
[2] = cl
->cl_vtoc
.v_bootinfo
[2];
4214 user_vtoc
->v_sanity
= VTOC_SANE
;
4215 user_vtoc
->v_version
= cl
->cl_vtoc
.v_version
;
4216 bcopy(cl
->cl_vtoc
.v_volume
, user_vtoc
->v_volume
, LEN_DKL_VVOL
);
4217 user_vtoc
->v_sectorsz
= cl
->cl_sys_blocksize
;
4218 user_vtoc
->v_nparts
= cl
->cl_vtoc
.v_nparts
;
4220 for (i
= 0; i
< 10; i
++)
4221 user_vtoc
->v_reserved
[i
] = cl
->cl_vtoc
.v_reserved
[i
];
4224 * Convert partitioning information.
4226 * Note the conversion from starting cylinder number
4227 * to starting sector number.
4230 lpart
= (struct dk_map2
*)cl
->cl_vtoc
.v_part
;
4231 vpart
= user_vtoc
->v_part
;
4233 nblks
= cl
->cl_g
.dkg_nsect
* cl
->cl_g
.dkg_nhead
;
4235 for (i
= 0; i
< V_NUMPAR
; i
++) {
4236 vpart
->p_tag
= lpart
->p_tag
;
4237 vpart
->p_flag
= lpart
->p_flag
;
4238 vpart
->p_start
= lmap
->dkl_cylno
* nblks
;
4239 vpart
->p_size
= lmap
->dkl_nblk
;
4245 user_vtoc
->timestamp
[i
] = (time_t)cl
->cl_vtoc
.v_timestamp
[i
];
4248 bcopy(cl
->cl_asciilabel
, user_vtoc
->v_asciilabel
, LEN_DKL_ASCII
);
4253 cmlb_dkio_partition(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
4256 struct partition64 p64
;
4259 efi_gpe_t
*partitions
;
4262 int n_gpe_per_blk
= 0;
4264 if (ddi_copyin((const void *)arg
, &p64
,
4265 sizeof (struct partition64
), flag
)) {
4269 buffer
= kmem_alloc(cl
->cl_sys_blocksize
, KM_SLEEP
);
4270 rval
= DK_TG_READ(cl
, buffer
, 1, cl
->cl_sys_blocksize
, tg_cookie
);
4274 cmlb_swap_efi_gpt(buffer
);
4276 if ((rval
= cmlb_validate_efi(buffer
)) != 0)
4279 nparts
= buffer
->efi_gpt_NumberOfPartitionEntries
;
4280 gpe_lba
= buffer
->efi_gpt_PartitionEntryLBA
;
4281 if (p64
.p_partno
>= nparts
) {
4282 /* couldn't find it */
4287 * Read the block that contains the requested GPE.
4289 n_gpe_per_blk
= cl
->cl_sys_blocksize
/ sizeof (efi_gpe_t
);
4290 gpe_lba
+= p64
.p_partno
/ n_gpe_per_blk
;
4291 rval
= DK_TG_READ(cl
, buffer
, gpe_lba
, cl
->cl_sys_blocksize
, tg_cookie
);
4296 partitions
= (efi_gpe_t
*)buffer
;
4297 partitions
+= p64
.p_partno
% n_gpe_per_blk
;
4299 /* Byte swap only the requested GPE */
4300 cmlb_swap_efi_gpe(1, partitions
);
4302 bcopy(&partitions
->efi_gpe_PartitionTypeGUID
, &p64
.p_type
,
4303 sizeof (struct uuid
));
4304 p64
.p_start
= partitions
->efi_gpe_StartingLBA
;
4305 p64
.p_size
= partitions
->efi_gpe_EndingLBA
-
4308 if (ddi_copyout(&p64
, (void *)arg
, sizeof (struct partition64
), flag
))
4312 kmem_free(buffer
, cl
->cl_sys_blocksize
);
4318 * Function: cmlb_dkio_set_vtoc
4320 * Description: This routine is the driver entry point for handling user
4321 * requests to set the current volume table of contents
4325 * dev the device number
4326 * arg pointer to user provided vtoc structure used to set the
4329 * flag this argument is a pass through to ddi_copyxxx()
4330 * directly from the mode argument of ioctl().
4332 * tg_cookie cookie from target driver to be passed back to target
4333 * driver when we call back to it through tg_ops.
4342 cmlb_dkio_set_vtoc(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
, int flag
,
4345 struct vtoc user_vtoc
;
4346 int shift
, rval
= 0;
4349 internal
= VOID2BOOLEAN(
4350 (cl
->cl_alter_behavior
& (CMLB_INTERNAL_MINOR_NODES
)) != 0);
4352 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
4353 shift
= CMLBUNIT_FORCE_P0_SHIFT
;
4355 shift
= CMLBUNIT_SHIFT
;
4357 #ifdef _MULTI_DATAMODEL
4358 switch (ddi_model_convert_from(flag
& FMODELS
)) {
4359 case DDI_MODEL_ILP32
: {
4360 struct vtoc32 user_vtoc32
;
4362 if (ddi_copyin((const void *)arg
, &user_vtoc32
,
4363 sizeof (struct vtoc32
), flag
)) {
4366 vtoc32tovtoc(user_vtoc32
, user_vtoc
);
4370 case DDI_MODEL_NONE
:
4371 if (ddi_copyin((const void *)arg
, &user_vtoc
,
4372 sizeof (struct vtoc
), flag
)) {
4377 #else /* ! _MULTI_DATAMODEL */
4378 if (ddi_copyin((const void *)arg
, &user_vtoc
,
4379 sizeof (struct vtoc
), flag
)) {
4382 #endif /* _MULTI_DATAMODEL */
4384 mutex_enter(CMLB_MUTEX(cl
));
4386 if (cl
->cl_blockcount
> CMLB_OLDVTOC_LIMIT
) {
4387 mutex_exit(CMLB_MUTEX(cl
));
4391 #if defined(__i386) || defined(__amd64)
4392 if (cl
->cl_tgt_blocksize
!= cl
->cl_sys_blocksize
) {
4393 mutex_exit(CMLB_MUTEX(cl
));
4398 if (cl
->cl_g
.dkg_ncyl
== 0) {
4399 mutex_exit(CMLB_MUTEX(cl
));
4403 mutex_exit(CMLB_MUTEX(cl
));
4404 cmlb_clear_efi(cl
, tg_cookie
);
4405 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd");
4406 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd,raw");
4409 * cmlb_dkio_set_vtoc creates duplicate minor nodes when
4410 * relabeling an SMI disk. To avoid that we remove them
4412 * It should be OK to remove a non-existed minor node.
4414 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h");
4415 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h,raw");
4417 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h",
4418 S_IFBLK
, (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
4419 cl
->cl_node_type
, NULL
, internal
);
4420 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h,raw",
4421 S_IFCHR
, (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
4422 cl
->cl_node_type
, NULL
, internal
);
4423 mutex_enter(CMLB_MUTEX(cl
));
4425 if ((rval
= cmlb_build_label_vtoc(cl
, &user_vtoc
)) == 0) {
4426 if ((rval
= cmlb_write_label(cl
, tg_cookie
)) == 0) {
4427 if (cmlb_validate_geometry(cl
,
4428 B_TRUE
, 0, tg_cookie
) != 0) {
4429 cmlb_dbg(CMLB_ERROR
, cl
,
4430 "cmlb_dkio_set_vtoc: "
4431 "Failed validate geometry\n");
4433 cl
->cl_msglog_flag
|= CMLB_ALLOW_2TB_WARN
;
4436 mutex_exit(CMLB_MUTEX(cl
));
4441 * Function: cmlb_dkio_set_extvtoc
4444 cmlb_dkio_set_extvtoc(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
, int flag
,
4447 int shift
, rval
= 0;
4448 struct vtoc user_vtoc
;
4451 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
4452 shift
= CMLBUNIT_FORCE_P0_SHIFT
;
4454 shift
= CMLBUNIT_SHIFT
;
4457 * Checking callers data model does not make much sense here
4458 * since extvtoc will always be equivalent to 64bit vtoc.
4459 * What is important is whether the kernel is in 32 or 64 bit
4463 if (ddi_copyin((const void *)arg
, &user_vtoc
,
4464 sizeof (struct extvtoc
), flag
)) {
4468 struct extvtoc user_extvtoc
;
4469 if (ddi_copyin((const void *)arg
, &user_extvtoc
,
4470 sizeof (struct extvtoc
), flag
)) {
4474 vtoctovtoc32(user_extvtoc
, user_vtoc
);
4477 internal
= VOID2BOOLEAN(
4478 (cl
->cl_alter_behavior
& (CMLB_INTERNAL_MINOR_NODES
)) != 0);
4479 mutex_enter(CMLB_MUTEX(cl
));
4480 #if defined(__i386) || defined(__amd64)
4481 if (cl
->cl_tgt_blocksize
!= cl
->cl_sys_blocksize
) {
4482 mutex_exit(CMLB_MUTEX(cl
));
4487 if (cl
->cl_g
.dkg_ncyl
== 0) {
4488 mutex_exit(CMLB_MUTEX(cl
));
4492 mutex_exit(CMLB_MUTEX(cl
));
4493 cmlb_clear_efi(cl
, tg_cookie
);
4494 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd");
4495 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "wd,raw");
4497 * cmlb_dkio_set_extvtoc creates duplicate minor nodes when
4498 * relabeling an SMI disk. To avoid that we remove them
4500 * It should be OK to remove a non-existed minor node.
4502 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h");
4503 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h,raw");
4505 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h",
4506 S_IFBLK
, (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
4507 cl
->cl_node_type
, NULL
, internal
);
4508 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "h,raw",
4509 S_IFCHR
, (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
4510 cl
->cl_node_type
, NULL
, internal
);
4512 mutex_enter(CMLB_MUTEX(cl
));
4514 if ((rval
= cmlb_build_label_vtoc(cl
, &user_vtoc
)) == 0) {
4515 if ((rval
= cmlb_write_label(cl
, tg_cookie
)) == 0) {
4516 if (cmlb_validate_geometry(cl
,
4517 B_TRUE
, 0, tg_cookie
) != 0) {
4518 cmlb_dbg(CMLB_ERROR
, cl
,
4519 "cmlb_dkio_set_vtoc: "
4520 "Failed validate geometry\n");
4524 mutex_exit(CMLB_MUTEX(cl
));
4529 * Function: cmlb_build_label_vtoc
4531 * Description: This routine updates the driver soft state current volume table
4532 * of contents based on a user specified vtoc.
4534 * Arguments: cl - driver soft state (unit) structure
4535 * user_vtoc - pointer to vtoc structure specifying vtoc to be used
4536 * to update the driver soft state.
4542 cmlb_build_label_vtoc(struct cmlb_lun
*cl
, struct vtoc
*user_vtoc
)
4544 struct dk_map
*lmap
;
4545 struct partition
*vpart
;
4547 #if defined(_SUNOS_VTOC_8)
4549 struct dk_map2
*lpart
;
4550 #endif /* defined(_SUNOS_VTOC_8) */
4553 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
4555 /* Sanity-check the vtoc */
4556 if (user_vtoc
->v_sanity
!= VTOC_SANE
||
4557 user_vtoc
->v_sectorsz
!= cl
->cl_sys_blocksize
||
4558 user_vtoc
->v_nparts
!= V_NUMPAR
) {
4559 cmlb_dbg(CMLB_INFO
, cl
,
4560 "cmlb_build_label_vtoc: vtoc not valid\n");
4564 nblks
= cl
->cl_g
.dkg_nsect
* cl
->cl_g
.dkg_nhead
;
4566 cmlb_dbg(CMLB_INFO
, cl
,
4567 "cmlb_build_label_vtoc: geom nblks is 0\n");
4571 #if defined(_SUNOS_VTOC_8)
4572 vpart
= user_vtoc
->v_part
;
4573 for (i
= 0; i
< V_NUMPAR
; i
++) {
4574 if (((unsigned)vpart
->p_start
% nblks
) != 0) {
4575 cmlb_dbg(CMLB_INFO
, cl
,
4576 "cmlb_build_label_vtoc: p_start not multiply of"
4577 "nblks part %d p_start %d nblks %d\n", i
,
4578 vpart
->p_start
, nblks
);
4581 ncyl
= (unsigned)vpart
->p_start
/ nblks
;
4582 ncyl
+= (unsigned)vpart
->p_size
/ nblks
;
4583 if (((unsigned)vpart
->p_size
% nblks
) != 0) {
4586 if (ncyl
> (int)cl
->cl_g
.dkg_ncyl
) {
4587 cmlb_dbg(CMLB_INFO
, cl
,
4588 "cmlb_build_label_vtoc: ncyl %d > dkg_ncyl %d"
4589 "p_size %ld p_start %ld nblks %d part number %d"
4591 ncyl
, cl
->cl_g
.dkg_ncyl
, vpart
->p_size
,
4592 vpart
->p_start
, nblks
,
4599 #endif /* defined(_SUNOS_VTOC_8) */
4601 /* Put appropriate vtoc structure fields into the disk label */
4602 #if defined(_SUNOS_VTOC_16)
4604 * The vtoc is always a 32bit data structure to maintain the
4605 * on-disk format. Convert "in place" instead of doing bcopy.
4607 vtoctovtoc32((*user_vtoc
), (*((struct vtoc32
*)&(cl
->cl_vtoc
))));
4610 * in the 16-slice vtoc, starting sectors are expressed in
4611 * numbers *relative* to the start of the Solaris fdisk partition.
4614 vpart
= user_vtoc
->v_part
;
4616 for (i
= 0; i
< (int)user_vtoc
->v_nparts
; i
++, lmap
++, vpart
++) {
4617 lmap
->dkl_cylno
= (unsigned)vpart
->p_start
/ nblks
;
4618 lmap
->dkl_nblk
= (unsigned)vpart
->p_size
;
4621 #elif defined(_SUNOS_VTOC_8)
4623 cl
->cl_vtoc
.v_bootinfo
[0] = (uint32_t)user_vtoc
->v_bootinfo
[0];
4624 cl
->cl_vtoc
.v_bootinfo
[1] = (uint32_t)user_vtoc
->v_bootinfo
[1];
4625 cl
->cl_vtoc
.v_bootinfo
[2] = (uint32_t)user_vtoc
->v_bootinfo
[2];
4627 cl
->cl_vtoc
.v_sanity
= (uint32_t)user_vtoc
->v_sanity
;
4628 cl
->cl_vtoc
.v_version
= (uint32_t)user_vtoc
->v_version
;
4630 bcopy(user_vtoc
->v_volume
, cl
->cl_vtoc
.v_volume
, LEN_DKL_VVOL
);
4632 cl
->cl_vtoc
.v_nparts
= user_vtoc
->v_nparts
;
4634 for (i
= 0; i
< 10; i
++)
4635 cl
->cl_vtoc
.v_reserved
[i
] = user_vtoc
->v_reserved
[i
];
4638 * Note the conversion from starting sector number
4639 * to starting cylinder number.
4640 * Return error if division results in a remainder.
4643 lpart
= cl
->cl_vtoc
.v_part
;
4644 vpart
= user_vtoc
->v_part
;
4646 for (i
= 0; i
< (int)user_vtoc
->v_nparts
; i
++) {
4647 lpart
->p_tag
= vpart
->p_tag
;
4648 lpart
->p_flag
= vpart
->p_flag
;
4649 lmap
->dkl_cylno
= (unsigned)vpart
->p_start
/ nblks
;
4650 lmap
->dkl_nblk
= (unsigned)vpart
->p_size
;
4658 if (user_vtoc
->timestamp
[i
] > TIME32_MAX
) {
4659 cl
->cl_vtoc
.v_timestamp
[i
] = TIME32_MAX
;
4661 cl
->cl_vtoc
.v_timestamp
[i
] = user_vtoc
->timestamp
[i
];
4664 cl
->cl_vtoc
.v_timestamp
[i
] = user_vtoc
->timestamp
[i
];
4668 bcopy(user_vtoc
->v_asciilabel
, cl
->cl_asciilabel
, LEN_DKL_ASCII
);
4670 #error "No VTOC format defined."
4676 * Function: cmlb_clear_efi
4678 * Description: This routine clears all EFI labels.
4681 * cl driver soft state (unit) structure
4683 * tg_cookie cookie from target driver to be passed back to target
4684 * driver when we call back to it through tg_ops.
4688 cmlb_clear_efi(struct cmlb_lun
*cl
, void *tg_cookie
)
4694 ASSERT(!mutex_owned(CMLB_MUTEX(cl
)));
4696 mutex_enter(CMLB_MUTEX(cl
));
4697 cl
->cl_reserved
= -1;
4698 mutex_exit(CMLB_MUTEX(cl
));
4700 gpt
= kmem_alloc(cl
->cl_sys_blocksize
, KM_SLEEP
);
4702 if (DK_TG_READ(cl
, gpt
, 1, cl
->cl_sys_blocksize
, tg_cookie
) != 0) {
4706 cmlb_swap_efi_gpt(gpt
);
4707 rval
= cmlb_validate_efi(gpt
);
4710 bzero(gpt
, sizeof (efi_gpt_t
));
4711 if (rval
= DK_TG_WRITE(cl
, gpt
, 1, cl
->cl_sys_blocksize
,
4713 cmlb_dbg(CMLB_INFO
, cl
,
4714 "cmlb_clear_efi: clear primary label failed\n");
4718 rval
= DK_TG_GETCAP(cl
, &cap
, tg_cookie
);
4723 if ((rval
= DK_TG_READ(cl
, gpt
, cap
- 1, cl
->cl_sys_blocksize
,
4727 cmlb_swap_efi_gpt(gpt
);
4728 rval
= cmlb_validate_efi(gpt
);
4731 cmlb_dbg(CMLB_TRACE
, cl
,
4732 "cmlb_clear_efi clear backup@%lu\n", cap
- 1);
4733 bzero(gpt
, sizeof (efi_gpt_t
));
4734 if ((rval
= DK_TG_WRITE(cl
, gpt
, cap
- 1, cl
->cl_sys_blocksize
,
4736 cmlb_dbg(CMLB_INFO
, cl
,
4737 "cmlb_clear_efi: clear backup label failed\n");
4741 * Refer to comments related to off-by-1 at the
4742 * header of this file
4744 if ((rval
= DK_TG_READ(cl
, gpt
, cap
- 2,
4745 cl
->cl_sys_blocksize
, tg_cookie
)) != 0) {
4748 cmlb_swap_efi_gpt(gpt
);
4749 rval
= cmlb_validate_efi(gpt
);
4751 /* clear legacy backup EFI label */
4752 cmlb_dbg(CMLB_TRACE
, cl
,
4753 "cmlb_clear_efi clear legacy backup@%lu\n",
4755 bzero(gpt
, sizeof (efi_gpt_t
));
4756 if ((rval
= DK_TG_WRITE(cl
, gpt
, cap
- 2,
4757 cl
->cl_sys_blocksize
, tg_cookie
))) {
4758 cmlb_dbg(CMLB_INFO
, cl
,
4759 "cmlb_clear_efi: clear legacy backup label "
4766 kmem_free(gpt
, cl
->cl_sys_blocksize
);
4770 * Function: cmlb_set_vtoc
4772 * Description: This routine writes data to the appropriate positions
4775 * cl driver soft state (unit) structure
4777 * dkl the data to be written
4779 * tg_cookie cookie from target driver to be passed back to target
4780 * driver when we call back to it through tg_ops.
4785 cmlb_set_vtoc(struct cmlb_lun
*cl
, struct dk_label
*dkl
, void *tg_cookie
)
4794 #if defined(__i386) || defined(__amd64)
4795 label_addr
= cl
->cl_solaris_offset
+ DK_LABEL_LOC
;
4797 /* Write the primary label at block 0 of the solaris partition. */
4801 rval
= DK_TG_WRITE(cl
, dkl
, label_addr
, cl
->cl_sys_blocksize
,
4809 * Calculate where the backup labels go. They are always on
4810 * the last alternate cylinder, but some older drives put them
4811 * on head 2 instead of the last head. They are always on the
4812 * first 5 odd sectors of the appropriate track.
4814 * We have no choice at this point, but to believe that the
4815 * disk label is valid. Use the geometry of the disk
4816 * as described in the label.
4818 cyl
= dkl
->dkl_ncyl
+ dkl
->dkl_acyl
- 1;
4819 head
= dkl
->dkl_nhead
- 1;
4822 * Write and verify the backup labels. Make sure we don't try to
4823 * write past the last cylinder.
4825 for (sec
= 1; ((sec
< 5 * 2 + 1) && (sec
< dkl
->dkl_nsect
)); sec
+= 2) {
4827 (cyl
* ((dkl
->dkl_nhead
* dkl
->dkl_nsect
) - dkl
->dkl_apc
)) +
4828 (head
* dkl
->dkl_nsect
) + sec
);
4829 #if defined(__i386) || defined(__amd64)
4830 blk
+= cl
->cl_solaris_offset
;
4832 rval
= DK_TG_WRITE(cl
, dkl
, blk
, cl
->cl_sys_blocksize
,
4834 cmlb_dbg(CMLB_INFO
, cl
,
4835 "cmlb_set_vtoc: wrote backup label %llx\n", blk
);
4845 * Function: cmlb_clear_vtoc
4847 * Description: This routine clears out the VTOC labels.
4850 * cl driver soft state (unit) structure
4852 * tg_cookie cookie from target driver to be passed back to target
4853 * driver when we call back to it through tg_ops.
4858 cmlb_clear_vtoc(struct cmlb_lun
*cl
, void *tg_cookie
)
4860 struct dk_label
*dkl
;
4862 mutex_exit(CMLB_MUTEX(cl
));
4863 dkl
= kmem_zalloc(cl
->cl_sys_blocksize
, KM_SLEEP
);
4864 mutex_enter(CMLB_MUTEX(cl
));
4866 * cmlb_set_vtoc uses these fields in order to figure out
4867 * where to overwrite the backup labels
4869 dkl
->dkl_apc
= cl
->cl_g
.dkg_apc
;
4870 dkl
->dkl_ncyl
= cl
->cl_g
.dkg_ncyl
;
4871 dkl
->dkl_acyl
= cl
->cl_g
.dkg_acyl
;
4872 dkl
->dkl_nhead
= cl
->cl_g
.dkg_nhead
;
4873 dkl
->dkl_nsect
= cl
->cl_g
.dkg_nsect
;
4874 mutex_exit(CMLB_MUTEX(cl
));
4875 (void) cmlb_set_vtoc(cl
, dkl
, tg_cookie
);
4876 kmem_free(dkl
, cl
->cl_sys_blocksize
);
4878 mutex_enter(CMLB_MUTEX(cl
));
4882 * Function: cmlb_write_label
4884 * Description: This routine will validate and write the driver soft state vtoc
4885 * contents to the device.
4890 * tg_cookie cookie from target driver to be passed back to target
4891 * driver when we call back to it through tg_ops.
4894 * Return Code: the code returned by cmlb_send_scsi_cmd()
4901 cmlb_write_label(struct cmlb_lun
*cl
, void *tg_cookie
)
4903 struct dk_label
*dkl
;
4909 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
4910 mutex_exit(CMLB_MUTEX(cl
));
4911 dkl
= kmem_zalloc(cl
->cl_sys_blocksize
, KM_SLEEP
);
4912 mutex_enter(CMLB_MUTEX(cl
));
4914 bcopy(&cl
->cl_vtoc
, &dkl
->dkl_vtoc
, sizeof (struct dk_vtoc
));
4915 dkl
->dkl_rpm
= cl
->cl_g
.dkg_rpm
;
4916 dkl
->dkl_pcyl
= cl
->cl_g
.dkg_pcyl
;
4917 dkl
->dkl_apc
= cl
->cl_g
.dkg_apc
;
4918 dkl
->dkl_intrlv
= cl
->cl_g
.dkg_intrlv
;
4919 dkl
->dkl_ncyl
= cl
->cl_g
.dkg_ncyl
;
4920 dkl
->dkl_acyl
= cl
->cl_g
.dkg_acyl
;
4921 dkl
->dkl_nhead
= cl
->cl_g
.dkg_nhead
;
4922 dkl
->dkl_nsect
= cl
->cl_g
.dkg_nsect
;
4924 #if defined(_SUNOS_VTOC_8)
4925 dkl
->dkl_obs1
= cl
->cl_g
.dkg_obs1
;
4926 dkl
->dkl_obs2
= cl
->cl_g
.dkg_obs2
;
4927 dkl
->dkl_obs3
= cl
->cl_g
.dkg_obs3
;
4928 for (i
= 0; i
< NDKMAP
; i
++) {
4929 dkl
->dkl_map
[i
].dkl_cylno
= cl
->cl_map
[i
].dkl_cylno
;
4930 dkl
->dkl_map
[i
].dkl_nblk
= cl
->cl_map
[i
].dkl_nblk
;
4932 bcopy(cl
->cl_asciilabel
, dkl
->dkl_asciilabel
, LEN_DKL_ASCII
);
4933 #elif defined(_SUNOS_VTOC_16)
4934 dkl
->dkl_skew
= cl
->cl_dkg_skew
;
4936 #error "No VTOC format defined."
4939 dkl
->dkl_magic
= DKL_MAGIC
;
4940 dkl
->dkl_write_reinstruct
= cl
->cl_g
.dkg_write_reinstruct
;
4941 dkl
->dkl_read_reinstruct
= cl
->cl_g
.dkg_read_reinstruct
;
4943 /* Construct checksum for the new disk label */
4946 i
= sizeof (struct dk_label
) / sizeof (short);
4950 dkl
->dkl_cksum
= sum
;
4952 mutex_exit(CMLB_MUTEX(cl
));
4954 rval
= cmlb_set_vtoc(cl
, dkl
, tg_cookie
);
4956 kmem_free(dkl
, cl
->cl_sys_blocksize
);
4957 mutex_enter(CMLB_MUTEX(cl
));
4962 * This routine implements the DKIOCSETEFI ioctl. This ioctl is currently
4963 * used to write (or clear) the GPT Partition Table header (primary/backup)
4964 * and GUID partition Entry Array (primary/backup). It is also used to write
4965 * the Protective MBR.
4968 cmlb_dkio_set_efi(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
, int flag
,
4972 int shift
, rval
= 0;
4977 if (ddi_copyin(arg
, &user_efi
, sizeof (dk_efi_t
), flag
))
4980 internal
= VOID2BOOLEAN(
4981 (cl
->cl_alter_behavior
& (CMLB_INTERNAL_MINOR_NODES
)) != 0);
4983 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
4984 shift
= CMLBUNIT_FORCE_P0_SHIFT
;
4986 shift
= CMLBUNIT_SHIFT
;
4988 user_efi
.dki_data
= (void *)(uintptr_t)user_efi
.dki_data_64
;
4990 if (user_efi
.dki_length
== 0 ||
4991 user_efi
.dki_length
> cmlb_tg_max_efi_xfer
)
4994 tgt_lba
= user_efi
.dki_lba
;
4996 mutex_enter(CMLB_MUTEX(cl
));
4997 if ((cmlb_check_update_blockcount(cl
, tg_cookie
) != 0) ||
4998 (cl
->cl_tgt_blocksize
== 0) ||
4999 (user_efi
.dki_length
% cl
->cl_sys_blocksize
)) {
5000 mutex_exit(CMLB_MUTEX(cl
));
5003 if (cl
->cl_tgt_blocksize
!= cl
->cl_sys_blocksize
)
5005 cl
->cl_tgt_blocksize
/ cl
->cl_sys_blocksize
;
5006 mutex_exit(CMLB_MUTEX(cl
));
5008 buffer
= kmem_alloc(user_efi
.dki_length
, KM_SLEEP
);
5009 if (ddi_copyin(user_efi
.dki_data
, buffer
, user_efi
.dki_length
, flag
)) {
5013 * let's clear the vtoc labels and clear the softstate
5016 mutex_enter(CMLB_MUTEX(cl
));
5017 if (cl
->cl_vtoc
.v_sanity
== VTOC_SANE
) {
5018 cmlb_dbg(CMLB_TRACE
, cl
,
5019 "cmlb_dkio_set_efi: CLEAR VTOC\n");
5020 if (cl
->cl_label_from_media
== CMLB_LABEL_VTOC
)
5021 cmlb_clear_vtoc(cl
, tg_cookie
);
5022 bzero(&cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
5023 mutex_exit(CMLB_MUTEX(cl
));
5024 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h");
5025 ddi_remove_minor_node(CMLB_DEVINFO(cl
), "h,raw");
5026 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "wd",
5028 (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
5029 cl
->cl_node_type
, NULL
, internal
);
5030 (void) cmlb_create_minor(CMLB_DEVINFO(cl
), "wd,raw",
5032 (CMLBUNIT(dev
, shift
) << shift
) | WD_NODE
,
5033 cl
->cl_node_type
, NULL
, internal
);
5035 mutex_exit(CMLB_MUTEX(cl
));
5037 rval
= DK_TG_WRITE(cl
, buffer
, tgt_lba
, user_efi
.dki_length
,
5041 mutex_enter(CMLB_MUTEX(cl
));
5042 cl
->cl_f_geometry_is_valid
= B_FALSE
;
5043 mutex_exit(CMLB_MUTEX(cl
));
5046 kmem_free(buffer
, user_efi
.dki_length
);
5051 * Function: cmlb_dkio_get_mboot
5053 * Description: This routine is the driver entry point for handling user
5054 * requests to get the current device mboot (DKIOCGMBOOT)
5057 * arg pointer to user provided mboot structure specifying
5058 * the current mboot.
5060 * flag this argument is a pass through to ddi_copyxxx()
5061 * directly from the mode argument of ioctl().
5063 * tg_cookie cookie from target driver to be passed back to target
5064 * driver when we call back to it through tg_ops.
5072 cmlb_dkio_get_mboot(struct cmlb_lun
*cl
, caddr_t arg
, int flag
, void *tg_cookie
)
5074 struct mboot
*mboot
;
5079 #if defined(_SUNOS_VTOC_8)
5080 if ((!ISREMOVABLE(cl
) && !ISHOTPLUGGABLE(cl
)) || (arg
== NULL
)) {
5081 #elif defined(_SUNOS_VTOC_16)
5088 * Read the mboot block, located at absolute block 0 on the target.
5090 buffer_size
= cl
->cl_sys_blocksize
;
5092 cmlb_dbg(CMLB_TRACE
, cl
,
5093 "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size
);
5095 mboot
= kmem_zalloc(buffer_size
, KM_SLEEP
);
5096 if ((rval
= DK_TG_READ(cl
, mboot
, 0, buffer_size
, tg_cookie
)) == 0) {
5097 if (ddi_copyout(mboot
, (void *)arg
,
5098 sizeof (struct mboot
), flag
) != 0) {
5102 kmem_free(mboot
, buffer_size
);
5108 * Function: cmlb_dkio_set_mboot
5110 * Description: This routine is the driver entry point for handling user
5111 * requests to validate and set the device master boot
5115 * arg pointer to user provided mboot structure used to set the
5118 * flag this argument is a pass through to ddi_copyxxx()
5119 * directly from the mode argument of ioctl().
5121 * tg_cookie cookie from target driver to be passed back to target
5122 * driver when we call back to it through tg_ops.
5130 cmlb_dkio_set_mboot(struct cmlb_lun
*cl
, caddr_t arg
, int flag
, void *tg_cookie
)
5132 struct mboot
*mboot
= NULL
;
5137 ASSERT(!mutex_owned(CMLB_MUTEX(cl
)));
5139 #if defined(_SUNOS_VTOC_8)
5140 if (!ISREMOVABLE(cl
) && !ISHOTPLUGGABLE(cl
)) {
5149 mboot
= kmem_zalloc(cl
->cl_sys_blocksize
, KM_SLEEP
);
5151 if (ddi_copyin((const void *)arg
, mboot
,
5152 cl
->cl_sys_blocksize
, flag
) != 0) {
5153 kmem_free(mboot
, cl
->cl_sys_blocksize
);
5157 /* Is this really a master boot record? */
5158 magic
= LE_16(mboot
->signature
);
5159 if (magic
!= MBB_MAGIC
) {
5160 kmem_free(mboot
, cl
->cl_sys_blocksize
);
5164 rval
= DK_TG_WRITE(cl
, mboot
, 0, cl
->cl_sys_blocksize
, tg_cookie
);
5166 mutex_enter(CMLB_MUTEX(cl
));
5167 #if defined(__i386) || defined(__amd64)
5170 * mboot has been written successfully.
5171 * update the fdisk and vtoc tables in memory
5173 rval
= cmlb_update_fdisk_and_vtoc(cl
, tg_cookie
);
5174 if ((!cl
->cl_f_geometry_is_valid
) || (rval
!= 0)) {
5175 mutex_exit(CMLB_MUTEX(cl
));
5176 kmem_free(mboot
, cl
->cl_sys_blocksize
);
5182 cmlb_setup_default_geometry(cl
, tg_cookie
);
5188 * mboot has been written successfully.
5189 * set up the default geometry and VTOC
5191 if (cl
->cl_blockcount
<= CMLB_EXTVTOC_LIMIT
)
5192 cmlb_setup_default_geometry(cl
, tg_cookie
);
5195 cl
->cl_msglog_flag
|= CMLB_ALLOW_2TB_WARN
;
5196 mutex_exit(CMLB_MUTEX(cl
));
5197 kmem_free(mboot
, cl
->cl_sys_blocksize
);
5202 #if defined(__i386) || defined(__amd64)
5205 cmlb_dkio_set_ext_part(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
5209 diskaddr_t capacity
;
5211 ASSERT(!mutex_owned(CMLB_MUTEX(cl
)));
5213 mutex_enter(CMLB_MUTEX(cl
));
5214 capacity
= cl
->cl_blockcount
;
5215 fdisk_rval
= cmlb_read_fdisk(cl
, capacity
, tg_cookie
);
5216 if (fdisk_rval
!= 0) {
5217 mutex_exit(CMLB_MUTEX(cl
));
5218 return (fdisk_rval
);
5221 mutex_exit(CMLB_MUTEX(cl
));
5222 return (fdisk_rval
);
5227 * Function: cmlb_setup_default_geometry
5229 * Description: This local utility routine sets the default geometry as part of
5230 * setting the device mboot.
5233 * cl driver soft state (unit) structure
5235 * tg_cookie cookie from target driver to be passed back to target
5236 * driver when we call back to it through tg_ops.
5239 * Note: This may be redundant with cmlb_build_default_label.
5242 cmlb_setup_default_geometry(struct cmlb_lun
*cl
, void *tg_cookie
)
5244 struct cmlb_geom pgeom
;
5245 struct cmlb_geom
*pgeomp
= &pgeom
;
5247 int geom_base_cap
= 1;
5250 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
5252 /* zero out the soft state geometry and partition table. */
5253 bzero(&cl
->cl_g
, sizeof (struct dk_geom
));
5254 bzero(&cl
->cl_vtoc
, sizeof (struct dk_vtoc
));
5255 bzero(cl
->cl_map
, NDKMAP
* (sizeof (struct dk_map
)));
5258 * For the rpm, we use the minimum for the disk.
5259 * For the head, cyl and number of sector per track,
5260 * if the capacity <= 1GB, head = 64, sect = 32.
5261 * else head = 255, sect 63
5262 * Note: the capacity should be equal to C*H*S values.
5263 * This will cause some truncation of size due to
5264 * round off errors. For CD-ROMs, this truncation can
5265 * have adverse side effects, so returning ncyl and
5266 * nhead as 1. The nsect will overflow for most of
5267 * CD-ROMs as nsect is of type ushort.
5269 if (cl
->cl_alter_behavior
& CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8
) {
5271 * newfs currently can not handle 255 ntracks for SPARC
5272 * so get the geometry from target driver instead of coming up
5273 * with one based on capacity.
5275 mutex_exit(CMLB_MUTEX(cl
));
5276 ret
= DK_TG_GETPHYGEOM(cl
, pgeomp
, tg_cookie
);
5277 mutex_enter(CMLB_MUTEX(cl
));
5282 cmlb_dbg(CMLB_ERROR
, cl
,
5283 "cmlb_setup_default_geometry: "
5284 "tg_getphygeom failed %d\n", ret
);
5286 /* do default setting, geometry based on capacity */
5290 if (geom_base_cap
) {
5292 cl
->cl_g
.dkg_ncyl
= 1;
5293 cl
->cl_g
.dkg_nhead
= 1;
5294 cl
->cl_g
.dkg_nsect
= cl
->cl_blockcount
;
5295 } else if (cl
->cl_blockcount
< 160) {
5297 cl
->cl_g
.dkg_nhead
= 1;
5298 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
;
5299 cl
->cl_g
.dkg_nsect
= 1;
5300 } else if (cl
->cl_blockcount
<= 0x1000) {
5301 /* Needed for unlabeled SCSI floppies. */
5302 cl
->cl_g
.dkg_nhead
= 2;
5303 cl
->cl_g
.dkg_ncyl
= 80;
5304 cl
->cl_g
.dkg_pcyl
= 80;
5305 cl
->cl_g
.dkg_nsect
= cl
->cl_blockcount
/ (2 * 80);
5306 } else if (cl
->cl_blockcount
<= 0x200000) {
5307 cl
->cl_g
.dkg_nhead
= 64;
5308 cl
->cl_g
.dkg_nsect
= 32;
5309 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
/ (64 * 32);
5311 cl
->cl_g
.dkg_nhead
= 255;
5313 cl
->cl_g
.dkg_nsect
= ((cl
->cl_blockcount
+
5314 (UINT16_MAX
* 255 * 63) - 1) /
5315 (UINT16_MAX
* 255 * 63)) * 63;
5317 if (cl
->cl_g
.dkg_nsect
== 0)
5318 cl
->cl_g
.dkg_nsect
= (UINT16_MAX
/ 63) * 63;
5320 cl
->cl_g
.dkg_ncyl
= cl
->cl_blockcount
/
5321 (255 * cl
->cl_g
.dkg_nsect
);
5324 cl
->cl_g
.dkg_acyl
= 0;
5325 cl
->cl_g
.dkg_bcyl
= 0;
5326 cl
->cl_g
.dkg_intrlv
= 1;
5327 cl
->cl_g
.dkg_rpm
= 200;
5328 if (cl
->cl_g
.dkg_pcyl
== 0)
5329 cl
->cl_g
.dkg_pcyl
= cl
->cl_g
.dkg_ncyl
+
5332 cl
->cl_g
.dkg_ncyl
= (short)pgeomp
->g_ncyl
;
5333 cl
->cl_g
.dkg_acyl
= pgeomp
->g_acyl
;
5334 cl
->cl_g
.dkg_nhead
= pgeomp
->g_nhead
;
5335 cl
->cl_g
.dkg_nsect
= pgeomp
->g_nsect
;
5336 cl
->cl_g
.dkg_intrlv
= pgeomp
->g_intrlv
;
5337 cl
->cl_g
.dkg_rpm
= pgeomp
->g_rpm
;
5338 cl
->cl_g
.dkg_pcyl
= cl
->cl_g
.dkg_ncyl
+ cl
->cl_g
.dkg_acyl
;
5341 cl
->cl_g
.dkg_read_reinstruct
= 0;
5342 cl
->cl_g
.dkg_write_reinstruct
= 0;
5343 cl
->cl_solaris_size
= cl
->cl_g
.dkg_ncyl
*
5344 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
;
5346 cl
->cl_map
['a'-'a'].dkl_cylno
= 0;
5347 cl
->cl_map
['a'-'a'].dkl_nblk
= cl
->cl_solaris_size
;
5349 cl
->cl_map
['c'-'a'].dkl_cylno
= 0;
5350 cl
->cl_map
['c'-'a'].dkl_nblk
= cl
->cl_solaris_size
;
5352 cl
->cl_vtoc
.v_part
[2].p_tag
= V_BACKUP
;
5353 cl
->cl_vtoc
.v_part
[2].p_flag
= V_UNMNT
;
5354 cl
->cl_vtoc
.v_nparts
= V_NUMPAR
;
5355 cl
->cl_vtoc
.v_version
= V_VERSION
;
5356 (void) sprintf((char *)cl
->cl_asciilabel
, "DEFAULT cyl %d alt %d"
5357 " hd %d sec %d", cl
->cl_g
.dkg_ncyl
, cl
->cl_g
.dkg_acyl
,
5358 cl
->cl_g
.dkg_nhead
, cl
->cl_g
.dkg_nsect
);
5360 cl
->cl_f_geometry_is_valid
= B_FALSE
;
5364 #if defined(__i386) || defined(__amd64)
5366 * Function: cmlb_update_fdisk_and_vtoc
5368 * Description: This local utility routine updates the device fdisk and vtoc
5369 * as part of setting the device mboot.
5372 * cl driver soft state (unit) structure
5374 * tg_cookie cookie from target driver to be passed back to target
5375 * driver when we call back to it through tg_ops.
5378 * Return Code: 0 for success or errno-type return code.
5380 * Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
5381 * these did exist separately in x86 sd.c.
5384 cmlb_update_fdisk_and_vtoc(struct cmlb_lun
*cl
, void *tg_cookie
)
5389 diskaddr_t capacity
;
5391 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
5393 if (cmlb_check_update_blockcount(cl
, tg_cookie
) != 0)
5397 * Set up the "whole disk" fdisk partition; this should always
5398 * exist, regardless of whether the disk contains an fdisk table
5401 cl
->cl_map
[P0_RAW_DISK
].dkl_cylno
= 0;
5402 cl
->cl_map
[P0_RAW_DISK
].dkl_nblk
= cl
->cl_blockcount
;
5405 * copy the lbasize and capacity so that if they're
5406 * reset while we're not holding the CMLB_MUTEX(cl), we will
5407 * continue to use valid values after the CMLB_MUTEX(cl) is
5410 capacity
= cl
->cl_blockcount
;
5413 * refresh the logical and physical geometry caches.
5414 * (data from mode sense format/rigid disk geometry pages,
5415 * and scsi_ifgetcap("geometry").
5417 cmlb_resync_geom_caches(cl
, capacity
, tg_cookie
);
5420 * Only DIRECT ACCESS devices will have Scl labels.
5421 * CD's supposedly have a Scl label, too
5423 if (cl
->cl_device_type
== DTYPE_DIRECT
|| ISREMOVABLE(cl
)) {
5424 fdisk_rval
= cmlb_read_fdisk(cl
, capacity
, tg_cookie
);
5425 if (fdisk_rval
!= 0) {
5426 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
5427 return (fdisk_rval
);
5430 if (cl
->cl_solaris_size
<= DK_LABEL_LOC
) {
5432 * Found fdisk table but no Solaris partition entry,
5433 * so don't call cmlb_uselabel() and don't create
5437 cl
->cl_f_geometry_is_valid
= B_TRUE
;
5438 goto no_solaris_partition
;
5440 } else if (capacity
< 0) {
5441 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
5446 * For Removable media We reach here if we have found a
5447 * SOLARIS PARTITION.
5448 * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
5449 * PARTITION has changed from the previous one, hence we will setup a
5450 * default VTOC in this case.
5452 if (!cl
->cl_f_geometry_is_valid
) {
5453 /* if we get here it is writable */
5454 /* we are called from SMBOOT, and after a write of fdisk */
5455 cmlb_build_default_label(cl
, tg_cookie
);
5459 no_solaris_partition
:
5461 #if defined(_SUNOS_VTOC_16)
5463 * If we have valid geometry, set up the remaining fdisk partitions.
5464 * Note that dkl_cylno is not used for the fdisk map entries, so
5465 * we set it to an entirely bogus value.
5467 for (count
= 0; count
< FDISK_PARTS
; count
++) {
5468 cl
->cl_map
[FDISK_P1
+ count
].dkl_cylno
= UINT32_MAX
;
5469 cl
->cl_map
[FDISK_P1
+ count
].dkl_nblk
=
5470 cl
->cl_fmap
[count
].fmap_nblk
;
5471 cl
->cl_offset
[FDISK_P1
+ count
] =
5472 cl
->cl_fmap
[count
].fmap_start
;
5476 for (count
= 0; count
< NDKMAP
; count
++) {
5477 #if defined(_SUNOS_VTOC_8)
5478 struct dk_map
*lp
= &cl
->cl_map
[count
];
5479 cl
->cl_offset
[count
] =
5480 cl
->cl_g
.dkg_nhead
* cl
->cl_g
.dkg_nsect
* lp
->dkl_cylno
;
5481 #elif defined(_SUNOS_VTOC_16)
5482 struct dkl_partition
*vp
= &cl
->cl_vtoc
.v_part
[count
];
5483 cl
->cl_offset
[count
] = vp
->p_start
+ cl
->cl_solaris_offset
;
5485 #error "No VTOC format defined."
5489 ASSERT(mutex_owned(CMLB_MUTEX(cl
)));
5494 #if defined(__i386) || defined(__amd64)
5496 cmlb_dkio_get_virtgeom(struct cmlb_lun
*cl
, caddr_t arg
, int flag
)
5500 /* Return the driver's notion of the media's logical geometry */
5501 struct dk_geom disk_geom
;
5502 struct dk_geom
*dkgp
= &disk_geom
;
5504 mutex_enter(CMLB_MUTEX(cl
));
5506 * If there is no HBA geometry available, or
5507 * if the HBA returned us something that doesn't
5508 * really fit into an Int 13/function 8 geometry
5509 * result, just fail the ioctl. See PSARC 1998/313.
5511 if (cl
->cl_lgeom
.g_nhead
== 0 ||
5512 cl
->cl_lgeom
.g_nsect
== 0 ||
5513 cl
->cl_lgeom
.g_ncyl
> 1024) {
5514 mutex_exit(CMLB_MUTEX(cl
));
5517 dkgp
->dkg_ncyl
= cl
->cl_lgeom
.g_ncyl
;
5518 dkgp
->dkg_acyl
= cl
->cl_lgeom
.g_acyl
;
5519 dkgp
->dkg_pcyl
= dkgp
->dkg_ncyl
+ dkgp
->dkg_acyl
;
5520 dkgp
->dkg_nhead
= cl
->cl_lgeom
.g_nhead
;
5521 dkgp
->dkg_nsect
= cl
->cl_lgeom
.g_nsect
;
5523 mutex_exit(CMLB_MUTEX(cl
));
5524 if (ddi_copyout(dkgp
, (void *)arg
,
5525 sizeof (struct dk_geom
), flag
)) {
5535 #if defined(__i386) || defined(__amd64)
5537 cmlb_dkio_get_phygeom(struct cmlb_lun
*cl
, caddr_t arg
, int flag
,
5541 diskaddr_t capacity
;
5544 /* Return the driver's notion of the media physical geometry */
5545 struct dk_geom disk_geom
;
5546 struct dk_geom
*dkgp
= &disk_geom
;
5548 mutex_enter(CMLB_MUTEX(cl
));
5550 if (cl
->cl_g
.dkg_nhead
!= 0 &&
5551 cl
->cl_g
.dkg_nsect
!= 0) {
5553 * We succeeded in getting a geometry, but
5554 * right now it is being reported as just the
5555 * Solaris fdisk partition, just like for
5556 * DKIOCGGEOM. We need to change that to be
5557 * correct for the entire disk now.
5559 bcopy(&cl
->cl_g
, dkgp
, sizeof (*dkgp
));
5561 dkgp
->dkg_ncyl
= cl
->cl_blockcount
/
5562 (dkgp
->dkg_nhead
* dkgp
->dkg_nsect
);
5564 bzero(dkgp
, sizeof (struct dk_geom
));
5566 * This disk does not have a Solaris VTOC
5567 * so we must present a physical geometry
5568 * that will remain consistent regardless
5569 * of how the disk is used. This will ensure
5570 * that the geometry does not change regardless
5571 * of the fdisk partition type (ie. EFI, FAT32,
5575 dkgp
->dkg_nhead
= cl
->cl_pgeom
.g_nhead
;
5576 dkgp
->dkg_nsect
= cl
->cl_pgeom
.g_nsect
;
5577 dkgp
->dkg_ncyl
= cl
->cl_pgeom
.g_ncyl
;
5578 dkgp
->dkg_acyl
= cl
->cl_pgeom
.g_acyl
;
5581 * Invalid cl_blockcount can generate invalid
5582 * dk_geom and may result in division by zero
5583 * system failure. Should make sure blockcount
5584 * is valid before using it here.
5586 if (cl
->cl_blockcount
== 0) {
5587 mutex_exit(CMLB_MUTEX(cl
));
5592 * Refer to comments related to off-by-1 at the
5593 * header of this file
5595 if (cl
->cl_alter_behavior
& CMLB_OFF_BY_ONE
)
5596 capacity
= cl
->cl_blockcount
- 1;
5598 capacity
= cl
->cl_blockcount
;
5600 cmlb_convert_geometry(cl
, capacity
, dkgp
, tg_cookie
);
5602 dkgp
->dkg_ncyl
= capacity
/
5603 (dkgp
->dkg_nhead
* dkgp
->dkg_nsect
);
5606 dkgp
->dkg_pcyl
= dkgp
->dkg_ncyl
+ dkgp
->dkg_acyl
;
5608 mutex_exit(CMLB_MUTEX(cl
));
5609 if (ddi_copyout(dkgp
, (void *)arg
, sizeof (struct dk_geom
), flag
))
5616 #if defined(__i386) || defined(__amd64)
5618 cmlb_dkio_partinfo(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
, int flag
)
5623 * Return parameters describing the selected disk slice.
5624 * Note: this ioctl is for the intel platform only
5628 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
5629 part
= getminor(dev
) & ((1 << CMLBUNIT_FORCE_P0_SHIFT
) - 1);
5631 part
= CMLBPART(dev
);
5633 mutex_enter(CMLB_MUTEX(cl
));
5634 /* don't check cl_solaris_size for pN */
5635 if (part
< P0_RAW_DISK
&& cl
->cl_solaris_size
== 0) {
5637 mutex_exit(CMLB_MUTEX(cl
));
5641 p
.p_start
= (daddr_t
)cl
->cl_offset
[part
];
5642 p
.p_length
= (int)cl
->cl_map
[part
].dkl_nblk
;
5643 mutex_exit(CMLB_MUTEX(cl
));
5644 #ifdef _MULTI_DATAMODEL
5645 switch (ddi_model_convert_from(flag
& FMODELS
)) {
5646 case DDI_MODEL_ILP32
:
5648 struct part_info32 p32
;
5650 p32
.p_start
= (daddr32_t
)p
.p_start
;
5651 p32
.p_length
= p
.p_length
;
5652 if (ddi_copyout(&p32
, (void *)arg
,
5653 sizeof (p32
), flag
))
5658 case DDI_MODEL_NONE
:
5660 if (ddi_copyout(&p
, (void *)arg
, sizeof (p
),
5666 #else /* ! _MULTI_DATAMODEL */
5667 if (ddi_copyout(&p
, (void *)arg
, sizeof (p
), flag
))
5669 #endif /* _MULTI_DATAMODEL */
5674 cmlb_dkio_extpartinfo(struct cmlb_lun
*cl
, dev_t dev
, caddr_t arg
, int flag
)
5679 * Return parameters describing the selected disk slice.
5680 * Note: this ioctl is for the intel platform only
5684 if (cl
->cl_alter_behavior
& CMLB_CREATE_P0_MINOR_NODE
)
5685 part
= getminor(dev
) & ((1 << CMLBUNIT_FORCE_P0_SHIFT
) - 1);
5687 part
= CMLBPART(dev
);
5689 mutex_enter(CMLB_MUTEX(cl
));
5690 /* don't check cl_solaris_size for pN */
5691 if (part
< P0_RAW_DISK
&& cl
->cl_solaris_size
== 0) {
5693 mutex_exit(CMLB_MUTEX(cl
));
5695 struct extpart_info p
;
5697 p
.p_start
= (diskaddr_t
)cl
->cl_offset
[part
];
5698 p
.p_length
= (diskaddr_t
)cl
->cl_map
[part
].dkl_nblk
;
5699 mutex_exit(CMLB_MUTEX(cl
));
5700 if (ddi_copyout(&p
, (void *)arg
, sizeof (p
), flag
))
5708 cmlb_prop_op(cmlb_handle_t cmlbhandle
,
5709 dev_t dev
, dev_info_t
*dip
, ddi_prop_op_t prop_op
, int mod_flags
,
5710 char *name
, caddr_t valuep
, int *lengthp
, int part
, void *tg_cookie
)
5712 struct cmlb_lun
*cl
;
5713 diskaddr_t capacity
;
5715 enum dp
{ DP_NBLOCKS
, DP_BLKSIZE
, DP_SSD
, DP_ROT
} dp
;
5720 tg_attribute_t tgattr
;
5722 /* Always fallback to ddi_prop_op... */
5723 cl
= (struct cmlb_lun
*)cmlbhandle
;
5725 fallback
: return (ddi_prop_op(dev
, dip
, prop_op
, mod_flags
,
5726 name
, valuep
, lengthp
));
5729 /* Pick up capacity and blocksize information. */
5730 capacity
= cl
->cl_blockcount
;
5733 lbasize
= cl
->cl_tgt_blocksize
;
5735 lbasize
= DEV_BSIZE
; /* 0 -> DEV_BSIZE units */
5737 /* Check for dynamic property of whole device. */
5738 if (dev
== DDI_DEV_T_ANY
) {
5739 /* Fallback to ddi_prop_op if we don't understand. */
5740 if (strcmp(name
, "device-nblocks") == 0)
5742 else if (strcmp(name
, "device-blksize") == 0)
5744 else if (strcmp(name
, "device-solid-state") == 0)
5746 else if (strcmp(name
, "device-rotational") == 0)
5751 /* get callers length, establish length of our dynamic prop */
5752 callers_length
= *lengthp
;
5753 if (dp
== DP_NBLOCKS
)
5754 *lengthp
= sizeof (uint64_t);
5755 else if ((dp
== DP_BLKSIZE
) || (dp
== DP_SSD
))
5756 *lengthp
= sizeof (uint32_t);
5758 /* service request for the length of the property */
5759 if (prop_op
== PROP_LEN
)
5760 return (DDI_PROP_SUCCESS
);
5763 case PROP_LEN_AND_VAL_ALLOC
:
5764 if ((buffer
= kmem_alloc(*lengthp
,
5765 (mod_flags
& DDI_PROP_CANSLEEP
) ?
5766 KM_SLEEP
: KM_NOSLEEP
)) == NULL
)
5767 return (DDI_PROP_NO_MEMORY
);
5768 *(caddr_t
*)valuep
= buffer
; /* set callers buf */
5771 case PROP_LEN_AND_VAL_BUF
:
5772 /* the length of the prop and the request must match */
5773 if (callers_length
!= *lengthp
)
5774 return (DDI_PROP_INVAL_ARG
);
5775 buffer
= valuep
; /* get callers buf */
5779 return (DDI_PROP_INVAL_ARG
);
5782 /* transfer the value into the buffer */
5785 *((uint64_t *)buffer
) = capacity
;
5788 *((uint32_t *)buffer
) = lbasize
;
5791 if (DK_TG_GETATTRIBUTE(cl
, &tgattr
, tg_cookie
) != 0)
5792 tgattr
.media_is_solid_state
= B_FALSE
;
5793 *((uint32_t *)buffer
) =
5794 tgattr
.media_is_solid_state
? 1 : 0;
5797 if (DK_TG_GETATTRIBUTE(cl
, &tgattr
, tg_cookie
) != 0)
5798 tgattr
.media_is_rotational
= B_TRUE
;
5799 *((uint32_t *)buffer
) =
5800 tgattr
.media_is_rotational
? 1 : 0;
5803 return (DDI_PROP_SUCCESS
);
5807 * Support dynamic size oriented properties of partition. Requests
5808 * issued under conditions where size is valid are passed to
5809 * ddi_prop_op_nblocks with the size information, otherwise the
5810 * request is passed to ddi_prop_op. Size depends on valid geometry.
5812 if (!cmlb_is_valid(cmlbhandle
))
5815 /* Get partition nblocks value. */
5816 (void) cmlb_partinfo(cmlbhandle
, part
,
5817 (diskaddr_t
*)&nblocks64
, NULL
, NULL
, NULL
, tg_cookie
);
5820 * Assume partition information is in sys_blocksize units, compute
5821 * divisor for size(9P) property representation.
5823 dblk
= lbasize
/ cl
->cl_sys_blocksize
;
5825 /* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5826 return (ddi_prop_op_nblocks_blksize(dev
, dip
, prop_op
, mod_flags
,
5827 name
, valuep
, lengthp
, nblocks64
/ dblk
, lbasize
));