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