Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / cmlb.c
blobecafe30ad418922b6d65f9323c22ec59bf8f729a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * This module provides support for labeling operations for target
31 * drivers.
34 #include <sys/scsi/scsi.h>
35 #include <sys/sunddi.h>
36 #include <sys/dklabel.h>
37 #include <sys/dkio.h>
38 #include <sys/vtoc.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/vtrace.h>
41 #include <sys/efi_partition.h>
42 #include <sys/cmlb.h>
43 #include <sys/cmlb_impl.h>
44 #if defined(__i386) || defined(__amd64)
45 #include <sys/fs/dv_node.h>
46 #endif
47 #include <sys/ddi_impldefs.h>
50 * Driver minor node structure and data table
52 struct driver_minor_data {
53 char *name;
54 minor_t minor;
55 int type;
58 static struct driver_minor_data dk_minor_data[] = {
59 {"a", 0, S_IFBLK},
60 {"b", 1, S_IFBLK},
61 {"c", 2, S_IFBLK},
62 {"d", 3, S_IFBLK},
63 {"e", 4, S_IFBLK},
64 {"f", 5, S_IFBLK},
65 {"g", 6, S_IFBLK},
66 {"h", 7, S_IFBLK},
67 #if defined(_SUNOS_VTOC_16)
68 {"i", 8, S_IFBLK},
69 {"j", 9, S_IFBLK},
70 {"k", 10, S_IFBLK},
71 {"l", 11, S_IFBLK},
72 {"m", 12, S_IFBLK},
73 {"n", 13, S_IFBLK},
74 {"o", 14, S_IFBLK},
75 {"p", 15, S_IFBLK},
76 #endif /* defined(_SUNOS_VTOC_16) */
77 #if defined(_FIRMWARE_NEEDS_FDISK)
78 {"q", 16, S_IFBLK},
79 {"r", 17, S_IFBLK},
80 {"s", 18, S_IFBLK},
81 {"t", 19, S_IFBLK},
82 {"u", 20, S_IFBLK},
83 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
84 {"a,raw", 0, S_IFCHR},
85 {"b,raw", 1, S_IFCHR},
86 {"c,raw", 2, S_IFCHR},
87 {"d,raw", 3, S_IFCHR},
88 {"e,raw", 4, S_IFCHR},
89 {"f,raw", 5, S_IFCHR},
90 {"g,raw", 6, S_IFCHR},
91 {"h,raw", 7, S_IFCHR},
92 #if defined(_SUNOS_VTOC_16)
93 {"i,raw", 8, S_IFCHR},
94 {"j,raw", 9, S_IFCHR},
95 {"k,raw", 10, S_IFCHR},
96 {"l,raw", 11, S_IFCHR},
97 {"m,raw", 12, S_IFCHR},
98 {"n,raw", 13, S_IFCHR},
99 {"o,raw", 14, S_IFCHR},
100 {"p,raw", 15, S_IFCHR},
101 #endif /* defined(_SUNOS_VTOC_16) */
102 #if defined(_FIRMWARE_NEEDS_FDISK)
103 {"q,raw", 16, S_IFCHR},
104 {"r,raw", 17, S_IFCHR},
105 {"s,raw", 18, S_IFCHR},
106 {"t,raw", 19, S_IFCHR},
107 {"u,raw", 20, S_IFCHR},
108 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
112 #if defined(__i386) || defined(__amd64)
113 #if defined(_FIRMWARE_NEEDS_FDISK)
114 static struct driver_minor_data dk_ext_minor_data[] = {
115 {"p5", 21, S_IFBLK},
116 {"p6", 22, S_IFBLK},
117 {"p7", 23, S_IFBLK},
118 {"p8", 24, S_IFBLK},
119 {"p9", 25, S_IFBLK},
120 {"p10", 26, S_IFBLK},
121 {"p11", 27, S_IFBLK},
122 {"p12", 28, S_IFBLK},
123 {"p13", 29, S_IFBLK},
124 {"p14", 30, S_IFBLK},
125 {"p15", 31, S_IFBLK},
126 {"p16", 32, S_IFBLK},
127 {"p17", 33, S_IFBLK},
128 {"p18", 34, S_IFBLK},
129 {"p19", 35, S_IFBLK},
130 {"p20", 36, S_IFBLK},
131 {"p21", 37, S_IFBLK},
132 {"p22", 38, S_IFBLK},
133 {"p23", 39, S_IFBLK},
134 {"p24", 40, S_IFBLK},
135 {"p25", 41, S_IFBLK},
136 {"p26", 42, S_IFBLK},
137 {"p27", 43, S_IFBLK},
138 {"p28", 44, S_IFBLK},
139 {"p29", 45, S_IFBLK},
140 {"p30", 46, S_IFBLK},
141 {"p31", 47, S_IFBLK},
142 {"p32", 48, S_IFBLK},
143 {"p33", 49, S_IFBLK},
144 {"p34", 50, S_IFBLK},
145 {"p35", 51, S_IFBLK},
146 {"p36", 52, S_IFBLK},
147 {"p5,raw", 21, S_IFCHR},
148 {"p6,raw", 22, S_IFCHR},
149 {"p7,raw", 23, S_IFCHR},
150 {"p8,raw", 24, S_IFCHR},
151 {"p9,raw", 25, S_IFCHR},
152 {"p10,raw", 26, S_IFCHR},
153 {"p11,raw", 27, S_IFCHR},
154 {"p12,raw", 28, S_IFCHR},
155 {"p13,raw", 29, S_IFCHR},
156 {"p14,raw", 30, S_IFCHR},
157 {"p15,raw", 31, S_IFCHR},
158 {"p16,raw", 32, S_IFCHR},
159 {"p17,raw", 33, S_IFCHR},
160 {"p18,raw", 34, S_IFCHR},
161 {"p19,raw", 35, S_IFCHR},
162 {"p20,raw", 36, S_IFCHR},
163 {"p21,raw", 37, S_IFCHR},
164 {"p22,raw", 38, S_IFCHR},
165 {"p23,raw", 39, S_IFCHR},
166 {"p24,raw", 40, S_IFCHR},
167 {"p25,raw", 41, S_IFCHR},
168 {"p26,raw", 42, S_IFCHR},
169 {"p27,raw", 43, S_IFCHR},
170 {"p28,raw", 44, S_IFCHR},
171 {"p29,raw", 45, S_IFCHR},
172 {"p30,raw", 46, S_IFCHR},
173 {"p31,raw", 47, S_IFCHR},
174 {"p32,raw", 48, S_IFCHR},
175 {"p33,raw", 49, S_IFCHR},
176 {"p34,raw", 50, S_IFCHR},
177 {"p35,raw", 51, S_IFCHR},
178 {"p36,raw", 52, S_IFCHR},
181 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
182 #endif /* if defined(__i386) || defined(__amd64) */
184 static struct driver_minor_data dk_minor_data_efi[] = {
185 {"a", 0, S_IFBLK},
186 {"b", 1, S_IFBLK},
187 {"c", 2, S_IFBLK},
188 {"d", 3, S_IFBLK},
189 {"e", 4, S_IFBLK},
190 {"f", 5, S_IFBLK},
191 {"g", 6, S_IFBLK},
192 {"wd", 7, S_IFBLK},
193 #if defined(_SUNOS_VTOC_16)
194 {"i", 8, S_IFBLK},
195 {"j", 9, S_IFBLK},
196 {"k", 10, S_IFBLK},
197 {"l", 11, S_IFBLK},
198 {"m", 12, S_IFBLK},
199 {"n", 13, S_IFBLK},
200 {"o", 14, S_IFBLK},
201 {"p", 15, S_IFBLK},
202 #endif /* defined(_SUNOS_VTOC_16) */
203 #if defined(_FIRMWARE_NEEDS_FDISK)
204 {"q", 16, S_IFBLK},
205 {"r", 17, S_IFBLK},
206 {"s", 18, S_IFBLK},
207 {"t", 19, S_IFBLK},
208 {"u", 20, S_IFBLK},
209 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
210 {"a,raw", 0, S_IFCHR},
211 {"b,raw", 1, S_IFCHR},
212 {"c,raw", 2, S_IFCHR},
213 {"d,raw", 3, S_IFCHR},
214 {"e,raw", 4, S_IFCHR},
215 {"f,raw", 5, S_IFCHR},
216 {"g,raw", 6, S_IFCHR},
217 {"wd,raw", 7, S_IFCHR},
218 #if defined(_SUNOS_VTOC_16)
219 {"i,raw", 8, S_IFCHR},
220 {"j,raw", 9, S_IFCHR},
221 {"k,raw", 10, S_IFCHR},
222 {"l,raw", 11, S_IFCHR},
223 {"m,raw", 12, S_IFCHR},
224 {"n,raw", 13, S_IFCHR},
225 {"o,raw", 14, S_IFCHR},
226 {"p,raw", 15, S_IFCHR},
227 #endif /* defined(_SUNOS_VTOC_16) */
228 #if defined(_FIRMWARE_NEEDS_FDISK)
229 {"q,raw", 16, S_IFCHR},
230 {"r,raw", 17, S_IFCHR},
231 {"s,raw", 18, S_IFCHR},
232 {"t,raw", 19, S_IFCHR},
233 {"u,raw", 20, S_IFCHR},
234 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
239 * Declare the dynamic properties implemented in prop_op(9E) implementation
240 * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
241 * of drivers that call cmlb_attach().
243 static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
244 {"Nblocks", DDI_PROP_TYPE_INT64, S_IFBLK},
245 {"Size", DDI_PROP_TYPE_INT64, S_IFCHR},
246 {"device-nblocks", DDI_PROP_TYPE_INT64},
247 {"device-blksize", DDI_PROP_TYPE_INT},
248 {"device-solid-state", DDI_PROP_TYPE_INT},
249 {"device-rotational", DDI_PROP_TYPE_INT},
250 {NULL}
254 * This implies an upper limit of 8192 GPT partitions
255 * in one transfer for GUID Partition Entry Array.
257 len_t cmlb_tg_max_efi_xfer = 1024 * 1024;
260 * External kernel interfaces
262 extern struct mod_ops mod_miscops;
264 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
265 int spec_type, minor_t minor_num);
268 * Global buffer and mutex for debug logging
270 static char cmlb_log_buffer[1024];
271 static kmutex_t cmlb_log_mutex;
274 struct cmlb_lun *cmlb_debug_cl = NULL;
275 uint_t cmlb_level_mask = 0x0;
277 int cmlb_rot_delay = 4; /* default rotational delay */
279 static struct modlmisc modlmisc = {
280 &mod_miscops, /* Type of module */
281 "Common Labeling module"
284 static struct modlinkage modlinkage = {
285 MODREV_1, (void *)&modlmisc, NULL
288 /* Local function prototypes */
289 static dev_t cmlb_make_device(struct cmlb_lun *cl);
290 static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
291 int flags, void *tg_cookie);
292 static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
293 void *tg_cookie);
294 static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
295 void *tg_cookie);
296 static void cmlb_swap_efi_gpt(efi_gpt_t *e);
297 static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
298 static int cmlb_validate_efi(efi_gpt_t *labp);
299 static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
300 void *tg_cookie);
301 static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
302 static int cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *l, int flags);
303 #if defined(_SUNOS_VTOC_8)
304 static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
305 #endif
306 static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
307 static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
308 static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
309 void *tg_cookie);
310 static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
311 static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
312 static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
313 static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
314 static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
315 static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
317 #if defined(__i386) || defined(__amd64)
318 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
319 #endif
321 #if defined(_FIRMWARE_NEEDS_FDISK)
322 static boolean_t cmlb_has_max_chs_vals(struct ipart *fdp);
323 #endif
325 #if defined(_SUNOS_VTOC_16)
326 static void cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
327 struct dk_geom *cl_g, void *tg_cookie);
328 #endif
330 static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
331 void *tg_cookie);
332 static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
333 static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
334 void *tg_cookie);
335 static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
336 static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
337 void *tg_cookie);
338 static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
339 int flag, void *tg_cookie);
340 static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
341 void *tg_cookie);
342 static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
343 void *tg_cookie);
344 static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
345 int flag, void *tg_cookie);
346 static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
347 int flag, void *tg_cookie);
348 static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
349 void *tg_cookie);
350 static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
351 void *tg_cookie);
352 static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
353 void *tg_cookie);
355 #if defined(__i386) || defined(__amd64)
356 static int cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
357 void *tg_cookie);
358 static int cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart,
359 uint32_t start, uint32_t size);
360 static int cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start,
361 void *tg_cookie);
362 static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
363 static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
364 void *tg_cookie);
365 static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
366 int flag);
367 static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
368 int flag);
369 #endif
371 static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
372 static void cmlb_v_log(dev_info_t *dev, const char *label, uint_t level,
373 const char *fmt, va_list ap);
374 static void cmlb_log(dev_info_t *dev, const char *label, uint_t level,
375 const char *fmt, ...);
378 _init(void)
380 mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
381 return (mod_install(&modlinkage));
385 _info(struct modinfo *modinfop)
387 return (mod_info(&modlinkage, modinfop));
391 _fini(void)
393 int err;
395 if ((err = mod_remove(&modlinkage)) != 0) {
396 return (err);
399 mutex_destroy(&cmlb_log_mutex);
400 return (err);
404 * cmlb_dbg is used for debugging to log additional info
405 * Level of output is controlled via cmlb_level_mask setting.
407 static void
408 cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
410 va_list ap;
411 dev_info_t *dev;
412 uint_t level_mask = 0;
414 ASSERT(cl != NULL);
415 dev = CMLB_DEVINFO(cl);
416 ASSERT(dev != NULL);
418 * Filter messages based on the global component and level masks,
419 * also print if cl matches the value of cmlb_debug_cl, or if
420 * cmlb_debug_cl is set to NULL.
422 if (comp & CMLB_TRACE)
423 level_mask |= CMLB_LOGMASK_TRACE;
425 if (comp & CMLB_INFO)
426 level_mask |= CMLB_LOGMASK_INFO;
428 if (comp & CMLB_ERROR)
429 level_mask |= CMLB_LOGMASK_ERROR;
431 if ((cmlb_level_mask & level_mask) &&
432 ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
433 va_start(ap, fmt);
434 cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
435 va_end(ap);
440 * cmlb_log is basically a duplicate of scsi_log. It is redefined here
441 * so that this module does not depend on scsi module.
443 static void
444 cmlb_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt, ...)
446 va_list ap;
448 va_start(ap, fmt);
449 cmlb_v_log(dev, label, level, fmt, ap);
450 va_end(ap);
453 static void
454 cmlb_v_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt,
455 va_list ap)
457 static char name[256];
458 int log_only = 0;
459 int boot_only = 0;
460 int console_only = 0;
462 mutex_enter(&cmlb_log_mutex);
464 if (dev) {
465 if (level == CE_PANIC || level == CE_WARN ||
466 level == CE_NOTE) {
467 (void) sprintf(name, "%s (%s%d):\n",
468 ddi_pathname(dev, cmlb_log_buffer),
469 label, ddi_get_instance(dev));
470 } else {
471 name[0] = '\0';
473 } else {
474 (void) sprintf(name, "%s:", label);
477 (void) vsprintf(cmlb_log_buffer, fmt, ap);
479 switch (cmlb_log_buffer[0]) {
480 case '!':
481 log_only = 1;
482 break;
483 case '?':
484 boot_only = 1;
485 break;
486 case '^':
487 console_only = 1;
488 break;
491 switch (level) {
492 case CE_NOTE:
493 level = CE_CONT;
494 /* FALLTHROUGH */
495 case CE_CONT:
496 case CE_WARN:
497 case CE_PANIC:
498 if (boot_only) {
499 cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
500 } else if (console_only) {
501 cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
502 } else if (log_only) {
503 cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
504 } else {
505 cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
507 break;
508 case CE_IGNORE:
509 break;
510 default:
511 cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
512 break;
514 mutex_exit(&cmlb_log_mutex);
519 * cmlb_alloc_handle:
521 * Allocates a handle.
523 * Arguments:
524 * cmlbhandlep pointer to handle
526 * Notes:
527 * Allocates a handle and stores the allocated handle in the area
528 * pointed to by cmlbhandlep
530 * Context:
531 * Kernel thread only (can sleep).
533 void
534 cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
536 struct cmlb_lun *cl;
538 cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
539 ASSERT(cmlbhandlep != NULL);
541 cl->cl_state = CMLB_INITED;
542 cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
543 mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
545 *cmlbhandlep = (cmlb_handle_t)(cl);
549 * cmlb_free_handle
551 * Frees handle.
553 * Arguments:
554 * cmlbhandlep pointer to handle
556 void
557 cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
559 struct cmlb_lun *cl;
561 cl = (struct cmlb_lun *)*cmlbhandlep;
562 if (cl != NULL) {
563 mutex_destroy(CMLB_MUTEX(cl));
564 kmem_free(cl, sizeof (struct cmlb_lun));
570 * cmlb_attach:
572 * Attach handle to device, create minor nodes for device.
574 * Arguments:
575 * devi pointer to device's dev_info structure.
576 * tgopsp pointer to array of functions cmlb can use to callback
577 * to target driver.
579 * device_type Peripheral device type as defined in
580 * scsi/generic/inquiry.h
582 * is_removable whether or not device is removable.
584 * is_hotpluggable whether or not device is hotpluggable.
586 * node_type minor node type (as used by ddi_create_minor_node)
588 * alter_behavior
589 * bit flags:
591 * CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
592 * an alternate slice for the default label, if
593 * device type is DTYPE_DIRECT an architectures default
594 * label type is VTOC16.
595 * Otherwise alternate slice will no be created.
598 * CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
599 * geometry and label for DKIOCGGEOM and DKIOCGVTOC
600 * on architecture with VTOC8 label types.
602 * CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
603 * one bug in obtaining capacity (in sd):
604 * SCSI READ_CAPACITY command returns the LBA number of the
605 * last logical block, but sd once treated this number as
606 * disks' capacity on x86 platform. And LBAs are addressed
607 * based 0. So the last block was lost on x86 platform.
609 * Now, we remove this workaround. In order for present sd
610 * driver to work with disks which are labeled/partitioned
611 * via previous sd, we add workaround as follows:
613 * 1) Locate backup EFI label: cmlb searches the next to
614 * last
615 * block for backup EFI label. If fails, it will
616 * turn to the last block for backup EFI label;
618 * 2) Clear backup EFI label: cmlb first search the last
619 * block for backup EFI label, and will search the
620 * next to last block only if failed for the last
621 * block.
623 * 3) Calculate geometry:refer to cmlb_convert_geometry()
624 * If capacity increasing by 1 causes disks' capacity
625 * to cross over the limits in geometry calculation,
626 * geometry info will change. This will raise an issue:
627 * In case that primary VTOC label is destroyed, format
628 * commandline can restore it via backup VTOC labels.
629 * And format locates backup VTOC labels by use of
630 * geometry. So changing geometry will
631 * prevent format from finding backup VTOC labels. To
632 * eliminate this side effect for compatibility,
633 * sd uses (capacity -1) to calculate geometry;
635 * 4) 1TB disks: some important data structures use
636 * 32-bit signed long/int (for example, daddr_t),
637 * so that sd doesn't support a disk with capacity
638 * larger than 1TB on 32-bit platform. However,
639 * for exactly 1TB disk, it was treated as (1T - 512)B
640 * in the past, and could have valid Solaris
641 * partitions. To workaround this, if an exactly 1TB
642 * disk has Solaris fdisk partition, it will be allowed
643 * to work with sd.
647 * CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
648 * the entire disk, if there is no valid partition info.
649 * If there is a valid Solaris partition, s0 and s2 will
650 * only cover the entire Solaris partition.
652 * CMLB_CREATE_P0_MINOR_NODE: create p0 node covering
653 * the entire disk. Used by lofi to ensure presence of
654 * whole disk device node in case of LOFI_MAP_FILE ioctl.
656 * cmlbhandle cmlb handle associated with device
658 * tg_cookie cookie from target driver to be passed back to target
659 * driver when we call back to it through tg_ops.
661 * Notes:
662 * Assumes a default label based on capacity for non-removable devices.
663 * If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
664 * for the architecture).
666 * For removable devices, default label type is assumed to be VTOC
667 * type. Create minor nodes based on a default label type.
668 * Label on the media is not validated.
669 * minor number consists of:
670 * if _SUNOS_VTOC_8 is defined
671 * lowest 3 bits is taken as partition number
672 * the rest is instance number
673 * if _SUNOS_VTOC_16 is defined
674 * lowest 6 bits is taken as partition number
675 * the rest is instance number
678 * Return values:
679 * 0 Success
680 * ENXIO creating minor nodes failed.
681 * EINVAL invalid arg, unsupported tg_ops version
684 cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
685 boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
686 int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
689 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
690 diskaddr_t cap;
691 int status;
693 if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
694 return (EINVAL);
696 mutex_enter(CMLB_MUTEX(cl));
698 CMLB_DEVINFO(cl) = devi;
699 cl->cmlb_tg_ops = tgopsp;
700 cl->cl_device_type = device_type;
701 cl->cl_is_removable = is_removable;
702 cl->cl_is_hotpluggable = is_hotpluggable;
703 cl->cl_node_type = node_type;
704 cl->cl_sys_blocksize = DEV_BSIZE;
705 cl->cl_f_geometry_is_valid = B_FALSE;
706 cl->cl_def_labeltype = CMLB_LABEL_VTOC;
707 cl->cl_alter_behavior = alter_behavior;
708 cl->cl_reserved = -1;
709 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
710 #if defined(__i386) || defined(__amd64)
711 cl->cl_logical_drive_count = 0;
712 #endif
714 if (!is_removable) {
715 mutex_exit(CMLB_MUTEX(cl));
716 status = DK_TG_GETCAP(cl, &cap, tg_cookie);
717 mutex_enter(CMLB_MUTEX(cl));
718 if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
719 /* set default EFI if > 2TB */
720 cl->cl_def_labeltype = CMLB_LABEL_EFI;
724 /* create minor nodes based on default label type */
725 cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
726 cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
728 if (cmlb_create_minor_nodes(cl) != 0) {
729 mutex_exit(CMLB_MUTEX(cl));
730 return (ENXIO);
733 /* Define the dynamic properties for devinfo spapshots. */
734 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
736 cl->cl_state = CMLB_ATTACHED;
738 mutex_exit(CMLB_MUTEX(cl));
739 return (0);
743 * cmlb_detach:
745 * Invalidate in-core labeling data and remove all minor nodes for
746 * the device associate with handle.
748 * Arguments:
749 * cmlbhandle cmlb handle associated with device.
751 * tg_cookie cookie from target driver to be passed back to target
752 * driver when we call back to it through tg_ops.
755 /*ARGSUSED1*/
756 void
757 cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
759 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
761 mutex_enter(CMLB_MUTEX(cl));
762 cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
763 cl->cl_f_geometry_is_valid = B_FALSE;
764 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
765 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
766 cl->cl_state = CMLB_INITED;
767 mutex_exit(CMLB_MUTEX(cl));
771 * cmlb_validate:
773 * Validates label.
775 * Arguments
776 * cmlbhandle cmlb handle associated with device.
778 * flags operation flags. used for verbosity control
780 * tg_cookie cookie from target driver to be passed back to target
781 * driver when we call back to it through tg_ops.
784 * Notes:
785 * If new label type is different from the current, adjust minor nodes
786 * accordingly.
788 * Return values:
789 * 0 success
790 * Note: having fdisk but no solaris partition is assumed
791 * success.
793 * ENOMEM memory allocation failed
794 * EIO i/o errors during read or get capacity
795 * EACCESS reservation conflicts
796 * EINVAL label was corrupt, or no default label was assumed
797 * ENXIO invalid handle
800 cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
802 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
803 int rval;
804 int ret = 0;
807 * Temp work-around checking cl for NULL since there is a bug
808 * in sd_detach calling this routine from taskq_dispatch
809 * inited function.
811 if (cl == NULL)
812 return (ENXIO);
814 mutex_enter(CMLB_MUTEX(cl));
815 if (cl->cl_state < CMLB_ATTACHED) {
816 mutex_exit(CMLB_MUTEX(cl));
817 return (ENXIO);
820 rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
821 flags, tg_cookie);
823 if (rval == ENOTSUP) {
824 if (cl->cl_f_geometry_is_valid) {
825 cl->cl_cur_labeltype = CMLB_LABEL_EFI;
826 ret = 0;
827 } else {
828 ret = EINVAL;
830 } else {
831 ret = rval;
832 if (ret == 0)
833 cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
836 if (ret == 0)
837 (void) cmlb_create_minor_nodes(cl);
839 mutex_exit(CMLB_MUTEX(cl));
840 return (ret);
844 * cmlb_invalidate:
845 * Invalidate in core label data
847 * Arguments:
848 * cmlbhandle cmlb handle associated with device.
849 * tg_cookie cookie from target driver to be passed back to target
850 * driver when we call back to it through tg_ops.
852 /*ARGSUSED1*/
853 void
854 cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
856 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
858 if (cl == NULL)
859 return;
861 mutex_enter(CMLB_MUTEX(cl));
862 cl->cl_f_geometry_is_valid = B_FALSE;
863 mutex_exit(CMLB_MUTEX(cl));
867 * cmlb_is_valid
868 * Get status on whether the incore label/geom data is valid
870 * Arguments:
871 * cmlbhandle cmlb handle associated with device.
873 * Return values:
874 * B_TRUE if incore label/geom data is valid.
875 * B_FALSE otherwise.
880 boolean_t
881 cmlb_is_valid(cmlb_handle_t cmlbhandle)
883 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
885 if (cmlbhandle == NULL)
886 return (B_FALSE);
888 return (cl->cl_f_geometry_is_valid);
895 * cmlb_close:
897 * Close the device, revert to a default label minor node for the device,
898 * if it is removable.
900 * Arguments:
901 * cmlbhandle cmlb handle associated with device.
903 * tg_cookie cookie from target driver to be passed back to target
904 * driver when we call back to it through tg_ops.
905 * Return values:
906 * 0 Success
907 * ENXIO Re-creating minor node failed.
909 /*ARGSUSED1*/
911 cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
913 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
915 mutex_enter(CMLB_MUTEX(cl));
916 cl->cl_f_geometry_is_valid = B_FALSE;
918 /* revert to default minor node for this device */
919 if (ISREMOVABLE(cl)) {
920 cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
921 (void) cmlb_create_minor_nodes(cl);
924 mutex_exit(CMLB_MUTEX(cl));
925 return (0);
929 * cmlb_get_devid_block:
930 * get the block number where device id is stored.
932 * Arguments:
933 * cmlbhandle cmlb handle associated with device.
934 * devidblockp pointer to block number.
935 * tg_cookie cookie from target driver to be passed back to target
936 * driver when we call back to it through tg_ops.
938 * Notes:
939 * It stores the block number of device id in the area pointed to
940 * by devidblockp.
941 * with the block number of device id.
943 * Return values:
944 * 0 success
945 * EINVAL device id does not apply to current label type.
947 /*ARGSUSED2*/
949 cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
950 void *tg_cookie)
952 daddr_t spc, blk, head, cyl;
953 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
955 mutex_enter(CMLB_MUTEX(cl));
956 if (cl->cl_state < CMLB_ATTACHED) {
957 mutex_exit(CMLB_MUTEX(cl));
958 return (EINVAL);
961 if ((!cl->cl_f_geometry_is_valid) ||
962 (cl->cl_solaris_size < DK_LABEL_LOC)) {
963 mutex_exit(CMLB_MUTEX(cl));
964 return (EINVAL);
967 if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
968 if (cl->cl_reserved != -1) {
969 blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
970 } else {
971 mutex_exit(CMLB_MUTEX(cl));
972 return (EINVAL);
974 } else {
975 /* if the disk is unlabeled, don't write a devid to it */
976 if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
977 mutex_exit(CMLB_MUTEX(cl));
978 return (EINVAL);
981 /* this geometry doesn't allow us to write a devid */
982 if (cl->cl_g.dkg_acyl < 2) {
983 mutex_exit(CMLB_MUTEX(cl));
984 return (EINVAL);
988 * Subtract 2 guarantees that the next to last cylinder
989 * is used
991 cyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl - 2;
992 spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
993 head = cl->cl_g.dkg_nhead - 1;
994 blk = cl->cl_solaris_offset +
995 (cyl * (spc - cl->cl_g.dkg_apc)) +
996 (head * cl->cl_g.dkg_nsect) + 1;
999 *devidblockp = blk;
1000 mutex_exit(CMLB_MUTEX(cl));
1001 return (0);
1005 * cmlb_partinfo:
1006 * Get partition info for specified partition number.
1008 * Arguments:
1009 * cmlbhandle cmlb handle associated with device.
1010 * part partition number
1011 * nblocksp pointer to number of blocks
1012 * startblockp pointer to starting block
1013 * partnamep pointer to name of partition
1014 * tagp pointer to tag info
1015 * tg_cookie cookie from target driver to be passed back to target
1016 * driver when we call back to it through tg_ops.
1019 * Notes:
1020 * If in-core label is not valid, this functions tries to revalidate
1021 * the label. If label is valid, it stores the total number of blocks
1022 * in this partition in the area pointed to by nblocksp, starting
1023 * block number in area pointed to by startblockp, pointer to partition
1024 * name in area pointed to by partnamep, and tag value in area
1025 * pointed by tagp.
1026 * For EFI labels, tag value will be set to 0.
1028 * For all nblocksp, startblockp and partnamep, tagp, a value of NULL
1029 * indicates the corresponding info is not requested.
1032 * Return values:
1033 * 0 success
1034 * EINVAL no valid label or requested partition number is invalid.
1038 cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
1039 diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
1042 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1043 int rval;
1044 #if defined(__i386) || defined(__amd64)
1045 int ext_part;
1046 #endif
1048 ASSERT(cl != NULL);
1049 mutex_enter(CMLB_MUTEX(cl));
1050 if (cl->cl_state < CMLB_ATTACHED) {
1051 mutex_exit(CMLB_MUTEX(cl));
1052 return (EINVAL);
1055 if (part < 0 || part >= MAXPART) {
1056 rval = EINVAL;
1057 } else {
1058 if (!cl->cl_f_geometry_is_valid)
1059 (void) cmlb_validate_geometry((struct cmlb_lun *)cl,
1060 B_FALSE, 0, tg_cookie);
1062 if (((!cl->cl_f_geometry_is_valid) ||
1063 (part < NDKMAP && cl->cl_solaris_size == 0)) &&
1064 (part != P0_RAW_DISK)) {
1065 rval = EINVAL;
1066 } else {
1067 if (startblockp != NULL)
1068 *startblockp = (diskaddr_t)cl->cl_offset[part];
1070 if (nblocksp != NULL)
1071 *nblocksp = (diskaddr_t)
1072 cl->cl_map[part].dkl_nblk;
1074 if (tagp != NULL)
1075 *tagp =
1076 ((cl->cl_cur_labeltype == CMLB_LABEL_EFI) ||
1077 (part >= NDKMAP)) ? V_UNASSIGNED :
1078 cl->cl_vtoc.v_part[part].p_tag;
1079 rval = 0;
1082 /* consistent with behavior of sd for getting minor name */
1083 if (partnamep != NULL) {
1084 #if defined(__i386) || defined(__amd64)
1085 #if defined(_FIRMWARE_NEEDS_FDISK)
1086 if (part > FDISK_P4) {
1087 ext_part = part-FDISK_P4-1;
1088 *partnamep = dk_ext_minor_data[ext_part].name;
1089 } else
1090 #endif
1091 #endif
1092 *partnamep = dk_minor_data[part].name;
1097 mutex_exit(CMLB_MUTEX(cl));
1098 return (rval);
1102 * cmlb_efi_label_capacity:
1103 * Get capacity stored in EFI disk label.
1105 * Arguments:
1106 * cmlbhandle cmlb handle associated with device.
1107 * capacity pointer to capacity stored in EFI disk label.
1108 * tg_cookie cookie from target driver to be passed back to target
1109 * driver when we call back to it through tg_ops.
1112 * Notes:
1113 * If in-core label is not valid, this functions tries to revalidate
1114 * the label. If label is valid and is an EFI label, it stores the capacity
1115 * in disk label in the area pointed to by capacity.
1118 * Return values:
1119 * 0 success
1120 * EINVAL no valid EFI label or capacity is NULL.
1124 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1125 void *tg_cookie)
1127 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1128 int rval;
1130 ASSERT(cl != NULL);
1131 mutex_enter(CMLB_MUTEX(cl));
1132 if (cl->cl_state < CMLB_ATTACHED) {
1133 mutex_exit(CMLB_MUTEX(cl));
1134 return (EINVAL);
1137 if (!cl->cl_f_geometry_is_valid)
1138 (void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1139 0, tg_cookie);
1141 if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1142 (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1143 rval = EINVAL;
1144 } else {
1145 *capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1146 rval = 0;
1149 mutex_exit(CMLB_MUTEX(cl));
1150 return (rval);
1153 /* Caller should make sure Test Unit Ready succeeds before calling this. */
1154 /*ARGSUSED*/
1156 cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1157 int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
1160 int err;
1161 struct cmlb_lun *cl;
1163 cl = (struct cmlb_lun *)cmlbhandle;
1165 ASSERT(cl != NULL);
1167 mutex_enter(CMLB_MUTEX(cl));
1168 if (cl->cl_state < CMLB_ATTACHED) {
1169 mutex_exit(CMLB_MUTEX(cl));
1170 return (EIO);
1173 switch (cmd) {
1174 case DKIOCSEXTVTOC:
1175 case DKIOCSGEOM:
1176 case DKIOCSETEFI:
1177 case DKIOCSMBOOT:
1178 #if defined(__i386) || defined(__amd64)
1179 case DKIOCSETEXTPART:
1180 #endif
1181 break;
1182 case DKIOCSVTOC:
1183 #if defined(__i386) || defined(__amd64)
1184 case DKIOCPARTINFO:
1185 #endif
1186 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1187 mutex_exit(CMLB_MUTEX(cl));
1188 return (EOVERFLOW);
1190 break;
1191 default:
1192 (void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1193 tg_cookie);
1195 switch (cmd) {
1196 case DKIOCGVTOC:
1197 case DKIOCGAPART:
1198 case DKIOCSAPART:
1200 if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1201 /* GPT label on disk */
1202 mutex_exit(CMLB_MUTEX(cl));
1203 return (ENOTSUP);
1204 } else if
1205 (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1206 mutex_exit(CMLB_MUTEX(cl));
1207 return (EOVERFLOW);
1209 break;
1211 case DKIOCGGEOM:
1212 if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1213 /* GPT label on disk */
1214 mutex_exit(CMLB_MUTEX(cl));
1215 return (ENOTSUP);
1217 break;
1218 default:
1219 break;
1223 mutex_exit(CMLB_MUTEX(cl));
1225 switch (cmd) {
1226 case DKIOCGGEOM:
1227 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1228 err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
1229 break;
1231 case DKIOCSGEOM:
1232 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1233 err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
1234 break;
1236 case DKIOCGAPART:
1237 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1238 err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1239 flag, tg_cookie);
1240 break;
1242 case DKIOCSAPART:
1243 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1244 err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
1245 break;
1247 case DKIOCGVTOC:
1248 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1249 err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
1250 break;
1252 case DKIOCGEXTVTOC:
1253 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1254 err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1255 break;
1257 case DKIOCGETEFI:
1258 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1259 err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
1260 break;
1262 case DKIOCPARTITION:
1263 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1264 err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
1265 break;
1267 case DKIOCSVTOC:
1268 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1269 err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1270 tg_cookie);
1271 break;
1273 case DKIOCSEXTVTOC:
1274 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1275 err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1276 tg_cookie);
1277 break;
1279 case DKIOCSETEFI:
1280 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1281 err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
1282 break;
1284 case DKIOCGMBOOT:
1285 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1286 err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1287 break;
1289 case DKIOCSMBOOT:
1290 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1291 err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1292 break;
1293 case DKIOCG_PHYGEOM:
1294 cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
1295 #if defined(__i386) || defined(__amd64)
1296 err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag, tg_cookie);
1297 #else
1298 err = ENOTTY;
1299 #endif
1300 break;
1301 case DKIOCG_VIRTGEOM:
1302 cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
1303 #if defined(__i386) || defined(__amd64)
1304 err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
1305 #else
1306 err = ENOTTY;
1307 #endif
1308 break;
1309 case DKIOCPARTINFO:
1310 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1311 #if defined(__i386) || defined(__amd64)
1312 err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
1313 #else
1314 err = ENOTTY;
1315 #endif
1316 break;
1317 case DKIOCEXTPARTINFO:
1318 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1319 #if defined(__i386) || defined(__amd64)
1320 err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1321 #else
1322 err = ENOTTY;
1323 #endif
1324 break;
1325 #if defined(__i386) || defined(__amd64)
1326 case DKIOCSETEXTPART:
1327 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEXTPART");
1328 err = cmlb_dkio_set_ext_part(cl, (caddr_t)arg, flag, tg_cookie);
1329 break;
1330 #endif
1331 default:
1332 err = ENOTTY;
1337 * An ioctl that succeeds and changed ('set') size(9P) information
1338 * needs to invalidate the cached devinfo snapshot to avoid having
1339 * old information being returned in a snapshots.
1341 * NB: When available, call ddi_change_minor_node() to clear
1342 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1344 if (err == 0) {
1345 switch (cmd) {
1346 case DKIOCSGEOM:
1347 case DKIOCSAPART:
1348 case DKIOCSVTOC:
1349 case DKIOCSEXTVTOC:
1350 case DKIOCSETEFI:
1351 i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1352 i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1355 return (err);
1358 dev_t
1359 cmlb_make_device(struct cmlb_lun *cl)
1361 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1362 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1363 ddi_get_instance(
1364 CMLB_DEVINFO(cl)) << CMLBUNIT_FORCE_P0_SHIFT));
1365 } else {
1366 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1367 ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
1372 * Function: cmlb_check_update_blockcount
1374 * Description: If current capacity value is invalid, obtains the
1375 * current capacity from target driver.
1377 * Return Code: 0 success
1378 * EIO failure
1380 static int
1381 cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
1383 int status;
1384 diskaddr_t capacity;
1385 uint32_t lbasize;
1387 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1389 if (cl->cl_f_geometry_is_valid)
1390 return (0);
1392 mutex_exit(CMLB_MUTEX(cl));
1393 status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1394 if (status != 0) {
1395 mutex_enter(CMLB_MUTEX(cl));
1396 return (EIO);
1399 status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1400 mutex_enter(CMLB_MUTEX(cl));
1401 if (status != 0)
1402 return (EIO);
1404 if ((capacity != 0) && (lbasize != 0)) {
1405 cl->cl_blockcount = capacity;
1406 cl->cl_tgt_blocksize = lbasize;
1407 if (!cl->cl_is_removable) {
1408 cl->cl_sys_blocksize = lbasize;
1410 return (0);
1411 } else {
1412 return (EIO);
1416 static int
1417 cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
1418 minor_t minor_num, char *node_type, int flag, boolean_t internal)
1420 if (internal)
1421 return (ddi_create_internal_pathname(dip,
1422 name, spec_type, minor_num));
1423 else
1424 return (ddi_create_minor_node(dip,
1425 name, spec_type, minor_num, node_type, flag));
1429 * Function: cmlb_create_minor_nodes
1431 * Description: Create or adjust the minor device nodes for the instance.
1432 * Minor nodes are created based on default label type,
1433 * current label type and last label type we created
1434 * minor nodes based on.
1437 * Arguments: cl - driver soft state (unit) structure
1439 * Return Code: 0 success
1440 * ENXIO failure.
1442 * Context: Kernel thread context
1444 static int
1445 cmlb_create_minor_nodes(struct cmlb_lun *cl)
1447 struct driver_minor_data *dmdp;
1448 int instance, shift;
1449 char name[48];
1450 cmlb_label_t newlabeltype;
1451 boolean_t internal;
1453 ASSERT(cl != NULL);
1454 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1456 internal = (cl->cl_alter_behavior & CMLB_INTERNAL_MINOR_NODES) != 0;
1458 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
1459 shift = CMLBUNIT_FORCE_P0_SHIFT;
1460 else
1461 shift = CMLBUNIT_SHIFT;
1463 /* check the most common case */
1464 if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1465 cl->cl_last_labeltype == cl->cl_cur_labeltype) {
1466 /* do nothing */
1467 return (0);
1470 if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
1471 /* we should never get here */
1472 return (ENXIO);
1475 if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
1476 /* first time during attach */
1477 newlabeltype = cl->cl_def_labeltype;
1479 instance = ddi_get_instance(CMLB_DEVINFO(cl));
1481 /* Create all the minor nodes for this target. */
1482 dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
1483 dk_minor_data;
1484 while (dmdp->name != NULL) {
1486 (void) sprintf(name, "%s", dmdp->name);
1488 if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
1489 dmdp->type,
1490 (instance << shift) | dmdp->minor,
1491 cl->cl_node_type, 0, internal) == DDI_FAILURE) {
1493 * Clean up any nodes that may have been
1494 * created, in case this fails in the middle
1495 * of the loop.
1497 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1498 return (ENXIO);
1500 dmdp++;
1502 cl->cl_last_labeltype = newlabeltype;
1503 #if defined(_SUNOS_VTOC_8)
1505 * "emulate" p0 device for sparc, used by lofi
1507 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1508 if (cmlb_create_minor(CMLB_DEVINFO(cl), "q", S_IFBLK,
1509 (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1510 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1511 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1512 return (ENXIO);
1515 if (cmlb_create_minor(CMLB_DEVINFO(cl), "q,raw",
1516 S_IFCHR,
1517 (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1518 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1519 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1520 return (ENXIO);
1523 #endif /* defined(_SUNOS_VTOC_8) */
1524 return (0);
1527 /* Not first time */
1528 if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1529 if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
1530 /* close time, revert to default. */
1531 newlabeltype = cl->cl_def_labeltype;
1532 } else {
1534 * do nothing since the type for which we last created
1535 * nodes matches the default
1537 return (0);
1539 } else {
1540 if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
1541 /* We are not closing, use current label type */
1542 newlabeltype = cl->cl_cur_labeltype;
1543 } else {
1545 * do nothing since the type for which we last created
1546 * nodes matches the current label type
1548 return (0);
1552 instance = ddi_get_instance(CMLB_DEVINFO(cl));
1555 * Currently we only fix up the s7 node when we are switching
1556 * label types from or to EFI. This is consistent with
1557 * current behavior of sd.
1559 if (newlabeltype == CMLB_LABEL_EFI &&
1560 cl->cl_last_labeltype != CMLB_LABEL_EFI) {
1561 /* from vtoc to EFI */
1562 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1563 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1564 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
1565 S_IFBLK, (instance << shift) | WD_NODE,
1566 cl->cl_node_type, 0, internal);
1567 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
1568 S_IFCHR, (instance << shift) | WD_NODE,
1569 cl->cl_node_type, 0, internal);
1570 } else {
1571 /* from efi to vtoc */
1572 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1573 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1574 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
1575 S_IFBLK, (instance << shift) | WD_NODE,
1576 cl->cl_node_type, 0, internal);
1577 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
1578 S_IFCHR, (instance << shift) | WD_NODE,
1579 cl->cl_node_type, 0, internal);
1582 cl->cl_last_labeltype = newlabeltype;
1583 return (0);
1587 * Function: cmlb_validate_geometry
1589 * Description: Read the label from the disk (if present). Update the unit's
1590 * geometry and vtoc information from the data in the label.
1591 * Verify that the label is valid.
1593 * Arguments:
1594 * cl driver soft state (unit) structure
1596 * forcerevalid force revalidation even if we are already valid.
1597 * flags operation flags from target driver. Used for verbosity
1598 * control at this time.
1599 * tg_cookie cookie from target driver to be passed back to target
1600 * driver when we call back to it through tg_ops.
1602 * Return Code: 0 - Successful completion
1603 * EINVAL - Invalid value in cl->cl_tgt_blocksize or
1604 * cl->cl_blockcount; or label on disk is corrupted
1605 * or unreadable.
1606 * EACCES - Reservation conflict at the device.
1607 * ENOMEM - Resource allocation error
1608 * ENOTSUP - geometry not applicable
1610 * Context: Kernel thread only (can sleep).
1612 static int
1613 cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1614 void *tg_cookie)
1616 int label_error = 0;
1617 diskaddr_t capacity;
1618 int count;
1620 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1622 if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1623 if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
1624 return (ENOTSUP);
1625 return (0);
1628 if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
1629 return (EIO);
1631 capacity = cl->cl_blockcount;
1634 * Set up the "whole disk" fdisk partition; this should always
1635 * exist, regardless of whether the disk contains an fdisk table
1636 * or vtoc.
1638 cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1639 cl->cl_offset[P0_RAW_DISK] = 0;
1641 * note if capacity > int32_max(1TB) we are in 64bit environment
1642 * so no truncation happens
1644 cl->cl_map[P0_RAW_DISK].dkl_nblk = capacity;
1647 * Refresh the logical and physical geometry caches.
1648 * (data from MODE SENSE format/rigid disk geometry pages,
1649 * and scsi_ifgetcap("geometry").
1651 cmlb_resync_geom_caches(cl, capacity, tg_cookie);
1653 cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1654 label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
1655 if (label_error == 0) {
1657 /* found a valid EFI label */
1658 cmlb_dbg(CMLB_TRACE, cl,
1659 "cmlb_validate_geometry: found EFI label\n");
1661 * solaris_size and geometry_is_valid are set in
1662 * cmlb_use_efi
1664 return (ENOTSUP);
1667 /* NO EFI label found */
1669 if (capacity > CMLB_EXTVTOC_LIMIT) {
1670 if (label_error == ESRCH) {
1672 * they've configured a LUN over 2TB, but used
1673 * format.dat to restrict format's view of the
1674 * capacity to be under 2TB in some earlier Solaris
1675 * release.
1677 /* i.e > 2TB with a VTOC < 2TB */
1678 if (!(flags & CMLB_SILENT) &&
1679 (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
1681 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1682 CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1683 "due to VTOC label. To use the full "
1684 "capacity of the disk, use format(8) to "
1685 "relabel the disk with EFI/GPT label.\n",
1686 CMLB_LABEL(cl),
1687 ddi_get_instance(CMLB_DEVINFO(cl)));
1689 cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1691 } else {
1692 return (ENOTSUP);
1696 label_error = 0;
1699 * at this point it is either labeled with a VTOC or it is
1700 * under 1TB (<= 1TB actually for off-by-1)
1704 * Only DIRECT ACCESS devices will have Scl labels.
1705 * CD's supposedly have a Scl label, too
1707 if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
1708 struct dk_label *dkl;
1709 offset_t label_addr;
1710 int rval;
1711 size_t buffer_size;
1714 * Note: This will set up cl->cl_solaris_size and
1715 * cl->cl_solaris_offset.
1717 rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1718 if ((rval != 0) && !ISCD(cl)) {
1719 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1720 return (rval);
1723 if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1725 * Found fdisk table but no Solaris partition entry,
1726 * so don't call cmlb_uselabel() and don't create
1727 * a default label.
1729 label_error = 0;
1730 cl->cl_f_geometry_is_valid = B_TRUE;
1731 goto no_solaris_partition;
1734 label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1736 buffer_size = cl->cl_sys_blocksize;
1738 cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
1739 "label_addr: 0x%x allocation size: 0x%x\n",
1740 label_addr, buffer_size);
1742 if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
1743 return (ENOMEM);
1745 mutex_exit(CMLB_MUTEX(cl));
1746 rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1747 mutex_enter(CMLB_MUTEX(cl));
1749 switch (rval) {
1750 case 0:
1752 * cmlb_uselabel will establish that the geometry
1753 * is valid.
1755 if (cmlb_uselabel(cl,
1756 (struct dk_label *)(uintptr_t)dkl, flags) !=
1757 CMLB_LABEL_IS_VALID) {
1758 label_error = EINVAL;
1759 } else
1760 cl->cl_label_from_media = CMLB_LABEL_VTOC;
1761 break;
1762 case EACCES:
1763 label_error = EACCES;
1764 break;
1765 default:
1766 label_error = EINVAL;
1767 break;
1770 kmem_free(dkl, buffer_size);
1774 * If a valid label was not found, AND if no reservation conflict
1775 * was detected, then go ahead and create a default label (4069506).
1777 * Note: currently, for VTOC_8 devices, the default label is created
1778 * for removables and hotpluggables only. For VTOC_16 devices, the
1779 * default label will be created for all devices.
1780 * (see cmlb_build_default_label)
1782 #if defined(_SUNOS_VTOC_8)
1783 if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1784 (label_error != EACCES)) {
1785 #elif defined(_SUNOS_VTOC_16)
1786 if (label_error != EACCES) {
1787 #endif
1788 if (!cl->cl_f_geometry_is_valid) {
1789 cmlb_build_default_label(cl, tg_cookie);
1791 label_error = 0;
1794 no_solaris_partition:
1796 #if defined(_SUNOS_VTOC_16)
1798 * If we have valid geometry, set up the remaining fdisk partitions.
1799 * Note that dkl_cylno is not used for the fdisk map entries, so
1800 * we set it to an entirely bogus value.
1802 for (count = 0; count < FDISK_PARTS; count++) {
1803 cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1804 cl->cl_map[FDISK_P1 + count].dkl_nblk =
1805 cl->cl_fmap[count].fmap_nblk;
1807 cl->cl_offset[FDISK_P1 + count] =
1808 cl->cl_fmap[count].fmap_start;
1810 #endif
1812 for (count = 0; count < NDKMAP; count++) {
1813 #if defined(_SUNOS_VTOC_8)
1814 struct dk_map *lp = &cl->cl_map[count];
1815 cl->cl_offset[count] =
1816 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
1817 #elif defined(_SUNOS_VTOC_16)
1818 struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
1820 cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
1821 #else
1822 #error "No VTOC format defined."
1823 #endif
1826 return (label_error);
1829 #if defined(_SUNOS_VTOC_16)
1831 * Function: cmlb_convert_geometry
1833 * Description: Convert physical geometry into a dk_geom structure. In
1834 * other words, make sure we don't wrap 16-bit values.
1835 * e.g. converting from geom_cache to dk_geom
1837 * Context: Kernel thread only
1839 static void
1840 cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
1841 struct dk_geom *cl_g, void *tg_cookie)
1844 ASSERT(cl != NULL);
1845 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1847 /* Unlabeled SCSI floppy device */
1848 if (capacity < 160) {
1849 /* Less than 80K */
1850 cl_g->dkg_nhead = 1;
1851 cl_g->dkg_ncyl = capacity;
1852 cl_g->dkg_nsect = 1;
1853 return;
1854 } else if (capacity <= 0x1000) {
1855 cl_g->dkg_nhead = 2;
1856 cl_g->dkg_ncyl = 80;
1857 cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
1858 return;
1862 * For all devices we calculate cylinders using the heads and sectors
1863 * we assign based on capacity of the device. The algorithm is
1864 * designed to be compatible with the way other operating systems
1865 * lay out fdisk tables for X86 and to insure that the cylinders never
1866 * exceed 65535 to prevent problems with X86 ioctls that report
1867 * geometry.
1868 * For some smaller disk sizes we report geometry that matches those
1869 * used by X86 BIOS usage. For larger disks, we use SPT that are
1870 * multiples of 63, since other OSes that are not limited to 16-bits
1871 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
1873 * The following table (in order) illustrates some end result
1874 * calculations:
1876 * Maximum number of blocks nhead nsect
1878 * 2097152 (1GB) 64 32
1879 * 16777216 (8GB) 128 32
1880 * 1052819775 (502.02GB) 255 63
1881 * 2105639550 (0.98TB) 255 126
1882 * 3158459325 (1.47TB) 255 189
1883 * 4211279100 (1.96TB) 255 252
1884 * 5264098875 (2.45TB) 255 315
1885 * ...
1887 * For Solid State Drive(SSD), it uses 4K page size inside and may be
1888 * double with every new generation. If the I/O is not aligned with
1889 * page size on SSDs, SSDs perform a lot slower.
1890 * By default, Solaris partition starts from cylinder 1. It will be
1891 * misaligned even with 4K if using heads(255) and SPT(63). To
1892 * workaround the problem, if the device is SSD, we use heads(224) and
1893 * SPT multiple of 56. Thus the default Solaris partition starts from
1894 * a position that aligns with 128K on a 512 bytes sector size SSD.
1897 if (capacity <= 0x200000) {
1898 cl_g->dkg_nhead = 64;
1899 cl_g->dkg_nsect = 32;
1900 } else if (capacity <= 0x01000000) {
1901 cl_g->dkg_nhead = 128;
1902 cl_g->dkg_nsect = 32;
1903 } else {
1904 tg_attribute_t tgattribute;
1905 int is_solid_state;
1906 unsigned short nhead;
1907 unsigned short nsect;
1909 bzero(&tgattribute, sizeof (tg_attribute_t));
1911 mutex_exit(CMLB_MUTEX(cl));
1912 is_solid_state =
1913 (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
1914 tgattribute.media_is_solid_state : FALSE;
1915 mutex_enter(CMLB_MUTEX(cl));
1917 if (is_solid_state) {
1918 nhead = 224;
1919 nsect = 56;
1920 } else {
1921 nhead = 255;
1922 nsect = 63;
1925 cl_g->dkg_nhead = nhead;
1927 /* make dkg_nsect be smallest multiple of nsect */
1928 cl_g->dkg_nsect = ((capacity +
1929 (UINT16_MAX * nhead * nsect) - 1) /
1930 (UINT16_MAX * nhead * nsect)) * nsect;
1932 if (cl_g->dkg_nsect == 0)
1933 cl_g->dkg_nsect = (UINT16_MAX / nsect) * nsect;
1937 #endif
1940 * Function: cmlb_resync_geom_caches
1942 * Description: (Re)initialize both geometry caches: the virtual geometry
1943 * information is extracted from the HBA (the "geometry"
1944 * capability), and the physical geometry cache data is
1945 * generated by issuing MODE SENSE commands.
1947 * Arguments:
1948 * cl driver soft state (unit) structure
1949 * capacity disk capacity in #blocks
1950 * tg_cookie cookie from target driver to be passed back to target
1951 * driver when we call back to it through tg_ops.
1953 * Context: Kernel thread only (can sleep).
1955 static void
1956 cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1957 void *tg_cookie)
1959 struct cmlb_geom pgeom;
1960 struct cmlb_geom lgeom;
1961 struct cmlb_geom *pgeomp = &pgeom;
1962 unsigned short nhead;
1963 unsigned short nsect;
1964 int spc;
1965 int ret;
1967 ASSERT(cl != NULL);
1968 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1971 * Ask the controller for its logical geometry.
1972 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1973 * then the lgeom cache will be invalid.
1975 mutex_exit(CMLB_MUTEX(cl));
1976 bzero(&lgeom, sizeof (struct cmlb_geom));
1977 ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1978 mutex_enter(CMLB_MUTEX(cl));
1980 bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
1983 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1984 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1986 if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1987 cl->cl_lgeom.g_nhead == 0) {
1989 * Note: Perhaps this needs to be more adaptive? The rationale
1990 * is that, if there's no HBA geometry from the HBA driver, any
1991 * guess is good, since this is the physical geometry. If MODE
1992 * SENSE fails this gives a max cylinder size for non-LBA access
1994 nhead = 255;
1995 nsect = 63;
1996 } else {
1997 nhead = cl->cl_lgeom.g_nhead;
1998 nsect = cl->cl_lgeom.g_nsect;
2001 if (ISCD(cl)) {
2002 pgeomp->g_nhead = 1;
2003 pgeomp->g_nsect = nsect * nhead;
2004 } else {
2005 pgeomp->g_nhead = nhead;
2006 pgeomp->g_nsect = nsect;
2009 spc = pgeomp->g_nhead * pgeomp->g_nsect;
2010 pgeomp->g_capacity = capacity;
2011 if (spc == 0)
2012 pgeomp->g_ncyl = 0;
2013 else
2014 pgeomp->g_ncyl = pgeomp->g_capacity / spc;
2015 pgeomp->g_acyl = 0;
2018 * Retrieve fresh geometry data from the hardware, stash it
2019 * here temporarily before we rebuild the incore label.
2021 * We want to use the MODE SENSE commands to derive the
2022 * physical geometry of the device, but if either command
2023 * fails, the logical geometry is used as the fallback for
2024 * disk label geometry.
2027 mutex_exit(CMLB_MUTEX(cl));
2028 (void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
2029 mutex_enter(CMLB_MUTEX(cl));
2032 * Now update the real copy while holding the mutex. This
2033 * way the global copy is never in an inconsistent state.
2035 bcopy(pgeomp, &cl->cl_pgeom, sizeof (cl->cl_pgeom));
2037 cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
2038 "(cached from lgeom)\n");
2039 cmlb_dbg(CMLB_INFO, cl,
2040 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2041 cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
2042 cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
2043 cmlb_dbg(CMLB_INFO, cl, " lbasize: %d; capacity: %ld; "
2044 "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
2045 cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
2046 cl->cl_pgeom.g_rpm);
2050 #if defined(__i386) || defined(__amd64)
2052 * Function: cmlb_update_ext_minor_nodes
2054 * Description: Routine to add/remove extended partition device nodes
2056 * Arguments:
2057 * cl driver soft state (unit) structure
2058 * num_parts Number of logical drives found on the LUN
2060 * Should be called with the mutex held
2062 * Return Code: 0 for success
2064 * Context: User and Kernel thread
2067 static int
2068 cmlb_update_ext_minor_nodes(struct cmlb_lun *cl, int num_parts)
2070 int i, count, shift;
2071 char name[48];
2072 int instance;
2073 struct driver_minor_data *demdp, *demdpr;
2074 char *devnm;
2075 dev_info_t *pdip;
2076 boolean_t internal;
2078 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2079 ASSERT(cl->cl_update_ext_minor_nodes == 1);
2081 internal = (cl->cl_alter_behavior & CMLB_INTERNAL_MINOR_NODES) != 0;
2082 instance = ddi_get_instance(CMLB_DEVINFO(cl));
2083 demdp = dk_ext_minor_data;
2084 demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2086 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
2087 shift = CMLBUNIT_FORCE_P0_SHIFT;
2088 else
2089 shift = CMLBUNIT_SHIFT;
2091 if (cl->cl_logical_drive_count) {
2092 for (i = 0; i < cl->cl_logical_drive_count; i++) {
2093 (void) sprintf(name, "%s", demdp->name);
2094 ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2095 (void) sprintf(name, "%s", demdpr->name);
2096 ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2097 demdp++;
2098 demdpr++;
2100 /* There are existing device nodes. Remove them */
2101 devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2102 (void) ddi_deviname(cl->cl_devi, devnm);
2103 pdip = ddi_get_parent(cl->cl_devi);
2104 (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2105 kmem_free(devnm, MAXNAMELEN + 1);
2108 demdp = dk_ext_minor_data;
2109 demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2111 for (i = 0; i < num_parts; i++) {
2112 (void) sprintf(name, "%s", demdp->name);
2113 if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
2114 demdp->type,
2115 (instance << shift) | demdp->minor,
2116 cl->cl_node_type, 0, internal) == DDI_FAILURE) {
2118 * Clean up any nodes that may have been
2119 * created, in case this fails in the middle
2120 * of the loop.
2122 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2123 cl->cl_logical_drive_count = 0;
2124 return (ENXIO);
2126 (void) sprintf(name, "%s", demdpr->name);
2127 if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
2128 demdpr->type,
2129 (instance << shift) | demdpr->minor,
2130 cl->cl_node_type, 0) == DDI_FAILURE) {
2132 * Clean up any nodes that may have been
2133 * created, in case this fails in the middle
2134 * of the loop.
2136 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2137 cl->cl_logical_drive_count = 0;
2138 return (ENXIO);
2140 demdp++;
2141 demdpr++;
2144 /* Update the cl_map array for logical drives */
2145 for (count = 0; count < MAX_EXT_PARTS; count++) {
2146 cl->cl_map[FDISK_P4 + 1 + count].dkl_cylno = UINT32_MAX;
2147 cl->cl_map[FDISK_P4 + 1 + count].dkl_nblk =
2148 cl->cl_fmap[FD_NUMPART + count].fmap_nblk;
2149 cl->cl_offset[FDISK_P4 + 1 + count] =
2150 cl->cl_fmap[FD_NUMPART + count].fmap_start;
2153 cl->cl_logical_drive_count = i;
2154 cl->cl_update_ext_minor_nodes = 0;
2155 return (0);
2158 * Function: cmlb_validate_ext_part
2160 * Description: utility routine to validate an extended partition's
2161 * metadata as found on disk
2163 * Arguments:
2164 * cl driver soft state (unit) structure
2165 * part partition number of the extended partition
2166 * epart partition number of the logical drive
2167 * start absolute sector number of the start of the logical
2168 * drive being validated
2169 * size size of logical drive being validated
2171 * Return Code: 0 for success
2173 * Context: User and Kernel thread
2175 * Algorithm :
2176 * Error cases are :
2177 * 1. If start block is lesser than or equal to the end block
2178 * 2. If either start block or end block is beyond the bounadry
2179 * of the extended partition.
2180 * 3. start or end block overlap with existing partitions.
2181 * To check this, first make sure that the start block doesnt
2182 * overlap with existing partitions. Then, calculate the
2183 * possible end block for the given start block that doesnt
2184 * overlap with existing partitions. This can be calculated by
2185 * first setting the possible end block to the end of the
2186 * extended partition (optimistic) and then, checking if there
2187 * is any other partition that lies after the start of the
2188 * partition being validated. If so, set the possible end to
2189 * one block less than the beginning of the next nearest partition
2190 * If the actual end block is greater than the calculated end
2191 * block, we have an overlap.
2194 static int
2195 cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart, uint32_t start,
2196 uint32_t size)
2198 int i;
2199 uint32_t end = start + size - 1;
2200 uint32_t ext_start = cl->cl_fmap[part].fmap_start;
2201 uint32_t ext_end = ext_start + cl->cl_fmap[part].fmap_nblk - 1;
2202 uint32_t ts, te;
2203 uint32_t poss_end = ext_end;
2205 if (end <= start) {
2206 return (1);
2210 * Check if the logical drive boundaries are within that of the
2211 * extended partition.
2213 if (start <= ext_start || start > ext_end || end <= ext_start ||
2214 end > ext_end) {
2215 return (1);
2219 * epart will be equal to FD_NUMPART if it is the first logical drive.
2220 * There is no need to check for overlaps with other logical drives,
2221 * since it is the only logical drive that we have come across so far.
2223 if (epart == FD_NUMPART) {
2224 return (0);
2227 /* Check for overlaps with existing logical drives */
2228 i = FD_NUMPART;
2229 ts = cl->cl_fmap[FD_NUMPART].fmap_start;
2230 te = ts + cl->cl_fmap[FD_NUMPART].fmap_nblk - 1;
2232 while ((i < epart) && ts && te) {
2233 if (start >= ts && start <= te) {
2234 return (1);
2237 if ((ts < poss_end) && (ts > start)) {
2238 poss_end = ts - 1;
2241 i++;
2242 ts = cl->cl_fmap[i].fmap_start;
2243 te = ts + cl->cl_fmap[i].fmap_nblk - 1;
2246 if (end > poss_end) {
2247 return (1);
2250 return (0);
2255 * Function: cmlb_is_linux_swap
2257 * Description: utility routine to verify if a partition is a linux swap
2258 * partition or not.
2260 * Arguments:
2261 * cl driver soft state (unit) structure
2262 * part_start absolute sector number of the start of the partition
2263 * being verified
2264 * tg_cookie cookie from target driver to be passed back to target
2265 * driver when we call back to it through tg_ops.
2267 * Return Code: 0 for success
2269 * Context: User and Kernel thread
2271 * Notes:
2272 * The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2273 * last 10 bytes of a disk block whose size is that of the linux page
2274 * size. This disk block is found at the beginning of the swap partition.
2276 static int
2277 cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start, void *tg_cookie)
2279 int i;
2280 int rval = -1;
2281 uint32_t seek_offset;
2282 uint32_t linux_pg_size;
2283 char *buf, *linux_swap_magic;
2284 int sec_sz = cl->cl_sys_blocksize;
2285 /* Known linux kernel page sizes */
2286 uint32_t linux_pg_size_arr[] = {4096, };
2288 ASSERT(cl != NULL);
2289 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2291 if ((buf = kmem_zalloc(sec_sz, KM_NOSLEEP)) == NULL) {
2292 return (ENOMEM);
2296 * Check if there is a sane Solaris VTOC
2297 * If there is a valid vtoc, no need to lookup
2298 * for the linux swap signature.
2300 mutex_exit(CMLB_MUTEX(cl));
2301 rval = DK_TG_READ(cl, buf, part_start + DK_LABEL_LOC,
2302 sec_sz, tg_cookie);
2303 mutex_enter(CMLB_MUTEX(cl));
2304 if (rval != 0) {
2305 cmlb_dbg(CMLB_ERROR, cl,
2306 "cmlb_is_linux_swap: disk vtoc read err\n");
2307 rval = EIO;
2308 goto done;
2311 if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
2312 (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
2313 rval = -1;
2314 goto done;
2318 /* No valid vtoc, so check for linux swap signature */
2319 linux_swap_magic = buf + sec_sz - 10;
2321 for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
2322 linux_pg_size = linux_pg_size_arr[i];
2323 seek_offset = linux_pg_size/sec_sz - 1;
2324 seek_offset += part_start;
2326 mutex_exit(CMLB_MUTEX(cl));
2327 rval = DK_TG_READ(cl, buf, seek_offset, sec_sz, tg_cookie);
2328 mutex_enter(CMLB_MUTEX(cl));
2330 if (rval != 0) {
2331 cmlb_dbg(CMLB_ERROR, cl,
2332 "cmlb_is_linux_swap: disk read err\n");
2333 rval = EIO;
2334 break;
2337 rval = -1;
2339 if ((strncmp(linux_swap_magic, "SWAP-SPACE", 10) == 0) ||
2340 (strncmp(linux_swap_magic, "SWAPSPACE2", 10) == 0)) {
2341 /* Found a linux swap */
2342 rval = 0;
2343 break;
2347 done:
2348 kmem_free(buf, sec_sz);
2349 return (rval);
2351 #endif
2354 * Function: cmlb_read_fdisk
2356 * Description: utility routine to read the fdisk table.
2358 * Arguments:
2359 * cl driver soft state (unit) structure
2360 * capacity disk capacity in #blocks
2361 * tg_cookie cookie from target driver to be passed back to target
2362 * driver when we call back to it through tg_ops.
2364 * Return Code: 0 for success (includes not reading for no_fdisk_present case
2365 * errnos from tg_rw if failed to read the first block.
2367 * Context: Kernel thread only (can sleep).
2369 /*ARGSUSED*/
2370 static int
2371 cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
2373 #if defined(_NO_FDISK_PRESENT)
2375 cl->cl_solaris_offset = 0;
2376 cl->cl_solaris_size = capacity;
2377 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2378 return (0);
2380 #elif defined(_FIRMWARE_NEEDS_FDISK)
2382 struct ipart *fdp;
2383 struct mboot *mbp;
2384 struct ipart fdisk[FD_NUMPART];
2385 int i, k;
2386 char sigbuf[2];
2387 caddr_t bufp;
2388 int uidx;
2389 int rval;
2390 int lba = 0;
2391 uint_t solaris_offset; /* offset to solaris part. */
2392 daddr_t solaris_size; /* size of solaris partition */
2393 uint32_t blocksize;
2394 #if defined(__i386) || defined(__amd64)
2395 struct ipart eparts[2];
2396 struct ipart *efdp1 = &eparts[0];
2397 struct ipart *efdp2 = &eparts[1];
2398 int ext_part_exists = 0;
2399 int ld_count = 0;
2400 #endif
2402 ASSERT(cl != NULL);
2403 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2406 * Start off assuming no fdisk table
2408 solaris_offset = 0;
2409 solaris_size = capacity;
2411 blocksize = cl->cl_tgt_blocksize;
2413 bufp = kmem_zalloc(blocksize, KM_SLEEP);
2415 mutex_exit(CMLB_MUTEX(cl));
2416 rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
2417 mutex_enter(CMLB_MUTEX(cl));
2419 if (rval != 0) {
2420 cmlb_dbg(CMLB_ERROR, cl,
2421 "cmlb_read_fdisk: fdisk read err\n");
2422 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2423 goto done;
2426 mbp = (struct mboot *)bufp;
2429 * The fdisk table does not begin on a 4-byte boundary within the
2430 * master boot record, so we copy it to an aligned structure to avoid
2431 * alignment exceptions on some processors.
2433 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2436 * Check for lba support before verifying sig; sig might not be
2437 * there, say on a blank disk, but the max_chs mark may still
2438 * be present.
2440 * Note: LBA support and BEFs are an x86-only concept but this
2441 * code should work OK on SPARC as well.
2445 * First, check for lba-access-ok on root node (or prom root node)
2446 * if present there, don't need to search fdisk table.
2448 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
2449 "lba-access-ok", 0) != 0) {
2450 /* All drives do LBA; don't search fdisk table */
2451 lba = 1;
2452 } else {
2453 /* Okay, look for mark in fdisk table */
2454 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2455 /* accumulate "lba" value from all partitions */
2456 lba = (lba || cmlb_has_max_chs_vals(fdp));
2460 if (lba != 0) {
2461 dev_t dev = cmlb_make_device(cl);
2463 if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
2464 "lba-access-ok", 0) == 0) {
2465 /* not found; create it */
2466 if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
2467 "lba-access-ok", NULL, 0) !=
2468 DDI_PROP_SUCCESS) {
2469 cmlb_dbg(CMLB_ERROR, cl,
2470 "cmlb_read_fdisk: Can't create lba "
2471 "property for instance %d\n",
2472 ddi_get_instance(CMLB_DEVINFO(cl)));
2477 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
2480 * Endian-independent signature check
2482 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
2483 (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
2484 cmlb_dbg(CMLB_ERROR, cl,
2485 "cmlb_read_fdisk: no fdisk\n");
2486 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2487 goto done;
2490 #ifdef CMLBDEBUG
2491 if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
2492 fdp = fdisk;
2493 cmlb_dbg(CMLB_INFO, cl, "cmlb_read_fdisk:\n");
2494 cmlb_dbg(CMLB_INFO, cl, " relsect "
2495 "numsect sysid bootid\n");
2496 for (i = 0; i < FD_NUMPART; i++, fdp++) {
2497 cmlb_dbg(CMLB_INFO, cl,
2498 " %d: %8d %8d 0x%08x 0x%08x\n",
2499 i, fdp->relsect, fdp->numsect,
2500 fdp->systid, fdp->bootid);
2503 #endif
2506 * Try to find the unix partition
2508 uidx = -1;
2509 solaris_offset = 0;
2510 solaris_size = 0;
2512 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2513 uint32_t relsect;
2514 uint32_t numsect;
2515 uchar_t systid;
2516 #if defined(__i386) || defined(__amd64)
2518 * Stores relative block offset from the beginning of the
2519 * Extended Partition.
2521 int ext_relsect = 0;
2522 #endif
2524 if (fdp->numsect == 0) {
2525 cl->cl_fmap[i].fmap_start = 0;
2526 cl->cl_fmap[i].fmap_nblk = 0;
2527 continue;
2531 * Data in the fdisk table is little-endian.
2533 relsect = LE_32(fdp->relsect);
2534 numsect = LE_32(fdp->numsect);
2536 cl->cl_fmap[i].fmap_start = relsect;
2537 cl->cl_fmap[i].fmap_nblk = numsect;
2538 cl->cl_fmap[i].fmap_systid = LE_8(fdp->systid);
2540 #if defined(__i386) || defined(__amd64)
2541 /* Support only one extended partition per LUN */
2542 if ((fdp->systid == EXTDOS || fdp->systid == FDISK_EXTLBA) &&
2543 (ext_part_exists == 0)) {
2544 int j;
2545 uint32_t logdrive_offset;
2546 uint32_t ext_numsect;
2547 uint32_t abs_secnum;
2549 ext_part_exists = 1;
2551 for (j = FD_NUMPART; j < FDISK_PARTS; j++) {
2552 mutex_exit(CMLB_MUTEX(cl));
2553 rval = DK_TG_READ(cl, bufp,
2554 (relsect + ext_relsect), blocksize,
2555 tg_cookie);
2556 mutex_enter(CMLB_MUTEX(cl));
2558 if (rval != 0) {
2559 cmlb_dbg(CMLB_ERROR, cl,
2560 "cmlb_read_fdisk: Extended "
2561 "partition read err\n");
2562 goto done;
2565 * The first ipart entry provides the offset
2566 * at which the logical drive starts off from
2567 * the beginning of the container partition
2568 * and the size of the logical drive.
2569 * The second ipart entry provides the offset
2570 * of the next container partition from the
2571 * beginning of the extended partition.
2573 bcopy(&bufp[FDISK_PART_TABLE_START], eparts,
2574 sizeof (eparts));
2575 logdrive_offset = LE_32(efdp1->relsect);
2576 ext_numsect = LE_32(efdp1->numsect);
2577 systid = LE_8(efdp1->systid);
2578 if (logdrive_offset <= 0 || ext_numsect <= 0)
2579 break;
2580 abs_secnum = relsect + ext_relsect +
2581 logdrive_offset;
2583 /* Boundary condition and overlap checking */
2584 if (cmlb_validate_ext_part(cl, i, j, abs_secnum,
2585 ext_numsect)) {
2586 break;
2589 if ((cl->cl_fmap[j].fmap_start != abs_secnum) ||
2590 (cl->cl_fmap[j].fmap_nblk != ext_numsect) ||
2591 (cl->cl_fmap[j].fmap_systid != systid)) {
2593 * Indicates change from previous
2594 * partinfo. Need to recreate
2595 * logical device nodes.
2597 cl->cl_update_ext_minor_nodes = 1;
2599 cl->cl_fmap[j].fmap_start = abs_secnum;
2600 cl->cl_fmap[j].fmap_nblk = ext_numsect;
2601 cl->cl_fmap[j].fmap_systid = systid;
2602 ld_count++;
2604 if ((efdp1->systid == SUNIXOS &&
2605 (cmlb_is_linux_swap(cl, abs_secnum,
2606 tg_cookie) != 0)) ||
2607 efdp1->systid == SUNIXOS2) {
2608 if (uidx == -1) {
2609 uidx = 0;
2610 solaris_offset = abs_secnum;
2611 solaris_size = ext_numsect;
2615 if ((ext_relsect = LE_32(efdp2->relsect)) == 0)
2616 break;
2620 #endif
2622 if (fdp->systid != SUNIXOS &&
2623 fdp->systid != SUNIXOS2 &&
2624 fdp->systid != EFI_PMBR) {
2625 continue;
2629 * use the last active solaris partition id found
2630 * (there should only be 1 active partition id)
2632 * if there are no active solaris partition id
2633 * then use the first inactive solaris partition id
2635 if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
2636 #if defined(__i386) || defined(__amd64)
2637 if (fdp->systid != SUNIXOS ||
2638 (fdp->systid == SUNIXOS &&
2639 (cmlb_is_linux_swap(cl, relsect,
2640 tg_cookie) != 0))) {
2641 #endif
2642 uidx = i;
2643 solaris_offset = relsect;
2644 solaris_size = numsect;
2645 #if defined(__i386) || defined(__amd64)
2647 #endif
2650 #if defined(__i386) || defined(__amd64)
2651 if (ld_count < cl->cl_logical_drive_count) {
2653 * Some/all logical drives were deleted. Clear out
2654 * the fmap entries correspoding to those deleted drives.
2656 for (k = ld_count + FD_NUMPART;
2657 k < cl->cl_logical_drive_count + FD_NUMPART; k++) {
2658 cl->cl_fmap[k].fmap_start = 0;
2659 cl->cl_fmap[k].fmap_nblk = 0;
2660 cl->cl_fmap[k].fmap_systid = 0;
2662 cl->cl_update_ext_minor_nodes = 1;
2664 if (cl->cl_update_ext_minor_nodes) {
2665 rval = cmlb_update_ext_minor_nodes(cl, ld_count);
2666 if (rval != 0) {
2667 goto done;
2670 #endif
2671 cmlb_dbg(CMLB_INFO, cl, "fdisk 0x%x 0x%lx",
2672 cl->cl_solaris_offset, cl->cl_solaris_size);
2673 done:
2676 * Clear the VTOC info, only if the Solaris partition entry
2677 * has moved, changed size, been deleted, or if the size of
2678 * the partition is too small to even fit the label sector.
2680 if ((cl->cl_solaris_offset != solaris_offset) ||
2681 (cl->cl_solaris_size != solaris_size) ||
2682 solaris_size <= DK_LABEL_LOC) {
2683 cmlb_dbg(CMLB_INFO, cl, "fdisk moved 0x%x 0x%lx",
2684 solaris_offset, solaris_size);
2685 bzero(&cl->cl_g, sizeof (struct dk_geom));
2686 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2687 bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
2688 cl->cl_f_geometry_is_valid = B_FALSE;
2690 cl->cl_solaris_offset = solaris_offset;
2691 cl->cl_solaris_size = solaris_size;
2692 kmem_free(bufp, blocksize);
2693 return (rval);
2695 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */
2696 #error "fdisk table presence undetermined for this platform."
2697 #endif /* #if defined(_NO_FDISK_PRESENT) */
2700 static void
2701 cmlb_swap_efi_gpt(efi_gpt_t *e)
2703 _NOTE(ASSUMING_PROTECTED(*e))
2704 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
2705 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
2706 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
2707 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
2708 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
2709 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
2710 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
2711 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
2712 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
2713 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
2714 e->efi_gpt_NumberOfPartitionEntries =
2715 LE_32(e->efi_gpt_NumberOfPartitionEntries);
2716 e->efi_gpt_SizeOfPartitionEntry =
2717 LE_32(e->efi_gpt_SizeOfPartitionEntry);
2718 e->efi_gpt_PartitionEntryArrayCRC32 =
2719 LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
2722 static void
2723 cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
2725 int i;
2727 _NOTE(ASSUMING_PROTECTED(*p))
2728 for (i = 0; i < nparts; i++) {
2729 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
2730 p[i].efi_gpe_PartitionTypeGUID);
2731 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
2732 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
2733 /* PartitionAttrs */
2737 static int
2738 cmlb_validate_efi(efi_gpt_t *labp)
2740 if (labp->efi_gpt_Signature != EFI_SIGNATURE)
2741 return (EINVAL);
2742 /* at least 96 bytes in this version of the spec. */
2743 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
2744 labp->efi_gpt_HeaderSize)
2745 return (EINVAL);
2746 /* this should be 128 bytes */
2747 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
2748 return (EINVAL);
2749 return (0);
2753 * This function returns B_FALSE if there is a valid MBR signature and no
2754 * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2756 * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2757 * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2758 * where a PMBR entry is not the only one. Also, if the first block has been
2759 * corrupted, currently best attempt to allow data access would be to try to
2760 * check for GPT headers. Hence in case of more than one partition entry, but
2761 * at least one EFI_PMBR partition type or no valid magic number, the function
2762 * returns B_TRUE to continue with looking for GPT header.
2765 static boolean_t
2766 cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2768 struct ipart *fdp;
2769 struct mboot *mbp = (struct mboot *)buf;
2770 struct ipart fdisk[FD_NUMPART];
2771 int i;
2773 if (is_mbr != NULL)
2774 *is_mbr = B_TRUE;
2776 if (LE_16(mbp->signature) != MBB_MAGIC) {
2777 if (is_mbr != NULL)
2778 *is_mbr = B_FALSE;
2779 return (B_TRUE);
2782 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2784 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2785 if (fdp->systid == EFI_PMBR)
2786 return (B_TRUE);
2789 return (B_FALSE);
2792 static int
2793 cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2794 void *tg_cookie)
2796 int i;
2797 int rval = 0;
2798 efi_gpe_t *partitions;
2799 uchar_t *buf;
2800 uint_t lbasize; /* is really how much to read */
2801 diskaddr_t cap = 0;
2802 uint_t nparts;
2803 diskaddr_t gpe_lba;
2804 diskaddr_t alternate_lba;
2805 int iofailed = 0;
2806 struct uuid uuid_type_reserved = EFI_RESERVED;
2807 #if defined(_FIRMWARE_NEEDS_FDISK)
2808 boolean_t is_mbr;
2809 #endif
2811 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2813 lbasize = cl->cl_sys_blocksize;
2815 cl->cl_reserved = -1;
2816 mutex_exit(CMLB_MUTEX(cl));
2818 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
2820 rval = DK_TG_READ(cl, buf, 0, lbasize, tg_cookie);
2821 if (rval) {
2822 iofailed = 1;
2823 goto done_err;
2825 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
2826 /* not ours */
2827 rval = ESRCH;
2828 goto done_err;
2831 #if defined(_FIRMWARE_NEEDS_FDISK)
2832 if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
2833 if (is_mbr)
2834 rval = ESRCH;
2835 else
2836 rval = EINVAL;
2837 goto done_err;
2839 #else
2840 if (!cmlb_check_efi_mbr(buf, NULL)) {
2841 rval = EINVAL;
2842 goto done_err;
2845 #endif
2847 rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
2848 if (rval) {
2849 iofailed = 1;
2850 goto done_err;
2852 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2854 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2856 * Couldn't read the primary, try the backup. Our
2857 * capacity at this point could be based on CHS, so
2858 * check what the device reports.
2860 rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
2861 if (rval) {
2862 iofailed = 1;
2863 goto done_err;
2867 * CMLB_OFF_BY_ONE case, we check the next to last block first
2868 * for backup GPT header, otherwise check the last block.
2871 if ((rval = DK_TG_READ(cl, buf,
2872 cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2873 lbasize, tg_cookie))
2874 != 0) {
2875 iofailed = 1;
2876 goto done_err;
2878 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2880 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2882 if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2883 goto done_err;
2884 if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2885 tg_cookie)) != 0)
2886 goto done_err;
2887 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2888 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
2889 goto done_err;
2891 if (!(flags & CMLB_SILENT))
2892 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2893 "primary label corrupt; using backup\n");
2896 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
2897 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2898 alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
2900 rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
2901 if (rval) {
2902 iofailed = 1;
2903 goto done_err;
2905 partitions = (efi_gpe_t *)buf;
2907 if (nparts > MAXPART) {
2908 nparts = MAXPART;
2910 cmlb_swap_efi_gpe(nparts, partitions);
2912 mutex_enter(CMLB_MUTEX(cl));
2914 /* Fill in partition table. */
2915 for (i = 0; i < nparts; i++) {
2916 if (partitions->efi_gpe_StartingLBA != 0 ||
2917 partitions->efi_gpe_EndingLBA != 0) {
2918 cl->cl_map[i].dkl_cylno =
2919 partitions->efi_gpe_StartingLBA;
2920 cl->cl_map[i].dkl_nblk =
2921 partitions->efi_gpe_EndingLBA -
2922 partitions->efi_gpe_StartingLBA + 1;
2923 cl->cl_offset[i] =
2924 partitions->efi_gpe_StartingLBA;
2927 if (cl->cl_reserved == -1) {
2928 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2929 &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2930 cl->cl_reserved = i;
2933 if (i == WD_NODE) {
2935 * minor number 7 corresponds to the whole disk
2936 * if the disk capacity is expanded after disk is
2937 * labeled, minor number 7 represents the capacity
2938 * indicated by the disk label.
2940 cl->cl_map[i].dkl_cylno = 0;
2941 if (alternate_lba == 1) {
2943 * We are using backup label. Since we can
2944 * find a valid label at the end of disk,
2945 * the disk capacity is not expanded.
2947 cl->cl_map[i].dkl_nblk = capacity;
2948 } else {
2949 cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2951 cl->cl_offset[i] = 0;
2953 partitions++;
2955 cl->cl_solaris_offset = 0;
2956 cl->cl_solaris_size = capacity;
2957 cl->cl_label_from_media = CMLB_LABEL_EFI;
2958 cl->cl_f_geometry_is_valid = B_TRUE;
2960 /* clear the vtoc label */
2961 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2963 kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2964 return (0);
2966 done_err:
2967 kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2968 mutex_enter(CMLB_MUTEX(cl));
2969 done_err1:
2971 * if we didn't find something that could look like a VTOC
2972 * and the disk is over 1TB, we know there isn't a valid label.
2973 * Otherwise let cmlb_uselabel decide what to do. We only
2974 * want to invalidate this if we're certain the label isn't
2975 * valid because cmlb_prop_op will now fail, which in turn
2976 * causes things like opens and stats on the partition to fail.
2978 if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
2979 cl->cl_f_geometry_is_valid = B_FALSE;
2981 return (rval);
2986 * Function: cmlb_uselabel
2988 * Description: Validate the disk label and update the relevant data (geometry,
2989 * partition, vtoc, and capacity data) in the cmlb_lun struct.
2990 * Marks the geometry of the unit as being valid.
2992 * Arguments: cl: unit struct.
2993 * dk_label: disk label
2995 * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
2996 * partition, vtoc, and capacity data are good.
2998 * CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
2999 * label; or computed capacity does not jibe with capacity
3000 * reported from the READ CAPACITY command.
3002 * Context: Kernel thread only (can sleep).
3004 static int
3005 cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
3007 short *sp;
3008 short sum;
3009 short count;
3010 int label_error = CMLB_LABEL_IS_VALID;
3011 int i;
3012 diskaddr_t label_capacity;
3013 uint32_t part_end;
3014 diskaddr_t track_capacity;
3015 #if defined(_SUNOS_VTOC_16)
3016 struct dkl_partition *vpartp;
3017 #endif
3018 ASSERT(cl != NULL);
3019 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3021 /* Validate the magic number of the label. */
3022 if (labp->dkl_magic != DKL_MAGIC) {
3023 return (CMLB_LABEL_IS_INVALID);
3026 /* Validate the checksum of the label. */
3027 sp = (short *)labp;
3028 sum = 0;
3029 count = sizeof (struct dk_label) / sizeof (short);
3030 while (count--) {
3031 sum ^= *sp++;
3034 if (sum != 0) {
3035 #if defined(_SUNOS_VTOC_16)
3036 if (!ISCD(cl)) {
3037 #elif defined(_SUNOS_VTOC_8)
3038 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3039 #endif
3040 if (!(flags & CMLB_SILENT))
3041 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3042 CE_WARN,
3043 "Corrupt label - label checksum failed\n");
3045 return (CMLB_LABEL_IS_INVALID);
3050 * Fill in geometry structure with data from label.
3052 bzero(&cl->cl_g, sizeof (struct dk_geom));
3053 cl->cl_g.dkg_ncyl = labp->dkl_ncyl;
3054 cl->cl_g.dkg_acyl = labp->dkl_acyl;
3055 cl->cl_g.dkg_bcyl = 0;
3056 cl->cl_g.dkg_nhead = labp->dkl_nhead;
3057 cl->cl_g.dkg_nsect = labp->dkl_nsect;
3058 cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
3060 #if defined(_SUNOS_VTOC_8)
3061 cl->cl_g.dkg_gap1 = labp->dkl_gap1;
3062 cl->cl_g.dkg_gap2 = labp->dkl_gap2;
3063 cl->cl_g.dkg_bhead = labp->dkl_bhead;
3064 #endif
3065 #if defined(_SUNOS_VTOC_16)
3066 cl->cl_dkg_skew = labp->dkl_skew;
3067 #endif
3069 #if defined(__i386) || defined(__amd64)
3070 cl->cl_g.dkg_apc = labp->dkl_apc;
3071 #endif
3074 * Currently we rely on the values in the label being accurate. If
3075 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
3077 * Note: In the future a MODE SENSE may be used to retrieve this data,
3078 * although this command is optional in SCSI-2.
3080 cl->cl_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600;
3081 cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
3082 (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
3085 * The Read and Write reinstruct values may not be valid
3086 * for older disks.
3088 cl->cl_g.dkg_read_reinstruct = labp->dkl_read_reinstruct;
3089 cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
3091 /* Fill in partition table. */
3092 #if defined(_SUNOS_VTOC_8)
3093 for (i = 0; i < NDKMAP; i++) {
3094 cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
3095 cl->cl_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk;
3097 #endif
3098 #if defined(_SUNOS_VTOC_16)
3099 vpartp = labp->dkl_vtoc.v_part;
3100 track_capacity = labp->dkl_nhead * labp->dkl_nsect;
3102 /* Prevent divide by zero */
3103 if (track_capacity == 0) {
3104 if (!(flags & CMLB_SILENT))
3105 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3106 "Corrupt label - zero nhead or nsect value\n");
3108 return (CMLB_LABEL_IS_INVALID);
3111 for (i = 0; i < NDKMAP; i++, vpartp++) {
3112 cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
3113 cl->cl_map[i].dkl_nblk = vpartp->p_size;
3115 #endif
3117 /* Fill in VTOC Structure. */
3118 bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
3119 #if defined(_SUNOS_VTOC_8)
3121 * The 8-slice vtoc does not include the ascii label; save it into
3122 * the device's soft state structure here.
3124 bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
3125 #endif
3127 /* Now look for a valid capacity. */
3128 track_capacity = (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
3129 label_capacity = (cl->cl_g.dkg_ncyl * track_capacity);
3131 if (cl->cl_g.dkg_acyl) {
3132 #if defined(__i386) || defined(__amd64)
3133 /* we may have > 1 alts cylinder */
3134 label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
3135 #else
3136 label_capacity += track_capacity;
3137 #endif
3141 * Force check here to ensure the computed capacity is valid.
3142 * If capacity is zero, it indicates an invalid label and
3143 * we should abort updating the relevant data then.
3145 if (label_capacity == 0) {
3146 if (!(flags & CMLB_SILENT))
3147 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3148 "Corrupt label - no valid capacity could be "
3149 "retrieved\n");
3151 return (CMLB_LABEL_IS_INVALID);
3154 /* Mark the geometry as valid. */
3155 cl->cl_f_geometry_is_valid = B_TRUE;
3158 * if we got invalidated when mutex exit and entered again,
3159 * if blockcount different than when we came in, need to
3160 * retry from beginning of cmlb_validate_geometry.
3161 * revisit this on next phase of utilizing this for
3162 * sd.
3165 if (label_capacity <= cl->cl_blockcount) {
3166 #if defined(_SUNOS_VTOC_8)
3168 * We can't let this happen on drives that are subdivided
3169 * into logical disks (i.e., that have an fdisk table).
3170 * The cl_blockcount field should always hold the full media
3171 * size in sectors, period. This code would overwrite
3172 * cl_blockcount with the size of the Solaris fdisk partition.
3174 cmlb_dbg(CMLB_ERROR, cl,
3175 "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3176 label_capacity, cl->cl_blockcount);
3177 cl->cl_solaris_size = label_capacity;
3179 #endif /* defined(_SUNOS_VTOC_8) */
3180 goto done;
3183 if (ISCD(cl)) {
3184 /* For CDROMs, we trust that the data in the label is OK. */
3185 #if defined(_SUNOS_VTOC_8)
3186 for (i = 0; i < NDKMAP; i++) {
3187 part_end = labp->dkl_nhead * labp->dkl_nsect *
3188 labp->dkl_map[i].dkl_cylno +
3189 labp->dkl_map[i].dkl_nblk - 1;
3191 if ((labp->dkl_map[i].dkl_nblk) &&
3192 (part_end > cl->cl_blockcount)) {
3193 cl->cl_f_geometry_is_valid = B_FALSE;
3194 break;
3197 #endif
3198 #if defined(_SUNOS_VTOC_16)
3199 vpartp = &(labp->dkl_vtoc.v_part[0]);
3200 for (i = 0; i < NDKMAP; i++, vpartp++) {
3201 part_end = vpartp->p_start + vpartp->p_size;
3202 if ((vpartp->p_size > 0) &&
3203 (part_end > cl->cl_blockcount)) {
3204 cl->cl_f_geometry_is_valid = B_FALSE;
3205 break;
3208 #endif
3209 } else {
3210 /* label_capacity > cl->cl_blockcount */
3211 if (!(flags & CMLB_SILENT)) {
3212 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3213 "Corrupt label - bad geometry\n");
3214 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
3215 "Label says %llu blocks; Drive says %llu blocks\n",
3216 label_capacity, cl->cl_blockcount);
3218 cl->cl_f_geometry_is_valid = B_FALSE;
3219 label_error = CMLB_LABEL_IS_INVALID;
3222 done:
3224 cmlb_dbg(CMLB_INFO, cl, "cmlb_uselabel: (label geometry)\n");
3225 cmlb_dbg(CMLB_INFO, cl,
3226 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3227 cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3228 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3230 cmlb_dbg(CMLB_INFO, cl,
3231 " label_capacity: %d; intrlv: %d; rpm: %d\n",
3232 cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
3233 cmlb_dbg(CMLB_INFO, cl, " wrt_reinstr: %d; rd_reinstr: %d\n",
3234 cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
3236 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3238 return (label_error);
3243 * Function: cmlb_build_default_label
3245 * Description: Generate a default label for those devices that do not have
3246 * one, e.g., new media, removable cartridges, etc..
3248 * Context: Kernel thread only
3250 /*ARGSUSED*/
3251 static void
3252 cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
3254 #if defined(_SUNOS_VTOC_16)
3255 uint_t phys_spc;
3256 uint_t disksize;
3257 struct dk_geom cl_g;
3258 diskaddr_t capacity;
3259 #endif
3261 ASSERT(cl != NULL);
3262 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3264 #if defined(_SUNOS_VTOC_8)
3266 * Note: This is a legacy check for non-removable devices on VTOC_8
3267 * only. This may be a valid check for VTOC_16 as well.
3268 * Once we understand why there is this difference between SPARC and
3269 * x86 platform, we could remove this legacy check.
3271 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3272 return;
3274 #endif
3276 bzero(&cl->cl_g, sizeof (struct dk_geom));
3277 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3278 bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
3280 #if defined(_SUNOS_VTOC_8)
3283 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
3284 * But it is still necessary to set up various geometry information,
3285 * and we are doing this here.
3289 * For the rpm, we use the minimum for the disk. For the head, cyl,
3290 * and number of sector per track, if the capacity <= 1GB, head = 64,
3291 * sect = 32. else head = 255, sect 63 Note: the capacity should be
3292 * equal to C*H*S values. This will cause some truncation of size due
3293 * to round off errors. For CD-ROMs, this truncation can have adverse
3294 * side effects, so returning ncyl and nhead as 1. The nsect will
3295 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
3297 cl->cl_solaris_size = cl->cl_blockcount;
3298 if (ISCD(cl)) {
3299 tg_attribute_t tgattribute;
3300 int is_writable;
3302 * Preserve the old behavior for non-writable
3303 * medias. Since dkg_nsect is a ushort, it
3304 * will lose bits as cdroms have more than
3305 * 65536 sectors. So if we recalculate
3306 * capacity, it will become much shorter.
3307 * But the dkg_* information is not
3308 * used for CDROMs so it is OK. But for
3309 * Writable CDs we need this information
3310 * to be valid (for newfs say). So we
3311 * make nsect and nhead > 1 that way
3312 * nsect can still stay within ushort limit
3313 * without losing any bits.
3316 bzero(&tgattribute, sizeof (tg_attribute_t));
3318 mutex_exit(CMLB_MUTEX(cl));
3319 is_writable =
3320 (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
3321 tgattribute.media_is_writable : 1;
3322 mutex_enter(CMLB_MUTEX(cl));
3324 if (is_writable) {
3325 cl->cl_g.dkg_nhead = 64;
3326 cl->cl_g.dkg_nsect = 32;
3327 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3328 cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
3329 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3330 } else {
3331 cl->cl_g.dkg_ncyl = 1;
3332 cl->cl_g.dkg_nhead = 1;
3333 cl->cl_g.dkg_nsect = cl->cl_blockcount;
3335 } else {
3336 if (cl->cl_blockcount < 160) {
3337 /* Less than 80K */
3338 cl->cl_g.dkg_nhead = 1;
3339 cl->cl_g.dkg_ncyl = cl->cl_blockcount;
3340 cl->cl_g.dkg_nsect = 1;
3341 } else if (cl->cl_blockcount <= 0x1000) {
3342 /* unlabeled SCSI floppy device */
3343 cl->cl_g.dkg_nhead = 2;
3344 cl->cl_g.dkg_ncyl = 80;
3345 cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
3346 } else if (cl->cl_blockcount <= 0x200000) {
3347 cl->cl_g.dkg_nhead = 64;
3348 cl->cl_g.dkg_nsect = 32;
3349 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3350 } else {
3351 cl->cl_g.dkg_nhead = 255;
3353 cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
3354 (UINT16_MAX * 255 * 63) - 1) /
3355 (UINT16_MAX * 255 * 63)) * 63;
3357 if (cl->cl_g.dkg_nsect == 0)
3358 cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
3360 cl->cl_g.dkg_ncyl = cl->cl_blockcount /
3361 (255 * cl->cl_g.dkg_nsect);
3364 cl->cl_solaris_size =
3365 (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
3366 cl->cl_g.dkg_nsect;
3370 cl->cl_g.dkg_acyl = 0;
3371 cl->cl_g.dkg_bcyl = 0;
3372 cl->cl_g.dkg_rpm = 200;
3373 cl->cl_asciilabel[0] = '\0';
3374 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl;
3376 cl->cl_map[0].dkl_cylno = 0;
3377 cl->cl_map[0].dkl_nblk = cl->cl_solaris_size;
3379 cl->cl_map[2].dkl_cylno = 0;
3380 cl->cl_map[2].dkl_nblk = cl->cl_solaris_size;
3382 #elif defined(_SUNOS_VTOC_16)
3384 if (cl->cl_solaris_size == 0) {
3386 * Got fdisk table but no solaris entry therefore
3387 * don't create a default label
3389 cl->cl_f_geometry_is_valid = B_TRUE;
3390 return;
3394 * For CDs we continue to use the physical geometry to calculate
3395 * number of cylinders. All other devices must convert the
3396 * physical geometry (cmlb_geom) to values that will fit
3397 * in a dk_geom structure.
3399 if (ISCD(cl)) {
3400 phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
3401 } else {
3402 /* Convert physical geometry to disk geometry */
3403 bzero(&cl_g, sizeof (struct dk_geom));
3406 * Refer to comments related to off-by-1 at the
3407 * header of this file.
3408 * Before calculating geometry, capacity should be
3409 * decreased by 1.
3412 if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
3413 capacity = cl->cl_blockcount - 1;
3414 else
3415 capacity = cl->cl_blockcount;
3418 cmlb_convert_geometry(cl, capacity, &cl_g, tg_cookie);
3419 bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
3420 phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3423 if (phys_spc == 0)
3424 return;
3425 cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
3426 if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3427 /* disable devid */
3428 cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
3429 disksize = cl->cl_solaris_size;
3430 } else {
3431 cl->cl_g.dkg_acyl = DK_ACYL;
3432 cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
3433 disksize = cl->cl_g.dkg_ncyl * phys_spc;
3436 if (ISCD(cl)) {
3438 * CD's don't use the "heads * sectors * cyls"-type of
3439 * geometry, but instead use the entire capacity of the media.
3441 disksize = cl->cl_solaris_size;
3442 cl->cl_g.dkg_nhead = 1;
3443 cl->cl_g.dkg_nsect = 1;
3444 cl->cl_g.dkg_rpm =
3445 (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
3447 cl->cl_vtoc.v_part[0].p_start = 0;
3448 cl->cl_vtoc.v_part[0].p_size = disksize;
3449 cl->cl_vtoc.v_part[0].p_tag = V_BACKUP;
3450 cl->cl_vtoc.v_part[0].p_flag = V_UNMNT;
3452 cl->cl_map[0].dkl_cylno = 0;
3453 cl->cl_map[0].dkl_nblk = disksize;
3454 cl->cl_offset[0] = 0;
3456 } else {
3458 * Hard disks and removable media cartridges
3460 cl->cl_g.dkg_rpm =
3461 (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
3462 cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
3464 /* Add boot slice */
3465 cl->cl_vtoc.v_part[8].p_start = 0;
3466 cl->cl_vtoc.v_part[8].p_size = phys_spc;
3467 cl->cl_vtoc.v_part[8].p_tag = V_BOOT;
3468 cl->cl_vtoc.v_part[8].p_flag = V_UNMNT;
3470 cl->cl_map[8].dkl_cylno = 0;
3471 cl->cl_map[8].dkl_nblk = phys_spc;
3472 cl->cl_offset[8] = 0;
3474 if ((cl->cl_alter_behavior &
3475 CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
3476 cl->cl_device_type == DTYPE_DIRECT) {
3477 cl->cl_vtoc.v_part[9].p_start = phys_spc;
3478 cl->cl_vtoc.v_part[9].p_size = 2 * phys_spc;
3479 cl->cl_vtoc.v_part[9].p_tag = V_ALTSCTR;
3480 cl->cl_vtoc.v_part[9].p_flag = 0;
3482 cl->cl_map[9].dkl_cylno = 1;
3483 cl->cl_map[9].dkl_nblk = 2 * phys_spc;
3484 cl->cl_offset[9] = phys_spc;
3488 cl->cl_g.dkg_apc = 0;
3490 /* Add backup slice */
3491 cl->cl_vtoc.v_part[2].p_start = 0;
3492 cl->cl_vtoc.v_part[2].p_size = disksize;
3493 cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
3494 cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
3496 cl->cl_map[2].dkl_cylno = 0;
3497 cl->cl_map[2].dkl_nblk = disksize;
3498 cl->cl_offset[2] = 0;
3501 * single slice (s0) covering the entire disk
3503 if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3504 cl->cl_vtoc.v_part[0].p_start = 0;
3505 cl->cl_vtoc.v_part[0].p_tag = V_UNASSIGNED;
3506 cl->cl_vtoc.v_part[0].p_flag = 0;
3507 cl->cl_vtoc.v_part[0].p_size = disksize;
3508 cl->cl_map[0].dkl_cylno = 0;
3509 cl->cl_map[0].dkl_nblk = disksize;
3510 cl->cl_offset[0] = 0;
3513 (void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
3514 " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3515 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3517 #else
3518 #error "No VTOC format defined."
3519 #endif
3521 cl->cl_g.dkg_read_reinstruct = 0;
3522 cl->cl_g.dkg_write_reinstruct = 0;
3524 cl->cl_g.dkg_intrlv = 1;
3526 cl->cl_vtoc.v_sanity = VTOC_SANE;
3527 cl->cl_vtoc.v_nparts = V_NUMPAR;
3528 cl->cl_vtoc.v_version = V_VERSION;
3530 cl->cl_f_geometry_is_valid = B_TRUE;
3531 cl->cl_label_from_media = CMLB_LABEL_UNDEF;
3533 cmlb_dbg(CMLB_INFO, cl,
3534 "cmlb_build_default_label: Default label created: "
3535 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3536 cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
3537 cl->cl_g.dkg_nsect, cl->cl_blockcount);
3541 #if defined(_FIRMWARE_NEEDS_FDISK)
3543 * Max CHS values, as they are encoded into bytes, for 1022/254/63
3545 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2))
3546 #define LBA_MAX_CYL (1022 & 0xFF)
3547 #define LBA_MAX_HEAD (254)
3551 * Function: cmlb_has_max_chs_vals
3553 * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
3555 * Arguments: fdp - ptr to CHS info
3557 * Return Code: True or false
3559 * Context: Any.
3561 static boolean_t
3562 cmlb_has_max_chs_vals(struct ipart *fdp)
3564 return ((fdp->begcyl == LBA_MAX_CYL) &&
3565 (fdp->beghead == LBA_MAX_HEAD) &&
3566 (fdp->begsect == LBA_MAX_SECT) &&
3567 (fdp->endcyl == LBA_MAX_CYL) &&
3568 (fdp->endhead == LBA_MAX_HEAD) &&
3569 (fdp->endsect == LBA_MAX_SECT));
3571 #endif
3574 * Function: cmlb_dkio_get_geometry
3576 * Description: This routine is the driver entry point for handling user
3577 * requests to get the device geometry (DKIOCGGEOM).
3579 * Arguments:
3580 * arg pointer to user provided dk_geom structure specifying
3581 * the controller's notion of the current geometry.
3583 * flag this argument is a pass through to ddi_copyxxx()
3584 * directly from the mode argument of ioctl().
3586 * tg_cookie cookie from target driver to be passed back to target
3587 * driver when we call back to it through tg_ops.
3589 * Return Code: 0
3590 * EFAULT
3591 * ENXIO
3592 * EIO
3594 static int
3595 cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
3596 void *tg_cookie)
3598 struct dk_geom *tmp_geom = NULL;
3599 int rval = 0;
3602 * cmlb_validate_geometry does not spin a disk up
3603 * if it was spcl down. We need to make sure it
3604 * is ready.
3606 mutex_enter(CMLB_MUTEX(cl));
3607 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3608 #if defined(_SUNOS_VTOC_8)
3609 if (rval == EINVAL &&
3610 cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
3612 * This is to return a default label geometry even when we
3613 * do not really assume a default label for the device.
3614 * dad driver utilizes this.
3616 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3617 cmlb_setup_default_geometry(cl, tg_cookie);
3618 rval = 0;
3621 #endif
3622 if (rval) {
3623 mutex_exit(CMLB_MUTEX(cl));
3624 return (rval);
3627 #if defined(__i386) || defined(__amd64)
3628 if (cl->cl_solaris_size == 0) {
3629 mutex_exit(CMLB_MUTEX(cl));
3630 return (EIO);
3632 #endif
3635 * Make a local copy of the soft state geometry to avoid some potential
3636 * race conditions associated with holding the mutex and updating the
3637 * write_reinstruct value
3639 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3640 bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
3642 if (tmp_geom->dkg_write_reinstruct == 0) {
3643 tmp_geom->dkg_write_reinstruct =
3644 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
3645 cmlb_rot_delay) / (int)60000);
3647 mutex_exit(CMLB_MUTEX(cl));
3649 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
3650 flag);
3651 if (rval != 0) {
3652 rval = EFAULT;
3655 kmem_free(tmp_geom, sizeof (struct dk_geom));
3656 return (rval);
3662 * Function: cmlb_dkio_set_geometry
3664 * Description: This routine is the driver entry point for handling user
3665 * requests to set the device geometry (DKIOCSGEOM). The actual
3666 * device geometry is not updated, just the driver "notion" of it.
3668 * Arguments:
3669 * arg pointer to user provided dk_geom structure used to set
3670 * the controller's notion of the current geometry.
3672 * flag this argument is a pass through to ddi_copyxxx()
3673 * directly from the mode argument of ioctl().
3675 * tg_cookie cookie from target driver to be passed back to target
3676 * driver when we call back to it through tg_ops.
3678 * Return Code: 0
3679 * EFAULT
3680 * ENXIO
3681 * EIO
3683 static int
3684 cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
3686 struct dk_geom *tmp_geom;
3687 struct dk_map *lp;
3688 int rval = 0;
3689 int i;
3692 #if defined(__i386) || defined(__amd64)
3693 if (cl->cl_solaris_size == 0) {
3694 return (EIO);
3696 #endif
3698 * We need to copy the user specified geometry into local
3699 * storage and then update the softstate. We don't want to hold
3700 * the mutex and copyin directly from the user to the soft state
3702 tmp_geom = (struct dk_geom *)
3703 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3704 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
3705 if (rval != 0) {
3706 kmem_free(tmp_geom, sizeof (struct dk_geom));
3707 return (EFAULT);
3710 mutex_enter(CMLB_MUTEX(cl));
3711 bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
3712 for (i = 0; i < NDKMAP; i++) {
3713 lp = &cl->cl_map[i];
3714 cl->cl_offset[i] =
3715 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3716 #if defined(__i386) || defined(__amd64)
3717 cl->cl_offset[i] += cl->cl_solaris_offset;
3718 #endif
3720 cl->cl_f_geometry_is_valid = B_FALSE;
3721 mutex_exit(CMLB_MUTEX(cl));
3722 kmem_free(tmp_geom, sizeof (struct dk_geom));
3724 return (rval);
3728 * Function: cmlb_dkio_get_partition
3730 * Description: This routine is the driver entry point for handling user
3731 * requests to get the partition table (DKIOCGAPART).
3733 * Arguments:
3734 * arg pointer to user provided dk_allmap structure specifying
3735 * the controller's notion of the current partition table.
3737 * flag this argument is a pass through to ddi_copyxxx()
3738 * directly from the mode argument of ioctl().
3740 * tg_cookie cookie from target driver to be passed back to target
3741 * driver when we call back to it through tg_ops.
3743 * Return Code: 0
3744 * EFAULT
3745 * ENXIO
3746 * EIO
3748 static int
3749 cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3750 void *tg_cookie)
3752 int rval = 0;
3753 int size;
3756 * Make sure the geometry is valid before getting the partition
3757 * information.
3759 mutex_enter(CMLB_MUTEX(cl));
3760 if ((rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie)) != 0) {
3761 mutex_exit(CMLB_MUTEX(cl));
3762 return (rval);
3764 mutex_exit(CMLB_MUTEX(cl));
3766 #if defined(__i386) || defined(__amd64)
3767 if (cl->cl_solaris_size == 0) {
3768 return (EIO);
3770 #endif
3772 #ifdef _MULTI_DATAMODEL
3773 switch (ddi_model_convert_from(flag & FMODELS)) {
3774 case DDI_MODEL_ILP32: {
3775 struct dk_map32 dk_map32[NDKMAP];
3776 int i;
3778 for (i = 0; i < NDKMAP; i++) {
3779 dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3780 dk_map32[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
3782 size = NDKMAP * sizeof (struct dk_map32);
3783 rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
3784 if (rval != 0) {
3785 rval = EFAULT;
3787 break;
3789 case DDI_MODEL_NONE:
3790 size = NDKMAP * sizeof (struct dk_map);
3791 rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3792 if (rval != 0) {
3793 rval = EFAULT;
3795 break;
3797 #else /* ! _MULTI_DATAMODEL */
3798 size = NDKMAP * sizeof (struct dk_map);
3799 rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3800 if (rval != 0) {
3801 rval = EFAULT;
3803 #endif /* _MULTI_DATAMODEL */
3804 return (rval);
3808 * Function: cmlb_dkio_set_partition
3810 * Description: This routine is the driver entry point for handling user
3811 * requests to set the partition table (DKIOCSAPART). The actual
3812 * device partition is not updated.
3814 * Arguments:
3815 * arg - pointer to user provided dk_allmap structure used to set
3816 * the controller's notion of the partition table.
3817 * flag - this argument is a pass through to ddi_copyxxx()
3818 * directly from the mode argument of ioctl().
3820 * Return Code: 0
3821 * EINVAL
3822 * EFAULT
3823 * ENXIO
3824 * EIO
3826 static int
3827 cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
3829 struct dk_map dk_map[NDKMAP];
3830 struct dk_map *lp;
3831 int rval = 0;
3832 int size;
3833 int i;
3834 #if defined(_SUNOS_VTOC_16)
3835 struct dkl_partition *vp;
3836 #endif
3839 * Set the map for all logical partitions. We lock
3840 * the priority just to make sure an interrupt doesn't
3841 * come in while the map is half updated.
3843 _NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3844 mutex_enter(CMLB_MUTEX(cl));
3846 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3847 mutex_exit(CMLB_MUTEX(cl));
3848 return (ENOTSUP);
3850 mutex_exit(CMLB_MUTEX(cl));
3851 if (cl->cl_solaris_size == 0) {
3852 return (EIO);
3855 #ifdef _MULTI_DATAMODEL
3856 switch (ddi_model_convert_from(flag & FMODELS)) {
3857 case DDI_MODEL_ILP32: {
3858 struct dk_map32 dk_map32[NDKMAP];
3860 size = NDKMAP * sizeof (struct dk_map32);
3861 rval = ddi_copyin((void *)arg, dk_map32, size, flag);
3862 if (rval != 0) {
3863 return (EFAULT);
3865 for (i = 0; i < NDKMAP; i++) {
3866 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
3867 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk;
3869 break;
3871 case DDI_MODEL_NONE:
3872 size = NDKMAP * sizeof (struct dk_map);
3873 rval = ddi_copyin((void *)arg, dk_map, size, flag);
3874 if (rval != 0) {
3875 return (EFAULT);
3877 break;
3879 #else /* ! _MULTI_DATAMODEL */
3880 size = NDKMAP * sizeof (struct dk_map);
3881 rval = ddi_copyin((void *)arg, dk_map, size, flag);
3882 if (rval != 0) {
3883 return (EFAULT);
3885 #endif /* _MULTI_DATAMODEL */
3887 mutex_enter(CMLB_MUTEX(cl));
3888 /* Note: The size used in this bcopy is set based upon the data model */
3889 bcopy(dk_map, cl->cl_map, size);
3890 #if defined(_SUNOS_VTOC_16)
3891 vp = (struct dkl_partition *)&(cl->cl_vtoc);
3892 #endif /* defined(_SUNOS_VTOC_16) */
3893 for (i = 0; i < NDKMAP; i++) {
3894 lp = &cl->cl_map[i];
3895 cl->cl_offset[i] =
3896 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3897 #if defined(_SUNOS_VTOC_16)
3898 vp->p_start = cl->cl_offset[i];
3899 vp->p_size = lp->dkl_nblk;
3900 vp++;
3901 #endif /* defined(_SUNOS_VTOC_16) */
3902 #if defined(__i386) || defined(__amd64)
3903 cl->cl_offset[i] += cl->cl_solaris_offset;
3904 #endif
3906 mutex_exit(CMLB_MUTEX(cl));
3907 return (rval);
3912 * Function: cmlb_dkio_get_vtoc
3914 * Description: This routine is the driver entry point for handling user
3915 * requests to get the current volume table of contents
3916 * (DKIOCGVTOC).
3918 * Arguments:
3919 * arg pointer to user provided vtoc structure specifying
3920 * the current vtoc.
3922 * flag this argument is a pass through to ddi_copyxxx()
3923 * directly from the mode argument of ioctl().
3925 * tg_cookie cookie from target driver to be passed back to target
3926 * driver when we call back to it through tg_ops.
3928 * Return Code: 0
3929 * EFAULT
3930 * ENXIO
3931 * EIO
3933 static int
3934 cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
3936 #if defined(_SUNOS_VTOC_8)
3937 struct vtoc user_vtoc;
3938 #endif /* defined(_SUNOS_VTOC_8) */
3939 int rval = 0;
3941 mutex_enter(CMLB_MUTEX(cl));
3942 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3943 mutex_exit(CMLB_MUTEX(cl));
3944 return (EOVERFLOW);
3947 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3949 #if defined(_SUNOS_VTOC_8)
3950 if (rval == EINVAL &&
3951 (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
3953 * This is to return a default label even when we do not
3954 * really assume a default label for the device.
3955 * dad driver utilizes this.
3957 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3958 cmlb_setup_default_geometry(cl, tg_cookie);
3959 rval = 0;
3962 #endif
3963 if (rval) {
3964 mutex_exit(CMLB_MUTEX(cl));
3965 return (rval);
3968 #if defined(_SUNOS_VTOC_8)
3969 cmlb_build_user_vtoc(cl, &user_vtoc);
3970 mutex_exit(CMLB_MUTEX(cl));
3972 #ifdef _MULTI_DATAMODEL
3973 switch (ddi_model_convert_from(flag & FMODELS)) {
3974 case DDI_MODEL_ILP32: {
3975 struct vtoc32 user_vtoc32;
3977 vtoctovtoc32(user_vtoc, user_vtoc32);
3978 if (ddi_copyout(&user_vtoc32, (void *)arg,
3979 sizeof (struct vtoc32), flag)) {
3980 return (EFAULT);
3982 break;
3985 case DDI_MODEL_NONE:
3986 if (ddi_copyout(&user_vtoc, (void *)arg,
3987 sizeof (struct vtoc), flag)) {
3988 return (EFAULT);
3990 break;
3992 #else /* ! _MULTI_DATAMODEL */
3993 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
3994 return (EFAULT);
3996 #endif /* _MULTI_DATAMODEL */
3998 #elif defined(_SUNOS_VTOC_16)
3999 mutex_exit(CMLB_MUTEX(cl));
4001 #ifdef _MULTI_DATAMODEL
4003 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4004 * 32-bit to maintain compatibility with existing on-disk
4005 * structures. Thus, we need to convert the structure when copying
4006 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
4007 * program. If the target is a 32-bit program, then no conversion
4008 * is necessary.
4010 /* LINTED: logical expression always true: op "||" */
4011 ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
4012 switch (ddi_model_convert_from(flag & FMODELS)) {
4013 case DDI_MODEL_ILP32:
4014 if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
4015 sizeof (cl->cl_vtoc), flag)) {
4016 return (EFAULT);
4018 break;
4020 case DDI_MODEL_NONE: {
4021 struct vtoc user_vtoc;
4023 vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
4024 if (ddi_copyout(&user_vtoc, (void *)arg,
4025 sizeof (struct vtoc), flag)) {
4026 return (EFAULT);
4028 break;
4031 #else /* ! _MULTI_DATAMODEL */
4032 if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
4033 flag)) {
4034 return (EFAULT);
4036 #endif /* _MULTI_DATAMODEL */
4037 #else
4038 #error "No VTOC format defined."
4039 #endif
4041 return (rval);
4046 * Function: cmlb_dkio_get_extvtoc
4048 static int
4049 cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
4050 void *tg_cookie)
4052 struct extvtoc ext_vtoc;
4053 #if defined(_SUNOS_VTOC_8)
4054 struct vtoc user_vtoc;
4055 #endif /* defined(_SUNOS_VTOC_8) */
4056 int rval = 0;
4058 bzero(&ext_vtoc, sizeof (struct extvtoc));
4059 mutex_enter(CMLB_MUTEX(cl));
4060 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
4062 #if defined(_SUNOS_VTOC_8)
4063 if (rval == EINVAL &&
4064 (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
4066 * This is to return a default label even when we do not
4067 * really assume a default label for the device.
4068 * dad driver utilizes this.
4070 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
4071 cmlb_setup_default_geometry(cl, tg_cookie);
4072 rval = 0;
4075 #endif
4076 if (rval) {
4077 mutex_exit(CMLB_MUTEX(cl));
4078 return (rval);
4081 #if defined(_SUNOS_VTOC_8)
4082 cmlb_build_user_vtoc(cl, &user_vtoc);
4083 mutex_exit(CMLB_MUTEX(cl));
4086 * Checking callers data model does not make much sense here
4087 * since extvtoc will always be equivalent to 64bit vtoc.
4088 * What is important is whether the kernel is in 32 or 64 bit
4091 #ifdef _LP64
4092 if (ddi_copyout(&user_vtoc, (void *)arg,
4093 sizeof (struct extvtoc), flag)) {
4094 return (EFAULT);
4096 #else
4097 vtoc32tovtoc(user_vtoc, ext_vtoc);
4098 if (ddi_copyout(&ext_vtoc, (void *)arg,
4099 sizeof (struct extvtoc), flag)) {
4100 return (EFAULT);
4102 #endif
4104 #elif defined(_SUNOS_VTOC_16)
4106 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4107 * 32-bit to maintain compatibility with existing on-disk
4108 * structures. Thus, we need to convert the structure when copying
4109 * it out to extvtoc
4111 vtoc32tovtoc(cl->cl_vtoc, ext_vtoc);
4112 mutex_exit(CMLB_MUTEX(cl));
4114 if (ddi_copyout(&ext_vtoc, (void *)arg, sizeof (struct extvtoc), flag))
4115 return (EFAULT);
4116 #else
4117 #error "No VTOC format defined."
4118 #endif
4120 return (rval);
4124 * This routine implements the DKIOCGETEFI ioctl. This ioctl is currently
4125 * used to read the GPT Partition Table Header (primary/backup), the GUID
4126 * partition Entry Array (primary/backup), and the MBR.
4128 static int
4129 cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
4131 dk_efi_t user_efi;
4132 int rval = 0;
4133 void *buffer;
4134 diskaddr_t tgt_lba;
4136 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4137 return (EFAULT);
4139 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4141 if (user_efi.dki_length == 0 ||
4142 user_efi.dki_length > cmlb_tg_max_efi_xfer)
4143 return (EINVAL);
4145 tgt_lba = user_efi.dki_lba;
4147 mutex_enter(CMLB_MUTEX(cl));
4148 if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4149 (cl->cl_tgt_blocksize == 0) ||
4150 (user_efi.dki_length % cl->cl_sys_blocksize)) {
4151 mutex_exit(CMLB_MUTEX(cl));
4152 return (EINVAL);
4154 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4155 tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
4156 cl->cl_sys_blocksize;
4157 mutex_exit(CMLB_MUTEX(cl));
4159 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4160 rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
4161 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
4162 user_efi.dki_length, flag) != 0)
4163 rval = EFAULT;
4165 kmem_free(buffer, user_efi.dki_length);
4166 return (rval);
4169 #if defined(_SUNOS_VTOC_8)
4171 * Function: cmlb_build_user_vtoc
4173 * Description: This routine populates a pass by reference variable with the
4174 * current volume table of contents.
4176 * Arguments: cl - driver soft state (unit) structure
4177 * user_vtoc - pointer to vtoc structure to be populated
4179 static void
4180 cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4182 struct dk_map2 *lpart;
4183 struct dk_map *lmap;
4184 struct partition *vpart;
4185 uint32_t nblks;
4186 int i;
4188 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4191 * Return vtoc structure fields in the provided VTOC area, addressed
4192 * by *vtoc.
4194 bzero(user_vtoc, sizeof (struct vtoc));
4195 user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
4196 user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
4197 user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
4198 user_vtoc->v_sanity = VTOC_SANE;
4199 user_vtoc->v_version = cl->cl_vtoc.v_version;
4200 bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
4201 user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
4202 user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
4204 for (i = 0; i < 10; i++)
4205 user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
4208 * Convert partitioning information.
4210 * Note the conversion from starting cylinder number
4211 * to starting sector number.
4213 lmap = cl->cl_map;
4214 lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
4215 vpart = user_vtoc->v_part;
4217 nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4219 for (i = 0; i < V_NUMPAR; i++) {
4220 vpart->p_tag = lpart->p_tag;
4221 vpart->p_flag = lpart->p_flag;
4222 vpart->p_start = lmap->dkl_cylno * nblks;
4223 vpart->p_size = lmap->dkl_nblk;
4224 lmap++;
4225 lpart++;
4226 vpart++;
4228 /* (4364927) */
4229 user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
4232 bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
4234 #endif
4236 static int
4237 cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
4238 void *tg_cookie)
4240 struct partition64 p64;
4241 int rval = 0;
4242 uint_t nparts;
4243 efi_gpe_t *partitions;
4244 efi_gpt_t *buffer;
4245 diskaddr_t gpe_lba;
4246 int n_gpe_per_blk = 0;
4248 if (ddi_copyin((const void *)arg, &p64,
4249 sizeof (struct partition64), flag)) {
4250 return (EFAULT);
4253 buffer = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4254 rval = DK_TG_READ(cl, buffer, 1, cl->cl_sys_blocksize, tg_cookie);
4255 if (rval != 0)
4256 goto done_error;
4258 cmlb_swap_efi_gpt(buffer);
4260 if ((rval = cmlb_validate_efi(buffer)) != 0)
4261 goto done_error;
4263 nparts = buffer->efi_gpt_NumberOfPartitionEntries;
4264 gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
4265 if (p64.p_partno >= nparts) {
4266 /* couldn't find it */
4267 rval = ESRCH;
4268 goto done_error;
4271 * Read the block that contains the requested GPE.
4273 n_gpe_per_blk = cl->cl_sys_blocksize / sizeof (efi_gpe_t);
4274 gpe_lba += p64.p_partno / n_gpe_per_blk;
4275 rval = DK_TG_READ(cl, buffer, gpe_lba, cl->cl_sys_blocksize, tg_cookie);
4277 if (rval) {
4278 goto done_error;
4280 partitions = (efi_gpe_t *)buffer;
4281 partitions += p64.p_partno % n_gpe_per_blk;
4283 /* Byte swap only the requested GPE */
4284 cmlb_swap_efi_gpe(1, partitions);
4286 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
4287 sizeof (struct uuid));
4288 p64.p_start = partitions->efi_gpe_StartingLBA;
4289 p64.p_size = partitions->efi_gpe_EndingLBA -
4290 p64.p_start + 1;
4292 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
4293 rval = EFAULT;
4295 done_error:
4296 kmem_free(buffer, cl->cl_sys_blocksize);
4297 return (rval);
4302 * Function: cmlb_dkio_set_vtoc
4304 * Description: This routine is the driver entry point for handling user
4305 * requests to set the current volume table of contents
4306 * (DKIOCSVTOC).
4308 * Arguments:
4309 * dev the device number
4310 * arg pointer to user provided vtoc structure used to set the
4311 * current vtoc.
4313 * flag this argument is a pass through to ddi_copyxxx()
4314 * directly from the mode argument of ioctl().
4316 * tg_cookie cookie from target driver to be passed back to target
4317 * driver when we call back to it through tg_ops.
4319 * Return Code: 0
4320 * EFAULT
4321 * ENXIO
4322 * EINVAL
4323 * ENOTSUP
4325 static int
4326 cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4327 void *tg_cookie)
4329 struct vtoc user_vtoc;
4330 int shift, rval = 0;
4331 boolean_t internal;
4333 internal = (cl->cl_alter_behavior & CMLB_INTERNAL_MINOR_NODES) != 0;
4335 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4336 shift = CMLBUNIT_FORCE_P0_SHIFT;
4337 else
4338 shift = CMLBUNIT_SHIFT;
4340 #ifdef _MULTI_DATAMODEL
4341 switch (ddi_model_convert_from(flag & FMODELS)) {
4342 case DDI_MODEL_ILP32: {
4343 struct vtoc32 user_vtoc32;
4345 if (ddi_copyin((const void *)arg, &user_vtoc32,
4346 sizeof (struct vtoc32), flag)) {
4347 return (EFAULT);
4349 vtoc32tovtoc(user_vtoc32, user_vtoc);
4350 break;
4353 case DDI_MODEL_NONE:
4354 if (ddi_copyin((const void *)arg, &user_vtoc,
4355 sizeof (struct vtoc), flag)) {
4356 return (EFAULT);
4358 break;
4360 #else /* ! _MULTI_DATAMODEL */
4361 if (ddi_copyin((const void *)arg, &user_vtoc,
4362 sizeof (struct vtoc), flag)) {
4363 return (EFAULT);
4365 #endif /* _MULTI_DATAMODEL */
4367 mutex_enter(CMLB_MUTEX(cl));
4369 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
4370 mutex_exit(CMLB_MUTEX(cl));
4371 return (EOVERFLOW);
4374 #if defined(__i386) || defined(__amd64)
4375 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4376 mutex_exit(CMLB_MUTEX(cl));
4377 return (EINVAL);
4379 #endif
4381 if (cl->cl_g.dkg_ncyl == 0) {
4382 mutex_exit(CMLB_MUTEX(cl));
4383 return (EINVAL);
4386 mutex_exit(CMLB_MUTEX(cl));
4387 cmlb_clear_efi(cl, tg_cookie);
4388 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4389 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4392 * cmlb_dkio_set_vtoc creates duplicate minor nodes when
4393 * relabeling an SMI disk. To avoid that we remove them
4394 * before creating.
4395 * It should be OK to remove a non-existed minor node.
4397 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4398 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4400 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4401 S_IFBLK, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4402 cl->cl_node_type, 0, internal);
4403 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4404 S_IFCHR, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4405 cl->cl_node_type, 0, internal);
4406 mutex_enter(CMLB_MUTEX(cl));
4408 if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4409 if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4410 if (cmlb_validate_geometry(cl,
4411 B_TRUE, 0, tg_cookie) != 0) {
4412 cmlb_dbg(CMLB_ERROR, cl,
4413 "cmlb_dkio_set_vtoc: "
4414 "Failed validate geometry\n");
4416 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
4419 mutex_exit(CMLB_MUTEX(cl));
4420 return (rval);
4424 * Function: cmlb_dkio_set_extvtoc
4426 static int
4427 cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4428 void *tg_cookie)
4430 int shift, rval = 0;
4431 struct vtoc user_vtoc;
4432 boolean_t internal;
4434 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4435 shift = CMLBUNIT_FORCE_P0_SHIFT;
4436 else
4437 shift = CMLBUNIT_SHIFT;
4440 * Checking callers data model does not make much sense here
4441 * since extvtoc will always be equivalent to 64bit vtoc.
4442 * What is important is whether the kernel is in 32 or 64 bit
4445 #ifdef _LP64
4446 if (ddi_copyin((const void *)arg, &user_vtoc,
4447 sizeof (struct extvtoc), flag)) {
4448 return (EFAULT);
4450 #else
4451 struct extvtoc user_extvtoc;
4452 if (ddi_copyin((const void *)arg, &user_extvtoc,
4453 sizeof (struct extvtoc), flag)) {
4454 return (EFAULT);
4457 vtoctovtoc32(user_extvtoc, user_vtoc);
4458 #endif
4460 internal = (cl->cl_alter_behavior & CMLB_INTERNAL_MINOR_NODES) != 0;
4461 mutex_enter(CMLB_MUTEX(cl));
4462 #if defined(__i386) || defined(__amd64)
4463 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4464 mutex_exit(CMLB_MUTEX(cl));
4465 return (EINVAL);
4467 #endif
4469 if (cl->cl_g.dkg_ncyl == 0) {
4470 mutex_exit(CMLB_MUTEX(cl));
4471 return (EINVAL);
4474 mutex_exit(CMLB_MUTEX(cl));
4475 cmlb_clear_efi(cl, tg_cookie);
4476 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4477 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4479 * cmlb_dkio_set_extvtoc creates duplicate minor nodes when
4480 * relabeling an SMI disk. To avoid that we remove them
4481 * before creating.
4482 * It should be OK to remove a non-existed minor node.
4484 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4485 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4487 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4488 S_IFBLK, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4489 cl->cl_node_type, 0, internal);
4490 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4491 S_IFCHR, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4492 cl->cl_node_type, 0, internal);
4494 mutex_enter(CMLB_MUTEX(cl));
4496 if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4497 if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4498 if (cmlb_validate_geometry(cl,
4499 B_TRUE, 0, tg_cookie) != 0) {
4500 cmlb_dbg(CMLB_ERROR, cl,
4501 "cmlb_dkio_set_vtoc: "
4502 "Failed validate geometry\n");
4506 mutex_exit(CMLB_MUTEX(cl));
4507 return (rval);
4511 * Function: cmlb_build_label_vtoc
4513 * Description: This routine updates the driver soft state current volume table
4514 * of contents based on a user specified vtoc.
4516 * Arguments: cl - driver soft state (unit) structure
4517 * user_vtoc - pointer to vtoc structure specifying vtoc to be used
4518 * to update the driver soft state.
4520 * Return Code: 0
4521 * EINVAL
4523 static int
4524 cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4526 struct dk_map *lmap;
4527 struct partition *vpart;
4528 uint_t nblks;
4529 #if defined(_SUNOS_VTOC_8)
4530 int ncyl;
4531 struct dk_map2 *lpart;
4532 #endif /* defined(_SUNOS_VTOC_8) */
4533 int i;
4535 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4537 /* Sanity-check the vtoc */
4538 if (user_vtoc->v_sanity != VTOC_SANE ||
4539 user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
4540 user_vtoc->v_nparts != V_NUMPAR) {
4541 cmlb_dbg(CMLB_INFO, cl,
4542 "cmlb_build_label_vtoc: vtoc not valid\n");
4543 return (EINVAL);
4546 nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4547 if (nblks == 0) {
4548 cmlb_dbg(CMLB_INFO, cl,
4549 "cmlb_build_label_vtoc: geom nblks is 0\n");
4550 return (EINVAL);
4553 #if defined(_SUNOS_VTOC_8)
4554 vpart = user_vtoc->v_part;
4555 for (i = 0; i < V_NUMPAR; i++) {
4556 if (((unsigned)vpart->p_start % nblks) != 0) {
4557 cmlb_dbg(CMLB_INFO, cl,
4558 "cmlb_build_label_vtoc: p_start not multiply of"
4559 "nblks part %d p_start %d nblks %d\n", i,
4560 vpart->p_start, nblks);
4561 return (EINVAL);
4563 ncyl = (unsigned)vpart->p_start / nblks;
4564 ncyl += (unsigned)vpart->p_size / nblks;
4565 if (((unsigned)vpart->p_size % nblks) != 0) {
4566 ncyl++;
4568 if (ncyl > (int)cl->cl_g.dkg_ncyl) {
4569 cmlb_dbg(CMLB_INFO, cl,
4570 "cmlb_build_label_vtoc: ncyl %d > dkg_ncyl %d"
4571 "p_size %ld p_start %ld nblks %d part number %d"
4572 "tag %d\n",
4573 ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
4574 vpart->p_start, nblks,
4575 i, vpart->p_tag);
4577 return (EINVAL);
4579 vpart++;
4581 #endif /* defined(_SUNOS_VTOC_8) */
4583 /* Put appropriate vtoc structure fields into the disk label */
4584 #if defined(_SUNOS_VTOC_16)
4586 * The vtoc is always a 32bit data structure to maintain the
4587 * on-disk format. Convert "in place" instead of doing bcopy.
4589 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
4592 * in the 16-slice vtoc, starting sectors are expressed in
4593 * numbers *relative* to the start of the Solaris fdisk partition.
4595 lmap = cl->cl_map;
4596 vpart = user_vtoc->v_part;
4598 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
4599 lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4600 lmap->dkl_nblk = (unsigned)vpart->p_size;
4603 #elif defined(_SUNOS_VTOC_8)
4605 cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
4606 cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
4607 cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
4609 cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
4610 cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
4612 bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
4614 cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
4616 for (i = 0; i < 10; i++)
4617 cl->cl_vtoc.v_reserved[i] = user_vtoc->v_reserved[i];
4620 * Note the conversion from starting sector number
4621 * to starting cylinder number.
4622 * Return error if division results in a remainder.
4624 lmap = cl->cl_map;
4625 lpart = cl->cl_vtoc.v_part;
4626 vpart = user_vtoc->v_part;
4628 for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
4629 lpart->p_tag = vpart->p_tag;
4630 lpart->p_flag = vpart->p_flag;
4631 lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4632 lmap->dkl_nblk = (unsigned)vpart->p_size;
4634 lmap++;
4635 lpart++;
4636 vpart++;
4638 /* (4387723) */
4639 #ifdef _LP64
4640 if (user_vtoc->timestamp[i] > TIME32_MAX) {
4641 cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
4642 } else {
4643 cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4645 #else
4646 cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4647 #endif
4650 bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
4651 #else
4652 #error "No VTOC format defined."
4653 #endif
4654 return (0);
4658 * Function: cmlb_clear_efi
4660 * Description: This routine clears all EFI labels.
4662 * Arguments:
4663 * cl driver soft state (unit) structure
4665 * tg_cookie cookie from target driver to be passed back to target
4666 * driver when we call back to it through tg_ops.
4667 * Return Code: void
4669 static void
4670 cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
4672 efi_gpt_t *gpt;
4673 diskaddr_t cap;
4674 int rval;
4676 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4678 mutex_enter(CMLB_MUTEX(cl));
4679 cl->cl_reserved = -1;
4680 mutex_exit(CMLB_MUTEX(cl));
4682 gpt = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4684 if (DK_TG_READ(cl, gpt, 1, cl->cl_sys_blocksize, tg_cookie) != 0) {
4685 goto done;
4688 cmlb_swap_efi_gpt(gpt);
4689 rval = cmlb_validate_efi(gpt);
4690 if (rval == 0) {
4691 /* clear primary */
4692 bzero(gpt, sizeof (efi_gpt_t));
4693 if (rval = DK_TG_WRITE(cl, gpt, 1, cl->cl_sys_blocksize,
4694 tg_cookie)) {
4695 cmlb_dbg(CMLB_INFO, cl,
4696 "cmlb_clear_efi: clear primary label failed\n");
4699 /* the backup */
4700 rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
4701 if (rval) {
4702 goto done;
4705 if ((rval = DK_TG_READ(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4706 tg_cookie)) != 0) {
4707 goto done;
4709 cmlb_swap_efi_gpt(gpt);
4710 rval = cmlb_validate_efi(gpt);
4711 if (rval == 0) {
4712 /* clear backup */
4713 cmlb_dbg(CMLB_TRACE, cl,
4714 "cmlb_clear_efi clear backup@%lu\n", cap - 1);
4715 bzero(gpt, sizeof (efi_gpt_t));
4716 if ((rval = DK_TG_WRITE(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4717 tg_cookie))) {
4718 cmlb_dbg(CMLB_INFO, cl,
4719 "cmlb_clear_efi: clear backup label failed\n");
4721 } else {
4723 * Refer to comments related to off-by-1 at the
4724 * header of this file
4726 if ((rval = DK_TG_READ(cl, gpt, cap - 2,
4727 cl->cl_sys_blocksize, tg_cookie)) != 0) {
4728 goto done;
4730 cmlb_swap_efi_gpt(gpt);
4731 rval = cmlb_validate_efi(gpt);
4732 if (rval == 0) {
4733 /* clear legacy backup EFI label */
4734 cmlb_dbg(CMLB_TRACE, cl,
4735 "cmlb_clear_efi clear legacy backup@%lu\n",
4736 cap - 2);
4737 bzero(gpt, sizeof (efi_gpt_t));
4738 if ((rval = DK_TG_WRITE(cl, gpt, cap - 2,
4739 cl->cl_sys_blocksize, tg_cookie))) {
4740 cmlb_dbg(CMLB_INFO, cl,
4741 "cmlb_clear_efi: clear legacy backup label "
4742 "failed\n");
4747 done:
4748 kmem_free(gpt, cl->cl_sys_blocksize);
4752 * Function: cmlb_set_vtoc
4754 * Description: This routine writes data to the appropriate positions
4756 * Arguments:
4757 * cl driver soft state (unit) structure
4759 * dkl the data to be written
4761 * tg_cookie cookie from target driver to be passed back to target
4762 * driver when we call back to it through tg_ops.
4764 * Return: void
4766 static int
4767 cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
4769 uint_t label_addr;
4770 int sec;
4771 diskaddr_t blk;
4772 int head;
4773 int cyl;
4774 int rval;
4776 #if defined(__i386) || defined(__amd64)
4777 label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
4778 #else
4779 /* Write the primary label at block 0 of the solaris partition. */
4780 label_addr = 0;
4781 #endif
4783 rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
4784 tg_cookie);
4786 if (rval != 0) {
4787 return (rval);
4791 * Calculate where the backup labels go. They are always on
4792 * the last alternate cylinder, but some older drives put them
4793 * on head 2 instead of the last head. They are always on the
4794 * first 5 odd sectors of the appropriate track.
4796 * We have no choice at this point, but to believe that the
4797 * disk label is valid. Use the geometry of the disk
4798 * as described in the label.
4800 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1;
4801 head = dkl->dkl_nhead - 1;
4804 * Write and verify the backup labels. Make sure we don't try to
4805 * write past the last cylinder.
4807 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
4808 blk = (diskaddr_t)(
4809 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
4810 (head * dkl->dkl_nsect) + sec);
4811 #if defined(__i386) || defined(__amd64)
4812 blk += cl->cl_solaris_offset;
4813 #endif
4814 rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
4815 tg_cookie);
4816 cmlb_dbg(CMLB_INFO, cl,
4817 "cmlb_set_vtoc: wrote backup label %llx\n", blk);
4818 if (rval != 0) {
4819 goto exit;
4822 exit:
4823 return (rval);
4827 * Function: cmlb_clear_vtoc
4829 * Description: This routine clears out the VTOC labels.
4831 * Arguments:
4832 * cl driver soft state (unit) structure
4834 * tg_cookie cookie from target driver to be passed back to target
4835 * driver when we call back to it through tg_ops.
4837 * Return: void
4839 static void
4840 cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
4842 struct dk_label *dkl;
4844 mutex_exit(CMLB_MUTEX(cl));
4845 dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4846 mutex_enter(CMLB_MUTEX(cl));
4848 * cmlb_set_vtoc uses these fields in order to figure out
4849 * where to overwrite the backup labels
4851 dkl->dkl_apc = cl->cl_g.dkg_apc;
4852 dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4853 dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4854 dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4855 dkl->dkl_nsect = cl->cl_g.dkg_nsect;
4856 mutex_exit(CMLB_MUTEX(cl));
4857 (void) cmlb_set_vtoc(cl, dkl, tg_cookie);
4858 kmem_free(dkl, cl->cl_sys_blocksize);
4860 mutex_enter(CMLB_MUTEX(cl));
4864 * Function: cmlb_write_label
4866 * Description: This routine will validate and write the driver soft state vtoc
4867 * contents to the device.
4869 * Arguments:
4870 * cl cmlb handle
4872 * tg_cookie cookie from target driver to be passed back to target
4873 * driver when we call back to it through tg_ops.
4876 * Return Code: the code returned by cmlb_send_scsi_cmd()
4878 * EINVAL
4879 * ENXIO
4880 * ENOMEM
4882 static int
4883 cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
4885 struct dk_label *dkl;
4886 short sum;
4887 short *sp;
4888 int i;
4889 int rval;
4891 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4892 mutex_exit(CMLB_MUTEX(cl));
4893 dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4894 mutex_enter(CMLB_MUTEX(cl));
4896 bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
4897 dkl->dkl_rpm = cl->cl_g.dkg_rpm;
4898 dkl->dkl_pcyl = cl->cl_g.dkg_pcyl;
4899 dkl->dkl_apc = cl->cl_g.dkg_apc;
4900 dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
4901 dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4902 dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4903 dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4904 dkl->dkl_nsect = cl->cl_g.dkg_nsect;
4906 #if defined(_SUNOS_VTOC_8)
4907 dkl->dkl_obs1 = cl->cl_g.dkg_obs1;
4908 dkl->dkl_obs2 = cl->cl_g.dkg_obs2;
4909 dkl->dkl_obs3 = cl->cl_g.dkg_obs3;
4910 for (i = 0; i < NDKMAP; i++) {
4911 dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
4912 dkl->dkl_map[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
4914 bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
4915 #elif defined(_SUNOS_VTOC_16)
4916 dkl->dkl_skew = cl->cl_dkg_skew;
4917 #else
4918 #error "No VTOC format defined."
4919 #endif
4921 dkl->dkl_magic = DKL_MAGIC;
4922 dkl->dkl_write_reinstruct = cl->cl_g.dkg_write_reinstruct;
4923 dkl->dkl_read_reinstruct = cl->cl_g.dkg_read_reinstruct;
4925 /* Construct checksum for the new disk label */
4926 sum = 0;
4927 sp = (short *)dkl;
4928 i = sizeof (struct dk_label) / sizeof (short);
4929 while (i--) {
4930 sum ^= *sp++;
4932 dkl->dkl_cksum = sum;
4934 mutex_exit(CMLB_MUTEX(cl));
4936 rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
4937 exit:
4938 kmem_free(dkl, cl->cl_sys_blocksize);
4939 mutex_enter(CMLB_MUTEX(cl));
4940 return (rval);
4944 * This routine implements the DKIOCSETEFI ioctl. This ioctl is currently
4945 * used to write (or clear) the GPT Partition Table header (primary/backup)
4946 * and GUID partition Entry Array (primary/backup). It is also used to write
4947 * the Protective MBR.
4949 static int
4950 cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4951 void *tg_cookie)
4953 dk_efi_t user_efi;
4954 int shift, rval = 0;
4955 void *buffer;
4956 diskaddr_t tgt_lba;
4957 boolean_t internal;
4959 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4960 return (EFAULT);
4962 internal = (cl->cl_alter_behavior & CMLB_INTERNAL_MINOR_NODES) != 0;
4964 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4965 shift = CMLBUNIT_FORCE_P0_SHIFT;
4966 else
4967 shift = CMLBUNIT_SHIFT;
4969 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4971 if (user_efi.dki_length == 0 ||
4972 user_efi.dki_length > cmlb_tg_max_efi_xfer)
4973 return (EINVAL);
4975 tgt_lba = user_efi.dki_lba;
4977 mutex_enter(CMLB_MUTEX(cl));
4978 if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4979 (cl->cl_tgt_blocksize == 0) ||
4980 (user_efi.dki_length % cl->cl_sys_blocksize)) {
4981 mutex_exit(CMLB_MUTEX(cl));
4982 return (EINVAL);
4984 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4985 tgt_lba = tgt_lba *
4986 cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
4987 mutex_exit(CMLB_MUTEX(cl));
4989 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4990 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
4991 rval = EFAULT;
4992 } else {
4994 * let's clear the vtoc labels and clear the softstate
4995 * vtoc.
4997 mutex_enter(CMLB_MUTEX(cl));
4998 if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
4999 cmlb_dbg(CMLB_TRACE, cl,
5000 "cmlb_dkio_set_efi: CLEAR VTOC\n");
5001 if (cl->cl_label_from_media == CMLB_LABEL_VTOC)
5002 cmlb_clear_vtoc(cl, tg_cookie);
5003 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5004 mutex_exit(CMLB_MUTEX(cl));
5005 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
5006 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
5007 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
5008 S_IFBLK,
5009 (CMLBUNIT(dev, shift) << shift) | WD_NODE,
5010 cl->cl_node_type, 0, internal);
5011 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
5012 S_IFCHR,
5013 (CMLBUNIT(dev, shift) << shift) | WD_NODE,
5014 cl->cl_node_type, 0, internal);
5015 } else
5016 mutex_exit(CMLB_MUTEX(cl));
5018 rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
5019 tg_cookie);
5021 if (rval == 0) {
5022 mutex_enter(CMLB_MUTEX(cl));
5023 cl->cl_f_geometry_is_valid = B_FALSE;
5024 mutex_exit(CMLB_MUTEX(cl));
5027 kmem_free(buffer, user_efi.dki_length);
5028 return (rval);
5032 * Function: cmlb_dkio_get_mboot
5034 * Description: This routine is the driver entry point for handling user
5035 * requests to get the current device mboot (DKIOCGMBOOT)
5037 * Arguments:
5038 * arg pointer to user provided mboot structure specifying
5039 * the current mboot.
5041 * flag this argument is a pass through to ddi_copyxxx()
5042 * directly from the mode argument of ioctl().
5044 * tg_cookie cookie from target driver to be passed back to target
5045 * driver when we call back to it through tg_ops.
5047 * Return Code: 0
5048 * EINVAL
5049 * EFAULT
5050 * ENXIO
5052 static int
5053 cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5055 struct mboot *mboot;
5056 int rval;
5057 size_t buffer_size;
5060 #if defined(_SUNOS_VTOC_8)
5061 if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
5062 #elif defined(_SUNOS_VTOC_16)
5063 if (arg == NULL) {
5064 #endif
5065 return (EINVAL);
5069 * Read the mboot block, located at absolute block 0 on the target.
5071 buffer_size = cl->cl_sys_blocksize;
5073 cmlb_dbg(CMLB_TRACE, cl,
5074 "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
5076 mboot = kmem_zalloc(buffer_size, KM_SLEEP);
5077 if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
5078 if (ddi_copyout(mboot, (void *)arg,
5079 sizeof (struct mboot), flag) != 0) {
5080 rval = EFAULT;
5083 kmem_free(mboot, buffer_size);
5084 return (rval);
5089 * Function: cmlb_dkio_set_mboot
5091 * Description: This routine is the driver entry point for handling user
5092 * requests to validate and set the device master boot
5093 * (DKIOCSMBOOT).
5095 * Arguments:
5096 * arg pointer to user provided mboot structure used to set the
5097 * master boot.
5099 * flag this argument is a pass through to ddi_copyxxx()
5100 * directly from the mode argument of ioctl().
5102 * tg_cookie cookie from target driver to be passed back to target
5103 * driver when we call back to it through tg_ops.
5105 * Return Code: 0
5106 * EINVAL
5107 * EFAULT
5108 * ENXIO
5110 static int
5111 cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5113 struct mboot *mboot = NULL;
5114 int rval;
5115 ushort_t magic;
5118 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5120 #if defined(_SUNOS_VTOC_8)
5121 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
5122 return (EINVAL);
5124 #endif
5126 if (arg == NULL) {
5127 return (EINVAL);
5130 mboot = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
5132 if (ddi_copyin((const void *)arg, mboot,
5133 cl->cl_sys_blocksize, flag) != 0) {
5134 kmem_free(mboot, cl->cl_sys_blocksize);
5135 return (EFAULT);
5138 /* Is this really a master boot record? */
5139 magic = LE_16(mboot->signature);
5140 if (magic != MBB_MAGIC) {
5141 kmem_free(mboot, cl->cl_sys_blocksize);
5142 return (EINVAL);
5145 rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
5147 mutex_enter(CMLB_MUTEX(cl));
5148 #if defined(__i386) || defined(__amd64)
5149 if (rval == 0) {
5151 * mboot has been written successfully.
5152 * update the fdisk and vtoc tables in memory
5154 rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
5155 if ((!cl->cl_f_geometry_is_valid) || (rval != 0)) {
5156 mutex_exit(CMLB_MUTEX(cl));
5157 kmem_free(mboot, cl->cl_sys_blocksize);
5158 return (rval);
5162 #ifdef __lock_lint
5163 cmlb_setup_default_geometry(cl, tg_cookie);
5164 #endif
5166 #else
5167 if (rval == 0) {
5169 * mboot has been written successfully.
5170 * set up the default geometry and VTOC
5172 if (cl->cl_blockcount <= CMLB_EXTVTOC_LIMIT)
5173 cmlb_setup_default_geometry(cl, tg_cookie);
5175 #endif
5176 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
5177 mutex_exit(CMLB_MUTEX(cl));
5178 kmem_free(mboot, cl->cl_sys_blocksize);
5179 return (rval);
5183 #if defined(__i386) || defined(__amd64)
5184 /*ARGSUSED*/
5185 static int
5186 cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
5187 void *tg_cookie)
5189 int fdisk_rval;
5190 diskaddr_t capacity;
5192 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5194 mutex_enter(CMLB_MUTEX(cl));
5195 capacity = cl->cl_blockcount;
5196 fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5197 if (fdisk_rval != 0) {
5198 mutex_exit(CMLB_MUTEX(cl));
5199 return (fdisk_rval);
5202 mutex_exit(CMLB_MUTEX(cl));
5203 return (fdisk_rval);
5205 #endif
5208 * Function: cmlb_setup_default_geometry
5210 * Description: This local utility routine sets the default geometry as part of
5211 * setting the device mboot.
5213 * Arguments:
5214 * cl driver soft state (unit) structure
5216 * tg_cookie cookie from target driver to be passed back to target
5217 * driver when we call back to it through tg_ops.
5220 * Note: This may be redundant with cmlb_build_default_label.
5222 static void
5223 cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
5225 struct cmlb_geom pgeom;
5226 struct cmlb_geom *pgeomp = &pgeom;
5227 int ret;
5228 int geom_base_cap = 1;
5231 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5233 /* zero out the soft state geometry and partition table. */
5234 bzero(&cl->cl_g, sizeof (struct dk_geom));
5235 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5236 bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
5239 * For the rpm, we use the minimum for the disk.
5240 * For the head, cyl and number of sector per track,
5241 * if the capacity <= 1GB, head = 64, sect = 32.
5242 * else head = 255, sect 63
5243 * Note: the capacity should be equal to C*H*S values.
5244 * This will cause some truncation of size due to
5245 * round off errors. For CD-ROMs, this truncation can
5246 * have adverse side effects, so returning ncyl and
5247 * nhead as 1. The nsect will overflow for most of
5248 * CD-ROMs as nsect is of type ushort.
5250 if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
5252 * newfs currently can not handle 255 ntracks for SPARC
5253 * so get the geometry from target driver instead of coming up
5254 * with one based on capacity.
5256 mutex_exit(CMLB_MUTEX(cl));
5257 ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
5258 mutex_enter(CMLB_MUTEX(cl));
5260 if (ret == 0) {
5261 geom_base_cap = 0;
5262 } else {
5263 cmlb_dbg(CMLB_ERROR, cl,
5264 "cmlb_setup_default_geometry: "
5265 "tg_getphygeom failed %d\n", ret);
5267 /* do default setting, geometry based on capacity */
5271 if (geom_base_cap) {
5272 if (ISCD(cl)) {
5273 cl->cl_g.dkg_ncyl = 1;
5274 cl->cl_g.dkg_nhead = 1;
5275 cl->cl_g.dkg_nsect = cl->cl_blockcount;
5276 } else if (cl->cl_blockcount < 160) {
5277 /* Less than 80K */
5278 cl->cl_g.dkg_nhead = 1;
5279 cl->cl_g.dkg_ncyl = cl->cl_blockcount;
5280 cl->cl_g.dkg_nsect = 1;
5281 } else if (cl->cl_blockcount <= 0x1000) {
5282 /* Needed for unlabeled SCSI floppies. */
5283 cl->cl_g.dkg_nhead = 2;
5284 cl->cl_g.dkg_ncyl = 80;
5285 cl->cl_g.dkg_pcyl = 80;
5286 cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
5287 } else if (cl->cl_blockcount <= 0x200000) {
5288 cl->cl_g.dkg_nhead = 64;
5289 cl->cl_g.dkg_nsect = 32;
5290 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
5291 } else {
5292 cl->cl_g.dkg_nhead = 255;
5294 cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
5295 (UINT16_MAX * 255 * 63) - 1) /
5296 (UINT16_MAX * 255 * 63)) * 63;
5298 if (cl->cl_g.dkg_nsect == 0)
5299 cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
5301 cl->cl_g.dkg_ncyl = cl->cl_blockcount /
5302 (255 * cl->cl_g.dkg_nsect);
5305 cl->cl_g.dkg_acyl = 0;
5306 cl->cl_g.dkg_bcyl = 0;
5307 cl->cl_g.dkg_intrlv = 1;
5308 cl->cl_g.dkg_rpm = 200;
5309 if (cl->cl_g.dkg_pcyl == 0)
5310 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
5311 cl->cl_g.dkg_acyl;
5312 } else {
5313 cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
5314 cl->cl_g.dkg_acyl = pgeomp->g_acyl;
5315 cl->cl_g.dkg_nhead = pgeomp->g_nhead;
5316 cl->cl_g.dkg_nsect = pgeomp->g_nsect;
5317 cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
5318 cl->cl_g.dkg_rpm = pgeomp->g_rpm;
5319 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
5322 cl->cl_g.dkg_read_reinstruct = 0;
5323 cl->cl_g.dkg_write_reinstruct = 0;
5324 cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
5325 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
5327 cl->cl_map['a'-'a'].dkl_cylno = 0;
5328 cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
5330 cl->cl_map['c'-'a'].dkl_cylno = 0;
5331 cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
5333 cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
5334 cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
5335 cl->cl_vtoc.v_nparts = V_NUMPAR;
5336 cl->cl_vtoc.v_version = V_VERSION;
5337 (void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
5338 " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
5339 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
5341 cl->cl_f_geometry_is_valid = B_FALSE;
5345 #if defined(__i386) || defined(__amd64)
5347 * Function: cmlb_update_fdisk_and_vtoc
5349 * Description: This local utility routine updates the device fdisk and vtoc
5350 * as part of setting the device mboot.
5352 * Arguments:
5353 * cl driver soft state (unit) structure
5355 * tg_cookie cookie from target driver to be passed back to target
5356 * driver when we call back to it through tg_ops.
5359 * Return Code: 0 for success or errno-type return code.
5361 * Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
5362 * these did exist separately in x86 sd.c.
5364 static int
5365 cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
5367 int count;
5368 int label_rc = 0;
5369 int fdisk_rval;
5370 diskaddr_t capacity;
5372 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5374 if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
5375 return (EINVAL);
5378 * Set up the "whole disk" fdisk partition; this should always
5379 * exist, regardless of whether the disk contains an fdisk table
5380 * or vtoc.
5382 cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
5383 cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
5386 * copy the lbasize and capacity so that if they're
5387 * reset while we're not holding the CMLB_MUTEX(cl), we will
5388 * continue to use valid values after the CMLB_MUTEX(cl) is
5389 * reacquired.
5391 capacity = cl->cl_blockcount;
5394 * refresh the logical and physical geometry caches.
5395 * (data from mode sense format/rigid disk geometry pages,
5396 * and scsi_ifgetcap("geometry").
5398 cmlb_resync_geom_caches(cl, capacity, tg_cookie);
5401 * Only DIRECT ACCESS devices will have Scl labels.
5402 * CD's supposedly have a Scl label, too
5404 if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
5405 fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5406 if (fdisk_rval != 0) {
5407 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5408 return (fdisk_rval);
5411 if (cl->cl_solaris_size <= DK_LABEL_LOC) {
5413 * Found fdisk table but no Solaris partition entry,
5414 * so don't call cmlb_uselabel() and don't create
5415 * a default label.
5417 label_rc = 0;
5418 cl->cl_f_geometry_is_valid = B_TRUE;
5419 goto no_solaris_partition;
5421 } else if (capacity < 0) {
5422 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5423 return (EINVAL);
5427 * For Removable media We reach here if we have found a
5428 * SOLARIS PARTITION.
5429 * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
5430 * PARTITION has changed from the previous one, hence we will setup a
5431 * default VTOC in this case.
5433 if (!cl->cl_f_geometry_is_valid) {
5434 /* if we get here it is writable */
5435 /* we are called from SMBOOT, and after a write of fdisk */
5436 cmlb_build_default_label(cl, tg_cookie);
5437 label_rc = 0;
5440 no_solaris_partition:
5442 #if defined(_SUNOS_VTOC_16)
5444 * If we have valid geometry, set up the remaining fdisk partitions.
5445 * Note that dkl_cylno is not used for the fdisk map entries, so
5446 * we set it to an entirely bogus value.
5448 for (count = 0; count < FDISK_PARTS; count++) {
5449 cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT32_MAX;
5450 cl->cl_map[FDISK_P1 + count].dkl_nblk =
5451 cl->cl_fmap[count].fmap_nblk;
5452 cl->cl_offset[FDISK_P1 + count] =
5453 cl->cl_fmap[count].fmap_start;
5455 #endif
5457 for (count = 0; count < NDKMAP; count++) {
5458 #if defined(_SUNOS_VTOC_8)
5459 struct dk_map *lp = &cl->cl_map[count];
5460 cl->cl_offset[count] =
5461 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
5462 #elif defined(_SUNOS_VTOC_16)
5463 struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
5464 cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
5465 #else
5466 #error "No VTOC format defined."
5467 #endif
5470 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5471 return (label_rc);
5473 #endif
5475 #if defined(__i386) || defined(__amd64)
5476 static int
5477 cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
5479 int err = 0;
5481 /* Return the driver's notion of the media's logical geometry */
5482 struct dk_geom disk_geom;
5483 struct dk_geom *dkgp = &disk_geom;
5485 mutex_enter(CMLB_MUTEX(cl));
5487 * If there is no HBA geometry available, or
5488 * if the HBA returned us something that doesn't
5489 * really fit into an Int 13/function 8 geometry
5490 * result, just fail the ioctl. See PSARC 1998/313.
5492 if (cl->cl_lgeom.g_nhead == 0 ||
5493 cl->cl_lgeom.g_nsect == 0 ||
5494 cl->cl_lgeom.g_ncyl > 1024) {
5495 mutex_exit(CMLB_MUTEX(cl));
5496 err = EINVAL;
5497 } else {
5498 dkgp->dkg_ncyl = cl->cl_lgeom.g_ncyl;
5499 dkgp->dkg_acyl = cl->cl_lgeom.g_acyl;
5500 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5501 dkgp->dkg_nhead = cl->cl_lgeom.g_nhead;
5502 dkgp->dkg_nsect = cl->cl_lgeom.g_nsect;
5504 mutex_exit(CMLB_MUTEX(cl));
5505 if (ddi_copyout(dkgp, (void *)arg,
5506 sizeof (struct dk_geom), flag)) {
5507 err = EFAULT;
5508 } else {
5509 err = 0;
5512 return (err);
5514 #endif
5516 #if defined(__i386) || defined(__amd64)
5517 static int
5518 cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
5519 void *tg_cookie)
5521 int err = 0;
5522 diskaddr_t capacity;
5525 /* Return the driver's notion of the media physical geometry */
5526 struct dk_geom disk_geom;
5527 struct dk_geom *dkgp = &disk_geom;
5529 mutex_enter(CMLB_MUTEX(cl));
5531 if (cl->cl_g.dkg_nhead != 0 &&
5532 cl->cl_g.dkg_nsect != 0) {
5534 * We succeeded in getting a geometry, but
5535 * right now it is being reported as just the
5536 * Solaris fdisk partition, just like for
5537 * DKIOCGGEOM. We need to change that to be
5538 * correct for the entire disk now.
5540 bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
5541 dkgp->dkg_acyl = 0;
5542 dkgp->dkg_ncyl = cl->cl_blockcount /
5543 (dkgp->dkg_nhead * dkgp->dkg_nsect);
5544 } else {
5545 bzero(dkgp, sizeof (struct dk_geom));
5547 * This disk does not have a Solaris VTOC
5548 * so we must present a physical geometry
5549 * that will remain consistent regardless
5550 * of how the disk is used. This will ensure
5551 * that the geometry does not change regardless
5552 * of the fdisk partition type (ie. EFI, FAT32,
5553 * Solaris, etc).
5555 if (ISCD(cl)) {
5556 dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
5557 dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
5558 dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
5559 dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
5560 } else {
5562 * Invalid cl_blockcount can generate invalid
5563 * dk_geom and may result in division by zero
5564 * system failure. Should make sure blockcount
5565 * is valid before using it here.
5567 if (cl->cl_blockcount == 0) {
5568 mutex_exit(CMLB_MUTEX(cl));
5569 err = EIO;
5570 return (err);
5573 * Refer to comments related to off-by-1 at the
5574 * header of this file
5576 if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
5577 capacity = cl->cl_blockcount - 1;
5578 else
5579 capacity = cl->cl_blockcount;
5581 cmlb_convert_geometry(cl, capacity, dkgp, tg_cookie);
5582 dkgp->dkg_acyl = 0;
5583 dkgp->dkg_ncyl = capacity /
5584 (dkgp->dkg_nhead * dkgp->dkg_nsect);
5587 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5589 mutex_exit(CMLB_MUTEX(cl));
5590 if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
5591 err = EFAULT;
5593 return (err);
5595 #endif
5597 #if defined(__i386) || defined(__amd64)
5598 static int
5599 cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
5601 int err = 0;
5604 * Return parameters describing the selected disk slice.
5605 * Note: this ioctl is for the intel platform only
5607 int part;
5609 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
5610 part = getminor(dev) & ((1 << CMLBUNIT_FORCE_P0_SHIFT) - 1);
5611 else
5612 part = CMLBPART(dev);
5614 mutex_enter(CMLB_MUTEX(cl));
5615 /* don't check cl_solaris_size for pN */
5616 if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5617 err = EIO;
5618 mutex_exit(CMLB_MUTEX(cl));
5619 } else {
5620 struct part_info p;
5622 p.p_start = (daddr_t)cl->cl_offset[part];
5623 p.p_length = (int)cl->cl_map[part].dkl_nblk;
5624 mutex_exit(CMLB_MUTEX(cl));
5625 #ifdef _MULTI_DATAMODEL
5626 switch (ddi_model_convert_from(flag & FMODELS)) {
5627 case DDI_MODEL_ILP32:
5629 struct part_info32 p32;
5631 p32.p_start = (daddr32_t)p.p_start;
5632 p32.p_length = p.p_length;
5633 if (ddi_copyout(&p32, (void *)arg,
5634 sizeof (p32), flag))
5635 err = EFAULT;
5636 break;
5639 case DDI_MODEL_NONE:
5641 if (ddi_copyout(&p, (void *)arg, sizeof (p),
5642 flag))
5643 err = EFAULT;
5644 break;
5647 #else /* ! _MULTI_DATAMODEL */
5648 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5649 err = EFAULT;
5650 #endif /* _MULTI_DATAMODEL */
5652 return (err);
5654 static int
5655 cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
5657 int err = 0;
5660 * Return parameters describing the selected disk slice.
5661 * Note: this ioctl is for the intel platform only
5663 int part;
5665 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
5666 part = getminor(dev) & ((1 << CMLBUNIT_FORCE_P0_SHIFT) - 1);
5667 else
5668 part = CMLBPART(dev);
5670 mutex_enter(CMLB_MUTEX(cl));
5671 /* don't check cl_solaris_size for pN */
5672 if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5673 err = EIO;
5674 mutex_exit(CMLB_MUTEX(cl));
5675 } else {
5676 struct extpart_info p;
5678 p.p_start = (diskaddr_t)cl->cl_offset[part];
5679 p.p_length = (diskaddr_t)cl->cl_map[part].dkl_nblk;
5680 mutex_exit(CMLB_MUTEX(cl));
5681 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5682 err = EFAULT;
5684 return (err);
5686 #endif
5689 cmlb_prop_op(cmlb_handle_t cmlbhandle,
5690 dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
5691 char *name, caddr_t valuep, int *lengthp, int part, void *tg_cookie)
5693 struct cmlb_lun *cl;
5694 diskaddr_t capacity;
5695 uint32_t lbasize;
5696 enum dp { DP_NBLOCKS, DP_BLKSIZE, DP_SSD, DP_ROT } dp;
5697 int callers_length;
5698 caddr_t buffer;
5699 uint64_t nblocks64;
5700 uint_t dblk;
5701 tg_attribute_t tgattr;
5703 /* Always fallback to ddi_prop_op... */
5704 cl = (struct cmlb_lun *)cmlbhandle;
5705 if (cl == NULL) {
5706 fallback: return (ddi_prop_op(dev, dip, prop_op, mod_flags,
5707 name, valuep, lengthp));
5710 /* Pick up capacity and blocksize information. */
5711 capacity = cl->cl_blockcount;
5712 if (capacity == 0)
5713 goto fallback;
5714 lbasize = cl->cl_tgt_blocksize;
5715 if (lbasize == 0)
5716 lbasize = DEV_BSIZE; /* 0 -> DEV_BSIZE units */
5718 /* Check for dynamic property of whole device. */
5719 if (dev == DDI_DEV_T_ANY) {
5720 /* Fallback to ddi_prop_op if we don't understand. */
5721 if (strcmp(name, "device-nblocks") == 0)
5722 dp = DP_NBLOCKS;
5723 else if (strcmp(name, "device-blksize") == 0)
5724 dp = DP_BLKSIZE;
5725 else if (strcmp(name, "device-solid-state") == 0)
5726 dp = DP_SSD;
5727 else if (strcmp(name, "device-rotational") == 0)
5728 dp = DP_ROT;
5729 else
5730 goto fallback;
5732 /* get callers length, establish length of our dynamic prop */
5733 callers_length = *lengthp;
5734 if (dp == DP_NBLOCKS)
5735 *lengthp = sizeof (uint64_t);
5736 else if ((dp == DP_BLKSIZE) || (dp == DP_SSD))
5737 *lengthp = sizeof (uint32_t);
5739 /* service request for the length of the property */
5740 if (prop_op == PROP_LEN)
5741 return (DDI_PROP_SUCCESS);
5743 switch (prop_op) {
5744 case PROP_LEN_AND_VAL_ALLOC:
5745 if ((buffer = kmem_alloc(*lengthp,
5746 (mod_flags & DDI_PROP_CANSLEEP) ?
5747 KM_SLEEP : KM_NOSLEEP)) == NULL)
5748 return (DDI_PROP_NO_MEMORY);
5749 *(caddr_t *)valuep = buffer; /* set callers buf */
5750 break;
5752 case PROP_LEN_AND_VAL_BUF:
5753 /* the length of the prop and the request must match */
5754 if (callers_length != *lengthp)
5755 return (DDI_PROP_INVAL_ARG);
5756 buffer = valuep; /* get callers buf */
5757 break;
5759 default:
5760 return (DDI_PROP_INVAL_ARG);
5763 /* transfer the value into the buffer */
5764 switch (dp) {
5765 case DP_NBLOCKS:
5766 *((uint64_t *)buffer) = capacity;
5767 break;
5768 case DP_BLKSIZE:
5769 *((uint32_t *)buffer) = lbasize;
5770 break;
5771 case DP_SSD:
5772 if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5773 tgattr.media_is_solid_state = B_FALSE;
5774 *((uint32_t *)buffer) =
5775 tgattr.media_is_solid_state ? 1 : 0;
5776 break;
5777 case DP_ROT:
5778 if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5779 tgattr.media_is_rotational = B_TRUE;
5780 *((uint32_t *)buffer) =
5781 tgattr.media_is_rotational ? 1 : 0;
5782 break;
5784 return (DDI_PROP_SUCCESS);
5788 * Support dynamic size oriented properties of partition. Requests
5789 * issued under conditions where size is valid are passed to
5790 * ddi_prop_op_nblocks with the size information, otherwise the
5791 * request is passed to ddi_prop_op. Size depends on valid geometry.
5793 if (!cmlb_is_valid(cmlbhandle))
5794 goto fallback;
5796 /* Get partition nblocks value. */
5797 (void) cmlb_partinfo(cmlbhandle, part,
5798 (diskaddr_t *)&nblocks64, NULL, NULL, NULL, tg_cookie);
5801 * Assume partition information is in sys_blocksize units, compute
5802 * divisor for size(9P) property representation.
5804 dblk = lbasize / cl->cl_sys_blocksize;
5806 /* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5807 return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
5808 name, valuep, lengthp, nblocks64 / dblk, lbasize));