xref: /dpdk/drivers/net/mlx5/mlx5_trigger.c (revision 974f1e7ef146ffa112ab1a2afaf5db0360bc159f)
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 		struct rte_mempool *mp;
108 
109 		if (!rxq_ctrl)
110 			continue;
111 		/* Pre-register Rx mempool. */
112 		mp = rxq_ctrl->rxq.mp;
113 		DRV_LOG(DEBUG,
114 			"port %u Rx queue %u registering"
115 			" mp %s having %u chunks",
116 			dev->data->port_id, rxq_ctrl->idx,
117 			mp->name, mp->nb_mem_chunks);
118 		mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
119 		ret = rxq_alloc_elts(rxq_ctrl);
120 		if (ret)
121 			goto error;
122 		rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
123 		if (!rxq_ctrl->ibv)
124 			goto error;
125 	}
126 	return 0;
127 error:
128 	ret = rte_errno; /* Save rte_errno before cleanup. */
129 	mlx5_rxq_stop(dev);
130 	rte_errno = ret; /* Restore rte_errno. */
131 	return -rte_errno;
132 }
133 
134 /**
135  * DPDK callback to start the device.
136  *
137  * Simulate device start by attaching all configured flows.
138  *
139  * @param dev
140  *   Pointer to Ethernet device structure.
141  *
142  * @return
143  *   0 on success, a negative errno value otherwise and rte_errno is set.
144  */
145 int
146 mlx5_dev_start(struct rte_eth_dev *dev)
147 {
148 	struct priv *priv = dev->data->dev_private;
149 	int ret;
150 
151 	dev->data->dev_started = 1;
152 	DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
153 		dev->data->port_id);
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 	if (rte_log_get_level(mlx5_logtype) == RTE_LOG_DEBUG)
167 		mlx5_mr_dump_dev(dev);
168 	ret = mlx5_rx_intr_vec_enable(dev);
169 	if (ret) {
170 		DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
171 			dev->data->port_id);
172 		goto error;
173 	}
174 	mlx5_xstats_init(dev);
175 	ret = mlx5_traffic_enable(dev);
176 	if (ret) {
177 		DRV_LOG(DEBUG, "port %u failed to set defaults flows",
178 			dev->data->port_id);
179 		goto error;
180 	}
181 	ret = mlx5_flow_start(dev, &priv->flows);
182 	if (ret) {
183 		DRV_LOG(DEBUG, "port %u failed to set flows",
184 			dev->data->port_id);
185 		goto error;
186 	}
187 	dev->tx_pkt_burst = mlx5_select_tx_function(dev);
188 	dev->rx_pkt_burst = mlx5_select_rx_function(dev);
189 	mlx5_dev_interrupt_handler_install(dev);
190 	return 0;
191 error:
192 	ret = rte_errno; /* Save rte_errno before cleanup. */
193 	/* Rollback. */
194 	dev->data->dev_started = 0;
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 
216 	dev->data->dev_started = 0;
217 	/* Prevent crashes when queues are still in use. */
218 	dev->rx_pkt_burst = removed_rx_burst;
219 	dev->tx_pkt_burst = removed_tx_burst;
220 	rte_wmb();
221 	usleep(1000 * priv->rxqs_n);
222 	DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
223 		dev->data->port_id);
224 	mlx5_flow_stop(dev, &priv->flows);
225 	mlx5_traffic_disable(dev);
226 	mlx5_rx_intr_vec_disable(dev);
227 	mlx5_dev_interrupt_handler_uninstall(dev);
228 	mlx5_txq_stop(dev);
229 	mlx5_rxq_stop(dev);
230 }
231 
232 /**
233  * Enable traffic flows configured by control plane
234  *
235  * @param dev
236  *   Pointer to Ethernet device private data.
237  * @param dev
238  *   Pointer to Ethernet device structure.
239  *
240  * @return
241  *   0 on success, a negative errno value otherwise and rte_errno is set.
242  */
243 int
244 mlx5_traffic_enable(struct rte_eth_dev *dev)
245 {
246 	struct priv *priv = dev->data->dev_private;
247 	struct rte_flow_item_eth bcast = {
248 		.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
249 	};
250 	struct rte_flow_item_eth ipv6_multi_spec = {
251 		.dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
252 	};
253 	struct rte_flow_item_eth ipv6_multi_mask = {
254 		.dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
255 	};
256 	struct rte_flow_item_eth unicast = {
257 		.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
258 	};
259 	struct rte_flow_item_eth unicast_mask = {
260 		.dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
261 	};
262 	const unsigned int vlan_filter_n = priv->vlan_filter_n;
263 	const struct ether_addr cmp = {
264 		.addr_bytes = "\x00\x00\x00\x00\x00\x00",
265 	};
266 	unsigned int i;
267 	unsigned int j;
268 	int ret;
269 
270 	if (priv->isolated)
271 		return 0;
272 	if (dev->data->promiscuous) {
273 		struct rte_flow_item_eth promisc = {
274 			.dst.addr_bytes = "\x00\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, &promisc, &promisc);
280 		if (ret)
281 			goto error;
282 	}
283 	if (dev->data->all_multicast) {
284 		struct rte_flow_item_eth multicast = {
285 			.dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
286 			.src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
287 			.type = 0,
288 		};
289 
290 		ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
291 		if (ret)
292 			goto error;
293 	} else {
294 		/* Add broadcast/multicast flows. */
295 		for (i = 0; i != vlan_filter_n; ++i) {
296 			uint16_t vlan = priv->vlan_filter[i];
297 
298 			struct rte_flow_item_vlan vlan_spec = {
299 				.tci = rte_cpu_to_be_16(vlan),
300 			};
301 			struct rte_flow_item_vlan vlan_mask = {
302 				.tci = 0xffff,
303 			};
304 
305 			ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
306 						  &vlan_spec, &vlan_mask);
307 			if (ret)
308 				goto error;
309 			ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
310 						  &ipv6_multi_mask,
311 						  &vlan_spec, &vlan_mask);
312 			if (ret)
313 				goto error;
314 		}
315 		if (!vlan_filter_n) {
316 			ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
317 			if (ret)
318 				goto error;
319 			ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
320 					     &ipv6_multi_mask);
321 			if (ret)
322 				goto error;
323 		}
324 	}
325 	/* Add MAC address flows. */
326 	for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
327 		struct ether_addr *mac = &dev->data->mac_addrs[i];
328 
329 		if (!memcmp(mac, &cmp, sizeof(*mac)))
330 			continue;
331 		memcpy(&unicast.dst.addr_bytes,
332 		       mac->addr_bytes,
333 		       ETHER_ADDR_LEN);
334 		for (j = 0; j != vlan_filter_n; ++j) {
335 			uint16_t vlan = priv->vlan_filter[j];
336 
337 			struct rte_flow_item_vlan vlan_spec = {
338 				.tci = rte_cpu_to_be_16(vlan),
339 			};
340 			struct rte_flow_item_vlan vlan_mask = {
341 				.tci = 0xffff,
342 			};
343 
344 			ret = mlx5_ctrl_flow_vlan(dev, &unicast,
345 						  &unicast_mask,
346 						  &vlan_spec,
347 						  &vlan_mask);
348 			if (ret)
349 				goto error;
350 		}
351 		if (!vlan_filter_n) {
352 			ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
353 			if (ret)
354 				goto error;
355 		}
356 	}
357 	return 0;
358 error:
359 	ret = rte_errno; /* Save rte_errno before cleanup. */
360 	mlx5_flow_list_flush(dev, &priv->ctrl_flows);
361 	rte_errno = ret; /* Restore rte_errno. */
362 	return -rte_errno;
363 }
364 
365 
366 /**
367  * Disable traffic flows configured by control plane
368  *
369  * @param dev
370  *   Pointer to Ethernet device private data.
371  */
372 void
373 mlx5_traffic_disable(struct rte_eth_dev *dev)
374 {
375 	struct priv *priv = dev->data->dev_private;
376 
377 	mlx5_flow_list_flush(dev, &priv->ctrl_flows);
378 }
379 
380 /**
381  * Restart traffic flows configured by control plane
382  *
383  * @param dev
384  *   Pointer to Ethernet device private data.
385  *
386  * @return
387  *   0 on success, a negative errno value otherwise and rte_errno is set.
388  */
389 int
390 mlx5_traffic_restart(struct rte_eth_dev *dev)
391 {
392 	if (dev->data->dev_started) {
393 		mlx5_traffic_disable(dev);
394 		return mlx5_traffic_enable(dev);
395 	}
396 	return 0;
397 }
398