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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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.)
49 static int copy_devinfo(const char *, const char *, uint_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
,
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
);
64 * Build a hotplug information snapshot. The path, connection,
65 * and flags indicate what information should be included.
68 getinfo(const char *path
, const char *connection
, uint_t flags
, hp_node_t
*retp
)
70 hp_node_t root
= NULL
;
75 if ((path
== NULL
) || (retp
== NULL
))
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
)
85 /* Copy in device and hotplug nodes from libdevinfo */
86 if ((rv
= copy_devinfo(basepath
, connection
, flags
, &root
)) != 0) {
92 /* Check if there were no connections */
94 dprintf("getinfo: no hotplug connections.\n");
99 /* Special case: exclude root nexus from snapshot */
100 if (strcmp(basepath
, "/") == 0) {
101 child
= root
->hp_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
);
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.
135 copy_devinfo(const char *path
, const char *connection
, uint_t flags
,
138 hp_node_t hp_root
= NULL
;
142 /* Get libdevinfo snapshot */
143 if ((di_root
= di_init(path
, DINFOSUBTREE
| DINFOHP
)) == DI_NODE_NIL
)
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
);
159 rv
= copy_devices(NULL
, di_root
, flags
, &hp_root
);
161 /* Destroy devinfo snapshot */
164 *rootp
= (rv
== 0) ? hp_root
: NULL
;
171 * Copy a full branch of device nodes. Used by copy_devinfo() and
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
;
182 /* Initialize results */
185 /* Enforce search semantics */
186 if (check_search(dev
, flags
) == 0)
189 /* Allocate new node for current device */
190 if ((self
= new_device_node(parent
, dev
)) == NULL
)
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) {
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
)) {
215 if ((rv
= copy_devices(self
, child
, flags
, &branch
)) != 0) {
216 (void) hp_fini(children
.head
);
221 node_list_add(&children
, branch
);
223 self
->hp_child
= children
.head
;
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.
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
;
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
)
272 * Ignore ports associated with connectors, unless
273 * a specific connection is being sought.
275 if ((connection
== NULL
) && (di_hp_depends_on(hp
) != -1))
278 /* If a connection is specified, ignore non-matching ports */
279 if ((connection
!= NULL
) &&
280 (strcmp(di_hp_name(hp
), connection
) != 0))
283 /* Create a new port node */
284 if ((node
= new_hotplug_node(parent
, hp
)) == NULL
) {
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)
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
; ) {
308 if (di_hp_type(hp
) == DDI_HP_CN_TYPE_VIRTUAL_PORT
)
311 /* If a connection is specified, ignore non-matching ports */
312 if ((connection
!= NULL
) &&
313 (strcmp(di_hp_name(hp
), connection
) != 0))
316 /* Create a new connector node */
317 if ((node
= new_hotplug_node(parent
, hp
)) == NULL
) {
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
);
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
)
334 /* Add new port node to port list */
335 if ((port_node
= new_hotplug_node(node
,
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)
349 node
->hp_child
= ports
.head
;
350 (void) memset(&ports
, 0, sizeof (hp_node_list_t
));
353 if (connections
.head
== NULL
)
355 *retp
= connections
.head
;
359 (void) hp_fini(ports
.head
);
360 (void) hp_fini(connections
.head
);
367 * Normalize the base path of a hotplug information snapshot.
368 * The caller must free the string that is allocated.
371 base_path(const char *path
)
376 devices_len
= strlen(S_DEVICES
);
378 if (strncmp(path
, S_DEVICES
, devices_len
) == 0)
379 base_path
= strdup(&path
[devices_len
]);
381 base_path
= strdup(path
);
389 * Callback function used by di_walk_node() to search for branches
390 * of the libdevinfo snapshot that contain hotplug nodes.
394 search_cb(di_node_t node
, void *arg
)
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
);
417 * Check if a device node was marked by an initial search pass.
420 check_search(di_node_t dev
, uint_t flags
)
424 if (flags
& HPINFOSEARCH
) {
425 dev_flags
= (uint_t
)(uintptr_t)di_node_private_get(dev
);
426 if ((dev_flags
& HPINFOSEARCH
) == 0)
436 * Utility function to append one node to a list of hotplug nodes.
439 node_list_add(hp_node_list_t
*listp
, hp_node_t node
)
441 if (listp
->prev
!= NULL
)
442 listp
->prev
->hp_sibling
= node
;
452 * Build a new hotplug node based on a specified devinfo node.
455 new_device_node(hp_node_t parent
, di_node_t dev
)
458 char *node_name
, *bus_addr
;
459 char name
[MAXPATHLEN
];
461 node
= (hp_node_t
)calloc(1, sizeof (struct hp_node
));
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");
476 node
->hp_name
= strdup(name
);
478 node
->hp_name
= strdup(node_name
);
487 * Build a new hotplug node based on a specified devinfo hotplug node.
490 new_hotplug_node(hp_node_t parent
, di_hp_t hp
)
495 node
= (hp_node_t
)calloc(1, sizeof (struct hp_node
));
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
;
508 node
->hp_type
= HP_NODE_CONNECTOR
;