xref: /dflybsd-src/usr.sbin/pciconf/err.c (revision d650e2183cf9557d58c726150ef990202fc85b98)
1*d650e218SMatthew Dillon /*-
2*d650e218SMatthew Dillon  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d650e218SMatthew Dillon  *
4*d650e218SMatthew Dillon  * Copyright (c) 2012 Hudson River Trading LLC
5*d650e218SMatthew Dillon  * Written by: John H. Baldwin <jhb@FreeBSD.org>
6*d650e218SMatthew Dillon  * All rights reserved.
7*d650e218SMatthew Dillon  *
8*d650e218SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
9*d650e218SMatthew Dillon  * modification, are permitted provided that the following conditions
10*d650e218SMatthew Dillon  * are met:
11*d650e218SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*d650e218SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*d650e218SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*d650e218SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
15*d650e218SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
16*d650e218SMatthew Dillon  *
17*d650e218SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*d650e218SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*d650e218SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*d650e218SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*d650e218SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*d650e218SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*d650e218SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*d650e218SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*d650e218SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*d650e218SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*d650e218SMatthew Dillon  * SUCH DAMAGE.
28*d650e218SMatthew Dillon  */
29*d650e218SMatthew Dillon 
30*d650e218SMatthew Dillon #include <sys/param.h>
31*d650e218SMatthew Dillon #include <sys/pciio.h>
32*d650e218SMatthew Dillon 
33*d650e218SMatthew Dillon #include <err.h>
34*d650e218SMatthew Dillon #include <stdio.h>
35*d650e218SMatthew Dillon 
36*d650e218SMatthew Dillon #include <bus/pci/pcireg.h>
37*d650e218SMatthew Dillon 
38*d650e218SMatthew Dillon #include "pciconf.h"
39*d650e218SMatthew Dillon 
40*d650e218SMatthew Dillon struct bit_table {
41*d650e218SMatthew Dillon 	uint32_t mask;
42*d650e218SMatthew Dillon 	const char *desc;
43*d650e218SMatthew Dillon };
44*d650e218SMatthew Dillon 
45*d650e218SMatthew Dillon /* Error indicators in the PCI status register (PCIR_STATUS). */
46*d650e218SMatthew Dillon static struct bit_table pci_status[] = {
47*d650e218SMatthew Dillon 	{ PCIM_STATUS_PERRREPORT, "Master Data Parity Error" },
48*d650e218SMatthew Dillon 	{ PCIM_STATUS_STABORT, "Sent Target-Abort" },
49*d650e218SMatthew Dillon 	{ PCIM_STATUS_RTABORT, "Received Target-Abort" },
50*d650e218SMatthew Dillon 	{ PCIM_STATUS_RMABORT, "Received Master-Abort" },
51*d650e218SMatthew Dillon 	{ PCIM_STATUS_SERR, "Signalled System Error" },
52*d650e218SMatthew Dillon 	{ PCIM_STATUS_PERR, "Detected Parity Error" },
53*d650e218SMatthew Dillon 	{ 0, NULL },
54*d650e218SMatthew Dillon };
55*d650e218SMatthew Dillon 
56*d650e218SMatthew Dillon /* Valid error indicator bits in PCIR_STATUS. */
57*d650e218SMatthew Dillon #define	PCI_ERRORS	(PCIM_STATUS_PERRREPORT | PCIM_STATUS_STABORT |	\
58*d650e218SMatthew Dillon 			 PCIM_STATUS_RTABORT | PCIM_STATUS_RMABORT |	\
59*d650e218SMatthew Dillon 			 PCIM_STATUS_SERR | PCIM_STATUS_PERR)
60*d650e218SMatthew Dillon 
61*d650e218SMatthew Dillon /* Error indicators in the PCI-Express device status register. */
62*d650e218SMatthew Dillon static struct bit_table pcie_device_status[] = {
63*d650e218SMatthew Dillon 	{ PCIEM_DEVSTS_CORR_ERR, "Correctable Error Detected" },
64*d650e218SMatthew Dillon 	{ PCIEM_DEVSTS_NFATAL_ERR, "Non-Fatal Error Detected" },
65*d650e218SMatthew Dillon 	{ PCIEM_DEVSTS_FATAL_ERR, "Fatal Error Detected" },
66*d650e218SMatthew Dillon 	{ PCIEM_DEVSTS_UNSUPP_REQ, "Unsupported Request Detected" },
67*d650e218SMatthew Dillon 	{ 0, NULL },
68*d650e218SMatthew Dillon };
69*d650e218SMatthew Dillon 
70*d650e218SMatthew Dillon /* Valid error indicator bits in the PCI-Express device status register. */
71*d650e218SMatthew Dillon #define	PCIE_ERRORS	(PCIEM_DEVSTS_CORR_ERR |		\
72*d650e218SMatthew Dillon 			 PCIEM_DEVSTS_NFATAL_ERR |			\
73*d650e218SMatthew Dillon 			 PCIEM_DEVSTS_FATAL_ERR |			\
74*d650e218SMatthew Dillon 			 PCIEM_DEVSTS_UNSUPP_REQ)
75*d650e218SMatthew Dillon 
76*d650e218SMatthew Dillon /* AER Uncorrected errors. */
77*d650e218SMatthew Dillon static struct bit_table aer_uc[] = {
78*d650e218SMatthew Dillon 	{ PCIM_AER_UC_TRAINING_ERROR, "Link Training Error" },
79*d650e218SMatthew Dillon 	{ PCIM_AER_UC_DL_PROTOCOL_ERROR, "Data Link Protocol Error" },
80*d650e218SMatthew Dillon 	{ PCIM_AER_UC_SURPRISE_LINK_DOWN, "Surprise Link Down Error" },
81*d650e218SMatthew Dillon 	{ PCIM_AER_UC_POISONED_TLP, "Poisoned TLP" },
82*d650e218SMatthew Dillon 	{ PCIM_AER_UC_FC_PROTOCOL_ERROR, "Flow Control Protocol Error" },
83*d650e218SMatthew Dillon 	{ PCIM_AER_UC_COMPLETION_TIMEOUT, "Completion Timeout" },
84*d650e218SMatthew Dillon 	{ PCIM_AER_UC_COMPLETER_ABORT, "Completer Abort" },
85*d650e218SMatthew Dillon 	{ PCIM_AER_UC_UNEXPECTED_COMPLETION, "Unexpected Completion" },
86*d650e218SMatthew Dillon 	{ PCIM_AER_UC_RECEIVER_OVERFLOW, "Receiver Overflow Error" },
87*d650e218SMatthew Dillon 	{ PCIM_AER_UC_MALFORMED_TLP, "Malformed TLP" },
88*d650e218SMatthew Dillon 	{ PCIM_AER_UC_ECRC_ERROR, "ECRC Error" },
89*d650e218SMatthew Dillon 	{ PCIM_AER_UC_UNSUPPORTED_REQUEST, "Unsupported Request" },
90*d650e218SMatthew Dillon 	{ PCIM_AER_UC_ACS_VIOLATION, "ACS Violation" },
91*d650e218SMatthew Dillon 	{ PCIM_AER_UC_INTERNAL_ERROR, "Uncorrectable Internal Error" },
92*d650e218SMatthew Dillon 	{ PCIM_AER_UC_MC_BLOCKED_TLP, "MC Blocked TLP" },
93*d650e218SMatthew Dillon 	{ PCIM_AER_UC_ATOMIC_EGRESS_BLK, "AtomicOp Egress Blocked" },
94*d650e218SMatthew Dillon 	{ PCIM_AER_UC_TLP_PREFIX_BLOCKED, "TLP Prefix Blocked Error" },
95*d650e218SMatthew Dillon 	{ 0, NULL },
96*d650e218SMatthew Dillon };
97*d650e218SMatthew Dillon 
98*d650e218SMatthew Dillon /* AER Corrected errors. */
99*d650e218SMatthew Dillon static struct bit_table aer_cor[] = {
100*d650e218SMatthew Dillon 	{ PCIM_AER_COR_RECEIVER_ERROR, "Receiver Error" },
101*d650e218SMatthew Dillon 	{ PCIM_AER_COR_BAD_TLP, "Bad TLP" },
102*d650e218SMatthew Dillon 	{ PCIM_AER_COR_BAD_DLLP, "Bad DLLP" },
103*d650e218SMatthew Dillon 	{ PCIM_AER_COR_REPLAY_ROLLOVER, "REPLAY_NUM Rollover" },
104*d650e218SMatthew Dillon 	{ PCIM_AER_COR_REPLAY_TIMEOUT, "Replay Timer Timeout" },
105*d650e218SMatthew Dillon 	{ PCIM_AER_COR_ADVISORY_NF_ERROR, "Advisory Non-Fatal Error" },
106*d650e218SMatthew Dillon 	{ PCIM_AER_COR_INTERNAL_ERROR, "Corrected Internal Error" },
107*d650e218SMatthew Dillon 	{ PCIM_AER_COR_HEADER_LOG_OVFLOW, "Header Log Overflow" },
108*d650e218SMatthew Dillon 	{ 0, NULL },
109*d650e218SMatthew Dillon };
110*d650e218SMatthew Dillon 
111*d650e218SMatthew Dillon static void
print_bits(const char * header,struct bit_table * table,uint32_t mask)112*d650e218SMatthew Dillon print_bits(const char *header, struct bit_table *table, uint32_t mask)
113*d650e218SMatthew Dillon {
114*d650e218SMatthew Dillon 	int first;
115*d650e218SMatthew Dillon 
116*d650e218SMatthew Dillon 	first = 1;
117*d650e218SMatthew Dillon 	for (; table->desc != NULL; table++)
118*d650e218SMatthew Dillon 		if (mask & table->mask) {
119*d650e218SMatthew Dillon 			if (first) {
120*d650e218SMatthew Dillon 				printf("%14s = ", header);
121*d650e218SMatthew Dillon 				first = 0;
122*d650e218SMatthew Dillon 			} else
123*d650e218SMatthew Dillon 				printf("                 ");
124*d650e218SMatthew Dillon 			printf("%s\n", table->desc);
125*d650e218SMatthew Dillon 			mask &= ~table->mask;
126*d650e218SMatthew Dillon 		}
127*d650e218SMatthew Dillon 	if (mask != 0) {
128*d650e218SMatthew Dillon 		if (first)
129*d650e218SMatthew Dillon 			printf("%14s = ", header);
130*d650e218SMatthew Dillon 		else
131*d650e218SMatthew Dillon 			printf("                 ");
132*d650e218SMatthew Dillon 		printf("Unknown: 0x%08x\n", mask);
133*d650e218SMatthew Dillon 	}
134*d650e218SMatthew Dillon }
135*d650e218SMatthew Dillon 
136*d650e218SMatthew Dillon void
list_errors(int fd,struct pci_conf * p)137*d650e218SMatthew Dillon list_errors(int fd, struct pci_conf *p)
138*d650e218SMatthew Dillon {
139*d650e218SMatthew Dillon 	uint32_t mask, severity;
140*d650e218SMatthew Dillon 	uint16_t sta, aer;
141*d650e218SMatthew Dillon 	uint8_t pcie;
142*d650e218SMatthew Dillon 
143*d650e218SMatthew Dillon 	/* First check for standard PCI errors. */
144*d650e218SMatthew Dillon 	sta = read_config(fd, &p->pc_sel, PCIR_STATUS, 2);
145*d650e218SMatthew Dillon 	print_bits("PCI errors", pci_status, sta & PCI_ERRORS);
146*d650e218SMatthew Dillon 
147*d650e218SMatthew Dillon 	/* See if this is a PCI-express device. */
148*d650e218SMatthew Dillon 	pcie = pci_find_cap(fd, p, PCIY_EXPRESS);
149*d650e218SMatthew Dillon 	if (pcie == 0)
150*d650e218SMatthew Dillon 		return;
151*d650e218SMatthew Dillon 
152*d650e218SMatthew Dillon 	/* Check for PCI-e errors. */
153*d650e218SMatthew Dillon 	sta = read_config(fd, &p->pc_sel, pcie + PCIER_DEVSTS, 2);
154*d650e218SMatthew Dillon 	print_bits("PCI-e errors", pcie_device_status, sta & PCIE_ERRORS);
155*d650e218SMatthew Dillon 
156*d650e218SMatthew Dillon 	/* See if this device supports AER. */
157*d650e218SMatthew Dillon 	aer = pcie_find_cap(fd, p, PCIZ_AER);
158*d650e218SMatthew Dillon 	if (aer == 0)
159*d650e218SMatthew Dillon 		return;
160*d650e218SMatthew Dillon 
161*d650e218SMatthew Dillon 	/* Check for uncorrected errors. */
162*d650e218SMatthew Dillon 	mask = read_config(fd, &p->pc_sel, aer + PCIR_AER_UC_STATUS, 4);
163*d650e218SMatthew Dillon         severity = read_config(fd, &p->pc_sel, aer + PCIR_AER_UC_SEVERITY, 4);
164*d650e218SMatthew Dillon 	print_bits("Fatal", aer_uc, mask & severity);
165*d650e218SMatthew Dillon 	print_bits("Non-fatal", aer_uc, mask & ~severity);
166*d650e218SMatthew Dillon 
167*d650e218SMatthew Dillon 	/* Check for corrected errors. */
168*d650e218SMatthew Dillon 	mask = read_config(fd, &p->pc_sel, aer + PCIR_AER_COR_STATUS, 4);
169*d650e218SMatthew Dillon 	print_bits("Corrected", aer_cor, mask);
170*d650e218SMatthew Dillon }
171