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 inline int compare_device_id(struct pci_dev *dev,
103 struct aer_err_info *e_info)
105 if (e_info->id == ((dev->bus->number << 8) | dev->devfn)) {
115 static int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
117 if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
118 e_info->dev[e_info->error_dev_num] = dev;
119 e_info->error_dev_num++;
127 #define PCI_BUS(x) (((x) >> 8) & 0xff)
130 * is_error_source - check whether the device is source of reported error
131 * @dev: pointer to pci_dev to be checked
132 * @e_info: pointer to reported error info
134 static bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
142 * When bus id is equal to 0, it might be a bad id
143 * reported by root port.
145 if (!nosourceid && (PCI_BUS(e_info->id) != 0)) {
146 result = compare_device_id(dev, e_info);
150 /* Continue id comparing if there is no multiple error */
151 if (!e_info->multi_error_valid)
158 * 2) bus id is equal to 0. Some ports might lose the bus
159 * id of error source id;
160 * 3) There are multiple errors and prior id comparing fails;
161 * We check AER status registers to find possible reporter.
163 if (atomic_read(&dev->enable_cnt) == 0)
165 pos = pci_pcie_cap(dev);
169 /* Check if AER is enabled */
170 pci_read_config_word(dev, pos + PCI_EXP_DEVCTL, ®16);
172 PCI_EXP_DEVCTL_CERE |
173 PCI_EXP_DEVCTL_NFERE |
174 PCI_EXP_DEVCTL_FERE |
175 PCI_EXP_DEVCTL_URRE)))
177 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
181 /* Check if error is recorded */
182 if (e_info->severity == AER_CORRECTABLE) {
183 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
184 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK, &mask);
186 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
187 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK, &mask);
195 static int find_device_iter(struct pci_dev *dev, void *data)
197 struct aer_err_info *e_info = (struct aer_err_info *)data;
199 if (is_error_source(dev, e_info)) {
200 add_error_device(e_info, dev);
202 /* If there is only a single error, stop iteration */
203 if (!e_info->multi_error_valid)
210 * find_source_device - search through device hierarchy for source device
211 * @parent: pointer to Root Port pci_dev data structure
212 * @e_info: including detailed error information such like id
214 * Return true if found.
216 * Invoked by DPC when error is detected at the Root Port.
218 static bool find_source_device(struct pci_dev *parent,
219 struct aer_err_info *e_info)
221 struct pci_dev *dev = parent;
224 /* Is Root Port an agent that sends error message? */
225 result = find_device_iter(dev, e_info);
229 pci_walk_bus(parent->subordinate, find_device_iter, e_info);
231 if (!e_info->error_dev_num) {
232 dev_printk(KERN_DEBUG, &parent->dev,
233 "can't find device of ID%04x\n",
240 static int report_error_detected(struct pci_dev *dev, void *data)
242 pci_ers_result_t vote;
243 struct pci_error_handlers *err_handler;
244 struct aer_broadcast_data *result_data;
245 result_data = (struct aer_broadcast_data *) data;
247 dev->error_state = result_data->state;
250 !dev->driver->err_handler ||
251 !dev->driver->err_handler->error_detected) {
252 if (result_data->state == pci_channel_io_frozen &&
253 !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
255 * In case of fatal recovery, if one of down-
256 * stream device has no driver. We might be
257 * unable to recover because a later insmod
258 * of a driver for this device is unaware of
261 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
263 "no AER-aware driver" : "no driver");
268 err_handler = dev->driver->err_handler;
269 vote = err_handler->error_detected(dev, result_data->state);
270 result_data->result = merge_result(result_data->result, vote);
274 static int report_mmio_enabled(struct pci_dev *dev, void *data)
276 pci_ers_result_t vote;
277 struct pci_error_handlers *err_handler;
278 struct aer_broadcast_data *result_data;
279 result_data = (struct aer_broadcast_data *) data;
282 !dev->driver->err_handler ||
283 !dev->driver->err_handler->mmio_enabled)
286 err_handler = dev->driver->err_handler;
287 vote = err_handler->mmio_enabled(dev);
288 result_data->result = merge_result(result_data->result, vote);
292 static int report_slot_reset(struct pci_dev *dev, void *data)
294 pci_ers_result_t vote;
295 struct pci_error_handlers *err_handler;
296 struct aer_broadcast_data *result_data;
297 result_data = (struct aer_broadcast_data *) data;
300 !dev->driver->err_handler ||
301 !dev->driver->err_handler->slot_reset)
304 err_handler = dev->driver->err_handler;
305 vote = err_handler->slot_reset(dev);
306 result_data->result = merge_result(result_data->result, vote);
310 static int report_resume(struct pci_dev *dev, void *data)
312 struct pci_error_handlers *err_handler;
314 dev->error_state = pci_channel_io_normal;
317 !dev->driver->err_handler ||
318 !dev->driver->err_handler->resume)
321 err_handler = dev->driver->err_handler;
322 err_handler->resume(dev);
327 * broadcast_error_message - handle message broadcast to downstream drivers
328 * @dev: pointer to from where in a hierarchy message is broadcasted down
329 * @state: error state
330 * @error_mesg: message to print
331 * @cb: callback to be broadcasted
333 * Invoked during error recovery process. Once being invoked, the content
334 * of error severity will be broadcasted to all downstream drivers in a
335 * hierarchy in question.
337 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
338 enum pci_channel_state state,
340 int (*cb)(struct pci_dev *, void *))
342 struct aer_broadcast_data result_data;
344 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
345 result_data.state = state;
346 if (cb == report_error_detected)
347 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
349 result_data.result = PCI_ERS_RESULT_RECOVERED;
351 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
353 * If the error is reported by a bridge, we think this error
354 * is related to the downstream link of the bridge, so we
355 * do error recovery on all subordinates of the bridge instead
356 * of the bridge and clear the error status of the bridge.
358 if (cb == report_error_detected)
359 dev->error_state = state;
360 pci_walk_bus(dev->subordinate, cb, &result_data);
361 if (cb == report_resume) {
362 pci_cleanup_aer_uncorrect_error_status(dev);
363 dev->error_state = pci_channel_io_normal;
367 * If the error is reported by an end point, we think this
368 * error is related to the upstream link of the end point.
370 pci_walk_bus(dev->bus, cb, &result_data);
373 return result_data.result;
376 struct find_aer_service_data {
377 struct pcie_port_service_driver *aer_driver;
381 static int find_aer_service_iter(struct device *device, void *data)
383 struct device_driver *driver;
384 struct pcie_port_service_driver *service_driver;
385 struct find_aer_service_data *result;
387 result = (struct find_aer_service_data *) data;
389 if (device->bus == &pcie_port_bus_type) {
390 struct pcie_device *pcie = to_pcie_device(device);
392 if (pcie->port->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
393 result->is_downstream = 1;
395 driver = device->driver;
397 service_driver = to_service_driver(driver);
398 if (service_driver->service == PCIE_PORT_SERVICE_AER) {
399 result->aer_driver = service_driver;
408 static void find_aer_service(struct pci_dev *dev,
409 struct find_aer_service_data *data)
412 retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
415 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
418 struct pci_dev *udev;
419 pci_ers_result_t status;
420 struct find_aer_service_data data;
422 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
425 udev = dev->bus->self;
427 data.is_downstream = 0;
428 data.aer_driver = NULL;
429 find_aer_service(udev, &data);
432 * Use the aer driver of the error agent firstly.
433 * If it hasn't the aer driver, use the root port's
435 if (!data.aer_driver || !data.aer_driver->reset_link) {
436 if (data.is_downstream &&
437 aerdev->device.driver &&
438 to_service_driver(aerdev->device.driver)->reset_link) {
440 to_service_driver(aerdev->device.driver);
442 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
444 return PCI_ERS_RESULT_DISCONNECT;
448 status = data.aer_driver->reset_link(udev);
449 if (status != PCI_ERS_RESULT_RECOVERED) {
450 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
451 "device %s failed\n", pci_name(udev));
452 return PCI_ERS_RESULT_DISCONNECT;
459 * do_recovery - handle nonfatal/fatal error recovery process
460 * @aerdev: pointer to a pcie_device data structure of root port
461 * @dev: pointer to a pci_dev data structure of agent detecting an error
462 * @severity: error severity type
464 * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
465 * error detected message to all downstream drivers within a hierarchy in
466 * question and return the returned code.
468 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
472 pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
473 enum pci_channel_state state;
475 if (severity == AER_FATAL)
476 state = pci_channel_io_frozen;
478 state = pci_channel_io_normal;
480 status = broadcast_error_message(dev,
483 report_error_detected);
485 if (severity == AER_FATAL) {
486 result = reset_link(aerdev, dev);
487 if (result != PCI_ERS_RESULT_RECOVERED) {
488 /* TODO: Should panic here? */
493 if (status == PCI_ERS_RESULT_CAN_RECOVER)
494 status = broadcast_error_message(dev,
497 report_mmio_enabled);
499 if (status == PCI_ERS_RESULT_NEED_RESET) {
501 * TODO: Should call platform-specific
502 * functions to reset slot before calling
503 * drivers' slot_reset callbacks?
505 status = broadcast_error_message(dev,
511 if (status == PCI_ERS_RESULT_RECOVERED)
512 broadcast_error_message(dev,
521 * handle_error_source - handle logging error into an event log
522 * @aerdev: pointer to pcie_device data structure of the root port
523 * @dev: pointer to pci_dev data structure of error source device
524 * @info: comprehensive error information
526 * Invoked when an error being detected by Root Port.
528 static void handle_error_source(struct pcie_device *aerdev,
530 struct aer_err_info *info)
532 pci_ers_result_t status = 0;
535 if (info->severity == AER_CORRECTABLE) {
537 * Correctable error does not need software intevention.
538 * No need to go through error recovery process.
540 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
542 pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
545 status = do_recovery(aerdev, dev, info->severity);
546 if (status == PCI_ERS_RESULT_RECOVERED) {
547 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
548 "successfully recovered\n");
550 /* TODO: Should kernel panic here? */
551 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
558 * get_e_source - retrieve an error source
559 * @rpc: pointer to the root port which holds an error
561 * Invoked by DPC handler to consume an error.
563 static struct aer_err_source *get_e_source(struct aer_rpc *rpc)
565 struct aer_err_source *e_source;
568 /* Lock access to Root error producer/consumer index */
569 spin_lock_irqsave(&rpc->e_lock, flags);
570 if (rpc->prod_idx == rpc->cons_idx) {
571 spin_unlock_irqrestore(&rpc->e_lock, flags);
574 e_source = &rpc->e_sources[rpc->cons_idx];
576 if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
578 spin_unlock_irqrestore(&rpc->e_lock, flags);
584 * get_device_error_info - read error status from dev and store it to info
585 * @dev: pointer to the device expected to have a error record
586 * @info: pointer to structure to store the error record
588 * Return 1 on success, 0 on error.
590 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
595 info->tlp_header_valid = 0;
597 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
599 /* The device might not support AER */
603 if (info->severity == AER_CORRECTABLE) {
604 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
606 pci_read_config_dword(dev, pos + PCI_ERR_COR_MASK,
608 if (!(info->status & ~info->mask))
610 } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
611 info->severity == AER_NONFATAL) {
613 /* Link is still healthy for IO reads */
614 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
616 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_MASK,
618 if (!(info->status & ~info->mask))
621 /* Get First Error Pointer */
622 pci_read_config_dword(dev, pos + PCI_ERR_CAP, &temp);
623 info->first_error = PCI_ERR_CAP_FEP(temp);
625 if (info->status & AER_LOG_TLP_MASKS) {
626 info->tlp_header_valid = 1;
627 pci_read_config_dword(dev,
628 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
629 pci_read_config_dword(dev,
630 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
631 pci_read_config_dword(dev,
632 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
633 pci_read_config_dword(dev,
634 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
641 static inline void aer_process_err_devices(struct pcie_device *p_device,
642 struct aer_err_info *e_info)
646 /* Report all before handle them, not to lost records by reset etc. */
647 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
648 if (get_device_error_info(e_info->dev[i], e_info))
649 aer_print_error(e_info->dev[i], e_info);
651 for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
652 if (get_device_error_info(e_info->dev[i], e_info))
653 handle_error_source(p_device, e_info->dev[i], e_info);
658 * aer_isr_one_error - consume an error detected by root port
659 * @p_device: pointer to error root port service device
660 * @e_src: pointer to an error source
662 static void aer_isr_one_error(struct pcie_device *p_device,
663 struct aer_err_source *e_src)
665 struct aer_err_info *e_info;
668 /* struct aer_err_info might be big, so we allocate it with slab */
669 e_info = kmalloc(sizeof(struct aer_err_info), GFP_KERNEL);
670 if (e_info == NULL) {
671 dev_printk(KERN_DEBUG, &p_device->port->dev,
672 "Can't allocate mem when processing AER errors\n");
677 * There is a possibility that both correctable error and
678 * uncorrectable error being logged. Report correctable error first.
680 for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
683 if (!(e_src->status & i))
686 memset(e_info, 0, sizeof(struct aer_err_info));
688 /* Init comprehensive error information */
689 if (i & PCI_ERR_ROOT_COR_RCV) {
690 e_info->id = ERR_COR_ID(e_src->id);
691 e_info->severity = AER_CORRECTABLE;
693 e_info->id = ERR_UNCOR_ID(e_src->id);
694 e_info->severity = ((e_src->status >> 6) & 1);
697 (PCI_ERR_ROOT_MULTI_COR_RCV |
698 PCI_ERR_ROOT_MULTI_UNCOR_RCV))
699 e_info->multi_error_valid = 1;
701 aer_print_port_info(p_device->port, e_info);
703 if (find_source_device(p_device->port, e_info))
704 aer_process_err_devices(p_device, e_info);
711 * aer_isr - consume errors detected by root port
712 * @work: definition of this work item
714 * Invoked, as DPC, when root port records new detected error
716 void aer_isr(struct work_struct *work)
718 struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
719 struct pcie_device *p_device = rpc->rpd;
720 struct aer_err_source *e_src;
722 mutex_lock(&rpc->rpc_mutex);
723 e_src = get_e_source(rpc);
725 aer_isr_one_error(p_device, e_src);
726 e_src = get_e_source(rpc);
728 mutex_unlock(&rpc->rpc_mutex);
730 wake_up(&rpc->wait_release);
734 * aer_init - provide AER initialization
735 * @dev: pointer to AER pcie device
737 * Invoked when AER service driver is loaded.
739 int aer_init(struct pcie_device *dev)
741 if (dev->port->aer_firmware_first) {
742 dev_printk(KERN_DEBUG, &dev->device,
743 "PCIe errors handled by platform firmware.\n");
747 if (aer_osc_setup(dev))
753 dev_printk(KERN_DEBUG, &dev->device,
754 "aerdrv forceload requested.\n");
755 dev->port->aer_firmware_first = 0;