slot.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * drivers/pci/slot.c
  3. * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
  4. * Copyright (C) 2006-2009 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. dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
  48. slot->number, pci_slot_name(slot));
  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. static char *make_slot_name(const char *name)
  67. {
  68. char *new_name;
  69. int len, max, dup;
  70. new_name = kstrdup(name, GFP_KERNEL);
  71. if (!new_name)
  72. return NULL;
  73. /*
  74. * Make sure we hit the realloc case the first time through the
  75. * loop. 'len' will be strlen(name) + 3 at that point which is
  76. * enough space for "name-X" and the trailing NUL.
  77. */
  78. len = strlen(name) + 2;
  79. max = 1;
  80. dup = 1;
  81. for (;;) {
  82. struct kobject *dup_slot;
  83. dup_slot = kset_find_obj(pci_slots_kset, new_name);
  84. if (!dup_slot)
  85. break;
  86. kobject_put(dup_slot);
  87. if (dup == max) {
  88. len++;
  89. max *= 10;
  90. kfree(new_name);
  91. new_name = kmalloc(len, GFP_KERNEL);
  92. if (!new_name)
  93. break;
  94. }
  95. sprintf(new_name, "%s-%d", name, dup++);
  96. }
  97. return new_name;
  98. }
  99. static int rename_slot(struct pci_slot *slot, const char *name)
  100. {
  101. int result = 0;
  102. char *slot_name;
  103. if (strcmp(pci_slot_name(slot), name) == 0)
  104. return result;
  105. slot_name = make_slot_name(name);
  106. if (!slot_name)
  107. return -ENOMEM;
  108. result = kobject_rename(&slot->kobj, slot_name);
  109. kfree(slot_name);
  110. return result;
  111. }
  112. static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
  113. {
  114. struct pci_slot *slot;
  115. /*
  116. * We already hold pci_bus_sem so don't worry
  117. */
  118. list_for_each_entry(slot, &parent->slots, list)
  119. if (slot->number == slot_nr) {
  120. kobject_get(&slot->kobj);
  121. return slot;
  122. }
  123. return NULL;
  124. }
  125. /**
  126. * pci_create_slot - create or increment refcount for physical PCI slot
  127. * @parent: struct pci_bus of parent bridge
  128. * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
  129. * @name: user visible string presented in /sys/bus/pci/slots/<name>
  130. * @hotplug: set if caller is hotplug driver, NULL otherwise
  131. *
  132. * PCI slots have first class attributes such as address, speed, width,
  133. * and a &struct pci_slot is used to manage them. This interface will
  134. * either return a new &struct pci_slot to the caller, or if the pci_slot
  135. * already exists, its refcount will be incremented.
  136. *
  137. * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
  138. *
  139. * There are known platforms with broken firmware that assign the same
  140. * name to multiple slots. Workaround these broken platforms by renaming
  141. * the slots on behalf of the caller. If firmware assigns name N to
  142. * multiple slots:
  143. *
  144. * The first slot is assigned N
  145. * The second slot is assigned N-1
  146. * The third slot is assigned N-2
  147. * etc.
  148. *
  149. * Placeholder slots:
  150. * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
  151. * a slot. There is one notable exception - pSeries (rpaphp), where the
  152. * @slot_nr cannot be determined until a device is actually inserted into
  153. * the slot. In this scenario, the caller may pass -1 for @slot_nr.
  154. *
  155. * The following semantics are imposed when the caller passes @slot_nr ==
  156. * -1. First, we no longer check for an existing %struct pci_slot, as there
  157. * may be many slots with @slot_nr of -1. The other change in semantics is
  158. * user-visible, which is the 'address' parameter presented in sysfs will
  159. * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
  160. * %struct pci_bus and bb is the bus number. In other words, the devfn of
  161. * the 'placeholder' slot will not be displayed.
  162. */
  163. struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
  164. const char *name,
  165. struct hotplug_slot *hotplug)
  166. {
  167. struct pci_dev *dev;
  168. struct pci_slot *slot;
  169. int err = 0;
  170. char *slot_name = NULL;
  171. down_write(&pci_bus_sem);
  172. if (slot_nr == -1)
  173. goto placeholder;
  174. /*
  175. * Hotplug drivers are allowed to rename an existing slot,
  176. * but only if not already claimed.
  177. */
  178. slot = get_slot(parent, slot_nr);
  179. if (slot) {
  180. if (hotplug) {
  181. if ((err = slot->hotplug ? -EBUSY : 0)
  182. || (err = rename_slot(slot, name))) {
  183. kobject_put(&slot->kobj);
  184. slot = NULL;
  185. goto err;
  186. }
  187. }
  188. goto out;
  189. }
  190. placeholder:
  191. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  192. if (!slot) {
  193. err = -ENOMEM;
  194. goto err;
  195. }
  196. slot->bus = parent;
  197. slot->number = slot_nr;
  198. slot->kobj.kset = pci_slots_kset;
  199. slot_name = make_slot_name(name);
  200. if (!slot_name) {
  201. err = -ENOMEM;
  202. goto err;
  203. }
  204. err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
  205. "%s", slot_name);
  206. if (err)
  207. goto err;
  208. INIT_LIST_HEAD(&slot->list);
  209. list_add(&slot->list, &parent->slots);
  210. list_for_each_entry(dev, &parent->devices, bus_list)
  211. if (PCI_SLOT(dev->devfn) == slot_nr)
  212. dev->slot = slot;
  213. dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
  214. slot_nr, pci_slot_name(slot));
  215. out:
  216. kfree(slot_name);
  217. up_write(&pci_bus_sem);
  218. return slot;
  219. err:
  220. kfree(slot);
  221. slot = ERR_PTR(err);
  222. goto out;
  223. }
  224. EXPORT_SYMBOL_GPL(pci_create_slot);
  225. /**
  226. * pci_renumber_slot - update %struct pci_slot -> number
  227. * @slot: &struct pci_slot to update
  228. * @slot_nr: new number for slot
  229. *
  230. * The primary purpose of this interface is to allow callers who earlier
  231. * created a placeholder slot in pci_create_slot() by passing a -1 as
  232. * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
  233. */
  234. void pci_renumber_slot(struct pci_slot *slot, int slot_nr)
  235. {
  236. struct pci_slot *tmp;
  237. down_write(&pci_bus_sem);
  238. list_for_each_entry(tmp, &slot->bus->slots, list) {
  239. WARN_ON(tmp->number == slot_nr);
  240. goto out;
  241. }
  242. slot->number = slot_nr;
  243. out:
  244. up_write(&pci_bus_sem);
  245. }
  246. EXPORT_SYMBOL_GPL(pci_renumber_slot);
  247. /**
  248. * pci_destroy_slot - decrement refcount for physical PCI slot
  249. * @slot: struct pci_slot to decrement
  250. *
  251. * %struct pci_slot is refcounted, so destroying them is really easy; we
  252. * just call kobject_put on its kobj and let our release methods do the
  253. * rest.
  254. */
  255. void pci_destroy_slot(struct pci_slot *slot)
  256. {
  257. dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
  258. slot->number, atomic_read(&slot->kobj.kref.refcount) - 1);
  259. down_write(&pci_bus_sem);
  260. kobject_put(&slot->kobj);
  261. up_write(&pci_bus_sem);
  262. }
  263. EXPORT_SYMBOL_GPL(pci_destroy_slot);
  264. #if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
  265. #include <linux/pci_hotplug.h>
  266. /**
  267. * pci_hp_create_link - create symbolic link to the hotplug driver module.
  268. * @pci_slot: struct pci_slot
  269. *
  270. * Helper function for pci_hotplug_core.c to create symbolic link to
  271. * the hotplug driver module.
  272. */
  273. void pci_hp_create_module_link(struct pci_slot *pci_slot)
  274. {
  275. struct hotplug_slot *slot = pci_slot->hotplug;
  276. struct kobject *kobj = NULL;
  277. int no_warn;
  278. if (!slot || !slot->ops)
  279. return;
  280. kobj = kset_find_obj(module_kset, slot->ops->mod_name);
  281. if (!kobj)
  282. return;
  283. no_warn = sysfs_create_link(&pci_slot->kobj, kobj, "module");
  284. kobject_put(kobj);
  285. }
  286. EXPORT_SYMBOL_GPL(pci_hp_create_module_link);
  287. /**
  288. * pci_hp_remove_link - remove symbolic link to the hotplug driver module.
  289. * @pci_slot: struct pci_slot
  290. *
  291. * Helper function for pci_hotplug_core.c to remove symbolic link to
  292. * the hotplug driver module.
  293. */
  294. void pci_hp_remove_module_link(struct pci_slot *pci_slot)
  295. {
  296. sysfs_remove_link(&pci_slot->kobj, "module");
  297. }
  298. EXPORT_SYMBOL_GPL(pci_hp_remove_module_link);
  299. #endif
  300. static int pci_slot_init(void)
  301. {
  302. struct kset *pci_bus_kset;
  303. pci_bus_kset = bus_get_kset(&pci_bus_type);
  304. pci_slots_kset = kset_create_and_add("slots", NULL,
  305. &pci_bus_kset->kobj);
  306. if (!pci_slots_kset) {
  307. printk(KERN_ERR "PCI: Slot initialization failure\n");
  308. return -ENOMEM;
  309. }
  310. return 0;
  311. }
  312. subsys_initcall(pci_slot_init);