vpci.c 5.6 KB

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