Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2021 Intel Corporation
3 : : */
4 : :
5 : : #include <bus_pci_driver.h>
6 : : #include <rte_devargs.h>
7 : : #include <rte_dmadev_pmd.h>
8 : : #include <rte_malloc.h>
9 : :
10 : : #include "idxd_internal.h"
11 : :
12 : : #define IDXD_VENDOR_ID 0x8086
13 : : #define IDXD_DEVICE_ID_SPR 0x0B25
14 : : #define IDXD_DEVICE_ID_GNRD 0x11FB
15 : : #define IDXD_DEVICE_ID_DMR 0x1212
16 : :
17 : : #define DEVICE_VERSION_1 0x100
18 : : #define DEVICE_VERSION_2 0x200
19 : : /*
20 : : * Set bits for Traffic Class A & B
21 : : * TC-A (Bits 2:0) and TC-B (Bits 5:3)
22 : : */
23 : : #define IDXD_SET_TC_A_B 0x9
24 : :
25 : : #define IDXD_PMD_DMADEV_NAME_PCI dmadev_idxd_pci
26 : :
27 : : const struct rte_pci_id pci_id_idxd_map[] = {
28 : : { RTE_PCI_DEVICE(IDXD_VENDOR_ID, IDXD_DEVICE_ID_SPR) },
29 : : { RTE_PCI_DEVICE(IDXD_VENDOR_ID, IDXD_DEVICE_ID_GNRD) },
30 : : { RTE_PCI_DEVICE(IDXD_VENDOR_ID, IDXD_DEVICE_ID_DMR) },
31 : : { .vendor_id = 0, /* sentinel */ },
32 : : };
33 : :
34 : : static inline int
35 : 0 : idxd_pci_dev_command(struct idxd_dmadev *idxd, enum rte_idxd_cmds command)
36 : : {
37 : : uint32_t err_code;
38 : 0 : uint16_t qid = idxd->qid;
39 : : int i = 0;
40 : :
41 [ # # ]: 0 : if (command >= idxd_disable_wq && command <= idxd_reset_wq)
42 : 0 : qid = (1 << qid);
43 : 0 : rte_spinlock_lock(&idxd->u.pci->lk);
44 : 0 : idxd->u.pci->regs->cmd = (command << IDXD_CMD_SHIFT) | qid;
45 : :
46 : : do {
47 : : rte_pause();
48 : 0 : err_code = idxd->u.pci->regs->cmdstatus;
49 [ # # ]: 0 : if (++i >= 1000) {
50 : 0 : IDXD_PMD_ERR("Timeout waiting for command response from HW");
51 : 0 : rte_spinlock_unlock(&idxd->u.pci->lk);
52 : 0 : err_code &= CMDSTATUS_ERR_MASK;
53 : 0 : return err_code;
54 : : }
55 [ # # ]: 0 : } while (err_code & CMDSTATUS_ACTIVE_MASK);
56 : 0 : rte_spinlock_unlock(&idxd->u.pci->lk);
57 : :
58 : 0 : err_code &= CMDSTATUS_ERR_MASK;
59 : 0 : return err_code;
60 : : }
61 : :
62 : : static uint32_t *
63 : : idxd_get_wq_cfg(struct idxd_pci_common *pci, uint8_t wq_idx)
64 : : {
65 : 0 : return RTE_PTR_ADD(pci->wq_regs_base,
66 : : (uintptr_t)wq_idx << (5 + pci->wq_cfg_sz));
67 : : }
68 : :
69 : : static int
70 : : idxd_is_wq_enabled(struct idxd_dmadev *idxd)
71 : : {
72 : 0 : uint32_t state = idxd_get_wq_cfg(idxd->u.pci, idxd->qid)[wq_state_idx];
73 : 0 : return ((state >> WQ_STATE_SHIFT) & WQ_STATE_MASK) == 0x1;
74 : : }
75 : :
76 : : static int
77 : 0 : idxd_pci_dev_stop(struct rte_dma_dev *dev)
78 : : {
79 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
80 : : uint8_t err_code;
81 : :
82 [ # # ]: 0 : if (!idxd_is_wq_enabled(idxd)) {
83 : 0 : IDXD_PMD_ERR("Work queue %d already disabled", idxd->qid);
84 : 0 : return 0;
85 : : }
86 : :
87 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_wq);
88 [ # # # # ]: 0 : if (err_code || idxd_is_wq_enabled(idxd)) {
89 : 0 : IDXD_PMD_ERR("Failed disabling work queue %d, error code: %#x",
90 : : idxd->qid, err_code);
91 [ # # ]: 0 : return err_code == 0 ? -1 : -err_code;
92 : : }
93 : 0 : IDXD_PMD_DEBUG("Work queue %d disabled OK", idxd->qid);
94 : :
95 : 0 : return 0;
96 : : }
97 : :
98 : : static int
99 : 0 : idxd_pci_dev_start(struct rte_dma_dev *dev)
100 : : {
101 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
102 : : uint8_t err_code;
103 : :
104 [ # # ]: 0 : if (idxd_is_wq_enabled(idxd)) {
105 : 0 : IDXD_PMD_WARN("WQ %d already enabled", idxd->qid);
106 : 0 : return 0;
107 : : }
108 : :
109 [ # # ]: 0 : if (idxd->desc_ring == NULL) {
110 : 0 : IDXD_PMD_ERR("WQ %d has not been fully configured", idxd->qid);
111 : 0 : return -EINVAL;
112 : : }
113 : :
114 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_enable_wq);
115 [ # # # # ]: 0 : if (err_code || !idxd_is_wq_enabled(idxd)) {
116 : 0 : IDXD_PMD_ERR("Failed enabling work queue %d, error code: %#x",
117 : : idxd->qid, err_code);
118 [ # # ]: 0 : return err_code == 0 ? -1 : -err_code;
119 : : }
120 : 0 : IDXD_PMD_DEBUG("Work queue %d enabled OK", idxd->qid);
121 : :
122 : 0 : return 0;
123 : : }
124 : :
125 : : static int
126 : 0 : idxd_pci_dev_close(struct rte_dma_dev *dev)
127 : : {
128 : 0 : struct idxd_dmadev *idxd = dev->fp_obj->dev_private;
129 : : uint8_t err_code;
130 : : int is_last_wq;
131 : :
132 [ # # ]: 0 : if (idxd_is_wq_enabled(idxd)) {
133 : : /* disable the wq */
134 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_wq);
135 [ # # ]: 0 : if (err_code) {
136 : 0 : IDXD_PMD_ERR("Error disabling wq: code %#x", err_code);
137 : 0 : return err_code;
138 : : }
139 : 0 : IDXD_PMD_DEBUG("IDXD WQ disabled OK");
140 : : }
141 : :
142 : : /* free device memory */
143 : 0 : IDXD_PMD_DEBUG("Freeing device driver memory");
144 : 0 : rte_free(idxd->batch_comp_ring);
145 : 0 : rte_free(idxd->desc_ring);
146 : :
147 : : /* if this is the last WQ on the device, disable the device and free
148 : : * the PCI struct
149 : : */
150 : : /* NOTE: review for potential ordering optimization */
151 : 0 : is_last_wq = (rte_atomic_fetch_sub_explicit(&idxd->u.pci->ref_count, 1,
152 : : rte_memory_order_seq_cst) == 1);
153 [ # # ]: 0 : if (is_last_wq) {
154 : : /* disable the device */
155 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_disable_dev);
156 [ # # ]: 0 : if (err_code) {
157 : 0 : IDXD_PMD_ERR("Error disabling device: code %#x", err_code);
158 : 0 : return err_code;
159 : : }
160 : 0 : IDXD_PMD_DEBUG("IDXD device disabled OK");
161 : 0 : rte_free(idxd->u.pci);
162 : : }
163 : :
164 : : return 0;
165 : : }
166 : :
167 : : static const struct rte_dma_dev_ops idxd_pci_ops = {
168 : : .dev_close = idxd_pci_dev_close,
169 : : .dev_dump = idxd_dump,
170 : : .dev_configure = idxd_configure,
171 : : .vchan_setup = idxd_vchan_setup,
172 : : .dev_info_get = idxd_info_get,
173 : : .stats_get = idxd_stats_get,
174 : : .stats_reset = idxd_stats_reset,
175 : : .dev_start = idxd_pci_dev_start,
176 : : .dev_stop = idxd_pci_dev_stop,
177 : : .vchan_status = idxd_vchan_status,
178 : : };
179 : :
180 : : /* each portal uses 4 x 4k pages */
181 : : #define IDXD_PORTAL_SIZE (4096 * 4)
182 : :
183 : : static int
184 : 0 : init_pci_device(struct rte_pci_device *dev, struct idxd_dmadev *idxd,
185 : : unsigned int max_queues)
186 : : {
187 : : struct idxd_pci_common *pci;
188 : : uint8_t nb_groups, nb_engines, nb_wqs;
189 : : uint16_t grp_offset, wq_offset; /* how far into bar0 the regs are */
190 : : uint16_t wq_size, total_wq_size;
191 : : uint8_t lg2_max_batch, lg2_max_copy_size;
192 : : uint32_t version;
193 : : unsigned int i, err_code;
194 : :
195 : 0 : pci = rte_malloc(NULL, sizeof(*pci), 0);
196 [ # # ]: 0 : if (pci == NULL) {
197 : 0 : IDXD_PMD_ERR("%s: Can't allocate memory", __func__);
198 : : err_code = -1;
199 : 0 : goto err;
200 : : }
201 : : memset(pci, 0, sizeof(*pci));
202 : : rte_spinlock_init(&pci->lk);
203 : :
204 : : /* assign the bar registers, and then configure device */
205 : 0 : pci->regs = dev->mem_resource[0].addr;
206 : 0 : version = pci->regs->version;
207 : 0 : grp_offset = (uint16_t)pci->regs->offsets[0];
208 : 0 : pci->grp_regs = RTE_PTR_ADD(pci->regs, grp_offset * 0x100);
209 : 0 : wq_offset = (uint16_t)(pci->regs->offsets[0] >> 16);
210 : 0 : pci->wq_regs_base = RTE_PTR_ADD(pci->regs, wq_offset * 0x100);
211 : 0 : pci->portals = dev->mem_resource[2].addr;
212 : 0 : pci->wq_cfg_sz = (pci->regs->wqcap >> 24) & 0x0F;
213 : :
214 : : /* reset */
215 : 0 : idxd->u.pci = pci;
216 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_reset_device);
217 [ # # ]: 0 : if (err_code) {
218 : 0 : IDXD_PMD_ERR("Error reset device: code %#x", err_code);
219 : 0 : goto err;
220 : : }
221 : :
222 : : /* sanity check device status */
223 [ # # ]: 0 : if (pci->regs->gensts & GENSTS_DEV_STATE_MASK) {
224 : : /* need function-level-reset (FLR) or is enabled */
225 : 0 : IDXD_PMD_ERR("Device status is not disabled, cannot init");
226 : : err_code = -1;
227 : 0 : goto err;
228 : : }
229 [ # # ]: 0 : if (pci->regs->cmdstatus & CMDSTATUS_ACTIVE_MASK) {
230 : : /* command in progress */
231 : 0 : IDXD_PMD_ERR("Device has a command in progress, cannot init");
232 : : err_code = -1;
233 : 0 : goto err;
234 : : }
235 : :
236 : : /* read basic info about the hardware for use when configuring */
237 : 0 : nb_groups = (uint8_t)pci->regs->grpcap;
238 : 0 : nb_engines = (uint8_t)pci->regs->engcap;
239 : 0 : nb_wqs = (uint8_t)(pci->regs->wqcap >> 16);
240 : 0 : total_wq_size = (uint16_t)pci->regs->wqcap;
241 : 0 : lg2_max_copy_size = (uint8_t)(pci->regs->gencap >> 16) & 0x1F;
242 : 0 : lg2_max_batch = (uint8_t)(pci->regs->gencap >> 21) & 0x0F;
243 : :
244 : 0 : IDXD_PMD_DEBUG("nb_groups = %u, nb_engines = %u, nb_wqs = %u",
245 : : nb_groups, nb_engines, nb_wqs);
246 : :
247 : : /* zero out any old config */
248 [ # # ]: 0 : for (i = 0; i < nb_groups; i++) {
249 : 0 : pci->grp_regs[i].grpengcfg = 0;
250 : 0 : pci->grp_regs[i].grpwqcfg[0] = 0;
251 [ # # ]: 0 : if (version <= DEVICE_VERSION_2)
252 : 0 : pci->grp_regs[i].grpflags |= IDXD_SET_TC_A_B;
253 : : }
254 [ # # ]: 0 : for (i = 0; i < nb_wqs; i++)
255 : 0 : idxd_get_wq_cfg(pci, i)[0] = 0;
256 : :
257 : : /* limit queues if necessary */
258 [ # # # # ]: 0 : if (max_queues != 0 && nb_wqs > max_queues) {
259 : 0 : nb_wqs = max_queues;
260 [ # # ]: 0 : if (nb_engines > max_queues)
261 : : nb_engines = max_queues;
262 [ # # ]: 0 : if (nb_groups > max_queues)
263 : : nb_engines = max_queues;
264 : 0 : IDXD_PMD_DEBUG("Limiting queues to %u", nb_wqs);
265 : : }
266 : :
267 : : /* put each engine into a separate group to avoid reordering */
268 : : if (nb_groups > nb_engines)
269 : : nb_groups = nb_engines;
270 : : if (nb_groups < nb_engines)
271 : : nb_engines = nb_groups;
272 : :
273 : : /* assign engines to groups, round-robin style */
274 [ # # ]: 0 : for (i = 0; i < nb_engines; i++) {
275 : 0 : IDXD_PMD_DEBUG("Assigning engine %u to group %u",
276 : : i, i % nb_groups);
277 : 0 : pci->grp_regs[i % nb_groups].grpengcfg |= (1ULL << i);
278 : : }
279 : :
280 : : /* now do the same for queues and give work slots to each queue */
281 : 0 : wq_size = total_wq_size / nb_wqs;
282 : 0 : IDXD_PMD_DEBUG("Work queue size = %u, max batch = 2^%u, max copy = 2^%u",
283 : : wq_size, lg2_max_batch, lg2_max_copy_size);
284 [ # # ]: 0 : for (i = 0; i < nb_wqs; i++) {
285 : : /* add engine "i" to a group */
286 : 0 : IDXD_PMD_DEBUG("Assigning work queue %u to group %u",
287 : : i, i % nb_groups);
288 : 0 : pci->grp_regs[i % nb_groups].grpwqcfg[0] |= (1ULL << i);
289 : : /* now configure it, in terms of size, max batch, mode */
290 : 0 : idxd_get_wq_cfg(pci, i)[wq_size_idx] = wq_size;
291 : 0 : idxd_get_wq_cfg(pci, i)[wq_mode_idx] = (1 << WQ_PRIORITY_SHIFT) |
292 : : WQ_MODE_DEDICATED;
293 : 0 : idxd_get_wq_cfg(pci, i)[wq_sizes_idx] = lg2_max_copy_size |
294 : 0 : (lg2_max_batch << WQ_BATCH_SZ_SHIFT);
295 : : }
296 : :
297 : 0 : IDXD_PMD_DEBUG(" Device Version: %"PRIx32, version);
298 : : /* dump the group configuration to output */
299 [ # # ]: 0 : for (i = 0; i < nb_groups; i++) {
300 : 0 : IDXD_PMD_DEBUG("## Group %d", i);
301 : 0 : IDXD_PMD_DEBUG(" GRPWQCFG: %"PRIx64, pci->grp_regs[i].grpwqcfg[0]);
302 : 0 : IDXD_PMD_DEBUG(" GRPENGCFG: %"PRIx64, pci->grp_regs[i].grpengcfg);
303 : 0 : IDXD_PMD_DEBUG(" GRPFLAGS: %"PRIx32, pci->grp_regs[i].grpflags);
304 : : }
305 : :
306 : 0 : idxd->u.pci = pci;
307 : 0 : idxd->max_batches = wq_size;
308 : 0 : idxd->max_batch_size = 1 << lg2_max_batch;
309 : :
310 : : /* enable the device itself */
311 : 0 : err_code = idxd_pci_dev_command(idxd, idxd_enable_dev);
312 [ # # ]: 0 : if (err_code) {
313 : 0 : IDXD_PMD_ERR("Error enabling device: code %#x", err_code);
314 : 0 : goto err;
315 : : }
316 : 0 : IDXD_PMD_DEBUG("IDXD Device enabled OK");
317 : :
318 : 0 : return nb_wqs;
319 : :
320 : 0 : err:
321 : 0 : rte_free(pci);
322 : 0 : return err_code;
323 : : }
324 : :
325 : : static int
326 : 0 : idxd_dmadev_probe_pci(struct rte_pci_driver *drv, struct rte_pci_device *dev)
327 : : {
328 : 0 : struct idxd_dmadev idxd = {0};
329 : : uint8_t nb_wqs;
330 : : int qid, ret = 0;
331 : : char name[PCI_PRI_STR_SIZE];
332 : 0 : unsigned int max_queues = 0;
333 : :
334 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
335 : 0 : IDXD_PMD_INFO("Init %s on NUMA node %d", name, dev->device.numa_node);
336 : 0 : dev->device.driver = &drv->driver;
337 : :
338 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
339 : : char qname[32];
340 : : int max_qid;
341 : :
342 : : /* look up queue 0 to get the PCI structure */
343 : : snprintf(qname, sizeof(qname), "%s-q0", name);
344 : 0 : IDXD_PMD_INFO("Looking up %s", qname);
345 : 0 : ret = idxd_dmadev_create(qname, &dev->device, NULL, &idxd_pci_ops);
346 [ # # ]: 0 : if (ret != 0) {
347 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
348 : 0 : return ret;
349 : : }
350 : 0 : qid = rte_dma_get_dev_id_by_name(qname);
351 : 0 : max_qid = rte_atomic_load_explicit(
352 : : &((struct idxd_dmadev *)rte_dma_fp_objs[qid].dev_private)->u.pci->ref_count,
353 : : rte_memory_order_seq_cst);
354 : :
355 : : /* we have queue 0 done, now configure the rest of the queues */
356 [ # # ]: 0 : for (qid = 1; qid < max_qid; qid++) {
357 : : /* add the queue number to each device name */
358 : : snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
359 : 0 : IDXD_PMD_INFO("Looking up %s", qname);
360 : 0 : ret = idxd_dmadev_create(qname, &dev->device, NULL, &idxd_pci_ops);
361 [ # # ]: 0 : if (ret != 0) {
362 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
363 : 0 : return ret;
364 : : }
365 : : }
366 : : return 0;
367 : : }
368 : :
369 [ # # # # ]: 0 : if (dev->device.devargs && dev->device.devargs->args[0] != '\0') {
370 : : /* if the number of devargs grows beyond just 1, use rte_kvargs */
371 [ # # ]: 0 : if (sscanf(dev->device.devargs->args,
372 : : "max_queues=%u", &max_queues) != 1) {
373 : 0 : IDXD_PMD_ERR("Invalid device parameter: '%s'",
374 : : dev->device.devargs->args);
375 : 0 : return -1;
376 : : }
377 : : }
378 : :
379 : 0 : ret = init_pci_device(dev, &idxd, max_queues);
380 [ # # ]: 0 : if (ret < 0) {
381 : 0 : IDXD_PMD_ERR("Error initializing PCI hardware");
382 : 0 : return ret;
383 : : }
384 [ # # ]: 0 : if (idxd.u.pci->portals == NULL) {
385 : 0 : IDXD_PMD_ERR("Error, invalid portal assigned during initialization");
386 : 0 : free(idxd.u.pci);
387 : 0 : return -EINVAL;
388 : : }
389 : : nb_wqs = (uint8_t)ret;
390 : :
391 : : /* set up one device for each queue */
392 [ # # ]: 0 : for (qid = 0; qid < nb_wqs; qid++) {
393 : : char qname[32];
394 : :
395 : : /* add the queue number to each device name */
396 : : snprintf(qname, sizeof(qname), "%s-q%d", name, qid);
397 : 0 : idxd.qid = qid;
398 : 0 : idxd.portal = RTE_PTR_ADD(idxd.u.pci->portals,
399 : : qid * IDXD_PORTAL_SIZE);
400 [ # # ]: 0 : if (idxd_is_wq_enabled(&idxd))
401 : 0 : IDXD_PMD_ERR("Error, WQ %u seems enabled", qid);
402 : 0 : ret = idxd_dmadev_create(qname, &dev->device,
403 : : &idxd, &idxd_pci_ops);
404 [ # # ]: 0 : if (ret != 0) {
405 : 0 : IDXD_PMD_ERR("Failed to create dmadev %s", name);
406 [ # # ]: 0 : if (qid == 0) /* if no devices using this, free pci */
407 : 0 : free(idxd.u.pci);
408 : 0 : return ret;
409 : : }
410 : 0 : rte_atomic_fetch_add_explicit(&idxd.u.pci->ref_count, 1, rte_memory_order_seq_cst);
411 : : }
412 : :
413 : : return 0;
414 : : }
415 : :
416 : : static int
417 : 0 : idxd_dmadev_destroy(const char *name)
418 : : {
419 : : int ret = 0;
420 : :
421 : : /* rte_dma_close is called by pmd_release */
422 : 0 : ret = rte_dma_pmd_release(name);
423 [ # # ]: 0 : if (ret)
424 : 0 : IDXD_PMD_DEBUG("Device cleanup failed");
425 : :
426 : 0 : return ret;
427 : : }
428 : :
429 : : static int
430 : 0 : idxd_dmadev_remove_pci(struct rte_pci_device *dev)
431 : : {
432 : : int i = 0;
433 : : char name[PCI_PRI_STR_SIZE];
434 : :
435 : 0 : rte_pci_device_name(&dev->addr, name, sizeof(name));
436 : :
437 : 0 : IDXD_PMD_INFO("Closing %s on NUMA node %d", name, dev->device.numa_node);
438 : :
439 [ # # ]: 0 : RTE_DMA_FOREACH_DEV(i) {
440 : : struct rte_dma_info info;
441 : 0 : rte_dma_info_get(i, &info);
442 [ # # ]: 0 : if (strncmp(name, info.dev_name, strlen(name)) == 0)
443 : 0 : idxd_dmadev_destroy(info.dev_name);
444 : : }
445 : :
446 : 0 : return 0;
447 : : }
448 : :
449 : : struct rte_pci_driver idxd_pmd_drv_pci = {
450 : : .id_table = pci_id_idxd_map,
451 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
452 : : .probe = idxd_dmadev_probe_pci,
453 : : .remove = idxd_dmadev_remove_pci,
454 : : };
455 : :
456 : 252 : RTE_PMD_REGISTER_PCI(IDXD_PMD_DMADEV_NAME_PCI, idxd_pmd_drv_pci);
457 : : RTE_PMD_REGISTER_PCI_TABLE(IDXD_PMD_DMADEV_NAME_PCI, pci_id_idxd_map);
458 : : RTE_PMD_REGISTER_KMOD_DEP(IDXD_PMD_DMADEV_NAME_PCI, "vfio-pci");
459 : : RTE_PMD_REGISTER_PARAM_STRING(dmadev_idxd_pci, "max_queues=0");
|