9328cda1b0851254b44c98ae2e19ee657c7872c1
[dpdk.git] / lib / librte_eventdev / rte_eventdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016 Cavium networks. 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 networks 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         uint8_t *queues_prio;
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_prio == NULL && nb_queues != 0) {
143                 /* Allocate memory to store queue priority */
144                 dev->data->queues_prio = rte_zmalloc_socket(
145                                 "eventdev->data->queues_prio",
146                                 sizeof(dev->data->queues_prio[0]) * nb_queues,
147                                 RTE_CACHE_LINE_SIZE, dev->data->socket_id);
148                 if (dev->data->queues_prio == NULL) {
149                         dev->data->nb_queues = 0;
150                         RTE_EDEV_LOG_ERR("failed to get mem for queue priority,"
151                                         "nb_queues %u", nb_queues);
152                         return -(ENOMEM);
153                 }
154         /* Re-configure */
155         } else if (dev->data->queues_prio != 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 priority */
162                 queues_prio = dev->data->queues_prio;
163                 queues_prio = rte_realloc(queues_prio,
164                                 sizeof(queues_prio[0]) * nb_queues,
165                                 RTE_CACHE_LINE_SIZE);
166                 if (queues_prio == NULL) {
167                         RTE_EDEV_LOG_ERR("failed to realloc queue priority,"
168                                                 " nb_queues %u", nb_queues);
169                         return -(ENOMEM);
170                 }
171                 dev->data->queues_prio = queues_prio;
172
173                 if (nb_queues > old_nb_queues) {
174                         uint8_t new_qs = nb_queues - old_nb_queues;
175
176                         memset(queues_prio + old_nb_queues, 0,
177                                 sizeof(queues_prio[0]) * new_qs);
178                 }
179         } else if (dev->data->queues_prio != 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 (dev->data->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 < info.min_dequeue_timeout_ns
370                         || dev_conf->dequeue_timeout_ns >
371                                  info.max_dequeue_timeout_ns) {
372                         RTE_EDEV_LOG_ERR("dev%d invalid dequeue_timeout_ns=%d"
373                         " min_dequeue_timeout_ns=%d max_dequeue_timeout_ns=%d",
374                         dev_id, dev_conf->dequeue_timeout_ns,
375                         info.min_dequeue_timeout_ns,
376                         info.max_dequeue_timeout_ns);
377                         return -EINVAL;
378                 }
379         }
380
381         /* Check nb_events_limit is in limit */
382         if (dev_conf->nb_events_limit > info.max_num_events) {
383                 RTE_EDEV_LOG_ERR("dev%d nb_events_limit=%d > max_num_events=%d",
384                 dev_id, dev_conf->nb_events_limit, info.max_num_events);
385                 return -EINVAL;
386         }
387
388         /* Check nb_event_queues is in limit */
389         if (!dev_conf->nb_event_queues) {
390                 RTE_EDEV_LOG_ERR("dev%d nb_event_queues cannot be zero",
391                                         dev_id);
392                 return -EINVAL;
393         }
394         if (dev_conf->nb_event_queues > info.max_event_queues) {
395                 RTE_EDEV_LOG_ERR("%d nb_event_queues=%d > max_event_queues=%d",
396                 dev_id, dev_conf->nb_event_queues, info.max_event_queues);
397                 return -EINVAL;
398         }
399
400         /* Check nb_event_ports is in limit */
401         if (!dev_conf->nb_event_ports) {
402                 RTE_EDEV_LOG_ERR("dev%d nb_event_ports cannot be zero", dev_id);
403                 return -EINVAL;
404         }
405         if (dev_conf->nb_event_ports > info.max_event_ports) {
406                 RTE_EDEV_LOG_ERR("id%d nb_event_ports=%d > max_event_ports= %d",
407                 dev_id, dev_conf->nb_event_ports, info.max_event_ports);
408                 return -EINVAL;
409         }
410
411         /* Check nb_event_queue_flows is in limit */
412         if (!dev_conf->nb_event_queue_flows) {
413                 RTE_EDEV_LOG_ERR("dev%d nb_flows cannot be zero", dev_id);
414                 return -EINVAL;
415         }
416         if (dev_conf->nb_event_queue_flows > info.max_event_queue_flows) {
417                 RTE_EDEV_LOG_ERR("dev%d nb_flows=%x > max_flows=%x",
418                 dev_id, dev_conf->nb_event_queue_flows,
419                 info.max_event_queue_flows);
420                 return -EINVAL;
421         }
422
423         /* Check nb_event_port_dequeue_depth is in limit */
424         if (!dev_conf->nb_event_port_dequeue_depth) {
425                 RTE_EDEV_LOG_ERR("dev%d nb_dequeue_depth cannot be zero",
426                                         dev_id);
427                 return -EINVAL;
428         }
429         if (dev_conf->nb_event_port_dequeue_depth >
430                          info.max_event_port_dequeue_depth) {
431                 RTE_EDEV_LOG_ERR("dev%d nb_dq_depth=%d > max_dq_depth=%d",
432                 dev_id, dev_conf->nb_event_port_dequeue_depth,
433                 info.max_event_port_dequeue_depth);
434                 return -EINVAL;
435         }
436
437         /* Check nb_event_port_enqueue_depth is in limit */
438         if (!dev_conf->nb_event_port_enqueue_depth) {
439                 RTE_EDEV_LOG_ERR("dev%d nb_enqueue_depth cannot be zero",
440                                         dev_id);
441                 return -EINVAL;
442         }
443         if (dev_conf->nb_event_port_enqueue_depth >
444                          info.max_event_port_enqueue_depth) {
445                 RTE_EDEV_LOG_ERR("dev%d nb_enq_depth=%d > max_enq_depth=%d",
446                 dev_id, dev_conf->nb_event_port_enqueue_depth,
447                 info.max_event_port_enqueue_depth);
448                 return -EINVAL;
449         }
450
451         /* Copy the dev_conf parameter into the dev structure */
452         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
453
454         /* Setup new number of queues and reconfigure device. */
455         diag = rte_event_dev_queue_config(dev, dev_conf->nb_event_queues);
456         if (diag != 0) {
457                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_queue_config = %d",
458                                 dev_id, diag);
459                 return diag;
460         }
461
462         /* Setup new number of ports and reconfigure device. */
463         diag = rte_event_dev_port_config(dev, dev_conf->nb_event_ports);
464         if (diag != 0) {
465                 rte_event_dev_queue_config(dev, 0);
466                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_port_config = %d",
467                                 dev_id, diag);
468                 return diag;
469         }
470
471         /* Configure the device */
472         diag = (*dev->dev_ops->dev_configure)(dev);
473         if (diag != 0) {
474                 RTE_EDEV_LOG_ERR("dev%d dev_configure = %d", dev_id, diag);
475                 rte_event_dev_queue_config(dev, 0);
476                 rte_event_dev_port_config(dev, 0);
477         }
478
479         dev->data->event_dev_cap = info.event_dev_cap;
480         return diag;
481 }
482
483 static inline int
484 is_valid_queue(struct rte_eventdev *dev, uint8_t queue_id)
485 {
486         if (queue_id < dev->data->nb_queues && queue_id <
487                                 RTE_EVENT_MAX_QUEUES_PER_DEV)
488                 return 1;
489         else
490                 return 0;
491 }
492
493 int
494 rte_event_queue_default_conf_get(uint8_t dev_id, uint8_t queue_id,
495                                  struct rte_event_queue_conf *queue_conf)
496 {
497         struct rte_eventdev *dev;
498
499         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
500         dev = &rte_eventdevs[dev_id];
501
502         if (queue_conf == NULL)
503                 return -EINVAL;
504
505         if (!is_valid_queue(dev, queue_id)) {
506                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
507                 return -EINVAL;
508         }
509
510         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf, -ENOTSUP);
511         memset(queue_conf, 0, sizeof(struct rte_event_queue_conf));
512         (*dev->dev_ops->queue_def_conf)(dev, queue_id, queue_conf);
513         return 0;
514 }
515
516 static inline int
517 is_valid_atomic_queue_conf(const struct rte_event_queue_conf *queue_conf)
518 {
519         if (queue_conf && (
520                 ((queue_conf->event_queue_cfg &
521                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
522                         == RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
523                 ((queue_conf->event_queue_cfg &
524                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
525                         == RTE_EVENT_QUEUE_CFG_ATOMIC_ONLY)
526                 ))
527                 return 1;
528         else
529                 return 0;
530 }
531
532 static inline int
533 is_valid_ordered_queue_conf(const struct rte_event_queue_conf *queue_conf)
534 {
535         if (queue_conf && (
536                 ((queue_conf->event_queue_cfg &
537                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
538                         == RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
539                 ((queue_conf->event_queue_cfg &
540                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
541                         == RTE_EVENT_QUEUE_CFG_ORDERED_ONLY)
542                 ))
543                 return 1;
544         else
545                 return 0;
546 }
547
548
549 int
550 rte_event_queue_setup(uint8_t dev_id, uint8_t queue_id,
551                       const struct rte_event_queue_conf *queue_conf)
552 {
553         struct rte_eventdev *dev;
554         struct rte_event_queue_conf def_conf;
555
556         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
557         dev = &rte_eventdevs[dev_id];
558
559         if (!is_valid_queue(dev, queue_id)) {
560                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
561                 return -EINVAL;
562         }
563
564         /* Check nb_atomic_flows limit */
565         if (is_valid_atomic_queue_conf(queue_conf)) {
566                 if (queue_conf->nb_atomic_flows == 0 ||
567                     queue_conf->nb_atomic_flows >
568                         dev->data->dev_conf.nb_event_queue_flows) {
569                         RTE_EDEV_LOG_ERR(
570                 "dev%d queue%d Invalid nb_atomic_flows=%d max_flows=%d",
571                         dev_id, queue_id, queue_conf->nb_atomic_flows,
572                         dev->data->dev_conf.nb_event_queue_flows);
573                         return -EINVAL;
574                 }
575         }
576
577         /* Check nb_atomic_order_sequences limit */
578         if (is_valid_ordered_queue_conf(queue_conf)) {
579                 if (queue_conf->nb_atomic_order_sequences == 0 ||
580                     queue_conf->nb_atomic_order_sequences >
581                         dev->data->dev_conf.nb_event_queue_flows) {
582                         RTE_EDEV_LOG_ERR(
583                 "dev%d queue%d Invalid nb_atomic_order_seq=%d max_flows=%d",
584                         dev_id, queue_id, queue_conf->nb_atomic_order_sequences,
585                         dev->data->dev_conf.nb_event_queue_flows);
586                         return -EINVAL;
587                 }
588         }
589
590         if (dev->data->dev_started) {
591                 RTE_EDEV_LOG_ERR(
592                     "device %d must be stopped to allow queue setup", dev_id);
593                 return -EBUSY;
594         }
595
596         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_setup, -ENOTSUP);
597
598         if (queue_conf == NULL) {
599                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf,
600                                         -ENOTSUP);
601                 (*dev->dev_ops->queue_def_conf)(dev, queue_id, &def_conf);
602                 queue_conf = &def_conf;
603         }
604
605         dev->data->queues_prio[queue_id] = queue_conf->priority;
606         return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf);
607 }
608
609 uint8_t
610 rte_event_queue_count(uint8_t dev_id)
611 {
612         struct rte_eventdev *dev;
613
614         dev = &rte_eventdevs[dev_id];
615         return dev->data->nb_queues;
616 }
617
618 uint8_t
619 rte_event_queue_priority(uint8_t dev_id, uint8_t queue_id)
620 {
621         struct rte_eventdev *dev;
622
623         dev = &rte_eventdevs[dev_id];
624         if (dev->data->event_dev_cap & RTE_EVENT_DEV_CAP_QUEUE_QOS)
625                 return dev->data->queues_prio[queue_id];
626         else
627                 return RTE_EVENT_DEV_PRIORITY_NORMAL;
628 }
629
630 static inline int
631 is_valid_port(struct rte_eventdev *dev, uint8_t port_id)
632 {
633         if (port_id < dev->data->nb_ports)
634                 return 1;
635         else
636                 return 0;
637 }
638
639 int
640 rte_event_port_default_conf_get(uint8_t dev_id, uint8_t port_id,
641                                  struct rte_event_port_conf *port_conf)
642 {
643         struct rte_eventdev *dev;
644
645         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
646         dev = &rte_eventdevs[dev_id];
647
648         if (port_conf == NULL)
649                 return -EINVAL;
650
651         if (!is_valid_port(dev, port_id)) {
652                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
653                 return -EINVAL;
654         }
655
656         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf, -ENOTSUP);
657         memset(port_conf, 0, sizeof(struct rte_event_port_conf));
658         (*dev->dev_ops->port_def_conf)(dev, port_id, port_conf);
659         return 0;
660 }
661
662 int
663 rte_event_port_setup(uint8_t dev_id, uint8_t port_id,
664                      const struct rte_event_port_conf *port_conf)
665 {
666         struct rte_eventdev *dev;
667         struct rte_event_port_conf def_conf;
668         int diag;
669
670         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
671         dev = &rte_eventdevs[dev_id];
672
673         if (!is_valid_port(dev, port_id)) {
674                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
675                 return -EINVAL;
676         }
677
678         /* Check new_event_threshold limit */
679         if ((port_conf && !port_conf->new_event_threshold) ||
680                         (port_conf && port_conf->new_event_threshold >
681                                  dev->data->dev_conf.nb_events_limit)) {
682                 RTE_EDEV_LOG_ERR(
683                    "dev%d port%d Invalid event_threshold=%d nb_events_limit=%d",
684                         dev_id, port_id, port_conf->new_event_threshold,
685                         dev->data->dev_conf.nb_events_limit);
686                 return -EINVAL;
687         }
688
689         /* Check dequeue_depth limit */
690         if ((port_conf && !port_conf->dequeue_depth) ||
691                         (port_conf && port_conf->dequeue_depth >
692                 dev->data->dev_conf.nb_event_port_dequeue_depth)) {
693                 RTE_EDEV_LOG_ERR(
694                    "dev%d port%d Invalid dequeue depth=%d max_dequeue_depth=%d",
695                         dev_id, port_id, port_conf->dequeue_depth,
696                         dev->data->dev_conf.nb_event_port_dequeue_depth);
697                 return -EINVAL;
698         }
699
700         /* Check enqueue_depth limit */
701         if ((port_conf && !port_conf->enqueue_depth) ||
702                         (port_conf && port_conf->enqueue_depth >
703                 dev->data->dev_conf.nb_event_port_enqueue_depth)) {
704                 RTE_EDEV_LOG_ERR(
705                    "dev%d port%d Invalid enqueue depth=%d max_enqueue_depth=%d",
706                         dev_id, port_id, port_conf->enqueue_depth,
707                         dev->data->dev_conf.nb_event_port_enqueue_depth);
708                 return -EINVAL;
709         }
710
711         if (dev->data->dev_started) {
712                 RTE_EDEV_LOG_ERR(
713                     "device %d must be stopped to allow port setup", dev_id);
714                 return -EBUSY;
715         }
716
717         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_setup, -ENOTSUP);
718
719         if (port_conf == NULL) {
720                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf,
721                                         -ENOTSUP);
722                 (*dev->dev_ops->port_def_conf)(dev, port_id, &def_conf);
723                 port_conf = &def_conf;
724         }
725
726         dev->data->ports_dequeue_depth[port_id] =
727                         port_conf->dequeue_depth;
728         dev->data->ports_enqueue_depth[port_id] =
729                         port_conf->enqueue_depth;
730
731         diag = (*dev->dev_ops->port_setup)(dev, port_id, port_conf);
732
733         /* Unlink all the queues from this port(default state after setup) */
734         if (!diag)
735                 diag = rte_event_port_unlink(dev_id, port_id, NULL, 0);
736
737         if (diag < 0)
738                 return diag;
739
740         return 0;
741 }
742
743 uint8_t
744 rte_event_port_dequeue_depth(uint8_t dev_id, uint8_t port_id)
745 {
746         struct rte_eventdev *dev;
747
748         dev = &rte_eventdevs[dev_id];
749         return dev->data->ports_dequeue_depth[port_id];
750 }
751
752 uint8_t
753 rte_event_port_enqueue_depth(uint8_t dev_id, uint8_t port_id)
754 {
755         struct rte_eventdev *dev;
756
757         dev = &rte_eventdevs[dev_id];
758         return dev->data->ports_enqueue_depth[port_id];
759 }
760
761 uint8_t
762 rte_event_port_count(uint8_t dev_id)
763 {
764         struct rte_eventdev *dev;
765
766         dev = &rte_eventdevs[dev_id];
767         return dev->data->nb_ports;
768 }
769
770 int
771 rte_event_port_link(uint8_t dev_id, uint8_t port_id,
772                     const uint8_t queues[], const uint8_t priorities[],
773                     uint16_t nb_links)
774 {
775         struct rte_eventdev *dev;
776         uint8_t queues_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
777         uint8_t priorities_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
778         uint16_t *links_map;
779         int i, diag;
780
781         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
782         dev = &rte_eventdevs[dev_id];
783         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_link, -ENOTSUP);
784
785         if (!is_valid_port(dev, port_id)) {
786                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
787                 return -EINVAL;
788         }
789
790         if (queues == NULL) {
791                 for (i = 0; i < dev->data->nb_queues; i++)
792                         queues_list[i] = i;
793
794                 queues = queues_list;
795                 nb_links = dev->data->nb_queues;
796         }
797
798         if (priorities == NULL) {
799                 for (i = 0; i < nb_links; i++)
800                         priorities_list[i] = RTE_EVENT_DEV_PRIORITY_NORMAL;
801
802                 priorities = priorities_list;
803         }
804
805         for (i = 0; i < nb_links; i++)
806                 if (queues[i] >= dev->data->nb_queues)
807                         return -EINVAL;
808
809         diag = (*dev->dev_ops->port_link)(dev, dev->data->ports[port_id],
810                                                 queues, priorities, nb_links);
811         if (diag < 0)
812                 return diag;
813
814         links_map = dev->data->links_map;
815         /* Point links_map to this port specific area */
816         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
817         for (i = 0; i < diag; i++)
818                 links_map[queues[i]] = (uint8_t)priorities[i];
819
820         return diag;
821 }
822
823 int
824 rte_event_port_unlink(uint8_t dev_id, uint8_t port_id,
825                       uint8_t queues[], uint16_t nb_unlinks)
826 {
827         struct rte_eventdev *dev;
828         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
829         int i, diag;
830         uint16_t *links_map;
831
832         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
833         dev = &rte_eventdevs[dev_id];
834         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_unlink, -ENOTSUP);
835
836         if (!is_valid_port(dev, port_id)) {
837                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
838                 return -EINVAL;
839         }
840
841         if (queues == NULL) {
842                 for (i = 0; i < dev->data->nb_queues; i++)
843                         all_queues[i] = i;
844                 queues = all_queues;
845                 nb_unlinks = dev->data->nb_queues;
846         }
847
848         for (i = 0; i < nb_unlinks; i++)
849                 if (queues[i] >= dev->data->nb_queues)
850                         return -EINVAL;
851
852         diag = (*dev->dev_ops->port_unlink)(dev, dev->data->ports[port_id],
853                                         queues, nb_unlinks);
854
855         if (diag < 0)
856                 return diag;
857
858         links_map = dev->data->links_map;
859         /* Point links_map to this port specific area */
860         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
861         for (i = 0; i < diag; i++)
862                 links_map[queues[i]] = EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
863
864         return diag;
865 }
866
867 int
868 rte_event_port_links_get(uint8_t dev_id, uint8_t port_id,
869                          uint8_t queues[], uint8_t priorities[])
870 {
871         struct rte_eventdev *dev;
872         uint16_t *links_map;
873         int i, count = 0;
874
875         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
876         dev = &rte_eventdevs[dev_id];
877         if (!is_valid_port(dev, port_id)) {
878                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
879                 return -EINVAL;
880         }
881
882         links_map = dev->data->links_map;
883         /* Point links_map to this port specific area */
884         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
885         for (i = 0; i < dev->data->nb_queues; i++) {
886                 if (links_map[i] != EVENT_QUEUE_SERVICE_PRIORITY_INVALID) {
887                         queues[count] = i;
888                         priorities[count] = (uint8_t)links_map[i];
889                         ++count;
890                 }
891         }
892         return count;
893 }
894
895 int
896 rte_event_dequeue_timeout_ticks(uint8_t dev_id, uint64_t ns,
897                                  uint64_t *timeout_ticks)
898 {
899         struct rte_eventdev *dev;
900
901         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
902         dev = &rte_eventdevs[dev_id];
903         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timeout_ticks, -ENOTSUP);
904
905         if (timeout_ticks == NULL)
906                 return -EINVAL;
907
908         return (*dev->dev_ops->timeout_ticks)(dev, ns, timeout_ticks);
909 }
910
911 int
912 rte_event_dev_dump(uint8_t dev_id, FILE *f)
913 {
914         struct rte_eventdev *dev;
915
916         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
917         dev = &rte_eventdevs[dev_id];
918         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
919
920         (*dev->dev_ops->dump)(dev, f);
921         return 0;
922
923 }
924
925 static int
926 xstats_get_count(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
927                 uint8_t queue_port_id)
928 {
929         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
930         if (dev->dev_ops->xstats_get_names != NULL)
931                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
932                                                         queue_port_id,
933                                                         NULL, NULL, 0);
934         return 0;
935 }
936
937 int
938 rte_event_dev_xstats_names_get(uint8_t dev_id,
939                 enum rte_event_dev_xstats_mode mode, uint8_t queue_port_id,
940                 struct rte_event_dev_xstats_name *xstats_names,
941                 unsigned int *ids, unsigned int size)
942 {
943         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
944         const int cnt_expected_entries = xstats_get_count(dev_id, mode,
945                                                           queue_port_id);
946         if (xstats_names == NULL || cnt_expected_entries < 0 ||
947                         (int)size < cnt_expected_entries)
948                 return cnt_expected_entries;
949
950         /* dev_id checked above */
951         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
952
953         if (dev->dev_ops->xstats_get_names != NULL)
954                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
955                                 queue_port_id, xstats_names, ids, size);
956
957         return -ENOTSUP;
958 }
959
960 /* retrieve eventdev extended statistics */
961 int
962 rte_event_dev_xstats_get(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
963                 uint8_t queue_port_id, const unsigned int ids[],
964                 uint64_t values[], unsigned int n)
965 {
966         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
967         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
968
969         /* implemented by the driver */
970         if (dev->dev_ops->xstats_get != NULL)
971                 return (*dev->dev_ops->xstats_get)(dev, mode, queue_port_id,
972                                 ids, values, n);
973         return -ENOTSUP;
974 }
975
976 uint64_t
977 rte_event_dev_xstats_by_name_get(uint8_t dev_id, const char *name,
978                 unsigned int *id)
979 {
980         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, 0);
981         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
982         unsigned int temp = -1;
983
984         if (id != NULL)
985                 *id = (unsigned int)-1;
986         else
987                 id = &temp; /* ensure driver never gets a NULL value */
988
989         /* implemented by driver */
990         if (dev->dev_ops->xstats_get_by_name != NULL)
991                 return (*dev->dev_ops->xstats_get_by_name)(dev, name, id);
992         return -ENOTSUP;
993 }
994
995 int rte_event_dev_xstats_reset(uint8_t dev_id,
996                 enum rte_event_dev_xstats_mode mode, int16_t queue_port_id,
997                 const uint32_t ids[], uint32_t nb_ids)
998 {
999         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1000         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1001
1002         if (dev->dev_ops->xstats_reset != NULL)
1003                 return (*dev->dev_ops->xstats_reset)(dev, mode, queue_port_id,
1004                                                         ids, nb_ids);
1005         return -ENOTSUP;
1006 }
1007
1008 int
1009 rte_event_dev_start(uint8_t dev_id)
1010 {
1011         struct rte_eventdev *dev;
1012         int diag;
1013
1014         RTE_EDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1015
1016         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1017         dev = &rte_eventdevs[dev_id];
1018         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1019
1020         if (dev->data->dev_started != 0) {
1021                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already started",
1022                         dev_id);
1023                 return 0;
1024         }
1025
1026         diag = (*dev->dev_ops->dev_start)(dev);
1027         if (diag == 0)
1028                 dev->data->dev_started = 1;
1029         else
1030                 return diag;
1031
1032         return 0;
1033 }
1034
1035 void
1036 rte_event_dev_stop(uint8_t dev_id)
1037 {
1038         struct rte_eventdev *dev;
1039
1040         RTE_EDEV_LOG_DEBUG("Stop dev_id=%" PRIu8, dev_id);
1041
1042         RTE_EVENTDEV_VALID_DEVID_OR_RET(dev_id);
1043         dev = &rte_eventdevs[dev_id];
1044         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1045
1046         if (dev->data->dev_started == 0) {
1047                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already stopped",
1048                         dev_id);
1049                 return;
1050         }
1051
1052         dev->data->dev_started = 0;
1053         (*dev->dev_ops->dev_stop)(dev);
1054 }
1055
1056 int
1057 rte_event_dev_close(uint8_t dev_id)
1058 {
1059         struct rte_eventdev *dev;
1060
1061         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1062         dev = &rte_eventdevs[dev_id];
1063         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1064
1065         /* Device must be stopped before it can be closed */
1066         if (dev->data->dev_started == 1) {
1067                 RTE_EDEV_LOG_ERR("Device %u must be stopped before closing",
1068                                 dev_id);
1069                 return -EBUSY;
1070         }
1071
1072         return (*dev->dev_ops->dev_close)(dev);
1073 }
1074
1075 static inline int
1076 rte_eventdev_data_alloc(uint8_t dev_id, struct rte_eventdev_data **data,
1077                 int socket_id)
1078 {
1079         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1080         const struct rte_memzone *mz;
1081         int n;
1082
1083         /* Generate memzone name */
1084         n = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u", dev_id);
1085         if (n >= (int)sizeof(mz_name))
1086                 return -EINVAL;
1087
1088         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1089                 mz = rte_memzone_reserve(mz_name,
1090                                 sizeof(struct rte_eventdev_data),
1091                                 socket_id, 0);
1092         } else
1093                 mz = rte_memzone_lookup(mz_name);
1094
1095         if (mz == NULL)
1096                 return -ENOMEM;
1097
1098         *data = mz->addr;
1099         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1100                 memset(*data, 0, sizeof(struct rte_eventdev_data));
1101
1102         return 0;
1103 }
1104
1105 static inline uint8_t
1106 rte_eventdev_find_free_device_index(void)
1107 {
1108         uint8_t dev_id;
1109
1110         for (dev_id = 0; dev_id < RTE_EVENT_MAX_DEVS; dev_id++) {
1111                 if (rte_eventdevs[dev_id].attached ==
1112                                 RTE_EVENTDEV_DETACHED)
1113                         return dev_id;
1114         }
1115         return RTE_EVENT_MAX_DEVS;
1116 }
1117
1118 struct rte_eventdev *
1119 rte_event_pmd_allocate(const char *name, int socket_id)
1120 {
1121         struct rte_eventdev *eventdev;
1122         uint8_t dev_id;
1123
1124         if (rte_event_pmd_get_named_dev(name) != NULL) {
1125                 RTE_EDEV_LOG_ERR("Event device with name %s already "
1126                                 "allocated!", name);
1127                 return NULL;
1128         }
1129
1130         dev_id = rte_eventdev_find_free_device_index();
1131         if (dev_id == RTE_EVENT_MAX_DEVS) {
1132                 RTE_EDEV_LOG_ERR("Reached maximum number of event devices");
1133                 return NULL;
1134         }
1135
1136         eventdev = &rte_eventdevs[dev_id];
1137
1138         if (eventdev->data == NULL) {
1139                 struct rte_eventdev_data *eventdev_data = NULL;
1140
1141                 int retval = rte_eventdev_data_alloc(dev_id, &eventdev_data,
1142                                 socket_id);
1143
1144                 if (retval < 0 || eventdev_data == NULL)
1145                         return NULL;
1146
1147                 eventdev->data = eventdev_data;
1148
1149                 snprintf(eventdev->data->name, RTE_EVENTDEV_NAME_MAX_LEN,
1150                                 "%s", name);
1151
1152                 eventdev->data->dev_id = dev_id;
1153                 eventdev->data->socket_id = socket_id;
1154                 eventdev->data->dev_started = 0;
1155
1156                 eventdev->attached = RTE_EVENTDEV_ATTACHED;
1157
1158                 eventdev_globals.nb_devs++;
1159         }
1160
1161         return eventdev;
1162 }
1163
1164 int
1165 rte_event_pmd_release(struct rte_eventdev *eventdev)
1166 {
1167         int ret;
1168         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1169         const struct rte_memzone *mz;
1170
1171         if (eventdev == NULL)
1172                 return -EINVAL;
1173
1174         eventdev->attached = RTE_EVENTDEV_DETACHED;
1175         eventdev_globals.nb_devs--;
1176
1177         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1178                 rte_free(eventdev->data->dev_private);
1179
1180                 /* Generate memzone name */
1181                 ret = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u",
1182                                 eventdev->data->dev_id);
1183                 if (ret >= (int)sizeof(mz_name))
1184                         return -EINVAL;
1185
1186                 mz = rte_memzone_lookup(mz_name);
1187                 if (mz == NULL)
1188                         return -ENOMEM;
1189
1190                 ret = rte_memzone_free(mz);
1191                 if (ret)
1192                         return ret;
1193         }
1194
1195         eventdev->data = NULL;
1196         return 0;
1197 }
1198
1199 struct rte_eventdev *
1200 rte_event_pmd_vdev_init(const char *name, size_t dev_private_size,
1201                 int socket_id)
1202 {
1203         struct rte_eventdev *eventdev;
1204
1205         /* Allocate device structure */
1206         eventdev = rte_event_pmd_allocate(name, socket_id);
1207         if (eventdev == NULL)
1208                 return NULL;
1209
1210         /* Allocate private device structure */
1211         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1212                 eventdev->data->dev_private =
1213                                 rte_zmalloc_socket("eventdev device private",
1214                                                 dev_private_size,
1215                                                 RTE_CACHE_LINE_SIZE,
1216                                                 socket_id);
1217
1218                 if (eventdev->data->dev_private == NULL)
1219                         rte_panic("Cannot allocate memzone for private device"
1220                                         " data");
1221         }
1222
1223         return eventdev;
1224 }
1225
1226 int
1227 rte_event_pmd_vdev_uninit(const char *name)
1228 {
1229         int ret;
1230         struct rte_eventdev *eventdev;
1231
1232         if (name == NULL)
1233                 return -EINVAL;
1234
1235         eventdev = rte_event_pmd_get_named_dev(name);
1236         if (eventdev == NULL)
1237                 return -ENODEV;
1238
1239         ret = rte_event_dev_close(eventdev->data->dev_id);
1240         if (ret < 0)
1241                 return ret;
1242
1243         /* Free the event device */
1244         rte_event_pmd_release(eventdev);
1245
1246         return 0;
1247 }