blob: 85c713f6cfdb62120d31ea40788f62f6045a79ca [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];
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050047 unsigned short prod_idx; /* Error Producer Index */
48 unsigned short cons_idx; /* Error Consumer Index */
49 int isr;
50 spinlock_t e_lock; /*
51 * Lock access to Error Status/ID Regs
52 * and error producer/consumer index
53 */
54 struct mutex rpc_mutex; /*
55 * only one thread could do
56 * recovery on the same
57 * root port hierarchy
58 */
59};
60
Rajat Jaindb89ccb2018-06-30 15:07:17 -050061/* AER stats for the device */
62struct aer_stats {
63
64 /*
65 * Fields for all AER capable devices. They indicate the errors
66 * "as seen by this device". Note that this may mean that if an
67 * end point is causing problems, the AER counters may increment
68 * at its link partner (e.g. root port) because the errors will be
69 * "seen" by the link partner and not the the problematic end point
70 * itself (which may report all counters as 0 as it never saw any
71 * problems).
72 */
73 /* Counters for different type of correctable errors */
74 u64 dev_cor_errs[AER_MAX_TYPEOF_COR_ERRS];
75 /* Counters for different type of fatal uncorrectable errors */
76 u64 dev_fatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
77 /* Counters for different type of nonfatal uncorrectable errors */
78 u64 dev_nonfatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
79 /* Total number of ERR_COR sent by this device */
80 u64 dev_total_cor_errs;
81 /* Total number of ERR_FATAL sent by this device */
82 u64 dev_total_fatal_errs;
83 /* Total number of ERR_NONFATAL sent by this device */
84 u64 dev_total_nonfatal_errs;
85
86 /*
87 * Fields for Root ports & root complex event collectors only, these
88 * indicate the total number of ERR_COR, ERR_FATAL, and ERR_NONFATAL
89 * messages received by the root port / event collector, INCLUDING the
90 * ones that are generated internally (by the rootport itself)
91 */
92 u64 rootport_total_cor_errs;
93 u64 rootport_total_fatal_errs;
94 u64 rootport_total_nonfatal_errs;
95};
96
Bjorn Helgaas23e672b2018-06-08 08:41:28 -050097#define AER_LOG_TLP_MASKS (PCI_ERR_UNC_POISON_TLP| \
98 PCI_ERR_UNC_ECRC| \
99 PCI_ERR_UNC_UNSUP| \
100 PCI_ERR_UNC_COMP_ABORT| \
101 PCI_ERR_UNC_UNX_COMP| \
102 PCI_ERR_UNC_MALF_TLP)
103
104#define SYSTEM_ERROR_INTR_ON_MESG_MASK (PCI_EXP_RTCTL_SECEE| \
105 PCI_EXP_RTCTL_SENFEE| \
106 PCI_EXP_RTCTL_SEFEE)
107#define ROOT_PORT_INTR_ON_MESG_MASK (PCI_ERR_ROOT_CMD_COR_EN| \
108 PCI_ERR_ROOT_CMD_NONFATAL_EN| \
109 PCI_ERR_ROOT_CMD_FATAL_EN)
110#define ERR_COR_ID(d) (d & 0xffff)
111#define ERR_UNCOR_ID(d) (d >> 16)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +0800112
Randy Dunlap7f785762007-10-05 13:17:58 -0700113static int pcie_aer_disable;
114
115void pci_no_aer(void)
116{
Bjorn Helgaas7ece1412016-09-06 16:24:37 -0500117 pcie_aer_disable = 1;
Randy Dunlap7f785762007-10-05 13:17:58 -0700118}
119
Rafael J. Wysockif1a7bfa2010-08-21 01:50:52 +0200120bool pci_aer_available(void)
121{
122 return !pcie_aer_disable && pci_msi_enabled();
123}
124
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -0500125#ifdef CONFIG_PCIE_ECRC
126
127#define ECRC_POLICY_DEFAULT 0 /* ECRC set by BIOS */
128#define ECRC_POLICY_OFF 1 /* ECRC off for performance */
129#define ECRC_POLICY_ON 2 /* ECRC on for data integrity */
130
131static int ecrc_policy = ECRC_POLICY_DEFAULT;
132
133static const char *ecrc_policy_str[] = {
134 [ECRC_POLICY_DEFAULT] = "bios",
135 [ECRC_POLICY_OFF] = "off",
136 [ECRC_POLICY_ON] = "on"
137};
138
139/**
140 * enable_ercr_checking - enable PCIe ECRC checking for a device
141 * @dev: the PCI device
142 *
143 * Returns 0 on success, or negative on failure.
144 */
145static int enable_ecrc_checking(struct pci_dev *dev)
146{
147 int pos;
148 u32 reg32;
149
150 if (!pci_is_pcie(dev))
151 return -ENODEV;
152
153 pos = dev->aer_cap;
154 if (!pos)
155 return -ENODEV;
156
157 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
158 if (reg32 & PCI_ERR_CAP_ECRC_GENC)
159 reg32 |= PCI_ERR_CAP_ECRC_GENE;
160 if (reg32 & PCI_ERR_CAP_ECRC_CHKC)
161 reg32 |= PCI_ERR_CAP_ECRC_CHKE;
162 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
163
164 return 0;
165}
166
167/**
168 * disable_ercr_checking - disables PCIe ECRC checking for a device
169 * @dev: the PCI device
170 *
171 * Returns 0 on success, or negative on failure.
172 */
173static int disable_ecrc_checking(struct pci_dev *dev)
174{
175 int pos;
176 u32 reg32;
177
178 if (!pci_is_pcie(dev))
179 return -ENODEV;
180
181 pos = dev->aer_cap;
182 if (!pos)
183 return -ENODEV;
184
185 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
186 reg32 &= ~(PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
187 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
188
189 return 0;
190}
191
192/**
193 * pcie_set_ecrc_checking - set/unset PCIe ECRC checking for a device based on global policy
194 * @dev: the PCI device
195 */
196void pcie_set_ecrc_checking(struct pci_dev *dev)
197{
198 switch (ecrc_policy) {
199 case ECRC_POLICY_DEFAULT:
200 return;
201 case ECRC_POLICY_OFF:
202 disable_ecrc_checking(dev);
203 break;
204 case ECRC_POLICY_ON:
205 enable_ecrc_checking(dev);
206 break;
207 default:
208 return;
209 }
210}
211
212/**
213 * pcie_ecrc_get_policy - parse kernel command-line ecrc option
214 */
215void pcie_ecrc_get_policy(char *str)
216{
217 int i;
218
219 for (i = 0; i < ARRAY_SIZE(ecrc_policy_str); i++)
220 if (!strncmp(str, ecrc_policy_str[i],
221 strlen(ecrc_policy_str[i])))
222 break;
223 if (i >= ARRAY_SIZE(ecrc_policy_str))
224 return;
225
226 ecrc_policy = i;
227}
228#endif /* CONFIG_PCIE_ECRC */
229
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500230#ifdef CONFIG_ACPI_APEI
231static inline int hest_match_pci(struct acpi_hest_aer_common *p,
232 struct pci_dev *pci)
233{
234 return ACPI_HEST_SEGMENT(p->bus) == pci_domain_nr(pci->bus) &&
235 ACPI_HEST_BUS(p->bus) == pci->bus->number &&
236 p->device == PCI_SLOT(pci->devfn) &&
237 p->function == PCI_FUNC(pci->devfn);
238}
239
240static inline bool hest_match_type(struct acpi_hest_header *hest_hdr,
241 struct pci_dev *dev)
242{
243 u16 hest_type = hest_hdr->type;
244 u8 pcie_type = pci_pcie_type(dev);
245
246 if ((hest_type == ACPI_HEST_TYPE_AER_ROOT_PORT &&
247 pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
248 (hest_type == ACPI_HEST_TYPE_AER_ENDPOINT &&
249 pcie_type == PCI_EXP_TYPE_ENDPOINT) ||
250 (hest_type == ACPI_HEST_TYPE_AER_BRIDGE &&
251 (dev->class >> 16) == PCI_BASE_CLASS_BRIDGE))
252 return true;
253 return false;
254}
255
256struct aer_hest_parse_info {
257 struct pci_dev *pci_dev;
258 int firmware_first;
259};
260
261static int hest_source_is_pcie_aer(struct acpi_hest_header *hest_hdr)
262{
263 if (hest_hdr->type == ACPI_HEST_TYPE_AER_ROOT_PORT ||
264 hest_hdr->type == ACPI_HEST_TYPE_AER_ENDPOINT ||
265 hest_hdr->type == ACPI_HEST_TYPE_AER_BRIDGE)
266 return 1;
267 return 0;
268}
269
270static int aer_hest_parse(struct acpi_hest_header *hest_hdr, void *data)
271{
272 struct aer_hest_parse_info *info = data;
273 struct acpi_hest_aer_common *p;
274 int ff;
275
276 if (!hest_source_is_pcie_aer(hest_hdr))
277 return 0;
278
279 p = (struct acpi_hest_aer_common *)(hest_hdr + 1);
280 ff = !!(p->flags & ACPI_HEST_FIRMWARE_FIRST);
281
282 /*
283 * If no specific device is supplied, determine whether
284 * FIRMWARE_FIRST is set for *any* PCIe device.
285 */
286 if (!info->pci_dev) {
287 info->firmware_first |= ff;
288 return 0;
289 }
290
291 /* Otherwise, check the specific device */
292 if (p->flags & ACPI_HEST_GLOBAL) {
293 if (hest_match_type(hest_hdr, info->pci_dev))
294 info->firmware_first = ff;
295 } else
296 if (hest_match_pci(p, info->pci_dev))
297 info->firmware_first = ff;
298
299 return 0;
300}
301
302static void aer_set_firmware_first(struct pci_dev *pci_dev)
303{
304 int rc;
305 struct aer_hest_parse_info info = {
306 .pci_dev = pci_dev,
307 .firmware_first = 0,
308 };
309
310 rc = apei_hest_parse(aer_hest_parse, &info);
311
312 if (rc)
313 pci_dev->__aer_firmware_first = 0;
314 else
315 pci_dev->__aer_firmware_first = info.firmware_first;
316 pci_dev->__aer_firmware_first_valid = 1;
317}
318
319int pcie_aer_get_firmware_first(struct pci_dev *dev)
320{
321 if (!pci_is_pcie(dev))
322 return 0;
323
Alexandru Gagniuc7af02fc2018-07-03 18:27:43 -0500324 if (pcie_ports_native)
325 return 0;
326
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500327 if (!dev->__aer_firmware_first_valid)
328 aer_set_firmware_first(dev);
329 return dev->__aer_firmware_first;
330}
331
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
Alexandru Gagniuc7af02fc2018-07-03 18:27:43 -0500345 if (pcie_ports_native)
346 return false;
347
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500348 if (!parsed) {
349 apei_hest_parse(aer_hest_parse, &info);
350 aer_firmware_first = info.firmware_first;
351 parsed = true;
352 }
353 return aer_firmware_first;
354}
355#endif
356
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500357#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
358 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
359
360int pci_enable_pcie_error_reporting(struct pci_dev *dev)
361{
362 if (pcie_aer_get_firmware_first(dev))
363 return -EIO;
364
365 if (!dev->aer_cap)
366 return -EIO;
367
368 return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
369}
370EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
371
372int pci_disable_pcie_error_reporting(struct pci_dev *dev)
373{
374 if (pcie_aer_get_firmware_first(dev))
375 return -EIO;
376
377 return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
378 PCI_EXP_AER_FLAGS);
379}
380EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
381
Oza Pawandeepec752f52018-07-19 17:58:09 -0500382void pci_aer_clear_device_status(struct pci_dev *dev)
383{
384 u16 sta;
385
386 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &sta);
387 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, sta);
388}
389
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500390int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
391{
392 int pos;
Oza Pawandeepe7b0b842018-07-19 17:58:05 -0500393 u32 status, sev;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500394
395 pos = dev->aer_cap;
396 if (!pos)
397 return -EIO;
398
Alexandru Gagniuc45687f92018-07-17 10:31:23 -0500399 if (pcie_aer_get_firmware_first(dev))
400 return -EIO;
401
Oza Pawandeepe7b0b842018-07-19 17:58:05 -0500402 /* Clear status bits for ERR_NONFATAL errors only */
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500403 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
Oza Pawandeepe7b0b842018-07-19 17:58:05 -0500404 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &sev);
405 status &= ~sev;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500406 if (status)
407 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
408
409 return 0;
410}
411EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
412
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500413void pci_aer_clear_fatal_status(struct pci_dev *dev)
414{
415 int pos;
416 u32 status, sev;
417
418 pos = dev->aer_cap;
419 if (!pos)
420 return;
421
Alexandru Gagniuc45687f92018-07-17 10:31:23 -0500422 if (pcie_aer_get_firmware_first(dev))
423 return;
424
Bjorn Helgaas7ab92e82018-07-19 17:55:58 -0500425 /* Clear status bits for ERR_FATAL errors only */
426 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
427 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &sev);
428 status &= sev;
429 if (status)
430 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
431}
432
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500433int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
434{
435 int pos;
436 u32 status;
437 int port_type;
438
439 if (!pci_is_pcie(dev))
440 return -ENODEV;
441
442 pos = dev->aer_cap;
443 if (!pos)
444 return -EIO;
445
Alexandru Gagniuc45687f92018-07-17 10:31:23 -0500446 if (pcie_aer_get_firmware_first(dev))
447 return -EIO;
448
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500449 port_type = pci_pcie_type(dev);
450 if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
451 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
452 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
453 }
454
455 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
456 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
457
458 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
459 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
460
461 return 0;
462}
463
Rajat Jain60ed9822018-06-21 16:48:26 -0700464void pci_aer_init(struct pci_dev *dev)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500465{
466 dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500467
468 if (dev->aer_cap)
469 dev->aer_stats = kzalloc(sizeof(struct aer_stats), GFP_KERNEL);
470
Rajat Jain60ed9822018-06-21 16:48:26 -0700471 pci_cleanup_aer_error_status_regs(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500472}
473
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500474void pci_aer_exit(struct pci_dev *dev)
475{
476 kfree(dev->aer_stats);
477 dev->aer_stats = NULL;
478}
479
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500480#define AER_AGENT_RECEIVER 0
481#define AER_AGENT_REQUESTER 1
482#define AER_AGENT_COMPLETER 2
483#define AER_AGENT_TRANSMITTER 3
484
485#define AER_AGENT_REQUESTER_MASK(t) ((t == AER_CORRECTABLE) ? \
486 0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
487#define AER_AGENT_COMPLETER_MASK(t) ((t == AER_CORRECTABLE) ? \
488 0 : PCI_ERR_UNC_COMP_ABORT)
489#define AER_AGENT_TRANSMITTER_MASK(t) ((t == AER_CORRECTABLE) ? \
490 (PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
491
492#define AER_GET_AGENT(t, e) \
493 ((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER : \
494 (e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER : \
495 (e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER : \
496 AER_AGENT_RECEIVER)
497
498#define AER_PHYSICAL_LAYER_ERROR 0
499#define AER_DATA_LINK_LAYER_ERROR 1
500#define AER_TRANSACTION_LAYER_ERROR 2
501
502#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
503 PCI_ERR_COR_RCVR : 0)
504#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
505 (PCI_ERR_COR_BAD_TLP| \
506 PCI_ERR_COR_BAD_DLLP| \
507 PCI_ERR_COR_REP_ROLL| \
508 PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
509
510#define AER_GET_LAYER_ERROR(t, e) \
511 ((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
512 (e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
513 AER_TRANSACTION_LAYER_ERROR)
514
515/*
516 * AER error strings
517 */
518static const char *aer_error_severity_string[] = {
519 "Uncorrected (Non-Fatal)",
520 "Uncorrected (Fatal)",
521 "Corrected"
522};
523
524static const char *aer_error_layer[] = {
525 "Physical Layer",
526 "Data Link Layer",
527 "Transaction Layer"
528};
529
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500530static const char *aer_correctable_error_string[AER_MAX_TYPEOF_COR_ERRS] = {
Tyler Baicarbd237802018-06-26 11:44:15 -0400531 "RxErr", /* Bit Position 0 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500532 NULL,
533 NULL,
534 NULL,
535 NULL,
536 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400537 "BadTLP", /* Bit Position 6 */
538 "BadDLLP", /* Bit Position 7 */
539 "Rollover", /* Bit Position 8 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500540 NULL,
541 NULL,
542 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400543 "Timeout", /* Bit Position 12 */
544 "NonFatalErr", /* Bit Position 13 */
545 "CorrIntErr", /* Bit Position 14 */
546 "HeaderOF", /* Bit Position 15 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500547};
548
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500549static const char *aer_uncorrectable_error_string[AER_MAX_TYPEOF_UNCOR_ERRS] = {
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500550 "Undefined", /* Bit Position 0 */
551 NULL,
552 NULL,
553 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400554 "DLP", /* Bit Position 4 */
555 "SDES", /* Bit Position 5 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500556 NULL,
557 NULL,
558 NULL,
559 NULL,
560 NULL,
561 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400562 "TLP", /* Bit Position 12 */
563 "FCP", /* Bit Position 13 */
564 "CmpltTO", /* Bit Position 14 */
565 "CmpltAbrt", /* Bit Position 15 */
566 "UnxCmplt", /* Bit Position 16 */
567 "RxOF", /* Bit Position 17 */
568 "MalfTLP", /* Bit Position 18 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500569 "ECRC", /* Bit Position 19 */
Tyler Baicarbd237802018-06-26 11:44:15 -0400570 "UnsupReq", /* Bit Position 20 */
571 "ACSViol", /* Bit Position 21 */
572 "UncorrIntErr", /* Bit Position 22 */
573 "BlockedTLP", /* Bit Position 23 */
574 "AtomicOpBlocked", /* Bit Position 24 */
575 "TLPBlockedErr", /* Bit Position 25 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500576};
577
578static const char *aer_agent_string[] = {
579 "Receiver ID",
580 "Requester ID",
581 "Completer ID",
582 "Transmitter ID"
583};
584
Rajat Jain81aa5202018-06-21 16:48:28 -0700585#define aer_stats_dev_attr(name, stats_array, strings_array, \
586 total_string, total_field) \
587 static ssize_t \
588 name##_show(struct device *dev, struct device_attribute *attr, \
589 char *buf) \
590{ \
591 unsigned int i; \
592 char *str = buf; \
593 struct pci_dev *pdev = to_pci_dev(dev); \
594 u64 *stats = pdev->aer_stats->stats_array; \
595 \
596 for (i = 0; i < ARRAY_SIZE(strings_array); i++) { \
597 if (strings_array[i]) \
598 str += sprintf(str, "%s %llu\n", \
599 strings_array[i], stats[i]); \
600 else if (stats[i]) \
601 str += sprintf(str, #stats_array "_bit[%d] %llu\n",\
602 i, stats[i]); \
603 } \
604 str += sprintf(str, "TOTAL_%s %llu\n", total_string, \
605 pdev->aer_stats->total_field); \
606 return str-buf; \
607} \
608static DEVICE_ATTR_RO(name)
609
610aer_stats_dev_attr(aer_dev_correctable, dev_cor_errs,
611 aer_correctable_error_string, "ERR_COR",
612 dev_total_cor_errs);
613aer_stats_dev_attr(aer_dev_fatal, dev_fatal_errs,
614 aer_uncorrectable_error_string, "ERR_FATAL",
615 dev_total_fatal_errs);
616aer_stats_dev_attr(aer_dev_nonfatal, dev_nonfatal_errs,
617 aer_uncorrectable_error_string, "ERR_NONFATAL",
618 dev_total_nonfatal_errs);
619
Rajat Jain12833012018-06-21 16:48:29 -0700620#define aer_stats_rootport_attr(name, field) \
621 static ssize_t \
622 name##_show(struct device *dev, struct device_attribute *attr, \
623 char *buf) \
624{ \
625 struct pci_dev *pdev = to_pci_dev(dev); \
626 return sprintf(buf, "%llu\n", pdev->aer_stats->field); \
627} \
628static DEVICE_ATTR_RO(name)
629
630aer_stats_rootport_attr(aer_rootport_total_err_cor,
631 rootport_total_cor_errs);
632aer_stats_rootport_attr(aer_rootport_total_err_fatal,
633 rootport_total_fatal_errs);
634aer_stats_rootport_attr(aer_rootport_total_err_nonfatal,
635 rootport_total_nonfatal_errs);
636
Rajat Jain81aa5202018-06-21 16:48:28 -0700637static struct attribute *aer_stats_attrs[] __ro_after_init = {
638 &dev_attr_aer_dev_correctable.attr,
639 &dev_attr_aer_dev_fatal.attr,
640 &dev_attr_aer_dev_nonfatal.attr,
Rajat Jain12833012018-06-21 16:48:29 -0700641 &dev_attr_aer_rootport_total_err_cor.attr,
642 &dev_attr_aer_rootport_total_err_fatal.attr,
643 &dev_attr_aer_rootport_total_err_nonfatal.attr,
Rajat Jain81aa5202018-06-21 16:48:28 -0700644 NULL
645};
646
647static umode_t aer_stats_attrs_are_visible(struct kobject *kobj,
648 struct attribute *a, int n)
649{
650 struct device *dev = kobj_to_dev(kobj);
651 struct pci_dev *pdev = to_pci_dev(dev);
652
653 if (!pdev->aer_stats)
654 return 0;
655
Rajat Jain12833012018-06-21 16:48:29 -0700656 if ((a == &dev_attr_aer_rootport_total_err_cor.attr ||
657 a == &dev_attr_aer_rootport_total_err_fatal.attr ||
658 a == &dev_attr_aer_rootport_total_err_nonfatal.attr) &&
659 pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT)
660 return 0;
661
Rajat Jain81aa5202018-06-21 16:48:28 -0700662 return a->mode;
663}
664
665const struct attribute_group aer_stats_attr_group = {
666 .attrs = aer_stats_attrs,
667 .is_visible = aer_stats_attrs_are_visible,
668};
669
670static void pci_dev_aer_stats_incr(struct pci_dev *pdev,
671 struct aer_err_info *info)
672{
673 int status, i, max = -1;
674 u64 *counter = NULL;
675 struct aer_stats *aer_stats = pdev->aer_stats;
676
677 if (!aer_stats)
678 return;
679
680 switch (info->severity) {
681 case AER_CORRECTABLE:
682 aer_stats->dev_total_cor_errs++;
683 counter = &aer_stats->dev_cor_errs[0];
684 max = AER_MAX_TYPEOF_COR_ERRS;
685 break;
686 case AER_NONFATAL:
687 aer_stats->dev_total_nonfatal_errs++;
688 counter = &aer_stats->dev_nonfatal_errs[0];
689 max = AER_MAX_TYPEOF_UNCOR_ERRS;
690 break;
691 case AER_FATAL:
692 aer_stats->dev_total_fatal_errs++;
693 counter = &aer_stats->dev_fatal_errs[0];
694 max = AER_MAX_TYPEOF_UNCOR_ERRS;
695 break;
696 }
697
698 status = (info->status & ~info->mask);
699 for (i = 0; i < max; i++)
700 if (status & (1 << i))
701 counter[i]++;
702}
703
Rajat Jain12833012018-06-21 16:48:29 -0700704static void pci_rootport_aer_stats_incr(struct pci_dev *pdev,
705 struct aer_err_source *e_src)
706{
707 struct aer_stats *aer_stats = pdev->aer_stats;
708
709 if (!aer_stats)
710 return;
711
712 if (e_src->status & PCI_ERR_ROOT_COR_RCV)
713 aer_stats->rootport_total_cor_errs++;
714
715 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
716 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
717 aer_stats->rootport_total_fatal_errs++;
718 else
719 aer_stats->rootport_total_nonfatal_errs++;
720 }
721}
722
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500723static void __print_tlp_header(struct pci_dev *dev,
724 struct aer_header_log_regs *t)
725{
726 pci_err(dev, " TLP Header: %08x %08x %08x %08x\n",
727 t->dw0, t->dw1, t->dw2, t->dw3);
728}
729
730static void __aer_print_error(struct pci_dev *dev,
731 struct aer_err_info *info)
732{
733 int i, status;
734 const char *errmsg = NULL;
735 status = (info->status & ~info->mask);
736
737 for (i = 0; i < 32; i++) {
738 if (!(status & (1 << i)))
739 continue;
740
741 if (info->severity == AER_CORRECTABLE)
742 errmsg = i < ARRAY_SIZE(aer_correctable_error_string) ?
743 aer_correctable_error_string[i] : NULL;
744 else
745 errmsg = i < ARRAY_SIZE(aer_uncorrectable_error_string) ?
746 aer_uncorrectable_error_string[i] : NULL;
747
748 if (errmsg)
749 pci_err(dev, " [%2d] %-22s%s\n", i, errmsg,
750 info->first_error == i ? " (First)" : "");
751 else
752 pci_err(dev, " [%2d] Unknown Error Bit%s\n",
753 i, info->first_error == i ? " (First)" : "");
754 }
Rajat Jain81aa5202018-06-21 16:48:28 -0700755 pci_dev_aer_stats_incr(dev, info);
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500756}
757
Keith Busch1e451162018-07-19 16:16:55 -0500758void aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500759{
760 int layer, agent;
761 int id = ((dev->bus->number << 8) | dev->devfn);
762
763 if (!info->status) {
764 pci_err(dev, "PCIe Bus Error: severity=%s, type=Inaccessible, (Unregistered Agent ID)\n",
765 aer_error_severity_string[info->severity]);
766 goto out;
767 }
768
769 layer = AER_GET_LAYER_ERROR(info->severity, info->status);
770 agent = AER_GET_AGENT(info->severity, info->status);
771
772 pci_err(dev, "PCIe Bus Error: severity=%s, type=%s, (%s)\n",
773 aer_error_severity_string[info->severity],
774 aer_error_layer[layer], aer_agent_string[agent]);
775
776 pci_err(dev, " device [%04x:%04x] error status/mask=%08x/%08x\n",
777 dev->vendor, dev->device,
778 info->status, info->mask);
779
780 __aer_print_error(dev, info);
781
782 if (info->tlp_header_valid)
783 __print_tlp_header(dev, &info->tlp);
784
785out:
786 if (info->id && info->error_dev_num > 1 && info->id == id)
787 pci_err(dev, " Error of this Agent is reported first\n");
788
789 trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
790 info->severity, info->tlp_header_valid, &info->tlp);
791}
792
793static void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
794{
795 u8 bus = info->id >> 8;
796 u8 devfn = info->id & 0xff;
797
798 pci_info(dev, "AER: %s%s error received: %04x:%02x:%02x.%d\n",
799 info->multi_error_valid ? "Multiple " : "",
800 aer_error_severity_string[info->severity],
801 pci_domain_nr(dev->bus), bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
802}
803
804#ifdef CONFIG_ACPI_APEI_PCIEAER
805int cper_severity_to_aer(int cper_severity)
806{
807 switch (cper_severity) {
808 case CPER_SEV_RECOVERABLE:
809 return AER_NONFATAL;
810 case CPER_SEV_FATAL:
811 return AER_FATAL;
812 default:
813 return AER_CORRECTABLE;
814 }
815}
816EXPORT_SYMBOL_GPL(cper_severity_to_aer);
817
818void cper_print_aer(struct pci_dev *dev, int aer_severity,
819 struct aer_capability_regs *aer)
820{
821 int layer, agent, tlp_header_valid = 0;
822 u32 status, mask;
823 struct aer_err_info info;
824
825 if (aer_severity == AER_CORRECTABLE) {
826 status = aer->cor_status;
827 mask = aer->cor_mask;
828 } else {
829 status = aer->uncor_status;
830 mask = aer->uncor_mask;
831 tlp_header_valid = status & AER_LOG_TLP_MASKS;
832 }
833
834 layer = AER_GET_LAYER_ERROR(aer_severity, status);
835 agent = AER_GET_AGENT(aer_severity, status);
836
837 memset(&info, 0, sizeof(info));
838 info.severity = aer_severity;
839 info.status = status;
840 info.mask = mask;
841 info.first_error = PCI_ERR_CAP_FEP(aer->cap_control);
842
843 pci_err(dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
844 __aer_print_error(dev, &info);
845 pci_err(dev, "aer_layer=%s, aer_agent=%s\n",
846 aer_error_layer[layer], aer_agent_string[agent]);
847
848 if (aer_severity != AER_CORRECTABLE)
849 pci_err(dev, "aer_uncor_severity: 0x%08x\n",
850 aer->uncor_severity);
851
852 if (tlp_header_valid)
853 __print_tlp_header(dev, &aer->header_log);
854
855 trace_aer_event(dev_name(&dev->dev), (status & ~mask),
856 aer_severity, tlp_header_valid, &aer->header_log);
857}
858#endif
859
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500860/**
861 * add_error_device - list device to be handled
862 * @e_info: pointer to error info
863 * @dev: pointer to pci_dev to be added
864 */
865static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
866{
867 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
Keith Busch60271ab02018-09-20 10:27:09 -0600868 e_info->dev[e_info->error_dev_num] = pci_dev_get(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500869 e_info->error_dev_num++;
870 return 0;
871 }
872 return -ENOSPC;
873}
874
875/**
876 * is_error_source - check whether the device is source of reported error
877 * @dev: pointer to pci_dev to be checked
878 * @e_info: pointer to reported error info
879 */
880static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
881{
882 int pos;
883 u32 status, mask;
884 u16 reg16;
885
886 /*
887 * When bus id is equal to 0, it might be a bad id
888 * reported by root port.
889 */
890 if ((PCI_BUS_NUM(e_info->id) != 0) &&
891 !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
892 /* Device ID match? */
893 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
894 return true;
895
896 /* Continue id comparing if there is no multiple error */
897 if (!e_info->multi_error_valid)
898 return false;
899 }
900
901 /*
902 * When either
903 * 1) bus id is equal to 0. Some ports might lose the bus
904 * id of error source id;
905 * 2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
906 * 3) There are multiple errors and prior ID comparing fails;
907 * We check AER status registers to find possible reporter.
908 */
909 if (atomic_read(&dev->enable_cnt) == 0)
910 return false;
911
912 /* Check if AER is enabled */
913 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
914 if (!(reg16 & PCI_EXP_AER_FLAGS))
915 return false;
916
917 pos = dev->aer_cap;
918 if (!pos)
919 return false;
920
921 /* Check if error is recorded */
922 if (e_info->severity == AER_CORRECTABLE) {
923 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
924 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
925 } else {
926 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
927 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
928 }
929 if (status & ~mask)
930 return true;
931
932 return false;
933}
934
935static int find_device_iter(struct pci_dev *dev, void *data)
936{
937 struct aer_err_info *e_info = (struct aer_err_info *)data;
938
939 if (is_error_source(dev, e_info)) {
940 /* List this device */
941 if (add_error_device(e_info, dev)) {
942 /* We cannot handle more... Stop iteration */
943 /* TODO: Should print error message here? */
944 return 1;
945 }
946
947 /* If there is only a single error, stop iteration */
948 if (!e_info->multi_error_valid)
949 return 1;
950 }
951 return 0;
952}
953
954/**
955 * find_source_device - search through device hierarchy for source device
956 * @parent: pointer to Root Port pci_dev data structure
957 * @e_info: including detailed error information such like id
958 *
959 * Return true if found.
960 *
961 * Invoked by DPC when error is detected at the Root Port.
962 * Caller of this function must set id, severity, and multi_error_valid of
963 * struct aer_err_info pointed by @e_info properly. This function must fill
964 * e_info->error_dev_num and e_info->dev[], based on the given information.
965 */
966static bool find_source_device(struct pci_dev *parent,
967 struct aer_err_info *e_info)
968{
969 struct pci_dev *dev = parent;
970 int result;
971
972 /* Must reset in this function */
973 e_info->error_dev_num = 0;
974
975 /* Is Root Port an agent that sends error message? */
976 result = find_device_iter(dev, e_info);
977 if (result)
978 return true;
979
980 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
981
982 if (!e_info->error_dev_num) {
983 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
984 e_info->id);
985 return false;
986 }
987 return true;
988}
989
990/**
991 * handle_error_source - handle logging error into an event log
992 * @dev: pointer to pci_dev data structure of error source device
993 * @info: comprehensive error information
994 *
995 * Invoked when an error being detected by Root Port.
996 */
997static void handle_error_source(struct pci_dev *dev, struct aer_err_info *info)
998{
999 int pos;
1000
1001 if (info->severity == AER_CORRECTABLE) {
1002 /*
1003 * Correctable error does not need software intervention.
1004 * No need to go through error recovery process.
1005 */
1006 pos = dev->aer_cap;
1007 if (pos)
1008 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
1009 info->status);
Oza Pawandeep10d790d92018-07-19 17:58:09 -05001010 pci_aer_clear_device_status(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001011 } else if (info->severity == AER_NONFATAL)
Keith Buschbdb5ac852018-09-20 10:27:12 -06001012 pcie_do_recovery(dev, pci_channel_io_normal,
1013 PCIE_PORT_SERVICE_AER);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001014 else if (info->severity == AER_FATAL)
Keith Buschbdb5ac852018-09-20 10:27:12 -06001015 pcie_do_recovery(dev, pci_channel_io_frozen,
1016 PCIE_PORT_SERVICE_AER);
Keith Busch60271ab02018-09-20 10:27:09 -06001017 pci_dev_put(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001018}
1019
1020#ifdef CONFIG_ACPI_APEI_PCIEAER
1021
1022#define AER_RECOVER_RING_ORDER 4
1023#define AER_RECOVER_RING_SIZE (1 << AER_RECOVER_RING_ORDER)
1024
1025struct aer_recover_entry {
1026 u8 bus;
1027 u8 devfn;
1028 u16 domain;
1029 int severity;
1030 struct aer_capability_regs *regs;
1031};
1032
1033static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
1034 AER_RECOVER_RING_SIZE);
1035
1036static void aer_recover_work_func(struct work_struct *work)
1037{
1038 struct aer_recover_entry entry;
1039 struct pci_dev *pdev;
1040
1041 while (kfifo_get(&aer_recover_ring, &entry)) {
1042 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
1043 entry.devfn);
1044 if (!pdev) {
1045 pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
1046 entry.domain, entry.bus,
1047 PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
1048 continue;
1049 }
1050 cper_print_aer(pdev, entry.severity, entry.regs);
1051 if (entry.severity == AER_NONFATAL)
Keith Buschbdb5ac852018-09-20 10:27:12 -06001052 pcie_do_recovery(pdev, pci_channel_io_normal,
1053 PCIE_PORT_SERVICE_AER);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001054 else if (entry.severity == AER_FATAL)
Keith Buschbdb5ac852018-09-20 10:27:12 -06001055 pcie_do_recovery(pdev, pci_channel_io_frozen,
1056 PCIE_PORT_SERVICE_AER);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001057 pci_dev_put(pdev);
1058 }
1059}
1060
1061/*
1062 * Mutual exclusion for writers of aer_recover_ring, reader side don't
1063 * need lock, because there is only one reader and lock is not needed
1064 * between reader and writer.
1065 */
1066static DEFINE_SPINLOCK(aer_recover_ring_lock);
1067static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
1068
1069void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
1070 int severity, struct aer_capability_regs *aer_regs)
1071{
1072 unsigned long flags;
1073 struct aer_recover_entry entry = {
1074 .bus = bus,
1075 .devfn = devfn,
1076 .domain = domain,
1077 .severity = severity,
1078 .regs = aer_regs,
1079 };
1080
1081 spin_lock_irqsave(&aer_recover_ring_lock, flags);
1082 if (kfifo_put(&aer_recover_ring, entry))
1083 schedule_work(&aer_recover_work);
1084 else
1085 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
1086 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1087 spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
1088}
1089EXPORT_SYMBOL_GPL(aer_recover_queue);
1090#endif
1091
1092/**
Keith Busch1e451162018-07-19 16:16:55 -05001093 * aer_get_device_error_info - read error status from dev and store it to info
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001094 * @dev: pointer to the device expected to have a error record
1095 * @info: pointer to structure to store the error record
1096 *
1097 * Return 1 on success, 0 on error.
1098 *
1099 * Note that @info is reused among all error devices. Clear fields properly.
1100 */
Keith Busch1e451162018-07-19 16:16:55 -05001101int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001102{
1103 int pos, temp;
1104
1105 /* Must reset in this function */
1106 info->status = 0;
1107 info->tlp_header_valid = 0;
1108
1109 pos = dev->aer_cap;
1110
1111 /* The device might not support AER */
1112 if (!pos)
1113 return 0;
1114
1115 if (info->severity == AER_CORRECTABLE) {
1116 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
1117 &info->status);
1118 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
1119 &info->mask);
1120 if (!(info->status & ~info->mask))
1121 return 0;
Keith Busch9d938ea2018-09-20 10:27:10 -06001122 } else if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
1123 pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
1124 info->severity == AER_NONFATAL) {
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001125
1126 /* Link is still healthy for IO reads */
1127 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
1128 &info->status);
1129 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
1130 &info->mask);
1131 if (!(info->status & ~info->mask))
1132 return 0;
1133
1134 /* Get First Error Pointer */
1135 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
1136 info->first_error = PCI_ERR_CAP_FEP(temp);
1137
1138 if (info->status & AER_LOG_TLP_MASKS) {
1139 info->tlp_header_valid = 1;
1140 pci_read_config_dword(dev,
1141 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
1142 pci_read_config_dword(dev,
1143 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
1144 pci_read_config_dword(dev,
1145 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
1146 pci_read_config_dword(dev,
1147 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
1148 }
1149 }
1150
1151 return 1;
1152}
1153
1154static inline void aer_process_err_devices(struct aer_err_info *e_info)
1155{
1156 int i;
1157
1158 /* Report all before handle them, not to lost records by reset etc. */
1159 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001160 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001161 aer_print_error(e_info->dev[i], e_info);
1162 }
1163 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001164 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001165 handle_error_source(e_info->dev[i], e_info);
1166 }
1167}
1168
1169/**
1170 * aer_isr_one_error - consume an error detected by root port
1171 * @rpc: pointer to the root port which holds an error
1172 * @e_src: pointer to an error source
1173 */
1174static void aer_isr_one_error(struct aer_rpc *rpc,
1175 struct aer_err_source *e_src)
1176{
1177 struct pci_dev *pdev = rpc->rpd;
Keith Buschfcd4d362018-09-18 17:58:42 -06001178 struct aer_err_info e_info;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001179
Rajat Jain12833012018-06-21 16:48:29 -07001180 pci_rootport_aer_stats_incr(pdev, e_src);
1181
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001182 /*
1183 * There is a possibility that both correctable error and
1184 * uncorrectable error being logged. Report correctable error first.
1185 */
1186 if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
Keith Buschfcd4d362018-09-18 17:58:42 -06001187 e_info.id = ERR_COR_ID(e_src->id);
1188 e_info.severity = AER_CORRECTABLE;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001189
1190 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
Keith Buschfcd4d362018-09-18 17:58:42 -06001191 e_info.multi_error_valid = 1;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001192 else
Keith Buschfcd4d362018-09-18 17:58:42 -06001193 e_info.multi_error_valid = 0;
1194 aer_print_port_info(pdev, &e_info);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001195
Keith Buschfcd4d362018-09-18 17:58:42 -06001196 if (find_source_device(pdev, &e_info))
1197 aer_process_err_devices(&e_info);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001198 }
1199
1200 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
Keith Buschfcd4d362018-09-18 17:58:42 -06001201 e_info.id = ERR_UNCOR_ID(e_src->id);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001202
1203 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
Keith Buschfcd4d362018-09-18 17:58:42 -06001204 e_info.severity = AER_FATAL;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001205 else
Keith Buschfcd4d362018-09-18 17:58:42 -06001206 e_info.severity = AER_NONFATAL;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001207
1208 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
Keith Buschfcd4d362018-09-18 17:58:42 -06001209 e_info.multi_error_valid = 1;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001210 else
Keith Buschfcd4d362018-09-18 17:58:42 -06001211 e_info.multi_error_valid = 0;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001212
Keith Buschfcd4d362018-09-18 17:58:42 -06001213 aer_print_port_info(pdev, &e_info);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001214
Keith Buschfcd4d362018-09-18 17:58:42 -06001215 if (find_source_device(pdev, &e_info))
1216 aer_process_err_devices(&e_info);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001217 }
1218}
1219
1220/**
1221 * get_e_source - retrieve an error source
1222 * @rpc: pointer to the root port which holds an error
1223 * @e_src: pointer to store retrieved error source
1224 *
1225 * Return 1 if an error source is retrieved, otherwise 0.
1226 *
1227 * Invoked by DPC handler to consume an error.
1228 */
1229static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
1230{
1231 unsigned long flags;
1232
1233 /* Lock access to Root error producer/consumer index */
1234 spin_lock_irqsave(&rpc->e_lock, flags);
1235 if (rpc->prod_idx == rpc->cons_idx) {
1236 spin_unlock_irqrestore(&rpc->e_lock, flags);
1237 return 0;
1238 }
1239
1240 *e_src = rpc->e_sources[rpc->cons_idx];
1241 rpc->cons_idx++;
1242 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
1243 rpc->cons_idx = 0;
1244 spin_unlock_irqrestore(&rpc->e_lock, flags);
1245
1246 return 1;
1247}
1248
1249/**
1250 * aer_isr - consume errors detected by root port
1251 * @work: definition of this work item
1252 *
1253 * Invoked, as DPC, when root port records new detected error
1254 */
1255static void aer_isr(struct work_struct *work)
1256{
1257 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
1258 struct aer_err_source uninitialized_var(e_src);
1259
1260 mutex_lock(&rpc->rpc_mutex);
1261 while (get_e_source(rpc, &e_src))
1262 aer_isr_one_error(rpc, &e_src);
1263 mutex_unlock(&rpc->rpc_mutex);
1264}
1265
Bjorn Helgaas3c43a642018-06-08 08:31:57 -05001266/**
1267 * aer_irq - Root Port's ISR
1268 * @irq: IRQ assigned to Root Port
1269 * @context: pointer to Root Port data structure
1270 *
1271 * Invoked when Root Port detects AER messages.
1272 */
1273irqreturn_t aer_irq(int irq, void *context)
1274{
1275 unsigned int status, id;
1276 struct pcie_device *pdev = (struct pcie_device *)context;
1277 struct aer_rpc *rpc = get_service_data(pdev);
1278 int next_prod_idx;
1279 unsigned long flags;
1280 int pos;
1281
1282 pos = pdev->port->aer_cap;
1283 /*
1284 * Must lock access to Root Error Status Reg, Root Error ID Reg,
1285 * and Root error producer/consumer index
1286 */
1287 spin_lock_irqsave(&rpc->e_lock, flags);
1288
1289 /* Read error status */
1290 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
1291 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
1292 spin_unlock_irqrestore(&rpc->e_lock, flags);
1293 return IRQ_NONE;
1294 }
1295
1296 /* Read error source and clear error status */
1297 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
1298 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
1299
1300 /* Store error source for later DPC handler */
1301 next_prod_idx = rpc->prod_idx + 1;
1302 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
1303 next_prod_idx = 0;
1304 if (next_prod_idx == rpc->cons_idx) {
1305 /*
1306 * Error Storm Condition - possibly the same error occurred.
1307 * Drop the error.
1308 */
1309 spin_unlock_irqrestore(&rpc->e_lock, flags);
1310 return IRQ_HANDLED;
1311 }
1312 rpc->e_sources[rpc->prod_idx].status = status;
1313 rpc->e_sources[rpc->prod_idx].id = id;
1314 rpc->prod_idx = next_prod_idx;
1315 spin_unlock_irqrestore(&rpc->e_lock, flags);
1316
1317 /* Invoke DPC handler */
1318 schedule_work(&rpc->dpc_handler);
1319
1320 return IRQ_HANDLED;
1321}
1322EXPORT_SYMBOL_GPL(aer_irq);
1323
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001324static int set_device_error_reporting(struct pci_dev *dev, void *data)
1325{
1326 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +08001327 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001328
Yijing Wang62f87c02012-07-24 17:20:03 +08001329 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
1330 (type == PCI_EXP_TYPE_UPSTREAM) ||
1331 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001332 if (enable)
1333 pci_enable_pcie_error_reporting(dev);
1334 else
1335 pci_disable_pcie_error_reporting(dev);
1336 }
1337
1338 if (enable)
1339 pcie_set_ecrc_checking(dev);
1340
1341 return 0;
1342}
1343
1344/**
1345 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
1346 * @dev: pointer to root port's pci_dev data structure
1347 * @enable: true = enable error reporting, false = disable error reporting.
1348 */
1349static void set_downstream_devices_error_reporting(struct pci_dev *dev,
1350 bool enable)
1351{
1352 set_device_error_reporting(dev, &enable);
1353
1354 if (!dev->subordinate)
1355 return;
1356 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
1357}
1358
1359/**
1360 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
1361 * @rpc: pointer to a Root Port data structure
1362 *
1363 * Invoked when PCIe bus loads AER service driver.
1364 */
1365static void aer_enable_rootport(struct aer_rpc *rpc)
1366{
Keith Busche13d17f2018-04-09 16:04:42 -06001367 struct pci_dev *pdev = rpc->rpd;
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001368 int aer_pos;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001369 u16 reg16;
1370 u32 reg32;
1371
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001372 /* Clear PCIe Capability's Device Status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001373 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
1374 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001375
1376 /* Disable system error generation in response to error messages */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001377 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
1378 SYSTEM_ERROR_INTR_ON_MESG_MASK);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001379
Keith Busch66b80802016-09-27 16:23:34 -04001380 aer_pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001381 /* Clear error status */
1382 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
1383 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
1384 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
1385 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
1386 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
1387 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
1388
1389 /*
1390 * Enable error reporting for the root port device and downstream port
1391 * devices.
1392 */
1393 set_downstream_devices_error_reporting(pdev, true);
1394
1395 /* Enable Root Port's interrupt in response to error messages */
1396 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
1397 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1398 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
1399}
1400
1401/**
1402 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
1403 * @rpc: pointer to a Root Port data structure
1404 *
1405 * Invoked when PCIe bus unloads AER service driver.
1406 */
1407static void aer_disable_rootport(struct aer_rpc *rpc)
1408{
Keith Busche13d17f2018-04-09 16:04:42 -06001409 struct pci_dev *pdev = rpc->rpd;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001410 u32 reg32;
1411 int pos;
1412
1413 /*
1414 * Disable error reporting for the root port device and downstream port
1415 * devices.
1416 */
1417 set_downstream_devices_error_reporting(pdev, false);
1418
Keith Busch66b80802016-09-27 16:23:34 -04001419 pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001420 /* Disable Root's interrupt in response to error messages */
1421 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1422 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1423 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
1424
1425 /* Clear Root's error status reg */
1426 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1427 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
1428}
1429
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001430/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001431 * aer_alloc_rpc - allocate Root Port data structure
1432 * @dev: pointer to the pcie_dev data structure
1433 *
1434 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001435 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001436static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001437{
1438 struct aer_rpc *rpc;
1439
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001440 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
1441 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001442 return NULL;
1443
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001444 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -07001445 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001446
Keith Busche13d17f2018-04-09 16:04:42 -06001447 rpc->rpd = dev->port;
David Howells65f27f32006-11-22 14:55:48 +00001448 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001449 mutex_init(&rpc->rpc_mutex);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001450
Stefan Assmann45e829e2009-12-03 06:49:24 -05001451 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001452 set_service_data(dev, rpc);
1453
1454 return rpc;
1455}
1456
1457/**
1458 * aer_remove - clean up resources
1459 * @dev: pointer to the pcie_dev data structure
1460 *
1461 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001462 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001463static void aer_remove(struct pcie_device *dev)
1464{
1465 struct aer_rpc *rpc = get_service_data(dev);
1466
1467 if (rpc) {
1468 /* If register interrupt service, it must be free. */
1469 if (rpc->isr)
1470 free_irq(dev->irq, dev);
1471
Sebastian Andrzej Siewior4ae21822016-01-25 10:08:00 -06001472 flush_work(&rpc->dpc_handler);
Hidetoshi Seto460d2982010-04-15 13:10:03 +09001473 aer_disable_rootport(rpc);
1474 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001475 set_service_data(dev, NULL);
1476 }
1477}
1478
1479/**
1480 * aer_probe - initialize resources
1481 * @dev: pointer to the pcie_dev data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001482 *
1483 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001484 */
Bill Pemberton15856ad2012-11-21 15:35:00 -05001485static int aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001486{
1487 int status;
1488 struct aer_rpc *rpc;
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001489 struct device *device = &dev->port->dev;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001490
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001491 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001492 rpc = aer_alloc_rpc(dev);
1493 if (!rpc) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001494 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001495 aer_remove(dev);
1496 return -ENOMEM;
1497 }
1498
1499 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001500 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
1501 if (status) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001502 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
1503 dev->irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001504 aer_remove(dev);
1505 return status;
1506 }
1507
1508 rpc->isr = 1;
1509
1510 aer_enable_rootport(rpc);
Bjorn Helgaas68a55ae2016-11-21 15:34:02 -06001511 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
1512 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001513}
1514
1515/**
1516 * aer_root_reset - reset link on Root Port
1517 * @dev: pointer to Root Port's pci_dev data structure
1518 *
1519 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001520 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001521static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
1522{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001523 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001524 int pos;
Sinan Kaya18426232018-07-19 18:04:09 -05001525 int rc;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001526
Keith Busch66b80802016-09-27 16:23:34 -04001527 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001528
1529 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001530 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1531 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1532 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001533
Keith Buschc4eed622018-09-20 10:27:11 -06001534 rc = pci_bus_error_reset(dev);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001535 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001536
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001537 /* Clear Root Error Status */
1538 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1539 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
1540
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001541 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001542 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1543 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1544 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001545
Sinan Kaya18426232018-07-19 18:04:09 -05001546 return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001547}
1548
Bjorn Helgaas0054ca82018-06-08 08:31:42 -05001549static struct pcie_port_service_driver aerdriver = {
1550 .name = "aer",
1551 .port_type = PCI_EXP_TYPE_ROOT_PORT,
1552 .service = PCIE_PORT_SERVICE_AER,
1553
1554 .probe = aer_probe,
1555 .remove = aer_remove,
Bjorn Helgaas0054ca82018-06-08 08:31:42 -05001556 .reset_link = aer_root_reset,
1557};
1558
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001559/**
1560 * aer_service_init - register AER root service driver
1561 *
1562 * Invoked when AER root service driver is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001563 */
Keith Buschc29de842018-09-20 10:27:06 -06001564int __init pcie_aer_init(void)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001565{
Rafael J. Wysockib22c3d82010-09-20 18:50:00 +02001566 if (!pci_aer_available() || aer_acpi_firmware_first())
Andi Kleen3e77a3f2009-09-16 22:40:22 +02001567 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +01001568 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001569}