/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2019 Mellanox Technologies. */

#include "health.h"
#include "en/ptp.h"
#include "en/devlink.h"
#include "lib/tout.h"

/* Keep this string array consistent with the MLX5E_SQ_STATE_* enums in en.h */
static const char * const sq_sw_state_type_name[] = {
	[MLX5E_SQ_STATE_ENABLED] = "enabled",
	[MLX5E_SQ_STATE_MPWQE] = "mpwqe",
	[MLX5E_SQ_STATE_RECOVERING] = "recovering",
	[MLX5E_SQ_STATE_IPSEC] = "ipsec",
	[MLX5E_SQ_STATE_DIM] = "dim",
	[MLX5E_SQ_STATE_VLAN_NEED_L2_INLINE] = "vlan_need_l2_inline",
	[MLX5E_SQ_STATE_PENDING_XSK_TX] = "pending_xsk_tx",
	[MLX5E_SQ_STATE_PENDING_TLS_RX_RESYNC] = "pending_tls_rx_resync",
	[MLX5E_SQ_STATE_TX_XDP_CSUM] = "tx_xdp_csum",
	[MLX5E_SQ_STATE_XDP_MULTIBUF] = "xdp_multibuf",
};

static int mlx5e_wait_for_sq_flush(struct mlx5e_txqsq *sq)
{
	struct mlx5_core_dev *dev = sq->mdev;
	unsigned long exp_time;

	exp_time = jiffies + msecs_to_jiffies(mlx5_tout_ms(dev, FLUSH_ON_ERROR));

	while (time_before(jiffies, exp_time)) {
		if (sq->cc == sq->pc)
			return 0;

		msleep(20);
	}

	netdev_err(sq->netdev,
		   "Wait for SQ 0x%x flush timeout (sq cc = 0x%x, sq pc = 0x%x)\n",
		   sq->sqn, sq->cc, sq->pc);

	return -ETIMEDOUT;
}

static void mlx5e_reset_txqsq_cc_pc(struct mlx5e_txqsq *sq)
{
	WARN_ONCE(sq->cc != sq->pc,
		  "SQ 0x%x: cc (0x%x) != pc (0x%x)\n",
		  sq->sqn, sq->cc, sq->pc);
	sq->cc = 0;
	sq->dma_fifo_cc = 0;
	sq->pc = 0;
}

#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_health_sq_put_sw_state(struct devlink_fmsg *fmsg, struct mlx5e_txqsq *sq)
{
#ifdef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	int err;
#endif
	int i;

	BUILD_BUG_ON_MSG(ARRAY_SIZE(sq_sw_state_type_name) != MLX5E_NUM_SQ_STATES,
			 "sq_sw_state_type_name string array must be consistent with MLX5E_SQ_STATE_* enum in en.h");
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SW State");
#else
	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SW State");
	if (err)
		return err;
#endif

	for (i = 0; i < ARRAY_SIZE(sq_sw_state_type_name); ++i) {
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
		devlink_fmsg_u32_pair_put(fmsg, sq_sw_state_type_name[i],
						test_bit(i, &sq->state));
#else
		err = devlink_fmsg_u32_pair_put(fmsg, sq_sw_state_type_name[i],
						test_bit(i, &sq->state));
		if (err)
			return err;
#endif
	}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#else
	return mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#endif
}
#endif

static int mlx5e_tx_reporter_err_cqe_recover(void *ctx)
{
	struct mlx5_core_dev *mdev;
	struct net_device *dev;
	struct mlx5e_txqsq *sq;
	u8 state;
	int err;

	sq = ctx;
	mdev = sq->mdev;
	dev = sq->netdev;

	if (!test_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state))
		return 0;

	err = mlx5_core_query_sq_state(mdev, sq->sqn, &state);
	if (err) {
		netdev_err(dev, "Failed to query SQ 0x%x state. err = %d\n",
			   sq->sqn, err);
		goto out;
	}

	if (state != MLX5_SQC_STATE_ERR)
		goto out;

	mlx5e_tx_disable_queue(sq->txq);

	err = mlx5e_wait_for_sq_flush(sq);
	if (err)
		goto out;

	/* At this point, no new packets will arrive from the stack as TXQ is
	 * marked with QUEUE_STATE_DRV_XOFF. In addition, NAPI cleared all
	 * pending WQEs. SQ can safely reset the SQ.
	 */

	err = mlx5e_health_sq_to_ready(mdev, dev, sq->sqn);
	if (err)
		goto out;

	mlx5e_reset_txqsq_cc_pc(sq);
	sq->stats->recover++;
	clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
	rtnl_lock();
	mlx5e_activate_txqsq(sq);
	rtnl_unlock();
	if (sq->channel)
		mlx5e_trigger_napi_icosq(sq->channel);
	else
		mlx5e_trigger_napi_sched(sq->cq.napi);

	return 0;
