adf_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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_transport_access_macros.h>
  58. #include "adf_dh895xccvf_hw_data.h"
  59. #include "adf_drv.h"
  60. static const char adf_driver_name[] = ADF_DH895XCCVF_DEVICE_NAME;
  61. #define ADF_SYSTEM_DEVICE(device_id) \
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
  63. static const struct pci_device_id adf_pci_tbl[] = {
  64. ADF_SYSTEM_DEVICE(ADF_DH895XCCIOV_PCI_DEVICE_ID),
  65. {0,}
  66. };
  67. MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
  68. static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
  69. static void adf_remove(struct pci_dev *dev);
  70. static struct pci_driver adf_driver = {
  71. .id_table = adf_pci_tbl,
  72. .name = adf_driver_name,
  73. .probe = adf_probe,
  74. .remove = adf_remove,
  75. };
  76. static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)
  77. {
  78. pci_release_regions(accel_dev->accel_pci_dev.pci_dev);
  79. pci_disable_device(accel_dev->accel_pci_dev.pci_dev);
  80. }
  81. static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
  82. {
  83. struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
  84. struct adf_accel_dev *pf;
  85. int i;
  86. for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
  87. struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
  88. if (bar->virt_addr)
  89. pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
  90. }
  91. if (accel_dev->hw_device) {
  92. switch (accel_pci_dev->pci_dev->device) {
  93. case ADF_DH895XCCIOV_PCI_DEVICE_ID:
  94. adf_clean_hw_data_dh895xcciov(accel_dev->hw_device);
  95. break;
  96. default:
  97. break;
  98. }
  99. kfree(accel_dev->hw_device);
  100. accel_dev->hw_device = NULL;
  101. }
  102. adf_cfg_dev_remove(accel_dev);
  103. debugfs_remove(accel_dev->debugfs_dir);
  104. pf = adf_devmgr_pci_to_accel_dev(accel_pci_dev->pci_dev->physfn);
  105. adf_devmgr_rm_dev(accel_dev, pf);
  106. }
  107. static int adf_dev_configure(struct adf_accel_dev *accel_dev)
  108. {
  109. char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
  110. unsigned long val, bank = 0;
  111. if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC))
  112. goto err;
  113. if (adf_cfg_section_add(accel_dev, "Accelerator0"))
  114. goto err;
  115. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, 0);
  116. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, key,
  117. (void *)&bank, ADF_DEC))
  118. goto err;
  119. val = bank;
  120. snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY, 0);
  121. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, key,
  122. (void *)&val, ADF_DEC))
  123. goto err;
  124. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, 0);
  125. val = 128;
  126. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, key,
  127. (void *)&val, ADF_DEC))
  128. goto err;
  129. val = 512;
  130. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, 0);
  131. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  132. key, (void *)&val, ADF_DEC))
  133. goto err;
  134. val = 0;
  135. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, 0);
  136. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  137. key, (void *)&val, ADF_DEC))
  138. goto err;
  139. val = 2;
  140. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, 0);
  141. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  142. key, (void *)&val, ADF_DEC))
  143. goto err;
  144. val = 8;
  145. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, 0);
  146. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  147. key, (void *)&val, ADF_DEC))
  148. goto err;
  149. val = 10;
  150. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, 0);
  151. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  152. key, (void *)&val, ADF_DEC))
  153. goto err;
  154. val = ADF_COALESCING_DEF_TIME;
  155. snprintf(key, sizeof(key), ADF_ETRMGR_COALESCE_TIMER_FORMAT,
  156. (int)bank);
  157. if (adf_cfg_add_key_value_param(accel_dev, "Accelerator0",
  158. key, (void *)&val, ADF_DEC))
  159. goto err;
  160. val = 1;
  161. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  162. ADF_NUM_CY, (void *)&val, ADF_DEC))
  163. goto err;
  164. set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
  165. return 0;
  166. err:
  167. dev_err(&GET_DEV(accel_dev), "Failed to configure QAT accel dev\n");
  168. return -EINVAL;
  169. }
  170. static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  171. {
  172. struct adf_accel_dev *accel_dev;
  173. struct adf_accel_dev *pf;
  174. struct adf_accel_pci *accel_pci_dev;
  175. struct adf_hw_device_data *hw_data;
  176. char name[ADF_DEVICE_NAME_LENGTH];
  177. unsigned int i, bar_nr;
  178. int ret, bar_mask;
  179. switch (ent->device) {
  180. case ADF_DH895XCCIOV_PCI_DEVICE_ID:
  181. break;
  182. default:
  183. dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);
  184. return -ENODEV;
  185. }
  186. accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
  187. dev_to_node(&pdev->dev));
  188. if (!accel_dev)
  189. return -ENOMEM;
  190. accel_dev->is_vf = true;
  191. pf = adf_devmgr_pci_to_accel_dev(pdev->physfn);
  192. accel_pci_dev = &accel_dev->accel_pci_dev;
  193. accel_pci_dev->pci_dev = pdev;
  194. /* Add accel device to accel table */
  195. if (adf_devmgr_add_dev(accel_dev, pf)) {
  196. dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
  197. kfree(accel_dev);
  198. return -EFAULT;
  199. }
  200. INIT_LIST_HEAD(&accel_dev->crypto_list);
  201. accel_dev->owner = THIS_MODULE;
  202. /* Allocate and configure device configuration structure */
  203. hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,
  204. dev_to_node(&pdev->dev));
  205. if (!hw_data) {
  206. ret = -ENOMEM;
  207. goto out_err;
  208. }
  209. accel_dev->hw_device = hw_data;
  210. switch (ent->device) {
  211. case ADF_DH895XCCIOV_PCI_DEVICE_ID:
  212. adf_init_hw_data_dh895xcciov(accel_dev->hw_device);
  213. break;
  214. default:
  215. ret = -ENODEV;
  216. goto out_err;
  217. }
  218. /* Get Accelerators and Accelerators Engines masks */
  219. hw_data->accel_mask = hw_data->get_accel_mask(hw_data->fuses);
  220. hw_data->ae_mask = hw_data->get_ae_mask(hw_data->fuses);
  221. accel_pci_dev->sku = hw_data->get_sku(hw_data);
  222. /* Create dev top level debugfs entry */
  223. snprintf(name, sizeof(name), "%s%s_%02x:%02d.%02d",
  224. ADF_DEVICE_NAME_PREFIX, hw_data->dev_class->name,
  225. pdev->bus->number, PCI_SLOT(pdev->devfn),
  226. PCI_FUNC(pdev->devfn));
  227. accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
  228. if (!accel_dev->debugfs_dir) {
  229. dev_err(&pdev->dev, "Could not create debugfs dir %s\n", name);
  230. ret = -EINVAL;
  231. goto out_err;
  232. }
  233. /* Create device configuration table */
  234. ret = adf_cfg_dev_add(accel_dev);
  235. if (ret)
  236. goto out_err;
  237. /* enable PCI device */
  238. if (pci_enable_device(pdev)) {
  239. ret = -EFAULT;
  240. goto out_err;
  241. }
  242. /* set dma identifier */
  243. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  244. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
  245. dev_err(&pdev->dev, "No usable DMA configuration\n");
  246. ret = -EFAULT;
  247. goto out_err_disable;
  248. } else {
  249. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  250. }
  251. } else {
  252. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  253. }
  254. if (pci_request_regions(pdev, adf_driver_name)) {
  255. ret = -EFAULT;
  256. goto out_err_disable;
  257. }
  258. /* Find and map all the device's BARS */
  259. i = 0;
  260. bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
  261. for_each_set_bit(bar_nr, (const unsigned long *)&bar_mask,
  262. ADF_PCI_MAX_BARS * 2) {
  263. struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
  264. bar->base_addr = pci_resource_start(pdev, bar_nr);
  265. if (!bar->base_addr)
  266. break;
  267. bar->size = pci_resource_len(pdev, bar_nr);
  268. bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
  269. if (!bar->virt_addr) {
  270. dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
  271. ret = -EFAULT;
  272. goto out_err_free_reg;
  273. }
  274. }
  275. pci_set_master(pdev);
  276. /* Completion for VF2PF request/response message exchange */
  277. init_completion(&accel_dev->vf.iov_msg_completion);
  278. ret = adf_dev_configure(accel_dev);
  279. if (ret)
  280. goto out_err_free_reg;
  281. ret = adf_dev_init(accel_dev);
  282. if (ret)
  283. goto out_err_dev_shutdown;
  284. ret = adf_dev_start(accel_dev);
  285. if (ret)
  286. goto out_err_dev_stop;
  287. return ret;
  288. out_err_dev_stop:
  289. adf_dev_stop(accel_dev);
  290. out_err_dev_shutdown:
  291. adf_dev_shutdown(accel_dev);
  292. out_err_free_reg:
  293. pci_release_regions(accel_pci_dev->pci_dev);
  294. out_err_disable:
  295. pci_disable_device(accel_pci_dev->pci_dev);
  296. out_err:
  297. adf_cleanup_accel(accel_dev);
  298. kfree(accel_dev);
  299. return ret;
  300. }
  301. static void adf_remove(struct pci_dev *pdev)
  302. {
  303. struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
  304. if (!accel_dev) {
  305. pr_err("QAT: Driver removal failed\n");
  306. return;
  307. }
  308. if (adf_dev_stop(accel_dev))
  309. dev_err(&GET_DEV(accel_dev), "Failed to stop QAT accel dev\n");
  310. adf_dev_shutdown(accel_dev);
  311. adf_cleanup_accel(accel_dev);
  312. adf_cleanup_pci_dev(accel_dev);
  313. kfree(accel_dev);
  314. }
  315. static int __init adfdrv_init(void)
  316. {
  317. request_module("intel_qat");
  318. if (pci_register_driver(&adf_driver)) {
  319. pr_err("QAT: Driver initialization failed\n");
  320. return -EFAULT;
  321. }
  322. return 0;
  323. }
  324. static void __exit adfdrv_release(void)
  325. {
  326. pci_unregister_driver(&adf_driver);
  327. adf_clean_vf_map(true);
  328. }
  329. module_init(adfdrv_init);
  330. module_exit(adfdrv_release);
  331. MODULE_LICENSE("Dual BSD/GPL");
  332. MODULE_AUTHOR("Intel");
  333. MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
  334. MODULE_VERSION(ADF_DRV_VERSION);