slot.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/err.h>
  12. #include "pci.h"
  13. struct kset *pci_slots_kset;
  14. EXPORT_SYMBOL_GPL(pci_slots_kset);
  15. static ssize_t pci_slot_attr_show(struct kobject *kobj,
  16. struct attribute *attr, char *buf)
  17. {
  18. struct pci_slot *slot = to_pci_slot(kobj);
  19. struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
  20. return attribute->show ? attribute->show(slot, buf) : -EIO;
  21. }
  22. static ssize_t pci_slot_attr_store(struct kobject *kobj,
  23. struct attribute *attr, const char *buf, size_t len)
  24. {
  25. struct pci_slot *slot = to_pci_slot(kobj);
  26. struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
  27. return attribute->store ? attribute->store(slot, buf, len) : -EIO;
  28. }
  29. static const struct sysfs_ops pci_slot_sysfs_ops = {
  30. .show = pci_slot_attr_show,
  31. .store = pci_slot_attr_store,
  32. };
  33. static ssize_t address_read_file(struct pci_slot *slot, char *buf)
  34. {
  35. if (slot->number == 0xff)
  36. return sysfs_emit(buf, "%04x:%02x\n",
  37. pci_domain_nr(slot->bus),
  38. slot->bus->number);
  39. return sysfs_emit(buf, "%04x:%02x:%02x\n",
  40. pci_domain_nr(slot->bus),
  41. slot->bus->number,
  42. slot->number);
  43. }
  44. static ssize_t bus_speed_read(enum pci_bus_speed speed, char *buf)
  45. {
  46. return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
  47. }
  48. static ssize_t max_speed_read_file(struct pci_slot *slot, char *buf)
  49. {
  50. return bus_speed_read(slot->bus->max_bus_speed, buf);
  51. }
  52. static ssize_t cur_speed_read_file(struct pci_slot *slot, char *buf)
  53. {
  54. return bus_speed_read(slot->bus->cur_bus_speed, buf);
  55. }
  56. static void pci_slot_release(struct kobject *kobj)
  57. {
  58. struct pci_dev *dev;
  59. struct pci_slot *slot = to_pci_slot(kobj);
  60. dev_dbg(&slot->bus->dev, "dev %02x, released physical slot %s\n",
  61. slot->number, pci_slot_name(slot));
  62. down_read(&pci_bus_sem);
  63. list_for_each_entry(dev, &slot->bus->devices, bus_list)
  64. if (PCI_SLOT(dev->devfn) == slot->number)
  65. dev->slot = NULL;
  66. up_read(&pci_bus_sem);
  67. list_del(&slot->list);
  68. pci_bus_put(slot->bus);
  69. kfree(slot);
  70. }
  71. static struct pci_slot_attribute pci_slot_attr_address =
  72. __ATTR(address, S_IRUGO, address_read_file, NULL);
  73. static struct pci_slot_attribute pci_slot_attr_max_speed =
  74. __ATTR(max_bus_speed, S_IRUGO, max_speed_read_file, NULL);
  75. static struct pci_slot_attribute pci_slot_attr_cur_speed =
  76. __ATTR(cur_bus_speed, S_IRUGO, cur_speed_read_file, NULL);
  77. static struct attribute *pci_slot_default_attrs[] = {
  78. &pci_slot_attr_address.attr,
  79. &pci_slot_attr_max_speed.attr,
  80. &pci_slot_attr_cur_speed.attr,
  81. NULL,
  82. };
  83. ATTRIBUTE_GROUPS(pci_slot_default);
  84. static const struct kobj_type pci_slot_ktype = {
  85. .sysfs_ops = &pci_slot_sysfs_ops,
  86. .release = &pci_slot_release,
  87. .default_groups = pci_slot_default_groups,
  88. };
  89. static char *make_slot_name(const char *name)
  90. {
  91. char *new_name;
  92. int len, max, dup;
  93. new_name = kstrdup(name, GFP_KERNEL);
  94. if (!new_name)
  95. return NULL;
  96. /*
  97. * Make sure we hit the realloc case the first time through the
  98. * loop. 'len' will be strlen(name) + 3 at that point which is
  99. * enough space for "name-X" and the trailing NUL.
  100. */
  101. len = strlen(name) + 2;
  102. max = 1;
  103. dup = 1;
  104. for (;;) {
  105. struct kobject *dup_slot;
  106. dup_slot = kset_find_obj(pci_slots_kset, new_name);
  107. if (!dup_slot)
  108. break;
  109. kobject_put(dup_slot);
  110. if (dup == max) {
  111. len++;
  112. max *= 10;
  113. kfree(new_name);
  114. new_name = kmalloc(len, GFP_KERNEL);
  115. if (!new_name)
  116. break;
  117. }
  118. sprintf(new_name, "%s-%d", name, dup++);
  119. }
  120. return new_name;
  121. }
  122. static int rename_slot(struct pci_slot *slot, const char *name)
  123. {
  124. int result = 0;
  125. char *slot_name;
  126. if (strcmp(pci_slot_name(slot), name) == 0)
  127. return result;
  128. slot_name = make_slot_name(name);
  129. if (!slot_name)
  130. return -ENOMEM;
  131. result = kobject_rename(&slot->kobj, slot_name);
  132. kfree(slot_name);
  133. return result;
  134. }
  135. void pci_dev_assign_slot(struct pci_dev *dev)
  136. {
  137. struct pci_slot *slot;
  138. mutex_lock(&pci_slot_mutex);
  139. list_for_each_entry(slot, &dev->bus->slots, list)
  140. if (PCI_SLOT(dev->devfn) == slot->number)
  141. dev->slot = slot;
  142. mutex_unlock(&pci_slot_mutex);
  143. }
  144. static struct pci_slot *get_slot(struct pci_bus *parent, int slot_nr)
  145. {
  146. struct pci_slot *slot;
  147. /* We already hold pci_slot_mutex */
  148. list_for_each_entry(slot, &parent->slots, list)
  149. if (slot->number == slot_nr) {
  150. kobject_get(&slot->kobj);
  151. return slot;
  152. }
  153. return NULL;
  154. }
  155. /**
  156. * pci_create_slot - create or increment refcount for physical PCI slot
  157. * @parent: struct pci_bus of parent bridge
  158. * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
  159. * @name: user visible string presented in /sys/bus/pci/slots/<name>
  160. * @hotplug: set if caller is hotplug driver, NULL otherwise
  161. *
  162. * PCI slots have first class attributes such as address, speed, width,
  163. * and a &struct pci_slot is used to manage them. This interface will
  164. * either return a new &struct pci_slot to the caller, or if the pci_slot
  165. * already exists, its refcount will be incremented.
  166. *
  167. * Slots are uniquely identified by a @pci_bus, @slot_nr tuple.
  168. *
  169. * There are known platforms with broken firmware that assign the same
  170. * name to multiple slots. Workaround these broken platforms by renaming
  171. * the slots on behalf of the caller. If firmware assigns name N to
  172. * multiple slots:
  173. *
  174. * The first slot is assigned N
  175. * The second slot is assigned N-1
  176. * The third slot is assigned N-2
  177. * etc.
  178. *
  179. * Placeholder slots:
  180. * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
  181. * a slot. There is one notable exception - pSeries (rpaphp), where the
  182. * @slot_nr cannot be determined until a device is actually inserted into
  183. * the slot. In this scenario, the caller may pass -1 for @slot_nr.
  184. *
  185. * The following semantics are imposed when the caller passes @slot_nr ==
  186. * -1. First, we no longer check for an existing %struct pci_slot, as there
  187. * may be many slots with @slot_nr of -1. The other change in semantics is
  188. * user-visible, which is the 'address' parameter presented in sysfs will
  189. * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
  190. * %struct pci_bus and bb is the bus number. In other words, the devfn of
  191. * the 'placeholder' slot will not be displayed.
  192. */
  193. struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
  194. const char *name,
  195. struct hotplug_slot *hotplug)
  196. {
  197. struct pci_dev *dev;
  198. struct pci_slot *slot;
  199. int err = 0;
  200. char *slot_name = NULL;
  201. mutex_lock(&pci_slot_mutex);
  202. if (slot_nr == -1)
  203. goto placeholder;
  204. /*
  205. * Hotplug drivers are allowed to rename an existing slot,
  206. * but only if not already claimed.
  207. */
  208. slot = get_slot(parent, slot_nr);
  209. if (slot) {
  210. if (hotplug) {
  211. if ((err = slot->hotplug ? -EBUSY : 0)
  212. || (err = rename_slot(slot, name))) {
  213. kobject_put(&slot->kobj);
  214. slot = NULL;
  215. goto err;
  216. }
  217. }
  218. goto out;
  219. }
  220. placeholder:
  221. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  222. if (!slot) {
  223. err = -ENOMEM;
  224. goto err;
  225. }
  226. slot->bus = pci_bus_get(parent);
  227. slot->number = slot_nr;
  228. slot->kobj.kset = pci_slots_kset;
  229. slot_name = make_slot_name(name);
  230. if (!slot_name) {
  231. err = -ENOMEM;
  232. pci_bus_put(slot->bus);
  233. kfree(slot);
  234. goto err;
  235. }
  236. INIT_LIST_HEAD(&slot->list);
  237. list_add(&slot->list, &parent->slots);
  238. err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
  239. "%s", slot_name);
  240. if (err) {
  241. kobject_put(&slot->kobj);
  242. goto err;
  243. }
  244. down_read(&pci_bus_sem);
  245. list_for_each_entry(dev, &parent->devices, bus_list)
  246. if (PCI_SLOT(dev->devfn) == slot_nr)
  247. dev->slot = slot;
  248. up_read(&pci_bus_sem);
  249. dev_dbg(&parent->dev, "dev %02x, created physical slot %s\n",
  250. slot_nr, pci_slot_name(slot));
  251. out:
  252. kfree(slot_name);
  253. mutex_unlock(&pci_slot_mutex);
  254. return slot;
  255. err:
  256. slot = ERR_PTR(err);
  257. goto out;
  258. }
  259. EXPORT_SYMBOL_GPL(pci_create_slot);
  260. /**
  261. * pci_destroy_slot - decrement refcount for physical PCI slot
  262. * @slot: struct pci_slot to decrement
  263. *
  264. * %struct pci_slot is refcounted, so destroying them is really easy; we
  265. * just call kobject_put on its kobj and let our release methods do the
  266. * rest.
  267. */
  268. void pci_destroy_slot(struct pci_slot *slot)
  269. {
  270. dev_dbg(&slot->bus->dev, "dev %02x, dec refcount to %d\n",
  271. slot->number, kref_read(&slot->kobj.kref) - 1);
  272. mutex_lock(&pci_slot_mutex);
  273. kobject_put(&slot->kobj);
  274. mutex_unlock(&pci_slot_mutex);
  275. }
  276. EXPORT_SYMBOL_GPL(pci_destroy_slot);
  277. #if defined(CONFIG_HOTPLUG_PCI) || defined(CONFIG_HOTPLUG_PCI_MODULE)
  278. #include <linux/pci_hotplug.h>
  279. /**
  280. * pci_hp_create_module_link - create symbolic link to hotplug driver module
  281. * @pci_slot: struct pci_slot
  282. *
  283. * Helper function for pci_hotplug_core.c to create symbolic link to
  284. * the hotplug driver module.
  285. */
  286. void pci_hp_create_module_link(struct pci_slot *pci_slot)
  287. {
  288. struct hotplug_slot *slot = pci_slot->hotplug;
  289. struct kobject *kobj = NULL;
  290. int ret;
  291. if (!slot || !slot->ops)
  292. return;
  293. kobj = kset_find_obj(module_kset, slot->mod_name);
  294. if (!kobj)
  295. return;
  296. ret = sysfs_create_link(&pci_slot->kobj, kobj, "module");
  297. if (ret)
  298. dev_err(&pci_slot->bus->dev, "Error creating sysfs link (%d)\n",
  299. ret);
  300. kobject_put(kobj);
  301. }
  302. EXPORT_SYMBOL_GPL(pci_hp_create_module_link);
  303. /**
  304. * pci_hp_remove_module_link - remove symbolic link to the hotplug driver
  305. * module.
  306. * @pci_slot: struct pci_slot
  307. *
  308. * Helper function for pci_hotplug_core.c to remove symbolic link to
  309. * the hotplug driver module.
  310. */
  311. void pci_hp_remove_module_link(struct pci_slot *pci_slot)
  312. {
  313. sysfs_remove_link(&pci_slot->kobj, "module");
  314. }
  315. EXPORT_SYMBOL_GPL(pci_hp_remove_module_link);
  316. #endif
  317. static int pci_slot_init(void)
  318. {
  319. struct kset *pci_bus_kset;
  320. pci_bus_kset = bus_get_kset(&pci_bus_type);
  321. pci_slots_kset = kset_create_and_add("slots", NULL,
  322. &pci_bus_kset->kobj);
  323. if (!pci_slots_kset) {
  324. pr_err("PCI: Slot initialization failure\n");
  325. return -ENOMEM;
  326. }
  327. return 0;
  328. }
  329. subsys_initcall(pci_slot_init);