#!/bin/sh

# ---------------------------------------------------------------
#  Mellanox NIC firmware performance tuning.
#
#  Iterates over all detected Mellanox network devices
#  and applies selected optimizations.
#
#  Run this once after installing a new NIC.
#  Requires system reboot.
# --------------------------------------------------------------

# Enable aggressive CQE Zipping.
cqe_compression=1

# Set relaxed ordering for PCI operations.
# Disable for Intel platforms, enable for AMD.
if lscpu | grep -q AuthenticAMD; then
    pci_wr_ordering=1
else
    pci_wr_ordering=0
fi

# Enable DevX.
uctx_en=1


devs=$(ls -d /sys/class/net/*/device/infiniband_verbs/uverbs* | cut -d / -f 5)

for dev in $devs; do
    pci_addr=$(grep PCI_SLOT_NAME /sys/class/net/$dev/device/uevent | tail -c +15)

    echo Device: $dev $pci_addr

    mstconfig -y -d $pci_addr s \
        CQE_COMPRESSION=$cqe_compression \
        PCI_WR_ORDERING=$pci_wr_ordering \
        UCTX_EN=$uctx_en

    # Switch link type to Ethernet if supported
    mstconfig -y -d $pci_addr s LINK_TYPE_P1=2 || true
    mstconfig -y -d $pci_addr s LINK_TYPE_P2=2 || true
done