crypto/nitrox: introduce Nitrox driver
[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
10 #define PCI_VENDOR_ID_CAVIUM    0x177d
11 #define NITROX_V_PCI_VF_DEV_ID  0x13
12
13 TAILQ_HEAD(ndev_list, nitrox_device);
14 static struct ndev_list ndev_list = TAILQ_HEAD_INITIALIZER(ndev_list);
15
16 static struct nitrox_device *
17 ndev_allocate(struct rte_pci_device *pdev)
18 {
19         struct nitrox_device *ndev;
20
21         ndev = rte_zmalloc_socket("nitrox device", sizeof(*ndev),
22                                    RTE_CACHE_LINE_SIZE,
23                                    pdev->device.numa_node);
24         if (!ndev)
25                 return NULL;
26
27         TAILQ_INSERT_TAIL(&ndev_list, ndev, next);
28         return ndev;
29 }
30
31 static void
32 ndev_init(struct nitrox_device *ndev, struct rte_pci_device *pdev)
33 {
34         enum nitrox_vf_mode vf_mode;
35
36         ndev->pdev = pdev;
37         ndev->bar_addr = pdev->mem_resource[0].addr;
38         vf_mode = vf_get_vf_config_mode(ndev->bar_addr);
39         ndev->nr_queues = vf_config_mode_to_nr_queues(vf_mode);
40 }
41
42 static struct nitrox_device *
43 find_ndev(struct rte_pci_device *pdev)
44 {
45         struct nitrox_device *ndev;
46
47         TAILQ_FOREACH(ndev, &ndev_list, next)
48                 if (ndev->pdev == pdev)
49                         return ndev;
50
51         return NULL;
52 }
53
54 static void
55 ndev_release(struct nitrox_device *ndev)
56 {
57         if (!ndev)
58                 return;
59
60         TAILQ_REMOVE(&ndev_list, ndev, next);
61         rte_free(ndev);
62 }
63
64 static int
65 nitrox_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
66                 struct rte_pci_device *pdev)
67 {
68         struct nitrox_device *ndev;
69
70         /* Nitrox CSR space */
71         if (!pdev->mem_resource[0].addr)
72                 return -EINVAL;
73
74         ndev = ndev_allocate(pdev);
75         if (!ndev)
76                 return -ENOMEM;
77
78         ndev_init(ndev, pdev);
79         return 0;
80 }
81
82 static int
83 nitrox_pci_remove(struct rte_pci_device *pdev)
84 {
85         struct nitrox_device *ndev;
86
87         ndev = find_ndev(pdev);
88         if (!ndev)
89                 return -ENODEV;
90
91         ndev_release(ndev);
92         return 0;
93 }
94
95 static struct rte_pci_id pci_id_nitrox_map[] = {
96         {
97                 /* Nitrox 5 VF */
98                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, NITROX_V_PCI_VF_DEV_ID)
99         },
100         {.device_id = 0},
101 };
102
103 static struct rte_pci_driver nitrox_pmd = {
104         .id_table       = pci_id_nitrox_map,
105         .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
106         .probe          = nitrox_pci_probe,
107         .remove         = nitrox_pci_remove,
108 };
109
110 RTE_PMD_REGISTER_PCI(nitrox, nitrox_pmd);
111 RTE_PMD_REGISTER_PCI_TABLE(nitrox, pci_id_nitrox_map);