slot.c 11 KB

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