bhyve_device.c 6.2 KB
Newer Older
1 2 3 4 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 33 34 35 36 37 38 39 40 41
/*
 * bhyve_device.c: bhyve device management
 *
 * Copyright (C) 2014 Roman Bogorodskiy
 *
 * 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: Roman Bogorodskiy
 */

#include <config.h>

#include "bhyve_device.h"
#include "domain_addr.h"
#include "viralloc.h"
#include "virlog.h"
#include "virstring.h"

#define VIR_FROM_THIS VIR_FROM_BHYVE

VIR_LOG_INIT("bhyve.bhyve_device");

static int
bhyveCollectPCIAddress(virDomainDefPtr def ATTRIBUTE_UNUSED,
                       virDomainDeviceDefPtr device ATTRIBUTE_UNUSED,
                       virDomainDeviceInfoPtr info,
                       void *opaque)
{
    int ret = -1;
42 43 44
    if (info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_DRIVE)
        return 0;

45
    virDomainPCIAddressSetPtr addrs = opaque;
46
    virPCIDeviceAddressPtr addr = &info->addr.pci;
47 48 49 50 51 52 53 54 55 56 57 58

    if (addr->domain == 0 && addr->bus == 0) {
        if (addr->slot == 0) {
            return 0;
        } else if (addr->slot == 1) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("PCI bus 0 slot 1 is reserved for the implicit "
                             "LPC PCI-ISA bridge"));
            return -1;
        }
    }

59 60
    if (virDomainPCIAddressReserveAddr(addrs, addr,
                                       VIR_PCI_CONNECT_TYPE_PCI_DEVICE) < 0) {
61
        goto cleanup;
62
    }
63 64 65 66 67 68 69

    ret = 0;
 cleanup:
    return ret;
}

virDomainPCIAddressSetPtr
70
bhyveDomainPCIAddressSetCreate(virDomainDefPtr def, unsigned int nbuses)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
    virDomainPCIAddressSetPtr addrs;

    if ((addrs = virDomainPCIAddressSetAlloc(nbuses)) == NULL)
        return NULL;

    if (virDomainPCIAddressBusSetModel(&addrs->buses[0],
                                       VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT) < 0)
        goto error;

    if (virDomainDeviceInfoIterate(def, bhyveCollectPCIAddress, addrs) < 0)
        goto error;

    return addrs;

 error:
    virDomainPCIAddressSetFree(addrs);
    return NULL;
}

static int
bhyveAssignDevicePCISlots(virDomainDefPtr def,
                          virDomainPCIAddressSetPtr addrs)
{
    size_t i;
96
    virPCIDeviceAddress lpc_addr;
97 98 99 100 101

    /* explicitly reserve slot 1 for LPC-ISA bridge */
    memset(&lpc_addr, 0, sizeof(lpc_addr));
    lpc_addr.slot = 0x1;

102 103
    if (virDomainPCIAddressReserveAddr(addrs, &lpc_addr,
                                       VIR_PCI_CONNECT_TYPE_PCI_DEVICE) < 0) {
104
        goto error;
105
    }
106 107

    for (i = 0; i < def->ncontrollers; i++) {
108
        if ((def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) ||
109 110 111
            (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_SATA) ||
            ((def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) &&
             (def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_USB_NEC_XHCI))) {
112 113
            if (def->controllers[i]->model == VIR_DOMAIN_CONTROLLER_MODEL_PCI_ROOT ||
                !virDeviceInfoPCIAddressWanted(&def->controllers[i]->info))
114 115
                continue;

116
            if (virDomainPCIAddressReserveNextAddr(addrs,
117
                                                   &def->controllers[i]->info,
118 119
                                                   VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
                                                   -1) < 0)
120 121
                goto error;
        }
122
    }
123

124 125 126 127 128 129
    for (i = 0; i < def->nnets; i++) {
        if (!virDeviceInfoPCIAddressWanted(&def->nets[i]->info))
            continue;
        if (virDomainPCIAddressReserveNextAddr(addrs,
                                               &def->nets[i]->info,
                                               VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                                               -1) < 0)
            goto error;
    }

    for (i = 0; i < def->ndisks; i++) {
        /* We only handle virtio disk addresses as SATA disks are
         * attached to a controller and don't have their own PCI
         * addresses */
        if (def->disks[i]->bus != VIR_DOMAIN_DISK_BUS_VIRTIO)
            continue;

        if (def->disks[i]->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI &&
            !virPCIDeviceAddressIsEmpty(&def->disks[i]->info.addr.pci))
            continue;
        if (virDomainPCIAddressReserveNextAddr(addrs, &def->disks[i]->info,
                                               VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
146 147
                                               -1) < 0)
            goto error;
148 149
    }

F
Fabian Freyer 已提交
150 151 152 153 154 155 156 157 158 159 160
    for (i = 0; i < def->nvideos; i++) {
        if (!virDeviceInfoPCIAddressWanted(&def->videos[i]->info))
            continue;
        if (virDomainPCIAddressReserveNextAddr(addrs,
                                               &def->videos[i]->info,
                                               VIR_PCI_CONNECT_TYPE_PCI_DEVICE,
                                               -1) < 0)
            goto error;
    }


161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    return 0;

 error:
    return -1;
}

int bhyveDomainAssignPCIAddresses(virDomainDefPtr def,
                                  virDomainObjPtr obj)
{
    virDomainPCIAddressSetPtr addrs = NULL;
    bhyveDomainObjPrivatePtr priv = NULL;

    int ret = -1;

    if (!(addrs = bhyveDomainPCIAddressSetCreate(def, 1)))
        goto cleanup;

    if (bhyveAssignDevicePCISlots(def, addrs) < 0)
        goto cleanup;

    if (obj && obj->privateData) {
        priv = obj->privateData;
        if (addrs) {
            virDomainPCIAddressSetFree(priv->pciaddrs);
            priv->persistentAddrs = 1;
            priv->pciaddrs = addrs;
        } else {
            priv->persistentAddrs = 0;
        }
    }

    ret = 0;

 cleanup:
    return ret;
}

int bhyveDomainAssignAddresses(virDomainDefPtr def, virDomainObjPtr obj)
{
    return bhyveDomainAssignPCIAddresses(def, obj);
}