out:
	clear_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state);
	return err;
}

struct mlx5e_tx_timeout_ctx {
	struct mlx5e_txqsq *sq;
	signed int status;
};

static int mlx5e_tx_reporter_timeout_recover(void *ctx)
{
	struct mlx5e_tx_timeout_ctx *to_ctx;
	struct mlx5e_priv *priv;
	struct mlx5_eq_comp *eq;
	struct mlx5e_txqsq *sq;
	int err;

	to_ctx = ctx;
	sq = to_ctx->sq;
	eq = sq->cq.mcq.eq;
	priv = sq->priv;
	err = mlx5e_health_channel_eq_recover(sq->netdev, eq, sq->cq.ch_stats);
	if (!err) {
		to_ctx->status = 0; /* this sq recovered */
		return err;
	}

	mutex_lock(&priv->state_lock);
	err = mlx5e_safe_reopen_channels(priv);
	mutex_unlock(&priv->state_lock);
	if (!err) {
		to_ctx->status = 1; /* all channels recovered */
		return err;
	}

	to_ctx->status = err;
	clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state);
	netdev_err(priv->netdev,
		   "mlx5e_safe_reopen_channels failed recovering from a tx_timeout, err(%d).\n",
		   err);

	return err;
}

#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
static int mlx5e_tx_reporter_ptpsq_unhealthy_recover(void *ctx)
{
	struct mlx5e_ptpsq *ptpsq = ctx;
	struct mlx5e_channels *chs;
	struct net_device *netdev;
	struct mlx5e_priv *priv;
	int carrier_ok;
	int err;

	if (!test_bit(MLX5E_SQ_STATE_RECOVERING, &ptpsq->txqsq.state))
		return 0;

	priv = ptpsq->txqsq.priv;

	mutex_lock(&priv->state_lock);
	chs = &priv->channels;
	netdev = priv->netdev;

	carrier_ok = netif_carrier_ok(netdev);
	netif_carrier_off(netdev);

	rtnl_lock();
	mlx5e_deactivate_priv_channels(priv);
	rtnl_unlock();

	mlx5e_ptp_close(chs->ptp);
	err = mlx5e_ptp_open(priv, &chs->params, chs->c[0]->lag_port, &chs->ptp);

	rtnl_lock();
	mlx5e_activate_priv_channels(priv);
	rtnl_unlock();

	/* return carrier back if needed */
	if (carrier_ok)
		netif_carrier_on(netdev);

	mutex_unlock(&priv->state_lock);

	return err;
}

/* state lock cannot be grabbed within this function.
 * It can cause a dead lock or a read-after-free.
 */
static int mlx5e_tx_reporter_recover_from_ctx(struct mlx5e_err_ctx *err_ctx)
{
	return err_ctx->recover(err_ctx->ctx);
}

static int mlx5e_tx_reporter_recover(struct devlink_health_reporter *reporter,
		                     void *context
#ifdef HAVE_HEALTH_REPORTER_RECOVER_HAS_EXTACK
                                    , struct netlink_ext_ack *extack
#endif
                                    )
{
	struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
	struct mlx5e_err_ctx *err_ctx = context;

	return err_ctx ? mlx5e_tx_reporter_recover_from_ctx(err_ctx) :
			 mlx5e_health_recover_channels(priv);
}
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_build_diagnose_output_sq_common(struct devlink_fmsg *fmsg,
						  struct mlx5e_txqsq *sq, int tc)
{
	bool stopped = netif_xmit_stopped(sq->txq);
	u8 state;
	int err;

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	devlink_fmsg_u32_pair_put(fmsg, "tc", tc);
	devlink_fmsg_u32_pair_put(fmsg, "txq ix", sq->txq_ix);
	devlink_fmsg_u32_pair_put(fmsg, "sqn", sq->sqn);

