xref: /titanic_51/usr/src/uts/common/sys/ddi_impldefs.h (revision 269473047d747f7815af570197e4ef7322d3632c)
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 _SYS_DDI_IMPLDEFS_H
27 #define	_SYS_DDI_IMPLDEFS_H
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/t_lock.h>
32 #include <sys/ddipropdefs.h>
33 #include <sys/devops.h>
34 #include <sys/autoconf.h>
35 #include <sys/mutex.h>
36 #include <vm/page.h>
37 #include <sys/dacf_impl.h>
38 #include <sys/ndifm.h>
39 #include <sys/epm.h>
40 #include <sys/ddidmareq.h>
41 #include <sys/ddi_intr.h>
42 #include <sys/ddi_hp.h>
43 #include <sys/ddi_hp_impl.h>
44 #include <sys/ddi_isa.h>
45 #include <sys/id_space.h>
46 #include <sys/modhash.h>
47 #include <sys/bitset.h>
48 
49 #ifdef	__cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * The device id implementation has been switched to be based on properties.
55  * For compatibility with di_devid libdevinfo interface the following
56  * must be defined:
57  */
58 #define	DEVID_COMPATIBILITY	((ddi_devid_t)-1)
59 
60 /*
61  * Definitions for node class.
62  * DDI_NC_PROM: a node with a nodeid that may be used in a promif call.
63  * DDI_NC_PSEUDO: a software created node with a software assigned nodeid.
64  */
65 typedef enum {
66 	DDI_NC_PROM = 0,
67 	DDI_NC_PSEUDO
68 } ddi_node_class_t;
69 
70 /*
71  * Definitions for generic callback mechanism.
72  */
73 typedef enum {
74 	DDI_CB_INTR_ADD,		/* More available interrupts */
75 	DDI_CB_INTR_REMOVE		/* Fewer available interrupts */
76 } ddi_cb_action_t;
77 
78 typedef enum {
79 	DDI_CB_FLAG_INTR = 0x1		/* Driver is IRM aware */
80 } ddi_cb_flags_t;
81 
82 #define	DDI_CB_FLAG_VALID(f)	((f) & DDI_CB_FLAG_INTR)
83 
84 typedef int	(*ddi_cb_func_t)(dev_info_t *dip, ddi_cb_action_t action,
85 		    void *cbarg, void *arg1, void *arg2);
86 
87 typedef struct ddi_cb {
88 	uint64_t	cb_flags;
89 	dev_info_t	*cb_dip;
90 	ddi_cb_func_t	cb_func;
91 	void		*cb_arg1;
92 	void		*cb_arg2;
93 } ddi_cb_t;
94 
95 /*
96  * dev_info:	The main device information structure this is intended to be
97  *		opaque to drivers and drivers should use ddi functions to
98  *		access *all* driver accessible fields.
99  *
100  * devi_parent_data includes property lists (interrupts, registers, etc.)
101  * devi_driver_data includes whatever the driver wants to place there.
102  */
103 struct devinfo_audit;
104 
105 typedef struct devi_port {
106 	union {
107 		struct {
108 			uint32_t type;
109 			uint32_t pad;
110 		} port;
111 		uint64_t type64;
112 	} info;
113 	void	*priv_p;
114 } devi_port_t;
115 
116 typedef struct devi_bus_priv {
117 	devi_port_t port_up;
118 	devi_port_t port_down;
119 } devi_bus_priv_t;
120 
121 struct iommulib_unit;
122 typedef struct iommulib_unit *iommulib_handle_t;
123 typedef uint8_t	ndi_flavor_t;
124 struct ddi_hp_cn_handle;
125 
126 struct dev_info  {
127 
128 	struct dev_info *devi_parent;	/* my parent node in tree	*/
129 	struct dev_info *devi_child;	/* my child list head		*/
130 	struct dev_info *devi_sibling;	/* next element on my level	*/
131 
132 	char	*devi_binding_name;	/* name used to bind driver:	*/
133 					/* shared storage, points to	*/
134 					/* devi_node_name, devi_compat_names */
135 					/* or devi_rebinding_name	*/
136 
137 	char	*devi_addr;		/* address part of name		*/
138 
139 	int	devi_nodeid;		/* device nodeid		*/
140 	int	devi_instance;		/* device instance number	*/
141 
142 	struct dev_ops *devi_ops;	/* driver operations		*/
143 
144 	void	*devi_parent_data;	/* parent private data		*/
145 	void	*devi_driver_data;	/* driver private data		*/
146 
147 	ddi_prop_t *devi_drv_prop_ptr;	/* head of driver prop list */
148 	ddi_prop_t *devi_sys_prop_ptr;	/* head of system prop list */
149 
150 	struct ddi_minor_data *devi_minor;	/* head of minor list */
151 	struct dev_info *devi_next;	/* Next instance of this device */
152 	kmutex_t devi_lock;		/* Protects per-devinfo data */
153 
154 	/* logical parents for busop primitives */
155 
156 	struct dev_info *devi_bus_map_fault;	/* bus_map_fault parent	 */
157 	struct dev_info *devi_bus_dma_map;	/* bus_dma_map parent	 */
158 	struct dev_info *devi_bus_dma_allochdl; /* bus_dma_newhdl parent */
159 	struct dev_info *devi_bus_dma_freehdl;  /* bus_dma_freehdl parent */
160 	struct dev_info *devi_bus_dma_bindhdl;  /* bus_dma_bindhdl parent */
161 	struct dev_info *devi_bus_dma_unbindhdl; /* bus_dma_unbindhdl parent */
162 	struct dev_info *devi_bus_dma_flush;    /* bus_dma_flush parent	 */
163 	struct dev_info *devi_bus_dma_win;	/* bus_dma_win parent	 */
164 	struct dev_info *devi_bus_dma_ctl;	/* bus_dma_ctl parent	 */
165 	struct dev_info	*devi_bus_ctl;		/* bus_ctl parent	 */
166 
167 	ddi_prop_t *devi_hw_prop_ptr;		/* head of hw prop list */
168 
169 	char	*devi_node_name;		/* The 'name' of the node */
170 	char	*devi_compat_names;		/* A list of driver names */
171 	size_t	devi_compat_length;		/* Size of compat_names */
172 
173 	int (*devi_bus_dma_bindfunc)(dev_info_t *, dev_info_t *,
174 	    ddi_dma_handle_t, struct ddi_dma_req *, ddi_dma_cookie_t *,
175 	    uint_t *);
176 	int (*devi_bus_dma_unbindfunc)(dev_info_t *, dev_info_t *,
177 	    ddi_dma_handle_t);
178 
179 	char		*devi_devid_str;	/* registered device id */
180 
181 	/*
182 	 * power management entries
183 	 * components exist even if the device is not currently power managed
184 	 */
185 	struct pm_info *devi_pm_info;		/* 0 => dev not power managed */
186 	uint_t		devi_pm_flags;		/* pm flags */
187 	int		devi_pm_num_components;	/* number of components */
188 	size_t		devi_pm_comp_size;	/* size of devi_components */
189 	struct pm_component *devi_pm_components; /* array of pm components */
190 	struct dev_info *devi_pm_ppm;		/* ppm attached to this one */
191 	void		*devi_pm_ppm_private;	/* for use by ppm driver */
192 	int		devi_pm_dev_thresh;	/* "device" threshold */
193 	uint_t		devi_pm_kidsupcnt;	/* # of kids powered up */
194 	struct pm_scan	*devi_pm_scan;		/* pm scan info */
195 	uint_t		devi_pm_noinvolpm;	/* # of descendents no-invol */
196 	uint_t		devi_pm_volpmd;		/* # of voluntarily pm'ed */
197 	kmutex_t	devi_pm_lock;		/* pm lock for state */
198 	kmutex_t	devi_pm_busy_lock;	/* for component busy count */
199 
200 	uint_t		devi_state;		/* device/bus state flags */
201 						/* see below for definitions */
202 	kcondvar_t	devi_cv;		/* cv */
203 	int		devi_ref;		/* reference count */
204 
205 	dacf_rsrvlist_t *devi_dacf_tasks;	/* dacf reservation queue */
206 
207 	ddi_node_class_t devi_node_class;	/* Node class */
208 	int		devi_node_attributes;	/* Node attributes: See below */
209 
210 	char		*devi_device_class;
211 
212 	/*
213 	 * New mpxio kernel hooks entries
214 	 */
215 	int		devi_mdi_component;	/* mpxio component type */
216 	void		*devi_mdi_client;	/* mpxio client information */
217 	void		*devi_mdi_xhci;		/* vhci/phci info */
218 
219 	ddi_prop_list_t	*devi_global_prop_list;	/* driver global properties */
220 	major_t		devi_major;		/* driver major number */
221 	ddi_node_state_t devi_node_state;	/* state of node */
222 	uint_t		devi_flags;		/* configuration flags */
223 	int		devi_circular;		/* for recursive operations */
224 	void		*devi_busy_thread;	/* thread operating on node */
225 	void		*devi_taskq;		/* hotplug taskq */
226 
227 	/* device driver statistical and audit info */
228 	struct devinfo_audit *devi_audit;		/* last state change */
229 
230 	/*
231 	 * FMA support for resource caches and error handlers
232 	 */
233 	struct i_ddi_fmhdl	*devi_fmhdl;
234 
235 	uint_t		devi_cpr_flags;
236 
237 	/* Owned by DDI interrupt framework */
238 	devinfo_intr_t	*devi_intr_p;
239 
240 	void		*devi_nex_pm;		/* nexus PM private */
241 
242 	char		*devi_addr_buf;		/* buffer for devi_addr */
243 
244 	char		*devi_rebinding_name;	/* binding_name of rebind */
245 
246 	/* For device contracts that have this dip's minor node as resource */
247 	kmutex_t	devi_ct_lock;		/* contract lock */
248 	kcondvar_t	devi_ct_cv;		/* contract cv */
249 	int		devi_ct_count;		/* # of outstanding responses */
250 	int		devi_ct_neg;		/* neg. occurred on dip */
251 	list_t		devi_ct;
252 
253 	/* owned by bus framework */
254 	devi_bus_priv_t	devi_bus;		/* bus private data */
255 
256 	/* Declarations of the pure dynamic properties to snapshot */
257 	struct i_ddi_prop_dyn	*devi_prop_dyn_driver;	/* prop_op */
258 	struct i_ddi_prop_dyn	*devi_prop_dyn_parent;	/* bus_prop_op */
259 
260 	/* For intel iommu support */
261 	void		*devi_iommu_private;
262 
263 	/* IOMMU handle */
264 	iommulib_handle_t	devi_iommulib_handle;
265 
266 	/* Generic callback mechanism */
267 	ddi_cb_t	*devi_cb_p;
268 
269 	/* ndi 'flavors' */
270 	ndi_flavor_t	devi_flavor;		/* flavor assigned by parent */
271 	ndi_flavor_t	devi_flavorv_n;		/* number of child-flavors */
272 	void		**devi_flavorv;		/* child-flavor specific data */
273 
274 	/* Owned by hotplug framework */
275 	struct ddi_hp_cn_handle *devi_hp_hdlp;   /* hotplug handle list */
276 };
277 
278 #define	DEVI(dev_info_type)	((struct dev_info *)(dev_info_type))
279 
280 /*
281  * NB: The 'name' field, for compatibility with old code (both existing
282  * device drivers and userland code), is now defined as the name used
283  * to bind the node to a device driver, and not the device node name.
284  * If the device node name does not define a binding to a device driver,
285  * and the framework uses a different algorithm to create the binding to
286  * the driver, the node name and binding name will be different.
287  *
288  * Note that this implies that the node name plus instance number does
289  * NOT create a unique driver id; only the binding name plus instance
290  * number creates a unique driver id.
291  *
292  * New code should not use 'devi_name'; use 'devi_binding_name' or
293  * 'devi_node_name' and/or the routines that access those fields.
294  */
295 
296 #define	devi_name devi_binding_name
297 
298 /*
299  * DDI_CF1, DDI_CF2 and DDI_DRV_UNLOADED are obsolete. They are kept
300  * around to allow legacy drivers to to compile.
301  */
302 #define	DDI_CF1(devi)		(DEVI(devi)->devi_addr != NULL)
303 #define	DDI_CF2(devi)		(DEVI(devi)->devi_ops != NULL)
304 #define	DDI_DRV_UNLOADED(devi)	(DEVI(devi)->devi_ops == &mod_nodev_ops)
305 
306 /*
307  * The device state flags (devi_state) contains information regarding
308  * the state of the device (Online/Offline/Down).  For bus nexus
309  * devices, the device state also contains state information regarding
310  * the state of the bus represented by this nexus node.
311  *
312  * Device state information is stored in bits [0-7], bus state in bits
313  * [8-15].
314  *
315  * NOTE: all devi_state updates should be protected by devi_lock.
316  */
317 #define	DEVI_DEVICE_OFFLINE	0x00000001
318 #define	DEVI_DEVICE_DOWN	0x00000002
319 #define	DEVI_DEVICE_DEGRADED	0x00000004
320 #define	DEVI_DEVICE_REMOVED	0x00000008 /* hardware removed */
321 
322 #define	DEVI_BUS_QUIESCED	0x00000100
323 #define	DEVI_BUS_DOWN		0x00000200
324 #define	DEVI_NDI_CONFIG		0x00000400 /* perform config when attaching */
325 
326 #define	DEVI_S_ATTACHING	0x00010000
327 #define	DEVI_S_DETACHING	0x00020000
328 #define	DEVI_S_ONLINING		0x00040000
329 #define	DEVI_S_OFFLINING	0x00080000
330 
331 #define	DEVI_S_INVOKING_DACF	0x00100000 /* busy invoking a dacf task */
332 
333 #define	DEVI_S_UNBOUND		0x00200000
334 #define	DEVI_S_REPORT		0x08000000 /* report status change */
335 
336 #define	DEVI_S_EVADD		0x10000000 /* state of devfs event */
337 #define	DEVI_S_EVREMOVE		0x20000000 /* state of devfs event */
338 #define	DEVI_S_NEED_RESET	0x40000000 /* devo_reset should be called */
339 
340 /*
341  * Device state macros.
342  * o All SET/CLR/DONE users must protect context with devi_lock.
343  * o DEVI_SET_DEVICE_ONLINE users must do his own DEVI_SET_REPORT.
344  * o DEVI_SET_DEVICE_{DOWN|DEGRADED|UP} should only be used when !OFFLINE.
345  * o DEVI_SET_DEVICE_UP clears DOWN and DEGRADED.
346  */
347 #define	DEVI_IS_DEVICE_OFFLINE(dip)					\
348 	((DEVI(dip)->devi_state & DEVI_DEVICE_OFFLINE) == DEVI_DEVICE_OFFLINE)
349 
350 #define	DEVI_SET_DEVICE_ONLINE(dip)	{				\
351 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
352 	if (DEVI(dip)->devi_state & DEVI_DEVICE_DEGRADED) {		\
353 		mutex_exit(&DEVI(dip)->devi_lock);			\
354 		e_ddi_undegrade_finalize(dip);				\
355 		mutex_enter(&DEVI(dip)->devi_lock);			\
356 	}								\
357 	/* setting ONLINE clears DOWN, DEGRADED, OFFLINE */		\
358 	DEVI(dip)->devi_state &= ~(DEVI_DEVICE_DOWN |			\
359 	    DEVI_DEVICE_DEGRADED | DEVI_DEVICE_OFFLINE);		\
360 	}
361 
362 #define	DEVI_SET_DEVICE_OFFLINE(dip)	{				\
363 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
364 	DEVI(dip)->devi_state |= (DEVI_DEVICE_OFFLINE | DEVI_S_REPORT);	\
365 	}
366 
367 #define	DEVI_IS_DEVICE_DOWN(dip)					\
368 	((DEVI(dip)->devi_state & DEVI_DEVICE_DOWN) == DEVI_DEVICE_DOWN)
369 
370 #define	DEVI_SET_DEVICE_DOWN(dip)	{				\
371 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
372 	ASSERT(!DEVI_IS_DEVICE_OFFLINE(dip));				\
373 	DEVI(dip)->devi_state |= (DEVI_DEVICE_DOWN | DEVI_S_REPORT);	\
374 	}
375 
376 #define	DEVI_IS_DEVICE_DEGRADED(dip)					\
377 	((DEVI(dip)->devi_state &					\
378 	    (DEVI_DEVICE_DEGRADED|DEVI_DEVICE_DOWN)) == DEVI_DEVICE_DEGRADED)
379 
380 #define	DEVI_SET_DEVICE_DEGRADED(dip)	{				\
381 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
382 	ASSERT(!DEVI_IS_DEVICE_OFFLINE(dip));				\
383 	mutex_exit(&DEVI(dip)->devi_lock);				\
384 	e_ddi_degrade_finalize(dip);					\
385 	mutex_enter(&DEVI(dip)->devi_lock);				\
386 	DEVI(dip)->devi_state |= (DEVI_DEVICE_DEGRADED | DEVI_S_REPORT); \
387 	}
388 
389 #define	DEVI_SET_DEVICE_UP(dip)		{				\
390 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
391 	ASSERT(!DEVI_IS_DEVICE_OFFLINE(dip));				\
392 	if (DEVI(dip)->devi_state & DEVI_DEVICE_DEGRADED) {		\
393 		mutex_exit(&DEVI(dip)->devi_lock);			\
394 		e_ddi_undegrade_finalize(dip);				\
395 		mutex_enter(&DEVI(dip)->devi_lock);			\
396 	}								\
397 	DEVI(dip)->devi_state &= ~(DEVI_DEVICE_DEGRADED | DEVI_DEVICE_DOWN); \
398 	DEVI(dip)->devi_state |= DEVI_S_REPORT;				\
399 	}
400 
401 /* Device removal and insertion */
402 #define	DEVI_IS_DEVICE_REMOVED(dip)					\
403 	((DEVI(dip)->devi_state & DEVI_DEVICE_REMOVED) == DEVI_DEVICE_REMOVED)
404 
405 #define	DEVI_SET_DEVICE_REMOVED(dip)	{				\
406 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
407 	DEVI(dip)->devi_state |= DEVI_DEVICE_REMOVED | DEVI_S_REPORT;	\
408 	}
409 
410 #define	DEVI_SET_DEVICE_REINSERTED(dip)	{				\
411 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
412 	DEVI(dip)->devi_state &= ~DEVI_DEVICE_REMOVED;			\
413 	DEVI(dip)->devi_state |= DEVI_S_REPORT;				\
414 	}
415 
416 /* Bus state change macros */
417 #define	DEVI_IS_BUS_QUIESCED(dip)					\
418 	((DEVI(dip)->devi_state & DEVI_BUS_QUIESCED) == DEVI_BUS_QUIESCED)
419 
420 #define	DEVI_SET_BUS_ACTIVE(dip)	{				\
421 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
422 	DEVI(dip)->devi_state &= ~DEVI_BUS_QUIESCED;			\
423 	DEVI(dip)->devi_state |= DEVI_S_REPORT;				\
424 	}
425 
426 #define	DEVI_SET_BUS_QUIESCE(dip)	{				\
427 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
428 	DEVI(dip)->devi_state |= (DEVI_BUS_QUIESCED | DEVI_S_REPORT);	\
429 	}
430 
431 #define	DEVI_IS_BUS_DOWN(dip)						\
432 	((DEVI(dip)->devi_state & DEVI_BUS_DOWN) == DEVI_BUS_DOWN)
433 
434 #define	DEVI_SET_BUS_UP(dip)		{				\
435 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
436 	DEVI(dip)->devi_state &= ~DEVI_BUS_DOWN;			\
437 	DEVI(dip)->devi_state |= DEVI_S_REPORT;				\
438 	}
439 
440 #define	DEVI_SET_BUS_DOWN(dip)		{				\
441 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
442 	DEVI(dip)->devi_state |= (DEVI_BUS_DOWN | DEVI_S_REPORT);	\
443 	}
444 
445 /* Status change report needed */
446 #define	DEVI_NEED_REPORT(dip)						\
447 	((DEVI(dip)->devi_state & DEVI_S_REPORT) == DEVI_S_REPORT)
448 
449 #define	DEVI_SET_REPORT(dip)		{				\
450 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
451 	DEVI(dip)->devi_state |= DEVI_S_REPORT;				\
452 	}
453 
454 #define	DEVI_REPORT_DONE(dip)		{				\
455 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
456 	DEVI(dip)->devi_state &= ~DEVI_S_REPORT;			\
457 	}
458 
459 /* Do an NDI_CONFIG for its children */
460 #define	DEVI_NEED_NDI_CONFIG(dip)					\
461 	((DEVI(dip)->devi_state & DEVI_NDI_CONFIG) == DEVI_NDI_CONFIG)
462 
463 #define	DEVI_SET_NDI_CONFIG(dip)	{				\
464 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
465 	DEVI(dip)->devi_state |= DEVI_NDI_CONFIG;			\
466 	}
467 
468 #define	DEVI_CLR_NDI_CONFIG(dip)	{				\
469 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
470 	DEVI(dip)->devi_state &= ~DEVI_NDI_CONFIG;			\
471 	}
472 
473 /* Attaching or detaching state */
474 #define	DEVI_IS_ATTACHING(dip)						\
475 	((DEVI(dip)->devi_state & DEVI_S_ATTACHING) == DEVI_S_ATTACHING)
476 
477 #define	DEVI_SET_ATTACHING(dip)		{				\
478 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
479 	DEVI(dip)->devi_state |= DEVI_S_ATTACHING;			\
480 	}
481 
482 #define	DEVI_CLR_ATTACHING(dip)		{				\
483 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
484 	DEVI(dip)->devi_state &= ~DEVI_S_ATTACHING;			\
485 	}
486 
487 #define	DEVI_IS_DETACHING(dip)						\
488 	((DEVI(dip)->devi_state & DEVI_S_DETACHING) == DEVI_S_DETACHING)
489 
490 #define	DEVI_SET_DETACHING(dip)		{				\
491 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
492 	DEVI(dip)->devi_state |= DEVI_S_DETACHING;			\
493 	}
494 
495 #define	DEVI_CLR_DETACHING(dip)		{				\
496 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
497 	DEVI(dip)->devi_state &= ~DEVI_S_DETACHING;			\
498 	}
499 
500 /* Onlining or offlining state */
501 #define	DEVI_IS_ONLINING(dip)						\
502 	((DEVI(dip)->devi_state & DEVI_S_ONLINING) == DEVI_S_ONLINING)
503 
504 #define	DEVI_SET_ONLINING(dip)		{				\
505 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
506 	DEVI(dip)->devi_state |= DEVI_S_ONLINING;			\
507 	}
508 
509 #define	DEVI_CLR_ONLINING(dip)		{				\
510 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
511 	DEVI(dip)->devi_state &= ~DEVI_S_ONLINING;			\
512 	}
513 
514 #define	DEVI_IS_OFFLINING(dip)						\
515 	((DEVI(dip)->devi_state & DEVI_S_OFFLINING) == DEVI_S_OFFLINING)
516 
517 #define	DEVI_SET_OFFLINING(dip)		{				\
518 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
519 	DEVI(dip)->devi_state |= DEVI_S_OFFLINING;			\
520 	}
521 
522 #define	DEVI_CLR_OFFLINING(dip)		{				\
523 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
524 	DEVI(dip)->devi_state &= ~DEVI_S_OFFLINING;			\
525 	}
526 
527 #define	DEVI_IS_IN_RECONFIG(dip)					\
528 	(DEVI(dip)->devi_state & (DEVI_S_OFFLINING | DEVI_S_ONLINING))
529 
530 /* Busy invoking a dacf task against this node */
531 #define	DEVI_IS_INVOKING_DACF(dip)					\
532 	((DEVI(dip)->devi_state & DEVI_S_INVOKING_DACF) == DEVI_S_INVOKING_DACF)
533 
534 #define	DEVI_SET_INVOKING_DACF(dip)	{				\
535 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
536 	DEVI(dip)->devi_state |= DEVI_S_INVOKING_DACF;			\
537 	}
538 
539 #define	DEVI_CLR_INVOKING_DACF(dip)	{				\
540 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
541 	DEVI(dip)->devi_state &= ~DEVI_S_INVOKING_DACF;			\
542 	}
543 
544 /* Events for add/remove */
545 #define	DEVI_EVADD(dip)							\
546 	((DEVI(dip)->devi_state & DEVI_S_EVADD) == DEVI_S_EVADD)
547 
548 #define	DEVI_SET_EVADD(dip)		{				\
549 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
550 	DEVI(dip)->devi_state &= ~DEVI_S_EVREMOVE;			\
551 	DEVI(dip)->devi_state |= DEVI_S_EVADD;				\
552 	}
553 
554 #define	DEVI_EVREMOVE(dip)						\
555 	((DEVI(dip)->devi_state & DEVI_S_EVREMOVE) == DEVI_S_EVREMOVE)
556 
557 #define	DEVI_SET_EVREMOVE(dip)		{				\
558 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
559 	DEVI(dip)->devi_state &= ~DEVI_S_EVADD;				\
560 	DEVI(dip)->devi_state |= DEVI_S_EVREMOVE;			\
561 	}
562 
563 #define	DEVI_SET_EVUNINIT(dip)		{				\
564 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
565 	DEVI(dip)->devi_state &= ~(DEVI_S_EVADD | DEVI_S_EVREMOVE);	\
566 	}
567 
568 /* Need to call the devo_reset entry point for this device at shutdown */
569 #define	DEVI_NEED_RESET(dip)						\
570 	((DEVI(dip)->devi_state & DEVI_S_NEED_RESET) == DEVI_S_NEED_RESET)
571 
572 #define	DEVI_SET_NEED_RESET(dip)	{				\
573 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
574 	DEVI(dip)->devi_state |= DEVI_S_NEED_RESET;			\
575 	}
576 
577 #define	DEVI_CLR_NEED_RESET(dip)	{				\
578 	ASSERT(mutex_owned(&DEVI(dip)->devi_lock));			\
579 	DEVI(dip)->devi_state &= ~DEVI_S_NEED_RESET;			\
580 	}
581 
582 /*
583  * devi_flags bits
584  *
585  * NOTE: all devi_state updates should be protected by devi_lock.
586  */
587 #define	DEVI_BUSY		0x00000001 /* busy configuring children */
588 #define	DEVI_MADE_CHILDREN	0x00000002 /* children made from specs */
589 #define	DEVI_ATTACHED_CHILDREN	0x00000004 /* attached all existing children */
590 #define	DEVI_BRANCH_HELD	0x00000008 /* branch rooted at this dip held */
591 #define	DEVI_NO_BIND		0x00000010 /* prevent driver binding */
592 #define	DEVI_REGISTERED_DEVID	0x00000020 /* device registered a devid */
593 #define	DEVI_PHCI_SIGNALS_VHCI	0x00000040 /* pHCI ndi_devi_exit signals vHCI */
594 #define	DEVI_REBIND		0x00000080 /* post initchild driver rebind */
595 #define	DEVI_RETIRED		0x00000100 /* device is retired */
596 #define	DEVI_RETIRING		0x00000200 /* being evaluated for retire */
597 #define	DEVI_R_CONSTRAINT	0x00000400 /* constraints have been applied  */
598 #define	DEVI_R_BLOCKED		0x00000800 /* constraints block retire  */
599 #define	DEVI_CT_NOP		0x00001000 /*  NOP contract event occurred */
600 
601 #define	DEVI_BUSY_CHANGING(dip)	(DEVI(dip)->devi_flags & DEVI_BUSY)
602 #define	DEVI_BUSY_OWNED(dip)	(DEVI_BUSY_CHANGING(dip) &&	\
603 	((DEVI(dip))->devi_busy_thread == curthread))
604 
605 char	*i_ddi_devi_class(dev_info_t *);
606 int	i_ddi_set_devi_class(dev_info_t *, char *, int);
607 
608 /*
609  * This structure represents one piece of bus space occupied by a given
610  * device. It is used in an array for devices with multiple address windows.
611  */
612 struct regspec {
613 	uint_t regspec_bustype;		/* cookie for bus type it's on */
614 	uint_t regspec_addr;		/* address of reg relative to bus */
615 	uint_t regspec_size;		/* size of this register set */
616 };
617 
618 /*
619  * This structure represents one piece of nexus bus space.
620  * It is used in an array for nexi with multiple bus spaces
621  * to define the childs offsets in the parents bus space.
622  */
623 struct rangespec {
624 	uint_t rng_cbustype;		/* Child's address, hi order */
625 	uint_t rng_coffset;		/* Child's address, lo order */
626 	uint_t rng_bustype;		/* Parent's address, hi order */
627 	uint_t rng_offset;		/* Parent's address, lo order */
628 	uint_t rng_size;		/* size of space for this entry */
629 };
630 
631 #ifdef _KERNEL
632 
633 typedef enum {
634 	DDI_PRE = 0,
635 	DDI_POST = 1
636 } ddi_pre_post_t;
637 
638 /*
639  * This structure represents notification of a child attach event
640  * These could both be the same if attach/detach commands were in the
641  * same name space.
642  * Note that the target dip is passed as an arg already.
643  */
644 struct attachspec {
645 	ddi_attach_cmd_t cmd;	/* type of event */
646 	ddi_pre_post_t	when;	/* one of DDI_PRE or DDI_POST */
647 	dev_info_t	*pdip;	/* parent of attaching node */
648 	int		result;	/* result of attach op (post command only) */
649 };
650 
651 /*
652  * This structure represents notification of a child detach event
653  * Note that the target dip is passed as an arg already.
654  */
655 struct detachspec {
656 	ddi_detach_cmd_t cmd;	/* type of event */
657 	ddi_pre_post_t	when;	/* one of DDI_PRE or DDI_POST */
658 	dev_info_t	*pdip;	/* parent of detaching node */
659 	int		result;	/* result of detach op (post command only) */
660 };
661 
662 #endif /* _KERNEL */
663 
664 typedef enum {
665 	DDM_MINOR = 0,
666 	DDM_ALIAS,
667 	DDM_DEFAULT,
668 	DDM_INTERNAL_PATH
669 } ddi_minor_type;
670 
671 /* implementation flags for driver specified device access control */
672 #define	DM_NO_FSPERM	0x1
673 
674 struct devplcy;
675 
676 struct ddi_minor {
677 	char		*name;		/* name of node */
678 	dev_t		dev;		/* device number */
679 	int		spec_type;	/* block or char */
680 	int		flags;		/* access flags */
681 	char		*node_type;	/* block, byte, serial, network */
682 	struct devplcy	*node_priv;	/* privilege for this minor */
683 	mode_t		priv_mode;	/* default apparent privilege mode */
684 };
685 
686 /*
687  * devi_node_attributes contains node attributes private to the
688  * ddi implementation. As a consumer, do not use these bit definitions
689  * directly, use the ndi functions that check for the existence of the
690  * specific node attributes.
691  *
692  * DDI_PERSISTENT indicates a 'persistent' node; one that is not
693  * automatically freed by the framework if the driver is unloaded
694  * or the driver fails to attach to this node.
695  *
696  * DDI_AUTO_ASSIGNED_NODEID indicates that the nodeid was auto-assigned
697  * by the framework and should be auto-freed if the node is removed.
698  *
699  * DDI_VHCI_NODE indicates that the node type is VHCI. This flag
700  * must be set by ndi_devi_config_vhci() routine only.
701  *
702  * DDI_HIDDEN_NODE indicates that the node should not show up in snapshots
703  * or in /devices.
704  *
705  * DDI_HOTPLUG_NODE indicates that the node created by nexus hotplug.
706  */
707 #define	DDI_PERSISTENT			0x01
708 #define	DDI_AUTO_ASSIGNED_NODEID	0x02
709 #define	DDI_VHCI_NODE			0x04
710 #define	DDI_HIDDEN_NODE			0x08
711 #define	DDI_HOTPLUG_NODE		0x10
712 
713 #define	DEVI_VHCI_NODE(dip)						\
714 	(DEVI(dip)->devi_node_attributes & DDI_VHCI_NODE)
715 
716 /*
717  * The ddi_minor_data structure gets filled in by ddi_create_minor_node.
718  * It then gets attached to the devinfo node as a property.
719  */
720 struct ddi_minor_data {
721 	struct ddi_minor_data *next;	/* next one in the chain */
722 	dev_info_t	*dip;		/* pointer to devinfo node */
723 	ddi_minor_type	type;		/* Following data type */
724 	struct ddi_minor d_minor;	/* Actual minor node data */
725 };
726 
727 #define	ddm_name	d_minor.name
728 #define	ddm_dev		d_minor.dev
729 #define	ddm_flags	d_minor.flags
730 #define	ddm_spec_type	d_minor.spec_type
731 #define	ddm_node_type	d_minor.node_type
732 #define	ddm_node_priv	d_minor.node_priv
733 #define	ddm_priv_mode	d_minor.priv_mode
734 
735 /*
736  * parent private data structure contains register, interrupt, property
737  * and range information.
738  */
739 struct ddi_parent_private_data {
740 	int par_nreg;			/* number of regs */
741 	struct regspec *par_reg;	/* array of regs */
742 	int par_nintr;			/* number of interrupts */
743 	struct intrspec *par_intr;	/* array of possible interrupts */
744 	int par_nrng;			/* number of ranges */
745 	struct rangespec *par_rng;	/* array of ranges */
746 };
747 #define	DEVI_PD(d)	\
748 	((struct ddi_parent_private_data *)DEVI((d))->devi_parent_data)
749 
750 #define	sparc_pd_getnreg(dev)		(DEVI_PD(dev)->par_nreg)
751 #define	sparc_pd_getnintr(dev)		(DEVI_PD(dev)->par_nintr)
752 #define	sparc_pd_getnrng(dev)		(DEVI_PD(dev)->par_nrng)
753 #define	sparc_pd_getreg(dev, n)		(&DEVI_PD(dev)->par_reg[(n)])
754 #define	sparc_pd_getintr(dev, n)	(&DEVI_PD(dev)->par_intr[(n)])
755 #define	sparc_pd_getrng(dev, n)		(&DEVI_PD(dev)->par_rng[(n)])
756 
757 #ifdef _KERNEL
758 /*
759  * This data structure is private to the indexed soft state allocator.
760  */
761 typedef struct i_ddi_soft_state {
762 	void		**array;	/* the array of pointers */
763 	kmutex_t	lock;		/* serialize access to this struct */
764 	size_t		size;		/* how many bytes per state struct */
765 	size_t		n_items;	/* how many structs herein */
766 	struct i_ddi_soft_state *next;	/* 'dirty' elements */
767 } i_ddi_soft_state;
768 
769 /*
770  * This data structure is private to the stringhashed soft state allocator.
771  */
772 typedef struct i_ddi_soft_state_bystr {
773 	size_t		ss_size;	/* how many bytes per state struct */
774 	mod_hash_t	*ss_mod_hash;	/* hash implementation */
775 } i_ddi_soft_state_bystr;
776 
777 /*
778  * This data structure is private to the ddi_strid_* implementation
779  */
780 typedef struct i_ddi_strid {
781 	id_space_t	*strid_space;
782 	mod_hash_t	*strid_byid;
783 	mod_hash_t	*strid_bystr;
784 } i_ddi_strid;
785 #endif /* _KERNEL */
786 
787 /*
788  * Solaris DDI DMA implementation structure and function definitions.
789  *
790  * Note: no callers of DDI functions must depend upon data structures
791  * declared below. They are not guaranteed to remain constant.
792  */
793 
794 /*
795  * Implementation DMA mapping structure.
796  *
797  * The publicly visible ddi_dma_req structure is filled
798  * in by a caller that wishes to map a memory object
799  * for DMA. Internal to this implementation of the public
800  * DDI DMA functions this request structure is put together
801  * with bus nexus specific functions that have additional
802  * information and constraints as to how to go about doing
803  * the requested mapping function
804  *
805  * In this implementation, some of the information from the
806  * original requester is retained throughout the lifetime
807  * of the I/O mapping being active.
808  */
809 
810 /*
811  * This is the implementation specific description
812  * of how we've mapped an object for DMA.
813  */
814 #if defined(__sparc)
815 typedef struct ddi_dma_impl {
816 	/*
817 	 * DMA mapping information
818 	 */
819 	ulong_t	dmai_mapping;	/* mapping cookie */
820 
821 	/*
822 	 * Size of the current mapping, in bytes.
823 	 *
824 	 * Note that this is distinct from the size of the object being mapped
825 	 * for DVMA. We might have only a portion of the object mapped at any
826 	 * given point in time.
827 	 */
828 	uint_t	dmai_size;
829 
830 	/*
831 	 * Offset, in bytes, into object that is currently mapped.
832 	 */
833 	off_t	dmai_offset;
834 
835 	/*
836 	 * Information gathered from the original DMA mapping
837 	 * request and saved for the lifetime of the mapping.
838 	 */
839 	uint_t		dmai_minxfer;
840 	uint_t		dmai_burstsizes;
841 	uint_t		dmai_ndvmapages;
842 	uint_t		dmai_pool;	/* cached DVMA space */
843 	uint_t		dmai_rflags;	/* requester's flags + ours */
844 	uint_t		dmai_inuse;	/* active handle? */
845 	uint_t		dmai_nwin;
846 	uint_t		dmai_winsize;
847 	caddr_t		dmai_nexus_private;
848 	void		*dmai_iopte;
849 	uint_t		*dmai_sbi;
850 	void		*dmai_minfo;	/* random mapping information */
851 	dev_info_t	*dmai_rdip;	/* original requester's dev_info_t */
852 	ddi_dma_obj_t	dmai_object;	/* requester's object */
853 	ddi_dma_attr_t	dmai_attr;	/* DMA attributes */
854 	ddi_dma_cookie_t *dmai_cookie;	/* pointer to first DMA cookie */
855 
856 	int		(*dmai_fault_check)(struct ddi_dma_impl *handle);
857 	void		(*dmai_fault_notify)(struct ddi_dma_impl *handle);
858 	int		dmai_fault;
859 	ndi_err_t	dmai_error;
860 
861 } ddi_dma_impl_t;
862 
863 #elif defined(__x86)
864 
865 /*
866  * ddi_dma_impl portion that genunix (sunddi.c) depends on. x86 rootnex
867  * implementation specific state is in dmai_private.
868  */
869 typedef struct ddi_dma_impl {
870 	ddi_dma_cookie_t *dmai_cookie; /* array of DMA cookies */
871 	void		*dmai_private;
872 
873 	/*
874 	 * Information gathered from the original dma mapping
875 	 * request and saved for the lifetime of the mapping.
876 	 */
877 	uint_t		dmai_minxfer;
878 	uint_t		dmai_burstsizes;
879 	uint_t		dmai_rflags;	/* requester's flags + ours */
880 	int		dmai_nwin;
881 	dev_info_t	*dmai_rdip;	/* original requester's dev_info_t */
882 
883 	ddi_dma_attr_t	dmai_attr;	/* DMA attributes */
884 
885 	int		(*dmai_fault_check)(struct ddi_dma_impl *handle);
886 	void		(*dmai_fault_notify)(struct ddi_dma_impl *handle);
887 	int		dmai_fault;
888 	ndi_err_t	dmai_error;
889 } ddi_dma_impl_t;
890 
891 #else
892 #error "struct ddi_dma_impl not defined for this architecture"
893 #endif  /* defined(__sparc) */
894 
895 /*
896  * For now DMA segments share state with the DMA handle
897  */
898 typedef ddi_dma_impl_t ddi_dma_seg_impl_t;
899 
900 /*
901  * These flags use reserved bits from the dma request flags.
902  *
903  * A note about the DMP_NOSYNC flags: the root nexus will
904  * set these as it sees best. If an intermediate nexus
905  * actually needs these operations, then during the unwind
906  * from the call to ddi_dma_bind, the nexus driver *must*
907  * clear the appropriate flag(s). This is because, as an
908  * optimization, ddi_dma_sync(9F) looks at these flags before
909  * deciding to spend the time going back up the tree.
910  */
911 
912 #define	_DMCM1	DDI_DMA_RDWR|DDI_DMA_REDZONE|DDI_DMA_PARTIAL
913 #define	_DMCM2	DDI_DMA_CONSISTENT|DMP_VMEREQ
914 #define	DMP_DDIFLAGS	(_DMCM1|_DMCM2)
915 #define	DMP_SHADOW	0x20
916 #define	DMP_LKIOPB	0x40
917 #define	DMP_LKSYSV	0x80
918 #define	DMP_IOCACHE	0x100
919 #define	DMP_USEHAT	0x200
920 #define	DMP_PHYSADDR	0x400
921 #define	DMP_INVALID	0x800
922 #define	DMP_NOLIMIT	0x1000
923 #define	DMP_VMEREQ	0x10000000
924 #define	DMP_BYPASSNEXUS	0x20000000
925 #define	DMP_NODEVSYNC	0x40000000
926 #define	DMP_NOCPUSYNC	0x80000000
927 #define	DMP_NOSYNC	(DMP_NODEVSYNC|DMP_NOCPUSYNC)
928 
929 /*
930  * In order to complete a device to device mapping that
931  * has percolated as high as an IU nexus (gone that high
932  * because the DMA request is a VADDR type), we define
933  * structure to use with the DDI_CTLOPS_DMAPMAPC request
934  * that re-traverses the request tree to finish the
935  * DMA 'mapping' for a device.
936  */
937 struct dma_phys_mapc {
938 	struct ddi_dma_req *dma_req;	/* original request */
939 	ddi_dma_impl_t *mp;		/* current handle, or none */
940 	int nptes;			/* number of ptes */
941 	void *ptes;			/* ptes already read */
942 };
943 
944 #define	MAXCALLBACK		20
945 
946 /*
947  * Callback definitions
948  */
949 struct ddi_callback {
950 	struct ddi_callback	*c_nfree;
951 	struct ddi_callback	*c_nlist;
952 	int			(*c_call)();
953 	int			c_count;
954 	caddr_t			c_arg;
955 	size_t			c_size;
956 };
957 
958 /*
959  * Pure dynamic property declaration. A pure dynamic property is a property
960  * for which a driver's prop_op(9E) implementation will return a value on
961  * demand, but the property name does not exist on a property list (global,
962  * driver, system, or hardware) - the person asking for the value must know
963  * the name and type information.
964  *
965  * For a pure dynamic property to show up in a di_init() devinfo shapshot, the
966  * devinfo driver must know name and type. The i_ddi_prop_dyn_t mechanism
967  * allows a driver to define an array of the name/type information of its
968  * dynamic properties. When a driver declares its dynamic properties in a
969  * i_ddi_prop_dyn_t array, and registers that array using
970  * i_ddi_prop_dyn_driver_set() the devinfo driver has sufficient information
971  * to represent the properties in a snapshot - calling the driver's
972  * prop_op(9E) to obtain values.
973  *
974  * The last element of a i_ddi_prop_dyn_t is detected via a NULL dp_name value.
975  *
976  * A pure dynamic property name associated with a minor_node/dev_t should be
977  * defined with a dp_spec_type of S_IFCHR or S_IFBLK, as appropriate.  The
978  * driver's prop_op(9E) entry point will be called for all
979  * ddi_create_minor_node(9F) nodes of the specified spec_type. For a driver
980  * where not all minor_node/dev_t combinations support the same named
981  * properties, it is the responsibility of the prop_op(9E) implementation to
982  * sort out what combinations are appropriate.
983  *
984  * A pure dynamic property of a devinfo node should be defined with a
985  * dp_spec_type of 0.
986  *
987  * NB: Public DDI property interfaces no longer support pure dynamic
988  * properties, but they are still still used.  A prime example is the cmlb
989  * implementation of size(9P) properties. Using pure dynamic properties
990  * reduces the space required to maintain per-partition information. Since
991  * there are no public interfaces to create pure dynamic properties,
992  * the i_ddi_prop_dyn_t mechanism should remain private.
993  */
994 typedef struct i_ddi_prop_dyn {
995 	char	*dp_name;		/* name of dynamic property */
996 	int	dp_type;		/* DDI_PROP_TYPE_ of property */
997 	int	dp_spec_type;		/* 0, S_IFCHR, S_IFBLK */
998 } i_ddi_prop_dyn_t;
999 void			i_ddi_prop_dyn_driver_set(dev_info_t *,
1000 			    i_ddi_prop_dyn_t *);
1001 i_ddi_prop_dyn_t	*i_ddi_prop_dyn_driver_get(dev_info_t *);
1002 void			i_ddi_prop_dyn_parent_set(dev_info_t *,
1003 			    i_ddi_prop_dyn_t *);
1004 i_ddi_prop_dyn_t	*i_ddi_prop_dyn_parent_get(dev_info_t *);
1005 void			i_ddi_prop_dyn_cache_invalidate(dev_info_t *,
1006 			    i_ddi_prop_dyn_t *);
1007 
1008 /*
1009  * Device id - Internal definition.
1010  */
1011 #define	DEVID_MAGIC_MSB		0x69
1012 #define	DEVID_MAGIC_LSB		0x64
1013 #define	DEVID_REV_MSB		0x00
1014 #define	DEVID_REV_LSB		0x01
1015 #define	DEVID_HINT_SIZE		4
1016 
1017 typedef struct impl_devid {
1018 	uchar_t	did_magic_hi;			/* device id magic # (msb) */
1019 	uchar_t	did_magic_lo;			/* device id magic # (lsb) */
1020 	uchar_t	did_rev_hi;			/* device id revision # (msb) */
1021 	uchar_t	did_rev_lo;			/* device id revision # (lsb) */
1022 	uchar_t	did_type_hi;			/* device id type (msb) */
1023 	uchar_t	did_type_lo;			/* device id type (lsb) */
1024 	uchar_t	did_len_hi;			/* length of devid data (msb) */
1025 	uchar_t	did_len_lo;			/* length of devid data (lsb) */
1026 	char	did_driver[DEVID_HINT_SIZE];	/* driver name - HINT */
1027 	char	did_id[1];			/* start of device id data */
1028 } impl_devid_t;
1029 
1030 #define	DEVID_GETTYPE(devid)		((ushort_t) \
1031 					    (((devid)->did_type_hi << NBBY) + \
1032 					    (devid)->did_type_lo))
1033 
1034 #define	DEVID_FORMTYPE(devid, type)	(devid)->did_type_hi = hibyte((type)); \
1035 					(devid)->did_type_lo = lobyte((type));
1036 
1037 #define	DEVID_GETLEN(devid)		((ushort_t) \
1038 					    (((devid)->did_len_hi << NBBY) + \
1039 					    (devid)->did_len_lo))
1040 
1041 #define	DEVID_FORMLEN(devid, len)	(devid)->did_len_hi = hibyte((len)); \
1042 					(devid)->did_len_lo = lobyte((len));
1043 
1044 /*
1045  * Per PSARC/1995/352, a binary devid contains fields for <magic number>,
1046  * <revision>, <driver_hint>, <type>, <id_length>, and the <id> itself.
1047  * This proposal would encode the binary devid into a string consisting
1048  * of "<magic><revision>,<driver_hint>@<type><id>" as indicated below
1049  * (<id_length> is rederived from the length of the string
1050  * representation of the <id>):
1051  *
1052  *	<magic>		->"id"
1053  *
1054  *	<rev>		->"%d"	// "0" -> type of DEVID_NONE  "id0"
1055  *				// NOTE: PSARC/1995/352 <revision> is "1".
1056  *				// NOTE: support limited to 10 revisions
1057  *				//	in current implementation
1058  *
1059  *	<driver_hint>	->"%s"	// "sd"/"ssd"
1060  *				// NOTE: driver names limited to 4
1061  *				//	characters for <revision> "1"
1062  *
1063  *	<type>		->'w' |	// DEVID_SCSI3_WWN	<hex_id>
1064  *			'W' |	// DEVID_SCSI3_WWN	<ascii_id>
1065  *			't' |	// DEVID_SCSI3_VPD_T10	<hex_id>
1066  *			'T' |	// DEVID_SCSI3_VPD_T10	<ascii_id>
1067  *			'x' |	// DEVID_SCSI3_VPD_EUI	<hex_id>
1068  *			'X' |	// DEVID_SCSI3_VPD_EUI	<ascii_id>
1069  *			'n' |	// DEVID_SCSI3_VPD_NAA	<hex_id>
1070  *			'N' |	// DEVID_SCSI3_VPD_NAA	<ascii_id>
1071  *			's' |	// DEVID_SCSI_SERIAL	<hex_id>
1072  *			'S' |	// DEVID_SCSI_SERIAL	<ascii_id>
1073  *			'f' |	// DEVID_FAB		<hex_id>
1074  *			'F' |	// DEVID_FAB		<ascii_id>
1075  *			'e' |	// DEVID_ENCAP		<hex_id>
1076  *			'E' |	// DEVID_ENCAP		<ascii_id>
1077  *			'a' |	// DEVID_ATA_SERIAL	<hex_id>
1078  *			'A' |	// DEVID_ATA_SERIAL	<ascii_id>
1079  *			'u' |	// unknown		<hex_id>
1080  *			'U'	// unknown		<ascii_id>
1081  *				// NOTE:lower case -> <hex_id>
1082  *				//	upper case -> <ascii_id>
1083  *				// NOTE:this covers all types currently
1084  *				//	defined for <revision> 1.
1085  *				// NOTE:a <type> can be added
1086  *				//	without changing the <revision>.
1087  *
1088  *	<id>		-> <ascii_id> |	// <type> is upper case
1089  *			<hex_id>	// <type> is lower case
1090  *
1091  *	<ascii_id>	// only if all bytes of binary <id> field
1092  *			// are in the set:
1093  *			//	[A-Z][a-z][0-9]+-.= and space and 0x00
1094  *			// the encoded form is:
1095  *			//	[A-Z][a-z][0-9]+-.= and _ and ~
1096  *			//	NOTE: ' ' <=> '_', 0x00 <=> '~'
1097  *			// these sets are chosen to avoid shell
1098  *			// and conflicts with DDI node names.
1099  *
1100  *	<hex_id>	// if not <ascii_id>; each byte of binary
1101  *			// <id> maps a to 2 digit ascii hex
1102  *			// representation in the string.
1103  *
1104  * This encoding provides a meaningful correlation between the /devices
1105  * path and the devid string where possible.
1106  *
1107  *   Fibre:
1108  *	sbus@6,0/SUNW,socal@d,10000/sf@1,0/ssd@w21000020370bb488,0:c,raw
1109  *	id1,ssd@w20000020370bb488:c,raw
1110  *
1111  *   Copper:
1112  *	sbus@7,0/SUNW,fas@3,8800000/sd@a,0:c
1113  *	id1,sd@SIBM_____1XY210__________:c
1114  */
1115 /* determine if a byte of an id meets ASCII representation requirements */
1116 #define	DEVID_IDBYTE_ISASCII(b)		(				\
1117 	(((b) >= 'a') && ((b) <= 'z')) ||				\
1118 	(((b) >= 'A') && ((b) <= 'Z')) ||				\
1119 	(((b) >= '0') && ((b) <= '9')) ||				\
1120 	(b == '+') || (b == '-') || (b == '.') || (b == '=') ||		\
1121 	(b == ' ') || (b == 0x00))
1122 
1123 /* set type to lower case to indicate that the did_id field is ascii */
1124 #define	DEVID_TYPE_SETASCII(c)	(c - 0x20)	/* 'a' -> 'A' */
1125 
1126 /* determine from type if did_id field is binary or ascii */
1127 #define	DEVID_TYPE_ISASCII(c)	(((c) >= 'A') && ((c) <= 'Z'))
1128 
1129 /* convert type field from binary to ascii */
1130 #define	DEVID_TYPE_BINTOASCII(b)	(				\
1131 	((b) == DEVID_SCSI3_WWN)	? 'w' :				\
1132 	((b) == DEVID_SCSI3_VPD_T10)	? 't' :				\
1133 	((b) == DEVID_SCSI3_VPD_EUI)	? 'x' :				\
1134 	((b) == DEVID_SCSI3_VPD_NAA)	? 'n' :				\
1135 	((b) == DEVID_SCSI_SERIAL)	? 's' :				\
1136 	((b) == DEVID_FAB)		? 'f' :				\
1137 	((b) == DEVID_ENCAP)		? 'e' :				\
1138 	((b) == DEVID_ATA_SERIAL)	? 'a' :				\
1139 	'u')						/* unknown */
1140 
1141 /* convert type field from ascii to binary */
1142 #define	DEVID_TYPE_ASCIITOBIN(c)	(				\
1143 	(((c) == 'w') || ((c) == 'W'))	? DEVID_SCSI3_WWN :		\
1144 	(((c) == 't') || ((c) == 'T'))	? DEVID_SCSI3_VPD_T10 :		\
1145 	(((c) == 'x') || ((c) == 'X'))	? DEVID_SCSI3_VPD_EUI :		\
1146 	(((c) == 'n') || ((c) == 'N'))	? DEVID_SCSI3_VPD_NAA :		\
1147 	(((c) == 's') || ((c) == 'S'))	? DEVID_SCSI_SERIAL :		\
1148 	(((c) == 'f') || ((c) == 'F'))	? DEVID_FAB :			\
1149 	(((c) == 'e') || ((c) == 'E'))	? DEVID_ENCAP :			\
1150 	(((c) == 'a') || ((c) == 'A'))	? DEVID_ATA_SERIAL :		\
1151 	DEVID_MAXTYPE +1)				/* unknown */
1152 
1153 /* determine if the type should be forced to hex encoding (non-ascii) */
1154 #define	DEVID_TYPE_BIN_FORCEHEX(b) (	\
1155 	((b) == DEVID_SCSI3_WWN) ||	\
1156 	((b) == DEVID_SCSI3_VPD_EUI) ||	\
1157 	((b) == DEVID_SCSI3_VPD_NAA) ||	\
1158 	((b) == DEVID_FAB))
1159 
1160 /* determine if the type is from a scsi3 vpd */
1161 #define	IS_DEVID_SCSI3_VPD_TYPE(b) (	\
1162 	((b) == DEVID_SCSI3_VPD_T10) ||	\
1163 	((b) == DEVID_SCSI3_VPD_EUI) ||	\
1164 	((b) == DEVID_SCSI3_VPD_NAA))
1165 
1166 /* convert rev field from binary to ascii (only supports 10 revs) */
1167 #define	DEVID_REV_BINTOASCII(b) (b + '0')
1168 
1169 /* convert rev field from ascii to binary (only supports 10 revs) */
1170 #define	DEVID_REV_ASCIITOBIN(c) (c - '0')
1171 
1172 /* name of devid property */
1173 #define	DEVID_PROP_NAME	"devid"
1174 
1175 /*
1176  * prop_name used by pci_{save,restore}_config_regs()
1177  */
1178 #define	SAVED_CONFIG_REGS "pci-config-regs"
1179 #define	SAVED_CONFIG_REGS_MASK "pcie-config-regs-mask"
1180 #define	SAVED_CONFIG_REGS_CAPINFO "pci-cap-info"
1181 
1182 typedef struct pci_config_header_state {
1183 	uint16_t	chs_command;
1184 	uint8_t		chs_cache_line_size;
1185 	uint8_t		chs_latency_timer;
1186 	uint8_t		chs_header_type;
1187 	uint8_t		chs_sec_latency_timer;
1188 	uint8_t		chs_bridge_control;
1189 	uint32_t	chs_base0;
1190 	uint32_t	chs_base1;
1191 	uint32_t	chs_base2;
1192 	uint32_t	chs_base3;
1193 	uint32_t	chs_base4;
1194 	uint32_t	chs_base5;
1195 } pci_config_header_state_t;
1196 
1197 #ifdef _KERNEL
1198 
1199 typedef struct pci_cap_save_desc {
1200 	uint16_t	cap_offset;
1201 	uint16_t	cap_id;
1202 	uint32_t	cap_nregs;
1203 } pci_cap_save_desc_t;
1204 
1205 typedef struct pci_cap_entry {
1206 	uint16_t		cap_id;
1207 	uint16_t		cap_reg;
1208 	uint16_t		cap_mask;
1209 	uint32_t		cap_ndwords;
1210 	uint32_t (*cap_save_func)(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
1211 	    uint32_t *regbuf, uint32_t ndwords);
1212 } pci_cap_entry_t;
1213 
1214 #endif /* _KERNEL */
1215 
1216 #ifdef	__cplusplus
1217 }
1218 #endif
1219 
1220 #endif	/* _SYS_DDI_IMPLDEFS_H */
1221