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