	err = mlx5_core_query_sq_state(sq->mdev, sq->sqn, &state);
	if (!err)
		devlink_fmsg_u8_pair_put(fmsg, "HW state", state);

	devlink_fmsg_bool_pair_put(fmsg, "stopped", stopped);
	devlink_fmsg_u32_pair_put(fmsg, "cc", sq->cc);
	devlink_fmsg_u32_pair_put(fmsg, "pc", sq->pc);
	mlx5e_health_sq_put_sw_state(fmsg, sq);
	mlx5e_health_cq_diag_fmsg(&sq->cq, fmsg);
	mlx5e_health_eq_diag_fmsg(sq->cq.mcq.eq, fmsg);
#else
	err = mlx5_core_query_sq_state(sq->mdev, sq->sqn, &state);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "tc", tc);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "txq ix", sq->txq_ix);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "sqn", sq->sqn);
	if (err)
		return err;

	err = devlink_fmsg_u8_pair_put(fmsg, "HW state", state);
	if (err)
		return err;

	err = devlink_fmsg_bool_pair_put(fmsg, "stopped", stopped);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "cc", sq->cc);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "pc", sq->pc);
	if (err)
		return err;

	err = mlx5e_health_sq_put_sw_state(fmsg, sq);
	if (err)
		return err;

	err = mlx5e_health_cq_diag_fmsg(&sq->cq, fmsg);
	if (err)
		return err;

	return mlx5e_health_eq_diag_fmsg(sq->cq.mcq.eq, fmsg);
#endif
}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_build_diagnose_output(struct devlink_fmsg *fmsg,
					struct mlx5e_txqsq *sq, int tc)
{
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	devlink_fmsg_obj_nest_start(fmsg);
	devlink_fmsg_u32_pair_put(fmsg, "channel ix", sq->ch_ix);
	mlx5e_tx_reporter_build_diagnose_output_sq_common(fmsg, sq, tc);
	devlink_fmsg_obj_nest_end(fmsg);
#else
	int err;

	err = devlink_fmsg_obj_nest_start(fmsg);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "channel ix", sq->ch_ix);
	if (err)
		return err;

	err = mlx5e_tx_reporter_build_diagnose_output_sq_common(fmsg, sq, tc);
	if (err)
		return err;

	err = devlink_fmsg_obj_nest_end(fmsg);
	if (err)
		return err;

	return 0;
