adf_drv.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/module.h>
  43. #include <linux/pci.h>
  44. #include <linux/init.h>
  45. #include <linux/types.h>
  46. #include <linux/fs.h>
  47. #include <linux/slab.h>
  48. #include <linux/errno.h>
  49. #include <linux/device.h>
  50. #include <linux/dma-mapping.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/workqueue.h>
  53. #include <linux/io.h>
  54. #include <adf_accel_devices.h>
  55. #include <adf_common_drv.h>
  56. #include <adf_cfg.h>
  57. #include "adf_c3xxxvf_hw_data.h"
  58. #define ADF_SYSTEM_DEVICE(device_id) \
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
  60. static const struct pci_device_id adf_pci_tbl[] = {
  61. ADF_SYSTEM_DEVICE(ADF_C3XXXIOV_PCI_DEVICE_ID),
  62. {0,}
  63. };
  64. MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
  65. static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
  66. static void adf_remove(struct pci_dev *dev);
  67. static struct pci_driver adf_driver = {
  68. .id_table = adf_pci_tbl,
  69. .name = ADF_C3XXXVF_DEVICE_NAME,
  70. .probe = adf_probe,
  71. .remove = adf_remove,
  72. };
  73. static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)
  74. {
  75. pci_release_regions(accel_dev->accel_pci_dev.pci_dev);
  76. pci_disable_device(accel_dev->accel_pci_dev.pci_dev);
  77. }
  78. static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
  79. {
  80. struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
  81. struct adf_accel_dev *pf;
  82. int i;
  83. for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
  84. struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
  85. if (bar->virt_addr)
  86. pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
  87. }
  88. if (accel_dev->hw_device) {
  89. switch (accel_pci_dev->pci_dev->device) {
  90. case ADF_C3XXXIOV_PCI_DEVICE_ID:
  91. adf_clean_hw_data_c3xxxiov(accel_dev->hw_device);
  92. break;
  93. default:
  94. break;
  95. }
  96. kfree(accel_dev->hw_device);
  97. accel_dev->hw_device = NULL;
  98. }
  99. adf_cfg_dev_remove(accel_dev);
  100. debugfs_remove(accel_dev->debugfs_dir);
  101. pf = adf_devmgr_pci_to_accel_dev(accel_pci_dev->pci_dev->physfn);
  102. adf_devmgr_rm_dev(accel_dev, pf);
  103. }
  104. static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  105. {
  106. struct adf_accel_dev *accel_dev;
  107. struct adf_accel_dev *pf;
  108. struct adf_accel_pci *accel_pci_dev;
  109. struct adf_hw_device_data *hw_data;
  110. char name[ADF_DEVICE_NAME_LENGTH];
  111. unsigned int i, bar_nr;
  112. unsigned long bar_mask;
  113. int ret;
  114. switch (ent->device) {
  115. case ADF_C3XXXIOV_PCI_DEVICE_ID:
  116. break;
  117. default:
  118. dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);
  119. return -ENODEV;
  120. }
  121. accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
  122. dev_to_node(&pdev->dev));
  123. if (!accel_dev)
  124. return -ENOMEM;
  125. accel_dev->is_vf = true;
  126. pf = adf_devmgr_pci_to_accel_dev(pdev->physfn);
  127. accel_pci_dev = &accel_dev->accel_pci_dev;
  128. accel_pci_dev->pci_dev = pdev;
  129. /* Add accel device to accel table */
  130. if (adf_devmgr_add_dev(accel_dev, pf)) {
  131. dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
  132. kfree(accel_dev);
  133. return -EFAULT;
  134. }
  135. INIT_LIST_HEAD(&accel_dev->crypto_list);
  136. accel_dev->owner = THIS_MODULE;
  137. /* Allocate and configure device configuration structure */
  138. hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,
  139. dev_to_node(&pdev->dev));
  140. if (!hw_data) {
  141. ret = -ENOMEM;
  142. goto out_err;
  143. }
  144. accel_dev->hw_device = hw_data;
  145. adf_init_hw_data_c3xxxiov(accel_dev->hw_device);
  146. /* Get Accelerators and Accelerators Engines masks */
  147. hw_data->accel_mask = hw_data->get_accel_mask(hw_data->fuses);
  148. hw_data->ae_mask = hw_data->get_ae_mask(hw_data->fuses);
  149. accel_pci_dev->sku = hw_data->get_sku(hw_data);
  150. /* Create dev top level debugfs entry */
  151. snprintf(name, sizeof(name), "%s%s_%02x:%02d.%d",
  152. ADF_DEVICE_NAME_PREFIX, hw_data->dev_class->name,
  153. pdev->bus->number, PCI_SLOT(pdev->devfn),
  154. PCI_FUNC(pdev->devfn));
  155. accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
  156. if (!accel_dev->debugfs_dir) {
  157. dev_err(&pdev->dev, "Could not create debugfs dir %s\n", name);
  158. ret = -EINVAL;
  159. goto out_err;
  160. }
  161. /* Create device configuration table */
  162. ret = adf_cfg_dev_add(accel_dev);
  163. if (ret)
  164. goto out_err;
  165. /* enable PCI device */
  166. if (pci_enable_device(pdev)) {
  167. ret = -EFAULT;
  168. goto out_err;
  169. }
  170. /* set dma identifier */
  171. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  172. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  173. dev_err(&pdev->dev, "No usable DMA configuration\n");
  174. ret = -EFAULT;
  175. goto out_err_disable;
  176. } else {
  177. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  178. }
  179. } else {
  180. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  181. }
  182. if (pci_request_regions(pdev, ADF_C3XXXVF_DEVICE_NAME)) {
  183. ret = -EFAULT;
  184. goto out_err_disable;
  185. }
  186. /* Find and map all the device's BARS */
  187. i = 0;
  188. bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
  189. for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {
  190. struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
  191. bar->base_addr = pci_resource_start(pdev, bar_nr);
  192. if (!bar->base_addr)
  193. break;
  194. bar->size = pci_resource_len(pdev, bar_nr);
  195. bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
  196. if (!bar->virt_addr) {
  197. dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
  198. ret = -EFAULT;
  199. goto out_err_free_reg;
  200. }
  201. }
  202. pci_set_master(pdev);
  203. /* Completion for VF2PF request/response message exchange */
  204. init_completion(&accel_dev->vf.iov_msg_completion);
  205. ret = qat_crypto_dev_config(accel_dev);
  206. if (ret)
  207. goto out_err_free_reg;
  208. set_bit(ADF_STATUS_PF_RUNNING, &accel_dev->status);
  209. ret = adf_dev_init(accel_dev);
  210. if (ret)
  211. goto out_err_dev_shutdown;
  212. ret = adf_dev_start(accel_dev);
  213. if (ret)
  214. goto out_err_dev_stop;
  215. return ret;
  216. out_err_dev_stop:
  217. adf_dev_stop(accel_dev);
  218. out_err_dev_shutdown:
  219. adf_dev_shutdown(accel_dev);
  220. out_err_free_reg:
  221. pci_release_regions(accel_pci_dev->pci_dev);
  222. out_err_disable:
  223. pci_disable_device(accel_pci_dev->pci_dev);
  224. out_err:
  225. adf_cleanup_accel(accel_dev);
  226. kfree(accel_dev);
  227. return ret;
  228. }
  229. static void adf_remove(struct pci_dev *pdev)
  230. {
  231. struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
  232. if (!accel_dev) {
  233. pr_err("QAT: Driver removal failed\n");
  234. return;
  235. }
  236. adf_dev_stop(accel_dev);
  237. adf_dev_shutdown(accel_dev);
  238. adf_cleanup_accel(accel_dev);
  239. adf_cleanup_pci_dev(accel_dev);
  240. kfree(accel_dev);
  241. }
  242. static int __init adfdrv_init(void)
  243. {
  244. request_module("intel_qat");
  245. if (pci_register_driver(&adf_driver)) {
  246. pr_err("QAT: Driver initialization failed\n");
  247. return -EFAULT;
  248. }
  249. return 0;
  250. }
  251. static void __exit adfdrv_release(void)
  252. {
  253. pci_unregister_driver(&adf_driver);
  254. adf_clean_vf_map(true);
  255. }
  256. module_init(adfdrv_init);
  257. module_exit(adfdrv_release);
  258. MODULE_LICENSE("Dual BSD/GPL");
  259. MODULE_AUTHOR("Intel");
  260. MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
  261. MODULE_VERSION(ADF_DRV_VERSION);