-
Notifications
You must be signed in to change notification settings - Fork 11
/
oc-reset.sh
executable file
·203 lines (174 loc) · 6.58 KB
/
oc-reset.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#
# Copyright 2016, 2017 International Business Machines
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# get capi-utils root
[ -h $0 ] && package_root=`ls -l "$0" |sed -e 's|.*-> ||'` || package_root="$0"
package_root=$(dirname $package_root)
# echo "DEBUG : Package_root is : $package_root"
source $package_root/oc-utils-common.sh
bold=\\033[1m
green=\\033[32m
blue=\\033[34m
red=\\033[31m
normal=\\033[0m
program=`basename "$0"`
mylock=0 # var used to remove lock dir only if we created it
# Print usage message helper function
function usage() {
echo "Usage: sudo ${program} [OPTIONS]"
echo " [-C <card>] card to reset."
echo " Example: if you want to reset card"
echo -e " ${green} IBM,oc-snap.0004:00:00.1.0 ${normal}"
echo " Command line should be:"
echo -e " ${green}sudo ./oc-reset.sh -C IBM,oc-snap.0004:00:00.1.0 ${normal}"
echo " Or:"
echo -e " ${green}sudo ./oc-reset.sh -C 0004:00:00.0 ${normal}"
echo " Or:"
echo -e " ${green}sudo ./oc-reset.sh -C 4 ${normal}"
echo " [-V] Print program version (${version})"
echo " [-h] Print this help message."
echo
echo "Utility to reset CAPI FPGA cards."
echo "Please ensure that you are aiming to reset the target FPGA Card."
echo "Pls notify other users who you work with them on the same server."
echo
}
# Select function for FPGA Cards Selection
function select_cards() {
# print current date on server for comparison
printf "\n${bold}Current date:${normal}$(date)\n"
# get number of cards in system
n=`ls /dev/ocxl 2>/dev/null | wc -l`
printf "${bold} $n OpenCAPI cards found.${normal}\n"
# Find all OC cards in the system
allcards=`ls /dev/ocxl 2>/dev/null | awk -F"." '{ print $2 }' | sed s/$/.0/ | sort`
allcards_array=($allcards)
# print card information
i=0;
slot_enum=""
delimiter="|"
while read d ; do
p[$i]=$(cat /sys/bus/pci/devices/${allcards_array[$i]}/subsystem_device)
# translate the slot number string to a hexa number
card_slot_hex=$(printf '%x' "0x${allcards_array[$i]::-8}")
# build a slot_enum of all card numbers and use in the test menu to test user input
if [ -z "$slot_enum" ]; then
slot_enum=$card_slot_hex
else
slot_enum=$slot_enum$delimiter$card_slot_hex
fi
# find in oc-devices files the card corresponding to the slot to display the informations
while IFS='' read -r line || [[ -n $line ]]; do
if [[ ${line:0:6} == ${p[$i]:0:6} ]]; then
parse_info=($line)
board_vendor[$i]=${parse_info[1]}
printf "${bold} Card %s:${normal} %s - %s \n" "$card_slot_hex" "${allcards_array[$i]}" "${board_vendor[$i]}"
fi
done < "$package_root/oc-devices"
i=$[$i+1]
done < <( lspci -d "1014":"062b" -s .1 )
printf "\n"
# prompt card until input is in list of available slots
while ! [[ "$c" =~ ^($slot_enum)$ ]]
do
echo -e " ${green}Which card number do you want to reset? [${bold}$slot_enum${normal}]: \c" | sed 's/|/-/g'
read -r c
done
printf "\n"
}
# Parse any options given on the command line
while getopts ":C:Vh" opt; do
case ${opt} in
C)
card=$OPTARG
;;
V)
echo "${version}" >&2
exit 0
;;
h)
usage;
exit 0
;;
\?)
printf "${bold}${red}ERROR:${normal} Invalid option: -${OPTARG}\n" >&2
exit 1
;;
:)
printf "${bold}${red}ERROR:${normal} Option -$OPTARG requires an argument.\n" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
ulimit -c unlimited
# check if OPENCAPI boards exists
# we get number of cards in system
ocapi_check=`ls /dev/ocxl 2>/dev/null | wc -l`
if [ $ocapi_check -eq 0 ]; then
printf "${bold}${red}ERROR:${normal} No OpenCAPI devices found\n"
exit 1
fi
if [ -n "$card" ]; then
if [[ $card != *":"* ]]; then
# if card argument is just the card slot (with no :) then add the necessary stuff around it
card=$(printf '%.4x:00:00.0' "0x${card}")
fi
else
select_cards
# Convert the slot number into a 000x:00:00.0 slot number
card=$(printf '%.4x:00:00.0' "0x${c}")
fi
#echo "card selected is : $card"
echo -e "${blue}Checking if card $card is locked${normal}"
LockDir="$LockDirPrefix$card" # taken from oc-utils-common.sh
# First step: create the dirname of $LockDir (typically /var/ocxl)
# in case it is not yet existing ("mkdir -p" always successful even if dir already exists)
mkdir -p `dirname $LockDir`
# Second step: trying to create $LockDir locking directory (typically into /var/ocxl)
# and testing if the creation succeeded ("mkdir" fails if dir already exists)
if mkdir $LockDir 2>/dev/null; then
echo -e "${blue}$LockDir created during oc-reset${normal}"
# The following line prepares a cleaning of the newly created dir when script will output
trap 'rm -rf "$LockDir";echo -e "${blue}$LockDir removed at the end of oc-reset${normal}"' EXIT
else
echo
printf "${bold}${red}ERROR:${normal} $LockDir is already existing\n"
printf " => Card has been locked already!\n"
DateLastBoot=`who -b | awk '{print $3 " " $4}'`
EpochLastBoot=`date -d "$DateLastBoot" +%s`
EpochLockDir=`stat --format=%Y $LockDir`
DateLockDir=`date --date @$EpochLockDir`
if [ $EpochLockDir -lt $EpochLastBoot ]; then
echo
echo "Last BOOT: `date --date @$EpochLastBoot` ($EpochLastBoot)"
echo "Last LOCK modification: $DateLockDir ($EpochLockDir)"
echo "$LockDir modified BEFORE last boot"
echo;echo "======================================================="
echo "LOCK is not supposed to still be here"
echo " ==> Deleting and recreating $LockDir"
rmdir $LockDir
mkdir $LockDir
# The following line prepares a cleaning of the newly created dir when script will output
trap 'rm -rf "$LockDir";echo -e "${blue}$LockDir remove at the end of oc-reset${normal}"' EXIT
else
echo "$LockDir modified AFTER last boot"
printf "${bold}${red}ERROR:${normal} Card has been recently locked!\n"
echo "Exiting..."
exit 10
fi
fi
reset_card $card factory " Resetting OpenCAPI card in slot $card"