xref: /titanic_51/usr/src/uts/common/os/ndifm.c (revision fe54a78e1aacf39261ad56e9903bce02e3fb6d21)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Fault Management for Nexus Device Drivers
28  *
29  * In addition to implementing and supporting Fault Management for Device
30  * Drivers (ddifm.c), nexus drivers must support their children by
31  * reporting FM capabilities, intializing interrupt block cookies
32  * for error handling callbacks and caching mapped resources for lookup
33  * during the detection of an IO transaction error.
34  *
35  * It is typically the nexus driver that receives an error indication
36  * for a fault that may have occurred in the data path of an IO transaction.
37  * Errors may be detected or received via an interrupt, a callback from
38  * another subsystem (e.g. a cpu trap) or examination of control data.
39  *
40  * Upon detection of an error, the nexus has a responsibility to alert
41  * its children of the error and the transaction associated with that
42  * error.  The actual implementation may vary depending upon the capabilities
43  * of the nexus, its underlying hardware and its children.  In this file,
44  * we provide support for typical nexus driver fault management tasks.
45  *
46  * Fault Management Initialization
47  *
48  *      Nexus drivers must implement two new busops, bus_fm_init() and
49  *      bus_fm_fini().  bus_fm_init() is called from a child nexus or device
50  *      driver and is expected to initialize any per-child state and return
51  *      the FM and error interrupt priority levels of the nexus driver.
52  *      Similarly, bus_fm_fini() is called by child drivers and should
53  *      clean-up any resources allocated during bus_fm_init().
54  *      These functions are called from passive kernel context, typically from
55  *      driver attach(9F) and detach(9F) entry points.
56  *
57  * Error Handler Dispatching
58  *
59  *      Nexus drivers implemented to support error handler capabilities
60  *	should invoke registered error handler callbacks for child drivers
61  *	thought to be involved in the error.
62  *	ndi_fm_handler_dispatch() is used to invoke
63  *      all error handlers and returns one of the following status
64  *      indications:
65  *
66  *      DDI_FM_OK - No errors found by any child
67  *      DDI_FM_FATAL - one or more children have detected a fatal error
68  *      DDI_FM_NONFATAL - no fatal errors, but one or more children have
69  *                            detected a non-fatal error
70  *
71  *      ndi_fm_handler_dispatch() may be called in any context
72  *      subject to the constraints specified by the interrupt iblock cookie
73  *      returned during initialization.
74  *
75  * Protected Accesses
76  *
77  *      When an access handle is mapped or a DMA handle is bound via the
78  *      standard busops, bus_map() or bus_dma_bindhdl(), a child driver
79  *      implemented to support DDI_FM_ACCCHK_CAPABLE or
80  *	DDI_FM_DMACHK_CAPABLE capabilites
81  *	expects the nexus to flag any errors detected for transactions
82  *	associated with the mapped or bound handles.
83  *
84  *      Children nexus or device drivers will set the following flags
85  *      in their ddi_device_access or dma_attr_flags when requesting
86  *      the an access or DMA handle mapping:
87  *
88  *      DDI_DMA_FLAGERR - nexus should set error status for any errors
89  *                              detected for a failed DMA transaction.
90  *      DDI_ACC_FLAGERR - nexus should set error status for any errors
91  *                              detected for a failed PIO transaction.
92  *
93  *      A nexus is expected to provide additional error detection and
94  *      handling for handles with these flags set.
95  *
96  * Exclusive Bus Access
97  *
98  *      In cases where a driver requires a high level of fault tolerance
99  *      for a programmed IO transaction, it is neccessary to grant exclusive
100  *      access to the bus resource.  Exclusivity guarantees that a fault
101  *      resulting from a transaction on the bus can be easily traced and
102  *      reported to the driver requesting the transaction.
103  *
104  *      Nexus drivers must implement two new busops to support exclusive
105  *      access, bus_fm_access_enter() and bus_fm_access_exit().  The IO
106  *      framework will use these functions when it must set-up access
107  *      handles that set devacc_attr_access to DDI_ACC_CAUTIOUS in
108  *      their ddi_device_acc_attr_t request.
109  *
110  *      Upon receipt of a bus_fm_access_enter() request, the nexus must prevent
111  *      all other access requests until it receives bus_fm_access_exit()
112  *      for the requested bus instance. bus_fm_access_enter() and
113  *	bus_fm_access_exit() may be called from user, kernel or kernel
114  *	interrupt context.
115  *
116  * Access and DMA Handle Caching
117  *
118  *      To aid a nexus driver in associating access or DMA handles with
119  *      a detected error, the nexus should cache all handles that are
120  *      associated with DDI_ACC_FLAGERR, DDI_ACC_CAUTIOUS_ACC or
121  *	DDI_DMA_FLAGERR requests from its children.  ndi_fmc_insert() is
122  *	called by a nexus to cache handles with the above protection flags
123  *	and ndi_fmc_remove() is called when that handle is unmapped or
124  *	unbound by the requesting child.  ndi_fmc_insert() and
125  *	ndi_fmc_remove() may be called from any user or kernel context.
126  *
127  *	FM cache element is implemented by kmem_cache. The elements are
128  *	stored in a doubly-linked searchable list.  When a handle is created,
129  *	ndi_fm_insert() allocates an entry from the kmem_cache and inserts
130  *	the entry to the head of the list.  When a handle is unmapped
131  *	or unbound, ndi_fm_remove() removes its associated cache entry from
132  *	the list.
133  *
134  *      Upon detection of an error, the nexus may invoke ndi_fmc_error() to
135  *      iterate over the handle cache of one or more of its FM compliant
136  *      children.  A comparison callback function is provided upon each
137  *      invocation of ndi_fmc_error() to tell the IO framework if a
138  *      handle is associated with an error.  If so, the framework will
139  *      set the error status for that handle before returning from
140  *      ndi_fmc_error().
141  *
142  *      ndi_fmc_error() may be called in any context
143  *      subject to the constraints specified by the interrupt iblock cookie
144  *      returned during initialization of the nexus and its children.
145  *
146  */
147 
148 #include <sys/types.h>
149 #include <sys/param.h>
150 #include <sys/debug.h>
151 #include <sys/sunddi.h>
152 #include <sys/sunndi.h>
153 #include <sys/ddi.h>
154 #include <sys/ndi_impldefs.h>
155 #include <sys/devctl.h>
156 #include <sys/nvpair.h>
157 #include <sys/ddifm.h>
158 #include <sys/ndifm.h>
159 #include <sys/spl.h>
160 #include <sys/sysmacros.h>
161 #include <sys/devops.h>
162 #include <sys/atomic.h>
163 #include <sys/kmem.h>
164 #include <sys/fm/io/ddi.h>
165 
166 kmem_cache_t *ndi_fm_entry_cache;
167 
168 void
169 ndi_fm_init(void)
170 {
171 	ndi_fm_entry_cache = kmem_cache_create("ndi_fm_entry_cache",
172 	    sizeof (ndi_fmcentry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
173 }
174 
175 /*
176  * Allocate and initialize a fault management resource cache
177  * A fault management cache consists of a set of cache elements that
178  * are allocated from "ndi_fm_entry_cache".
179  */
180 /* ARGSUSED */
181 void
182 i_ndi_fmc_create(ndi_fmc_t **fcpp, int qlen, ddi_iblock_cookie_t ibc)
183 {
184 	ndi_fmc_t *fcp;
185 
186 	fcp = kmem_zalloc(sizeof (ndi_fmc_t), KM_SLEEP);
187 	mutex_init(&fcp->fc_lock, NULL, MUTEX_DRIVER, ibc);
188 
189 	*fcpp = fcp;
190 }
191 
192 /*
193  * Destroy and resources associated with the given fault management cache.
194  */
195 void
196 i_ndi_fmc_destroy(ndi_fmc_t *fcp)
197 {
198 	ndi_fmcentry_t *fep, *pp;
199 
200 	if (fcp == NULL)
201 		return;
202 
203 	/* Free all the cached entries, this should not happen though */
204 	mutex_enter(&fcp->fc_lock);
205 	for (fep = fcp->fc_head; fep != NULL; fep = pp) {
206 		pp = fep->fce_next;
207 		kmem_cache_free(ndi_fm_entry_cache, fep);
208 	}
209 	mutex_exit(&fcp->fc_lock);
210 	mutex_destroy(&fcp->fc_lock);
211 	kmem_free(fcp, sizeof (ndi_fmc_t));
212 }
213 
214 /*
215  * ndi_fmc_insert -
216  * 	Add a new entry to the specified cache.
217  *
218  * 	This function must be called at or below LOCK_LEVEL
219  */
220 void
221 ndi_fmc_insert(dev_info_t *dip, int flag, void *resource, void *bus_specific)
222 {
223 	struct dev_info *devi = DEVI(dip);
224 	ndi_fmc_t *fcp;
225 	ndi_fmcentry_t *fep, **fpp;
226 	struct i_ddi_fmhdl *fmhdl;
227 
228 	ASSERT(devi);
229 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
230 
231 	fmhdl = devi->devi_fmhdl;
232 	if (fmhdl == NULL) {
233 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP);
234 		return;
235 	}
236 
237 	if (flag == DMA_HANDLE) {
238 		if (!DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
239 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
240 			    DDI_NOSLEEP);
241 			return;
242 		}
243 		fcp = fmhdl->fh_dma_cache;
244 		fpp = &((ddi_dma_impl_t *)resource)->dmai_error.err_fep;
245 	} else if (flag == ACC_HANDLE) {
246 		if (!DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
247 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
248 			    DDI_NOSLEEP);
249 			return;
250 		}
251 		fcp = fmhdl->fh_acc_cache;
252 		fpp = &((ddi_acc_impl_t *)resource)->ahi_err->err_fep;
253 	}
254 
255 	fep = kmem_cache_alloc(ndi_fm_entry_cache, KM_NOSLEEP);
256 	if (fep == NULL) {
257 		atomic_inc_64(&fmhdl->fh_kstat.fek_fmc_full.value.ui64);
258 		return;
259 	}
260 
261 	/*
262 	 * Set-up the handle resource and bus_specific information.
263 	 * Also remember the pointer back to the cache for quick removal.
264 	 */
265 	fep->fce_bus_specific = bus_specific;
266 	fep->fce_resource = resource;
267 	fep->fce_next = NULL;
268 
269 	/* Add entry to the end of the active list */
270 	mutex_enter(&fcp->fc_lock);
271 	ASSERT(*fpp == NULL);
272 	*fpp = fep;
273 	fep->fce_prev = fcp->fc_tail;
274 	if (fcp->fc_tail != NULL)
275 		fcp->fc_tail->fce_next = fep;
276 	else
277 		fcp->fc_head = fep;
278 	fcp->fc_tail = fep;
279 	mutex_exit(&fcp->fc_lock);
280 }
281 
282 /*
283  * 	Remove an entry from the specified cache of access or dma mappings
284  *
285  * 	This function must be called at or below LOCK_LEVEL.
286  */
287 void
288 ndi_fmc_remove(dev_info_t *dip, int flag, const void *resource)
289 {
290 	ndi_fmc_t *fcp;
291 	ndi_fmcentry_t *fep;
292 	struct dev_info *devi = DEVI(dip);
293 	struct i_ddi_fmhdl *fmhdl;
294 
295 	ASSERT(devi);
296 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
297 
298 	fmhdl = devi->devi_fmhdl;
299 	if (fmhdl == NULL) {
300 		i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL, DDI_NOSLEEP);
301 		return;
302 	}
303 
304 	/* Find cache entry pointer for this resource */
305 	if (flag == DMA_HANDLE) {
306 		if (!DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
307 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
308 			    DDI_NOSLEEP);
309 			return;
310 		}
311 		fcp = fmhdl->fh_dma_cache;
312 
313 		ASSERT(fcp);
314 
315 		mutex_enter(&fcp->fc_lock);
316 		fep = ((ddi_dma_impl_t *)resource)->dmai_error.err_fep;
317 		((ddi_dma_impl_t *)resource)->dmai_error.err_fep = NULL;
318 	} else if (flag == ACC_HANDLE) {
319 		if (!DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
320 			i_ddi_drv_ereport_post(dip, DVR_EFMCAP, NULL,
321 			    DDI_NOSLEEP);
322 			return;
323 		}
324 		fcp = fmhdl->fh_acc_cache;
325 
326 		ASSERT(fcp);
327 
328 		mutex_enter(&fcp->fc_lock);
329 		fep = ((ddi_acc_impl_t *)resource)->ahi_err->err_fep;
330 		((ddi_acc_impl_t *)resource)->ahi_err->err_fep = NULL;
331 	} else {
332 		return;
333 	}
334 
335 	/*
336 	 * Resource not in cache, return
337 	 */
338 	if (fep == NULL) {
339 		mutex_exit(&fcp->fc_lock);
340 		atomic_inc_64(&fmhdl->fh_kstat.fek_fmc_miss.value.ui64);
341 		return;
342 	}
343 
344 	/*
345 	 * Updates to FM cache pointers require us to grab fmc_lock
346 	 * to synchronize access to the cache for ndi_fmc_insert()
347 	 * and ndi_fmc_error()
348 	 */
349 	if (fep == fcp->fc_head)
350 		fcp->fc_head = fep->fce_next;
351 	else
352 		fep->fce_prev->fce_next = fep->fce_next;
353 	if (fep == fcp->fc_tail)
354 		fcp->fc_tail = fep->fce_prev;
355 	else
356 		fep->fce_next->fce_prev = fep->fce_prev;
357 	mutex_exit(&fcp->fc_lock);
358 
359 	kmem_cache_free(ndi_fm_entry_cache, fep);
360 }
361 
362 int
363 ndi_fmc_entry_error(dev_info_t *dip, int flag, ddi_fm_error_t *derr,
364     const void *bus_err_state)
365 {
366 	int status, fatal = 0, nonfatal = 0;
367 	ndi_fmc_t *fcp = NULL;
368 	ndi_fmcentry_t *fep;
369 	struct i_ddi_fmhdl *fmhdl;
370 
371 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
372 
373 	fmhdl = DEVI(dip)->devi_fmhdl;
374 	ASSERT(fmhdl);
375 	status = DDI_FM_UNKNOWN;
376 
377 	if (flag == DMA_HANDLE && DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
378 		fcp = fmhdl->fh_dma_cache;
379 		ASSERT(fcp);
380 	} else if (flag == ACC_HANDLE && DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
381 		fcp = fmhdl->fh_acc_cache;
382 		ASSERT(fcp);
383 	}
384 
385 	if (fcp != NULL) {
386 
387 		/*
388 		 * Check active resource entries
389 		 */
390 		mutex_enter(&fcp->fc_lock);
391 		for (fep = fcp->fc_head; fep != NULL; fep = fep->fce_next) {
392 			ddi_fmcompare_t compare_func;
393 
394 			/*
395 			 * Compare captured error state with handle
396 			 * resources.  During the comparison and
397 			 * subsequent error handling, we block
398 			 * attempts to free the cache entry.
399 			 */
400 			compare_func = (flag == ACC_HANDLE) ?
401 			    i_ddi_fm_acc_err_cf_get((ddi_acc_handle_t)
402 			    fep->fce_resource) :
403 			    i_ddi_fm_dma_err_cf_get((ddi_dma_handle_t)
404 			    fep->fce_resource);
405 
406 			status = compare_func(dip, fep->fce_resource,
407 			    bus_err_state, fep->fce_bus_specific);
408 			if (status == DDI_FM_UNKNOWN || status == DDI_FM_OK)
409 				continue;
410 
411 			if (status == DDI_FM_FATAL)
412 				++fatal;
413 			else if (status == DDI_FM_NONFATAL)
414 				++nonfatal;
415 
416 			/* Set the error for this resource handle */
417 			if (flag == ACC_HANDLE) {
418 				ddi_acc_handle_t ap = fep->fce_resource;
419 
420 				i_ddi_fm_acc_err_set(ap, derr->fme_ena, status,
421 				    DDI_FM_ERR_UNEXPECTED);
422 				ddi_fm_acc_err_get(ap, derr, DDI_FME_VERSION);
423 				derr->fme_acc_handle = ap;
424 			} else {
425 				ddi_dma_handle_t dp = fep->fce_resource;
426 
427 				i_ddi_fm_dma_err_set(dp, derr->fme_ena, status,
428 				    DDI_FM_ERR_UNEXPECTED);
429 				ddi_fm_dma_err_get(dp, derr, DDI_FME_VERSION);
430 				derr->fme_dma_handle = dp;
431 			}
432 			break;
433 		}
434 		mutex_exit(&fcp->fc_lock);
435 	}
436 	return (fatal ? DDI_FM_FATAL : nonfatal ? DDI_FM_NONFATAL :
437 	    DDI_FM_UNKNOWN);
438 }
439 
440 /*
441  * Check error state against the handle resource stored in the specified
442  * FM cache.  If tdip != NULL, we check only the cache entries for tdip.
443  * The caller must ensure that tdip is valid throughout the call and
444  * all FM data structures can be safely accesses.
445  *
446  * If tdip == NULL, we check all children that have registered their
447  * FM_DMA_CHK or FM_ACC_CHK capabilities.
448  *
449  * The following status values may be returned:
450  *
451  *	DDI_FM_FATAL - if at least one cache entry comparison yields a
452  *			fatal error.
453  *
454  *	DDI_FM_NONFATAL - if at least one cache entry comparison yields a
455  *			non-fatal error and no comparison yields a fatal error.
456  *
457  *	DDI_FM_UNKNOWN - cache entry comparisons did not yield fatal or
458  *			non-fatal errors.
459  *
460  */
461 int
462 ndi_fmc_error(dev_info_t *dip, dev_info_t *tdip, int flag, uint64_t ena,
463     const void *bus_err_state)
464 {
465 	int status, fatal = 0, nonfatal = 0;
466 	ddi_fm_error_t derr;
467 	struct i_ddi_fmhdl *fmhdl;
468 	struct i_ddi_fmtgt *tgt;
469 
470 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
471 
472 	i_ddi_fm_handler_enter(dip);
473 	fmhdl = DEVI(dip)->devi_fmhdl;
474 	ASSERT(fmhdl);
475 
476 	bzero(&derr, sizeof (ddi_fm_error_t));
477 	derr.fme_version = DDI_FME_VERSION;
478 	derr.fme_flag = DDI_FM_ERR_UNEXPECTED;
479 	derr.fme_ena = ena;
480 
481 	for (tgt = fmhdl->fh_tgts; tgt != NULL; tgt = tgt->ft_next) {
482 
483 		if (tdip != NULL && tdip != tgt->ft_dip)
484 			continue;
485 
486 		/*
487 		 * Attempt to find the entry in this childs handle cache
488 		 */
489 		status = ndi_fmc_entry_error(tgt->ft_dip, flag, &derr,
490 		    bus_err_state);
491 
492 		if (status == DDI_FM_FATAL)
493 			++fatal;
494 		else if (status == DDI_FM_NONFATAL)
495 			++nonfatal;
496 		else
497 			continue;
498 
499 		/*
500 		 * Call our child to process this error.
501 		 */
502 		status = tgt->ft_errhdl->eh_func(tgt->ft_dip, &derr,
503 		    tgt->ft_errhdl->eh_impl);
504 
505 		if (status == DDI_FM_FATAL)
506 			++fatal;
507 		else if (status == DDI_FM_NONFATAL)
508 			++nonfatal;
509 	}
510 
511 	i_ddi_fm_handler_exit(dip);
512 
513 	if (fatal)
514 		return (DDI_FM_FATAL);
515 	else if (nonfatal)
516 		return (DDI_FM_NONFATAL);
517 
518 	return (DDI_FM_UNKNOWN);
519 }
520 
521 int
522 ndi_fmc_entry_error_all(dev_info_t *dip, int flag, ddi_fm_error_t *derr)
523 {
524 	ndi_fmc_t *fcp = NULL;
525 	ndi_fmcentry_t *fep;
526 	struct i_ddi_fmhdl *fmhdl;
527 	int nonfatal = 0;
528 
529 	ASSERT(flag == DMA_HANDLE || flag == ACC_HANDLE);
530 
531 	fmhdl = DEVI(dip)->devi_fmhdl;
532 	ASSERT(fmhdl);
533 
534 	if (flag == DMA_HANDLE && DDI_FM_DMA_ERR_CAP(fmhdl->fh_cap)) {
535 		fcp = fmhdl->fh_dma_cache;
536 		ASSERT(fcp);
537 	} else if (flag == ACC_HANDLE && DDI_FM_ACC_ERR_CAP(fmhdl->fh_cap)) {
538 		fcp = fmhdl->fh_acc_cache;
539 		ASSERT(fcp);
540 	}
541 
542 	if (fcp != NULL) {
543 		/*
544 		 * Check active resource entries
545 		 */
546 		mutex_enter(&fcp->fc_lock);
547 		for (fep = fcp->fc_head; fep != NULL; fep = fep->fce_next) {
548 			/* Set the error for this resource handle */
549 			nonfatal++;
550 			if (flag == ACC_HANDLE) {
551 				ddi_acc_handle_t ap = fep->fce_resource;
552 
553 				i_ddi_fm_acc_err_set(ap, derr->fme_ena,
554 				    DDI_FM_NONFATAL, DDI_FM_ERR_UNEXPECTED);
555 				ddi_fm_acc_err_get(ap, derr, DDI_FME_VERSION);
556 				derr->fme_acc_handle = ap;
557 			} else {
558 				ddi_dma_handle_t dp = fep->fce_resource;
559 
560 				i_ddi_fm_dma_err_set(dp, derr->fme_ena,
561 				    DDI_FM_NONFATAL, DDI_FM_ERR_UNEXPECTED);
562 				ddi_fm_dma_err_get(dp, derr, DDI_FME_VERSION);
563 				derr->fme_dma_handle = dp;
564 			}
565 		}
566 		mutex_exit(&fcp->fc_lock);
567 	}
568 	return (nonfatal ? DDI_FM_NONFATAL : DDI_FM_UNKNOWN);
569 }
570 
571 /*
572  * Dispatch registered error handlers for dip.  If tdip != NULL, only
573  * the error handler (if available) for tdip is invoked.  Otherwise,
574  * all registered error handlers are invoked.
575  *
576  * The following status values may be returned:
577  *
578  *	DDI_FM_FATAL - if at least one error handler returns a
579  *			fatal error.
580  *
581  *	DDI_FM_NONFATAL - if at least one error handler returns a
582  *			non-fatal error and none returned a fatal error.
583  *
584  *	DDI_FM_UNKNOWN - if at least one error handler returns
585  *			unknown status and none return fatal or non-fatal.
586  *
587  *	DDI_FM_OK - if all error handlers return DDI_FM_OK
588  */
589 int
590 ndi_fm_handler_dispatch(dev_info_t *dip, dev_info_t *tdip,
591     const ddi_fm_error_t *nerr)
592 {
593 	int status;
594 	int unknown = 0, fatal = 0, nonfatal = 0;
595 	struct i_ddi_fmhdl *hdl;
596 	struct i_ddi_fmtgt *tgt;
597 
598 	status = DDI_FM_UNKNOWN;
599 
600 	i_ddi_fm_handler_enter(dip);
601 	hdl = DEVI(dip)->devi_fmhdl;
602 	tgt = hdl->fh_tgts;
603 	while (tgt != NULL) {
604 		if (tdip == NULL || tdip == tgt->ft_dip) {
605 			struct i_ddi_errhdl *errhdl;
606 
607 			errhdl = tgt->ft_errhdl;
608 			status = errhdl->eh_func(tgt->ft_dip, nerr,
609 			    errhdl->eh_impl);
610 
611 			if (status == DDI_FM_FATAL)
612 				++fatal;
613 			else if (status == DDI_FM_NONFATAL)
614 				++nonfatal;
615 			else if (status == DDI_FM_UNKNOWN)
616 				++unknown;
617 
618 			/* Only interested in one target */
619 			if (tdip != NULL)
620 				break;
621 		}
622 		tgt = tgt->ft_next;
623 	}
624 	i_ddi_fm_handler_exit(dip);
625 
626 	if (fatal)
627 		return (DDI_FM_FATAL);
628 	else if (nonfatal)
629 		return (DDI_FM_NONFATAL);
630 	else if (unknown)
631 		return (DDI_FM_UNKNOWN);
632 	else
633 		return (DDI_FM_OK);
634 }
635 
636 /*
637  * Set error status for specified access or DMA handle
638  *
639  * May be called in any context but caller must insure validity of
640  * handle.
641  */
642 void
643 ndi_fm_acc_err_set(ddi_acc_handle_t handle, ddi_fm_error_t *dfe)
644 {
645 	i_ddi_fm_acc_err_set(handle, dfe->fme_ena, dfe->fme_status,
646 	    dfe->fme_flag);
647 }
648 
649 void
650 ndi_fm_dma_err_set(ddi_dma_handle_t handle, ddi_fm_error_t *dfe)
651 {
652 	i_ddi_fm_dma_err_set(handle, dfe->fme_ena, dfe->fme_status,
653 	    dfe->fme_flag);
654 }
655 
656 /*
657  * Call parent busop fm initialization routine.
658  *
659  * Called during driver attach(1M)
660  */
661 int
662 i_ndi_busop_fm_init(dev_info_t *dip, int tcap, ddi_iblock_cookie_t *ibc)
663 {
664 	int pcap;
665 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
666 
667 	if (dip == ddi_root_node())
668 		return (ddi_system_fmcap | DDI_FM_EREPORT_CAPABLE);
669 
670 	/* Valid operation for BUSO_REV_6 and above */
671 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
672 		return (DDI_FM_NOT_CAPABLE);
673 
674 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_init == NULL)
675 		return (DDI_FM_NOT_CAPABLE);
676 
677 	pcap = (*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_init)
678 	    (pdip, dip, tcap, ibc);
679 
680 	return (pcap);
681 }
682 
683 /*
684  * Call parent busop fm clean-up routine.
685  *
686  * Called during driver detach(1M)
687  */
688 void
689 i_ndi_busop_fm_fini(dev_info_t *dip)
690 {
691 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
692 
693 	if (dip == ddi_root_node())
694 		return;
695 
696 	/* Valid operation for BUSO_REV_6 and above */
697 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
698 		return;
699 
700 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_fini == NULL)
701 		return;
702 
703 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_fini)(pdip, dip);
704 }
705 
706 /*
707  * The following routines provide exclusive access to a nexus resource
708  *
709  * These busops may be called in user or kernel driver context.
710  */
711 void
712 i_ndi_busop_access_enter(dev_info_t *dip, ddi_acc_handle_t handle)
713 {
714 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
715 
716 	/* Valid operation for BUSO_REV_6 and above */
717 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
718 		return;
719 
720 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_enter == NULL)
721 		return;
722 
723 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_enter)
724 	    (pdip, handle);
725 }
726 
727 void
728 i_ndi_busop_access_exit(dev_info_t *dip, ddi_acc_handle_t handle)
729 {
730 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
731 
732 	/* Valid operation for BUSO_REV_6 and above */
733 	if (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6)
734 		return;
735 
736 	if (DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_exit == NULL)
737 		return;
738 
739 	(*DEVI(pdip)->devi_ops->devo_bus_ops->bus_fm_access_exit)(pdip, handle);
740 }
741