Merge tag 'pci-v4.16-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-2.6-block.git] / drivers / pci / pcie / aer / aerdrv_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/pci/pcie/aer/aerdrv_core.c
4  *
5  * This file implements the core part of PCIe AER. When a PCIe
6  * error is delivered, an error message will be collected and printed to
7  * console, then, an error recovery procedure will be executed by following
8  * the PCI error recovery rules.
9  *
10  * Copyright (C) 2006 Intel Corp.
11  *      Tom Long Nguyen (tom.l.nguyen@intel.com)
12  *      Zhang Yanmin (yanmin.zhang@intel.com)
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/pm.h>
21 #include <linux/suspend.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/kfifo.h>
25 #include "aerdrv.h"
26
27 #define PCI_EXP_AER_FLAGS       (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
28                                  PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
29
30 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
31 {
32         if (pcie_aer_get_firmware_first(dev))
33                 return -EIO;
34
35         if (!dev->aer_cap)
36                 return -EIO;
37
38         return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
39 }
40 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
41
42 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
43 {
44         if (pcie_aer_get_firmware_first(dev))
45                 return -EIO;
46
47         return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
48                                           PCI_EXP_AER_FLAGS);
49 }
50 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
51
52 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
53 {
54         int pos;
55         u32 status;
56
57         pos = dev->aer_cap;
58         if (!pos)
59                 return -EIO;
60
61         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
62         if (status)
63                 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
64
65         return 0;
66 }
67 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
68
69 int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
70 {
71         int pos;
72         u32 status;
73         int port_type;
74
75         if (!pci_is_pcie(dev))
76                 return -ENODEV;
77
78         pos = dev->aer_cap;
79         if (!pos)
80                 return -EIO;
81
82         port_type = pci_pcie_type(dev);
83         if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
84                 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
85                 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
86         }
87
88         pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
89         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
90
91         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
92         pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
93
94         return 0;
95 }
96
97 int pci_aer_init(struct pci_dev *dev)
98 {
99         dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
100         return pci_cleanup_aer_error_status_regs(dev);
101 }
102
103 /**
104  * add_error_device - list device to be handled
105  * @e_info: pointer to error info
106  * @dev: pointer to pci_dev to be added
107  */
108 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
109 {
110         if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
111                 e_info->dev[e_info->error_dev_num] = dev;
112                 e_info->error_dev_num++;
113                 return 0;
114         }
115         return -ENOSPC;
116 }
117
118 /**
119  * is_error_source - check whether the device is source of reported error
120  * @dev: pointer to pci_dev to be checked
121  * @e_info: pointer to reported error info
122  */
123 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
124 {
125         int pos;
126         u32 status, mask;
127         u16 reg16;
128
129         /*
130          * When bus id is equal to 0, it might be a bad id
131          * reported by root port.
132          */
133         if ((PCI_BUS_NUM(e_info->id) != 0) &&
134             !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
135                 /* Device ID match? */
136                 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
137                         return true;
138
139                 /* Continue id comparing if there is no multiple error */
140                 if (!e_info->multi_error_valid)
141                         return false;
142         }
143
144         /*
145          * When either
146          *      1) bus id is equal to 0. Some ports might lose the bus
147          *              id of error source id;
148          *      2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
149          *      3) There are multiple errors and prior ID comparing fails;
150          * We check AER status registers to find possible reporter.
151          */
152         if (atomic_read(&dev->enable_cnt) == 0)
153                 return false;
154
155         /* Check if AER is enabled */
156         pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
157         if (!(reg16 & PCI_EXP_AER_FLAGS))
158                 return false;
159
160         pos = dev->aer_cap;
161         if (!pos)
162                 return false;
163
164         /* Check if error is recorded */
165         if (e_info->severity == AER_CORRECTABLE) {
166                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
167                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
168         } else {
169                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
170                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
171         }
172         if (status & ~mask)
173                 return true;
174
175         return false;
176 }
177
178 static int find_device_iter(struct pci_dev *dev, void *data)
179 {
180         struct aer_err_info *e_info = (struct aer_err_info *)data;
181
182         if (is_error_source(dev, e_info)) {
183                 /* List this device */
184                 if (add_error_device(e_info, dev)) {
185                         /* We cannot handle more... Stop iteration */
186                         /* TODO: Should print error message here? */
187                         return 1;
188                 }
189
190                 /* If there is only a single error, stop iteration */
191                 if (!e_info->multi_error_valid)
192                         return 1;
193         }
194         return 0;
195 }
196
197 /**
198  * find_source_device - search through device hierarchy for source device
199  * @parent: pointer to Root Port pci_dev data structure
200  * @e_info: including detailed error information such like id
201  *
202  * Return true if found.
203  *
204  * Invoked by DPC when error is detected at the Root Port.
205  * Caller of this function must set id, severity, and multi_error_valid of
206  * struct aer_err_info pointed by @e_info properly.  This function must fill
207  * e_info->error_dev_num and e_info->dev[], based on the given information.
208  */
209 static bool find_source_device(struct pci_dev *parent,
210                 struct aer_err_info *e_info)
211 {
212         struct pci_dev *dev = parent;
213         int result;
214
215         /* Must reset in this function */
216         e_info->error_dev_num = 0;
217
218         /* Is Root Port an agent that sends error message? */
219         result = find_device_iter(dev, e_info);
220         if (result)
221                 return true;
222
223         pci_walk_bus(parent->subordinate, find_device_iter, e_info);
224
225         if (!e_info->error_dev_num) {
226                 pci_printk(KERN_DEBUG, parent, "can't find device of ID%04x\n",
227                            e_info->id);
228                 return false;
229         }
230         return true;
231 }
232
233 static int report_error_detected(struct pci_dev *dev, void *data)
234 {
235         pci_ers_result_t vote;
236         const struct pci_error_handlers *err_handler;
237         struct aer_broadcast_data *result_data;
238         result_data = (struct aer_broadcast_data *) data;
239
240         device_lock(&dev->dev);
241         dev->error_state = result_data->state;
242
243         if (!dev->driver ||
244                 !dev->driver->err_handler ||
245                 !dev->driver->err_handler->error_detected) {
246                 if (result_data->state == pci_channel_io_frozen &&
247                         dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
248                         /*
249                          * In case of fatal recovery, if one of down-
250                          * stream device has no driver. We might be
251                          * unable to recover because a later insmod
252                          * of a driver for this device is unaware of
253                          * its hw state.
254                          */
255                         pci_printk(KERN_DEBUG, dev, "device has %s\n",
256                                    dev->driver ?
257                                    "no AER-aware driver" : "no driver");
258                 }
259
260                 /*
261                  * If there's any device in the subtree that does not
262                  * have an error_detected callback, returning
263                  * PCI_ERS_RESULT_NO_AER_DRIVER prevents calling of
264                  * the subsequent mmio_enabled/slot_reset/resume
265                  * callbacks of "any" device in the subtree. All the
266                  * devices in the subtree are left in the error state
267                  * without recovery.
268                  */
269
270                 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE)
271                         vote = PCI_ERS_RESULT_NO_AER_DRIVER;
272                 else
273                         vote = PCI_ERS_RESULT_NONE;
274         } else {
275                 err_handler = dev->driver->err_handler;
276                 vote = err_handler->error_detected(dev, result_data->state);
277                 pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
278         }
279
280         result_data->result = merge_result(result_data->result, vote);
281         device_unlock(&dev->dev);
282         return 0;
283 }
284
285 static int report_mmio_enabled(struct pci_dev *dev, void *data)
286 {
287         pci_ers_result_t vote;
288         const struct pci_error_handlers *err_handler;
289         struct aer_broadcast_data *result_data;
290         result_data = (struct aer_broadcast_data *) data;
291
292         device_lock(&dev->dev);
293         if (!dev->driver ||
294                 !dev->driver->err_handler ||
295                 !dev->driver->err_handler->mmio_enabled)
296                 goto out;
297
298         err_handler = dev->driver->err_handler;
299         vote = err_handler->mmio_enabled(dev);
300         result_data->result = merge_result(result_data->result, vote);
301 out:
302         device_unlock(&dev->dev);
303         return 0;
304 }
305
306 static int report_slot_reset(struct pci_dev *dev, void *data)
307 {
308         pci_ers_result_t vote;
309         const struct pci_error_handlers *err_handler;
310         struct aer_broadcast_data *result_data;
311         result_data = (struct aer_broadcast_data *) data;
312
313         device_lock(&dev->dev);
314         if (!dev->driver ||
315                 !dev->driver->err_handler ||
316                 !dev->driver->err_handler->slot_reset)
317                 goto out;
318
319         err_handler = dev->driver->err_handler;
320         vote = err_handler->slot_reset(dev);
321         result_data->result = merge_result(result_data->result, vote);
322 out:
323         device_unlock(&dev->dev);
324         return 0;
325 }
326
327 static int report_resume(struct pci_dev *dev, void *data)
328 {
329         const struct pci_error_handlers *err_handler;
330
331         device_lock(&dev->dev);
332         dev->error_state = pci_channel_io_normal;
333
334         if (!dev->driver ||
335                 !dev->driver->err_handler ||
336                 !dev->driver->err_handler->resume)
337                 goto out;
338
339         err_handler = dev->driver->err_handler;
340         err_handler->resume(dev);
341         pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
342 out:
343         device_unlock(&dev->dev);
344         return 0;
345 }
346
347 /**
348  * broadcast_error_message - handle message broadcast to downstream drivers
349  * @dev: pointer to from where in a hierarchy message is broadcasted down
350  * @state: error state
351  * @error_mesg: message to print
352  * @cb: callback to be broadcasted
353  *
354  * Invoked during error recovery process. Once being invoked, the content
355  * of error severity will be broadcasted to all downstream drivers in a
356  * hierarchy in question.
357  */
358 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
359         enum pci_channel_state state,
360         char *error_mesg,
361         int (*cb)(struct pci_dev *, void *))
362 {
363         struct aer_broadcast_data result_data;
364
365         pci_printk(KERN_DEBUG, dev, "broadcast %s message\n", error_mesg);
366         result_data.state = state;
367         if (cb == report_error_detected)
368                 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
369         else
370                 result_data.result = PCI_ERS_RESULT_RECOVERED;
371
372         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
373                 /*
374                  * If the error is reported by a bridge, we think this error
375                  * is related to the downstream link of the bridge, so we
376                  * do error recovery on all subordinates of the bridge instead
377                  * of the bridge and clear the error status of the bridge.
378                  */
379                 if (cb == report_error_detected)
380                         dev->error_state = state;
381                 pci_walk_bus(dev->subordinate, cb, &result_data);
382                 if (cb == report_resume) {
383                         pci_cleanup_aer_uncorrect_error_status(dev);
384                         dev->error_state = pci_channel_io_normal;
385                 }
386         } else {
387                 /*
388                  * If the error is reported by an end point, we think this
389                  * error is related to the upstream link of the end point.
390                  */
391                 if (state == pci_channel_io_normal)
392                         /*
393                          * the error is non fatal so the bus is ok, just invoke
394                          * the callback for the function that logged the error.
395                          */
396                         cb(dev, &result_data);
397                 else
398                         pci_walk_bus(dev->bus, cb, &result_data);
399         }
400
401         return result_data.result;
402 }
403
404 /**
405  * default_reset_link - default reset function
406  * @dev: pointer to pci_dev data structure
407  *
408  * Invoked when performing link reset on a Downstream Port or a
409  * Root Port with no aer driver.
410  */
411 static pci_ers_result_t default_reset_link(struct pci_dev *dev)
412 {
413         pci_reset_bridge_secondary_bus(dev);
414         pci_printk(KERN_DEBUG, dev, "downstream link has been reset\n");
415         return PCI_ERS_RESULT_RECOVERED;
416 }
417
418 static int find_aer_service_iter(struct device *device, void *data)
419 {
420         struct pcie_port_service_driver *service_driver, **drv;
421
422         drv = (struct pcie_port_service_driver **) data;
423
424         if (device->bus == &pcie_port_bus_type && device->driver) {
425                 service_driver = to_service_driver(device->driver);
426                 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
427                         *drv = service_driver;
428                         return 1;
429                 }
430         }
431
432         return 0;
433 }
434
435 static struct pcie_port_service_driver *find_aer_service(struct pci_dev *dev)
436 {
437         struct pcie_port_service_driver *drv = NULL;
438
439         device_for_each_child(&dev->dev, &drv, find_aer_service_iter);
440
441         return drv;
442 }
443
444 static pci_ers_result_t reset_link(struct pci_dev *dev)
445 {
446         struct pci_dev *udev;
447         pci_ers_result_t status;
448         struct pcie_port_service_driver *driver;
449
450         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
451                 /* Reset this port for all subordinates */
452                 udev = dev;
453         } else {
454                 /* Reset the upstream component (likely downstream port) */
455                 udev = dev->bus->self;
456         }
457
458         /* Use the aer driver of the component firstly */
459         driver = find_aer_service(udev);
460
461         if (driver && driver->reset_link) {
462                 status = driver->reset_link(udev);
463         } else if (udev->has_secondary_link) {
464                 status = default_reset_link(udev);
465         } else {
466                 pci_printk(KERN_DEBUG, dev, "no link-reset support at upstream device %s\n",
467                         pci_name(udev));
468                 return PCI_ERS_RESULT_DISCONNECT;
469         }
470
471         if (status != PCI_ERS_RESULT_RECOVERED) {
472                 pci_printk(KERN_DEBUG, dev, "link reset at upstream device %s failed\n",
473                         pci_name(udev));
474                 return PCI_ERS_RESULT_DISCONNECT;
475         }
476
477         return status;
478 }
479
480 /**
481  * do_recovery - handle nonfatal/fatal error recovery process
482  * @dev: pointer to a pci_dev data structure of agent detecting an error
483  * @severity: error severity type
484  *
485  * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
486  * error detected message to all downstream drivers within a hierarchy in
487  * question and return the returned code.
488  */
489 static void do_recovery(struct pci_dev *dev, int severity)
490 {
491         pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
492         enum pci_channel_state state;
493
494         if (severity == AER_FATAL)
495                 state = pci_channel_io_frozen;
496         else
497                 state = pci_channel_io_normal;
498
499         status = broadcast_error_message(dev,
500                         state,
501                         "error_detected",
502                         report_error_detected);
503
504         if (severity == AER_FATAL) {
505                 result = reset_link(dev);
506                 if (result != PCI_ERS_RESULT_RECOVERED)
507                         goto failed;
508         }
509
510         if (status == PCI_ERS_RESULT_CAN_RECOVER)
511                 status = broadcast_error_message(dev,
512                                 state,
513                                 "mmio_enabled",
514                                 report_mmio_enabled);
515
516         if (status == PCI_ERS_RESULT_NEED_RESET) {
517                 /*
518                  * TODO: Should call platform-specific
519                  * functions to reset slot before calling
520                  * drivers' slot_reset callbacks?
521                  */
522                 status = broadcast_error_message(dev,
523                                 state,
524                                 "slot_reset",
525                                 report_slot_reset);
526         }
527
528         if (status != PCI_ERS_RESULT_RECOVERED)
529                 goto failed;
530
531         broadcast_error_message(dev,
532                                 state,
533                                 "resume",
534                                 report_resume);
535
536         pci_info(dev, "AER: Device recovery successful\n");
537         return;
538
539 failed:
540         pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT);
541         /* TODO: Should kernel panic here? */
542         pci_info(dev, "AER: Device recovery failed\n");
543 }
544
545 /**
546  * handle_error_source - handle logging error into an event log
547  * @aerdev: pointer to pcie_device data structure of the root port
548  * @dev: pointer to pci_dev data structure of error source device
549  * @info: comprehensive error information
550  *
551  * Invoked when an error being detected by Root Port.
552  */
553 static void handle_error_source(struct pcie_device *aerdev,
554         struct pci_dev *dev,
555         struct aer_err_info *info)
556 {
557         int pos;
558
559         if (info->severity == AER_CORRECTABLE) {
560                 /*
561                  * Correctable error does not need software intervention.
562                  * No need to go through error recovery process.
563                  */
564                 pos = dev->aer_cap;
565                 if (pos)
566                         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
567                                         info->status);
568         } else
569                 do_recovery(dev, info->severity);
570 }
571
572 #ifdef CONFIG_ACPI_APEI_PCIEAER
573 static void aer_recover_work_func(struct work_struct *work);
574
575 #define AER_RECOVER_RING_ORDER          4
576 #define AER_RECOVER_RING_SIZE           (1 << AER_RECOVER_RING_ORDER)
577
578 struct aer_recover_entry {
579         u8      bus;
580         u8      devfn;
581         u16     domain;
582         int     severity;
583         struct aer_capability_regs *regs;
584 };
585
586 static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
587                     AER_RECOVER_RING_SIZE);
588 /*
589  * Mutual exclusion for writers of aer_recover_ring, reader side don't
590  * need lock, because there is only one reader and lock is not needed
591  * between reader and writer.
592  */
593 static DEFINE_SPINLOCK(aer_recover_ring_lock);
594 static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
595
596 void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
597                        int severity, struct aer_capability_regs *aer_regs)
598 {
599         unsigned long flags;
600         struct aer_recover_entry entry = {
601                 .bus            = bus,
602                 .devfn          = devfn,
603                 .domain         = domain,
604                 .severity       = severity,
605                 .regs           = aer_regs,
606         };
607
608         spin_lock_irqsave(&aer_recover_ring_lock, flags);
609         if (kfifo_put(&aer_recover_ring, entry))
610                 schedule_work(&aer_recover_work);
611         else
612                 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
613                        domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
614         spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
615 }
616 EXPORT_SYMBOL_GPL(aer_recover_queue);
617
618 static void aer_recover_work_func(struct work_struct *work)
619 {
620         struct aer_recover_entry entry;
621         struct pci_dev *pdev;
622
623         while (kfifo_get(&aer_recover_ring, &entry)) {
624                 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
625                                                    entry.devfn);
626                 if (!pdev) {
627                         pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
628                                entry.domain, entry.bus,
629                                PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
630                         continue;
631                 }
632                 cper_print_aer(pdev, entry.severity, entry.regs);
633                 if (entry.severity != AER_CORRECTABLE)
634                         do_recovery(pdev, entry.severity);
635                 pci_dev_put(pdev);
636         }
637 }
638 #endif
639
640 /**
641  * get_device_error_info - read error status from dev and store it to info
642  * @dev: pointer to the device expected to have a error record
643  * @info: pointer to structure to store the error record
644  *
645  * Return 1 on success, 0 on error.
646  *
647  * Note that @info is reused among all error devices. Clear fields properly.
648  */
649 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
650 {
651         int pos, temp;
652
653         /* Must reset in this function */
654         info->status = 0;
655         info->tlp_header_valid = 0;
656
657         pos = dev->aer_cap;
658
659         /* The device might not support AER */
660         if (!pos)
661                 return 0;
662
663         if (info->severity == AER_CORRECTABLE) {
664                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
665                         &info->status);
666                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
667                         &info->mask);
668                 if (!(info->status & ~info->mask))
669                         return 0;
670         } else if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
671                 info->severity == AER_NONFATAL) {
672
673                 /* Link is still healthy for IO reads */
674                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
675                         &info->status);
676                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
677                         &info->mask);
678                 if (!(info->status & ~info->mask))
679                         return 0;
680
681                 /* Get First Error Pointer */
682                 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
683                 info->first_error = PCI_ERR_CAP_FEP(temp);
684
685                 if (info->status & AER_LOG_TLP_MASKS) {
686                         info->tlp_header_valid = 1;
687                         pci_read_config_dword(dev,
688                                 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
689                         pci_read_config_dword(dev,
690                                 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
691                         pci_read_config_dword(dev,
692                                 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
693                         pci_read_config_dword(dev,
694                                 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
695                 }
696         }
697
698         return 1;
699 }
700
701 static inline void aer_process_err_devices(struct pcie_device *p_device,
702                         struct aer_err_info *e_info)
703 {
704         int i;
705
706         /* Report all before handle them, not to lost records by reset etc. */
707         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
708                 if (get_device_error_info(e_info->dev[i], e_info))
709                         aer_print_error(e_info->dev[i], e_info);
710         }
711         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
712                 if (get_device_error_info(e_info->dev[i], e_info))
713                         handle_error_source(p_device, e_info->dev[i], e_info);
714         }
715 }
716
717 /**
718  * aer_isr_one_error - consume an error detected by root port
719  * @p_device: pointer to error root port service device
720  * @e_src: pointer to an error source
721  */
722 static void aer_isr_one_error(struct pcie_device *p_device,
723                 struct aer_err_source *e_src)
724 {
725         struct aer_rpc *rpc = get_service_data(p_device);
726         struct aer_err_info *e_info = &rpc->e_info;
727
728         /*
729          * There is a possibility that both correctable error and
730          * uncorrectable error being logged. Report correctable error first.
731          */
732         if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
733                 e_info->id = ERR_COR_ID(e_src->id);
734                 e_info->severity = AER_CORRECTABLE;
735
736                 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
737                         e_info->multi_error_valid = 1;
738                 else
739                         e_info->multi_error_valid = 0;
740
741                 aer_print_port_info(p_device->port, e_info);
742
743                 if (find_source_device(p_device->port, e_info))
744                         aer_process_err_devices(p_device, e_info);
745         }
746
747         if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
748                 e_info->id = ERR_UNCOR_ID(e_src->id);
749
750                 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
751                         e_info->severity = AER_FATAL;
752                 else
753                         e_info->severity = AER_NONFATAL;
754
755                 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
756                         e_info->multi_error_valid = 1;
757                 else
758                         e_info->multi_error_valid = 0;
759
760                 aer_print_port_info(p_device->port, e_info);
761
762                 if (find_source_device(p_device->port, e_info))
763                         aer_process_err_devices(p_device, e_info);
764         }
765 }
766
767 /**
768  * get_e_source - retrieve an error source
769  * @rpc: pointer to the root port which holds an error
770  * @e_src: pointer to store retrieved error source
771  *
772  * Return 1 if an error source is retrieved, otherwise 0.
773  *
774  * Invoked by DPC handler to consume an error.
775  */
776 static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
777 {
778         unsigned long flags;
779
780         /* Lock access to Root error producer/consumer index */
781         spin_lock_irqsave(&rpc->e_lock, flags);
782         if (rpc->prod_idx == rpc->cons_idx) {
783                 spin_unlock_irqrestore(&rpc->e_lock, flags);
784                 return 0;
785         }
786
787         *e_src = rpc->e_sources[rpc->cons_idx];
788         rpc->cons_idx++;
789         if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
790                 rpc->cons_idx = 0;
791         spin_unlock_irqrestore(&rpc->e_lock, flags);
792
793         return 1;
794 }
795
796 /**
797  * aer_isr - consume errors detected by root port
798  * @work: definition of this work item
799  *
800  * Invoked, as DPC, when root port records new detected error
801  */
802 void aer_isr(struct work_struct *work)
803 {
804         struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
805         struct pcie_device *p_device = rpc->rpd;
806         struct aer_err_source uninitialized_var(e_src);
807
808         mutex_lock(&rpc->rpc_mutex);
809         while (get_e_source(rpc, &e_src))
810                 aer_isr_one_error(p_device, &e_src);
811         mutex_unlock(&rpc->rpc_mutex);
812 }