raw/ioat: add data path for idxd device
[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_dev_configure(const struct rte_rawdev *dev,
49                 rte_rawdev_obj_t config, size_t config_size)
50 {
51         struct idxd_rawdev *idxd = dev->dev_private;
52         struct rte_idxd_rawdev *rte_idxd = &idxd->public;
53         struct rte_ioat_rawdev_config *cfg = config;
54         uint16_t max_desc = cfg->ring_size;
55         uint16_t max_batches = max_desc / BATCH_SIZE;
56         uint16_t i;
57
58         if (config_size != sizeof(*cfg))
59                 return -EINVAL;
60
61         if (dev->started) {
62                 IOAT_PMD_ERR("%s: Error, device is started.", __func__);
63                 return -EAGAIN;
64         }
65
66         rte_idxd->hdls_disable = cfg->hdls_disable;
67
68         /* limit the batches to what can be stored in hardware */
69         if (max_batches > idxd->max_batches) {
70                 IOAT_PMD_DEBUG("Ring size of %u is too large for this device, need to limit to %u batches of %u",
71                                 max_desc, idxd->max_batches, BATCH_SIZE);
72                 max_batches = idxd->max_batches;
73                 max_desc = max_batches * BATCH_SIZE;
74         }
75         if (!rte_is_power_of_2(max_desc))
76                 max_desc = rte_align32pow2(max_desc);
77         IOAT_PMD_DEBUG("Rawdev %u using %u descriptors in %u batches",
78                         dev->dev_id, max_desc, max_batches);
79
80         /* in case we are reconfiguring a device, free any existing memory */
81         rte_free(rte_idxd->batch_ring);
82         rte_free(rte_idxd->hdl_ring);
83
84         rte_idxd->batch_ring = rte_zmalloc(NULL,
85                         sizeof(*rte_idxd->batch_ring) * max_batches, 0);
86         if (rte_idxd->batch_ring == NULL)
87                 return -ENOMEM;
88
89         rte_idxd->hdl_ring = rte_zmalloc(NULL,
90                         sizeof(*rte_idxd->hdl_ring) * max_desc, 0);
91         if (rte_idxd->hdl_ring == NULL) {
92                 rte_free(rte_idxd->batch_ring);
93                 rte_idxd->batch_ring = NULL;
94                 return -ENOMEM;
95         }
96         rte_idxd->batch_ring_sz = max_batches;
97         rte_idxd->hdl_ring_sz = max_desc;
98
99         for (i = 0; i < rte_idxd->batch_ring_sz; i++) {
100                 struct rte_idxd_desc_batch *b = &rte_idxd->batch_ring[i];
101                 b->batch_desc.completion = rte_mem_virt2iova(&b->comp);
102                 b->batch_desc.desc_addr = rte_mem_virt2iova(&b->null_desc);
103                 b->batch_desc.op_flags = (idxd_op_batch << IDXD_CMD_OP_SHIFT) |
104                                 IDXD_FLAG_COMPLETION_ADDR_VALID |
105                                 IDXD_FLAG_REQUEST_COMPLETION;
106         }
107
108         return 0;
109 }
110
111 int
112 idxd_rawdev_create(const char *name, struct rte_device *dev,
113                    const struct idxd_rawdev *base_idxd,
114                    const struct rte_rawdev_ops *ops)
115 {
116         struct idxd_rawdev *idxd;
117         struct rte_rawdev *rawdev = NULL;
118         const struct rte_memzone *mz = NULL;
119         char mz_name[RTE_MEMZONE_NAMESIZE];
120         int ret = 0;
121
122         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_hw_desc) != 64);
123         RTE_BUILD_BUG_ON(offsetof(struct rte_idxd_hw_desc, size) != 32);
124         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_completion) != 32);
125
126         if (!name) {
127                 IOAT_PMD_ERR("Invalid name of the device!");
128                 ret = -EINVAL;
129                 goto cleanup;
130         }
131
132         /* Allocate device structure */
133         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct idxd_rawdev),
134                                          dev->numa_node);
135         if (rawdev == NULL) {
136                 IOAT_PMD_ERR("Unable to allocate raw device");
137                 ret = -ENOMEM;
138                 goto cleanup;
139         }
140
141         snprintf(mz_name, sizeof(mz_name), "rawdev%u_private", rawdev->dev_id);
142         mz = rte_memzone_reserve(mz_name, sizeof(struct idxd_rawdev),
143                         dev->numa_node, RTE_MEMZONE_IOVA_CONTIG);
144         if (mz == NULL) {
145                 IOAT_PMD_ERR("Unable to reserve memzone for private data\n");
146                 ret = -ENOMEM;
147                 goto cleanup;
148         }
149         rawdev->dev_private = mz->addr;
150         rawdev->dev_ops = ops;
151         rawdev->device = dev;
152         rawdev->driver_name = IOAT_PMD_RAWDEV_NAME_STR;
153
154         idxd = rawdev->dev_private;
155         *idxd = *base_idxd; /* copy over the main fields already passed in */
156         idxd->public.type = RTE_IDXD_DEV;
157         idxd->rawdev = rawdev;
158         idxd->mz = mz;
159
160         return 0;
161
162 cleanup:
163         if (rawdev)
164                 rte_rawdev_pmd_release(rawdev);
165
166         return ret;
167 }