debuggers.hg

changeset 21961:b8a651785471

xl: PCI code cleanups

Get rid of scan_sys_pcidir() and open-code it inside
libxl_device_pci_list_assignable() since it's not a generically re-useable
function and we're not supporting pcistub driver now. Also use macros for sysfs
dirs in libxl_device_pci_reset

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Wed Aug 04 14:23:29 2010 +0100 (2010-08-04)
parents d7ec0e180601
children 3eec4c068649
files tools/libxl/libxl_pci.c
line diff
     1.1 --- a/tools/libxl/libxl_pci.c	Tue Aug 03 18:10:28 2010 +0100
     1.2 +++ b/tools/libxl/libxl_pci.c	Wed Aug 04 14:23:29 2010 +0100
     1.3 @@ -325,6 +325,57 @@ static int is_assigned(libxl_device_pci 
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +int libxl_device_pci_list_assignable(libxl_ctx *ctx, libxl_device_pci **list, int *num)
     1.8 +{
     1.9 +    libxl_device_pci *pcidevs = NULL, *new, *assigned;
    1.10 +    struct dirent *de;
    1.11 +    DIR *dir;
    1.12 +    int rc, num_assigned;
    1.13 +
    1.14 +    *num = 0;
    1.15 +    *list = NULL;
    1.16 +
    1.17 +    rc = get_all_assigned_devices(ctx, &assigned, &num_assigned);
    1.18 +    if ( rc )
    1.19 +        return rc;
    1.20 +
    1.21 +    dir = opendir(SYSFS_PCIBACK_DRIVER);
    1.22 +    if ( NULL == dir ) {
    1.23 +        if ( errno == ENOENT ) {
    1.24 +            XL_LOG(ctx, XL_LOG_ERROR, "Looks like pciback driver not loaded");
    1.25 +        }else{
    1.26 +            XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER);
    1.27 +        }
    1.28 +        free(assigned);
    1.29 +        return ERROR_FAIL;
    1.30 +    }
    1.31 +
    1.32 +    while( (de = readdir(dir)) ) {
    1.33 +        unsigned dom, bus, dev, func;
    1.34 +        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
    1.35 +            continue;
    1.36 +
    1.37 +        if ( is_assigned(assigned, num_assigned, dom, bus, dev, func) )
    1.38 +            continue;
    1.39 +
    1.40 +        new = realloc(pcidevs, ((*num) + 1) * sizeof(*new));
    1.41 +        if ( NULL == new )
    1.42 +            continue;
    1.43 +
    1.44 +        pcidevs = new;
    1.45 +        new = pcidevs + *num;
    1.46 +
    1.47 +        memset(new, 0, sizeof(*new));
    1.48 +        libxl_device_pci_init(new, dom, bus, dev, func, 0);
    1.49 +        (*num)++;
    1.50 +    }
    1.51 +
    1.52 +    closedir(dir);
    1.53 +    free(assigned);
    1.54 +    *list = pcidevs;
    1.55 +    return 0;
    1.56 +}
    1.57 +
    1.58  static int do_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
    1.59  {
    1.60      char *path;
    1.61 @@ -554,63 +605,6 @@ out:
    1.62      return 0;
    1.63  }
    1.64  
    1.65 -static libxl_device_pci *scan_sys_pcidir(libxl_device_pci *assigned,
    1.66 -                                         int num_assigned, const char *path, int *num)
    1.67 -{
    1.68 -    libxl_device_pci *pcidevs = NULL, *new;
    1.69 -    struct dirent *de;
    1.70 -    DIR *dir;
    1.71 -
    1.72 -    dir = opendir(path);
    1.73 -    if ( NULL == dir )
    1.74 -        return pcidevs;
    1.75 -
    1.76 -    while( (de = readdir(dir)) ) {
    1.77 -        unsigned dom, bus, dev, func;
    1.78 -        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
    1.79 -            continue;
    1.80 -
    1.81 -        if ( is_assigned(assigned, num_assigned, dom, bus, dev, func) )
    1.82 -            continue;
    1.83 -
    1.84 -        new = realloc(pcidevs, ((*num) + 1) * sizeof(*new));
    1.85 -        if ( NULL == new )
    1.86 -            continue;
    1.87 -
    1.88 -        pcidevs = new;
    1.89 -        new = pcidevs + *num;
    1.90 -
    1.91 -        memset(new, 0, sizeof(*new));
    1.92 -        libxl_device_pci_init(new, dom, bus, dev, func, 0);
    1.93 -        (*num)++;
    1.94 -    }
    1.95 -
    1.96 -    closedir(dir);
    1.97 -    return pcidevs;
    1.98 -}
    1.99 -
   1.100 -int libxl_device_pci_list_assignable(libxl_ctx *ctx, libxl_device_pci **list, int *num)
   1.101 -{
   1.102 -    libxl_device_pci *pcidevs = NULL;
   1.103 -    libxl_device_pci *assigned;
   1.104 -    int num_assigned, rc;
   1.105 -
   1.106 -    *num = 0;
   1.107 -    *list = NULL;
   1.108 -
   1.109 -    rc = get_all_assigned_devices(ctx, &assigned, &num_assigned);
   1.110 -    if ( rc )
   1.111 -        return rc;
   1.112 -
   1.113 -    pcidevs = scan_sys_pcidir(assigned, num_assigned,
   1.114 -                              SYSFS_PCIBACK_DRIVER, num);
   1.115 -
   1.116 -    free(assigned);
   1.117 -    if ( *num )
   1.118 -        *list = pcidevs;
   1.119 -    return 0;
   1.120 -}
   1.121 -
   1.122  int libxl_device_pci_list_assigned(libxl_ctx *ctx, libxl_device_pci **list, uint32_t domid, int *num)
   1.123  {
   1.124      char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
   1.125 @@ -623,7 +617,7 @@ int libxl_device_pci_list_assigned(libxl
   1.126      if (!num_devs) {
   1.127          *num = 0;
   1.128          *list = NULL;
   1.129 -        return ERROR_FAIL;
   1.130 +        return 0;
   1.131      }
   1.132      n = atoi(num_devs);
   1.133      pcidevs = calloc(n, sizeof(libxl_device_pci));
   1.134 @@ -689,9 +683,10 @@ int libxl_device_pci_init(libxl_device_p
   1.135  int libxl_device_pci_reset(libxl_ctx *ctx, unsigned int domain, unsigned int bus,
   1.136                           unsigned int dev, unsigned int func)
   1.137  {
   1.138 -    char *reset = "/sys/bus/pci/drivers/pciback/do_flr";
   1.139 +    char *reset;
   1.140      int fd, rc;
   1.141  
   1.142 +    reset = libxl_sprintf(ctx, "%s/pciback/do_flr", SYSFS_PCI_DEV);
   1.143      fd = open(reset, O_WRONLY);
   1.144      if (fd > 0) {
   1.145          char *buf = libxl_sprintf(ctx, PCI_BDF, domain, bus, dev, func);
   1.146 @@ -703,7 +698,7 @@ int libxl_device_pci_reset(libxl_ctx *ct
   1.147      }
   1.148      if (errno != ENOENT)
   1.149          XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Failed to access pciback path %s", reset);
   1.150 -    reset = libxl_sprintf(ctx, "/sys/bus/pci/devices/"PCI_BDF"/reset", domain, bus, dev, func);
   1.151 +    reset = libxl_sprintf(ctx, "%s/"PCI_BDF"/reset", SYSFS_PCI_DEV, domain, bus, dev, func);
   1.152      fd = open(reset, O_WRONLY);
   1.153      if (fd > 0) {
   1.154          rc = write(fd, "1", 1);