2009-03-02 16:18:11 +00:00
|
|
|
/*
|
2012-12-13 14:52:25 +00:00
|
|
|
* virpci.c: helper APIs for managing host PCI devices
|
|
|
|
*
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
* Copyright (C) 2009-2015 Red Hat, Inc.
|
2009-03-02 16:18:11 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-03-02 16:18:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-13 14:52:25 +00:00
|
|
|
#include "virpci.h"
|
2017-07-31 04:21:45 +00:00
|
|
|
#include "virnetdev.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2014-01-24 15:47:20 +00:00
|
|
|
#include "virkmod.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.pci");
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
#define PCI_SYSFS "/sys/bus/pci/"
|
|
|
|
#define PCI_ID_LEN 10 /* "XXXX XXXX" */
|
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIELinkSpeed,
|
|
|
|
VIR_PCIE_LINK_SPEED_LAST,
|
2019-01-20 16:30:15 +00:00
|
|
|
"", "2.5", "5", "8", "16",
|
|
|
|
);
|
2014-07-24 01:52:22 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIStubDriver,
|
|
|
|
VIR_PCI_STUB_DRIVER_LAST,
|
2015-10-23 09:54:07 +00:00
|
|
|
"none",
|
|
|
|
"pciback", /* XEN */
|
|
|
|
"vfio-pci", /* VFIO */
|
2019-01-20 16:30:15 +00:00
|
|
|
);
|
2015-10-23 09:54:07 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIHeader,
|
|
|
|
VIR_PCI_HEADER_LAST,
|
2016-03-15 11:22:03 +00:00
|
|
|
"endpoint",
|
|
|
|
"pci-bridge",
|
|
|
|
"cardbus-bridge",
|
2019-01-20 16:30:15 +00:00
|
|
|
);
|
2016-03-15 11:22:03 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virPCIDevice {
|
2015-12-15 08:44:35 +00:00
|
|
|
virPCIDeviceAddress address;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-07-30 09:37:43 +00:00
|
|
|
char *name; /* domain:bus:slot.function */
|
2009-03-02 16:18:11 +00:00
|
|
|
char id[PCI_ID_LEN]; /* product vendor */
|
2011-06-22 20:52:32 +00:00
|
|
|
char *path;
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
/* The driver:domain which uses the device */
|
|
|
|
char *used_by_drvname;
|
|
|
|
char *used_by_domname;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pcie_cap_pos;
|
|
|
|
unsigned int pci_pm_cap_pos;
|
2013-04-10 10:44:41 +00:00
|
|
|
bool has_flr;
|
|
|
|
bool has_pm_reset;
|
2013-04-10 10:09:23 +00:00
|
|
|
bool managed;
|
2015-10-23 09:54:07 +00:00
|
|
|
|
|
|
|
virPCIStubDriver stubDriver;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
|
|
|
/* used by reattach function */
|
2013-04-10 10:44:41 +00:00
|
|
|
bool unbind_from_stub;
|
|
|
|
bool remove_slot;
|
|
|
|
bool reprobe;
|
2009-03-02 16:18:11 +00:00
|
|
|
};
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virPCIDeviceList {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectLockable parent;
|
|
|
|
|
2013-07-05 18:46:35 +00:00
|
|
|
size_t count;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr *devs;
|
2009-10-27 17:30:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* For virReportOOMError() and virReportSystemError() */
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
/* Specifications referenced in comments:
|
|
|
|
* PCI30 - PCI Local Bus Specification 3.0
|
|
|
|
* PCIe20 - PCI Express Base Specification 2.0
|
|
|
|
* BR12 - PCI-to-PCI Bridge Architecture Specification 1.2
|
|
|
|
* PM12 - PCI Bus Power Management Interface Specification 1.2
|
|
|
|
* ECN_AF - Advanced Capabilities for Conventional PCI ECN
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Type 0 config space header length; PCI30 Section 6.1 Configuration Space Organization */
|
|
|
|
#define PCI_CONF_LEN 0x100
|
|
|
|
#define PCI_CONF_HEADER_LEN 0x40
|
|
|
|
|
|
|
|
/* PCI30 6.2.1 */
|
|
|
|
#define PCI_HEADER_TYPE 0x0e /* Header type */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_HEADER_TYPE_BRIDGE 0x1
|
|
|
|
#define PCI_HEADER_TYPE_MASK 0x7f
|
|
|
|
#define PCI_HEADER_TYPE_MULTI 0x80
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PCI30 6.2.1 Device Identification */
|
|
|
|
#define PCI_CLASS_DEVICE 0x0a /* Device class */
|
|
|
|
|
|
|
|
/* Class Code for bridge; PCI30 D.7 Base Class 06h */
|
|
|
|
#define PCI_CLASS_BRIDGE_PCI 0x0604
|
|
|
|
|
|
|
|
/* PCI30 6.2.3 Device Status */
|
|
|
|
#define PCI_STATUS 0x06 /* 16 bits */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_STATUS_CAP_LIST 0x10 /* Support Capability List */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PCI30 6.7 Capabilities List */
|
|
|
|
#define PCI_CAPABILITY_LIST 0x34 /* Offset of first capability list entry */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_CAP_FLAGS 2 /* Capability defined flags (16 bits) */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PM12 3.2.1 Capability Identifier */
|
|
|
|
#define PCI_CAP_ID_PM 0x01 /* Power Management */
|
|
|
|
/* PCI30 H Capability IDs */
|
|
|
|
#define PCI_CAP_ID_EXP 0x10 /* PCI Express */
|
|
|
|
/* ECN_AF 6.x.1.1 Capability ID for AF */
|
|
|
|
#define PCI_CAP_ID_AF 0x13 /* Advanced Features */
|
|
|
|
|
|
|
|
/* PCIe20 7.8.3 Device Capabilities Register (Offset 04h) */
|
|
|
|
#define PCI_EXP_DEVCAP 0x4 /* Device capabilities */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_DEVCAP_FLR (1<<28) /* Function Level Reset */
|
|
|
|
#define PCI_EXP_LNKCAP 0xc /* Link Capabilities */
|
2017-05-16 13:19:19 +00:00
|
|
|
#define PCI_EXP_LNKCAP_SPEED 0x0000f /* Maximum Link Speed */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_LNKCAP_WIDTH 0x003f0 /* Maximum Link Width */
|
|
|
|
#define PCI_EXP_LNKSTA 0x12 /* Link Status */
|
|
|
|
#define PCI_EXP_LNKSTA_SPEED 0x000f /* Negotiated Link Speed */
|
|
|
|
#define PCI_EXP_LNKSTA_WIDTH 0x03f0 /* Negotiated Link Width */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Header type 1 BR12 3.2 PCI-to-PCI Bridge Configuration Space Header Format */
|
|
|
|
#define PCI_PRIMARY_BUS 0x18 /* BR12 3.2.5.2 Primary bus number */
|
|
|
|
#define PCI_SECONDARY_BUS 0x19 /* BR12 3.2.5.3 Secondary bus number */
|
|
|
|
#define PCI_SUBORDINATE_BUS 0x1a /* BR12 3.2.5.4 Highest bus number behind the bridge */
|
|
|
|
#define PCI_BRIDGE_CONTROL 0x3e
|
|
|
|
/* BR12 3.2.5.18 Bridge Control Register */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_BRIDGE_CTL_RESET 0x40 /* Secondary bus reset */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PM12 3.2.4 Power Management Control/Status (Offset = 4) */
|
|
|
|
#define PCI_PM_CTRL 4 /* PM control and status register */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_PM_CTRL_STATE_MASK 0x3 /* Current power state (D0 to D3) */
|
|
|
|
#define PCI_PM_CTRL_STATE_D0 0x0 /* D0 state */
|
|
|
|
#define PCI_PM_CTRL_STATE_D3hot 0x3 /* D3 state */
|
|
|
|
#define PCI_PM_CTRL_NO_SOFT_RESET 0x8 /* No reset for D3hot->D0 */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* ECN_AF 6.x.1 Advanced Features Capability Structure */
|
|
|
|
#define PCI_AF_CAP 0x3 /* Advanced features capabilities */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_AF_CAP_FLR 0x2 /* Function Level Reset */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
#define PCI_EXP_FLAGS 0x2
|
|
|
|
#define PCI_EXP_FLAGS_TYPE 0x00f0
|
|
|
|
#define PCI_EXP_TYPE_DOWNSTREAM 0x6
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_BASE 0x100
|
|
|
|
#define PCI_EXT_CAP_LIMIT 0x1000
|
|
|
|
#define PCI_EXT_CAP_ID_MASK 0x0000ffff
|
|
|
|
#define PCI_EXT_CAP_OFFSET_SHIFT 20
|
|
|
|
#define PCI_EXT_CAP_OFFSET_MASK 0x00000ffc
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_ID_ACS 0x000d
|
|
|
|
#define PCI_EXT_ACS_CTRL 0x06
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_ACS_SV 0x01
|
|
|
|
#define PCI_EXT_CAP_ACS_RR 0x04
|
|
|
|
#define PCI_EXT_CAP_ACS_CR 0x08
|
|
|
|
#define PCI_EXT_CAP_ACS_UF 0x10
|
2017-11-03 12:09:47 +00:00
|
|
|
#define PCI_EXT_CAP_ACS_ENABLED (PCI_EXT_CAP_ACS_SV | \
|
|
|
|
PCI_EXT_CAP_ACS_RR | \
|
|
|
|
PCI_EXT_CAP_ACS_CR | \
|
2009-12-22 17:21:15 +00:00
|
|
|
PCI_EXT_CAP_ACS_UF)
|
|
|
|
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_TYPE_ROOT_INT_EP 0x9 /* Root Complex Integrated Endpoint */
|
|
|
|
#define PCI_EXP_TYPE_ROOT_EC 0xa /* Root Complex Event Collector */
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static virClassPtr virPCIDeviceListClass;
|
|
|
|
|
|
|
|
static void virPCIDeviceListDispose(void *obj);
|
|
|
|
|
|
|
|
static int virPCIOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virPCIDeviceList, virClassForObjectLockable()))
|
2013-01-16 11:49:54 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virPCI);
|
2013-01-16 11:49:54 +00:00
|
|
|
|
2013-07-01 03:51:00 +00:00
|
|
|
|
2015-06-30 19:53:53 +00:00
|
|
|
static char *
|
|
|
|
virPCIDriverDir(const char *driver)
|
2013-07-01 03:51:00 +00:00
|
|
|
{
|
2015-06-30 19:53:53 +00:00
|
|
|
char *buffer;
|
2013-07-01 03:51:00 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
buffer = g_strdup_printf(PCI_SYSFS "drivers/%s", driver);
|
2015-06-30 19:53:53 +00:00
|
|
|
return buffer;
|
2013-07-01 03:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
static char *
|
|
|
|
virPCIFile(const char *device, const char *file)
|
2013-07-01 03:51:00 +00:00
|
|
|
{
|
2015-06-30 19:23:31 +00:00
|
|
|
char *buffer;
|
2013-07-01 03:51:00 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
buffer = g_strdup_printf(PCI_SYSFS "devices/%s/%s", device, file);
|
2015-06-30 19:23:31 +00:00
|
|
|
return buffer;
|
2013-07-01 03:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* virPCIDeviceGetDriverPathAndName - put the path to the driver
|
|
|
|
* directory of the driver in use for this device in @path and the
|
|
|
|
* name of the driver in @name. Both could be NULL if it's not bound
|
|
|
|
* to any driver.
|
|
|
|
*
|
|
|
|
* Return 0 for success, -1 for error.
|
|
|
|
*/
|
2014-01-16 11:27:23 +00:00
|
|
|
int
|
2013-07-01 03:51:00 +00:00
|
|
|
virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvlink = NULL;
|
2013-07-01 03:51:00 +00:00
|
|
|
|
|
|
|
*path = *name = NULL;
|
|
|
|
/* drvlink = "/sys/bus/pci/dddd:bb:ss.ff/driver" */
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(drvlink = virPCIFile(dev->name, "driver")))
|
2013-07-01 03:51:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-01-15 10:44:53 +00:00
|
|
|
if (!virFileExists(drvlink)) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-07-01 03:51:00 +00:00
|
|
|
if (virFileIsLink(drvlink) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s driver file %s is not a symlink"),
|
|
|
|
dev->name, drvlink);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virFileResolveLink(drvlink, path) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s driver symlink %s"),
|
|
|
|
dev->name, drvlink);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* path = "/sys/bus/pci/drivers/${drivername}" */
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
*name = g_path_get_basename(*path);
|
2013-07-01 03:51:00 +00:00
|
|
|
/* name = "${drivername}" */
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-07-01 03:51:00 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
VIR_FREE(*path);
|
|
|
|
VIR_FREE(*name);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2019-08-13 13:11:14 +00:00
|
|
|
virPCIDeviceConfigOpenInternal(virPCIDevicePtr dev, bool readonly, bool fatal)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2019-08-13 13:11:14 +00:00
|
|
|
fd = open(dev->path, readonly ? O_RDONLY : O_RDWR);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
if (fd < 0) {
|
2012-12-04 21:50:58 +00:00
|
|
|
if (fatal) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to open config space file '%s'"),
|
|
|
|
dev->path);
|
|
|
|
} else {
|
|
|
|
VIR_WARN("Failed to open config space file '%s': %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
dev->path, g_strerror(errno));
|
2012-12-04 21:50:58 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: opened %s", dev->id, dev->name, dev->path);
|
2012-12-04 21:50:58 +00:00
|
|
|
return fd;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:58:25 +00:00
|
|
|
static int
|
2019-08-13 13:17:44 +00:00
|
|
|
virPCIDeviceConfigOpen(virPCIDevicePtr dev)
|
2019-08-13 12:58:25 +00:00
|
|
|
{
|
2019-08-13 13:17:44 +00:00
|
|
|
return virPCIDeviceConfigOpenInternal(dev, true, true);
|
2019-08-13 12:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
static int
|
|
|
|
virPCIDeviceConfigOpenTry(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
return virPCIDeviceConfigOpenInternal(dev, true, false);
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:07:53 +00:00
|
|
|
static int
|
|
|
|
virPCIDeviceConfigOpenWrite(virPCIDevicePtr dev)
|
|
|
|
{
|
2019-08-13 13:11:14 +00:00
|
|
|
return virPCIDeviceConfigOpenInternal(dev, false, true);
|
2019-08-13 13:07:53 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
static void
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(virPCIDevicePtr dev, int cfgfd)
|
2010-07-23 19:03:29 +00:00
|
|
|
{
|
2012-12-04 21:50:58 +00:00
|
|
|
if (VIR_CLOSE(cfgfd) < 0) {
|
|
|
|
VIR_WARN("Failed to close config space file '%s': %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
dev->path, g_strerror(errno));
|
2012-12-04 21:50:58 +00:00
|
|
|
}
|
2010-07-23 19:03:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(virPCIDevicePtr dev,
|
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pos,
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t *buf,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int buflen)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
memset(buf, 0, buflen);
|
|
|
|
|
2012-12-04 21:50:58 +00:00
|
|
|
if (lseek(cfgfd, pos, SEEK_SET) != pos ||
|
|
|
|
saferead(cfgfd, buf, buflen) != buflen) {
|
2010-05-19 10:00:18 +00:00
|
|
|
VIR_WARN("Failed to read from '%s' : %s", dev->path,
|
2020-02-26 17:57:34 +00:00
|
|
|
g_strerror(errno));
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceRead8(virPCIDevicePtr dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf, sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceRead16(virPCIDevicePtr dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return (buf[0] << 0) | (buf[1] << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceRead32(virPCIDevicePtr dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[4];
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return (buf[0] << 0) | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:07:27 +00:00
|
|
|
static int
|
|
|
|
virPCIDeviceReadClass(virPCIDevicePtr dev, uint16_t *device_class)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
|
|
|
g_autofree char *id_str = NULL;
|
2013-12-24 18:07:27 +00:00
|
|
|
unsigned int value;
|
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(path = virPCIFile(dev->name, "class")))
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-12-24 18:07:27 +00:00
|
|
|
|
|
|
|
/* class string is '0xNNNNNN\n' ... i.e. 9 bytes */
|
|
|
|
if (virFileReadAll(path, 9, &id_str) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-12-24 18:07:27 +00:00
|
|
|
|
|
|
|
id_str[8] = '\0';
|
|
|
|
if (virStrToLong_ui(id_str, NULL, 16, &value) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unusual value in %s/devices/%s/class: %s"),
|
|
|
|
PCI_SYSFS, dev->name, id_str);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-12-24 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*device_class = (value >> 8) & 0xFFFF;
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2013-12-24 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite(virPCIDevicePtr dev,
|
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pos,
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t *buf,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int buflen)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2012-12-04 21:50:58 +00:00
|
|
|
if (lseek(cfgfd, pos, SEEK_SET) != pos ||
|
|
|
|
safewrite(cfgfd, buf, buflen) != buflen) {
|
2010-05-19 10:00:18 +00:00
|
|
|
VIR_WARN("Failed to write to '%s' : %s", dev->path,
|
2020-02-26 17:57:34 +00:00
|
|
|
g_strerror(errno));
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceWrite16(virPCIDevicePtr dev, int cfgfd, unsigned int pos, uint16_t val)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[2] = { (val >> 0), (val >> 8) };
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceWrite32(virPCIDevicePtr dev, int cfgfd, unsigned int pos, uint32_t val)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2012-12-05 10:58:39 +00:00
|
|
|
uint8_t buf[4] = { (val >> 0), (val >> 8), (val >> 16), (val >> 24) };
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 23:00:32 +00:00
|
|
|
typedef int (*virPCIDeviceIterPredicate)(virPCIDevicePtr, virPCIDevicePtr,
|
|
|
|
void *);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Iterate over available PCI devices calling @predicate
|
|
|
|
* to compare each one to @dev.
|
|
|
|
* Return -1 on error since we don't want to assume it is
|
|
|
|
* safe to reset if there is an error.
|
|
|
|
*/
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceIterDevices(virPCIDeviceIterPredicate predicate,
|
|
|
|
virPCIDevicePtr dev,
|
|
|
|
virPCIDevicePtr *matched,
|
|
|
|
void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
2009-03-03 11:25:35 +00:00
|
|
|
int ret = 0;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int rc;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
*matched = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: iterating over " PCI_SYSFS "devices", dev->id, dev->name);
|
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, PCI_SYSFS "devices") < 0)
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((ret = virDirRead(dir, &entry, PCI_SYSFS "devices")) > 0) {
|
2010-03-30 15:31:19 +00:00
|
|
|
unsigned int domain, bus, slot, function;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) check = NULL;
|
2010-03-30 15:31:19 +00:00
|
|
|
char *tmp;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-03-30 15:31:19 +00:00
|
|
|
/* expected format: <domain>:<bus>:<slot>.<function> */
|
|
|
|
if (/* domain */
|
|
|
|
virStrToLong_ui(entry->d_name, &tmp, 16, &domain) < 0 || *tmp != ':' ||
|
|
|
|
/* bus */
|
|
|
|
virStrToLong_ui(tmp + 1, &tmp, 16, &bus) < 0 || *tmp != ':' ||
|
|
|
|
/* slot */
|
|
|
|
virStrToLong_ui(tmp + 1, &tmp, 16, &slot) < 0 || *tmp != '.' ||
|
|
|
|
/* function */
|
|
|
|
virStrToLong_ui(tmp + 1, NULL, 16, &function) < 0) {
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_WARN("Unusual entry in " PCI_SYSFS "devices: %s", entry->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
check = virPCIDeviceNew(domain, bus, slot, function);
|
2009-08-17 14:05:23 +00:00
|
|
|
if (!check) {
|
2009-03-03 11:25:35 +00:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
rc = predicate(dev, check, data);
|
|
|
|
if (rc < 0) {
|
|
|
|
/* the predicate returned an error, bail */
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2014-09-03 19:39:21 +00:00
|
|
|
} else if (rc == 1) {
|
2009-08-17 14:05:23 +00:00
|
|
|
VIR_DEBUG("%s %s: iter matched on %s", dev->id, dev->name, check->name);
|
2019-10-16 11:43:52 +00:00
|
|
|
*matched = g_steal_pointer(&check);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
ret = 1;
|
2009-03-02 16:18:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2009-03-03 11:25:35 +00:00
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceFindCapabilityOffset(virPCIDevicePtr dev,
|
|
|
|
int cfgfd,
|
|
|
|
unsigned int capability)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint16_t status;
|
|
|
|
uint8_t pos;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
status = virPCIDeviceRead16(dev, cfgfd, PCI_STATUS);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (!(status & PCI_STATUS_CAP_LIST))
|
|
|
|
return 0;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceRead8(dev, cfgfd, PCI_CAPABILITY_LIST);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Zero indicates last capability, capabilities can't
|
|
|
|
* be in the config space header and 0xff is returned
|
|
|
|
* by the kernel if we don't have access to this region
|
|
|
|
*
|
|
|
|
* Note: we're not handling loops or extended
|
|
|
|
* capabilities here.
|
|
|
|
*/
|
|
|
|
while (pos >= PCI_CONF_HEADER_LEN && pos != 0xff) {
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t capid = virPCIDeviceRead8(dev, cfgfd, pos);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (capid == capability) {
|
|
|
|
VIR_DEBUG("%s %s: found cap 0x%.2x at 0x%.2x",
|
|
|
|
dev->id, dev->name, capability, pos);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceRead8(dev, cfgfd, pos + 1);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: failed to find cap 0x%.2x", dev->id, dev->name, capability);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
static unsigned int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFindExtendedCapabilityOffset(virPCIDevicePtr dev,
|
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int capability)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
int ttl;
|
|
|
|
unsigned int pos;
|
|
|
|
uint32_t header;
|
|
|
|
|
|
|
|
/* minimum 8 bytes per capability */
|
|
|
|
ttl = (PCI_EXT_CAP_LIMIT - PCI_EXT_CAP_BASE) / 8;
|
|
|
|
pos = PCI_EXT_CAP_BASE;
|
|
|
|
|
|
|
|
while (ttl > 0 && pos >= PCI_EXT_CAP_BASE) {
|
2013-01-14 22:11:44 +00:00
|
|
|
header = virPCIDeviceRead32(dev, cfgfd, pos);
|
2009-12-22 17:21:15 +00:00
|
|
|
|
|
|
|
if ((header & PCI_EXT_CAP_ID_MASK) == capability)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
pos = (header >> PCI_EXT_CAP_OFFSET_SHIFT) & PCI_EXT_CAP_OFFSET_MASK;
|
|
|
|
ttl--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
/* detects whether this device has FLR. Returns 0 if the device does
|
|
|
|
* not have FLR, 1 if it does, and -1 on error
|
|
|
|
*/
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2009-07-31 14:35:53 +00:00
|
|
|
uint32_t caps;
|
2009-03-02 16:18:11 +00:00
|
|
|
uint8_t pos;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2010-07-23 19:03:29 +00:00
|
|
|
int found;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* The PCIe Function Level Reset capability allows
|
|
|
|
* individual device functions to be reset without
|
|
|
|
* affecting any other functions on the device or
|
|
|
|
* any other devices on the bus. This is only common
|
|
|
|
* on SR-IOV NICs at the moment.
|
|
|
|
*/
|
|
|
|
if (dev->pcie_cap_pos) {
|
2013-01-14 22:11:44 +00:00
|
|
|
caps = virPCIDeviceRead32(dev, cfgfd, dev->pcie_cap_pos + PCI_EXP_DEVCAP);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (caps & PCI_EXP_DEVCAP_FLR) {
|
|
|
|
VIR_DEBUG("%s %s: detected PCIe FLR capability", dev->id, dev->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The PCI AF Function Level Reset capability is
|
|
|
|
* the same thing, except for conventional PCI
|
|
|
|
* devices. This is not common yet.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_AF);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (pos) {
|
2013-01-14 22:11:44 +00:00
|
|
|
caps = virPCIDeviceRead16(dev, cfgfd, pos + PCI_AF_CAP);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (caps & PCI_AF_CAP_FLR) {
|
|
|
|
VIR_DEBUG("%s %s: detected PCI FLR capability", dev->id, dev->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
/* there are some buggy devices that do support FLR, but forget to
|
|
|
|
* advertise that fact in their capabilities. However, FLR is *required*
|
|
|
|
* to be present for virtual functions (VFs), so if we see that this
|
|
|
|
* device is a VF, we just assume FLR works
|
|
|
|
*/
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf(PCI_SYSFS "devices/%s/physfn", dev->name);
|
2010-07-23 19:03:29 +00:00
|
|
|
|
|
|
|
found = virFileExists(path);
|
|
|
|
if (found) {
|
|
|
|
VIR_DEBUG("%s %s: buggy device didn't advertise FLR, but is a VF; forcing flr on",
|
|
|
|
dev->id, dev->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: no FLR capability found", dev->id, dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Require the device has the PCI Power Management capability
|
|
|
|
* and that a D3hot->D0 transition will results in a full
|
|
|
|
* internal reset, not just a soft reset.
|
|
|
|
*/
|
2013-04-15 10:29:23 +00:00
|
|
|
static unsigned int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceDetectPowerManagementReset(virPCIDevicePtr dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
if (dev->pci_pm_cap_pos) {
|
|
|
|
uint32_t ctl;
|
|
|
|
|
|
|
|
/* require the NO_SOFT_RESET bit is clear */
|
2013-01-14 22:11:44 +00:00
|
|
|
ctl = virPCIDeviceRead32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (!(ctl & PCI_PM_CTRL_NO_SOFT_RESET)) {
|
|
|
|
VIR_DEBUG("%s %s: detected PM reset capability", dev->id, dev->name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: no PM reset capability found", dev->id, dev->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-26 16:43:04 +00:00
|
|
|
/* Any active devices on the same domain/bus ? */
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSharesBusWithActive(virPCIDevicePtr dev, virPCIDevicePtr check, void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceList *inactiveDevs = data;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-07-23 09:25:24 +00:00
|
|
|
/* Different domain, different bus, or simply identical device */
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.domain != check->address.domain ||
|
|
|
|
dev->address.bus != check->address.bus ||
|
|
|
|
(dev->address.slot == check->address.slot &&
|
|
|
|
dev->address.function == check->address.function))
|
2009-08-17 14:05:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-07-26 16:43:04 +00:00
|
|
|
/* same bus, but inactive, i.e. about to be assigned to guest */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (inactiveDevs && virPCIDeviceListFind(inactiveDevs, check))
|
2009-08-17 14:05:22 +00:00
|
|
|
return 0;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2009-08-17 14:05:22 +00:00
|
|
|
return 1;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
static virPCIDevicePtr
|
|
|
|
virPCIDeviceBusContainsActiveDevices(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr active = NULL;
|
|
|
|
if (virPCIDeviceIterDevices(virPCIDeviceSharesBusWithActive,
|
|
|
|
dev, &active, inactiveDevs) < 0)
|
2009-08-17 14:05:23 +00:00
|
|
|
return NULL;
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is @check the parent of @dev ? */
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceIsParent(virPCIDevicePtr dev, virPCIDevicePtr check, void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint16_t device_class;
|
|
|
|
uint8_t header_type, secondary, subordinate;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr *best = data;
|
2012-12-04 21:50:58 +00:00
|
|
|
int ret = 0;
|
|
|
|
int fd;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.domain != check->address.domain)
|
2009-03-02 16:18:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpenTry(check)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* Is it a bridge? */
|
2013-12-24 18:07:27 +00:00
|
|
|
ret = virPCIDeviceReadClass(check, &device_class);
|
|
|
|
if (ret < 0 || device_class != PCI_CLASS_BRIDGE_PCI)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Is it a plane? */
|
2013-01-14 22:11:44 +00:00
|
|
|
header_type = virPCIDeviceRead8(check, fd, PCI_HEADER_TYPE);
|
2009-03-02 16:18:11 +00:00
|
|
|
if ((header_type & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_BRIDGE)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
secondary = virPCIDeviceRead8(check, fd, PCI_SECONDARY_BUS);
|
|
|
|
subordinate = virPCIDeviceRead8(check, fd, PCI_SUBORDINATE_BUS);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-01-19 13:17:20 +00:00
|
|
|
VIR_DEBUG("%s %s: found parent device %s", dev->id, dev->name, check->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
/* if the secondary bus exactly equals the device's bus, then we found
|
|
|
|
* the direct parent. No further work is necessary
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus == secondary) {
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
|
2013-09-10 18:10:55 +00:00
|
|
|
/* otherwise, SRIOV allows VFs to be on different buses than their PFs.
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
* In this case, what we need to do is look for the "best" match; i.e.
|
|
|
|
* the most restrictive match that still satisfies all of the conditions.
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus > secondary && dev->address.bus <= subordinate) {
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (*best == NULL) {
|
2015-12-15 08:44:35 +00:00
|
|
|
*best = virPCIDeviceNew(check->address.domain,
|
|
|
|
check->address.bus,
|
|
|
|
check->address.slot,
|
|
|
|
check->address.function);
|
2012-12-04 21:50:58 +00:00
|
|
|
if (*best == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
/* OK, we had already recorded a previous "best" match for the
|
|
|
|
* parent. See if the current device is more restrictive than the
|
|
|
|
* best, and if so, make it the new best
|
|
|
|
*/
|
2012-12-04 21:50:58 +00:00
|
|
|
int bestfd;
|
|
|
|
uint8_t best_secondary;
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
if ((bestfd = virPCIDeviceConfigOpenTry(*best)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2013-01-14 22:11:44 +00:00
|
|
|
best_secondary = virPCIDeviceRead8(*best, bestfd, PCI_SECONDARY_BUS);
|
|
|
|
virPCIDeviceConfigClose(*best, bestfd);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
|
|
|
if (secondary > best_secondary) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(*best);
|
2015-12-15 08:44:35 +00:00
|
|
|
*best = virPCIDeviceNew(check->address.domain,
|
|
|
|
check->address.bus,
|
|
|
|
check->address.slot,
|
|
|
|
check->address.function);
|
2012-12-04 21:50:58 +00:00
|
|
|
if (*best == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(check, fd);
|
2012-12-04 21:50:58 +00:00
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetParent(virPCIDevicePtr dev, virPCIDevicePtr *parent)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr best = NULL;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
*parent = NULL;
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceIterDevices(virPCIDeviceIsParent, dev, parent, &best);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (ret == 1)
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(best);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
else if (ret == 0)
|
|
|
|
*parent = best;
|
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Secondary Bus Reset is our sledgehammer - it resets all
|
|
|
|
* devices behind a bus.
|
|
|
|
*/
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceTrySecondaryBusReset(virPCIDevicePtr dev,
|
|
|
|
int cfgfd,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) parent = NULL;
|
|
|
|
g_autoptr(virPCIDevice) conflict = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
uint8_t config_space[PCI_CONF_LEN];
|
|
|
|
uint16_t ctl;
|
|
|
|
int ret = -1;
|
2012-12-04 21:50:58 +00:00
|
|
|
int parentfd;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2012-08-31 13:44:21 +00:00
|
|
|
/* Refuse to do a secondary bus reset if there are other
|
|
|
|
* devices/functions behind the bus are used by the host
|
|
|
|
* or other guests.
|
2009-03-02 16:18:11 +00:00
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((conflict = virPCIDeviceBusContainsActiveDevices(dev, inactiveDevs))) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Active %s devices on bus with %s, not doing bus reset"),
|
|
|
|
conflict->name, dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the parent bus */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceGetParent(dev, &parent) < 0)
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
return -1;
|
2009-03-02 16:18:11 +00:00
|
|
|
if (!parent) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to find parent device for %s"),
|
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-08-13 13:07:53 +00:00
|
|
|
if ((parentfd = virPCIDeviceConfigOpenWrite(parent)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto out;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: doing a secondary bus reset", dev->id, dev->name);
|
|
|
|
|
|
|
|
/* Save and restore the device's config space; we only do this
|
|
|
|
* for the supplied device since we refuse to do a reset if there
|
|
|
|
* are multiple devices/functions
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceRead(dev, cfgfd, 0, config_space, PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-02-19 15:04:35 +00:00
|
|
|
_("Failed to read PCI config space for %s"),
|
2009-08-14 07:31:11 +00:00
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the control register, set the reset flag, wait 200ms,
|
|
|
|
* unset the reset flag and wait 200ms.
|
|
|
|
*/
|
2019-08-15 09:44:06 +00:00
|
|
|
ctl = virPCIDeviceRead16(dev, parentfd, PCI_BRIDGE_CONTROL);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite16(parent, parentfd, PCI_BRIDGE_CONTROL,
|
|
|
|
ctl | PCI_BRIDGE_CTL_RESET);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(200 * 1000); /* sleep 200ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite16(parent, parentfd, PCI_BRIDGE_CONTROL, ctl);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(200 * 1000); /* sleep 200ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceWrite(dev, cfgfd, 0, config_space, PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to restore PCI config space for %s"),
|
|
|
|
dev->name);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
ret = 0;
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
out:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(parent, parentfd);
|
2009-03-02 16:18:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Power management reset attempts to reset a device using a
|
|
|
|
* D-state transition from D3hot to D0. Note, in detect_pm_reset()
|
|
|
|
* above we require the device supports a full internal reset.
|
|
|
|
*/
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceTryPowerManagementReset(virPCIDevicePtr dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t config_space[PCI_CONF_LEN];
|
|
|
|
uint32_t ctl;
|
|
|
|
|
|
|
|
if (!dev->pci_pm_cap_pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Save and restore the device's config space. */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceRead(dev, cfgfd, 0, &config_space[0], PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-02-19 15:04:35 +00:00
|
|
|
_("Failed to read PCI config space for %s"),
|
2009-08-14 07:31:11 +00:00
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: doing a power management reset", dev->id, dev->name);
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ctl = virPCIDeviceRead32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL);
|
2009-03-02 16:18:11 +00:00
|
|
|
ctl &= ~PCI_PM_CTRL_STATE_MASK;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL,
|
|
|
|
ctl | PCI_PM_CTRL_STATE_D3hot);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(10 * 1000); /* sleep 10ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL,
|
|
|
|
ctl | PCI_PM_CTRL_STATE_D0);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(10 * 1000); /* sleep 10ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceWrite(dev, cfgfd, 0, &config_space[0], PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to restore PCI config space for %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceInit(virPCIDevicePtr dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2010-07-23 19:03:29 +00:00
|
|
|
int flr;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
dev->pcie_cap_pos = virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_EXP);
|
|
|
|
dev->pci_pm_cap_pos = virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_PM);
|
|
|
|
flr = virPCIDeviceDetectFunctionLevelReset(dev, cfgfd);
|
2010-07-28 18:07:08 +00:00
|
|
|
if (flr < 0)
|
2010-07-23 19:03:29 +00:00
|
|
|
return flr;
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->has_flr = !!flr;
|
|
|
|
dev->has_pm_reset = !!virPCIDeviceDetectPowerManagementReset(dev, cfgfd);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceReset(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceList *activeDevs,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvPath = NULL;
|
|
|
|
g_autofree char *drvName = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
int ret = -1;
|
2013-06-29 02:35:21 +00:00
|
|
|
int fd = -1;
|
2017-01-23 13:37:10 +00:00
|
|
|
int hdrType = -1;
|
|
|
|
|
|
|
|
if (virPCIGetHeaderType(dev, &hdrType) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (hdrType != VIR_PCI_HEADER_ENDPOINT) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid attempt to reset PCI device %s. "
|
|
|
|
"Only PCI endpoint devices can be reset"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, dev)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Not resetting active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-29 02:35:21 +00:00
|
|
|
/* If the device is currently bound to vfio-pci, ignore all
|
|
|
|
* requests to reset it, since the vfio-pci driver will always
|
|
|
|
* reset it whenever appropriate, so doing it ourselves would just
|
|
|
|
* be redundant.
|
|
|
|
*/
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(dev, &drvPath, &drvName) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
if (virPCIStubDriverTypeFromString(drvName) == VIR_PCI_STUB_DRIVER_VFIO) {
|
2013-06-29 02:35:21 +00:00
|
|
|
VIR_DEBUG("Device %s is bound to vfio-pci - skip reset",
|
|
|
|
dev->name);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Resetting device %s", dev->name);
|
|
|
|
|
2019-08-13 13:07:53 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpenWrite(dev)) < 0)
|
2013-06-29 02:35:21 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* KVM will perform FLR when starting and stopping
|
|
|
|
* a guest, so there is no need for us to do it here.
|
|
|
|
*/
|
2012-12-04 21:50:58 +00:00
|
|
|
if (dev->has_flr) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-08-14 07:31:11 +00:00
|
|
|
/* If the device supports PCI power management reset,
|
|
|
|
* that's the next best thing because it only resets
|
|
|
|
* the function, not the whole device.
|
|
|
|
*/
|
|
|
|
if (dev->has_pm_reset)
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceTryPowerManagementReset(dev, fd);
|
2009-08-14 07:31:11 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* Bus reset is not an option with the root bus */
|
2015-12-15 08:44:35 +00:00
|
|
|
if (ret < 0 && dev->address.bus != 0)
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceTrySecondaryBusReset(dev, fd, inactiveDevs);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-08-14 07:31:11 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Unable to reset PCI device %s: %s"),
|
|
|
|
dev->name,
|
2013-06-29 02:35:21 +00:00
|
|
|
err ? err->message :
|
|
|
|
_("no FLR, PM reset or bus reset available"));
|
2009-08-14 07:31:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
2009-03-02 16:18:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
static int
|
2015-10-23 09:54:07 +00:00
|
|
|
virPCIProbeStubDriver(virPCIStubDriver driver)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2015-10-23 09:54:07 +00:00
|
|
|
const char *drvname = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvpath = NULL;
|
2013-05-24 10:14:02 +00:00
|
|
|
bool probed = false;
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
if (driver == VIR_PCI_STUB_DRIVER_NONE ||
|
|
|
|
!(drvname = virPCIStubDriverTypeToString(driver))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s",
|
|
|
|
_("Attempting to use unknown stub driver"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
recheck:
|
2018-07-24 15:52:21 +00:00
|
|
|
if ((drvpath = virPCIDriverDir(drvname)) && virFileExists(drvpath))
|
2013-01-10 07:51:43 +00:00
|
|
|
/* driver already loaded, return */
|
|
|
|
return 0;
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
if (!probed) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *errbuf = NULL;
|
2013-05-24 10:14:02 +00:00
|
|
|
probed = true;
|
2020-06-16 11:07:30 +00:00
|
|
|
if ((errbuf = virKModLoad(drvname))) {
|
2015-10-23 09:54:07 +00:00
|
|
|
VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
|
2014-01-24 15:47:20 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2020-06-16 12:07:05 +00:00
|
|
|
VIR_FREE(drvpath);
|
2009-04-03 12:38:52 +00:00
|
|
|
goto recheck;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2020-06-16 11:12:09 +00:00
|
|
|
/* If we know failure was because of admin config, let's report that;
|
2014-01-24 15:47:20 +00:00
|
|
|
* otherwise, report a more generic failure message
|
|
|
|
*/
|
2020-06-16 11:12:09 +00:00
|
|
|
if (virKModIsProhibited(drvname)) {
|
2014-01-24 15:47:20 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to load PCI stub module %s: "
|
|
|
|
"administratively prohibited"),
|
2015-10-23 09:54:07 +00:00
|
|
|
drvname);
|
2014-01-24 15:47:20 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to load PCI stub module %s"),
|
2015-10-23 09:54:07 +00:00
|
|
|
drvname);
|
2014-01-24 15:47:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
return -1;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 11:27:23 +00:00
|
|
|
int
|
2015-10-29 15:09:54 +00:00
|
|
|
virPCIDeviceUnbind(virPCIDevicePtr dev)
|
2014-01-16 11:27:23 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
|
|
|
g_autofree char *drvpath = NULL;
|
|
|
|
g_autofree char *driver = NULL;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(dev, &drvpath, &driver) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
if (!driver)
|
2014-01-16 11:27:23 +00:00
|
|
|
/* The device is not bound to any driver */
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(path = virPCIFile(dev->name, "driver/unbind")))
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
|
|
|
if (virFileExists(path)) {
|
|
|
|
if (virFileWriteStr(path, dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to unbind PCI device '%s' from %s"),
|
|
|
|
dev->name, driver);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2014-01-16 11:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2014-01-16 11:27:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:58:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virPCIDeviceRebind:
|
|
|
|
* @dev: virPCIDevice object describing the device to rebind
|
|
|
|
*
|
|
|
|
* unbind a device from its driver, then immediately rebind it.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure
|
|
|
|
*/
|
|
|
|
int virPCIDeviceRebind(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
if (virPCIDeviceUnbind(dev) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to trigger a probe for PCI device '%s'"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 03:36:45 +00:00
|
|
|
/*
|
|
|
|
* Bind a PCI device to a driver using driver_override sysfs interface.
|
|
|
|
* E.g.
|
|
|
|
*
|
|
|
|
* echo driver-name > /sys/bus/pci/devices/0000:03:00.0/driver_override
|
|
|
|
* echo 0000:03:00.0 > /sys/bus/pci/devices/0000:03:00.0/driver/unbind
|
|
|
|
* echo 0000:03:00.0 > /sys/bus/pci/drivers_probe
|
|
|
|
*
|
|
|
|
* An empty driverName will cause the device to be bound to its
|
|
|
|
* preferred driver.
|
|
|
|
*/
|
2011-04-06 07:13:10 +00:00
|
|
|
static int
|
2016-08-02 03:36:45 +00:00
|
|
|
virPCIDeviceBindWithDriverOverride(virPCIDevicePtr dev,
|
|
|
|
const char *driverName)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
if (!(path = virPCIFile(dev->name, "driver_override")))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virFileWriteStr(path, driverName, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to add driver '%s' to driver_override "
|
|
|
|
" interface of PCI device '%s'"),
|
|
|
|
driverName, dev->name);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:58:15 +00:00
|
|
|
if (virPCIDeviceRebind(dev) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-08-23 09:45:01 +00:00
|
|
|
virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
|
2016-08-02 03:36:45 +00:00
|
|
|
{
|
|
|
|
if (!dev->unbind_from_stub) {
|
|
|
|
VIR_DEBUG("Unbind from stub skipped for PCI device %s", dev->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virPCIDeviceBindWithDriverOverride(dev, "\n");
|
|
|
|
}
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
static int
|
2019-08-23 09:45:01 +00:00
|
|
|
virPCIDeviceBindToStub(virPCIDevicePtr dev)
|
2016-08-02 03:36:45 +00:00
|
|
|
{
|
|
|
|
const char *stubDriverName;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *stubDriverPath = NULL;
|
|
|
|
g_autofree char *driverLink = NULL;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
/* Check the device is configured to use one of the known stub drivers */
|
|
|
|
if (dev->stubDriver == VIR_PCI_STUB_DRIVER_NONE) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("No stub driver configured for PCI device %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
} else if (!(stubDriverName = virPCIStubDriverTypeToString(dev->stubDriver))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown stub driver configured for PCI device %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(stubDriverPath = virPCIDriverDir(stubDriverName)) ||
|
|
|
|
!(driverLink = virPCIFile(dev->name, "driver")))
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
if (virFileExists(driverLink)) {
|
|
|
|
if (virFileLinkPointsTo(driverLink, stubDriverPath)) {
|
|
|
|
/* The device is already bound to the correct driver */
|
|
|
|
VIR_DEBUG("Device %s is already bound to %s",
|
|
|
|
dev->name, stubDriverName);
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virPCIDeviceBindWithDriverOverride(dev, stubDriverName) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
dev->unbind_from_stub = true;
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:54:45 +00:00
|
|
|
/* virPCIDeviceDetach:
|
|
|
|
*
|
|
|
|
* Detach this device from the host driver, attach it to the stub
|
|
|
|
* driver (previously set with virPCIDeviceSetStubDriver(), and add *a
|
|
|
|
* copy* of the object to the inactiveDevs list (if provided). This
|
|
|
|
* function will *never* consume dev, so the caller should free it.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure (will fail if the device is
|
|
|
|
* already in the activeDevs list, but will be a NOP if the device is
|
|
|
|
* already bound to the stub).
|
|
|
|
*
|
|
|
|
* GENERAL NOTE: activeDevs should be a list of all PCI devices
|
|
|
|
* currently in use by a domain. inactiveDevs is a list of all PCI
|
|
|
|
* devices that libvirt has detached from the host driver + attached
|
|
|
|
* to the stub driver, but hasn't yet assigned to a domain. Any device
|
|
|
|
* that is still attached to its host driver should not be on either
|
|
|
|
* list.
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceDetach(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceList *activeDevs,
|
2013-05-30 18:14:46 +00:00
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2014-01-24 15:47:20 +00:00
|
|
|
if (virPCIProbeStubDriver(dev->stubDriver) < 0)
|
2009-04-03 12:38:52 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, dev)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-06-14 21:12:35 +00:00
|
|
|
_("Not detaching active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:15:11 +00:00
|
|
|
if (virPCIDeviceBindToStub(dev) < 0)
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-06-04 19:54:45 +00:00
|
|
|
/* Add *a copy of* the dev into list inactiveDevs, if
|
|
|
|
* it's not already there.
|
|
|
|
*/
|
2016-01-07 16:50:15 +00:00
|
|
|
if (inactiveDevs && !virPCIDeviceListFind(inactiveDevs, dev)) {
|
|
|
|
VIR_DEBUG("Adding PCI device %s to inactive list", dev->name);
|
|
|
|
if (virPCIDeviceListAddCopy(inactiveDevs, dev) < 0)
|
|
|
|
return -1;
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
virhostdev: remove virHostdevReattachPCIDevice
virHostdevReattachPCIDevice() is a static that simply does
a wait loop with virPCIDeviceWaitForCleanup() before
calling virPCIDeviceReattach().
This loop traces back to commit d1e5676c0d, aiming to
solve a race condition between Libvirt returning the
device back to the host and QEMU trying to access it in
the meantime, which resulted in QEMU exiting on error
and killing the guest. This happens because device_del
is asynchronous, returning OK even if the guest didn't
release the device. Commit 01abc8a1b8 moved this code
to qemu_hostdev.c, 82e8dd4cf8 added the pci-stub conditional
for the loop, 899b261127 moved the code to virhostdev.c
where it stood until now.
The intent of this wait loop is still valid: device_del
is still not bullet proof into preventing the conditions
that commit d1e5676c0d aimed to fix, especially when considering
all the architectures we must support. However, this loop
is executed only in virHostdevReattachPCIDevice(), leaving
every other virPCIDeviceReattach() call prone to that error.
Let's move the wait loop code to virPCIDeviceReattach(). This
will:
- make every reattach call safe from this race condition
with the pci-stub;
- allow for a bit of code cleanup (virHostdevReattachPCIDevice()
can be erased, and virHostdevReAttachPCIDevices() can use
virPCIDeviceReattach() directly);
- make it easier to understand the overall reattach mechanisms in
Libvirt, without the risk of a newcomer wondering why reattach
is done slightly different in some instances.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-07-23 17:35:40 +00:00
|
|
|
/*
|
|
|
|
* Pre-condition: inactivePCIHostdevs & activePCIHostdevs
|
|
|
|
* are locked
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceReattach(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceListPtr activeDevs,
|
pci: autolearn name of stub driver, remove from arglist
virPCIDeviceReattach and virPCIDeviceUnbindFromStub (called by
virPCIDeviceReattach) had previously required the name of the stub
driver as input. This is unnecessary, because the name of the driver
the device is currently bound to can be found by looking at the link:
/sys/bus/pci/dddd:bb:ss.ff/driver
Instead of requiring that the name of the expected stub driver name
and only unbinding if that one name is matched, we no longer take a
driver name in the arglist for either of these
functions. virPCIDeviceUnbindFromStub just compares the name of the
currently bound driver to a list of "well known" stubs (right now
contains "pci-stub" and "vfio-pci" for qemu, and "pciback" for xen),
and only performs the unbind if it's one of those devices.
This allows virsh nodedevice-reattach to work properly across a
libvirtd restart, and fixes a couple of cases where we were
erroneously still hard-coding "pci-stub" as the drive name.
For some unknown reason, virPCIDeviceReattach had been calling
modprobe on the stub driver prior to unbinding the device. This was
problematic because we no longer know the name of the stub driver in
that function. However, it is pointless to probe for the stub driver
at that time anyway - because the device is bound to the stub driver,
we are guaranteed that it is already loaded, and so that call to
modprobe has been removed.
2013-05-01 18:44:10 +00:00
|
|
|
virPCIDeviceListPtr inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, dev)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-06-14 21:12:35 +00:00
|
|
|
_("Not reattaching active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
pci: autolearn name of stub driver, remove from arglist
virPCIDeviceReattach and virPCIDeviceUnbindFromStub (called by
virPCIDeviceReattach) had previously required the name of the stub
driver as input. This is unnecessary, because the name of the driver
the device is currently bound to can be found by looking at the link:
/sys/bus/pci/dddd:bb:ss.ff/driver
Instead of requiring that the name of the expected stub driver name
and only unbinding if that one name is matched, we no longer take a
driver name in the arglist for either of these
functions. virPCIDeviceUnbindFromStub just compares the name of the
currently bound driver to a list of "well known" stubs (right now
contains "pci-stub" and "vfio-pci" for qemu, and "pciback" for xen),
and only performs the unbind if it's one of those devices.
This allows virsh nodedevice-reattach to work properly across a
libvirtd restart, and fixes a couple of cases where we were
erroneously still hard-coding "pci-stub" as the drive name.
For some unknown reason, virPCIDeviceReattach had been calling
modprobe on the stub driver prior to unbinding the device. This was
problematic because we no longer know the name of the stub driver in
that function. However, it is pointless to probe for the stub driver
at that time anyway - because the device is bound to the stub driver,
we are guaranteed that it is already loaded, and so that call to
modprobe has been removed.
2013-05-01 18:44:10 +00:00
|
|
|
if (virPCIDeviceUnbindFromStub(dev) < 0)
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Steal the dev from list inactiveDevs */
|
2016-01-07 16:50:15 +00:00
|
|
|
if (inactiveDevs) {
|
|
|
|
VIR_DEBUG("Removing PCI device %s from inactive list", dev->name);
|
2013-06-04 20:02:33 +00:00
|
|
|
virPCIDeviceListDel(inactiveDevs, dev);
|
2016-01-07 16:50:15 +00:00
|
|
|
}
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceReadID(virPCIDevicePtr dev, const char *id_name)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
char *id_str;
|
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(path = virPCIFile(dev->name, id_name)))
|
2011-04-03 09:21:15 +00:00
|
|
|
return NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* ID string is '0xNNNN\n' ... i.e. 7 bytes */
|
2018-07-24 15:52:21 +00:00
|
|
|
if (virFileReadAll(path, 7, &id_str) < 0)
|
2009-03-02 16:18:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check for 0x suffix */
|
|
|
|
if (id_str[0] != '0' || id_str[1] != 'x') {
|
|
|
|
VIR_FREE(id_str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Chop off the newline; we know the string is 7 bytes */
|
|
|
|
id_str[6] = '\0';
|
|
|
|
|
|
|
|
return id_str;
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:36:25 +00:00
|
|
|
bool
|
|
|
|
virPCIDeviceAddressIsValid(virPCIDeviceAddressPtr addr,
|
|
|
|
bool report)
|
|
|
|
{
|
|
|
|
if (addr->bus > 0xFF) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address bus='0x%x', "
|
|
|
|
"must be <= 0xFF"),
|
|
|
|
addr->bus);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (addr->slot > 0x1F) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address slot='0x%x', "
|
|
|
|
"must be <= 0x1F"),
|
|
|
|
addr->slot);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (addr->function > 7) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address function=0x%x, "
|
|
|
|
"must be <= 7"),
|
|
|
|
addr->function);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (virPCIDeviceAddressIsEmpty(addr)) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("Invalid PCI address 0000:00:00, at least "
|
|
|
|
"one of domain, bus, or slot must be > 0"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
virPCIDeviceAddressIsEmpty(const virPCIDeviceAddress *addr)
|
|
|
|
{
|
|
|
|
return !(addr->domain || addr->bus || addr->slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-04 08:43:40 +00:00
|
|
|
virPCIDeviceAddressEqual(const virPCIDeviceAddress *addr1,
|
|
|
|
const virPCIDeviceAddress *addr2)
|
2018-09-13 14:36:25 +00:00
|
|
|
{
|
|
|
|
if (addr1->domain == addr2->domain &&
|
|
|
|
addr1->bus == addr2->bus &&
|
|
|
|
addr1->slot == addr2->slot &&
|
|
|
|
addr1->function == addr2->function) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:32:37 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceAddressCopy:
|
|
|
|
* @dst: where to store address
|
|
|
|
* @src: source address to copy
|
|
|
|
*
|
|
|
|
* Creates a deep copy of given @src address and stores it into
|
|
|
|
* @dst which has to be pre-allocated by caller.
|
|
|
|
*/
|
|
|
|
void virPCIDeviceAddressCopy(virPCIDeviceAddressPtr dst,
|
|
|
|
const virPCIDeviceAddress *src)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, sizeof(*src));
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:32:38 +00:00
|
|
|
char *
|
2019-06-06 15:02:11 +00:00
|
|
|
virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
|
2018-09-04 16:32:38 +00:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
str = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT,
|
|
|
|
addr->domain,
|
|
|
|
addr->bus,
|
|
|
|
addr->slot,
|
|
|
|
addr->function);
|
2018-09-04 16:32:38 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceNew(unsigned int domain,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int slot,
|
|
|
|
unsigned int function)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) dev = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *vendor = NULL;
|
|
|
|
g_autofree char *product = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2020-10-05 17:13:12 +00:00
|
|
|
dev = g_new0(virPCIDevice, 1);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
dev->address.domain = domain;
|
|
|
|
dev->address.bus = bus;
|
|
|
|
dev->address.slot = slot;
|
|
|
|
dev->address.function = function;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
dev->name = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, domain, bus, slot,
|
|
|
|
function);
|
2019-07-30 09:37:43 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
dev->path = g_strdup_printf(PCI_SYSFS "devices/%s/config", dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-09-13 13:32:43 +00:00
|
|
|
if (!virFileExists(dev->path)) {
|
2010-04-30 15:44:19 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Device %s not found: could not access %s"),
|
|
|
|
dev->name, dev->path);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2010-04-30 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
vendor = virPCIDeviceReadID(dev, "vendor");
|
|
|
|
product = virPCIDeviceReadID(dev, "device");
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
if (!vendor || !product) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-03-02 16:18:11 +00:00
|
|
|
_("Failed to read product/vendor ID for %s"),
|
|
|
|
dev->name);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* strings contain '0x' prefix */
|
2019-11-13 13:53:42 +00:00
|
|
|
if (g_snprintf(dev->id, sizeof(dev->id), "%s %s", &vendor[2],
|
|
|
|
&product[2]) >= sizeof(dev->id)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-06-22 20:52:32 +00:00
|
|
|
_("dev->id buffer overflow: %s %s"),
|
|
|
|
&vendor[2], &product[2]);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2011-06-22 20:52:32 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
|
|
|
|
|
2019-10-16 11:35:54 +00:00
|
|
|
return g_steal_pointer(&dev);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceCopy(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
virPCIDevicePtr copy;
|
|
|
|
|
2020-10-05 17:13:12 +00:00
|
|
|
copy = g_new0(virPCIDevice, 1);
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
/* shallow copy to take care of most attributes */
|
|
|
|
*copy = *dev;
|
2015-10-23 09:54:07 +00:00
|
|
|
copy->path = NULL;
|
2014-03-01 06:28:56 +00:00
|
|
|
copy->used_by_drvname = copy->used_by_domname = NULL;
|
2019-10-20 11:49:46 +00:00
|
|
|
copy->name = g_strdup(dev->name);
|
|
|
|
copy->path = g_strdup(dev->path);
|
|
|
|
copy->used_by_drvname = g_strdup(dev->used_by_drvname);
|
|
|
|
copy->used_by_domname = g_strdup(dev->used_by_domname);
|
2013-05-31 15:06:32 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
void
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(virPCIDevicePtr dev)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2009-08-17 14:05:23 +00:00
|
|
|
if (!dev)
|
|
|
|
return;
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: freeing", dev->id, dev->name);
|
2019-07-30 09:37:43 +00:00
|
|
|
VIR_FREE(dev->name);
|
2011-06-22 20:52:32 +00:00
|
|
|
VIR_FREE(dev->path);
|
2014-03-01 06:28:56 +00:00
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2015-01-14 11:02:40 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceGetAddress:
|
|
|
|
* @dev: device to get address from
|
|
|
|
*
|
|
|
|
* Take a PCI device on input and return its PCI address. The
|
2015-12-15 08:44:35 +00:00
|
|
|
* returned object is owned by the device and must not be freed.
|
2015-01-14 11:02:40 +00:00
|
|
|
*
|
2015-12-15 08:44:35 +00:00
|
|
|
* Returns: a pointer to the address, which can never be NULL.
|
2015-01-14 11:02:40 +00:00
|
|
|
*/
|
|
|
|
virPCIDeviceAddressPtr
|
|
|
|
virPCIDeviceGetAddress(virPCIDevicePtr dev)
|
|
|
|
{
|
2015-12-15 08:44:35 +00:00
|
|
|
return &(dev->address);
|
2015-01-14 11:02:40 +00:00
|
|
|
}
|
|
|
|
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
const char *
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetName(virPCIDevicePtr dev)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
|
|
|
return dev->name;
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:30:03 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceGetConfigPath:
|
|
|
|
*
|
|
|
|
* Returns a pointer to a string containing the path of @dev's PCI
|
|
|
|
* config file.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
virPCIDeviceGetConfigPath(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->path;
|
|
|
|
}
|
|
|
|
|
2013-04-10 10:09:23 +00:00
|
|
|
void virPCIDeviceSetManaged(virPCIDevicePtr dev, bool managed)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-04-10 10:09:23 +00:00
|
|
|
dev->managed = managed;
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIDeviceGetManaged(virPCIDevicePtr dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
|
|
|
return dev->managed;
|
|
|
|
}
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
void
|
|
|
|
virPCIDeviceSetStubDriver(virPCIDevicePtr dev, virPCIStubDriver driver)
|
2013-04-23 18:50:15 +00:00
|
|
|
{
|
2015-10-23 09:54:07 +00:00
|
|
|
dev->stubDriver = driver;
|
2013-04-23 18:50:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
virPCIStubDriver
|
2013-04-23 18:50:15 +00:00
|
|
|
virPCIDeviceGetStubDriver(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->stubDriver;
|
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetUnbindFromStub(virPCIDevicePtr dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->unbind_from_stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-10 10:44:41 +00:00
|
|
|
virPCIDeviceSetUnbindFromStub(virPCIDevicePtr dev, bool unbind)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = unbind;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetRemoveSlot(virPCIDevicePtr dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->remove_slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-10 10:44:41 +00:00
|
|
|
virPCIDeviceSetRemoveSlot(virPCIDevicePtr dev, bool remove_slot)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->remove_slot = remove_slot;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetReprobe(virPCIDevicePtr dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->reprobe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-10 10:44:41 +00:00
|
|
|
virPCIDeviceSetReprobe(virPCIDevicePtr dev, bool reprobe)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->reprobe = reprobe;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
int
|
|
|
|
virPCIDeviceSetUsedBy(virPCIDevicePtr dev,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
2019-10-20 11:49:46 +00:00
|
|
|
dev->used_by_drvname = g_strdup(drv_name);
|
|
|
|
dev->used_by_domname = g_strdup(dom_name);
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
return 0;
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
void
|
|
|
|
virPCIDeviceGetUsedBy(virPCIDevicePtr dev,
|
|
|
|
const char **drv_name,
|
|
|
|
const char **dom_name)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
*drv_name = dev->used_by_drvname;
|
|
|
|
*dom_name = dev->used_by_domname;
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListPtr
|
|
|
|
virPCIDeviceListNew(void)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListPtr list;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
if (virPCIInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(list = virObjectLockableNew(virPCIDeviceListClass)))
|
2009-08-17 14:05:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static void
|
|
|
|
virPCIDeviceListDispose(void *obj)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-01-16 11:49:54 +00:00
|
|
|
virPCIDeviceListPtr list = obj;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(list->devs[i]);
|
2009-08-17 14:05:23 +00:00
|
|
|
list->devs[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->count = 0;
|
|
|
|
VIR_FREE(list->devs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListAdd(virPCIDeviceListPtr list,
|
|
|
|
virPCIDevicePtr dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceListFind(list, dev)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Device %s is already in use"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-07-11 03:06:43 +00:00
|
|
|
return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 01:27:52 +00:00
|
|
|
|
|
|
|
/* virPCIDeviceListAddCopy - add a *copy* of the device to this list */
|
|
|
|
int
|
|
|
|
virPCIDeviceListAddCopy(virPCIDeviceListPtr list, virPCIDevicePtr dev)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) copy = virPCIDeviceCopy(dev);
|
2013-06-25 01:27:52 +00:00
|
|
|
|
|
|
|
if (!copy)
|
|
|
|
return -1;
|
2018-07-24 15:52:22 +00:00
|
|
|
if (virPCIDeviceListAdd(list, copy) < 0)
|
2013-06-25 01:27:52 +00:00
|
|
|
return -1;
|
2018-07-24 15:52:22 +00:00
|
|
|
|
|
|
|
copy = NULL;
|
2013-06-25 01:27:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListGet(virPCIDeviceListPtr list,
|
|
|
|
int idx)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
|
|
|
if (idx >= list->count)
|
|
|
|
return NULL;
|
|
|
|
if (idx < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:44:27 +00:00
|
|
|
size_t
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListCount(virPCIDeviceListPtr list)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2009-10-27 17:30:16 +00:00
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListStealIndex(virPCIDeviceListPtr list,
|
|
|
|
int idx)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr ret;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
if (idx < 0 || idx >= list->count)
|
|
|
|
return NULL;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
ret = list->devs[idx];
|
2013-07-05 18:46:35 +00:00
|
|
|
VIR_DELETE_ELEMENT(list->devs, idx, list->count);
|
2009-10-27 17:30:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListSteal(virPCIDeviceListPtr list,
|
|
|
|
virPCIDevicePtr dev)
|
2012-12-04 07:30:46 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
return virPCIDeviceListStealIndex(list, virPCIDeviceListFindIndex(list, dev));
|
2012-12-04 07:30:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 17:30:16 +00:00
|
|
|
void
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListDel(virPCIDeviceListPtr list,
|
|
|
|
virPCIDevicePtr dev)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
2018-07-27 16:07:38 +00:00
|
|
|
virPCIDeviceFree(virPCIDeviceListSteal(list, dev));
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListFindIndex(virPCIDeviceListPtr list, virPCIDevicePtr dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
virPCIDevicePtr other = list->devs[i];
|
|
|
|
if (other->address.domain == dev->address.domain &&
|
|
|
|
other->address.bus == dev->address.bus &&
|
|
|
|
other->address.slot == dev->address.slot &&
|
|
|
|
other->address.function == dev->address.function)
|
2012-12-04 07:30:46 +00:00
|
|
|
return i;
|
2015-12-15 08:44:35 +00:00
|
|
|
}
|
2012-12-04 07:30:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListFindByIDs(virPCIDeviceListPtr list,
|
|
|
|
unsigned int domain,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int slot,
|
|
|
|
unsigned int function)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
2015-12-15 08:44:35 +00:00
|
|
|
virPCIDevicePtr other = list->devs[i];
|
|
|
|
if (other->address.domain == domain &&
|
|
|
|
other->address.bus == bus &&
|
|
|
|
other->address.slot == slot &&
|
|
|
|
other->address.function == function)
|
2013-05-31 15:06:32 +00:00
|
|
|
return list->devs[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListFind(virPCIDeviceListPtr list, virPCIDevicePtr dev)
|
2012-12-04 07:30:46 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int idx;
|
2012-12-04 07:30:46 +00:00
|
|
|
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
if ((idx = virPCIDeviceListFindIndex(list, dev)) >= 0)
|
|
|
|
return list->devs[idx];
|
2012-12-04 07:30:46 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
2009-08-14 13:20:40 +00:00
|
|
|
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
int virPCIDeviceFileIterate(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceFileActor actor,
|
|
|
|
void *opaque)
|
2009-08-14 13:20:40 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pcidir = NULL;
|
2009-08-14 13:20:40 +00:00
|
|
|
DIR *dir = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
struct dirent *ent;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
pcidir = g_strdup_printf("/sys/bus/pci/devices/" VIR_PCI_DEVICE_ADDRESS_FMT,
|
|
|
|
dev->address.domain, dev->address.bus, dev->address.slot,
|
|
|
|
dev->address.function);
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, pcidir) < 0)
|
2009-08-14 13:20:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *file = NULL;
|
2009-08-14 13:20:40 +00:00
|
|
|
/* Device assignment requires:
|
2011-03-17 20:26:36 +00:00
|
|
|
* $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN,
|
2015-04-23 07:32:16 +00:00
|
|
|
* $PCIDIR/rom, $PCIDIR/reset, $PCIDIR/vendor, $PCIDIR/device
|
2009-08-14 13:20:40 +00:00
|
|
|
*/
|
|
|
|
if (STREQ(ent->d_name, "config") ||
|
|
|
|
STRPREFIX(ent->d_name, "resource") ||
|
2011-03-17 20:26:36 +00:00
|
|
|
STREQ(ent->d_name, "rom") ||
|
2015-04-23 07:32:16 +00:00
|
|
|
STREQ(ent->d_name, "vendor") ||
|
|
|
|
STREQ(ent->d_name, "device") ||
|
2011-03-17 20:26:36 +00:00
|
|
|
STREQ(ent->d_name, "reset")) {
|
2019-10-22 13:26:14 +00:00
|
|
|
file = g_strdup_printf("%s/%s", pcidir, ent->d_name);
|
2010-02-10 09:55:39 +00:00
|
|
|
if ((actor)(dev, file, opaque) < 0)
|
2009-08-14 13:20:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
|
|
|
goto cleanup;
|
2009-08-14 13:20:40 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2009-08-14 13:20:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
/* virPCIDeviceAddressIOMMUGroupIterate:
|
|
|
|
* Call @actor for all devices in the same iommu_group as orig
|
|
|
|
* (including orig itself) Even if there is no iommu_group for the
|
|
|
|
* device, call @actor once for orig.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
|
|
|
|
virPCIDeviceAddressActor actor,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *groupPath = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
DIR *groupDir = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
struct dirent *ent;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
groupPath = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT "/iommu_group/devices",
|
|
|
|
orig->domain, orig->bus, orig->slot, orig->function);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2016-06-21 14:52:37 +00:00
|
|
|
if (virDirOpenQuiet(&groupDir, groupPath) < 0) {
|
2013-06-23 18:47:57 +00:00
|
|
|
/* just process the original device, nothing more */
|
|
|
|
ret = (actor)(orig, opaque);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(groupDir, &ent, groupPath)) > 0) {
|
2013-06-23 18:47:57 +00:00
|
|
|
virPCIDeviceAddress newDev;
|
|
|
|
|
|
|
|
if (virPCIDeviceAddressParse(ent->d_name, &newDev) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Found invalid device link '%s' in '%s'"),
|
|
|
|
ent->d_name, groupPath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((actor)(&newDev, opaque) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
2013-06-23 18:47:57 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(groupDir);
|
2013-06-23 18:47:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virPCIDeviceGetIOMMUGroupAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaque)
|
|
|
|
{
|
|
|
|
virPCIDeviceListPtr groupList = opaque;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) newDev = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
if (!(newDev = virPCIDeviceNew(newDevAddr->domain, newDevAddr->bus,
|
|
|
|
newDevAddr->slot, newDevAddr->function)))
|
2018-07-24 15:52:22 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
if (virPCIDeviceListAdd(groupList, newDev) < 0)
|
2018-07-24 15:52:22 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
newDev = NULL; /* it's now on the list */
|
2018-07-24 15:52:22 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virPCIDeviceGetIOMMUGroupList - return a virPCIDeviceList containing
|
|
|
|
* all of the devices in the same iommu_group as @dev.
|
|
|
|
*
|
|
|
|
* Return the new list, or NULL on failure
|
|
|
|
*/
|
|
|
|
virPCIDeviceListPtr
|
|
|
|
virPCIDeviceGetIOMMUGroupList(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
virPCIDeviceListPtr groupList = virPCIDeviceListNew();
|
|
|
|
|
|
|
|
if (!groupList)
|
|
|
|
goto error;
|
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (virPCIDeviceAddressIOMMUGroupIterate(&(dev->address),
|
2013-06-23 18:47:57 +00:00
|
|
|
virPCIDeviceGetIOMMUGroupAddOne,
|
|
|
|
groupList) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return groupList;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2013-06-23 18:47:57 +00:00
|
|
|
virObjectUnref(groupList);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
virPCIDeviceAddressPtr **iommuGroupDevices;
|
|
|
|
size_t *nIommuGroupDevices;
|
|
|
|
} virPCIDeviceAddressList;
|
|
|
|
typedef virPCIDeviceAddressList *virPCIDeviceAddressListPtr;
|
|
|
|
|
|
|
|
static int
|
|
|
|
virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaque)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virPCIDeviceAddressListPtr addrList = opaque;
|
|
|
|
virPCIDeviceAddressPtr copyAddr;
|
|
|
|
|
|
|
|
/* make a copy to insert onto the list */
|
2020-10-05 17:13:12 +00:00
|
|
|
copyAddr = g_new0(virPCIDeviceAddress, 1);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
*copyAddr = *newDevAddr;
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(*addrList->iommuGroupDevices,
|
|
|
|
*addrList->nIommuGroupDevices, copyAddr) < 0)
|
2013-06-23 18:47:57 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-06-23 18:47:57 +00:00
|
|
|
VIR_FREE(copyAddr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virPCIDeviceAddressGetIOMMUGroupAddresses - return a
|
|
|
|
* virPCIDeviceList containing all of the devices in the same
|
|
|
|
* iommu_group as @dev.
|
|
|
|
*
|
|
|
|
* Return the new list, or NULL on failure
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
|
|
|
|
virPCIDeviceAddressPtr **iommuGroupDevices,
|
|
|
|
size_t *nIommuGroupDevices)
|
|
|
|
{
|
|
|
|
virPCIDeviceAddressList addrList = { iommuGroupDevices,
|
|
|
|
nIommuGroupDevices };
|
|
|
|
|
|
|
|
if (virPCIDeviceAddressIOMMUGroupIterate(devAddr,
|
|
|
|
virPCIGetIOMMUGroupAddressesAddOne,
|
|
|
|
&addrList) < 0)
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* virPCIDeviceAddressGetIOMMUGroupNum - return the group number of
|
|
|
|
* this PCI device's iommu_group, or -2 if there is no iommu_group for
|
|
|
|
* the device (or -1 if there was any other error)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *devName = NULL;
|
|
|
|
g_autofree char *devPath = NULL;
|
|
|
|
g_autofree char *groupPath = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *groupNumStr = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
unsigned int groupNum;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
devName = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain, addr->bus,
|
|
|
|
addr->slot, addr->function);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(devPath = virPCIFile(devName, "iommu_group")))
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
|
|
|
if (virFileIsLink(devPath) != 1)
|
|
|
|
return -2;
|
2013-06-23 18:47:57 +00:00
|
|
|
if (virFileResolveLink(devPath, &groupPath) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s iommu_group symlink %s"),
|
|
|
|
devName, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
groupNumStr = g_path_get_basename(groupPath);
|
2013-06-23 18:47:57 +00:00
|
|
|
if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("device %s iommu_group symlink %s has "
|
|
|
|
"invalid group number %s"),
|
|
|
|
devName, groupPath, groupNumStr);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return groupNum;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-19 12:43:13 +00:00
|
|
|
char *
|
|
|
|
virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr)
|
|
|
|
{
|
|
|
|
g_autoptr(virPCIDevice) pci = NULL;
|
|
|
|
|
|
|
|
if (!(pci = virPCIDeviceNew(devAddr->domain,
|
|
|
|
devAddr->bus,
|
|
|
|
devAddr->slot,
|
|
|
|
devAddr->function)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virPCIDeviceGetIOMMUGroupDev(pci);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-14 20:18:44 +00:00
|
|
|
/* virPCIDeviceGetIOMMUGroupDev - return the name of the device used
|
|
|
|
* to control this PCI device's group (e.g. "/dev/vfio/15")
|
2013-04-25 10:34:43 +00:00
|
|
|
*/
|
|
|
|
char *
|
2013-06-14 20:18:44 +00:00
|
|
|
virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
|
2013-04-25 10:34:43 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *devPath = NULL;
|
|
|
|
g_autofree char *groupPath = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *groupFile = NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
if (!(devPath = virPCIFile(dev->name, "iommu_group")))
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
if (virFileIsLink(devPath) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s iommu_group file %s is not a symlink"),
|
|
|
|
dev->name, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
|
|
|
if (virFileResolveLink(devPath, &groupPath) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s iommu_group symlink %s"),
|
|
|
|
dev->name, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
2019-12-23 15:47:21 +00:00
|
|
|
groupFile = g_path_get_basename(groupPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
return g_strdup_printf("/dev/vfio/%s", groupFile);
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceDownstreamLacksACS(virPCIDevicePtr dev)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t ctrl;
|
|
|
|
unsigned int pos;
|
2012-12-04 21:50:58 +00:00
|
|
|
int fd;
|
|
|
|
int ret = 0;
|
2013-12-24 18:07:27 +00:00
|
|
|
uint16_t device_class;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2009-12-22 17:21:15 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceInit(dev, fd) < 0) {
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:07:27 +00:00
|
|
|
if (virPCIDeviceReadClass(dev, &device_class) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
pos = dev->pcie_cap_pos;
|
2013-12-24 18:07:27 +00:00
|
|
|
if (!pos || device_class != PCI_CLASS_BRIDGE_PCI)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
flags = virPCIDeviceRead16(dev, fd, pos + PCI_EXP_FLAGS);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (((flags & PCI_EXP_FLAGS_TYPE) >> 4) != PCI_EXP_TYPE_DOWNSTREAM)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceFindExtendedCapabilityOffset(dev, fd, PCI_EXT_CAP_ID_ACS);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (!pos) {
|
|
|
|
VIR_DEBUG("%s %s: downstream port lacks ACS", dev->id, dev->name);
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ctrl = virPCIDeviceRead16(dev, fd, pos + PCI_EXT_ACS_CTRL);
|
2009-12-22 17:21:15 +00:00
|
|
|
if ((ctrl & PCI_EXT_CAP_ACS_ENABLED) != PCI_EXT_CAP_ACS_ENABLED) {
|
|
|
|
VIR_DEBUG("%s %s: downstream port has ACS disabled",
|
|
|
|
dev->id, dev->name);
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
2012-12-04 21:50:58 +00:00
|
|
|
return ret;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceIsBehindSwitchLackingACS(virPCIDevicePtr dev)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) parent = NULL;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceGetParent(dev, &parent) < 0)
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
return -1;
|
2010-01-19 16:26:54 +00:00
|
|
|
if (!parent) {
|
|
|
|
/* if we have no parent, and this is the root bus, ACS doesn't come
|
|
|
|
* into play since devices on the root bus can't P2P without going
|
|
|
|
* through the root IOMMU.
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus == 0) {
|
2010-01-19 16:26:54 +00:00
|
|
|
return 0;
|
2014-09-03 19:39:21 +00:00
|
|
|
} else {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-19 16:26:54 +00:00
|
|
|
_("Failed to find parent device for %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX we should rather fail when we can't find device's parent and
|
|
|
|
* stop the loop when we get to root instead of just stopping when no
|
|
|
|
* parent can be found
|
|
|
|
*/
|
|
|
|
do {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) tmp = NULL;
|
2009-12-22 17:21:15 +00:00
|
|
|
int acs;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int ret;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
acs = virPCIDeviceDownstreamLacksACS(parent);
|
2009-12-22 17:21:15 +00:00
|
|
|
|
|
|
|
if (acs) {
|
|
|
|
if (acs < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = parent;
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceGetParent(parent, &parent);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
2009-12-22 17:21:15 +00:00
|
|
|
} while (parent);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
int virPCIDeviceIsAssignable(virPCIDevicePtr dev,
|
|
|
|
int strict_acs_check)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* XXX This could be a great place to actually check that a non-managed
|
|
|
|
* device isn't in use, e.g. by checking that device is either un-bound
|
|
|
|
* or bound to a stub driver.
|
|
|
|
*/
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceIsBehindSwitchLackingACS(dev);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
if (!strict_acs_check) {
|
|
|
|
VIR_DEBUG("%s %s: strict ACS check disabled; device assignment allowed",
|
|
|
|
dev->id, dev->name);
|
|
|
|
} else {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-22 17:21:15 +00:00
|
|
|
_("Device %s is behind a switch lacking ACS and "
|
|
|
|
"cannot be assigned"),
|
|
|
|
dev->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
logStrToLong_ui(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
|
|
|
unsigned int *result)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = virStrToLong_ui(s, end_ptr, base, result);
|
2015-12-20 22:58:58 +00:00
|
|
|
if (ret != 0)
|
2011-08-16 04:28:43 +00:00
|
|
|
VIR_ERROR(_("Failed to convert '%s' to unsigned int"), s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-17 15:57:19 +00:00
|
|
|
int
|
|
|
|
virPCIDeviceAddressParse(char *address,
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressPtr bdf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
|
|
|
|
if ((address == NULL) || (logStrToLong_ui(address, &p, 16,
|
|
|
|
&bdf->domain) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->bus) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->slot) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->function) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 21:57:45 +00:00
|
|
|
return 0;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 08:00:54 +00:00
|
|
|
|
2018-11-08 11:00:26 +00:00
|
|
|
bool
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
virZPCIDeviceAddressIsIncomplete(const virZPCIDeviceAddress *addr)
|
2018-11-08 11:00:26 +00:00
|
|
|
{
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
return !addr->uid.isSet || !addr->fid.isSet;
|
2018-11-08 11:00:26 +00:00
|
|
|
}
|
|
|
|
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virZPCIDeviceAddressIsPresent(const virZPCIDeviceAddress *addr)
|
|
|
|
{
|
|
|
|
return addr->uid.isSet || addr->fid.isSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-15 14:50:21 +00:00
|
|
|
#ifdef __linux__
|
2018-11-08 11:00:26 +00:00
|
|
|
|
2017-03-07 19:23:01 +00:00
|
|
|
virPCIDeviceAddressPtr
|
2016-05-17 13:16:05 +00:00
|
|
|
virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2016-05-17 13:16:05 +00:00
|
|
|
virPCIDeviceAddressPtr bdf = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *config_address = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_path = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
|
|
|
if (!virFileExists(device_link)) {
|
2015-12-20 22:58:58 +00:00
|
|
|
VIR_DEBUG("'%s' does not exist", device_link);
|
2016-05-17 13:16:05 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:04:38 +00:00
|
|
|
device_path = virFileCanonicalizePath(device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
if (device_path == NULL) {
|
2012-11-23 08:02:07 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to resolve device link '%s'"),
|
|
|
|
device_link);
|
2016-05-17 13:16:05 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
config_address = g_path_get_basename(device_path);
|
2020-10-05 17:13:12 +00:00
|
|
|
bdf = g_new0(virPCIDeviceAddress, 1);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (virPCIDeviceAddressParse(config_address, bdf) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-08-16 04:28:43 +00:00
|
|
|
_("Failed to parse PCI config address '%s'"),
|
|
|
|
config_address);
|
2016-05-17 13:16:05 +00:00
|
|
|
VIR_FREE(bdf);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
return bdf;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:27:29 +00:00
|
|
|
/**
|
|
|
|
* virPCIGetPhysicalFunction:
|
|
|
|
* @vf_sysfs_path: sysfs path for the virtual function
|
|
|
|
* @pf: where to store the physical function's address
|
|
|
|
*
|
|
|
|
* Given @vf_sysfs_path, this function will store the pointer
|
|
|
|
* to a newly-allocated virPCIDeviceAddress in @pf.
|
|
|
|
*
|
|
|
|
* @pf might be NULL if @vf_sysfs_path does not point to a
|
|
|
|
* virtual function. If it's not NULL, then it should be
|
|
|
|
* freed by the caller when no longer needed.
|
|
|
|
*
|
|
|
|
* Returns: >=0 on success, <0 on failure
|
2011-08-16 04:28:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path,
|
2015-12-20 22:58:58 +00:00
|
|
|
virPCIDeviceAddressPtr *pf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_link = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-25 08:27:12 +00:00
|
|
|
*pf = NULL;
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
if (virBuildPath(&device_link, vf_sysfs_path, "physfn") == -1) {
|
|
|
|
virReportOOMError();
|
2016-05-17 14:38:47 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if ((*pf = virPCIGetDeviceAddressFromSysfsLink(device_link))) {
|
2019-07-30 12:43:44 +00:00
|
|
|
VIR_DEBUG("PF for VF device '%s': " VIR_PCI_DEVICE_ADDRESS_FMT,
|
|
|
|
vf_sysfs_path,
|
2015-12-20 22:58:58 +00:00
|
|
|
(*pf)->domain, (*pf)->bus, (*pf)->slot, (*pf)->function);
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 14:38:47 +00:00
|
|
|
return 0;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
/*
|
|
|
|
* Returns virtual functions of a physical function
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctions(const char *sysfs_path,
|
|
|
|
virPCIDeviceAddressPtr **virtual_functions,
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
size_t *num_virtual_functions,
|
|
|
|
unsigned int *max_virtual_functions)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *totalvfs_file = NULL;
|
|
|
|
g_autofree char *totalvfs_str = NULL;
|
2016-04-03 18:16:51 +00:00
|
|
|
virPCIDeviceAddressPtr config_addr = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2013-07-01 03:52:43 +00:00
|
|
|
*virtual_functions = NULL;
|
|
|
|
*num_virtual_functions = 0;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
*max_virtual_functions = 0;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
totalvfs_file = g_strdup_printf("%s/sriov_totalvfs", sysfs_path);
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
if (virFileExists(totalvfs_file)) {
|
|
|
|
char *end = NULL; /* so that terminating \n doesn't create error */
|
|
|
|
|
|
|
|
if (virFileReadAll(totalvfs_file, 16, &totalvfs_str) < 0)
|
|
|
|
goto error;
|
|
|
|
if (virStrToLong_ui(totalvfs_str, &end, 10, max_virtual_functions) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unrecognized value in %s: %s"),
|
|
|
|
totalvfs_file, totalvfs_str);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 03:52:43 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
do {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_link = NULL;
|
2013-11-05 10:13:05 +00:00
|
|
|
/* look for virtfn%d links until one isn't found */
|
2019-10-22 13:26:14 +00:00
|
|
|
device_link = g_strdup_printf("%s/virtfn%zu", sysfs_path,
|
|
|
|
*num_virtual_functions);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
if (!virFileExists(device_link))
|
|
|
|
break;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (!(config_addr = virPCIGetDeviceAddressFromSysfsLink(device_link))) {
|
2013-11-05 10:13:05 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to get SRIOV function from device link '%s'"),
|
|
|
|
device_link);
|
|
|
|
goto error;
|
|
|
|
}
|
2013-03-25 13:10:51 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (VIR_APPEND_ELEMENT(*virtual_functions, *num_virtual_functions,
|
|
|
|
config_addr) < 0)
|
2013-11-05 10:13:05 +00:00
|
|
|
goto error;
|
|
|
|
} while (1);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
VIR_DEBUG("Found %zu virtual functions for %s",
|
|
|
|
*num_virtual_functions, sysfs_path);
|
2011-08-16 04:28:43 +00:00
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-11-05 10:13:05 +00:00
|
|
|
VIR_FREE(config_addr);
|
2011-08-16 04:28:43 +00:00
|
|
|
return ret;
|
2013-03-25 13:10:51 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2013-11-05 10:13:05 +00:00
|
|
|
for (i = 0; i < *num_virtual_functions; i++)
|
|
|
|
VIR_FREE((*virtual_functions)[i]);
|
|
|
|
VIR_FREE(*virtual_functions);
|
2016-05-17 14:31:16 +00:00
|
|
|
*num_virtual_functions = 0;
|
2013-03-25 13:10:51 +00:00
|
|
|
goto cleanup;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
/*
|
|
|
|
* Returns 1 if vf device is a virtual function, 0 if not, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIIsVirtualFunction(const char *vf_sysfs_device_link)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *vf_sysfs_physfn_link = NULL;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
vf_sysfs_physfn_link = g_strdup_printf("%s/physfn", vf_sysfs_device_link);
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return virFileExists(vf_sysfs_physfn_link);
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the sriov virtual function index of vf given its pf
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
|
|
|
|
const char *vf_sysfs_device_link,
|
|
|
|
int *vf_index)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int ret = -1;
|
|
|
|
size_t i;
|
2013-11-08 10:39:08 +00:00
|
|
|
size_t num_virt_fns = 0;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
unsigned int max_virt_fns = 0;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressPtr vf_bdf = NULL;
|
|
|
|
virPCIDeviceAddressPtr *virt_fns = NULL;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (!(vf_bdf = virPCIGetDeviceAddressFromSysfsLink(vf_sysfs_device_link)))
|
2011-08-16 04:28:48 +00:00
|
|
|
return ret;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetVirtualFunctions(pf_sysfs_device_link, &virt_fns,
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
&num_virt_fns, &max_virt_fns) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-08-16 04:28:48 +00:00
|
|
|
_("Error getting physical function's '%s' "
|
2013-01-14 22:11:44 +00:00
|
|
|
"virtual_functions"), pf_sysfs_device_link);
|
2011-08-16 04:28:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_virt_fns; i++) {
|
2019-07-18 18:28:15 +00:00
|
|
|
if (virPCIDeviceAddressEqual(vf_bdf, virt_fns[i])) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*vf_index = i;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
out:
|
2011-08-16 04:28:48 +00:00
|
|
|
|
|
|
|
/* free virtual functions */
|
|
|
|
for (i = 0; i < num_virt_fns; i++)
|
2013-01-14 22:11:44 +00:00
|
|
|
VIR_FREE(virt_fns[i]);
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2011-09-18 16:36:13 +00:00
|
|
|
VIR_FREE(virt_fns);
|
2011-08-16 04:28:48 +00:00
|
|
|
VIR_FREE(vf_bdf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-14 10:50:01 +00:00
|
|
|
/*
|
|
|
|
* Returns a path to the PCI sysfs file given the BDF of the PCI function
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetSysfsFile(char *virPCIDeviceName, char **pci_sysfs_device_link)
|
2011-12-14 10:50:01 +00:00
|
|
|
{
|
2019-10-22 13:26:14 +00:00
|
|
|
*pci_sysfs_device_link = g_strdup_printf(PCI_SYSFS "devices/%s",
|
|
|
|
virPCIDeviceName);
|
2013-07-18 10:13:46 +00:00
|
|
|
return 0;
|
2011-12-14 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:23 +00:00
|
|
|
int
|
2015-12-15 08:53:31 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
|
2013-01-14 22:11:44 +00:00
|
|
|
char **pci_sysfs_device_link)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2019-10-22 13:26:14 +00:00
|
|
|
*pci_sysfs_device_link = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
|
|
|
|
addr->bus, addr->slot, addr->function);
|
2013-07-18 10:13:46 +00:00
|
|
|
return 0;
|
2012-03-06 01:12:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
/**
|
|
|
|
* virPCIGetNetName:
|
|
|
|
* @device_link_sysfs_path: sysfs path to the PCI device
|
|
|
|
* @idx: used to choose which netdev when there are several
|
|
|
|
* (ignored if physPortID is set)
|
|
|
|
* @physPortID: match this string in the netdev's phys_port_id
|
|
|
|
* (or NULL to ignore and use idx instead)
|
|
|
|
* @netname: used to return the name of the netdev
|
|
|
|
* (set to NULL (but returns success) if there is no netdev)
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error (error has been logged)
|
2011-08-16 04:28:48 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-07-31 04:21:45 +00:00
|
|
|
virPCIGetNetName(const char *device_link_sysfs_path,
|
|
|
|
size_t idx,
|
|
|
|
char *physPortID,
|
|
|
|
char **netname)
|
2013-01-14 22:11:44 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pcidev_sysfs_net_path = NULL;
|
|
|
|
g_autofree char *firstEntryName = NULL;
|
|
|
|
g_autofree char *thisPhysPortID = NULL;
|
2013-01-14 22:11:44 +00:00
|
|
|
int ret = -1;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *entry = NULL;
|
2017-07-31 04:21:45 +00:00
|
|
|
size_t i = 0;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2019-01-23 09:38:48 +00:00
|
|
|
*netname = NULL;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path,
|
|
|
|
"net") == -1) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (virDirOpenQuiet(&dir, pcidev_sysfs_net_path) < 0) {
|
|
|
|
/* this *isn't* an error - caller needs to check for netname == NULL */
|
|
|
|
ret = 0;
|
2017-07-31 04:21:45 +00:00
|
|
|
goto cleanup;
|
2017-03-03 16:54:59 +00:00
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while (virDirRead(dir, &entry, pcidev_sysfs_net_path) > 0) {
|
2017-07-31 04:21:45 +00:00
|
|
|
/* if the caller sent a physPortID, compare it to the
|
|
|
|
* physportID of this netdev. If not, look for entry[idx].
|
|
|
|
*/
|
|
|
|
if (physPortID) {
|
|
|
|
if (virNetDevGetPhysPortID(entry->d_name, &thisPhysPortID) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* if this one doesn't match, keep looking */
|
|
|
|
if (STRNEQ_NULLABLE(physPortID, thisPhysPortID)) {
|
|
|
|
VIR_FREE(thisPhysPortID);
|
util: virPCIGetNetName(): use first netdev name when phys_port_id isn't matched
The mlx4 (Mellanox) netdev driver implements the sysfs phys_port_id
file for both VFs and PFs, so you can find the VF netdev plugged into
the same physical port as any given PF netdev by comparing the
contents of phys_port_id of the respective netdevs. That's what
libvirt does when attempting to find the PF netdev for a given VF
netdev (or vice versa).
Most other netdev's drivers don't implement phys_port_id, so the file
is visible in sysfs directory listing, but attempts to read it result
in ENOTSUPP. In these cases, libvirt is unable to read phys_port_id of
either the PF or the VF, so it just returns the first entry in the
PF/VF's list of netdevs.
But we've found that the i40e driver is in between those two
situations - it implements phys_port_id for PF netdevs, but doesn't
implement it for VF netdevs. So libvirt would successfully read the
phys_port_id of the PF netdev, then try to find a VF netdev with
matching phys_port_id, but would fail because phys_port_id is NULL for
all VFs. This would result in a message like the following:
Could not find network device with phys_port_id '3cfdfe9edc39'
under PCI device at /sys/class/net/ens4f1/device/virtfn0
To solve this problem in a way that won't break functionality for
anyone else, this patch saves the first netdev name we find for the
device, and returns that if we fail to find a netdev with the desired
phys_port_id.
2017-09-15 15:26:14 +00:00
|
|
|
/* save the first entry we find to use as a failsafe
|
|
|
|
* in case we don't match the phys_port_id. This is
|
|
|
|
* needed because some NIC drivers (e.g. i40e)
|
|
|
|
* implement phys_port_id for PFs, but not for VFs
|
|
|
|
*/
|
2019-10-18 13:08:21 +00:00
|
|
|
if (!firstEntryName)
|
|
|
|
firstEntryName = g_strdup(entry->d_name);
|
util: virPCIGetNetName(): use first netdev name when phys_port_id isn't matched
The mlx4 (Mellanox) netdev driver implements the sysfs phys_port_id
file for both VFs and PFs, so you can find the VF netdev plugged into
the same physical port as any given PF netdev by comparing the
contents of phys_port_id of the respective netdevs. That's what
libvirt does when attempting to find the PF netdev for a given VF
netdev (or vice versa).
Most other netdev's drivers don't implement phys_port_id, so the file
is visible in sysfs directory listing, but attempts to read it result
in ENOTSUPP. In these cases, libvirt is unable to read phys_port_id of
either the PF or the VF, so it just returns the first entry in the
PF/VF's list of netdevs.
But we've found that the i40e driver is in between those two
situations - it implements phys_port_id for PF netdevs, but doesn't
implement it for VF netdevs. So libvirt would successfully read the
phys_port_id of the PF netdev, then try to find a VF netdev with
matching phys_port_id, but would fail because phys_port_id is NULL for
all VFs. This would result in a message like the following:
Could not find network device with phys_port_id '3cfdfe9edc39'
under PCI device at /sys/class/net/ens4f1/device/virtfn0
To solve this problem in a way that won't break functionality for
anyone else, this patch saves the first netdev name we find for the
device, and returns that if we fail to find a netdev with the desired
phys_port_id.
2017-09-15 15:26:14 +00:00
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (i++ < idx)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
*netname = g_strdup(entry->d_name);
|
2017-07-31 04:21:45 +00:00
|
|
|
|
|
|
|
ret = 0;
|
2013-01-14 22:11:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (physPortID) {
|
util: virPCIGetNetName(): use first netdev name when phys_port_id isn't matched
The mlx4 (Mellanox) netdev driver implements the sysfs phys_port_id
file for both VFs and PFs, so you can find the VF netdev plugged into
the same physical port as any given PF netdev by comparing the
contents of phys_port_id of the respective netdevs. That's what
libvirt does when attempting to find the PF netdev for a given VF
netdev (or vice versa).
Most other netdev's drivers don't implement phys_port_id, so the file
is visible in sysfs directory listing, but attempts to read it result
in ENOTSUPP. In these cases, libvirt is unable to read phys_port_id of
either the PF or the VF, so it just returns the first entry in the
PF/VF's list of netdevs.
But we've found that the i40e driver is in between those two
situations - it implements phys_port_id for PF netdevs, but doesn't
implement it for VF netdevs. So libvirt would successfully read the
phys_port_id of the PF netdev, then try to find a VF netdev with
matching phys_port_id, but would fail because phys_port_id is NULL for
all VFs. This would result in a message like the following:
Could not find network device with phys_port_id '3cfdfe9edc39'
under PCI device at /sys/class/net/ens4f1/device/virtfn0
To solve this problem in a way that won't break functionality for
anyone else, this patch saves the first netdev name we find for the
device, and returns that if we fail to find a netdev with the desired
phys_port_id.
2017-09-15 15:26:14 +00:00
|
|
|
if (firstEntryName) {
|
|
|
|
/* we didn't match the provided phys_port_id, but this
|
|
|
|
* is probably because phys_port_id isn't implemented
|
|
|
|
* for this NIC driver, so just return the first
|
|
|
|
* (probably only) netname we found.
|
|
|
|
*/
|
|
|
|
*netname = firstEntryName;
|
|
|
|
firstEntryName = NULL;
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Could not find network device with "
|
|
|
|
"phys_port_id '%s' under PCI device at %s"),
|
|
|
|
physPortID, device_link_sysfs_path);
|
|
|
|
}
|
2017-07-31 04:21:45 +00:00
|
|
|
} else {
|
|
|
|
ret = 0; /* no netdev at the given index is *not* an error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2013-01-14 22:11:44 +00:00
|
|
|
return ret;
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
int pfNetDevIdx,
|
|
|
|
char **pfname,
|
|
|
|
int *vf_index)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressPtr pf_config_address = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pf_sysfs_device_path = NULL;
|
|
|
|
g_autofree char *vfname = NULL;
|
|
|
|
g_autofree char *vfPhysPortID = NULL;
|
2012-03-06 01:12:23 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address) < 0)
|
2017-03-03 16:54:59 +00:00
|
|
|
goto cleanup;
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2016-05-25 08:01:58 +00:00
|
|
|
if (!pf_config_address)
|
2017-03-03 16:54:59 +00:00
|
|
|
goto cleanup;
|
2016-05-25 08:01:58 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceAddressGetSysfsFile(pf_config_address,
|
|
|
|
&pf_sysfs_device_path) < 0) {
|
2017-03-03 16:54:59 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (virPCIGetVirtualFunctionIndex(pf_sysfs_device_path,
|
|
|
|
vf_sysfs_device_path, vf_index) < 0) {
|
|
|
|
goto cleanup;
|
2012-03-06 01:12:23 +00:00
|
|
|
}
|
|
|
|
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
/* If the caller hasn't asked for a specific pfNetDevIdx, and VF
|
|
|
|
* is bound to a netdev, learn that netdev's phys_port_id (if
|
|
|
|
* available). This can be used to disambiguate when the PF has
|
|
|
|
* multiple netdevs. If the VF isn't bound to a netdev, then we
|
|
|
|
* return netdev[pfNetDevIdx] on the PF, which may or may not be
|
|
|
|
* correct.
|
|
|
|
*/
|
|
|
|
if (pfNetDevIdx == -1) {
|
|
|
|
if (virPCIGetNetName(vf_sysfs_device_path, 0, NULL, &vfname) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (vfname) {
|
|
|
|
if (virNetDevGetPhysPortID(vfname, &vfPhysPortID) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
pfNetDevIdx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virPCIGetNetName(pf_sysfs_device_path,
|
|
|
|
pfNetDevIdx, vfPhysPortID, pfname) < 0) {
|
2012-03-06 01:12:23 +00:00
|
|
|
goto cleanup;
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (!*pfname) {
|
|
|
|
/* this shouldn't be possible. A VF can't exist unless its
|
|
|
|
* PF device is bound to a network driver
|
|
|
|
*/
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("The PF device for VF %s has no network device name"),
|
|
|
|
vf_sysfs_device_path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-03-06 01:12:23 +00:00
|
|
|
VIR_FREE(pf_config_address);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-01-19 19:33:43 +00:00
|
|
|
|
|
|
|
ssize_t
|
|
|
|
virPCIGetMdevTypes(const char *sysfspath,
|
|
|
|
virMediatedDeviceTypePtr **types)
|
|
|
|
{
|
|
|
|
ssize_t ret = -1;
|
|
|
|
int dirret = -1;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *entry;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *types_path = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virMediatedDeviceType) mdev_type = NULL;
|
2018-01-19 19:33:43 +00:00
|
|
|
virMediatedDeviceTypePtr *mdev_types = NULL;
|
|
|
|
size_t ntypes = 0;
|
|
|
|
size_t i;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
types_path = g_strdup_printf("%s/mdev_supported_types", sysfspath);
|
2018-01-19 19:33:43 +00:00
|
|
|
|
|
|
|
if ((dirret = virDirOpenIfExists(&dir, types_path)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (dirret == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirret = virDirRead(dir, &entry, types_path)) > 0) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *tmppath = NULL;
|
2018-01-19 19:33:43 +00:00
|
|
|
/* append the type id to the path and read the attributes from there */
|
2019-10-22 13:26:14 +00:00
|
|
|
tmppath = g_strdup_printf("%s/%s", types_path, entry->d_name);
|
2018-01-19 19:33:43 +00:00
|
|
|
|
|
|
|
if (virMediatedDeviceTypeReadAttrs(tmppath, &mdev_type) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dirret < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*types = g_steal_pointer(&mdev_types);
|
2018-01-19 19:33:43 +00:00
|
|
|
ret = ntypes;
|
|
|
|
ntypes = 0;
|
|
|
|
cleanup:
|
|
|
|
for (i = 0; i < ntypes; i++)
|
|
|
|
virMediatedDeviceTypeFree(mdev_types[i]);
|
|
|
|
VIR_FREE(mdev_types);
|
|
|
|
VIR_DIR_CLOSE(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
#else
|
2012-03-08 20:41:53 +00:00
|
|
|
static const char *unsupported = N_("not supported on non-linux platforms");
|
|
|
|
|
2017-03-07 19:23:01 +00:00
|
|
|
virPCIDeviceAddressPtr
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetDeviceAddressFromSysfsLink(const char *device_link G_GNUC_UNUSED)
|
2017-03-07 19:23:01 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2017-03-25 13:12:42 +00:00
|
|
|
return NULL;
|
2017-03-07 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path G_GNUC_UNUSED,
|
|
|
|
virPCIDeviceAddressPtr *pf G_GNUC_UNUSED)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetVirtualFunctions(const char *sysfs_path G_GNUC_UNUSED,
|
|
|
|
virPCIDeviceAddressPtr **virtual_functions G_GNUC_UNUSED,
|
|
|
|
size_t *num_virtual_functions G_GNUC_UNUSED,
|
|
|
|
unsigned int *max_virtual_functions G_GNUC_UNUSED)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIIsVirtualFunction(const char *vf_sysfs_device_link G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link G_GNUC_UNUSED,
|
|
|
|
const char *vf_sysfs_device_link G_GNUC_UNUSED,
|
|
|
|
int *vf_index G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:10:12 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetSysfsFile(char *virPCIDeviceName G_GNUC_UNUSED,
|
|
|
|
char **pci_sysfs_device_link G_GNUC_UNUSED)
|
2019-07-19 06:10:12 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-08 19:19:36 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr dev G_GNUC_UNUSED,
|
|
|
|
char **pci_sysfs_device_link G_GNUC_UNUSED)
|
2012-03-08 19:19:36 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-03-08 19:19:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetNetName(const char *device_link_sysfs_path G_GNUC_UNUSED,
|
|
|
|
size_t idx G_GNUC_UNUSED,
|
|
|
|
char *physPortID G_GNUC_UNUSED,
|
|
|
|
char **netname G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path G_GNUC_UNUSED,
|
|
|
|
int pfNetDevIdx G_GNUC_UNUSED,
|
|
|
|
char **pfname G_GNUC_UNUSED,
|
|
|
|
int *vf_index G_GNUC_UNUSED)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-03-06 01:12:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-01-29 16:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
ssize_t
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetMdevTypes(const char *sysfspath G_GNUC_UNUSED,
|
|
|
|
virMediatedDeviceTypePtr **types G_GNUC_UNUSED)
|
2018-01-29 16:07:38 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
#endif /* __linux__ */
|
2014-05-15 08:04:28 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virPCIDeviceIsPCIExpress(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = dev->pcie_cap_pos != 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virPCIDeviceHasPCIExpressLink(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
uint16_t cap, type;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
cap = virPCIDeviceRead16(dev, fd, dev->pcie_cap_pos + PCI_CAP_FLAGS);
|
|
|
|
type = (cap & PCI_EXP_FLAGS_TYPE) >> 4;
|
|
|
|
|
|
|
|
ret = type != PCI_EXP_TYPE_ROOT_INT_EP && type != PCI_EXP_TYPE_ROOT_EC;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virPCIDeviceGetLinkCapSta(virPCIDevicePtr dev,
|
|
|
|
int *cap_port,
|
|
|
|
unsigned int *cap_speed,
|
|
|
|
unsigned int *cap_width,
|
|
|
|
unsigned int *sta_speed,
|
|
|
|
unsigned int *sta_width)
|
|
|
|
{
|
|
|
|
uint32_t t;
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!dev->pcie_cap_pos) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("pci device %s is not a PCI-Express device"),
|
|
|
|
dev->name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = virPCIDeviceRead32(dev, fd, dev->pcie_cap_pos + PCI_EXP_LNKCAP);
|
|
|
|
|
|
|
|
*cap_port = t >> 24;
|
|
|
|
*cap_speed = t & PCI_EXP_LNKCAP_SPEED;
|
|
|
|
*cap_width = (t & PCI_EXP_LNKCAP_WIDTH) >> 4;
|
|
|
|
|
|
|
|
t = virPCIDeviceRead16(dev, fd, dev->pcie_cap_pos + PCI_EXP_LNKSTA);
|
|
|
|
|
|
|
|
*sta_speed = t & PCI_EXP_LNKSTA_SPEED;
|
|
|
|
*sta_width = (t & PCI_EXP_LNKSTA_WIDTH) >> 4;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-23 04:38:30 +00:00
|
|
|
|
|
|
|
|
2016-03-15 11:22:03 +00:00
|
|
|
int virPCIGetHeaderType(virPCIDevicePtr dev, int *hdrType)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
uint8_t type;
|
|
|
|
|
|
|
|
*hdrType = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2016-03-15 11:22:03 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
type = virPCIDeviceRead8(dev, fd, PCI_HEADER_TYPE);
|
|
|
|
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
|
|
|
|
type &= PCI_HEADER_TYPE_MASK;
|
|
|
|
if (type >= VIR_PCI_HEADER_LAST) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2019-06-21 15:58:28 +00:00
|
|
|
_("Unknown PCI header type '%d' for device '%s'"),
|
|
|
|
type, dev->name);
|
2016-03-15 11:22:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*hdrType = type;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-23 04:38:30 +00:00
|
|
|
void
|
|
|
|
virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_FREE(dev->link_cap);
|
|
|
|
VIR_FREE(dev->link_sta);
|
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
2018-07-24 15:52:20 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virPCIDeviceAddressFree(virPCIDeviceAddressPtr address)
|
|
|
|
{
|
|
|
|
VIR_FREE(address);
|
|
|
|
}
|