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