Patchwork [1/2] Support sysfs in ofpath

login
register
mail settings
Submitter Milan Kupcevic
Date Aug. 4, 2011, 8:11 p.m.
Message ID <1312488675-13139-1-git-send-email-milan@physics.harvard.edu>
Download mbox | patch
Permalink /patch/108566/
State New
Headers show

Comments

Milan Kupcevic - Aug. 4, 2011, 8:11 p.m.
Get scsi, sata, and firewire drive info from sysfs as legacy /proc/scsi
interface does not exist anymore.

Signed-off-by: Milan Kupcevic <milan@physics.harvard.edu>
---
 ybin/ofpath |  261 ++++++++++++++++++++++++++---------------------------------
 1 files changed, 116 insertions(+), 145 deletions(-)

Patch

diff --git a/ybin/ofpath b/ybin/ofpath
index aff5583..7557484 100755
--- a/ybin/ofpath
+++ b/ybin/ofpath
@@ -3,6 +3,9 @@ 
 ###############################################################################
 ##
 ## ofpath: determine OpenFirmware path from unix device node
+##
+## Copyright (C) 2010, 2011 Milan Kupcevic
+##
 ## Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 ##
 ## Portions based on show_of_path.sh:
@@ -27,7 +30,7 @@ 
 
 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
 PRG="${0##*/}"
-VERSION=1.0.7
+VERSION=1.0.7+debian2
 DEBUG=0
 export LC_COLLATE=C
 
@@ -36,9 +39,10 @@  version()
 {
 echo \
 "$PRG $VERSION
-Written by Ethan Benson
+Written by Ethan Benson, portions rewritten by Milan Kupcevic
 Portions based on show_of_path.sh written by Olaf Hering
 
+Copyright (C) 2010, 2011 Milan Kupcevic
 Copyright (C) 2000, 2001, 2002, 2003 Ethan Benson
 Portions Copyright (C) 2000 Olaf Hering
 This is free software; see the source for copying conditions.  There is NO
@@ -181,158 +185,125 @@  printhost()
     return 0
 }
 
