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