xref: /titanic_51/usr/src/uts/common/os/ndifm.c (revision 00d0963faf2e861a4aef6b1bf28f99a5b2b20755)
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
5*00d0963fSdilpreet  * Common Development and Distribution License (the "License").
6*00d0963fSdilpreet  * 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*00d0963fSdilpreet  * Copyright 2006 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 Nexus Device Drivers
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * In addition to implementing and supporting Fault Management for Device
327c478bd9Sstevel@tonic-gate  * Drivers (ddifm.c), nexus drivers must support their children by
337c478bd9Sstevel@tonic-gate  * reporting FM capabilities, intializing interrupt block cookies
347c478bd9Sstevel@tonic-gate  * for error handling callbacks and caching mapped resources for lookup
357c478bd9Sstevel@tonic-gate  * during the detection of an IO transaction error.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * It is typically the nexus driver that receives an error indication
387c478bd9Sstevel@tonic-gate  * for a fault that may have occurred in the data path of an IO transaction.
397c478bd9Sstevel@tonic-gate  * Errors may be detected or received via an interrupt, a callback from
407c478bd9Sstevel@tonic-gate  * another subsystem (e.g. a cpu trap) or examination of control data.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Upon detection of an error, the nexus has a responsibility to alert
437c478bd9Sstevel@tonic-gate  * its children of the error and the transaction associated with that
447c478bd9Sstevel@tonic-gate  * error.  The actual implementation may vary depending upon the capabilities
457c478bd9Sstevel@tonic-gate  * of the nexus, its underlying hardware and its children.  In this file,
467c478bd9Sstevel@tonic-gate  * we provide support for typical nexus driver fault management tasks.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Fault Management Initialization
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *      Nexus drivers must implement two new busops, bus_fm_init() and
517c478bd9Sstevel@tonic-gate  *      bus_fm_fini().  bus_fm_init() is called from a child nexus or device
527c478bd9Sstevel@tonic-gate  *      driver and is expected to initialize any per-child state and return
537c478bd9Sstevel@tonic-gate  *      the FM and error interrupt priority levels of the nexus driver.
547c478bd9Sstevel@tonic-gate  *      Similarly, bus_fm_fini() is called by child drivers and should
557c478bd9Sstevel@tonic-gate  *      clean-up any resources allocated during bus_fm_init().
567c478bd9Sstevel@tonic-gate  *      These functions are called from passive kernel context, typically from
577c478bd9Sstevel@tonic-gate  *      driver attach(9F) and detach(9F) entry points.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Error Handler Dispatching
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *      Nexus drivers implemented to support error handler capabilities
627c478bd9Sstevel@tonic-gate  *	should invoke registered error handler callbacks for child drivers
637c478bd9Sstevel@tonic-gate  *	thought to be involved in the error.
647c478bd9Sstevel@tonic-gate  *	ndi_fm_handler_dispatch() is used to invoke
657c478bd9Sstevel@tonic-gate  *      all error handlers and returns one of the following status
667c478bd9Sstevel@tonic-gate  *      indications:
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *      DDI_FM_OK - No errors found by any child
697c478bd9Sstevel@tonic-gate  *      DDI_FM_FATAL - one or more children have detected a fatal error
707c478bd9Sstevel@tonic-gate  *      DDI_FM_NONFATAL - no fatal errors, but one or more children have
717c478bd9Sstevel@tonic-gate  *                            detected a non-fatal error
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *      ndi_fm_handler_dispatch() may be called in any context
747c478bd9Sstevel@tonic-gate  *      subject to the constraints specified by the interrupt iblock cookie
757c478bd9Sstevel@tonic-gate  *      returned during initialization.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Protected Accesses
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  *      When an access handle is mapped or a DMA handle is bound via the
807c478bd9Sstevel@tonic-gate  *      standard busops, bus_map() or bus_dma_bindhdl(), a child driver
817c478bd9Sstevel@tonic-gate  *      implemented to support DDI_FM_ACCCHK_CAPABLE or
827c478bd9Sstevel@tonic-gate  *	DDI_FM_DMACHK_CAPABLE capabilites
837c478bd9Sstevel@tonic-gate  *	expects the nexus to flag any errors detected for transactions
847c478bd9Sstevel@tonic-gate  *	associated with the mapped or bound handles.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  *      Children nexus or device drivers will set the following flags
877c478bd9Sstevel@tonic-gate  *      in their ddi_device_access or dma_attr_flags when requesting
887c478bd9Sstevel@tonic-gate  *      the an access or DMA handle mapping:
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  *      DDI_DMA_FLAGERR - nexus should set error status for any errors
917c478bd9Sstevel@tonic-gate  *                              detected for a failed DMA transaction.
927c478bd9Sstevel@tonic-gate  *      DDI_ACC_FLAGERR - nexus should set error status for any errors
937c478bd9Sstevel@tonic-gate  *                              detected for a failed PIO transaction.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *      A nexus is expected to provide additional error detection and
967c478bd9Sstevel@tonic-gate  *      handling for handles with these flags set.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * Exclusive Bus Access
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  *      In cases where a driver requires a high level of fault tolerance
1017c478bd9Sstevel@tonic-gate  *      for a programmed IO transaction, it is neccessary to grant exclusive
1027c478bd9Sstevel@tonic-gate  *      access to the bus resource.  Exclusivity guarantees that a fault
1037c478bd9Sstevel@tonic-gate  *      resulting from a transaction on the bus can be easily traced and
1047c478bd9Sstevel@tonic-gate  *      reported to the driver requesting the transaction.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *      Nexus drivers must implement two new busops to support exclusive
1077c478bd9Sstevel@tonic-gate  *      access, bus_fm_access_enter() and bus_fm_access_exit().  The IO
1087c478bd9Sstevel@tonic-gate  *      framework will use these functions when it must set-up access
1097c478bd9Sstevel@tonic-gate  *      handles that set devacc_attr_access to DDI_ACC_CAUTIOUS in
1107c478bd9Sstevel@tonic-gate  *      their ddi_device_acc_attr_t request.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *      Upon receipt of a bus_fm_access_enter() request, the nexus must prevent
1137c478bd9Sstevel@tonic-gate  *      all other access requests until it receives bus_fm_access_exit()
1147c478bd9Sstevel@tonic-gate  *      for the requested bus instance. bus_fm_access_enter() and
1157c478bd9Sstevel@tonic-gate  *	bus_fm_access_exit() may be called from user, kernel or kernel
1167c478bd9Sstevel@tonic-gate  *	interrupt context.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * Access and DMA Handle Caching
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  *      To aid a nexus driver in associating access or DMA handles with
1217c478bd9Sstevel@tonic-gate  *      a detected error, the nexus should cache all handles that are
1227c478bd9Sstevel@tonic-gate  *      associated with DDI_ACC_FLAGERR, DDI_ACC_CAUTIOUS_ACC or
1237c478bd9Sstevel@tonic-gate  *	DDI_DMA_FLAGERR requests from its children.  ndi_fmc_insert() is
1247c478bd9Sstevel@tonic-gate  *	called by a nexus to cache handles with the above protection flags
1257c478bd9Sstevel@tonic-gate  *	and ndi_fmc_remove() is called when that handle is unmapped or
1267c478bd9Sstevel@tonic-gate  *	unbound by the requesting child.  ndi_fmc_insert() and
1277c478bd9Sstevel@tonic-gate  *	ndi_fmc_remove() may be called from any user or kernel context.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  *	FM caches are allocated during ddi_fm_init() and maintained
1307c478bd9Sstevel@tonic-gate  *	as an array of elements that may be on one of two lists:
1317c478bd9Sstevel@tonic-gate  *	free or active.  The free list is a singly-linked list of
1327c478bd9Sstevel@tonic-gate  *	elements available for activity.  ndi_fm_insert() moves the
1337c478bd9Sstevel@tonic-gate  *	element at the head of the free to the active list.  The active
1347c478bd9Sstevel@tonic-gate  *	list is a doubly-linked searchable list.
1357c478bd9Sstevel@tonic-gate  *	When a handle is unmapped or unbound, its associated cache
1367c478bd9Sstevel@tonic-gate  *	entry is removed from the active list back to the free list.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  *      Upon detection of an error, the nexus may invoke ndi_fmc_error() to
1397c478bd9Sstevel@tonic-gate  *      iterate over the handle cache of one or more of its FM compliant
1407c478bd9Sstevel@tonic-gate  *      children.  A comparison callback function is provided upon each
1417c478bd9Sstevel@tonic-gate  *      invocation of ndi_fmc_error() to tell the IO framework if a
1427c478bd9Sstevel@tonic-gate  *      handle is associated with an error.  If so, the framework will
1437c478bd9Sstevel@tonic-gate  *      set the error status for that handle before returning from
1447c478bd9Sstevel@tonic-gate  *      ndi_fmc_error().
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  *      ndi_fmc_error() may be called in any context
1477c478bd9Sstevel@tonic-gate  *      subject to the constraints specified by the interrupt iblock cookie
1487c478bd9Sstevel@tonic-gate  *      returned during initialization of the nexus and its children.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #include <sys/types.h>
1537c478bd9Sstevel@tonic-gate #include <sys/param.h>
1547c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1557c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
1567c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
1577c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
1587c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
1597c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
1607c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
1617c478bd9Sstevel@tonic-gate #include <sys/ddifm.h>
1627c478bd9Sstevel@tonic-gate #include <sys/ndifm.h>
1637c478bd9Sstevel@tonic-gate #include <sys/spl.h>
1647c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
1657c478bd9Sstevel@tonic-gate #include <sys/devops.h>
1667c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
1677c478bd9Sstevel@tonic-gate #include <sys/fm/io/ddi.h>
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Allocate and initialize a fault management resource cache
1717c478bd9Sstevel@tonic-gate  * A fault management cache consists of a set of cache elements that
1727c478bd9Sstevel@tonic-gate  * may be on one of two lists: free or active.
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  * At creation time, every element but one is placed on the free list
1757c478bd9Sstevel@tonic-gate  * except for the first element.  This element is reserved as the first
1767c478bd9Sstevel@tonic-gate  * element of the active list and serves as an anchor for the active
1777c478bd9Sstevel@tonic-gate  * list in ndi_fmc_insert() and ndi_fmc_remove().  In these functions,
1787c478bd9Sstevel@tonic-gate  * it is not neccessary to check for the existence or validity of
1797c478bd9Sstevel@tonic-gate  * the active list.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate void
1827c478bd9Sstevel@tonic-gate i_ndi_fmc_create(ndi_fmc_t **fcpp, int qlen, ddi_iblock_cookie_t ibc)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	ndi_fmc_t *fcp;
1857c478bd9Sstevel@tonic-gate 	ndi_fmcentry_t *fep;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	ASSERT(qlen > 1);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	fcp = kmem_zalloc(sizeof (ndi_fmc_t), KM_SLEEP);
1907c478bd9Sstevel@tonic-gate 	mutex_init(&fcp->fc_lock, NULL, MUTEX_DRIVER, ibc);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* Preallocate and initialize entries for this fm cache */
1937c478bd9Sstevel@tonic-gate 	fcp->fc_elems = kmem_zalloc(qlen * sizeof (ndi_fmcentry_t), KM_SLEEP);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	fcp->fc_len = qlen;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Intialize the active and free lists */
1987c478bd9Sstevel@tonic-gate 	fcp->fc_active = fcp->fc_tail = fcp->fc_elems;
1997c478bd9Sstevel@tonic-gate 	fcp->fc_free = fcp->fc_elems + 1;
2007c478bd9Sstevel@tonic-gate 	qlen--;
2017c478bd9Sstevel@tonic-gate 	for (fep = fcp->fc_free; qlen > 1; qlen--) {
2027c478bd9Sstevel@tonic-gate 		fep->fce_prev = fep + 1;
2037c478bd9Sstevel@tonic-gate 		fep++;
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	*fcpp = fcp;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Destroy and resources associated with the given fault management cache.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate void
2137c478bd9Sstevel@tonic-gate i_ndi_fmc_destroy(ndi_fmc_t *fcp)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	if (fcp == NULL)
2167c478bd9Sstevel@tonic-gate 		return;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	kmem_free(fcp->fc_elems, fcp->fc_len * sizeof (ndi_fmcentry_t));
2197c478bd9Sstevel@tonic-gate 	kmem_free(fcp, sizeof (ndi_fmc_t));
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Grow an existing fault management cache by grow_sz number of entries
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate static int
2267c478bd9Sstevel@tonic-gate fmc_grow(ndi_fmc_t *fcp, int flag, int grow_sz)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	int olen, nlen;
2297c478bd9Sstevel@tonic-gate 	void *resource;
2307c478bd9Sstevel@tonic-gate 	ndi_fmcentry_t *ncp, *oep, *nep, *nnep;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	ASSERT(grow_sz);
2337c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->fc_lock));
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/* Allocate a new cache */
2367c478bd9Sstevel@tonic-gate 	nlen = grow_sz + fcp->fc_len;
2377c478bd9Sstevel@tonic-gate 	if ((ncp = kmem_zalloc(nlen * sizeof (ndi_fmcentry_t),
2387c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL)
2397c478bd9Sstevel@tonic-gate 		return (1);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* Migrate old cache to new cache */
2427c478bd9Sstevel@tonic-gate 	oep = fcp->fc_elems;
2437c478bd9Sstevel@tonic-gate 	olen = fcp->fc_len;
2447c478bd9Sstevel@tonic-gate 	for (nep = ncp; ; olen--) {
2457c478bd9Sstevel@tonic-gate 		resource = nep->fce_resource = oep->fce_resource;
2467c478bd9Sstevel@tonic-gate 		nep->fce_bus_specific = oep->fce_bus_specific;
2477c478bd9Sstevel@tonic-gate 		if (resource) {
2487c478bd9Sstevel@tonic-gate 			if (flag == DMA_HANDLE) {
2497c478bd9Sstevel@tonic-gate 				((ddi_dma_impl_t *)resource)->
2507c478bd9Sstevel@tonic-gate 				    dmai_error.err_fep = nep;
2517c478bd9Sstevel@tonic-gate 			} else if (flag == ACC_HANDLE) {
2527c478bd9Sstevel@tonic-gate 				((ddi_acc_impl_t *)resource)->
2537c478bd9Sstevel@tonic-gate 				    ahi_err->err_fep = nep;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		/*
2587c478bd9Sstevel@tonic-gate 		 * This is the last entry.  Set the tail pointer and
2597c478bd9Sstevel@tonic-gate 		 * terminate processing of the old cache.
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		if (olen == 1) {
2627c478bd9Sstevel@tonic-gate 			fcp->fc_tail = nep;
2637c478bd9Sstevel@tonic-gate 			++nep;
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		/*
2687c478bd9Sstevel@tonic-gate 		 * Set the next and previous pointer for the new cache
2697c478bd9Sstevel@tonic-gate 		 * entry.
2707c478bd9Sstevel@tonic-gate 		 */
2717c478bd9Sstevel@tonic-gate 		nnep = nep + 1;
2727c478bd9Sstevel@tonic-gate 		nep->fce_next = nnep;
2737c478bd9Sstevel@tonic-gate 		nnep->fce_prev = nep;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		/* Advance to the next entry */
2767c478bd9Sstevel@tonic-gate 		++oep;
2777c478bd9Sstevel@tonic-gate 		nep = nnep;
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	kmem_free(fcp->fc_elems, fcp->fc_len * sizeof (ndi_fmcentry_t));
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* Initialize and add remaining new cache entries to the free list */
2837c478bd9Sstevel@tonic-gate 	olen = fcp->fc_len + 1;
2847c478bd9Sstevel@tonic-gate 	fcp->fc_len = nlen;
2857c478bd9Sstevel@tonic-gate 	for (fcp->fc_free = nep; nlen > olen; nlen--) {
2867c478bd9Sstevel@tonic-gate 		nep->fce_prev = nep + 1;
2877c478bd9Sstevel@tonic-gate 		nep++;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	fcp->fc_active = ncp;
2917c478bd9Sstevel@tonic-gate 	fcp->fc_elems = ncp;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * ndi_fmc_insert -
2987c478bd9Sstevel@tonic-gate  * 	Add a new entry to the specified cache.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * 	This function must be called at or below LOCK_LEVEL
3017c478bd9Sstevel@tonic-gate  */
3027c478bd9Sstevel@tonic-gate void
3037c478bd9Sstevel@tonic-gate ndi_fmc_insert(dev_info_t *dip, int flag, void *resource, void *bus_specific)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3067c478bd9Sstevel@tonic-gate 	ndi_fmc_t *fcp;
3077c478bd9Sstevel@tonic-gate 	ndi_fmcentry_t *fep, **fpp;
3087c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	ASSERT(devi);
3117c478bd9Sstevel@tonic-gate 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	fmhdl = devi->devi_fmhdl;
3147c478bd9Sstevel@tonic-gate 	if (fmhdl == NULL) {
3157c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP);
3167c478bd9Sstevel@tonic-gate 		return;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (flag == DMA_HANDLE) {
3207c478bd9Sstevel@tonic-gate 		if (!DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
3217c478bd9Sstevel@tonic-gate 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
3227c478bd9Sstevel@tonic-gate 			    DDI_NOSLEEP);
3237c478bd9Sstevel@tonic-gate 			return;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 		fcp = fmhdl->fh_dma_cache;
3267c478bd9Sstevel@tonic-gate 		fpp = &((ddi_dma_impl_t *)resource)->dmai_error.err_fep;
3277c478bd9Sstevel@tonic-gate 	} else if (flag == ACC_HANDLE) {
3287c478bd9Sstevel@tonic-gate 		if (!DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
3297c478bd9Sstevel@tonic-gate 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
3307c478bd9Sstevel@tonic-gate 			    DDI_NOSLEEP);
3317c478bd9Sstevel@tonic-gate 			return;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		fcp = fmhdl->fh_acc_cache;
3347c478bd9Sstevel@tonic-gate 		fpp = &((ddi_acc_impl_t *)resource)->ahi_err->err_fep;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	ASSERT(*fpp == NULL);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	mutex_enter(&fcp->fc_lock);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* Get an entry from the free list */
3417c478bd9Sstevel@tonic-gate 	fep = fcp->fc_free;
3427c478bd9Sstevel@tonic-gate 	if (fep == NULL) {
3437c478bd9Sstevel@tonic-gate 		if (fmc_grow(fcp, flag,
3447c478bd9Sstevel@tonic-gate 		    (flag == ACC_HANDLE ? default_acccache_sz :
3457c478bd9Sstevel@tonic-gate 		    default_dmacache_sz)) != 0) {
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			/* Unable to get an entry or grow this cache */
3487c478bd9Sstevel@tonic-gate 			atomic_add_64(
3497c478bd9Sstevel@tonic-gate 			    &fmhdl->fh_kstat.fek_fmc_full.value.ui64, 1);
3507c478bd9Sstevel@tonic-gate 			mutex_exit(&fcp->fc_lock);
3517c478bd9Sstevel@tonic-gate 			return;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 		atomic_add_64(&fmhdl->fh_kstat.fek_fmc_grew.value.ui64, 1);
3547c478bd9Sstevel@tonic-gate 		fep = fcp->fc_free;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	fcp->fc_free = fep->fce_prev;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Set-up the handle resource and bus_specific information.
3607c478bd9Sstevel@tonic-gate 	 * Also remember the pointer back to the cache for quick removal.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	fep->fce_bus_specific = bus_specific;
3637c478bd9Sstevel@tonic-gate 	fep->fce_resource = resource;
3647c478bd9Sstevel@tonic-gate 	fep->fce_next = NULL;
3657c478bd9Sstevel@tonic-gate 	*fpp = fep;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/* Add entry to the end of the active list */
3687c478bd9Sstevel@tonic-gate 	fep->fce_prev = fcp->fc_tail;
3697c478bd9Sstevel@tonic-gate 	fcp->fc_tail->fce_next = fep;
3707c478bd9Sstevel@tonic-gate 	fcp->fc_tail = fep;
3717c478bd9Sstevel@tonic-gate 	mutex_exit(&fcp->fc_lock);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * 	Remove an entry from the specified cache of access or dma mappings
3767c478bd9Sstevel@tonic-gate  *
3777c478bd9Sstevel@tonic-gate  * 	This function must be called at or below LOCK_LEVEL.
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate void
3807c478bd9Sstevel@tonic-gate ndi_fmc_remove(dev_info_t *dip, int flag, const void *resource)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	ndi_fmc_t *fcp;
3837c478bd9Sstevel@tonic-gate 	ndi_fmcentry_t *fep;
3847c478bd9Sstevel@tonic-gate 	struct dev_info *devi = DEVI(dip);
3857c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	ASSERT(devi);
3887c478bd9Sstevel@tonic-gate 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	fmhdl = devi->devi_fmhdl;
3917c478bd9Sstevel@tonic-gate 	if (fmhdl == NULL) {
3927c478bd9Sstevel@tonic-gate 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP);
3937c478bd9Sstevel@tonic-gate 		return;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* Find cache entry pointer for this resource */
3977c478bd9Sstevel@tonic-gate 	if (flag == DMA_HANDLE) {
3987c478bd9Sstevel@tonic-gate 		if (!DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
3997c478bd9Sstevel@tonic-gate 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
4007c478bd9Sstevel@tonic-gate 			    DDI_NOSLEEP);
4017c478bd9Sstevel@tonic-gate 			return;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		fcp = fmhdl->fh_dma_cache;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		ASSERT(fcp);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		mutex_enter(&fcp->fc_lock);
4087c478bd9Sstevel@tonic-gate 		fep = ((ddi_dma_impl_t *)resource)->dmai_error.err_fep;
4097c478bd9Sstevel@tonic-gate 		((ddi_dma_impl_t *)resource)->dmai_error.err_fep = NULL;
4107c478bd9Sstevel@tonic-gate 	} else if (flag == ACC_HANDLE) {
4117c478bd9Sstevel@tonic-gate 		if (!DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
4127c478bd9Sstevel@tonic-gate 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
4137c478bd9Sstevel@tonic-gate 			    DDI_NOSLEEP);
4147c478bd9Sstevel@tonic-gate 			return;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 		fcp = fmhdl->fh_acc_cache;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		ASSERT(fcp);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		mutex_enter(&fcp->fc_lock);
4217c478bd9Sstevel@tonic-gate 		fep = ((ddi_acc_impl_t *)resource)->ahi_err->err_fep;
4227c478bd9Sstevel@tonic-gate 		((ddi_acc_impl_t *)resource)->ahi_err->err_fep = NULL;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	/*
4267c478bd9Sstevel@tonic-gate 	 * Resource not in cache, return
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if (fep == NULL) {
4297c478bd9Sstevel@tonic-gate 		mutex_exit(&fcp->fc_lock);
4307c478bd9Sstevel@tonic-gate 		return;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	fep->fce_prev->fce_next = fep->fce_next;
4347c478bd9Sstevel@tonic-gate 	if (fep == fcp->fc_tail)
4357c478bd9Sstevel@tonic-gate 		fcp->fc_tail = fep->fce_prev;
4367c478bd9Sstevel@tonic-gate 	else
4377c478bd9Sstevel@tonic-gate 		fep->fce_next->fce_prev = fep->fce_prev;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/* Add entry back to the free list */
4407c478bd9Sstevel@tonic-gate 	fep->fce_prev = fcp->fc_free;
4417c478bd9Sstevel@tonic-gate 	fcp->fc_free = fep;
4427c478bd9Sstevel@tonic-gate 	mutex_exit(&fcp->fc_lock);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
445*00d0963fSdilpreet int
446*00d0963fSdilpreet ndi_fmc_entry_error(dev_info_t *dip, int flag, ddi_fm_error_t *derr,
447*00d0963fSdilpreet     const void *bus_err_state)
448*00d0963fSdilpreet {
449*00d0963fSdilpreet 	int status, fatal = 0, nonfatal = 0;
450*00d0963fSdilpreet 	ndi_fmc_t *fcp = NULL;
451*00d0963fSdilpreet 	ndi_fmcentry_t *fep;
452*00d0963fSdilpreet 	struct i_ddi_fmhdl *fmhdl;
453*00d0963fSdilpreet 
454*00d0963fSdilpreet 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
455*00d0963fSdilpreet 
456*00d0963fSdilpreet 	fmhdl = DEVI(dip)->devi_fmhdl;
457*00d0963fSdilpreet 	ASSERT(fmhdl);
458*00d0963fSdilpreet 	status = DDI_FM_UNKNOWN;
459*00d0963fSdilpreet 
460*00d0963fSdilpreet 	if (flag == DMA_HANDLE && DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
461*00d0963fSdilpreet 		fcp = fmhdl->fh_dma_cache;
462*00d0963fSdilpreet 		ASSERT(fcp);
463*00d0963fSdilpreet 	} else if (flag == ACC_HANDLE && DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
464*00d0963fSdilpreet 		fcp = fmhdl->fh_acc_cache;
465*00d0963fSdilpreet 		ASSERT(fcp);
466*00d0963fSdilpreet 	}
467*00d0963fSdilpreet 
468*00d0963fSdilpreet 	if (fcp != NULL) {
469*00d0963fSdilpreet 
470*00d0963fSdilpreet 		/*
471*00d0963fSdilpreet 		 * Check active resource entries
472*00d0963fSdilpreet 		 */
473*00d0963fSdilpreet 		mutex_enter(&fcp->fc_lock);
474*00d0963fSdilpreet 		for (fep = fcp->fc_active->fce_next; fep != NULL;
475*00d0963fSdilpreet 		    fep = fep->fce_next) {
476*00d0963fSdilpreet 			ddi_fmcompare_t compare_func;
477*00d0963fSdilpreet 
478*00d0963fSdilpreet 			/*
479*00d0963fSdilpreet 			 * Compare captured error state with handle
480*00d0963fSdilpreet 			 * resources.  During the comparison and
481*00d0963fSdilpreet 			 * subsequent error handling, we block
482*00d0963fSdilpreet 			 * attempts to free the cache entry.
483*00d0963fSdilpreet 			 */
484*00d0963fSdilpreet 			compare_func = (flag == ACC_HANDLE) ?
485*00d0963fSdilpreet 			    i_ddi_fm_acc_err_cf_get((ddi_acc_handle_t)
486*00d0963fSdilpreet 				fep->fce_resource) :
487*00d0963fSdilpreet 			    i_ddi_fm_dma_err_cf_get((ddi_dma_handle_t)
488*00d0963fSdilpreet 				fep->fce_resource);
489*00d0963fSdilpreet 
490*00d0963fSdilpreet 			status = compare_func(dip, fep->fce_resource,
491*00d0963fSdilpreet 			    bus_err_state, fep->fce_bus_specific);
492*00d0963fSdilpreet 			if (status == DDI_FM_UNKNOWN || status == DDI_FM_OK)
493*00d0963fSdilpreet 				continue;
494*00d0963fSdilpreet 
495*00d0963fSdilpreet 			if (status == DDI_FM_FATAL)
496*00d0963fSdilpreet 				++fatal;
497*00d0963fSdilpreet 			else if (status == DDI_FM_NONFATAL)
498*00d0963fSdilpreet 				++nonfatal;
499*00d0963fSdilpreet 
500*00d0963fSdilpreet 			/* Set the error for this resource handle */
501*00d0963fSdilpreet 			if (flag == ACC_HANDLE) {
502*00d0963fSdilpreet 				ddi_acc_handle_t ap = fep->fce_resource;
503*00d0963fSdilpreet 
504*00d0963fSdilpreet 				i_ddi_fm_acc_err_set(ap, derr->fme_ena, status,
505*00d0963fSdilpreet 				    DDI_FM_ERR_UNEXPECTED);
506*00d0963fSdilpreet 				ddi_fm_acc_err_get(ap, derr, DDI_FME_VERSION);
507*00d0963fSdilpreet 				derr->fme_acc_handle = ap;
508*00d0963fSdilpreet 			} else {
509*00d0963fSdilpreet 				ddi_dma_handle_t dp = fep->fce_resource;
510*00d0963fSdilpreet 
511*00d0963fSdilpreet 				i_ddi_fm_dma_err_set(dp, derr->fme_ena, status,
512*00d0963fSdilpreet 				    DDI_FM_ERR_UNEXPECTED);
513*00d0963fSdilpreet 				ddi_fm_dma_err_get(dp, derr, DDI_FME_VERSION);
514*00d0963fSdilpreet 				derr->fme_dma_handle = dp;
515*00d0963fSdilpreet 			}
516*00d0963fSdilpreet 			break;
517*00d0963fSdilpreet 		}
518*00d0963fSdilpreet 		mutex_exit(&fcp->fc_lock);
519*00d0963fSdilpreet 	}
520*00d0963fSdilpreet 	return (fatal ? DDI_FM_FATAL : nonfatal ? DDI_FM_NONFATAL :
521*00d0963fSdilpreet 	    DDI_FM_UNKNOWN);
522*00d0963fSdilpreet }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Check error state against the handle resource stored in the specified
5267c478bd9Sstevel@tonic-gate  * FM cache.  If tdip != NULL, we check only the cache entries for tdip.
5277c478bd9Sstevel@tonic-gate  * The caller must ensure that tdip is valid throughout the call and
5287c478bd9Sstevel@tonic-gate  * all FM data structures can be safely accesses.
5297c478bd9Sstevel@tonic-gate  *
5307c478bd9Sstevel@tonic-gate  * If tdip == NULL, we check all children that have registered their
5317c478bd9Sstevel@tonic-gate  * FM_DMA_CHK or FM_ACC_CHK capabilities.
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  * The following status values may be returned:
5347c478bd9Sstevel@tonic-gate  *
5357c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL - if at least one cache entry comparison yields a
5367c478bd9Sstevel@tonic-gate  *			fatal error.
5377c478bd9Sstevel@tonic-gate  *
5387c478bd9Sstevel@tonic-gate  *	DDI_FM_NONFATAL - if at least one cache entry comparison yields a
5397c478bd9Sstevel@tonic-gate  *			non-fatal error and no comparison yields a fatal error.
5407c478bd9Sstevel@tonic-gate  *
5417c478bd9Sstevel@tonic-gate  *	DDI_FM_UNKNOWN - cache entry comparisons did not yield fatal or
5427c478bd9Sstevel@tonic-gate  *			non-fatal errors.
5437c478bd9Sstevel@tonic-gate  *
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate int
546*00d0963fSdilpreet ndi_fmc_error(dev_info_t *dip, dev_info_t *tdip, int flag, uint64_t ena,
547*00d0963fSdilpreet     const void *bus_err_state)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	int status, fatal = 0, nonfatal = 0;
5507c478bd9Sstevel@tonic-gate 	ddi_fm_error_t derr;
5517c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *fmhdl;
5527c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(dip);
5577c478bd9Sstevel@tonic-gate 	fmhdl = DEVI(dip)->devi_fmhdl;
5587c478bd9Sstevel@tonic-gate 	ASSERT(fmhdl);
559*00d0963fSdilpreet 
560*00d0963fSdilpreet 	bzero(&derr, sizeof (ddi_fm_error_t));
561*00d0963fSdilpreet 	derr.fme_version = DDI_FME_VERSION;
562*00d0963fSdilpreet 	derr.fme_flag = DDI_FM_ERR_UNEXPECTED;
563*00d0963fSdilpreet 	derr.fme_ena = ena;
564*00d0963fSdilpreet 
5657c478bd9Sstevel@tonic-gate 	for (tgt = fmhdl->fh_tgts; tgt != NULL; tgt = tgt->ft_next) {
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		if (tdip != NULL && tdip != tgt->ft_dip)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		/*
571*00d0963fSdilpreet 		 * Attempt to find the entry in this childs handle cache
5727c478bd9Sstevel@tonic-gate 		 */
573*00d0963fSdilpreet 		status = ndi_fmc_entry_error(tgt->ft_dip, flag, &derr,
574*00d0963fSdilpreet 		    bus_err_state);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (status == DDI_FM_FATAL)
5777c478bd9Sstevel@tonic-gate 			++fatal;
5787c478bd9Sstevel@tonic-gate 		else if (status == DDI_FM_NONFATAL)
5797c478bd9Sstevel@tonic-gate 			++nonfatal;
580*00d0963fSdilpreet 		else
581*00d0963fSdilpreet 			continue;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		/*
5847c478bd9Sstevel@tonic-gate 		 * Call our child to process this error.
5857c478bd9Sstevel@tonic-gate 		 */
586*00d0963fSdilpreet 		status = tgt->ft_errhdl->eh_func(tgt->ft_dip, &derr,
587*00d0963fSdilpreet 		    tgt->ft_errhdl->eh_impl);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		if (status == DDI_FM_FATAL)
5907c478bd9Sstevel@tonic-gate 			++fatal;
5917c478bd9Sstevel@tonic-gate 		else if (status == DDI_FM_NONFATAL)
5927c478bd9Sstevel@tonic-gate 			++nonfatal;
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(dip);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (fatal)
5987c478bd9Sstevel@tonic-gate 		return (DDI_FM_FATAL);
5997c478bd9Sstevel@tonic-gate 	else if (nonfatal)
6007c478bd9Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	return (DDI_FM_UNKNOWN);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Dispatch registered error handlers for dip.  If tdip != NULL, only
6077c478bd9Sstevel@tonic-gate  * the error handler (if available) for tdip is invoked.  Otherwise,
6087c478bd9Sstevel@tonic-gate  * all registered error handlers are invoked.
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  * The following status values may be returned:
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  *	DDI_FM_FATAL - if at least one error handler returns a
6137c478bd9Sstevel@tonic-gate  *			fatal error.
6147c478bd9Sstevel@tonic-gate  *
6157c478bd9Sstevel@tonic-gate  *	DDI_FM_NONFATAL - if at least one error handler returns a
6167c478bd9Sstevel@tonic-gate  *			non-fatal error and none returned a fatal error.
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  *	DDI_FM_UNKNOWN - if at least one error handler returns
6197c478bd9Sstevel@tonic-gate  *			unknown status and none return fatal or non-fatal.
6207c478bd9Sstevel@tonic-gate  *
6217c478bd9Sstevel@tonic-gate  *	DDI_FM_OK - if all error handlers return DDI_FM_OK
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate int
6247c478bd9Sstevel@tonic-gate ndi_fm_handler_dispatch(dev_info_t *dip, dev_info_t *tdip,
6257c478bd9Sstevel@tonic-gate     const ddi_fm_error_t *nerr)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	int status;
6287c478bd9Sstevel@tonic-gate 	int unknown = 0, fatal = 0, nonfatal = 0;
6297c478bd9Sstevel@tonic-gate 	struct i_ddi_fmhdl *hdl;
6307c478bd9Sstevel@tonic-gate 	struct i_ddi_fmtgt *tgt;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	status = DDI_FM_UNKNOWN;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_enter(dip);
6357c478bd9Sstevel@tonic-gate 	hdl = DEVI(dip)->devi_fmhdl;
6367c478bd9Sstevel@tonic-gate 	tgt = hdl->fh_tgts;
6377c478bd9Sstevel@tonic-gate 	while (tgt != NULL) {
6387c478bd9Sstevel@tonic-gate 		if (tdip == NULL || tdip == tgt->ft_dip) {
6397c478bd9Sstevel@tonic-gate 			struct i_ddi_errhdl *errhdl;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 			errhdl = tgt->ft_errhdl;
6427c478bd9Sstevel@tonic-gate 			status = errhdl->eh_func(tgt->ft_dip, nerr,
6437c478bd9Sstevel@tonic-gate 			    errhdl->eh_impl);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 			if (status == DDI_FM_FATAL)
6467c478bd9Sstevel@tonic-gate 				++fatal;
6477c478bd9Sstevel@tonic-gate 			else if (status == DDI_FM_NONFATAL)
6487c478bd9Sstevel@tonic-gate 				++nonfatal;
6497c478bd9Sstevel@tonic-gate 			else if (status == DDI_FM_UNKNOWN)
6507c478bd9Sstevel@tonic-gate 				++unknown;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			/* Only interested in one target */
6537c478bd9Sstevel@tonic-gate 			if (tdip != NULL)
6547c478bd9Sstevel@tonic-gate 				break;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 		tgt = tgt->ft_next;
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 	i_ddi_fm_handler_exit(dip);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (fatal)
6617c478bd9Sstevel@tonic-gate 		return (DDI_FM_FATAL);
6627c478bd9Sstevel@tonic-gate 	else if (nonfatal)
6637c478bd9Sstevel@tonic-gate 		return (DDI_FM_NONFATAL);
6647c478bd9Sstevel@tonic-gate 	else if (unknown)
6657c478bd9Sstevel@tonic-gate 		return (DDI_FM_UNKNOWN);
6667c478bd9Sstevel@tonic-gate 	else
6677c478bd9Sstevel@tonic-gate 		return (DDI_FM_OK);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * Set error status for specified access or DMA handle
6727c478bd9Sstevel@tonic-gate  *
6737c478bd9Sstevel@tonic-gate  * May be called in any context but caller must insure validity of
6747c478bd9Sstevel@tonic-gate  * handle.
6757c478bd9Sstevel@tonic-gate  */
6767c478bd9Sstevel@tonic-gate void
6777c478bd9Sstevel@tonic-gate ndi_fm_acc_err_set(ddi_acc_handle_t handle, ddi_fm_error_t *dfe)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	i_ddi_fm_acc_err_set(handle, dfe->fme_ena, dfe->fme_status,
6807c478bd9Sstevel@tonic-gate 	    dfe->fme_flag);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate void
6847c478bd9Sstevel@tonic-gate ndi_fm_dma_err_set(ddi_dma_handle_t handle, ddi_fm_error_t *dfe)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	i_ddi_fm_dma_err_set(handle, dfe->fme_ena, dfe->fme_status,
6877c478bd9Sstevel@tonic-gate 	    dfe->fme_flag);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * Call parent busop fm initialization routine.
6927c478bd9Sstevel@tonic-gate  *
6937c478bd9Sstevel@tonic-gate  * Called during driver attach(1M)
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate int
6967c478bd9Sstevel@tonic-gate i_ndi_busop_fm_init(dev_info_t *dip, int tcap, ddi_iblock_cookie_t *ibc)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	int pcap;
6997c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node())
7027c478bd9Sstevel@tonic-gate 		return (ddi_system_fmcap | DDI_FM_EREPORT_CAPABLE);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* Valid operation for BUSO_REV_6 and above */
7057c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
7067c478bd9Sstevel@tonic-gate 		return (DDI_FM_NOT_CAPABLE);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_init == NULL)
7097c478bd9Sstevel@tonic-gate 		return (DDI_FM_NOT_CAPABLE);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	pcap = (*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_init)
7127c478bd9Sstevel@tonic-gate 	    (pdip, dip, tcap, ibc);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (pcap);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * Call parent busop fm clean-up routine.
7197c478bd9Sstevel@tonic-gate  *
7207c478bd9Sstevel@tonic-gate  * Called during driver detach(1M)
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate void
7237c478bd9Sstevel@tonic-gate i_ndi_busop_fm_fini(dev_info_t *dip)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node())
7287c478bd9Sstevel@tonic-gate 		return;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/* Valid operation for BUSO_REV_6 and above */
7317c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
7327c478bd9Sstevel@tonic-gate 		return;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_fini == NULL)
7357c478bd9Sstevel@tonic-gate 		return;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_fini)(pdip, dip);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * The following routines provide exclusive access to a nexus resource
7427c478bd9Sstevel@tonic-gate  *
7437c478bd9Sstevel@tonic-gate  * These busops may be called in user or kernel driver context.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate void
7467c478bd9Sstevel@tonic-gate i_ndi_busop_access_enter(dev_info_t *dip, ddi_acc_handle_t handle)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/* Valid operation for BUSO_REV_6 and above */
7517c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
7527c478bd9Sstevel@tonic-gate 		return;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_enter == NULL)
7557c478bd9Sstevel@tonic-gate 		return;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_enter)
7587c478bd9Sstevel@tonic-gate 	    (pdip, handle);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate void
7627c478bd9Sstevel@tonic-gate i_ndi_busop_access_exit(dev_info_t *dip, ddi_acc_handle_t handle)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/* Valid operation for BUSO_REV_6 and above */
7677c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
7687c478bd9Sstevel@tonic-gate 		return;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_exit == NULL)
7717c478bd9Sstevel@tonic-gate 		return;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_exit)(pdip, handle);
7747c478bd9Sstevel@tonic-gate }
775