1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/pci.h>
50#include <linux/init.h>
51#include <linux/types.h>
52#include <linux/fs.h>
53#include <linux/slab.h>
54#include <linux/errno.h>
55#include <linux/device.h>
56#include <linux/dma-mapping.h>
57#include <linux/platform_device.h>
58#include <linux/workqueue.h>
59#include <linux/io.h>
60#include <adf_accel_devices.h>
61#include <adf_common_drv.h>
62#include <adf_cfg.h>
63#include "adf_c3xxx_hw_data.h"
64
65#define ADF_SYSTEM_DEVICE(device_id) \
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
67
68static const struct pci_device_id adf_pci_tbl[] = {
69 ADF_SYSTEM_DEVICE(ADF_C3XXX_PCI_DEVICE_ID),
70 {0,}
71};
72MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
73
74static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
75static void adf_remove(struct pci_dev *dev);
76
77static struct pci_driver adf_driver = {
78 .id_table = adf_pci_tbl,
79 .name = ADF_C3XXX_DEVICE_NAME,
80 .probe = adf_probe,
81 .remove = adf_remove,
82 .sriov_configure = adf_sriov_configure,
83};
84
85static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)
86{
87 pci_release_regions(accel_dev->accel_pci_dev.pci_dev);
88 pci_disable_device(accel_dev->accel_pci_dev.pci_dev);
89}
90
91static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
92{
93 struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
94 int i;
95
96 for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
97 struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
98
99 if (bar->virt_addr)
100 pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
101 }
102
103 if (accel_dev->hw_device) {
104 switch (accel_pci_dev->pci_dev->device) {
105 case ADF_C3XXX_PCI_DEVICE_ID:
106 adf_clean_hw_data_c3xxx(accel_dev->hw_device);
107 break;
108 default:
109 break;
110 }
111 kfree(accel_dev->hw_device);
112 accel_dev->hw_device = NULL;
113 }
114 adf_cfg_dev_remove(accel_dev);
115 debugfs_remove(accel_dev->debugfs_dir);
116 adf_devmgr_rm_dev(accel_dev, NULL);
117}
118
119static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
120{
121 struct adf_accel_dev *accel_dev;
122 struct adf_accel_pci *accel_pci_dev;
123 struct adf_hw_device_data *hw_data;
124 char name[ADF_DEVICE_NAME_LENGTH];
125 unsigned int i, bar_nr;
126 int ret, bar_mask;
127
128 switch (ent->device) {
129 case ADF_C3XXX_PCI_DEVICE_ID:
130 break;
131 default:
132 dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);
133 return -ENODEV;
134 }
135
136 if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {
137
138
139
140 dev_err(&pdev->dev, "Invalid NUMA configuration.\n");
141 return -EINVAL;
142 }
143
144 accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
145 dev_to_node(&pdev->dev));
146 if (!accel_dev)
147 return -ENOMEM;
148
149 INIT_LIST_HEAD(&accel_dev->crypto_list);
150 accel_pci_dev = &accel_dev->accel_pci_dev;
151 accel_pci_dev->pci_dev = pdev;
152
153
154
155 if (adf_devmgr_add_dev(accel_dev, NULL)) {
156 dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
157 kfree(accel_dev);
158 return -EFAULT;
159 }
160
161 accel_dev->owner = THIS_MODULE;
162
163 hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,
164 dev_to_node(&pdev->dev));
165 if (!hw_data) {
166 ret = -ENOMEM;
167 goto out_err;
168 }
169
170 accel_dev->hw_device = hw_data;
171 adf_init_hw_data_c3xxx(accel_dev->hw_device);
172 pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);
173 pci_read_config_dword(pdev, ADF_DEVICE_FUSECTL_OFFSET,
174 &hw_data->fuses);
175
176
177 hw_data->accel_mask = hw_data->get_accel_mask(hw_data->fuses);
178 hw_data->ae_mask = hw_data->get_ae_mask(hw_data->fuses);
179 accel_pci_dev->sku = hw_data->get_sku(hw_data);
180
181 if (!hw_data->accel_mask || !hw_data->ae_mask ||
182 ((~hw_data->ae_mask) & 0x01)) {
183 dev_err(&pdev->dev, "No acceleration units found");
184 ret = -EFAULT;
185 goto out_err;
186 }
187
188
189 snprintf(name, sizeof(name), "%s%s_%02x:%02d.%02d",
190 ADF_DEVICE_NAME_PREFIX, hw_data->dev_class->name,
191 pdev->bus->number, PCI_SLOT(pdev->devfn),
192 PCI_FUNC(pdev->devfn));
193
194 accel_dev->debugfs_dir = debugfs_create_dir(name, NULL);
195 if (!accel_dev->debugfs_dir) {
196 dev_err(&pdev->dev, "Could not create debugfs dir %s\n", name);
197 ret = -EINVAL;
198 goto out_err;
199 }
200
201
202 ret = adf_cfg_dev_add(accel_dev);
203 if (ret)
204 goto out_err;
205
206
207 if (pci_enable_device(pdev)) {
208 ret = -EFAULT;
209 goto out_err;
210 }
211
212
213 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
214 if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
215 dev_err(&pdev->dev, "No usable DMA configuration\n");
216 ret = -EFAULT;
217 goto out_err_disable;
218 } else {
219 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
220 }
221
222 } else {
223 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
224 }
225
226 if (pci_request_regions(pdev, ADF_C3XXX_DEVICE_NAME)) {
227 ret = -EFAULT;
228 goto out_err_disable;
229 }
230
231
232 pci_read_config_dword(pdev, ADF_DEVICE_LEGFUSE_OFFSET,
233 &hw_data->accel_capabilities_mask);
234
235
236 i = 0;
237 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
238 for_each_set_bit(bar_nr, (const unsigned long *)&bar_mask,
239 ADF_PCI_MAX_BARS * 2) {
240 struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
241
242 bar->base_addr = pci_resource_start(pdev, bar_nr);
243 if (!bar->base_addr)
244 break;
245 bar->size = pci_resource_len(pdev, bar_nr);
246 bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
247 if (!bar->virt_addr) {
248 dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
249 ret = -EFAULT;
250 goto out_err_free_reg;
251 }
252 }
253 pci_set_master(pdev);
254
255 if (adf_enable_aer(accel_dev, &adf_driver)) {
256 dev_err(&pdev->dev, "Failed to enable aer\n");
257 ret = -EFAULT;
258 goto out_err_free_reg;
259 }
260
261 if (pci_save_state(pdev)) {
262 dev_err(&pdev->dev, "Failed to save pci state\n");
263 ret = -ENOMEM;
264 goto out_err_free_reg;
265 }
266
267 ret = qat_crypto_dev_config(accel_dev);
268 if (ret)
269 goto out_err_free_reg;
270
271 ret = adf_dev_init(accel_dev);
272 if (ret)
273 goto out_err_dev_shutdown;
274
275 ret = adf_dev_start(accel_dev);
276 if (ret)
277 goto out_err_dev_stop;
278
279 return ret;
280
281out_err_dev_stop:
282 adf_dev_stop(accel_dev);
283out_err_dev_shutdown:
284 adf_dev_shutdown(accel_dev);
285out_err_free_reg:
286 pci_release_regions(accel_pci_dev->pci_dev);
287out_err_disable:
288 pci_disable_device(accel_pci_dev->pci_dev);
289out_err:
290 adf_cleanup_accel(accel_dev);
291 kfree(accel_dev);
292 return ret;
293}
294
295static void adf_remove(struct pci_dev *pdev)
296{
297 struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
298
299 if (!accel_dev) {
300 pr_err("QAT: Driver removal failed\n");
301 return;
302 }
303 if (adf_dev_stop(accel_dev))
304 dev_err(&GET_DEV(accel_dev), "Failed to stop QAT accel dev\n");
305
306 adf_dev_shutdown(accel_dev);
307 adf_disable_aer(accel_dev);
308 adf_cleanup_accel(accel_dev);
309 adf_cleanup_pci_dev(accel_dev);
310 kfree(accel_dev);
311}
312
313static int __init adfdrv_init(void)
314{
315 request_module("intel_qat");
316
317 if (pci_register_driver(&adf_driver)) {
318 pr_err("QAT: Driver initialization failed\n");
319 return -EFAULT;
320 }
321 return 0;
322}
323
324static void __exit adfdrv_release(void)
325{
326 pci_unregister_driver(&adf_driver);
327}
328
329module_init(adfdrv_init);
330module_exit(adfdrv_release);
331
332MODULE_LICENSE("Dual BSD/GPL");
333MODULE_AUTHOR("Intel");
334MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
335MODULE_VERSION(ADF_DRV_VERSION);
336