slot.c 6.7 KB

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