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>
15 #include "mlx5_rxtx.h"
16 #include "mlx5_utils.h"
17 #include "rte_pmd_mlx5.h"
20 * Stop traffic on Tx queues.
23 * Pointer to Ethernet device structure.
26 mlx5_txq_stop(struct rte_eth_dev *dev)
28 struct mlx5_priv *priv = dev->data->dev_private;
31 for (i = 0; i != priv->txqs_n; ++i)
32 mlx5_txq_release(dev, i);
36 * Start traffic on Tx queues.
39 * Pointer to Ethernet device structure.
42 * 0 on success, a negative errno value otherwise and rte_errno is set.
45 mlx5_txq_start(struct rte_eth_dev *dev)
47 struct mlx5_priv *priv = dev->data->dev_private;
51 for (i = 0; i != priv->txqs_n; ++i) {
52 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
56 if (txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN) {
57 txq_ctrl->obj = mlx5_txq_obj_new
58 (dev, i, MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN);
60 txq_alloc_elts(txq_ctrl);
61 txq_ctrl->obj = mlx5_txq_obj_new
62 (dev, i, priv->txpp_en ?
63 MLX5_TXQ_OBJ_TYPE_DEVX_SQ :
64 MLX5_TXQ_OBJ_TYPE_IBV);
73 ret = rte_errno; /* Save rte_errno before cleanup. */
75 mlx5_txq_release(dev, i);
77 rte_errno = ret; /* Restore rte_errno. */
82 * Stop traffic on Rx queues.
85 * Pointer to Ethernet device structure.
88 mlx5_rxq_stop(struct rte_eth_dev *dev)
90 struct mlx5_priv *priv = dev->data->dev_private;
93 for (i = 0; i != priv->rxqs_n; ++i)
94 mlx5_rxq_release(dev, i);
98 * Start traffic on Rx queues.
101 * Pointer to Ethernet device structure.
104 * 0 on success, a negative errno value otherwise and rte_errno is set.
107 mlx5_rxq_start(struct rte_eth_dev *dev)
109 struct mlx5_priv *priv = dev->data->dev_private;
112 enum mlx5_rxq_obj_type obj_type = MLX5_RXQ_OBJ_TYPE_IBV;
113 struct mlx5_rxq_data *rxq = NULL;
115 for (i = 0; i < priv->rxqs_n; ++i) {
116 rxq = (*priv->rxqs)[i];
117 if (rxq && rxq->lro) {
118 obj_type = MLX5_RXQ_OBJ_TYPE_DEVX_RQ;
122 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
123 if (mlx5_mprq_alloc_mp(dev)) {
124 /* Should not release Rx queues but return immediately. */
127 for (i = 0; i != priv->rxqs_n; ++i) {
128 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
129 struct rte_mempool *mp;
133 if (rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN) {
134 rxq_ctrl->obj = mlx5_rxq_obj_new
135 (dev, i, MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN);
140 /* Pre-register Rx mempool. */
141 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
142 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
144 "port %u Rx queue %u registering"
145 " mp %s having %u chunks",
146 dev->data->port_id, rxq_ctrl->rxq.idx,
147 mp->name, mp->nb_mem_chunks);
148 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
149 ret = rxq_alloc_elts(rxq_ctrl);
152 rxq_ctrl->obj = mlx5_rxq_obj_new(dev, i, obj_type);
155 if (obj_type == MLX5_RXQ_OBJ_TYPE_IBV)
156 rxq_ctrl->wqn = rxq_ctrl->obj->wq->wq_num;
157 else if (obj_type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ)
158 rxq_ctrl->wqn = rxq_ctrl->obj->rq->id;
162 ret = rte_errno; /* Save rte_errno before cleanup. */
164 mlx5_rxq_release(dev, i);
166 rte_errno = ret; /* Restore rte_errno. */
171 * Binds Tx queues to Rx queues for hairpin.
173 * Binds Tx queues to the target Rx queues.
176 * Pointer to Ethernet device structure.
179 * 0 on success, a negative errno value otherwise and rte_errno is set.
182 mlx5_hairpin_bind(struct rte_eth_dev *dev)
184 struct mlx5_priv *priv = dev->data->dev_private;
185 struct mlx5_devx_modify_sq_attr sq_attr = { 0 };
186 struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
187 struct mlx5_txq_ctrl *txq_ctrl;
188 struct mlx5_rxq_ctrl *rxq_ctrl;
189 struct mlx5_devx_obj *sq;
190 struct mlx5_devx_obj *rq;
194 for (i = 0; i != priv->txqs_n; ++i) {
195 txq_ctrl = mlx5_txq_get(dev, i);
198 if (txq_ctrl->type != MLX5_TXQ_TYPE_HAIRPIN) {
199 mlx5_txq_release(dev, i);
202 if (!txq_ctrl->obj) {
204 DRV_LOG(ERR, "port %u no txq object found: %d",
205 dev->data->port_id, i);
206 mlx5_txq_release(dev, i);
209 sq = txq_ctrl->obj->sq;
210 rxq_ctrl = mlx5_rxq_get(dev,
211 txq_ctrl->hairpin_conf.peers[0].queue);
213 mlx5_txq_release(dev, i);
215 DRV_LOG(ERR, "port %u no rxq object found: %d",
217 txq_ctrl->hairpin_conf.peers[0].queue);
220 if (rxq_ctrl->type != MLX5_RXQ_TYPE_HAIRPIN ||
221 rxq_ctrl->hairpin_conf.peers[0].queue != i) {
223 DRV_LOG(ERR, "port %u Tx queue %d can't be binded to "
224 "Rx queue %d", dev->data->port_id,
225 i, txq_ctrl->hairpin_conf.peers[0].queue);
228 rq = rxq_ctrl->obj->rq;
231 DRV_LOG(ERR, "port %u hairpin no matching rxq: %d",
233 txq_ctrl->hairpin_conf.peers[0].queue);
236 sq_attr.state = MLX5_SQC_STATE_RDY;
237 sq_attr.sq_state = MLX5_SQC_STATE_RST;
238 sq_attr.hairpin_peer_rq = rq->id;
239 sq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
240 ret = mlx5_devx_cmd_modify_sq(sq, &sq_attr);
243 rq_attr.state = MLX5_SQC_STATE_RDY;
244 rq_attr.rq_state = MLX5_SQC_STATE_RST;
245 rq_attr.hairpin_peer_sq = sq->id;
246 rq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
247 ret = mlx5_devx_cmd_modify_rq(rq, &rq_attr);
250 mlx5_txq_release(dev, i);
251 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
255 mlx5_txq_release(dev, i);
256 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
261 * DPDK callback to start the device.
263 * Simulate device start by attaching all configured flows.
266 * Pointer to Ethernet device structure.
269 * 0 on success, a negative errno value otherwise and rte_errno is set.
272 mlx5_dev_start(struct rte_eth_dev *dev)
274 struct mlx5_priv *priv = dev->data->dev_private;
278 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
279 fine_inline = rte_mbuf_dynflag_lookup
280 (RTE_PMD_MLX5_FINE_GRANULARITY_INLINE, NULL);
282 rte_net_mlx5_dynf_inline_mask = 1UL << fine_inline;
284 rte_net_mlx5_dynf_inline_mask = 0;
285 if (dev->data->nb_rx_queues > 0) {
286 ret = mlx5_dev_configure_rss_reta(dev);
288 DRV_LOG(ERR, "port %u reta config failed: %s",
289 dev->data->port_id, strerror(rte_errno));
293 ret = mlx5_txpp_start(dev);
295 DRV_LOG(ERR, "port %u Tx packet pacing init failed: %s",
296 dev->data->port_id, strerror(rte_errno));
299 ret = mlx5_txq_start(dev);
301 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
302 dev->data->port_id, strerror(rte_errno));
305 ret = mlx5_rxq_start(dev);
307 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
308 dev->data->port_id, strerror(rte_errno));
311 ret = mlx5_hairpin_bind(dev);
313 DRV_LOG(ERR, "port %u hairpin binding failed: %s",
314 dev->data->port_id, strerror(rte_errno));
317 /* Set started flag here for the following steps like control flow. */
318 dev->data->dev_started = 1;
319 ret = mlx5_rx_intr_vec_enable(dev);
321 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
325 mlx5_os_stats_init(dev);
326 ret = mlx5_traffic_enable(dev);
328 DRV_LOG(ERR, "port %u failed to set defaults flows",
332 /* Set a mask and offset of dynamic metadata flows into Rx queues. */
333 mlx5_flow_rxq_dynf_metadata_set(dev);
334 /* Set flags and context to convert Rx timestamps. */
335 mlx5_rxq_timestamp_set(dev);
336 /* Set a mask and offset of scheduling on timestamp into Tx queues. */
337 mlx5_txq_dynf_timestamp_set(dev);
339 * In non-cached mode, it only needs to start the default mreg copy
340 * action and no flow created by application exists anymore.
341 * But it is worth wrapping the interface for further usage.
343 ret = mlx5_flow_start_default(dev);
345 DRV_LOG(DEBUG, "port %u failed to start default actions: %s",
346 dev->data->port_id, strerror(rte_errno));
350 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
351 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
352 /* Enable datapath on secondary process. */
353 mlx5_mp_os_req_start_rxtx(dev);
354 if (priv->sh->intr_handle.fd >= 0) {
355 priv->sh->port[priv->dev_port - 1].ih_port_id =
356 (uint32_t)dev->data->port_id;
358 DRV_LOG(INFO, "port %u starts without LSC and RMV interrupts.",
360 dev->data->dev_conf.intr_conf.lsc = 0;
361 dev->data->dev_conf.intr_conf.rmv = 0;
363 if (priv->sh->intr_handle_devx.fd >= 0)
364 priv->sh->port[priv->dev_port - 1].devx_ih_port_id =
365 (uint32_t)dev->data->port_id;
368 ret = rte_errno; /* Save rte_errno before cleanup. */
370 dev->data->dev_started = 0;
371 mlx5_flow_stop_default(dev);
372 mlx5_traffic_disable(dev);
375 mlx5_txpp_stop(dev); /* Stop last. */
376 rte_errno = ret; /* Restore rte_errno. */
381 * DPDK callback to stop the device.
383 * Simulate device stop by detaching all configured flows.
386 * Pointer to Ethernet device structure.
389 mlx5_dev_stop(struct rte_eth_dev *dev)
391 struct mlx5_priv *priv = dev->data->dev_private;
393 dev->data->dev_started = 0;
394 /* Prevent crashes when queues are still in use. */
395 dev->rx_pkt_burst = removed_rx_burst;
396 dev->tx_pkt_burst = removed_tx_burst;
398 /* Disable datapath on secondary process. */
399 mlx5_mp_os_req_stop_rxtx(dev);
400 usleep(1000 * priv->rxqs_n);
401 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
402 mlx5_flow_stop_default(dev);
403 /* Control flows for default traffic can be removed firstly. */
404 mlx5_traffic_disable(dev);
405 /* All RX queue flags will be cleared in the flush interface. */
406 mlx5_flow_list_flush(dev, &priv->flows, true);
407 mlx5_rx_intr_vec_disable(dev);
408 priv->sh->port[priv->dev_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
409 priv->sh->port[priv->dev_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
416 * Enable traffic flows configured by control plane
419 * Pointer to Ethernet device private data.
421 * Pointer to Ethernet device structure.
424 * 0 on success, a negative errno value otherwise and rte_errno is set.
427 mlx5_traffic_enable(struct rte_eth_dev *dev)
429 struct mlx5_priv *priv = dev->data->dev_private;
430 struct rte_flow_item_eth bcast = {
431 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
433 struct rte_flow_item_eth ipv6_multi_spec = {
434 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
436 struct rte_flow_item_eth ipv6_multi_mask = {
437 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
439 struct rte_flow_item_eth unicast = {
440 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
442 struct rte_flow_item_eth unicast_mask = {
443 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
445 const unsigned int vlan_filter_n = priv->vlan_filter_n;
446 const struct rte_ether_addr cmp = {
447 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
454 * Hairpin txq default flow should be created no matter if it is
455 * isolation mode. Or else all the packets to be sent will be sent
456 * out directly without the TX flow actions, e.g. encapsulation.
458 for (i = 0; i != priv->txqs_n; ++i) {
459 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
462 if (txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN) {
463 ret = mlx5_ctrl_flow_source_queue(dev, i);
465 mlx5_txq_release(dev, i);
469 mlx5_txq_release(dev, i);
471 if (priv->config.dv_esw_en && !priv->config.vf) {
472 if (mlx5_flow_create_esw_table_zero_flow(dev))
473 priv->fdb_def_rule = 1;
475 DRV_LOG(INFO, "port %u FDB default rule cannot be"
476 " configured - only Eswitch group 0 flows are"
477 " supported.", dev->data->port_id);
479 if (!priv->config.lacp_by_user && priv->pf_bond >= 0) {
480 ret = mlx5_flow_lacp_miss(dev);
482 DRV_LOG(INFO, "port %u LACP rule cannot be created - "
483 "forward LACP to kernel.", dev->data->port_id);
485 DRV_LOG(INFO, "LACP traffic will be missed in port %u."
486 , dev->data->port_id);
490 if (dev->data->promiscuous) {
491 struct rte_flow_item_eth promisc = {
492 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
493 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
497 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
501 if (dev->data->all_multicast) {
502 struct rte_flow_item_eth multicast = {
503 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
504 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
508 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
512 /* Add broadcast/multicast flows. */
513 for (i = 0; i != vlan_filter_n; ++i) {
514 uint16_t vlan = priv->vlan_filter[i];
516 struct rte_flow_item_vlan vlan_spec = {
517 .tci = rte_cpu_to_be_16(vlan),
519 struct rte_flow_item_vlan vlan_mask =
520 rte_flow_item_vlan_mask;
522 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
523 &vlan_spec, &vlan_mask);
526 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
528 &vlan_spec, &vlan_mask);
532 if (!vlan_filter_n) {
533 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
536 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
542 /* Add MAC address flows. */
543 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
544 struct rte_ether_addr *mac = &dev->data->mac_addrs[i];
546 if (!memcmp(mac, &cmp, sizeof(*mac)))
548 memcpy(&unicast.dst.addr_bytes,
551 for (j = 0; j != vlan_filter_n; ++j) {
552 uint16_t vlan = priv->vlan_filter[j];
554 struct rte_flow_item_vlan vlan_spec = {
555 .tci = rte_cpu_to_be_16(vlan),
557 struct rte_flow_item_vlan vlan_mask =
558 rte_flow_item_vlan_mask;
560 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
567 if (!vlan_filter_n) {
568 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
575 ret = rte_errno; /* Save rte_errno before cleanup. */
576 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
577 rte_errno = ret; /* Restore rte_errno. */
583 * Disable traffic flows configured by control plane
586 * Pointer to Ethernet device private data.
589 mlx5_traffic_disable(struct rte_eth_dev *dev)
591 struct mlx5_priv *priv = dev->data->dev_private;
593 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
597 * Restart traffic flows configured by control plane
600 * Pointer to Ethernet device private data.
603 * 0 on success, a negative errno value otherwise and rte_errno is set.
606 mlx5_traffic_restart(struct rte_eth_dev *dev)
608 if (dev->data->dev_started) {
609 mlx5_traffic_disable(dev);
610 return mlx5_traffic_enable(dev);