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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * This file contains functions to implement automatic configuration 28 * of scsi disks. 29 */ 30 #include "global.h" 31 32 #include <fcntl.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <strings.h> 36 #include <stdlib.h> 37 #include <ctype.h> 38 39 #include "misc.h" 40 #include "param.h" 41 #include "ctlr_scsi.h" 42 #include "auto_sense.h" 43 #include "partition.h" 44 #include "label.h" 45 #include "startup.h" 46 #include "analyze.h" 47 #include "io.h" 48 #include "hardware_structs.h" 49 #include "menu_fdisk.h" 50 51 52 #define DISK_NAME_MAX 256 53 54 extern int nctypes; 55 extern struct ctlr_type ctlr_types[]; 56 57 58 /* 59 * Marker for free hog partition 60 */ 61 #define HOG (-1) 62 63 64 65 /* 66 * Default partition tables 67 * 68 * Disk capacity root swap usr 69 * ------------- ---- ---- --- 70 * 0mb to 64mb 0 0 remainder 71 * 64mb to 180mb 16mb 16mb remainder 72 * 180mb to 280mb 16mb 32mb remainder 73 * 280mb to 380mb 24mb 32mb remainder 74 * 380mb to 600mb 32mb 32mb remainder 75 * 600mb to 1gb 32mb 64mb remainder 76 * 1gb to 2gb 64mb 128mb remainder 77 * 2gb on up 128mb 128mb remainder 78 */ 79 struct part_table { 80 int partitions[NDKMAP]; 81 }; 82 83 static struct part_table part_table_64mb = { 84 { 0, 0, 0, 0, 0, 0, HOG, 0} 85 }; 86 87 static struct part_table part_table_180mb = { 88 { 16, 16, 0, 0, 0, 0, HOG, 0} 89 }; 90 91 static struct part_table part_table_280mb = { 92 { 16, 32, 0, 0, 0, 0, HOG, 0} 93 }; 94 95 static struct part_table part_table_380mb = { 96 { 24, 32, 0, 0, 0, 0, HOG, 0} 97 }; 98 99 static struct part_table part_table_600mb = { 100 { 32, 32, 0, 0, 0, 0, HOG, 0} 101 }; 102 103 static struct part_table part_table_1gb = { 104 { 32, 64, 0, 0, 0, 0, HOG, 0} 105 }; 106 107 static struct part_table part_table_2gb = { 108 { 64, 128, 0, 0, 0, 0, HOG, 0} 109 }; 110 111 static struct part_table part_table_infinity = { 112 { 128, 128, 0, 0, 0, 0, HOG, 0} 113 }; 114 115 116 static struct default_partitions { 117 diskaddr_t min_capacity; 118 diskaddr_t max_capacity; 119 struct part_table *part_table; 120 } default_partitions[] = { 121 { 0, 64, &part_table_64mb }, /* 0 to 64 mb */ 122 { 64, 180, &part_table_180mb }, /* 64 to 180 mb */ 123 { 180, 280, &part_table_280mb }, /* 180 to 280 mb */ 124 { 280, 380, &part_table_380mb }, /* 280 to 380 mb */ 125 { 380, 600, &part_table_600mb }, /* 380 to 600 mb */ 126 { 600, 1024, &part_table_1gb }, /* 600 to 1 gb */ 127 { 1024, 2048, &part_table_2gb }, /* 1 to 2 gb */ 128 { 2048, INFINITY, &part_table_infinity }, /* 2 gb on up */ 129 }; 130 131 #define DEFAULT_PARTITION_TABLE_SIZE \ 132 (sizeof (default_partitions) / sizeof (struct default_partitions)) 133 134 /* 135 * msgs for check() 136 */ 137 #define FORMAT_MSG "Auto configuration via format.dat" 138 #define GENERIC_MSG "Auto configuration via generic SCSI-2" 139 140 /* 141 * Disks on symbios(Hardwire raid controller) return a fixed number 142 * of heads(64)/cylinders(64) and adjust the cylinders depending 143 * capacity of the configured lun. 144 * In such a case we get number of physical cylinders < 3 which 145 * is the minimum required by solaris(2 reserved + 1 data cylinders). 146 * Hence try to adjust the cylinders by reducing the "nsect/nhead". 147 * 148 */ 149 /* 150 * assuming a minimum of 32 block cylinders. 151 */ 152 #define MINIMUM_NO_HEADS 2 153 #define MINIMUM_NO_SECTORS 16 154 155 #define MINIMUM_NO_CYLINDERS 128 156 157 #if defined(_SUNOS_VTOC_8) 158 159 /* These are 16-bit fields */ 160 #define MAXIMUM_NO_HEADS 65535 161 #define MAXIMUM_NO_SECTORS 65535 162 #define MAXIMUM_NO_CYLINDERS 65535 163 164 #endif /* defined(_SUNOS_VTOC_8) */ 165 166 /* 167 * minimum number of cylinders required by Solaris. 168 */ 169 #define SUN_MIN_CYL 3 170 171 172 173 /* 174 * ANSI prototypes for local static functions 175 */ 176 static struct disk_type *generic_disk_sense( 177 int fd, 178 int can_prompt, 179 struct dk_label *label, 180 struct scsi_inquiry *inquiry, 181 struct scsi_capacity_16 *capacity, 182 char *disk_name); 183 static int use_existing_disk_type( 184 int fd, 185 int can_prompt, 186 struct dk_label *label, 187 struct scsi_inquiry *inquiry, 188 struct disk_type *disk_type, 189 struct scsi_capacity_16 *capacity); 190 int build_default_partition(struct dk_label *label, 191 int ctrl_type); 192 static struct disk_type *find_scsi_disk_type( 193 char *disk_name, 194 struct dk_label *label); 195 static struct disk_type *find_scsi_disk_by_name( 196 char *disk_name); 197 static struct ctlr_type *find_scsi_ctlr_type(void); 198 static struct ctlr_info *find_scsi_ctlr_info( 199 struct dk_cinfo *dkinfo); 200 static struct disk_type *new_scsi_disk_type( 201 int fd, 202 char *disk_name, 203 struct dk_label *label); 204 static struct disk_info *find_scsi_disk_info( 205 struct dk_cinfo *dkinfo); 206 207 static struct disk_type *new_direct_disk_type(int fd, char *disk_name, 208 struct dk_label *label); 209 210 static struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo); 211 static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 212 static int auto_label_init(struct dk_label *label); 213 static struct ctlr_type *find_direct_ctlr_type(void); 214 static struct ctlr_info *find_direct_ctlr_info(struct dk_cinfo *dkinfo); 215 static struct disk_info *find_direct_disk_info(struct dk_cinfo *dkinfo); 216 217 static char *get_sun_disk_name( 218 char *disk_name, 219 struct scsi_inquiry *inquiry); 220 static char *get_generic_disk_name( 221 char *disk_name, 222 struct scsi_inquiry *inquiry); 223 static int force_blocksize(int fd); 224 static int raw_format(int fd); 225 static char *strcopy( 226 char *dst, 227 char *src, 228 int n); 229 static int adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl, 230 uint_t *nsect, uint_t *nhead); 231 static void compute_chs_values(diskaddr_t total_capacity, 232 diskaddr_t usable_capacity, uint_t *pcylp, 233 uint_t *nheadp, uint_t *nsectp); 234 #if defined(_SUNOS_VTOC_8) 235 static diskaddr_t square_box( 236 diskaddr_t capacity, 237 uint_t *dim1, uint_t lim1, 238 uint_t *dim2, uint_t lim2, 239 uint_t *dim3, uint_t lim3); 240 #endif /* defined(_SUNOS_VTOC_8) */ 241 242 243 /* 244 * We need to get information necessary to construct a *new* efi 245 * label type 246 */ 247 struct disk_type * 248 auto_efi_sense(int fd, struct efi_info *label) 249 { 250 251 struct dk_gpt *vtoc; 252 int i; 253 254 struct disk_type *disk, *dp; 255 struct disk_info *disk_info; 256 struct ctlr_info *ctlr; 257 struct dk_cinfo dkinfo; 258 struct partition_info *part; 259 260 /* 261 * get vendor, product, revision and capacity info. 262 */ 263 if (get_disk_info(fd, label) == -1) { 264 return ((struct disk_type *)NULL); 265 } 266 /* 267 * Now build the default partition table 268 */ 269 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) { 270 err_print("efi_alloc_and_init failed. \n"); 271 return ((struct disk_type *)NULL); 272 } 273 274 label->e_parts = vtoc; 275 276 /* 277 * Create a whole hog EFI partition table: 278 * S0 takes the whole disk except the primary EFI label, 279 * backup EFI label, and the reserved partition. 280 */ 281 vtoc->efi_parts[0].p_tag = V_USR; 282 vtoc->efi_parts[0].p_start = vtoc->efi_first_u_lba; 283 vtoc->efi_parts[0].p_size = vtoc->efi_last_u_lba - vtoc->efi_first_u_lba 284 - EFI_MIN_RESV_SIZE + 1; 285 286 /* 287 * S1-S6 are unassigned slices. 288 */ 289 for (i = 1; i < vtoc->efi_nparts - 2; i ++) { 290 vtoc->efi_parts[i].p_tag = V_UNASSIGNED; 291 vtoc->efi_parts[i].p_start = 0; 292 vtoc->efi_parts[i].p_size = 0; 293 } 294 295 /* 296 * The reserved slice 297 */ 298 vtoc->efi_parts[vtoc->efi_nparts - 1].p_tag = V_RESERVED; 299 vtoc->efi_parts[vtoc->efi_nparts - 1].p_start = 300 vtoc->efi_last_u_lba - EFI_MIN_RESV_SIZE + 1; 301 vtoc->efi_parts[vtoc->efi_nparts - 1].p_size = EFI_MIN_RESV_SIZE; 302 303 /* 304 * Now stick all of it into the disk_type struct 305 */ 306 307 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 308 if (option_msg && diag_msg) { 309 err_print("DKIOCINFO failed\n"); 310 } 311 return (NULL); 312 } 313 if ((cur_ctype != NULL) && (cur_ctype->ctype_ctype == DKC_DIRECT)) { 314 ctlr = find_direct_ctlr_info(&dkinfo); 315 disk_info = find_direct_disk_info(&dkinfo); 316 } else { 317 ctlr = find_scsi_ctlr_info(&dkinfo); 318 disk_info = find_scsi_disk_info(&dkinfo); 319 } 320 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 321 assert(disk_info->disk_ctlr == ctlr); 322 dp = ctlr->ctlr_ctype->ctype_dlist; 323 if (dp == NULL) { 324 ctlr->ctlr_ctype->ctype_dlist = dp; 325 } else { 326 while (dp->dtype_next != NULL) { 327 dp = dp->dtype_next; 328 } 329 dp->dtype_next = disk; 330 } 331 disk->dtype_next = NULL; 332 333 (void) strlcpy(disk->vendor, label->vendor, 334 sizeof (disk->vendor)); 335 (void) strlcpy(disk->product, label->product, 336 sizeof (disk->product)); 337 (void) strlcpy(disk->revision, label->revision, 338 sizeof (disk->revision)); 339 disk->capacity = label->capacity; 340 341 part = (struct partition_info *) 342 zalloc(sizeof (struct partition_info)); 343 disk->dtype_plist = part; 344 345 part->pinfo_name = alloc_string("default"); 346 part->pinfo_next = NULL; 347 part->etoc = vtoc; 348 349 bzero(disk_info->v_volume, LEN_DKL_VVOL); 350 disk_info->disk_parts = part; 351 return (disk); 352 } 353 354 static int 355 efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 356 { 357 void *data = dk_ioc->dki_data; 358 int error; 359 360 dk_ioc->dki_data_64 = (uint64_t)(uintptr_t)data; 361 error = ioctl(fd, cmd, (void *)dk_ioc); 362 dk_ioc->dki_data = data; 363 364 return (error); 365 } 366 367 static struct ctlr_type * 368 find_direct_ctlr_type() 369 { 370 struct mctlr_list *mlp; 371 372 mlp = controlp; 373 374 while (mlp != NULL) { 375 if (mlp->ctlr_type->ctype_ctype == DKC_DIRECT) { 376 return (mlp->ctlr_type); 377 } 378 mlp = mlp->next; 379 } 380 381 impossible("no DIRECT controller type"); 382 383 return ((struct ctlr_type *)NULL); 384 } 385 386 static struct ctlr_info * 387 find_direct_ctlr_info( 388 struct dk_cinfo *dkinfo) 389 { 390 struct ctlr_info *ctlr; 391 392 if (dkinfo->dki_ctype != DKC_DIRECT) 393 return (NULL); 394 395 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) { 396 if (ctlr->ctlr_addr == dkinfo->dki_addr && 397 ctlr->ctlr_space == dkinfo->dki_space && 398 ctlr->ctlr_ctype->ctype_ctype == DKC_DIRECT) { 399 return (ctlr); 400 } 401 } 402 403 impossible("no DIRECT controller info"); 404 /*NOTREACHED*/ 405 } 406 407 static struct disk_info * 408 find_direct_disk_info( 409 struct dk_cinfo *dkinfo) 410 { 411 struct disk_info *disk; 412 struct dk_cinfo *dp; 413 414 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 415 assert(dkinfo->dki_ctype == DKC_DIRECT); 416 dp = &disk->disk_dkinfo; 417 if (dp->dki_ctype == dkinfo->dki_ctype && 418 dp->dki_cnum == dkinfo->dki_cnum && 419 dp->dki_unit == dkinfo->dki_unit && 420 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) { 421 return (disk); 422 } 423 } 424 425 impossible("No DIRECT disk info instance\n"); 426 /*NOTREACHED*/ 427 } 428 429 /* 430 * To convert EFI to SMI labels, we need to get label geometry. 431 * Unfortunately at this time there is no good way to do so. 432 * DKIOCGGEOM will fail if disk is EFI labeled. So we hack around 433 * it and clear EFI label, do a DKIOCGGEOM and put the EFI label 434 * back on disk. 435 * This routine gets the label geometry and initializes the label 436 * It uses cur_file as opened device. 437 * returns 0 if succeeds or -1 if failed. 438 */ 439 static int 440 auto_label_init(struct dk_label *label) 441 { 442 dk_efi_t dk_ioc; 443 dk_efi_t dk_ioc_back; 444 efi_gpt_t *data = NULL; 445 efi_gpt_t *databack = NULL; 446 struct dk_geom disk_geom; 447 struct dk_minfo disk_info; 448 efi_gpt_t *backsigp; 449 int fd = cur_file; 450 int rval = -1; 451 int efisize = EFI_LABEL_SIZE * 2; 452 int success = 0; 453 uint64_t sig; 454 uint64_t backsig; 455 456 if ((data = calloc(efisize, 1)) == NULL) { 457 err_print("auto_label_init: calloc failed\n"); 458 goto auto_label_init_out; 459 } 460 461 dk_ioc.dki_data = data; 462 dk_ioc.dki_lba = 1; 463 dk_ioc.dki_length = efisize; 464 465 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc) != 0) { 466 err_print("auto_label_init: GETEFI failed\n"); 467 goto auto_label_init_out; 468 } 469 470 if ((databack = calloc(efisize, 1)) == NULL) { 471 err_print("auto_label_init calloc2 failed"); 472 goto auto_label_init_out; 473 } 474 475 /* get the LBA size and capacity */ 476 if (ioctl(fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) == -1) { 477 err_print("auto_label_init: dkiocgmediainfo failed\n"); 478 goto auto_label_init_out; 479 } 480 481 if (disk_info.dki_lbsize == 0) { 482 if (option_msg && diag_msg) { 483 err_print("auto_lbal_init: assuming 512 byte" 484 "block size"); 485 } 486 disk_info.dki_lbsize = DEV_BSIZE; 487 } 488 489 if (disk_info.dki_lbsize != DEV_BSIZE) { 490 err_print("auto_label_init: lbasize is not 512\n"); 491 goto auto_label_init_out; 492 } 493 494 dk_ioc_back.dki_data = databack; 495 496 /* 497 * back up efi label goes to capacity - 1, we are reading an extra block 498 * before the back up label. 499 */ 500 dk_ioc_back.dki_lba = disk_info.dki_capacity - 1 - 1; 501 dk_ioc_back.dki_length = efisize; 502 503 if (efi_ioctl(fd, DKIOCGETEFI, &dk_ioc_back) != 0) { 504 err_print("auto_label_init: GETEFI backup failed\n"); 505 goto auto_label_init_out; 506 } 507 508 sig = dk_ioc.dki_data->efi_gpt_Signature; 509 dk_ioc.dki_data->efi_gpt_Signature = 0x0; 510 511 enter_critical(); 512 513 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc) == -1) { 514 err_print("auto_label_init: SETEFI failed\n"); 515 exit_critical(); 516 goto auto_label_init_out; 517 } 518 519 backsigp = (efi_gpt_t *)((uintptr_t)dk_ioc_back.dki_data + DEV_BSIZE); 520 521 backsig = backsigp->efi_gpt_Signature; 522 523 backsigp->efi_gpt_Signature = 0; 524 525 if (efi_ioctl(fd, DKIOCSETEFI, &dk_ioc_back) == -1) { 526 err_print("auto_label_init: SETEFI backup failed\n"); 527 } 528 529 if (ioctl(cur_file, DKIOCGGEOM, &disk_geom) != 0) 530 err_print("auto_label_init: GGEOM failed\n"); 531 else 532 success = 1; 533 534 dk_ioc.dki_data->efi_gpt_Signature = sig; 535 backsigp->efi_gpt_Signature = backsig; 536 537 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc_back) == -1) { 538 err_print("auto_label_init: SETEFI revert backup failed\n"); 539 success = 0; 540 } 541 542 if (efi_ioctl(cur_file, DKIOCSETEFI, &dk_ioc) == -1) { 543 err_print("auto_label_init: SETEFI revert failed\n"); 544 success = 0; 545 } 546 547 exit_critical(); 548 549 if (success == 0) 550 goto auto_label_init_out; 551 552 ncyl = disk_geom.dkg_ncyl; 553 acyl = disk_geom.dkg_acyl; 554 nhead = disk_geom.dkg_nhead; 555 nsect = disk_geom.dkg_nsect; 556 pcyl = ncyl + acyl; 557 558 label->dkl_pcyl = pcyl; 559 label->dkl_ncyl = ncyl; 560 label->dkl_acyl = acyl; 561 label->dkl_nhead = nhead; 562 label->dkl_nsect = nsect; 563 label->dkl_apc = 0; 564 label->dkl_intrlv = 1; 565 label->dkl_rpm = disk_geom.dkg_rpm; 566 567 label->dkl_magic = DKL_MAGIC; 568 569 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 570 "%s cyl %u alt %u hd %u sec %u", 571 "DEFAULT", ncyl, acyl, nhead, nsect); 572 573 rval = 0; 574 #if defined(_FIRMWARE_NEEDS_FDISK) 575 (void) auto_solaris_part(label); 576 ncyl = label->dkl_ncyl; 577 578 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 579 580 if (!build_default_partition(label, DKC_DIRECT)) { 581 rval = -1; 582 } 583 584 (void) checksum(label, CK_MAKESUM); 585 586 587 auto_label_init_out: 588 if (data) 589 free(data); 590 if (databack) 591 free(databack); 592 593 return (rval); 594 } 595 596 static struct disk_type * 597 new_direct_disk_type( 598 int fd, 599 char *disk_name, 600 struct dk_label *label) 601 { 602 struct disk_type *dp; 603 struct disk_type *disk; 604 struct ctlr_info *ctlr; 605 struct dk_cinfo dkinfo; 606 struct partition_info *part = NULL; 607 struct partition_info *pt; 608 struct disk_info *disk_info; 609 int i; 610 611 /* 612 * Get the disk controller info for this disk 613 */ 614 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 615 if (option_msg && diag_msg) { 616 err_print("DKIOCINFO failed\n"); 617 } 618 return (NULL); 619 } 620 621 /* 622 * Find the ctlr_info for this disk. 623 */ 624 ctlr = find_direct_ctlr_info(&dkinfo); 625 626 /* 627 * Allocate a new disk type for the direct controller. 628 */ 629 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 630 631 /* 632 * Find the disk_info instance for this disk. 633 */ 634 disk_info = find_direct_disk_info(&dkinfo); 635 636 /* 637 * The controller and the disk should match. 638 */ 639 assert(disk_info->disk_ctlr == ctlr); 640 641 /* 642 * Link the disk into the list of disks 643 */ 644 dp = ctlr->ctlr_ctype->ctype_dlist; 645 if (dp == NULL) { 646 ctlr->ctlr_ctype->ctype_dlist = dp; 647 } else { 648 while (dp->dtype_next != NULL) { 649 dp = dp->dtype_next; 650 } 651 dp->dtype_next = disk; 652 } 653 disk->dtype_next = NULL; 654 655 /* 656 * Allocate and initialize the disk name. 657 */ 658 disk->dtype_asciilabel = alloc_string(disk_name); 659 660 /* 661 * Initialize disk geometry info 662 */ 663 disk->dtype_pcyl = label->dkl_pcyl; 664 disk->dtype_ncyl = label->dkl_ncyl; 665 disk->dtype_acyl = label->dkl_acyl; 666 disk->dtype_nhead = label->dkl_nhead; 667 disk->dtype_nsect = label->dkl_nsect; 668 disk->dtype_rpm = label->dkl_rpm; 669 670 part = (struct partition_info *) 671 zalloc(sizeof (struct partition_info)); 672 pt = disk->dtype_plist; 673 if (pt == NULL) { 674 disk->dtype_plist = part; 675 } else { 676 while (pt->pinfo_next != NULL) { 677 pt = pt->pinfo_next; 678 } 679 pt->pinfo_next = part; 680 } 681 682 part->pinfo_next = NULL; 683 684 /* 685 * Set up the partition name 686 */ 687 part->pinfo_name = alloc_string("default"); 688 689 /* 690 * Fill in the partition info from the label 691 */ 692 for (i = 0; i < NDKMAP; i++) { 693 694 #if defined(_SUNOS_VTOC_8) 695 part->pinfo_map[i] = label->dkl_map[i]; 696 697 #elif defined(_SUNOS_VTOC_16) 698 part->pinfo_map[i].dkl_cylno = 699 label->dkl_vtoc.v_part[i].p_start / 700 ((blkaddr_t)(disk->dtype_nhead * 701 disk->dtype_nsect - apc)); 702 part->pinfo_map[i].dkl_nblk = 703 label->dkl_vtoc.v_part[i].p_size; 704 #else 705 #error No VTOC format defined. 706 #endif /* defined(_SUNOS_VTOC_8) */ 707 } 708 709 /* 710 * Use the VTOC if valid, or install a default 711 */ 712 if (label->dkl_vtoc.v_version == V_VERSION) { 713 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume, 714 LEN_DKL_VVOL); 715 part->vtoc = label->dkl_vtoc; 716 } else { 717 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL); 718 set_vtoc_defaults(part); 719 } 720 721 /* 722 * Link the disk to the partition map 723 */ 724 disk_info->disk_parts = part; 725 726 return (disk); 727 } 728 729 /* 730 * Get a disk type that has label info. This is used to convert 731 * EFI label to SMI label 732 */ 733 struct disk_type * 734 auto_direct_get_geom_label(int fd, struct dk_label *label) 735 { 736 struct disk_type *disk_type; 737 738 if (auto_label_init(label) != 0) { 739 err_print("auto_direct_get_geom_label: failed to get label" 740 "geometry"); 741 return (NULL); 742 } else { 743 disk_type = new_direct_disk_type(fd, "DEFAULT", label); 744 return (disk_type); 745 } 746 } 747 748 /* 749 * Auto-sense a scsi disk configuration, ie get the information 750 * necessary to construct a label. We have two different 751 * ways to auto-sense a scsi disk: 752 * - format.dat override, via inquiry name 753 * - generic scsi, via standard mode sense and inquiry 754 * Depending on how and when we are called, and/or 755 * change geometry and reformat. 756 */ 757 struct disk_type * 758 auto_sense( 759 int fd, 760 int can_prompt, 761 struct dk_label *label) 762 { 763 struct scsi_inquiry inquiry; 764 struct scsi_capacity_16 capacity; 765 struct disk_type *disk_type; 766 char disk_name[DISK_NAME_MAX]; 767 int force_format_dat = 0; 768 int force_generic = 0; 769 u_ioparam_t ioparam; 770 int deflt; 771 772 /* 773 * First, if expert mode, find out if the user 774 * wants to override any of the standard methods. 775 */ 776 if (can_prompt && expert_mode) { 777 deflt = 1; 778 ioparam.io_charlist = confirm_list; 779 if (input(FIO_MSTR, FORMAT_MSG, '?', &ioparam, 780 &deflt, DATA_INPUT) == 0) { 781 force_format_dat = 1; 782 } else if (input(FIO_MSTR, GENERIC_MSG, '?', &ioparam, 783 &deflt, DATA_INPUT) == 0) { 784 force_generic = 1; 785 } 786 } 787 788 /* 789 * Get the Inquiry data. If this fails, there's 790 * no hope for this disk, so give up. 791 */ 792 if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) { 793 return ((struct disk_type *)NULL); 794 } 795 if (option_msg && diag_msg) { 796 err_print("Product id: "); 797 print_buf(inquiry.inq_pid, sizeof (inquiry.inq_pid)); 798 err_print("\n"); 799 } 800 801 /* 802 * Get the Read Capacity 803 */ 804 if (uscsi_read_capacity(fd, &capacity)) { 805 return ((struct disk_type *)NULL); 806 } 807 808 /* 809 * If the reported capacity is set to zero, then the disk 810 * is not usable. If the reported capacity is set to all 811 * 0xf's, then this disk is too large. These could only 812 * happen with a device that supports LBAs larger than 64 813 * bits which are not defined by any current T10 standards 814 * or by error responding from target. 815 */ 816 if ((capacity.sc_capacity == 0) || 817 (capacity.sc_capacity == UINT_MAX64)) { 818 if (option_msg && diag_msg) { 819 err_print("Invalid capacity\n"); 820 } 821 return ((struct disk_type *)NULL); 822 } 823 if (option_msg && diag_msg) { 824 err_print("blocks: %llu (0x%llx)\n", 825 capacity.sc_capacity, capacity.sc_capacity); 826 err_print("blksize: %u\n", capacity.sc_lbasize); 827 } 828 829 /* 830 * Extract the disk name for the format.dat override 831 */ 832 (void) get_sun_disk_name(disk_name, &inquiry); 833 if (option_msg && diag_msg) { 834 err_print("disk name: `%s`\n", disk_name); 835 } 836 837 /* 838 * Figure out which method we use for auto sense. 839 * If a particular method fails, we fall back to 840 * the next possibility. 841 */ 842 843 if (force_generic) { 844 return (generic_disk_sense(fd, can_prompt, label, 845 &inquiry, &capacity, disk_name)); 846 } 847 848 /* 849 * Try for an existing format.dat first 850 */ 851 if ((disk_type = find_scsi_disk_by_name(disk_name)) != NULL) { 852 if (use_existing_disk_type(fd, can_prompt, label, 853 &inquiry, disk_type, &capacity)) { 854 return (disk_type); 855 } 856 if (force_format_dat) { 857 return (NULL); 858 } 859 } 860 861 /* 862 * Otherwise, try using generic SCSI-2 sense and inquiry. 863 */ 864 865 return (generic_disk_sense(fd, can_prompt, label, 866 &inquiry, &capacity, disk_name)); 867 } 868 869 870 871 /*ARGSUSED*/ 872 static struct disk_type * 873 generic_disk_sense( 874 int fd, 875 int can_prompt, 876 struct dk_label *label, 877 struct scsi_inquiry *inquiry, 878 struct scsi_capacity_16 *capacity, 879 char *disk_name) 880 { 881 struct disk_type *disk; 882 int setdefault = 0; 883 uint_t pcyl = 0; 884 uint_t ncyl = 0; 885 uint_t acyl = 0; 886 uint_t nhead = 0; 887 uint_t nsect = 0; 888 int rpm = 0; 889 diskaddr_t nblocks = 0; 890 diskaddr_t tblocks = 0; 891 union { 892 struct mode_format page3; 893 uchar_t buf3[MAX_MODE_SENSE_SIZE]; 894 } u_page3; 895 union { 896 struct mode_geometry page4; 897 uchar_t buf4[MAX_MODE_SENSE_SIZE]; 898 } u_page4; 899 struct scsi_capacity_16 new_capacity; 900 struct mode_format *page3 = &u_page3.page3; 901 struct mode_geometry *page4 = &u_page4.page4; 902 struct scsi_ms_header header; 903 904 /* 905 * If the name of this disk appears to be "SUN", use it, 906 * otherwise construct a name out of the generic 907 * Inquiry info. If it turns out that we already 908 * have a SUN disk type of this name that differs 909 * in geometry, we will revert to the generic name 910 * anyway. 911 */ 912 if (memcmp(disk_name, "SUN", strlen("SUN")) != 0) { 913 (void) get_generic_disk_name(disk_name, inquiry); 914 } 915 916 /* 917 * If the device's block size is not 512, we have to 918 * change block size, reformat, and then sense the 919 * geometry. To do this, we must be able to prompt 920 * the user. 921 */ 922 if (capacity->sc_lbasize != DEV_BSIZE) { 923 if (!can_prompt) { 924 return (NULL); 925 } 926 if (force_blocksize(fd)) { 927 goto err; 928 } 929 930 /* 931 * Get the capacity again, since this has changed 932 */ 933 if (uscsi_read_capacity(fd, &new_capacity)) { 934 goto err; 935 } 936 if (option_msg && diag_msg) { 937 err_print("blocks: %llu (0x%llx)\n", 938 new_capacity.sc_capacity, 939 new_capacity.sc_capacity); 940 err_print("blksize: %u\n", new_capacity.sc_lbasize); 941 } 942 capacity = &new_capacity; 943 if (capacity->sc_lbasize != DEV_BSIZE) { 944 goto err; 945 } 946 } 947 948 /* 949 * Get the number of blocks from Read Capacity data. Note that 950 * the logical block address range from 0 to capacity->sc_capacity. 951 * Limit the size to 2 TB (UINT32_MAX) to use with SMI labels. 952 */ 953 tblocks = (capacity->sc_capacity + 1); 954 if (tblocks > UINT32_MAX) 955 nblocks = UINT32_MAX; 956 else 957 nblocks = tblocks; 958 959 /* 960 * Get current Page 3 - Format Parameters page 961 */ 962 if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, MODE_SENSE_PC_CURRENT, 963 (caddr_t)&u_page3, MAX_MODE_SENSE_SIZE, &header)) { 964 setdefault = 1; 965 } 966 967 /* 968 * Get current Page 4 - Drive Geometry page 969 */ 970 if (uscsi_mode_sense(fd, DAD_MODE_GEOMETRY, MODE_SENSE_PC_CURRENT, 971 (caddr_t)&u_page4, MAX_MODE_SENSE_SIZE, &header)) { 972 setdefault = 1; 973 } 974 975 if (setdefault != 1) { 976 /* The inquiry of mode page 3 & page 4 are successful */ 977 /* 978 * Correct for byte order if necessary 979 */ 980 page4->rpm = BE_16(page4->rpm); 981 page4->step_rate = BE_16(page4->step_rate); 982 page3->tracks_per_zone = BE_16(page3->tracks_per_zone); 983 page3->alt_sect_zone = BE_16(page3->alt_sect_zone); 984 page3->alt_tracks_zone = BE_16(page3->alt_tracks_zone); 985 page3->alt_tracks_vol = BE_16(page3->alt_tracks_vol); 986 page3->sect_track = BE_16(page3->sect_track); 987 page3->data_bytes_sect = BE_16(page3->data_bytes_sect); 988 page3->interleave = BE_16(page3->interleave); 989 page3->track_skew = BE_16(page3->track_skew); 990 page3->cylinder_skew = BE_16(page3->cylinder_skew); 991 992 993 /* 994 * Construct a new label out of the sense data, 995 * Inquiry and Capacity. 996 * 997 * If the disk capacity is > 1TB then simply compute 998 * the CHS values based on the total disk capacity and 999 * not use the values from mode-sense data. 1000 */ 1001 if (tblocks > INT32_MAX) { 1002 compute_chs_values(tblocks, nblocks, &pcyl, &nhead, 1003 &nsect); 1004 } else { 1005 pcyl = (page4->cyl_ub << 16) + (page4->cyl_mb << 8) + 1006 page4->cyl_lb; 1007 nhead = page4->heads; 1008 nsect = page3->sect_track; 1009 } 1010 1011 rpm = page4->rpm; 1012 1013 /* 1014 * If the number of physical cylinders reported is less 1015 * the SUN_MIN_CYL(3) then try to adjust the geometry so that 1016 * we have atleast SUN_MIN_CYL cylinders. 1017 */ 1018 if (pcyl < SUN_MIN_CYL) { 1019 if (nhead == 0 || nsect == 0) { 1020 setdefault = 1; 1021 } else if (adjust_disk_geometry( 1022 (diskaddr_t)(capacity->sc_capacity + 1), 1023 &pcyl, &nhead, &nsect)) { 1024 setdefault = 1; 1025 } 1026 } 1027 } 1028 1029 if (setdefault == 1) { 1030 /* 1031 * If the number of cylinders or the number of heads reported 1032 * is zero, we think the inquiry of page 3 and page 4 failed. 1033 * We will set the geometry infomation by ourselves. 1034 */ 1035 compute_chs_values(tblocks, nblocks, &pcyl, &nhead, &nsect); 1036 } 1037 1038 /* 1039 * The sd driver reserves 2 cylinders the backup disk label and 1040 * the deviceid. Set the number of data cylinders to pcyl-acyl. 1041 */ 1042 acyl = DK_ACYL; 1043 ncyl = pcyl - acyl; 1044 1045 if (option_msg && diag_msg) { 1046 err_print("Geometry:\n"); 1047 err_print(" pcyl: %u\n", pcyl); 1048 err_print(" ncyl: %u\n", ncyl); 1049 err_print(" heads: %u\n", nhead); 1050 err_print(" nsects: %u\n", nsect); 1051 err_print(" acyl: %u\n", acyl); 1052 1053 #if defined(_SUNOS_VTOC_16) 1054 err_print(" bcyl: %u\n", bcyl); 1055 #endif /* defined(_SUNOS_VTOC_16) */ 1056 1057 err_print(" rpm: %d\n", rpm); 1058 err_print(" nblocks: %llu\n", nblocks); 1059 } 1060 1061 /* 1062 * Some drives do not support page4 or report 0 for page4->rpm, 1063 * adjust it to AVG_RPM, 3600. 1064 */ 1065 if (rpm < MIN_RPM || rpm > MAX_RPM) { 1066 err_print("The current rpm value %d is invalid," 1067 " adjusting it to %d\n", rpm, AVG_RPM); 1068 rpm = AVG_RPM; 1069 } 1070 1071 /* 1072 * Some drives report 0 for nsect (page 3, byte 10 and 11) if they 1073 * have variable number of sectors per track. So adjust nsect. 1074 * Also the value is defined as vendor specific, hence check if 1075 * it is in a tolerable range. The values (32 and 4 below) are 1076 * chosen so that this change below does not generate a different 1077 * geometry for currently supported sun disks. 1078 */ 1079 if ((nsect == 0) || 1080 ((diskaddr_t)pcyl * nhead * nsect) < (nblocks - nblocks/32) || 1081 ((diskaddr_t)pcyl * nhead * nsect) > (nblocks + nblocks/4)) { 1082 if (nblocks > (pcyl * nhead)) { 1083 err_print("Mode sense page(3) reports nsect value" 1084 " as %d, adjusting it to %llu\n", 1085 nsect, nblocks / (pcyl * nhead)); 1086 nsect = nblocks / (pcyl * nhead); 1087 } else { 1088 /* convert capacity to nsect * nhead * pcyl */ 1089 err_print("\nWARNING: Disk geometry is based on " 1090 "capacity data.\n\n"); 1091 compute_chs_values(tblocks, nblocks, &pcyl, &nhead, 1092 &nsect); 1093 ncyl = pcyl - acyl; 1094 if (option_msg && diag_msg) { 1095 err_print("Geometry:(after adjustment)\n"); 1096 err_print(" pcyl: %u\n", pcyl); 1097 err_print(" ncyl: %u\n", ncyl); 1098 err_print(" heads: %u\n", nhead); 1099 err_print(" nsects: %u\n", nsect); 1100 err_print(" acyl: %u\n", acyl); 1101 1102 #if defined(_SUNOS_VTOC_16) 1103 err_print(" bcyl: %u\n", bcyl); 1104 #endif 1105 1106 err_print(" rpm: %d\n", rpm); 1107 err_print(" nblocks: %llu\n", nblocks); 1108 } 1109 } 1110 } 1111 1112 /* 1113 * Some drives report their physical geometry such that 1114 * it is greater than the actual capacity. Adjust the 1115 * geometry to allow for this, so we don't run off 1116 * the end of the disk. 1117 */ 1118 if (((diskaddr_t)pcyl * nhead * nsect) > nblocks) { 1119 uint_t p = pcyl; 1120 if (option_msg && diag_msg) { 1121 err_print("Computed capacity (%llu) exceeds actual " 1122 "disk capacity (%llu)\n", 1123 (diskaddr_t)pcyl * nhead * nsect, nblocks); 1124 } 1125 do { 1126 pcyl--; 1127 } while (((diskaddr_t)pcyl * nhead * nsect) > nblocks); 1128 1129 if (can_prompt && expert_mode && !option_f) { 1130 /* 1131 * Try to adjust nsect instead of pcyl to see if we 1132 * can optimize. For compatability reasons do this 1133 * only in expert mode (refer to bug 1144812). 1134 */ 1135 uint_t n = nsect; 1136 do { 1137 n--; 1138 } while (((diskaddr_t)p * nhead * n) > nblocks); 1139 if (((diskaddr_t)p * nhead * n) > 1140 ((diskaddr_t)pcyl * nhead * nsect)) { 1141 u_ioparam_t ioparam; 1142 int deflt = 1; 1143 /* 1144 * Ask the user for a choice here. 1145 */ 1146 ioparam.io_bounds.lower = 1; 1147 ioparam.io_bounds.upper = 2; 1148 err_print("1. Capacity = %llu, with pcyl = %u " 1149 "nhead = %u nsect = %u\n", 1150 ((diskaddr_t)pcyl * nhead * nsect), 1151 pcyl, nhead, nsect); 1152 err_print("2. Capacity = %llu, with pcyl = %u " 1153 "nhead = %u nsect = %u\n", 1154 ((diskaddr_t)p * nhead * n), 1155 p, nhead, n); 1156 if (input(FIO_INT, "Select one of the above " 1157 "choices ", ':', &ioparam, 1158 &deflt, DATA_INPUT) == 2) { 1159 pcyl = p; 1160 nsect = n; 1161 } 1162 } 1163 } 1164 } 1165 1166 #if defined(_SUNOS_VTOC_8) 1167 /* 1168 * Finally, we need to make sure we don't overflow any of the 1169 * fields in our disk label. To do this we need to `square 1170 * the box' so to speak. We will lose bits here. 1171 */ 1172 1173 if ((pcyl > MAXIMUM_NO_CYLINDERS && 1174 ((nsect > MAXIMUM_NO_SECTORS) || 1175 (nhead > MAXIMUM_NO_HEADS))) || 1176 ((nsect > MAXIMUM_NO_SECTORS) && 1177 (nhead > MAXIMUM_NO_HEADS))) { 1178 err_print("This disk is too big to label. " 1179 " You will lose some blocks.\n"); 1180 } 1181 if ((pcyl > MAXIMUM_NO_CYLINDERS) || 1182 (nsect > MAXIMUM_NO_SECTORS) || 1183 (nhead > MAXIMUM_NO_HEADS)) { 1184 u_ioparam_t ioparam; 1185 int order; 1186 char msg[256]; 1187 1188 order = ((pcyl > nhead)<<2) | 1189 ((pcyl > nsect)<<1) | 1190 (nhead > nsect); 1191 switch (order) { 1192 case 0x7: /* pcyl > nhead > nsect */ 1193 nblocks = 1194 square_box(nblocks, 1195 &pcyl, MAXIMUM_NO_CYLINDERS, 1196 &nhead, MAXIMUM_NO_HEADS, 1197 &nsect, MAXIMUM_NO_SECTORS); 1198 break; 1199 case 0x6: /* pcyl > nsect > nhead */ 1200 nblocks = 1201 square_box(nblocks, 1202 &pcyl, MAXIMUM_NO_CYLINDERS, 1203 &nsect, MAXIMUM_NO_SECTORS, 1204 &nhead, MAXIMUM_NO_HEADS); 1205 break; 1206 case 0x4: /* nsect > pcyl > nhead */ 1207 nblocks = 1208 square_box(nblocks, 1209 &nsect, MAXIMUM_NO_SECTORS, 1210 &pcyl, MAXIMUM_NO_CYLINDERS, 1211 &nhead, MAXIMUM_NO_HEADS); 1212 break; 1213 case 0x0: /* nsect > nhead > pcyl */ 1214 nblocks = 1215 square_box(nblocks, 1216 &nsect, MAXIMUM_NO_SECTORS, 1217 &nhead, MAXIMUM_NO_HEADS, 1218 &pcyl, MAXIMUM_NO_CYLINDERS); 1219 break; 1220 case 0x3: /* nhead > pcyl > nsect */ 1221 nblocks = 1222 square_box(nblocks, 1223 &nhead, MAXIMUM_NO_HEADS, 1224 &pcyl, MAXIMUM_NO_CYLINDERS, 1225 &nsect, MAXIMUM_NO_SECTORS); 1226 break; 1227 case 0x1: /* nhead > nsect > pcyl */ 1228 nblocks = 1229 square_box(nblocks, 1230 &nhead, MAXIMUM_NO_HEADS, 1231 &nsect, MAXIMUM_NO_SECTORS, 1232 &pcyl, MAXIMUM_NO_CYLINDERS); 1233 break; 1234 default: 1235 /* How did we get here? */ 1236 impossible("label overflow adjustment"); 1237 1238 /* Do something useful */ 1239 nblocks = 1240 square_box(nblocks, 1241 &nhead, MAXIMUM_NO_HEADS, 1242 &nsect, MAXIMUM_NO_SECTORS, 1243 &pcyl, MAXIMUM_NO_CYLINDERS); 1244 break; 1245 } 1246 if (option_msg && diag_msg && 1247 (capacity->sc_capacity + 1 != nblocks)) { 1248 err_print("After adjusting geometry you lost" 1249 " %llu of %llu blocks.\n", 1250 (capacity->sc_capacity + 1 - nblocks), 1251 capacity->sc_capacity + 1); 1252 } 1253 while (can_prompt && expert_mode && !option_f) { 1254 int deflt = 1; 1255 1256 /* 1257 * Allow user to modify this by hand if desired. 1258 */ 1259 (void) sprintf(msg, 1260 "\nGeometry: %u heads, %u sectors %u cylinders" 1261 " result in %llu out of %llu blocks.\n" 1262 "Do you want to modify the device geometry", 1263 nhead, nsect, pcyl, 1264 nblocks, capacity->sc_capacity + 1); 1265 1266 ioparam.io_charlist = confirm_list; 1267 if (input(FIO_MSTR, msg, '?', &ioparam, 1268 &deflt, DATA_INPUT) != 0) 1269 break; 1270 1271 ioparam.io_bounds.lower = MINIMUM_NO_HEADS; 1272 ioparam.io_bounds.upper = MAXIMUM_NO_HEADS; 1273 nhead = input(FIO_INT, "Number of heads", ':', 1274 &ioparam, (int *)&nhead, DATA_INPUT); 1275 ioparam.io_bounds.lower = MINIMUM_NO_SECTORS; 1276 ioparam.io_bounds.upper = MAXIMUM_NO_SECTORS; 1277 nsect = input(FIO_INT, 1278 "Number of sectors per track", 1279 ':', &ioparam, (int *)&nsect, DATA_INPUT); 1280 ioparam.io_bounds.lower = SUN_MIN_CYL; 1281 ioparam.io_bounds.upper = MAXIMUM_NO_CYLINDERS; 1282 pcyl = input(FIO_INT, "Number of cylinders", 1283 ':', &ioparam, (int *)&pcyl, DATA_INPUT); 1284 nblocks = (diskaddr_t)nhead * nsect * pcyl; 1285 if (nblocks > capacity->sc_capacity + 1) { 1286 err_print("Warning: %llu blocks exceeds " 1287 "disk capacity of %llu blocks\n", 1288 nblocks, 1289 capacity->sc_capacity + 1); 1290 } 1291 } 1292 } 1293 #endif /* defined(_SUNOS_VTOC_8) */ 1294 1295 ncyl = pcyl - acyl; 1296 1297 if (option_msg && diag_msg) { 1298 err_print("\nGeometry after adjusting for capacity:\n"); 1299 err_print(" pcyl: %u\n", pcyl); 1300 err_print(" ncyl: %u\n", ncyl); 1301 err_print(" heads: %u\n", nhead); 1302 err_print(" nsects: %u\n", nsect); 1303 err_print(" acyl: %u\n", acyl); 1304 err_print(" rpm: %d\n", rpm); 1305 } 1306 1307 (void) memset((char *)label, 0, sizeof (struct dk_label)); 1308 1309 label->dkl_magic = DKL_MAGIC; 1310 1311 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 1312 "%s cyl %u alt %u hd %u sec %u", 1313 disk_name, ncyl, acyl, nhead, nsect); 1314 1315 label->dkl_pcyl = pcyl; 1316 label->dkl_ncyl = ncyl; 1317 label->dkl_acyl = acyl; 1318 label->dkl_nhead = nhead; 1319 label->dkl_nsect = nsect; 1320 label->dkl_apc = 0; 1321 label->dkl_intrlv = 1; 1322 label->dkl_rpm = rpm; 1323 1324 #if defined(_FIRMWARE_NEEDS_FDISK) 1325 if (auto_solaris_part(label) == -1) 1326 goto err; 1327 ncyl = label->dkl_ncyl; 1328 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */ 1329 1330 1331 if (!build_default_partition(label, DKC_SCSI_CCS)) { 1332 goto err; 1333 } 1334 1335 (void) checksum(label, CK_MAKESUM); 1336 1337 /* 1338 * Find an existing disk type defined for this disk. 1339 * For this to work, both the name and geometry must 1340 * match. If there is no such type, but there already 1341 * is a disk defined with that name, but with a different 1342 * geometry, construct a new generic disk name out of 1343 * the inquiry information. Whatever name we're 1344 * finally using, if there's no such disk type defined, 1345 * build a new disk definition. 1346 */ 1347 if ((disk = find_scsi_disk_type(disk_name, label)) == NULL) { 1348 if (find_scsi_disk_by_name(disk_name) != NULL) { 1349 char old_name[DISK_NAME_MAX]; 1350 (void) strcpy(old_name, disk_name); 1351 (void) get_generic_disk_name(disk_name, 1352 inquiry); 1353 if (option_msg && diag_msg) { 1354 err_print( 1355 "Changing disk type name from '%s' to '%s'\n", old_name, disk_name); 1356 } 1357 (void) snprintf(label->dkl_asciilabel, 1358 sizeof (label->dkl_asciilabel), 1359 "%s cyl %u alt %u hd %u sec %u", 1360 disk_name, ncyl, acyl, nhead, nsect); 1361 (void) checksum(label, CK_MAKESUM); 1362 disk = find_scsi_disk_type(disk_name, label); 1363 } 1364 if (disk == NULL) { 1365 disk = new_scsi_disk_type(fd, disk_name, label); 1366 if (disk == NULL) 1367 goto err; 1368 } 1369 } 1370 1371 return (disk); 1372 1373 err: 1374 if (option_msg && diag_msg) { 1375 err_print( 1376 "Configuration via generic SCSI-2 information failed\n"); 1377 } 1378 return (NULL); 1379 } 1380 1381 1382 /*ARGSUSED*/ 1383 static int 1384 use_existing_disk_type( 1385 int fd, 1386 int can_prompt, 1387 struct dk_label *label, 1388 struct scsi_inquiry *inquiry, 1389 struct disk_type *disk_type, 1390 struct scsi_capacity_16 *capacity) 1391 { 1392 struct scsi_capacity_16 new_capacity; 1393 int pcyl; 1394 int acyl; 1395 int nhead; 1396 int nsect; 1397 int rpm; 1398 1399 /* 1400 * If the device's block size is not 512, we have to 1401 * change block size, reformat, and then sense the 1402 * geometry. To do this, we must be able to prompt 1403 * the user. 1404 */ 1405 if (capacity->sc_lbasize != DEV_BSIZE) { 1406 if (!can_prompt) { 1407 return (0); 1408 } 1409 if (force_blocksize(fd)) { 1410 goto err; 1411 } 1412 1413 /* 1414 * Get the capacity again, since this has changed 1415 */ 1416 if (uscsi_read_capacity(fd, &new_capacity)) { 1417 goto err; 1418 } 1419 1420 if (option_msg && diag_msg) { 1421 err_print("blocks: %llu (0x%llx)\n", 1422 new_capacity.sc_capacity, 1423 new_capacity.sc_capacity); 1424 err_print("blksize: %u\n", new_capacity.sc_lbasize); 1425 } 1426 1427 capacity = &new_capacity; 1428 if (capacity->sc_lbasize != DEV_BSIZE) { 1429 goto err; 1430 } 1431 } 1432 1433 /* 1434 * Construct a new label out of the format.dat 1435 */ 1436 pcyl = disk_type->dtype_pcyl; 1437 acyl = disk_type->dtype_acyl; 1438 ncyl = disk_type->dtype_ncyl; 1439 nhead = disk_type->dtype_nhead; 1440 nsect = disk_type->dtype_nsect; 1441 rpm = disk_type->dtype_rpm; 1442 1443 if (option_msg && diag_msg) { 1444 err_print("Format.dat geometry:\n"); 1445 err_print(" pcyl: %u\n", pcyl); 1446 err_print(" heads: %u\n", nhead); 1447 err_print(" nsects: %u\n", nsect); 1448 err_print(" acyl: %u\n", acyl); 1449 err_print(" rpm: %d\n", rpm); 1450 } 1451 1452 (void) memset((char *)label, 0, sizeof (struct dk_label)); 1453 1454 label->dkl_magic = DKL_MAGIC; 1455 1456 (void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel), 1457 "%s cyl %u alt %u hd %u sec %u", 1458 disk_type->dtype_asciilabel, 1459 ncyl, acyl, nhead, nsect); 1460 1461 label->dkl_pcyl = pcyl; 1462 label->dkl_ncyl = ncyl; 1463 label->dkl_acyl = acyl; 1464 label->dkl_nhead = nhead; 1465 label->dkl_nsect = nsect; 1466 label->dkl_apc = 0; 1467 label->dkl_intrlv = 1; 1468 label->dkl_rpm = rpm; 1469 1470 if (!build_default_partition(label, DKC_SCSI_CCS)) { 1471 goto err; 1472 } 1473 1474 (void) checksum(label, CK_MAKESUM); 1475 return (1); 1476 1477 err: 1478 if (option_msg && diag_msg) { 1479 err_print( 1480 "Configuration via format.dat geometry failed\n"); 1481 } 1482 return (0); 1483 } 1484 1485 int 1486 build_default_partition( 1487 struct dk_label *label, 1488 int ctrl_type) 1489 { 1490 int i; 1491 int ncyls[NDKMAP]; 1492 diskaddr_t nblks; 1493 int cyl; 1494 struct dk_vtoc *vtoc; 1495 struct part_table *pt; 1496 struct default_partitions *dpt; 1497 diskaddr_t capacity; 1498 int freecyls; 1499 int blks_per_cyl; 1500 int ncyl; 1501 1502 #ifdef lint 1503 ctrl_type = ctrl_type; 1504 #endif 1505 1506 /* 1507 * Install a default vtoc 1508 */ 1509 vtoc = &label->dkl_vtoc; 1510 vtoc->v_version = V_VERSION; 1511 vtoc->v_nparts = NDKMAP; 1512 vtoc->v_sanity = VTOC_SANE; 1513 1514 for (i = 0; i < NDKMAP; i++) { 1515 vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag; 1516 vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag; 1517 } 1518 1519 /* 1520 * Find a partition that matches this disk. Capacity 1521 * is in integral number of megabytes. 1522 */ 1523 capacity = ((diskaddr_t)(label->dkl_ncyl) * label->dkl_nhead * 1524 label->dkl_nsect) / (1024 * 1024) / DEV_BSIZE; 1525 dpt = default_partitions; 1526 for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) { 1527 if (capacity >= dpt->min_capacity && 1528 capacity < dpt->max_capacity) { 1529 break; 1530 } 1531 } 1532 if (i == DEFAULT_PARTITION_TABLE_SIZE) { 1533 if (option_msg && diag_msg) { 1534 err_print("No matching default partition (%llu)\n", 1535 capacity); 1536 } 1537 return (0); 1538 } 1539 pt = dpt->part_table; 1540 1541 /* 1542 * Go through default partition table, finding fixed 1543 * sized entries. 1544 */ 1545 freecyls = label->dkl_ncyl; 1546 blks_per_cyl = label->dkl_nhead * label->dkl_nsect; 1547 for (i = 0; i < NDKMAP; i++) { 1548 if (pt->partitions[i] == HOG || pt->partitions[i] == 0) { 1549 ncyls[i] = 0; 1550 } else { 1551 /* 1552 * Calculate number of cylinders necessary 1553 * for specified size, rounding up to 1554 * the next greatest integral number of 1555 * cylinders. Always give what they 1556 * asked or more, never less. 1557 */ 1558 nblks = pt->partitions[i] * ((1024*1024)/DEV_BSIZE); 1559 nblks += (blks_per_cyl - 1); 1560 ncyls[i] = nblks / blks_per_cyl; 1561 freecyls -= ncyls[i]; 1562 } 1563 } 1564 1565 if (freecyls < 0) { 1566 if (option_msg && diag_msg) { 1567 for (i = 0; i < NDKMAP; i++) { 1568 if (ncyls[i] == 0) 1569 continue; 1570 err_print("Partition %d: %u cyls\n", 1571 i, ncyls[i]); 1572 } 1573 err_print("Free cylinders exhausted (%d)\n", 1574 freecyls); 1575 } 1576 return (0); 1577 } 1578 #if defined(i386) 1579 /* 1580 * Set the default boot partition to 1 cylinder 1581 */ 1582 ncyls[8] = 1; 1583 freecyls -= 1; 1584 1585 /* 1586 * If current disk type is not a SCSI disk, 1587 * set the default alternates partition to 2 cylinders 1588 */ 1589 if (ctrl_type != DKC_SCSI_CCS) { 1590 ncyls[9] = 2; 1591 freecyls -= 2; 1592 } 1593 #endif /* defined(i386) */ 1594 1595 /* 1596 * Set the free hog partition to whatever space remains. 1597 * It's an error to have more than one HOG partition, 1598 * but we don't verify that here. 1599 */ 1600 for (i = 0; i < NDKMAP; i++) { 1601 if (pt->partitions[i] == HOG) { 1602 assert(ncyls[i] == 0); 1603 ncyls[i] = freecyls; 1604 break; 1605 } 1606 } 1607 1608 /* 1609 * Error checking 1610 */ 1611 ncyl = 0; 1612 for (i = 0; i < NDKMAP; i++) { 1613 ncyl += ncyls[i]; 1614 } 1615 assert(ncyl == (label->dkl_ncyl)); 1616 1617 /* 1618 * Finally, install the partition in the label. 1619 */ 1620 cyl = 0; 1621 1622 #if defined(_SUNOS_VTOC_16) 1623 for (i = NDKMAP/2; i < NDKMAP; i++) { 1624 if (i == 2 || ncyls[i] == 0) 1625 continue; 1626 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl; 1627 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl; 1628 cyl += ncyls[i]; 1629 } 1630 for (i = 0; i < NDKMAP/2; i++) { 1631 1632 #elif defined(_SUNOS_VTOC_8) 1633 for (i = 0; i < NDKMAP; i++) { 1634 1635 #else 1636 #error No VTOC format defined. 1637 #endif /* defined(_SUNOS_VTOC_16) */ 1638 1639 if (i == 2 || ncyls[i] == 0) { 1640 #if defined(_SUNOS_VTOC_8) 1641 if (i != 2) { 1642 label->dkl_map[i].dkl_cylno = 0; 1643 label->dkl_map[i].dkl_nblk = 0; 1644 } 1645 #endif 1646 continue; 1647 } 1648 #if defined(_SUNOS_VTOC_8) 1649 label->dkl_map[i].dkl_cylno = cyl; 1650 label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl; 1651 #elif defined(_SUNOS_VTOC_16) 1652 label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl; 1653 label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl; 1654 1655 #else 1656 #error No VTOC format defined. 1657 #endif /* defined(_SUNOS_VTOC_8) */ 1658 1659 cyl += ncyls[i]; 1660 } 1661 1662 /* 1663 * Set the whole disk partition 1664 */ 1665 #if defined(_SUNOS_VTOC_8) 1666 label->dkl_map[2].dkl_cylno = 0; 1667 label->dkl_map[2].dkl_nblk = 1668 label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect; 1669 1670 #elif defined(_SUNOS_VTOC_16) 1671 label->dkl_vtoc.v_part[2].p_start = 0; 1672 label->dkl_vtoc.v_part[2].p_size = 1673 (label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead * 1674 label->dkl_nsect; 1675 #else 1676 #error No VTOC format defined. 1677 #endif /* defined(_SUNOS_VTOC_8) */ 1678 1679 1680 if (option_msg && diag_msg) { 1681 float scaled; 1682 err_print("\n"); 1683 for (i = 0; i < NDKMAP; i++) { 1684 #if defined(_SUNOS_VTOC_8) 1685 if (label->dkl_map[i].dkl_nblk == 0) 1686 1687 #elif defined(_SUNOS_VTOC_16) 1688 if (label->dkl_vtoc.v_part[i].p_size == 0) 1689 1690 #else 1691 #error No VTOC format defined. 1692 #endif /* defined(_SUNOS_VTOC_8) */ 1693 1694 continue; 1695 err_print("Partition %d: ", i); 1696 #if defined(_SUNOS_VTOC_8) 1697 scaled = bn2mb(label->dkl_map[i].dkl_nblk); 1698 1699 #elif defined(_SUNOS_VTOC_16) 1700 1701 scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size); 1702 #else 1703 #error No VTOC format defined. 1704 #endif /* defined(_SUNOS_VTOC_8) */ 1705 1706 if (scaled > 1024.0) { 1707 err_print("%6.2fGB ", scaled/1024.0); 1708 } else { 1709 err_print("%6.2fMB ", scaled); 1710 } 1711 err_print(" %6d cylinders\n", 1712 #if defined(_SUNOS_VTOC_8) 1713 label->dkl_map[i].dkl_nblk/blks_per_cyl); 1714 1715 #elif defined(_SUNOS_VTOC_16) 1716 label->dkl_vtoc.v_part[i].p_size/blks_per_cyl); 1717 1718 #else 1719 #error No VTOC format defined. 1720 #endif /* defined(_SUNOS_VTOC_8) */ 1721 1722 } 1723 err_print("\n"); 1724 } 1725 1726 return (1); 1727 } 1728 1729 1730 1731 /* 1732 * Find an existing scsi disk definition by this name, 1733 * if possible. 1734 */ 1735 static struct disk_type * 1736 find_scsi_disk_type( 1737 char *disk_name, 1738 struct dk_label *label) 1739 { 1740 struct ctlr_type *ctlr; 1741 struct disk_type *dp; 1742 1743 ctlr = find_scsi_ctlr_type(); 1744 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) { 1745 if (dp->dtype_asciilabel) { 1746 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) && 1747 dp->dtype_pcyl == label->dkl_pcyl && 1748 dp->dtype_ncyl == label->dkl_ncyl && 1749 dp->dtype_acyl == label->dkl_acyl && 1750 dp->dtype_nhead == label->dkl_nhead && 1751 dp->dtype_nsect == label->dkl_nsect) { 1752 return (dp); 1753 } 1754 } 1755 } 1756 1757 return ((struct disk_type *)NULL); 1758 } 1759 1760 1761 /* 1762 * Find an existing scsi disk definition by this name, 1763 * if possible. 1764 */ 1765 static struct disk_type * 1766 find_scsi_disk_by_name( 1767 char *disk_name) 1768 { 1769 struct ctlr_type *ctlr; 1770 struct disk_type *dp; 1771 1772 ctlr = find_scsi_ctlr_type(); 1773 for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) { 1774 if (dp->dtype_asciilabel) { 1775 if ((strcmp(dp->dtype_asciilabel, disk_name) == 0)) { 1776 return (dp); 1777 } 1778 } 1779 } 1780 1781 return ((struct disk_type *)NULL); 1782 } 1783 1784 1785 /* 1786 * Return a pointer to the ctlr_type structure for SCSI 1787 * disks. This list is built into the program, so there's 1788 * no chance of not being able to find it, unless someone 1789 * totally mangles the code. 1790 */ 1791 static struct ctlr_type * 1792 find_scsi_ctlr_type() 1793 { 1794 struct mctlr_list *mlp; 1795 1796 mlp = controlp; 1797 1798 while (mlp != NULL) { 1799 if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS || 1800 mlp->ctlr_type->ctype_ctype == DKC_VBD) { 1801 return (mlp->ctlr_type); 1802 } 1803 mlp = mlp->next; 1804 } 1805 1806 impossible("no SCSI controller type"); 1807 1808 return ((struct ctlr_type *)NULL); 1809 } 1810 1811 1812 1813 /* 1814 * Return a pointer to the scsi ctlr_info structure. This 1815 * structure is allocated the first time format sees a 1816 * disk on this controller, so it must be present. 1817 */ 1818 static struct ctlr_info * 1819 find_scsi_ctlr_info( 1820 struct dk_cinfo *dkinfo) 1821 { 1822 struct ctlr_info *ctlr; 1823 1824 if (dkinfo->dki_ctype != DKC_SCSI_CCS && 1825 dkinfo->dki_ctype != DKC_VBD) { 1826 return (NULL); 1827 } 1828 1829 for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) { 1830 if (ctlr->ctlr_addr == dkinfo->dki_addr && 1831 ctlr->ctlr_space == dkinfo->dki_space && 1832 (ctlr->ctlr_ctype->ctype_ctype == DKC_SCSI_CCS || 1833 ctlr->ctlr_ctype->ctype_ctype == DKC_VBD)) { 1834 return (ctlr); 1835 } 1836 } 1837 1838 impossible("no SCSI controller info"); 1839 1840 return ((struct ctlr_info *)NULL); 1841 } 1842 1843 1844 1845 static struct disk_type * 1846 new_scsi_disk_type( 1847 int fd, 1848 char *disk_name, 1849 struct dk_label *label) 1850 { 1851 struct disk_type *dp; 1852 struct disk_type *disk; 1853 struct ctlr_info *ctlr; 1854 struct dk_cinfo dkinfo; 1855 struct partition_info *part; 1856 struct partition_info *pt; 1857 struct disk_info *disk_info; 1858 int i; 1859 1860 /* 1861 * Get the disk controller info for this disk 1862 */ 1863 if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) { 1864 if (option_msg && diag_msg) { 1865 err_print("DKIOCINFO failed\n"); 1866 } 1867 return (NULL); 1868 } 1869 1870 /* 1871 * Find the ctlr_info for this disk. 1872 */ 1873 ctlr = find_scsi_ctlr_info(&dkinfo); 1874 1875 /* 1876 * Allocate a new disk type for the SCSI controller. 1877 */ 1878 disk = (struct disk_type *)zalloc(sizeof (struct disk_type)); 1879 1880 /* 1881 * Find the disk_info instance for this disk. 1882 */ 1883 disk_info = find_scsi_disk_info(&dkinfo); 1884 1885 /* 1886 * The controller and the disk should match. 1887 */ 1888 assert(disk_info->disk_ctlr == ctlr); 1889 1890 /* 1891 * Link the disk into the list of disks 1892 */ 1893 dp = ctlr->ctlr_ctype->ctype_dlist; 1894 if (dp == NULL) { 1895 ctlr->ctlr_ctype->ctype_dlist = disk; 1896 } else { 1897 while (dp->dtype_next != NULL) { 1898 dp = dp->dtype_next; 1899 } 1900 dp->dtype_next = disk; 1901 } 1902 disk->dtype_next = NULL; 1903 1904 /* 1905 * Allocate and initialize the disk name. 1906 */ 1907 disk->dtype_asciilabel = alloc_string(disk_name); 1908 1909 /* 1910 * Initialize disk geometry info 1911 */ 1912 disk->dtype_pcyl = label->dkl_pcyl; 1913 disk->dtype_ncyl = label->dkl_ncyl; 1914 disk->dtype_acyl = label->dkl_acyl; 1915 disk->dtype_nhead = label->dkl_nhead; 1916 disk->dtype_nsect = label->dkl_nsect; 1917 disk->dtype_rpm = label->dkl_rpm; 1918 1919 /* 1920 * Attempt to match the partition map in the label 1921 * with a know partition for this disk type. 1922 */ 1923 for (part = disk->dtype_plist; part; part = part->pinfo_next) { 1924 if (parts_match(label, part)) { 1925 break; 1926 } 1927 } 1928 1929 /* 1930 * If no match was made, we need to create a partition 1931 * map for this disk. 1932 */ 1933 if (part == NULL) { 1934 part = (struct partition_info *) 1935 zalloc(sizeof (struct partition_info)); 1936 pt = disk->dtype_plist; 1937 if (pt == NULL) { 1938 disk->dtype_plist = part; 1939 } else { 1940 while (pt->pinfo_next != NULL) { 1941 pt = pt->pinfo_next; 1942 } 1943 pt->pinfo_next = part; 1944 } 1945 part->pinfo_next = NULL; 1946 1947 /* 1948 * Set up the partition name 1949 */ 1950 part->pinfo_name = alloc_string("default"); 1951 1952 /* 1953 * Fill in the partition info from the label 1954 */ 1955 for (i = 0; i < NDKMAP; i++) { 1956 1957 #if defined(_SUNOS_VTOC_8) 1958 part->pinfo_map[i] = label->dkl_map[i]; 1959 1960 #elif defined(_SUNOS_VTOC_16) 1961 part->pinfo_map[i].dkl_cylno = 1962 label->dkl_vtoc.v_part[i].p_start / 1963 ((blkaddr32_t)(disk->dtype_nhead * 1964 disk->dtype_nsect - apc)); 1965 part->pinfo_map[i].dkl_nblk = 1966 label->dkl_vtoc.v_part[i].p_size; 1967 #else 1968 #error No VTOC format defined. 1969 #endif /* defined(_SUNOS_VTOC_8) */ 1970 1971 } 1972 } 1973 1974 1975 /* 1976 * Use the VTOC if valid, or install a default 1977 */ 1978 if (label->dkl_vtoc.v_version == V_VERSION) { 1979 (void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume, 1980 LEN_DKL_VVOL); 1981 part->vtoc = label->dkl_vtoc; 1982 } else { 1983 (void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL); 1984 set_vtoc_defaults(part); 1985 } 1986 1987 /* 1988 * Link the disk to the partition map 1989 */ 1990 disk_info->disk_parts = part; 1991 1992 return (disk); 1993 } 1994 1995 1996 /* 1997 * Delete a disk type from disk type list. 1998 */ 1999 int 2000 delete_disk_type( 2001 struct disk_type *disk_type) 2002 { 2003 struct ctlr_type *ctlr; 2004 struct disk_type *dp, *disk; 2005 2006 if (cur_ctype->ctype_ctype == DKC_DIRECT) 2007 ctlr = find_direct_ctlr_type(); 2008 else 2009 ctlr = find_scsi_ctlr_type(); 2010 if (ctlr == NULL || ctlr->ctype_dlist == NULL) { 2011 return (-1); 2012 } 2013 2014 disk = ctlr->ctype_dlist; 2015 if (disk == disk_type) { 2016 ctlr->ctype_dlist = disk->dtype_next; 2017 if (cur_label == L_TYPE_EFI) 2018 free(disk->dtype_plist->etoc); 2019 free(disk->dtype_plist); 2020 free(disk); 2021 return (0); 2022 } else { 2023 for (dp = disk->dtype_next; dp != NULL; 2024 disk = disk->dtype_next, dp = dp->dtype_next) { 2025 if (dp == disk_type) { 2026 disk->dtype_next = dp->dtype_next; 2027 if (cur_label == L_TYPE_EFI) 2028 free(dp->dtype_plist->etoc); 2029 free(dp->dtype_plist); 2030 free(dp); 2031 return (0); 2032 } 2033 } 2034 return (-1); 2035 } 2036 } 2037 2038 2039 static struct disk_info * 2040 find_scsi_disk_info( 2041 struct dk_cinfo *dkinfo) 2042 { 2043 struct disk_info *disk; 2044 struct dk_cinfo *dp; 2045 2046 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 2047 assert(dkinfo->dki_ctype == DKC_SCSI_CCS || 2048 dkinfo->dki_ctype == DKC_VBD); 2049 dp = &disk->disk_dkinfo; 2050 if (dp->dki_ctype == dkinfo->dki_ctype && 2051 dp->dki_cnum == dkinfo->dki_cnum && 2052 dp->dki_unit == dkinfo->dki_unit && 2053 strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) { 2054 return (disk); 2055 } 2056 } 2057 2058 impossible("No SCSI disk info instance\n"); 2059 2060 return ((struct disk_info *)NULL); 2061 } 2062 2063 2064 static char * 2065 get_sun_disk_name( 2066 char *disk_name, 2067 struct scsi_inquiry *inquiry) 2068 { 2069 /* 2070 * Extract the sun name of the disk 2071 */ 2072 (void) memset(disk_name, 0, DISK_NAME_MAX); 2073 (void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7); 2074 2075 return (disk_name); 2076 } 2077 2078 2079 static char * 2080 get_generic_disk_name( 2081 char *disk_name, 2082 struct scsi_inquiry *inquiry) 2083 { 2084 char *p; 2085 2086 (void) memset(disk_name, 0, DISK_NAME_MAX); 2087 p = strcopy(disk_name, inquiry->inq_vid, 2088 sizeof (inquiry->inq_vid)); 2089 *p++ = '-'; 2090 p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid)); 2091 *p++ = '-'; 2092 p = strcopy(p, inquiry->inq_revision, 2093 sizeof (inquiry->inq_revision)); 2094 2095 return (disk_name); 2096 } 2097 2098 2099 2100 static int 2101 force_blocksize( 2102 int fd) 2103 { 2104 union { 2105 struct mode_format page3; 2106 uchar_t buf3[MAX_MODE_SENSE_SIZE]; 2107 } u_page3; 2108 struct mode_format *page3 = &u_page3.page3; 2109 struct scsi_ms_header header; 2110 2111 if (check("\ 2112 Must reformat device to 512-byte blocksize. Continue") == 0) { 2113 2114 /* 2115 * Get current Page 3 - Format Parameters page 2116 */ 2117 if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, 2118 MODE_SENSE_PC_CURRENT, (caddr_t)&u_page3, 2119 MAX_MODE_SENSE_SIZE, &header)) { 2120 goto err; 2121 } 2122 2123 /* 2124 * Make our changes to the geometry 2125 */ 2126 header.mode_header.length = 0; 2127 header.mode_header.device_specific = 0; 2128 page3->mode_page.ps = 0; 2129 page3->data_bytes_sect = DEV_BSIZE; 2130 2131 /* 2132 * make sure that logical block size is of 2133 * DEV_BSIZE. 2134 */ 2135 header.block_descriptor.blksize_hi = (DEV_BSIZE >> 16); 2136 header.block_descriptor.blksize_mid = (DEV_BSIZE >> 8); 2137 header.block_descriptor.blksize_lo = (char)(DEV_BSIZE); 2138 /* 2139 * Select current Page 3 - Format Parameters page 2140 */ 2141 if (uscsi_mode_select(fd, DAD_MODE_FORMAT, 2142 MODE_SELECT_PF, (caddr_t)&u_page3, 2143 MODESENSE_PAGE_LEN(&u_page3), &header)) { 2144 goto err; 2145 } 2146 2147 /* 2148 * Now reformat the device 2149 */ 2150 if (raw_format(fd)) { 2151 goto err; 2152 } 2153 return (0); 2154 } 2155 2156 err: 2157 if (option_msg && diag_msg) { 2158 err_print( 2159 "Reformat device to 512-byte blocksize failed\n"); 2160 } 2161 return (1); 2162 } 2163 2164 static int 2165 raw_format( 2166 int fd) 2167 { 2168 union scsi_cdb cdb; 2169 struct uscsi_cmd ucmd; 2170 struct scsi_defect_hdr defect_hdr; 2171 2172 (void) memset((char *)&ucmd, 0, sizeof (ucmd)); 2173 (void) memset((char *)&cdb, 0, sizeof (union scsi_cdb)); 2174 (void) memset((char *)&defect_hdr, 0, sizeof (defect_hdr)); 2175 cdb.scc_cmd = SCMD_FORMAT; 2176 ucmd.uscsi_cdb = (caddr_t)&cdb; 2177 ucmd.uscsi_cdblen = CDB_GROUP0; 2178 ucmd.uscsi_bufaddr = (caddr_t)&defect_hdr; 2179 ucmd.uscsi_buflen = sizeof (defect_hdr); 2180 cdb.cdb_opaque[1] = FPB_DATA; 2181 2182 /* 2183 * Issue the format ioctl 2184 */ 2185 fmt_print("Formatting...\n"); 2186 (void) fflush(stdout); 2187 if (uscsi_cmd(fd, &ucmd, 2188 (option_msg && diag_msg) ? F_NORMAL : F_SILENT)) { 2189 return (1); 2190 } 2191 return (0); 2192 } 2193 2194 /* 2195 * Copy a string of characters from src to dst, for at 2196 * most n bytes. Strip all leading and trailing spaces, 2197 * and stop if there are any non-printable characters. 2198 * Return ptr to the next character to be filled. 2199 */ 2200 static char * 2201 strcopy( 2202 char *dst, 2203 char *src, 2204 int n) 2205 { 2206 int i; 2207 2208 while (*src == ' ' && n > 0) { 2209 src++; 2210 n--; 2211 } 2212 2213 for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) { 2214 if (*src == ' ') { 2215 i++; 2216 } else { 2217 while (i-- > 0) 2218 *dst++ = ' '; 2219 *dst++ = *src; 2220 } 2221 } 2222 2223 *dst = 0; 2224 return (dst); 2225 } 2226 2227 /* 2228 * adjust disk geometry. 2229 * This is used when disk reports a disk geometry page having 2230 * no of physical cylinders is < 3 which is the minimum required 2231 * by Solaris (2 for storing labels and at least one as a data 2232 * cylinder ) 2233 */ 2234 int 2235 adjust_disk_geometry(diskaddr_t capacity, uint_t *cyl, uint_t *nhead, 2236 uint_t *nsect) 2237 { 2238 uint_t lcyl = *cyl; 2239 uint_t lnhead = *nhead; 2240 uint_t lnsect = *nsect; 2241 2242 assert(lcyl < SUN_MIN_CYL); 2243 2244 /* 2245 * reduce nsect by 2 for each iteration and re-calculate 2246 * the number of cylinders. 2247 */ 2248 while (lnsect > MINIMUM_NO_SECTORS && 2249 lcyl < MINIMUM_NO_CYLINDERS) { 2250 /* 2251 * make sure that we do not go below MINIMUM_NO_SECTORS. 2252 */ 2253 lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2); 2254 lcyl = (capacity) / (lnhead * lnsect); 2255 } 2256 /* 2257 * If the geometry still does not satisfy 2258 * MINIMUM_NO_CYLINDERS then try to reduce the 2259 * no of heads. 2260 */ 2261 while (lnhead > MINIMUM_NO_HEADS && 2262 lcyl < MINIMUM_NO_CYLINDERS) { 2263 lnhead = max(MINIMUM_NO_HEADS, lnhead / 2); 2264 lcyl = (capacity) / (lnhead * lnsect); 2265 } 2266 /* 2267 * now we should have atleast SUN_MIN_CYL cylinders. 2268 * If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS 2269 * and MINIMUM_NO_HEADS then return error. 2270 */ 2271 if (lcyl < SUN_MIN_CYL) 2272 return (1); 2273 else { 2274 *cyl = lcyl; 2275 *nhead = lnhead; 2276 *nsect = lnsect; 2277 return (0); 2278 } 2279 } 2280 2281 #if defined(_SUNOS_VTOC_8) 2282 /* 2283 * Reduce the size of one dimention below a specified 2284 * limit with a minimum loss of volume. Dimenstions are 2285 * assumed to be passed in form the largest value (the one 2286 * that needs to be reduced) to the smallest value. The 2287 * values will be twiddled until they are all less than or 2288 * equal to their limit. Returns the number in the new geometry. 2289 */ 2290 static diskaddr_t 2291 square_box( 2292 diskaddr_t capacity, 2293 uint_t *dim1, uint_t lim1, 2294 uint_t *dim2, uint_t lim2, 2295 uint_t *dim3, uint_t lim3) 2296 { 2297 uint_t i; 2298 2299 /* 2300 * Although the routine should work with any ordering of 2301 * parameters, it's most efficient if they are passed in 2302 * in decreasing magnitude. 2303 */ 2304 assert(*dim1 >= *dim2); 2305 assert(*dim2 >= *dim3); 2306 2307 /* 2308 * This is done in a very arbitrary manner. We could try to 2309 * find better values but I can't come up with a method that 2310 * would run in a reasonable amount of time. That could take 2311 * approximately 65535 * 65535 iterations of a dozen flops each 2312 * or well over 4G flops. 2313 * 2314 * First: 2315 * 2316 * Let's see how far we can go with bitshifts w/o losing 2317 * any blocks. 2318 */ 2319 2320 for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++); 2321 if (i) { 2322 *dim1 = ((*dim1)>>i); 2323 *dim3 = ((*dim3)<<i); 2324 } 2325 2326 if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) { 2327 double d[4]; 2328 2329 /* 2330 * Second: 2331 * 2332 * Set the highest value at its limit then calculate errors, 2333 * adjusting the 2nd highest value (we get better resolution 2334 * that way). 2335 */ 2336 d[1] = lim1; 2337 d[3] = *dim3; 2338 d[2] = (double)capacity/(d[1]*d[3]); 2339 2340 /* 2341 * If we overflowed the middle term, set it to its limit and 2342 * chose a new low term. 2343 */ 2344 if (d[2] > lim2) { 2345 d[2] = lim2; 2346 d[3] = (double)capacity/(d[1]*d[2]); 2347 } 2348 /* 2349 * Convert to integers. 2350 */ 2351 *dim1 = (int)d[1]; 2352 *dim2 = (int)d[2]; 2353 *dim3 = (int)d[3]; 2354 } 2355 /* 2356 * Fixup any other possible problems. 2357 * If this happens, we need a new disklabel format. 2358 */ 2359 if (*dim1 > lim1) *dim1 = lim1; 2360 if (*dim2 > lim2) *dim2 = lim2; 2361 if (*dim3 > lim3) *dim3 = lim3; 2362 return (*dim1 * *dim2 * *dim3); 2363 } 2364 #endif /* defined(_SUNOS_VTOC_8) */ 2365 2366 /* 2367 * Calculate CHS values based on the capacity data. 2368 * 2369 * NOTE: This function is same as cmlb_convert_geomerty() function in 2370 * cmlb kernel module. 2371 */ 2372 static void 2373 compute_chs_values(diskaddr_t total_capacity, diskaddr_t usable_capacity, 2374 uint_t *pcylp, uint_t *nheadp, uint_t *nsectp) 2375 { 2376 2377 /* Unlabeled SCSI floppy device */ 2378 if (total_capacity <= 0x1000) { 2379 *nheadp = 2; 2380 *pcylp = 80; 2381 *nsectp = total_capacity / (80 * 2); 2382 return; 2383 } 2384 2385 /* 2386 * For all devices we calculate cylinders using the heads and sectors 2387 * we assign based on capacity of the device. The algorithm is 2388 * designed to be compatible with the way other operating systems 2389 * lay out fdisk tables for X86 and to insure that the cylinders never 2390 * exceed 65535 to prevent problems with X86 ioctls that report 2391 * geometry. 2392 * For some smaller disk sizes we report geometry that matches those 2393 * used by X86 BIOS usage. For larger disks, we use SPT that are 2394 * multiples of 63, since other OSes that are not limited to 16-bits 2395 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT. 2396 * 2397 * The following table (in order) illustrates some end result 2398 * calculations: 2399 * 2400 * Maximum number of blocks nhead nsect 2401 * 2402 * 2097152 (1GB) 64 32 2403 * 16777216 (8GB) 128 32 2404 * 1052819775 (502.02GB) 255 63 2405 * 2105639550 (0.98TB) 255 126 2406 * 3158459325 (1.47TB) 255 189 2407 * 4211279100 (1.96TB) 255 252 2408 * 5264098875 (2.45TB) 255 315 2409 * ... 2410 */ 2411 2412 if (total_capacity <= 0x200000) { 2413 *nheadp = 64; 2414 *nsectp = 32; 2415 } else if (total_capacity <= 0x01000000) { 2416 *nheadp = 128; 2417 *nsectp = 32; 2418 } else { 2419 *nheadp = 255; 2420 2421 /* make nsect be smallest multiple of 63 */ 2422 *nsectp = ((total_capacity + 2423 (UINT16_MAX * 255 * 63) - 1) / 2424 (UINT16_MAX * 255 * 63)) * 63; 2425 2426 if (*nsectp == 0) 2427 *nsectp = (UINT16_MAX / 63) * 63; 2428 } 2429 2430 if (usable_capacity < total_capacity) 2431 *pcylp = usable_capacity / ((*nheadp) * (*nsectp)); 2432 else 2433 *pcylp = total_capacity / ((*nheadp) * (*nsectp)); 2434 } 2435