New upstream version 17.11.5
[deb_dpdk.git] / drivers / net / mlx5 / mlx5_trigger.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <unistd.h>
34
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_interrupts.h>
38 #include <rte_alarm.h>
39
40 #include "mlx5.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
43
44 /**
45  * Stop traffic on Tx queues.
46  *
47  * @param dev
48  *   Pointer to Ethernet device structure.
49  */
50 static void
51 mlx5_txq_stop(struct rte_eth_dev *dev)
52 {
53         struct priv *priv = dev->data->dev_private;
54         unsigned int i;
55
56         for (i = 0; i != priv->txqs_n; ++i)
57                 mlx5_txq_release(dev, i);
58 }
59
60 /**
61  * Start traffic on Tx queues.
62  *
63  * @param dev
64  *   Pointer to Ethernet device structure.
65  *
66  * @return
67  *   0 on success, a negative errno value otherwise and rte_errno is set.
68  */
69 static int
70 mlx5_txq_start(struct rte_eth_dev *dev)
71 {
72         struct priv *priv = dev->data->dev_private;
73         unsigned int i;
74         int ret;
75
76         for (i = 0; i != priv->txqs_n; ++i) {
77                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
78
79                 if (!txq_ctrl)
80                         continue;
81                 txq_alloc_elts(txq_ctrl);
82                 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
83                 if (!txq_ctrl->ibv) {
84                         rte_errno = ENOMEM;
85                         goto error;
86                 }
87         }
88         ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd);
89         if (ret) {
90                 /* Adjust index for rollback. */
91                 i = priv->txqs_n - 1;
92                 goto error;
93         }
94         return 0;
95 error:
96         ret = rte_errno; /* Save rte_errno before cleanup. */
97         do {
98                 mlx5_txq_release(dev, i);
99         } while (i-- != 0);
100         rte_errno = ret; /* Restore rte_errno. */
101         return -rte_errno;
102 }
103
104 /**
105  * Stop traffic on Rx queues.
106  *
107  * @param dev
108  *   Pointer to Ethernet device structure.
109  */
110 static void
111 mlx5_rxq_stop(struct rte_eth_dev *dev)
112 {
113         struct priv *priv = dev->data->dev_private;
114         unsigned int i;
115
116         for (i = 0; i != priv->rxqs_n; ++i)
117                 mlx5_rxq_release(dev, i);
118 }
119
120 /**
121  * Start traffic on Rx queues.
122  *
123  * @param dev
124  *   Pointer to Ethernet device structure.
125  *
126  * @return
127  *   0 on success, a negative errno value otherwise and rte_errno is set.
128  */
129 static int
130 mlx5_rxq_start(struct rte_eth_dev *dev)
131 {
132         struct priv *priv = dev->data->dev_private;
133         unsigned int i;
134         int ret = 0;
135
136         for (i = 0; i != priv->rxqs_n; ++i) {
137                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
138
139                 if (!rxq_ctrl)
140                         continue;
141                 ret = rxq_alloc_elts(rxq_ctrl);
142                 if (ret)
143                         goto error;
144                 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
145                 if (!rxq_ctrl->ibv)
146                         goto error;
147         }
148         return 0;
149 error:
150         ret = rte_errno; /* Save rte_errno before cleanup. */
151         do {
152                 mlx5_rxq_release(dev, i);
153         } while (i-- != 0);
154         rte_errno = ret; /* Restore rte_errno. */
155         return -rte_errno;
156 }
157
158 /**
159  * DPDK callback to start the device.
160  *
161  * Simulate device start by attaching all configured flows.
162  *
163  * @param dev
164  *   Pointer to Ethernet device structure.
165  *
166  * @return
167  *   0 on success, a negative errno value otherwise and rte_errno is set.
168  */
169 int
170 mlx5_dev_start(struct rte_eth_dev *dev)
171 {
172         struct priv *priv = dev->data->dev_private;
173         int ret;
174
175         DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
176         ret = mlx5_flow_create_drop_queue(dev);
177         if (ret) {
178                 DRV_LOG(ERR, "port %u drop queue allocation failed: %s",
179                         dev->data->port_id, strerror(rte_errno));
180                 goto error;
181         }
182         ret = mlx5_txq_start(dev);
183         if (ret) {
184                 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
185                         dev->data->port_id, strerror(rte_errno));
186                 return -rte_errno;
187         }
188         ret = mlx5_rxq_start(dev);
189         if (ret) {
190                 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
191                         dev->data->port_id, strerror(rte_errno));
192                 mlx5_txq_stop(dev);
193                 return -rte_errno;
194         }
195         dev->data->dev_started = 1;
196         ret = mlx5_rx_intr_vec_enable(dev);
197         if (ret) {
198                 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
199                         dev->data->port_id);
200                 goto error;
201         }
202         mlx5_xstats_init(dev);
203         ret = mlx5_traffic_enable(dev);
204         if (ret) {
205                 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
206                         dev->data->port_id);
207                 goto error;
208         }
209         ret = mlx5_flow_start(dev, &priv->flows);
210         if (ret) {
211                 DRV_LOG(DEBUG, "port %u failed to set flows",
212                         dev->data->port_id);
213                 goto error;
214         }
215         dev->tx_pkt_burst = mlx5_select_tx_function(dev);
216         dev->rx_pkt_burst = mlx5_select_rx_function(dev);
217         mlx5_dev_interrupt_handler_install(dev);
218         return 0;
219 error:
220         ret = rte_errno; /* Save rte_errno before cleanup. */
221         /* Rollback. */
222         dev->data->dev_started = 0;
223         mlx5_flow_stop(dev, &priv->flows);
224         mlx5_traffic_disable(dev);
225         mlx5_txq_stop(dev);
226         mlx5_rxq_stop(dev);
227         mlx5_flow_delete_drop_queue(dev);
228         rte_errno = ret; /* Restore rte_errno. */
229         return -rte_errno;
230 }
231
232 /**
233  * DPDK callback to stop the device.
234  *
235  * Simulate device stop by detaching all configured flows.
236  *
237  * @param dev
238  *   Pointer to Ethernet device structure.
239  */
240 void
241 mlx5_dev_stop(struct rte_eth_dev *dev)
242 {
243         struct priv *priv = dev->data->dev_private;
244
245         dev->data->dev_started = 0;
246         /* Prevent crashes when queues are still in use. */
247         dev->rx_pkt_burst = removed_rx_burst;
248         dev->tx_pkt_burst = removed_tx_burst;
249         rte_wmb();
250         usleep(1000 * priv->rxqs_n);
251         DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
252         mlx5_flow_stop(dev, &priv->flows);
253         mlx5_traffic_disable(dev);
254         mlx5_rx_intr_vec_disable(dev);
255         mlx5_dev_interrupt_handler_uninstall(dev);
256         mlx5_txq_stop(dev);
257         mlx5_rxq_stop(dev);
258         mlx5_flow_delete_drop_queue(dev);
259 }
260
261 /**
262  * Enable traffic flows configured by control plane
263  *
264  * @param dev
265  *   Pointer to Ethernet device private data.
266  * @param dev
267  *   Pointer to Ethernet device structure.
268  *
269  * @return
270  *   0 on success, a negative errno value otherwise and rte_errno is set.
271  */
272 int
273 mlx5_traffic_enable(struct rte_eth_dev *dev)
274 {
275         struct priv *priv = dev->data->dev_private;
276         struct rte_flow_item_eth bcast = {
277                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
278         };
279         struct rte_flow_item_eth ipv6_multi_spec = {
280                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
281         };
282         struct rte_flow_item_eth ipv6_multi_mask = {
283                 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
284         };
285         struct rte_flow_item_eth unicast = {
286                 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
287         };
288         struct rte_flow_item_eth unicast_mask = {
289                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
290         };
291         const unsigned int vlan_filter_n = priv->vlan_filter_n;
292         const struct ether_addr cmp = {
293                 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
294         };
295         unsigned int i;
296         unsigned int j;
297         int ret;
298
299         if (priv->isolated)
300                 return 0;
301         if (dev->data->promiscuous) {
302                 struct rte_flow_item_eth promisc = {
303                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
304                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
305                         .type = 0,
306                 };
307
308                 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
309                 if (ret)
310                         goto error;
311         }
312         if (dev->data->all_multicast) {
313                 struct rte_flow_item_eth multicast = {
314                         .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
315                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
316                         .type = 0,
317                 };
318
319                 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
320                 if (ret)
321                         goto error;
322         } else {
323                 /* Add broadcast/multicast flows. */
324                 for (i = 0; i != vlan_filter_n; ++i) {
325                         uint16_t vlan = priv->vlan_filter[i];
326
327                         struct rte_flow_item_vlan vlan_spec = {
328                                 .tci = rte_cpu_to_be_16(vlan),
329                         };
330                         struct rte_flow_item_vlan vlan_mask =
331                                 rte_flow_item_vlan_mask;
332
333                         ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
334                                                   &vlan_spec, &vlan_mask);
335                         if (ret)
336                                 goto error;
337                         ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
338                                                   &ipv6_multi_mask,
339                                                   &vlan_spec, &vlan_mask);
340                         if (ret)
341                                 goto error;
342                 }
343                 if (!vlan_filter_n) {
344                         ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
345                         if (ret)
346                                 goto error;
347                         ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
348                                              &ipv6_multi_mask);
349                         if (ret)
350                                 goto error;
351                 }
352         }
353         /* Add MAC address flows. */
354         for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
355                 struct ether_addr *mac = &dev->data->mac_addrs[i];
356
357                 if (!memcmp(mac, &cmp, sizeof(*mac)))
358                         continue;
359                 memcpy(&unicast.dst.addr_bytes,
360                        mac->addr_bytes,
361                        ETHER_ADDR_LEN);
362                 for (j = 0; j != vlan_filter_n; ++j) {
363                         uint16_t vlan = priv->vlan_filter[j];
364
365                         struct rte_flow_item_vlan vlan_spec = {
366                                 .tci = rte_cpu_to_be_16(vlan),
367                         };
368                         struct rte_flow_item_vlan vlan_mask =
369                                 rte_flow_item_vlan_mask;
370
371                         ret = mlx5_ctrl_flow_vlan(dev, &unicast,
372                                                   &unicast_mask,
373                                                   &vlan_spec,
374                                                   &vlan_mask);
375                         if (ret)
376                                 goto error;
377                 }
378                 if (!vlan_filter_n) {
379                         ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
380                         if (ret)
381                                 goto error;
382                 }
383         }
384         return 0;
385 error:
386         ret = rte_errno; /* Save rte_errno before cleanup. */
387         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
388         rte_errno = ret; /* Restore rte_errno. */
389         return -rte_errno;
390 }
391
392
393 /**
394  * Disable traffic flows configured by control plane
395  *
396  * @param dev
397  *   Pointer to Ethernet device private data.
398  */
399 void
400 mlx5_traffic_disable(struct rte_eth_dev *dev)
401 {
402         struct priv *priv = dev->data->dev_private;
403
404         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
405 }
406
407 /**
408  * Restart traffic flows configured by control plane
409  *
410  * @param dev
411  *   Pointer to Ethernet device private data.
412  *
413  * @return
414  *   0 on success, a negative errno value otherwise and rte_errno is set.
415  */
416 int
417 mlx5_traffic_restart(struct rte_eth_dev *dev)
418 {
419         if (dev->data->dev_started) {
420                 mlx5_traffic_disable(dev);
421                 return mlx5_traffic_enable(dev);
422         }
423         return 0;
424 }