PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/pci/slot.c

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