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 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;
111 /* Pre-register Rx mempool. */
112 mp = rxq_ctrl->rxq.mp;
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);
122 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
128 ret = rte_errno; /* Save rte_errno before cleanup. */
130 rte_errno = ret; /* Restore rte_errno. */
135 * DPDK callback to start the device.
137 * Simulate device start by attaching all configured flows.
140 * Pointer to Ethernet device structure.
143 * 0 on success, a negative errno value otherwise and rte_errno is set.
146 mlx5_dev_start(struct rte_eth_dev *dev)
148 struct priv *priv = dev->data->dev_private;
151 dev->data->dev_started = 1;
152 DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
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 if (rte_log_get_level(mlx5_logtype) == RTE_LOG_DEBUG)
167 mlx5_mr_dump_dev(dev);
168 ret = mlx5_rx_intr_vec_enable(dev);
170 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
174 mlx5_xstats_init(dev);
175 ret = mlx5_traffic_enable(dev);
177 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
181 ret = mlx5_flow_start(dev, &priv->flows);
183 DRV_LOG(DEBUG, "port %u failed to set flows",
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);
192 ret = rte_errno; /* Save rte_errno before cleanup. */
194 dev->data->dev_started = 0;
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;
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;
221 usleep(1000 * priv->rxqs_n);
222 DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
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);
233 * Enable traffic flows configured by control plane
236 * Pointer to Ethernet device private data.
238 * Pointer to Ethernet device structure.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_traffic_enable(struct rte_eth_dev *dev)
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",
250 struct rte_flow_item_eth ipv6_multi_spec = {
251 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
253 struct rte_flow_item_eth ipv6_multi_mask = {
254 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
256 struct rte_flow_item_eth unicast = {
257 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
259 struct rte_flow_item_eth unicast_mask = {
260 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
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",
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",
279 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
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",
290 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
294 /* Add broadcast/multicast flows. */
295 for (i = 0; i != vlan_filter_n; ++i) {
296 uint16_t vlan = priv->vlan_filter[i];
298 struct rte_flow_item_vlan vlan_spec = {
299 .tci = rte_cpu_to_be_16(vlan),
301 struct rte_flow_item_vlan vlan_mask = {
305 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
306 &vlan_spec, &vlan_mask);
309 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
311 &vlan_spec, &vlan_mask);
315 if (!vlan_filter_n) {
316 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
319 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
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];
329 if (!memcmp(mac, &cmp, sizeof(*mac)))
331 memcpy(&unicast.dst.addr_bytes,
334 for (j = 0; j != vlan_filter_n; ++j) {
335 uint16_t vlan = priv->vlan_filter[j];
337 struct rte_flow_item_vlan vlan_spec = {
338 .tci = rte_cpu_to_be_16(vlan),
340 struct rte_flow_item_vlan 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);