dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / hotplugd / hotplugd_info.c
blobe33b00e18cb4423045a8abe090cec05753414fd3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <libdevinfo.h>
31 #include <libhotplug.h>
32 #include <libhotplug_impl.h>
33 #include <sys/sunddi.h>
34 #include <sys/ddi_hp.h>
35 #include "hotplugd_impl.h"
38 * Define a list of hotplug nodes.
39 * (Only used within this module.)
41 typedef struct {
42 hp_node_t head;
43 hp_node_t prev;
44 } hp_node_list_t;
47 * Local functions.
49 static int copy_devinfo(const char *, const char *, uint_t,
50 hp_node_t *);
51 static int copy_devices(hp_node_t, di_node_t, uint_t, hp_node_t *);
52 static int copy_hotplug(hp_node_t, di_node_t, const char *, uint_t,
53 hp_node_t *);
54 static char *base_path(const char *);
55 static int search_cb(di_node_t, void *);
56 static int check_search(di_node_t, uint_t);
57 static hp_node_t new_device_node(hp_node_t, di_node_t);
58 static hp_node_t new_hotplug_node(hp_node_t, di_hp_t);
59 static void node_list_add(hp_node_list_t *, hp_node_t);
62 * getinfo()
64 * Build a hotplug information snapshot. The path, connection,
65 * and flags indicate what information should be included.
67 int
68 getinfo(const char *path, const char *connection, uint_t flags, hp_node_t *retp)
70 hp_node_t root = NULL;
71 hp_node_t child;
72 char *basepath;
73 int rv;
75 if ((path == NULL) || (retp == NULL))
76 return (EINVAL);
78 dprintf("getinfo: path=%s, connection=%s, flags=0x%x\n", path,
79 (connection == NULL) ? "NULL" : connection, flags);
81 /* Allocate the base path */
82 if ((basepath = base_path(path)) == NULL)
83 return (ENOMEM);
85 /* Copy in device and hotplug nodes from libdevinfo */
86 if ((rv = copy_devinfo(basepath, connection, flags, &root)) != 0) {
87 hp_fini(root);
88 free(basepath);
89 return (rv);
92 /* Check if there were no connections */
93 if (root == NULL) {
94 dprintf("getinfo: no hotplug connections.\n");
95 free(basepath);
96 return (ENOENT);
99 /* Special case: exclude root nexus from snapshot */
100 if (strcmp(basepath, "/") == 0) {
101 child = root->hp_child;
102 free(root->hp_name);
103 free(root);
104 root = child;
105 for (child = root; child; child = child->hp_sibling)
106 child->hp_parent = NULL;
109 /* Store a pointer to the base path in each root node */
110 for (child = root; child != NULL; child = child->hp_sibling)
111 child->hp_basepath = basepath;
113 /* Copy in usage information from RCM */
114 if (flags & HPINFOUSAGE) {
115 if ((rv = copy_usage(root)) != 0) {
116 (void) hp_fini(root);
117 return (rv);
121 *retp = root;
122 return (0);
126 * copy_devinfo()
128 * Copy information about device and hotplug nodes from libdevinfo.
130 * When path is set to "/", the results need to be limited only to
131 * branches that contain hotplug information. An initial search
132 * is performed to mark which branches contain hotplug nodes.
134 static int
135 copy_devinfo(const char *path, const char *connection, uint_t flags,
136 hp_node_t *rootp)
138 hp_node_t hp_root = NULL;
139 di_node_t di_root;
140 int rv;
142 /* Get libdevinfo snapshot */
143 if ((di_root = di_init(path, DINFOSUBTREE | DINFOHP)) == DI_NODE_NIL)
144 return (errno);
146 /* Do initial search pass, if required */
147 if (strcmp(path, "/") == 0) {
148 flags |= HPINFOSEARCH;
149 (void) di_walk_node(di_root, DI_WALK_CLDFIRST, NULL, search_cb);
153 * If a connection is specified, just copy immediate hotplug info.
154 * Else, copy the device tree normally.
156 if (connection != NULL)
157 rv = copy_hotplug(NULL, di_root, connection, flags, &hp_root);
158 else
159 rv = copy_devices(NULL, di_root, flags, &hp_root);
161 /* Destroy devinfo snapshot */
162 di_fini(di_root);
164 *rootp = (rv == 0) ? hp_root : NULL;
165 return (rv);
169 * copy_devices()
171 * Copy a full branch of device nodes. Used by copy_devinfo() and
172 * copy_hotplug().
174 static int
175 copy_devices(hp_node_t parent, di_node_t dev, uint_t flags, hp_node_t *rootp)
177 hp_node_list_t children;
178 hp_node_t self, branch;
179 di_node_t child;
180 int rv = 0;
182 /* Initialize results */
183 *rootp = NULL;
185 /* Enforce search semantics */
186 if (check_search(dev, flags) == 0)
187 return (0);
189 /* Allocate new node for current device */
190 if ((self = new_device_node(parent, dev)) == NULL)
191 return (ENOMEM);
194 * If the device has hotplug nodes, then use copy_hotplug()
195 * instead to build the branch associated with current device.
197 if (di_hp_next(dev, DI_HP_NIL) != DI_HP_NIL) {
198 if ((rv = copy_hotplug(self, dev, NULL, flags,
199 &self->hp_child)) != 0) {
200 free(self);
201 return (rv);
203 *rootp = self;
204 return (0);
208 * The device does not have hotplug nodes. Use normal
209 * approach of iterating through its child device nodes.
211 (void) memset(&children, 0, sizeof (hp_node_list_t));
212 for (child = di_child_node(dev); child != DI_NODE_NIL;
213 child = di_sibling_node(child)) {
214 branch = NULL;
215 if ((rv = copy_devices(self, child, flags, &branch)) != 0) {
216 (void) hp_fini(children.head);
217 free(self);
218 return (rv);
220 if (branch != NULL)
221 node_list_add(&children, branch);
223 self->hp_child = children.head;
225 /* Done */
226 *rootp = self;
227 return (0);
231 * copy_hotplug()
233 * Copy a full branch of hotplug nodes. Used by copy_devinfo()
234 * and copy_devices().
236 * If a connection is specified, the results are limited only
237 * to the branch associated with that specific connection.
239 static int
240 copy_hotplug(hp_node_t parent, di_node_t dev, const char *connection,
241 uint_t flags, hp_node_t *retp)
243 hp_node_list_t connections, ports;
244 hp_node_t node, port_node;
245 di_node_t child_dev;
246 di_hp_t hp, port_hp;
247 uint_t child_flags;
248 int rv, physnum;
250 /* Stop implementing the HPINFOSEARCH flag */
251 child_flags = flags & ~(HPINFOSEARCH);
253 /* Clear lists of discovered ports and connections */
254 (void) memset(&ports, 0, sizeof (hp_node_list_t));
255 (void) memset(&connections, 0, sizeof (hp_node_list_t));
258 * Scan virtual ports.
260 * If a connection is specified and it matches a virtual port,
261 * this will build the branch associated with that connection.
262 * Else, this will only build branches for virtual ports that
263 * are not associated with a physical connector.
265 for (hp = DI_HP_NIL; (hp = di_hp_next(dev, hp)) != DI_HP_NIL; ) {
267 /* Ignore connectors */
268 if (di_hp_type(hp) != DDI_HP_CN_TYPE_VIRTUAL_PORT)
269 continue;
272 * Ignore ports associated with connectors, unless
273 * a specific connection is being sought.
275 if ((connection == NULL) && (di_hp_depends_on(hp) != -1))
276 continue;
278 /* If a connection is specified, ignore non-matching ports */
279 if ((connection != NULL) &&
280 (strcmp(di_hp_name(hp), connection) != 0))
281 continue;
283 /* Create a new port node */
284 if ((node = new_hotplug_node(parent, hp)) == NULL) {
285 rv = ENOMEM;
286 goto fail;
289 /* Add port node to connection list */
290 node_list_add(&connections, node);
292 /* Add branch of child devices to port node */
293 if ((child_dev = di_hp_child(hp)) != DI_NODE_NIL)
294 if ((rv = copy_devices(node, child_dev, child_flags,
295 &node->hp_child)) != 0)
296 goto fail;
300 * Scan physical connectors.
302 * If a connection is specified, the results will be limited
303 * only to the branch associated with that connection.
305 for (hp = DI_HP_NIL; (hp = di_hp_next(dev, hp)) != DI_HP_NIL; ) {
307 /* Ignore ports */
308 if (di_hp_type(hp) == DDI_HP_CN_TYPE_VIRTUAL_PORT)
309 continue;
311 /* If a connection is specified, ignore non-matching ports */
312 if ((connection != NULL) &&
313 (strcmp(di_hp_name(hp), connection) != 0))
314 continue;
316 /* Create a new connector node */
317 if ((node = new_hotplug_node(parent, hp)) == NULL) {
318 rv = ENOMEM;
319 goto fail;
322 /* Add connector node to connection list */
323 node_list_add(&connections, node);
325 /* Add branches of associated port nodes */
326 physnum = di_hp_connection(hp);
327 port_hp = DI_HP_NIL;
328 while ((port_hp = di_hp_next(dev, port_hp)) != DI_HP_NIL) {
330 /* Ignore irrelevant connections */
331 if (di_hp_depends_on(port_hp) != physnum)
332 continue;
334 /* Add new port node to port list */
335 if ((port_node = new_hotplug_node(node,
336 port_hp)) == NULL) {
337 rv = ENOMEM;
338 goto fail;
340 node_list_add(&ports, port_node);
342 /* Add branch of child devices */
343 if ((child_dev = di_hp_child(port_hp)) != DI_NODE_NIL) {
344 if ((rv = copy_devices(port_node, child_dev,
345 child_flags, &port_node->hp_child)) != 0)
346 goto fail;
349 node->hp_child = ports.head;
350 (void) memset(&ports, 0, sizeof (hp_node_list_t));
353 if (connections.head == NULL)
354 return (ENXIO);
355 *retp = connections.head;
356 return (0);
358 fail:
359 (void) hp_fini(ports.head);
360 (void) hp_fini(connections.head);
361 return (rv);
365 * base_path()
367 * Normalize the base path of a hotplug information snapshot.
368 * The caller must free the string that is allocated.
370 static char *
371 base_path(const char *path)
373 char *base_path;
374 size_t devices_len;
376 devices_len = strlen(S_DEVICES);
378 if (strncmp(path, S_DEVICES, devices_len) == 0)
379 base_path = strdup(&path[devices_len]);
380 else
381 base_path = strdup(path);
383 return (base_path);
387 * search_cb()
389 * Callback function used by di_walk_node() to search for branches
390 * of the libdevinfo snapshot that contain hotplug nodes.
392 /*ARGSUSED*/
393 static int
394 search_cb(di_node_t node, void *arg)
396 di_node_t parent;
397 uint_t flags;
399 (void) di_node_private_set(node, (void *)(uintptr_t)0);
401 if (di_hp_next(node, DI_HP_NIL) == DI_HP_NIL)
402 return (DI_WALK_CONTINUE);
404 for (parent = node; parent != DI_NODE_NIL;
405 parent = di_parent_node(parent)) {
406 flags = (uint_t)(uintptr_t)di_node_private_get(parent);
407 flags |= HPINFOSEARCH;
408 (void) di_node_private_set(parent, (void *)(uintptr_t)flags);
411 return (DI_WALK_CONTINUE);
415 * check_search()
417 * Check if a device node was marked by an initial search pass.
419 static int
420 check_search(di_node_t dev, uint_t flags)
422 uint_t dev_flags;
424 if (flags & HPINFOSEARCH) {
425 dev_flags = (uint_t)(uintptr_t)di_node_private_get(dev);
426 if ((dev_flags & HPINFOSEARCH) == 0)
427 return (0);
430 return (1);
434 * node_list_add()
436 * Utility function to append one node to a list of hotplug nodes.
438 static void
439 node_list_add(hp_node_list_t *listp, hp_node_t node)
441 if (listp->prev != NULL)
442 listp->prev->hp_sibling = node;
443 else
444 listp->head = node;
446 listp->prev = node;
450 * new_device_node()
452 * Build a new hotplug node based on a specified devinfo node.
454 static hp_node_t
455 new_device_node(hp_node_t parent, di_node_t dev)
457 hp_node_t node;
458 char *node_name, *bus_addr;
459 char name[MAXPATHLEN];
461 node = (hp_node_t)calloc(1, sizeof (struct hp_node));
463 if (node != NULL) {
464 node->hp_parent = parent;
465 node->hp_type = HP_NODE_DEVICE;
467 node_name = di_node_name(dev);
468 bus_addr = di_bus_addr(dev);
469 if (bus_addr && (strlen(bus_addr) > 0)) {
470 if (snprintf(name, sizeof (name), "%s@%s", node_name,
471 bus_addr) >= sizeof (name)) {
472 log_err("Path too long for device node.\n");
473 free(node);
474 return (NULL);
476 node->hp_name = strdup(name);
477 } else
478 node->hp_name = strdup(node_name);
481 return (node);
485 * new_hotplug_node()
487 * Build a new hotplug node based on a specified devinfo hotplug node.
489 static hp_node_t
490 new_hotplug_node(hp_node_t parent, di_hp_t hp)
492 hp_node_t node;
493 char *s;
495 node = (hp_node_t)calloc(1, sizeof (struct hp_node));
497 if (node != NULL) {
498 node->hp_parent = parent;
499 node->hp_state = di_hp_state(hp);
500 node->hp_last_change = di_hp_last_change(hp);
501 if ((s = di_hp_name(hp)) != NULL)
502 node->hp_name = strdup(s);
503 if ((s = di_hp_description(hp)) != NULL)
504 node->hp_description = strdup(s);
505 if (di_hp_type(hp) == DDI_HP_CN_TYPE_VIRTUAL_PORT)
506 node->hp_type = HP_NODE_PORT;
507 else
508 node->hp_type = HP_NODE_CONNECTOR;
511 return (node);