blob: 0a60275f05820cb46fc49fd51302bd4dd4d22761 [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
35struct aer_err_source {
36 unsigned int status;
37 unsigned int id;
38};
39
40struct aer_rpc {
41 struct pci_dev *rpd; /* Root Port device */
42 struct work_struct dpc_handler;
43 struct aer_err_source e_sources[AER_ERROR_SOURCES_MAX];
44 struct aer_err_info e_info;
45 unsigned short prod_idx; /* Error Producer Index */
46 unsigned short cons_idx; /* Error Consumer Index */
47 int isr;
48 spinlock_t e_lock; /*
49 * Lock access to Error Status/ID Regs
50 * and error producer/consumer index
51 */
52 struct mutex rpc_mutex; /*
53 * only one thread could do
54 * recovery on the same
55 * root port hierarchy
56 */
57};
58
59#define AER_LOG_TLP_MASKS (PCI_ERR_UNC_POISON_TLP| \
60 PCI_ERR_UNC_ECRC| \
61 PCI_ERR_UNC_UNSUP| \
62 PCI_ERR_UNC_COMP_ABORT| \
63 PCI_ERR_UNC_UNX_COMP| \
64 PCI_ERR_UNC_MALF_TLP)
65
66#define SYSTEM_ERROR_INTR_ON_MESG_MASK (PCI_EXP_RTCTL_SECEE| \
67 PCI_EXP_RTCTL_SENFEE| \
68 PCI_EXP_RTCTL_SEFEE)
69#define ROOT_PORT_INTR_ON_MESG_MASK (PCI_ERR_ROOT_CMD_COR_EN| \
70 PCI_ERR_ROOT_CMD_NONFATAL_EN| \
71 PCI_ERR_ROOT_CMD_FATAL_EN)
72#define ERR_COR_ID(d) (d & 0xffff)
73#define ERR_UNCOR_ID(d) (d >> 16)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +080074
Randy Dunlap7f785762007-10-05 13:17:58 -070075static int pcie_aer_disable;
76
77void pci_no_aer(void)
78{
Bjorn Helgaas7ece1412016-09-06 16:24:37 -050079 pcie_aer_disable = 1;
Randy Dunlap7f785762007-10-05 13:17:58 -070080}
81
Rafael J. Wysockif1a7bfa2010-08-21 01:50:52 +020082bool pci_aer_available(void)
83{
84 return !pcie_aer_disable && pci_msi_enabled();
85}
86
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -050087#ifdef CONFIG_PCIE_ECRC
88
89#define ECRC_POLICY_DEFAULT 0 /* ECRC set by BIOS */
90#define ECRC_POLICY_OFF 1 /* ECRC off for performance */
91#define ECRC_POLICY_ON 2 /* ECRC on for data integrity */
92
93static int ecrc_policy = ECRC_POLICY_DEFAULT;
94
95static const char *ecrc_policy_str[] = {
96 [ECRC_POLICY_DEFAULT] = "bios",
97 [ECRC_POLICY_OFF] = "off",
98 [ECRC_POLICY_ON] = "on"
99};
100
101/**
102 * enable_ercr_checking - enable PCIe ECRC checking for a device
103 * @dev: the PCI device
104 *
105 * Returns 0 on success, or negative on failure.
106 */
107static int enable_ecrc_checking(struct pci_dev *dev)
108{
109 int pos;
110 u32 reg32;
111
112 if (!pci_is_pcie(dev))
113 return -ENODEV;
114
115 pos = dev->aer_cap;
116 if (!pos)
117 return -ENODEV;
118
119 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
120 if (reg32 & PCI_ERR_CAP_ECRC_GENC)
121 reg32 |= PCI_ERR_CAP_ECRC_GENE;
122 if (reg32 & PCI_ERR_CAP_ECRC_CHKC)
123 reg32 |= PCI_ERR_CAP_ECRC_CHKE;
124 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
125
126 return 0;
127}
128
129/**
130 * disable_ercr_checking - disables PCIe ECRC checking for a device
131 * @dev: the PCI device
132 *
133 * Returns 0 on success, or negative on failure.
134 */
135static int disable_ecrc_checking(struct pci_dev *dev)
136{
137 int pos;
138 u32 reg32;
139
140 if (!pci_is_pcie(dev))
141 return -ENODEV;
142
143 pos = dev->aer_cap;
144 if (!pos)
145 return -ENODEV;
146
147 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &reg32);
148 reg32 &= ~(PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
149 pci_write_config_dword(dev, pos + PCI_ERR_CAP, reg32);
150
151 return 0;
152}
153
154/**
155 * pcie_set_ecrc_checking - set/unset PCIe ECRC checking for a device based on global policy
156 * @dev: the PCI device
157 */
158void pcie_set_ecrc_checking(struct pci_dev *dev)
159{
160 switch (ecrc_policy) {
161 case ECRC_POLICY_DEFAULT:
162 return;
163 case ECRC_POLICY_OFF:
164 disable_ecrc_checking(dev);
165 break;
166 case ECRC_POLICY_ON:
167 enable_ecrc_checking(dev);
168 break;
169 default:
170 return;
171 }
172}
173
174/**
175 * pcie_ecrc_get_policy - parse kernel command-line ecrc option
176 */
177void pcie_ecrc_get_policy(char *str)
178{
179 int i;
180
181 for (i = 0; i < ARRAY_SIZE(ecrc_policy_str); i++)
182 if (!strncmp(str, ecrc_policy_str[i],
183 strlen(ecrc_policy_str[i])))
184 break;
185 if (i >= ARRAY_SIZE(ecrc_policy_str))
186 return;
187
188 ecrc_policy = i;
189}
190#endif /* CONFIG_PCIE_ECRC */
191
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500192#ifdef CONFIG_ACPI_APEI
193static inline int hest_match_pci(struct acpi_hest_aer_common *p,
194 struct pci_dev *pci)
195{
196 return ACPI_HEST_SEGMENT(p->bus) == pci_domain_nr(pci->bus) &&
197 ACPI_HEST_BUS(p->bus) == pci->bus->number &&
198 p->device == PCI_SLOT(pci->devfn) &&
199 p->function == PCI_FUNC(pci->devfn);
200}
201
202static inline bool hest_match_type(struct acpi_hest_header *hest_hdr,
203 struct pci_dev *dev)
204{
205 u16 hest_type = hest_hdr->type;
206 u8 pcie_type = pci_pcie_type(dev);
207
208 if ((hest_type == ACPI_HEST_TYPE_AER_ROOT_PORT &&
209 pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
210 (hest_type == ACPI_HEST_TYPE_AER_ENDPOINT &&
211 pcie_type == PCI_EXP_TYPE_ENDPOINT) ||
212 (hest_type == ACPI_HEST_TYPE_AER_BRIDGE &&
213 (dev->class >> 16) == PCI_BASE_CLASS_BRIDGE))
214 return true;
215 return false;
216}
217
218struct aer_hest_parse_info {
219 struct pci_dev *pci_dev;
220 int firmware_first;
221};
222
223static int hest_source_is_pcie_aer(struct acpi_hest_header *hest_hdr)
224{
225 if (hest_hdr->type == ACPI_HEST_TYPE_AER_ROOT_PORT ||
226 hest_hdr->type == ACPI_HEST_TYPE_AER_ENDPOINT ||
227 hest_hdr->type == ACPI_HEST_TYPE_AER_BRIDGE)
228 return 1;
229 return 0;
230}
231
232static int aer_hest_parse(struct acpi_hest_header *hest_hdr, void *data)
233{
234 struct aer_hest_parse_info *info = data;
235 struct acpi_hest_aer_common *p;
236 int ff;
237
238 if (!hest_source_is_pcie_aer(hest_hdr))
239 return 0;
240
241 p = (struct acpi_hest_aer_common *)(hest_hdr + 1);
242 ff = !!(p->flags & ACPI_HEST_FIRMWARE_FIRST);
243
244 /*
245 * If no specific device is supplied, determine whether
246 * FIRMWARE_FIRST is set for *any* PCIe device.
247 */
248 if (!info->pci_dev) {
249 info->firmware_first |= ff;
250 return 0;
251 }
252
253 /* Otherwise, check the specific device */
254 if (p->flags & ACPI_HEST_GLOBAL) {
255 if (hest_match_type(hest_hdr, info->pci_dev))
256 info->firmware_first = ff;
257 } else
258 if (hest_match_pci(p, info->pci_dev))
259 info->firmware_first = ff;
260
261 return 0;
262}
263
264static void aer_set_firmware_first(struct pci_dev *pci_dev)
265{
266 int rc;
267 struct aer_hest_parse_info info = {
268 .pci_dev = pci_dev,
269 .firmware_first = 0,
270 };
271
272 rc = apei_hest_parse(aer_hest_parse, &info);
273
274 if (rc)
275 pci_dev->__aer_firmware_first = 0;
276 else
277 pci_dev->__aer_firmware_first = info.firmware_first;
278 pci_dev->__aer_firmware_first_valid = 1;
279}
280
281int pcie_aer_get_firmware_first(struct pci_dev *dev)
282{
283 if (!pci_is_pcie(dev))
284 return 0;
285
286 if (!dev->__aer_firmware_first_valid)
287 aer_set_firmware_first(dev);
288 return dev->__aer_firmware_first;
289}
Bjorn Helgaas41cbc9e2018-06-08 08:40:00 -0500290#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
291 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
Bjorn Helgaas256a4592018-06-08 08:39:45 -0500292
293static bool aer_firmware_first;
294
295/**
296 * aer_acpi_firmware_first - Check if APEI should control AER.
297 */
298bool aer_acpi_firmware_first(void)
299{
300 static bool parsed = false;
301 struct aer_hest_parse_info info = {
302 .pci_dev = NULL, /* Check all PCIe devices */
303 .firmware_first = 0,
304 };
305
306 if (!parsed) {
307 apei_hest_parse(aer_hest_parse, &info);
308 aer_firmware_first = info.firmware_first;
309 parsed = true;
310 }
311 return aer_firmware_first;
312}
313#endif
314
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500315#define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
316 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
317
318int pci_enable_pcie_error_reporting(struct pci_dev *dev)
319{
320 if (pcie_aer_get_firmware_first(dev))
321 return -EIO;
322
323 if (!dev->aer_cap)
324 return -EIO;
325
326 return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
327}
328EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
329
330int pci_disable_pcie_error_reporting(struct pci_dev *dev)
331{
332 if (pcie_aer_get_firmware_first(dev))
333 return -EIO;
334
335 return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
336 PCI_EXP_AER_FLAGS);
337}
338EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
339
340int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
341{
342 int pos;
343 u32 status;
344
345 pos = dev->aer_cap;
346 if (!pos)
347 return -EIO;
348
349 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
350 if (status)
351 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
352
353 return 0;
354}
355EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
356
357int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
358{
359 int pos;
360 u32 status;
361 int port_type;
362
363 if (!pci_is_pcie(dev))
364 return -ENODEV;
365
366 pos = dev->aer_cap;
367 if (!pos)
368 return -EIO;
369
370 port_type = pci_pcie_type(dev);
371 if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
372 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
373 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
374 }
375
376 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
377 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
378
379 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
380 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
381
382 return 0;
383}
384
385int pci_aer_init(struct pci_dev *dev)
386{
387 dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
388 return pci_cleanup_aer_error_status_regs(dev);
389}
390
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500391#define AER_AGENT_RECEIVER 0
392#define AER_AGENT_REQUESTER 1
393#define AER_AGENT_COMPLETER 2
394#define AER_AGENT_TRANSMITTER 3
395
396#define AER_AGENT_REQUESTER_MASK(t) ((t == AER_CORRECTABLE) ? \
397 0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
398#define AER_AGENT_COMPLETER_MASK(t) ((t == AER_CORRECTABLE) ? \
399 0 : PCI_ERR_UNC_COMP_ABORT)
400#define AER_AGENT_TRANSMITTER_MASK(t) ((t == AER_CORRECTABLE) ? \
401 (PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
402
403#define AER_GET_AGENT(t, e) \
404 ((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER : \
405 (e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER : \
406 (e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER : \
407 AER_AGENT_RECEIVER)
408
409#define AER_PHYSICAL_LAYER_ERROR 0
410#define AER_DATA_LINK_LAYER_ERROR 1
411#define AER_TRANSACTION_LAYER_ERROR 2
412
413#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
414 PCI_ERR_COR_RCVR : 0)
415#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ? \
416 (PCI_ERR_COR_BAD_TLP| \
417 PCI_ERR_COR_BAD_DLLP| \
418 PCI_ERR_COR_REP_ROLL| \
419 PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
420
421#define AER_GET_LAYER_ERROR(t, e) \
422 ((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
423 (e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
424 AER_TRANSACTION_LAYER_ERROR)
425
426/*
427 * AER error strings
428 */
429static const char *aer_error_severity_string[] = {
430 "Uncorrected (Non-Fatal)",
431 "Uncorrected (Fatal)",
432 "Corrected"
433};
434
435static const char *aer_error_layer[] = {
436 "Physical Layer",
437 "Data Link Layer",
438 "Transaction Layer"
439};
440
441static const char *aer_correctable_error_string[] = {
442 "Receiver Error", /* Bit Position 0 */
443 NULL,
444 NULL,
445 NULL,
446 NULL,
447 NULL,
448 "Bad TLP", /* Bit Position 6 */
449 "Bad DLLP", /* Bit Position 7 */
450 "RELAY_NUM Rollover", /* Bit Position 8 */
451 NULL,
452 NULL,
453 NULL,
454 "Replay Timer Timeout", /* Bit Position 12 */
455 "Advisory Non-Fatal", /* Bit Position 13 */
456 "Corrected Internal Error", /* Bit Position 14 */
457 "Header Log Overflow", /* Bit Position 15 */
458};
459
460static const char *aer_uncorrectable_error_string[] = {
461 "Undefined", /* Bit Position 0 */
462 NULL,
463 NULL,
464 NULL,
465 "Data Link Protocol", /* Bit Position 4 */
466 "Surprise Down Error", /* Bit Position 5 */
467 NULL,
468 NULL,
469 NULL,
470 NULL,
471 NULL,
472 NULL,
473 "Poisoned TLP", /* Bit Position 12 */
474 "Flow Control Protocol", /* Bit Position 13 */
475 "Completion Timeout", /* Bit Position 14 */
476 "Completer Abort", /* Bit Position 15 */
477 "Unexpected Completion", /* Bit Position 16 */
478 "Receiver Overflow", /* Bit Position 17 */
479 "Malformed TLP", /* Bit Position 18 */
480 "ECRC", /* Bit Position 19 */
481 "Unsupported Request", /* Bit Position 20 */
482 "ACS Violation", /* Bit Position 21 */
483 "Uncorrectable Internal Error", /* Bit Position 22 */
484 "MC Blocked TLP", /* Bit Position 23 */
485 "AtomicOp Egress Blocked", /* Bit Position 24 */
486 "TLP Prefix Blocked Error", /* Bit Position 25 */
487};
488
489static const char *aer_agent_string[] = {
490 "Receiver ID",
491 "Requester ID",
492 "Completer ID",
493 "Transmitter ID"
494};
495
496static void __print_tlp_header(struct pci_dev *dev,
497 struct aer_header_log_regs *t)
498{
499 pci_err(dev, " TLP Header: %08x %08x %08x %08x\n",
500 t->dw0, t->dw1, t->dw2, t->dw3);
501}
502
503static void __aer_print_error(struct pci_dev *dev,
504 struct aer_err_info *info)
505{
506 int i, status;
507 const char *errmsg = NULL;
508 status = (info->status & ~info->mask);
509
510 for (i = 0; i < 32; i++) {
511 if (!(status & (1 << i)))
512 continue;
513
514 if (info->severity == AER_CORRECTABLE)
515 errmsg = i < ARRAY_SIZE(aer_correctable_error_string) ?
516 aer_correctable_error_string[i] : NULL;
517 else
518 errmsg = i < ARRAY_SIZE(aer_uncorrectable_error_string) ?
519 aer_uncorrectable_error_string[i] : NULL;
520
521 if (errmsg)
522 pci_err(dev, " [%2d] %-22s%s\n", i, errmsg,
523 info->first_error == i ? " (First)" : "");
524 else
525 pci_err(dev, " [%2d] Unknown Error Bit%s\n",
526 i, info->first_error == i ? " (First)" : "");
527 }
528}
529
Keith Busch1e451162018-07-19 16:16:55 -0500530void aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaas0319c9a2018-06-08 08:39:38 -0500531{
532 int layer, agent;
533 int id = ((dev->bus->number << 8) | dev->devfn);
534
535 if (!info->status) {
536 pci_err(dev, "PCIe Bus Error: severity=%s, type=Inaccessible, (Unregistered Agent ID)\n",
537 aer_error_severity_string[info->severity]);
538 goto out;
539 }
540
541 layer = AER_GET_LAYER_ERROR(info->severity, info->status);
542 agent = AER_GET_AGENT(info->severity, info->status);
543
544 pci_err(dev, "PCIe Bus Error: severity=%s, type=%s, (%s)\n",
545 aer_error_severity_string[info->severity],
546 aer_error_layer[layer], aer_agent_string[agent]);
547
548 pci_err(dev, " device [%04x:%04x] error status/mask=%08x/%08x\n",
549 dev->vendor, dev->device,
550 info->status, info->mask);
551
552 __aer_print_error(dev, info);
553
554 if (info->tlp_header_valid)
555 __print_tlp_header(dev, &info->tlp);
556
557out:
558 if (info->id && info->error_dev_num > 1 && info->id == id)
559 pci_err(dev, " Error of this Agent is reported first\n");
560
561 trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
562 info->severity, info->tlp_header_valid, &info->tlp);
563}
564
565static void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
566{
567 u8 bus = info->id >> 8;
568 u8 devfn = info->id & 0xff;
569
570 pci_info(dev, "AER: %s%s error received: %04x:%02x:%02x.%d\n",
571 info->multi_error_valid ? "Multiple " : "",
572 aer_error_severity_string[info->severity],
573 pci_domain_nr(dev->bus), bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
574}
575
576#ifdef CONFIG_ACPI_APEI_PCIEAER
577int cper_severity_to_aer(int cper_severity)
578{
579 switch (cper_severity) {
580 case CPER_SEV_RECOVERABLE:
581 return AER_NONFATAL;
582 case CPER_SEV_FATAL:
583 return AER_FATAL;
584 default:
585 return AER_CORRECTABLE;
586 }
587}
588EXPORT_SYMBOL_GPL(cper_severity_to_aer);
589
590void cper_print_aer(struct pci_dev *dev, int aer_severity,
591 struct aer_capability_regs *aer)
592{
593 int layer, agent, tlp_header_valid = 0;
594 u32 status, mask;
595 struct aer_err_info info;
596
597 if (aer_severity == AER_CORRECTABLE) {
598 status = aer->cor_status;
599 mask = aer->cor_mask;
600 } else {
601 status = aer->uncor_status;
602 mask = aer->uncor_mask;
603 tlp_header_valid = status & AER_LOG_TLP_MASKS;
604 }
605
606 layer = AER_GET_LAYER_ERROR(aer_severity, status);
607 agent = AER_GET_AGENT(aer_severity, status);
608
609 memset(&info, 0, sizeof(info));
610 info.severity = aer_severity;
611 info.status = status;
612 info.mask = mask;
613 info.first_error = PCI_ERR_CAP_FEP(aer->cap_control);
614
615 pci_err(dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
616 __aer_print_error(dev, &info);
617 pci_err(dev, "aer_layer=%s, aer_agent=%s\n",
618 aer_error_layer[layer], aer_agent_string[agent]);
619
620 if (aer_severity != AER_CORRECTABLE)
621 pci_err(dev, "aer_uncor_severity: 0x%08x\n",
622 aer->uncor_severity);
623
624 if (tlp_header_valid)
625 __print_tlp_header(dev, &aer->header_log);
626
627 trace_aer_event(dev_name(&dev->dev), (status & ~mask),
628 aer_severity, tlp_header_valid, &aer->header_log);
629}
630#endif
631
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500632/**
633 * add_error_device - list device to be handled
634 * @e_info: pointer to error info
635 * @dev: pointer to pci_dev to be added
636 */
637static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
638{
639 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
640 e_info->dev[e_info->error_dev_num] = dev;
641 e_info->error_dev_num++;
642 return 0;
643 }
644 return -ENOSPC;
645}
646
647/**
648 * is_error_source - check whether the device is source of reported error
649 * @dev: pointer to pci_dev to be checked
650 * @e_info: pointer to reported error info
651 */
652static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
653{
654 int pos;
655 u32 status, mask;
656 u16 reg16;
657
658 /*
659 * When bus id is equal to 0, it might be a bad id
660 * reported by root port.
661 */
662 if ((PCI_BUS_NUM(e_info->id) != 0) &&
663 !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
664 /* Device ID match? */
665 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
666 return true;
667
668 /* Continue id comparing if there is no multiple error */
669 if (!e_info->multi_error_valid)
670 return false;
671 }
672
673 /*
674 * When either
675 * 1) bus id is equal to 0. Some ports might lose the bus
676 * id of error source id;
677 * 2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
678 * 3) There are multiple errors and prior ID comparing fails;
679 * We check AER status registers to find possible reporter.
680 */
681 if (atomic_read(&dev->enable_cnt) == 0)
682 return false;
683
684 /* Check if AER is enabled */
685 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
686 if (!(reg16 & PCI_EXP_AER_FLAGS))
687 return false;
688
689 pos = dev->aer_cap;
690 if (!pos)
691 return false;
692
693 /* Check if error is recorded */
694 if (e_info->severity == AER_CORRECTABLE) {
695 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
696 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
697 } else {
698 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
699 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
700 }
701 if (status & ~mask)
702 return true;
703
704 return false;
705}
706
707static int find_device_iter(struct pci_dev *dev, void *data)
708{
709 struct aer_err_info *e_info = (struct aer_err_info *)data;
710
711 if (is_error_source(dev, e_info)) {
712 /* List this device */
713 if (add_error_device(e_info, dev)) {
714 /* We cannot handle more... Stop iteration */
715 /* TODO: Should print error message here? */
716 return 1;
717 }
718
719 /* If there is only a single error, stop iteration */
720 if (!e_info->multi_error_valid)
721 return 1;
722 }
723 return 0;
724}
725
726/**
727 * find_source_device - search through device hierarchy for source device
728 * @parent: pointer to Root Port pci_dev data structure
729 * @e_info: including detailed error information such like id
730 *
731 * Return true if found.
732 *
733 * Invoked by DPC when error is detected at the Root Port.
734 * Caller of this function must set id, severity, and multi_error_valid of
735 * struct aer_err_info pointed by @e_info properly. This function must fill
736 * e_info->error_dev_num and e_info->dev[], based on the given information.
737 */
738static bool find_source_device(struct pci_dev *parent,
739 struct aer_err_info *e_info)
740{
741 struct pci_dev *dev = parent;
742 int result;
743
744 /* Must reset in this function */
745 e_info->error_dev_num = 0;
746
747 /* Is Root Port an agent that sends error message? */
748 result = find_device_iter(dev, e_info);
749 if (result)
750 return true;
751
752 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
753
754 if (!e_info->error_dev_num) {
755 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
756 e_info->id);
757 return false;
758 }
759 return true;
760}
761
762/**
763 * handle_error_source - handle logging error into an event log
764 * @dev: pointer to pci_dev data structure of error source device
765 * @info: comprehensive error information
766 *
767 * Invoked when an error being detected by Root Port.
768 */
769static void handle_error_source(struct pci_dev *dev, struct aer_err_info *info)
770{
771 int pos;
772
773 if (info->severity == AER_CORRECTABLE) {
774 /*
775 * Correctable error does not need software intervention.
776 * No need to go through error recovery process.
777 */
778 pos = dev->aer_cap;
779 if (pos)
780 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
781 info->status);
782 } else if (info->severity == AER_NONFATAL)
783 pcie_do_nonfatal_recovery(dev);
784 else if (info->severity == AER_FATAL)
785 pcie_do_fatal_recovery(dev, PCIE_PORT_SERVICE_AER);
786}
787
788#ifdef CONFIG_ACPI_APEI_PCIEAER
789
790#define AER_RECOVER_RING_ORDER 4
791#define AER_RECOVER_RING_SIZE (1 << AER_RECOVER_RING_ORDER)
792
793struct aer_recover_entry {
794 u8 bus;
795 u8 devfn;
796 u16 domain;
797 int severity;
798 struct aer_capability_regs *regs;
799};
800
801static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
802 AER_RECOVER_RING_SIZE);
803
804static void aer_recover_work_func(struct work_struct *work)
805{
806 struct aer_recover_entry entry;
807 struct pci_dev *pdev;
808
809 while (kfifo_get(&aer_recover_ring, &entry)) {
810 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
811 entry.devfn);
812 if (!pdev) {
813 pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
814 entry.domain, entry.bus,
815 PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
816 continue;
817 }
818 cper_print_aer(pdev, entry.severity, entry.regs);
819 if (entry.severity == AER_NONFATAL)
820 pcie_do_nonfatal_recovery(pdev);
821 else if (entry.severity == AER_FATAL)
822 pcie_do_fatal_recovery(pdev, PCIE_PORT_SERVICE_AER);
823 pci_dev_put(pdev);
824 }
825}
826
827/*
828 * Mutual exclusion for writers of aer_recover_ring, reader side don't
829 * need lock, because there is only one reader and lock is not needed
830 * between reader and writer.
831 */
832static DEFINE_SPINLOCK(aer_recover_ring_lock);
833static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
834
835void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
836 int severity, struct aer_capability_regs *aer_regs)
837{
838 unsigned long flags;
839 struct aer_recover_entry entry = {
840 .bus = bus,
841 .devfn = devfn,
842 .domain = domain,
843 .severity = severity,
844 .regs = aer_regs,
845 };
846
847 spin_lock_irqsave(&aer_recover_ring_lock, flags);
848 if (kfifo_put(&aer_recover_ring, entry))
849 schedule_work(&aer_recover_work);
850 else
851 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
852 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
853 spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
854}
855EXPORT_SYMBOL_GPL(aer_recover_queue);
856#endif
857
858/**
Keith Busch1e451162018-07-19 16:16:55 -0500859 * aer_get_device_error_info - read error status from dev and store it to info
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500860 * @dev: pointer to the device expected to have a error record
861 * @info: pointer to structure to store the error record
862 *
863 * Return 1 on success, 0 on error.
864 *
865 * Note that @info is reused among all error devices. Clear fields properly.
866 */
Keith Busch1e451162018-07-19 16:16:55 -0500867int aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500868{
869 int pos, temp;
870
871 /* Must reset in this function */
872 info->status = 0;
873 info->tlp_header_valid = 0;
874
875 pos = dev->aer_cap;
876
877 /* The device might not support AER */
878 if (!pos)
879 return 0;
880
881 if (info->severity == AER_CORRECTABLE) {
882 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
883 &info->status);
884 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
885 &info->mask);
886 if (!(info->status & ~info->mask))
887 return 0;
888 } else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
889 info->severity == AER_NONFATAL) {
890
891 /* Link is still healthy for IO reads */
892 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
893 &info->status);
894 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
895 &info->mask);
896 if (!(info->status & ~info->mask))
897 return 0;
898
899 /* Get First Error Pointer */
900 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
901 info->first_error = PCI_ERR_CAP_FEP(temp);
902
903 if (info->status & AER_LOG_TLP_MASKS) {
904 info->tlp_header_valid = 1;
905 pci_read_config_dword(dev,
906 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
907 pci_read_config_dword(dev,
908 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
909 pci_read_config_dword(dev,
910 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
911 pci_read_config_dword(dev,
912 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
913 }
914 }
915
916 return 1;
917}
918
919static inline void aer_process_err_devices(struct aer_err_info *e_info)
920{
921 int i;
922
923 /* Report all before handle them, not to lost records by reset etc. */
924 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -0500925 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500926 aer_print_error(e_info->dev[i], e_info);
927 }
928 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
Keith Busch1e451162018-07-19 16:16:55 -0500929 if (aer_get_device_error_info(e_info->dev[i], e_info))
Bjorn Helgaasfd3362c2018-06-08 08:33:39 -0500930 handle_error_source(e_info->dev[i], e_info);
931 }
932}
933
934/**
935 * aer_isr_one_error - consume an error detected by root port
936 * @rpc: pointer to the root port which holds an error
937 * @e_src: pointer to an error source
938 */
939static void aer_isr_one_error(struct aer_rpc *rpc,
940 struct aer_err_source *e_src)
941{
942 struct pci_dev *pdev = rpc->rpd;
943 struct aer_err_info *e_info = &rpc->e_info;
944
945 /*
946 * There is a possibility that both correctable error and
947 * uncorrectable error being logged. Report correctable error first.
948 */
949 if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
950 e_info->id = ERR_COR_ID(e_src->id);
951 e_info->severity = AER_CORRECTABLE;
952
953 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
954 e_info->multi_error_valid = 1;
955 else
956 e_info->multi_error_valid = 0;
957 aer_print_port_info(pdev, e_info);
958
959 if (find_source_device(pdev, e_info))
960 aer_process_err_devices(e_info);
961 }
962
963 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
964 e_info->id = ERR_UNCOR_ID(e_src->id);
965
966 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
967 e_info->severity = AER_FATAL;
968 else
969 e_info->severity = AER_NONFATAL;
970
971 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
972 e_info->multi_error_valid = 1;
973 else
974 e_info->multi_error_valid = 0;
975
976 aer_print_port_info(pdev, e_info);
977
978 if (find_source_device(pdev, e_info))
979 aer_process_err_devices(e_info);
980 }
981}
982
983/**
984 * get_e_source - retrieve an error source
985 * @rpc: pointer to the root port which holds an error
986 * @e_src: pointer to store retrieved error source
987 *
988 * Return 1 if an error source is retrieved, otherwise 0.
989 *
990 * Invoked by DPC handler to consume an error.
991 */
992static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
993{
994 unsigned long flags;
995
996 /* Lock access to Root error producer/consumer index */
997 spin_lock_irqsave(&rpc->e_lock, flags);
998 if (rpc->prod_idx == rpc->cons_idx) {
999 spin_unlock_irqrestore(&rpc->e_lock, flags);
1000 return 0;
1001 }
1002
1003 *e_src = rpc->e_sources[rpc->cons_idx];
1004 rpc->cons_idx++;
1005 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
1006 rpc->cons_idx = 0;
1007 spin_unlock_irqrestore(&rpc->e_lock, flags);
1008
1009 return 1;
1010}
1011
1012/**
1013 * aer_isr - consume errors detected by root port
1014 * @work: definition of this work item
1015 *
1016 * Invoked, as DPC, when root port records new detected error
1017 */
1018static void aer_isr(struct work_struct *work)
1019{
1020 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
1021 struct aer_err_source uninitialized_var(e_src);
1022
1023 mutex_lock(&rpc->rpc_mutex);
1024 while (get_e_source(rpc, &e_src))
1025 aer_isr_one_error(rpc, &e_src);
1026 mutex_unlock(&rpc->rpc_mutex);
1027}
1028
Bjorn Helgaas3c43a642018-06-08 08:31:57 -05001029/**
1030 * aer_irq - Root Port's ISR
1031 * @irq: IRQ assigned to Root Port
1032 * @context: pointer to Root Port data structure
1033 *
1034 * Invoked when Root Port detects AER messages.
1035 */
1036irqreturn_t aer_irq(int irq, void *context)
1037{
1038 unsigned int status, id;
1039 struct pcie_device *pdev = (struct pcie_device *)context;
1040 struct aer_rpc *rpc = get_service_data(pdev);
1041 int next_prod_idx;
1042 unsigned long flags;
1043 int pos;
1044
1045 pos = pdev->port->aer_cap;
1046 /*
1047 * Must lock access to Root Error Status Reg, Root Error ID Reg,
1048 * and Root error producer/consumer index
1049 */
1050 spin_lock_irqsave(&rpc->e_lock, flags);
1051
1052 /* Read error status */
1053 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, &status);
1054 if (!(status & (PCI_ERR_ROOT_UNCOR_RCV|PCI_ERR_ROOT_COR_RCV))) {
1055 spin_unlock_irqrestore(&rpc->e_lock, flags);
1056 return IRQ_NONE;
1057 }
1058
1059 /* Read error source and clear error status */
1060 pci_read_config_dword(pdev->port, pos + PCI_ERR_ROOT_ERR_SRC, &id);
1061 pci_write_config_dword(pdev->port, pos + PCI_ERR_ROOT_STATUS, status);
1062
1063 /* Store error source for later DPC handler */
1064 next_prod_idx = rpc->prod_idx + 1;
1065 if (next_prod_idx == AER_ERROR_SOURCES_MAX)
1066 next_prod_idx = 0;
1067 if (next_prod_idx == rpc->cons_idx) {
1068 /*
1069 * Error Storm Condition - possibly the same error occurred.
1070 * Drop the error.
1071 */
1072 spin_unlock_irqrestore(&rpc->e_lock, flags);
1073 return IRQ_HANDLED;
1074 }
1075 rpc->e_sources[rpc->prod_idx].status = status;
1076 rpc->e_sources[rpc->prod_idx].id = id;
1077 rpc->prod_idx = next_prod_idx;
1078 spin_unlock_irqrestore(&rpc->e_lock, flags);
1079
1080 /* Invoke DPC handler */
1081 schedule_work(&rpc->dpc_handler);
1082
1083 return IRQ_HANDLED;
1084}
1085EXPORT_SYMBOL_GPL(aer_irq);
1086
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001087static int set_device_error_reporting(struct pci_dev *dev, void *data)
1088{
1089 bool enable = *((bool *)data);
Yijing Wang62f87c02012-07-24 17:20:03 +08001090 int type = pci_pcie_type(dev);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001091
Yijing Wang62f87c02012-07-24 17:20:03 +08001092 if ((type == PCI_EXP_TYPE_ROOT_PORT) ||
1093 (type == PCI_EXP_TYPE_UPSTREAM) ||
1094 (type == PCI_EXP_TYPE_DOWNSTREAM)) {
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001095 if (enable)
1096 pci_enable_pcie_error_reporting(dev);
1097 else
1098 pci_disable_pcie_error_reporting(dev);
1099 }
1100
1101 if (enable)
1102 pcie_set_ecrc_checking(dev);
1103
1104 return 0;
1105}
1106
1107/**
1108 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
1109 * @dev: pointer to root port's pci_dev data structure
1110 * @enable: true = enable error reporting, false = disable error reporting.
1111 */
1112static void set_downstream_devices_error_reporting(struct pci_dev *dev,
1113 bool enable)
1114{
1115 set_device_error_reporting(dev, &enable);
1116
1117 if (!dev->subordinate)
1118 return;
1119 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
1120}
1121
1122/**
1123 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
1124 * @rpc: pointer to a Root Port data structure
1125 *
1126 * Invoked when PCIe bus loads AER service driver.
1127 */
1128static void aer_enable_rootport(struct aer_rpc *rpc)
1129{
Keith Busche13d17f2018-04-09 16:04:42 -06001130 struct pci_dev *pdev = rpc->rpd;
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001131 int aer_pos;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001132 u16 reg16;
1133 u32 reg32;
1134
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001135 /* Clear PCIe Capability's Device Status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001136 pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
1137 pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001138
1139 /* Disable system error generation in response to error messages */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001140 pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
1141 SYSTEM_ERROR_INTR_ON_MESG_MASK);
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001142
Keith Busch66b80802016-09-27 16:23:34 -04001143 aer_pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001144 /* Clear error status */
1145 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
1146 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
1147 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
1148 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
1149 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
1150 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
1151
1152 /*
1153 * Enable error reporting for the root port device and downstream port
1154 * devices.
1155 */
1156 set_downstream_devices_error_reporting(pdev, true);
1157
1158 /* Enable Root Port's interrupt in response to error messages */
1159 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, &reg32);
1160 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1161 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_COMMAND, reg32);
1162}
1163
1164/**
1165 * aer_disable_rootport - disable Root Port's interrupts when receiving messages
1166 * @rpc: pointer to a Root Port data structure
1167 *
1168 * Invoked when PCIe bus unloads AER service driver.
1169 */
1170static void aer_disable_rootport(struct aer_rpc *rpc)
1171{
Keith Busche13d17f2018-04-09 16:04:42 -06001172 struct pci_dev *pdev = rpc->rpd;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001173 u32 reg32;
1174 int pos;
1175
1176 /*
1177 * Disable error reporting for the root port device and downstream port
1178 * devices.
1179 */
1180 set_downstream_devices_error_reporting(pdev, false);
1181
Keith Busch66b80802016-09-27 16:23:34 -04001182 pos = pdev->aer_cap;
Hidetoshi Seto843f4692010-04-15 13:10:53 +09001183 /* Disable Root's interrupt in response to error messages */
1184 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1185 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1186 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, reg32);
1187
1188 /* Clear Root's error status reg */
1189 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1190 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
1191}
1192
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001193/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001194 * aer_alloc_rpc - allocate Root Port data structure
1195 * @dev: pointer to the pcie_dev data structure
1196 *
1197 * Invoked when Root Port's AER service is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001198 */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001199static struct aer_rpc *aer_alloc_rpc(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001200{
1201 struct aer_rpc *rpc;
1202
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001203 rpc = kzalloc(sizeof(struct aer_rpc), GFP_KERNEL);
1204 if (!rpc)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001205 return NULL;
1206
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001207 /* Initialize Root lock access, e_lock, to Root Error Status Reg */
Milind Arun Choudharyf5609d72007-07-09 11:55:54 -07001208 spin_lock_init(&rpc->e_lock);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001209
Keith Busche13d17f2018-04-09 16:04:42 -06001210 rpc->rpd = dev->port;
David Howells65f27f32006-11-22 14:55:48 +00001211 INIT_WORK(&rpc->dpc_handler, aer_isr);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001212 mutex_init(&rpc->rpc_mutex);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001213
Stefan Assmann45e829e2009-12-03 06:49:24 -05001214 /* Use PCIe bus function to store rpc into PCIe device */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001215 set_service_data(dev, rpc);
1216
1217 return rpc;
1218}
1219
1220/**
1221 * aer_remove - clean up resources
1222 * @dev: pointer to the pcie_dev data structure
1223 *
1224 * Invoked when PCI Express bus unloads or AER probe fails.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001225 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001226static void aer_remove(struct pcie_device *dev)
1227{
1228 struct aer_rpc *rpc = get_service_data(dev);
1229
1230 if (rpc) {
1231 /* If register interrupt service, it must be free. */
1232 if (rpc->isr)
1233 free_irq(dev->irq, dev);
1234
Sebastian Andrzej Siewior4ae21822016-01-25 10:08:00 -06001235 flush_work(&rpc->dpc_handler);
Hidetoshi Seto460d2982010-04-15 13:10:03 +09001236 aer_disable_rootport(rpc);
1237 kfree(rpc);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001238 set_service_data(dev, NULL);
1239 }
1240}
1241
1242/**
1243 * aer_probe - initialize resources
1244 * @dev: pointer to the pcie_dev data structure
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001245 *
1246 * Invoked when PCI Express bus loads AER service driver.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001247 */
Bill Pemberton15856ad2012-11-21 15:35:00 -05001248static int aer_probe(struct pcie_device *dev)
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001249{
1250 int status;
1251 struct aer_rpc *rpc;
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001252 struct device *device = &dev->port->dev;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001253
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001254 /* Alloc rpc data structure */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001255 rpc = aer_alloc_rpc(dev);
1256 if (!rpc) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001257 dev_printk(KERN_DEBUG, device, "alloc AER rpc failed\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001258 aer_remove(dev);
1259 return -ENOMEM;
1260 }
1261
1262 /* Request IRQ ISR */
Hidetoshi Setoc9a91882009-09-07 17:07:29 +09001263 status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", dev);
1264 if (status) {
Bjorn Helgaas576700b2016-11-21 15:24:25 -06001265 dev_printk(KERN_DEBUG, device, "request AER IRQ %d failed\n",
1266 dev->irq);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001267 aer_remove(dev);
1268 return status;
1269 }
1270
1271 rpc->isr = 1;
1272
1273 aer_enable_rootport(rpc);
Bjorn Helgaas68a55ae2016-11-21 15:34:02 -06001274 dev_info(device, "AER enabled with IRQ %d\n", dev->irq);
1275 return 0;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001276}
1277
1278/**
1279 * aer_root_reset - reset link on Root Port
1280 * @dev: pointer to Root Port's pci_dev data structure
1281 *
1282 * Invoked by Port Bus driver when performing link reset at Root Port.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001283 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001284static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
1285{
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001286 u32 reg32;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001287 int pos;
1288
Keith Busch66b80802016-09-27 16:23:34 -04001289 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001290
1291 /* Disable Root's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001292 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1293 reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
1294 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001295
Alex Williamson1b95ce82013-08-08 14:10:20 -06001296 pci_reset_bridge_secondary_bus(dev);
Frederick Lawler7506dc72018-01-18 12:55:24 -06001297 pci_printk(KERN_DEBUG, dev, "Root Port link has been reset\n");
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001298
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001299 /* Clear Root Error Status */
1300 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &reg32);
1301 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, reg32);
1302
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001303 /* Enable Root Port's interrupt in response to error messages */
Hidetoshi Setoc6d34ed2010-04-15 13:09:13 +09001304 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, &reg32);
1305 reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
1306 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_COMMAND, reg32);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001307
1308 return PCI_ERS_RESULT_RECOVERED;
1309}
1310
1311/**
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001312 * aer_error_resume - clean up corresponding error status bits
1313 * @dev: pointer to Root Port's pci_dev data structure
1314 *
1315 * Invoked by Port Bus driver during nonfatal recovery.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001316 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001317static void aer_error_resume(struct pci_dev *dev)
1318{
1319 int pos;
1320 u32 status, mask;
1321 u16 reg16;
1322
1323 /* Clean up Root device status */
Jiang Liu43bd4ee2012-07-24 17:20:11 +08001324 pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &reg16);
1325 pcie_capability_write_word(dev, PCI_EXP_DEVSTA, reg16);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001326
1327 /* Clean AER Root Error Status */
Keith Busch66b80802016-09-27 16:23:34 -04001328 pos = dev->aer_cap;
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001329 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
1330 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
Oza Pawandeep7e9084b2018-05-17 16:44:13 -05001331 status &= ~mask; /* Clear corresponding nonfatal bits */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001332 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
1333}
1334
Bjorn Helgaas0054ca82018-06-08 08:31:42 -05001335static struct pcie_port_service_driver aerdriver = {
1336 .name = "aer",
1337 .port_type = PCI_EXP_TYPE_ROOT_PORT,
1338 .service = PCIE_PORT_SERVICE_AER,
1339
1340 .probe = aer_probe,
1341 .remove = aer_remove,
1342 .error_resume = aer_error_resume,
1343 .reset_link = aer_root_reset,
1344};
1345
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001346/**
1347 * aer_service_init - register AER root service driver
1348 *
1349 * Invoked when AER root service driver is loaded.
Hidetoshi Setof6d37802010-04-15 13:22:11 +09001350 */
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001351static int __init aer_service_init(void)
1352{
Rafael J. Wysockib22c3d82010-09-20 18:50:00 +02001353 if (!pci_aer_available() || aer_acpi_firmware_first())
Andi Kleen3e77a3f2009-09-16 22:40:22 +02001354 return -ENXIO;
Sam Ravnborgc1996c22007-02-27 10:22:00 +01001355 return pcie_port_service_register(&aerdriver);
Zhang, Yanmin6c2b3742006-07-31 15:21:33 +08001356}
Paul Gortmaker87563362016-08-24 16:57:46 -04001357device_initcall(aer_service_init);