slot.c 11 KB

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