vpci.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/spinlock.h>
  11. #include "pciback.h"
  12. #define PCI_SLOT_MAX 32
  13. #define DRV_NAME "xen-pciback"
  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. spinlock_t lock;
  18. };
  19. static inline struct list_head *list_first(struct list_head *head)
  20. {
  21. return head->next;
  22. }
  23. struct pci_dev *xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
  24. unsigned int domain, 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. unsigned long flags;
  31. if (domain != 0 || bus != 0)
  32. return NULL;
  33. if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
  34. spin_lock_irqsave(&vpci_dev->lock, flags);
  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. spin_unlock_irqrestore(&vpci_dev->lock, flags);
  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. int xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev, struct pci_dev *dev,
  55. int devid, 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. unsigned long flags;
  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. spin_lock_irqsave(&vpci_dev->lock, flags);
  76. /* Keep multi-function devices together on the virtual PCI bus */
  77. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  78. if (!list_empty(&vpci_dev->dev_list[slot])) {
  79. t = list_entry(list_first(&vpci_dev->dev_list[slot]),
  80. struct pci_dev_entry, list);
  81. if (match_slot(dev, t->dev)) {
  82. pr_info(DRV_NAME ": vpci: %s: "
  83. "assign to virtual slot %d func %d\n",
  84. pci_name(dev), slot,
  85. PCI_FUNC(dev->devfn));
  86. list_add_tail(&dev_entry->list,
  87. &vpci_dev->dev_list[slot]);
  88. func = PCI_FUNC(dev->devfn);
  89. goto unlock;
  90. }
  91. }
  92. }
  93. /* Assign to a new slot on the virtual PCI bus */
  94. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  95. if (list_empty(&vpci_dev->dev_list[slot])) {
  96. printk(KERN_INFO DRV_NAME
  97. ": vpci: %s: assign to virtual slot %d\n",
  98. pci_name(dev), slot);
  99. list_add_tail(&dev_entry->list,
  100. &vpci_dev->dev_list[slot]);
  101. func = PCI_FUNC(dev->devfn);
  102. goto unlock;
  103. }
  104. }
  105. err = -ENOMEM;
  106. xenbus_dev_fatal(pdev->xdev, err,
  107. "No more space on root virtual PCI bus");
  108. unlock:
  109. spin_unlock_irqrestore(&vpci_dev->lock, flags);
  110. /* Publish this device. */
  111. if (!err)
  112. err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
  113. out:
  114. return err;
  115. }
  116. void xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
  117. struct pci_dev *dev)
  118. {
  119. int slot;
  120. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  121. struct pci_dev *found_dev = NULL;
  122. unsigned long flags;
  123. spin_lock_irqsave(&vpci_dev->lock, flags);
  124. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  125. struct pci_dev_entry *e, *tmp;
  126. list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
  127. list) {
  128. if (e->dev == dev) {
  129. list_del(&e->list);
  130. found_dev = e->dev;
  131. kfree(e);
  132. goto out;
  133. }
  134. }
  135. }
  136. out:
  137. spin_unlock_irqrestore(&vpci_dev->lock, flags);
  138. if (found_dev)
  139. pcistub_put_pci_dev(found_dev);
  140. }
  141. int xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
  142. {
  143. int slot;
  144. struct vpci_dev_data *vpci_dev;
  145. vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
  146. if (!vpci_dev)
  147. return -ENOMEM;
  148. spin_lock_init(&vpci_dev->lock);
  149. for (slot = 0; slot < PCI_SLOT_MAX; slot++)
  150. INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
  151. pdev->pci_dev_data = vpci_dev;
  152. return 0;
  153. }
  154. int xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
  155. publish_pci_root_cb publish_cb)
  156. {
  157. /* The Virtual PCI bus has only one root */
  158. return publish_cb(pdev, 0, 0);
  159. }
  160. void xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
  161. {
  162. int slot;
  163. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  164. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  165. struct pci_dev_entry *e, *tmp;
  166. list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
  167. list) {
  168. list_del(&e->list);
  169. pcistub_put_pci_dev(e->dev);
  170. kfree(e);
  171. }
  172. }
  173. kfree(vpci_dev);
  174. pdev->pci_dev_data = NULL;
  175. }
  176. int xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
  177. struct xen_pcibk_device *pdev,
  178. unsigned int *domain, unsigned int *bus,
  179. unsigned int *devfn)
  180. {
  181. struct pci_dev_entry *entry;
  182. struct pci_dev *dev = NULL;
  183. struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
  184. unsigned long flags;
  185. int found = 0, slot;
  186. spin_lock_irqsave(&vpci_dev->lock, flags);
  187. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  188. list_for_each_entry(entry,
  189. &vpci_dev->dev_list[slot],
  190. list) {
  191. dev = entry->dev;
  192. if (dev && dev->bus->number == pcidev->bus->number
  193. && pci_domain_nr(dev->bus) ==
  194. pci_domain_nr(pcidev->bus)
  195. && dev->devfn == pcidev->devfn) {
  196. found = 1;
  197. *domain = 0;
  198. *bus = 0;
  199. *devfn = PCI_DEVFN(slot,
  200. PCI_FUNC(pcidev->devfn));
  201. }
  202. }
  203. }
  204. spin_unlock_irqrestore(&vpci_dev->lock, flags);
  205. return found;
  206. }