adf_aer.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/pci.h>
  43. #include <linux/aer.h>
  44. #include <linux/completion.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/delay.h>
  47. #include "adf_accel_devices.h"
  48. #include "adf_common_drv.h"
  49. static struct workqueue_struct *device_reset_wq;
  50. static pci_ers_result_t adf_error_detected(struct pci_dev *pdev,
  51. pci_channel_state_t state)
  52. {
  53. struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
  54. dev_info(&pdev->dev, "Acceleration driver hardware error detected.\n");
  55. if (!accel_dev) {
  56. dev_err(&pdev->dev, "Can't find acceleration device\n");
  57. return PCI_ERS_RESULT_DISCONNECT;
  58. }
  59. if (state == pci_channel_io_perm_failure) {
  60. dev_err(&pdev->dev, "Can't recover from device error\n");
  61. return PCI_ERS_RESULT_DISCONNECT;
  62. }
  63. return PCI_ERS_RESULT_NEED_RESET;
  64. }
  65. /* reset dev data */
  66. struct adf_reset_dev_data {
  67. int mode;
  68. struct adf_accel_dev *accel_dev;
  69. struct completion compl;
  70. struct work_struct reset_work;
  71. };
  72. static void adf_dev_restore(struct adf_accel_dev *accel_dev)
  73. {
  74. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  75. struct pci_dev *parent = pdev->bus->self;
  76. uint16_t bridge_ctl = 0;
  77. if (accel_dev->is_vf)
  78. return;
  79. dev_info(&GET_DEV(accel_dev), "Resetting device qat_dev%d\n",
  80. accel_dev->accel_id);
  81. if (!parent)
  82. parent = pdev;
  83. if (!pci_wait_for_pending_transaction(pdev))
  84. dev_info(&GET_DEV(accel_dev),
  85. "Transaction still in progress. Proceeding\n");
  86. pci_read_config_word(parent, PCI_BRIDGE_CONTROL, &bridge_ctl);
  87. bridge_ctl |= PCI_BRIDGE_CTL_BUS_RESET;
  88. pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
  89. msleep(100);
  90. bridge_ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  91. pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
  92. msleep(100);
  93. pci_restore_state(pdev);
  94. pci_save_state(pdev);
  95. }
  96. static void adf_device_reset_worker(struct work_struct *work)
  97. {
  98. struct adf_reset_dev_data *reset_data =
  99. container_of(work, struct adf_reset_dev_data, reset_work);
  100. struct adf_accel_dev *accel_dev = reset_data->accel_dev;
  101. adf_dev_restarting_notify(accel_dev);
  102. adf_dev_stop(accel_dev);
  103. adf_dev_shutdown(accel_dev);
  104. adf_dev_restore(accel_dev);
  105. if (adf_dev_init(accel_dev) || adf_dev_start(accel_dev)) {
  106. /* The device hanged and we can't restart it so stop here */
  107. dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
  108. kfree(reset_data);
  109. WARN(1, "QAT: device restart failed. Device is unusable\n");
  110. return;
  111. }
  112. adf_dev_restarted_notify(accel_dev);
  113. clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
  114. /* The dev is back alive. Notify the caller if in sync mode */
  115. if (reset_data->mode == ADF_DEV_RESET_SYNC)
  116. complete(&reset_data->compl);
  117. else
  118. kfree(reset_data);
  119. }
  120. static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
  121. enum adf_dev_reset_mode mode)
  122. {
  123. struct adf_reset_dev_data *reset_data;
  124. if (!adf_dev_started(accel_dev) ||
  125. test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
  126. return 0;
  127. set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
  128. reset_data = kzalloc(sizeof(*reset_data), GFP_ATOMIC);
  129. if (!reset_data)
  130. return -ENOMEM;
  131. reset_data->accel_dev = accel_dev;
  132. init_completion(&reset_data->compl);
  133. reset_data->mode = mode;
  134. INIT_WORK(&reset_data->reset_work, adf_device_reset_worker);
  135. queue_work(device_reset_wq, &reset_data->reset_work);
  136. /* If in sync mode wait for the result */
  137. if (mode == ADF_DEV_RESET_SYNC) {
  138. int ret = 0;
  139. /* Maximum device reset time is 10 seconds */
  140. unsigned long wait_jiffies = msecs_to_jiffies(10000);
  141. unsigned long timeout = wait_for_completion_timeout(
  142. &reset_data->compl, wait_jiffies);
  143. if (!timeout) {
  144. dev_err(&GET_DEV(accel_dev),
  145. "Reset device timeout expired\n");
  146. ret = -EFAULT;
  147. }
  148. kfree(reset_data);
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. static pci_ers_result_t adf_slot_reset(struct pci_dev *pdev)
  154. {
  155. struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
  156. if (!accel_dev) {
  157. pr_err("QAT: Can't find acceleration device\n");
  158. return PCI_ERS_RESULT_DISCONNECT;
  159. }
  160. pci_cleanup_aer_uncorrect_error_status(pdev);
  161. if (adf_dev_aer_schedule_reset(accel_dev, ADF_DEV_RESET_SYNC))
  162. return PCI_ERS_RESULT_DISCONNECT;
  163. return PCI_ERS_RESULT_RECOVERED;
  164. }
  165. static void adf_resume(struct pci_dev *pdev)
  166. {
  167. dev_info(&pdev->dev, "Acceleration driver reset completed\n");
  168. dev_info(&pdev->dev, "Device is up and runnig\n");
  169. }
  170. static struct pci_error_handlers adf_err_handler = {
  171. .error_detected = adf_error_detected,
  172. .slot_reset = adf_slot_reset,
  173. .resume = adf_resume,
  174. };
  175. /**
  176. * adf_enable_aer() - Enable Advance Error Reporting for acceleration device
  177. * @accel_dev: Pointer to acceleration device.
  178. * @adf: PCI device driver owning the given acceleration device.
  179. *
  180. * Function enables PCI Advance Error Reporting for the
  181. * QAT acceleration device accel_dev.
  182. * To be used by QAT device specific drivers.
  183. *
  184. * Return: 0 on success, error code otherwise.
  185. */
  186. int adf_enable_aer(struct adf_accel_dev *accel_dev, struct pci_driver *adf)
  187. {
  188. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  189. adf->err_handler = &adf_err_handler;
  190. pci_enable_pcie_error_reporting(pdev);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(adf_enable_aer);
  194. /**
  195. * adf_disable_aer() - Enable Advance Error Reporting for acceleration device
  196. * @accel_dev: Pointer to acceleration device.
  197. *
  198. * Function disables PCI Advance Error Reporting for the
  199. * QAT acceleration device accel_dev.
  200. * To be used by QAT device specific drivers.
  201. *
  202. * Return: void
  203. */
  204. void adf_disable_aer(struct adf_accel_dev *accel_dev)
  205. {
  206. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  207. pci_disable_pcie_error_reporting(pdev);
  208. }
  209. EXPORT_SYMBOL_GPL(adf_disable_aer);
  210. int adf_init_aer(void)
  211. {
  212. device_reset_wq = create_workqueue("qat_device_reset_wq");
  213. return !device_reset_wq ? -EFAULT : 0;
  214. }
  215. void adf_exit_aer(void)
  216. {
  217. if (device_reset_wq)
  218. destroy_workqueue(device_reset_wq);
  219. device_reset_wq = NULL;
  220. }