+# read OpenFirmware device path from its corresponding devspec
+find_of_path()
+{
+  [ -z "$1" ] && return
+  [ -f "$1/devspec" ] && {
+    OF_PATH="`cat $1/devspec`"
+    SYS_PATH="$1"
+    return
+  }
+  find_of_path "${1%/*}"
+}
+
 ## this finds information we need on both newworld and oldworld macs.
 ## mainly what scsi host a disk is attached to.
 scsiinfo()
 {
-    ## see if system has scsi at all
-    if [ ! -f /proc/scsi/scsi ] ; then
-	local kver="$(uname -r)"
-	case "$kver" in
-	    2.5.*|2.6.*)
-		if [ -d /sys/bus/scsi/devices -a \
-		    -n "$(ls /sys/bus/scsi/devices 2>/dev/null)" ] ; then
-		    echo 1>&2 "$PRG: /proc/scsi/scsi does not exist"
-		    echo 1>&2 "$PRG: Make sure you compiled your kernel with CONFIG_SCSI_PROC_FS=y"
-		    return 1
-		fi
-		;;
-	esac
-	echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
-	return 1
-    fi
-
-    ## first we have to figure out the SCSI ID, have to do that
-    ## anyway [to] find the attached scsi disks = "Direct-Access" and
-    ## stop at sda=1 sdb=2 or whatever count in 3 lines steps
-
-    ## get last letter of device node, ie sda -> a
-    SUBNODE=${DEVNODE##*sd}
-
-    ## turn SUBNODE above into a number starting at 1, ie a -> 1
-    SUBDEV="$(smalltr $SUBNODE)"
-    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SUBNODE=$SUBNODE SUBDEV=$SUBDEV"
-
-    DEVCOUNT=0
-
-    ## copy scsi file into a variable removing "Attached Devices"
-    ## which is the first line. this avoids a lot of
-    ## [incmopatible] crap later, and improves readability.
-
-    ## find number of lines once and recycle that number, to save
-    ## some time (linecount is a bit slow). subtract one line
-    ## to scrap Attached Devices:
-
-    SCSILINES="$(($(linecount /proc/scsi/scsi) - 1))"
-
-    if [ "$SUBDEV" -gt "$(cat /proc/scsi/scsi | grep Direct-Access | linecount)" ] ; then
-	echo 1>&2 "$PRG: /dev/$DEVNODE: Device not configured"
-	return 1
-    fi
-
-    PROCSCSI="$(cat /proc/scsi/scsi | tail -n $SCSILINES)"
-
-    for i in $(smallseq $(($SCSILINES / 3))) ; do
-
-	## put every scsi device into one single line
-	DEVINFO="$(echo "$PROCSCSI" | head -n $(($i * 3)) | tail -n 3)"
-	[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVINFO=$DEVINFO"
-
-	## cut the type field, expect "Direct-Access" later.
-	DEVTYPE="$(v=$(echo ${DEVINFO##*Type: }) ; echo ${v%% *})"
-	[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVTYPE=$DEVTYPE"
-
-	## get the device id.
-	DEVID="$(v=$(echo ${DEVINFO##*Id: }) ; n=$(echo ${v%% *}) ; echo ${n#*0})"
-	[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVID=$DEVID"
-
-	## get the scsi host id.
-	DEVHOST="$(v=$(echo ${DEVINFO##*Host: scsi}) ; echo ${v%% *})"
-	[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVHOST=$DEVHOST"
-
-	if [ "$DEVTYPE" = "Direct-Access" ] || [ "$DEVTYPE" = "Direct-Access-RBC" ] ; then
-	    DEVCOUNT="$(($DEVCOUNT + 1))"
-	    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVCOUNT=$DEVCOUNT"
-	    if [ "$SUBDEV" = "$DEVCOUNT" ] ; then
-		DEVICE_HOST=$DEVHOST
-		DEVICE_ID=$DEVID
-		[ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
-		break
-	    fi
-	fi
-    done
-
-    ## figure out what the scsi driver is, it is /proc/scsi/dirname.
-    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: DEVICE_HOST=$DEVICE_HOST"
-    SCSI_DRIVER="$(x=`ls /proc/scsi/*/$DEVICE_HOST 2>/dev/null | cat` ; y=`echo ${x##*proc/scsi/}` ; echo ${y%%/*})"
-    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_DRIVER=$SCSI_DRIVER"
-
-    ## figure out which host we found.
-    SCSI_HOSTNUMBER="$(v=`ls /proc/scsi/$SCSI_DRIVER/* 2>/dev/null | cat | grep -n "$DEVICE_HOST\>"` ; echo ${v%%:*})"
-    [ "$DEBUG" = 1 ] && echo 1>&2 "$PRG: DEBUG: SCSI_HOSTNUMBER=$SCSI_HOSTNUMBER"
-
+  SYS_DEVICE="`readlink -f /sys/block/$DEVNODE/device`"
+
+  [ -e "$SYS_DEVICE" ] && {
+ 
+    SCSI_DEVICE=${SYS_DEVICE##*/}
+    HOST_ID=${SCSI_DEVICE%%:*}
+    BUS_ID=`T=${SCSI_DEVICE#*:}; echo ${T%%:*}`
+    DEVICE_ID=`T=${SCSI_DEVICE#*:*:}; echo ${T%%:*}`
+    LUN_ID=${SCSI_DEVICE##*:}
+
+    OF_PATH=;find_of_path "$SYS_DEVICE"
+
+    SCSI_DRIVER=`cat /sys/class/scsi_host/host$HOST_ID/proc_name 2>/dev/null`
+
+    SCSI_NAME=`cat /proc/device-tree$OF_PATH/name 2>/dev/null`
+
+    IEEE1394_ID=`cat /sys/block/$DEVNODE/device/ieee1394_id 2>/dev/null`
+    IEEE1394_ID=${IEEE1394_ID%%:*}
+
+    PLUG_ID=$(ls -dv $SYS_PATH/host* 2>/dev/null | grep -n "/host$HOST_ID$")
+    PLUG_ID=$((${PLUG_ID%%:*}-1))
+
+    PART=${PARTITION:+:$PARTITION}
+
+    [ "$LUN_ID" != "0" ] && LUNX=`printf ",%x" $LUN_ID`
+
+    [ "$DEBUG" = "1" ] && {
+      echo SYS_DEVICE=$SYS_DEVICE
+      echo SCSI_DEVICE=$SCSI_DEVICE
+      echo HOST_ID=$HOST_ID
+      echo BUS_ID=$BUS_ID
+      echo DEVICE_ID=$DEVICE_ID
+      echo LUN_ID=$LUN_ID
+      echo PLUG_ID=$PLUG_ID
+      echo IEEE1394_ID=$IEEE1394_ID
+      echo SCSI_NAME=$SCSI_NAME
+      echo SCSI_DRIVER=$SCSI_DRIVER
+      echo SYS_PATH=$SYS_PATH
+      echo OF_PATH=$OF_PATH
+      echo PART=$PART
+      echo LUNX=$LUNX
+    }
+ 
+    SCSI_DRIVER=${SCSI_DRIVER:?}
+ 
+    [ -z "$OF_PATH" ] && {
+      echo 1>&2 "Cannot find $DEVNODE among OpenFirmware registered devices"
+ 
+      exit 1
+    }
+ 
     return 0
+  }
+ 
+  echo 1>&2 "Link /sys/block/$DEVNODE/device does not exist"
+ 
+  exit 1
 }
 
-## generic function that can find OF device paths for scsi devices,
+## add OF device node to the host OF path,
 ## must be run after scsiinfo().
 scsi_ofpath()
 {
-    case "$SCSI_DRIVER" in
-	aic7xxx)
-	    HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
-			lgrep "$i" "^ADPT" "^pci900[45]" "^pciclass,01000" ; done)"
-	    DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-	    echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
-	    ;;
-	sym53c8xx)
-	    HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
-			lgrep "$i" "^Symbios" "^pci1000" "^pciclass,01000" ; done)"
-	    DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-	    echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
-	    ;;
-	mesh)
-	    HOST_LIST="$(for i in `find /proc/device-tree -name compatible` ; do
-			lgrep "$i" "mesh" ; done)"
-	    DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-	    echo "${DEVICE_PATH##*device-tree}/@$DEVICE_ID:$PARTITION"
-	    ;;
-	ata_k2|sata_svw)
-            #Not all G5 device trees have a compatible "k2-sata" node 
-            #per channel use parent
-	    HOST_LIST="$(for i in `find /proc/device-tree -name compatible ` ; do
-			lgrep "$i" "k2-s-ata" ; done | sort)"
-	    DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-	    K2_DEVICE_ID=0
-	    while [ "$DEVICE_PATH" = "" ] ; do
-	        SCSI_HOSTNUMBER=`expr $SCSI_HOSTNUMBER - 1`
-	        let "K2_DEVICE_ID += 1"
-	        DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-	    done
-	    echo "${DEVICE_PATH##*device-tree}/k2-sata@$K2_DEVICE_ID/disk@0:$PARTITION"
-	    ;;
-	usb-storage)
-            HOST_LIST="$(for i in `find /proc/device-tree -name name | grep usb` ; do
-                        lgrep "$i" "disk" ; done)"
-            DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-            echo "${DEVICE_PATH##*device-tree}:$PARTITION"
-	    ;;
-	sbp2|"")
-            # sbp-2 driver may not have a dir in /proc/scsi
-            HOST_LIST="$(for i in `find /proc/device-tree -name name` ; do
-                        lgrep "$i" "sbp-2" ; done)"
-            if [ "$SCSI_HOSTNUMBER" = "" ] ; then
-                SCSI_HOSTNUMBER=1
-            fi
-            DEVICE_PATH="$(printhost $SCSI_HOSTNUMBER $HOST_LIST)"
-            echo "${DEVICE_PATH##*device-tree}/disk@0:$PARTITION"
-	    ;;
-	*)
-	    echo 1>&2 "$PRG: Driver: $SCSI_DRIVER is not supported"
-	    return 1
-	    ;;
+  if [ -d "/proc/device-tree$OF_PATH/sas" ] ; then
+
+    SAS_ID=$(( ($BUS_ID << 16) | ($DEVICE_ID << 8) | $LUN_ID))
+    printf "%s/sas/disk@%x%s\n" $OF_PATH $SAS_ID $LUNX$PART
+
+  elif [ -d "/proc/device-tree$OF_PATH/scsi@`printf %x $BUS_ID`" ] ; then
+
+    printf "%s/scsi@%x/@%x%s\n" $OF_PATH $BUS_ID $DEVICE_ID $LUNX$PART
+
+  else
+
+    case "$SCSI_DRIVER-$SCSI_NAME" in
+      aic7xxx-*|sym53c8xx-*|mesh-*|*-scsi)
+        printf "%s/@%x%s\n" $OF_PATH $DEVICE_ID $LUNX$PART
+        ;;
+      sata_sil-*|sata_vsc-*)
+        printf "%s/@%x%s\n" $OF_PATH $(($PLUG_ID * 2)) $PART
+        ;;
+      sata_svw-*|*-pci-ata)
+        printf "%s/@%x/@%x%s\n" $OF_PATH $PLUG_ID $DEVICE_ID $PART
+        ;;
+      pata_macio-*)
+        printf "%s/@%x%s\n" $OF_PATH $DEVICE_ID $PART
+        ;;
+      sbp2-*)
+        IEEE1394_ID=${IEEE1394_ID:?}
+        printf "%s/node@%s/sbp-2/@%x%s\n" $OF_PATH $IEEE1394_ID $DEVICE_ID $PART
+        ;;
+      ipr-obsidian)
+        printf "%s/@%x,%x%s\n" $OF_PATH $BUS_ID $DEVICE_ID $LUNX$PART
+        ;;
+      *)
+        echo 1>&2 "${PRG}: ${SCSI_NAME} (${SCSI_DRIVER}) is not supported"
+        return 1
+        ;;
     esac
-    return 0
+
+  fi
+
+  return 0
 }
 
 ide_ofpath()
@@ -432,11 +403,11 @@  ide_ofpath()
 
 	case "$DEVNODE" in
 	    hda|hdc|hde|hdg|hdi|hdk|hdm|hdo)
-		echo "${DEVSPEC}${MASTER}:$PARTITION"
+		echo "${DEVSPEC}${MASTER}${PARTITION:+:${PARTITION}}"
 		return 0
 		;;
 	    hdb|hdd|hdf|hdh|hdj|hdl|hdn|hdp)
-		echo "${DEVSPEC}${SLAVE}:$PARTITION"
+		echo "${DEVSPEC}${SLAVE}${PARTITION:+:${PARTITION}}"
 		return 0
 		;;
 	    *)