• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_c3xxx_hw_data.h"
20 
21 static const struct pci_device_id adf_pci_tbl[] = {
22 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_C3XXX), },
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_C3XXX_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_C3XXX:
59 			adf_clean_hw_data_c3xxx(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_C3XXX:
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_c3xxx(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 
130 	/* Get Accelerators and Accelerators Engines masks */
131 	hw_data->accel_mask = hw_data->get_accel_mask(hw_data->fuses);
132 	hw_data->ae_mask = hw_data->get_ae_mask(hw_data->fuses);
133 	accel_pci_dev->sku = hw_data->get_sku(hw_data);
134 	/* If the device has no acceleration engines then ignore it. */
135 	if (!hw_data->accel_mask || !hw_data->ae_mask ||
136 	    ((~hw_data->ae_mask) & 0x01)) {
137 		dev_err(&pdev->dev, "No acceleration units found");
138 		ret = -EFAULT;
139 		goto out_err;
140 	}
141 
142 	/* Create dev top level debugfs entry */
143 	snprintf(name, sizeof(name), "%s%s_%s", ADF_DEVICE_NAME_PREFIX,
144 		 hw_data->dev_class->name, pci_name(pdev));
145 
146 	accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
147 
148 	/* Create device configuration table */
149 	ret = adf_cfg_dev_add(accel_dev);
150 	if (ret)
151 		goto out_err;
152 
153 	/* enable PCI device */
154 	if (pci_enable_device(pdev)) {
155 		ret = -EFAULT;
156 		goto out_err;
157 	}
158 
159 	/* set dma identifier */
160 	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
161 		if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
162 			dev_err(&pdev->dev, "No usable DMA configuration\n");
163 			ret = -EFAULT;
164 			goto out_err_disable;
165 		} else {
166 			pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
167 		}
168 
169 	} else {
170 		pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
171 	}
172 
173 	if (pci_request_regions(pdev, ADF_C3XXX_DEVICE_NAME)) {
174 		ret = -EFAULT;
175 		goto out_err_disable;
176 	}
177 
178 	/* Read accelerator capabilities mask */
179 	pci_read_config_dword(pdev, ADF_DEVICE_LEGFUSE_OFFSET,
180 			      &hw_data->accel_capabilities_mask);
181 
182 	/* Find and map all the device's BARS */
183 	i = 0;
184 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
185 	for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {
186 		struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
187 
188 		bar->base_addr = pci_resource_start(pdev, bar_nr);
189 		if (!bar->base_addr)
190 			break;
191 		bar->size = pci_resource_len(pdev, bar_nr);
192 		bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
193 		if (!bar->virt_addr) {
194 			dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
195 			ret = -EFAULT;
196 			goto out_err_free_reg;
197 		}
198 	}
199 	pci_set_master(pdev);
200 
201 	if (adf_enable_aer(accel_dev)) {
202 		dev_err(&pdev->dev, "Failed to enable aer\n");
203 		ret = -EFAULT;
204 		goto out_err_free_reg;
205 	}
206 
207 	if (pci_save_state(pdev)) {
208 		dev_err(&pdev->dev, "Failed to save pci state\n");
209 		ret = -ENOMEM;
210 		goto out_err_free_reg;
211 	}
212 
213 	ret = qat_crypto_dev_config(accel_dev);
214 	if (ret)
215 		goto out_err_free_reg;
216 
217 	ret = adf_dev_init(accel_dev);
218 	if (ret)
219 		goto out_err_dev_shutdown;
220 
221 	ret = adf_dev_start(accel_dev);
222 	if (ret)
223 		goto out_err_dev_stop;
224 
225 	return ret;
226 
227 out_err_dev_stop:
228 	adf_dev_stop(accel_dev);
229 out_err_dev_shutdown:
230 	adf_dev_shutdown(accel_dev);
231 out_err_free_reg:
232 	pci_release_regions(accel_pci_dev->pci_dev);
233 out_err_disable:
234 	pci_disable_device(accel_pci_dev->pci_dev);
235 out_err:
236 	adf_cleanup_accel(accel_dev);
237 	kfree(accel_dev);
238 	return ret;
239 }
240 
adf_remove(struct pci_dev * pdev)241 static void adf_remove(struct pci_dev *pdev)
242 {
243 	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
244 
245 	if (!accel_dev) {
246 		pr_err("QAT: Driver removal failed\n");
247 		return;
248 	}
249 	adf_dev_stop(accel_dev);
250 	adf_dev_shutdown(accel_dev);
251 	adf_disable_aer(accel_dev);
252 	adf_cleanup_accel(accel_dev);
253 	adf_cleanup_pci_dev(accel_dev);
254 	kfree(accel_dev);
255 }
256 
adfdrv_init(void)257 static int __init adfdrv_init(void)
258 {
259 	request_module("intel_qat");
260 
261 	if (pci_register_driver(&adf_driver)) {
262 		pr_err("QAT: Driver initialization failed\n");
263 		return -EFAULT;
264 	}
265 	return 0;
266 }
267 
adfdrv_release(void)268 static void __exit adfdrv_release(void)
269 {
270 	pci_unregister_driver(&adf_driver);
271 }
272 
273 module_init(adfdrv_init);
274 module_exit(adfdrv_release);
275 
276 MODULE_LICENSE("Dual BSD/GPL");
277 MODULE_AUTHOR("Intel");
278 MODULE_FIRMWARE(ADF_C3XXX_FW);
279 MODULE_FIRMWARE(ADF_C3XXX_MMP);
280 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
281 MODULE_VERSION(ADF_DRV_VERSION);
282