vpci.c 6.2 KB

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