domain_addr.h 12.8 KB
Newer Older
1 2 3
/*
 * domain_addr.h: helper APIs for managing domain device addresses
 *
4
 * Copyright (C) 2006-2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#ifndef __DOMAIN_ADDR_H__
# define __DOMAIN_ADDR_H__

# include "domain_conf.h"

# define VIR_PCI_ADDRESS_SLOT_LAST 31
# define VIR_PCI_ADDRESS_FUNCTION_LAST 7

typedef enum {
33
   VIR_PCI_CONNECT_HOTPLUGGABLE = 1 << 0, /* is hotplug needed/supported */
34

35 36 37 38 39
   /* set for devices that can share a single slot in auto-assignment
    * (by assigning one device to each of the 8 functions on the slot)
    */
   VIR_PCI_CONNECT_AGGREGATE_SLOT = 1 << 1,

40 41
   /* kinds of devices as a bitmap so they can be combined (some PCI
    * controllers permit connecting multiple types of devices)
42
    */
43 44 45 46 47 48 49 50 51
   VIR_PCI_CONNECT_TYPE_PCI_DEVICE = 1 << 2,
   VIR_PCI_CONNECT_TYPE_PCIE_DEVICE = 1 << 3,
   VIR_PCI_CONNECT_TYPE_PCIE_ROOT_PORT = 1 << 4,
   VIR_PCI_CONNECT_TYPE_PCIE_SWITCH_UPSTREAM_PORT = 1 << 5,
   VIR_PCI_CONNECT_TYPE_PCIE_SWITCH_DOWNSTREAM_PORT = 1 << 6,
   VIR_PCI_CONNECT_TYPE_DMI_TO_PCI_BRIDGE = 1 << 7,
   VIR_PCI_CONNECT_TYPE_PCI_EXPANDER_BUS = 1 << 8,
   VIR_PCI_CONNECT_TYPE_PCIE_EXPANDER_BUS = 1 << 9,
   VIR_PCI_CONNECT_TYPE_PCI_BRIDGE = 1 << 10,
52
   VIR_PCI_CONNECT_TYPE_PCIE_TO_PCI_BRIDGE = 1 << 11,
53 54
} virDomainPCIConnectFlags;

55 56 57 58 59 60 61
/* a combination of all bits that describe the type of connections
 * allowed, e.g. PCI, PCIe, switch
 */
# define VIR_PCI_CONNECT_TYPES_MASK \
   (VIR_PCI_CONNECT_TYPE_PCI_DEVICE | VIR_PCI_CONNECT_TYPE_PCIE_DEVICE | \
    VIR_PCI_CONNECT_TYPE_PCIE_SWITCH_UPSTREAM_PORT | \
    VIR_PCI_CONNECT_TYPE_PCIE_SWITCH_DOWNSTREAM_PORT | \
62
    VIR_PCI_CONNECT_TYPE_PCIE_ROOT_PORT | \
63 64
    VIR_PCI_CONNECT_TYPE_DMI_TO_PCI_BRIDGE | \
    VIR_PCI_CONNECT_TYPE_PCI_EXPANDER_BUS | \
65
    VIR_PCI_CONNECT_TYPE_PCIE_EXPANDER_BUS | \
66 67
    VIR_PCI_CONNECT_TYPE_PCI_BRIDGE | \
    VIR_PCI_CONNECT_TYPE_PCIE_TO_PCI_BRIDGE)
68 69 70 71 72 73 74 75 76

/* combination of all bits that could be used to connect a normal
 * endpoint device (i.e. excluding the connection possible between an
 * upstream and downstream switch port, or a PCIe root port and a PCIe
 * port)
 */
# define VIR_PCI_CONNECT_TYPES_ENDPOINT \
   (VIR_PCI_CONNECT_TYPE_PCI_DEVICE | VIR_PCI_CONNECT_TYPE_PCIE_DEVICE)

