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
|
|
|
|
*
|
2013-02-15 22:01:44 +00:00
|
|
|
* Copyright (C) 2009-2013 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
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Mark McLoughlin <markmc@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-13 14:52:25 +00:00
|
|
|
#include "virpci.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2011-08-16 04:28:43 +00:00
|
|
|
#include <stdlib.h>
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-04-25 20:24:42 +00:00
|
|
|
#include "dirname.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.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"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
|
|
|
#include "virutil.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
#define PCI_SYSFS "/sys/bus/pci/"
|
|
|
|
#define PCI_ID_LEN 10 /* "XXXX XXXX" */
|
|
|
|
#define PCI_ADDR_LEN 13 /* "XXXX:XX:XX.X" */
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
#define SRIOV_FOUND 0
|
|
|
|
#define SRIOV_NOT_FOUND 1
|
|
|
|
#define SRIOV_ERROR -1
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virPCIDevice {
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int domain;
|
|
|
|
unsigned int bus;
|
|
|
|
unsigned int slot;
|
|
|
|
unsigned int function;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
char name[PCI_ADDR_LEN]; /* domain:bus:slot.function */
|
|
|
|
char id[PCI_ID_LEN]; /* product vendor */
|
2011-06-22 20:52:32 +00:00
|
|
|
char *path;
|
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 *used_by; /* The domain which uses the device */
|
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;
|
2013-04-23 18:50:15 +00:00
|
|
|
const char *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-04-15 10:29:23 +00:00
|
|
|
unsigned int 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 */
|
|
|
|
|
|
|
|
/* 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 */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_EXP_DEVCAP_FLR (1<<28) /* Function Level Reset */
|
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
|
2013-01-14 22:11:44 +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)
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static virClassPtr virPCIDeviceListClass;
|
|
|
|
|
|
|
|
static void virPCIDeviceListDispose(void *obj);
|
|
|
|
|
|
|
|
static int virPCIOnceInit(void)
|
|
|
|
{
|
|
|
|
if (!(virPCIDeviceListClass = virClassNew(virClassForObjectLockable(),
|
|
|
|
"virPCIDeviceList",
|
|
|
|
sizeof(virPCIDeviceList),
|
|
|
|
virPCIDeviceListDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virPCI)
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigOpen(virPCIDevicePtr dev, bool fatal)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(dev->path, 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 {
|
|
|
|
char ebuf[1024];
|
|
|
|
VIR_WARN("Failed to open config space file '%s': %s",
|
|
|
|
dev->path, virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
char ebuf[1024];
|
|
|
|
VIR_WARN("Failed to close config space file '%s': %s",
|
|
|
|
dev->path, virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
}
|
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) {
|
2009-03-02 16:18:11 +00:00
|
|
|
char ebuf[1024];
|
2010-05-19 10:00:18 +00:00
|
|
|
VIR_WARN("Failed to read from '%s' : %s", dev->path,
|
2009-03-02 16:18:11 +00:00
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2009-03-02 16:18:11 +00:00
|
|
|
char ebuf[1024];
|
2010-05-19 10:00:18 +00:00
|
|
|
VIR_WARN("Failed to write to '%s' : %s", dev->path,
|
2009-03-02 16:18:11 +00:00
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
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-01-14 22:11:44 +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);
|
|
|
|
|
|
|
|
dir = opendir(PCI_SYSFS "devices");
|
|
|
|
if (!dir) {
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_WARN("Failed to open " PCI_SYSFS "devices");
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((entry = readdir(dir))) {
|
2010-03-30 15:31:19 +00:00
|
|
|
unsigned int domain, bus, slot, function;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr check;
|
2010-03-30 15:31:19 +00:00
|
|
|
char *tmp;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Ignore '.' and '..' */
|
|
|
|
if (entry->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
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 */
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
*matched = 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;
|
|
|
|
}
|
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-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(check);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
closedir(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;
|
2010-07-23 19:03:29 +00:00
|
|
|
char *path;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (virAsprintf(&path, PCI_SYSFS "devices/%s/physfn", dev->name) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
found = virFileExists(path);
|
|
|
|
VIR_FREE(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 */
|
2009-08-17 14:05:23 +00:00
|
|
|
if (dev->domain != check->domain ||
|
|
|
|
dev->bus != check->bus ||
|
2010-07-23 09:25:24 +00:00
|
|
|
(dev->slot == check->slot &&
|
|
|
|
dev->function == check->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
|
|
|
|
2009-08-17 14:05:23 +00:00
|
|
|
if (dev->domain != check->domain)
|
2009-03-02 16:18:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(check, false)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* Is it a bridge? */
|
2013-01-14 22:11:44 +00:00
|
|
|
device_class = virPCIDeviceRead16(check, fd, PCI_CLASS_DEVICE);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (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
|
|
|
|
*/
|
2012-12-04 21:50:58 +00:00
|
|
|
if (dev->bus == secondary) {
|
|
|
|
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
|
|
|
|
|
|
|
/* otherwise, SRIOV allows VFs to be on different busses then their PFs.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (dev->bus > secondary && dev->bus <= subordinate) {
|
|
|
|
if (*best == NULL) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*best = virPCIDeviceNew(check->domain, check->bus, check->slot,
|
|
|
|
check->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;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((bestfd = virPCIDeviceConfigOpen(*best, false)) < 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);
|
|
|
|
*best = virPCIDeviceNew(check->domain, check->bus, check->slot,
|
|
|
|
check->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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 21:50:58 +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
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr parent, conflict;
|
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;
|
|
|
|
}
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((parentfd = virPCIDeviceConfigOpen(parent, true)) < 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.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
ctl = virPCIDeviceRead16(dev, cfgfd, 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
|
|
|
|
|
|
|
usleep(200 * 1000); /* sleep 200ms */
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite16(parent, parentfd, PCI_BRIDGE_CONTROL, ctl);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
usleep(200 * 1000); /* sleep 200ms */
|
|
|
|
|
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
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
out:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(parent, parentfd);
|
|
|
|
virPCIDeviceFree(parent);
|
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
|
|
|
|
|
|
|
usleep(10 * 1000); /* sleep 10ms */
|
|
|
|
|
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
|
|
|
|
|
|
|
usleep(10 * 1000); /* sleep 10ms */
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int ret = -1;
|
2012-12-04 21:50:58 +00:00
|
|
|
int fd;
|
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-01-14 22:11:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev, true)) < 0)
|
2009-03-02 16:18:11 +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
|
|
|
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 */
|
2009-08-14 07:31:11 +00:00
|
|
|
if (ret < 0 && dev->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,
|
|
|
|
err ? err->message : _("no FLR, PM reset or bus reset available"));
|
|
|
|
}
|
|
|
|
|
2012-12-04 21:50:58 +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
|
|
|
|
2011-04-03 09:21:15 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDriverDir(char **buffer, const char *driver)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2011-04-03 09:21:15 +00:00
|
|
|
VIR_FREE(*buffer);
|
|
|
|
|
|
|
|
if (virAsprintf(buffer, PCI_SYSFS "drivers/%s", driver) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 09:21:15 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDriverFile(char **buffer, const char *driver, const char *file)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2011-04-03 09:21:15 +00:00
|
|
|
VIR_FREE(*buffer);
|
|
|
|
|
|
|
|
if (virAsprintf(buffer, PCI_SYSFS "drivers/%s/%s", driver, file) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2011-04-03 09:21:15 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIFile(char **buffer, const char *device, const char *file)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2011-04-03 09:21:15 +00:00
|
|
|
VIR_FREE(*buffer);
|
|
|
|
|
|
|
|
if (virAsprintf(buffer, PCI_SYSFS "devices/%s/%s", device, file) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIProbeStubDriver(const char *driver)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2011-04-03 09:21:15 +00:00
|
|
|
char *drvpath = NULL;
|
2013-05-24 10:14:02 +00:00
|
|
|
bool probed = false;
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
recheck:
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverDir(&drvpath, driver) == 0 && virFileExists(drvpath)) {
|
2013-01-10 07:51:43 +00:00
|
|
|
/* driver already loaded, return */
|
2011-04-03 09:21:15 +00:00
|
|
|
VIR_FREE(drvpath);
|
2013-01-10 07:51:43 +00:00
|
|
|
return 0;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(drvpath);
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
if (!probed) {
|
2013-01-10 07:51:43 +00:00
|
|
|
const char *const probecmd[] = { MODPROBE, driver, NULL };
|
2013-05-24 10:14:02 +00:00
|
|
|
probed = true;
|
2013-01-10 07:51:43 +00:00
|
|
|
if (virRun(probecmd, NULL) < 0) {
|
2009-03-02 16:18:11 +00:00
|
|
|
char ebuf[1024];
|
2013-01-10 07:51:43 +00:00
|
|
|
VIR_WARN("failed to load driver %s: %s", driver,
|
2012-03-29 09:52:04 +00:00
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
2013-01-10 07:51:43 +00:00
|
|
|
return -1;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
goto recheck;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
return -1;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
static const char *virPCIKnownStubs[] = {
|
|
|
|
"pciback", /* used by xen */
|
|
|
|
"pci-stub", /* used by kvm legacy passthrough */
|
|
|
|
"vfio-pci", /* used by VFIO device assignment */
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2011-04-06 07:13:10 +00:00
|
|
|
static int
|
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
|
|
|
virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
|
2011-04-06 07:13:10 +00:00
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
char *drvdir = NULL;
|
|
|
|
char *path = NULL;
|
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
|
|
|
char *driver = NULL;
|
|
|
|
const char **stubTest;
|
|
|
|
bool isStub = false;
|
2011-04-06 07:13:10 +00:00
|
|
|
|
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 the device is currently bound to one of the "well known"
|
|
|
|
* stub drivers, then unbind it, otherwise ignore it.
|
|
|
|
*/
|
|
|
|
if (virPCIFile(&path, dev->name, "driver") < 0)
|
2011-05-03 15:29:26 +00:00
|
|
|
goto cleanup;
|
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
|
|
|
/* path = "/sys/bus/pci/dddd:bb:ss.ff/driver" */
|
|
|
|
if (virFileIsLink(path) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s driver file %s is not a symlink"),
|
|
|
|
dev->name, path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virFileResolveLink(path, &drvdir) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s driver symlink %s"),
|
|
|
|
dev->name, path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* drvdir = "/sys/bus/pci/drivers/${drivername}" */
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(driver, last_component(drvdir)) < 0)
|
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
|
|
|
goto cleanup;
|
2011-05-03 15:29:26 +00:00
|
|
|
|
2011-04-06 07:13:14 +00:00
|
|
|
if (!dev->unbind_from_stub)
|
|
|
|
goto remove_slot;
|
|
|
|
|
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 the device isn't bound to a known stub, skip the unbind. */
|
|
|
|
for (stubTest = virPCIKnownStubs; *stubTest != NULL; stubTest++) {
|
|
|
|
if (STREQ(driver, *stubTest)) {
|
|
|
|
isStub = true;
|
|
|
|
VIR_DEBUG("Found stub driver %s", *stubTest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isStub)
|
|
|
|
goto remove_slot;
|
2011-04-06 07:13:10 +00:00
|
|
|
|
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 (virFileExists(drvdir)) {
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "unbind") < 0) {
|
2011-04-06 07:13:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileWriteStr(path, dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2011-04-06 07:13:14 +00:00
|
|
|
_("Failed to unbind PCI device '%s' from %s"),
|
2011-04-06 07:13:10 +00:00
|
|
|
dev->name, driver);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = false;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
|
|
|
remove_slot:
|
|
|
|
if (!dev->remove_slot)
|
|
|
|
goto reprobe;
|
2011-04-06 07:13:10 +00:00
|
|
|
|
|
|
|
/* Xen's pciback.ko wants you to use remove_slot on the specific device */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "remove_slot") < 0) {
|
2011-04-06 07:13:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2011-04-06 07:13:14 +00:00
|
|
|
_("Failed to remove slot for PCI device '%s' from %s"),
|
2011-04-06 07:13:10 +00:00
|
|
|
dev->name, driver);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->remove_slot = false;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
|
|
|
reprobe:
|
|
|
|
if (!dev->reprobe) {
|
|
|
|
result = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-04-06 07:13:10 +00:00
|
|
|
|
|
|
|
/* Trigger a re-probe of the device is not in the stub's dynamic
|
|
|
|
* ID table. If the stub is available, but 'remove_id' isn't
|
|
|
|
* available, then re-probing would just cause the device to be
|
|
|
|
* re-bound to the stub.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "remove_id") < 0) {
|
2011-04-06 07:13:10 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileExists(drvdir) || virFileExists(path)) {
|
|
|
|
if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to trigger a re-probe for PCI device '%s'"),
|
|
|
|
dev->name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2011-04-06 07:13:14 +00:00
|
|
|
/* do not do it again */
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = false;
|
|
|
|
dev->remove_slot = false;
|
|
|
|
dev->reprobe = false;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
2011-04-06 07:13:10 +00:00
|
|
|
VIR_FREE(drvdir);
|
|
|
|
VIR_FREE(path);
|
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
|
|
|
VIR_FREE(driver);
|
2011-04-06 07:13:10 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceBindToStub(virPCIDevicePtr dev, const char *driver)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2011-04-03 09:21:15 +00:00
|
|
|
int result = -1;
|
|
|
|
char *drvdir = NULL;
|
|
|
|
char *path = NULL;
|
2013-04-10 10:44:41 +00:00
|
|
|
int reprobe = false;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
|
|
|
/* check whether the device is already bound to a driver */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverDir(&drvdir, driver) < 0 ||
|
|
|
|
virPCIFile(&path, dev->name, "driver") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileExists(path)) {
|
|
|
|
if (virFileLinkPointsTo(path, drvdir)) {
|
|
|
|
/* The device is already bound to pci-stub */
|
|
|
|
result = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
reprobe = true;
|
2011-04-06 07:13:14 +00:00
|
|
|
}
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
/* Add the PCI device ID to the stub's dynamic ID table;
|
|
|
|
* this is needed to allow us to bind the device to the stub.
|
|
|
|
* Note: if the device is not currently bound to any driver,
|
|
|
|
* stub will immediately be bound to the device. Also, note
|
|
|
|
* that if a new device with this ID is hotplugged, or if a probe
|
|
|
|
* is triggered for such a device, it will also be immediately
|
|
|
|
* bound by the stub.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "new_id") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto cleanup;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 09:47:08 +00:00
|
|
|
if (virFileWriteStr(path, dev->id, 0) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-03 12:38:52 +00:00
|
|
|
_("Failed to add PCI device ID '%s' to %s"),
|
|
|
|
dev->id, driver);
|
2011-04-03 09:21:15 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 07:13:14 +00:00
|
|
|
/* check whether the device is bound to pci-stub when we write dev->id to
|
|
|
|
* new_id.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverDir(&drvdir, driver) < 0 ||
|
|
|
|
virPCIFile(&path, dev->name, "driver") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileLinkPointsTo(path, drvdir)) {
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = true;
|
|
|
|
dev->remove_slot = true;
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
|
|
|
}
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* If the device is already bound to a driver, unbind it.
|
|
|
|
* Note, this will have rather unpleasant side effects if this
|
|
|
|
* PCI device happens to be IDE controller for the disk hosting
|
|
|
|
* your root filesystem.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIFile(&path, dev->name, "driver/unbind") < 0) {
|
2011-04-03 09:21:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-04-06 07:13:14 +00:00
|
|
|
if (virFileExists(path)) {
|
|
|
|
if (virFileWriteStr(path, dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to unbind PCI device '%s'"),
|
|
|
|
dev->name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
dev->reprobe = reprobe;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 12:38:52 +00:00
|
|
|
/* If the device isn't already bound to pci-stub, try binding it now.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverDir(&drvdir, driver) < 0 ||
|
|
|
|
virPCIFile(&path, dev->name, "driver") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
2009-04-03 12:38:52 +00:00
|
|
|
if (!virFileLinkPointsTo(path, drvdir)) {
|
|
|
|
/* Xen's pciback.ko wants you to use new_slot first */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "new_slot") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 09:47:08 +00:00
|
|
|
if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-03 12:38:52 +00:00
|
|
|
_("Failed to add slot for PCI device '%s' to %s"),
|
|
|
|
dev->name, driver);
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->remove_slot = true;
|
2011-04-03 09:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "bind") < 0) {
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 09:47:08 +00:00
|
|
|
if (virFileWriteStr(path, dev->name, 0) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-03 12:38:52 +00:00
|
|
|
_("Failed to bind PCI device '%s' to %s"),
|
|
|
|
dev->name, driver);
|
2011-04-06 07:13:14 +00:00
|
|
|
goto remove_id;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = true;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 07:13:14 +00:00
|
|
|
remove_id:
|
2009-04-03 12:38:52 +00:00
|
|
|
/* If 'remove_id' exists, remove the device id from pci-stub's dynamic
|
|
|
|
* ID table so that 'drivers_probe' works below.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDriverFile(&path, driver, "remove_id") < 0) {
|
2011-04-11 22:25:25 +00:00
|
|
|
/* We do not remove PCI ID from pci-stub, and we cannot reprobe it */
|
2011-04-06 07:13:14 +00:00
|
|
|
if (dev->reprobe) {
|
|
|
|
VIR_WARN("Could not remove PCI ID '%s' from %s, and the device "
|
|
|
|
"cannot be probed again.", dev->id, driver);
|
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->reprobe = false;
|
2011-04-03 09:21:15 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-03 09:47:08 +00:00
|
|
|
if (virFileExists(path) && virFileWriteStr(path, dev->id, 0) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-04-03 12:38:52 +00:00
|
|
|
_("Failed to remove PCI ID '%s' from %s"),
|
|
|
|
dev->id, driver);
|
2011-04-06 07:13:14 +00:00
|
|
|
|
2011-04-11 22:25:25 +00:00
|
|
|
/* remove PCI ID from pci-stub failed, and we cannot reprobe it */
|
2011-04-06 07:13:14 +00:00
|
|
|
if (dev->reprobe) {
|
|
|
|
VIR_WARN("Failed to remove PCI ID '%s' from %s, and the device "
|
|
|
|
"cannot be probed again.", dev->id, driver);
|
|
|
|
}
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->reprobe = false;
|
2011-04-03 09:21:15 +00:00
|
|
|
goto cleanup;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 09:21:15 +00:00
|
|
|
result = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(drvdir);
|
|
|
|
VIR_FREE(path);
|
|
|
|
|
2011-04-06 07:13:14 +00:00
|
|
|
if (result < 0) {
|
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
|
|
|
virPCIDeviceUnbindFromStub(dev);
|
2011-04-06 07:13:14 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 09:21:15 +00:00
|
|
|
return result;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceDetach(virPCIDevicePtr dev,
|
|
|
|
virPCIDeviceList *activeDevs,
|
|
|
|
virPCIDeviceList *inactiveDevs,
|
|
|
|
const char *driver)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2013-04-23 18:50:15 +00:00
|
|
|
if (!driver && dev->stubDriver)
|
|
|
|
driver = dev->stubDriver;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIProbeStubDriver(driver) < 0) {
|
2013-01-10 07:51:43 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to load PCI stub module %s"), driver);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceBindToStub(dev, driver) < 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;
|
|
|
|
|
|
|
|
/* Add the dev into list inactiveDevs */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (inactiveDevs && !virPCIDeviceListFind(inactiveDevs, dev)) {
|
|
|
|
if (virPCIDeviceListAdd(inactiveDevs, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
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 */
|
|
|
|
if (inactiveDevs)
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListSteal(inactiveDevs, dev);
|
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
|
|
|
}
|
|
|
|
|
2010-01-20 20:53:59 +00:00
|
|
|
/* Certain hypervisors (like qemu/kvm) map the PCI bar(s) on
|
|
|
|
* the host when doing device passthrough. This can lead to a race
|
|
|
|
* condition where the hypervisor is still cleaning up the device while
|
|
|
|
* libvirt is trying to re-attach it to the host device driver. To avoid
|
|
|
|
* this situation, we look through /proc/iomem, and if the hypervisor is
|
2013-04-19 20:18:14 +00:00
|
|
|
* still holding on to the bar (denoted by the string in the matcher
|
|
|
|
* variable), then we can wait around a bit for that to clear up.
|
2010-01-20 20:53:59 +00:00
|
|
|
*
|
|
|
|
* A typical /proc/iomem looks like this (snipped for brevity):
|
|
|
|
* 00010000-0008efff : System RAM
|
|
|
|
* 0008f000-0008ffff : reserved
|
|
|
|
* ...
|
|
|
|
* 00100000-cc9fcfff : System RAM
|
|
|
|
* 00200000-00483d3b : Kernel code
|
|
|
|
* 00483d3c-005c88df : Kernel data
|
|
|
|
* cc9fd000-ccc71fff : ACPI Non-volatile Storage
|
|
|
|
* ...
|
|
|
|
* d0200000-d02fffff : PCI Bus #05
|
|
|
|
* d0200000-d021ffff : 0000:05:00.0
|
|
|
|
* d0200000-d021ffff : e1000e
|
|
|
|
* d0220000-d023ffff : 0000:05:00.0
|
|
|
|
* d0220000-d023ffff : e1000e
|
|
|
|
* ...
|
|
|
|
* f0000000-f0003fff : 0000:00:1b.0
|
|
|
|
* f0000000-f0003fff : kvm_assigned_device
|
|
|
|
*
|
|
|
|
* Returns 0 if we are clear to continue, and 1 if the hypervisor is still
|
2013-04-19 20:18:14 +00:00
|
|
|
* holding on to the resource.
|
2010-01-20 20:53:59 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWaitForCleanup(virPCIDevicePtr dev, const char *matcher)
|
2010-01-20 20:53:59 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[160];
|
2010-03-30 15:31:19 +00:00
|
|
|
char *tmp;
|
2010-01-20 20:53:59 +00:00
|
|
|
unsigned long long start, end;
|
2010-03-30 15:31:19 +00:00
|
|
|
unsigned int domain, bus, slot, function;
|
2013-05-24 10:14:02 +00:00
|
|
|
bool in_matching_device;
|
2010-01-20 20:53:59 +00:00
|
|
|
int ret;
|
|
|
|
size_t match_depth;
|
|
|
|
|
|
|
|
fp = fopen("/proc/iomem", "r");
|
|
|
|
if (!fp) {
|
|
|
|
/* If we failed to open iomem, we just basically ignore the error. The
|
|
|
|
* unbind might succeed anyway, and besides, it's very likely we have
|
|
|
|
* no way to report the error
|
|
|
|
*/
|
2011-05-09 09:24:09 +00:00
|
|
|
VIR_DEBUG("Failed to open /proc/iomem, trying to continue anyway");
|
2010-01-20 20:53:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2013-05-24 10:14:02 +00:00
|
|
|
in_matching_device = false;
|
2010-01-20 20:53:59 +00:00
|
|
|
match_depth = 0;
|
|
|
|
while (fgets(line, sizeof(line), fp) != 0) {
|
|
|
|
/* the logic here is a bit confusing. For each line, we look to
|
|
|
|
* see if it matches the domain:bus:slot.function we were given.
|
|
|
|
* If this line matches the DBSF, then any subsequent lines indented
|
|
|
|
* by 2 spaces are the PCI regions for this device. It's also
|
|
|
|
* possible that none of the PCI regions are currently mapped, in
|
|
|
|
* which case we have no indented regions. This code handles all
|
|
|
|
* of these situations
|
|
|
|
*/
|
|
|
|
if (in_matching_device && (strspn(line, " ") == (match_depth + 2))) {
|
2010-03-30 15:31:19 +00:00
|
|
|
/* expected format: <start>-<end> : <suffix> */
|
|
|
|
if (/* start */
|
|
|
|
virStrToLong_ull(line, &tmp, 16, &start) < 0 || *tmp != '-' ||
|
|
|
|
/* end */
|
|
|
|
virStrToLong_ull(tmp + 1, &tmp, 16, &end) < 0 ||
|
|
|
|
(tmp = STRSKIP(tmp, " : ")) == NULL)
|
2010-01-20 20:53:59 +00:00
|
|
|
continue;
|
|
|
|
|
2010-03-30 15:31:19 +00:00
|
|
|
if (STRPREFIX(tmp, matcher)) {
|
2010-01-20 20:53:59 +00:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-05-24 10:14:02 +00:00
|
|
|
in_matching_device = false;
|
2010-01-20 20:53:59 +00:00
|
|
|
|
2010-03-30 15:31:19 +00:00
|
|
|
/* expected format: <start>-<end> : <domain>:<bus>:<slot>.<function> */
|
|
|
|
if (/* start */
|
|
|
|
virStrToLong_ull(line, &tmp, 16, &start) < 0 || *tmp != '-' ||
|
|
|
|
/* end */
|
|
|
|
virStrToLong_ull(tmp + 1, &tmp, 16, &end) < 0 ||
|
|
|
|
(tmp = STRSKIP(tmp, " : ")) == NULL ||
|
|
|
|
/* domain */
|
|
|
|
virStrToLong_ui(tmp, &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, &tmp, 16, &function) < 0 || *tmp != '\n')
|
2010-01-20 20:53:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (domain != dev->domain || bus != dev->bus || slot != dev->slot ||
|
|
|
|
function != dev->function)
|
|
|
|
continue;
|
2013-05-24 10:14:02 +00:00
|
|
|
in_matching_device = true;
|
2010-01-20 20:53:59 +00:00
|
|
|
match_depth = strspn(line, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-20 00:25:23 +00:00
|
|
|
VIR_FORCE_FCLOSE(fp);
|
2010-01-20 20:53:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-04-06 06:21:00 +00:00
|
|
|
char *path = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
char *id_str;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIFile(&path, dev->name, id_name) < 0) {
|
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 */
|
2011-04-03 09:21:15 +00:00
|
|
|
if (virFileReadAll(path, 7, &id_str) < 0) {
|
|
|
|
VIR_FREE(path);
|
2009-03-02 16:18:11 +00:00
|
|
|
return NULL;
|
2011-04-03 09:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(path);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-12-14 10:50:14 +00:00
|
|
|
int
|
2013-04-15 10:29:23 +00:00
|
|
|
virPCIGetAddrString(unsigned int domain,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int slot,
|
|
|
|
unsigned int function,
|
2013-01-14 22:11:44 +00:00
|
|
|
char **pciConfigAddr)
|
2011-12-14 10:50:14 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr dev = NULL;
|
2011-12-14 10:50:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
dev = virPCIDeviceNew(domain, bus, slot, function);
|
2011-12-14 10:50:14 +00:00
|
|
|
if (dev != NULL) {
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(*pciConfigAddr, dev->name) < 0)
|
2011-12-14 10:50:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(dev);
|
2011-12-14 10:50:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr dev;
|
2011-06-30 23:35:46 +00:00
|
|
|
char *vendor = NULL;
|
|
|
|
char *product = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
if (VIR_ALLOC(dev) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-03-02 16:18:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->domain = domain;
|
|
|
|
dev->bus = bus;
|
|
|
|
dev->slot = slot;
|
|
|
|
dev->function = function;
|
|
|
|
|
2011-06-22 20:52:32 +00:00
|
|
|
if (snprintf(dev->name, sizeof(dev->name), "%.4x:%.2x:%.2x.%.1x",
|
|
|
|
dev->domain, dev->bus, dev->slot,
|
|
|
|
dev->function) >= sizeof(dev->name)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-06-22 20:52:32 +00:00
|
|
|
_("dev->name buffer overflow: %.4x:%.2x:%.2x.%.1x"),
|
|
|
|
dev->domain, dev->bus, dev->slot, dev->function);
|
2011-06-30 23:35:46 +00:00
|
|
|
goto error;
|
2011-06-22 20:52:32 +00:00
|
|
|
}
|
|
|
|
if (virAsprintf(&dev->path, PCI_SYSFS "devices/%s/config",
|
|
|
|
dev->name) < 0) {
|
|
|
|
virReportOOMError();
|
2011-06-30 23:35:46 +00:00
|
|
|
goto error;
|
2011-06-22 20:52:32 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-04-30 15:44:19 +00:00
|
|
|
if (access(dev->path, F_OK) != 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Device %s not found: could not access %s"),
|
|
|
|
dev->name, dev->path);
|
2011-06-30 23:35:46 +00:00
|
|
|
goto error;
|
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);
|
2011-06-30 23:35:46 +00:00
|
|
|
goto error;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* strings contain '0x' prefix */
|
2011-06-22 20:52:32 +00:00
|
|
|
if (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]);
|
2011-06-30 23:35:46 +00:00
|
|
|
goto error;
|
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);
|
|
|
|
|
2011-06-30 23:35:46 +00:00
|
|
|
cleanup:
|
|
|
|
VIR_FREE(product);
|
|
|
|
VIR_FREE(vendor);
|
2009-03-02 16:18:11 +00:00
|
|
|
return dev;
|
2011-06-30 23:35:46 +00:00
|
|
|
|
|
|
|
error:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(dev);
|
2011-06-30 23:35:46 +00:00
|
|
|
dev = NULL;
|
|
|
|
goto cleanup;
|
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);
|
2011-06-22 20:52:32 +00:00
|
|
|
VIR_FREE(dev->path);
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
2009-08-17 14:05:23 +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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int
|
|
|
|
virPCIDeviceGetManaged(virPCIDevicePtr dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
|
|
|
return dev->managed;
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:50:15 +00:00
|
|
|
void
|
|
|
|
virPCIDeviceSetStubDriver(virPCIDevicePtr dev, const char *driver)
|
|
|
|
{
|
|
|
|
dev->stubDriver = driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virPCIDeviceGetStubDriver(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->stubDriver;
|
|
|
|
}
|
|
|
|
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int
|
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
|
|
|
}
|
|
|
|
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int
|
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
|
|
|
}
|
|
|
|
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
void
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetUsedBy(virPCIDevicePtr dev, const char *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
|
|
|
{
|
|
|
|
dev->used_by = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetUsedBy(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->used_by;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
void virPCIDeviceReattachInit(virPCIDevicePtr pci)
|
2011-07-03 12:09:44 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
pci->unbind_from_stub = true;
|
|
|
|
pci->remove_slot = true;
|
|
|
|
pci->reprobe = true;
|
2011-07-03 12:09:44 +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;
|
2009-08-17 14:05:23 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(list->devs, list->count+1) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-08-17 14:05:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->devs[list->count++] = dev;
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
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];
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
if (idx != --list->count) {
|
|
|
|
memmove(&list->devs[idx],
|
|
|
|
&list->devs[idx + 1],
|
|
|
|
sizeof(*list->devs) * (list->count - idx));
|
|
|
|
}
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
if (VIR_REALLOC_N(list->devs, list->count) < 0) {
|
|
|
|
; /* not fatal */
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
2012-12-04 07:30:46 +00:00
|
|
|
|
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
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr ret = virPCIDeviceListSteal(list, dev);
|
2009-10-27 17:30:16 +00:00
|
|
|
if (ret)
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(ret);
|
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
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
|
|
|
if (list->devs[i]->domain == dev->domain &&
|
|
|
|
list->devs[i]->bus == dev->bus &&
|
|
|
|
list->devs[i]->slot == dev->slot &&
|
|
|
|
list->devs[i]->function == dev->function)
|
2012-12-04 07:30:46 +00:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr
|
|
|
|
virPCIDeviceListFind(virPCIDeviceListPtr list, virPCIDevicePtr dev)
|
2012-12-04 07:30:46 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((i = virPCIDeviceListFindIndex(list, dev)) >= 0)
|
2012-12-04 07:30:46 +00:00
|
|
|
return list->devs[i];
|
|
|
|
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
|
|
|
{
|
|
|
|
char *pcidir = NULL;
|
|
|
|
char *file = NULL;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
if (virAsprintf(&pcidir, "/sys/bus/pci/devices/%04x:%02x:%02x.%x",
|
|
|
|
dev->domain, dev->bus, dev->slot, dev->function) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-08-14 13:20:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dir = opendir(pcidir))) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-08-14 13:20:40 +00:00
|
|
|
_("cannot open %s"), pcidir);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
|
|
|
/* Device assignment requires:
|
2011-03-17 20:26:36 +00:00
|
|
|
* $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN,
|
|
|
|
* $PCIDIR/rom, $PCIDIR/reset
|
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") ||
|
|
|
|
STREQ(ent->d_name, "reset")) {
|
2009-08-14 13:20:40 +00:00
|
|
|
if (virAsprintf(&file, "%s/%s", pcidir, ent->d_name) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-08-14 13:20:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2010-02-10 09:55:39 +00:00
|
|
|
if ((actor)(dev, file, opaque) < 0)
|
2009-08-14 13:20:40 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_FREE(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
|
|
|
VIR_FREE(file);
|
|
|
|
VIR_FREE(pcidir);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-04-25 10:34:43 +00:00
|
|
|
/* virPCIDeviceGetVFIOGroupDev - return the name of the device used to
|
|
|
|
* control this PCI device's group (e.g. "/dev/vfio/15")
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virPCIDeviceGetVFIOGroupDev(virPCIDevicePtr dev)
|
|
|
|
{
|
|
|
|
char *devPath = NULL;
|
|
|
|
char *groupPath = NULL;
|
|
|
|
char *groupDev = NULL;
|
|
|
|
|
|
|
|
if (virPCIFile(&devPath, dev->name, "iommu_group") < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (virFileIsLink(devPath) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s iommu_group file %s is not a symlink"),
|
|
|
|
dev->name, devPath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virFileResolveLink(devPath, &groupPath) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s iommu_group symlink %s"),
|
|
|
|
dev->name, devPath);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virAsprintf(&groupDev, "/dev/vfio/%s",
|
|
|
|
last_component(groupPath)) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(devPath);
|
|
|
|
VIR_FREE(groupPath);
|
|
|
|
return groupDev;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev, true)) < 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;
|
|
|
|
}
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
pos = dev->pcie_cap_pos;
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!pos || virPCIDeviceRead16(dev, fd, PCI_CLASS_DEVICE) != 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
|
|
|
}
|
|
|
|
|
2012-12-04 21:50:58 +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
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr parent;
|
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.
|
|
|
|
*/
|
|
|
|
if (dev->bus == 0)
|
|
|
|
return 0;
|
|
|
|
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 {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr tmp;
|
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) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(parent);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (acs < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = parent;
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceGetParent(parent, &parent);
|
|
|
|
virPCIDeviceFree(tmp);
|
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
|
|
|
|
2011-08-16 17:54:15 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
/*
|
2011-10-28 22:04:07 +00:00
|
|
|
* returns true if equal
|
2011-08-16 04:28:48 +00:00
|
|
|
*/
|
2011-10-28 22:04:07 +00:00
|
|
|
static bool
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressIsEqual(virPCIDeviceAddressPtr bdf1,
|
|
|
|
virPCIDeviceAddressPtr bdf2)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
|
|
|
return ((bdf1->domain == bdf2->domain) &&
|
|
|
|
(bdf1->bus == bdf2->bus) &&
|
|
|
|
(bdf1->slot == bdf2->slot) &&
|
|
|
|
(bdf1->function == bdf2->function));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
if (ret != 0) {
|
|
|
|
VIR_ERROR(_("Failed to convert '%s' to unsigned int"), s);
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Converted '%s' to unsigned int %u", s, *result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIParseDeviceAddress(char *address,
|
|
|
|
virPCIDeviceAddressPtr bdf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if ((address == NULL) || (logStrToLong_ui(address, &p, 16,
|
|
|
|
&bdf->domain) == -1)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->bus) == -1)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->slot) == -1)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->function) == -1)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetDeviceAddressFromSysfsLink(const char *device_link,
|
|
|
|
virPCIDeviceAddressPtr *bdf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
char *config_address = NULL;
|
|
|
|
char *device_path = NULL;
|
|
|
|
char errbuf[64];
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to resolve device path from device link '%s'",
|
|
|
|
device_link);
|
|
|
|
|
|
|
|
if (!virFileExists(device_link)) {
|
|
|
|
VIR_DEBUG("sysfs_path '%s' does not exist", device_link);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-17 09:23:12 +00:00
|
|
|
device_path = canonicalize_file_name(device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
if (device_path == NULL) {
|
|
|
|
memset(errbuf, '\0', sizeof(errbuf));
|
2012-11-23 08:02:07 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to resolve device link '%s'"),
|
|
|
|
device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-04-25 20:24:42 +00:00
|
|
|
config_address = last_component(device_path);
|
2011-08-16 04:28:43 +00:00
|
|
|
if (VIR_ALLOC(*bdf) != 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIParseDeviceAddress(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);
|
|
|
|
VIR_FREE(*bdf);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
VIR_DEBUG("virPCIDeviceAddress %.4x:%.2x:%.2x.%.1x",
|
2011-08-16 04:28:43 +00:00
|
|
|
(*bdf)->domain,
|
|
|
|
(*bdf)->bus,
|
|
|
|
(*bdf)->slot,
|
|
|
|
(*bdf)->function);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
VIR_FREE(device_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns Physical function given a virtual function
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path,
|
|
|
|
virPCIDeviceAddressPtr *physical_function)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *device_link = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to get SR IOV physical function for device "
|
|
|
|
"with sysfs path '%s'", vf_sysfs_path);
|
|
|
|
|
|
|
|
if (virBuildPath(&device_link, vf_sysfs_path, "physfn") == -1) {
|
|
|
|
virReportOOMError();
|
|
|
|
return ret;
|
|
|
|
} else {
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIGetDeviceAddressFromSysfsLink(device_link,
|
|
|
|
physical_function);
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(device_link);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns virtual functions of a physical function
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctions(const char *sysfs_path,
|
|
|
|
virPCIDeviceAddressPtr **virtual_functions,
|
|
|
|
unsigned int *num_virtual_functions)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2013-03-25 13:10:51 +00:00
|
|
|
int i;
|
2011-08-16 04:28:43 +00:00
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *entry = NULL;
|
|
|
|
char *device_link = NULL;
|
|
|
|
char errbuf[64];
|
|
|
|
|
|
|
|
VIR_DEBUG("Attempting to get SR IOV virtual functions for device"
|
|
|
|
"with sysfs path '%s'", sysfs_path);
|
|
|
|
|
|
|
|
dir = opendir(sysfs_path);
|
|
|
|
if (dir == NULL) {
|
|
|
|
memset(errbuf, '\0', sizeof(errbuf));
|
2012-11-23 08:02:07 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to open dir '%s'"),
|
|
|
|
sysfs_path);
|
2011-08-16 04:28:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*virtual_functions = NULL;
|
|
|
|
*num_virtual_functions = 0;
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (STRPREFIX(entry->d_name, "virtfn")) {
|
2013-03-25 13:10:51 +00:00
|
|
|
virPCIDeviceAddress *config_addr = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
|
|
|
if (virBuildPath(&device_link, sysfs_path, entry->d_name) == -1) {
|
|
|
|
virReportOOMError();
|
2013-03-25 13:10:51 +00:00
|
|
|
goto error;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("Number of virtual functions: %d",
|
|
|
|
*num_virtual_functions);
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetDeviceAddressFromSysfsLink(device_link,
|
2013-03-25 13:10:51 +00:00
|
|
|
&config_addr) !=
|
2011-08-16 04:28:43 +00:00
|
|
|
SRIOV_FOUND) {
|
2013-03-25 13:10:51 +00:00
|
|
|
VIR_WARN("Failed to get SRIOV function from device "
|
|
|
|
"link '%s'", device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
VIR_FREE(device_link);
|
2013-03-25 13:10:51 +00:00
|
|
|
continue;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
2013-03-25 13:10:51 +00:00
|
|
|
|
2013-04-09 18:06:51 +00:00
|
|
|
if (VIR_REALLOC_N(*virtual_functions,
|
|
|
|
*num_virtual_functions + 1) < 0) {
|
2013-03-25 13:10:51 +00:00
|
|
|
virReportOOMError();
|
|
|
|
VIR_FREE(config_addr);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*virtual_functions)[*num_virtual_functions] = config_addr;
|
|
|
|
(*num_virtual_functions)++;
|
2011-08-16 04:28:43 +00:00
|
|
|
VIR_FREE(device_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2013-03-25 13:10:51 +00:00
|
|
|
cleanup:
|
|
|
|
VIR_FREE(device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
if (dir)
|
|
|
|
closedir(dir);
|
|
|
|
return ret;
|
2013-03-25 13:10:51 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (*virtual_functions) {
|
|
|
|
for (i = 0; i < *num_virtual_functions; i++)
|
|
|
|
VIR_FREE((*virtual_functions)[i]);
|
|
|
|
VIR_FREE(*virtual_functions);
|
|
|
|
}
|
|
|
|
goto cleanup;
|
2011-08-16 04:28:43 +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
|
|
|
{
|
|
|
|
char *vf_sysfs_physfn_link = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (virAsprintf(&vf_sysfs_physfn_link, "%s/physfn",
|
2013-01-14 22:11:44 +00:00
|
|
|
vf_sysfs_device_link) < 0) {
|
2011-08-16 04:28:48 +00:00
|
|
|
virReportOOMError();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virFileExists(vf_sysfs_physfn_link);
|
|
|
|
|
|
|
|
VIR_FREE(vf_sysfs_physfn_link);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
{
|
|
|
|
int ret = -1, i;
|
|
|
|
unsigned int num_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
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetDeviceAddressFromSysfsLink(vf_sysfs_device_link,
|
|
|
|
&vf_bdf) < 0)
|
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,
|
|
|
|
&num_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++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceAddressIsEqual(vf_bdf, virt_fns[i])) {
|
|
|
|
*vf_index = i;
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
if (virAsprintf(pci_sysfs_device_link, PCI_SYSFS "devices/%s",
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceName) < 0) {
|
2011-12-14 10:50:01 +00:00
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:23 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr dev,
|
|
|
|
char **pci_sysfs_device_link)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
|
|
|
if (virAsprintf(pci_sysfs_device_link,
|
|
|
|
PCI_SYSFS "devices/%04x:%02x:%02x.%x", dev->domain,
|
|
|
|
dev->bus, dev->slot, dev->function) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
/*
|
|
|
|
* Returns the network device name of a pci device
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetNetName(char *device_link_sysfs_path, char **netname)
|
|
|
|
{
|
|
|
|
char *pcidev_sysfs_net_path = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *entry = NULL;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path,
|
|
|
|
"net") == -1) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = opendir(pcidev_sysfs_net_path);
|
|
|
|
if (dir == NULL)
|
|
|
|
goto out;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (STREQ(entry->d_name, ".") ||
|
|
|
|
STREQ(entry->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Assume a single directory entry */
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(*netname, entry->d_name) > 0)
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
2011-08-16 04:28:48 +00:00
|
|
|
|
|
|
|
out:
|
2013-01-14 22:11:44 +00:00
|
|
|
VIR_FREE(pcidev_sysfs_net_path);
|
2011-08-16 04:28:48 +00:00
|
|
|
|
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,
|
|
|
|
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;
|
2012-03-06 01:12:23 +00:00
|
|
|
char *pf_sysfs_device_path = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address) < 0)
|
2012-03-06 01:12:23 +00:00
|
|
|
return ret;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceAddressGetSysfsFile(pf_config_address,
|
|
|
|
&pf_sysfs_device_path) < 0) {
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
VIR_FREE(pf_config_address);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetVirtualFunctionIndex(pf_sysfs_device_path, vf_sysfs_device_path,
|
|
|
|
vf_index) < 0)
|
2012-03-06 01:12:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIGetNetName(pf_sysfs_device_path, pfname);
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(pf_config_address);
|
|
|
|
VIR_FREE(pf_sysfs_device_path);
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path ATTRIBUTE_UNUSED,
|
|
|
|
virPCIDeviceAddressPtr *physical_function ATTRIBUTE_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
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctions(const char *sysfs_path ATTRIBUTE_UNUSED,
|
|
|
|
virPCIDeviceAddressPtr **virtual_functions ATTRIBUTE_UNUSED,
|
|
|
|
unsigned int *num_virtual_functions ATTRIBUTE_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
|
2013-02-15 22:01:44 +00:00
|
|
|
virPCIIsVirtualFunction(const char *vf_sysfs_device_link ATTRIBUTE_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
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link ATTRIBUTE_UNUSED,
|
|
|
|
const char *vf_sysfs_device_link ATTRIBUTE_UNUSED,
|
|
|
|
int *vf_index ATTRIBUTE_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-08 19:19:36 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr dev ATTRIBUTE_UNUSED,
|
|
|
|
char **pci_sysfs_device_link ATTRIBUTE_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
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetNetName(char *device_link_sysfs_path ATTRIBUTE_UNUSED,
|
2011-08-16 17:54:15 +00:00
|
|
|
char **netname ATTRIBUTE_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
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path ATTRIBUTE_UNUSED,
|
|
|
|
char **pfname ATTRIBUTE_UNUSED,
|
|
|
|
int *vf_index ATTRIBUTE_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;
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
#endif /* __linux__ */
|