PageRenderTime 238ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/pci/slot.c

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