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