xref: /dpdk/drivers/net/mlx5/mlx5_trigger.c (revision 2f82d143fb318042f47a50694baa4507b51b7381)
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 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 	DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
152 		dev->data->port_id);
153 	rte_mempool_walk(mlx5_mp2mr_iter, priv);
154 	ret = mlx5_txq_start(dev);
155 	if (ret) {
156 		DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
157 			dev->data->port_id, strerror(rte_errno));
158 		goto error;
159 	}
160 	ret = mlx5_rxq_start(dev);
161 	if (ret) {
162 		DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
163 			dev->data->port_id, strerror(rte_errno));
164 		goto error;
165 	}
166 	ret = mlx5_rx_intr_vec_enable(dev);
167 	if (ret) {
168 		DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
169 			dev->data->port_id);
170 		goto error;
171 	}
172 	mlx5_xstats_init(dev);
173 	ret = mlx5_traffic_enable(dev);
174 	if (ret) {
175 		DRV_LOG(DEBUG, "port %u failed to set defaults flows",
176 			dev->data->port_id);
177 		goto error;
178 	}
179 	ret = mlx5_flow_start(dev, &priv->flows);
180 	if (ret) {
181 		DRV_LOG(DEBUG, "port %u failed to set flows",
182 			dev->data->port_id);
183 		goto error;
184 	}
185 	dev->tx_pkt_burst = mlx5_select_tx_function(dev);
186 	dev->rx_pkt_burst = mlx5_select_rx_function(dev);
187 	mlx5_dev_interrupt_handler_install(dev);
188 	return 0;
189 error:
190 	ret = rte_errno; /* Save rte_errno before cleanup. */
191 	/* Rollback. */
192 	dev->data->dev_started = 0;
193 	for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
194 		mlx5_mr_release(mr);
195 	mlx5_flow_stop(dev, &priv->flows);
196 	mlx5_traffic_disable(dev);
197 	mlx5_txq_stop(dev);
198 	mlx5_rxq_stop(dev);
199 	rte_errno = ret; /* Restore rte_errno. */
200 	return -rte_errno;
201 }
202 
203 /**
204  * DPDK callback to stop the device.
205  *
206  * Simulate device stop by detaching all configured flows.
207  *
208  * @param dev
209  *   Pointer to Ethernet device structure.
210  */
211 void
212 mlx5_dev_stop(struct rte_eth_dev *dev)
213 {
214 	struct priv *priv = dev->data->dev_private;
215 	struct mlx5_mr *mr;
216 
217 	dev->data->dev_started = 0;
218 	/* Prevent crashes when queues are still in use. */
219 	dev->rx_pkt_burst = removed_rx_burst;
220 	dev->tx_pkt_burst = removed_tx_burst;
221 	rte_wmb();
222 	usleep(1000 * priv->rxqs_n);
223 	DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
224 		dev->data->port_id);
225 	mlx5_flow_stop(dev, &priv->flows);
226 	mlx5_traffic_disable(dev);
227 	mlx5_rx_intr_vec_disable(dev);
228 	mlx5_dev_interrupt_handler_uninstall(dev);
229 	mlx5_txq_stop(dev);
230 	mlx5_rxq_stop(dev);
231 	for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
232 		mlx5_mr_release(mr);
233 }
234 
235 /**
236  * Enable traffic flows configured by control plane
237  *
238  * @param dev
239  *   Pointer to Ethernet device private data.
240  * @param dev
241  *   Pointer to Ethernet device structure.
242  *
243  * @return
244  *   0 on success, a negative errno value otherwise and rte_errno is set.
245  */
246 int
247 mlx5_traffic_enable(struct rte_eth_dev *dev)
248 {
249 	struct priv *priv = dev->data->dev_private;
250 	struct rte_flow_item_eth bcast = {
251 		.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
252 	};
253 	struct rte_flow_item_eth ipv6_multi_spec = {
254 		.dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
255 	};
256 	struct rte_flow_item_eth ipv6_multi_mask = {
257 		.dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
258 	};
259 	struct rte_flow_item_eth unicast = {
260 		.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
261 	};
262 	struct rte_flow_item_eth unicast_mask = {
263 		.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
264 	};
265 	const unsigned int vlan_filter_n = priv->vlan_filter_n;
266 	const struct ether_addr cmp = {
267 		.addr_bytes = "\x00\x00\x00\x00\x00\x00",
268 	};
269 	unsigned int i;
270 	unsigned int j;
271 	int ret;
272 
273 	if (priv->isolated)
274 		return 0;
275 	if (dev->data->promiscuous) {
276 		struct rte_flow_item_eth promisc = {
277 			.dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
278 			.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
279 			.type = 0,
280 		};
281 
282 		ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
283 		if (ret)
284 			goto error;
285 	}
286 	if (dev->data->all_multicast) {
287 		struct rte_flow_item_eth multicast = {
288 			.dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
289 			.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
290 			.type = 0,
291 		};
292 
293 		ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
294 		if (ret)
295 			goto error;
296 	} else {
297 		/* Add broadcast/multicast flows. */
298 		for (i = 0; i != vlan_filter_n; ++i) {
299 			uint16_t vlan = priv->vlan_filter[i];
300 
301 			struct rte_flow_item_vlan vlan_spec = {
302 				.tci = rte_cpu_to_be_16(vlan),
303 			};
304 			struct rte_flow_item_vlan vlan_mask = {
305 				.tci = 0xffff,
306 			};
307 
308 			ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
309 						  &vlan_spec, &vlan_mask);
310 			if (ret)
311 				goto error;
312 			ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
313 						  &ipv6_multi_mask,
314 						  &vlan_spec, &vlan_mask);
315 			if (ret)
316 				goto error;
317 		}
318 		if (!vlan_filter_n) {
319 			ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
320 			if (ret)
321 				goto error;
322 			ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
323 					     &ipv6_multi_mask);
324 			if (ret)
325 				goto error;
326 		}
327 	}
328 	/* Add MAC address flows. */
329 	for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
330 		struct ether_addr *mac = &dev->data->mac_addrs[i];
331 
332 		if (!memcmp(mac, &cmp, sizeof(*mac)))
333 			continue;
334 		memcpy(&unicast.dst.addr_bytes,
335 		       mac->addr_bytes,
336 		       ETHER_ADDR_LEN);
337 		for (j = 0; j != vlan_filter_n; ++j) {
338 			uint16_t vlan = priv->vlan_filter[j];
339 
340 			struct rte_flow_item_vlan vlan_spec = {
341 				.tci = rte_cpu_to_be_16(vlan),
342 			};
343 			struct rte_flow_item_vlan vlan_mask = {
344 				.tci = 0xffff,
345 			};
346 
347 			ret = mlx5_ctrl_flow_vlan(dev, &unicast,
348 						  &unicast_mask,
349 						  &vlan_spec,
350 						  &vlan_mask);
351 			if (ret)
352 				goto error;
353 		}
354 		if (!vlan_filter_n) {
355 			ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
356 			if (ret)
357 				goto error;
358 		}
359 	}
360 	return 0;
361 error:
362 	ret = rte_errno; /* Save rte_errno before cleanup. */
363 	mlx5_flow_list_flush(dev, &priv->ctrl_flows);
364 	rte_errno = ret; /* Restore rte_errno. */
365 	return -rte_errno;
366 }
367 
368 
369 /**
370  * Disable traffic flows configured by control plane
371  *
372  * @param dev
373  *   Pointer to Ethernet device private data.
374  */
375 void
376 mlx5_traffic_disable(struct rte_eth_dev *dev)
377 {
378 	struct priv *priv = dev->data->dev_private;
379 
380 	mlx5_flow_list_flush(dev, &priv->ctrl_flows);
381 }
382 
383 /**
384  * Restart traffic flows configured by control plane
385  *
386  * @param dev
387  *   Pointer to Ethernet device private data.
388  *
389  * @return
390  *   0 on success, a negative errno value otherwise and rte_errno is set.
391  */
392 int
393 mlx5_traffic_restart(struct rte_eth_dev *dev)
394 {
395 	if (dev->data->dev_started) {
396 		mlx5_traffic_disable(dev);
397 		return mlx5_traffic_enable(dev);
398 	}
399 	return 0;
400 }
401