1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox.
7 #include <rte_malloc.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_ethdev_vdev.h>
10 #include <rte_devargs.h>
11 #include <rte_kvargs.h>
12 #include <rte_bus_vdev.h>
14 #include "failsafe_private.h"
16 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
17 static const struct rte_eth_link eth_link = {
18 .link_speed = ETH_SPEED_NUM_10G,
19 .link_duplex = ETH_LINK_FULL_DUPLEX,
20 .link_status = ETH_LINK_UP,
21 .link_autoneg = ETH_LINK_AUTONEG,
25 fs_sub_device_alloc(struct rte_eth_dev *dev,
32 ret = failsafe_args_count_subdevice(dev, params);
35 if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
36 ERROR("Cannot allocate more than %d ports",
37 FAILSAFE_MAX_ETHPORTS);
40 nb_subs = PRIV(dev)->subs_tail;
41 PRIV(dev)->subs = rte_zmalloc(NULL,
42 sizeof(struct sub_device) * nb_subs,
44 if (PRIV(dev)->subs == NULL) {
45 ERROR("Could not allocate sub_devices");
48 /* Initiate static sub devices linked list. */
49 for (i = 1; i < nb_subs; i++)
50 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i;
51 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs;
56 fs_sub_device_free(struct rte_eth_dev *dev)
58 rte_free(PRIV(dev)->subs);
61 static void fs_hotplug_alarm(void *arg);
64 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
70 if (PRIV(dev)->pending_alarm)
72 ret = rte_eal_alarm_set(hotplug_poll * 1000,
76 ERROR("Could not set up plug-in event detection");
79 PRIV(dev)->pending_alarm = 1;
84 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
88 if (PRIV(dev)->pending_alarm) {
90 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
92 ERROR("rte_eal_alarm_cancel failed (errno: %s)",
96 PRIV(dev)->pending_alarm = 0;
103 fs_hotplug_alarm(void *arg)
105 struct rte_eth_dev *dev = arg;
106 struct sub_device *sdev;
110 if (!PRIV(dev)->pending_alarm)
112 PRIV(dev)->pending_alarm = 0;
113 FOREACH_SUBDEV(sdev, i, dev)
114 if (sdev->state != PRIV(dev)->state)
116 /* if we have non-probed device */
117 if (i != PRIV(dev)->subs_tail) {
118 ret = failsafe_eth_dev_state_sync(dev);
120 ERROR("Unable to synchronize sub_device state");
122 failsafe_dev_remove(dev);
123 ret = failsafe_hotplug_alarm_install(dev);
125 ERROR("Unable to set up next alarm");
129 fs_eth_dev_create(struct rte_vdev_device *vdev)
131 struct rte_eth_dev *dev;
132 struct ether_addr *mac;
133 struct fs_priv *priv;
134 struct sub_device *sdev;
136 unsigned int socket_id;
142 socket_id = rte_socket_id();
143 INFO("Creating fail-safe device on NUMA socket %u", socket_id);
144 params = rte_vdev_device_args(vdev);
145 if (params == NULL) {
146 ERROR("This PMD requires sub-devices, none provided");
149 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
151 ERROR("Unable to allocate rte_eth_dev");
156 dev->dev_ops = &failsafe_ops;
157 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
158 dev->data->dev_link = eth_link;
159 PRIV(dev)->nb_mac_addr = 1;
160 TAILQ_INIT(&PRIV(dev)->flow_list);
161 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
162 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
163 ret = fs_sub_device_alloc(dev, params);
165 ERROR("Could not allocate sub_devices");
168 ret = failsafe_args_parse(dev, params);
171 ret = rte_eth_dev_owner_new(&priv->my_owner.id);
173 ERROR("Failed to get unique owner identifier");
176 snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
177 FAILSAFE_OWNER_NAME);
178 ret = failsafe_eal_init(dev);
181 ret = failsafe_hotplug_alarm_install(dev);
183 ERROR("Could not set up plug-in event detection");
186 mac = &dev->data->mac_addrs[0];
189 * If MAC address was provided as a parameter,
190 * apply to all probed slaves.
192 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
193 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
196 ERROR("Failed to set default MAC address");
202 * Use the ether_addr from first probed
203 * device, either preferred or fallback.
205 FOREACH_SUBDEV(sdev, i, dev)
206 if (sdev->state >= DEV_PROBED) {
207 ether_addr_copy(Ð(sdev)->data->mac_addrs[0],
212 * If no device has been probed and no ether_addr
213 * has been provided on the command line, use a random
215 * It will be applied during future slave state syncs to
218 if (i == priv->subs_tail)
219 eth_random_addr(&mac->addr_bytes[0]);
221 INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
222 mac->addr_bytes[0], mac->addr_bytes[1],
223 mac->addr_bytes[2], mac->addr_bytes[3],
224 mac->addr_bytes[4], mac->addr_bytes[5]);
225 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
226 PRIV(dev)->intr_handle = (struct rte_intr_handle){
228 .type = RTE_INTR_HANDLE_EXT,
232 failsafe_args_free(dev);
234 fs_sub_device_free(dev);
237 rte_eth_dev_release_port(dev);
242 fs_rte_eth_free(const char *name)
244 struct rte_eth_dev *dev;
247 dev = rte_eth_dev_allocated(name);
250 ret = failsafe_eal_uninit(dev);
252 ERROR("Error while uninitializing sub-EAL");
253 failsafe_args_free(dev);
254 fs_sub_device_free(dev);
256 rte_eth_dev_release_port(dev);
261 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
265 name = rte_vdev_device_name(vdev);
266 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
268 return fs_eth_dev_create(vdev);
272 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
276 name = rte_vdev_device_name(vdev);
277 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
278 return fs_rte_eth_free(name);
281 static struct rte_vdev_driver failsafe_drv = {
282 .probe = rte_pmd_failsafe_probe,
283 .remove = rte_pmd_failsafe_remove,
286 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
287 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);