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