1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_LIBDEVINFO_H
27 #define	_LIBDEVINFO_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <errno.h>
34 #include <libnvpair.h>
35 #include <sys/param.h>
36 #include <sys/sunddi.h>
37 #include <sys/sunmdi.h>
38 #include <sys/openpromio.h>
39 #include <sys/ddi_impldefs.h>
40 #include <sys/devinfo_impl.h>
41 #include <limits.h>
42 
43 /*
44  * flags for di_walk_node
45  */
46 #define	DI_WALK_CLDFIRST	0
47 #define	DI_WALK_SIBFIRST	1
48 #define	DI_WALK_LINKGEN		2
49 
50 #define	DI_WALK_MASK		0xf
51 
52 /*
53  * flags for di_walk_link
54  */
55 #define	DI_LINK_SRC		1
56 #define	DI_LINK_TGT		2
57 
58 /*
59  * return code for node_callback
60  */
61 #define	DI_WALK_CONTINUE	0
62 #define	DI_WALK_PRUNESIB	-1
63 #define	DI_WALK_PRUNECHILD	-2
64 #define	DI_WALK_TERMINATE	-3
65 
66 /*
67  * flags for di_walk_minor
68  */
69 #define	DI_CHECK_ALIAS		0x10
70 #define	DI_CHECK_INTERNAL_PATH	0x20
71 
72 #define	DI_CHECK_MASK		0xf0
73 
74 /*
75  * flags for di_walk_hp
76  */
77 #define	DI_HP_CONNECTOR		0x1
78 #define	DI_HP_PORT		0x2
79 
80 /* nodeid types */
81 #define	DI_PSEUDO_NODEID	-1
82 #define	DI_SID_NODEID		-2
83 #define	DI_PROM_NODEID		-3
84 
85 /* node & device states */
86 #define	DI_DRIVER_DETACHED	0x8000
87 #define	DI_DEVICE_OFFLINE	0x1
88 #define	DI_DEVICE_DOWN		0x2
89 #define	DI_DEVICE_DEGRADED	0x4
90 #define	DI_DEVICE_REMOVED	0x8
91 #define	DI_BUS_QUIESCED		0x100
92 #define	DI_BUS_DOWN		0x200
93 
94 /* property types */
95 #define	DI_PROP_TYPE_BOOLEAN	0
96 #define	DI_PROP_TYPE_INT	1
97 #define	DI_PROP_TYPE_STRING	2
98 #define	DI_PROP_TYPE_BYTE	3
99 #define	DI_PROP_TYPE_UNKNOWN	4
100 #define	DI_PROP_TYPE_UNDEF_IT	5
101 #define	DI_PROP_TYPE_INT64	6
102 
103 /* private macro for checking if a prop type is valid */
104 #define	DI_PROP_TYPE_VALID(type) \
105 	((((type) >= DI_PROP_TYPE_INT) && ((type) <= DI_PROP_TYPE_BYTE)) || \
106 	    ((type) == DI_PROP_TYPE_INT64))
107 
108 /* opaque handles */
109 typedef struct di_node		*di_node_t;		/* node */
110 typedef struct di_minor		*di_minor_t;		/* minor_node */
111 typedef struct di_path		*di_path_t;		/* path_node */
112 typedef struct di_link		*di_link_t;		/* link */
113 typedef struct di_lnode		*di_lnode_t;		/* endpoint */
114 typedef struct di_devlink	*di_devlink_t;		/* devlink */
115 typedef struct di_hp		*di_hp_t;		/* hotplug */
116 
117 typedef struct di_prop		*di_prop_t;		/* node property */
118 typedef struct di_path_prop	*di_path_prop_t;	/* path property */
119 typedef struct di_prom_prop	*di_prom_prop_t;	/* prom property */
120 
121 typedef struct di_prom_handle	*di_prom_handle_t;	/* prom snapshot */
122 typedef struct di_devlink_handle *di_devlink_handle_t;	/* devlink snapshot */
123 
124 
125 /*
126  * Null handles to make handles really opaque
127  */
128 #define	DI_NODE_NIL		NULL
129 #define	DI_MINOR_NIL		NULL
130 #define	DI_PATH_NIL		NULL
131 #define	DI_LINK_NIL		NULL
132 #define	DI_LNODE_NIL		NULL
133 #define	DI_PROP_NIL		NULL
134 #define	DI_PROM_PROP_NIL	NULL
135 #define	DI_PROM_HANDLE_NIL	NULL
136 #define	DI_HP_NIL		NULL
137 
138 /*
139  * IEEE 1275 properties and other standardized property names
140  */
141 #define	DI_PROP_FIRST_CHAS	"first-in-chassis"
142 #define	DI_PROP_SLOT_NAMES	"slot-names"
143 #define	DI_PROP_PHYS_SLOT	"physical-slot#"
144 #define	DI_PROP_DEV_TYPE	"device_type"
145 #define	DI_PROP_BUS_RANGE	"bus-range"
146 #define	DI_PROP_SERID		"serialid#"
147 #define	DI_PROP_REG		"reg"
148 #define	DI_PROP_AP_NAMES	"ap-names"
149 
150 /* Interface Prototypes */
151 
152 /*
153  * Snapshot initialization and cleanup
154  */
155 extern di_node_t	di_init(const char *phys_path, uint_t flag);
156 extern void		di_fini(di_node_t root);
157 
158 /*
159  * node: traversal, data access, and parameters
160  */
161 extern int		di_walk_node(di_node_t root, uint_t flag, void *arg,
162 			    int (*node_callback)(di_node_t node, void *arg));
163 
164 extern di_node_t	di_drv_first_node(const char *drv_name, di_node_t root);
165 extern di_node_t	di_drv_next_node(di_node_t node);
166 
167 extern di_node_t	di_parent_node(di_node_t node);
168 extern di_node_t	di_sibling_node(di_node_t node);
169 extern di_node_t	di_child_node(di_node_t node);
170 
171 extern char		*di_node_name(di_node_t node);
172 extern char		*di_bus_addr(di_node_t node);
173 extern char		*di_binding_name(di_node_t node);
174 extern int		di_compatible_names(di_node_t, char **names);
175 extern int		di_instance(di_node_t node);
176 extern int		di_nodeid(di_node_t node);
177 extern int		di_driver_major(di_node_t node);
178 extern uint_t		di_state(di_node_t node);
179 extern ddi_node_state_t	di_node_state(di_node_t node);
180 extern ddi_devid_t	di_devid(di_node_t node);
181 extern char		*di_driver_name(di_node_t node);
182 extern uint_t		di_driver_ops(di_node_t node);
183 
184 extern void		di_node_private_set(di_node_t node, void *data);
185 extern void		*di_node_private_get(di_node_t node);
186 
187 extern char		*di_devfs_path(di_node_t node);
188 extern char		*di_devfs_minor_path(di_minor_t minor);
189 extern void		di_devfs_path_free(char *path_buf);
190 
191 /*
192  * path_node: traversal, data access, and parameters
193  */
194 extern di_path_t	di_path_phci_next_path(di_node_t node, di_path_t);
195 extern di_path_t	di_path_client_next_path(di_node_t node, di_path_t);
196 
197 extern di_node_t	di_path_phci_node(di_path_t path);
198 extern di_node_t	di_path_client_node(di_path_t path);
199 
200 extern char		*di_path_node_name(di_path_t path);
201 extern char		*di_path_bus_addr(di_path_t path);
202 extern int		di_path_instance(di_path_t path);
203 extern di_path_state_t	di_path_state(di_path_t path);
204 extern uint_t		di_path_flags(di_path_t path);
205 
206 extern char		*di_path_devfs_path(di_path_t path);
207 extern char		*di_path_client_devfs_path(di_path_t path);
208 
209 extern void		di_path_private_set(di_path_t path, void *data);
210 extern void		*di_path_private_get(di_path_t path);
211 
212 /*
213  * minor_node: traversal, data access, and parameters
214  */
215 extern int		di_walk_minor(di_node_t root, const char *minortype,
216 			    uint_t flag, void *arg,
217 			    int (*minor_callback)(di_node_t node,
218 			    di_minor_t minor, void *arg));
219 extern di_minor_t	di_minor_next(di_node_t node, di_minor_t minor);
220 
221 extern di_node_t	di_minor_devinfo(di_minor_t minor);
222 extern ddi_minor_type	di_minor_type(di_minor_t minor);
223 extern char		*di_minor_name(di_minor_t minor);
224 extern dev_t		di_minor_devt(di_minor_t minor);
225 extern int		di_minor_spectype(di_minor_t minor);
226 extern char		*di_minor_nodetype(di_minor_t node);
227 
228 extern void		di_minor_private_set(di_minor_t minor, void *data);
229 extern void		*di_minor_private_get(di_minor_t minor);
230 
231 /*
232  * node: property access
233  */
234 extern di_prop_t	di_prop_next(di_node_t node, di_prop_t prop);
235 
236 extern char		*di_prop_name(di_prop_t prop);
237 extern int		di_prop_type(di_prop_t prop);
238 extern dev_t		di_prop_devt(di_prop_t prop);
239 
240 extern int		di_prop_ints(di_prop_t prop, int **prop_data);
241 extern int		di_prop_int64(di_prop_t prop, int64_t **prop_data);
242 extern int		di_prop_strings(di_prop_t prop, char **prop_data);
243 extern int		di_prop_bytes(di_prop_t prop, uchar_t **prop_data);
244 
245 extern int		di_prop_lookup_bytes(dev_t dev, di_node_t node,
246 			    const char *prop_name, uchar_t **prop_data);
247 extern int		di_prop_lookup_ints(dev_t dev, di_node_t node,
248 			    const char *prop_name, int **prop_data);
249 extern int		di_prop_lookup_int64(dev_t dev, di_node_t node,
250 			    const char *prop_name, int64_t **prop_data);
251 extern int		di_prop_lookup_strings(dev_t dev, di_node_t node,
252 			    const char *prop_name, char **prop_data);
253 
254 /*
255  * prom_node: property access
256  */
257 extern di_prom_handle_t	di_prom_init(void);
258 extern void		di_prom_fini(di_prom_handle_t ph);
259 
260 extern di_prom_prop_t	di_prom_prop_next(di_prom_handle_t ph, di_node_t node,
261 			    di_prom_prop_t prom_prop);
262 
263 extern char		*di_prom_prop_name(di_prom_prop_t prom_prop);
264 extern int		di_prom_prop_data(di_prom_prop_t prop,
265 			    uchar_t **prom_prop_data);
266 
267 extern int		di_prom_prop_lookup_ints(di_prom_handle_t prom,
268 			    di_node_t node, const char *prom_prop_name,
269 			    int **prom_prop_data);
270 extern int		di_prom_prop_lookup_strings(di_prom_handle_t prom,
271 			    di_node_t node, const char *prom_prop_name,
272 			    char **prom_prop_data);
273 extern int		di_prom_prop_lookup_bytes(di_prom_handle_t prom,
274 			    di_node_t node, const char *prom_prop_name,
275 			    uchar_t **prom_prop_data);
276 
277 /*
278  * path_node: property access
279  */
280 extern di_path_prop_t	di_path_prop_next(di_path_t path, di_path_prop_t prop);
281 
282 extern char		*di_path_prop_name(di_path_prop_t prop);
283 extern int		di_path_prop_type(di_path_prop_t prop);
284 extern int		di_path_prop_len(di_path_prop_t prop);
285 
286 extern int		di_path_prop_bytes(di_path_prop_t prop,
287 			    uchar_t **prop_data);
288 extern int		di_path_prop_ints(di_path_prop_t prop,
289 			    int **prop_data);
290 extern int		di_path_prop_int64s(di_path_prop_t prop,
291 			    int64_t **prop_data);
292 extern int		di_path_prop_strings(di_path_prop_t prop,
293 			    char **prop_data);
294 
295 extern int		di_path_prop_lookup_bytes(di_path_t path,
296 			    const char *prop_name, uchar_t **prop_data);
297 extern int		di_path_prop_lookup_ints(di_path_t path,
298 			    const char *prop_name, int **prop_data);
299 extern int		di_path_prop_lookup_int64s(di_path_t path,
300 			    const char *prop_name, int64_t **prop_data);
301 extern int		di_path_prop_lookup_strings(di_path_t path,
302 			    const char *prop_name, char **prop_data);
303 
304 /*
305  * layering link/lnode: traversal, data access, and parameters
306  */
307 extern int		di_walk_link(di_node_t root, uint_t flag,
308 			    uint_t endpoint, void *arg,
309 			    int (*link_callback)(di_link_t link, void *arg));
310 extern int		di_walk_lnode(di_node_t root, uint_t flag, void *arg,
311 			    int (*lnode_callback)(di_lnode_t lnode, void *arg));
312 
313 extern di_link_t	di_link_next_by_node(di_node_t node,
314 			    di_link_t link, uint_t endpoint);
315 extern di_link_t	di_link_next_by_lnode(di_lnode_t lnode,
316 			    di_link_t link, uint_t endpoint);
317 extern di_lnode_t	di_lnode_next(di_node_t node, di_lnode_t lnode);
318 extern char		*di_lnode_name(di_lnode_t lnode);
319 
320 extern int		di_link_spectype(di_link_t link);
321 extern di_lnode_t	di_link_to_lnode(di_link_t link, uint_t endpoint);
322 
323 extern di_node_t	di_lnode_devinfo(di_lnode_t lnode);
324 extern int		di_lnode_devt(di_lnode_t lnode, dev_t *devt);
325 
326 extern void		di_link_private_set(di_link_t link, void *data);
327 extern void		*di_link_private_get(di_link_t link);
328 extern void		di_lnode_private_set(di_lnode_t lnode, void *data);
329 extern void		*di_lnode_private_get(di_lnode_t lnode);
330 
331 /*
332  * hp_node: traversal, data access, and parameters
333  */
334 extern int		di_walk_hp(di_node_t node, const char *type,
335 			    uint_t flag, void *arg,
336 			    int (*hp_callback)(di_node_t node, di_hp_t hp,
337 			    void *arg));
338 extern di_hp_t		di_hp_next(di_node_t node, di_hp_t hp);
339 
340 extern char		*di_hp_name(di_hp_t hp);
341 extern int		di_hp_connection(di_hp_t hp);
342 extern int		di_hp_depends_on(di_hp_t hp);
343 extern int		di_hp_state(di_hp_t hp);
344 extern int		di_hp_type(di_hp_t hp);
345 extern char		*di_hp_description(di_hp_t hp);
346 extern time_t		di_hp_last_change(di_hp_t hp);
347 extern di_node_t	di_hp_child(di_hp_t hp);
348 
349 /*
350  * Private interfaces
351  *
352  * The interfaces and structures below are private to this implementation
353  * of Solaris and are subject to change at any time without notice.
354  *
355  * Applications and drivers using these interfaces may fail
356  * to run on future releases.
357  */
358 extern di_prop_t di_prop_find(dev_t match_dev, di_node_t node,
359     const char *name);
360 extern int di_devfs_path_match(const char *dp1, const char *dp2);
361 
362 extern di_node_t	di_vhci_first_node(di_node_t root);
363 extern di_node_t	di_vhci_next_node(di_node_t node);
364 extern di_node_t	di_phci_first_node(di_node_t vhci_node);
365 extern di_node_t	di_phci_next_node(di_node_t node);
366 
367 /*
368  * Interfaces for handling IEEE 1275 and other standardized properties
369  */
370 
371 /* structure for a single slot */
372 typedef struct di_slot_name {
373 	int num;	/* corresponding pci device number */
374 	char *name;
375 } di_slot_name_t;
376 
377 extern void di_slot_names_free(int count, di_slot_name_t *slot_names);
378 extern int di_slot_names_decode(uchar_t *rawdata, int rawlen,
379     di_slot_name_t **prop_data);
380 extern int di_prop_slot_names(di_prop_t prop, di_slot_name_t **prop_data);
381 extern int di_prom_prop_slot_names(di_prom_prop_t prom_prop,
382     di_slot_name_t **prop_data);
383 extern int di_prop_lookup_slot_names(dev_t dev, di_node_t node,
384     di_slot_name_t **prop_data);
385 extern int di_prom_prop_lookup_slot_names(di_prom_handle_t ph, di_node_t node,
386     di_slot_name_t **prop_data);
387 
388 /*
389  * XXX Remove the private di_path_(addr,next,next_phci,next_client) interfaces
390  * below after NWS consolidation switches to using di_path_bus_addr,
391  * di_path_phci_next_path, and di_path_client_next_path per CR6638521.
392  */
393 extern char *di_path_addr(di_path_t path, char *buf);
394 extern di_path_t di_path_next(di_node_t node, di_path_t path);
395 extern di_path_t di_path_next_phci(di_node_t node, di_path_t path);
396 extern di_path_t di_path_next_client(di_node_t node, di_path_t path);
397 
398 /*
399  * Interfaces for private data
400  */
401 extern di_node_t di_init_driver(const char *drv_name, uint_t flag);
402 extern di_node_t di_init_impl(const char *phys_path, uint_t flag,
403     struct di_priv_data *priv_data);
404 
405 /*
406  * Prtconf needs to know property lists, raw prop_data, and private data
407  */
408 extern di_prop_t di_prop_drv_next(di_node_t node, di_prop_t prop);
409 extern di_prop_t di_prop_sys_next(di_node_t node, di_prop_t prop);
410 extern di_prop_t di_prop_global_next(di_node_t node, di_prop_t prop);
411 extern di_prop_t di_prop_hw_next(di_node_t node, di_prop_t prop);
412 
413 extern int di_prop_rawdata(di_prop_t prop, uchar_t **prop_data);
414 extern void *di_parent_private_data(di_node_t node);
415 extern void *di_driver_private_data(di_node_t node);
416 
417 /*
418  * The value of the dip's devi_flags field
419  */
420 uint_t di_flags(di_node_t node);
421 
422 /*
423  * Types of links for devlink lookup
424  */
425 #define	DI_PRIMARY_LINK		0x01
426 #define	DI_SECONDARY_LINK	0x02
427 #define	DI_LINK_TYPES		0x03
428 
429 /*
430  * Flag for di_devlink_init()
431  */
432 #define	DI_MAKE_LINK	0x01
433 
434 /*
435  * Flag for di_devlink_close()
436  */
437 #define	DI_LINK_ERROR	0x01
438 
439 /*
440  * For devfsadm synchronous link creation interfaces
441  */
442 #define	DEVFSADM_SYNCH_DOOR	".devfsadm_synch_door"
443 
444 /*
445  * devlink create argument
446  */
447 struct dca_off {
448 	uint32_t	dca_root;
449 	uint32_t	dca_minor;
450 	uint32_t	dca_driver;
451 	int		dca_error;
452 	int		dca_flags;
453 	char		dca_name[PATH_MAX+MAXNAMELEN];
454 };
455 
456 extern di_devlink_handle_t di_devlink_init(const char *name, uint_t flags);
457 extern int di_devlink_walk(di_devlink_handle_t hdl, const char *re,
458     const char *minor_path, uint_t flags, void *arg,
459     int (*devlink_callback)(di_devlink_t, void *));
460 extern const char *di_devlink_path(di_devlink_t devlink);
461 extern const char *di_devlink_content(di_devlink_t devlink);
462 extern int di_devlink_type(di_devlink_t devlink);
463 extern di_devlink_t di_devlink_dup(di_devlink_t devlink);
464 extern int di_devlink_free(di_devlink_t devlink);
465 extern int di_devlink_fini(di_devlink_handle_t *hdlp);
466 
467 extern di_devlink_handle_t di_devlink_open(const char *root_dir, uint_t flags);
468 extern int di_devlink_close(di_devlink_handle_t *pp, int flag);
469 extern int di_devlink_rm_link(di_devlink_handle_t hdp, const char *link);
470 extern int di_devlink_add_link(di_devlink_handle_t hdp, const char *link,
471     const char *content, int flags);
472 extern int di_devlink_update(di_devlink_handle_t hdp);
473 extern di_devlink_handle_t di_devlink_init_root(const char *root,
474     const char *name, uint_t flags);
475 extern int di_devlink_cache_walk(di_devlink_handle_t hdp, const char *re,
476     const char *path, uint_t flags, void *arg,
477     int (*devlink_callback)(di_devlink_t, void *));
478 
479 /*
480  * Private interfaces for I/O retire
481  */
482 typedef struct di_retire {
483 	void	*rt_hdl;
484 	void	(*rt_abort)(void *hdl, const char *format, ...);
485 	void	(*rt_debug)(void *hdl, const char *format, ...);
486 } di_retire_t;
487 
488 extern int di_retire_device(char *path, di_retire_t *dp, int flags);
489 extern int di_unretire_device(char *path, di_retire_t *dp);
490 extern uint_t di_retired(di_node_t node);
491 
492 /*
493  * Private interfaces for /etc/logindevperm
494  */
495 extern int di_devperm_login(const char *, uid_t, gid_t, void (*)(char *));
496 extern int di_devperm_logout(const char *);
497 
498 /*
499  * Private interface for looking up, by path string, a node/path/minor
500  * in a snapshot.
501  */
502 extern di_path_t di_lookup_path(di_node_t root, char *path);
503 extern di_node_t di_lookup_node(di_node_t root, char *path);
504 
505 /*
506  * Private hotplug interfaces to be used between cfgadm pci plugin and
507  * devfsadm link generator.
508  */
509 extern char *di_dli_name(char *);
510 extern int di_dli_openr(char *);
511 extern int di_dli_openw(char *);
512 extern void di_dli_close(int);
513 
514 /*
515  * Private interface for parsing path_to_inst binding file
516  */
517 extern int devfs_parse_binding_file(const char *,
518 	int (*)(void *, const char *, int, const char *), void *);
519 extern int devfs_walk_minor_nodes(const char *,
520 	int (*)(void *, const char *), void *);
521 
522 /*
523  * finddev - alternate readdir to discover only /dev persisted device names
524  */
525 typedef struct __finddevhdl *finddevhdl_t;
526 
527 extern int		device_exists(const char *);
528 extern int		finddev_readdir(const char *, finddevhdl_t *);
529 extern int		finddev_emptydir(const char *);
530 extern void		finddev_close(finddevhdl_t);
531 extern const char	*finddev_next(finddevhdl_t);
532 
533 
534 /*
535  * Private interfaces for non-global /dev profile
536  */
537 typedef struct __di_prof	*di_prof_t;
538 
539 extern int	di_prof_init(const char *mountpt, di_prof_t *);
540 extern void	di_prof_fini(di_prof_t);
541 extern int	di_prof_commit(di_prof_t);
542 extern int	di_prof_add_dev(di_prof_t, const char *);
543 extern int	di_prof_add_exclude(di_prof_t, const char *);
544 extern int	di_prof_add_symlink(di_prof_t, const char *, const char *);
545 extern int	di_prof_add_map(di_prof_t, const char *, const char *);
546 
547 /*
548  * Private interfaces for <driver><instance><minor> to path conversion.
549  * NOTE: These interfaces do not require or cause attach.  The implementation
550  * uses the kernel instance-tree (/etc/path_to_inst) and the di_devlinks
551  * database information.
552  */
553 typedef struct __di_dim	*di_dim_t;
554 
555 extern di_dim_t	di_dim_init();
556 extern void	di_dim_fini(di_dim_t);
557 extern char	*di_dim_path_devices(di_dim_t,
558 		    char *drv_name, int instance, char *minor_name);
559 extern char	*di_dim_path_dev(di_dim_t,
560 		    char *drv_name, int instance, char *minor_name);
561 
562 
563 #ifdef	__cplusplus
564 }
565 #endif
566 
567 #endif	/* _LIBDEVINFO_H */
568