2
0

xen-host-pci-device.c 9.6 KB

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