1 /* SPDX-License-Identifier: BSD-3-Clause
13 #include <sys/types.h>
14 #include <sys/queue.h>
16 #include <rte_string_fns.h>
17 #include <rte_byteorder.h>
19 #include <rte_debug.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_memzone.h>
25 #include <rte_per_lcore.h>
26 #include <rte_lcore.h>
27 #include <rte_atomic.h>
28 #include <rte_branch_prediction.h>
29 #include <rte_common.h>
30 #include <rte_malloc.h>
31 #include <rte_errno.h>
32 #include <rte_telemetry.h>
34 #include "rte_rawdev.h"
35 #include "rte_rawdev_pmd.h"
37 static struct rte_rawdev rte_rawdevices[RTE_RAWDEV_MAX_DEVS];
39 struct rte_rawdev *rte_rawdevs = rte_rawdevices;
41 static struct rte_rawdev_global rawdev_globals = {
45 /* Raw device, northbound API implementation */
47 rte_rawdev_count(void)
49 return rawdev_globals.nb_devs;
53 rte_rawdev_get_dev_id(const char *name)
60 for (i = 0; i < rawdev_globals.nb_devs; i++)
61 if ((strcmp(rte_rawdevices[i].name, name)
63 (rte_rawdevices[i].attached ==
70 rte_rawdev_socket_id(uint16_t dev_id)
72 struct rte_rawdev *dev;
74 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
75 dev = &rte_rawdevs[dev_id];
77 return dev->socket_id;
81 rte_rawdev_info_get(uint16_t dev_id, struct rte_rawdev_info *dev_info,
82 size_t dev_private_size)
84 struct rte_rawdev *rawdev;
87 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
88 RTE_FUNC_PTR_OR_ERR_RET(dev_info, -EINVAL);
90 rawdev = &rte_rawdevs[dev_id];
92 if (dev_info->dev_private != NULL) {
93 RTE_FUNC_PTR_OR_ERR_RET(*rawdev->dev_ops->dev_info_get, -ENOTSUP);
94 ret = (*rawdev->dev_ops->dev_info_get)(rawdev,
95 dev_info->dev_private,
99 dev_info->driver_name = rawdev->driver_name;
100 dev_info->device = rawdev->device;
101 dev_info->socket_id = rawdev->socket_id;
107 rte_rawdev_configure(uint16_t dev_id, struct rte_rawdev_info *dev_conf,
108 size_t dev_private_size)
110 struct rte_rawdev *dev;
113 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
114 RTE_FUNC_PTR_OR_ERR_RET(dev_conf, -EINVAL);
116 dev = &rte_rawdevs[dev_id];
118 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
122 "device %d must be stopped to allow configuration", dev_id);
126 /* Configure the device */
127 diag = (*dev->dev_ops->dev_configure)(dev, dev_conf->dev_private,
130 RTE_RDEV_ERR("dev%d dev_configure = %d", dev_id, diag);
138 rte_rawdev_queue_conf_get(uint16_t dev_id,
140 rte_rawdev_obj_t queue_conf,
141 size_t queue_conf_size)
143 struct rte_rawdev *dev;
145 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
146 dev = &rte_rawdevs[dev_id];
148 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf, -ENOTSUP);
149 return (*dev->dev_ops->queue_def_conf)(dev, queue_id, queue_conf,
154 rte_rawdev_queue_setup(uint16_t dev_id,
156 rte_rawdev_obj_t queue_conf,
157 size_t queue_conf_size)
159 struct rte_rawdev *dev;
161 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
162 dev = &rte_rawdevs[dev_id];
164 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_setup, -ENOTSUP);
165 return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf,
170 rte_rawdev_queue_release(uint16_t dev_id, uint16_t queue_id)
172 struct rte_rawdev *dev;
174 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
175 dev = &rte_rawdevs[dev_id];
177 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
178 return (*dev->dev_ops->queue_release)(dev, queue_id);
182 rte_rawdev_queue_count(uint16_t dev_id)
184 struct rte_rawdev *dev;
186 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
187 dev = &rte_rawdevs[dev_id];
189 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_count, -ENOTSUP);
190 return (*dev->dev_ops->queue_count)(dev);
194 rte_rawdev_get_attr(uint16_t dev_id,
195 const char *attr_name,
196 uint64_t *attr_value)
198 struct rte_rawdev *dev;
200 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
201 dev = &rte_rawdevs[dev_id];
203 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->attr_get, -ENOTSUP);
204 return (*dev->dev_ops->attr_get)(dev, attr_name, attr_value);
208 rte_rawdev_set_attr(uint16_t dev_id,
209 const char *attr_name,
210 const uint64_t attr_value)
212 struct rte_rawdev *dev;
214 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
215 dev = &rte_rawdevs[dev_id];
217 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->attr_set, -ENOTSUP);
218 return (*dev->dev_ops->attr_set)(dev, attr_name, attr_value);
222 rte_rawdev_enqueue_buffers(uint16_t dev_id,
223 struct rte_rawdev_buf **buffers,
225 rte_rawdev_obj_t context)
227 struct rte_rawdev *dev;
229 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
230 dev = &rte_rawdevs[dev_id];
232 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->enqueue_bufs, -ENOTSUP);
233 return (*dev->dev_ops->enqueue_bufs)(dev, buffers, count, context);
237 rte_rawdev_dequeue_buffers(uint16_t dev_id,
238 struct rte_rawdev_buf **buffers,
240 rte_rawdev_obj_t context)
242 struct rte_rawdev *dev;
244 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
245 dev = &rte_rawdevs[dev_id];
247 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dequeue_bufs, -ENOTSUP);
248 return (*dev->dev_ops->dequeue_bufs)(dev, buffers, count, context);
252 rte_rawdev_dump(uint16_t dev_id, FILE *f)
254 struct rte_rawdev *dev;
256 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
257 dev = &rte_rawdevs[dev_id];
259 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
260 return (*dev->dev_ops->dump)(dev, f);
264 xstats_get_count(uint16_t dev_id)
266 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
268 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_names, -ENOTSUP);
269 return (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
273 rte_rawdev_xstats_names_get(uint16_t dev_id,
274 struct rte_rawdev_xstats_name *xstats_names,
277 const struct rte_rawdev *dev;
278 int cnt_expected_entries;
280 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
282 cnt_expected_entries = xstats_get_count(dev_id);
284 if (xstats_names == NULL || cnt_expected_entries < 0 ||
285 (int)size < cnt_expected_entries || size <= 0)
286 return cnt_expected_entries;
288 dev = &rte_rawdevs[dev_id];
290 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_names, -ENOTSUP);
291 return (*dev->dev_ops->xstats_get_names)(dev, xstats_names, size);
294 /* retrieve rawdev extended statistics */
296 rte_rawdev_xstats_get(uint16_t dev_id,
297 const unsigned int ids[],
301 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
302 const struct rte_rawdev *dev = &rte_rawdevs[dev_id];
304 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get, -ENOTSUP);
305 return (*dev->dev_ops->xstats_get)(dev, ids, values, n);
309 rte_rawdev_xstats_by_name_get(uint16_t dev_id,
313 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, 0);
314 const struct rte_rawdev *dev = &rte_rawdevs[dev_id];
315 unsigned int temp = -1;
318 *id = (unsigned int)-1;
320 id = &temp; /* driver never gets a NULL value */
322 /* implemented by driver */
323 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_by_name, -ENOTSUP);
324 return (*dev->dev_ops->xstats_get_by_name)(dev, name, id);
328 rte_rawdev_xstats_reset(uint16_t dev_id,
329 const uint32_t ids[], uint32_t nb_ids)
331 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
332 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
334 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_reset, -ENOTSUP);
335 return (*dev->dev_ops->xstats_reset)(dev, ids, nb_ids);
339 rte_rawdev_firmware_status_get(uint16_t dev_id, rte_rawdev_obj_t status_info)
341 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
342 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
344 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_status_get, -ENOTSUP);
345 return (*dev->dev_ops->firmware_status_get)(dev, status_info);
349 rte_rawdev_firmware_version_get(uint16_t dev_id, rte_rawdev_obj_t version_info)
351 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
352 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
354 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_version_get, -ENOTSUP);
355 return (*dev->dev_ops->firmware_version_get)(dev, version_info);
359 rte_rawdev_firmware_load(uint16_t dev_id, rte_rawdev_obj_t firmware_image)
361 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
362 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
367 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_load, -ENOTSUP);
368 return (*dev->dev_ops->firmware_load)(dev, firmware_image);
372 rte_rawdev_firmware_unload(uint16_t dev_id)
374 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
375 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
377 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_load, -ENOTSUP);
378 return (*dev->dev_ops->firmware_unload)(dev);
382 rte_rawdev_selftest(uint16_t dev_id)
384 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
385 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
387 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_selftest, -ENOTSUP);
388 return (*dev->dev_ops->dev_selftest)(dev_id);
392 rte_rawdev_start(uint16_t dev_id)
394 struct rte_rawdev *dev;
397 RTE_RDEV_DEBUG("Start dev_id=%" PRIu8, dev_id);
399 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
400 dev = &rte_rawdevs[dev_id];
401 if (dev->started != 0) {
402 RTE_RDEV_ERR("Device with dev_id=%" PRIu8 "already started",
407 if (dev->dev_ops->dev_start == NULL)
410 diag = (*dev->dev_ops->dev_start)(dev);
420 rte_rawdev_stop(uint16_t dev_id)
422 struct rte_rawdev *dev;
424 RTE_RDEV_DEBUG("Stop dev_id=%" PRIu8, dev_id);
426 RTE_RAWDEV_VALID_DEVID_OR_RET(dev_id);
427 dev = &rte_rawdevs[dev_id];
429 if (dev->started == 0) {
430 RTE_RDEV_ERR("Device with dev_id=%" PRIu8 "already stopped",
435 if (dev->dev_ops->dev_stop == NULL)
438 (*dev->dev_ops->dev_stop)(dev);
445 rte_rawdev_close(uint16_t dev_id)
447 struct rte_rawdev *dev;
449 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
450 dev = &rte_rawdevs[dev_id];
452 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
453 /* Device must be stopped before it can be closed */
454 if (dev->started == 1) {
455 RTE_RDEV_ERR("Device %u must be stopped before closing",
460 return (*dev->dev_ops->dev_close)(dev);
464 rte_rawdev_reset(uint16_t dev_id)
466 struct rte_rawdev *dev;
468 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
469 dev = &rte_rawdevs[dev_id];
471 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
472 /* Reset is not dependent on state of the device */
473 return (*dev->dev_ops->dev_reset)(dev);
476 static inline uint8_t
477 rte_rawdev_find_free_device_index(void)
481 for (dev_id = 0; dev_id < RTE_RAWDEV_MAX_DEVS; dev_id++) {
482 if (rte_rawdevs[dev_id].attached ==
487 return RTE_RAWDEV_MAX_DEVS;
491 rte_rawdev_pmd_allocate(const char *name, size_t dev_priv_size, int socket_id)
493 struct rte_rawdev *rawdev;
496 if (rte_rawdev_pmd_get_named_dev(name) != NULL) {
497 RTE_RDEV_ERR("Event device with name %s already allocated!",
502 dev_id = rte_rawdev_find_free_device_index();
503 if (dev_id == RTE_RAWDEV_MAX_DEVS) {
504 RTE_RDEV_ERR("Reached maximum number of raw devices");
508 rawdev = &rte_rawdevs[dev_id];
510 if (dev_priv_size > 0) {
511 rawdev->dev_private = rte_zmalloc_socket("rawdev private",
515 if (!rawdev->dev_private) {
516 RTE_RDEV_ERR("Unable to allocate memory for rawdev");
521 rawdev->dev_id = dev_id;
522 rawdev->socket_id = socket_id;
524 strlcpy(rawdev->name, name, RTE_RAWDEV_NAME_MAX_LEN);
526 rawdev->attached = RTE_RAWDEV_ATTACHED;
527 rawdev_globals.nb_devs++;
533 rte_rawdev_pmd_release(struct rte_rawdev *rawdev)
540 ret = rte_rawdev_close(rawdev->dev_id);
544 rawdev->attached = RTE_RAWDEV_DETACHED;
545 rawdev_globals.nb_devs--;
548 rawdev->socket_id = 0;
549 rawdev->dev_ops = NULL;
550 if (rawdev->dev_private) {
551 rte_free(rawdev->dev_private);
552 rawdev->dev_private = NULL;
559 handle_dev_list(const char *cmd __rte_unused,
560 const char *params __rte_unused,
561 struct rte_tel_data *d)
565 rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
566 for (i = 0; i < rawdev_globals.nb_devs; i++)
567 if (rte_rawdevices[i].attached == RTE_RAWDEV_ATTACHED)
568 rte_tel_data_add_array_int(d, i);
573 handle_dev_xstats(const char *cmd __rte_unused,
575 struct rte_tel_data *d)
577 uint64_t *rawdev_xstats;
578 struct rte_rawdev_xstats_name *xstat_names;
579 int dev_id, num_xstats, i, ret;
583 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
586 dev_id = strtoul(params, &end_param, 0);
587 if (*end_param != '\0')
589 "Extra parameters passed to rawdev telemetry command, ignoring");
590 if (!rte_rawdev_pmd_is_valid_dev(dev_id))
593 num_xstats = xstats_get_count(dev_id);
597 /* use one malloc for names, stats and ids */
598 rawdev_xstats = malloc((sizeof(uint64_t) +
599 sizeof(struct rte_rawdev_xstats_name) +
600 sizeof(unsigned int)) * num_xstats);
601 if (rawdev_xstats == NULL)
603 xstat_names = (void *)&rawdev_xstats[num_xstats];
604 ids = (void *)&xstat_names[num_xstats];
606 ret = rte_rawdev_xstats_names_get(dev_id, xstat_names, num_xstats);
607 if (ret < 0 || ret > num_xstats) {
612 for (i = 0; i < num_xstats; i++)
615 ret = rte_rawdev_xstats_get(dev_id, ids, rawdev_xstats, num_xstats);
616 if (ret < 0 || ret > num_xstats) {
621 rte_tel_data_start_dict(d);
622 for (i = 0; i < num_xstats; i++)
623 rte_tel_data_add_dict_u64(d, xstat_names[i].name,
630 RTE_LOG_REGISTER_DEFAULT(librawdev_logtype, INFO);
632 RTE_INIT(librawdev_init_telemetry)
634 rte_telemetry_register_cmd("/rawdev/list", handle_dev_list,
635 "Returns list of available rawdev ports. Takes no parameters");
636 rte_telemetry_register_cmd("/rawdev/xstats", handle_dev_xstats,
637 "Returns the xstats for a rawdev port. Parameters: int port_id");