net/mlx5: create advanced RxQ via DevX
[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 Technologies, Ltd
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 mlx5_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 mlx5_priv *priv = dev->data->dev_private;
46         unsigned int i;
47         int ret;
48
49         for (i = 0; i != priv->txqs_n; ++i) {
50                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
51
52                 if (!txq_ctrl)
53                         continue;
54                 txq_alloc_elts(txq_ctrl);
55                 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
56                 if (!txq_ctrl->ibv) {
57                         rte_errno = ENOMEM;
58                         goto error;
59                 }
60         }
61         return 0;
62 error:
63         ret = rte_errno; /* Save rte_errno before cleanup. */
64         do {
65                 mlx5_txq_release(dev, i);
66         } while (i-- != 0);
67         rte_errno = ret; /* Restore rte_errno. */
68         return -rte_errno;
69 }
70
71 /**
72  * Stop traffic on Rx queues.
73  *
74  * @param dev
75  *   Pointer to Ethernet device structure.
76  */
77 static void
78 mlx5_rxq_stop(struct rte_eth_dev *dev)
79 {
80         struct mlx5_priv *priv = dev->data->dev_private;
81         unsigned int i;
82
83         for (i = 0; i != priv->rxqs_n; ++i)
84                 mlx5_rxq_release(dev, i);
85 }
86
87 /**
88  * Start traffic on Rx queues.
89  *
90  * @param dev
91  *   Pointer to Ethernet device structure.
92  *
93  * @return
94  *   0 on success, a negative errno value otherwise and rte_errno is set.
95  */
96 static int
97 mlx5_rxq_start(struct rte_eth_dev *dev)
98 {
99         struct mlx5_priv *priv = dev->data->dev_private;
100         unsigned int i;
101         int ret = 0;
102
103         /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
104         if (mlx5_mprq_alloc_mp(dev)) {
105                 /* Should not release Rx queues but return immediately. */
106                 return -rte_errno;
107         }
108         for (i = 0; i != priv->rxqs_n; ++i) {
109                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
110                 struct rte_mempool *mp;
111
112                 if (!rxq_ctrl)
113                         continue;
114                 /* Pre-register Rx mempool. */
115                 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
116                      rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
117                 DRV_LOG(DEBUG,
118                         "port %u Rx queue %u registering"
119                         " mp %s having %u chunks",
120                         dev->data->port_id, rxq_ctrl->rxq.idx,
121                         mp->name, mp->nb_mem_chunks);
122                 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
123                 ret = rxq_alloc_elts(rxq_ctrl);
124                 if (ret)
125                         goto error;
126                 rxq_ctrl->obj = mlx5_rxq_obj_new(dev, i,
127                                                  MLX5_RXQ_OBJ_TYPE_DEVX_RQ);
128                 if (!rxq_ctrl->obj)
129                         goto error;
130                 rxq_ctrl->wqn = rxq_ctrl->obj->wq->wq_num;
131         }
132         return 0;
133 error:
134         ret = rte_errno; /* Save rte_errno before cleanup. */
135         do {
136                 mlx5_rxq_release(dev, i);
137         } while (i-- != 0);
138         rte_errno = ret; /* Restore rte_errno. */
139         return -rte_errno;
140 }
141
142 /**
143  * DPDK callback to start the device.
144  *
145  * Simulate device start by attaching all configured flows.
146  *
147  * @param dev
148  *   Pointer to Ethernet device structure.
149  *
150  * @return
151  *   0 on success, a negative errno value otherwise and rte_errno is set.
152  */
153 int
154 mlx5_dev_start(struct rte_eth_dev *dev)
155 {
156         struct mlx5_priv *priv = dev->data->dev_private;
157         int ret;
158
159         DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
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                 return -rte_errno;
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                 mlx5_txq_stop(dev);
171                 return -rte_errno;
172         }
173         dev->data->dev_started = 1;
174         ret = mlx5_rx_intr_vec_enable(dev);
175         if (ret) {
176                 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
177                         dev->data->port_id);
178                 goto error;
179         }
180         mlx5_stats_init(dev);
181         ret = mlx5_traffic_enable(dev);
182         if (ret) {
183                 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
184                         dev->data->port_id);
185                 goto error;
186         }
187         ret = mlx5_flow_start(dev, &priv->flows);
188         if (ret) {
189                 DRV_LOG(DEBUG, "port %u failed to set flows",
190                         dev->data->port_id);
191                 goto error;
192         }
193         rte_wmb();
194         dev->tx_pkt_burst = mlx5_select_tx_function(dev);
195         dev->rx_pkt_burst = mlx5_select_rx_function(dev);
196         /* Enable datapath on secondary process. */
197         mlx5_mp_req_start_rxtx(dev);
198         mlx5_dev_interrupt_handler_install(dev);
199         return 0;
200 error:
201         ret = rte_errno; /* Save rte_errno before cleanup. */
202         /* Rollback. */
203         dev->data->dev_started = 0;
204         mlx5_flow_stop(dev, &priv->flows);
205         mlx5_traffic_disable(dev);
206         mlx5_txq_stop(dev);
207         mlx5_rxq_stop(dev);
208         rte_errno = ret; /* Restore rte_errno. */
209         return -rte_errno;
210 }
211
212 /**
213  * DPDK callback to stop the device.
214  *
215  * Simulate device stop by detaching all configured flows.
216  *
217  * @param dev
218  *   Pointer to Ethernet device structure.
219  */
220 void
221 mlx5_dev_stop(struct rte_eth_dev *dev)
222 {
223         struct mlx5_priv *priv = dev->data->dev_private;
224
225         dev->data->dev_started = 0;
226         /* Prevent crashes when queues are still in use. */
227         dev->rx_pkt_burst = removed_rx_burst;
228         dev->tx_pkt_burst = removed_tx_burst;
229         rte_wmb();
230         /* Disable datapath on secondary process. */
231         mlx5_mp_req_stop_rxtx(dev);
232         usleep(1000 * priv->rxqs_n);
233         DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
234         mlx5_flow_stop(dev, &priv->flows);
235         mlx5_traffic_disable(dev);
236         mlx5_rx_intr_vec_disable(dev);
237         mlx5_dev_interrupt_handler_uninstall(dev);
238         mlx5_txq_stop(dev);
239         mlx5_rxq_stop(dev);
240 }
241
242 /**
243  * Enable traffic flows configured by control plane
244  *
245  * @param dev
246  *   Pointer to Ethernet device private data.
247  * @param dev
248  *   Pointer to Ethernet device structure.
249  *
250  * @return
251  *   0 on success, a negative errno value otherwise and rte_errno is set.
252  */
253 int
254 mlx5_traffic_enable(struct rte_eth_dev *dev)
255 {
256         struct mlx5_priv *priv = dev->data->dev_private;
257         struct rte_flow_item_eth bcast = {
258                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
259         };
260         struct rte_flow_item_eth ipv6_multi_spec = {
261                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
262         };
263         struct rte_flow_item_eth ipv6_multi_mask = {
264                 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
265         };
266         struct rte_flow_item_eth unicast = {
267                 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
268         };
269         struct rte_flow_item_eth unicast_mask = {
270                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
271         };
272         const unsigned int vlan_filter_n = priv->vlan_filter_n;
273         const struct rte_ether_addr cmp = {
274                 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
275         };
276         unsigned int i;
277         unsigned int j;
278         int ret;
279
280         if (priv->isolated)
281                 return 0;
282         if (dev->data->promiscuous) {
283                 struct rte_flow_item_eth promisc = {
284                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
285                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
286                         .type = 0,
287                 };
288
289                 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
290                 if (ret)
291                         goto error;
292         }
293         if (dev->data->all_multicast) {
294                 struct rte_flow_item_eth multicast = {
295                         .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
296                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
297                         .type = 0,
298                 };
299
300                 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
301                 if (ret)
302                         goto error;
303         } else {
304                 /* Add broadcast/multicast flows. */
305                 for (i = 0; i != vlan_filter_n; ++i) {
306                         uint16_t vlan = priv->vlan_filter[i];
307
308                         struct rte_flow_item_vlan vlan_spec = {
309                                 .tci = rte_cpu_to_be_16(vlan),
310                         };
311                         struct rte_flow_item_vlan vlan_mask =
312                                 rte_flow_item_vlan_mask;
313
314                         ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
315                                                   &vlan_spec, &vlan_mask);
316                         if (ret)
317                                 goto error;
318                         ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
319                                                   &ipv6_multi_mask,
320                                                   &vlan_spec, &vlan_mask);
321                         if (ret)
322                                 goto error;
323                 }
324                 if (!vlan_filter_n) {
325                         ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
326                         if (ret)
327                                 goto error;
328                         ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
329                                              &ipv6_multi_mask);
330                         if (ret)
331                                 goto error;
332                 }
333         }
334         /* Add MAC address flows. */
335         for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
336                 struct rte_ether_addr *mac = &dev->data->mac_addrs[i];
337
338                 if (!memcmp(mac, &cmp, sizeof(*mac)))
339                         continue;
340                 memcpy(&unicast.dst.addr_bytes,
341                        mac->addr_bytes,
342                        RTE_ETHER_ADDR_LEN);
343                 for (j = 0; j != vlan_filter_n; ++j) {
344                         uint16_t vlan = priv->vlan_filter[j];
345
346                         struct rte_flow_item_vlan vlan_spec = {
347                                 .tci = rte_cpu_to_be_16(vlan),
348                         };
349                         struct rte_flow_item_vlan vlan_mask =
350                                 rte_flow_item_vlan_mask;
351
352                         ret = mlx5_ctrl_flow_vlan(dev, &unicast,
353                                                   &unicast_mask,
354                                                   &vlan_spec,
355                                                   &vlan_mask);
356                         if (ret)
357                                 goto error;
358                 }
359                 if (!vlan_filter_n) {
360                         ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
361                         if (ret)
362                                 goto error;
363                 }
364         }
365         return 0;
366 error:
367         ret = rte_errno; /* Save rte_errno before cleanup. */
368         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
369         rte_errno = ret; /* Restore rte_errno. */
370         return -rte_errno;
371 }
372
373
374 /**
375  * Disable traffic flows configured by control plane
376  *
377  * @param dev
378  *   Pointer to Ethernet device private data.
379  */
380 void
381 mlx5_traffic_disable(struct rte_eth_dev *dev)
382 {
383         struct mlx5_priv *priv = dev->data->dev_private;
384
385         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
386 }
387
388 /**
389  * Restart traffic flows configured by control plane
390  *
391  * @param dev
392  *   Pointer to Ethernet device private data.
393  *
394  * @return
395  *   0 on success, a negative errno value otherwise and rte_errno is set.
396  */
397 int
398 mlx5_traffic_restart(struct rte_eth_dev *dev)
399 {
400         if (dev->data->dev_started) {
401                 mlx5_traffic_disable(dev);
402                 return mlx5_traffic_enable(dev);
403         }
404         return 0;
405 }