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