1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "mdescplugin.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate static char *device_format_disk_name(char *devfs_path);
32*0Sstevel@tonic-gate static char *device_get_disk_name_from_dir(char *basedir, char *path);
33*0Sstevel@tonic-gate static cfga_list_data_t *device_get_disk_cfga_info(char *cfgpath);
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /* These 3 variable are defined and set in mdescplugin.c */
36*0Sstevel@tonic-gate extern picl_nodehdl_t	root_node;
37*0Sstevel@tonic-gate extern md_t		*mdp;
38*0Sstevel@tonic-gate extern mde_cookie_t	rootnode;
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /* This routine is defined in cpu_prop_update.c */
41*0Sstevel@tonic-gate extern void set_prop_info(ptree_propinfo_t *propinfo, int size, char *name,
42*0Sstevel@tonic-gate     int type);
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate int
disk_discovery(void)45*0Sstevel@tonic-gate disk_discovery(void)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	int			status = PICL_FAILURE;
48*0Sstevel@tonic-gate 	picl_nodehdl_t		discovery_node;
49*0Sstevel@tonic-gate 	picl_nodehdl_t		new_node;
50*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
51*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
52*0Sstevel@tonic-gate 	char			*cfgpath, *dev_path, *nac;
53*0Sstevel@tonic-gate 	cfga_list_data_t	*disk_data;
54*0Sstevel@tonic-gate 	int			x, num_nodes, ndisks;
55*0Sstevel@tonic-gate 	mde_cookie_t		*disklistp;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	num_nodes = md_node_count(mdp);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	disklistp = (mde_cookie_t *) alloca(sizeof (mde_cookie_t) *num_nodes);
60*0Sstevel@tonic-gate 	if (disklistp == NULL) {
61*0Sstevel@tonic-gate 		return (status);
62*0Sstevel@tonic-gate 	}
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	/*
65*0Sstevel@tonic-gate 	 * Starting at the root node, scan the "fwd" dag for
66*0Sstevel@tonic-gate 	 * all the disks in this description.
67*0Sstevel@tonic-gate 	 */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	ndisks = md_scan_dag(mdp, rootnode, md_find_name(mdp, "disk_nac"),
70*0Sstevel@tonic-gate 	    md_find_name(mdp, "fwd"), disklistp);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	if (ndisks <= 0) {
73*0Sstevel@tonic-gate 		return (status);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	status = ptree_create_and_add_node(root_node, DISK_DISCOVERY_NAME,
77*0Sstevel@tonic-gate 	    PICL_CLASS_PICL, &discovery_node);
78*0Sstevel@tonic-gate 	if (status != PICL_SUCCESS)
79*0Sstevel@tonic-gate 		return (status);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	for (x = 0; x < ndisks; x++) {
82*0Sstevel@tonic-gate 		if (md_get_prop_str(mdp, disklistp[x], "phys_path",
83*0Sstevel@tonic-gate 			&dev_path) != 0) {
84*0Sstevel@tonic-gate 			continue;
85*0Sstevel@tonic-gate 		}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 		if (md_get_prop_str(mdp, disklistp[x], "nac_name",
88*0Sstevel@tonic-gate 			&nac) != 0) {
89*0Sstevel@tonic-gate 			continue;
90*0Sstevel@tonic-gate 		}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 		(void) ptree_create_and_add_node(discovery_node, "disk",
93*0Sstevel@tonic-gate 		    PICL_CLASS_DISK, &new_node);
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 		set_prop_info(&propinfo, PICL_PROPNAMELEN_MAX, "Path",
96*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(new_node, &propinfo,
99*0Sstevel@tonic-gate 		    (void *)dev_path, &proph);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		set_prop_info(&propinfo, PICL_PROPNAMELEN_MAX, "Location",
102*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(new_node, &propinfo,
105*0Sstevel@tonic-gate 		    (void *)nac, &proph);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		set_prop_info(&propinfo, PICL_PROPNAMELEN_MAX, "State",
108*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 		cfgpath = device_format_disk_name(dev_path);
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		if (cfgpath == NULL) {
113*0Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(new_node, &propinfo,
114*0Sstevel@tonic-gate 			    (void *)strdup(UNCONFIGURED), &proph);
115*0Sstevel@tonic-gate 			continue;
116*0Sstevel@tonic-gate 		}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 		disk_data = device_get_disk_cfga_info(cfgpath);
119*0Sstevel@tonic-gate 		if (disk_data == NULL) {
120*0Sstevel@tonic-gate 			continue;
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		switch (disk_data->ap_o_state) {
124*0Sstevel@tonic-gate 		case CFGA_STAT_UNCONFIGURED:
125*0Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(new_node, &propinfo,
126*0Sstevel@tonic-gate 			    (void *)strdup(UNCONFIGURED), &proph);
127*0Sstevel@tonic-gate 			break;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		case CFGA_STAT_CONFIGURED:
130*0Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(new_node, &propinfo,
131*0Sstevel@tonic-gate 			    (void *)strdup(CONFIGURED), &proph);
132*0Sstevel@tonic-gate 			break;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		default:
135*0Sstevel@tonic-gate 			break;
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	return (status);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate static cfga_list_data_t *
device_get_disk_cfga_info(char * cfgpath)142*0Sstevel@tonic-gate device_get_disk_cfga_info(char *cfgpath)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	char			**apid_names;
145*0Sstevel@tonic-gate 	char			apid_name[CFGA_AP_LOG_ID_LEN];
146*0Sstevel@tonic-gate 	cfga_err_t		cfga_err;
147*0Sstevel@tonic-gate 	struct cfga_list_data	*list_data;
148*0Sstevel@tonic-gate 	int			list_len, count;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	(void) strcpy(apid_name, cfgpath);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	apid_names = (char **)malloc(2 * sizeof (char *));
153*0Sstevel@tonic-gate 	apid_names[0] = apid_name;
154*0Sstevel@tonic-gate 	apid_names[1] = NULL;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	cfga_err = config_list_ext(1, (char * const *)apid_names, &list_data,
157*0Sstevel@tonic-gate 	    &list_len, NULL, NULL, NULL, CFGA_FLAG_LIST_ALL);
158*0Sstevel@tonic-gate 	free(apid_names);
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if (cfga_err != CFGA_OK || list_len == 0) {
161*0Sstevel@tonic-gate 		return (NULL);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/* free any extra entries if this is not unique */
165*0Sstevel@tonic-gate 	if (list_len > 1) {
166*0Sstevel@tonic-gate 		for (count = 1; count < list_len; count++) {
167*0Sstevel@tonic-gate 			free(&list_data[count]);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	return (&list_data[0]);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate static char *
device_format_disk_name(char * devfs_path)176*0Sstevel@tonic-gate device_format_disk_name(char *devfs_path)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	char	devname[256];
179*0Sstevel@tonic-gate 	char	*diskname, *dev_cpy;
180*0Sstevel@tonic-gate 	char	apid_name[CFGA_AP_LOG_ID_LEN];
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	(void) snprintf(devname, sizeof (devname), "/devices%s:a,raw",
183*0Sstevel@tonic-gate 	    devfs_path);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	diskname = device_get_disk_name_from_dir("/dev/rdsk", devname);
186*0Sstevel@tonic-gate 	if (diskname != NULL) {
187*0Sstevel@tonic-gate 		*strrchr(diskname, 's') = '\0';
188*0Sstevel@tonic-gate 		dev_cpy = strdup(diskname);
189*0Sstevel@tonic-gate 		*strchr(dev_cpy, 't') = '\0';
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		(void) snprintf(apid_name, sizeof (apid_name), "%s::dsk/%s",
192*0Sstevel@tonic-gate 			dev_cpy, diskname);
193*0Sstevel@tonic-gate 		return (strdup(apid_name));
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	return (NULL);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate /*
200*0Sstevel@tonic-gate  * Getting a disk name is annoying.  Walking controllers
201*0Sstevel@tonic-gate  * doesn't work if disks were added out of order (ie a new
202*0Sstevel@tonic-gate  * controller card was installed), and DKIO controller numbers
203*0Sstevel@tonic-gate  * seem to always be 0.  So we do it the old fashioned way:
204*0Sstevel@tonic-gate  *
205*0Sstevel@tonic-gate  * We get a target name (in the /devices tree), and we want
206*0Sstevel@tonic-gate  * the node in /dev/rdsk that is a symlink to it.  So we walk
207*0Sstevel@tonic-gate  * /dev/rdsk, stating each entry.  Since stat follows the symlink
208*0Sstevel@tonic-gate  * automatically, we just compare the device and inode numbers
209*0Sstevel@tonic-gate  * to the device and inode numbers of the target.  According to
210*0Sstevel@tonic-gate  * the stat man page, this constitues a unique match.  The only
211*0Sstevel@tonic-gate  * little cleanup is that this includes a slice #, which we take
212*0Sstevel@tonic-gate  *  off.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate static char *
device_get_disk_name_from_dir(char * basedir,char * path)216*0Sstevel@tonic-gate device_get_disk_name_from_dir(char *basedir, char *path)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	DIR		*dir;
219*0Sstevel@tonic-gate 	struct dirent	*dirent;
220*0Sstevel@tonic-gate 	struct stat	srcstat, targstat;
221*0Sstevel@tonic-gate 	int		loc_err;
222*0Sstevel@tonic-gate 	char		fullname[256 + MAXNAMLEN];
223*0Sstevel@tonic-gate 	char		*ptr;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	loc_err = stat(path, &srcstat);
226*0Sstevel@tonic-gate 	if (loc_err < 0) {
227*0Sstevel@tonic-gate 		return (NULL);
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	dir = opendir(basedir);
231*0Sstevel@tonic-gate 	if (dir == NULL) {
232*0Sstevel@tonic-gate 		return (NULL);
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	while ((dirent = readdir(dir)) != NULL) {
236*0Sstevel@tonic-gate 		(void) snprintf(fullname, sizeof (fullname),
237*0Sstevel@tonic-gate 			"%s/%s", basedir, dirent->d_name);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 		loc_err = stat(fullname, &targstat);
240*0Sstevel@tonic-gate 		if (loc_err == 0) {
241*0Sstevel@tonic-gate 			if ((memcmp((void *)&(targstat.st_ino),
242*0Sstevel@tonic-gate 				(void *)&(srcstat.st_ino),
243*0Sstevel@tonic-gate 				sizeof (ino_t)) == 0) &&
244*0Sstevel@tonic-gate 				(memcmp((void *)&(targstat.st_dev),
245*0Sstevel@tonic-gate 				(void *)&(srcstat.st_dev),
246*0Sstevel@tonic-gate 				sizeof (dev_t)) == 0)) {
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 				ptr = strdup(dirent->d_name);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 				(void) closedir(dir);
251*0Sstevel@tonic-gate 				return (ptr);
252*0Sstevel@tonic-gate 			}
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	(void) closedir(dir);
257*0Sstevel@tonic-gate 	return (NULL);
258*0Sstevel@tonic-gate }
259