Xen: use qemu_strtoul instead of strtol
[qemu.git] / hw / xen / xen-host-pci-device.c
blob83da9c47cbc42a47533a43d0b13d5fdad93a06ae
1 /*
2 * Copyright (C) 2011 Citrix Ltd.
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
7 */
9 #include "qemu-common.h"
10 #include "xen-host-pci-device.h"
12 #define XEN_HOST_PCI_MAX_EXT_CAP \
13 ((PCIE_CONFIG_SPACE_SIZE - PCI_CONFIG_SPACE_SIZE) / (PCI_CAP_SIZEOF + 4))
15 #ifdef XEN_HOST_PCI_DEVICE_DEBUG
16 # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
17 #else
18 # define XEN_HOST_PCI_LOG(f, a...) (void)0
19 #endif
22 * from linux/ioport.h
23 * IO resources have these defined flags.
25 #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
27 #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
28 #define IORESOURCE_IO 0x00000100
29 #define IORESOURCE_MEM 0x00000200
31 #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
32 #define IORESOURCE_MEM_64 0x00100000
34 static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
35 const char *name, char *buf, ssize_t size)
37 int rc;
39 rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
40 d->domain, d->bus, d->dev, d->func, name);
41 assert(rc >= 0 && rc < size);
45 /* This size should be enough to read the first 7 lines of a resource file */
46 #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
47 static int xen_host_pci_get_resource(XenHostPCIDevice *d)
49 int i, rc, fd;
50 char path[PATH_MAX];
51 char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
52 unsigned long long start, end, flags, size;
53 char *endptr, *s;
54 uint8_t type;
56 xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
58 fd = open(path, O_RDONLY);
59 if (fd == -1) {
60 XEN_HOST_PCI_LOG("Error: Can't open %s: %s\n", path, strerror(errno));
61 return -errno;
64 do {
65 rc = read(fd, &buf, sizeof (buf) - 1);
66 if (rc < 0 && errno != EINTR) {
67 rc = -errno;
68 goto out;
70 } while (rc < 0);
71 buf[rc] = 0;
72 rc = 0;
74 s = buf;
75 for (i = 0; i < PCI_NUM_REGIONS; i++) {
76 type = 0;
78 start = strtoll(s, &endptr, 16);
79 if (*endptr != ' ' || s == endptr) {
80 break;
82 s = endptr + 1;
83 end = strtoll(s, &endptr, 16);
84 if (*endptr != ' ' || s == endptr) {
85 break;
87 s = endptr + 1;
88 flags = strtoll(s, &endptr, 16);
89 if (*endptr != '\n' || s == endptr) {
90 break;
92 s = endptr + 1;
94 if (start) {
95 size = end - start + 1;
96 } else {
97 size = 0;
100 if (flags & IORESOURCE_IO) {
101 type |= XEN_HOST_PCI_REGION_TYPE_IO;
103 if (flags & IORESOURCE_MEM) {
104 type |= XEN_HOST_PCI_REGION_TYPE_MEM;
106 if (flags & IORESOURCE_PREFETCH) {
107 type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
109 if (flags & IORESOURCE_MEM_64) {
110 type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
113 if (i < PCI_ROM_SLOT) {
114 d->io_regions[i].base_addr = start;
115 d->io_regions[i].size = size;
116 d->io_regions[i].type = type;
117 d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
118 } else {
119 d->rom.base_addr = start;
120 d->rom.size = size;
121 d->rom.type = type;
122 d->rom.bus_flags = flags & IORESOURCE_BITS;
125 if (i != PCI_NUM_REGIONS) {
126 /* Invalid format or input to short */
127 rc = -ENODEV;
130 out:
131 close(fd);
132 return rc;
135 /* This size should be enough to read a long from a file */
136 #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
137 static int xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
138 unsigned int *pvalue, int base)
140 char path[PATH_MAX];
141 char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
142 int fd, rc;
143 unsigned long value;
144 const char *endptr;
146 xen_host_pci_sysfs_path(d, name, path, sizeof(path));
148 fd = open(path, O_RDONLY);
149 if (fd == -1) {
150 XEN_HOST_PCI_LOG("Error: Can't open %s: %s\n", path, strerror(errno));
151 return -errno;
153 do {
154 rc = read(fd, &buf, sizeof (buf) - 1);
155 if (rc < 0 && errno != EINTR) {
156 rc = -errno;
157 goto out;
159 } while (rc < 0);
160 buf[rc] = 0;
161 rc = qemu_strtoul(buf, &endptr, base, &value);
162 if (!rc) {
163 assert(value <= UINT_MAX);
164 *pvalue = value;
166 out:
167 close(fd);
168 return rc;
171 static inline int xen_host_pci_get_hex_value(XenHostPCIDevice *d,
172 const char *name,
173 unsigned int *pvalue)
175 return xen_host_pci_get_value(d, name, pvalue, 16);
178 static inline int xen_host_pci_get_dec_value(XenHostPCIDevice *d,
179 const char *name,
180 unsigned int *pvalue)
182 return xen_host_pci_get_value(d, name, pvalue, 10);
185 static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
187 char path[PATH_MAX];
188 struct stat buf;
190 xen_host_pci_sysfs_path(d, "physfn", path, sizeof(path));
192 return !stat(path, &buf);
195 static int xen_host_pci_config_open(XenHostPCIDevice *d)
197 char path[PATH_MAX];
199 xen_host_pci_sysfs_path(d, "config", path, sizeof(path));
201 d->config_fd = open(path, O_RDWR);
202 if (d->config_fd < 0) {
203 return -errno;
205 return 0;
208 static int xen_host_pci_config_read(XenHostPCIDevice *d,
209 int pos, void *buf, int len)
211 int rc;
213 do {
214 rc = pread(d->config_fd, buf, len, pos);
215 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
216 if (rc != len) {
217 return -errno;
219 return 0;
222 static int xen_host_pci_config_write(XenHostPCIDevice *d,
223 int pos, const void *buf, int len)
225 int rc;
227 do {
228 rc = pwrite(d->config_fd, buf, len, pos);
229 } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
230 if (rc != len) {
231 return -errno;
233 return 0;
237 int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
239 uint8_t buf;
240 int rc = xen_host_pci_config_read(d, pos, &buf, 1);
241 if (!rc) {
242 *p = buf;
244 return rc;
247 int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
249 uint16_t buf;
250 int rc = xen_host_pci_config_read(d, pos, &buf, 2);
251 if (!rc) {
252 *p = le16_to_cpu(buf);
254 return rc;
257 int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
259 uint32_t buf;
260 int rc = xen_host_pci_config_read(d, pos, &buf, 4);
261 if (!rc) {
262 *p = le32_to_cpu(buf);
264 return rc;
267 int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
269 return xen_host_pci_config_read(d, pos, buf, len);
272 int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
274 return xen_host_pci_config_write(d, pos, &data, 1);
277 int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
279 data = cpu_to_le16(data);
280 return xen_host_pci_config_write(d, pos, &data, 2);
283 int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
285 data = cpu_to_le32(data);
286 return xen_host_pci_config_write(d, pos, &data, 4);
289 int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
291 return xen_host_pci_config_write(d, pos, buf, len);
294 int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
296 uint32_t header = 0;
297 int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
298 int pos = PCI_CONFIG_SPACE_SIZE;
300 do {
301 if (xen_host_pci_get_long(d, pos, &header)) {
302 break;
305 * If we have no capabilities, this is indicated by cap ID,
306 * cap version and next pointer all being 0.
308 if (header == 0) {
309 break;
312 if (PCI_EXT_CAP_ID(header) == cap) {
313 return pos;
316 pos = PCI_EXT_CAP_NEXT(header);
317 if (pos < PCI_CONFIG_SPACE_SIZE) {
318 break;
321 max_cap--;
322 } while (max_cap > 0);
324 return -1;
327 int xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
328 uint8_t bus, uint8_t dev, uint8_t func)
330 unsigned int v;
331 int rc = 0;
333 d->config_fd = -1;
334 d->domain = domain;
335 d->bus = bus;
336 d->dev = dev;
337 d->func = func;
339 rc = xen_host_pci_config_open(d);
340 if (rc) {
341 goto error;
343 rc = xen_host_pci_get_resource(d);
344 if (rc) {
345 goto error;
347 rc = xen_host_pci_get_hex_value(d, "vendor", &v);
348 if (rc) {
349 goto error;
351 d->vendor_id = v;
352 rc = xen_host_pci_get_hex_value(d, "device", &v);
353 if (rc) {
354 goto error;
356 d->device_id = v;
357 rc = xen_host_pci_get_dec_value(d, "irq", &v);
358 if (rc) {
359 goto error;
361 d->irq = v;
362 rc = xen_host_pci_get_hex_value(d, "class", &v);
363 if (rc) {
364 goto error;
366 d->class_code = v;
367 d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
369 return 0;
370 error:
371 if (d->config_fd >= 0) {
372 close(d->config_fd);
373 d->config_fd = -1;
375 return rc;
378 bool xen_host_pci_device_closed(XenHostPCIDevice *d)
380 return d->config_fd == -1;
383 void xen_host_pci_device_put(XenHostPCIDevice *d)
385 if (d->config_fd >= 0) {
386 close(d->config_fd);
387 d->config_fd = -1;