Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[sfrench/cifs-2.6.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 "aerdrv.h"
27
28 static int forceload;
29 static int nosourceid;
30 module_param(forceload, bool, 0);
31 module_param(nosourceid, bool, 0);
32
33 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
34 {
35         u16 reg16 = 0;
36         int pos;
37
38         if (dev->aer_firmware_first)
39                 return -EIO;
40
41         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
42         if (!pos)
43                 return -EIO;
44
45         pos = pci_pcie_cap(dev);
46         if (!pos)
47                 return -EIO;
48
49         pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
50         reg16 = reg16 |
51                 PCI_EXP_DEVCTL_CERE |
52                 PCI_EXP_DEVCTL_NFERE |
53                 PCI_EXP_DEVCTL_FERE |
54                 PCI_EXP_DEVCTL_URRE;
55         pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
56
57         return 0;
58 }
59 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
60
61 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
62 {
63         u16 reg16 = 0;
64         int pos;
65
66         if (dev->aer_firmware_first)
67                 return -EIO;
68
69         pos = pci_pcie_cap(dev);
70         if (!pos)
71                 return -EIO;
72
73         pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
74         reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
75                         PCI_EXP_DEVCTL_NFERE |
76                         PCI_EXP_DEVCTL_FERE |
77                         PCI_EXP_DEVCTL_URRE);
78         pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
79
80         return 0;
81 }
82 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
83
84 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
85 {
86         int pos;
87         u32 status;
88
89         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
90         if (!pos)
91                 return -EIO;
92
93         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
94         if (status)
95                 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
96
97         return 0;
98 }
99 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
100
101 static int set_device_error_reporting(struct pci_dev *dev, void *data)
102 {
103         bool enable = *((bool *)data);
104
105         if ((dev->pcie_type == PCI_EXP_TYPE_ROOT_PORT) ||
106             (dev->pcie_type == PCI_EXP_TYPE_UPSTREAM) ||
107             (dev->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)) {
108                 if (enable)
109                         pci_enable_pcie_error_reporting(dev);
110                 else
111                         pci_disable_pcie_error_reporting(dev);
112         }
113
114         if (enable)
115                 pcie_set_ecrc_checking(dev);
116
117         return 0;
118 }
119
120 /**
121  * set_downstream_devices_error_reporting - enable/disable the error reporting  bits on the root port and its downstream ports.
122  * @dev: pointer to root port's pci_dev data structure
123  * @enable: true = enable error reporting, false = disable error reporting.
124  */
125 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
126                                                    bool enable)
127 {
128         set_device_error_reporting(dev, &enable);
129
130         if (!dev->subordinate)
131                 return;
132         pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
133 }
134
135 static inline int compare_device_id(struct pci_dev *dev,
136                         struct aer_err_info *e_info)
137 {
138         if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
139                 /*
140                  * Device ID match
141                  */
142                 return 1;
143         }
144
145         return 0;
146 }
147
148 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
149 {
150         if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
151                 e_info->dev[e_info->error_dev_num] = dev;
152                 e_info->error_dev_num++;
153                 return 1;
154         }
155
156         return 0;
157 }
158
159
160 #define PCI_BUS(x)      (((x) >> 8) & 0xff)
161
162 static int find_device_iter(struct pci_dev *dev, void *data)
163 {
164         int pos;
165         u32 status;
166         u32 mask;
167         u16 reg16;
168         int result;
169         struct aer_err_info *e_info = (struct aer_err_info *)data;
170
171         /*
172          * When bus id is equal to 0, it might be a bad id
173          * reported by root port.
174          */
175         if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
176                 result = compare_device_id(dev, e_info);
177                 if (result)
178                         add_error_device(e_info, dev);
179
180                 /*
181                  * If there is no multiple error, we stop
182                  * or continue based on the id comparing.
183                  */
184                 if (!e_info->multi_error_valid)
185                         return result;
186
187                 /*
188                  * If there are multiple errors and id does match,
189                  * We need continue to search other devices under
190                  * the root port. Return 0 means that.
191                  */
192                 if (result)
193                         return 0;
194         }
195
196         /*
197          * When either
198          *      1) nosourceid==y;
199          *      2) bus id is equal to 0. Some ports might lose the bus
200          *              id of error source id;
201          *      3) There are multiple errors and prior id comparing fails;
202          * We check AER status registers to find the initial reporter.
203          */
204         if (atomic_read(&dev->enable_cnt) == 0)
205                 return 0;
206         pos = pci_pcie_cap(dev);
207         if (!pos)
208                 return 0;
209         /* Check if AER is enabled */
210         pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, &reg16);
211         if (!(reg16 & (
212                 PCI_EXP_DEVCTL_CERE |
213                 PCI_EXP_DEVCTL_NFERE |
214                 PCI_EXP_DEVCTL_FERE |
215                 PCI_EXP_DEVCTL_URRE)))
216                 return 0;
217         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
218         if (!pos)
219                 return 0;
220
221         status = 0;
222         mask = 0;
223         if (e_info->severity == AER_CORRECTABLE) {
224                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
225                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
226                 if (status & ~mask) {
227                         add_error_device(e_info, dev);
228                         goto added;
229                 }
230         } else {
231                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
232                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
233                 if (status & ~mask) {
234                         add_error_device(e_info, dev);
235                         goto added;
236                 }
237         }
238
239         return 0;
240
241 added:
242         if (e_info->multi_error_valid)
243                 return 0;
244         else
245                 return 1;
246 }
247
248 /**
249  * find_source_device - search through device hierarchy for source device
250  * @parent: pointer to Root Port pci_dev data structure
251  * @err_info: including detailed error information such like id
252  *
253  * Invoked when error is detected at the Root Port.
254  */
255 static void find_source_device(struct pci_dev *parent,
256                 struct aer_err_info *e_info)
257 {
258         struct pci_dev *dev = parent;
259         int result;
260
261         /* Is Root Port an agent that sends error message? */
262         result = find_device_iter(dev, e_info);
263         if (result)
264                 return;
265
266         pci_walk_bus(parent->subordinate, find_device_iter, e_info);
267 }
268
269 static int report_error_detected(struct pci_dev *dev, void *data)
270 {
271         pci_ers_result_t vote;
272         struct pci_error_handlers *err_handler;
273         struct aer_broadcast_data *result_data;
274         result_data = (struct aer_broadcast_data *) data;
275
276         dev->error_state = result_data->state;
277
278         if (!dev->driver ||
279                 !dev->driver->err_handler ||
280                 !dev->driver->err_handler->error_detected) {
281                 if (result_data->state == pci_channel_io_frozen &&
282                         !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
283                         /*
284                          * In case of fatal recovery, if one of down-
285                          * stream device has no driver. We might be
286                          * unable to recover because a later insmod
287                          * of a driver for this device is unaware of
288                          * its hw state.
289                          */
290                         dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
291                                    dev->driver ?
292                                    "no AER-aware driver" : "no driver");
293                 }
294                 return 0;
295         }
296
297         err_handler = dev->driver->err_handler;
298         vote = err_handler->error_detected(dev, result_data->state);
299         result_data->result = merge_result(result_data->result, vote);
300         return 0;
301 }
302
303 static int report_mmio_enabled(struct pci_dev *dev, void *data)
304 {
305         pci_ers_result_t vote;
306         struct pci_error_handlers *err_handler;
307         struct aer_broadcast_data *result_data;
308         result_data = (struct aer_broadcast_data *) data;
309
310         if (!dev->driver ||
311                 !dev->driver->err_handler ||
312                 !dev->driver->err_handler->mmio_enabled)
313                 return 0;
314
315         err_handler = dev->driver->err_handler;
316         vote = err_handler->mmio_enabled(dev);
317         result_data->result = merge_result(result_data->result, vote);
318         return 0;
319 }
320
321 static int report_slot_reset(struct pci_dev *dev, void *data)
322 {
323         pci_ers_result_t vote;
324         struct pci_error_handlers *err_handler;
325         struct aer_broadcast_data *result_data;
326         result_data = (struct aer_broadcast_data *) data;
327
328         if (!dev->driver ||
329                 !dev->driver->err_handler ||
330                 !dev->driver->err_handler->slot_reset)
331                 return 0;
332
333         err_handler = dev->driver->err_handler;
334         vote = err_handler->slot_reset(dev);
335         result_data->result = merge_result(result_data->result, vote);
336         return 0;
337 }
338
339 static int report_resume(struct pci_dev *dev, void *data)
340 {
341         struct pci_error_handlers *err_handler;
342
343         dev->error_state = pci_channel_io_normal;
344
345         if (!dev->driver ||
346                 !dev->driver->err_handler ||
347                 !dev->driver->err_handler->resume)
348                 return 0;
349
350         err_handler = dev->driver->err_handler;
351         err_handler->resume(dev);
352         return 0;
353 }
354
355 /**
356  * broadcast_error_message - handle message broadcast to downstream drivers
357  * @dev: pointer to from where in a hierarchy message is broadcasted down
358  * @state: error state
359  * @error_mesg: message to print
360  * @cb: callback to be broadcasted
361  *
362  * Invoked during error recovery process. Once being invoked, the content
363  * of error severity will be broadcasted to all downstream drivers in a
364  * hierarchy in question.
365  */
366 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
367         enum pci_channel_state state,
368         char *error_mesg,
369         int (*cb)(struct pci_dev *, void *))
370 {
371         struct aer_broadcast_data result_data;
372
373         dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
374         result_data.state = state;
375         if (cb == report_error_detected)
376                 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
377         else
378                 result_data.result = PCI_ERS_RESULT_RECOVERED;
379
380         if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
381                 /*
382                  * If the error is reported by a bridge, we think this error
383                  * is related to the downstream link of the bridge, so we
384                  * do error recovery on all subordinates of the bridge instead
385                  * of the bridge and clear the error status of the bridge.
386                  */
387                 if (cb == report_error_detected)
388                         dev->error_state = state;
389                 pci_walk_bus(dev->subordinate, cb, &result_data);
390                 if (cb == report_resume) {
391                         pci_cleanup_aer_uncorrect_error_status(dev);
392                         dev->error_state = pci_channel_io_normal;
393                 }
394         } else {
395                 /*
396                  * If the error is reported by an end point, we think this
397                  * error is related to the upstream link of the end point.
398                  */
399                 pci_walk_bus(dev->bus, cb, &result_data);
400         }
401
402         return result_data.result;
403 }
404
405 struct find_aer_service_data {
406         struct pcie_port_service_driver *aer_driver;
407         int is_downstream;
408 };
409
410 static int find_aer_service_iter(struct device *device, void *data)
411 {
412         struct device_driver *driver;
413         struct pcie_port_service_driver *service_driver;
414         struct find_aer_service_data *result;
415
416         result = (struct find_aer_service_data *) data;
417
418         if (device->bus == &pcie_port_bus_type) {
419                 struct pcie_device *pcie = to_pcie_device(device);
420
421                 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
422                         result->is_downstream = 1;
423
424                 driver = device->driver;
425                 if (driver) {
426                         service_driver = to_service_driver(driver);
427                         if (service_driver->service == PCIE_PORT_SERVICE_AER) {
428                                 result->aer_driver = service_driver;
429                                 return 1;
430                         }
431                 }
432         }
433
434         return 0;
435 }
436
437 static void find_aer_service(struct pci_dev *dev,
438                 struct find_aer_service_data *data)
439 {
440         int retval;
441         retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
442 }
443
444 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
445                 struct pci_dev *dev)
446 {
447         struct pci_dev *udev;
448         pci_ers_result_t status;
449         struct find_aer_service_data data;
450
451         if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
452                 udev = dev;
453         else
454                 udev = dev->bus->self;
455
456         data.is_downstream = 0;
457         data.aer_driver = NULL;
458         find_aer_service(udev, &data);
459
460         /*
461          * Use the aer driver of the error agent firstly.
462          * If it hasn't the aer driver, use the root port's
463          */
464         if (!data.aer_driver || !data.aer_driver->reset_link) {
465                 if (data.is_downstream &&
466                         aerdev->device.driver &&
467                         to_service_driver(aerdev->device.driver)->reset_link) {
468                         data.aer_driver =
469                                 to_service_driver(aerdev->device.driver);
470                 } else {
471                         dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
472                                    "support\n");
473                         return PCI_ERS_RESULT_DISCONNECT;
474                 }
475         }
476
477         status = data.aer_driver->reset_link(udev);
478         if (status != PCI_ERS_RESULT_RECOVERED) {
479                 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
480                            "device %s failed\n", pci_name(udev));
481                 return PCI_ERS_RESULT_DISCONNECT;
482         }
483
484         return status;
485 }
486
487 /**
488  * do_recovery - handle nonfatal/fatal error recovery process
489  * @aerdev: pointer to a pcie_device data structure of root port
490  * @dev: pointer to a pci_dev data structure of agent detecting an error
491  * @severity: error severity type
492  *
493  * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
494  * error detected message to all downstream drivers within a hierarchy in
495  * question and return the returned code.
496  */
497 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
498                 struct pci_dev *dev,
499                 int severity)
500 {
501         pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
502         enum pci_channel_state state;
503
504         if (severity == AER_FATAL)
505                 state = pci_channel_io_frozen;
506         else
507                 state = pci_channel_io_normal;
508
509         status = broadcast_error_message(dev,
510                         state,
511                         "error_detected",
512                         report_error_detected);
513
514         if (severity == AER_FATAL) {
515                 result = reset_link(aerdev, dev);
516                 if (result != PCI_ERS_RESULT_RECOVERED) {
517                         /* TODO: Should panic here? */
518                         return result;
519                 }
520         }
521
522         if (status == PCI_ERS_RESULT_CAN_RECOVER)
523                 status = broadcast_error_message(dev,
524                                 state,
525                                 "mmio_enabled",
526                                 report_mmio_enabled);
527
528         if (status == PCI_ERS_RESULT_NEED_RESET) {
529                 /*
530                  * TODO: Should call platform-specific
531                  * functions to reset slot before calling
532                  * drivers' slot_reset callbacks?
533                  */
534                 status = broadcast_error_message(dev,
535                                 state,
536                                 "slot_reset",
537                                 report_slot_reset);
538         }
539
540         if (status == PCI_ERS_RESULT_RECOVERED)
541                 broadcast_error_message(dev,
542                                 state,
543                                 "resume",
544                                 report_resume);
545
546         return status;
547 }
548
549 /**
550  * handle_error_source - handle logging error into an event log
551  * @aerdev: pointer to pcie_device data structure of the root port
552  * @dev: pointer to pci_dev data structure of error source device
553  * @info: comprehensive error information
554  *
555  * Invoked when an error being detected by Root Port.
556  */
557 static void handle_error_source(struct pcie_device *aerdev,
558         struct pci_dev *dev,
559         struct aer_err_info *info)
560 {
561         pci_ers_result_t status = 0;
562         int pos;
563
564         if (info->severity == AER_CORRECTABLE) {
565                 /*
566                  * Correctable error does not need software intevention.
567                  * No need to go through error recovery process.
568                  */
569                 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
570                 if (pos)
571                         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
572                                         info->status);
573         } else {
574                 status = do_recovery(aerdev, dev, info->severity);
575                 if (status == PCI_ERS_RESULT_RECOVERED) {
576                         dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
577                                    "successfully recovered\n");
578                 } else {
579                         /* TODO: Should kernel panic here? */
580                         dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
581                                    "recover\n");
582                 }
583         }
584 }
585
586 /**
587  * aer_enable_rootport - enable Root Port's interrupts when receiving messages
588  * @rpc: pointer to a Root Port data structure
589  *
590  * Invoked when PCIe bus loads AER service driver.
591  */
592 void aer_enable_rootport(struct aer_rpc *rpc)
593 {
594         struct pci_dev *pdev = rpc->rpd->port;
595         int pos, aer_pos;
596         u16 reg16;
597         u32 reg32;
598
599         pos = pci_pcie_cap(pdev);
600         /* Clear PCIe Capability's Device Status */
601         pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, &reg16);
602         pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
603
604         /* Disable system error generation in response to error messages */
605         pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, &reg16);
606         reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
607         pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
608
609         aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
610         /* Clear error status */
611         pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, &reg32);
612         pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
613         pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, &reg32);
614         pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
615         pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, &reg32);
616         pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
617
618         /*
619          * Enable error reporting for the root port device and downstream port
620          * devices.
621          */
622         set_downstream_devices_error_reporting(pdev, true);
623
624         /* Enable Root Port's interrupt in response to error messages */
625         pci_write_config_dword(pdev,
626                 aer_pos + PCI_ERR_ROOT_COMMAND,
627                 ROOT_PORT_INTR_ON_MESG_MASK);
628 }
629
630 /**
631  * disable_root_aer - disable Root Port's interrupts when receiving messages
632  * @rpc: pointer to a Root Port data structure
633  *
634  * Invoked when PCIe bus unloads AER service driver.
635  */
636 static void disable_root_aer(struct aer_rpc *rpc)
637 {
638         struct pci_dev *pdev = rpc->rpd->port;
639         u32 reg32;
640         int pos;
641
642         /*
643          * Disable error reporting for the root port device and downstream port
644          * devices.
645          */
646         set_downstream_devices_error_reporting(pdev, false);
647
648         pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
649         /* Disable Root's interrupt in response to error messages */
650         pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, 0);
651
652         /* Clear Root's error status reg */
653         pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, &reg32);
654         pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
655 }
656
657 /**
658  * get_e_source - retrieve an error source
659  * @rpc: pointer to the root port which holds an error
660  *
661  * Invoked by DPC handler to consume an error.
662  */
663 static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
664 {
665         struct aer_err_source *e_source;
666         unsigned long flags;
667
668         /* Lock access to Root error producer/consumer index */
669         spin_lock_irqsave(&rpc->e_lock, flags);
670         if (rpc->prod_idx == rpc->cons_idx) {
671                 spin_unlock_irqrestore(&rpc->e_lock, flags);
672                 return NULL;
673         }
674         e_source = &rpc->e_sources[rpc->cons_idx];
675         rpc->cons_idx++;
676         if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
677                 rpc->cons_idx = 0;
678         spin_unlock_irqrestore(&rpc->e_lock, flags);
679
680         return e_source;
681 }
682
683 /**
684  * get_device_error_info - read error status from dev and store it to info
685  * @dev: pointer to the device expected to have a error record
686  * @info: pointer to structure to store the error record
687  *
688  * Return 1 on success, 0 on error.
689  */
690 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
691 {
692         int pos, temp;
693
694         info->status = 0;
695         info->tlp_header_valid = 0;
696
697         pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
698
699         /* The device might not support AER */
700         if (!pos)
701                 return 1;
702
703         if (info->severity == AER_CORRECTABLE) {
704                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
705                         &info->status);
706                 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
707                         &info->mask);
708                 if (!(info->status & ~info->mask))
709                         return 0;
710         } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
711                 info->severity == AER_NONFATAL) {
712
713                 /* Link is still healthy for IO reads */
714                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
715                         &info->status);
716                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
717                         &info->mask);
718                 if (!(info->status & ~info->mask))
719                         return 0;
720
721                 /* Get First Error Pointer */
722                 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
723                 info->first_error = PCI_ERR_CAP_FEP(temp);
724
725                 if (info->status & AER_LOG_TLP_MASKS) {
726                         info->tlp_header_valid = 1;
727                         pci_read_config_dword(dev,
728                                 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
729                         pci_read_config_dword(dev,
730                                 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
731                         pci_read_config_dword(dev,
732                                 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
733                         pci_read_config_dword(dev,
734                                 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
735                 }
736         }
737
738         return 1;
739 }
740
741 static inline void aer_process_err_devices(struct pcie_device *p_device,
742                         struct aer_err_info *e_info)
743 {
744         int i;
745
746         if (!e_info->dev[0]) {
747                 dev_printk(KERN_DEBUG, &p_device->port->dev,
748                                 "can't find device of ID%04x\n",
749                                 e_info->id);
750         }
751
752         /* Report all before handle them, not to lost records by reset etc. */
753         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
754                 if (get_device_error_info(e_info->dev[i], e_info))
755                         aer_print_error(e_info->dev[i], e_info);
756         }
757         for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
758                 if (get_device_error_info(e_info->dev[i], e_info))
759                         handle_error_source(p_device, e_info->dev[i], e_info);
760         }
761 }
762
763 /**
764  * aer_isr_one_error - consume an error detected by root port
765  * @p_device: pointer to error root port service device
766  * @e_src: pointer to an error source
767  */
768 static void aer_isr_one_error(struct pcie_device *p_device,
769                 struct aer_err_source *e_src)
770 {
771         struct aer_err_info *e_info;
772         int i;
773
774         /* struct aer_err_info might be big, so we allocate it with slab */
775         e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
776         if (e_info == NULL) {
777                 dev_printk(KERN_DEBUG, &p_device->port->dev,
778                         "Can't allocate mem when processing AER errors\n");
779                 return;
780         }
781
782         /*
783          * There is a possibility that both correctable error and
784          * uncorrectable error being logged. Report correctable error first.
785          */
786         for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
787                 if (i > 4)
788                         break;
789                 if (!(e_src->status & i))
790                         continue;
791
792                 memset(e_info, 0, sizeof(struct aer_err_info));
793
794                 /* Init comprehensive error information */
795                 if (i & PCI_ERR_ROOT_COR_RCV) {
796                         e_info->id = ERR_COR_ID(e_src->id);
797                         e_info->severity = AER_CORRECTABLE;
798                 } else {
799                         e_info->id = ERR_UNCOR_ID(e_src->id);
800                         e_info->severity = ((e_src->status >> 6) & 1);
801                 }
802                 if (e_src->status &
803                         (PCI_ERR_ROOT_MULTI_COR_RCV |
804                          PCI_ERR_ROOT_MULTI_UNCOR_RCV))
805                         e_info->multi_error_valid = 1;
806
807                 aer_print_port_info(p_device->port, e_info);
808
809                 find_source_device(p_device->port, e_info);
810                 aer_process_err_devices(p_device, e_info);
811         }
812
813         kfree(e_info);
814 }
815
816 /**
817  * aer_isr - consume errors detected by root port
818  * @work: definition of this work item
819  *
820  * Invoked, as DPC, when root port records new detected error
821  */
822 void aer_isr(struct work_struct *work)
823 {
824         struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
825         struct pcie_device *p_device = rpc->rpd;
826         struct aer_err_source *e_src;
827
828         mutex_lock(&rpc->rpc_mutex);
829         e_src = get_e_source(rpc);
830         while (e_src) {
831                 aer_isr_one_error(p_device, e_src);
832                 e_src = get_e_source(rpc);
833         }
834         mutex_unlock(&rpc->rpc_mutex);
835
836         wake_up(&rpc->wait_release);
837 }
838
839 /**
840  * aer_delete_rootport - disable root port aer and delete service data
841  * @rpc: pointer to a root port device being deleted
842  *
843  * Invoked when AER service unloaded on a specific Root Port
844  */
845 void aer_delete_rootport(struct aer_rpc *rpc)
846 {
847         /* Disable root port AER itself */
848         disable_root_aer(rpc);
849
850         kfree(rpc);
851 }
852
853 /**
854  * aer_init - provide AER initialization
855  * @dev: pointer to AER pcie device
856  *
857  * Invoked when AER service driver is loaded.
858  */
859 int aer_init(struct pcie_device *dev)
860 {
861         if (dev->port->aer_firmware_first) {
862                 dev_printk(KERN_DEBUG, &dev->device,
863                            "PCIe errors handled by platform firmware.\n");
864                 goto out;
865         }
866
867         if (aer_osc_setup(dev))
868                 goto out;
869
870         return 0;
871 out:
872         if (forceload) {
873                 dev_printk(KERN_DEBUG, &dev->device,
874                            "aerdrv forceload requested.\n");
875                 dev->port->aer_firmware_first = 0;
876                 return 0;
877         }
878         return -ENXIO;
879 }