eventdev: implement the northbound APIs
[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->pci_dev = dev->pci_dev;
129         return 0;
130 }
131
132 static inline int
133 rte_event_dev_queue_config(struct rte_eventdev *dev, uint8_t nb_queues)
134 {
135         uint8_t old_nb_queues = dev->data->nb_queues;
136         uint8_t *queues_prio;
137         unsigned int i;
138
139         RTE_EDEV_LOG_DEBUG("Setup %d queues on device %u", nb_queues,
140                          dev->data->dev_id);
141
142         /* First time configuration */
143         if (dev->data->queues_prio == NULL && nb_queues != 0) {
144                 /* Allocate memory to store queue priority */
145                 dev->data->queues_prio = rte_zmalloc_socket(
146                                 "eventdev->data->queues_prio",
147                                 sizeof(dev->data->queues_prio[0]) * nb_queues,
148                                 RTE_CACHE_LINE_SIZE, dev->data->socket_id);
149                 if (dev->data->queues_prio == NULL) {
150                         dev->data->nb_queues = 0;
151                         RTE_EDEV_LOG_ERR("failed to get mem for queue priority,"
152                                         "nb_queues %u", nb_queues);
153                         return -(ENOMEM);
154                 }
155         /* Re-configure */
156         } else if (dev->data->queues_prio != NULL && nb_queues != 0) {
157                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
158
159                 for (i = nb_queues; i < old_nb_queues; i++)
160                         (*dev->dev_ops->queue_release)(dev, i);
161
162                 /* Re allocate memory to store queue priority */
163                 queues_prio = dev->data->queues_prio;
164                 queues_prio = rte_realloc(queues_prio,
165                                 sizeof(queues_prio[0]) * nb_queues,
166                                 RTE_CACHE_LINE_SIZE);
167                 if (queues_prio == NULL) {
168                         RTE_EDEV_LOG_ERR("failed to realloc queue priority,"
169                                                 " nb_queues %u", nb_queues);
170                         return -(ENOMEM);
171                 }
172                 dev->data->queues_prio = queues_prio;
173
174                 if (nb_queues > old_nb_queues) {
175                         uint8_t new_qs = nb_queues - old_nb_queues;
176
177                         memset(queues_prio + old_nb_queues, 0,
178                                 sizeof(queues_prio[0]) * new_qs);
179                 }
180         } else if (dev->data->queues_prio != NULL && nb_queues == 0) {
181                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_release, -ENOTSUP);
182
183                 for (i = nb_queues; i < old_nb_queues; i++)
184                         (*dev->dev_ops->queue_release)(dev, i);
185         }
186
187         dev->data->nb_queues = nb_queues;
188         return 0;
189 }
190
191 static inline int
192 rte_event_dev_port_config(struct rte_eventdev *dev, uint8_t nb_ports)
193 {
194         uint8_t old_nb_ports = dev->data->nb_ports;
195         void **ports;
196         uint16_t *links_map;
197         uint8_t *ports_dequeue_depth;
198         uint8_t *ports_enqueue_depth;
199         unsigned int i;
200
201         RTE_EDEV_LOG_DEBUG("Setup %d ports on device %u", nb_ports,
202                          dev->data->dev_id);
203
204         /* First time configuration */
205         if (dev->data->ports == NULL && nb_ports != 0) {
206                 dev->data->ports = rte_zmalloc_socket("eventdev->data->ports",
207                                 sizeof(dev->data->ports[0]) * nb_ports,
208                                 RTE_CACHE_LINE_SIZE, dev->data->socket_id);
209                 if (dev->data->ports == NULL) {
210                         dev->data->nb_ports = 0;
211                         RTE_EDEV_LOG_ERR("failed to get mem for port meta data,"
212                                         "nb_ports %u", nb_ports);
213                         return -(ENOMEM);
214                 }
215
216                 /* Allocate memory to store ports dequeue depth */
217                 dev->data->ports_dequeue_depth =
218                         rte_zmalloc_socket("eventdev->ports_dequeue_depth",
219                         sizeof(dev->data->ports_dequeue_depth[0]) * nb_ports,
220                         RTE_CACHE_LINE_SIZE, dev->data->socket_id);
221                 if (dev->data->ports_dequeue_depth == NULL) {
222                         dev->data->nb_ports = 0;
223                         RTE_EDEV_LOG_ERR("failed to get mem for port deq meta,"
224                                         "nb_ports %u", nb_ports);
225                         return -(ENOMEM);
226                 }
227
228                 /* Allocate memory to store ports enqueue depth */
229                 dev->data->ports_enqueue_depth =
230                         rte_zmalloc_socket("eventdev->ports_enqueue_depth",
231                         sizeof(dev->data->ports_enqueue_depth[0]) * nb_ports,
232                         RTE_CACHE_LINE_SIZE, dev->data->socket_id);
233                 if (dev->data->ports_enqueue_depth == NULL) {
234                         dev->data->nb_ports = 0;
235                         RTE_EDEV_LOG_ERR("failed to get mem for port enq meta,"
236                                         "nb_ports %u", nb_ports);
237                         return -(ENOMEM);
238                 }
239
240                 /* Allocate memory to store queue to port link connection */
241                 dev->data->links_map =
242                         rte_zmalloc_socket("eventdev->links_map",
243                         sizeof(dev->data->links_map[0]) * nb_ports *
244                         RTE_EVENT_MAX_QUEUES_PER_DEV,
245                         RTE_CACHE_LINE_SIZE, dev->data->socket_id);
246                 if (dev->data->links_map == NULL) {
247                         dev->data->nb_ports = 0;
248                         RTE_EDEV_LOG_ERR("failed to get mem for port_map area,"
249                                         "nb_ports %u", nb_ports);
250                         return -(ENOMEM);
251                 }
252         } else if (dev->data->ports != NULL && nb_ports != 0) {/* re-config */
253                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_release, -ENOTSUP);
254
255                 ports = dev->data->ports;
256                 ports_dequeue_depth = dev->data->ports_dequeue_depth;
257                 ports_enqueue_depth = dev->data->ports_enqueue_depth;
258                 links_map = dev->data->links_map;
259
260                 for (i = nb_ports; i < old_nb_ports; i++)
261                         (*dev->dev_ops->port_release)(ports[i]);
262
263                 /* Realloc memory for ports */
264                 ports = rte_realloc(ports, sizeof(ports[0]) * nb_ports,
265                                 RTE_CACHE_LINE_SIZE);
266                 if (ports == NULL) {
267                         RTE_EDEV_LOG_ERR("failed to realloc port meta data,"
268                                                 " nb_ports %u", nb_ports);
269                         return -(ENOMEM);
270                 }
271
272                 /* Realloc memory for ports_dequeue_depth */
273                 ports_dequeue_depth = rte_realloc(ports_dequeue_depth,
274                         sizeof(ports_dequeue_depth[0]) * nb_ports,
275                         RTE_CACHE_LINE_SIZE);
276                 if (ports_dequeue_depth == NULL) {
277                         RTE_EDEV_LOG_ERR("failed to realloc port dequeue meta,"
278                                                 " nb_ports %u", nb_ports);
279                         return -(ENOMEM);
280                 }
281
282                 /* Realloc memory for ports_enqueue_depth */
283                 ports_enqueue_depth = rte_realloc(ports_enqueue_depth,
284                         sizeof(ports_enqueue_depth[0]) * nb_ports,
285                         RTE_CACHE_LINE_SIZE);
286                 if (ports_enqueue_depth == NULL) {
287                         RTE_EDEV_LOG_ERR("failed to realloc port enqueue meta,"
288                                                 " nb_ports %u", nb_ports);
289                         return -(ENOMEM);
290                 }
291
292                 /* Realloc memory to store queue to port link connection */
293                 links_map = rte_realloc(links_map,
294                         sizeof(dev->data->links_map[0]) * nb_ports *
295                         RTE_EVENT_MAX_QUEUES_PER_DEV,
296                         RTE_CACHE_LINE_SIZE);
297                 if (dev->data->links_map == NULL) {
298                         dev->data->nb_ports = 0;
299                         RTE_EDEV_LOG_ERR("failed to realloc mem for port_map,"
300                                         "nb_ports %u", nb_ports);
301                         return -(ENOMEM);
302                 }
303
304                 if (nb_ports > old_nb_ports) {
305                         uint8_t new_ps = nb_ports - old_nb_ports;
306
307                         memset(ports + old_nb_ports, 0,
308                                 sizeof(ports[0]) * new_ps);
309                         memset(ports_dequeue_depth + old_nb_ports, 0,
310                                 sizeof(ports_dequeue_depth[0]) * new_ps);
311                         memset(ports_enqueue_depth + old_nb_ports, 0,
312                                 sizeof(ports_enqueue_depth[0]) * new_ps);
313                         memset(links_map +
314                                 (old_nb_ports * RTE_EVENT_MAX_QUEUES_PER_DEV),
315                                 0, sizeof(ports_enqueue_depth[0]) * new_ps);
316                 }
317
318                 dev->data->ports = ports;
319                 dev->data->ports_dequeue_depth = ports_dequeue_depth;
320                 dev->data->ports_enqueue_depth = ports_enqueue_depth;
321                 dev->data->links_map = links_map;
322         } else if (dev->data->ports != NULL && nb_ports == 0) {
323                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_release, -ENOTSUP);
324
325                 ports = dev->data->ports;
326                 for (i = nb_ports; i < old_nb_ports; i++)
327                         (*dev->dev_ops->port_release)(ports[i]);
328         }
329
330         dev->data->nb_ports = nb_ports;
331         return 0;
332 }
333
334 int
335 rte_event_dev_configure(uint8_t dev_id,
336                         const struct rte_event_dev_config *dev_conf)
337 {
338         struct rte_eventdev *dev;
339         struct rte_event_dev_info info;
340         int diag;
341
342         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
343         dev = &rte_eventdevs[dev_id];
344
345         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_infos_get, -ENOTSUP);
346         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
347
348         if (dev->data->dev_started) {
349                 RTE_EDEV_LOG_ERR(
350                     "device %d must be stopped to allow configuration", dev_id);
351                 return -EBUSY;
352         }
353
354         if (dev_conf == NULL)
355                 return -EINVAL;
356
357         (*dev->dev_ops->dev_infos_get)(dev, &info);
358
359         /* Check dequeue_timeout_ns value is in limit */
360         if (!dev_conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
361                 if (dev_conf->dequeue_timeout_ns < info.min_dequeue_timeout_ns
362                         || dev_conf->dequeue_timeout_ns >
363                                  info.max_dequeue_timeout_ns) {
364                         RTE_EDEV_LOG_ERR("dev%d invalid dequeue_timeout_ns=%d"
365                         " min_dequeue_timeout_ns=%d max_dequeue_timeout_ns=%d",
366                         dev_id, dev_conf->dequeue_timeout_ns,
367                         info.min_dequeue_timeout_ns,
368                         info.max_dequeue_timeout_ns);
369                         return -EINVAL;
370                 }
371         }
372
373         /* Check nb_events_limit is in limit */
374         if (dev_conf->nb_events_limit > info.max_num_events) {
375                 RTE_EDEV_LOG_ERR("dev%d nb_events_limit=%d > max_num_events=%d",
376                 dev_id, dev_conf->nb_events_limit, info.max_num_events);
377                 return -EINVAL;
378         }
379
380         /* Check nb_event_queues is in limit */
381         if (!dev_conf->nb_event_queues) {
382                 RTE_EDEV_LOG_ERR("dev%d nb_event_queues cannot be zero",
383                                         dev_id);
384                 return -EINVAL;
385         }
386         if (dev_conf->nb_event_queues > info.max_event_queues) {
387                 RTE_EDEV_LOG_ERR("%d nb_event_queues=%d > max_event_queues=%d",
388                 dev_id, dev_conf->nb_event_queues, info.max_event_queues);
389                 return -EINVAL;
390         }
391
392         /* Check nb_event_ports is in limit */
393         if (!dev_conf->nb_event_ports) {
394                 RTE_EDEV_LOG_ERR("dev%d nb_event_ports cannot be zero", dev_id);
395                 return -EINVAL;
396         }
397         if (dev_conf->nb_event_ports > info.max_event_ports) {
398                 RTE_EDEV_LOG_ERR("id%d nb_event_ports=%d > max_event_ports= %d",
399                 dev_id, dev_conf->nb_event_ports, info.max_event_ports);
400                 return -EINVAL;
401         }
402
403         /* Check nb_event_queue_flows is in limit */
404         if (!dev_conf->nb_event_queue_flows) {
405                 RTE_EDEV_LOG_ERR("dev%d nb_flows cannot be zero", dev_id);
406                 return -EINVAL;
407         }
408         if (dev_conf->nb_event_queue_flows > info.max_event_queue_flows) {
409                 RTE_EDEV_LOG_ERR("dev%d nb_flows=%x > max_flows=%x",
410                 dev_id, dev_conf->nb_event_queue_flows,
411                 info.max_event_queue_flows);
412                 return -EINVAL;
413         }
414
415         /* Check nb_event_port_dequeue_depth is in limit */
416         if (!dev_conf->nb_event_port_dequeue_depth) {
417                 RTE_EDEV_LOG_ERR("dev%d nb_dequeue_depth cannot be zero",
418                                         dev_id);
419                 return -EINVAL;
420         }
421         if (dev_conf->nb_event_port_dequeue_depth >
422                          info.max_event_port_dequeue_depth) {
423                 RTE_EDEV_LOG_ERR("dev%d nb_dq_depth=%d > max_dq_depth=%d",
424                 dev_id, dev_conf->nb_event_port_dequeue_depth,
425                 info.max_event_port_dequeue_depth);
426                 return -EINVAL;
427         }
428
429         /* Check nb_event_port_enqueue_depth is in limit */
430         if (!dev_conf->nb_event_port_enqueue_depth) {
431                 RTE_EDEV_LOG_ERR("dev%d nb_enqueue_depth cannot be zero",
432                                         dev_id);
433                 return -EINVAL;
434         }
435         if (dev_conf->nb_event_port_enqueue_depth >
436                          info.max_event_port_enqueue_depth) {
437                 RTE_EDEV_LOG_ERR("dev%d nb_enq_depth=%d > max_enq_depth=%d",
438                 dev_id, dev_conf->nb_event_port_enqueue_depth,
439                 info.max_event_port_enqueue_depth);
440                 return -EINVAL;
441         }
442
443         /* Copy the dev_conf parameter into the dev structure */
444         memcpy(&dev->data->dev_conf, dev_conf, sizeof(dev->data->dev_conf));
445
446         /* Setup new number of queues and reconfigure device. */
447         diag = rte_event_dev_queue_config(dev, dev_conf->nb_event_queues);
448         if (diag != 0) {
449                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_queue_config = %d",
450                                 dev_id, diag);
451                 return diag;
452         }
453
454         /* Setup new number of ports and reconfigure device. */
455         diag = rte_event_dev_port_config(dev, dev_conf->nb_event_ports);
456         if (diag != 0) {
457                 rte_event_dev_queue_config(dev, 0);
458                 RTE_EDEV_LOG_ERR("dev%d rte_event_dev_port_config = %d",
459                                 dev_id, diag);
460                 return diag;
461         }
462
463         /* Configure the device */
464         diag = (*dev->dev_ops->dev_configure)(dev);
465         if (diag != 0) {
466                 RTE_EDEV_LOG_ERR("dev%d dev_configure = %d", dev_id, diag);
467                 rte_event_dev_queue_config(dev, 0);
468                 rte_event_dev_port_config(dev, 0);
469         }
470
471         dev->data->event_dev_cap = info.event_dev_cap;
472         return diag;
473 }
474
475 static inline int
476 is_valid_queue(struct rte_eventdev *dev, uint8_t queue_id)
477 {
478         if (queue_id < dev->data->nb_queues && queue_id <
479                                 RTE_EVENT_MAX_QUEUES_PER_DEV)
480                 return 1;
481         else
482                 return 0;
483 }
484
485 int
486 rte_event_queue_default_conf_get(uint8_t dev_id, uint8_t queue_id,
487                                  struct rte_event_queue_conf *queue_conf)
488 {
489         struct rte_eventdev *dev;
490
491         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
492         dev = &rte_eventdevs[dev_id];
493
494         if (queue_conf == NULL)
495                 return -EINVAL;
496
497         if (!is_valid_queue(dev, queue_id)) {
498                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
499                 return -EINVAL;
500         }
501
502         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf, -ENOTSUP);
503         memset(queue_conf, 0, sizeof(struct rte_event_queue_conf));
504         (*dev->dev_ops->queue_def_conf)(dev, queue_id, queue_conf);
505         return 0;
506 }
507
508 static inline int
509 is_valid_atomic_queue_conf(const struct rte_event_queue_conf *queue_conf)
510 {
511         if (queue_conf && (
512                 ((queue_conf->event_queue_cfg &
513                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
514                         == RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
515                 ((queue_conf->event_queue_cfg &
516                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
517                         == RTE_EVENT_QUEUE_CFG_ATOMIC_ONLY)
518                 ))
519                 return 1;
520         else
521                 return 0;
522 }
523
524 static inline int
525 is_valid_ordered_queue_conf(const struct rte_event_queue_conf *queue_conf)
526 {
527         if (queue_conf && (
528                 ((queue_conf->event_queue_cfg &
529                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
530                         == RTE_EVENT_QUEUE_CFG_ALL_TYPES) ||
531                 ((queue_conf->event_queue_cfg &
532                         RTE_EVENT_QUEUE_CFG_TYPE_MASK)
533                         == RTE_EVENT_QUEUE_CFG_ORDERED_ONLY)
534                 ))
535                 return 1;
536         else
537                 return 0;
538 }
539
540
541 int
542 rte_event_queue_setup(uint8_t dev_id, uint8_t queue_id,
543                       const struct rte_event_queue_conf *queue_conf)
544 {
545         struct rte_eventdev *dev;
546         struct rte_event_queue_conf def_conf;
547
548         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
549         dev = &rte_eventdevs[dev_id];
550
551         if (!is_valid_queue(dev, queue_id)) {
552                 RTE_EDEV_LOG_ERR("Invalid queue_id=%" PRIu8, queue_id);
553                 return -EINVAL;
554         }
555
556         /* Check nb_atomic_flows limit */
557         if (is_valid_atomic_queue_conf(queue_conf)) {
558                 if (queue_conf->nb_atomic_flows == 0 ||
559                     queue_conf->nb_atomic_flows >
560                         dev->data->dev_conf.nb_event_queue_flows) {
561                         RTE_EDEV_LOG_ERR(
562                 "dev%d queue%d Invalid nb_atomic_flows=%d max_flows=%d",
563                         dev_id, queue_id, queue_conf->nb_atomic_flows,
564                         dev->data->dev_conf.nb_event_queue_flows);
565                         return -EINVAL;
566                 }
567         }
568
569         /* Check nb_atomic_order_sequences limit */
570         if (is_valid_ordered_queue_conf(queue_conf)) {
571                 if (queue_conf->nb_atomic_order_sequences == 0 ||
572                     queue_conf->nb_atomic_order_sequences >
573                         dev->data->dev_conf.nb_event_queue_flows) {
574                         RTE_EDEV_LOG_ERR(
575                 "dev%d queue%d Invalid nb_atomic_order_seq=%d max_flows=%d",
576                         dev_id, queue_id, queue_conf->nb_atomic_order_sequences,
577                         dev->data->dev_conf.nb_event_queue_flows);
578                         return -EINVAL;
579                 }
580         }
581
582         if (dev->data->dev_started) {
583                 RTE_EDEV_LOG_ERR(
584                     "device %d must be stopped to allow queue setup", dev_id);
585                 return -EBUSY;
586         }
587
588         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_setup, -ENOTSUP);
589
590         if (queue_conf == NULL) {
591                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->queue_def_conf,
592                                         -ENOTSUP);
593                 (*dev->dev_ops->queue_def_conf)(dev, queue_id, &def_conf);
594                 def_conf.event_queue_cfg = RTE_EVENT_QUEUE_CFG_DEFAULT;
595                 queue_conf = &def_conf;
596         }
597
598         dev->data->queues_prio[queue_id] = queue_conf->priority;
599         return (*dev->dev_ops->queue_setup)(dev, queue_id, queue_conf);
600 }
601
602 uint8_t
603 rte_event_queue_count(uint8_t dev_id)
604 {
605         struct rte_eventdev *dev;
606
607         dev = &rte_eventdevs[dev_id];
608         return dev->data->nb_queues;
609 }
610
611 uint8_t
612 rte_event_queue_priority(uint8_t dev_id, uint8_t queue_id)
613 {
614         struct rte_eventdev *dev;
615
616         dev = &rte_eventdevs[dev_id];
617         if (dev->data->event_dev_cap & RTE_EVENT_DEV_CAP_QUEUE_QOS)
618                 return dev->data->queues_prio[queue_id];
619         else
620                 return RTE_EVENT_DEV_PRIORITY_NORMAL;
621 }
622
623 static inline int
624 is_valid_port(struct rte_eventdev *dev, uint8_t port_id)
625 {
626         if (port_id < dev->data->nb_ports)
627                 return 1;
628         else
629                 return 0;
630 }
631
632 int
633 rte_event_port_default_conf_get(uint8_t dev_id, uint8_t port_id,
634                                  struct rte_event_port_conf *port_conf)
635 {
636         struct rte_eventdev *dev;
637
638         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
639         dev = &rte_eventdevs[dev_id];
640
641         if (port_conf == NULL)
642                 return -EINVAL;
643
644         if (!is_valid_port(dev, port_id)) {
645                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
646                 return -EINVAL;
647         }
648
649         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf, -ENOTSUP);
650         memset(port_conf, 0, sizeof(struct rte_event_port_conf));
651         (*dev->dev_ops->port_def_conf)(dev, port_id, port_conf);
652         return 0;
653 }
654
655 int
656 rte_event_port_setup(uint8_t dev_id, uint8_t port_id,
657                      const struct rte_event_port_conf *port_conf)
658 {
659         struct rte_eventdev *dev;
660         struct rte_event_port_conf def_conf;
661         int diag;
662
663         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
664         dev = &rte_eventdevs[dev_id];
665
666         if (!is_valid_port(dev, port_id)) {
667                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
668                 return -EINVAL;
669         }
670
671         /* Check new_event_threshold limit */
672         if ((port_conf && !port_conf->new_event_threshold) ||
673                         (port_conf && port_conf->new_event_threshold >
674                                  dev->data->dev_conf.nb_events_limit)) {
675                 RTE_EDEV_LOG_ERR(
676                    "dev%d port%d Invalid event_threshold=%d nb_events_limit=%d",
677                         dev_id, port_id, port_conf->new_event_threshold,
678                         dev->data->dev_conf.nb_events_limit);
679                 return -EINVAL;
680         }
681
682         /* Check dequeue_depth limit */
683         if ((port_conf && !port_conf->dequeue_depth) ||
684                         (port_conf && port_conf->dequeue_depth >
685                 dev->data->dev_conf.nb_event_port_dequeue_depth)) {
686                 RTE_EDEV_LOG_ERR(
687                    "dev%d port%d Invalid dequeue depth=%d max_dequeue_depth=%d",
688                         dev_id, port_id, port_conf->dequeue_depth,
689                         dev->data->dev_conf.nb_event_port_dequeue_depth);
690                 return -EINVAL;
691         }
692
693         /* Check enqueue_depth limit */
694         if ((port_conf && !port_conf->enqueue_depth) ||
695                         (port_conf && port_conf->enqueue_depth >
696                 dev->data->dev_conf.nb_event_port_enqueue_depth)) {
697                 RTE_EDEV_LOG_ERR(
698                    "dev%d port%d Invalid enqueue depth=%d max_enqueue_depth=%d",
699                         dev_id, port_id, port_conf->enqueue_depth,
700                         dev->data->dev_conf.nb_event_port_enqueue_depth);
701                 return -EINVAL;
702         }
703
704         if (dev->data->dev_started) {
705                 RTE_EDEV_LOG_ERR(
706                     "device %d must be stopped to allow port setup", dev_id);
707                 return -EBUSY;
708         }
709
710         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_setup, -ENOTSUP);
711
712         if (port_conf == NULL) {
713                 RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_def_conf,
714                                         -ENOTSUP);
715                 (*dev->dev_ops->port_def_conf)(dev, port_id, &def_conf);
716                 port_conf = &def_conf;
717         }
718
719         dev->data->ports_dequeue_depth[port_id] =
720                         port_conf->dequeue_depth;
721         dev->data->ports_enqueue_depth[port_id] =
722                         port_conf->enqueue_depth;
723
724         diag = (*dev->dev_ops->port_setup)(dev, port_id, port_conf);
725
726         /* Unlink all the queues from this port(default state after setup) */
727         if (!diag)
728                 diag = rte_event_port_unlink(dev_id, port_id, NULL, 0);
729
730         if (diag < 0)
731                 return diag;
732
733         return 0;
734 }
735
736 uint8_t
737 rte_event_port_dequeue_depth(uint8_t dev_id, uint8_t port_id)
738 {
739         struct rte_eventdev *dev;
740
741         dev = &rte_eventdevs[dev_id];
742         return dev->data->ports_dequeue_depth[port_id];
743 }
744
745 uint8_t
746 rte_event_port_enqueue_depth(uint8_t dev_id, uint8_t port_id)
747 {
748         struct rte_eventdev *dev;
749
750         dev = &rte_eventdevs[dev_id];
751         return dev->data->ports_enqueue_depth[port_id];
752 }
753
754 uint8_t
755 rte_event_port_count(uint8_t dev_id)
756 {
757         struct rte_eventdev *dev;
758
759         dev = &rte_eventdevs[dev_id];
760         return dev->data->nb_ports;
761 }
762
763 int
764 rte_event_port_link(uint8_t dev_id, uint8_t port_id,
765                     const uint8_t queues[], const uint8_t priorities[],
766                     uint16_t nb_links)
767 {
768         struct rte_eventdev *dev;
769         uint8_t queues_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
770         uint8_t priorities_list[RTE_EVENT_MAX_QUEUES_PER_DEV];
771         uint16_t *links_map;
772         int i, diag;
773
774         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
775         dev = &rte_eventdevs[dev_id];
776         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_link, -ENOTSUP);
777
778         if (!is_valid_port(dev, port_id)) {
779                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
780                 return -EINVAL;
781         }
782
783         if (queues == NULL) {
784                 for (i = 0; i < dev->data->nb_queues; i++)
785                         queues_list[i] = i;
786
787                 queues = queues_list;
788                 nb_links = dev->data->nb_queues;
789         }
790
791         if (priorities == NULL) {
792                 for (i = 0; i < nb_links; i++)
793                         priorities_list[i] = RTE_EVENT_DEV_PRIORITY_NORMAL;
794
795                 priorities = priorities_list;
796         }
797
798         for (i = 0; i < nb_links; i++)
799                 if (queues[i] >= RTE_EVENT_MAX_QUEUES_PER_DEV)
800                         return -EINVAL;
801
802         diag = (*dev->dev_ops->port_link)(dev->data->ports[port_id], queues,
803                                                 priorities, nb_links);
804         if (diag < 0)
805                 return diag;
806
807         links_map = dev->data->links_map;
808         /* Point links_map to this port specific area */
809         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
810         for (i = 0; i < diag; i++)
811                 links_map[queues[i]] = (uint8_t)priorities[i];
812
813         return diag;
814 }
815
816 #define EVENT_QUEUE_SERVICE_PRIORITY_INVALID (0xdead)
817
818 int
819 rte_event_port_unlink(uint8_t dev_id, uint8_t port_id,
820                       uint8_t queues[], uint16_t nb_unlinks)
821 {
822         struct rte_eventdev *dev;
823         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
824         int i, diag;
825         uint16_t *links_map;
826
827         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
828         dev = &rte_eventdevs[dev_id];
829         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->port_unlink, -ENOTSUP);
830
831         if (!is_valid_port(dev, port_id)) {
832                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
833                 return -EINVAL;
834         }
835
836         if (queues == NULL) {
837                 for (i = 0; i < dev->data->nb_queues; i++)
838                         all_queues[i] = i;
839                 queues = all_queues;
840                 nb_unlinks = dev->data->nb_queues;
841         }
842
843         for (i = 0; i < nb_unlinks; i++)
844                 if (queues[i] >= RTE_EVENT_MAX_QUEUES_PER_DEV)
845                         return -EINVAL;
846
847         diag = (*dev->dev_ops->port_unlink)(dev->data->ports[port_id], queues,
848                                         nb_unlinks);
849
850         if (diag < 0)
851                 return diag;
852
853         links_map = dev->data->links_map;
854         /* Point links_map to this port specific area */
855         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
856         for (i = 0; i < diag; i++)
857                 links_map[queues[i]] = EVENT_QUEUE_SERVICE_PRIORITY_INVALID;
858
859         return diag;
860 }
861
862 int
863 rte_event_port_links_get(uint8_t dev_id, uint8_t port_id,
864                          uint8_t queues[], uint8_t priorities[])
865 {
866         struct rte_eventdev *dev;
867         uint16_t *links_map;
868         int i, count = 0;
869
870         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
871         dev = &rte_eventdevs[dev_id];
872         if (!is_valid_port(dev, port_id)) {
873                 RTE_EDEV_LOG_ERR("Invalid port_id=%" PRIu8, port_id);
874                 return -EINVAL;
875         }
876
877         links_map = dev->data->links_map;
878         /* Point links_map to this port specific area */
879         links_map += (port_id * RTE_EVENT_MAX_QUEUES_PER_DEV);
880         for (i = 0; i < RTE_EVENT_MAX_QUEUES_PER_DEV; i++) {
881                 if (links_map[i] != EVENT_QUEUE_SERVICE_PRIORITY_INVALID) {
882                         queues[count] = i;
883                         priorities[count] = (uint8_t)links_map[i];
884                         ++count;
885                 }
886         }
887         return count;
888 }
889
890 int
891 rte_event_dequeue_timeout_ticks(uint8_t dev_id, uint64_t ns,
892                                  uint64_t *timeout_ticks)
893 {
894         struct rte_eventdev *dev;
895
896         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
897         dev = &rte_eventdevs[dev_id];
898         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->timeout_ticks, -ENOTSUP);
899
900         if (timeout_ticks == NULL)
901                 return -EINVAL;
902
903         (*dev->dev_ops->timeout_ticks)(dev, ns, timeout_ticks);
904         return 0;
905 }
906
907 int
908 rte_event_dev_dump(uint8_t dev_id, FILE *f)
909 {
910         struct rte_eventdev *dev;
911
912         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
913         dev = &rte_eventdevs[dev_id];
914         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dump, -ENOTSUP);
915
916         (*dev->dev_ops->dump)(dev, f);
917         return 0;
918
919 }
920
921 int
922 rte_event_dev_start(uint8_t dev_id)
923 {
924         struct rte_eventdev *dev;
925         int diag;
926
927         RTE_EDEV_LOG_DEBUG("Start dev_id=%" PRIu8, dev_id);
928
929         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
930         dev = &rte_eventdevs[dev_id];
931         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
932
933         if (dev->data->dev_started != 0) {
934                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already started",
935                         dev_id);
936                 return 0;
937         }
938
939         diag = (*dev->dev_ops->dev_start)(dev);
940         if (diag == 0)
941                 dev->data->dev_started = 1;
942         else
943                 return diag;
944
945         return 0;
946 }
947
948 void
949 rte_event_dev_stop(uint8_t dev_id)
950 {
951         struct rte_eventdev *dev;
952
953         RTE_EDEV_LOG_DEBUG("Stop dev_id=%" PRIu8, dev_id);
954
955         RTE_EVENTDEV_VALID_DEVID_OR_RET(dev_id);
956         dev = &rte_eventdevs[dev_id];
957         RTE_FUNC_PTR_OR_RET(*dev->dev_ops->dev_stop);
958
959         if (dev->data->dev_started == 0) {
960                 RTE_EDEV_LOG_ERR("Device with dev_id=%" PRIu8 "already stopped",
961                         dev_id);
962                 return;
963         }
964
965         dev->data->dev_started = 0;
966         (*dev->dev_ops->dev_stop)(dev);
967 }
968
969 int
970 rte_event_dev_close(uint8_t dev_id)
971 {
972         struct rte_eventdev *dev;
973
974         RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
975         dev = &rte_eventdevs[dev_id];
976         RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
977
978         /* Device must be stopped before it can be closed */
979         if (dev->data->dev_started == 1) {
980                 RTE_EDEV_LOG_ERR("Device %u must be stopped before closing",
981                                 dev_id);
982                 return -EBUSY;
983         }
984
985         return (*dev->dev_ops->dev_close)(dev);
986 }