xref: /titanic_51/usr/src/uts/common/os/ddifm.c (revision 63e9dad6fd7aa76bcbab14646b8f095642f8a2ae)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Fault Management for Device Drivers
30  *
31  * Device drivers wishing to participate in fault management may do so by
32  * first initializing their fault management state and capabilties via
33  * ddi_fm_init(). If the system supports the requested FM capabilities,
34  * the IO framework will intialize FM state and return a bit mask of the
35  * requested capabilities.
36  *
37  * If the system does not support the requested FM capabilities,
38  * the device driver must behave in accordance with the programming semantics
39  * defined below for the capabilities returned from ddi_fm_init().
40  * ddi_fm_init() must be called at attach(9E) time and ddi_fm_fini() must be
41  * called from detach(9E) to perform FM clean-up.
42  *
43  * Driver Fault Management Capabilities
44  *
45  * DDI_FM_NOT_CAPABLE
46  *
47  *	This is the default fault management capability for drivers.  Drivers
48  *	that implement no fault management capabilites or do not participate
49  *	in fault management activities have their FM capability bitmask set
50  *	to 0.
51  *
52  * DDI_FM_EREPORT_CAPABLE
53  *
54  *	When this capability bit is set, drivers are expected to generate error
55  *	report events via ddi_ereport_post() for the associated faults
56  *	that are diagnosed by the IO fault manager DE.  ddi_ereport_post()
57  *	may be called in any context subject to the constraints specified
58  *	by the interrupt iblock cookie	returned during initialization.
59  *
60  *	Error reports resulting from hardware component specific and common IO
61  *	fault and driver defects must be accompanied by an Eversholt fault
62  *	tree (.eft) by the Solaris fault manager (fmd(1M)) for
63  *	diagnosis.
64  *
65  * DDI_FM_ERRCB_CAPABLE
66  *
67  *	Device drivers are expected to implement and register an error
68  *	handler callback function.  ddi_fm_handler_register() and
69  *	ddi_fm_handler_unregister() must be
70  *	called in passive kernel context, typically during an attach(9E)
71  *	or detach(9E) operation.  When called by the FM IO framework,
72  *	the callback function should check for error conditions for the
73  *	hardware and software under its control.  All detected errors
74  *	should have ereport events generated for them.
75  *
76  *	Upon completion of the error handler callback, the driver should
77  *	return one of the following values:
78  *
79  *	#define DDI_FM_OK - no error was detected
80  *	#define DDI_FM_FATAL - a fatal error was detected
81  *	#define DDI_FM_NONFATAL - a non-fatal error was detected
82  *	#define DDI_FM_UNKNOWN - the error status is unknown
83  *
84  *	To insure single threaded access to error handling callbacks,
85  *	the device driver may use i_ddi_fm_handler_enter() and
86  *	i_ddi_fm_handler_exit() when entering and exiting the callback.
87  *
88  * DDI_FM_ACCCHK_CAPABLE/DDI_FM_DMACHK_CAPABLE
89  *
90  *	Device drivers are expected to set-up access and DMA handles
91  *	with FM-specific attributes designed to allow nexus parent
92  *	drivers to flag any errors seen during subsequent IO transactions.
93  *	Drivers must set the devacc_attr_acc_flag member of their
94  *	ddi_device_acc_attr_t structures to DDI_FLAGERR_ACC or DDI_CAUTIOUS_ACC.
95  *	For DMA transactions, driver must set the dma_attr_flags of
96  *	their ddi_dma_attr_t structures to DDI_DMA_FLAGERR.
97  *
98  *	Upon completion of an IO transaction, device drivers are expected
99  *	to check the status of host-side hardware access and device-side
100  *	dma completions by calling ddi_acc_err_check() or ddi_dma_err_check()
101  *	respectively. If the handle is associated with an error detected by
102  *	the nexus parent or FM IO framework, ddi_fm_error_t data (status, ena
103  *	and error expectation) is returned.  If status of DDI_FM_NONFATAL or
104  *	DDI_FM_FATAL is returned, the ena is valid and the expectation flag
105  *	will be set to 1 if the error was unexpected (i.e. not the result
106  *	of a peek or poke type operation).
107  *
108  *	ddi_acc_err_check() and ddi_dma_err_check() may be called in any
109  *	context	subject to the constraints specified by the interrupt
110  *	iblock cookie returned during initialization.
111  *
112  *	Device drivers should generate an access (DDI_FM_IO_ACC) or dma
113  *	(DDI_FM_IO_DMA) data path error report if DDI_FM_NONFATAL or
114  *	DDI_FM_FATAL is returned.
115  *
116  */
117 
118 #include <sys/types.h>
119 #include <sys/sunddi.h>
120 #include <sys/sunndi.h>
121 #include <sys/kmem.h>
122 #include <sys/nvpair.h>
123 #include <sys/fm/protocol.h>
124 #include <sys/ndifm.h>
125 #include <sys/ddifm.h>
126 #include <sys/ddi_impldefs.h>
127 #include <sys/ddi_isa.h>
128 #include <sys/spl.h>
129 #include <sys/varargs.h>
130 #include <sys/systm.h>
131 #include <sys/disp.h>
132 #include <sys/atomic.h>
133 #include <sys/errorq_impl.h>
134 #include <sys/kobj.h>
135 #include <sys/fm/util.h>
136 #include <sys/fm/io/ddi.h>
137 
138 #define	ERPT_CLASS_SZ	sizeof (DDI_IO_CLASS) + sizeof (FM_EREPORT_CLASS) + \
139 			    DDI_MAX_ERPT_CLASS + 2
140 /* Globals */
141 int default_dmacache_sz = DEFAULT_DMACACHE_SZ;
142 int default_acccache_sz = DEFAULT_ACCCACHE_SZ;
143 int ddi_system_fmcap = 0;
144 
145 static struct i_ddi_fmkstat ddifm_kstat_template = {
146 	{"erpt_dropped", KSTAT_DATA_UINT64 },
147 	{"fm_cache_full", KSTAT_DATA_UINT64 },
148 	{"fm_cache_grew", KSTAT_DATA_UINT64 },
149 	{"acc_err", KSTAT_DATA_UINT64 },
150 	{"dma_err", KSTAT_DATA_UINT64 }
151 };
152 
153 /*
154  * Update the service state following the detection of an
155  * error.
156  */
157 void
158 ddi_fm_service_impact(dev_info_t *dip, int svc_impact)
159 {
160 	uint64_t ena;
161 	char buf[FM_MAX_CLASS];
162 
163 	ena = fm_ena_generate(0, FM_ENA_FMT1);
164 	mutex_enter(&(DEVI(dip)->devi_lock));
165 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
166 		switch (svc_impact) {
167 		case DDI_SERVICE_LOST:
168 			DEVI_SET_DEVICE_DOWN(dip);
169 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
170 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_LOST);
171 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
172 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
173 			    NULL);
174 			break;
175 		case DDI_SERVICE_DEGRADED:
176 			DEVI_SET_DEVICE_DEGRADED(dip);
177 			if (DEVI_IS_DEVICE_DEGRADED(dip)) {
178 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
179 				    DDI_FM_SERVICE_IMPACT,
180 				    DDI_FM_SERVICE_DEGRADED);
181 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
182 				    FM_VERSION, DATA_TYPE_UINT8,
183 				    FM_EREPORT_VERS0, NULL);
184 			} else if (DEVI_IS_DEVICE_DOWN(dip)) {
185 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
186 				    DDI_FM_SERVICE_IMPACT,
187 				    DDI_FM_SERVICE_LOST);
188 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
189 				    FM_VERSION, DATA_TYPE_UINT8,
190 				    FM_EREPORT_VERS0, NULL);
191 			}
192 			break;
193 		case DDI_SERVICE_RESTORED:
194 			DEVI_SET_DEVICE_UP(dip);
195 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
196 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_RESTORED);
197 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
198 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
199 			    NULL);
200 			break;
201 		case DDI_SERVICE_UNAFFECTED:
202 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
203 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_UNAFFECTED);
204 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
205 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
206 			    NULL);
207 			break;
208 		default:
209 			break;
210 		}
211 	}
212 	mutex_exit(&(DEVI(dip)->devi_lock));
213 }
214 
215 void
216 i_ddi_drv_ereport_post(dev_info_t *dip, const char *error_class,
217     nvlist_t *errp, int sflag)
218 {
219 	int i;
220 	int depth;
221 	char classp[DDI_DVR_MAX_CLASS];
222 	caddr_t stkp;
223 	char *buf;
224 	char **stkpp;
225 	char *sym;
226 	pc_t stack[DDI_FM_STKDEPTH];
227 	ulong_t off;
228 	dev_info_t *root_dip = ddi_root_node();
229 
230 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(root_dip)))
231 		return;
232 
233 	(void) snprintf(classp, DDI_DVR_MAX_CLASS, "%s%s", DVR_ERPT,
234 	    error_class);
235 
236 	if (sflag == DDI_SLEEP) {
237 		depth = getpcstack(stack, DDI_FM_STKDEPTH);
238 
239 		/* Allocate array of char * for nvlist payload */
240 		stkpp = (char **)kmem_alloc(depth * sizeof (char *), KM_SLEEP);
241 
242 		/*
243 		 * Allocate temporary 64-bit aligned buffer for stack
244 		 * symbol strings
245 		 */
246 		buf = kmem_alloc(depth * DDI_FM_SYM_SZ, KM_SLEEP);
247 
248 		stkp = buf;
249 		for (i = 0; i < depth; ++i) {
250 			sym = kobj_getsymname(stack[i], &off);
251 			(void) snprintf(stkp, DDI_FM_SYM_SZ,
252 			    "\t%s+%lx\n", sym ? sym : "?", off);
253 			stkpp[i] = stkp;
254 			stkp += DDI_FM_SYM_SZ;
255 		}
256 
257 		if (errp)
258 			ddi_fm_ereport_post(root_dip,
259 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
260 			    FM_VERSION, DATA_TYPE_UINT8, 0,
261 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
262 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
263 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
264 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
265 		else
266 			ddi_fm_ereport_post(root_dip,
267 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
268 			    FM_VERSION, DATA_TYPE_UINT8, 0,
269 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
270 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
271 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
272 			    NULL);
273 
274 		kmem_free(stkpp, depth * sizeof (char *));
275 		kmem_free(buf, depth * DDI_FM_SYM_SZ);
276 
277 	} else {
278 		if (errp)
279 			ddi_fm_ereport_post(root_dip,
280 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
281 			    FM_VERSION, DATA_TYPE_UINT8, 0,
282 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
283 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
284 		else
285 			ddi_fm_ereport_post(root_dip,
286 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
287 			    FM_VERSION, DATA_TYPE_UINT8, 0,
288 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
289 			    NULL);
290 	}
291 }
292 
293 /*
294  * fm_dev_ereport_postv: Common consolidation private interface to
295  * post a device tree oriented dev_scheme ereport. The device tree is
296  * composed of the following entities: devinfo nodes, minor nodes, and
297  * pathinfo nodes. All entities are associated with some devinfo node,
298  * either directly or indirectly. The intended devinfo node association
299  * for the ereport is communicated by the 'dip' argument. A minor node,
300  * an entity below 'dip', is represented by a non-null 'minor_name'
301  * argument. An application specific caller, like scsi_fm_ereport_post,
302  * can override the devinfo path with a pathinfo path via a non-null
303  * 'devpath' argument - in this case 'dip' is the MPXIO client node and
304  * devpath should be the path through the pHCI devinfo node to the
305  * pathinfo node.
306  *
307  * This interface also allows the caller to decide if the error being
308  * reported is know to be associated with a specific device identity
309  * via the 'devid' argument. The caller needs to control wether the
310  * devid appears as an authority in the FMRI because for some types of
311  * errors, like transport errors, the identity of the device on the
312  * other end of the transport is not guaranteed to be the current
313  * identity of the dip. For transport errors the caller should specify
314  * a NULL devid, even when there is a valid devid associated with the dip.
315  *
316  * The ddi_fm_ereport_post() implementation calls this interface with
317  * just a dip: devpath, minor_name, and devid are all NULL. The
318  * scsi_fm_ereport_post() implementation may call this interface with
319  * non-null devpath, minor_name, and devid arguments depending on
320  * wether MPXIO is enabled, and wether a transport or non-transport
321  * error is being posted.
322  */
323 void
324 fm_dev_ereport_postv(dev_info_t *dip, dev_info_t *eqdip,
325     const char *devpath, const char *minor_name, const char *devid,
326     const char *error_class, uint64_t ena, int sflag, va_list ap)
327 {
328 	struct i_ddi_fmhdl	*fmhdl;
329 	errorq_elem_t		*eqep;
330 	nv_alloc_t		*nva;
331 	nvlist_t		*ereport = NULL;
332 	nvlist_t		*detector = NULL;
333 	char			*name;
334 	data_type_t		type;
335 	uint8_t			version;
336 	char			class[ERPT_CLASS_SZ];
337 	char			path[MAXPATHLEN];
338 
339 	ASSERT(dip && eqdip && error_class);
340 
341 	/*
342 	 * This interface should be called with a fm_capable eqdip. The
343 	 * ddi_fm_ereport_post* interfaces call with eqdip == dip,
344 	 * ndi_fm_ereport_post* interfaces call with eqdip == ddi_parent(dip).
345 	 */
346 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(eqdip)))
347 		goto err;
348 
349 	/* get ereport nvlist handle */
350 	if ((sflag == DDI_SLEEP) && !panicstr) {
351 		/*
352 		 * Driver defect - should not call with DDI_SLEEP while in
353 		 * interrupt context.
354 		 */
355 		if (servicing_interrupt()) {
356 			i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, sflag);
357 			goto err;
358 		}
359 
360 		/* Use normal interfaces to allocate memory. */
361 		if ((ereport = fm_nvlist_create(NULL)) == NULL)
362 			goto err;
363 		nva = NULL;
364 	} else {
365 		/* Use errorq interfaces to avoid memory allocation. */
366 		fmhdl = DEVI(eqdip)->devi_fmhdl;
367 		ASSERT(fmhdl);
368 		eqep = errorq_reserve(fmhdl->fh_errorq);
369 		if (eqep == NULL)
370 			goto err;
371 
372 		ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
373 		nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
374 		ASSERT(nva);
375 	}
376 	ASSERT(ereport);
377 
378 	/*
379 	 * Form parts of an ereport:
380 	 *	A: version
381 	 * 	B: error_class
382 	 *	C: ena
383 	 *	D: detector	(path and optional devid authority)
384 	 *	E: payload
385 	 *
386 	 * A: ereport version: first payload tuple must be the version.
387 	 */
388 	name = va_arg(ap, char *);
389 	type = va_arg(ap, data_type_t);
390 	version = va_arg(ap, uint_t);
391 	if ((strcmp(name, FM_VERSION) != 0) || (type != DATA_TYPE_UINT8)) {
392 		i_ddi_drv_ereport_post(dip, DVR_EVER, NULL, sflag);
393 		goto err;
394 	}
395 
396 	/* B: ereport error_class: add "io." prefix to class. */
397 	(void) snprintf(class, ERPT_CLASS_SZ, "%s.%s",
398 	    DDI_IO_CLASS, error_class);
399 
400 	/* C: ereport ena: if not passed in, generate new ena. */
401 	if (ena == 0)
402 		ena = fm_ena_generate(0, FM_ENA_FMT1);
403 
404 	/* D: detector: form dev scheme fmri with path and devid. */
405 	if (devpath) {
406 		(void) strlcpy(path, devpath, sizeof (path));
407 	} else {
408 		/* derive devpath from dip */
409 		if (dip == ddi_root_node())
410 			(void) strcpy(path, "/");
411 		else
412 			(void) ddi_pathname(dip, path);
413 	}
414 	if (minor_name) {
415 		(void) strlcat(path, ":", sizeof (path));
416 		(void) strlcat(path, minor_name, sizeof (path));
417 	}
418 	detector = fm_nvlist_create(nva);
419 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, path, devid);
420 
421 	/* Pull parts of ereport together into ereport. */
422 	fm_ereport_set(ereport, version, class, ena, detector, NULL);
423 
424 	/* Add the payload to ereport. */
425 	name = va_arg(ap, char *);
426 	(void) i_fm_payload_set(ereport, name, ap);
427 
428 	/* Post the ereport. */
429 	if (nva)
430 		errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
431 	else
432 		fm_ereport_post(ereport, EVCH_SLEEP);
433 	goto out;
434 
435 	/* Count errors as drops. */
436 err:	if (fmhdl)
437 		atomic_add_64(&fmhdl->fh_kstat.fek_erpt_dropped.value.ui64, 1);
438 
439 out:	if (ereport && (nva == NULL))
440 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
441 	if (detector && (nva == NULL))
442 		fm_nvlist_destroy(detector, FM_NVA_FREE);
443 }
444 
445 /*
446  * Generate an error report for consumption by the Solaris Fault Manager,
447  * fmd(1M).  Valid ereport classes are defined in /usr/include/sys/fm/io.
448  *
449  * The ENA should be set if this error is a result of an error status
450  * returned from ddi_dma_err_check() or ddi_acc_err_check().  Otherwise,
451  * an ENA value of 0 is appropriate.
452  *
453  * If sflag == DDI_NOSLEEP, ddi_fm_ereport_post () may be called
454  * from user, kernel, interrupt or high-interrupt context.  Otherwise,
455  * ddi_fm_ereport_post() must be called from user or kernel context.
456  *
457  * The ndi_interfaces are provided for use by nexus drivers to post
458  * ereports about children who may not themselves be fm_capable.
459  *
460  * All interfaces end up in the common fm_dev_ereport_postv code above.
461  */
462 void
463 ddi_fm_ereport_post(dev_info_t *dip,
464     const char *error_class, uint64_t ena, int sflag, ...)
465 {
466 	va_list ap;
467 
468 	ASSERT(dip && error_class);
469 	va_start(ap, sflag);
470 	fm_dev_ereport_postv(dip, dip, NULL, NULL, NULL,
471 	    error_class, ena, sflag, ap);
472 	va_end(ap);
473 }
474 
475 void
476 ndi_fm_ereport_post(dev_info_t *dip,
477     const char *error_class, uint64_t ena, int sflag, ...)
478 {
479 	va_list ap;
480 
481 	ASSERT(dip && error_class && (sflag == DDI_SLEEP));
482 	va_start(ap, sflag);
483 	fm_dev_ereport_postv(dip, ddi_get_parent(dip), NULL, NULL, NULL,
484 	    error_class, ena, sflag, ap);
485 	va_end(ap);
486 }
487 
488 /*
489  * Driver error handling entry.  Prevents multiple simultaneous calls into
490  * driver error handling callback.
491  *
492  * May be called from a context consistent with the iblock_cookie returned
493  * in ddi_fm_init().
494  */
495 void
496 i_ddi_fm_handler_enter(dev_info_t *dip)
497 {
498 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
499 
500 	mutex_enter(&hdl->fh_lock);
501 	hdl->fh_lock_owner = curthread;
502 }
503 
504 /*
505  * Driver error handling exit.
506  *
507  * May be called from a context consistent with the iblock_cookie returned
508  * in ddi_fm_init().
509  */
510 void
511 i_ddi_fm_handler_exit(dev_info_t *dip)
512 {
513 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
514 
515 	hdl->fh_lock_owner = NULL;
516 	mutex_exit(&hdl->fh_lock);
517 }
518 
519 boolean_t
520 i_ddi_fm_handler_owned(dev_info_t *dip)
521 {
522 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
523 
524 	return (hdl->fh_lock_owner == curthread);
525 }
526 
527 /*
528  * Register a fault manager error handler for this device instance
529  *
530  * This function must be called from a driver's attach(9E) routine.
531  */
532 void
533 ddi_fm_handler_register(dev_info_t *dip, ddi_err_func_t handler,
534     void *impl_data)
535 {
536 	dev_info_t *pdip;
537 	struct i_ddi_fmhdl *pfmhdl;
538 	struct i_ddi_errhdl *new_eh;
539 	struct i_ddi_fmtgt *tgt;
540 
541 	/*
542 	 * Check for proper calling context.
543 	 * The DDI configuration framework does not support
544 	 * DR states to allow checking for proper invocation
545 	 * from a DDI_ATTACH or DDI_RESUME.  This limits context checking
546 	 * to interrupt only.
547 	 */
548 	if (servicing_interrupt()) {
549 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
550 		return;
551 	}
552 
553 	if (dip == ddi_root_node())
554 		pdip = dip;
555 	else
556 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
557 
558 	ASSERT(pdip);
559 
560 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
561 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
562 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
563 		return;
564 	}
565 
566 	new_eh = kmem_zalloc(sizeof (struct i_ddi_errhdl), KM_SLEEP);
567 	new_eh->eh_func = handler;
568 	new_eh->eh_impl = impl_data;
569 
570 	/* Add dip to parent's target list of registered error handlers */
571 	tgt = kmem_alloc(sizeof (struct i_ddi_fmtgt), KM_SLEEP);
572 	tgt->ft_dip = dip;
573 	tgt->ft_errhdl = new_eh;
574 
575 	i_ddi_fm_handler_enter(pdip);
576 	pfmhdl = DEVI(pdip)->devi_fmhdl;
577 	ASSERT(pfmhdl);
578 	tgt->ft_next = pfmhdl->fh_tgts;
579 	pfmhdl->fh_tgts = tgt;
580 	i_ddi_fm_handler_exit(pdip);
581 }
582 
583 /*
584  * Unregister a fault manager error handler for this device instance
585  *
586  * This function must be called from a drivers attach(9E) or detach(9E)
587  * routine.
588  */
589 void
590 ddi_fm_handler_unregister(dev_info_t *dip)
591 {
592 	dev_info_t *pdip;
593 	struct i_ddi_fmhdl *pfmhdl;
594 	struct i_ddi_fmtgt *tgt, **ptgt;
595 
596 	/*
597 	 * Check for proper calling context.
598 	 * The DDI configuration framework does not support
599 	 * DR states to allow checking for proper invocation
600 	 * from a DDI_DETACH or DDI_SUSPEND.  This limits context checking
601 	 * to interrupt only.
602 	 */
603 	if (servicing_interrupt()) {
604 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
605 		return;
606 	}
607 
608 	if (dip == ddi_root_node())
609 		pdip = dip;
610 	else
611 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
612 
613 	ASSERT(pdip);
614 
615 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
616 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
617 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
618 		return;
619 	}
620 
621 	i_ddi_fm_handler_enter(pdip);
622 	pfmhdl = DEVI(pdip)->devi_fmhdl;
623 	ASSERT(pfmhdl);
624 	ptgt = &pfmhdl->fh_tgts;
625 	for (tgt = pfmhdl->fh_tgts; tgt != NULL; tgt = tgt->ft_next) {
626 		if (dip == tgt->ft_dip) {
627 			*ptgt = tgt->ft_next;
628 			kmem_free(tgt->ft_errhdl, sizeof (struct i_ddi_errhdl));
629 			kmem_free(tgt, sizeof (struct i_ddi_fmtgt));
630 			break;
631 		}
632 		ptgt = &tgt->ft_next;
633 	}
634 	i_ddi_fm_handler_exit(pdip);
635 
636 
637 }
638 
639 /*
640  * Initialize Fault Management capabilities for this device instance (dip).
641  * When called with the following capabilities, data structures neccessary
642  * for fault management activities are allocated and initialized.
643  *
644  *	DDI_FM_EREPORT_CAPABLE - initialize ereport errorq and ereport
645  *				capable driver property.
646  *
647  *	DDI_FM_ERRCB_CAPABLE - check with parent for ability to register
648  *				an error handler.
649  *
650  *	DDI_FM_ACCCHK_CAPABLE - initialize access handle cache and acc-chk
651  *				driver property
652  *
653  *	DDI_FM_DMACHK_CAPABLE - initialize dma handle cache and dma-chk
654  *				driver property
655  *
656  * A driver's FM capability level may not exceed that of its parent or
657  * system-wide FM capability.  The available capability level for this
658  * device instance is returned in *fmcap.
659  *
660  * This function must be called from a driver's attach(9E) entry point.
661  */
662 void
663 ddi_fm_init(dev_info_t *dip, int *fmcap, ddi_iblock_cookie_t *ibcp)
664 {
665 	struct dev_info *devi = DEVI(dip);
666 	struct i_ddi_fmhdl *fmhdl;
667 	ddi_iblock_cookie_t ibc;
668 	int pcap, newcap = DDI_FM_NOT_CAPABLE;
669 
670 	if (!DEVI_IS_ATTACHING(dip)) {
671 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
672 		*fmcap = DDI_FM_NOT_CAPABLE;
673 		return;
674 	}
675 
676 	if (DDI_FM_DEFAULT_CAP(*fmcap))
677 		return;
678 
679 	/*
680 	 * Check parent for supported FM level
681 	 * and correct error handling PIL
682 	 */
683 	if (dip != ddi_root_node()) {
684 
685 		/*
686 		 * Initialize the default ibc.  The parent may change it
687 		 * depending upon its capabilities.
688 		 */
689 		ibc = (ddi_iblock_cookie_t)ipltospl(FM_ERR_PIL);
690 
691 		pcap = i_ndi_busop_fm_init(dip, *fmcap, &ibc);
692 	} else {
693 		pcap = *fmcap;
694 		ibc = *ibcp;
695 	}
696 
697 	/* Initialize the per-device instance FM handle */
698 	fmhdl = kmem_zalloc(sizeof (struct i_ddi_fmhdl), KM_SLEEP);
699 
700 	if ((fmhdl->fh_ksp = kstat_create((char *)ddi_driver_name(dip),
701 	    ddi_get_instance(dip), "fm", "misc",
702 	    KSTAT_TYPE_NAMED, sizeof (struct i_ddi_fmkstat) /
703 	    sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) == NULL) {
704 		mutex_destroy(&fmhdl->fh_lock);
705 		kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
706 		*fmcap = DDI_FM_NOT_CAPABLE;
707 		return;
708 	}
709 
710 	bcopy(&ddifm_kstat_template, &fmhdl->fh_kstat,
711 	    sizeof (struct i_ddi_fmkstat));
712 	fmhdl->fh_ksp->ks_data = &fmhdl->fh_kstat;
713 	fmhdl->fh_ksp->ks_private = fmhdl;
714 	kstat_install(fmhdl->fh_ksp);
715 
716 	fmhdl->fh_dma_cache = NULL;
717 	fmhdl->fh_acc_cache = NULL;
718 	fmhdl->fh_tgts = NULL;
719 	fmhdl->fh_dip = dip;
720 	fmhdl->fh_ibc = ibc;
721 	mutex_init(&fmhdl->fh_lock, NULL, MUTEX_DRIVER, fmhdl->fh_ibc);
722 	devi->devi_fmhdl = fmhdl;
723 
724 	/*
725 	 * Initialize support for ereport generation
726 	 */
727 	if (DDI_FM_EREPORT_CAP(*fmcap) && DDI_FM_EREPORT_CAP(pcap)) {
728 		fmhdl->fh_errorq = ereport_errorq;
729 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
730 		    "fm-ereport-capable", 0) == 0)
731 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
732 			    DDI_PROP_CANSLEEP, "fm-ereport-capable", NULL, 0);
733 
734 		newcap |= DDI_FM_EREPORT_CAPABLE;
735 	}
736 
737 	/*
738 	 * Need cooperation of the parent for error handling
739 	 */
740 
741 	if (DDI_FM_ERRCB_CAP(*fmcap) && DDI_FM_ERRCB_CAP(pcap)) {
742 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
743 		    "fm-errcb-capable", 0) == 0)
744 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
745 			    DDI_PROP_CANSLEEP, "fm-errcb-capable", NULL, 0);
746 
747 		newcap |= DDI_FM_ERRCB_CAPABLE;
748 	}
749 
750 	/*
751 	 * Support for DMA and Access error handling
752 	 */
753 
754 	if (DDI_FM_DMA_ERR_CAP(*fmcap) && DDI_FM_DMA_ERR_CAP(pcap)) {
755 		i_ndi_fmc_create(&fmhdl->fh_dma_cache, 2, ibc);
756 
757 		/* Set-up dma chk capability prop */
758 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
759 		    "fm-dmachk-capable", 0) == 0)
760 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
761 			    DDI_PROP_CANSLEEP, "fm-dmachk-capable", NULL, 0);
762 
763 		newcap |= DDI_FM_DMACHK_CAPABLE;
764 	}
765 
766 	if (DDI_FM_ACC_ERR_CAP(*fmcap) && DDI_FM_ACC_ERR_CAP(pcap)) {
767 		i_ndi_fmc_create(&fmhdl->fh_acc_cache, 2, ibc);
768 		/* Set-up dma chk capability prop */
769 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
770 		    "fm-accchk-capable", 0) == 0)
771 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
772 			    DDI_PROP_CANSLEEP, "fm-accchk-capable", NULL, 0);
773 
774 		newcap |= DDI_FM_ACCCHK_CAPABLE;
775 	}
776 
777 	/*
778 	 * Return the capability support available
779 	 * to this driver instance
780 	 */
781 	fmhdl->fh_cap = newcap;
782 	*fmcap = newcap;
783 
784 	if (ibcp != NULL)
785 		*ibcp = ibc;
786 }
787 
788 /*
789  * Finalize Fault Management activities for this device instance.
790  * Outstanding IO transaction must be completed prior to calling
791  * this routine.  All previously allocated resources and error handler
792  * registration are cleared and deallocated.
793  *
794  * This function must be called from a driver's detach(9E) entry point.
795  */
796 void
797 ddi_fm_fini(dev_info_t *dip)
798 {
799 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
800 
801 	ASSERT(fmhdl);
802 
803 	if (!(DEVI_IS_DETACHING(dip) || DEVI_IS_ATTACHING(dip))) {
804 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
805 		return;
806 	}
807 
808 	kstat_delete(fmhdl->fh_ksp);
809 
810 	if (DDI_FM_EREPORT_CAP(fmhdl->fh_cap)) {
811 		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
812 		    "fm-ereport-capable");
813 	}
814 
815 	if (dip != ddi_root_node()) {
816 		if (DDI_FM_ERRCB_CAP(fmhdl->fh_cap)) {
817 			ddi_fm_handler_unregister(dip);
818 			(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
819 			    "fm-errcb-capable");
820 		}
821 
822 		if (DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap) ||
823 		    DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
824 			if (fmhdl->fh_dma_cache != NULL) {
825 				i_ndi_fmc_destroy(fmhdl->fh_dma_cache);
826 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
827 				    "fm-dmachk-capable");
828 			}
829 			if (fmhdl->fh_acc_cache != NULL) {
830 				i_ndi_fmc_destroy(fmhdl->fh_acc_cache);
831 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
832 				    "fm-accachk-capable");
833 			}
834 		}
835 
836 		i_ndi_busop_fm_fini(dip);
837 	}
838 
839 	kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
840 	DEVI(dip)->devi_fmhdl = NULL;
841 }
842 
843 /*
844  * Return the fault management capability level for this device instance.
845  *
846  * This function may be called from user, kernel, or interrupt context.
847  */
848 int
849 ddi_fm_capable(dev_info_t *dip)
850 {
851 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
852 
853 	if (fmhdl == NULL)
854 		return (DDI_FM_NOT_CAPABLE);
855 
856 	return (fmhdl->fh_cap);
857 }
858 
859 /*
860  * Routines to set and get error information for/from an access or dma handle
861  *
862  * These routines may be called from user, kernel, and interrupt contexts.
863  */
864 void
865 ddi_fm_acc_err_get(ddi_acc_handle_t handle, ddi_fm_error_t *de, int version)
866 {
867 	ndi_err_t *errp;
868 
869 	if (handle == NULL)
870 		return;
871 
872 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
873 		ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
874 
875 		i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
876 		cmn_err(CE_PANIC, "ddi_fm_acc_err_get: "
877 		    "Invalid driver version\n");
878 	}
879 
880 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
881 	de->fme_status = errp->err_status;
882 	de->fme_ena = errp->err_ena;
883 	de->fme_flag = errp->err_expected;
884 	de->fme_acc_handle = handle;
885 }
886 
887 void
888 ddi_fm_dma_err_get(ddi_dma_handle_t handle, ddi_fm_error_t *de, int version)
889 {
890 	ndi_err_t *errp;
891 
892 	if (handle == NULL)
893 		return;
894 
895 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
896 		i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
897 		    DVR_EVER, NULL, DDI_NOSLEEP);
898 		cmn_err(CE_PANIC, "ddi_fm_dma_err_get: "
899 		    "Invalid driver version\n");
900 	}
901 
902 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
903 
904 	de->fme_status = errp->err_status;
905 	de->fme_ena = errp->err_ena;
906 	de->fme_flag = errp->err_expected;
907 	de->fme_dma_handle = handle;
908 }
909 
910 void
911 ddi_fm_acc_err_clear(ddi_acc_handle_t handle, int version)
912 {
913 	ndi_err_t *errp;
914 
915 	if (handle == NULL)
916 		return;
917 
918 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
919 		ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
920 
921 		i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
922 		cmn_err(CE_PANIC, "ddi_fm_acc_err_clear: "
923 		    "Invalid driver version\n");
924 	}
925 
926 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
927 	errp->err_status = DDI_FM_OK;
928 	errp->err_ena = 0;
929 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
930 }
931 
932 void
933 ddi_fm_dma_err_clear(ddi_dma_handle_t handle, int version)
934 {
935 	ndi_err_t *errp;
936 
937 	if (handle == NULL)
938 		return;
939 
940 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
941 		i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
942 		    DVR_EVER, NULL, DDI_NOSLEEP);
943 		cmn_err(CE_PANIC, "ddi_fm_dma_err_clear: "
944 		    "Invalid driver version\n");
945 	}
946 
947 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
948 
949 	errp->err_status = DDI_FM_OK;
950 	errp->err_ena = 0;
951 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
952 }
953 
954 void
955 i_ddi_fm_acc_err_set(ddi_acc_handle_t handle, uint64_t ena, int status,
956     int flag)
957 {
958 	ddi_acc_hdl_t *hdlp = impl_acc_hdl_get(handle);
959 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
960 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->ah_dip)->devi_fmhdl;
961 
962 	i_hdlp->ahi_err->err_ena = ena;
963 	i_hdlp->ahi_err->err_status = status;
964 	i_hdlp->ahi_err->err_expected = flag;
965 	atomic_add_64(&fmhdl->fh_kstat.fek_acc_err.value.ui64, 1);
966 }
967 
968 void
969 i_ddi_fm_dma_err_set(ddi_dma_handle_t handle, uint64_t ena, int status,
970     int flag)
971 {
972 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
973 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->dmai_rdip)->devi_fmhdl;
974 
975 	hdlp->dmai_error.err_ena = ena;
976 	hdlp->dmai_error.err_status = status;
977 	hdlp->dmai_error.err_expected = flag;
978 	atomic_add_64(&fmhdl->fh_kstat.fek_dma_err.value.ui64, 1);
979 }
980 
981 ddi_fmcompare_t
982 i_ddi_fm_acc_err_cf_get(ddi_acc_handle_t handle)
983 {
984 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
985 
986 	return (i_hdlp->ahi_err->err_cf);
987 }
988 
989 ddi_fmcompare_t
990 i_ddi_fm_dma_err_cf_get(ddi_dma_handle_t handle)
991 {
992 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
993 
994 	return (hdlp->dmai_error.err_cf);
995 }
996