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) {
51 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
55 txq_alloc_elts(txq_ctrl);
56 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
62 ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd);
67 ret = rte_errno; /* Save rte_errno before cleanup. */
69 rte_errno = ret; /* Restore rte_errno. */
74 * Stop traffic on Rx queues.
77 * Pointer to Ethernet device structure.
80 mlx5_rxq_stop(struct rte_eth_dev *dev)
82 struct priv *priv = dev->data->dev_private;
85 for (i = 0; i != priv->rxqs_n; ++i)
86 mlx5_rxq_release(dev, i);
90 * Start traffic on Rx queues.
93 * Pointer to Ethernet device structure.
96 * 0 on success, a negative errno value otherwise and rte_errno is set.
99 mlx5_rxq_start(struct rte_eth_dev *dev)
101 struct priv *priv = dev->data->dev_private;
105 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
106 if (mlx5_mprq_alloc_mp(dev))
108 for (i = 0; i != priv->rxqs_n; ++i) {
109 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
110 struct rte_mempool *mp;
114 /* Pre-register Rx mempool. */
115 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
116 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
118 "port %u Rx queue %u registering"
119 " mp %s having %u chunks",
120 dev->data->port_id, rxq_ctrl->idx,
121 mp->name, mp->nb_mem_chunks);
122 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
123 ret = rxq_alloc_elts(rxq_ctrl);
126 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
132 ret = rte_errno; /* Save rte_errno before cleanup. */
134 rte_errno = ret; /* Restore rte_errno. */
139 * DPDK callback to start the device.
141 * Simulate device start by attaching all configured flows.
144 * Pointer to Ethernet device structure.
147 * 0 on success, a negative errno value otherwise and rte_errno is set.
150 mlx5_dev_start(struct rte_eth_dev *dev)
152 struct priv *priv = dev->data->dev_private;
155 dev->data->dev_started = 1;
156 DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
158 ret = mlx5_txq_start(dev);
160 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
161 dev->data->port_id, strerror(rte_errno));
164 ret = mlx5_rxq_start(dev);
166 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
167 dev->data->port_id, strerror(rte_errno));
170 ret = mlx5_rx_intr_vec_enable(dev);
172 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
176 mlx5_xstats_init(dev);
177 ret = mlx5_traffic_enable(dev);
179 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
183 ret = mlx5_flow_start(dev, &priv->flows);
185 DRV_LOG(DEBUG, "port %u failed to set flows",
189 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
190 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
191 mlx5_dev_interrupt_handler_install(dev);
194 ret = rte_errno; /* Save rte_errno before cleanup. */
196 dev->data->dev_started = 0;
197 mlx5_flow_stop(dev, &priv->flows);
198 mlx5_traffic_disable(dev);
201 rte_errno = ret; /* Restore rte_errno. */
206 * DPDK callback to stop the device.
208 * Simulate device stop by detaching all configured flows.
211 * Pointer to Ethernet device structure.
214 mlx5_dev_stop(struct rte_eth_dev *dev)
216 struct priv *priv = dev->data->dev_private;
218 dev->data->dev_started = 0;
219 /* Prevent crashes when queues are still in use. */
220 dev->rx_pkt_burst = removed_rx_burst;
221 dev->tx_pkt_burst = removed_tx_burst;
223 usleep(1000 * priv->rxqs_n);
224 DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
226 mlx5_flow_stop(dev, &priv->flows);
227 mlx5_traffic_disable(dev);
228 mlx5_rx_intr_vec_disable(dev);
229 mlx5_dev_interrupt_handler_uninstall(dev);
235 * Enable traffic flows configured by control plane
238 * Pointer to Ethernet device private data.
240 * Pointer to Ethernet device structure.
243 * 0 on success, a negative errno value otherwise and rte_errno is set.
246 mlx5_traffic_enable(struct rte_eth_dev *dev)
248 struct priv *priv = dev->data->dev_private;
249 struct rte_flow_item_eth bcast = {
250 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
252 struct rte_flow_item_eth ipv6_multi_spec = {
253 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
255 struct rte_flow_item_eth ipv6_multi_mask = {
256 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
258 struct rte_flow_item_eth unicast = {
259 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
261 struct rte_flow_item_eth unicast_mask = {
262 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
264 const unsigned int vlan_filter_n = priv->vlan_filter_n;
265 const struct ether_addr cmp = {
266 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
274 if (dev->data->promiscuous) {
275 struct rte_flow_item_eth promisc = {
276 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
277 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
281 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
285 if (dev->data->all_multicast) {
286 struct rte_flow_item_eth multicast = {
287 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
288 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
292 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
296 /* Add broadcast/multicast flows. */
297 for (i = 0; i != vlan_filter_n; ++i) {
298 uint16_t vlan = priv->vlan_filter[i];
300 struct rte_flow_item_vlan vlan_spec = {
301 .tci = rte_cpu_to_be_16(vlan),
303 struct rte_flow_item_vlan vlan_mask =
304 rte_flow_item_vlan_mask;
306 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
307 &vlan_spec, &vlan_mask);
310 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
312 &vlan_spec, &vlan_mask);
316 if (!vlan_filter_n) {
317 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
320 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
326 /* Add MAC address flows. */
327 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
328 struct ether_addr *mac = &dev->data->mac_addrs[i];
330 if (!memcmp(mac, &cmp, sizeof(*mac)))
332 memcpy(&unicast.dst.addr_bytes,
335 for (j = 0; j != vlan_filter_n; ++j) {
336 uint16_t vlan = priv->vlan_filter[j];
338 struct rte_flow_item_vlan vlan_spec = {
339 .tci = rte_cpu_to_be_16(vlan),
341 struct rte_flow_item_vlan vlan_mask =
342 rte_flow_item_vlan_mask;
344 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
351 if (!vlan_filter_n) {
352 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
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. */
367 * Disable traffic flows configured by control plane
370 * Pointer to Ethernet device private data.
373 mlx5_traffic_disable(struct rte_eth_dev *dev)
375 struct priv *priv = dev->data->dev_private;
377 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
381 * Restart traffic flows configured by control plane
384 * Pointer to Ethernet device private data.
387 * 0 on success, a negative errno value otherwise and rte_errno is set.
390 mlx5_traffic_restart(struct rte_eth_dev *dev)
392 if (dev->data->dev_started) {
393 mlx5_traffic_disable(dev);
394 return mlx5_traffic_enable(dev);