cryptodev: move device-specific structures
[dpdk.git] / drivers / crypto / octeontx / otx_cryptodev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Cavium, Inc
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <cryptodev_pmd.h>
8 #include <rte_log.h>
9 #include <rte_pci.h>
10
11 #include "otx_cryptodev.h"
12 #include "otx_cryptodev_ops.h"
13
14 #include "cpt_pmd_logs.h"
15
16 /* Device ID */
17 #define PCI_VENDOR_ID_CAVIUM            0x177d
18 #define CPT_81XX_PCI_VF_DEVICE_ID       0xa041
19
20 uint8_t otx_cryptodev_driver_id;
21
22 static struct rte_pci_id pci_id_cpt_table[] = {
23         {
24                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, CPT_81XX_PCI_VF_DEVICE_ID),
25         },
26         /* sentinel */
27         {
28                 .device_id = 0
29         },
30 };
31
32 static int
33 otx_cpt_pci_probe(struct rte_pci_driver *pci_drv,
34                         struct rte_pci_device *pci_dev)
35 {
36         struct rte_cryptodev *cryptodev;
37         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
38         int retval;
39
40         if (pci_drv == NULL)
41                 return -ENODEV;
42
43         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
44
45         cryptodev = rte_cryptodev_pmd_allocate(name, rte_socket_id());
46         if (cryptodev == NULL)
47                 return -ENOMEM;
48
49         cryptodev->device = &pci_dev->device;
50         cryptodev->device->driver = &pci_drv->driver;
51         cryptodev->driver_id = otx_cryptodev_driver_id;
52
53         /* init user callbacks */
54         TAILQ_INIT(&(cryptodev->link_intr_cbs));
55
56         /* Invoke PMD device initialization function */
57         retval = otx_cpt_dev_create(cryptodev);
58         if (retval == 0) {
59                 rte_cryptodev_pmd_probing_finish(cryptodev);
60                 return 0;
61         }
62
63         CPT_LOG_ERR("[DRV %s]: Failed to create device "
64                         "(vendor_id: 0x%x device_id: 0x%x",
65                         pci_drv->driver.name,
66                         (unsigned int) pci_dev->id.vendor_id,
67                         (unsigned int) pci_dev->id.device_id);
68
69         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
70
71         return -ENXIO;
72 }
73
74 static int
75 otx_cpt_pci_remove(struct rte_pci_device *pci_dev)
76 {
77         struct rte_cryptodev *cryptodev;
78         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
79         void *dev_priv;
80
81         if (pci_dev == NULL)
82                 return -EINVAL;
83
84         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
85
86         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
87         if (cryptodev == NULL)
88                 return -ENODEV;
89
90         if (pci_dev->driver == NULL)
91                 return -ENODEV;
92
93         dev_priv = cryptodev->data->dev_private;
94
95         /* free crypto device */
96         rte_cryptodev_pmd_release_device(cryptodev);
97
98         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
99                 rte_free(dev_priv);
100
101         cryptodev->device->driver = NULL;
102         cryptodev->device = NULL;
103         cryptodev->data = NULL;
104
105         return 0;
106 }
107
108 static struct rte_pci_driver otx_cryptodev_pmd = {
109         .id_table = pci_id_cpt_table,
110         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
111         .probe = otx_cpt_pci_probe,
112         .remove = otx_cpt_pci_remove,
113 };
114
115 static struct cryptodev_driver otx_cryptodev_drv;
116
117 RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_OCTEONTX_PMD, otx_cryptodev_pmd);
118 RTE_PMD_REGISTER_PCI_TABLE(CRYPTODEV_NAME_OCTEONTX_PMD, pci_id_cpt_table);
119 RTE_PMD_REGISTER_KMOD_DEP(CRYPTODEV_NAME_OCTEONTX_PMD, "vfio-pci");
120 RTE_PMD_REGISTER_CRYPTO_DRIVER(otx_cryptodev_drv, otx_cryptodev_pmd.driver,
121                 otx_cryptodev_driver_id);
122 RTE_LOG_REGISTER_DEFAULT(otx_cpt_logtype, NOTICE);