xref: /titanic_52/usr/src/lib/libdiskmgt/common/findevs.c (revision 0167b58cea98965c58fab4be4e690b6e456f7440)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0167b58cScg149915  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <libgen.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <devid.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
437c478bd9Sstevel@tonic-gate #include "disks_private.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	CLUSTER_DEV	"did"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* specify which disk links to use in the /dev directory */
487c478bd9Sstevel@tonic-gate #define	DEVLINK_REGEX		"rdsk/.*"
497c478bd9Sstevel@tonic-gate #define	DEVLINK_FLOPPY_REGEX	"rdiskette[0-9]"
507c478bd9Sstevel@tonic-gate #define	DEVLINK_DID_REGEX	"did/rdsk/.*"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	FLOPPY_NAME	"rdiskette"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	MAXPROPLEN		1024
557c478bd9Sstevel@tonic-gate #define	DEVICE_ID_PROP		"devid"
567c478bd9Sstevel@tonic-gate #define	PROD_ID_PROP		"inquiry-product-id"
577c478bd9Sstevel@tonic-gate #define	PROD_ID_USB_PROP	"usb-product-name"
587c478bd9Sstevel@tonic-gate #define	REMOVABLE_PROP		"removable-media"
59*0167b58cScg149915 #define	HOTPLUGGABLE_PROP	"hotpluggable"
607c478bd9Sstevel@tonic-gate #define	SCSI_OPTIONS_PROP	"scsi-options"
617c478bd9Sstevel@tonic-gate #define	VENDOR_ID_PROP		"inquiry-vendor-id"
627c478bd9Sstevel@tonic-gate #define	VENDOR_ID_USB_PROP	"usb-vendor-name"
637c478bd9Sstevel@tonic-gate #define	WWN_PROP		"node-wwn"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* The list of names of possible disk types used by libdevinfo. */
667c478bd9Sstevel@tonic-gate static char *disktypes[] = {
677c478bd9Sstevel@tonic-gate 	DDI_NT_BLOCK,
687c478bd9Sstevel@tonic-gate 	DDI_NT_BLOCK_CHAN,
697c478bd9Sstevel@tonic-gate 	DDI_NT_BLOCK_WWN,
707c478bd9Sstevel@tonic-gate 	DDI_NT_BLOCK_FABRIC,
717c478bd9Sstevel@tonic-gate 	DDI_NT_CD_CHAN,
727c478bd9Sstevel@tonic-gate 	DDI_NT_CD,
737c478bd9Sstevel@tonic-gate 	DDI_NT_FD,
747c478bd9Sstevel@tonic-gate 	NULL
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Most of the removable media will be lumped under here; CD, DVD, MO, etc.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static char *cdromtypes[] = {
817c478bd9Sstevel@tonic-gate 	DDI_NT_CD_CHAN,
827c478bd9Sstevel@tonic-gate 	DDI_NT_CD,
837c478bd9Sstevel@tonic-gate 	NULL
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static char *ctrltypes[] = {
877c478bd9Sstevel@tonic-gate 	DDI_NT_SCSI_NEXUS,
887c478bd9Sstevel@tonic-gate 	DDI_NT_SCSI_ATTACHMENT_POINT,
897c478bd9Sstevel@tonic-gate 	DDI_NT_FC_ATTACHMENT_POINT,
907c478bd9Sstevel@tonic-gate 	NULL
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static char *bustypes[] = {
947c478bd9Sstevel@tonic-gate 	"sbus",
957c478bd9Sstevel@tonic-gate 	"pci",
967c478bd9Sstevel@tonic-gate 	"usb",
977c478bd9Sstevel@tonic-gate 	NULL
987c478bd9Sstevel@tonic-gate };
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static bus_t		*add_bus(struct search_args *args, di_node_t node,
1017c478bd9Sstevel@tonic-gate 			    di_minor_t minor, controller_t *cp);
1027c478bd9Sstevel@tonic-gate static int		add_cluster_devs(di_node_t node, di_minor_t minor,
1037c478bd9Sstevel@tonic-gate 			    void *arg);
1047c478bd9Sstevel@tonic-gate static controller_t	*add_controller(struct search_args *args,
1057c478bd9Sstevel@tonic-gate 			    di_node_t node, di_minor_t minor);
1067c478bd9Sstevel@tonic-gate static int		add_devpath(di_devlink_t devlink, void *arg);
1077c478bd9Sstevel@tonic-gate static int		add_devs(di_node_t node, di_minor_t minor, void *arg);
1087c478bd9Sstevel@tonic-gate static int		add_disk2controller(disk_t *diskp,
1097c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1107c478bd9Sstevel@tonic-gate static int		add_disk2path(disk_t *dp, path_t *pp,
1117c478bd9Sstevel@tonic-gate 			    di_path_state_t st, char *wwn);
1127c478bd9Sstevel@tonic-gate static int		add_int2array(int p, int **parray);
1137c478bd9Sstevel@tonic-gate static int		add_ptr2array(void *p, void ***parray);
1147c478bd9Sstevel@tonic-gate static char		*bus_type(di_node_t node, di_minor_t minor,
1157c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1167c478bd9Sstevel@tonic-gate static int		can_remove_controller(controller_t *cp,
1177c478bd9Sstevel@tonic-gate 			    controller_t *currp);
1187c478bd9Sstevel@tonic-gate static void		clean_paths(struct search_args *args);
1197c478bd9Sstevel@tonic-gate static disk_t		*create_disk(char *deviceid, char *kernel_name,
1207c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1217c478bd9Sstevel@tonic-gate static char		*ctype(di_node_t node, di_minor_t minor);
1227c478bd9Sstevel@tonic-gate static boolean_t	disk_is_cdrom(char *type);
1237c478bd9Sstevel@tonic-gate static alias_t		*find_alias(disk_t *diskp, char *kernel_name);
1247c478bd9Sstevel@tonic-gate static bus_t		*find_bus(struct search_args *args, char *name);
1257c478bd9Sstevel@tonic-gate static controller_t	*find_controller(struct search_args *args, char *name);
1267c478bd9Sstevel@tonic-gate static int		fix_cluster_devpath(di_devlink_t devlink, void *arg);
1277c478bd9Sstevel@tonic-gate static disk_t		*get_disk_by_deviceid(disk_t *listp, char *devid);
1287c478bd9Sstevel@tonic-gate static void		get_disk_name_from_path(char *path, char *name,
1297c478bd9Sstevel@tonic-gate 			    int size);
1307c478bd9Sstevel@tonic-gate static char		*get_byte_prop(char *prop_name, di_node_t node);
1317c478bd9Sstevel@tonic-gate static di_node_t	get_parent_bus(di_node_t node,
1327c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1337c478bd9Sstevel@tonic-gate static int		get_prom_int(char *prop_name, di_node_t node,
1347c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1357c478bd9Sstevel@tonic-gate static char		*get_prom_str(char *prop_name, di_node_t node,
1367c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1377c478bd9Sstevel@tonic-gate static int		get_prop(char *prop_name, di_node_t node);
1387c478bd9Sstevel@tonic-gate static char		*get_str_prop(char *prop_name, di_node_t node);
1397c478bd9Sstevel@tonic-gate static int		have_disk(struct search_args *args, char *devid,
1407c478bd9Sstevel@tonic-gate 			    char *kernel_name, disk_t **diskp);
1417c478bd9Sstevel@tonic-gate static int		is_cluster_disk(di_node_t node, di_minor_t minor);
1427c478bd9Sstevel@tonic-gate static int		is_ctds(char *name);
1437c478bd9Sstevel@tonic-gate static int		is_drive(di_minor_t minor);
1447c478bd9Sstevel@tonic-gate static int		is_HBA(di_node_t node, di_minor_t minor);
1457c478bd9Sstevel@tonic-gate static int		new_alias(disk_t *diskp, char *kernel_path,
1467c478bd9Sstevel@tonic-gate 			    char *devlink_path, struct search_args *args);
1477c478bd9Sstevel@tonic-gate static int		new_devpath(alias_t *ap, char *devpath);
1487c478bd9Sstevel@tonic-gate static path_t		*new_path(controller_t *cp, disk_t *diskp,
1497c478bd9Sstevel@tonic-gate 			    di_node_t node, di_path_state_t st, char *wwn);
1507c478bd9Sstevel@tonic-gate static void		remove_invalid_controller(char *name,
1517c478bd9Sstevel@tonic-gate 			    controller_t *currp, struct search_args *args);
1527c478bd9Sstevel@tonic-gate static char		*str_case_index(register char *s1, register char *s2);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * The functions in this file do a dev tree walk to build up a model of the
1567c478bd9Sstevel@tonic-gate  * disks, controllers and paths on the system.  This model is returned in the
1577c478bd9Sstevel@tonic-gate  * args->disk_listp and args->controller_listp members of the args param.
1587c478bd9Sstevel@tonic-gate  * There is no global data for this file so it is thread safe.  It is up to
1597c478bd9Sstevel@tonic-gate  * the caller to merge the resulting model with any existing model that is
1607c478bd9Sstevel@tonic-gate  * cached.  The caller must also free the memory for this model when it is
1617c478bd9Sstevel@tonic-gate  * no longer needed.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate void
1647c478bd9Sstevel@tonic-gate findevs(struct search_args *args)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	uint_t			flags;
1677c478bd9Sstevel@tonic-gate 	di_node_t		di_root;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	args->dev_walk_status = 0;
1707c478bd9Sstevel@tonic-gate 	args->disk_listp = NULL;
1717c478bd9Sstevel@tonic-gate 	args->controller_listp = NULL;
1727c478bd9Sstevel@tonic-gate 	args->bus_listp = NULL;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	args->handle = di_devlink_init(NULL, 0);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Have to make several passes at this with the new devfs caching.
1787c478bd9Sstevel@tonic-gate 	 * First, we find non-mpxio devices. Then we find mpxio/multipath
1797c478bd9Sstevel@tonic-gate 	 * devices. Finally, we get cluster devices.
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate 	flags = DINFOCACHE;
1827c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1837c478bd9Sstevel@tonic-gate 	args->ph = di_prom_init();
1847c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1857c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	flags = DINFOCPYALL | DINFOPATH;
1887c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1897c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1907c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* do another pass to clean up cluster devpaths */
1937c478bd9Sstevel@tonic-gate 	flags = DINFOCACHE;
1947c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1957c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, DDI_PSEUDO, 0, args, add_cluster_devs);
1967c478bd9Sstevel@tonic-gate 	if (args->ph != DI_PROM_HANDLE_NIL) {
1977c478bd9Sstevel@tonic-gate 	    (void) di_prom_fini(args->ph);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	di_fini(di_root);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	(void) di_devlink_fini(&(args->handle));
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	clean_paths(args);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Definitions of private functions
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate static bus_t *
2117c478bd9Sstevel@tonic-gate add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
2127c478bd9Sstevel@tonic-gate 	controller_t *cp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	char		*btype;
2157c478bd9Sstevel@tonic-gate 	char		*devpath;
2167c478bd9Sstevel@tonic-gate 	bus_t		*bp;
2177c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
2187c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
2217c478bd9Sstevel@tonic-gate 	    return (NULL);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if ((btype = bus_type(node, minor, args->ph)) == NULL) {
2257c478bd9Sstevel@tonic-gate 	    return (add_bus(args, di_parent_node(node),
2267c478bd9Sstevel@tonic-gate 		di_minor_next(di_parent_node(node), NULL), cp));
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if ((bp = find_bus(args, devpath)) != NULL) {
2327c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	    if (cp != NULL) {
2357c478bd9Sstevel@tonic-gate 		if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
2367c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
2377c478bd9Sstevel@tonic-gate 		    return (NULL);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	    }
2407c478bd9Sstevel@tonic-gate 	    return (bp);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* Special handling for root node. */
2447c478bd9Sstevel@tonic-gate 	if (strcmp(devpath, "/") == 0) {
2457c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
2467c478bd9Sstevel@tonic-gate 	    return (NULL);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (dm_debug) {
2507c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	bp = (bus_t *)calloc(1, sizeof (bus_t));
2547c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
2557c478bd9Sstevel@tonic-gate 	    return (NULL);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	bp->name = strdup(devpath);
2597c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
2607c478bd9Sstevel@tonic-gate 	if (bp->name == NULL) {
2617c478bd9Sstevel@tonic-gate 	    args->dev_walk_status = ENOMEM;
2627c478bd9Sstevel@tonic-gate 	    cache_free_bus(bp);
2637c478bd9Sstevel@tonic-gate 	    return (NULL);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	bp->btype = strdup(btype);
2677c478bd9Sstevel@tonic-gate 	if (bp->btype == NULL) {
2687c478bd9Sstevel@tonic-gate 	    args->dev_walk_status = ENOMEM;
2697c478bd9Sstevel@tonic-gate 	    cache_free_bus(bp);
2707c478bd9Sstevel@tonic-gate 	    return (NULL);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
2747c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
2777c478bd9Sstevel@tonic-gate 	    args->dev_walk_status = ENOMEM;
2787c478bd9Sstevel@tonic-gate 	    cache_free_bus(bp);
2797c478bd9Sstevel@tonic-gate 	    return (NULL);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* if parent node is a bus, get its name */
2837c478bd9Sstevel@tonic-gate 	if ((pnode = get_parent_bus(node, args)) != NULL) {
2847c478bd9Sstevel@tonic-gate 	    devpath = di_devfs_path(pnode);
2857c478bd9Sstevel@tonic-gate 	    bp->pname = strdup(devpath);
2867c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
2877c478bd9Sstevel@tonic-gate 	    if (bp->pname == NULL) {
2887c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
2897c478bd9Sstevel@tonic-gate 		cache_free_bus(bp);
2907c478bd9Sstevel@tonic-gate 		return (NULL);
2917c478bd9Sstevel@tonic-gate 	    }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	} else {
2947c478bd9Sstevel@tonic-gate 	    bp->pname = NULL;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	bp->freq = get_prom_int("clock-frequency", node, args->ph);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
3007c478bd9Sstevel@tonic-gate 	if (bp->controllers == NULL) {
3017c478bd9Sstevel@tonic-gate 	    args->dev_walk_status = ENOMEM;
3027c478bd9Sstevel@tonic-gate 	    cache_free_bus(bp);
3037c478bd9Sstevel@tonic-gate 	    return (NULL);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	bp->controllers[0] = NULL;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
3087c478bd9Sstevel@tonic-gate 	    if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
3097c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
3107c478bd9Sstevel@tonic-gate 		return (NULL);
3117c478bd9Sstevel@tonic-gate 	    }
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	bp->next = args->bus_listp;
3157c478bd9Sstevel@tonic-gate 	args->bus_listp = bp;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	return (bp);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static int
3217c478bd9Sstevel@tonic-gate add_cluster_devs(di_node_t node, di_minor_t minor, void *arg)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	struct search_args	*args;
3247c478bd9Sstevel@tonic-gate 	char			*devpath;
3257c478bd9Sstevel@tonic-gate 	char			slice_path[MAXPATHLEN];
3267c478bd9Sstevel@tonic-gate 	int			result = DI_WALK_CONTINUE;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (!is_cluster_disk(node, minor)) {
3297c478bd9Sstevel@tonic-gate 	    return (DI_WALK_CONTINUE);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
3357c478bd9Sstevel@tonic-gate 	    /* This is all just debugging code */
3367c478bd9Sstevel@tonic-gate 	    char	*devpath;
3377c478bd9Sstevel@tonic-gate 	    char	dev_name[MAXPATHLEN];
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	    devpath = di_devfs_path(node);
3407c478bd9Sstevel@tonic-gate 	    (void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
3417c478bd9Sstevel@tonic-gate 		di_minor_name(minor));
3427c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "INFO: cluster dev: %s\n", dev_name);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	args->node = node;
3487c478bd9Sstevel@tonic-gate 	args->minor = minor;
3497c478bd9Sstevel@tonic-gate 	args->dev_walk_status = 0;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * Fix the devpaths for the cluster drive.
3537c478bd9Sstevel@tonic-gate 	 *
3547c478bd9Sstevel@tonic-gate 	 * We will come through here once for each raw slice device name.
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
3577c478bd9Sstevel@tonic-gate 	(void) snprintf(slice_path, sizeof (slice_path), "%s:%s", devpath,
3587c478bd9Sstevel@tonic-gate 	    di_minor_name(minor));
3597c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* Walk the /dev tree to get the cluster devlinks. */
3627c478bd9Sstevel@tonic-gate 	(void) di_devlink_walk(args->handle, DEVLINK_DID_REGEX, slice_path,
3637c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, arg, fix_cluster_devpath);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (args->dev_walk_status != 0) {
3667c478bd9Sstevel@tonic-gate 	    result = DI_WALK_TERMINATE;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	return (result);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate static controller_t *
3737c478bd9Sstevel@tonic-gate add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	char		*devpath;
3767c478bd9Sstevel@tonic-gate 	controller_t	*cp;
3777c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
3787c478bd9Sstevel@tonic-gate 	char		*c_type = DM_CTYPE_UNKNOWN;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if ((cp = find_controller(args, devpath)) != NULL) {
3837c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
3847c478bd9Sstevel@tonic-gate 	    return (cp);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
3887c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
3897c478bd9Sstevel@tonic-gate 	    di_node_t pnode;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	    pnode = di_parent_node(node);
3927c478bd9Sstevel@tonic-gate 	    if (pnode != DI_NODE_NIL) {
3937c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
3947c478bd9Sstevel@tonic-gate 		devpath = di_devfs_path(pnode);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		if ((cp = find_controller(args, devpath)) != NULL) {
3977c478bd9Sstevel@tonic-gate 		    di_devfs_path_free((void *) devpath);
3987c478bd9Sstevel@tonic-gate 		    return (cp);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		/* not in the list, create it */
4027c478bd9Sstevel@tonic-gate 		node = pnode;
4037c478bd9Sstevel@tonic-gate 		c_type = DM_CTYPE_FIBRE;
4047c478bd9Sstevel@tonic-gate 	    }
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (dm_debug) {
4087c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	cp = (controller_t *)calloc(1, sizeof (controller_t));
4127c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
4137c478bd9Sstevel@tonic-gate 	    return (NULL);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	cp->name = strdup(devpath);
4177c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
4187c478bd9Sstevel@tonic-gate 	if (cp->name == NULL) {
4197c478bd9Sstevel@tonic-gate 	    cache_free_controller(cp);
4207c478bd9Sstevel@tonic-gate 	    return (NULL);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
4247c478bd9Sstevel@tonic-gate 	    c_type = ctype(node, minor);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 	cp->ctype = c_type;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
4297c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
4327c478bd9Sstevel@tonic-gate 	    cache_free_controller(cp);
4337c478bd9Sstevel@tonic-gate 	    return (NULL);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
4377c478bd9Sstevel@tonic-gate 	    cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
4417c478bd9Sstevel@tonic-gate 	    cp->multiplex = 1;
4427c478bd9Sstevel@tonic-gate 	} else {
4437c478bd9Sstevel@tonic-gate 	    cp->multiplex = 0;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	cp->freq = get_prom_int("clock-frequency", node, args->ph);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
4497c478bd9Sstevel@tonic-gate 	if (cp->disks == NULL) {
4507c478bd9Sstevel@tonic-gate 	    cache_free_controller(cp);
4517c478bd9Sstevel@tonic-gate 	    return (NULL);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 	cp->disks[0] = NULL;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	cp->next = args->controller_listp;
4567c478bd9Sstevel@tonic-gate 	args->controller_listp = cp;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	cp->bus = add_bus(args, di_parent_node(node),
4597c478bd9Sstevel@tonic-gate 	    di_minor_next(di_parent_node(node), NULL), cp);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	return (cp);
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate static int
4657c478bd9Sstevel@tonic-gate add_devpath(di_devlink_t devlink, void *arg)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	struct search_args *args;
4687c478bd9Sstevel@tonic-gate 	char		*devidstr;
4697c478bd9Sstevel@tonic-gate 	disk_t		*diskp;
4707c478bd9Sstevel@tonic-gate 	char		kernel_name[MAXPATHLEN];
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	/*
4757c478bd9Sstevel@tonic-gate 	 * Get the diskp value from calling have_disk. Can either be found
4767c478bd9Sstevel@tonic-gate 	 * by kernel name or devid.
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	diskp = NULL;
4807c478bd9Sstevel@tonic-gate 	devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
4817c478bd9Sstevel@tonic-gate 	(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
4827c478bd9Sstevel@tonic-gate 	    di_node_name(args->node), di_instance(args->node));
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	(void) have_disk(args, devidstr, kernel_name, &diskp);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
4887c478bd9Sstevel@tonic-gate 	 * For diskettes it is /dev/rdiskette*.
4897c478bd9Sstevel@tonic-gate 	 * On Intel we would also get each fdisk partition as well
4907c478bd9Sstevel@tonic-gate 	 * (e.g. /dev/rdsk/c0t0d0p0).
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
4937c478bd9Sstevel@tonic-gate 	    alias_t	*ap;
4947c478bd9Sstevel@tonic-gate 	    char	*devlink_path;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	    if (diskp->drv_type != DM_DT_FLOPPY) {
4977c478bd9Sstevel@tonic-gate 		/*
4987c478bd9Sstevel@tonic-gate 		 * Add other controllers for multipath disks.  This will have
4997c478bd9Sstevel@tonic-gate 		 * no effect if the controller relationship is already set up.
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 		if (add_disk2controller(diskp, args) != 0) {
5027c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 	    }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	    (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
5077c478bd9Sstevel@tonic-gate 		di_node_name(args->node), di_instance(args->node));
5087c478bd9Sstevel@tonic-gate 	    devlink_path = (char *)di_devlink_path(devlink);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	    if (dm_debug > 1) {
5117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO:     devpath %s\n", devlink_path);
5127c478bd9Sstevel@tonic-gate 	    }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	    if ((ap = find_alias(diskp, kernel_name)) == NULL) {
5157c478bd9Sstevel@tonic-gate 		if (new_alias(diskp, kernel_name, devlink_path, args) != 0) {
5167c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 	    } else {
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * It is possible that we have already added this devpath.
5217c478bd9Sstevel@tonic-gate 		 * Do not add it again. new_devpath will return a 0 if
5227c478bd9Sstevel@tonic-gate 		 * found, and not add the path.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		if (new_devpath(ap, devlink_path) != 0) {
5257c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	    }
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate static int
5347c478bd9Sstevel@tonic-gate add_devs(di_node_t node, di_minor_t minor, void *arg)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	struct search_args	*args;
5377c478bd9Sstevel@tonic-gate 	int result = DI_WALK_CONTINUE;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
5427c478bd9Sstevel@tonic-gate 	    /* This is all just debugging code */
5437c478bd9Sstevel@tonic-gate 	    char	*devpath;
5447c478bd9Sstevel@tonic-gate 	    char	dev_name[MAXPATHLEN];
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	    devpath = di_devfs_path(node);
5477c478bd9Sstevel@tonic-gate 	    (void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
5487c478bd9Sstevel@tonic-gate 		di_minor_name(minor));
5497c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr,
5527c478bd9Sstevel@tonic-gate 		"INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
5537c478bd9Sstevel@tonic-gate 		dev_name,
5547c478bd9Sstevel@tonic-gate 		di_node_name(node), di_instance(node),
5557c478bd9Sstevel@tonic-gate 		di_minor_spectype(minor),
5567c478bd9Sstevel@tonic-gate 		(di_minor_nodetype(minor) != NULL ?
5577c478bd9Sstevel@tonic-gate 		    di_minor_nodetype(minor) : "NULL"));
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (bus_type(node, minor, args->ph) != NULL) {
5617c478bd9Sstevel@tonic-gate 	    if (add_bus(args, node, minor, NULL) == NULL) {
5627c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
5637c478bd9Sstevel@tonic-gate 		result = DI_WALK_TERMINATE;
5647c478bd9Sstevel@tonic-gate 	    }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	} else if (is_HBA(node, minor)) {
5677c478bd9Sstevel@tonic-gate 	    if (add_controller(args, node, minor) == NULL) {
5687c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
5697c478bd9Sstevel@tonic-gate 		result = DI_WALK_TERMINATE;
5707c478bd9Sstevel@tonic-gate 	    }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	} else if (di_minor_spectype(minor) == S_IFCHR && is_drive(minor)) {
5737c478bd9Sstevel@tonic-gate 	    char	*devidstr;
5747c478bd9Sstevel@tonic-gate 	    char	kernel_name[MAXPATHLEN];
5757c478bd9Sstevel@tonic-gate 	    disk_t	*diskp;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	    (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
5787c478bd9Sstevel@tonic-gate 		di_node_name(node), di_instance(node));
5797c478bd9Sstevel@tonic-gate 	    devidstr = get_str_prop(DEVICE_ID_PROP, node);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	    args->node = node;
5827c478bd9Sstevel@tonic-gate 	    args->minor = minor;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	    /* Check if we already got this disk and this is another slice */
5857c478bd9Sstevel@tonic-gate 	    if (!have_disk(args, devidstr, kernel_name, &diskp)) {
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		args->dev_walk_status = 0;
5887c478bd9Sstevel@tonic-gate 		/* This is a newly found disk, create the disk structure. */
5897c478bd9Sstevel@tonic-gate 		diskp = create_disk(devidstr, kernel_name, args);
5907c478bd9Sstevel@tonic-gate 		if (diskp == NULL) {
5917c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if (diskp->drv_type != DM_DT_FLOPPY) {
5957c478bd9Sstevel@tonic-gate 		    /* add the controller relationship */
5967c478bd9Sstevel@tonic-gate 		    if (args->dev_walk_status == 0) {
5977c478bd9Sstevel@tonic-gate 			if (add_disk2controller(diskp, args) != 0) {
5987c478bd9Sstevel@tonic-gate 			    args->dev_walk_status = ENOMEM;
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 		    }
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 	    }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	    /* Add the devpaths for the drive. */
6057c478bd9Sstevel@tonic-gate 	    if (args->dev_walk_status == 0) {
6067c478bd9Sstevel@tonic-gate 		char	*devpath;
6077c478bd9Sstevel@tonic-gate 		char	slice_path[MAXPATHLEN];
6087c478bd9Sstevel@tonic-gate 		char	*pattern;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6117c478bd9Sstevel@tonic-gate 		 * We will come through here once for each of the raw slice
6127c478bd9Sstevel@tonic-gate 		 * device names.
6137c478bd9Sstevel@tonic-gate 		 */
6147c478bd9Sstevel@tonic-gate 		devpath = di_devfs_path(node);
6157c478bd9Sstevel@tonic-gate 		(void) snprintf(slice_path, sizeof (slice_path), "%s:%s",
6167c478bd9Sstevel@tonic-gate 		    devpath, di_minor_name(minor));
6177c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(di_minor_nodetype(minor), DDI_NT_FD)) {
6207c478bd9Sstevel@tonic-gate 		    pattern = DEVLINK_FLOPPY_REGEX;
6217c478bd9Sstevel@tonic-gate 		} else {
6227c478bd9Sstevel@tonic-gate 		    pattern = DEVLINK_REGEX;
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		/* Walk the /dev tree to get the devlinks. */
6267c478bd9Sstevel@tonic-gate 		(void) di_devlink_walk(args->handle, pattern, slice_path,
6277c478bd9Sstevel@tonic-gate 		    DI_PRIMARY_LINK, arg, add_devpath);
6287c478bd9Sstevel@tonic-gate 	    }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	    if (args->dev_walk_status != 0) {
6317c478bd9Sstevel@tonic-gate 		result = DI_WALK_TERMINATE;
6327c478bd9Sstevel@tonic-gate 	    }
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	return (result);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate static int
6397c478bd9Sstevel@tonic-gate add_disk2controller(disk_t *diskp, struct search_args *args)
6407c478bd9Sstevel@tonic-gate {
6417c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
6427c478bd9Sstevel@tonic-gate 	controller_t	*cp;
6437c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
6447c478bd9Sstevel@tonic-gate 	di_node_t	node;
6457c478bd9Sstevel@tonic-gate 	int		i;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	node = args->node;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
6507c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
6517c478bd9Sstevel@tonic-gate 	    return (0);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	minor = di_minor_next(pnode, NULL);
6557c478bd9Sstevel@tonic-gate 	if (minor == NULL) {
6567c478bd9Sstevel@tonic-gate 	    return (0);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if ((cp = add_controller(args, pnode, minor)) == NULL) {
6607c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/* check if the disk <-> ctrl assoc is already there */
6647c478bd9Sstevel@tonic-gate 	for (i = 0; diskp->controllers[i]; i++) {
6657c478bd9Sstevel@tonic-gate 	    if (cp == diskp->controllers[i]) {
6667c478bd9Sstevel@tonic-gate 		return (0);
6677c478bd9Sstevel@tonic-gate 	    }
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/* this is a new controller for this disk */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* add the disk to the controlller */
6737c478bd9Sstevel@tonic-gate 	if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
6747c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	/* add the controlller to the disk */
6787c478bd9Sstevel@tonic-gate 	if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
6797c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * Set up paths for mpxio controlled drives.
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
6867c478bd9Sstevel@tonic-gate 	    /* note: mpxio di_path stuff is all consolidation private */
6877c478bd9Sstevel@tonic-gate 	    di_path_t   pi = DI_PATH_NIL;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	    while ((pi = di_path_next_phci(node, pi)) != DI_PATH_NIL) {
6907c478bd9Sstevel@tonic-gate 		int	cnt;
6917c478bd9Sstevel@tonic-gate 		uchar_t	*bytes;
6927c478bd9Sstevel@tonic-gate 		char	str[MAXPATHLEN];
6937c478bd9Sstevel@tonic-gate 		char	*wwn;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		di_node_t phci_node = di_path_phci_node(pi);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		/* get the node wwn */
6987c478bd9Sstevel@tonic-gate 		cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
6997c478bd9Sstevel@tonic-gate 		wwn = NULL;
7007c478bd9Sstevel@tonic-gate 		if (cnt > 0) {
7017c478bd9Sstevel@tonic-gate 		    int	i;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		    str[0] = 0;
7047c478bd9Sstevel@tonic-gate 		    for (i = 0; i < cnt; i++) {
7057c478bd9Sstevel@tonic-gate 			char bstr[8];	/* a byte is only 2 hex chars + null */
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 			(void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
7087c478bd9Sstevel@tonic-gate 			(void) strlcat(str, bstr, sizeof (str));
7097c478bd9Sstevel@tonic-gate 		    }
7107c478bd9Sstevel@tonic-gate 		    wwn = str;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		if (new_path(cp, diskp, phci_node, di_path_state(pi), wwn)
7147c478bd9Sstevel@tonic-gate 		    == NULL) {
7157c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 	    }
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	return (0);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate static int
7247c478bd9Sstevel@tonic-gate add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
7277c478bd9Sstevel@tonic-gate 	if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
7287c478bd9Sstevel@tonic-gate 	    cache_free_path(pp);
7297c478bd9Sstevel@tonic-gate 	    return (0);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* add the path to the disk */
7337c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
7347c478bd9Sstevel@tonic-gate 	    cache_free_path(pp);
7357c478bd9Sstevel@tonic-gate 	    return (0);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7397c478bd9Sstevel@tonic-gate 	if (add_int2array(st, &pp->states) != 0) {
7407c478bd9Sstevel@tonic-gate 	    cache_free_path(pp);
7417c478bd9Sstevel@tonic-gate 	    return (0);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7457c478bd9Sstevel@tonic-gate 	if (wwn != NULL) {
7467c478bd9Sstevel@tonic-gate 	    char	*wp;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	    if ((wp = strdup(wwn)) != NULL) {
7497c478bd9Sstevel@tonic-gate 		if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
7507c478bd9Sstevel@tonic-gate 		    cache_free_path(pp);
7517c478bd9Sstevel@tonic-gate 		    return (0);
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	    }
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (1);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate static int
7607c478bd9Sstevel@tonic-gate add_int2array(int p, int **parray)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	int		i;
7637c478bd9Sstevel@tonic-gate 	int		cnt;
7647c478bd9Sstevel@tonic-gate 	int		*pa;
7657c478bd9Sstevel@tonic-gate 	int		*new_array;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	pa = *parray;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	cnt = 0;
7707c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
7717c478bd9Sstevel@tonic-gate 	    for (; pa[cnt] != -1; cnt++);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	new_array = (int *)calloc(cnt + 2, sizeof (int *));
7757c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
7767c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	/* copy the existing array */
7807c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
7817c478bd9Sstevel@tonic-gate 	    new_array[i] = pa[i];
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	new_array[i] = p;
7857c478bd9Sstevel@tonic-gate 	new_array[i + 1] = -1;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	free(pa);
7887c478bd9Sstevel@tonic-gate 	*parray = new_array;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	return (0);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate static int
7947c478bd9Sstevel@tonic-gate add_ptr2array(void *p, void ***parray)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	int		i;
7977c478bd9Sstevel@tonic-gate 	int		cnt;
7987c478bd9Sstevel@tonic-gate 	void		**pa;
7997c478bd9Sstevel@tonic-gate 	void		**new_array;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	pa = *parray;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	cnt = 0;
8047c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
8057c478bd9Sstevel@tonic-gate 	    for (; pa[cnt]; cnt++);
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	new_array = (void **)calloc(cnt + 2, sizeof (void *));
8097c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
8107c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/* copy the existing array */
8147c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
8157c478bd9Sstevel@tonic-gate 	    new_array[i] = pa[i];
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	new_array[i] = p;
8197c478bd9Sstevel@tonic-gate 	new_array[i + 1] = NULL;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	free(pa);
8227c478bd9Sstevel@tonic-gate 	*parray = new_array;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	return (0);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * This double checks that we aren't going to get into a bad situation.
8297c478bd9Sstevel@tonic-gate  * This function should never fail, but I just want to double check things.
8307c478bd9Sstevel@tonic-gate  */
8317c478bd9Sstevel@tonic-gate static int
8327c478bd9Sstevel@tonic-gate can_remove_controller(controller_t *cp, controller_t *currp)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	if (dm_debug) {
8357c478bd9Sstevel@tonic-gate 	    if (cp == currp) {
8367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ERROR: remove current controller\n");
8377c478bd9Sstevel@tonic-gate 	    }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	    if (cp->disks != NULL && cp->disks[0] != NULL) {
8407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8417c478bd9Sstevel@tonic-gate 		    "ERROR: remove controller with disk ptrs\n");
8427c478bd9Sstevel@tonic-gate 	    }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	    if (cp->paths != NULL && cp->paths[0] != NULL) {
8457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8467c478bd9Sstevel@tonic-gate 		    "ERROR: remove controller with path ptrs\n");
8477c478bd9Sstevel@tonic-gate 	    }
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	return (1);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate /*
8547c478bd9Sstevel@tonic-gate  * If we have a controller in the list that is really a path then we need to
8557c478bd9Sstevel@tonic-gate  * take that controller out of the list since nodes that are paths are not
8567c478bd9Sstevel@tonic-gate  * considered to be controllers.
8577c478bd9Sstevel@tonic-gate  */
8587c478bd9Sstevel@tonic-gate static void
8597c478bd9Sstevel@tonic-gate clean_paths(struct search_args *args)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	controller_t	*cp;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
8647c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
8657c478bd9Sstevel@tonic-gate 	    path_t	**pp;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	    pp = cp->paths;
8687c478bd9Sstevel@tonic-gate 	    if (pp != NULL) {
8697c478bd9Sstevel@tonic-gate 		int i;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		for (i = 0; pp[i]; i++) {
8727c478bd9Sstevel@tonic-gate 		    remove_invalid_controller(pp[i]->name, cp, args);
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 	    }
8757c478bd9Sstevel@tonic-gate 	    cp = cp->next;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate static disk_t *
8807c478bd9Sstevel@tonic-gate create_disk(char *deviceid, char *kernel_name, struct search_args *args)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	disk_t	*diskp;
8837c478bd9Sstevel@tonic-gate 	char	*type;
8847c478bd9Sstevel@tonic-gate 	char	*prod_id;
8857c478bd9Sstevel@tonic-gate 	char	*vendor_id;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if (dm_debug) {
8887c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	diskp = calloc(1, sizeof (disk_t));
8927c478bd9Sstevel@tonic-gate 	if (diskp == NULL) {
8937c478bd9Sstevel@tonic-gate 	    return (NULL);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	diskp->controllers = (controller_t **)
8977c478bd9Sstevel@tonic-gate 	    calloc(1, sizeof (controller_t *));
8987c478bd9Sstevel@tonic-gate 	if (diskp->controllers == NULL) {
8997c478bd9Sstevel@tonic-gate 	    cache_free_disk(diskp);
9007c478bd9Sstevel@tonic-gate 	    return (NULL);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	diskp->controllers[0] = NULL;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	diskp->devid = NULL;
9057c478bd9Sstevel@tonic-gate 	if (deviceid != NULL) {
9067c478bd9Sstevel@tonic-gate 	    if ((diskp->device_id = strdup(deviceid)) == NULL) {
9077c478bd9Sstevel@tonic-gate 		cache_free_disk(diskp);
9087c478bd9Sstevel@tonic-gate 		return (NULL);
9097c478bd9Sstevel@tonic-gate 	    }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	    (void) devid_str_decode(deviceid, &(diskp->devid), NULL);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
9157c478bd9Sstevel@tonic-gate 	    diskp->kernel_name = strdup(kernel_name);
9167c478bd9Sstevel@tonic-gate 	    if (diskp->kernel_name == NULL) {
9177c478bd9Sstevel@tonic-gate 		cache_free_disk(diskp);
9187c478bd9Sstevel@tonic-gate 		return (NULL);
9197c478bd9Sstevel@tonic-gate 	    }
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	diskp->paths = NULL;
9237c478bd9Sstevel@tonic-gate 	diskp->aliases = NULL;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	diskp->cd_rom = 0;
9267c478bd9Sstevel@tonic-gate 	diskp->rpm = 0;
9277c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(args->minor);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	prod_id = get_str_prop(PROD_ID_PROP, args->node);
9307c478bd9Sstevel@tonic-gate 	if (prod_id != NULL) {
9317c478bd9Sstevel@tonic-gate 	    if ((diskp->product_id = strdup(prod_id)) == NULL) {
9327c478bd9Sstevel@tonic-gate 		cache_free_disk(diskp);
9337c478bd9Sstevel@tonic-gate 		return (NULL);
9347c478bd9Sstevel@tonic-gate 	    }
9357c478bd9Sstevel@tonic-gate 	} else {
9367c478bd9Sstevel@tonic-gate 	    prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
9377c478bd9Sstevel@tonic-gate 	    if (prod_id != NULL) {
9387c478bd9Sstevel@tonic-gate 		if ((diskp->product_id = strdup(prod_id)) == NULL) {
9397c478bd9Sstevel@tonic-gate 		    cache_free_disk(diskp);
9407c478bd9Sstevel@tonic-gate 		    return (NULL);
9417c478bd9Sstevel@tonic-gate 		}
9427c478bd9Sstevel@tonic-gate 	    }
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
9467c478bd9Sstevel@tonic-gate 	if (vendor_id != NULL) {
9477c478bd9Sstevel@tonic-gate 	    if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
9487c478bd9Sstevel@tonic-gate 		cache_free_disk(diskp);
9497c478bd9Sstevel@tonic-gate 		return (NULL);
9507c478bd9Sstevel@tonic-gate 	    }
9517c478bd9Sstevel@tonic-gate 	} else {
9527c478bd9Sstevel@tonic-gate 	    vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
9537c478bd9Sstevel@tonic-gate 	    if (vendor_id != NULL) {
9547c478bd9Sstevel@tonic-gate 		if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
9557c478bd9Sstevel@tonic-gate 		    cache_free_disk(diskp);
9567c478bd9Sstevel@tonic-gate 		    return (NULL);
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 	    }
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
9637c478bd9Sstevel@tonic-gate 	 * We try to use uscsi later to determine the real type.
9647c478bd9Sstevel@tonic-gate 	 * The cd_rom flag tells us that the kernel categorized the drive
9657c478bd9Sstevel@tonic-gate 	 * as a CD-ROM.  We leave the drv_type as UKNOWN for now.
9667c478bd9Sstevel@tonic-gate 	 * The combination of the cd_rom flag being set with the drv_type of
9677c478bd9Sstevel@tonic-gate 	 * unknown is what triggers the uscsi probe in drive.c.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	if (disk_is_cdrom(type)) {
9707c478bd9Sstevel@tonic-gate 	    diskp->drv_type = DM_DT_UNKNOWN;
9717c478bd9Sstevel@tonic-gate 	    diskp->cd_rom = 1;
9727c478bd9Sstevel@tonic-gate 	    diskp->removable = 1;
9737c478bd9Sstevel@tonic-gate 	} else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
9747c478bd9Sstevel@tonic-gate 	    diskp->drv_type = DM_DT_FLOPPY;
9757c478bd9Sstevel@tonic-gate 	    diskp->removable = 1;
9767c478bd9Sstevel@tonic-gate 	} else {
9777c478bd9Sstevel@tonic-gate 		/* not a "CD-ROM" or Floppy */
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	    diskp->removable = get_prop(REMOVABLE_PROP, args->node);
9807c478bd9Sstevel@tonic-gate 	    if (diskp->removable == -1) {
981*0167b58cScg149915 		/*
982*0167b58cScg149915 		 * This is a workaround. Hotpluggable devices don't export
983*0167b58cScg149915 		 * a "removable-media" property, but they are treated as
984*0167b58cScg149915 		 * removable media devices by vold to implement automount.
985*0167b58cScg149915 		 * Once vold is EOL'ed, it should be removed.
986*0167b58cScg149915 		 */
987*0167b58cScg149915 		diskp->removable = get_prop(HOTPLUGGABLE_PROP, args->node);
988*0167b58cScg149915 	    }
989*0167b58cScg149915 
990*0167b58cScg149915 	    if (diskp->removable == -1) {
9917c478bd9Sstevel@tonic-gate 		diskp->removable = 0;
9923e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
9937c478bd9Sstevel@tonic-gate 		/*
9947c478bd9Sstevel@tonic-gate 		 * x86 does not have removable property.  Check for common
9957c478bd9Sstevel@tonic-gate 		 * removable drives, zip & jaz, and mark those correctly.
9967c478bd9Sstevel@tonic-gate 		 */
9977c478bd9Sstevel@tonic-gate 		if (vendor_id != NULL && prod_id != NULL) {
9987c478bd9Sstevel@tonic-gate 		    if (str_case_index(vendor_id, "iomega") != NULL) {
9997c478bd9Sstevel@tonic-gate 			if (str_case_index(prod_id, "jaz") != NULL) {
10007c478bd9Sstevel@tonic-gate 			    diskp->removable = 1;
10017c478bd9Sstevel@tonic-gate 			} else if (str_case_index(prod_id, "zip") != NULL) {
10027c478bd9Sstevel@tonic-gate 			    diskp->removable = 1;
10037c478bd9Sstevel@tonic-gate 			}
10047c478bd9Sstevel@tonic-gate 		    }
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate #endif
10077c478bd9Sstevel@tonic-gate 	    }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	    if (diskp->removable) {
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * For removable jaz or zip drives there is no way
10127c478bd9Sstevel@tonic-gate 		 * to get the drive type unless media is inserted, so we
10137c478bd9Sstevel@tonic-gate 		 * look at the product-id for a hint.
10147c478bd9Sstevel@tonic-gate 		 */
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 		diskp->drv_type = DM_DT_UNKNOWN;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 		if (prod_id != NULL) {
10197c478bd9Sstevel@tonic-gate 		    if (str_case_index(prod_id, "jaz") != NULL) {
10207c478bd9Sstevel@tonic-gate 			diskp->drv_type = DM_DT_JAZ;
10217c478bd9Sstevel@tonic-gate 		    } else if (str_case_index(prod_id, "zip") != NULL) {
10227c478bd9Sstevel@tonic-gate 			diskp->drv_type = DM_DT_ZIP;
10237c478bd9Sstevel@tonic-gate 		    }
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 	    } else {
10267c478bd9Sstevel@tonic-gate 		diskp->drv_type = DM_DT_FIXED;
10277c478bd9Sstevel@tonic-gate 	    }
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	diskp->volm_path_set = 0;
10307c478bd9Sstevel@tonic-gate 	diskp->volm_path = NULL;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	diskp->next = args->disk_listp;
10337c478bd9Sstevel@tonic-gate 	args->disk_listp = diskp;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	return (diskp);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate static char *
10397c478bd9Sstevel@tonic-gate ctype(di_node_t node, di_minor_t minor)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	char	*type;
10427c478bd9Sstevel@tonic-gate 	char	*name;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
10457c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	/* IDE disks use SCSI nexus as the type, so handle this special case */
10487c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(name, "ide")) {
10497c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_ATA);
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb")) {
10537c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_USB);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
10577c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT)) {
10587c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_SCSI);
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT)) {
10627c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_FIBRE);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
10667c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "fp")) {
10677c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_FIBRE);
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
10717c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "ide")) {
10727c478bd9Sstevel@tonic-gate 	    return (DM_CTYPE_ATA);
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	return (DM_CTYPE_UNKNOWN);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate static boolean_t
10797c478bd9Sstevel@tonic-gate disk_is_cdrom(char *type)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	int		type_index;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	for (type_index = 0; cdromtypes[type_index] != NULL; type_index++) {
10857c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(type, cdromtypes[type_index])) {
10867c478bd9Sstevel@tonic-gate 		return (B_TRUE);
10877c478bd9Sstevel@tonic-gate 	    }
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	return (B_FALSE);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate static alias_t *
10947c478bd9Sstevel@tonic-gate find_alias(disk_t *diskp, char *kernel_name)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	alias_t	*ap;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
10997c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
11007c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
11017c478bd9Sstevel@tonic-gate 		return (ap);
11027c478bd9Sstevel@tonic-gate 	    }
11037c478bd9Sstevel@tonic-gate 	    ap = ap->next;
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	return (NULL);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static bus_t *
11107c478bd9Sstevel@tonic-gate find_bus(struct search_args *args, char *name)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	bus_t *listp;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	listp = args->bus_listp;
11157c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
11167c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(listp->name, name)) {
11177c478bd9Sstevel@tonic-gate 		return (listp);
11187c478bd9Sstevel@tonic-gate 	    }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	    listp = listp->next;
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	return (NULL);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate static controller_t *
11277c478bd9Sstevel@tonic-gate find_controller(struct search_args *args, char *name)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	controller_t *listp;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	listp = args->controller_listp;
11327c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
11337c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(listp->name, name)) {
11347c478bd9Sstevel@tonic-gate 		return (listp);
11357c478bd9Sstevel@tonic-gate 	    }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	    listp = listp->next;
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	return (NULL);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate fix_cluster_devpath(di_devlink_t devlink, void *arg)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	int			fd;
11477c478bd9Sstevel@tonic-gate 	struct search_args	*args;
11487c478bd9Sstevel@tonic-gate 	char			*devlink_path;
11497c478bd9Sstevel@tonic-gate 	disk_t			*diskp = NULL;
11507c478bd9Sstevel@tonic-gate 	alias_t			*ap = NULL;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * The devlink_path is of the form /dev/did/rdsk/d1s0.
11547c478bd9Sstevel@tonic-gate 	 */
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/* Find the disk by the deviceid we read from the cluster disk. */
11597c478bd9Sstevel@tonic-gate 	devlink_path = (char *)di_devlink_path(devlink);
11607c478bd9Sstevel@tonic-gate 	if (devlink_path == NULL) {
11617c478bd9Sstevel@tonic-gate 	    return (DI_WALK_CONTINUE);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if ((fd = open(devlink_path, O_RDONLY|O_NDELAY)) >= 0) {
11657c478bd9Sstevel@tonic-gate 	    ddi_devid_t	devid;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	    if (dm_debug > 1) {
11687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO:     cluster devpath %s\n",
11697c478bd9Sstevel@tonic-gate 		    devlink_path);
11707c478bd9Sstevel@tonic-gate 	    }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	    if (devid_get(fd, &devid) == 0) {
11737c478bd9Sstevel@tonic-gate 		char *minor;
11747c478bd9Sstevel@tonic-gate 		char *devidstr;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		minor = di_minor_name(args->minor);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		if ((devidstr = devid_str_encode(devid, minor)) != NULL) {
11797c478bd9Sstevel@tonic-gate 		    diskp = get_disk_by_deviceid(args->disk_listp, devidstr);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 			/*
11827c478bd9Sstevel@tonic-gate 			 * This really shouldn't happen, since we should have
11837c478bd9Sstevel@tonic-gate 			 * found all of the disks during our first pass through
11847c478bd9Sstevel@tonic-gate 			 * the dev tree, but just in case...
11857c478bd9Sstevel@tonic-gate 			 */
11867c478bd9Sstevel@tonic-gate 			if (diskp == NULL) {
11877c478bd9Sstevel@tonic-gate 			    if (dm_debug > 1) {
11887c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
11897c478bd9Sstevel@tonic-gate 				    "INFO:     cluster create disk\n");
11907c478bd9Sstevel@tonic-gate 			    }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			    diskp = create_disk(devidstr, NULL, args);
11937c478bd9Sstevel@tonic-gate 			    if (diskp == NULL) {
11947c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
11957c478bd9Sstevel@tonic-gate 			    }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 			    /* add the controller relationship */
11987c478bd9Sstevel@tonic-gate 			    if (args->dev_walk_status == 0) {
11997c478bd9Sstevel@tonic-gate 				if (add_disk2controller(diskp, args) != 0) {
12007c478bd9Sstevel@tonic-gate 				    args->dev_walk_status = ENOMEM;
12017c478bd9Sstevel@tonic-gate 				}
12027c478bd9Sstevel@tonic-gate 			    }
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 			    if (new_alias(diskp, NULL, devlink_path, args)
12057c478bd9Sstevel@tonic-gate 				!= 0) {
12067c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
12077c478bd9Sstevel@tonic-gate 			    }
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 			devid_str_free(devidstr);
12117c478bd9Sstevel@tonic-gate 		}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		devid_free(devid);
12147c478bd9Sstevel@tonic-gate 	    }
12157c478bd9Sstevel@tonic-gate 	    (void) close(fd);
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
12207c478bd9Sstevel@tonic-gate 	    if (dm_debug > 1) {
12217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO:     cluster found disk\n");
12227c478bd9Sstevel@tonic-gate 	    }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	    ap = diskp->aliases;
12257c478bd9Sstevel@tonic-gate 	}
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 	if (ap != NULL) {
12287c478bd9Sstevel@tonic-gate 	    /* NOTE: if ap->next != NULL have cluster disks w/ multiple paths */
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	    if (!ap->cluster) {
12317c478bd9Sstevel@tonic-gate 		char	*basep;
12327c478bd9Sstevel@tonic-gate 		char	*namep;
12337c478bd9Sstevel@tonic-gate 		int	cnt = 0;
12347c478bd9Sstevel@tonic-gate 		int	size;
12357c478bd9Sstevel@tonic-gate 		char	alias[MAXPATHLEN];
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		/*
12387c478bd9Sstevel@tonic-gate 		 * First time; save the /dev/rdsk devpaths and update the
12397c478bd9Sstevel@tonic-gate 		 * alias info with the new alias name.
12407c478bd9Sstevel@tonic-gate 		 */
12417c478bd9Sstevel@tonic-gate 		ap->orig_paths = ap->devpaths;
12427c478bd9Sstevel@tonic-gate 		ap->devpaths = NULL;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 		free(ap->alias);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 		/* get the new cluster alias name */
12477c478bd9Sstevel@tonic-gate 		basep = strrchr(devlink_path, '/');
12487c478bd9Sstevel@tonic-gate 		if (basep == NULL) {
12497c478bd9Sstevel@tonic-gate 		    basep = devlink_path;
12507c478bd9Sstevel@tonic-gate 		} else {
12517c478bd9Sstevel@tonic-gate 		    basep++;
12527c478bd9Sstevel@tonic-gate 		}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		size = sizeof (alias) - 1;
12557c478bd9Sstevel@tonic-gate 		namep = alias;
12567c478bd9Sstevel@tonic-gate 		while (*basep != 0 && *basep != 's' && cnt < size) {
12577c478bd9Sstevel@tonic-gate 		    *namep++ = *basep++;
12587c478bd9Sstevel@tonic-gate 		    cnt++;
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate 		*namep = 0;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		if ((ap->alias = strdup(alias)) == NULL) {
12637c478bd9Sstevel@tonic-gate 		    args->dev_walk_status = ENOMEM;
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		ap->cluster = 1;
12677c478bd9Sstevel@tonic-gate 	    }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	    if (new_devpath(ap, devlink_path) != 0) {
12707c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
12717c478bd9Sstevel@tonic-gate 	    }
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate /*
12787c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id.
12797c478bd9Sstevel@tonic-gate  * We got the device id from the dev tree walk.  This is encoded
12807c478bd9Sstevel@tonic-gate  * using devid_str_encode(3DEVID).   In order to check the device ids we need
12817c478bd9Sstevel@tonic-gate  * to use the devid_compare(3DEVID) function, so we need to decode the
12827c478bd9Sstevel@tonic-gate  * string representation of the device id.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate static disk_t *
12857c478bd9Sstevel@tonic-gate get_disk_by_deviceid(disk_t *listp, char *devidstr)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
12907c478bd9Sstevel@tonic-gate 	    return (NULL);
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
12947c478bd9Sstevel@tonic-gate 	    if (listp->devid != NULL &&
12957c478bd9Sstevel@tonic-gate 		devid_compare(listp->devid, devid) == 0) {
12967c478bd9Sstevel@tonic-gate 		break;
12977c478bd9Sstevel@tonic-gate 	    }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	    listp = listp->next;
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	devid_free(devid);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	return (listp);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate  * Get the base disk name with no path prefix and no slice (if there is one).
13097c478bd9Sstevel@tonic-gate  * The name parameter should be big enough to hold the name.
13107c478bd9Sstevel@tonic-gate  * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
13117c478bd9Sstevel@tonic-gate  * and converts the raw diskette name.
13127c478bd9Sstevel@tonic-gate  * But, we don't know how to strip off the slice from third party drive
13137c478bd9Sstevel@tonic-gate  * names.  That just means that their drive name will include a slice on
13147c478bd9Sstevel@tonic-gate  * it.
13157c478bd9Sstevel@tonic-gate  */
13167c478bd9Sstevel@tonic-gate static void
13177c478bd9Sstevel@tonic-gate get_disk_name_from_path(char *path, char *name, int size)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	char		*basep;
13207c478bd9Sstevel@tonic-gate 	int		cnt = 0;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	basep = strrchr(path, '/');
13237c478bd9Sstevel@tonic-gate 	if (basep == NULL) {
13247c478bd9Sstevel@tonic-gate 	    basep = path;
13257c478bd9Sstevel@tonic-gate 	} else {
13267c478bd9Sstevel@tonic-gate 	    basep++;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	size = size - 1;	/* leave room for terminating 0 */
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	if (is_ctds(basep)) {
13327c478bd9Sstevel@tonic-gate 	    while (*basep != 0 && *basep != 's' && cnt < size) {
13337c478bd9Sstevel@tonic-gate 		*name++ = *basep++;
13347c478bd9Sstevel@tonic-gate 		cnt++;
13357c478bd9Sstevel@tonic-gate 	    }
13367c478bd9Sstevel@tonic-gate 	    *name = 0;
13377c478bd9Sstevel@tonic-gate 	} else {
13387c478bd9Sstevel@tonic-gate 	    if (strncmp(basep, FLOPPY_NAME, sizeof (FLOPPY_NAME) - 1) == 0) {
13397c478bd9Sstevel@tonic-gate 		/*
13407c478bd9Sstevel@tonic-gate 		 * a floppy, convert rdiskette name to diskette name,
13417c478bd9Sstevel@tonic-gate 		 * by skipping over the 'r' for raw diskette
13427c478bd9Sstevel@tonic-gate 		 */
13437c478bd9Sstevel@tonic-gate 		basep++;
13447c478bd9Sstevel@tonic-gate 	    }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	    /* not a ctds name, just copy it */
13477c478bd9Sstevel@tonic-gate 	    (void) strlcpy(name, basep, size);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate }
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate static char *
13527c478bd9Sstevel@tonic-gate get_byte_prop(char *prop_name, di_node_t node)
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate 	int	cnt;
13557c478bd9Sstevel@tonic-gate 	uchar_t	*bytes;
13567c478bd9Sstevel@tonic-gate 	int	i;
13577c478bd9Sstevel@tonic-gate 	char	str[MAXPATHLEN];
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
13607c478bd9Sstevel@tonic-gate 	if (cnt < 1) {
13617c478bd9Sstevel@tonic-gate 	    return (NULL);
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	str[0] = 0;
13657c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
13667c478bd9Sstevel@tonic-gate 	    char bstr[8];	/* a byte is only 2 hex chars + null */
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	    (void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
13697c478bd9Sstevel@tonic-gate 	    (void) strlcat(str, bstr, sizeof (str));
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 	return (strdup(str));
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate static di_node_t
13757c478bd9Sstevel@tonic-gate get_parent_bus(di_node_t node, struct search_args *args)
13767c478bd9Sstevel@tonic-gate {
13777c478bd9Sstevel@tonic-gate 	di_node_t pnode;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
13807c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
13817c478bd9Sstevel@tonic-gate 	    return (NULL);
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
13857c478bd9Sstevel@tonic-gate 	    return (pnode);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	return (get_parent_bus(pnode, args));
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate static int
13927c478bd9Sstevel@tonic-gate get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	int *n;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
13977c478bd9Sstevel@tonic-gate 	    return (*n);
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	return (0);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate static char *
14047c478bd9Sstevel@tonic-gate get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	char *str;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
14097c478bd9Sstevel@tonic-gate 	    return (str);
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	return (NULL);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate  * Get one of the positive int or boolean properties.
14177c478bd9Sstevel@tonic-gate  */
14187c478bd9Sstevel@tonic-gate static int
14197c478bd9Sstevel@tonic-gate get_prop(char *prop_name, di_node_t node)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	int num;
14227c478bd9Sstevel@tonic-gate 	int *ip;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
14257c478bd9Sstevel@tonic-gate 	    >= 0) {
14267c478bd9Sstevel@tonic-gate 	    if (num == 0) {
14277c478bd9Sstevel@tonic-gate 		/* boolean */
14287c478bd9Sstevel@tonic-gate 		return (1);
14297c478bd9Sstevel@tonic-gate 	    } else if (num == 1) {
14307c478bd9Sstevel@tonic-gate 		/* single int */
14317c478bd9Sstevel@tonic-gate 		return (*ip);
14327c478bd9Sstevel@tonic-gate 	    }
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	return (-1);
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate static char *
14397c478bd9Sstevel@tonic-gate get_str_prop(char *prop_name, di_node_t node)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate 	char *str;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
14447c478bd9Sstevel@tonic-gate 	    return (str);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	return (NULL);
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate /*
14517c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id, if the
14527c478bd9Sstevel@tonic-gate  * drive has a device id, or the kernel name, if it doesn't have a device id.
14537c478bd9Sstevel@tonic-gate  */
14547c478bd9Sstevel@tonic-gate static int
14557c478bd9Sstevel@tonic-gate have_disk(struct search_args *args, char *devidstr, char *kernel_name,
14567c478bd9Sstevel@tonic-gate     disk_t **diskp)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	disk_t *listp;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	*diskp = NULL;
14617c478bd9Sstevel@tonic-gate 	listp = args->disk_listp;
14627c478bd9Sstevel@tonic-gate 	if (devidstr != NULL) {
14637c478bd9Sstevel@tonic-gate 	    if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
14647c478bd9Sstevel@tonic-gate 		return (1);
14657c478bd9Sstevel@tonic-gate 	    }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	} else {
14687c478bd9Sstevel@tonic-gate 	    /* no devid, try matching the kernel names on the drives */
14697c478bd9Sstevel@tonic-gate 	    while (listp != NULL) {
14707c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(kernel_name, listp->kernel_name)) {
14717c478bd9Sstevel@tonic-gate 		    *diskp = listp;
14727c478bd9Sstevel@tonic-gate 		    return (1);
14737c478bd9Sstevel@tonic-gate 		}
14747c478bd9Sstevel@tonic-gate 		listp = listp->next;
14757c478bd9Sstevel@tonic-gate 	    }
14767c478bd9Sstevel@tonic-gate 	}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	return (0);
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate static char *
14827c478bd9Sstevel@tonic-gate bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	char	*type;
14857c478bd9Sstevel@tonic-gate 	int	i;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	type = get_prom_str("device_type", node, ph);
14887c478bd9Sstevel@tonic-gate 	if (type == NULL) {
14897c478bd9Sstevel@tonic-gate 	    type = di_node_name(node);
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	for (i = 0; bustypes[i]; i++) {
14937c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(type, bustypes[i])) {
14947c478bd9Sstevel@tonic-gate 		return (type);
14957c478bd9Sstevel@tonic-gate 	    }
14967c478bd9Sstevel@tonic-gate 	}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (minor != NULL && strcmp(di_minor_nodetype(minor),
14997c478bd9Sstevel@tonic-gate 	    DDI_NT_USB_ATTACHMENT_POINT) == 0) {
15007c478bd9Sstevel@tonic-gate 	    return ("usb");
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	return (NULL);
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate static int
15077c478bd9Sstevel@tonic-gate is_cluster_disk(di_node_t node, di_minor_t minor)
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate 	if (di_minor_spectype(minor) == S_IFCHR &&
15107c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(di_minor_nodetype(minor), DDI_PSEUDO) &&
15117c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(di_node_name(node), CLUSTER_DEV)) {
15127c478bd9Sstevel@tonic-gate 	    return (1);
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	return (0);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /*
15197c478bd9Sstevel@tonic-gate  * If the input name is in c[t]ds format then return 1, otherwise return 0.
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate static int
15227c478bd9Sstevel@tonic-gate is_ctds(char *name)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	char	*p;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	p = name;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	if (*p++ != 'c') {
15297c478bd9Sstevel@tonic-gate 	    return (0);
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 	/* skip controller digits */
15327c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
15337c478bd9Sstevel@tonic-gate 	    p++;
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	/* handle optional target */
15377c478bd9Sstevel@tonic-gate 	if (*p == 't') {
15387c478bd9Sstevel@tonic-gate 	    p++;
15397c478bd9Sstevel@tonic-gate 	    /* skip over target */
15407c478bd9Sstevel@tonic-gate 	    while (isdigit(*p) || isupper(*p)) {
15417c478bd9Sstevel@tonic-gate 		p++;
15427c478bd9Sstevel@tonic-gate 	    }
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	if (*p++ != 'd') {
15467c478bd9Sstevel@tonic-gate 	    return (0);
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
15497c478bd9Sstevel@tonic-gate 	    p++;
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	if (*p++ != 's') {
15537c478bd9Sstevel@tonic-gate 	    return (0);
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	/* check the slice number */
15577c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
15587c478bd9Sstevel@tonic-gate 	    p++;
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	if (*p != 0) {
15627c478bd9Sstevel@tonic-gate 	    return (0);
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	return (1);
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate static int
15697c478bd9Sstevel@tonic-gate is_drive(di_minor_t minor)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	char	*type;
15727c478bd9Sstevel@tonic-gate 	int	type_index;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
15757c478bd9Sstevel@tonic-gate 	type_index = 0;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	while (disktypes[type_index] != NULL) {
15787c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(type, disktypes[type_index])) {
15797c478bd9Sstevel@tonic-gate 		return (1);
15807c478bd9Sstevel@tonic-gate 	    }
15817c478bd9Sstevel@tonic-gate 	    type_index++;
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	return (0);
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate static int
15887c478bd9Sstevel@tonic-gate is_HBA(di_node_t node, di_minor_t minor)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	char	*type;
15917c478bd9Sstevel@tonic-gate 	char	*name;
15927c478bd9Sstevel@tonic-gate 	int	type_index;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
15957c478bd9Sstevel@tonic-gate 	type_index = 0;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	while (ctrltypes[type_index] != NULL) {
15987c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
15997c478bd9Sstevel@tonic-gate 		return (1);
16007c478bd9Sstevel@tonic-gate 	    }
16017c478bd9Sstevel@tonic-gate 	    type_index++;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
16057c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
16067c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "ide")) {
16077c478bd9Sstevel@tonic-gate 	    return (1);
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	return (0);
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate static int
16147c478bd9Sstevel@tonic-gate new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
16157c478bd9Sstevel@tonic-gate 	struct search_args *args)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	alias_t		*aliasp;
16187c478bd9Sstevel@tonic-gate 	char		alias[MAXPATHLEN];
16197c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	aliasp = malloc(sizeof (alias_t));
16227c478bd9Sstevel@tonic-gate 	if (aliasp == NULL) {
16237c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	aliasp->alias = NULL;
16277c478bd9Sstevel@tonic-gate 	aliasp->kstat_name = NULL;
16287c478bd9Sstevel@tonic-gate 	aliasp->wwn = NULL;
16297c478bd9Sstevel@tonic-gate 	aliasp->devpaths = NULL;
16307c478bd9Sstevel@tonic-gate 	aliasp->orig_paths = NULL;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	get_disk_name_from_path(devlink_path, alias, sizeof (alias));
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	aliasp->alias = strdup(alias);
16357c478bd9Sstevel@tonic-gate 	if (aliasp->alias == NULL) {
16367c478bd9Sstevel@tonic-gate 	    cache_free_alias(aliasp);
16377c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
16387c478bd9Sstevel@tonic-gate 	}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
16417c478bd9Sstevel@tonic-gate 	    aliasp->kstat_name = strdup(kernel_name);
16427c478bd9Sstevel@tonic-gate 	    if (aliasp->kstat_name == NULL) {
16437c478bd9Sstevel@tonic-gate 		cache_free_alias(aliasp);
16447c478bd9Sstevel@tonic-gate 		return (ENOMEM);
16457c478bd9Sstevel@tonic-gate 	    }
16467c478bd9Sstevel@tonic-gate 	} else {
16477c478bd9Sstevel@tonic-gate 	    aliasp->kstat_name = NULL;
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	aliasp->cluster = 0;
16517c478bd9Sstevel@tonic-gate 	aliasp->lun = get_prop(DM_LUN, args->node);
16527c478bd9Sstevel@tonic-gate 	aliasp->target = get_prop(DM_TARGET, args->node);
16537c478bd9Sstevel@tonic-gate 	aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(args->node);
16567c478bd9Sstevel@tonic-gate 	if (pnode != DI_NODE_NIL) {
16577c478bd9Sstevel@tonic-gate 	    char prop_name[MAXPROPLEN];
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	    (void) snprintf(prop_name, sizeof (prop_name),
16607c478bd9Sstevel@tonic-gate 		"target%d-sync-speed", aliasp->target);
16617c478bd9Sstevel@tonic-gate 	    diskp->sync_speed = get_prop(prop_name, pnode);
16627c478bd9Sstevel@tonic-gate 	    (void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
16637c478bd9Sstevel@tonic-gate 		aliasp->target);
16647c478bd9Sstevel@tonic-gate 	    diskp->wide = get_prop(prop_name, pnode);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	if (new_devpath(aliasp, devlink_path) != 0) {
16687c478bd9Sstevel@tonic-gate 	    cache_free_alias(aliasp);
16697c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	aliasp->next = diskp->aliases;
16737c478bd9Sstevel@tonic-gate 	diskp->aliases = aliasp;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	return (0);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate /*
16797c478bd9Sstevel@tonic-gate  * Append the new devpath to the end of the devpath list.  This is important
16807c478bd9Sstevel@tonic-gate  * since we may want to use the order of the devpaths to match up the vtoc
16817c478bd9Sstevel@tonic-gate  * entries.
16827c478bd9Sstevel@tonic-gate  */
16837c478bd9Sstevel@tonic-gate static int
16847c478bd9Sstevel@tonic-gate new_devpath(alias_t *ap, char *devpath)
16857c478bd9Sstevel@tonic-gate {
16867c478bd9Sstevel@tonic-gate 	slice_t	*newdp;
16877c478bd9Sstevel@tonic-gate 	slice_t *alistp;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/*
16907c478bd9Sstevel@tonic-gate 	 * First, search the alias list to be sure that this devpath is
16917c478bd9Sstevel@tonic-gate 	 * not already there.
16927c478bd9Sstevel@tonic-gate 	 */
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
16957c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
16967c478bd9Sstevel@tonic-gate 		return (0);
16977c478bd9Sstevel@tonic-gate 	    }
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	/*
17017c478bd9Sstevel@tonic-gate 	 * Otherwise, not found so add this new devpath to the list.
17027c478bd9Sstevel@tonic-gate 	 */
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	newdp = malloc(sizeof (slice_t));
17057c478bd9Sstevel@tonic-gate 	if (newdp == NULL) {
17067c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	newdp->devpath = strdup(devpath);
17107c478bd9Sstevel@tonic-gate 	if (newdp->devpath == NULL) {
17117c478bd9Sstevel@tonic-gate 	    free(newdp);
17127c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate 	newdp->slice_num = -1;
17157c478bd9Sstevel@tonic-gate 	newdp->next = NULL;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if (ap->devpaths == NULL) {
17187c478bd9Sstevel@tonic-gate 	    ap->devpaths = newdp;
17197c478bd9Sstevel@tonic-gate 	} else {
17207c478bd9Sstevel@tonic-gate 	    /* append the devpath to the end of the list */
17217c478bd9Sstevel@tonic-gate 	    slice_t	*dp;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	    dp = ap->devpaths;
17247c478bd9Sstevel@tonic-gate 	    while (dp->next != NULL) {
17257c478bd9Sstevel@tonic-gate 		dp = dp->next;
17267c478bd9Sstevel@tonic-gate 	    }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	    dp->next = newdp;
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	return (0);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate static path_t *
17357c478bd9Sstevel@tonic-gate new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
17367c478bd9Sstevel@tonic-gate 	char *wwn)
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	char		*devpath;
17397c478bd9Sstevel@tonic-gate 	path_t		*pp;
17407c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
17437c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
17447c478bd9Sstevel@tonic-gate 	    di_node_t pnode;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	    pnode = di_parent_node(node);
17477c478bd9Sstevel@tonic-gate 	    if (pnode != DI_NODE_NIL) {
17487c478bd9Sstevel@tonic-gate 		node = pnode;
17497c478bd9Sstevel@tonic-gate 	    }
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	/* check if the path is already there */
17557c478bd9Sstevel@tonic-gate 	pp = NULL;
17567c478bd9Sstevel@tonic-gate 	if (cp->paths != NULL) {
17577c478bd9Sstevel@tonic-gate 	    int i;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	    for (i = 0; cp->paths[i]; i++) {
17607c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
17617c478bd9Sstevel@tonic-gate 		    pp = cp->paths[i];
17627c478bd9Sstevel@tonic-gate 		    break;
17637c478bd9Sstevel@tonic-gate 		}
17647c478bd9Sstevel@tonic-gate 	    }
17657c478bd9Sstevel@tonic-gate 	}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
17687c478bd9Sstevel@tonic-gate 	    /* the path exists, add this disk to it */
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	    if (!add_disk2path(dp, pp, st, wwn)) {
17737c478bd9Sstevel@tonic-gate 		return (NULL);
17747c478bd9Sstevel@tonic-gate 	    }
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	    return (pp);
17777c478bd9Sstevel@tonic-gate 	}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/* create a new path */
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	pp = calloc(1, sizeof (path_t));
17827c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
17837c478bd9Sstevel@tonic-gate 	    di_devfs_path_free((void *) devpath);
17847c478bd9Sstevel@tonic-gate 	    return (NULL);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	pp->name = strdup(devpath);
17887c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
17897c478bd9Sstevel@tonic-gate 	if (pp->name == NULL) {
17907c478bd9Sstevel@tonic-gate 	    cache_free_path(pp);
17917c478bd9Sstevel@tonic-gate 	    return (NULL);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
17957c478bd9Sstevel@tonic-gate 	if (!add_disk2path(dp, pp, st, wwn)) {
17967c478bd9Sstevel@tonic-gate 	    return (NULL);
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/* add the path to the controller */
18007c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
18017c478bd9Sstevel@tonic-gate 	    cache_free_path(pp);
18027c478bd9Sstevel@tonic-gate 	    return (NULL);
18037c478bd9Sstevel@tonic-gate 	}
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	/* add the controller to the path */
18067c478bd9Sstevel@tonic-gate 	pp->controller = cp;
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	minor = di_minor_next(node, NULL);
18097c478bd9Sstevel@tonic-gate 	if (minor != NULL) {
18107c478bd9Sstevel@tonic-gate 	    pp->ctype = ctype(node, minor);
18117c478bd9Sstevel@tonic-gate 	} else {
18127c478bd9Sstevel@tonic-gate 	    pp->ctype = DM_CTYPE_UNKNOWN;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	return (pp);
18167c478bd9Sstevel@tonic-gate }
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate /*
18197c478bd9Sstevel@tonic-gate  * We pass in the current controller pointer (currp) so we can double check
18207c478bd9Sstevel@tonic-gate  * that we aren't corrupting the list by removing the element we are on.  This
18217c478bd9Sstevel@tonic-gate  * should never happen, but it doesn't hurt to double check.
18227c478bd9Sstevel@tonic-gate  */
18237c478bd9Sstevel@tonic-gate static void
18247c478bd9Sstevel@tonic-gate remove_invalid_controller(char *name, controller_t *currp,
18257c478bd9Sstevel@tonic-gate     struct search_args *args)
18267c478bd9Sstevel@tonic-gate {
18277c478bd9Sstevel@tonic-gate 	controller_t *cp;
18287c478bd9Sstevel@tonic-gate 	bus_t *bp;
18297c478bd9Sstevel@tonic-gate 	controller_t *prevp;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	bp = args->bus_listp;
18327c478bd9Sstevel@tonic-gate 	while (bp != NULL) {
18337c478bd9Sstevel@tonic-gate 	    int i;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	    for (i = 0; bp->controllers[i]; i++) {
18367c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
18377c478bd9Sstevel@tonic-gate 		    int j;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 		    /* remove pointer to invalid controller (it is a path) */
18407c478bd9Sstevel@tonic-gate 		    for (j = i; bp->controllers[j]; j++) {
18417c478bd9Sstevel@tonic-gate 			bp->controllers[j] = bp->controllers[j + 1];
18427c478bd9Sstevel@tonic-gate 		    }
18437c478bd9Sstevel@tonic-gate 		}
18447c478bd9Sstevel@tonic-gate 	    }
18457c478bd9Sstevel@tonic-gate 	    bp = bp->next;
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if (args->controller_listp == NULL) {
18497c478bd9Sstevel@tonic-gate 	    return;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
18537c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->name, name)) {
18547c478bd9Sstevel@tonic-gate 	    if (can_remove_controller(cp, currp)) {
18557c478bd9Sstevel@tonic-gate 		args->controller_listp = cp->next;
18567c478bd9Sstevel@tonic-gate 		cache_free_controller(cp);
18577c478bd9Sstevel@tonic-gate 	    }
18587c478bd9Sstevel@tonic-gate 	    return;
18597c478bd9Sstevel@tonic-gate 	}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	prevp = cp;
18627c478bd9Sstevel@tonic-gate 	cp = cp->next;
18637c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
18647c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(cp->name, name)) {
18657c478bd9Sstevel@tonic-gate 		if (can_remove_controller(cp, currp)) {
18667c478bd9Sstevel@tonic-gate 		    prevp->next = cp->next;
18677c478bd9Sstevel@tonic-gate 		    cache_free_controller(cp);
18687c478bd9Sstevel@tonic-gate 		}
18697c478bd9Sstevel@tonic-gate 		return;
18707c478bd9Sstevel@tonic-gate 	    }
18717c478bd9Sstevel@tonic-gate 	    prevp = cp;
18727c478bd9Sstevel@tonic-gate 	    cp = cp->next;
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate /*
18777c478bd9Sstevel@tonic-gate  * This is the standard strstr code modified for case independence.
18787c478bd9Sstevel@tonic-gate  */
18797c478bd9Sstevel@tonic-gate static char *
18807c478bd9Sstevel@tonic-gate str_case_index(register char *s1, register char *s2)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	uint_t s2len = strlen(s2); /* length of the second string */
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	/* If the length of the second string is 0, return the first arg. */
18857c478bd9Sstevel@tonic-gate 	if (s2len == 0) {
18867c478bd9Sstevel@tonic-gate 	    return (s1);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	while (strlen(s1) >= s2len) {
18907c478bd9Sstevel@tonic-gate 	    if (strncasecmp(s1, s2, s2len) == 0) {
18917c478bd9Sstevel@tonic-gate 		return (s1);
18927c478bd9Sstevel@tonic-gate 	    }
18937c478bd9Sstevel@tonic-gate 	    s1++;
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 	return (NULL);
18967c478bd9Sstevel@tonic-gate }
1897