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>
26 #include <linux/slab.h>
27 #include <linux/kfifo.h>
30 static bool forceload;
31 static bool nosourceid;
32 module_param(forceload, bool, 0);
33 module_param(nosourceid, bool, 0);
35 #define PCI_EXP_AER_FLAGS (PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
36 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
38 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
40 if (pcie_aer_get_firmware_first(dev))
43 if (!pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR))
46 return pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
48 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
50 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
52 if (pcie_aer_get_firmware_first(dev))
55 return pcie_capability_clear_word(dev, PCI_EXP_DEVCTL,
58 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
60 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
65 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
69 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
71 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
75 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
77 int pci_cleanup_aer_error_status_regs(struct pci_dev *dev)
83 if (!pci_is_pcie(dev))
86 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
90 port_type = pci_pcie_type(dev);
91 if (port_type == PCI_EXP_TYPE_ROOT_PORT) {
92 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
93 pci_write_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, status);
96 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
97 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
99 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
100 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
106 * add_error_device - list device to be handled
107 * @e_info: pointer to error info
108 * @dev: pointer to pci_dev to be added
110 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
112 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
113 e_info->dev[e_info->error_dev_num] = dev;
114 e_info->error_dev_num++;
121 * is_error_source - check whether the device is source of reported error
122 * @dev: pointer to pci_dev to be checked
123 * @e_info: pointer to reported error info
125 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
132 * When bus id is equal to 0, it might be a bad id
133 * reported by root port.
135 if (!nosourceid && (PCI_BUS_NUM(e_info->id) != 0)) {
136 /* Device ID match? */
137 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
140 /* Continue id comparing if there is no multiple error */
141 if (!e_info->multi_error_valid)
148 * 2) bus id is equal to 0. Some ports might lose the bus
149 * id of error source id;
150 * 3) There are multiple errors and prior id comparing fails;
151 * We check AER status registers to find possible reporter.
153 if (atomic_read(&dev->enable_cnt) == 0)
156 /* Check if AER is enabled */
157 pcie_capability_read_word(dev, PCI_EXP_DEVCTL, ®16);
158 if (!(reg16 & PCI_EXP_AER_FLAGS))
161 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
165 /* Check if error is recorded */
166 if (e_info->severity == AER_CORRECTABLE) {
167 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
168 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
170 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
171 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
179 static int find_device_iter(struct pci_dev *dev, void *data)
181 struct aer_err_info *e_info = (struct aer_err_info *)data;
183 if (is_error_source(dev, e_info)) {
184 /* List this device */
185 if (add_error_device(e_info, dev)) {
186 /* We cannot handle more... Stop iteration */
187 /* TODO: Should print error message here? */
191 /* If there is only a single error, stop iteration */
192 if (!e_info->multi_error_valid)
199 * find_source_device - search through device hierarchy for source device
200 * @parent: pointer to Root Port pci_dev data structure
201 * @e_info: including detailed error information such like id
203 * Return true if found.
205 * Invoked by DPC when error is detected at the Root Port.
206 * Caller of this function must set id, severity, and multi_error_valid of
207 * struct aer_err_info pointed by @e_info properly. This function must fill
208 * e_info->error_dev_num and e_info->dev[], based on the given information.
210 static bool find_source_device(struct pci_dev *parent,
211 struct aer_err_info *e_info)
213 struct pci_dev *dev = parent;
216 /* Must reset in this function */
217 e_info->error_dev_num = 0;
219 /* Is Root Port an agent that sends error message? */
220 result = find_device_iter(dev, e_info);
224 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
226 if (!e_info->error_dev_num) {
227 dev_printk(KERN_DEBUG, &parent->dev,
228 "can't find device of ID%04x\n",
235 static int report_error_detected(struct pci_dev *dev, void *data)
237 pci_ers_result_t vote;
238 const struct pci_error_handlers *err_handler;
239 struct aer_broadcast_data *result_data;
240 result_data = (struct aer_broadcast_data *) data;
242 device_lock(&dev->dev);
243 dev->error_state = result_data->state;
246 !dev->driver->err_handler ||
247 !dev->driver->err_handler->error_detected) {
248 if (result_data->state == pci_channel_io_frozen &&
249 !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
251 * In case of fatal recovery, if one of down-
252 * stream device has no driver. We might be
253 * unable to recover because a later insmod
254 * of a driver for this device is unaware of
257 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
259 "no AER-aware driver" : "no driver");
263 * If there's any device in the subtree that does not
264 * have an error_detected callback, returning
265 * PCI_ERS_RESULT_NO_AER_DRIVER prevents calling of
266 * the subsequent mmio_enabled/slot_reset/resume
267 * callbacks of "any" device in the subtree. All the
268 * devices in the subtree are left in the error state
272 if (!(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE))
273 vote = PCI_ERS_RESULT_NO_AER_DRIVER;
275 vote = PCI_ERS_RESULT_NONE;
277 err_handler = dev->driver->err_handler;
278 vote = err_handler->error_detected(dev, result_data->state);
281 result_data->result = merge_result(result_data->result, vote);
282 device_unlock(&dev->dev);
286 static int report_mmio_enabled(struct pci_dev *dev, void *data)
288 pci_ers_result_t vote;
289 const struct pci_error_handlers *err_handler;
290 struct aer_broadcast_data *result_data;
291 result_data = (struct aer_broadcast_data *) data;
293 device_lock(&dev->dev);
295 !dev->driver->err_handler ||
296 !dev->driver->err_handler->mmio_enabled)
299 err_handler = dev->driver->err_handler;
300 vote = err_handler->mmio_enabled(dev);
301 result_data->result = merge_result(result_data->result, vote);
303 device_unlock(&dev->dev);
307 static int report_slot_reset(struct pci_dev *dev, void *data)
309 pci_ers_result_t vote;
310 const struct pci_error_handlers *err_handler;
311 struct aer_broadcast_data *result_data;
312 result_data = (struct aer_broadcast_data *) data;
314 device_lock(&dev->dev);
316 !dev->driver->err_handler ||
317 !dev->driver->err_handler->slot_reset)
320 err_handler = dev->driver->err_handler;
321 vote = err_handler->slot_reset(dev);
322 result_data->result = merge_result(result_data->result, vote);
324 device_unlock(&dev->dev);
328 static int report_resume(struct pci_dev *dev, void *data)
330 const struct pci_error_handlers *err_handler;
332 device_lock(&dev->dev);
333 dev->error_state = pci_channel_io_normal;
336 !dev->driver->err_handler ||
337 !dev->driver->err_handler->resume)
340 err_handler = dev->driver->err_handler;
341 err_handler->resume(dev);
343 device_unlock(&dev->dev);
348 * broadcast_error_message - handle message broadcast to downstream drivers
349 * @dev: pointer to from where in a hierarchy message is broadcasted down
350 * @state: error state
351 * @error_mesg: message to print
352 * @cb: callback to be broadcasted
354 * Invoked during error recovery process. Once being invoked, the content
355 * of error severity will be broadcasted to all downstream drivers in a
356 * hierarchy in question.
358 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
359 enum pci_channel_state state,
361 int (*cb)(struct pci_dev *, void *))
363 struct aer_broadcast_data result_data;
365 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
366 result_data.state = state;
367 if (cb == report_error_detected)
368 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
370 result_data.result = PCI_ERS_RESULT_RECOVERED;
372 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
374 * If the error is reported by a bridge, we think this error
375 * is related to the downstream link of the bridge, so we
376 * do error recovery on all subordinates of the bridge instead
377 * of the bridge and clear the error status of the bridge.
379 if (cb == report_error_detected)
380 dev->error_state = state;
381 pci_walk_bus(dev->subordinate, cb, &result_data);
382 if (cb == report_resume) {
383 pci_cleanup_aer_uncorrect_error_status(dev);
384 dev->error_state = pci_channel_io_normal;
388 * If the error is reported by an end point, we think this
389 * error is related to the upstream link of the end point.
391 if (state == pci_channel_io_normal)
393 * the error is non fatal so the bus is ok, just invoke
394 * the callback for the function that logged the error.
396 cb(dev, &result_data);
398 pci_walk_bus(dev->bus, cb, &result_data);
401 return result_data.result;
405 * default_reset_link - default reset function
406 * @dev: pointer to pci_dev data structure
408 * Invoked when performing link reset on a Downstream Port or a
409 * Root Port with no aer driver.
411 static pci_ers_result_t default_reset_link(struct pci_dev *dev)
413 pci_reset_bridge_secondary_bus(dev);
414 dev_printk(KERN_DEBUG, &dev->dev, "downstream link has been reset\n");
415 return PCI_ERS_RESULT_RECOVERED;
418 static int find_aer_service_iter(struct device *device, void *data)
420 struct pcie_port_service_driver *service_driver, **drv;
422 drv = (struct pcie_port_service_driver **) data;
424 if (device->bus == &pcie_port_bus_type && device->driver) {
425 service_driver = to_service_driver(device->driver);
426 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
427 *drv = service_driver;
435 static struct pcie_port_service_driver *find_aer_service(struct pci_dev *dev)
437 struct pcie_port_service_driver *drv = NULL;
439 device_for_each_child(&dev->dev, &drv, find_aer_service_iter);
444 static pci_ers_result_t reset_link(struct pci_dev *dev)
446 struct pci_dev *udev;
447 pci_ers_result_t status;
448 struct pcie_port_service_driver *driver;
450 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
451 /* Reset this port for all subordinates */
454 /* Reset the upstream component (likely downstream port) */
455 udev = dev->bus->self;
458 /* Use the aer driver of the component firstly */
459 driver = find_aer_service(udev);
461 if (driver && driver->reset_link) {
462 status = driver->reset_link(udev);
463 } else if (udev->has_secondary_link) {
464 status = default_reset_link(udev);
466 dev_printk(KERN_DEBUG, &dev->dev,
467 "no link-reset support at upstream device %s\n",
469 return PCI_ERS_RESULT_DISCONNECT;
472 if (status != PCI_ERS_RESULT_RECOVERED) {
473 dev_printk(KERN_DEBUG, &dev->dev,
474 "link reset at upstream device %s failed\n",
476 return PCI_ERS_RESULT_DISCONNECT;
483 * do_recovery - handle nonfatal/fatal error recovery process
484 * @dev: pointer to a pci_dev data structure of agent detecting an error
485 * @severity: error severity type
487 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
488 * error detected message to all downstream drivers within a hierarchy in
489 * question and return the returned code.
491 static void do_recovery(struct pci_dev *dev, int severity)
493 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
494 enum pci_channel_state state;
496 if (severity == AER_FATAL)
497 state = pci_channel_io_frozen;
499 state = pci_channel_io_normal;
501 status = broadcast_error_message(dev,
504 report_error_detected);
506 if (severity == AER_FATAL) {
507 result = reset_link(dev);
508 if (result != PCI_ERS_RESULT_RECOVERED)
512 if (status == PCI_ERS_RESULT_CAN_RECOVER)
513 status = broadcast_error_message(dev,
516 report_mmio_enabled);
518 if (status == PCI_ERS_RESULT_NEED_RESET) {
520 * TODO: Should call platform-specific
521 * functions to reset slot before calling
522 * drivers' slot_reset callbacks?
524 status = broadcast_error_message(dev,
530 if (status != PCI_ERS_RESULT_RECOVERED)
533 broadcast_error_message(dev,
538 dev_info(&dev->dev, "AER: Device recovery successful\n");
542 /* TODO: Should kernel panic here? */
543 dev_info(&dev->dev, "AER: Device recovery failed\n");
547 * handle_error_source - handle logging error into an event log
548 * @aerdev: pointer to pcie_device data structure of the root port
549 * @dev: pointer to pci_dev data structure of error source device
550 * @info: comprehensive error information
552 * Invoked when an error being detected by Root Port.
554 static void handle_error_source(struct pcie_device *aerdev,
556 struct aer_err_info *info)
560 if (info->severity == AER_CORRECTABLE) {
562 * Correctable error does not need software intervention.
563 * No need to go through error recovery process.
565 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
567 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
570 do_recovery(dev, info->severity);
573 #ifdef CONFIG_ACPI_APEI_PCIEAER
574 static void aer_recover_work_func(struct work_struct *work);
576 #define AER_RECOVER_RING_ORDER 4
577 #define AER_RECOVER_RING_SIZE (1 << AER_RECOVER_RING_ORDER)
579 struct aer_recover_entry {
584 struct aer_capability_regs *regs;
587 static DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
588 AER_RECOVER_RING_SIZE);
590 * Mutual exclusion for writers of aer_recover_ring, reader side don't
591 * need lock, because there is only one reader and lock is not needed
592 * between reader and writer.
594 static DEFINE_SPINLOCK(aer_recover_ring_lock);
595 static DECLARE_WORK(aer_recover_work, aer_recover_work_func);
597 void aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
598 int severity, struct aer_capability_regs *aer_regs)
601 struct aer_recover_entry entry = {
605 .severity = severity,
609 spin_lock_irqsave(&aer_recover_ring_lock, flags);
610 if (kfifo_put(&aer_recover_ring, entry))
611 schedule_work(&aer_recover_work);
613 pr_err("AER recover: Buffer overflow when recovering AER for %04x:%02x:%02x:%x\n",
614 domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
615 spin_unlock_irqrestore(&aer_recover_ring_lock, flags);
617 EXPORT_SYMBOL_GPL(aer_recover_queue);
619 static void aer_recover_work_func(struct work_struct *work)
621 struct aer_recover_entry entry;
622 struct pci_dev *pdev;
624 while (kfifo_get(&aer_recover_ring, &entry)) {
625 pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
628 pr_err("AER recover: Can not find pci_dev for %04x:%02x:%02x:%x\n",
629 entry.domain, entry.bus,
630 PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
633 cper_print_aer(pdev, entry.severity, entry.regs);
634 do_recovery(pdev, entry.severity);
641 * get_device_error_info - read error status from dev and store it to info
642 * @dev: pointer to the device expected to have a error record
643 * @info: pointer to structure to store the error record
645 * Return 1 on success, 0 on error.
647 * Note that @info is reused among all error devices. Clear fields properly.
649 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
653 /* Must reset in this function */
655 info->tlp_header_valid = 0;
657 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
659 /* The device might not support AER */
663 if (info->severity == AER_CORRECTABLE) {
664 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
666 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
668 if (!(info->status & ~info->mask))
670 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
671 info->severity == AER_NONFATAL) {
673 /* Link is still healthy for IO reads */
674 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
676 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
678 if (!(info->status & ~info->mask))
681 /* Get First Error Pointer */
682 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
683 info->first_error = PCI_ERR_CAP_FEP(temp);
685 if (info->status & AER_LOG_TLP_MASKS) {
686 info->tlp_header_valid = 1;
687 pci_read_config_dword(dev,
688 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
689 pci_read_config_dword(dev,
690 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
691 pci_read_config_dword(dev,
692 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
693 pci_read_config_dword(dev,
694 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
701 static inline void aer_process_err_devices(struct pcie_device *p_device,
702 struct aer_err_info *e_info)
706 /* Report all before handle them, not to lost records by reset etc. */
707 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
708 if (get_device_error_info(e_info->dev[i], e_info))
709 aer_print_error(e_info->dev[i], e_info);
711 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
712 if (get_device_error_info(e_info->dev[i], e_info))
713 handle_error_source(p_device, e_info->dev[i], e_info);
718 * aer_isr_one_error - consume an error detected by root port
719 * @p_device: pointer to error root port service device
720 * @e_src: pointer to an error source
722 static void aer_isr_one_error(struct pcie_device *p_device,
723 struct aer_err_source *e_src)
725 struct aer_err_info *e_info;
727 /* struct aer_err_info might be big, so we allocate it with slab */
728 e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
730 dev_printk(KERN_DEBUG, &p_device->port->dev,
731 "Can't allocate mem when processing AER errors\n");
736 * There is a possibility that both correctable error and
737 * uncorrectable error being logged. Report correctable error first.
739 if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
740 e_info->id = ERR_COR_ID(e_src->id);
741 e_info->severity = AER_CORRECTABLE;
743 if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
744 e_info->multi_error_valid = 1;
746 e_info->multi_error_valid = 0;
748 aer_print_port_info(p_device->port, e_info);
750 if (find_source_device(p_device->port, e_info))
751 aer_process_err_devices(p_device, e_info);
754 if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
755 e_info->id = ERR_UNCOR_ID(e_src->id);
757 if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
758 e_info->severity = AER_FATAL;
760 e_info->severity = AER_NONFATAL;
762 if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
763 e_info->multi_error_valid = 1;
765 e_info->multi_error_valid = 0;
767 aer_print_port_info(p_device->port, e_info);
769 if (find_source_device(p_device->port, e_info))
770 aer_process_err_devices(p_device, e_info);
777 * get_e_source - retrieve an error source
778 * @rpc: pointer to the root port which holds an error
779 * @e_src: pointer to store retrieved error source
781 * Return 1 if an error source is retrieved, otherwise 0.
783 * Invoked by DPC handler to consume an error.
785 static int get_e_source(struct aer_rpc *rpc, struct aer_err_source *e_src)
789 /* Lock access to Root error producer/consumer index */
790 spin_lock_irqsave(&rpc->e_lock, flags);
791 if (rpc->prod_idx == rpc->cons_idx) {
792 spin_unlock_irqrestore(&rpc->e_lock, flags);
796 *e_src = rpc->e_sources[rpc->cons_idx];
798 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
800 spin_unlock_irqrestore(&rpc->e_lock, flags);
806 * aer_isr - consume errors detected by root port
807 * @work: definition of this work item
809 * Invoked, as DPC, when root port records new detected error
811 void aer_isr(struct work_struct *work)
813 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
814 struct pcie_device *p_device = rpc->rpd;
815 struct aer_err_source uninitialized_var(e_src);
817 mutex_lock(&rpc->rpc_mutex);
818 while (get_e_source(rpc, &e_src))
819 aer_isr_one_error(p_device, &e_src);
820 mutex_unlock(&rpc->rpc_mutex);
824 * aer_init - provide AER initialization
825 * @dev: pointer to AER pcie device
827 * Invoked when AER service driver is loaded.
829 int aer_init(struct pcie_device *dev)
832 dev_printk(KERN_DEBUG, &dev->device,
833 "aerdrv forceload requested.\n");
834 pcie_aer_force_firmware_first(dev->port, 0);