xref: /illumos-gate/usr/src/uts/sun4/io/px/px_fm.c (revision f41150ba)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PX Fault Management Architecture
30  */
31 #include <sys/types.h>
32 #include <sys/sunndi.h>
33 #include <sys/sunddi.h>
34 #include <sys/fm/protocol.h>
35 #include <sys/fm/util.h>
36 #include <sys/fm/io/pci.h>
37 #include <sys/membar.h>
38 #include "px_obj.h"
39 
40 #define	PX_PCIE_PANIC_BITS \
41 	(PCIE_AER_UCE_DLP | PCIE_AER_UCE_FCP | PCIE_AER_UCE_TO | \
42 	PCIE_AER_UCE_RO | PCIE_AER_UCE_MTLP | PCIE_AER_UCE_ECRC)
43 #define	PX_PCIE_NO_PANIC_BITS \
44 	(PCIE_AER_UCE_TRAINING | PCIE_AER_UCE_SD | PCIE_AER_UCE_CA | \
45 	PCIE_AER_UCE_UC | PCIE_AER_UCE_UR)
46 
47 /*
48  * Global panicing state variabled used to control if further error handling
49  * should occur.  If the system is already panic'ing or if PX itself has
50  * recommended panic'ing the system, no further error handling should occur to
51  * prevent the system from hanging.
52  */
53 boolean_t px_panicing = B_FALSE;
54 
55 static pf_data_t *px_get_pfd(px_t *px_p);
56 
57 static int px_pcie_ptlp(dev_info_t *dip, ddi_fm_error_t *derr,
58     px_err_pcie_t *regs);
59 
60 #if defined(DEBUG)
61 static void px_pcie_log(dev_info_t *dip, px_err_pcie_t *regs);
62 #else	/* DEBUG */
63 #define	px_pcie_log 0 &&
64 #endif	/* DEBUG */
65 
66 /*
67  * Initialize px FMA support
68  */
69 int
70 px_fm_attach(px_t *px_p)
71 {
72 	int		i;
73 	dev_info_t	*dip = px_p->px_dip;
74 	pcie_bus_t	*bus_p;
75 
76 	px_p->px_fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
77 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
78 
79 	/*
80 	 * Initialize pci_target_queue for FMA handling of
81 	 * pci errors.
82 	 */
83 	pci_targetq_init();
84 
85 	/*
86 	 * check parents' capability
87 	 */
88 	ddi_fm_init(dip, &px_p->px_fm_cap, &px_p->px_fm_ibc);
89 
90 	/*
91 	 * parents need to be ereport and error handling capable
92 	 */
93 	ASSERT(px_p->px_fm_cap &&
94 	    (DDI_FM_ERRCB_CAPABLE | DDI_FM_EREPORT_CAPABLE));
95 
96 	/*
97 	 * Initialize lock to synchronize fabric error handling
98 	 */
99 	mutex_init(&px_p->px_fm_mutex, NULL, MUTEX_DRIVER,
100 	    (void *)px_p->px_fm_ibc);
101 
102 
103 	pcie_rc_init_bus(dip);
104 
105 	px_p->px_pfd_idx = 0;
106 	for (i = 0; i < 5; i++)
107 		pcie_rc_init_pfd(dip, &px_p->px_pfd_arr[i]);
108 	PCIE_DIP2PFD(dip) = px_p->px_pfd_arr;
109 
110 	bus_p = PCIE_DIP2BUS(dip);
111 	bus_p->bus_rp_bdf = px_p->px_bdf;
112 	bus_p->bus_rp_dip = dip;
113 
114 	/*
115 	 * register error callback in parent
116 	 */
117 	ddi_fm_handler_register(dip, px_fm_callback, px_p);
118 
119 	return (DDI_SUCCESS);
120 }
121 
122 /*
123  * Deregister FMA
124  */
125 void
126 px_fm_detach(px_t *px_p)
127 {
128 	int i;
129 
130 	ddi_fm_handler_unregister(px_p->px_dip);
131 	mutex_destroy(&px_p->px_fm_mutex);
132 	ddi_fm_fini(px_p->px_dip);
133 	for (i = 0; i < 5; i++)
134 		pcie_rc_fini_pfd(&px_p->px_pfd_arr[i]);
135 	pcie_rc_fini_bus(px_p->px_dip);
136 }
137 
138 /*
139  * Function used to setup access functions depending on level of desired
140  * protection.
141  */
142 void
143 px_fm_acc_setup(ddi_map_req_t *mp, dev_info_t *rdip, pci_regspec_t *rp)
144 {
145 	uchar_t fflag;
146 	ndi_err_t *errp;
147 	ddi_acc_hdl_t *hp;
148 	ddi_acc_impl_t *ap;
149 
150 	hp = mp->map_handlep;
151 	ap = (ddi_acc_impl_t *)hp->ah_platform_private;
152 	fflag = ap->ahi_common.ah_acc.devacc_attr_access;
153 
154 	if (mp->map_op == DDI_MO_MAP_LOCKED) {
155 		ndi_fmc_insert(rdip, ACC_HANDLE, (void *)hp, NULL);
156 		switch (fflag) {
157 		case DDI_FLAGERR_ACC:
158 			ap->ahi_get8 = i_ddi_prot_get8;
159 			ap->ahi_get16 = i_ddi_prot_get16;
160 			ap->ahi_get32 = i_ddi_prot_get32;
161 			ap->ahi_get64 = i_ddi_prot_get64;
162 			ap->ahi_put8 = i_ddi_prot_put8;
163 			ap->ahi_put16 = i_ddi_prot_put16;
164 			ap->ahi_put32 = i_ddi_prot_put32;
165 			ap->ahi_put64 = i_ddi_prot_put64;
166 			ap->ahi_rep_get8 = i_ddi_prot_rep_get8;
167 			ap->ahi_rep_get16 = i_ddi_prot_rep_get16;
168 			ap->ahi_rep_get32 = i_ddi_prot_rep_get32;
169 			ap->ahi_rep_get64 = i_ddi_prot_rep_get64;
170 			ap->ahi_rep_put8 = i_ddi_prot_rep_put8;
171 			ap->ahi_rep_put16 = i_ddi_prot_rep_put16;
172 			ap->ahi_rep_put32 = i_ddi_prot_rep_put32;
173 			ap->ahi_rep_put64 = i_ddi_prot_rep_put64;
174 			impl_acc_err_init(hp);
175 			errp = ((ddi_acc_impl_t *)hp)->ahi_err;
176 			if ((rp->pci_phys_hi & PCI_REG_ADDR_M) ==
177 			    PCI_ADDR_CONFIG)
178 				errp->err_cf = px_err_cfg_hdl_check;
179 			else
180 				errp->err_cf = px_err_pio_hdl_check;
181 			break;
182 		case DDI_CAUTIOUS_ACC :
183 			ap->ahi_get8 = i_ddi_caut_get8;
184 			ap->ahi_get16 = i_ddi_caut_get16;
185 			ap->ahi_get32 = i_ddi_caut_get32;
186 			ap->ahi_get64 = i_ddi_caut_get64;
187 			ap->ahi_put8 = i_ddi_caut_put8;
188 			ap->ahi_put16 = i_ddi_caut_put16;
189 			ap->ahi_put32 = i_ddi_caut_put32;
190 			ap->ahi_put64 = i_ddi_caut_put64;
191 			ap->ahi_rep_get8 = i_ddi_caut_rep_get8;
192 			ap->ahi_rep_get16 = i_ddi_caut_rep_get16;
193 			ap->ahi_rep_get32 = i_ddi_caut_rep_get32;
194 			ap->ahi_rep_get64 = i_ddi_caut_rep_get64;
195 			ap->ahi_rep_put8 = i_ddi_caut_rep_put8;
196 			ap->ahi_rep_put16 = i_ddi_caut_rep_put16;
197 			ap->ahi_rep_put32 = i_ddi_caut_rep_put32;
198 			ap->ahi_rep_put64 = i_ddi_caut_rep_put64;
199 			impl_acc_err_init(hp);
200 			errp = ((ddi_acc_impl_t *)hp)->ahi_err;
201 			if ((rp->pci_phys_hi & PCI_REG_ADDR_M) ==
202 			    PCI_ADDR_CONFIG)
203 				errp->err_cf = px_err_cfg_hdl_check;
204 			else
205 				errp->err_cf = px_err_pio_hdl_check;
206 			break;
207 		default:
208 			break;
209 		}
210 	} else if (mp->map_op == DDI_MO_UNMAP) {
211 		ndi_fmc_remove(rdip, ACC_HANDLE, (void *)hp);
212 	}
213 }
214 
215 /*
216  * Function used to initialize FMA for our children nodes. Called
217  * through pci busops when child node calls ddi_fm_init.
218  */
219 /*ARGSUSED*/
220 int
221 px_fm_init_child(dev_info_t *dip, dev_info_t *cdip, int cap,
222     ddi_iblock_cookie_t *ibc_p)
223 {
224 	px_t *px_p = DIP_TO_STATE(dip);
225 
226 	ASSERT(ibc_p != NULL);
227 	*ibc_p = px_p->px_fm_ibc;
228 
229 	return (px_p->px_fm_cap);
230 }
231 
232 /*
233  * lock access for exclusive PCIe access
234  */
235 void
236 px_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
237 {
238 	px_pec_t	*pec_p = ((px_t *)DIP_TO_STATE(dip))->px_pec_p;
239 
240 	/*
241 	 * Exclusive access has been used for cautious put/get,
242 	 * Both utilize i_ddi_ontrap which, on sparcv9, implements
243 	 * similar protection as what on_trap() does, and which calls
244 	 * membar  #Sync to flush out all cpu deferred errors
245 	 * prior to get/put operation, so here we're not calling
246 	 * membar  #Sync - a difference from what's in pci_bus_enter().
247 	 */
248 	mutex_enter(&pec_p->pec_pokefault_mutex);
249 	pec_p->pec_acc_hdl = handle;
250 }
251 
252 /*
253  * unlock access for exclusive PCIe access
254  */
255 /* ARGSUSED */
256 void
257 px_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
258 {
259 	px_t		*px_p = DIP_TO_STATE(dip);
260 	px_pec_t	*pec_p = px_p->px_pec_p;
261 
262 	pec_p->pec_acc_hdl = NULL;
263 	mutex_exit(&pec_p->pec_pokefault_mutex);
264 }
265 
266 static uint64_t
267 px_in_addr_range(dev_info_t *dip, px_ranges_t *ranges_p, uint64_t addr)
268 {
269 	uint64_t	addr_low, addr_high;
270 
271 	addr_low = ((uint64_t)ranges_p->parent_high << 32) |
272 	    (uint64_t)ranges_p->parent_low;
273 	addr_high = addr_low + ((uint64_t)ranges_p->size_high << 32) +
274 	    (uint64_t)ranges_p->size_low;
275 
276 	DBG(DBG_ERR_INTR, dip, "Addr: 0x%llx high: 0x%llx low: 0x%llx\n",
277 	    addr, addr_high, addr_low);
278 
279 	if ((addr < addr_high) && (addr >= addr_low))
280 		return (addr_low);
281 
282 	return (0);
283 }
284 
285 /*
286  * PCI error callback which is registered with our parent to call
287  * for PCIe logging when the CPU traps due to PCIe Uncorrectable Errors
288  * and PCI BERR/TO/UE on IO Loads.
289  */
290 /*ARGSUSED*/
291 int
292 px_fm_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
293 {
294 	dev_info_t	*pdip = ddi_get_parent(dip);
295 	px_t		*px_p = (px_t *)impl_data;
296 	int		i, acc_type = 0;
297 	int		lookup, rc_err, fab_err;
298 	uint64_t	addr, base_addr;
299 	uint64_t	fault_addr = (uint64_t)derr->fme_bus_specific;
300 	pcie_req_id_t	bdf;
301 	px_ranges_t	*ranges_p;
302 	int		range_len;
303 
304 	/*
305 	 * If the current thread already owns the px_fm_mutex, then we
306 	 * have encountered an error while processing a previous
307 	 * error.  Attempting to take the mutex again will cause the
308 	 * system to deadlock.
309 	 */
310 	if (px_p->px_fm_mutex_owner == curthread)
311 		return (DDI_FM_FATAL);
312 
313 	i_ddi_fm_handler_exit(pdip);
314 	if (px_fm_enter(px_p) != DDI_SUCCESS) {
315 		i_ddi_fm_handler_enter(pdip);
316 		return (DDI_FM_FATAL);
317 	}
318 
319 	/*
320 	 * Make sure this failed load came from this PCIe port.	 Check by
321 	 * matching the upper 32 bits of the address with the ranges property.
322 	 */
323 	range_len = px_p->px_ranges_length / sizeof (px_ranges_t);
324 	i = 0;
325 	for (ranges_p = px_p->px_ranges_p; i < range_len; i++, ranges_p++) {
326 		base_addr = px_in_addr_range(dip, ranges_p, fault_addr);
327 		if (base_addr) {
328 			switch (ranges_p->child_high & PCI_ADDR_MASK) {
329 			case PCI_ADDR_CONFIG:
330 				acc_type = PF_ADDR_CFG;
331 				addr = NULL;
332 				bdf = (pcie_req_id_t)((fault_addr >> 12) &
333 				    0xFFFF);
334 				break;
335 			case PCI_ADDR_IO:
336 			case PCI_ADDR_MEM64:
337 			case PCI_ADDR_MEM32:
338 				acc_type = PF_ADDR_PIO;
339 				addr = fault_addr - base_addr;
340 				bdf = NULL;
341 				break;
342 			}
343 			break;
344 		}
345 	}
346 
347 	/* This address doesn't belong to this leaf, just return with OK */
348 	if (!acc_type) {
349 		px_fm_exit(px_p);
350 		i_ddi_fm_handler_enter(pdip);
351 		return (DDI_FM_OK);
352 	}
353 
354 	rc_err = px_err_cmn_intr(px_p, derr, PX_TRAP_CALL, PX_FM_BLOCK_ALL);
355 	lookup = pf_hdl_lookup(dip, derr->fme_ena, acc_type, (uint64_t)addr,
356 	    bdf);
357 
358 	px_rp_en_q(px_p, bdf, addr,
359 	    (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB));
360 
361 	fab_err = px_scan_fabric(px_p, dip, derr);
362 
363 	px_fm_exit(px_p);
364 	i_ddi_fm_handler_enter(pdip);
365 
366 	if (!px_die)
367 		return (DDI_FM_OK);
368 
369 	if ((rc_err & (PX_PANIC | PX_PROTECTED)) ||
370 	    (fab_err & PF_ERR_FATAL_FLAGS) ||
371 	    (lookup == PF_HDL_NOTFOUND))
372 		return (DDI_FM_FATAL);
373 	else if ((rc_err == PX_NO_ERROR) && (fab_err == PF_ERR_NO_ERROR))
374 		return (DDI_FM_OK);
375 
376 	return (DDI_FM_NONFATAL);
377 }
378 
379 /*
380  * px_err_fabric_intr:
381  * Interrupt handler for PCIE fabric block.
382  * o lock
383  * o create derr
384  * o px_err_cmn_intr(leaf, with jbc)
385  * o send ereport(fire fmri, derr, payload = BDF)
386  * o dispatch (leaf)
387  * o unlock
388  * o handle error: fatal? fm_panic() : return INTR_CLAIMED)
389  */
390 /* ARGSUSED */
391 uint_t
392 px_err_fabric_intr(px_t *px_p, msgcode_t msg_code, pcie_req_id_t rid)
393 {
394 	dev_info_t	*rpdip = px_p->px_dip;
395 	int		rc_err, fab_err;
396 	ddi_fm_error_t	derr;
397 	uint32_t	rp_status;
398 	uint16_t	ce_source, ue_source;
399 
400 	if (px_fm_enter(px_p) != DDI_SUCCESS)
401 		goto done;
402 
403 	/* Create the derr */
404 	bzero(&derr, sizeof (ddi_fm_error_t));
405 	derr.fme_version = DDI_FME_VERSION;
406 	derr.fme_ena = fm_ena_generate(0, FM_ENA_FMT1);
407 	derr.fme_flag = DDI_FM_ERR_UNEXPECTED;
408 
409 	px_err_safeacc_check(px_p, &derr);
410 
411 	if (msg_code == PCIE_MSG_CODE_ERR_COR) {
412 		rp_status = PCIE_AER_RE_STS_CE_RCVD;
413 		ce_source = rid;
414 		ue_source = 0;
415 	} else {
416 		rp_status = PCIE_AER_RE_STS_FE_NFE_RCVD;
417 		ce_source = 0;
418 		ue_source = rid;
419 		if (msg_code == PCIE_MSG_CODE_ERR_NONFATAL)
420 			rp_status |= PCIE_AER_RE_STS_NFE_MSGS_RCVD;
421 		else {
422 			rp_status |= PCIE_AER_RE_STS_FE_MSGS_RCVD;
423 			rp_status |= PCIE_AER_RE_STS_FIRST_UC_FATAL;
424 		}
425 	}
426 
427 	if (derr.fme_flag == DDI_FM_ERR_UNEXPECTED) {
428 		ddi_fm_ereport_post(rpdip, PCI_ERROR_SUBCLASS "." PCIEX_FABRIC,
429 		    derr.fme_ena,
430 		    DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8, 0,
431 		    FIRE_PRIMARY, DATA_TYPE_BOOLEAN_VALUE, B_TRUE,
432 		    "pcie_adv_rp_status", DATA_TYPE_UINT32, rp_status,
433 		    "pcie_adv_rp_command", DATA_TYPE_UINT32, 0,
434 		    "pcie_adv_rp_ce_src_id", DATA_TYPE_UINT16, ce_source,
435 		    "pcie_adv_rp_ue_src_id", DATA_TYPE_UINT16, ue_source,
436 		    NULL);
437 	}
438 
439 	/* Ensure that the rid of the fabric message will get scanned. */
440 	px_rp_en_q(px_p, rid, NULL, NULL);
441 
442 	rc_err = px_err_cmn_intr(px_p, &derr, PX_INTR_CALL, PX_FM_BLOCK_PCIE);
443 
444 	/* call rootport dispatch */
445 	fab_err = px_scan_fabric(px_p, rpdip, &derr);
446 
447 	px_err_panic(rc_err, PX_RC, fab_err, B_TRUE);
448 	px_fm_exit(px_p);
449 	px_err_panic(rc_err, PX_RC, fab_err, B_FALSE);
450 
451 done:
452 	return (DDI_INTR_CLAIMED);
453 }
454 
455 /*
456  * px_scan_fabric:
457  *
458  * Check for drain state and if there is anything to scan.
459  */
460 int
461 px_scan_fabric(px_t *px_p, dev_info_t *rpdip, ddi_fm_error_t *derr) {
462 	int fab_err = 0;
463 
464 	ASSERT(MUTEX_HELD(&px_p->px_fm_mutex));
465 
466 	if (!px_lib_is_in_drain_state(px_p) && px_p->px_pfd_idx) {
467 		fab_err = pf_scan_fabric(rpdip, derr, px_p->px_pfd_arr);
468 		px_p->px_pfd_idx = 0;
469 	}
470 
471 	return (fab_err);
472 }
473 
474 /*
475  * px_err_safeacc_check:
476  * Check to see if a peek/poke and cautious access is currently being
477  * done on a particular leaf.
478  *
479  * Safe access reads induced fire errors will be handled by cpu trap handler
480  * which will call px_fm_callback() which calls this function. In that
481  * case, the derr fields will be set by trap handler with the correct values.
482  *
483  * Safe access writes induced errors will be handled by px interrupt
484  * handlers, this function will fill in the derr fields.
485  *
486  * If a cpu trap does occur, it will quiesce all other interrupts allowing
487  * the cpu trap error handling to finish before Fire receives an interrupt.
488  *
489  * If fire does indeed have an error when a cpu trap occurs as a result of
490  * a safe access, a trap followed by a Mondo/Fabric interrupt will occur.
491  * In which case derr will be initialized as "UNEXPECTED" by the interrupt
492  * handler and this function will need to find if this error occured in the
493  * middle of a safe access operation.
494  *
495  * @param px_p		leaf in which to check access
496  * @param derr		fm err data structure to be updated
497  */
498 void
499 px_err_safeacc_check(px_t *px_p, ddi_fm_error_t *derr)
500 {
501 	px_pec_t 	*pec_p = px_p->px_pec_p;
502 	int		acctype = pec_p->pec_safeacc_type;
503 
504 	ASSERT(MUTEX_HELD(&px_p->px_fm_mutex));
505 
506 	if (derr->fme_flag != DDI_FM_ERR_UNEXPECTED) {
507 		return;
508 	}
509 
510 	/* safe access checking */
511 	switch (acctype) {
512 	case DDI_FM_ERR_EXPECTED:
513 		/*
514 		 * cautious access protection, protected from all err.
515 		 */
516 		ddi_fm_acc_err_get(pec_p->pec_acc_hdl, derr,
517 		    DDI_FME_VERSION);
518 		derr->fme_flag = acctype;
519 		derr->fme_acc_handle = pec_p->pec_acc_hdl;
520 		break;
521 	case DDI_FM_ERR_POKE:
522 		/*
523 		 * ddi_poke protection, check nexus and children for
524 		 * expected errors.
525 		 */
526 		membar_sync();
527 		derr->fme_flag = acctype;
528 		break;
529 	case DDI_FM_ERR_PEEK:
530 		derr->fme_flag = acctype;
531 		break;
532 	}
533 }
534 
535 /*
536  * Suggest panic if any EQ (except CE q) has overflown.
537  */
538 int
539 px_err_check_eq(dev_info_t *dip)
540 {
541 	px_t			*px_p = DIP_TO_STATE(dip);
542 	px_msiq_state_t 	*msiq_state_p = &px_p->px_ib_p->ib_msiq_state;
543 	px_pec_t		*pec_p = px_p->px_pec_p;
544 	msiqid_t		eq_no = msiq_state_p->msiq_1st_msiq_id;
545 	pci_msiq_state_t	msiq_state;
546 	int			i;
547 
548 	for (i = 0; i < msiq_state_p->msiq_cnt; i++) {
549 		if (i + eq_no == pec_p->pec_corr_msg_msiq_id) /* skip CE q */
550 			continue;
551 		if ((px_lib_msiq_getstate(dip, i + eq_no, &msiq_state) !=
552 		    DDI_SUCCESS) || msiq_state == PCI_MSIQ_STATE_ERROR)
553 			return (PX_PANIC);
554 	}
555 	return (PX_NO_PANIC);
556 }
557 
558 /* ARGSUSED */
559 int
560 px_err_check_pcie(dev_info_t *dip, ddi_fm_error_t *derr, px_err_pcie_t *regs)
561 {
562 	px_t		*px_p = DIP_TO_STATE(dip);
563 	pf_data_t	*pfd_p = px_get_pfd(px_p);
564 	int		i;
565 	pf_pcie_adv_err_regs_t *adv_reg = PCIE_ADV_REG(pfd_p);
566 
567 	/*
568 	 * set RC s_status in PCI term to coordinate with downstream fabric
569 	 * errors ananlysis.
570 	 */
571 	if (regs->primary_ue & PCIE_AER_UCE_UR)
572 		PCI_BDG_ERR_REG(pfd_p)->pci_bdg_sec_stat = PCI_STAT_R_MAST_AB;
573 	if (regs->primary_ue & PCIE_AER_UCE_CA)
574 		PCI_BDG_ERR_REG(pfd_p)->pci_bdg_sec_stat = PCI_STAT_R_TARG_AB;
575 	if (regs->primary_ue & (PCIE_AER_UCE_PTLP | PCIE_AER_UCE_ECRC))
576 		PCI_BDG_ERR_REG(pfd_p)->pci_bdg_sec_stat = PCI_STAT_PERROR;
577 
578 	adv_reg->pcie_ce_status = regs->ce_reg;
579 	adv_reg->pcie_ue_status = regs->ue_reg | regs->primary_ue;
580 	PCIE_ADV_HDR(pfd_p, 0) = regs->rx_hdr1;
581 	PCIE_ADV_HDR(pfd_p, 1) = regs->rx_hdr2;
582 	PCIE_ADV_HDR(pfd_p, 2) = regs->rx_hdr3;
583 	PCIE_ADV_HDR(pfd_p, 3) = regs->rx_hdr4;
584 	for (i = regs->primary_ue; i != 1; i = i >> 1)
585 		adv_reg->pcie_adv_ctl++;
586 
587 	if (regs->primary_ue & (PCIE_AER_UCE_UR | PCIE_AER_UCE_CA)) {
588 		if (pf_tlp_decode(PCIE_DIP2BUS(dip), adv_reg) == DDI_SUCCESS)
589 			PCIE_ROOT_FAULT(pfd_p)->fault_bdf =
590 			    adv_reg->pcie_ue_tgt_bdf;
591 	} else if (regs->primary_ue & PCIE_AER_UCE_PTLP) {
592 		if (pf_tlp_decode(PCIE_DIP2BUS(dip), adv_reg) == DDI_SUCCESS) {
593 			PCIE_ROOT_FAULT(pfd_p)->fault_bdf =
594 			    adv_reg->pcie_ue_tgt_bdf;
595 			if (adv_reg->pcie_ue_tgt_trans ==
596 			    PF_ADDR_PIO)
597 				PCIE_ROOT_FAULT(pfd_p)->fault_addr =
598 				    adv_reg->pcie_ue_tgt_addr;
599 		}
600 
601 		/*
602 		 * Normally for Poisoned Completion TLPs we can look at the
603 		 * transmit log header for the original request and the original
604 		 * address, however this doesn't seem to be working.  HW BUG.
605 		 */
606 	}
607 
608 	px_pcie_log(dip, regs);
609 
610 	/* Return No Error here and let the pcie misc module analyse it */
611 	return (PX_NO_ERROR);
612 }
613 
614 #if defined(DEBUG)
615 static void
616 px_pcie_log(dev_info_t *dip, px_err_pcie_t *regs)
617 {
618 	DBG(DBG_ERR_INTR, dip,
619 	    "A PCIe RC error has occured\n"
620 	    "\tCE: 0x%x UE: 0x%x Primary UE: 0x%x\n"
621 	    "\tTX Hdr: 0x%x 0x%x 0x%x 0x%x\n\tRX Hdr: 0x%x 0x%x 0x%x 0x%x\n",
622 	    regs->ce_reg, regs->ue_reg, regs->primary_ue,
623 	    regs->tx_hdr1, regs->tx_hdr2, regs->tx_hdr3, regs->tx_hdr4,
624 	    regs->rx_hdr1, regs->rx_hdr2, regs->rx_hdr3, regs->rx_hdr4);
625 }
626 #endif
627 
628 /*
629  * look through poisoned TLP cases and suggest panic/no panic depend on
630  * handle lookup.
631  */
632 static int
633 px_pcie_ptlp(dev_info_t *dip, ddi_fm_error_t *derr, px_err_pcie_t *regs)
634 {
635 	pf_pcie_adv_err_regs_t adv_reg;
636 	pcie_req_id_t	bdf;
637 	uint64_t	addr;
638 	uint32_t	trans_type;
639 	int		tlp_sts, tlp_cmd;
640 	int		lookup = PF_HDL_NOTFOUND;
641 
642 	if (regs->primary_ue != PCIE_AER_UCE_PTLP)
643 		return (PX_PANIC);
644 
645 	if (!regs->rx_hdr1)
646 		goto done;
647 
648 	adv_reg.pcie_ue_hdr[0] = regs->rx_hdr1;
649 	adv_reg.pcie_ue_hdr[1] = regs->rx_hdr2;
650 	adv_reg.pcie_ue_hdr[2] = regs->rx_hdr3;
651 	adv_reg.pcie_ue_hdr[3] = regs->rx_hdr4;
652 
653 	tlp_sts = pf_tlp_decode(PCIE_DIP2BUS(dip), &adv_reg);
654 	tlp_cmd = ((pcie_tlp_hdr_t *)(adv_reg.pcie_ue_hdr))->type;
655 
656 	if (tlp_sts == DDI_FAILURE)
657 		goto done;
658 
659 	bdf = adv_reg.pcie_ue_tgt_bdf;
660 	addr = adv_reg.pcie_ue_tgt_addr;
661 	trans_type = adv_reg.pcie_ue_tgt_trans;
662 
663 	switch (tlp_cmd) {
664 	case PCIE_TLP_TYPE_CPL:
665 	case PCIE_TLP_TYPE_CPLLK:
666 		/*
667 		 * Usually a PTLP is a CPL with data.  Grab the completer BDF
668 		 * from the RX TLP, and the original address from the TX TLP.
669 		 */
670 		if (regs->tx_hdr1) {
671 			adv_reg.pcie_ue_hdr[0] = regs->tx_hdr1;
672 			adv_reg.pcie_ue_hdr[1] = regs->tx_hdr2;
673 			adv_reg.pcie_ue_hdr[2] = regs->tx_hdr3;
674 			adv_reg.pcie_ue_hdr[3] = regs->tx_hdr4;
675 
676 			lookup = pf_tlp_decode(PCIE_DIP2BUS(dip), &adv_reg);
677 			if (lookup != DDI_SUCCESS)
678 				break;
679 			addr = adv_reg.pcie_ue_tgt_addr;
680 			trans_type = adv_reg.pcie_ue_tgt_trans;
681 		} /* FALLTHRU */
682 	case PCIE_TLP_TYPE_IO:
683 	case PCIE_TLP_TYPE_MEM:
684 	case PCIE_TLP_TYPE_MEMLK:
685 		lookup = pf_hdl_lookup(dip, derr->fme_ena, trans_type, addr,
686 		    bdf);
687 		break;
688 	default:
689 		lookup = PF_HDL_NOTFOUND;
690 	}
691 done:
692 	return (lookup == PF_HDL_FOUND ? PX_NO_PANIC : PX_PANIC);
693 }
694 
695 /*
696  * px_get_pdf automatically allocates a RC pf_data_t and returns a pointer to
697  * it.  This function should be used when an error requires a fabric scan.
698  */
699 static pf_data_t *
700 px_get_pfd(px_t *px_p) {
701 	int		idx = px_p->px_pfd_idx++;
702 	pf_data_t	*pfd_p = &px_p->px_pfd_arr[idx];
703 
704 	/* Clear Old Data */
705 	PCIE_ROOT_FAULT(pfd_p)->fault_bdf = 0;
706 	PCIE_ROOT_FAULT(pfd_p)->fault_addr = 0;
707 	PCI_BDG_ERR_REG(pfd_p)->pci_bdg_sec_stat = 0;
708 	PCIE_ADV_REG(pfd_p)->pcie_ce_status = 0;
709 	PCIE_ADV_REG(pfd_p)->pcie_ue_status = 0;
710 
711 	pfd_p->pe_next = NULL;
712 
713 	if (idx > 0) {
714 		px_p->px_pfd_arr[idx - 1].pe_next = pfd_p;
715 		pfd_p->pe_prev = &px_p->px_pfd_arr[idx - 1];
716 	} else {
717 		pfd_p->pe_prev = NULL;
718 	}
719 
720 	pfd_p->pe_valid = B_TRUE;
721 
722 	return (pfd_p);
723 }
724 
725 /*
726  * This function appends a pf_data structure to the error q which is used later
727  * during PCIe fabric scan.  It signifies:
728  * o errs rcvd in RC, that may have been propagated to/from the fabric
729  * o the fabric scan code should scan the device path of fault bdf/addr
730  *
731  * fault_bdf: The bdf that caused the fault, which may have error bits set.
732  * fault_addr: The PIO addr that caused the fault, such as failed PIO, but not
733  *	       failed DMAs.
734  * s_status: Secondary Status equivalent to why the fault occured.
735  *	     (ie S-TA/MA, R-TA)
736  * Either the fault bdf or addr may be NULL, but not both.
737  */
738 void
739 px_rp_en_q(px_t *px_p, pcie_req_id_t fault_bdf, uint32_t fault_addr,
740     uint16_t s_status)
741 {
742 	pf_data_t	*pfd_p;
743 
744 	if (!fault_bdf && !fault_addr)
745 		return;
746 
747 	pfd_p = px_get_pfd(px_p);
748 
749 	PCIE_ROOT_FAULT(pfd_p)->fault_bdf = fault_bdf;
750 	PCIE_ROOT_FAULT(pfd_p)->fault_addr = (uint64_t)fault_addr;
751 	PCI_BDG_ERR_REG(pfd_p)->pci_bdg_sec_stat = s_status;
752 }
753 
754 
755 /*
756  * Find and Mark CFG Handles as failed associated with the given BDF. We should
757  * always know the BDF for CFG accesses, since it is encoded in the address of
758  * the TLP.  Since there can be multiple cfg handles, mark them all as failed.
759  */
760 /* ARGSUSED */
761 int
762 px_err_cfg_hdl_check(dev_info_t *dip, const void *handle, const void *arg1,
763     const void *arg2)
764 {
765 	int			status = DDI_FM_FATAL;
766 	uint32_t		addr = *(uint32_t *)arg1;
767 	uint16_t		bdf = *(uint16_t *)arg2;
768 	pcie_bus_t		*bus_p;
769 
770 	DBG(DBG_ERR_INTR, dip, "Check CFG Hdl: dip 0x%p addr 0x%x bdf=0x%x\n",
771 	    dip, addr, bdf);
772 
773 	bus_p = PCIE_DIP2BUS(dip);
774 
775 	/*
776 	 * Because CFG and IO Acc Handlers are on the same cache list and both
777 	 * types of hdls gets called for both types of errors.  For this checker
778 	 * only mark the device as "Non-Fatal" if the addr == NULL and bdf !=
779 	 * NULL.
780 	 */
781 	status = (!addr && (bus_p->bus_bdf == bdf)) ? DDI_FM_NONFATAL :
782 	    DDI_FM_FATAL;
783 
784 	return (status);
785 }
786 
787 /*
788  * Find and Mark all ACC Handles associated with a give address and BDF as
789  * failed.  If the BDF != NULL, then check to see if the device has a ACC Handle
790  * associated with ADDR.  If the handle is not found, mark all the handles as
791  * failed.  If the BDF == NULL, mark the handle as failed if it is associated
792  * with ADDR.
793  */
794 int
795 px_err_pio_hdl_check(dev_info_t *dip, const void *handle, const void *arg1,
796     const void *arg2)
797 {
798 	dev_info_t		*px_dip = PCIE_DIP2BUS(dip)->bus_rp_dip;
799 	px_t			*px_p = INST_TO_STATE(ddi_get_instance(px_dip));
800 	px_ranges_t		*ranges_p;
801 	int			range_len;
802 	ddi_acc_handle_t	ap = (ddi_acc_handle_t)handle;
803 	ddi_acc_hdl_t		*hp = impl_acc_hdl_get(ap);
804 	int			i, status = DDI_FM_FATAL;
805 	uint64_t		fault_addr = *(uint64_t *)arg1;
806 	uint16_t		bdf = *(uint16_t *)arg2;
807 	uint64_t		base_addr, range_addr;
808 	uint_t			size;
809 
810 	DBG(DBG_ERR_INTR, dip, "Check PIO Hdl: dip 0x%x addr 0x%x bdf=0x%x\n",
811 	    dip, fault_addr, bdf);
812 
813 	/* Normalize the base addr to the addr and strip off the HB info. */
814 	base_addr = (hp->ah_pfn << MMU_PAGESHIFT) + hp->ah_offset;
815 	range_len = px_p->px_ranges_length / sizeof (px_ranges_t);
816 	i = 0;
817 	for (ranges_p = px_p->px_ranges_p; i < range_len; i++, ranges_p++) {
818 		range_addr = px_in_addr_range(dip, ranges_p, base_addr);
819 		if (range_addr) {
820 			switch (ranges_p->child_high & PCI_ADDR_MASK) {
821 			case PCI_ADDR_IO:
822 			case PCI_ADDR_MEM64:
823 			case PCI_ADDR_MEM32:
824 				base_addr = base_addr - range_addr;
825 				break;
826 			}
827 			break;
828 		}
829 	}
830 
831 	/*
832 	 * Mark the handle as failed if the ADDR is mapped, or if we
833 	 * know the BDF and ADDR == 0.
834 	 */
835 	size = hp->ah_len;
836 	if (((fault_addr >= base_addr) && (fault_addr < (base_addr + size))) ||
837 	    ((fault_addr == NULL) && (bdf == PCIE_DIP2BUS(dip)->bus_bdf)))
838 		status = DDI_FM_NONFATAL;
839 
840 	return (status);
841 }
842 
843 /*
844  * Find and Mark all DNA Handles associated with a give address and BDF as
845  * failed.  If the BDF != NULL, then check to see if the device has a DMA Handle
846  * associated with ADDR.  If the handle is not found, mark all the handles as
847  * failed.  If the BDF == NULL, mark the handle as failed if it is associated
848  * with ADDR.
849  */
850 int
851 px_err_dma_hdl_check(dev_info_t *dip, const void *handle, const void *arg1,
852     const void *arg2)
853 {
854 	ddi_dma_impl_t		*pcie_dp;
855 	int			status = DDI_FM_FATAL;
856 	uint32_t		addr = *(uint32_t *)arg1;
857 	uint16_t		bdf = *(uint16_t *)arg2;
858 	uint32_t		base_addr;
859 	uint_t			size;
860 
861 	DBG(DBG_ERR_INTR, dip, "Check PIO Hdl: dip 0x%x addr 0x%x bdf=0x%x\n",
862 	    dip, addr, bdf);
863 
864 	pcie_dp = (ddi_dma_impl_t *)handle;
865 	base_addr = (uint32_t)pcie_dp->dmai_mapping;
866 	size = pcie_dp->dmai_size;
867 
868 	/*
869 	 * Mark the handle as failed if the ADDR is mapped, or if we
870 	 * know the BDF and ADDR == 0.
871 	 */
872 	if (((addr >= base_addr) && (addr < (base_addr + size))) ||
873 	    ((addr == NULL) && (bdf != NULL)))
874 		status = DDI_FM_NONFATAL;
875 
876 	return (status);
877 }
878 
879 int
880 px_fm_enter(px_t *px_p) {
881 	if (px_panicing || (px_p->px_fm_mutex_owner == curthread))
882 		return (DDI_FAILURE);
883 
884 	mutex_enter(&px_p->px_fm_mutex);
885 	/*
886 	 * In rare cases when trap occurs and in the middle of scanning the
887 	 * fabric, a PIO will fail in the scan fabric.  The CPU error handling
888 	 * code will correctly panic the system, while a mondo for the failed
889 	 * PIO may also show up.  Normally the mondo will try to grab the mutex
890 	 * and wait until the callback finishes.  But in this rare case,
891 	 * mutex_enter actually suceeds also continues to scan the fabric.
892 	 *
893 	 * This code below is designed specifically to check for this case.  If
894 	 * we successfully grab the px_fm_mutex, the px_fm_mutex_owner better be
895 	 * NULL.  If it isn't that means we are in the rare corner case.  Return
896 	 * DDI_FAILURE, this should prevent PX from doing anymore error
897 	 * handling.
898 	 */
899 	if (px_p->px_fm_mutex_owner) {
900 		return (DDI_FAILURE);
901 	}
902 
903 	px_p->px_fm_mutex_owner = curthread;
904 
905 	if (px_panicing) {
906 		px_fm_exit(px_p);
907 		return (DDI_FAILURE);
908 	}
909 	return (DDI_SUCCESS);
910 }
911 
912 void
913 px_fm_exit(px_t *px_p) {
914 	px_p->px_fm_mutex_owner = NULL;
915 	mutex_exit(&px_p->px_fm_mutex);
916 }
917 
918 /*
919  * Panic if the err tunable is set and that we are not already in the middle
920  * of panic'ing.
921  *
922  * rc_err = Error severity of PX specific errors
923  * msg = Where the error was detected
924  * fabric_err = Error severity of PCIe Fabric errors
925  * isTest = Test if error severity causes panic
926  */
927 #define	MSZ (sizeof (fm_msg) -strlen(fm_msg) - 1)
928 void
929 px_err_panic(int rc_err, int msg, int fabric_err, boolean_t isTest)
930 {
931 	char fm_msg[96] = "";
932 	int ferr = PX_NO_ERROR;
933 
934 	if (panicstr) {
935 		px_panicing = B_TRUE;
936 		return;
937 	}
938 
939 	if (!(rc_err & px_die))
940 		goto fabric;
941 	if (msg & PX_RC)
942 		(void) strncat(fm_msg, px_panic_rc_msg, MSZ);
943 	if (msg & PX_RP)
944 		(void) strncat(fm_msg, px_panic_rp_msg, MSZ);
945 	if (msg & PX_HB)
946 		(void) strncat(fm_msg, px_panic_hb_msg, MSZ);
947 
948 fabric:
949 	if (fabric_err & PF_ERR_FATAL_FLAGS)
950 		ferr = PX_PANIC;
951 	else if (fabric_err & ~(PF_ERR_FATAL_FLAGS | PF_ERR_NO_ERROR))
952 		ferr = PX_NO_PANIC;
953 
954 	if (ferr & px_die) {
955 		if (strlen(fm_msg)) {
956 			(void) strncat(fm_msg, " and", MSZ);
957 		}
958 		(void) strncat(fm_msg, px_panic_fab_msg, MSZ);
959 	}
960 
961 	if (strlen(fm_msg)) {
962 		px_panicing = B_TRUE;
963 		if (!isTest)
964 			fm_panic("Fatal error has occured in:%s.(0x%x)(0x%x)",
965 			    fm_msg, rc_err, fabric_err);
966 	}
967 }
968