crypto/octeontx: add hardware init routine
[dpdk.git] / drivers / crypto / octeontx / otx_cryptodev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4
5 #include <rte_alarm.h>
6 #include <rte_bus_pci.h>
7 #include <rte_cryptodev.h>
8 #include <rte_malloc.h>
9
10 #include "cpt_pmd_logs.h"
11
12 #include "otx_cryptodev.h"
13 #include "otx_cryptodev_hw_access.h"
14 #include "otx_cryptodev_ops.h"
15
16 /* Alarm routines */
17
18 static void
19 otx_cpt_alarm_cb(void *arg)
20 {
21         struct cpt_vf *cptvf = arg;
22         otx_cpt_poll_misc(cptvf);
23         rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
24                           otx_cpt_alarm_cb, cptvf);
25 }
26
27 static int
28 otx_cpt_periodic_alarm_start(void *arg)
29 {
30         return rte_eal_alarm_set(CPT_INTR_POLL_INTERVAL_MS * 1000,
31                                  otx_cpt_alarm_cb, arg);
32 }
33
34 int
35 otx_cpt_dev_create(struct rte_cryptodev *c_dev)
36 {
37         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(c_dev->device);
38         struct cpt_vf *cptvf = NULL;
39         void *reg_base;
40         char dev_name[32];
41         int ret;
42
43         if (pdev->mem_resource[0].phys_addr == 0ULL)
44                 return -EIO;
45
46         /* for secondary processes, we don't initialise any further as primary
47          * has already done this work.
48          */
49         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
50                 return 0;
51
52         cptvf = rte_zmalloc_socket("otx_cryptodev_private_mem",
53                         sizeof(struct cpt_vf), RTE_CACHE_LINE_SIZE,
54                         rte_socket_id());
55
56         if (cptvf == NULL) {
57                 CPT_LOG_ERR("Cannot allocate memory for device private data");
58                 return -ENOMEM;
59         }
60
61         snprintf(dev_name, 32, "%02x:%02x.%x",
62                         pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
63
64         reg_base = pdev->mem_resource[0].addr;
65         if (!reg_base) {
66                 CPT_LOG_ERR("Failed to map BAR0 of %s", dev_name);
67                 ret = -ENODEV;
68                 goto fail;
69         }
70
71         ret = otx_cpt_hw_init(cptvf, pdev, reg_base, dev_name);
72         if (ret) {
73                 CPT_LOG_ERR("Failed to init cptvf %s", dev_name);
74                 ret = -EIO;
75                 goto fail;
76         }
77
78         /* Start off timer for mailbox interrupts */
79         otx_cpt_periodic_alarm_start(cptvf);
80
81         c_dev->dev_ops = NULL;
82
83         c_dev->enqueue_burst = NULL;
84         c_dev->dequeue_burst = NULL;
85
86         c_dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
87                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
88                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
89                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
90                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
91                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT;
92
93         /* Save dev private data */
94         c_dev->data->dev_private = cptvf;
95
96         return 0;
97
98 fail:
99         if (cptvf) {
100                 /* Free private data allocated */
101                 rte_free(cptvf);
102         }
103
104         return ret;
105 }