slot.c 12 KB

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