ia64/xen-unstable

changeset 19530:0b9b6d5a61c1

sbdf2devicepath: converts SBDF into device path.

'SBDF' format is "[SEG#:]BUS#:DEV#.FUNC#"
ex) 0000:0a:1f.3
Device path format is "HID[:UID]-DEV#.FUNC#[-DEV#.FUNC#[...]]"
ex) PNP0A08:0-2.0-0.0

The command can be executed as follows.

# sbdf2devicepath 0a:1f.3
PNP0A08:0-2.0-0.0

Signed-off-by: Yuji Shimada <shimada-yxb@necst.nec.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Apr 09 09:59:16 2009 +0100 (2009-04-09)
parents 90d5fb694620
children 72d4c5c83508
files tools/misc/sbdf2devicepath
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/misc/sbdf2devicepath	Thu Apr 09 09:59:16 2009 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +#!/usr/bin/env python
     1.5 +#  -*- mode: python; -*-
     1.6 +#============================================================================
     1.7 +# This library is free software; you can redistribute it and/or
     1.8 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.9 +# License as published by the Free Software Foundation.
    1.10 +#
    1.11 +# This library is distributed in the hope that it will be useful,
    1.12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.14 +# Lesser General Public License for more details.
    1.15 +#
    1.16 +# You should have received a copy of the GNU Lesser General Public
    1.17 +# License along with this library; if not, write to the Free Software
    1.18 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.19 +#============================================================================
    1.20 +# Copyright (c) 2009, NEC Corporation.
    1.21 +#============================================================================
    1.22 +# This script converts SBDF into device path.
    1.23 +#   'SBDF' format is "[SEG#:]BUS#:DEV#.FUNC#"
    1.24 +#       ex) 0000:0a:1f.3
    1.25 +#   Device path format is "HID[:UID]-DEV#.FUNC#[-DEV#.FUNC#[...]]"
    1.26 +#       ex) PNP0A08:0-2.0-0.0
    1.27 +#=============================================================================
    1.28 +
    1.29 +import sys
    1.30 +import os
    1.31 +
    1.32 +# add fallback path for non-native python path installs if needed
    1.33 +sys.path.append('/usr/lib/python')
    1.34 +sys.path.append('/usr/lib64/python')
    1.35 +from xen.util.pci import *
    1.36 +
    1.37 +SYSFS_ACPI_DEVS_PATH = '/firmware/acpi/namespace/ACPI/_SB'
    1.38 +
    1.39 +def find_hid_uid(dom, b, d, f):
    1.40 +    obj_list = os.listdir(sb_path)
    1.41 +    for obj in obj_list:
    1.42 +        obj_path = sb_path + '/' + obj.strip() + '/'
    1.43 +        if os.path.exists(obj_path + 'seg') and \
    1.44 +            os.path.exists(obj_path + 'bbn'):
    1.45 +            seg = open(obj_path + 'seg').read()
    1.46 +            bbn = open(obj_path + 'bbn').read()
    1.47 +            if int(seg) == dom and int(bbn) == b:
    1.48 +                hid = open(obj_path + 'hid').read()
    1.49 +                if os.path.exists(obj_path + 'uid') is False:
    1.50 +                    path_str = hid.strip()
    1.51 +                else:
    1.52 +                    uid = open(obj_path + 'uid').read()
    1.53 +                    path_str = hid.strip() + ':' + uid.strip()
    1.54 +                return path_str
    1.55 +    return None
    1.56 +
    1.57 +def make_device_path(dom, b, d, f):
    1.58 +    dev = PciDevice(dom, b, d, f)
    1.59 +    parent = dev.find_parent()
    1.60 +    if parent is None:
    1.61 +        path_str = find_hid_uid(dom, b, d, f)
    1.62 +        path_str = path_str + '-' + hex(d).replace('0x', '') + '.' + \
    1.63 +            hex(f).replace('0x', '')
    1.64 +        return path_str
    1.65 +    (pdom, pb, pd, pf) = parent
    1.66 +    path_str = make_device_path(pdom, pb, pd, pf)
    1.67 +    path_str = path_str + '-' + hex(d).replace('0x', '') + '.' + \
    1.68 +        hex(f).replace('0x', '')
    1.69 +    return path_str
    1.70 +
    1.71 +# main
    1.72 +if len(sys.argv) <> 2:
    1.73 +    print 'Usage: sbdf2devicepath SBDF\n'
    1.74 +else:
    1.75 +    sb_path = find_sysfs_mnt() + SYSFS_ACPI_DEVS_PATH
    1.76 +    if os.path.exists(sb_path):
    1.77 +        path = os.environ['PATH']
    1.78 +        os.environ['PATH'] = path + ':/sbin' + ':/user/sbin'
    1.79 +        sbdf = sys.argv[1]
    1.80 +        (dom, b, d, f) = parse_pci_name(sbdf)
    1.81 +        path_str = make_device_path(dom, b, d, f)
    1.82 +        print path_str
    1.83 +    else:
    1.84 +        print sb_path + ' not found.\n'
    1.85 +        print 'This command is only for linux 2.6.18.8 xen kernel.\n'