8042988909ba3aec943f4b2a6428e6ae78aea9c2
[dpdk.git] / 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                 queue_conf = &def_conf;
606         }
607
608         dev->data->queues_prio[queue_id] = queue_conf->priority;
609         return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf);
610 }
611
612 uint8_t
613 rte_event_queue_count(uint8_t dev_id)
614 {
615         struct rte_eventdev *dev;
616
617         dev = &rte_eventdevs[dev_id];
618         return dev->data->nb_queues;
619 }
620
621 uint8_t
622 rte_event_queue_priority(uint8_t dev_id, uint8_t queue_id)
623 {
624         struct rte_eventdev *dev;
625
626         dev = &rte_eventdevs[dev_id];
627         if (dev->data->event_dev_cap & RTE_EVENT_DEV_CAP_QUEUE_QOS)
628                 return dev->data->queues_prio[queue_id];
629         else
630                 return RTE_EVENT_DEV_PRIORITY_NORMAL;
631 }
632
633 static inline int
634 is_valid_port(struct rte_eventdev *dev, uint8_t port_id)
635 {
636         if (port_id < dev->data->nb_ports)
637                 return 1;
638         else
639                 return 0;
640 }
641
642 int
643 rte_event_port_default_conf_get(uint8_t dev_id, uint8_t port_id,
644                                  struct rte_event_port_conf *port_conf)
645 {
646         struct rte_eventdev *dev;
647
648         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
649         dev = &rte_eventdevs[dev_id];
650
651         if (port_conf == NULL)
652                 return -EINVAL;
653
654         if (!is_valid_port(dev, port_id)) {
655                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
656                 return -EINVAL;
657         }
658
659         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf, -ENOTSUP);
660         memset(port_conf, 0, sizeof(struct rte_event_port_conf));
661         (*dev->dev_ops->port_def_conf)(dev, port_id, port_conf);
662         return 0;
663 }
664
665 int
666 rte_event_port_setup(uint8_t dev_id, uint8_t port_id,
667                      const struct rte_event_port_conf *port_conf)
668 {
669         struct rte_eventdev *dev;
670         struct rte_event_port_conf def_conf;
671         int diag;
672
673         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
674         dev = &rte_eventdevs[dev_id];
675
676         if (!is_valid_port(dev, port_id)) {
677                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
678                 return -EINVAL;
679         }
680
681         /* Check new_event_threshold limit */
682         if ((port_conf && !port_conf->new_event_threshold) ||
683                         (port_conf && port_conf->new_event_threshold >
684                                  dev->data->dev_conf.nb_events_limit)) {
685                 RTE_EDEV_LOG_ERR(
686                    "dev%d port%d Invalid event_threshold=%d nb_events_limit=%d",
687                         dev_id, port_id, port_conf->new_event_threshold,
688                         dev->data->dev_conf.nb_events_limit);
689                 return -EINVAL;
690         }
691
692         /* Check dequeue_depth limit */
693         if ((port_conf && !port_conf->dequeue_depth) ||
694                         (port_conf && port_conf->dequeue_depth >
695                 dev->data->dev_conf.nb_event_port_dequeue_depth)) {
696                 RTE_EDEV_LOG_ERR(
697                    "dev%d port%d Invalid dequeue depth=%d max_dequeue_depth=%d",
698                         dev_id, port_id, port_conf->dequeue_depth,
699                         dev->data->dev_conf.nb_event_port_dequeue_depth);
700                 return -EINVAL;
701         }
702
703         /* Check enqueue_depth limit */
704         if ((port_conf && !port_conf->enqueue_depth) ||
705                         (port_conf && port_conf->enqueue_depth >
706                 dev->data->dev_conf.nb_event_port_enqueue_depth)) {
707                 RTE_EDEV_LOG_ERR(
708                    "dev%d port%d Invalid enqueue depth=%d max_enqueue_depth=%d",
709                         dev_id, port_id, port_conf->enqueue_depth,
710                         dev->data->dev_conf.nb_event_port_enqueue_depth);
711                 return -EINVAL;
712         }
713
714         if (dev->data->dev_started) {
715                 RTE_EDEV_LOG_ERR(
716                     "device %d must be stopped to allow port setup", dev_id);
717                 return -EBUSY;
718         }
719
720         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_setup, -ENOTSUP);
721
722         if (port_conf == NULL) {
723                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf,
724                                         -ENOTSUP);
725                 (*dev->dev_ops->port_def_conf)(dev, port_id, &def_conf);
726                 port_conf = &def_conf;
727         }
728
729         dev->data->ports_dequeue_depth[port_id] =
730                         port_conf->dequeue_depth;
731         dev->data->ports_enqueue_depth[port_id] =
732                         port_conf->enqueue_depth;
733
734         diag = (*dev->dev_ops->port_setup)(dev, port_id, port_conf);
735
736         /* Unlink all the queues from this port(default state after setup) */
737         if (!diag)
738                 diag = rte_event_port_unlink(dev_id, port_id, NULL, 0);
739
740         if (diag < 0)
741                 return diag;
742
743         return 0;
744 }
745
746 uint8_t
747 rte_event_port_dequeue_depth(uint8_t dev_id, uint8_t port_id)
748 {
749         struct rte_eventdev *dev;
750
751         dev = &rte_eventdevs[dev_id];
752         return dev->data->ports_dequeue_depth[port_id];
753 }
754
755 uint8_t
756 rte_event_port_enqueue_depth(uint8_t dev_id, uint8_t port_id)
757 {
758         struct rte_eventdev *dev;
759
760         dev = &rte_eventdevs[dev_id];
761         return dev->data->ports_enqueue_depth[port_id];
762 }
763
764 uint8_t
765 rte_event_port_count(uint8_t dev_id)
766 {
767         struct rte_eventdev *dev;
768
769         dev = &rte_eventdevs[dev_id];
770         return dev->data->nb_ports;
771 }
772
773 int
774 rte_event_port_link(uint8_t dev_id, uint8_t port_id,
775                     const uint8_t queues[], const uint8_t priorities[],
776                     uint16_t nb_links)
777 {
778         struct rte_eventdev *dev;
779         uint8_t queues_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
780         uint8_t priorities_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
781         uint16_t *links_map;
782         int i, diag;
783
784         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
785         dev = &rte_eventdevs[dev_id];
786         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_link, -ENOTSUP);
787
788         if (!is_valid_port(dev, port_id)) {
789                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
790                 return -EINVAL;
791         }
792
793         if (queues == NULL) {
794                 for (i = 0; i < dev->data->nb_queues; i++)
795                         queues_list[i] = i;
796
797                 queues = queues_list;
798                 nb_links = dev->data->nb_queues;
799         }
800
801         if (priorities == NULL) {
802                 for (i = 0; i < nb_links; i++)
803                         priorities_list[i] = RTE_EVENT_DEV_PRIORITY_NORMAL;
804
805                 priorities = priorities_list;
806         }
807
808         for (i = 0; i < nb_links; i++)
809                 if (queues[i] >= dev->data->nb_queues)
810                         return -EINVAL;
811
812         diag = (*dev->dev_ops->port_link)(dev, dev->data->ports[port_id],
813                                                 queues, priorities, nb_links);
814         if (diag < 0)
815                 return diag;
816
817         links_map = dev->data->links_map;
818         /* Point links_map to this port specific area */
819         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
820         for (i = 0; i < diag; i++)
821                 links_map[queues[i]] = (uint8_t)priorities[i];
822
823         return diag;
824 }
825
826 int
827 rte_event_port_unlink(uint8_t dev_id, uint8_t port_id,
828                       uint8_t queues[], uint16_t nb_unlinks)
829 {
830         struct rte_eventdev *dev;
831         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
832         int i, diag;
833         uint16_t *links_map;
834
835         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
836         dev = &rte_eventdevs[dev_id];
837         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_unlink, -ENOTSUP);
838
839         if (!is_valid_port(dev, port_id)) {
840                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
841                 return -EINVAL;
842         }
843
844         if (queues == NULL) {
845                 for (i = 0; i < dev->data->nb_queues; i++)
846                         all_queues[i] = i;
847                 queues = all_queues;
848                 nb_unlinks = dev->data->nb_queues;
849         }
850
851         for (i = 0; i < nb_unlinks; i++)
852                 if (queues[i] >= dev->data->nb_queues)
853                         return -EINVAL;
854
855         diag = (*dev->dev_ops->port_unlink)(dev, dev->data->ports[port_id],
856                                         queues, nb_unlinks);
857
858         if (diag < 0)
859                 return diag;
860
861         links_map = dev->data->links_map;
862         /* Point links_map to this port specific area */
863         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
864         for (i = 0; i < diag; i++)
865                 links_map[queues[i]] = EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
866
867         return diag;
868 }
869
870 int
871 rte_event_port_links_get(uint8_t dev_id, uint8_t port_id,
872                          uint8_t queues[], uint8_t priorities[])
873 {
874         struct rte_eventdev *dev;
875         uint16_t *links_map;
876         int i, count = 0;
877
878         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
879         dev = &rte_eventdevs[dev_id];
880         if (!is_valid_port(dev, port_id)) {
881                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
882                 return -EINVAL;
883         }
884
885         links_map = dev->data->links_map;
886         /* Point links_map to this port specific area */
887         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
888         for (i = 0; i < dev->data->nb_queues; i++) {
889                 if (links_map[i] != EVENT_QUEUE_SERVICE_PRIORITY_INVALID) {
890                         queues[count] = i;
891                         priorities[count] = (uint8_t)links_map[i];
892                         ++count;
893                 }
894         }
895         return count;
896 }
897
898 int
899 rte_event_dequeue_timeout_ticks(uint8_t dev_id, uint64_t ns,
900                                  uint64_t *timeout_ticks)
901 {
902         struct rte_eventdev *dev;
903
904         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
905         dev = &rte_eventdevs[dev_id];
906         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timeout_ticks, -ENOTSUP);
907
908         if (timeout_ticks == NULL)
909                 return -EINVAL;
910
911         return (*dev->dev_ops->timeout_ticks)(dev, ns, timeout_ticks);
912 }
913
914 int
915 rte_event_dev_dump(uint8_t dev_id, FILE *f)
916 {
917         struct rte_eventdev *dev;
918
919         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
920         dev = &rte_eventdevs[dev_id];
921         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
922
923         (*dev->dev_ops->dump)(dev, f);
924         return 0;
925
926 }
927
928 static int
929 xstats_get_count(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
930                 uint8_t queue_port_id)
931 {
932         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
933         if (dev->dev_ops->xstats_get_names != NULL)
934                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
935                                                         queue_port_id,
936                                                         NULL, NULL, 0);
937         return 0;
938 }
939
940 int
941 rte_event_dev_xstats_names_get(uint8_t dev_id,
942                 enum rte_event_dev_xstats_mode mode, uint8_t queue_port_id,
943                 struct rte_event_dev_xstats_name *xstats_names,
944                 unsigned int *ids, unsigned int size)
945 {
946         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
947         const int cnt_expected_entries = xstats_get_count(dev_id, mode,
948                                                           queue_port_id);
949         if (xstats_names == NULL || cnt_expected_entries < 0 ||
950                         (int)size < cnt_expected_entries)
951                 return cnt_expected_entries;
952
953         /* dev_id checked above */
954         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
955
956         if (dev->dev_ops->xstats_get_names != NULL)
957                 return (*dev->dev_ops->xstats_get_names)(dev, mode,
958                                 queue_port_id, xstats_names, ids, size);
959
960         return -ENOTSUP;
961 }
962
963 /* retrieve eventdev extended statistics */
964 int
965 rte_event_dev_xstats_get(uint8_t dev_id, enum rte_event_dev_xstats_mode mode,
966                 uint8_t queue_port_id, const unsigned int ids[],
967                 uint64_t values[], unsigned int n)
968 {
969         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -ENODEV);
970         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
971
972         /* implemented by the driver */
973         if (dev->dev_ops->xstats_get != NULL)
974                 return (*dev->dev_ops->xstats_get)(dev, mode, queue_port_id,
975                                 ids, values, n);
976         return -ENOTSUP;
977 }
978
979 uint64_t
980 rte_event_dev_xstats_by_name_get(uint8_t dev_id, const char *name,
981                 unsigned int *id)
982 {
983         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, 0);
984         const struct rte_eventdev *dev = &rte_eventdevs[dev_id];
985         unsigned int temp = -1;
986
987         if (id != NULL)
988                 *id = (unsigned int)-1;
989         else
990                 id = &temp; /* ensure driver never gets a NULL value */
991
992         /* implemented by driver */
993         if (dev->dev_ops->xstats_get_by_name != NULL)
994                 return (*dev->dev_ops->xstats_get_by_name)(dev, name, id);
995         return -ENOTSUP;
996 }
997
998 int rte_event_dev_xstats_reset(uint8_t dev_id,
999                 enum rte_event_dev_xstats_mode mode, int16_t queue_port_id,
1000                 const uint32_t ids[], uint32_t nb_ids)
1001 {
1002         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1003         struct rte_eventdev *dev = &rte_eventdevs[dev_id];
1004
1005         if (dev->dev_ops->xstats_reset != NULL)
1006                 return (*dev->dev_ops->xstats_reset)(dev, mode, queue_port_id,
1007                                                         ids, nb_ids);
1008         return -ENOTSUP;
1009 }
1010
1011 int
1012 rte_event_dev_start(uint8_t dev_id)
1013 {
1014         struct rte_eventdev *dev;
1015         int diag;
1016
1017         RTE_EDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
1018
1019         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1020         dev = &rte_eventdevs[dev_id];
1021         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
1022
1023         if (dev->data->dev_started != 0) {
1024                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already started",
1025                         dev_id);
1026                 return 0;
1027         }
1028
1029         diag = (*dev->dev_ops->dev_start)(dev);
1030         if (diag == 0)
1031                 dev->data->dev_started = 1;
1032         else
1033                 return diag;
1034
1035         return 0;
1036 }
1037
1038 void
1039 rte_event_dev_stop(uint8_t dev_id)
1040 {
1041         struct rte_eventdev *dev;
1042
1043         RTE_EDEV_LOG_DEBUG("Stop dev_id=%" PRIu8, dev_id);
1044
1045         RTE_EVENTDEV_VALID_DEVID_OR_RET(dev_id);
1046         dev = &rte_eventdevs[dev_id];
1047         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
1048
1049         if (dev->data->dev_started == 0) {
1050                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already stopped",
1051                         dev_id);
1052                 return;
1053         }
1054
1055         dev->data->dev_started = 0;
1056         (*dev->dev_ops->dev_stop)(dev);
1057 }
1058
1059 int
1060 rte_event_dev_close(uint8_t dev_id)
1061 {
1062         struct rte_eventdev *dev;
1063
1064         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
1065         dev = &rte_eventdevs[dev_id];
1066         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
1067
1068         /* Device must be stopped before it can be closed */
1069         if (dev->data->dev_started == 1) {
1070                 RTE_EDEV_LOG_ERR("Device %u must be stopped before closing",
1071                                 dev_id);
1072                 return -EBUSY;
1073         }
1074
1075         return (*dev->dev_ops->dev_close)(dev);
1076 }
1077
1078 static inline int
1079 rte_eventdev_data_alloc(uint8_t dev_id, struct rte_eventdev_data **data,
1080                 int socket_id)
1081 {
1082         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1083         const struct rte_memzone *mz;
1084         int n;
1085
1086         /* Generate memzone name */
1087         n = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u", dev_id);
1088         if (n >= (int)sizeof(mz_name))
1089                 return -EINVAL;
1090
1091         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1092                 mz = rte_memzone_reserve(mz_name,
1093                                 sizeof(struct rte_eventdev_data),
1094                                 socket_id, 0);
1095         } else
1096                 mz = rte_memzone_lookup(mz_name);
1097
1098         if (mz == NULL)
1099                 return -ENOMEM;
1100
1101         *data = mz->addr;
1102         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1103                 memset(*data, 0, sizeof(struct rte_eventdev_data));
1104
1105         return 0;
1106 }
1107
1108 static inline uint8_t
1109 rte_eventdev_find_free_device_index(void)
1110 {
1111         uint8_t dev_id;
1112
1113         for (dev_id = 0; dev_id < RTE_EVENT_MAX_DEVS; dev_id++) {
1114                 if (rte_eventdevs[dev_id].attached ==
1115                                 RTE_EVENTDEV_DETACHED)
1116                         return dev_id;
1117         }
1118         return RTE_EVENT_MAX_DEVS;
1119 }
1120
1121 struct rte_eventdev *
1122 rte_event_pmd_allocate(const char *name, int socket_id)
1123 {
1124         struct rte_eventdev *eventdev;
1125         uint8_t dev_id;
1126
1127         if (rte_event_pmd_get_named_dev(name) != NULL) {
1128                 RTE_EDEV_LOG_ERR("Event device with name %s already "
1129                                 "allocated!", name);
1130                 return NULL;
1131         }
1132
1133         dev_id = rte_eventdev_find_free_device_index();
1134         if (dev_id == RTE_EVENT_MAX_DEVS) {
1135                 RTE_EDEV_LOG_ERR("Reached maximum number of event devices");
1136                 return NULL;
1137         }
1138
1139         eventdev = &rte_eventdevs[dev_id];
1140
1141         if (eventdev->data == NULL) {
1142                 struct rte_eventdev_data *eventdev_data = NULL;
1143
1144                 int retval = rte_eventdev_data_alloc(dev_id, &eventdev_data,
1145                                 socket_id);
1146
1147                 if (retval < 0 || eventdev_data == NULL)
1148                         return NULL;
1149
1150                 eventdev->data = eventdev_data;
1151
1152                 snprintf(eventdev->data->name, RTE_EVENTDEV_NAME_MAX_LEN,
1153                                 "%s", name);
1154
1155                 eventdev->data->dev_id = dev_id;
1156                 eventdev->data->socket_id = socket_id;
1157                 eventdev->data->dev_started = 0;
1158
1159                 eventdev->attached = RTE_EVENTDEV_ATTACHED;
1160
1161                 eventdev_globals.nb_devs++;
1162         }
1163
1164         return eventdev;
1165 }
1166
1167 int
1168 rte_event_pmd_release(struct rte_eventdev *eventdev)
1169 {
1170         int ret;
1171         char mz_name[RTE_EVENTDEV_NAME_MAX_LEN];
1172         const struct rte_memzone *mz;
1173
1174         if (eventdev == NULL)
1175                 return -EINVAL;
1176
1177         ret = rte_event_dev_close(eventdev->data->dev_id);
1178         if (ret < 0)
1179                 return ret;
1180
1181         eventdev->attached = RTE_EVENTDEV_DETACHED;
1182         eventdev_globals.nb_devs--;
1183
1184         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1185                 rte_free(eventdev->data->dev_private);
1186
1187                 /* Generate memzone name */
1188                 ret = snprintf(mz_name, sizeof(mz_name), "rte_eventdev_data_%u",
1189                                 eventdev->data->dev_id);
1190                 if (ret >= (int)sizeof(mz_name))
1191                         return -EINVAL;
1192
1193                 mz = rte_memzone_lookup(mz_name);
1194                 if (mz == NULL)
1195                         return -ENOMEM;
1196
1197                 ret = rte_memzone_free(mz);
1198                 if (ret)
1199                         return ret;
1200         }
1201
1202         eventdev->data = NULL;
1203         return 0;
1204 }
1205
1206 struct rte_eventdev *
1207 rte_event_pmd_vdev_init(const char *name, size_t dev_private_size,
1208                 int socket_id)
1209 {
1210         struct rte_eventdev *eventdev;
1211
1212         /* Allocate device structure */
1213         eventdev = rte_event_pmd_allocate(name, socket_id);
1214         if (eventdev == NULL)
1215                 return NULL;
1216
1217         /* Allocate private device structure */
1218         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1219                 eventdev->data->dev_private =
1220                                 rte_zmalloc_socket("eventdev device private",
1221                                                 dev_private_size,
1222                                                 RTE_CACHE_LINE_SIZE,
1223                                                 socket_id);
1224
1225                 if (eventdev->data->dev_private == NULL)
1226                         rte_panic("Cannot allocate memzone for private device"
1227                                         " data");
1228         }
1229
1230         return eventdev;
1231 }
1232
1233 int
1234 rte_event_pmd_vdev_uninit(const char *name)
1235 {
1236         struct rte_eventdev *eventdev;
1237
1238         if (name == NULL)
1239                 return -EINVAL;
1240
1241         eventdev = rte_event_pmd_get_named_dev(name);
1242         if (eventdev == NULL)
1243                 return -ENODEV;
1244
1245         /* Free the event device */
1246         rte_event_pmd_release(eventdev);
1247
1248         return 0;
1249 }
1250
1251 int
1252 rte_event_pmd_pci_probe(struct rte_pci_driver *pci_drv,
1253                         struct rte_pci_device *pci_dev)
1254 {
1255         struct rte_eventdev_driver *eventdrv;
1256         struct rte_eventdev *eventdev;
1257
1258         char eventdev_name[RTE_EVENTDEV_NAME_MAX_LEN];
1259
1260         int retval;
1261
1262         eventdrv = (struct rte_eventdev_driver *)pci_drv;
1263         if (eventdrv == NULL)
1264                 return -ENODEV;
1265
1266         rte_eal_pci_device_name(&pci_dev->addr, eventdev_name,
1267                         sizeof(eventdev_name));
1268
1269         eventdev = rte_event_pmd_allocate(eventdev_name,
1270                          pci_dev->device.numa_node);
1271         if (eventdev == NULL)
1272                 return -ENOMEM;
1273
1274         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1275                 eventdev->data->dev_private =
1276                                 rte_zmalloc_socket(
1277                                                 "eventdev private structure",
1278                                                 eventdrv->dev_private_size,
1279                                                 RTE_CACHE_LINE_SIZE,
1280                                                 rte_socket_id());
1281
1282                 if (eventdev->data->dev_private == NULL)
1283                         rte_panic("Cannot allocate memzone for private "
1284                                         "device data");
1285         }
1286
1287         eventdev->dev = &pci_dev->device;
1288         eventdev->driver = eventdrv;
1289
1290         /* Invoke PMD device initialization function */
1291         retval = (*eventdrv->eventdev_init)(eventdev);
1292         if (retval == 0)
1293                 return 0;
1294
1295         RTE_EDEV_LOG_ERR("driver %s: (vendor_id=0x%x device_id=0x%x)"
1296                         " failed", pci_drv->driver.name,
1297                         (unsigned int) pci_dev->id.vendor_id,
1298                         (unsigned int) pci_dev->id.device_id);
1299
1300         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1301                 rte_free(eventdev->data->dev_private);
1302
1303         eventdev->attached = RTE_EVENTDEV_DETACHED;
1304         eventdev_globals.nb_devs--;
1305
1306         return -ENXIO;
1307 }
1308
1309 int
1310 rte_event_pmd_pci_remove(struct rte_pci_device *pci_dev)
1311 {
1312         const struct rte_eventdev_driver *eventdrv;
1313         struct rte_eventdev *eventdev;
1314         char eventdev_name[RTE_EVENTDEV_NAME_MAX_LEN];
1315         int ret;
1316
1317         if (pci_dev == NULL)
1318                 return -EINVAL;
1319
1320         rte_eal_pci_device_name(&pci_dev->addr, eventdev_name,
1321                         sizeof(eventdev_name));
1322
1323         eventdev = rte_event_pmd_get_named_dev(eventdev_name);
1324         if (eventdev == NULL)
1325                 return -ENODEV;
1326
1327         eventdrv = (const struct rte_eventdev_driver *)pci_dev->driver;
1328         if (eventdrv == NULL)
1329                 return -ENODEV;
1330
1331         /* Invoke PMD device un-init function */
1332         if (*eventdrv->eventdev_uninit) {
1333                 ret = (*eventdrv->eventdev_uninit)(eventdev);
1334                 if (ret)
1335                         return ret;
1336         }
1337
1338         /* Free event device */
1339         rte_event_pmd_release(eventdev);
1340
1341         eventdev->dev = NULL;
1342         eventdev->driver = NULL;
1343
1344         return 0;
1345 }