1236415Sjhb/*-
2236415Sjhb * Copyright (c) 2012 Advanced Computing Technologies LLC
3236415Sjhb * Written by: John H. Baldwin <jhb@FreeBSD.org>
4236415Sjhb * All rights reserved.
5236415Sjhb *
6236415Sjhb * Redistribution and use in source and binary forms, with or without
7236415Sjhb * modification, are permitted provided that the following conditions
8236415Sjhb * are met:
9236415Sjhb * 1. Redistributions of source code must retain the above copyright
10236415Sjhb *    notice, this list of conditions and the following disclaimer.
11236415Sjhb * 2. Redistributions in binary form must reproduce the above copyright
12236415Sjhb *    notice, this list of conditions and the following disclaimer in the
13236415Sjhb *    documentation and/or other materials provided with the distribution.
14236415Sjhb *
15236415Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16236415Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17236415Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18236415Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19236415Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20236415Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21236415Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22236415Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23236415Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24236415Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25236415Sjhb * SUCH DAMAGE.
26236415Sjhb */
27236415Sjhb
28236415Sjhb#ifndef lint
29236415Sjhbstatic const char rcsid[] =
30236415Sjhb    "$FreeBSD$";
31236415Sjhb#endif /* not lint */
32236415Sjhb
33236415Sjhb#include <sys/param.h>
34236415Sjhb#include <sys/pciio.h>
35236415Sjhb
36236415Sjhb#include <err.h>
37236415Sjhb#include <stdio.h>
38236415Sjhb
39236415Sjhb#include <dev/pci/pcireg.h>
40236415Sjhb
41236415Sjhb#include "pciconf.h"
42236415Sjhb
43236415Sjhbstruct bit_table {
44236415Sjhb	uint32_t mask;
45236415Sjhb	const char *desc;
46236415Sjhb};
47236415Sjhb
48236415Sjhb/* Error indicators in the PCI status register (PCIR_STATUS). */
49236415Sjhbstatic struct bit_table pci_status[] = {
50236415Sjhb	{ PCIM_STATUS_MDPERR, "Master Data Parity Error" },
51236415Sjhb	{ PCIM_STATUS_STABORT, "Sent Target-Abort" },
52236415Sjhb	{ PCIM_STATUS_RTABORT, "Received Target-Abort" },
53236415Sjhb	{ PCIM_STATUS_RMABORT, "Received Master-Abort" },
54236415Sjhb	{ PCIM_STATUS_SERR, "Signalled System Error" },
55236415Sjhb	{ PCIM_STATUS_PERR, "Detected Parity Error" },
56236415Sjhb	{ 0, NULL },
57236415Sjhb};
58236415Sjhb
59236415Sjhb/* Valid error indicator bits in PCIR_STATUS. */
60236415Sjhb#define	PCI_ERRORS	(PCIM_STATUS_MDPERR | PCIM_STATUS_STABORT |	\
61236415Sjhb			 PCIM_STATUS_RTABORT | PCIM_STATUS_RMABORT |	\
62236415Sjhb			 PCIM_STATUS_SERR | PCIM_STATUS_PERR)
63236415Sjhb
64236415Sjhb/* Error indicators in the PCI-Express device status register. */
65236415Sjhbstatic struct bit_table pcie_device_status[] = {
66240680Sgavin	{ PCIEM_STA_CORRECTABLE_ERROR, "Correctable Error Detected" },
67240680Sgavin	{ PCIEM_STA_NON_FATAL_ERROR, "Non-Fatal Error Detected" },
68240680Sgavin	{ PCIEM_STA_FATAL_ERROR, "Fatal Error Detected" },
69240680Sgavin	{ PCIEM_STA_UNSUPPORTED_REQ, "Unsupported Request Detected" },
70236415Sjhb	{ 0, NULL },
71236415Sjhb};
72236415Sjhb
73236415Sjhb/* Valid error indicator bits in the PCI-Express device status register. */
74240680Sgavin#define	PCIE_ERRORS	(PCIEM_STA_CORRECTABLE_ERROR |		\
75240680Sgavin			 PCIEM_STA_NON_FATAL_ERROR |			\
76240680Sgavin			 PCIEM_STA_FATAL_ERROR |			\
77240680Sgavin			 PCIEM_STA_UNSUPPORTED_REQ)
78236415Sjhb
79236415Sjhb/* AER Uncorrected errors. */
80236415Sjhbstatic struct bit_table aer_uc[] = {
81236415Sjhb	{ PCIM_AER_UC_TRAINING_ERROR, "Link Training Error" },
82236415Sjhb	{ PCIM_AER_UC_DL_PROTOCOL_ERROR, "Data Link Protocol Error" },
83236415Sjhb	{ PCIM_AER_UC_SURPRISE_LINK_DOWN, "Surprise Link Down Error" },
84236415Sjhb	{ PCIM_AER_UC_POISONED_TLP, "Poisoned TLP" },
85236415Sjhb	{ PCIM_AER_UC_FC_PROTOCOL_ERROR, "Flow Control Protocol Error" },
86236415Sjhb	{ PCIM_AER_UC_COMPLETION_TIMEOUT, "Completion Timeout" },
87236415Sjhb	{ PCIM_AER_UC_COMPLETER_ABORT, "Completer Abort" },
88236415Sjhb	{ PCIM_AER_UC_UNEXPECTED_COMPLETION, "Unexpected Completion" },
89236415Sjhb	{ PCIM_AER_UC_RECEIVER_OVERFLOW, "Receiver Overflow Error" },
90236415Sjhb	{ PCIM_AER_UC_MALFORMED_TLP, "Malformed TLP" },
91236415Sjhb	{ PCIM_AER_UC_ECRC_ERROR, "ECRC Error" },
92236415Sjhb	{ PCIM_AER_UC_UNSUPPORTED_REQUEST, "Unsupported Request" },
93236415Sjhb	{ PCIM_AER_UC_ACS_VIOLATION, "ACS Violation" },
94240474Sjhb	{ PCIM_AER_UC_INTERNAL_ERROR, "Uncorrectable Internal Error" },
95240474Sjhb	{ PCIM_AER_UC_MC_BLOCKED_TLP, "MC Blocked TLP" },
96240474Sjhb	{ PCIM_AER_UC_ATOMIC_EGRESS_BLK, "AtomicOp Egress Blocked" },
97240474Sjhb	{ PCIM_AER_UC_TLP_PREFIX_BLOCKED, "TLP Prefix Blocked Error" },
98236415Sjhb	{ 0, NULL },
99236415Sjhb};
100236415Sjhb
101236415Sjhb/* AER Corrected errors. */
102236415Sjhbstatic struct bit_table aer_cor[] = {
103236415Sjhb	{ PCIM_AER_COR_RECEIVER_ERROR, "Receiver Error" },
104236415Sjhb	{ PCIM_AER_COR_BAD_TLP, "Bad TLP" },
105236415Sjhb	{ PCIM_AER_COR_BAD_DLLP, "Bad DLLP" },
106236415Sjhb	{ PCIM_AER_COR_REPLAY_ROLLOVER, "REPLAY_NUM Rollover" },
107236415Sjhb	{ PCIM_AER_COR_REPLAY_TIMEOUT, "Replay Timer Timeout" },
108236415Sjhb	{ PCIM_AER_COR_ADVISORY_NF_ERROR, "Advisory Non-Fatal Error" },
109240474Sjhb	{ PCIM_AER_COR_INTERNAL_ERROR, "Corrected Internal Error" },
110240474Sjhb	{ PCIM_AER_COR_HEADER_LOG_OVFLOW, "Header Log Overflow" },
111236415Sjhb	{ 0, NULL },
112236415Sjhb};
113236415Sjhb
114236415Sjhbstatic void
115236415Sjhbprint_bits(const char *header, struct bit_table *table, uint32_t mask)
116236415Sjhb{
117236415Sjhb	int first;
118236415Sjhb
119236415Sjhb	first = 1;
120236415Sjhb	for (; table->desc != NULL; table++)
121236415Sjhb		if (mask & table->mask) {
122236415Sjhb			if (first) {
123236415Sjhb				printf("%14s = ", header);
124236415Sjhb				first = 0;
125236415Sjhb			} else
126236415Sjhb				printf("                 ");
127236415Sjhb			printf("%s\n", table->desc);
128236415Sjhb			mask &= ~table->mask;
129236415Sjhb		}
130236415Sjhb	if (mask != 0) {
131236415Sjhb		if (first)
132236415Sjhb			printf("%14s = ", header);
133236415Sjhb		else
134236415Sjhb			printf("                 ");
135236415Sjhb		printf("Unknown: 0x%08x\n", mask);
136236415Sjhb	}
137236415Sjhb}
138236415Sjhb
139236415Sjhbvoid
140236415Sjhblist_errors(int fd, struct pci_conf *p)
141236415Sjhb{
142236415Sjhb	uint32_t mask, severity;
143236415Sjhb	uint16_t sta, aer;
144236415Sjhb	uint8_t pcie;
145236415Sjhb
146236415Sjhb	/* First check for standard PCI errors. */
147236415Sjhb	sta = read_config(fd, &p->pc_sel, PCIR_STATUS, 2);
148236415Sjhb	print_bits("PCI errors", pci_status, sta & PCI_ERRORS);
149236415Sjhb
150236415Sjhb	/* See if this is a PCI-express device. */
151236415Sjhb	pcie = pci_find_cap(fd, p, PCIY_EXPRESS);
152236415Sjhb	if (pcie == 0)
153236415Sjhb		return;
154236415Sjhb
155236415Sjhb	/* Check for PCI-e errors. */
156240680Sgavin	sta = read_config(fd, &p->pc_sel, pcie + PCIER_DEVICE_STA, 2);
157236415Sjhb	print_bits("PCI-e errors", pcie_device_status, sta & PCIE_ERRORS);
158236415Sjhb
159236415Sjhb	/* See if this device supports AER. */
160236415Sjhb	aer = pcie_find_cap(fd, p, PCIZ_AER);
161236415Sjhb	if (aer == 0)
162236415Sjhb		return;
163236415Sjhb
164236415Sjhb	/* Check for uncorrected errors. */
165236415Sjhb	mask = read_config(fd, &p->pc_sel, aer + PCIR_AER_UC_STATUS, 4);
166236415Sjhb        severity = read_config(fd, &p->pc_sel, aer + PCIR_AER_UC_SEVERITY, 4);
167236415Sjhb	print_bits("Fatal", aer_uc, mask & severity);
168236415Sjhb	print_bits("Non-fatal", aer_uc, mask & ~severity);
169236415Sjhb
170236415Sjhb	/* Check for corrected errors. */
171236415Sjhb	mask = read_config(fd, &p->pc_sel, aer + PCIR_AER_COR_STATUS, 4);
172236415Sjhb	print_bits("Corrected", aer_cor, mask);
173236415Sjhb}
174