77 78 79
virDomainPCIConnectFlags
virDomainPCIControllerModelToConnectType(virDomainControllerModelPCI model);

80 81 82 83 84
typedef struct {
    /* each function is represented by one bit, set if that function is
     * in use by a device, or clear if it isn't.
     */
    uint8_t functions;
85 86 87 88 89 90 91

    /* aggregate is true if this slot has only devices with
     * VIR_PCI_CONNECT_AGGREGATE assigned to its functions (meaning
     * that other devices with the same flags could also be
     * auto-assigned to the other functions)
     */
    bool aggregate;
92 93
} virDomainPCIAddressSlot;

94 95
typedef struct {
    virDomainControllerModelPCI model;
96
    /* flags and min/max can be computed from model, but
97 98 99
     * having them ready makes life easier.
     */
    virDomainPCIConnectFlags flags;
100
    size_t minSlot, maxSlot; /* usually 0,0 or 0,31, or 1,31 */
101 102 103
    /* Each bit in a slot represents one function on that slot. If the
     * bit is set, that function is in use by a device.
     */
104
    virDomainPCIAddressSlot slot[VIR_PCI_ADDRESS_SLOT_LAST + 1];
105 106 107 108 109 110

    /* See virDomainDeviceInfo::isolationGroup */
    unsigned int isolationGroup;

    /* See virDomainDeviceInfo::isolationGroupLocked */
    bool isolationGroupLocked;
111 112 113 114 115 116 117 118
} virDomainPCIAddressBus;
typedef virDomainPCIAddressBus *virDomainPCIAddressBusPtr;

struct _virDomainPCIAddressSet {
    virDomainPCIAddressBus *buses;
    size_t nbuses;
    bool dryRun;          /* on a dry run, new buses are auto-added
                             and addresses aren't saved in device infos */
119
    /* If true, the guest can have multiple pci-root controllers */
120
    bool areMultipleRootsSupported;
121 122
    /* If true, the guest can use the pcie-to-pci-bridge controller */
    bool isPCIeToPCIBridgeSupported;
123 124 125 126
};
typedef struct _virDomainPCIAddressSet virDomainPCIAddressSet;
typedef virDomainPCIAddressSet *virDomainPCIAddressSetPtr;

127
char *virDomainPCIAddressAsString(virPCIDeviceAddressPtr addr)
128 129 130 131 132 133 134
      ATTRIBUTE_NONNULL(1);

virDomainPCIAddressSetPtr virDomainPCIAddressSetAlloc(unsigned int nbuses);

void virDomainPCIAddressSetFree(virDomainPCIAddressSetPtr addrs);

bool virDomainPCIAddressValidate(virDomainPCIAddressSetPtr addrs,
135
                                 virPCIDeviceAddressPtr addr,
136 137 138 139 140 141 142 143 144 145
                                 const char *addrStr,
                                 virDomainPCIConnectFlags flags,
                                 bool fromConfig)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);


int virDomainPCIAddressBusSetModel(virDomainPCIAddressBusPtr bus,
                                   virDomainControllerModelPCI model)
    ATTRIBUTE_NONNULL(1);

146 147 148
bool virDomainPCIAddressBusIsFullyReserved(virDomainPCIAddressBusPtr bus)
    ATTRIBUTE_NONNULL(1);

149 150 151
bool virDomainPCIAddressBusIsEmpty(virDomainPCIAddressBusPtr bus)
    ATTRIBUTE_NONNULL(1);

152
bool virDomainPCIAddressSlotInUse(virDomainPCIAddressSetPtr addrs,
153
                                  virPCIDeviceAddressPtr addr)
154 155
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

156
int virDomainPCIAddressReserveAddr(virDomainPCIAddressSetPtr addrs,
157
                                   virPCIDeviceAddressPtr addr,
158 159
                                   virDomainPCIConnectFlags flags,
                                   unsigned int isolationGroup)
