net/mlx5: fix secondary process verification
[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 static void
45 priv_txq_stop(struct priv *priv)
46 {
47         unsigned int i;
48
49         for (i = 0; i != priv->txqs_n; ++i)
50                 mlx5_priv_txq_release(priv, i);
51 }
52
53 static int
54 priv_txq_start(struct priv *priv)
55 {
56         unsigned int i;
57         int ret = 0;
58
59         /* Add memory regions to Tx queues. */
60         for (i = 0; i != priv->txqs_n; ++i) {
61                 unsigned int idx = 0;
62                 struct mlx5_mr *mr;
63                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_priv_txq_get(priv, i);
64
65                 if (!txq_ctrl)
66                         continue;
67                 LIST_FOREACH(mr, &priv->mr, next)
68                         priv_txq_mp2mr_reg(priv, &txq_ctrl->txq, mr->mp, idx++);
69                 txq_alloc_elts(txq_ctrl);
70                 txq_ctrl->ibv = mlx5_priv_txq_ibv_new(priv, i);
71                 if (!txq_ctrl->ibv) {
72                         ret = ENOMEM;
73                         goto error;
74                 }
75         }
76         return -ret;
77 error:
78         priv_txq_stop(priv);
79         return -ret;
80 }
81
82 static void
83 priv_rxq_stop(struct priv *priv)
84 {
85         unsigned int i;
86
87         for (i = 0; i != priv->rxqs_n; ++i)
88                 mlx5_priv_rxq_release(priv, i);
89 }
90
91 static int
92 priv_rxq_start(struct priv *priv)
93 {
94         unsigned int i;
95         int ret = 0;
96
97         for (i = 0; i != priv->rxqs_n; ++i) {
98                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_priv_rxq_get(priv, i);
99
100                 if (!rxq_ctrl)
101                         continue;
102                 ret = rxq_alloc_elts(rxq_ctrl);
103                 if (ret)
104                         goto error;
105                 rxq_ctrl->ibv = mlx5_priv_rxq_ibv_new(priv, i);
106                 if (!rxq_ctrl->ibv) {
107                         ret = ENOMEM;
108                         goto error;
109                 }
110         }
111         return -ret;
112 error:
113         priv_rxq_stop(priv);
114         return -ret;
115 }
116
117 /**
118  * DPDK callback to start the device.
119  *
120  * Simulate device start by attaching all configured flows.
121  *
122  * @param dev
123  *   Pointer to Ethernet device structure.
124  *
125  * @return
126  *   0 on success, negative errno value on failure.
127  */
128 int
129 mlx5_dev_start(struct rte_eth_dev *dev)
130 {
131         struct priv *priv = dev->data->dev_private;
132         struct mlx5_mr *mr = NULL;
133         int err;
134
135         dev->data->dev_started = 1;
136         priv_lock(priv);
137         err = priv_flow_create_drop_queue(priv);
138         if (err) {
139                 ERROR("%p: Drop queue allocation failed: %s",
140                       (void *)dev, strerror(err));
141                 goto error;
142         }
143         DEBUG("%p: allocating and configuring hash RX queues", (void *)dev);
144         rte_mempool_walk(mlx5_mp2mr_iter, priv);
145         err = priv_txq_start(priv);
146         if (err) {
147                 ERROR("%p: TXQ allocation failed: %s",
148                       (void *)dev, strerror(err));
149                 goto error;
150         }
151         /* Update send callback. */
152         priv_dev_select_tx_function(priv, dev);
153         err = priv_rxq_start(priv);
154         if (err) {
155                 ERROR("%p: RXQ allocation failed: %s",
156                       (void *)dev, strerror(err));
157                 goto error;
158         }
159         /* Update receive callback. */
160         priv_dev_select_rx_function(priv, dev);
161         err = priv_dev_traffic_enable(priv, dev);
162         if (err) {
163                 ERROR("%p: an error occurred while configuring control flows:"
164                       " %s",
165                       (void *)priv, strerror(err));
166                 goto error;
167         }
168         err = priv_flow_start(priv, &priv->flows);
169         if (err) {
170                 ERROR("%p: an error occurred while configuring flows:"
171                       " %s",
172                       (void *)priv, strerror(err));
173                 goto error;
174         }
175         err = priv_rx_intr_vec_enable(priv);
176         if (err) {
177                 ERROR("%p: RX interrupt vector creation failed",
178                       (void *)priv);
179                 goto error;
180         }
181         priv_dev_interrupt_handler_install(priv, dev);
182         priv_xstats_init(priv);
183         priv_unlock(priv);
184         return 0;
185 error:
186         /* Rollback. */
187         dev->data->dev_started = 0;
188         for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
189                 priv_mr_release(priv, mr);
190         priv_flow_stop(priv, &priv->flows);
191         priv_dev_traffic_disable(priv, dev);
192         priv_txq_stop(priv);
193         priv_rxq_stop(priv);
194         priv_flow_delete_drop_queue(priv);
195         priv_unlock(priv);
196         return -err;
197 }
198
199 /**
200  * DPDK callback to stop the device.
201  *
202  * Simulate device stop by detaching all configured flows.
203  *
204  * @param dev
205  *   Pointer to Ethernet device structure.
206  */
207 void
208 mlx5_dev_stop(struct rte_eth_dev *dev)
209 {
210         struct priv *priv = dev->data->dev_private;
211         struct mlx5_mr *mr;
212
213         priv_lock(priv);
214         dev->data->dev_started = 0;
215         /* Prevent crashes when queues are still in use. */
216         dev->rx_pkt_burst = removed_rx_burst;
217         dev->tx_pkt_burst = removed_tx_burst;
218         rte_wmb();
219         usleep(1000 * priv->rxqs_n);
220         DEBUG("%p: cleaning up and destroying hash RX queues", (void *)dev);
221         priv_flow_stop(priv, &priv->flows);
222         priv_dev_traffic_disable(priv, dev);
223         priv_rx_intr_vec_disable(priv);
224         priv_dev_interrupt_handler_uninstall(priv, dev);
225         priv_txq_stop(priv);
226         priv_rxq_stop(priv);
227         for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
228                 priv_mr_release(priv, mr);
229         priv_flow_delete_drop_queue(priv);
230         priv_unlock(priv);
231 }
232
233 /**
234  * Enable traffic flows configured by control plane
235  *
236  * @param priv
237  *   Pointer to Ethernet device private data.
238  * @param dev
239  *   Pointer to Ethernet device structure.
240  *
241  * @return
242  *   0 on success.
243  */
244 int
245 priv_dev_traffic_enable(struct priv *priv, struct rte_eth_dev *dev)
246 {
247         struct rte_flow_item_eth bcast = {
248                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
249         };
250         struct rte_flow_item_eth ipv6_multi_spec = {
251                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
252         };
253         struct rte_flow_item_eth ipv6_multi_mask = {
254                 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
255         };
256         struct rte_flow_item_eth unicast = {
257                 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
258         };
259         struct rte_flow_item_eth unicast_mask = {
260                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
261         };
262         const unsigned int vlan_filter_n = priv->vlan_filter_n;
263         const struct ether_addr cmp = {
264                 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
265         };
266         unsigned int i;
267         unsigned int j;
268         int ret;
269
270         if (priv->isolated)
271                 return 0;
272         if (dev->data->promiscuous) {
273                 struct rte_flow_item_eth promisc = {
274                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
275                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
276                         .type = 0,
277                 };
278
279                 claim_zero(mlx5_ctrl_flow(dev, &promisc, &promisc));
280                 return 0;
281         }
282         if (dev->data->all_multicast) {
283                 struct rte_flow_item_eth multicast = {
284                         .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
285                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
286                         .type = 0,
287                 };
288
289                 claim_zero(mlx5_ctrl_flow(dev, &multicast, &multicast));
290         } else {
291                 /* Add broadcast/multicast flows. */
292                 for (i = 0; i != vlan_filter_n; ++i) {
293                         uint16_t vlan = priv->vlan_filter[i];
294
295                         struct rte_flow_item_vlan vlan_spec = {
296                                 .tci = rte_cpu_to_be_16(vlan),
297                         };
298                         struct rte_flow_item_vlan vlan_mask = {
299                                 .tci = 0xffff,
300                         };
301
302                         ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
303                                                   &vlan_spec, &vlan_mask);
304                         if (ret)
305                                 goto error;
306                         ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
307                                                   &ipv6_multi_mask,
308                                                   &vlan_spec, &vlan_mask);
309                         if (ret)
310                                 goto error;
311                 }
312                 if (!vlan_filter_n) {
313                         ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
314                         if (ret)
315                                 goto error;
316                         ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
317                                              &ipv6_multi_mask);
318                         if (ret)
319                                 goto error;
320                 }
321         }
322         /* Add MAC address flows. */
323         for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
324                 struct ether_addr *mac = &dev->data->mac_addrs[i];
325
326                 if (!memcmp(mac, &cmp, sizeof(*mac)))
327                         continue;
328                 memcpy(&unicast.dst.addr_bytes,
329                        mac->addr_bytes,
330                        ETHER_ADDR_LEN);
331                 for (j = 0; j != vlan_filter_n; ++j) {
332                         uint16_t vlan = priv->vlan_filter[j];
333
334                         struct rte_flow_item_vlan vlan_spec = {
335                                 .tci = rte_cpu_to_be_16(vlan),
336                         };
337                         struct rte_flow_item_vlan vlan_mask = {
338                                 .tci = 0xffff,
339                         };
340
341                         ret = mlx5_ctrl_flow_vlan(dev, &unicast,
342                                                   &unicast_mask,
343                                                   &vlan_spec,
344                                                   &vlan_mask);
345                         if (ret)
346                                 goto error;
347                 }
348                 if (!vlan_filter_n) {
349                         ret = mlx5_ctrl_flow(dev, &unicast,
350                                              &unicast_mask);
351                         if (ret)
352                                 goto error;
353                 }
354         }
355         return 0;
356 error:
357         return rte_errno;
358 }
359
360
361 /**
362  * Disable traffic flows configured by control plane
363  *
364  * @param priv
365  *   Pointer to Ethernet device private data.
366  * @param dev
367  *   Pointer to Ethernet device structure.
368  *
369  * @return
370  *   0 on success.
371  */
372 int
373 priv_dev_traffic_disable(struct priv *priv, struct rte_eth_dev *dev)
374 {
375         (void)dev;
376         priv_flow_flush(priv, &priv->ctrl_flows);
377         return 0;
378 }
379
380 /**
381  * Restart traffic flows configured by control plane
382  *
383  * @param priv
384  *   Pointer to Ethernet device private data.
385  * @param dev
386  *   Pointer to Ethernet device structure.
387  *
388  * @return
389  *   0 on success.
390  */
391 int
392 priv_dev_traffic_restart(struct priv *priv, struct rte_eth_dev *dev)
393 {
394         if (dev->data->dev_started) {
395                 priv_dev_traffic_disable(priv, dev);
396                 priv_dev_traffic_enable(priv, dev);
397         }
398         return 0;
399 }
400
401 /**
402  * Restart traffic flows configured by control plane
403  *
404  * @param dev
405  *   Pointer to Ethernet device structure.
406  *
407  * @return
408  *   0 on success.
409  */
410 int
411 mlx5_traffic_restart(struct rte_eth_dev *dev)
412 {
413         struct priv *priv = dev->data->dev_private;
414
415         priv_lock(priv);
416         priv_dev_traffic_restart(priv, dev);
417         priv_unlock(priv);
418         return 0;
419 }