raw/ioat: add datapath data structures for idxd devices
[dpdk.git] / drivers / raw / ioat / ioat_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation
3  */
4
5 #include <rte_rawdev_pmd.h>
6 #include <rte_memzone.h>
7 #include <rte_common.h>
8
9 #include "ioat_private.h"
10
11 int
12 idxd_rawdev_close(struct rte_rawdev *dev __rte_unused)
13 {
14         return 0;
15 }
16
17 int
18 idxd_dev_dump(struct rte_rawdev *dev, FILE *f)
19 {
20         struct idxd_rawdev *idxd = dev->dev_private;
21         struct rte_idxd_rawdev *rte_idxd = &idxd->public;
22         int i;
23
24         fprintf(f, "Raw Device #%d\n", dev->dev_id);
25         fprintf(f, "Driver: %s\n\n", dev->driver_name);
26
27         fprintf(f, "Portal: %p\n", rte_idxd->portal);
28         fprintf(f, "Batch Ring size: %u\n", rte_idxd->batch_ring_sz);
29         fprintf(f, "Comp Handle Ring size: %u\n\n", rte_idxd->hdl_ring_sz);
30
31         fprintf(f, "Next batch: %u\n", rte_idxd->next_batch);
32         fprintf(f, "Next batch to be completed: %u\n", rte_idxd->next_completed);
33         for (i = 0; i < rte_idxd->batch_ring_sz; i++) {
34                 struct rte_idxd_desc_batch *b = &rte_idxd->batch_ring[i];
35                 fprintf(f, "Batch %u @%p: submitted=%u, op_count=%u, hdl_end=%u\n",
36                                 i, b, b->submitted, b->op_count, b->hdl_end);
37         }
38
39         fprintf(f, "\n");
40         fprintf(f, "Next free hdl: %u\n", rte_idxd->next_free_hdl);
41         fprintf(f, "Last completed hdl: %u\n", rte_idxd->last_completed_hdl);
42         fprintf(f, "Next returned hdl: %u\n", rte_idxd->next_ret_hdl);
43
44         return 0;
45 }
46
47 int
48 idxd_rawdev_create(const char *name, struct rte_device *dev,
49                    const struct idxd_rawdev *base_idxd,
50                    const struct rte_rawdev_ops *ops)
51 {
52         struct idxd_rawdev *idxd;
53         struct rte_rawdev *rawdev = NULL;
54         const struct rte_memzone *mz = NULL;
55         char mz_name[RTE_MEMZONE_NAMESIZE];
56         int ret = 0;
57
58         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_hw_desc) != 64);
59         RTE_BUILD_BUG_ON(offsetof(struct rte_idxd_hw_desc, size) != 32);
60         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_completion) != 32);
61
62         if (!name) {
63                 IOAT_PMD_ERR("Invalid name of the device!");
64                 ret = -EINVAL;
65                 goto cleanup;
66         }
67
68         /* Allocate device structure */
69         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct idxd_rawdev),
70                                          dev->numa_node);
71         if (rawdev == NULL) {
72                 IOAT_PMD_ERR("Unable to allocate raw device");
73                 ret = -ENOMEM;
74                 goto cleanup;
75         }
76
77         snprintf(mz_name, sizeof(mz_name), "rawdev%u_private", rawdev->dev_id);
78         mz = rte_memzone_reserve(mz_name, sizeof(struct idxd_rawdev),
79                         dev->numa_node, RTE_MEMZONE_IOVA_CONTIG);
80         if (mz == NULL) {
81                 IOAT_PMD_ERR("Unable to reserve memzone for private data\n");
82                 ret = -ENOMEM;
83                 goto cleanup;
84         }
85         rawdev->dev_private = mz->addr;
86         rawdev->dev_ops = ops;
87         rawdev->device = dev;
88         rawdev->driver_name = IOAT_PMD_RAWDEV_NAME_STR;
89
90         idxd = rawdev->dev_private;
91         *idxd = *base_idxd; /* copy over the main fields already passed in */
92         idxd->rawdev = rawdev;
93         idxd->mz = mz;
94
95         return 0;
96
97 cleanup:
98         if (rawdev)
99                 rte_rawdev_pmd_release(rawdev);
100
101         return ret;
102 }