xref: /freebsd/sys/dev/qat/qat_hw/qat_4xxxvf/adf_drv.c (revision e0c4386e)
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 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 	/* Logical BARs configuration for 64bit BARs:
163 	     bar 0 and 1 - logical BAR0
164 	     bar 2 and 3 - logical BAR1
165 	     bar 4 and 5 - logical BAR3
166 	*/
167 	for (bar_nr = 0;
168 	     bar_nr < (ADF_PCI_MAX_BARS * 2) && bar_nr < PCIR_MAX_BAR_0;
169 	     bar_nr += 2) {
170 		struct adf_bar *bar;
171 
172 		rid = PCIR_BAR(bar_nr);
173 		bar = &accel_pci_dev->pci_bars[bar_nr / 2];
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(dev, "Failed to map BAR %d\n", bar_nr);
180 			ret = ENXIO;
181 			goto out_err;
182 		}
183 		bar->base_addr = rman_get_start(bar->virt_addr);
184 		bar->size = rman_get_size(bar->virt_addr);
185 	}
186 	pci_enable_busmaster(dev);
187 
188 	/* Completion for VF2PF request/response message exchange */
189 	init_completion(&accel_dev->u1.vf.msg_received);
190 	mutex_init(&accel_dev->u1.vf.rpreset_lock);
191 
192 	ret = hw_data->config_device(accel_dev);
193 	if (ret)
194 		goto out_err;
195 
196 	ret = adf_dev_init(accel_dev);
197 	if (!ret)
198 		ret = adf_dev_start(accel_dev);
199 
200 	if (ret) {
201 		device_printf(
202 		    GET_DEV(accel_dev),
203 		    "Failed to start - make sure PF enabled services match VF configuration.\n");
204 		adf_dev_stop(accel_dev);
205 		adf_dev_shutdown(accel_dev);
206 		return 0;
207 	}
208 
209 	cfg_dev = accel_dev->cfg->dev;
210 	adf_cfg_device_clear(cfg_dev, accel_dev);
211 	free(cfg_dev, M_QAT);
212 	accel_dev->cfg->dev = NULL;
213 
214 	return ret;
215 
216 out_err:
217 	adf_cleanup_accel(accel_dev);
218 	return ret;
219 }
220 
221 static int
222 adf_detach(device_t dev)
223 {
224 	struct adf_accel_dev *accel_dev = device_get_softc(dev);
225 
226 	if (!accel_dev) {
227 		printf("QAT: Driver removal failed\n");
228 		return EFAULT;
229 	}
230 
231 	adf_flush_vf_wq(accel_dev);
232 	clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
233 	adf_dev_stop(accel_dev);
234 	adf_dev_shutdown(accel_dev);
235 	adf_cleanup_accel(accel_dev);
236 	return 0;
237 }
238 
239 static int
240 adf_modevent(module_t mod, int type, void *data)
241 {
242 
243 	switch (type) {
244 	case MOD_UNLOAD:
245 		adf_clean_vf_map(true);
246 		return 0;
247 	default:
248 		return EOPNOTSUPP;
249 	}
250 }
251 
252 static device_method_t adf_methods[] = { DEVMETHOD(device_probe, adf_probe),
253 					 DEVMETHOD(device_attach, adf_attach),
254 					 DEVMETHOD(device_detach, adf_detach),
255 
256 					 DEVMETHOD_END };
257 
258 static driver_t adf_driver = { "qat",
259 			       adf_methods,
260 			       sizeof(struct adf_accel_dev) };
261 
262 DRIVER_MODULE_ORDERED(qat_4xxxvf,
263 		      pci,
264 		      adf_driver,
265 		      adf_modevent,
266 		      NULL,
267 		      SI_ORDER_THIRD);
268 MODULE_VERSION(qat_4xxxvf, 1);
269 MODULE_DEPEND(qat_4xxxvf, qat_common, 1, 1, 1);
270 MODULE_DEPEND(qat_4xxxvf, qat_api, 1, 1, 1);
271 MODULE_DEPEND(qat_4xxxvf, linuxkpi, 1, 1, 1);
272