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