Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / crypto / qat / qat_c62x / adf_drv.c
blob1f5de442e1e6de8b6aaf30b53f9d8abc8010337e
1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/pci.h>
6 #include <linux/init.h>
7 #include <linux/types.h>
8 #include <linux/fs.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/platform_device.h>
14 #include <linux/workqueue.h>
15 #include <linux/io.h>
16 #include <adf_accel_devices.h>
17 #include <adf_common_drv.h>
18 #include <adf_cfg.h>
19 #include "adf_c62x_hw_data.h"
21 static const struct pci_device_id adf_pci_tbl[] = {
22 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_C62X), },
23 { }
25 MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
27 static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
28 static void adf_remove(struct pci_dev *dev);
30 static struct pci_driver adf_driver = {
31 .id_table = adf_pci_tbl,
32 .name = ADF_C62X_DEVICE_NAME,
33 .probe = adf_probe,
34 .remove = adf_remove,
35 .sriov_configure = adf_sriov_configure,
38 static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)
40 pci_release_regions(accel_dev->accel_pci_dev.pci_dev);
41 pci_disable_device(accel_dev->accel_pci_dev.pci_dev);
44 static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
46 struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
47 int i;
49 for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
50 struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
52 if (bar->virt_addr)
53 pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
56 if (accel_dev->hw_device) {
57 switch (accel_pci_dev->pci_dev->device) {
58 case PCI_DEVICE_ID_INTEL_QAT_C62X:
59 adf_clean_hw_data_c62x(accel_dev->hw_device);
60 break;
61 default:
62 break;
64 kfree(accel_dev->hw_device);
65 accel_dev->hw_device = NULL;
67 adf_cfg_dev_remove(accel_dev);
68 debugfs_remove(accel_dev->debugfs_dir);
69 adf_devmgr_rm_dev(accel_dev, NULL);
72 static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
74 struct adf_accel_dev *accel_dev;
75 struct adf_accel_pci *accel_pci_dev;
76 struct adf_hw_device_data *hw_data;
77 char name[ADF_DEVICE_NAME_LENGTH];
78 unsigned int i, bar_nr;
79 unsigned long bar_mask;
80 int ret;
82 switch (ent->device) {
83 case PCI_DEVICE_ID_INTEL_QAT_C62X:
84 break;
85 default:
86 dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);
87 return -ENODEV;
90 if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {
91 /* If the accelerator is connected to a node with no memory
92 * there is no point in using the accelerator since the remote
93 * memory transaction will be very slow. */
94 dev_err(&pdev->dev, "Invalid NUMA configuration.\n");
95 return -EINVAL;
98 accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
99 dev_to_node(&pdev->dev));
100 if (!accel_dev)
101 return -ENOMEM;
103 INIT_LIST_HEAD(&accel_dev->crypto_list);
104 accel_pci_dev = &accel_dev->accel_pci_dev;
105 accel_pci_dev->pci_dev = pdev;
107 /* Add accel device to accel table.
108 * This should be called before adf_cleanup_accel is called */
109 if (adf_devmgr_add_dev(accel_dev, NULL)) {
110 dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
111 kfree(accel_dev);
112 return -EFAULT;
115 accel_dev->owner = THIS_MODULE;
116 /* Allocate and configure device configuration structure */
117 hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,
118 dev_to_node(&pdev->dev));
119 if (!hw_data) {
120 ret = -ENOMEM;
121 goto out_err;
124 accel_dev->hw_device = hw_data;
125 adf_init_hw_data_c62x(accel_dev->hw_device);
126 pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);
127 pci_read_config_dword(pdev, ADF_DEVICE_FUSECTL_OFFSET,
128 &hw_data->fuses);
129 pci_read_config_dword(pdev, ADF_C62X_SOFTSTRAP_CSR_OFFSET,
130 &hw_data->straps);
132 /* Get Accelerators and Accelerators Engines masks */
133 hw_data->accel_mask = hw_data->get_accel_mask(hw_data);
134 hw_data->ae_mask = hw_data->get_ae_mask(hw_data);
135 accel_pci_dev->sku = hw_data->get_sku(hw_data);
136 /* If the device has no acceleration engines then ignore it. */
137 if (!hw_data->accel_mask || !hw_data->ae_mask ||
138 ((~hw_data->ae_mask) & 0x01)) {
139 dev_err(&pdev->dev, "No acceleration units found");
140 ret = -EFAULT;
141 goto out_err;
144 /* Create dev top level debugfs entry */
145 snprintf(name, sizeof(name), "%s%s_%s", ADF_DEVICE_NAME_PREFIX,
146 hw_data->dev_class->name, pci_name(pdev));
148 accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
150 /* Create device configuration table */
151 ret = adf_cfg_dev_add(accel_dev);
152 if (ret)
153 goto out_err;
155 /* enable PCI device */
156 if (pci_enable_device(pdev)) {
157 ret = -EFAULT;
158 goto out_err;
161 /* set dma identifier */
162 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
163 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
164 dev_err(&pdev->dev, "No usable DMA configuration\n");
165 ret = -EFAULT;
166 goto out_err_disable;
167 } else {
168 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
171 } else {
172 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
175 if (pci_request_regions(pdev, ADF_C62X_DEVICE_NAME)) {
176 ret = -EFAULT;
177 goto out_err_disable;
180 /* Get accelerator capabilities mask */
181 hw_data->accel_capabilities_mask = hw_data->get_accel_cap(accel_dev);
183 /* Find and map all the device's BARS */
184 i = (hw_data->fuses & ADF_DEVICE_FUSECTL_MASK) ? 1 : 0;
185 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
186 for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {
187 struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
189 bar->base_addr = pci_resource_start(pdev, bar_nr);
190 if (!bar->base_addr)
191 break;
192 bar->size = pci_resource_len(pdev, bar_nr);
193 bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
194 if (!bar->virt_addr) {
195 dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
196 ret = -EFAULT;
197 goto out_err_free_reg;
200 pci_set_master(pdev);
202 if (adf_enable_aer(accel_dev)) {
203 dev_err(&pdev->dev, "Failed to enable aer\n");
204 ret = -EFAULT;
205 goto out_err_free_reg;
208 if (pci_save_state(pdev)) {
209 dev_err(&pdev->dev, "Failed to save pci state\n");
210 ret = -ENOMEM;
211 goto out_err_free_reg;
214 ret = qat_crypto_dev_config(accel_dev);
215 if (ret)
216 goto out_err_free_reg;
218 ret = adf_dev_init(accel_dev);
219 if (ret)
220 goto out_err_dev_shutdown;
222 ret = adf_dev_start(accel_dev);
223 if (ret)
224 goto out_err_dev_stop;
226 return ret;
228 out_err_dev_stop:
229 adf_dev_stop(accel_dev);
230 out_err_dev_shutdown:
231 adf_dev_shutdown(accel_dev);
232 out_err_free_reg:
233 pci_release_regions(accel_pci_dev->pci_dev);
234 out_err_disable:
235 pci_disable_device(accel_pci_dev->pci_dev);
236 out_err:
237 adf_cleanup_accel(accel_dev);
238 kfree(accel_dev);
239 return ret;
242 static void adf_remove(struct pci_dev *pdev)
244 struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
246 if (!accel_dev) {
247 pr_err("QAT: Driver removal failed\n");
248 return;
250 adf_dev_stop(accel_dev);
251 adf_dev_shutdown(accel_dev);
252 adf_disable_aer(accel_dev);
253 adf_cleanup_accel(accel_dev);
254 adf_cleanup_pci_dev(accel_dev);
255 kfree(accel_dev);
258 static int __init adfdrv_init(void)
260 request_module("intel_qat");
262 if (pci_register_driver(&adf_driver)) {
263 pr_err("QAT: Driver initialization failed\n");
264 return -EFAULT;
266 return 0;
269 static void __exit adfdrv_release(void)
271 pci_unregister_driver(&adf_driver);
274 module_init(adfdrv_init);
275 module_exit(adfdrv_release);
277 MODULE_LICENSE("Dual BSD/GPL");
278 MODULE_AUTHOR("Intel");
279 MODULE_FIRMWARE(ADF_C62X_FW);
280 MODULE_FIRMWARE(ADF_C62X_MMP);
281 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
282 MODULE_VERSION(ADF_DRV_VERSION);