#endif
}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_build_diagnose_output_ptpsq(struct devlink_fmsg *fmsg,
					      struct mlx5e_ptpsq *ptpsq, int tc)
{
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	devlink_fmsg_obj_nest_start(fmsg);
	devlink_fmsg_string_pair_put(fmsg, "channel", "ptp");
	mlx5e_tx_reporter_build_diagnose_output_sq_common(fmsg, &ptpsq->txqsq, tc);
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Port TS");
	mlx5e_health_cq_diag_fmsg(&ptpsq->ts_cq, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	devlink_fmsg_obj_nest_end(fmsg);
#else
	int err;

	err = devlink_fmsg_obj_nest_start(fmsg);
	if (err)
		return err;

	err = devlink_fmsg_string_pair_put(fmsg, "channel", "ptp");
	if (err)
		return err;

	err = mlx5e_tx_reporter_build_diagnose_output_sq_common(fmsg, &ptpsq->txqsq, tc);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Port TS");
	if (err)
		return err;

	err = mlx5e_health_cq_diag_fmsg(&ptpsq->ts_cq, fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	if (err)
		return err;

	err = devlink_fmsg_obj_nest_end(fmsg);
	if (err)
		return err;

	return 0;
#endif
}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_diagnose_generic_txqsq(struct devlink_fmsg *fmsg,
					 struct mlx5e_txqsq *txqsq)
{
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	bool real_time =  mlx5_is_real_time_sq(txqsq->mdev);
	u32 sq_sz = mlx5_wq_cyc_get_size(&txqsq->wq);
	u32 sq_stride = MLX5_SEND_WQE_BB;

	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SQ");
	devlink_fmsg_u64_pair_put(fmsg, "stride size", sq_stride);
	devlink_fmsg_u32_pair_put(fmsg, "size", sq_sz);
	devlink_fmsg_string_pair_put(fmsg, "ts_format", real_time ? "RT" : "FRC");
	mlx5e_health_cq_common_diag_fmsg(&txqsq->cq, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#else
	u32 sq_stride, sq_sz;
	bool real_time;
	int err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SQ");
	if (err)
		return err;

	real_time =  mlx5_is_real_time_sq(txqsq->mdev);
	sq_sz = mlx5_wq_cyc_get_size(&txqsq->wq);
	sq_stride = MLX5_SEND_WQE_BB;

	err = devlink_fmsg_u64_pair_put(fmsg, "stride size", sq_stride);
	if (err)
		return err;

	err = devlink_fmsg_u32_pair_put(fmsg, "size", sq_sz);
	if (err)
		return err;

	err = devlink_fmsg_string_pair_put(fmsg, "ts_format", real_time ? "RT" : "FRC");
	if (err)
		return err;

	err = mlx5e_health_cq_common_diag_fmsg(&txqsq->cq, fmsg);
	if (err)
		return err;

	return mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#endif
}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_diagnose_generic_tx_port_ts(struct devlink_fmsg *fmsg,
					      struct mlx5e_ptpsq *ptpsq)
{
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Port TS");
	mlx5e_health_cq_common_diag_fmsg(&ptpsq->ts_cq, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#else
	int err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Port TS");
	if (err)
		return err;

	err = mlx5e_health_cq_common_diag_fmsg(&ptpsq->ts_cq, fmsg);
	if (err)
		return err;

	return mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#endif
}

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
static void
#else
static int
#endif
mlx5e_tx_reporter_diagnose_common_config(struct devlink_health_reporter *reporter,
					 struct devlink_fmsg *fmsg)
{
	struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
	struct mlx5e_txqsq *generic_sq = priv->txq2sq[0];
	struct mlx5e_ptp *ptp_ch = priv->channels.ptp;
	struct mlx5e_ptpsq *generic_ptpsq;
#ifdef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	int err;
#endif

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Common Config");
	mlx5e_tx_reporter_diagnose_generic_txqsq(fmsg, generic_sq);
#else
	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "Common Config");
	if (err)
		return err;

	err = mlx5e_tx_reporter_diagnose_generic_txqsq(fmsg, generic_sq);
	if (err)
		return err;
#endif

	if (!ptp_ch || !test_bit(MLX5E_PTP_STATE_TX, ptp_ch->state))
		goto out;

	generic_ptpsq = &ptp_ch->ptpsq[0];

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "PTP");
	mlx5e_tx_reporter_diagnose_generic_txqsq(fmsg, &generic_ptpsq->txqsq);
	mlx5e_tx_reporter_diagnose_generic_tx_port_ts(fmsg, generic_ptpsq);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#else
	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "PTP");
	if (err)
		return err;

	err = mlx5e_tx_reporter_diagnose_generic_txqsq(fmsg, &generic_ptpsq->txqsq);
	if (err)
		return err;

	err = mlx5e_tx_reporter_diagnose_generic_tx_port_ts(fmsg, generic_ptpsq);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	if (err)
		return err;

#endif
out:
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#else
	return mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#endif
}

static int mlx5e_tx_reporter_diagnose(struct devlink_health_reporter *reporter,
				      struct devlink_fmsg *fmsg
#ifdef HAVE_HEALTH_REPORTER_RECOVER_HAS_EXTACK
                                      , struct netlink_ext_ack *extack
#endif
                                     )
{
	struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
	struct mlx5e_ptp *ptp_ch = priv->channels.ptp;
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	int i, tc;
#else
	int i, tc, err = 0;
#endif

	mutex_lock(&priv->state_lock);

	if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
		goto unlock;

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_tx_reporter_diagnose_common_config(reporter, fmsg);
	devlink_fmsg_arr_pair_nest_start(fmsg, "SQs");
#else
	err = mlx5e_tx_reporter_diagnose_common_config(reporter, fmsg);
	if (err)
		goto unlock;

	err = devlink_fmsg_arr_pair_nest_start(fmsg, "SQs");
	if (err)
		goto unlock;
#endif
	for (i = 0; i < priv->channels.num; i++) {
		struct mlx5e_channel *c = priv->channels.c[i];

		for (tc = 0; tc < mlx5e_get_dcb_num_tc(&priv->channels.params); tc++) {
			struct mlx5e_txqsq *sq = &c->sq[tc];
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
			mlx5e_tx_reporter_build_diagnose_output(fmsg, sq, tc);
#else
			err = mlx5e_tx_reporter_build_diagnose_output(fmsg, sq, tc);
			if (err)
				goto unlock;
#endif
		}
	}

	if (!ptp_ch || !test_bit(MLX5E_PTP_STATE_TX, ptp_ch->state))
		goto close_sqs_nest;

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	for (tc = 0; tc < mlx5e_get_dcb_num_tc(&priv->channels.params); tc++)
		mlx5e_tx_reporter_build_diagnose_output_ptpsq(fmsg,
				&ptp_ch->ptpsq[tc],
				tc);

#else
	for (tc = 0; tc < mlx5e_get_dcb_num_tc(&priv->channels.params); tc++) {
		err = mlx5e_tx_reporter_build_diagnose_output_ptpsq(fmsg,
								    &ptp_ch->ptpsq[tc],
								    tc);
		if (err)
			goto unlock;
	}
#endif

close_sqs_nest:
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	devlink_fmsg_arr_pair_nest_end(fmsg);
#else
	err = devlink_fmsg_arr_pair_nest_end(fmsg);
	if (err)
		goto unlock;
#endif

unlock:
	mutex_unlock(&priv->state_lock);
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	return 0;
#else
	return err;
#endif
}

static int mlx5e_tx_reporter_dump_sq(struct mlx5e_priv *priv, struct devlink_fmsg *fmsg,
				     void *ctx)
{
	struct mlx5_rsc_key key = {};
	struct mlx5e_txqsq *sq = ctx;
#ifdef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	int err;
#endif

	if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
		return 0;

#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SX Slice");
#else
	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SX Slice");
	if (err)
		return err;

#endif
	key.size = PAGE_SIZE;
	key.rsc = MLX5_SGMT_TYPE_SX_SLICE_ALL;
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SQ");
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "QPC");
#else
	err = mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SQ");
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "QPC");
	if (err)
		return err;
