xref: /illumos-gate/usr/src/lib/libdiskmgt/common/findevs.c (revision c470f5752f71dfecd12e42f4da179d8f8291ce52)
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  */
21*c470f575SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
2342f64fdbSSarah Jelinek  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*c470f575SYuri Pankov  */
26*c470f575SYuri Pankov 
27*c470f575SYuri Pankov /*
289729663dSGeorge Wilson  * Copyright (c) 2011 by Delphix. All rights reserved.
29*c470f575SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
40681d9761SEric Taylor #include <sys/mkdev.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <libgen.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <devid.h>
45e7cbe64fSgw25295 #include <sys/fs/zfs.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
487c478bd9Sstevel@tonic-gate #include "disks_private.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* specify which disk links to use in the /dev directory */
517c478bd9Sstevel@tonic-gate #define	DEVLINK_REGEX		"rdsk/.*"
527c478bd9Sstevel@tonic-gate #define	DEVLINK_FLOPPY_REGEX	"rdiskette[0-9]"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	FLOPPY_NAME	"rdiskette"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	MAXPROPLEN		1024
577c478bd9Sstevel@tonic-gate #define	DEVICE_ID_PROP		"devid"
587c478bd9Sstevel@tonic-gate #define	PROD_ID_PROP		"inquiry-product-id"
597c478bd9Sstevel@tonic-gate #define	PROD_ID_USB_PROP	"usb-product-name"
607c478bd9Sstevel@tonic-gate #define	REMOVABLE_PROP		"removable-media"
610167b58cScg149915 #define	HOTPLUGGABLE_PROP	"hotpluggable"
627c478bd9Sstevel@tonic-gate #define	SCSI_OPTIONS_PROP	"scsi-options"
637c478bd9Sstevel@tonic-gate #define	VENDOR_ID_PROP		"inquiry-vendor-id"
647c478bd9Sstevel@tonic-gate #define	VENDOR_ID_USB_PROP	"usb-vendor-name"
657c478bd9Sstevel@tonic-gate #define	WWN_PROP		"node-wwn"
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static char *ctrltypes[] = {
687c478bd9Sstevel@tonic-gate 	DDI_NT_FC_ATTACHMENT_POINT,
69*c470f575SYuri Pankov 	DDI_NT_NVME_ATTACHMENT_POINT,
70*c470f575SYuri Pankov 	DDI_NT_SATA_ATTACHMENT_POINT,
71*c470f575SYuri Pankov 	DDI_NT_SATA_NEXUS,
72*c470f575SYuri Pankov 	DDI_NT_SCSI_ATTACHMENT_POINT,
73*c470f575SYuri Pankov 	DDI_NT_SCSI_NEXUS,
747c478bd9Sstevel@tonic-gate 	NULL
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static char *bustypes[] = {
787c478bd9Sstevel@tonic-gate 	"sbus",
797c478bd9Sstevel@tonic-gate 	"pci",
807c478bd9Sstevel@tonic-gate 	"usb",
817c478bd9Sstevel@tonic-gate 	NULL
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static bus_t		*add_bus(struct search_args *args, di_node_t node,
857c478bd9Sstevel@tonic-gate 			    di_minor_t minor, controller_t *cp);
867c478bd9Sstevel@tonic-gate static controller_t	*add_controller(struct search_args *args,
877c478bd9Sstevel@tonic-gate 			    di_node_t node, di_minor_t minor);
887c478bd9Sstevel@tonic-gate static int		add_devpath(di_devlink_t devlink, void *arg);
897c478bd9Sstevel@tonic-gate static int		add_devs(di_node_t node, di_minor_t minor, void *arg);
907c478bd9Sstevel@tonic-gate static int		add_disk2controller(disk_t *diskp,
917c478bd9Sstevel@tonic-gate 			    struct search_args *args);
927c478bd9Sstevel@tonic-gate static int		add_disk2path(disk_t *dp, path_t *pp,
937c478bd9Sstevel@tonic-gate 			    di_path_state_t st, char *wwn);
947c478bd9Sstevel@tonic-gate static int		add_int2array(int p, int **parray);
957c478bd9Sstevel@tonic-gate static int		add_ptr2array(void *p, void ***parray);
967c478bd9Sstevel@tonic-gate static char		*bus_type(di_node_t node, di_minor_t minor,
977c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
9842f64fdbSSarah Jelinek static void		remove_controller(controller_t *cp,
997c478bd9Sstevel@tonic-gate 			    controller_t *currp);
1007c478bd9Sstevel@tonic-gate static void		clean_paths(struct search_args *args);
1017c478bd9Sstevel@tonic-gate static disk_t		*create_disk(char *deviceid, char *kernel_name,
1027c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1037c478bd9Sstevel@tonic-gate static char		*ctype(di_node_t node, di_minor_t minor);
10497ab9a43SJohn Levon static boolean_t	disk_is_cdrom(const char *type);
1057c478bd9Sstevel@tonic-gate static alias_t		*find_alias(disk_t *diskp, char *kernel_name);
1067c478bd9Sstevel@tonic-gate static bus_t		*find_bus(struct search_args *args, char *name);
1077c478bd9Sstevel@tonic-gate static controller_t	*find_controller(struct search_args *args, char *name);
1087c478bd9Sstevel@tonic-gate static disk_t		*get_disk_by_deviceid(disk_t *listp, char *devid);
1097c478bd9Sstevel@tonic-gate static void		get_disk_name_from_path(char *path, char *name,
1107c478bd9Sstevel@tonic-gate 			    int size);
1117c478bd9Sstevel@tonic-gate static char		*get_byte_prop(char *prop_name, di_node_t node);
1127c478bd9Sstevel@tonic-gate static di_node_t	get_parent_bus(di_node_t node,
1137c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1147c478bd9Sstevel@tonic-gate static int		get_prom_int(char *prop_name, di_node_t node,
1157c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1167c478bd9Sstevel@tonic-gate static char		*get_prom_str(char *prop_name, di_node_t node,
1177c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1187c478bd9Sstevel@tonic-gate static int		get_prop(char *prop_name, di_node_t node);
1197c478bd9Sstevel@tonic-gate static char		*get_str_prop(char *prop_name, di_node_t node);
1207c478bd9Sstevel@tonic-gate static int		have_disk(struct search_args *args, char *devid,
1217c478bd9Sstevel@tonic-gate 			    char *kernel_name, disk_t **diskp);
1227c478bd9Sstevel@tonic-gate static int		is_ctds(char *name);
1237c478bd9Sstevel@tonic-gate static int		is_drive(di_minor_t minor);
124e7cbe64fSgw25295 static int		is_zvol(di_node_t node, di_minor_t minor);
125*c470f575SYuri Pankov static int		is_ctrl(di_node_t node, di_minor_t minor);
1267c478bd9Sstevel@tonic-gate static int		new_alias(disk_t *diskp, char *kernel_path,
1277c478bd9Sstevel@tonic-gate 			    char *devlink_path, struct search_args *args);
1287c478bd9Sstevel@tonic-gate static int		new_devpath(alias_t *ap, char *devpath);
1297c478bd9Sstevel@tonic-gate static path_t		*new_path(controller_t *cp, disk_t *diskp,
1307c478bd9Sstevel@tonic-gate 			    di_node_t node, di_path_state_t st, char *wwn);
1317c478bd9Sstevel@tonic-gate static void		remove_invalid_controller(char *name,
1327c478bd9Sstevel@tonic-gate 			    controller_t *currp, struct search_args *args);
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 	di_node_t		di_root;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	args->bus_listp = NULL;
149*c470f575SYuri Pankov 	args->controller_listp = NULL;
150*c470f575SYuri Pankov 	args->disk_listp = NULL;
1517c478bd9Sstevel@tonic-gate 
152*c470f575SYuri Pankov 	args->dev_walk_status = 0;
1537c478bd9Sstevel@tonic-gate 	args->handle = di_devlink_init(NULL, 0);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Have to make several passes at this with the new devfs caching.
1577c478bd9Sstevel@tonic-gate 	 * First, we find non-mpxio devices. Then we find mpxio/multipath
158*c470f575SYuri Pankov 	 * devices.
1597c478bd9Sstevel@tonic-gate 	 */
160*c470f575SYuri Pankov 	di_root = di_init("/", DINFOCACHE);
1617c478bd9Sstevel@tonic-gate 	args->ph = di_prom_init();
1627c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1637c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1647c478bd9Sstevel@tonic-gate 
165*c470f575SYuri Pankov 	di_root = di_init("/", DINFOCPYALL|DINFOPATH);
1667c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1677c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	(void) di_devlink_fini(&(args->handle));
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	clean_paths(args);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Definitions of private functions
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static bus_t *
1797c478bd9Sstevel@tonic-gate add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
1807c478bd9Sstevel@tonic-gate     controller_t *cp)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	char		*btype;
1837c478bd9Sstevel@tonic-gate 	char		*devpath;
1847c478bd9Sstevel@tonic-gate 	bus_t		*bp;
1857c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
1867c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
1897c478bd9Sstevel@tonic-gate 		return (NULL);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if ((btype = bus_type(node, minor, args->ph)) == NULL) {
1937c478bd9Sstevel@tonic-gate 		return (add_bus(args, di_parent_node(node),
1947c478bd9Sstevel@tonic-gate 		    di_minor_next(di_parent_node(node), NULL), cp));
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if ((bp = find_bus(args, devpath)) != NULL) {
2007c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
20342f64fdbSSarah Jelinek 			if (add_ptr2array(cp,
20442f64fdbSSarah Jelinek 			    (void ***)&bp->controllers) != 0) {
2057c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
2067c478bd9Sstevel@tonic-gate 				return (NULL);
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 		return (bp);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* Special handling for root node. */
2137c478bd9Sstevel@tonic-gate 	if (strcmp(devpath, "/") == 0) {
2147c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
2157c478bd9Sstevel@tonic-gate 		return (NULL);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (dm_debug) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	bp = (bus_t *)calloc(1, sizeof (bus_t));
2237c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
2247c478bd9Sstevel@tonic-gate 		return (NULL);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	bp->name = strdup(devpath);
2287c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
2297c478bd9Sstevel@tonic-gate 	if (bp->name == NULL) {
2307c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
2317c478bd9Sstevel@tonic-gate 		cache_free_bus(bp);
2327c478bd9Sstevel@tonic-gate 		return (NULL);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	bp->btype = strdup(btype);
2367c478bd9Sstevel@tonic-gate 	if (bp->btype == NULL) {
2377c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
2387c478bd9Sstevel@tonic-gate 		cache_free_bus(bp);
2397c478bd9Sstevel@tonic-gate 		return (NULL);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
2437c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
2467c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
2477c478bd9Sstevel@tonic-gate 		cache_free_bus(bp);
2487c478bd9Sstevel@tonic-gate 		return (NULL);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* if parent node is a bus, get its name */
2527c478bd9Sstevel@tonic-gate 	if ((pnode = get_parent_bus(node, args)) != NULL) {
2537c478bd9Sstevel@tonic-gate 		devpath = di_devfs_path(pnode);
2547c478bd9Sstevel@tonic-gate 		bp->pname = strdup(devpath);
2557c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
2567c478bd9Sstevel@tonic-gate 		if (bp->pname == NULL) {
2577c478bd9Sstevel@tonic-gate 			args->dev_walk_status = ENOMEM;
2587c478bd9Sstevel@tonic-gate 			cache_free_bus(bp);
2597c478bd9Sstevel@tonic-gate 			return (NULL);
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	} else {
2637c478bd9Sstevel@tonic-gate 		bp->pname = NULL;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	bp->freq = get_prom_int("clock-frequency", node, args->ph);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
2697c478bd9Sstevel@tonic-gate 	if (bp->controllers == NULL) {
2707c478bd9Sstevel@tonic-gate 		args->dev_walk_status = ENOMEM;
2717c478bd9Sstevel@tonic-gate 		cache_free_bus(bp);
2727c478bd9Sstevel@tonic-gate 		return (NULL);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	bp->controllers[0] = NULL;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
2777c478bd9Sstevel@tonic-gate 		if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
2787c478bd9Sstevel@tonic-gate 			args->dev_walk_status = ENOMEM;
2797c478bd9Sstevel@tonic-gate 			return (NULL);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	bp->next = args->bus_listp;
2847c478bd9Sstevel@tonic-gate 	args->bus_listp = bp;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	return (bp);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate static controller_t *
2907c478bd9Sstevel@tonic-gate add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	char		*devpath;
2937c478bd9Sstevel@tonic-gate 	controller_t	*cp;
2947c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
2957c478bd9Sstevel@tonic-gate 	char		*c_type = DM_CTYPE_UNKNOWN;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if ((cp = find_controller(args, devpath)) != NULL) {
3007c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
3017c478bd9Sstevel@tonic-gate 		return (cp);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
3057c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
3067c478bd9Sstevel@tonic-gate 		di_node_t pnode;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		pnode = di_parent_node(node);
3097c478bd9Sstevel@tonic-gate 		if (pnode != DI_NODE_NIL) {
3107c478bd9Sstevel@tonic-gate 			di_devfs_path_free((void *) devpath);
3117c478bd9Sstevel@tonic-gate 			devpath = di_devfs_path(pnode);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 			if ((cp = find_controller(args, devpath)) != NULL) {
3147c478bd9Sstevel@tonic-gate 				di_devfs_path_free((void *) devpath);
3157c478bd9Sstevel@tonic-gate 				return (cp);
3167c478bd9Sstevel@tonic-gate 			}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			/* not in the list, create it */
3197c478bd9Sstevel@tonic-gate 			node = pnode;
3207c478bd9Sstevel@tonic-gate 			c_type = DM_CTYPE_FIBRE;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (dm_debug) {
3257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	cp = (controller_t *)calloc(1, sizeof (controller_t));
3297c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
3307c478bd9Sstevel@tonic-gate 		return (NULL);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	cp->name = strdup(devpath);
3347c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
3357c478bd9Sstevel@tonic-gate 	if (cp->name == NULL) {
3367c478bd9Sstevel@tonic-gate 		cache_free_controller(cp);
3377c478bd9Sstevel@tonic-gate 		return (NULL);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
3417c478bd9Sstevel@tonic-gate 		c_type = ctype(node, minor);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 	cp->ctype = c_type;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
3467c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
3497c478bd9Sstevel@tonic-gate 		cache_free_controller(cp);
3507c478bd9Sstevel@tonic-gate 		return (NULL);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
3547c478bd9Sstevel@tonic-gate 		cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
3587c478bd9Sstevel@tonic-gate 		cp->multiplex = 1;
3597c478bd9Sstevel@tonic-gate 	} else {
3607c478bd9Sstevel@tonic-gate 		cp->multiplex = 0;
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	cp->freq = get_prom_int("clock-frequency", node, args->ph);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
3667c478bd9Sstevel@tonic-gate 	if (cp->disks == NULL) {
3677c478bd9Sstevel@tonic-gate 		cache_free_controller(cp);
3687c478bd9Sstevel@tonic-gate 		return (NULL);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	cp->disks[0] = NULL;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	cp->next = args->controller_listp;
3737c478bd9Sstevel@tonic-gate 	args->controller_listp = cp;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	cp->bus = add_bus(args, di_parent_node(node),
3767c478bd9Sstevel@tonic-gate 	    di_minor_next(di_parent_node(node), NULL), cp);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (cp);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static int
3827c478bd9Sstevel@tonic-gate add_devpath(di_devlink_t devlink, void *arg)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	struct search_args *args;
3857c478bd9Sstevel@tonic-gate 	char		*devidstr;
3867c478bd9Sstevel@tonic-gate 	disk_t		*diskp;
3877c478bd9Sstevel@tonic-gate 	char		kernel_name[MAXPATHLEN];
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Get the diskp value from calling have_disk. Can either be found
3937c478bd9Sstevel@tonic-gate 	 * by kernel name or devid.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	diskp = NULL;
3977c478bd9Sstevel@tonic-gate 	devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
3987c478bd9Sstevel@tonic-gate 	(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
3997c478bd9Sstevel@tonic-gate 	    di_node_name(args->node), di_instance(args->node));
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	(void) have_disk(args, devidstr, kernel_name, &diskp);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
4057c478bd9Sstevel@tonic-gate 	 * For diskettes it is /dev/rdiskette*.
4067c478bd9Sstevel@tonic-gate 	 * On Intel we would also get each fdisk partition as well
4077c478bd9Sstevel@tonic-gate 	 * (e.g. /dev/rdsk/c0t0d0p0).
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
4107c478bd9Sstevel@tonic-gate 		alias_t	*ap;
4117c478bd9Sstevel@tonic-gate 		char	*devlink_path;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		if (diskp->drv_type != DM_DT_FLOPPY) {
4147c478bd9Sstevel@tonic-gate 			/*
41542f64fdbSSarah Jelinek 			 * Add other controllers for multipath disks.
41642f64fdbSSarah Jelinek 			 * This will have no effect if the controller
41742f64fdbSSarah Jelinek 			 * relationship is already set up.
4187c478bd9Sstevel@tonic-gate 			 */
4197c478bd9Sstevel@tonic-gate 			if (add_disk2controller(diskp, args) != 0) {
4207c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
4217c478bd9Sstevel@tonic-gate 			}
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
4257c478bd9Sstevel@tonic-gate 		    di_node_name(args->node), di_instance(args->node));
4267c478bd9Sstevel@tonic-gate 		devlink_path = (char *)di_devlink_path(devlink);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		if (dm_debug > 1) {
42942f64fdbSSarah Jelinek 			(void) fprintf(stderr,
43042f64fdbSSarah Jelinek 			    "INFO:     devpath %s\n", devlink_path);
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if ((ap = find_alias(diskp, kernel_name)) == NULL) {
43442f64fdbSSarah Jelinek 			if (new_alias(diskp, kernel_name, devlink_path,
43542f64fdbSSarah Jelinek 			    args) != 0) {
4367c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
4377c478bd9Sstevel@tonic-gate 			}
4387c478bd9Sstevel@tonic-gate 		} else {
4397c478bd9Sstevel@tonic-gate 			/*
44042f64fdbSSarah Jelinek 			 * It is possible that we have already added this
44142f64fdbSSarah Jelinek 			 * devpath.  Do not add it again. new_devpath will
44242f64fdbSSarah Jelinek 			 * return a 0 if found, and not add the path.
4437c478bd9Sstevel@tonic-gate 			 */
4447c478bd9Sstevel@tonic-gate 			if (new_devpath(ap, devlink_path) != 0) {
4457c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate static int
4547c478bd9Sstevel@tonic-gate add_devs(di_node_t node, di_minor_t minor, void *arg)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	struct search_args	*args;
4577c478bd9Sstevel@tonic-gate 	int result = DI_WALK_CONTINUE;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
4627c478bd9Sstevel@tonic-gate 		/* This is all just debugging code */
4637c478bd9Sstevel@tonic-gate 		char	*devpath;
4647c478bd9Sstevel@tonic-gate 		char	dev_name[MAXPATHLEN];
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		devpath = di_devfs_path(node);
4677c478bd9Sstevel@tonic-gate 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
4687c478bd9Sstevel@tonic-gate 		    di_minor_name(minor));
4697c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4727c478bd9Sstevel@tonic-gate 		    "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
47342f64fdbSSarah Jelinek 		    dev_name, di_node_name(node), di_instance(node),
4747c478bd9Sstevel@tonic-gate 		    di_minor_spectype(minor),
4757c478bd9Sstevel@tonic-gate 		    (di_minor_nodetype(minor) != NULL ?
4767c478bd9Sstevel@tonic-gate 		    di_minor_nodetype(minor) : "NULL"));
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (bus_type(node, minor, args->ph) != NULL) {
4807c478bd9Sstevel@tonic-gate 		if (add_bus(args, node, minor, NULL) == NULL) {
4817c478bd9Sstevel@tonic-gate 			args->dev_walk_status = ENOMEM;
4827c478bd9Sstevel@tonic-gate 			result = DI_WALK_TERMINATE;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 
485*c470f575SYuri Pankov 	} else if (is_ctrl(node, minor)) {
4867c478bd9Sstevel@tonic-gate 		if (add_controller(args, node, minor) == NULL) {
4877c478bd9Sstevel@tonic-gate 			args->dev_walk_status = ENOMEM;
4887c478bd9Sstevel@tonic-gate 			result = DI_WALK_TERMINATE;
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 
491e7cbe64fSgw25295 	} else if (di_minor_spectype(minor) == S_IFCHR &&
492e7cbe64fSgw25295 	    (is_drive(minor) || is_zvol(node, minor))) {
4937c478bd9Sstevel@tonic-gate 		char	*devidstr;
4947c478bd9Sstevel@tonic-gate 		char	kernel_name[MAXPATHLEN];
4957c478bd9Sstevel@tonic-gate 		disk_t	*diskp;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
4987c478bd9Sstevel@tonic-gate 		    di_node_name(node), di_instance(node));
4997c478bd9Sstevel@tonic-gate 		devidstr = get_str_prop(DEVICE_ID_PROP, node);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		args->node = node;
5027c478bd9Sstevel@tonic-gate 		args->minor = minor;
50342f64fdbSSarah Jelinek 		/*
50442f64fdbSSarah Jelinek 		 * Check if we already got this disk and
50542f64fdbSSarah Jelinek 		 * this is another slice.
50642f64fdbSSarah Jelinek 		 */
5077c478bd9Sstevel@tonic-gate 		if (!have_disk(args, devidstr, kernel_name, &diskp)) {
5087c478bd9Sstevel@tonic-gate 			args->dev_walk_status = 0;
50942f64fdbSSarah Jelinek 			/*
51042f64fdbSSarah Jelinek 			 * This is a newly found disk, create the
51142f64fdbSSarah Jelinek 			 * disk structure.
51242f64fdbSSarah Jelinek 			 */
5137c478bd9Sstevel@tonic-gate 			diskp = create_disk(devidstr, kernel_name, args);
5147c478bd9Sstevel@tonic-gate 			if (diskp == NULL) {
5157c478bd9Sstevel@tonic-gate 				args->dev_walk_status = ENOMEM;
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 			if (diskp->drv_type != DM_DT_FLOPPY) {
5197c478bd9Sstevel@tonic-gate 				/* add the controller relationship */
5207c478bd9Sstevel@tonic-gate 				if (args->dev_walk_status == 0) {
52142f64fdbSSarah Jelinek 					if (add_disk2controller(diskp,
52242f64fdbSSarah Jelinek 					    args) != 0) {
5237c478bd9Sstevel@tonic-gate 						args->dev_walk_status = ENOMEM;
5247c478bd9Sstevel@tonic-gate 					}
5257c478bd9Sstevel@tonic-gate 				}
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 		}
528681d9761SEric Taylor 		if (is_zvol(node, minor)) {
529681d9761SEric Taylor 			char zvdsk[MAXNAMELEN];
530681d9761SEric Taylor 			char *str;
531681d9761SEric Taylor 			alias_t *ap;
532681d9761SEric Taylor 
533681d9761SEric Taylor 			if (di_prop_lookup_strings(di_minor_devt(minor),
534681d9761SEric Taylor 			    node, "name", &str) == -1)
535681d9761SEric Taylor 				return (DI_WALK_CONTINUE);
536681d9761SEric Taylor 			(void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s",
537681d9761SEric Taylor 			    str);
538681d9761SEric Taylor 			if ((ap = find_alias(diskp, kernel_name)) == NULL) {
539681d9761SEric Taylor 				if (new_alias(diskp, kernel_name,
540681d9761SEric Taylor 				    zvdsk, args) != 0) {
541681d9761SEric Taylor 					args->dev_walk_status = ENOMEM;
542681d9761SEric Taylor 				}
543681d9761SEric Taylor 			} else {
544681d9761SEric Taylor 				/*
54542f64fdbSSarah Jelinek 				 * It is possible that we have already added
54642f64fdbSSarah Jelinek 				 * this devpath.
54742f64fdbSSarah Jelinek 				 * Do not add it again. new_devpath will
54842f64fdbSSarah Jelinek 				 * return a 0 if found, and not add the path.
549681d9761SEric Taylor 				 */
550681d9761SEric Taylor 				if (new_devpath(ap, zvdsk) != 0) {
551681d9761SEric Taylor 					args->dev_walk_status = ENOMEM;
552681d9761SEric Taylor 				}
553681d9761SEric Taylor 			}
554681d9761SEric Taylor 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/* Add the devpaths for the drive. */
5577c478bd9Sstevel@tonic-gate 		if (args->dev_walk_status == 0) {
5587c478bd9Sstevel@tonic-gate 			char	*devpath;
5597c478bd9Sstevel@tonic-gate 			char	slice_path[MAXPATHLEN];
5607c478bd9Sstevel@tonic-gate 			char	*pattern;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			/*
56342f64fdbSSarah Jelinek 			 * We will come through here once for each of
56442f64fdbSSarah Jelinek 			 * the raw slice device names.
5657c478bd9Sstevel@tonic-gate 			 */
5667c478bd9Sstevel@tonic-gate 			devpath = di_devfs_path(node);
56742f64fdbSSarah Jelinek 			(void) snprintf(slice_path,
56842f64fdbSSarah Jelinek 			    sizeof (slice_path), "%s:%s",
5697c478bd9Sstevel@tonic-gate 			    devpath, di_minor_name(minor));
5707c478bd9Sstevel@tonic-gate 			di_devfs_path_free((void *) devpath);
5717c478bd9Sstevel@tonic-gate 
57242f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(di_minor_nodetype(minor),
57342f64fdbSSarah Jelinek 			    DDI_NT_FD)) {
5747c478bd9Sstevel@tonic-gate 				pattern = DEVLINK_FLOPPY_REGEX;
5757c478bd9Sstevel@tonic-gate 			} else {
5767c478bd9Sstevel@tonic-gate 				pattern = DEVLINK_REGEX;
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 			/* Walk the /dev tree to get the devlinks. */
58042f64fdbSSarah Jelinek 			(void) di_devlink_walk(args->handle, pattern,
58142f64fdbSSarah Jelinek 			    slice_path, DI_PRIMARY_LINK, arg, add_devpath);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		if (args->dev_walk_status != 0) {
5857c478bd9Sstevel@tonic-gate 			result = DI_WALK_TERMINATE;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	return (result);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate static int
5937c478bd9Sstevel@tonic-gate add_disk2controller(disk_t *diskp, struct search_args *args)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
5967c478bd9Sstevel@tonic-gate 	controller_t	*cp;
5977c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
5987c478bd9Sstevel@tonic-gate 	di_node_t	node;
5997c478bd9Sstevel@tonic-gate 	int		i;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	node = args->node;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
6047c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
6057c478bd9Sstevel@tonic-gate 		return (0);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	minor = di_minor_next(pnode, NULL);
6097c478bd9Sstevel@tonic-gate 	if (minor == NULL) {
6107c478bd9Sstevel@tonic-gate 		return (0);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if ((cp = add_controller(args, pnode, minor)) == NULL) {
6147c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/* check if the disk <-> ctrl assoc is already there */
6187c478bd9Sstevel@tonic-gate 	for (i = 0; diskp->controllers[i]; i++) {
6197c478bd9Sstevel@tonic-gate 		if (cp == diskp->controllers[i]) {
6207c478bd9Sstevel@tonic-gate 			return (0);
6217c478bd9Sstevel@tonic-gate 		}
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/* this is a new controller for this disk */
6257c478bd9Sstevel@tonic-gate 
626*c470f575SYuri Pankov 	/* add the disk to the controller */
6277c478bd9Sstevel@tonic-gate 	if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
6287c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
631*c470f575SYuri Pankov 	/* add the controller to the disk */
6327c478bd9Sstevel@tonic-gate 	if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
6337c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/*
6377c478bd9Sstevel@tonic-gate 	 * Set up paths for mpxio controlled drives.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
6407c478bd9Sstevel@tonic-gate 		/* note: mpxio di_path stuff is all consolidation private */
6417c478bd9Sstevel@tonic-gate 		di_path_t   pi = DI_PATH_NIL;
6427c478bd9Sstevel@tonic-gate 
64342f64fdbSSarah Jelinek 		while (
64442f64fdbSSarah Jelinek 		    (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
6457c478bd9Sstevel@tonic-gate 			int	cnt;
6467c478bd9Sstevel@tonic-gate 			uchar_t	*bytes;
6477c478bd9Sstevel@tonic-gate 			char	str[MAXPATHLEN];
6487c478bd9Sstevel@tonic-gate 			char	*wwn;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 			di_node_t phci_node = di_path_phci_node(pi);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			/* get the node wwn */
6537c478bd9Sstevel@tonic-gate 			cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
6547c478bd9Sstevel@tonic-gate 			wwn = NULL;
6557c478bd9Sstevel@tonic-gate 			if (cnt > 0) {
6567c478bd9Sstevel@tonic-gate 				int	i;
6577c478bd9Sstevel@tonic-gate 				str[0] = 0;
6587c478bd9Sstevel@tonic-gate 
65942f64fdbSSarah Jelinek 				for (i = 0; i < cnt; i++) {
66042f64fdbSSarah Jelinek 					/*
66142f64fdbSSarah Jelinek 					 * A byte is only 2 hex chars + null.
66242f64fdbSSarah Jelinek 					 */
66342f64fdbSSarah Jelinek 					char bstr[8];
66442f64fdbSSarah Jelinek 
66542f64fdbSSarah Jelinek 					(void) snprintf(bstr,
66642f64fdbSSarah Jelinek 					    sizeof (bstr), "%.2x", bytes[i]);
6677c478bd9Sstevel@tonic-gate 					(void) strlcat(str, bstr, sizeof (str));
6687c478bd9Sstevel@tonic-gate 				}
6697c478bd9Sstevel@tonic-gate 				wwn = str;
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 
67242f64fdbSSarah Jelinek 			if (new_path(cp, diskp, phci_node,
67342f64fdbSSarah Jelinek 			    di_path_state(pi), wwn) == NULL) {
6747c478bd9Sstevel@tonic-gate 				return (ENOMEM);
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static int
6837c478bd9Sstevel@tonic-gate add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
6867c478bd9Sstevel@tonic-gate 	if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
6877c478bd9Sstevel@tonic-gate 		cache_free_path(pp);
6887c478bd9Sstevel@tonic-gate 		return (0);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/* add the path to the disk */
6927c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
6937c478bd9Sstevel@tonic-gate 		cache_free_path(pp);
6947c478bd9Sstevel@tonic-gate 		return (0);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
6987c478bd9Sstevel@tonic-gate 	if (add_int2array(st, &pp->states) != 0) {
6997c478bd9Sstevel@tonic-gate 		cache_free_path(pp);
7007c478bd9Sstevel@tonic-gate 		return (0);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7047c478bd9Sstevel@tonic-gate 	if (wwn != NULL) {
7057c478bd9Sstevel@tonic-gate 		char	*wp;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if ((wp = strdup(wwn)) != NULL) {
7087c478bd9Sstevel@tonic-gate 			if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
7097c478bd9Sstevel@tonic-gate 				cache_free_path(pp);
7107c478bd9Sstevel@tonic-gate 				return (0);
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	return (1);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate static int
7197c478bd9Sstevel@tonic-gate add_int2array(int p, int **parray)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	int		i;
7227c478bd9Sstevel@tonic-gate 	int		cnt;
7237c478bd9Sstevel@tonic-gate 	int		*pa;
7247c478bd9Sstevel@tonic-gate 	int		*new_array;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	pa = *parray;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	cnt = 0;
7297c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
730602ca9eaScth 		for (; pa[cnt] != -1; cnt++)
731602ca9eaScth 			;
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	new_array = (int *)calloc(cnt + 2, sizeof (int *));
7357c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
7367c478bd9Sstevel@tonic-gate 		return (ENOMEM);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/* copy the existing array */
7407c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
7417c478bd9Sstevel@tonic-gate 		new_array[i] = pa[i];
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	new_array[i] = p;
7457c478bd9Sstevel@tonic-gate 	new_array[i + 1] = -1;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	free(pa);
7487c478bd9Sstevel@tonic-gate 	*parray = new_array;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate static int
7547c478bd9Sstevel@tonic-gate add_ptr2array(void *p, void ***parray)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	int		i;
7577c478bd9Sstevel@tonic-gate 	int		cnt;
7587c478bd9Sstevel@tonic-gate 	void		**pa;
7597c478bd9Sstevel@tonic-gate 	void		**new_array;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	pa = *parray;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	cnt = 0;
7647c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
765602ca9eaScth 		for (; pa[cnt]; cnt++)
766602ca9eaScth 			;
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	new_array = (void **)calloc(cnt + 2, sizeof (void *));
7707c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
7717c478bd9Sstevel@tonic-gate 		return (ENOMEM);
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/* copy the existing array */
7757c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
7767c478bd9Sstevel@tonic-gate 		new_array[i] = pa[i];
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	new_array[i] = p;
7807c478bd9Sstevel@tonic-gate 	new_array[i + 1] = NULL;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	free(pa);
7837c478bd9Sstevel@tonic-gate 	*parray = new_array;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	return (0);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate /*
78942f64fdbSSarah Jelinek  * This function checks to see if a controller has other associations
79042f64fdbSSarah Jelinek  * that may be valid. If we are calling this function, we have found that
79142f64fdbSSarah Jelinek  * a controller for an mpxio device is showing up independently of the
79242f64fdbSSarah Jelinek  * mpxio controller, noted as /scsi_vhci. This can happen with some FC
79342f64fdbSSarah Jelinek  * cards that have inbound management devices that show up as well, with
79442f64fdbSSarah Jelinek  * the real controller data associated. We do not want to display these
79542f64fdbSSarah Jelinek  * 'devices' as real devices in libdiskmgt.
7967c478bd9Sstevel@tonic-gate  */
79742f64fdbSSarah Jelinek static void
79842f64fdbSSarah Jelinek remove_controller(controller_t *cp, controller_t *currp)
7997c478bd9Sstevel@tonic-gate {
80042f64fdbSSarah Jelinek 	int	i;
80142f64fdbSSarah Jelinek 
8027c478bd9Sstevel@tonic-gate 	if (cp == currp) {
80342f64fdbSSarah Jelinek 		if (dm_debug) {
80442f64fdbSSarah Jelinek 			(void) fprintf(stderr, "ERROR: removing current"
80542f64fdbSSarah Jelinek 			    " controller\n");
80642f64fdbSSarah Jelinek 		}
80742f64fdbSSarah Jelinek 		return;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if (cp->disks != NULL && cp->disks[0] != NULL) {
81142f64fdbSSarah Jelinek 		if (dm_debug) {
8127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
81342f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
81442f64fdbSSarah Jelinek 			    " with disk ptrs.\n");
8157c478bd9Sstevel@tonic-gate 		}
81642f64fdbSSarah Jelinek 		/*
81742f64fdbSSarah Jelinek 		 * loop through the disks and remove the reference to the
81842f64fdbSSarah Jelinek 		 * controller for this disk structure. The disk itself
81942f64fdbSSarah Jelinek 		 * is still a valid device, the controller being removed
82042f64fdbSSarah Jelinek 		 * is a 'path' so any disk that has a reference to it
82142f64fdbSSarah Jelinek 		 * as a controller needs to have this reference removed.
82242f64fdbSSarah Jelinek 		 */
8239729663dSGeorge Wilson 		for (i = 0; cp->disks[i]; i++) {
8249729663dSGeorge Wilson 			disk_t *dp = cp->disks[i];
82542f64fdbSSarah Jelinek 			int j;
82642f64fdbSSarah Jelinek 
8279729663dSGeorge Wilson 			for (j = 0; dp->controllers[j]; j++) {
8289729663dSGeorge Wilson 				int k;
8299729663dSGeorge Wilson 
8309729663dSGeorge Wilson 				if (libdiskmgt_str_eq(dp->controllers[j]->name,
8319729663dSGeorge Wilson 				    cp->name)) {
8329729663dSGeorge Wilson 
8339729663dSGeorge Wilson 					if (dm_debug) {
8349729663dSGeorge Wilson 						(void) fprintf(stderr,
8359729663dSGeorge Wilson 						    "INFO: REMOVING disk %s on "
8369729663dSGeorge Wilson 						    "controller %s\n",
8379729663dSGeorge Wilson 						    dp->kernel_name, cp->name);
8389729663dSGeorge Wilson 					}
8399729663dSGeorge Wilson 					for (k = j; dp->controllers[k]; k++) {
8409729663dSGeorge Wilson 						dp->controllers[k] =
8419729663dSGeorge Wilson 						    dp->controllers[k + 1];
84242f64fdbSSarah Jelinek 					}
84342f64fdbSSarah Jelinek 				}
84442f64fdbSSarah Jelinek 			}
84542f64fdbSSarah Jelinek 		}
84642f64fdbSSarah Jelinek 	}
84742f64fdbSSarah Jelinek 	/*
84842f64fdbSSarah Jelinek 	 * Paths are removed with the call to cache_free_controller()
84942f64fdbSSarah Jelinek 	 * below.
85042f64fdbSSarah Jelinek 	 */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (cp->paths != NULL && cp->paths[0] != NULL) {
85342f64fdbSSarah Jelinek 		if (dm_debug) {
8547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
85542f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
85642f64fdbSSarah Jelinek 			    " with path ptrs. \n");
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 	}
85942f64fdbSSarah Jelinek 	cache_free_controller(cp);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * If we have a controller in the list that is really a path then we need to
8647c478bd9Sstevel@tonic-gate  * take that controller out of the list since nodes that are paths are not
8657c478bd9Sstevel@tonic-gate  * considered to be controllers.
8667c478bd9Sstevel@tonic-gate  */
8677c478bd9Sstevel@tonic-gate static void
8687c478bd9Sstevel@tonic-gate clean_paths(struct search_args *args)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	controller_t	*cp;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
8737c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
8747c478bd9Sstevel@tonic-gate 		path_t	**pp;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		pp = cp->paths;
8777c478bd9Sstevel@tonic-gate 		if (pp != NULL) {
8787c478bd9Sstevel@tonic-gate 			int i;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			for (i = 0; pp[i]; i++) {
88142f64fdbSSarah Jelinek 				remove_invalid_controller(pp[i]->name, cp,
88242f64fdbSSarah Jelinek 				    args);
8837c478bd9Sstevel@tonic-gate 			}
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 		cp = cp->next;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate static disk_t *
8907c478bd9Sstevel@tonic-gate create_disk(char *deviceid, char *kernel_name, struct search_args *args)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate 	disk_t	*diskp;
8937c478bd9Sstevel@tonic-gate 	char	*type;
8947c478bd9Sstevel@tonic-gate 	char	*prod_id;
8957c478bd9Sstevel@tonic-gate 	char	*vendor_id;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if (dm_debug) {
8987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	diskp = calloc(1, sizeof (disk_t));
9027c478bd9Sstevel@tonic-gate 	if (diskp == NULL) {
9037c478bd9Sstevel@tonic-gate 		return (NULL);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	diskp->controllers = (controller_t **)
9077c478bd9Sstevel@tonic-gate 	    calloc(1, sizeof (controller_t *));
9087c478bd9Sstevel@tonic-gate 	if (diskp->controllers == NULL) {
9097c478bd9Sstevel@tonic-gate 		cache_free_disk(diskp);
9107c478bd9Sstevel@tonic-gate 		return (NULL);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	diskp->controllers[0] = NULL;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	diskp->devid = NULL;
9157c478bd9Sstevel@tonic-gate 	if (deviceid != NULL) {
9167c478bd9Sstevel@tonic-gate 		if ((diskp->device_id = strdup(deviceid)) == NULL) {
9177c478bd9Sstevel@tonic-gate 			cache_free_disk(diskp);
9187c478bd9Sstevel@tonic-gate 			return (NULL);
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 		(void) devid_str_decode(deviceid, &(diskp->devid), NULL);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
9247c478bd9Sstevel@tonic-gate 		diskp->kernel_name = strdup(kernel_name);
9257c478bd9Sstevel@tonic-gate 		if (diskp->kernel_name == NULL) {
9267c478bd9Sstevel@tonic-gate 			cache_free_disk(diskp);
9277c478bd9Sstevel@tonic-gate 			return (NULL);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	diskp->paths = NULL;
9327c478bd9Sstevel@tonic-gate 	diskp->aliases = NULL;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	diskp->cd_rom = 0;
9357c478bd9Sstevel@tonic-gate 	diskp->rpm = 0;
93659d8f100SGarrett D'Amore 	diskp->solid_state = -1;
9377c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(args->minor);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	prod_id = get_str_prop(PROD_ID_PROP, args->node);
9407c478bd9Sstevel@tonic-gate 	if (prod_id != NULL) {
9417c478bd9Sstevel@tonic-gate 		if ((diskp->product_id = strdup(prod_id)) == NULL) {
9427c478bd9Sstevel@tonic-gate 			cache_free_disk(diskp);
9437c478bd9Sstevel@tonic-gate 			return (NULL);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	} else {
9467c478bd9Sstevel@tonic-gate 		prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
9477c478bd9Sstevel@tonic-gate 		if (prod_id != NULL) {
9487c478bd9Sstevel@tonic-gate 			if ((diskp->product_id = strdup(prod_id)) == NULL) {
9497c478bd9Sstevel@tonic-gate 				cache_free_disk(diskp);
9507c478bd9Sstevel@tonic-gate 				return (NULL);
9517c478bd9Sstevel@tonic-gate 			}
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
9567c478bd9Sstevel@tonic-gate 	if (vendor_id != NULL) {
9577c478bd9Sstevel@tonic-gate 		if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
9587c478bd9Sstevel@tonic-gate 			cache_free_disk(diskp);
9597c478bd9Sstevel@tonic-gate 			return (NULL);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	} else {
962e4adc82dSHans Rosenfeld 		vendor_id = get_str_prop(VENDOR_ID_USB_PROP, args->node);
9637c478bd9Sstevel@tonic-gate 		if (vendor_id != NULL) {
9647c478bd9Sstevel@tonic-gate 			if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
9657c478bd9Sstevel@tonic-gate 				cache_free_disk(diskp);
9667c478bd9Sstevel@tonic-gate 				return (NULL);
9677c478bd9Sstevel@tonic-gate 			}
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
9737c478bd9Sstevel@tonic-gate 	 * We try to use uscsi later to determine the real type.
9747c478bd9Sstevel@tonic-gate 	 * The cd_rom flag tells us that the kernel categorized the drive
975*c470f575SYuri Pankov 	 * as a CD-ROM.  We leave the drv_type as UNKNOWN for now.
9767c478bd9Sstevel@tonic-gate 	 * The combination of the cd_rom flag being set with the drv_type of
9777c478bd9Sstevel@tonic-gate 	 * unknown is what triggers the uscsi probe in drive.c.
9787c478bd9Sstevel@tonic-gate 	 */
9797c478bd9Sstevel@tonic-gate 	if (disk_is_cdrom(type)) {
9807c478bd9Sstevel@tonic-gate 		diskp->drv_type = DM_DT_UNKNOWN;
9817c478bd9Sstevel@tonic-gate 		diskp->cd_rom = 1;
9827c478bd9Sstevel@tonic-gate 		diskp->removable = 1;
9837c478bd9Sstevel@tonic-gate 	} else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
9847c478bd9Sstevel@tonic-gate 		diskp->drv_type = DM_DT_FLOPPY;
9857c478bd9Sstevel@tonic-gate 		diskp->removable = 1;
9867c478bd9Sstevel@tonic-gate 	} else {
987*c470f575SYuri Pankov 		/* not a CD-ROM or Floppy */
9887c478bd9Sstevel@tonic-gate 		diskp->removable = get_prop(REMOVABLE_PROP, args->node);
9890167b58cScg149915 
9900167b58cScg149915 		if (diskp->removable == -1) {
9917c478bd9Sstevel@tonic-gate 			diskp->removable = 0;
9927c478bd9Sstevel@tonic-gate 			diskp->drv_type = DM_DT_FIXED;
9937c478bd9Sstevel@tonic-gate 		}
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	diskp->next = args->disk_listp;
9977c478bd9Sstevel@tonic-gate 	args->disk_listp = diskp;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	return (diskp);
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate static char *
10037c478bd9Sstevel@tonic-gate ctype(di_node_t node, di_minor_t minor)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	char	*type;
10067c478bd9Sstevel@tonic-gate 	char	*name;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
10097c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/* IDE disks use SCSI nexus as the type, so handle this special case */
1012*c470f575SYuri Pankov 	if ((libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1013*c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_PSEUDO)) &&
1014*c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "ide"))
10157c478bd9Sstevel@tonic-gate 		return (DM_CTYPE_ATA);
10167c478bd9Sstevel@tonic-gate 
1017*c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT) ||
1018*c470f575SYuri Pankov 	    (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
1019*c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "fp")))
1020*c470f575SYuri Pankov 		return (DM_CTYPE_FIBRE);
1021*c470f575SYuri Pankov 
1022*c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_NVME_ATTACHMENT_POINT))
1023*c470f575SYuri Pankov 		return (DM_CTYPE_NVME);
1024*c470f575SYuri Pankov 
1025*c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_SATA_NEXUS) ||
1026*c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_NT_SATA_ATTACHMENT_POINT))
1027*c470f575SYuri Pankov 		return (DM_CTYPE_SATA);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1030*c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT))
10317c478bd9Sstevel@tonic-gate 		return (DM_CTYPE_SCSI);
10327c478bd9Sstevel@tonic-gate 
1033*c470f575SYuri Pankov 	if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb"))
1034*c470f575SYuri Pankov 		return (DM_CTYPE_USB);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1037*c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "xpvd"))
1038*c470f575SYuri Pankov 		return (DM_CTYPE_XEN);
1039*c470f575SYuri Pankov 
1040*c470f575SYuri Pankov 	if (dm_debug) {
1041*c470f575SYuri Pankov 		(void) fprintf(stderr,
1042*c470f575SYuri Pankov 		    "INFO: unknown controller type=%s name=%s\n", type, name);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	return (DM_CTYPE_UNKNOWN);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate static boolean_t
104997ab9a43SJohn Levon disk_is_cdrom(const char *type)
10507c478bd9Sstevel@tonic-gate {
105197ab9a43SJohn Levon 	return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate static alias_t *
10557c478bd9Sstevel@tonic-gate find_alias(disk_t *diskp, char *kernel_name)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	alias_t	*ap;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
10607c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
10617c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
10627c478bd9Sstevel@tonic-gate 			return (ap);
10637c478bd9Sstevel@tonic-gate 		}
10647c478bd9Sstevel@tonic-gate 		ap = ap->next;
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	return (NULL);
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate static bus_t *
10717c478bd9Sstevel@tonic-gate find_bus(struct search_args *args, char *name)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	bus_t *listp;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	listp = args->bus_listp;
10767c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
10777c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(listp->name, name)) {
10787c478bd9Sstevel@tonic-gate 			return (listp);
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 		listp = listp->next;
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	return (NULL);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate static controller_t *
10877c478bd9Sstevel@tonic-gate find_controller(struct search_args *args, char *name)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate 	controller_t *listp;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	listp = args->controller_listp;
10927c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
10937c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(listp->name, name)) {
10947c478bd9Sstevel@tonic-gate 			return (listp);
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 		listp = listp->next;
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	return (NULL);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id.
11047c478bd9Sstevel@tonic-gate  * We got the device id from the dev tree walk.  This is encoded
11057c478bd9Sstevel@tonic-gate  * using devid_str_encode(3DEVID).   In order to check the device ids we need
11067c478bd9Sstevel@tonic-gate  * to use the devid_compare(3DEVID) function, so we need to decode the
11077c478bd9Sstevel@tonic-gate  * string representation of the device id.
11087c478bd9Sstevel@tonic-gate  */
11097c478bd9Sstevel@tonic-gate static disk_t *
11107c478bd9Sstevel@tonic-gate get_disk_by_deviceid(disk_t *listp, char *devidstr)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
11157c478bd9Sstevel@tonic-gate 		return (NULL);
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
11197c478bd9Sstevel@tonic-gate 		if (listp->devid != NULL &&
11207c478bd9Sstevel@tonic-gate 		    devid_compare(listp->devid, devid) == 0) {
11217c478bd9Sstevel@tonic-gate 			break;
11227c478bd9Sstevel@tonic-gate 		}
11237c478bd9Sstevel@tonic-gate 		listp = listp->next;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	devid_free(devid);
11277c478bd9Sstevel@tonic-gate 	return (listp);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Get the base disk name with no path prefix and no slice (if there is one).
11327c478bd9Sstevel@tonic-gate  * The name parameter should be big enough to hold the name.
11337c478bd9Sstevel@tonic-gate  * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
11347c478bd9Sstevel@tonic-gate  * and converts the raw diskette name.
11357c478bd9Sstevel@tonic-gate  * But, we don't know how to strip off the slice from third party drive
11367c478bd9Sstevel@tonic-gate  * names.  That just means that their drive name will include a slice on
11377c478bd9Sstevel@tonic-gate  * it.
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate static void
11407c478bd9Sstevel@tonic-gate get_disk_name_from_path(char *path, char *name, int size)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	char		*basep;
11437c478bd9Sstevel@tonic-gate 	int		cnt = 0;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	basep = strrchr(path, '/');
11467c478bd9Sstevel@tonic-gate 	if (basep == NULL) {
11477c478bd9Sstevel@tonic-gate 		basep = path;
11487c478bd9Sstevel@tonic-gate 	} else {
11497c478bd9Sstevel@tonic-gate 		basep++;
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	size = size - 1;	/* leave room for terminating 0 */
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (is_ctds(basep)) {
11557c478bd9Sstevel@tonic-gate 		while (*basep != 0 && *basep != 's' && cnt < size) {
11567c478bd9Sstevel@tonic-gate 			*name++ = *basep++;
11577c478bd9Sstevel@tonic-gate 				cnt++;
11587c478bd9Sstevel@tonic-gate 		}
11597c478bd9Sstevel@tonic-gate 		*name = 0;
11607c478bd9Sstevel@tonic-gate 	} else {
116142f64fdbSSarah Jelinek 		if (strncmp(basep, FLOPPY_NAME,
116242f64fdbSSarah Jelinek 		    sizeof (FLOPPY_NAME) - 1) == 0) {
11637c478bd9Sstevel@tonic-gate 			/*
11647c478bd9Sstevel@tonic-gate 			 * a floppy, convert rdiskette name to diskette name,
11657c478bd9Sstevel@tonic-gate 			 * by skipping over the 'r' for raw diskette
11667c478bd9Sstevel@tonic-gate 			 */
11677c478bd9Sstevel@tonic-gate 			basep++;
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		/* not a ctds name, just copy it */
11717c478bd9Sstevel@tonic-gate 		(void) strlcpy(name, basep, size);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate static char *
11767c478bd9Sstevel@tonic-gate get_byte_prop(char *prop_name, di_node_t node)
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 	int	cnt;
11797c478bd9Sstevel@tonic-gate 	uchar_t	*bytes;
11807c478bd9Sstevel@tonic-gate 	int	i;
11817c478bd9Sstevel@tonic-gate 	char	str[MAXPATHLEN];
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
11847c478bd9Sstevel@tonic-gate 	if (cnt < 1) {
11857c478bd9Sstevel@tonic-gate 		return (NULL);
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	str[0] = 0;
11897c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
11907c478bd9Sstevel@tonic-gate 		char bstr[8];	/* a byte is only 2 hex chars + null */
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		(void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
11937c478bd9Sstevel@tonic-gate 		(void) strlcat(str, bstr, sizeof (str));
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	return (strdup(str));
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate static di_node_t
11997c478bd9Sstevel@tonic-gate get_parent_bus(di_node_t node, struct search_args *args)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	di_node_t pnode;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
12047c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
12057c478bd9Sstevel@tonic-gate 		return (NULL);
12067c478bd9Sstevel@tonic-gate 	}
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
12097c478bd9Sstevel@tonic-gate 		return (pnode);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	return (get_parent_bus(pnode, args));
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate static int
12167c478bd9Sstevel@tonic-gate get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	int *n;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
12217c478bd9Sstevel@tonic-gate 		return (*n);
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	return (0);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate static char *
12287c478bd9Sstevel@tonic-gate get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	char *str;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
12337c478bd9Sstevel@tonic-gate 		return (str);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	return (NULL);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /*
12407c478bd9Sstevel@tonic-gate  * Get one of the positive int or boolean properties.
12417c478bd9Sstevel@tonic-gate  */
12427c478bd9Sstevel@tonic-gate static int
12437c478bd9Sstevel@tonic-gate get_prop(char *prop_name, di_node_t node)
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	int num;
12467c478bd9Sstevel@tonic-gate 	int *ip;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
12497c478bd9Sstevel@tonic-gate 	    >= 0) {
12507c478bd9Sstevel@tonic-gate 		if (num == 0) {
12517c478bd9Sstevel@tonic-gate 			/* boolean */
12527c478bd9Sstevel@tonic-gate 			return (1);
12537c478bd9Sstevel@tonic-gate 		} else if (num == 1) {
12547c478bd9Sstevel@tonic-gate 			/* single int */
12557c478bd9Sstevel@tonic-gate 			return (*ip);
12567c478bd9Sstevel@tonic-gate 		}
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	return (-1);
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate static char *
12627c478bd9Sstevel@tonic-gate get_str_prop(char *prop_name, di_node_t node)
12637c478bd9Sstevel@tonic-gate {
12647c478bd9Sstevel@tonic-gate 	char *str;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
12677c478bd9Sstevel@tonic-gate 		return (str);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (NULL);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id, if the
12757c478bd9Sstevel@tonic-gate  * drive has a device id, or the kernel name, if it doesn't have a device id.
12767c478bd9Sstevel@tonic-gate  */
12777c478bd9Sstevel@tonic-gate static int
12787c478bd9Sstevel@tonic-gate have_disk(struct search_args *args, char *devidstr, char *kernel_name,
12797c478bd9Sstevel@tonic-gate     disk_t **diskp)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	disk_t *listp;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	*diskp = NULL;
12847c478bd9Sstevel@tonic-gate 	listp = args->disk_listp;
12857c478bd9Sstevel@tonic-gate 	if (devidstr != NULL) {
12867c478bd9Sstevel@tonic-gate 		if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
12877c478bd9Sstevel@tonic-gate 			return (1);
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	} else {
12917c478bd9Sstevel@tonic-gate 		/* no devid, try matching the kernel names on the drives */
12927c478bd9Sstevel@tonic-gate 		while (listp != NULL) {
129342f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(kernel_name,
129442f64fdbSSarah Jelinek 			    listp->kernel_name)) {
12957c478bd9Sstevel@tonic-gate 				*diskp = listp;
12967c478bd9Sstevel@tonic-gate 				return (1);
12977c478bd9Sstevel@tonic-gate 			}
12987c478bd9Sstevel@tonic-gate 			listp = listp->next;
12997c478bd9Sstevel@tonic-gate 		}
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	return (0);
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate static char *
13057c478bd9Sstevel@tonic-gate bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	char	*type;
13087c478bd9Sstevel@tonic-gate 	int	i;
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	type = get_prom_str("device_type", node, ph);
13117c478bd9Sstevel@tonic-gate 	if (type == NULL) {
13127c478bd9Sstevel@tonic-gate 		type = di_node_name(node);
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	for (i = 0; bustypes[i]; i++) {
13167c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(type, bustypes[i])) {
13177c478bd9Sstevel@tonic-gate 			return (type);
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	if (minor != NULL && strcmp(di_minor_nodetype(minor),
13227c478bd9Sstevel@tonic-gate 	    DDI_NT_USB_ATTACHMENT_POINT) == 0) {
13237c478bd9Sstevel@tonic-gate 		return ("usb");
13247c478bd9Sstevel@tonic-gate 	}
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	return (NULL);
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /*
13307c478bd9Sstevel@tonic-gate  * If the input name is in c[t]ds format then return 1, otherwise return 0.
13317c478bd9Sstevel@tonic-gate  */
13327c478bd9Sstevel@tonic-gate static int
13337c478bd9Sstevel@tonic-gate is_ctds(char *name)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate 	char	*p;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	p = name;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	if (*p++ != 'c') {
13407c478bd9Sstevel@tonic-gate 		return (0);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	/* skip controller digits */
13437c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
13447c478bd9Sstevel@tonic-gate 		p++;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/* handle optional target */
13487c478bd9Sstevel@tonic-gate 	if (*p == 't') {
13497c478bd9Sstevel@tonic-gate 		p++;
13507c478bd9Sstevel@tonic-gate 		/* skip over target */
13517c478bd9Sstevel@tonic-gate 		while (isdigit(*p) || isupper(*p)) {
13527c478bd9Sstevel@tonic-gate 			p++;
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (*p++ != 'd') {
13577c478bd9Sstevel@tonic-gate 		return (0);
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
13607c478bd9Sstevel@tonic-gate 		p++;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	if (*p++ != 's') {
13647c478bd9Sstevel@tonic-gate 		return (0);
13657c478bd9Sstevel@tonic-gate 	}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	/* check the slice number */
13687c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
13697c478bd9Sstevel@tonic-gate 		p++;
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	if (*p != 0) {
13737c478bd9Sstevel@tonic-gate 		return (0);
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	return (1);
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate static int
13807c478bd9Sstevel@tonic-gate is_drive(di_minor_t minor)
13817c478bd9Sstevel@tonic-gate {
138297ab9a43SJohn Levon 	return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK,
138397ab9a43SJohn Levon 	    strlen(DDI_NT_BLOCK)) == 0);
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate static int
1387e7cbe64fSgw25295 is_zvol(di_node_t node, di_minor_t minor)
1388e7cbe64fSgw25295 {
1389e7cbe64fSgw25295 	if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) &&
1390681d9761SEric Taylor 	    minor(di_minor_devt(minor)))
1391e7cbe64fSgw25295 		return (1);
1392e7cbe64fSgw25295 	return (0);
1393e7cbe64fSgw25295 }
1394e7cbe64fSgw25295 
1395e7cbe64fSgw25295 static int
1396*c470f575SYuri Pankov is_ctrl(di_node_t node, di_minor_t minor)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	char	*type;
13997c478bd9Sstevel@tonic-gate 	char	*name;
14007c478bd9Sstevel@tonic-gate 	int	type_index;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
14037c478bd9Sstevel@tonic-gate 	type_index = 0;
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	while (ctrltypes[type_index] != NULL) {
14067c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
14077c478bd9Sstevel@tonic-gate 			return (1);
14087c478bd9Sstevel@tonic-gate 		}
14097c478bd9Sstevel@tonic-gate 		type_index++;
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
14137c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1414*c470f575SYuri Pankov 	    (libdiskmgt_str_eq(name, "ide") ||
1415*c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "xpvd")))
14167c478bd9Sstevel@tonic-gate 		return (1);
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	return (0);
14197c478bd9Sstevel@tonic-gate }
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate static int
14227c478bd9Sstevel@tonic-gate new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
14237c478bd9Sstevel@tonic-gate     struct search_args *args)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	alias_t		*aliasp;
14267c478bd9Sstevel@tonic-gate 	char		alias[MAXPATHLEN];
14277c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	aliasp = malloc(sizeof (alias_t));
14307c478bd9Sstevel@tonic-gate 	if (aliasp == NULL) {
14317c478bd9Sstevel@tonic-gate 		return (ENOMEM);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	aliasp->alias = NULL;
14357c478bd9Sstevel@tonic-gate 	aliasp->kstat_name = NULL;
14367c478bd9Sstevel@tonic-gate 	aliasp->wwn = NULL;
14377c478bd9Sstevel@tonic-gate 	aliasp->devpaths = NULL;
14387c478bd9Sstevel@tonic-gate 	aliasp->orig_paths = NULL;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	get_disk_name_from_path(devlink_path, alias, sizeof (alias));
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	aliasp->alias = strdup(alias);
14437c478bd9Sstevel@tonic-gate 	if (aliasp->alias == NULL) {
14447c478bd9Sstevel@tonic-gate 		cache_free_alias(aliasp);
14457c478bd9Sstevel@tonic-gate 		return (ENOMEM);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
14497c478bd9Sstevel@tonic-gate 		aliasp->kstat_name = strdup(kernel_name);
14507c478bd9Sstevel@tonic-gate 		if (aliasp->kstat_name == NULL) {
14517c478bd9Sstevel@tonic-gate 			cache_free_alias(aliasp);
14527c478bd9Sstevel@tonic-gate 			return (ENOMEM);
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 	} else {
14557c478bd9Sstevel@tonic-gate 		aliasp->kstat_name = NULL;
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	aliasp->lun = get_prop(DM_LUN, args->node);
14597c478bd9Sstevel@tonic-gate 	aliasp->target = get_prop(DM_TARGET, args->node);
14607c478bd9Sstevel@tonic-gate 	aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(args->node);
14637c478bd9Sstevel@tonic-gate 	if (pnode != DI_NODE_NIL) {
14647c478bd9Sstevel@tonic-gate 		char prop_name[MAXPROPLEN];
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 		(void) snprintf(prop_name, sizeof (prop_name),
14677c478bd9Sstevel@tonic-gate 		    "target%d-sync-speed", aliasp->target);
14687c478bd9Sstevel@tonic-gate 		diskp->sync_speed = get_prop(prop_name, pnode);
14697c478bd9Sstevel@tonic-gate 		(void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
14707c478bd9Sstevel@tonic-gate 		    aliasp->target);
14717c478bd9Sstevel@tonic-gate 		diskp->wide = get_prop(prop_name, pnode);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (new_devpath(aliasp, devlink_path) != 0) {
14757c478bd9Sstevel@tonic-gate 		cache_free_alias(aliasp);
14767c478bd9Sstevel@tonic-gate 		return (ENOMEM);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	aliasp->next = diskp->aliases;
14807c478bd9Sstevel@tonic-gate 	diskp->aliases = aliasp;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	return (0);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /*
14867c478bd9Sstevel@tonic-gate  * Append the new devpath to the end of the devpath list.  This is important
14877c478bd9Sstevel@tonic-gate  * since we may want to use the order of the devpaths to match up the vtoc
14887c478bd9Sstevel@tonic-gate  * entries.
14897c478bd9Sstevel@tonic-gate  */
14907c478bd9Sstevel@tonic-gate static int
14917c478bd9Sstevel@tonic-gate new_devpath(alias_t *ap, char *devpath)
14927c478bd9Sstevel@tonic-gate {
14937c478bd9Sstevel@tonic-gate 	slice_t	*newdp;
14947c478bd9Sstevel@tonic-gate 	slice_t *alistp;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	/*
14977c478bd9Sstevel@tonic-gate 	 * First, search the alias list to be sure that this devpath is
14987c478bd9Sstevel@tonic-gate 	 * not already there.
14997c478bd9Sstevel@tonic-gate 	 */
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
15027c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
15037c478bd9Sstevel@tonic-gate 			return (0);
15047c478bd9Sstevel@tonic-gate 		}
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	/*
15087c478bd9Sstevel@tonic-gate 	 * Otherwise, not found so add this new devpath to the list.
15097c478bd9Sstevel@tonic-gate 	 */
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	newdp = malloc(sizeof (slice_t));
15127c478bd9Sstevel@tonic-gate 	if (newdp == NULL) {
15137c478bd9Sstevel@tonic-gate 		return (ENOMEM);
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	newdp->devpath = strdup(devpath);
15177c478bd9Sstevel@tonic-gate 	if (newdp->devpath == NULL) {
15187c478bd9Sstevel@tonic-gate 		free(newdp);
15197c478bd9Sstevel@tonic-gate 		return (ENOMEM);
15207c478bd9Sstevel@tonic-gate 	}
15217c478bd9Sstevel@tonic-gate 	newdp->slice_num = -1;
15227c478bd9Sstevel@tonic-gate 	newdp->next = NULL;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if (ap->devpaths == NULL) {
15257c478bd9Sstevel@tonic-gate 		ap->devpaths = newdp;
15267c478bd9Sstevel@tonic-gate 	} else {
15277c478bd9Sstevel@tonic-gate 		/* append the devpath to the end of the list */
15287c478bd9Sstevel@tonic-gate 		slice_t	*dp;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 		dp = ap->devpaths;
15317c478bd9Sstevel@tonic-gate 		while (dp->next != NULL) {
15327c478bd9Sstevel@tonic-gate 			dp = dp->next;
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 		dp->next = newdp;
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	return (0);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate static path_t *
15427c478bd9Sstevel@tonic-gate new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
15437c478bd9Sstevel@tonic-gate     char *wwn)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	char		*devpath;
15467c478bd9Sstevel@tonic-gate 	path_t		*pp;
15477c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
15507c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
15517c478bd9Sstevel@tonic-gate 		di_node_t pnode;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		pnode = di_parent_node(node);
15547c478bd9Sstevel@tonic-gate 		if (pnode != DI_NODE_NIL) {
15557c478bd9Sstevel@tonic-gate 			node = pnode;
15567c478bd9Sstevel@tonic-gate 		}
15577c478bd9Sstevel@tonic-gate 	}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	/* check if the path is already there */
15627c478bd9Sstevel@tonic-gate 	pp = NULL;
15637c478bd9Sstevel@tonic-gate 	if (cp->paths != NULL) {
15647c478bd9Sstevel@tonic-gate 		int i;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		for (i = 0; cp->paths[i]; i++) {
15677c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
15687c478bd9Sstevel@tonic-gate 				pp = cp->paths[i];
15697c478bd9Sstevel@tonic-gate 				break;
15707c478bd9Sstevel@tonic-gate 			}
15717c478bd9Sstevel@tonic-gate 		}
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
15757c478bd9Sstevel@tonic-gate 		/* the path exists, add this disk to it */
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
15787c478bd9Sstevel@tonic-gate 		if (!add_disk2path(dp, pp, st, wwn)) {
15797c478bd9Sstevel@tonic-gate 			return (NULL);
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 		return (pp);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	/* create a new path */
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	pp = calloc(1, sizeof (path_t));
15877c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
15887c478bd9Sstevel@tonic-gate 		di_devfs_path_free((void *) devpath);
15897c478bd9Sstevel@tonic-gate 		return (NULL);
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	pp->name = strdup(devpath);
15937c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
15947c478bd9Sstevel@tonic-gate 	if (pp->name == NULL) {
15957c478bd9Sstevel@tonic-gate 		cache_free_path(pp);
15967c478bd9Sstevel@tonic-gate 		return (NULL);
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
16007c478bd9Sstevel@tonic-gate 	if (!add_disk2path(dp, pp, st, wwn)) {
16017c478bd9Sstevel@tonic-gate 		return (NULL);
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	/* add the path to the controller */
16057c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
16067c478bd9Sstevel@tonic-gate 		cache_free_path(pp);
16077c478bd9Sstevel@tonic-gate 		return (NULL);
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	/* add the controller to the path */
16117c478bd9Sstevel@tonic-gate 	pp->controller = cp;
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	minor = di_minor_next(node, NULL);
16147c478bd9Sstevel@tonic-gate 	if (minor != NULL) {
16157c478bd9Sstevel@tonic-gate 		pp->ctype = ctype(node, minor);
16167c478bd9Sstevel@tonic-gate 	} else {
16177c478bd9Sstevel@tonic-gate 		pp->ctype = DM_CTYPE_UNKNOWN;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	return (pp);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate /*
16247c478bd9Sstevel@tonic-gate  * We pass in the current controller pointer (currp) so we can double check
16257c478bd9Sstevel@tonic-gate  * that we aren't corrupting the list by removing the element we are on.  This
16267c478bd9Sstevel@tonic-gate  * should never happen, but it doesn't hurt to double check.
16277c478bd9Sstevel@tonic-gate  */
16287c478bd9Sstevel@tonic-gate static void
16297c478bd9Sstevel@tonic-gate remove_invalid_controller(char *name, controller_t *currp,
16307c478bd9Sstevel@tonic-gate     struct search_args *args)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	controller_t *cp;
16337c478bd9Sstevel@tonic-gate 	bus_t *bp;
16347c478bd9Sstevel@tonic-gate 	controller_t *prevp;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	bp = args->bus_listp;
16377c478bd9Sstevel@tonic-gate 	while (bp != NULL) {
16387c478bd9Sstevel@tonic-gate 		int i;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		for (i = 0; bp->controllers[i]; i++) {
16417c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
16427c478bd9Sstevel@tonic-gate 				int j;
164342f64fdbSSarah Jelinek 				/*
164442f64fdbSSarah Jelinek 				 * remove pointer to invalid controller.
164542f64fdbSSarah Jelinek 				 * (it is a path)
164642f64fdbSSarah Jelinek 				 */
16477c478bd9Sstevel@tonic-gate 				for (j = i; bp->controllers[j]; j++) {
164842f64fdbSSarah Jelinek 					bp->controllers[j] =
164942f64fdbSSarah Jelinek 					    bp->controllers[j + 1];
16507c478bd9Sstevel@tonic-gate 				}
16517c478bd9Sstevel@tonic-gate 			}
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 		bp = bp->next;
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	if (args->controller_listp == NULL) {
16577c478bd9Sstevel@tonic-gate 		return;
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
16617c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->name, name)) {
16627c478bd9Sstevel@tonic-gate 		args->controller_listp = cp->next;
166342f64fdbSSarah Jelinek 		if (dm_debug) {
166442f64fdbSSarah Jelinek 			(void) fprintf(stderr,
166542f64fdbSSarah Jelinek 			    "INFO: Removed controller %s from list\n",
166642f64fdbSSarah Jelinek 			    cp->name);
16677c478bd9Sstevel@tonic-gate 		}
166842f64fdbSSarah Jelinek 		remove_controller(cp, currp);
16697c478bd9Sstevel@tonic-gate 		return;
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	prevp = cp;
16737c478bd9Sstevel@tonic-gate 	cp = cp->next;
16747c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
16757c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(cp->name, name)) {
167642f64fdbSSarah Jelinek 			if (dm_debug) {
167742f64fdbSSarah Jelinek 				(void) fprintf(stderr,
167842f64fdbSSarah Jelinek 				    "INFO: Removed controller %s from list\n",
167942f64fdbSSarah Jelinek 				    cp->name);
16807c478bd9Sstevel@tonic-gate 			}
168142f64fdbSSarah Jelinek 			prevp->next = cp->next;
168242f64fdbSSarah Jelinek 			remove_controller(cp, currp);
16837c478bd9Sstevel@tonic-gate 			return;
16847c478bd9Sstevel@tonic-gate 		}
16857c478bd9Sstevel@tonic-gate 		prevp = cp;
16867c478bd9Sstevel@tonic-gate 		cp = cp->next;
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate }
1689