Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / crypto / intel / qat / qat_4xxx / adf_drv.c
blobd7de1cad1335436095c58c63a025df41bdc2698a
1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2020 Intel Corporation */
3 #include <linux/device.h>
4 #include <linux/module.h>
5 #include <linux/pci.h>
7 #include <adf_accel_devices.h>
8 #include <adf_cfg.h>
9 #include <adf_common_drv.h>
10 #include <adf_dbgfs.h>
11 #include <adf_gen4_config.h>
12 #include <adf_gen4_hw_data.h>
14 #include "adf_4xxx_hw_data.h"
16 static const struct pci_device_id adf_pci_tbl[] = {
17 { PCI_VDEVICE(INTEL, ADF_4XXX_PCI_DEVICE_ID), },
18 { PCI_VDEVICE(INTEL, ADF_401XX_PCI_DEVICE_ID), },
19 { PCI_VDEVICE(INTEL, ADF_402XX_PCI_DEVICE_ID), },
20 { }
22 MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
24 static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
26 if (accel_dev->hw_device) {
27 adf_clean_hw_data_4xxx(accel_dev->hw_device);
28 accel_dev->hw_device = NULL;
30 adf_dbgfs_exit(accel_dev);
31 adf_cfg_dev_remove(accel_dev);
32 adf_devmgr_rm_dev(accel_dev, NULL);
35 static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
37 struct adf_accel_dev *accel_dev;
38 struct adf_accel_pci *accel_pci_dev;
39 struct adf_hw_device_data *hw_data;
40 unsigned int i, bar_nr;
41 unsigned long bar_mask;
42 struct adf_bar *bar;
43 int ret;
45 if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {
47 * If the accelerator is connected to a node with no memory
48 * there is no point in using the accelerator since the remote
49 * memory transaction will be very slow.
51 dev_err(&pdev->dev, "Invalid NUMA configuration.\n");
52 return -EINVAL;
55 accel_dev = devm_kzalloc(&pdev->dev, sizeof(*accel_dev), GFP_KERNEL);
56 if (!accel_dev)
57 return -ENOMEM;
59 INIT_LIST_HEAD(&accel_dev->crypto_list);
60 accel_pci_dev = &accel_dev->accel_pci_dev;
61 accel_pci_dev->pci_dev = pdev;
64 * Add accel device to accel table
65 * This should be called before adf_cleanup_accel is called
67 if (adf_devmgr_add_dev(accel_dev, NULL)) {
68 dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
69 return -EFAULT;
72 accel_dev->owner = THIS_MODULE;
73 /* Allocate and initialise device hardware meta-data structure */
74 hw_data = devm_kzalloc(&pdev->dev, sizeof(*hw_data), GFP_KERNEL);
75 if (!hw_data) {
76 ret = -ENOMEM;
77 goto out_err;
80 accel_dev->hw_device = hw_data;
81 adf_init_hw_data_4xxx(accel_dev->hw_device, ent->device);
83 pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);
84 pci_read_config_dword(pdev, ADF_GEN4_FUSECTL4_OFFSET, &hw_data->fuses);
86 /* Get Accelerators and Accelerators Engines masks */
87 hw_data->accel_mask = hw_data->get_accel_mask(hw_data);
88 hw_data->ae_mask = hw_data->get_ae_mask(hw_data);
89 accel_pci_dev->sku = hw_data->get_sku(hw_data);
90 /* If the device has no acceleration engines then ignore it */
91 if (!hw_data->accel_mask || !hw_data->ae_mask ||
92 (~hw_data->ae_mask & 0x01)) {
93 dev_err(&pdev->dev, "No acceleration units found.\n");
94 ret = -EFAULT;
95 goto out_err;
98 /* Create device configuration table */
99 ret = adf_cfg_dev_add(accel_dev);
100 if (ret)
101 goto out_err;
103 /* Enable PCI device */
104 ret = pcim_enable_device(pdev);
105 if (ret) {
106 dev_err(&pdev->dev, "Can't enable PCI device.\n");
107 goto out_err;
110 /* Set DMA identifier */
111 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
112 if (ret) {
113 dev_err(&pdev->dev, "No usable DMA configuration.\n");
114 goto out_err;
117 ret = adf_gen4_cfg_dev_init(accel_dev);
118 if (ret) {
119 dev_err(&pdev->dev, "Failed to initialize configuration.\n");
120 goto out_err;
123 /* Get accelerator capabilities mask */
124 hw_data->accel_capabilities_mask = hw_data->get_accel_cap(accel_dev);
125 if (!hw_data->accel_capabilities_mask) {
126 dev_err(&pdev->dev, "Failed to get capabilities mask.\n");
127 ret = -EINVAL;
128 goto out_err;
131 /* Find and map all the device's BARS */
132 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM) & ADF_GEN4_BAR_MASK;
134 ret = pcim_request_all_regions(pdev, pci_name(pdev));
135 if (ret) {
136 dev_err(&pdev->dev, "Failed to request PCI regions.\n");
137 goto out_err;
140 i = 0;
141 for_each_set_bit(bar_nr, &bar_mask, PCI_STD_NUM_BARS) {
142 bar = &accel_pci_dev->pci_bars[i++];
143 bar->virt_addr = pcim_iomap(pdev, bar_nr, 0);
144 if (!bar->virt_addr) {
145 dev_err(&pdev->dev, "Failed to ioremap PCI region.\n");
146 ret = -ENOMEM;
147 goto out_err;
151 pci_set_master(pdev);
153 if (pci_save_state(pdev)) {
154 dev_err(&pdev->dev, "Failed to save pci state.\n");
155 ret = -ENOMEM;
156 goto out_err;
159 accel_dev->ras_errors.enabled = true;
160 adf_dbgfs_init(accel_dev);
162 ret = adf_dev_up(accel_dev, true);
163 if (ret)
164 goto out_err_dev_stop;
166 ret = adf_sysfs_init(accel_dev);
167 if (ret)
168 goto out_err_dev_stop;
170 return ret;
172 out_err_dev_stop:
173 adf_dev_down(accel_dev);
174 out_err:
175 adf_cleanup_accel(accel_dev);
176 return ret;
179 static void adf_remove(struct pci_dev *pdev)
181 struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
183 if (!accel_dev) {
184 pr_err("QAT: Driver removal failed\n");
185 return;
187 adf_dev_down(accel_dev);
188 adf_cleanup_accel(accel_dev);
191 static struct pci_driver adf_driver = {
192 .id_table = adf_pci_tbl,
193 .name = ADF_4XXX_DEVICE_NAME,
194 .probe = adf_probe,
195 .remove = adf_remove,
196 .sriov_configure = adf_sriov_configure,
197 .err_handler = &adf_err_handler,
200 module_pci_driver(adf_driver);
202 MODULE_LICENSE("Dual BSD/GPL");
203 MODULE_AUTHOR("Intel");
204 MODULE_FIRMWARE(ADF_4XXX_FW);
205 MODULE_FIRMWARE(ADF_402XX_FW);
206 MODULE_FIRMWARE(ADF_4XXX_MMP);
207 MODULE_FIRMWARE(ADF_402XX_MMP);
208 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
209 MODULE_VERSION(ADF_DRV_VERSION);
210 MODULE_SOFTDEP("pre: crypto-intel_qat");
211 MODULE_IMPORT_NS("CRYPTO_QAT");