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 module_param(forceload, bool, 0);
 
  31 #define PCI_CFG_SPACE_SIZE      (0x100)
 
  32 int pci_find_aer_capability(struct pci_dev *dev)
 
  37         /* Check if it's a pci-express device */
 
  38         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
 
  42         /* Check if it supports pci-express AER */
 
  43         pos = PCI_CFG_SPACE_SIZE;
 
  45                 if (pci_read_config_dword(dev, pos, ®32))
 
  48                 /* some broken boards return ~0 */
 
  49                 if (reg32 == 0xffffffff)
 
  52                 if (PCI_EXT_CAP_ID(reg32) == PCI_EXT_CAP_ID_ERR)
 
  61 int pci_enable_pcie_error_reporting(struct pci_dev *dev)
 
  66         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
 
  70         pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
 
  73                 PCI_EXP_DEVCTL_NFERE |
 
  76         pci_write_config_word(dev, pos+PCI_EXP_DEVCTL,
 
  81 int pci_disable_pcie_error_reporting(struct pci_dev *dev)
 
  86         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
 
  90         pci_read_config_word(dev, pos+PCI_EXP_DEVCTL, ®16);
 
  91         reg16 = reg16 & ~(PCI_EXP_DEVCTL_CERE |
 
  92                         PCI_EXP_DEVCTL_NFERE |
 
  95         pci_write_config_word(dev, pos+PCI_EXP_DEVCTL,
 
 100 int pci_cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
 
 105         pos = pci_find_aer_capability(dev);
 
 109         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
 
 110         pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
 
 111         if (dev->error_state == pci_channel_io_normal)
 
 112                 status &= ~mask; /* Clear corresponding nonfatal bits */
 
 114                 status &= mask; /* Clear corresponding fatal bits */
 
 115         pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
 
 120 int pci_cleanup_aer_correct_error_status(struct pci_dev *dev)
 
 125         pos = pci_find_aer_capability(dev);
 
 129         pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS, &status);
 
 130         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS, status);
 
 135 static int find_device_iter(struct device *device, void *data)
 
 138         u16 id = *(unsigned long *)data;
 
 139         u8 secondary, subordinate, d_bus = id >> 8;
 
 141         if (device->bus == &pci_bus_type) {
 
 142                 dev = to_pci_dev(device);
 
 143                 if (id == ((dev->bus->number << 8) | dev->devfn)) {
 
 147                         *(unsigned long*)data = (unsigned long)device;
 
 152                  * If device is P2P, check if it is an upstream?
 
 154                 if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
 
 155                         pci_read_config_byte(dev, PCI_SECONDARY_BUS,
 
 157                         pci_read_config_byte(dev, PCI_SUBORDINATE_BUS,
 
 159                         if (d_bus >= secondary && d_bus <= subordinate) {
 
 160                                 *(unsigned long*)data = (unsigned long)device;
 
 170  * find_source_device - search through device hierarchy for source device
 
 171  * @parent: pointer to Root Port pci_dev data structure
 
 172  * @id: device ID of agent who sends an error message to this Root Port
 
 174  * Invoked when error is detected at the Root Port.
 
 176 static struct device* find_source_device(struct pci_dev *parent, u16 id)
 
 178         struct pci_dev *dev = parent;
 
 179         struct device *device;
 
 180         unsigned long device_addr;
 
 183         /* Is Root Port an agent that sends error message? */
 
 184         if (id == ((dev->bus->number << 8) | dev->devfn))
 
 189                 if ((status = device_for_each_child(&dev->dev,
 
 190                         &device_addr, find_device_iter))) {
 
 191                         device = (struct device*)device_addr;
 
 192                         dev = to_pci_dev(device);
 
 193                         if (id == ((dev->bus->number << 8) | dev->devfn))
 
 201 static void report_error_detected(struct pci_dev *dev, void *data)
 
 203         pci_ers_result_t vote;
 
 204         struct pci_error_handlers *err_handler;
 
 205         struct aer_broadcast_data *result_data;
 
 206         result_data = (struct aer_broadcast_data *) data;
 
 208         dev->error_state = result_data->state;
 
 211                 !dev->driver->err_handler ||
 
 212                 !dev->driver->err_handler->error_detected) {
 
 213                 if (result_data->state == pci_channel_io_frozen &&
 
 214                         !(dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)) {
 
 216                          * In case of fatal recovery, if one of down-
 
 217                          * stream device has no driver. We might be
 
 218                          * unable to recover because a later insmod
 
 219                          * of a driver for this device is unaware of
 
 222                         printk(KERN_DEBUG "Device ID[%s] has %s\n",
 
 223                                         dev->dev.bus_id, (dev->driver) ?
 
 224                                         "no AER-aware driver" : "no driver");
 
 229         err_handler = dev->driver->err_handler;
 
 230         vote = err_handler->error_detected(dev, result_data->state);
 
 231         result_data->result = merge_result(result_data->result, vote);
 
 235 static void report_mmio_enabled(struct pci_dev *dev, void *data)
 
 237         pci_ers_result_t vote;
 
 238         struct pci_error_handlers *err_handler;
 
 239         struct aer_broadcast_data *result_data;
 
 240         result_data = (struct aer_broadcast_data *) data;
 
 243                 !dev->driver->err_handler ||
 
 244                 !dev->driver->err_handler->mmio_enabled)
 
 247         err_handler = dev->driver->err_handler;
 
 248         vote = err_handler->mmio_enabled(dev);
 
 249         result_data->result = merge_result(result_data->result, vote);
 
 253 static void report_slot_reset(struct pci_dev *dev, void *data)
 
 255         pci_ers_result_t vote;
 
 256         struct pci_error_handlers *err_handler;
 
 257         struct aer_broadcast_data *result_data;
 
 258         result_data = (struct aer_broadcast_data *) data;
 
 261                 !dev->driver->err_handler ||
 
 262                 !dev->driver->err_handler->slot_reset)
 
 265         err_handler = dev->driver->err_handler;
 
 266         vote = err_handler->slot_reset(dev);
 
 267         result_data->result = merge_result(result_data->result, vote);
 
 271 static void report_resume(struct pci_dev *dev, void *data)
 
 273         struct pci_error_handlers *err_handler;
 
 275         dev->error_state = pci_channel_io_normal;
 
 278                 !dev->driver->err_handler ||
 
 279                 !dev->driver->err_handler->slot_reset)
 
 282         err_handler = dev->driver->err_handler;
 
 283         err_handler->resume(dev);
 
 288  * broadcast_error_message - handle message broadcast to downstream drivers
 
 289  * @dev: pointer to from where in a hierarchy message is broadcasted down
 
 290  * @state: error state
 
 291  * @error_mesg: message to print
 
 292  * @cb: callback to be broadcasted
 
 294  * Invoked during error recovery process. Once being invoked, the content
 
 295  * of error severity will be broadcasted to all downstream drivers in a
 
 296  * hierarchy in question.
 
 298 static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
 
 299         enum pci_channel_state state,
 
 301         void (*cb)(struct pci_dev *, void *))
 
 303         struct aer_broadcast_data result_data;
 
 305         printk(KERN_DEBUG "Broadcast %s message\n", error_mesg);
 
 306         result_data.state = state;
 
 307         if (cb == report_error_detected)
 
 308                 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
 
 310                 result_data.result = PCI_ERS_RESULT_RECOVERED;
 
 312         if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE) {
 
 314                  * If the error is reported by a bridge, we think this error
 
 315                  * is related to the downstream link of the bridge, so we
 
 316                  * do error recovery on all subordinates of the bridge instead
 
 317                  * of the bridge and clear the error status of the bridge.
 
 319                 if (cb == report_error_detected)
 
 320                         dev->error_state = state;
 
 321                 pci_walk_bus(dev->subordinate, cb, &result_data);
 
 322                 if (cb == report_resume) {
 
 323                         pci_cleanup_aer_uncorrect_error_status(dev);
 
 324                         dev->error_state = pci_channel_io_normal;
 
 329                  * If the error is reported by an end point, we think this
 
 330                  * error is related to the upstream link of the end point.
 
 332                 pci_walk_bus(dev->bus, cb, &result_data);
 
 335         return result_data.result;
 
 338 struct find_aer_service_data {
 
 339         struct pcie_port_service_driver *aer_driver;
 
 343 static int find_aer_service_iter(struct device *device, void *data)
 
 345         struct device_driver *driver;
 
 346         struct pcie_port_service_driver *service_driver;
 
 347         struct pcie_device *pcie_dev;
 
 348         struct find_aer_service_data *result;
 
 350         result = (struct find_aer_service_data *) data;
 
 352         if (device->bus == &pcie_port_bus_type) {
 
 353                 pcie_dev = to_pcie_device(device);
 
 354                 if (pcie_dev->id.port_type == PCIE_SW_DOWNSTREAM_PORT)
 
 355                         result->is_downstream = 1;
 
 357                 driver = device->driver;
 
 359                         service_driver = to_service_driver(driver);
 
 360                         if (service_driver->id_table->service_type ==
 
 361                                         PCIE_PORT_SERVICE_AER) {
 
 362                                 result->aer_driver = service_driver;
 
 371 static void find_aer_service(struct pci_dev *dev,
 
 372                 struct find_aer_service_data *data)
 
 375         retval = device_for_each_child(&dev->dev, data, find_aer_service_iter);
 
 378 static pci_ers_result_t reset_link(struct pcie_device *aerdev,
 
 381         struct pci_dev *udev;
 
 382         pci_ers_result_t status;
 
 383         struct find_aer_service_data data;
 
 385         if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE)
 
 388                 udev= dev->bus->self;
 
 390         data.is_downstream = 0;
 
 391         data.aer_driver = NULL;
 
 392         find_aer_service(udev, &data);
 
 395          * Use the aer driver of the error agent firstly.
 
 396          * If it hasn't the aer driver, use the root port's
 
 398         if (!data.aer_driver || !data.aer_driver->reset_link) {
 
 399                 if (data.is_downstream &&
 
 400                         aerdev->device.driver &&
 
 401                         to_service_driver(aerdev->device.driver)->reset_link) {
 
 403                                 to_service_driver(aerdev->device.driver);
 
 405                         printk(KERN_DEBUG "No link-reset support to Device ID"
 
 408                         return PCI_ERS_RESULT_DISCONNECT;
 
 412         status = data.aer_driver->reset_link(udev);
 
 413         if (status != PCI_ERS_RESULT_RECOVERED) {
 
 414                 printk(KERN_DEBUG "Link reset at upstream Device ID"
 
 417                 return PCI_ERS_RESULT_DISCONNECT;
 
 424  * do_recovery - handle nonfatal/fatal error recovery process
 
 425  * @aerdev: pointer to a pcie_device data structure of root port
 
 426  * @dev: pointer to a pci_dev data structure of agent detecting an error
 
 427  * @severity: error severity type
 
 429  * Invoked when an error is nonfatal/fatal. Once being invoked, broadcast
 
 430  * error detected message to all downstream drivers within a hierarchy in
 
 431  * question and return the returned code.
 
 433 static pci_ers_result_t do_recovery(struct pcie_device *aerdev,
 
 437         pci_ers_result_t status, result = PCI_ERS_RESULT_RECOVERED;
 
 438         enum pci_channel_state state;
 
 440         if (severity == AER_FATAL)
 
 441                 state = pci_channel_io_frozen;
 
 443                 state = pci_channel_io_normal;
 
 445         status = broadcast_error_message(dev,
 
 448                         report_error_detected);
 
 450         if (severity == AER_FATAL) {
 
 451                 result = reset_link(aerdev, dev);
 
 452                 if (result != PCI_ERS_RESULT_RECOVERED) {
 
 453                         /* TODO: Should panic here? */
 
 458         if (status == PCI_ERS_RESULT_CAN_RECOVER)
 
 459                 status = broadcast_error_message(dev,
 
 462                                 report_mmio_enabled);
 
 464         if (status == PCI_ERS_RESULT_NEED_RESET) {
 
 466                  * TODO: Should call platform-specific
 
 467                  * functions to reset slot before calling
 
 468                  * drivers' slot_reset callbacks?
 
 470                 status = broadcast_error_message(dev,
 
 476         if (status == PCI_ERS_RESULT_RECOVERED)
 
 477                 broadcast_error_message(dev,
 
 486  * handle_error_source - handle logging error into an event log
 
 487  * @aerdev: pointer to pcie_device data structure of the root port
 
 488  * @dev: pointer to pci_dev data structure of error source device
 
 489  * @info: comprehensive error information
 
 491  * Invoked when an error being detected by Root Port.
 
 493 static void handle_error_source(struct pcie_device * aerdev,
 
 495         struct aer_err_info info)
 
 497         pci_ers_result_t status = 0;
 
 500         if (info.severity == AER_CORRECTABLE) {
 
 502                  * Correctable error does not need software intevention.
 
 503                  * No need to go through error recovery process.
 
 505                 pos = pci_find_aer_capability(dev);
 
 507                         pci_write_config_dword(dev, pos + PCI_ERR_COR_STATUS,
 
 510                 status = do_recovery(aerdev, dev, info.severity);
 
 511                 if (status == PCI_ERS_RESULT_RECOVERED) {
 
 512                         printk(KERN_DEBUG "AER driver successfully recovered\n");
 
 514                         /* TODO: Should kernel panic here? */
 
 515                         printk(KERN_DEBUG "AER driver didn't recover\n");
 
 521  * aer_enable_rootport - enable Root Port's interrupts when receiving messages
 
 522  * @rpc: pointer to a Root Port data structure
 
 524  * Invoked when PCIE bus loads AER service driver.
 
 526 void aer_enable_rootport(struct aer_rpc *rpc)
 
 528         struct pci_dev *pdev = rpc->rpd->port;
 
 533         pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
 
 534         /* Clear PCIE Capability's Device Status */
 
 535         pci_read_config_word(pdev, pos+PCI_EXP_DEVSTA, ®16);
 
 536         pci_write_config_word(pdev, pos+PCI_EXP_DEVSTA, reg16);
 
 538         /* Disable system error generation in response to error messages */
 
 539         pci_read_config_word(pdev, pos + PCI_EXP_RTCTL, ®16);
 
 540         reg16 &= ~(SYSTEM_ERROR_INTR_ON_MESG_MASK);
 
 541         pci_write_config_word(pdev, pos + PCI_EXP_RTCTL, reg16);
 
 543         aer_pos = pci_find_aer_capability(pdev);
 
 544         /* Clear error status */
 
 545         pci_read_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, ®32);
 
 546         pci_write_config_dword(pdev, aer_pos + PCI_ERR_ROOT_STATUS, reg32);
 
 547         pci_read_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, ®32);
 
 548         pci_write_config_dword(pdev, aer_pos + PCI_ERR_COR_STATUS, reg32);
 
 549         pci_read_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, ®32);
 
 550         pci_write_config_dword(pdev, aer_pos + PCI_ERR_UNCOR_STATUS, reg32);
 
 552         /* Enable Root Port device reporting error itself */
 
 553         pci_read_config_word(pdev, pos+PCI_EXP_DEVCTL, ®16);
 
 555                 PCI_EXP_DEVCTL_CERE |
 
 556                 PCI_EXP_DEVCTL_NFERE |
 
 557                 PCI_EXP_DEVCTL_FERE |
 
 559         pci_write_config_word(pdev, pos+PCI_EXP_DEVCTL,
 
 562         /* Enable Root Port's interrupt in response to error messages */
 
 563         pci_write_config_dword(pdev,
 
 564                 aer_pos + PCI_ERR_ROOT_COMMAND,
 
 565                 ROOT_PORT_INTR_ON_MESG_MASK);
 
 569  * disable_root_aer - disable Root Port's interrupts when receiving messages
 
 570  * @rpc: pointer to a Root Port data structure
 
 572  * Invoked when PCIE bus unloads AER service driver.
 
 574 static void disable_root_aer(struct aer_rpc *rpc)
 
 576         struct pci_dev *pdev = rpc->rpd->port;
 
 580         pos = pci_find_aer_capability(pdev);
 
 581         /* Disable Root's interrupt in response to error messages */
 
 582         pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_COMMAND, 0);
 
 584         /* Clear Root's error status reg */
 
 585         pci_read_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, ®32);
 
 586         pci_write_config_dword(pdev, pos + PCI_ERR_ROOT_STATUS, reg32);
 
 590  * get_e_source - retrieve an error source
 
 591  * @rpc: pointer to the root port which holds an error
 
 593  * Invoked by DPC handler to consume an error.
 
 595 static struct aer_err_source* get_e_source(struct aer_rpc *rpc)
 
 597         struct aer_err_source *e_source;
 
 600         /* Lock access to Root error producer/consumer index */
 
 601         spin_lock_irqsave(&rpc->e_lock, flags);
 
 602         if (rpc->prod_idx == rpc->cons_idx) {
 
 603                 spin_unlock_irqrestore(&rpc->e_lock, flags);
 
 606         e_source = &rpc->e_sources[rpc->cons_idx];
 
 608         if (rpc->cons_idx == AER_ERROR_SOURCES_MAX)
 
 610         spin_unlock_irqrestore(&rpc->e_lock, flags);
 
 615 static int get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
 
 619         pos = pci_find_aer_capability(dev);
 
 621         /* The device might not support AER */
 
 625         if (info->severity == AER_CORRECTABLE) {
 
 626                 pci_read_config_dword(dev, pos + PCI_ERR_COR_STATUS,
 
 628                 if (!(info->status & ERR_CORRECTABLE_ERROR_MASK))
 
 629                         return AER_UNSUCCESS;
 
 630         } else if (dev->hdr_type & PCI_HEADER_TYPE_BRIDGE ||
 
 631                 info->severity == AER_NONFATAL) {
 
 633                 /* Link is still healthy for IO reads */
 
 634                 pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS,
 
 636                 if (!(info->status & ERR_UNCORRECTABLE_ERROR_MASK))
 
 637                         return AER_UNSUCCESS;
 
 639                 if (info->status & AER_LOG_TLP_MASKS) {
 
 640                         info->flags |= AER_TLP_HEADER_VALID_FLAG;
 
 641                         pci_read_config_dword(dev,
 
 642                                 pos + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
 
 643                         pci_read_config_dword(dev,
 
 644                                 pos + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
 
 645                         pci_read_config_dword(dev,
 
 646                                 pos + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
 
 647                         pci_read_config_dword(dev,
 
 648                                 pos + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
 
 656  * aer_isr_one_error - consume an error detected by root port
 
 657  * @p_device: pointer to error root port service device
 
 658  * @e_src: pointer to an error source
 
 660 static void aer_isr_one_error(struct pcie_device *p_device,
 
 661                 struct aer_err_source *e_src)
 
 663         struct device *s_device;
 
 664         struct aer_err_info e_info = {0, 0, 0,};
 
 669          * There is a possibility that both correctable error and
 
 670          * uncorrectable error being logged. Report correctable error first.
 
 672         for (i = 1; i & ROOT_ERR_STATUS_MASKS ; i <<= 2) {
 
 675                 if (!(e_src->status & i))
 
 678                 /* Init comprehensive error information */
 
 679                 if (i & PCI_ERR_ROOT_COR_RCV) {
 
 680                         id = ERR_COR_ID(e_src->id);
 
 681                         e_info.severity = AER_CORRECTABLE;
 
 683                         id = ERR_UNCOR_ID(e_src->id);
 
 684                         e_info.severity = ((e_src->status >> 6) & 1);
 
 687                         (PCI_ERR_ROOT_MULTI_COR_RCV |
 
 688                          PCI_ERR_ROOT_MULTI_UNCOR_RCV))
 
 689                         e_info.flags |= AER_MULTI_ERROR_VALID_FLAG;
 
 690                 if (!(s_device = find_source_device(p_device->port, id))) {
 
 691                         printk(KERN_DEBUG "%s->can't find device of ID%04x\n",
 
 695                 if (get_device_error_info(to_pci_dev(s_device), &e_info) ==
 
 697                         aer_print_error(to_pci_dev(s_device), &e_info);
 
 698                         handle_error_source(p_device,
 
 699                                 to_pci_dev(s_device),
 
 706  * aer_isr - consume errors detected by root port
 
 707  * @work: definition of this work item
 
 709  * Invoked, as DPC, when root port records new detected error
 
 711 void aer_isr(struct work_struct *work)
 
 713         struct aer_rpc *rpc = container_of(work, struct aer_rpc, dpc_handler);
 
 714         struct pcie_device *p_device = rpc->rpd;
 
 715         struct aer_err_source *e_src;
 
 717         mutex_lock(&rpc->rpc_mutex);
 
 718         e_src = get_e_source(rpc);
 
 720                 aer_isr_one_error(p_device, e_src);
 
 721                 e_src = get_e_source(rpc);
 
 723         mutex_unlock(&rpc->rpc_mutex);
 
 725         wake_up(&rpc->wait_release);
 
 729  * aer_delete_rootport - disable root port aer and delete service data
 
 730  * @rpc: pointer to a root port device being deleted
 
 732  * Invoked when AER service unloaded on a specific Root Port
 
 734 void aer_delete_rootport(struct aer_rpc *rpc)
 
 736         /* Disable root port AER itself */
 
 737         disable_root_aer(rpc);
 
 743  * aer_init - provide AER initialization
 
 744  * @dev: pointer to AER pcie device
 
 746  * Invoked when AER service driver is loaded.
 
 748 int aer_init(struct pcie_device *dev)
 
 750         if (aer_osc_setup(dev) && !forceload)
 
 756 EXPORT_SYMBOL_GPL(pci_find_aer_capability);
 
 757 EXPORT_SYMBOL_GPL(pci_enable_pcie_error_reporting);
 
 758 EXPORT_SYMBOL_GPL(pci_disable_pcie_error_reporting);
 
 759 EXPORT_SYMBOL_GPL(pci_cleanup_aer_uncorrect_error_status);
 
 760 EXPORT_SYMBOL_GPL(pci_cleanup_aer_correct_error_status);