virpci.h 11.1 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
#include "internal.h"
#include "virmdev.h"
#include "virobject.h"
#include "virenum.h"
27

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

35 36 37
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDeviceList, virObjectUnref);


38 39
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_UID UINT16_MAX
#define VIR_DOMAIN_DEVICE_ZPCI_MAX_FID UINT32_MAX
40

41
typedef struct _virZPCIDeviceAddressID virZPCIDeviceAddressID;
42 43
typedef struct _virZPCIDeviceAddress virZPCIDeviceAddress;
typedef virZPCIDeviceAddress *virZPCIDeviceAddressPtr;
44 45 46 47 48 49

struct _virZPCIDeviceAddressID {
    unsigned int value;
    bool isSet;
};

50
struct _virZPCIDeviceAddress {
51 52
    virZPCIDeviceAddressID uid; /* exempt from syntax-check */
    virZPCIDeviceAddressID fid;
53
    /* Don't forget to update virPCIDeviceAddressCopy if needed. */
54 55
};

56
#define VIR_PCI_DEVICE_ADDRESS_FMT "%04x:%02x:%02x.%d"
57

58
struct _virPCIDeviceAddress {
59 60 61 62
    unsigned int domain;
    unsigned int bus;
    unsigned int slot;
    unsigned int function;
63
    int multi; /* virTristateSwitch */
64 65
    int extFlags; /* enum virPCIDeviceAddressExtensionFlags */
    virZPCIDeviceAddress zpci;
66
    /* Don't forget to update virPCIDeviceAddressCopy if needed. */
67 68
};

69 70 71 72 73 74 75 76 77
typedef enum {
    VIR_PCI_STUB_DRIVER_NONE = 0,
    VIR_PCI_STUB_DRIVER_XEN,
    VIR_PCI_STUB_DRIVER_VFIO,
    VIR_PCI_STUB_DRIVER_LAST
} virPCIStubDriver;

VIR_ENUM_DECL(virPCIStubDriver);

78 79 80 81 82
typedef enum {
    VIR_PCIE_LINK_SPEED_NA = 0,
    VIR_PCIE_LINK_SPEED_25,
    VIR_PCIE_LINK_SPEED_5,
    VIR_PCIE_LINK_SPEED_8,
83
    VIR_PCIE_LINK_SPEED_16,
84 85 86
    VIR_PCIE_LINK_SPEED_LAST
} virPCIELinkSpeed;

87
VIR_ENUM_DECL(virPCIELinkSpeed);
88

89 90 91 92 93 94 95 96
typedef enum {
    VIR_PCI_HEADER_ENDPOINT = 0,
    VIR_PCI_HEADER_PCI_BRIDGE,
    VIR_PCI_HEADER_CARDBUS_BRIDGE,

    VIR_PCI_HEADER_LAST
} virPCIHeaderType;

97
VIR_ENUM_DECL(virPCIHeader);
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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 */
};

116 117 118 119
virPCIDevicePtr virPCIDeviceNew(unsigned int domain,
                                unsigned int bus,
                                unsigned int slot,
                                unsigned int function);
L
Laine Stump 已提交
120
virPCIDevicePtr virPCIDeviceCopy(virPCIDevicePtr dev);
121 122
void virPCIDeviceFree(virPCIDevicePtr dev);
const char *virPCIDeviceGetName(virPCIDevicePtr dev);
123
const char *virPCIDeviceGetConfigPath(virPCIDevicePtr dev);
124 125 126

int virPCIDeviceDetach(virPCIDevicePtr dev,
                       virPCIDeviceListPtr activeDevs,
127
                       virPCIDeviceListPtr inactiveDevs);
128 129
int virPCIDeviceReattach(virPCIDevicePtr dev,
                         virPCIDeviceListPtr activeDevs,
130
                         virPCIDeviceListPtr inactiveDevs);
131 132 133 134 135
int virPCIDeviceReset(virPCIDevicePtr dev,
                      virPCIDeviceListPtr activeDevs,
                      virPCIDeviceListPtr inactiveDevs);

void virPCIDeviceSetManaged(virPCIDevice *dev,
136
                            bool managed);
137
bool virPCIDeviceGetManaged(virPCIDevice *dev);
138 139 140
void virPCIDeviceSetStubDriver(virPCIDevicePtr dev,
                               virPCIStubDriver driver);
virPCIStubDriver virPCIDeviceGetStubDriver(virPCIDevicePtr dev);
141
virPCIDeviceAddressPtr virPCIDeviceGetAddress(virPCIDevicePtr dev);
C
Chunyan Liu 已提交
142 143 144 145 146 147
int virPCIDeviceSetUsedBy(virPCIDevice *dev,
                          const char *drv_name,
                          const char *dom_name);
void virPCIDeviceGetUsedBy(virPCIDevice *dev,
                           const char **drv_name,
                           const char **dom_name);
148
bool virPCIDeviceGetUnbindFromStub(virPCIDevicePtr dev);
149
void  virPCIDeviceSetUnbindFromStub(virPCIDevice *dev,
150
                                    bool unbind);
151
bool virPCIDeviceGetRemoveSlot(virPCIDevicePtr dev);
152
void virPCIDeviceSetRemoveSlot(virPCIDevice *dev,
153
                               bool remove_slot);
