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