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