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