Skip to content

Commit 9bb83d2

Browse files
dgsudharsankellyyeh
authored andcommitted
[201911][mellanox] Extend Mellanox FW utils with CPLD update (sonic-net#12172)
Porting sonic-net#3723 to 201911 #### Why I did it Extend Mellanox FW utils with CPLD update feature Added support for CPLD upgrade to Mellanox FW utility #### How I did it Updated Mellanox FW utility #### How to verify it mlnx-fw-upgrade.sh --upgrade --cpld # Regular CPLD update flow UPDATE_MLNX_CPLD_FW=1 mlnx-fw-upgrade.sh --upgrade # Force CPLD refresh only #### Ensure to add label/tag for the feature raised. example - [PR#2174](sonic-net/sonic-utilities#2174) where, Generic Config and Update feature has been labelled as GCU.
1 parent e3648d0 commit 9bb83d2

File tree

1 file changed

+146
-19
lines changed

1 file changed

+146
-19
lines changed

platform/mellanox/mlnx-fw-upgrade.j2

+146-19
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare -rA FW_REQUIRED_MAP=( \
4040
)
4141

4242
IMAGE_UPGRADE="${NO_PARAM}"
43+
CPLD_UPGRADE="${NO_PARAM}"
4344
VERBOSE_LEVEL="${VERBOSE_MIN}"
4445

4546
function PrintHelp() {
@@ -48,12 +49,14 @@ function PrintHelp() {
4849
echo
4950
echo "OPTIONS:"
5051
echo " -u, --upgrade Upgrade ASIC firmware using next boot image (useful after SONiC-To-SONiC update)"
52+
echo " -c, --cpld Upgrade CPLD firmware (requires -u|--upgrade)"
5153
echo " -v, --verbose Verbose mode"
5254
echo " -h, --help Print help"
5355
echo
5456
echo "Examples:"
5557
echo " ./${SCRIPT_NAME} --verbose"
5658
echo " ./${SCRIPT_NAME} --upgrade"
59+
echo " ./${SCRIPT_NAME} --upgrade --cpld --verbose"
5760
echo " ./${SCRIPT_NAME} --help"
5861
echo
5962
}
@@ -64,6 +67,9 @@ function ParseArguments() {
6467
-u|--upgrade)
6568
IMAGE_UPGRADE="${YES_PARAM}"
6669
;;
70+
-c|--cpld)
71+
CPLD_UPGRADE="${YES_PARAM}"
72+
;;
6773
-v|--verbose)
6874
VERBOSE_LEVEL="${VERBOSE_MAX}"
6975
;;
@@ -120,16 +126,44 @@ function ExitSuccess() {
120126
exit "${EXIT_SUCCESS}"
121127
}
122128

129+
function ParseMachineConf() {
130+
ONIE_MACHINE="$(cat /host/machine.conf | grep 'onie_machine=' | cut -f2 -d'=')"
131+
ONIE_PLATFORM="$(cat /host/machine.conf | grep 'onie_platform=' | cut -f2 -d'=')"
132+
}
133+
134+
function ShowProgressBar() {
135+
local -rA SPIN=(
136+
[0]="-"
137+
[1]="\\"
138+
[2]="|"
139+
[3]="/"
140+
)
141+
142+
if [[ "${VERBOSE_LEVEL}" -lt "${VERBOSE_INFO}" ]]; then
143+
sleep 2s
144+
return "${EXIT_SUCCESS}"
145+
fi
146+
147+
# Print progress bar: use carriage return to overwrite command line content
148+
for i in "${SPIN[@]}"; do
149+
echo -ne "\r[${i}] ${1}"
150+
sleep 0.5s
151+
done
152+
153+
# Clear command line content + carriage return
154+
echo -ne "\033[1K\r"
155+
}
156+
123157
function WaitForDevice() {
124158
local -i QUERY_RETRY_COUNT_MAX="10"
125159
local -i QUERY_RETRY_COUNT="0"
126160

127-
${QUERY_CMD} > /dev/null
161+
${QUERY_CMD} &> /dev/null
128162

129163
while [[ ("${QUERY_RETRY_COUNT}" -lt "${QUERY_RETRY_COUNT_MAX}") && ("$?" -ne "${EXIT_SUCCESS}") ]]; do
130164
sleep 1s
131165
((QUERY_RETRY_COUNT++))
132-
${QUERY_CMD} > /dev/null
166+
${QUERY_CMD} &> /dev/null
133167
done
134168
}
135169

