1/*
2 * drivers/pci/pcie/aer/aerdrv_errprint.c
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License.  See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Format error messages and print them to console.
9 *
10 * Copyright (C) 2006 Intel Corp.
11 *	Tom Long Nguyen (tom.l.nguyen@intel.com)
12 *	Zhang Yanmin (yanmin.zhang@intel.com)
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/pci.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/pm.h>
21#include <linux/suspend.h>
22#include <linux/cper.h>
23
24#include "aerdrv.h"
25#include <ras/ras_event.h>
26
27#define AER_AGENT_RECEIVER		0
28#define AER_AGENT_REQUESTER		1
29#define AER_AGENT_COMPLETER		2
30#define AER_AGENT_TRANSMITTER		3
31
32#define AER_AGENT_REQUESTER_MASK(t)	((t == AER_CORRECTABLE) ?	\
33	0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
34#define AER_AGENT_COMPLETER_MASK(t)	((t == AER_CORRECTABLE) ?	\
35	0 : PCI_ERR_UNC_COMP_ABORT)
36#define AER_AGENT_TRANSMITTER_MASK(t)	((t == AER_CORRECTABLE) ?	\
37	(PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
38
39#define AER_GET_AGENT(t, e)						\
40	((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER :	\
41	(e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER :	\
42	(e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER :	\
43	AER_AGENT_RECEIVER)
44
45#define AER_PHYSICAL_LAYER_ERROR	0
46#define AER_DATA_LINK_LAYER_ERROR	1
47#define AER_TRANSACTION_LAYER_ERROR	2
48
49#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ?	\
50	PCI_ERR_COR_RCVR : 0)
51#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ?	\
52	(PCI_ERR_COR_BAD_TLP|						\
53	PCI_ERR_COR_BAD_DLLP|						\
54	PCI_ERR_COR_REP_ROLL|						\
55	PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
56
57#define AER_GET_LAYER_ERROR(t, e)					\
58	((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
59	(e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
60	AER_TRANSACTION_LAYER_ERROR)
61
62/*
63 * AER error strings
64 */
65static const char *aer_error_severity_string[] = {
66	"Uncorrected (Non-Fatal)",
67	"Uncorrected (Fatal)",
68	"Corrected"
69};
70
71static const char *aer_error_layer[] = {
72	"Physical Layer",
73	"Data Link Layer",
74	"Transaction Layer"
75};
76
77static const char *aer_correctable_error_string[] = {
78	"Receiver Error",		/* Bit Position 0	*/
79	NULL,
80	NULL,
81	NULL,
82	NULL,
83	NULL,
84	"Bad TLP",			/* Bit Position 6	*/
85	"Bad DLLP",			/* Bit Position 7	*/
86	"RELAY_NUM Rollover",		/* Bit Position 8	*/
87	NULL,
88	NULL,
89	NULL,
90	"Replay Timer Timeout",		/* Bit Position 12	*/
91	"Advisory Non-Fatal",		/* Bit Position 13	*/
92	"Corrected Internal Error",	/* Bit Position 14	*/
93	"Header Log Overflow",		/* Bit Position 15	*/
94};
95
96static const char *aer_uncorrectable_error_string[] = {
97	"Undefined",			/* Bit Position 0	*/
98	NULL,
99	NULL,
100	NULL,
101	"Data Link Protocol",		/* Bit Position 4	*/
102	"Surprise Down Error",		/* Bit Position 5	*/
103	NULL,
104	NULL,
105	NULL,
106	NULL,
107	NULL,
108	NULL,
109	"Poisoned TLP",			/* Bit Position 12	*/
110	"Flow Control Protocol",	/* Bit Position 13	*/
111	"Completion Timeout",		/* Bit Position 14	*/
112	"Completer Abort",		/* Bit Position 15	*/
113	"Unexpected Completion",	/* Bit Position 16	*/
114	"Receiver Overflow",		/* Bit Position 17	*/
115	"Malformed TLP",		/* Bit Position 18	*/
116	"ECRC",				/* Bit Position 19	*/
117	"Unsupported Request",		/* Bit Position 20	*/
118	"ACS Violation",		/* Bit Position 21	*/
119	"Uncorrectable Internal Error",	/* Bit Position 22	*/
120	"MC Blocked TLP",		/* Bit Position 23	*/
121	"AtomicOp Egress Blocked",	/* Bit Position 24	*/
122	"TLP Prefix Blocked Error",	/* Bit Position 25	*/
123};
124
125static const char *aer_agent_string[] = {
126	"Receiver ID",
127	"Requester ID",
128	"Completer ID",
129	"Transmitter ID"
130};
131
132static void __print_tlp_header(struct pci_dev *dev,
133			       struct aer_header_log_regs *t)
134{
135	dev_err(&dev->dev, "  TLP Header: %08x %08x %08x %08x\n",
136		t->dw0, t->dw1, t->dw2, t->dw3);
137}
138
139static void __aer_print_error(struct pci_dev *dev,
140			      struct aer_err_info *info)
141{
142	int i, status;
143	const char *errmsg = NULL;
144	status = (info->status & ~info->mask);
145
146	for (i = 0; i < 32; i++) {
147		if (!(status & (1 << i)))
148			continue;
149
150		if (info->severity == AER_CORRECTABLE)
151			errmsg = i < ARRAY_SIZE(aer_correctable_error_string) ?
152				aer_correctable_error_string[i] : NULL;
153		else
154			errmsg = i < ARRAY_SIZE(aer_uncorrectable_error_string) ?
155				aer_uncorrectable_error_string[i] : NULL;
156
157		if (errmsg)
158			dev_err(&dev->dev, "   [%2d] %-22s%s\n", i, errmsg,
159				info->first_error == i ? " (First)" : "");
160		else
161			dev_err(&dev->dev, "   [%2d] Unknown Error Bit%s\n",
162				i, info->first_error == i ? " (First)" : "");
163	}
164}
165
166void aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
167{
168	int layer, agent;
169	int id = ((dev->bus->number << 8) | dev->devfn);
170
171	if (!info->status) {
172		dev_err(&dev->dev, "PCIe Bus Error: severity=%s, type=Unaccessible, id=%04x(Unregistered Agent ID)\n",
173			aer_error_severity_string[info->severity], id);
174		goto out;
175	}
176
177	layer = AER_GET_LAYER_ERROR(info->severity, info->status);
178	agent = AER_GET_AGENT(info->severity, info->status);
179
180	dev_err(&dev->dev, "PCIe Bus Error: severity=%s, type=%s, id=%04x(%s)\n",
181		aer_error_severity_string[info->severity],
182		aer_error_layer[layer], id, aer_agent_string[agent]);
183
184	dev_err(&dev->dev, "  device [%04x:%04x] error status/mask=%08x/%08x\n",
185		dev->vendor, dev->device,
186		info->status, info->mask);
187
188	__aer_print_error(dev, info);
189
190	if (info->tlp_header_valid)
191		__print_tlp_header(dev, &info->tlp);
192
193out:
194	if (info->id && info->error_dev_num > 1 && info->id == id)
195		dev_err(&dev->dev, "  Error of this Agent(%04x) is reported first\n", id);
196
197	trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
198			info->severity);
199}
200
201void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
202{
203	dev_info(&dev->dev, "AER: %s%s error received: id=%04x\n",
204		info->multi_error_valid ? "Multiple " : "",
205		aer_error_severity_string[info->severity], info->id);
206}
207
208#ifdef CONFIG_ACPI_APEI_PCIEAER
209int cper_severity_to_aer(int cper_severity)
210{
211	switch (cper_severity) {
212	case CPER_SEV_RECOVERABLE:
213		return AER_NONFATAL;
214	case CPER_SEV_FATAL:
215		return AER_FATAL;
216	default:
217		return AER_CORRECTABLE;
218	}
219}
220EXPORT_SYMBOL_GPL(cper_severity_to_aer);
221
222void cper_print_aer(struct pci_dev *dev, int cper_severity,
223		    struct aer_capability_regs *aer)
224{
225	int aer_severity, layer, agent, status_strs_size, tlp_header_valid = 0;
226	u32 status, mask;
227	const char **status_strs;
228
229	aer_severity = cper_severity_to_aer(cper_severity);
230
231	if (aer_severity == AER_CORRECTABLE) {
232		status = aer->cor_status;
233		mask = aer->cor_mask;
234		status_strs = aer_correctable_error_string;
235		status_strs_size = ARRAY_SIZE(aer_correctable_error_string);
236	} else {
237		status = aer->uncor_status;
238		mask = aer->uncor_mask;
239		status_strs = aer_uncorrectable_error_string;
240		status_strs_size = ARRAY_SIZE(aer_uncorrectable_error_string);
241		tlp_header_valid = status & AER_LOG_TLP_MASKS;
242	}
243
244	layer = AER_GET_LAYER_ERROR(aer_severity, status);
245	agent = AER_GET_AGENT(aer_severity, status);
246
247	dev_err(&dev->dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
248	cper_print_bits("", status, status_strs, status_strs_size);
249	dev_err(&dev->dev, "aer_layer=%s, aer_agent=%s\n",
250		aer_error_layer[layer], aer_agent_string[agent]);
251
252	if (aer_severity != AER_CORRECTABLE)
253		dev_err(&dev->dev, "aer_uncor_severity: 0x%08x\n",
254			aer->uncor_severity);
255
256	if (tlp_header_valid)
257		__print_tlp_header(dev, &aer->header_log);
258
259	trace_aer_event(dev_name(&dev->dev), (status & ~mask),
260			aer_severity);
261}
262#endif
263