xref: /titanic_53/usr/src/uts/common/os/ddifm.c (revision 0b1ecf758b8478dd5908ae0c523dc922d39b7632)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
500d0963fSdilpreet  * Common Development and Distribution License (the "License").
600d0963fSdilpreet  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22eae2e508Skrishnae  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Fault Management for Device Drivers
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Device drivers wishing to participate in fault management may do so by
327c478bd9Sstevel@tonic-gate  * first initializing their fault management state and capabilties via
337c478bd9Sstevel@tonic-gate  * ddi_fm_init(). If the system supports the requested FM capabilities,
347c478bd9Sstevel@tonic-gate  * the IO framework will intialize FM state and return a bit mask of the
357c478bd9Sstevel@tonic-gate  * requested capabilities.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * If the system does not support the requested FM capabilities,
387c478bd9Sstevel@tonic-gate  * the device driver must behave in accordance with the programming semantics
397c478bd9Sstevel@tonic-gate  * defined below for the capabilities returned from ddi_fm_init().
407c478bd9Sstevel@tonic-gate  * ddi_fm_init() must be called at attach(9E) time and ddi_fm_fini() must be
417c478bd9Sstevel@tonic-gate  * called from detach(9E) to perform FM clean-up.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Driver Fault Management Capabilities
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * DDI_FM_NOT_CAPABLE
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *	This is the default fault management capability for drivers.  Drivers
487c478bd9Sstevel@tonic-gate  *	that implement no fault management capabilites or do not participate
497c478bd9Sstevel@tonic-gate  *	in fault management activities have their FM capability bitmask set
507c478bd9Sstevel@tonic-gate  *	to 0.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * DDI_FM_EREPORT_CAPABLE
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	When this capability bit is set, drivers are expected to generate error
557c478bd9Sstevel@tonic-gate  *	report events via ddi_ereport_post() for the associated faults
567c478bd9Sstevel@tonic-gate  *	that are diagnosed by the IO fault manager DE.  ddi_ereport_post()
577c478bd9Sstevel@tonic-gate  *	may be called in any context subject to the constraints specified
587c478bd9Sstevel@tonic-gate  *	by the interrupt iblock cookie	returned during initialization.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *	Error reports resulting from hardware component specific and common IO
617c478bd9Sstevel@tonic-gate  *	fault and driver defects must be accompanied by an Eversholt fault
627c478bd9Sstevel@tonic-gate  *	tree (.eft) by the Solaris fault manager (fmd(1M)) for
637c478bd9Sstevel@tonic-gate  *	diagnosis.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * DDI_FM_ERRCB_CAPABLE
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *	Device drivers are expected to implement and register an error
687c478bd9Sstevel@tonic-gate  *	handler callback function.  ddi_fm_handler_register() and
697c478bd9Sstevel@tonic-gate  *	ddi_fm_handler_unregister() must be
707c478bd9Sstevel@tonic-gate  *	called in passive kernel context, typically during an attach(9E)
717c478bd9Sstevel@tonic-gate  *	or detach(9E) operation.  When called by the FM IO framework,
727c478bd9Sstevel@tonic-gate  *	the callback function should check for error conditions for the
737c478bd9Sstevel@tonic-gate  *	hardware and software under its control.  All detected errors
747c478bd9Sstevel@tonic-gate  *	should have ereport events generated for them.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  *	Upon completion of the error handler callback, the driver should
777c478bd9Sstevel@tonic-gate  *	return one of the following values:
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  *	#define DDI_FM_OK - no error was detected
807c478bd9Sstevel@tonic-gate  *	#define DDI_FM_FATAL - a fatal error was detected
817c478bd9Sstevel@tonic-gate  *	#define DDI_FM_NONFATAL - a non-fatal error was detected
827c478bd9Sstevel@tonic-gate  *	#define DDI_FM_UNKNOWN - the error status is unknown
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  *	To insure single threaded access to error handling callbacks,
857c478bd9Sstevel@tonic-gate  *	the device driver may use i_ddi_fm_handler_enter() and
867c478bd9Sstevel@tonic-gate  *	i_ddi_fm_handler_exit() when entering and exiting the callback.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * DDI_FM_ACCCHK_CAPABLE/DDI_FM_DMACHK_CAPABLE
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *	Device drivers are expected to set-up access and DMA handles
917c478bd9Sstevel@tonic-gate  *	with FM-specific attributes designed to allow nexus parent
927c478bd9Sstevel@tonic-gate  *	drivers to flag any errors seen during subsequent IO transactions.
937c478bd9Sstevel@tonic-gate  *	Drivers must set the devacc_attr_acc_flag member of their
947c478bd9Sstevel@tonic-gate  *	ddi_device_acc_attr_t structures to DDI_FLAGERR_ACC or DDI_CAUTIOUS_ACC.
957c478bd9Sstevel@tonic-gate  *	For DMA transactions, driver must set the dma_attr_flags of
967c478bd9Sstevel@tonic-gate  *	their ddi_dma_attr_t structures to DDI_DMA_FLAGERR.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *	Upon completion of an IO transaction, device drivers are expected
997c478bd9Sstevel@tonic-gate  *	to check the status of host-side hardware access and device-side
1007c478bd9Sstevel@tonic-gate  *	dma completions by calling ddi_acc_err_check() or ddi_dma_err_check()
1017c478bd9Sstevel@tonic-gate  *	respectively. If the handle is associated with an error detected by
1027c478bd9Sstevel@tonic-gate  *	the nexus parent or FM IO framework, ddi_fm_error_t data (status, ena
1037c478bd9Sstevel@tonic-gate  *	and error expectation) is returned.  If status of DDI_FM_NONFATAL or
1047c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL is returned, the ena is valid and the expectation flag
1057c478bd9Sstevel@tonic-gate  *	will be set to 1 if the error was unexpected (i.e. not the result
1067c478bd9Sstevel@tonic-gate  *	of a peek or poke type operation).
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  *	ddi_acc_err_check() and ddi_dma_err_check() may be called in any
1097c478bd9Sstevel@tonic-gate  *	context	subject to the constraints specified by the interrupt
1107c478bd9Sstevel@tonic-gate  *	iblock cookie returned during initialization.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *	Device drivers should generate an access (DDI_FM_IO_ACC) or dma
1137c478bd9Sstevel@tonic-gate  *	(DDI_FM_IO_DMA) data path error report if DDI_FM_NONFATAL or
1147c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL is returned.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #include <sys/types.h>
1197c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1207c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
1217c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1227c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
1237c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
1247c478bd9Sstevel@tonic-gate #include <sys/ndifm.h>
1257c478bd9Sstevel@tonic-gate #include <sys/ddifm.h>
1267c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
1277c478bd9Sstevel@tonic-gate #include <sys/ddi_isa.h>
1287c478bd9Sstevel@tonic-gate #include <sys/spl.h>
1297c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
1307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1317c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1327c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1337c478bd9Sstevel@tonic-gate #include <sys/errorq_impl.h>
1347c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1357c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
1367c478bd9Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	ERPT_CLASS_SZ	sizeof (DDI_IO_CLASS) + sizeof (FM_EREPORT_CLASS) + \
1397c478bd9Sstevel@tonic-gate 			    DDI_MAX_ERPT_CLASS + 2
1407c478bd9Sstevel@tonic-gate /* Globals */
1417c478bd9Sstevel@tonic-gate int default_dmacache_sz = DEFAULT_DMACACHE_SZ;
1427c478bd9Sstevel@tonic-gate int default_acccache_sz = DEFAULT_ACCCACHE_SZ;
1437c478bd9Sstevel@tonic-gate int ddi_system_fmcap = 0;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static struct i_ddi_fmkstat ddifm_kstat_template = {
1467c478bd9Sstevel@tonic-gate 	{"erpt_dropped", KSTAT_DATA_UINT64 },
1477c478bd9Sstevel@tonic-gate 	{"fm_cache_full", KSTAT_DATA_UINT64 },
1487c478bd9Sstevel@tonic-gate 	{"fm_cache_grew", KSTAT_DATA_UINT64 },
1497c478bd9Sstevel@tonic-gate 	{"acc_err", KSTAT_DATA_UINT64 },
1507c478bd9Sstevel@tonic-gate 	{"dma_err", KSTAT_DATA_UINT64 }
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Update the service state following the detection of an
1557c478bd9Sstevel@tonic-gate  * error.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate void
1587c478bd9Sstevel@tonic-gate ddi_fm_service_impact(dev_info_t *dip, int svc_impact)
1597c478bd9Sstevel@tonic-gate {
16000d0963fSdilpreet 	uint64_t ena;
16100d0963fSdilpreet 	char buf[FM_MAX_CLASS];
16200d0963fSdilpreet 
16300d0963fSdilpreet 	ena = fm_ena_generate(0, FM_ENA_FMT1);
1647c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
1657c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
1667c478bd9Sstevel@tonic-gate 		switch (svc_impact) {
1677c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_LOST:
1687c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DOWN(dip);
16900d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
17000d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_LOST);
17100d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
17200d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
17300d0963fSdilpreet 			    NULL);
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_DEGRADED:
1767c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DEGRADED(dip);
17700d0963fSdilpreet 			if (DEVI_IS_DEVICE_DEGRADED(dip)) {
17800d0963fSdilpreet 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
17900d0963fSdilpreet 				    DDI_FM_SERVICE_IMPACT,
18000d0963fSdilpreet 				    DDI_FM_SERVICE_DEGRADED);
18100d0963fSdilpreet 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
18200d0963fSdilpreet 				    FM_VERSION, DATA_TYPE_UINT8,
18300d0963fSdilpreet 				    FM_EREPORT_VERS0, NULL);
18400d0963fSdilpreet 			} else if (DEVI_IS_DEVICE_DOWN(dip)) {
18500d0963fSdilpreet 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
18600d0963fSdilpreet 				    DDI_FM_SERVICE_IMPACT,
18700d0963fSdilpreet 				    DDI_FM_SERVICE_LOST);
18800d0963fSdilpreet 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
18900d0963fSdilpreet 				    FM_VERSION, DATA_TYPE_UINT8,
19000d0963fSdilpreet 				    FM_EREPORT_VERS0, NULL);
19100d0963fSdilpreet 			}
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_RESTORED:
1947c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_UP(dip);
19500d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
19600d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_RESTORED);
19700d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
19800d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
19900d0963fSdilpreet 			    NULL);
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_UNAFFECTED:
20200d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
20300d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_UNAFFECTED);
20400d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
20500d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
20600d0963fSdilpreet 			    NULL);
20700d0963fSdilpreet 			break;
2087c478bd9Sstevel@tonic-gate 		default:
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate void
2167c478bd9Sstevel@tonic-gate i_ddi_drv_ereport_post(dev_info_t *dip, const char *error_class,
2177c478bd9Sstevel@tonic-gate     nvlist_t *errp, int sflag)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	int i;
2207c478bd9Sstevel@tonic-gate 	int depth;
2217c478bd9Sstevel@tonic-gate 	char classp[DDI_DVR_MAX_CLASS];
2227c478bd9Sstevel@tonic-gate 	caddr_t stkp;
2237c478bd9Sstevel@tonic-gate 	char *buf;
2247c478bd9Sstevel@tonic-gate 	char **stkpp;
2257c478bd9Sstevel@tonic-gate 	char *sym;
2267c478bd9Sstevel@tonic-gate 	pc_t stack[DDI_FM_STKDEPTH];
2277c478bd9Sstevel@tonic-gate 	ulong_t off;
2287c478bd9Sstevel@tonic-gate 	dev_info_t *root_dip = ddi_root_node();
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(root_dip)))
2317c478bd9Sstevel@tonic-gate 		return;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	(void) snprintf(classp, DDI_DVR_MAX_CLASS, "%s%s", DVR_ERPT,
2347c478bd9Sstevel@tonic-gate 	    error_class);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if (sflag == DDI_SLEEP) {
2377c478bd9Sstevel@tonic-gate 		depth = getpcstack(stack, DDI_FM_STKDEPTH);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		/* Allocate array of char * for nvlist payload */
2407c478bd9Sstevel@tonic-gate 		stkpp = (char **)kmem_alloc(depth * sizeof (char *), KM_SLEEP);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * Allocate temporary 64-bit aligned buffer for stack
2447c478bd9Sstevel@tonic-gate 		 * symbol strings
2457c478bd9Sstevel@tonic-gate 		 */
2467c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(depth * DDI_FM_SYM_SZ, KM_SLEEP);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		stkp = buf;
2497c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; ++i) {
2507c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(stack[i], &off);
2517c478bd9Sstevel@tonic-gate 			(void) snprintf(stkp, DDI_FM_SYM_SZ,
2527c478bd9Sstevel@tonic-gate 			    "\t%s+%lx\n", sym ? sym : "?", off);
2537c478bd9Sstevel@tonic-gate 			stkpp[i] = stkp;
2547c478bd9Sstevel@tonic-gate 			stkp += DDI_FM_SYM_SZ;
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		if (errp)
2587c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2597c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2607c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2617c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2627c478bd9Sstevel@tonic-gate 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
2637c478bd9Sstevel@tonic-gate 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
2647c478bd9Sstevel@tonic-gate 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
2657c478bd9Sstevel@tonic-gate 		else
2667c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2677c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2687c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2697c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2707c478bd9Sstevel@tonic-gate 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
2717c478bd9Sstevel@tonic-gate 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
2727c478bd9Sstevel@tonic-gate 			    NULL);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		kmem_free(stkpp, depth * sizeof (char *));
2757c478bd9Sstevel@tonic-gate 		kmem_free(buf, depth * DDI_FM_SYM_SZ);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	} else {
2787c478bd9Sstevel@tonic-gate 		if (errp)
2797c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2807c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2817c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2827c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2837c478bd9Sstevel@tonic-gate 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
2847c478bd9Sstevel@tonic-gate 		else
2857c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2867c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2877c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2887c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2897c478bd9Sstevel@tonic-gate 			    NULL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
294602ca9eaScth  * fm_dev_ereport_postv: Common consolidation private interface to
295602ca9eaScth  * post a device tree oriented dev_scheme ereport. The device tree is
296602ca9eaScth  * composed of the following entities: devinfo nodes, minor nodes, and
297602ca9eaScth  * pathinfo nodes. All entities are associated with some devinfo node,
298602ca9eaScth  * either directly or indirectly. The intended devinfo node association
299602ca9eaScth  * for the ereport is communicated by the 'dip' argument. A minor node,
300602ca9eaScth  * an entity below 'dip', is represented by a non-null 'minor_name'
301602ca9eaScth  * argument. An application specific caller, like scsi_fm_ereport_post,
302602ca9eaScth  * can override the devinfo path with a pathinfo path via a non-null
303602ca9eaScth  * 'devpath' argument - in this case 'dip' is the MPXIO client node and
304602ca9eaScth  * devpath should be the path through the pHCI devinfo node to the
305602ca9eaScth  * pathinfo node.
306602ca9eaScth  *
307602ca9eaScth  * This interface also allows the caller to decide if the error being
308602ca9eaScth  * reported is know to be associated with a specific device identity
309602ca9eaScth  * via the 'devid' argument. The caller needs to control wether the
310602ca9eaScth  * devid appears as an authority in the FMRI because for some types of
311602ca9eaScth  * errors, like transport errors, the identity of the device on the
312602ca9eaScth  * other end of the transport is not guaranteed to be the current
313602ca9eaScth  * identity of the dip. For transport errors the caller should specify
314602ca9eaScth  * a NULL devid, even when there is a valid devid associated with the dip.
315602ca9eaScth  *
316602ca9eaScth  * The ddi_fm_ereport_post() implementation calls this interface with
317602ca9eaScth  * just a dip: devpath, minor_name, and devid are all NULL. The
318602ca9eaScth  * scsi_fm_ereport_post() implementation may call this interface with
319602ca9eaScth  * non-null devpath, minor_name, and devid arguments depending on
320602ca9eaScth  * wether MPXIO is enabled, and wether a transport or non-transport
321602ca9eaScth  * error is being posted.
322602ca9eaScth  */
323602ca9eaScth void
324602ca9eaScth fm_dev_ereport_postv(dev_info_t *dip, dev_info_t *eqdip,
325602ca9eaScth     const char *devpath, const char *minor_name, const char *devid,
326602ca9eaScth     const char *error_class, uint64_t ena, int sflag, va_list ap)
327602ca9eaScth {
328*0b1ecf75Scth 	nv_alloc_t		*nva = NULL;
329*0b1ecf75Scth 	struct i_ddi_fmhdl	*fmhdl = NULL;
330602ca9eaScth 	errorq_elem_t		*eqep;
331602ca9eaScth 	nvlist_t		*ereport = NULL;
332602ca9eaScth 	nvlist_t		*detector = NULL;
333602ca9eaScth 	char			*name;
334602ca9eaScth 	data_type_t		type;
335602ca9eaScth 	uint8_t			version;
336602ca9eaScth 	char			class[ERPT_CLASS_SZ];
337602ca9eaScth 	char			path[MAXPATHLEN];
338602ca9eaScth 
339602ca9eaScth 	ASSERT(dip && eqdip && error_class);
340602ca9eaScth 
341602ca9eaScth 	/*
342602ca9eaScth 	 * This interface should be called with a fm_capable eqdip. The
343602ca9eaScth 	 * ddi_fm_ereport_post* interfaces call with eqdip == dip,
344602ca9eaScth 	 * ndi_fm_ereport_post* interfaces call with eqdip == ddi_parent(dip).
345602ca9eaScth 	 */
346602ca9eaScth 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(eqdip)))
347602ca9eaScth 		goto err;
348602ca9eaScth 
349602ca9eaScth 	/* get ereport nvlist handle */
350602ca9eaScth 	if ((sflag == DDI_SLEEP) && !panicstr) {
351602ca9eaScth 		/*
352602ca9eaScth 		 * Driver defect - should not call with DDI_SLEEP while in
353602ca9eaScth 		 * interrupt context.
354602ca9eaScth 		 */
355602ca9eaScth 		if (servicing_interrupt()) {
356602ca9eaScth 			i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, sflag);
357602ca9eaScth 			goto err;
358602ca9eaScth 		}
359602ca9eaScth 
360602ca9eaScth 		/* Use normal interfaces to allocate memory. */
361602ca9eaScth 		if ((ereport = fm_nvlist_create(NULL)) == NULL)
362602ca9eaScth 			goto err;
363*0b1ecf75Scth 		ASSERT(nva == NULL);
364602ca9eaScth 	} else {
365602ca9eaScth 		/* Use errorq interfaces to avoid memory allocation. */
366602ca9eaScth 		fmhdl = DEVI(eqdip)->devi_fmhdl;
367602ca9eaScth 		ASSERT(fmhdl);
368602ca9eaScth 		eqep = errorq_reserve(fmhdl->fh_errorq);
369602ca9eaScth 		if (eqep == NULL)
370602ca9eaScth 			goto err;
371602ca9eaScth 
372602ca9eaScth 		ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
373602ca9eaScth 		nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
374602ca9eaScth 		ASSERT(nva);
375602ca9eaScth 	}
376602ca9eaScth 	ASSERT(ereport);
377602ca9eaScth 
378602ca9eaScth 	/*
379602ca9eaScth 	 * Form parts of an ereport:
380602ca9eaScth 	 *	A: version
381602ca9eaScth 	 * 	B: error_class
382602ca9eaScth 	 *	C: ena
383602ca9eaScth 	 *	D: detector	(path and optional devid authority)
384602ca9eaScth 	 *	E: payload
385602ca9eaScth 	 *
386602ca9eaScth 	 * A: ereport version: first payload tuple must be the version.
387602ca9eaScth 	 */
388602ca9eaScth 	name = va_arg(ap, char *);
389602ca9eaScth 	type = va_arg(ap, data_type_t);
390602ca9eaScth 	version = va_arg(ap, uint_t);
391602ca9eaScth 	if ((strcmp(name, FM_VERSION) != 0) || (type != DATA_TYPE_UINT8)) {
392602ca9eaScth 		i_ddi_drv_ereport_post(dip, DVR_EVER, NULL, sflag);
393602ca9eaScth 		goto err;
394602ca9eaScth 	}
395602ca9eaScth 
396602ca9eaScth 	/* B: ereport error_class: add "io." prefix to class. */
397602ca9eaScth 	(void) snprintf(class, ERPT_CLASS_SZ, "%s.%s",
398602ca9eaScth 	    DDI_IO_CLASS, error_class);
399602ca9eaScth 
400602ca9eaScth 	/* C: ereport ena: if not passed in, generate new ena. */
401602ca9eaScth 	if (ena == 0)
402602ca9eaScth 		ena = fm_ena_generate(0, FM_ENA_FMT1);
403602ca9eaScth 
404602ca9eaScth 	/* D: detector: form dev scheme fmri with path and devid. */
405602ca9eaScth 	if (devpath) {
406602ca9eaScth 		(void) strlcpy(path, devpath, sizeof (path));
407602ca9eaScth 	} else {
408602ca9eaScth 		/* derive devpath from dip */
409602ca9eaScth 		if (dip == ddi_root_node())
410602ca9eaScth 			(void) strcpy(path, "/");
411602ca9eaScth 		else
412602ca9eaScth 			(void) ddi_pathname(dip, path);
413602ca9eaScth 	}
414602ca9eaScth 	if (minor_name) {
415602ca9eaScth 		(void) strlcat(path, ":", sizeof (path));
416602ca9eaScth 		(void) strlcat(path, minor_name, sizeof (path));
417602ca9eaScth 	}
418602ca9eaScth 	detector = fm_nvlist_create(nva);
419602ca9eaScth 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, path, devid);
420602ca9eaScth 
421602ca9eaScth 	/* Pull parts of ereport together into ereport. */
422602ca9eaScth 	fm_ereport_set(ereport, version, class, ena, detector, NULL);
423602ca9eaScth 
424602ca9eaScth 	/* Add the payload to ereport. */
425602ca9eaScth 	name = va_arg(ap, char *);
426602ca9eaScth 	(void) i_fm_payload_set(ereport, name, ap);
427602ca9eaScth 
428602ca9eaScth 	/* Post the ereport. */
429602ca9eaScth 	if (nva)
430602ca9eaScth 		errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
431602ca9eaScth 	else
432602ca9eaScth 		fm_ereport_post(ereport, EVCH_SLEEP);
433602ca9eaScth 	goto out;
434602ca9eaScth 
435602ca9eaScth 	/* Count errors as drops. */
436602ca9eaScth err:	if (fmhdl)
437602ca9eaScth 		atomic_add_64(&fmhdl->fh_kstat.fek_erpt_dropped.value.ui64, 1);
438602ca9eaScth 
439*0b1ecf75Scth 	/* Free up nvlists if normal interfaces were used to allocate memory */
440602ca9eaScth out:	if (ereport && (nva == NULL))
441602ca9eaScth 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
442602ca9eaScth 	if (detector && (nva == NULL))
443602ca9eaScth 		fm_nvlist_destroy(detector, FM_NVA_FREE);
444602ca9eaScth }
445602ca9eaScth 
446602ca9eaScth /*
4477c478bd9Sstevel@tonic-gate  * Generate an error report for consumption by the Solaris Fault Manager,
448602ca9eaScth  * fmd(1M).  Valid ereport classes are defined in /usr/include/sys/fm/io.
449602ca9eaScth  *
450602ca9eaScth  * The ENA should be set if this error is a result of an error status
451602ca9eaScth  * returned from ddi_dma_err_check() or ddi_acc_err_check().  Otherwise,
452602ca9eaScth  * an ENA value of 0 is appropriate.
4537c478bd9Sstevel@tonic-gate  *
4547c478bd9Sstevel@tonic-gate  * If sflag == DDI_NOSLEEP, ddi_fm_ereport_post () may be called
4557c478bd9Sstevel@tonic-gate  * from user, kernel, interrupt or high-interrupt context.  Otherwise,
4567c478bd9Sstevel@tonic-gate  * ddi_fm_ereport_post() must be called from user or kernel context.
457602ca9eaScth  *
458602ca9eaScth  * The ndi_interfaces are provided for use by nexus drivers to post
459602ca9eaScth  * ereports about children who may not themselves be fm_capable.
460602ca9eaScth  *
461602ca9eaScth  * All interfaces end up in the common fm_dev_ereport_postv code above.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate void
464602ca9eaScth ddi_fm_ereport_post(dev_info_t *dip,
465602ca9eaScth     const char *error_class, uint64_t ena, int sflag, ...)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	va_list ap;
4687c478bd9Sstevel@tonic-gate 
469602ca9eaScth 	ASSERT(dip && error_class);
4707c478bd9Sstevel@tonic-gate 	va_start(ap, sflag);
471602ca9eaScth 	fm_dev_ereport_postv(dip, dip, NULL, NULL, NULL,
472602ca9eaScth 	    error_class, ena, sflag, ap);
4737c478bd9Sstevel@tonic-gate 	va_end(ap);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
476602ca9eaScth void
477602ca9eaScth ndi_fm_ereport_post(dev_info_t *dip,
478602ca9eaScth     const char *error_class, uint64_t ena, int sflag, ...)
479602ca9eaScth {
480602ca9eaScth 	va_list ap;
481602ca9eaScth 
482602ca9eaScth 	ASSERT(dip && error_class && (sflag == DDI_SLEEP));
483602ca9eaScth 	va_start(ap, sflag);
484602ca9eaScth 	fm_dev_ereport_postv(dip, ddi_get_parent(dip), NULL, NULL, NULL,
485602ca9eaScth 	    error_class, ena, sflag, ap);
4867c478bd9Sstevel@tonic-gate 	va_end(ap);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Driver error handling entry.  Prevents multiple simultaneous calls into
4917c478bd9Sstevel@tonic-gate  * driver error handling callback.
4927c478bd9Sstevel@tonic-gate  *
4937c478bd9Sstevel@tonic-gate  * May be called from a context consistent with the iblock_cookie returned
4947c478bd9Sstevel@tonic-gate  * in ddi_fm_init().
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate void
4977c478bd9Sstevel@tonic-gate i_ddi_fm_handler_enter(dev_info_t *dip)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->fh_lock);
502eae2e508Skrishnae 	hdl->fh_lock_owner = curthread;
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * Driver error handling exit.
5077c478bd9Sstevel@tonic-gate  *
5087c478bd9Sstevel@tonic-gate  * May be called from a context consistent with the iblock_cookie returned
5097c478bd9Sstevel@tonic-gate  * in ddi_fm_init().
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate void
5127c478bd9Sstevel@tonic-gate i_ddi_fm_handler_exit(dev_info_t *dip)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
5157c478bd9Sstevel@tonic-gate 
516eae2e508Skrishnae 	hdl->fh_lock_owner = NULL;
5177c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->fh_lock);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
520eae2e508Skrishnae boolean_t
521eae2e508Skrishnae i_ddi_fm_handler_owned(dev_info_t *dip)
522eae2e508Skrishnae {
523eae2e508Skrishnae 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
524eae2e508Skrishnae 
525eae2e508Skrishnae 	return (hdl->fh_lock_owner == curthread);
526eae2e508Skrishnae }
527eae2e508Skrishnae 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Register a fault manager error handler for this device instance
5307c478bd9Sstevel@tonic-gate  *
5317c478bd9Sstevel@tonic-gate  * This function must be called from a driver's attach(9E) routine.
5327c478bd9Sstevel@tonic-gate  */
5337c478bd9Sstevel@tonic-gate void
5347c478bd9Sstevel@tonic-gate ddi_fm_handler_register(dev_info_t *dip, ddi_err_func_t handler,
5357c478bd9Sstevel@tonic-gate     void *impl_data)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
5387c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *pfmhdl;
5397c478bd9Sstevel@tonic-gate 	struct i_ddi_errhdl *new_eh;
5407c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Check for proper calling context.
5447c478bd9Sstevel@tonic-gate 	 * The DDI configuration framework does not support
5457c478bd9Sstevel@tonic-gate 	 * DR states to allow checking for proper invocation
5467c478bd9Sstevel@tonic-gate 	 * from a DDI_ATTACH or DDI_RESUME.  This limits context checking
5477c478bd9Sstevel@tonic-gate 	 * to interrupt only.
5487c478bd9Sstevel@tonic-gate 	 */
5497c478bd9Sstevel@tonic-gate 	if (servicing_interrupt()) {
5507c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
5517c478bd9Sstevel@tonic-gate 		return;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547aec1d6eScindi 	if (dip == ddi_root_node())
5557aec1d6eScindi 		pdip = dip;
5567aec1d6eScindi 	else
5577c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
5627c478bd9Sstevel@tonic-gate 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
5637c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
5647c478bd9Sstevel@tonic-gate 		return;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	new_eh = kmem_zalloc(sizeof (struct i_ddi_errhdl), KM_SLEEP);
5687c478bd9Sstevel@tonic-gate 	new_eh->eh_func = handler;
5697c478bd9Sstevel@tonic-gate 	new_eh->eh_impl = impl_data;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/* Add dip to parent's target list of registered error handlers */
5727c478bd9Sstevel@tonic-gate 	tgt = kmem_alloc(sizeof (struct i_ddi_fmtgt), KM_SLEEP);
5737c478bd9Sstevel@tonic-gate 	tgt->ft_dip = dip;
5747c478bd9Sstevel@tonic-gate 	tgt->ft_errhdl = new_eh;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(pdip);
5777c478bd9Sstevel@tonic-gate 	pfmhdl = DEVI(pdip)->devi_fmhdl;
5787c478bd9Sstevel@tonic-gate 	ASSERT(pfmhdl);
5797c478bd9Sstevel@tonic-gate 	tgt->ft_next = pfmhdl->fh_tgts;
5807c478bd9Sstevel@tonic-gate 	pfmhdl->fh_tgts = tgt;
5817c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(pdip);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Unregister a fault manager error handler for this device instance
5867c478bd9Sstevel@tonic-gate  *
5877c478bd9Sstevel@tonic-gate  * This function must be called from a drivers attach(9E) or detach(9E)
5887c478bd9Sstevel@tonic-gate  * routine.
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate void
5917c478bd9Sstevel@tonic-gate ddi_fm_handler_unregister(dev_info_t *dip)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
5947c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *pfmhdl;
5957c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt, **ptgt;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * Check for proper calling context.
5997c478bd9Sstevel@tonic-gate 	 * The DDI configuration framework does not support
6007c478bd9Sstevel@tonic-gate 	 * DR states to allow checking for proper invocation
6017c478bd9Sstevel@tonic-gate 	 * from a DDI_DETACH or DDI_SUSPEND.  This limits context checking
6027c478bd9Sstevel@tonic-gate 	 * to interrupt only.
6037c478bd9Sstevel@tonic-gate 	 */
6047c478bd9Sstevel@tonic-gate 	if (servicing_interrupt()) {
6057c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097aec1d6eScindi 	if (dip == ddi_root_node())
6107aec1d6eScindi 		pdip = dip;
6117aec1d6eScindi 	else
6127c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
6177c478bd9Sstevel@tonic-gate 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
6187c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
6197c478bd9Sstevel@tonic-gate 		return;
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(pdip);
6237c478bd9Sstevel@tonic-gate 	pfmhdl = DEVI(pdip)->devi_fmhdl;
6247c478bd9Sstevel@tonic-gate 	ASSERT(pfmhdl);
6257c478bd9Sstevel@tonic-gate 	ptgt = &pfmhdl->fh_tgts;
6267c478bd9Sstevel@tonic-gate 	for (tgt = pfmhdl->fh_tgts; tgt != NULL; tgt = tgt->ft_next) {
6277c478bd9Sstevel@tonic-gate 		if (dip == tgt->ft_dip) {
6287c478bd9Sstevel@tonic-gate 			*ptgt = tgt->ft_next;
6297c478bd9Sstevel@tonic-gate 			kmem_free(tgt->ft_errhdl, sizeof (struct i_ddi_errhdl));
6307c478bd9Sstevel@tonic-gate 			kmem_free(tgt, sizeof (struct i_ddi_fmtgt));
6317c478bd9Sstevel@tonic-gate 			break;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 		ptgt = &tgt->ft_next;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(pdip);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * Initialize Fault Management capabilities for this device instance (dip).
6427c478bd9Sstevel@tonic-gate  * When called with the following capabilities, data structures neccessary
6437c478bd9Sstevel@tonic-gate  * for fault management activities are allocated and initialized.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  *	DDI_FM_EREPORT_CAPABLE - initialize ereport errorq and ereport
6467c478bd9Sstevel@tonic-gate  *				capable driver property.
6477c478bd9Sstevel@tonic-gate  *
6487c478bd9Sstevel@tonic-gate  *	DDI_FM_ERRCB_CAPABLE - check with parent for ability to register
6497c478bd9Sstevel@tonic-gate  *				an error handler.
6507c478bd9Sstevel@tonic-gate  *
6517c478bd9Sstevel@tonic-gate  *	DDI_FM_ACCCHK_CAPABLE - initialize access handle cache and acc-chk
6527c478bd9Sstevel@tonic-gate  *				driver property
6537c478bd9Sstevel@tonic-gate  *
6547c478bd9Sstevel@tonic-gate  *	DDI_FM_DMACHK_CAPABLE - initialize dma handle cache and dma-chk
6557c478bd9Sstevel@tonic-gate  *				driver property
6567c478bd9Sstevel@tonic-gate  *
6577c478bd9Sstevel@tonic-gate  * A driver's FM capability level may not exceed that of its parent or
6587c478bd9Sstevel@tonic-gate  * system-wide FM capability.  The available capability level for this
6597c478bd9Sstevel@tonic-gate  * device instance is returned in *fmcap.
6607c478bd9Sstevel@tonic-gate  *
6617c478bd9Sstevel@tonic-gate  * This function must be called from a driver's attach(9E) entry point.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate void
6647aec1d6eScindi ddi_fm_init(dev_info_t *dip, int *fmcap, ddi_iblock_cookie_t *ibcp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6677c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl;
6687aec1d6eScindi 	ddi_iblock_cookie_t ibc;
6697c478bd9Sstevel@tonic-gate 	int pcap, newcap = DDI_FM_NOT_CAPABLE;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(dip)) {
6727c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
6737c478bd9Sstevel@tonic-gate 		*fmcap = DDI_FM_NOT_CAPABLE;
6747c478bd9Sstevel@tonic-gate 		return;
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (DDI_FM_DEFAULT_CAP(*fmcap))
6787c478bd9Sstevel@tonic-gate 		return;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	/*
6817c478bd9Sstevel@tonic-gate 	 * Check parent for supported FM level
6827c478bd9Sstevel@tonic-gate 	 * and correct error handling PIL
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	if (dip != ddi_root_node()) {
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * Initialize the default ibc.  The parent may change it
6887c478bd9Sstevel@tonic-gate 		 * depending upon its capabilities.
6897c478bd9Sstevel@tonic-gate 		 */
6907aec1d6eScindi 		ibc = (ddi_iblock_cookie_t)ipltospl(FM_ERR_PIL);
6917c478bd9Sstevel@tonic-gate 
6927aec1d6eScindi 		pcap = i_ndi_busop_fm_init(dip, *fmcap, &ibc);
6937c478bd9Sstevel@tonic-gate 	} else {
6947c478bd9Sstevel@tonic-gate 		pcap = *fmcap;
6957aec1d6eScindi 		ibc = *ibcp;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/* Initialize the per-device instance FM handle */
6997c478bd9Sstevel@tonic-gate 	fmhdl = kmem_zalloc(sizeof (struct i_ddi_fmhdl), KM_SLEEP);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if ((fmhdl->fh_ksp = kstat_create((char *)ddi_driver_name(dip),
7027c478bd9Sstevel@tonic-gate 	    ddi_get_instance(dip), "fm", "misc",
7037c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, sizeof (struct i_ddi_fmkstat) /
7047c478bd9Sstevel@tonic-gate 	    sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) == NULL) {
7057c478bd9Sstevel@tonic-gate 		mutex_destroy(&fmhdl->fh_lock);
7067c478bd9Sstevel@tonic-gate 		kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
7077c478bd9Sstevel@tonic-gate 		*fmcap = DDI_FM_NOT_CAPABLE;
7087c478bd9Sstevel@tonic-gate 		return;
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	bcopy(&ddifm_kstat_template, &fmhdl->fh_kstat,
7127c478bd9Sstevel@tonic-gate 	    sizeof (struct i_ddi_fmkstat));
7137c478bd9Sstevel@tonic-gate 	fmhdl->fh_ksp->ks_data = &fmhdl->fh_kstat;
7147c478bd9Sstevel@tonic-gate 	fmhdl->fh_ksp->ks_private = fmhdl;
7157c478bd9Sstevel@tonic-gate 	kstat_install(fmhdl->fh_ksp);
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	fmhdl->fh_dma_cache = NULL;
7187c478bd9Sstevel@tonic-gate 	fmhdl->fh_acc_cache = NULL;
7197c478bd9Sstevel@tonic-gate 	fmhdl->fh_tgts = NULL;
7207c478bd9Sstevel@tonic-gate 	fmhdl->fh_dip = dip;
7217aec1d6eScindi 	fmhdl->fh_ibc = ibc;
7227c478bd9Sstevel@tonic-gate 	mutex_init(&fmhdl->fh_lock, NULL, MUTEX_DRIVER, fmhdl->fh_ibc);
7237c478bd9Sstevel@tonic-gate 	devi->devi_fmhdl = fmhdl;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * Initialize support for ereport generation
7277c478bd9Sstevel@tonic-gate 	 */
72800d0963fSdilpreet 	if (DDI_FM_EREPORT_CAP(*fmcap) && DDI_FM_EREPORT_CAP(pcap)) {
7297c478bd9Sstevel@tonic-gate 		fmhdl->fh_errorq = ereport_errorq;
73000d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7317c478bd9Sstevel@tonic-gate 		    "fm-ereport-capable", 0) == 0)
73200d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7337c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-ereport-capable", NULL, 0);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_EREPORT_CAPABLE;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	/*
7397c478bd9Sstevel@tonic-gate 	 * Need cooperation of the parent for error handling
7407c478bd9Sstevel@tonic-gate 	 */
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (DDI_FM_ERRCB_CAP(*fmcap) && DDI_FM_ERRCB_CAP(pcap)) {
74300d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7447c478bd9Sstevel@tonic-gate 		    "fm-errcb-capable", 0) == 0)
74500d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7467c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-errcb-capable", NULL, 0);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_ERRCB_CAPABLE;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * Support for DMA and Access error handling
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	if (DDI_FM_DMA_ERR_CAP(*fmcap) && DDI_FM_DMA_ERR_CAP(pcap)) {
7567aec1d6eScindi 		i_ndi_fmc_create(&fmhdl->fh_dma_cache, 2, ibc);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		/* Set-up dma chk capability prop */
75900d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7607c478bd9Sstevel@tonic-gate 		    "fm-dmachk-capable", 0) == 0)
76100d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7627c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-dmachk-capable", NULL, 0);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_DMACHK_CAPABLE;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (DDI_FM_ACC_ERR_CAP(*fmcap) && DDI_FM_ACC_ERR_CAP(pcap)) {
7687aec1d6eScindi 		i_ndi_fmc_create(&fmhdl->fh_acc_cache, 2, ibc);
7697c478bd9Sstevel@tonic-gate 		/* Set-up dma chk capability prop */
77000d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7717c478bd9Sstevel@tonic-gate 		    "fm-accchk-capable", 0) == 0)
77200d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7737c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-accchk-capable", NULL, 0);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_ACCCHK_CAPABLE;
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * Return the capability support available
7807c478bd9Sstevel@tonic-gate 	 * to this driver instance
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	fmhdl->fh_cap = newcap;
7837c478bd9Sstevel@tonic-gate 	*fmcap = newcap;
7847aec1d6eScindi 
7857aec1d6eScindi 	if (ibcp != NULL)
7867aec1d6eScindi 		*ibcp = ibc;
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Finalize Fault Management activities for this device instance.
7917c478bd9Sstevel@tonic-gate  * Outstanding IO transaction must be completed prior to calling
7927c478bd9Sstevel@tonic-gate  * this routine.  All previously allocated resources and error handler
7937c478bd9Sstevel@tonic-gate  * registration are cleared and deallocated.
7947c478bd9Sstevel@tonic-gate  *
7957c478bd9Sstevel@tonic-gate  * This function must be called from a driver's detach(9E) entry point.
7967c478bd9Sstevel@tonic-gate  */
7977c478bd9Sstevel@tonic-gate void
7987c478bd9Sstevel@tonic-gate ddi_fm_fini(dev_info_t *dip)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
8017c478bd9Sstevel@tonic-gate 
80200d0963fSdilpreet 	ASSERT(fmhdl);
80300d0963fSdilpreet 
8047c478bd9Sstevel@tonic-gate 	if (!(DEVI_IS_DETACHING(dip) || DEVI_IS_ATTACHING(dip))) {
8057c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
8067c478bd9Sstevel@tonic-gate 		return;
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	kstat_delete(fmhdl->fh_ksp);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (DDI_FM_EREPORT_CAP(fmhdl->fh_cap)) {
81200d0963fSdilpreet 		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8137c478bd9Sstevel@tonic-gate 		    "fm-ereport-capable");
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (dip != ddi_root_node()) {
817cecfa358Sstephh 		if (DDI_FM_ERRCB_CAP(fmhdl->fh_cap)) {
8187c478bd9Sstevel@tonic-gate 			ddi_fm_handler_unregister(dip);
819cecfa358Sstephh 			(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
820cecfa358Sstephh 			    "fm-errcb-capable");
821cecfa358Sstephh 		}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		if (DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap) ||
8247c478bd9Sstevel@tonic-gate 		    DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
8257c478bd9Sstevel@tonic-gate 			if (fmhdl->fh_dma_cache != NULL) {
8267c478bd9Sstevel@tonic-gate 				i_ndi_fmc_destroy(fmhdl->fh_dma_cache);
82700d0963fSdilpreet 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8287c478bd9Sstevel@tonic-gate 				    "fm-dmachk-capable");
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			if (fmhdl->fh_acc_cache != NULL) {
8317c478bd9Sstevel@tonic-gate 				i_ndi_fmc_destroy(fmhdl->fh_acc_cache);
83200d0963fSdilpreet 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8337c478bd9Sstevel@tonic-gate 				    "fm-accachk-capable");
8347c478bd9Sstevel@tonic-gate 			}
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		i_ndi_busop_fm_fini(dip);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
8417c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_fmhdl = NULL;
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Return the fault management capability level for this device instance.
8467c478bd9Sstevel@tonic-gate  *
8477c478bd9Sstevel@tonic-gate  * This function may be called from user, kernel, or interrupt context.
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate int
8507c478bd9Sstevel@tonic-gate ddi_fm_capable(dev_info_t *dip)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (fmhdl == NULL)
8557c478bd9Sstevel@tonic-gate 		return (DDI_FM_NOT_CAPABLE);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	return (fmhdl->fh_cap);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate  * Routines to set and get error information for/from an access or dma handle
8627c478bd9Sstevel@tonic-gate  *
8637c478bd9Sstevel@tonic-gate  * These routines may be called from user, kernel, and interrupt contexts.
8647c478bd9Sstevel@tonic-gate  */
8657c478bd9Sstevel@tonic-gate void
8667c478bd9Sstevel@tonic-gate ddi_fm_acc_err_get(ddi_acc_handle_t handle, ddi_fm_error_t *de, int version)
8677c478bd9Sstevel@tonic-gate {
86800d0963fSdilpreet 	ndi_err_t *errp;
86900d0963fSdilpreet 
87000d0963fSdilpreet 	if (handle == NULL)
87100d0963fSdilpreet 		return;
8727c478bd9Sstevel@tonic-gate 
8738aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
8747c478bd9Sstevel@tonic-gate 		ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
87700d0963fSdilpreet 		cmn_err(CE_PANIC, "ddi_fm_acc_err_get: "
8787c478bd9Sstevel@tonic-gate 		    "Invalid driver version\n");
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
88100d0963fSdilpreet 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
8827c478bd9Sstevel@tonic-gate 	de->fme_status = errp->err_status;
8837c478bd9Sstevel@tonic-gate 	de->fme_ena = errp->err_ena;
8847c478bd9Sstevel@tonic-gate 	de->fme_flag = errp->err_expected;
8857c478bd9Sstevel@tonic-gate 	de->fme_acc_handle = handle;
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate void
8897c478bd9Sstevel@tonic-gate ddi_fm_dma_err_get(ddi_dma_handle_t handle, ddi_fm_error_t *de, int version)
8907c478bd9Sstevel@tonic-gate {
89100d0963fSdilpreet 	ndi_err_t *errp;
89200d0963fSdilpreet 
89300d0963fSdilpreet 	if (handle == NULL)
89400d0963fSdilpreet 		return;
8957c478bd9Sstevel@tonic-gate 
8968aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
8977c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
8987c478bd9Sstevel@tonic-gate 		    DVR_EVER, NULL, DDI_NOSLEEP);
8997c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "ddi_fm_dma_err_get: "
9007c478bd9Sstevel@tonic-gate 		    "Invalid driver version\n");
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
90300d0963fSdilpreet 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
90400d0963fSdilpreet 
9057c478bd9Sstevel@tonic-gate 	de->fme_status = errp->err_status;
9067c478bd9Sstevel@tonic-gate 	de->fme_ena = errp->err_ena;
9077c478bd9Sstevel@tonic-gate 	de->fme_flag = errp->err_expected;
9087c478bd9Sstevel@tonic-gate 	de->fme_dma_handle = handle;
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate void
91200d0963fSdilpreet ddi_fm_acc_err_clear(ddi_acc_handle_t handle, int version)
91300d0963fSdilpreet {
91400d0963fSdilpreet 	ndi_err_t *errp;
91500d0963fSdilpreet 
91600d0963fSdilpreet 	if (handle == NULL)
91700d0963fSdilpreet 		return;
91800d0963fSdilpreet 
9198aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
92000d0963fSdilpreet 		ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
92100d0963fSdilpreet 
92200d0963fSdilpreet 		i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
92300d0963fSdilpreet 		cmn_err(CE_PANIC, "ddi_fm_acc_err_clear: "
92400d0963fSdilpreet 		    "Invalid driver version\n");
92500d0963fSdilpreet 	}
92600d0963fSdilpreet 
92700d0963fSdilpreet 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
92800d0963fSdilpreet 	errp->err_status = DDI_FM_OK;
92900d0963fSdilpreet 	errp->err_ena = 0;
93000d0963fSdilpreet 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
93100d0963fSdilpreet }
93200d0963fSdilpreet 
93300d0963fSdilpreet void
93400d0963fSdilpreet ddi_fm_dma_err_clear(ddi_dma_handle_t handle, int version)
93500d0963fSdilpreet {
93600d0963fSdilpreet 	ndi_err_t *errp;
93700d0963fSdilpreet 
93800d0963fSdilpreet 	if (handle == NULL)
93900d0963fSdilpreet 		return;
94000d0963fSdilpreet 
9418aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
94200d0963fSdilpreet 		i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
94300d0963fSdilpreet 		    DVR_EVER, NULL, DDI_NOSLEEP);
94400d0963fSdilpreet 		cmn_err(CE_PANIC, "ddi_fm_dma_err_clear: "
94500d0963fSdilpreet 		    "Invalid driver version\n");
94600d0963fSdilpreet 	}
94700d0963fSdilpreet 
94800d0963fSdilpreet 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
94900d0963fSdilpreet 
95000d0963fSdilpreet 	errp->err_status = DDI_FM_OK;
95100d0963fSdilpreet 	errp->err_ena = 0;
95200d0963fSdilpreet 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
95300d0963fSdilpreet }
95400d0963fSdilpreet 
95500d0963fSdilpreet void
9567c478bd9Sstevel@tonic-gate i_ddi_fm_acc_err_set(ddi_acc_handle_t handle, uint64_t ena, int status,
9577c478bd9Sstevel@tonic-gate     int flag)
9587c478bd9Sstevel@tonic-gate {
9597c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hdlp = impl_acc_hdl_get(handle);
9607c478bd9Sstevel@tonic-gate 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
9617c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->ah_dip)->devi_fmhdl;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_ena = ena;
9647c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_status = status;
9657c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_expected = flag;
9667c478bd9Sstevel@tonic-gate 	atomic_add_64(&fmhdl->fh_kstat.fek_acc_err.value.ui64, 1);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate void
9707c478bd9Sstevel@tonic-gate i_ddi_fm_dma_err_set(ddi_dma_handle_t handle, uint64_t ena, int status,
9717c478bd9Sstevel@tonic-gate     int flag)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
9747c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->dmai_rdip)->devi_fmhdl;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_ena = ena;
9777c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_status = status;
9787c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_expected = flag;
9797c478bd9Sstevel@tonic-gate 	atomic_add_64(&fmhdl->fh_kstat.fek_dma_err.value.ui64, 1);
9807c478bd9Sstevel@tonic-gate }
98100d0963fSdilpreet 
98200d0963fSdilpreet ddi_fmcompare_t
98300d0963fSdilpreet i_ddi_fm_acc_err_cf_get(ddi_acc_handle_t handle)
98400d0963fSdilpreet {
98500d0963fSdilpreet 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
98600d0963fSdilpreet 
98700d0963fSdilpreet 	return (i_hdlp->ahi_err->err_cf);
98800d0963fSdilpreet }
98900d0963fSdilpreet 
99000d0963fSdilpreet ddi_fmcompare_t
99100d0963fSdilpreet i_ddi_fm_dma_err_cf_get(ddi_dma_handle_t handle)
99200d0963fSdilpreet {
99300d0963fSdilpreet 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
99400d0963fSdilpreet 
99500d0963fSdilpreet 	return (hdlp->dmai_error.err_cf);
99600d0963fSdilpreet }
997