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
518c2aff7Sartem  * Common Development and Distribution License (the "License").
618c2aff7Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2242f64fdbSSarah Jelinek  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
249729663dSGeorge Wilson  * Copyright (c) 2011 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
35681d9761SEric Taylor #include <sys/mkdev.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <devid.h>
40e7cbe64fSgw #include <sys/fs/zfs.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"
590167b58cScg #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 static char *ctrltypes[] = {
667c478bd9Sstevel@tonic-gate 	DDI_NT_SCSI_NEXUS,
677c478bd9Sstevel@tonic-gate 	DDI_NT_SCSI_ATTACHMENT_POINT,
687c478bd9Sstevel@tonic-gate 	DDI_NT_FC_ATTACHMENT_POINT,
697c478bd9Sstevel@tonic-gate 	NULL
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static char *bustypes[] = {
737c478bd9Sstevel@tonic-gate 	"sbus",
747c478bd9Sstevel@tonic-gate 	"pci",
757c478bd9Sstevel@tonic-gate 	"usb",
767c478bd9Sstevel@tonic-gate 	NULL
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate static bus_t		*add_bus(struct search_args *args, di_node_t node,
807c478bd9Sstevel@tonic-gate 			    di_minor_t minor, controller_t *cp);
817c478bd9Sstevel@tonic-gate static int		add_cluster_devs(di_node_t node, di_minor_t minor,
827c478bd9Sstevel@tonic-gate 			    void *arg);
837c478bd9Sstevel@tonic-gate static controller_t	*add_controller(struct search_args *args,
847c478bd9Sstevel@tonic-gate 			    di_node_t node, di_minor_t minor);
857c478bd9Sstevel@tonic-gate static int		add_devpath(di_devlink_t devlink, void *arg);
867c478bd9Sstevel@tonic-gate static int		add_devs(di_node_t node, di_minor_t minor, void *arg);
877c478bd9Sstevel@tonic-gate static int		add_disk2controller(disk_t *diskp,
887c478bd9Sstevel@tonic-gate 			    struct search_args *args);
897c478bd9Sstevel@tonic-gate static int		add_disk2path(disk_t *dp, path_t *pp,
907c478bd9Sstevel@tonic-gate 			    di_path_state_t st, char *wwn);
917c478bd9Sstevel@tonic-gate static int		add_int2array(int p, int **parray);
927c478bd9Sstevel@tonic-gate static int		add_ptr2array(void *p, void ***parray);
937c478bd9Sstevel@tonic-gate static char		*bus_type(di_node_t node, di_minor_t minor,
947c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
9542f64fdbSSarah Jelinek static void		remove_controller(controller_t *cp,
967c478bd9Sstevel@tonic-gate 			    controller_t *currp);
977c478bd9Sstevel@tonic-gate static void		clean_paths(struct search_args *args);
987c478bd9Sstevel@tonic-gate static disk_t		*create_disk(char *deviceid, char *kernel_name,
997c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1007c478bd9Sstevel@tonic-gate static char		*ctype(di_node_t node, di_minor_t minor);
10197ab9a43SJohn Levon static boolean_t	disk_is_cdrom(const char *type);
1027c478bd9Sstevel@tonic-gate static alias_t		*find_alias(disk_t *diskp, char *kernel_name);
1037c478bd9Sstevel@tonic-gate static bus_t		*find_bus(struct search_args *args, char *name);
1047c478bd9Sstevel@tonic-gate static controller_t	*find_controller(struct search_args *args, char *name);
1057c478bd9Sstevel@tonic-gate static int		fix_cluster_devpath(di_devlink_t devlink, void *arg);
1067c478bd9Sstevel@tonic-gate static disk_t		*get_disk_by_deviceid(disk_t *listp, char *devid);
1077c478bd9Sstevel@tonic-gate static void		get_disk_name_from_path(char *path, char *name,
1087c478bd9Sstevel@tonic-gate 			    int size);
1097c478bd9Sstevel@tonic-gate static char		*get_byte_prop(char *prop_name, di_node_t node);
1107c478bd9Sstevel@tonic-gate static di_node_t	get_parent_bus(di_node_t node,
1117c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1127c478bd9Sstevel@tonic-gate static int		get_prom_int(char *prop_name, di_node_t node,
1137c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1147c478bd9Sstevel@tonic-gate static char		*get_prom_str(char *prop_name, di_node_t node,
1157c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1167c478bd9Sstevel@tonic-gate static int		get_prop(char *prop_name, di_node_t node);
1177c478bd9Sstevel@tonic-gate static char		*get_str_prop(char *prop_name, di_node_t node);
1187c478bd9Sstevel@tonic-gate static int		have_disk(struct search_args *args, char *devid,
1197c478bd9Sstevel@tonic-gate 			    char *kernel_name, disk_t **diskp);
1207c478bd9Sstevel@tonic-gate static int		is_cluster_disk(di_node_t node, di_minor_t minor);
1217c478bd9Sstevel@tonic-gate static int		is_ctds(char *name);
1227c478bd9Sstevel@tonic-gate static int		is_drive(di_minor_t minor);
123e7cbe64fSgw static int		is_zvol(di_node_t node, di_minor_t minor);
1247c478bd9Sstevel@tonic-gate static int		is_HBA(di_node_t node, di_minor_t minor);
1257c478bd9Sstevel@tonic-gate static int		new_alias(disk_t *diskp, char *kernel_path,
1267c478bd9Sstevel@tonic-gate 			    char *devlink_path, struct search_args *args);
1277c478bd9Sstevel@tonic-gate static int		new_devpath(alias_t *ap, char *devpath);
1287c478bd9Sstevel@tonic-gate static path_t		*new_path(controller_t *cp, disk_t *diskp,
1297c478bd9Sstevel@tonic-gate 			    di_node_t node, di_path_state_t st, char *wwn);
1307c478bd9Sstevel@tonic-gate static void		remove_invalid_controller(char *name,
1317c478bd9Sstevel@tonic-gate 			    controller_t *currp, struct search_args *args);
1327c478bd9Sstevel@tonic-gate static char		*str_case_index(register char *s1, register char *s2);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * The functions in this file do a dev tree walk to build up a model of the
1367c478bd9Sstevel@tonic-gate  * disks, controllers and paths on the system.  This model is returned in the
1377c478bd9Sstevel@tonic-gate  * args->disk_listp and args->controller_listp members of the args param.
1387c478bd9Sstevel@tonic-gate  * There is no global data for this file so it is thread safe.  It is up to
1397c478bd9Sstevel@tonic-gate  * the caller to merge the resulting model with any existing model that is
1407c478bd9Sstevel@tonic-gate  * cached.  The caller must also free the memory for this model when it is
1417c478bd9Sstevel@tonic-gate  * no longer needed.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate void
1447c478bd9Sstevel@tonic-gate findevs(struct search_args *args)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	uint_t			flags;
1477c478bd9Sstevel@tonic-gate 	di_node_t		di_root;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	args->dev_walk_status = 0;
1507c478bd9Sstevel@tonic-gate 	args->disk_listp = NULL;
1517c478bd9Sstevel@tonic-gate 	args->controller_listp = NULL;
1527c478bd9Sstevel@tonic-gate 	args->bus_listp = NULL;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	args->handle = di_devlink_init(NULL, 0);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * Have to make several passes at this with the new devfs caching.
1587c478bd9Sstevel@tonic-gate 	 * First, we find non-mpxio devices. Then we find mpxio/multipath
1597c478bd9Sstevel@tonic-gate 	 * devices. Finally, we get cluster devices.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	flags = DINFOCACHE;
1627c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1637c478bd9Sstevel@tonic-gate 	args->ph = di_prom_init();
1647c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1657c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	flags = DINFOCPYALL | DINFOPATH;
1687c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1697c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1707c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/* do another pass to clean up cluster devpaths */
1737c478bd9Sstevel@tonic-gate 	flags = DINFOCACHE;
1747c478bd9Sstevel@tonic-gate 	di_root = di_init("/", flags);
1757c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, DDI_PSEUDO, 0, args, add_cluster_devs);
1767c478bd9Sstevel@tonic-gate 	if (args->ph != DI_PROM_HANDLE_NIL) {
17742f64fdbSSarah Jelinek 		(void) di_prom_fini(args->ph);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	(void) di_devlink_fini(&(args->handle));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	clean_paths(args);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Definitions of private functions
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate static bus_t *
1917c478bd9Sstevel@tonic-gate add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
1927c478bd9Sstevel@tonic-gate 	controller_t *cp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	char		*btype;
1957c478bd9Sstevel@tonic-gate 	char		*devpath;
1967c478bd9Sstevel@tonic-gate 	bus_t		*bp;
1977c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
1987c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
20142f64fdbSSarah Jelinek 		return (NULL);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if ((btype = bus_type(node, minor, args->ph)) == NULL) {
20542f64fdbSSarah Jelinek 		return (add_bus(args, di_parent_node(node),
20642f64fdbSSarah Jelinek 		    di_minor_next(di_parent_node(node), NULL), cp));
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if ((bp = find_bus(args, devpath)) != NULL) {
21242f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
2137c478bd9Sstevel@tonic-gate 
21442f64fdbSSarah Jelinek 		if (cp != NULL) {
21542f64fdbSSarah Jelinek 			if (add_ptr2array(cp,
21642f64fdbSSarah Jelinek 			    (void ***)&bp->controllers) != 0) {
21742f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
21842f64fdbSSarah Jelinek 				return (NULL);
21942f64fdbSSarah Jelinek 			}
2207c478bd9Sstevel@tonic-gate 		}
22142f64fdbSSarah Jelinek 		return (bp);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* Special handling for root node. */
2257c478bd9Sstevel@tonic-gate 	if (strcmp(devpath, "/") == 0) {
22642f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
22742f64fdbSSarah Jelinek 		return (NULL);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (dm_debug) {
23142f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	bp = (bus_t *)calloc(1, sizeof (bus_t));
2357c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
23642f64fdbSSarah Jelinek 		return (NULL);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	bp->name = strdup(devpath);
2407c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
2417c478bd9Sstevel@tonic-gate 	if (bp->name == NULL) {
24242f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
24342f64fdbSSarah Jelinek 		cache_free_bus(bp);
24442f64fdbSSarah Jelinek 		return (NULL);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	bp->btype = strdup(btype);
2487c478bd9Sstevel@tonic-gate 	if (bp->btype == NULL) {
24942f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
25042f64fdbSSarah Jelinek 		cache_free_bus(bp);
25142f64fdbSSarah Jelinek 		return (NULL);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
2557c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
25842f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
25942f64fdbSSarah Jelinek 		cache_free_bus(bp);
26042f64fdbSSarah Jelinek 		return (NULL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* if parent node is a bus, get its name */
2647c478bd9Sstevel@tonic-gate 	if ((pnode = get_parent_bus(node, args)) != NULL) {
26542f64fdbSSarah Jelinek 		devpath = di_devfs_path(pnode);
26642f64fdbSSarah Jelinek 		bp->pname = strdup(devpath);
26742f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
26842f64fdbSSarah Jelinek 		if (bp->pname == NULL) {
26942f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
27042f64fdbSSarah Jelinek 			cache_free_bus(bp);
27142f64fdbSSarah Jelinek 			return (NULL);
27242f64fdbSSarah Jelinek 		}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	} else {
27542f64fdbSSarah Jelinek 		bp->pname = NULL;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	bp->freq = get_prom_int("clock-frequency", node, args->ph);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
2817c478bd9Sstevel@tonic-gate 	if (bp->controllers == NULL) {
28242f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
28342f64fdbSSarah Jelinek 		cache_free_bus(bp);
28442f64fdbSSarah Jelinek 		return (NULL);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	bp->controllers[0] = NULL;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
28942f64fdbSSarah Jelinek 		if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
29042f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
29142f64fdbSSarah Jelinek 			return (NULL);
29242f64fdbSSarah Jelinek 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	bp->next = args->bus_listp;
2967c478bd9Sstevel@tonic-gate 	args->bus_listp = bp;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (bp);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int
3027c478bd9Sstevel@tonic-gate add_cluster_devs(di_node_t node, di_minor_t minor, void *arg)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct search_args	*args;
3057c478bd9Sstevel@tonic-gate 	char			*devpath;
3067c478bd9Sstevel@tonic-gate 	char			slice_path[MAXPATHLEN];
3077c478bd9Sstevel@tonic-gate 	int			result = DI_WALK_CONTINUE;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (!is_cluster_disk(node, minor)) {
31042f64fdbSSarah Jelinek 		return (DI_WALK_CONTINUE);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
31642f64fdbSSarah Jelinek 		/* This is all just debugging code */
31742f64fdbSSarah Jelinek 		char	*devpath;
31842f64fdbSSarah Jelinek 		char	dev_name[MAXPATHLEN];
3197c478bd9Sstevel@tonic-gate 
32042f64fdbSSarah Jelinek 		devpath = di_devfs_path(node);
32142f64fdbSSarah Jelinek 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
32242f64fdbSSarah Jelinek 		    di_minor_name(minor));
32342f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
3247c478bd9Sstevel@tonic-gate 
32542f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: cluster dev: %s\n", dev_name);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	args->node = node;
3297c478bd9Sstevel@tonic-gate 	args->minor = minor;
3307c478bd9Sstevel@tonic-gate 	args->dev_walk_status = 0;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Fix the devpaths for the cluster drive.
3347c478bd9Sstevel@tonic-gate 	 *
3357c478bd9Sstevel@tonic-gate 	 * We will come through here once for each raw slice device name.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
3387c478bd9Sstevel@tonic-gate 	(void) snprintf(slice_path, sizeof (slice_path), "%s:%s", devpath,
3397c478bd9Sstevel@tonic-gate 	    di_minor_name(minor));
3407c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/* Walk the /dev tree to get the cluster devlinks. */
3437c478bd9Sstevel@tonic-gate 	(void) di_devlink_walk(args->handle, DEVLINK_DID_REGEX, slice_path,
3447c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, arg, fix_cluster_devpath);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (args->dev_walk_status != 0) {
34742f64fdbSSarah Jelinek 		result = DI_WALK_TERMINATE;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	return (result);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static controller_t *
3547c478bd9Sstevel@tonic-gate add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	char		*devpath;
3577c478bd9Sstevel@tonic-gate 	controller_t	*cp;
3587c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
3597c478bd9Sstevel@tonic-gate 	char		*c_type = DM_CTYPE_UNKNOWN;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if ((cp = find_controller(args, devpath)) != NULL) {
36442f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
36542f64fdbSSarah Jelinek 		return (cp);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
3697c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
37042f64fdbSSarah Jelinek 		di_node_t pnode;
3717c478bd9Sstevel@tonic-gate 
37242f64fdbSSarah Jelinek 		pnode = di_parent_node(node);
37342f64fdbSSarah Jelinek 		if (pnode != DI_NODE_NIL) {
37442f64fdbSSarah Jelinek 			di_devfs_path_free((void *) devpath);
37542f64fdbSSarah Jelinek 			devpath = di_devfs_path(pnode);
3767c478bd9Sstevel@tonic-gate 
37742f64fdbSSarah Jelinek 			if ((cp = find_controller(args, devpath)) != NULL) {
37842f64fdbSSarah Jelinek 				di_devfs_path_free((void *) devpath);
37942f64fdbSSarah Jelinek 				return (cp);
38042f64fdbSSarah Jelinek 			}
3817c478bd9Sstevel@tonic-gate 
38242f64fdbSSarah Jelinek 			/* not in the list, create it */
38342f64fdbSSarah Jelinek 			node = pnode;
38442f64fdbSSarah Jelinek 			c_type = DM_CTYPE_FIBRE;
38542f64fdbSSarah Jelinek 		}
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (dm_debug) {
38942f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	cp = (controller_t *)calloc(1, sizeof (controller_t));
3937c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
39442f64fdbSSarah Jelinek 		return (NULL);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	cp->name = strdup(devpath);
3987c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
3997c478bd9Sstevel@tonic-gate 	if (cp->name == NULL) {
40042f64fdbSSarah Jelinek 		cache_free_controller(cp);
40142f64fdbSSarah Jelinek 		return (NULL);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
40542f64fdbSSarah Jelinek 		c_type = ctype(node, minor);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	cp->ctype = c_type;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
4107c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
41342f64fdbSSarah Jelinek 		cache_free_controller(cp);
41442f64fdbSSarah Jelinek 		return (NULL);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
41842f64fdbSSarah Jelinek 		cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
42242f64fdbSSarah Jelinek 		cp->multiplex = 1;
4237c478bd9Sstevel@tonic-gate 	} else {
42442f64fdbSSarah Jelinek 		cp->multiplex = 0;
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	cp->freq = get_prom_int("clock-frequency", node, args->ph);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
4307c478bd9Sstevel@tonic-gate 	if (cp->disks == NULL) {
43142f64fdbSSarah Jelinek 		cache_free_controller(cp);
43242f64fdbSSarah Jelinek 		return (NULL);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 	cp->disks[0] = NULL;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	cp->next = args->controller_listp;
4377c478bd9Sstevel@tonic-gate 	args->controller_listp = cp;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	cp->bus = add_bus(args, di_parent_node(node),
4407c478bd9Sstevel@tonic-gate 	    di_minor_next(di_parent_node(node), NULL), cp);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return (cp);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static int
4467c478bd9Sstevel@tonic-gate add_devpath(di_devlink_t devlink, void *arg)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	struct search_args *args;
4497c478bd9Sstevel@tonic-gate 	char		*devidstr;
4507c478bd9Sstevel@tonic-gate 	disk_t		*diskp;
4517c478bd9Sstevel@tonic-gate 	char		kernel_name[MAXPATHLEN];
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * Get the diskp value from calling have_disk. Can either be found
4577c478bd9Sstevel@tonic-gate 	 * by kernel name or devid.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	diskp = NULL;
4617c478bd9Sstevel@tonic-gate 	devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
4627c478bd9Sstevel@tonic-gate 	(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
4637c478bd9Sstevel@tonic-gate 	    di_node_name(args->node), di_instance(args->node));
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	(void) have_disk(args, devidstr, kernel_name, &diskp);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
4697c478bd9Sstevel@tonic-gate 	 * For diskettes it is /dev/rdiskette*.
4707c478bd9Sstevel@tonic-gate 	 * On Intel we would also get each fdisk partition as well
4717c478bd9Sstevel@tonic-gate 	 * (e.g. /dev/rdsk/c0t0d0p0).
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
47442f64fdbSSarah Jelinek 		alias_t	*ap;
47542f64fdbSSarah Jelinek 		char	*devlink_path;
4767c478bd9Sstevel@tonic-gate 
47742f64fdbSSarah Jelinek 		if (diskp->drv_type != DM_DT_FLOPPY) {
47842f64fdbSSarah Jelinek 			/*
47942f64fdbSSarah Jelinek 			 * Add other controllers for multipath disks.
48042f64fdbSSarah Jelinek 			 * This will have no effect if the controller
48142f64fdbSSarah Jelinek 			 * relationship is already set up.
48242f64fdbSSarah Jelinek 			 */
48342f64fdbSSarah Jelinek 			if (add_disk2controller(diskp, args) != 0) {
48442f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
48542f64fdbSSarah Jelinek 			}
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 
48842f64fdbSSarah Jelinek 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
48942f64fdbSSarah Jelinek 		    di_node_name(args->node), di_instance(args->node));
49042f64fdbSSarah Jelinek 		devlink_path = (char *)di_devlink_path(devlink);
4917c478bd9Sstevel@tonic-gate 
49242f64fdbSSarah Jelinek 		if (dm_debug > 1) {
49342f64fdbSSarah Jelinek 			(void) fprintf(stderr,
49442f64fdbSSarah Jelinek 			    "INFO:     devpath %s\n", devlink_path);
4957c478bd9Sstevel@tonic-gate 		}
49642f64fdbSSarah Jelinek 
49742f64fdbSSarah Jelinek 		if ((ap = find_alias(diskp, kernel_name)) == NULL) {
49842f64fdbSSarah Jelinek 			if (new_alias(diskp, kernel_name, devlink_path,
49942f64fdbSSarah Jelinek 			    args) != 0) {
50042f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
50142f64fdbSSarah Jelinek 			}
50242f64fdbSSarah Jelinek 		} else {
50342f64fdbSSarah Jelinek 			/*
50442f64fdbSSarah Jelinek 			 * It is possible that we have already added this
50542f64fdbSSarah Jelinek 			 * devpath.  Do not add it again. new_devpath will
50642f64fdbSSarah Jelinek 			 * return a 0 if found, and not add the path.
50742f64fdbSSarah Jelinek 			 */
50842f64fdbSSarah Jelinek 			if (new_devpath(ap, devlink_path) != 0) {
50942f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
51042f64fdbSSarah Jelinek 			}
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate static int
5187c478bd9Sstevel@tonic-gate add_devs(di_node_t node, di_minor_t minor, void *arg)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	struct search_args	*args;
5217c478bd9Sstevel@tonic-gate 	int result = DI_WALK_CONTINUE;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
52642f64fdbSSarah Jelinek 		/* This is all just debugging code */
52742f64fdbSSarah Jelinek 		char	*devpath;
52842f64fdbSSarah Jelinek 		char	dev_name[MAXPATHLEN];
52942f64fdbSSarah Jelinek 
53042f64fdbSSarah Jelinek 		devpath = di_devfs_path(node);
53142f64fdbSSarah Jelinek 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
53242f64fdbSSarah Jelinek 		    di_minor_name(minor));
53342f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
53442f64fdbSSarah Jelinek 
53542f64fdbSSarah Jelinek 		(void) fprintf(stderr,
53642f64fdbSSarah Jelinek 		    "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
53742f64fdbSSarah Jelinek 		    dev_name, di_node_name(node), di_instance(node),
53842f64fdbSSarah Jelinek 		    di_minor_spectype(minor),
53942f64fdbSSarah Jelinek 		    (di_minor_nodetype(minor) != NULL ?
5407c478bd9Sstevel@tonic-gate 		    di_minor_nodetype(minor) : "NULL"));
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (bus_type(node, minor, args->ph) != NULL) {
54442f64fdbSSarah Jelinek 		if (add_bus(args, node, minor, NULL) == NULL) {
54542f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
54642f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
54742f64fdbSSarah Jelinek 		}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	} else if (is_HBA(node, minor)) {
55042f64fdbSSarah Jelinek 		if (add_controller(args, node, minor) == NULL) {
55142f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
55242f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
55342f64fdbSSarah Jelinek 		}
5547c478bd9Sstevel@tonic-gate 
555e7cbe64fSgw 	} else if (di_minor_spectype(minor) == S_IFCHR &&
55642f64fdbSSarah Jelinek 	    (is_drive(minor) || is_zvol(node, minor))) {
55742f64fdbSSarah Jelinek 		char	*devidstr;
55842f64fdbSSarah Jelinek 		char	kernel_name[MAXPATHLEN];
55942f64fdbSSarah Jelinek 		disk_t	*diskp;
5607c478bd9Sstevel@tonic-gate 
56142f64fdbSSarah Jelinek 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
56242f64fdbSSarah Jelinek 		    di_node_name(node), di_instance(node));
56342f64fdbSSarah Jelinek 		devidstr = get_str_prop(DEVICE_ID_PROP, node);
56442f64fdbSSarah Jelinek 
56542f64fdbSSarah Jelinek 		args->node = node;
56642f64fdbSSarah Jelinek 		args->minor = minor;
56742f64fdbSSarah Jelinek 		/*
56842f64fdbSSarah Jelinek 		 * Check if we already got this disk and
56942f64fdbSSarah Jelinek 		 * this is another slice.
57042f64fdbSSarah Jelinek 		 */
57142f64fdbSSarah Jelinek 		if (!have_disk(args, devidstr, kernel_name, &diskp)) {
57242f64fdbSSarah Jelinek 			args->dev_walk_status = 0;
57342f64fdbSSarah Jelinek 			/*
57442f64fdbSSarah Jelinek 			 * This is a newly found disk, create the
57542f64fdbSSarah Jelinek 			 * disk structure.
57642f64fdbSSarah Jelinek 			 */
57742f64fdbSSarah Jelinek 			diskp = create_disk(devidstr, kernel_name, args);
57842f64fdbSSarah Jelinek 			if (diskp == NULL) {
57942f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
58042f64fdbSSarah Jelinek 			}
58142f64fdbSSarah Jelinek 
58242f64fdbSSarah Jelinek 			if (diskp->drv_type != DM_DT_FLOPPY) {
58342f64fdbSSarah Jelinek 				/* add the controller relationship */
58442f64fdbSSarah Jelinek 				if (args->dev_walk_status == 0) {
58542f64fdbSSarah Jelinek 					if (add_disk2controller(diskp,
58642f64fdbSSarah Jelinek 					    args) != 0) {
58742f64fdbSSarah Jelinek 						args->dev_walk_status = ENOMEM;
58842f64fdbSSarah Jelinek 					}
58942f64fdbSSarah Jelinek 				}
5907c478bd9Sstevel@tonic-gate 			}
5917c478bd9Sstevel@tonic-gate 		}
59242f64fdbSSarah Jelinek 		if (is_zvol(node, minor)) {
59342f64fdbSSarah Jelinek 			char zvdsk[MAXNAMELEN];
59442f64fdbSSarah Jelinek 			char *str;
59542f64fdbSSarah Jelinek 			alias_t *ap;
59642f64fdbSSarah Jelinek 
59742f64fdbSSarah Jelinek 			if (di_prop_lookup_strings(di_minor_devt(minor),
59842f64fdbSSarah Jelinek 			    node, "name", &str) == -1)
59942f64fdbSSarah Jelinek 				return (DI_WALK_CONTINUE);
60042f64fdbSSarah Jelinek 			(void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s",
60142f64fdbSSarah Jelinek 			    str);
60242f64fdbSSarah Jelinek 			if ((ap = find_alias(diskp, kernel_name)) == NULL) {
60342f64fdbSSarah Jelinek 				if (new_alias(diskp, kernel_name,
60442f64fdbSSarah Jelinek 				    zvdsk, args) != 0) {
60542f64fdbSSarah Jelinek 					args->dev_walk_status = ENOMEM;
60642f64fdbSSarah Jelinek 				}
60742f64fdbSSarah Jelinek 			} else {
60842f64fdbSSarah Jelinek 				/*
60942f64fdbSSarah Jelinek 				 * It is possible that we have already added
61042f64fdbSSarah Jelinek 				 * this devpath.
61142f64fdbSSarah Jelinek 				 * Do not add it again. new_devpath will
61242f64fdbSSarah Jelinek 				 * return a 0 if found, and not add the path.
61342f64fdbSSarah Jelinek 				 */
61442f64fdbSSarah Jelinek 				if (new_devpath(ap, zvdsk) != 0) {
61542f64fdbSSarah Jelinek 					args->dev_walk_status = ENOMEM;
61642f64fdbSSarah Jelinek 				}
61742f64fdbSSarah Jelinek 			}
618681d9761SEric Taylor 		}
6197c478bd9Sstevel@tonic-gate 
62042f64fdbSSarah Jelinek 		/* Add the devpaths for the drive. */
62142f64fdbSSarah Jelinek 		if (args->dev_walk_status == 0) {
62242f64fdbSSarah Jelinek 			char	*devpath;
62342f64fdbSSarah Jelinek 			char	slice_path[MAXPATHLEN];
62442f64fdbSSarah Jelinek 			char	*pattern;
6257c478bd9Sstevel@tonic-gate 
62642f64fdbSSarah Jelinek 			/*
62742f64fdbSSarah Jelinek 			 * We will come through here once for each of
62842f64fdbSSarah Jelinek 			 * the raw slice device names.
62942f64fdbSSarah Jelinek 			 */
63042f64fdbSSarah Jelinek 			devpath = di_devfs_path(node);
63142f64fdbSSarah Jelinek 			(void) snprintf(slice_path,
63242f64fdbSSarah Jelinek 			    sizeof (slice_path), "%s:%s",
63342f64fdbSSarah Jelinek 			    devpath, di_minor_name(minor));
63442f64fdbSSarah Jelinek 			di_devfs_path_free((void *) devpath);
63542f64fdbSSarah Jelinek 
63642f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(di_minor_nodetype(minor),
63742f64fdbSSarah Jelinek 			    DDI_NT_FD)) {
63842f64fdbSSarah Jelinek 				pattern = DEVLINK_FLOPPY_REGEX;
63942f64fdbSSarah Jelinek 			} else {
64042f64fdbSSarah Jelinek 				pattern = DEVLINK_REGEX;
64142f64fdbSSarah Jelinek 			}
6427c478bd9Sstevel@tonic-gate 
64342f64fdbSSarah Jelinek 			/* Walk the /dev tree to get the devlinks. */
64442f64fdbSSarah Jelinek 			(void) di_devlink_walk(args->handle, pattern,
64542f64fdbSSarah Jelinek 			    slice_path, DI_PRIMARY_LINK, arg, add_devpath);
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 
64842f64fdbSSarah Jelinek 		if (args->dev_walk_status != 0) {
64942f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
65042f64fdbSSarah Jelinek 		}
6517c478bd9Sstevel@tonic-gate 	}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	return (result);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate static int
6577c478bd9Sstevel@tonic-gate add_disk2controller(disk_t *diskp, struct search_args *args)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
6607c478bd9Sstevel@tonic-gate 	controller_t	*cp;
6617c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
6627c478bd9Sstevel@tonic-gate 	di_node_t	node;
6637c478bd9Sstevel@tonic-gate 	int		i;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	node = args->node;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
6687c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
66942f64fdbSSarah Jelinek 		return (0);
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	minor = di_minor_next(pnode, NULL);
6737c478bd9Sstevel@tonic-gate 	if (minor == NULL) {
67442f64fdbSSarah Jelinek 		return (0);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if ((cp = add_controller(args, pnode, minor)) == NULL) {
67842f64fdbSSarah Jelinek 		return (ENOMEM);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/* check if the disk <-> ctrl assoc is already there */
6827c478bd9Sstevel@tonic-gate 	for (i = 0; diskp->controllers[i]; i++) {
68342f64fdbSSarah Jelinek 		if (cp == diskp->controllers[i]) {
68442f64fdbSSarah Jelinek 			return (0);
68542f64fdbSSarah Jelinek 		}
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* this is a new controller for this disk */
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/* add the disk to the controlller */
6917c478bd9Sstevel@tonic-gate 	if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
69242f64fdbSSarah Jelinek 		return (ENOMEM);
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	/* add the controlller to the disk */
6967c478bd9Sstevel@tonic-gate 	if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
69742f64fdbSSarah Jelinek 		return (ENOMEM);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Set up paths for mpxio controlled drives.
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
70442f64fdbSSarah Jelinek 		/* note: mpxio di_path stuff is all consolidation private */
70542f64fdbSSarah Jelinek 		di_path_t   pi = DI_PATH_NIL;
70642f64fdbSSarah Jelinek 
70742f64fdbSSarah Jelinek 		while (
70842f64fdbSSarah Jelinek 		    (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
70942f64fdbSSarah Jelinek 			int	cnt;
71042f64fdbSSarah Jelinek 			uchar_t	*bytes;
71142f64fdbSSarah Jelinek 			char	str[MAXPATHLEN];
71242f64fdbSSarah Jelinek 			char	*wwn;
71342f64fdbSSarah Jelinek 
71442f64fdbSSarah Jelinek 			di_node_t phci_node = di_path_phci_node(pi);
71542f64fdbSSarah Jelinek 
71642f64fdbSSarah Jelinek 			/* get the node wwn */
71742f64fdbSSarah Jelinek 			cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
71842f64fdbSSarah Jelinek 			wwn = NULL;
71942f64fdbSSarah Jelinek 			if (cnt > 0) {
72042f64fdbSSarah Jelinek 				int	i;
72142f64fdbSSarah Jelinek 				str[0] = 0;
72242f64fdbSSarah Jelinek 
72342f64fdbSSarah Jelinek 				for (i = 0; i < cnt; i++) {
72442f64fdbSSarah Jelinek 					/*
72542f64fdbSSarah Jelinek 					 * A byte is only 2 hex chars + null.
72642f64fdbSSarah Jelinek 					 */
72742f64fdbSSarah Jelinek 					char bstr[8];
72842f64fdbSSarah Jelinek 
72942f64fdbSSarah Jelinek 					(void) snprintf(bstr,
73042f64fdbSSarah Jelinek 					    sizeof (bstr), "%.2x", bytes[i]);
73142f64fdbSSarah Jelinek 					(void) strlcat(str, bstr, sizeof (str));
73242f64fdbSSarah Jelinek 				}
73342f64fdbSSarah Jelinek 				wwn = str;
73442f64fdbSSarah Jelinek 			}
7357c478bd9Sstevel@tonic-gate 
73642f64fdbSSarah Jelinek 			if (new_path(cp, diskp, phci_node,
73742f64fdbSSarah Jelinek 			    di_path_state(pi), wwn) == NULL) {
73842f64fdbSSarah Jelinek 				return (ENOMEM);
73942f64fdbSSarah Jelinek 			}
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	return (0);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate static int
7477c478bd9Sstevel@tonic-gate add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
7507c478bd9Sstevel@tonic-gate 	if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
75142f64fdbSSarah Jelinek 		cache_free_path(pp);
75242f64fdbSSarah Jelinek 		return (0);
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/* add the path to the disk */
7567c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
75742f64fdbSSarah Jelinek 		cache_free_path(pp);
75842f64fdbSSarah Jelinek 		return (0);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7627c478bd9Sstevel@tonic-gate 	if (add_int2array(st, &pp->states) != 0) {
76342f64fdbSSarah Jelinek 		cache_free_path(pp);
76442f64fdbSSarah Jelinek 		return (0);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7687c478bd9Sstevel@tonic-gate 	if (wwn != NULL) {
76942f64fdbSSarah Jelinek 		char	*wp;
7707c478bd9Sstevel@tonic-gate 
77142f64fdbSSarah Jelinek 		if ((wp = strdup(wwn)) != NULL) {
77242f64fdbSSarah Jelinek 			if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
77342f64fdbSSarah Jelinek 				cache_free_path(pp);
77442f64fdbSSarah Jelinek 				return (0);
77542f64fdbSSarah Jelinek 			}
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	return (1);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate static int
7837c478bd9Sstevel@tonic-gate add_int2array(int p, int **parray)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	int		i;
7867c478bd9Sstevel@tonic-gate 	int		cnt;
7877c478bd9Sstevel@tonic-gate 	int		*pa;
7887c478bd9Sstevel@tonic-gate 	int		*new_array;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	pa = *parray;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	cnt = 0;
7937c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
79442f64fdbSSarah Jelinek 		for (; pa[cnt] != -1; cnt++)
79542f64fdbSSarah Jelinek 			;
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	new_array = (int *)calloc(cnt + 2, sizeof (int *));
7997c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
80042f64fdbSSarah Jelinek 		return (ENOMEM);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/* copy the existing array */
8047c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
80542f64fdbSSarah Jelinek 		new_array[i] = pa[i];
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	new_array[i] = p;
8097c478bd9Sstevel@tonic-gate 	new_array[i + 1] = -1;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	free(pa);
8127c478bd9Sstevel@tonic-gate 	*parray = new_array;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	return (0);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate static int
8187c478bd9Sstevel@tonic-gate add_ptr2array(void *p, void ***parray)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	int		i;
8217c478bd9Sstevel@tonic-gate 	int		cnt;
8227c478bd9Sstevel@tonic-gate 	void		**pa;
8237c478bd9Sstevel@tonic-gate 	void		**new_array;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	pa = *parray;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	cnt = 0;
8287c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
82942f64fdbSSarah Jelinek 		for (; pa[cnt]; cnt++)
83042f64fdbSSarah Jelinek 			;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	new_array = (void **)calloc(cnt + 2, sizeof (void *));
8347c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
83542f64fdbSSarah Jelinek 		return (ENOMEM);
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/* copy the existing array */
8397c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
84042f64fdbSSarah Jelinek 		new_array[i] = pa[i];
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	new_array[i] = p;
8447c478bd9Sstevel@tonic-gate 	new_array[i + 1] = NULL;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	free(pa);
8477c478bd9Sstevel@tonic-gate 	*parray = new_array;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	return (0);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
85342f64fdbSSarah Jelinek  * This function checks to see if a controller has other associations
85442f64fdbSSarah Jelinek  * that may be valid. If we are calling this function, we have found that
85542f64fdbSSarah Jelinek  * a controller for an mpxio device is showing up independently of the
85642f64fdbSSarah Jelinek  * mpxio controller, noted as /scsi_vhci. This can happen with some FC
85742f64fdbSSarah Jelinek  * cards that have inbound management devices that show up as well, with
85842f64fdbSSarah Jelinek  * the real controller data associated. We do not want to display these
85942f64fdbSSarah Jelinek  * 'devices' as real devices in libdiskmgt.
8607c478bd9Sstevel@tonic-gate  */
86142f64fdbSSarah Jelinek static void
86242f64fdbSSarah Jelinek remove_controller(controller_t *cp, controller_t *currp)
8637c478bd9Sstevel@tonic-gate {
86442f64fdbSSarah Jelinek 	int	i;
8657c478bd9Sstevel@tonic-gate 
86642f64fdbSSarah Jelinek 	if (cp == currp) {
86742f64fdbSSarah Jelinek 		if (dm_debug) {
86842f64fdbSSarah Jelinek 			(void) fprintf(stderr, "ERROR: removing current"
86942f64fdbSSarah Jelinek 			    " controller\n");
87042f64fdbSSarah Jelinek 		}
87142f64fdbSSarah Jelinek 		return;
87242f64fdbSSarah Jelinek 	}
8737c478bd9Sstevel@tonic-gate 
87442f64fdbSSarah Jelinek 	if (cp->disks != NULL && cp->disks[0] != NULL) {
87542f64fdbSSarah Jelinek 		if (dm_debug) {
87642f64fdbSSarah Jelinek 			(void) fprintf(stderr,
87742f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
87842f64fdbSSarah Jelinek 			    " with disk ptrs.\n");
87942f64fdbSSarah Jelinek 		}
88042f64fdbSSarah Jelinek 		/*
88142f64fdbSSarah Jelinek 		 * loop through the disks and remove the reference to the
88242f64fdbSSarah Jelinek 		 * controller for this disk structure. The disk itself
88342f64fdbSSarah Jelinek 		 * is still a valid device, the controller being removed
88442f64fdbSSarah Jelinek 		 * is a 'path' so any disk that has a reference to it
88542f64fdbSSarah Jelinek 		 * as a controller needs to have this reference removed.
88642f64fdbSSarah Jelinek 		 */
8879729663dSGeorge Wilson 		for (i = 0; cp->disks[i]; i++) {
8889729663dSGeorge Wilson 			disk_t *dp = cp->disks[i];
8899729663dSGeorge Wilson 			int j;
8909729663dSGeorge Wilson 
8919729663dSGeorge Wilson 			for (j = 0; dp->controllers[j]; j++) {
8929729663dSGeorge Wilson 				int k;
8939729663dSGeorge Wilson 
8949729663dSGeorge Wilson 				if (libdiskmgt_str_eq(dp->controllers[j]->name,
89542f64fdbSSarah Jelinek 				    cp->name)) {
89642f64fdbSSarah Jelinek 
8979729663dSGeorge Wilson 					if (dm_debug) {
8989729663dSGeorge Wilson 						(void) fprintf(stderr,
8999729663dSGeorge Wilson 						    "INFO: REMOVING disk %s on "
9009729663dSGeorge Wilson 						    "controller %s\n",
9019729663dSGeorge Wilson 						    dp->kernel_name, cp->name);
9029729663dSGeorge Wilson 					}
9039729663dSGeorge Wilson 					for (k = j; dp->controllers[k]; k++) {
9049729663dSGeorge Wilson 						dp->controllers[k] =
9059729663dSGeorge Wilson 						    dp->controllers[k + 1];
90642f64fdbSSarah Jelinek 					}
90742f64fdbSSarah Jelinek 				}
90842f64fdbSSarah Jelinek 			}
90942f64fdbSSarah Jelinek 		}
9107c478bd9Sstevel@tonic-gate 	}
91142f64fdbSSarah Jelinek 	/*
91242f64fdbSSarah Jelinek 	 * Paths are removed with the call to cache_free_controller()
91342f64fdbSSarah Jelinek 	 * below.
91442f64fdbSSarah Jelinek 	 */
9157c478bd9Sstevel@tonic-gate 
91642f64fdbSSarah Jelinek 	if (cp->paths != NULL && cp->paths[0] != NULL) {
91742f64fdbSSarah Jelinek 		if (dm_debug) {
91842f64fdbSSarah Jelinek 			(void) fprintf(stderr,
91942f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
92042f64fdbSSarah Jelinek 			    " with path ptrs. \n");
92142f64fdbSSarah Jelinek 		}
92242f64fdbSSarah Jelinek 	}
92342f64fdbSSarah Jelinek 	cache_free_controller(cp);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate  * If we have a controller in the list that is really a path then we need to
9287c478bd9Sstevel@tonic-gate  * take that controller out of the list since nodes that are paths are not
9297c478bd9Sstevel@tonic-gate  * considered to be controllers.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate static void
9327c478bd9Sstevel@tonic-gate clean_paths(struct search_args *args)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	controller_t	*cp;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
9377c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
93842f64fdbSSarah Jelinek 		path_t	**pp;
9397c478bd9Sstevel@tonic-gate 
94042f64fdbSSarah Jelinek 		pp = cp->paths;
94142f64fdbSSarah Jelinek 		if (pp != NULL) {
94242f64fdbSSarah Jelinek 			int i;
9437c478bd9Sstevel@tonic-gate 
94442f64fdbSSarah Jelinek 			for (i = 0; pp[i]; i++) {
94542f64fdbSSarah Jelinek 				remove_invalid_controller(pp[i]->name, cp,
94642f64fdbSSarah Jelinek 				    args);
94742f64fdbSSarah Jelinek 			}
9487c478bd9Sstevel@tonic-gate 		}
94942f64fdbSSarah Jelinek 		cp = cp->next;
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate static disk_t *
9547c478bd9Sstevel@tonic-gate create_disk(char *deviceid, char *kernel_name, struct search_args *args)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	disk_t	*diskp;
9577c478bd9Sstevel@tonic-gate 	char	*type;
9587c478bd9Sstevel@tonic-gate 	char	*prod_id;
9597c478bd9Sstevel@tonic-gate 	char	*vendor_id;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (dm_debug) {
96242f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	diskp = calloc(1, sizeof (disk_t));
9667c478bd9Sstevel@tonic-gate 	if (diskp == NULL) {
96742f64fdbSSarah Jelinek 		return (NULL);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	diskp->controllers = (controller_t **)
9717c478bd9Sstevel@tonic-gate 	    calloc(1, sizeof (controller_t *));
9727c478bd9Sstevel@tonic-gate 	if (diskp->controllers == NULL) {
97342f64fdbSSarah Jelinek 		cache_free_disk(diskp);
97442f64fdbSSarah Jelinek 		return (NULL);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	diskp->controllers[0] = NULL;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	diskp->devid = NULL;
9797c478bd9Sstevel@tonic-gate 	if (deviceid != NULL) {
98042f64fdbSSarah Jelinek 		if ((diskp->device_id = strdup(deviceid)) == NULL) {
98142f64fdbSSarah Jelinek 			cache_free_disk(diskp);
98242f64fdbSSarah Jelinek 			return (NULL);
98342f64fdbSSarah Jelinek 		}
98442f64fdbSSarah Jelinek 		(void) devid_str_decode(deviceid, &(diskp->devid), NULL);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
98842f64fdbSSarah Jelinek 		diskp->kernel_name = strdup(kernel_name);
98942f64fdbSSarah Jelinek 		if (diskp->kernel_name == NULL) {
99042f64fdbSSarah Jelinek 			cache_free_disk(diskp);
99142f64fdbSSarah Jelinek 			return (NULL);
99242f64fdbSSarah Jelinek 		}
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	diskp->paths = NULL;
9967c478bd9Sstevel@tonic-gate 	diskp->aliases = NULL;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	diskp->cd_rom = 0;
9997c478bd9Sstevel@tonic-gate 	diskp->rpm = 0;
1000*59d8f100SGarrett D'Amore 	diskp->solid_state = -1;
10017c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(args->minor);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	prod_id = get_str_prop(PROD_ID_PROP, args->node);
10047c478bd9Sstevel@tonic-gate 	if (prod_id != NULL) {
10057c478bd9Sstevel@tonic-gate 		if ((diskp->product_id = strdup(prod_id)) == NULL) {
100642f64fdbSSarah Jelinek 			cache_free_disk(diskp);
100742f64fdbSSarah Jelinek 			return (NULL);
100842f64fdbSSarah Jelinek 		}
100942f64fdbSSarah Jelinek 	} else {
101042f64fdbSSarah Jelinek 		prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
101142f64fdbSSarah Jelinek 		if (prod_id != NULL) {
101242f64fdbSSarah Jelinek 			if ((diskp->product_id = strdup(prod_id)) == NULL) {
101342f64fdbSSarah Jelinek 				cache_free_disk(diskp);
101442f64fdbSSarah Jelinek 				return (NULL);
101542f64fdbSSarah Jelinek 			}
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
10207c478bd9Sstevel@tonic-gate 	if (vendor_id != NULL) {
10217c478bd9Sstevel@tonic-gate 		if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
102242f64fdbSSarah Jelinek 			cache_free_disk(diskp);
102342f64fdbSSarah Jelinek 			return (NULL);
102442f64fdbSSarah Jelinek 		}
102542f64fdbSSarah Jelinek 	} else {
102642f64fdbSSarah Jelinek 		vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
102742f64fdbSSarah Jelinek 		if (vendor_id != NULL) {
102842f64fdbSSarah Jelinek 			if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
102942f64fdbSSarah Jelinek 				cache_free_disk(diskp);
103042f64fdbSSarah Jelinek 				return (NULL);
103142f64fdbSSarah Jelinek 			}
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
10377c478bd9Sstevel@tonic-gate 	 * We try to use uscsi later to determine the real type.
10387c478bd9Sstevel@tonic-gate 	 * The cd_rom flag tells us that the kernel categorized the drive
10397c478bd9Sstevel@tonic-gate 	 * as a CD-ROM.  We leave the drv_type as UKNOWN for now.
10407c478bd9Sstevel@tonic-gate 	 * The combination of the cd_rom flag being set with the drv_type of
10417c478bd9Sstevel@tonic-gate 	 * unknown is what triggers the uscsi probe in drive.c.
10427c478bd9Sstevel@tonic-gate 	 */
10437c478bd9Sstevel@tonic-gate 	if (disk_is_cdrom(type)) {
104442f64fdbSSarah Jelinek 		diskp->drv_type = DM_DT_UNKNOWN;
104542f64fdbSSarah Jelinek 		diskp->cd_rom = 1;
104642f64fdbSSarah Jelinek 		diskp->removable = 1;
10477c478bd9Sstevel@tonic-gate 	} else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
104842f64fdbSSarah Jelinek 		diskp->drv_type = DM_DT_FLOPPY;
104942f64fdbSSarah Jelinek 		diskp->removable = 1;
10507c478bd9Sstevel@tonic-gate 	} else {
10510167b58cScg 		/* not a "CD-ROM" or Floppy */
105242f64fdbSSarah Jelinek 		diskp->removable = get_prop(REMOVABLE_PROP, args->node);
10537c478bd9Sstevel@tonic-gate 
105442f64fdbSSarah Jelinek 		if (diskp->removable == -1) {
105542f64fdbSSarah Jelinek 			diskp->removable = 0;
10563e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
105742f64fdbSSarah Jelinek 			/*
105842f64fdbSSarah Jelinek 			 * x86 does not have removable property.
105942f64fdbSSarah Jelinek 			 * Check for common removable drives, zip & jaz,
106042f64fdbSSarah Jelinek 			 * and mark those correctly.
106142f64fdbSSarah Jelinek 			 */
106242f64fdbSSarah Jelinek 			if (vendor_id != NULL && prod_id != NULL) {
106342f64fdbSSarah Jelinek 				if (str_case_index(vendor_id,
106442f64fdbSSarah Jelinek 				    "iomega") != NULL) {
106542f64fdbSSarah Jelinek 					if (str_case_index(prod_id,
106642f64fdbSSarah Jelinek 					    "jaz") != NULL) {
106742f64fdbSSarah Jelinek 						diskp->removable = 1;
106842f64fdbSSarah Jelinek 					} else if (str_case_index(prod_id,
106942f64fdbSSarah Jelinek 					    "zip") != NULL) {
107042f64fdbSSarah Jelinek 						diskp->removable = 1;
107142f64fdbSSarah Jelinek 					}
107242f64fdbSSarah Jelinek 				}
10737c478bd9Sstevel@tonic-gate 			}
10747c478bd9Sstevel@tonic-gate #endif
107542f64fdbSSarah Jelinek 		}
10767c478bd9Sstevel@tonic-gate 
107742f64fdbSSarah Jelinek 		if (diskp->removable) {
107842f64fdbSSarah Jelinek 			/*
107942f64fdbSSarah Jelinek 			 * For removable jaz or zip drives there is no way
108042f64fdbSSarah Jelinek 			 * to get the drive type unless media is inserted,so
108142f64fdbSSarah Jelinek 			 * we look at the product-id for a hint.
108242f64fdbSSarah Jelinek 			 */
108342f64fdbSSarah Jelinek 			diskp->drv_type = DM_DT_UNKNOWN;
108442f64fdbSSarah Jelinek 
108542f64fdbSSarah Jelinek 			if (prod_id != NULL) {
108642f64fdbSSarah Jelinek 				if (str_case_index(prod_id, "jaz") != NULL) {
108742f64fdbSSarah Jelinek 					diskp->drv_type = DM_DT_JAZ;
108842f64fdbSSarah Jelinek 				} else if (str_case_index(prod_id,
108942f64fdbSSarah Jelinek 				    "zip") != NULL) {
109042f64fdbSSarah Jelinek 					diskp->drv_type = DM_DT_ZIP;
109142f64fdbSSarah Jelinek 				}
109242f64fdbSSarah Jelinek 			}
109342f64fdbSSarah Jelinek 		} else {
109442f64fdbSSarah Jelinek 			diskp->drv_type = DM_DT_FIXED;
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	diskp->next = args->disk_listp;
10997c478bd9Sstevel@tonic-gate 	args->disk_listp = diskp;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	return (diskp);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate static char *
11057c478bd9Sstevel@tonic-gate ctype(di_node_t node, di_minor_t minor)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	char	*type;
11087c478bd9Sstevel@tonic-gate 	char	*name;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
11117c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/* IDE disks use SCSI nexus as the type, so handle this special case */
11147c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(name, "ide")) {
111542f64fdbSSarah Jelinek 		return (DM_CTYPE_ATA);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb")) {
111942f64fdbSSarah Jelinek 		return (DM_CTYPE_USB);
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
11237c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT)) {
112442f64fdbSSarah Jelinek 			return (DM_CTYPE_SCSI);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT)) {
112842f64fdbSSarah Jelinek 		return (DM_CTYPE_FIBRE);
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
11327c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "fp")) {
113342f64fdbSSarah Jelinek 		return (DM_CTYPE_FIBRE);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
11377c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "ide")) {
113842f64fdbSSarah Jelinek 		return (DM_CTYPE_ATA);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	return (DM_CTYPE_UNKNOWN);
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate static boolean_t
114597ab9a43SJohn Levon disk_is_cdrom(const char *type)
11467c478bd9Sstevel@tonic-gate {
114797ab9a43SJohn Levon 	return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate static alias_t *
11517c478bd9Sstevel@tonic-gate find_alias(disk_t *diskp, char *kernel_name)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	alias_t	*ap;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
11567c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
115742f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
115842f64fdbSSarah Jelinek 			return (ap);
115942f64fdbSSarah Jelinek 		}
116042f64fdbSSarah Jelinek 		ap = ap->next;
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	return (NULL);
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate static bus_t *
11677c478bd9Sstevel@tonic-gate find_bus(struct search_args *args, char *name)
11687c478bd9Sstevel@tonic-gate {
11697c478bd9Sstevel@tonic-gate 	bus_t *listp;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	listp = args->bus_listp;
11727c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
117342f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(listp->name, name)) {
117442f64fdbSSarah Jelinek 			return (listp);
117542f64fdbSSarah Jelinek 		}
117642f64fdbSSarah Jelinek 		listp = listp->next;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	return (NULL);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate static controller_t *
11837c478bd9Sstevel@tonic-gate find_controller(struct search_args *args, char *name)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	controller_t *listp;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	listp = args->controller_listp;
11887c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
118942f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(listp->name, name)) {
119042f64fdbSSarah Jelinek 			return (listp);
119142f64fdbSSarah Jelinek 		}
119242f64fdbSSarah Jelinek 		listp = listp->next;
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	return (NULL);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate static int
11997c478bd9Sstevel@tonic-gate fix_cluster_devpath(di_devlink_t devlink, void *arg)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	int			fd;
12027c478bd9Sstevel@tonic-gate 	struct search_args	*args;
12037c478bd9Sstevel@tonic-gate 	char			*devlink_path;
12047c478bd9Sstevel@tonic-gate 	disk_t			*diskp = NULL;
12057c478bd9Sstevel@tonic-gate 	alias_t			*ap = NULL;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * The devlink_path is of the form /dev/did/rdsk/d1s0.
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/* Find the disk by the deviceid we read from the cluster disk. */
12147c478bd9Sstevel@tonic-gate 	devlink_path = (char *)di_devlink_path(devlink);
12157c478bd9Sstevel@tonic-gate 	if (devlink_path == NULL) {
121642f64fdbSSarah Jelinek 		return (DI_WALK_CONTINUE);
12177c478bd9Sstevel@tonic-gate 	}
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if ((fd = open(devlink_path, O_RDONLY|O_NDELAY)) >= 0) {
122042f64fdbSSarah Jelinek 		ddi_devid_t	devid;
12217c478bd9Sstevel@tonic-gate 
122242f64fdbSSarah Jelinek 		if (dm_debug > 1) {
122342f64fdbSSarah Jelinek 			(void) fprintf(stderr, "INFO:     cluster devpath %s\n",
122442f64fdbSSarah Jelinek 			    devlink_path);
122542f64fdbSSarah Jelinek 		}
12267c478bd9Sstevel@tonic-gate 
122742f64fdbSSarah Jelinek 		if (devid_get(fd, &devid) == 0) {
122842f64fdbSSarah Jelinek 			char *minor;
122942f64fdbSSarah Jelinek 			char *devidstr;
123042f64fdbSSarah Jelinek 
123142f64fdbSSarah Jelinek 			minor = di_minor_name(args->minor);
123242f64fdbSSarah Jelinek 
123342f64fdbSSarah Jelinek 			if ((devidstr =
123442f64fdbSSarah Jelinek 			    devid_str_encode(devid, minor)) != NULL) {
123542f64fdbSSarah Jelinek 				diskp = get_disk_by_deviceid(args->disk_listp,
123642f64fdbSSarah Jelinek 				    devidstr);
123742f64fdbSSarah Jelinek 				/*
123842f64fdbSSarah Jelinek 				 * This really shouldn't happen, since
123942f64fdbSSarah Jelinek 				 * we should have found all of the disks
124042f64fdbSSarah Jelinek 				 * during our first pass through
124142f64fdbSSarah Jelinek 				 * the dev tree, but just in case...
124242f64fdbSSarah Jelinek 				 */
124342f64fdbSSarah Jelinek 				if (diskp == NULL) {
124442f64fdbSSarah Jelinek 					if (dm_debug > 1) {
124542f64fdbSSarah Jelinek 						(void) fprintf(stderr,
124642f64fdbSSarah Jelinek 						    "INFO:    cluster create"
124742f64fdbSSarah Jelinek 						    " disk\n");
124842f64fdbSSarah Jelinek 					}
124942f64fdbSSarah Jelinek 
125042f64fdbSSarah Jelinek 					diskp = create_disk(devidstr,
125142f64fdbSSarah Jelinek 					    NULL, args);
125242f64fdbSSarah Jelinek 					if (diskp == NULL) {
125342f64fdbSSarah Jelinek 						args->dev_walk_status = ENOMEM;
125442f64fdbSSarah Jelinek 					}
125542f64fdbSSarah Jelinek 
125642f64fdbSSarah Jelinek 					/* add the controller relationship */
125742f64fdbSSarah Jelinek 					if (args->dev_walk_status == 0) {
125842f64fdbSSarah Jelinek 						if (add_disk2controller(diskp,
125942f64fdbSSarah Jelinek 						    args) != 0) {
126042f64fdbSSarah Jelinek 							args->dev_walk_status
126142f64fdbSSarah Jelinek 							    = ENOMEM;
126242f64fdbSSarah Jelinek 						}
126342f64fdbSSarah Jelinek 					}
126442f64fdbSSarah Jelinek 
126542f64fdbSSarah Jelinek 					if (new_alias(diskp, NULL,
126642f64fdbSSarah Jelinek 					    devlink_path, args) != 0) {
126742f64fdbSSarah Jelinek 						args->dev_walk_status = ENOMEM;
126842f64fdbSSarah Jelinek 					}
12697c478bd9Sstevel@tonic-gate 				}
127042f64fdbSSarah Jelinek 				devid_str_free(devidstr);
12717c478bd9Sstevel@tonic-gate 			}
127242f64fdbSSarah Jelinek 			devid_free(devid);
12737c478bd9Sstevel@tonic-gate 		}
127442f64fdbSSarah Jelinek 		(void) close(fd);
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
127942f64fdbSSarah Jelinek 		if (dm_debug > 1) {
128042f64fdbSSarah Jelinek 			(void) fprintf(stderr, "INFO:     cluster found"
128142f64fdbSSarah Jelinek 			    " disk\n");
128242f64fdbSSarah Jelinek 		}
128342f64fdbSSarah Jelinek 		ap = diskp->aliases;
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if (ap != NULL) {
12877c478bd9Sstevel@tonic-gate 		/*
128842f64fdbSSarah Jelinek 		 * NOTE: if ap->next != NULL have cluster
128942f64fdbSSarah Jelinek 		 * disks w/ multiple paths.
12907c478bd9Sstevel@tonic-gate 		 */
12917c478bd9Sstevel@tonic-gate 
129242f64fdbSSarah Jelinek 		if (!ap->cluster) {
129342f64fdbSSarah Jelinek 			char	*basep;
129442f64fdbSSarah Jelinek 			char	*namep;
129542f64fdbSSarah Jelinek 			int	cnt = 0;
129642f64fdbSSarah Jelinek 			int	size;
129742f64fdbSSarah Jelinek 			char	alias[MAXPATHLEN];
12987c478bd9Sstevel@tonic-gate 
129942f64fdbSSarah Jelinek 			/*
130042f64fdbSSarah Jelinek 			 * First time; save the /dev/rdsk devpaths and
130142f64fdbSSarah Jelinek 			 * update the alias info with the new alias name.
130242f64fdbSSarah Jelinek 			 */
130342f64fdbSSarah Jelinek 			ap->orig_paths = ap->devpaths;
130442f64fdbSSarah Jelinek 			ap->devpaths = NULL;
13057c478bd9Sstevel@tonic-gate 
130642f64fdbSSarah Jelinek 			free(ap->alias);
13077c478bd9Sstevel@tonic-gate 
130842f64fdbSSarah Jelinek 			/* get the new cluster alias name */
130942f64fdbSSarah Jelinek 			basep = strrchr(devlink_path, '/');
131042f64fdbSSarah Jelinek 			if (basep == NULL) {
131142f64fdbSSarah Jelinek 				basep = devlink_path;
131242f64fdbSSarah Jelinek 			} else {
131342f64fdbSSarah Jelinek 				basep++;
131442f64fdbSSarah Jelinek 			}
131542f64fdbSSarah Jelinek 			size = sizeof (alias) - 1;
131642f64fdbSSarah Jelinek 			namep = alias;
13177c478bd9Sstevel@tonic-gate 
131842f64fdbSSarah Jelinek 			while (*basep != 0 && *basep != 's' && cnt < size) {
131942f64fdbSSarah Jelinek 				*namep++ = *basep++;
132042f64fdbSSarah Jelinek 				cnt++;
132142f64fdbSSarah Jelinek 			}
132242f64fdbSSarah Jelinek 			*namep = 0;
13237c478bd9Sstevel@tonic-gate 
132442f64fdbSSarah Jelinek 			if ((ap->alias = strdup(alias)) == NULL) {
132542f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
132642f64fdbSSarah Jelinek 			}
132742f64fdbSSarah Jelinek 
132842f64fdbSSarah Jelinek 			ap->cluster = 1;
132942f64fdbSSarah Jelinek 		}
133042f64fdbSSarah Jelinek 
133142f64fdbSSarah Jelinek 		if (new_devpath(ap, devlink_path) != 0) {
133242f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
133342f64fdbSSarah Jelinek 		}
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id.
13417c478bd9Sstevel@tonic-gate  * We got the device id from the dev tree walk.  This is encoded
13427c478bd9Sstevel@tonic-gate  * using devid_str_encode(3DEVID).   In order to check the device ids we need
13437c478bd9Sstevel@tonic-gate  * to use the devid_compare(3DEVID) function, so we need to decode the
13447c478bd9Sstevel@tonic-gate  * string representation of the device id.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate static disk_t *
13477c478bd9Sstevel@tonic-gate get_disk_by_deviceid(disk_t *listp, char *devidstr)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
135242f64fdbSSarah Jelinek 		return (NULL);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
135642f64fdbSSarah Jelinek 		if (listp->devid != NULL &&
135742f64fdbSSarah Jelinek 		    devid_compare(listp->devid, devid) == 0) {
135842f64fdbSSarah Jelinek 			break;
135942f64fdbSSarah Jelinek 		}
136042f64fdbSSarah Jelinek 		listp = listp->next;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	devid_free(devid);
13647c478bd9Sstevel@tonic-gate 	return (listp);
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate /*
13687c478bd9Sstevel@tonic-gate  * Get the base disk name with no path prefix and no slice (if there is one).
13697c478bd9Sstevel@tonic-gate  * The name parameter should be big enough to hold the name.
13707c478bd9Sstevel@tonic-gate  * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
13717c478bd9Sstevel@tonic-gate  * and converts the raw diskette name.
13727c478bd9Sstevel@tonic-gate  * But, we don't know how to strip off the slice from third party drive
13737c478bd9Sstevel@tonic-gate  * names.  That just means that their drive name will include a slice on
13747c478bd9Sstevel@tonic-gate  * it.
13757c478bd9Sstevel@tonic-gate  */
13767c478bd9Sstevel@tonic-gate static void
13777c478bd9Sstevel@tonic-gate get_disk_name_from_path(char *path, char *name, int size)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	char		*basep;
13807c478bd9Sstevel@tonic-gate 	int		cnt = 0;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	basep = strrchr(path, '/');
13837c478bd9Sstevel@tonic-gate 	if (basep == NULL) {
138442f64fdbSSarah Jelinek 		basep = path;
13857c478bd9Sstevel@tonic-gate 	} else {
138642f64fdbSSarah Jelinek 		basep++;
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	size = size - 1;	/* leave room for terminating 0 */
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	if (is_ctds(basep)) {
139242f64fdbSSarah Jelinek 		while (*basep != 0 && *basep != 's' && cnt < size) {
139342f64fdbSSarah Jelinek 			*name++ = *basep++;
139442f64fdbSSarah Jelinek 				cnt++;
139542f64fdbSSarah Jelinek 		}
139642f64fdbSSarah Jelinek 		*name = 0;
13977c478bd9Sstevel@tonic-gate 	} else {
139842f64fdbSSarah Jelinek 		if (strncmp(basep, FLOPPY_NAME,
139942f64fdbSSarah Jelinek 		    sizeof (FLOPPY_NAME) - 1) == 0) {
140042f64fdbSSarah Jelinek 			/*
140142f64fdbSSarah Jelinek 			 * a floppy, convert rdiskette name to diskette name,
140242f64fdbSSarah Jelinek 			 * by skipping over the 'r' for raw diskette
140342f64fdbSSarah Jelinek 			 */
140442f64fdbSSarah Jelinek 			basep++;
140542f64fdbSSarah Jelinek 		}
14067c478bd9Sstevel@tonic-gate 
140742f64fdbSSarah Jelinek 		/* not a ctds name, just copy it */
140842f64fdbSSarah Jelinek 		(void) strlcpy(name, basep, size);
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate static char *
14137c478bd9Sstevel@tonic-gate get_byte_prop(char *prop_name, di_node_t node)
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	int	cnt;
14167c478bd9Sstevel@tonic-gate 	uchar_t	*bytes;
14177c478bd9Sstevel@tonic-gate 	int	i;
14187c478bd9Sstevel@tonic-gate 	char	str[MAXPATHLEN];
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
14217c478bd9Sstevel@tonic-gate 	if (cnt < 1) {
142242f64fdbSSarah Jelinek 		return (NULL);
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	str[0] = 0;
14267c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
142742f64fdbSSarah Jelinek 		char bstr[8];	/* a byte is only 2 hex chars + null */
14287c478bd9Sstevel@tonic-gate 
142942f64fdbSSarah Jelinek 		(void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
143042f64fdbSSarah Jelinek 		(void) strlcat(str, bstr, sizeof (str));
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 	return (strdup(str));
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate static di_node_t
14367c478bd9Sstevel@tonic-gate get_parent_bus(di_node_t node, struct search_args *args)
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	di_node_t pnode;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
14417c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
144242f64fdbSSarah Jelinek 		return (NULL);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
144642f64fdbSSarah Jelinek 		return (pnode);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	return (get_parent_bus(pnode, args));
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate static int
14537c478bd9Sstevel@tonic-gate get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	int *n;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
145842f64fdbSSarah Jelinek 		return (*n);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	return (0);
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate static char *
14657c478bd9Sstevel@tonic-gate get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
14667c478bd9Sstevel@tonic-gate {
14677c478bd9Sstevel@tonic-gate 	char *str;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
147042f64fdbSSarah Jelinek 		return (str);
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	return (NULL);
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate /*
14777c478bd9Sstevel@tonic-gate  * Get one of the positive int or boolean properties.
14787c478bd9Sstevel@tonic-gate  */
14797c478bd9Sstevel@tonic-gate static int
14807c478bd9Sstevel@tonic-gate get_prop(char *prop_name, di_node_t node)
14817c478bd9Sstevel@tonic-gate {
14827c478bd9Sstevel@tonic-gate 	int num;
14837c478bd9Sstevel@tonic-gate 	int *ip;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
14867c478bd9Sstevel@tonic-gate 	    >= 0) {
148742f64fdbSSarah Jelinek 		if (num == 0) {
148842f64fdbSSarah Jelinek 			/* boolean */
148942f64fdbSSarah Jelinek 			return (1);
149042f64fdbSSarah Jelinek 		} else if (num == 1) {
149142f64fdbSSarah Jelinek 			/* single int */
149242f64fdbSSarah Jelinek 			return (*ip);
149342f64fdbSSarah Jelinek 		}
14947c478bd9Sstevel@tonic-gate 	}
14957c478bd9Sstevel@tonic-gate 	return (-1);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate static char *
14997c478bd9Sstevel@tonic-gate get_str_prop(char *prop_name, di_node_t node)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	char *str;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
150442f64fdbSSarah Jelinek 		return (str);
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	return (NULL);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*
15117c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id, if the
15127c478bd9Sstevel@tonic-gate  * drive has a device id, or the kernel name, if it doesn't have a device id.
15137c478bd9Sstevel@tonic-gate  */
15147c478bd9Sstevel@tonic-gate static int
15157c478bd9Sstevel@tonic-gate have_disk(struct search_args *args, char *devidstr, char *kernel_name,
15167c478bd9Sstevel@tonic-gate     disk_t **diskp)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	disk_t *listp;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	*diskp = NULL;
15217c478bd9Sstevel@tonic-gate 	listp = args->disk_listp;
15227c478bd9Sstevel@tonic-gate 	if (devidstr != NULL) {
152342f64fdbSSarah Jelinek 		if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
152442f64fdbSSarah Jelinek 			return (1);
152542f64fdbSSarah Jelinek 		}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	} else {
152842f64fdbSSarah Jelinek 		/* no devid, try matching the kernel names on the drives */
152942f64fdbSSarah Jelinek 		while (listp != NULL) {
153042f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(kernel_name,
153142f64fdbSSarah Jelinek 			    listp->kernel_name)) {
153242f64fdbSSarah Jelinek 				*diskp = listp;
153342f64fdbSSarah Jelinek 				return (1);
153442f64fdbSSarah Jelinek 			}
153542f64fdbSSarah Jelinek 			listp = listp->next;
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 	return (0);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate static char *
15427c478bd9Sstevel@tonic-gate bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	char	*type;
15457c478bd9Sstevel@tonic-gate 	int	i;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	type = get_prom_str("device_type", node, ph);
15487c478bd9Sstevel@tonic-gate 	if (type == NULL) {
154942f64fdbSSarah Jelinek 		type = di_node_name(node);
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	for (i = 0; bustypes[i]; i++) {
155342f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(type, bustypes[i])) {
155442f64fdbSSarah Jelinek 			return (type);
155542f64fdbSSarah Jelinek 		}
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (minor != NULL && strcmp(di_minor_nodetype(minor),
15597c478bd9Sstevel@tonic-gate 	    DDI_NT_USB_ATTACHMENT_POINT) == 0) {
156042f64fdbSSarah Jelinek 		return ("usb");
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	return (NULL);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate static int
15677c478bd9Sstevel@tonic-gate is_cluster_disk(di_node_t node, di_minor_t minor)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	if (di_minor_spectype(minor) == S_IFCHR &&
15707c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(di_minor_nodetype(minor), DDI_PSEUDO) &&
15717c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(di_node_name(node), CLUSTER_DEV)) {
157242f64fdbSSarah Jelinek 		return (1);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	return (0);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate  * If the input name is in c[t]ds format then return 1, otherwise return 0.
15807c478bd9Sstevel@tonic-gate  */
15817c478bd9Sstevel@tonic-gate static int
15827c478bd9Sstevel@tonic-gate is_ctds(char *name)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	char	*p;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	p = name;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	if (*p++ != 'c') {
158942f64fdbSSarah Jelinek 		return (0);
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 	/* skip controller digits */
15927c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
159342f64fdbSSarah Jelinek 		p++;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/* handle optional target */
15977c478bd9Sstevel@tonic-gate 	if (*p == 't') {
15987c478bd9Sstevel@tonic-gate 		p++;
159942f64fdbSSarah Jelinek 		/* skip over target */
160042f64fdbSSarah Jelinek 		while (isdigit(*p) || isupper(*p)) {
160142f64fdbSSarah Jelinek 			p++;
160242f64fdbSSarah Jelinek 		}
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (*p++ != 'd') {
160642f64fdbSSarah Jelinek 		return (0);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
160942f64fdbSSarah Jelinek 		p++;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (*p++ != 's') {
161342f64fdbSSarah Jelinek 		return (0);
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/* check the slice number */
16177c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
161842f64fdbSSarah Jelinek 		p++;
16197c478bd9Sstevel@tonic-gate 	}
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	if (*p != 0) {
162242f64fdbSSarah Jelinek 		return (0);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	return (1);
16267c478bd9Sstevel@tonic-gate }
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate static int
16297c478bd9Sstevel@tonic-gate is_drive(di_minor_t minor)
16307c478bd9Sstevel@tonic-gate {
163197ab9a43SJohn Levon 	return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK,
163297ab9a43SJohn Levon 	    strlen(DDI_NT_BLOCK)) == 0);
16337c478bd9Sstevel@tonic-gate }
16347c478bd9Sstevel@tonic-gate 
1635e7cbe64fSgw static int
1636e7cbe64fSgw is_zvol(di_node_t node, di_minor_t minor)
1637e7cbe64fSgw {
1638e7cbe64fSgw 	if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) &&
1639681d9761SEric Taylor 	    minor(di_minor_devt(minor)))
1640e7cbe64fSgw 		return (1);
1641e7cbe64fSgw 	return (0);
1642e7cbe64fSgw }
1643e7cbe64fSgw 
16447c478bd9Sstevel@tonic-gate static int
16457c478bd9Sstevel@tonic-gate is_HBA(di_node_t node, di_minor_t minor)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	char	*type;
16487c478bd9Sstevel@tonic-gate 	char	*name;
16497c478bd9Sstevel@tonic-gate 	int	type_index;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
16527c478bd9Sstevel@tonic-gate 	type_index = 0;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	while (ctrltypes[type_index] != NULL) {
165542f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
165642f64fdbSSarah Jelinek 			return (1);
165742f64fdbSSarah Jelinek 		}
165842f64fdbSSarah Jelinek 		type_index++;
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
16627c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
16637c478bd9Sstevel@tonic-gate 	    libdiskmgt_str_eq(name, "ide")) {
166442f64fdbSSarah Jelinek 		return (1);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	return (0);
16687c478bd9Sstevel@tonic-gate }
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate static int
16717c478bd9Sstevel@tonic-gate new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
16727c478bd9Sstevel@tonic-gate 	struct search_args *args)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	alias_t		*aliasp;
16757c478bd9Sstevel@tonic-gate 	char		alias[MAXPATHLEN];
16767c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	aliasp = malloc(sizeof (alias_t));
16797c478bd9Sstevel@tonic-gate 	if (aliasp == NULL) {
168042f64fdbSSarah Jelinek 		return (ENOMEM);
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	aliasp->alias = NULL;
16847c478bd9Sstevel@tonic-gate 	aliasp->kstat_name = NULL;
16857c478bd9Sstevel@tonic-gate 	aliasp->wwn = NULL;
16867c478bd9Sstevel@tonic-gate 	aliasp->devpaths = NULL;
16877c478bd9Sstevel@tonic-gate 	aliasp->orig_paths = NULL;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	get_disk_name_from_path(devlink_path, alias, sizeof (alias));
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	aliasp->alias = strdup(alias);
16927c478bd9Sstevel@tonic-gate 	if (aliasp->alias == NULL) {
169342f64fdbSSarah Jelinek 		cache_free_alias(aliasp);
169442f64fdbSSarah Jelinek 		return (ENOMEM);
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
169842f64fdbSSarah Jelinek 		aliasp->kstat_name = strdup(kernel_name);
169942f64fdbSSarah Jelinek 		if (aliasp->kstat_name == NULL) {
170042f64fdbSSarah Jelinek 			cache_free_alias(aliasp);
170142f64fdbSSarah Jelinek 			return (ENOMEM);
170242f64fdbSSarah Jelinek 		}
17037c478bd9Sstevel@tonic-gate 	} else {
170442f64fdbSSarah Jelinek 		aliasp->kstat_name = NULL;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	aliasp->cluster = 0;
17087c478bd9Sstevel@tonic-gate 	aliasp->lun = get_prop(DM_LUN, args->node);
17097c478bd9Sstevel@tonic-gate 	aliasp->target = get_prop(DM_TARGET, args->node);
17107c478bd9Sstevel@tonic-gate 	aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(args->node);
17137c478bd9Sstevel@tonic-gate 	if (pnode != DI_NODE_NIL) {
171442f64fdbSSarah Jelinek 		char prop_name[MAXPROPLEN];
17157c478bd9Sstevel@tonic-gate 
171642f64fdbSSarah Jelinek 		(void) snprintf(prop_name, sizeof (prop_name),
171742f64fdbSSarah Jelinek 		    "target%d-sync-speed", aliasp->target);
171842f64fdbSSarah Jelinek 		diskp->sync_speed = get_prop(prop_name, pnode);
171942f64fdbSSarah Jelinek 		(void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
172042f64fdbSSarah Jelinek 		    aliasp->target);
172142f64fdbSSarah Jelinek 		diskp->wide = get_prop(prop_name, pnode);
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	if (new_devpath(aliasp, devlink_path) != 0) {
172542f64fdbSSarah Jelinek 		cache_free_alias(aliasp);
172642f64fdbSSarah Jelinek 		return (ENOMEM);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	aliasp->next = diskp->aliases;
17307c478bd9Sstevel@tonic-gate 	diskp->aliases = aliasp;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	return (0);
17337c478bd9Sstevel@tonic-gate }
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate /*
17367c478bd9Sstevel@tonic-gate  * Append the new devpath to the end of the devpath list.  This is important
17377c478bd9Sstevel@tonic-gate  * since we may want to use the order of the devpaths to match up the vtoc
17387c478bd9Sstevel@tonic-gate  * entries.
17397c478bd9Sstevel@tonic-gate  */
17407c478bd9Sstevel@tonic-gate static int
17417c478bd9Sstevel@tonic-gate new_devpath(alias_t *ap, char *devpath)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate 	slice_t	*newdp;
17447c478bd9Sstevel@tonic-gate 	slice_t *alistp;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/*
17477c478bd9Sstevel@tonic-gate 	 * First, search the alias list to be sure that this devpath is
17487c478bd9Sstevel@tonic-gate 	 * not already there.
17497c478bd9Sstevel@tonic-gate 	 */
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
175242f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
175342f64fdbSSarah Jelinek 			return (0);
175442f64fdbSSarah Jelinek 		}
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * Otherwise, not found so add this new devpath to the list.
17597c478bd9Sstevel@tonic-gate 	 */
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	newdp = malloc(sizeof (slice_t));
17627c478bd9Sstevel@tonic-gate 	if (newdp == NULL) {
176342f64fdbSSarah Jelinek 		return (ENOMEM);
17647c478bd9Sstevel@tonic-gate 	}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	newdp->devpath = strdup(devpath);
17677c478bd9Sstevel@tonic-gate 	if (newdp->devpath == NULL) {
176842f64fdbSSarah Jelinek 		free(newdp);
176942f64fdbSSarah Jelinek 		return (ENOMEM);
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 	newdp->slice_num = -1;
17727c478bd9Sstevel@tonic-gate 	newdp->next = NULL;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if (ap->devpaths == NULL) {
177542f64fdbSSarah Jelinek 		ap->devpaths = newdp;
17767c478bd9Sstevel@tonic-gate 	} else {
177742f64fdbSSarah Jelinek 		/* append the devpath to the end of the list */
177842f64fdbSSarah Jelinek 		slice_t	*dp;
17797c478bd9Sstevel@tonic-gate 
178042f64fdbSSarah Jelinek 		dp = ap->devpaths;
178142f64fdbSSarah Jelinek 		while (dp->next != NULL) {
178242f64fdbSSarah Jelinek 			dp = dp->next;
178342f64fdbSSarah Jelinek 		}
17847c478bd9Sstevel@tonic-gate 
178542f64fdbSSarah Jelinek 		dp->next = newdp;
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	return (0);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate static path_t *
17927c478bd9Sstevel@tonic-gate new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
17937c478bd9Sstevel@tonic-gate 	char *wwn)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	char		*devpath;
17967c478bd9Sstevel@tonic-gate 	path_t		*pp;
17977c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
18007c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
180142f64fdbSSarah Jelinek 		di_node_t pnode;
18027c478bd9Sstevel@tonic-gate 
180342f64fdbSSarah Jelinek 		pnode = di_parent_node(node);
180442f64fdbSSarah Jelinek 		if (pnode != DI_NODE_NIL) {
180542f64fdbSSarah Jelinek 			node = pnode;
180642f64fdbSSarah Jelinek 		}
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/* check if the path is already there */
18127c478bd9Sstevel@tonic-gate 	pp = NULL;
18137c478bd9Sstevel@tonic-gate 	if (cp->paths != NULL) {
181442f64fdbSSarah Jelinek 		int i;
18157c478bd9Sstevel@tonic-gate 
181642f64fdbSSarah Jelinek 		for (i = 0; cp->paths[i]; i++) {
181742f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
181842f64fdbSSarah Jelinek 				pp = cp->paths[i];
181942f64fdbSSarah Jelinek 				break;
182042f64fdbSSarah Jelinek 			}
18217c478bd9Sstevel@tonic-gate 		}
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
182542f64fdbSSarah Jelinek 		/* the path exists, add this disk to it */
18267c478bd9Sstevel@tonic-gate 
182742f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
182842f64fdbSSarah Jelinek 		if (!add_disk2path(dp, pp, st, wwn)) {
182942f64fdbSSarah Jelinek 			return (NULL);
183042f64fdbSSarah Jelinek 		}
183142f64fdbSSarah Jelinek 		return (pp);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/* create a new path */
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	pp = calloc(1, sizeof (path_t));
18377c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
183842f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
183942f64fdbSSarah Jelinek 		return (NULL);
18407c478bd9Sstevel@tonic-gate 	}
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	pp->name = strdup(devpath);
18437c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
18447c478bd9Sstevel@tonic-gate 	if (pp->name == NULL) {
184542f64fdbSSarah Jelinek 		cache_free_path(pp);
184642f64fdbSSarah Jelinek 		return (NULL);
18477c478bd9Sstevel@tonic-gate 	}
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
18507c478bd9Sstevel@tonic-gate 	if (!add_disk2path(dp, pp, st, wwn)) {
185142f64fdbSSarah Jelinek 		return (NULL);
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	/* add the path to the controller */
18557c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
185642f64fdbSSarah Jelinek 		cache_free_path(pp);
185742f64fdbSSarah Jelinek 		return (NULL);
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/* add the controller to the path */
18617c478bd9Sstevel@tonic-gate 	pp->controller = cp;
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	minor = di_minor_next(node, NULL);
18647c478bd9Sstevel@tonic-gate 	if (minor != NULL) {
186542f64fdbSSarah Jelinek 		pp->ctype = ctype(node, minor);
18667c478bd9Sstevel@tonic-gate 	} else {
186742f64fdbSSarah Jelinek 		pp->ctype = DM_CTYPE_UNKNOWN;
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	return (pp);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate /*
18747c478bd9Sstevel@tonic-gate  * We pass in the current controller pointer (currp) so we can double check
18757c478bd9Sstevel@tonic-gate  * that we aren't corrupting the list by removing the element we are on.  This
18767c478bd9Sstevel@tonic-gate  * should never happen, but it doesn't hurt to double check.
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate static void
18797c478bd9Sstevel@tonic-gate remove_invalid_controller(char *name, controller_t *currp,
18807c478bd9Sstevel@tonic-gate     struct search_args *args)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	controller_t *cp;
18837c478bd9Sstevel@tonic-gate 	bus_t *bp;
18847c478bd9Sstevel@tonic-gate 	controller_t *prevp;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	bp = args->bus_listp;
18877c478bd9Sstevel@tonic-gate 	while (bp != NULL) {
188842f64fdbSSarah Jelinek 		int i;
18897c478bd9Sstevel@tonic-gate 
189042f64fdbSSarah Jelinek 		for (i = 0; bp->controllers[i]; i++) {
189142f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
189242f64fdbSSarah Jelinek 				int j;
189342f64fdbSSarah Jelinek 				/*
189442f64fdbSSarah Jelinek 				 * remove pointer to invalid controller.
189542f64fdbSSarah Jelinek 				 * (it is a path)
189642f64fdbSSarah Jelinek 				 */
189742f64fdbSSarah Jelinek 				for (j = i; bp->controllers[j]; j++) {
189842f64fdbSSarah Jelinek 					bp->controllers[j] =
189942f64fdbSSarah Jelinek 					    bp->controllers[j + 1];
190042f64fdbSSarah Jelinek 				}
190142f64fdbSSarah Jelinek 			}
19027c478bd9Sstevel@tonic-gate 		}
190342f64fdbSSarah Jelinek 		bp = bp->next;
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (args->controller_listp == NULL) {
190742f64fdbSSarah Jelinek 		return;
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
19117c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->name, name)) {
19127c478bd9Sstevel@tonic-gate 		args->controller_listp = cp->next;
191342f64fdbSSarah Jelinek 		if (dm_debug) {
191442f64fdbSSarah Jelinek 			(void) fprintf(stderr,
191542f64fdbSSarah Jelinek 			    "INFO: Removed controller %s from list\n",
191642f64fdbSSarah Jelinek 			    cp->name);
191742f64fdbSSarah Jelinek 		}
191842f64fdbSSarah Jelinek 		remove_controller(cp, currp);
191942f64fdbSSarah Jelinek 		return;
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	prevp = cp;
19237c478bd9Sstevel@tonic-gate 	cp = cp->next;
19247c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
192542f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(cp->name, name)) {
192642f64fdbSSarah Jelinek 			if (dm_debug) {
192742f64fdbSSarah Jelinek 				(void) fprintf(stderr,
192842f64fdbSSarah Jelinek 				    "INFO: Removed controller %s from list\n",
192942f64fdbSSarah Jelinek 				    cp->name);
193042f64fdbSSarah Jelinek 			}
193142f64fdbSSarah Jelinek 			prevp->next = cp->next;
193242f64fdbSSarah Jelinek 			remove_controller(cp, currp);
193342f64fdbSSarah Jelinek 			return;
19347c478bd9Sstevel@tonic-gate 		}
193542f64fdbSSarah Jelinek 		prevp = cp;
193642f64fdbSSarah Jelinek 		cp = cp->next;
19377c478bd9Sstevel@tonic-gate 	}
19387c478bd9Sstevel@tonic-gate }
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate /*
19417c478bd9Sstevel@tonic-gate  * This is the standard strstr code modified for case independence.
19427c478bd9Sstevel@tonic-gate  */
19437c478bd9Sstevel@tonic-gate static char *
19447c478bd9Sstevel@tonic-gate str_case_index(register char *s1, register char *s2)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate 	uint_t s2len = strlen(s2); /* length of the second string */
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/* If the length of the second string is 0, return the first arg. */
19497c478bd9Sstevel@tonic-gate 	if (s2len == 0) {
195042f64fdbSSarah Jelinek 		return (s1);
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	while (strlen(s1) >= s2len) {
195442f64fdbSSarah Jelinek 		if (strncasecmp(s1, s2, s2len) == 0) {
195542f64fdbSSarah Jelinek 			return (s1);
195642f64fdbSSarah Jelinek 		}
195742f64fdbSSarah Jelinek 		s1++;
19587c478bd9Sstevel@tonic-gate 	}
19597c478bd9Sstevel@tonic-gate 	return (NULL);
19607c478bd9Sstevel@tonic-gate }
1961