vpci.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Backend - Provides a Virtual PCI bus (with real devices)
  4. * to the frontend
  5. *
  6. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/list.h>
  10. #include <linux/slab.h>
  11. #include <linux/pci.h>
  12. #include <linux/mutex.h>
  13. #include "pciback.h"
  14. #define PCI_SLOT_MAX 32
  15. struct vpci_dev_data {
  16. /* Access to dev_list must be protected by lock */
  17. struct list_head dev_list[PCI_SLOT_MAX];
  18. struct mutex lock;
  19. };
  20. static inline struct list_head *list_first(struct list_head *head)
  21. {
  22. return head->next;
  23. }
  24. static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
  25. unsigned int domain,
  26. unsigned int bus,
  27. unsigned int devfn)
  28. {
  29. struct pci_dev_entry *entry;
  30. struct pci_dev *dev = NULL;
  31. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  32. if (domain != 0 || bus != 0)
  33. return NULL;
  34. if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
  35. mutex_lock(&vpci_dev->lock);
  36. list_for_each_entry(entry,
  37. &vpci_dev->dev_list[PCI_SLOT(devfn)],
  38. list) {
  39. if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
  40. dev = entry->dev;
  41. break;
  42. }
  43. }
  44. mutex_unlock(&vpci_dev->lock);
  45. }
  46. return dev;
  47. }
  48. static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
  49. {
  50. if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
  51. && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
  52. return 1;
  53. return 0;
  54. }
  55. static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
  56. struct pci_dev *dev, int devid,
  57. publish_pci_dev_cb publish_cb)
  58. {
  59. int err = 0, slot, func = PCI_FUNC(dev->devfn);
  60. struct pci_dev_entry *t, *dev_entry;
  61. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  62. if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
  63. err = -EFAULT;
  64. xenbus_dev_fatal(pdev->xdev, err,
  65. "Can't export bridges on the virtual PCI bus");
  66. goto out;
  67. }
  68. dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
  69. if (!dev_entry) {
  70. err = -ENOMEM;
  71. xenbus_dev_fatal(pdev->xdev, err,
  72. "Error adding entry to virtual PCI bus");
  73. goto out;
  74. }
  75. dev_entry->dev = dev;
  76. mutex_lock(&vpci_dev->lock);
  77. /*
  78. * Keep multi-function devices together on the virtual PCI bus, except
  79. * that we want to keep virtual functions at func 0 on their own. They
  80. * aren't multi-function devices and hence their presence at func 0
  81. * may cause guests to not scan the other functions.
  82. */
  83. if (!dev->is_virtfn || func) {
  84. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  85. if (list_empty(&vpci_dev->dev_list[slot]))
  86. continue;
  87. t = list_entry(list_first(&vpci_dev->dev_list[slot]),
  88. struct pci_dev_entry, list);
  89. if (t->dev->is_virtfn && !PCI_FUNC(t->dev->devfn))
  90. continue;
  91. if (match_slot(dev, t->dev)) {
  92. pr_info("vpci: %s: assign to virtual slot %d func %d\n",
  93. pci_name(dev), slot,
  94. func);
  95. list_add_tail(&dev_entry->list,
  96. &vpci_dev->dev_list[slot]);
  97. goto unlock;
  98. }
  99. }
  100. }
  101. /* Assign to a new slot on the virtual PCI bus */
  102. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  103. if (list_empty(&vpci_dev->dev_list[slot])) {
  104. pr_info("vpci: %s: assign to virtual slot %d\n",
  105. pci_name(dev), slot);
  106. list_add_tail(&dev_entry->list,
  107. &vpci_dev->dev_list[slot]);
  108. goto unlock;
  109. }
  110. }
  111. err = -ENOMEM;
  112. xenbus_dev_fatal(pdev->xdev, err,
  113. "No more space on root virtual PCI bus");
  114. unlock:
  115. mutex_unlock(&vpci_dev->lock);
  116. /* Publish this device. */
  117. if (!err)
  118. err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
  119. else
  120. kfree(dev_entry);
  121. out:
  122. return err;
  123. }
  124. static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
  125. struct pci_dev *dev, bool lock)
  126. {
  127. int slot;
  128. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  129. struct pci_dev *found_dev = NULL;
  130. mutex_lock(&vpci_dev->lock);
  131. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  132. struct pci_dev_entry *e;
  133. list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
  134. if (e->dev == dev) {
  135. list_del(&e->list);
  136. found_dev = e->dev;
  137. kfree(e);
  138. goto out;
  139. }
  140. }
  141. }
  142. out:
  143. mutex_unlock(&vpci_dev->lock);
  144. if (found_dev) {
  145. if (lock)
  146. device_lock(&found_dev->dev);
  147. pcistub_put_pci_dev(found_dev);
  148. if (lock)
  149. device_unlock(&found_dev->dev);
  150. }
  151. }
  152. static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
  153. {
  154. int slot;
  155. struct vpci_dev_data *vpci_dev;
  156. vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
  157. if (!vpci_dev)
  158. return -ENOMEM;
  159. mutex_init(&vpci_dev->lock);
  160. for (slot = 0; slot < PCI_SLOT_MAX; slot++)
  161. INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
  162. pdev->pci_dev_data = vpci_dev;
  163. return 0;
  164. }
  165. static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
  166. publish_pci_root_cb publish_cb)
  167. {
  168. /* The Virtual PCI bus has only one root */
  169. return publish_cb(pdev, 0, 0);
  170. }
  171. static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
  172. {
  173. int slot;
  174. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  175. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  176. struct pci_dev_entry *e, *tmp;
  177. list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
  178. list) {
  179. struct pci_dev *dev = e->dev;
  180. list_del(&e->list);
  181. device_lock(&dev->dev);
  182. pcistub_put_pci_dev(dev);
  183. device_unlock(&dev->dev);
  184. kfree(e);
  185. }
  186. }
  187. kfree(vpci_dev);
  188. pdev->pci_dev_data = NULL;
  189. }
  190. static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
  191. struct xen_pcibk_device *pdev,
  192. unsigned int *domain, unsigned int *bus,
  193. unsigned int *devfn)
  194. {
  195. struct pci_dev_entry *entry;
  196. struct pci_dev *dev = NULL;
  197. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  198. int found = 0, slot;
  199. mutex_lock(&vpci_dev->lock);
  200. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  201. list_for_each_entry(entry,
  202. &vpci_dev->dev_list[slot],
  203. list) {
  204. dev = entry->dev;
  205. if (dev && dev->bus->number == pcidev->bus->number
  206. && pci_domain_nr(dev->bus) ==
  207. pci_domain_nr(pcidev->bus)
  208. && dev->devfn == pcidev->devfn) {
  209. found = 1;
  210. *domain = 0;
  211. *bus = 0;
  212. *devfn = PCI_DEVFN(slot,
  213. PCI_FUNC(pcidev->devfn));
  214. }
  215. }
  216. }
  217. mutex_unlock(&vpci_dev->lock);
  218. return found;
  219. }
  220. const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
  221. .name = "vpci",
  222. .init = __xen_pcibk_init_devices,
  223. .free = __xen_pcibk_release_devices,
  224. .find = __xen_pcibk_get_pcifront_dev,
  225. .publish = __xen_pcibk_publish_pci_roots,
  226. .release = __xen_pcibk_release_pci_dev,
  227. .add = __xen_pcibk_add_pci_dev,
  228. .get = __xen_pcibk_get_pci_dev,
  229. };