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>
30 static int nosourceid;
31 module_param(forceload, bool, 0);
32 module_param(nosourceid, bool, 0);
34 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
39 if (dev->aer_firmware_first)
42 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
46 pos = pci_pcie_cap(dev);
50 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
53 PCI_EXP_DEVCTL_NFERE |
56 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
60 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
62 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
67 if (dev->aer_firmware_first)
70 pos = pci_pcie_cap(dev);
74 pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
75 reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
76 PCI_EXP_DEVCTL_NFERE |
79 pci_write_config_word(dev, pos+PCI_EXP_DEVCTL, reg16);
83 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
85 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
90 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
94 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
96 pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
100 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
102 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
104 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
105 e_info->dev[e_info->error_dev_num] = dev;
106 e_info->error_dev_num++;
114 #define PCI_BUS(x) (((x) >> 8) & 0xff)
117 * is_error_source - check whether the device is source of reported error
118 * @dev: pointer to pci_dev to be checked
119 * @e_info: pointer to reported error info
121 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
128 * When bus id is equal to 0, it might be a bad id
129 * reported by root port.
131 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
132 /* Device ID match? */
133 if (e_info->id == ((dev->bus->number << 8) | dev->devfn))
136 /* Continue id comparing if there is no multiple error */
137 if (!e_info->multi_error_valid)
144 * 2) bus id is equal to 0. Some ports might lose the bus
145 * id of error source id;
146 * 3) There are multiple errors and prior id comparing fails;
147 * We check AER status registers to find possible reporter.
149 if (atomic_read(&dev->enable_cnt) == 0)
151 pos = pci_pcie_cap(dev);
155 /* Check if AER is enabled */
156 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, ®16);
158 PCI_EXP_DEVCTL_CERE |
159 PCI_EXP_DEVCTL_NFERE |
160 PCI_EXP_DEVCTL_FERE |
161 PCI_EXP_DEVCTL_URRE)))
163 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
167 /* Check if error is recorded */
168 if (e_info->severity == AER_CORRECTABLE) {
169 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
170 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
172 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
173 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
181 static int find_device_iter(struct pci_dev *dev, void *data)
183 struct aer_err_info *e_info = (struct aer_err_info *)data;
185 if (is_error_source(dev, e_info)) {
186 add_error_device(e_info, dev);
188 /* If there is only a single error, stop iteration */
189 if (!e_info->multi_error_valid)
196 * find_source_device - search through device hierarchy for source device
197 * @parent: pointer to Root Port pci_dev data structure
198 * @e_info: including detailed error information such like id
200 * Return true if found.
202 * Invoked by DPC when error is detected at the Root Port.
204 static bool find_source_device(struct pci_dev *parent,
205 struct aer_err_info *e_info)
207 struct pci_dev *dev = parent;
210 /* Is Root Port an agent that sends error message? */
211 result = find_device_iter(dev, e_info);
215 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
217 if (!e_info->error_dev_num) {
218 dev_printk(KERN_DEBUG, &parent->dev,
219 "can't find device of ID%04x\n",
226 static int report_error_detected(struct pci_dev *dev, void *data)
228 pci_ers_result_t vote;
229 struct pci_error_handlers *err_handler;
230 struct aer_broadcast_data *result_data;
231 result_data = (struct aer_broadcast_data *) data;
233 dev->error_state = result_data->state;
236 !dev->driver->err_handler ||
237 !dev->driver->err_handler->error_detected) {
238 if (result_data->state == pci_channel_io_frozen &&
239 !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
241 * In case of fatal recovery, if one of down-
242 * stream device has no driver. We might be
243 * unable to recover because a later insmod
244 * of a driver for this device is unaware of
247 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
249 "no AER-aware driver" : "no driver");
254 err_handler = dev->driver->err_handler;
255 vote = err_handler->error_detected(dev, result_data->state);
256 result_data->result = merge_result(result_data->result, vote);
260 static int report_mmio_enabled(struct pci_dev *dev, void *data)
262 pci_ers_result_t vote;
263 struct pci_error_handlers *err_handler;
264 struct aer_broadcast_data *result_data;
265 result_data = (struct aer_broadcast_data *) data;
268 !dev->driver->err_handler ||
269 !dev->driver->err_handler->mmio_enabled)
272 err_handler = dev->driver->err_handler;
273 vote = err_handler->mmio_enabled(dev);
274 result_data->result = merge_result(result_data->result, vote);
278 static int report_slot_reset(struct pci_dev *dev, void *data)
280 pci_ers_result_t vote;
281 struct pci_error_handlers *err_handler;
282 struct aer_broadcast_data *result_data;
283 result_data = (struct aer_broadcast_data *) data;
286 !dev->driver->err_handler ||
287 !dev->driver->err_handler->slot_reset)
290 err_handler = dev->driver->err_handler;
291 vote = err_handler->slot_reset(dev);
292 result_data->result = merge_result(result_data->result, vote);
296 static int report_resume(struct pci_dev *dev, void *data)
298 struct pci_error_handlers *err_handler;
300 dev->error_state = pci_channel_io_normal;
303 !dev->driver->err_handler ||
304 !dev->driver->err_handler->resume)
307 err_handler = dev->driver->err_handler;
308 err_handler->resume(dev);
313 * broadcast_error_message - handle message broadcast to downstream drivers
314 * @dev: pointer to from where in a hierarchy message is broadcasted down
315 * @state: error state
316 * @error_mesg: message to print
317 * @cb: callback to be broadcasted
319 * Invoked during error recovery process. Once being invoked, the content
320 * of error severity will be broadcasted to all downstream drivers in a
321 * hierarchy in question.
323 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
324 enum pci_channel_state state,
326 int (*cb)(struct pci_dev *, void *))
328 struct aer_broadcast_data result_data;
330 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
331 result_data.state = state;
332 if (cb == report_error_detected)
333 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
335 result_data.result = PCI_ERS_RESULT_RECOVERED;
337 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
339 * If the error is reported by a bridge, we think this error
340 * is related to the downstream link of the bridge, so we
341 * do error recovery on all subordinates of the bridge instead
342 * of the bridge and clear the error status of the bridge.
344 if (cb == report_error_detected)
345 dev->error_state = state;
346 pci_walk_bus(dev->subordinate, cb, &result_data);
347 if (cb == report_resume) {
348 pci_cleanup_aer_uncorrect_error_status(dev);
349 dev->error_state = pci_channel_io_normal;
353 * If the error is reported by an end point, we think this
354 * error is related to the upstream link of the end point.
356 pci_walk_bus(dev->bus, cb, &result_data);
359 return result_data.result;
362 struct find_aer_service_data {
363 struct pcie_port_service_driver *aer_driver;
367 static int find_aer_service_iter(struct device *device, void *data)
369 struct device_driver *driver;
370 struct pcie_port_service_driver *service_driver;
371 struct find_aer_service_data *result;
373 result = (struct find_aer_service_data *) data;
375 if (device->bus == &pcie_port_bus_type) {
376 struct pcie_device *pcie = to_pcie_device(device);
378 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
379 result->is_downstream = 1;
381 driver = device->driver;
383 service_driver = to_service_driver(driver);
384 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
385 result->aer_driver = service_driver;
394 static void find_aer_service(struct pci_dev *dev,
395 struct find_aer_service_data *data)
398 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
401 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
404 struct pci_dev *udev;
405 pci_ers_result_t status;
406 struct find_aer_service_data data;
408 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
411 udev = dev->bus->self;
413 data.is_downstream = 0;
414 data.aer_driver = NULL;
415 find_aer_service(udev, &data);
418 * Use the aer driver of the error agent firstly.
419 * If it hasn't the aer driver, use the root port's
421 if (!data.aer_driver || !data.aer_driver->reset_link) {
422 if (data.is_downstream &&
423 aerdev->device.driver &&
424 to_service_driver(aerdev->device.driver)->reset_link) {
426 to_service_driver(aerdev->device.driver);
428 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
430 return PCI_ERS_RESULT_DISCONNECT;
434 status = data.aer_driver->reset_link(udev);
435 if (status != PCI_ERS_RESULT_RECOVERED) {
436 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
437 "device %s failed\n", pci_name(udev));
438 return PCI_ERS_RESULT_DISCONNECT;
445 * do_recovery - handle nonfatal/fatal error recovery process
446 * @aerdev: pointer to a pcie_device data structure of root port
447 * @dev: pointer to a pci_dev data structure of agent detecting an error
448 * @severity: error severity type
450 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
451 * error detected message to all downstream drivers within a hierarchy in
452 * question and return the returned code.
454 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
458 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
459 enum pci_channel_state state;
461 if (severity == AER_FATAL)
462 state = pci_channel_io_frozen;
464 state = pci_channel_io_normal;
466 status = broadcast_error_message(dev,
469 report_error_detected);
471 if (severity == AER_FATAL) {
472 result = reset_link(aerdev, dev);
473 if (result != PCI_ERS_RESULT_RECOVERED) {
474 /* TODO: Should panic here? */
479 if (status == PCI_ERS_RESULT_CAN_RECOVER)
480 status = broadcast_error_message(dev,
483 report_mmio_enabled);
485 if (status == PCI_ERS_RESULT_NEED_RESET) {
487 * TODO: Should call platform-specific
488 * functions to reset slot before calling
489 * drivers' slot_reset callbacks?
491 status = broadcast_error_message(dev,
497 if (status == PCI_ERS_RESULT_RECOVERED)
498 broadcast_error_message(dev,
507 * handle_error_source - handle logging error into an event log
508 * @aerdev: pointer to pcie_device data structure of the root port
509 * @dev: pointer to pci_dev data structure of error source device
510 * @info: comprehensive error information
512 * Invoked when an error being detected by Root Port.
514 static void handle_error_source(struct pcie_device *aerdev,
516 struct aer_err_info *info)
518 pci_ers_result_t status = 0;
521 if (info->severity == AER_CORRECTABLE) {
523 * Correctable error does not need software intevention.
524 * No need to go through error recovery process.
526 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
528 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
531 status = do_recovery(aerdev, dev, info->severity);
532 if (status == PCI_ERS_RESULT_RECOVERED) {
533 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
534 "successfully recovered\n");
536 /* TODO: Should kernel panic here? */
537 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
544 * get_e_source - retrieve an error source
545 * @rpc: pointer to the root port which holds an error
547 * Invoked by DPC handler to consume an error.
549 static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
551 struct aer_err_source *e_source;
554 /* Lock access to Root error producer/consumer index */
555 spin_lock_irqsave(&rpc->e_lock, flags);
556 if (rpc->prod_idx == rpc->cons_idx) {
557 spin_unlock_irqrestore(&rpc->e_lock, flags);
560 e_source = &rpc->e_sources[rpc->cons_idx];
562 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
564 spin_unlock_irqrestore(&rpc->e_lock, flags);
570 * get_device_error_info - read error status from dev and store it to info
571 * @dev: pointer to the device expected to have a error record
572 * @info: pointer to structure to store the error record
574 * Return 1 on success, 0 on error.
576 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
581 info->tlp_header_valid = 0;
583 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
585 /* The device might not support AER */
589 if (info->severity == AER_CORRECTABLE) {
590 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
592 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
594 if (!(info->status & ~info->mask))
596 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
597 info->severity == AER_NONFATAL) {
599 /* Link is still healthy for IO reads */
600 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
602 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
604 if (!(info->status & ~info->mask))
607 /* Get First Error Pointer */
608 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
609 info->first_error = PCI_ERR_CAP_FEP(temp);
611 if (info->status & AER_LOG_TLP_MASKS) {
612 info->tlp_header_valid = 1;
613 pci_read_config_dword(dev,
614 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
615 pci_read_config_dword(dev,
616 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
617 pci_read_config_dword(dev,
618 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
619 pci_read_config_dword(dev,
620 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
627 static inline void aer_process_err_devices(struct pcie_device *p_device,
628 struct aer_err_info *e_info)
632 /* Report all before handle them, not to lost records by reset etc. */
633 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
634 if (get_device_error_info(e_info->dev[i], e_info))
635 aer_print_error(e_info->dev[i], e_info);
637 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
638 if (get_device_error_info(e_info->dev[i], e_info))
639 handle_error_source(p_device, e_info->dev[i], e_info);
644 * aer_isr_one_error - consume an error detected by root port
645 * @p_device: pointer to error root port service device
646 * @e_src: pointer to an error source
648 static void aer_isr_one_error(struct pcie_device *p_device,
649 struct aer_err_source *e_src)
651 struct aer_err_info *e_info;
654 /* struct aer_err_info might be big, so we allocate it with slab */
655 e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
656 if (e_info == NULL) {
657 dev_printk(KERN_DEBUG, &p_device->port->dev,
658 "Can't allocate mem when processing AER errors\n");
663 * There is a possibility that both correctable error and
664 * uncorrectable error being logged. Report correctable error first.
666 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
669 if (!(e_src->status & i))
672 memset(e_info, 0, sizeof(struct aer_err_info));
674 /* Init comprehensive error information */
675 if (i & PCI_ERR_ROOT_COR_RCV) {
676 e_info->id = ERR_COR_ID(e_src->id);
677 e_info->severity = AER_CORRECTABLE;
679 e_info->id = ERR_UNCOR_ID(e_src->id);
680 e_info->severity = ((e_src->status >> 6) & 1);
683 (PCI_ERR_ROOT_MULTI_COR_RCV |
684 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
685 e_info->multi_error_valid = 1;
687 aer_print_port_info(p_device->port, e_info);
689 if (find_source_device(p_device->port, e_info))
690 aer_process_err_devices(p_device, e_info);
697 * aer_isr - consume errors detected by root port
698 * @work: definition of this work item
700 * Invoked, as DPC, when root port records new detected error
702 void aer_isr(struct work_struct *work)
704 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
705 struct pcie_device *p_device = rpc->rpd;
706 struct aer_err_source *e_src;
708 mutex_lock(&rpc->rpc_mutex);
709 e_src = get_e_source(rpc);
711 aer_isr_one_error(p_device, e_src);
712 e_src = get_e_source(rpc);
714 mutex_unlock(&rpc->rpc_mutex);
716 wake_up(&rpc->wait_release);
720 * aer_init - provide AER initialization
721 * @dev: pointer to AER pcie device
723 * Invoked when AER service driver is loaded.
725 int aer_init(struct pcie_device *dev)
727 if (dev->port->aer_firmware_first) {
728 dev_printk(KERN_DEBUG, &dev->device,
729 "PCIe errors handled by platform firmware.\n");
733 if (aer_osc_setup(dev))
739 dev_printk(KERN_DEBUG, &dev->device,
740 "aerdrv forceload requested.\n");
741 dev->port->aer_firmware_first = 0;