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, MLX5_TXQ_OBJ_TYPE_IBV);
71 ret = rte_errno; /* Save rte_errno before cleanup. */
73 mlx5_txq_release(dev, i);
75 rte_errno = ret; /* Restore rte_errno. */
80 * Stop traffic on Rx queues.
83 * Pointer to Ethernet device structure.
86 mlx5_rxq_stop(struct rte_eth_dev *dev)
88 struct mlx5_priv *priv = dev->data->dev_private;
91 for (i = 0; i != priv->rxqs_n; ++i)
92 mlx5_rxq_release(dev, i);
96 * Start traffic on Rx queues.
99 * Pointer to Ethernet device structure.
102 * 0 on success, a negative errno value otherwise and rte_errno is set.
105 mlx5_rxq_start(struct rte_eth_dev *dev)
107 struct mlx5_priv *priv = dev->data->dev_private;
110 enum mlx5_rxq_obj_type obj_type = MLX5_RXQ_OBJ_TYPE_IBV;
111 struct mlx5_rxq_data *rxq = NULL;
113 for (i = 0; i < priv->rxqs_n; ++i) {
114 rxq = (*priv->rxqs)[i];
116 if (rxq && rxq->lro) {
117 obj_type = MLX5_RXQ_OBJ_TYPE_DEVX_RQ;
121 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
122 if (mlx5_mprq_alloc_mp(dev)) {
123 /* Should not release Rx queues but return immediately. */
126 for (i = 0; i != priv->rxqs_n; ++i) {
127 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
128 struct rte_mempool *mp;
132 if (rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN) {
133 rxq_ctrl->obj = mlx5_rxq_obj_new
134 (dev, i, MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN);
139 /* Pre-register Rx mempool. */
140 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
141 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
143 "port %u Rx queue %u registering"
144 " mp %s having %u chunks",
145 dev->data->port_id, rxq_ctrl->rxq.idx,
146 mp->name, mp->nb_mem_chunks);
147 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
148 ret = rxq_alloc_elts(rxq_ctrl);
151 rxq_ctrl->obj = mlx5_rxq_obj_new(dev, i, obj_type);
154 if (obj_type == MLX5_RXQ_OBJ_TYPE_IBV)
155 rxq_ctrl->wqn = rxq_ctrl->obj->wq->wq_num;
156 else if (obj_type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ)
157 rxq_ctrl->wqn = rxq_ctrl->obj->rq->id;
161 ret = rte_errno; /* Save rte_errno before cleanup. */
163 mlx5_rxq_release(dev, i);
165 rte_errno = ret; /* Restore rte_errno. */
170 * Binds Tx queues to Rx queues for hairpin.
172 * Binds Tx queues to the target Rx queues.
175 * Pointer to Ethernet device structure.
178 * 0 on success, a negative errno value otherwise and rte_errno is set.
181 mlx5_hairpin_bind(struct rte_eth_dev *dev)
183 struct mlx5_priv *priv = dev->data->dev_private;
184 struct mlx5_devx_modify_sq_attr sq_attr = { 0 };
185 struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
186 struct mlx5_txq_ctrl *txq_ctrl;
187 struct mlx5_rxq_ctrl *rxq_ctrl;
188 struct mlx5_devx_obj *sq;
189 struct mlx5_devx_obj *rq;
193 for (i = 0; i != priv->txqs_n; ++i) {
194 txq_ctrl = mlx5_txq_get(dev, i);
197 if (txq_ctrl->type != MLX5_TXQ_TYPE_HAIRPIN) {
198 mlx5_txq_release(dev, i);
201 if (!txq_ctrl->obj) {
203 DRV_LOG(ERR, "port %u no txq object found: %d",
204 dev->data->port_id, i);
205 mlx5_txq_release(dev, i);
208 sq = txq_ctrl->obj->sq;
209 rxq_ctrl = mlx5_rxq_get(dev,
210 txq_ctrl->hairpin_conf.peers[0].queue);
212 mlx5_txq_release(dev, i);
214 DRV_LOG(ERR, "port %u no rxq object found: %d",
216 txq_ctrl->hairpin_conf.peers[0].queue);
219 if (rxq_ctrl->type != MLX5_RXQ_TYPE_HAIRPIN ||
220 rxq_ctrl->hairpin_conf.peers[0].queue != i) {
222 DRV_LOG(ERR, "port %u Tx queue %d can't be binded to "
223 "Rx queue %d", dev->data->port_id,
224 i, txq_ctrl->hairpin_conf.peers[0].queue);
227 rq = rxq_ctrl->obj->rq;
230 DRV_LOG(ERR, "port %u hairpin no matching rxq: %d",
232 txq_ctrl->hairpin_conf.peers[0].queue);
235 sq_attr.state = MLX5_SQC_STATE_RDY;
236 sq_attr.sq_state = MLX5_SQC_STATE_RST;
237 sq_attr.hairpin_peer_rq = rq->id;
238 sq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
239 ret = mlx5_devx_cmd_modify_sq(sq, &sq_attr);
242 rq_attr.state = MLX5_SQC_STATE_RDY;
243 rq_attr.rq_state = MLX5_SQC_STATE_RST;
244 rq_attr.hairpin_peer_sq = sq->id;
245 rq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
246 ret = mlx5_devx_cmd_modify_rq(rq, &rq_attr);
249 mlx5_txq_release(dev, i);
250 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
254 mlx5_txq_release(dev, i);
255 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
260 * DPDK callback to start the device.
262 * Simulate device start by attaching all configured flows.
265 * Pointer to Ethernet device structure.
268 * 0 on success, a negative errno value otherwise and rte_errno is set.
271 mlx5_dev_start(struct rte_eth_dev *dev)
276 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
277 fine_inline = rte_mbuf_dynflag_lookup
278 (RTE_PMD_MLX5_FINE_GRANULARITY_INLINE, NULL);
280 rte_net_mlx5_dynf_inline_mask = 1UL << fine_inline;
282 rte_net_mlx5_dynf_inline_mask = 0;
283 if (dev->data->nb_rx_queues > 0) {
284 ret = mlx5_dev_configure_rss_reta(dev);
286 DRV_LOG(ERR, "port %u reta config failed: %s",
287 dev->data->port_id, strerror(rte_errno));
291 ret = mlx5_txq_start(dev);
293 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
294 dev->data->port_id, strerror(rte_errno));
297 ret = mlx5_rxq_start(dev);
299 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
300 dev->data->port_id, strerror(rte_errno));
304 ret = mlx5_hairpin_bind(dev);
306 DRV_LOG(ERR, "port %u hairpin binding failed: %s",
307 dev->data->port_id, strerror(rte_errno));
311 /* Set started flag here for the following steps like control flow. */
312 dev->data->dev_started = 1;
313 ret = mlx5_rx_intr_vec_enable(dev);
315 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
319 mlx5_stats_init(dev);
320 ret = mlx5_traffic_enable(dev);
322 DRV_LOG(ERR, "port %u failed to set defaults flows",
326 /* Set a mask and offset of dynamic metadata flows into Rx queues*/
327 mlx5_flow_rxq_dynf_metadata_set(dev);
329 * In non-cached mode, it only needs to start the default mreg copy
330 * action and no flow created by application exists anymore.
331 * But it is worth wrapping the interface for further usage.
333 ret = mlx5_flow_start_default(dev);
335 DRV_LOG(DEBUG, "port %u failed to start default actions: %s",
336 dev->data->port_id, strerror(rte_errno));
340 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
341 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
342 /* Enable datapath on secondary process. */
343 mlx5_mp_req_start_rxtx(dev);
344 mlx5_dev_interrupt_handler_install(dev);
347 ret = rte_errno; /* Save rte_errno before cleanup. */
349 dev->data->dev_started = 0;
350 mlx5_flow_stop_default(dev);
351 mlx5_traffic_disable(dev);
354 rte_errno = ret; /* Restore rte_errno. */
359 * DPDK callback to stop the device.
361 * Simulate device stop by detaching all configured flows.
364 * Pointer to Ethernet device structure.
367 mlx5_dev_stop(struct rte_eth_dev *dev)
369 struct mlx5_priv *priv = dev->data->dev_private;
371 dev->data->dev_started = 0;
372 /* Prevent crashes when queues are still in use. */
373 dev->rx_pkt_burst = removed_rx_burst;
374 dev->tx_pkt_burst = removed_tx_burst;
376 /* Disable datapath on secondary process. */
377 mlx5_mp_req_stop_rxtx(dev);
378 usleep(1000 * priv->rxqs_n);
379 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
380 mlx5_flow_stop_default(dev);
381 /* Control flows for default traffic can be removed firstly. */
382 mlx5_traffic_disable(dev);
383 /* All RX queue flags will be cleared in the flush interface. */
384 mlx5_flow_list_flush(dev, &priv->flows, true);
385 mlx5_rx_intr_vec_disable(dev);
386 mlx5_dev_interrupt_handler_uninstall(dev);
392 * Enable traffic flows configured by control plane
395 * Pointer to Ethernet device private data.
397 * Pointer to Ethernet device structure.
400 * 0 on success, a negative errno value otherwise and rte_errno is set.
403 mlx5_traffic_enable(struct rte_eth_dev *dev)
405 struct mlx5_priv *priv = dev->data->dev_private;
406 struct rte_flow_item_eth bcast = {
407 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
409 struct rte_flow_item_eth ipv6_multi_spec = {
410 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
412 struct rte_flow_item_eth ipv6_multi_mask = {
413 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
415 struct rte_flow_item_eth unicast = {
416 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
418 struct rte_flow_item_eth unicast_mask = {
419 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
421 const unsigned int vlan_filter_n = priv->vlan_filter_n;
422 const struct rte_ether_addr cmp = {
423 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
430 * Hairpin txq default flow should be created no matter if it is
431 * isolation mode. Or else all the packets to be sent will be sent
432 * out directly without the TX flow actions, e.g. encapsulation.
434 for (i = 0; i != priv->txqs_n; ++i) {
435 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
438 if (txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN) {
439 ret = mlx5_ctrl_flow_source_queue(dev, i);
441 mlx5_txq_release(dev, i);
445 mlx5_txq_release(dev, i);
447 if (priv->config.dv_esw_en && !priv->config.vf) {
448 if (mlx5_flow_create_esw_table_zero_flow(dev))
449 priv->fdb_def_rule = 1;
451 DRV_LOG(INFO, "port %u FDB default rule cannot be"
452 " configured - only Eswitch group 0 flows are"
453 " supported.", dev->data->port_id);
457 if (dev->data->promiscuous) {
458 struct rte_flow_item_eth promisc = {
459 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
460 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
464 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
468 if (dev->data->all_multicast) {
469 struct rte_flow_item_eth multicast = {
470 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
471 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
475 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
479 /* Add broadcast/multicast flows. */
480 for (i = 0; i != vlan_filter_n; ++i) {
481 uint16_t vlan = priv->vlan_filter[i];
483 struct rte_flow_item_vlan vlan_spec = {
484 .tci = rte_cpu_to_be_16(vlan),
486 struct rte_flow_item_vlan vlan_mask =
487 rte_flow_item_vlan_mask;
489 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
490 &vlan_spec, &vlan_mask);
493 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
495 &vlan_spec, &vlan_mask);
499 if (!vlan_filter_n) {
500 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
503 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
509 /* Add MAC address flows. */
510 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
511 struct rte_ether_addr *mac = &dev->data->mac_addrs[i];
513 if (!memcmp(mac, &cmp, sizeof(*mac)))
515 memcpy(&unicast.dst.addr_bytes,
518 for (j = 0; j != vlan_filter_n; ++j) {
519 uint16_t vlan = priv->vlan_filter[j];
521 struct rte_flow_item_vlan vlan_spec = {
522 .tci = rte_cpu_to_be_16(vlan),
524 struct rte_flow_item_vlan vlan_mask =
525 rte_flow_item_vlan_mask;
527 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
534 if (!vlan_filter_n) {
535 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
542 ret = rte_errno; /* Save rte_errno before cleanup. */
543 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
544 rte_errno = ret; /* Restore rte_errno. */
550 * Disable traffic flows configured by control plane
553 * Pointer to Ethernet device private data.
556 mlx5_traffic_disable(struct rte_eth_dev *dev)
558 struct mlx5_priv *priv = dev->data->dev_private;
560 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
564 * Restart traffic flows configured by control plane
567 * Pointer to Ethernet device private data.
570 * 0 on success, a negative errno value otherwise and rte_errno is set.
573 mlx5_traffic_restart(struct rte_eth_dev *dev)
575 if (dev->data->dev_started) {
576 mlx5_traffic_disable(dev);
577 return mlx5_traffic_enable(dev);