xref: /aosp_15_r20/external/sg3_utils/scripts/cciss_id (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1#!/bin/bash
2#
3# cciss_id
4#
5# Generates device node names according to the cciss naming rules
6#
7# Copyright (C) 2011 SUSE Linux Products GmbH
8# Author:
9#       Hannes Reinecke <[email protected]>
10#
11#
12#       This program is free software; you can redistribute it and/or modify it
13#       under the terms of the GNU General Public License as published by the
14#       Free Software Foundation version 2 of the License.
15#
16# This script generates a device node name which is compatible
17# with the 'cciss' device naming rules.
18# It is intended to provide backward-compatible names for the
19# 'hpsa' driver.
20#
21
22cciss_enumerate()
23{
24    local last_pci_dev=${1##0000:}
25    local cur_pci_dev
26    local cciss_num=0
27
28    for cur_pci_dev in $(lspci -n | tac | sed -n 's/\(..:..\..\) .* 103c:\(3220\|3230\|3238\|323a\|323b\) .*/\1/p') ; do
29	if [ "$cur_pci_dev" == "$last_pci_dev" ] ; then
30	    echo "$cciss_num"
31	    return;
32	fi
33	cciss_num=$(($cciss_num + 1))
34    done
35    echo "$cciss_num"
36}
37
38hpsa_lun_offset()
39{
40    local scsi_host=$1
41
42    scsi_id=$(lsscsi 2>/dev/null | sed -n "s/.\(${scsi_host}:[0-9]*:[0-9]*:[0-9]*\)..*disk .*/\1/p" | head -1)
43    echo ${scsi_id##*:}
44}
45
46DEVPATH=$1
47SCSIPATH=$(cd -P /sys$DEVPATH/device; echo $PWD)
48SCSIID=${SCSIPATH##*/}
49HOSTID=${SCSIID%%:*}
50LUNID=${SCSIID##*:}
51PCIPATH=${SCSIPATH%%/host*}
52PCIDEV=${PCIPATH##*/}
53HOSTPATH=${PCIPATH}/host${HOSTID}/scsi_host/host${HOSTID}
54read controller 2>/dev/null <${HOSTPATH}/ctlr_num || controller=$(cciss_enumerate $PCIDEV)
55
56# hpsa lies about the LUN ...
57disk_offset=$(hpsa_lun_offset $HOSTID)
58if [ "$disk_offset" ] ; then
59    disk=$(( $LUNID - $disk_offset ))
60else
61    disk=$LUNID
62fi
63
64if [ "$controller" ] && [ "$disk" ] ; then
65    echo "ID_CCISS=c${controller}d${disk}"
66fi
67