Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PCI Error Disconnect Recover support
0004  * Author: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
0005  *
0006  * Copyright (C) 2020 Intel Corp.
0007  */
0008 
0009 #define dev_fmt(fmt) "EDR: " fmt
0010 
0011 #include <linux/pci.h>
0012 #include <linux/pci-acpi.h>
0013 
0014 #include "portdrv.h"
0015 #include "../pci.h"
0016 
0017 #define EDR_PORT_DPC_ENABLE_DSM     0x0C
0018 #define EDR_PORT_LOCATE_DSM     0x0D
0019 #define EDR_OST_SUCCESS         0x80
0020 #define EDR_OST_FAILED          0x81
0021 
0022 /*
0023  * _DSM wrapper function to enable/disable DPC
0024  * @pdev   : PCI device structure
0025  *
0026  * returns 0 on success or errno on failure.
0027  */
0028 static int acpi_enable_dpc(struct pci_dev *pdev)
0029 {
0030     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0031     union acpi_object *obj, argv4, req;
0032     int status = 0;
0033 
0034     /*
0035      * Behavior when calling unsupported _DSM functions is undefined,
0036      * so check whether EDR_PORT_DPC_ENABLE_DSM is supported.
0037      */
0038     if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
0039                 1ULL << EDR_PORT_DPC_ENABLE_DSM))
0040         return 0;
0041 
0042     req.type = ACPI_TYPE_INTEGER;
0043     req.integer.value = 1;
0044 
0045     argv4.type = ACPI_TYPE_PACKAGE;
0046     argv4.package.count = 1;
0047     argv4.package.elements = &req;
0048 
0049     /*
0050      * Per Downstream Port Containment Related Enhancements ECN to PCI
0051      * Firmware Specification r3.2, sec 4.6.12, EDR_PORT_DPC_ENABLE_DSM is
0052      * optional.  Return success if it's not implemented.
0053      */
0054     obj = acpi_evaluate_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
0055                 EDR_PORT_DPC_ENABLE_DSM, &argv4);
0056     if (!obj)
0057         return 0;
0058 
0059     if (obj->type != ACPI_TYPE_INTEGER) {
0060         pci_err(pdev, FW_BUG "Enable DPC _DSM returned non integer\n");
0061         status = -EIO;
0062     }
0063 
0064     if (obj->integer.value != 1) {
0065         pci_err(pdev, "Enable DPC _DSM failed to enable DPC\n");
0066         status = -EIO;
0067     }
0068 
0069     ACPI_FREE(obj);
0070 
0071     return status;
0072 }
0073 
0074 /*
0075  * _DSM wrapper function to locate DPC port
0076  * @pdev   : Device which received EDR event
0077  *
0078  * Returns pci_dev or NULL.  Caller is responsible for dropping a reference
0079  * on the returned pci_dev with pci_dev_put().
0080  */
0081 static struct pci_dev *acpi_dpc_port_get(struct pci_dev *pdev)
0082 {
0083     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0084     union acpi_object *obj;
0085     u16 port;
0086 
0087     /*
0088      * Behavior when calling unsupported _DSM functions is undefined,
0089      * so check whether EDR_PORT_DPC_ENABLE_DSM is supported.
0090      */
0091     if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
0092                 1ULL << EDR_PORT_LOCATE_DSM))
0093         return pci_dev_get(pdev);
0094 
0095     obj = acpi_evaluate_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
0096                 EDR_PORT_LOCATE_DSM, NULL);
0097     if (!obj)
0098         return pci_dev_get(pdev);
0099 
0100     if (obj->type != ACPI_TYPE_INTEGER) {
0101         ACPI_FREE(obj);
0102         pci_err(pdev, FW_BUG "Locate Port _DSM returned non integer\n");
0103         return NULL;
0104     }
0105 
0106     /*
0107      * Firmware returns DPC port BDF details in following format:
0108      *  15:8 = bus
0109      *   7:3 = device
0110      *   2:0 = function
0111      */
0112     port = obj->integer.value;
0113 
0114     ACPI_FREE(obj);
0115 
0116     return pci_get_domain_bus_and_slot(pci_domain_nr(pdev->bus),
0117                        PCI_BUS_NUM(port), port & 0xff);
0118 }
0119 
0120 /*
0121  * _OST wrapper function to let firmware know the status of EDR event
0122  * @pdev   : Device used to send _OST
0123  * @edev   : Device which experienced EDR event
0124  * @status : Status of EDR event
0125  */
0126 static int acpi_send_edr_status(struct pci_dev *pdev, struct pci_dev *edev,
0127                 u16 status)
0128 {
0129     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0130     u32 ost_status;
0131 
0132     pci_dbg(pdev, "Status for %s: %#x\n", pci_name(edev), status);
0133 
0134     ost_status = PCI_DEVID(edev->bus->number, edev->devfn) << 16;
0135     ost_status |= status;
0136 
0137     status = acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_DISCONNECT_RECOVER,
0138                    ost_status, NULL);
0139     if (ACPI_FAILURE(status))
0140         return -EINVAL;
0141 
0142     return 0;
0143 }
0144 
0145 static void edr_handle_event(acpi_handle handle, u32 event, void *data)
0146 {
0147     struct pci_dev *pdev = data, *edev;
0148     pci_ers_result_t estate = PCI_ERS_RESULT_DISCONNECT;
0149     u16 status;
0150 
0151     if (event != ACPI_NOTIFY_DISCONNECT_RECOVER)
0152         return;
0153 
0154     pci_info(pdev, "EDR event received\n");
0155 
0156     /* Locate the port which issued EDR event */
0157     edev = acpi_dpc_port_get(pdev);
0158     if (!edev) {
0159         pci_err(pdev, "Firmware failed to locate DPC port\n");
0160         return;
0161     }
0162 
0163     pci_dbg(pdev, "Reported EDR dev: %s\n", pci_name(edev));
0164 
0165     /* If port does not support DPC, just send the OST */
0166     if (!edev->dpc_cap) {
0167         pci_err(edev, FW_BUG "This device doesn't support DPC\n");
0168         goto send_ost;
0169     }
0170 
0171     /* Check if there is a valid DPC trigger */
0172     pci_read_config_word(edev, edev->dpc_cap + PCI_EXP_DPC_STATUS, &status);
0173     if (!(status & PCI_EXP_DPC_STATUS_TRIGGER)) {
0174         pci_err(edev, "Invalid DPC trigger %#010x\n", status);
0175         goto send_ost;
0176     }
0177 
0178     dpc_process_error(edev);
0179     pci_aer_raw_clear_status(edev);
0180 
0181     /*
0182      * Irrespective of whether the DPC event is triggered by ERR_FATAL
0183      * or ERR_NONFATAL, since the link is already down, use the FATAL
0184      * error recovery path for both cases.
0185      */
0186     estate = pcie_do_recovery(edev, pci_channel_io_frozen, dpc_reset_link);
0187 
0188 send_ost:
0189 
0190     /*
0191      * If recovery is successful, send _OST(0xF, BDF << 16 | 0x80)
0192      * to firmware. If not successful, send _OST(0xF, BDF << 16 | 0x81).
0193      */
0194     if (estate == PCI_ERS_RESULT_RECOVERED) {
0195         pci_dbg(edev, "DPC port successfully recovered\n");
0196         acpi_send_edr_status(pdev, edev, EDR_OST_SUCCESS);
0197     } else {
0198         pci_dbg(edev, "DPC port recovery failed\n");
0199         acpi_send_edr_status(pdev, edev, EDR_OST_FAILED);
0200     }
0201 
0202     pci_dev_put(edev);
0203 }
0204 
0205 void pci_acpi_add_edr_notifier(struct pci_dev *pdev)
0206 {
0207     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0208     acpi_status status;
0209 
0210     if (!adev) {
0211         pci_dbg(pdev, "No valid ACPI node, skipping EDR init\n");
0212         return;
0213     }
0214 
0215     status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
0216                          edr_handle_event, pdev);
0217     if (ACPI_FAILURE(status)) {
0218         pci_err(pdev, "Failed to install notify handler\n");
0219         return;
0220     }
0221 
0222     if (acpi_enable_dpc(pdev))
0223         acpi_remove_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
0224                        edr_handle_event);
0225     else
0226         pci_dbg(pdev, "Notify handler installed\n");
0227 }
0228 
0229 void pci_acpi_remove_edr_notifier(struct pci_dev *pdev)
0230 {
0231     struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
0232 
0233     if (!adev)
0234         return;
0235 
0236     acpi_remove_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
0237                    edr_handle_event);
0238     pci_dbg(pdev, "Notify handler removed\n");
0239 }