154
bool virPCIDeviceGetReprobe(virPCIDevicePtr dev);
155
void virPCIDeviceSetReprobe(virPCIDevice *dev,
156
                            bool reprobe);
157 158 159 160 161


virPCIDeviceListPtr virPCIDeviceListNew(void);
int  virPCIDeviceListAdd(virPCIDeviceListPtr list,
                         virPCIDevicePtr dev);
L
Laine Stump 已提交
162
int virPCIDeviceListAddCopy(virPCIDeviceListPtr list, virPCIDevicePtr dev);
163 164
virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list,
                                    int idx);
165
size_t virPCIDeviceListCount(virPCIDeviceListPtr list);
166 167 168 169 170 171 172 173
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 已提交
174 175 176 177 178 179
virPCIDevicePtr
virPCIDeviceListFindByIDs(virPCIDeviceListPtr list,
                          unsigned int domain,
                          unsigned int bus,
                          unsigned int slot,
                          unsigned int function);
180 181
int virPCIDeviceListFindIndex(virPCIDeviceListPtr list,
                              virPCIDevicePtr dev);
182

183 184 185 186 187 188 189
/*
 * 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
 */
190 191 192 193 194
typedef int (*virPCIDeviceFileActor)(virPCIDevicePtr dev,
                                     const char *path, void *opaque);
int virPCIDeviceFileIterate(virPCIDevicePtr dev,
                            virPCIDeviceFileActor actor,
                            void *opaque);
L
Laine Stump 已提交
195

196
typedef int (*virPCIDeviceAddressActor)(virPCIDeviceAddressPtr addr,
L
Laine Stump 已提交
197 198 199 200 201 202 203 204
                                        void *opaque);
int virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
                                         virPCIDeviceAddressActor actor,
                                         void *opaque);
virPCIDeviceListPtr virPCIDeviceGetIOMMUGroupList(virPCIDevicePtr dev);
int virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
                                              virPCIDeviceAddressPtr **iommuGroupDevices,
                                              size_t *nIommuGroupDevices);
205
int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr);
206
char *virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr);
L
Laine Stump 已提交
207
char *virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev);
208

209 210
int virPCIDeviceIsAssignable(virPCIDevicePtr dev,
                             int strict_acs_check);
J
Jiri Denemark 已提交
211

212 213 214
virPCIDeviceAddressPtr
virPCIGetDeviceAddressFromSysfsLink(const char *device_link);

215 216
int virPCIGetPhysicalFunction(const char *vf_sysfs_path,
                              virPCIDeviceAddressPtr *pf);
217

218 219
int virPCIGetVirtualFunctions(const char *sysfs_path,
                              virPCIDeviceAddressPtr **virtual_functions,
220 221
                              size_t *num_virtual_functions,
                              unsigned int *max_virtual_functions);
222

223
int virPCIIsVirtualFunction(const char *vf_sysfs_device_link);
224

225 226 227
int virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
                                        const char *vf_sysfs_device_link,
                                        int *vf_index);
228

229
int virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
230
                                    char **pci_sysfs_device_link);
R
Roopa Prabhu 已提交
231

232 233 234 235
int virPCIGetNetName(const char *device_link_sysfs_path,
                     size_t idx,
                     char *physPortID,
                     char **netname);
236

237 238
int virPCIGetSysfsFile(char *virPCIDeviceName,
                             char **pci_sysfs_device_link)
239
    G_GNUC_WARN_UNUSED_RESULT;
240

241 242 243 244
bool virPCIDeviceAddressIsValid(virPCIDeviceAddressPtr addr,
                                bool report);
bool virPCIDeviceAddressIsEmpty(const virPCIDeviceAddress *addr);

245 246
bool virPCIDeviceAddressEqual(const virPCIDeviceAddress *addr1,
                              const virPCIDeviceAddress *addr2);
247 248
void virPCIDeviceAddressCopy(virPCIDeviceAddressPtr dst,
                             const virPCIDeviceAddress *src);
249

250
char *virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
251 252
      ATTRIBUTE_NONNULL(1);

253 254
int virPCIDeviceAddressParse(char *address, virPCIDeviceAddressPtr bdf);

255 256
bool virZPCIDeviceAddressIsIncomplete(const virZPCIDeviceAddress *addr);
bool virZPCIDeviceAddressIsPresent(const virZPCIDeviceAddress *addr);
257

258
int virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
259 260 261
                                 int pfNetDevIdx,
                                 char **pfname,
                                 int *vf_index);
R
Roopa Prabhu 已提交
262

263
int virPCIDeviceUnbind(virPCIDevicePtr dev);
264
int virPCIDeviceRebind(virPCIDevicePtr dev);
265 266 267 268
int virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev,
                                     char **path,
                                     char **name);

269 270 271 272 273 274 275 276
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);
277

278 279
int virPCIGetHeaderType(virPCIDevicePtr dev, int *hdrType);

280 281
void virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev);

282 283 284
ssize_t virPCIGetMdevTypes(const char *sysfspath,
                           virMediatedDeviceType ***types);

285 286
void virPCIDeviceAddressFree(virPCIDeviceAddressPtr address);

287 288 289
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDevice, virPCIDeviceFree);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDeviceAddress, virPCIDeviceAddressFree);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIEDeviceInfo, virPCIEDeviceInfoFree);