xen-host-pci-device.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (C) 2011 Citrix Ltd.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. *
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qapi/error.h"
  10. #include "qemu/cutils.h"
  11. #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))
  14. #ifdef XEN_HOST_PCI_DEVICE_DEBUG
  15. # define XEN_HOST_PCI_LOG(f, a...) fprintf(stderr, "%s: " f, __func__, ##a)
  16. #else
  17. # define XEN_HOST_PCI_LOG(f, a...) (void)0
  18. #endif
  19. /*
  20. * from linux/ioport.h
  21. * IO resources have these defined flags.
  22. */
  23. #define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
  24. #define IORESOURCE_TYPE_BITS 0x00000f00 /* Resource type */
  25. #define IORESOURCE_IO 0x00000100
  26. #define IORESOURCE_MEM 0x00000200
  27. #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
  28. #define IORESOURCE_MEM_64 0x00100000
  29. static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d,
  30. const char *name, char *buf, ssize_t size)
  31. {
  32. int rc;
  33. rc = snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s",
  34. d->domain, d->bus, d->dev, d->func, name);
  35. assert(rc >= 0 && rc < size);
  36. }
  37. /* This size should be enough to read the first 7 lines of a resource file */
  38. #define XEN_HOST_PCI_RESOURCE_BUFFER_SIZE 400
  39. static void xen_host_pci_get_resource(XenHostPCIDevice *d, Error **errp)
  40. {
  41. int i, rc, fd;
  42. char path[PATH_MAX];
  43. char buf[XEN_HOST_PCI_RESOURCE_BUFFER_SIZE];
  44. unsigned long long start, end, flags, size;
  45. char *endptr, *s;
  46. uint8_t type;
  47. xen_host_pci_sysfs_path(d, "resource", path, sizeof(path));
  48. fd = open(path, O_RDONLY);
  49. if (fd == -1) {
  50. error_setg_file_open(errp, errno, path);
  51. return;
  52. }
  53. do {
  54. rc = read(fd, &buf, sizeof(buf) - 1);
  55. if (rc < 0 && errno != EINTR) {
  56. error_setg_errno(errp, errno, "read err");
  57. goto out;
  58. }
  59. } while (rc < 0);
  60. buf[rc] = 0;
  61. s = buf;
  62. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  63. type = 0;
  64. start = strtoll(s, &endptr, 16);
  65. if (*endptr != ' ' || s == endptr) {
  66. break;
  67. }
  68. s = endptr + 1;
  69. end = strtoll(s, &endptr, 16);
  70. if (*endptr != ' ' || s == endptr) {
  71. break;
  72. }
  73. s = endptr + 1;
  74. flags = strtoll(s, &endptr, 16);
  75. if (*endptr != '\n' || s == endptr) {
  76. break;
  77. }
  78. s = endptr + 1;
  79. if (start) {
  80. size = end - start + 1;
  81. } else {
  82. size = 0;
  83. }
  84. if (flags & IORESOURCE_IO) {
  85. type |= XEN_HOST_PCI_REGION_TYPE_IO;
  86. }
  87. if (flags & IORESOURCE_MEM) {
  88. type |= XEN_HOST_PCI_REGION_TYPE_MEM;
  89. }
  90. if (flags & IORESOURCE_PREFETCH) {
  91. type |= XEN_HOST_PCI_REGION_TYPE_PREFETCH;
  92. }
  93. if (flags & IORESOURCE_MEM_64) {
  94. type |= XEN_HOST_PCI_REGION_TYPE_MEM_64;
  95. }
  96. if (i < PCI_ROM_SLOT) {
  97. d->io_regions[i].base_addr = start;
  98. d->io_regions[i].size = size;
  99. d->io_regions[i].type = type;
  100. d->io_regions[i].bus_flags = flags & IORESOURCE_BITS;
  101. } else {
  102. d->rom.base_addr = start;
  103. d->rom.size = size;
  104. d->rom.type = type;
  105. d->rom.bus_flags = flags & IORESOURCE_BITS;
  106. }
  107. }
  108. if (i != PCI_NUM_REGIONS) {
  109. error_setg(errp, "Invalid format or input too short: %s", buf);
  110. }
  111. out:
  112. close(fd);
  113. }
  114. /* This size should be enough to read a long from a file */
  115. #define XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE 22
  116. static void xen_host_pci_get_value(XenHostPCIDevice *d, const char *name,
  117. unsigned int *pvalue, int base, Error **errp)
  118. {
  119. char path[PATH_MAX];
  120. char buf[XEN_HOST_PCI_GET_VALUE_BUFFER_SIZE];
  121. int fd, rc;
  122. unsigned long value;
  123. const char *endptr;
  124. xen_host_pci_sysfs_path(d, name, path, sizeof(path));
  125. fd = open(path, O_RDONLY);
  126. if (fd == -1) {
  127. error_setg_file_open(errp, errno, path);
  128. return;
  129. }
  130. do {
  131. rc = read(fd, &buf, sizeof(buf) - 1);
  132. if (rc < 0 && errno != EINTR) {
  133. error_setg_errno(errp, errno, "read err");
  134. goto out;
  135. }
  136. } while (rc < 0);
  137. buf[rc] = 0;
  138. rc = qemu_strtoul(buf, &endptr, base, &value);
  139. if (!rc) {
  140. assert(value <= UINT_MAX);
  141. *pvalue = value;
  142. } else {
  143. error_setg_errno(errp, -rc, "failed to parse value '%s'", buf);
  144. }
  145. out:
  146. close(fd);
  147. }
  148. static inline void xen_host_pci_get_hex_value(XenHostPCIDevice *d,
  149. const char *name,
  150. unsigned int *pvalue,
  151. Error **errp)
  152. {
  153. xen_host_pci_get_value(d, name, pvalue, 16, errp);
  154. }
  155. static inline void xen_host_pci_get_dec_value(XenHostPCIDevice *d,
  156. const char *name,
  157. unsigned int *pvalue,
  158. Error **errp)
  159. {
  160. xen_host_pci_get_value(d, name, pvalue, 10, errp);
  161. }
  162. static bool xen_host_pci_dev_is_virtfn(XenHostPCIDevice *d)
  163. {
  164. char path[PATH_MAX];
  165. struct stat buf;
  166. xen_host_pci_sysfs_path(d, "physfn", path, sizeof(path));
  167. return !stat(path, &buf);
  168. }
  169. static void xen_host_pci_config_open(XenHostPCIDevice *d, Error **errp)
  170. {
  171. char path[PATH_MAX];
  172. xen_host_pci_sysfs_path(d, "config", path, sizeof(path));
  173. d->config_fd = open(path, O_RDWR);
  174. if (d->config_fd == -1) {
  175. error_setg_file_open(errp, errno, path);
  176. }
  177. }
  178. static int xen_host_pci_config_read(XenHostPCIDevice *d,
  179. int pos, void *buf, int len)
  180. {
  181. int rc;
  182. do {
  183. rc = pread(d->config_fd, buf, len, pos);
  184. } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
  185. if (rc != len) {
  186. return -errno;
  187. }
  188. return 0;
  189. }
  190. static int xen_host_pci_config_write(XenHostPCIDevice *d,
  191. int pos, const void *buf, int len)
  192. {
  193. int rc;
  194. do {
  195. rc = pwrite(d->config_fd, buf, len, pos);
  196. } while (rc < 0 && (errno == EINTR || errno == EAGAIN));
  197. if (rc != len) {
  198. return -errno;
  199. }
  200. return 0;
  201. }
  202. int xen_host_pci_get_byte(XenHostPCIDevice *d, int pos, uint8_t *p)
  203. {
  204. uint8_t buf;
  205. int rc = xen_host_pci_config_read(d, pos, &buf, 1);
  206. if (!rc) {
  207. *p = buf;
  208. }
  209. return rc;
  210. }
  211. int xen_host_pci_get_word(XenHostPCIDevice *d, int pos, uint16_t *p)
  212. {
  213. uint16_t buf;
  214. int rc = xen_host_pci_config_read(d, pos, &buf, 2);
  215. if (!rc) {
  216. *p = le16_to_cpu(buf);
  217. }
  218. return rc;
  219. }
  220. int xen_host_pci_get_long(XenHostPCIDevice *d, int pos, uint32_t *p)
  221. {
  222. uint32_t buf;
  223. int rc = xen_host_pci_config_read(d, pos, &buf, 4);
  224. if (!rc) {
  225. *p = le32_to_cpu(buf);
  226. }
  227. return rc;
  228. }
  229. int xen_host_pci_get_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
  230. {
  231. return xen_host_pci_config_read(d, pos, buf, len);
  232. }
  233. int xen_host_pci_set_byte(XenHostPCIDevice *d, int pos, uint8_t data)
  234. {
  235. return xen_host_pci_config_write(d, pos, &data, 1);
  236. }
  237. int xen_host_pci_set_word(XenHostPCIDevice *d, int pos, uint16_t data)
  238. {
  239. data = cpu_to_le16(data);
  240. return xen_host_pci_config_write(d, pos, &data, 2);
  241. }
  242. int xen_host_pci_set_long(XenHostPCIDevice *d, int pos, uint32_t data)
  243. {
  244. data = cpu_to_le32(data);
  245. return xen_host_pci_config_write(d, pos, &data, 4);
  246. }
  247. int xen_host_pci_set_block(XenHostPCIDevice *d, int pos, uint8_t *buf, int len)
  248. {
  249. return xen_host_pci_config_write(d, pos, buf, len);
  250. }
  251. int xen_host_pci_find_ext_cap_offset(XenHostPCIDevice *d, uint32_t cap)
  252. {
  253. uint32_t header = 0;
  254. int max_cap = XEN_HOST_PCI_MAX_EXT_CAP;
  255. int pos = PCI_CONFIG_SPACE_SIZE;
  256. do {
  257. if (xen_host_pci_get_long(d, pos, &header)) {
  258. break;
  259. }
  260. /*
  261. * If we have no capabilities, this is indicated by cap ID,
  262. * cap version and next pointer all being 0.
  263. */
  264. if (header == 0) {
  265. break;
  266. }
  267. if (PCI_EXT_CAP_ID(header) == cap) {
  268. return pos;
  269. }
  270. pos = PCI_EXT_CAP_NEXT(header);
  271. if (pos < PCI_CONFIG_SPACE_SIZE) {
  272. break;
  273. }
  274. max_cap--;
  275. } while (max_cap > 0);
  276. return -1;
  277. }
  278. void xen_host_pci_device_get(XenHostPCIDevice *d, uint16_t domain,
  279. uint8_t bus, uint8_t dev, uint8_t func,
  280. Error **errp)
  281. {
  282. ERRP_GUARD();
  283. unsigned int v;
  284. d->config_fd = -1;
  285. d->domain = domain;
  286. d->bus = bus;
  287. d->dev = dev;
  288. d->func = func;
  289. xen_host_pci_config_open(d, errp);
  290. if (*errp) {
  291. goto error;
  292. }
  293. xen_host_pci_get_resource(d, errp);
  294. if (*errp) {
  295. goto error;
  296. }
  297. xen_host_pci_get_hex_value(d, "vendor", &v, errp);
  298. if (*errp) {
  299. goto error;
  300. }
  301. d->vendor_id = v;
  302. xen_host_pci_get_hex_value(d, "device", &v, errp);
  303. if (*errp) {
  304. goto error;
  305. }
  306. d->device_id = v;
  307. xen_host_pci_get_dec_value(d, "irq", &v, errp);
  308. if (*errp) {
  309. goto error;
  310. }
  311. d->irq = v;
  312. xen_host_pci_get_hex_value(d, "class", &v, errp);
  313. if (*errp) {
  314. goto error;
  315. }
  316. d->class_code = v;
  317. d->is_virtfn = xen_host_pci_dev_is_virtfn(d);
  318. return;
  319. error:
  320. if (d->config_fd >= 0) {
  321. close(d->config_fd);
  322. d->config_fd = -1;
  323. }
  324. }
  325. bool xen_host_pci_device_closed(XenHostPCIDevice *d)
  326. {
  327. return d->config_fd == -1;
  328. }
  329. void xen_host_pci_device_put(XenHostPCIDevice *d)
  330. {
  331. if (d->config_fd >= 0) {
  332. close(d->config_fd);
  333. d->config_fd = -1;
  334. }
  335. }