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,
57 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
62 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
66 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
67 reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
68 PCI_EXP_DEVCTL_NFERE |
71 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL,
76 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
81 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
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 */
90 status &= mask; /* Clear corresponding fatal bits */
91 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
97 int pci_cleanup_aer_correct_error_status(struct pci_dev *dev)
102 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
106 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
107 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
114 static int set_device_error_reporting(struct pci_dev *dev, void *data)
116 bool enable = *((bool *)data);
118 if (dev->pcie_type == PCIE_RC_PORT ||
119 dev->pcie_type == PCIE_SW_UPSTREAM_PORT ||
120 dev->pcie_type == PCIE_SW_DOWNSTREAM_PORT) {
122 pci_enable_pcie_error_reporting(dev);
124 pci_disable_pcie_error_reporting(dev);
128 pcie_set_ecrc_checking(dev);
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.
138 static void set_downstream_devices_error_reporting(struct pci_dev *dev,
141 set_device_error_reporting(dev, &enable);
143 if (!dev->subordinate)
145 pci_walk_bus(dev->subordinate, set_device_error_reporting, &enable);
148 static inline int compare_device_id(struct pci_dev *dev,
149 struct aer_err_info *e_info)
151 if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
161 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
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++;
172 #define PCI_BUS(x) (((x) >> 8) & 0xff)
174 static int find_device_iter(struct pci_dev *dev, void *data)
181 struct aer_err_info *e_info = (struct aer_err_info *)data;
184 * When bus id is equal to 0, it might be a bad id
185 * reported by root port.
187 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
188 result = compare_device_id(dev, e_info);
190 add_error_device(e_info, dev);
193 * If there is no multiple error, we stop
194 * or continue based on the id comparing.
196 if (!(e_info->flags & AER_MULTI_ERROR_VALID_FLAG))
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.
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.
216 if (atomic_read(&dev->enable_cnt) == 0)
218 pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
221 /* Check if AER is enabled */
222 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
224 PCI_EXP_DEVCTL_CERE |
225 PCI_EXP_DEVCTL_NFERE |
226 PCI_EXP_DEVCTL_FERE |
227 PCI_EXP_DEVCTL_URRE)))
229 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
235 if (e_info->severity == AER_CORRECTABLE) {
236 pci_read_config_dword(dev,
237 pos + PCI_ERR_COR_STATUS,
239 pci_read_config_dword(dev,
240 pos + PCI_ERR_COR_MASK,
242 if (status & ERR_CORRECTABLE_ERROR_MASK & ~mask) {
243 add_error_device(e_info, dev);
247 pci_read_config_dword(dev,
248 pos + PCI_ERR_UNCOR_STATUS,
250 pci_read_config_dword(dev,
251 pos + PCI_ERR_UNCOR_MASK,
253 if (status & ERR_UNCORRECTABLE_ERROR_MASK & ~mask) {
254 add_error_device(e_info, dev);
262 if (e_info->flags & AER_MULTI_ERROR_VALID_FLAG)
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
273 * Invoked when error is detected at the Root Port.
275 static void find_source_device(struct pci_dev *parent,
276 struct aer_err_info *e_info)
278 struct pci_dev *dev = parent;
281 /* Is Root Port an agent that sends error message? */
282 result = find_device_iter(dev, e_info);
286 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
289 static int report_error_detected(struct pci_dev *dev, void *data)
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;
296 dev->error_state = result_data->state;
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)) {
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
310 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
312 "no AER-aware driver" : "no driver");
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);
323 static int report_mmio_enabled(struct pci_dev *dev, void *data)
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;
331 !dev->driver->err_handler ||
332 !dev->driver->err_handler->mmio_enabled)
335 err_handler = dev->driver->err_handler;
336 vote = err_handler->mmio_enabled(dev);
337 result_data->result = merge_result(result_data->result, vote);
341 static int report_slot_reset(struct pci_dev *dev, void *data)
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;
349 !dev->driver->err_handler ||
350 !dev->driver->err_handler->slot_reset)
353 err_handler = dev->driver->err_handler;
354 vote = err_handler->slot_reset(dev);
355 result_data->result = merge_result(result_data->result, vote);
359 static int report_resume(struct pci_dev *dev, void *data)
361 struct pci_error_handlers *err_handler;
363 dev->error_state = pci_channel_io_normal;
366 !dev->driver->err_handler ||
367 !dev->driver->err_handler->resume)
370 err_handler = dev->driver->err_handler;
371 err_handler->resume(dev);
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
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.
386 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
387 enum pci_channel_state state,
389 int (*cb)(struct pci_dev *, void *))
391 struct aer_broadcast_data result_data;
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;
398 result_data.result = PCI_ERS_RESULT_RECOVERED;
400 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
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.
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;
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.
420 pci_walk_bus(dev->bus, cb, &result_data);
423 return result_data.result;
426 struct find_aer_service_data {
427 struct pcie_port_service_driver *aer_driver;
431 static int find_aer_service_iter(struct device *device, void *data)
433 struct device_driver *driver;
434 struct pcie_port_service_driver *service_driver;
435 struct find_aer_service_data *result;
437 result = (struct find_aer_service_data *) data;
439 if (device->bus == &pcie_port_bus_type) {
440 struct pcie_port_data *port_data;
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;
446 driver = device->driver;
448 service_driver = to_service_driver(driver);
449 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
450 result->aer_driver = service_driver;
459 static void find_aer_service(struct pci_dev *dev,
460 struct find_aer_service_data *data)
463 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
466 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
469 struct pci_dev *udev;
470 pci_ers_result_t status;
471 struct find_aer_service_data data;
473 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
476 udev= dev->bus->self;
478 data.is_downstream = 0;
479 data.aer_driver = NULL;
480 find_aer_service(udev, &data);
483 * Use the aer driver of the error agent firstly.
484 * If it hasn't the aer driver, use the root port's
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) {
491 to_service_driver(aerdev->device.driver);
493 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
495 return PCI_ERS_RESULT_DISCONNECT;
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;
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
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.
519 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
523 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
524 enum pci_channel_state state;
526 if (severity == AER_FATAL)
527 state = pci_channel_io_frozen;
529 state = pci_channel_io_normal;
531 status = broadcast_error_message(dev,
534 report_error_detected);
536 if (severity == AER_FATAL) {
537 result = reset_link(aerdev, dev);
538 if (result != PCI_ERS_RESULT_RECOVERED) {
539 /* TODO: Should panic here? */
544 if (status == PCI_ERS_RESULT_CAN_RECOVER)
545 status = broadcast_error_message(dev,
548 report_mmio_enabled);
550 if (status == PCI_ERS_RESULT_NEED_RESET) {
552 * TODO: Should call platform-specific
553 * functions to reset slot before calling
554 * drivers' slot_reset callbacks?
556 status = broadcast_error_message(dev,
562 if (status == PCI_ERS_RESULT_RECOVERED)
563 broadcast_error_message(dev,
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
577 * Invoked when an error being detected by Root Port.
579 static void handle_error_source(struct pcie_device * aerdev,
581 struct aer_err_info *info)
583 pci_ers_result_t status = 0;
586 if (info->severity == AER_CORRECTABLE) {
588 * Correctable error does not need software intevention.
589 * No need to go through error recovery process.
591 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
593 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
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");
601 /* TODO: Should kernel panic here? */
602 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
609 * aer_enable_rootport - enable Root Port's interrupts when receiving messages
610 * @rpc: pointer to a Root Port data structure
612 * Invoked when PCIE bus loads AER service driver.
614 void aer_enable_rootport(struct aer_rpc *rpc)
616 struct pci_dev *pdev = rpc->rpd->port;
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, ®16);
624 pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
626 /* Disable system error generation in response to error messages */
627 pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, ®16);
628 reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
629 pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
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, ®32);
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, ®32);
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, ®32);
638 pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
641 * Enable error reporting for the root port device and downstream port
644 set_downstream_devices_error_reporting(pdev, true);
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);
653 * disable_root_aer - disable Root Port's interrupts when receiving messages
654 * @rpc: pointer to a Root Port data structure
656 * Invoked when PCIE bus unloads AER service driver.
658 static void disable_root_aer(struct aer_rpc *rpc)
660 struct pci_dev *pdev = rpc->rpd->port;
665 * Disable error reporting for the root port device and downstream port
668 set_downstream_devices_error_reporting(pdev, false);
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);
674 /* Clear Root's error status reg */
675 pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
676 pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
680 * get_e_source - retrieve an error source
681 * @rpc: pointer to the root port which holds an error
683 * Invoked by DPC handler to consume an error.
685 static struct aer_err_source* get_e_source(struct aer_rpc *rpc)
687 struct aer_err_source *e_source;
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);
696 e_source = &rpc->e_sources[rpc->cons_idx];
698 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
700 spin_unlock_irqrestore(&rpc->e_lock, flags);
705 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
709 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
711 /* The device might not support AER */
715 if (info->severity == AER_CORRECTABLE) {
716 pci_read_config_dword(dev, pos + PCI_ERR_COR_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) {
723 /* Link is still healthy for IO reads */
724 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
726 if (!(info->status & ERR_UNCORRECTABLE_ERROR_MASK))
727 return AER_UNSUCCESS;
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);
745 static inline void aer_process_err_devices(struct pcie_device *p_device,
746 struct aer_err_info *e_info)
750 if (!e_info->dev[0]) {
751 dev_printk(KERN_DEBUG, &p_device->port->dev,
752 "can't find device of ID%04x\n",
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) ==
759 aer_print_error(e_info->dev[i], e_info);
760 handle_error_source(p_device,
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
772 static void aer_isr_one_error(struct pcie_device *p_device,
773 struct aer_err_source *e_src)
775 struct aer_err_info *e_info;
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");
787 * There is a possibility that both correctable error and
788 * uncorrectable error being logged. Report correctable error first.
790 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
793 if (!(e_src->status & i))
796 memset(e_info, 0, sizeof(struct aer_err_info));
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;
803 e_info->id = ERR_UNCOR_ID(e_src->id);
804 e_info->severity = ((e_src->status >> 6) & 1);
807 (PCI_ERR_ROOT_MULTI_COR_RCV |
808 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
809 e_info->flags |= AER_MULTI_ERROR_VALID_FLAG;
811 find_source_device(p_device->port, e_info);
812 aer_process_err_devices(p_device, e_info);
819 * aer_isr - consume errors detected by root port
820 * @work: definition of this work item
822 * Invoked, as DPC, when root port records new detected error
824 void aer_isr(struct work_struct *work)
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;
830 mutex_lock(&rpc->rpc_mutex);
831 e_src = get_e_source(rpc);
833 aer_isr_one_error(p_device, e_src);
834 e_src = get_e_source(rpc);
836 mutex_unlock(&rpc->rpc_mutex);
838 wake_up(&rpc->wait_release);
842 * aer_delete_rootport - disable root port aer and delete service data
843 * @rpc: pointer to a root port device being deleted
845 * Invoked when AER service unloaded on a specific Root Port
847 void aer_delete_rootport(struct aer_rpc *rpc)
849 /* Disable root port AER itself */
850 disable_root_aer(rpc);
856 * aer_init - provide AER initialization
857 * @dev: pointer to AER pcie device
859 * Invoked when AER service driver is loaded.
861 int aer_init(struct pcie_device *dev)
863 if (aer_osc_setup(dev) && !forceload)
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);