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