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
adf_probe(device_t dev)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
adf_cleanup_accel(struct adf_accel_dev * accel_dev)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
adf_attach(device_t dev)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 accel_dev->hw_device = hw_data;
121 adf_init_hw_data_4xxxiov(accel_dev->hw_device);
122 accel_pci_dev->revid = pci_get_revid(dev);
123
124 hw_data->fuses = pci_read_config(dev, ADF_4XXXIOV_VFFUSECTL4_OFFSET, 4);
125
126 /* Get Accelerators and Accelerators Engines masks */
127 hw_data->accel_mask = hw_data->get_accel_mask(accel_dev);
128 hw_data->ae_mask = hw_data->get_ae_mask(accel_dev);
129 hw_data->admin_ae_mask = hw_data->ae_mask;
130 accel_pci_dev->sku = hw_data->get_sku(hw_data);
131
132 /* Create device configuration table */
133 ret = adf_cfg_dev_add(accel_dev);
134 if (ret)
135 goto out_err;
136
137 pci_set_max_read_req(dev, 1024);
138
139 ret = bus_dma_tag_create(bus_get_dma_tag(dev),
140 1,
141 0,
142 BUS_SPACE_MAXADDR,
143 BUS_SPACE_MAXADDR,
144 NULL,
145 NULL,
146 BUS_SPACE_MAXSIZE,
147 /* BUS_SPACE_UNRESTRICTED */ 1,
148 BUS_SPACE_MAXSIZE,
149 0,
150 NULL,
151 NULL,
152 &accel_dev->dma_tag);
153
154 hw_data->accel_capabilities_mask = adf_4xxxvf_get_hw_cap(accel_dev);
155
156 /* Find and map all the device's BARS */
157 /* Logical BARs configuration for 64bit BARs:
158 bar 0 and 1 - logical BAR0
159 bar 2 and 3 - logical BAR1
160 bar 4 and 5 - logical BAR3
161 */
162 for (bar_nr = 0;
163 bar_nr < (ADF_PCI_MAX_BARS * 2) && bar_nr < PCIR_MAX_BAR_0;
164 bar_nr += 2) {
165 struct adf_bar *bar;
166
167 rid = PCIR_BAR(bar_nr);
168 bar = &accel_pci_dev->pci_bars[bar_nr / 2];
169 bar->virt_addr = bus_alloc_resource_any(dev,
170 SYS_RES_MEMORY,
171 &rid,
172 RF_ACTIVE);
173 if (!bar->virt_addr) {
174 device_printf(dev, "Failed to map BAR %d\n", bar_nr);
175 ret = ENXIO;
176 goto out_err;
177 }
178 bar->base_addr = rman_get_start(bar->virt_addr);
179 bar->size = rman_get_size(bar->virt_addr);
180 }
181 pci_enable_busmaster(dev);
182
183 /* Completion for VF2PF request/response message exchange */
184 init_completion(&accel_dev->u1.vf.msg_received);
185 mutex_init(&accel_dev->u1.vf.rpreset_lock);
186
187 ret = hw_data->config_device(accel_dev);
188 if (ret)
189 goto out_err;
190
191 ret = adf_dev_init(accel_dev);
192 if (!ret)
193 ret = adf_dev_start(accel_dev);
194
195 if (ret) {
196 device_printf(
197 GET_DEV(accel_dev),
198 "Failed to start - make sure PF enabled services match VF configuration.\n");
199 adf_dev_stop(accel_dev);
200 adf_dev_shutdown(accel_dev);
201 return 0;
202 }
203
204 cfg_dev = accel_dev->cfg->dev;
205 adf_cfg_device_clear(cfg_dev, accel_dev);
206 free(cfg_dev, M_QAT);
207 accel_dev->cfg->dev = NULL;
208
209 return ret;
210
211 out_err:
212 adf_cleanup_accel(accel_dev);
213 return ret;
214 }
215
216 static int
adf_detach(device_t dev)217 adf_detach(device_t dev)
218 {
219 struct adf_accel_dev *accel_dev = device_get_softc(dev);
220
221 if (!accel_dev) {
222 printf("QAT: Driver removal failed\n");
223 return EFAULT;
224 }
225
226 adf_flush_vf_wq(accel_dev);
227 clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
228 adf_dev_stop(accel_dev);
229 adf_dev_shutdown(accel_dev);
230 adf_cleanup_accel(accel_dev);
231 return 0;
232 }
233
234 static int
adf_modevent(module_t mod,int type,void * data)235 adf_modevent(module_t mod, int type, void *data)
236 {
237
238 switch (type) {
239 case MOD_UNLOAD:
240 adf_clean_vf_map(true);
241 return 0;
242 default:
243 return EOPNOTSUPP;
244 }
245 }
246
247 static device_method_t adf_methods[] = { DEVMETHOD(device_probe, adf_probe),
248 DEVMETHOD(device_attach, adf_attach),
249 DEVMETHOD(device_detach, adf_detach),
250
251 DEVMETHOD_END };
252
253 static driver_t adf_driver = { "qat",
254 adf_methods,
255 sizeof(struct adf_accel_dev) };
256
257 DRIVER_MODULE_ORDERED(qat_4xxxvf,
258 pci,
259 adf_driver,
260 adf_modevent,
261 NULL,
262 SI_ORDER_THIRD);
263 MODULE_VERSION(qat_4xxxvf, 1);
264 MODULE_DEPEND(qat_4xxxvf, qat_common, 1, 1, 1);
265 MODULE_DEPEND(qat_4xxxvf, qat_api, 1, 1, 1);
266 MODULE_DEPEND(qat_4xxxvf, linuxkpi, 1, 1, 1);
267