1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
28*7c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/scsi/conf/autoconf.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
35*7c478bd9Sstevel@tonic-gate #include "disks_private.h"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate static descriptor_t	**get_assoc_buses(descriptor_t *desc, int *errp);
38*7c478bd9Sstevel@tonic-gate static descriptor_t	**get_assoc_drives(descriptor_t *desc, int *errp);
39*7c478bd9Sstevel@tonic-gate static descriptor_t	**get_assoc_paths(descriptor_t *desc, int *errp);
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate descriptor_t **
controller_get_assoc_descriptors(descriptor_t * desc,dm_desc_type_t type,int * errp)42*7c478bd9Sstevel@tonic-gate controller_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
43*7c478bd9Sstevel@tonic-gate     int *errp)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	switch (type) {
46*7c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
47*7c478bd9Sstevel@tonic-gate 	    return (get_assoc_drives(desc, errp));
48*7c478bd9Sstevel@tonic-gate 	case DM_PATH:
49*7c478bd9Sstevel@tonic-gate 	    return (get_assoc_paths(desc, errp));
50*7c478bd9Sstevel@tonic-gate 	case DM_BUS:
51*7c478bd9Sstevel@tonic-gate 	    return (get_assoc_buses(desc, errp));
52*7c478bd9Sstevel@tonic-gate 	}
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
55*7c478bd9Sstevel@tonic-gate 	return (NULL);
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate nvlist_t *
controller_get_attributes(descriptor_t * dp,int * errp)59*7c478bd9Sstevel@tonic-gate controller_get_attributes(descriptor_t *dp, int *errp)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	controller_t	*cp;
62*7c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
65*7c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
66*7c478bd9Sstevel@tonic-gate 	    return (NULL);
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	cp = dp->p.controller;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(attrs, DM_CTYPE, cp->ctype) != 0) {
72*7c478bd9Sstevel@tonic-gate 	    nvlist_free(attrs);
73*7c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
74*7c478bd9Sstevel@tonic-gate 	    return (NULL);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (cp->multiplex) {
78*7c478bd9Sstevel@tonic-gate 	    if (nvlist_add_boolean(attrs, DM_MULTIPLEX) != 0) {
79*7c478bd9Sstevel@tonic-gate 		nvlist_free(attrs);
80*7c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
81*7c478bd9Sstevel@tonic-gate 		return (NULL);
82*7c478bd9Sstevel@tonic-gate 	    }
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (cp->scsi_options != -1) {
86*7c478bd9Sstevel@tonic-gate 	    if (cp->scsi_options & SCSI_OPTIONS_FAST) {
87*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_FAST) != 0) {
88*7c478bd9Sstevel@tonic-gate 		    nvlist_free(attrs);
89*7c478bd9Sstevel@tonic-gate 		    *errp = ENOMEM;
90*7c478bd9Sstevel@tonic-gate 		    return (NULL);
91*7c478bd9Sstevel@tonic-gate 		}
92*7c478bd9Sstevel@tonic-gate 	    }
93*7c478bd9Sstevel@tonic-gate 	    if (cp->scsi_options & SCSI_OPTIONS_WIDE) {
94*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_WIDE) != 0) {
95*7c478bd9Sstevel@tonic-gate 		    nvlist_free(attrs);
96*7c478bd9Sstevel@tonic-gate 		    *errp = ENOMEM;
97*7c478bd9Sstevel@tonic-gate 		    return (NULL);
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 	    }
100*7c478bd9Sstevel@tonic-gate 	    if (cp->scsi_options & SCSI_OPTIONS_FAST20) {
101*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_FAST20) != 0) {
102*7c478bd9Sstevel@tonic-gate 		    nvlist_free(attrs);
103*7c478bd9Sstevel@tonic-gate 		    *errp = ENOMEM;
104*7c478bd9Sstevel@tonic-gate 		    return (NULL);
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 	    }
107*7c478bd9Sstevel@tonic-gate 	    if (cp->scsi_options & SCSI_OPTIONS_FAST40) {
108*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_FAST40) != 0) {
109*7c478bd9Sstevel@tonic-gate 		    nvlist_free(attrs);
110*7c478bd9Sstevel@tonic-gate 		    *errp = ENOMEM;
111*7c478bd9Sstevel@tonic-gate 		    return (NULL);
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 	    }
114*7c478bd9Sstevel@tonic-gate 	    if (cp->scsi_options & SCSI_OPTIONS_FAST80) {
115*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_FAST80) != 0) {
116*7c478bd9Sstevel@tonic-gate 		    nvlist_free(attrs);
117*7c478bd9Sstevel@tonic-gate 		    *errp = ENOMEM;
118*7c478bd9Sstevel@tonic-gate 		    return (NULL);
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 	    }
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (cp->freq != 0) {
124*7c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_CLOCK, cp->freq) != 0) {
125*7c478bd9Sstevel@tonic-gate 		nvlist_free(attrs);
126*7c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
127*7c478bd9Sstevel@tonic-gate 		return (NULL);
128*7c478bd9Sstevel@tonic-gate 	    }
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	*errp = 0;
132*7c478bd9Sstevel@tonic-gate 	return (attrs);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate descriptor_t *
controller_get_descriptor_by_name(char * name,int * errp)136*7c478bd9Sstevel@tonic-gate controller_get_descriptor_by_name(char *name, int *errp)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	descriptor_t	**controllers;
139*7c478bd9Sstevel@tonic-gate 	int		i;
140*7c478bd9Sstevel@tonic-gate 	descriptor_t	*controller = NULL;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	controllers = cache_get_descriptors(DM_CONTROLLER, errp);
143*7c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
144*7c478bd9Sstevel@tonic-gate 	    return (NULL);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	for (i = 0; controllers[i]; i++) {
148*7c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, controllers[i]->p.controller->name)) {
149*7c478bd9Sstevel@tonic-gate 		controller = controllers[i];
150*7c478bd9Sstevel@tonic-gate 	    } else {
151*7c478bd9Sstevel@tonic-gate 		/* clean up the unused descriptors */
152*7c478bd9Sstevel@tonic-gate 		cache_free_descriptor(controllers[i]);
153*7c478bd9Sstevel@tonic-gate 	    }
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 	free(controllers);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	if (controller == NULL) {
158*7c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	return (controller);
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
165*7c478bd9Sstevel@tonic-gate descriptor_t **
controller_get_descriptors(int filter[],int * errp)166*7c478bd9Sstevel@tonic-gate controller_get_descriptors(int filter[], int *errp)
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate 	return (cache_get_descriptors(DM_CONTROLLER, errp));
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate char *
controller_get_name(descriptor_t * desc)172*7c478bd9Sstevel@tonic-gate controller_get_name(descriptor_t *desc)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	return (desc->p.controller->name);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
178*7c478bd9Sstevel@tonic-gate nvlist_t *
controller_get_stats(descriptor_t * dp,int stat_type,int * errp)179*7c478bd9Sstevel@tonic-gate controller_get_stats(descriptor_t *dp, int stat_type, int *errp)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	/* There are no stat types defined for controllers */
182*7c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
183*7c478bd9Sstevel@tonic-gate 	return (NULL);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate int
controller_make_descriptors()187*7c478bd9Sstevel@tonic-gate controller_make_descriptors()
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	int		error;
190*7c478bd9Sstevel@tonic-gate 	controller_t	*cp;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	cp = cache_get_controllerlist();
193*7c478bd9Sstevel@tonic-gate 	while (cp != NULL) {
194*7c478bd9Sstevel@tonic-gate 	    cache_load_desc(DM_CONTROLLER, cp, NULL, NULL, &error);
195*7c478bd9Sstevel@tonic-gate 	    if (error != 0) {
196*7c478bd9Sstevel@tonic-gate 		return (error);
197*7c478bd9Sstevel@tonic-gate 	    }
198*7c478bd9Sstevel@tonic-gate 	    cp = cp->next;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	return (0);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate static descriptor_t **
get_assoc_buses(descriptor_t * desc,int * errp)205*7c478bd9Sstevel@tonic-gate get_assoc_buses(descriptor_t *desc, int *errp)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	controller_t	*cp;
208*7c478bd9Sstevel@tonic-gate 	descriptor_t	**buses;
209*7c478bd9Sstevel@tonic-gate 	int		pos = 0;
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	cp = desc->p.controller;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/* make the snapshot */
214*7c478bd9Sstevel@tonic-gate 	buses = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
215*7c478bd9Sstevel@tonic-gate 	if (buses == NULL) {
216*7c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
217*7c478bd9Sstevel@tonic-gate 	    return (NULL);
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if (cp->bus != NULL) {
221*7c478bd9Sstevel@tonic-gate 	    buses[pos++] = cache_get_desc(DM_BUS, cp->bus, NULL, NULL, errp);
222*7c478bd9Sstevel@tonic-gate 	    if (*errp != 0) {
223*7c478bd9Sstevel@tonic-gate 		cache_free_descriptors(buses);
224*7c478bd9Sstevel@tonic-gate 		return (NULL);
225*7c478bd9Sstevel@tonic-gate 	    }
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 	buses[pos] = NULL;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	*errp = 0;
230*7c478bd9Sstevel@tonic-gate 	return (buses);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate static descriptor_t **
get_assoc_drives(descriptor_t * desc,int * errp)234*7c478bd9Sstevel@tonic-gate get_assoc_drives(descriptor_t *desc, int *errp)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	controller_t	*cp;
237*7c478bd9Sstevel@tonic-gate 	descriptor_t	**drives;
238*7c478bd9Sstevel@tonic-gate 	int		cnt;
239*7c478bd9Sstevel@tonic-gate 	int		i;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	cp = desc->p.controller;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	/* Count how many we have. */
244*7c478bd9Sstevel@tonic-gate 	for (cnt = 0; cp->disks[cnt]; cnt++);
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	/* make the snapshot */
247*7c478bd9Sstevel@tonic-gate 	drives = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
248*7c478bd9Sstevel@tonic-gate 	if (drives == NULL) {
249*7c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
250*7c478bd9Sstevel@tonic-gate 	    return (NULL);
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	for (i = 0; cp->disks[i]; i++) {
254*7c478bd9Sstevel@tonic-gate 	    drives[i] = cache_get_desc(DM_DRIVE, cp->disks[i], NULL, NULL,
255*7c478bd9Sstevel@tonic-gate 		errp);
256*7c478bd9Sstevel@tonic-gate 	    if (*errp != 0) {
257*7c478bd9Sstevel@tonic-gate 		cache_free_descriptors(drives);
258*7c478bd9Sstevel@tonic-gate 		return (NULL);
259*7c478bd9Sstevel@tonic-gate 	    }
260*7c478bd9Sstevel@tonic-gate 	}
261*7c478bd9Sstevel@tonic-gate 	drives[i] = NULL;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	*errp = 0;
264*7c478bd9Sstevel@tonic-gate 	return (drives);
265*7c478bd9Sstevel@tonic-gate }
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate static descriptor_t **
get_assoc_paths(descriptor_t * desc,int * errp)268*7c478bd9Sstevel@tonic-gate get_assoc_paths(descriptor_t *desc, int *errp)
269*7c478bd9Sstevel@tonic-gate {
270*7c478bd9Sstevel@tonic-gate 	path_t		**pp;
271*7c478bd9Sstevel@tonic-gate 	int		cnt;
272*7c478bd9Sstevel@tonic-gate 	descriptor_t	**paths;
273*7c478bd9Sstevel@tonic-gate 	int		i;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	pp = desc->p.controller->paths;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	/* Count how many we have. */
278*7c478bd9Sstevel@tonic-gate 	cnt = 0;
279*7c478bd9Sstevel@tonic-gate 	if (pp != NULL) {
280*7c478bd9Sstevel@tonic-gate 	    for (; pp[cnt]; cnt++);
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	/* make the snapshot */
284*7c478bd9Sstevel@tonic-gate 	paths = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
285*7c478bd9Sstevel@tonic-gate 	if (paths == NULL) {
286*7c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
287*7c478bd9Sstevel@tonic-gate 	    return (NULL);
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	/*
291*7c478bd9Sstevel@tonic-gate 	 * The name field of the descriptor is not filled in.  Thus, we
292*7c478bd9Sstevel@tonic-gate 	 * know not to try to lookup drive-path state information within
293*7c478bd9Sstevel@tonic-gate 	 * the path code if we try to get attributes for this descriptor.
294*7c478bd9Sstevel@tonic-gate 	 */
295*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
296*7c478bd9Sstevel@tonic-gate 	    paths[i] = cache_get_desc(DM_PATH, pp[i], NULL, NULL, errp);
297*7c478bd9Sstevel@tonic-gate 	    if (*errp != 0) {
298*7c478bd9Sstevel@tonic-gate 		cache_free_descriptors(paths);
299*7c478bd9Sstevel@tonic-gate 		return (NULL);
300*7c478bd9Sstevel@tonic-gate 	    }
301*7c478bd9Sstevel@tonic-gate 	}
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	paths[i] = NULL;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	*errp = 0;
306*7c478bd9Sstevel@tonic-gate 	return (paths);
307*7c478bd9Sstevel@tonic-gate }
308