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