c11621e9331f62ffdb79c20df4fc9f21bc41a85f
[dpdk.git] / drivers / net / failsafe / failsafe_eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5
6 #include <rte_malloc.h>
7
8 #include "failsafe_private.h"
9
10 static int
11 fs_ethdev_portid_get(const char *name, uint16_t *port_id)
12 {
13         uint16_t pid;
14         size_t len;
15
16         if (name == NULL) {
17                 DEBUG("Null pointer is specified\n");
18                 return -EINVAL;
19         }
20         len = strlen(name);
21         RTE_ETH_FOREACH_DEV(pid) {
22                 if (!strncmp(name, rte_eth_devices[pid].device->name, len)) {
23                         *port_id = pid;
24                         return 0;
25                 }
26         }
27         return -ENODEV;
28 }
29
30 static int
31 fs_bus_init(struct rte_eth_dev *dev)
32 {
33         struct sub_device *sdev;
34         struct rte_devargs *da;
35         uint8_t i;
36         uint16_t pid;
37         int ret;
38
39         FOREACH_SUBDEV(sdev, i, dev) {
40                 if (sdev->state != DEV_PARSED)
41                         continue;
42                 da = &sdev->devargs;
43                 if (fs_ethdev_portid_get(da->name, &pid) != 0) {
44                         ret = rte_eal_hotplug_add(da->bus->name,
45                                                   da->name,
46                                                   da->args);
47                         if (ret) {
48                                 ERROR("sub_device %d probe failed %s%s%s", i,
49                                       rte_errno ? "(" : "",
50                                       rte_errno ? strerror(rte_errno) : "",
51                                       rte_errno ? ")" : "");
52                                 continue;
53                         }
54                         if (fs_ethdev_portid_get(da->name, &pid) != 0) {
55                                 ERROR("sub_device %d init went wrong", i);
56                                 return -ENODEV;
57                         }
58                 } else {
59                         char devstr[DEVARGS_MAXLEN] = "";
60                         struct rte_devargs *probed_da =
61                                         rte_eth_devices[pid].device->devargs;
62
63                         /* Take control of device probed by EAL options. */
64                         free(da->args);
65                         memset(da, 0, sizeof(*da));
66                         if (probed_da != NULL)
67                                 snprintf(devstr, sizeof(devstr), "%s,%s",
68                                          probed_da->name, probed_da->args);
69                         else
70                                 snprintf(devstr, sizeof(devstr), "%s",
71                                          rte_eth_devices[pid].device->name);
72                         ret = rte_eal_devargs_parse(da, "%s", devstr);
73                         if (ret) {
74                                 ERROR("Probed devargs parsing failed with code"
75                                       " %d", ret);
76                                 return ret;
77                         }
78                         INFO("Taking control of a probed sub device"
79                               " %d named %s", i, da->name);
80                 }
81                 ret = rte_eth_dev_owner_set(pid, &PRIV(dev)->my_owner);
82                 if (ret < 0) {
83                         INFO("sub_device %d owner set failed (%s),"
84                              " will try again later", i, strerror(-ret));
85                         continue;
86                 } else if (strncmp(rte_eth_devices[pid].device->name, da->name,
87                            strlen(da->name)) != 0) {
88                         /*
89                          * The device probably was removed and its port id was
90                          * reallocated before ownership set.
91                          */
92                         rte_eth_dev_owner_unset(pid, PRIV(dev)->my_owner.id);
93                         INFO("sub_device %d was probably removed before taking"
94                              " ownership, will try again later", i);
95                         continue;
96                 }
97                 ETH(sdev) = &rte_eth_devices[pid];
98                 SUB_ID(sdev) = i;
99                 sdev->fs_dev = dev;
100                 sdev->dev = ETH(sdev)->device;
101                 ETH(sdev)->state = RTE_ETH_DEV_DEFERRED;
102                 sdev->state = DEV_PROBED;
103         }
104         return 0;
105 }
106
107 int
108 failsafe_eal_init(struct rte_eth_dev *dev)
109 {
110         int ret;
111
112         ret = fs_bus_init(dev);
113         if (ret)
114                 return ret;
115         if (PRIV(dev)->state < DEV_PROBED)
116                 PRIV(dev)->state = DEV_PROBED;
117         fs_switch_dev(dev, NULL);
118         return 0;
119 }
120
121 static int
122 fs_bus_uninit(struct rte_eth_dev *dev)
123 {
124         struct sub_device *sdev = NULL;
125         uint8_t i;
126         int sdev_ret;
127         int ret = 0;
128
129         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
130                 sdev_ret = rte_eal_hotplug_remove(sdev->bus->name,
131                                                         sdev->dev->name);
132                 if (sdev_ret) {
133                         ERROR("Failed to remove requested device %s (err: %d)",
134                               sdev->dev->name, sdev_ret);
135                         continue;
136                 }
137                 sdev->state = DEV_PROBED - 1;
138         }
139         return ret;
140 }
141
142 int
143 failsafe_eal_uninit(struct rte_eth_dev *dev)
144 {
145         int ret;
146
147         ret = fs_bus_uninit(dev);
148         PRIV(dev)->state = DEV_PROBED - 1;
149         return ret;
150 }