xref: /titanic_54/usr/src/uts/common/os/ddifm.c (revision 837c1ac4e72b7d86278cca88b1075af557f7d161)
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 /*
22*837c1ac4SStephen Hanson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Fault Management for Device Drivers
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * Device drivers wishing to participate in fault management may do so by
307c478bd9Sstevel@tonic-gate  * first initializing their fault management state and capabilties via
317c478bd9Sstevel@tonic-gate  * ddi_fm_init(). If the system supports the requested FM capabilities,
327c478bd9Sstevel@tonic-gate  * the IO framework will intialize FM state and return a bit mask of the
337c478bd9Sstevel@tonic-gate  * requested capabilities.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * If the system does not support the requested FM capabilities,
367c478bd9Sstevel@tonic-gate  * the device driver must behave in accordance with the programming semantics
377c478bd9Sstevel@tonic-gate  * defined below for the capabilities returned from ddi_fm_init().
387c478bd9Sstevel@tonic-gate  * ddi_fm_init() must be called at attach(9E) time and ddi_fm_fini() must be
397c478bd9Sstevel@tonic-gate  * called from detach(9E) to perform FM clean-up.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * Driver Fault Management Capabilities
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * DDI_FM_NOT_CAPABLE
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *	This is the default fault management capability for drivers.  Drivers
467c478bd9Sstevel@tonic-gate  *	that implement no fault management capabilites or do not participate
477c478bd9Sstevel@tonic-gate  *	in fault management activities have their FM capability bitmask set
487c478bd9Sstevel@tonic-gate  *	to 0.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * DDI_FM_EREPORT_CAPABLE
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	When this capability bit is set, drivers are expected to generate error
537c478bd9Sstevel@tonic-gate  *	report events via ddi_ereport_post() for the associated faults
547c478bd9Sstevel@tonic-gate  *	that are diagnosed by the IO fault manager DE.  ddi_ereport_post()
557c478bd9Sstevel@tonic-gate  *	may be called in any context subject to the constraints specified
567c478bd9Sstevel@tonic-gate  *	by the interrupt iblock cookie	returned during initialization.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	Error reports resulting from hardware component specific and common IO
597c478bd9Sstevel@tonic-gate  *	fault and driver defects must be accompanied by an Eversholt fault
607c478bd9Sstevel@tonic-gate  *	tree (.eft) by the Solaris fault manager (fmd(1M)) for
617c478bd9Sstevel@tonic-gate  *	diagnosis.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * DDI_FM_ERRCB_CAPABLE
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *	Device drivers are expected to implement and register an error
667c478bd9Sstevel@tonic-gate  *	handler callback function.  ddi_fm_handler_register() and
677c478bd9Sstevel@tonic-gate  *	ddi_fm_handler_unregister() must be
687c478bd9Sstevel@tonic-gate  *	called in passive kernel context, typically during an attach(9E)
697c478bd9Sstevel@tonic-gate  *	or detach(9E) operation.  When called by the FM IO framework,
707c478bd9Sstevel@tonic-gate  *	the callback function should check for error conditions for the
717c478bd9Sstevel@tonic-gate  *	hardware and software under its control.  All detected errors
727c478bd9Sstevel@tonic-gate  *	should have ereport events generated for them.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *	Upon completion of the error handler callback, the driver should
757c478bd9Sstevel@tonic-gate  *	return one of the following values:
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *	#define DDI_FM_OK - no error was detected
787c478bd9Sstevel@tonic-gate  *	#define DDI_FM_FATAL - a fatal error was detected
797c478bd9Sstevel@tonic-gate  *	#define DDI_FM_NONFATAL - a non-fatal error was detected
807c478bd9Sstevel@tonic-gate  *	#define DDI_FM_UNKNOWN - the error status is unknown
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  *	To insure single threaded access to error handling callbacks,
837c478bd9Sstevel@tonic-gate  *	the device driver may use i_ddi_fm_handler_enter() and
847c478bd9Sstevel@tonic-gate  *	i_ddi_fm_handler_exit() when entering and exiting the callback.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * DDI_FM_ACCCHK_CAPABLE/DDI_FM_DMACHK_CAPABLE
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  *	Device drivers are expected to set-up access and DMA handles
897c478bd9Sstevel@tonic-gate  *	with FM-specific attributes designed to allow nexus parent
907c478bd9Sstevel@tonic-gate  *	drivers to flag any errors seen during subsequent IO transactions.
917c478bd9Sstevel@tonic-gate  *	Drivers must set the devacc_attr_acc_flag member of their
927c478bd9Sstevel@tonic-gate  *	ddi_device_acc_attr_t structures to DDI_FLAGERR_ACC or DDI_CAUTIOUS_ACC.
937c478bd9Sstevel@tonic-gate  *	For DMA transactions, driver must set the dma_attr_flags of
947c478bd9Sstevel@tonic-gate  *	their ddi_dma_attr_t structures to DDI_DMA_FLAGERR.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *	Upon completion of an IO transaction, device drivers are expected
977c478bd9Sstevel@tonic-gate  *	to check the status of host-side hardware access and device-side
987c478bd9Sstevel@tonic-gate  *	dma completions by calling ddi_acc_err_check() or ddi_dma_err_check()
997c478bd9Sstevel@tonic-gate  *	respectively. If the handle is associated with an error detected by
1007c478bd9Sstevel@tonic-gate  *	the nexus parent or FM IO framework, ddi_fm_error_t data (status, ena
1017c478bd9Sstevel@tonic-gate  *	and error expectation) is returned.  If status of DDI_FM_NONFATAL or
1027c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL is returned, the ena is valid and the expectation flag
1037c478bd9Sstevel@tonic-gate  *	will be set to 1 if the error was unexpected (i.e. not the result
1047c478bd9Sstevel@tonic-gate  *	of a peek or poke type operation).
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *	ddi_acc_err_check() and ddi_dma_err_check() may be called in any
1077c478bd9Sstevel@tonic-gate  *	context	subject to the constraints specified by the interrupt
1087c478bd9Sstevel@tonic-gate  *	iblock cookie returned during initialization.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  *	Device drivers should generate an access (DDI_FM_IO_ACC) or dma
1117c478bd9Sstevel@tonic-gate  *	(DDI_FM_IO_DMA) data path error report if DDI_FM_NONFATAL or
1127c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL is returned.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #include <sys/types.h>
1177c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1187c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
1197c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
1207c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
1217c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
1227c478bd9Sstevel@tonic-gate #include <sys/ndifm.h>
1237c478bd9Sstevel@tonic-gate #include <sys/ddifm.h>
1247c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
1257c478bd9Sstevel@tonic-gate #include <sys/ddi_isa.h>
1267c478bd9Sstevel@tonic-gate #include <sys/spl.h>
1277c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
1287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1297c478bd9Sstevel@tonic-gate #include <sys/disp.h>
1307c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1317c478bd9Sstevel@tonic-gate #include <sys/errorq_impl.h>
1327c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
1337c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
1347c478bd9Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	ERPT_CLASS_SZ	sizeof (DDI_IO_CLASS) + sizeof (FM_EREPORT_CLASS) + \
1377c478bd9Sstevel@tonic-gate 			    DDI_MAX_ERPT_CLASS + 2
1387c478bd9Sstevel@tonic-gate /* Globals */
1397c478bd9Sstevel@tonic-gate int default_dmacache_sz = DEFAULT_DMACACHE_SZ;
1407c478bd9Sstevel@tonic-gate int default_acccache_sz = DEFAULT_ACCCACHE_SZ;
1417c478bd9Sstevel@tonic-gate int ddi_system_fmcap = 0;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static struct i_ddi_fmkstat ddifm_kstat_template = {
1447c478bd9Sstevel@tonic-gate 	{"erpt_dropped", KSTAT_DATA_UINT64 },
145a5667d81SCheng Sean Ye 	{"fm_cache_miss", KSTAT_DATA_UINT64 },
1467c478bd9Sstevel@tonic-gate 	{"fm_cache_full", KSTAT_DATA_UINT64 },
1477c478bd9Sstevel@tonic-gate 	{"acc_err", KSTAT_DATA_UINT64 },
1487c478bd9Sstevel@tonic-gate 	{"dma_err", KSTAT_DATA_UINT64 }
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * Update the service state following the detection of an
1537c478bd9Sstevel@tonic-gate  * error.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate void
1567c478bd9Sstevel@tonic-gate ddi_fm_service_impact(dev_info_t *dip, int svc_impact)
1577c478bd9Sstevel@tonic-gate {
15800d0963fSdilpreet 	uint64_t ena;
15900d0963fSdilpreet 	char buf[FM_MAX_CLASS];
16000d0963fSdilpreet 
16100d0963fSdilpreet 	ena = fm_ena_generate(0, FM_ENA_FMT1);
1627c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
1637c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
1647c478bd9Sstevel@tonic-gate 		switch (svc_impact) {
1657c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_LOST:
1667c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DOWN(dip);
16700d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
16800d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_LOST);
16900d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
17000d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
17100d0963fSdilpreet 			    NULL);
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_DEGRADED:
1747c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DEGRADED(dip);
17500d0963fSdilpreet 			if (DEVI_IS_DEVICE_DEGRADED(dip)) {
17600d0963fSdilpreet 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
17700d0963fSdilpreet 				    DDI_FM_SERVICE_IMPACT,
17800d0963fSdilpreet 				    DDI_FM_SERVICE_DEGRADED);
17900d0963fSdilpreet 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
18000d0963fSdilpreet 				    FM_VERSION, DATA_TYPE_UINT8,
18100d0963fSdilpreet 				    FM_EREPORT_VERS0, NULL);
18200d0963fSdilpreet 			} else if (DEVI_IS_DEVICE_DOWN(dip)) {
18300d0963fSdilpreet 				(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
18400d0963fSdilpreet 				    DDI_FM_SERVICE_IMPACT,
18500d0963fSdilpreet 				    DDI_FM_SERVICE_LOST);
18600d0963fSdilpreet 				ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
18700d0963fSdilpreet 				    FM_VERSION, DATA_TYPE_UINT8,
18800d0963fSdilpreet 				    FM_EREPORT_VERS0, NULL);
18900d0963fSdilpreet 			}
1907c478bd9Sstevel@tonic-gate 			break;
1917c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_RESTORED:
1927c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_UP(dip);
19300d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
19400d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_RESTORED);
19500d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
19600d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
19700d0963fSdilpreet 			    NULL);
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_UNAFFECTED:
20000d0963fSdilpreet 			(void) snprintf(buf, FM_MAX_CLASS, "%s.%s",
20100d0963fSdilpreet 			    DDI_FM_SERVICE_IMPACT, DDI_FM_SERVICE_UNAFFECTED);
20200d0963fSdilpreet 			ddi_fm_ereport_post(dip, buf, ena, DDI_NOSLEEP,
20300d0963fSdilpreet 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
20400d0963fSdilpreet 			    NULL);
20500d0963fSdilpreet 			break;
2067c478bd9Sstevel@tonic-gate 		default:
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate void
2147c478bd9Sstevel@tonic-gate i_ddi_drv_ereport_post(dev_info_t *dip, const char *error_class,
2157c478bd9Sstevel@tonic-gate     nvlist_t *errp, int sflag)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	int i;
2187c478bd9Sstevel@tonic-gate 	int depth;
2197c478bd9Sstevel@tonic-gate 	char classp[DDI_DVR_MAX_CLASS];
2207c478bd9Sstevel@tonic-gate 	caddr_t stkp;
2217c478bd9Sstevel@tonic-gate 	char *buf;
2227c478bd9Sstevel@tonic-gate 	char **stkpp;
2237c478bd9Sstevel@tonic-gate 	char *sym;
2247c478bd9Sstevel@tonic-gate 	pc_t stack[DDI_FM_STKDEPTH];
2257c478bd9Sstevel@tonic-gate 	ulong_t off;
2267c478bd9Sstevel@tonic-gate 	dev_info_t *root_dip = ddi_root_node();
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(root_dip)))
2297c478bd9Sstevel@tonic-gate 		return;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	(void) snprintf(classp, DDI_DVR_MAX_CLASS, "%s%s", DVR_ERPT,
2327c478bd9Sstevel@tonic-gate 	    error_class);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (sflag == DDI_SLEEP) {
2357c478bd9Sstevel@tonic-gate 		depth = getpcstack(stack, DDI_FM_STKDEPTH);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		/* Allocate array of char * for nvlist payload */
2387c478bd9Sstevel@tonic-gate 		stkpp = (char **)kmem_alloc(depth * sizeof (char *), KM_SLEEP);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		/*
2417c478bd9Sstevel@tonic-gate 		 * Allocate temporary 64-bit aligned buffer for stack
2427c478bd9Sstevel@tonic-gate 		 * symbol strings
2437c478bd9Sstevel@tonic-gate 		 */
2447c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(depth * DDI_FM_SYM_SZ, KM_SLEEP);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		stkp = buf;
2477c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; ++i) {
2487c478bd9Sstevel@tonic-gate 			sym = kobj_getsymname(stack[i], &off);
2497c478bd9Sstevel@tonic-gate 			(void) snprintf(stkp, DDI_FM_SYM_SZ,
2507c478bd9Sstevel@tonic-gate 			    "\t%s+%lx\n", sym ? sym : "?", off);
2517c478bd9Sstevel@tonic-gate 			stkpp[i] = stkp;
2527c478bd9Sstevel@tonic-gate 			stkp += DDI_FM_SYM_SZ;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		if (errp)
2567c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2577c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2587c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2597c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2607c478bd9Sstevel@tonic-gate 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
2617c478bd9Sstevel@tonic-gate 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
2627c478bd9Sstevel@tonic-gate 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
2637c478bd9Sstevel@tonic-gate 		else
2647c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2657c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2667c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2677c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2687c478bd9Sstevel@tonic-gate 			    DVR_STACK_DEPTH, DATA_TYPE_UINT32, depth,
2697c478bd9Sstevel@tonic-gate 			    DVR_STACK, DATA_TYPE_STRING_ARRAY, depth, stkpp,
2707c478bd9Sstevel@tonic-gate 			    NULL);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		kmem_free(stkpp, depth * sizeof (char *));
2737c478bd9Sstevel@tonic-gate 		kmem_free(buf, depth * DDI_FM_SYM_SZ);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	} else {
2767c478bd9Sstevel@tonic-gate 		if (errp)
2777c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2787c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2797c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2807c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2817c478bd9Sstevel@tonic-gate 			    DVR_ERR_SPECIFIC, DATA_TYPE_NVLIST, errp, NULL);
2827c478bd9Sstevel@tonic-gate 		else
2837c478bd9Sstevel@tonic-gate 			ddi_fm_ereport_post(root_dip,
2847c478bd9Sstevel@tonic-gate 			    classp, fm_ena_generate(0, FM_ENA_FMT1), sflag,
2857c478bd9Sstevel@tonic-gate 			    FM_VERSION, DATA_TYPE_UINT8, 0,
2867c478bd9Sstevel@tonic-gate 			    DVR_NAME, DATA_TYPE_STRING, ddi_driver_name(dip),
2877c478bd9Sstevel@tonic-gate 			    NULL);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
292602ca9eaScth  * fm_dev_ereport_postv: Common consolidation private interface to
293602ca9eaScth  * post a device tree oriented dev_scheme ereport. The device tree is
294602ca9eaScth  * composed of the following entities: devinfo nodes, minor nodes, and
295602ca9eaScth  * pathinfo nodes. All entities are associated with some devinfo node,
296602ca9eaScth  * either directly or indirectly. The intended devinfo node association
297602ca9eaScth  * for the ereport is communicated by the 'dip' argument. A minor node,
298602ca9eaScth  * an entity below 'dip', is represented by a non-null 'minor_name'
299602ca9eaScth  * argument. An application specific caller, like scsi_fm_ereport_post,
300602ca9eaScth  * can override the devinfo path with a pathinfo path via a non-null
301602ca9eaScth  * 'devpath' argument - in this case 'dip' is the MPXIO client node and
302602ca9eaScth  * devpath should be the path through the pHCI devinfo node to the
303602ca9eaScth  * pathinfo node.
304602ca9eaScth  *
305602ca9eaScth  * This interface also allows the caller to decide if the error being
306602ca9eaScth  * reported is know to be associated with a specific device identity
307602ca9eaScth  * via the 'devid' argument. The caller needs to control wether the
308602ca9eaScth  * devid appears as an authority in the FMRI because for some types of
309602ca9eaScth  * errors, like transport errors, the identity of the device on the
310602ca9eaScth  * other end of the transport is not guaranteed to be the current
311602ca9eaScth  * identity of the dip. For transport errors the caller should specify
312602ca9eaScth  * a NULL devid, even when there is a valid devid associated with the dip.
313602ca9eaScth  *
314602ca9eaScth  * The ddi_fm_ereport_post() implementation calls this interface with
315602ca9eaScth  * just a dip: devpath, minor_name, and devid are all NULL. The
316602ca9eaScth  * scsi_fm_ereport_post() implementation may call this interface with
317602ca9eaScth  * non-null devpath, minor_name, and devid arguments depending on
318602ca9eaScth  * wether MPXIO is enabled, and wether a transport or non-transport
319602ca9eaScth  * error is being posted.
320602ca9eaScth  */
321602ca9eaScth void
322602ca9eaScth fm_dev_ereport_postv(dev_info_t *dip, dev_info_t *eqdip,
323602ca9eaScth     const char *devpath, const char *minor_name, const char *devid,
324602ca9eaScth     const char *error_class, uint64_t ena, int sflag, va_list ap)
325602ca9eaScth {
3260b1ecf75Scth 	nv_alloc_t		*nva = NULL;
3270b1ecf75Scth 	struct i_ddi_fmhdl	*fmhdl = NULL;
328602ca9eaScth 	errorq_elem_t		*eqep;
329602ca9eaScth 	nvlist_t		*ereport = NULL;
330602ca9eaScth 	nvlist_t		*detector = NULL;
331602ca9eaScth 	char			*name;
332602ca9eaScth 	data_type_t		type;
333602ca9eaScth 	uint8_t			version;
334602ca9eaScth 	char			class[ERPT_CLASS_SZ];
335602ca9eaScth 	char			path[MAXPATHLEN];
336602ca9eaScth 
337602ca9eaScth 	ASSERT(dip && eqdip && error_class);
338602ca9eaScth 
339602ca9eaScth 	/*
340602ca9eaScth 	 * This interface should be called with a fm_capable eqdip. The
341602ca9eaScth 	 * ddi_fm_ereport_post* interfaces call with eqdip == dip,
342602ca9eaScth 	 * ndi_fm_ereport_post* interfaces call with eqdip == ddi_parent(dip).
343602ca9eaScth 	 */
344602ca9eaScth 	if (!DDI_FM_EREPORT_CAP(ddi_fm_capable(eqdip)))
345602ca9eaScth 		goto err;
346602ca9eaScth 
347602ca9eaScth 	/* get ereport nvlist handle */
348602ca9eaScth 	if ((sflag == DDI_SLEEP) && !panicstr) {
349602ca9eaScth 		/*
350602ca9eaScth 		 * Driver defect - should not call with DDI_SLEEP while in
351602ca9eaScth 		 * interrupt context.
352602ca9eaScth 		 */
353602ca9eaScth 		if (servicing_interrupt()) {
354602ca9eaScth 			i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, sflag);
355602ca9eaScth 			goto err;
356602ca9eaScth 		}
357602ca9eaScth 
358602ca9eaScth 		/* Use normal interfaces to allocate memory. */
359602ca9eaScth 		if ((ereport = fm_nvlist_create(NULL)) == NULL)
360602ca9eaScth 			goto err;
3610b1ecf75Scth 		ASSERT(nva == NULL);
362602ca9eaScth 	} else {
363602ca9eaScth 		/* Use errorq interfaces to avoid memory allocation. */
364602ca9eaScth 		fmhdl = DEVI(eqdip)->devi_fmhdl;
365602ca9eaScth 		ASSERT(fmhdl);
366602ca9eaScth 		eqep = errorq_reserve(fmhdl->fh_errorq);
367602ca9eaScth 		if (eqep == NULL)
368602ca9eaScth 			goto err;
369602ca9eaScth 
370602ca9eaScth 		ereport = errorq_elem_nvl(fmhdl->fh_errorq, eqep);
371602ca9eaScth 		nva = errorq_elem_nva(fmhdl->fh_errorq, eqep);
372602ca9eaScth 		ASSERT(nva);
373602ca9eaScth 	}
374602ca9eaScth 	ASSERT(ereport);
375602ca9eaScth 
376602ca9eaScth 	/*
377602ca9eaScth 	 * Form parts of an ereport:
378602ca9eaScth 	 *	A: version
379602ca9eaScth 	 * 	B: error_class
380602ca9eaScth 	 *	C: ena
381602ca9eaScth 	 *	D: detector	(path and optional devid authority)
382602ca9eaScth 	 *	E: payload
383602ca9eaScth 	 *
384602ca9eaScth 	 * A: ereport version: first payload tuple must be the version.
385602ca9eaScth 	 */
386602ca9eaScth 	name = va_arg(ap, char *);
387602ca9eaScth 	type = va_arg(ap, data_type_t);
388602ca9eaScth 	version = va_arg(ap, uint_t);
389602ca9eaScth 	if ((strcmp(name, FM_VERSION) != 0) || (type != DATA_TYPE_UINT8)) {
390602ca9eaScth 		i_ddi_drv_ereport_post(dip, DVR_EVER, NULL, sflag);
391602ca9eaScth 		goto err;
392602ca9eaScth 	}
393602ca9eaScth 
394602ca9eaScth 	/* B: ereport error_class: add "io." prefix to class. */
395602ca9eaScth 	(void) snprintf(class, ERPT_CLASS_SZ, "%s.%s",
396602ca9eaScth 	    DDI_IO_CLASS, error_class);
397602ca9eaScth 
398602ca9eaScth 	/* C: ereport ena: if not passed in, generate new ena. */
399602ca9eaScth 	if (ena == 0)
400602ca9eaScth 		ena = fm_ena_generate(0, FM_ENA_FMT1);
401602ca9eaScth 
402602ca9eaScth 	/* D: detector: form dev scheme fmri with path and devid. */
403602ca9eaScth 	if (devpath) {
404602ca9eaScth 		(void) strlcpy(path, devpath, sizeof (path));
405602ca9eaScth 	} else {
406602ca9eaScth 		/* derive devpath from dip */
407602ca9eaScth 		if (dip == ddi_root_node())
408602ca9eaScth 			(void) strcpy(path, "/");
409602ca9eaScth 		else
410602ca9eaScth 			(void) ddi_pathname(dip, path);
411602ca9eaScth 	}
412602ca9eaScth 	if (minor_name) {
413602ca9eaScth 		(void) strlcat(path, ":", sizeof (path));
414602ca9eaScth 		(void) strlcat(path, minor_name, sizeof (path));
415602ca9eaScth 	}
416602ca9eaScth 	detector = fm_nvlist_create(nva);
417602ca9eaScth 	fm_fmri_dev_set(detector, FM_DEV_SCHEME_VERSION, NULL, path, devid);
418602ca9eaScth 
419602ca9eaScth 	/* Pull parts of ereport together into ereport. */
420602ca9eaScth 	fm_ereport_set(ereport, version, class, ena, detector, NULL);
421602ca9eaScth 
422602ca9eaScth 	/* Add the payload to ereport. */
423602ca9eaScth 	name = va_arg(ap, char *);
424602ca9eaScth 	(void) i_fm_payload_set(ereport, name, ap);
425602ca9eaScth 
426602ca9eaScth 	/* Post the ereport. */
427602ca9eaScth 	if (nva)
428602ca9eaScth 		errorq_commit(fmhdl->fh_errorq, eqep, ERRORQ_ASYNC);
429602ca9eaScth 	else
430602ca9eaScth 		fm_ereport_post(ereport, EVCH_SLEEP);
431602ca9eaScth 	goto out;
432602ca9eaScth 
433602ca9eaScth 	/* Count errors as drops. */
434602ca9eaScth err:	if (fmhdl)
435602ca9eaScth 		atomic_add_64(&fmhdl->fh_kstat.fek_erpt_dropped.value.ui64, 1);
436602ca9eaScth 
4370b1ecf75Scth 	/* Free up nvlists if normal interfaces were used to allocate memory */
438602ca9eaScth out:	if (ereport && (nva == NULL))
439602ca9eaScth 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
440602ca9eaScth 	if (detector && (nva == NULL))
441602ca9eaScth 		fm_nvlist_destroy(detector, FM_NVA_FREE);
442602ca9eaScth }
443602ca9eaScth 
444602ca9eaScth /*
4457c478bd9Sstevel@tonic-gate  * Generate an error report for consumption by the Solaris Fault Manager,
446602ca9eaScth  * fmd(1M).  Valid ereport classes are defined in /usr/include/sys/fm/io.
447602ca9eaScth  *
448602ca9eaScth  * The ENA should be set if this error is a result of an error status
449602ca9eaScth  * returned from ddi_dma_err_check() or ddi_acc_err_check().  Otherwise,
450602ca9eaScth  * an ENA value of 0 is appropriate.
4517c478bd9Sstevel@tonic-gate  *
4527c478bd9Sstevel@tonic-gate  * If sflag == DDI_NOSLEEP, ddi_fm_ereport_post () may be called
4537c478bd9Sstevel@tonic-gate  * from user, kernel, interrupt or high-interrupt context.  Otherwise,
4547c478bd9Sstevel@tonic-gate  * ddi_fm_ereport_post() must be called from user or kernel context.
455602ca9eaScth  *
456602ca9eaScth  * The ndi_interfaces are provided for use by nexus drivers to post
457602ca9eaScth  * ereports about children who may not themselves be fm_capable.
458602ca9eaScth  *
459602ca9eaScth  * All interfaces end up in the common fm_dev_ereport_postv code above.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate void
462602ca9eaScth ddi_fm_ereport_post(dev_info_t *dip,
463602ca9eaScth     const char *error_class, uint64_t ena, int sflag, ...)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	va_list ap;
4667c478bd9Sstevel@tonic-gate 
467602ca9eaScth 	ASSERT(dip && error_class);
4687c478bd9Sstevel@tonic-gate 	va_start(ap, sflag);
469602ca9eaScth 	fm_dev_ereport_postv(dip, dip, NULL, NULL, NULL,
470602ca9eaScth 	    error_class, ena, sflag, ap);
4717c478bd9Sstevel@tonic-gate 	va_end(ap);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
474602ca9eaScth void
475602ca9eaScth ndi_fm_ereport_post(dev_info_t *dip,
476602ca9eaScth     const char *error_class, uint64_t ena, int sflag, ...)
477602ca9eaScth {
478602ca9eaScth 	va_list ap;
479602ca9eaScth 
480602ca9eaScth 	ASSERT(dip && error_class && (sflag == DDI_SLEEP));
481602ca9eaScth 	va_start(ap, sflag);
482602ca9eaScth 	fm_dev_ereport_postv(dip, ddi_get_parent(dip), NULL, NULL, NULL,
483602ca9eaScth 	    error_class, ena, sflag, ap);
4847c478bd9Sstevel@tonic-gate 	va_end(ap);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * Driver error handling entry.  Prevents multiple simultaneous calls into
4897c478bd9Sstevel@tonic-gate  * driver error handling callback.
4907c478bd9Sstevel@tonic-gate  *
4917c478bd9Sstevel@tonic-gate  * May be called from a context consistent with the iblock_cookie returned
4927c478bd9Sstevel@tonic-gate  * in ddi_fm_init().
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate void
4957c478bd9Sstevel@tonic-gate i_ddi_fm_handler_enter(dev_info_t *dip)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	mutex_enter(&hdl->fh_lock);
500eae2e508Skrishnae 	hdl->fh_lock_owner = curthread;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Driver error handling exit.
5057c478bd9Sstevel@tonic-gate  *
5067c478bd9Sstevel@tonic-gate  * May be called from a context consistent with the iblock_cookie returned
5077c478bd9Sstevel@tonic-gate  * in ddi_fm_init().
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate void
5107c478bd9Sstevel@tonic-gate i_ddi_fm_handler_exit(dev_info_t *dip)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
5137c478bd9Sstevel@tonic-gate 
514eae2e508Skrishnae 	hdl->fh_lock_owner = NULL;
5157c478bd9Sstevel@tonic-gate 	mutex_exit(&hdl->fh_lock);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
518eae2e508Skrishnae boolean_t
519eae2e508Skrishnae i_ddi_fm_handler_owned(dev_info_t *dip)
520eae2e508Skrishnae {
521eae2e508Skrishnae 	struct i_ddi_fmhdl *hdl = DEVI(dip)->devi_fmhdl;
522eae2e508Skrishnae 
523eae2e508Skrishnae 	return (hdl->fh_lock_owner == curthread);
524eae2e508Skrishnae }
525eae2e508Skrishnae 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * Register a fault manager error handler for this device instance
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  * This function must be called from a driver's attach(9E) routine.
5307c478bd9Sstevel@tonic-gate  */
5317c478bd9Sstevel@tonic-gate void
5327c478bd9Sstevel@tonic-gate ddi_fm_handler_register(dev_info_t *dip, ddi_err_func_t handler,
5337c478bd9Sstevel@tonic-gate     void *impl_data)
5347c478bd9Sstevel@tonic-gate {
5357c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
5367c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *pfmhdl;
5377c478bd9Sstevel@tonic-gate 	struct i_ddi_errhdl *new_eh;
5387c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * Check for proper calling context.
5427c478bd9Sstevel@tonic-gate 	 * The DDI configuration framework does not support
5437c478bd9Sstevel@tonic-gate 	 * DR states to allow checking for proper invocation
5447c478bd9Sstevel@tonic-gate 	 * from a DDI_ATTACH or DDI_RESUME.  This limits context checking
5457c478bd9Sstevel@tonic-gate 	 * to interrupt only.
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	if (servicing_interrupt()) {
5487c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
5497c478bd9Sstevel@tonic-gate 		return;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527aec1d6eScindi 	if (dip == ddi_root_node())
5537aec1d6eScindi 		pdip = dip;
5547aec1d6eScindi 	else
5557c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
5607c478bd9Sstevel@tonic-gate 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
5617c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
5627c478bd9Sstevel@tonic-gate 		return;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	new_eh = kmem_zalloc(sizeof (struct i_ddi_errhdl), KM_SLEEP);
5667c478bd9Sstevel@tonic-gate 	new_eh->eh_func = handler;
5677c478bd9Sstevel@tonic-gate 	new_eh->eh_impl = impl_data;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/* Add dip to parent's target list of registered error handlers */
5707c478bd9Sstevel@tonic-gate 	tgt = kmem_alloc(sizeof (struct i_ddi_fmtgt), KM_SLEEP);
5717c478bd9Sstevel@tonic-gate 	tgt->ft_dip = dip;
5727c478bd9Sstevel@tonic-gate 	tgt->ft_errhdl = new_eh;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(pdip);
5757c478bd9Sstevel@tonic-gate 	pfmhdl = DEVI(pdip)->devi_fmhdl;
5767c478bd9Sstevel@tonic-gate 	ASSERT(pfmhdl);
5777c478bd9Sstevel@tonic-gate 	tgt->ft_next = pfmhdl->fh_tgts;
5787c478bd9Sstevel@tonic-gate 	pfmhdl->fh_tgts = tgt;
5797c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(pdip);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  * Unregister a fault manager error handler for this device instance
5847c478bd9Sstevel@tonic-gate  *
5857c478bd9Sstevel@tonic-gate  * This function must be called from a drivers attach(9E) or detach(9E)
5867c478bd9Sstevel@tonic-gate  * routine.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate void
5897c478bd9Sstevel@tonic-gate ddi_fm_handler_unregister(dev_info_t *dip)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
5927c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *pfmhdl;
5937c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt, **ptgt;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * Check for proper calling context.
5977c478bd9Sstevel@tonic-gate 	 * The DDI configuration framework does not support
5987c478bd9Sstevel@tonic-gate 	 * DR states to allow checking for proper invocation
5997c478bd9Sstevel@tonic-gate 	 * from a DDI_DETACH or DDI_SUSPEND.  This limits context checking
6007c478bd9Sstevel@tonic-gate 	 * to interrupt only.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	if (servicing_interrupt()) {
6037c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
6047c478bd9Sstevel@tonic-gate 		return;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077aec1d6eScindi 	if (dip == ddi_root_node())
6087aec1d6eScindi 		pdip = dip;
6097aec1d6eScindi 	else
6107c478bd9Sstevel@tonic-gate 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (!(DDI_FM_ERRCB_CAP(ddi_fm_capable(dip)) &&
6157c478bd9Sstevel@tonic-gate 	    DDI_FM_ERRCB_CAP(ddi_fm_capable(pdip)))) {
6167c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_SLEEP);
6177c478bd9Sstevel@tonic-gate 		return;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(pdip);
6217c478bd9Sstevel@tonic-gate 	pfmhdl = DEVI(pdip)->devi_fmhdl;
6227c478bd9Sstevel@tonic-gate 	ASSERT(pfmhdl);
6237c478bd9Sstevel@tonic-gate 	ptgt = &pfmhdl->fh_tgts;
6247c478bd9Sstevel@tonic-gate 	for (tgt = pfmhdl->fh_tgts; tgt != NULL; tgt = tgt->ft_next) {
6257c478bd9Sstevel@tonic-gate 		if (dip == tgt->ft_dip) {
6267c478bd9Sstevel@tonic-gate 			*ptgt = tgt->ft_next;
6277c478bd9Sstevel@tonic-gate 			kmem_free(tgt->ft_errhdl, sizeof (struct i_ddi_errhdl));
6287c478bd9Sstevel@tonic-gate 			kmem_free(tgt, sizeof (struct i_ddi_fmtgt));
6297c478bd9Sstevel@tonic-gate 			break;
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 		ptgt = &tgt->ft_next;
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(pdip);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Initialize Fault Management capabilities for this device instance (dip).
6407c478bd9Sstevel@tonic-gate  * When called with the following capabilities, data structures neccessary
6417c478bd9Sstevel@tonic-gate  * for fault management activities are allocated and initialized.
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  *	DDI_FM_EREPORT_CAPABLE - initialize ereport errorq and ereport
6447c478bd9Sstevel@tonic-gate  *				capable driver property.
6457c478bd9Sstevel@tonic-gate  *
6467c478bd9Sstevel@tonic-gate  *	DDI_FM_ERRCB_CAPABLE - check with parent for ability to register
6477c478bd9Sstevel@tonic-gate  *				an error handler.
6487c478bd9Sstevel@tonic-gate  *
6497c478bd9Sstevel@tonic-gate  *	DDI_FM_ACCCHK_CAPABLE - initialize access handle cache and acc-chk
6507c478bd9Sstevel@tonic-gate  *				driver property
6517c478bd9Sstevel@tonic-gate  *
6527c478bd9Sstevel@tonic-gate  *	DDI_FM_DMACHK_CAPABLE - initialize dma handle cache and dma-chk
6537c478bd9Sstevel@tonic-gate  *				driver property
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * A driver's FM capability level may not exceed that of its parent or
6567c478bd9Sstevel@tonic-gate  * system-wide FM capability.  The available capability level for this
6577c478bd9Sstevel@tonic-gate  * device instance is returned in *fmcap.
6587c478bd9Sstevel@tonic-gate  *
6597c478bd9Sstevel@tonic-gate  * This function must be called from a driver's attach(9E) entry point.
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate void
6627aec1d6eScindi ddi_fm_init(dev_info_t *dip, int *fmcap, ddi_iblock_cookie_t *ibcp)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
6657c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl;
6667aec1d6eScindi 	ddi_iblock_cookie_t ibc;
6677c478bd9Sstevel@tonic-gate 	int pcap, newcap = DDI_FM_NOT_CAPABLE;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(dip)) {
6707c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
6717c478bd9Sstevel@tonic-gate 		*fmcap = DDI_FM_NOT_CAPABLE;
6727c478bd9Sstevel@tonic-gate 		return;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	if (DDI_FM_DEFAULT_CAP(*fmcap))
6767c478bd9Sstevel@tonic-gate 		return;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * Check parent for supported FM level
6807c478bd9Sstevel@tonic-gate 	 * and correct error handling PIL
6817c478bd9Sstevel@tonic-gate 	 */
6827c478bd9Sstevel@tonic-gate 	if (dip != ddi_root_node()) {
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Initialize the default ibc.  The parent may change it
6867c478bd9Sstevel@tonic-gate 		 * depending upon its capabilities.
6877c478bd9Sstevel@tonic-gate 		 */
6887aec1d6eScindi 		ibc = (ddi_iblock_cookie_t)ipltospl(FM_ERR_PIL);
6897c478bd9Sstevel@tonic-gate 
6907aec1d6eScindi 		pcap = i_ndi_busop_fm_init(dip, *fmcap, &ibc);
6917c478bd9Sstevel@tonic-gate 	} else {
6927c478bd9Sstevel@tonic-gate 		pcap = *fmcap;
6937aec1d6eScindi 		ibc = *ibcp;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/* Initialize the per-device instance FM handle */
6977c478bd9Sstevel@tonic-gate 	fmhdl = kmem_zalloc(sizeof (struct i_ddi_fmhdl), KM_SLEEP);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if ((fmhdl->fh_ksp = kstat_create((char *)ddi_driver_name(dip),
7007c478bd9Sstevel@tonic-gate 	    ddi_get_instance(dip), "fm", "misc",
7017c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, sizeof (struct i_ddi_fmkstat) /
7027c478bd9Sstevel@tonic-gate 	    sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) == NULL) {
7037c478bd9Sstevel@tonic-gate 		mutex_destroy(&fmhdl->fh_lock);
7047c478bd9Sstevel@tonic-gate 		kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
7057c478bd9Sstevel@tonic-gate 		*fmcap = DDI_FM_NOT_CAPABLE;
7067c478bd9Sstevel@tonic-gate 		return;
7077c478bd9Sstevel@tonic-gate 	}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	bcopy(&ddifm_kstat_template, &fmhdl->fh_kstat,
7107c478bd9Sstevel@tonic-gate 	    sizeof (struct i_ddi_fmkstat));
7117c478bd9Sstevel@tonic-gate 	fmhdl->fh_ksp->ks_data = &fmhdl->fh_kstat;
7127c478bd9Sstevel@tonic-gate 	fmhdl->fh_ksp->ks_private = fmhdl;
7137c478bd9Sstevel@tonic-gate 	kstat_install(fmhdl->fh_ksp);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	fmhdl->fh_dma_cache = NULL;
7167c478bd9Sstevel@tonic-gate 	fmhdl->fh_acc_cache = NULL;
7177c478bd9Sstevel@tonic-gate 	fmhdl->fh_tgts = NULL;
7187c478bd9Sstevel@tonic-gate 	fmhdl->fh_dip = dip;
7197aec1d6eScindi 	fmhdl->fh_ibc = ibc;
7207c478bd9Sstevel@tonic-gate 	mutex_init(&fmhdl->fh_lock, NULL, MUTEX_DRIVER, fmhdl->fh_ibc);
7217c478bd9Sstevel@tonic-gate 	devi->devi_fmhdl = fmhdl;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * Initialize support for ereport generation
7257c478bd9Sstevel@tonic-gate 	 */
72600d0963fSdilpreet 	if (DDI_FM_EREPORT_CAP(*fmcap) && DDI_FM_EREPORT_CAP(pcap)) {
7277c478bd9Sstevel@tonic-gate 		fmhdl->fh_errorq = ereport_errorq;
72800d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7297c478bd9Sstevel@tonic-gate 		    "fm-ereport-capable", 0) == 0)
73000d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7317c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-ereport-capable", NULL, 0);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_EREPORT_CAPABLE;
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * Need cooperation of the parent for error handling
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (DDI_FM_ERRCB_CAP(*fmcap) && DDI_FM_ERRCB_CAP(pcap)) {
74100d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7427c478bd9Sstevel@tonic-gate 		    "fm-errcb-capable", 0) == 0)
74300d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7447c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-errcb-capable", NULL, 0);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_ERRCB_CAPABLE;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * Support for DMA and Access error handling
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (DDI_FM_DMA_ERR_CAP(*fmcap) && DDI_FM_DMA_ERR_CAP(pcap)) {
7547aec1d6eScindi 		i_ndi_fmc_create(&fmhdl->fh_dma_cache, 2, ibc);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		/* Set-up dma chk capability prop */
75700d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7587c478bd9Sstevel@tonic-gate 		    "fm-dmachk-capable", 0) == 0)
75900d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7607c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-dmachk-capable", NULL, 0);
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_DMACHK_CAPABLE;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (DDI_FM_ACC_ERR_CAP(*fmcap) && DDI_FM_ACC_ERR_CAP(pcap)) {
7667aec1d6eScindi 		i_ndi_fmc_create(&fmhdl->fh_acc_cache, 2, ibc);
7677c478bd9Sstevel@tonic-gate 		/* Set-up dma chk capability prop */
76800d0963fSdilpreet 		if (ddi_getprop(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
7697c478bd9Sstevel@tonic-gate 		    "fm-accchk-capable", 0) == 0)
77000d0963fSdilpreet 			(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
7717c478bd9Sstevel@tonic-gate 			    DDI_PROP_CANSLEEP, "fm-accchk-capable", NULL, 0);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		newcap |= DDI_FM_ACCCHK_CAPABLE;
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * Return the capability support available
7787c478bd9Sstevel@tonic-gate 	 * to this driver instance
7797c478bd9Sstevel@tonic-gate 	 */
7807c478bd9Sstevel@tonic-gate 	fmhdl->fh_cap = newcap;
7817c478bd9Sstevel@tonic-gate 	*fmcap = newcap;
7827aec1d6eScindi 
7837aec1d6eScindi 	if (ibcp != NULL)
7847aec1d6eScindi 		*ibcp = ibc;
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Finalize Fault Management activities for this device instance.
7897c478bd9Sstevel@tonic-gate  * Outstanding IO transaction must be completed prior to calling
7907c478bd9Sstevel@tonic-gate  * this routine.  All previously allocated resources and error handler
7917c478bd9Sstevel@tonic-gate  * registration are cleared and deallocated.
7927c478bd9Sstevel@tonic-gate  *
7937c478bd9Sstevel@tonic-gate  * This function must be called from a driver's detach(9E) entry point.
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate void
7967c478bd9Sstevel@tonic-gate ddi_fm_fini(dev_info_t *dip)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
7997c478bd9Sstevel@tonic-gate 
80000d0963fSdilpreet 	ASSERT(fmhdl);
80100d0963fSdilpreet 
8027c478bd9Sstevel@tonic-gate 	if (!(DEVI_IS_DETACHING(dip) || DEVI_IS_ATTACHING(dip))) {
8037c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_ECONTEXT, NULL, DDI_NOSLEEP);
8047c478bd9Sstevel@tonic-gate 		return;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	kstat_delete(fmhdl->fh_ksp);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (DDI_FM_EREPORT_CAP(fmhdl->fh_cap)) {
81000d0963fSdilpreet 		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8117c478bd9Sstevel@tonic-gate 		    "fm-ereport-capable");
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (dip != ddi_root_node()) {
815cecfa358Sstephh 		if (DDI_FM_ERRCB_CAP(fmhdl->fh_cap)) {
8167c478bd9Sstevel@tonic-gate 			ddi_fm_handler_unregister(dip);
817cecfa358Sstephh 			(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
818cecfa358Sstephh 			    "fm-errcb-capable");
819cecfa358Sstephh 		}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		if (DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap) ||
8227c478bd9Sstevel@tonic-gate 		    DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
8237c478bd9Sstevel@tonic-gate 			if (fmhdl->fh_dma_cache != NULL) {
8247c478bd9Sstevel@tonic-gate 				i_ndi_fmc_destroy(fmhdl->fh_dma_cache);
82500d0963fSdilpreet 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8267c478bd9Sstevel@tonic-gate 				    "fm-dmachk-capable");
8277c478bd9Sstevel@tonic-gate 			}
8287c478bd9Sstevel@tonic-gate 			if (fmhdl->fh_acc_cache != NULL) {
8297c478bd9Sstevel@tonic-gate 				i_ndi_fmc_destroy(fmhdl->fh_acc_cache);
83000d0963fSdilpreet 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
8317c478bd9Sstevel@tonic-gate 				    "fm-accachk-capable");
8327c478bd9Sstevel@tonic-gate 			}
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		i_ndi_busop_fm_fini(dip);
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	kmem_free(fmhdl, sizeof (struct i_ddi_fmhdl));
8397c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_fmhdl = NULL;
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Return the fault management capability level for this device instance.
8447c478bd9Sstevel@tonic-gate  *
8457c478bd9Sstevel@tonic-gate  * This function may be called from user, kernel, or interrupt context.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate int
8487c478bd9Sstevel@tonic-gate ddi_fm_capable(dev_info_t *dip)
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(dip)->devi_fmhdl;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (fmhdl == NULL)
8537c478bd9Sstevel@tonic-gate 		return (DDI_FM_NOT_CAPABLE);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	return (fmhdl->fh_cap);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * Routines to set and get error information for/from an access or dma handle
8607c478bd9Sstevel@tonic-gate  *
8617c478bd9Sstevel@tonic-gate  * These routines may be called from user, kernel, and interrupt contexts.
8627c478bd9Sstevel@tonic-gate  */
863*837c1ac4SStephen Hanson 
864*837c1ac4SStephen Hanson static void
865*837c1ac4SStephen Hanson ddi_fm_acc_err_get_fail(ddi_acc_handle_t handle)
866*837c1ac4SStephen Hanson {
867*837c1ac4SStephen Hanson 	ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
868*837c1ac4SStephen Hanson 
869*837c1ac4SStephen Hanson 	i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
870*837c1ac4SStephen Hanson 	cmn_err(CE_PANIC, "ddi_fm_acc_err_get: Invalid driver version\n");
871*837c1ac4SStephen Hanson }
872*837c1ac4SStephen Hanson 
8737c478bd9Sstevel@tonic-gate void
8747c478bd9Sstevel@tonic-gate ddi_fm_acc_err_get(ddi_acc_handle_t handle, ddi_fm_error_t *de, int version)
8757c478bd9Sstevel@tonic-gate {
87600d0963fSdilpreet 	ndi_err_t *errp;
87700d0963fSdilpreet 
87800d0963fSdilpreet 	if (handle == NULL)
87900d0963fSdilpreet 		return;
8807c478bd9Sstevel@tonic-gate 
8818aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
882*837c1ac4SStephen Hanson 		ddi_fm_acc_err_get_fail(handle);
883*837c1ac4SStephen Hanson 		return;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
88600d0963fSdilpreet 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
887*837c1ac4SStephen Hanson 	if (errp->err_status == DDI_FM_OK) {
888*837c1ac4SStephen Hanson 		if (de->fme_status != DDI_FM_OK)
889*837c1ac4SStephen Hanson 			de->fme_status = DDI_FM_OK;
890*837c1ac4SStephen Hanson 		return;
891*837c1ac4SStephen Hanson 	}
8927c478bd9Sstevel@tonic-gate 	de->fme_status = errp->err_status;
8937c478bd9Sstevel@tonic-gate 	de->fme_ena = errp->err_ena;
8947c478bd9Sstevel@tonic-gate 	de->fme_flag = errp->err_expected;
8957c478bd9Sstevel@tonic-gate 	de->fme_acc_handle = handle;
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate void
899*837c1ac4SStephen Hanson ddi_fm_dma_err_get_fail(ddi_dma_handle_t handle)
900*837c1ac4SStephen Hanson {
901*837c1ac4SStephen Hanson 	i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
902*837c1ac4SStephen Hanson 	    DVR_EVER, NULL, DDI_NOSLEEP);
903*837c1ac4SStephen Hanson 	cmn_err(CE_PANIC, "ddi_fm_dma_err_get: Invalid driver version\n");
904*837c1ac4SStephen Hanson }
905*837c1ac4SStephen Hanson 
906*837c1ac4SStephen Hanson void
9077c478bd9Sstevel@tonic-gate ddi_fm_dma_err_get(ddi_dma_handle_t handle, ddi_fm_error_t *de, int version)
9087c478bd9Sstevel@tonic-gate {
90900d0963fSdilpreet 	ndi_err_t *errp;
91000d0963fSdilpreet 
91100d0963fSdilpreet 	if (handle == NULL)
91200d0963fSdilpreet 		return;
9137c478bd9Sstevel@tonic-gate 
9148aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
915*837c1ac4SStephen Hanson 		ddi_fm_dma_err_get_fail(handle);
916*837c1ac4SStephen Hanson 		return;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 
91900d0963fSdilpreet 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
92000d0963fSdilpreet 
921*837c1ac4SStephen Hanson 	if (errp->err_status == DDI_FM_OK) {
922*837c1ac4SStephen Hanson 		if (de->fme_status != DDI_FM_OK)
923*837c1ac4SStephen Hanson 			de->fme_status = DDI_FM_OK;
924*837c1ac4SStephen Hanson 		return;
925*837c1ac4SStephen Hanson 	}
9267c478bd9Sstevel@tonic-gate 	de->fme_status = errp->err_status;
9277c478bd9Sstevel@tonic-gate 	de->fme_ena = errp->err_ena;
9287c478bd9Sstevel@tonic-gate 	de->fme_flag = errp->err_expected;
9297c478bd9Sstevel@tonic-gate 	de->fme_dma_handle = handle;
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate void
933*837c1ac4SStephen Hanson ddi_fm_acc_err_clear_fail(ddi_acc_handle_t handle)
934*837c1ac4SStephen Hanson {
935*837c1ac4SStephen Hanson 	ddi_acc_hdl_t *hp = impl_acc_hdl_get(handle);
936*837c1ac4SStephen Hanson 
937*837c1ac4SStephen Hanson 	i_ddi_drv_ereport_post(hp->ah_dip, DVR_EVER, NULL, DDI_NOSLEEP);
938*837c1ac4SStephen Hanson 	cmn_err(CE_PANIC, "ddi_fm_acc_err_clear: Invalid driver version\n");
939*837c1ac4SStephen Hanson }
940*837c1ac4SStephen Hanson 
941*837c1ac4SStephen Hanson void
94200d0963fSdilpreet ddi_fm_acc_err_clear(ddi_acc_handle_t handle, int version)
94300d0963fSdilpreet {
94400d0963fSdilpreet 	ndi_err_t *errp;
94500d0963fSdilpreet 
94600d0963fSdilpreet 	if (handle == NULL)
94700d0963fSdilpreet 		return;
94800d0963fSdilpreet 
9498aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
950*837c1ac4SStephen Hanson 		ddi_fm_acc_err_clear_fail(handle);
951*837c1ac4SStephen Hanson 		return;
95200d0963fSdilpreet 	}
95300d0963fSdilpreet 
95400d0963fSdilpreet 	errp = ((ddi_acc_impl_t *)handle)->ahi_err;
95500d0963fSdilpreet 	errp->err_status = DDI_FM_OK;
95600d0963fSdilpreet 	errp->err_ena = 0;
95700d0963fSdilpreet 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
95800d0963fSdilpreet }
95900d0963fSdilpreet 
96000d0963fSdilpreet void
961*837c1ac4SStephen Hanson ddi_fm_dma_err_clear_fail(ddi_dma_handle_t handle)
962*837c1ac4SStephen Hanson {
963*837c1ac4SStephen Hanson 	i_ddi_drv_ereport_post(((ddi_dma_impl_t *)handle)->dmai_rdip,
964*837c1ac4SStephen Hanson 	    DVR_EVER, NULL, DDI_NOSLEEP);
965*837c1ac4SStephen Hanson 	cmn_err(CE_PANIC, "ddi_fm_dma_err_clear: Invalid driver version\n");
966*837c1ac4SStephen Hanson }
967*837c1ac4SStephen Hanson 
968*837c1ac4SStephen Hanson void
96900d0963fSdilpreet ddi_fm_dma_err_clear(ddi_dma_handle_t handle, int version)
97000d0963fSdilpreet {
97100d0963fSdilpreet 	ndi_err_t *errp;
97200d0963fSdilpreet 
97300d0963fSdilpreet 	if (handle == NULL)
97400d0963fSdilpreet 		return;
97500d0963fSdilpreet 
9768aec9182Sstephh 	if (version != DDI_FME_VER0 && version != DDI_FME_VER1) {
977*837c1ac4SStephen Hanson 		ddi_fm_dma_err_clear_fail(handle);
978*837c1ac4SStephen Hanson 		return;
97900d0963fSdilpreet 	}
98000d0963fSdilpreet 
98100d0963fSdilpreet 	errp = &((ddi_dma_impl_t *)handle)->dmai_error;
98200d0963fSdilpreet 
98300d0963fSdilpreet 	errp->err_status = DDI_FM_OK;
98400d0963fSdilpreet 	errp->err_ena = 0;
98500d0963fSdilpreet 	errp->err_expected = DDI_FM_ERR_UNEXPECTED;
98600d0963fSdilpreet }
98700d0963fSdilpreet 
98800d0963fSdilpreet void
9897c478bd9Sstevel@tonic-gate i_ddi_fm_acc_err_set(ddi_acc_handle_t handle, uint64_t ena, int status,
9907c478bd9Sstevel@tonic-gate     int flag)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hdlp = impl_acc_hdl_get(handle);
9937c478bd9Sstevel@tonic-gate 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
9947c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->ah_dip)->devi_fmhdl;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_ena = ena;
9977c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_status = status;
9987c478bd9Sstevel@tonic-gate 	i_hdlp->ahi_err->err_expected = flag;
9997c478bd9Sstevel@tonic-gate 	atomic_add_64(&fmhdl->fh_kstat.fek_acc_err.value.ui64, 1);
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate void
10037c478bd9Sstevel@tonic-gate i_ddi_fm_dma_err_set(ddi_dma_handle_t handle, uint64_t ena, int status,
10047c478bd9Sstevel@tonic-gate     int flag)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
10077c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl = DEVI(hdlp->dmai_rdip)->devi_fmhdl;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_ena = ena;
10107c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_status = status;
10117c478bd9Sstevel@tonic-gate 	hdlp->dmai_error.err_expected = flag;
10127c478bd9Sstevel@tonic-gate 	atomic_add_64(&fmhdl->fh_kstat.fek_dma_err.value.ui64, 1);
10137c478bd9Sstevel@tonic-gate }
101400d0963fSdilpreet 
101500d0963fSdilpreet ddi_fmcompare_t
101600d0963fSdilpreet i_ddi_fm_acc_err_cf_get(ddi_acc_handle_t handle)
101700d0963fSdilpreet {
101800d0963fSdilpreet 	ddi_acc_impl_t *i_hdlp = (ddi_acc_impl_t *)handle;
101900d0963fSdilpreet 
102000d0963fSdilpreet 	return (i_hdlp->ahi_err->err_cf);
102100d0963fSdilpreet }
102200d0963fSdilpreet 
102300d0963fSdilpreet ddi_fmcompare_t
102400d0963fSdilpreet i_ddi_fm_dma_err_cf_get(ddi_dma_handle_t handle)
102500d0963fSdilpreet {
102600d0963fSdilpreet 	ddi_dma_impl_t *hdlp = (ddi_dma_impl_t *)handle;
102700d0963fSdilpreet 
102800d0963fSdilpreet 	return (hdlp->dmai_error.err_cf);
102900d0963fSdilpreet }
1030