node_device_linux_sysfs.c 7.6 KB
Newer Older
1
/*
O
Osier Yang 已提交
2
 * node_device_linux_sysfs.c: Linux specific code to gather device data
3
 * that is available from sysfs (but not from UDEV or HAL).
4
 *
5
 * Copyright (C) 2009-2015 Red Hat, Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 *
 */

#include <config.h>

#include <fcntl.h>
26
#include <sys/stat.h>
27
#include <stdlib.h>
28

29
#include "node_device_driver.h"
30
#include "node_device_hal.h"
31
#include "node_device_linux_sysfs.h"
32
#include "virerror.h"
33
#include "viralloc.h"
34
#include "virlog.h"
E
Eric Blake 已提交
35
#include "virfile.h"
36
#include "virstring.h"
37 38 39 40 41

#define VIR_FROM_THIS VIR_FROM_NODEDEV

#ifdef __linux__

42 43
VIR_LOG_INIT("node_device.node_device_linux_sysfs");

O
Osier Yang 已提交
44
int
45
nodeDeviceSysfsGetSCSIHostCaps(virNodeDevCapDataPtr d)
46
{
47 48
    char *max_vports = NULL;
    char *vports = NULL;
O
Osier Yang 已提交
49
    int ret = -1;
50

J
John Ferlan 已提交
51 52 53 54 55 56
    if (virReadSCSIUniqueId(NULL, d->scsi_host.host,
                            &d->scsi_host.unique_id) < 0) {
        VIR_DEBUG("Failed to read unique_id for host%d", d->scsi_host.host);
        d->scsi_host.unique_id = -1;
    }

57
    VIR_DEBUG("Checking if host%d is an FC HBA", d->scsi_host.host);
58

59
    if (virIsCapableFCHost(NULL, d->scsi_host.host)) {
O
Osier Yang 已提交
60 61 62 63 64
        d->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST;

        if (virReadFCHost(NULL,
                          d->scsi_host.host,
                          "port_name",
65
                          &d->scsi_host.wwpn) < 0) {
66
            VIR_WARN("Failed to read WWPN for host%d", d->scsi_host.host);
O
Osier Yang 已提交
67 68 69 70 71 72
            goto cleanup;
        }

        if (virReadFCHost(NULL,
                          d->scsi_host.host,
                          "node_name",
73
                          &d->scsi_host.wwnn) < 0) {
74
            VIR_WARN("Failed to read WWNN for host%d", d->scsi_host.host);
O
Osier Yang 已提交
75 76 77 78 79 80
            goto cleanup;
        }

        if (virReadFCHost(NULL,
                          d->scsi_host.host,
                          "fabric_name",
81
                          &d->scsi_host.fabric_wwn) < 0) {
82 83
            VIR_WARN("Failed to read fabric WWN for host%d",
                     d->scsi_host.host);
O
Osier Yang 已提交
84 85
            goto cleanup;
        }
86 87
    }

88
    if (virIsCapableVport(NULL, d->scsi_host.host)) {
O
Osier Yang 已提交
89
        d->scsi_host.flags |= VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS;
O
Osier Yang 已提交
90

91
        if (virReadFCHost(NULL,
92
                          d->scsi_host.host,
93 94
                          "max_npiv_vports",
                          &max_vports) < 0) {
95 96
            VIR_WARN("Failed to read max_npiv_vports for host%d",
                     d->scsi_host.host);
97 98 99 100
            goto cleanup;
        }

         if (virReadFCHost(NULL,
101
                          d->scsi_host.host,
102 103
                          "npiv_vports_inuse",
                          &vports) < 0) {
104 105
            VIR_WARN("Failed to read npiv_vports_inuse for host%d",
                     d->scsi_host.host);
106 107 108 109 110
            goto cleanup;
        }

        if (virStrToLong_i(max_vports, NULL, 10,
                           &d->scsi_host.max_vports) < 0) {
111
            VIR_WARN("Failed to parse value of max_npiv_vports '%s'",
112 113 114 115 116 117
                      max_vports);
            goto cleanup;
        }

        if (virStrToLong_i(vports, NULL, 10,
                           &d->scsi_host.vports) < 0) {
118 119
            VIR_WARN("Failed to parse value of npiv_vports_inuse '%s'",
                     vports);
120 121 122 123
            goto cleanup;
        }
    }

O
Osier Yang 已提交
124
    ret = 0;
125
 cleanup:
O
Osier Yang 已提交
126
    if (ret < 0) {
127 128 129 130
        /* Clear the two flags in case of producing confusing XML output */
        d->scsi_host.flags &= ~(VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST |
                                VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS);

131 132
        VIR_FREE(d->scsi_host.wwnn);
        VIR_FREE(d->scsi_host.wwpn);
O
Osier Yang 已提交
133
        VIR_FREE(d->scsi_host.fabric_wwn);
134
    }
135 136
    VIR_FREE(max_vports);
    VIR_FREE(vports);
O
Osier Yang 已提交
137
    return ret;
138 139
}

