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