slot.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * drivers/pci/slot.c
  3. * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
  4. * Copyright (C) 2006-2008 Hewlett-Packard Development Company, L.P.
  5. * Alex Chiang <achiang@hp.com>
  6. */
  7. #include <linux/kobject.h>
  8. #include <linux/pci.h>
  9. #include <linux/err.h>
  10. #include "pci.h"
  11. struct kset *pci_slots_kset;
  12. EXPORT_SYMBOL_GPL(pci_slots_kset);
  13. static ssize_t pci_slot_attr_show(struct kobject *kobj,
  14. struct attribute *attr, char *buf)
  15. {
  16. struct pci_slot *slot = to_pci_slot(kobj);
  17. struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
  18. return attribute->show ? attribute->show(slot, buf) : -EIO;
  19. }
  20. static ssize_t pci_slot_attr_store(struct kobject *kobj,
  21. struct attribute *attr, const char *buf, size_t len)
  22. {
  23. struct pci_slot *slot = to_pci_slot(kobj);
  24. struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
  25. return attribute->store ? attribute->store(slot, buf, len) : -EIO;
  26. }
  27. static struct sysfs_ops pci_slot_sysfs_ops = {
  28. .show = pci_slot_attr_show,
  29. .store = pci_slot_attr_store,
  30. };
  31. static ssize_t address_read_file(struct pci_slot *slot, char *buf)
  32. {
  33. if (slot->number == 0xff)
  34. return sprintf(buf, "%04x:%02x\n",
  35. pci_domain_nr(slot->bus),
  36. slot->bus->number);
  37. else
  38. return sprintf(buf, "%04x:%02x:%02x\n",
  39. pci_domain_nr(slot->bus),
  40. slot->bus->number,
  41. slot->number);
  42. }
  43. static void pci_slot_release(struct kobject *kobj)
  44. {
  45. struct pci_dev *dev;
  46. struct pci_slot *slot = to_pci_slot(kobj);
  47. pr_debug("%s: releasing pci_slot on %x:%d\n", __func__,
  48. slot->bus->number, slot->number);
  49. list_for_each_entry(dev, &slot->bus->devices, bus_list)
  50. if (PCI_SLOT(dev->devfn) == slot->number)
  51. dev->slot = NULL;
  52. list_del(&slot->list);
  53. kfree(slot);
  54. }
  55. static struct pci_slot_attribute pci_slot_attr_address =
  56. __ATTR(address, (S_IFREG | S_IRUGO), address_read_file, NULL);
  57. static struct attribute *pci_slot_default_attrs[] = {
  58. &pci_slot_attr_address.attr,
  59. NULL,
  60. };
  61. static struct kobj_type pci_slot_ktype = {
  62. .sysfs_ops = &pci_slot_sysfs_ops,
  63. .release = &pci_slot_release,
  64. .default_attrs = pci_slot_default_attrs,
  65. };
  66. /**
  67. * pci_create_slot - create or increment refcount for physical PCI slot
  68. * @parent: struct pci_bus of parent bridge
  69. * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
  70. * @name: user visible string presented in /sys/bus/pci/slots/<name>
  71. *
  72. * PCI slots have first class attributes such as address, speed, width,
  73. * and a &struct pci_slot is used to manage them. This interface will
  74. * either return a new &struct pci_slot to the caller, or if the pci_slot
  75. * already exists, its refcount will be incremented.
  76. *
  77. * Slots are uniquely identified by a @pci_bus, @slot_nr, @name tuple.
  78. *
  79. * Placeholder slots:
  80. * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
  81. * a slot. There is one notable exception - pSeries (rpaphp), where the
  82. * @slot_nr cannot be determined until a device is actually inserted into
  83. * the slot. In this scenario, the caller may pass -1 for @slot_nr.
  84. *
  85. * The following semantics are imposed when the caller passes @slot_nr ==
  86. * -1. First, the check for existing %struct pci_slot is skipped, as the
  87. * caller may know about several unpopulated slots on a given %struct
  88. * pci_bus, and each slot would have a @slot_nr of -1. Uniqueness for
  89. * these slots is then determined by the @name parameter. We expect
  90. * kobject_init_and_add() to warn us if the caller attempts to create
  91. * multiple slots with the same name. The other change in semantics is
  92. * user-visible, which is the 'address' parameter presented in sysfs will
  93. * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
  94. * %struct pci_bus and bb is the bus number. In other words, the devfn of
  95. * the 'placeholder' slot will not be displayed.
  96. */
  97. struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
  98. const char *name)
  99. {
  100. struct pci_dev *dev;
  101. struct pci_slot *slot;
  102. int err;
  103. down_write(&pci_bus_sem);
  104. if (slot_nr == -1)
  105. goto placeholder;
  106. /* If we've already created this slot, bump refcount and return. */
  107. list_for_each_entry(slot, &parent->slots, list) {
  108. if (slot->number == slot_nr) {
  109. kobject_get(&slot->kobj);
  110. pr_debug("%s: inc refcount to %d on %04x:%02x:%02x\n",
  111. __func__,
  112. atomic_read(&slot->kobj.kref.refcount),
  113. pci_domain_nr(parent), parent->number,
  114. slot_nr);
  115. goto out;
  116. }
  117. }
  118. placeholder:
  119. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  120. if (!slot) {
  121. slot = ERR_PTR(-ENOMEM);
  122. goto out;
  123. }
  124. slot->bus = parent;
  125. slot->number = slot_nr;
  126. slot->kobj.kset = pci_slots_kset;
  127. err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
  128. "%s", name);
  129. if (err) {
  130. printk(KERN_ERR "Unable to register kobject %s\n", name);
  131. goto err;
  132. }
  133. INIT_LIST_HEAD(&slot->list);
  134. list_add(&slot->list, &parent->slots);
  135. list_for_each_entry(dev, &parent->devices, bus_list)
  136. if (PCI_SLOT(dev->devfn) == slot_nr)
  137. dev->slot = slot;
  138. /* Don't care if debug printk has a -1 for slot_nr */
  139. pr_debug("%s: created pci_slot on %04x:%02x:%02x\n",
  140. __func__, pci_domain_nr(parent), parent->number, slot_nr);
  141. out:
  142. up_write(&pci_bus_sem);
  143. return slot;
  144. err:
  145. kfree(slot);
  146. slot = ERR_PTR(err);
  147. goto out;
  148. }
  149. EXPORT_SYMBOL_GPL(pci_create_slot);
  150. /**
  151. * pci_update_slot_number - update %struct pci_slot -> number
  152. * @slot - %struct pci_slot to update
  153. * @slot_nr - new number for slot
  154. *
  155. * The primary purpose of this interface is to allow callers who earlier
  156. * created a placeholder slot in pci_create_slot() by passing a -1 as
  157. * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
  158. */
  159. void pci_update_slot_number(struct pci_slot *slot, int slot_nr)
  160. {
  161. int name_count = 0;
  162. struct pci_slot *tmp;
  163. down_write(&pci_bus_sem);
  164. list_for_each_entry(tmp, &slot->bus->slots, list) {
  165. WARN_ON(tmp->number == slot_nr);
  166. if (!strcmp(kobject_name(&tmp->kobj), kobject_name(&slot->kobj)))
  167. name_count++;
  168. }
  169. if (name_count > 1)
  170. printk(KERN_WARNING "pci_update_slot_number found %d slots with the same name: %s\n", name_count, kobject_name(&slot->kobj));
  171. slot->number = slot_nr;
  172. up_write(&pci_bus_sem);
  173. }
  174. EXPORT_SYMBOL_GPL(pci_update_slot_number);
  175. /**
  176. * pci_destroy_slot - decrement refcount for physical PCI slot
  177. * @slot: struct pci_slot to decrement
  178. *
  179. * %struct pci_slot is refcounted, so destroying them is really easy; we
  180. * just call kobject_put on its kobj and let our release methods do the
  181. * rest.
  182. */
  183. void pci_destroy_slot(struct pci_slot *slot)
  184. {
  185. pr_debug("%s: dec refcount to %d on %04x:%02x:%02x\n", __func__,
  186. atomic_read(&slot->kobj.kref.refcount) - 1,
  187. pci_domain_nr(slot->bus), slot->bus->number, slot->number);
  188. down_write(&pci_bus_sem);
  189. kobject_put(&slot->kobj);
  190. up_write(&pci_bus_sem);
  191. }
  192. EXPORT_SYMBOL_GPL(pci_destroy_slot);
  193. static int pci_slot_init(void)
  194. {
  195. struct kset *pci_bus_kset;
  196. pci_bus_kset = bus_get_kset(&pci_bus_type);
  197. pci_slots_kset = kset_create_and_add("slots", NULL,
  198. &pci_bus_kset->kobj);
  199. if (!pci_slots_kset) {
  200. printk(KERN_ERR "PCI: Slot initialization failure\n");
  201. return -ENOMEM;
  202. }
  203. return 0;
  204. }
  205. subsys_initcall(pci_slot_init);