#!/usr/bin/bash
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Copyright (c) 2020 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.

if [ ! -f /sbin/mlnx_bf_configure.env ]; then
	echo "Error: /sbin/mlnx_bf_configure.env file not found"
	exit 1
fi

/bin/rm -f /run/mlnx_bf_configure_sync

. /sbin/mlnx_bf_configure.env

### MAIN ###
# Source config file early to pick up FORCE_DPU_CONFIGURE before the host_en check.
if [ -f /etc/mellanox/mlnx-bf.conf ]; then
	. /etc/mellanox/mlnx-bf.conf
fi
FORCE_DPU_CONFIGURE=${FORCE_DPU_CONFIGURE:-"no"}

pci_id=$(lspci -nD -d 15b3: | grep -E "$BLUEFIELD_DEVICES" | cut -d ' ' -f 1 | head -1)
host_en=$(mlxreg -d ${pci_id} --get --reg_name MMHI | grep host_en | awk '{print $NF}')

decimal=$(( host_en ))
is_power_of_two=$(( (decimal & (decimal - 1)) == 0 ))

if [[ $decimal != 0 && $is_power_of_two == 1 ]]; then
	if [ "X${FORCE_DPU_CONFIGURE}" == "Xyes" ]; then
		info "host_en=$host_en indicates Controller device, but FORCE_DPU_CONFIGURE=yes is set. Continuing..."
	else
		if is_bf3; then
			info "Detected Controller device. Exiting..."
			exit 0
		fi
	fi
fi

is_SecureBoot=0
if (mokutil --sb-state 2>&1 | grep -q "SecureBoot enabled"); then
	is_SecureBoot=1
fi

if [ $is_SecureBoot -eq 1 ]; then
	mst_dev=`/bin/ls -1 /dev/mst/mt*_pciconf0 2> /dev/null`
	if [ ! -n "${mst_dev}" ]; then
		mst start > /dev/null 2>&1
	fi
fi

IPSEC_FULL_OFFLOAD=${IPSEC_FULL_OFFLOAD:-"no"}
LAG_HASH_MODE=${LAG_HASH_MODE:-"yes"}
# On BF4, default to enabling eswitch multiport unless explicitly configured.
# Can be overridden by setting ENABLE_ESWITCH_MULTIPORT in
# /etc/mellanox/mlnx-bf.conf or /etc/mellanox/mlnx-ovs.conf.
if [ -z "${ENABLE_ESWITCH_MULTIPORT}" ]; then
	if [ $(lspci -nD -d 15b3: | grep -E "a2df" | wc -l) -gt 0 ]; then
		ENABLE_ESWITCH_MULTIPORT="yes"
	else
		ENABLE_ESWITCH_MULTIPORT="no"
	fi
fi
SNAP_DMA_SF=${SNAP_DMA_SF:-"auto"}
SNAP_DMA_SF_NUM=${SNAP_DMA_SF_NUM:-8000}
SNAP_DMA_SF_MAC=${SNAP_DMA_SF_MAC:-""}
SNAP_EMU_MNG_MANAGERS_FILE=${SNAP_EMU_MNG_MANAGERS_FILE:-"/run/mellanox/snap_emulation_managers"}
MLXCONFIG_DEBUG_LOG=${MLXCONFIG_DEBUG_LOG:-"/tmp/mlxconfig_debug.log"}
SWITCHDEV_DEVICES_LIST=()

{
RDMA_SET_NETNS_EXCLUSIVE=${RDMA_SET_NETNS_EXCLUSIVE:-"yes"}
if [ "X${RDMA_SET_NETNS_EXCLUSIVE}" == "Xyes" ]; then
	if ! (rdma system show netns 2>&1 | grep -q exclusive); then
		start_time=$(awk '{print $1}' /proc/uptime)
		output=$(rdma system set netns exclusive 2>&1)
		rc=$?
		while [ $rc -ne 0 ]; do
			elapsed=$(awk "BEGIN {printf \"%.0f\", $(awk '{print $1}' /proc/uptime) - $start_time}")
			if [ $elapsed -gt $RDMA_SET_NETNS_TIMEOUT ]; then
				break
			fi
			sleep 1
			output=$(rdma system set netns exclusive 2>&1)
			rc=$?
		done
	fi

	if (rdma system show netns 2>&1 | grep -q exclusive); then
		info "The RDMA subsystem is set in network namespace exclusive mode."
	else
		error "Failed to set rdma exclusive mode: $output"
	fi
fi
} &

