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 if (dev->aer_firmware_first)
41 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
45 pos = pci_pcie_cap(dev);
49 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
52 PCI_EXP_DEVCTL_NFERE |
55 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
59 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
61 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
66 if (dev->aer_firmware_first)
69 pos = pci_pcie_cap(dev);
73 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
74 reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
75 PCI_EXP_DEVCTL_NFERE |
78 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
82 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
84 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
89 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
93 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
95 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
99 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
101 static int set_device_error_reporting(struct pci_dev *dev, void *data)
103 bool enable = *((bool *)data);
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)) {
109 pci_enable_pcie_error_reporting(dev);
111 pci_disable_pcie_error_reporting(dev);
115 pcie_set_ecrc_checking(dev);
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.
125 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
128 set_device_error_reporting(dev, &enable);
130 if (!dev->subordinate)
132 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
135 static inline int compare_device_id(struct pci_dev *dev,
136 struct aer_err_info *e_info)
138 if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
148 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
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++;
160 #define PCI_BUS(x) (((x) >> 8) & 0xff)
162 static int find_device_iter(struct pci_dev *dev, void *data)
169 struct aer_err_info *e_info = (struct aer_err_info *)data;
172 * When bus id is equal to 0, it might be a bad id
173 * reported by root port.
175 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
176 result = compare_device_id(dev, e_info);
178 add_error_device(e_info, dev);
181 * If there is no multiple error, we stop
182 * or continue based on the id comparing.
184 if (!e_info->multi_error_valid)
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.
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.
204 if (atomic_read(&dev->enable_cnt) == 0)
206 pos = pci_pcie_cap(dev);
209 /* Check if AER is enabled */
210 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
212 PCI_EXP_DEVCTL_CERE |
213 PCI_EXP_DEVCTL_NFERE |
214 PCI_EXP_DEVCTL_FERE |
215 PCI_EXP_DEVCTL_URRE)))
217 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
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);
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);
242 if (e_info->multi_error_valid)
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
253 * Invoked when error is detected at the Root Port.
255 static void find_source_device(struct pci_dev *parent,
256 struct aer_err_info *e_info)
258 struct pci_dev *dev = parent;
261 /* Is Root Port an agent that sends error message? */
262 result = find_device_iter(dev, e_info);
266 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
269 static int report_error_detected(struct pci_dev *dev, void *data)
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;
276 dev->error_state = result_data->state;
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)) {
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
290 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
292 "no AER-aware driver" : "no driver");
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);
303 static int report_mmio_enabled(struct pci_dev *dev, void *data)
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;
311 !dev->driver->err_handler ||
312 !dev->driver->err_handler->mmio_enabled)
315 err_handler = dev->driver->err_handler;
316 vote = err_handler->mmio_enabled(dev);
317 result_data->result = merge_result(result_data->result, vote);
321 static int report_slot_reset(struct pci_dev *dev, void *data)
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;
329 !dev->driver->err_handler ||
330 !dev->driver->err_handler->slot_reset)
333 err_handler = dev->driver->err_handler;
334 vote = err_handler->slot_reset(dev);
335 result_data->result = merge_result(result_data->result, vote);
339 static int report_resume(struct pci_dev *dev, void *data)
341 struct pci_error_handlers *err_handler;
343 dev->error_state = pci_channel_io_normal;
346 !dev->driver->err_handler ||
347 !dev->driver->err_handler->resume)
350 err_handler = dev->driver->err_handler;
351 err_handler->resume(dev);
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
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.
366 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
367 enum pci_channel_state state,
369 int (*cb)(struct pci_dev *, void *))
371 struct aer_broadcast_data result_data;
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;
378 result_data.result = PCI_ERS_RESULT_RECOVERED;
380 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
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.
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;
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.
399 pci_walk_bus(dev->bus, cb, &result_data);
402 return result_data.result;
405 struct find_aer_service_data {
406 struct pcie_port_service_driver *aer_driver;
410 static int find_aer_service_iter(struct device *device, void *data)
412 struct device_driver *driver;
413 struct pcie_port_service_driver *service_driver;
414 struct find_aer_service_data *result;
416 result = (struct find_aer_service_data *) data;
418 if (device->bus == &pcie_port_bus_type) {
419 struct pcie_device *pcie = to_pcie_device(device);
421 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
422 result->is_downstream = 1;
424 driver = device->driver;
426 service_driver = to_service_driver(driver);
427 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
428 result->aer_driver = service_driver;
437 static void find_aer_service(struct pci_dev *dev,
438 struct find_aer_service_data *data)
441 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
444 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
447 struct pci_dev *udev;
448 pci_ers_result_t status;
449 struct find_aer_service_data data;
451 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
454 udev = dev->bus->self;
456 data.is_downstream = 0;
457 data.aer_driver = NULL;
458 find_aer_service(udev, &data);
461 * Use the aer driver of the error agent firstly.
462 * If it hasn't the aer driver, use the root port's
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) {
469 to_service_driver(aerdev->device.driver);
471 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
473 return PCI_ERS_RESULT_DISCONNECT;
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;
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
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.
497 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
501 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
502 enum pci_channel_state state;
504 if (severity == AER_FATAL)
505 state = pci_channel_io_frozen;
507 state = pci_channel_io_normal;
509 status = broadcast_error_message(dev,
512 report_error_detected);
514 if (severity == AER_FATAL) {
515 result = reset_link(aerdev, dev);
516 if (result != PCI_ERS_RESULT_RECOVERED) {
517 /* TODO: Should panic here? */
522 if (status == PCI_ERS_RESULT_CAN_RECOVER)
523 status = broadcast_error_message(dev,
526 report_mmio_enabled);
528 if (status == PCI_ERS_RESULT_NEED_RESET) {
530 * TODO: Should call platform-specific
531 * functions to reset slot before calling
532 * drivers' slot_reset callbacks?
534 status = broadcast_error_message(dev,
540 if (status == PCI_ERS_RESULT_RECOVERED)
541 broadcast_error_message(dev,
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
555 * Invoked when an error being detected by Root Port.
557 static void handle_error_source(struct pcie_device *aerdev,
559 struct aer_err_info *info)
561 pci_ers_result_t status = 0;
564 if (info->severity == AER_CORRECTABLE) {
566 * Correctable error does not need software intevention.
567 * No need to go through error recovery process.
569 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
571 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
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");
579 /* TODO: Should kernel panic here? */
580 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
587 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
588 * @rpc: pointer to a Root Port data structure
590 * Invoked when PCIe bus loads AER service driver.
592 void aer_enable_rootport(struct aer_rpc *rpc)
594 struct pci_dev *pdev = rpc->rpd->port;
599 pos = pci_pcie_cap(pdev);
600 /* Clear PCIe Capability's Device Status */
601 pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, ®16);
602 pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
604 /* Disable system error generation in response to error messages */
605 pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, ®16);
606 reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
607 pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
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, ®32);
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, ®32);
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, ®32);
616 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
619 * Enable error reporting for the root port device and downstream port
622 set_downstream_devices_error_reporting(pdev, true);
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);
631 * disable_root_aer - disable Root Port's interrupts when receiving messages
632 * @rpc: pointer to a Root Port data structure
634 * Invoked when PCIe bus unloads AER service driver.
636 static void disable_root_aer(struct aer_rpc *rpc)
638 struct pci_dev *pdev = rpc->rpd->port;
643 * Disable error reporting for the root port device and downstream port
646 set_downstream_devices_error_reporting(pdev, false);
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);
652 /* Clear Root's error status reg */
653 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
654 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
658 * get_e_source - retrieve an error source
659 * @rpc: pointer to the root port which holds an error
661 * Invoked by DPC handler to consume an error.
663 static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
665 struct aer_err_source *e_source;
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);
674 e_source = &rpc->e_sources[rpc->cons_idx];
676 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
678 spin_unlock_irqrestore(&rpc->e_lock, flags);
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
688 * Return 1 on success, 0 on error.
690 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
695 info->tlp_header_valid = 0;
697 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
699 /* The device might not support AER */
703 if (info->severity == AER_CORRECTABLE) {
704 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
706 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
708 if (!(info->status & ~info->mask))
710 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
711 info->severity == AER_NONFATAL) {
713 /* Link is still healthy for IO reads */
714 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
716 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
718 if (!(info->status & ~info->mask))
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);
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);
741 static inline void aer_process_err_devices(struct pcie_device *p_device,
742 struct aer_err_info *e_info)
746 if (!e_info->dev[0]) {
747 dev_printk(KERN_DEBUG, &p_device->port->dev,
748 "can't find device of ID%04x\n",
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);
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);
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
768 static void aer_isr_one_error(struct pcie_device *p_device,
769 struct aer_err_source *e_src)
771 struct aer_err_info *e_info;
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");
783 * There is a possibility that both correctable error and
784 * uncorrectable error being logged. Report correctable error first.
786 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
789 if (!(e_src->status & i))
792 memset(e_info, 0, sizeof(struct aer_err_info));
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;
799 e_info->id = ERR_UNCOR_ID(e_src->id);
800 e_info->severity = ((e_src->status >> 6) & 1);
803 (PCI_ERR_ROOT_MULTI_COR_RCV |
804 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
805 e_info->multi_error_valid = 1;
807 aer_print_port_info(p_device->port, e_info);
809 find_source_device(p_device->port, e_info);
810 aer_process_err_devices(p_device, e_info);
817 * aer_isr - consume errors detected by root port
818 * @work: definition of this work item
820 * Invoked, as DPC, when root port records new detected error
822 void aer_isr(struct work_struct *work)
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;
828 mutex_lock(&rpc->rpc_mutex);
829 e_src = get_e_source(rpc);
831 aer_isr_one_error(p_device, e_src);
832 e_src = get_e_source(rpc);
834 mutex_unlock(&rpc->rpc_mutex);
836 wake_up(&rpc->wait_release);
840 * aer_delete_rootport - disable root port aer and delete service data
841 * @rpc: pointer to a root port device being deleted
843 * Invoked when AER service unloaded on a specific Root Port
845 void aer_delete_rootport(struct aer_rpc *rpc)
847 /* Disable root port AER itself */
848 disable_root_aer(rpc);
854 * aer_init - provide AER initialization
855 * @dev: pointer to AER pcie device
857 * Invoked when AER service driver is loaded.
859 int aer_init(struct pcie_device *dev)
861 if (dev->port->aer_firmware_first) {
862 dev_printk(KERN_DEBUG, &dev->device,
863 "PCIe errors handled by platform firmware.\n");
867 if (aer_osc_setup(dev))
873 dev_printk(KERN_DEBUG, &dev->device,
874 "aerdrv forceload requested.\n");
875 dev->port->aer_firmware_first = 0;