event/octeontx2: add Rx adapter
[dpdk.git] / drivers / event / octeontx2 / otx2_evdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_bus_pci.h>
8 #include <rte_common.h>
9 #include <rte_eal.h>
10 #include <rte_eventdev_pmd_pci.h>
11 #include <rte_kvargs.h>
12 #include <rte_mbuf_pool_ops.h>
13 #include <rte_pci.h>
14
15 #include "otx2_evdev_stats.h"
16 #include "otx2_evdev.h"
17 #include "otx2_irq.h"
18 #include "otx2_tim_evdev.h"
19
20 static inline int
21 sso_get_msix_offsets(const struct rte_eventdev *event_dev)
22 {
23         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
24         uint8_t nb_ports = dev->nb_event_ports * (dev->dual_ws ? 2 : 1);
25         struct otx2_mbox *mbox = dev->mbox;
26         struct msix_offset_rsp *msix_rsp;
27         int i, rc;
28
29         /* Get SSO and SSOW MSIX vector offsets */
30         otx2_mbox_alloc_msg_msix_offset(mbox);
31         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
32
33         for (i = 0; i < nb_ports; i++)
34                 dev->ssow_msixoff[i] = msix_rsp->ssow_msixoff[i];
35
36         for (i = 0; i < dev->nb_event_queues; i++)
37                 dev->sso_msixoff[i] = msix_rsp->sso_msixoff[i];
38
39         return rc;
40 }
41
42 void
43 sso_fastpath_fns_set(struct rte_eventdev *event_dev)
44 {
45         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
46
47         event_dev->enqueue                      = otx2_ssogws_enq;
48         event_dev->enqueue_burst                = otx2_ssogws_enq_burst;
49         event_dev->enqueue_new_burst            = otx2_ssogws_enq_new_burst;
50         event_dev->enqueue_forward_burst        = otx2_ssogws_enq_fwd_burst;
51
52         event_dev->dequeue                      = otx2_ssogws_deq;
53         event_dev->dequeue_burst                = otx2_ssogws_deq_burst;
54         if (dev->is_timeout_deq) {
55                 event_dev->dequeue              = otx2_ssogws_deq_timeout;
56                 event_dev->dequeue_burst        = otx2_ssogws_deq_timeout_burst;
57         }
58
59         if (dev->dual_ws) {
60                 event_dev->enqueue              = otx2_ssogws_dual_enq;
61                 event_dev->enqueue_burst        = otx2_ssogws_dual_enq_burst;
62                 event_dev->enqueue_new_burst    =
63                                         otx2_ssogws_dual_enq_new_burst;
64                 event_dev->enqueue_forward_burst =
65                                         otx2_ssogws_dual_enq_fwd_burst;
66                 event_dev->dequeue              = otx2_ssogws_dual_deq;
67                 event_dev->dequeue_burst        = otx2_ssogws_dual_deq_burst;
68                 if (dev->is_timeout_deq) {
69                         event_dev->dequeue      = otx2_ssogws_dual_deq_timeout;
70                         event_dev->dequeue_burst =
71                                         otx2_ssogws_dual_deq_timeout_burst;
72                 }
73         }
74         rte_mb();
75 }
76
77 static void
78 otx2_sso_info_get(struct rte_eventdev *event_dev,
79                   struct rte_event_dev_info *dev_info)
80 {
81         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
82
83         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX2_PMD);
84         dev_info->min_dequeue_timeout_ns = dev->min_dequeue_timeout_ns;
85         dev_info->max_dequeue_timeout_ns = dev->max_dequeue_timeout_ns;
86         dev_info->max_event_queues = dev->max_event_queues;
87         dev_info->max_event_queue_flows = (1ULL << 20);
88         dev_info->max_event_queue_priority_levels = 8;
89         dev_info->max_event_priority_levels = 1;
90         dev_info->max_event_ports = dev->max_event_ports;
91         dev_info->max_event_port_dequeue_depth = 1;
92         dev_info->max_event_port_enqueue_depth = 1;
93         dev_info->max_num_events =  dev->max_num_events;
94         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
95                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
96                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES |
97                                         RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
98                                         RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
99                                         RTE_EVENT_DEV_CAP_NONSEQ_MODE;
100 }
101
102 static void
103 sso_port_link_modify(struct otx2_ssogws *ws, uint8_t queue, uint8_t enable)
104 {
105         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
106         uint64_t val;
107
108         val = queue;
109         val |= 0ULL << 12; /* SET 0 */
110         val |= 0x8000800080000000; /* Dont modify rest of the masks */
111         val |= (uint64_t)enable << 14;   /* Enable/Disable Membership. */
112
113         otx2_write64(val, base + SSOW_LF_GWS_GRPMSK_CHG);
114 }
115
116 static int
117 otx2_sso_port_link(struct rte_eventdev *event_dev, void *port,
118                    const uint8_t queues[], const uint8_t priorities[],
119                    uint16_t nb_links)
120 {
121         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
122         uint8_t port_id = 0;
123         uint16_t link;
124
125         RTE_SET_USED(priorities);
126         for (link = 0; link < nb_links; link++) {
127                 if (dev->dual_ws) {
128                         struct otx2_ssogws_dual *ws = port;
129
130                         port_id = ws->port;
131                         sso_port_link_modify((struct otx2_ssogws *)
132                                         &ws->ws_state[0], queues[link], true);
133                         sso_port_link_modify((struct otx2_ssogws *)
134                                         &ws->ws_state[1], queues[link], true);
135                 } else {
136                         struct otx2_ssogws *ws = port;
137
138                         port_id = ws->port;
139                         sso_port_link_modify(ws, queues[link], true);
140                 }
141         }
142         sso_func_trace("Port=%d nb_links=%d", port_id, nb_links);
143
144         return (int)nb_links;
145 }
146
147 static int
148 otx2_sso_port_unlink(struct rte_eventdev *event_dev, void *port,
149                      uint8_t queues[], uint16_t nb_unlinks)
150 {
151         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
152         uint8_t port_id = 0;
153         uint16_t unlink;
154
155         for (unlink = 0; unlink < nb_unlinks; unlink++) {
156                 if (dev->dual_ws) {
157                         struct otx2_ssogws_dual *ws = port;
158
159                         port_id = ws->port;
160                         sso_port_link_modify((struct otx2_ssogws *)
161                                         &ws->ws_state[0], queues[unlink],
162                                         false);
163                         sso_port_link_modify((struct otx2_ssogws *)
164                                         &ws->ws_state[1], queues[unlink],
165                                         false);
166                 } else {
167                         struct otx2_ssogws *ws = port;
168
169                         port_id = ws->port;
170                         sso_port_link_modify(ws, queues[unlink], false);
171                 }
172         }
173         sso_func_trace("Port=%d nb_unlinks=%d", port_id, nb_unlinks);
174
175         return (int)nb_unlinks;
176 }
177
178 static int
179 sso_hw_lf_cfg(struct otx2_mbox *mbox, enum otx2_sso_lf_type type,
180               uint16_t nb_lf, uint8_t attach)
181 {
182         if (attach) {
183                 struct rsrc_attach_req *req;
184
185                 req = otx2_mbox_alloc_msg_attach_resources(mbox);
186                 switch (type) {
187                 case SSO_LF_GGRP:
188                         req->sso = nb_lf;
189                         break;
190                 case SSO_LF_GWS:
191                         req->ssow = nb_lf;
192                         break;
193                 default:
194                         return -EINVAL;
195                 }
196                 req->modify = true;
197                 if (otx2_mbox_process(mbox) < 0)
198                         return -EIO;
199         } else {
200                 struct rsrc_detach_req *req;
201
202                 req = otx2_mbox_alloc_msg_detach_resources(mbox);
203                 switch (type) {
204                 case SSO_LF_GGRP:
205                         req->sso = true;
206                         break;
207                 case SSO_LF_GWS:
208                         req->ssow = true;
209                         break;
210                 default:
211                         return -EINVAL;
212                 }
213                 req->partial = true;
214                 if (otx2_mbox_process(mbox) < 0)
215                         return -EIO;
216         }
217
218         return 0;
219 }
220
221 static int
222 sso_lf_cfg(struct otx2_sso_evdev *dev, struct otx2_mbox *mbox,
223            enum otx2_sso_lf_type type, uint16_t nb_lf, uint8_t alloc)
224 {
225         void *rsp;
226         int rc;
227
228         if (alloc) {
229                 switch (type) {
230                 case SSO_LF_GGRP:
231                         {
232                         struct sso_lf_alloc_req *req_ggrp;
233                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_alloc(mbox);
234                         req_ggrp->hwgrps = nb_lf;
235                         }
236                         break;
237                 case SSO_LF_GWS:
238                         {
239                         struct ssow_lf_alloc_req *req_hws;
240                         req_hws = otx2_mbox_alloc_msg_ssow_lf_alloc(mbox);
241                         req_hws->hws = nb_lf;
242                         }
243                         break;
244                 default:
245                         return -EINVAL;
246                 }
247         } else {
248                 switch (type) {
249                 case SSO_LF_GGRP:
250                         {
251                         struct sso_lf_free_req *req_ggrp;
252                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_free(mbox);
253                         req_ggrp->hwgrps = nb_lf;
254                         }
255                         break;
256                 case SSO_LF_GWS:
257                         {
258                         struct ssow_lf_free_req *req_hws;
259                         req_hws = otx2_mbox_alloc_msg_ssow_lf_free(mbox);
260                         req_hws->hws = nb_lf;
261                         }
262                         break;
263                 default:
264                         return -EINVAL;
265                 }
266         }
267
268         rc = otx2_mbox_process_msg_tmo(mbox, (void **)&rsp, ~0);
269         if (rc < 0)
270                 return rc;
271
272         if (alloc && type == SSO_LF_GGRP) {
273                 struct sso_lf_alloc_rsp *rsp_ggrp = rsp;
274
275                 dev->xaq_buf_size = rsp_ggrp->xaq_buf_size;
276                 dev->xae_waes = rsp_ggrp->xaq_wq_entries;
277                 dev->iue = rsp_ggrp->in_unit_entries;
278         }
279
280         return 0;
281 }
282
283 static void
284 otx2_sso_port_release(void *port)
285 {
286         rte_free(port);
287 }
288
289 static void
290 otx2_sso_queue_release(struct rte_eventdev *event_dev, uint8_t queue_id)
291 {
292         RTE_SET_USED(event_dev);
293         RTE_SET_USED(queue_id);
294 }
295
296 static void
297 sso_clr_links(const struct rte_eventdev *event_dev)
298 {
299         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
300         int i, j;
301
302         for (i = 0; i < dev->nb_event_ports; i++) {
303                 if (dev->dual_ws) {
304                         struct otx2_ssogws_dual *ws;
305
306                         ws = event_dev->data->ports[i];
307                         for (j = 0; j < dev->nb_event_queues; j++) {
308                                 sso_port_link_modify((struct otx2_ssogws *)
309                                                 &ws->ws_state[0], j, false);
310                                 sso_port_link_modify((struct otx2_ssogws *)
311                                                 &ws->ws_state[1], j, false);
312                         }
313                 } else {
314                         struct otx2_ssogws *ws;
315
316                         ws = event_dev->data->ports[i];
317                         for (j = 0; j < dev->nb_event_queues; j++)
318                                 sso_port_link_modify(ws, j, false);
319                 }
320         }
321 }
322
323 static void
324 sso_set_port_ops(struct otx2_ssogws *ws, uintptr_t base)
325 {
326         ws->tag_op              = base + SSOW_LF_GWS_TAG;
327         ws->wqp_op              = base + SSOW_LF_GWS_WQP;
328         ws->getwrk_op           = base + SSOW_LF_GWS_OP_GET_WORK;
329         ws->swtp_op             = base + SSOW_LF_GWS_SWTP;
330         ws->swtag_norm_op       = base + SSOW_LF_GWS_OP_SWTAG_NORM;
331         ws->swtag_desched_op    = base + SSOW_LF_GWS_OP_SWTAG_DESCHED;
332 }
333
334 static int
335 sso_configure_dual_ports(const struct rte_eventdev *event_dev)
336 {
337         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
338         struct otx2_mbox *mbox = dev->mbox;
339         uint8_t vws = 0;
340         uint8_t nb_lf;
341         int i, rc;
342
343         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
344
345         nb_lf = dev->nb_event_ports * 2;
346         /* Ask AF to attach required LFs. */
347         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
348         if (rc < 0) {
349                 otx2_err("Failed to attach SSO GWS LF");
350                 return -ENODEV;
351         }
352
353         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
354                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
355                 otx2_err("Failed to init SSO GWS LF");
356                 return -ENODEV;
357         }
358
359         for (i = 0; i < dev->nb_event_ports; i++) {
360                 struct otx2_ssogws_dual *ws;
361                 uintptr_t base;
362
363                 /* Free memory prior to re-allocation if needed */
364                 if (event_dev->data->ports[i] != NULL) {
365                         ws = event_dev->data->ports[i];
366                         rte_free(ws);
367                         ws = NULL;
368                 }
369
370                 /* Allocate event port memory */
371                 ws = rte_zmalloc_socket("otx2_sso_ws",
372                                         sizeof(struct otx2_ssogws_dual),
373                                         RTE_CACHE_LINE_SIZE,
374                                         event_dev->data->socket_id);
375                 if (ws == NULL) {
376                         otx2_err("Failed to alloc memory for port=%d", i);
377                         rc = -ENOMEM;
378                         break;
379                 }
380
381                 ws->port = i;
382                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | vws << 12);
383                 sso_set_port_ops((struct otx2_ssogws *)&ws->ws_state[0], base);
384                 vws++;
385
386                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | vws << 12);
387                 sso_set_port_ops((struct otx2_ssogws *)&ws->ws_state[1], base);
388                 vws++;
389
390                 event_dev->data->ports[i] = ws;
391         }
392
393         if (rc < 0) {
394                 sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, false);
395                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
396         }
397
398         return rc;
399 }
400
401 static int
402 sso_configure_ports(const struct rte_eventdev *event_dev)
403 {
404         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
405         struct otx2_mbox *mbox = dev->mbox;
406         uint8_t nb_lf;
407         int i, rc;
408
409         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
410
411         nb_lf = dev->nb_event_ports;
412         /* Ask AF to attach required LFs. */
413         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
414         if (rc < 0) {
415                 otx2_err("Failed to attach SSO GWS LF");
416                 return -ENODEV;
417         }
418
419         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
420                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
421                 otx2_err("Failed to init SSO GWS LF");
422                 return -ENODEV;
423         }
424
425         for (i = 0; i < nb_lf; i++) {
426                 struct otx2_ssogws *ws;
427                 uintptr_t base;
428
429                 /* Free memory prior to re-allocation if needed */
430                 if (event_dev->data->ports[i] != NULL) {
431                         ws = event_dev->data->ports[i];
432                         rte_free(ws);
433                         ws = NULL;
434                 }
435
436                 /* Allocate event port memory */
437                 ws = rte_zmalloc_socket("otx2_sso_ws",
438                                         sizeof(struct otx2_ssogws),
439                                         RTE_CACHE_LINE_SIZE,
440                                         event_dev->data->socket_id);
441                 if (ws == NULL) {
442                         otx2_err("Failed to alloc memory for port=%d", i);
443                         rc = -ENOMEM;
444                         break;
445                 }
446
447                 ws->port = i;
448                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | i << 12);
449                 sso_set_port_ops(ws, base);
450
451                 event_dev->data->ports[i] = ws;
452         }
453
454         if (rc < 0) {
455                 sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, false);
456                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
457         }
458
459         return rc;
460 }
461
462 static int
463 sso_configure_queues(const struct rte_eventdev *event_dev)
464 {
465         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
466         struct otx2_mbox *mbox = dev->mbox;
467         uint8_t nb_lf;
468         int rc;
469
470         otx2_sso_dbg("Configuring event queues %d", dev->nb_event_queues);
471
472         nb_lf = dev->nb_event_queues;
473         /* Ask AF to attach required LFs. */
474         rc = sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, true);
475         if (rc < 0) {
476                 otx2_err("Failed to attach SSO GGRP LF");
477                 return -ENODEV;
478         }
479
480         if (sso_lf_cfg(dev, mbox, SSO_LF_GGRP, nb_lf, true) < 0) {
481                 sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, false);
482                 otx2_err("Failed to init SSO GGRP LF");
483                 return -ENODEV;
484         }
485
486         return rc;
487 }
488
489 static int
490 sso_xaq_allocate(struct otx2_sso_evdev *dev)
491 {
492         const struct rte_memzone *mz;
493         struct npa_aura_s *aura;
494         static int reconfig_cnt;
495         char pool_name[RTE_MEMZONE_NAMESIZE];
496         uint32_t xaq_cnt;
497         int rc;
498
499         if (dev->xaq_pool)
500                 rte_mempool_free(dev->xaq_pool);
501
502         /*
503          * Allocate memory for Add work backpressure.
504          */
505         mz = rte_memzone_lookup(OTX2_SSO_FC_NAME);
506         if (mz == NULL)
507                 mz = rte_memzone_reserve_aligned(OTX2_SSO_FC_NAME,
508                                                  OTX2_ALIGN +
509                                                  sizeof(struct npa_aura_s),
510                                                  rte_socket_id(),
511                                                  RTE_MEMZONE_IOVA_CONTIG,
512                                                  OTX2_ALIGN);
513         if (mz == NULL) {
514                 otx2_err("Failed to allocate mem for fcmem");
515                 return -ENOMEM;
516         }
517
518         dev->fc_iova = mz->iova;
519         dev->fc_mem = mz->addr;
520
521         aura = (struct npa_aura_s *)((uintptr_t)dev->fc_mem + OTX2_ALIGN);
522         memset(aura, 0, sizeof(struct npa_aura_s));
523
524         aura->fc_ena = 1;
525         aura->fc_addr = dev->fc_iova;
526         aura->fc_hyst_bits = 0; /* Store count on all updates */
527
528         /* Taken from HRM 14.3.3(4) */
529         xaq_cnt = dev->nb_event_queues * OTX2_SSO_XAQ_CACHE_CNT;
530         if (dev->xae_cnt)
531                 xaq_cnt += dev->xae_cnt / dev->xae_waes;
532         else if (dev->adptr_xae_cnt)
533                 xaq_cnt += (dev->adptr_xae_cnt / dev->xae_waes) +
534                         (OTX2_SSO_XAQ_SLACK * dev->nb_event_queues);
535         else
536                 xaq_cnt += (dev->iue / dev->xae_waes) +
537                         (OTX2_SSO_XAQ_SLACK * dev->nb_event_queues);
538
539         otx2_sso_dbg("Configuring %d xaq buffers", xaq_cnt);
540         /* Setup XAQ based on number of nb queues. */
541         snprintf(pool_name, 30, "otx2_xaq_buf_pool_%d", reconfig_cnt);
542         dev->xaq_pool = (void *)rte_mempool_create_empty(pool_name,
543                         xaq_cnt, dev->xaq_buf_size, 0, 0,
544                         rte_socket_id(), 0);
545
546         if (dev->xaq_pool == NULL) {
547                 otx2_err("Unable to create empty mempool.");
548                 rte_memzone_free(mz);
549                 return -ENOMEM;
550         }
551
552         rc = rte_mempool_set_ops_byname(dev->xaq_pool,
553                                         rte_mbuf_platform_mempool_ops(), aura);
554         if (rc != 0) {
555                 otx2_err("Unable to set xaqpool ops.");
556                 goto alloc_fail;
557         }
558
559         rc = rte_mempool_populate_default(dev->xaq_pool);
560         if (rc < 0) {
561                 otx2_err("Unable to set populate xaqpool.");
562                 goto alloc_fail;
563         }
564         reconfig_cnt++;
565         /* When SW does addwork (enqueue) check if there is space in XAQ by
566          * comparing fc_addr above against the xaq_lmt calculated below.
567          * There should be a minimum headroom (OTX2_SSO_XAQ_SLACK / 2) for SSO
568          * to request XAQ to cache them even before enqueue is called.
569          */
570         dev->xaq_lmt = xaq_cnt - (OTX2_SSO_XAQ_SLACK / 2 *
571                                   dev->nb_event_queues);
572         dev->nb_xaq_cfg = xaq_cnt;
573
574         return 0;
575 alloc_fail:
576         rte_mempool_free(dev->xaq_pool);
577         rte_memzone_free(mz);
578         return rc;
579 }
580
581 static int
582 sso_ggrp_alloc_xaq(struct otx2_sso_evdev *dev)
583 {
584         struct otx2_mbox *mbox = dev->mbox;
585         struct sso_hw_setconfig *req;
586
587         otx2_sso_dbg("Configuring XAQ for GGRPs");
588         req = otx2_mbox_alloc_msg_sso_hw_setconfig(mbox);
589         req->npa_pf_func = otx2_npa_pf_func_get();
590         req->npa_aura_id = npa_lf_aura_handle_to_aura(dev->xaq_pool->pool_id);
591         req->hwgrps = dev->nb_event_queues;
592
593         return otx2_mbox_process(mbox);
594 }
595
596 static void
597 sso_lf_teardown(struct otx2_sso_evdev *dev,
598                 enum otx2_sso_lf_type lf_type)
599 {
600         uint8_t nb_lf;
601
602         switch (lf_type) {
603         case SSO_LF_GGRP:
604                 nb_lf = dev->nb_event_queues;
605                 break;
606         case SSO_LF_GWS:
607                 nb_lf = dev->nb_event_ports;
608                 nb_lf *= dev->dual_ws ? 2 : 1;
609                 break;
610         default:
611                 return;
612         }
613
614         sso_lf_cfg(dev, dev->mbox, lf_type, nb_lf, false);
615         sso_hw_lf_cfg(dev->mbox, lf_type, nb_lf, false);
616 }
617
618 static int
619 otx2_sso_configure(const struct rte_eventdev *event_dev)
620 {
621         struct rte_event_dev_config *conf = &event_dev->data->dev_conf;
622         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
623         uint32_t deq_tmo_ns;
624         int rc;
625
626         sso_func_trace();
627         deq_tmo_ns = conf->dequeue_timeout_ns;
628
629         if (deq_tmo_ns == 0)
630                 deq_tmo_ns = dev->min_dequeue_timeout_ns;
631
632         if (deq_tmo_ns < dev->min_dequeue_timeout_ns ||
633             deq_tmo_ns > dev->max_dequeue_timeout_ns) {
634                 otx2_err("Unsupported dequeue timeout requested");
635                 return -EINVAL;
636         }
637
638         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
639                 dev->is_timeout_deq = 1;
640
641         dev->deq_tmo_ns = deq_tmo_ns;
642
643         if (conf->nb_event_ports > dev->max_event_ports ||
644             conf->nb_event_queues > dev->max_event_queues) {
645                 otx2_err("Unsupported event queues/ports requested");
646                 return -EINVAL;
647         }
648
649         if (conf->nb_event_port_dequeue_depth > 1) {
650                 otx2_err("Unsupported event port deq depth requested");
651                 return -EINVAL;
652         }
653
654         if (conf->nb_event_port_enqueue_depth > 1) {
655                 otx2_err("Unsupported event port enq depth requested");
656                 return -EINVAL;
657         }
658
659         if (dev->configured)
660                 sso_unregister_irqs(event_dev);
661
662         if (dev->nb_event_queues) {
663                 /* Finit any previous queues. */
664                 sso_lf_teardown(dev, SSO_LF_GGRP);
665         }
666         if (dev->nb_event_ports) {
667                 /* Finit any previous ports. */
668                 sso_lf_teardown(dev, SSO_LF_GWS);
669         }
670
671         dev->nb_event_queues = conf->nb_event_queues;
672         dev->nb_event_ports = conf->nb_event_ports;
673
674         if (dev->dual_ws)
675                 rc = sso_configure_dual_ports(event_dev);
676         else
677                 rc = sso_configure_ports(event_dev);
678
679         if (rc < 0) {
680                 otx2_err("Failed to configure event ports");
681                 return -ENODEV;
682         }
683
684         if (sso_configure_queues(event_dev) < 0) {
685                 otx2_err("Failed to configure event queues");
686                 rc = -ENODEV;
687                 goto teardown_hws;
688         }
689
690         if (sso_xaq_allocate(dev) < 0) {
691                 rc = -ENOMEM;
692                 goto teardown_hwggrp;
693         }
694
695         /* Clear any prior port-queue mapping. */
696         sso_clr_links(event_dev);
697         rc = sso_ggrp_alloc_xaq(dev);
698         if (rc < 0) {
699                 otx2_err("Failed to alloc xaq to ggrp %d", rc);
700                 goto teardown_hwggrp;
701         }
702
703         rc = sso_get_msix_offsets(event_dev);
704         if (rc < 0) {
705                 otx2_err("Failed to get msix offsets %d", rc);
706                 goto teardown_hwggrp;
707         }
708
709         rc = sso_register_irqs(event_dev);
710         if (rc < 0) {
711                 otx2_err("Failed to register irq %d", rc);
712                 goto teardown_hwggrp;
713         }
714
715         dev->configured = 1;
716         rte_mb();
717
718         return 0;
719 teardown_hwggrp:
720         sso_lf_teardown(dev, SSO_LF_GGRP);
721 teardown_hws:
722         sso_lf_teardown(dev, SSO_LF_GWS);
723         dev->nb_event_queues = 0;
724         dev->nb_event_ports = 0;
725         dev->configured = 0;
726         return rc;
727 }
728
729 static void
730 otx2_sso_queue_def_conf(struct rte_eventdev *event_dev, uint8_t queue_id,
731                         struct rte_event_queue_conf *queue_conf)
732 {
733         RTE_SET_USED(event_dev);
734         RTE_SET_USED(queue_id);
735
736         queue_conf->nb_atomic_flows = (1ULL << 20);
737         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
738         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
739         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
740 }
741
742 static int
743 otx2_sso_queue_setup(struct rte_eventdev *event_dev, uint8_t queue_id,
744                      const struct rte_event_queue_conf *queue_conf)
745 {
746         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
747         struct otx2_mbox *mbox = dev->mbox;
748         struct sso_grp_priority *req;
749         int rc;
750
751         sso_func_trace("Queue=%d prio=%d", queue_id, queue_conf->priority);
752
753         req = otx2_mbox_alloc_msg_sso_grp_set_priority(dev->mbox);
754         req->grp = queue_id;
755         req->weight = 0xFF;
756         req->affinity = 0xFF;
757         /* Normalize <0-255> to <0-7> */
758         req->priority = queue_conf->priority / 32;
759
760         rc = otx2_mbox_process(mbox);
761         if (rc < 0) {
762                 otx2_err("Failed to set priority queue=%d", queue_id);
763                 return rc;
764         }
765
766         return 0;
767 }
768
769 static void
770 otx2_sso_port_def_conf(struct rte_eventdev *event_dev, uint8_t port_id,
771                        struct rte_event_port_conf *port_conf)
772 {
773         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
774
775         RTE_SET_USED(port_id);
776         port_conf->new_event_threshold = dev->max_num_events;
777         port_conf->dequeue_depth = 1;
778         port_conf->enqueue_depth = 1;
779 }
780
781 static int
782 otx2_sso_port_setup(struct rte_eventdev *event_dev, uint8_t port_id,
783                     const struct rte_event_port_conf *port_conf)
784 {
785         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
786         uintptr_t grps_base[OTX2_SSO_MAX_VHGRP] = {0};
787         uint64_t val;
788         uint16_t q;
789
790         sso_func_trace("Port=%d", port_id);
791         RTE_SET_USED(port_conf);
792
793         if (event_dev->data->ports[port_id] == NULL) {
794                 otx2_err("Invalid port Id %d", port_id);
795                 return -EINVAL;
796         }
797
798         for (q = 0; q < dev->nb_event_queues; q++) {
799                 grps_base[q] = dev->bar2 + (RVU_BLOCK_ADDR_SSO << 20 | q << 12);
800                 if (grps_base[q] == 0) {
801                         otx2_err("Failed to get grp[%d] base addr", q);
802                         return -EINVAL;
803                 }
804         }
805
806         /* Set get_work timeout for HWS */
807         val = NSEC2USEC(dev->deq_tmo_ns) - 1;
808
809         if (dev->dual_ws) {
810                 struct otx2_ssogws_dual *ws = event_dev->data->ports[port_id];
811
812                 rte_memcpy(ws->grps_base, grps_base,
813                            sizeof(uintptr_t) * OTX2_SSO_MAX_VHGRP);
814                 ws->fc_mem = dev->fc_mem;
815                 ws->xaq_lmt = dev->xaq_lmt;
816                 otx2_write64(val, OTX2_SSOW_GET_BASE_ADDR(
817                              ws->ws_state[0].getwrk_op) + SSOW_LF_GWS_NW_TIM);
818                 otx2_write64(val, OTX2_SSOW_GET_BASE_ADDR(
819                              ws->ws_state[1].getwrk_op) + SSOW_LF_GWS_NW_TIM);
820         } else {
821                 struct otx2_ssogws *ws = event_dev->data->ports[port_id];
822                 uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
823
824                 rte_memcpy(ws->grps_base, grps_base,
825                            sizeof(uintptr_t) * OTX2_SSO_MAX_VHGRP);
826                 ws->fc_mem = dev->fc_mem;
827                 ws->xaq_lmt = dev->xaq_lmt;
828                 otx2_write64(val, base + SSOW_LF_GWS_NW_TIM);
829         }
830
831         otx2_sso_dbg("Port=%d ws=%p", port_id, event_dev->data->ports[port_id]);
832
833         return 0;
834 }
835
836 static int
837 otx2_sso_timeout_ticks(struct rte_eventdev *event_dev, uint64_t ns,
838                        uint64_t *tmo_ticks)
839 {
840         RTE_SET_USED(event_dev);
841         *tmo_ticks = NSEC2TICK(ns, rte_get_timer_hz());
842
843         return 0;
844 }
845
846 static void
847 ssogws_dump(struct otx2_ssogws *ws, FILE *f)
848 {
849         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
850
851         fprintf(f, "SSOW_LF_GWS Base addr   0x%" PRIx64 "\n", (uint64_t)base);
852         fprintf(f, "SSOW_LF_GWS_LINKS       0x%" PRIx64 "\n",
853                 otx2_read64(base + SSOW_LF_GWS_LINKS));
854         fprintf(f, "SSOW_LF_GWS_PENDWQP     0x%" PRIx64 "\n",
855                 otx2_read64(base + SSOW_LF_GWS_PENDWQP));
856         fprintf(f, "SSOW_LF_GWS_PENDSTATE   0x%" PRIx64 "\n",
857                 otx2_read64(base + SSOW_LF_GWS_PENDSTATE));
858         fprintf(f, "SSOW_LF_GWS_NW_TIM      0x%" PRIx64 "\n",
859                 otx2_read64(base + SSOW_LF_GWS_NW_TIM));
860         fprintf(f, "SSOW_LF_GWS_TAG         0x%" PRIx64 "\n",
861                 otx2_read64(base + SSOW_LF_GWS_TAG));
862         fprintf(f, "SSOW_LF_GWS_WQP         0x%" PRIx64 "\n",
863                 otx2_read64(base + SSOW_LF_GWS_TAG));
864         fprintf(f, "SSOW_LF_GWS_SWTP        0x%" PRIx64 "\n",
865                 otx2_read64(base + SSOW_LF_GWS_SWTP));
866         fprintf(f, "SSOW_LF_GWS_PENDTAG     0x%" PRIx64 "\n",
867                 otx2_read64(base + SSOW_LF_GWS_PENDTAG));
868 }
869
870 static void
871 ssoggrp_dump(uintptr_t base, FILE *f)
872 {
873         fprintf(f, "SSO_LF_GGRP Base addr   0x%" PRIx64 "\n", (uint64_t)base);
874         fprintf(f, "SSO_LF_GGRP_QCTL        0x%" PRIx64 "\n",
875                 otx2_read64(base + SSO_LF_GGRP_QCTL));
876         fprintf(f, "SSO_LF_GGRP_XAQ_CNT     0x%" PRIx64 "\n",
877                 otx2_read64(base + SSO_LF_GGRP_XAQ_CNT));
878         fprintf(f, "SSO_LF_GGRP_INT_THR     0x%" PRIx64 "\n",
879                 otx2_read64(base + SSO_LF_GGRP_INT_THR));
880         fprintf(f, "SSO_LF_GGRP_INT_CNT     0x%" PRIX64 "\n",
881                 otx2_read64(base + SSO_LF_GGRP_INT_CNT));
882         fprintf(f, "SSO_LF_GGRP_AQ_CNT      0x%" PRIX64 "\n",
883                 otx2_read64(base + SSO_LF_GGRP_AQ_CNT));
884         fprintf(f, "SSO_LF_GGRP_AQ_THR      0x%" PRIX64 "\n",
885                 otx2_read64(base + SSO_LF_GGRP_AQ_THR));
886         fprintf(f, "SSO_LF_GGRP_MISC_CNT    0x%" PRIx64 "\n",
887                 otx2_read64(base + SSO_LF_GGRP_MISC_CNT));
888 }
889
890 static void
891 otx2_sso_dump(struct rte_eventdev *event_dev, FILE *f)
892 {
893         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
894         uint8_t queue;
895         uint8_t port;
896
897         fprintf(f, "[%s] SSO running in [%s] mode\n", __func__, dev->dual_ws ?
898                 "dual_ws" : "single_ws");
899         /* Dump SSOW registers */
900         for (port = 0; port < dev->nb_event_ports; port++) {
901                 if (dev->dual_ws) {
902                         struct otx2_ssogws_dual *ws =
903                                 event_dev->data->ports[port];
904
905                         fprintf(f, "[%s] SSO dual workslot[%d] vws[%d] dump\n",
906                                 __func__, port, 0);
907                         ssogws_dump((struct otx2_ssogws *)&ws->ws_state[0], f);
908                         fprintf(f, "[%s]SSO dual workslot[%d] vws[%d] dump\n",
909                                 __func__, port, 1);
910                         ssogws_dump((struct otx2_ssogws *)&ws->ws_state[1], f);
911                 } else {
912                         fprintf(f, "[%s]SSO single workslot[%d] dump\n",
913                                 __func__, port);
914                         ssogws_dump(event_dev->data->ports[port], f);
915                 }
916         }
917
918         /* Dump SSO registers */
919         for (queue = 0; queue < dev->nb_event_queues; queue++) {
920                 fprintf(f, "[%s]SSO group[%d] dump\n", __func__, queue);
921                 if (dev->dual_ws) {
922                         struct otx2_ssogws_dual *ws = event_dev->data->ports[0];
923                         ssoggrp_dump(ws->grps_base[queue], f);
924                 } else {
925                         struct otx2_ssogws *ws = event_dev->data->ports[0];
926                         ssoggrp_dump(ws->grps_base[queue], f);
927                 }
928         }
929 }
930
931 static void
932 otx2_handle_event(void *arg, struct rte_event event)
933 {
934         struct rte_eventdev *event_dev = arg;
935
936         if (event_dev->dev_ops->dev_stop_flush != NULL)
937                 event_dev->dev_ops->dev_stop_flush(event_dev->data->dev_id,
938                                 event, event_dev->data->dev_stop_flush_arg);
939 }
940
941 static void
942 sso_qos_cfg(struct rte_eventdev *event_dev)
943 {
944         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
945         struct sso_grp_qos_cfg *req;
946         uint16_t i;
947
948         for (i = 0; i < dev->qos_queue_cnt; i++) {
949                 uint8_t xaq_prcnt = dev->qos_parse_data[i].xaq_prcnt;
950                 uint8_t iaq_prcnt = dev->qos_parse_data[i].iaq_prcnt;
951                 uint8_t taq_prcnt = dev->qos_parse_data[i].taq_prcnt;
952
953                 if (dev->qos_parse_data[i].queue >= dev->nb_event_queues)
954                         continue;
955
956                 req = otx2_mbox_alloc_msg_sso_grp_qos_config(dev->mbox);
957                 req->xaq_limit = (dev->nb_xaq_cfg *
958                                   (xaq_prcnt ? xaq_prcnt : 100)) / 100;
959                 req->taq_thr = (SSO_HWGRP_IAQ_MAX_THR_MASK *
960                                 (iaq_prcnt ? iaq_prcnt : 100)) / 100;
961                 req->iaq_thr = (SSO_HWGRP_TAQ_MAX_THR_MASK *
962                                 (taq_prcnt ? taq_prcnt : 100)) / 100;
963         }
964
965         if (dev->qos_queue_cnt)
966                 otx2_mbox_process(dev->mbox);
967 }
968
969 static void
970 sso_cleanup(struct rte_eventdev *event_dev, uint8_t enable)
971 {
972         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
973         uint16_t i;
974
975         for (i = 0; i < dev->nb_event_ports; i++) {
976                 if (dev->dual_ws) {
977                         struct otx2_ssogws_dual *ws;
978
979                         ws = event_dev->data->ports[i];
980                         ssogws_reset((struct otx2_ssogws *)&ws->ws_state[0]);
981                         ssogws_reset((struct otx2_ssogws *)&ws->ws_state[1]);
982                         ws->swtag_req = 0;
983                         ws->vws = 0;
984                         ws->ws_state[0].cur_grp = 0;
985                         ws->ws_state[0].cur_tt = SSO_SYNC_EMPTY;
986                         ws->ws_state[1].cur_grp = 0;
987                         ws->ws_state[1].cur_tt = SSO_SYNC_EMPTY;
988                 } else {
989                         struct otx2_ssogws *ws;
990
991                         ws = event_dev->data->ports[i];
992                         ssogws_reset(ws);
993                         ws->swtag_req = 0;
994                         ws->cur_grp = 0;
995                         ws->cur_tt = SSO_SYNC_EMPTY;
996                 }
997         }
998
999         rte_mb();
1000         if (dev->dual_ws) {
1001                 struct otx2_ssogws_dual *ws = event_dev->data->ports[0];
1002                 struct otx2_ssogws temp_ws;
1003
1004                 memcpy(&temp_ws, &ws->ws_state[0],
1005                        sizeof(struct otx2_ssogws_state));
1006                 for (i = 0; i < dev->nb_event_queues; i++) {
1007                         /* Consume all the events through HWS0 */
1008                         ssogws_flush_events(&temp_ws, i, ws->grps_base[i],
1009                                             otx2_handle_event, event_dev);
1010                         /* Enable/Disable SSO GGRP */
1011                         otx2_write64(enable, ws->grps_base[i] +
1012                                      SSO_LF_GGRP_QCTL);
1013                 }
1014                 ws->ws_state[0].cur_grp = 0;
1015                 ws->ws_state[0].cur_tt = SSO_SYNC_EMPTY;
1016         } else {
1017                 struct otx2_ssogws *ws = event_dev->data->ports[0];
1018
1019                 for (i = 0; i < dev->nb_event_queues; i++) {
1020                         /* Consume all the events through HWS0 */
1021                         ssogws_flush_events(ws, i, ws->grps_base[i],
1022                                             otx2_handle_event, event_dev);
1023                         /* Enable/Disable SSO GGRP */
1024                         otx2_write64(enable, ws->grps_base[i] +
1025                                      SSO_LF_GGRP_QCTL);
1026                 }
1027                 ws->cur_grp = 0;
1028                 ws->cur_tt = SSO_SYNC_EMPTY;
1029         }
1030
1031         /* reset SSO GWS cache */
1032         otx2_mbox_alloc_msg_sso_ws_cache_inv(dev->mbox);
1033         otx2_mbox_process(dev->mbox);
1034 }
1035
1036 int
1037 sso_xae_reconfigure(struct rte_eventdev *event_dev)
1038 {
1039         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
1040         struct rte_mempool *prev_xaq_pool;
1041         int rc = 0;
1042
1043         if (event_dev->data->dev_started)
1044                 sso_cleanup(event_dev, 0);
1045
1046         prev_xaq_pool = dev->xaq_pool;
1047         dev->xaq_pool = NULL;
1048         sso_xaq_allocate(dev);
1049         rc = sso_ggrp_alloc_xaq(dev);
1050         if (rc < 0) {
1051                 otx2_err("Failed to alloc xaq to ggrp %d", rc);
1052                 rte_mempool_free(prev_xaq_pool);
1053                 return rc;
1054         }
1055
1056         rte_mempool_free(prev_xaq_pool);
1057         rte_mb();
1058         if (event_dev->data->dev_started)
1059                 sso_cleanup(event_dev, 1);
1060
1061         return 0;
1062 }
1063
1064 static int
1065 otx2_sso_start(struct rte_eventdev *event_dev)
1066 {
1067         sso_func_trace();
1068         sso_qos_cfg(event_dev);
1069         sso_cleanup(event_dev, 1);
1070         sso_fastpath_fns_set(event_dev);
1071
1072         return 0;
1073 }
1074
1075 static void
1076 otx2_sso_stop(struct rte_eventdev *event_dev)
1077 {
1078         sso_func_trace();
1079         sso_cleanup(event_dev, 0);
1080         rte_mb();
1081 }
1082
1083 static int
1084 otx2_sso_close(struct rte_eventdev *event_dev)
1085 {
1086         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
1087         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
1088         uint16_t i;
1089
1090         if (!dev->configured)
1091                 return 0;
1092
1093         sso_unregister_irqs(event_dev);
1094
1095         for (i = 0; i < dev->nb_event_queues; i++)
1096                 all_queues[i] = i;
1097
1098         for (i = 0; i < dev->nb_event_ports; i++)
1099                 otx2_sso_port_unlink(event_dev, event_dev->data->ports[i],
1100                                      all_queues, dev->nb_event_queues);
1101
1102         sso_lf_teardown(dev, SSO_LF_GGRP);
1103         sso_lf_teardown(dev, SSO_LF_GWS);
1104         dev->nb_event_ports = 0;
1105         dev->nb_event_queues = 0;
1106         rte_mempool_free(dev->xaq_pool);
1107         rte_memzone_free(rte_memzone_lookup(OTX2_SSO_FC_NAME));
1108
1109         return 0;
1110 }
1111
1112 /* Initialize and register event driver with DPDK Application */
1113 static struct rte_eventdev_ops otx2_sso_ops = {
1114         .dev_infos_get    = otx2_sso_info_get,
1115         .dev_configure    = otx2_sso_configure,
1116         .queue_def_conf   = otx2_sso_queue_def_conf,
1117         .queue_setup      = otx2_sso_queue_setup,
1118         .queue_release    = otx2_sso_queue_release,
1119         .port_def_conf    = otx2_sso_port_def_conf,
1120         .port_setup       = otx2_sso_port_setup,
1121         .port_release     = otx2_sso_port_release,
1122         .port_link        = otx2_sso_port_link,
1123         .port_unlink      = otx2_sso_port_unlink,
1124         .timeout_ticks    = otx2_sso_timeout_ticks,
1125
1126         .eth_rx_adapter_caps_get  = otx2_sso_rx_adapter_caps_get,
1127         .eth_rx_adapter_queue_add = otx2_sso_rx_adapter_queue_add,
1128         .eth_rx_adapter_queue_del = otx2_sso_rx_adapter_queue_del,
1129
1130         .timer_adapter_caps_get = otx2_tim_caps_get,
1131
1132         .xstats_get       = otx2_sso_xstats_get,
1133         .xstats_reset     = otx2_sso_xstats_reset,
1134         .xstats_get_names = otx2_sso_xstats_get_names,
1135
1136         .dump             = otx2_sso_dump,
1137         .dev_start        = otx2_sso_start,
1138         .dev_stop         = otx2_sso_stop,
1139         .dev_close        = otx2_sso_close,
1140         .dev_selftest     = otx2_sso_selftest,
1141 };
1142
1143 #define OTX2_SSO_XAE_CNT        "xae_cnt"
1144 #define OTX2_SSO_SINGLE_WS      "single_ws"
1145 #define OTX2_SSO_GGRP_QOS       "qos"
1146 #define OTX2_SSO_SELFTEST       "selftest"
1147
1148 static void
1149 parse_queue_param(char *value, void *opaque)
1150 {
1151         struct otx2_sso_qos queue_qos = {0};
1152         uint8_t *val = (uint8_t *)&queue_qos;
1153         struct otx2_sso_evdev *dev = opaque;
1154         char *tok = strtok(value, "-");
1155
1156         if (!strlen(value))
1157                 return;
1158
1159         while (tok != NULL) {
1160                 *val = atoi(tok);
1161                 tok = strtok(NULL, "-");
1162                 val++;
1163         }
1164
1165         if (val != (&queue_qos.iaq_prcnt + 1)) {
1166                 otx2_err("Invalid QoS parameter expected [Qx-XAQ-TAQ-IAQ]");
1167                 return;
1168         }
1169
1170         dev->qos_queue_cnt++;
1171         dev->qos_parse_data = rte_realloc(dev->qos_parse_data,
1172                                           sizeof(struct otx2_sso_qos) *
1173                                           dev->qos_queue_cnt, 0);
1174         dev->qos_parse_data[dev->qos_queue_cnt - 1] = queue_qos;
1175 }
1176
1177 static void
1178 parse_qos_list(const char *value, void *opaque)
1179 {
1180         char *s = strdup(value);
1181         char *start = NULL;
1182         char *end = NULL;
1183         char *f = s;
1184
1185         while (*s) {
1186                 if (*s == '[')
1187                         start = s;
1188                 else if (*s == ']')
1189                         end = s;
1190
1191                 if (start < end && *start) {
1192                         *end = 0;
1193                         parse_queue_param(start + 1, opaque);
1194                         s = end;
1195                         start = end;
1196                 }
1197                 s++;
1198         }
1199
1200         free(f);
1201 }
1202
1203 static int
1204 parse_sso_kvargs_dict(const char *key, const char *value, void *opaque)
1205 {
1206         RTE_SET_USED(key);
1207
1208         /* Dict format [Qx-XAQ-TAQ-IAQ][Qz-XAQ-TAQ-IAQ] use '-' cause ','
1209          * isn't allowed. Everything is expressed in percentages, 0 represents
1210          * default.
1211          */
1212         parse_qos_list(value, opaque);
1213
1214         return 0;
1215 }
1216
1217 static void
1218 sso_parse_devargs(struct otx2_sso_evdev *dev, struct rte_devargs *devargs)
1219 {
1220         struct rte_kvargs *kvlist;
1221         uint8_t single_ws = 0;
1222
1223         if (devargs == NULL)
1224                 return;
1225         kvlist = rte_kvargs_parse(devargs->args, NULL);
1226         if (kvlist == NULL)
1227                 return;
1228
1229         rte_kvargs_process(kvlist, OTX2_SSO_SELFTEST, &parse_kvargs_flag,
1230                            &dev->selftest);
1231         rte_kvargs_process(kvlist, OTX2_SSO_XAE_CNT, &parse_kvargs_value,
1232                            &dev->xae_cnt);
1233         rte_kvargs_process(kvlist, OTX2_SSO_SINGLE_WS, &parse_kvargs_flag,
1234                            &single_ws);
1235         rte_kvargs_process(kvlist, OTX2_SSO_GGRP_QOS, &parse_sso_kvargs_dict,
1236                            dev);
1237
1238         dev->dual_ws = !single_ws;
1239         rte_kvargs_free(kvlist);
1240 }
1241
1242 static int
1243 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
1244 {
1245         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
1246                                        sizeof(struct otx2_sso_evdev),
1247                                        otx2_sso_init);
1248 }
1249
1250 static int
1251 otx2_sso_remove(struct rte_pci_device *pci_dev)
1252 {
1253         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
1254 }
1255
1256 static const struct rte_pci_id pci_sso_map[] = {
1257         {
1258                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
1259                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
1260         },
1261         {
1262                 .vendor_id = 0,
1263         },
1264 };
1265
1266 static struct rte_pci_driver pci_sso = {
1267         .id_table = pci_sso_map,
1268         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
1269         .probe = otx2_sso_probe,
1270         .remove = otx2_sso_remove,
1271 };
1272
1273 int
1274 otx2_sso_init(struct rte_eventdev *event_dev)
1275 {
1276         struct free_rsrcs_rsp *rsrc_cnt;
1277         struct rte_pci_device *pci_dev;
1278         struct otx2_sso_evdev *dev;
1279         int rc;
1280
1281         event_dev->dev_ops = &otx2_sso_ops;
1282         /* For secondary processes, the primary has done all the work */
1283         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1284                 sso_fastpath_fns_set(event_dev);
1285                 return 0;
1286         }
1287
1288         dev = sso_pmd_priv(event_dev);
1289
1290         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
1291
1292         /* Initialize the base otx2_dev object */
1293         rc = otx2_dev_init(pci_dev, dev);
1294         if (rc < 0) {
1295                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
1296                 goto error;
1297         }
1298
1299         /* Get SSO and SSOW MSIX rsrc cnt */
1300         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
1301         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
1302         if (rc < 0) {
1303                 otx2_err("Unable to get free rsrc count");
1304                 goto otx2_dev_uninit;
1305         }
1306         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
1307                      rsrc_cnt->ssow, rsrc_cnt->npa);
1308
1309         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
1310         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
1311         /* Grab the NPA LF if required */
1312         rc = otx2_npa_lf_init(pci_dev, dev);
1313         if (rc < 0) {
1314                 otx2_err("Unable to init NPA lf. It might not be provisioned");
1315                 goto otx2_dev_uninit;
1316         }
1317
1318         dev->drv_inited = true;
1319         dev->is_timeout_deq = 0;
1320         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
1321         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
1322         dev->max_num_events = -1;
1323         dev->nb_event_queues = 0;
1324         dev->nb_event_ports = 0;
1325
1326         if (!dev->max_event_ports || !dev->max_event_queues) {
1327                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
1328                          dev->max_event_queues, dev->max_event_ports);
1329                 rc = -ENODEV;
1330                 goto otx2_npa_lf_uninit;
1331         }
1332
1333         dev->dual_ws = 1;
1334         sso_parse_devargs(dev, pci_dev->device.devargs);
1335         if (dev->dual_ws) {
1336                 otx2_sso_dbg("Using dual workslot mode");
1337                 dev->max_event_ports = dev->max_event_ports / 2;
1338         } else {
1339                 otx2_sso_dbg("Using single workslot mode");
1340         }
1341
1342         otx2_sso_pf_func_set(dev->pf_func);
1343         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
1344                      event_dev->data->name, dev->max_event_queues,
1345                      dev->max_event_ports);
1346         if (dev->selftest) {
1347                 event_dev->dev->driver = &pci_sso.driver;
1348                 event_dev->dev_ops->dev_selftest();
1349         }
1350
1351         otx2_tim_init(pci_dev, (struct otx2_dev *)dev);
1352
1353         return 0;
1354
1355 otx2_npa_lf_uninit:
1356         otx2_npa_lf_fini();
1357 otx2_dev_uninit:
1358         otx2_dev_fini(pci_dev, dev);
1359 error:
1360         return rc;
1361 }
1362
1363 int
1364 otx2_sso_fini(struct rte_eventdev *event_dev)
1365 {
1366         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
1367         struct rte_pci_device *pci_dev;
1368
1369         /* For secondary processes, nothing to be done */
1370         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1371                 return 0;
1372
1373         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
1374
1375         if (!dev->drv_inited)
1376                 goto dev_fini;
1377
1378         dev->drv_inited = false;
1379         otx2_npa_lf_fini();
1380
1381 dev_fini:
1382         if (otx2_npa_lf_active(dev)) {
1383                 otx2_info("Common resource in use by other devices");
1384                 return -EAGAIN;
1385         }
1386
1387         otx2_tim_fini();
1388         otx2_dev_fini(pci_dev, dev);
1389
1390         return 0;
1391 }
1392
1393 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
1394 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
1395 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");
1396 RTE_PMD_REGISTER_PARAM_STRING(event_octeontx2, OTX2_SSO_XAE_CNT "=<int>"
1397                               OTX2_SSO_SINGLE_WS "=1"
1398                               OTX2_SSO_GGRP_QOS "=<string>"
1399                               OTX2_SSO_SELFTEST "=1");