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