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"
20
21 static const struct pci_device_id adf_pci_tbl[] = {
22 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_C62X), },
23 { }
24 };
25 MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
26
27 static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
28 static void adf_remove(struct pci_dev *dev);
29
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,
36 };
37
adf_cleanup_pci_dev(struct adf_accel_dev * accel_dev)38 static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)
39 {
40 pci_release_regions(accel_dev->accel_pci_dev.pci_dev);
41 pci_disable_device(accel_dev->accel_pci_dev.pci_dev);
42 }
43
adf_cleanup_accel(struct adf_accel_dev * accel_dev)44 static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
45 {
46 struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
47 int i;
48
49 for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
50 struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
51
52 if (bar->virt_addr)
53 pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
54 }
55
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;
63 }
64 kfree(accel_dev->hw_device);
65 accel_dev->hw_device = NULL;
66 }
67 adf_cfg_dev_remove(accel_dev);
68 debugfs_remove(accel_dev->debugfs_dir);
69 adf_devmgr_rm_dev(accel_dev, NULL);
70 }
71
adf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)72 static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
73 {
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;
81
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;
88 }
89
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;
96 }
97
98 accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
99 dev_to_node(&pdev->dev));
100 if (!accel_dev)
101 return -ENOMEM;
102
103 INIT_LIST_HEAD(&accel_dev->crypto_list);
104 accel_pci_dev = &accel_dev->accel_pci_dev;
105 accel_pci_dev->pci_dev = pdev;
106
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;
113 }
114
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;
122 }
123
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);
131
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;
142 }
143
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));
147
148 accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
149
150 /* Create device configuration table */
151 ret = adf_cfg_dev_add(accel_dev);
152 if (ret)
153 goto out_err;
154
155 /* enable PCI device */
156 if (pci_enable_device(pdev)) {
157 ret = -EFAULT;
158 goto out_err;
159 }
160
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));
169 }
170
171 } else {
172 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
173 }
174
175 if (pci_request_regions(pdev, ADF_C62X_DEVICE_NAME)) {
176 ret = -EFAULT;
177 goto out_err_disable;
178 }
179
180 /* Read accelerator capabilities mask */
181 pci_read_config_dword(pdev, ADF_DEVICE_LEGFUSE_OFFSET,
182 &hw_data->accel_capabilities_mask);
183
184 /* Find and map all the device's BARS */
185 i = (hw_data->fuses & ADF_DEVICE_FUSECTL_MASK) ? 1 : 0;
186 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
187 for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {
188 struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
189
190 bar->base_addr = pci_resource_start(pdev, bar_nr);
191 if (!bar->base_addr)
192 break;
193 bar->size = pci_resource_len(pdev, bar_nr);
194 bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
195 if (!bar->virt_addr) {
196 dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
197 ret = -EFAULT;
198 goto out_err_free_reg;
199 }
200 }
201 pci_set_master(pdev);
202
203 if (adf_enable_aer(accel_dev)) {
204 dev_err(&pdev->dev, "Failed to enable aer\n");
205 ret = -EFAULT;
206 goto out_err_free_reg;
207 }
208
209 if (pci_save_state(pdev)) {
210 dev_err(&pdev->dev, "Failed to save pci state\n");
211 ret = -ENOMEM;
212 goto out_err_free_reg;
213 }
214
215 ret = qat_crypto_dev_config(accel_dev);
216 if (ret)
217 goto out_err_free_reg;
218
219 ret = adf_dev_init(accel_dev);
220 if (ret)
221 goto out_err_dev_shutdown;
222
223 ret = adf_dev_start(accel_dev);
224 if (ret)
225 goto out_err_dev_stop;
226
227 return ret;
228
229 out_err_dev_stop:
230 adf_dev_stop(accel_dev);
231 out_err_dev_shutdown:
232 adf_dev_shutdown(accel_dev);
233 out_err_free_reg:
234 pci_release_regions(accel_pci_dev->pci_dev);
235 out_err_disable:
236 pci_disable_device(accel_pci_dev->pci_dev);
237 out_err:
238 adf_cleanup_accel(accel_dev);
239 kfree(accel_dev);
240 return ret;
241 }
242
adf_remove(struct pci_dev * pdev)243 static void adf_remove(struct pci_dev *pdev)
244 {
245 struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
246
247 if (!accel_dev) {
248 pr_err("QAT: Driver removal failed\n");
249 return;
250 }
251 adf_dev_stop(accel_dev);
252 adf_dev_shutdown(accel_dev);
253 adf_disable_aer(accel_dev);
254 adf_cleanup_accel(accel_dev);
255 adf_cleanup_pci_dev(accel_dev);
256 kfree(accel_dev);
257 }
258
adfdrv_init(void)259 static int __init adfdrv_init(void)
260 {
261 request_module("intel_qat");
262
263 if (pci_register_driver(&adf_driver)) {
264 pr_err("QAT: Driver initialization failed\n");
265 return -EFAULT;
266 }
267 return 0;
268 }
269
adfdrv_release(void)270 static void __exit adfdrv_release(void)
271 {
272 pci_unregister_driver(&adf_driver);
273 }
274
275 module_init(adfdrv_init);
276 module_exit(adfdrv_release);
277
278 MODULE_LICENSE("Dual BSD/GPL");
279 MODULE_AUTHOR("Intel");
280 MODULE_FIRMWARE(ADF_C62X_FW);
281 MODULE_FIRMWARE(ADF_C62X_MMP);
282 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
283 MODULE_VERSION(ADF_DRV_VERSION);
284