@@ -170,7 +204,7 @@ function RunCmd() {
170204
fi
171205
}
172206

173-
function UpgradeFW() {
207+
function UpgradeASICFW() {
174208
local -r _FS_MOUNTPOINT="$1"
175209

176210
local -r _ASIC_TYPE="$(GetAsicType)"
@@ -209,30 +243,114 @@ function UpgradeFW() {
209243
fi
210244

211245
if [[ "${_FW_CURRENT}" == "${_FW_AVAILABLE}" ]]; then
212-
ExitSuccess "firmware is up to date"
246+
LogInfo "firmware is up to date"
213247
else
214248
LogNotice "firmware upgrade is required. Installing compatible version..."
215249
RunCmd "${BURN_CMD} -i ${_FW_FILE}"
216250
fi
217251
}
218252

219-
function UpgradeFWFromImage() {
220-
local -r _NEXT_SONIC_IMAGE="$(sonic_installer list | grep "Next: " | cut -f2 -d' ')"
221-
local -r _CURRENT_SONIC_IMAGE="$(sonic_installer list | grep "Current: " | cut -f2 -d' ')"
253+
function UpgradeCPLDFW_Worker() {
254+
local -r _CPLD_BURN_FILE="${1}"
255+
local -r _CPLD_REFRESH_FILE="${2}"
256+
local -r _ASIC_DEV="$(find /dev/mst -iname '*_pciconf0')"
222257

223-
local -r _FS_PATH="/host/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}/fs.squashfs"
224-
local -r _FS_MOUNTPOINT="/tmp/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}-fs"
258+
if [[ -f /tmp/cpld_fw_updated ]]; then
259+
RunCmd "cpldupdate --dev ${_ASIC_DEV} ${_CPLD_REFRESH_FILE}"
260+
return "${EXIT_SUCCESS}"
261+
fi
225262

226-
if [[ "${_CURRENT_SONIC_IMAGE}" == "${_NEXT_SONIC_IMAGE}" ]]; then
227-
ExitSuccess "firmware is up to date"
263+
RunCmd "cpldupdate --dev ${_ASIC_DEV} ${_CPLD_BURN_FILE}"
264+
RunCmd "cpldupdate --dev ${_ASIC_DEV} ${_CPLD_REFRESH_FILE}"
265+
}
266+
267+
function UpgradeCPLDFW() {
268+
local -r _CPLD_ARCHIVE="$1"
269+
270+
if [[ -f /tmp/cpld_fw_updated ]]; then
271+
LogWarning "forced CPLD refresh was requested for ${ONIE_PLATFORM}"
272+
CPLD_UPGRADE="${YES_PARAM}"
273+
fi
274+
275+
if [[ "${CPLD_UPGRADE}" != "${YES_PARAM}" ]]; then
276+
LogNotice "CPLD upgrade was not requested for ${ONIE_PLATFORM}"
277+
return "${EXIT_SUCCESS}"
278+
fi
279+
280+
if [[ ! -f "${_CPLD_ARCHIVE}" ]]; then
281+
LogNotice "CPLD update $(basename ${_CPLD_ARCHIVE}) was not provided for ${ONIE_PLATFORM}"
282+
return "${EXIT_SUCCESS}"
283+
fi
284+
285+
CPLD_DIR="$(mktemp -d)"
286+
287+
RunCmd "tar xzf ${_CPLD_ARCHIVE} -C ${CPLD_DIR}"
288+
289+
local -r _CPLD_BURN_FILE="${CPLD_DIR}/$(cat ${CPLD_DIR}/bundle.txt | grep 'burn=' | cut -f2 -d'=')"
290+
local -r _CPLD_REFRESH_FILE="${CPLD_DIR}/$(cat ${CPLD_DIR}/bundle.txt | grep 'refresh=' | cut -f2 -d'=')"
291+
local -r _CPLD_VERSION="$(cat ${CPLD_DIR}/bundle.txt | grep 'version=' | cut -f2 -d'=')"
292+
293+
local _CURRENT_CPLD_VERSION="${_CPLD_VERSION}"
294+
local _TARGET_CPLD_VERSION="${_CPLD_VERSION}"
295+
296+
if [[ -f /bsp/cpld/cpld_mgmt_version ]]; then
297+
_CURRENT_CPLD_VERSION="$(cat /bsp/cpld/cpld_mgmt_version)"
298+
elif [[ -f /var/run/hw-management/system/cpld1_version ]]; then
299+
_CURRENT_CPLD_VERSION="$(cat /var/run/hw-management/system/cpld1_version)"
228300
else
229-
mkdir -p "${_FS_MOUNTPOINT}"
230-
mount -t squashfs "${_FS_PATH}" "${_FS_MOUNTPOINT}"
301+
ExitFailure "could not retrieve current CPLD firmware version"
302+
fi
231303

232-
UpgradeFW "${_FS_MOUNTPOINT}"
304+
if [[ "${_CURRENT_CPLD_VERSION}" = "${_TARGET_CPLD_VERSION}" ]]; then
305+
LogInfo "CPLD firmware is up to date"
306+
return "${EXIT_SUCCESS}"
307+
fi
308+
309+
LogNotice "CPLD firmware upgrade is required. Installing compatible version..."
310+
311+
LogInfo "current CPLD firmware version: ${_CURRENT_CPLD_VERSION}"
312+
LogInfo "target CPLD firmware version: ${_TARGET_CPLD_VERSION}"
313+
314+
LogInfo "CPLD burn firmware file: ${_CPLD_BURN_FILE}"
315+
LogInfo "CPLD refresh firmware file: ${_CPLD_REFRESH_FILE}"
316+
317+
UpgradeCPLDFW_Worker "${_CPLD_BURN_FILE}" "${_CPLD_REFRESH_FILE}" &
318+
local -r _PID="$!"
319+
320+
while $(ps -e -o pid | grep -E "^[[:blank:]]*${_PID}$" &> /dev/null); do
321+
ShowProgressBar "CPLD update..."
322+
done
233323

234-
umount -rf "${_FS_MOUNTPOINT}"
235-
rm -rf "${_FS_MOUNTPOINT}"
324+
RunCmd "wait ${_PID}"
325+
}
326+
327+
function UpgradeFWFromImage() {
328+
local -r _NEXT_SONIC_IMAGE="$(sonic_installer list | grep 'Next: ' | cut -f2 -d' ')"
329+
local -r _CURRENT_SONIC_IMAGE="$(sonic_installer list | grep 'Current: ' | cut -f2 -d' ')"
330+
331+
if [[ "${_CURRENT_SONIC_IMAGE}" = "${_NEXT_SONIC_IMAGE}" ]]; then
332+
ExitSuccess "firmware is up to date"
333+
fi
334+
335+
FS_PATH="/host/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}/fs.squashfs"
336+
FS_MOUNTPOINT="/tmp/image-${_NEXT_SONIC_IMAGE#SONiC-OS-}-fs"
337+
338+
RunCmd "mkdir -p ${FS_MOUNTPOINT}"
339+
RunCmd "mount -t squashfs ${FS_PATH} ${FS_MOUNTPOINT}"
340+
341+
342+
UpgradeASICFW "${FS_MOUNTPOINT}${FW_FILE}"
343+
UpgradeCPLDFW "${FS_MOUNTPOINT}/etc/mlnx/cpld/${ONIE_MACHINE#mlnx_}_cpld.tar.gz"
344+
}
345+
346+
function Cleanup() {
347+
if [[ -d "${FS_MOUNTPOINT}" ]]; then
348+
umount -rf "${FS_MOUNTPOINT}"
349+
rm -rf "${FS_MOUNTPOINT}"
350+
fi
351+
352+
if [[ -d "${CPLD_DIR}" ]]; then
353+
rm -rf "${CPLD_DIR}"
236354
fi
237355
}
238356

@@ -242,16 +360,25 @@ function ExitIfQEMU() {
242360
fi
243361
}
244362

363+
trap Cleanup EXIT
364+
365+
ParseMachineConf
245366
ParseArguments "$@"
246367

247368
ExitIfQEMU
248369

249370
WaitForDevice
250371

251-
if [ "${IMAGE_UPGRADE}" != "${YES_PARAM}" ]; then
252-
UpgradeFW
253-
else
372+
if [[ "${CPLD_UPGRADE}" = "${YES_PARAM}" ]]; then
373+
if [[ "${IMAGE_UPGRADE}" = "${NO_PARAM}" ]]; then
374+
ExitFailure "mandatory parameter was not provided: -u|--upgrade"
375+
fi
376+
fi
377+
378+
if [[ "${IMAGE_UPGRADE}" = "${YES_PARAM}" ]]; then
254379
UpgradeFWFromImage
380+
else
381+
UpgradeASICFW
255382
fi
256383

257384
ExitSuccess "firmware upgrade is completed"

0 commit comments

Comments
 (0)