def bind(self, drvname): """Bind the PCI device to driver 'drvname'.""" _LOG.debug("binding device '%s' to driver '%s'%s", self._pci_info["pciaddr"], drvname, self._proc.hostmsg) failmsg = f"failed to bind device '{self._pci_info['pciaddr']}' to driver '{drvname}'" \ f"{self._proc.hostmsg}" drvpath = Path(f"/sys/bus/pci/drivers/{drvname}") if not FSHelpers.exists(drvpath, proc=self._proc): raise Error( f"{failmsg}':\npath '{drvpath}' does not exist{self._proc.hostmsg}" ) cur_drvname = self.get_driver_name() if cur_drvname == drvname: _LOG.debug("device '%s' is already bound to driver '%s'%s", self._pci_info["pciaddr"], drvname, self._proc.hostmsg) return if cur_drvname: raise Error( f"{failmsg}:\nit is already bound to driver '{cur_drvname}'") # At this point we do not know if the driver supports this PCI ID. So start with the # assumption that it does not, in which case writing to the 'new_id' file should do both: # * make the driver aware of the PCI ID # * bind the device path = f"{drvpath}/new_id" val = f"{self._pci_info['vendorid']} {self._pci_info['devid']}" bound = True try: with self._proc.open(path, "wt") as fobj: _LOG.debug("writing '%s' to file '%s'", val, path) fobj.write(val) except Error as err: bound = False if not bound: # Probably the driver already knows about this PCI ID. Use the 'bind' file in this case. path = f"{drvpath}/bind" val = self._pci_info["pciaddr"] with self._proc.open(path, "wt") as fobj: _LOG.debug("writing '%s' to file '%s'", val, path) try: fobj.write(val) except Error as err: raise Error( f"{failmsg}:\n{err}\n{self.get_new_dmesg()}") from err # Verify that the device is bound to the driver. if not self._get_driver()[1]: raise Error(f"{failmsg}\n{self.get_new_dmesg()}") _LOG.debug("binded device '%s' to driver '%s'%s\n%s", self._pci_info["pciaddr"], drvname, self._proc.hostmsg, self.get_new_dmesg())
def _get_driver(self): """ Find out whether the PCI device is bound to any driver. If it is not, returns the '(None, None)' tuple. Otherwise returns a tuple of: * driver name * driver sysfs path """ drvpath = Path(f"{self._devpath}/driver") if not FSHelpers.exists(drvpath, proc=self._proc): return (None, None) drvpath = FSHelpers.abspath(drvpath, proc=self._proc) drvname = Path(drvpath).name return (drvname, drvpath)
def _get_hw_addr(self): """ Return the hardware address for the NIC corresponding to the network interface. Typically the hardware address is a PCI address, such as '0000:04:00.0'. """ # The "device" symlink leads to the sysfs subdirectory corresponding to the underlying NIC. path = self._sysfsbase / "device" if not FSHelpers.exists(path, proc=self._proc): raise ErrorNotFound( f"cannot find network interface '{self.ifname}':\n" f"path '{path}' does not exist{self._proc.hostmsg}'") # The name of the subdirectory is the hardware address. path = FSHelpers.abspath(path, proc=self._proc) return path.name
def __init__(self, devid, cpunum, proc, dmesg=None): """The class constructor. The arguments are the same as in '_WultDeviceBase.__init__()'.""" super().__init__(devid, cpunum, proc, dmesg=dmesg) self._pci_info = None self._devpath = None path = Path(f"/sys/bus/pci/devices/{self._devid}") if not FSHelpers.exists(path, proc=proc): raise ErrorNotFound( f"cannot find device '{self._devid}'{self._proc.hostmsg}:\n" f"path {path} does not exist") self._devpath = FSHelpers.abspath(path, proc=self._proc) self._pci_info = LsPCI.LsPCI(proc).get_info(Path(self._devpath).name) if self.supported_devices and self._pci_info[ "devid"] not in self.supported_devices: supported = [ "%s - %s" % (key, val) for key, val in self.supported_devices.items() ] supported = "\n * ".join(supported) raise ErrorNotSupported( f"PCI device '{self._pci_info['pciaddr']}' (PCI ID " f"{self._pci_info['devid']}) is not supported by wult driver " f"{self.drvname}.\nHere is the list of supported PCI IDs:\n* " f"{supported}") self.info["name"] = "Intel I210" self.info["devid"] = self._pci_info["pciaddr"] if self.supported_devices: self.info["descr"] = self.supported_devices[ self._pci_info["devid"]] else: self.info["name"] = self._pci_info["name"] self.info["descr"] = self.info['name'].capitalize() self.info["descr"] += f". PCI address {self._pci_info['pciaddr']}, Vendor ID " \ f"{self._pci_info['vendorid']}, Device ID {self._pci_info['devid']}." self.info["aspm_enabled"] = self._pci_info["aspm_enabled"]