pci_slot.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * pci_slot.c - ACPI PCI Slot Driver
  3. *
  4. * The code here is heavily leveraged from the acpiphp module.
  5. * Thanks to Matthew Wilcox <matthew@wil.cx> for much guidance.
  6. * Thanks to Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com> for code
  7. * review and fixes.
  8. *
  9. * Copyright (C) 2007-2008 Hewlett-Packard Development Company, L.P.
  10. * Alex Chiang <achiang@hp.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/pci.h>
  31. #include <linux/acpi.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #include <linux/dmi.h>
  35. static bool debug;
  36. static int check_sta_before_sun;
  37. #define DRIVER_VERSION "0.1"
  38. #define DRIVER_AUTHOR "Alex Chiang <achiang@hp.com>"
  39. #define DRIVER_DESC "ACPI PCI Slot Detection Driver"
  40. MODULE_AUTHOR(DRIVER_AUTHOR);
  41. MODULE_DESCRIPTION(DRIVER_DESC);
  42. MODULE_LICENSE("GPL");
  43. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  44. module_param(debug, bool, 0644);
  45. #define _COMPONENT ACPI_PCI_COMPONENT
  46. ACPI_MODULE_NAME("pci_slot");
  47. #define MY_NAME "pci_slot"
  48. #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
  49. #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
  50. #define dbg(format, arg...) \
  51. do { \
  52. if (debug) \
  53. printk(KERN_DEBUG "%s: " format, \
  54. MY_NAME , ## arg); \
  55. } while (0)
  56. #define SLOT_NAME_SIZE 21 /* Inspired by #define in acpiphp.h */
  57. struct acpi_pci_slot {
  58. acpi_handle root_handle; /* handle of the root bridge */
  59. struct pci_slot *pci_slot; /* corresponding pci_slot */
  60. struct list_head list; /* node in the list of slots */
  61. };
  62. static int acpi_pci_slot_add(struct acpi_pci_root *root);
  63. static void acpi_pci_slot_remove(struct acpi_pci_root *root);
  64. static LIST_HEAD(slot_list);
  65. static DEFINE_MUTEX(slot_list_lock);
  66. static struct acpi_pci_driver acpi_pci_slot_driver = {
  67. .add = acpi_pci_slot_add,
  68. .remove = acpi_pci_slot_remove,
  69. };
  70. static int
  71. check_slot(acpi_handle handle, unsigned long long *sun)
  72. {
  73. int device = -1;
  74. unsigned long long adr, sta;
  75. acpi_status status;
  76. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  77. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  78. dbg("Checking slot on path: %s\n", (char *)buffer.pointer);
  79. if (check_sta_before_sun) {
  80. /* If SxFy doesn't have _STA, we just assume it's there */
  81. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  82. if (ACPI_SUCCESS(status) && !(sta & ACPI_STA_DEVICE_PRESENT))
  83. goto out;
  84. }
  85. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  86. if (ACPI_FAILURE(status)) {
  87. dbg("_ADR returned %d on %s\n", status, (char *)buffer.pointer);
  88. goto out;
  89. }
  90. /* No _SUN == not a slot == bail */
  91. status = acpi_evaluate_integer(handle, "_SUN", NULL, sun);
  92. if (ACPI_FAILURE(status)) {
  93. dbg("_SUN returned %d on %s\n", status, (char *)buffer.pointer);
  94. goto out;
  95. }
  96. device = (adr >> 16) & 0xffff;
  97. out:
  98. kfree(buffer.pointer);
  99. return device;
  100. }
  101. struct callback_args {
  102. acpi_walk_callback user_function; /* only for walk_p2p_bridge */
  103. struct pci_bus *pci_bus;
  104. acpi_handle root_handle;
  105. };
  106. /*
  107. * register_slot
  108. *
  109. * Called once for each SxFy object in the namespace. Don't worry about
  110. * calling pci_create_slot multiple times for the same pci_bus:device,
  111. * since each subsequent call simply bumps the refcount on the pci_slot.
  112. *
  113. * The number of calls to pci_destroy_slot from unregister_slot is
  114. * symmetrical.
  115. */
  116. static acpi_status
  117. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  118. {
  119. int device;
  120. unsigned long long sun;
  121. char name[SLOT_NAME_SIZE];
  122. struct acpi_pci_slot *slot;
  123. struct pci_slot *pci_slot;
  124. struct callback_args *parent_context = context;
  125. struct pci_bus *pci_bus = parent_context->pci_bus;
  126. device = check_slot(handle, &sun);
  127. if (device < 0)
  128. return AE_OK;
  129. slot = kmalloc(sizeof(*slot), GFP_KERNEL);
  130. if (!slot) {
  131. err("%s: cannot allocate memory\n", __func__);
  132. return AE_OK;
  133. }
  134. snprintf(name, sizeof(name), "%llu", sun);
  135. pci_slot = pci_create_slot(pci_bus, device, name, NULL);
  136. if (IS_ERR(pci_slot)) {
  137. err("pci_create_slot returned %ld\n", PTR_ERR(pci_slot));
  138. kfree(slot);
  139. return AE_OK;
  140. }
  141. slot->root_handle = parent_context->root_handle;
  142. slot->pci_slot = pci_slot;
  143. INIT_LIST_HEAD(&slot->list);
  144. mutex_lock(&slot_list_lock);
  145. list_add(&slot->list, &slot_list);
  146. mutex_unlock(&slot_list_lock);
  147. get_device(&pci_bus->dev);
  148. dbg("pci_slot: %p, pci_bus: %x, device: %d, name: %s\n",
  149. pci_slot, pci_bus->number, device, name);
  150. return AE_OK;
  151. }
  152. /*
  153. * walk_p2p_bridge - discover and walk p2p bridges
  154. * @handle: points to an acpi_pci_root
  155. * @context: p2p_bridge_context pointer
  156. *
  157. * Note that when we call ourselves recursively, we pass a different
  158. * value of pci_bus in the child_context.
  159. */
  160. static acpi_status
  161. walk_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  162. {
  163. int device, function;
  164. unsigned long long adr;
  165. acpi_status status;
  166. acpi_handle dummy_handle;
  167. acpi_walk_callback user_function;
  168. struct pci_dev *dev;
  169. struct pci_bus *pci_bus;
  170. struct callback_args child_context;
  171. struct callback_args *parent_context = context;
  172. pci_bus = parent_context->pci_bus;
  173. user_function = parent_context->user_function;
  174. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  175. if (ACPI_FAILURE(status))
  176. return AE_OK;
  177. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  178. if (ACPI_FAILURE(status))
  179. return AE_OK;
  180. device = (adr >> 16) & 0xffff;
  181. function = adr & 0xffff;
  182. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  183. if (!dev || !dev->subordinate)
  184. goto out;
  185. child_context.pci_bus = dev->subordinate;
  186. child_context.user_function = user_function;
  187. child_context.root_handle = parent_context->root_handle;
  188. dbg("p2p bridge walk, pci_bus = %x\n", dev->subordinate->number);
  189. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  190. user_function, NULL, &child_context, NULL);
  191. if (ACPI_FAILURE(status))
  192. goto out;
  193. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  194. walk_p2p_bridge, NULL, &child_context, NULL);
  195. out:
  196. pci_dev_put(dev);
  197. return AE_OK;
  198. }
  199. /*
  200. * walk_root_bridge - generic root bridge walker
  201. * @root: poiner of an acpi_pci_root
  202. * @user_function: user callback for slot objects
  203. *
  204. * Call user_function for all objects underneath this root bridge.
  205. * Walk p2p bridges underneath us and call user_function on those too.
  206. */
  207. static int
  208. walk_root_bridge(struct acpi_pci_root *root, acpi_walk_callback user_function)
  209. {
  210. acpi_status status;
  211. acpi_handle handle = root->device->handle;
  212. struct pci_bus *pci_bus = root->bus;
  213. struct callback_args context;
  214. context.pci_bus = pci_bus;
  215. context.user_function = user_function;
  216. context.root_handle = handle;
  217. dbg("root bridge walk, pci_bus = %x\n", pci_bus->number);
  218. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  219. user_function, NULL, &context, NULL);
  220. if (ACPI_FAILURE(status))
  221. return status;
  222. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  223. walk_p2p_bridge, NULL, &context, NULL);
  224. if (ACPI_FAILURE(status))
  225. err("%s: walk_p2p_bridge failure - %d\n", __func__, status);
  226. return status;
  227. }
  228. /*
  229. * acpi_pci_slot_add
  230. * @handle: points to an acpi_pci_root
  231. */
  232. static int
  233. acpi_pci_slot_add(struct acpi_pci_root *root)
  234. {
  235. acpi_status status;
  236. status = walk_root_bridge(root, register_slot);
  237. if (ACPI_FAILURE(status))
  238. err("%s: register_slot failure - %d\n", __func__, status);
  239. return status;
  240. }
  241. /*
  242. * acpi_pci_slot_remove
  243. * @handle: points to an acpi_pci_root
  244. */
  245. static void
  246. acpi_pci_slot_remove(struct acpi_pci_root *root)
  247. {
  248. struct acpi_pci_slot *slot, *tmp;
  249. struct pci_bus *pbus;
  250. acpi_handle handle = root->device->handle;
  251. mutex_lock(&slot_list_lock);
  252. list_for_each_entry_safe(slot, tmp, &slot_list, list) {
  253. if (slot->root_handle == handle) {
  254. list_del(&slot->list);
  255. pbus = slot->pci_slot->bus;
  256. pci_destroy_slot(slot->pci_slot);
  257. put_device(&pbus->dev);
  258. kfree(slot);
  259. }
  260. }
  261. mutex_unlock(&slot_list_lock);
  262. }
  263. static int do_sta_before_sun(const struct dmi_system_id *d)
  264. {
  265. info("%s detected: will evaluate _STA before calling _SUN\n", d->ident);
  266. check_sta_before_sun = 1;
  267. return 0;
  268. }
  269. static struct dmi_system_id acpi_pci_slot_dmi_table[] __initdata = {
  270. /*
  271. * Fujitsu Primequest machines will return 1023 to indicate an
  272. * error if the _SUN method is evaluated on SxFy objects that
  273. * are not present (as indicated by _STA), so for those machines,
  274. * we want to check _STA before evaluating _SUN.
  275. */
  276. {
  277. .callback = do_sta_before_sun,
  278. .ident = "Fujitsu PRIMEQUEST",
  279. .matches = {
  280. DMI_MATCH(DMI_BIOS_VENDOR, "FUJITSU LIMITED"),
  281. DMI_MATCH(DMI_BIOS_VERSION, "PRIMEQUEST"),
  282. },
  283. },
  284. {}
  285. };
  286. static int __init
  287. acpi_pci_slot_init(void)
  288. {
  289. dmi_check_system(acpi_pci_slot_dmi_table);
  290. acpi_pci_register_driver(&acpi_pci_slot_driver);
  291. return 0;
  292. }
  293. static void __exit
  294. acpi_pci_slot_exit(void)
  295. {
  296. acpi_pci_unregister_driver(&acpi_pci_slot_driver);
  297. }
  298. module_init(acpi_pci_slot_init);
  299. module_exit(acpi_pci_slot_exit);