13ccda647Slclee /*
23ccda647Slclee * CDDL HEADER START
33ccda647Slclee *
43ccda647Slclee * The contents of this file are subject to the terms of the
5e8fb11a1Sshidokht * Common Development and Distribution License (the "License").
6e8fb11a1Sshidokht * You may not use this file except in compliance with the License.
73ccda647Slclee *
83ccda647Slclee * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93ccda647Slclee * or http://www.opensolaris.org/os/licensing.
103ccda647Slclee * See the License for the specific language governing permissions
113ccda647Slclee * and limitations under the License.
123ccda647Slclee *
133ccda647Slclee * When distributing Covered Code, include this CDDL HEADER in each
143ccda647Slclee * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153ccda647Slclee * If applicable, add the following below this CDDL HEADER, with the
163ccda647Slclee * fields enclosed by brackets "[]" replaced with your own identifying
173ccda647Slclee * information: Portions Copyright [yyyy] [name of copyright owner]
183ccda647Slclee *
193ccda647Slclee * CDDL HEADER END
203ccda647Slclee */
213ccda647Slclee
223ccda647Slclee /*
23fc2d3b38SGarrett D'Amore * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
247e74b1cbSSharath M Srinivasan * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
253ccda647Slclee * Use is subject to license terms.
263ccda647Slclee */
273ccda647Slclee
283ccda647Slclee /*
293ccda647Slclee * This module provides support for labeling operations for target
303ccda647Slclee * drivers.
313ccda647Slclee */
323ccda647Slclee
333ccda647Slclee #include <sys/scsi/scsi.h>
343ccda647Slclee #include <sys/sunddi.h>
353ccda647Slclee #include <sys/dklabel.h>
363ccda647Slclee #include <sys/dkio.h>
373ccda647Slclee #include <sys/vtoc.h>
383ccda647Slclee #include <sys/dktp/fdisk.h>
393ccda647Slclee #include <sys/vtrace.h>
403ccda647Slclee #include <sys/efi_partition.h>
413ccda647Slclee #include <sys/cmlb.h>
423ccda647Slclee #include <sys/cmlb_impl.h>
43aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
44aa1b14e7SSheshadri Vasudevan #include <sys/fs/dv_node.h>
45aa1b14e7SSheshadri Vasudevan #endif
46b9ccdc5aScth #include <sys/ddi_impldefs.h>
473ccda647Slclee
483ccda647Slclee /*
493ccda647Slclee * Driver minor node structure and data table
503ccda647Slclee */
513ccda647Slclee struct driver_minor_data {
523ccda647Slclee char *name;
533ccda647Slclee minor_t minor;
543ccda647Slclee int type;
553ccda647Slclee };
563ccda647Slclee
573ccda647Slclee static struct driver_minor_data dk_minor_data[] = {
583ccda647Slclee {"a", 0, S_IFBLK},
593ccda647Slclee {"b", 1, S_IFBLK},
603ccda647Slclee {"c", 2, S_IFBLK},
613ccda647Slclee {"d", 3, S_IFBLK},
623ccda647Slclee {"e", 4, S_IFBLK},
633ccda647Slclee {"f", 5, S_IFBLK},
643ccda647Slclee {"g", 6, S_IFBLK},
653ccda647Slclee {"h", 7, S_IFBLK},
663ccda647Slclee #if defined(_SUNOS_VTOC_16)
673ccda647Slclee {"i", 8, S_IFBLK},
683ccda647Slclee {"j", 9, S_IFBLK},
693ccda647Slclee {"k", 10, S_IFBLK},
703ccda647Slclee {"l", 11, S_IFBLK},
713ccda647Slclee {"m", 12, S_IFBLK},
723ccda647Slclee {"n", 13, S_IFBLK},
733ccda647Slclee {"o", 14, S_IFBLK},
743ccda647Slclee {"p", 15, S_IFBLK},
753ccda647Slclee #endif /* defined(_SUNOS_VTOC_16) */
763ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
773ccda647Slclee {"q", 16, S_IFBLK},
783ccda647Slclee {"r", 17, S_IFBLK},
793ccda647Slclee {"s", 18, S_IFBLK},
803ccda647Slclee {"t", 19, S_IFBLK},
813ccda647Slclee {"u", 20, S_IFBLK},
823ccda647Slclee #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
833ccda647Slclee {"a,raw", 0, S_IFCHR},
843ccda647Slclee {"b,raw", 1, S_IFCHR},
853ccda647Slclee {"c,raw", 2, S_IFCHR},
863ccda647Slclee {"d,raw", 3, S_IFCHR},
873ccda647Slclee {"e,raw", 4, S_IFCHR},
883ccda647Slclee {"f,raw", 5, S_IFCHR},
893ccda647Slclee {"g,raw", 6, S_IFCHR},
903ccda647Slclee {"h,raw", 7, S_IFCHR},
913ccda647Slclee #if defined(_SUNOS_VTOC_16)
923ccda647Slclee {"i,raw", 8, S_IFCHR},
933ccda647Slclee {"j,raw", 9, S_IFCHR},
943ccda647Slclee {"k,raw", 10, S_IFCHR},
953ccda647Slclee {"l,raw", 11, S_IFCHR},
963ccda647Slclee {"m,raw", 12, S_IFCHR},
973ccda647Slclee {"n,raw", 13, S_IFCHR},
983ccda647Slclee {"o,raw", 14, S_IFCHR},
993ccda647Slclee {"p,raw", 15, S_IFCHR},
1003ccda647Slclee #endif /* defined(_SUNOS_VTOC_16) */
1013ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
1023ccda647Slclee {"q,raw", 16, S_IFCHR},
1033ccda647Slclee {"r,raw", 17, S_IFCHR},
1043ccda647Slclee {"s,raw", 18, S_IFCHR},
1053ccda647Slclee {"t,raw", 19, S_IFCHR},
1063ccda647Slclee {"u,raw", 20, S_IFCHR},
1073ccda647Slclee #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
1083ccda647Slclee {0}
1093ccda647Slclee };
1103ccda647Slclee
111aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
112aa1b14e7SSheshadri Vasudevan #if defined(_FIRMWARE_NEEDS_FDISK)
113aa1b14e7SSheshadri Vasudevan static struct driver_minor_data dk_ext_minor_data[] = {
114aa1b14e7SSheshadri Vasudevan {"p5", 21, S_IFBLK},
115aa1b14e7SSheshadri Vasudevan {"p6", 22, S_IFBLK},
116aa1b14e7SSheshadri Vasudevan {"p7", 23, S_IFBLK},
117aa1b14e7SSheshadri Vasudevan {"p8", 24, S_IFBLK},
118aa1b14e7SSheshadri Vasudevan {"p9", 25, S_IFBLK},
119aa1b14e7SSheshadri Vasudevan {"p10", 26, S_IFBLK},
120aa1b14e7SSheshadri Vasudevan {"p11", 27, S_IFBLK},
121aa1b14e7SSheshadri Vasudevan {"p12", 28, S_IFBLK},
122aa1b14e7SSheshadri Vasudevan {"p13", 29, S_IFBLK},
123aa1b14e7SSheshadri Vasudevan {"p14", 30, S_IFBLK},
124aa1b14e7SSheshadri Vasudevan {"p15", 31, S_IFBLK},
125aa1b14e7SSheshadri Vasudevan {"p16", 32, S_IFBLK},
126aa1b14e7SSheshadri Vasudevan {"p17", 33, S_IFBLK},
127aa1b14e7SSheshadri Vasudevan {"p18", 34, S_IFBLK},
128aa1b14e7SSheshadri Vasudevan {"p19", 35, S_IFBLK},
129aa1b14e7SSheshadri Vasudevan {"p20", 36, S_IFBLK},
130aa1b14e7SSheshadri Vasudevan {"p21", 37, S_IFBLK},
131aa1b14e7SSheshadri Vasudevan {"p22", 38, S_IFBLK},
132aa1b14e7SSheshadri Vasudevan {"p23", 39, S_IFBLK},
133aa1b14e7SSheshadri Vasudevan {"p24", 40, S_IFBLK},
134aa1b14e7SSheshadri Vasudevan {"p25", 41, S_IFBLK},
135aa1b14e7SSheshadri Vasudevan {"p26", 42, S_IFBLK},
136aa1b14e7SSheshadri Vasudevan {"p27", 43, S_IFBLK},
137aa1b14e7SSheshadri Vasudevan {"p28", 44, S_IFBLK},
138aa1b14e7SSheshadri Vasudevan {"p29", 45, S_IFBLK},
139aa1b14e7SSheshadri Vasudevan {"p30", 46, S_IFBLK},
140aa1b14e7SSheshadri Vasudevan {"p31", 47, S_IFBLK},
141aa1b14e7SSheshadri Vasudevan {"p32", 48, S_IFBLK},
142aa1b14e7SSheshadri Vasudevan {"p33", 49, S_IFBLK},
143aa1b14e7SSheshadri Vasudevan {"p34", 50, S_IFBLK},
144aa1b14e7SSheshadri Vasudevan {"p35", 51, S_IFBLK},
145aa1b14e7SSheshadri Vasudevan {"p36", 52, S_IFBLK},
146aa1b14e7SSheshadri Vasudevan {"p5,raw", 21, S_IFCHR},
147aa1b14e7SSheshadri Vasudevan {"p6,raw", 22, S_IFCHR},
148aa1b14e7SSheshadri Vasudevan {"p7,raw", 23, S_IFCHR},
149aa1b14e7SSheshadri Vasudevan {"p8,raw", 24, S_IFCHR},
150aa1b14e7SSheshadri Vasudevan {"p9,raw", 25, S_IFCHR},
151aa1b14e7SSheshadri Vasudevan {"p10,raw", 26, S_IFCHR},
152aa1b14e7SSheshadri Vasudevan {"p11,raw", 27, S_IFCHR},
153aa1b14e7SSheshadri Vasudevan {"p12,raw", 28, S_IFCHR},
154aa1b14e7SSheshadri Vasudevan {"p13,raw", 29, S_IFCHR},
155aa1b14e7SSheshadri Vasudevan {"p14,raw", 30, S_IFCHR},
156aa1b14e7SSheshadri Vasudevan {"p15,raw", 31, S_IFCHR},
157aa1b14e7SSheshadri Vasudevan {"p16,raw", 32, S_IFCHR},
158aa1b14e7SSheshadri Vasudevan {"p17,raw", 33, S_IFCHR},
159aa1b14e7SSheshadri Vasudevan {"p18,raw", 34, S_IFCHR},
160aa1b14e7SSheshadri Vasudevan {"p19,raw", 35, S_IFCHR},
161aa1b14e7SSheshadri Vasudevan {"p20,raw", 36, S_IFCHR},
162aa1b14e7SSheshadri Vasudevan {"p21,raw", 37, S_IFCHR},
163aa1b14e7SSheshadri Vasudevan {"p22,raw", 38, S_IFCHR},
164aa1b14e7SSheshadri Vasudevan {"p23,raw", 39, S_IFCHR},
165aa1b14e7SSheshadri Vasudevan {"p24,raw", 40, S_IFCHR},
166aa1b14e7SSheshadri Vasudevan {"p25,raw", 41, S_IFCHR},
167aa1b14e7SSheshadri Vasudevan {"p26,raw", 42, S_IFCHR},
168aa1b14e7SSheshadri Vasudevan {"p27,raw", 43, S_IFCHR},
169aa1b14e7SSheshadri Vasudevan {"p28,raw", 44, S_IFCHR},
170aa1b14e7SSheshadri Vasudevan {"p29,raw", 45, S_IFCHR},
171aa1b14e7SSheshadri Vasudevan {"p30,raw", 46, S_IFCHR},
172aa1b14e7SSheshadri Vasudevan {"p31,raw", 47, S_IFCHR},
173aa1b14e7SSheshadri Vasudevan {"p32,raw", 48, S_IFCHR},
174aa1b14e7SSheshadri Vasudevan {"p33,raw", 49, S_IFCHR},
175aa1b14e7SSheshadri Vasudevan {"p34,raw", 50, S_IFCHR},
176aa1b14e7SSheshadri Vasudevan {"p35,raw", 51, S_IFCHR},
177aa1b14e7SSheshadri Vasudevan {"p36,raw", 52, S_IFCHR},
178aa1b14e7SSheshadri Vasudevan {0}
179aa1b14e7SSheshadri Vasudevan };
180aa1b14e7SSheshadri Vasudevan #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
181aa1b14e7SSheshadri Vasudevan #endif /* if defined(__i386) || defined(__amd64) */
182aa1b14e7SSheshadri Vasudevan
1833ccda647Slclee static struct driver_minor_data dk_minor_data_efi[] = {
1843ccda647Slclee {"a", 0, S_IFBLK},
1853ccda647Slclee {"b", 1, S_IFBLK},
1863ccda647Slclee {"c", 2, S_IFBLK},
1873ccda647Slclee {"d", 3, S_IFBLK},
1883ccda647Slclee {"e", 4, S_IFBLK},
1893ccda647Slclee {"f", 5, S_IFBLK},
1903ccda647Slclee {"g", 6, S_IFBLK},
1913ccda647Slclee {"wd", 7, S_IFBLK},
1927e70b434SShidokht Yadegari #if defined(_SUNOS_VTOC_16)
1937e70b434SShidokht Yadegari {"i", 8, S_IFBLK},
1947e70b434SShidokht Yadegari {"j", 9, S_IFBLK},
1957e70b434SShidokht Yadegari {"k", 10, S_IFBLK},
1967e70b434SShidokht Yadegari {"l", 11, S_IFBLK},
1977e70b434SShidokht Yadegari {"m", 12, S_IFBLK},
1987e70b434SShidokht Yadegari {"n", 13, S_IFBLK},
1997e70b434SShidokht Yadegari {"o", 14, S_IFBLK},
2007e70b434SShidokht Yadegari {"p", 15, S_IFBLK},
2017e70b434SShidokht Yadegari #endif /* defined(_SUNOS_VTOC_16) */
2023ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
2033ccda647Slclee {"q", 16, S_IFBLK},
2043ccda647Slclee {"r", 17, S_IFBLK},
2053ccda647Slclee {"s", 18, S_IFBLK},
2063ccda647Slclee {"t", 19, S_IFBLK},
2073ccda647Slclee {"u", 20, S_IFBLK},
2083ccda647Slclee #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
2093ccda647Slclee {"a,raw", 0, S_IFCHR},
2103ccda647Slclee {"b,raw", 1, S_IFCHR},
2113ccda647Slclee {"c,raw", 2, S_IFCHR},
2123ccda647Slclee {"d,raw", 3, S_IFCHR},
2133ccda647Slclee {"e,raw", 4, S_IFCHR},
2143ccda647Slclee {"f,raw", 5, S_IFCHR},
2153ccda647Slclee {"g,raw", 6, S_IFCHR},
2163ccda647Slclee {"wd,raw", 7, S_IFCHR},
2177e70b434SShidokht Yadegari #if defined(_SUNOS_VTOC_16)
2187e70b434SShidokht Yadegari {"i,raw", 8, S_IFCHR},
2197e70b434SShidokht Yadegari {"j,raw", 9, S_IFCHR},
2207e70b434SShidokht Yadegari {"k,raw", 10, S_IFCHR},
2217e70b434SShidokht Yadegari {"l,raw", 11, S_IFCHR},
2227e70b434SShidokht Yadegari {"m,raw", 12, S_IFCHR},
2237e70b434SShidokht Yadegari {"n,raw", 13, S_IFCHR},
2247e70b434SShidokht Yadegari {"o,raw", 14, S_IFCHR},
2257e70b434SShidokht Yadegari {"p,raw", 15, S_IFCHR},
2267e70b434SShidokht Yadegari #endif /* defined(_SUNOS_VTOC_16) */
2273ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
2283ccda647Slclee {"q,raw", 16, S_IFCHR},
2293ccda647Slclee {"r,raw", 17, S_IFCHR},
2303ccda647Slclee {"s,raw", 18, S_IFCHR},
2313ccda647Slclee {"t,raw", 19, S_IFCHR},
2323ccda647Slclee {"u,raw", 20, S_IFCHR},
2333ccda647Slclee #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
2343ccda647Slclee {0}
2353ccda647Slclee };
2363ccda647Slclee
237b9ccdc5aScth /*
238b9ccdc5aScth * Declare the dynamic properties implemented in prop_op(9E) implementation
239b9ccdc5aScth * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
240b9ccdc5aScth * of drivers that call cmlb_attach().
241b9ccdc5aScth */
242b9ccdc5aScth static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
243b9ccdc5aScth {"Nblocks", DDI_PROP_TYPE_INT64, S_IFBLK},
244b9ccdc5aScth {"Size", DDI_PROP_TYPE_INT64, S_IFCHR},
245b9ccdc5aScth {"device-nblocks", DDI_PROP_TYPE_INT64},
246b9ccdc5aScth {"device-blksize", DDI_PROP_TYPE_INT},
247fc2d3b38SGarrett D'Amore {"device-solid-state", DDI_PROP_TYPE_INT},
248*89c81bb4SYuri Pankov {"device-rotational", DDI_PROP_TYPE_INT},
249b9ccdc5aScth {NULL}
250b9ccdc5aScth };
2513ccda647Slclee
25206bbe1e0Sedp /*
253c31fac72SShidokht Yadegari * This implies an upper limit of 8192 GPT partitions
254c31fac72SShidokht Yadegari * in one transfer for GUID Partition Entry Array.
255c31fac72SShidokht Yadegari */
256c31fac72SShidokht Yadegari len_t cmlb_tg_max_efi_xfer = 1024 * 1024;
257c31fac72SShidokht Yadegari
258c31fac72SShidokht Yadegari /*
25906bbe1e0Sedp * External kernel interfaces
26006bbe1e0Sedp */
2613ccda647Slclee extern struct mod_ops mod_miscops;
2623ccda647Slclee
26306bbe1e0Sedp extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
26406bbe1e0Sedp int spec_type, minor_t minor_num);
26506bbe1e0Sedp
2663ccda647Slclee /*
2673ccda647Slclee * Global buffer and mutex for debug logging
2683ccda647Slclee */
2693ccda647Slclee static char cmlb_log_buffer[1024];
2703ccda647Slclee static kmutex_t cmlb_log_mutex;
2713ccda647Slclee
2723ccda647Slclee
273e8fb11a1Sshidokht struct cmlb_lun *cmlb_debug_cl = NULL;
2743ccda647Slclee uint_t cmlb_level_mask = 0x0;
2753ccda647Slclee
2763ccda647Slclee int cmlb_rot_delay = 4; /* default rotational delay */
2773ccda647Slclee
2783ccda647Slclee static struct modlmisc modlmisc = {
2793ccda647Slclee &mod_miscops, /* Type of module */
280342440ecSPrasad Singamsetty "Common Labeling module"
2813ccda647Slclee };
2823ccda647Slclee
2833ccda647Slclee static struct modlinkage modlinkage = {
2843ccda647Slclee MODREV_1, (void *)&modlmisc, NULL
2853ccda647Slclee };
2863ccda647Slclee
2873ccda647Slclee /* Local function prototypes */
288e8fb11a1Sshidokht static dev_t cmlb_make_device(struct cmlb_lun *cl);
2897f0b8309SEdward Pilatowicz static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
290e8fb11a1Sshidokht int flags, void *tg_cookie);
291e8fb11a1Sshidokht static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
292e8fb11a1Sshidokht void *tg_cookie);
293e8fb11a1Sshidokht static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
294e8fb11a1Sshidokht void *tg_cookie);
2953ccda647Slclee static void cmlb_swap_efi_gpt(efi_gpt_t *e);
2963ccda647Slclee static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
2973ccda647Slclee static int cmlb_validate_efi(efi_gpt_t *labp);
298e8fb11a1Sshidokht static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
299e8fb11a1Sshidokht void *tg_cookie);
300e8fb11a1Sshidokht static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
301e8fb11a1Sshidokht static int cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *l, int flags);
302e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
303e8fb11a1Sshidokht static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
304e8fb11a1Sshidokht #endif
305e8fb11a1Sshidokht static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
306e8fb11a1Sshidokht static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
307e8fb11a1Sshidokht static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
308e8fb11a1Sshidokht void *tg_cookie);
309e8fb11a1Sshidokht static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
310e8fb11a1Sshidokht static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
311e8fb11a1Sshidokht static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
312e8fb11a1Sshidokht static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
313e8fb11a1Sshidokht static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
3147f0b8309SEdward Pilatowicz static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
3153ccda647Slclee
3163ccda647Slclee #if defined(__i386) || defined(__amd64)
317e8fb11a1Sshidokht static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
3183ccda647Slclee #endif
3193ccda647Slclee
3203ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
3217f0b8309SEdward Pilatowicz static boolean_t cmlb_has_max_chs_vals(struct ipart *fdp);
3223ccda647Slclee #endif
3233ccda647Slclee
3243ccda647Slclee #if defined(_SUNOS_VTOC_16)
325b081f1c4Syu, larry liu - Sun Microsystems - Beijing China static void cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
326b081f1c4Syu, larry liu - Sun Microsystems - Beijing China struct dk_geom *cl_g, void *tg_cookie);
3273ccda647Slclee #endif
3283ccda647Slclee
329e8fb11a1Sshidokht static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
330e8fb11a1Sshidokht void *tg_cookie);
331e8fb11a1Sshidokht static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
332e8fb11a1Sshidokht static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
333e8fb11a1Sshidokht void *tg_cookie);
334e8fb11a1Sshidokht static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
335e8fb11a1Sshidokht static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
336e8fb11a1Sshidokht void *tg_cookie);
337e8fb11a1Sshidokht static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
338e8fb11a1Sshidokht int flag, void *tg_cookie);
339e8fb11a1Sshidokht static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
340e8fb11a1Sshidokht void *tg_cookie);
341342440ecSPrasad Singamsetty static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
342342440ecSPrasad Singamsetty void *tg_cookie);
343e8fb11a1Sshidokht static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
344e8fb11a1Sshidokht int flag, void *tg_cookie);
345342440ecSPrasad Singamsetty static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
346342440ecSPrasad Singamsetty int flag, void *tg_cookie);
347e8fb11a1Sshidokht static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
348e8fb11a1Sshidokht void *tg_cookie);
349e8fb11a1Sshidokht static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
350e8fb11a1Sshidokht void *tg_cookie);
351e8fb11a1Sshidokht static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
352e8fb11a1Sshidokht void *tg_cookie);
3533ccda647Slclee
3543ccda647Slclee #if defined(__i386) || defined(__amd64)
355aa1b14e7SSheshadri Vasudevan static int cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
356aa1b14e7SSheshadri Vasudevan void *tg_cookie);
357aa1b14e7SSheshadri Vasudevan static int cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart,
358aa1b14e7SSheshadri Vasudevan uint32_t start, uint32_t size);
359aa1b14e7SSheshadri Vasudevan static int cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start,
360aa1b14e7SSheshadri Vasudevan void *tg_cookie);
361e8fb11a1Sshidokht static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
362b081f1c4Syu, larry liu - Sun Microsystems - Beijing China static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
363b081f1c4Syu, larry liu - Sun Microsystems - Beijing China void *tg_cookie);
364e8fb11a1Sshidokht static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
3653ccda647Slclee int flag);
366342440ecSPrasad Singamsetty static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
367342440ecSPrasad Singamsetty int flag);
3683ccda647Slclee #endif
3693ccda647Slclee
370e8fb11a1Sshidokht static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
371c31fac72SShidokht Yadegari static void cmlb_v_log(dev_info_t *dev, const char *label, uint_t level,
3723ccda647Slclee const char *fmt, va_list ap);
373c31fac72SShidokht Yadegari static void cmlb_log(dev_info_t *dev, const char *label, uint_t level,
3743ccda647Slclee const char *fmt, ...);
3753ccda647Slclee
3763ccda647Slclee int
_init(void)3773ccda647Slclee _init(void)
3783ccda647Slclee {
3793ccda647Slclee mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
3803ccda647Slclee return (mod_install(&modlinkage));
3813ccda647Slclee }
3823ccda647Slclee
3833ccda647Slclee int
_info(struct modinfo * modinfop)3843ccda647Slclee _info(struct modinfo *modinfop)
3853ccda647Slclee {
3863ccda647Slclee return (mod_info(&modlinkage, modinfop));
3873ccda647Slclee }
3883ccda647Slclee
3893ccda647Slclee int
_fini(void)3903ccda647Slclee _fini(void)
3913ccda647Slclee {
3923ccda647Slclee int err;
3933ccda647Slclee
3943ccda647Slclee if ((err = mod_remove(&modlinkage)) != 0) {
3953ccda647Slclee return (err);
3963ccda647Slclee }
3973ccda647Slclee
3983ccda647Slclee mutex_destroy(&cmlb_log_mutex);
3993ccda647Slclee return (err);
4003ccda647Slclee }
4013ccda647Slclee
4023ccda647Slclee /*
4033ccda647Slclee * cmlb_dbg is used for debugging to log additional info
4043ccda647Slclee * Level of output is controlled via cmlb_level_mask setting.
4053ccda647Slclee */
4063ccda647Slclee static void
cmlb_dbg(uint_t comp,struct cmlb_lun * cl,const char * fmt,...)407e8fb11a1Sshidokht cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
4083ccda647Slclee {
4093ccda647Slclee va_list ap;
4103ccda647Slclee dev_info_t *dev;
4113ccda647Slclee uint_t level_mask = 0;
4123ccda647Slclee
413e8fb11a1Sshidokht ASSERT(cl != NULL);
414e8fb11a1Sshidokht dev = CMLB_DEVINFO(cl);
4153ccda647Slclee ASSERT(dev != NULL);
4163ccda647Slclee /*
4173ccda647Slclee * Filter messages based on the global component and level masks,
418e8fb11a1Sshidokht * also print if cl matches the value of cmlb_debug_cl, or if
419e8fb11a1Sshidokht * cmlb_debug_cl is set to NULL.
4203ccda647Slclee */
4213ccda647Slclee if (comp & CMLB_TRACE)
4223ccda647Slclee level_mask |= CMLB_LOGMASK_TRACE;
4233ccda647Slclee
4243ccda647Slclee if (comp & CMLB_INFO)
4253ccda647Slclee level_mask |= CMLB_LOGMASK_INFO;
4263ccda647Slclee
4273ccda647Slclee if (comp & CMLB_ERROR)
4283ccda647Slclee level_mask |= CMLB_LOGMASK_ERROR;
4293ccda647Slclee
4303ccda647Slclee if ((cmlb_level_mask & level_mask) &&
431e8fb11a1Sshidokht ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
4323ccda647Slclee va_start(ap, fmt);
433e8fb11a1Sshidokht cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
4343ccda647Slclee va_end(ap);
4353ccda647Slclee }
4363ccda647Slclee }
4373ccda647Slclee
4383ccda647Slclee /*
4393ccda647Slclee * cmlb_log is basically a duplicate of scsi_log. It is redefined here
4403ccda647Slclee * so that this module does not depend on scsi module.
4413ccda647Slclee */
4423ccda647Slclee static void
cmlb_log(dev_info_t * dev,const char * label,uint_t level,const char * fmt,...)443c31fac72SShidokht Yadegari cmlb_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt, ...)
4443ccda647Slclee {
4453ccda647Slclee va_list ap;
4463ccda647Slclee
4473ccda647Slclee va_start(ap, fmt);
4483ccda647Slclee cmlb_v_log(dev, label, level, fmt, ap);
4493ccda647Slclee va_end(ap);
4503ccda647Slclee }
4513ccda647Slclee
4523ccda647Slclee static void
cmlb_v_log(dev_info_t * dev,const char * label,uint_t level,const char * fmt,va_list ap)453c31fac72SShidokht Yadegari cmlb_v_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt,
4543ccda647Slclee va_list ap)
4553ccda647Slclee {
4563ccda647Slclee static char name[256];
4573ccda647Slclee int log_only = 0;
4583ccda647Slclee int boot_only = 0;
4593ccda647Slclee int console_only = 0;
4603ccda647Slclee
4613ccda647Slclee mutex_enter(&cmlb_log_mutex);
4623ccda647Slclee
4633ccda647Slclee if (dev) {
4643ccda647Slclee if (level == CE_PANIC || level == CE_WARN ||
4653ccda647Slclee level == CE_NOTE) {
4663ccda647Slclee (void) sprintf(name, "%s (%s%d):\n",
4673ccda647Slclee ddi_pathname(dev, cmlb_log_buffer),
4683ccda647Slclee label, ddi_get_instance(dev));
4693ccda647Slclee } else {
4703ccda647Slclee name[0] = '\0';
4713ccda647Slclee }
4723ccda647Slclee } else {
4733ccda647Slclee (void) sprintf(name, "%s:", label);
4743ccda647Slclee }
4753ccda647Slclee
4763ccda647Slclee (void) vsprintf(cmlb_log_buffer, fmt, ap);
4773ccda647Slclee
4783ccda647Slclee switch (cmlb_log_buffer[0]) {
4793ccda647Slclee case '!':
4803ccda647Slclee log_only = 1;
4813ccda647Slclee break;
4823ccda647Slclee case '?':
4833ccda647Slclee boot_only = 1;
4843ccda647Slclee break;
4853ccda647Slclee case '^':
4863ccda647Slclee console_only = 1;
4873ccda647Slclee break;
4883ccda647Slclee }
4893ccda647Slclee
4903ccda647Slclee switch (level) {
4913ccda647Slclee case CE_NOTE:
4923ccda647Slclee level = CE_CONT;
4933ccda647Slclee /* FALLTHROUGH */
4943ccda647Slclee case CE_CONT:
4953ccda647Slclee case CE_WARN:
4963ccda647Slclee case CE_PANIC:
4973ccda647Slclee if (boot_only) {
4983ccda647Slclee cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
4993ccda647Slclee } else if (console_only) {
5003ccda647Slclee cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
5013ccda647Slclee } else if (log_only) {
5023ccda647Slclee cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
5033ccda647Slclee } else {
5043ccda647Slclee cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
5053ccda647Slclee }
5063ccda647Slclee break;
5073ccda647Slclee case CE_IGNORE:
5083ccda647Slclee break;
5093ccda647Slclee default:
5103ccda647Slclee cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
5113ccda647Slclee break;
5123ccda647Slclee }
5133ccda647Slclee mutex_exit(&cmlb_log_mutex);
5143ccda647Slclee }
5153ccda647Slclee
5163ccda647Slclee
5173ccda647Slclee /*
5183ccda647Slclee * cmlb_alloc_handle:
5193ccda647Slclee *
5203ccda647Slclee * Allocates a handle.
5213ccda647Slclee *
5223ccda647Slclee * Arguments:
5233ccda647Slclee * cmlbhandlep pointer to handle
5243ccda647Slclee *
5253ccda647Slclee * Notes:
5263ccda647Slclee * Allocates a handle and stores the allocated handle in the area
5273ccda647Slclee * pointed to by cmlbhandlep
5283ccda647Slclee *
5293ccda647Slclee * Context:
5303ccda647Slclee * Kernel thread only (can sleep).
5313ccda647Slclee */
5323ccda647Slclee void
cmlb_alloc_handle(cmlb_handle_t * cmlbhandlep)5333ccda647Slclee cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
5343ccda647Slclee {
535e8fb11a1Sshidokht struct cmlb_lun *cl;
5363ccda647Slclee
537e8fb11a1Sshidokht cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
5383ccda647Slclee ASSERT(cmlbhandlep != NULL);
5393ccda647Slclee
540e8fb11a1Sshidokht cl->cl_state = CMLB_INITED;
541e8fb11a1Sshidokht cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
542e8fb11a1Sshidokht mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
5433ccda647Slclee
544e8fb11a1Sshidokht *cmlbhandlep = (cmlb_handle_t)(cl);
5453ccda647Slclee }
5463ccda647Slclee
5473ccda647Slclee /*
5483ccda647Slclee * cmlb_free_handle
5493ccda647Slclee *
5503ccda647Slclee * Frees handle.
5513ccda647Slclee *
5523ccda647Slclee * Arguments:
5533ccda647Slclee * cmlbhandlep pointer to handle
5543ccda647Slclee */
5553ccda647Slclee void
cmlb_free_handle(cmlb_handle_t * cmlbhandlep)5563ccda647Slclee cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
5573ccda647Slclee {
558e8fb11a1Sshidokht struct cmlb_lun *cl;
5593ccda647Slclee
560e8fb11a1Sshidokht cl = (struct cmlb_lun *)*cmlbhandlep;
561e8fb11a1Sshidokht if (cl != NULL) {
562e8fb11a1Sshidokht mutex_destroy(CMLB_MUTEX(cl));
563e8fb11a1Sshidokht kmem_free(cl, sizeof (struct cmlb_lun));
5643ccda647Slclee }
5653ccda647Slclee
5663ccda647Slclee }
5673ccda647Slclee
5683ccda647Slclee /*
5693ccda647Slclee * cmlb_attach:
5703ccda647Slclee *
5713ccda647Slclee * Attach handle to device, create minor nodes for device.
5723ccda647Slclee *
5733ccda647Slclee * Arguments:
5743ccda647Slclee * devi pointer to device's dev_info structure.
5753ccda647Slclee * tgopsp pointer to array of functions cmlb can use to callback
5763ccda647Slclee * to target driver.
5773ccda647Slclee *
5783ccda647Slclee * device_type Peripheral device type as defined in
5793ccda647Slclee * scsi/generic/inquiry.h
5803ccda647Slclee *
5813ccda647Slclee * is_removable whether or not device is removable.
5823ccda647Slclee *
583e8fb11a1Sshidokht * is_hotpluggable whether or not device is hotpluggable.
584e8fb11a1Sshidokht *
5853ccda647Slclee * node_type minor node type (as used by ddi_create_minor_node)
5863ccda647Slclee *
5873ccda647Slclee * alter_behavior
5883ccda647Slclee * bit flags:
5893ccda647Slclee *
5903ccda647Slclee * CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
5913ccda647Slclee * an alternate slice for the default label, if
5923ccda647Slclee * device type is DTYPE_DIRECT an architectures default
5933ccda647Slclee * label type is VTOC16.
5943ccda647Slclee * Otherwise alternate slice will no be created.
5953ccda647Slclee *
5963ccda647Slclee *
5973ccda647Slclee * CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
5983ccda647Slclee * geometry and label for DKIOCGGEOM and DKIOCGVTOC
5993ccda647Slclee * on architecture with VTOC8 label types.
6003ccda647Slclee *
601e8fb11a1Sshidokht * CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
602e8fb11a1Sshidokht * one bug in obtaining capacity (in sd):
603e8fb11a1Sshidokht * SCSI READ_CAPACITY command returns the LBA number of the
604e8fb11a1Sshidokht * last logical block, but sd once treated this number as
605e8fb11a1Sshidokht * disks' capacity on x86 platform. And LBAs are addressed
606e8fb11a1Sshidokht * based 0. So the last block was lost on x86 platform.
607e8fb11a1Sshidokht *
608e8fb11a1Sshidokht * Now, we remove this workaround. In order for present sd
609e8fb11a1Sshidokht * driver to work with disks which are labeled/partitioned
610e8fb11a1Sshidokht * via previous sd, we add workaround as follows:
611e8fb11a1Sshidokht *
612e8fb11a1Sshidokht * 1) Locate backup EFI label: cmlb searches the next to
613e8fb11a1Sshidokht * last
614e8fb11a1Sshidokht * block for backup EFI label. If fails, it will
615e8fb11a1Sshidokht * turn to the last block for backup EFI label;
616e8fb11a1Sshidokht *
617e8fb11a1Sshidokht * 2) Clear backup EFI label: cmlb first search the last
618e8fb11a1Sshidokht * block for backup EFI label, and will search the
619e8fb11a1Sshidokht * next to last block only if failed for the last
620e8fb11a1Sshidokht * block.
621e8fb11a1Sshidokht *
622e8fb11a1Sshidokht * 3) Calculate geometry:refer to cmlb_convert_geometry()
623e8fb11a1Sshidokht * If capacity increasing by 1 causes disks' capacity
624342440ecSPrasad Singamsetty * to cross over the limits in geometry calculation,
625e8fb11a1Sshidokht * geometry info will change. This will raise an issue:
626e8fb11a1Sshidokht * In case that primary VTOC label is destroyed, format
627e8fb11a1Sshidokht * commandline can restore it via backup VTOC labels.
628e8fb11a1Sshidokht * And format locates backup VTOC labels by use of
629e8fb11a1Sshidokht * geometry. So changing geometry will
630e8fb11a1Sshidokht * prevent format from finding backup VTOC labels. To
631e8fb11a1Sshidokht * eliminate this side effect for compatibility,
632e8fb11a1Sshidokht * sd uses (capacity -1) to calculate geometry;
633e8fb11a1Sshidokht *
634e8fb11a1Sshidokht * 4) 1TB disks: some important data structures use
635e8fb11a1Sshidokht * 32-bit signed long/int (for example, daddr_t),
636e8fb11a1Sshidokht * so that sd doesn't support a disk with capacity
637e8fb11a1Sshidokht * larger than 1TB on 32-bit platform. However,
638e8fb11a1Sshidokht * for exactly 1TB disk, it was treated as (1T - 512)B
639e8fb11a1Sshidokht * in the past, and could have valid Solaris
640e8fb11a1Sshidokht * partitions. To workaround this, if an exactly 1TB
641e8fb11a1Sshidokht * disk has Solaris fdisk partition, it will be allowed
642e8fb11a1Sshidokht * to work with sd.
643e8fb11a1Sshidokht *
644e8fb11a1Sshidokht *
6453ccda647Slclee *
646843e1988Sjohnlev * CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
647843e1988Sjohnlev * the entire disk, if there is no valid partition info.
648843e1988Sjohnlev * If there is a valid Solaris partition, s0 and s2 will
649843e1988Sjohnlev * only cover the entire Solaris partition.
650843e1988Sjohnlev *
651843e1988Sjohnlev *
6523ccda647Slclee * cmlbhandle cmlb handle associated with device
6533ccda647Slclee *
654e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
655e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
656e8fb11a1Sshidokht *
6573ccda647Slclee * Notes:
6583ccda647Slclee * Assumes a default label based on capacity for non-removable devices.
6593ccda647Slclee * If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
6603ccda647Slclee * for the architecture).
6613ccda647Slclee *
6623ccda647Slclee * For removable devices, default label type is assumed to be VTOC
6633ccda647Slclee * type. Create minor nodes based on a default label type.
6643ccda647Slclee * Label on the media is not validated.
6653ccda647Slclee * minor number consists of:
6663ccda647Slclee * if _SUNOS_VTOC_8 is defined
6673ccda647Slclee * lowest 3 bits is taken as partition number
6683ccda647Slclee * the rest is instance number
6693ccda647Slclee * if _SUNOS_VTOC_16 is defined
6703ccda647Slclee * lowest 6 bits is taken as partition number
6713ccda647Slclee * the rest is instance number
6723ccda647Slclee *
6733ccda647Slclee *
6743ccda647Slclee * Return values:
6753ccda647Slclee * 0 Success
6763ccda647Slclee * ENXIO creating minor nodes failed.
677e8fb11a1Sshidokht * EINVAL invalid arg, unsupported tg_ops version
6783ccda647Slclee */
6793ccda647Slclee int
cmlb_attach(dev_info_t * devi,cmlb_tg_ops_t * tgopsp,int device_type,boolean_t is_removable,boolean_t is_hotpluggable,char * node_type,int alter_behavior,cmlb_handle_t cmlbhandle,void * tg_cookie)6803ccda647Slclee cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
6817f0b8309SEdward Pilatowicz boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
682e8fb11a1Sshidokht int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
6833ccda647Slclee {
6843ccda647Slclee
685e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
6863ccda647Slclee diskaddr_t cap;
6873ccda647Slclee int status;
6883ccda647Slclee
6897f0b8309SEdward Pilatowicz ASSERT(VALID_BOOLEAN(is_removable));
6907f0b8309SEdward Pilatowicz ASSERT(VALID_BOOLEAN(is_hotpluggable));
6917f0b8309SEdward Pilatowicz
692e8fb11a1Sshidokht if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
693e8fb11a1Sshidokht return (EINVAL);
6943ccda647Slclee
695e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
696e8fb11a1Sshidokht
697e8fb11a1Sshidokht CMLB_DEVINFO(cl) = devi;
698e8fb11a1Sshidokht cl->cmlb_tg_ops = tgopsp;
699e8fb11a1Sshidokht cl->cl_device_type = device_type;
700e8fb11a1Sshidokht cl->cl_is_removable = is_removable;
701e8fb11a1Sshidokht cl->cl_is_hotpluggable = is_hotpluggable;
702e8fb11a1Sshidokht cl->cl_node_type = node_type;
703e8fb11a1Sshidokht cl->cl_sys_blocksize = DEV_BSIZE;
7047f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
705e8fb11a1Sshidokht cl->cl_def_labeltype = CMLB_LABEL_VTOC;
706e8fb11a1Sshidokht cl->cl_alter_behavior = alter_behavior;
707e8fb11a1Sshidokht cl->cl_reserved = -1;
708342440ecSPrasad Singamsetty cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
709aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
710aa1b14e7SSheshadri Vasudevan cl->cl_logical_drive_count = 0;
711aa1b14e7SSheshadri Vasudevan #endif
7123ccda647Slclee
7137f0b8309SEdward Pilatowicz if (!is_removable) {
714e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
715e8fb11a1Sshidokht status = DK_TG_GETCAP(cl, &cap, tg_cookie);
716e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
717342440ecSPrasad Singamsetty if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
718342440ecSPrasad Singamsetty /* set default EFI if > 2TB */
719e8fb11a1Sshidokht cl->cl_def_labeltype = CMLB_LABEL_EFI;
7203ccda647Slclee }
7213ccda647Slclee }
7223ccda647Slclee
7233ccda647Slclee /* create minor nodes based on default label type */
724e8fb11a1Sshidokht cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
725e8fb11a1Sshidokht cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
7263ccda647Slclee
727e8fb11a1Sshidokht if (cmlb_create_minor_nodes(cl) != 0) {
728e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
7293ccda647Slclee return (ENXIO);
7303ccda647Slclee }
7313ccda647Slclee
732b9ccdc5aScth /* Define the dynamic properties for devinfo spapshots. */
733b9ccdc5aScth i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
734b9ccdc5aScth
735e8fb11a1Sshidokht cl->cl_state = CMLB_ATTACHED;
7363ccda647Slclee
737e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
7383ccda647Slclee return (0);
7393ccda647Slclee }
7403ccda647Slclee
7413ccda647Slclee /*
7423ccda647Slclee * cmlb_detach:
7433ccda647Slclee *
7443ccda647Slclee * Invalidate in-core labeling data and remove all minor nodes for
7453ccda647Slclee * the device associate with handle.
7463ccda647Slclee *
7473ccda647Slclee * Arguments:
7483ccda647Slclee * cmlbhandle cmlb handle associated with device.
7493ccda647Slclee *
750e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
751e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
752e8fb11a1Sshidokht *
7533ccda647Slclee */
754e8fb11a1Sshidokht /*ARGSUSED1*/
7553ccda647Slclee void
cmlb_detach(cmlb_handle_t cmlbhandle,void * tg_cookie)756e8fb11a1Sshidokht cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
7573ccda647Slclee {
758e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
7593ccda647Slclee
760e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
761e8fb11a1Sshidokht cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
7627f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
763e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
764b9ccdc5aScth i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
765e8fb11a1Sshidokht cl->cl_state = CMLB_INITED;
766e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
7673ccda647Slclee }
7683ccda647Slclee
7693ccda647Slclee /*
7703ccda647Slclee * cmlb_validate:
7713ccda647Slclee *
7723ccda647Slclee * Validates label.
7733ccda647Slclee *
7743ccda647Slclee * Arguments
7753ccda647Slclee * cmlbhandle cmlb handle associated with device.
7763ccda647Slclee *
777e8fb11a1Sshidokht * flags operation flags. used for verbosity control
778e8fb11a1Sshidokht *
779e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
780e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
781e8fb11a1Sshidokht *
782e8fb11a1Sshidokht *
7833ccda647Slclee * Notes:
7843ccda647Slclee * If new label type is different from the current, adjust minor nodes
7853ccda647Slclee * accordingly.
7863ccda647Slclee *
7873ccda647Slclee * Return values:
7883ccda647Slclee * 0 success
7893ccda647Slclee * Note: having fdisk but no solaris partition is assumed
7903ccda647Slclee * success.
7913ccda647Slclee *
7923ccda647Slclee * ENOMEM memory allocation failed
7933ccda647Slclee * EIO i/o errors during read or get capacity
7943ccda647Slclee * EACCESS reservation conflicts
7953ccda647Slclee * EINVAL label was corrupt, or no default label was assumed
7963ccda647Slclee * ENXIO invalid handle
7973ccda647Slclee */
7983ccda647Slclee int
cmlb_validate(cmlb_handle_t cmlbhandle,int flags,void * tg_cookie)799e8fb11a1Sshidokht cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
8003ccda647Slclee {
801e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8023ccda647Slclee int rval;
8033ccda647Slclee int ret = 0;
8043ccda647Slclee
8053ccda647Slclee /*
806e8fb11a1Sshidokht * Temp work-around checking cl for NULL since there is a bug
8073ccda647Slclee * in sd_detach calling this routine from taskq_dispatch
8083ccda647Slclee * inited function.
8093ccda647Slclee */
810e8fb11a1Sshidokht if (cl == NULL)
8113ccda647Slclee return (ENXIO);
8123ccda647Slclee
813e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
814e8fb11a1Sshidokht if (cl->cl_state < CMLB_ATTACHED) {
815e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
8163ccda647Slclee return (ENXIO);
8173ccda647Slclee }
8183ccda647Slclee
8197f0b8309SEdward Pilatowicz rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
820e8fb11a1Sshidokht flags, tg_cookie);
8213ccda647Slclee
8223ccda647Slclee if (rval == ENOTSUP) {
8237f0b8309SEdward Pilatowicz if (cl->cl_f_geometry_is_valid) {
824e8fb11a1Sshidokht cl->cl_cur_labeltype = CMLB_LABEL_EFI;
8253ccda647Slclee ret = 0;
8263ccda647Slclee } else {
8273ccda647Slclee ret = EINVAL;
8283ccda647Slclee }
8293ccda647Slclee } else {
8303ccda647Slclee ret = rval;
8313ccda647Slclee if (ret == 0)
832e8fb11a1Sshidokht cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
8333ccda647Slclee }
8343ccda647Slclee
8353ccda647Slclee if (ret == 0)
836e8fb11a1Sshidokht (void) cmlb_create_minor_nodes(cl);
8373ccda647Slclee
838e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
8393ccda647Slclee return (ret);
8403ccda647Slclee }
8413ccda647Slclee
8423ccda647Slclee /*
8433ccda647Slclee * cmlb_invalidate:
8443ccda647Slclee * Invalidate in core label data
8453ccda647Slclee *
8463ccda647Slclee * Arguments:
8473ccda647Slclee * cmlbhandle cmlb handle associated with device.
848e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
849e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
8503ccda647Slclee */
851e8fb11a1Sshidokht /*ARGSUSED1*/
8523ccda647Slclee void
cmlb_invalidate(cmlb_handle_t cmlbhandle,void * tg_cookie)853e8fb11a1Sshidokht cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
8543ccda647Slclee {
855e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
8563ccda647Slclee
857e8fb11a1Sshidokht if (cl == NULL)
8583ccda647Slclee return;
8593ccda647Slclee
860e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
8617f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
862e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
8633ccda647Slclee }
8643ccda647Slclee
8653ccda647Slclee /*
866e8fb11a1Sshidokht * cmlb_is_valid
867e8fb11a1Sshidokht * Get status on whether the incore label/geom data is valid
868e8fb11a1Sshidokht *
869e8fb11a1Sshidokht * Arguments:
870e8fb11a1Sshidokht * cmlbhandle cmlb handle associated with device.
871e8fb11a1Sshidokht *
872e8fb11a1Sshidokht * Return values:
8737f0b8309SEdward Pilatowicz * B_TRUE if incore label/geom data is valid.
8747f0b8309SEdward Pilatowicz * B_FALSE otherwise.
875e8fb11a1Sshidokht *
876e8fb11a1Sshidokht */
877e8fb11a1Sshidokht
878e8fb11a1Sshidokht
8797f0b8309SEdward Pilatowicz boolean_t
cmlb_is_valid(cmlb_handle_t cmlbhandle)880e8fb11a1Sshidokht cmlb_is_valid(cmlb_handle_t cmlbhandle)
881e8fb11a1Sshidokht {
882e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
883e8fb11a1Sshidokht
884e8fb11a1Sshidokht if (cmlbhandle == NULL)
8857f0b8309SEdward Pilatowicz return (B_FALSE);
886e8fb11a1Sshidokht
887e8fb11a1Sshidokht return (cl->cl_f_geometry_is_valid);
888e8fb11a1Sshidokht
889e8fb11a1Sshidokht }
890e8fb11a1Sshidokht
891e8fb11a1Sshidokht
892e8fb11a1Sshidokht
893e8fb11a1Sshidokht /*
8943ccda647Slclee * cmlb_close:
8953ccda647Slclee *
8963ccda647Slclee * Close the device, revert to a default label minor node for the device,
8973ccda647Slclee * if it is removable.
8983ccda647Slclee *
8993ccda647Slclee * Arguments:
9003ccda647Slclee * cmlbhandle cmlb handle associated with device.
9013ccda647Slclee *
902e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
903e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
9043ccda647Slclee * Return values:
9053ccda647Slclee * 0 Success
9063ccda647Slclee * ENXIO Re-creating minor node failed.
9073ccda647Slclee */
908e8fb11a1Sshidokht /*ARGSUSED1*/
9093ccda647Slclee int
cmlb_close(cmlb_handle_t cmlbhandle,void * tg_cookie)910e8fb11a1Sshidokht cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
9113ccda647Slclee {
912e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
9133ccda647Slclee
914e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
9157f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
9163ccda647Slclee
9173ccda647Slclee /* revert to default minor node for this device */
918e8fb11a1Sshidokht if (ISREMOVABLE(cl)) {
919e8fb11a1Sshidokht cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
920e8fb11a1Sshidokht (void) cmlb_create_minor_nodes(cl);
9213ccda647Slclee }
9223ccda647Slclee
923e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
9243ccda647Slclee return (0);
9253ccda647Slclee }
9263ccda647Slclee
9273ccda647Slclee /*
9283ccda647Slclee * cmlb_get_devid_block:
9293ccda647Slclee * get the block number where device id is stored.
9303ccda647Slclee *
9313ccda647Slclee * Arguments:
9323ccda647Slclee * cmlbhandle cmlb handle associated with device.
9333ccda647Slclee * devidblockp pointer to block number.
934e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
935e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
9363ccda647Slclee *
9373ccda647Slclee * Notes:
9383ccda647Slclee * It stores the block number of device id in the area pointed to
9393ccda647Slclee * by devidblockp.
9403ccda647Slclee * with the block number of device id.
9413ccda647Slclee *
9423ccda647Slclee * Return values:
9433ccda647Slclee * 0 success
9443ccda647Slclee * EINVAL device id does not apply to current label type.
9453ccda647Slclee */
946e8fb11a1Sshidokht /*ARGSUSED2*/
9473ccda647Slclee int
cmlb_get_devid_block(cmlb_handle_t cmlbhandle,diskaddr_t * devidblockp,void * tg_cookie)948e8fb11a1Sshidokht cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
949e8fb11a1Sshidokht void *tg_cookie)
9503ccda647Slclee {
9513ccda647Slclee daddr_t spc, blk, head, cyl;
952e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
9533ccda647Slclee
954e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
955e8fb11a1Sshidokht if (cl->cl_state < CMLB_ATTACHED) {
956e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
9573ccda647Slclee return (EINVAL);
9583ccda647Slclee }
9593ccda647Slclee
9607f0b8309SEdward Pilatowicz if ((!cl->cl_f_geometry_is_valid) ||
961e8fb11a1Sshidokht (cl->cl_solaris_size < DK_LABEL_LOC)) {
962e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
963e8fb11a1Sshidokht return (EINVAL);
964e8fb11a1Sshidokht }
965e8fb11a1Sshidokht
966e8fb11a1Sshidokht if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
967e8fb11a1Sshidokht if (cl->cl_reserved != -1) {
968e8fb11a1Sshidokht blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
969e8fb11a1Sshidokht } else {
970e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
971e8fb11a1Sshidokht return (EINVAL);
972e8fb11a1Sshidokht }
973e8fb11a1Sshidokht } else {
9745d22422eSml40262 /* if the disk is unlabeled, don't write a devid to it */
975342440ecSPrasad Singamsetty if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
9765d22422eSml40262 mutex_exit(CMLB_MUTEX(cl));
9775d22422eSml40262 return (EINVAL);
9785d22422eSml40262 }
9795d22422eSml40262
9803ccda647Slclee /* this geometry doesn't allow us to write a devid */
981e8fb11a1Sshidokht if (cl->cl_g.dkg_acyl < 2) {
982e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
9833ccda647Slclee return (EINVAL);
9843ccda647Slclee }
9853ccda647Slclee
9863ccda647Slclee /*
9873ccda647Slclee * Subtract 2 guarantees that the next to last cylinder
9883ccda647Slclee * is used
9893ccda647Slclee */
990e8fb11a1Sshidokht cyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl - 2;
991e8fb11a1Sshidokht spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
992e8fb11a1Sshidokht head = cl->cl_g.dkg_nhead - 1;
9935d22422eSml40262 blk = cl->cl_solaris_offset +
9945d22422eSml40262 (cyl * (spc - cl->cl_g.dkg_apc)) +
995e8fb11a1Sshidokht (head * cl->cl_g.dkg_nsect) + 1;
9963ccda647Slclee }
997e8fb11a1Sshidokht
9983ccda647Slclee *devidblockp = blk;
999e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
10003ccda647Slclee return (0);
10013ccda647Slclee }
10023ccda647Slclee
10033ccda647Slclee /*
10043ccda647Slclee * cmlb_partinfo:
10053ccda647Slclee * Get partition info for specified partition number.
10063ccda647Slclee *
10073ccda647Slclee * Arguments:
10083ccda647Slclee * cmlbhandle cmlb handle associated with device.
10093ccda647Slclee * part partition number
10103ccda647Slclee * nblocksp pointer to number of blocks
10113ccda647Slclee * startblockp pointer to starting block
10123ccda647Slclee * partnamep pointer to name of partition
10133ccda647Slclee * tagp pointer to tag info
1014e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
1015e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
10163ccda647Slclee *
10173ccda647Slclee *
10183ccda647Slclee * Notes:
10193ccda647Slclee * If in-core label is not valid, this functions tries to revalidate
10203ccda647Slclee * the label. If label is valid, it stores the total number of blocks
10213ccda647Slclee * in this partition in the area pointed to by nblocksp, starting
10223ccda647Slclee * block number in area pointed to by startblockp, pointer to partition
10233ccda647Slclee * name in area pointed to by partnamep, and tag value in area
10243ccda647Slclee * pointed by tagp.
10253ccda647Slclee * For EFI labels, tag value will be set to 0.
10263ccda647Slclee *
10273ccda647Slclee * For all nblocksp, startblockp and partnamep, tagp, a value of NULL
10283ccda647Slclee * indicates the corresponding info is not requested.
10293ccda647Slclee *
10303ccda647Slclee *
10313ccda647Slclee * Return values:
10323ccda647Slclee * 0 success
10333ccda647Slclee * EINVAL no valid label or requested partition number is invalid.
10343ccda647Slclee *
10353ccda647Slclee */
10363ccda647Slclee int
cmlb_partinfo(cmlb_handle_t cmlbhandle,int part,diskaddr_t * nblocksp,diskaddr_t * startblockp,char ** partnamep,uint16_t * tagp,void * tg_cookie)10373ccda647Slclee cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
1038e8fb11a1Sshidokht diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
10393ccda647Slclee {
10403ccda647Slclee
1041e8fb11a1Sshidokht struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
10423ccda647Slclee int rval;
1043aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
1044aa1b14e7SSheshadri Vasudevan int ext_part;
1045aa1b14e7SSheshadri Vasudevan #endif
10463ccda647Slclee
1047e8fb11a1Sshidokht ASSERT(cl != NULL);
1048e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
1049e8fb11a1Sshidokht if (cl->cl_state < CMLB_ATTACHED) {
1050e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
10513ccda647Slclee return (EINVAL);
10523ccda647Slclee }
10533ccda647Slclee
10543ccda647Slclee if (part < 0 || part >= MAXPART) {
10553ccda647Slclee rval = EINVAL;
10563ccda647Slclee } else {
10577f0b8309SEdward Pilatowicz if (!cl->cl_f_geometry_is_valid)
10587f0b8309SEdward Pilatowicz (void) cmlb_validate_geometry((struct cmlb_lun *)cl,
10597f0b8309SEdward Pilatowicz B_FALSE, 0, tg_cookie);
1060e8fb11a1Sshidokht
1061342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_16)
10627f0b8309SEdward Pilatowicz if (((!cl->cl_f_geometry_is_valid) ||
1063342440ecSPrasad Singamsetty (part < NDKMAP && cl->cl_solaris_size == 0)) &&
1064342440ecSPrasad Singamsetty (part != P0_RAW_DISK)) {
1065342440ecSPrasad Singamsetty #else
10667f0b8309SEdward Pilatowicz if ((!cl->cl_f_geometry_is_valid) ||
1067e8fb11a1Sshidokht (part < NDKMAP && cl->cl_solaris_size == 0)) {
1068342440ecSPrasad Singamsetty #endif
10693ccda647Slclee rval = EINVAL;
10703ccda647Slclee } else {
10713ccda647Slclee if (startblockp != NULL)
1072e8fb11a1Sshidokht *startblockp = (diskaddr_t)cl->cl_offset[part];
10733ccda647Slclee
10743ccda647Slclee if (nblocksp != NULL)
10753ccda647Slclee *nblocksp = (diskaddr_t)
1076e8fb11a1Sshidokht cl->cl_map[part].dkl_nblk;
10773ccda647Slclee
10783ccda647Slclee if (tagp != NULL)
1079c31fac72SShidokht Yadegari *tagp =
1080c31fac72SShidokht Yadegari ((cl->cl_cur_labeltype == CMLB_LABEL_EFI) ||
1081c31fac72SShidokht Yadegari (part >= NDKMAP)) ? V_UNASSIGNED :
1082c31fac72SShidokht Yadegari cl->cl_vtoc.v_part[part].p_tag;
10833ccda647Slclee rval = 0;
10843ccda647Slclee }
10853ccda647Slclee
10863ccda647Slclee /* consistent with behavior of sd for getting minor name */
1087aa1b14e7SSheshadri Vasudevan if (partnamep != NULL) {
1088aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
1089aa1b14e7SSheshadri Vasudevan #if defined(_FIRMWARE_NEEDS_FDISK)
1090aa1b14e7SSheshadri Vasudevan if (part > FDISK_P4) {
1091aa1b14e7SSheshadri Vasudevan ext_part = part-FDISK_P4-1;
1092aa1b14e7SSheshadri Vasudevan *partnamep = dk_ext_minor_data[ext_part].name;
1093aa1b14e7SSheshadri Vasudevan } else
1094aa1b14e7SSheshadri Vasudevan #endif
1095aa1b14e7SSheshadri Vasudevan #endif
10963ccda647Slclee *partnamep = dk_minor_data[part].name;
1097aa1b14e7SSheshadri Vasudevan }
10983ccda647Slclee
10993ccda647Slclee }
11003ccda647Slclee
1101e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
11023ccda647Slclee return (rval);
11033ccda647Slclee }
11043ccda647Slclee
1105af007057Syl194034 /*
1106af007057Syl194034 * cmlb_efi_label_capacity:
1107af007057Syl194034 * Get capacity stored in EFI disk label.
1108af007057Syl194034 *
1109af007057Syl194034 * Arguments:
1110af007057Syl194034 * cmlbhandle cmlb handle associated with device.
1111af007057Syl194034 * capacity pointer to capacity stored in EFI disk label.
1112af007057Syl194034 * tg_cookie cookie from target driver to be passed back to target
1113af007057Syl194034 * driver when we call back to it through tg_ops.
1114af007057Syl194034 *
1115af007057Syl194034 *
1116af007057Syl194034 * Notes:
1117af007057Syl194034 * If in-core label is not valid, this functions tries to revalidate
1118af007057Syl194034 * the label. If label is valid and is an EFI label, it stores the capacity
1119af007057Syl194034 * in disk label in the area pointed to by capacity.
1120af007057Syl194034 *
1121af007057Syl194034 *
1122af007057Syl194034 * Return values:
1123af007057Syl194034 * 0 success
1124af007057Syl194034 * EINVAL no valid EFI label or capacity is NULL.
1125af007057Syl194034 *
1126af007057Syl194034 */
1127af007057Syl194034 int
1128af007057Syl194034 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1129af007057Syl194034 void *tg_cookie)
1130af007057Syl194034 {
1131af007057Syl194034 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1132af007057Syl194034 int rval;
1133af007057Syl194034
1134af007057Syl194034 ASSERT(cl != NULL);
1135af007057Syl194034 mutex_enter(CMLB_MUTEX(cl));
1136af007057Syl194034 if (cl->cl_state < CMLB_ATTACHED) {
1137af007057Syl194034 mutex_exit(CMLB_MUTEX(cl));
1138af007057Syl194034 return (EINVAL);
1139af007057Syl194034 }
1140af007057Syl194034
11417f0b8309SEdward Pilatowicz if (!cl->cl_f_geometry_is_valid)
11427f0b8309SEdward Pilatowicz (void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1143af007057Syl194034 0, tg_cookie);
1144af007057Syl194034
11457f0b8309SEdward Pilatowicz if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1146af007057Syl194034 (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1147af007057Syl194034 rval = EINVAL;
1148af007057Syl194034 } else {
1149af007057Syl194034 *capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1150af007057Syl194034 rval = 0;
1151af007057Syl194034 }
1152af007057Syl194034
1153af007057Syl194034 mutex_exit(CMLB_MUTEX(cl));
1154af007057Syl194034 return (rval);
1155af007057Syl194034 }
1156af007057Syl194034
1157e8fb11a1Sshidokht /* Caller should make sure Test Unit Ready succeeds before calling this. */
11583ccda647Slclee /*ARGSUSED*/
11593ccda647Slclee int
11603ccda647Slclee cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1161e8fb11a1Sshidokht int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
11623ccda647Slclee {
11633ccda647Slclee
11643ccda647Slclee int err;
1165e8fb11a1Sshidokht struct cmlb_lun *cl;
11663ccda647Slclee
1167e8fb11a1Sshidokht cl = (struct cmlb_lun *)cmlbhandle;
11683ccda647Slclee
1169e8fb11a1Sshidokht ASSERT(cl != NULL);
11703ccda647Slclee
1171e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
1172e8fb11a1Sshidokht if (cl->cl_state < CMLB_ATTACHED) {
1173e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
11743ccda647Slclee return (EIO);
11753ccda647Slclee }
11763ccda647Slclee
11773ccda647Slclee switch (cmd) {
1178342440ecSPrasad Singamsetty case DKIOCSEXTVTOC:
11793ccda647Slclee case DKIOCSGEOM:
11803ccda647Slclee case DKIOCSETEFI:
11813ccda647Slclee case DKIOCSMBOOT:
1182aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
1183aa1b14e7SSheshadri Vasudevan case DKIOCSETEXTPART:
1184aa1b14e7SSheshadri Vasudevan #endif
11853ccda647Slclee break;
1186342440ecSPrasad Singamsetty case DKIOCSVTOC:
1187342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
1188342440ecSPrasad Singamsetty case DKIOCPARTINFO:
1189342440ecSPrasad Singamsetty #endif
1190342440ecSPrasad Singamsetty if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1191342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
1192342440ecSPrasad Singamsetty return (EOVERFLOW);
1193342440ecSPrasad Singamsetty }
1194342440ecSPrasad Singamsetty break;
11953ccda647Slclee default:
1196342440ecSPrasad Singamsetty (void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1197adc076afSshidokht tg_cookie);
1198e8fb11a1Sshidokht
1199e8fb11a1Sshidokht switch (cmd) {
1200e8fb11a1Sshidokht case DKIOCGVTOC:
1201342440ecSPrasad Singamsetty case DKIOCGAPART:
1202e8fb11a1Sshidokht case DKIOCSAPART:
1203e8fb11a1Sshidokht
1204342440ecSPrasad Singamsetty if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1205342440ecSPrasad Singamsetty /* GPT label on disk */
1206342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
1207342440ecSPrasad Singamsetty return (ENOTSUP);
1208342440ecSPrasad Singamsetty } else if
1209342440ecSPrasad Singamsetty (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1210342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
1211342440ecSPrasad Singamsetty return (EOVERFLOW);
1212342440ecSPrasad Singamsetty }
1213342440ecSPrasad Singamsetty break;
1214342440ecSPrasad Singamsetty
1215342440ecSPrasad Singamsetty case DKIOCGGEOM:
1216342440ecSPrasad Singamsetty if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1217342440ecSPrasad Singamsetty /* GPT label on disk */
1218e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
1219e8fb11a1Sshidokht return (ENOTSUP);
1220e8fb11a1Sshidokht }
1221342440ecSPrasad Singamsetty break;
1222342440ecSPrasad Singamsetty default:
1223342440ecSPrasad Singamsetty break;
12243ccda647Slclee }
1225e8fb11a1Sshidokht }
12263ccda647Slclee
1227e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
12283ccda647Slclee
12293ccda647Slclee switch (cmd) {
12303ccda647Slclee case DKIOCGGEOM:
1231e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1232e8fb11a1Sshidokht err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
12333ccda647Slclee break;
12343ccda647Slclee
12353ccda647Slclee case DKIOCSGEOM:
1236e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1237e8fb11a1Sshidokht err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
12383ccda647Slclee break;
12393ccda647Slclee
12403ccda647Slclee case DKIOCGAPART:
1241e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1242e8fb11a1Sshidokht err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1243e8fb11a1Sshidokht flag, tg_cookie);
12443ccda647Slclee break;
12453ccda647Slclee
12463ccda647Slclee case DKIOCSAPART:
1247e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1248e8fb11a1Sshidokht err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
12493ccda647Slclee break;
12503ccda647Slclee
12513ccda647Slclee case DKIOCGVTOC:
1252e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1253e8fb11a1Sshidokht err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
12543ccda647Slclee break;
12553ccda647Slclee
1256342440ecSPrasad Singamsetty case DKIOCGEXTVTOC:
1257342440ecSPrasad Singamsetty cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1258342440ecSPrasad Singamsetty err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1259342440ecSPrasad Singamsetty break;
1260342440ecSPrasad Singamsetty
12613ccda647Slclee case DKIOCGETEFI:
1262e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1263e8fb11a1Sshidokht err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
12643ccda647Slclee break;
12653ccda647Slclee
12663ccda647Slclee case DKIOCPARTITION:
1267e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1268e8fb11a1Sshidokht err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
12693ccda647Slclee break;
12703ccda647Slclee
12713ccda647Slclee case DKIOCSVTOC:
1272e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1273e8fb11a1Sshidokht err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1274e8fb11a1Sshidokht tg_cookie);
12753ccda647Slclee break;
12763ccda647Slclee
1277342440ecSPrasad Singamsetty case DKIOCSEXTVTOC:
1278342440ecSPrasad Singamsetty cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1279342440ecSPrasad Singamsetty err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1280342440ecSPrasad Singamsetty tg_cookie);
1281342440ecSPrasad Singamsetty break;
1282342440ecSPrasad Singamsetty
12833ccda647Slclee case DKIOCSETEFI:
1284e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1285e8fb11a1Sshidokht err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
12863ccda647Slclee break;
12873ccda647Slclee
12883ccda647Slclee case DKIOCGMBOOT:
1289e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1290e8fb11a1Sshidokht err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
12913ccda647Slclee break;
12923ccda647Slclee
12933ccda647Slclee case DKIOCSMBOOT:
1294e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1295e8fb11a1Sshidokht err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
12963ccda647Slclee break;
12973ccda647Slclee case DKIOCG_PHYGEOM:
1298e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
12993ccda647Slclee #if defined(__i386) || defined(__amd64)
1300b081f1c4Syu, larry liu - Sun Microsystems - Beijing China err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag, tg_cookie);
13013ccda647Slclee #else
13023ccda647Slclee err = ENOTTY;
13033ccda647Slclee #endif
13043ccda647Slclee break;
13053ccda647Slclee case DKIOCG_VIRTGEOM:
1306e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
13073ccda647Slclee #if defined(__i386) || defined(__amd64)
1308e8fb11a1Sshidokht err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
13093ccda647Slclee #else
13103ccda647Slclee err = ENOTTY;
13113ccda647Slclee #endif
13123ccda647Slclee break;
13133ccda647Slclee case DKIOCPARTINFO:
1314e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
13153ccda647Slclee #if defined(__i386) || defined(__amd64)
1316e8fb11a1Sshidokht err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
13173ccda647Slclee #else
13183ccda647Slclee err = ENOTTY;
13193ccda647Slclee #endif
13203ccda647Slclee break;
1321342440ecSPrasad Singamsetty case DKIOCEXTPARTINFO:
1322342440ecSPrasad Singamsetty cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1323342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
1324342440ecSPrasad Singamsetty err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1325342440ecSPrasad Singamsetty #else
1326342440ecSPrasad Singamsetty err = ENOTTY;
1327342440ecSPrasad Singamsetty #endif
1328342440ecSPrasad Singamsetty break;
1329aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
1330aa1b14e7SSheshadri Vasudevan case DKIOCSETEXTPART:
1331aa1b14e7SSheshadri Vasudevan cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEXTPART");
1332aa1b14e7SSheshadri Vasudevan err = cmlb_dkio_set_ext_part(cl, (caddr_t)arg, flag, tg_cookie);
1333aa1b14e7SSheshadri Vasudevan break;
1334aa1b14e7SSheshadri Vasudevan #endif
13353ccda647Slclee default:
13363ccda647Slclee err = ENOTTY;
13373ccda647Slclee
13383ccda647Slclee }
1339b9ccdc5aScth
1340b9ccdc5aScth /*
1341b9ccdc5aScth * An ioctl that succeeds and changed ('set') size(9P) information
1342b9ccdc5aScth * needs to invalidate the cached devinfo snapshot to avoid having
1343b9ccdc5aScth * old information being returned in a snapshots.
1344b9ccdc5aScth *
1345b9ccdc5aScth * NB: When available, call ddi_change_minor_node() to clear
1346b9ccdc5aScth * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1347b9ccdc5aScth */
1348b9ccdc5aScth if (err == 0) {
1349b9ccdc5aScth switch (cmd) {
1350b9ccdc5aScth case DKIOCSGEOM:
1351b9ccdc5aScth case DKIOCSAPART:
1352b9ccdc5aScth case DKIOCSVTOC:
1353342440ecSPrasad Singamsetty case DKIOCSEXTVTOC:
1354b9ccdc5aScth case DKIOCSETEFI:
1355b9ccdc5aScth i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1356b9ccdc5aScth i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1357b9ccdc5aScth }
1358b9ccdc5aScth }
13593ccda647Slclee return (err);
13603ccda647Slclee }
13613ccda647Slclee
13623ccda647Slclee dev_t
1363e8fb11a1Sshidokht cmlb_make_device(struct cmlb_lun *cl)
13643ccda647Slclee {
13655c066ec2SJerry Gilliam return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1366e8fb11a1Sshidokht ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
13673ccda647Slclee }
13683ccda647Slclee
13693ccda647Slclee /*
13703ccda647Slclee * Function: cmlb_check_update_blockcount
13713ccda647Slclee *
13723ccda647Slclee * Description: If current capacity value is invalid, obtains the
13733ccda647Slclee * current capacity from target driver.
13743ccda647Slclee *
13753ccda647Slclee * Return Code: 0 success
13763ccda647Slclee * EIO failure
13773ccda647Slclee */
13783ccda647Slclee static int
1379e8fb11a1Sshidokht cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
13803ccda647Slclee {
13813ccda647Slclee int status;
13823ccda647Slclee diskaddr_t capacity;
1383e8fb11a1Sshidokht uint32_t lbasize;
13843ccda647Slclee
1385e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
13863ccda647Slclee
13877f0b8309SEdward Pilatowicz if (cl->cl_f_geometry_is_valid)
13887f0b8309SEdward Pilatowicz return (0);
13897f0b8309SEdward Pilatowicz
1390e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
1391e8fb11a1Sshidokht status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1392e8fb11a1Sshidokht if (status != 0) {
1393e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
1394e8fb11a1Sshidokht return (EIO);
1395e8fb11a1Sshidokht }
1396e8fb11a1Sshidokht
1397e8fb11a1Sshidokht status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1398e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
1399e8fb11a1Sshidokht if (status != 0)
1400e8fb11a1Sshidokht return (EIO);
1401e8fb11a1Sshidokht
1402e8fb11a1Sshidokht if ((capacity != 0) && (lbasize != 0)) {
1403e8fb11a1Sshidokht cl->cl_blockcount = capacity;
1404e8fb11a1Sshidokht cl->cl_tgt_blocksize = lbasize;
140565908c77Syu, larry liu - Sun Microsystems - Beijing China if (!cl->cl_is_removable) {
140665908c77Syu, larry liu - Sun Microsystems - Beijing China cl->cl_sys_blocksize = lbasize;
140765908c77Syu, larry liu - Sun Microsystems - Beijing China }
14083ccda647Slclee return (0);
14097f0b8309SEdward Pilatowicz } else {
14103ccda647Slclee return (EIO);
14117f0b8309SEdward Pilatowicz }
14123ccda647Slclee }
14133ccda647Slclee
141406bbe1e0Sedp static int
141506bbe1e0Sedp cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
141606bbe1e0Sedp minor_t minor_num, char *node_type, int flag, boolean_t internal)
141706bbe1e0Sedp {
14187f0b8309SEdward Pilatowicz ASSERT(VALID_BOOLEAN(internal));
14197f0b8309SEdward Pilatowicz
142006bbe1e0Sedp if (internal)
142106bbe1e0Sedp return (ddi_create_internal_pathname(dip,
142206bbe1e0Sedp name, spec_type, minor_num));
142306bbe1e0Sedp else
142406bbe1e0Sedp return (ddi_create_minor_node(dip,
142506bbe1e0Sedp name, spec_type, minor_num, node_type, flag));
142606bbe1e0Sedp }
142706bbe1e0Sedp
14283ccda647Slclee /*
14293ccda647Slclee * Function: cmlb_create_minor_nodes
14303ccda647Slclee *
14313ccda647Slclee * Description: Create or adjust the minor device nodes for the instance.
14323ccda647Slclee * Minor nodes are created based on default label type,
14333ccda647Slclee * current label type and last label type we created
14343ccda647Slclee * minor nodes based on.
14353ccda647Slclee *
14363ccda647Slclee *
1437e8fb11a1Sshidokht * Arguments: cl - driver soft state (unit) structure
14383ccda647Slclee *
14393ccda647Slclee * Return Code: 0 success
14403ccda647Slclee * ENXIO failure.
14413ccda647Slclee *
14423ccda647Slclee * Context: Kernel thread context
14433ccda647Slclee */
14443ccda647Slclee static int
1445e8fb11a1Sshidokht cmlb_create_minor_nodes(struct cmlb_lun *cl)
14463ccda647Slclee {
14473ccda647Slclee struct driver_minor_data *dmdp;
14483ccda647Slclee int instance;
14493ccda647Slclee char name[48];
14503ccda647Slclee cmlb_label_t newlabeltype;
145106bbe1e0Sedp boolean_t internal;
14523ccda647Slclee
1453e8fb11a1Sshidokht ASSERT(cl != NULL);
1454e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
14553ccda647Slclee
14567f0b8309SEdward Pilatowicz internal = VOID2BOOLEAN(
14577f0b8309SEdward Pilatowicz (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
14583ccda647Slclee
14593ccda647Slclee /* check the most common case */
1460e8fb11a1Sshidokht if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1461e8fb11a1Sshidokht cl->cl_last_labeltype == cl->cl_cur_labeltype) {
14623ccda647Slclee /* do nothing */
14633ccda647Slclee return (0);
14643ccda647Slclee }
14653ccda647Slclee
1466e8fb11a1Sshidokht if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
14673ccda647Slclee /* we should never get here */
14683ccda647Slclee return (ENXIO);
14693ccda647Slclee }
14703ccda647Slclee
1471e8fb11a1Sshidokht if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
14723ccda647Slclee /* first time during attach */
1473e8fb11a1Sshidokht newlabeltype = cl->cl_def_labeltype;
14743ccda647Slclee
1475e8fb11a1Sshidokht instance = ddi_get_instance(CMLB_DEVINFO(cl));
14763ccda647Slclee
14773ccda647Slclee /* Create all the minor nodes for this target. */
14783ccda647Slclee dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
14793ccda647Slclee dk_minor_data;
14803ccda647Slclee while (dmdp->name != NULL) {
14813ccda647Slclee
14823ccda647Slclee (void) sprintf(name, "%s", dmdp->name);
14833ccda647Slclee
148406bbe1e0Sedp if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
14853ccda647Slclee dmdp->type,
14863ccda647Slclee (instance << CMLBUNIT_SHIFT) | dmdp->minor,
148706bbe1e0Sedp cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
14883ccda647Slclee /*
14893ccda647Slclee * Clean up any nodes that may have been
14903ccda647Slclee * created, in case this fails in the middle
14913ccda647Slclee * of the loop.
14923ccda647Slclee */
1493e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
14943ccda647Slclee return (ENXIO);
14953ccda647Slclee }
14963ccda647Slclee dmdp++;
14973ccda647Slclee }
1498e8fb11a1Sshidokht cl->cl_last_labeltype = newlabeltype;
14993ccda647Slclee return (0);
15003ccda647Slclee }
15013ccda647Slclee
15023ccda647Slclee /* Not first time */
1503e8fb11a1Sshidokht if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1504e8fb11a1Sshidokht if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
15053ccda647Slclee /* close time, revert to default. */
1506e8fb11a1Sshidokht newlabeltype = cl->cl_def_labeltype;
15073ccda647Slclee } else {
15083ccda647Slclee /*
15093ccda647Slclee * do nothing since the type for which we last created
15103ccda647Slclee * nodes matches the default
15113ccda647Slclee */
15123ccda647Slclee return (0);
15133ccda647Slclee }
15143ccda647Slclee } else {
1515e8fb11a1Sshidokht if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
15163ccda647Slclee /* We are not closing, use current label type */
1517e8fb11a1Sshidokht newlabeltype = cl->cl_cur_labeltype;
15183ccda647Slclee } else {
15193ccda647Slclee /*
15203ccda647Slclee * do nothing since the type for which we last created
15213ccda647Slclee * nodes matches the current label type
15223ccda647Slclee */
15233ccda647Slclee return (0);
15243ccda647Slclee }
15253ccda647Slclee }
15263ccda647Slclee
1527e8fb11a1Sshidokht instance = ddi_get_instance(CMLB_DEVINFO(cl));
15283ccda647Slclee
15293ccda647Slclee /*
15303ccda647Slclee * Currently we only fix up the s7 node when we are switching
15313ccda647Slclee * label types from or to EFI. This is consistent with
15323ccda647Slclee * current behavior of sd.
15333ccda647Slclee */
15343ccda647Slclee if (newlabeltype == CMLB_LABEL_EFI &&
1535e8fb11a1Sshidokht cl->cl_last_labeltype != CMLB_LABEL_EFI) {
15363ccda647Slclee /* from vtoc to EFI */
1537e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1538e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
153906bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
15403ccda647Slclee S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
154106bbe1e0Sedp cl->cl_node_type, NULL, internal);
154206bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
15433ccda647Slclee S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
154406bbe1e0Sedp cl->cl_node_type, NULL, internal);
15453ccda647Slclee } else {
15463ccda647Slclee /* from efi to vtoc */
1547e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1548e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
154906bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
15503ccda647Slclee S_IFBLK, (instance << CMLBUNIT_SHIFT) | WD_NODE,
155106bbe1e0Sedp cl->cl_node_type, NULL, internal);
155206bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
15533ccda647Slclee S_IFCHR, (instance << CMLBUNIT_SHIFT) | WD_NODE,
155406bbe1e0Sedp cl->cl_node_type, NULL, internal);
15553ccda647Slclee }
15563ccda647Slclee
1557e8fb11a1Sshidokht cl->cl_last_labeltype = newlabeltype;
15583ccda647Slclee return (0);
15593ccda647Slclee }
15603ccda647Slclee
15613ccda647Slclee /*
15623ccda647Slclee * Function: cmlb_validate_geometry
15633ccda647Slclee *
15643ccda647Slclee * Description: Read the label from the disk (if present). Update the unit's
15653ccda647Slclee * geometry and vtoc information from the data in the label.
15663ccda647Slclee * Verify that the label is valid.
15673ccda647Slclee *
1568e8fb11a1Sshidokht * Arguments:
1569e8fb11a1Sshidokht * cl driver soft state (unit) structure
1570e8fb11a1Sshidokht *
1571e8fb11a1Sshidokht * forcerevalid force revalidation even if we are already valid.
1572e8fb11a1Sshidokht * flags operation flags from target driver. Used for verbosity
1573e8fb11a1Sshidokht * control at this time.
1574e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
1575e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
15763ccda647Slclee *
15773ccda647Slclee * Return Code: 0 - Successful completion
1578e8fb11a1Sshidokht * EINVAL - Invalid value in cl->cl_tgt_blocksize or
1579e8fb11a1Sshidokht * cl->cl_blockcount; or label on disk is corrupted
15803ccda647Slclee * or unreadable.
15813ccda647Slclee * EACCES - Reservation conflict at the device.
15823ccda647Slclee * ENOMEM - Resource allocation error
15833ccda647Slclee * ENOTSUP - geometry not applicable
15843ccda647Slclee *
15853ccda647Slclee * Context: Kernel thread only (can sleep).
15863ccda647Slclee */
15873ccda647Slclee static int
15887f0b8309SEdward Pilatowicz cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1589e8fb11a1Sshidokht void *tg_cookie)
15903ccda647Slclee {
15913ccda647Slclee int label_error = 0;
15923ccda647Slclee diskaddr_t capacity;
15933ccda647Slclee int count;
15943ccda647Slclee
1595e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
15967f0b8309SEdward Pilatowicz ASSERT(VALID_BOOLEAN(forcerevalid));
15973ccda647Slclee
15987f0b8309SEdward Pilatowicz if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1599e8fb11a1Sshidokht if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
16003ccda647Slclee return (ENOTSUP);
16013ccda647Slclee return (0);
16023ccda647Slclee }
16033ccda647Slclee
1604e8fb11a1Sshidokht if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
16053ccda647Slclee return (EIO);
16063ccda647Slclee
1607e8fb11a1Sshidokht capacity = cl->cl_blockcount;
16083ccda647Slclee
16093ccda647Slclee #if defined(_SUNOS_VTOC_16)
16103ccda647Slclee /*
16113ccda647Slclee * Set up the "whole disk" fdisk partition; this should always
16123ccda647Slclee * exist, regardless of whether the disk contains an fdisk table
16133ccda647Slclee * or vtoc.
16143ccda647Slclee */
1615e8fb11a1Sshidokht cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1616342440ecSPrasad Singamsetty cl->cl_offset[P0_RAW_DISK] = 0;
16173ccda647Slclee /*
1618342440ecSPrasad Singamsetty * note if capacity > int32_max(1TB) we are in 64bit environment
1619342440ecSPrasad Singamsetty * so no truncation happens
16203ccda647Slclee */
1621e8fb11a1Sshidokht cl->cl_map[P0_RAW_DISK].dkl_nblk = capacity;
16223ccda647Slclee #endif
16233ccda647Slclee /*
16243ccda647Slclee * Refresh the logical and physical geometry caches.
16253ccda647Slclee * (data from MODE SENSE format/rigid disk geometry pages,
16263ccda647Slclee * and scsi_ifgetcap("geometry").
16273ccda647Slclee */
1628e8fb11a1Sshidokht cmlb_resync_geom_caches(cl, capacity, tg_cookie);
16293ccda647Slclee
1630342440ecSPrasad Singamsetty cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1631e8fb11a1Sshidokht label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
16323ccda647Slclee if (label_error == 0) {
16333ccda647Slclee
16343ccda647Slclee /* found a valid EFI label */
1635e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl,
16363ccda647Slclee "cmlb_validate_geometry: found EFI label\n");
16373ccda647Slclee /*
16383ccda647Slclee * solaris_size and geometry_is_valid are set in
16393ccda647Slclee * cmlb_use_efi
16403ccda647Slclee */
16413ccda647Slclee return (ENOTSUP);
16423ccda647Slclee }
16433ccda647Slclee
16443ccda647Slclee /* NO EFI label found */
16453ccda647Slclee
1646342440ecSPrasad Singamsetty if (capacity > CMLB_EXTVTOC_LIMIT) {
16473ccda647Slclee if (label_error == ESRCH) {
16483ccda647Slclee /*
1649342440ecSPrasad Singamsetty * they've configured a LUN over 2TB, but used
16503ccda647Slclee * format.dat to restrict format's view of the
1651342440ecSPrasad Singamsetty * capacity to be under 2TB in some earlier Solaris
1652342440ecSPrasad Singamsetty * release.
16533ccda647Slclee */
1654342440ecSPrasad Singamsetty /* i.e > 2TB with a VTOC < 2TB */
1655342440ecSPrasad Singamsetty if (!(flags & CMLB_SILENT) &&
1656342440ecSPrasad Singamsetty (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
16573ccda647Slclee
1658e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1659342440ecSPrasad Singamsetty CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1660342440ecSPrasad Singamsetty "due to VTOC label. To use the full "
1661342440ecSPrasad Singamsetty "capacity of the disk, use format(1M) to "
1662342440ecSPrasad Singamsetty "relabel the disk with EFI/GPT label.\n",
1663342440ecSPrasad Singamsetty CMLB_LABEL(cl),
1664342440ecSPrasad Singamsetty ddi_get_instance(CMLB_DEVINFO(cl)));
1665342440ecSPrasad Singamsetty
1666342440ecSPrasad Singamsetty cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1667e8fb11a1Sshidokht }
16683ccda647Slclee } else {
16693ccda647Slclee return (ENOTSUP);
16703ccda647Slclee }
16713ccda647Slclee }
16723ccda647Slclee
16733ccda647Slclee label_error = 0;
16743ccda647Slclee
16753ccda647Slclee /*
16763ccda647Slclee * at this point it is either labeled with a VTOC or it is
1677e8fb11a1Sshidokht * under 1TB (<= 1TB actually for off-by-1)
16783ccda647Slclee */
16793ccda647Slclee
16803ccda647Slclee /*
1681e8fb11a1Sshidokht * Only DIRECT ACCESS devices will have Scl labels.
1682e8fb11a1Sshidokht * CD's supposedly have a Scl label, too
16833ccda647Slclee */
1684e8fb11a1Sshidokht if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
16853ccda647Slclee struct dk_label *dkl;
16863ccda647Slclee offset_t label_addr;
16873ccda647Slclee int rval;
16883ccda647Slclee size_t buffer_size;
16893ccda647Slclee
16903ccda647Slclee /*
1691e8fb11a1Sshidokht * Note: This will set up cl->cl_solaris_size and
1692e8fb11a1Sshidokht * cl->cl_solaris_offset.
16933ccda647Slclee */
1694e8fb11a1Sshidokht rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1695e8fb11a1Sshidokht if ((rval != 0) && !ISCD(cl)) {
1696e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
16973ccda647Slclee return (rval);
16983ccda647Slclee }
16993ccda647Slclee
1700e8fb11a1Sshidokht if (cl->cl_solaris_size <= DK_LABEL_LOC) {
17013ccda647Slclee /*
17023ccda647Slclee * Found fdisk table but no Solaris partition entry,
17033ccda647Slclee * so don't call cmlb_uselabel() and don't create
17043ccda647Slclee * a default label.
17053ccda647Slclee */
17063ccda647Slclee label_error = 0;
17077f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
17083ccda647Slclee goto no_solaris_partition;
17093ccda647Slclee }
17103ccda647Slclee
1711e8fb11a1Sshidokht label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1712e8fb11a1Sshidokht
171365908c77Syu, larry liu - Sun Microsystems - Beijing China buffer_size = cl->cl_sys_blocksize;
17143ccda647Slclee
1715e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
17163ccda647Slclee "label_addr: 0x%x allocation size: 0x%x\n",
17173ccda647Slclee label_addr, buffer_size);
17183ccda647Slclee
17193ccda647Slclee if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
17203ccda647Slclee return (ENOMEM);
17213ccda647Slclee
1722e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
1723e8fb11a1Sshidokht rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1724e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
17253ccda647Slclee
17263ccda647Slclee switch (rval) {
17273ccda647Slclee case 0:
17283ccda647Slclee /*
17293ccda647Slclee * cmlb_uselabel will establish that the geometry
17303ccda647Slclee * is valid.
17313ccda647Slclee */
1732e8fb11a1Sshidokht if (cmlb_uselabel(cl,
1733e8fb11a1Sshidokht (struct dk_label *)(uintptr_t)dkl, flags) !=
17343ccda647Slclee CMLB_LABEL_IS_VALID) {
17353ccda647Slclee label_error = EINVAL;
17363ccda647Slclee } else
1737342440ecSPrasad Singamsetty cl->cl_label_from_media = CMLB_LABEL_VTOC;
17383ccda647Slclee break;
17393ccda647Slclee case EACCES:
17403ccda647Slclee label_error = EACCES;
17413ccda647Slclee break;
17423ccda647Slclee default:
17433ccda647Slclee label_error = EINVAL;
17443ccda647Slclee break;
17453ccda647Slclee }
17463ccda647Slclee
17473ccda647Slclee kmem_free(dkl, buffer_size);
17483ccda647Slclee }
17493ccda647Slclee
17503ccda647Slclee /*
17513ccda647Slclee * If a valid label was not found, AND if no reservation conflict
17523ccda647Slclee * was detected, then go ahead and create a default label (4069506).
17533ccda647Slclee *
17543ccda647Slclee * Note: currently, for VTOC_8 devices, the default label is created
1755e8fb11a1Sshidokht * for removables and hotpluggables only. For VTOC_16 devices, the
1756e8fb11a1Sshidokht * default label will be created for all devices.
17573ccda647Slclee * (see cmlb_build_default_label)
17583ccda647Slclee */
17593ccda647Slclee #if defined(_SUNOS_VTOC_8)
1760e8fb11a1Sshidokht if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1761e8fb11a1Sshidokht (label_error != EACCES)) {
17623ccda647Slclee #elif defined(_SUNOS_VTOC_16)
17633ccda647Slclee if (label_error != EACCES) {
17643ccda647Slclee #endif
17657f0b8309SEdward Pilatowicz if (!cl->cl_f_geometry_is_valid) {
1766e8fb11a1Sshidokht cmlb_build_default_label(cl, tg_cookie);
17673ccda647Slclee }
17683ccda647Slclee label_error = 0;
17693ccda647Slclee }
17703ccda647Slclee
17713ccda647Slclee no_solaris_partition:
17723ccda647Slclee
17733ccda647Slclee #if defined(_SUNOS_VTOC_16)
17743ccda647Slclee /*
17753ccda647Slclee * If we have valid geometry, set up the remaining fdisk partitions.
17763ccda647Slclee * Note that dkl_cylno is not used for the fdisk map entries, so
17773ccda647Slclee * we set it to an entirely bogus value.
17783ccda647Slclee */
1779aa1b14e7SSheshadri Vasudevan for (count = 0; count < FDISK_PARTS; count++) {
1780342440ecSPrasad Singamsetty cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1781e8fb11a1Sshidokht cl->cl_map[FDISK_P1 + count].dkl_nblk =
1782e8fb11a1Sshidokht cl->cl_fmap[count].fmap_nblk;
17833ccda647Slclee
1784e8fb11a1Sshidokht cl->cl_offset[FDISK_P1 + count] =
1785e8fb11a1Sshidokht cl->cl_fmap[count].fmap_start;
17863ccda647Slclee }
17873ccda647Slclee #endif
17883ccda647Slclee
17893ccda647Slclee for (count = 0; count < NDKMAP; count++) {
17903ccda647Slclee #if defined(_SUNOS_VTOC_8)
1791e8fb11a1Sshidokht struct dk_map *lp = &cl->cl_map[count];
1792e8fb11a1Sshidokht cl->cl_offset[count] =
1793e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
17943ccda647Slclee #elif defined(_SUNOS_VTOC_16)
1795e8fb11a1Sshidokht struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
17963ccda647Slclee
1797e8fb11a1Sshidokht cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
17983ccda647Slclee #else
17993ccda647Slclee #error "No VTOC format defined."
18003ccda647Slclee #endif
18013ccda647Slclee }
18023ccda647Slclee
18033ccda647Slclee return (label_error);
18043ccda647Slclee }
18053ccda647Slclee
18063ccda647Slclee #if defined(_SUNOS_VTOC_16)
18073ccda647Slclee /*
18083ccda647Slclee * Function: cmlb_convert_geometry
18093ccda647Slclee *
18103ccda647Slclee * Description: Convert physical geometry into a dk_geom structure. In
18113ccda647Slclee * other words, make sure we don't wrap 16-bit values.
18123ccda647Slclee * e.g. converting from geom_cache to dk_geom
18133ccda647Slclee *
18143ccda647Slclee * Context: Kernel thread only
18153ccda647Slclee */
18163ccda647Slclee static void
1817b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
1818b081f1c4Syu, larry liu - Sun Microsystems - Beijing China struct dk_geom *cl_g, void *tg_cookie)
18193ccda647Slclee {
18203ccda647Slclee
1821b081f1c4Syu, larry liu - Sun Microsystems - Beijing China ASSERT(cl != NULL);
1822b081f1c4Syu, larry liu - Sun Microsystems - Beijing China ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1823b081f1c4Syu, larry liu - Sun Microsystems - Beijing China
18243ccda647Slclee /* Unlabeled SCSI floppy device */
1825c31fac72SShidokht Yadegari if (capacity < 160) {
1826c31fac72SShidokht Yadegari /* Less than 80K */
1827c31fac72SShidokht Yadegari cl_g->dkg_nhead = 1;
1828c31fac72SShidokht Yadegari cl_g->dkg_ncyl = capacity;
1829c31fac72SShidokht Yadegari cl_g->dkg_nsect = 1;
1830c31fac72SShidokht Yadegari return;
1831c31fac72SShidokht Yadegari } else if (capacity <= 0x1000) {
1832e8fb11a1Sshidokht cl_g->dkg_nhead = 2;
1833e8fb11a1Sshidokht cl_g->dkg_ncyl = 80;
1834e8fb11a1Sshidokht cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
18353ccda647Slclee return;
18363ccda647Slclee }
18373ccda647Slclee
18383ccda647Slclee /*
1839342440ecSPrasad Singamsetty * For all devices we calculate cylinders using the heads and sectors
1840342440ecSPrasad Singamsetty * we assign based on capacity of the device. The algorithm is
1841342440ecSPrasad Singamsetty * designed to be compatible with the way other operating systems
1842342440ecSPrasad Singamsetty * lay out fdisk tables for X86 and to insure that the cylinders never
1843342440ecSPrasad Singamsetty * exceed 65535 to prevent problems with X86 ioctls that report
1844342440ecSPrasad Singamsetty * geometry.
1845342440ecSPrasad Singamsetty * For some smaller disk sizes we report geometry that matches those
1846342440ecSPrasad Singamsetty * used by X86 BIOS usage. For larger disks, we use SPT that are
1847342440ecSPrasad Singamsetty * multiples of 63, since other OSes that are not limited to 16-bits
1848342440ecSPrasad Singamsetty * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
18493ccda647Slclee *
1850342440ecSPrasad Singamsetty * The following table (in order) illustrates some end result
1851342440ecSPrasad Singamsetty * calculations:
1852342440ecSPrasad Singamsetty *
1853342440ecSPrasad Singamsetty * Maximum number of blocks nhead nsect
1854342440ecSPrasad Singamsetty *
1855342440ecSPrasad Singamsetty * 2097152 (1GB) 64 32
1856342440ecSPrasad Singamsetty * 16777216 (8GB) 128 32
1857342440ecSPrasad Singamsetty * 1052819775 (502.02GB) 255 63
1858342440ecSPrasad Singamsetty * 2105639550 (0.98TB) 255 126
1859342440ecSPrasad Singamsetty * 3158459325 (1.47TB) 255 189
1860342440ecSPrasad Singamsetty * 4211279100 (1.96TB) 255 252
1861342440ecSPrasad Singamsetty * 5264098875 (2.45TB) 255 315
1862342440ecSPrasad Singamsetty * ...
1863b081f1c4Syu, larry liu - Sun Microsystems - Beijing China *
1864b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * For Solid State Drive(SSD), it uses 4K page size inside and may be
1865b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * double with every new generation. If the I/O is not aligned with
1866b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * page size on SSDs, SSDs perform a lot slower.
1867b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * By default, Solaris partition starts from cylinder 1. It will be
1868b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * misaligned even with 4K if using heads(255) and SPT(63). To
1869b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * workaround the problem, if the device is SSD, we use heads(224) and
1870b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * SPT multiple of 56. Thus the default Solaris partition starts from
1871b081f1c4Syu, larry liu - Sun Microsystems - Beijing China * a position that aligns with 128K on a 512 bytes sector size SSD.
18723ccda647Slclee */
18733ccda647Slclee
1874342440ecSPrasad Singamsetty if (capacity <= 0x200000) {
1875342440ecSPrasad Singamsetty cl_g->dkg_nhead = 64;
1876342440ecSPrasad Singamsetty cl_g->dkg_nsect = 32;
1877342440ecSPrasad Singamsetty } else if (capacity <= 0x01000000) {
1878342440ecSPrasad Singamsetty cl_g->dkg_nhead = 128;
1879342440ecSPrasad Singamsetty cl_g->dkg_nsect = 32;
1880342440ecSPrasad Singamsetty } else {
1881b081f1c4Syu, larry liu - Sun Microsystems - Beijing China tg_attribute_t tgattribute;
1882b081f1c4Syu, larry liu - Sun Microsystems - Beijing China int is_solid_state;
1883b081f1c4Syu, larry liu - Sun Microsystems - Beijing China unsigned short nhead;
1884b081f1c4Syu, larry liu - Sun Microsystems - Beijing China unsigned short nsect;
1885342440ecSPrasad Singamsetty
1886b081f1c4Syu, larry liu - Sun Microsystems - Beijing China bzero(&tgattribute, sizeof (tg_attribute_t));
1887b081f1c4Syu, larry liu - Sun Microsystems - Beijing China
1888b081f1c4Syu, larry liu - Sun Microsystems - Beijing China mutex_exit(CMLB_MUTEX(cl));
1889b081f1c4Syu, larry liu - Sun Microsystems - Beijing China is_solid_state =
1890b081f1c4Syu, larry liu - Sun Microsystems - Beijing China (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
1891b081f1c4Syu, larry liu - Sun Microsystems - Beijing China tgattribute.media_is_solid_state : FALSE;
1892b081f1c4Syu, larry liu - Sun Microsystems - Beijing China mutex_enter(CMLB_MUTEX(cl));
1893b081f1c4Syu, larry liu - Sun Microsystems - Beijing China
1894b081f1c4Syu, larry liu - Sun Microsystems - Beijing China if (is_solid_state) {
1895b081f1c4Syu, larry liu - Sun Microsystems - Beijing China nhead = 224;
1896b081f1c4Syu, larry liu - Sun Microsystems - Beijing China nsect = 56;
1897b081f1c4Syu, larry liu - Sun Microsystems - Beijing China } else {
1898b081f1c4Syu, larry liu - Sun Microsystems - Beijing China nhead = 255;
1899b081f1c4Syu, larry liu - Sun Microsystems - Beijing China nsect = 63;
1900b081f1c4Syu, larry liu - Sun Microsystems - Beijing China }
1901b081f1c4Syu, larry liu - Sun Microsystems - Beijing China
1902b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cl_g->dkg_nhead = nhead;
1903b081f1c4Syu, larry liu - Sun Microsystems - Beijing China
1904b132e0a0Slu cai - Sun Microsystems - Beijing China /* make dkg_nsect be smallest multiple of nsect */
1905342440ecSPrasad Singamsetty cl_g->dkg_nsect = ((capacity +
1906b081f1c4Syu, larry liu - Sun Microsystems - Beijing China (UINT16_MAX * nhead * nsect) - 1) /
1907b081f1c4Syu, larry liu - Sun Microsystems - Beijing China (UINT16_MAX * nhead * nsect)) * nsect;
1908342440ecSPrasad Singamsetty
1909342440ecSPrasad Singamsetty if (cl_g->dkg_nsect == 0)
1910b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cl_g->dkg_nsect = (UINT16_MAX / nsect) * nsect;
1911342440ecSPrasad Singamsetty }
1912342440ecSPrasad Singamsetty
19133ccda647Slclee }
19143ccda647Slclee #endif
19153ccda647Slclee
19163ccda647Slclee /*
19173ccda647Slclee * Function: cmlb_resync_geom_caches
19183ccda647Slclee *
19193ccda647Slclee * Description: (Re)initialize both geometry caches: the virtual geometry
19203ccda647Slclee * information is extracted from the HBA (the "geometry"
19213ccda647Slclee * capability), and the physical geometry cache data is
19223ccda647Slclee * generated by issuing MODE SENSE commands.
19233ccda647Slclee *
1924e8fb11a1Sshidokht * Arguments:
1925e8fb11a1Sshidokht * cl driver soft state (unit) structure
1926e8fb11a1Sshidokht * capacity disk capacity in #blocks
1927e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
1928e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
19293ccda647Slclee *
19303ccda647Slclee * Context: Kernel thread only (can sleep).
19313ccda647Slclee */
19323ccda647Slclee static void
1933e8fb11a1Sshidokht cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1934e8fb11a1Sshidokht void *tg_cookie)
19353ccda647Slclee {
19363ccda647Slclee struct cmlb_geom pgeom;
19373ccda647Slclee struct cmlb_geom lgeom;
19383ccda647Slclee struct cmlb_geom *pgeomp = &pgeom;
19393ccda647Slclee unsigned short nhead;
19403ccda647Slclee unsigned short nsect;
19413ccda647Slclee int spc;
19423ccda647Slclee int ret;
19433ccda647Slclee
1944e8fb11a1Sshidokht ASSERT(cl != NULL);
1945e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
19463ccda647Slclee
19473ccda647Slclee /*
19483ccda647Slclee * Ask the controller for its logical geometry.
19493ccda647Slclee * Note: if the HBA does not support scsi_ifgetcap("geometry"),
19503ccda647Slclee * then the lgeom cache will be invalid.
19513ccda647Slclee */
1952e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
19533ccda647Slclee bzero(&lgeom, sizeof (struct cmlb_geom));
1954e8fb11a1Sshidokht ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1955e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
19563ccda647Slclee
1957e8fb11a1Sshidokht bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
19583ccda647Slclee
19593ccda647Slclee /*
19603ccda647Slclee * Initialize the pgeom cache from lgeom, so that if MODE SENSE
19613ccda647Slclee * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
19623ccda647Slclee */
1963e8fb11a1Sshidokht if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1964e8fb11a1Sshidokht cl->cl_lgeom.g_nhead == 0) {
19653ccda647Slclee /*
19663ccda647Slclee * Note: Perhaps this needs to be more adaptive? The rationale
19673ccda647Slclee * is that, if there's no HBA geometry from the HBA driver, any
19683ccda647Slclee * guess is good, since this is the physical geometry. If MODE
19693ccda647Slclee * SENSE fails this gives a max cylinder size for non-LBA access
19703ccda647Slclee */
19713ccda647Slclee nhead = 255;
19723ccda647Slclee nsect = 63;
19733ccda647Slclee } else {
1974e8fb11a1Sshidokht nhead = cl->cl_lgeom.g_nhead;
1975e8fb11a1Sshidokht nsect = cl->cl_lgeom.g_nsect;
19763ccda647Slclee }
19773ccda647Slclee
1978e8fb11a1Sshidokht if (ISCD(cl)) {
19793ccda647Slclee pgeomp->g_nhead = 1;
19803ccda647Slclee pgeomp->g_nsect = nsect * nhead;
19813ccda647Slclee } else {
19823ccda647Slclee pgeomp->g_nhead = nhead;
19833ccda647Slclee pgeomp->g_nsect = nsect;
19843ccda647Slclee }
19853ccda647Slclee
19863ccda647Slclee spc = pgeomp->g_nhead * pgeomp->g_nsect;
19873ccda647Slclee pgeomp->g_capacity = capacity;
19887501b6d5SSheshadri Vasudevan if (spc == 0)
19897501b6d5SSheshadri Vasudevan pgeomp->g_ncyl = 0;
19907501b6d5SSheshadri Vasudevan else
19913ccda647Slclee pgeomp->g_ncyl = pgeomp->g_capacity / spc;
19923ccda647Slclee pgeomp->g_acyl = 0;
19933ccda647Slclee
19943ccda647Slclee /*
19953ccda647Slclee * Retrieve fresh geometry data from the hardware, stash it
19963ccda647Slclee * here temporarily before we rebuild the incore label.
19973ccda647Slclee *
19983ccda647Slclee * We want to use the MODE SENSE commands to derive the
19993ccda647Slclee * physical geometry of the device, but if either command
20003ccda647Slclee * fails, the logical geometry is used as the fallback for
20013ccda647Slclee * disk label geometry.
20023ccda647Slclee */
20033ccda647Slclee
2004e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
2005e8fb11a1Sshidokht (void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
2006e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
20073ccda647Slclee
20083ccda647Slclee /*
20093ccda647Slclee * Now update the real copy while holding the mutex. This
20103ccda647Slclee * way the global copy is never in an inconsistent state.
20113ccda647Slclee */
2012e8fb11a1Sshidokht bcopy(pgeomp, &cl->cl_pgeom, sizeof (cl->cl_pgeom));
20133ccda647Slclee
2014e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
20153ccda647Slclee "(cached from lgeom)\n");
2016e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
20173ccda647Slclee " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2018e8fb11a1Sshidokht cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
2019e8fb11a1Sshidokht cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
2020e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, " lbasize: %d; capacity: %ld; "
2021e8fb11a1Sshidokht "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
2022e8fb11a1Sshidokht cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
2023e8fb11a1Sshidokht cl->cl_pgeom.g_rpm);
20243ccda647Slclee }
20253ccda647Slclee
20263ccda647Slclee
2027aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2028aa1b14e7SSheshadri Vasudevan /*
2029aa1b14e7SSheshadri Vasudevan * Function: cmlb_update_ext_minor_nodes
2030aa1b14e7SSheshadri Vasudevan *
2031aa1b14e7SSheshadri Vasudevan * Description: Routine to add/remove extended partition device nodes
2032aa1b14e7SSheshadri Vasudevan *
2033aa1b14e7SSheshadri Vasudevan * Arguments:
2034aa1b14e7SSheshadri Vasudevan * cl driver soft state (unit) structure
2035aa1b14e7SSheshadri Vasudevan * num_parts Number of logical drives found on the LUN
2036aa1b14e7SSheshadri Vasudevan *
2037aa1b14e7SSheshadri Vasudevan * Should be called with the mutex held
2038aa1b14e7SSheshadri Vasudevan *
2039aa1b14e7SSheshadri Vasudevan * Return Code: 0 for success
2040aa1b14e7SSheshadri Vasudevan *
2041aa1b14e7SSheshadri Vasudevan * Context: User and Kernel thread
2042aa1b14e7SSheshadri Vasudevan *
2043aa1b14e7SSheshadri Vasudevan */
2044aa1b14e7SSheshadri Vasudevan static int
2045aa1b14e7SSheshadri Vasudevan cmlb_update_ext_minor_nodes(struct cmlb_lun *cl, int num_parts)
2046aa1b14e7SSheshadri Vasudevan {
2047aa1b14e7SSheshadri Vasudevan int i, count;
2048aa1b14e7SSheshadri Vasudevan char name[48];
2049aa1b14e7SSheshadri Vasudevan int instance;
2050aa1b14e7SSheshadri Vasudevan struct driver_minor_data *demdp, *demdpr;
2051aa1b14e7SSheshadri Vasudevan char *devnm;
2052aa1b14e7SSheshadri Vasudevan dev_info_t *pdip;
2053aa1b14e7SSheshadri Vasudevan boolean_t internal;
2054aa1b14e7SSheshadri Vasudevan
2055aa1b14e7SSheshadri Vasudevan ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2056aa1b14e7SSheshadri Vasudevan ASSERT(cl->cl_update_ext_minor_nodes == 1);
2057aa1b14e7SSheshadri Vasudevan
2058aa1b14e7SSheshadri Vasudevan internal = VOID2BOOLEAN(
2059aa1b14e7SSheshadri Vasudevan (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
2060aa1b14e7SSheshadri Vasudevan instance = ddi_get_instance(CMLB_DEVINFO(cl));
2061aa1b14e7SSheshadri Vasudevan demdp = dk_ext_minor_data;
2062aa1b14e7SSheshadri Vasudevan demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2063aa1b14e7SSheshadri Vasudevan
2064aa1b14e7SSheshadri Vasudevan
2065aa1b14e7SSheshadri Vasudevan if (cl->cl_logical_drive_count) {
2066aa1b14e7SSheshadri Vasudevan for (i = 0; i < cl->cl_logical_drive_count; i++) {
2067aa1b14e7SSheshadri Vasudevan (void) sprintf(name, "%s", demdp->name);
2068aa1b14e7SSheshadri Vasudevan ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2069aa1b14e7SSheshadri Vasudevan (void) sprintf(name, "%s", demdpr->name);
2070aa1b14e7SSheshadri Vasudevan ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2071aa1b14e7SSheshadri Vasudevan demdp++;
2072aa1b14e7SSheshadri Vasudevan demdpr++;
2073aa1b14e7SSheshadri Vasudevan }
2074aa1b14e7SSheshadri Vasudevan /* There are existing device nodes. Remove them */
2075aa1b14e7SSheshadri Vasudevan devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2076aa1b14e7SSheshadri Vasudevan (void) ddi_deviname(cl->cl_devi, devnm);
2077aa1b14e7SSheshadri Vasudevan pdip = ddi_get_parent(cl->cl_devi);
2078aa1b14e7SSheshadri Vasudevan (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2079aa1b14e7SSheshadri Vasudevan kmem_free(devnm, MAXNAMELEN + 1);
2080aa1b14e7SSheshadri Vasudevan }
2081aa1b14e7SSheshadri Vasudevan
2082aa1b14e7SSheshadri Vasudevan demdp = dk_ext_minor_data;
2083aa1b14e7SSheshadri Vasudevan demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2084aa1b14e7SSheshadri Vasudevan
2085aa1b14e7SSheshadri Vasudevan for (i = 0; i < num_parts; i++) {
2086aa1b14e7SSheshadri Vasudevan (void) sprintf(name, "%s", demdp->name);
2087aa1b14e7SSheshadri Vasudevan if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
2088aa1b14e7SSheshadri Vasudevan demdp->type,
2089aa1b14e7SSheshadri Vasudevan (instance << CMLBUNIT_SHIFT) | demdp->minor,
2090aa1b14e7SSheshadri Vasudevan cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
2091aa1b14e7SSheshadri Vasudevan /*
2092aa1b14e7SSheshadri Vasudevan * Clean up any nodes that may have been
2093aa1b14e7SSheshadri Vasudevan * created, in case this fails in the middle
2094aa1b14e7SSheshadri Vasudevan * of the loop.
2095aa1b14e7SSheshadri Vasudevan */
2096aa1b14e7SSheshadri Vasudevan ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2097aa1b14e7SSheshadri Vasudevan cl->cl_logical_drive_count = 0;
2098aa1b14e7SSheshadri Vasudevan return (ENXIO);
2099aa1b14e7SSheshadri Vasudevan }
2100aa1b14e7SSheshadri Vasudevan (void) sprintf(name, "%s", demdpr->name);
2101aa1b14e7SSheshadri Vasudevan if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
2102aa1b14e7SSheshadri Vasudevan demdpr->type,
2103aa1b14e7SSheshadri Vasudevan (instance << CMLBUNIT_SHIFT) | demdpr->minor,
2104aa1b14e7SSheshadri Vasudevan cl->cl_node_type, NULL) == DDI_FAILURE) {
2105aa1b14e7SSheshadri Vasudevan /*
2106aa1b14e7SSheshadri Vasudevan * Clean up any nodes that may have been
2107aa1b14e7SSheshadri Vasudevan * created, in case this fails in the middle
2108aa1b14e7SSheshadri Vasudevan * of the loop.
2109aa1b14e7SSheshadri Vasudevan */
2110aa1b14e7SSheshadri Vasudevan ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2111aa1b14e7SSheshadri Vasudevan cl->cl_logical_drive_count = 0;
2112aa1b14e7SSheshadri Vasudevan return (ENXIO);
2113aa1b14e7SSheshadri Vasudevan }
2114aa1b14e7SSheshadri Vasudevan demdp++;
2115aa1b14e7SSheshadri Vasudevan demdpr++;
2116aa1b14e7SSheshadri Vasudevan }
2117aa1b14e7SSheshadri Vasudevan
2118aa1b14e7SSheshadri Vasudevan /* Update the cl_map array for logical drives */
2119aa1b14e7SSheshadri Vasudevan for (count = 0; count < MAX_EXT_PARTS; count++) {
2120aa1b14e7SSheshadri Vasudevan cl->cl_map[FDISK_P4 + 1 + count].dkl_cylno = UINT32_MAX;
2121aa1b14e7SSheshadri Vasudevan cl->cl_map[FDISK_P4 + 1 + count].dkl_nblk =
2122aa1b14e7SSheshadri Vasudevan cl->cl_fmap[FD_NUMPART + count].fmap_nblk;
2123aa1b14e7SSheshadri Vasudevan cl->cl_offset[FDISK_P4 + 1 + count] =
2124aa1b14e7SSheshadri Vasudevan cl->cl_fmap[FD_NUMPART + count].fmap_start;
2125aa1b14e7SSheshadri Vasudevan }
2126aa1b14e7SSheshadri Vasudevan
2127aa1b14e7SSheshadri Vasudevan cl->cl_logical_drive_count = i;
2128aa1b14e7SSheshadri Vasudevan cl->cl_update_ext_minor_nodes = 0;
2129aa1b14e7SSheshadri Vasudevan return (0);
2130aa1b14e7SSheshadri Vasudevan }
2131aa1b14e7SSheshadri Vasudevan /*
2132aa1b14e7SSheshadri Vasudevan * Function: cmlb_validate_ext_part
2133aa1b14e7SSheshadri Vasudevan *
2134aa1b14e7SSheshadri Vasudevan * Description: utility routine to validate an extended partition's
2135aa1b14e7SSheshadri Vasudevan * metadata as found on disk
2136aa1b14e7SSheshadri Vasudevan *
2137aa1b14e7SSheshadri Vasudevan * Arguments:
2138aa1b14e7SSheshadri Vasudevan * cl driver soft state (unit) structure
2139aa1b14e7SSheshadri Vasudevan * part partition number of the extended partition
2140aa1b14e7SSheshadri Vasudevan * epart partition number of the logical drive
2141aa1b14e7SSheshadri Vasudevan * start absolute sector number of the start of the logical
2142aa1b14e7SSheshadri Vasudevan * drive being validated
2143aa1b14e7SSheshadri Vasudevan * size size of logical drive being validated
2144aa1b14e7SSheshadri Vasudevan *
2145aa1b14e7SSheshadri Vasudevan * Return Code: 0 for success
2146aa1b14e7SSheshadri Vasudevan *
2147aa1b14e7SSheshadri Vasudevan * Context: User and Kernel thread
2148aa1b14e7SSheshadri Vasudevan *
2149aa1b14e7SSheshadri Vasudevan * Algorithm :
2150aa1b14e7SSheshadri Vasudevan * Error cases are :
2151aa1b14e7SSheshadri Vasudevan * 1. If start block is lesser than or equal to the end block
2152aa1b14e7SSheshadri Vasudevan * 2. If either start block or end block is beyond the bounadry
2153aa1b14e7SSheshadri Vasudevan * of the extended partition.
2154aa1b14e7SSheshadri Vasudevan * 3. start or end block overlap with existing partitions.
2155aa1b14e7SSheshadri Vasudevan * To check this, first make sure that the start block doesnt
2156aa1b14e7SSheshadri Vasudevan * overlap with existing partitions. Then, calculate the
2157aa1b14e7SSheshadri Vasudevan * possible end block for the given start block that doesnt
2158aa1b14e7SSheshadri Vasudevan * overlap with existing partitions. This can be calculated by
2159aa1b14e7SSheshadri Vasudevan * first setting the possible end block to the end of the
2160aa1b14e7SSheshadri Vasudevan * extended partition (optimistic) and then, checking if there
2161aa1b14e7SSheshadri Vasudevan * is any other partition that lies after the start of the
2162aa1b14e7SSheshadri Vasudevan * partition being validated. If so, set the possible end to
2163aa1b14e7SSheshadri Vasudevan * one block less than the beginning of the next nearest partition
2164aa1b14e7SSheshadri Vasudevan * If the actual end block is greater than the calculated end
2165aa1b14e7SSheshadri Vasudevan * block, we have an overlap.
2166aa1b14e7SSheshadri Vasudevan *
2167aa1b14e7SSheshadri Vasudevan */
2168aa1b14e7SSheshadri Vasudevan static int
2169aa1b14e7SSheshadri Vasudevan cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart, uint32_t start,
2170aa1b14e7SSheshadri Vasudevan uint32_t size)
2171aa1b14e7SSheshadri Vasudevan {
2172aa1b14e7SSheshadri Vasudevan int i;
2173aa1b14e7SSheshadri Vasudevan uint32_t end = start + size - 1;
2174aa1b14e7SSheshadri Vasudevan uint32_t ext_start = cl->cl_fmap[part].fmap_start;
2175aa1b14e7SSheshadri Vasudevan uint32_t ext_end = ext_start + cl->cl_fmap[part].fmap_nblk - 1;
2176aa1b14e7SSheshadri Vasudevan uint32_t ts, te;
2177aa1b14e7SSheshadri Vasudevan uint32_t poss_end = ext_end;
2178aa1b14e7SSheshadri Vasudevan
2179aa1b14e7SSheshadri Vasudevan if (end <= start) {
2180aa1b14e7SSheshadri Vasudevan return (1);
2181aa1b14e7SSheshadri Vasudevan }
2182aa1b14e7SSheshadri Vasudevan
2183aa1b14e7SSheshadri Vasudevan /*
2184aa1b14e7SSheshadri Vasudevan * Check if the logical drive boundaries are within that of the
2185aa1b14e7SSheshadri Vasudevan * extended partition.
2186aa1b14e7SSheshadri Vasudevan */
2187aa1b14e7SSheshadri Vasudevan if (start <= ext_start || start > ext_end || end <= ext_start ||
2188aa1b14e7SSheshadri Vasudevan end > ext_end) {
2189aa1b14e7SSheshadri Vasudevan return (1);
2190aa1b14e7SSheshadri Vasudevan }
2191aa1b14e7SSheshadri Vasudevan
2192aa1b14e7SSheshadri Vasudevan /*
2193aa1b14e7SSheshadri Vasudevan * epart will be equal to FD_NUMPART if it is the first logical drive.
2194aa1b14e7SSheshadri Vasudevan * There is no need to check for overlaps with other logical drives,
2195aa1b14e7SSheshadri Vasudevan * since it is the only logical drive that we have come across so far.
2196aa1b14e7SSheshadri Vasudevan */
2197aa1b14e7SSheshadri Vasudevan if (epart == FD_NUMPART) {
2198aa1b14e7SSheshadri Vasudevan return (0);
2199aa1b14e7SSheshadri Vasudevan }
2200aa1b14e7SSheshadri Vasudevan
2201aa1b14e7SSheshadri Vasudevan /* Check for overlaps with existing logical drives */
2202aa1b14e7SSheshadri Vasudevan i = FD_NUMPART;
2203aa1b14e7SSheshadri Vasudevan ts = cl->cl_fmap[FD_NUMPART].fmap_start;
2204aa1b14e7SSheshadri Vasudevan te = ts + cl->cl_fmap[FD_NUMPART].fmap_nblk - 1;
2205aa1b14e7SSheshadri Vasudevan
2206aa1b14e7SSheshadri Vasudevan while ((i < epart) && ts && te) {
2207aa1b14e7SSheshadri Vasudevan if (start >= ts && start <= te) {
2208aa1b14e7SSheshadri Vasudevan return (1);
2209aa1b14e7SSheshadri Vasudevan }
2210aa1b14e7SSheshadri Vasudevan
2211aa1b14e7SSheshadri Vasudevan if ((ts < poss_end) && (ts > start)) {
2212aa1b14e7SSheshadri Vasudevan poss_end = ts - 1;
2213aa1b14e7SSheshadri Vasudevan }
2214aa1b14e7SSheshadri Vasudevan
2215aa1b14e7SSheshadri Vasudevan i++;
2216aa1b14e7SSheshadri Vasudevan ts = cl->cl_fmap[i].fmap_start;
2217aa1b14e7SSheshadri Vasudevan te = ts + cl->cl_fmap[i].fmap_nblk - 1;
2218aa1b14e7SSheshadri Vasudevan }
2219aa1b14e7SSheshadri Vasudevan
2220aa1b14e7SSheshadri Vasudevan if (end > poss_end) {
2221aa1b14e7SSheshadri Vasudevan return (1);
2222aa1b14e7SSheshadri Vasudevan }
2223aa1b14e7SSheshadri Vasudevan
2224aa1b14e7SSheshadri Vasudevan return (0);
2225aa1b14e7SSheshadri Vasudevan }
2226aa1b14e7SSheshadri Vasudevan
2227aa1b14e7SSheshadri Vasudevan
2228aa1b14e7SSheshadri Vasudevan /*
2229aa1b14e7SSheshadri Vasudevan * Function: cmlb_is_linux_swap
2230aa1b14e7SSheshadri Vasudevan *
2231aa1b14e7SSheshadri Vasudevan * Description: utility routine to verify if a partition is a linux swap
2232aa1b14e7SSheshadri Vasudevan * partition or not.
2233aa1b14e7SSheshadri Vasudevan *
2234aa1b14e7SSheshadri Vasudevan * Arguments:
2235aa1b14e7SSheshadri Vasudevan * cl driver soft state (unit) structure
2236aa1b14e7SSheshadri Vasudevan * part_start absolute sector number of the start of the partition
2237aa1b14e7SSheshadri Vasudevan * being verified
2238aa1b14e7SSheshadri Vasudevan * tg_cookie cookie from target driver to be passed back to target
2239aa1b14e7SSheshadri Vasudevan * driver when we call back to it through tg_ops.
2240aa1b14e7SSheshadri Vasudevan *
2241aa1b14e7SSheshadri Vasudevan * Return Code: 0 for success
2242aa1b14e7SSheshadri Vasudevan *
2243aa1b14e7SSheshadri Vasudevan * Context: User and Kernel thread
2244aa1b14e7SSheshadri Vasudevan *
2245aa1b14e7SSheshadri Vasudevan * Notes:
2246aa1b14e7SSheshadri Vasudevan * The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2247aa1b14e7SSheshadri Vasudevan * last 10 bytes of a disk block whose size is that of the linux page
2248aa1b14e7SSheshadri Vasudevan * size. This disk block is found at the beginning of the swap partition.
2249aa1b14e7SSheshadri Vasudevan */
2250aa1b14e7SSheshadri Vasudevan static int
2251aa1b14e7SSheshadri Vasudevan cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start, void *tg_cookie)
2252aa1b14e7SSheshadri Vasudevan {
2253aa1b14e7SSheshadri Vasudevan int i;
2254aa1b14e7SSheshadri Vasudevan int rval = -1;
2255aa1b14e7SSheshadri Vasudevan uint32_t seek_offset;
2256aa1b14e7SSheshadri Vasudevan uint32_t linux_pg_size;
2257aa1b14e7SSheshadri Vasudevan char *buf, *linux_swap_magic;
2258aa1b14e7SSheshadri Vasudevan int sec_sz = cl->cl_sys_blocksize;
2259aa1b14e7SSheshadri Vasudevan /* Known linux kernel page sizes */
2260aa1b14e7SSheshadri Vasudevan uint32_t linux_pg_size_arr[] = {4096, };
2261aa1b14e7SSheshadri Vasudevan
2262aa1b14e7SSheshadri Vasudevan ASSERT(cl != NULL);
2263aa1b14e7SSheshadri Vasudevan ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2264aa1b14e7SSheshadri Vasudevan
2265aa1b14e7SSheshadri Vasudevan if ((buf = kmem_zalloc(sec_sz, KM_NOSLEEP)) == NULL) {
2266aa1b14e7SSheshadri Vasudevan return (ENOMEM);
2267aa1b14e7SSheshadri Vasudevan }
2268aa1b14e7SSheshadri Vasudevan
2269e998e519SSheshadri Vasudevan /*
2270e998e519SSheshadri Vasudevan * Check if there is a sane Solaris VTOC
2271e998e519SSheshadri Vasudevan * If there is a valid vtoc, no need to lookup
2272e998e519SSheshadri Vasudevan * for the linux swap signature.
2273e998e519SSheshadri Vasudevan */
2274e998e519SSheshadri Vasudevan mutex_exit(CMLB_MUTEX(cl));
2275e998e519SSheshadri Vasudevan rval = DK_TG_READ(cl, buf, part_start + DK_LABEL_LOC,
2276e998e519SSheshadri Vasudevan sec_sz, tg_cookie);
2277e998e519SSheshadri Vasudevan mutex_enter(CMLB_MUTEX(cl));
2278e998e519SSheshadri Vasudevan if (rval != 0) {
2279e998e519SSheshadri Vasudevan cmlb_dbg(CMLB_ERROR, cl,
2280e998e519SSheshadri Vasudevan "cmlb_is_linux_swap: disk vtoc read err\n");
2281e998e519SSheshadri Vasudevan rval = EIO;
2282e998e519SSheshadri Vasudevan goto done;
2283e998e519SSheshadri Vasudevan }
2284e998e519SSheshadri Vasudevan
2285e998e519SSheshadri Vasudevan if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
2286e998e519SSheshadri Vasudevan (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
2287e998e519SSheshadri Vasudevan rval = -1;
2288e998e519SSheshadri Vasudevan goto done;
2289e998e519SSheshadri Vasudevan }
2290e998e519SSheshadri Vasudevan
2291e998e519SSheshadri Vasudevan
2292e998e519SSheshadri Vasudevan /* No valid vtoc, so check for linux swap signature */
2293aa1b14e7SSheshadri Vasudevan linux_swap_magic = buf + sec_sz - 10;
2294aa1b14e7SSheshadri Vasudevan
2295aa1b14e7SSheshadri Vasudevan for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
2296aa1b14e7SSheshadri Vasudevan linux_pg_size = linux_pg_size_arr[i];
2297aa1b14e7SSheshadri Vasudevan seek_offset = linux_pg_size/sec_sz - 1;
2298aa1b14e7SSheshadri Vasudevan seek_offset += part_start;
2299aa1b14e7SSheshadri Vasudevan
2300aa1b14e7SSheshadri Vasudevan mutex_exit(CMLB_MUTEX(cl));
2301aa1b14e7SSheshadri Vasudevan rval = DK_TG_READ(cl, buf, seek_offset, sec_sz, tg_cookie);
2302aa1b14e7SSheshadri Vasudevan mutex_enter(CMLB_MUTEX(cl));
2303aa1b14e7SSheshadri Vasudevan
2304aa1b14e7SSheshadri Vasudevan if (rval != 0) {
2305aa1b14e7SSheshadri Vasudevan cmlb_dbg(CMLB_ERROR, cl,
2306aa1b14e7SSheshadri Vasudevan "cmlb_is_linux_swap: disk read err\n");
2307aa1b14e7SSheshadri Vasudevan rval = EIO;
2308aa1b14e7SSheshadri Vasudevan break;
2309aa1b14e7SSheshadri Vasudevan }
2310aa1b14e7SSheshadri Vasudevan
2311aa1b14e7SSheshadri Vasudevan rval = -1;
2312aa1b14e7SSheshadri Vasudevan
2313aa1b14e7SSheshadri Vasudevan if ((strncmp(linux_swap_magic, "SWAP-SPACE", 10) == 0) ||
2314aa1b14e7SSheshadri Vasudevan (strncmp(linux_swap_magic, "SWAPSPACE2", 10) == 0)) {
2315aa1b14e7SSheshadri Vasudevan /* Found a linux swap */
2316aa1b14e7SSheshadri Vasudevan rval = 0;
2317aa1b14e7SSheshadri Vasudevan break;
2318aa1b14e7SSheshadri Vasudevan }
2319aa1b14e7SSheshadri Vasudevan }
2320aa1b14e7SSheshadri Vasudevan
2321e998e519SSheshadri Vasudevan done:
2322aa1b14e7SSheshadri Vasudevan kmem_free(buf, sec_sz);
2323aa1b14e7SSheshadri Vasudevan return (rval);
2324aa1b14e7SSheshadri Vasudevan }
2325aa1b14e7SSheshadri Vasudevan #endif
2326aa1b14e7SSheshadri Vasudevan
23273ccda647Slclee /*
23283ccda647Slclee * Function: cmlb_read_fdisk
23293ccda647Slclee *
23303ccda647Slclee * Description: utility routine to read the fdisk table.
23313ccda647Slclee *
2332e8fb11a1Sshidokht * Arguments:
2333e8fb11a1Sshidokht * cl driver soft state (unit) structure
2334e8fb11a1Sshidokht * capacity disk capacity in #blocks
2335e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
2336e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
23373ccda647Slclee *
23383ccda647Slclee * Return Code: 0 for success (includes not reading for no_fdisk_present case
23393ccda647Slclee * errnos from tg_rw if failed to read the first block.
23403ccda647Slclee *
23413ccda647Slclee * Context: Kernel thread only (can sleep).
23423ccda647Slclee */
23433ccda647Slclee /*ARGSUSED*/
23443ccda647Slclee static int
2345e8fb11a1Sshidokht cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
23463ccda647Slclee {
23473ccda647Slclee #if defined(_NO_FDISK_PRESENT)
23483ccda647Slclee
2349e8fb11a1Sshidokht cl->cl_solaris_offset = 0;
2350e8fb11a1Sshidokht cl->cl_solaris_size = capacity;
2351e8fb11a1Sshidokht bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
23523ccda647Slclee return (0);
23533ccda647Slclee
23543ccda647Slclee #elif defined(_FIRMWARE_NEEDS_FDISK)
23553ccda647Slclee
23563ccda647Slclee struct ipart *fdp;
23573ccda647Slclee struct mboot *mbp;
23583ccda647Slclee struct ipart fdisk[FD_NUMPART];
2359aa1b14e7SSheshadri Vasudevan int i, k;
23603ccda647Slclee char sigbuf[2];
23613ccda647Slclee caddr_t bufp;
23623ccda647Slclee int uidx;
23633ccda647Slclee int rval;
23643ccda647Slclee int lba = 0;
23653ccda647Slclee uint_t solaris_offset; /* offset to solaris part. */
23663ccda647Slclee daddr_t solaris_size; /* size of solaris partition */
23673ccda647Slclee uint32_t blocksize;
2368aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2369aa1b14e7SSheshadri Vasudevan struct ipart eparts[2];
2370aa1b14e7SSheshadri Vasudevan struct ipart *efdp1 = &eparts[0];
2371aa1b14e7SSheshadri Vasudevan struct ipart *efdp2 = &eparts[1];
2372aa1b14e7SSheshadri Vasudevan int ext_part_exists = 0;
2373aa1b14e7SSheshadri Vasudevan int ld_count = 0;
2374aa1b14e7SSheshadri Vasudevan #endif
23753ccda647Slclee
2376e8fb11a1Sshidokht ASSERT(cl != NULL);
2377e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
23783ccda647Slclee
23793ccda647Slclee /*
23803ccda647Slclee * Start off assuming no fdisk table
23813ccda647Slclee */
23823ccda647Slclee solaris_offset = 0;
23833ccda647Slclee solaris_size = capacity;
23843ccda647Slclee
2385e8fb11a1Sshidokht blocksize = cl->cl_tgt_blocksize;
23863ccda647Slclee
23873ccda647Slclee bufp = kmem_zalloc(blocksize, KM_SLEEP);
23883ccda647Slclee
2389e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
2390e8fb11a1Sshidokht rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
2391e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
23923ccda647Slclee
23933ccda647Slclee if (rval != 0) {
2394e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
23953ccda647Slclee "cmlb_read_fdisk: fdisk read err\n");
2396e8fb11a1Sshidokht bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2397e8fb11a1Sshidokht goto done;
23983ccda647Slclee }
23993ccda647Slclee
24003ccda647Slclee mbp = (struct mboot *)bufp;
24013ccda647Slclee
24023ccda647Slclee /*
24033ccda647Slclee * The fdisk table does not begin on a 4-byte boundary within the
24043ccda647Slclee * master boot record, so we copy it to an aligned structure to avoid
24053ccda647Slclee * alignment exceptions on some processors.
24063ccda647Slclee */
24073ccda647Slclee bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
24083ccda647Slclee
24093ccda647Slclee /*
24103ccda647Slclee * Check for lba support before verifying sig; sig might not be
24113ccda647Slclee * there, say on a blank disk, but the max_chs mark may still
24123ccda647Slclee * be present.
24133ccda647Slclee *
24143ccda647Slclee * Note: LBA support and BEFs are an x86-only concept but this
24153ccda647Slclee * code should work OK on SPARC as well.
24163ccda647Slclee */
24173ccda647Slclee
24183ccda647Slclee /*
24193ccda647Slclee * First, check for lba-access-ok on root node (or prom root node)
24203ccda647Slclee * if present there, don't need to search fdisk table.
24213ccda647Slclee */
24223ccda647Slclee if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
24233ccda647Slclee "lba-access-ok", 0) != 0) {
24243ccda647Slclee /* All drives do LBA; don't search fdisk table */
24253ccda647Slclee lba = 1;
24263ccda647Slclee } else {
24273ccda647Slclee /* Okay, look for mark in fdisk table */
24283ccda647Slclee for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
24293ccda647Slclee /* accumulate "lba" value from all partitions */
24303ccda647Slclee lba = (lba || cmlb_has_max_chs_vals(fdp));
24313ccda647Slclee }
24323ccda647Slclee }
24333ccda647Slclee
24343ccda647Slclee if (lba != 0) {
2435e8fb11a1Sshidokht dev_t dev = cmlb_make_device(cl);
24363ccda647Slclee
2437e8fb11a1Sshidokht if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
24383ccda647Slclee "lba-access-ok", 0) == 0) {
24393ccda647Slclee /* not found; create it */
2440e8fb11a1Sshidokht if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
24413ccda647Slclee "lba-access-ok", (caddr_t)NULL, 0) !=
24423ccda647Slclee DDI_PROP_SUCCESS) {
2443e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
24443ccda647Slclee "cmlb_read_fdisk: Can't create lba "
24453ccda647Slclee "property for instance %d\n",
2446e8fb11a1Sshidokht ddi_get_instance(CMLB_DEVINFO(cl)));
24473ccda647Slclee }
24483ccda647Slclee }
24493ccda647Slclee }
24503ccda647Slclee
24513ccda647Slclee bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
24523ccda647Slclee
24533ccda647Slclee /*
24543ccda647Slclee * Endian-independent signature check
24553ccda647Slclee */
24563ccda647Slclee if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
24573ccda647Slclee (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
2458e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
24593ccda647Slclee "cmlb_read_fdisk: no fdisk\n");
2460e8fb11a1Sshidokht bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
24613ccda647Slclee goto done;
24623ccda647Slclee }
24633ccda647Slclee
24643ccda647Slclee #ifdef CMLBDEBUG
2465e8fb11a1Sshidokht if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
24663ccda647Slclee fdp = fdisk;
2467e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, "cmlb_read_fdisk:\n");
2468e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, " relsect "
24693ccda647Slclee "numsect sysid bootid\n");
24703ccda647Slclee for (i = 0; i < FD_NUMPART; i++, fdp++) {
2471e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
24723ccda647Slclee " %d: %8d %8d 0x%08x 0x%08x\n",
24733ccda647Slclee i, fdp->relsect, fdp->numsect,
24743ccda647Slclee fdp->systid, fdp->bootid);
24753ccda647Slclee }
24763ccda647Slclee }
24773ccda647Slclee #endif
24783ccda647Slclee
24793ccda647Slclee /*
24803ccda647Slclee * Try to find the unix partition
24813ccda647Slclee */
24823ccda647Slclee uidx = -1;
24833ccda647Slclee solaris_offset = 0;
24843ccda647Slclee solaris_size = 0;
24853ccda647Slclee
24863ccda647Slclee for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2487342440ecSPrasad Singamsetty uint32_t relsect;
2488342440ecSPrasad Singamsetty uint32_t numsect;
2489aa1b14e7SSheshadri Vasudevan uchar_t systid;
2490aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2491aa1b14e7SSheshadri Vasudevan /*
2492aa1b14e7SSheshadri Vasudevan * Stores relative block offset from the beginning of the
2493aa1b14e7SSheshadri Vasudevan * Extended Partition.
2494aa1b14e7SSheshadri Vasudevan */
2495aa1b14e7SSheshadri Vasudevan int ext_relsect = 0;
2496aa1b14e7SSheshadri Vasudevan #endif
24973ccda647Slclee
24983ccda647Slclee if (fdp->numsect == 0) {
2499e8fb11a1Sshidokht cl->cl_fmap[i].fmap_start = 0;
2500e8fb11a1Sshidokht cl->cl_fmap[i].fmap_nblk = 0;
25013ccda647Slclee continue;
25023ccda647Slclee }
25033ccda647Slclee
25043ccda647Slclee /*
25053ccda647Slclee * Data in the fdisk table is little-endian.
25063ccda647Slclee */
25073ccda647Slclee relsect = LE_32(fdp->relsect);
25083ccda647Slclee numsect = LE_32(fdp->numsect);
25093ccda647Slclee
2510e8fb11a1Sshidokht cl->cl_fmap[i].fmap_start = relsect;
2511e8fb11a1Sshidokht cl->cl_fmap[i].fmap_nblk = numsect;
2512aa1b14e7SSheshadri Vasudevan cl->cl_fmap[i].fmap_systid = LE_8(fdp->systid);
2513aa1b14e7SSheshadri Vasudevan
2514aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2515aa1b14e7SSheshadri Vasudevan /* Support only one extended partition per LUN */
2516aa1b14e7SSheshadri Vasudevan if ((fdp->systid == EXTDOS || fdp->systid == FDISK_EXTLBA) &&
2517aa1b14e7SSheshadri Vasudevan (ext_part_exists == 0)) {
2518aa1b14e7SSheshadri Vasudevan int j;
2519aa1b14e7SSheshadri Vasudevan uint32_t logdrive_offset;
2520aa1b14e7SSheshadri Vasudevan uint32_t ext_numsect;
2521aa1b14e7SSheshadri Vasudevan uint32_t abs_secnum;
2522aa1b14e7SSheshadri Vasudevan
2523aa1b14e7SSheshadri Vasudevan ext_part_exists = 1;
2524aa1b14e7SSheshadri Vasudevan
2525aa1b14e7SSheshadri Vasudevan for (j = FD_NUMPART; j < FDISK_PARTS; j++) {
2526aa1b14e7SSheshadri Vasudevan mutex_exit(CMLB_MUTEX(cl));
2527aa1b14e7SSheshadri Vasudevan rval = DK_TG_READ(cl, bufp,
2528aa1b14e7SSheshadri Vasudevan (relsect + ext_relsect), blocksize,
2529aa1b14e7SSheshadri Vasudevan tg_cookie);
2530aa1b14e7SSheshadri Vasudevan mutex_enter(CMLB_MUTEX(cl));
2531aa1b14e7SSheshadri Vasudevan
2532aa1b14e7SSheshadri Vasudevan if (rval != 0) {
2533aa1b14e7SSheshadri Vasudevan cmlb_dbg(CMLB_ERROR, cl,
2534aa1b14e7SSheshadri Vasudevan "cmlb_read_fdisk: Extended "
2535aa1b14e7SSheshadri Vasudevan "partition read err\n");
2536aa1b14e7SSheshadri Vasudevan goto done;
2537aa1b14e7SSheshadri Vasudevan }
2538aa1b14e7SSheshadri Vasudevan /*
2539aa1b14e7SSheshadri Vasudevan * The first ipart entry provides the offset
2540aa1b14e7SSheshadri Vasudevan * at which the logical drive starts off from
2541aa1b14e7SSheshadri Vasudevan * the beginning of the container partition
2542aa1b14e7SSheshadri Vasudevan * and the size of the logical drive.
2543aa1b14e7SSheshadri Vasudevan * The second ipart entry provides the offset
2544aa1b14e7SSheshadri Vasudevan * of the next container partition from the
2545aa1b14e7SSheshadri Vasudevan * beginning of the extended partition.
2546aa1b14e7SSheshadri Vasudevan */
2547aa1b14e7SSheshadri Vasudevan bcopy(&bufp[FDISK_PART_TABLE_START], eparts,
2548aa1b14e7SSheshadri Vasudevan sizeof (eparts));
2549aa1b14e7SSheshadri Vasudevan logdrive_offset = LE_32(efdp1->relsect);
2550aa1b14e7SSheshadri Vasudevan ext_numsect = LE_32(efdp1->numsect);
2551aa1b14e7SSheshadri Vasudevan systid = LE_8(efdp1->systid);
2552aa1b14e7SSheshadri Vasudevan if (logdrive_offset <= 0 || ext_numsect <= 0)
2553aa1b14e7SSheshadri Vasudevan break;
2554aa1b14e7SSheshadri Vasudevan abs_secnum = relsect + ext_relsect +
2555aa1b14e7SSheshadri Vasudevan logdrive_offset;
2556aa1b14e7SSheshadri Vasudevan
2557aa1b14e7SSheshadri Vasudevan /* Boundary condition and overlap checking */
2558aa1b14e7SSheshadri Vasudevan if (cmlb_validate_ext_part(cl, i, j, abs_secnum,
2559aa1b14e7SSheshadri Vasudevan ext_numsect)) {
2560aa1b14e7SSheshadri Vasudevan break;
2561aa1b14e7SSheshadri Vasudevan }
2562aa1b14e7SSheshadri Vasudevan
2563aa1b14e7SSheshadri Vasudevan if ((cl->cl_fmap[j].fmap_start != abs_secnum) ||
2564aa1b14e7SSheshadri Vasudevan (cl->cl_fmap[j].fmap_nblk != ext_numsect) ||
2565aa1b14e7SSheshadri Vasudevan (cl->cl_fmap[j].fmap_systid != systid)) {
2566aa1b14e7SSheshadri Vasudevan /*
2567aa1b14e7SSheshadri Vasudevan * Indicates change from previous
2568aa1b14e7SSheshadri Vasudevan * partinfo. Need to recreate
2569aa1b14e7SSheshadri Vasudevan * logical device nodes.
2570aa1b14e7SSheshadri Vasudevan */
2571aa1b14e7SSheshadri Vasudevan cl->cl_update_ext_minor_nodes = 1;
2572aa1b14e7SSheshadri Vasudevan }
2573aa1b14e7SSheshadri Vasudevan cl->cl_fmap[j].fmap_start = abs_secnum;
2574aa1b14e7SSheshadri Vasudevan cl->cl_fmap[j].fmap_nblk = ext_numsect;
2575aa1b14e7SSheshadri Vasudevan cl->cl_fmap[j].fmap_systid = systid;
2576aa1b14e7SSheshadri Vasudevan ld_count++;
2577aa1b14e7SSheshadri Vasudevan
2578e998e519SSheshadri Vasudevan if ((efdp1->systid == SUNIXOS &&
2579e998e519SSheshadri Vasudevan (cmlb_is_linux_swap(cl, abs_secnum,
2580e998e519SSheshadri Vasudevan tg_cookie) != 0)) ||
2581e998e519SSheshadri Vasudevan efdp1->systid == SUNIXOS2) {
2582e998e519SSheshadri Vasudevan if (uidx == -1) {
2583aa1b14e7SSheshadri Vasudevan uidx = 0;
2584aa1b14e7SSheshadri Vasudevan solaris_offset = abs_secnum;
2585aa1b14e7SSheshadri Vasudevan solaris_size = ext_numsect;
2586aa1b14e7SSheshadri Vasudevan }
2587aa1b14e7SSheshadri Vasudevan }
2588aa1b14e7SSheshadri Vasudevan
2589aa1b14e7SSheshadri Vasudevan if ((ext_relsect = LE_32(efdp2->relsect)) == 0)
2590aa1b14e7SSheshadri Vasudevan break;
2591aa1b14e7SSheshadri Vasudevan }
2592aa1b14e7SSheshadri Vasudevan }
2593aa1b14e7SSheshadri Vasudevan
2594aa1b14e7SSheshadri Vasudevan #endif
25953ccda647Slclee
25963ccda647Slclee if (fdp->systid != SUNIXOS &&
25973ccda647Slclee fdp->systid != SUNIXOS2 &&
25983ccda647Slclee fdp->systid != EFI_PMBR) {
25993ccda647Slclee continue;
26003ccda647Slclee }
26013ccda647Slclee
26023ccda647Slclee /*
26033ccda647Slclee * use the last active solaris partition id found
26043ccda647Slclee * (there should only be 1 active partition id)
26053ccda647Slclee *
26063ccda647Slclee * if there are no active solaris partition id
26073ccda647Slclee * then use the first inactive solaris partition id
26083ccda647Slclee */
26093ccda647Slclee if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
2610aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2611e998e519SSheshadri Vasudevan if (fdp->systid != SUNIXOS ||
2612e998e519SSheshadri Vasudevan (fdp->systid == SUNIXOS &&
2613e998e519SSheshadri Vasudevan (cmlb_is_linux_swap(cl, relsect,
2614e998e519SSheshadri Vasudevan tg_cookie) != 0))) {
2615aa1b14e7SSheshadri Vasudevan #endif
26163ccda647Slclee uidx = i;
26173ccda647Slclee solaris_offset = relsect;
26183ccda647Slclee solaris_size = numsect;
2619aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2620aa1b14e7SSheshadri Vasudevan }
2621aa1b14e7SSheshadri Vasudevan #endif
26223ccda647Slclee }
26233ccda647Slclee }
2624aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
2625aa1b14e7SSheshadri Vasudevan if (ld_count < cl->cl_logical_drive_count) {
2626aa1b14e7SSheshadri Vasudevan /*
2627aa1b14e7SSheshadri Vasudevan * Some/all logical drives were deleted. Clear out
2628aa1b14e7SSheshadri Vasudevan * the fmap entries correspoding to those deleted drives.
2629aa1b14e7SSheshadri Vasudevan */
2630aa1b14e7SSheshadri Vasudevan for (k = ld_count + FD_NUMPART;
2631aa1b14e7SSheshadri Vasudevan k < cl->cl_logical_drive_count + FD_NUMPART; k++) {
2632aa1b14e7SSheshadri Vasudevan cl->cl_fmap[k].fmap_start = 0;
2633aa1b14e7SSheshadri Vasudevan cl->cl_fmap[k].fmap_nblk = 0;
2634aa1b14e7SSheshadri Vasudevan cl->cl_fmap[k].fmap_systid = 0;
2635aa1b14e7SSheshadri Vasudevan }
2636aa1b14e7SSheshadri Vasudevan cl->cl_update_ext_minor_nodes = 1;
2637aa1b14e7SSheshadri Vasudevan }
2638aa1b14e7SSheshadri Vasudevan if (cl->cl_update_ext_minor_nodes) {
2639aa1b14e7SSheshadri Vasudevan rval = cmlb_update_ext_minor_nodes(cl, ld_count);
2640aa1b14e7SSheshadri Vasudevan if (rval != 0) {
2641aa1b14e7SSheshadri Vasudevan goto done;
2642aa1b14e7SSheshadri Vasudevan }
2643aa1b14e7SSheshadri Vasudevan }
2644aa1b14e7SSheshadri Vasudevan #endif
2645e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, "fdisk 0x%x 0x%lx",
2646e8fb11a1Sshidokht cl->cl_solaris_offset, cl->cl_solaris_size);
26473ccda647Slclee done:
26483ccda647Slclee
26493ccda647Slclee /*
26503ccda647Slclee * Clear the VTOC info, only if the Solaris partition entry
26513ccda647Slclee * has moved, changed size, been deleted, or if the size of
26523ccda647Slclee * the partition is too small to even fit the label sector.
26533ccda647Slclee */
2654e8fb11a1Sshidokht if ((cl->cl_solaris_offset != solaris_offset) ||
2655e8fb11a1Sshidokht (cl->cl_solaris_size != solaris_size) ||
26563ccda647Slclee solaris_size <= DK_LABEL_LOC) {
2657e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, "fdisk moved 0x%x 0x%lx",
26583ccda647Slclee solaris_offset, solaris_size);
2659e8fb11a1Sshidokht bzero(&cl->cl_g, sizeof (struct dk_geom));
2660e8fb11a1Sshidokht bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2661e8fb11a1Sshidokht bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
26627f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
26633ccda647Slclee }
2664e8fb11a1Sshidokht cl->cl_solaris_offset = solaris_offset;
2665e8fb11a1Sshidokht cl->cl_solaris_size = solaris_size;
26663ccda647Slclee kmem_free(bufp, blocksize);
26673ccda647Slclee return (rval);
26683ccda647Slclee
26693ccda647Slclee #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */
26703ccda647Slclee #error "fdisk table presence undetermined for this platform."
26713ccda647Slclee #endif /* #if defined(_NO_FDISK_PRESENT) */
26723ccda647Slclee }
26733ccda647Slclee
26743ccda647Slclee static void
26753ccda647Slclee cmlb_swap_efi_gpt(efi_gpt_t *e)
26763ccda647Slclee {
26773ccda647Slclee _NOTE(ASSUMING_PROTECTED(*e))
26783ccda647Slclee e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
26793ccda647Slclee e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
26803ccda647Slclee e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
26813ccda647Slclee e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
26823ccda647Slclee e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
26833ccda647Slclee e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
26843ccda647Slclee e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
26853ccda647Slclee e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
26863ccda647Slclee UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
26873ccda647Slclee e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
26883ccda647Slclee e->efi_gpt_NumberOfPartitionEntries =
26893ccda647Slclee LE_32(e->efi_gpt_NumberOfPartitionEntries);
26903ccda647Slclee e->efi_gpt_SizeOfPartitionEntry =
26913ccda647Slclee LE_32(e->efi_gpt_SizeOfPartitionEntry);
26923ccda647Slclee e->efi_gpt_PartitionEntryArrayCRC32 =
26933ccda647Slclee LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
26943ccda647Slclee }
26953ccda647Slclee
26963ccda647Slclee static void
26973ccda647Slclee cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
26983ccda647Slclee {
26993ccda647Slclee int i;
27003ccda647Slclee
27013ccda647Slclee _NOTE(ASSUMING_PROTECTED(*p))
27023ccda647Slclee for (i = 0; i < nparts; i++) {
27033ccda647Slclee UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
27043ccda647Slclee p[i].efi_gpe_PartitionTypeGUID);
27053ccda647Slclee p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
27063ccda647Slclee p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
27073ccda647Slclee /* PartitionAttrs */
27083ccda647Slclee }
27093ccda647Slclee }
27103ccda647Slclee
27113ccda647Slclee static int
27123ccda647Slclee cmlb_validate_efi(efi_gpt_t *labp)
27133ccda647Slclee {
27143ccda647Slclee if (labp->efi_gpt_Signature != EFI_SIGNATURE)
27153ccda647Slclee return (EINVAL);
27163ccda647Slclee /* at least 96 bytes in this version of the spec. */
27173ccda647Slclee if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
27183ccda647Slclee labp->efi_gpt_HeaderSize)
27193ccda647Slclee return (EINVAL);
27203ccda647Slclee /* this should be 128 bytes */
27213ccda647Slclee if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
27223ccda647Slclee return (EINVAL);
27233ccda647Slclee return (0);
27243ccda647Slclee }
27253ccda647Slclee
2726ab92f716Sshidokht /*
27277f0b8309SEdward Pilatowicz * This function returns B_FALSE if there is a valid MBR signature and no
27287f0b8309SEdward Pilatowicz * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2729ab92f716Sshidokht *
2730ab92f716Sshidokht * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2731ab92f716Sshidokht * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2732ab92f716Sshidokht * where a PMBR entry is not the only one. Also, if the first block has been
2733ab92f716Sshidokht * corrupted, currently best attempt to allow data access would be to try to
2734ab92f716Sshidokht * check for GPT headers. Hence in case of more than one partition entry, but
2735ab92f716Sshidokht * at least one EFI_PMBR partition type or no valid magic number, the function
27367f0b8309SEdward Pilatowicz * returns B_TRUE to continue with looking for GPT header.
2737ab92f716Sshidokht */
2738ab92f716Sshidokht
27397f0b8309SEdward Pilatowicz static boolean_t
27407f0b8309SEdward Pilatowicz cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2741ab92f716Sshidokht {
2742ab92f716Sshidokht struct ipart *fdp;
2743ab92f716Sshidokht struct mboot *mbp = (struct mboot *)buf;
2744ab92f716Sshidokht struct ipart fdisk[FD_NUMPART];
2745ab92f716Sshidokht int i;
2746ab92f716Sshidokht
2747342440ecSPrasad Singamsetty if (is_mbr != NULL)
27487f0b8309SEdward Pilatowicz *is_mbr = B_TRUE;
2749342440ecSPrasad Singamsetty
2750342440ecSPrasad Singamsetty if (LE_16(mbp->signature) != MBB_MAGIC) {
2751342440ecSPrasad Singamsetty if (is_mbr != NULL)
27527f0b8309SEdward Pilatowicz *is_mbr = B_FALSE;
27537f0b8309SEdward Pilatowicz return (B_TRUE);
2754342440ecSPrasad Singamsetty }
2755ab92f716Sshidokht
2756ab92f716Sshidokht bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2757ab92f716Sshidokht
2758ab92f716Sshidokht for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2759ab92f716Sshidokht if (fdp->systid == EFI_PMBR)
27607f0b8309SEdward Pilatowicz return (B_TRUE);
2761ab92f716Sshidokht }
2762ab92f716Sshidokht
27637f0b8309SEdward Pilatowicz return (B_FALSE);
2764ab92f716Sshidokht }
2765ab92f716Sshidokht
27663ccda647Slclee static int
2767e8fb11a1Sshidokht cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2768e8fb11a1Sshidokht void *tg_cookie)
27693ccda647Slclee {
27703ccda647Slclee int i;
27713ccda647Slclee int rval = 0;
27723ccda647Slclee efi_gpe_t *partitions;
27733ccda647Slclee uchar_t *buf;
27743ccda647Slclee uint_t lbasize; /* is really how much to read */
2775e8fb11a1Sshidokht diskaddr_t cap = 0;
27763ccda647Slclee uint_t nparts;
27773ccda647Slclee diskaddr_t gpe_lba;
2778af007057Syl194034 diskaddr_t alternate_lba;
27791cec2d8cSshidokht int iofailed = 0;
2780e8fb11a1Sshidokht struct uuid uuid_type_reserved = EFI_RESERVED;
2781342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
27827f0b8309SEdward Pilatowicz boolean_t is_mbr;
2783342440ecSPrasad Singamsetty #endif
27843ccda647Slclee
2785e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
27863ccda647Slclee
2787e8fb11a1Sshidokht lbasize = cl->cl_sys_blocksize;
2788e8fb11a1Sshidokht
2789e8fb11a1Sshidokht cl->cl_reserved = -1;
2790e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
27913ccda647Slclee
27923ccda647Slclee buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
27933ccda647Slclee
2794e8fb11a1Sshidokht rval = DK_TG_READ(cl, buf, 0, lbasize, tg_cookie);
27953ccda647Slclee if (rval) {
27961cec2d8cSshidokht iofailed = 1;
27973ccda647Slclee goto done_err;
27983ccda647Slclee }
27993ccda647Slclee if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
28003ccda647Slclee /* not ours */
28013ccda647Slclee rval = ESRCH;
28023ccda647Slclee goto done_err;
28033ccda647Slclee }
28043ccda647Slclee
2805342440ecSPrasad Singamsetty #if defined(_FIRMWARE_NEEDS_FDISK)
28067f0b8309SEdward Pilatowicz if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
28077f0b8309SEdward Pilatowicz if (is_mbr)
2808342440ecSPrasad Singamsetty rval = ESRCH;
2809342440ecSPrasad Singamsetty else
2810ab92f716Sshidokht rval = EINVAL;
2811ab92f716Sshidokht goto done_err;
2812ab92f716Sshidokht }
2813342440ecSPrasad Singamsetty #else
28147f0b8309SEdward Pilatowicz if (!cmlb_check_efi_mbr(buf, NULL)) {
2815342440ecSPrasad Singamsetty rval = EINVAL;
2816342440ecSPrasad Singamsetty goto done_err;
2817342440ecSPrasad Singamsetty }
2818342440ecSPrasad Singamsetty
2819342440ecSPrasad Singamsetty #endif
2820ab92f716Sshidokht
2821e8fb11a1Sshidokht rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
28223ccda647Slclee if (rval) {
28231cec2d8cSshidokht iofailed = 1;
28243ccda647Slclee goto done_err;
28253ccda647Slclee }
28263ccda647Slclee cmlb_swap_efi_gpt((efi_gpt_t *)buf);
28273ccda647Slclee
28283ccda647Slclee if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
28293ccda647Slclee /*
28303ccda647Slclee * Couldn't read the primary, try the backup. Our
28313ccda647Slclee * capacity at this point could be based on CHS, so
28323ccda647Slclee * check what the device reports.
28333ccda647Slclee */
2834e8fb11a1Sshidokht rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
28353ccda647Slclee if (rval) {
28361cec2d8cSshidokht iofailed = 1;
28373ccda647Slclee goto done_err;
28383ccda647Slclee }
2839e8fb11a1Sshidokht
2840e8fb11a1Sshidokht /*
2841e8fb11a1Sshidokht * CMLB_OFF_BY_ONE case, we check the next to last block first
2842e8fb11a1Sshidokht * for backup GPT header, otherwise check the last block.
2843e8fb11a1Sshidokht */
2844e8fb11a1Sshidokht
2845e8fb11a1Sshidokht if ((rval = DK_TG_READ(cl, buf,
2846e8fb11a1Sshidokht cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2847e8fb11a1Sshidokht lbasize, tg_cookie))
2848e8fb11a1Sshidokht != 0) {
28491cec2d8cSshidokht iofailed = 1;
28503ccda647Slclee goto done_err;
28513ccda647Slclee }
28523ccda647Slclee cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2853e8fb11a1Sshidokht
2854e8fb11a1Sshidokht if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2855e8fb11a1Sshidokht
2856e8fb11a1Sshidokht if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2857e8fb11a1Sshidokht goto done_err;
2858e8fb11a1Sshidokht if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2859e8fb11a1Sshidokht tg_cookie)) != 0)
2860e8fb11a1Sshidokht goto done_err;
2861e8fb11a1Sshidokht cmlb_swap_efi_gpt((efi_gpt_t *)buf);
28623ccda647Slclee if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
28633ccda647Slclee goto done_err;
2864e8fb11a1Sshidokht }
2865e8fb11a1Sshidokht if (!(flags & CMLB_SILENT))
2866e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
28673ccda647Slclee "primary label corrupt; using backup\n");
28683ccda647Slclee }
28693ccda647Slclee
28703ccda647Slclee nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
28713ccda647Slclee gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2872af007057Syl194034 alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
28733ccda647Slclee
2874e8fb11a1Sshidokht rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
28753ccda647Slclee if (rval) {
28761cec2d8cSshidokht iofailed = 1;
28773ccda647Slclee goto done_err;
28783ccda647Slclee }
28793ccda647Slclee partitions = (efi_gpe_t *)buf;
28803ccda647Slclee
28813ccda647Slclee if (nparts > MAXPART) {
28823ccda647Slclee nparts = MAXPART;
28833ccda647Slclee }
28843ccda647Slclee cmlb_swap_efi_gpe(nparts, partitions);
28853ccda647Slclee
2886e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
28873ccda647Slclee
28883ccda647Slclee /* Fill in partition table. */
28893ccda647Slclee for (i = 0; i < nparts; i++) {
28903ccda647Slclee if (partitions->efi_gpe_StartingLBA != 0 ||
28913ccda647Slclee partitions->efi_gpe_EndingLBA != 0) {
2892e8fb11a1Sshidokht cl->cl_map[i].dkl_cylno =
28933ccda647Slclee partitions->efi_gpe_StartingLBA;
2894e8fb11a1Sshidokht cl->cl_map[i].dkl_nblk =
28953ccda647Slclee partitions->efi_gpe_EndingLBA -
28963ccda647Slclee partitions->efi_gpe_StartingLBA + 1;
2897e8fb11a1Sshidokht cl->cl_offset[i] =
28983ccda647Slclee partitions->efi_gpe_StartingLBA;
28993ccda647Slclee }
2900e8fb11a1Sshidokht
2901e8fb11a1Sshidokht if (cl->cl_reserved == -1) {
2902e8fb11a1Sshidokht if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2903e8fb11a1Sshidokht &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2904e8fb11a1Sshidokht cl->cl_reserved = i;
2905e8fb11a1Sshidokht }
2906e8fb11a1Sshidokht }
29073ccda647Slclee if (i == WD_NODE) {
29083ccda647Slclee /*
29093ccda647Slclee * minor number 7 corresponds to the whole disk
2910af007057Syl194034 * if the disk capacity is expanded after disk is
2911af007057Syl194034 * labeled, minor number 7 represents the capacity
2912af007057Syl194034 * indicated by the disk label.
29133ccda647Slclee */
2914e8fb11a1Sshidokht cl->cl_map[i].dkl_cylno = 0;
2915af007057Syl194034 if (alternate_lba == 1) {
2916af007057Syl194034 /*
2917af007057Syl194034 * We are using backup label. Since we can
2918af007057Syl194034 * find a valid label at the end of disk,
2919af007057Syl194034 * the disk capacity is not expanded.
2920af007057Syl194034 */
2921e8fb11a1Sshidokht cl->cl_map[i].dkl_nblk = capacity;
2922af007057Syl194034 } else {
2923af007057Syl194034 cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2924af007057Syl194034 }
2925e8fb11a1Sshidokht cl->cl_offset[i] = 0;
29263ccda647Slclee }
29273ccda647Slclee partitions++;
29283ccda647Slclee }
2929e8fb11a1Sshidokht cl->cl_solaris_offset = 0;
2930e8fb11a1Sshidokht cl->cl_solaris_size = capacity;
2931342440ecSPrasad Singamsetty cl->cl_label_from_media = CMLB_LABEL_EFI;
29327f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
2933e8fb11a1Sshidokht
2934e8fb11a1Sshidokht /* clear the vtoc label */
2935e8fb11a1Sshidokht bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2936e8fb11a1Sshidokht
29373ccda647Slclee kmem_free(buf, EFI_MIN_ARRAY_SIZE);
29383ccda647Slclee return (0);
29393ccda647Slclee
29403ccda647Slclee done_err:
29413ccda647Slclee kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2942e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
2943e8fb11a1Sshidokht done_err1:
29443ccda647Slclee /*
29453ccda647Slclee * if we didn't find something that could look like a VTOC
29463ccda647Slclee * and the disk is over 1TB, we know there isn't a valid label.
29473ccda647Slclee * Otherwise let cmlb_uselabel decide what to do. We only
29483ccda647Slclee * want to invalidate this if we're certain the label isn't
29493ccda647Slclee * valid because cmlb_prop_op will now fail, which in turn
29503ccda647Slclee * causes things like opens and stats on the partition to fail.
29513ccda647Slclee */
2952342440ecSPrasad Singamsetty if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
29537f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
29543ccda647Slclee }
29553ccda647Slclee return (rval);
29563ccda647Slclee }
29573ccda647Slclee
29583ccda647Slclee
29593ccda647Slclee /*
29603ccda647Slclee * Function: cmlb_uselabel
29613ccda647Slclee *
29623ccda647Slclee * Description: Validate the disk label and update the relevant data (geometry,
29633ccda647Slclee * partition, vtoc, and capacity data) in the cmlb_lun struct.
29643ccda647Slclee * Marks the geometry of the unit as being valid.
29653ccda647Slclee *
2966e8fb11a1Sshidokht * Arguments: cl: unit struct.
29673ccda647Slclee * dk_label: disk label
29683ccda647Slclee *
29693ccda647Slclee * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
29703ccda647Slclee * partition, vtoc, and capacity data are good.
29713ccda647Slclee *
29723ccda647Slclee * CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
29733ccda647Slclee * label; or computed capacity does not jibe with capacity
29743ccda647Slclee * reported from the READ CAPACITY command.
29753ccda647Slclee *
29763ccda647Slclee * Context: Kernel thread only (can sleep).
29773ccda647Slclee */
29783ccda647Slclee static int
2979e8fb11a1Sshidokht cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
29803ccda647Slclee {
29813ccda647Slclee short *sp;
29823ccda647Slclee short sum;
29833ccda647Slclee short count;
29843ccda647Slclee int label_error = CMLB_LABEL_IS_VALID;
29853ccda647Slclee int i;
29863ccda647Slclee diskaddr_t label_capacity;
2987342440ecSPrasad Singamsetty uint32_t part_end;
29883ccda647Slclee diskaddr_t track_capacity;
29893ccda647Slclee #if defined(_SUNOS_VTOC_16)
29903ccda647Slclee struct dkl_partition *vpartp;
29913ccda647Slclee #endif
2992e8fb11a1Sshidokht ASSERT(cl != NULL);
2993e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
29943ccda647Slclee
29953ccda647Slclee /* Validate the magic number of the label. */
29963ccda647Slclee if (labp->dkl_magic != DKL_MAGIC) {
29973ccda647Slclee #if defined(__sparc)
2998e8fb11a1Sshidokht if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
2999e8fb11a1Sshidokht if (!(flags & CMLB_SILENT))
3000e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3001e8fb11a1Sshidokht CE_WARN,
30023ccda647Slclee "Corrupt label; wrong magic number\n");
30033ccda647Slclee }
30043ccda647Slclee #endif
30053ccda647Slclee return (CMLB_LABEL_IS_INVALID);
30063ccda647Slclee }
30073ccda647Slclee
30083ccda647Slclee /* Validate the checksum of the label. */
30093ccda647Slclee sp = (short *)labp;
30103ccda647Slclee sum = 0;
30113ccda647Slclee count = sizeof (struct dk_label) / sizeof (short);
30123ccda647Slclee while (count--) {
30133ccda647Slclee sum ^= *sp++;
30143ccda647Slclee }
30153ccda647Slclee
30163ccda647Slclee if (sum != 0) {
30173ccda647Slclee #if defined(_SUNOS_VTOC_16)
3018e8fb11a1Sshidokht if (!ISCD(cl)) {
30193ccda647Slclee #elif defined(_SUNOS_VTOC_8)
3020e8fb11a1Sshidokht if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
30213ccda647Slclee #endif
3022e8fb11a1Sshidokht if (!(flags & CMLB_SILENT))
3023e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3024e8fb11a1Sshidokht CE_WARN,
30253ccda647Slclee "Corrupt label - label checksum failed\n");
30263ccda647Slclee }
30273ccda647Slclee return (CMLB_LABEL_IS_INVALID);
30283ccda647Slclee }
30293ccda647Slclee
30303ccda647Slclee
30313ccda647Slclee /*
30323ccda647Slclee * Fill in geometry structure with data from label.
30333ccda647Slclee */
3034e8fb11a1Sshidokht bzero(&cl->cl_g, sizeof (struct dk_geom));
3035e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = labp->dkl_ncyl;
3036e8fb11a1Sshidokht cl->cl_g.dkg_acyl = labp->dkl_acyl;
3037e8fb11a1Sshidokht cl->cl_g.dkg_bcyl = 0;
3038e8fb11a1Sshidokht cl->cl_g.dkg_nhead = labp->dkl_nhead;
3039e8fb11a1Sshidokht cl->cl_g.dkg_nsect = labp->dkl_nsect;
3040e8fb11a1Sshidokht cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
30413ccda647Slclee
30423ccda647Slclee #if defined(_SUNOS_VTOC_8)
3043e8fb11a1Sshidokht cl->cl_g.dkg_gap1 = labp->dkl_gap1;
3044e8fb11a1Sshidokht cl->cl_g.dkg_gap2 = labp->dkl_gap2;
3045e8fb11a1Sshidokht cl->cl_g.dkg_bhead = labp->dkl_bhead;
30463ccda647Slclee #endif
30473ccda647Slclee #if defined(_SUNOS_VTOC_16)
3048e8fb11a1Sshidokht cl->cl_dkg_skew = labp->dkl_skew;
30493ccda647Slclee #endif
30503ccda647Slclee
30513ccda647Slclee #if defined(__i386) || defined(__amd64)
3052e8fb11a1Sshidokht cl->cl_g.dkg_apc = labp->dkl_apc;
30533ccda647Slclee #endif
30543ccda647Slclee
30553ccda647Slclee /*
30563ccda647Slclee * Currently we rely on the values in the label being accurate. If
30573ccda647Slclee * dkl_rpm or dkl_pcly are zero in the label, use a default value.
30583ccda647Slclee *
30593ccda647Slclee * Note: In the future a MODE SENSE may be used to retrieve this data,
30603ccda647Slclee * although this command is optional in SCSI-2.
30613ccda647Slclee */
3062e8fb11a1Sshidokht cl->cl_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600;
3063e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
3064e8fb11a1Sshidokht (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
30653ccda647Slclee
30663ccda647Slclee /*
30673ccda647Slclee * The Read and Write reinstruct values may not be valid
30683ccda647Slclee * for older disks.
30693ccda647Slclee */
3070e8fb11a1Sshidokht cl->cl_g.dkg_read_reinstruct = labp->dkl_read_reinstruct;
3071e8fb11a1Sshidokht cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
30723ccda647Slclee
30733ccda647Slclee /* Fill in partition table. */
30743ccda647Slclee #if defined(_SUNOS_VTOC_8)
30753ccda647Slclee for (i = 0; i < NDKMAP; i++) {
3076e8fb11a1Sshidokht cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
3077e8fb11a1Sshidokht cl->cl_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk;
30783ccda647Slclee }
30793ccda647Slclee #endif
30803ccda647Slclee #if defined(_SUNOS_VTOC_16)
30813ccda647Slclee vpartp = labp->dkl_vtoc.v_part;
30823ccda647Slclee track_capacity = labp->dkl_nhead * labp->dkl_nsect;
30833ccda647Slclee
3084e8fb11a1Sshidokht /* Prevent divide by zero */
3085e8fb11a1Sshidokht if (track_capacity == 0) {
3086e8fb11a1Sshidokht if (!(flags & CMLB_SILENT))
3087e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3088e8fb11a1Sshidokht "Corrupt label - zero nhead or nsect value\n");
3089e8fb11a1Sshidokht
3090e8fb11a1Sshidokht return (CMLB_LABEL_IS_INVALID);
3091e8fb11a1Sshidokht }
3092e8fb11a1Sshidokht
30933ccda647Slclee for (i = 0; i < NDKMAP; i++, vpartp++) {
3094e8fb11a1Sshidokht cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
3095e8fb11a1Sshidokht cl->cl_map[i].dkl_nblk = vpartp->p_size;
30963ccda647Slclee }
30973ccda647Slclee #endif
30983ccda647Slclee
30993ccda647Slclee /* Fill in VTOC Structure. */
3100e8fb11a1Sshidokht bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
31013ccda647Slclee #if defined(_SUNOS_VTOC_8)
31023ccda647Slclee /*
31033ccda647Slclee * The 8-slice vtoc does not include the ascii label; save it into
31043ccda647Slclee * the device's soft state structure here.
31053ccda647Slclee */
3106e8fb11a1Sshidokht bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
31073ccda647Slclee #endif
31083ccda647Slclee
31093ccda647Slclee /* Now look for a valid capacity. */
3110e8fb11a1Sshidokht track_capacity = (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
3111e8fb11a1Sshidokht label_capacity = (cl->cl_g.dkg_ncyl * track_capacity);
31123ccda647Slclee
3113e8fb11a1Sshidokht if (cl->cl_g.dkg_acyl) {
31143ccda647Slclee #if defined(__i386) || defined(__amd64)
31153ccda647Slclee /* we may have > 1 alts cylinder */
3116e8fb11a1Sshidokht label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
31173ccda647Slclee #else
31183ccda647Slclee label_capacity += track_capacity;
31193ccda647Slclee #endif
31203ccda647Slclee }
31213ccda647Slclee
31223ccda647Slclee /*
3123e8fb11a1Sshidokht * Force check here to ensure the computed capacity is valid.
3124e8fb11a1Sshidokht * If capacity is zero, it indicates an invalid label and
3125e8fb11a1Sshidokht * we should abort updating the relevant data then.
3126e8fb11a1Sshidokht */
3127e8fb11a1Sshidokht if (label_capacity == 0) {
3128e8fb11a1Sshidokht if (!(flags & CMLB_SILENT))
3129e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3130e8fb11a1Sshidokht "Corrupt label - no valid capacity could be "
3131e8fb11a1Sshidokht "retrieved\n");
3132e8fb11a1Sshidokht
3133e8fb11a1Sshidokht return (CMLB_LABEL_IS_INVALID);
3134e8fb11a1Sshidokht }
3135e8fb11a1Sshidokht
3136e8fb11a1Sshidokht /* Mark the geometry as valid. */
31377f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
3138e8fb11a1Sshidokht
3139e8fb11a1Sshidokht /*
31403ccda647Slclee * if we got invalidated when mutex exit and entered again,
31413ccda647Slclee * if blockcount different than when we came in, need to
31423ccda647Slclee * retry from beginning of cmlb_validate_geometry.
31433ccda647Slclee * revisit this on next phase of utilizing this for
31443ccda647Slclee * sd.
31453ccda647Slclee */
31463ccda647Slclee
3147e8fb11a1Sshidokht if (label_capacity <= cl->cl_blockcount) {
31483ccda647Slclee #if defined(_SUNOS_VTOC_8)
31493ccda647Slclee /*
31503ccda647Slclee * We can't let this happen on drives that are subdivided
31513ccda647Slclee * into logical disks (i.e., that have an fdisk table).
3152e8fb11a1Sshidokht * The cl_blockcount field should always hold the full media
31533ccda647Slclee * size in sectors, period. This code would overwrite
3154e8fb11a1Sshidokht * cl_blockcount with the size of the Solaris fdisk partition.
31553ccda647Slclee */
3156e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
31573ccda647Slclee "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3158e8fb11a1Sshidokht label_capacity, cl->cl_blockcount);
3159e8fb11a1Sshidokht cl->cl_solaris_size = label_capacity;
31603ccda647Slclee
31613ccda647Slclee #endif /* defined(_SUNOS_VTOC_8) */
31623ccda647Slclee goto done;
31633ccda647Slclee }
31643ccda647Slclee
3165e8fb11a1Sshidokht if (ISCD(cl)) {
31663ccda647Slclee /* For CDROMs, we trust that the data in the label is OK. */
31673ccda647Slclee #if defined(_SUNOS_VTOC_8)
31683ccda647Slclee for (i = 0; i < NDKMAP; i++) {
31693ccda647Slclee part_end = labp->dkl_nhead * labp->dkl_nsect *
31703ccda647Slclee labp->dkl_map[i].dkl_cylno +
31713ccda647Slclee labp->dkl_map[i].dkl_nblk - 1;
31723ccda647Slclee
31733ccda647Slclee if ((labp->dkl_map[i].dkl_nblk) &&
3174e8fb11a1Sshidokht (part_end > cl->cl_blockcount)) {
31757f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
31763ccda647Slclee break;
31773ccda647Slclee }
31783ccda647Slclee }
31793ccda647Slclee #endif
31803ccda647Slclee #if defined(_SUNOS_VTOC_16)
31813ccda647Slclee vpartp = &(labp->dkl_vtoc.v_part[0]);
31823ccda647Slclee for (i = 0; i < NDKMAP; i++, vpartp++) {
31833ccda647Slclee part_end = vpartp->p_start + vpartp->p_size;
31843ccda647Slclee if ((vpartp->p_size > 0) &&
3185e8fb11a1Sshidokht (part_end > cl->cl_blockcount)) {
31867f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
31873ccda647Slclee break;
31883ccda647Slclee }
31893ccda647Slclee }
31903ccda647Slclee #endif
31913ccda647Slclee } else {
3192e8fb11a1Sshidokht /* label_capacity > cl->cl_blockcount */
3193e8fb11a1Sshidokht if (!(flags & CMLB_SILENT)) {
3194e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
31953ccda647Slclee "Corrupt label - bad geometry\n");
3196e8fb11a1Sshidokht cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
31973ccda647Slclee "Label says %llu blocks; Drive says %llu blocks\n",
3198e8fb11a1Sshidokht label_capacity, cl->cl_blockcount);
3199e8fb11a1Sshidokht }
32007f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
32013ccda647Slclee label_error = CMLB_LABEL_IS_INVALID;
32023ccda647Slclee }
32033ccda647Slclee
32043ccda647Slclee done:
32053ccda647Slclee
3206e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, "cmlb_uselabel: (label geometry)\n");
3207e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
32083ccda647Slclee " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3209e8fb11a1Sshidokht cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3210e8fb11a1Sshidokht cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
32113ccda647Slclee
3212e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
32133ccda647Slclee " label_capacity: %d; intrlv: %d; rpm: %d\n",
3214e8fb11a1Sshidokht cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
3215e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl, " wrt_reinstr: %d; rd_reinstr: %d\n",
3216e8fb11a1Sshidokht cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
32173ccda647Slclee
3218e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
32193ccda647Slclee
32203ccda647Slclee return (label_error);
32213ccda647Slclee }
32223ccda647Slclee
32233ccda647Slclee
32243ccda647Slclee /*
32253ccda647Slclee * Function: cmlb_build_default_label
32263ccda647Slclee *
32273ccda647Slclee * Description: Generate a default label for those devices that do not have
32283ccda647Slclee * one, e.g., new media, removable cartridges, etc..
32293ccda647Slclee *
32303ccda647Slclee * Context: Kernel thread only
32313ccda647Slclee */
3232e8fb11a1Sshidokht /*ARGSUSED*/
32333ccda647Slclee static void
3234e8fb11a1Sshidokht cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
32353ccda647Slclee {
32363ccda647Slclee #if defined(_SUNOS_VTOC_16)
32373ccda647Slclee uint_t phys_spc;
32383ccda647Slclee uint_t disksize;
3239e8fb11a1Sshidokht struct dk_geom cl_g;
3240e8fb11a1Sshidokht diskaddr_t capacity;
32413ccda647Slclee #endif
32423ccda647Slclee
3243e8fb11a1Sshidokht ASSERT(cl != NULL);
3244e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
32453ccda647Slclee
32463ccda647Slclee #if defined(_SUNOS_VTOC_8)
32473ccda647Slclee /*
32483ccda647Slclee * Note: This is a legacy check for non-removable devices on VTOC_8
32493ccda647Slclee * only. This may be a valid check for VTOC_16 as well.
3250e8fb11a1Sshidokht * Once we understand why there is this difference between SPARC and
3251e8fb11a1Sshidokht * x86 platform, we could remove this legacy check.
32523ccda647Slclee */
3253e8fb11a1Sshidokht if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
32543ccda647Slclee return;
32553ccda647Slclee }
32563ccda647Slclee #endif
32573ccda647Slclee
3258e8fb11a1Sshidokht bzero(&cl->cl_g, sizeof (struct dk_geom));
3259e8fb11a1Sshidokht bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3260e8fb11a1Sshidokht bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
32613ccda647Slclee
32623ccda647Slclee #if defined(_SUNOS_VTOC_8)
32633ccda647Slclee
32643ccda647Slclee /*
32653ccda647Slclee * It's a REMOVABLE media, therefore no label (on sparc, anyway).
32663ccda647Slclee * But it is still necessary to set up various geometry information,
32673ccda647Slclee * and we are doing this here.
32683ccda647Slclee */
32693ccda647Slclee
32703ccda647Slclee /*
32713ccda647Slclee * For the rpm, we use the minimum for the disk. For the head, cyl,
32723ccda647Slclee * and number of sector per track, if the capacity <= 1GB, head = 64,
32733ccda647Slclee * sect = 32. else head = 255, sect 63 Note: the capacity should be
32743ccda647Slclee * equal to C*H*S values. This will cause some truncation of size due
32753ccda647Slclee * to round off errors. For CD-ROMs, this truncation can have adverse
32763ccda647Slclee * side effects, so returning ncyl and nhead as 1. The nsect will
32773ccda647Slclee * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
32783ccda647Slclee */
3279e8fb11a1Sshidokht cl->cl_solaris_size = cl->cl_blockcount;
3280e8fb11a1Sshidokht if (ISCD(cl)) {
32813ccda647Slclee tg_attribute_t tgattribute;
32823ccda647Slclee int is_writable;
32833ccda647Slclee /*
32843ccda647Slclee * Preserve the old behavior for non-writable
32853ccda647Slclee * medias. Since dkg_nsect is a ushort, it
32863ccda647Slclee * will lose bits as cdroms have more than
32873ccda647Slclee * 65536 sectors. So if we recalculate
32883ccda647Slclee * capacity, it will become much shorter.
32893ccda647Slclee * But the dkg_* information is not
32903ccda647Slclee * used for CDROMs so it is OK. But for
32913ccda647Slclee * Writable CDs we need this information
32923ccda647Slclee * to be valid (for newfs say). So we
32933ccda647Slclee * make nsect and nhead > 1 that way
32943ccda647Slclee * nsect can still stay within ushort limit
32953ccda647Slclee * without losing any bits.
32963ccda647Slclee */
32973ccda647Slclee
32983ccda647Slclee bzero(&tgattribute, sizeof (tg_attribute_t));
32993ccda647Slclee
3300e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
3301e8fb11a1Sshidokht is_writable =
3302e8fb11a1Sshidokht (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
33033ccda647Slclee tgattribute.media_is_writable : 1;
3304e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
33053ccda647Slclee
33063ccda647Slclee if (is_writable) {
3307e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 64;
3308e8fb11a1Sshidokht cl->cl_g.dkg_nsect = 32;
3309e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3310342440ecSPrasad Singamsetty cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
3311e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
33123ccda647Slclee } else {
3313e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = 1;
3314e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 1;
3315e8fb11a1Sshidokht cl->cl_g.dkg_nsect = cl->cl_blockcount;
33163ccda647Slclee }
33173ccda647Slclee } else {
3318c31fac72SShidokht Yadegari if (cl->cl_blockcount < 160) {
3319c31fac72SShidokht Yadegari /* Less than 80K */
3320c31fac72SShidokht Yadegari cl->cl_g.dkg_nhead = 1;
3321c31fac72SShidokht Yadegari cl->cl_g.dkg_ncyl = cl->cl_blockcount;
3322c31fac72SShidokht Yadegari cl->cl_g.dkg_nsect = 1;
3323c31fac72SShidokht Yadegari } else if (cl->cl_blockcount <= 0x1000) {
33243ccda647Slclee /* unlabeled SCSI floppy device */
3325e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 2;
3326e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = 80;
3327e8fb11a1Sshidokht cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
3328e8fb11a1Sshidokht } else if (cl->cl_blockcount <= 0x200000) {
3329e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 64;
3330e8fb11a1Sshidokht cl->cl_g.dkg_nsect = 32;
3331e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
33323ccda647Slclee } else {
3333e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 255;
333498a65dd4Sshidokht
333598a65dd4Sshidokht cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
333698a65dd4Sshidokht (UINT16_MAX * 255 * 63) - 1) /
333798a65dd4Sshidokht (UINT16_MAX * 255 * 63)) * 63;
333898a65dd4Sshidokht
333998a65dd4Sshidokht if (cl->cl_g.dkg_nsect == 0)
334098a65dd4Sshidokht cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
334198a65dd4Sshidokht
334298a65dd4Sshidokht cl->cl_g.dkg_ncyl = cl->cl_blockcount /
334398a65dd4Sshidokht (255 * cl->cl_g.dkg_nsect);
33443ccda647Slclee }
334598a65dd4Sshidokht
3346e8fb11a1Sshidokht cl->cl_solaris_size =
3347342440ecSPrasad Singamsetty (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
3348342440ecSPrasad Singamsetty cl->cl_g.dkg_nsect;
33493ccda647Slclee
33503ccda647Slclee }
33513ccda647Slclee
3352e8fb11a1Sshidokht cl->cl_g.dkg_acyl = 0;
3353e8fb11a1Sshidokht cl->cl_g.dkg_bcyl = 0;
3354e8fb11a1Sshidokht cl->cl_g.dkg_rpm = 200;
3355e8fb11a1Sshidokht cl->cl_asciilabel[0] = '\0';
3356e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl;
33573ccda647Slclee
3358e8fb11a1Sshidokht cl->cl_map[0].dkl_cylno = 0;
3359e8fb11a1Sshidokht cl->cl_map[0].dkl_nblk = cl->cl_solaris_size;
33603ccda647Slclee
3361e8fb11a1Sshidokht cl->cl_map[2].dkl_cylno = 0;
3362e8fb11a1Sshidokht cl->cl_map[2].dkl_nblk = cl->cl_solaris_size;
33633ccda647Slclee
33643ccda647Slclee #elif defined(_SUNOS_VTOC_16)
33653ccda647Slclee
3366e8fb11a1Sshidokht if (cl->cl_solaris_size == 0) {
33673ccda647Slclee /*
33683ccda647Slclee * Got fdisk table but no solaris entry therefore
33693ccda647Slclee * don't create a default label
33703ccda647Slclee */
33717f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
33723ccda647Slclee return;
33733ccda647Slclee }
33743ccda647Slclee
33753ccda647Slclee /*
33763ccda647Slclee * For CDs we continue to use the physical geometry to calculate
33773ccda647Slclee * number of cylinders. All other devices must convert the
33783ccda647Slclee * physical geometry (cmlb_geom) to values that will fit
33793ccda647Slclee * in a dk_geom structure.
33803ccda647Slclee */
3381e8fb11a1Sshidokht if (ISCD(cl)) {
3382e8fb11a1Sshidokht phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
33833ccda647Slclee } else {
33843ccda647Slclee /* Convert physical geometry to disk geometry */
3385e8fb11a1Sshidokht bzero(&cl_g, sizeof (struct dk_geom));
3386e8fb11a1Sshidokht
3387e8fb11a1Sshidokht /*
3388e8fb11a1Sshidokht * Refer to comments related to off-by-1 at the
3389e8fb11a1Sshidokht * header of this file.
3390b9ccdc5aScth * Before calculating geometry, capacity should be
3391e8fb11a1Sshidokht * decreased by 1.
3392e8fb11a1Sshidokht */
3393e8fb11a1Sshidokht
3394e8fb11a1Sshidokht if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
3395e8fb11a1Sshidokht capacity = cl->cl_blockcount - 1;
3396e8fb11a1Sshidokht else
3397e8fb11a1Sshidokht capacity = cl->cl_blockcount;
3398e8fb11a1Sshidokht
3399e8fb11a1Sshidokht
3400b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cmlb_convert_geometry(cl, capacity, &cl_g, tg_cookie);
3401e8fb11a1Sshidokht bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
3402e8fb11a1Sshidokht phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
34033ccda647Slclee }
34043ccda647Slclee
34057501b6d5SSheshadri Vasudevan if (phys_spc == 0)
34067501b6d5SSheshadri Vasudevan return;
3407e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
3408843e1988Sjohnlev if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3409843e1988Sjohnlev /* disable devid */
3410843e1988Sjohnlev cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
3411843e1988Sjohnlev disksize = cl->cl_solaris_size;
3412843e1988Sjohnlev } else {
3413e8fb11a1Sshidokht cl->cl_g.dkg_acyl = DK_ACYL;
3414e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
3415e8fb11a1Sshidokht disksize = cl->cl_g.dkg_ncyl * phys_spc;
3416843e1988Sjohnlev }
34173ccda647Slclee
3418e8fb11a1Sshidokht if (ISCD(cl)) {
34193ccda647Slclee /*
34203ccda647Slclee * CD's don't use the "heads * sectors * cyls"-type of
34213ccda647Slclee * geometry, but instead use the entire capacity of the media.
34223ccda647Slclee */
3423e8fb11a1Sshidokht disksize = cl->cl_solaris_size;
3424e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 1;
3425e8fb11a1Sshidokht cl->cl_g.dkg_nsect = 1;
3426e8fb11a1Sshidokht cl->cl_g.dkg_rpm =
3427e8fb11a1Sshidokht (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
34283ccda647Slclee
3429e8fb11a1Sshidokht cl->cl_vtoc.v_part[0].p_start = 0;
3430e8fb11a1Sshidokht cl->cl_vtoc.v_part[0].p_size = disksize;
3431e8fb11a1Sshidokht cl->cl_vtoc.v_part[0].p_tag = V_BACKUP;
3432e8fb11a1Sshidokht cl->cl_vtoc.v_part[0].p_flag = V_UNMNT;
34333ccda647Slclee
3434e8fb11a1Sshidokht cl->cl_map[0].dkl_cylno = 0;
3435e8fb11a1Sshidokht cl->cl_map[0].dkl_nblk = disksize;
3436e8fb11a1Sshidokht cl->cl_offset[0] = 0;
34373ccda647Slclee
34383ccda647Slclee } else {
34393ccda647Slclee /*
34403ccda647Slclee * Hard disks and removable media cartridges
34413ccda647Slclee */
3442e8fb11a1Sshidokht cl->cl_g.dkg_rpm =
3443e8fb11a1Sshidokht (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
3444e8fb11a1Sshidokht cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
34453ccda647Slclee
34463ccda647Slclee /* Add boot slice */
3447e8fb11a1Sshidokht cl->cl_vtoc.v_part[8].p_start = 0;
3448e8fb11a1Sshidokht cl->cl_vtoc.v_part[8].p_size = phys_spc;
3449e8fb11a1Sshidokht cl->cl_vtoc.v_part[8].p_tag = V_BOOT;
3450e8fb11a1Sshidokht cl->cl_vtoc.v_part[8].p_flag = V_UNMNT;
34513ccda647Slclee
3452e8fb11a1Sshidokht cl->cl_map[8].dkl_cylno = 0;
3453e8fb11a1Sshidokht cl->cl_map[8].dkl_nblk = phys_spc;
3454e8fb11a1Sshidokht cl->cl_offset[8] = 0;
34553ccda647Slclee
3456e8fb11a1Sshidokht if ((cl->cl_alter_behavior &
34573ccda647Slclee CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
3458e8fb11a1Sshidokht cl->cl_device_type == DTYPE_DIRECT) {
3459e8fb11a1Sshidokht cl->cl_vtoc.v_part[9].p_start = phys_spc;
3460e8fb11a1Sshidokht cl->cl_vtoc.v_part[9].p_size = 2 * phys_spc;
3461e8fb11a1Sshidokht cl->cl_vtoc.v_part[9].p_tag = V_ALTSCTR;
3462e8fb11a1Sshidokht cl->cl_vtoc.v_part[9].p_flag = 0;
34633ccda647Slclee
3464e8fb11a1Sshidokht cl->cl_map[9].dkl_cylno = 1;
3465e8fb11a1Sshidokht cl->cl_map[9].dkl_nblk = 2 * phys_spc;
3466e8fb11a1Sshidokht cl->cl_offset[9] = phys_spc;
34673ccda647Slclee }
34683ccda647Slclee }
34693ccda647Slclee
3470e8fb11a1Sshidokht cl->cl_g.dkg_apc = 0;
34713ccda647Slclee
34723ccda647Slclee /* Add backup slice */
3473e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_start = 0;
3474e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_size = disksize;
3475e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
3476e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
34773ccda647Slclee
3478e8fb11a1Sshidokht cl->cl_map[2].dkl_cylno = 0;
3479e8fb11a1Sshidokht cl->cl_map[2].dkl_nblk = disksize;
3480e8fb11a1Sshidokht cl->cl_offset[2] = 0;
34813ccda647Slclee
3482843e1988Sjohnlev /*
3483843e1988Sjohnlev * single slice (s0) covering the entire disk
3484843e1988Sjohnlev */
3485843e1988Sjohnlev if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3486843e1988Sjohnlev cl->cl_vtoc.v_part[0].p_start = 0;
3487843e1988Sjohnlev cl->cl_vtoc.v_part[0].p_tag = V_UNASSIGNED;
3488843e1988Sjohnlev cl->cl_vtoc.v_part[0].p_flag = 0;
3489843e1988Sjohnlev cl->cl_vtoc.v_part[0].p_size = disksize;
3490843e1988Sjohnlev cl->cl_map[0].dkl_cylno = 0;
3491843e1988Sjohnlev cl->cl_map[0].dkl_nblk = disksize;
3492843e1988Sjohnlev cl->cl_offset[0] = 0;
3493843e1988Sjohnlev }
3494843e1988Sjohnlev
3495e8fb11a1Sshidokht (void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
3496e8fb11a1Sshidokht " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3497e8fb11a1Sshidokht cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
34983ccda647Slclee
34993ccda647Slclee #else
35003ccda647Slclee #error "No VTOC format defined."
35013ccda647Slclee #endif
35023ccda647Slclee
3503e8fb11a1Sshidokht cl->cl_g.dkg_read_reinstruct = 0;
3504e8fb11a1Sshidokht cl->cl_g.dkg_write_reinstruct = 0;
35053ccda647Slclee
3506e8fb11a1Sshidokht cl->cl_g.dkg_intrlv = 1;
35073ccda647Slclee
3508e8fb11a1Sshidokht cl->cl_vtoc.v_sanity = VTOC_SANE;
3509c813bb04SGabriel Carrillo cl->cl_vtoc.v_nparts = V_NUMPAR;
3510c813bb04SGabriel Carrillo cl->cl_vtoc.v_version = V_VERSION;
35113ccda647Slclee
35127f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
3513342440ecSPrasad Singamsetty cl->cl_label_from_media = CMLB_LABEL_UNDEF;
35143ccda647Slclee
3515e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
35163ccda647Slclee "cmlb_build_default_label: Default label created: "
35173ccda647Slclee "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3518e8fb11a1Sshidokht cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
3519e8fb11a1Sshidokht cl->cl_g.dkg_nsect, cl->cl_blockcount);
35203ccda647Slclee }
35213ccda647Slclee
35223ccda647Slclee
35233ccda647Slclee #if defined(_FIRMWARE_NEEDS_FDISK)
35243ccda647Slclee /*
35253ccda647Slclee * Max CHS values, as they are encoded into bytes, for 1022/254/63
35263ccda647Slclee */
35273ccda647Slclee #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2))
35283ccda647Slclee #define LBA_MAX_CYL (1022 & 0xFF)
35293ccda647Slclee #define LBA_MAX_HEAD (254)
35303ccda647Slclee
35313ccda647Slclee
35323ccda647Slclee /*
35333ccda647Slclee * Function: cmlb_has_max_chs_vals
35343ccda647Slclee *
35357f0b8309SEdward Pilatowicz * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
35363ccda647Slclee *
35373ccda647Slclee * Arguments: fdp - ptr to CHS info
35383ccda647Slclee *
35393ccda647Slclee * Return Code: True or false
35403ccda647Slclee *
35413ccda647Slclee * Context: Any.
35423ccda647Slclee */
35437f0b8309SEdward Pilatowicz static boolean_t
35443ccda647Slclee cmlb_has_max_chs_vals(struct ipart *fdp)
35453ccda647Slclee {
35463ccda647Slclee return ((fdp->begcyl == LBA_MAX_CYL) &&
35473ccda647Slclee (fdp->beghead == LBA_MAX_HEAD) &&
35483ccda647Slclee (fdp->begsect == LBA_MAX_SECT) &&
35493ccda647Slclee (fdp->endcyl == LBA_MAX_CYL) &&
35503ccda647Slclee (fdp->endhead == LBA_MAX_HEAD) &&
35513ccda647Slclee (fdp->endsect == LBA_MAX_SECT));
35523ccda647Slclee }
35533ccda647Slclee #endif
35543ccda647Slclee
35553ccda647Slclee /*
35563ccda647Slclee * Function: cmlb_dkio_get_geometry
35573ccda647Slclee *
35583ccda647Slclee * Description: This routine is the driver entry point for handling user
35593ccda647Slclee * requests to get the device geometry (DKIOCGGEOM).
35603ccda647Slclee *
35613ccda647Slclee * Arguments:
3562e8fb11a1Sshidokht * arg pointer to user provided dk_geom structure specifying
35633ccda647Slclee * the controller's notion of the current geometry.
3564e8fb11a1Sshidokht *
3565e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
35663ccda647Slclee * directly from the mode argument of ioctl().
35673ccda647Slclee *
3568e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
3569e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
3570e8fb11a1Sshidokht *
35713ccda647Slclee * Return Code: 0
35723ccda647Slclee * EFAULT
35733ccda647Slclee * ENXIO
35743ccda647Slclee * EIO
35753ccda647Slclee */
35763ccda647Slclee static int
3577e8fb11a1Sshidokht cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
3578e8fb11a1Sshidokht void *tg_cookie)
35793ccda647Slclee {
35803ccda647Slclee struct dk_geom *tmp_geom = NULL;
35813ccda647Slclee int rval = 0;
35823ccda647Slclee
35833ccda647Slclee /*
35843ccda647Slclee * cmlb_validate_geometry does not spin a disk up
3585e8fb11a1Sshidokht * if it was spcl down. We need to make sure it
35863ccda647Slclee * is ready.
35873ccda647Slclee */
3588e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
35897f0b8309SEdward Pilatowicz rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
35903ccda647Slclee #if defined(_SUNOS_VTOC_8)
35913ccda647Slclee if (rval == EINVAL &&
3592e8fb11a1Sshidokht cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
35933ccda647Slclee /*
35943ccda647Slclee * This is to return a default label geometry even when we
35953ccda647Slclee * do not really assume a default label for the device.
35963ccda647Slclee * dad driver utilizes this.
35973ccda647Slclee */
3598342440ecSPrasad Singamsetty if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3599e8fb11a1Sshidokht cmlb_setup_default_geometry(cl, tg_cookie);
36003ccda647Slclee rval = 0;
36013ccda647Slclee }
36023ccda647Slclee }
36033ccda647Slclee #endif
36043ccda647Slclee if (rval) {
3605e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
36063ccda647Slclee return (rval);
36073ccda647Slclee }
36083ccda647Slclee
36093ccda647Slclee #if defined(__i386) || defined(__amd64)
3610e8fb11a1Sshidokht if (cl->cl_solaris_size == 0) {
3611e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
36123ccda647Slclee return (EIO);
36133ccda647Slclee }
36143ccda647Slclee #endif
36153ccda647Slclee
36163ccda647Slclee /*
36173ccda647Slclee * Make a local copy of the soft state geometry to avoid some potential
36183ccda647Slclee * race conditions associated with holding the mutex and updating the
36193ccda647Slclee * write_reinstruct value
36203ccda647Slclee */
36213ccda647Slclee tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3622e8fb11a1Sshidokht bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
36233ccda647Slclee
36243ccda647Slclee if (tmp_geom->dkg_write_reinstruct == 0) {
36253ccda647Slclee tmp_geom->dkg_write_reinstruct =
36263ccda647Slclee (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
36273ccda647Slclee cmlb_rot_delay) / (int)60000);
36283ccda647Slclee }
3629e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
36303ccda647Slclee
36313ccda647Slclee rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
36323ccda647Slclee flag);
36333ccda647Slclee if (rval != 0) {
36343ccda647Slclee rval = EFAULT;
36353ccda647Slclee }
36363ccda647Slclee
36373ccda647Slclee kmem_free(tmp_geom, sizeof (struct dk_geom));
36383ccda647Slclee return (rval);
36393ccda647Slclee
36403ccda647Slclee }
36413ccda647Slclee
36423ccda647Slclee
36433ccda647Slclee /*
36443ccda647Slclee * Function: cmlb_dkio_set_geometry
36453ccda647Slclee *
36463ccda647Slclee * Description: This routine is the driver entry point for handling user
36473ccda647Slclee * requests to set the device geometry (DKIOCSGEOM). The actual
36483ccda647Slclee * device geometry is not updated, just the driver "notion" of it.
36493ccda647Slclee *
36503ccda647Slclee * Arguments:
3651e8fb11a1Sshidokht * arg pointer to user provided dk_geom structure used to set
36523ccda647Slclee * the controller's notion of the current geometry.
3653e8fb11a1Sshidokht *
3654e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
36553ccda647Slclee * directly from the mode argument of ioctl().
36563ccda647Slclee *
3657e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
3658e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
3659e8fb11a1Sshidokht *
36603ccda647Slclee * Return Code: 0
36613ccda647Slclee * EFAULT
36623ccda647Slclee * ENXIO
36633ccda647Slclee * EIO
36643ccda647Slclee */
36653ccda647Slclee static int
3666e8fb11a1Sshidokht cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
36673ccda647Slclee {
36683ccda647Slclee struct dk_geom *tmp_geom;
36693ccda647Slclee struct dk_map *lp;
36703ccda647Slclee int rval = 0;
36713ccda647Slclee int i;
36723ccda647Slclee
36733ccda647Slclee
36743ccda647Slclee #if defined(__i386) || defined(__amd64)
3675e8fb11a1Sshidokht if (cl->cl_solaris_size == 0) {
36763ccda647Slclee return (EIO);
36773ccda647Slclee }
36783ccda647Slclee #endif
36793ccda647Slclee /*
36803ccda647Slclee * We need to copy the user specified geometry into local
36813ccda647Slclee * storage and then update the softstate. We don't want to hold
36823ccda647Slclee * the mutex and copyin directly from the user to the soft state
36833ccda647Slclee */
36843ccda647Slclee tmp_geom = (struct dk_geom *)
36853ccda647Slclee kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
36863ccda647Slclee rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
36873ccda647Slclee if (rval != 0) {
36883ccda647Slclee kmem_free(tmp_geom, sizeof (struct dk_geom));
36893ccda647Slclee return (EFAULT);
36903ccda647Slclee }
36913ccda647Slclee
3692e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
3693e8fb11a1Sshidokht bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
36943ccda647Slclee for (i = 0; i < NDKMAP; i++) {
3695e8fb11a1Sshidokht lp = &cl->cl_map[i];
3696e8fb11a1Sshidokht cl->cl_offset[i] =
3697e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
36983ccda647Slclee #if defined(__i386) || defined(__amd64)
3699e8fb11a1Sshidokht cl->cl_offset[i] += cl->cl_solaris_offset;
37003ccda647Slclee #endif
37013ccda647Slclee }
37027f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
3703e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
37043ccda647Slclee kmem_free(tmp_geom, sizeof (struct dk_geom));
37053ccda647Slclee
37063ccda647Slclee return (rval);
37073ccda647Slclee }
37083ccda647Slclee
37093ccda647Slclee /*
37103ccda647Slclee * Function: cmlb_dkio_get_partition
37113ccda647Slclee *
37123ccda647Slclee * Description: This routine is the driver entry point for handling user
37133ccda647Slclee * requests to get the partition table (DKIOCGAPART).
37143ccda647Slclee *
37153ccda647Slclee * Arguments:
3716e8fb11a1Sshidokht * arg pointer to user provided dk_allmap structure specifying
37173ccda647Slclee * the controller's notion of the current partition table.
3718e8fb11a1Sshidokht *
3719e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
37203ccda647Slclee * directly from the mode argument of ioctl().
37213ccda647Slclee *
3722e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
3723e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
3724e8fb11a1Sshidokht *
37253ccda647Slclee * Return Code: 0
37263ccda647Slclee * EFAULT
37273ccda647Slclee * ENXIO
37283ccda647Slclee * EIO
37293ccda647Slclee */
37303ccda647Slclee static int
3731e8fb11a1Sshidokht cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3732e8fb11a1Sshidokht void *tg_cookie)
37333ccda647Slclee {
37343ccda647Slclee int rval = 0;
37353ccda647Slclee int size;
37363ccda647Slclee
37373ccda647Slclee /*
37383ccda647Slclee * Make sure the geometry is valid before getting the partition
37393ccda647Slclee * information.
37403ccda647Slclee */
3741e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
37427f0b8309SEdward Pilatowicz if ((rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie)) != 0) {
3743e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
37443ccda647Slclee return (rval);
37453ccda647Slclee }
3746e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
37473ccda647Slclee
37483ccda647Slclee #if defined(__i386) || defined(__amd64)
3749e8fb11a1Sshidokht if (cl->cl_solaris_size == 0) {
37503ccda647Slclee return (EIO);
37513ccda647Slclee }
37523ccda647Slclee #endif
37533ccda647Slclee
37543ccda647Slclee #ifdef _MULTI_DATAMODEL
37553ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
37563ccda647Slclee case DDI_MODEL_ILP32: {
37573ccda647Slclee struct dk_map32 dk_map32[NDKMAP];
37583ccda647Slclee int i;
37593ccda647Slclee
37603ccda647Slclee for (i = 0; i < NDKMAP; i++) {
3761e8fb11a1Sshidokht dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3762e8fb11a1Sshidokht dk_map32[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
37633ccda647Slclee }
37643ccda647Slclee size = NDKMAP * sizeof (struct dk_map32);
37653ccda647Slclee rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
37663ccda647Slclee if (rval != 0) {
37673ccda647Slclee rval = EFAULT;
37683ccda647Slclee }
37693ccda647Slclee break;
37703ccda647Slclee }
37713ccda647Slclee case DDI_MODEL_NONE:
37723ccda647Slclee size = NDKMAP * sizeof (struct dk_map);
3773e8fb11a1Sshidokht rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
37743ccda647Slclee if (rval != 0) {
37753ccda647Slclee rval = EFAULT;
37763ccda647Slclee }
37773ccda647Slclee break;
37783ccda647Slclee }
37793ccda647Slclee #else /* ! _MULTI_DATAMODEL */
37803ccda647Slclee size = NDKMAP * sizeof (struct dk_map);
3781e8fb11a1Sshidokht rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
37823ccda647Slclee if (rval != 0) {
37833ccda647Slclee rval = EFAULT;
37843ccda647Slclee }
37853ccda647Slclee #endif /* _MULTI_DATAMODEL */
37863ccda647Slclee return (rval);
37873ccda647Slclee }
37883ccda647Slclee
37893ccda647Slclee /*
37903ccda647Slclee * Function: cmlb_dkio_set_partition
37913ccda647Slclee *
37923ccda647Slclee * Description: This routine is the driver entry point for handling user
37933ccda647Slclee * requests to set the partition table (DKIOCSAPART). The actual
37943ccda647Slclee * device partition is not updated.
37953ccda647Slclee *
37963ccda647Slclee * Arguments:
37973ccda647Slclee * arg - pointer to user provided dk_allmap structure used to set
37983ccda647Slclee * the controller's notion of the partition table.
37993ccda647Slclee * flag - this argument is a pass through to ddi_copyxxx()
38003ccda647Slclee * directly from the mode argument of ioctl().
38013ccda647Slclee *
38023ccda647Slclee * Return Code: 0
38033ccda647Slclee * EINVAL
38043ccda647Slclee * EFAULT
38053ccda647Slclee * ENXIO
38063ccda647Slclee * EIO
38073ccda647Slclee */
38083ccda647Slclee static int
3809e8fb11a1Sshidokht cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
38103ccda647Slclee {
38113ccda647Slclee struct dk_map dk_map[NDKMAP];
38123ccda647Slclee struct dk_map *lp;
38133ccda647Slclee int rval = 0;
38143ccda647Slclee int size;
38153ccda647Slclee int i;
38163ccda647Slclee #if defined(_SUNOS_VTOC_16)
38173ccda647Slclee struct dkl_partition *vp;
38183ccda647Slclee #endif
38193ccda647Slclee
38203ccda647Slclee /*
38213ccda647Slclee * Set the map for all logical partitions. We lock
38223ccda647Slclee * the priority just to make sure an interrupt doesn't
38233ccda647Slclee * come in while the map is half updated.
38243ccda647Slclee */
3825e8fb11a1Sshidokht _NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3826e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
38273ccda647Slclee
3828342440ecSPrasad Singamsetty if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3829e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
38303ccda647Slclee return (ENOTSUP);
38313ccda647Slclee }
3832e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
3833e8fb11a1Sshidokht if (cl->cl_solaris_size == 0) {
38343ccda647Slclee return (EIO);
38353ccda647Slclee }
38363ccda647Slclee
38373ccda647Slclee #ifdef _MULTI_DATAMODEL
38383ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
38393ccda647Slclee case DDI_MODEL_ILP32: {
38403ccda647Slclee struct dk_map32 dk_map32[NDKMAP];
38413ccda647Slclee
38423ccda647Slclee size = NDKMAP * sizeof (struct dk_map32);
38433ccda647Slclee rval = ddi_copyin((void *)arg, dk_map32, size, flag);
38443ccda647Slclee if (rval != 0) {
38453ccda647Slclee return (EFAULT);
38463ccda647Slclee }
38473ccda647Slclee for (i = 0; i < NDKMAP; i++) {
38483ccda647Slclee dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
38493ccda647Slclee dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk;
38503ccda647Slclee }
38513ccda647Slclee break;
38523ccda647Slclee }
38533ccda647Slclee case DDI_MODEL_NONE:
38543ccda647Slclee size = NDKMAP * sizeof (struct dk_map);
38553ccda647Slclee rval = ddi_copyin((void *)arg, dk_map, size, flag);
38563ccda647Slclee if (rval != 0) {
38573ccda647Slclee return (EFAULT);
38583ccda647Slclee }
38593ccda647Slclee break;
38603ccda647Slclee }
38613ccda647Slclee #else /* ! _MULTI_DATAMODEL */
38623ccda647Slclee size = NDKMAP * sizeof (struct dk_map);
38633ccda647Slclee rval = ddi_copyin((void *)arg, dk_map, size, flag);
38643ccda647Slclee if (rval != 0) {
38653ccda647Slclee return (EFAULT);
38663ccda647Slclee }
38673ccda647Slclee #endif /* _MULTI_DATAMODEL */
38683ccda647Slclee
3869e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
38703ccda647Slclee /* Note: The size used in this bcopy is set based upon the data model */
3871e8fb11a1Sshidokht bcopy(dk_map, cl->cl_map, size);
38723ccda647Slclee #if defined(_SUNOS_VTOC_16)
3873e8fb11a1Sshidokht vp = (struct dkl_partition *)&(cl->cl_vtoc);
38743ccda647Slclee #endif /* defined(_SUNOS_VTOC_16) */
38753ccda647Slclee for (i = 0; i < NDKMAP; i++) {
3876e8fb11a1Sshidokht lp = &cl->cl_map[i];
3877e8fb11a1Sshidokht cl->cl_offset[i] =
3878e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
38793ccda647Slclee #if defined(_SUNOS_VTOC_16)
3880e8fb11a1Sshidokht vp->p_start = cl->cl_offset[i];
38813ccda647Slclee vp->p_size = lp->dkl_nblk;
38823ccda647Slclee vp++;
38833ccda647Slclee #endif /* defined(_SUNOS_VTOC_16) */
38843ccda647Slclee #if defined(__i386) || defined(__amd64)
3885e8fb11a1Sshidokht cl->cl_offset[i] += cl->cl_solaris_offset;
38863ccda647Slclee #endif
38873ccda647Slclee }
3888e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
38893ccda647Slclee return (rval);
38903ccda647Slclee }
38913ccda647Slclee
38923ccda647Slclee
38933ccda647Slclee /*
38943ccda647Slclee * Function: cmlb_dkio_get_vtoc
38953ccda647Slclee *
38963ccda647Slclee * Description: This routine is the driver entry point for handling user
38973ccda647Slclee * requests to get the current volume table of contents
38983ccda647Slclee * (DKIOCGVTOC).
38993ccda647Slclee *
39003ccda647Slclee * Arguments:
3901e8fb11a1Sshidokht * arg pointer to user provided vtoc structure specifying
39023ccda647Slclee * the current vtoc.
3903e8fb11a1Sshidokht *
3904e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
39053ccda647Slclee * directly from the mode argument of ioctl().
39063ccda647Slclee *
3907e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
3908e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
3909e8fb11a1Sshidokht *
39103ccda647Slclee * Return Code: 0
39113ccda647Slclee * EFAULT
39123ccda647Slclee * ENXIO
39133ccda647Slclee * EIO
39143ccda647Slclee */
39153ccda647Slclee static int
3916e8fb11a1Sshidokht cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
39173ccda647Slclee {
39183ccda647Slclee #if defined(_SUNOS_VTOC_8)
39193ccda647Slclee struct vtoc user_vtoc;
39203ccda647Slclee #endif /* defined(_SUNOS_VTOC_8) */
39213ccda647Slclee int rval = 0;
39223ccda647Slclee
3923e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
3924342440ecSPrasad Singamsetty if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3925342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
3926342440ecSPrasad Singamsetty return (EOVERFLOW);
3927342440ecSPrasad Singamsetty }
3928342440ecSPrasad Singamsetty
39297f0b8309SEdward Pilatowicz rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
39303ccda647Slclee
39313ccda647Slclee #if defined(_SUNOS_VTOC_8)
39323ccda647Slclee if (rval == EINVAL &&
3933e8fb11a1Sshidokht (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
39343ccda647Slclee /*
39353ccda647Slclee * This is to return a default label even when we do not
39363ccda647Slclee * really assume a default label for the device.
39373ccda647Slclee * dad driver utilizes this.
39383ccda647Slclee */
3939342440ecSPrasad Singamsetty if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3940e8fb11a1Sshidokht cmlb_setup_default_geometry(cl, tg_cookie);
39413ccda647Slclee rval = 0;
39423ccda647Slclee }
39433ccda647Slclee }
39443ccda647Slclee #endif
39453ccda647Slclee if (rval) {
3946e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
39473ccda647Slclee return (rval);
39483ccda647Slclee }
39493ccda647Slclee
39503ccda647Slclee #if defined(_SUNOS_VTOC_8)
3951e8fb11a1Sshidokht cmlb_build_user_vtoc(cl, &user_vtoc);
3952e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
39533ccda647Slclee
39543ccda647Slclee #ifdef _MULTI_DATAMODEL
39553ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
39563ccda647Slclee case DDI_MODEL_ILP32: {
39573ccda647Slclee struct vtoc32 user_vtoc32;
39583ccda647Slclee
39593ccda647Slclee vtoctovtoc32(user_vtoc, user_vtoc32);
39603ccda647Slclee if (ddi_copyout(&user_vtoc32, (void *)arg,
39613ccda647Slclee sizeof (struct vtoc32), flag)) {
39623ccda647Slclee return (EFAULT);
39633ccda647Slclee }
39643ccda647Slclee break;
39653ccda647Slclee }
39663ccda647Slclee
39673ccda647Slclee case DDI_MODEL_NONE:
39683ccda647Slclee if (ddi_copyout(&user_vtoc, (void *)arg,
39693ccda647Slclee sizeof (struct vtoc), flag)) {
39703ccda647Slclee return (EFAULT);
39713ccda647Slclee }
39723ccda647Slclee break;
39733ccda647Slclee }
39743ccda647Slclee #else /* ! _MULTI_DATAMODEL */
39753ccda647Slclee if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
39763ccda647Slclee return (EFAULT);
39773ccda647Slclee }
39783ccda647Slclee #endif /* _MULTI_DATAMODEL */
39793ccda647Slclee
39803ccda647Slclee #elif defined(_SUNOS_VTOC_16)
3981e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
39823ccda647Slclee
39833ccda647Slclee #ifdef _MULTI_DATAMODEL
39843ccda647Slclee /*
3985e8fb11a1Sshidokht * The cl_vtoc structure is a "struct dk_vtoc" which is always
39863ccda647Slclee * 32-bit to maintain compatibility with existing on-disk
39873ccda647Slclee * structures. Thus, we need to convert the structure when copying
39883ccda647Slclee * it out to a datamodel-dependent "struct vtoc" in a 64-bit
39893ccda647Slclee * program. If the target is a 32-bit program, then no conversion
39903ccda647Slclee * is necessary.
39913ccda647Slclee */
39923ccda647Slclee /* LINTED: logical expression always true: op "||" */
3993e8fb11a1Sshidokht ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
39943ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
39953ccda647Slclee case DDI_MODEL_ILP32:
3996e8fb11a1Sshidokht if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
3997e8fb11a1Sshidokht sizeof (cl->cl_vtoc), flag)) {
39983ccda647Slclee return (EFAULT);
39993ccda647Slclee }
40003ccda647Slclee break;
40013ccda647Slclee
40023ccda647Slclee case DDI_MODEL_NONE: {
40033ccda647Slclee struct vtoc user_vtoc;
40043ccda647Slclee
4005e8fb11a1Sshidokht vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
40063ccda647Slclee if (ddi_copyout(&user_vtoc, (void *)arg,
40073ccda647Slclee sizeof (struct vtoc), flag)) {
40083ccda647Slclee return (EFAULT);
40093ccda647Slclee }
40103ccda647Slclee break;
40113ccda647Slclee }
40123ccda647Slclee }
40133ccda647Slclee #else /* ! _MULTI_DATAMODEL */
4014e8fb11a1Sshidokht if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
40153ccda647Slclee flag)) {
40163ccda647Slclee return (EFAULT);
40173ccda647Slclee }
40183ccda647Slclee #endif /* _MULTI_DATAMODEL */
40193ccda647Slclee #else
40203ccda647Slclee #error "No VTOC format defined."
40213ccda647Slclee #endif
40223ccda647Slclee
40233ccda647Slclee return (rval);
40243ccda647Slclee }
40253ccda647Slclee
4026342440ecSPrasad Singamsetty
4027342440ecSPrasad Singamsetty /*
4028342440ecSPrasad Singamsetty * Function: cmlb_dkio_get_extvtoc
4029342440ecSPrasad Singamsetty */
4030342440ecSPrasad Singamsetty static int
4031342440ecSPrasad Singamsetty cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
4032342440ecSPrasad Singamsetty void *tg_cookie)
4033342440ecSPrasad Singamsetty {
4034342440ecSPrasad Singamsetty struct extvtoc ext_vtoc;
4035342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
4036342440ecSPrasad Singamsetty struct vtoc user_vtoc;
4037342440ecSPrasad Singamsetty #endif /* defined(_SUNOS_VTOC_8) */
4038342440ecSPrasad Singamsetty int rval = 0;
4039342440ecSPrasad Singamsetty
4040342440ecSPrasad Singamsetty bzero(&ext_vtoc, sizeof (struct extvtoc));
4041342440ecSPrasad Singamsetty mutex_enter(CMLB_MUTEX(cl));
40427f0b8309SEdward Pilatowicz rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
4043342440ecSPrasad Singamsetty
4044342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
4045342440ecSPrasad Singamsetty if (rval == EINVAL &&
4046342440ecSPrasad Singamsetty (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
4047342440ecSPrasad Singamsetty /*
4048342440ecSPrasad Singamsetty * This is to return a default label even when we do not
4049342440ecSPrasad Singamsetty * really assume a default label for the device.
4050342440ecSPrasad Singamsetty * dad driver utilizes this.
4051342440ecSPrasad Singamsetty */
4052342440ecSPrasad Singamsetty if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
4053342440ecSPrasad Singamsetty cmlb_setup_default_geometry(cl, tg_cookie);
4054342440ecSPrasad Singamsetty rval = 0;
4055342440ecSPrasad Singamsetty }
4056342440ecSPrasad Singamsetty }
4057342440ecSPrasad Singamsetty #endif
4058342440ecSPrasad Singamsetty if (rval) {
4059342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4060342440ecSPrasad Singamsetty return (rval);
4061342440ecSPrasad Singamsetty }
4062342440ecSPrasad Singamsetty
4063342440ecSPrasad Singamsetty #if defined(_SUNOS_VTOC_8)
4064342440ecSPrasad Singamsetty cmlb_build_user_vtoc(cl, &user_vtoc);
4065342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4066342440ecSPrasad Singamsetty
4067342440ecSPrasad Singamsetty /*
4068342440ecSPrasad Singamsetty * Checking callers data model does not make much sense here
4069342440ecSPrasad Singamsetty * since extvtoc will always be equivalent to 64bit vtoc.
4070342440ecSPrasad Singamsetty * What is important is whether the kernel is in 32 or 64 bit
4071342440ecSPrasad Singamsetty */
4072342440ecSPrasad Singamsetty
4073342440ecSPrasad Singamsetty #ifdef _LP64
4074342440ecSPrasad Singamsetty if (ddi_copyout(&user_vtoc, (void *)arg,
4075342440ecSPrasad Singamsetty sizeof (struct extvtoc), flag)) {
4076342440ecSPrasad Singamsetty return (EFAULT);
4077342440ecSPrasad Singamsetty }
4078342440ecSPrasad Singamsetty #else
4079342440ecSPrasad Singamsetty vtoc32tovtoc(user_vtoc, ext_vtoc);
4080342440ecSPrasad Singamsetty if (ddi_copyout(&ext_vtoc, (void *)arg,
4081342440ecSPrasad Singamsetty sizeof (struct extvtoc), flag)) {
4082342440ecSPrasad Singamsetty return (EFAULT);
4083342440ecSPrasad Singamsetty }
4084342440ecSPrasad Singamsetty #endif
4085342440ecSPrasad Singamsetty
4086342440ecSPrasad Singamsetty #elif defined(_SUNOS_VTOC_16)
4087342440ecSPrasad Singamsetty /*
4088342440ecSPrasad Singamsetty * The cl_vtoc structure is a "struct dk_vtoc" which is always
4089342440ecSPrasad Singamsetty * 32-bit to maintain compatibility with existing on-disk
4090342440ecSPrasad Singamsetty * structures. Thus, we need to convert the structure when copying
4091342440ecSPrasad Singamsetty * it out to extvtoc
4092342440ecSPrasad Singamsetty */
4093342440ecSPrasad Singamsetty vtoc32tovtoc(cl->cl_vtoc, ext_vtoc);
40941fad2c0eSXiao Raymond Li mutex_exit(CMLB_MUTEX(cl));
4095342440ecSPrasad Singamsetty
4096342440ecSPrasad Singamsetty if (ddi_copyout(&ext_vtoc, (void *)arg, sizeof (struct extvtoc), flag))
4097342440ecSPrasad Singamsetty return (EFAULT);
4098342440ecSPrasad Singamsetty #else
4099342440ecSPrasad Singamsetty #error "No VTOC format defined."
4100342440ecSPrasad Singamsetty #endif
4101342440ecSPrasad Singamsetty
4102342440ecSPrasad Singamsetty return (rval);
4103342440ecSPrasad Singamsetty }
4104c31fac72SShidokht Yadegari
4105c31fac72SShidokht Yadegari /*
4106c31fac72SShidokht Yadegari * This routine implements the DKIOCGETEFI ioctl. This ioctl is currently
4107c31fac72SShidokht Yadegari * used to read the GPT Partition Table Header (primary/backup), the GUID
4108c31fac72SShidokht Yadegari * partition Entry Array (primary/backup), and the MBR.
4109c31fac72SShidokht Yadegari */
41103ccda647Slclee static int
4111e8fb11a1Sshidokht cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
41123ccda647Slclee {
41133ccda647Slclee dk_efi_t user_efi;
41143ccda647Slclee int rval = 0;
41153ccda647Slclee void *buffer;
4116e8fb11a1Sshidokht diskaddr_t tgt_lba;
41173ccda647Slclee
41183ccda647Slclee if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
41193ccda647Slclee return (EFAULT);
41203ccda647Slclee
41213ccda647Slclee user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
41223ccda647Slclee
4123de6d0fcdSShidokht Yadegari if (user_efi.dki_length == 0 ||
4124de6d0fcdSShidokht Yadegari user_efi.dki_length > cmlb_tg_max_efi_xfer)
4125c31fac72SShidokht Yadegari return (EINVAL);
4126c31fac72SShidokht Yadegari
4127e8fb11a1Sshidokht tgt_lba = user_efi.dki_lba;
4128e8fb11a1Sshidokht
4129e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
4130e8fb11a1Sshidokht if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4131de6d0fcdSShidokht Yadegari (cl->cl_tgt_blocksize == 0) ||
4132de6d0fcdSShidokht Yadegari (user_efi.dki_length % cl->cl_sys_blocksize)) {
4133e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4134e8fb11a1Sshidokht return (EINVAL);
4135e8fb11a1Sshidokht }
4136e8fb11a1Sshidokht if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4137e8fb11a1Sshidokht tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
4138e8fb11a1Sshidokht cl->cl_sys_blocksize;
4139e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4140e8fb11a1Sshidokht
41413ccda647Slclee buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4142e8fb11a1Sshidokht rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
41433ccda647Slclee if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
41443ccda647Slclee user_efi.dki_length, flag) != 0)
41453ccda647Slclee rval = EFAULT;
41463ccda647Slclee
41473ccda647Slclee kmem_free(buffer, user_efi.dki_length);
41483ccda647Slclee return (rval);
41493ccda647Slclee }
41503ccda647Slclee
4151e8fb11a1Sshidokht #if defined(_SUNOS_VTOC_8)
41523ccda647Slclee /*
41533ccda647Slclee * Function: cmlb_build_user_vtoc
41543ccda647Slclee *
41553ccda647Slclee * Description: This routine populates a pass by reference variable with the
41563ccda647Slclee * current volume table of contents.
41573ccda647Slclee *
4158e8fb11a1Sshidokht * Arguments: cl - driver soft state (unit) structure
41593ccda647Slclee * user_vtoc - pointer to vtoc structure to be populated
41603ccda647Slclee */
41613ccda647Slclee static void
4162e8fb11a1Sshidokht cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
41633ccda647Slclee {
41643ccda647Slclee struct dk_map2 *lpart;
41653ccda647Slclee struct dk_map *lmap;
41663ccda647Slclee struct partition *vpart;
4167342440ecSPrasad Singamsetty uint32_t nblks;
41683ccda647Slclee int i;
41693ccda647Slclee
4170e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
41713ccda647Slclee
41723ccda647Slclee /*
41733ccda647Slclee * Return vtoc structure fields in the provided VTOC area, addressed
41743ccda647Slclee * by *vtoc.
41753ccda647Slclee */
41763ccda647Slclee bzero(user_vtoc, sizeof (struct vtoc));
4177e8fb11a1Sshidokht user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
4178e8fb11a1Sshidokht user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
4179e8fb11a1Sshidokht user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
41803ccda647Slclee user_vtoc->v_sanity = VTOC_SANE;
4181e8fb11a1Sshidokht user_vtoc->v_version = cl->cl_vtoc.v_version;
4182e8fb11a1Sshidokht bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
4183e8fb11a1Sshidokht user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
4184e8fb11a1Sshidokht user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
41853ccda647Slclee
41863ccda647Slclee for (i = 0; i < 10; i++)
4187e8fb11a1Sshidokht user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
41883ccda647Slclee
41893ccda647Slclee /*
41903ccda647Slclee * Convert partitioning information.
41913ccda647Slclee *
41923ccda647Slclee * Note the conversion from starting cylinder number
41933ccda647Slclee * to starting sector number.
41943ccda647Slclee */
4195e8fb11a1Sshidokht lmap = cl->cl_map;
4196e8fb11a1Sshidokht lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
41973ccda647Slclee vpart = user_vtoc->v_part;
41983ccda647Slclee
4199e8fb11a1Sshidokht nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
42003ccda647Slclee
42013ccda647Slclee for (i = 0; i < V_NUMPAR; i++) {
42023ccda647Slclee vpart->p_tag = lpart->p_tag;
42033ccda647Slclee vpart->p_flag = lpart->p_flag;
42043ccda647Slclee vpart->p_start = lmap->dkl_cylno * nblks;
42053ccda647Slclee vpart->p_size = lmap->dkl_nblk;
42063ccda647Slclee lmap++;
42073ccda647Slclee lpart++;
42083ccda647Slclee vpart++;
42093ccda647Slclee
42103ccda647Slclee /* (4364927) */
4211e8fb11a1Sshidokht user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
42123ccda647Slclee }
42133ccda647Slclee
4214e8fb11a1Sshidokht bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
42153ccda647Slclee }
4216e8fb11a1Sshidokht #endif
42173ccda647Slclee
42183ccda647Slclee static int
4219e8fb11a1Sshidokht cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
4220e8fb11a1Sshidokht void *tg_cookie)
42213ccda647Slclee {
42223ccda647Slclee struct partition64 p64;
42233ccda647Slclee int rval = 0;
42243ccda647Slclee uint_t nparts;
42253ccda647Slclee efi_gpe_t *partitions;
42263ccda647Slclee efi_gpt_t *buffer;
42273ccda647Slclee diskaddr_t gpe_lba;
42287e74b1cbSSharath M Srinivasan int n_gpe_per_blk = 0;
42293ccda647Slclee
42303ccda647Slclee if (ddi_copyin((const void *)arg, &p64,
42313ccda647Slclee sizeof (struct partition64), flag)) {
42323ccda647Slclee return (EFAULT);
42333ccda647Slclee }
42343ccda647Slclee
42357e74b1cbSSharath M Srinivasan buffer = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
423665908c77Syu, larry liu - Sun Microsystems - Beijing China rval = DK_TG_READ(cl, buffer, 1, cl->cl_sys_blocksize, tg_cookie);
42373ccda647Slclee if (rval != 0)
42383ccda647Slclee goto done_error;
42393ccda647Slclee
42403ccda647Slclee cmlb_swap_efi_gpt(buffer);
42413ccda647Slclee
42423ccda647Slclee if ((rval = cmlb_validate_efi(buffer)) != 0)
42433ccda647Slclee goto done_error;
42443ccda647Slclee
42453ccda647Slclee nparts = buffer->efi_gpt_NumberOfPartitionEntries;
42463ccda647Slclee gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
42477e74b1cbSSharath M Srinivasan if (p64.p_partno >= nparts) {
42483ccda647Slclee /* couldn't find it */
42493ccda647Slclee rval = ESRCH;
42503ccda647Slclee goto done_error;
42513ccda647Slclee }
42523ccda647Slclee /*
42537e74b1cbSSharath M Srinivasan * Read the block that contains the requested GPE.
42543ccda647Slclee */
42557e74b1cbSSharath M Srinivasan n_gpe_per_blk = cl->cl_sys_blocksize / sizeof (efi_gpe_t);
42567e74b1cbSSharath M Srinivasan gpe_lba += p64.p_partno / n_gpe_per_blk;
42577e74b1cbSSharath M Srinivasan rval = DK_TG_READ(cl, buffer, gpe_lba, cl->cl_sys_blocksize, tg_cookie);
42583ccda647Slclee
42593ccda647Slclee if (rval) {
42603ccda647Slclee goto done_error;
42613ccda647Slclee }
42623ccda647Slclee partitions = (efi_gpe_t *)buffer;
42637e74b1cbSSharath M Srinivasan partitions += p64.p_partno % n_gpe_per_blk;
42649cd72fdfSSharath M Srinivasan
42659cd72fdfSSharath M Srinivasan /* Byte swap only the requested GPE */
42669cd72fdfSSharath M Srinivasan cmlb_swap_efi_gpe(1, partitions);
42679cd72fdfSSharath M Srinivasan
42683ccda647Slclee bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
42693ccda647Slclee sizeof (struct uuid));
42703ccda647Slclee p64.p_start = partitions->efi_gpe_StartingLBA;
42713ccda647Slclee p64.p_size = partitions->efi_gpe_EndingLBA -
42723ccda647Slclee p64.p_start + 1;
42733ccda647Slclee
42743ccda647Slclee if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
42753ccda647Slclee rval = EFAULT;
42763ccda647Slclee
42773ccda647Slclee done_error:
42787e74b1cbSSharath M Srinivasan kmem_free(buffer, cl->cl_sys_blocksize);
42793ccda647Slclee return (rval);
42803ccda647Slclee }
42813ccda647Slclee
42823ccda647Slclee
42833ccda647Slclee /*
42843ccda647Slclee * Function: cmlb_dkio_set_vtoc
42853ccda647Slclee *
42863ccda647Slclee * Description: This routine is the driver entry point for handling user
42873ccda647Slclee * requests to set the current volume table of contents
42883ccda647Slclee * (DKIOCSVTOC).
42893ccda647Slclee *
4290e8fb11a1Sshidokht * Arguments:
4291e8fb11a1Sshidokht * dev the device number
4292e8fb11a1Sshidokht * arg pointer to user provided vtoc structure used to set the
42933ccda647Slclee * current vtoc.
4294e8fb11a1Sshidokht *
4295e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
42963ccda647Slclee * directly from the mode argument of ioctl().
42973ccda647Slclee *
4298e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
4299e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
4300e8fb11a1Sshidokht *
43013ccda647Slclee * Return Code: 0
43023ccda647Slclee * EFAULT
43033ccda647Slclee * ENXIO
43043ccda647Slclee * EINVAL
43053ccda647Slclee * ENOTSUP
43063ccda647Slclee */
43073ccda647Slclee static int
4308e8fb11a1Sshidokht cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4309e8fb11a1Sshidokht void *tg_cookie)
43103ccda647Slclee {
43113ccda647Slclee struct vtoc user_vtoc;
43123ccda647Slclee int rval = 0;
431306bbe1e0Sedp boolean_t internal;
431406bbe1e0Sedp
43157f0b8309SEdward Pilatowicz internal = VOID2BOOLEAN(
43167f0b8309SEdward Pilatowicz (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
43173ccda647Slclee
43183ccda647Slclee #ifdef _MULTI_DATAMODEL
43193ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
43203ccda647Slclee case DDI_MODEL_ILP32: {
43213ccda647Slclee struct vtoc32 user_vtoc32;
43223ccda647Slclee
43233ccda647Slclee if (ddi_copyin((const void *)arg, &user_vtoc32,
43243ccda647Slclee sizeof (struct vtoc32), flag)) {
43253ccda647Slclee return (EFAULT);
43263ccda647Slclee }
43273ccda647Slclee vtoc32tovtoc(user_vtoc32, user_vtoc);
43283ccda647Slclee break;
43293ccda647Slclee }
43303ccda647Slclee
43313ccda647Slclee case DDI_MODEL_NONE:
43323ccda647Slclee if (ddi_copyin((const void *)arg, &user_vtoc,
43333ccda647Slclee sizeof (struct vtoc), flag)) {
43343ccda647Slclee return (EFAULT);
43353ccda647Slclee }
43363ccda647Slclee break;
43373ccda647Slclee }
43383ccda647Slclee #else /* ! _MULTI_DATAMODEL */
43393ccda647Slclee if (ddi_copyin((const void *)arg, &user_vtoc,
43403ccda647Slclee sizeof (struct vtoc), flag)) {
43413ccda647Slclee return (EFAULT);
43423ccda647Slclee }
43433ccda647Slclee #endif /* _MULTI_DATAMODEL */
43443ccda647Slclee
4345e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
4346342440ecSPrasad Singamsetty
4347342440ecSPrasad Singamsetty if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
4348e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4349342440ecSPrasad Singamsetty return (EOVERFLOW);
43503ccda647Slclee }
4351e8fb11a1Sshidokht
4352e8fb11a1Sshidokht #if defined(__i386) || defined(__amd64)
4353e8fb11a1Sshidokht if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4354e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4355e8fb11a1Sshidokht return (EINVAL);
4356e8fb11a1Sshidokht }
4357e8fb11a1Sshidokht #endif
4358e8fb11a1Sshidokht
4359e8fb11a1Sshidokht if (cl->cl_g.dkg_ncyl == 0) {
4360e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
43613ccda647Slclee return (EINVAL);
43623ccda647Slclee }
43633ccda647Slclee
4364e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4365e8fb11a1Sshidokht cmlb_clear_efi(cl, tg_cookie);
4366e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4367e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
436848794f22SSriram Popuri
436948794f22SSriram Popuri /*
437048794f22SSriram Popuri * cmlb_dkio_set_vtoc creates duplicate minor nodes when
437148794f22SSriram Popuri * relabeling an SMI disk. To avoid that we remove them
437248794f22SSriram Popuri * before creating.
437348794f22SSriram Popuri * It should be OK to remove a non-existed minor node.
437448794f22SSriram Popuri */
437548794f22SSriram Popuri ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
437648794f22SSriram Popuri ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
437748794f22SSriram Popuri
437806bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
43793ccda647Slclee S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
438006bbe1e0Sedp cl->cl_node_type, NULL, internal);
438106bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
43823ccda647Slclee S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
438306bbe1e0Sedp cl->cl_node_type, NULL, internal);
4384e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
43853ccda647Slclee
4386e8fb11a1Sshidokht if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4387e8fb11a1Sshidokht if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
43887f0b8309SEdward Pilatowicz if (cmlb_validate_geometry(cl,
43897f0b8309SEdward Pilatowicz B_TRUE, 0, tg_cookie) != 0) {
4390e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
43913ccda647Slclee "cmlb_dkio_set_vtoc: "
43923ccda647Slclee "Failed validate geometry\n");
43933ccda647Slclee }
4394342440ecSPrasad Singamsetty cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
43953ccda647Slclee }
43963ccda647Slclee }
4397e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
43983ccda647Slclee return (rval);
43993ccda647Slclee }
44003ccda647Slclee
4401342440ecSPrasad Singamsetty /*
4402342440ecSPrasad Singamsetty * Function: cmlb_dkio_set_extvtoc
4403342440ecSPrasad Singamsetty */
4404342440ecSPrasad Singamsetty static int
4405342440ecSPrasad Singamsetty cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4406342440ecSPrasad Singamsetty void *tg_cookie)
4407342440ecSPrasad Singamsetty {
4408342440ecSPrasad Singamsetty int rval = 0;
4409342440ecSPrasad Singamsetty struct vtoc user_vtoc;
44107e70b434SShidokht Yadegari boolean_t internal;
44117e70b434SShidokht Yadegari
4412342440ecSPrasad Singamsetty
4413342440ecSPrasad Singamsetty /*
4414342440ecSPrasad Singamsetty * Checking callers data model does not make much sense here
4415342440ecSPrasad Singamsetty * since extvtoc will always be equivalent to 64bit vtoc.
4416342440ecSPrasad Singamsetty * What is important is whether the kernel is in 32 or 64 bit
4417342440ecSPrasad Singamsetty */
4418342440ecSPrasad Singamsetty
4419342440ecSPrasad Singamsetty #ifdef _LP64
4420342440ecSPrasad Singamsetty if (ddi_copyin((const void *)arg, &user_vtoc,
4421342440ecSPrasad Singamsetty sizeof (struct extvtoc), flag)) {
4422342440ecSPrasad Singamsetty return (EFAULT);
4423342440ecSPrasad Singamsetty }
4424342440ecSPrasad Singamsetty #else
4425342440ecSPrasad Singamsetty struct extvtoc user_extvtoc;
4426342440ecSPrasad Singamsetty if (ddi_copyin((const void *)arg, &user_extvtoc,
4427342440ecSPrasad Singamsetty sizeof (struct extvtoc), flag)) {
4428342440ecSPrasad Singamsetty return (EFAULT);
4429342440ecSPrasad Singamsetty }
4430342440ecSPrasad Singamsetty
4431342440ecSPrasad Singamsetty vtoctovtoc32(user_extvtoc, user_vtoc);
4432342440ecSPrasad Singamsetty #endif
4433342440ecSPrasad Singamsetty
44347f0b8309SEdward Pilatowicz internal = VOID2BOOLEAN(
44357f0b8309SEdward Pilatowicz (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4436342440ecSPrasad Singamsetty mutex_enter(CMLB_MUTEX(cl));
4437342440ecSPrasad Singamsetty #if defined(__i386) || defined(__amd64)
4438342440ecSPrasad Singamsetty if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4439342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4440342440ecSPrasad Singamsetty return (EINVAL);
4441342440ecSPrasad Singamsetty }
4442342440ecSPrasad Singamsetty #endif
4443342440ecSPrasad Singamsetty
4444342440ecSPrasad Singamsetty if (cl->cl_g.dkg_ncyl == 0) {
4445342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4446342440ecSPrasad Singamsetty return (EINVAL);
4447342440ecSPrasad Singamsetty }
4448342440ecSPrasad Singamsetty
4449342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4450342440ecSPrasad Singamsetty cmlb_clear_efi(cl, tg_cookie);
4451342440ecSPrasad Singamsetty ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4452342440ecSPrasad Singamsetty ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
44537abd0658SSriram Popuri /*
44547abd0658SSriram Popuri * cmlb_dkio_set_extvtoc creates duplicate minor nodes when
44557abd0658SSriram Popuri * relabeling an SMI disk. To avoid that we remove them
44567abd0658SSriram Popuri * before creating.
44577abd0658SSriram Popuri * It should be OK to remove a non-existed minor node.
44587abd0658SSriram Popuri */
44597abd0658SSriram Popuri ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
44607abd0658SSriram Popuri ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
44617abd0658SSriram Popuri
44627e70b434SShidokht Yadegari (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4463342440ecSPrasad Singamsetty S_IFBLK, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
44647e70b434SShidokht Yadegari cl->cl_node_type, NULL, internal);
44657e70b434SShidokht Yadegari (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4466342440ecSPrasad Singamsetty S_IFCHR, (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
44677e70b434SShidokht Yadegari cl->cl_node_type, NULL, internal);
44687e70b434SShidokht Yadegari
4469342440ecSPrasad Singamsetty mutex_enter(CMLB_MUTEX(cl));
4470342440ecSPrasad Singamsetty
4471342440ecSPrasad Singamsetty if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4472342440ecSPrasad Singamsetty if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
44737f0b8309SEdward Pilatowicz if (cmlb_validate_geometry(cl,
44747f0b8309SEdward Pilatowicz B_TRUE, 0, tg_cookie) != 0) {
4475342440ecSPrasad Singamsetty cmlb_dbg(CMLB_ERROR, cl,
4476342440ecSPrasad Singamsetty "cmlb_dkio_set_vtoc: "
4477342440ecSPrasad Singamsetty "Failed validate geometry\n");
4478342440ecSPrasad Singamsetty }
4479342440ecSPrasad Singamsetty }
4480342440ecSPrasad Singamsetty }
4481342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
4482342440ecSPrasad Singamsetty return (rval);
4483342440ecSPrasad Singamsetty }
44843ccda647Slclee
44853ccda647Slclee /*
44863ccda647Slclee * Function: cmlb_build_label_vtoc
44873ccda647Slclee *
44883ccda647Slclee * Description: This routine updates the driver soft state current volume table
44893ccda647Slclee * of contents based on a user specified vtoc.
44903ccda647Slclee *
4491e8fb11a1Sshidokht * Arguments: cl - driver soft state (unit) structure
44923ccda647Slclee * user_vtoc - pointer to vtoc structure specifying vtoc to be used
44933ccda647Slclee * to update the driver soft state.
44943ccda647Slclee *
44953ccda647Slclee * Return Code: 0
44963ccda647Slclee * EINVAL
44973ccda647Slclee */
44983ccda647Slclee static int
4499e8fb11a1Sshidokht cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
45003ccda647Slclee {
45013ccda647Slclee struct dk_map *lmap;
45023ccda647Slclee struct partition *vpart;
4503342440ecSPrasad Singamsetty uint_t nblks;
45043ccda647Slclee #if defined(_SUNOS_VTOC_8)
45053ccda647Slclee int ncyl;
45063ccda647Slclee struct dk_map2 *lpart;
45073ccda647Slclee #endif /* defined(_SUNOS_VTOC_8) */
45083ccda647Slclee int i;
45093ccda647Slclee
4510e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
45113ccda647Slclee
45123ccda647Slclee /* Sanity-check the vtoc */
45133ccda647Slclee if (user_vtoc->v_sanity != VTOC_SANE ||
4514e8fb11a1Sshidokht user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
45153ccda647Slclee user_vtoc->v_nparts != V_NUMPAR) {
4516e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
45173ccda647Slclee "cmlb_build_label_vtoc: vtoc not valid\n");
45183ccda647Slclee return (EINVAL);
45193ccda647Slclee }
45203ccda647Slclee
4521e8fb11a1Sshidokht nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
45223ccda647Slclee if (nblks == 0) {
4523e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
45243ccda647Slclee "cmlb_build_label_vtoc: geom nblks is 0\n");
45253ccda647Slclee return (EINVAL);
45263ccda647Slclee }
45273ccda647Slclee
45283ccda647Slclee #if defined(_SUNOS_VTOC_8)
45293ccda647Slclee vpart = user_vtoc->v_part;
45303ccda647Slclee for (i = 0; i < V_NUMPAR; i++) {
4531342440ecSPrasad Singamsetty if (((unsigned)vpart->p_start % nblks) != 0) {
4532e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
45333ccda647Slclee "cmlb_build_label_vtoc: p_start not multiply of"
45343ccda647Slclee "nblks part %d p_start %d nblks %d\n", i,
45353ccda647Slclee vpart->p_start, nblks);
45363ccda647Slclee return (EINVAL);
45373ccda647Slclee }
4538342440ecSPrasad Singamsetty ncyl = (unsigned)vpart->p_start / nblks;
4539342440ecSPrasad Singamsetty ncyl += (unsigned)vpart->p_size / nblks;
4540342440ecSPrasad Singamsetty if (((unsigned)vpart->p_size % nblks) != 0) {
45413ccda647Slclee ncyl++;
45423ccda647Slclee }
4543e8fb11a1Sshidokht if (ncyl > (int)cl->cl_g.dkg_ncyl) {
4544e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
45453ccda647Slclee "cmlb_build_label_vtoc: ncyl %d > dkg_ncyl %d"
45463ccda647Slclee "p_size %ld p_start %ld nblks %d part number %d"
45473ccda647Slclee "tag %d\n",
4548e8fb11a1Sshidokht ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
45493ccda647Slclee vpart->p_start, nblks,
45503ccda647Slclee i, vpart->p_tag);
45513ccda647Slclee
45523ccda647Slclee return (EINVAL);
45533ccda647Slclee }
45543ccda647Slclee vpart++;
45553ccda647Slclee }
45563ccda647Slclee #endif /* defined(_SUNOS_VTOC_8) */
45573ccda647Slclee
45583ccda647Slclee /* Put appropriate vtoc structure fields into the disk label */
45593ccda647Slclee #if defined(_SUNOS_VTOC_16)
45603ccda647Slclee /*
45613ccda647Slclee * The vtoc is always a 32bit data structure to maintain the
45623ccda647Slclee * on-disk format. Convert "in place" instead of doing bcopy.
45633ccda647Slclee */
4564e8fb11a1Sshidokht vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
45653ccda647Slclee
45663ccda647Slclee /*
45673ccda647Slclee * in the 16-slice vtoc, starting sectors are expressed in
45683ccda647Slclee * numbers *relative* to the start of the Solaris fdisk partition.
45693ccda647Slclee */
4570e8fb11a1Sshidokht lmap = cl->cl_map;
45713ccda647Slclee vpart = user_vtoc->v_part;
45723ccda647Slclee
45733ccda647Slclee for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
4574342440ecSPrasad Singamsetty lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4575342440ecSPrasad Singamsetty lmap->dkl_nblk = (unsigned)vpart->p_size;
45763ccda647Slclee }
45773ccda647Slclee
45783ccda647Slclee #elif defined(_SUNOS_VTOC_8)
45793ccda647Slclee
4580e8fb11a1Sshidokht cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
4581e8fb11a1Sshidokht cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
4582e8fb11a1Sshidokht cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
45833ccda647Slclee
4584e8fb11a1Sshidokht cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
4585e8fb11a1Sshidokht cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
45863ccda647Slclee
4587e8fb11a1Sshidokht bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
45883ccda647Slclee
4589e8fb11a1Sshidokht cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
45903ccda647Slclee
45913ccda647Slclee for (i = 0; i < 10; i++)
4592e8fb11a1Sshidokht cl->cl_vtoc.v_reserved[i] = user_vtoc->v_reserved[i];
45933ccda647Slclee
45943ccda647Slclee /*
45953ccda647Slclee * Note the conversion from starting sector number
45963ccda647Slclee * to starting cylinder number.
45973ccda647Slclee * Return error if division results in a remainder.
45983ccda647Slclee */
4599e8fb11a1Sshidokht lmap = cl->cl_map;
4600e8fb11a1Sshidokht lpart = cl->cl_vtoc.v_part;
46013ccda647Slclee vpart = user_vtoc->v_part;
46023ccda647Slclee
46033ccda647Slclee for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
46043ccda647Slclee lpart->p_tag = vpart->p_tag;
46053ccda647Slclee lpart->p_flag = vpart->p_flag;
4606342440ecSPrasad Singamsetty lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4607342440ecSPrasad Singamsetty lmap->dkl_nblk = (unsigned)vpart->p_size;
46083ccda647Slclee
46093ccda647Slclee lmap++;
46103ccda647Slclee lpart++;
46113ccda647Slclee vpart++;
46123ccda647Slclee
46133ccda647Slclee /* (4387723) */
46143ccda647Slclee #ifdef _LP64
46153ccda647Slclee if (user_vtoc->timestamp[i] > TIME32_MAX) {
4616e8fb11a1Sshidokht cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
46173ccda647Slclee } else {
4618e8fb11a1Sshidokht cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
46193ccda647Slclee }
46203ccda647Slclee #else
4621e8fb11a1Sshidokht cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
46223ccda647Slclee #endif
46233ccda647Slclee }
46243ccda647Slclee
4625e8fb11a1Sshidokht bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
46263ccda647Slclee #else
46273ccda647Slclee #error "No VTOC format defined."
46283ccda647Slclee #endif
46293ccda647Slclee return (0);
46303ccda647Slclee }
46313ccda647Slclee
46323ccda647Slclee /*
46333ccda647Slclee * Function: cmlb_clear_efi
46343ccda647Slclee *
46353ccda647Slclee * Description: This routine clears all EFI labels.
46363ccda647Slclee *
4637e8fb11a1Sshidokht * Arguments:
4638e8fb11a1Sshidokht * cl driver soft state (unit) structure
46393ccda647Slclee *
4640e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
4641e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
46423ccda647Slclee * Return Code: void
46433ccda647Slclee */
46443ccda647Slclee static void
4645e8fb11a1Sshidokht cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
46463ccda647Slclee {
46473ccda647Slclee efi_gpt_t *gpt;
46483ccda647Slclee diskaddr_t cap;
46493ccda647Slclee int rval;
46503ccda647Slclee
4651e8fb11a1Sshidokht ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4652e8fb11a1Sshidokht
4653e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
4654e8fb11a1Sshidokht cl->cl_reserved = -1;
4655e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
46563ccda647Slclee
465765908c77Syu, larry liu - Sun Microsystems - Beijing China gpt = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
46583ccda647Slclee
465965908c77Syu, larry liu - Sun Microsystems - Beijing China if (DK_TG_READ(cl, gpt, 1, cl->cl_sys_blocksize, tg_cookie) != 0) {
46603ccda647Slclee goto done;
46613ccda647Slclee }
46623ccda647Slclee
46633ccda647Slclee cmlb_swap_efi_gpt(gpt);
46643ccda647Slclee rval = cmlb_validate_efi(gpt);
46653ccda647Slclee if (rval == 0) {
46663ccda647Slclee /* clear primary */
46673ccda647Slclee bzero(gpt, sizeof (efi_gpt_t));
466865908c77Syu, larry liu - Sun Microsystems - Beijing China if (rval = DK_TG_WRITE(cl, gpt, 1, cl->cl_sys_blocksize,
466965908c77Syu, larry liu - Sun Microsystems - Beijing China tg_cookie)) {
4670e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
46713ccda647Slclee "cmlb_clear_efi: clear primary label failed\n");
46723ccda647Slclee }
46733ccda647Slclee }
46743ccda647Slclee /* the backup */
4675e8fb11a1Sshidokht rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
46763ccda647Slclee if (rval) {
46773ccda647Slclee goto done;
46783ccda647Slclee }
46793ccda647Slclee
468065908c77Syu, larry liu - Sun Microsystems - Beijing China if ((rval = DK_TG_READ(cl, gpt, cap - 1, cl->cl_sys_blocksize,
468165908c77Syu, larry liu - Sun Microsystems - Beijing China tg_cookie)) != 0) {
46823ccda647Slclee goto done;
46833ccda647Slclee }
46843ccda647Slclee cmlb_swap_efi_gpt(gpt);
46853ccda647Slclee rval = cmlb_validate_efi(gpt);
46863ccda647Slclee if (rval == 0) {
46873ccda647Slclee /* clear backup */
4688e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl,
46893ccda647Slclee "cmlb_clear_efi clear backup@%lu\n", cap - 1);
46903ccda647Slclee bzero(gpt, sizeof (efi_gpt_t));
469165908c77Syu, larry liu - Sun Microsystems - Beijing China if ((rval = DK_TG_WRITE(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4692e8fb11a1Sshidokht tg_cookie))) {
4693e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
46943ccda647Slclee "cmlb_clear_efi: clear backup label failed\n");
46953ccda647Slclee }
4696e8fb11a1Sshidokht } else {
4697e8fb11a1Sshidokht /*
4698e8fb11a1Sshidokht * Refer to comments related to off-by-1 at the
4699e8fb11a1Sshidokht * header of this file
4700e8fb11a1Sshidokht */
4701e8fb11a1Sshidokht if ((rval = DK_TG_READ(cl, gpt, cap - 2,
470265908c77Syu, larry liu - Sun Microsystems - Beijing China cl->cl_sys_blocksize, tg_cookie)) != 0) {
4703e8fb11a1Sshidokht goto done;
4704e8fb11a1Sshidokht }
4705e8fb11a1Sshidokht cmlb_swap_efi_gpt(gpt);
4706e8fb11a1Sshidokht rval = cmlb_validate_efi(gpt);
4707e8fb11a1Sshidokht if (rval == 0) {
4708e8fb11a1Sshidokht /* clear legacy backup EFI label */
4709e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl,
4710e8fb11a1Sshidokht "cmlb_clear_efi clear legacy backup@%lu\n",
4711e8fb11a1Sshidokht cap - 2);
4712e8fb11a1Sshidokht bzero(gpt, sizeof (efi_gpt_t));
4713e8fb11a1Sshidokht if ((rval = DK_TG_WRITE(cl, gpt, cap - 2,
471465908c77Syu, larry liu - Sun Microsystems - Beijing China cl->cl_sys_blocksize, tg_cookie))) {
4715e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
4716e8fb11a1Sshidokht "cmlb_clear_efi: clear legacy backup label "
4717e8fb11a1Sshidokht "failed\n");
4718e8fb11a1Sshidokht }
4719e8fb11a1Sshidokht }
47203ccda647Slclee }
47213ccda647Slclee
47223ccda647Slclee done:
472365908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(gpt, cl->cl_sys_blocksize);
47243ccda647Slclee }
47253ccda647Slclee
47263ccda647Slclee /*
47273ccda647Slclee * Function: cmlb_set_vtoc
47283ccda647Slclee *
47293ccda647Slclee * Description: This routine writes data to the appropriate positions
47303ccda647Slclee *
4731e8fb11a1Sshidokht * Arguments:
4732e8fb11a1Sshidokht * cl driver soft state (unit) structure
4733e8fb11a1Sshidokht *
4734e8fb11a1Sshidokht * dkl the data to be written
4735e8fb11a1Sshidokht *
4736e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
4737e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
47383ccda647Slclee *
47393ccda647Slclee * Return: void
47403ccda647Slclee */
47413ccda647Slclee static int
4742e8fb11a1Sshidokht cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
47433ccda647Slclee {
47443ccda647Slclee uint_t label_addr;
47453ccda647Slclee int sec;
4746342440ecSPrasad Singamsetty diskaddr_t blk;
47473ccda647Slclee int head;
47483ccda647Slclee int cyl;
47493ccda647Slclee int rval;
47503ccda647Slclee
47513ccda647Slclee #if defined(__i386) || defined(__amd64)
4752e8fb11a1Sshidokht label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
47533ccda647Slclee #else
47543ccda647Slclee /* Write the primary label at block 0 of the solaris partition. */
47553ccda647Slclee label_addr = 0;
47563ccda647Slclee #endif
47573ccda647Slclee
4758e8fb11a1Sshidokht rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
4759e8fb11a1Sshidokht tg_cookie);
47603ccda647Slclee
47613ccda647Slclee if (rval != 0) {
47623ccda647Slclee return (rval);
47633ccda647Slclee }
47643ccda647Slclee
47653ccda647Slclee /*
47663ccda647Slclee * Calculate where the backup labels go. They are always on
47673ccda647Slclee * the last alternate cylinder, but some older drives put them
47683ccda647Slclee * on head 2 instead of the last head. They are always on the
47693ccda647Slclee * first 5 odd sectors of the appropriate track.
47703ccda647Slclee *
47713ccda647Slclee * We have no choice at this point, but to believe that the
47723ccda647Slclee * disk label is valid. Use the geometry of the disk
47733ccda647Slclee * as described in the label.
47743ccda647Slclee */
47753ccda647Slclee cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1;
47763ccda647Slclee head = dkl->dkl_nhead - 1;
47773ccda647Slclee
47783ccda647Slclee /*
47793ccda647Slclee * Write and verify the backup labels. Make sure we don't try to
47803ccda647Slclee * write past the last cylinder.
47813ccda647Slclee */
47823ccda647Slclee for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
4783342440ecSPrasad Singamsetty blk = (diskaddr_t)(
47843ccda647Slclee (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
47853ccda647Slclee (head * dkl->dkl_nsect) + sec);
47863ccda647Slclee #if defined(__i386) || defined(__amd64)
4787e8fb11a1Sshidokht blk += cl->cl_solaris_offset;
47883ccda647Slclee #endif
4789e8fb11a1Sshidokht rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
4790e8fb11a1Sshidokht tg_cookie);
4791e8fb11a1Sshidokht cmlb_dbg(CMLB_INFO, cl,
4792342440ecSPrasad Singamsetty "cmlb_set_vtoc: wrote backup label %llx\n", blk);
47933ccda647Slclee if (rval != 0) {
47943ccda647Slclee goto exit;
47953ccda647Slclee }
47963ccda647Slclee }
47973ccda647Slclee exit:
47983ccda647Slclee return (rval);
47993ccda647Slclee }
48003ccda647Slclee
48013ccda647Slclee /*
48023ccda647Slclee * Function: cmlb_clear_vtoc
48033ccda647Slclee *
48043ccda647Slclee * Description: This routine clears out the VTOC labels.
48053ccda647Slclee *
4806e8fb11a1Sshidokht * Arguments:
4807e8fb11a1Sshidokht * cl driver soft state (unit) structure
4808e8fb11a1Sshidokht *
4809e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
4810e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
48113ccda647Slclee *
48123ccda647Slclee * Return: void
48133ccda647Slclee */
48143ccda647Slclee static void
4815e8fb11a1Sshidokht cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
48163ccda647Slclee {
48173ccda647Slclee struct dk_label *dkl;
48183ccda647Slclee
4819e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
482065908c77Syu, larry liu - Sun Microsystems - Beijing China dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4821e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
48223ccda647Slclee /*
48233ccda647Slclee * cmlb_set_vtoc uses these fields in order to figure out
48243ccda647Slclee * where to overwrite the backup labels
48253ccda647Slclee */
4826e8fb11a1Sshidokht dkl->dkl_apc = cl->cl_g.dkg_apc;
4827e8fb11a1Sshidokht dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4828e8fb11a1Sshidokht dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4829e8fb11a1Sshidokht dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4830e8fb11a1Sshidokht dkl->dkl_nsect = cl->cl_g.dkg_nsect;
4831e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4832e8fb11a1Sshidokht (void) cmlb_set_vtoc(cl, dkl, tg_cookie);
483365908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(dkl, cl->cl_sys_blocksize);
48343ccda647Slclee
4835e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
48363ccda647Slclee }
48373ccda647Slclee
48383ccda647Slclee /*
48393ccda647Slclee * Function: cmlb_write_label
48403ccda647Slclee *
48413ccda647Slclee * Description: This routine will validate and write the driver soft state vtoc
48423ccda647Slclee * contents to the device.
48433ccda647Slclee *
4844e8fb11a1Sshidokht * Arguments:
4845e8fb11a1Sshidokht * cl cmlb handle
4846e8fb11a1Sshidokht *
4847e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
4848e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
4849e8fb11a1Sshidokht *
48503ccda647Slclee *
48513ccda647Slclee * Return Code: the code returned by cmlb_send_scsi_cmd()
48523ccda647Slclee * 0
48533ccda647Slclee * EINVAL
48543ccda647Slclee * ENXIO
48553ccda647Slclee * ENOMEM
48563ccda647Slclee */
48573ccda647Slclee static int
4858e8fb11a1Sshidokht cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
48593ccda647Slclee {
48603ccda647Slclee struct dk_label *dkl;
48613ccda647Slclee short sum;
48623ccda647Slclee short *sp;
48633ccda647Slclee int i;
48643ccda647Slclee int rval;
48653ccda647Slclee
4866e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4867e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
486865908c77Syu, larry liu - Sun Microsystems - Beijing China dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4869e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
48703ccda647Slclee
4871e8fb11a1Sshidokht bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
4872e8fb11a1Sshidokht dkl->dkl_rpm = cl->cl_g.dkg_rpm;
4873e8fb11a1Sshidokht dkl->dkl_pcyl = cl->cl_g.dkg_pcyl;
4874e8fb11a1Sshidokht dkl->dkl_apc = cl->cl_g.dkg_apc;
4875e8fb11a1Sshidokht dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
4876e8fb11a1Sshidokht dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4877e8fb11a1Sshidokht dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4878e8fb11a1Sshidokht dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4879e8fb11a1Sshidokht dkl->dkl_nsect = cl->cl_g.dkg_nsect;
48803ccda647Slclee
48813ccda647Slclee #if defined(_SUNOS_VTOC_8)
4882e8fb11a1Sshidokht dkl->dkl_obs1 = cl->cl_g.dkg_obs1;
4883e8fb11a1Sshidokht dkl->dkl_obs2 = cl->cl_g.dkg_obs2;
4884e8fb11a1Sshidokht dkl->dkl_obs3 = cl->cl_g.dkg_obs3;
48853ccda647Slclee for (i = 0; i < NDKMAP; i++) {
4886e8fb11a1Sshidokht dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
4887e8fb11a1Sshidokht dkl->dkl_map[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
48883ccda647Slclee }
4889e8fb11a1Sshidokht bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
48903ccda647Slclee #elif defined(_SUNOS_VTOC_16)
4891e8fb11a1Sshidokht dkl->dkl_skew = cl->cl_dkg_skew;
48923ccda647Slclee #else
48933ccda647Slclee #error "No VTOC format defined."
48943ccda647Slclee #endif
48953ccda647Slclee
48963ccda647Slclee dkl->dkl_magic = DKL_MAGIC;
4897e8fb11a1Sshidokht dkl->dkl_write_reinstruct = cl->cl_g.dkg_write_reinstruct;
4898e8fb11a1Sshidokht dkl->dkl_read_reinstruct = cl->cl_g.dkg_read_reinstruct;
48993ccda647Slclee
49003ccda647Slclee /* Construct checksum for the new disk label */
49013ccda647Slclee sum = 0;
49023ccda647Slclee sp = (short *)dkl;
49033ccda647Slclee i = sizeof (struct dk_label) / sizeof (short);
49043ccda647Slclee while (i--) {
49053ccda647Slclee sum ^= *sp++;
49063ccda647Slclee }
49073ccda647Slclee dkl->dkl_cksum = sum;
49083ccda647Slclee
4909e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
49103ccda647Slclee
4911e8fb11a1Sshidokht rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
49123ccda647Slclee exit:
491365908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(dkl, cl->cl_sys_blocksize);
4914e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
49153ccda647Slclee return (rval);
49163ccda647Slclee }
49173ccda647Slclee
4918c31fac72SShidokht Yadegari /*
4919c31fac72SShidokht Yadegari * This routine implements the DKIOCSETEFI ioctl. This ioctl is currently
4920c31fac72SShidokht Yadegari * used to write (or clear) the GPT Partition Table header (primary/backup)
4921c31fac72SShidokht Yadegari * and GUID partition Entry Array (primary/backup). It is also used to write
4922c31fac72SShidokht Yadegari * the Protective MBR.
4923c31fac72SShidokht Yadegari */
49243ccda647Slclee static int
4925e8fb11a1Sshidokht cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4926e8fb11a1Sshidokht void *tg_cookie)
49273ccda647Slclee {
49283ccda647Slclee dk_efi_t user_efi;
49293ccda647Slclee int rval = 0;
49303ccda647Slclee void *buffer;
4931e8fb11a1Sshidokht diskaddr_t tgt_lba;
493206bbe1e0Sedp boolean_t internal;
49333ccda647Slclee
49343ccda647Slclee if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
49353ccda647Slclee return (EFAULT);
49363ccda647Slclee
49377f0b8309SEdward Pilatowicz internal = VOID2BOOLEAN(
49387f0b8309SEdward Pilatowicz (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
493906bbe1e0Sedp
49403ccda647Slclee user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
49413ccda647Slclee
4942de6d0fcdSShidokht Yadegari if (user_efi.dki_length == 0 ||
4943de6d0fcdSShidokht Yadegari user_efi.dki_length > cmlb_tg_max_efi_xfer)
4944c31fac72SShidokht Yadegari return (EINVAL);
4945c31fac72SShidokht Yadegari
4946de6d0fcdSShidokht Yadegari tgt_lba = user_efi.dki_lba;
4947de6d0fcdSShidokht Yadegari
4948de6d0fcdSShidokht Yadegari mutex_enter(CMLB_MUTEX(cl));
4949de6d0fcdSShidokht Yadegari if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4950de6d0fcdSShidokht Yadegari (cl->cl_tgt_blocksize == 0) ||
4951de6d0fcdSShidokht Yadegari (user_efi.dki_length % cl->cl_sys_blocksize)) {
4952de6d0fcdSShidokht Yadegari mutex_exit(CMLB_MUTEX(cl));
4953de6d0fcdSShidokht Yadegari return (EINVAL);
4954de6d0fcdSShidokht Yadegari }
4955de6d0fcdSShidokht Yadegari if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4956de6d0fcdSShidokht Yadegari tgt_lba = tgt_lba *
4957de6d0fcdSShidokht Yadegari cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
4958de6d0fcdSShidokht Yadegari mutex_exit(CMLB_MUTEX(cl));
4959de6d0fcdSShidokht Yadegari
49603ccda647Slclee buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
49613ccda647Slclee if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
49623ccda647Slclee rval = EFAULT;
49633ccda647Slclee } else {
49643ccda647Slclee /*
49653ccda647Slclee * let's clear the vtoc labels and clear the softstate
49663ccda647Slclee * vtoc.
49673ccda647Slclee */
4968e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
4969e8fb11a1Sshidokht if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
4970e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl,
49713ccda647Slclee "cmlb_dkio_set_efi: CLEAR VTOC\n");
4972342440ecSPrasad Singamsetty if (cl->cl_label_from_media == CMLB_LABEL_VTOC)
4973e8fb11a1Sshidokht cmlb_clear_vtoc(cl, tg_cookie);
4974e8fb11a1Sshidokht bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
4975e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4976e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4977e8fb11a1Sshidokht ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
497806bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
49793ccda647Slclee S_IFBLK,
49803ccda647Slclee (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
498106bbe1e0Sedp cl->cl_node_type, NULL, internal);
498206bbe1e0Sedp (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
49833ccda647Slclee S_IFCHR,
49843ccda647Slclee (CMLBUNIT(dev) << CMLBUNIT_SHIFT) | WD_NODE,
498506bbe1e0Sedp cl->cl_node_type, NULL, internal);
49863ccda647Slclee } else
4987e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
4988e8fb11a1Sshidokht
4989e8fb11a1Sshidokht rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
4990e8fb11a1Sshidokht tg_cookie);
4991e8fb11a1Sshidokht
49923ccda647Slclee if (rval == 0) {
4993e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
49947f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
4995e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
49963ccda647Slclee }
49973ccda647Slclee }
49983ccda647Slclee kmem_free(buffer, user_efi.dki_length);
49993ccda647Slclee return (rval);
50003ccda647Slclee }
50013ccda647Slclee
50023ccda647Slclee /*
50033ccda647Slclee * Function: cmlb_dkio_get_mboot
50043ccda647Slclee *
50053ccda647Slclee * Description: This routine is the driver entry point for handling user
50063ccda647Slclee * requests to get the current device mboot (DKIOCGMBOOT)
50073ccda647Slclee *
50083ccda647Slclee * Arguments:
5009e8fb11a1Sshidokht * arg pointer to user provided mboot structure specifying
50103ccda647Slclee * the current mboot.
5011e8fb11a1Sshidokht *
5012e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
50133ccda647Slclee * directly from the mode argument of ioctl().
50143ccda647Slclee *
5015e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
5016e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
5017e8fb11a1Sshidokht *
50183ccda647Slclee * Return Code: 0
50193ccda647Slclee * EINVAL
50203ccda647Slclee * EFAULT
50213ccda647Slclee * ENXIO
50223ccda647Slclee */
50233ccda647Slclee static int
5024e8fb11a1Sshidokht cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
50253ccda647Slclee {
50263ccda647Slclee struct mboot *mboot;
50273ccda647Slclee int rval;
50283ccda647Slclee size_t buffer_size;
50293ccda647Slclee
50303ccda647Slclee
50313ccda647Slclee #if defined(_SUNOS_VTOC_8)
5032e8fb11a1Sshidokht if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
50333ccda647Slclee #elif defined(_SUNOS_VTOC_16)
50343ccda647Slclee if (arg == NULL) {
50353ccda647Slclee #endif
50363ccda647Slclee return (EINVAL);
50373ccda647Slclee }
50383ccda647Slclee
50393ccda647Slclee /*
50403ccda647Slclee * Read the mboot block, located at absolute block 0 on the target.
50413ccda647Slclee */
504265908c77Syu, larry liu - Sun Microsystems - Beijing China buffer_size = cl->cl_sys_blocksize;
50433ccda647Slclee
5044e8fb11a1Sshidokht cmlb_dbg(CMLB_TRACE, cl,
50453ccda647Slclee "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
50463ccda647Slclee
50473ccda647Slclee mboot = kmem_zalloc(buffer_size, KM_SLEEP);
5048e8fb11a1Sshidokht if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
50493ccda647Slclee if (ddi_copyout(mboot, (void *)arg,
50503ccda647Slclee sizeof (struct mboot), flag) != 0) {
50513ccda647Slclee rval = EFAULT;
50523ccda647Slclee }
50533ccda647Slclee }
50543ccda647Slclee kmem_free(mboot, buffer_size);
50553ccda647Slclee return (rval);
50563ccda647Slclee }
50573ccda647Slclee
50583ccda647Slclee
50593ccda647Slclee /*
50603ccda647Slclee * Function: cmlb_dkio_set_mboot
50613ccda647Slclee *
50623ccda647Slclee * Description: This routine is the driver entry point for handling user
50633ccda647Slclee * requests to validate and set the device master boot
50643ccda647Slclee * (DKIOCSMBOOT).
50653ccda647Slclee *
50663ccda647Slclee * Arguments:
5067e8fb11a1Sshidokht * arg pointer to user provided mboot structure used to set the
50683ccda647Slclee * master boot.
5069e8fb11a1Sshidokht *
5070e8fb11a1Sshidokht * flag this argument is a pass through to ddi_copyxxx()
50713ccda647Slclee * directly from the mode argument of ioctl().
50723ccda647Slclee *
5073e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
5074e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
5075e8fb11a1Sshidokht *
50763ccda647Slclee * Return Code: 0
50773ccda647Slclee * EINVAL
50783ccda647Slclee * EFAULT
50793ccda647Slclee * ENXIO
50803ccda647Slclee */
50813ccda647Slclee static int
5082e8fb11a1Sshidokht cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
50833ccda647Slclee {
50843ccda647Slclee struct mboot *mboot = NULL;
50853ccda647Slclee int rval;
50863ccda647Slclee ushort_t magic;
50873ccda647Slclee
50883ccda647Slclee
5089e8fb11a1Sshidokht ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
50903ccda647Slclee
50913ccda647Slclee #if defined(_SUNOS_VTOC_8)
5092e8fb11a1Sshidokht if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
50933ccda647Slclee return (EINVAL);
50943ccda647Slclee }
50953ccda647Slclee #endif
50963ccda647Slclee
50973ccda647Slclee if (arg == NULL) {
50983ccda647Slclee return (EINVAL);
50993ccda647Slclee }
51003ccda647Slclee
510165908c77Syu, larry liu - Sun Microsystems - Beijing China mboot = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
51023ccda647Slclee
51033ccda647Slclee if (ddi_copyin((const void *)arg, mboot,
510465908c77Syu, larry liu - Sun Microsystems - Beijing China cl->cl_sys_blocksize, flag) != 0) {
510565908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(mboot, cl->cl_sys_blocksize);
51063ccda647Slclee return (EFAULT);
51073ccda647Slclee }
51083ccda647Slclee
51093ccda647Slclee /* Is this really a master boot record? */
51103ccda647Slclee magic = LE_16(mboot->signature);
51113ccda647Slclee if (magic != MBB_MAGIC) {
511265908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(mboot, cl->cl_sys_blocksize);
51133ccda647Slclee return (EINVAL);
51143ccda647Slclee }
51153ccda647Slclee
5116e8fb11a1Sshidokht rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
51173ccda647Slclee
5118e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
51193ccda647Slclee #if defined(__i386) || defined(__amd64)
51203ccda647Slclee if (rval == 0) {
51213ccda647Slclee /*
51223ccda647Slclee * mboot has been written successfully.
51233ccda647Slclee * update the fdisk and vtoc tables in memory
51243ccda647Slclee */
5125e8fb11a1Sshidokht rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
51267f0b8309SEdward Pilatowicz if ((!cl->cl_f_geometry_is_valid) || (rval != 0)) {
5127e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
512865908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(mboot, cl->cl_sys_blocksize);
51293ccda647Slclee return (rval);
51303ccda647Slclee }
51313ccda647Slclee }
5132e8fb11a1Sshidokht
5133e8fb11a1Sshidokht #ifdef __lock_lint
5134e8fb11a1Sshidokht cmlb_setup_default_geometry(cl, tg_cookie);
5135e8fb11a1Sshidokht #endif
5136e8fb11a1Sshidokht
51373ccda647Slclee #else
51383ccda647Slclee if (rval == 0) {
51393ccda647Slclee /*
51403ccda647Slclee * mboot has been written successfully.
51413ccda647Slclee * set up the default geometry and VTOC
51423ccda647Slclee */
5143342440ecSPrasad Singamsetty if (cl->cl_blockcount <= CMLB_EXTVTOC_LIMIT)
5144e8fb11a1Sshidokht cmlb_setup_default_geometry(cl, tg_cookie);
51453ccda647Slclee }
51463ccda647Slclee #endif
5147342440ecSPrasad Singamsetty cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
5148e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
514965908c77Syu, larry liu - Sun Microsystems - Beijing China kmem_free(mboot, cl->cl_sys_blocksize);
51503ccda647Slclee return (rval);
51513ccda647Slclee }
51523ccda647Slclee
51533ccda647Slclee
5154aa1b14e7SSheshadri Vasudevan #if defined(__i386) || defined(__amd64)
5155aa1b14e7SSheshadri Vasudevan /*ARGSUSED*/
5156aa1b14e7SSheshadri Vasudevan static int
5157aa1b14e7SSheshadri Vasudevan cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
5158aa1b14e7SSheshadri Vasudevan void *tg_cookie)
5159aa1b14e7SSheshadri Vasudevan {
5160aa1b14e7SSheshadri Vasudevan int fdisk_rval;
5161aa1b14e7SSheshadri Vasudevan diskaddr_t capacity;
5162aa1b14e7SSheshadri Vasudevan
5163aa1b14e7SSheshadri Vasudevan ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5164aa1b14e7SSheshadri Vasudevan
5165aa1b14e7SSheshadri Vasudevan mutex_enter(CMLB_MUTEX(cl));
5166aa1b14e7SSheshadri Vasudevan capacity = cl->cl_blockcount;
5167aa1b14e7SSheshadri Vasudevan fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5168aa1b14e7SSheshadri Vasudevan if (fdisk_rval != 0) {
5169aa1b14e7SSheshadri Vasudevan mutex_exit(CMLB_MUTEX(cl));
5170aa1b14e7SSheshadri Vasudevan return (fdisk_rval);
5171aa1b14e7SSheshadri Vasudevan }
5172aa1b14e7SSheshadri Vasudevan
5173aa1b14e7SSheshadri Vasudevan mutex_exit(CMLB_MUTEX(cl));
5174aa1b14e7SSheshadri Vasudevan return (fdisk_rval);
5175aa1b14e7SSheshadri Vasudevan }
5176aa1b14e7SSheshadri Vasudevan #endif
5177aa1b14e7SSheshadri Vasudevan
51783ccda647Slclee /*
51793ccda647Slclee * Function: cmlb_setup_default_geometry
51803ccda647Slclee *
51813ccda647Slclee * Description: This local utility routine sets the default geometry as part of
51823ccda647Slclee * setting the device mboot.
51833ccda647Slclee *
5184e8fb11a1Sshidokht * Arguments:
5185e8fb11a1Sshidokht * cl driver soft state (unit) structure
5186e8fb11a1Sshidokht *
5187e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
5188e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
5189e8fb11a1Sshidokht *
51903ccda647Slclee *
51913ccda647Slclee * Note: This may be redundant with cmlb_build_default_label.
51923ccda647Slclee */
51933ccda647Slclee static void
5194e8fb11a1Sshidokht cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
51953ccda647Slclee {
51963ccda647Slclee struct cmlb_geom pgeom;
51973ccda647Slclee struct cmlb_geom *pgeomp = &pgeom;
51983ccda647Slclee int ret;
51993ccda647Slclee int geom_base_cap = 1;
52003ccda647Slclee
52013ccda647Slclee
5202e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
52033ccda647Slclee
52043ccda647Slclee /* zero out the soft state geometry and partition table. */
5205e8fb11a1Sshidokht bzero(&cl->cl_g, sizeof (struct dk_geom));
5206e8fb11a1Sshidokht bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5207e8fb11a1Sshidokht bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
52083ccda647Slclee
52093ccda647Slclee /*
52103ccda647Slclee * For the rpm, we use the minimum for the disk.
52113ccda647Slclee * For the head, cyl and number of sector per track,
52123ccda647Slclee * if the capacity <= 1GB, head = 64, sect = 32.
52133ccda647Slclee * else head = 255, sect 63
52143ccda647Slclee * Note: the capacity should be equal to C*H*S values.
52153ccda647Slclee * This will cause some truncation of size due to
52163ccda647Slclee * round off errors. For CD-ROMs, this truncation can
52173ccda647Slclee * have adverse side effects, so returning ncyl and
52183ccda647Slclee * nhead as 1. The nsect will overflow for most of
52193ccda647Slclee * CD-ROMs as nsect is of type ushort.
52203ccda647Slclee */
5221e8fb11a1Sshidokht if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
52223ccda647Slclee /*
52233ccda647Slclee * newfs currently can not handle 255 ntracks for SPARC
52243ccda647Slclee * so get the geometry from target driver instead of coming up
52253ccda647Slclee * with one based on capacity.
52263ccda647Slclee */
5227e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
5228e8fb11a1Sshidokht ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
5229e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
52303ccda647Slclee
52313ccda647Slclee if (ret == 0) {
52323ccda647Slclee geom_base_cap = 0;
52333ccda647Slclee } else {
5234e8fb11a1Sshidokht cmlb_dbg(CMLB_ERROR, cl,
52353ccda647Slclee "cmlb_setup_default_geometry: "
52363ccda647Slclee "tg_getphygeom failed %d\n", ret);
52373ccda647Slclee
52383ccda647Slclee /* do default setting, geometry based on capacity */
52393ccda647Slclee }
52403ccda647Slclee }
52413ccda647Slclee
52423ccda647Slclee if (geom_base_cap) {
5243e8fb11a1Sshidokht if (ISCD(cl)) {
5244e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = 1;
5245e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 1;
5246e8fb11a1Sshidokht cl->cl_g.dkg_nsect = cl->cl_blockcount;
5247c31fac72SShidokht Yadegari } else if (cl->cl_blockcount < 160) {
5248c31fac72SShidokht Yadegari /* Less than 80K */
5249c31fac72SShidokht Yadegari cl->cl_g.dkg_nhead = 1;
5250c31fac72SShidokht Yadegari cl->cl_g.dkg_ncyl = cl->cl_blockcount;
5251c31fac72SShidokht Yadegari cl->cl_g.dkg_nsect = 1;
5252e8fb11a1Sshidokht } else if (cl->cl_blockcount <= 0x1000) {
52533ccda647Slclee /* Needed for unlabeled SCSI floppies. */
5254e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 2;
5255e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = 80;
5256e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = 80;
5257e8fb11a1Sshidokht cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
5258e8fb11a1Sshidokht } else if (cl->cl_blockcount <= 0x200000) {
5259e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 64;
5260e8fb11a1Sshidokht cl->cl_g.dkg_nsect = 32;
5261e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
52623ccda647Slclee } else {
5263e8fb11a1Sshidokht cl->cl_g.dkg_nhead = 255;
526498a65dd4Sshidokht
526598a65dd4Sshidokht cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
526698a65dd4Sshidokht (UINT16_MAX * 255 * 63) - 1) /
526798a65dd4Sshidokht (UINT16_MAX * 255 * 63)) * 63;
526898a65dd4Sshidokht
526998a65dd4Sshidokht if (cl->cl_g.dkg_nsect == 0)
527098a65dd4Sshidokht cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
527198a65dd4Sshidokht
527298a65dd4Sshidokht cl->cl_g.dkg_ncyl = cl->cl_blockcount /
527398a65dd4Sshidokht (255 * cl->cl_g.dkg_nsect);
52743ccda647Slclee }
52753ccda647Slclee
5276e8fb11a1Sshidokht cl->cl_g.dkg_acyl = 0;
5277e8fb11a1Sshidokht cl->cl_g.dkg_bcyl = 0;
5278e8fb11a1Sshidokht cl->cl_g.dkg_intrlv = 1;
5279e8fb11a1Sshidokht cl->cl_g.dkg_rpm = 200;
5280e8fb11a1Sshidokht if (cl->cl_g.dkg_pcyl == 0)
5281e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
5282e8fb11a1Sshidokht cl->cl_g.dkg_acyl;
52833ccda647Slclee } else {
5284e8fb11a1Sshidokht cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
5285e8fb11a1Sshidokht cl->cl_g.dkg_acyl = pgeomp->g_acyl;
5286e8fb11a1Sshidokht cl->cl_g.dkg_nhead = pgeomp->g_nhead;
5287e8fb11a1Sshidokht cl->cl_g.dkg_nsect = pgeomp->g_nsect;
5288e8fb11a1Sshidokht cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
5289e8fb11a1Sshidokht cl->cl_g.dkg_rpm = pgeomp->g_rpm;
5290e8fb11a1Sshidokht cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
52913ccda647Slclee }
52923ccda647Slclee
5293e8fb11a1Sshidokht cl->cl_g.dkg_read_reinstruct = 0;
5294e8fb11a1Sshidokht cl->cl_g.dkg_write_reinstruct = 0;
5295e8fb11a1Sshidokht cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
5296e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
52973ccda647Slclee
5298e8fb11a1Sshidokht cl->cl_map['a'-'a'].dkl_cylno = 0;
5299e8fb11a1Sshidokht cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
53003ccda647Slclee
5301e8fb11a1Sshidokht cl->cl_map['c'-'a'].dkl_cylno = 0;
5302e8fb11a1Sshidokht cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
53033ccda647Slclee
5304e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
5305e8fb11a1Sshidokht cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
5306e8fb11a1Sshidokht cl->cl_vtoc.v_nparts = V_NUMPAR;
5307e8fb11a1Sshidokht cl->cl_vtoc.v_version = V_VERSION;
5308e8fb11a1Sshidokht (void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
5309e8fb11a1Sshidokht " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
5310e8fb11a1Sshidokht cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
53113ccda647Slclee
53127f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_FALSE;
53133ccda647Slclee }
53143ccda647Slclee
53153ccda647Slclee
53163ccda647Slclee #if defined(__i386) || defined(__amd64)
53173ccda647Slclee /*
53183ccda647Slclee * Function: cmlb_update_fdisk_and_vtoc
53193ccda647Slclee *
53203ccda647Slclee * Description: This local utility routine updates the device fdisk and vtoc
53213ccda647Slclee * as part of setting the device mboot.
53223ccda647Slclee *
5323e8fb11a1Sshidokht * Arguments:
5324e8fb11a1Sshidokht * cl driver soft state (unit) structure
5325e8fb11a1Sshidokht *
5326e8fb11a1Sshidokht * tg_cookie cookie from target driver to be passed back to target
5327e8fb11a1Sshidokht * driver when we call back to it through tg_ops.
5328e8fb11a1Sshidokht *
53293ccda647Slclee *
53303ccda647Slclee * Return Code: 0 for success or errno-type return code.
53313ccda647Slclee *
53323ccda647Slclee * Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
53333ccda647Slclee * these did exist separately in x86 sd.c.
53343ccda647Slclee */
53353ccda647Slclee static int
5336e8fb11a1Sshidokht cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
53373ccda647Slclee {
53383ccda647Slclee int count;
53393ccda647Slclee int label_rc = 0;
53403ccda647Slclee int fdisk_rval;
53413ccda647Slclee diskaddr_t capacity;
53423ccda647Slclee
5343e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
53443ccda647Slclee
5345e8fb11a1Sshidokht if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
53463ccda647Slclee return (EINVAL);
53473ccda647Slclee
53483ccda647Slclee #if defined(_SUNOS_VTOC_16)
53493ccda647Slclee /*
53503ccda647Slclee * Set up the "whole disk" fdisk partition; this should always
53513ccda647Slclee * exist, regardless of whether the disk contains an fdisk table
53523ccda647Slclee * or vtoc.
53533ccda647Slclee */
5354e8fb11a1Sshidokht cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
5355e8fb11a1Sshidokht cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
53563ccda647Slclee #endif /* defined(_SUNOS_VTOC_16) */
53573ccda647Slclee
53583ccda647Slclee /*
53593ccda647Slclee * copy the lbasize and capacity so that if they're
5360e8fb11a1Sshidokht * reset while we're not holding the CMLB_MUTEX(cl), we will
5361e8fb11a1Sshidokht * continue to use valid values after the CMLB_MUTEX(cl) is
53623ccda647Slclee * reacquired.
53633ccda647Slclee */
5364e8fb11a1Sshidokht capacity = cl->cl_blockcount;
53653ccda647Slclee
53663ccda647Slclee /*
53673ccda647Slclee * refresh the logical and physical geometry caches.
53683ccda647Slclee * (data from mode sense format/rigid disk geometry pages,
53693ccda647Slclee * and scsi_ifgetcap("geometry").
53703ccda647Slclee */
5371e8fb11a1Sshidokht cmlb_resync_geom_caches(cl, capacity, tg_cookie);
53723ccda647Slclee
53733ccda647Slclee /*
5374e8fb11a1Sshidokht * Only DIRECT ACCESS devices will have Scl labels.
5375e8fb11a1Sshidokht * CD's supposedly have a Scl label, too
53763ccda647Slclee */
5377e8fb11a1Sshidokht if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
5378e8fb11a1Sshidokht fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
53793ccda647Slclee if (fdisk_rval != 0) {
5380e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
53813ccda647Slclee return (fdisk_rval);
53823ccda647Slclee }
53833ccda647Slclee
5384e8fb11a1Sshidokht if (cl->cl_solaris_size <= DK_LABEL_LOC) {
53853ccda647Slclee /*
53863ccda647Slclee * Found fdisk table but no Solaris partition entry,
53873ccda647Slclee * so don't call cmlb_uselabel() and don't create
53883ccda647Slclee * a default label.
53893ccda647Slclee */
53903ccda647Slclee label_rc = 0;
53917f0b8309SEdward Pilatowicz cl->cl_f_geometry_is_valid = B_TRUE;
53923ccda647Slclee goto no_solaris_partition;
53933ccda647Slclee }
53943ccda647Slclee } else if (capacity < 0) {
5395e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
53963ccda647Slclee return (EINVAL);
53973ccda647Slclee }
53983ccda647Slclee
53993ccda647Slclee /*
54003ccda647Slclee * For Removable media We reach here if we have found a
54013ccda647Slclee * SOLARIS PARTITION.
54027f0b8309SEdward Pilatowicz * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
54033ccda647Slclee * PARTITION has changed from the previous one, hence we will setup a
54043ccda647Slclee * default VTOC in this case.
54053ccda647Slclee */
54067f0b8309SEdward Pilatowicz if (!cl->cl_f_geometry_is_valid) {
54073ccda647Slclee /* if we get here it is writable */
54083ccda647Slclee /* we are called from SMBOOT, and after a write of fdisk */
5409e8fb11a1Sshidokht cmlb_build_default_label(cl, tg_cookie);
54103ccda647Slclee label_rc = 0;
54113ccda647Slclee }
54123ccda647Slclee
54133ccda647Slclee no_solaris_partition:
54143ccda647Slclee
54153ccda647Slclee #if defined(_SUNOS_VTOC_16)
54163ccda647Slclee /*
54173ccda647Slclee * If we have valid geometry, set up the remaining fdisk partitions.
54183ccda647Slclee * Note that dkl_cylno is not used for the fdisk map entries, so
54193ccda647Slclee * we set it to an entirely bogus value.
54203ccda647Slclee */
5421aa1b14e7SSheshadri Vasudevan for (count = 0; count < FDISK_PARTS; count++) {
5422342440ecSPrasad Singamsetty cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT32_MAX;
5423e8fb11a1Sshidokht cl->cl_map[FDISK_P1 + count].dkl_nblk =
5424e8fb11a1Sshidokht cl->cl_fmap[count].fmap_nblk;
5425e8fb11a1Sshidokht cl->cl_offset[FDISK_P1 + count] =
5426e8fb11a1Sshidokht cl->cl_fmap[count].fmap_start;
54273ccda647Slclee }
54283ccda647Slclee #endif
54293ccda647Slclee
54303ccda647Slclee for (count = 0; count < NDKMAP; count++) {
54313ccda647Slclee #if defined(_SUNOS_VTOC_8)
5432e8fb11a1Sshidokht struct dk_map *lp = &cl->cl_map[count];
5433e8fb11a1Sshidokht cl->cl_offset[count] =
5434e8fb11a1Sshidokht cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
54353ccda647Slclee #elif defined(_SUNOS_VTOC_16)
5436e8fb11a1Sshidokht struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
5437e8fb11a1Sshidokht cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
54383ccda647Slclee #else
54393ccda647Slclee #error "No VTOC format defined."
54403ccda647Slclee #endif
54413ccda647Slclee }
54423ccda647Slclee
5443e8fb11a1Sshidokht ASSERT(mutex_owned(CMLB_MUTEX(cl)));
54443ccda647Slclee return (label_rc);
54453ccda647Slclee }
54463ccda647Slclee #endif
54473ccda647Slclee
54483ccda647Slclee #if defined(__i386) || defined(__amd64)
54493ccda647Slclee static int
5450e8fb11a1Sshidokht cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
54513ccda647Slclee {
54523ccda647Slclee int err = 0;
54533ccda647Slclee
54543ccda647Slclee /* Return the driver's notion of the media's logical geometry */
54553ccda647Slclee struct dk_geom disk_geom;
54563ccda647Slclee struct dk_geom *dkgp = &disk_geom;
54573ccda647Slclee
5458e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
54593ccda647Slclee /*
54603ccda647Slclee * If there is no HBA geometry available, or
54613ccda647Slclee * if the HBA returned us something that doesn't
54623ccda647Slclee * really fit into an Int 13/function 8 geometry
54633ccda647Slclee * result, just fail the ioctl. See PSARC 1998/313.
54643ccda647Slclee */
5465e8fb11a1Sshidokht if (cl->cl_lgeom.g_nhead == 0 ||
5466e8fb11a1Sshidokht cl->cl_lgeom.g_nsect == 0 ||
5467e8fb11a1Sshidokht cl->cl_lgeom.g_ncyl > 1024) {
5468e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
54693ccda647Slclee err = EINVAL;
54703ccda647Slclee } else {
5471e8fb11a1Sshidokht dkgp->dkg_ncyl = cl->cl_lgeom.g_ncyl;
5472e8fb11a1Sshidokht dkgp->dkg_acyl = cl->cl_lgeom.g_acyl;
54733ccda647Slclee dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5474e8fb11a1Sshidokht dkgp->dkg_nhead = cl->cl_lgeom.g_nhead;
5475e8fb11a1Sshidokht dkgp->dkg_nsect = cl->cl_lgeom.g_nsect;
54763ccda647Slclee
54771c97599bSshidokht mutex_exit(CMLB_MUTEX(cl));
54783ccda647Slclee if (ddi_copyout(dkgp, (void *)arg,
54793ccda647Slclee sizeof (struct dk_geom), flag)) {
54803ccda647Slclee err = EFAULT;
54813ccda647Slclee } else {
54823ccda647Slclee err = 0;
54833ccda647Slclee }
54843ccda647Slclee }
54853ccda647Slclee return (err);
54863ccda647Slclee }
54873ccda647Slclee #endif
54883ccda647Slclee
54893ccda647Slclee #if defined(__i386) || defined(__amd64)
54903ccda647Slclee static int
5491b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
5492b081f1c4Syu, larry liu - Sun Microsystems - Beijing China void *tg_cookie)
54933ccda647Slclee {
54943ccda647Slclee int err = 0;
5495e8fb11a1Sshidokht diskaddr_t capacity;
54963ccda647Slclee
54973ccda647Slclee
54983ccda647Slclee /* Return the driver's notion of the media physical geometry */
54993ccda647Slclee struct dk_geom disk_geom;
55003ccda647Slclee struct dk_geom *dkgp = &disk_geom;
55013ccda647Slclee
5502e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
55033ccda647Slclee
5504e8fb11a1Sshidokht if (cl->cl_g.dkg_nhead != 0 &&
5505e8fb11a1Sshidokht cl->cl_g.dkg_nsect != 0) {
55063ccda647Slclee /*
55073ccda647Slclee * We succeeded in getting a geometry, but
55083ccda647Slclee * right now it is being reported as just the
55093ccda647Slclee * Solaris fdisk partition, just like for
55103ccda647Slclee * DKIOCGGEOM. We need to change that to be
55113ccda647Slclee * correct for the entire disk now.
55123ccda647Slclee */
5513e8fb11a1Sshidokht bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
55143ccda647Slclee dkgp->dkg_acyl = 0;
5515e8fb11a1Sshidokht dkgp->dkg_ncyl = cl->cl_blockcount /
55163ccda647Slclee (dkgp->dkg_nhead * dkgp->dkg_nsect);
55173ccda647Slclee } else {
55183ccda647Slclee bzero(dkgp, sizeof (struct dk_geom));
55193ccda647Slclee /*
55203ccda647Slclee * This disk does not have a Solaris VTOC
55213ccda647Slclee * so we must present a physical geometry
55223ccda647Slclee * that will remain consistent regardless
55233ccda647Slclee * of how the disk is used. This will ensure
55243ccda647Slclee * that the geometry does not change regardless
55253ccda647Slclee * of the fdisk partition type (ie. EFI, FAT32,
55263ccda647Slclee * Solaris, etc).
55273ccda647Slclee */
5528e8fb11a1Sshidokht if (ISCD(cl)) {
5529e8fb11a1Sshidokht dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
5530e8fb11a1Sshidokht dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
5531e8fb11a1Sshidokht dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
5532e8fb11a1Sshidokht dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
55333ccda647Slclee } else {
5534e8fb11a1Sshidokht /*
5535e8fb11a1Sshidokht * Invalid cl_blockcount can generate invalid
5536e8fb11a1Sshidokht * dk_geom and may result in division by zero
5537e8fb11a1Sshidokht * system failure. Should make sure blockcount
5538e8fb11a1Sshidokht * is valid before using it here.
5539e8fb11a1Sshidokht */
5540e8fb11a1Sshidokht if (cl->cl_blockcount == 0) {
5541e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
5542e8fb11a1Sshidokht err = EIO;
5543e8fb11a1Sshidokht return (err);
5544e8fb11a1Sshidokht }
5545e8fb11a1Sshidokht /*
5546e8fb11a1Sshidokht * Refer to comments related to off-by-1 at the
5547e8fb11a1Sshidokht * header of this file
5548e8fb11a1Sshidokht */
5549e8fb11a1Sshidokht if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
5550e8fb11a1Sshidokht capacity = cl->cl_blockcount - 1;
5551e8fb11a1Sshidokht else
5552e8fb11a1Sshidokht capacity = cl->cl_blockcount;
5553e8fb11a1Sshidokht
5554b081f1c4Syu, larry liu - Sun Microsystems - Beijing China cmlb_convert_geometry(cl, capacity, dkgp, tg_cookie);
55553ccda647Slclee dkgp->dkg_acyl = 0;
5556e8fb11a1Sshidokht dkgp->dkg_ncyl = capacity /
55573ccda647Slclee (dkgp->dkg_nhead * dkgp->dkg_nsect);
55583ccda647Slclee }
55593ccda647Slclee }
55603ccda647Slclee dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
55613ccda647Slclee
5562e8fb11a1Sshidokht mutex_exit(CMLB_MUTEX(cl));
55631c97599bSshidokht if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
55643ccda647Slclee err = EFAULT;
55651c97599bSshidokht
55663ccda647Slclee return (err);
55673ccda647Slclee }
55683ccda647Slclee #endif
55693ccda647Slclee
55703ccda647Slclee #if defined(__i386) || defined(__amd64)
55713ccda647Slclee static int
5572e8fb11a1Sshidokht cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
55733ccda647Slclee {
55743ccda647Slclee int err = 0;
55753ccda647Slclee
55763ccda647Slclee /*
55773ccda647Slclee * Return parameters describing the selected disk slice.
55783ccda647Slclee * Note: this ioctl is for the intel platform only
55793ccda647Slclee */
55803ccda647Slclee int part;
55813ccda647Slclee
55823ccda647Slclee part = CMLBPART(dev);
55833ccda647Slclee
5584e8fb11a1Sshidokht mutex_enter(CMLB_MUTEX(cl));
5585e8fb11a1Sshidokht /* don't check cl_solaris_size for pN */
5586e8fb11a1Sshidokht if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
55873ccda647Slclee err = EIO;
55881c97599bSshidokht mutex_exit(CMLB_MUTEX(cl));
55893ccda647Slclee } else {
55903ccda647Slclee struct part_info p;
55913ccda647Slclee
5592e8fb11a1Sshidokht p.p_start = (daddr_t)cl->cl_offset[part];
5593e8fb11a1Sshidokht p.p_length = (int)cl->cl_map[part].dkl_nblk;
55941c97599bSshidokht mutex_exit(CMLB_MUTEX(cl));
55953ccda647Slclee #ifdef _MULTI_DATAMODEL
55963ccda647Slclee switch (ddi_model_convert_from(flag & FMODELS)) {
55973ccda647Slclee case DDI_MODEL_ILP32:
55983ccda647Slclee {
55993ccda647Slclee struct part_info32 p32;
56003ccda647Slclee
56013ccda647Slclee p32.p_start = (daddr32_t)p.p_start;
56023ccda647Slclee p32.p_length = p.p_length;
56033ccda647Slclee if (ddi_copyout(&p32, (void *)arg,
56043ccda647Slclee sizeof (p32), flag))
56053ccda647Slclee err = EFAULT;
56063ccda647Slclee break;
56073ccda647Slclee }
56083ccda647Slclee
56093ccda647Slclee case DDI_MODEL_NONE:
56103ccda647Slclee {
56113ccda647Slclee if (ddi_copyout(&p, (void *)arg, sizeof (p),
56123ccda647Slclee flag))
56133ccda647Slclee err = EFAULT;
56143ccda647Slclee break;
56153ccda647Slclee }
56163ccda647Slclee }
56173ccda647Slclee #else /* ! _MULTI_DATAMODEL */
56183ccda647Slclee if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
56193ccda647Slclee err = EFAULT;
56203ccda647Slclee #endif /* _MULTI_DATAMODEL */
56213ccda647Slclee }
56223ccda647Slclee return (err);
56233ccda647Slclee }
5624342440ecSPrasad Singamsetty static int
5625342440ecSPrasad Singamsetty cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
5626342440ecSPrasad Singamsetty {
5627342440ecSPrasad Singamsetty int err = 0;
5628342440ecSPrasad Singamsetty
5629342440ecSPrasad Singamsetty /*
5630342440ecSPrasad Singamsetty * Return parameters describing the selected disk slice.
5631342440ecSPrasad Singamsetty * Note: this ioctl is for the intel platform only
5632342440ecSPrasad Singamsetty */
5633342440ecSPrasad Singamsetty int part;
5634342440ecSPrasad Singamsetty
5635342440ecSPrasad Singamsetty part = CMLBPART(dev);
5636342440ecSPrasad Singamsetty
5637342440ecSPrasad Singamsetty mutex_enter(CMLB_MUTEX(cl));
5638342440ecSPrasad Singamsetty /* don't check cl_solaris_size for pN */
5639342440ecSPrasad Singamsetty if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5640342440ecSPrasad Singamsetty err = EIO;
5641342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
5642342440ecSPrasad Singamsetty } else {
5643342440ecSPrasad Singamsetty struct extpart_info p;
5644342440ecSPrasad Singamsetty
5645342440ecSPrasad Singamsetty p.p_start = (diskaddr_t)cl->cl_offset[part];
5646342440ecSPrasad Singamsetty p.p_length = (diskaddr_t)cl->cl_map[part].dkl_nblk;
5647342440ecSPrasad Singamsetty mutex_exit(CMLB_MUTEX(cl));
5648342440ecSPrasad Singamsetty if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5649342440ecSPrasad Singamsetty err = EFAULT;
5650342440ecSPrasad Singamsetty }
5651342440ecSPrasad Singamsetty return (err);
5652342440ecSPrasad Singamsetty }
56533ccda647Slclee #endif
5654b9ccdc5aScth
5655b9ccdc5aScth int
5656b9ccdc5aScth cmlb_prop_op(cmlb_handle_t cmlbhandle,
5657b9ccdc5aScth dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
5658b9ccdc5aScth char *name, caddr_t valuep, int *lengthp, int part, void *tg_cookie)
5659b9ccdc5aScth {
5660b9ccdc5aScth struct cmlb_lun *cl;
5661b9ccdc5aScth diskaddr_t capacity;
5662b9ccdc5aScth uint32_t lbasize;
5663*89c81bb4SYuri Pankov enum dp { DP_NBLOCKS, DP_BLKSIZE, DP_SSD, DP_ROT } dp;
5664b9ccdc5aScth int callers_length;
5665b9ccdc5aScth caddr_t buffer;
5666b9ccdc5aScth uint64_t nblocks64;
5667b9ccdc5aScth uint_t dblk;
5668fc2d3b38SGarrett D'Amore tg_attribute_t tgattr;
5669b9ccdc5aScth
5670b9ccdc5aScth /* Always fallback to ddi_prop_op... */
5671b9ccdc5aScth cl = (struct cmlb_lun *)cmlbhandle;
5672b9ccdc5aScth if (cl == NULL) {
5673b9ccdc5aScth fallback: return (ddi_prop_op(dev, dip, prop_op, mod_flags,
5674b9ccdc5aScth name, valuep, lengthp));
5675b9ccdc5aScth }
5676b9ccdc5aScth
5677b9ccdc5aScth /* Pick up capacity and blocksize information. */
5678b9ccdc5aScth capacity = cl->cl_blockcount;
5679b9ccdc5aScth if (capacity == 0)
5680b9ccdc5aScth goto fallback;
5681b9ccdc5aScth lbasize = cl->cl_tgt_blocksize;
5682b9ccdc5aScth if (lbasize == 0)
5683b9ccdc5aScth lbasize = DEV_BSIZE; /* 0 -> DEV_BSIZE units */
5684b9ccdc5aScth
5685b9ccdc5aScth /* Check for dynamic property of whole device. */
5686b9ccdc5aScth if (dev == DDI_DEV_T_ANY) {
5687b9ccdc5aScth /* Fallback to ddi_prop_op if we don't understand. */
5688b9ccdc5aScth if (strcmp(name, "device-nblocks") == 0)
5689b9ccdc5aScth dp = DP_NBLOCKS;
5690b9ccdc5aScth else if (strcmp(name, "device-blksize") == 0)
5691b9ccdc5aScth dp = DP_BLKSIZE;
5692fc2d3b38SGarrett D'Amore else if (strcmp(name, "device-solid-state") == 0)
5693fc2d3b38SGarrett D'Amore dp = DP_SSD;
5694*89c81bb4SYuri Pankov else if (strcmp(name, "device-rotational") == 0)
5695*89c81bb4SYuri Pankov dp = DP_ROT;
5696b9ccdc5aScth else
5697b9ccdc5aScth goto fallback;
5698b9ccdc5aScth
5699b9ccdc5aScth /* get callers length, establish length of our dynamic prop */
5700b9ccdc5aScth callers_length = *lengthp;
5701b9ccdc5aScth if (dp == DP_NBLOCKS)
5702b9ccdc5aScth *lengthp = sizeof (uint64_t);
5703fc2d3b38SGarrett D'Amore else if ((dp == DP_BLKSIZE) || (dp == DP_SSD))
5704b9ccdc5aScth *lengthp = sizeof (uint32_t);
5705b9ccdc5aScth
5706b9ccdc5aScth /* service request for the length of the property */
5707b9ccdc5aScth if (prop_op == PROP_LEN)
5708b9ccdc5aScth return (DDI_PROP_SUCCESS);
5709b9ccdc5aScth
5710b9ccdc5aScth switch (prop_op) {
5711b9ccdc5aScth case PROP_LEN_AND_VAL_ALLOC:
5712b9ccdc5aScth if ((buffer = kmem_alloc(*lengthp,
5713b9ccdc5aScth (mod_flags & DDI_PROP_CANSLEEP) ?
5714b9ccdc5aScth KM_SLEEP : KM_NOSLEEP)) == NULL)
5715b9ccdc5aScth return (DDI_PROP_NO_MEMORY);
5716b9ccdc5aScth *(caddr_t *)valuep = buffer; /* set callers buf */
5717b9ccdc5aScth break;
5718b9ccdc5aScth
5719b9ccdc5aScth case PROP_LEN_AND_VAL_BUF:
5720b9ccdc5aScth /* the length of the prop and the request must match */
5721b9ccdc5aScth if (callers_length != *lengthp)
5722b9ccdc5aScth return (DDI_PROP_INVAL_ARG);
5723b9ccdc5aScth buffer = valuep; /* get callers buf */
5724b9ccdc5aScth break;
5725b9ccdc5aScth
5726b9ccdc5aScth default:
5727b9ccdc5aScth return (DDI_PROP_INVAL_ARG);
5728b9ccdc5aScth }
5729b9ccdc5aScth
5730b9ccdc5aScth /* transfer the value into the buffer */
5731fc2d3b38SGarrett D'Amore switch (dp) {
5732fc2d3b38SGarrett D'Amore case DP_NBLOCKS:
5733b9ccdc5aScth *((uint64_t *)buffer) = capacity;
5734fc2d3b38SGarrett D'Amore break;
5735fc2d3b38SGarrett D'Amore case DP_BLKSIZE:
5736b9ccdc5aScth *((uint32_t *)buffer) = lbasize;
5737fc2d3b38SGarrett D'Amore break;
5738fc2d3b38SGarrett D'Amore case DP_SSD:
5739fc2d3b38SGarrett D'Amore if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5740fc2d3b38SGarrett D'Amore tgattr.media_is_solid_state = B_FALSE;
5741fc2d3b38SGarrett D'Amore *((uint32_t *)buffer) =
5742fc2d3b38SGarrett D'Amore tgattr.media_is_solid_state ? 1 : 0;
5743fc2d3b38SGarrett D'Amore break;
5744*89c81bb4SYuri Pankov case DP_ROT:
5745*89c81bb4SYuri Pankov if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5746*89c81bb4SYuri Pankov tgattr.media_is_rotational = B_TRUE;
5747*89c81bb4SYuri Pankov *((uint32_t *)buffer) =
5748*89c81bb4SYuri Pankov tgattr.media_is_rotational ? 1 : 0;
5749*89c81bb4SYuri Pankov break;
5750fc2d3b38SGarrett D'Amore }
5751b9ccdc5aScth return (DDI_PROP_SUCCESS);
5752b9ccdc5aScth }
5753b9ccdc5aScth
5754b9ccdc5aScth /*
5755b9ccdc5aScth * Support dynamic size oriented properties of partition. Requests
5756b9ccdc5aScth * issued under conditions where size is valid are passed to
5757b9ccdc5aScth * ddi_prop_op_nblocks with the size information, otherwise the
5758b9ccdc5aScth * request is passed to ddi_prop_op. Size depends on valid geometry.
5759b9ccdc5aScth */
5760b9ccdc5aScth if (!cmlb_is_valid(cmlbhandle))
5761b9ccdc5aScth goto fallback;
5762b9ccdc5aScth
5763b9ccdc5aScth /* Get partition nblocks value. */
5764b9ccdc5aScth (void) cmlb_partinfo(cmlbhandle, part,
5765b9ccdc5aScth (diskaddr_t *)&nblocks64, NULL, NULL, NULL, tg_cookie);
5766b9ccdc5aScth
5767b9ccdc5aScth /*
576865908c77Syu, larry liu - Sun Microsystems - Beijing China * Assume partition information is in sys_blocksize units, compute
5769b9ccdc5aScth * divisor for size(9P) property representation.
5770b9ccdc5aScth */
577165908c77Syu, larry liu - Sun Microsystems - Beijing China dblk = lbasize / cl->cl_sys_blocksize;
5772b9ccdc5aScth
5773b9ccdc5aScth /* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5774b9ccdc5aScth return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
5775b9ccdc5aScth name, valuep, lengthp, nblocks64 / dblk, lbasize));
5776b9ccdc5aScth }
5777