adf_aer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. void adf_reset_sbr(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 (!parent)
  78. parent = pdev;
  79. if (!pci_wait_for_pending_transaction(pdev))
  80. dev_info(&GET_DEV(accel_dev),
  81. "Transaction still in progress. Proceeding\n");
  82. dev_info(&GET_DEV(accel_dev), "Secondary bus reset\n");
  83. pci_read_config_word(parent, PCI_BRIDGE_CONTROL, &bridge_ctl);
  84. bridge_ctl |= PCI_BRIDGE_CTL_BUS_RESET;
  85. pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
  86. msleep(100);
  87. bridge_ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  88. pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
  89. msleep(100);
  90. }
  91. EXPORT_SYMBOL_GPL(adf_reset_sbr);
  92. void adf_reset_flr(struct adf_accel_dev *accel_dev)
  93. {
  94. pcie_flr(accel_to_pci_dev(accel_dev));
  95. }
  96. EXPORT_SYMBOL_GPL(adf_reset_flr);
  97. void adf_dev_restore(struct adf_accel_dev *accel_dev)
  98. {
  99. struct adf_hw_device_data *hw_device = accel_dev->hw_device;
  100. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  101. if (hw_device->reset_device) {
  102. dev_info(&GET_DEV(accel_dev), "Resetting device qat_dev%d\n",
  103. accel_dev->accel_id);
  104. hw_device->reset_device(accel_dev);
  105. pci_restore_state(pdev);
  106. pci_save_state(pdev);
  107. }
  108. }
  109. static void adf_device_reset_worker(struct work_struct *work)
  110. {
  111. struct adf_reset_dev_data *reset_data =
  112. container_of(work, struct adf_reset_dev_data, reset_work);
  113. struct adf_accel_dev *accel_dev = reset_data->accel_dev;
  114. adf_dev_restarting_notify(accel_dev);
  115. adf_dev_stop(accel_dev);
  116. adf_dev_shutdown(accel_dev);
  117. if (adf_dev_init(accel_dev) || adf_dev_start(accel_dev)) {
  118. /* The device hanged and we can't restart it so stop here */
  119. dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
  120. kfree(reset_data);
  121. WARN(1, "QAT: device restart failed. Device is unusable\n");
  122. return;
  123. }
  124. adf_dev_restarted_notify(accel_dev);
  125. clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
  126. /* The dev is back alive. Notify the caller if in sync mode */
  127. if (reset_data->mode == ADF_DEV_RESET_SYNC)
  128. complete(&reset_data->compl);
  129. else
  130. kfree(reset_data);
  131. }
  132. static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
  133. enum adf_dev_reset_mode mode)
  134. {
  135. struct adf_reset_dev_data *reset_data;
  136. if (!adf_dev_started(accel_dev) ||
  137. test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
  138. return 0;
  139. set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
  140. reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
  141. if (!reset_data)
  142. return -ENOMEM;
  143. reset_data->accel_dev = accel_dev;
  144. init_completion(&reset_data->compl);
  145. reset_data->mode = mode;
  146. INIT_WORK(&reset_data->reset_work, adf_device_reset_worker);
  147. queue_work(device_reset_wq, &reset_data->reset_work);
  148. /* If in sync mode wait for the result */
  149. if (mode == ADF_DEV_RESET_SYNC) {
  150. int ret = 0;
  151. /* Maximum device reset time is 10 seconds */
  152. unsigned long wait_jiffies = msecs_to_jiffies(10000);
  153. unsigned long timeout = wait_for_completion_timeout(
  154. &reset_data->compl, wait_jiffies);
  155. if (!timeout) {
  156. dev_err(&GET_DEV(accel_dev),
  157. "Reset device timeout expired\n");
  158. ret = -EFAULT;
  159. }
  160. kfree(reset_data);
  161. return ret;
  162. }
  163. return 0;
  164. }
  165. static pci_ers_result_t adf_slot_reset(struct pci_dev *pdev)
  166. {
  167. struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
  168. if (!accel_dev) {
  169. pr_err("QAT: Can't find acceleration device\n");
  170. return PCI_ERS_RESULT_DISCONNECT;
  171. }
  172. pci_cleanup_aer_uncorrect_error_status(pdev);
  173. if (adf_dev_aer_schedule_reset(accel_dev, ADF_DEV_RESET_SYNC))
  174. return PCI_ERS_RESULT_DISCONNECT;
  175. return PCI_ERS_RESULT_RECOVERED;
  176. }
  177. static void adf_resume(struct pci_dev *pdev)
  178. {
  179. dev_info(&pdev->dev, "Acceleration driver reset completed\n");
  180. dev_info(&pdev->dev, "Device is up and running\n");
  181. }
  182. static const struct pci_error_handlers adf_err_handler = {
  183. .error_detected = adf_error_detected,
  184. .slot_reset = adf_slot_reset,
  185. .resume = adf_resume,
  186. };
  187. /**
  188. * adf_enable_aer() - Enable Advance Error Reporting for acceleration device
  189. * @accel_dev: Pointer to acceleration device.
  190. * @adf: PCI device driver owning the given acceleration device.
  191. *
  192. * Function enables PCI Advance Error Reporting for the
  193. * QAT acceleration device accel_dev.
  194. * To be used by QAT device specific drivers.
  195. *
  196. * Return: 0 on success, error code otherwise.
  197. */
  198. int adf_enable_aer(struct adf_accel_dev *accel_dev, struct pci_driver *adf)
  199. {
  200. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  201. adf->err_handler = &adf_err_handler;
  202. pci_enable_pcie_error_reporting(pdev);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL_GPL(adf_enable_aer);
  206. /**
  207. * adf_disable_aer() - Enable Advance Error Reporting for acceleration device
  208. * @accel_dev: Pointer to acceleration device.
  209. *
  210. * Function disables PCI Advance Error Reporting for the
  211. * QAT acceleration device accel_dev.
  212. * To be used by QAT device specific drivers.
  213. *
  214. * Return: void
  215. */
  216. void adf_disable_aer(struct adf_accel_dev *accel_dev)
  217. {
  218. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  219. pci_disable_pcie_error_reporting(pdev);
  220. }
  221. EXPORT_SYMBOL_GPL(adf_disable_aer);
  222. int adf_init_aer(void)
  223. {
  224. device_reset_wq = alloc_workqueue("qat_device_reset_wq",
  225. WQ_MEM_RECLAIM, 0);
  226. return !device_reset_wq ? -EFAULT : 0;
  227. }
  228. void adf_exit_aer(void)
  229. {
  230. if (device_reset_wq)
  231. destroy_workqueue(device_reset_wq);
  232. device_reset_wq = NULL;
  233. }