event/dpaa2: support crypto adapter
[dpdk.git] / drivers / event / dpaa2 / dpaa2_eventdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2017 NXP
4  *
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <errno.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <sys/epoll.h>
14
15 #include <rte_atomic.h>
16 #include <rte_byteorder.h>
17 #include <rte_common.h>
18 #include <rte_debug.h>
19 #include <rte_dev.h>
20 #include <rte_eal.h>
21 #include <rte_fslmc.h>
22 #include <rte_lcore.h>
23 #include <rte_log.h>
24 #include <rte_malloc.h>
25 #include <rte_memcpy.h>
26 #include <rte_memory.h>
27 #include <rte_pci.h>
28 #include <rte_bus_vdev.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_cryptodev.h>
31 #include <rte_event_eth_rx_adapter.h>
32
33 #include <fslmc_vfio.h>
34 #include <dpaa2_hw_pvt.h>
35 #include <dpaa2_hw_mempool.h>
36 #include <dpaa2_hw_dpio.h>
37 #include <dpaa2_ethdev.h>
38 #include <dpaa2_sec_event.h>
39 #include "dpaa2_eventdev.h"
40 #include "dpaa2_eventdev_logs.h"
41 #include <portal/dpaa2_hw_pvt.h>
42 #include <mc/fsl_dpci.h>
43
44 /* Clarifications
45  * Evendev = SoC Instance
46  * Eventport = DPIO Instance
47  * Eventqueue = DPCON Instance
48  * 1 Eventdev can have N Eventqueue
49  * Soft Event Flow is DPCI Instance
50  */
51
52 /* Dynamic logging identified for mempool */
53 int dpaa2_logtype_event;
54
55 static uint16_t
56 dpaa2_eventdev_enqueue_burst(void *port, const struct rte_event ev[],
57                              uint16_t nb_events)
58 {
59
60         struct dpaa2_port *dpaa2_portal = port;
61         struct dpaa2_dpio_dev *dpio_dev;
62         uint32_t queue_id = ev[0].queue_id;
63         struct dpaa2_eventq *evq_info;
64         uint32_t fqid;
65         struct qbman_swp *swp;
66         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
67         uint32_t loop, frames_to_send;
68         struct qbman_eq_desc eqdesc[MAX_TX_RING_SLOTS];
69         uint16_t num_tx = 0;
70         int i, n, ret;
71         uint8_t channel_index;
72
73         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
74                 /* Affine current thread context to a qman portal */
75                 ret = dpaa2_affine_qbman_swp();
76                 if (ret < 0) {
77                         DPAA2_EVENTDEV_ERR("Failure in affining portal");
78                         return 0;
79                 }
80         }
81         /* todo - dpaa2_portal shall have dpio_dev - no per thread variable */
82         dpio_dev = DPAA2_PER_LCORE_DPIO;
83         swp = DPAA2_PER_LCORE_PORTAL;
84
85         if (likely(dpaa2_portal->is_port_linked))
86                 goto skip_linking;
87
88         /* Create mapping between portal and channel to receive packets */
89         for (i = 0; i < DPAA2_EVENT_MAX_QUEUES; i++) {
90                 evq_info = &dpaa2_portal->evq_info[i];
91                 if (!evq_info->event_port)
92                         continue;
93
94                 ret = dpio_add_static_dequeue_channel(dpio_dev->dpio,
95                                                       CMD_PRI_LOW,
96                                                       dpio_dev->token,
97                                                       evq_info->dpcon->dpcon_id,
98                                                       &channel_index);
99                 if (ret < 0) {
100                         DPAA2_EVENTDEV_ERR(
101                                 "Static dequeue config failed: err(%d)", ret);
102                         goto err;
103                 }
104
105                 qbman_swp_push_set(swp, channel_index, 1);
106                 evq_info->dpcon->channel_index = channel_index;
107         }
108         dpaa2_portal->is_port_linked = true;
109
110 skip_linking:
111         evq_info = &dpaa2_portal->evq_info[queue_id];
112
113         while (nb_events) {
114                 frames_to_send = (nb_events > dpaa2_eqcr_size) ?
115                         dpaa2_eqcr_size : nb_events;
116
117                 for (loop = 0; loop < frames_to_send; loop++) {
118                         const struct rte_event *event = &ev[num_tx + loop];
119
120                         if (event->sched_type != RTE_SCHED_TYPE_ATOMIC)
121                                 fqid = evq_info->dpci->rx_queue[
122                                         DPAA2_EVENT_DPCI_PARALLEL_QUEUE].fqid;
123                         else
124                                 fqid = evq_info->dpci->rx_queue[
125                                         DPAA2_EVENT_DPCI_ATOMIC_QUEUE].fqid;
126
127                         /* Prepare enqueue descriptor */
128                         qbman_eq_desc_clear(&eqdesc[loop]);
129                         qbman_eq_desc_set_fq(&eqdesc[loop], fqid);
130                         qbman_eq_desc_set_no_orp(&eqdesc[loop], 0);
131                         qbman_eq_desc_set_response(&eqdesc[loop], 0, 0);
132
133                         if (event->sched_type == RTE_SCHED_TYPE_ATOMIC
134                                 && event->mbuf->seqn) {
135                                 uint8_t dqrr_index = event->mbuf->seqn - 1;
136
137                                 qbman_eq_desc_set_dca(&eqdesc[loop], 1,
138                                                       dqrr_index, 0);
139                                 DPAA2_PER_LCORE_DQRR_SIZE--;
140                                 DPAA2_PER_LCORE_DQRR_HELD &= ~(1 << dqrr_index);
141                         }
142
143                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
144
145                         /*
146                          * todo - need to align with hw context data
147                          * to avoid copy
148                          */
149                         struct rte_event *ev_temp = rte_malloc(NULL,
150                                                 sizeof(struct rte_event), 0);
151
152                         if (!ev_temp) {
153                                 if (!loop)
154                                         return num_tx;
155                                 frames_to_send = loop;
156                                 DPAA2_EVENTDEV_ERR(
157                                         "Unable to allocate event object");
158                                 goto send_partial;
159                         }
160                         rte_memcpy(ev_temp, event, sizeof(struct rte_event));
161                         DPAA2_SET_FD_ADDR((&fd_arr[loop]), (size_t)ev_temp);
162                         DPAA2_SET_FD_LEN((&fd_arr[loop]),
163                                          sizeof(struct rte_event));
164                 }
165 send_partial:
166                 loop = 0;
167                 while (loop < frames_to_send) {
168                         loop += qbman_swp_enqueue_multiple_desc(swp,
169                                         &eqdesc[loop], &fd_arr[loop],
170                                         frames_to_send - loop);
171                 }
172                 num_tx += frames_to_send;
173                 nb_events -= frames_to_send;
174         }
175
176         return num_tx;
177 err:
178         for (n = 0; n < i; n++) {
179                 evq_info = &dpaa2_portal->evq_info[n];
180                 if (!evq_info->event_port)
181                         continue;
182                 qbman_swp_push_set(swp, evq_info->dpcon->channel_index, 0);
183                 dpio_remove_static_dequeue_channel(dpio_dev->dpio, 0,
184                                                 dpio_dev->token,
185                                                 evq_info->dpcon->dpcon_id);
186         }
187         return 0;
188
189 }
190
191 static uint16_t
192 dpaa2_eventdev_enqueue(void *port, const struct rte_event *ev)
193 {
194         return dpaa2_eventdev_enqueue_burst(port, ev, 1);
195 }
196
197 static void dpaa2_eventdev_dequeue_wait(uint64_t timeout_ticks)
198 {
199         struct epoll_event epoll_ev;
200
201         qbman_swp_interrupt_clear_status(DPAA2_PER_LCORE_PORTAL,
202                                          QBMAN_SWP_INTERRUPT_DQRI);
203
204         epoll_wait(DPAA2_PER_LCORE_DPIO->epoll_fd,
205                          &epoll_ev, 1, timeout_ticks);
206 }
207
208 static void dpaa2_eventdev_process_parallel(struct qbman_swp *swp,
209                                             const struct qbman_fd *fd,
210                                             const struct qbman_result *dq,
211                                             struct dpaa2_queue *rxq,
212                                             struct rte_event *ev)
213 {
214         struct rte_event *ev_temp =
215                 (struct rte_event *)(size_t)DPAA2_GET_FD_ADDR(fd);
216
217         RTE_SET_USED(rxq);
218
219         rte_memcpy(ev, ev_temp, sizeof(struct rte_event));
220         rte_free(ev_temp);
221
222         qbman_swp_dqrr_consume(swp, dq);
223 }
224
225 static void dpaa2_eventdev_process_atomic(struct qbman_swp *swp,
226                                           const struct qbman_fd *fd,
227                                           const struct qbman_result *dq,
228                                           struct dpaa2_queue *rxq,
229                                           struct rte_event *ev)
230 {
231         struct rte_event *ev_temp =
232                 (struct rte_event *)(size_t)DPAA2_GET_FD_ADDR(fd);
233         uint8_t dqrr_index = qbman_get_dqrr_idx(dq);
234
235         RTE_SET_USED(swp);
236         RTE_SET_USED(rxq);
237
238         rte_memcpy(ev, ev_temp, sizeof(struct rte_event));
239         rte_free(ev_temp);
240         ev->mbuf->seqn = dqrr_index + 1;
241         DPAA2_PER_LCORE_DQRR_SIZE++;
242         DPAA2_PER_LCORE_DQRR_HELD |= 1 << dqrr_index;
243         DPAA2_PER_LCORE_DQRR_MBUF(dqrr_index) = ev->mbuf;
244 }
245
246 static uint16_t
247 dpaa2_eventdev_dequeue_burst(void *port, struct rte_event ev[],
248                              uint16_t nb_events, uint64_t timeout_ticks)
249 {
250         const struct qbman_result *dq;
251         struct dpaa2_dpio_dev *dpio_dev = NULL;
252         struct dpaa2_port *dpaa2_portal = port;
253         struct dpaa2_eventq *evq_info;
254         struct qbman_swp *swp;
255         const struct qbman_fd *fd;
256         struct dpaa2_queue *rxq;
257         int num_pkts = 0, ret, i = 0, n;
258         uint8_t channel_index;
259
260         if (unlikely(!DPAA2_PER_LCORE_DPIO)) {
261                 /* Affine current thread context to a qman portal */
262                 ret = dpaa2_affine_qbman_swp();
263                 if (ret < 0) {
264                         DPAA2_EVENTDEV_ERR("Failure in affining portal");
265                         return 0;
266                 }
267         }
268
269         dpio_dev = DPAA2_PER_LCORE_DPIO;
270         swp = DPAA2_PER_LCORE_PORTAL;
271
272         if (likely(dpaa2_portal->is_port_linked))
273                 goto skip_linking;
274
275         /* Create mapping between portal and channel to receive packets */
276         for (i = 0; i < DPAA2_EVENT_MAX_QUEUES; i++) {
277                 evq_info = &dpaa2_portal->evq_info[i];
278                 if (!evq_info->event_port)
279                         continue;
280
281                 ret = dpio_add_static_dequeue_channel(dpio_dev->dpio,
282                                                       CMD_PRI_LOW,
283                                                       dpio_dev->token,
284                                                       evq_info->dpcon->dpcon_id,
285                                                       &channel_index);
286                 if (ret < 0) {
287                         DPAA2_EVENTDEV_ERR(
288                                 "Static dequeue config failed: err(%d)", ret);
289                         goto err;
290                 }
291
292                 qbman_swp_push_set(swp, channel_index, 1);
293                 evq_info->dpcon->channel_index = channel_index;
294         }
295         dpaa2_portal->is_port_linked = true;
296
297 skip_linking:
298         /* Check if there are atomic contexts to be released */
299         while (DPAA2_PER_LCORE_DQRR_SIZE) {
300                 if (DPAA2_PER_LCORE_DQRR_HELD & (1 << i)) {
301                         qbman_swp_dqrr_idx_consume(swp, i);
302                         DPAA2_PER_LCORE_DQRR_SIZE--;
303                         DPAA2_PER_LCORE_DQRR_MBUF(i)->seqn =
304                                 DPAA2_INVALID_MBUF_SEQN;
305                 }
306                 i++;
307         }
308         DPAA2_PER_LCORE_DQRR_HELD = 0;
309
310         do {
311                 dq = qbman_swp_dqrr_next(swp);
312                 if (!dq) {
313                         if (!num_pkts && timeout_ticks) {
314                                 dpaa2_eventdev_dequeue_wait(timeout_ticks);
315                                 timeout_ticks = 0;
316                                 continue;
317                         }
318                         return num_pkts;
319                 }
320                 qbman_swp_prefetch_dqrr_next(swp);
321
322                 fd = qbman_result_DQ_fd(dq);
323                 rxq = (struct dpaa2_queue *)(size_t)qbman_result_DQ_fqd_ctx(dq);
324                 if (rxq) {
325                         rxq->cb(swp, fd, dq, rxq, &ev[num_pkts]);
326                 } else {
327                         qbman_swp_dqrr_consume(swp, dq);
328                         DPAA2_EVENTDEV_ERR("Null Return VQ received");
329                         return 0;
330                 }
331
332                 num_pkts++;
333         } while (num_pkts < nb_events);
334
335         return num_pkts;
336 err:
337         for (n = 0; n < i; n++) {
338                 evq_info = &dpaa2_portal->evq_info[n];
339                 if (!evq_info->event_port)
340                         continue;
341
342                 qbman_swp_push_set(swp, evq_info->dpcon->channel_index, 0);
343                 dpio_remove_static_dequeue_channel(dpio_dev->dpio, 0,
344                                                         dpio_dev->token,
345                                                 evq_info->dpcon->dpcon_id);
346         }
347         return 0;
348 }
349
350 static uint16_t
351 dpaa2_eventdev_dequeue(void *port, struct rte_event *ev,
352                        uint64_t timeout_ticks)
353 {
354         return dpaa2_eventdev_dequeue_burst(port, ev, 1, timeout_ticks);
355 }
356
357 static void
358 dpaa2_eventdev_info_get(struct rte_eventdev *dev,
359                         struct rte_event_dev_info *dev_info)
360 {
361         struct dpaa2_eventdev *priv = dev->data->dev_private;
362
363         EVENTDEV_INIT_FUNC_TRACE();
364
365         RTE_SET_USED(dev);
366
367         memset(dev_info, 0, sizeof(struct rte_event_dev_info));
368         dev_info->min_dequeue_timeout_ns =
369                 DPAA2_EVENT_MIN_DEQUEUE_TIMEOUT;
370         dev_info->max_dequeue_timeout_ns =
371                 DPAA2_EVENT_MAX_DEQUEUE_TIMEOUT;
372         dev_info->dequeue_timeout_ns =
373                 DPAA2_EVENT_PORT_DEQUEUE_TIMEOUT_NS;
374         dev_info->max_event_queues = priv->max_event_queues;
375         dev_info->max_event_queue_flows =
376                 DPAA2_EVENT_MAX_QUEUE_FLOWS;
377         dev_info->max_event_queue_priority_levels =
378                 DPAA2_EVENT_MAX_QUEUE_PRIORITY_LEVELS;
379         dev_info->max_event_priority_levels =
380                 DPAA2_EVENT_MAX_EVENT_PRIORITY_LEVELS;
381         dev_info->max_event_ports = rte_fslmc_get_device_count(DPAA2_IO);
382         /* we only support dpio upto number of cores*/
383         if (dev_info->max_event_ports > rte_lcore_count())
384                 dev_info->max_event_ports = rte_lcore_count();
385         dev_info->max_event_port_dequeue_depth =
386                 DPAA2_EVENT_MAX_PORT_DEQUEUE_DEPTH;
387         dev_info->max_event_port_enqueue_depth =
388                 DPAA2_EVENT_MAX_PORT_ENQUEUE_DEPTH;
389         dev_info->max_num_events = DPAA2_EVENT_MAX_NUM_EVENTS;
390         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
391                 RTE_EVENT_DEV_CAP_BURST_MODE|
392                 RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
393                 RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
394                 RTE_EVENT_DEV_CAP_NONSEQ_MODE;
395
396 }
397
398 static int
399 dpaa2_eventdev_configure(const struct rte_eventdev *dev)
400 {
401         struct dpaa2_eventdev *priv = dev->data->dev_private;
402         struct rte_event_dev_config *conf = &dev->data->dev_conf;
403
404         EVENTDEV_INIT_FUNC_TRACE();
405
406         priv->nb_event_queues = conf->nb_event_queues;
407         priv->nb_event_ports = conf->nb_event_ports;
408         priv->nb_event_queue_flows = conf->nb_event_queue_flows;
409         priv->nb_event_port_dequeue_depth = conf->nb_event_port_dequeue_depth;
410         priv->nb_event_port_enqueue_depth = conf->nb_event_port_enqueue_depth;
411         priv->event_dev_cfg = conf->event_dev_cfg;
412
413         /* Check dequeue timeout method is per dequeue or global */
414         if (priv->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
415                 /*
416                  * Use timeout value as given in dequeue operation.
417                  * So invalidating this timeout value.
418                  */
419                 priv->dequeue_timeout_ns = 0;
420
421         } else if (conf->dequeue_timeout_ns == 0) {
422                 priv->dequeue_timeout_ns = DPAA2_EVENT_PORT_DEQUEUE_TIMEOUT_NS;
423         } else {
424                 priv->dequeue_timeout_ns = conf->dequeue_timeout_ns;
425         }
426
427         DPAA2_EVENTDEV_DEBUG("Configured eventdev devid=%d",
428                              dev->data->dev_id);
429         return 0;
430 }
431
432 static int
433 dpaa2_eventdev_start(struct rte_eventdev *dev)
434 {
435         EVENTDEV_INIT_FUNC_TRACE();
436
437         RTE_SET_USED(dev);
438
439         return 0;
440 }
441
442 static void
443 dpaa2_eventdev_stop(struct rte_eventdev *dev)
444 {
445         EVENTDEV_INIT_FUNC_TRACE();
446
447         RTE_SET_USED(dev);
448 }
449
450 static int
451 dpaa2_eventdev_close(struct rte_eventdev *dev)
452 {
453         EVENTDEV_INIT_FUNC_TRACE();
454
455         RTE_SET_USED(dev);
456
457         return 0;
458 }
459
460 static void
461 dpaa2_eventdev_queue_def_conf(struct rte_eventdev *dev, uint8_t queue_id,
462                               struct rte_event_queue_conf *queue_conf)
463 {
464         EVENTDEV_INIT_FUNC_TRACE();
465
466         RTE_SET_USED(dev);
467         RTE_SET_USED(queue_id);
468         RTE_SET_USED(queue_conf);
469
470         queue_conf->nb_atomic_flows = DPAA2_EVENT_QUEUE_ATOMIC_FLOWS;
471         queue_conf->schedule_type = RTE_SCHED_TYPE_ATOMIC |
472                                       RTE_SCHED_TYPE_PARALLEL;
473         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
474 }
475
476 static int
477 dpaa2_eventdev_queue_setup(struct rte_eventdev *dev, uint8_t queue_id,
478                            const struct rte_event_queue_conf *queue_conf)
479 {
480         struct dpaa2_eventdev *priv = dev->data->dev_private;
481         struct dpaa2_eventq *evq_info = &priv->evq_info[queue_id];
482
483         EVENTDEV_INIT_FUNC_TRACE();
484
485         switch (queue_conf->schedule_type) {
486         case RTE_SCHED_TYPE_PARALLEL:
487         case RTE_SCHED_TYPE_ATOMIC:
488                 break;
489         case RTE_SCHED_TYPE_ORDERED:
490                 DPAA2_EVENTDEV_ERR("Schedule type is not supported.");
491                 return -1;
492         }
493         evq_info->event_queue_cfg = queue_conf->event_queue_cfg;
494         evq_info->event_queue_id = queue_id;
495
496         return 0;
497 }
498
499 static void
500 dpaa2_eventdev_queue_release(struct rte_eventdev *dev, uint8_t queue_id)
501 {
502         EVENTDEV_INIT_FUNC_TRACE();
503
504         RTE_SET_USED(dev);
505         RTE_SET_USED(queue_id);
506 }
507
508 static void
509 dpaa2_eventdev_port_def_conf(struct rte_eventdev *dev, uint8_t port_id,
510                              struct rte_event_port_conf *port_conf)
511 {
512         EVENTDEV_INIT_FUNC_TRACE();
513
514         RTE_SET_USED(dev);
515         RTE_SET_USED(port_id);
516
517         port_conf->new_event_threshold =
518                 DPAA2_EVENT_MAX_NUM_EVENTS;
519         port_conf->dequeue_depth =
520                 DPAA2_EVENT_MAX_PORT_DEQUEUE_DEPTH;
521         port_conf->enqueue_depth =
522                 DPAA2_EVENT_MAX_PORT_ENQUEUE_DEPTH;
523         port_conf->disable_implicit_release = 0;
524 }
525
526 static int
527 dpaa2_eventdev_port_setup(struct rte_eventdev *dev, uint8_t port_id,
528                           const struct rte_event_port_conf *port_conf)
529 {
530         char event_port_name[32];
531         struct dpaa2_port *portal;
532
533         EVENTDEV_INIT_FUNC_TRACE();
534
535         RTE_SET_USED(port_conf);
536
537         sprintf(event_port_name, "event-port-%d", port_id);
538         portal = rte_malloc(event_port_name, sizeof(struct dpaa2_port), 0);
539         if (!portal) {
540                 DPAA2_EVENTDEV_ERR("Memory allocation failure");
541                 return -ENOMEM;
542         }
543
544         memset(portal, 0, sizeof(struct dpaa2_port));
545         dev->data->ports[port_id] = portal;
546         return 0;
547 }
548
549 static void
550 dpaa2_eventdev_port_release(void *port)
551 {
552         struct dpaa2_port *portal = port;
553
554         EVENTDEV_INIT_FUNC_TRACE();
555
556         /* TODO: Cleanup is required when ports are in linked state. */
557         if (portal->is_port_linked)
558                 DPAA2_EVENTDEV_WARN("Event port must be unlinked before release");
559
560         if (portal)
561                 rte_free(portal);
562
563         portal = NULL;
564 }
565
566 static int
567 dpaa2_eventdev_port_link(struct rte_eventdev *dev, void *port,
568                          const uint8_t queues[], const uint8_t priorities[],
569                         uint16_t nb_links)
570 {
571         struct dpaa2_eventdev *priv = dev->data->dev_private;
572         struct dpaa2_port *dpaa2_portal = port;
573         struct dpaa2_eventq *evq_info;
574         uint16_t i;
575
576         EVENTDEV_INIT_FUNC_TRACE();
577
578         RTE_SET_USED(priorities);
579
580         for (i = 0; i < nb_links; i++) {
581                 evq_info = &priv->evq_info[queues[i]];
582                 memcpy(&dpaa2_portal->evq_info[queues[i]], evq_info,
583                            sizeof(struct dpaa2_eventq));
584                 dpaa2_portal->evq_info[queues[i]].event_port = port;
585                 dpaa2_portal->num_linked_evq++;
586         }
587
588         return (int)nb_links;
589 }
590
591 static int
592 dpaa2_eventdev_port_unlink(struct rte_eventdev *dev, void *port,
593                            uint8_t queues[], uint16_t nb_unlinks)
594 {
595         struct dpaa2_port *dpaa2_portal = port;
596         int i;
597         struct dpaa2_dpio_dev *dpio_dev = NULL;
598         struct dpaa2_eventq *evq_info;
599         struct qbman_swp *swp;
600
601         EVENTDEV_INIT_FUNC_TRACE();
602
603         RTE_SET_USED(dev);
604         RTE_SET_USED(queues);
605
606         for (i = 0; i < nb_unlinks; i++) {
607                 evq_info = &dpaa2_portal->evq_info[queues[i]];
608
609                 if (DPAA2_PER_LCORE_DPIO && evq_info->dpcon) {
610                         /* todo dpaa2_portal shall have dpio_dev-no per lcore*/
611                         dpio_dev = DPAA2_PER_LCORE_DPIO;
612                         swp = DPAA2_PER_LCORE_PORTAL;
613
614                         qbman_swp_push_set(swp,
615                                         evq_info->dpcon->channel_index, 0);
616                         dpio_remove_static_dequeue_channel(dpio_dev->dpio, 0,
617                                                 dpio_dev->token,
618                                                 evq_info->dpcon->dpcon_id);
619                 }
620                 memset(evq_info, 0, sizeof(struct dpaa2_eventq));
621                 if (dpaa2_portal->num_linked_evq)
622                         dpaa2_portal->num_linked_evq--;
623         }
624
625         if (!dpaa2_portal->num_linked_evq)
626                 dpaa2_portal->is_port_linked = false;
627
628         return (int)nb_unlinks;
629 }
630
631
632 static int
633 dpaa2_eventdev_timeout_ticks(struct rte_eventdev *dev, uint64_t ns,
634                              uint64_t *timeout_ticks)
635 {
636         uint32_t scale = 1000*1000;
637
638         EVENTDEV_INIT_FUNC_TRACE();
639
640         RTE_SET_USED(dev);
641         *timeout_ticks = ns * scale;
642
643         return 0;
644 }
645
646 static void
647 dpaa2_eventdev_dump(struct rte_eventdev *dev, FILE *f)
648 {
649         EVENTDEV_INIT_FUNC_TRACE();
650
651         RTE_SET_USED(dev);
652         RTE_SET_USED(f);
653 }
654
655 static int
656 dpaa2_eventdev_eth_caps_get(const struct rte_eventdev *dev,
657                             const struct rte_eth_dev *eth_dev,
658                             uint32_t *caps)
659 {
660         const char *ethdev_driver = eth_dev->device->driver->name;
661
662         EVENTDEV_INIT_FUNC_TRACE();
663
664         RTE_SET_USED(dev);
665
666         if (!strcmp(ethdev_driver, "net_dpaa2"))
667                 *caps = RTE_EVENT_ETH_RX_ADAPTER_DPAA2_CAP;
668         else
669                 *caps = RTE_EVENT_ETH_RX_ADAPTER_SW_CAP;
670
671         return 0;
672 }
673
674 static int
675 dpaa2_eventdev_eth_queue_add_all(const struct rte_eventdev *dev,
676                 const struct rte_eth_dev *eth_dev,
677                 const struct rte_event_eth_rx_adapter_queue_conf *queue_conf)
678 {
679         struct dpaa2_eventdev *priv = dev->data->dev_private;
680         uint8_t ev_qid = queue_conf->ev.queue_id;
681         uint16_t dpcon_id = priv->evq_info[ev_qid].dpcon->dpcon_id;
682         int i, ret;
683
684         EVENTDEV_INIT_FUNC_TRACE();
685
686         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
687                 ret = dpaa2_eth_eventq_attach(eth_dev, i,
688                                 dpcon_id, queue_conf);
689                 if (ret) {
690                         DPAA2_EVENTDEV_ERR(
691                                 "Event queue attach failed: err(%d)", ret);
692                         goto fail;
693                 }
694         }
695         return 0;
696 fail:
697         for (i = (i - 1); i >= 0 ; i--)
698                 dpaa2_eth_eventq_detach(eth_dev, i);
699
700         return ret;
701 }
702
703 static int
704 dpaa2_eventdev_eth_queue_add(const struct rte_eventdev *dev,
705                 const struct rte_eth_dev *eth_dev,
706                 int32_t rx_queue_id,
707                 const struct rte_event_eth_rx_adapter_queue_conf *queue_conf)
708 {
709         struct dpaa2_eventdev *priv = dev->data->dev_private;
710         uint8_t ev_qid = queue_conf->ev.queue_id;
711         uint16_t dpcon_id = priv->evq_info[ev_qid].dpcon->dpcon_id;
712         int ret;
713
714         EVENTDEV_INIT_FUNC_TRACE();
715
716         if (rx_queue_id == -1)
717                 return dpaa2_eventdev_eth_queue_add_all(dev,
718                                 eth_dev, queue_conf);
719
720         ret = dpaa2_eth_eventq_attach(eth_dev, rx_queue_id,
721                         dpcon_id, queue_conf);
722         if (ret) {
723                 DPAA2_EVENTDEV_ERR(
724                         "Event queue attach failed: err(%d)", ret);
725                 return ret;
726         }
727         return 0;
728 }
729
730 static int
731 dpaa2_eventdev_eth_queue_del_all(const struct rte_eventdev *dev,
732                              const struct rte_eth_dev *eth_dev)
733 {
734         int i, ret;
735
736         EVENTDEV_INIT_FUNC_TRACE();
737
738         RTE_SET_USED(dev);
739
740         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
741                 ret = dpaa2_eth_eventq_detach(eth_dev, i);
742                 if (ret) {
743                         DPAA2_EVENTDEV_ERR(
744                                 "Event queue detach failed: err(%d)", ret);
745                         return ret;
746                 }
747         }
748
749         return 0;
750 }
751
752 static int
753 dpaa2_eventdev_eth_queue_del(const struct rte_eventdev *dev,
754                              const struct rte_eth_dev *eth_dev,
755                              int32_t rx_queue_id)
756 {
757         int ret;
758
759         EVENTDEV_INIT_FUNC_TRACE();
760
761         if (rx_queue_id == -1)
762                 return dpaa2_eventdev_eth_queue_del_all(dev, eth_dev);
763
764         ret = dpaa2_eth_eventq_detach(eth_dev, rx_queue_id);
765         if (ret) {
766                 DPAA2_EVENTDEV_ERR(
767                         "Event queue detach failed: err(%d)", ret);
768                 return ret;
769         }
770
771         return 0;
772 }
773
774 static int
775 dpaa2_eventdev_eth_start(const struct rte_eventdev *dev,
776                          const struct rte_eth_dev *eth_dev)
777 {
778         EVENTDEV_INIT_FUNC_TRACE();
779
780         RTE_SET_USED(dev);
781         RTE_SET_USED(eth_dev);
782
783         return 0;
784 }
785
786 static int
787 dpaa2_eventdev_eth_stop(const struct rte_eventdev *dev,
788                         const struct rte_eth_dev *eth_dev)
789 {
790         EVENTDEV_INIT_FUNC_TRACE();
791
792         RTE_SET_USED(dev);
793         RTE_SET_USED(eth_dev);
794
795         return 0;
796 }
797
798 static int
799 dpaa2_eventdev_crypto_caps_get(const struct rte_eventdev *dev,
800                             const struct rte_cryptodev *cdev,
801                             uint32_t *caps)
802 {
803         const char *name = cdev->data->name;
804
805         EVENTDEV_INIT_FUNC_TRACE();
806
807         RTE_SET_USED(dev);
808
809         if (!strncmp(name, "dpsec-", 6))
810                 *caps = RTE_EVENT_CRYPTO_ADAPTER_DPAA2_CAP;
811         else
812                 return -1;
813
814         return 0;
815 }
816
817 static int
818 dpaa2_eventdev_crypto_queue_add_all(const struct rte_eventdev *dev,
819                 const struct rte_cryptodev *cryptodev,
820                 const struct rte_event *ev)
821 {
822         struct dpaa2_eventdev *priv = dev->data->dev_private;
823         uint8_t ev_qid = ev->queue_id;
824         uint16_t dpcon_id = priv->evq_info[ev_qid].dpcon->dpcon_id;
825         int i, ret;
826
827         EVENTDEV_INIT_FUNC_TRACE();
828
829         for (i = 0; i < cryptodev->data->nb_queue_pairs; i++) {
830                 ret = dpaa2_sec_eventq_attach(cryptodev, i,
831                                 dpcon_id, ev);
832                 if (ret) {
833                         DPAA2_EVENTDEV_ERR("dpaa2_sec_eventq_attach failed: ret %d\n",
834                                     ret);
835                         goto fail;
836                 }
837         }
838         return 0;
839 fail:
840         for (i = (i - 1); i >= 0 ; i--)
841                 dpaa2_sec_eventq_detach(cryptodev, i);
842
843         return ret;
844 }
845
846 static int
847 dpaa2_eventdev_crypto_queue_add(const struct rte_eventdev *dev,
848                 const struct rte_cryptodev *cryptodev,
849                 int32_t rx_queue_id,
850                 const struct rte_event *ev)
851 {
852         struct dpaa2_eventdev *priv = dev->data->dev_private;
853         uint8_t ev_qid = ev->queue_id;
854         uint16_t dpcon_id = priv->evq_info[ev_qid].dpcon->dpcon_id;
855         int ret;
856
857         EVENTDEV_INIT_FUNC_TRACE();
858
859         if (rx_queue_id == -1)
860                 return dpaa2_eventdev_crypto_queue_add_all(dev,
861                                 cryptodev, ev);
862
863         ret = dpaa2_sec_eventq_attach(cryptodev, rx_queue_id,
864                         dpcon_id, ev);
865         if (ret) {
866                 DPAA2_EVENTDEV_ERR(
867                         "dpaa2_sec_eventq_attach failed: ret: %d\n", ret);
868                 return ret;
869         }
870         return 0;
871 }
872
873 static int
874 dpaa2_eventdev_crypto_queue_del_all(const struct rte_eventdev *dev,
875                              const struct rte_cryptodev *cdev)
876 {
877         int i, ret;
878
879         EVENTDEV_INIT_FUNC_TRACE();
880
881         RTE_SET_USED(dev);
882
883         for (i = 0; i < cdev->data->nb_queue_pairs; i++) {
884                 ret = dpaa2_sec_eventq_detach(cdev, i);
885                 if (ret) {
886                         DPAA2_EVENTDEV_ERR(
887                                 "dpaa2_sec_eventq_detach failed:ret %d\n", ret);
888                         return ret;
889                 }
890         }
891
892         return 0;
893 }
894
895 static int
896 dpaa2_eventdev_crypto_queue_del(const struct rte_eventdev *dev,
897                              const struct rte_cryptodev *cryptodev,
898                              int32_t rx_queue_id)
899 {
900         int ret;
901
902         EVENTDEV_INIT_FUNC_TRACE();
903
904         if (rx_queue_id == -1)
905                 return dpaa2_eventdev_crypto_queue_del_all(dev, cryptodev);
906
907         ret = dpaa2_sec_eventq_detach(cryptodev, rx_queue_id);
908         if (ret) {
909                 DPAA2_EVENTDEV_ERR(
910                         "dpaa2_sec_eventq_detach failed: ret: %d\n", ret);
911                 return ret;
912         }
913
914         return 0;
915 }
916
917 static int
918 dpaa2_eventdev_crypto_start(const struct rte_eventdev *dev,
919                             const struct rte_cryptodev *cryptodev)
920 {
921         EVENTDEV_INIT_FUNC_TRACE();
922
923         RTE_SET_USED(dev);
924         RTE_SET_USED(cryptodev);
925
926         return 0;
927 }
928
929 static int
930 dpaa2_eventdev_crypto_stop(const struct rte_eventdev *dev,
931                            const struct rte_cryptodev *cryptodev)
932 {
933         EVENTDEV_INIT_FUNC_TRACE();
934
935         RTE_SET_USED(dev);
936         RTE_SET_USED(cryptodev);
937
938         return 0;
939 }
940
941 static struct rte_eventdev_ops dpaa2_eventdev_ops = {
942         .dev_infos_get    = dpaa2_eventdev_info_get,
943         .dev_configure    = dpaa2_eventdev_configure,
944         .dev_start        = dpaa2_eventdev_start,
945         .dev_stop         = dpaa2_eventdev_stop,
946         .dev_close        = dpaa2_eventdev_close,
947         .queue_def_conf   = dpaa2_eventdev_queue_def_conf,
948         .queue_setup      = dpaa2_eventdev_queue_setup,
949         .queue_release    = dpaa2_eventdev_queue_release,
950         .port_def_conf    = dpaa2_eventdev_port_def_conf,
951         .port_setup       = dpaa2_eventdev_port_setup,
952         .port_release     = dpaa2_eventdev_port_release,
953         .port_link        = dpaa2_eventdev_port_link,
954         .port_unlink      = dpaa2_eventdev_port_unlink,
955         .timeout_ticks    = dpaa2_eventdev_timeout_ticks,
956         .dump             = dpaa2_eventdev_dump,
957         .eth_rx_adapter_caps_get = dpaa2_eventdev_eth_caps_get,
958         .eth_rx_adapter_queue_add = dpaa2_eventdev_eth_queue_add,
959         .eth_rx_adapter_queue_del = dpaa2_eventdev_eth_queue_del,
960         .eth_rx_adapter_start = dpaa2_eventdev_eth_start,
961         .eth_rx_adapter_stop = dpaa2_eventdev_eth_stop,
962         .crypto_adapter_caps_get        = dpaa2_eventdev_crypto_caps_get,
963         .crypto_adapter_queue_pair_add  = dpaa2_eventdev_crypto_queue_add,
964         .crypto_adapter_queue_pair_del  = dpaa2_eventdev_crypto_queue_del,
965         .crypto_adapter_start           = dpaa2_eventdev_crypto_start,
966         .crypto_adapter_stop            = dpaa2_eventdev_crypto_stop,
967 };
968
969 static int
970 dpaa2_eventdev_setup_dpci(struct dpaa2_dpci_dev *dpci_dev,
971                           struct dpaa2_dpcon_dev *dpcon_dev)
972 {
973         struct dpci_rx_queue_cfg rx_queue_cfg;
974         int ret, i;
975
976         /*Do settings to get the frame on a DPCON object*/
977         rx_queue_cfg.options = DPCI_QUEUE_OPT_DEST |
978                   DPCI_QUEUE_OPT_USER_CTX;
979         rx_queue_cfg.dest_cfg.dest_type = DPCI_DEST_DPCON;
980         rx_queue_cfg.dest_cfg.dest_id = dpcon_dev->dpcon_id;
981         rx_queue_cfg.dest_cfg.priority = DPAA2_EVENT_DEFAULT_DPCI_PRIO;
982
983         dpci_dev->rx_queue[DPAA2_EVENT_DPCI_PARALLEL_QUEUE].cb =
984                 dpaa2_eventdev_process_parallel;
985         dpci_dev->rx_queue[DPAA2_EVENT_DPCI_ATOMIC_QUEUE].cb =
986                 dpaa2_eventdev_process_atomic;
987
988         for (i = 0 ; i < DPAA2_EVENT_DPCI_MAX_QUEUES; i++) {
989                 rx_queue_cfg.user_ctx = (size_t)(&dpci_dev->rx_queue[i]);
990                 ret = dpci_set_rx_queue(&dpci_dev->dpci,
991                                         CMD_PRI_LOW,
992                                         dpci_dev->token, i,
993                                         &rx_queue_cfg);
994                 if (ret) {
995                         DPAA2_EVENTDEV_ERR(
996                                 "DPCI Rx queue setup failed: err(%d)",
997                                 ret);
998                         return ret;
999                 }
1000         }
1001         return 0;
1002 }
1003
1004 static int
1005 dpaa2_eventdev_create(const char *name)
1006 {
1007         struct rte_eventdev *eventdev;
1008         struct dpaa2_eventdev *priv;
1009         struct dpaa2_dpcon_dev *dpcon_dev = NULL;
1010         struct dpaa2_dpci_dev *dpci_dev = NULL;
1011         int ret;
1012
1013         eventdev = rte_event_pmd_vdev_init(name,
1014                                            sizeof(struct dpaa2_eventdev),
1015                                            rte_socket_id());
1016         if (eventdev == NULL) {
1017                 DPAA2_EVENTDEV_ERR("Failed to create Event device %s", name);
1018                 goto fail;
1019         }
1020
1021         eventdev->dev_ops       = &dpaa2_eventdev_ops;
1022         eventdev->enqueue       = dpaa2_eventdev_enqueue;
1023         eventdev->enqueue_burst = dpaa2_eventdev_enqueue_burst;
1024         eventdev->enqueue_new_burst = dpaa2_eventdev_enqueue_burst;
1025         eventdev->enqueue_forward_burst = dpaa2_eventdev_enqueue_burst;
1026         eventdev->dequeue       = dpaa2_eventdev_dequeue;
1027         eventdev->dequeue_burst = dpaa2_eventdev_dequeue_burst;
1028
1029         /* For secondary processes, the primary has done all the work */
1030         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1031                 return 0;
1032
1033         priv = eventdev->data->dev_private;
1034         priv->max_event_queues = 0;
1035
1036         do {
1037                 dpcon_dev = rte_dpaa2_alloc_dpcon_dev();
1038                 if (!dpcon_dev)
1039                         break;
1040                 priv->evq_info[priv->max_event_queues].dpcon = dpcon_dev;
1041
1042                 dpci_dev = rte_dpaa2_alloc_dpci_dev();
1043                 if (!dpci_dev) {
1044                         rte_dpaa2_free_dpcon_dev(dpcon_dev);
1045                         break;
1046                 }
1047                 priv->evq_info[priv->max_event_queues].dpci = dpci_dev;
1048
1049                 ret = dpaa2_eventdev_setup_dpci(dpci_dev, dpcon_dev);
1050                 if (ret) {
1051                         DPAA2_EVENTDEV_ERR(
1052                                     "DPCI setup failed: err(%d)", ret);
1053                         return ret;
1054                 }
1055                 priv->max_event_queues++;
1056         } while (dpcon_dev && dpci_dev);
1057
1058         RTE_LOG(INFO, PMD, "%s eventdev created\n", name);
1059
1060         return 0;
1061 fail:
1062         return -EFAULT;
1063 }
1064
1065 static int
1066 dpaa2_eventdev_probe(struct rte_vdev_device *vdev)
1067 {
1068         const char *name;
1069
1070         name = rte_vdev_device_name(vdev);
1071         DPAA2_EVENTDEV_INFO("Initializing %s", name);
1072         return dpaa2_eventdev_create(name);
1073 }
1074
1075 static int
1076 dpaa2_eventdev_remove(struct rte_vdev_device *vdev)
1077 {
1078         const char *name;
1079
1080         name = rte_vdev_device_name(vdev);
1081         DPAA2_EVENTDEV_INFO("Closing %s", name);
1082
1083         return rte_event_pmd_vdev_uninit(name);
1084 }
1085
1086 static struct rte_vdev_driver vdev_eventdev_dpaa2_pmd = {
1087         .probe = dpaa2_eventdev_probe,
1088         .remove = dpaa2_eventdev_remove
1089 };
1090
1091 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_DPAA2_PMD, vdev_eventdev_dpaa2_pmd);
1092
1093 RTE_INIT(dpaa2_eventdev_init_log)
1094 {
1095         dpaa2_logtype_event = rte_log_register("pmd.event.dpaa2");
1096         if (dpaa2_logtype_event >= 0)
1097                 rte_log_set_level(dpaa2_logtype_event, RTE_LOG_NOTICE);
1098 }