2 * drivers/pci/pcie/aer/aerdrv_core.c
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
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.
13 * Copyright (C) 2006 Intel Corp.
14 * Tom Long Nguyen (tom.l.nguyen@intel.com)
15 * Zhang Yanmin (yanmin.zhang@intel.com)
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
29 static int nosourceid;
30 module_param(forceload, bool, 0);
31 module_param(nosourceid, bool, 0);
33 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
38 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
42 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
46 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
49 PCI_EXP_DEVCTL_NFERE |
52 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
56 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
58 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
63 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
67 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
68 reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
69 PCI_EXP_DEVCTL_NFERE |
72 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
76 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
78 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
83 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
87 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
88 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
89 if (dev->error_state == pci_channel_io_normal)
90 status &= ~mask; /* Clear corresponding nonfatal bits */
92 status &= mask; /* Clear corresponding fatal bits */
93 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
97 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
100 int pci_cleanup_aer_correct_error_status(struct pci_dev *dev)
105 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
109 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
110 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
116 static int set_device_error_reporting(struct pci_dev *dev, void *data)
118 bool enable = *((bool *)data);
120 if (dev->pcie_type == PCIE_RC_PORT ||
121 dev->pcie_type == PCIE_SW_UPSTREAM_PORT ||
122 dev->pcie_type == PCIE_SW_DOWNSTREAM_PORT) {
124 pci_enable_pcie_error_reporting(dev);
126 pci_disable_pcie_error_reporting(dev);
130 pcie_set_ecrc_checking(dev);
136 * set_downstream_devices_error_reporting - enable/disable the error reporting bits on the root port and its downstream ports.
137 * @dev: pointer to root port's pci_dev data structure
138 * @enable: true = enable error reporting, false = disable error reporting.
140 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
143 set_device_error_reporting(dev, &enable);
145 if (!dev->subordinate)
147 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
150 static inline int compare_device_id(struct pci_dev *dev,
151 struct aer_err_info *e_info)
153 if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
163 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
165 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
166 e_info->dev[e_info->error_dev_num] = dev;
167 e_info->error_dev_num++;
175 #define PCI_BUS(x) (((x) >> 8) & 0xff)
177 static int find_device_iter(struct pci_dev *dev, void *data)
184 struct aer_err_info *e_info = (struct aer_err_info *)data;
187 * When bus id is equal to 0, it might be a bad id
188 * reported by root port.
190 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
191 result = compare_device_id(dev, e_info);
193 add_error_device(e_info, dev);
196 * If there is no multiple error, we stop
197 * or continue based on the id comparing.
199 if (!e_info->multi_error_valid)
203 * If there are multiple errors and id does match,
204 * We need continue to search other devices under
205 * the root port. Return 0 means that.
214 * 2) bus id is equal to 0. Some ports might lose the bus
215 * id of error source id;
216 * 3) There are multiple errors and prior id comparing fails;
217 * We check AER status registers to find the initial reporter.
219 if (atomic_read(&dev->enable_cnt) == 0)
221 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
224 /* Check if AER is enabled */
225 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
227 PCI_EXP_DEVCTL_CERE |
228 PCI_EXP_DEVCTL_NFERE |
229 PCI_EXP_DEVCTL_FERE |
230 PCI_EXP_DEVCTL_URRE)))
232 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
238 if (e_info->severity == AER_CORRECTABLE) {
239 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
240 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
241 if (status & ~mask) {
242 add_error_device(e_info, dev);
246 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
247 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
248 if (status & ~mask) {
249 add_error_device(e_info, dev);
257 if (e_info->multi_error_valid)
264 * find_source_device - search through device hierarchy for source device
265 * @parent: pointer to Root Port pci_dev data structure
266 * @err_info: including detailed error information such like id
268 * Invoked when error is detected at the Root Port.
270 static void find_source_device(struct pci_dev *parent,
271 struct aer_err_info *e_info)
273 struct pci_dev *dev = parent;
276 /* Is Root Port an agent that sends error message? */
277 result = find_device_iter(dev, e_info);
281 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
284 static int report_error_detected(struct pci_dev *dev, void *data)
286 pci_ers_result_t vote;
287 struct pci_error_handlers *err_handler;
288 struct aer_broadcast_data *result_data;
289 result_data = (struct aer_broadcast_data *) data;
291 dev->error_state = result_data->state;
294 !dev->driver->err_handler ||
295 !dev->driver->err_handler->error_detected) {
296 if (result_data->state == pci_channel_io_frozen &&
297 !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
299 * In case of fatal recovery, if one of down-
300 * stream device has no driver. We might be
301 * unable to recover because a later insmod
302 * of a driver for this device is unaware of
305 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
307 "no AER-aware driver" : "no driver");
312 err_handler = dev->driver->err_handler;
313 vote = err_handler->error_detected(dev, result_data->state);
314 result_data->result = merge_result(result_data->result, vote);
318 static int report_mmio_enabled(struct pci_dev *dev, void *data)
320 pci_ers_result_t vote;
321 struct pci_error_handlers *err_handler;
322 struct aer_broadcast_data *result_data;
323 result_data = (struct aer_broadcast_data *) data;
326 !dev->driver->err_handler ||
327 !dev->driver->err_handler->mmio_enabled)
330 err_handler = dev->driver->err_handler;
331 vote = err_handler->mmio_enabled(dev);
332 result_data->result = merge_result(result_data->result, vote);
336 static int report_slot_reset(struct pci_dev *dev, void *data)
338 pci_ers_result_t vote;
339 struct pci_error_handlers *err_handler;
340 struct aer_broadcast_data *result_data;
341 result_data = (struct aer_broadcast_data *) data;
344 !dev->driver->err_handler ||
345 !dev->driver->err_handler->slot_reset)
348 err_handler = dev->driver->err_handler;
349 vote = err_handler->slot_reset(dev);
350 result_data->result = merge_result(result_data->result, vote);
354 static int report_resume(struct pci_dev *dev, void *data)
356 struct pci_error_handlers *err_handler;
358 dev->error_state = pci_channel_io_normal;
361 !dev->driver->err_handler ||
362 !dev->driver->err_handler->resume)
365 err_handler = dev->driver->err_handler;
366 err_handler->resume(dev);
371 * broadcast_error_message - handle message broadcast to downstream drivers
372 * @dev: pointer to from where in a hierarchy message is broadcasted down
373 * @state: error state
374 * @error_mesg: message to print
375 * @cb: callback to be broadcasted
377 * Invoked during error recovery process. Once being invoked, the content
378 * of error severity will be broadcasted to all downstream drivers in a
379 * hierarchy in question.
381 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
382 enum pci_channel_state state,
384 int (*cb)(struct pci_dev *, void *))
386 struct aer_broadcast_data result_data;
388 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
389 result_data.state = state;
390 if (cb == report_error_detected)
391 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
393 result_data.result = PCI_ERS_RESULT_RECOVERED;
395 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
397 * If the error is reported by a bridge, we think this error
398 * is related to the downstream link of the bridge, so we
399 * do error recovery on all subordinates of the bridge instead
400 * of the bridge and clear the error status of the bridge.
402 if (cb == report_error_detected)
403 dev->error_state = state;
404 pci_walk_bus(dev->subordinate, cb, &result_data);
405 if (cb == report_resume) {
406 pci_cleanup_aer_uncorrect_error_status(dev);
407 dev->error_state = pci_channel_io_normal;
411 * If the error is reported by an end point, we think this
412 * error is related to the upstream link of the end point.
414 pci_walk_bus(dev->bus, cb, &result_data);
417 return result_data.result;
420 struct find_aer_service_data {
421 struct pcie_port_service_driver *aer_driver;
425 static int find_aer_service_iter(struct device *device, void *data)
427 struct device_driver *driver;
428 struct pcie_port_service_driver *service_driver;
429 struct find_aer_service_data *result;
431 result = (struct find_aer_service_data *) data;
433 if (device->bus == &pcie_port_bus_type) {
434 struct pcie_port_data *port_data;
436 port_data = pci_get_drvdata(to_pcie_device(device)->port);
437 if (port_data->port_type == PCIE_SW_DOWNSTREAM_PORT)
438 result->is_downstream = 1;
440 driver = device->driver;
442 service_driver = to_service_driver(driver);
443 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
444 result->aer_driver = service_driver;
453 static void find_aer_service(struct pci_dev *dev,
454 struct find_aer_service_data *data)
457 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
460 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
463 struct pci_dev *udev;
464 pci_ers_result_t status;
465 struct find_aer_service_data data;
467 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
470 udev = dev->bus->self;
472 data.is_downstream = 0;
473 data.aer_driver = NULL;
474 find_aer_service(udev, &data);
477 * Use the aer driver of the error agent firstly.
478 * If it hasn't the aer driver, use the root port's
480 if (!data.aer_driver || !data.aer_driver->reset_link) {
481 if (data.is_downstream &&
482 aerdev->device.driver &&
483 to_service_driver(aerdev->device.driver)->reset_link) {
485 to_service_driver(aerdev->device.driver);
487 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
489 return PCI_ERS_RESULT_DISCONNECT;
493 status = data.aer_driver->reset_link(udev);
494 if (status != PCI_ERS_RESULT_RECOVERED) {
495 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
496 "device %s failed\n", pci_name(udev));
497 return PCI_ERS_RESULT_DISCONNECT;
504 * do_recovery - handle nonfatal/fatal error recovery process
505 * @aerdev: pointer to a pcie_device data structure of root port
506 * @dev: pointer to a pci_dev data structure of agent detecting an error
507 * @severity: error severity type
509 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
510 * error detected message to all downstream drivers within a hierarchy in
511 * question and return the returned code.
513 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
517 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
518 enum pci_channel_state state;
520 if (severity == AER_FATAL)
521 state = pci_channel_io_frozen;
523 state = pci_channel_io_normal;
525 status = broadcast_error_message(dev,
528 report_error_detected);
530 if (severity == AER_FATAL) {
531 result = reset_link(aerdev, dev);
532 if (result != PCI_ERS_RESULT_RECOVERED) {
533 /* TODO: Should panic here? */
538 if (status == PCI_ERS_RESULT_CAN_RECOVER)
539 status = broadcast_error_message(dev,
542 report_mmio_enabled);
544 if (status == PCI_ERS_RESULT_NEED_RESET) {
546 * TODO: Should call platform-specific
547 * functions to reset slot before calling
548 * drivers' slot_reset callbacks?
550 status = broadcast_error_message(dev,
556 if (status == PCI_ERS_RESULT_RECOVERED)
557 broadcast_error_message(dev,
566 * handle_error_source - handle logging error into an event log
567 * @aerdev: pointer to pcie_device data structure of the root port
568 * @dev: pointer to pci_dev data structure of error source device
569 * @info: comprehensive error information
571 * Invoked when an error being detected by Root Port.
573 static void handle_error_source(struct pcie_device *aerdev,
575 struct aer_err_info *info)
577 pci_ers_result_t status = 0;
580 if (info->severity == AER_CORRECTABLE) {
582 * Correctable error does not need software intevention.
583 * No need to go through error recovery process.
585 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
587 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
590 status = do_recovery(aerdev, dev, info->severity);
591 if (status == PCI_ERS_RESULT_RECOVERED) {
592 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
593 "successfully recovered\n");
595 /* TODO: Should kernel panic here? */
596 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
603 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
604 * @rpc: pointer to a Root Port data structure
606 * Invoked when PCIE bus loads AER service driver.
608 void aer_enable_rootport(struct aer_rpc *rpc)
610 struct pci_dev *pdev = rpc->rpd->port;
615 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
616 /* Clear PCIE Capability's Device Status */
617 pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, ®16);
618 pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
620 /* Disable system error generation in response to error messages */
621 pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, ®16);
622 reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
623 pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
625 aer_pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
626 /* Clear error status */
627 pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, ®32);
628 pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
629 pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, ®32);
630 pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
631 pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, ®32);
632 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
635 * Enable error reporting for the root port device and downstream port
638 set_downstream_devices_error_reporting(pdev, true);
640 /* Enable Root Port's interrupt in response to error messages */
641 pci_write_config_dword(pdev,
642 aer_pos + PCI_ERR_ROOT_COMMAND,
643 ROOT_PORT_INTR_ON_MESG_MASK);
647 * disable_root_aer - disable Root Port's interrupts when receiving messages
648 * @rpc: pointer to a Root Port data structure
650 * Invoked when PCIE bus unloads AER service driver.
652 static void disable_root_aer(struct aer_rpc *rpc)
654 struct pci_dev *pdev = rpc->rpd->port;
659 * Disable error reporting for the root port device and downstream port
662 set_downstream_devices_error_reporting(pdev, false);
664 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ERR);
665 /* Disable Root's interrupt in response to error messages */
666 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, 0);
668 /* Clear Root's error status reg */
669 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
670 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
674 * get_e_source - retrieve an error source
675 * @rpc: pointer to the root port which holds an error
677 * Invoked by DPC handler to consume an error.
679 static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
681 struct aer_err_source *e_source;
684 /* Lock access to Root error producer/consumer index */
685 spin_lock_irqsave(&rpc->e_lock, flags);
686 if (rpc->prod_idx == rpc->cons_idx) {
687 spin_unlock_irqrestore(&rpc->e_lock, flags);
690 e_source = &rpc->e_sources[rpc->cons_idx];
692 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
694 spin_unlock_irqrestore(&rpc->e_lock, flags);
699 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
704 info->tlp_header_valid = 0;
706 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
708 /* The device might not support AER */
712 if (info->severity == AER_CORRECTABLE) {
713 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
715 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
717 if (!(info->status & ~info->mask))
718 return AER_UNSUCCESS;
719 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
720 info->severity == AER_NONFATAL) {
722 /* Link is still healthy for IO reads */
723 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
725 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
727 if (!(info->status & ~info->mask))
728 return AER_UNSUCCESS;
730 /* Get First Error Pointer */
731 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
732 info->first_error = PCI_ERR_CAP_FEP(temp);
734 if (info->status & AER_LOG_TLP_MASKS) {
735 info->tlp_header_valid = 1;
736 pci_read_config_dword(dev,
737 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
738 pci_read_config_dword(dev,
739 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
740 pci_read_config_dword(dev,
741 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
742 pci_read_config_dword(dev,
743 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
750 static inline void aer_process_err_devices(struct pcie_device *p_device,
751 struct aer_err_info *e_info)
755 if (!e_info->dev[0]) {
756 dev_printk(KERN_DEBUG, &p_device->port->dev,
757 "can't find device of ID%04x\n",
761 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
762 if (get_device_error_info(e_info->dev[i], e_info) ==
764 aer_print_error(e_info->dev[i], e_info);
765 handle_error_source(p_device,
773 * aer_isr_one_error - consume an error detected by root port
774 * @p_device: pointer to error root port service device
775 * @e_src: pointer to an error source
777 static void aer_isr_one_error(struct pcie_device *p_device,
778 struct aer_err_source *e_src)
780 struct aer_err_info *e_info;
783 /* struct aer_err_info might be big, so we allocate it with slab */
784 e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
785 if (e_info == NULL) {
786 dev_printk(KERN_DEBUG, &p_device->port->dev,
787 "Can't allocate mem when processing AER errors\n");
792 * There is a possibility that both correctable error and
793 * uncorrectable error being logged. Report correctable error first.
795 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
798 if (!(e_src->status & i))
801 memset(e_info, 0, sizeof(struct aer_err_info));
803 /* Init comprehensive error information */
804 if (i & PCI_ERR_ROOT_COR_RCV) {
805 e_info->id = ERR_COR_ID(e_src->id);
806 e_info->severity = AER_CORRECTABLE;
808 e_info->id = ERR_UNCOR_ID(e_src->id);
809 e_info->severity = ((e_src->status >> 6) & 1);
812 (PCI_ERR_ROOT_MULTI_COR_RCV |
813 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
814 e_info->multi_error_valid = 1;
816 aer_print_port_info(p_device->port, e_info);
818 find_source_device(p_device->port, e_info);
819 aer_process_err_devices(p_device, e_info);
826 * aer_isr - consume errors detected by root port
827 * @work: definition of this work item
829 * Invoked, as DPC, when root port records new detected error
831 void aer_isr(struct work_struct *work)
833 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
834 struct pcie_device *p_device = rpc->rpd;
835 struct aer_err_source *e_src;
837 mutex_lock(&rpc->rpc_mutex);
838 e_src = get_e_source(rpc);
840 aer_isr_one_error(p_device, e_src);
841 e_src = get_e_source(rpc);
843 mutex_unlock(&rpc->rpc_mutex);
845 wake_up(&rpc->wait_release);
849 * aer_delete_rootport - disable root port aer and delete service data
850 * @rpc: pointer to a root port device being deleted
852 * Invoked when AER service unloaded on a specific Root Port
854 void aer_delete_rootport(struct aer_rpc *rpc)
856 /* Disable root port AER itself */
857 disable_root_aer(rpc);
863 * aer_init - provide AER initialization
864 * @dev: pointer to AER pcie device
866 * Invoked when AER service driver is loaded.
868 int aer_init(struct pcie_device *dev)
870 if (aer_osc_setup(dev) && !forceload)