blob: 47c67de1ccf15dd5eb8adb09e3b367dc2237872b [file] [log] [blame]
Bjorn Helgaase1e86ee2018-01-26 14:12:23 -06001// SPDX-License-Identifier: GPL-2.0
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * Implement the AER root port service driver. The driver registers an IRQ
4 * handler. When a root port triggers an AER interrupt, the IRQ handler
5 * collects root port status and schedules work.
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08006 *
7 * Copyright (C) 2006 Intel Corp.
8 * Tom Long Nguyen (tom.l.nguyen@intel.com)
9 * Zhang Yanmin (yanmin.zhang@intel.com)
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -050010 *
11 * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
12 * Andrew Patterson <andrew.patterson@hp.com>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080013 */
14
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -050015#include <linux/cper.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080016#include <linux/pci.h>
Rafael J. Wysocki415e12b2011-01-07 00:55:09 +010017#include <linux/pci-acpi.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040018#include <linux/sched.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080019#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/pm.h>
22#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/delay.h>
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -050025#include <linux/kfifo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Bjorn Helgaas256a4592018-06-08 08:39:45 -050027#include <acpi/apei.h>
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -050028#include <ras/ras_event.h>
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080029
Bjorn Helgaas4696b822018-06-08 08:48:47 -050030#include "../pci.h"
31#include "portdrv.h"
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050032
33#define AER_ERROR_SOURCES_MAX 100
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050034
Rajat Jaindb89ccb2018-06-30 15:07:17 -050035#define AER_MAX_TYPEOF_COR_ERRS 16 /* as per PCI_ERR_COR_STATUS */
36#define AER_MAX_TYPEOF_UNCOR_ERRS 26 /* as per PCI_ERR_UNCOR_STATUS*/
37
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050038struct aer_err_source {
39 unsigned int status;
40 unsigned int id;
41};
42
43struct aer_rpc {
44 struct pci_dev *rpd; /* Root Port device */
45 struct work_struct dpc_handler;
46 struct aer_err_source e_sources[AER_ERROR_SOURCES_MAX];
47 struct aer_err_info e_info;
48 unsigned short prod_idx; /* Error Producer Index */
49 unsigned short cons_idx; /* Error Consumer Index */
50 int isr;
51 spinlock_t e_lock; /*
52 * Lock access to Error Status/ID Regs
53 * and error producer/consumer index
54 */
55 struct mutex rpc_mutex; /*
56 * only one thread could do
57 * recovery on the same
58 * root port hierarchy
59 */
60};
61
Rajat Jaindb89ccb2018-06-30 15:07:17 -050062/* AER stats for the device */
63struct aer_stats {
64
65 /*
66 * Fields for all AER capable devices. They indicate the errors
67 * "as seen by this device". Note that this may mean that if an
68 * end point is causing problems, the AER counters may increment
69 * at its link partner (e.g. root port) because the errors will be
70 * "seen" by the link partner and not the the problematic end point
71 * itself (which may report all counters as 0 as it never saw any
72 * problems).
73 */
74 /* Counters for different type of correctable errors */
75 u64 dev_cor_errs[AER_MAX_TYPEOF_COR_ERRS];
76 /* Counters for different type of fatal uncorrectable errors */
77 u64 dev_fatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
78 /* Counters for different type of nonfatal uncorrectable errors */
79 u64 dev_nonfatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
80 /* Total number of ERR_COR sent by this device */
81 u64 dev_total_cor_errs;
82 /* Total number of ERR_FATAL sent by this device */
83 u64 dev_total_fatal_errs;
84 /* Total number of ERR_NONFATAL sent by this device */
85 u64 dev_total_nonfatal_errs;
86
87 /*
88 * Fields for Root ports & root complex event collectors only, these
89 * indicate the total number of ERR_COR, ERR_FATAL, and ERR_NONFATAL
90 * messages received by the root port / event collector, INCLUDING the
91 * ones that are generated internally (by the rootport itself)
92 */
93 u64 rootport_total_cor_errs;
94 u64 rootport_total_fatal_errs;
95 u64 rootport_total_nonfatal_errs;
96};
97
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050098#define AER_LOG_TLP_MASKS (PCI_ERR_UNC_POISON_TLP| \
99 PCI_ERR_UNC_ECRC| \
100 PCI_ERR_UNC_UNSUP| \
101 PCI_ERR_UNC_COMP_ABORT| \
102 PCI_ERR_UNC_UNX_COMP| \
103 PCI_ERR_UNC_MALF_TLP)
104
105#define SYSTEM_ERROR_INTR_ON_MESG_MASK (PCI_EXP_RTCTL_SECEE| \
106 PCI_EXP_RTCTL_SENFEE| \
107 PCI_EXP_RTCTL_SEFEE)
108#define ROOT_PORT_INTR_ON_MESG_MASK (PCI_ERR_ROOT_CMD_COR_EN| \
109 PCI_ERR_ROOT_CMD_NONFATAL_EN| \
110 PCI_ERR_ROOT_CMD_FATAL_EN)
111#define ERR_COR_ID(d) (d & 0xffff)
112#define ERR_UNCOR_ID(d) (d >> 16)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800113
Randy Dunlap7f785762007-10-05 13:17:58 -0700114static int pcie_aer_disable;
115
116void pci_no_aer(void)
117{
Bjorn Helgaas7ece1412016-09-06 16:24:37 -0500118 pcie_aer_disable = 1;
Randy Dunlap7f785762007-10-05 13:17:58 -0700119}
120
Rafael J. Wysockif1a7bfa2010-08-21 01:50:52 +0200121bool pci_aer_available(void)
122{
123 return !pcie_aer_disable && pci_msi_enabled();
124}
125
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -0500126#ifdef CONFIG_PCIE_ECRC
127
128#define ECRC_POLICY_DEFAULT 0 /* ECRC set by BIOS */
129#define ECRC_POLICY_OFF 1 /* ECRC off for performance */
130#define ECRC_POLICY_ON 2 /* ECRC on for data integrity */
131
132static int ecrc_policy = ECRC_POLICY_DEFAULT;
133
134static const char *ecrc_policy_str[] = {
135 [ECRC_POLICY_DEFAULT] = "bios",
136 [ECRC_POLICY_OFF] = "off",
137 [ECRC_POLICY_ON] = "on"
138};
139
140/**
141 * enable_ercr_checking - enable PCIe ECRC checking for a device
142 * @dev: the PCI device
143 *
144 * Returns 0 on success, or negative on failure.
145 */
146static int enable_ecrc_checking(struct pci_dev *dev)
147{
148 int pos;
149 u32 reg32;
150
151 if (!pci_is_pcie(dev))
152 return -ENODEV;
153
154 pos = dev->aer_cap;
155 if (!pos)
156 return -ENODEV;
157
158 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
159 if (reg32 & PCI_ERR_CAP_ECRC_GENC)
160 reg32 |= PCI_ERR_CAP_ECRC_GENE;
161 if (reg32 & PCI_ERR_CAP_ECRC_CHKC)
162 reg32 |= PCI_ERR_CAP_ECRC_CHKE;
163 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
164
165 return 0;
166}
167
168/**
169 * disable_ercr_checking - disables PCIe ECRC checking for a device
170 * @dev: the PCI device
171 *
172 * Returns 0 on success, or negative on failure.
173 */
174static int disable_ecrc_checking(struct pci_dev *dev)
175{
176 int pos;
177 u32 reg32;
178
179 if (!pci_is_pcie(dev))
180 return -ENODEV;
181
182 pos = dev->aer_cap;
183 if (!pos)
184 return -ENODEV;
185
186 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
187 reg32 &= ~(PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
188 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
189
190 return 0;
191}
192
193/**
194 * pcie_set_ecrc_checking - set/unset PCIe ECRC checking for a device based on global policy
195 * @dev: the PCI device
196 */
197void pcie_set_ecrc_checking(struct pci_dev *dev)
198{
199 switch (ecrc_policy) {
200 case ECRC_POLICY_DEFAULT:
201 return;
202 case ECRC_POLICY_OFF:
203 disable_ecrc_checking(dev);
204 break;
205 case ECRC_POLICY_ON:
206 enable_ecrc_checking(dev);
207 break;
208 default:
209 return;
210 }
211}
212
213/**
214 * pcie_ecrc_get_policy - parse kernel command-line ecrc option
215 */
216void pcie_ecrc_get_policy(char *str)
217{
218 int i;
219
220 for (i = 0; i < ARRAY_SIZE(ecrc_policy_str); i++)
221 if (!strncmp(str, ecrc_policy_str[i],
222 strlen(ecrc_policy_str[i])))
223 break;
224 if (i >= ARRAY_SIZE(ecrc_policy_str))
225 return;
226
227 ecrc_policy = i;
228}
229#endif /* CONFIG_PCIE_ECRC */
230
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500231#ifdef CONFIG_ACPI_APEI
232static inline int hest_match_pci(struct acpi_hest_aer_common *p,
233 struct pci_dev *pci)
234{
235 return ACPI_HEST_SEGMENT(p->bus) == pci_domain_nr(pci->bus) &&
236 ACPI_HEST_BUS(p->bus) == pci->bus->number &&
237 p->device == PCI_SLOT(pci->devfn) &&
238 p->function == PCI_FUNC(pci->devfn);
239}
240
241static inline bool hest_match_type(struct acpi_hest_header *hest_hdr,
242 struct pci_dev *dev)
243{
244 u16 hest_type = hest_hdr->type;
245 u8 pcie_type = pci_pcie_type(dev);
246
247 if ((hest_type == ACPI_HEST_TYPE_AER_ROOT_PORT &&
248 pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
249 (hest_type == ACPI_HEST_TYPE_AER_ENDPOINT &&
250 pcie_type == PCI_EXP_TYPE_ENDPOINT) ||
251 (hest_type == ACPI_HEST_TYPE_AER_BRIDGE &&
252 (dev->class >> 16) == PCI_BASE_CLASS_BRIDGE))
253 return true;
254 return false;
255}
256
257struct aer_hest_parse_info {
258 struct pci_dev *pci_dev;
259 int firmware_first;
260};
261
262static int hest_source_is_pcie_aer(struct acpi_hest_header *hest_hdr)
263{
264 if (hest_hdr->type == ACPI_HEST_TYPE_AER_ROOT_PORT ||
265 hest_hdr->type == ACPI_HEST_TYPE_AER_ENDPOINT ||
266 hest_hdr->type == ACPI_HEST_TYPE_AER_BRIDGE)
267 return 1;
268 return 0;
269}
270
271static int aer_hest_parse(struct acpi_hest_header *hest_hdr, void *data)
272{
273 struct aer_hest_parse_info *info = data;
274 struct acpi_hest_aer_common *p;
275 int ff;
276
277 if (!hest_source_is_pcie_aer(hest_hdr))
278 return 0;
279
280 p = (struct acpi_hest_aer_common *)(hest_hdr + 1);
281 ff = !!(p->flags & ACPI_HEST_FIRMWARE_FIRST);
282
283 /*
284 * If no specific device is supplied, determine whether
285 * FIRMWARE_FIRST is set for *any* PCIe device.
286 */
287 if (!info->pci_dev) {
288 info->firmware_first |= ff;
289 return 0;
290 }
291
292 /* Otherwise, check the specific device */
293 if (p->flags & ACPI_HEST_GLOBAL) {
294 if (hest_match_type(hest_hdr, info->pci_dev))
295 info->firmware_first = ff;
296 } else
297 if (hest_match_pci(p, info->pci_dev))
298 info->firmware_first = ff;
299
300 return 0;
301}
302
303static void aer_set_firmware_first(struct pci_dev *pci_dev)
304{
305 int rc;
306 struct aer_hest_parse_info info = {
307 .pci_dev = pci_dev,
308 .firmware_first = 0,
309 };
310
311 rc = apei_hest_parse(aer_hest_parse, &info);
312
313 if (rc)
314 pci_dev->__aer_firmware_first = 0;
315 else
316 pci_dev->__aer_firmware_first = info.firmware_first;
317 pci_dev->__aer_firmware_first_valid = 1;
318}
319
320int pcie_aer_get_firmware_first(struct pci_dev *dev)
321{
322 if (!pci_is_pcie(dev))
323 return 0;
324
325 if (!dev->__aer_firmware_first_valid)
326 aer_set_firmware_first(dev);
327 return dev->__aer_firmware_first;
328}
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -0500329#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
330 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500331
332static bool aer_firmware_first;
333
334/**
335 * aer_acpi_firmware_first - Check if APEI should control AER.
336 */
337bool aer_acpi_firmware_first(void)
338{
339 static bool parsed = false;
340 struct aer_hest_parse_info info = {
341 .pci_dev = NULL, /* Check all PCIe devices */
342 .firmware_first = 0,
343 };
344
345 if (!parsed) {
346 apei_hest_parse(aer_hest_parse, &info);
347 aer_firmware_first = info.firmware_first;
348 parsed = true;
349 }
350 return aer_firmware_first;
351}
352#endif
353
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500354#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
355 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
356
357int pci_enable_pcie_error_reporting(struct pci_dev *dev)
358{
359 if (pcie_aer_get_firmware_first(dev))
360 return -EIO;
361
362 if (!dev->aer_cap)
363 return -EIO;
364
365 return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
366}
367EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
368
369int pci_disable_pcie_error_reporting(struct pci_dev *dev)
370{
371 if (pcie_aer_get_firmware_first(dev))
372 return -EIO;
373
374 return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
375 PCI_EXP_AER_FLAGS);
376}
377EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
378
379int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
380{
381 int pos;
382 u32 status;
383
384 pos = dev->aer_cap;
385 if (!pos)
386 return -EIO;
387
388 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
389 if (status)
390 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
391
392 return 0;
393}
394EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
395
396int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
397{
398 int pos;
399 u32 status;
400 int port_type;
401
402 if (!pci_is_pcie(dev))
403 return -ENODEV;
404
405 pos = dev->aer_cap;
406 if (!pos)
407 return -EIO;
408
409 port_type = pci_pcie_type(dev);
410 if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
411 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
412 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
413 }
414
415 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
416 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
417
418 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
419 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
420
421 return 0;
422}
423
Rajat Jain60ed9822018-06-21 16:48:26 -0700424void pci_aer_init(struct pci_dev *dev)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500425{
426 dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500427
428 if (dev->aer_cap)
429 dev->aer_stats = kzalloc(sizeof(struct aer_stats), GFP_KERNEL);
430
Rajat Jain60ed9822018-06-21 16:48:26 -0700431 pci_cleanup_aer_error_status_regs(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500432}
433
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500434void pci_aer_exit(struct pci_dev *dev)
435{
436 kfree(dev->aer_stats);
437 dev->aer_stats = NULL;
438}
439
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500440#define AER_AGENT_RECEIVER 0
441#define AER_AGENT_REQUESTER 1
442#define AER_AGENT_COMPLETER 2
443#define AER_AGENT_TRANSMITTER 3
444
445#define AER_AGENT_REQUESTER_MASK(t) ((t == AER_CORRECTABLE) ? \
446 0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
447#define AER_AGENT_COMPLETER_MASK(t) ((t == AER_CORRECTABLE) ? \
448 0 : PCI_ERR_UNC_COMP_ABORT)
449#define AER_AGENT_TRANSMITTER_MASK(t) ((t == AER_CORRECTABLE) ? \
450 (PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
451
452#define AER_GET_AGENT(t, e) \
453 ((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER : \
454 (e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER : \
455 (e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER : \
456 AER_AGENT_RECEIVER)
457
458#define AER_PHYSICAL_LAYER_ERROR 0
459#define AER_DATA_LINK_LAYER_ERROR 1
460#define AER_TRANSACTION_LAYER_ERROR 2
461
462#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
463 PCI_ERR_COR_RCVR : 0)
464#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
465 (PCI_ERR_COR_BAD_TLP| \
466 PCI_ERR_COR_BAD_DLLP| \
467 PCI_ERR_COR_REP_ROLL| \
468 PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
469
470#define AER_GET_LAYER_ERROR(t, e) \
471 ((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
472 (e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
473 AER_TRANSACTION_LAYER_ERROR)
474
475/*
476 * AER error strings
477 */
478static const char *aer_error_severity_string[] = {
479 "Uncorrected (Non-Fatal)",
480 "Uncorrected (Fatal)",
481 "Corrected"
482};
483
484static const char *aer_error_layer[] = {
485 "Physical Layer",
486 "Data Link Layer",
487 "Transaction Layer"
488};
489
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500490static const char *aer_correctable_error_string[AER_MAX_TYPEOF_COR_ERRS] = {
Tyler Baicarbd237802018-06-26 11:44:15 -0400491 "RxErr", /* Bit Position 0 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500492 NULL,
493 NULL,
494 NULL,
495 NULL,
496 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400497 "BadTLP", /* Bit Position 6 */
498 "BadDLLP", /* Bit Position 7 */
499 "Rollover", /* Bit Position 8 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500500 NULL,
501 NULL,
502 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400503 "Timeout", /* Bit Position 12 */
504 "NonFatalErr", /* Bit Position 13 */
505 "CorrIntErr", /* Bit Position 14 */
506 "HeaderOF", /* Bit Position 15 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500507};
508
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500509static const char *aer_uncorrectable_error_string[AER_MAX_TYPEOF_UNCOR_ERRS] = {
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500510 "Undefined", /* Bit Position 0 */
511 NULL,
512 NULL,
513 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400514 "DLP", /* Bit Position 4 */
515 "SDES", /* Bit Position 5 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500516 NULL,
517 NULL,
518 NULL,
519 NULL,
520 NULL,
521 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400522 "TLP", /* Bit Position 12 */
523 "FCP", /* Bit Position 13 */
524 "CmpltTO", /* Bit Position 14 */
525 "CmpltAbrt", /* Bit Position 15 */
526 "UnxCmplt", /* Bit Position 16 */
527 "RxOF", /* Bit Position 17 */
528 "MalfTLP", /* Bit Position 18 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500529 "ECRC", /* Bit Position 19 */
Tyler Baicarbd237802018-06-26 11:44:15 -0400530 "UnsupReq", /* Bit Position 20 */
531 "ACSViol", /* Bit Position 21 */
532 "UncorrIntErr", /* Bit Position 22 */
533 "BlockedTLP", /* Bit Position 23 */
534 "AtomicOpBlocked", /* Bit Position 24 */
535 "TLPBlockedErr", /* Bit Position 25 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500536};
537
538static const char *aer_agent_string[] = {
539 "Receiver ID",
540 "Requester ID",
541 "Completer ID",
542 "Transmitter ID"
543};
544
Rajat Jain81aa5202018-06-21 16:48:28 -0700545#define aer_stats_dev_attr(name, stats_array, strings_array, \
546 total_string, total_field) \
547 static ssize_t \
548 name##_show(struct device *dev, struct device_attribute *attr, \
549 char *buf) \
550{ \
551 unsigned int i; \
552 char *str = buf; \
553 struct pci_dev *pdev = to_pci_dev(dev); \
554 u64 *stats = pdev->aer_stats->stats_array; \
555 \
556 for (i = 0; i < ARRAY_SIZE(strings_array); i++) { \
557 if (strings_array[i]) \
558 str += sprintf(str, "%s %llu\n", \
559 strings_array[i], stats[i]); \
560 else if (stats[i]) \
561 str += sprintf(str, #stats_array "_bit[%d] %llu\n",\
562 i, stats[i]); \
563 } \
564 str += sprintf(str, "TOTAL_%s %llu\n", total_string, \
565 pdev->aer_stats->total_field); \
566 return str-buf; \
567} \
568static DEVICE_ATTR_RO(name)
569
570aer_stats_dev_attr(aer_dev_correctable, dev_cor_errs,
571 aer_correctable_error_string, "ERR_COR",
572 dev_total_cor_errs);
573aer_stats_dev_attr(aer_dev_fatal, dev_fatal_errs,
574 aer_uncorrectable_error_string, "ERR_FATAL",
575 dev_total_fatal_errs);
576aer_stats_dev_attr(aer_dev_nonfatal, dev_nonfatal_errs,
577 aer_uncorrectable_error_string, "ERR_NONFATAL",
578 dev_total_nonfatal_errs);
579
Rajat Jain12833012018-06-21 16:48:29 -0700580#define aer_stats_rootport_attr(name, field) \
581 static ssize_t \
582 name##_show(struct device *dev, struct device_attribute *attr, \
583 char *buf) \
584{ \
585 struct pci_dev *pdev = to_pci_dev(dev); \
586 return sprintf(buf, "%llu\n", pdev->aer_stats->field); \
587} \
588static DEVICE_ATTR_RO(name)
589
590aer_stats_rootport_attr(aer_rootport_total_err_cor,
591 rootport_total_cor_errs);
592aer_stats_rootport_attr(aer_rootport_total_err_fatal,
593 rootport_total_fatal_errs);
594aer_stats_rootport_attr(aer_rootport_total_err_nonfatal,
595 rootport_total_nonfatal_errs);
596
Rajat Jain81aa5202018-06-21 16:48:28 -0700597static struct attribute *aer_stats_attrs[] __ro_after_init = {
598 &dev_attr_aer_dev_correctable.attr,
599 &dev_attr_aer_dev_fatal.attr,
600 &dev_attr_aer_dev_nonfatal.attr,
Rajat Jain12833012018-06-21 16:48:29 -0700601 &dev_attr_aer_rootport_total_err_cor.attr,
602 &dev_attr_aer_rootport_total_err_fatal.attr,
603 &dev_attr_aer_rootport_total_err_nonfatal.attr,
Rajat Jain81aa5202018-06-21 16:48:28 -0700604 NULL
605};
606
607static umode_t aer_stats_attrs_are_visible(struct kobject *kobj,
608 struct attribute *a, int n)
609{
610 struct device *dev = kobj_to_dev(kobj);
611 struct pci_dev *pdev = to_pci_dev(dev);
612
613 if (!pdev->aer_stats)
614 return 0;
615
Rajat Jain12833012018-06-21 16:48:29 -0700616 if ((a == &dev_attr_aer_rootport_total_err_cor.attr ||
617 a == &dev_attr_aer_rootport_total_err_fatal.attr ||
618 a == &dev_attr_aer_rootport_total_err_nonfatal.attr) &&
619 pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT)
620 return 0;
621
Rajat Jain81aa5202018-06-21 16:48:28 -0700622 return a->mode;
623}
624
625const struct attribute_group aer_stats_attr_group = {
626 .attrs = aer_stats_attrs,
627 .is_visible = aer_stats_attrs_are_visible,
628};
629
630static void pci_dev_aer_stats_incr(struct pci_dev *pdev,
631 struct aer_err_info *info)
632{
633 int status, i, max = -1;
634 u64 *counter = NULL;
635 struct aer_stats *aer_stats = pdev->aer_stats;
636
637 if (!aer_stats)
638 return;
639
640 switch (info->severity) {
641 case AER_CORRECTABLE:
642 aer_stats->dev_total_cor_errs++;
643 counter = &aer_stats->dev_cor_errs[0];
644 max = AER_MAX_TYPEOF_COR_ERRS;
645 break;
646 case AER_NONFATAL:
647 aer_stats->dev_total_nonfatal_errs++;
648 counter = &aer_stats->dev_nonfatal_errs[0];
649 max = AER_MAX_TYPEOF_UNCOR_ERRS;
650 break;
651 case AER_FATAL:
652 aer_stats->dev_total_fatal_errs++;
653 counter = &aer_stats->dev_fatal_errs[0];
654 max = AER_MAX_TYPEOF_UNCOR_ERRS;
655 break;
656 }
657
658 status = (info->status & ~info->mask);
659 for (i = 0; i < max; i++)
660 if (status & (1 << i))
661 counter[i]++;
662}
663
Rajat Jain12833012018-06-21 16:48:29 -0700664static void pci_rootport_aer_stats_incr(struct pci_dev *pdev,
665 struct aer_err_source *e_src)
666{
667 struct aer_stats *aer_stats = pdev->aer_stats;
668
669 if (!aer_stats)
670 return;
671
672 if (e_src->status & PCI_ERR_ROOT_COR_RCV)
673 aer_stats->rootport_total_cor_errs++;
674
675 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
676 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
677 aer_stats->rootport_total_fatal_errs++;
678 else
679 aer_stats->rootport_total_nonfatal_errs++;
680 }
681}
682
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500683static void __print_tlp_header(struct pci_dev *dev,
684 struct aer_header_log_regs *t)
685{
686 pci_err(dev, " TLP Header: %08x %08x %08x %08x\n",
687 t->dw0, t->dw1, t->dw2, t->dw3);
688}
689
690static void __aer_print_error(struct pci_dev *dev,
691 struct aer_err_info *info)
692{
693 int i, status;
694 const char *errmsg = NULL;
695 status = (info->status & ~info->mask);
696
697 for (i = 0; i < 32; i++) {
698 if (!(status & (1 << i)))
699 continue;
700
701 if (info->severity == AER_CORRECTABLE)
702 errmsg = i < ARRAY_SIZE(aer_correctable_error_string) ?
703 aer_correctable_error_string[i] : NULL;
704 else
705 errmsg = i < ARRAY_SIZE(aer_uncorrectable_error_string) ?
706 aer_uncorrectable_error_string[i] : NULL;
707
708 if (errmsg)
709 pci_err(dev, " [%2d] %-22s%s\n", i, errmsg,
710 info->first_error == i ? " (First)" : "");
711 else
712 pci_err(dev, " [%2d] Unknown Error Bit%s\n",
713 i, info->first_error == i ? " (First)" : "");
714 }
Rajat Jain81aa5202018-06-21 16:48:28 -0700715 pci_dev_aer_stats_incr(dev, info);
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500716}
717
Keith Busch1e451162018-07-19 16:16:55 -0500718void aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500719{
720 int layer, agent;
721 int id = ((dev->bus->number << 8) | dev->devfn);
722
723 if (!info->status) {
724 pci_err(dev, "PCIe Bus Error: severity=%s, type=Inaccessible, (Unregistered Agent ID)\n",
725 aer_error_severity_string[info->severity]);
726 goto out;
727 }
728
729 layer = AER_GET_LAYER_ERROR(info->severity, info->status);
730 agent = AER_GET_AGENT(info->severity, info->status);
731
732 pci_err(dev, "PCIe Bus Error: severity=%s, type=%s, (%s)\n",
733 aer_error_severity_string[info->severity],
734 aer_error_layer[layer], aer_agent_string[agent]);
735
736 pci_err(dev, " device [%04x:%04x] error status/mask=%08x/%08x\n",
737 dev->vendor, dev->device,
738 info->status, info->mask);
739
740 __aer_print_error(dev, info);
741
742 if (info->tlp_header_valid)
743 __print_tlp_header(dev, &info->tlp);
744
745out:
746 if (info->id && info->error_dev_num > 1 && info->id == id)
747 pci_err(dev, " Error of this Agent is reported first\n");
748
749 trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
750 info->severity, info->tlp_header_valid, &info->tlp);
751}
752
753static void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
754{
755 u8 bus = info->id >> 8;
756 u8 devfn = info->id & 0xff;
757
758 pci_info(dev, "AER: %s%s error received: %04x:%02x:%02x.%d\n",
759 info->multi_error_valid ? "Multiple " : "",
760 aer_error_severity_string[info->severity],
761 pci_domain_nr(dev->bus), bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
762}
763
764#ifdef CONFIG_ACPI_APEI_PCIEAER
765int cper_severity_to_aer(int cper_severity)
766{
767 switch (cper_severity) {
768 case CPER_SEV_RECOVERABLE:
769 return AER_NONFATAL;
770 case CPER_SEV_FATAL:
771 return AER_FATAL;
772 default:
773 return AER_CORRECTABLE;
774 }
775}
776EXPORT_SYMBOL_GPL(cper_severity_to_aer);
777
778void cper_print_aer(struct pci_dev *dev, int aer_severity,
779 struct aer_capability_regs *aer)
780{
781 int layer, agent, tlp_header_valid = 0;
782 u32 status, mask;
783 struct aer_err_info info;
784
785 if (aer_severity == AER_CORRECTABLE) {
786 status = aer->cor_status;
787 mask = aer->cor_mask;
788 } else {
789 status = aer->uncor_status;
790 mask = aer->uncor_mask;
791 tlp_header_valid = status & AER_LOG_TLP_MASKS;
792 }
793
794 layer = AER_GET_LAYER_ERROR(aer_severity, status);
795 agent = AER_GET_AGENT(aer_severity, status);
796
797 memset(&info, 0, sizeof(info));
798 info.severity = aer_severity;
799 info.status = status;
800 info.mask = mask;
801 info.first_error = PCI_ERR_CAP_FEP(aer->cap_control);
802
803 pci_err(dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
804 __aer_print_error(dev, &info);
805 pci_err(dev, "aer_layer=%s, aer_agent=%s\n",
806 aer_error_layer[layer], aer_agent_string[agent]);
807
808 if (aer_severity != AER_CORRECTABLE)
809 pci_err(dev, "aer_uncor_severity: 0x%08x\n",
810 aer->uncor_severity);
811
812 if (tlp_header_valid)
813 __print_tlp_header(dev, &aer->header_log);
814
815 trace_aer_event(dev_name(&dev->dev), (status & ~mask),
816 aer_severity, tlp_header_valid, &aer->header_log);
817}
818#endif
819
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500820/**
821 * add_error_device - list device to be handled
822 * @e_info: pointer to error info
823 * @dev: pointer to pci_dev to be added
824 */
825static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
826{
827 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
828 e_info->dev[e_info->error_dev_num] = dev;
829 e_info->error_dev_num++;
830 return 0;
831 }
832 return -ENOSPC;
833}
834
835/**
836 * is_error_source - check whether the device is source of reported error
837 * @dev: pointer to pci_dev to be checked
838 * @e_info: pointer to reported error info
839 */
840static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
841{
842 int pos;
843 u32 status, mask;
844 u16 reg16;
845
846 /*
847 * When bus id is equal to 0, it might be a bad id
848 * reported by root port.
849 */
850 if ((PCI_BUS_NUM(e_info->id) != 0) &&
851 !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
852 /* Device ID match? */
853 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
854 return true;
855
856 /* Continue id comparing if there is no multiple error */
857 if (!e_info->multi_error_valid)
858 return false;
859 }
860
861 /*
862 * When either
863 * 1) bus id is equal to 0. Some ports might lose the bus
864 * id of error source id;
865 * 2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
866 * 3) There are multiple errors and prior ID comparing fails;
867 * We check AER status registers to find possible reporter.
868 */
869 if (atomic_read(&dev->enable_cnt) == 0)
870 return false;
871
872 /* Check if AER is enabled */
873 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
874 if (!(reg16 & PCI_EXP_AER_FLAGS))
875 return false;
876
877 pos = dev->aer_cap;
878 if (!pos)
879 return false;
880
881 /* Check if error is recorded */
882 if (e_info->severity == AER_CORRECTABLE) {
883 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
884 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
885 } else {
886 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
887 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
888 }
889 if (status & ~mask)
890 return true;
891
892 return false;
893}
894
895static int find_device_iter(struct pci_dev *dev, void *data)
896{
897 struct aer_err_info *e_info = (struct aer_err_info *)data;
898
899 if (is_error_source(dev, e_info)) {
900 /* List this device */
901 if (add_error_device(e_info, dev)) {
902 /* We cannot handle more... Stop iteration */
903 /* TODO: Should print error message here? */
904 return 1;
905 }
906
907 /* If there is only a single error, stop iteration */
908 if (!e_info->multi_error_valid)
909 return 1;
910 }
911 return 0;
912}
913
914/**
915 * find_source_device - search through device hierarchy for source device
916 * @parent: pointer to Root Port pci_dev data structure
917 * @e_info: including detailed error information such like id
918 *
919 * Return true if found.
920 *
921 * Invoked by DPC when error is detected at the Root Port.
922 * Caller of this function must set id, severity, and multi_error_valid of
923 * struct aer_err_info pointed by @e_info properly. This function must fill
924 * e_info->error_dev_num and e_info->dev[], based on the given information.
925 */
926static bool find_source_device(struct pci_dev *parent,
927 struct aer_err_info *e_info)
928{
929 struct pci_dev *dev = parent;
930 int result;
931
932 /* Must reset in this function */
933 e_info->error_dev_num = 0;
934
935 /* Is Root Port an agent that sends error message? */
936 result = find_device_iter(dev, e_info);
937 if (result)
938 return true;
939
940 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
941
942 if (!e_info->error_dev_num) {
943 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
944 e_info->id);
945 return false;
946 }
947 return true;
948}
949
950/**
951 * handle_error_source - handle logging error into an event log
952 * @dev: pointer to pci_dev data structure of error source device
953 * @info: comprehensive error information
954 *
955 * Invoked when an error being detected by Root Port.
956 */
957static void handle_error_source(struct pci_dev *dev, struct aer_err_info *info)
958{
959 int pos;
960
961 if (info->severity == AER_CORRECTABLE) {
962 /*
963 * Correctable error does not need software intervention.
964 * No need to go through error recovery process.
965 */
966 pos = dev->aer_cap;
967 if (pos)
968 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
969 info->status);
970 } else if (info->severity == AER_NONFATAL)
971 pcie_do_nonfatal_recovery(dev);
972 else if (info->severity == AER_FATAL)
973 pcie_do_fatal_recovery(dev, PCIE_PORT_SERVICE_AER);
974}
975
976#ifdef CONFIG_ACPI_APEI_PCIEAER
977
978#define AER_RECOVER_RING_ORDER 4
979#define AER_RECOVER_RING_SIZE (1 << AER_RECOVER_RING_ORDER)
980
981struct aer_recover_entry {
982 u8 bus;
983 u8 devfn;
984 u16 domain;
985 int severity;
986 struct aer_capability_regs *regs;
987};
988
989static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
990 AER_RECOVER_RING_SIZE);
991
992static void aer_recover_work_func(struct work_struct *work)
993{
994 struct aer_recover_entry entry;
995 struct pci_dev *pdev;
996
997 while (kfifo_get(&aer_recover_ring, &entry)) {
998 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
999 entry.devfn);
1000 if (!pdev) {
1001 pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
1002 entry.domain, entry.bus,
1003 PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
1004 continue;
1005 }
1006 cper_print_aer(pdev, entry.severity, entry.regs);
1007 if (entry.severity == AER_NONFATAL)
1008 pcie_do_nonfatal_recovery(pdev);
1009 else if (entry.severity == AER_FATAL)
1010 pcie_do_fatal_recovery(pdev, PCIE_PORT_SERVICE_AER);
1011 pci_dev_put(pdev);
1012 }
1013}
1014
1015/*
1016 * Mutual exclusion for writers of aer_recover_ring, reader side don't
1017 * need lock, because there is only one reader and lock is not needed
1018 * between reader and writer.
1019 */
1020static DEFINE_SPINLOCK(aer_recover_ring_lock);
1021static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
1022
1023void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
1024 int severity, struct aer_capability_regs *aer_regs)
1025{
1026 unsigned long flags;
1027 struct aer_recover_entry entry = {
1028 .bus = bus,
1029 .devfn = devfn,
1030 .domain = domain,
1031 .severity = severity,
1032 .regs = aer_regs,
1033 };
1034
1035 spin_lock_irqsave(&aer_recover_ring_lock, flags);
1036 if (kfifo_put(&aer_recover_ring, entry))
1037 schedule_work(&aer_recover_work);
1038 else
1039 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
1040 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1041 spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
1042}
1043EXPORT_SYMBOL_GPL(aer_recover_queue);
1044#endif
1045
1046/**
Keith Busch1e451162018-07-19 16:16:55 -05001047 * aer_get_device_error_info - read error status from dev and store it to info
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001048 * @dev: pointer to the device expected to have a error record
1049 * @info: pointer to structure to store the error record
1050 *
1051 * Return 1 on success, 0 on error.
1052 *
1053 * Note that @info is reused among all error devices. Clear fields properly.
1054 */
Keith Busch1e451162018-07-19 16:16:55 -05001055int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001056{
1057 int pos, temp;
1058
1059 /* Must reset in this function */
1060 info->status = 0;
1061 info->tlp_header_valid = 0;
1062
1063 pos = dev->aer_cap;
1064
1065 /* The device might not support AER */
1066 if (!pos)
1067 return 0;
1068
1069 if (info->severity == AER_CORRECTABLE) {
1070 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
1071 &info->status);
1072 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
1073 &info->mask);
1074 if (!(info->status & ~info->mask))
1075 return 0;
1076 } else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1077 info->severity == AER_NONFATAL) {
1078
1079 /* Link is still healthy for IO reads */
1080 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
1081 &info->status);
1082 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
1083 &info->mask);
1084 if (!(info->status & ~info->mask))
1085 return 0;
1086
1087 /* Get First Error Pointer */
1088 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
1089 info->first_error = PCI_ERR_CAP_FEP(temp);
1090
1091 if (info->status & AER_LOG_TLP_MASKS) {
1092 info->tlp_header_valid = 1;
1093 pci_read_config_dword(dev,
1094 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
1095 pci_read_config_dword(dev,
1096 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
1097 pci_read_config_dword(dev,
1098 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
1099 pci_read_config_dword(dev,
1100 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
1101 }
1102 }
1103
1104 return 1;
1105}
1106
1107static inline void aer_process_err_devices(struct aer_err_info *e_info)
1108{
1109 int i;
1110
1111 /* Report all before handle them, not to lost records by reset etc. */
1112 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001113 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001114 aer_print_error(e_info->dev[i], e_info);
1115 }
1116 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001117 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001118 handle_error_source(e_info->dev[i], e_info);
1119 }
1120}
1121
1122/**
1123 * aer_isr_one_error - consume an error detected by root port
1124 * @rpc: pointer to the root port which holds an error
1125 * @e_src: pointer to an error source
1126 */
1127static void aer_isr_one_error(struct aer_rpc *rpc,
1128 struct aer_err_source *e_src)
1129{
1130 struct pci_dev *pdev = rpc->rpd;
1131 struct aer_err_info *e_info = &rpc->e_info;
1132
Rajat Jain12833012018-06-21 16:48:29 -07001133 pci_rootport_aer_stats_incr(pdev, e_src);
1134
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001135 /*
1136 * There is a possibility that both correctable error and
1137 * uncorrectable error being logged. Report correctable error first.
1138 */
1139 if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
1140 e_info->id = ERR_COR_ID(e_src->id);
1141 e_info->severity = AER_CORRECTABLE;
1142
1143 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
1144 e_info->multi_error_valid = 1;
1145 else
1146 e_info->multi_error_valid = 0;
1147 aer_print_port_info(pdev, e_info);
1148
1149 if (find_source_device(pdev, e_info))
1150 aer_process_err_devices(e_info);
1151 }
1152
1153 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
1154 e_info->id = ERR_UNCOR_ID(e_src->id);
1155
1156 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
1157 e_info->severity = AER_FATAL;
1158 else
1159 e_info->severity = AER_NONFATAL;
1160
1161 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
1162 e_info->multi_error_valid = 1;
1163 else
1164 e_info->multi_error_valid = 0;
1165
1166 aer_print_port_info(pdev, e_info);
1167
1168 if (find_source_device(pdev, e_info))
1169 aer_process_err_devices(e_info);
1170 }
1171}
1172
1173/**
1174 * get_e_source - retrieve an error source
1175 * @rpc: pointer to the root port which holds an error
1176 * @e_src: pointer to store retrieved error source
1177 *
1178 * Return 1 if an error source is retrieved, otherwise 0.
1179 *
1180 * Invoked by DPC handler to consume an error.
1181 */
1182static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
1183{
1184 unsigned long flags;
1185
1186 /* Lock access to Root error producer/consumer index */
1187 spin_lock_irqsave(&rpc->e_lock, flags);
1188 if (rpc->prod_idx == rpc->cons_idx) {
1189 spin_unlock_irqrestore(&rpc->e_lock, flags);
1190 return 0;
1191 }
1192
1193 *e_src = rpc->e_sources[rpc->cons_idx];
1194 rpc->cons_idx++;
1195 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
1196 rpc->cons_idx = 0;
1197 spin_unlock_irqrestore(&rpc->e_lock, flags);
1198
1199 return 1;
1200}
1201
1202/**
1203 * aer_isr - consume errors detected by root port
1204 * @work: definition of this work item
1205 *
1206 * Invoked, as DPC, when root port records new detected error
1207 */
1208static void aer_isr(struct work_struct *work)
1209{
1210 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
1211 struct aer_err_source uninitialized_var(e_src);
1212
1213 mutex_lock(&rpc->rpc_mutex);
1214 while (get_e_source(rpc, &e_src))
1215 aer_isr_one_error(rpc, &e_src);
1216 mutex_unlock(&rpc->rpc_mutex);
1217}
1218
Bjorn Helgaas3c43a642018-06-08 08:31:57 -05001219/**
1220 * aer_irq - Root Port's ISR
1221 * @irq: IRQ assigned to Root Port
1222 * @context: pointer to Root Port data structure
1223 *
1224 * Invoked when Root Port detects AER messages.
1225 */
1226irqreturn_t aer_irq(int irq, void *context)
1227{
1228 unsigned int status, id;
1229 struct pcie_device *pdev = (struct pcie_device *)context;
1230 struct aer_rpc *rpc = get_service_data(pdev);
1231 int next_prod_idx;
1232 unsigned long flags;
1233 int pos;
1234
1235 pos = pdev->port->aer_cap;
1236 /*
1237 * Must lock access to Root Error Status Reg, Root Error ID Reg,
1238 * and Root error producer/consumer index
1239 */
1240 spin_lock_irqsave(&rpc->e_lock, flags);
1241
1242 /* Read error status */
1243 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
1244 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
1245 spin_unlock_irqrestore(&rpc->e_lock, flags);
1246 return IRQ_NONE;
1247 }
1248
1249 /* Read error source and clear error status */
1250 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
1251 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
1252
1253 /* Store error source for later DPC handler */
1254 next_prod_idx = rpc->prod_idx + 1;
1255 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
1256 next_prod_idx = 0;
1257 if (next_prod_idx == rpc->cons_idx) {
1258 /*
1259 * Error Storm Condition - possibly the same error occurred.
1260 * Drop the error.
1261 */
1262 spin_unlock_irqrestore(&rpc->e_lock, flags);
1263 return IRQ_HANDLED;
1264 }
1265 rpc->e_sources[rpc->prod_idx].status = status;
1266 rpc->e_sources[rpc->prod_idx].id = id;
1267 rpc->prod_idx = next_prod_idx;
1268 spin_unlock_irqrestore(&rpc->e_lock, flags);
1269
1270 /* Invoke DPC handler */
1271 schedule_work(&rpc->dpc_handler);
1272
1273 return IRQ_HANDLED;
1274}
1275EXPORT_SYMBOL_GPL(aer_irq);
1276
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001277static int set_device_error_reporting(struct pci_dev *dev, void *data)
1278{
1279 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +08001280 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001281
Yijing Wang62f87c02012-07-24 17:20:03 +08001282 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
1283 (type == PCI_EXP_TYPE_UPSTREAM) ||
1284 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001285 if (enable)
1286 pci_enable_pcie_error_reporting(dev);
1287 else
1288 pci_disable_pcie_error_reporting(dev);
1289 }
1290
1291 if (enable)
1292 pcie_set_ecrc_checking(dev);
1293
1294 return 0;
1295}
1296
1297/**
1298 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
1299 * @dev: pointer to root port's pci_dev data structure
1300 * @enable: true = enable error reporting, false = disable error reporting.
1301 */
1302static void set_downstream_devices_error_reporting(struct pci_dev *dev,
1303 bool enable)
1304{
1305 set_device_error_reporting(dev, &enable);
1306
1307 if (!dev->subordinate)
1308 return;
1309 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
1310}
1311
1312/**
1313 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
1314 * @rpc: pointer to a Root Port data structure
1315 *
1316 * Invoked when PCIe bus loads AER service driver.
1317 */
1318static void aer_enable_rootport(struct aer_rpc *rpc)
1319{
Keith Busche13d17f2018-04-09 16:04:42 -06001320 struct pci_dev *pdev = rpc->rpd;
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001321 int aer_pos;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001322 u16 reg16;
1323 u32 reg32;
1324
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001325 /* Clear PCIe Capability's Device Status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001326 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
1327 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001328
1329 /* Disable system error generation in response to error messages */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001330 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
1331 SYSTEM_ERROR_INTR_ON_MESG_MASK);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001332
Keith Busch66b80802016-09-27 16:23:34 -04001333 aer_pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001334 /* Clear error status */
1335 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
1336 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
1337 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
1338 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
1339 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
1340 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
1341
1342 /*
1343 * Enable error reporting for the root port device and downstream port
1344 * devices.
1345 */
1346 set_downstream_devices_error_reporting(pdev, true);
1347
1348 /* Enable Root Port's interrupt in response to error messages */
1349 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
1350 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1351 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
1352}
1353
1354/**
1355 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
1356 * @rpc: pointer to a Root Port data structure
1357 *
1358 * Invoked when PCIe bus unloads AER service driver.
1359 */
1360static void aer_disable_rootport(struct aer_rpc *rpc)
1361{
Keith Busche13d17f2018-04-09 16:04:42 -06001362 struct pci_dev *pdev = rpc->rpd;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001363 u32 reg32;
1364 int pos;
1365
1366 /*
1367 * Disable error reporting for the root port device and downstream port
1368 * devices.
1369 */
1370 set_downstream_devices_error_reporting(pdev, false);
1371
Keith Busch66b80802016-09-27 16:23:34 -04001372 pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001373 /* Disable Root's interrupt in response to error messages */
1374 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1375 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1376 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
1377
1378 /* Clear Root's error status reg */
1379 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1380 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
1381}
1382
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001383/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001384 * aer_alloc_rpc - allocate Root Port data structure
1385 * @dev: pointer to the pcie_dev data structure
1386 *
1387 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001388 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001389static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001390{
1391 struct aer_rpc *rpc;
1392
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001393 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
1394 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001395 return NULL;
1396
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001397 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -07001398 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001399
Keith Busche13d17f2018-04-09 16:04:42 -06001400 rpc->rpd = dev->port;
David Howells65f27f32006-11-22 14:55:48 +00001401 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001402 mutex_init(&rpc->rpc_mutex);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001403
Stefan Assmann45e829e2009-12-03 06:49:24 -05001404 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001405 set_service_data(dev, rpc);
1406
1407 return rpc;
1408}
1409
1410/**
1411 * aer_remove - clean up resources
1412 * @dev: pointer to the pcie_dev data structure
1413 *
1414 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001415 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001416static void aer_remove(struct pcie_device *dev)
1417{
1418 struct aer_rpc *rpc = get_service_data(dev);
1419
1420 if (rpc) {
1421 /* If register interrupt service, it must be free. */
1422 if (rpc->isr)
1423 free_irq(dev->irq, dev);
1424
Sebastian Andrzej Siewior4ae21822016-01-25 10:08:00 -06001425 flush_work(&rpc->dpc_handler);
Hidetoshi Seto460d2982010-04-15 13:10:03 +09001426 aer_disable_rootport(rpc);
1427 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001428 set_service_data(dev, NULL);
1429 }
1430}
1431
1432/**
1433 * aer_probe - initialize resources
1434 * @dev: pointer to the pcie_dev data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001435 *
1436 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001437 */
Bill Pemberton15856ad2012-11-21 15:35:00 -05001438static int aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001439{
1440 int status;
1441 struct aer_rpc *rpc;
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001442 struct device *device = &dev->port->dev;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001443
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001444 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001445 rpc = aer_alloc_rpc(dev);
1446 if (!rpc) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001447 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001448 aer_remove(dev);
1449 return -ENOMEM;
1450 }
1451
1452 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001453 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
1454 if (status) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001455 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
1456 dev->irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001457 aer_remove(dev);
1458 return status;
1459 }
1460
1461 rpc->isr = 1;
1462
1463 aer_enable_rootport(rpc);
Bjorn Helgaas68a55ae2016-11-21 15:34:02 -06001464 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
1465 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001466}
1467
1468/**
1469 * aer_root_reset - reset link on Root Port
1470 * @dev: pointer to Root Port's pci_dev data structure
1471 *
1472 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001473 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001474static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
1475{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001476 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001477 int pos;
1478
Keith Busch66b80802016-09-27 16:23:34 -04001479 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001480
1481 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001482 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1483 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1484 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001485
Alex Williamson1b95ce82013-08-08 14:10:20 -06001486 pci_reset_bridge_secondary_bus(dev);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001487 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001488
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001489 /* Clear Root Error Status */
1490 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1491 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
1492
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001493 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001494 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1495 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1496 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001497
1498 return PCI_ERS_RESULT_RECOVERED;
1499}
1500
1501/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001502 * aer_error_resume - clean up corresponding error status bits
1503 * @dev: pointer to Root Port's pci_dev data structure
1504 *
1505 * Invoked by Port Bus driver during nonfatal recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001506 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001507static void aer_error_resume(struct pci_dev *dev)
1508{
1509 int pos;
1510 u32 status, mask;
1511 u16 reg16;
1512
1513 /* Clean up Root device status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001514 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &reg16);
1515 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001516
1517 /* Clean AER Root Error Status */
Keith Busch66b80802016-09-27 16:23:34 -04001518 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001519 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
1520 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
Oza Pawandeep7e9084b2018-05-17 16:44:13 -05001521 status &= ~mask; /* Clear corresponding nonfatal bits */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001522 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
1523}
1524
Bjorn Helgaas0054ca82018-06-08 08:31:42 -05001525static struct pcie_port_service_driver aerdriver = {
1526 .name = "aer",
1527 .port_type = PCI_EXP_TYPE_ROOT_PORT,
1528 .service = PCIE_PORT_SERVICE_AER,
1529
1530 .probe = aer_probe,
1531 .remove = aer_remove,
1532 .error_resume = aer_error_resume,
1533 .reset_link = aer_root_reset,
1534};
1535
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001536/**
1537 * aer_service_init - register AER root service driver
1538 *
1539 * Invoked when AER root service driver is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001540 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001541static int __init aer_service_init(void)
1542{
Rafael J. Wysockib22c3d82010-09-20 18:50:00 +02001543 if (!pci_aer_available() || aer_acpi_firmware_first())
Andi Kleen3e77a3f2009-09-16 22:40:22 +02001544 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +01001545 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001546}
Paul Gortmaker87563362016-08-24 16:57:46 -04001547device_initcall(aer_service_init);