b24642ddf8e78a66348f9c997586a85afe2eae1e
[deb_dpdk.git] / drivers / bus / fslmc / fslmc_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 NXP. All rights reserved.
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
36 #include <rte_log.h>
37 #include <rte_bus.h>
38 #include <rte_eal_memconfig.h>
39 #include <rte_malloc.h>
40 #include <rte_devargs.h>
41 #include <rte_memcpy.h>
42 #include <rte_ethdev.h>
43
44 #include "rte_fslmc.h"
45 #include "fslmc_vfio.h"
46
47 #define FSLMC_BUS_LOG(level, fmt, args...) \
48         RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
49
50 struct rte_fslmc_bus rte_fslmc_bus;
51
52 static int
53 rte_fslmc_scan(void)
54 {
55         int ret;
56
57         ret = fslmc_vfio_setup_group();
58         if (ret) {
59                 FSLMC_BUS_LOG(ERR, "fslmc: Unable to setup VFIO");
60                 return ret;
61         }
62
63         ret = fslmc_vfio_process_group();
64         if (ret) {
65                 FSLMC_BUS_LOG(ERR, "fslmc: Unable to setup devices");
66                 return -1;
67         }
68
69         RTE_LOG(INFO, EAL, "fslmc: Bus scan completed\n");
70         return 0;
71 }
72
73 static int
74 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
75                 struct rte_dpaa2_device *dpaa2_dev)
76 {
77         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
78                 return 0;
79
80         return 1;
81 }
82
83 static int
84 rte_fslmc_probe(void)
85 {
86         int ret = 0;
87         struct rte_dpaa2_device *dev;
88         struct rte_dpaa2_driver *drv;
89
90         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
91                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
92                         ret = rte_fslmc_match(drv, dev);
93                         if (ret)
94                                 continue;
95
96                         if (!drv->probe)
97                                 continue;
98
99                         ret = drv->probe(drv, dev);
100                         if (ret)
101                                 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
102                         break;
103                 }
104         }
105         return ret;
106 }
107
108 /*register a fslmc bus based dpaa2 driver */
109 void
110 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
111 {
112         RTE_VERIFY(driver);
113
114         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
115         /* Update Bus references */
116         driver->fslmc_bus = &rte_fslmc_bus;
117 }
118
119 /*un-register a fslmc bus based dpaa2 driver */
120 void
121 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
122 {
123         struct rte_fslmc_bus *fslmc_bus;
124
125         fslmc_bus = driver->fslmc_bus;
126
127         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
128         /* Update Bus references */
129         driver->fslmc_bus = NULL;
130 }
131
132 struct rte_fslmc_bus rte_fslmc_bus = {
133         .bus = {
134                 .scan = rte_fslmc_scan,
135                 .probe = rte_fslmc_probe,
136         },
137         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
138         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
139 };
140
141 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);