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)
83 struct rte_rawdev *rawdev;
85 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
86 RTE_FUNC_PTR_OR_ERR_RET(dev_info, -EINVAL);
88 rawdev = &rte_rawdevs[dev_id];
90 if (dev_info->dev_private != NULL) {
91 RTE_FUNC_PTR_OR_ERR_RET(*rawdev->dev_ops->dev_info_get, -ENOTSUP);
92 (*rawdev->dev_ops->dev_info_get)(rawdev, dev_info->dev_private);
95 dev_info->driver_name = rawdev->driver_name;
96 dev_info->device = rawdev->device;
97 dev_info->socket_id = rawdev->socket_id;
103 rte_rawdev_configure(uint16_t dev_id, struct rte_rawdev_info *dev_conf)
105 struct rte_rawdev *dev;
108 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
109 RTE_FUNC_PTR_OR_ERR_RET(dev_conf, -EINVAL);
111 dev = &rte_rawdevs[dev_id];
113 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
117 "device %d must be stopped to allow configuration", dev_id);
121 /* Configure the device */
122 diag = (*dev->dev_ops->dev_configure)(dev, dev_conf->dev_private);
124 RTE_RDEV_ERR("dev%d dev_configure = %d", dev_id, diag);
132 rte_rawdev_queue_conf_get(uint16_t dev_id,
134 rte_rawdev_obj_t queue_conf)
136 struct rte_rawdev *dev;
138 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
139 dev = &rte_rawdevs[dev_id];
141 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf, -ENOTSUP);
142 (*dev->dev_ops->queue_def_conf)(dev, queue_id, queue_conf);
147 rte_rawdev_queue_setup(uint16_t dev_id,
149 rte_rawdev_obj_t queue_conf)
151 struct rte_rawdev *dev;
153 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
154 dev = &rte_rawdevs[dev_id];
156 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_setup, -ENOTSUP);
157 return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf);
161 rte_rawdev_queue_release(uint16_t dev_id, uint16_t queue_id)
163 struct rte_rawdev *dev;
165 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
166 dev = &rte_rawdevs[dev_id];
168 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
169 return (*dev->dev_ops->queue_release)(dev, queue_id);
173 rte_rawdev_queue_count(uint16_t dev_id)
175 struct rte_rawdev *dev;
177 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
178 dev = &rte_rawdevs[dev_id];
180 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_count, -ENOTSUP);
181 return (*dev->dev_ops->queue_count)(dev);
185 rte_rawdev_get_attr(uint16_t dev_id,
186 const char *attr_name,
187 uint64_t *attr_value)
189 struct rte_rawdev *dev;
191 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
192 dev = &rte_rawdevs[dev_id];
194 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->attr_get, -ENOTSUP);
195 return (*dev->dev_ops->attr_get)(dev, attr_name, attr_value);
199 rte_rawdev_set_attr(uint16_t dev_id,
200 const char *attr_name,
201 const uint64_t attr_value)
203 struct rte_rawdev *dev;
205 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
206 dev = &rte_rawdevs[dev_id];
208 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->attr_set, -ENOTSUP);
209 return (*dev->dev_ops->attr_set)(dev, attr_name, attr_value);
213 rte_rawdev_enqueue_buffers(uint16_t dev_id,
214 struct rte_rawdev_buf **buffers,
216 rte_rawdev_obj_t context)
218 struct rte_rawdev *dev;
220 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
221 dev = &rte_rawdevs[dev_id];
223 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->enqueue_bufs, -ENOTSUP);
224 return (*dev->dev_ops->enqueue_bufs)(dev, buffers, count, context);
228 rte_rawdev_dequeue_buffers(uint16_t dev_id,
229 struct rte_rawdev_buf **buffers,
231 rte_rawdev_obj_t context)
233 struct rte_rawdev *dev;
235 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
236 dev = &rte_rawdevs[dev_id];
238 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dequeue_bufs, -ENOTSUP);
239 return (*dev->dev_ops->dequeue_bufs)(dev, buffers, count, context);
243 rte_rawdev_dump(uint16_t dev_id, FILE *f)
245 struct rte_rawdev *dev;
247 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
248 dev = &rte_rawdevs[dev_id];
250 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
251 return (*dev->dev_ops->dump)(dev, f);
255 xstats_get_count(uint16_t dev_id)
257 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
259 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_names, -ENOTSUP);
260 return (*dev->dev_ops->xstats_get_names)(dev, NULL, 0);
264 rte_rawdev_xstats_names_get(uint16_t dev_id,
265 struct rte_rawdev_xstats_name *xstats_names,
268 const struct rte_rawdev *dev;
269 int cnt_expected_entries;
271 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
273 cnt_expected_entries = xstats_get_count(dev_id);
275 if (xstats_names == NULL || cnt_expected_entries < 0 ||
276 (int)size < cnt_expected_entries || size <= 0)
277 return cnt_expected_entries;
279 dev = &rte_rawdevs[dev_id];
281 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_names, -ENOTSUP);
282 return (*dev->dev_ops->xstats_get_names)(dev, xstats_names, size);
285 /* retrieve rawdev extended statistics */
287 rte_rawdev_xstats_get(uint16_t dev_id,
288 const unsigned int ids[],
292 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
293 const struct rte_rawdev *dev = &rte_rawdevs[dev_id];
295 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get, -ENOTSUP);
296 return (*dev->dev_ops->xstats_get)(dev, ids, values, n);
300 rte_rawdev_xstats_by_name_get(uint16_t dev_id,
304 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, 0);
305 const struct rte_rawdev *dev = &rte_rawdevs[dev_id];
306 unsigned int temp = -1;
309 *id = (unsigned int)-1;
311 id = &temp; /* driver never gets a NULL value */
313 /* implemented by driver */
314 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_get_by_name, -ENOTSUP);
315 return (*dev->dev_ops->xstats_get_by_name)(dev, name, id);
319 rte_rawdev_xstats_reset(uint16_t dev_id,
320 const uint32_t ids[], uint32_t nb_ids)
322 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
323 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
325 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->xstats_reset, -ENOTSUP);
326 return (*dev->dev_ops->xstats_reset)(dev, ids, nb_ids);
330 rte_rawdev_firmware_status_get(uint16_t dev_id, rte_rawdev_obj_t status_info)
332 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
333 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
335 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_status_get, -ENOTSUP);
336 return (*dev->dev_ops->firmware_status_get)(dev, status_info);
340 rte_rawdev_firmware_version_get(uint16_t dev_id, rte_rawdev_obj_t version_info)
342 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
343 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
345 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_version_get, -ENOTSUP);
346 return (*dev->dev_ops->firmware_version_get)(dev, version_info);
350 rte_rawdev_firmware_load(uint16_t dev_id, rte_rawdev_obj_t firmware_image)
352 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
353 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
358 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_load, -ENOTSUP);
359 return (*dev->dev_ops->firmware_load)(dev, firmware_image);
363 rte_rawdev_firmware_unload(uint16_t dev_id)
365 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
366 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
368 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->firmware_load, -ENOTSUP);
369 return (*dev->dev_ops->firmware_unload)(dev);
373 rte_rawdev_selftest(uint16_t dev_id)
375 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
376 struct rte_rawdev *dev = &rte_rawdevs[dev_id];
378 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_selftest, -ENOTSUP);
379 return (*dev->dev_ops->dev_selftest)(dev_id);
383 rte_rawdev_start(uint16_t dev_id)
385 struct rte_rawdev *dev;
388 RTE_RDEV_DEBUG("Start dev_id=%" PRIu8, dev_id);
390 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
391 dev = &rte_rawdevs[dev_id];
392 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
394 if (dev->started != 0) {
395 RTE_RDEV_ERR("Device with dev_id=%" PRIu8 "already started",
400 diag = (*dev->dev_ops->dev_start)(dev);
410 rte_rawdev_stop(uint16_t dev_id)
412 struct rte_rawdev *dev;
414 RTE_RDEV_DEBUG("Stop dev_id=%" PRIu8, dev_id);
416 RTE_RAWDEV_VALID_DEVID_OR_RET(dev_id);
417 dev = &rte_rawdevs[dev_id];
419 RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
421 if (dev->started == 0) {
422 RTE_RDEV_ERR("Device with dev_id=%" PRIu8 "already stopped",
427 (*dev->dev_ops->dev_stop)(dev);
432 rte_rawdev_close(uint16_t dev_id)
434 struct rte_rawdev *dev;
436 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
437 dev = &rte_rawdevs[dev_id];
439 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
440 /* Device must be stopped before it can be closed */
441 if (dev->started == 1) {
442 RTE_RDEV_ERR("Device %u must be stopped before closing",
447 return (*dev->dev_ops->dev_close)(dev);
451 rte_rawdev_reset(uint16_t dev_id)
453 struct rte_rawdev *dev;
455 RTE_RAWDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
456 dev = &rte_rawdevs[dev_id];
458 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_reset, -ENOTSUP);
459 /* Reset is not dependent on state of the device */
460 return (*dev->dev_ops->dev_reset)(dev);
463 static inline uint8_t
464 rte_rawdev_find_free_device_index(void)
468 for (dev_id = 0; dev_id < RTE_RAWDEV_MAX_DEVS; dev_id++) {
469 if (rte_rawdevs[dev_id].attached ==
474 return RTE_RAWDEV_MAX_DEVS;
478 rte_rawdev_pmd_allocate(const char *name, size_t dev_priv_size, int socket_id)
480 struct rte_rawdev *rawdev;
483 if (rte_rawdev_pmd_get_named_dev(name) != NULL) {
484 RTE_RDEV_ERR("Event device with name %s already allocated!",
489 dev_id = rte_rawdev_find_free_device_index();
490 if (dev_id == RTE_RAWDEV_MAX_DEVS) {
491 RTE_RDEV_ERR("Reached maximum number of raw devices");
495 rawdev = &rte_rawdevs[dev_id];
497 if (dev_priv_size > 0) {
498 rawdev->dev_private = rte_zmalloc_socket("rawdev private",
502 if (!rawdev->dev_private) {
503 RTE_RDEV_ERR("Unable to allocate memory for rawdev");
508 rawdev->dev_id = dev_id;
509 rawdev->socket_id = socket_id;
511 strlcpy(rawdev->name, name, RTE_RAWDEV_NAME_MAX_LEN);
513 rawdev->attached = RTE_RAWDEV_ATTACHED;
514 rawdev_globals.nb_devs++;
520 rte_rawdev_pmd_release(struct rte_rawdev *rawdev)
527 ret = rte_rawdev_close(rawdev->dev_id);
531 rawdev->attached = RTE_RAWDEV_DETACHED;
532 rawdev_globals.nb_devs--;
535 rawdev->socket_id = 0;
536 rawdev->dev_ops = NULL;
537 if (rawdev->dev_private) {
538 rte_free(rawdev->dev_private);
539 rawdev->dev_private = NULL;
546 handle_dev_list(const char *cmd __rte_unused,
547 const char *params __rte_unused,
548 struct rte_tel_data *d)
552 rte_tel_data_start_array(d, RTE_TEL_INT_VAL);
553 for (i = 0; i < rawdev_globals.nb_devs; i++)
554 if (rte_rawdevices[i].attached == RTE_RAWDEV_ATTACHED)
555 rte_tel_data_add_array_int(d, i);
560 handle_dev_xstats(const char *cmd __rte_unused,
562 struct rte_tel_data *d)
564 uint64_t *rawdev_xstats;
565 struct rte_rawdev_xstats_name *xstat_names;
566 int dev_id, num_xstats, i, ret;
569 if (params == NULL || strlen(params) == 0 || !isdigit(*params))
572 dev_id = atoi(params);
573 if (!rte_rawdev_pmd_is_valid_dev(dev_id))
576 num_xstats = xstats_get_count(dev_id);
580 /* use one malloc for names, stats and ids */
581 rawdev_xstats = malloc((sizeof(uint64_t) +
582 sizeof(struct rte_rawdev_xstats_name) +
583 sizeof(unsigned int)) * num_xstats);
584 if (rawdev_xstats == NULL)
586 xstat_names = (void *)&rawdev_xstats[num_xstats];
587 ids = (void *)&xstat_names[num_xstats];
589 ret = rte_rawdev_xstats_names_get(dev_id, xstat_names, num_xstats);
590 if (ret < 0 || ret > num_xstats) {
595 for (i = 0; i < num_xstats; i++)
598 ret = rte_rawdev_xstats_get(dev_id, ids, rawdev_xstats, num_xstats);
599 if (ret < 0 || ret > num_xstats) {
604 rte_tel_data_start_dict(d);
605 for (i = 0; i < num_xstats; i++)
606 rte_tel_data_add_dict_u64(d, xstat_names[i].name,
613 RTE_LOG_REGISTER(librawdev_logtype, lib.rawdev, INFO);
615 RTE_INIT(librawdev_init_telemetry)
617 rte_telemetry_register_cmd("/rawdev/list", handle_dev_list,
618 "Returns list of available rawdev ports. Takes no parameters");
619 rte_telemetry_register_cmd("/rawdev/xstats", handle_dev_xstats,
620 "Returns the xstats for a rawdev port. Parameters: int port_id");