raw/dpaa2_cmdif: add attribute get functionality
[dpdk.git] / drivers / raw / dpaa2_cmdif / dpaa2_cmdif.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 NXP
3  */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8
9 #include <rte_bus_vdev.h>
10 #include <rte_atomic.h>
11 #include <rte_interrupts.h>
12 #include <rte_branch_prediction.h>
13 #include <rte_lcore.h>
14
15 #include <rte_rawdev.h>
16 #include <rte_rawdev_pmd.h>
17
18 #include <portal/dpaa2_hw_pvt.h>
19 #include <portal/dpaa2_hw_dpio.h>
20 #include "dpaa2_cmdif_logs.h"
21
22 /* Dynamic log type identifier */
23 int dpaa2_cmdif_logtype;
24
25 /* CMDIF driver name */
26 #define DPAA2_CMDIF_PMD_NAME dpaa2_dpci
27
28 /* CMDIF driver object */
29 static struct rte_vdev_driver dpaa2_cmdif_drv;
30
31 /*
32  * This API provides the DPCI device ID in 'attr_value'.
33  * The device ID shall be passed by GPP to the AIOP using CMDIF commands.
34  */
35 static int
36 dpaa2_cmdif_get_attr(struct rte_rawdev *dev,
37                      const char *attr_name,
38                      uint64_t *attr_value)
39 {
40         struct dpaa2_dpci_dev *cidev = dev->dev_private;
41
42         DPAA2_CMDIF_FUNC_TRACE();
43
44         RTE_SET_USED(attr_name);
45
46         if (!attr_value) {
47                 DPAA2_CMDIF_ERR("Invalid arguments for getting attributes");
48                 return -EINVAL;
49         }
50         *attr_value = cidev->dpci_id;
51
52         return 0;
53 }
54
55 static const struct rte_rawdev_ops dpaa2_cmdif_ops = {
56         .attr_get = dpaa2_cmdif_get_attr,
57 };
58
59 static int
60 dpaa2_cmdif_create(const char *name,
61                    struct rte_vdev_device *vdev,
62                    int socket_id)
63 {
64         struct rte_rawdev *rawdev;
65         struct dpaa2_dpci_dev *cidev;
66
67         /* Allocate device structure */
68         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct dpaa2_dpci_dev),
69                                          socket_id);
70         if (!rawdev) {
71                 DPAA2_CMDIF_ERR("Unable to allocate rawdevice");
72                 return -EINVAL;
73         }
74
75         rawdev->dev_ops = &dpaa2_cmdif_ops;
76         rawdev->device = &vdev->device;
77         rawdev->driver_name = vdev->device.driver->name;
78
79         /* For secondary processes, the primary has done all the work */
80         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
81                 return 0;
82
83         cidev = rte_dpaa2_alloc_dpci_dev();
84         if (!cidev) {
85                 DPAA2_CMDIF_ERR("Unable to allocate CI device");
86                 rte_rawdev_pmd_release(rawdev);
87                 return -ENODEV;
88         }
89
90         rawdev->dev_private = cidev;
91
92         return 0;
93 }
94
95 static int
96 dpaa2_cmdif_destroy(const char *name)
97 {
98         int ret;
99         struct rte_rawdev *rdev;
100
101         rdev = rte_rawdev_pmd_get_named_dev(name);
102         if (!rdev) {
103                 DPAA2_CMDIF_ERR("Invalid device name (%s)", name);
104                 return -EINVAL;
105         }
106
107         /* The primary process will only free the DPCI device */
108         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
109                 rte_dpaa2_free_dpci_dev(rdev->dev_private);
110
111         ret = rte_rawdev_pmd_release(rdev);
112         if (ret)
113                 DPAA2_CMDIF_DEBUG("Device cleanup failed");
114
115         return 0;
116 }
117
118 static int
119 dpaa2_cmdif_probe(struct rte_vdev_device *vdev)
120 {
121         const char *name;
122         int ret = 0;
123
124         name = rte_vdev_device_name(vdev);
125
126         DPAA2_CMDIF_INFO("Init %s on NUMA node %d", name, rte_socket_id());
127
128         ret = dpaa2_cmdif_create(name, vdev, rte_socket_id());
129
130         return ret;
131 }
132
133 static int
134 dpaa2_cmdif_remove(struct rte_vdev_device *vdev)
135 {
136         const char *name;
137         int ret;
138
139         name = rte_vdev_device_name(vdev);
140
141         DPAA2_CMDIF_INFO("Closing %s on NUMA node %d", name, rte_socket_id());
142
143         ret = dpaa2_cmdif_destroy(name);
144
145         return ret;
146 }
147
148 static struct rte_vdev_driver dpaa2_cmdif_drv = {
149         .probe = dpaa2_cmdif_probe,
150         .remove = dpaa2_cmdif_remove
151 };
152
153 RTE_PMD_REGISTER_VDEV(DPAA2_CMDIF_PMD_NAME, dpaa2_cmdif_drv);
154
155 RTE_INIT(dpaa2_cmdif_init_log);
156
157 static void
158 dpaa2_cmdif_init_log(void)
159 {
160         dpaa2_cmdif_logtype = rte_log_register("pmd.raw.dpaa2.cmdif");
161         if (dpaa2_cmdif_logtype >= 0)
162                 rte_log_set_level(dpaa2_cmdif_logtype, RTE_LOG_INFO);
163 }