virpci.h 10.7 KB
Newer Older
1
/*
2 3
 * virpci.h: helper APIs for managing host PCI devices
 *
4
 * Copyright (C) 2009, 2011-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20
 */

21
#pragma once
22

23 24 25 26 27 28
#include "internal.h"
#include "virmdev.h"
#include "virobject.h"
#include "virutil.h"
#include "virautoclean.h"
#include "virenum.h"
29

30 31 32 33 34 35 36
typedef struct _virPCIDevice virPCIDevice;
typedef virPCIDevice *virPCIDevicePtr;
typedef struct _virPCIDeviceAddress virPCIDeviceAddress;
typedef virPCIDeviceAddress *virPCIDeviceAddressPtr;
typedef struct _virPCIDeviceList virPCIDeviceList;
typedef virPCIDeviceList *virPCIDeviceListPtr;

37 38
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_UID UINT16_MAX
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_FID UINT32_MAX
39

40 41 42 43 44 45 46
typedef struct _virZPCIDeviceAddress virZPCIDeviceAddress;
typedef virZPCIDeviceAddress *virZPCIDeviceAddressPtr;
struct _virZPCIDeviceAddress {
    unsigned int uid; /* exempt from syntax-check */
    unsigned int fid;
};

47 48
#define VIR_PCI_DEVICE_ADDRESS_FMT "%.4x:%.2x:%.2x.%.1x"

49
struct _virPCIDeviceAddress {
50 51 52 53
    unsigned int domain;
    unsigned int bus;
    unsigned int slot;
    unsigned int function;
54
    int multi; /* virTristateSwitch */
55 56
    int extFlags; /* enum virPCIDeviceAddressExtensionFlags */
    virZPCIDeviceAddress zpci;
57 58
};

59 60 61 62 63 64 65 66 67 68
typedef enum {
    VIR_PCI_STUB_DRIVER_NONE = 0,
    VIR_PCI_STUB_DRIVER_XEN,
    VIR_PCI_STUB_DRIVER_KVM,
    VIR_PCI_STUB_DRIVER_VFIO,
    VIR_PCI_STUB_DRIVER_LAST
} virPCIStubDriver;

VIR_ENUM_DECL(virPCIStubDriver);

69 70 71 72 73
typedef enum {
    VIR_PCIE_LINK_SPEED_NA = 0,
    VIR_PCIE_LINK_SPEED_25,
    VIR_PCIE_LINK_SPEED_5,
    VIR_PCIE_LINK_SPEED_8,
74
    VIR_PCIE_LINK_SPEED_16,
75 76 77
    VIR_PCIE_LINK_SPEED_LAST
} virPCIELinkSpeed;

78
VIR_ENUM_DECL(virPCIELinkSpeed);
79

80 81 82 83 84 85 86 87
typedef enum {
    VIR_PCI_HEADER_ENDPOINT = 0,
    VIR_PCI_HEADER_PCI_BRIDGE,
    VIR_PCI_HEADER_CARDBUS_BRIDGE,

    VIR_PCI_HEADER_LAST
} virPCIHeaderType;

88
VIR_ENUM_DECL(virPCIHeader);
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
typedef struct _virPCIELink virPCIELink;
typedef virPCIELink *virPCIELinkPtr;
struct _virPCIELink {
    int port;
    virPCIELinkSpeed speed;
    unsigned int width;
};

typedef struct _virPCIEDeviceInfo virPCIEDeviceInfo;
typedef virPCIEDeviceInfo *virPCIEDeviceInfoPtr;
struct _virPCIEDeviceInfo {
    /* Not all PCI Express devices have link. For example this 'Root Complex
     * Integrated Endpoint' and 'Root Complex Event Collector' don't have it. */
    virPCIELink *link_cap;   /* PCIe device link capabilities */
    virPCIELink *link_sta;   /* Actually negotiated capabilities */
};

107 108 109 110
virPCIDevicePtr virPCIDeviceNew(unsigned int domain,
                                unsigned int bus,
                                unsigned int slot,
                                unsigned int function);
L
Laine Stump 已提交
111
virPCIDevicePtr virPCIDeviceCopy(virPCIDevicePtr dev);
112 113
void virPCIDeviceFree(virPCIDevicePtr dev);
const char *virPCIDeviceGetName(virPCIDevicePtr dev);
114
const char *virPCIDeviceGetConfigPath(virPCIDevicePtr dev);
115 116 117

