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  */
21c470f575SYuri Pankov 
227c478bd9Sstevel@tonic-gate /*
2342f64fdbSSarah Jelinek  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25c470f575SYuri Pankov  */
26c470f575SYuri Pankov 
27c470f575SYuri Pankov /*
289729663dSGeorge Wilson  * Copyright (c) 2011 by Delphix. All rights reserved.
29c470f575SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
30*f85f43edSRobert Mustacchi  * Copyright 2021 Oxide Computer Company
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
41681d9761SEric Taylor #include <sys/mkdev.h>
427c478bd9Sstevel@tonic-gate #include <ctype.h>
437c478bd9Sstevel@tonic-gate #include <libgen.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <devid.h>
46e7cbe64fSgw #include <sys/fs/zfs.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
497c478bd9Sstevel@tonic-gate #include "disks_private.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* specify which disk links to use in the /dev directory */
527c478bd9Sstevel@tonic-gate #define	DEVLINK_REGEX		"rdsk/.*"
537c478bd9Sstevel@tonic-gate #define	DEVLINK_FLOPPY_REGEX	"rdiskette[0-9]"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	FLOPPY_NAME	"rdiskette"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	MAXPROPLEN		1024
587c478bd9Sstevel@tonic-gate #define	DEVICE_ID_PROP		"devid"
597c478bd9Sstevel@tonic-gate #define	PROD_ID_PROP		"inquiry-product-id"
607c478bd9Sstevel@tonic-gate #define	PROD_ID_USB_PROP	"usb-product-name"
617c478bd9Sstevel@tonic-gate #define	REMOVABLE_PROP		"removable-media"
620167b58cScg #define	HOTPLUGGABLE_PROP	"hotpluggable"
637c478bd9Sstevel@tonic-gate #define	SCSI_OPTIONS_PROP	"scsi-options"
647c478bd9Sstevel@tonic-gate #define	VENDOR_ID_PROP		"inquiry-vendor-id"
657c478bd9Sstevel@tonic-gate #define	VENDOR_ID_USB_PROP	"usb-vendor-name"
667c478bd9Sstevel@tonic-gate #define	WWN_PROP		"node-wwn"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static char *ctrltypes[] = {
697c478bd9Sstevel@tonic-gate 	DDI_NT_FC_ATTACHMENT_POINT,
70c470f575SYuri Pankov 	DDI_NT_NVME_ATTACHMENT_POINT,
71c470f575SYuri Pankov 	DDI_NT_SATA_ATTACHMENT_POINT,
72c470f575SYuri Pankov 	DDI_NT_SATA_NEXUS,
73c470f575SYuri Pankov 	DDI_NT_SCSI_ATTACHMENT_POINT,
74c470f575SYuri Pankov 	DDI_NT_SCSI_NEXUS,
757c478bd9Sstevel@tonic-gate 	NULL
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static char *bustypes[] = {
797c478bd9Sstevel@tonic-gate 	"sbus",
807c478bd9Sstevel@tonic-gate 	"pci",
817c478bd9Sstevel@tonic-gate 	"usb",
827c478bd9Sstevel@tonic-gate 	NULL
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static bus_t		*add_bus(struct search_args *args, di_node_t node,
867c478bd9Sstevel@tonic-gate 			    di_minor_t minor, controller_t *cp);
877c478bd9Sstevel@tonic-gate static controller_t	*add_controller(struct search_args *args,
887c478bd9Sstevel@tonic-gate 			    di_node_t node, di_minor_t minor);
897c478bd9Sstevel@tonic-gate static int		add_devpath(di_devlink_t devlink, void *arg);
907c478bd9Sstevel@tonic-gate static int		add_devs(di_node_t node, di_minor_t minor, void *arg);
917c478bd9Sstevel@tonic-gate static int		add_disk2controller(disk_t *diskp,
927c478bd9Sstevel@tonic-gate 			    struct search_args *args);
937c478bd9Sstevel@tonic-gate static int		add_disk2path(disk_t *dp, path_t *pp,
947c478bd9Sstevel@tonic-gate 			    di_path_state_t st, char *wwn);
957c478bd9Sstevel@tonic-gate static int		add_int2array(int p, int **parray);
967c478bd9Sstevel@tonic-gate static int		add_ptr2array(void *p, void ***parray);
977c478bd9Sstevel@tonic-gate static char		*bus_type(di_node_t node, di_minor_t minor,
987c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
9942f64fdbSSarah Jelinek static void		remove_controller(controller_t *cp,
1007c478bd9Sstevel@tonic-gate 			    controller_t *currp);
1017c478bd9Sstevel@tonic-gate static void		clean_paths(struct search_args *args);
1027c478bd9Sstevel@tonic-gate static disk_t		*create_disk(char *deviceid, char *kernel_name,
1037c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1047c478bd9Sstevel@tonic-gate static char		*ctype(di_node_t node, di_minor_t minor);
10597ab9a43SJohn Levon static boolean_t	disk_is_cdrom(const char *type);
1067c478bd9Sstevel@tonic-gate static alias_t		*find_alias(disk_t *diskp, char *kernel_name);
1077c478bd9Sstevel@tonic-gate static bus_t		*find_bus(struct search_args *args, char *name);
1087c478bd9Sstevel@tonic-gate static controller_t	*find_controller(struct search_args *args, char *name);
1097c478bd9Sstevel@tonic-gate static disk_t		*get_disk_by_deviceid(disk_t *listp, char *devid);
1107c478bd9Sstevel@tonic-gate static void		get_disk_name_from_path(char *path, char *name,
1117c478bd9Sstevel@tonic-gate 			    int size);
1127c478bd9Sstevel@tonic-gate static char		*get_byte_prop(char *prop_name, di_node_t node);
1137c478bd9Sstevel@tonic-gate static di_node_t	get_parent_bus(di_node_t node,
1147c478bd9Sstevel@tonic-gate 			    struct search_args *args);
1157c478bd9Sstevel@tonic-gate static int		get_prom_int(char *prop_name, di_node_t node,
1167c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1177c478bd9Sstevel@tonic-gate static char		*get_prom_str(char *prop_name, di_node_t node,
1187c478bd9Sstevel@tonic-gate 			    di_prom_handle_t ph);
1197c478bd9Sstevel@tonic-gate static int		get_prop(char *prop_name, di_node_t node);
1207c478bd9Sstevel@tonic-gate static char		*get_str_prop(char *prop_name, di_node_t node);
1217c478bd9Sstevel@tonic-gate static int		have_disk(struct search_args *args, char *devid,
1227c478bd9Sstevel@tonic-gate 			    char *kernel_name, disk_t **diskp);
1237c478bd9Sstevel@tonic-gate static int		is_ctds(char *name);
1247c478bd9Sstevel@tonic-gate static int		is_drive(di_minor_t minor);
125e7cbe64fSgw static int		is_zvol(di_node_t node, di_minor_t minor);
126c470f575SYuri Pankov static int		is_ctrl(di_node_t node, di_minor_t minor);
1277c478bd9Sstevel@tonic-gate static int		new_alias(disk_t *diskp, char *kernel_path,
1287c478bd9Sstevel@tonic-gate 			    char *devlink_path, struct search_args *args);
1297c478bd9Sstevel@tonic-gate static int		new_devpath(alias_t *ap, char *devpath);
1307c478bd9Sstevel@tonic-gate static path_t		*new_path(controller_t *cp, disk_t *diskp,
1317c478bd9Sstevel@tonic-gate 			    di_node_t node, di_path_state_t st, char *wwn);
1327c478bd9Sstevel@tonic-gate static void		remove_invalid_controller(char *name,
1337c478bd9Sstevel@tonic-gate 			    controller_t *currp, struct search_args *args);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * The functions in this file do a dev tree walk to build up a model of the
1377c478bd9Sstevel@tonic-gate  * disks, controllers and paths on the system.  This model is returned in the
1387c478bd9Sstevel@tonic-gate  * args->disk_listp and args->controller_listp members of the args param.
1397c478bd9Sstevel@tonic-gate  * There is no global data for this file so it is thread safe.  It is up to
1407c478bd9Sstevel@tonic-gate  * the caller to merge the resulting model with any existing model that is
1417c478bd9Sstevel@tonic-gate  * cached.  The caller must also free the memory for this model when it is
1427c478bd9Sstevel@tonic-gate  * no longer needed.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate void
findevs(struct search_args * args)1457c478bd9Sstevel@tonic-gate findevs(struct search_args *args)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	di_node_t		di_root;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	args->bus_listp = NULL;
150c470f575SYuri Pankov 	args->controller_listp = NULL;
151c470f575SYuri Pankov 	args->disk_listp = NULL;
1527c478bd9Sstevel@tonic-gate 
153be235d17SJoshua M. Clulow 	args->ph = DI_PROM_HANDLE_NIL;
154be235d17SJoshua M. Clulow 	args->handle = DI_LINK_NIL;
155c470f575SYuri Pankov 	args->dev_walk_status = 0;
156be235d17SJoshua M. Clulow 
157be235d17SJoshua M. Clulow 	/*
158be235d17SJoshua M. Clulow 	 * Create device information library handles, which must be destroyed
159be235d17SJoshua M. Clulow 	 * before we return.
160be235d17SJoshua M. Clulow 	 */
161be235d17SJoshua M. Clulow 	if ((args->ph = di_prom_init()) == DI_PROM_HANDLE_NIL ||
162be235d17SJoshua M. Clulow 	    (args->handle = di_devlink_init(NULL, 0)) == DI_LINK_NIL) {
163be235d17SJoshua M. Clulow 		/*
164be235d17SJoshua M. Clulow 		 * We could not open all of the handles we need, so clean up
165be235d17SJoshua M. Clulow 		 * and report failure to the caller.
166be235d17SJoshua M. Clulow 		 */
167be235d17SJoshua M. Clulow 		args->dev_walk_status = errno;
168be235d17SJoshua M. Clulow 		goto cleanup;
169be235d17SJoshua M. Clulow 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Have to make several passes at this with the new devfs caching.
1737c478bd9Sstevel@tonic-gate 	 * First, we find non-mpxio devices. Then we find mpxio/multipath
174c470f575SYuri Pankov 	 * devices.
1757c478bd9Sstevel@tonic-gate 	 */
176c470f575SYuri Pankov 	di_root = di_init("/", DINFOCACHE);
1777c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1787c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1797c478bd9Sstevel@tonic-gate 
180c470f575SYuri Pankov 	di_root = di_init("/", DINFOCPYALL|DINFOPATH);
1817c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(di_root, NULL, 0, args, add_devs);
1827c478bd9Sstevel@tonic-gate 	di_fini(di_root);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	clean_paths(args);
185be235d17SJoshua M. Clulow 
186be235d17SJoshua M. Clulow cleanup:
187be235d17SJoshua M. Clulow 	if (args->ph != DI_PROM_HANDLE_NIL) {
188be235d17SJoshua M. Clulow 		di_prom_fini(args->ph);
189be235d17SJoshua M. Clulow 		args->ph = DI_PROM_HANDLE_NIL;
190be235d17SJoshua M. Clulow 	}
191be235d17SJoshua M. Clulow 	if (args->handle != DI_LINK_NIL) {
192be235d17SJoshua M. Clulow 		(void) di_devlink_fini(&(args->handle));
193be235d17SJoshua M. Clulow 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Definitions of private functions
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static bus_t *
add_bus(struct search_args * args,di_node_t node,di_minor_t minor,controller_t * cp)2017c478bd9Sstevel@tonic-gate add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
202c470f575SYuri Pankov     controller_t *cp)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	char		*btype;
2057c478bd9Sstevel@tonic-gate 	char		*devpath;
2067c478bd9Sstevel@tonic-gate 	bus_t		*bp;
2077c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
2087c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
21142f64fdbSSarah Jelinek 		return (NULL);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if ((btype = bus_type(node, minor, args->ph)) == NULL) {
21542f64fdbSSarah Jelinek 		return (add_bus(args, di_parent_node(node),
21642f64fdbSSarah Jelinek 		    di_minor_next(di_parent_node(node), NULL), cp));
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if ((bp = find_bus(args, devpath)) != NULL) {
22242f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
2237c478bd9Sstevel@tonic-gate 
22442f64fdbSSarah Jelinek 		if (cp != NULL) {
22542f64fdbSSarah Jelinek 			if (add_ptr2array(cp,
22642f64fdbSSarah Jelinek 			    (void ***)&bp->controllers) != 0) {
22742f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
22842f64fdbSSarah Jelinek 				return (NULL);
22942f64fdbSSarah Jelinek 			}
2307c478bd9Sstevel@tonic-gate 		}
23142f64fdbSSarah Jelinek 		return (bp);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* Special handling for root node. */
2357c478bd9Sstevel@tonic-gate 	if (strcmp(devpath, "/") == 0) {
23642f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
23742f64fdbSSarah Jelinek 		return (NULL);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (dm_debug) {
24142f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	bp = (bus_t *)calloc(1, sizeof (bus_t));
2457c478bd9Sstevel@tonic-gate 	if (bp == NULL) {
24642f64fdbSSarah Jelinek 		return (NULL);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	bp->name = strdup(devpath);
2507c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
2517c478bd9Sstevel@tonic-gate 	if (bp->name == NULL) {
25242f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
25342f64fdbSSarah Jelinek 		cache_free_bus(bp);
25442f64fdbSSarah Jelinek 		return (NULL);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	bp->btype = strdup(btype);
2587c478bd9Sstevel@tonic-gate 	if (bp->btype == NULL) {
25942f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
26042f64fdbSSarah Jelinek 		cache_free_bus(bp);
26142f64fdbSSarah Jelinek 		return (NULL);
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
2657c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
26842f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
26942f64fdbSSarah Jelinek 		cache_free_bus(bp);
27042f64fdbSSarah Jelinek 		return (NULL);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/* if parent node is a bus, get its name */
2747c478bd9Sstevel@tonic-gate 	if ((pnode = get_parent_bus(node, args)) != NULL) {
27542f64fdbSSarah Jelinek 		devpath = di_devfs_path(pnode);
27642f64fdbSSarah Jelinek 		bp->pname = strdup(devpath);
27742f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
27842f64fdbSSarah Jelinek 		if (bp->pname == NULL) {
27942f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
28042f64fdbSSarah Jelinek 			cache_free_bus(bp);
28142f64fdbSSarah Jelinek 			return (NULL);
28242f64fdbSSarah Jelinek 		}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	} else {
28542f64fdbSSarah Jelinek 		bp->pname = NULL;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	bp->freq = get_prom_int("clock-frequency", node, args->ph);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
2917c478bd9Sstevel@tonic-gate 	if (bp->controllers == NULL) {
29242f64fdbSSarah Jelinek 		args->dev_walk_status = ENOMEM;
29342f64fdbSSarah Jelinek 		cache_free_bus(bp);
29442f64fdbSSarah Jelinek 		return (NULL);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 	bp->controllers[0] = NULL;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
29942f64fdbSSarah Jelinek 		if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
30042f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
30142f64fdbSSarah Jelinek 			return (NULL);
30242f64fdbSSarah Jelinek 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	bp->next = args->bus_listp;
3067c478bd9Sstevel@tonic-gate 	args->bus_listp = bp;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (bp);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate static controller_t *
add_controller(struct search_args * args,di_node_t node,di_minor_t minor)3127c478bd9Sstevel@tonic-gate add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	char		*devpath;
3157c478bd9Sstevel@tonic-gate 	controller_t	*cp;
3167c478bd9Sstevel@tonic-gate 	char		kstat_name[MAXPATHLEN];
3177c478bd9Sstevel@tonic-gate 	char		*c_type = DM_CTYPE_UNKNOWN;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if ((cp = find_controller(args, devpath)) != NULL) {
32242f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
32342f64fdbSSarah Jelinek 		return (cp);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
3277c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
32842f64fdbSSarah Jelinek 		di_node_t pnode;
3297c478bd9Sstevel@tonic-gate 
33042f64fdbSSarah Jelinek 		pnode = di_parent_node(node);
33142f64fdbSSarah Jelinek 		if (pnode != DI_NODE_NIL) {
33242f64fdbSSarah Jelinek 			di_devfs_path_free((void *) devpath);
33342f64fdbSSarah Jelinek 			devpath = di_devfs_path(pnode);
3347c478bd9Sstevel@tonic-gate 
33542f64fdbSSarah Jelinek 			if ((cp = find_controller(args, devpath)) != NULL) {
33642f64fdbSSarah Jelinek 				di_devfs_path_free((void *) devpath);
33742f64fdbSSarah Jelinek 				return (cp);
33842f64fdbSSarah Jelinek 			}
3397c478bd9Sstevel@tonic-gate 
34042f64fdbSSarah Jelinek 			/* not in the list, create it */
34142f64fdbSSarah Jelinek 			node = pnode;
34242f64fdbSSarah Jelinek 			c_type = DM_CTYPE_FIBRE;
34342f64fdbSSarah Jelinek 		}
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (dm_debug) {
34742f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	cp = (controller_t *)calloc(1, sizeof (controller_t));
3517c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
35242f64fdbSSarah Jelinek 		return (NULL);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	cp->name = strdup(devpath);
3567c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
3577c478bd9Sstevel@tonic-gate 	if (cp->name == NULL) {
35842f64fdbSSarah Jelinek 		cache_free_controller(cp);
35942f64fdbSSarah Jelinek 		return (NULL);
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
36342f64fdbSSarah Jelinek 		c_type = ctype(node, minor);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	cp->ctype = c_type;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	(void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
3687c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_instance(node));
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
37142f64fdbSSarah Jelinek 		cache_free_controller(cp);
37242f64fdbSSarah Jelinek 		return (NULL);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
37642f64fdbSSarah Jelinek 		cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
38042f64fdbSSarah Jelinek 		cp->multiplex = 1;
3817c478bd9Sstevel@tonic-gate 	} else {
38242f64fdbSSarah Jelinek 		cp->multiplex = 0;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	cp->freq = get_prom_int("clock-frequency", node, args->ph);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
3887c478bd9Sstevel@tonic-gate 	if (cp->disks == NULL) {
38942f64fdbSSarah Jelinek 		cache_free_controller(cp);
39042f64fdbSSarah Jelinek 		return (NULL);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	cp->disks[0] = NULL;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	cp->next = args->controller_listp;
3957c478bd9Sstevel@tonic-gate 	args->controller_listp = cp;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	cp->bus = add_bus(args, di_parent_node(node),
3987c478bd9Sstevel@tonic-gate 	    di_minor_next(di_parent_node(node), NULL), cp);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	return (cp);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static int
add_devpath(di_devlink_t devlink,void * arg)4047c478bd9Sstevel@tonic-gate add_devpath(di_devlink_t devlink, void *arg)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	struct search_args *args;
4077c478bd9Sstevel@tonic-gate 	char		*devidstr;
4087c478bd9Sstevel@tonic-gate 	disk_t		*diskp;
4097c478bd9Sstevel@tonic-gate 	char		kernel_name[MAXPATHLEN];
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	args =	(struct search_args *)arg;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * Get the diskp value from calling have_disk. Can either be found
4157c478bd9Sstevel@tonic-gate 	 * by kernel name or devid.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	diskp = NULL;
4197c478bd9Sstevel@tonic-gate 	devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
4207c478bd9Sstevel@tonic-gate 	(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
4217c478bd9Sstevel@tonic-gate 	    di_node_name(args->node), di_instance(args->node));
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	(void) have_disk(args, devidstr, kernel_name, &diskp);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
4277c478bd9Sstevel@tonic-gate 	 * For diskettes it is /dev/rdiskette*.
4287c478bd9Sstevel@tonic-gate 	 * On Intel we would also get each fdisk partition as well
4297c478bd9Sstevel@tonic-gate 	 * (e.g. /dev/rdsk/c0t0d0p0).
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	if (diskp != NULL) {
43242f64fdbSSarah Jelinek 		alias_t	*ap;
43342f64fdbSSarah Jelinek 		char	*devlink_path;
4347c478bd9Sstevel@tonic-gate 
43542f64fdbSSarah Jelinek 		if (diskp->drv_type != DM_DT_FLOPPY) {
43642f64fdbSSarah Jelinek 			/*
43742f64fdbSSarah Jelinek 			 * Add other controllers for multipath disks.
43842f64fdbSSarah Jelinek 			 * This will have no effect if the controller
43942f64fdbSSarah Jelinek 			 * relationship is already set up.
44042f64fdbSSarah Jelinek 			 */
44142f64fdbSSarah Jelinek 			if (add_disk2controller(diskp, args) != 0) {
44242f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
44342f64fdbSSarah Jelinek 			}
4447c478bd9Sstevel@tonic-gate 		}
4457c478bd9Sstevel@tonic-gate 
44642f64fdbSSarah Jelinek 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
44742f64fdbSSarah Jelinek 		    di_node_name(args->node), di_instance(args->node));
44842f64fdbSSarah Jelinek 		devlink_path = (char *)di_devlink_path(devlink);
4497c478bd9Sstevel@tonic-gate 
45042f64fdbSSarah Jelinek 		if (dm_debug > 1) {
45142f64fdbSSarah Jelinek 			(void) fprintf(stderr,
45242f64fdbSSarah Jelinek 			    "INFO:     devpath %s\n", devlink_path);
4537c478bd9Sstevel@tonic-gate 		}
45442f64fdbSSarah Jelinek 
45542f64fdbSSarah Jelinek 		if ((ap = find_alias(diskp, kernel_name)) == NULL) {
45642f64fdbSSarah Jelinek 			if (new_alias(diskp, kernel_name, devlink_path,
45742f64fdbSSarah Jelinek 			    args) != 0) {
45842f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
45942f64fdbSSarah Jelinek 			}
46042f64fdbSSarah Jelinek 		} else {
46142f64fdbSSarah Jelinek 			/*
46242f64fdbSSarah Jelinek 			 * It is possible that we have already added this
46342f64fdbSSarah Jelinek 			 * devpath.  Do not add it again. new_devpath will
46442f64fdbSSarah Jelinek 			 * return a 0 if found, and not add the path.
46542f64fdbSSarah Jelinek 			 */
46642f64fdbSSarah Jelinek 			if (new_devpath(ap, devlink_path) != 0) {
46742f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
46842f64fdbSSarah Jelinek 			}
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate static int
add_devs(di_node_t node,di_minor_t minor,void * arg)4767c478bd9Sstevel@tonic-gate add_devs(di_node_t node, di_minor_t minor, void *arg)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	struct search_args	*args;
4797c478bd9Sstevel@tonic-gate 	int result = DI_WALK_CONTINUE;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	args = (struct search_args *)arg;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (dm_debug > 1) {
48442f64fdbSSarah Jelinek 		/* This is all just debugging code */
48542f64fdbSSarah Jelinek 		char	*devpath;
48642f64fdbSSarah Jelinek 		char	dev_name[MAXPATHLEN];
48742f64fdbSSarah Jelinek 
48842f64fdbSSarah Jelinek 		devpath = di_devfs_path(node);
48942f64fdbSSarah Jelinek 		(void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
49042f64fdbSSarah Jelinek 		    di_minor_name(minor));
49142f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
49242f64fdbSSarah Jelinek 
49342f64fdbSSarah Jelinek 		(void) fprintf(stderr,
49442f64fdbSSarah Jelinek 		    "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
49542f64fdbSSarah Jelinek 		    dev_name, di_node_name(node), di_instance(node),
49642f64fdbSSarah Jelinek 		    di_minor_spectype(minor),
49742f64fdbSSarah Jelinek 		    (di_minor_nodetype(minor) != NULL ?
4987c478bd9Sstevel@tonic-gate 		    di_minor_nodetype(minor) : "NULL"));
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (bus_type(node, minor, args->ph) != NULL) {
50242f64fdbSSarah Jelinek 		if (add_bus(args, node, minor, NULL) == NULL) {
50342f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
50442f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
50542f64fdbSSarah Jelinek 		}
5067c478bd9Sstevel@tonic-gate 
507c470f575SYuri Pankov 	} else if (is_ctrl(node, minor)) {
50842f64fdbSSarah Jelinek 		if (add_controller(args, node, minor) == NULL) {
50942f64fdbSSarah Jelinek 			args->dev_walk_status = ENOMEM;
51042f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
51142f64fdbSSarah Jelinek 		}
5127c478bd9Sstevel@tonic-gate 
513e7cbe64fSgw 	} else if (di_minor_spectype(minor) == S_IFCHR &&
51442f64fdbSSarah Jelinek 	    (is_drive(minor) || is_zvol(node, minor))) {
51542f64fdbSSarah Jelinek 		char	*devidstr;
51642f64fdbSSarah Jelinek 		char	kernel_name[MAXPATHLEN];
51742f64fdbSSarah Jelinek 		disk_t	*diskp;
5187c478bd9Sstevel@tonic-gate 
51942f64fdbSSarah Jelinek 		(void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
52042f64fdbSSarah Jelinek 		    di_node_name(node), di_instance(node));
52142f64fdbSSarah Jelinek 		devidstr = get_str_prop(DEVICE_ID_PROP, node);
52242f64fdbSSarah Jelinek 
52342f64fdbSSarah Jelinek 		args->node = node;
52442f64fdbSSarah Jelinek 		args->minor = minor;
52542f64fdbSSarah Jelinek 		/*
52642f64fdbSSarah Jelinek 		 * Check if we already got this disk and
52742f64fdbSSarah Jelinek 		 * this is another slice.
52842f64fdbSSarah Jelinek 		 */
52942f64fdbSSarah Jelinek 		if (!have_disk(args, devidstr, kernel_name, &diskp)) {
53042f64fdbSSarah Jelinek 			args->dev_walk_status = 0;
53142f64fdbSSarah Jelinek 			/*
53242f64fdbSSarah Jelinek 			 * This is a newly found disk, create the
53342f64fdbSSarah Jelinek 			 * disk structure.
53442f64fdbSSarah Jelinek 			 */
53542f64fdbSSarah Jelinek 			diskp = create_disk(devidstr, kernel_name, args);
53642f64fdbSSarah Jelinek 			if (diskp == NULL) {
53742f64fdbSSarah Jelinek 				args->dev_walk_status = ENOMEM;
53842f64fdbSSarah Jelinek 			}
53942f64fdbSSarah Jelinek 
54042f64fdbSSarah Jelinek 			if (diskp->drv_type != DM_DT_FLOPPY) {
54142f64fdbSSarah Jelinek 				/* add the controller relationship */
54242f64fdbSSarah Jelinek 				if (args->dev_walk_status == 0) {
54342f64fdbSSarah Jelinek 					if (add_disk2controller(diskp,
54442f64fdbSSarah Jelinek 					    args) != 0) {
54542f64fdbSSarah Jelinek 						args->dev_walk_status = ENOMEM;
54642f64fdbSSarah Jelinek 					}
54742f64fdbSSarah Jelinek 				}
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 		}
55042f64fdbSSarah Jelinek 		if (is_zvol(node, minor)) {
55142f64fdbSSarah Jelinek 			char zvdsk[MAXNAMELEN];
55242f64fdbSSarah Jelinek 			char *str;
55342f64fdbSSarah Jelinek 			alias_t *ap;
55442f64fdbSSarah Jelinek 
55542f64fdbSSarah Jelinek 			if (di_prop_lookup_strings(di_minor_devt(minor),
55642f64fdbSSarah Jelinek 			    node, "name", &str) == -1)
55742f64fdbSSarah Jelinek 				return (DI_WALK_CONTINUE);
55842f64fdbSSarah Jelinek 			(void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s",
55942f64fdbSSarah Jelinek 			    str);
56042f64fdbSSarah Jelinek 			if ((ap = find_alias(diskp, kernel_name)) == NULL) {
56142f64fdbSSarah Jelinek 				if (new_alias(diskp, kernel_name,
56242f64fdbSSarah Jelinek 				    zvdsk, args) != 0) {
56342f64fdbSSarah Jelinek 					args->dev_walk_status = ENOMEM;
56442f64fdbSSarah Jelinek 				}
56542f64fdbSSarah Jelinek 			} else {
56642f64fdbSSarah Jelinek 				/*
56742f64fdbSSarah Jelinek 				 * It is possible that we have already added
56842f64fdbSSarah Jelinek 				 * this devpath.
56942f64fdbSSarah Jelinek 				 * Do not add it again. new_devpath will
57042f64fdbSSarah Jelinek 				 * return a 0 if found, and not add the path.
57142f64fdbSSarah Jelinek 				 */
57242f64fdbSSarah Jelinek 				if (new_devpath(ap, zvdsk) != 0) {
57342f64fdbSSarah Jelinek 					args->dev_walk_status = ENOMEM;
57442f64fdbSSarah Jelinek 				}
57542f64fdbSSarah Jelinek 			}
576681d9761SEric Taylor 		}
5777c478bd9Sstevel@tonic-gate 
57842f64fdbSSarah Jelinek 		/* Add the devpaths for the drive. */
57942f64fdbSSarah Jelinek 		if (args->dev_walk_status == 0) {
58042f64fdbSSarah Jelinek 			char	*devpath;
58142f64fdbSSarah Jelinek 			char	slice_path[MAXPATHLEN];
58242f64fdbSSarah Jelinek 			char	*pattern;
5837c478bd9Sstevel@tonic-gate 
58442f64fdbSSarah Jelinek 			/*
58542f64fdbSSarah Jelinek 			 * We will come through here once for each of
58642f64fdbSSarah Jelinek 			 * the raw slice device names.
58742f64fdbSSarah Jelinek 			 */
58842f64fdbSSarah Jelinek 			devpath = di_devfs_path(node);
58942f64fdbSSarah Jelinek 			(void) snprintf(slice_path,
59042f64fdbSSarah Jelinek 			    sizeof (slice_path), "%s:%s",
59142f64fdbSSarah Jelinek 			    devpath, di_minor_name(minor));
59242f64fdbSSarah Jelinek 			di_devfs_path_free((void *) devpath);
59342f64fdbSSarah Jelinek 
59442f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(di_minor_nodetype(minor),
59542f64fdbSSarah Jelinek 			    DDI_NT_FD)) {
59642f64fdbSSarah Jelinek 				pattern = DEVLINK_FLOPPY_REGEX;
59742f64fdbSSarah Jelinek 			} else {
59842f64fdbSSarah Jelinek 				pattern = DEVLINK_REGEX;
59942f64fdbSSarah Jelinek 			}
6007c478bd9Sstevel@tonic-gate 
60142f64fdbSSarah Jelinek 			/* Walk the /dev tree to get the devlinks. */
60242f64fdbSSarah Jelinek 			(void) di_devlink_walk(args->handle, pattern,
60342f64fdbSSarah Jelinek 			    slice_path, DI_PRIMARY_LINK, arg, add_devpath);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 
60642f64fdbSSarah Jelinek 		if (args->dev_walk_status != 0) {
60742f64fdbSSarah Jelinek 			result = DI_WALK_TERMINATE;
60842f64fdbSSarah Jelinek 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	return (result);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate static int
add_disk2controller(disk_t * diskp,struct search_args * args)6157c478bd9Sstevel@tonic-gate add_disk2controller(disk_t *diskp, struct search_args *args)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
6187c478bd9Sstevel@tonic-gate 	controller_t	*cp;
6197c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
6207c478bd9Sstevel@tonic-gate 	di_node_t	node;
6217c478bd9Sstevel@tonic-gate 	int		i;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	node = args->node;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
6267c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
62742f64fdbSSarah Jelinek 		return (0);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
630*f85f43edSRobert Mustacchi 	/*
631*f85f43edSRobert Mustacchi 	 * Certain pseudo-device nodes do not all immediately have a valid
632*f85f43edSRobert Mustacchi 	 * parent-node. In particular, lofi (and zfs) would point to the generic
633*f85f43edSRobert Mustacchi 	 * /pseudo node. As a result, if we find a lofi disk, redirect it to the
634*f85f43edSRobert Mustacchi 	 * actual path. If we don't find it in this, then just fall back to the
635*f85f43edSRobert Mustacchi 	 * traditional path.
636*f85f43edSRobert Mustacchi 	 */
637*f85f43edSRobert Mustacchi 	if (libdiskmgt_str_eq(di_node_name(pnode), "pseudo") &&
638*f85f43edSRobert Mustacchi 	    libdiskmgt_str_eq(di_node_name(node), "lofi")) {
639*f85f43edSRobert Mustacchi 		di_node_t n;
640*f85f43edSRobert Mustacchi 
641*f85f43edSRobert Mustacchi 		n = di_drv_first_node("lofi", pnode);
642*f85f43edSRobert Mustacchi 		while (n != DI_NODE_NIL) {
643*f85f43edSRobert Mustacchi 			if (di_instance(n) == 0) {
644*f85f43edSRobert Mustacchi 				pnode = n;
645*f85f43edSRobert Mustacchi 				break;
646*f85f43edSRobert Mustacchi 			}
647*f85f43edSRobert Mustacchi 
648*f85f43edSRobert Mustacchi 			n = di_drv_next_node(n);
649*f85f43edSRobert Mustacchi 		}
650*f85f43edSRobert Mustacchi 	}
651*f85f43edSRobert Mustacchi 
6527c478bd9Sstevel@tonic-gate 	minor = di_minor_next(pnode, NULL);
6537c478bd9Sstevel@tonic-gate 	if (minor == NULL) {
65442f64fdbSSarah Jelinek 		return (0);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if ((cp = add_controller(args, pnode, minor)) == NULL) {
65842f64fdbSSarah Jelinek 		return (ENOMEM);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* check if the disk <-> ctrl assoc is already there */
6627c478bd9Sstevel@tonic-gate 	for (i = 0; diskp->controllers[i]; i++) {
66342f64fdbSSarah Jelinek 		if (cp == diskp->controllers[i]) {
66442f64fdbSSarah Jelinek 			return (0);
66542f64fdbSSarah Jelinek 		}
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/* this is a new controller for this disk */
6697c478bd9Sstevel@tonic-gate 
670c470f575SYuri Pankov 	/* add the disk to the controller */
6717c478bd9Sstevel@tonic-gate 	if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
67242f64fdbSSarah Jelinek 		return (ENOMEM);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
675c470f575SYuri Pankov 	/* add the controller to the disk */
6767c478bd9Sstevel@tonic-gate 	if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
67742f64fdbSSarah Jelinek 		return (ENOMEM);
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * Set up paths for mpxio controlled drives.
6827c478bd9Sstevel@tonic-gate 	 */
6837c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
68442f64fdbSSarah Jelinek 		/* note: mpxio di_path stuff is all consolidation private */
68542f64fdbSSarah Jelinek 		di_path_t   pi = DI_PATH_NIL;
68642f64fdbSSarah Jelinek 
68742f64fdbSSarah Jelinek 		while (
68842f64fdbSSarah Jelinek 		    (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
68942f64fdbSSarah Jelinek 			int	cnt;
69042f64fdbSSarah Jelinek 			uchar_t	*bytes;
69142f64fdbSSarah Jelinek 			char	str[MAXPATHLEN];
69242f64fdbSSarah Jelinek 			char	*wwn;
69342f64fdbSSarah Jelinek 
69442f64fdbSSarah Jelinek 			di_node_t phci_node = di_path_phci_node(pi);
69542f64fdbSSarah Jelinek 
69642f64fdbSSarah Jelinek 			/* get the node wwn */
69742f64fdbSSarah Jelinek 			cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
69842f64fdbSSarah Jelinek 			wwn = NULL;
69942f64fdbSSarah Jelinek 			if (cnt > 0) {
70042f64fdbSSarah Jelinek 				int	i;
70142f64fdbSSarah Jelinek 				str[0] = 0;
70242f64fdbSSarah Jelinek 
70342f64fdbSSarah Jelinek 				for (i = 0; i < cnt; i++) {
70442f64fdbSSarah Jelinek 					/*
70542f64fdbSSarah Jelinek 					 * A byte is only 2 hex chars + null.
70642f64fdbSSarah Jelinek 					 */
70742f64fdbSSarah Jelinek 					char bstr[8];
70842f64fdbSSarah Jelinek 
70942f64fdbSSarah Jelinek 					(void) snprintf(bstr,
71042f64fdbSSarah Jelinek 					    sizeof (bstr), "%.2x", bytes[i]);
71142f64fdbSSarah Jelinek 					(void) strlcat(str, bstr, sizeof (str));
71242f64fdbSSarah Jelinek 				}
71342f64fdbSSarah Jelinek 				wwn = str;
71442f64fdbSSarah Jelinek 			}
7157c478bd9Sstevel@tonic-gate 
71642f64fdbSSarah Jelinek 			if (new_path(cp, diskp, phci_node,
71742f64fdbSSarah Jelinek 			    di_path_state(pi), wwn) == NULL) {
71842f64fdbSSarah Jelinek 				return (ENOMEM);
71942f64fdbSSarah Jelinek 			}
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	return (0);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate static int
add_disk2path(disk_t * dp,path_t * pp,di_path_state_t st,char * wwn)7277c478bd9Sstevel@tonic-gate add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
7307c478bd9Sstevel@tonic-gate 	if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
73142f64fdbSSarah Jelinek 		cache_free_path(pp);
73242f64fdbSSarah Jelinek 		return (0);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/* add the path to the disk */
7367c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
73742f64fdbSSarah Jelinek 		cache_free_path(pp);
73842f64fdbSSarah Jelinek 		return (0);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7427c478bd9Sstevel@tonic-gate 	if (add_int2array(st, &pp->states) != 0) {
74342f64fdbSSarah Jelinek 		cache_free_path(pp);
74442f64fdbSSarah Jelinek 		return (0);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/* add the path state for this disk */
7487c478bd9Sstevel@tonic-gate 	if (wwn != NULL) {
74942f64fdbSSarah Jelinek 		char	*wp;
7507c478bd9Sstevel@tonic-gate 
75142f64fdbSSarah Jelinek 		if ((wp = strdup(wwn)) != NULL) {
75242f64fdbSSarah Jelinek 			if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
75342f64fdbSSarah Jelinek 				cache_free_path(pp);
75442f64fdbSSarah Jelinek 				return (0);
75542f64fdbSSarah Jelinek 			}
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	return (1);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate static int
add_int2array(int p,int ** parray)7637c478bd9Sstevel@tonic-gate add_int2array(int p, int **parray)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	int		i;
7667c478bd9Sstevel@tonic-gate 	int		cnt;
7677c478bd9Sstevel@tonic-gate 	int		*pa;
7687c478bd9Sstevel@tonic-gate 	int		*new_array;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	pa = *parray;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	cnt = 0;
7737c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
77442f64fdbSSarah Jelinek 		for (; pa[cnt] != -1; cnt++)
77542f64fdbSSarah Jelinek 			;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	new_array = (int *)calloc(cnt + 2, sizeof (int *));
7797c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
78042f64fdbSSarah Jelinek 		return (ENOMEM);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	/* copy the existing array */
7847c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
78542f64fdbSSarah Jelinek 		new_array[i] = pa[i];
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	new_array[i] = p;
7897c478bd9Sstevel@tonic-gate 	new_array[i + 1] = -1;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	free(pa);
7927c478bd9Sstevel@tonic-gate 	*parray = new_array;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	return (0);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static int
add_ptr2array(void * p,void *** parray)7987c478bd9Sstevel@tonic-gate add_ptr2array(void *p, void ***parray)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	int		i;
8017c478bd9Sstevel@tonic-gate 	int		cnt;
8027c478bd9Sstevel@tonic-gate 	void		**pa;
8037c478bd9Sstevel@tonic-gate 	void		**new_array;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	pa = *parray;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	cnt = 0;
8087c478bd9Sstevel@tonic-gate 	if (pa != NULL) {
80942f64fdbSSarah Jelinek 		for (; pa[cnt]; cnt++)
81042f64fdbSSarah Jelinek 			;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	new_array = (void **)calloc(cnt + 2, sizeof (void *));
8147c478bd9Sstevel@tonic-gate 	if (new_array == NULL) {
81542f64fdbSSarah Jelinek 		return (ENOMEM);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	/* copy the existing array */
8197c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
82042f64fdbSSarah Jelinek 		new_array[i] = pa[i];
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	new_array[i] = p;
8247c478bd9Sstevel@tonic-gate 	new_array[i + 1] = NULL;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	free(pa);
8277c478bd9Sstevel@tonic-gate 	*parray = new_array;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	return (0);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate /*
83342f64fdbSSarah Jelinek  * This function checks to see if a controller has other associations
83442f64fdbSSarah Jelinek  * that may be valid. If we are calling this function, we have found that
83542f64fdbSSarah Jelinek  * a controller for an mpxio device is showing up independently of the
83642f64fdbSSarah Jelinek  * mpxio controller, noted as /scsi_vhci. This can happen with some FC
83742f64fdbSSarah Jelinek  * cards that have inbound management devices that show up as well, with
83842f64fdbSSarah Jelinek  * the real controller data associated. We do not want to display these
83942f64fdbSSarah Jelinek  * 'devices' as real devices in libdiskmgt.
8407c478bd9Sstevel@tonic-gate  */
84142f64fdbSSarah Jelinek static void
remove_controller(controller_t * cp,controller_t * currp)84242f64fdbSSarah Jelinek remove_controller(controller_t *cp, controller_t *currp)
8437c478bd9Sstevel@tonic-gate {
84442f64fdbSSarah Jelinek 	int	i;
8457c478bd9Sstevel@tonic-gate 
84642f64fdbSSarah Jelinek 	if (cp == currp) {
84742f64fdbSSarah Jelinek 		if (dm_debug) {
84842f64fdbSSarah Jelinek 			(void) fprintf(stderr, "ERROR: removing current"
84942f64fdbSSarah Jelinek 			    " controller\n");
85042f64fdbSSarah Jelinek 		}
85142f64fdbSSarah Jelinek 		return;
85242f64fdbSSarah Jelinek 	}
8537c478bd9Sstevel@tonic-gate 
85442f64fdbSSarah Jelinek 	if (cp->disks != NULL && cp->disks[0] != NULL) {
85542f64fdbSSarah Jelinek 		if (dm_debug) {
85642f64fdbSSarah Jelinek 			(void) fprintf(stderr,
85742f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
85842f64fdbSSarah Jelinek 			    " with disk ptrs.\n");
85942f64fdbSSarah Jelinek 		}
86042f64fdbSSarah Jelinek 		/*
86142f64fdbSSarah Jelinek 		 * loop through the disks and remove the reference to the
86242f64fdbSSarah Jelinek 		 * controller for this disk structure. The disk itself
86342f64fdbSSarah Jelinek 		 * is still a valid device, the controller being removed
86442f64fdbSSarah Jelinek 		 * is a 'path' so any disk that has a reference to it
86542f64fdbSSarah Jelinek 		 * as a controller needs to have this reference removed.
86642f64fdbSSarah Jelinek 		 */
8679729663dSGeorge Wilson 		for (i = 0; cp->disks[i]; i++) {
8689729663dSGeorge Wilson 			disk_t *dp = cp->disks[i];
8699729663dSGeorge Wilson 			int j;
8709729663dSGeorge Wilson 
8719729663dSGeorge Wilson 			for (j = 0; dp->controllers[j]; j++) {
8729729663dSGeorge Wilson 				int k;
8739729663dSGeorge Wilson 
8749729663dSGeorge Wilson 				if (libdiskmgt_str_eq(dp->controllers[j]->name,
87542f64fdbSSarah Jelinek 				    cp->name)) {
87642f64fdbSSarah Jelinek 
8779729663dSGeorge Wilson 					if (dm_debug) {
8789729663dSGeorge Wilson 						(void) fprintf(stderr,
8799729663dSGeorge Wilson 						    "INFO: REMOVING disk %s on "
8809729663dSGeorge Wilson 						    "controller %s\n",
8819729663dSGeorge Wilson 						    dp->kernel_name, cp->name);
8829729663dSGeorge Wilson 					}
8839729663dSGeorge Wilson 					for (k = j; dp->controllers[k]; k++) {
8849729663dSGeorge Wilson 						dp->controllers[k] =
8859729663dSGeorge Wilson 						    dp->controllers[k + 1];
88642f64fdbSSarah Jelinek 					}
88742f64fdbSSarah Jelinek 				}
88842f64fdbSSarah Jelinek 			}
88942f64fdbSSarah Jelinek 		}
8907c478bd9Sstevel@tonic-gate 	}
89142f64fdbSSarah Jelinek 	/*
89242f64fdbSSarah Jelinek 	 * Paths are removed with the call to cache_free_controller()
89342f64fdbSSarah Jelinek 	 * below.
89442f64fdbSSarah Jelinek 	 */
8957c478bd9Sstevel@tonic-gate 
89642f64fdbSSarah Jelinek 	if (cp->paths != NULL && cp->paths[0] != NULL) {
89742f64fdbSSarah Jelinek 		if (dm_debug) {
89842f64fdbSSarah Jelinek 			(void) fprintf(stderr,
89942f64fdbSSarah Jelinek 			    "INFO: removing inbound management controller"
90042f64fdbSSarah Jelinek 			    " with path ptrs. \n");
90142f64fdbSSarah Jelinek 		}
90242f64fdbSSarah Jelinek 	}
90342f64fdbSSarah Jelinek 	cache_free_controller(cp);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * If we have a controller in the list that is really a path then we need to
9087c478bd9Sstevel@tonic-gate  * take that controller out of the list since nodes that are paths are not
9097c478bd9Sstevel@tonic-gate  * considered to be controllers.
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate static void
clean_paths(struct search_args * args)9127c478bd9Sstevel@tonic-gate clean_paths(struct search_args *args)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	controller_t	*cp;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
9177c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
91842f64fdbSSarah Jelinek 		path_t	**pp;
9197c478bd9Sstevel@tonic-gate 
92042f64fdbSSarah Jelinek 		pp = cp->paths;
92142f64fdbSSarah Jelinek 		if (pp != NULL) {
92242f64fdbSSarah Jelinek 			int i;
9237c478bd9Sstevel@tonic-gate 
92442f64fdbSSarah Jelinek 			for (i = 0; pp[i]; i++) {
92542f64fdbSSarah Jelinek 				remove_invalid_controller(pp[i]->name, cp,
92642f64fdbSSarah Jelinek 				    args);
92742f64fdbSSarah Jelinek 			}
9287c478bd9Sstevel@tonic-gate 		}
92942f64fdbSSarah Jelinek 		cp = cp->next;
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate static disk_t *
create_disk(char * deviceid,char * kernel_name,struct search_args * args)9347c478bd9Sstevel@tonic-gate create_disk(char *deviceid, char *kernel_name, struct search_args *args)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	disk_t	*diskp;
9377c478bd9Sstevel@tonic-gate 	char	*type;
9387c478bd9Sstevel@tonic-gate 	char	*prod_id;
9397c478bd9Sstevel@tonic-gate 	char	*vendor_id;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (dm_debug) {
94242f64fdbSSarah Jelinek 		(void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	diskp = calloc(1, sizeof (disk_t));
9467c478bd9Sstevel@tonic-gate 	if (diskp == NULL) {
94742f64fdbSSarah Jelinek 		return (NULL);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	diskp->controllers = (controller_t **)
9517c478bd9Sstevel@tonic-gate 	    calloc(1, sizeof (controller_t *));
9527c478bd9Sstevel@tonic-gate 	if (diskp->controllers == NULL) {
95342f64fdbSSarah Jelinek 		cache_free_disk(diskp);
95442f64fdbSSarah Jelinek 		return (NULL);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 	diskp->controllers[0] = NULL;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	diskp->devid = NULL;
9597c478bd9Sstevel@tonic-gate 	if (deviceid != NULL) {
96042f64fdbSSarah Jelinek 		if ((diskp->device_id = strdup(deviceid)) == NULL) {
96142f64fdbSSarah Jelinek 			cache_free_disk(diskp);
96242f64fdbSSarah Jelinek 			return (NULL);
96342f64fdbSSarah Jelinek 		}
96442f64fdbSSarah Jelinek 		(void) devid_str_decode(deviceid, &(diskp->devid), NULL);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
96842f64fdbSSarah Jelinek 		diskp->kernel_name = strdup(kernel_name);
96942f64fdbSSarah Jelinek 		if (diskp->kernel_name == NULL) {
97042f64fdbSSarah Jelinek 			cache_free_disk(diskp);
97142f64fdbSSarah Jelinek 			return (NULL);
97242f64fdbSSarah Jelinek 		}
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	diskp->paths = NULL;
9767c478bd9Sstevel@tonic-gate 	diskp->aliases = NULL;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	diskp->cd_rom = 0;
9797c478bd9Sstevel@tonic-gate 	diskp->rpm = 0;
98059d8f100SGarrett D'Amore 	diskp->solid_state = -1;
9817c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(args->minor);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	prod_id = get_str_prop(PROD_ID_PROP, args->node);
9847c478bd9Sstevel@tonic-gate 	if (prod_id != NULL) {
9857c478bd9Sstevel@tonic-gate 		if ((diskp->product_id = strdup(prod_id)) == NULL) {
98642f64fdbSSarah Jelinek 			cache_free_disk(diskp);
98742f64fdbSSarah Jelinek 			return (NULL);
98842f64fdbSSarah Jelinek 		}
98942f64fdbSSarah Jelinek 	} else {
99042f64fdbSSarah Jelinek 		prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
99142f64fdbSSarah Jelinek 		if (prod_id != NULL) {
99242f64fdbSSarah Jelinek 			if ((diskp->product_id = strdup(prod_id)) == NULL) {
99342f64fdbSSarah Jelinek 				cache_free_disk(diskp);
99442f64fdbSSarah Jelinek 				return (NULL);
99542f64fdbSSarah Jelinek 			}
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
10007c478bd9Sstevel@tonic-gate 	if (vendor_id != NULL) {
10017c478bd9Sstevel@tonic-gate 		if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
100242f64fdbSSarah Jelinek 			cache_free_disk(diskp);
100342f64fdbSSarah Jelinek 			return (NULL);
100442f64fdbSSarah Jelinek 		}
100542f64fdbSSarah Jelinek 	} else {
1006e4adc82dSHans Rosenfeld 		vendor_id = get_str_prop(VENDOR_ID_USB_PROP, args->node);
100742f64fdbSSarah Jelinek 		if (vendor_id != NULL) {
100842f64fdbSSarah Jelinek 			if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
100942f64fdbSSarah Jelinek 				cache_free_disk(diskp);
101042f64fdbSSarah Jelinek 				return (NULL);
101142f64fdbSSarah Jelinek 			}
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
10177c478bd9Sstevel@tonic-gate 	 * We try to use uscsi later to determine the real type.
10187c478bd9Sstevel@tonic-gate 	 * The cd_rom flag tells us that the kernel categorized the drive
1019c470f575SYuri Pankov 	 * as a CD-ROM.  We leave the drv_type as UNKNOWN for now.
10207c478bd9Sstevel@tonic-gate 	 * The combination of the cd_rom flag being set with the drv_type of
10217c478bd9Sstevel@tonic-gate 	 * unknown is what triggers the uscsi probe in drive.c.
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	if (disk_is_cdrom(type)) {
102442f64fdbSSarah Jelinek 		diskp->drv_type = DM_DT_UNKNOWN;
102542f64fdbSSarah Jelinek 		diskp->cd_rom = 1;
102642f64fdbSSarah Jelinek 		diskp->removable = 1;
10277c478bd9Sstevel@tonic-gate 	} else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
102842f64fdbSSarah Jelinek 		diskp->drv_type = DM_DT_FLOPPY;
102942f64fdbSSarah Jelinek 		diskp->removable = 1;
10307c478bd9Sstevel@tonic-gate 	} else {
1031c470f575SYuri Pankov 		/* not a CD-ROM or Floppy */
103242f64fdbSSarah Jelinek 		diskp->removable = get_prop(REMOVABLE_PROP, args->node);
10337c478bd9Sstevel@tonic-gate 
103442f64fdbSSarah Jelinek 		if (diskp->removable == -1) {
103542f64fdbSSarah Jelinek 			diskp->removable = 0;
103642f64fdbSSarah Jelinek 			diskp->drv_type = DM_DT_FIXED;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	diskp->next = args->disk_listp;
10417c478bd9Sstevel@tonic-gate 	args->disk_listp = diskp;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	return (diskp);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate static char *
ctype(di_node_t node,di_minor_t minor)10477c478bd9Sstevel@tonic-gate ctype(di_node_t node, di_minor_t minor)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 	char	*type;
10507c478bd9Sstevel@tonic-gate 	char	*name;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
10537c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/* IDE disks use SCSI nexus as the type, so handle this special case */
1056c470f575SYuri Pankov 	if ((libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1057c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_PSEUDO)) &&
1058c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "ide"))
105942f64fdbSSarah Jelinek 		return (DM_CTYPE_ATA);
10607c478bd9Sstevel@tonic-gate 
1061c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT) ||
1062c470f575SYuri Pankov 	    (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
1063c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "fp")))
1064c470f575SYuri Pankov 		return (DM_CTYPE_FIBRE);
10657c478bd9Sstevel@tonic-gate 
1066c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_NVME_ATTACHMENT_POINT))
1067c470f575SYuri Pankov 		return (DM_CTYPE_NVME);
10687c478bd9Sstevel@tonic-gate 
1069c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_SATA_NEXUS) ||
1070c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_NT_SATA_ATTACHMENT_POINT))
1071c470f575SYuri Pankov 		return (DM_CTYPE_SATA);
10727c478bd9Sstevel@tonic-gate 
1073c470f575SYuri Pankov 	if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1074c470f575SYuri Pankov 	    libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT))
1075c470f575SYuri Pankov 		return (DM_CTYPE_SCSI);
1076c470f575SYuri Pankov 
1077c470f575SYuri Pankov 	if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb"))
1078c470f575SYuri Pankov 		return (DM_CTYPE_USB);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1081c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "xpvd"))
1082c470f575SYuri Pankov 		return (DM_CTYPE_XEN);
1083c470f575SYuri Pankov 
1084*f85f43edSRobert Mustacchi 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1085*f85f43edSRobert Mustacchi 	    libdiskmgt_str_eq(name, "lofi"))
1086*f85f43edSRobert Mustacchi 		return (DM_CTYPE_LOFI);
1087*f85f43edSRobert Mustacchi 
1088c470f575SYuri Pankov 	if (dm_debug) {
1089c470f575SYuri Pankov 		(void) fprintf(stderr,
1090c470f575SYuri Pankov 		    "INFO: unknown controller type=%s name=%s\n", type, name);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	return (DM_CTYPE_UNKNOWN);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate static boolean_t
disk_is_cdrom(const char * type)109797ab9a43SJohn Levon disk_is_cdrom(const char *type)
10987c478bd9Sstevel@tonic-gate {
109997ab9a43SJohn Levon 	return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate static alias_t *
find_alias(disk_t * diskp,char * kernel_name)11037c478bd9Sstevel@tonic-gate find_alias(disk_t *diskp, char *kernel_name)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	alias_t	*ap;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
11087c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
110942f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
111042f64fdbSSarah Jelinek 			return (ap);
111142f64fdbSSarah Jelinek 		}
111242f64fdbSSarah Jelinek 		ap = ap->next;
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	return (NULL);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate static bus_t *
find_bus(struct search_args * args,char * name)11197c478bd9Sstevel@tonic-gate find_bus(struct search_args *args, char *name)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	bus_t *listp;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	listp = args->bus_listp;
11247c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
112542f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(listp->name, name)) {
112642f64fdbSSarah Jelinek 			return (listp);
112742f64fdbSSarah Jelinek 		}
112842f64fdbSSarah Jelinek 		listp = listp->next;
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	return (NULL);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate static controller_t *
find_controller(struct search_args * args,char * name)11357c478bd9Sstevel@tonic-gate find_controller(struct search_args *args, char *name)
11367c478bd9Sstevel@tonic-gate {
11377c478bd9Sstevel@tonic-gate 	controller_t *listp;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	listp = args->controller_listp;
11407c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
114142f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(listp->name, name)) {
114242f64fdbSSarah Jelinek 			return (listp);
114342f64fdbSSarah Jelinek 		}
114442f64fdbSSarah Jelinek 		listp = listp->next;
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	return (NULL);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id.
11527c478bd9Sstevel@tonic-gate  * We got the device id from the dev tree walk.  This is encoded
11537c478bd9Sstevel@tonic-gate  * using devid_str_encode(3DEVID).   In order to check the device ids we need
11547c478bd9Sstevel@tonic-gate  * to use the devid_compare(3DEVID) function, so we need to decode the
11557c478bd9Sstevel@tonic-gate  * string representation of the device id.
11567c478bd9Sstevel@tonic-gate  */
11577c478bd9Sstevel@tonic-gate static disk_t *
get_disk_by_deviceid(disk_t * listp,char * devidstr)11587c478bd9Sstevel@tonic-gate get_disk_by_deviceid(disk_t *listp, char *devidstr)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
116342f64fdbSSarah Jelinek 		return (NULL);
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	while (listp != NULL) {
116742f64fdbSSarah Jelinek 		if (listp->devid != NULL &&
116842f64fdbSSarah Jelinek 		    devid_compare(listp->devid, devid) == 0) {
116942f64fdbSSarah Jelinek 			break;
117042f64fdbSSarah Jelinek 		}
117142f64fdbSSarah Jelinek 		listp = listp->next;
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	devid_free(devid);
11757c478bd9Sstevel@tonic-gate 	return (listp);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate /*
11797c478bd9Sstevel@tonic-gate  * Get the base disk name with no path prefix and no slice (if there is one).
11807c478bd9Sstevel@tonic-gate  * The name parameter should be big enough to hold the name.
11817c478bd9Sstevel@tonic-gate  * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
11827c478bd9Sstevel@tonic-gate  * and converts the raw diskette name.
11837c478bd9Sstevel@tonic-gate  * But, we don't know how to strip off the slice from third party drive
11847c478bd9Sstevel@tonic-gate  * names.  That just means that their drive name will include a slice on
11857c478bd9Sstevel@tonic-gate  * it.
11867c478bd9Sstevel@tonic-gate  */
11877c478bd9Sstevel@tonic-gate static void
get_disk_name_from_path(char * path,char * name,int size)11887c478bd9Sstevel@tonic-gate get_disk_name_from_path(char *path, char *name, int size)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	char		*basep;
11917c478bd9Sstevel@tonic-gate 	int		cnt = 0;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	basep = strrchr(path, '/');
11947c478bd9Sstevel@tonic-gate 	if (basep == NULL) {
119542f64fdbSSarah Jelinek 		basep = path;
11967c478bd9Sstevel@tonic-gate 	} else {
119742f64fdbSSarah Jelinek 		basep++;
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	size = size - 1;	/* leave room for terminating 0 */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (is_ctds(basep)) {
120342f64fdbSSarah Jelinek 		while (*basep != 0 && *basep != 's' && cnt < size) {
120442f64fdbSSarah Jelinek 			*name++ = *basep++;
120542f64fdbSSarah Jelinek 				cnt++;
120642f64fdbSSarah Jelinek 		}
120742f64fdbSSarah Jelinek 		*name = 0;
12087c478bd9Sstevel@tonic-gate 	} else {
120942f64fdbSSarah Jelinek 		if (strncmp(basep, FLOPPY_NAME,
121042f64fdbSSarah Jelinek 		    sizeof (FLOPPY_NAME) - 1) == 0) {
121142f64fdbSSarah Jelinek 			/*
121242f64fdbSSarah Jelinek 			 * a floppy, convert rdiskette name to diskette name,
121342f64fdbSSarah Jelinek 			 * by skipping over the 'r' for raw diskette
121442f64fdbSSarah Jelinek 			 */
121542f64fdbSSarah Jelinek 			basep++;
121642f64fdbSSarah Jelinek 		}
12177c478bd9Sstevel@tonic-gate 
121842f64fdbSSarah Jelinek 		/* not a ctds name, just copy it */
121942f64fdbSSarah Jelinek 		(void) strlcpy(name, basep, size);
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate static char *
get_byte_prop(char * prop_name,di_node_t node)12247c478bd9Sstevel@tonic-gate get_byte_prop(char *prop_name, di_node_t node)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	int	cnt;
12277c478bd9Sstevel@tonic-gate 	uchar_t	*bytes;
12287c478bd9Sstevel@tonic-gate 	int	i;
12297c478bd9Sstevel@tonic-gate 	char	str[MAXPATHLEN];
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
12327c478bd9Sstevel@tonic-gate 	if (cnt < 1) {
123342f64fdbSSarah Jelinek 		return (NULL);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	str[0] = 0;
12377c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
123842f64fdbSSarah Jelinek 		char bstr[8];	/* a byte is only 2 hex chars + null */
12397c478bd9Sstevel@tonic-gate 
124042f64fdbSSarah Jelinek 		(void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
124142f64fdbSSarah Jelinek 		(void) strlcat(str, bstr, sizeof (str));
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 	return (strdup(str));
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate static di_node_t
get_parent_bus(di_node_t node,struct search_args * args)12477c478bd9Sstevel@tonic-gate get_parent_bus(di_node_t node, struct search_args *args)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	di_node_t pnode;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(node);
12527c478bd9Sstevel@tonic-gate 	if (pnode == DI_NODE_NIL) {
125342f64fdbSSarah Jelinek 		return (NULL);
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
125742f64fdbSSarah Jelinek 		return (pnode);
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	return (get_parent_bus(pnode, args));
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate static int
get_prom_int(char * prop_name,di_node_t node,di_prom_handle_t ph)12647c478bd9Sstevel@tonic-gate get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
12657c478bd9Sstevel@tonic-gate {
12667c478bd9Sstevel@tonic-gate 	int *n;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
126942f64fdbSSarah Jelinek 		return (*n);
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	return (0);
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate static char *
get_prom_str(char * prop_name,di_node_t node,di_prom_handle_t ph)12767c478bd9Sstevel@tonic-gate get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	char *str;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
128142f64fdbSSarah Jelinek 		return (str);
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	return (NULL);
12857c478bd9Sstevel@tonic-gate }
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate /*
12887c478bd9Sstevel@tonic-gate  * Get one of the positive int or boolean properties.
12897c478bd9Sstevel@tonic-gate  */
12907c478bd9Sstevel@tonic-gate static int
get_prop(char * prop_name,di_node_t node)12917c478bd9Sstevel@tonic-gate get_prop(char *prop_name, di_node_t node)
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate 	int num;
12947c478bd9Sstevel@tonic-gate 	int *ip;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
12977c478bd9Sstevel@tonic-gate 	    >= 0) {
129842f64fdbSSarah Jelinek 		if (num == 0) {
129942f64fdbSSarah Jelinek 			/* boolean */
130042f64fdbSSarah Jelinek 			return (1);
130142f64fdbSSarah Jelinek 		} else if (num == 1) {
130242f64fdbSSarah Jelinek 			/* single int */
130342f64fdbSSarah Jelinek 			return (*ip);
130442f64fdbSSarah Jelinek 		}
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 	return (-1);
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate static char *
get_str_prop(char * prop_name,di_node_t node)13107c478bd9Sstevel@tonic-gate get_str_prop(char *prop_name, di_node_t node)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	char *str;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
131542f64fdbSSarah Jelinek 		return (str);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	return (NULL);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate  * Check if we have the drive in our list, based upon the device id, if the
13237c478bd9Sstevel@tonic-gate  * drive has a device id, or the kernel name, if it doesn't have a device id.
13247c478bd9Sstevel@tonic-gate  */
13257c478bd9Sstevel@tonic-gate static int
have_disk(struct search_args * args,char * devidstr,char * kernel_name,disk_t ** diskp)13267c478bd9Sstevel@tonic-gate have_disk(struct search_args *args, char *devidstr, char *kernel_name,
13277c478bd9Sstevel@tonic-gate     disk_t **diskp)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	disk_t *listp;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	*diskp = NULL;
13327c478bd9Sstevel@tonic-gate 	listp = args->disk_listp;
13337c478bd9Sstevel@tonic-gate 	if (devidstr != NULL) {
133442f64fdbSSarah Jelinek 		if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
133542f64fdbSSarah Jelinek 			return (1);
133642f64fdbSSarah Jelinek 		}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	} else {
133942f64fdbSSarah Jelinek 		/* no devid, try matching the kernel names on the drives */
134042f64fdbSSarah Jelinek 		while (listp != NULL) {
134142f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(kernel_name,
134242f64fdbSSarah Jelinek 			    listp->kernel_name)) {
134342f64fdbSSarah Jelinek 				*diskp = listp;
134442f64fdbSSarah Jelinek 				return (1);
134542f64fdbSSarah Jelinek 			}
134642f64fdbSSarah Jelinek 			listp = listp->next;
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	return (0);
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate static char *
bus_type(di_node_t node,di_minor_t minor,di_prom_handle_t ph)13537c478bd9Sstevel@tonic-gate bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	char	*type;
13567c478bd9Sstevel@tonic-gate 	int	i;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	type = get_prom_str("device_type", node, ph);
13597c478bd9Sstevel@tonic-gate 	if (type == NULL) {
136042f64fdbSSarah Jelinek 		type = di_node_name(node);
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	for (i = 0; bustypes[i]; i++) {
136442f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(type, bustypes[i])) {
136542f64fdbSSarah Jelinek 			return (type);
136642f64fdbSSarah Jelinek 		}
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	if (minor != NULL && strcmp(di_minor_nodetype(minor),
13707c478bd9Sstevel@tonic-gate 	    DDI_NT_USB_ATTACHMENT_POINT) == 0) {
137142f64fdbSSarah Jelinek 		return ("usb");
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	return (NULL);
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate /*
13787c478bd9Sstevel@tonic-gate  * If the input name is in c[t]ds format then return 1, otherwise return 0.
13797c478bd9Sstevel@tonic-gate  */
13807c478bd9Sstevel@tonic-gate static int
is_ctds(char * name)13817c478bd9Sstevel@tonic-gate is_ctds(char *name)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	char	*p;
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	p = name;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	if (*p++ != 'c') {
138842f64fdbSSarah Jelinek 		return (0);
13897c478bd9Sstevel@tonic-gate 	}
13907c478bd9Sstevel@tonic-gate 	/* skip controller digits */
13917c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
139242f64fdbSSarah Jelinek 		p++;
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	/* handle optional target */
13967c478bd9Sstevel@tonic-gate 	if (*p == 't') {
13977c478bd9Sstevel@tonic-gate 		p++;
139842f64fdbSSarah Jelinek 		/* skip over target */
139942f64fdbSSarah Jelinek 		while (isdigit(*p) || isupper(*p)) {
140042f64fdbSSarah Jelinek 			p++;
140142f64fdbSSarah Jelinek 		}
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	if (*p++ != 'd') {
140542f64fdbSSarah Jelinek 		return (0);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
140842f64fdbSSarah Jelinek 		p++;
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (*p++ != 's') {
141242f64fdbSSarah Jelinek 		return (0);
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/* check the slice number */
14167c478bd9Sstevel@tonic-gate 	while (isdigit(*p)) {
141742f64fdbSSarah Jelinek 		p++;
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	if (*p != 0) {
142142f64fdbSSarah Jelinek 		return (0);
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	return (1);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate static int
is_drive(di_minor_t minor)14287c478bd9Sstevel@tonic-gate is_drive(di_minor_t minor)
14297c478bd9Sstevel@tonic-gate {
143097ab9a43SJohn Levon 	return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK,
143197ab9a43SJohn Levon 	    strlen(DDI_NT_BLOCK)) == 0);
14327c478bd9Sstevel@tonic-gate }
14337c478bd9Sstevel@tonic-gate 
1434e7cbe64fSgw static int
is_zvol(di_node_t node,di_minor_t minor)1435e7cbe64fSgw is_zvol(di_node_t node, di_minor_t minor)
1436e7cbe64fSgw {
1437e7cbe64fSgw 	if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) &&
1438681d9761SEric Taylor 	    minor(di_minor_devt(minor)))
1439e7cbe64fSgw 		return (1);
1440e7cbe64fSgw 	return (0);
1441e7cbe64fSgw }
1442e7cbe64fSgw 
14437c478bd9Sstevel@tonic-gate static int
is_ctrl(di_node_t node,di_minor_t minor)1444c470f575SYuri Pankov is_ctrl(di_node_t node, di_minor_t minor)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	char	*type;
14477c478bd9Sstevel@tonic-gate 	char	*name;
14487c478bd9Sstevel@tonic-gate 	int	type_index;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	type = di_minor_nodetype(minor);
14517c478bd9Sstevel@tonic-gate 	type_index = 0;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	while (ctrltypes[type_index] != NULL) {
145442f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
145542f64fdbSSarah Jelinek 			return (1);
145642f64fdbSSarah Jelinek 		}
145742f64fdbSSarah Jelinek 		type_index++;
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
14617c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1462c470f575SYuri Pankov 	    (libdiskmgt_str_eq(name, "ide") ||
1463c470f575SYuri Pankov 	    libdiskmgt_str_eq(name, "xpvd")))
146442f64fdbSSarah Jelinek 		return (1);
14657c478bd9Sstevel@tonic-gate 
1466*f85f43edSRobert Mustacchi 	if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1467*f85f43edSRobert Mustacchi 	    libdiskmgt_str_eq(name, "lofi") &&
1468*f85f43edSRobert Mustacchi 	    libdiskmgt_str_eq(di_minor_name(minor), "ctl"))
1469*f85f43edSRobert Mustacchi 		return (1);
1470*f85f43edSRobert Mustacchi 
14717c478bd9Sstevel@tonic-gate 	return (0);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate static int
new_alias(disk_t * diskp,char * kernel_name,char * devlink_path,struct search_args * args)14757c478bd9Sstevel@tonic-gate new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
1476c470f575SYuri Pankov     struct search_args *args)
14777c478bd9Sstevel@tonic-gate {
14787c478bd9Sstevel@tonic-gate 	alias_t		*aliasp;
14797c478bd9Sstevel@tonic-gate 	char		alias[MAXPATHLEN];
14807c478bd9Sstevel@tonic-gate 	di_node_t	pnode;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	aliasp = malloc(sizeof (alias_t));
14837c478bd9Sstevel@tonic-gate 	if (aliasp == NULL) {
148442f64fdbSSarah Jelinek 		return (ENOMEM);
14857c478bd9Sstevel@tonic-gate 	}
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	aliasp->alias = NULL;
14887c478bd9Sstevel@tonic-gate 	aliasp->kstat_name = NULL;
14897c478bd9Sstevel@tonic-gate 	aliasp->wwn = NULL;
14907c478bd9Sstevel@tonic-gate 	aliasp->devpaths = NULL;
14917c478bd9Sstevel@tonic-gate 	aliasp->orig_paths = NULL;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	get_disk_name_from_path(devlink_path, alias, sizeof (alias));
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	aliasp->alias = strdup(alias);
14967c478bd9Sstevel@tonic-gate 	if (aliasp->alias == NULL) {
149742f64fdbSSarah Jelinek 		cache_free_alias(aliasp);
149842f64fdbSSarah Jelinek 		return (ENOMEM);
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	if (kernel_name != NULL) {
150242f64fdbSSarah Jelinek 		aliasp->kstat_name = strdup(kernel_name);
150342f64fdbSSarah Jelinek 		if (aliasp->kstat_name == NULL) {
150442f64fdbSSarah Jelinek 			cache_free_alias(aliasp);
150542f64fdbSSarah Jelinek 			return (ENOMEM);
150642f64fdbSSarah Jelinek 		}
15077c478bd9Sstevel@tonic-gate 	} else {
150842f64fdbSSarah Jelinek 		aliasp->kstat_name = NULL;
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	aliasp->lun = get_prop(DM_LUN, args->node);
15127c478bd9Sstevel@tonic-gate 	aliasp->target = get_prop(DM_TARGET, args->node);
15137c478bd9Sstevel@tonic-gate 	aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	pnode = di_parent_node(args->node);
15167c478bd9Sstevel@tonic-gate 	if (pnode != DI_NODE_NIL) {
151742f64fdbSSarah Jelinek 		char prop_name[MAXPROPLEN];
15187c478bd9Sstevel@tonic-gate 
151942f64fdbSSarah Jelinek 		(void) snprintf(prop_name, sizeof (prop_name),
152042f64fdbSSarah Jelinek 		    "target%d-sync-speed", aliasp->target);
152142f64fdbSSarah Jelinek 		diskp->sync_speed = get_prop(prop_name, pnode);
152242f64fdbSSarah Jelinek 		(void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
152342f64fdbSSarah Jelinek 		    aliasp->target);
152442f64fdbSSarah Jelinek 		diskp->wide = get_prop(prop_name, pnode);
15257c478bd9Sstevel@tonic-gate 	}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if (new_devpath(aliasp, devlink_path) != 0) {
152842f64fdbSSarah Jelinek 		cache_free_alias(aliasp);
152942f64fdbSSarah Jelinek 		return (ENOMEM);
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	aliasp->next = diskp->aliases;
15337c478bd9Sstevel@tonic-gate 	diskp->aliases = aliasp;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	return (0);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate  * Append the new devpath to the end of the devpath list.  This is important
15407c478bd9Sstevel@tonic-gate  * since we may want to use the order of the devpaths to match up the vtoc
15417c478bd9Sstevel@tonic-gate  * entries.
15427c478bd9Sstevel@tonic-gate  */
15437c478bd9Sstevel@tonic-gate static int
new_devpath(alias_t * ap,char * devpath)15447c478bd9Sstevel@tonic-gate new_devpath(alias_t *ap, char *devpath)
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate 	slice_t	*newdp;
15477c478bd9Sstevel@tonic-gate 	slice_t *alistp;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * First, search the alias list to be sure that this devpath is
15517c478bd9Sstevel@tonic-gate 	 * not already there.
15527c478bd9Sstevel@tonic-gate 	 */
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
155542f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
155642f64fdbSSarah Jelinek 			return (0);
155742f64fdbSSarah Jelinek 		}
15587c478bd9Sstevel@tonic-gate 	}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	/*
15617c478bd9Sstevel@tonic-gate 	 * Otherwise, not found so add this new devpath to the list.
15627c478bd9Sstevel@tonic-gate 	 */
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	newdp = malloc(sizeof (slice_t));
15657c478bd9Sstevel@tonic-gate 	if (newdp == NULL) {
156642f64fdbSSarah Jelinek 		return (ENOMEM);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	newdp->devpath = strdup(devpath);
15707c478bd9Sstevel@tonic-gate 	if (newdp->devpath == NULL) {
157142f64fdbSSarah Jelinek 		free(newdp);
157242f64fdbSSarah Jelinek 		return (ENOMEM);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 	newdp->slice_num = -1;
15757c478bd9Sstevel@tonic-gate 	newdp->next = NULL;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (ap->devpaths == NULL) {
157842f64fdbSSarah Jelinek 		ap->devpaths = newdp;
15797c478bd9Sstevel@tonic-gate 	} else {
158042f64fdbSSarah Jelinek 		/* append the devpath to the end of the list */
158142f64fdbSSarah Jelinek 		slice_t	*dp;
15827c478bd9Sstevel@tonic-gate 
158342f64fdbSSarah Jelinek 		dp = ap->devpaths;
158442f64fdbSSarah Jelinek 		while (dp->next != NULL) {
158542f64fdbSSarah Jelinek 			dp = dp->next;
158642f64fdbSSarah Jelinek 		}
15877c478bd9Sstevel@tonic-gate 
158842f64fdbSSarah Jelinek 		dp->next = newdp;
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	return (0);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate static path_t *
new_path(controller_t * cp,disk_t * dp,di_node_t node,di_path_state_t st,char * wwn)15957c478bd9Sstevel@tonic-gate new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
1596c470f575SYuri Pankov     char *wwn)
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	char		*devpath;
15997c478bd9Sstevel@tonic-gate 	path_t		*pp;
16007c478bd9Sstevel@tonic-gate 	di_minor_t	minor;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	/* Special handling for fp attachment node. */
16037c478bd9Sstevel@tonic-gate 	if (strcmp(di_node_name(node), "fp") == 0) {
160442f64fdbSSarah Jelinek 		di_node_t pnode;
16057c478bd9Sstevel@tonic-gate 
160642f64fdbSSarah Jelinek 		pnode = di_parent_node(node);
160742f64fdbSSarah Jelinek 		if (pnode != DI_NODE_NIL) {
160842f64fdbSSarah Jelinek 			node = pnode;
160942f64fdbSSarah Jelinek 		}
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	devpath = di_devfs_path(node);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/* check if the path is already there */
16157c478bd9Sstevel@tonic-gate 	pp = NULL;
16167c478bd9Sstevel@tonic-gate 	if (cp->paths != NULL) {
161742f64fdbSSarah Jelinek 		int i;
16187c478bd9Sstevel@tonic-gate 
161942f64fdbSSarah Jelinek 		for (i = 0; cp->paths[i]; i++) {
162042f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
162142f64fdbSSarah Jelinek 				pp = cp->paths[i];
162242f64fdbSSarah Jelinek 				break;
162342f64fdbSSarah Jelinek 			}
16247c478bd9Sstevel@tonic-gate 		}
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
162842f64fdbSSarah Jelinek 		/* the path exists, add this disk to it */
16297c478bd9Sstevel@tonic-gate 
163042f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
163142f64fdbSSarah Jelinek 		if (!add_disk2path(dp, pp, st, wwn)) {
163242f64fdbSSarah Jelinek 			return (NULL);
163342f64fdbSSarah Jelinek 		}
163442f64fdbSSarah Jelinek 		return (pp);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	/* create a new path */
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	pp = calloc(1, sizeof (path_t));
16407c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
164142f64fdbSSarah Jelinek 		di_devfs_path_free((void *) devpath);
164242f64fdbSSarah Jelinek 		return (NULL);
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	pp->name = strdup(devpath);
16467c478bd9Sstevel@tonic-gate 	di_devfs_path_free((void *) devpath);
16477c478bd9Sstevel@tonic-gate 	if (pp->name == NULL) {
164842f64fdbSSarah Jelinek 		cache_free_path(pp);
164942f64fdbSSarah Jelinek 		return (NULL);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	/* add the disk to the path */
16537c478bd9Sstevel@tonic-gate 	if (!add_disk2path(dp, pp, st, wwn)) {
165442f64fdbSSarah Jelinek 		return (NULL);
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	/* add the path to the controller */
16587c478bd9Sstevel@tonic-gate 	if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
165942f64fdbSSarah Jelinek 		cache_free_path(pp);
166042f64fdbSSarah Jelinek 		return (NULL);
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	/* add the controller to the path */
16647c478bd9Sstevel@tonic-gate 	pp->controller = cp;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	minor = di_minor_next(node, NULL);
16677c478bd9Sstevel@tonic-gate 	if (minor != NULL) {
166842f64fdbSSarah Jelinek 		pp->ctype = ctype(node, minor);
16697c478bd9Sstevel@tonic-gate 	} else {
167042f64fdbSSarah Jelinek 		pp->ctype = DM_CTYPE_UNKNOWN;
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	return (pp);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate  * We pass in the current controller pointer (currp) so we can double check
16787c478bd9Sstevel@tonic-gate  * that we aren't corrupting the list by removing the element we are on.  This
16797c478bd9Sstevel@tonic-gate  * should never happen, but it doesn't hurt to double check.
16807c478bd9Sstevel@tonic-gate  */
16817c478bd9Sstevel@tonic-gate static void
remove_invalid_controller(char * name,controller_t * currp,struct search_args * args)16827c478bd9Sstevel@tonic-gate remove_invalid_controller(char *name, controller_t *currp,
16837c478bd9Sstevel@tonic-gate     struct search_args *args)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	controller_t *cp;
16867c478bd9Sstevel@tonic-gate 	bus_t *bp;
16877c478bd9Sstevel@tonic-gate 	controller_t *prevp;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	bp = args->bus_listp;
16907c478bd9Sstevel@tonic-gate 	while (bp != NULL) {
169142f64fdbSSarah Jelinek 		int i;
16927c478bd9Sstevel@tonic-gate 
169342f64fdbSSarah Jelinek 		for (i = 0; bp->controllers[i]; i++) {
169442f64fdbSSarah Jelinek 			if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
169542f64fdbSSarah Jelinek 				int j;
169642f64fdbSSarah Jelinek 				/*
169742f64fdbSSarah Jelinek 				 * remove pointer to invalid controller.
169842f64fdbSSarah Jelinek 				 * (it is a path)
169942f64fdbSSarah Jelinek 				 */
170042f64fdbSSarah Jelinek 				for (j = i; bp->controllers[j]; j++) {
170142f64fdbSSarah Jelinek 					bp->controllers[j] =
170242f64fdbSSarah Jelinek 					    bp->controllers[j + 1];
170342f64fdbSSarah Jelinek 				}
170442f64fdbSSarah Jelinek 			}
17057c478bd9Sstevel@tonic-gate 		}
170642f64fdbSSarah Jelinek 		bp = bp->next;
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (args->controller_listp == NULL) {
171042f64fdbSSarah Jelinek 		return;
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	cp = args->controller_listp;
17147c478bd9Sstevel@tonic-gate 	if (libdiskmgt_str_eq(cp->name, name)) {
17157c478bd9Sstevel@tonic-gate 		args->controller_listp = cp->next;
171642f64fdbSSarah Jelinek 		if (dm_debug) {
171742f64fdbSSarah Jelinek 			(void) fprintf(stderr,
171842f64fdbSSarah Jelinek 			    "INFO: Removed controller %s from list\n",
171942f64fdbSSarah Jelinek 			    cp->name);
172042f64fdbSSarah Jelinek 		}
172142f64fdbSSarah Jelinek 		remove_controller(cp, currp);
172242f64fdbSSarah Jelinek 		return;
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	prevp = cp;
17267c478bd9Sstevel@tonic-gate 	cp = cp->next;
17277c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
172842f64fdbSSarah Jelinek 		if (libdiskmgt_str_eq(cp->name, name)) {
172942f64fdbSSarah Jelinek 			if (dm_debug) {
173042f64fdbSSarah Jelinek 				(void) fprintf(stderr,
173142f64fdbSSarah Jelinek 				    "INFO: Removed controller %s from list\n",
173242f64fdbSSarah Jelinek 				    cp->name);
173342f64fdbSSarah Jelinek 			}
173442f64fdbSSarah Jelinek 			prevp->next = cp->next;
173542f64fdbSSarah Jelinek 			remove_controller(cp, currp);
173642f64fdbSSarah Jelinek 			return;
17377c478bd9Sstevel@tonic-gate 		}
173842f64fdbSSarah Jelinek 		prevp = cp;
173942f64fdbSSarah Jelinek 		cp = cp->next;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate }
1742