edr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Error Disconnect Recover support
  4. * Author: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
  5. *
  6. * Copyright (C) 2020 Intel Corp.
  7. */
  8. #define dev_fmt(fmt) "EDR: " fmt
  9. #include <linux/pci.h>
  10. #include <linux/pci-acpi.h>
  11. #include "portdrv.h"
  12. #include "../pci.h"
  13. #define EDR_PORT_DPC_ENABLE_DSM 0x0C
  14. #define EDR_PORT_LOCATE_DSM 0x0D
  15. #define EDR_OST_SUCCESS 0x80
  16. #define EDR_OST_FAILED 0x81
  17. /*
  18. * _DSM wrapper function to enable/disable DPC
  19. * @pdev : PCI device structure
  20. *
  21. * returns 0 on success or errno on failure.
  22. */
  23. static int acpi_enable_dpc(struct pci_dev *pdev)
  24. {
  25. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  26. union acpi_object *obj, argv4, req;
  27. int status = 0;
  28. /*
  29. * Behavior when calling unsupported _DSM functions is undefined,
  30. * so check whether EDR_PORT_DPC_ENABLE_DSM is supported.
  31. */
  32. if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
  33. 1ULL << EDR_PORT_DPC_ENABLE_DSM))
  34. return 0;
  35. req.type = ACPI_TYPE_INTEGER;
  36. req.integer.value = 1;
  37. argv4.type = ACPI_TYPE_PACKAGE;
  38. argv4.package.count = 1;
  39. argv4.package.elements = &req;
  40. /*
  41. * Per Downstream Port Containment Related Enhancements ECN to PCI
  42. * Firmware Specification r3.2, sec 4.6.12, EDR_PORT_DPC_ENABLE_DSM is
  43. * optional. Return success if it's not implemented.
  44. */
  45. obj = acpi_evaluate_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
  46. EDR_PORT_DPC_ENABLE_DSM, &argv4);
  47. if (!obj)
  48. return 0;
  49. if (obj->type != ACPI_TYPE_INTEGER) {
  50. pci_err(pdev, FW_BUG "Enable DPC _DSM returned non integer\n");
  51. status = -EIO;
  52. }
  53. if (obj->integer.value != 1) {
  54. pci_err(pdev, "Enable DPC _DSM failed to enable DPC\n");
  55. status = -EIO;
  56. }
  57. ACPI_FREE(obj);
  58. return status;
  59. }
  60. /*
  61. * _DSM wrapper function to locate DPC port
  62. * @pdev : Device which received EDR event
  63. *
  64. * Returns pci_dev or NULL. Caller is responsible for dropping a reference
  65. * on the returned pci_dev with pci_dev_put().
  66. */
  67. static struct pci_dev *acpi_dpc_port_get(struct pci_dev *pdev)
  68. {
  69. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  70. union acpi_object *obj;
  71. u16 port;
  72. /*
  73. * Behavior when calling unsupported _DSM functions is undefined,
  74. * so check whether EDR_PORT_DPC_ENABLE_DSM is supported.
  75. */
  76. if (!acpi_check_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
  77. 1ULL << EDR_PORT_LOCATE_DSM))
  78. return pci_dev_get(pdev);
  79. obj = acpi_evaluate_dsm(adev->handle, &pci_acpi_dsm_guid, 5,
  80. EDR_PORT_LOCATE_DSM, NULL);
  81. if (!obj)
  82. return pci_dev_get(pdev);
  83. if (obj->type != ACPI_TYPE_INTEGER) {
  84. ACPI_FREE(obj);
  85. pci_err(pdev, FW_BUG "Locate Port _DSM returned non integer\n");
  86. return NULL;
  87. }
  88. /*
  89. * Firmware returns DPC port BDF details in following format:
  90. * 15:8 = bus
  91. * 7:3 = device
  92. * 2:0 = function
  93. */
  94. port = obj->integer.value;
  95. ACPI_FREE(obj);
  96. return pci_get_domain_bus_and_slot(pci_domain_nr(pdev->bus),
  97. PCI_BUS_NUM(port), port & 0xff);
  98. }
  99. /*
  100. * _OST wrapper function to let firmware know the status of EDR event
  101. * @pdev : Device used to send _OST
  102. * @edev : Device which experienced EDR event
  103. * @status : Status of EDR event
  104. */
  105. static int acpi_send_edr_status(struct pci_dev *pdev, struct pci_dev *edev,
  106. u16 status)
  107. {
  108. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  109. u32 ost_status;
  110. pci_dbg(pdev, "Status for %s: %#x\n", pci_name(edev), status);
  111. ost_status = PCI_DEVID(edev->bus->number, edev->devfn) << 16;
  112. ost_status |= status;
  113. status = acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_DISCONNECT_RECOVER,
  114. ost_status, NULL);
  115. if (ACPI_FAILURE(status))
  116. return -EINVAL;
  117. return 0;
  118. }
  119. static void edr_handle_event(acpi_handle handle, u32 event, void *data)
  120. {
  121. struct pci_dev *pdev = data, *edev;
  122. pci_ers_result_t estate = PCI_ERS_RESULT_DISCONNECT;
  123. u16 status;
  124. if (event != ACPI_NOTIFY_DISCONNECT_RECOVER)
  125. return;
  126. pci_info(pdev, "EDR event received\n");
  127. /* Locate the port which issued EDR event */
  128. edev = acpi_dpc_port_get(pdev);
  129. if (!edev) {
  130. pci_err(pdev, "Firmware failed to locate DPC port\n");
  131. return;
  132. }
  133. pci_dbg(pdev, "Reported EDR dev: %s\n", pci_name(edev));
  134. /* If port does not support DPC, just send the OST */
  135. if (!edev->dpc_cap) {
  136. pci_err(edev, FW_BUG "This device doesn't support DPC\n");
  137. goto send_ost;
  138. }
  139. /* Check if there is a valid DPC trigger */
  140. pci_read_config_word(edev, edev->dpc_cap + PCI_EXP_DPC_STATUS, &status);
  141. if (!(status & PCI_EXP_DPC_STATUS_TRIGGER)) {
  142. pci_err(edev, "Invalid DPC trigger %#010x\n", status);
  143. goto send_ost;
  144. }
  145. dpc_process_error(edev);
  146. pci_aer_raw_clear_status(edev);
  147. /*
  148. * Irrespective of whether the DPC event is triggered by ERR_FATAL
  149. * or ERR_NONFATAL, since the link is already down, use the FATAL
  150. * error recovery path for both cases.
  151. */
  152. estate = pcie_do_recovery(edev, pci_channel_io_frozen, dpc_reset_link);
  153. send_ost:
  154. /*
  155. * If recovery is successful, send _OST(0xF, BDF << 16 | 0x80)
  156. * to firmware. If not successful, send _OST(0xF, BDF << 16 | 0x81).
  157. */
  158. if (estate == PCI_ERS_RESULT_RECOVERED) {
  159. pci_dbg(edev, "DPC port successfully recovered\n");
  160. acpi_send_edr_status(pdev, edev, EDR_OST_SUCCESS);
  161. } else {
  162. pci_dbg(edev, "DPC port recovery failed\n");
  163. acpi_send_edr_status(pdev, edev, EDR_OST_FAILED);
  164. }
  165. pci_dev_put(edev);
  166. }
  167. void pci_acpi_add_edr_notifier(struct pci_dev *pdev)
  168. {
  169. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  170. acpi_status status;
  171. if (!adev) {
  172. pci_dbg(pdev, "No valid ACPI node, skipping EDR init\n");
  173. return;
  174. }
  175. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  176. edr_handle_event, pdev);
  177. if (ACPI_FAILURE(status)) {
  178. pci_err(pdev, "Failed to install notify handler\n");
  179. return;
  180. }
  181. if (acpi_enable_dpc(pdev))
  182. acpi_remove_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  183. edr_handle_event);
  184. else
  185. pci_dbg(pdev, "Notify handler installed\n");
  186. }
  187. void pci_acpi_remove_edr_notifier(struct pci_dev *pdev)
  188. {
  189. struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
  190. if (!adev)
  191. return;
  192. acpi_remove_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  193. edr_handle_event);
  194. pci_dbg(pdev, "Notify handler removed\n");
  195. }