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