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