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