1*4882a593Smuzhiyun#!/bin/sh 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 3*4882a593Smuzhiyun 4*4882a593Smuzhiyunpe_ok() { 5*4882a593Smuzhiyun local dev="$1" 6*4882a593Smuzhiyun local path="/sys/bus/pci/devices/$dev/eeh_pe_state" 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun # if a driver doesn't support the error handling callbacks then the 9*4882a593Smuzhiyun # device is recovered by removing and re-probing it. This causes the 10*4882a593Smuzhiyun # sysfs directory to disappear so read the PE state once and squash 11*4882a593Smuzhiyun # any potential error messages 12*4882a593Smuzhiyun local eeh_state="$(cat $path 2>/dev/null)" 13*4882a593Smuzhiyun if [ -z "$eeh_state" ]; then 14*4882a593Smuzhiyun return 1; 15*4882a593Smuzhiyun fi 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun local fw_state="$(echo $eeh_state | cut -d' ' -f1)" 18*4882a593Smuzhiyun local sw_state="$(echo $eeh_state | cut -d' ' -f2)" 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun # If EEH_PE_ISOLATED or EEH_PE_RECOVERING are set then the PE is in an 21*4882a593Smuzhiyun # error state or being recovered. Either way, not ok. 22*4882a593Smuzhiyun if [ "$((sw_state & 0x3))" -ne 0 ] ; then 23*4882a593Smuzhiyun return 1 24*4882a593Smuzhiyun fi 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun # A functioning PE should have the EEH_STATE_MMIO_ACTIVE and 27*4882a593Smuzhiyun # EEH_STATE_DMA_ACTIVE flags set. For some goddamn stupid reason 28*4882a593Smuzhiyun # the platform backends set these when the PE is in reset. The 29*4882a593Smuzhiyun # RECOVERING check above should stop any false positives though. 30*4882a593Smuzhiyun if [ "$((fw_state & 0x18))" -ne "$((0x18))" ] ; then 31*4882a593Smuzhiyun return 1 32*4882a593Smuzhiyun fi 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun return 0; 35*4882a593Smuzhiyun} 36*4882a593Smuzhiyun 37*4882a593Smuzhiyuneeh_supported() { 38*4882a593Smuzhiyun test -e /proc/powerpc/eeh && \ 39*4882a593Smuzhiyun grep -q 'EEH Subsystem is enabled' /proc/powerpc/eeh 40*4882a593Smuzhiyun} 41*4882a593Smuzhiyun 42*4882a593Smuzhiyuneeh_one_dev() { 43*4882a593Smuzhiyun local dev="$1" 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun # Using this function from the command line is sometimes useful for 46*4882a593Smuzhiyun # testing so check that the argument is a well-formed sysfs device 47*4882a593Smuzhiyun # name. 48*4882a593Smuzhiyun if ! test -e /sys/bus/pci/devices/$dev/ ; then 49*4882a593Smuzhiyun echo "Error: '$dev' must be a sysfs device name (DDDD:BB:DD.F)" 50*4882a593Smuzhiyun return 1; 51*4882a593Smuzhiyun fi 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun # Break it 54*4882a593Smuzhiyun echo $dev >/sys/kernel/debug/powerpc/eeh_dev_break 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun # Force an EEH device check. If the kernel has already 57*4882a593Smuzhiyun # noticed the EEH (due to a driver poll or whatever), this 58*4882a593Smuzhiyun # is a no-op. 59*4882a593Smuzhiyun echo $dev >/sys/kernel/debug/powerpc/eeh_dev_check 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun # Default to a 60s timeout when waiting for a device to recover. This 62*4882a593Smuzhiyun # is an arbitrary default which can be overridden by setting the 63*4882a593Smuzhiyun # EEH_MAX_WAIT environmental variable when required. 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun # The current record holder for longest recovery time is: 66*4882a593Smuzhiyun # "Adaptec Series 8 12G SAS/PCIe 3" at 39 seconds 67*4882a593Smuzhiyun max_wait=${EEH_MAX_WAIT:=60} 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun for i in `seq 0 ${max_wait}` ; do 70*4882a593Smuzhiyun if pe_ok $dev ; then 71*4882a593Smuzhiyun break; 72*4882a593Smuzhiyun fi 73*4882a593Smuzhiyun echo "$dev, waited $i/${max_wait}" 74*4882a593Smuzhiyun sleep 1 75*4882a593Smuzhiyun done 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun if ! pe_ok $dev ; then 78*4882a593Smuzhiyun echo "$dev, Failed to recover!" 79*4882a593Smuzhiyun return 1; 80*4882a593Smuzhiyun fi 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun echo "$dev, Recovered after $i seconds" 83*4882a593Smuzhiyun return 0; 84*4882a593Smuzhiyun} 85*4882a593Smuzhiyun 86