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