0001
0002
0003
0004
0005
0006
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
0024
0025
0026
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
0036
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
0051
0052
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
0076
0077
0078
0079
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
0089
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
0108
0109
0110
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
0122
0123
0124
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
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
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
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
0183
0184
0185
0186 estate = pcie_do_recovery(edev, pci_channel_io_frozen, dpc_reset_link);
0187
0188 send_ost:
0189
0190
0191
0192
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 }