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];
115 if (rxq && rxq->lro) {
116 obj_type = MLX5_RXQ_OBJ_TYPE_DEVX_RQ;
120 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
121 if (mlx5_mprq_alloc_mp(dev)) {
122 /* Should not release Rx queues but return immediately. */
125 for (i = 0; i != priv->rxqs_n; ++i) {
126 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
127 struct rte_mempool *mp;
131 if (rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN) {
132 rxq_ctrl->obj = mlx5_rxq_obj_new
133 (dev, i, MLX5_RXQ_OBJ_TYPE_DEVX_HAIRPIN);
138 /* Pre-register Rx mempool. */
139 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
140 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
142 "port %u Rx queue %u registering"
143 " mp %s having %u chunks",
144 dev->data->port_id, rxq_ctrl->rxq.idx,
145 mp->name, mp->nb_mem_chunks);
146 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
147 ret = rxq_alloc_elts(rxq_ctrl);
150 rxq_ctrl->obj = mlx5_rxq_obj_new(dev, i, obj_type);
153 if (obj_type == MLX5_RXQ_OBJ_TYPE_IBV)
154 rxq_ctrl->wqn = rxq_ctrl->obj->wq->wq_num;
155 else if (obj_type == MLX5_RXQ_OBJ_TYPE_DEVX_RQ)
156 rxq_ctrl->wqn = rxq_ctrl->obj->rq->id;
160 ret = rte_errno; /* Save rte_errno before cleanup. */
162 mlx5_rxq_release(dev, i);
164 rte_errno = ret; /* Restore rte_errno. */
169 * Binds Tx queues to Rx queues for hairpin.
171 * Binds Tx queues to the target Rx queues.
174 * Pointer to Ethernet device structure.
177 * 0 on success, a negative errno value otherwise and rte_errno is set.
180 mlx5_hairpin_bind(struct rte_eth_dev *dev)
182 struct mlx5_priv *priv = dev->data->dev_private;
183 struct mlx5_devx_modify_sq_attr sq_attr = { 0 };
184 struct mlx5_devx_modify_rq_attr rq_attr = { 0 };
185 struct mlx5_txq_ctrl *txq_ctrl;
186 struct mlx5_rxq_ctrl *rxq_ctrl;
187 struct mlx5_devx_obj *sq;
188 struct mlx5_devx_obj *rq;
192 for (i = 0; i != priv->txqs_n; ++i) {
193 txq_ctrl = mlx5_txq_get(dev, i);
196 if (txq_ctrl->type != MLX5_TXQ_TYPE_HAIRPIN) {
197 mlx5_txq_release(dev, i);
200 if (!txq_ctrl->obj) {
202 DRV_LOG(ERR, "port %u no txq object found: %d",
203 dev->data->port_id, i);
204 mlx5_txq_release(dev, i);
207 sq = txq_ctrl->obj->sq;
208 rxq_ctrl = mlx5_rxq_get(dev,
209 txq_ctrl->hairpin_conf.peers[0].queue);
211 mlx5_txq_release(dev, i);
213 DRV_LOG(ERR, "port %u no rxq object found: %d",
215 txq_ctrl->hairpin_conf.peers[0].queue);
218 if (rxq_ctrl->type != MLX5_RXQ_TYPE_HAIRPIN ||
219 rxq_ctrl->hairpin_conf.peers[0].queue != i) {
221 DRV_LOG(ERR, "port %u Tx queue %d can't be binded to "
222 "Rx queue %d", dev->data->port_id,
223 i, txq_ctrl->hairpin_conf.peers[0].queue);
226 rq = rxq_ctrl->obj->rq;
229 DRV_LOG(ERR, "port %u hairpin no matching rxq: %d",
231 txq_ctrl->hairpin_conf.peers[0].queue);
234 sq_attr.state = MLX5_SQC_STATE_RDY;
235 sq_attr.sq_state = MLX5_SQC_STATE_RST;
236 sq_attr.hairpin_peer_rq = rq->id;
237 sq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
238 ret = mlx5_devx_cmd_modify_sq(sq, &sq_attr);
241 rq_attr.state = MLX5_SQC_STATE_RDY;
242 rq_attr.rq_state = MLX5_SQC_STATE_RST;
243 rq_attr.hairpin_peer_sq = sq->id;
244 rq_attr.hairpin_peer_vhca = priv->config.hca_attr.vhca_id;
245 ret = mlx5_devx_cmd_modify_rq(rq, &rq_attr);
248 mlx5_txq_release(dev, i);
249 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
253 mlx5_txq_release(dev, i);
254 mlx5_rxq_release(dev, txq_ctrl->hairpin_conf.peers[0].queue);
259 * DPDK callback to start the device.
261 * Simulate device start by attaching all configured flows.
264 * Pointer to Ethernet device structure.
267 * 0 on success, a negative errno value otherwise and rte_errno is set.
270 mlx5_dev_start(struct rte_eth_dev *dev)
272 struct mlx5_priv *priv = dev->data->dev_private;
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_os_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 if (priv->sh->intr_handle.fd >= 0) {
345 priv->sh->port[priv->dev_port - 1].ih_port_id =
346 (uint32_t)dev->data->port_id;
348 DRV_LOG(INFO, "port %u starts without LSC and RMV interrupts.",
350 dev->data->dev_conf.intr_conf.lsc = 0;
351 dev->data->dev_conf.intr_conf.rmv = 0;
353 if (priv->sh->intr_handle_devx.fd >= 0)
354 priv->sh->port[priv->dev_port - 1].devx_ih_port_id =
355 (uint32_t)dev->data->port_id;
358 ret = rte_errno; /* Save rte_errno before cleanup. */
360 dev->data->dev_started = 0;
361 mlx5_flow_stop_default(dev);
362 mlx5_traffic_disable(dev);
365 rte_errno = ret; /* Restore rte_errno. */
370 * DPDK callback to stop the device.
372 * Simulate device stop by detaching all configured flows.
375 * Pointer to Ethernet device structure.
378 mlx5_dev_stop(struct rte_eth_dev *dev)
380 struct mlx5_priv *priv = dev->data->dev_private;
382 dev->data->dev_started = 0;
383 /* Prevent crashes when queues are still in use. */
384 dev->rx_pkt_burst = removed_rx_burst;
385 dev->tx_pkt_burst = removed_tx_burst;
387 /* Disable datapath on secondary process. */
388 mlx5_mp_req_stop_rxtx(dev);
389 usleep(1000 * priv->rxqs_n);
390 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
391 mlx5_flow_stop_default(dev);
392 /* Control flows for default traffic can be removed firstly. */
393 mlx5_traffic_disable(dev);
394 /* All RX queue flags will be cleared in the flush interface. */
395 mlx5_flow_list_flush(dev, &priv->flows, true);
396 mlx5_rx_intr_vec_disable(dev);
397 priv->sh->port[priv->dev_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
398 priv->sh->port[priv->dev_port - 1].devx_ih_port_id = RTE_MAX_ETHPORTS;
404 * Enable traffic flows configured by control plane
407 * Pointer to Ethernet device private data.
409 * Pointer to Ethernet device structure.
412 * 0 on success, a negative errno value otherwise and rte_errno is set.
415 mlx5_traffic_enable(struct rte_eth_dev *dev)
417 struct mlx5_priv *priv = dev->data->dev_private;
418 struct rte_flow_item_eth bcast = {
419 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
421 struct rte_flow_item_eth ipv6_multi_spec = {
422 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
424 struct rte_flow_item_eth ipv6_multi_mask = {
425 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
427 struct rte_flow_item_eth unicast = {
428 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
430 struct rte_flow_item_eth unicast_mask = {
431 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
433 const unsigned int vlan_filter_n = priv->vlan_filter_n;
434 const struct rte_ether_addr cmp = {
435 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
442 * Hairpin txq default flow should be created no matter if it is
443 * isolation mode. Or else all the packets to be sent will be sent
444 * out directly without the TX flow actions, e.g. encapsulation.
446 for (i = 0; i != priv->txqs_n; ++i) {
447 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
450 if (txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN) {
451 ret = mlx5_ctrl_flow_source_queue(dev, i);
453 mlx5_txq_release(dev, i);
457 mlx5_txq_release(dev, i);
459 if (priv->config.dv_esw_en && !priv->config.vf) {
460 if (mlx5_flow_create_esw_table_zero_flow(dev))
461 priv->fdb_def_rule = 1;
463 DRV_LOG(INFO, "port %u FDB default rule cannot be"
464 " configured - only Eswitch group 0 flows are"
465 " supported.", dev->data->port_id);
469 if (dev->data->promiscuous) {
470 struct rte_flow_item_eth promisc = {
471 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
472 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
476 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
480 if (dev->data->all_multicast) {
481 struct rte_flow_item_eth multicast = {
482 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
483 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
487 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
491 /* Add broadcast/multicast flows. */
492 for (i = 0; i != vlan_filter_n; ++i) {
493 uint16_t vlan = priv->vlan_filter[i];
495 struct rte_flow_item_vlan vlan_spec = {
496 .tci = rte_cpu_to_be_16(vlan),
498 struct rte_flow_item_vlan vlan_mask =
499 rte_flow_item_vlan_mask;
501 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
502 &vlan_spec, &vlan_mask);
505 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
507 &vlan_spec, &vlan_mask);
511 if (!vlan_filter_n) {
512 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
515 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
521 /* Add MAC address flows. */
522 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
523 struct rte_ether_addr *mac = &dev->data->mac_addrs[i];
525 if (!memcmp(mac, &cmp, sizeof(*mac)))
527 memcpy(&unicast.dst.addr_bytes,
530 for (j = 0; j != vlan_filter_n; ++j) {
531 uint16_t vlan = priv->vlan_filter[j];
533 struct rte_flow_item_vlan vlan_spec = {
534 .tci = rte_cpu_to_be_16(vlan),
536 struct rte_flow_item_vlan vlan_mask =
537 rte_flow_item_vlan_mask;
539 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
546 if (!vlan_filter_n) {
547 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
554 ret = rte_errno; /* Save rte_errno before cleanup. */
555 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
556 rte_errno = ret; /* Restore rte_errno. */
562 * Disable traffic flows configured by control plane
565 * Pointer to Ethernet device private data.
568 mlx5_traffic_disable(struct rte_eth_dev *dev)
570 struct mlx5_priv *priv = dev->data->dev_private;
572 mlx5_flow_list_flush(dev, &priv->ctrl_flows, false);
576 * Restart traffic flows configured by control plane
579 * Pointer to Ethernet device private data.
582 * 0 on success, a negative errno value otherwise and rte_errno is set.
585 mlx5_traffic_restart(struct rte_eth_dev *dev)
587 if (dev->data->dev_started) {
588 mlx5_traffic_disable(dev);
589 return mlx5_traffic_enable(dev);