ctrl_dev=$(lspci -nD -d 15b3: | grep -E "$BLUEFIELD_DEVICES" | cut -d ' ' -f 1 | head -1)
num_of_supported_devs=$(lspci -nD -d 15b3: | grep -E "$SUPPORTED_DEVICES" | wc -l)

start_time=$(awk '{print $1}' /proc/uptime)
mft_cmd="$mftconfig -d $ctrl_dev -e q INTERNAL_CPU_MODEL"
mft_output=$($mft_cmd 2>&1)
if ! (echo "$mft_output" | grep -q 'E- The Device doesn.*t support INTERNAL_CPU_MODEL parameter'); then
	while (echo "$mft_output" | grep -q "Failed to open"); do
		echo "$mft_cmd" >> ${MLXCONFIG_DEBUG_LOG}
		echo "$mft_output" >> ${MLXCONFIG_DEBUG_LOG}
		elapsed=$(awk "BEGIN {printf \"%.0f\", $(awk '{print $1}' /proc/uptime) - $start_time}")
		if [ $elapsed -gt $MLXCONFIG_TIMEOUT ]; then
			error "Failed to run $mftconfig query on $ctrl_dev after $elapsed seconds"
			exit 1
		fi
		sleep 1
		mft_output=$($mftconfig -d $ctrl_dev -e q INTERNAL_CPU_MODEL 2>&1)
	done
else
	info "Device $ctrl_dev does not support INTERNAL_CPU_MODEL parameter"
fi
info "MFT output: $mft_output"

