xref: /freebsd/sys/dev/qat/qat_hw/qat_4xxxvf/adf_drv.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 #include "qat_freebsd.h"
4 #include <adf_accel_devices.h>
5 #include <adf_common_drv.h>
6 #include <adf_cfg.h>
7 #include "adf_4xxxvf_hw_data.h"
8 #include "adf_gen4_hw_data.h"
9 #include "adf_fw_counters.h"
10 #include "adf_cfg_device.h"
11 #include <sys/types.h>
12 #include <sys/kernel.h>
13 #include <sys/malloc.h>
14 #include <machine/bus_dma.h>
15 #include <dev/pci/pcireg.h>
16 
17 static MALLOC_DEFINE(M_QAT_4XXXVF, "qat_4xxxvf", "qat_4xxxvf");
18 
19 #define ADF_SYSTEM_DEVICE(device_id)                                           \
20 	{                                                                      \
21 		PCI_VENDOR_ID_INTEL, device_id                                 \
22 	}
23 
24 static const struct pci_device_id adf_pci_tbl[] =
25     { ADF_SYSTEM_DEVICE(ADF_4XXXIOV_PCI_DEVICE_ID),
26       ADF_SYSTEM_DEVICE(ADF_401XXIOV_PCI_DEVICE_ID),
27       {
28 	  0,
29       } };
30 
31 static int
32 adf_probe(device_t dev)
33 {
34 	const struct pci_device_id *id;
35 
36 	for (id = adf_pci_tbl; id->vendor != 0; id++) {
37 		if (pci_get_vendor(dev) == id->vendor &&
38 		    pci_get_device(dev) == id->device) {
39 			device_set_desc(dev,
40 					"Intel " ADF_4XXXVF_DEVICE_NAME
41 					" QuickAssist");
42 			return BUS_PROBE_GENERIC;
43 		}
44 	}
45 	return ENXIO;
46 }
47 
48 static void
49 adf_cleanup_accel(struct adf_accel_dev *accel_dev)
50 {
51 	struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;
52 	struct adf_accel_dev *pf;
53 	int i;
54 
55 	if (accel_dev->dma_tag)
56 		bus_dma_tag_destroy(accel_dev->dma_tag);
57 
58 	for (i = 0; i < ADF_PCI_MAX_BARS; i++) {
59 		struct adf_bar *bar = &accel_pci_dev->pci_bars[i];
60 
61 		if (bar->virt_addr)
62 			bus_free_resource(accel_pci_dev->pci_dev,
63 					  SYS_RES_MEMORY,
64 					  bar->virt_addr);
65 	}
66 
67 	/*
68 	 * As adf_clean_hw_data_4xxxiov() will update class index, before
69 	 * index is updated, vf must be remove from accel_table.
70 	 */
71 	pf = adf_devmgr_pci_to_accel_dev(pci_find_pf(accel_pci_dev->pci_dev));
72 	adf_devmgr_rm_dev(accel_dev, pf);
73 
74 	if (accel_dev->hw_device) {
75 		switch (pci_get_device(accel_pci_dev->pci_dev)) {
76 		case ADF_4XXXIOV_PCI_DEVICE_ID:
77 		case ADF_401XXIOV_PCI_DEVICE_ID:
78 			adf_clean_hw_data_4xxxiov(accel_dev->hw_device);
79 			break;
80 		default:
81 			break;
82 		}
83 		free(accel_dev->hw_device, M_QAT_4XXXVF);
84 		accel_dev->hw_device = NULL;
85 	}
86 	adf_cfg_dev_remove(accel_dev);
87 }
88 
89 static int
90 adf_attach(device_t dev)
91 {
92 	struct adf_accel_dev *accel_dev;
93 	struct adf_accel_dev *pf;
94 	struct adf_accel_pci *accel_pci_dev;
95 	struct adf_hw_device_data *hw_data;
96 	unsigned int i, bar_nr;
97 	int ret = 0;
98 	int rid;
99 	struct adf_cfg_device *cfg_dev = NULL;
100 
101 	accel_dev = device_get_softc(dev);
102 	accel_dev->is_vf = true;
103 	pf = adf_devmgr_pci_to_accel_dev(pci_find_pf(dev));
104 
105 	INIT_LIST_HEAD(&accel_dev->crypto_list);
106 	accel_pci_dev = &accel_dev->accel_pci_dev;
107 	accel_pci_dev->pci_dev = dev;
108 
109 	if (bus_get_domain(dev, &accel_pci_dev->node) != 0)
110 		accel_pci_dev->node = 0;
111 
112 	/* Add accel device to accel table */
113 	if (adf_devmgr_add_dev(accel_dev, pf)) {
114 		device_printf(GET_DEV(accel_dev),
115 			      "Failed to add new accelerator device.\n");
116 		return -EFAULT;
117 	}
118 	/* Allocate and configure device configuration structure */
119 	hw_data = malloc(sizeof(*hw_data), M_QAT_4XXXVF, M_WAITOK | M_ZERO);
120 	if (!hw_data) {
121 		ret = -ENOMEM;
122 		goto out_err;
123 	}
124 
125 	accel_dev->hw_device = hw_data;
126 	adf_init_hw_data_4xxxiov(accel_dev->hw_device);
127 	accel_pci_dev->revid = pci_get_revid(dev);
128 
129 	hw_data->fuses = pci_read_config(dev, ADF_4XXXIOV_VFFUSECTL4_OFFSET, 4);
130 
131 	/* Get Accelerators and Accelerators Engines masks */
132 	hw_data->accel_mask = hw_data->get_accel_mask(accel_dev);
133 	hw_data->ae_mask = hw_data->get_ae_mask(accel_dev);
134 	hw_data->admin_ae_mask = hw_data->ae_mask;
135 	accel_pci_dev->sku = hw_data->get_sku(hw_data);
136 
137 	/* Create device configuration table */
138 	ret = adf_cfg_dev_add(accel_dev);
139 	if (ret)
140 		goto out_err;
141 
142 	pci_set_max_read_req(dev, 1024);
143 
144 	ret = bus_dma_tag_create(bus_get_dma_tag(dev),
145 				 1,
146 				 0,
147 				 BUS_SPACE_MAXADDR,
148 				 BUS_SPACE_MAXADDR,
149 				 NULL,
150 				 NULL,
151 				 BUS_SPACE_MAXSIZE,
152 				 /* BUS_SPACE_UNRESTRICTED */ 1,
153 				 BUS_SPACE_MAXSIZE,
154 				 0,
155 				 NULL,
156 				 NULL,
157 				 &accel_dev->dma_tag);
158 
159 	hw_data->accel_capabilities_mask = adf_4xxxvf_get_hw_cap(accel_dev);
160 
161 	/* Find and map all the device's BARS */
162 	i = 0;
163 	for (bar_nr = 0; i < ADF_PCI_MAX_BARS && bar_nr < PCIR_MAX_BAR_0;
164 	     bar_nr++) {
165 		struct adf_bar *bar;
166 
167 		rid = PCIR_BAR(bar_nr);
168 		if (bus_get_resource(dev, SYS_RES_MEMORY, rid, NULL, NULL) !=
169 		    0) {
170 			continue;
171 		}
172 		bar = &accel_pci_dev->pci_bars[i++];
173 		bar->virt_addr = bus_alloc_resource_any(dev,
174 							SYS_RES_MEMORY,
175 							&rid,
176 							RF_ACTIVE);
177 		if (!bar->virt_addr) {
178 			device_printf(GET_DEV(accel_dev),
179 				      "Failed to map BAR %d\n",
180 				      bar_nr);
181 			ret = ENXIO;
182 			goto out_err;
183 		}
184 		bar->base_addr = rman_get_start(bar->virt_addr);
185 		bar->size = rman_get_size(bar->virt_addr);
186 	}
187 
188 	if (i == 0) {
189 		device_printf(
190 		    GET_DEV(accel_dev),
191 		    "No BARs mapped. Please check if PCI BARs are mapped correctly for device\n");
192 		ret = ENXIO;
193 		goto out_err;
194 	}
195 
196 	pci_enable_busmaster(dev);
197 
198 	/* Completion for VF2PF request/response message exchange */
199 	init_completion(&accel_dev->u1.vf.msg_received);
200 	mutex_init(&accel_dev->u1.vf.rpreset_lock);
201 
202 	ret = hw_data->config_device(accel_dev);
203 	if (ret)
204 		goto out_err;
205 
206 	ret = adf_dev_init(accel_dev);
207 	if (!ret)
208 		ret = adf_dev_start(accel_dev);
209 
210 	if (ret) {
211 		device_printf(
212 		    GET_DEV(accel_dev),
213 		    "Failed to start - make sure PF enabled services match VF configuration.\n");
214 		adf_dev_stop(accel_dev);
215 		adf_dev_shutdown(accel_dev);
216 		return 0;
217 	}
218 
219 	cfg_dev = accel_dev->cfg->dev;
220 	adf_cfg_device_clear(cfg_dev, accel_dev);
221 	free(cfg_dev, M_QAT);
222 	accel_dev->cfg->dev = NULL;
223 
224 	return ret;
225 
226 out_err:
227 	adf_cleanup_accel(accel_dev);
228 	return ret;
229 }
230 
231 static int
232 adf_detach(device_t dev)
233 {
234 	struct adf_accel_dev *accel_dev = device_get_softc(dev);
235 
236 	if (!accel_dev) {
237 		printf("QAT: Driver removal failed\n");
238 		return EFAULT;
239 	}
240 
241 	adf_flush_vf_wq(accel_dev);
242 	clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
243 	adf_dev_stop(accel_dev);
244 	adf_dev_shutdown(accel_dev);
245 	adf_cleanup_accel(accel_dev);
246 	return 0;
247 }
248 
249 static int
250 adf_modevent(module_t mod, int type, void *data)
251 {
252 
253 	switch (type) {
254 	case MOD_UNLOAD:
255 		adf_clean_vf_map(true);
256 		return 0;
257 	default:
258 		return EOPNOTSUPP;
259 	}
260 }
261 
262 static device_method_t adf_methods[] = { DEVMETHOD(device_probe, adf_probe),
263 					 DEVMETHOD(device_attach, adf_attach),
264 					 DEVMETHOD(device_detach, adf_detach),
265 
266 					 DEVMETHOD_END };
267 
268 static driver_t adf_driver = { "qat",
269 			       adf_methods,
270 			       sizeof(struct adf_accel_dev) };
271 
272 DRIVER_MODULE_ORDERED(qat_4xxxvf,
273 		      pci,
274 		      adf_driver,
275 		      adf_modevent,
276 		      NULL,
277 		      SI_ORDER_THIRD);
278 MODULE_VERSION(qat_4xxxvf, 1);
279 MODULE_DEPEND(qat_4xxxvf, qat_common, 1, 1, 1);
280 MODULE_DEPEND(qat_4xxxvf, qat_api, 1, 1, 1);
281 MODULE_DEPEND(qat_4xxxvf, linuxkpi, 1, 1, 1);
282