dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libdiskmgt / common / bus.c
blob2b406aa6a666af5a76e3defb36655f625d2990fa
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <fcntl.h>
30 #include <libdevinfo.h>
31 #include <stdlib.h>
32 #include <sys/sunddi.h>
33 #include <sys/types.h>
34 #include <sys/scsi/conf/autoconf.h>
36 #include "libdiskmgt.h"
37 #include "disks_private.h"
39 static descriptor_t **get_assoc_buses(descriptor_t *desc, int *errp);
40 static descriptor_t **get_assoc_controllers(descriptor_t *desc, int *errp);
42 descriptor_t **
43 bus_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type, int *errp)
45 switch (type) {
46 case DM_BUS:
47 return (get_assoc_buses(desc, errp));
48 case DM_CONTROLLER:
49 return (get_assoc_controllers(desc, errp));
52 *errp = EINVAL;
53 return (NULL);
56 nvlist_t *
57 bus_get_attributes(descriptor_t *dp, int *errp)
59 bus_t *bp;
60 nvlist_t *attrs;
62 if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
63 *errp = ENOMEM;
64 return (NULL);
67 bp = dp->p.bus;
69 if (nvlist_add_string(attrs, DM_BTYPE, bp->btype) != 0) {
70 nvlist_free(attrs);
71 *errp = ENOMEM;
72 return (NULL);
75 if (bp->freq != 0) {
76 if (nvlist_add_uint32(attrs, DM_CLOCK, bp->freq) != 0) {
77 nvlist_free(attrs);
78 *errp = ENOMEM;
79 return (NULL);
83 if (bp->pname != NULL) {
84 if (nvlist_add_string(attrs, DM_PNAME, bp->pname) != 0) {
85 nvlist_free(attrs);
86 *errp = ENOMEM;
87 return (NULL);
91 *errp = 0;
92 return (attrs);
95 descriptor_t *
96 bus_get_descriptor_by_name(char *name, int *errp)
98 descriptor_t **buses;
99 int i;
100 descriptor_t *bus = NULL;
102 buses = cache_get_descriptors(DM_BUS, errp);
103 if (*errp != 0) {
104 return (NULL);
107 for (i = 0; buses[i]; i++) {
108 if (libdiskmgt_str_eq(name, buses[i]->p.bus->name)) {
109 bus = buses[i];
110 } else {
111 /* clean up the unused descriptors */
112 cache_free_descriptor(buses[i]);
115 free(buses);
117 if (bus == NULL) {
118 *errp = ENODEV;
121 return (bus);
124 /* ARGSUSED */
125 descriptor_t **
126 bus_get_descriptors(int filter[], int *errp)
128 return (cache_get_descriptors(DM_BUS, errp));
131 char *
132 bus_get_name(descriptor_t *desc)
134 return (desc->p.bus->name);
137 /* ARGSUSED */
138 nvlist_t *
139 bus_get_stats(descriptor_t *dp, int stat_type, int *errp)
141 /* There are no stat types defined for controllers */
142 *errp = EINVAL;
143 return (NULL);
147 bus_make_descriptors()
149 int error;
150 bus_t *bp;
152 bp = cache_get_buslist();
153 while (bp != NULL) {
154 cache_load_desc(DM_BUS, bp, NULL, NULL, &error);
155 if (error != 0) {
156 return (error);
158 bp = bp->next;
161 return (0);
164 static descriptor_t **
165 get_assoc_buses(descriptor_t *desc, int *errp)
167 bus_t *bp;
168 char *name;
169 descriptor_t **allbuses;
170 descriptor_t **buses;
171 int cnt;
172 int i;
173 int pos;
175 bp = desc->p.bus;
176 name = bp->name;
178 allbuses = cache_get_descriptors(DM_BUS, errp);
179 if (*errp != 0) {
180 return (NULL);
183 /* Count how many we have (we overcount, but thats ok). */
184 for (cnt = 0; allbuses[cnt]; cnt++);
186 /* make the snapshot */
187 buses = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
188 if (buses == NULL) {
189 *errp = ENOMEM;
190 cache_free_descriptors(allbuses);
191 return (NULL);
195 * Get this buses parent bus and get the buses that I am the parent of.
197 pos = 0;
198 for (i = 0; allbuses[i]; i++) {
199 if (libdiskmgt_str_eq(name, allbuses[i]->p.bus->pname)) {
200 buses[pos++] = allbuses[i];
201 } else if (bp->pname != NULL &&
202 libdiskmgt_str_eq(bp->pname, allbuses[i]->p.bus->name)) {
204 buses[pos++] = allbuses[i];
205 } else {
206 /* clean up the unused descriptor */
207 cache_free_descriptor(allbuses[i]);
210 buses[pos] = NULL;
212 free(allbuses);
214 *errp = 0;
215 return (buses);
218 static descriptor_t **
219 get_assoc_controllers(descriptor_t *desc, int *errp)
221 bus_t *bp;
222 descriptor_t **controllers;
223 int cnt;
224 int i;
226 bp = desc->p.bus;
228 /* Count how many we have. */
229 for (cnt = 0; bp->controllers[cnt]; cnt++);
231 /* make the snapshot */
232 controllers = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
233 if (controllers == NULL) {
234 *errp = ENOMEM;
235 return (NULL);
238 for (i = 0; bp->controllers[i]; i++) {
239 controllers[i] = cache_get_desc(DM_CONTROLLER, bp->controllers[i],
240 NULL, NULL, errp);
241 if (*errp != 0) {
242 cache_free_descriptors(controllers);
243 return (NULL);
246 controllers[i] = NULL;
248 *errp = 0;
249 return (controllers);