blob: 2b344c9e2d46c8aac9615e8cb1f5210e8c28876e [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
Alexandru Gagniuc7af02fc2018-07-03 18:27:43 -0500325 if (pcie_ports_native)
326 return 0;
327
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500328 if (!dev->__aer_firmware_first_valid)
329 aer_set_firmware_first(dev);
330 return dev->__aer_firmware_first;
331}
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -0500332#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
333 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500334
335static bool aer_firmware_first;
336
337/**
338 * aer_acpi_firmware_first - Check if APEI should control AER.
339 */
340bool aer_acpi_firmware_first(void)
341{
342 static bool parsed = false;
343 struct aer_hest_parse_info info = {
344 .pci_dev = NULL, /* Check all PCIe devices */
345 .firmware_first = 0,
346 };
347
Alexandru Gagniuc7af02fc2018-07-03 18:27:43 -0500348 if (pcie_ports_native)
349 return false;
350
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500351 if (!parsed) {
352 apei_hest_parse(aer_hest_parse, &info);
353 aer_firmware_first = info.firmware_first;
354 parsed = true;
355 }
356 return aer_firmware_first;
357}
358#endif
359
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500360#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
361 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
362
363int pci_enable_pcie_error_reporting(struct pci_dev *dev)
364{
365 if (pcie_aer_get_firmware_first(dev))
366 return -EIO;
367
368 if (!dev->aer_cap)
369 return -EIO;
370
371 return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
372}
373EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
374
375int pci_disable_pcie_error_reporting(struct pci_dev *dev)
376{
377 if (pcie_aer_get_firmware_first(dev))
378 return -EIO;
379
380 return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
381 PCI_EXP_AER_FLAGS);
382}
383EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
384
Oza Pawandeepec752f52018-07-19 17:58:09 -0500385void pci_aer_clear_device_status(struct pci_dev *dev)
386{
387 u16 sta;
388
389 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &sta);
390 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, sta);
391}
392
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500393int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
394{
395 int pos;
Oza Pawandeepe7b0b842018-07-19 17:58:05 -0500396 u32 status, sev;
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500397
398 pos = dev->aer_cap;
399 if (!pos)
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
422 /* Clear status bits for ERR_FATAL errors only */
423 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
424 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &sev);
425 status &= sev;
426 if (status)
427 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
428}
429
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500430int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
431{
432 int pos;
433 u32 status;
434 int port_type;
435
436 if (!pci_is_pcie(dev))
437 return -ENODEV;
438
439 pos = dev->aer_cap;
440 if (!pos)
441 return -EIO;
442
443 port_type = pci_pcie_type(dev);
444 if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
445 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
446 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
447 }
448
449 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
450 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
451
452 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
453 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
454
455 return 0;
456}
457
Rajat Jain60ed9822018-06-21 16:48:26 -0700458void pci_aer_init(struct pci_dev *dev)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500459{
460 dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500461
462 if (dev->aer_cap)
463 dev->aer_stats = kzalloc(sizeof(struct aer_stats), GFP_KERNEL);
464
Rajat Jain60ed9822018-06-21 16:48:26 -0700465 pci_cleanup_aer_error_status_regs(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500466}
467
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500468void pci_aer_exit(struct pci_dev *dev)
469{
470 kfree(dev->aer_stats);
471 dev->aer_stats = NULL;
472}
473
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500474#define AER_AGENT_RECEIVER 0
475#define AER_AGENT_REQUESTER 1
476#define AER_AGENT_COMPLETER 2
477#define AER_AGENT_TRANSMITTER 3
478
479#define AER_AGENT_REQUESTER_MASK(t) ((t == AER_CORRECTABLE) ? \
480 0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
481#define AER_AGENT_COMPLETER_MASK(t) ((t == AER_CORRECTABLE) ? \
482 0 : PCI_ERR_UNC_COMP_ABORT)
483#define AER_AGENT_TRANSMITTER_MASK(t) ((t == AER_CORRECTABLE) ? \
484 (PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
485
486#define AER_GET_AGENT(t, e) \
487 ((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER : \
488 (e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER : \
489 (e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER : \
490 AER_AGENT_RECEIVER)
491
492#define AER_PHYSICAL_LAYER_ERROR 0
493#define AER_DATA_LINK_LAYER_ERROR 1
494#define AER_TRANSACTION_LAYER_ERROR 2
495
496#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
497 PCI_ERR_COR_RCVR : 0)
498#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
499 (PCI_ERR_COR_BAD_TLP| \
500 PCI_ERR_COR_BAD_DLLP| \
501 PCI_ERR_COR_REP_ROLL| \
502 PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
503
504#define AER_GET_LAYER_ERROR(t, e) \
505 ((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
506 (e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
507 AER_TRANSACTION_LAYER_ERROR)
508
509/*
510 * AER error strings
511 */
512static const char *aer_error_severity_string[] = {
513 "Uncorrected (Non-Fatal)",
514 "Uncorrected (Fatal)",
515 "Corrected"
516};
517
518static const char *aer_error_layer[] = {
519 "Physical Layer",
520 "Data Link Layer",
521 "Transaction Layer"
522};
523
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500524static const char *aer_correctable_error_string[AER_MAX_TYPEOF_COR_ERRS] = {
Tyler Baicarbd237802018-06-26 11:44:15 -0400525 "RxErr", /* Bit Position 0 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500526 NULL,
527 NULL,
528 NULL,
529 NULL,
530 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400531 "BadTLP", /* Bit Position 6 */
532 "BadDLLP", /* Bit Position 7 */
533 "Rollover", /* Bit Position 8 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500534 NULL,
535 NULL,
536 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400537 "Timeout", /* Bit Position 12 */
538 "NonFatalErr", /* Bit Position 13 */
539 "CorrIntErr", /* Bit Position 14 */
540 "HeaderOF", /* Bit Position 15 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500541};
542
Rajat Jaindb89ccb2018-06-30 15:07:17 -0500543static const char *aer_uncorrectable_error_string[AER_MAX_TYPEOF_UNCOR_ERRS] = {
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500544 "Undefined", /* Bit Position 0 */
545 NULL,
546 NULL,
547 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400548 "DLP", /* Bit Position 4 */
549 "SDES", /* Bit Position 5 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500550 NULL,
551 NULL,
552 NULL,
553 NULL,
554 NULL,
555 NULL,
Tyler Baicarbd237802018-06-26 11:44:15 -0400556 "TLP", /* Bit Position 12 */
557 "FCP", /* Bit Position 13 */
558 "CmpltTO", /* Bit Position 14 */
559 "CmpltAbrt", /* Bit Position 15 */
560 "UnxCmplt", /* Bit Position 16 */
561 "RxOF", /* Bit Position 17 */
562 "MalfTLP", /* Bit Position 18 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500563 "ECRC", /* Bit Position 19 */
Tyler Baicarbd237802018-06-26 11:44:15 -0400564 "UnsupReq", /* Bit Position 20 */
565 "ACSViol", /* Bit Position 21 */
566 "UncorrIntErr", /* Bit Position 22 */
567 "BlockedTLP", /* Bit Position 23 */
568 "AtomicOpBlocked", /* Bit Position 24 */
569 "TLPBlockedErr", /* Bit Position 25 */
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500570};
571
572static const char *aer_agent_string[] = {
573 "Receiver ID",
574 "Requester ID",
575 "Completer ID",
576 "Transmitter ID"
577};
578
Rajat Jain81aa5202018-06-21 16:48:28 -0700579#define aer_stats_dev_attr(name, stats_array, strings_array, \
580 total_string, total_field) \
581 static ssize_t \
582 name##_show(struct device *dev, struct device_attribute *attr, \
583 char *buf) \
584{ \
585 unsigned int i; \
586 char *str = buf; \
587 struct pci_dev *pdev = to_pci_dev(dev); \
588 u64 *stats = pdev->aer_stats->stats_array; \
589 \
590 for (i = 0; i < ARRAY_SIZE(strings_array); i++) { \
591 if (strings_array[i]) \
592 str += sprintf(str, "%s %llu\n", \
593 strings_array[i], stats[i]); \
594 else if (stats[i]) \
595 str += sprintf(str, #stats_array "_bit[%d] %llu\n",\
596 i, stats[i]); \
597 } \
598 str += sprintf(str, "TOTAL_%s %llu\n", total_string, \
599 pdev->aer_stats->total_field); \
600 return str-buf; \
601} \
602static DEVICE_ATTR_RO(name)
603
604aer_stats_dev_attr(aer_dev_correctable, dev_cor_errs,
605 aer_correctable_error_string, "ERR_COR",
606 dev_total_cor_errs);
607aer_stats_dev_attr(aer_dev_fatal, dev_fatal_errs,
608 aer_uncorrectable_error_string, "ERR_FATAL",
609 dev_total_fatal_errs);
610aer_stats_dev_attr(aer_dev_nonfatal, dev_nonfatal_errs,
611 aer_uncorrectable_error_string, "ERR_NONFATAL",
612 dev_total_nonfatal_errs);
613
Rajat Jain12833012018-06-21 16:48:29 -0700614#define aer_stats_rootport_attr(name, field) \
615 static ssize_t \
616 name##_show(struct device *dev, struct device_attribute *attr, \
617 char *buf) \
618{ \
619 struct pci_dev *pdev = to_pci_dev(dev); \
620 return sprintf(buf, "%llu\n", pdev->aer_stats->field); \
621} \
622static DEVICE_ATTR_RO(name)
623
624aer_stats_rootport_attr(aer_rootport_total_err_cor,
625 rootport_total_cor_errs);
626aer_stats_rootport_attr(aer_rootport_total_err_fatal,
627 rootport_total_fatal_errs);
628aer_stats_rootport_attr(aer_rootport_total_err_nonfatal,
629 rootport_total_nonfatal_errs);
630
Rajat Jain81aa5202018-06-21 16:48:28 -0700631static struct attribute *aer_stats_attrs[] __ro_after_init = {
632 &dev_attr_aer_dev_correctable.attr,
633 &dev_attr_aer_dev_fatal.attr,
634 &dev_attr_aer_dev_nonfatal.attr,
Rajat Jain12833012018-06-21 16:48:29 -0700635 &dev_attr_aer_rootport_total_err_cor.attr,
636 &dev_attr_aer_rootport_total_err_fatal.attr,
637 &dev_attr_aer_rootport_total_err_nonfatal.attr,
Rajat Jain81aa5202018-06-21 16:48:28 -0700638 NULL
639};
640
641static umode_t aer_stats_attrs_are_visible(struct kobject *kobj,
642 struct attribute *a, int n)
643{
644 struct device *dev = kobj_to_dev(kobj);
645 struct pci_dev *pdev = to_pci_dev(dev);
646
647 if (!pdev->aer_stats)
648 return 0;
649
Rajat Jain12833012018-06-21 16:48:29 -0700650 if ((a == &dev_attr_aer_rootport_total_err_cor.attr ||
651 a == &dev_attr_aer_rootport_total_err_fatal.attr ||
652 a == &dev_attr_aer_rootport_total_err_nonfatal.attr) &&
653 pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT)
654 return 0;
655
Rajat Jain81aa5202018-06-21 16:48:28 -0700656 return a->mode;
657}
658
659const struct attribute_group aer_stats_attr_group = {
660 .attrs = aer_stats_attrs,
661 .is_visible = aer_stats_attrs_are_visible,
662};
663
664static void pci_dev_aer_stats_incr(struct pci_dev *pdev,
665 struct aer_err_info *info)
666{
667 int status, i, max = -1;
668 u64 *counter = NULL;
669 struct aer_stats *aer_stats = pdev->aer_stats;
670
671 if (!aer_stats)
672 return;
673
674 switch (info->severity) {
675 case AER_CORRECTABLE:
676 aer_stats->dev_total_cor_errs++;
677 counter = &aer_stats->dev_cor_errs[0];
678 max = AER_MAX_TYPEOF_COR_ERRS;
679 break;
680 case AER_NONFATAL:
681 aer_stats->dev_total_nonfatal_errs++;
682 counter = &aer_stats->dev_nonfatal_errs[0];
683 max = AER_MAX_TYPEOF_UNCOR_ERRS;
684 break;
685 case AER_FATAL:
686 aer_stats->dev_total_fatal_errs++;
687 counter = &aer_stats->dev_fatal_errs[0];
688 max = AER_MAX_TYPEOF_UNCOR_ERRS;
689 break;
690 }
691
692 status = (info->status & ~info->mask);
693 for (i = 0; i < max; i++)
694 if (status & (1 << i))
695 counter[i]++;
696}
697
Rajat Jain12833012018-06-21 16:48:29 -0700698static void pci_rootport_aer_stats_incr(struct pci_dev *pdev,
699 struct aer_err_source *e_src)
700{
701 struct aer_stats *aer_stats = pdev->aer_stats;
702
703 if (!aer_stats)
704 return;
705
706 if (e_src->status & PCI_ERR_ROOT_COR_RCV)
707 aer_stats->rootport_total_cor_errs++;
708
709 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
710 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
711 aer_stats->rootport_total_fatal_errs++;
712 else
713 aer_stats->rootport_total_nonfatal_errs++;
714 }
715}
716
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500717static void __print_tlp_header(struct pci_dev *dev,
718 struct aer_header_log_regs *t)
719{
720 pci_err(dev, " TLP Header: %08x %08x %08x %08x\n",
721 t->dw0, t->dw1, t->dw2, t->dw3);
722}
723
724static void __aer_print_error(struct pci_dev *dev,
725 struct aer_err_info *info)
726{
727 int i, status;
728 const char *errmsg = NULL;
729 status = (info->status & ~info->mask);
730
731 for (i = 0; i < 32; i++) {
732 if (!(status & (1 << i)))
733 continue;
734
735 if (info->severity == AER_CORRECTABLE)
736 errmsg = i < ARRAY_SIZE(aer_correctable_error_string) ?
737 aer_correctable_error_string[i] : NULL;
738 else
739 errmsg = i < ARRAY_SIZE(aer_uncorrectable_error_string) ?
740 aer_uncorrectable_error_string[i] : NULL;
741
742 if (errmsg)
743 pci_err(dev, " [%2d] %-22s%s\n", i, errmsg,
744 info->first_error == i ? " (First)" : "");
745 else
746 pci_err(dev, " [%2d] Unknown Error Bit%s\n",
747 i, info->first_error == i ? " (First)" : "");
748 }
Rajat Jain81aa5202018-06-21 16:48:28 -0700749 pci_dev_aer_stats_incr(dev, info);
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500750}
751
Keith Busch1e451162018-07-19 16:16:55 -0500752void aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500753{
754 int layer, agent;
755 int id = ((dev->bus->number << 8) | dev->devfn);
756
757 if (!info->status) {
758 pci_err(dev, "PCIe Bus Error: severity=%s, type=Inaccessible, (Unregistered Agent ID)\n",
759 aer_error_severity_string[info->severity]);
760 goto out;
761 }
762
763 layer = AER_GET_LAYER_ERROR(info->severity, info->status);
764 agent = AER_GET_AGENT(info->severity, info->status);
765
766 pci_err(dev, "PCIe Bus Error: severity=%s, type=%s, (%s)\n",
767 aer_error_severity_string[info->severity],
768 aer_error_layer[layer], aer_agent_string[agent]);
769
770 pci_err(dev, " device [%04x:%04x] error status/mask=%08x/%08x\n",
771 dev->vendor, dev->device,
772 info->status, info->mask);
773
774 __aer_print_error(dev, info);
775
776 if (info->tlp_header_valid)
777 __print_tlp_header(dev, &info->tlp);
778
779out:
780 if (info->id && info->error_dev_num > 1 && info->id == id)
781 pci_err(dev, " Error of this Agent is reported first\n");
782
783 trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
784 info->severity, info->tlp_header_valid, &info->tlp);
785}
786
787static void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
788{
789 u8 bus = info->id >> 8;
790 u8 devfn = info->id & 0xff;
791
792 pci_info(dev, "AER: %s%s error received: %04x:%02x:%02x.%d\n",
793 info->multi_error_valid ? "Multiple " : "",
794 aer_error_severity_string[info->severity],
795 pci_domain_nr(dev->bus), bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
796}
797
798#ifdef CONFIG_ACPI_APEI_PCIEAER
799int cper_severity_to_aer(int cper_severity)
800{
801 switch (cper_severity) {
802 case CPER_SEV_RECOVERABLE:
803 return AER_NONFATAL;
804 case CPER_SEV_FATAL:
805 return AER_FATAL;
806 default:
807 return AER_CORRECTABLE;
808 }
809}
810EXPORT_SYMBOL_GPL(cper_severity_to_aer);
811
812void cper_print_aer(struct pci_dev *dev, int aer_severity,
813 struct aer_capability_regs *aer)
814{
815 int layer, agent, tlp_header_valid = 0;
816 u32 status, mask;
817 struct aer_err_info info;
818
819 if (aer_severity == AER_CORRECTABLE) {
820 status = aer->cor_status;
821 mask = aer->cor_mask;
822 } else {
823 status = aer->uncor_status;
824 mask = aer->uncor_mask;
825 tlp_header_valid = status & AER_LOG_TLP_MASKS;
826 }
827
828 layer = AER_GET_LAYER_ERROR(aer_severity, status);
829 agent = AER_GET_AGENT(aer_severity, status);
830
831 memset(&info, 0, sizeof(info));
832 info.severity = aer_severity;
833 info.status = status;
834 info.mask = mask;
835 info.first_error = PCI_ERR_CAP_FEP(aer->cap_control);
836
837 pci_err(dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
838 __aer_print_error(dev, &info);
839 pci_err(dev, "aer_layer=%s, aer_agent=%s\n",
840 aer_error_layer[layer], aer_agent_string[agent]);
841
842 if (aer_severity != AER_CORRECTABLE)
843 pci_err(dev, "aer_uncor_severity: 0x%08x\n",
844 aer->uncor_severity);
845
846 if (tlp_header_valid)
847 __print_tlp_header(dev, &aer->header_log);
848
849 trace_aer_event(dev_name(&dev->dev), (status & ~mask),
850 aer_severity, tlp_header_valid, &aer->header_log);
851}
852#endif
853
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500854/**
855 * add_error_device - list device to be handled
856 * @e_info: pointer to error info
857 * @dev: pointer to pci_dev to be added
858 */
859static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
860{
861 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
862 e_info->dev[e_info->error_dev_num] = dev;
863 e_info->error_dev_num++;
864 return 0;
865 }
866 return -ENOSPC;
867}
868
869/**
870 * is_error_source - check whether the device is source of reported error
871 * @dev: pointer to pci_dev to be checked
872 * @e_info: pointer to reported error info
873 */
874static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
875{
876 int pos;
877 u32 status, mask;
878 u16 reg16;
879
880 /*
881 * When bus id is equal to 0, it might be a bad id
882 * reported by root port.
883 */
884 if ((PCI_BUS_NUM(e_info->id) != 0) &&
885 !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
886 /* Device ID match? */
887 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
888 return true;
889
890 /* Continue id comparing if there is no multiple error */
891 if (!e_info->multi_error_valid)
892 return false;
893 }
894
895 /*
896 * When either
897 * 1) bus id is equal to 0. Some ports might lose the bus
898 * id of error source id;
899 * 2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
900 * 3) There are multiple errors and prior ID comparing fails;
901 * We check AER status registers to find possible reporter.
902 */
903 if (atomic_read(&dev->enable_cnt) == 0)
904 return false;
905
906 /* Check if AER is enabled */
907 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
908 if (!(reg16 & PCI_EXP_AER_FLAGS))
909 return false;
910
911 pos = dev->aer_cap;
912 if (!pos)
913 return false;
914
915 /* Check if error is recorded */
916 if (e_info->severity == AER_CORRECTABLE) {
917 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
918 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
919 } else {
920 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
921 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
922 }
923 if (status & ~mask)
924 return true;
925
926 return false;
927}
928
929static int find_device_iter(struct pci_dev *dev, void *data)
930{
931 struct aer_err_info *e_info = (struct aer_err_info *)data;
932
933 if (is_error_source(dev, e_info)) {
934 /* List this device */
935 if (add_error_device(e_info, dev)) {
936 /* We cannot handle more... Stop iteration */
937 /* TODO: Should print error message here? */
938 return 1;
939 }
940
941 /* If there is only a single error, stop iteration */
942 if (!e_info->multi_error_valid)
943 return 1;
944 }
945 return 0;
946}
947
948/**
949 * find_source_device - search through device hierarchy for source device
950 * @parent: pointer to Root Port pci_dev data structure
951 * @e_info: including detailed error information such like id
952 *
953 * Return true if found.
954 *
955 * Invoked by DPC when error is detected at the Root Port.
956 * Caller of this function must set id, severity, and multi_error_valid of
957 * struct aer_err_info pointed by @e_info properly. This function must fill
958 * e_info->error_dev_num and e_info->dev[], based on the given information.
959 */
960static bool find_source_device(struct pci_dev *parent,
961 struct aer_err_info *e_info)
962{
963 struct pci_dev *dev = parent;
964 int result;
965
966 /* Must reset in this function */
967 e_info->error_dev_num = 0;
968
969 /* Is Root Port an agent that sends error message? */
970 result = find_device_iter(dev, e_info);
971 if (result)
972 return true;
973
974 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
975
976 if (!e_info->error_dev_num) {
977 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
978 e_info->id);
979 return false;
980 }
981 return true;
982}
983
984/**
985 * handle_error_source - handle logging error into an event log
986 * @dev: pointer to pci_dev data structure of error source device
987 * @info: comprehensive error information
988 *
989 * Invoked when an error being detected by Root Port.
990 */
991static void handle_error_source(struct pci_dev *dev, struct aer_err_info *info)
992{
993 int pos;
994
995 if (info->severity == AER_CORRECTABLE) {
996 /*
997 * Correctable error does not need software intervention.
998 * No need to go through error recovery process.
999 */
1000 pos = dev->aer_cap;
1001 if (pos)
1002 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
1003 info->status);
Oza Pawandeep10d790d92018-07-19 17:58:09 -05001004 pci_aer_clear_device_status(dev);
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001005 } else if (info->severity == AER_NONFATAL)
1006 pcie_do_nonfatal_recovery(dev);
1007 else if (info->severity == AER_FATAL)
1008 pcie_do_fatal_recovery(dev, PCIE_PORT_SERVICE_AER);
1009}
1010
1011#ifdef CONFIG_ACPI_APEI_PCIEAER
1012
1013#define AER_RECOVER_RING_ORDER 4
1014#define AER_RECOVER_RING_SIZE (1 << AER_RECOVER_RING_ORDER)
1015
1016struct aer_recover_entry {
1017 u8 bus;
1018 u8 devfn;
1019 u16 domain;
1020 int severity;
1021 struct aer_capability_regs *regs;
1022};
1023
1024static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
1025 AER_RECOVER_RING_SIZE);
1026
1027static void aer_recover_work_func(struct work_struct *work)
1028{
1029 struct aer_recover_entry entry;
1030 struct pci_dev *pdev;
1031
1032 while (kfifo_get(&aer_recover_ring, &entry)) {
1033 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
1034 entry.devfn);
1035 if (!pdev) {
1036 pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
1037 entry.domain, entry.bus,
1038 PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
1039 continue;
1040 }
1041 cper_print_aer(pdev, entry.severity, entry.regs);
1042 if (entry.severity == AER_NONFATAL)
1043 pcie_do_nonfatal_recovery(pdev);
1044 else if (entry.severity == AER_FATAL)
1045 pcie_do_fatal_recovery(pdev, PCIE_PORT_SERVICE_AER);
1046 pci_dev_put(pdev);
1047 }
1048}
1049
1050/*
1051 * Mutual exclusion for writers of aer_recover_ring, reader side don't
1052 * need lock, because there is only one reader and lock is not needed
1053 * between reader and writer.
1054 */
1055static DEFINE_SPINLOCK(aer_recover_ring_lock);
1056static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
1057
1058void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
1059 int severity, struct aer_capability_regs *aer_regs)
1060{
1061 unsigned long flags;
1062 struct aer_recover_entry entry = {
1063 .bus = bus,
1064 .devfn = devfn,
1065 .domain = domain,
1066 .severity = severity,
1067 .regs = aer_regs,
1068 };
1069
1070 spin_lock_irqsave(&aer_recover_ring_lock, flags);
1071 if (kfifo_put(&aer_recover_ring, entry))
1072 schedule_work(&aer_recover_work);
1073 else
1074 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
1075 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1076 spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
1077}
1078EXPORT_SYMBOL_GPL(aer_recover_queue);
1079#endif
1080
1081/**
Keith Busch1e451162018-07-19 16:16:55 -05001082 * aer_get_device_error_info - read error status from dev and store it to info
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001083 * @dev: pointer to the device expected to have a error record
1084 * @info: pointer to structure to store the error record
1085 *
1086 * Return 1 on success, 0 on error.
1087 *
1088 * Note that @info is reused among all error devices. Clear fields properly.
1089 */
Keith Busch1e451162018-07-19 16:16:55 -05001090int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001091{
1092 int pos, temp;
1093
1094 /* Must reset in this function */
1095 info->status = 0;
1096 info->tlp_header_valid = 0;
1097
1098 pos = dev->aer_cap;
1099
1100 /* The device might not support AER */
1101 if (!pos)
1102 return 0;
1103
1104 if (info->severity == AER_CORRECTABLE) {
1105 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
1106 &info->status);
1107 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
1108 &info->mask);
1109 if (!(info->status & ~info->mask))
1110 return 0;
1111 } else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1112 info->severity == AER_NONFATAL) {
1113
1114 /* Link is still healthy for IO reads */
1115 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
1116 &info->status);
1117 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
1118 &info->mask);
1119 if (!(info->status & ~info->mask))
1120 return 0;
1121
1122 /* Get First Error Pointer */
1123 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
1124 info->first_error = PCI_ERR_CAP_FEP(temp);
1125
1126 if (info->status & AER_LOG_TLP_MASKS) {
1127 info->tlp_header_valid = 1;
1128 pci_read_config_dword(dev,
1129 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
1130 pci_read_config_dword(dev,
1131 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
1132 pci_read_config_dword(dev,
1133 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
1134 pci_read_config_dword(dev,
1135 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
1136 }
1137 }
1138
1139 return 1;
1140}
1141
1142static inline void aer_process_err_devices(struct aer_err_info *e_info)
1143{
1144 int i;
1145
1146 /* Report all before handle them, not to lost records by reset etc. */
1147 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001148 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001149 aer_print_error(e_info->dev[i], e_info);
1150 }
1151 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -05001152 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001153 handle_error_source(e_info->dev[i], e_info);
1154 }
1155}
1156
1157/**
1158 * aer_isr_one_error - consume an error detected by root port
1159 * @rpc: pointer to the root port which holds an error
1160 * @e_src: pointer to an error source
1161 */
1162static void aer_isr_one_error(struct aer_rpc *rpc,
1163 struct aer_err_source *e_src)
1164{
1165 struct pci_dev *pdev = rpc->rpd;
1166 struct aer_err_info *e_info = &rpc->e_info;
1167
Rajat Jain12833012018-06-21 16:48:29 -07001168 pci_rootport_aer_stats_incr(pdev, e_src);
1169
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -05001170 /*
1171 * There is a possibility that both correctable error and
1172 * uncorrectable error being logged. Report correctable error first.
1173 */
1174 if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
1175 e_info->id = ERR_COR_ID(e_src->id);
1176 e_info->severity = AER_CORRECTABLE;
1177
1178 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
1179 e_info->multi_error_valid = 1;
1180 else
1181 e_info->multi_error_valid = 0;
1182 aer_print_port_info(pdev, e_info);
1183
1184 if (find_source_device(pdev, e_info))
1185 aer_process_err_devices(e_info);
1186 }
1187
1188 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
1189 e_info->id = ERR_UNCOR_ID(e_src->id);
1190
1191 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
1192 e_info->severity = AER_FATAL;
1193 else
1194 e_info->severity = AER_NONFATAL;
1195
1196 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
1197 e_info->multi_error_valid = 1;
1198 else
1199 e_info->multi_error_valid = 0;
1200
1201 aer_print_port_info(pdev, e_info);
1202
1203 if (find_source_device(pdev, e_info))
1204 aer_process_err_devices(e_info);
1205 }
1206}
1207
1208/**
1209 * get_e_source - retrieve an error source
1210 * @rpc: pointer to the root port which holds an error
1211 * @e_src: pointer to store retrieved error source
1212 *
1213 * Return 1 if an error source is retrieved, otherwise 0.
1214 *
1215 * Invoked by DPC handler to consume an error.
1216 */
1217static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
1218{
1219 unsigned long flags;
1220
1221 /* Lock access to Root error producer/consumer index */
1222 spin_lock_irqsave(&rpc->e_lock, flags);
1223 if (rpc->prod_idx == rpc->cons_idx) {
1224 spin_unlock_irqrestore(&rpc->e_lock, flags);
1225 return 0;
1226 }
1227
1228 *e_src = rpc->e_sources[rpc->cons_idx];
1229 rpc->cons_idx++;
1230 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
1231 rpc->cons_idx = 0;
1232 spin_unlock_irqrestore(&rpc->e_lock, flags);
1233
1234 return 1;
1235}
1236
1237/**
1238 * aer_isr - consume errors detected by root port
1239 * @work: definition of this work item
1240 *
1241 * Invoked, as DPC, when root port records new detected error
1242 */
1243static void aer_isr(struct work_struct *work)
1244{
1245 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
1246 struct aer_err_source uninitialized_var(e_src);
1247
1248 mutex_lock(&rpc->rpc_mutex);
1249 while (get_e_source(rpc, &e_src))
1250 aer_isr_one_error(rpc, &e_src);
1251 mutex_unlock(&rpc->rpc_mutex);
1252}
1253
Bjorn Helgaas3c43a642018-06-08 08:31:57 -05001254/**
1255 * aer_irq - Root Port's ISR
1256 * @irq: IRQ assigned to Root Port
1257 * @context: pointer to Root Port data structure
1258 *
1259 * Invoked when Root Port detects AER messages.
1260 */
1261irqreturn_t aer_irq(int irq, void *context)
1262{
1263 unsigned int status, id;
1264 struct pcie_device *pdev = (struct pcie_device *)context;
1265 struct aer_rpc *rpc = get_service_data(pdev);
1266 int next_prod_idx;
1267 unsigned long flags;
1268 int pos;
1269
1270 pos = pdev->port->aer_cap;
1271 /*
1272 * Must lock access to Root Error Status Reg, Root Error ID Reg,
1273 * and Root error producer/consumer index
1274 */
1275 spin_lock_irqsave(&rpc->e_lock, flags);
1276
1277 /* Read error status */
1278 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
1279 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
1280 spin_unlock_irqrestore(&rpc->e_lock, flags);
1281 return IRQ_NONE;
1282 }
1283
1284 /* Read error source and clear error status */
1285 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
1286 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
1287
1288 /* Store error source for later DPC handler */
1289 next_prod_idx = rpc->prod_idx + 1;
1290 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
1291 next_prod_idx = 0;
1292 if (next_prod_idx == rpc->cons_idx) {
1293 /*
1294 * Error Storm Condition - possibly the same error occurred.
1295 * Drop the error.
1296 */
1297 spin_unlock_irqrestore(&rpc->e_lock, flags);
1298 return IRQ_HANDLED;
1299 }
1300 rpc->e_sources[rpc->prod_idx].status = status;
1301 rpc->e_sources[rpc->prod_idx].id = id;
1302 rpc->prod_idx = next_prod_idx;
1303 spin_unlock_irqrestore(&rpc->e_lock, flags);
1304
1305 /* Invoke DPC handler */
1306 schedule_work(&rpc->dpc_handler);
1307
1308 return IRQ_HANDLED;
1309}
1310EXPORT_SYMBOL_GPL(aer_irq);
1311
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001312static int set_device_error_reporting(struct pci_dev *dev, void *data)
1313{
1314 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +08001315 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001316
Yijing Wang62f87c02012-07-24 17:20:03 +08001317 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
1318 (type == PCI_EXP_TYPE_UPSTREAM) ||
1319 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001320 if (enable)
1321 pci_enable_pcie_error_reporting(dev);
1322 else
1323 pci_disable_pcie_error_reporting(dev);
1324 }
1325
1326 if (enable)
1327 pcie_set_ecrc_checking(dev);
1328
1329 return 0;
1330}
1331
1332/**
1333 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
1334 * @dev: pointer to root port's pci_dev data structure
1335 * @enable: true = enable error reporting, false = disable error reporting.
1336 */
1337static void set_downstream_devices_error_reporting(struct pci_dev *dev,
1338 bool enable)
1339{
1340 set_device_error_reporting(dev, &enable);
1341
1342 if (!dev->subordinate)
1343 return;
1344 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
1345}
1346
1347/**
1348 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
1349 * @rpc: pointer to a Root Port data structure
1350 *
1351 * Invoked when PCIe bus loads AER service driver.
1352 */
1353static void aer_enable_rootport(struct aer_rpc *rpc)
1354{
Keith Busche13d17f2018-04-09 16:04:42 -06001355 struct pci_dev *pdev = rpc->rpd;
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001356 int aer_pos;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001357 u16 reg16;
1358 u32 reg32;
1359
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001360 /* Clear PCIe Capability's Device Status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001361 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
1362 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001363
1364 /* Disable system error generation in response to error messages */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001365 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
1366 SYSTEM_ERROR_INTR_ON_MESG_MASK);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001367
Keith Busch66b80802016-09-27 16:23:34 -04001368 aer_pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001369 /* Clear error status */
1370 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
1371 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
1372 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
1373 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
1374 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
1375 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
1376
1377 /*
1378 * Enable error reporting for the root port device and downstream port
1379 * devices.
1380 */
1381 set_downstream_devices_error_reporting(pdev, true);
1382
1383 /* Enable Root Port's interrupt in response to error messages */
1384 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
1385 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1386 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
1387}
1388
1389/**
1390 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
1391 * @rpc: pointer to a Root Port data structure
1392 *
1393 * Invoked when PCIe bus unloads AER service driver.
1394 */
1395static void aer_disable_rootport(struct aer_rpc *rpc)
1396{
Keith Busche13d17f2018-04-09 16:04:42 -06001397 struct pci_dev *pdev = rpc->rpd;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001398 u32 reg32;
1399 int pos;
1400
1401 /*
1402 * Disable error reporting for the root port device and downstream port
1403 * devices.
1404 */
1405 set_downstream_devices_error_reporting(pdev, false);
1406
Keith Busch66b80802016-09-27 16:23:34 -04001407 pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001408 /* Disable Root's interrupt in response to error messages */
1409 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1410 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1411 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
1412
1413 /* Clear Root's error status reg */
1414 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1415 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
1416}
1417
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001418/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001419 * aer_alloc_rpc - allocate Root Port data structure
1420 * @dev: pointer to the pcie_dev data structure
1421 *
1422 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001423 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001424static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001425{
1426 struct aer_rpc *rpc;
1427
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001428 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
1429 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001430 return NULL;
1431
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001432 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -07001433 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001434
Keith Busche13d17f2018-04-09 16:04:42 -06001435 rpc->rpd = dev->port;
David Howells65f27f32006-11-22 14:55:48 +00001436 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001437 mutex_init(&rpc->rpc_mutex);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001438
Stefan Assmann45e829e2009-12-03 06:49:24 -05001439 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001440 set_service_data(dev, rpc);
1441
1442 return rpc;
1443}
1444
1445/**
1446 * aer_remove - clean up resources
1447 * @dev: pointer to the pcie_dev data structure
1448 *
1449 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001450 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001451static void aer_remove(struct pcie_device *dev)
1452{
1453 struct aer_rpc *rpc = get_service_data(dev);
1454
1455 if (rpc) {
1456 /* If register interrupt service, it must be free. */
1457 if (rpc->isr)
1458 free_irq(dev->irq, dev);
1459
Sebastian Andrzej Siewior4ae21822016-01-25 10:08:00 -06001460 flush_work(&rpc->dpc_handler);
Hidetoshi Seto460d2982010-04-15 13:10:03 +09001461 aer_disable_rootport(rpc);
1462 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001463 set_service_data(dev, NULL);
1464 }
1465}
1466
1467/**
1468 * aer_probe - initialize resources
1469 * @dev: pointer to the pcie_dev data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001470 *
1471 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001472 */
Bill Pemberton15856ad2012-11-21 15:35:00 -05001473static int aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001474{
1475 int status;
1476 struct aer_rpc *rpc;
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001477 struct device *device = &dev->port->dev;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001478
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001479 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001480 rpc = aer_alloc_rpc(dev);
1481 if (!rpc) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001482 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001483 aer_remove(dev);
1484 return -ENOMEM;
1485 }
1486
1487 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001488 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
1489 if (status) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001490 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
1491 dev->irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001492 aer_remove(dev);
1493 return status;
1494 }
1495
1496 rpc->isr = 1;
1497
1498 aer_enable_rootport(rpc);
Bjorn Helgaas68a55ae2016-11-21 15:34:02 -06001499 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
1500 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001501}
1502
1503/**
1504 * aer_root_reset - reset link on Root Port
1505 * @dev: pointer to Root Port's pci_dev data structure
1506 *
1507 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001508 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001509static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
1510{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001511 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001512 int pos;
1513
Keith Busch66b80802016-09-27 16:23:34 -04001514 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001515
1516 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001517 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1518 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1519 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001520
Alex Williamson1b95ce82013-08-08 14:10:20 -06001521 pci_reset_bridge_secondary_bus(dev);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001522 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001523
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001524 /* Clear Root Error Status */
1525 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1526 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
1527
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001528 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001529 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1530 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1531 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001532
1533 return PCI_ERS_RESULT_RECOVERED;
1534}
1535
1536/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001537 * aer_error_resume - clean up corresponding error status bits
1538 * @dev: pointer to Root Port's pci_dev data structure
1539 *
1540 * Invoked by Port Bus driver during nonfatal recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001541 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001542static void aer_error_resume(struct pci_dev *dev)
1543{
Oza Pawandeepec752f52018-07-19 17:58:09 -05001544 pci_aer_clear_device_status(dev);
Oza Pawandeep5b6c0962018-07-19 17:58:06 -05001545 pci_cleanup_aer_uncorrect_error_status(dev);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001546}
1547
Bjorn Helgaas0054ca82018-06-08 08:31:42 -05001548static struct pcie_port_service_driver aerdriver = {
1549 .name = "aer",
1550 .port_type = PCI_EXP_TYPE_ROOT_PORT,
1551 .service = PCIE_PORT_SERVICE_AER,
1552
1553 .probe = aer_probe,
1554 .remove = aer_remove,
1555 .error_resume = aer_error_resume,
1556 .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 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001564static int __init aer_service_init(void)
1565{
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}
Paul Gortmaker87563362016-08-24 16:57:46 -04001570device_initcall(aer_service_init);