f71598d5793be2fdaa6a99e2bd16bfeb0897b2b8
[deb_dpdk.git] / drivers / bus / fslmc / fslmc_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 NXP.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of NXP nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <string.h>
34 #include <dirent.h>
35 #include <stdbool.h>
36
37 #include <rte_log.h>
38 #include <rte_bus.h>
39 #include <rte_eal_memconfig.h>
40 #include <rte_malloc.h>
41 #include <rte_devargs.h>
42 #include <rte_memcpy.h>
43 #include <rte_ethdev.h>
44
45 #include "rte_fslmc.h"
46 #include "fslmc_vfio.h"
47
48 #define FSLMC_BUS_LOG(level, fmt, args...) \
49         RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
50
51 struct rte_fslmc_bus rte_fslmc_bus;
52
53 static int
54 rte_fslmc_scan(void)
55 {
56         int ret;
57
58         ret = fslmc_vfio_setup_group();
59         if (ret) {
60                 FSLMC_BUS_LOG(ERR, "fslmc: Unable to setup VFIO");
61                 return ret;
62         }
63
64         ret = fslmc_vfio_process_group();
65         if (ret) {
66                 FSLMC_BUS_LOG(ERR, "fslmc: Unable to setup devices");
67                 return -1;
68         }
69
70         RTE_LOG(INFO, EAL, "fslmc: Bus scan completed\n");
71         return 0;
72 }
73
74 static int
75 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
76                 struct rte_dpaa2_device *dpaa2_dev)
77 {
78         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
79                 return 0;
80
81         return 1;
82 }
83
84 static int
85 rte_fslmc_probe(void)
86 {
87         int ret = 0;
88         struct rte_dpaa2_device *dev;
89         struct rte_dpaa2_driver *drv;
90
91         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
92                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
93                         ret = rte_fslmc_match(drv, dev);
94                         if (ret)
95                                 continue;
96
97                         if (!drv->probe)
98                                 continue;
99
100                         ret = drv->probe(drv, dev);
101                         if (ret)
102                                 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
103                         break;
104                 }
105         }
106         return ret;
107 }
108
109 static struct rte_device *
110 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
111                       const void *data)
112 {
113         struct rte_dpaa2_device *dev;
114
115         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
116                 if (start && &dev->device == start) {
117                         start = NULL;  /* starting point found */
118                         continue;
119                 }
120
121                 if (cmp(&dev->device, data) == 0)
122                         return &dev->device;
123         }
124
125         return NULL;
126 }
127
128 /*register a fslmc bus based dpaa2 driver */
129 void
130 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
131 {
132         RTE_VERIFY(driver);
133
134         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
135         /* Update Bus references */
136         driver->fslmc_bus = &rte_fslmc_bus;
137 }
138
139 /*un-register a fslmc bus based dpaa2 driver */
140 void
141 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
142 {
143         struct rte_fslmc_bus *fslmc_bus;
144
145         fslmc_bus = driver->fslmc_bus;
146
147         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
148         /* Update Bus references */
149         driver->fslmc_bus = NULL;
150 }
151
152 struct rte_fslmc_bus rte_fslmc_bus = {
153         .bus = {
154                 .scan = rte_fslmc_scan,
155                 .probe = rte_fslmc_probe,
156                 .find_device = rte_fslmc_find_device,
157         },
158         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
159         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
160 };
161
162 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);