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