#!/usr/bin/bash
#
# Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
#
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: LicenseRef-NVIDIA-Proprietary
#
# IPG SET with link ceremony (matches RCP dms_spectrum-x.sh sequence):
# 1. Write PIPG register
# 2. Sleep 1s
# 3. PAOS admin_status=2 (link down)
# 4. PAOS admin_status=1 (link up)
# 5. Poll PAOS oper_status until UP (timeout 30s); on timeout warn and continue
# 6. Read PIPG back and verify the admin value was applied
source "$(dirname "$0")/_lib.sh"
trace "$@"
# The PIPG ipg field is seven bits; firmware validates device-specific limits.
readonly IPG_ADMIN_MAX=127
readonly LINK_UP_TIMEOUT_S=30
readonly LINK_UP_POLL_INTERVAL_S=2
BDF=$(bdf_from_target "$1"); VALUE="$2"

[[ "$VALUE" =~ ^[0-9]+$ ]] || tool_fail 1 "Error: IPG admin '$VALUE' must be an integer in 0..$IPG_ADMIN_MAX"
VALUE="${VALUE#"${VALUE%%[!0]*}"}"; VALUE="${VALUE:-0}"
[[ ${#VALUE} -le ${#IPG_ADMIN_MAX} ]] && (( VALUE <= IPG_ADMIN_MAX )) || \
    tool_fail 1 "Error: IPG admin '$VALUE' must be an integer in 0..$IPG_ADMIN_MAX"

# Keep command output out of success responses and forward failures through tool_fail.
if ! OUT=$(mlxreg -d "$BDF" -y --set "ipg=${VALUE}" --reg_name PIPG \
    --indexes 'local_port=1,lp_msb=0,ipg_cap_index=0' 2>&1); then
    tool_fail 1 "Error: mlxreg PIPG SET failed: ${OUT//$'\n'/ }"
fi

# 2. Sleep (RCP: sleep 1 between IPG write and link toggle)
sleep 1

# 3. Link down (PAOS admin_status=2, fd=1 force down, ase=1 admin state enable)
if ! OUT=$(mlxreg -d "$BDF" -y --set 'admin_status=2,fd=1,ase=1' --reg_name PAOS \
    --indexes 'local_port=1,lp_msb=0' 2>&1); then
    tool_fail 1 "Error: IPG link down failed: ${OUT//$'\n'/ }"
fi

# 4. Link up (PAOS admin_status=1)
if ! OUT=$(mlxreg -d "$BDF" -y --set 'admin_status=1,fd=1,ase=1' --reg_name PAOS \
    --indexes 'local_port=1,lp_msb=0' 2>&1); then
    tool_fail 1 "Error: IPG link up failed: ${OUT//$'\n'/ }"
fi

# 5. Poll oper_status for the configured link-up wait interval.
for ((elapsed=0; elapsed<LINK_UP_TIMEOUT_S; elapsed+=LINK_UP_POLL_INTERVAL_S)); do
    if ! OUT=$(mlxreg -d "$BDF" --reg_name PAOS --get --indexes 'local_port=1,lp_msb=0' 2>&1); then
        tool_fail 1 "Error: IPG PAOS GET failed: ${OUT//$'\n'/ }"
    fi
    OPER=$(printf '%s\n' "$OUT" | awk '$1 == "oper_status" {print $NF; exit}')
    [[ "$OPER" =~ $MLXREG_HEX32_PATTERN ]] || tool_fail 1 "Error: missing or invalid PAOS oper_status"
    (( OPER == 1 )) && break
    sleep "$LINK_UP_POLL_INTERVAL_S"
done
# The IPG write does not depend on link, only the link-up wait does. A port
# with no peer (bad cable, switch port in maintenance, link flap at boot) must
# not fail the write: this tool runs inside a boot chain, so a fatal exit here
# would leave every later phase and every healthy port unconfigured. Warn and
# fall through to the admin readback, which still catches a firmware no-op.
LINK_STATE="link up"
if (( OPER != 1 )); then
    LINK_STATE="link still down"
    echo "Warning: $BDF link not up after ${LINK_UP_TIMEOUT_S}s; IPG admin=${VALUE} written, continuing without link-up confirmation" >&2
fi

# Echoing the request hides firmware no-ops; verify admin after the link transition.
if ! OUT=$(mlxreg -d "$BDF" --reg_name PIPG --get \
    --indexes 'local_port=1,lp_msb=0,ipg_cap_index=0' 2>&1); then
    tool_fail 1 "Error: IPG PIPG GET failed: ${OUT//$'\n'/ }"
fi
ACTUAL=$(printf '%s\n' "$OUT" | awk '$1 == "ipg" {print $NF; exit}')
[[ "$ACTUAL" =~ $MLXREG_HEX32_PATTERN ]] || tool_fail 1 "Error: missing or invalid PIPG ipg readback"
(( ACTUAL == VALUE )) || tool_fail 1 "Error: IPG admin requested $VALUE, read back $((ACTUAL)) ($LINK_STATE)"

echo "admin=${VALUE}"