#endif
	key.rsc = MLX5_SGMT_TYPE_FULL_QPC;
	key.index1 = sq->sqn;
	key.num_of_obj1 = 1;
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "send_buff");
#else
	err = mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_start(fmsg, "send_buff");
	if (err)
		return err;
#endif
	key.rsc = MLX5_SGMT_TYPE_SND_BUFF;
	key.num_of_obj2 = MLX5_RSC_DUMP_ALL;
#ifndef HAVE_INT_DEVLINK_FMSG_U8_PAIR
	mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);

	mlx5e_health_fmsg_named_obj_nest_end(fmsg);

	return 0;
#else
	err = mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	if (err)
		return err;

	err = mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	if (err)
		return err;

	return mlx5e_health_fmsg_named_obj_nest_end(fmsg);
#endif
}

static int mlx5e_tx_reporter_timeout_dump(struct mlx5e_priv *priv, struct devlink_fmsg *fmsg,
					  void *ctx)
{
	struct mlx5e_tx_timeout_ctx *to_ctx = ctx;

	return mlx5e_tx_reporter_dump_sq(priv, fmsg, to_ctx->sq);
}

static int mlx5e_tx_reporter_ptpsq_unhealthy_dump(struct mlx5e_priv *priv,
						  struct devlink_fmsg *fmsg,
						  void *ctx)
{
	struct mlx5e_ptpsq *ptpsq = ctx;

	return mlx5e_tx_reporter_dump_sq(priv, fmsg, &ptpsq->txqsq);
}

