f14a3edd34e5853f75c862497616dd6bb2b6aceb
[dpdk.git] / drivers / event / cnxk / cn10k_eventdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "cn10k_worker.h"
6 #include "cnxk_eventdev.h"
7 #include "cnxk_worker.h"
8
9 #define CN10K_SET_EVDEV_DEQ_OP(dev, deq_op, deq_ops)                           \
10         (deq_op = deq_ops[!!(dev->rx_offloads & NIX_RX_OFFLOAD_VLAN_STRIP_F)]  \
11                          [!!(dev->rx_offloads & NIX_RX_OFFLOAD_TSTAMP_F)]      \
12                          [!!(dev->rx_offloads & NIX_RX_OFFLOAD_MARK_UPDATE_F)] \
13                          [!!(dev->rx_offloads & NIX_RX_OFFLOAD_CHECKSUM_F)]    \
14                          [!!(dev->rx_offloads & NIX_RX_OFFLOAD_PTYPE_F)]       \
15                          [!!(dev->rx_offloads & NIX_RX_OFFLOAD_RSS_F)])
16
17 #define CN10K_SET_EVDEV_ENQ_OP(dev, enq_op, enq_ops)                           \
18         (enq_op =                                                              \
19                  enq_ops[!!(dev->tx_offloads & NIX_TX_OFFLOAD_TSTAMP_F)]       \
20                         [!!(dev->tx_offloads & NIX_TX_OFFLOAD_TSO_F)]          \
21                         [!!(dev->tx_offloads & NIX_TX_OFFLOAD_MBUF_NOFF_F)]    \
22                         [!!(dev->tx_offloads & NIX_TX_OFFLOAD_VLAN_QINQ_F)]    \
23                         [!!(dev->tx_offloads & NIX_TX_OFFLOAD_OL3_OL4_CSUM_F)] \
24                         [!!(dev->tx_offloads & NIX_TX_OFFLOAD_L3_L4_CSUM_F)])
25
26 static uint32_t
27 cn10k_sso_gw_mode_wdata(struct cnxk_sso_evdev *dev)
28 {
29         uint32_t wdata = BIT(16) | 1;
30
31         switch (dev->gw_mode) {
32         case CN10K_GW_MODE_NONE:
33         default:
34                 break;
35         case CN10K_GW_MODE_PREF:
36                 wdata |= BIT(19);
37                 break;
38         case CN10K_GW_MODE_PREF_WFE:
39                 wdata |= BIT(20) | BIT(19);
40                 break;
41         }
42
43         return wdata;
44 }
45
46 static void *
47 cn10k_sso_init_hws_mem(void *arg, uint8_t port_id)
48 {
49         struct cnxk_sso_evdev *dev = arg;
50         struct cn10k_sso_hws *ws;
51
52         /* Allocate event port memory */
53         ws = rte_zmalloc("cn10k_ws",
54                          sizeof(struct cn10k_sso_hws) + RTE_CACHE_LINE_SIZE,
55                          RTE_CACHE_LINE_SIZE);
56         if (ws == NULL) {
57                 plt_err("Failed to alloc memory for port=%d", port_id);
58                 return NULL;
59         }
60
61         /* First cache line is reserved for cookie */
62         ws = (struct cn10k_sso_hws *)((uint8_t *)ws + RTE_CACHE_LINE_SIZE);
63         ws->base = roc_sso_hws_base_get(&dev->sso, port_id);
64         ws->tx_base = ws->base;
65         ws->hws_id = port_id;
66         ws->swtag_req = 0;
67         ws->gw_wdata = cn10k_sso_gw_mode_wdata(dev);
68         ws->lmt_base = dev->sso.lmt_base;
69
70         return ws;
71 }
72
73 static int
74 cn10k_sso_hws_link(void *arg, void *port, uint16_t *map, uint16_t nb_link)
75 {
76         struct cnxk_sso_evdev *dev = arg;
77         struct cn10k_sso_hws *ws = port;
78
79         return roc_sso_hws_link(&dev->sso, ws->hws_id, map, nb_link);
80 }
81
82 static int
83 cn10k_sso_hws_unlink(void *arg, void *port, uint16_t *map, uint16_t nb_link)
84 {
85         struct cnxk_sso_evdev *dev = arg;
86         struct cn10k_sso_hws *ws = port;
87
88         return roc_sso_hws_unlink(&dev->sso, ws->hws_id, map, nb_link);
89 }
90
91 static void
92 cn10k_sso_hws_setup(void *arg, void *hws, uintptr_t *grps_base)
93 {
94         struct cnxk_sso_evdev *dev = arg;
95         struct cn10k_sso_hws *ws = hws;
96         uint64_t val;
97
98         rte_memcpy(ws->grps_base, grps_base,
99                    sizeof(uintptr_t) * CNXK_SSO_MAX_HWGRP);
100         ws->fc_mem = dev->fc_mem;
101         ws->xaq_lmt = dev->xaq_lmt;
102
103         /* Set get_work timeout for HWS */
104         val = NSEC2USEC(dev->deq_tmo_ns) - 1;
105         plt_write64(val, ws->base + SSOW_LF_GWS_NW_TIM);
106 }
107
108 static void
109 cn10k_sso_hws_release(void *arg, void *hws)
110 {
111         struct cnxk_sso_evdev *dev = arg;
112         struct cn10k_sso_hws *ws = hws;
113         int i;
114
115         for (i = 0; i < dev->nb_event_queues; i++)
116                 roc_sso_hws_unlink(&dev->sso, ws->hws_id, (uint16_t *)&i, 1);
117         memset(ws, 0, sizeof(*ws));
118 }
119
120 static void
121 cn10k_sso_hws_flush_events(void *hws, uint8_t queue_id, uintptr_t base,
122                            cnxk_handle_event_t fn, void *arg)
123 {
124         struct cn10k_sso_hws *ws = hws;
125         uint64_t cq_ds_cnt = 1;
126         uint64_t aq_cnt = 1;
127         uint64_t ds_cnt = 1;
128         struct rte_event ev;
129         uint64_t val, req;
130
131         plt_write64(0, base + SSO_LF_GGRP_QCTL);
132
133         req = queue_id;     /* GGRP ID */
134         req |= BIT_ULL(18); /* Grouped */
135         req |= BIT_ULL(16); /* WAIT */
136
137         aq_cnt = plt_read64(base + SSO_LF_GGRP_AQ_CNT);
138         ds_cnt = plt_read64(base + SSO_LF_GGRP_MISC_CNT);
139         cq_ds_cnt = plt_read64(base + SSO_LF_GGRP_INT_CNT);
140         cq_ds_cnt &= 0x3FFF3FFF0000;
141
142         while (aq_cnt || cq_ds_cnt || ds_cnt) {
143                 plt_write64(req, ws->base + SSOW_LF_GWS_OP_GET_WORK0);
144                 cn10k_sso_hws_get_work_empty(ws, &ev);
145                 if (fn != NULL && ev.u64 != 0)
146                         fn(arg, ev);
147                 if (ev.sched_type != SSO_TT_EMPTY)
148                         cnxk_sso_hws_swtag_flush(
149                                 ws->base + SSOW_LF_GWS_WQE0,
150                                 ws->base + SSOW_LF_GWS_OP_SWTAG_FLUSH);
151                 do {
152                         val = plt_read64(ws->base + SSOW_LF_GWS_PENDSTATE);
153                 } while (val & BIT_ULL(56));
154                 aq_cnt = plt_read64(base + SSO_LF_GGRP_AQ_CNT);
155                 ds_cnt = plt_read64(base + SSO_LF_GGRP_MISC_CNT);
156                 cq_ds_cnt = plt_read64(base + SSO_LF_GGRP_INT_CNT);
157                 /* Extract cq and ds count */
158                 cq_ds_cnt &= 0x3FFF3FFF0000;
159         }
160
161         plt_write64(0, ws->base + SSOW_LF_GWS_OP_GWC_INVAL);
162         rte_mb();
163 }
164
165 static void
166 cn10k_sso_hws_reset(void *arg, void *hws)
167 {
168         struct cnxk_sso_evdev *dev = arg;
169         struct cn10k_sso_hws *ws = hws;
170         uintptr_t base = ws->base;
171         uint64_t pend_state;
172         union {
173                 __uint128_t wdata;
174                 uint64_t u64[2];
175         } gw;
176         uint8_t pend_tt;
177
178         /* Wait till getwork/swtp/waitw/desched completes. */
179         do {
180                 pend_state = plt_read64(base + SSOW_LF_GWS_PENDSTATE);
181         } while (pend_state & (BIT_ULL(63) | BIT_ULL(62) | BIT_ULL(58) |
182                                BIT_ULL(56) | BIT_ULL(54)));
183         pend_tt = CNXK_TT_FROM_TAG(plt_read64(base + SSOW_LF_GWS_WQE0));
184         if (pend_tt != SSO_TT_EMPTY) { /* Work was pending */
185                 if (pend_tt == SSO_TT_ATOMIC || pend_tt == SSO_TT_ORDERED)
186                         cnxk_sso_hws_swtag_untag(base +
187                                                  SSOW_LF_GWS_OP_SWTAG_UNTAG);
188                 plt_write64(0, base + SSOW_LF_GWS_OP_DESCHED);
189         }
190
191         /* Wait for desched to complete. */
192         do {
193                 pend_state = plt_read64(base + SSOW_LF_GWS_PENDSTATE);
194         } while (pend_state & BIT_ULL(58));
195
196         switch (dev->gw_mode) {
197         case CN10K_GW_MODE_PREF:
198                 while (plt_read64(base + SSOW_LF_GWS_PRF_WQE0) & BIT_ULL(63))
199                         ;
200                 break;
201         case CN10K_GW_MODE_PREF_WFE:
202                 while (plt_read64(base + SSOW_LF_GWS_PRF_WQE0) &
203                        SSOW_LF_GWS_TAG_PEND_GET_WORK_BIT)
204                         continue;
205                 plt_write64(0, base + SSOW_LF_GWS_OP_GWC_INVAL);
206                 break;
207         case CN10K_GW_MODE_NONE:
208         default:
209                 break;
210         }
211
212         if (CNXK_TT_FROM_TAG(plt_read64(base + SSOW_LF_GWS_PRF_WQE0)) !=
213             SSO_TT_EMPTY) {
214                 plt_write64(BIT_ULL(16) | 1,
215                             ws->base + SSOW_LF_GWS_OP_GET_WORK0);
216                 do {
217                         roc_load_pair(gw.u64[0], gw.u64[1],
218                                       ws->base + SSOW_LF_GWS_WQE0);
219                 } while (gw.u64[0] & BIT_ULL(63));
220                 pend_tt = CNXK_TT_FROM_TAG(plt_read64(base + SSOW_LF_GWS_WQE0));
221                 if (pend_tt != SSO_TT_EMPTY) { /* Work was pending */
222                         if (pend_tt == SSO_TT_ATOMIC ||
223                             pend_tt == SSO_TT_ORDERED)
224                                 cnxk_sso_hws_swtag_untag(
225                                         base + SSOW_LF_GWS_OP_SWTAG_UNTAG);
226                         plt_write64(0, base + SSOW_LF_GWS_OP_DESCHED);
227                 }
228         }
229
230         plt_write64(0, base + SSOW_LF_GWS_OP_GWC_INVAL);
231         rte_mb();
232 }
233
234 static void
235 cn10k_sso_set_rsrc(void *arg)
236 {
237         struct cnxk_sso_evdev *dev = arg;
238
239         dev->max_event_ports = dev->sso.max_hws;
240         dev->max_event_queues =
241                 dev->sso.max_hwgrp > RTE_EVENT_MAX_QUEUES_PER_DEV ?
242                               RTE_EVENT_MAX_QUEUES_PER_DEV :
243                               dev->sso.max_hwgrp;
244 }
245
246 static int
247 cn10k_sso_rsrc_init(void *arg, uint8_t hws, uint8_t hwgrp)
248 {
249         struct cnxk_sso_evdev *dev = arg;
250
251         return roc_sso_rsrc_init(&dev->sso, hws, hwgrp);
252 }
253
254 static int
255 cn10k_sso_updt_tx_adptr_data(const struct rte_eventdev *event_dev)
256 {
257         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
258         int i;
259
260         if (dev->tx_adptr_data == NULL)
261                 return 0;
262
263         for (i = 0; i < dev->nb_event_ports; i++) {
264                 struct cn10k_sso_hws *ws = event_dev->data->ports[i];
265                 void *ws_cookie;
266
267                 ws_cookie = cnxk_sso_hws_get_cookie(ws);
268                 ws_cookie = rte_realloc_socket(
269                         ws_cookie,
270                         sizeof(struct cnxk_sso_hws_cookie) +
271                                 sizeof(struct cn10k_sso_hws) +
272                                 (sizeof(uint64_t) * (dev->max_port_id + 1) *
273                                  RTE_MAX_QUEUES_PER_PORT),
274                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
275                 if (ws_cookie == NULL)
276                         return -ENOMEM;
277                 ws = RTE_PTR_ADD(ws_cookie, sizeof(struct cnxk_sso_hws_cookie));
278                 memcpy(&ws->tx_adptr_data, dev->tx_adptr_data,
279                        sizeof(uint64_t) * (dev->max_port_id + 1) *
280                                RTE_MAX_QUEUES_PER_PORT);
281                 event_dev->data->ports[i] = ws;
282         }
283
284         return 0;
285 }
286
287 static void
288 cn10k_sso_fp_fns_set(struct rte_eventdev *event_dev)
289 {
290         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
291         const event_dequeue_t sso_hws_deq[2][2][2][2][2][2] = {
292 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
293         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_##name,
294                 NIX_RX_FASTPATH_MODES
295 #undef R
296         };
297
298         const event_dequeue_burst_t sso_hws_deq_burst[2][2][2][2][2][2] = {
299 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
300         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_burst_##name,
301                 NIX_RX_FASTPATH_MODES
302 #undef R
303         };
304
305         const event_dequeue_t sso_hws_deq_tmo[2][2][2][2][2][2] = {
306 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
307         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_tmo_##name,
308                 NIX_RX_FASTPATH_MODES
309 #undef R
310         };
311
312         const event_dequeue_burst_t sso_hws_deq_tmo_burst[2][2][2][2][2][2] = {
313 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
314         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_tmo_burst_##name,
315                 NIX_RX_FASTPATH_MODES
316 #undef R
317         };
318
319         const event_dequeue_t sso_hws_deq_seg[2][2][2][2][2][2] = {
320 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
321         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_seg_##name,
322                 NIX_RX_FASTPATH_MODES
323 #undef R
324         };
325
326         const event_dequeue_burst_t sso_hws_deq_seg_burst[2][2][2][2][2][2] = {
327 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
328         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_seg_burst_##name,
329                 NIX_RX_FASTPATH_MODES
330 #undef R
331         };
332
333         const event_dequeue_t sso_hws_deq_tmo_seg[2][2][2][2][2][2] = {
334 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
335         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_tmo_seg_##name,
336                 NIX_RX_FASTPATH_MODES
337 #undef R
338         };
339
340         const event_dequeue_burst_t
341                 sso_hws_deq_tmo_seg_burst[2][2][2][2][2][2] = {
342 #define R(name, f5, f4, f3, f2, f1, f0, flags)                                 \
343         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_deq_tmo_seg_burst_##name,
344                         NIX_RX_FASTPATH_MODES
345 #undef R
346                 };
347
348         /* Tx modes */
349         const event_tx_adapter_enqueue
350                 sso_hws_tx_adptr_enq[2][2][2][2][2][2] = {
351 #define T(name, f5, f4, f3, f2, f1, f0, sz, flags)                             \
352         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_tx_adptr_enq_##name,
353                         NIX_TX_FASTPATH_MODES
354 #undef T
355                 };
356
357         const event_tx_adapter_enqueue
358                 sso_hws_tx_adptr_enq_seg[2][2][2][2][2][2] = {
359 #define T(name, f5, f4, f3, f2, f1, f0, sz, flags)                             \
360         [f5][f4][f3][f2][f1][f0] = cn10k_sso_hws_tx_adptr_enq_seg_##name,
361                         NIX_TX_FASTPATH_MODES
362 #undef T
363                 };
364
365         event_dev->enqueue = cn10k_sso_hws_enq;
366         event_dev->enqueue_burst = cn10k_sso_hws_enq_burst;
367         event_dev->enqueue_new_burst = cn10k_sso_hws_enq_new_burst;
368         event_dev->enqueue_forward_burst = cn10k_sso_hws_enq_fwd_burst;
369         if (dev->rx_offloads & NIX_RX_MULTI_SEG_F) {
370                 CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue,
371                                        sso_hws_deq_seg);
372                 CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue_burst,
373                                        sso_hws_deq_seg_burst);
374                 if (dev->is_timeout_deq) {
375                         CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue,
376                                                sso_hws_deq_tmo_seg);
377                         CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue_burst,
378                                                sso_hws_deq_tmo_seg_burst);
379                 }
380         } else {
381                 CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue, sso_hws_deq);
382                 CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue_burst,
383                                        sso_hws_deq_burst);
384                 if (dev->is_timeout_deq) {
385                         CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue,
386                                                sso_hws_deq_tmo);
387                         CN10K_SET_EVDEV_DEQ_OP(dev, event_dev->dequeue_burst,
388                                                sso_hws_deq_tmo_burst);
389                 }
390         }
391
392         if (dev->tx_offloads & NIX_TX_MULTI_SEG_F)
393                 CN10K_SET_EVDEV_ENQ_OP(dev, event_dev->txa_enqueue,
394                                        sso_hws_tx_adptr_enq_seg);
395         else
396                 CN10K_SET_EVDEV_ENQ_OP(dev, event_dev->txa_enqueue,
397                                        sso_hws_tx_adptr_enq);
398
399         event_dev->txa_enqueue_same_dest = event_dev->txa_enqueue;
400 }
401
402 static void
403 cn10k_sso_info_get(struct rte_eventdev *event_dev,
404                    struct rte_event_dev_info *dev_info)
405 {
406         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
407
408         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_CN10K_PMD);
409         cnxk_sso_info_get(dev, dev_info);
410 }
411
412 static int
413 cn10k_sso_dev_configure(const struct rte_eventdev *event_dev)
414 {
415         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
416         int rc;
417
418         rc = cnxk_sso_dev_validate(event_dev);
419         if (rc < 0) {
420                 plt_err("Invalid event device configuration");
421                 return -EINVAL;
422         }
423
424         roc_sso_rsrc_fini(&dev->sso);
425
426         rc = cn10k_sso_rsrc_init(dev, dev->nb_event_ports,
427                                  dev->nb_event_queues);
428         if (rc < 0) {
429                 plt_err("Failed to initialize SSO resources");
430                 return -ENODEV;
431         }
432
433         rc = cnxk_sso_xaq_allocate(dev);
434         if (rc < 0)
435                 goto cnxk_rsrc_fini;
436
437         rc = cnxk_setup_event_ports(event_dev, cn10k_sso_init_hws_mem,
438                                     cn10k_sso_hws_setup);
439         if (rc < 0)
440                 goto cnxk_rsrc_fini;
441
442         /* Restore any prior port-queue mapping. */
443         cnxk_sso_restore_links(event_dev, cn10k_sso_hws_link);
444
445         dev->configured = 1;
446         rte_mb();
447
448         return 0;
449 cnxk_rsrc_fini:
450         roc_sso_rsrc_fini(&dev->sso);
451         dev->nb_event_ports = 0;
452         return rc;
453 }
454
455 static int
456 cn10k_sso_port_setup(struct rte_eventdev *event_dev, uint8_t port_id,
457                      const struct rte_event_port_conf *port_conf)
458 {
459
460         RTE_SET_USED(port_conf);
461         return cnxk_sso_port_setup(event_dev, port_id, cn10k_sso_hws_setup);
462 }
463
464 static void
465 cn10k_sso_port_release(void *port)
466 {
467         struct cnxk_sso_hws_cookie *gws_cookie = cnxk_sso_hws_get_cookie(port);
468         struct cnxk_sso_evdev *dev;
469
470         if (port == NULL)
471                 return;
472
473         dev = cnxk_sso_pmd_priv(gws_cookie->event_dev);
474         if (!gws_cookie->configured)
475                 goto free;
476
477         cn10k_sso_hws_release(dev, port);
478         memset(gws_cookie, 0, sizeof(*gws_cookie));
479 free:
480         rte_free(gws_cookie);
481 }
482
483 static int
484 cn10k_sso_port_link(struct rte_eventdev *event_dev, void *port,
485                     const uint8_t queues[], const uint8_t priorities[],
486                     uint16_t nb_links)
487 {
488         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
489         uint16_t hwgrp_ids[nb_links];
490         uint16_t link;
491
492         RTE_SET_USED(priorities);
493         for (link = 0; link < nb_links; link++)
494                 hwgrp_ids[link] = queues[link];
495         nb_links = cn10k_sso_hws_link(dev, port, hwgrp_ids, nb_links);
496
497         return (int)nb_links;
498 }
499
500 static int
501 cn10k_sso_port_unlink(struct rte_eventdev *event_dev, void *port,
502                       uint8_t queues[], uint16_t nb_unlinks)
503 {
504         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
505         uint16_t hwgrp_ids[nb_unlinks];
506         uint16_t unlink;
507
508         for (unlink = 0; unlink < nb_unlinks; unlink++)
509                 hwgrp_ids[unlink] = queues[unlink];
510         nb_unlinks = cn10k_sso_hws_unlink(dev, port, hwgrp_ids, nb_unlinks);
511
512         return (int)nb_unlinks;
513 }
514
515 static int
516 cn10k_sso_start(struct rte_eventdev *event_dev)
517 {
518         int rc;
519
520         rc = cn10k_sso_updt_tx_adptr_data(event_dev);
521         if (rc < 0)
522                 return rc;
523
524         rc = cnxk_sso_start(event_dev, cn10k_sso_hws_reset,
525                             cn10k_sso_hws_flush_events);
526         if (rc < 0)
527                 return rc;
528         cn10k_sso_fp_fns_set(event_dev);
529
530         return rc;
531 }
532
533 static void
534 cn10k_sso_stop(struct rte_eventdev *event_dev)
535 {
536         cnxk_sso_stop(event_dev, cn10k_sso_hws_reset,
537                       cn10k_sso_hws_flush_events);
538 }
539
540 static int
541 cn10k_sso_close(struct rte_eventdev *event_dev)
542 {
543         return cnxk_sso_close(event_dev, cn10k_sso_hws_unlink);
544 }
545
546 static int
547 cn10k_sso_selftest(void)
548 {
549         return cnxk_sso_selftest(RTE_STR(event_cn10k));
550 }
551
552 static int
553 cn10k_sso_rx_adapter_caps_get(const struct rte_eventdev *event_dev,
554                               const struct rte_eth_dev *eth_dev, uint32_t *caps)
555 {
556         int rc;
557
558         RTE_SET_USED(event_dev);
559         rc = strncmp(eth_dev->device->driver->name, "net_cn10k", 9);
560         if (rc)
561                 *caps = RTE_EVENT_ETH_RX_ADAPTER_SW_CAP;
562         else
563                 *caps = RTE_EVENT_ETH_RX_ADAPTER_CAP_INTERNAL_PORT |
564                         RTE_EVENT_ETH_RX_ADAPTER_CAP_MULTI_EVENTQ |
565                         RTE_EVENT_ETH_RX_ADAPTER_CAP_OVERRIDE_FLOW_ID |
566                         RTE_EVENT_ETH_RX_ADAPTER_CAP_EVENT_VECTOR;
567
568         return 0;
569 }
570
571 static void
572 cn10k_sso_set_priv_mem(const struct rte_eventdev *event_dev, void *lookup_mem,
573                        void *tstmp_info)
574 {
575         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
576         int i;
577
578         for (i = 0; i < dev->nb_event_ports; i++) {
579                 struct cn10k_sso_hws *ws = event_dev->data->ports[i];
580                 ws->lookup_mem = lookup_mem;
581                 ws->tstamp = tstmp_info;
582         }
583 }
584
585 static int
586 cn10k_sso_rx_adapter_queue_add(
587         const struct rte_eventdev *event_dev, const struct rte_eth_dev *eth_dev,
588         int32_t rx_queue_id,
589         const struct rte_event_eth_rx_adapter_queue_conf *queue_conf)
590 {
591         struct cn10k_eth_rxq *rxq;
592         void *lookup_mem;
593         void *tstmp_info;
594         int rc;
595
596         rc = strncmp(eth_dev->device->driver->name, "net_cn10k", 8);
597         if (rc)
598                 return -EINVAL;
599
600         rc = cnxk_sso_rx_adapter_queue_add(event_dev, eth_dev, rx_queue_id,
601                                            queue_conf);
602         if (rc)
603                 return -EINVAL;
604         rxq = eth_dev->data->rx_queues[0];
605         lookup_mem = rxq->lookup_mem;
606         tstmp_info = rxq->tstamp;
607         cn10k_sso_set_priv_mem(event_dev, lookup_mem, tstmp_info);
608         cn10k_sso_fp_fns_set((struct rte_eventdev *)(uintptr_t)event_dev);
609
610         return 0;
611 }
612
613 static int
614 cn10k_sso_rx_adapter_queue_del(const struct rte_eventdev *event_dev,
615                                const struct rte_eth_dev *eth_dev,
616                                int32_t rx_queue_id)
617 {
618         int rc;
619
620         rc = strncmp(eth_dev->device->driver->name, "net_cn10k", 8);
621         if (rc)
622                 return -EINVAL;
623
624         return cnxk_sso_rx_adapter_queue_del(event_dev, eth_dev, rx_queue_id);
625 }
626
627 static int
628 cn10k_sso_rx_adapter_vector_limits(
629         const struct rte_eventdev *dev, const struct rte_eth_dev *eth_dev,
630         struct rte_event_eth_rx_adapter_vector_limits *limits)
631 {
632         struct cnxk_eth_dev *cnxk_eth_dev;
633         int ret;
634
635         RTE_SET_USED(dev);
636         ret = strncmp(eth_dev->device->driver->name, "net_cn10k", 8);
637         if (ret)
638                 return -ENOTSUP;
639
640         cnxk_eth_dev = cnxk_eth_pmd_priv(eth_dev);
641         limits->log2_sz = true;
642         limits->min_sz = 1 << ROC_NIX_VWQE_MIN_SIZE_LOG2;
643         limits->max_sz = 1 << ROC_NIX_VWQE_MAX_SIZE_LOG2;
644         limits->min_timeout_ns =
645                 (roc_nix_get_vwqe_interval(&cnxk_eth_dev->nix) + 1) * 100;
646         limits->max_timeout_ns = BITMASK_ULL(8, 0) * limits->min_timeout_ns;
647
648         return 0;
649 }
650
651 static int
652 cnxk_sso_rx_adapter_vwqe_enable(struct cnxk_eth_dev *cnxk_eth_dev,
653                                 uint16_t port_id, uint16_t rq_id, uint16_t sz,
654                                 uint64_t tmo_ns, struct rte_mempool *vmp)
655 {
656         struct roc_nix_rq *rq;
657
658         rq = &cnxk_eth_dev->rqs[rq_id];
659
660         if (!rq->sso_ena)
661                 return -EINVAL;
662         if (rq->flow_tag_width == 0)
663                 return -EINVAL;
664
665         rq->vwqe_ena = 1;
666         rq->vwqe_first_skip = 0;
667         rq->vwqe_aura_handle = roc_npa_aura_handle_to_aura(vmp->pool_id);
668         rq->vwqe_max_sz_exp = rte_log2_u32(sz);
669         rq->vwqe_wait_tmo =
670                 tmo_ns /
671                 ((roc_nix_get_vwqe_interval(&cnxk_eth_dev->nix) + 1) * 100);
672         rq->tag_mask = (port_id & 0xF) << 20;
673         rq->tag_mask |=
674                 (((port_id >> 4) & 0xF) | (RTE_EVENT_TYPE_ETHDEV_VECTOR << 4))
675                 << 24;
676
677         return roc_nix_rq_modify(&cnxk_eth_dev->nix, rq, 0);
678 }
679
680 static int
681 cn10k_sso_rx_adapter_vector_config(
682         const struct rte_eventdev *event_dev, const struct rte_eth_dev *eth_dev,
683         int32_t rx_queue_id,
684         const struct rte_event_eth_rx_adapter_event_vector_config *config)
685 {
686         struct cnxk_eth_dev *cnxk_eth_dev;
687         struct cnxk_sso_evdev *dev;
688         int i, rc;
689
690         rc = strncmp(eth_dev->device->driver->name, "net_cn10k", 8);
691         if (rc)
692                 return -EINVAL;
693
694         dev = cnxk_sso_pmd_priv(event_dev);
695         cnxk_eth_dev = cnxk_eth_pmd_priv(eth_dev);
696         if (rx_queue_id < 0) {
697                 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
698                         cnxk_sso_updt_xae_cnt(dev, config->vector_mp,
699                                               RTE_EVENT_TYPE_ETHDEV_VECTOR);
700                         rc = cnxk_sso_xae_reconfigure(
701                                 (struct rte_eventdev *)(uintptr_t)event_dev);
702                         rc = cnxk_sso_rx_adapter_vwqe_enable(
703                                 cnxk_eth_dev, eth_dev->data->port_id, i,
704                                 config->vector_sz, config->vector_timeout_ns,
705                                 config->vector_mp);
706                         if (rc)
707                                 return -EINVAL;
708                 }
709         } else {
710
711                 cnxk_sso_updt_xae_cnt(dev, config->vector_mp,
712                                       RTE_EVENT_TYPE_ETHDEV_VECTOR);
713                 rc = cnxk_sso_xae_reconfigure(
714                         (struct rte_eventdev *)(uintptr_t)event_dev);
715                 rc = cnxk_sso_rx_adapter_vwqe_enable(
716                         cnxk_eth_dev, eth_dev->data->port_id, rx_queue_id,
717                         config->vector_sz, config->vector_timeout_ns,
718                         config->vector_mp);
719                 if (rc)
720                         return -EINVAL;
721         }
722
723         return 0;
724 }
725
726 static int
727 cn10k_sso_tx_adapter_caps_get(const struct rte_eventdev *dev,
728                               const struct rte_eth_dev *eth_dev, uint32_t *caps)
729 {
730         int ret;
731
732         RTE_SET_USED(dev);
733         ret = strncmp(eth_dev->device->driver->name, "net_cn10k", 8);
734         if (ret)
735                 *caps = 0;
736         else
737                 *caps = RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT |
738                         RTE_EVENT_ETH_TX_ADAPTER_CAP_EVENT_VECTOR;
739
740         return 0;
741 }
742
743 static int
744 cn10k_sso_tx_adapter_queue_add(uint8_t id, const struct rte_eventdev *event_dev,
745                                const struct rte_eth_dev *eth_dev,
746                                int32_t tx_queue_id)
747 {
748         int rc;
749
750         RTE_SET_USED(id);
751         rc = cnxk_sso_tx_adapter_queue_add(event_dev, eth_dev, tx_queue_id);
752         if (rc < 0)
753                 return rc;
754         rc = cn10k_sso_updt_tx_adptr_data(event_dev);
755         if (rc < 0)
756                 return rc;
757         cn10k_sso_fp_fns_set((struct rte_eventdev *)(uintptr_t)event_dev);
758
759         return 0;
760 }
761
762 static int
763 cn10k_sso_tx_adapter_queue_del(uint8_t id, const struct rte_eventdev *event_dev,
764                                const struct rte_eth_dev *eth_dev,
765                                int32_t tx_queue_id)
766 {
767         int rc;
768
769         RTE_SET_USED(id);
770         rc = cnxk_sso_tx_adapter_queue_del(event_dev, eth_dev, tx_queue_id);
771         if (rc < 0)
772                 return rc;
773         return cn10k_sso_updt_tx_adptr_data(event_dev);
774 }
775
776 static struct rte_eventdev_ops cn10k_sso_dev_ops = {
777         .dev_infos_get = cn10k_sso_info_get,
778         .dev_configure = cn10k_sso_dev_configure,
779         .queue_def_conf = cnxk_sso_queue_def_conf,
780         .queue_setup = cnxk_sso_queue_setup,
781         .queue_release = cnxk_sso_queue_release,
782         .port_def_conf = cnxk_sso_port_def_conf,
783         .port_setup = cn10k_sso_port_setup,
784         .port_release = cn10k_sso_port_release,
785         .port_link = cn10k_sso_port_link,
786         .port_unlink = cn10k_sso_port_unlink,
787         .timeout_ticks = cnxk_sso_timeout_ticks,
788
789         .eth_rx_adapter_caps_get = cn10k_sso_rx_adapter_caps_get,
790         .eth_rx_adapter_queue_add = cn10k_sso_rx_adapter_queue_add,
791         .eth_rx_adapter_queue_del = cn10k_sso_rx_adapter_queue_del,
792         .eth_rx_adapter_start = cnxk_sso_rx_adapter_start,
793         .eth_rx_adapter_stop = cnxk_sso_rx_adapter_stop,
794
795         .eth_rx_adapter_vector_limits_get = cn10k_sso_rx_adapter_vector_limits,
796         .eth_rx_adapter_event_vector_config =
797                 cn10k_sso_rx_adapter_vector_config,
798
799         .eth_tx_adapter_caps_get = cn10k_sso_tx_adapter_caps_get,
800         .eth_tx_adapter_queue_add = cn10k_sso_tx_adapter_queue_add,
801         .eth_tx_adapter_queue_del = cn10k_sso_tx_adapter_queue_del,
802
803         .timer_adapter_caps_get = cnxk_tim_caps_get,
804
805         .dump = cnxk_sso_dump,
806         .dev_start = cn10k_sso_start,
807         .dev_stop = cn10k_sso_stop,
808         .dev_close = cn10k_sso_close,
809         .dev_selftest = cn10k_sso_selftest,
810 };
811
812 static int
813 cn10k_sso_init(struct rte_eventdev *event_dev)
814 {
815         struct cnxk_sso_evdev *dev = cnxk_sso_pmd_priv(event_dev);
816         int rc;
817
818         if (RTE_CACHE_LINE_SIZE != 64) {
819                 plt_err("Driver not compiled for CN10K");
820                 return -EFAULT;
821         }
822
823         rc = roc_plt_init();
824         if (rc < 0) {
825                 plt_err("Failed to initialize platform model");
826                 return rc;
827         }
828
829         event_dev->dev_ops = &cn10k_sso_dev_ops;
830         /* For secondary processes, the primary has done all the work */
831         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
832                 cn10k_sso_fp_fns_set(event_dev);
833                 return 0;
834         }
835
836         rc = cnxk_sso_init(event_dev);
837         if (rc < 0)
838                 return rc;
839
840         cn10k_sso_set_rsrc(cnxk_sso_pmd_priv(event_dev));
841         if (!dev->max_event_ports || !dev->max_event_queues) {
842                 plt_err("Not enough eventdev resource queues=%d ports=%d",
843                         dev->max_event_queues, dev->max_event_ports);
844                 cnxk_sso_fini(event_dev);
845                 return -ENODEV;
846         }
847
848         plt_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
849                     event_dev->data->name, dev->max_event_queues,
850                     dev->max_event_ports);
851
852         return 0;
853 }
854
855 static int
856 cn10k_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
857 {
858         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
859                                        sizeof(struct cnxk_sso_evdev),
860                                        cn10k_sso_init);
861 }
862
863 static const struct rte_pci_id cn10k_pci_sso_map[] = {
864         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KA, PCI_DEVID_CNXK_RVU_SSO_TIM_PF),
865         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KAS, PCI_DEVID_CNXK_RVU_SSO_TIM_PF),
866         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KA, PCI_DEVID_CNXK_RVU_SSO_TIM_VF),
867         CNXK_PCI_ID(PCI_SUBSYSTEM_DEVID_CN10KAS, PCI_DEVID_CNXK_RVU_SSO_TIM_VF),
868         {
869                 .vendor_id = 0,
870         },
871 };
872
873 static struct rte_pci_driver cn10k_pci_sso = {
874         .id_table = cn10k_pci_sso_map,
875         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA,
876         .probe = cn10k_sso_probe,
877         .remove = cnxk_sso_remove,
878 };
879
880 RTE_PMD_REGISTER_PCI(event_cn10k, cn10k_pci_sso);
881 RTE_PMD_REGISTER_PCI_TABLE(event_cn10k, cn10k_pci_sso_map);
882 RTE_PMD_REGISTER_KMOD_DEP(event_cn10k, "vfio-pci");
883 RTE_PMD_REGISTER_PARAM_STRING(event_cn10k, CNXK_SSO_XAE_CNT "=<int>"
884                               CNXK_SSO_GGRP_QOS "=<string>"
885                               CNXK_SSO_FORCE_BP "=1"
886                               CN10K_SSO_GW_MODE "=<int>"
887                               CNXK_TIM_DISABLE_NPA "=1"
888                               CNXK_TIM_CHNK_SLOTS "=<int>"
889                               CNXK_TIM_RINGS_LMT "=<int>"
890                               CNXK_TIM_STATS_ENA "=1");