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