net/mlx5: use SPDX tags on 6WIND copyrighted files
[dpdk.git] / drivers / net / mlx5 / mlx5_trigger.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5
6 #include <unistd.h>
7
8 #include <rte_ether.h>
9 #include <rte_ethdev_driver.h>
10 #include <rte_interrupts.h>
11 #include <rte_alarm.h>
12
13 #include "mlx5.h"
14 #include "mlx5_rxtx.h"
15 #include "mlx5_utils.h"
16
17 static void
18 priv_txq_stop(struct priv *priv)
19 {
20         unsigned int i;
21
22         for (i = 0; i != priv->txqs_n; ++i)
23                 mlx5_priv_txq_release(priv, i);
24 }
25
26 static int
27 priv_txq_start(struct priv *priv)
28 {
29         unsigned int i;
30         int ret = 0;
31
32         /* Add memory regions to Tx queues. */
33         for (i = 0; i != priv->txqs_n; ++i) {
34                 unsigned int idx = 0;
35                 struct mlx5_mr *mr;
36                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_priv_txq_get(priv, i);
37
38                 if (!txq_ctrl)
39                         continue;
40                 LIST_FOREACH(mr, &priv->mr, next) {
41                         priv_txq_mp2mr_reg(priv, &txq_ctrl->txq, mr->mp, idx++);
42                         if (idx == MLX5_PMD_TX_MP_CACHE)
43                                 break;
44                 }
45                 txq_alloc_elts(txq_ctrl);
46                 txq_ctrl->ibv = mlx5_priv_txq_ibv_new(priv, i);
47                 if (!txq_ctrl->ibv) {
48                         ret = ENOMEM;
49                         goto error;
50                 }
51         }
52         return -ret;
53 error:
54         priv_txq_stop(priv);
55         return -ret;
56 }
57
58 static void
59 priv_rxq_stop(struct priv *priv)
60 {
61         unsigned int i;
62
63         for (i = 0; i != priv->rxqs_n; ++i)
64                 mlx5_priv_rxq_release(priv, i);
65 }
66
67 static int
68 priv_rxq_start(struct priv *priv)
69 {
70         unsigned int i;
71         int ret = 0;
72
73         for (i = 0; i != priv->rxqs_n; ++i) {
74                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_priv_rxq_get(priv, i);
75
76                 if (!rxq_ctrl)
77                         continue;
78                 ret = rxq_alloc_elts(rxq_ctrl);
79                 if (ret)
80                         goto error;
81                 rxq_ctrl->ibv = mlx5_priv_rxq_ibv_new(priv, i);
82                 if (!rxq_ctrl->ibv) {
83                         ret = ENOMEM;
84                         goto error;
85                 }
86         }
87         return -ret;
88 error:
89         priv_rxq_stop(priv);
90         return -ret;
91 }
92
93 /**
94  * DPDK callback to start the device.
95  *
96  * Simulate device start by attaching all configured flows.
97  *
98  * @param dev
99  *   Pointer to Ethernet device structure.
100  *
101  * @return
102  *   0 on success, negative errno value on failure.
103  */
104 int
105 mlx5_dev_start(struct rte_eth_dev *dev)
106 {
107         struct priv *priv = dev->data->dev_private;
108         struct mlx5_mr *mr = NULL;
109         int err;
110
111         dev->data->dev_started = 1;
112         priv_lock(priv);
113         err = priv_flow_create_drop_queue(priv);
114         if (err) {
115                 ERROR("%p: Drop queue allocation failed: %s",
116                       (void *)dev, strerror(err));
117                 goto error;
118         }
119         DEBUG("%p: allocating and configuring hash RX queues", (void *)dev);
120         rte_mempool_walk(mlx5_mp2mr_iter, priv);
121         err = priv_txq_start(priv);
122         if (err) {
123                 ERROR("%p: TXQ allocation failed: %s",
124                       (void *)dev, strerror(err));
125                 goto error;
126         }
127         err = priv_rxq_start(priv);
128         if (err) {
129                 ERROR("%p: RXQ allocation failed: %s",
130                       (void *)dev, strerror(err));
131                 goto error;
132         }
133         err = priv_rx_intr_vec_enable(priv);
134         if (err) {
135                 ERROR("%p: RX interrupt vector creation failed",
136                       (void *)priv);
137                 goto error;
138         }
139         priv_xstats_init(priv);
140         /* Update link status and Tx/Rx callbacks for the first time. */
141         memset(&dev->data->dev_link, 0, sizeof(struct rte_eth_link));
142         priv_link_update(priv, 1);
143         priv_dev_interrupt_handler_install(priv, dev);
144         priv_unlock(priv);
145         return 0;
146 error:
147         /* Rollback. */
148         dev->data->dev_started = 0;
149         for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
150                 priv_mr_release(priv, mr);
151         priv_flow_stop(priv, &priv->flows);
152         priv_dev_traffic_disable(priv, dev);
153         priv_txq_stop(priv);
154         priv_rxq_stop(priv);
155         priv_flow_delete_drop_queue(priv);
156         priv_unlock(priv);
157         return -err;
158 }
159
160 /**
161  * DPDK callback to stop the device.
162  *
163  * Simulate device stop by detaching all configured flows.
164  *
165  * @param dev
166  *   Pointer to Ethernet device structure.
167  */
168 void
169 mlx5_dev_stop(struct rte_eth_dev *dev)
170 {
171         struct priv *priv = dev->data->dev_private;
172         struct mlx5_mr *mr;
173
174         priv_lock(priv);
175         dev->data->dev_started = 0;
176         /* Prevent crashes when queues are still in use. */
177         dev->rx_pkt_burst = removed_rx_burst;
178         dev->tx_pkt_burst = removed_tx_burst;
179         rte_wmb();
180         usleep(1000 * priv->rxqs_n);
181         DEBUG("%p: cleaning up and destroying hash RX queues", (void *)dev);
182         priv_flow_stop(priv, &priv->flows);
183         priv_dev_traffic_disable(priv, dev);
184         priv_rx_intr_vec_disable(priv);
185         priv_dev_interrupt_handler_uninstall(priv, dev);
186         priv_txq_stop(priv);
187         priv_rxq_stop(priv);
188         for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
189                 priv_mr_release(priv, mr);
190         priv_flow_delete_drop_queue(priv);
191         priv_unlock(priv);
192 }
193
194 /**
195  * Enable traffic flows configured by control plane
196  *
197  * @param priv
198  *   Pointer to Ethernet device private data.
199  * @param dev
200  *   Pointer to Ethernet device structure.
201  *
202  * @return
203  *   0 on success.
204  */
205 int
206 priv_dev_traffic_enable(struct priv *priv, struct rte_eth_dev *dev)
207 {
208         struct rte_flow_item_eth bcast = {
209                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
210         };
211         struct rte_flow_item_eth ipv6_multi_spec = {
212                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
213         };
214         struct rte_flow_item_eth ipv6_multi_mask = {
215                 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
216         };
217         struct rte_flow_item_eth unicast = {
218                 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
219         };
220         struct rte_flow_item_eth unicast_mask = {
221                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
222         };
223         const unsigned int vlan_filter_n = priv->vlan_filter_n;
224         const struct ether_addr cmp = {
225                 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
226         };
227         unsigned int i;
228         unsigned int j;
229         int ret;
230
231         if (priv->isolated)
232                 return 0;
233         if (dev->data->promiscuous) {
234                 struct rte_flow_item_eth promisc = {
235                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
236                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
237                         .type = 0,
238                 };
239
240                 claim_zero(mlx5_ctrl_flow(dev, &promisc, &promisc));
241                 return 0;
242         }
243         if (dev->data->all_multicast) {
244                 struct rte_flow_item_eth multicast = {
245                         .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
246                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
247                         .type = 0,
248                 };
249
250                 claim_zero(mlx5_ctrl_flow(dev, &multicast, &multicast));
251         } else {
252                 /* Add broadcast/multicast flows. */
253                 for (i = 0; i != vlan_filter_n; ++i) {
254                         uint16_t vlan = priv->vlan_filter[i];
255
256                         struct rte_flow_item_vlan vlan_spec = {
257                                 .tci = rte_cpu_to_be_16(vlan),
258                         };
259                         struct rte_flow_item_vlan vlan_mask = {
260                                 .tci = 0xffff,
261                         };
262
263                         ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
264                                                   &vlan_spec, &vlan_mask);
265                         if (ret)
266                                 goto error;
267                         ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
268                                                   &ipv6_multi_mask,
269                                                   &vlan_spec, &vlan_mask);
270                         if (ret)
271                                 goto error;
272                 }
273                 if (!vlan_filter_n) {
274                         ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
275                         if (ret)
276                                 goto error;
277                         ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
278                                              &ipv6_multi_mask);
279                         if (ret)
280                                 goto error;
281                 }
282         }
283         /* Add MAC address flows. */
284         for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
285                 struct ether_addr *mac = &dev->data->mac_addrs[i];
286
287                 if (!memcmp(mac, &cmp, sizeof(*mac)))
288                         continue;
289                 memcpy(&unicast.dst.addr_bytes,
290                        mac->addr_bytes,
291                        ETHER_ADDR_LEN);
292                 for (j = 0; j != vlan_filter_n; ++j) {
293                         uint16_t vlan = priv->vlan_filter[j];
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, &unicast,
303                                                   &unicast_mask,
304                                                   &vlan_spec,
305                                                   &vlan_mask);
306                         if (ret)
307                                 goto error;
308                 }
309                 if (!vlan_filter_n) {
310                         ret = mlx5_ctrl_flow(dev, &unicast,
311                                              &unicast_mask);
312                         if (ret)
313                                 goto error;
314                 }
315         }
316         return 0;
317 error:
318         return rte_errno;
319 }
320
321
322 /**
323  * Disable traffic flows configured by control plane
324  *
325  * @param priv
326  *   Pointer to Ethernet device private data.
327  * @param dev
328  *   Pointer to Ethernet device structure.
329  *
330  * @return
331  *   0 on success.
332  */
333 int
334 priv_dev_traffic_disable(struct priv *priv, struct rte_eth_dev *dev)
335 {
336         (void)dev;
337         priv_flow_flush(priv, &priv->ctrl_flows);
338         return 0;
339 }
340
341 /**
342  * Restart traffic flows configured by control plane
343  *
344  * @param priv
345  *   Pointer to Ethernet device private data.
346  * @param dev
347  *   Pointer to Ethernet device structure.
348  *
349  * @return
350  *   0 on success.
351  */
352 int
353 priv_dev_traffic_restart(struct priv *priv, struct rte_eth_dev *dev)
354 {
355         if (dev->data->dev_started) {
356                 priv_dev_traffic_disable(priv, dev);
357                 priv_dev_traffic_enable(priv, dev);
358         }
359         return 0;
360 }
361
362 /**
363  * Restart traffic flows configured by control plane
364  *
365  * @param dev
366  *   Pointer to Ethernet device structure.
367  *
368  * @return
369  *   0 on success.
370  */
371 int
372 mlx5_traffic_restart(struct rte_eth_dev *dev)
373 {
374         struct priv *priv = dev->data->dev_private;
375
376         priv_lock(priv);
377         priv_dev_traffic_restart(priv, dev);
378         priv_unlock(priv);
379         return 0;
380 }