slot.c 11 KB

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