PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/pci/slot.c

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