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