xref: /onnv-gate/usr/src/lib/libdiskmgt/common/bus.c (revision 0:68f95e015346)
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 2004 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 <fcntl.h>
30*0Sstevel@tonic-gate #include <libdevinfo.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <sys/sunddi.h>
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/scsi/conf/autoconf.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include "libdiskmgt.h"
37*0Sstevel@tonic-gate #include "disks_private.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static descriptor_t	**get_assoc_buses(descriptor_t *desc, int *errp);
40*0Sstevel@tonic-gate static descriptor_t	**get_assoc_controllers(descriptor_t *desc, int *errp);
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate descriptor_t **
bus_get_assoc_descriptors(descriptor_t * desc,dm_desc_type_t type,int * errp)43*0Sstevel@tonic-gate bus_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type, int *errp)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	switch (type) {
46*0Sstevel@tonic-gate 	case DM_BUS:
47*0Sstevel@tonic-gate 	    return (get_assoc_buses(desc, errp));
48*0Sstevel@tonic-gate 	case DM_CONTROLLER:
49*0Sstevel@tonic-gate 	    return (get_assoc_controllers(desc, errp));
50*0Sstevel@tonic-gate 	}
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	*errp = EINVAL;
53*0Sstevel@tonic-gate 	return (NULL);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate nvlist_t *
bus_get_attributes(descriptor_t * dp,int * errp)57*0Sstevel@tonic-gate bus_get_attributes(descriptor_t *dp, int *errp)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	bus_t		*bp;
60*0Sstevel@tonic-gate 	nvlist_t	*attrs;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
63*0Sstevel@tonic-gate 	    *errp = ENOMEM;
64*0Sstevel@tonic-gate 	    return (NULL);
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	bp = dp->p.bus;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if (nvlist_add_string(attrs, DM_BTYPE, bp->btype) != 0) {
70*0Sstevel@tonic-gate 	    nvlist_free(attrs);
71*0Sstevel@tonic-gate 	    *errp = ENOMEM;
72*0Sstevel@tonic-gate 	    return (NULL);
73*0Sstevel@tonic-gate 	}
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (bp->freq != 0) {
76*0Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_CLOCK, bp->freq) != 0) {
77*0Sstevel@tonic-gate 		nvlist_free(attrs);
78*0Sstevel@tonic-gate 		*errp = ENOMEM;
79*0Sstevel@tonic-gate 		return (NULL);
80*0Sstevel@tonic-gate 	    }
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	if (bp->pname != NULL) {
84*0Sstevel@tonic-gate 	    if (nvlist_add_string(attrs, DM_PNAME, bp->pname) != 0) {
85*0Sstevel@tonic-gate 		nvlist_free(attrs);
86*0Sstevel@tonic-gate 		*errp = ENOMEM;
87*0Sstevel@tonic-gate 		return (NULL);
88*0Sstevel@tonic-gate 	    }
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	*errp = 0;
92*0Sstevel@tonic-gate 	return (attrs);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate descriptor_t *
bus_get_descriptor_by_name(char * name,int * errp)96*0Sstevel@tonic-gate bus_get_descriptor_by_name(char *name, int *errp)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	descriptor_t	**buses;
99*0Sstevel@tonic-gate 	int		i;
100*0Sstevel@tonic-gate 	descriptor_t	*bus = NULL;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	buses = cache_get_descriptors(DM_BUS, errp);
103*0Sstevel@tonic-gate 	if (*errp != 0) {
104*0Sstevel@tonic-gate 	    return (NULL);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	for (i = 0; buses[i]; i++) {
108*0Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, buses[i]->p.bus->name)) {
109*0Sstevel@tonic-gate 		bus = buses[i];
110*0Sstevel@tonic-gate 	    } else {
111*0Sstevel@tonic-gate 		/* clean up the unused descriptors */
112*0Sstevel@tonic-gate 		cache_free_descriptor(buses[i]);
113*0Sstevel@tonic-gate 	    }
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	free(buses);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if (bus == NULL) {
118*0Sstevel@tonic-gate 	    *errp = ENODEV;
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	return (bus);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate /* ARGSUSED */
125*0Sstevel@tonic-gate descriptor_t **
bus_get_descriptors(int filter[],int * errp)126*0Sstevel@tonic-gate bus_get_descriptors(int filter[], int *errp)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	return (cache_get_descriptors(DM_BUS, errp));
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate char *
bus_get_name(descriptor_t * desc)132*0Sstevel@tonic-gate bus_get_name(descriptor_t *desc)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	return (desc->p.bus->name);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /* ARGSUSED */
138*0Sstevel@tonic-gate nvlist_t *
bus_get_stats(descriptor_t * dp,int stat_type,int * errp)139*0Sstevel@tonic-gate bus_get_stats(descriptor_t *dp, int stat_type, int *errp)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	/* There are no stat types defined for controllers */
142*0Sstevel@tonic-gate 	*errp = EINVAL;
143*0Sstevel@tonic-gate 	return (NULL);
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate int
bus_make_descriptors()147*0Sstevel@tonic-gate bus_make_descriptors()
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	int	error;
150*0Sstevel@tonic-gate 	bus_t	*bp;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	bp = cache_get_buslist();
153*0Sstevel@tonic-gate 	while (bp != NULL) {
154*0Sstevel@tonic-gate 	    cache_load_desc(DM_BUS, bp, NULL, NULL, &error);
155*0Sstevel@tonic-gate 	    if (error != 0) {
156*0Sstevel@tonic-gate 		return (error);
157*0Sstevel@tonic-gate 	    }
158*0Sstevel@tonic-gate 	    bp = bp->next;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	return (0);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static descriptor_t **
get_assoc_buses(descriptor_t * desc,int * errp)165*0Sstevel@tonic-gate get_assoc_buses(descriptor_t *desc, int *errp)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	bus_t		*bp;
168*0Sstevel@tonic-gate 	char		*name;
169*0Sstevel@tonic-gate 	descriptor_t	**allbuses;
170*0Sstevel@tonic-gate 	descriptor_t	**buses;
171*0Sstevel@tonic-gate 	int		cnt;
172*0Sstevel@tonic-gate 	int		i;
173*0Sstevel@tonic-gate 	int		pos;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	bp = desc->p.bus;
176*0Sstevel@tonic-gate 	name = bp->name;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	allbuses = cache_get_descriptors(DM_BUS, errp);
179*0Sstevel@tonic-gate 	if (*errp != 0) {
180*0Sstevel@tonic-gate 	    return (NULL);
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/* Count how many we have (we overcount, but thats ok). */
184*0Sstevel@tonic-gate 	for (cnt = 0; allbuses[cnt]; cnt++);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	/* make the snapshot */
187*0Sstevel@tonic-gate 	buses = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
188*0Sstevel@tonic-gate 	if (buses == NULL) {
189*0Sstevel@tonic-gate 	    *errp = ENOMEM;
190*0Sstevel@tonic-gate 	    cache_free_descriptors(allbuses);
191*0Sstevel@tonic-gate 	    return (NULL);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	/*
195*0Sstevel@tonic-gate 	 * Get this buses parent bus and get the buses that I am the parent of.
196*0Sstevel@tonic-gate 	 */
197*0Sstevel@tonic-gate 	pos = 0;
198*0Sstevel@tonic-gate 	for (i = 0; allbuses[i]; i++) {
199*0Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, allbuses[i]->p.bus->pname)) {
200*0Sstevel@tonic-gate 		buses[pos++] = allbuses[i];
201*0Sstevel@tonic-gate 	    } else if (bp->pname != NULL &&
202*0Sstevel@tonic-gate 		libdiskmgt_str_eq(bp->pname, allbuses[i]->p.bus->name)) {
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		buses[pos++] = allbuses[i];
205*0Sstevel@tonic-gate 	    } else {
206*0Sstevel@tonic-gate 		/* clean up the unused descriptor */
207*0Sstevel@tonic-gate 		cache_free_descriptor(allbuses[i]);
208*0Sstevel@tonic-gate 	    }
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 	buses[pos] = NULL;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	free(allbuses);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	*errp = 0;
215*0Sstevel@tonic-gate 	return (buses);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate static descriptor_t **
get_assoc_controllers(descriptor_t * desc,int * errp)219*0Sstevel@tonic-gate get_assoc_controllers(descriptor_t *desc, int *errp)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	bus_t		*bp;
222*0Sstevel@tonic-gate 	descriptor_t	**controllers;
223*0Sstevel@tonic-gate 	int		cnt;
224*0Sstevel@tonic-gate 	int		i;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	bp = desc->p.bus;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	/* Count how many we have. */
229*0Sstevel@tonic-gate 	for (cnt = 0; bp->controllers[cnt]; cnt++);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/* make the snapshot */
232*0Sstevel@tonic-gate 	controllers = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
233*0Sstevel@tonic-gate 	if (controllers == NULL) {
234*0Sstevel@tonic-gate 	    *errp = ENOMEM;
235*0Sstevel@tonic-gate 	    return (NULL);
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	for (i = 0; bp->controllers[i]; i++) {
239*0Sstevel@tonic-gate 	    controllers[i] = cache_get_desc(DM_CONTROLLER, bp->controllers[i],
240*0Sstevel@tonic-gate 		NULL, NULL, errp);
241*0Sstevel@tonic-gate 	    if (*errp != 0) {
242*0Sstevel@tonic-gate 		cache_free_descriptors(controllers);
243*0Sstevel@tonic-gate 		return (NULL);
244*0Sstevel@tonic-gate 	    }
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 	controllers[i] = NULL;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	*errp = 0;
249*0Sstevel@tonic-gate 	return (controllers);
250*0Sstevel@tonic-gate }
251