d01c1ee3670d119fadb36990ebd7ab5f799fa282
[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 #include <rte_string_fns.h>
9
10 #include "ioat_private.h"
11
12 static const char * const xstat_names[] = {
13                 "failed_enqueues", "successful_enqueues",
14                 "copies_started", "copies_completed"
15 };
16
17 int
18 ioat_xstats_get(const struct rte_rawdev *dev, const unsigned int ids[],
19                 uint64_t values[], unsigned int n)
20 {
21         const struct rte_ioat_rawdev *ioat = dev->dev_private;
22         const uint64_t *stats = (const void *)&ioat->xstats;
23         unsigned int i;
24
25         for (i = 0; i < n; i++) {
26                 if (ids[i] > sizeof(ioat->xstats)/sizeof(*stats))
27                         values[i] = 0;
28                 else
29                         values[i] = stats[ids[i]];
30         }
31         return n;
32 }
33
34 int
35 ioat_xstats_get_names(const struct rte_rawdev *dev,
36                 struct rte_rawdev_xstats_name *names,
37                 unsigned int size)
38 {
39         unsigned int i;
40
41         RTE_SET_USED(dev);
42         if (size < RTE_DIM(xstat_names))
43                 return RTE_DIM(xstat_names);
44
45         for (i = 0; i < RTE_DIM(xstat_names); i++)
46                 strlcpy(names[i].name, xstat_names[i], sizeof(names[i]));
47
48         return RTE_DIM(xstat_names);
49 }
50
51 int
52 ioat_xstats_reset(struct rte_rawdev *dev, const uint32_t *ids, uint32_t nb_ids)
53 {
54         struct rte_ioat_rawdev *ioat = dev->dev_private;
55         uint64_t *stats = (void *)&ioat->xstats;
56         unsigned int i;
57
58         if (!ids) {
59                 memset(&ioat->xstats, 0, sizeof(ioat->xstats));
60                 return 0;
61         }
62
63         for (i = 0; i < nb_ids; i++)
64                 if (ids[i] < sizeof(ioat->xstats)/sizeof(*stats))
65                         stats[ids[i]] = 0;
66
67         return 0;
68 }
69
70 int
71 idxd_rawdev_close(struct rte_rawdev *dev __rte_unused)
72 {
73         return 0;
74 }
75
76 int
77 idxd_dev_dump(struct rte_rawdev *dev, FILE *f)
78 {
79         struct idxd_rawdev *idxd = dev->dev_private;
80         struct rte_idxd_rawdev *rte_idxd = &idxd->public;
81         int i;
82
83         fprintf(f, "Raw Device #%d\n", dev->dev_id);
84         fprintf(f, "Driver: %s\n\n", dev->driver_name);
85
86         fprintf(f, "Portal: %p\n", rte_idxd->portal);
87         fprintf(f, "Config: {ring_size: %u, hdls_disable: %u}\n\n",
88                         rte_idxd->cfg.ring_size, rte_idxd->cfg.hdls_disable);
89
90         fprintf(f, "max batches: %u\n", rte_idxd->max_batches);
91         fprintf(f, "batch idx read: %u\n", rte_idxd->batch_idx_read);
92         fprintf(f, "batch idx write: %u\n", rte_idxd->batch_idx_write);
93         fprintf(f, "batch idxes:");
94         for (i = 0; i < rte_idxd->max_batches + 1; i++)
95                 fprintf(f, "%u ", rte_idxd->batch_idx_ring[i]);
96         fprintf(f, "\n\n");
97
98         fprintf(f, "hdls read: %u\n", rte_idxd->max_batches);
99         fprintf(f, "hdls avail: %u\n", rte_idxd->hdls_avail);
100         fprintf(f, "batch start: %u\n", rte_idxd->batch_start);
101         fprintf(f, "batch size: %u\n", rte_idxd->batch_size);
102
103         return 0;
104 }
105
106 int
107 idxd_dev_info_get(struct rte_rawdev *dev, rte_rawdev_obj_t dev_info,
108                 size_t info_size)
109 {
110         struct rte_ioat_rawdev_config *cfg = dev_info;
111         struct idxd_rawdev *idxd = dev->dev_private;
112         struct rte_idxd_rawdev *rte_idxd = &idxd->public;
113
114         if (info_size != sizeof(*cfg))
115                 return -EINVAL;
116
117         if (cfg != NULL)
118                 *cfg = rte_idxd->cfg;
119         return 0;
120 }
121
122 int
123 idxd_dev_configure(const struct rte_rawdev *dev,
124                 rte_rawdev_obj_t config, size_t config_size)
125 {
126         struct idxd_rawdev *idxd = dev->dev_private;
127         struct rte_idxd_rawdev *rte_idxd = &idxd->public;
128         struct rte_ioat_rawdev_config *cfg = config;
129         uint16_t max_desc = cfg->ring_size;
130
131         if (config_size != sizeof(*cfg))
132                 return -EINVAL;
133
134         if (dev->started) {
135                 IOAT_PMD_ERR("%s: Error, device is started.", __func__);
136                 return -EAGAIN;
137         }
138
139         rte_idxd->cfg = *cfg;
140
141         if (!rte_is_power_of_2(max_desc))
142                 max_desc = rte_align32pow2(max_desc);
143         IOAT_PMD_DEBUG("Rawdev %u using %u descriptors",
144                         dev->dev_id, max_desc);
145         rte_idxd->desc_ring_mask = max_desc - 1;
146
147         /* in case we are reconfiguring a device, free any existing memory */
148         rte_free(rte_idxd->desc_ring);
149         rte_free(rte_idxd->hdl_ring);
150
151         /* allocate the descriptor ring at 2x size as batches can't wrap */
152         rte_idxd->desc_ring = rte_zmalloc(NULL,
153                         sizeof(*rte_idxd->desc_ring) * max_desc * 2, 0);
154         if (rte_idxd->desc_ring == NULL)
155                 return -ENOMEM;
156         rte_idxd->desc_iova = rte_mem_virt2iova(rte_idxd->desc_ring);
157
158         rte_idxd->hdl_ring = rte_zmalloc(NULL,
159                         sizeof(*rte_idxd->hdl_ring) * max_desc, 0);
160         if (rte_idxd->hdl_ring == NULL) {
161                 rte_free(rte_idxd->desc_ring);
162                 rte_idxd->desc_ring = NULL;
163                 return -ENOMEM;
164         }
165         rte_idxd->hdl_ring_flags = rte_zmalloc(NULL,
166                         sizeof(*rte_idxd->hdl_ring_flags) * max_desc, 0);
167         if (rte_idxd->hdl_ring_flags == NULL) {
168                 rte_free(rte_idxd->desc_ring);
169                 rte_free(rte_idxd->hdl_ring);
170                 rte_idxd->desc_ring = NULL;
171                 rte_idxd->hdl_ring = NULL;
172                 return -ENOMEM;
173         }
174         rte_idxd->hdls_read = rte_idxd->batch_start = 0;
175         rte_idxd->batch_size = 0;
176
177         return 0;
178 }
179
180 int
181 idxd_rawdev_create(const char *name, struct rte_device *dev,
182                    const struct idxd_rawdev *base_idxd,
183                    const struct rte_rawdev_ops *ops)
184 {
185         struct idxd_rawdev *idxd;
186         struct rte_idxd_rawdev *public;
187         struct rte_rawdev *rawdev = NULL;
188         const struct rte_memzone *mz = NULL;
189         char mz_name[RTE_MEMZONE_NAMESIZE];
190         int ret = 0;
191
192         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_hw_desc) != 64);
193         RTE_BUILD_BUG_ON(offsetof(struct rte_idxd_hw_desc, size) != 32);
194         RTE_BUILD_BUG_ON(sizeof(struct rte_idxd_completion) != 32);
195
196         if (!name) {
197                 IOAT_PMD_ERR("Invalid name of the device!");
198                 ret = -EINVAL;
199                 goto cleanup;
200         }
201
202         /* Allocate device structure */
203         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct idxd_rawdev),
204                                          dev->numa_node);
205         if (rawdev == NULL) {
206                 IOAT_PMD_ERR("Unable to allocate raw device");
207                 ret = -ENOMEM;
208                 goto cleanup;
209         }
210
211         /* Allocate memory for the primary process or else return the memory
212          * of primary memzone for the secondary process.
213          */
214         snprintf(mz_name, sizeof(mz_name), "rawdev%u_private", rawdev->dev_id);
215         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
216                 mz = rte_memzone_lookup(mz_name);
217                 if (mz == NULL) {
218                         IOAT_PMD_ERR("Unable lookup memzone for private data\n");
219                         ret = -ENOMEM;
220                         goto cleanup;
221                 }
222                 rawdev->dev_private = mz->addr;
223                 rawdev->dev_ops = ops;
224                 rawdev->device = dev;
225                 return 0;
226         }
227         mz = rte_memzone_reserve(mz_name, sizeof(struct idxd_rawdev),
228                         dev->numa_node, RTE_MEMZONE_IOVA_CONTIG);
229         if (mz == NULL) {
230                 IOAT_PMD_ERR("Unable to reserve memzone for private data\n");
231                 ret = -ENOMEM;
232                 goto cleanup;
233         }
234         rawdev->dev_private = mz->addr;
235         rawdev->dev_ops = ops;
236         rawdev->device = dev;
237         rawdev->driver_name = IOAT_PMD_RAWDEV_NAME_STR;
238
239         idxd = rawdev->dev_private;
240         *idxd = *base_idxd; /* copy over the main fields already passed in */
241         idxd->rawdev = rawdev;
242         idxd->mz = mz;
243
244         public = &idxd->public;
245         public->type = RTE_IDXD_DEV;
246         public->max_batches = idxd->max_batches;
247         public->batch_idx_read = 0;
248         public->batch_idx_write = 0;
249         /* allocate batch index ring. The +1 is because we can never fully use
250          * the ring, otherwise read == write means both full and empty.
251          */
252         public->batch_idx_ring = rte_zmalloc(NULL,
253                         sizeof(uint16_t) * (idxd->max_batches + 1), 0);
254         if (public->batch_idx_ring == NULL) {
255                 IOAT_PMD_ERR("Unable to reserve memory for batch data\n");
256                 ret = -ENOMEM;
257                 goto cleanup;
258         }
259
260         return 0;
261
262 cleanup:
263         if (mz)
264                 rte_memzone_free(mz);
265         if (rawdev)
266                 rte_rawdev_pmd_release(rawdev);
267
268         return ret;
269 }