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;
103 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
104 if (mlx5_mprq_alloc_mp(dev)) {
105 /* Should not release Rx queues but return immediately. */
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->rxq.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->obj = mlx5_rxq_obj_new(dev, i);
129 rxq_ctrl->wqn = rxq_ctrl->obj->wq->wq_num;
133 ret = rte_errno; /* Save rte_errno before cleanup. */
135 mlx5_rxq_release(dev, i);
137 rte_errno = ret; /* Restore rte_errno. */
142 * DPDK callback to start the device.
144 * Simulate device start by attaching all configured flows.
147 * Pointer to Ethernet device structure.
150 * 0 on success, a negative errno value otherwise and rte_errno is set.
153 mlx5_dev_start(struct rte_eth_dev *dev)
155 struct mlx5_priv *priv = dev->data->dev_private;
158 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
159 ret = mlx5_txq_start(dev);
161 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
162 dev->data->port_id, strerror(rte_errno));
165 ret = mlx5_rxq_start(dev);
167 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
168 dev->data->port_id, strerror(rte_errno));
172 dev->data->dev_started = 1;
173 ret = mlx5_rx_intr_vec_enable(dev);
175 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
179 mlx5_stats_init(dev);
180 ret = mlx5_traffic_enable(dev);
182 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
186 ret = mlx5_flow_start(dev, &priv->flows);
188 DRV_LOG(DEBUG, "port %u failed to set flows",
193 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
194 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
195 /* Enable datapath on secondary process. */
196 mlx5_mp_req_start_rxtx(dev);
197 mlx5_dev_interrupt_handler_install(dev);
200 ret = rte_errno; /* Save rte_errno before cleanup. */
202 dev->data->dev_started = 0;
203 mlx5_flow_stop(dev, &priv->flows);
204 mlx5_traffic_disable(dev);
207 rte_errno = ret; /* Restore rte_errno. */
212 * DPDK callback to stop the device.
214 * Simulate device stop by detaching all configured flows.
217 * Pointer to Ethernet device structure.
220 mlx5_dev_stop(struct rte_eth_dev *dev)
222 struct mlx5_priv *priv = dev->data->dev_private;
224 dev->data->dev_started = 0;
225 /* Prevent crashes when queues are still in use. */
226 dev->rx_pkt_burst = removed_rx_burst;
227 dev->tx_pkt_burst = removed_tx_burst;
229 /* Disable datapath on secondary process. */
230 mlx5_mp_req_stop_rxtx(dev);
231 usleep(1000 * priv->rxqs_n);
232 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
233 mlx5_flow_stop(dev, &priv->flows);
234 mlx5_traffic_disable(dev);
235 mlx5_rx_intr_vec_disable(dev);
236 mlx5_dev_interrupt_handler_uninstall(dev);
242 * Enable traffic flows configured by control plane
245 * Pointer to Ethernet device private data.
247 * Pointer to Ethernet device structure.
250 * 0 on success, a negative errno value otherwise and rte_errno is set.
253 mlx5_traffic_enable(struct rte_eth_dev *dev)
255 struct mlx5_priv *priv = dev->data->dev_private;
256 struct rte_flow_item_eth bcast = {
257 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
259 struct rte_flow_item_eth ipv6_multi_spec = {
260 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
262 struct rte_flow_item_eth ipv6_multi_mask = {
263 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
265 struct rte_flow_item_eth unicast = {
266 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
268 struct rte_flow_item_eth unicast_mask = {
269 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
271 const unsigned int vlan_filter_n = priv->vlan_filter_n;
272 const struct rte_ether_addr cmp = {
273 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
281 if (dev->data->promiscuous) {
282 struct rte_flow_item_eth promisc = {
283 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
284 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
288 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
292 if (dev->data->all_multicast) {
293 struct rte_flow_item_eth multicast = {
294 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
295 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
299 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
303 /* Add broadcast/multicast flows. */
304 for (i = 0; i != vlan_filter_n; ++i) {
305 uint16_t vlan = priv->vlan_filter[i];
307 struct rte_flow_item_vlan vlan_spec = {
308 .tci = rte_cpu_to_be_16(vlan),
310 struct rte_flow_item_vlan vlan_mask =
311 rte_flow_item_vlan_mask;
313 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
314 &vlan_spec, &vlan_mask);
317 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
319 &vlan_spec, &vlan_mask);
323 if (!vlan_filter_n) {
324 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
327 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
333 /* Add MAC address flows. */
334 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
335 struct rte_ether_addr *mac = &dev->data->mac_addrs[i];
337 if (!memcmp(mac, &cmp, sizeof(*mac)))
339 memcpy(&unicast.dst.addr_bytes,
342 for (j = 0; j != vlan_filter_n; ++j) {
343 uint16_t vlan = priv->vlan_filter[j];
345 struct rte_flow_item_vlan vlan_spec = {
346 .tci = rte_cpu_to_be_16(vlan),
348 struct rte_flow_item_vlan vlan_mask =
349 rte_flow_item_vlan_mask;
351 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
358 if (!vlan_filter_n) {
359 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
366 ret = rte_errno; /* Save rte_errno before cleanup. */
367 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
368 rte_errno = ret; /* Restore rte_errno. */
374 * Disable traffic flows configured by control plane
377 * Pointer to Ethernet device private data.
380 mlx5_traffic_disable(struct rte_eth_dev *dev)
382 struct mlx5_priv *priv = dev->data->dev_private;
384 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
388 * Restart traffic flows configured by control plane
391 * Pointer to Ethernet device private data.
394 * 0 on success, a negative errno value otherwise and rte_errno is set.
397 mlx5_traffic_restart(struct rte_eth_dev *dev)
399 if (dev->data->dev_started) {
400 mlx5_traffic_disable(dev);
401 return mlx5_traffic_enable(dev);