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