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