ia64/xen-unstable

view tools/python/xen/xend/server/pciquirk.py @ 13642:ba3ec84c9423

[XEND] Add missing ConsoleController.py

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Jan 26 02:44:35 2007 +0000 (2007-01-26)
parents 5bed7bc05c8a
children 7869f58ff551
line source
1 from xen.xend.XendLogging import log
2 from xen.xend.XendError import XendError, VmError
3 import sys
4 import os.path
5 from xen.xend.sxp import *
7 QUIRK_SYSFS_NODE = "/sys/bus/pci/drivers/pciback/quirks"
8 QUIRK_CONFIG_FILE = "/etc/xen/xend-pci-quirks.sxp"
9 PERMISSIVE_CONFIG_FILE = "/etc/xen/xend-pci-permissive.sxp"
10 PERMISSIVE_SYSFS_NODE = "/sys/bus/pci/drivers/pciback/permissive"
12 class PCIQuirk:
13 def __init__( self, vendor, device, subvendor, subdevice, domain, bus, slot, func):
14 self.vendor = vendor
15 self.device = device
16 self.subvendor = subvendor
17 self.subdevice = subdevice
18 self.domain = domain
19 self.bus = bus
20 self.slot = slot
21 self.func = func
23 self.devid = "%04x:%04x:%04x:%04x" % (vendor, device, subvendor, subdevice)
24 self.pciid = "%04x:%02x:%02x.%01x" % (domain, bus, slot, func)
25 self.quirks = self.__getQuirksByID()
27 self.__sendQuirks()
28 self.__sendPermDevs()
30 def __matchPCIdev( self, list ):
31 ret = False
32 if list == None:
33 return False
34 for id in list:
35 if id.startswith(self.devid[:9]): # id's vendor and device ID match
36 skey = id.split(':')
37 size = len(skey)
38 if (size == 2): # subvendor/subdevice not suplied
39 ret = True
40 break
41 elif (size == 4): # check subvendor/subdevice
42 # check subvendor
43 subven = '%04x' % self.subvendor
44 if ((skey[2] != 'FFFF') and
45 (skey[2] != 'ffff') and
46 (skey[2] != subven)):
47 continue
48 # check subdevice
49 subdev = '%04x' % self.subdevice
50 if ((skey[3] != 'FFFF') and
51 (skey[3] != 'ffff') and
52 (skey[3] != subdev)):
53 continue
54 ret = True
55 break
56 else:
57 log.debug("WARNING: invalid configuration entry: %s" % id)
58 ret = False
59 break
60 return ret
62 def __getQuirksByID( self ):
63 if os.path.exists(QUIRK_CONFIG_FILE):
64 try:
65 fin = file(QUIRK_CONFIG_FILE, 'rb')
66 try:
67 pci_quirks_config = parse(fin)
68 finally:
69 fin.close()
70 if pci_quirks_config is None:
71 pci_quirks_config = ['xend-pci-quirks']
72 else:
73 pci_quirks_config.insert(0, 'xend-pci-quirks')
74 self.pci_quirks_config = pci_quirks_config
75 except Exception, ex:
76 raise XendError("Reading config file %s: %s" %
77 (QUIRK_CONFIG_FILE, str(ex)))
78 else:
79 log.info("Config file does not exist: %s" % QUIRK_CONFIG_FILE)
80 self.pci_quirks_config = ['xend-pci-quirks']
82 devices = children(self.pci_quirks_config)
83 for dev in devices:
84 ids = child_at(child(dev,'pci_ids'),0)
85 fields = child_at(child(dev,'pci_config_space_fields'),0)
86 if self.__matchPCIdev( ids ):
87 log.info("Quirks found for PCI device [%s]" % self.devid)
88 return fields
90 log.info("NO quirks found for PCI device [%s]" % self.devid)
91 return []
93 def __sendQuirks(self):
94 for quirk in self.quirks:
95 log.debug("Quirk Info: %04x:%02x:%02x.%1x-%s" % (self.domain,
96 self.bus, self.slot, self.func, quirk))
97 try:
98 f = file(QUIRK_SYSFS_NODE ,"w")
99 f.write( "%04x:%02x:%02x.%1x-%s" % (self.domain, self.bus,
100 self.slot, self.func, quirk) )
101 f.close()
102 except Exception, e:
103 raise VmError("pci: failed to open/write/close quirks " +
104 "sysfs node - " + str(e))
106 def __devIsUnconstrained( self ):
107 if os.path.exists(PERMISSIVE_CONFIG_FILE):
108 try:
109 fin = file(PERMISSIVE_CONFIG_FILE, 'rb')
110 try:
111 pci_perm_dev_config = parse(fin)
112 finally:
113 fin.close()
114 if pci_perm_dev_config is None:
115 pci_perm_dev_config = ['']
116 else:
117 pci_perm_dev_config.insert(0, '')
118 self.pci_perm_dev_config = pci_perm_dev_config
119 except Exception, ex:
120 raise XendError("Reading config file %s: %s" %
121 (PERMISSIVE_CONFIG_FILE,str(ex)))
122 else:
123 log.info("Config file does not exist: %s" % PERMISSIVE_CONFIG_FILE)
124 self.pci_perm_dev_config = ['xend-pci-perm-devs']
126 devices = child_at(child(pci_perm_dev_config, 'unconstrained_dev_ids'),0)
127 if self.__matchPCIdev( devices ):
128 log.debug("Permissive mode enabled for PCI device [%s]" %
129 self.devid)
130 return True
131 log.debug("Permissive mode NOT enabled for PCI device [%s]" %
132 self.devid)
133 return False
135 def __sendPermDevs(self):
136 if self.__devIsUnconstrained( ):
137 log.debug("Unconstrained device: %04x:%02x:%02x.%1x" %
138 (self.domain, self.bus, self.slot, self.func))
139 try:
140 f = file(PERMISSIVE_SYSFS_NODE ,"w")
141 f.write( "%04x:%02x:%02x.%1x" % (self.domain, self.bus,
142 self.slot, self.func))
143 f.close()
144 except Exception, e:
145 raise VmError("pci: failed to open/write/close permissive " +
146 "sysfs node: " + str(e))