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