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