vpci.c 6.0 KB

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