int virPCIDeviceDetach(virPCIDevicePtr dev,
                       virPCIDeviceListPtr activeDevs,
118
                       virPCIDeviceListPtr inactiveDevs);
119 120
int virPCIDeviceReattach(virPCIDevicePtr dev,
                         virPCIDeviceListPtr activeDevs,
121
                         virPCIDeviceListPtr inactiveDevs);
122 123 124 125 126
int virPCIDeviceReset(virPCIDevicePtr dev,
                      virPCIDeviceListPtr activeDevs,
                      virPCIDeviceListPtr inactiveDevs);

void virPCIDeviceSetManaged(virPCIDevice *dev,
127
                            bool managed);
128
bool virPCIDeviceGetManaged(virPCIDevice *dev);
129 130 131
void virPCIDeviceSetStubDriver(virPCIDevicePtr dev,
                               virPCIStubDriver driver);
virPCIStubDriver virPCIDeviceGetStubDriver(virPCIDevicePtr dev);
132
virPCIDeviceAddressPtr virPCIDeviceGetAddress(virPCIDevicePtr dev);
C
Chunyan Liu 已提交
133 134 135 136 137 138
int virPCIDeviceSetUsedBy(virPCIDevice *dev,
                          const char *drv_name,
                          const char *dom_name);
void virPCIDeviceGetUsedBy(virPCIDevice *dev,
                           const char **drv_name,
                           const char **dom_name);
139
bool virPCIDeviceGetUnbindFromStub(virPCIDevicePtr dev);
140
void  virPCIDeviceSetUnbindFromStub(virPCIDevice *dev,
141
                                    bool unbind);
142
bool virPCIDeviceGetRemoveSlot(virPCIDevicePtr dev);
143
void virPCIDeviceSetRemoveSlot(virPCIDevice *dev,
144
                               bool remove_slot);
145
bool virPCIDeviceGetReprobe(virPCIDevicePtr dev);
146
void virPCIDeviceSetReprobe(virPCIDevice *dev,
147
                            bool reprobe);
148 149 150 151 152


virPCIDeviceListPtr virPCIDeviceListNew(void);
int  virPCIDeviceListAdd(virPCIDeviceListPtr list,
                         virPCIDevicePtr dev);
L
Laine Stump 已提交
153
int virPCIDeviceListAddCopy(virPCIDeviceListPtr list, virPCIDevicePtr dev);
154 155
virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list,
                                    int idx);
156
size_t virPCIDeviceListCount(virPCIDeviceListPtr list);
157 158 159 160 161 162 163 164
virPCIDevicePtr virPCIDeviceListSteal(virPCIDeviceListPtr list,
                                      virPCIDevicePtr dev);
virPCIDevicePtr virPCIDeviceListStealIndex(virPCIDeviceListPtr list,
                                           int idx);
void virPCIDeviceListDel(virPCIDeviceListPtr list,
                         virPCIDevicePtr dev);
virPCIDevicePtr virPCIDeviceListFind(virPCIDeviceListPtr list,
                                     virPCIDevicePtr dev);
L
Laine Stump 已提交
165 166 167 168 169 170
virPCIDevicePtr
virPCIDeviceListFindByIDs(virPCIDeviceListPtr list,
                          unsigned int domain,
                          unsigned int bus,
                          unsigned int slot,
                          unsigned int function);
171 172
int virPCIDeviceListFindIndex(virPCIDeviceListPtr list,
                              virPCIDevicePtr dev);
173

174 175 176 177 178 179 180
/*
 * Callback that will be invoked once for each file
 * associated with / used for PCI host device access.
 *
 * Should return 0 if successfully processed, or
 * -1 to indicate error and abort iteration
 */
181 182 183 184 185
typedef int (*virPCIDeviceFileActor)(virPCIDevicePtr dev,
                                     const char *path, void *opaque);
int virPCIDeviceFileIterate(virPCIDevicePtr dev,
                            virPCIDeviceFileActor actor,
                            void *opaque);
L
Laine Stump 已提交
186

187
typedef int (*virPCIDeviceAddressActor)(virPCIDeviceAddressPtr addr,
L
Laine Stump 已提交
188 189 190 191 192 193 194 195
                                        void *opaque);
int virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
                                         virPCIDeviceAddressActor actor,
                                         void *opaque);
virPCIDeviceListPtr virPCIDeviceGetIOMMUGroupList(virPCIDevicePtr dev);
int virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
                                              virPCIDeviceAddressPtr **iommuGroupDevices,
                                              size_t *nIommuGroupDevices);
196
int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr);
L
Laine Stump 已提交
197
char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
198

199 200 201
int virPCIDeviceIsAssignable(virPCIDevicePtr dev,
                             int strict_acs_check);
int virPCIDeviceWaitForCleanup(virPCIDevicePtr dev, const char *matcher);
J
Jiri Denemark 已提交
202

203 204 205
virPCIDeviceAddressPtr
virPCIGetDeviceAddressFromSysfsLink(const char *device_link);

206 207
int virPCIGetPhysicalFunction(const char *vf_sysfs_path,
                              virPCIDeviceAddressPtr *pf);
208

209 210
int virPCIGetVirtualFunctions(const char *sysfs_path,
                              virPCIDeviceAddressPtr **virtual_functions,
211 212
                              size_t *num_virtual_functions,
                              unsigned int *max_virtual_functions);
213

214
int virPCIIsVirtualFunction(const char *vf_sysfs_device_link);
215

216 217 218
int virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
                                        const char *vf_sysfs_device_link,
                                        int *vf_index);
219

220
int virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
221
                                    char **pci_sysfs_device_link);
R
Roopa Prabhu 已提交
222

223 224 225 226
int virPCIGetNetName(const char *device_link_sysfs_path,
                     size_t idx,
                     char *physPortID,
                     char **netname);
227

228 229
int virPCIGetSysfsFile(char *virPCIDeviceName,
                             char **pci_sysfs_device_link)
230 231
    ATTRIBUTE_RETURN_CHECK;

232 233 234 235 236 237 238
bool virPCIDeviceAddressIsValid(virPCIDeviceAddressPtr addr,
                                bool report);
bool virPCIDeviceAddressIsEmpty(const virPCIDeviceAddress *addr);

bool virPCIDeviceAddressEqual(virPCIDeviceAddress *addr1,
                              virPCIDeviceAddress *addr2);

239
char *virPCIDeviceAddressAsString(virPCIDeviceAddressPtr addr)
240 241
      ATTRIBUTE_NONNULL(1);

242 243
int virPCIDeviceAddressParse(char *address, virPCIDeviceAddressPtr bdf);

244 245 246
bool virZPCIDeviceAddressIsValid(virZPCIDeviceAddressPtr zpci);
bool virZPCIDeviceAddressIsEmpty(const virZPCIDeviceAddress *addr);

247
int virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
248 249 250
                                 int pfNetDevIdx,
                                 char **pfname,
                                 int *vf_index);
R
Roopa Prabhu 已提交
251

252
int virPCIDeviceUnbind(virPCIDevicePtr dev);
253
int virPCIDeviceRebind(virPCIDevicePtr dev);
254 255 256 257
int virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev,
                                     char **path,
                                     char **name);

258 259 260 261 262 263 264 265
int virPCIDeviceIsPCIExpress(virPCIDevicePtr dev);
int virPCIDeviceHasPCIExpressLink(virPCIDevicePtr dev);
int virPCIDeviceGetLinkCapSta(virPCIDevicePtr dev,
                              int *ca_port,
                              unsigned int *cap_speed,
                              unsigned int *cap_width,
                              unsigned int *sta_speed,
                              unsigned int *sta_width);
266

267 268
int virPCIGetHeaderType(virPCIDevicePtr dev, int *hdrType);

269 270
void virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev);

271 272 273
ssize_t virPCIGetMdevTypes(const char *sysfspath,
                           virMediatedDeviceType ***types);

274 275
void virPCIDeviceAddressFree(virPCIDeviceAddressPtr address);

276 277 278
VIR_DEFINE_AUTOPTR_FUNC(virPCIDevice, virPCIDeviceFree);
VIR_DEFINE_AUTOPTR_FUNC(virPCIDeviceAddress, virPCIDeviceAddressFree);
VIR_DEFINE_AUTOPTR_FUNC(virPCIEDeviceInfo, virPCIEDeviceInfoFree);