eal: export function to get runtime directory
[dpdk.git] / lib / librte_eventdev / rte_eventdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Cavium, Inc
3  */
4
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <errno.h>
11 #include <stdint.h>
12 #include <inttypes.h>
13 #include <sys/types.h>
14 #include <sys/queue.h>
15
16 #include <rte_byteorder.h>
17 #include <rte_log.h>
18 #include <rte_debug.h>
19 #include <rte_dev.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_memzone.h>
23 #include <rte_eal.h>
24 #include <rte_per_lcore.h>
25 #include <rte_lcore.h>
26 #include <rte_atomic.h>
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
29 #include <rte_malloc.h>
30 #include <rte_errno.h>
31 #include <rte_ethdev.h>
32 #include <rte_cryptodev.h>
33 #include <rte_cryptodev_pmd.h>
34
35 #include "rte_eventdev.h"
36 #include "rte_eventdev_pmd.h"
37
38 struct rte_eventdev rte_event_devices[RTE_EVENT_MAX_DEVS];
39
40 struct rte_eventdev *rte_eventdevs = &rte_event_devices[0];
41
42 static struct rte_eventdev_global eventdev_globals = {
43         .nb_devs                = 0
44 };
45
46 struct rte_eventdev_global *rte_eventdev_globals = &eventdev_globals;
47
48 /* Event dev north bound API implementation */
49
50 uint8_t
51 rte_event_dev_count(void)
52 {
53         return rte_eventdev_globals->nb_devs;
54 }
55
56 int
57 rte_event_dev_get_dev_id(const char *name)
58 {
59         int i;
60         uint8_t cmp;
61
62         if (!name)
63                 return -EINVAL;
64
65         for (i = 0; i < rte_eventdev_globals->nb_devs; i++) {
66                 cmp = (strncmp(rte_event_devices[i].data->name, name,
67                                 RTE_EVENTDEV_NAME_MAX_LEN) == 0) ||
68                         (rte_event_devices[i].dev ? (strncmp(
69                                 rte_event_devices[i].dev->driver->name, name,
70                                          RTE_EVENTDEV_NAME_MAX_LEN) == 0) : 0);
71                 if (cmp && (rte_event_devices[i].attached ==
72                                         RTE_EVENTDEV_ATTACHED))
73                         return i;
74         }
75         return -ENODEV;
76 }
77
78 int
79 rte_event_dev_socket_id(uint8_t dev_id)
80 {
81         struct rte_eventdev *dev;
82
83         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
84         dev = &rte_eventdevs[dev_id];
85
86         return dev->data->socket_id;
87 }
88
89 int
90 rte_event_dev_info_get(uint8_t dev_id, struct rte_event_dev_info *dev_info)
91 {
92         struct rte_eventdev *dev;
93
94         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
95         dev = &rte_eventdevs[dev_id];
96
97         if (dev_info == NULL)
98                 return -EINVAL;
99
100         memset(dev_info, 0, sizeof(struct rte_event_dev_info));
101
102         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
103         (*dev->dev_ops->dev_infos_get)(dev, dev_info);
104
105         dev_info->dequeue_timeout_ns = dev->data->dev_conf.dequeue_timeout_ns;
106
107         dev_info->dev = dev->dev;
108         return 0;
109 }
110
111 int
112 rte_event_eth_rx_adapter_caps_get(uint8_t dev_id, uint16_t eth_port_id,
113                                 uint32_t *caps)
114 {
115         struct rte_eventdev *dev;
116
117         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
118         RTE_ETH_VALID_PORTID_OR_ERR_RET(eth_port_id, -EINVAL);
119
120         dev = &rte_eventdevs[dev_id];
121
122         if (caps == NULL)
123                 return -EINVAL;
124         *caps = 0;
125
126         return dev->dev_ops->eth_rx_adapter_caps_get ?
127                                 (*dev->dev_ops->eth_rx_adapter_caps_get)(dev,
128                                                 &rte_eth_devices[eth_port_id],
129                                                 caps)
130                                 : 0;
131 }
132
133 int __rte_experimental
134 rte_event_timer_adapter_caps_get(uint8_t dev_id, uint32_t *caps)
135 {
136         struct rte_eventdev *dev;
137         const struct rte_event_timer_adapter_ops *ops;
138
139         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
140
141         dev = &rte_eventdevs[dev_id];
142
143         if (caps == NULL)
144                 return -EINVAL;
145         *caps = 0;
146
147         return dev->dev_ops->timer_adapter_caps_get ?
148                                 (*dev->dev_ops->timer_adapter_caps_get)(dev,
149                                                                         0,
150                                                                         caps,
151                                                                         &ops)
152                                 : 0;
153 }
154
155 int __rte_experimental
156 rte_event_crypto_adapter_caps_get(uint8_t dev_id, uint8_t cdev_id,
157                                   uint32_t *caps)
158 {
159         struct rte_eventdev *dev;
160         struct rte_cryptodev *cdev;
161
162         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
163         if (!rte_cryptodev_pmd_is_valid_dev(cdev_id))
164                 return -EINVAL;
165
166         dev = &rte_eventdevs[dev_id];
167         cdev = rte_cryptodev_pmd_get_dev(cdev_id);
168
169         if (caps == NULL)
170                 return -EINVAL;
171         *caps = 0;
172
173         return dev->dev_ops->crypto_adapter_caps_get ?
174                 (*dev->dev_ops->crypto_adapter_caps_get)
175                 (dev, cdev, caps) : -ENOTSUP;
176 }
177
178 int __rte_experimental
179 rte_event_eth_tx_adapter_caps_get(uint8_t dev_id, uint16_t eth_port_id,
180                                 uint32_t *caps)
181 {
182         struct rte_eventdev *dev;
183         struct rte_eth_dev *eth_dev;
184
185         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
186         RTE_ETH_VALID_PORTID_OR_ERR_RET(eth_port_id, -EINVAL);
187
188         dev = &rte_eventdevs[dev_id];
189         eth_dev = &rte_eth_devices[eth_port_id];
190
191         if (caps == NULL)
192                 return -EINVAL;
193
194         *caps = 0;
195
196         return dev->dev_ops->eth_tx_adapter_caps_get ?
197                         (*dev->dev_ops->eth_tx_adapter_caps_get)(dev,
198                                                                 eth_dev,
199                                                                 caps)
200                         : 0;
201 }
202
203 static inline int
204 rte_event_dev_queue_config(struct rte_eventdev *dev, uint8_t nb_queues)
205 {
206         uint8_t old_nb_queues = dev->data->nb_queues;
207         struct rte_event_queue_conf *queues_cfg;
208         unsigned int i;
209
210         RTE_EDEV_LOG_DEBUG("Setup %d queues on device %u", nb_queues,
211                          dev->data->dev_id);
212
213         /* First time configuration */
214         if (dev->data->queues_cfg == NULL && nb_queues != 0) {
215                 /* Allocate memory to store queue configuration */
216                 dev->data->queues_cfg = rte_zmalloc_socket(
217                                 "eventdev->data->queues_cfg",
218                                 sizeof(dev->data->queues_cfg[0]) * nb_queues,
219                                 RTE_CACHE_LINE_SIZE, dev->data->socket_id);
220                 if (dev->data->queues_cfg == NULL) {
221                         dev->data->nb_queues = 0;
222                         RTE_EDEV_LOG_ERR("failed to get mem for queue cfg,"
223                                         "nb_queues %u", nb_queues);
224                         return -(ENOMEM);
225                 }
226         /* Re-configure */
227         } else if (dev->data->queues_cfg != NULL && nb_queues != 0) {
228                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
229
230                 for (i = nb_queues; i < old_nb_queues; i++)
231                         (*dev->dev_ops->queue_release)(dev, i);
232
233                 /* Re allocate memory to store queue configuration */
234                 queues_cfg = dev->data->queues_cfg;
235                 queues_cfg = rte_realloc(queues_cfg,
236                                 sizeof(queues_cfg[0]) * nb_queues,
237                                 RTE_CACHE_LINE_SIZE);
238                 if (queues_cfg == NULL) {
239                         RTE_EDEV_LOG_ERR("failed to realloc queue cfg memory,"
240                                                 " nb_queues %u", nb_queues);
241                         return -(ENOMEM);
242                 }
243                 dev->data->queues_cfg = queues_cfg;
244
245                 if (nb_queues > old_nb_queues) {
246                         uint8_t new_qs = nb_queues - old_nb_queues;
247
248                         memset(queues_cfg + old_nb_queues, 0,
249                                 sizeof(queues_cfg[0]) * new_qs);
250                 }
251         } else if (dev->data->queues_cfg != NULL && nb_queues == 0) {
252                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
253
254                 for (i = nb_queues; i < old_nb_queues; i++)
255                         (*dev->dev_ops->queue_release)(dev, i);
256         }
257
258         dev->data->nb_queues = nb_queues;
259         return 0;
260 }
261
262 #define EVENT_QUEUE_SERVICE_PRIORITY_INVALID (0xdead)
263
264 static inline int
265 rte_event_dev_port_config(struct rte_eventdev *dev, uint8_t nb_ports)
266 {
267         uint8_t old_nb_ports = dev->data->nb_ports;
268         void **ports;
269         uint16_t *links_map;
270         struct rte_event_port_conf *ports_cfg;
271         unsigned int i;
272
273         RTE_EDEV_LOG_DEBUG("Setup %d ports on device %u", nb_ports,
274                          dev->data->dev_id);
275
276         /* First time configuration */
277         if (dev->data->ports == NULL && nb_ports != 0) {
278                 dev->data->ports = rte_zmalloc_socket("eventdev->data->ports",
279                                 sizeof(dev->data->ports[0]) * nb_ports,
280                                 RTE_CACHE_LINE_SIZE, dev->data->socket_id);
281                 if (dev->data->ports == NULL) {
282                         dev->data->nb_ports = 0;
283                         RTE_EDEV_LOG_ERR("failed to get mem for port meta data,"
284                                         "nb_ports %u", nb_ports);
285                         return -(ENOMEM);
286                 }
287
288                 /* Allocate memory to store port configurations */
289                 dev->data->ports_cfg =
290                         rte_zmalloc_socket("eventdev->ports_cfg",
291                         sizeof(dev->data->ports_cfg[0]) * nb_ports,
292                         RTE_CACHE_LINE_SIZE, dev->data->socket_id);
293                 if (dev->data->ports_cfg == NULL) {
294                         dev->data->nb_ports = 0;
295                         RTE_EDEV_LOG_ERR("failed to get mem for port cfg,"
296                                         "nb_ports %u", nb_ports);
297                         return -(ENOMEM);
298                 }
299
300                 /* Allocate memory to store queue to port link connection */
301                 dev->data->links_map =
302                         rte_zmalloc_socket("eventdev->links_map",
303                         sizeof(dev->data->links_map[0]) * nb_ports *
304                         RTE_EVENT_MAX_QUEUES_PER_DEV,
305                         RTE_CACHE_LINE_SIZE, dev->data->socket_id);
306                 if (dev->data->links_map == NULL) {
307                         dev->data->nb_ports = 0;
308                         RTE_EDEV_LOG_ERR("failed to get mem for port_map area,"
309                                         "nb_ports %u", nb_ports);
310                         return -(ENOMEM);
311                 }
312                 for (i = 0; i < nb_ports * RTE_EVENT_MAX_QUEUES_PER_DEV; i++)
313                         dev->data->links_map[i] =
314                                 EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
315         } else if (dev->data->ports != NULL && nb_ports != 0) {/* re-config */
316                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_release, -ENOTSUP);
317
318                 ports = dev->data->ports;
319                 ports_cfg = dev->data->ports_cfg;
320                 links_map = dev->data->links_map;
321
322                 for (i = nb_ports; i < old_nb_ports; i++)
323                         (*dev->dev_ops->port_release)(ports[i]);
324
325                 /* Realloc memory for ports */
326                 ports = rte_realloc(ports, sizeof(ports[0]) * nb_ports,
327                                 RTE_CACHE_LINE_SIZE);
328                 if (ports == NULL) {
329                         RTE_EDEV_LOG_ERR("failed to realloc port meta data,"
330                                                 " nb_ports %u", nb_ports);
331                         return -(ENOMEM);
332                 }
333
334                 /* Realloc memory for ports_cfg */
335                 ports_cfg = rte_realloc(ports_cfg,
336                         sizeof(ports_cfg[0]) * nb_ports,
337                         RTE_CACHE_LINE_SIZE);
338                 if (ports_cfg == NULL) {
339                         RTE_EDEV_LOG_ERR("failed to realloc port cfg mem,"
340                                                 " nb_ports %u", nb_ports);
341                         return -(ENOMEM);
342                 }
343
344                 /* Realloc memory to store queue to port link connection */
345                 links_map = rte_realloc(links_map,
346                         sizeof(dev->data->links_map[0]) * nb_ports *
347                         RTE_EVENT_MAX_QUEUES_PER_DEV,
348                         RTE_CACHE_LINE_SIZE);
349                 if (links_map == NULL) {
350                         dev->data->nb_ports = 0;
351                         RTE_EDEV_LOG_ERR("failed to realloc mem for port_map,"
352                                         "nb_ports %u", nb_ports);
353                         return -(ENOMEM);
354                 }
355
356                 if (nb_ports > old_nb_ports) {
357                         uint8_t new_ps = nb_ports - old_nb_ports;
358                         unsigned int old_links_map_end =
359                                 old_nb_ports * RTE_EVENT_MAX_QUEUES_PER_DEV;
360                         unsigned int links_map_end =
361                                 nb_ports * RTE_EVENT_MAX_QUEUES_PER_DEV;
362
363                         memset(ports + old_nb_ports, 0,
364                                 sizeof(ports[0]) * new_ps);
365                         memset(ports_cfg + old_nb_ports, 0,
366                                 sizeof(ports_cfg[0]) * new_ps);
367                         for (i = old_links_map_end; i < links_map_end; i++)
368                                 links_map[i] =
369                                         EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
370                 }
371
372                 dev->data->ports = ports;
373                 dev->data->ports_cfg = ports_cfg;
374                 dev->data->links_map = links_map;
375         } else if (dev->data->ports != NULL && nb_ports == 0) {
376                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_release, -ENOTSUP);
377
378                 ports = dev->data->ports;
379                 for (i = nb_ports; i < old_nb_ports; i++)
380                         (*dev->dev_ops->port_release)(ports[i]);
381         }
382
383         dev->data->nb_ports = nb_ports;
384         return 0;
385 }
386
387 int
388 rte_event_dev_configure(uint8_t dev_id,
389                         const struct rte_event_dev_config *dev_conf)
390 {
391         struct rte_eventdev *dev;
392         struct rte_event_dev_info info;
393         int diag;
394
395         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
396         dev = &rte_eventdevs[dev_id];
397
398         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
399         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
400
401         if (dev->data->dev_started) {
402                 RTE_EDEV_LOG_ERR(
403                     "device %d must be stopped to allow configuration", dev_id);
404                 return -EBUSY;
405         }
406
407         if (dev_conf == NULL)
408                 return -EINVAL;
409
410         (*dev->dev_ops->dev_infos_get)(dev, &info);
411
412         /* Check dequeue_timeout_ns value is in limit */
413         if (!(dev_conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)) {
414                 if (dev_conf->dequeue_timeout_ns &&
415                     (dev_conf->dequeue_timeout_ns < info.min_dequeue_timeout_ns
416                         || dev_conf->dequeue_timeout_ns >
417                                  info.max_dequeue_timeout_ns)) {
418                         RTE_EDEV_LOG_ERR("dev%d invalid dequeue_timeout_ns=%d"
419                         " min_dequeue_timeout_ns=%d max_dequeue_timeout_ns=%d",
420                         dev_id, dev_conf->dequeue_timeout_ns,
421                         info.min_dequeue_timeout_ns,
422                         info.max_dequeue_timeout_ns);
423                         return -EINVAL;
424                 }
425         }
426
427         /* Check nb_events_limit is in limit */
428         if (dev_conf->nb_events_limit > info.max_num_events) {
429                 RTE_EDEV_LOG_ERR("dev%d nb_events_limit=%d > max_num_events=%d",
430                 dev_id, dev_conf->nb_events_limit, info.max_num_events);
431                 return -EINVAL;
432         }
433
434         /* Check nb_event_queues is in limit */
435         if (!dev_conf->nb_event_queues) {
436                 RTE_EDEV_LOG_ERR("dev%d nb_event_queues cannot be zero",
437                                         dev_id);
438                 return -EINVAL;
439         }
440         if (dev_conf->nb_event_queues > info.max_event_queues) {
441                 RTE_EDEV_LOG_ERR("%d nb_event_queues=%d > max_event_queues=%d",
442                 dev_id, dev_conf->nb_event_queues, info.max_event_queues);
443                 return -EINVAL;
444         }
445
446         /* Check nb_event_ports is in limit */
447         if (!dev_conf->nb_event_ports) {
448                 RTE_EDEV_LOG_ERR("dev%d nb_event_ports cannot be zero", dev_id);
449                 return -EINVAL;
450         }
451         if (dev_conf->nb_event_ports > info.max_event_ports) {
452                 RTE_EDEV_LOG_ERR("id%d nb_event_ports=%d > max_event_ports= %d",
453                 dev_id, dev_conf->nb_event_ports, info.max_event_ports);
454                 return -EINVAL;
455         }
456
457         /* Check nb_event_queue_flows is in limit */
458         if (!dev_conf->nb_event_queue_flows) {
459                 RTE_EDEV_LOG_ERR("dev%d nb_flows cannot be zero", dev_id);
460                 return -EINVAL;
461         }
462         if (dev_conf->nb_event_queue_flows > info.max_event_queue_flows) {
463                 RTE_EDEV_LOG_ERR("dev%d nb_flows=%x > max_flows=%x",
464                 dev_id, dev_conf->nb_event_queue_flows,
465                 info.max_event_queue_flows);
466                 return -EINVAL;
467         }
468
469         /* Check nb_event_port_dequeue_depth is in limit */
470         if (!dev_conf->nb_event_port_dequeue_depth) {
471                 RTE_EDEV_LOG_ERR("dev%d nb_dequeue_depth cannot be zero",
472                                         dev_id);
473                 return -EINVAL;
474         }
475         if ((info.event_dev_cap & RTE_EVENT_DEV_CAP_BURST_MODE) &&
476                  (dev_conf->nb_event_port_dequeue_depth >
477                          info.max_event_port_dequeue_depth)) {
478                 RTE_EDEV_LOG_ERR("dev%d nb_dq_depth=%d > max_dq_depth=%d",
479                 dev_id, dev_conf->nb_event_port_dequeue_depth,
480                 info.max_event_port_dequeue_depth);
481                 return -EINVAL;
482         }
483
484         /* Check nb_event_port_enqueue_depth is in limit */
485         if (!dev_conf->nb_event_port_enqueue_depth) {
486                 RTE_EDEV_LOG_ERR("dev%d nb_enqueue_depth cannot be zero",
487                                         dev_id);
488                 return -EINVAL;
489         }
490         if ((info.event_dev_cap & RTE_EVENT_DEV_CAP_BURST_MODE) &&
491                 (dev_conf->nb_event_port_enqueue_depth >
492                          info.max_event_port_enqueue_depth)) {
493                 RTE_EDEV_LOG_ERR("dev%d nb_enq_depth=%d > max_enq_depth=%d",
494                 dev_id, dev_conf->nb_event_port_enqueue_depth,
495                 info.max_event_port_enqueue_depth);
496                 return -EINVAL;
497         }
498
499         /* Copy the dev_conf parameter into the dev structure */
500         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
501
502         /* Setup new number of queues and reconfigure device. */
503         diag = rte_event_dev_queue_config(dev, dev_conf->nb_event_queues);
504         if (diag != 0) {
505                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_queue_config = %d",
506                                 dev_id, diag);
507                 return diag;
508         }
509
510         /* Setup new number of ports and reconfigure device. */
511         diag = rte_event_dev_port_config(dev, dev_conf->nb_event_ports);
512         if (diag != 0) {
513                 rte_event_dev_queue_config(dev, 0);
514                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_port_config = %d",
515                                 dev_id, diag);
516                 return diag;
517         }
518
519         /* Configure the device */
520         diag = (*dev->dev_ops->dev_configure)(dev);
521         if (diag != 0) {
522                 RTE_EDEV_LOG_ERR("dev%d dev_configure = %d", dev_id, diag);
523                 rte_event_dev_queue_config(dev, 0);
524                 rte_event_dev_port_config(dev, 0);
525         }
526
527         dev->data->event_dev_cap = info.event_dev_cap;
528         return diag;
529 }
530
531 static inline int
532 is_valid_queue(struct rte_eventdev *dev, uint8_t queue_id)
533 {
534         if (queue_id < dev->data->nb_queues && queue_id <
535                                 RTE_EVENT_MAX_QUEUES_PER_DEV)
536                 return 1;
537         else
538                 return 0;
539 }
540
541 int
542 rte_event_queue_default_conf_get(uint8_t dev_id, uint8_t queue_id,
543                                  struct rte_event_queue_conf *queue_conf)
544 {
545         struct rte_eventdev *dev;
546
547         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
548         dev = &rte_eventdevs[dev_id];
549
550         if (queue_conf == NULL)
551                 return -EINVAL;
552
553         if (!is_valid_queue(dev, queue_id)) {
554                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
555                 return -EINVAL;
556         }
557
558         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf, -ENOTSUP);
559         memset(queue_conf, 0, sizeof(struct rte_event_queue_conf));
560         (*dev->dev_ops->queue_def_conf)(dev, queue_id, queue_conf);
561         return 0;
562 }
563
564 static inline int
565 is_valid_atomic_queue_conf(const struct rte_event_queue_conf *queue_conf)
566 {
567         if (queue_conf &&
568                 !(queue_conf->event_queue_cfg &
569                   RTE_EVENT_QUEUE_CFG_SINGLE_LINK) &&
570                 ((queue_conf->event_queue_cfg &
571                          RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
572                 (queue_conf->schedule_type
573                         == RTE_SCHED_TYPE_ATOMIC)
574                 ))
575                 return 1;
576         else
577                 return 0;
578 }
579
580 static inline int
581 is_valid_ordered_queue_conf(const struct rte_event_queue_conf *queue_conf)
582 {
583         if (queue_conf &&
584                 !(queue_conf->event_queue_cfg &
585                   RTE_EVENT_QUEUE_CFG_SINGLE_LINK) &&
586                 ((queue_conf->event_queue_cfg &
587                          RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
588                 (queue_conf->schedule_type
589                         == RTE_SCHED_TYPE_ORDERED)
590                 ))
591                 return 1;
592         else
593                 return 0;
594 }
595
596
597 int
598 rte_event_queue_setup(uint8_t dev_id, uint8_t queue_id,
599                       const struct rte_event_queue_conf *queue_conf)
600 {
601         struct rte_eventdev *dev;
602         struct rte_event_queue_conf def_conf;
603
604         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
605         dev = &rte_eventdevs[dev_id];
606
607         if (!is_valid_queue(dev, queue_id)) {
608                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
609                 return -EINVAL;
610         }
611
612         /* Check nb_atomic_flows limit */
613         if (is_valid_atomic_queue_conf(queue_conf)) {
614                 if (queue_conf->nb_atomic_flows == 0 ||
615                     queue_conf->nb_atomic_flows >
616                         dev->data->dev_conf.nb_event_queue_flows) {
617                         RTE_EDEV_LOG_ERR(
618                 "dev%d queue%d Invalid nb_atomic_flows=%d max_flows=%d",
619                         dev_id, queue_id, queue_conf->nb_atomic_flows,
620                         dev->data->dev_conf.nb_event_queue_flows);
621                         return -EINVAL;
622                 }
623         }
624
625         /* Check nb_atomic_order_sequences limit */
626         if (is_valid_ordered_queue_conf(queue_conf)) {
627                 if (queue_conf->nb_atomic_order_sequences == 0 ||
628                     queue_conf->nb_atomic_order_sequences >
629                         dev->data->dev_conf.nb_event_queue_flows) {
630                         RTE_EDEV_LOG_ERR(
631                 "dev%d queue%d Invalid nb_atomic_order_seq=%d max_flows=%d",
632                         dev_id, queue_id, queue_conf->nb_atomic_order_sequences,
633                         dev->data->dev_conf.nb_event_queue_flows);
634                         return -EINVAL;
635                 }
636         }
637
638         if (dev->data->dev_started) {
639                 RTE_EDEV_LOG_ERR(
640                     "device %d must be stopped to allow queue setup", dev_id);
641                 return -EBUSY;
642         }
643
644         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_setup, -ENOTSUP);
645
646         if (queue_conf == NULL) {
647                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf,
648                                         -ENOTSUP);
649                 (*dev->dev_ops->queue_def_conf)(dev, queue_id, &def_conf);
650                 queue_conf = &def_conf;
651         }
652
653         dev->data->queues_cfg[queue_id] = *queue_conf;
654         return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf);
655 }
656
657 static inline int
658 is_valid_port(struct rte_eventdev *dev, uint8_t port_id)
659 {
660         if (port_id < dev->data->nb_ports)
661                 return 1;
662         else
663                 return 0;
664 }
665
666 int
667 rte_event_port_default_conf_get(uint8_t dev_id, uint8_t port_id,
668                                  struct rte_event_port_conf *port_conf)
669 {
670         struct rte_eventdev *dev;
671
672         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
673         dev = &rte_eventdevs[dev_id];
674
675         if (port_conf == NULL)
676                 return -EINVAL;
677
678         if (!is_valid_port(dev, port_id)) {
679                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
680                 return -EINVAL;
681         }
682
683         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf, -ENOTSUP);
684         memset(port_conf, 0, sizeof(struct rte_event_port_conf));
685         (*dev->dev_ops->port_def_conf)(dev, port_id, port_conf);
686         return 0;
687 }
688
689 int
690 rte_event_port_setup(uint8_t dev_id, uint8_t port_id,
691                      const struct rte_event_port_conf *port_conf)
692 {
693         struct rte_eventdev *dev;
694         struct rte_event_port_conf def_conf;
695         int diag;
696
697         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
698         dev = &rte_eventdevs[dev_id];
699
700         if (!is_valid_port(dev, port_id)) {
701                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
702                 return -EINVAL;
703         }
704
705         /* Check new_event_threshold limit */
706         if ((port_conf && !port_conf->new_event_threshold) ||
707                         (port_conf && port_conf->new_event_threshold >
708                                  dev->data->dev_conf.nb_events_limit)) {
709                 RTE_EDEV_LOG_ERR(
710                    "dev%d port%d Invalid event_threshold=%d nb_events_limit=%d",
711                         dev_id, port_id, port_conf->new_event_threshold,
712                         dev->data->dev_conf.nb_events_limit);
713                 return -EINVAL;
714         }
715
716         /* Check dequeue_depth limit */
717         if ((port_conf && !port_conf->dequeue_depth) ||
718                         (port_conf && port_conf->dequeue_depth >
719                 dev->data->dev_conf.nb_event_port_dequeue_depth)) {
720                 RTE_EDEV_LOG_ERR(
721                    "dev%d port%d Invalid dequeue depth=%d max_dequeue_depth=%d",
722                         dev_id, port_id, port_conf->dequeue_depth,
723                         dev->data->dev_conf.nb_event_port_dequeue_depth);
724                 return -EINVAL;
725         }
726
727         /* Check enqueue_depth limit */
728         if ((port_conf && !port_conf->enqueue_depth) ||
729                         (port_conf && port_conf->enqueue_depth >
730                 dev->data->dev_conf.nb_event_port_enqueue_depth)) {
731                 RTE_EDEV_LOG_ERR(
732                    "dev%d port%d Invalid enqueue depth=%d max_enqueue_depth=%d",
733                         dev_id, port_id, port_conf->enqueue_depth,
734                         dev->data->dev_conf.nb_event_port_enqueue_depth);
735                 return -EINVAL;
736         }
737
738         if (port_conf && port_conf->disable_implicit_release &&
739             !(dev->data->event_dev_cap &
740               RTE_EVENT_DEV_CAP_IMPLICIT_RELEASE_DISABLE)) {
741                 RTE_EDEV_LOG_ERR(
742                    "dev%d port%d Implicit release disable not supported",
743                         dev_id, port_id);
744                 return -EINVAL;
745         }
746
747         if (dev->data->dev_started) {
748                 RTE_EDEV_LOG_ERR(
749                     "device %d must be stopped to allow port setup", dev_id);
750                 return -EBUSY;
751         }
752
753         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_setup, -ENOTSUP);
754
755         if (port_conf == NULL) {
756                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf,
757                                         -ENOTSUP);
758                 (*dev->dev_ops->port_def_conf)(dev, port_id, &def_conf);
759                 port_conf = &def_conf;
760         }
761
762         dev->data->ports_cfg[port_id] = *port_conf;
763
764         diag = (*dev->dev_ops->port_setup)(dev, port_id, port_conf);
765
766         /* Unlink all the queues from this port(default state after setup) */
767         if (!diag)
768                 diag = rte_event_port_unlink(dev_id, port_id, NULL, 0);
769
770         if (diag < 0)
771                 return diag;
772
773         return 0;
774 }
775
776 int
777 rte_event_dev_attr_get(uint8_t dev_id, uint32_t attr_id,
778                        uint32_t *attr_value)
779 {
780         struct rte_eventdev *dev;
781
782         if (!attr_value)
783                 return -EINVAL;
784         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
785         dev = &rte_eventdevs[dev_id];
786
787         switch (attr_id) {
788         case RTE_EVENT_DEV_ATTR_PORT_COUNT:
789                 *attr_value = dev->data->nb_ports;
790                 break;
791         case RTE_EVENT_DEV_ATTR_QUEUE_COUNT:
792                 *attr_value = dev->data->nb_queues;
793                 break;
794         case RTE_EVENT_DEV_ATTR_STARTED:
795                 *attr_value = dev->data->dev_started;
796                 break;
797         default:
798                 return -EINVAL;
799         }
800
801         return 0;
802 }
803
804 int
805 rte_event_port_attr_get(uint8_t dev_id, uint8_t port_id, uint32_t attr_id,
806                         uint32_t *attr_value)
807 {
808         struct rte_eventdev *dev;
809
810         if (!attr_value)
811                 return -EINVAL;
812
813         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
814         dev = &rte_eventdevs[dev_id];
815         if (!is_valid_port(dev, port_id)) {
816                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
817                 return -EINVAL;
818         }
819
820         switch (attr_id) {
821         case RTE_EVENT_PORT_ATTR_ENQ_DEPTH:
822                 *attr_value = dev->data->ports_cfg[port_id].enqueue_depth;
823                 break;
824         case RTE_EVENT_PORT_ATTR_DEQ_DEPTH:
825                 *attr_value = dev->data->ports_cfg[port_id].dequeue_depth;
826                 break;
827         case RTE_EVENT_PORT_ATTR_NEW_EVENT_THRESHOLD:
828                 *attr_value = dev->data->ports_cfg[port_id].new_event_threshold;
829                 break;
830         default:
831                 return -EINVAL;
832         };
833         return 0;
834 }
835
836 int
837 rte_event_queue_attr_get(uint8_t dev_id, uint8_t queue_id, uint32_t attr_id,
838                         uint32_t *attr_value)
839 {
840         struct rte_event_queue_conf *conf;
841         struct rte_eventdev *dev;
842
843         if (!attr_value)
844                 return -EINVAL;
845
846         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
847         dev = &rte_eventdevs[dev_id];
848         if (!is_valid_queue(dev, queue_id)) {
849                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
850                 return -EINVAL;
851         }
852
853         conf = &dev->data->queues_cfg[queue_id];
854
855         switch (attr_id) {
856         case RTE_EVENT_QUEUE_ATTR_PRIORITY:
857                 *attr_value = RTE_EVENT_DEV_PRIORITY_NORMAL;
858                 if (dev->data->event_dev_cap & RTE_EVENT_DEV_CAP_QUEUE_QOS)
859                         *attr_value = conf->priority;
860                 break;
861         case RTE_EVENT_QUEUE_ATTR_NB_ATOMIC_FLOWS:
862                 *attr_value = conf->nb_atomic_flows;
863                 break;
864         case RTE_EVENT_QUEUE_ATTR_NB_ATOMIC_ORDER_SEQUENCES:
865                 *attr_value = conf->nb_atomic_order_sequences;
866                 break;
867         case RTE_EVENT_QUEUE_ATTR_EVENT_QUEUE_CFG:
868                 *attr_value = conf->event_queue_cfg;
869                 break;
870         case RTE_EVENT_QUEUE_ATTR_SCHEDULE_TYPE:
871                 if (conf->event_queue_cfg & RTE_EVENT_QUEUE_CFG_ALL_TYPES)
872                         return -EOVERFLOW;
873
874                 *attr_value = conf->schedule_type;
875                 break;
876         default:
877                 return -EINVAL;
878         };
879         return 0;
880 }
881
882 int
883 rte_event_port_link(uint8_t dev_id, uint8_t port_id,
884                     const uint8_t queues[], const uint8_t priorities[],
885                     uint16_t nb_links)
886 {
887         struct rte_eventdev *dev;
888         uint8_t queues_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
889         uint8_t priorities_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
890         uint16_t *links_map;
891         int i, diag;
892
893         RTE_EVENTDEV_VALID_DEVID_OR_ERRNO_RET(dev_id, -EINVAL, 0);
894         dev = &rte_eventdevs[dev_id];
895
896         if (*dev->dev_ops->port_link == NULL) {
897                 RTE_PMD_DEBUG_TRACE("Function not supported\n");
898                 rte_errno = -ENOTSUP;
899                 return 0;
900         }
901
902         if (!is_valid_port(dev, port_id)) {
903                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
904                 rte_errno = -EINVAL;
905                 return 0;
906         }
907
908         if (queues == NULL) {
909                 for (i = 0; i < dev->data->nb_queues; i++)
910                         queues_list[i] = i;
911
912                 queues = queues_list;
913                 nb_links = dev->data->nb_queues;
914         }
915
916         if (priorities == NULL) {
917                 for (i = 0; i < nb_links; i++)
918                         priorities_list[i] = RTE_EVENT_DEV_PRIORITY_NORMAL;
919
920                 priorities = priorities_list;
921         }
922
923         for (i = 0; i < nb_links; i++)
924                 if (queues[i] >= dev->data->nb_queues) {
925                         rte_errno = -EINVAL;
926                         return 0;
927                 }
928
929         diag = (*dev->dev_ops->port_link)(dev, dev->data->ports[port_id],
930                                                 queues, priorities, nb_links);
931         if (diag < 0)
932                 return diag;
933
934         links_map = dev->data->links_map;
935         /* Point links_map to this port specific area */
936         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
937         for (i = 0; i < diag; i++)
938                 links_map[queues[i]] = (uint8_t)priorities[i];
939
940         return diag;
941 }
942
943 int
944 rte_event_port_unlink(uint8_t dev_id, uint8_t port_id,
945                       uint8_t queues[], uint16_t nb_unlinks)
946 {
947         struct rte_eventdev *dev;
948         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
949         int i, diag, j;
950         uint16_t *links_map;
951
952         RTE_EVENTDEV_VALID_DEVID_OR_ERRNO_RET(dev_id, -EINVAL, 0);
953         dev = &rte_eventdevs[dev_id];
954
955         if (*dev->dev_ops->port_unlink == NULL) {
956                 RTE_PMD_DEBUG_TRACE("Function not supported\n");
957                 rte_errno = -ENOTSUP;
958                 return 0;
959         }
960
961         if (!is_valid_port(dev, port_id)) {
962                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
963                 rte_errno = -EINVAL;
964                 return 0;
965         }
966
967         links_map = dev->data->links_map;
968         /* Point links_map to this port specific area */
969         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
970
971         if (queues == NULL) {
972                 j = 0;
973                 for (i = 0; i < dev->data->nb_queues; i++) {
974                         if (links_map[i] !=
975                                         EVENT_QUEUE_SERVICE_PRIORITY_INVALID) {
976                                 all_queues[j] = i;
977                                 j++;
978                         }
979                 }
980                 queues = all_queues;
981         } else {
982                 for (j = 0; j < nb_unlinks; j++) {
983                         if (links_map[queues[j]] ==
984                                         EVENT_QUEUE_SERVICE_PRIORITY_INVALID)
985                                 break;
986                 }
987         }
988
989         nb_unlinks = j;
990         for (i = 0; i < nb_unlinks; i++)
991                 if (queues[i] >= dev->data->nb_queues) {
992                         rte_errno = -EINVAL;
993                         return 0;
994                 }
995
996         diag = (*dev->dev_ops->port_unlink)(dev, dev->data->ports[port_id],
997                                         queues, nb_unlinks);
998
999         if (diag < 0)
1000                 return diag;
1001
1002         for (i = 0; i < diag; i++)
1003                 links_map[queues[i]] = EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
1004
1005         return diag;
1006 }
1007
1008 int __rte_experimental
1009 rte_event_port_unlinks_in_progress(uint8_t dev_id, uint8_t port_id)
1010 {
1011         struct rte_eventdev *dev;
1012
1013         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1014         dev = &rte_eventdevs[dev_id];
1015         if (!is_valid_port(dev, port_id)) {
1016                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
1017                 return -EINVAL;
1018         }
1019
1020         /* Return 0 if the PMD does not implement unlinks in progress.
1021          * This allows PMDs which handle unlink synchronously to not implement
1022          * this function at all.
1023          */
1024         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_unlinks_in_progress, 0);
1025
1026         return (*dev->dev_ops->port_unlinks_in_progress)(dev,
1027                         dev->data->ports[port_id]);
1028 }
1029
1030 int
1031 rte_event_port_links_get(uint8_t dev_id, uint8_t port_id,
1032                          uint8_t queues[], uint8_t priorities[])
1033 {
1034         struct rte_eventdev *dev;
1035         uint16_t *links_map;
1036         int i, count = 0;
1037
1038         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1039         dev = &rte_eventdevs[dev_id];
1040         if (!is_valid_port(dev, port_id)) {
1041                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
1042                 return -EINVAL;
1043         }
1044
1045         links_map = dev->data->links_map;
1046         /* Point links_map to this port specific area */
1047         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
1048         for (i = 0; i < dev->data->nb_queues; i++) {
1049                 if (links_map[i] != EVENT_QUEUE_SERVICE_PRIORITY_INVALID) {
1050                         queues[count] = i;
1051                         priorities[count] = (uint8_t)links_map[i];
1052                         ++count;
1053                 }
1054         }
1055         return count;
1056 }
1057
1058 int
1059 rte_event_dequeue_timeout_ticks(uint8_t dev_id, uint64_t ns,
1060                                  uint64_t *timeout_ticks)
1061 {
1062         struct rte_eventdev *dev;
1063
1064         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1065         dev = &rte_eventdevs[dev_id];
1066         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timeout_ticks, -ENOTSUP);
1067
1068         if (timeout_ticks == NULL)
1069                 return -EINVAL;
1070
1071         return (*dev->dev_ops->timeout_ticks)(dev, ns, timeout_ticks);
1072 }
1073
1074 int
1075 rte_event_dev_service_id_get(uint8_t dev_id, uint32_t *service_id)
1076 {
1077         struct rte_eventdev *dev;
1078
1079         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1080         dev = &rte_eventdevs[dev_id];
1081
1082         if (service_id == NULL)
1083                 return -EINVAL;
1084
1085         if (dev->data->service_inited)
1086                 *service_id = dev->data->service_id;
1087
1088         return dev->data->service_inited ? 0 : -ESRCH;
1089 }
1090
1091 int
1092 rte_event_dev_dump(uint8_t dev_id, FILE *f)
1093 {
1094         struct rte_eventdev *dev;
1095
1096         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1097         dev = &rte_eventdevs[dev_id];
1098         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
1099
1100         (*dev->dev_ops->dump)(dev, f);
1101         return 0;
1102
1103 }
1104
1105 static int
1106 xstats_get_count(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
1107                 uint8_t queue_port_id)
1108 {
1109         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1110         if (dev->dev_ops->xstats_get_names != NULL)
1111                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
1112                                                         queue_port_id,
1113                                                         NULL, NULL, 0);
1114         return 0;
1115 }
1116
1117 int
1118 rte_event_dev_xstats_names_get(uint8_t dev_id,
1119                 enum rte_event_dev_xstats_mode mode, uint8_t queue_port_id,
1120                 struct rte_event_dev_xstats_name *xstats_names,
1121                 unsigned int *ids, unsigned int size)
1122 {
1123         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
1124         const int cnt_expected_entries = xstats_get_count(dev_id, mode,
1125                                                           queue_port_id);
1126         if (xstats_names == NULL || cnt_expected_entries < 0 ||
1127                         (int)size < cnt_expected_entries)
1128                 return cnt_expected_entries;
1129
1130         /* dev_id checked above */
1131         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1132
1133         if (dev->dev_ops->xstats_get_names != NULL)
1134                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
1135                                 queue_port_id, xstats_names, ids, size);
1136
1137         return -ENOTSUP;
1138 }
1139
1140 /* retrieve eventdev extended statistics */
1141 int
1142 rte_event_dev_xstats_get(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
1143                 uint8_t queue_port_id, const unsigned int ids[],
1144                 uint64_t values[], unsigned int n)
1145 {
1146         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
1147         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1148
1149         /* implemented by the driver */
1150         if (dev->dev_ops->xstats_get != NULL)
1151                 return (*dev->dev_ops->xstats_get)(dev, mode, queue_port_id,
1152                                 ids, values, n);
1153         return -ENOTSUP;
1154 }
1155
1156 uint64_t
1157 rte_event_dev_xstats_by_name_get(uint8_t dev_id, const char *name,
1158                 unsigned int *id)
1159 {
1160         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, 0);
1161         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1162         unsigned int temp = -1;
1163
1164         if (id != NULL)
1165                 *id = (unsigned int)-1;
1166         else
1167                 id = &temp; /* ensure driver never gets a NULL value */
1168
1169         /* implemented by driver */
1170         if (dev->dev_ops->xstats_get_by_name != NULL)
1171                 return (*dev->dev_ops->xstats_get_by_name)(dev, name, id);
1172         return -ENOTSUP;
1173 }
1174
1175 int rte_event_dev_xstats_reset(uint8_t dev_id,
1176                 enum rte_event_dev_xstats_mode mode, int16_t queue_port_id,
1177                 const uint32_t ids[], uint32_t nb_ids)
1178 {
1179         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1180         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1181
1182         if (dev->dev_ops->xstats_reset != NULL)
1183                 return (*dev->dev_ops->xstats_reset)(dev, mode, queue_port_id,
1184                                                         ids, nb_ids);
1185         return -ENOTSUP;
1186 }
1187
1188 int rte_event_dev_selftest(uint8_t dev_id)
1189 {
1190         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1191         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1192
1193         if (dev->dev_ops->dev_selftest != NULL)
1194                 return (*dev->dev_ops->dev_selftest)();
1195         return -ENOTSUP;
1196 }
1197
1198 int
1199 rte_event_dev_start(uint8_t dev_id)
1200 {
1201         struct rte_eventdev *dev;
1202         int diag;
1203
1204         RTE_EDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1205
1206         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1207         dev = &rte_eventdevs[dev_id];
1208         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1209
1210         if (dev->data->dev_started != 0) {
1211                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already started",
1212                         dev_id);
1213                 return 0;
1214         }
1215
1216         diag = (*dev->dev_ops->dev_start)(dev);
1217         if (diag == 0)
1218                 dev->data->dev_started = 1;
1219         else
1220                 return diag;
1221
1222         return 0;
1223 }
1224
1225 int
1226 rte_event_dev_stop_flush_callback_register(uint8_t dev_id,
1227                 eventdev_stop_flush_t callback, void *userdata)
1228 {
1229         struct rte_eventdev *dev;
1230
1231         RTE_EDEV_LOG_DEBUG("Stop flush register dev_id=%" PRIu8, dev_id);
1232
1233         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1234         dev = &rte_eventdevs[dev_id];
1235
1236         dev->dev_ops->dev_stop_flush = callback;
1237         dev->data->dev_stop_flush_arg = userdata;
1238
1239         return 0;
1240 }
1241
1242 void
1243 rte_event_dev_stop(uint8_t dev_id)
1244 {
1245         struct rte_eventdev *dev;
1246
1247         RTE_EDEV_LOG_DEBUG("Stop dev_id=%" PRIu8, dev_id);
1248
1249         RTE_EVENTDEV_VALID_DEVID_OR_RET(dev_id);
1250         dev = &rte_eventdevs[dev_id];
1251         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1252
1253         if (dev->data->dev_started == 0) {
1254                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already stopped",
1255                         dev_id);
1256                 return;
1257         }
1258
1259         dev->data->dev_started = 0;
1260         (*dev->dev_ops->dev_stop)(dev);
1261 }
1262
1263 int
1264 rte_event_dev_close(uint8_t dev_id)
1265 {
1266         struct rte_eventdev *dev;
1267
1268         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1269         dev = &rte_eventdevs[dev_id];
1270         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1271
1272         /* Device must be stopped before it can be closed */
1273         if (dev->data->dev_started == 1) {
1274                 RTE_EDEV_LOG_ERR("Device %u must be stopped before closing",
1275                                 dev_id);
1276                 return -EBUSY;
1277         }
1278
1279         return (*dev->dev_ops->dev_close)(dev);
1280 }
1281
1282 static inline int
1283 rte_eventdev_data_alloc(uint8_t dev_id, struct rte_eventdev_data **data,
1284                 int socket_id)
1285 {
1286         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1287         const struct rte_memzone *mz;
1288         int n;
1289
1290         /* Generate memzone name */
1291         n = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u", dev_id);
1292         if (n >= (int)sizeof(mz_name))
1293                 return -EINVAL;
1294
1295         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1296                 mz = rte_memzone_reserve(mz_name,
1297                                 sizeof(struct rte_eventdev_data),
1298                                 socket_id, 0);
1299         } else
1300                 mz = rte_memzone_lookup(mz_name);
1301
1302         if (mz == NULL)
1303                 return -ENOMEM;
1304
1305         *data = mz->addr;
1306         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1307                 memset(*data, 0, sizeof(struct rte_eventdev_data));
1308
1309         return 0;
1310 }
1311
1312 static inline uint8_t
1313 rte_eventdev_find_free_device_index(void)
1314 {
1315         uint8_t dev_id;
1316
1317         for (dev_id = 0; dev_id < RTE_EVENT_MAX_DEVS; dev_id++) {
1318                 if (rte_eventdevs[dev_id].attached ==
1319                                 RTE_EVENTDEV_DETACHED)
1320                         return dev_id;
1321         }
1322         return RTE_EVENT_MAX_DEVS;
1323 }
1324
1325 static uint16_t
1326 rte_event_tx_adapter_enqueue(__rte_unused void *port,
1327                         __rte_unused struct rte_event ev[],
1328                         __rte_unused uint16_t nb_events)
1329 {
1330         rte_errno = ENOTSUP;
1331         return 0;
1332 }
1333
1334 struct rte_eventdev *
1335 rte_event_pmd_allocate(const char *name, int socket_id)
1336 {
1337         struct rte_eventdev *eventdev;
1338         uint8_t dev_id;
1339
1340         if (rte_event_pmd_get_named_dev(name) != NULL) {
1341                 RTE_EDEV_LOG_ERR("Event device with name %s already "
1342                                 "allocated!", name);
1343                 return NULL;
1344         }
1345
1346         dev_id = rte_eventdev_find_free_device_index();
1347         if (dev_id == RTE_EVENT_MAX_DEVS) {
1348                 RTE_EDEV_LOG_ERR("Reached maximum number of event devices");
1349                 return NULL;
1350         }
1351
1352         eventdev = &rte_eventdevs[dev_id];
1353
1354         eventdev->txa_enqueue = rte_event_tx_adapter_enqueue;
1355
1356         if (eventdev->data == NULL) {
1357                 struct rte_eventdev_data *eventdev_data = NULL;
1358
1359                 int retval = rte_eventdev_data_alloc(dev_id, &eventdev_data,
1360                                 socket_id);
1361
1362                 if (retval < 0 || eventdev_data == NULL)
1363                         return NULL;
1364
1365                 eventdev->data = eventdev_data;
1366
1367                 snprintf(eventdev->data->name, RTE_EVENTDEV_NAME_MAX_LEN,
1368                                 "%s", name);
1369
1370                 eventdev->data->dev_id = dev_id;
1371                 eventdev->data->socket_id = socket_id;
1372                 eventdev->data->dev_started = 0;
1373
1374                 eventdev->attached = RTE_EVENTDEV_ATTACHED;
1375
1376                 eventdev_globals.nb_devs++;
1377         }
1378
1379         return eventdev;
1380 }
1381
1382 int
1383 rte_event_pmd_release(struct rte_eventdev *eventdev)
1384 {
1385         int ret;
1386         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1387         const struct rte_memzone *mz;
1388
1389         if (eventdev == NULL)
1390                 return -EINVAL;
1391
1392         eventdev->attached = RTE_EVENTDEV_DETACHED;
1393         eventdev_globals.nb_devs--;
1394
1395         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1396                 rte_free(eventdev->data->dev_private);
1397
1398                 /* Generate memzone name */
1399                 ret = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u",
1400                                 eventdev->data->dev_id);
1401                 if (ret >= (int)sizeof(mz_name))
1402                         return -EINVAL;
1403
1404                 mz = rte_memzone_lookup(mz_name);
1405                 if (mz == NULL)
1406                         return -ENOMEM;
1407
1408                 ret = rte_memzone_free(mz);
1409                 if (ret)
1410                         return ret;
1411         }
1412
1413         eventdev->data = NULL;
1414         return 0;
1415 }