crypto/nitrox: create symmetric cryptodev
[dpdk.git] / drivers / crypto / nitrox / nitrox_device.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_malloc.h>
6
7 #include "nitrox_device.h"
8 #include "nitrox_hal.h"
9 #include "nitrox_sym.h"
10
11 #define PCI_VENDOR_ID_CAVIUM    0x177d
12 #define NITROX_V_PCI_VF_DEV_ID  0x13
13
14 TAILQ_HEAD(ndev_list, nitrox_device);
15 static struct ndev_list ndev_list = TAILQ_HEAD_INITIALIZER(ndev_list);
16
17 static struct nitrox_device *
18 ndev_allocate(struct rte_pci_device *pdev)
19 {
20         struct nitrox_device *ndev;
21
22         ndev = rte_zmalloc_socket("nitrox device", sizeof(*ndev),
23                                    RTE_CACHE_LINE_SIZE,
24                                    pdev->device.numa_node);
25         if (!ndev)
26                 return NULL;
27
28         TAILQ_INSERT_TAIL(&ndev_list, ndev, next);
29         return ndev;
30 }
31
32 static void
33 ndev_init(struct nitrox_device *ndev, struct rte_pci_device *pdev)
34 {
35         enum nitrox_vf_mode vf_mode;
36
37         ndev->pdev = pdev;
38         ndev->bar_addr = pdev->mem_resource[0].addr;
39         vf_mode = vf_get_vf_config_mode(ndev->bar_addr);
40         ndev->nr_queues = vf_config_mode_to_nr_queues(vf_mode);
41 }
42
43 static struct nitrox_device *
44 find_ndev(struct rte_pci_device *pdev)
45 {
46         struct nitrox_device *ndev;
47
48         TAILQ_FOREACH(ndev, &ndev_list, next)
49                 if (ndev->pdev == pdev)
50                         return ndev;
51
52         return NULL;
53 }
54
55 static void
56 ndev_release(struct nitrox_device *ndev)
57 {
58         if (!ndev)
59                 return;
60
61         TAILQ_REMOVE(&ndev_list, ndev, next);
62         rte_free(ndev);
63 }
64
65 static int
66 nitrox_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
67                 struct rte_pci_device *pdev)
68 {
69         struct nitrox_device *ndev;
70         int err;
71
72         /* Nitrox CSR space */
73         if (!pdev->mem_resource[0].addr)
74                 return -EINVAL;
75
76         ndev = ndev_allocate(pdev);
77         if (!ndev)
78                 return -ENOMEM;
79
80         ndev_init(ndev, pdev);
81         err = nitrox_sym_pmd_create(ndev);
82         if (err) {
83                 ndev_release(ndev);
84                 return err;
85         }
86
87         return 0;
88 }
89
90 static int
91 nitrox_pci_remove(struct rte_pci_device *pdev)
92 {
93         struct nitrox_device *ndev;
94         int err;
95
96         ndev = find_ndev(pdev);
97         if (!ndev)
98                 return -ENODEV;
99
100         err = nitrox_sym_pmd_destroy(ndev);
101         if (err)
102                 return err;
103
104         ndev_release(ndev);
105         return 0;
106 }
107
108 static struct rte_pci_id pci_id_nitrox_map[] = {
109         {
110                 /* Nitrox 5 VF */
111                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, NITROX_V_PCI_VF_DEV_ID)
112         },
113         {.device_id = 0},
114 };
115
116 static struct rte_pci_driver nitrox_pmd = {
117         .id_table       = pci_id_nitrox_map,
118         .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
119         .probe          = nitrox_pci_probe,
120         .remove         = nitrox_pci_remove,
121 };
122
123 RTE_PMD_REGISTER_PCI(nitrox, nitrox_pmd);
124 RTE_PMD_REGISTER_PCI_TABLE(nitrox, pci_id_nitrox_map);