From patchwork Thu Aug 11 06:38:43 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell Currey X-Patchwork-Id: 658073 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3s8yzg0qfLz9sCY for ; Thu, 11 Aug 2016 16:39:27 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3s8yzf731MzDr0r for ; Thu, 11 Aug 2016 16:39:26 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from russell.cc (russell.cc [IPv6:2404:9400:2:0:216:3eff:fee0:3370]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3s8yzZ6rgYzDqJ4 for ; Thu, 11 Aug 2016 16:39:22 +1000 (AEST) Received: from snap.ozlabs.ibm.com (static-82-10.transact.net.au [122.99.82.10]) by russell.cc (OpenSMTPD) with ESMTPSA id f7221646 TLS version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO; Thu, 11 Aug 2016 06:39:24 +0000 (UTC) From: Russell Currey To: skiboot@lists.ozlabs.org Date: Thu, 11 Aug 2016 16:38:43 +1000 Message-Id: <20160811063843.22734-1-ruscur@russell.cc> X-Mailer: git-send-email 2.9.2 Subject: [Skiboot] [PATCH] external: NPU hardware procedure script X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alistair@popple.id.au MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Performing NPU hardware procedures requires some config space magic. Put all that magic into a script, so you can just specify the target device and the procedure number. Signed-off-by: Russell Currey --- external/npu/run_procedure.sh | 119 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 external/npu/run_procedure.sh diff --git a/external/npu/run_procedure.sh b/external/npu/run_procedure.sh new file mode 100755 index 0000000..f61ec8d --- /dev/null +++ b/external/npu/run_procedure.sh @@ -0,0 +1,119 @@ +#!/bin/bash + +# Copyright 2016 IBM Corp. +# +# 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. +# + +function usage() { + echo -e "$0: run a NPU hardware procedure (requires root)\n" + echo -e "Usage: $0 \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"