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_dbgfs.h>
20 #include "adf_dh895xccvf_hw_data.h"
21 
22 static const struct pci_device_id adf_pci_tbl[] = {
23 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF), },
24 	{ }
25 };
26 MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
27 
28 static int adf_probe(struct pci_dev *dev, const struct pci_device_id *ent);
29 static void adf_remove(struct pci_dev *dev);
30 
31 static struct pci_driver adf_driver = {
32 	.id_table = adf_pci_tbl,
33 	.name = ADF_DH895XCCVF_DEVICE_NAME,
34 	.probe = adf_probe,
35 	.remove = adf_remove,
36 };
37 
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 
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 	struct adf_accel_dev *pf;
48 	int i;
49 
50 	for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
51 		struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
52 
53 		if (bar->virt_addr)
54 			pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);
55 	}
56 
57 	if (accel_dev->hw_device) {
58 		switch (accel_pci_dev->pci_dev->device) {
59 		case PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF:
60 			adf_clean_hw_data_dh895xcciov(accel_dev->hw_device);
61 			break;
62 		default:
63 			break;
64 		}
65 		kfree(accel_dev->hw_device);
66 		accel_dev->hw_device = NULL;
67 	}
68 	adf_dbgfs_exit(accel_dev);
69 	adf_cfg_dev_remove(accel_dev);
70 	pf = adf_devmgr_pci_to_accel_dev(accel_pci_dev->pci_dev->physfn);
71 	adf_devmgr_rm_dev(accel_dev, pf);
72 }
73 
74 static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
75 {
76 	struct adf_accel_dev *accel_dev;
77 	struct adf_accel_dev *pf;
78 	struct adf_accel_pci *accel_pci_dev;
79 	struct adf_hw_device_data *hw_data;
80 	unsigned int i, bar_nr;
81 	unsigned long bar_mask;
82 	int ret;
83 
84 	switch (ent->device) {
85 	case PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF:
86 		break;
87 	default:
88 		dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);
89 		return -ENODEV;
90 	}
91 
92 	accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,
93 				 dev_to_node(&pdev->dev));
94 	if (!accel_dev)
95 		return -ENOMEM;
96 
97 	accel_dev->is_vf = true;
98 	pf = adf_devmgr_pci_to_accel_dev(pdev->physfn);
99 	accel_pci_dev = &accel_dev->accel_pci_dev;
100 	accel_pci_dev->pci_dev = pdev;
101 
102 	/* Add accel device to accel table */
103 	if (adf_devmgr_add_dev(accel_dev, pf)) {
104 		dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
105 		kfree(accel_dev);
106 		return -EFAULT;
107 	}
108 	INIT_LIST_HEAD(&accel_dev->crypto_list);
109 
110 	accel_dev->owner = THIS_MODULE;
111 	/* Allocate and configure device configuration structure */
112 	hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,
113 			       dev_to_node(&pdev->dev));
114 	if (!hw_data) {
115 		ret = -ENOMEM;
116 		goto out_err;
117 	}
118 	accel_dev->hw_device = hw_data;
119 	adf_init_hw_data_dh895xcciov(accel_dev->hw_device);
120 
121 	/* Get Accelerators and Accelerators Engines masks */
122 	hw_data->accel_mask = hw_data->get_accel_mask(hw_data);
123 	hw_data->ae_mask = hw_data->get_ae_mask(hw_data);
124 	accel_pci_dev->sku = hw_data->get_sku(hw_data);
125 
126 	/* Create device configuration table */
127 	ret = adf_cfg_dev_add(accel_dev);
128 	if (ret)
129 		goto out_err;
130 
131 	/* enable PCI device */
132 	if (pci_enable_device(pdev)) {
133 		ret = -EFAULT;
134 		goto out_err;
135 	}
136 
137 	/* set dma identifier */
138 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
139 	if (ret) {
140 		dev_err(&pdev->dev, "No usable DMA configuration\n");
141 		goto out_err_disable;
142 	}
143 
144 	if (pci_request_regions(pdev, ADF_DH895XCCVF_DEVICE_NAME)) {
145 		ret = -EFAULT;
146 		goto out_err_disable;
147 	}
148 
149 	/* Find and map all the device's BARS */
150 	i = 0;
151 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
152 	for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {
153 		struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];
154 
155 		bar->base_addr = pci_resource_start(pdev, bar_nr);
156 		if (!bar->base_addr)
157 			break;
158 		bar->size = pci_resource_len(pdev, bar_nr);
159 		bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);
160 		if (!bar->virt_addr) {
161 			dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);
162 			ret = -EFAULT;
163 			goto out_err_free_reg;
164 		}
165 	}
166 	pci_set_master(pdev);
167 	/* Completion for VF2PF request/response message exchange */
168 	init_completion(&accel_dev->vf.msg_received);
169 
170 	adf_dbgfs_init(accel_dev);
171 
172 	ret = adf_dev_up(accel_dev, false);
173 	if (ret)
174 		goto out_err_dev_stop;
175 
176 	return ret;
177 
178 out_err_dev_stop:
179 	adf_dev_down(accel_dev, false);
180 out_err_free_reg:
181 	pci_release_regions(accel_pci_dev->pci_dev);
182 out_err_disable:
183 	pci_disable_device(accel_pci_dev->pci_dev);
184 out_err:
185 	adf_cleanup_accel(accel_dev);
186 	kfree(accel_dev);
187 	return ret;
188 }
189 
190 static void adf_remove(struct pci_dev *pdev)
191 {
192 	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
193 
194 	if (!accel_dev) {
195 		pr_err("QAT: Driver removal failed\n");
196 		return;
197 	}
198 	adf_flush_vf_wq(accel_dev);
199 	adf_dev_down(accel_dev, false);
200 	adf_cleanup_accel(accel_dev);
201 	adf_cleanup_pci_dev(accel_dev);
202 	kfree(accel_dev);
203 }
204 
205 static int __init adfdrv_init(void)
206 {
207 	request_module("intel_qat");
208 
209 	if (pci_register_driver(&adf_driver)) {
210 		pr_err("QAT: Driver initialization failed\n");
211 		return -EFAULT;
212 	}
213 	return 0;
214 }
215 
216 static void __exit adfdrv_release(void)
217 {
218 	pci_unregister_driver(&adf_driver);
219 	adf_clean_vf_map(true);
220 }
221 
222 module_init(adfdrv_init);
223 module_exit(adfdrv_release);
224 
225 MODULE_LICENSE("Dual BSD/GPL");
226 MODULE_AUTHOR("Intel");
227 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
228 MODULE_VERSION(ADF_DRV_VERSION);
229