static int mlx5e_tx_reporter_dump_all_sqs(struct mlx5e_priv *priv,
					  struct devlink_fmsg *fmsg)
{
	struct mlx5e_ptp *ptp_ch = priv->channels.ptp;
	struct mlx5_rsc_key key = {};
	int i, tc;

	if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
		return 0;

	mlx5e_health_fmsg_named_obj_nest_start(fmsg, "SX Slice");
	key.size = PAGE_SIZE;
	key.rsc = MLX5_SGMT_TYPE_SX_SLICE_ALL;
	mlx5e_health_rsc_fmsg_dump(priv, &key, fmsg);
	mlx5e_health_fmsg_named_obj_nest_end(fmsg);
	devlink_fmsg_arr_pair_nest_start(fmsg, "SQs");

	for (i = 0; i < priv->channels.num; i++) {
		struct mlx5e_channel *c = priv->channels.c[i];

		for (tc = 0; tc < mlx5e_get_dcb_num_tc(&priv->channels.params); tc++) {
			struct mlx5e_txqsq *sq = &c->sq[tc];

			mlx5e_health_queue_dump(priv, fmsg, sq->sqn, "SQ");
		}
	}

	if (ptp_ch && test_bit(MLX5E_PTP_STATE_TX, ptp_ch->state)) {
		for (tc = 0; tc < mlx5e_get_dcb_num_tc(&priv->channels.params); tc++) {
			struct mlx5e_txqsq *sq = &ptp_ch->ptpsq[tc].txqsq;

			mlx5e_health_queue_dump(priv, fmsg, sq->sqn, "PTP SQ");
		}
	}

	devlink_fmsg_arr_pair_nest_end(fmsg);
	return 0;
}

static int mlx5e_tx_reporter_dump_from_ctx(struct mlx5e_priv *priv,
					   struct mlx5e_err_ctx *err_ctx,
					   struct devlink_fmsg *fmsg)
{
	return err_ctx->dump(priv, fmsg, err_ctx->ctx);
}

static int mlx5e_tx_reporter_dump(struct devlink_health_reporter *reporter,
				  struct devlink_fmsg *fmsg, void *context
#ifdef HAVE_HEALTH_REPORTER_RECOVER_HAS_EXTACK
                                  , struct netlink_ext_ack *extack
#endif
                                 )
{
	struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
	struct mlx5e_err_ctx *err_ctx = context;

	return err_ctx ? mlx5e_tx_reporter_dump_from_ctx(priv, err_ctx, fmsg) :
			 mlx5e_tx_reporter_dump_all_sqs(priv, fmsg);
}
#endif /* HAVE_DEVLINK_HEALTH_REPORT_SUPPORT */

void mlx5e_reporter_tx_err_cqe(struct mlx5e_txqsq *sq)
{
	char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
	struct mlx5e_priv *priv = sq->priv;
	struct mlx5e_err_ctx err_ctx = {};

	err_ctx.ctx = sq;
	err_ctx.recover = mlx5e_tx_reporter_err_cqe_recover;
#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
       err_ctx.dump = mlx5e_tx_reporter_dump_sq;
#endif /* HAVE_DEVLINK_HEALTH_REPORT_SUPPORT */
	snprintf(err_str, sizeof(err_str), "ERR CQE on SQ: 0x%x", sq->sqn);

	mlx5e_health_report(priv, priv->tx_reporter, err_str, &err_ctx);
}

int mlx5e_reporter_tx_timeout(struct mlx5e_txqsq *sq)
{
	char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
	struct mlx5e_tx_timeout_ctx to_ctx = {};
	struct mlx5e_priv *priv = sq->priv;
	struct mlx5e_err_ctx err_ctx = {};

	to_ctx.sq = sq;
	err_ctx.ctx = &to_ctx;
	err_ctx.recover = mlx5e_tx_reporter_timeout_recover;
#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
	err_ctx.dump = mlx5e_tx_reporter_timeout_dump;
#endif
	snprintf(err_str, sizeof(err_str),
		 "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x, usecs since last trans: %u",
		 sq->ch_ix, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc,
		 jiffies_to_usecs(jiffies - READ_ONCE(sq->txq->trans_start)));

	mlx5e_health_report(priv, priv->tx_reporter, err_str, &err_ctx);
	return to_ctx.status;
}