160 161
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

162 163 164 165 166 167
int virDomainPCIAddressReserveNextAddr(virDomainPCIAddressSetPtr addrs,
                                       virDomainDeviceInfoPtr dev,
                                       virDomainPCIConnectFlags flags,
                                       int function)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

168
int virDomainPCIAddressEnsureAddr(virDomainPCIAddressSetPtr addrs,
169 170
                                  virDomainDeviceInfoPtr dev,
                                  virDomainPCIConnectFlags flags)
171 172
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

173 174
void virDomainPCIAddressReleaseAddr(virDomainPCIAddressSetPtr addrs,
                                    virPCIDeviceAddressPtr addr)
175 176
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

177 178 179
void virDomainPCIAddressSetAllMulti(virDomainDefPtr def)
    ATTRIBUTE_NONNULL(1);

J
Ján Tomko 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
struct _virDomainCCWAddressSet {
    virHashTablePtr defined;
    virDomainDeviceCCWAddress next;
};
typedef struct _virDomainCCWAddressSet virDomainCCWAddressSet;
typedef virDomainCCWAddressSet *virDomainCCWAddressSetPtr;

int virDomainCCWAddressAssign(virDomainDeviceInfoPtr dev,
                              virDomainCCWAddressSetPtr addrs,
                              bool autoassign)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
void virDomainCCWAddressSetFree(virDomainCCWAddressSetPtr addrs);
int virDomainCCWAddressAllocate(virDomainDefPtr def,
                                virDomainDeviceDefPtr dev,
                                virDomainDeviceInfoPtr info,
                                void *data)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);
int virDomainCCWAddressValidate(virDomainDefPtr def,
                                virDomainDeviceDefPtr dev,
                                virDomainDeviceInfoPtr info,
                                void *data)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);

int virDomainCCWAddressReleaseAddr(virDomainCCWAddressSetPtr addrs,
                                   virDomainDeviceInfoPtr dev)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
virDomainCCWAddressSetPtr virDomainCCWAddressSetCreate(void);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

struct _virDomainVirtioSerialController {
    unsigned int idx;
    virBitmapPtr ports;
};

typedef struct _virDomainVirtioSerialController virDomainVirtioSerialController;
typedef virDomainVirtioSerialController *virDomainVirtioSerialControllerPtr;

struct _virDomainVirtioSerialAddrSet {
    virDomainVirtioSerialControllerPtr *controllers;
    size_t ncontrollers;
};
typedef struct _virDomainVirtioSerialAddrSet virDomainVirtioSerialAddrSet;
typedef virDomainVirtioSerialAddrSet *virDomainVirtioSerialAddrSetPtr;

virDomainVirtioSerialAddrSetPtr
virDomainVirtioSerialAddrSetCreate(void);
int
virDomainVirtioSerialAddrSetAddControllers(virDomainVirtioSerialAddrSetPtr addrs,
                                           virDomainDefPtr def)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
void
virDomainVirtioSerialAddrSetFree(virDomainVirtioSerialAddrSetPtr addrs);
231 232 233
virDomainVirtioSerialAddrSetPtr
virDomainVirtioSerialAddrSetCreateFromDomain(virDomainDefPtr def)
    ATTRIBUTE_NONNULL(1);
234 235 236
bool
virDomainVirtioSerialAddrIsComplete(virDomainDeviceInfoPtr info);
int
237 238 239 240
virDomainVirtioSerialAddrAutoAssignFromCache(virDomainDefPtr def,
                                             virDomainVirtioSerialAddrSetPtr addrs,
                                             virDomainDeviceInfoPtr info,
                                             bool allowZero)
J
Ján Tomko 已提交
241
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
242

243 244 245 246 247 248
int
virDomainVirtioSerialAddrAutoAssign(virDomainDefPtr def,
                                    virDomainDeviceInfoPtr info,
                                    bool allowZero)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

