a51d532553d7eab7d9db13bf45d261d6426ad137
[dpdk.git] / drivers / crypto / octeontx2 / otx2_cryptodev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <rte_crypto.h>
8 #include <rte_cryptodev.h>
9 #include <rte_cryptodev_pmd.h>
10 #include <rte_dev.h>
11 #include <rte_errno.h>
12 #include <rte_mempool.h>
13 #include <rte_pci.h>
14
15 #include "otx2_common.h"
16 #include "otx2_cryptodev.h"
17 #include "otx2_cryptodev_capabilities.h"
18 #include "otx2_cryptodev_mbox.h"
19 #include "otx2_cryptodev_ops.h"
20 #include "otx2_dev.h"
21
22 /* CPT common headers */
23 #include "cpt_common.h"
24 #include "cpt_pmd_logs.h"
25
26 uint8_t otx2_cryptodev_driver_id;
27
28 static struct rte_pci_id pci_id_cpt_table[] = {
29         {
30                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
31                                PCI_DEVID_OCTEONTX2_RVU_CPT_VF)
32         },
33         /* sentinel */
34         {
35                 .device_id = 0
36         },
37 };
38
39 static int
40 otx2_cpt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
41                    struct rte_pci_device *pci_dev)
42 {
43         struct rte_cryptodev_pmd_init_params init_params = {
44                 .name = "",
45                 .socket_id = rte_socket_id(),
46                 .private_data_size = sizeof(struct otx2_cpt_vf)
47         };
48         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
49         struct rte_cryptodev *dev;
50         struct otx2_dev *otx2_dev;
51         struct otx2_cpt_vf *vf;
52         uint16_t nb_queues;
53         int ret;
54
55         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
56
57         dev = rte_cryptodev_pmd_create(name, &pci_dev->device, &init_params);
58         if (dev == NULL) {
59                 ret = -ENODEV;
60                 goto exit;
61         }
62
63         dev->dev_ops = &otx2_cpt_ops;
64
65         dev->driver_id = otx2_cryptodev_driver_id;
66
67         /* Get private data space allocated */
68         vf = dev->data->dev_private;
69
70         otx2_dev = &vf->otx2_dev;
71
72         /* Initialize the base otx2_dev object */
73         ret = otx2_dev_init(pci_dev, otx2_dev);
74         if (ret) {
75                 CPT_LOG_ERR("Could not initialize otx2_dev");
76                 goto pmd_destroy;
77         }
78
79         /* Get number of queues available on the device */
80         ret = otx2_cpt_available_queues_get(dev, &nb_queues);
81         if (ret) {
82                 CPT_LOG_ERR("Could not determine the number of queues available");
83                 goto otx2_dev_fini;
84         }
85
86         /* Don't exceed the limits set per VF */
87         nb_queues = RTE_MIN(nb_queues, OTX2_CPT_MAX_QUEUES_PER_VF);
88
89         if (nb_queues == 0) {
90                 CPT_LOG_ERR("No free queues available on the device");
91                 goto otx2_dev_fini;
92         }
93
94         vf->max_queues = nb_queues;
95
96         CPT_LOG_INFO("Max queues supported by device: %d", vf->max_queues);
97
98         ret = otx2_cpt_hardware_caps_get(dev, vf->hw_caps);
99         if (ret) {
100                 CPT_LOG_ERR("Could not determine hardware capabilities");
101                 goto otx2_dev_fini;
102         }
103
104         otx2_crypto_capabilities_init(vf->hw_caps);
105
106         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
107                              RTE_CRYPTODEV_FF_HW_ACCELERATED |
108                              RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
109                              RTE_CRYPTODEV_FF_IN_PLACE_SGL |
110                              RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
111                              RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
112                              RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
113                              RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
114                              RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
115
116         return 0;
117
118 otx2_dev_fini:
119         otx2_dev_fini(pci_dev, otx2_dev);
120 pmd_destroy:
121         rte_cryptodev_pmd_destroy(dev);
122 exit:
123         CPT_LOG_ERR("Could not create device (vendor_id: 0x%x device_id: 0x%x)",
124                     pci_dev->id.vendor_id, pci_dev->id.device_id);
125         return ret;
126 }
127
128 static int
129 otx2_cpt_pci_remove(struct rte_pci_device *pci_dev)
130 {
131         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
132         struct rte_cryptodev *dev;
133
134         if (pci_dev == NULL)
135                 return -EINVAL;
136
137         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
138
139         dev = rte_cryptodev_pmd_get_named_dev(name);
140         if (dev == NULL)
141                 return -ENODEV;
142
143         return rte_cryptodev_pmd_destroy(dev);
144 }
145
146 static struct rte_pci_driver otx2_cryptodev_pmd = {
147         .id_table = pci_id_cpt_table,
148         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
149         .probe = otx2_cpt_pci_probe,
150         .remove = otx2_cpt_pci_remove,
151 };
152
153 static struct cryptodev_driver otx2_cryptodev_drv;
154
155 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_OCTEONTX2_PMD, otx2_cryptodev_pmd);
156 RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_OCTEONTX2_PMD, pci_id_cpt_table);
157 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_OCTEONTX2_PMD, "vfio-pci");
158 RTE_PMD_REGISTER_CRYPTO_DRIVER(otx2_cryptodev_drv, otx2_cryptodev_pmd.driver,
159                 otx2_cryptodev_driver_id);
160 RTE_LOG_REGISTER(otx2_cpt_logtype, pmd.crypto.octeontx2, NOTICE);