adf_isr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "adf_accel_devices.h"
  49. #include "adf_common_drv.h"
  50. #include "adf_cfg.h"
  51. #include "adf_cfg_strings.h"
  52. #include "adf_cfg_common.h"
  53. #include "adf_transport_access_macros.h"
  54. #include "adf_transport_internal.h"
  55. static int adf_enable_msix(struct adf_accel_dev *accel_dev)
  56. {
  57. struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
  58. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  59. u32 msix_num_entries = 1;
  60. /* If SR-IOV is disabled, add entries for each bank */
  61. if (!accel_dev->pf.vf_info) {
  62. int i;
  63. msix_num_entries += hw_data->num_banks;
  64. for (i = 0; i < msix_num_entries; i++)
  65. pci_dev_info->msix_entries.entries[i].entry = i;
  66. } else {
  67. pci_dev_info->msix_entries.entries[0].entry =
  68. hw_data->num_banks;
  69. }
  70. if (pci_enable_msix_exact(pci_dev_info->pci_dev,
  71. pci_dev_info->msix_entries.entries,
  72. msix_num_entries)) {
  73. dev_err(&GET_DEV(accel_dev), "Failed to enable MSI-X IRQ(s)\n");
  74. return -EFAULT;
  75. }
  76. return 0;
  77. }
  78. static void adf_disable_msix(struct adf_accel_pci *pci_dev_info)
  79. {
  80. pci_disable_msix(pci_dev_info->pci_dev);
  81. }
  82. static irqreturn_t adf_msix_isr_bundle(int irq, void *bank_ptr)
  83. {
  84. struct adf_etr_bank_data *bank = bank_ptr;
  85. WRITE_CSR_INT_FLAG_AND_COL(bank->csr_addr, bank->bank_number, 0);
  86. tasklet_hi_schedule(&bank->resp_handler);
  87. return IRQ_HANDLED;
  88. }
  89. static irqreturn_t adf_msix_isr_ae(int irq, void *dev_ptr)
  90. {
  91. struct adf_accel_dev *accel_dev = dev_ptr;
  92. #ifdef CONFIG_PCI_IOV
  93. /* If SR-IOV is enabled (vf_info is non-NULL), check for VF->PF ints */
  94. if (accel_dev->pf.vf_info) {
  95. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  96. struct adf_bar *pmisc =
  97. &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)];
  98. void __iomem *pmisc_bar_addr = pmisc->virt_addr;
  99. u32 vf_mask;
  100. /* Get the interrupt sources triggered by VFs */
  101. vf_mask = ((ADF_CSR_RD(pmisc_bar_addr, ADF_ERRSOU5) &
  102. 0x0000FFFF) << 16) |
  103. ((ADF_CSR_RD(pmisc_bar_addr, ADF_ERRSOU3) &
  104. 0x01FFFE00) >> 9);
  105. if (vf_mask) {
  106. struct adf_accel_vf_info *vf_info;
  107. bool irq_handled = false;
  108. int i;
  109. /* Disable VF2PF interrupts for VFs with pending ints */
  110. adf_disable_vf2pf_interrupts(accel_dev, vf_mask);
  111. /*
  112. * Schedule tasklets to handle VF2PF interrupt BHs
  113. * unless the VF is malicious and is attempting to
  114. * flood the host OS with VF2PF interrupts.
  115. */
  116. for_each_set_bit(i, (const unsigned long *)&vf_mask,
  117. (sizeof(vf_mask) * BITS_PER_BYTE)) {
  118. vf_info = accel_dev->pf.vf_info + i;
  119. if (!__ratelimit(&vf_info->vf2pf_ratelimit)) {
  120. dev_info(&GET_DEV(accel_dev),
  121. "Too many ints from VF%d\n",
  122. vf_info->vf_nr + 1);
  123. continue;
  124. }
  125. /* Tasklet will re-enable ints from this VF */
  126. tasklet_hi_schedule(&vf_info->vf2pf_bh_tasklet);
  127. irq_handled = true;
  128. }
  129. if (irq_handled)
  130. return IRQ_HANDLED;
  131. }
  132. }
  133. #endif /* CONFIG_PCI_IOV */
  134. dev_dbg(&GET_DEV(accel_dev), "qat_dev%d spurious AE interrupt\n",
  135. accel_dev->accel_id);
  136. return IRQ_NONE;
  137. }
  138. static int adf_request_irqs(struct adf_accel_dev *accel_dev)
  139. {
  140. struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
  141. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  142. struct msix_entry *msixe = pci_dev_info->msix_entries.entries;
  143. struct adf_etr_data *etr_data = accel_dev->transport;
  144. int ret, i = 0;
  145. char *name;
  146. /* Request msix irq for all banks unless SR-IOV enabled */
  147. if (!accel_dev->pf.vf_info) {
  148. for (i = 0; i < hw_data->num_banks; i++) {
  149. struct adf_etr_bank_data *bank = &etr_data->banks[i];
  150. unsigned int cpu, cpus = num_online_cpus();
  151. name = *(pci_dev_info->msix_entries.names + i);
  152. snprintf(name, ADF_MAX_MSIX_VECTOR_NAME,
  153. "qat%d-bundle%d", accel_dev->accel_id, i);
  154. ret = request_irq(msixe[i].vector,
  155. adf_msix_isr_bundle, 0, name, bank);
  156. if (ret) {
  157. dev_err(&GET_DEV(accel_dev),
  158. "failed to enable irq %d for %s\n",
  159. msixe[i].vector, name);
  160. return ret;
  161. }
  162. cpu = ((accel_dev->accel_id * hw_data->num_banks) +
  163. i) % cpus;
  164. irq_set_affinity_hint(msixe[i].vector,
  165. get_cpu_mask(cpu));
  166. }
  167. }
  168. /* Request msix irq for AE */
  169. name = *(pci_dev_info->msix_entries.names + i);
  170. snprintf(name, ADF_MAX_MSIX_VECTOR_NAME,
  171. "qat%d-ae-cluster", accel_dev->accel_id);
  172. ret = request_irq(msixe[i].vector, adf_msix_isr_ae, 0, name, accel_dev);
  173. if (ret) {
  174. dev_err(&GET_DEV(accel_dev),
  175. "failed to enable irq %d, for %s\n",
  176. msixe[i].vector, name);
  177. return ret;
  178. }
  179. return ret;
  180. }
  181. static void adf_free_irqs(struct adf_accel_dev *accel_dev)
  182. {
  183. struct adf_accel_pci *pci_dev_info = &accel_dev->accel_pci_dev;
  184. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  185. struct msix_entry *msixe = pci_dev_info->msix_entries.entries;
  186. struct adf_etr_data *etr_data = accel_dev->transport;
  187. int i = 0;
  188. if (pci_dev_info->msix_entries.num_entries > 1) {
  189. for (i = 0; i < hw_data->num_banks; i++) {
  190. irq_set_affinity_hint(msixe[i].vector, NULL);
  191. free_irq(msixe[i].vector, &etr_data->banks[i]);
  192. }
  193. }
  194. irq_set_affinity_hint(msixe[i].vector, NULL);
  195. free_irq(msixe[i].vector, accel_dev);
  196. }
  197. static int adf_isr_alloc_msix_entry_table(struct adf_accel_dev *accel_dev)
  198. {
  199. int i;
  200. char **names;
  201. struct msix_entry *entries;
  202. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  203. u32 msix_num_entries = 1;
  204. /* If SR-IOV is disabled (vf_info is NULL), add entries for each bank */
  205. if (!accel_dev->pf.vf_info)
  206. msix_num_entries += hw_data->num_banks;
  207. entries = kzalloc_node(msix_num_entries * sizeof(*entries),
  208. GFP_KERNEL, dev_to_node(&GET_DEV(accel_dev)));
  209. if (!entries)
  210. return -ENOMEM;
  211. names = kcalloc(msix_num_entries, sizeof(char *), GFP_KERNEL);
  212. if (!names) {
  213. kfree(entries);
  214. return -ENOMEM;
  215. }
  216. for (i = 0; i < msix_num_entries; i++) {
  217. *(names + i) = kzalloc(ADF_MAX_MSIX_VECTOR_NAME, GFP_KERNEL);
  218. if (!(*(names + i)))
  219. goto err;
  220. }
  221. accel_dev->accel_pci_dev.msix_entries.num_entries = msix_num_entries;
  222. accel_dev->accel_pci_dev.msix_entries.entries = entries;
  223. accel_dev->accel_pci_dev.msix_entries.names = names;
  224. return 0;
  225. err:
  226. for (i = 0; i < msix_num_entries; i++)
  227. kfree(*(names + i));
  228. kfree(entries);
  229. kfree(names);
  230. return -ENOMEM;
  231. }
  232. static void adf_isr_free_msix_entry_table(struct adf_accel_dev *accel_dev)
  233. {
  234. char **names = accel_dev->accel_pci_dev.msix_entries.names;
  235. int i;
  236. kfree(accel_dev->accel_pci_dev.msix_entries.entries);
  237. for (i = 0; i < accel_dev->accel_pci_dev.msix_entries.num_entries; i++)
  238. kfree(*(names + i));
  239. kfree(names);
  240. }
  241. static int adf_setup_bh(struct adf_accel_dev *accel_dev)
  242. {
  243. struct adf_etr_data *priv_data = accel_dev->transport;
  244. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  245. int i;
  246. for (i = 0; i < hw_data->num_banks; i++)
  247. tasklet_init(&priv_data->banks[i].resp_handler,
  248. adf_response_handler,
  249. (unsigned long)&priv_data->banks[i]);
  250. return 0;
  251. }
  252. static void adf_cleanup_bh(struct adf_accel_dev *accel_dev)
  253. {
  254. struct adf_etr_data *priv_data = accel_dev->transport;
  255. struct adf_hw_device_data *hw_data = accel_dev->hw_device;
  256. int i;
  257. for (i = 0; i < hw_data->num_banks; i++) {
  258. tasklet_disable(&priv_data->banks[i].resp_handler);
  259. tasklet_kill(&priv_data->banks[i].resp_handler);
  260. }
  261. }
  262. /**
  263. * adf_isr_resource_free() - Free IRQ for acceleration device
  264. * @accel_dev: Pointer to acceleration device.
  265. *
  266. * Function frees interrupts for acceleration device.
  267. */
  268. void adf_isr_resource_free(struct adf_accel_dev *accel_dev)
  269. {
  270. adf_free_irqs(accel_dev);
  271. adf_cleanup_bh(accel_dev);
  272. adf_disable_msix(&accel_dev->accel_pci_dev);
  273. adf_isr_free_msix_entry_table(accel_dev);
  274. }
  275. EXPORT_SYMBOL_GPL(adf_isr_resource_free);
  276. /**
  277. * adf_isr_resource_alloc() - Allocate IRQ for acceleration device
  278. * @accel_dev: Pointer to acceleration device.
  279. *
  280. * Function allocates interrupts for acceleration device.
  281. *
  282. * Return: 0 on success, error code otherwise.
  283. */
  284. int adf_isr_resource_alloc(struct adf_accel_dev *accel_dev)
  285. {
  286. int ret;
  287. ret = adf_isr_alloc_msix_entry_table(accel_dev);
  288. if (ret)
  289. goto err_out;
  290. ret = adf_enable_msix(accel_dev);
  291. if (ret)
  292. goto err_free_msix_table;
  293. ret = adf_setup_bh(accel_dev);
  294. if (ret)
  295. goto err_disable_msix;
  296. ret = adf_request_irqs(accel_dev);
  297. if (ret)
  298. goto err_cleanup_bh;
  299. return 0;
  300. err_cleanup_bh:
  301. adf_cleanup_bh(accel_dev);
  302. err_disable_msix:
  303. adf_disable_msix(&accel_dev->accel_pci_dev);
  304. err_free_msix_table:
  305. adf_isr_free_msix_entry_table(accel_dev);
  306. err_out:
  307. return ret;
  308. }
  309. EXPORT_SYMBOL_GPL(adf_isr_resource_alloc);