adf_vf_isr.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/init.h>
  43. #include <linux/types.h>
  44. #include <linux/pci.h>
  45. #include <linux/slab.h>
  46. #include <linux/errno.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/workqueue.h>
  49. #include "adf_accel_devices.h"
  50. #include "adf_common_drv.h"
  51. #include "adf_cfg.h"
  52. #include "adf_cfg_strings.h"
  53. #include "adf_cfg_common.h"
  54. #include "adf_transport_access_macros.h"
  55. #include "adf_transport_internal.h"
  56. #include "adf_pf2vf_msg.h"
  57. #define ADF_VINTSOU_OFFSET 0x204
  58. #define ADF_VINTSOU_BUN BIT(0)
  59. #define ADF_VINTSOU_PF2VF BIT(1)
  60. static struct workqueue_struct *adf_vf_stop_wq;
  61. struct adf_vf_stop_data {
  62. struct adf_accel_dev *accel_dev;
  63. struct work_struct work;
  64. };
  65. static int adf_enable_msi(struct adf_accel_dev *accel_dev)
  66. {
  67. struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
  68. int stat = pci_enable_msi(pci_dev_info->pci_dev);
  69. if (stat) {
  70. dev_err(&GET_DEV(accel_dev),
  71. "Failed to enable MSI interrupts\n");
  72. return stat;
  73. }
  74. accel_dev->vf.irq_name = kzalloc(ADF_MAX_MSIX_VECTOR_NAME, GFP_KERNEL);
  75. if (!accel_dev->vf.irq_name)
  76. return -ENOMEM;
  77. return stat;
  78. }
  79. static void adf_disable_msi(struct adf_accel_dev *accel_dev)
  80. {
  81. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  82. kfree(accel_dev->vf.irq_name);
  83. pci_disable_msi(pdev);
  84. }
  85. static void adf_dev_stop_async(struct work_struct *work)
  86. {
  87. struct adf_vf_stop_data *stop_data =
  88. container_of(work, struct adf_vf_stop_data, work);
  89. struct adf_accel_dev *accel_dev = stop_data->accel_dev;
  90. adf_dev_stop(accel_dev);
  91. adf_dev_shutdown(accel_dev);
  92. /* Re-enable PF2VF interrupts */
  93. adf_enable_pf2vf_interrupts(accel_dev);
  94. kfree(stop_data);
  95. }
  96. static void adf_pf2vf_bh_handler(void *data)
  97. {
  98. struct adf_accel_dev *accel_dev = data;
  99. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  100. struct adf_bar *pmisc =
  101. &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
  102. void __iomem *pmisc_bar_addr = pmisc->virt_addr;
  103. u32 msg;
  104. /* Read the message from PF */
  105. msg = ADF_CSR_RD(pmisc_bar_addr, hw_data->get_pf2vf_offset(0));
  106. if (!(msg & ADF_PF2VF_MSGORIGIN_SYSTEM))
  107. /* Ignore legacy non-system (non-kernel) PF2VF messages */
  108. goto err;
  109. switch ((msg & ADF_PF2VF_MSGTYPE_MASK) >> ADF_PF2VF_MSGTYPE_SHIFT) {
  110. case ADF_PF2VF_MSGTYPE_RESTARTING: {
  111. struct adf_vf_stop_data *stop_data;
  112. dev_dbg(&GET_DEV(accel_dev),
  113. "Restarting msg received from PF 0x%x\n", msg);
  114. clear_bit(ADF_STATUS_PF_RUNNING, &accel_dev->status);
  115. stop_data = kzalloc(sizeof(*stop_data), GFP_ATOMIC);
  116. if (!stop_data) {
  117. dev_err(&GET_DEV(accel_dev),
  118. "Couldn't schedule stop for vf_%d\n",
  119. accel_dev->accel_id);
  120. return;
  121. }
  122. stop_data->accel_dev = accel_dev;
  123. INIT_WORK(&stop_data->work, adf_dev_stop_async);
  124. queue_work(adf_vf_stop_wq, &stop_data->work);
  125. /* To ack, clear the PF2VFINT bit */
  126. msg &= ~ADF_PF2VF_INT;
  127. ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
  128. return;
  129. }
  130. case ADF_PF2VF_MSGTYPE_VERSION_RESP:
  131. dev_dbg(&GET_DEV(accel_dev),
  132. "Version resp received from PF 0x%x\n", msg);
  133. accel_dev->vf.pf_version =
  134. (msg & ADF_PF2VF_VERSION_RESP_VERS_MASK) >>
  135. ADF_PF2VF_VERSION_RESP_VERS_SHIFT;
  136. accel_dev->vf.compatible =
  137. (msg & ADF_PF2VF_VERSION_RESP_RESULT_MASK) >>
  138. ADF_PF2VF_VERSION_RESP_RESULT_SHIFT;
  139. complete(&accel_dev->vf.iov_msg_completion);
  140. break;
  141. default:
  142. goto err;
  143. }
  144. /* To ack, clear the PF2VFINT bit */
  145. msg &= ~ADF_PF2VF_INT;
  146. ADF_CSR_WR(pmisc_bar_addr, hw_data->get_pf2vf_offset(0), msg);
  147. /* Re-enable PF2VF interrupts */
  148. adf_enable_pf2vf_interrupts(accel_dev);
  149. return;
  150. err:
  151. dev_err(&GET_DEV(accel_dev),
  152. "Unknown message from PF (0x%x); leaving PF2VF ints disabled\n",
  153. msg);
  154. }
  155. static int adf_setup_pf2vf_bh(struct adf_accel_dev *accel_dev)
  156. {
  157. tasklet_init(&accel_dev->vf.pf2vf_bh_tasklet,
  158. (void *)adf_pf2vf_bh_handler, (unsigned long)accel_dev);
  159. mutex_init(&accel_dev->vf.vf2pf_lock);
  160. return 0;
  161. }
  162. static void adf_cleanup_pf2vf_bh(struct adf_accel_dev *accel_dev)
  163. {
  164. tasklet_disable(&accel_dev->vf.pf2vf_bh_tasklet);
  165. tasklet_kill(&accel_dev->vf.pf2vf_bh_tasklet);
  166. mutex_destroy(&accel_dev->vf.vf2pf_lock);
  167. }
  168. static irqreturn_t adf_isr(int irq, void *privdata)
  169. {
  170. struct adf_accel_dev *accel_dev = privdata;
  171. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  172. struct adf_bar *pmisc =
  173. &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
  174. void __iomem *pmisc_bar_addr = pmisc->virt_addr;
  175. u32 v_int;
  176. /* Read VF INT source CSR to determine the source of VF interrupt */
  177. v_int = ADF_CSR_RD(pmisc_bar_addr, ADF_VINTSOU_OFFSET);
  178. /* Check for PF2VF interrupt */
  179. if (v_int & ADF_VINTSOU_PF2VF) {
  180. /* Disable PF to VF interrupt */
  181. adf_disable_pf2vf_interrupts(accel_dev);
  182. /* Schedule tasklet to handle interrupt BH */
  183. tasklet_hi_schedule(&accel_dev->vf.pf2vf_bh_tasklet);
  184. return IRQ_HANDLED;
  185. }
  186. /* Check bundle interrupt */
  187. if (v_int & ADF_VINTSOU_BUN) {
  188. struct adf_etr_data *etr_data = accel_dev->transport;
  189. struct adf_etr_bank_data *bank = &etr_data->banks[0];
  190. /* Disable Flag and Coalesce Ring Interrupts */
  191. WRITE_CSR_INT_FLAG_AND_COL(bank->csr_addr, bank->bank_number,
  192. 0);
  193. tasklet_hi_schedule(&bank->resp_handler);
  194. return IRQ_HANDLED;
  195. }
  196. return IRQ_NONE;
  197. }
  198. static int adf_request_msi_irq(struct adf_accel_dev *accel_dev)
  199. {
  200. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  201. unsigned int cpu;
  202. int ret;
  203. snprintf(accel_dev->vf.irq_name, ADF_MAX_MSIX_VECTOR_NAME,
  204. "qat_%02x:%02d.%02d", pdev->bus->number, PCI_SLOT(pdev->devfn),
  205. PCI_FUNC(pdev->devfn));
  206. ret = request_irq(pdev->irq, adf_isr, 0, accel_dev->vf.irq_name,
  207. (void *)accel_dev);
  208. if (ret) {
  209. dev_err(&GET_DEV(accel_dev), "failed to enable irq for %s\n",
  210. accel_dev->vf.irq_name);
  211. return ret;
  212. }
  213. cpu = accel_dev->accel_id % num_online_cpus();
  214. irq_set_affinity_hint(pdev->irq, get_cpu_mask(cpu));
  215. return ret;
  216. }
  217. static int adf_setup_bh(struct adf_accel_dev *accel_dev)
  218. {
  219. struct adf_etr_data *priv_data = accel_dev->transport;
  220. tasklet_init(&priv_data->banks[0].resp_handler, adf_response_handler,
  221. (unsigned long)priv_data->banks);
  222. return 0;
  223. }
  224. static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
  225. {
  226. struct adf_etr_data *priv_data = accel_dev->transport;
  227. tasklet_disable(&priv_data->banks[0].resp_handler);
  228. tasklet_kill(&priv_data->banks[0].resp_handler);
  229. }
  230. /**
  231. * adf_vf_isr_resource_free() - Free IRQ for acceleration device
  232. * @accel_dev: Pointer to acceleration device.
  233. *
  234. * Function frees interrupts for acceleration device virtual function.
  235. */
  236. void adf_vf_isr_resource_free(struct adf_accel_dev *accel_dev)
  237. {
  238. struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
  239. irq_set_affinity_hint(pdev->irq, NULL);
  240. free_irq(pdev->irq, (void *)accel_dev);
  241. adf_cleanup_bh(accel_dev);
  242. adf_cleanup_pf2vf_bh(accel_dev);
  243. adf_disable_msi(accel_dev);
  244. }
  245. EXPORT_SYMBOL_GPL(adf_vf_isr_resource_free);
  246. /**
  247. * adf_vf_isr_resource_alloc() - Allocate IRQ for acceleration device
  248. * @accel_dev: Pointer to acceleration device.
  249. *
  250. * Function allocates interrupts for acceleration device virtual function.
  251. *
  252. * Return: 0 on success, error code otherwise.
  253. */
  254. int adf_vf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
  255. {
  256. if (adf_enable_msi(accel_dev))
  257. goto err_out;
  258. if (adf_setup_pf2vf_bh(accel_dev))
  259. goto err_disable_msi;
  260. if (adf_setup_bh(accel_dev))
  261. goto err_cleanup_pf2vf_bh;
  262. if (adf_request_msi_irq(accel_dev))
  263. goto err_cleanup_bh;
  264. return 0;
  265. err_cleanup_bh:
  266. adf_cleanup_bh(accel_dev);
  267. err_cleanup_pf2vf_bh:
  268. adf_cleanup_pf2vf_bh(accel_dev);
  269. err_disable_msi:
  270. adf_disable_msi(accel_dev);
  271. err_out:
  272. return -EFAULT;
  273. }
  274. EXPORT_SYMBOL_GPL(adf_vf_isr_resource_alloc);
  275. int __init adf_init_vf_wq(void)
  276. {
  277. adf_vf_stop_wq = alloc_workqueue("adf_vf_stop_wq", WQ_MEM_RECLAIM, 0);
  278. return !adf_vf_stop_wq ? -EFAULT : 0;
  279. }
  280. void adf_exit_vf_wq(void)
  281. {
  282. if (adf_vf_stop_wq)
  283. destroy_workqueue(adf_vf_stop_wq);
  284. adf_vf_stop_wq = NULL;
  285. }