1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
9 #include <rte_ethdev_driver.h>
10 #include <rte_interrupts.h>
11 #include <rte_alarm.h>
14 #include "mlx5_rxtx.h"
15 #include "mlx5_utils.h"
18 * Stop traffic on Tx queues.
21 * Pointer to Ethernet device structure.
24 mlx5_txq_stop(struct rte_eth_dev *dev)
26 struct priv *priv = dev->data->dev_private;
29 for (i = 0; i != priv->txqs_n; ++i)
30 mlx5_txq_release(dev, i);
34 * Start traffic on Tx queues.
37 * Pointer to Ethernet device structure.
40 * 0 on success, a negative errno value otherwise and rte_errno is set.
43 mlx5_txq_start(struct rte_eth_dev *dev)
45 struct priv *priv = dev->data->dev_private;
49 /* Add memory regions to Tx queues. */
50 for (i = 0; i != priv->txqs_n; ++i) {
53 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
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)
62 txq_alloc_elts(txq_ctrl);
63 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
69 ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd);
74 ret = rte_errno; /* Save rte_errno before cleanup. */
76 rte_errno = ret; /* Restore rte_errno. */
81 * Stop traffic on Rx queues.
84 * Pointer to Ethernet device structure.
87 mlx5_rxq_stop(struct rte_eth_dev *dev)
89 struct priv *priv = dev->data->dev_private;
92 for (i = 0; i != priv->rxqs_n; ++i)
93 mlx5_rxq_release(dev, i);
97 * Start traffic on Rx queues.
100 * Pointer to Ethernet device structure.
103 * 0 on success, a negative errno value otherwise and rte_errno is set.
106 mlx5_rxq_start(struct rte_eth_dev *dev)
108 struct priv *priv = dev->data->dev_private;
112 for (i = 0; i != priv->rxqs_n; ++i) {
113 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
117 ret = rxq_alloc_elts(rxq_ctrl);
120 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
126 ret = rte_errno; /* Save rte_errno before cleanup. */
128 rte_errno = ret; /* Restore rte_errno. */
133 * DPDK callback to start the device.
135 * Simulate device start by attaching all configured flows.
138 * Pointer to Ethernet device structure.
141 * 0 on success, a negative errno value otherwise and rte_errno is set.
144 mlx5_dev_start(struct rte_eth_dev *dev)
146 struct priv *priv = dev->data->dev_private;
147 struct mlx5_mr *mr = NULL;
150 dev->data->dev_started = 1;
151 DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
153 rte_mempool_walk(mlx5_mp2mr_iter, priv);
154 ret = mlx5_txq_start(dev);
156 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
157 dev->data->port_id, strerror(rte_errno));
160 ret = mlx5_rxq_start(dev);
162 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
163 dev->data->port_id, strerror(rte_errno));
166 ret = mlx5_rx_intr_vec_enable(dev);
168 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
172 mlx5_xstats_init(dev);
173 ret = mlx5_traffic_enable(dev);
175 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
179 ret = mlx5_flow_start(dev, &priv->flows);
181 DRV_LOG(DEBUG, "port %u failed to set flows",
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);
190 ret = rte_errno; /* Save rte_errno before cleanup. */
192 dev->data->dev_started = 0;
193 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
195 mlx5_flow_stop(dev, &priv->flows);
196 mlx5_traffic_disable(dev);
199 rte_errno = ret; /* Restore rte_errno. */
204 * DPDK callback to stop the device.
206 * Simulate device stop by detaching all configured flows.
209 * Pointer to Ethernet device structure.
212 mlx5_dev_stop(struct rte_eth_dev *dev)
214 struct priv *priv = dev->data->dev_private;
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;
222 usleep(1000 * priv->rxqs_n);
223 DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
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);
231 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
236 * Enable traffic flows configured by control plane
239 * Pointer to Ethernet device private data.
241 * Pointer to Ethernet device structure.
244 * 0 on success, a negative errno value otherwise and rte_errno is set.
247 mlx5_traffic_enable(struct rte_eth_dev *dev)
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",
253 struct rte_flow_item_eth ipv6_multi_spec = {
254 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
256 struct rte_flow_item_eth ipv6_multi_mask = {
257 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
259 struct rte_flow_item_eth unicast = {
260 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
262 struct rte_flow_item_eth unicast_mask = {
263 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
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",
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",
282 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
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",
293 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
297 /* Add broadcast/multicast flows. */
298 for (i = 0; i != vlan_filter_n; ++i) {
299 uint16_t vlan = priv->vlan_filter[i];
301 struct rte_flow_item_vlan vlan_spec = {
302 .tci = rte_cpu_to_be_16(vlan),
304 struct rte_flow_item_vlan vlan_mask = {
308 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
309 &vlan_spec, &vlan_mask);
312 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
314 &vlan_spec, &vlan_mask);
318 if (!vlan_filter_n) {
319 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
322 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
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];
332 if (!memcmp(mac, &cmp, sizeof(*mac)))
334 memcpy(&unicast.dst.addr_bytes,
337 for (j = 0; j != vlan_filter_n; ++j) {
338 uint16_t vlan = priv->vlan_filter[j];
340 struct rte_flow_item_vlan vlan_spec = {
341 .tci = rte_cpu_to_be_16(vlan),
343 struct rte_flow_item_vlan vlan_mask = {
347 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
354 if (!vlan_filter_n) {
355 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
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. */
370 * Disable traffic flows configured by control plane
373 * Pointer to Ethernet device private data.
376 mlx5_traffic_disable(struct rte_eth_dev *dev)
378 struct priv *priv = dev->data->dev_private;
380 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
384 * Restart traffic flows configured by control plane
387 * Pointer to Ethernet device private data.
390 * 0 on success, a negative errno value otherwise and rte_errno is set.
393 mlx5_traffic_restart(struct rte_eth_dev *dev)
395 if (dev->data->dev_started) {
396 mlx5_traffic_disable(dev);
397 return mlx5_traffic_enable(dev);