aboutsummaryrefslogtreecommitdiffstats
path: root/roms/skiboot/external/npu/run_procedure.sh
blob: e5df327e073d648cf2306786d9f910fb0e6017f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
#
# Run an NPU Hardware procedure
#
# Copyright 2016 IBM Corp.


function usage() {
	echo -e "$0: run a NPU hardware procedure (requires root)\n"
	echo -e "Usage: $0 <PCI bdfn> <procedure number>\n"
	echo -e "Example: $0 0008:00:01.1 1"
	echo -e "Procedures are documented in skiboot/doc/nvlink.rst"
}

function check_root() {
	if [ "$(id -u)" != "0" ]; then
		echo -e "Error: $0 must be run as root\n" 1>&2
		exit 1
	fi
}

function check_args() {
	if [ "$#" -eq 0 ]; then
		usage
		exit 1
	fi

	if [ "$#" -gt 2 ]; then
		echo -e "Error: too many arguments\n" 1>&2
		usage
		exit 1
	fi

	if [[ "$1" == "-h" || "$1" == "--help" ]]; then
		usage
		exit 0
	fi

	if ! [ "$2" -eq "$2" ] 2>/dev/null; then
		echo -e "Procedure must be a decimal number\n" 1>&2
		usage
		exit 1
	fi

	if [[ "$2" -lt "0" || "$2" -gt "12" ]]; then
		echo -e "Invalid procedure number\n" 1>&2
		usage
		exit 2
	fi

	pci_check=$(lspci -s $1)
	if [[ $? -ne 0 || $pci_check == "" ]]; then
		echo -e "Invalid PCI device\n" 1>&2
		usage
		exit 2
	fi
}

function run_procedure() {
	# Convert procedure number into hex
	procedure=$(echo "obase=16; $2" | bc)

	# Check the status register to make sure we can run a procedure
	status=$(setpci -s $1 0x84.L)
	if [[ $status == 8000000* ]]; then
		echo "Procedure in progress, try again." 1>&2
		echo "If that doesn't work, use procedure 0 to abort." 1>&2
		exit 3
	fi

	# Start the procedure
	setpci -s $1 0x88.L=0x0000000$procedure >/dev/null
	if [ $? -ne 0 ]; then
		echo "Control register write failed!" 1>&2
		exit 3
	fi

	iterations=1
	while [[ $(setpci -s $1 0x84.L) == 8000000* ]]; do
		((iterations++))
	done

	# Check again, procedure should be finished
	status=$(setpci -s $1 0x84.L)

	echo "Done in $iterations iteration(s)!"

	if [[ $status == 40000000 ]]; then
		echo "Procedure completed successfully."
		exit 0
	elif [[ $status == 40000001 ]]; then
		echo "Transient failure, try again." 1>&2
		exit 4
	elif [[ $status == 40000002 ]]; then
		echo "Permanent failure, reboot required?" 1>&2
		exit 5
	elif [[ $status == 40000003 ]]; then
		echo "Procedure aborted." 1>&2
		exit 6
	elif [[ $status == 40000004 ]]; then
		echo "Unsupported procedure." 1>&2
		exit 7
	fi
}

check_args "$@"
check_root
run_procedure "$1" "$2"