event/octeontx2: support linking queues to ports
[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.h"
16
17 static void
18 otx2_sso_info_get(struct rte_eventdev *event_dev,
19                   struct rte_event_dev_info *dev_info)
20 {
21         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
22
23         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX2_PMD);
24         dev_info->min_dequeue_timeout_ns = dev->min_dequeue_timeout_ns;
25         dev_info->max_dequeue_timeout_ns = dev->max_dequeue_timeout_ns;
26         dev_info->max_event_queues = dev->max_event_queues;
27         dev_info->max_event_queue_flows = (1ULL << 20);
28         dev_info->max_event_queue_priority_levels = 8;
29         dev_info->max_event_priority_levels = 1;
30         dev_info->max_event_ports = dev->max_event_ports;
31         dev_info->max_event_port_dequeue_depth = 1;
32         dev_info->max_event_port_enqueue_depth = 1;
33         dev_info->max_num_events =  dev->max_num_events;
34         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
35                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
36                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES |
37                                         RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
38                                         RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
39                                         RTE_EVENT_DEV_CAP_NONSEQ_MODE;
40 }
41
42 static void
43 sso_port_link_modify(struct otx2_ssogws *ws, uint8_t queue, uint8_t enable)
44 {
45         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
46         uint64_t val;
47
48         val = queue;
49         val |= 0ULL << 12; /* SET 0 */
50         val |= 0x8000800080000000; /* Dont modify rest of the masks */
51         val |= (uint64_t)enable << 14;   /* Enable/Disable Membership. */
52
53         otx2_write64(val, base + SSOW_LF_GWS_GRPMSK_CHG);
54 }
55
56 static int
57 otx2_sso_port_link(struct rte_eventdev *event_dev, void *port,
58                    const uint8_t queues[], const uint8_t priorities[],
59                    uint16_t nb_links)
60 {
61         uint8_t port_id = 0;
62         uint16_t link;
63
64         RTE_SET_USED(event_dev);
65         RTE_SET_USED(priorities);
66         for (link = 0; link < nb_links; link++) {
67                 struct otx2_ssogws *ws = port;
68
69                 port_id = ws->port;
70                 sso_port_link_modify(ws, queues[link], true);
71         }
72         sso_func_trace("Port=%d nb_links=%d", port_id, nb_links);
73
74         return (int)nb_links;
75 }
76
77 static int
78 otx2_sso_port_unlink(struct rte_eventdev *event_dev, void *port,
79                      uint8_t queues[], uint16_t nb_unlinks)
80 {
81         uint8_t port_id = 0;
82         uint16_t unlink;
83
84         RTE_SET_USED(event_dev);
85         for (unlink = 0; unlink < nb_unlinks; unlink++) {
86                 struct otx2_ssogws *ws = port;
87
88                 port_id = ws->port;
89                 sso_port_link_modify(ws, queues[unlink], false);
90         }
91         sso_func_trace("Port=%d nb_unlinks=%d", port_id, nb_unlinks);
92
93         return (int)nb_unlinks;
94 }
95
96 static int
97 sso_hw_lf_cfg(struct otx2_mbox *mbox, enum otx2_sso_lf_type type,
98               uint16_t nb_lf, uint8_t attach)
99 {
100         if (attach) {
101                 struct rsrc_attach_req *req;
102
103                 req = otx2_mbox_alloc_msg_attach_resources(mbox);
104                 switch (type) {
105                 case SSO_LF_GGRP:
106                         req->sso = nb_lf;
107                         break;
108                 case SSO_LF_GWS:
109                         req->ssow = nb_lf;
110                         break;
111                 default:
112                         return -EINVAL;
113                 }
114                 req->modify = true;
115                 if (otx2_mbox_process(mbox) < 0)
116                         return -EIO;
117         } else {
118                 struct rsrc_detach_req *req;
119
120                 req = otx2_mbox_alloc_msg_detach_resources(mbox);
121                 switch (type) {
122                 case SSO_LF_GGRP:
123                         req->sso = true;
124                         break;
125                 case SSO_LF_GWS:
126                         req->ssow = true;
127                         break;
128                 default:
129                         return -EINVAL;
130                 }
131                 req->partial = true;
132                 if (otx2_mbox_process(mbox) < 0)
133                         return -EIO;
134         }
135
136         return 0;
137 }
138
139 static int
140 sso_lf_cfg(struct otx2_sso_evdev *dev, struct otx2_mbox *mbox,
141            enum otx2_sso_lf_type type, uint16_t nb_lf, uint8_t alloc)
142 {
143         void *rsp;
144         int rc;
145
146         if (alloc) {
147                 switch (type) {
148                 case SSO_LF_GGRP:
149                         {
150                         struct sso_lf_alloc_req *req_ggrp;
151                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_alloc(mbox);
152                         req_ggrp->hwgrps = nb_lf;
153                         }
154                         break;
155                 case SSO_LF_GWS:
156                         {
157                         struct ssow_lf_alloc_req *req_hws;
158                         req_hws = otx2_mbox_alloc_msg_ssow_lf_alloc(mbox);
159                         req_hws->hws = nb_lf;
160                         }
161                         break;
162                 default:
163                         return -EINVAL;
164                 }
165         } else {
166                 switch (type) {
167                 case SSO_LF_GGRP:
168                         {
169                         struct sso_lf_free_req *req_ggrp;
170                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_free(mbox);
171                         req_ggrp->hwgrps = nb_lf;
172                         }
173                         break;
174                 case SSO_LF_GWS:
175                         {
176                         struct ssow_lf_free_req *req_hws;
177                         req_hws = otx2_mbox_alloc_msg_ssow_lf_free(mbox);
178                         req_hws->hws = nb_lf;
179                         }
180                         break;
181                 default:
182                         return -EINVAL;
183                 }
184         }
185
186         rc = otx2_mbox_process_msg_tmo(mbox, (void **)&rsp, ~0);
187         if (rc < 0)
188                 return rc;
189
190         if (alloc && type == SSO_LF_GGRP) {
191                 struct sso_lf_alloc_rsp *rsp_ggrp = rsp;
192
193                 dev->xaq_buf_size = rsp_ggrp->xaq_buf_size;
194                 dev->xae_waes = rsp_ggrp->xaq_wq_entries;
195                 dev->iue = rsp_ggrp->in_unit_entries;
196         }
197
198         return 0;
199 }
200
201 static void
202 otx2_sso_port_release(void *port)
203 {
204         rte_free(port);
205 }
206
207 static void
208 otx2_sso_queue_release(struct rte_eventdev *event_dev, uint8_t queue_id)
209 {
210         RTE_SET_USED(event_dev);
211         RTE_SET_USED(queue_id);
212 }
213
214 static void
215 sso_clr_links(const struct rte_eventdev *event_dev)
216 {
217         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
218         int i, j;
219
220         for (i = 0; i < dev->nb_event_ports; i++) {
221                 struct otx2_ssogws *ws;
222
223                 ws = event_dev->data->ports[i];
224                 for (j = 0; j < dev->nb_event_queues; j++)
225                         sso_port_link_modify(ws, j, false);
226         }
227 }
228
229 static void
230 sso_set_port_ops(struct otx2_ssogws *ws, uintptr_t base)
231 {
232         ws->tag_op              = base + SSOW_LF_GWS_TAG;
233         ws->wqp_op              = base + SSOW_LF_GWS_WQP;
234         ws->getwrk_op           = base + SSOW_LF_GWS_OP_GET_WORK;
235         ws->swtp_op             = base + SSOW_LF_GWS_SWTP;
236         ws->swtag_norm_op       = base + SSOW_LF_GWS_OP_SWTAG_NORM;
237         ws->swtag_desched_op    = base + SSOW_LF_GWS_OP_SWTAG_DESCHED;
238 }
239
240 static int
241 sso_configure_ports(const struct rte_eventdev *event_dev)
242 {
243         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
244         struct otx2_mbox *mbox = dev->mbox;
245         uint8_t nb_lf;
246         int i, rc;
247
248         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
249
250         nb_lf = dev->nb_event_ports;
251         /* Ask AF to attach required LFs. */
252         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
253         if (rc < 0) {
254                 otx2_err("Failed to attach SSO GWS LF");
255                 return -ENODEV;
256         }
257
258         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
259                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
260                 otx2_err("Failed to init SSO GWS LF");
261                 return -ENODEV;
262         }
263
264         for (i = 0; i < nb_lf; i++) {
265                 struct otx2_ssogws *ws;
266                 uintptr_t base;
267
268                 /* Free memory prior to re-allocation if needed */
269                 if (event_dev->data->ports[i] != NULL) {
270                         ws = event_dev->data->ports[i];
271                         rte_free(ws);
272                         ws = NULL;
273                 }
274
275                 /* Allocate event port memory */
276                 ws = rte_zmalloc_socket("otx2_sso_ws",
277                                         sizeof(struct otx2_ssogws),
278                                         RTE_CACHE_LINE_SIZE,
279                                         event_dev->data->socket_id);
280                 if (ws == NULL) {
281                         otx2_err("Failed to alloc memory for port=%d", i);
282                         rc = -ENOMEM;
283                         break;
284                 }
285
286                 ws->port = i;
287                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | i << 12);
288                 sso_set_port_ops(ws, base);
289
290                 event_dev->data->ports[i] = ws;
291         }
292
293         if (rc < 0) {
294                 sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, false);
295                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
296         }
297
298         return rc;
299 }
300
301 static int
302 sso_configure_queues(const struct rte_eventdev *event_dev)
303 {
304         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
305         struct otx2_mbox *mbox = dev->mbox;
306         uint8_t nb_lf;
307         int rc;
308
309         otx2_sso_dbg("Configuring event queues %d", dev->nb_event_queues);
310
311         nb_lf = dev->nb_event_queues;
312         /* Ask AF to attach required LFs. */
313         rc = sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, true);
314         if (rc < 0) {
315                 otx2_err("Failed to attach SSO GGRP LF");
316                 return -ENODEV;
317         }
318
319         if (sso_lf_cfg(dev, mbox, SSO_LF_GGRP, nb_lf, true) < 0) {
320                 sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, false);
321                 otx2_err("Failed to init SSO GGRP LF");
322                 return -ENODEV;
323         }
324
325         return rc;
326 }
327
328 static int
329 sso_xaq_allocate(struct otx2_sso_evdev *dev)
330 {
331         const struct rte_memzone *mz;
332         struct npa_aura_s *aura;
333         static int reconfig_cnt;
334         char pool_name[RTE_MEMZONE_NAMESIZE];
335         uint32_t xaq_cnt;
336         int rc;
337
338         if (dev->xaq_pool)
339                 rte_mempool_free(dev->xaq_pool);
340
341         /*
342          * Allocate memory for Add work backpressure.
343          */
344         mz = rte_memzone_lookup(OTX2_SSO_FC_NAME);
345         if (mz == NULL)
346                 mz = rte_memzone_reserve_aligned(OTX2_SSO_FC_NAME,
347                                                  OTX2_ALIGN +
348                                                  sizeof(struct npa_aura_s),
349                                                  rte_socket_id(),
350                                                  RTE_MEMZONE_IOVA_CONTIG,
351                                                  OTX2_ALIGN);
352         if (mz == NULL) {
353                 otx2_err("Failed to allocate mem for fcmem");
354                 return -ENOMEM;
355         }
356
357         dev->fc_iova = mz->iova;
358         dev->fc_mem = mz->addr;
359
360         aura = (struct npa_aura_s *)((uintptr_t)dev->fc_mem + OTX2_ALIGN);
361         memset(aura, 0, sizeof(struct npa_aura_s));
362
363         aura->fc_ena = 1;
364         aura->fc_addr = dev->fc_iova;
365         aura->fc_hyst_bits = 0; /* Store count on all updates */
366
367         /* Taken from HRM 14.3.3(4) */
368         xaq_cnt = dev->nb_event_queues * OTX2_SSO_XAQ_CACHE_CNT;
369         if (dev->xae_cnt)
370                 xaq_cnt += dev->xae_cnt / dev->xae_waes;
371         else
372                 xaq_cnt += (dev->iue / dev->xae_waes) +
373                         (OTX2_SSO_XAQ_SLACK * dev->nb_event_queues);
374
375         otx2_sso_dbg("Configuring %d xaq buffers", xaq_cnt);
376         /* Setup XAQ based on number of nb queues. */
377         snprintf(pool_name, 30, "otx2_xaq_buf_pool_%d", reconfig_cnt);
378         dev->xaq_pool = (void *)rte_mempool_create_empty(pool_name,
379                         xaq_cnt, dev->xaq_buf_size, 0, 0,
380                         rte_socket_id(), 0);
381
382         if (dev->xaq_pool == NULL) {
383                 otx2_err("Unable to create empty mempool.");
384                 rte_memzone_free(mz);
385                 return -ENOMEM;
386         }
387
388         rc = rte_mempool_set_ops_byname(dev->xaq_pool,
389                                         rte_mbuf_platform_mempool_ops(), aura);
390         if (rc != 0) {
391                 otx2_err("Unable to set xaqpool ops.");
392                 goto alloc_fail;
393         }
394
395         rc = rte_mempool_populate_default(dev->xaq_pool);
396         if (rc < 0) {
397                 otx2_err("Unable to set populate xaqpool.");
398                 goto alloc_fail;
399         }
400         reconfig_cnt++;
401         /* When SW does addwork (enqueue) check if there is space in XAQ by
402          * comparing fc_addr above against the xaq_lmt calculated below.
403          * There should be a minimum headroom (OTX2_SSO_XAQ_SLACK / 2) for SSO
404          * to request XAQ to cache them even before enqueue is called.
405          */
406         dev->xaq_lmt = xaq_cnt - (OTX2_SSO_XAQ_SLACK / 2 *
407                                   dev->nb_event_queues);
408         dev->nb_xaq_cfg = xaq_cnt;
409
410         return 0;
411 alloc_fail:
412         rte_mempool_free(dev->xaq_pool);
413         rte_memzone_free(mz);
414         return rc;
415 }
416
417 static int
418 sso_ggrp_alloc_xaq(struct otx2_sso_evdev *dev)
419 {
420         struct otx2_mbox *mbox = dev->mbox;
421         struct sso_hw_setconfig *req;
422
423         otx2_sso_dbg("Configuring XAQ for GGRPs");
424         req = otx2_mbox_alloc_msg_sso_hw_setconfig(mbox);
425         req->npa_pf_func = otx2_npa_pf_func_get();
426         req->npa_aura_id = npa_lf_aura_handle_to_aura(dev->xaq_pool->pool_id);
427         req->hwgrps = dev->nb_event_queues;
428
429         return otx2_mbox_process(mbox);
430 }
431
432 static void
433 sso_lf_teardown(struct otx2_sso_evdev *dev,
434                 enum otx2_sso_lf_type lf_type)
435 {
436         uint8_t nb_lf;
437
438         switch (lf_type) {
439         case SSO_LF_GGRP:
440                 nb_lf = dev->nb_event_queues;
441                 break;
442         case SSO_LF_GWS:
443                 nb_lf = dev->nb_event_ports;
444                 break;
445         default:
446                 return;
447         }
448
449         sso_lf_cfg(dev, dev->mbox, lf_type, nb_lf, false);
450         sso_hw_lf_cfg(dev->mbox, lf_type, nb_lf, false);
451 }
452
453 static int
454 otx2_sso_configure(const struct rte_eventdev *event_dev)
455 {
456         struct rte_event_dev_config *conf = &event_dev->data->dev_conf;
457         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
458         uint32_t deq_tmo_ns;
459         int rc;
460
461         sso_func_trace();
462         deq_tmo_ns = conf->dequeue_timeout_ns;
463
464         if (deq_tmo_ns == 0)
465                 deq_tmo_ns = dev->min_dequeue_timeout_ns;
466
467         if (deq_tmo_ns < dev->min_dequeue_timeout_ns ||
468             deq_tmo_ns > dev->max_dequeue_timeout_ns) {
469                 otx2_err("Unsupported dequeue timeout requested");
470                 return -EINVAL;
471         }
472
473         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
474                 dev->is_timeout_deq = 1;
475
476         dev->deq_tmo_ns = deq_tmo_ns;
477
478         if (conf->nb_event_ports > dev->max_event_ports ||
479             conf->nb_event_queues > dev->max_event_queues) {
480                 otx2_err("Unsupported event queues/ports requested");
481                 return -EINVAL;
482         }
483
484         if (conf->nb_event_port_dequeue_depth > 1) {
485                 otx2_err("Unsupported event port deq depth requested");
486                 return -EINVAL;
487         }
488
489         if (conf->nb_event_port_enqueue_depth > 1) {
490                 otx2_err("Unsupported event port enq depth requested");
491                 return -EINVAL;
492         }
493
494         if (dev->nb_event_queues) {
495                 /* Finit any previous queues. */
496                 sso_lf_teardown(dev, SSO_LF_GGRP);
497         }
498         if (dev->nb_event_ports) {
499                 /* Finit any previous ports. */
500                 sso_lf_teardown(dev, SSO_LF_GWS);
501         }
502
503         dev->nb_event_queues = conf->nb_event_queues;
504         dev->nb_event_ports = conf->nb_event_ports;
505
506         if (sso_configure_ports(event_dev)) {
507                 otx2_err("Failed to configure event ports");
508                 return -ENODEV;
509         }
510
511         if (sso_configure_queues(event_dev) < 0) {
512                 otx2_err("Failed to configure event queues");
513                 rc = -ENODEV;
514                 goto teardown_hws;
515         }
516
517         if (sso_xaq_allocate(dev) < 0) {
518                 rc = -ENOMEM;
519                 goto teardown_hwggrp;
520         }
521
522         /* Clear any prior port-queue mapping. */
523         sso_clr_links(event_dev);
524         rc = sso_ggrp_alloc_xaq(dev);
525         if (rc < 0) {
526                 otx2_err("Failed to alloc xaq to ggrp %d", rc);
527                 goto teardown_hwggrp;
528         }
529
530         dev->configured = 1;
531         rte_mb();
532
533         return 0;
534 teardown_hwggrp:
535         sso_lf_teardown(dev, SSO_LF_GGRP);
536 teardown_hws:
537         sso_lf_teardown(dev, SSO_LF_GWS);
538         dev->nb_event_queues = 0;
539         dev->nb_event_ports = 0;
540         dev->configured = 0;
541         return rc;
542 }
543
544 static void
545 otx2_sso_queue_def_conf(struct rte_eventdev *event_dev, uint8_t queue_id,
546                         struct rte_event_queue_conf *queue_conf)
547 {
548         RTE_SET_USED(event_dev);
549         RTE_SET_USED(queue_id);
550
551         queue_conf->nb_atomic_flows = (1ULL << 20);
552         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
553         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
554         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
555 }
556
557 static int
558 otx2_sso_queue_setup(struct rte_eventdev *event_dev, uint8_t queue_id,
559                      const struct rte_event_queue_conf *queue_conf)
560 {
561         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
562         struct otx2_mbox *mbox = dev->mbox;
563         struct sso_grp_priority *req;
564         int rc;
565
566         sso_func_trace("Queue=%d prio=%d", queue_id, queue_conf->priority);
567
568         req = otx2_mbox_alloc_msg_sso_grp_set_priority(dev->mbox);
569         req->grp = queue_id;
570         req->weight = 0xFF;
571         req->affinity = 0xFF;
572         /* Normalize <0-255> to <0-7> */
573         req->priority = queue_conf->priority / 32;
574
575         rc = otx2_mbox_process(mbox);
576         if (rc < 0) {
577                 otx2_err("Failed to set priority queue=%d", queue_id);
578                 return rc;
579         }
580
581         return 0;
582 }
583
584 static void
585 otx2_sso_port_def_conf(struct rte_eventdev *event_dev, uint8_t port_id,
586                        struct rte_event_port_conf *port_conf)
587 {
588         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
589
590         RTE_SET_USED(port_id);
591         port_conf->new_event_threshold = dev->max_num_events;
592         port_conf->dequeue_depth = 1;
593         port_conf->enqueue_depth = 1;
594 }
595
596 static int
597 otx2_sso_port_setup(struct rte_eventdev *event_dev, uint8_t port_id,
598                     const struct rte_event_port_conf *port_conf)
599 {
600         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
601         uintptr_t grps_base[OTX2_SSO_MAX_VHGRP] = {0};
602         uint64_t val;
603         uint16_t q;
604
605         sso_func_trace("Port=%d", port_id);
606         RTE_SET_USED(port_conf);
607
608         if (event_dev->data->ports[port_id] == NULL) {
609                 otx2_err("Invalid port Id %d", port_id);
610                 return -EINVAL;
611         }
612
613         for (q = 0; q < dev->nb_event_queues; q++) {
614                 grps_base[q] = dev->bar2 + (RVU_BLOCK_ADDR_SSO << 20 | q << 12);
615                 if (grps_base[q] == 0) {
616                         otx2_err("Failed to get grp[%d] base addr", q);
617                         return -EINVAL;
618                 }
619         }
620
621         /* Set get_work timeout for HWS */
622         val = NSEC2USEC(dev->deq_tmo_ns) - 1;
623
624         struct otx2_ssogws *ws = event_dev->data->ports[port_id];
625         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
626
627         rte_memcpy(ws->grps_base, grps_base,
628                    sizeof(uintptr_t) * OTX2_SSO_MAX_VHGRP);
629         ws->fc_mem = dev->fc_mem;
630         ws->xaq_lmt = dev->xaq_lmt;
631         otx2_write64(val, base + SSOW_LF_GWS_NW_TIM);
632
633         otx2_sso_dbg("Port=%d ws=%p", port_id, event_dev->data->ports[port_id]);
634
635         return 0;
636 }
637
638 /* Initialize and register event driver with DPDK Application */
639 static struct rte_eventdev_ops otx2_sso_ops = {
640         .dev_infos_get    = otx2_sso_info_get,
641         .dev_configure    = otx2_sso_configure,
642         .queue_def_conf   = otx2_sso_queue_def_conf,
643         .queue_setup      = otx2_sso_queue_setup,
644         .queue_release    = otx2_sso_queue_release,
645         .port_def_conf    = otx2_sso_port_def_conf,
646         .port_setup       = otx2_sso_port_setup,
647         .port_release     = otx2_sso_port_release,
648         .port_link        = otx2_sso_port_link,
649         .port_unlink      = otx2_sso_port_unlink,
650 };
651
652 #define OTX2_SSO_XAE_CNT        "xae_cnt"
653
654 static void
655 sso_parse_devargs(struct otx2_sso_evdev *dev, struct rte_devargs *devargs)
656 {
657         struct rte_kvargs *kvlist;
658
659         if (devargs == NULL)
660                 return;
661         kvlist = rte_kvargs_parse(devargs->args, NULL);
662         if (kvlist == NULL)
663                 return;
664
665         rte_kvargs_process(kvlist, OTX2_SSO_XAE_CNT, &parse_kvargs_value,
666                            &dev->xae_cnt);
667
668         rte_kvargs_free(kvlist);
669 }
670
671 static int
672 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
673 {
674         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
675                                        sizeof(struct otx2_sso_evdev),
676                                        otx2_sso_init);
677 }
678
679 static int
680 otx2_sso_remove(struct rte_pci_device *pci_dev)
681 {
682         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
683 }
684
685 static const struct rte_pci_id pci_sso_map[] = {
686         {
687                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
688                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
689         },
690         {
691                 .vendor_id = 0,
692         },
693 };
694
695 static struct rte_pci_driver pci_sso = {
696         .id_table = pci_sso_map,
697         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
698         .probe = otx2_sso_probe,
699         .remove = otx2_sso_remove,
700 };
701
702 int
703 otx2_sso_init(struct rte_eventdev *event_dev)
704 {
705         struct free_rsrcs_rsp *rsrc_cnt;
706         struct rte_pci_device *pci_dev;
707         struct otx2_sso_evdev *dev;
708         int rc;
709
710         event_dev->dev_ops = &otx2_sso_ops;
711         /* For secondary processes, the primary has done all the work */
712         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
713                 return 0;
714
715         dev = sso_pmd_priv(event_dev);
716
717         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
718
719         /* Initialize the base otx2_dev object */
720         rc = otx2_dev_init(pci_dev, dev);
721         if (rc < 0) {
722                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
723                 goto error;
724         }
725
726         /* Get SSO and SSOW MSIX rsrc cnt */
727         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
728         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
729         if (rc < 0) {
730                 otx2_err("Unable to get free rsrc count");
731                 goto otx2_dev_uninit;
732         }
733         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
734                      rsrc_cnt->ssow, rsrc_cnt->npa);
735
736         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
737         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
738         /* Grab the NPA LF if required */
739         rc = otx2_npa_lf_init(pci_dev, dev);
740         if (rc < 0) {
741                 otx2_err("Unable to init NPA lf. It might not be provisioned");
742                 goto otx2_dev_uninit;
743         }
744
745         dev->drv_inited = true;
746         dev->is_timeout_deq = 0;
747         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
748         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
749         dev->max_num_events = -1;
750         dev->nb_event_queues = 0;
751         dev->nb_event_ports = 0;
752
753         if (!dev->max_event_ports || !dev->max_event_queues) {
754                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
755                          dev->max_event_queues, dev->max_event_ports);
756                 rc = -ENODEV;
757                 goto otx2_npa_lf_uninit;
758         }
759
760         sso_parse_devargs(dev, pci_dev->device.devargs);
761
762         otx2_sso_pf_func_set(dev->pf_func);
763         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
764                      event_dev->data->name, dev->max_event_queues,
765                      dev->max_event_ports);
766
767
768         return 0;
769
770 otx2_npa_lf_uninit:
771         otx2_npa_lf_fini();
772 otx2_dev_uninit:
773         otx2_dev_fini(pci_dev, dev);
774 error:
775         return rc;
776 }
777
778 int
779 otx2_sso_fini(struct rte_eventdev *event_dev)
780 {
781         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
782         struct rte_pci_device *pci_dev;
783
784         /* For secondary processes, nothing to be done */
785         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
786                 return 0;
787
788         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
789
790         if (!dev->drv_inited)
791                 goto dev_fini;
792
793         dev->drv_inited = false;
794         otx2_npa_lf_fini();
795
796 dev_fini:
797         if (otx2_npa_lf_active(dev)) {
798                 otx2_info("Common resource in use by other devices");
799                 return -EAGAIN;
800         }
801
802         otx2_dev_fini(pci_dev, dev);
803
804         return 0;
805 }
806
807 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
808 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
809 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");
810 RTE_PMD_REGISTER_PARAM_STRING(event_octeontx2, OTX2_SSO_XAE_CNT "=<int>");