New upstream version 17.11.1
[deb_dpdk.git] / drivers / net / failsafe / failsafe.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_alarm.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_ethdev_vdev.h>
38 #include <rte_devargs.h>
39 #include <rte_kvargs.h>
40 #include <rte_bus_vdev.h>
41
42 #include "failsafe_private.h"
43
44 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
45 static const struct rte_eth_link eth_link = {
46         .link_speed = ETH_SPEED_NUM_10G,
47         .link_duplex = ETH_LINK_FULL_DUPLEX,
48         .link_status = ETH_LINK_UP,
49         .link_autoneg = ETH_LINK_AUTONEG,
50 };
51
52 static int
53 fs_sub_device_alloc(struct rte_eth_dev *dev,
54                 const char *params)
55 {
56         uint8_t nb_subs;
57         int ret;
58
59         ret = failsafe_args_count_subdevice(dev, params);
60         if (ret)
61                 return ret;
62         if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
63                 ERROR("Cannot allocate more than %d ports",
64                         FAILSAFE_MAX_ETHPORTS);
65                 return -ENOSPC;
66         }
67         nb_subs = PRIV(dev)->subs_tail;
68         PRIV(dev)->subs = rte_zmalloc(NULL,
69                         sizeof(struct sub_device) * nb_subs,
70                         RTE_CACHE_LINE_SIZE);
71         if (PRIV(dev)->subs == NULL) {
72                 ERROR("Could not allocate sub_devices");
73                 return -ENOMEM;
74         }
75         return 0;
76 }
77
78 static void
79 fs_sub_device_free(struct rte_eth_dev *dev)
80 {
81         rte_free(PRIV(dev)->subs);
82 }
83
84 static void fs_hotplug_alarm(void *arg);
85
86 int
87 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
88 {
89         int ret;
90
91         if (dev == NULL)
92                 return -EINVAL;
93         if (PRIV(dev)->pending_alarm)
94                 return 0;
95         ret = rte_eal_alarm_set(hotplug_poll * 1000,
96                                 fs_hotplug_alarm,
97                                 dev);
98         if (ret) {
99                 ERROR("Could not set up plug-in event detection");
100                 return ret;
101         }
102         PRIV(dev)->pending_alarm = 1;
103         return 0;
104 }
105
106 int
107 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
108 {
109         int ret = 0;
110
111         if (PRIV(dev)->pending_alarm) {
112                 rte_errno = 0;
113                 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
114                 if (rte_errno) {
115                         ERROR("rte_eal_alarm_cancel failed (errno: %s)",
116                               strerror(rte_errno));
117                         ret = -rte_errno;
118                 } else {
119                         PRIV(dev)->pending_alarm = 0;
120                 }
121         }
122         return ret;
123 }
124
125 static void
126 fs_hotplug_alarm(void *arg)
127 {
128         struct rte_eth_dev *dev = arg;
129         struct sub_device *sdev;
130         int ret;
131         uint8_t i;
132
133         if (!PRIV(dev)->pending_alarm)
134                 return;
135         PRIV(dev)->pending_alarm = 0;
136         FOREACH_SUBDEV(sdev, i, dev)
137                 if (sdev->state != PRIV(dev)->state)
138                         break;
139         /* if we have non-probed device */
140         if (i != PRIV(dev)->subs_tail) {
141                 ret = failsafe_eth_dev_state_sync(dev);
142                 if (ret)
143                         ERROR("Unable to synchronize sub_device state");
144         }
145         failsafe_dev_remove(dev);
146         ret = failsafe_hotplug_alarm_install(dev);
147         if (ret)
148                 ERROR("Unable to set up next alarm");
149 }
150
151 static int
152 fs_eth_dev_create(struct rte_vdev_device *vdev)
153 {
154         struct rte_eth_dev *dev;
155         struct ether_addr *mac;
156         struct fs_priv *priv;
157         struct sub_device *sdev;
158         const char *params;
159         unsigned int socket_id;
160         uint8_t i;
161         int ret;
162
163         dev = NULL;
164         priv = NULL;
165         socket_id = rte_socket_id();
166         INFO("Creating fail-safe device on NUMA socket %u", socket_id);
167         params = rte_vdev_device_args(vdev);
168         if (params == NULL) {
169                 ERROR("This PMD requires sub-devices, none provided");
170                 return -1;
171         }
172         dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
173         if (dev == NULL) {
174                 ERROR("Unable to allocate rte_eth_dev");
175                 return -1;
176         }
177         priv = PRIV(dev);
178         priv->dev = dev;
179         dev->dev_ops = &failsafe_ops;
180         dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
181         dev->data->dev_link = eth_link;
182         PRIV(dev)->nb_mac_addr = 1;
183         TAILQ_INIT(&PRIV(dev)->flow_list);
184         dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
185         dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
186         ret = fs_sub_device_alloc(dev, params);
187         if (ret) {
188                 ERROR("Could not allocate sub_devices");
189                 goto free_dev;
190         }
191         ret = failsafe_args_parse(dev, params);
192         if (ret)
193                 goto free_subs;
194         ret = failsafe_eal_init(dev);
195         if (ret)
196                 goto free_args;
197         ret = failsafe_hotplug_alarm_install(dev);
198         if (ret) {
199                 ERROR("Could not set up plug-in event detection");
200                 goto free_args;
201         }
202         mac = &dev->data->mac_addrs[0];
203         if (mac_from_arg) {
204                 /*
205                  * If MAC address was provided as a parameter,
206                  * apply to all probed slaves.
207                  */
208                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
209                         ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
210                                                                mac);
211                         if (ret) {
212                                 ERROR("Failed to set default MAC address");
213                                 goto free_args;
214                         }
215                 }
216         } else {
217                 /*
218                  * Use the ether_addr from first probed
219                  * device, either preferred or fallback.
220                  */
221                 FOREACH_SUBDEV(sdev, i, dev)
222                         if (sdev->state >= DEV_PROBED) {
223                                 ether_addr_copy(&ETH(sdev)->data->mac_addrs[0],
224                                                 mac);
225                                 break;
226                         }
227                 /*
228                  * If no device has been probed and no ether_addr
229                  * has been provided on the command line, use a random
230                  * valid one.
231                  * It will be applied during future slave state syncs to
232                  * probed slaves.
233                  */
234                 if (i == priv->subs_tail)
235                         eth_random_addr(&mac->addr_bytes[0]);
236         }
237         INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
238                 mac->addr_bytes[0], mac->addr_bytes[1],
239                 mac->addr_bytes[2], mac->addr_bytes[3],
240                 mac->addr_bytes[4], mac->addr_bytes[5]);
241         dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
242         return 0;
243 free_args:
244         failsafe_args_free(dev);
245 free_subs:
246         fs_sub_device_free(dev);
247 free_dev:
248         rte_free(PRIV(dev));
249         rte_eth_dev_release_port(dev);
250         return -1;
251 }
252
253 static int
254 fs_rte_eth_free(const char *name)
255 {
256         struct rte_eth_dev *dev;
257         int ret;
258
259         dev = rte_eth_dev_allocated(name);
260         if (dev == NULL)
261                 return -ENODEV;
262         ret = failsafe_eal_uninit(dev);
263         if (ret)
264                 ERROR("Error while uninitializing sub-EAL");
265         failsafe_args_free(dev);
266         fs_sub_device_free(dev);
267         rte_free(PRIV(dev));
268         rte_eth_dev_release_port(dev);
269         return ret;
270 }
271
272 static int
273 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
274 {
275         const char *name;
276
277         name = rte_vdev_device_name(vdev);
278         INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
279                         name);
280         return fs_eth_dev_create(vdev);
281 }
282
283 static int
284 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
285 {
286         const char *name;
287
288         name = rte_vdev_device_name(vdev);
289         INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
290         return fs_rte_eth_free(name);
291 }
292
293 static struct rte_vdev_driver failsafe_drv = {
294         .probe = rte_pmd_failsafe_probe,
295         .remove = rte_pmd_failsafe_remove,
296 };
297
298 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
299 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);