xref: /linux/drivers/crypto/intel/qat/qat_4xxx/adf_drv.c (revision 151ebcf0797b1a3ba53c8843dc21748c80e098c7)
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>
6 
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>
13 
14 #include "adf_4xxx_hw_data.h"
15 
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 	{ }
21 };
22 MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
23 
24 static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
25 {
26 	if (accel_dev->hw_device) {
27 		adf_clean_hw_data_4xxx(accel_dev->hw_device);
28 		accel_dev->hw_device = NULL;
29 	}
30 	adf_dbgfs_exit(accel_dev);
31 	adf_cfg_dev_remove(accel_dev);
32 	adf_devmgr_rm_dev(accel_dev, NULL);
33 }
34 
35 static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
36 {
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;
44 
45 	if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {
46 		/*
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.
50 		 */
51 		dev_err(&pdev->dev, "Invalid NUMA configuration.\n");
52 		return -EINVAL;
53 	}
54 
55 	accel_dev = devm_kzalloc(&pdev->dev, sizeof(*accel_dev), GFP_KERNEL);
56 	if (!accel_dev)
57 		return -ENOMEM;
58 
59 	INIT_LIST_HEAD(&accel_dev->crypto_list);
60 	accel_pci_dev = &accel_dev->accel_pci_dev;
61 	accel_pci_dev->pci_dev = pdev;
62 
63 	/*
64 	 * Add accel device to accel table
65 	 * This should be called before adf_cleanup_accel is called
66 	 */
67 	if (adf_devmgr_add_dev(accel_dev, NULL)) {
68 		dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
69 		return -EFAULT;
70 	}
71 
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;
78 	}
79 
80 	accel_dev->hw_device = hw_data;
81 	adf_init_hw_data_4xxx(accel_dev->hw_device, ent->device);
82 
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);
85 
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;
96 	}
97 
98 	/* Create device configuration table */
99 	ret = adf_cfg_dev_add(accel_dev);
100 	if (ret)
101 		goto out_err;
102 
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;
108 	}
109 
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;
115 	}
116 
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;
121 	}
122 
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;
129 	}
130 
131 	/* Find and map all the device's BARS */
132 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM) & ADF_GEN4_BAR_MASK;
133 
134 	ret = pcim_iomap_regions_request_all(pdev, bar_mask, pci_name(pdev));
135 	if (ret) {
136 		dev_err(&pdev->dev, "Failed to map pci regions.\n");
137 		goto out_err;
138 	}
139 
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_table(pdev)[bar_nr];
144 	}
145 
146 	pci_set_master(pdev);
147 
148 	if (pci_save_state(pdev)) {
149 		dev_err(&pdev->dev, "Failed to save pci state.\n");
150 		ret = -ENOMEM;
151 		goto out_err;
152 	}
153 
154 	accel_dev->ras_errors.enabled = true;
155 	adf_dbgfs_init(accel_dev);
156 
157 	ret = adf_dev_up(accel_dev, true);
158 	if (ret)
159 		goto out_err_dev_stop;
160 
161 	ret = adf_sysfs_init(accel_dev);
162 	if (ret)
163 		goto out_err_dev_stop;
164 
165 	return ret;
166 
167 out_err_dev_stop:
168 	adf_dev_down(accel_dev, false);
169 out_err:
170 	adf_cleanup_accel(accel_dev);
171 	return ret;
172 }
173 
174 static void adf_remove(struct pci_dev *pdev)
175 {
176 	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
177 
178 	if (!accel_dev) {
179 		pr_err("QAT: Driver removal failed\n");
180 		return;
181 	}
182 	adf_dev_down(accel_dev, false);
183 	adf_cleanup_accel(accel_dev);
184 }
185 
186 static struct pci_driver adf_driver = {
187 	.id_table = adf_pci_tbl,
188 	.name = ADF_4XXX_DEVICE_NAME,
189 	.probe = adf_probe,
190 	.remove = adf_remove,
191 	.sriov_configure = adf_sriov_configure,
192 	.err_handler = &adf_err_handler,
193 };
194 
195 module_pci_driver(adf_driver);
196 
197 MODULE_LICENSE("Dual BSD/GPL");
198 MODULE_AUTHOR("Intel");
199 MODULE_FIRMWARE(ADF_4XXX_FW);
200 MODULE_FIRMWARE(ADF_402XX_FW);
201 MODULE_FIRMWARE(ADF_4XXX_MMP);
202 MODULE_FIRMWARE(ADF_402XX_MMP);
203 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
204 MODULE_VERSION(ADF_DRV_VERSION);
205 MODULE_SOFTDEP("pre: crypto-intel_qat");
206 MODULE_IMPORT_NS(CRYPTO_QAT);
207