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 mlx5_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 mlx5_priv *priv = dev->data->dev_private;
49 for (i = 0; i != priv->txqs_n; ++i) {
50 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
54 txq_alloc_elts(txq_ctrl);
55 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
63 ret = rte_errno; /* Save rte_errno before cleanup. */
65 mlx5_txq_release(dev, i);
67 rte_errno = ret; /* Restore rte_errno. */
72 * Stop traffic on Rx queues.
75 * Pointer to Ethernet device structure.
78 mlx5_rxq_stop(struct rte_eth_dev *dev)
80 struct mlx5_priv *priv = dev->data->dev_private;
83 for (i = 0; i != priv->rxqs_n; ++i)
84 mlx5_rxq_release(dev, i);
88 * Start traffic on Rx queues.
91 * Pointer to Ethernet device structure.
94 * 0 on success, a negative errno value otherwise and rte_errno is set.
97 mlx5_rxq_start(struct rte_eth_dev *dev)
99 struct mlx5_priv *priv = dev->data->dev_private;
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;
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. */
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;
117 /* Pre-register Rx mempool. */
118 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
119 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
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);
129 rxq_ctrl->obj = mlx5_rxq_obj_new(dev, i, obj_type);
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;
139 ret = rte_errno; /* Save rte_errno before cleanup. */
141 mlx5_rxq_release(dev, i);
143 rte_errno = ret; /* Restore rte_errno. */
148 * DPDK callback to start the device.
150 * Simulate device start by attaching all configured flows.
153 * Pointer to Ethernet device structure.
156 * 0 on success, a negative errno value otherwise and rte_errno is set.
159 mlx5_dev_start(struct rte_eth_dev *dev)
161 struct mlx5_priv *priv = dev->data->dev_private;
164 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
165 ret = mlx5_txq_start(dev);
167 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
168 dev->data->port_id, strerror(rte_errno));
171 ret = mlx5_rxq_start(dev);
173 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
174 dev->data->port_id, strerror(rte_errno));
178 dev->data->dev_started = 1;
179 ret = mlx5_rx_intr_vec_enable(dev);
181 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
185 mlx5_stats_init(dev);
186 ret = mlx5_traffic_enable(dev);
188 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
192 ret = mlx5_flow_start(dev, &priv->flows);
194 DRV_LOG(DEBUG, "port %u failed to set flows",
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);
206 ret = rte_errno; /* Save rte_errno before cleanup. */
208 dev->data->dev_started = 0;
209 mlx5_flow_stop(dev, &priv->flows);
210 mlx5_traffic_disable(dev);
213 rte_errno = ret; /* Restore rte_errno. */
218 * DPDK callback to stop the device.
220 * Simulate device stop by detaching all configured flows.
223 * Pointer to Ethernet device structure.
226 mlx5_dev_stop(struct rte_eth_dev *dev)
228 struct mlx5_priv *priv = dev->data->dev_private;
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;
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);
248 * Enable traffic flows configured by control plane
251 * Pointer to Ethernet device private data.
253 * Pointer to Ethernet device structure.
256 * 0 on success, a negative errno value otherwise and rte_errno is set.
259 mlx5_traffic_enable(struct rte_eth_dev *dev)
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",
265 struct rte_flow_item_eth ipv6_multi_spec = {
266 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
268 struct rte_flow_item_eth ipv6_multi_mask = {
269 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
271 struct rte_flow_item_eth unicast = {
272 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
274 struct rte_flow_item_eth unicast_mask = {
275 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
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",
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",
294 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
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",
305 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
309 /* Add broadcast/multicast flows. */
310 for (i = 0; i != vlan_filter_n; ++i) {
311 uint16_t vlan = priv->vlan_filter[i];
313 struct rte_flow_item_vlan vlan_spec = {
314 .tci = rte_cpu_to_be_16(vlan),
316 struct rte_flow_item_vlan vlan_mask =
317 rte_flow_item_vlan_mask;
319 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
320 &vlan_spec, &vlan_mask);
323 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
325 &vlan_spec, &vlan_mask);
329 if (!vlan_filter_n) {
330 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
333 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
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];
343 if (!memcmp(mac, &cmp, sizeof(*mac)))
345 memcpy(&unicast.dst.addr_bytes,
348 for (j = 0; j != vlan_filter_n; ++j) {
349 uint16_t vlan = priv->vlan_filter[j];
351 struct rte_flow_item_vlan vlan_spec = {
352 .tci = rte_cpu_to_be_16(vlan),
354 struct rte_flow_item_vlan vlan_mask =
355 rte_flow_item_vlan_mask;
357 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
364 if (!vlan_filter_n) {
365 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
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. */
380 * Disable traffic flows configured by control plane
383 * Pointer to Ethernet device private data.
386 mlx5_traffic_disable(struct rte_eth_dev *dev)
388 struct mlx5_priv *priv = dev->data->dev_private;
390 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
394 * Restart traffic flows configured by control plane
397 * Pointer to Ethernet device private data.
400 * 0 on success, a negative errno value otherwise and rte_errno is set.
403 mlx5_traffic_restart(struct rte_eth_dev *dev)
405 if (dev->data->dev_started) {
406 mlx5_traffic_disable(dev);
407 return mlx5_traffic_enable(dev);