slot.c 11 KB

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