249
int
J
Ján Tomko 已提交
250 251
virDomainVirtioSerialAddrAssign(virDomainDefPtr def,
                                virDomainVirtioSerialAddrSetPtr addrs,
252 253 254
                                virDomainDeviceInfoPtr info,
                                bool allowZero,
                                bool portOnly)
J
Ján Tomko 已提交
255
    ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
256 257 258 259 260 261 262 263 264 265 266 267 268

int
virDomainVirtioSerialAddrReserve(virDomainDefPtr def,
                                 virDomainDeviceDefPtr dev,
                                 virDomainDeviceInfoPtr info,
                                 void *data)
    ATTRIBUTE_NONNULL(3) ATTRIBUTE_NONNULL(4);

int
virDomainVirtioSerialAddrRelease(virDomainVirtioSerialAddrSetPtr addrs,
                                 virDomainDeviceInfoPtr info)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

269 270 271 272 273 274 275 276 277 278 279 280
bool
virDomainUSBAddressPortIsValid(unsigned int *port)
    ATTRIBUTE_NONNULL(1);

void
virDomainUSBAddressPortFormatBuf(virBufferPtr buf,
                                 unsigned int *port)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
char *
virDomainUSBAddressPortFormat(unsigned int *port)
    ATTRIBUTE_NONNULL(1);

281 282
# define VIR_DOMAIN_USB_HUB_PORTS 8

J
Ján Tomko 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
typedef struct _virDomainUSBAddressHub virDomainUSBAddressHub;
typedef virDomainUSBAddressHub *virDomainUSBAddressHubPtr;
struct _virDomainUSBAddressHub {
    /* indexes are shifted by one:
     * ports[0] represents port 1, because ports are numbered from 1 */
    virBitmapPtr portmap;
    size_t nports;
    virDomainUSBAddressHubPtr *ports;
};

struct _virDomainUSBAddressSet {
    /* every <controller type='usb' index='i'> is represented
     * as a hub at buses[i] */
    virDomainUSBAddressHubPtr *buses;
    size_t nbuses;
};
typedef struct _virDomainUSBAddressSet virDomainUSBAddressSet;
typedef virDomainUSBAddressSet *virDomainUSBAddressSetPtr;

virDomainUSBAddressSetPtr virDomainUSBAddressSetCreate(void);
303 304 305 306

int virDomainUSBAddressSetAddControllers(virDomainUSBAddressSetPtr addrs,
                                         virDomainDefPtr def)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
J
Ján Tomko 已提交
307 308 309 310
int
virDomainUSBAddressSetAddHub(virDomainUSBAddressSetPtr addrs,
                             virDomainHubDefPtr hub)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
311 312
size_t
virDomainUSBAddressCountAllPorts(virDomainDefPtr def);
J
Ján Tomko 已提交
313 314
void virDomainUSBAddressSetFree(virDomainUSBAddressSetPtr addrs);

315 316 317 318
int
virDomainUSBAddressPresent(virDomainDeviceInfoPtr info,
                           void *data)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
J
Ján Tomko 已提交
319 320 321 322
int
virDomainUSBAddressReserve(virDomainDeviceInfoPtr info,
                           void *data)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
J
Ján Tomko 已提交
323 324 325 326 327 328 329 330 331

int
virDomainUSBAddressAssign(virDomainUSBAddressSetPtr addrs,
                          virDomainDeviceInfoPtr info)
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

int
virDomainUSBAddressEnsure(virDomainUSBAddressSetPtr addrs,
                          virDomainDeviceInfoPtr info)
332
    ATTRIBUTE_NONNULL(2);
333 334 335 336

int
virDomainUSBAddressRelease(virDomainUSBAddressSetPtr addrs,
                           virDomainDeviceInfoPtr info)
337
    ATTRIBUTE_NONNULL(2);
338
#endif /* __DOMAIN_ADDR_H__ */