esw_multiport_device=""
num_of_devs_switchdev=0
declare -A CX9_DEV_LAST_ECPF  # key=PCI domain:bus (physical CX-9), value=last ECPF PCI BDF on that device
# Set eswitch mode to switchdev for all devices
if (is_smartnic_mode "$ctrl_dev"); then
	stop_ipsec_services
	# Rank each BDF by PCI device ID before sorting (0=NIC, 1=BlueField)
	# so all NIC (e.g. ASTRA CX-9) links are configured before the
	# BlueField devices. Within each group the order is unchanged:
	# domain descending, then bus/device/function ascending.
	for dev in $(devlink dev show | awk '/^[[:space:]]/ {next} /^pci\// {b=tolower($1); sub(/^pci\//,"",b); sub(/:$/,"",b); if(!(b in s)){s[b]; print b}}' \
		| while read -r bdf; do
			if grep -qiE "$BLUEFIELD_DEVICES" /sys/bus/pci/devices/${bdf}/device 2>/dev/null; then
				echo "1/$bdf"
			else
				echo "0/$bdf"
			fi
		done \
		| sort -t/ -k1,1 -k2.1,2.4r -k2.6,2.7 -k2.9,2.10 -k2.12,2.12 | cut -d/ -f2)
	do
		if ! (is_dev_eth "$dev"); then
			info "Link type is IB for ${dev}. Skipping mode configuration."
			continue
		fi

		if is_dev_param_supported ${dev} lag_port_select_mode; then
			if [ "X${LAG_HASH_MODE}" == "Xno" ]; then
				set_dev_param ${dev} lag_port_select_mode queue_affinity
			elif [ "X${LAG_MULTIPORT_ESW_MODE}" == "Xyes" ]; then
				eswitch_mode=`get_eswitch_mode ${dev}`
				if [ "${eswitch_mode}" != "legacy" ]; then
					set_eswitch_mode ${dev} legacy
					RC=$((RC+$?))
				fi

				set_dev_param ${dev} lag_port_select_mode multiport_esw
			fi
		fi

		if [ "X${ENCAP_NONE_MODE}" == "Xyes" ]; then
			eswitch_mode=`get_eswitch_mode ${dev}`
			if [ "${eswitch_mode}" != "legacy" ]; then
				set_eswitch_mode ${dev} legacy
				RC=$((RC+$?))
			fi

			set_dev_param ${dev} encap none
		fi

		steering_mode=`get_steering_mode ${dev}`
		if [ "${steering_mode}" == "dmfs" ]; then
			eswitch_mode=`get_eswitch_mode ${dev}`
			if [ "${eswitch_mode}" != "legacy" ]; then
				set_eswitch_mode ${dev} legacy
				RC=$((RC+$?))
			fi

			set_steering_mode ${dev} smfs
		fi

		if [ "${IPSEC_FULL_OFFLOAD}" == "yes" ]; then
			lscpu | grep Flags | grep sha1 | grep sha2 | grep -q aes
			if [ $? -eq 0 ]; then
				eswitch_mode=`get_eswitch_mode ${dev}`
				if [ "${eswitch_mode}" != "legacy" ]; then
					set_eswitch_mode ${dev} legacy
					RC=$((RC+$?))
				fi
				steering_mode=`get_steering_mode ${dev}`
				if [ "${steering_mode}" != "dmfs" ]; then
					set_steering_mode ${dev} dmfs
				fi
			else
				info "Crypto disabled on this devide. Skipping IPsec mode configuration."
			fi
		fi

		eswitch_mode=`get_eswitch_mode ${dev}`
		if [ "${eswitch_mode}" != "switchdev" ]; then
			if [ "X${LEGACY_METADATA_MATCH_MODE}" == "Xyes" ]; then
				set_dev_param ${dev} vport_match_mode legacy
			fi

			set_eswitch_mode ${dev} switchdev
			RC=$((RC+$?))
		fi
		eswitch_mode=`get_eswitch_mode ${dev}`
		if [ "${eswitch_mode}" == "switchdev" ]; then
			num_of_devs_switchdev=$((num_of_devs_switchdev+1))
			SWITCHDEV_DEVICES_LIST+=( "$dev" )
			dev_id=$(cat /sys/bus/pci/devices/${dev}/device 2>/dev/null)
			if echo "$dev_id" | grep -qiE "$CX9_DEVICE_ID"; then
				# Track last ECPF per physical CX-9 SuperNIC (PCI domain:bus).
				# In ASTRA, an Orchid PCI domain holds TWO independent CX-9
				# SuperNICs on different buses, and each forms its own E-W
				# multi-plane LAG across its own ECPFs; esw_multiport must be
				# set on the LAST ECPF of each device (highest PCI function =
				# LAG master). The devlink loop is sorted, so each subsequent
				# ECPF of the same device overwrites the previous, leaving
				# the last one. Keying by domain alone would collapse the two
				# SuperNICs of an Orchid into one entry.
				cx9_dev="${dev%:*}"
				CX9_DEV_LAST_ECPF[$cx9_dev]="$dev"
			fi
			if echo "$dev_id" | grep -qE "0xa2df"; then
				# BF4 device — track for esw_multiport (SD or ASTRA N-S)
				esw_multiport_device="$dev"
			fi
		fi
		if is_sd_mode ${dev}; then
			debug "Adding SD device $dev to SD_DEVICES_LIST"
			SD_DEVICES_LIST+=( "$dev" )
		fi
		if is_supported_device ${dev}; then
			debug "Adding supported device $dev to DEVICE_LIST_FOR_OVS_BRIDGES"
			DEVICE_LIST_FOR_OVS_BRIDGES+=( "$dev" )
		fi
	done

	# Set esw_multiport after all ECPFs have been switched to switchdev mode.
	# This ordering ensures all eswitch instances are properly initialized
	# before the multi-port eswitch LAG is configured (required for ASTRA).
	#
	# In ASTRA mode, esw_multiport must be set on:
	# 1. The BF4 device (0xa2df) — for N-S multi-port eswitch (SD or Lupine dual-link)
	# 2. The last ECPF of EVERY CX-9 SuperNIC (0x1025) — for E-W multi-plane LAG,
	#    only when LAG_RESOURCE_ALLOCATION is PRE_ALLOCATION(1)
	# Both coexist on Grace in ASTRA configurations.

	# BF4 (N-S) esw_multiport
	if [ -n "$esw_multiport_device" ]; then
		if [ "X${ENABLE_ESWITCH_MULTIPORT}" == "Xyes" ]; then
			info "Enabling ESW Multiport on BF4 device $esw_multiport_device"
			set_dev_param ${esw_multiport_device} esw_multiport 1 "ESW Multiport:"
		else
			if is_sd_mode ${esw_multiport_device}; then
				info "Enabling ESW Multiport for SD device $esw_multiport_device"
				set_dev_param ${esw_multiport_device} esw_multiport 1 "ESW Multiport:"
			fi
		fi
	fi

	# CX-9 (E-W) esw_multiport — one per physical SuperNIC (last ECPF on each PCI domain:bus)
	if [ ${#CX9_DEV_LAST_ECPF[@]} -gt 0 ]; then
		if [ "X${ENABLE_ESWITCH_MULTIPORT}" == "Xyes" ]; then
			for cx9_dev in $(echo "${!CX9_DEV_LAST_ECPF[@]}" | tr ' ' '\n' | sort); do
				lag_master="${CX9_DEV_LAST_ECPF[$cx9_dev]}"
				if is_lag_resource_pre_allocation ${lag_master}; then
					info "Enabling ESW Multiport on last CX-9 ECPF $lag_master (device $cx9_dev)"
					set_dev_param ${lag_master} esw_multiport 1 "ESW Multiport:"
				else
					info "Skipping ESW Multiport on CX-9 ECPF $lag_master (device $cx9_dev): LAG_RESOURCE_ALLOCATION is not PRE_ALLOCATION(1) or PRE_ALLOCATION_QA(2)"
				fi
			done
		fi
	fi

	start_ipsec_services
fi

# For BF4 SD with merged eswitch, use a single bridge on LAG master only
if is_bf4 && [ ${#SD_DEVICES_LIST[@]} -gt 0 ]; then
	if [ "X${ENABLE_SD_MERGED_ESWITCH}" != "Xno" ]; then
		SD_MERGED_ESWITCH=1
		info "BF4 SD merged eswitch mode: single OVS bridge on LAG master only."

		# Preserve non-SD devices (e.g. CX-9) for their own bridges,
		# but remove all SD devices — the merged bridge collects their
		# ports at bridge-planning time from SD_DEVICES_LIST directly.
		new_list=()
		for dev in "${DEVICE_LIST_FOR_OVS_BRIDGES[@]}"; do
			if ! in_array "$dev" "${SD_DEVICES_LIST[@]}"; then
				new_list+=( "$dev" )
			fi
		done
		DEVICE_LIST_FOR_OVS_BRIDGES=( "${new_list[@]}" )
	fi
fi

# Create well-known DMA SF for SNAP doca service on the 2nd PCI link ECPF
# The DMA SF provides an additional ibdev needed for 800Gbps DMA across dual
# Grace PCI links in BF-4. It is created only when all preconditions are met:
#   - BF4 platform
#   - Multi-port eswitch enabled
#   - At least 2 switchdev ECPFs across two PCI links
#   - Emulation enabled (PCI_SWITCH_EMU_MNG_ENABLE + at least one emulation type)
#   - PER_PF_NUM_SF=1 and PF_TOTAL_SF >= SNAP_DMA_SF_NUM
# The SF is created on the ECPF of the 2nd PCI link that has no RDMA device.
SNAP_DMA_SF_TARGET_ECPF=""

create_snap_dma_sf()
{
	# Must be BF4
	if ! is_bf4; then
		debug "SNAP DMA SF: not BF4, skipping"
		return 1
	fi

	# Must have multiport eswitch enabled
	if [ "X${ENABLE_ESWITCH_MULTIPORT}" != "Xyes" ]; then
		debug "SNAP DMA SF: multiport eswitch not enabled, skipping"
		return 1
	fi

	# Must have at least 2 switchdev ECPFs
	if [ ${#SWITCHDEV_DEVICES_LIST[@]} -lt 2 ]; then
		debug "SNAP DMA SF: fewer than 2 switchdev ECPFs, skipping"
		return 1
	fi

	local emu_found=0
	local ctrl=""
	for ctrl in ${SWITCHDEV_DEVICES_LIST[@]}; do
		# Check emulation is enabled on the ECPF
		local emu_output
		emu_output=$(run_mlxconfig "$mftconfig -d ${ctrl} -e q \
			PCI_SWITCH_EMU_MNG_ENABLE \
			VIRTIO_NET_EMU_MNG_ENABLE NVME_EMU_MNG_ENABLE \
			VIRTIO_BLK_EMU_MNG_ENABLE VIRTIO_FS_EMU_MNG_ENABLE")

		if ! (echo "$emu_output" | grep -q 'PCI_SWITCH_EMU_MNG_ENABLE.*True(1)') && \
		! (echo "$emu_output" | grep 'PCI_SWITCH_EMU_MNG_ENABLE' | awk '{print $3}' | grep -q "^1$"); then
			debug "SNAP DMA SF: PCI_SWITCH_EMU_MNG_ENABLE not set on $ctrl, skipping"
			continue
		fi

		local emu_found=0
		local emu_type
		for emu_type in VIRTIO_NET_EMU_MNG_ENABLE NVME_EMU_MNG_ENABLE \
				VIRTIO_BLK_EMU_MNG_ENABLE VIRTIO_FS_EMU_MNG_ENABLE; do
			if (echo "$emu_output" | grep -q "${emu_type}.*True(1)") || \
			(echo "$emu_output" | grep "${emu_type}" | awk '{print $3}' | grep -q "^1$"); then
				emu_found=1
				break
			fi
		done
		if [ $emu_found -eq 0 ]; then
			debug "SNAP DMA SF: no emulation type enabled on $ctrl, skipping"
			continue
		fi

		# Check PER_PF_NUM_SF and PF_TOTAL_SF
		local sf_output
		sf_output=$(run_mlxconfig "$mftconfig -d ${ctrl} -e q PER_PF_NUM_SF PF_TOTAL_SF")
		if ! (echo "$sf_output" | grep -q 'PER_PF_NUM_SF.*True(1)') && \
		! (echo "$sf_output" | grep 'PER_PF_NUM_SF' | awk '{print $3}' | grep -q "^1$"); then
			debug "SNAP DMA SF: PER_PF_NUM_SF not enabled on $ctrl, skipping"
			continue
		fi
		local total_sf
		total_sf=$(echo "$sf_output" | grep -o 'PF_TOTAL_SF.*' | awk '{print $3}' | head -1)
		if [ -n "$total_sf" ] && [ "$total_sf" -lt 1 ] 2>/dev/null; then
			debug "SNAP DMA SF: PF_TOTAL_SF ($total_sf) is 0, no capacity to create SF, skipping"
			continue
		fi
	done

	# --- Identify the target ECPF on the 2nd PCI link ---
	# a. List all switchdev ECPFs (ASTRA/HW multiplane excluded upstream)
	# b. Find PCI link (domain:bus) of each ECPF
	# c. Find RDMA device for each ECPF
	# d. Eliminate ECPFs that have an RDMA device
	# e. Eliminate ECPFs sharing the same PCI link as RDMA-bearing ECPFs
	# f. Pick the first remaining ECPF
	local links_with_rdma=""
	local dev pci_link rdma_dev

	for dev in ${SWITCHDEV_DEVICES_LIST[@]}; do
		pci_link=$(echo $dev | cut -d: -f1-2)
		rdma_dev=$(/bin/ls /sys/bus/pci/devices/${dev}/infiniband/ 2>/dev/null | head -1)
		if [ -n "$rdma_dev" ]; then
			links_with_rdma="$links_with_rdma $pci_link"
			debug "SNAP DMA SF: ECPF $dev has RDMA device $rdma_dev on link $pci_link"
		fi
	done

	local target_ecpf=""
	local on_rdma_link rdma_link
	for dev in ${SWITCHDEV_DEVICES_LIST[@]}; do
		# Skip if this ECPF has an RDMA device
		rdma_dev=$(/bin/ls /sys/bus/pci/devices/${dev}/infiniband/ 2>/dev/null | head -1)
		if [ -n "$rdma_dev" ]; then
			continue
		fi
		# Skip if this ECPF is on the same PCI link as one with RDMA
		pci_link=$(echo $dev | cut -d: -f1-2)
		on_rdma_link=0
		for rdma_link in $links_with_rdma; do
			if [ "$pci_link" == "$rdma_link" ]; then
				on_rdma_link=1
				break
			fi
		done
		if [ $on_rdma_link -eq 0 ]; then
			target_ecpf=$dev
			break
		fi
	done

	if [ -z "$target_ecpf" ]; then
		debug "SNAP DMA SF: no eligible 2nd-link ECPF found, skipping"
		return 1
	fi

	SNAP_DMA_SF_TARGET_ECPF=$target_ecpf
	info "SNAP DMA SF: target ECPF is $target_ecpf (2nd PCI link)"

	# Determine MAC address:
	# 1. User-specified SNAP_DMA_SF_MAC takes priority
	# 2. Otherwise derive a deterministic MAC from PCI device + sfnum
	#    so the address is stable across reboots
	local snap_dma_sf_mac
	if [ -n "$SNAP_DMA_SF_MAC" ]; then
		snap_dma_sf_mac=$SNAP_DMA_SF_MAC
	else
		snap_dma_sf_mac=$(echo -n "${target_ecpf}:${SNAP_DMA_SF_NUM}" | md5sum | \
			sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
	fi

	info "Creating SNAP DMA SF (sfnum=$SNAP_DMA_SF_NUM, mac=$snap_dma_sf_mac) on $target_ecpf"

	if ! (which mlnx-sf > /dev/null 2>&1); then
		info "mlnx-sf not found, skipping SNAP DMA SF creation"
		return 1
	fi

	# Workaround: the driver requires --controller when creating an SF on
	# the 2nd PCI link ECPF. In SD mode the 2nd host uses controller 4;
	# in non-SD it uses controller 3.
	local ctrl_opt=""
	if is_sd_mode ${target_ecpf}; then
		ctrl_opt="--controller 4"
	else
		ctrl_opt="--controller 3"
	fi

	# Create SF with RoCE disabled, no netdev
	mlnx-sf --action create --device $target_ecpf \
		--sfnum $SNAP_DMA_SF_NUM \
		--hwaddr $snap_dma_sf_mac \
		$ctrl_opt \
		--disable-roce 2>/dev/null
	local rc=$?
	if [ $rc -ne 0 ]; then
		debug "SNAP DMA SF on $target_ecpf sfnum=$SNAP_DMA_SF_NUM may already exist or failed (rc=$rc)"
		return 1
	fi

	# Disable netdev on the SF (devlink param netdev_disable=true + reload)
	# Find the auxiliary device for this SF
	local aux_dev=""
	local retries=0
	while [ -z "$aux_dev" ] && [ $retries -lt 20 ]; do
		aux_dev=$(/bin/ls -d /sys/bus/pci/devices/${target_ecpf}/mlx5_core.sf.* 2>/dev/null | \
			xargs -I{} sh -c 'cat {}/sfnum 2>/dev/null | grep -q "^${SNAP_DMA_SF_NUM}$" && basename {}' 2>/dev/null | head -1)
		if [ -z "$aux_dev" ]; then
			sleep 0.1
			retries=$((retries+1))
		fi
	done
	if [ -n "$aux_dev" ]; then
		devlink dev param set auxiliary/${aux_dev} name enable_eth value false cmode driverinit 2>/dev/null
		devlink dev reload auxiliary/${aux_dev} 2>/dev/null
		debug "SNAP DMA SF: disabled netdev on auxiliary/${aux_dev}"
	else
		debug "SNAP DMA SF: could not find auxiliary device to disable netdev"
	fi

	# Bring the SF representor UP (but it must NOT be added to any OVS bridge)
	local sf_rep
	sf_rep=$(mlnx-sf --action show --device $target_ecpf --json 2>/dev/null | \
		python3 -c "import sys,json; data=json.load(sys.stdin); \
		[print(sf.get('netdev','')) for sf in data if str(sf.get('sfnum',''))=='$SNAP_DMA_SF_NUM']" 2>/dev/null | head -1)
	if [ -n "$sf_rep" ]; then
		ip link set $sf_rep up 2>/dev/null
		info "SNAP DMA SF: representor $sf_rep set UP (not added to OVS bridge)"
	fi

	info "Created SNAP DMA SF on $target_ecpf sfnum=$SNAP_DMA_SF_NUM mac=$snap_dma_sf_mac"
	return 0
}

# Run SNAP DMA SF creation if enabled (auto or yes)
if [ "X${SNAP_DMA_SF}" == "Xauto" ] || [ "X${SNAP_DMA_SF}" == "Xyes" ]; then
	create_snap_dma_sf
fi

# Export discovered ECPF ibdevs for SNAP emulation manager auto-configuration
if [ ${#SWITCHDEV_DEVICES_LIST[@]} -gt 0 ]; then
	mkdir -p $(dirname $SNAP_EMU_MNG_MANAGERS_FILE)
	> $SNAP_EMU_MNG_MANAGERS_FILE
	for em_dev in ${SWITCHDEV_DEVICES_LIST[@]}; do
		ibdev=$(/bin/ls /sys/bus/pci/devices/${em_dev}/infiniband/ 2>/dev/null | head -1)
		if [ -n "$ibdev" ]; then
			echo "$ibdev $em_dev" >> $SNAP_EMU_MNG_MANAGERS_FILE
			debug "Exported emulation manager: $ibdev ($em_dev)"
		fi
	done
	info "Exported ${#SWITCHDEV_DEVICES_LIST[@]} emulation managers to $SNAP_EMU_MNG_MANAGERS_FILE"
fi

debug "num_of_devs_switchdev: $num_of_devs_switchdev"
debug "SWITCHDEV_DEVICES_LIST: ${SWITCHDEV_DEVICES_LIST[*]}"
debug "SD_DEVICES_LIST: ${SD_DEVICES_LIST[*]}"
debug "DEVICE_LIST_FOR_OVS_BRIDGES: ${DEVICE_LIST_FOR_OVS_BRIDGES[*]}"
debug "SD_MERGED_ESWITCH: $SD_MERGED_ESWITCH"

if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
	info "Running in chroot environment. Exiting..."
	exit 0
fi

if [ $RC -ne 0 ]; then
	error "Exiting due to failures. RC=$RC"
	exit $RC
fi

if [ $num_of_devs_switchdev -eq 0 ]; then
	info "No devices configured to switchdev mode. Skipping SF/Bridges configuration."
	exit 0
fi

if [ -f /etc/mellanox/mlnx-sf.conf ]; then
	# Wait until NetworkManager is active to avoid race condition
	NETWORK_MANAGER_TIMEOUT=${NETWORK_MANAGER_TIMEOUT:-120}
	start_time=$(awk '{print $1}' /proc/uptime)
	while ! (systemctl is-active NetworkManager 2> /dev/null)
	do
		info "NetworkManager is not active. Waiting..."
		elapsed=$(awk "BEGIN {printf \"%.0f\", $(awk '{print $1}' /proc/uptime) - $start_time}")
		if [ $elapsed -gt ${NETWORK_MANAGER_TIMEOUT} ]; then
			info "NetworkManager timeout: ${NETWORK_MANAGER_TIMEOUT}"
			break
		fi
		sleep 0.1
	done
	. /etc/mellanox/mlnx-sf.conf
fi

touch /run/mlnx_bf_configure_sync

#Indication that the script has finished and other underlying services can start
if systemctl is-active --quiet mlnx_bf_configure_sync.service; then
	systemctl stop --no-block mlnx_bf_configure_sync
fi

wait
RC=$((RC+$?))

exit $RC