140 141 142 143 144 145 146 147 148 149 150 151 152

static int
nodeDeviceSysfsGetPCISRIOVCaps(const char *sysfsPath,
                               virNodeDevCapDataPtr data)
{
    size_t i;
    int ret;

    /* this could be a refresh, so clear out the old data */
    for (i = 0; i < data->pci_dev.num_virtual_functions; i++)
       VIR_FREE(data->pci_dev.virtual_functions[i]);
    VIR_FREE(data->pci_dev.virtual_functions);
    data->pci_dev.num_virtual_functions = 0;
153
    data->pci_dev.max_virtual_functions = 0;
154 155 156 157 158 159 160
    data->pci_dev.flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;
    data->pci_dev.flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;

    if (!virPCIGetPhysicalFunction(sysfsPath, &data->pci_dev.physical_function))
        data->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION;

    ret = virPCIGetVirtualFunctions(sysfsPath, &data->pci_dev.virtual_functions,
161 162
                                    &data->pci_dev.num_virtual_functions,
                                    &data->pci_dev.max_virtual_functions);
163 164 165
    if (ret < 0)
        return ret;

166 167
    if (data->pci_dev.num_virtual_functions > 0 ||
        data->pci_dev.max_virtual_functions > 0)
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        data->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;

    return ret;
}


static int
nodeDeviceSysfsGetPCIIOMMUGroupCaps(virNodeDevCapDataPtr data)
{
    size_t i;
    int tmpGroup, ret = -1;
    virPCIDeviceAddress addr;

    /* this could be a refresh, so clear out the old data */
    for (i = 0; i < data->pci_dev.nIommuGroupDevices; i++)
       VIR_FREE(data->pci_dev.iommuGroupDevices[i]);
    VIR_FREE(data->pci_dev.iommuGroupDevices);
    data->pci_dev.nIommuGroupDevices = 0;
    data->pci_dev.iommuGroupNumber = 0;

    addr.domain = data->pci_dev.domain;
    addr.bus = data->pci_dev.bus;
    addr.slot = data->pci_dev.slot;
    addr.function = data->pci_dev.function;
    tmpGroup = virPCIDeviceAddressGetIOMMUGroupNum(&addr);
    if (tmpGroup == -1) {
        /* error was already reported */
        goto cleanup;
    }
    if (tmpGroup == -2) {
        /* -2 return means there is no iommu_group data */
        ret = 0;
        goto cleanup;
    }
    if (tmpGroup >= 0) {
        if (virPCIDeviceAddressGetIOMMUGroupAddresses(&addr, &data->pci_dev.iommuGroupDevices,
                                                      &data->pci_dev.nIommuGroupDevices) < 0)
            goto cleanup;
        data->pci_dev.iommuGroupNumber = tmpGroup;
    }

    ret = 0;
 cleanup:
    return ret;
}


/* nodeDeviceSysfsGetPCIRelatedCaps() get info that is stored in sysfs
 * about devices related to this device, i.e. things that can change
 * without this device itself changing. These must be refreshed
 * anytime full XML of the device is requested, because they can
 * change with no corresponding notification from the kernel/udev.
 */
int
nodeDeviceSysfsGetPCIRelatedDevCaps(const char *sysfsPath,
                                    virNodeDevCapDataPtr data)
{
    if (nodeDeviceSysfsGetPCISRIOVCaps(sysfsPath, data) < 0)
        return -1;
    if (nodeDeviceSysfsGetPCIIOMMUGroupCaps(data) < 0)
        return -1;
    return 0;
}


233 234 235
#else

int
236
nodeDeviceSysfsGetSCSIHostCaps(virNodeDevCapDataPtr d ATTRIBUTE_UNUSED)
237 238 239 240
{
    return -1;
}

241 242 243 244 245 246 247
int
nodeDeviceSysfsGetPCIRelatedDevCaps(const char *sysfsPath ATTRIBUTE_UNUSED,
                                    virNodeDevCapDataPtr data ATTRIBUTE_UNUSED)
{
    return -1;
}

248
#endif /* __linux__ */