drivers/crypto: invoke probing finish function
[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 <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_cryptodev_sec.h"
21 #include "otx2_dev.h"
22
23 /* CPT common headers */
24 #include "cpt_common.h"
25 #include "cpt_pmd_logs.h"
26
27 uint8_t otx2_cryptodev_driver_id;
28
29 static struct rte_pci_id pci_id_cpt_table[] = {
30         {
31                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
32                                PCI_DEVID_OCTEONTX2_RVU_CPT_VF)
33         },
34         /* sentinel */
35         {
36                 .device_id = 0
37         },
38 };
39
40 uint64_t
41 otx2_cpt_default_ff_get(void)
42 {
43         return RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
44                RTE_CRYPTODEV_FF_HW_ACCELERATED |
45                RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
46                RTE_CRYPTODEV_FF_IN_PLACE_SGL |
47                RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
48                RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
49                RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
50                RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
51                RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
52                RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
53                RTE_CRYPTODEV_FF_SECURITY |
54                RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
55 }
56
57 static int
58 otx2_cpt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
59                    struct rte_pci_device *pci_dev)
60 {
61         struct rte_cryptodev_pmd_init_params init_params = {
62                 .name = "",
63                 .socket_id = rte_socket_id(),
64                 .private_data_size = sizeof(struct otx2_cpt_vf)
65         };
66         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
67         struct rte_cryptodev *dev;
68         struct otx2_dev *otx2_dev;
69         struct otx2_cpt_vf *vf;
70         uint16_t nb_queues;
71         int ret;
72
73         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
74
75         dev = rte_cryptodev_pmd_create(name, &pci_dev->device, &init_params);
76         if (dev == NULL) {
77                 ret = -ENODEV;
78                 goto exit;
79         }
80
81         dev->dev_ops = &otx2_cpt_ops;
82
83         dev->driver_id = otx2_cryptodev_driver_id;
84
85         /* Get private data space allocated */
86         vf = dev->data->dev_private;
87
88         otx2_dev = &vf->otx2_dev;
89
90         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
91                 /* Initialize the base otx2_dev object */
92                 ret = otx2_dev_init(pci_dev, otx2_dev);
93                 if (ret) {
94                         CPT_LOG_ERR("Could not initialize otx2_dev");
95                         goto pmd_destroy;
96                 }
97
98                 /* Get number of queues available on the device */
99                 ret = otx2_cpt_available_queues_get(dev, &nb_queues);
100                 if (ret) {
101                         CPT_LOG_ERR("Could not determine the number of queues available");
102                         goto otx2_dev_fini;
103                 }
104
105                 /* Don't exceed the limits set per VF */
106                 nb_queues = RTE_MIN(nb_queues, OTX2_CPT_MAX_QUEUES_PER_VF);
107
108                 if (nb_queues == 0) {
109                         CPT_LOG_ERR("No free queues available on the device");
110                         goto otx2_dev_fini;
111                 }
112
113                 vf->max_queues = nb_queues;
114
115                 CPT_LOG_INFO("Max queues supported by device: %d",
116                                 vf->max_queues);
117
118                 ret = otx2_cpt_hardware_caps_get(dev, vf->hw_caps);
119                 if (ret) {
120                         CPT_LOG_ERR("Could not determine hardware capabilities");
121                         goto otx2_dev_fini;
122                 }
123         }
124
125         otx2_crypto_capabilities_init(vf->hw_caps);
126         otx2_crypto_sec_capabilities_init(vf->hw_caps);
127
128         /* Create security ctx */
129         ret = otx2_crypto_sec_ctx_create(dev);
130         if (ret)
131                 goto otx2_dev_fini;
132
133         dev->feature_flags = otx2_cpt_default_ff_get();
134
135         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
136                 otx2_cpt_set_enqdeq_fns(dev);
137
138         rte_cryptodev_pmd_probing_finish(dev);
139
140         return 0;
141
142 otx2_dev_fini:
143         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
144                 otx2_dev_fini(pci_dev, otx2_dev);
145 pmd_destroy:
146         rte_cryptodev_pmd_destroy(dev);
147 exit:
148         CPT_LOG_ERR("Could not create device (vendor_id: 0x%x device_id: 0x%x)",
149                     pci_dev->id.vendor_id, pci_dev->id.device_id);
150         return ret;
151 }
152
153 static int
154 otx2_cpt_pci_remove(struct rte_pci_device *pci_dev)
155 {
156         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
157         struct rte_cryptodev *dev;
158
159         if (pci_dev == NULL)
160                 return -EINVAL;
161
162         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
163
164         dev = rte_cryptodev_pmd_get_named_dev(name);
165         if (dev == NULL)
166                 return -ENODEV;
167
168         /* Destroy security ctx */
169         otx2_crypto_sec_ctx_destroy(dev);
170
171         return rte_cryptodev_pmd_destroy(dev);
172 }
173
174 static struct rte_pci_driver otx2_cryptodev_pmd = {
175         .id_table = pci_id_cpt_table,
176         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
177         .probe = otx2_cpt_pci_probe,
178         .remove = otx2_cpt_pci_remove,
179 };
180
181 static struct cryptodev_driver otx2_cryptodev_drv;
182
183 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_OCTEONTX2_PMD, otx2_cryptodev_pmd);
184 RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_OCTEONTX2_PMD, pci_id_cpt_table);
185 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_OCTEONTX2_PMD, "vfio-pci");
186 RTE_PMD_REGISTER_CRYPTO_DRIVER(otx2_cryptodev_drv, otx2_cryptodev_pmd.driver,
187                 otx2_cryptodev_driver_id);
188 RTE_LOG_REGISTER_DEFAULT(otx2_cpt_logtype, NOTICE);