#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
void mlx5e_reporter_tx_ptpsq_unhealthy(struct mlx5e_ptpsq *ptpsq)
{
	struct mlx5e_ptp_metadata_map *map = &ptpsq->metadata_map;
	char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
	struct mlx5e_txqsq *txqsq = &ptpsq->txqsq;
	struct mlx5e_cq *ts_cq = &ptpsq->ts_cq;
	struct mlx5e_priv *priv = txqsq->priv;
	struct mlx5e_err_ctx err_ctx = {};

	err_ctx.ctx = ptpsq;
	err_ctx.recover = mlx5e_tx_reporter_ptpsq_unhealthy_recover;
	err_ctx.dump = mlx5e_tx_reporter_ptpsq_unhealthy_dump;
	snprintf(err_str, sizeof(err_str),
		 "Unhealthy TX port TS queue: %d, SQ: 0x%x, CQ: 0x%x, Undelivered CQEs: %u Map Capacity: %u",
		 txqsq->ch_ix, txqsq->sqn, ts_cq->mcq.cqn, map->undelivered_counter, map->capacity);

	mlx5e_health_report(priv, priv->tx_reporter, err_str, &err_ctx);
}

static const struct devlink_health_reporter_ops mlx5_tx_reporter_ops = {
		.name = "tx",
		.recover = mlx5e_tx_reporter_recover,
		.diagnose = mlx5e_tx_reporter_diagnose,
		.dump = mlx5e_tx_reporter_dump,
};
#endif

#define MLX5_REPORTER_TX_GRACEFUL_PERIOD 500

void mlx5e_reporter_tx_create(struct mlx5e_priv *priv)
{
#ifndef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
	priv->tx_reporter = NULL;
#else
	struct devlink_health_reporter *reporter;

#ifndef HAVE_DEVLINK_PORT_HEALTH_REPORTER_CREATE
	struct mlx5_core_dev *mdev = priv->mdev;
	struct devlink *devlink;
#endif
#ifdef HAVE_DEVLINK_PORT_HEALTH_REPORTER_CREATE
#ifdef HAVE_NET_DEVICE_DEVLINK_PORT
	reporter = devlink_port_health_reporter_create(priv->netdev->devlink_port, &mlx5_tx_reporter_ops,
#else
	reporter = devlink_port_health_reporter_create(mlx5e_devlink_get_dl_port(priv), &mlx5_tx_reporter_ops,
#endif
						       MLX5_REPORTER_TX_GRACEFUL_PERIOD, priv);
#else /* HAVE_DEVLINK_PORT_HEALTH_REPORTER_CREATE */
	devlink = priv_to_devlink(mdev);
	reporter =
		 devlink_health_reporter_create(devlink, &mlx5_tx_reporter_ops,
						MLX5_REPORTER_TX_GRACEFUL_PERIOD,
#ifdef HAVE_DEVLINK_HEALTH_REPORTER_CREATE_5_ARGS
						true,
#endif
						priv);
#endif /* HAVE_DEVLINK_PORT_HEALTH_REPORTER_CREATE */
	if (IS_ERR(reporter)) {
		netdev_warn(priv->netdev,
			    "Failed to create tx reporter, err = %ld\n",
			    PTR_ERR(reporter));
		return;
	}
	priv->tx_reporter = reporter;
#endif /* HAVE_DEVLINK_HEALTH_REPORT_SUPPORT */
}

void mlx5e_reporter_tx_destroy(struct mlx5e_priv *priv)
{
	if (!priv->tx_reporter)
		return;

#ifdef HAVE_DEVLINK_HEALTH_REPORT_SUPPORT

#ifdef HAVE_DEVLINK_PORT_HEALTH_REPORTER_DESTROY
	devlink_port_health_reporter_destroy(priv->tx_reporter);
#else
	devlink_health_reporter_destroy(priv->tx_reporter);
#endif /* HAVE_DEVLINK_PORT_HEALTH_REPORTER_DESTROY  */

#endif /* HAVE_DEVLINK_HEALTH_REPORT_SUPPORT */
	priv->tx_reporter = NULL;
}
