slot.c 11 KB

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